|
18 | 18 | Demonstrates: |
19 | 19 | 1. Basic command execution and code running |
20 | 20 | 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) |
22 | 22 | """ |
23 | 23 |
|
24 | 24 | from agentcube import CodeInterpreterClient |
@@ -61,23 +61,26 @@ def session_reuse_example(): |
61 | 61 |
|
62 | 62 | This pattern is essential for low-code/no-code platforms (like Dify) |
63 | 63 | 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. |
65 | 68 | """ |
66 | | - print("\n=== Session Reuse (State Persistence) ===\n") |
| 69 | + print("\n=== Session Reuse (File State Persistence) ===\n") |
67 | 70 |
|
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") |
70 | 73 | client1 = CodeInterpreterClient(verbose=True) |
71 | | - client1.run_code("python", "x = 42") |
| 74 | + client1.write_file("42", "/tmp/value.txt") |
72 | 75 | session_id = client1.session_id |
73 | 76 | print(f"Session ID saved: {session_id}") |
74 | 77 | # Don't call stop() - let session persist |
75 | 78 |
|
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") |
78 | 81 | 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" |
81 | 84 |
|
82 | 85 | # Step 3: Cleanup |
83 | 86 | print("\nStep 3: Delete session") |
|
0 commit comments