Skip to content

Commit ca3f59b

Browse files
committed
fix sdk session reuse example
Signed-off-by: Zhou Zihang <z@mcac.cc>
1 parent 4458b62 commit ca3f59b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

sdk-python/examples/basic_usage.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Demonstrates:
1919
1. Basic command execution and code running
2020
2. File operations
21-
3. Session reuse for state persistence (useful for AI workflows)
21+
3. Session reuse for file-system state persistence (useful for AI workflows)
2222
"""
2323

2424
from agentcube import CodeInterpreterClient
@@ -61,23 +61,26 @@ def session_reuse_example():
6161
6262
This pattern is essential for low-code/no-code platforms (like Dify)
6363
where the interpreter is invoked multiple times as a tool within a
64-
single workflow, and state needs to persist across invocations.
64+
single workflow, and file state needs to persist across invocations.
65+
66+
Note: each run_code call starts a new process, so Python variables
67+
do not persist across calls.
6568
"""
66-
print("\n=== Session Reuse (State Persistence) ===\n")
69+
print("\n=== Session Reuse (File State Persistence) ===\n")
6770

68-
# Step 1: Create session and set variable
69-
print("Step 1: Create session, set x = 42")
71+
# Step 1: Create session and write a file
72+
print("Step 1: Create session, write /tmp/value.txt = 42")
7073
client1 = CodeInterpreterClient(verbose=True)
71-
client1.run_code("python", "x = 42")
74+
client1.write_file("42", "/tmp/value.txt")
7275
session_id = client1.session_id
7376
print(f"Session ID saved: {session_id}")
7477
# Don't call stop() - let session persist
7578

76-
# Step 2: Reuse session - variable x should still exist
77-
print("\nStep 2: Reuse session, access x")
79+
# Step 2: Reuse session - file system state should still exist
80+
print("\nStep 2: Reuse session, read /tmp/value.txt")
7881
client2 = CodeInterpreterClient(session_id=session_id, verbose=True)
79-
result = client2.run_code("python", "print(f'x = {x}')")
80-
print(f"Result: {result.strip()}") # Should print "x = 42"
82+
result = client2.run_code("python", "print(open('/tmp/value.txt').read())")
83+
print(f"Result: {result.strip()}") # Should print "42"
8184

8285
# Step 3: Cleanup
8386
print("\nStep 3: Delete session")

0 commit comments

Comments
 (0)