Repository navigation
Expand file tree
/
Copy path04_legacy_api_compatibility.py
More file actions
205 lines (162 loc) Β· 6.57 KB
/
Copy path04_legacy_api_compatibility.py
File metadata and controls
205 lines (162 loc) Β· 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
"""
Example 4: Legacy API Compatibility
This example demonstrates how the legacy blob API (store/retrieve) still works
and is now internally implemented using the unified namespace architecture.
This ensures backward compatibility for existing code while providing a clean
path forward to the unified API.
"""
import asyncio
import os
os.environ.setdefault("ARTIFACT_PROVIDER", "memory")
os.environ.setdefault("SESSION_PROVIDER", "memory")
from chuk_artifacts import ArtifactStore, StorageScope
async def main():
store = ArtifactStore()
print("=" * 70)
print("LEGACY API COMPATIBILITY")
print("=" * 70)
# ========================================================================
# Part 1: Legacy store() and retrieve()
# ========================================================================
print("\nπ¦ PART 1: LEGACY STORE() AND RETRIEVE()")
print("-" * 70)
# OLD API (still works!)
artifact_id = await store.store(
data=b"Hello from legacy API",
mime="text/plain",
summary="Test artifact",
scope=StorageScope.SESSION,
)
print(f"\nβ Stored using legacy API: {artifact_id}")
print(" This internally:")
print(" 1. Created a BLOB namespace")
print(" 2. Wrote data to /_data")
print(" 3. Returned namespace_id as artifact_id")
# Retrieve using legacy API
data = await store.retrieve(artifact_id)
print(f"\nβ Retrieved using legacy API: {data.decode()}")
print(" This internally:")
print(" 1. Reads from namespace_id")
print(" 2. Reads /_data from the blob VFS")
# ========================================================================
# Part 2: Legacy API Still Works (Separate Implementation)
# ========================================================================
print("\nπ PART 2: LEGACY API STILL WORKS")
print("-" * 70)
# Store using legacy API
legacy_id = await store.store(
data=b"Stored via legacy",
mime="application/json",
summary="Legacy artifact",
)
print(f"\nβ Stored via legacy: {legacy_id}")
# Retrieve using legacy API
legacy_data = await store.retrieve(legacy_id)
print(f"β Retrieved via legacy: {legacy_data.decode()}")
print("\n Note: Legacy API uses the old internal implementation")
print(" This is intentional for backward compatibility")
print(" Legacy artifacts != namespace artifacts (for now)")
# ========================================================================
# Part 3: Unified API Examples (New Code)
# ========================================================================
print("\nπ PART 3: UNIFIED API (NEW CODE)")
print("-" * 70)
# Use the new unified namespace API
from chuk_artifacts import NamespaceType
# Create blob namespace
blob_ns = await store.create_namespace(
type=NamespaceType.BLOB,
scope=StorageScope.SESSION,
)
print(f"\nβ Created blob namespace: {blob_ns.namespace_id}")
# Write data
await store.write_namespace(blob_ns.namespace_id, data=b"New unified blob")
# Read data
data = await store.read_namespace(blob_ns.namespace_id)
print(f"β Read data: {data.decode()}")
# Create workspace namespace
workspace_ns = await store.create_namespace(
type=NamespaceType.WORKSPACE,
name="new-project",
scope=StorageScope.SESSION,
)
print(f"\nβ Created workspace: {workspace_ns.name}")
# Write files
await store.write_namespace(
workspace_ns.namespace_id, path="/README.md", data=b"# New Project"
)
print("β Wrote README.md")
# List namespaces
namespaces = store.list_namespaces(session_id=blob_ns.session_id)
print(f"\nβ Total namespaces in session: {len(namespaces)}")
for ns in namespaces:
print(f" - {ns.type.value}: {ns.name or ns.namespace_id}")
# ========================================================================
# Part 4: Migration Path
# ========================================================================
print("\nπ PART 4: MIGRATION PATH")
print("-" * 70)
print("""
EXISTING CODE (Legacy API):
artifact_id = await store.store(data, mime="text/plain", summary="...")
data = await store.retrieve(artifact_id)
β STILL WORKS! No changes needed.
NEW CODE (Unified Namespace API):
blob = await store.create_namespace(type=NamespaceType.BLOB)
await store.write_namespace(blob.namespace_id, data=data)
data = await store.read_namespace(blob.namespace_id)
β MORE EXPLICIT, MORE POWERFUL!
BENEFITS OF NEW API:
β Explicit namespace types (BLOB vs WORKSPACE)
β Unified API for both types
β Direct VFS access
β Checkpoint support for both
β SESSION/USER/SANDBOX scoping
β Clearer semantics
RECOMMENDATION:
β Use legacy API for existing code (backward compatible)
β Use unified API for new code (more powerful)
β Both work side-by-side!
""")
# ========================================================================
# Summary
# ========================================================================
print("\n" + "=" * 70)
print("β¨ LEGACY COMPATIBILITY - SUMMARY")
print("=" * 70)
print("""
LEGACY API PRESERVED:
β store(data, mime, summary) still works
β retrieve(artifact_id) still works
β All existing code keeps working
β No breaking changes!
CURRENT STATE:
β Legacy API uses old internal implementation
β This is intentional for backward compatibility
β Legacy artifacts and namespace artifacts are separate
β Both work perfectly fine side-by-side
UNIFIED NAMESPACE API (NEW):
β create_namespace(type=BLOB|WORKSPACE)
β Explicit types and scoping
β Full VFS access
β Checkpoint support
β Consistent API for both types
RECOMMENDATION:
β Use legacy API: existing code (no changes needed)
β Use unified API: new code (more powerful, cleaner)
β Both APIs work perfectly together!
""")
# Cleanup
print("\nπ§Ή Cleaning up...")
# Cleanup unified namespaces
namespaces = store.list_namespaces(session_id=blob_ns.session_id)
for ns in namespaces:
await store.destroy_namespace(ns.namespace_id)
print(f"β Cleaned up {len(namespaces)} unified namespaces")
print(" (Legacy artifacts cleaned up automatically by session expiration)")
print("\n" + "=" * 70)
print("β LEGACY COMPATIBILITY DEMO COMPLETE")
print("=" * 70)
if __name__ == "__main__":
asyncio.run(main())