Skip to content

Commit 6c4528b

Browse files
committed
use a seesion id to key the sandbox instance
1 parent 75ba7c6 commit 6c4528b

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

examples/openai-agents/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"type": "module",
1616
"dependencies": {
1717
"@openai/agents": "^0.3.2",
18+
"nanoid": "^3.3.11",
1819
"react": "^19.2.0",
1920
"react-dom": "^19.2.0"
2021
},

examples/openai-agents/src/app.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
CommandResult,
66
FileOperationResult
77
} from '@cloudflare/sandbox/openai';
8+
import { nanoid } from 'nanoid';
89

910
interface Response {
1011
naturalResponse: string | null;
@@ -19,14 +20,30 @@ interface Message {
1920
timestamp: number;
2021
}
2122

23+
/**
24+
* Get or create a session ID for this user.
25+
* The session ID is stored in localStorage and persists across browser sessions.
26+
*/
27+
function getOrCreateSessionId(): string {
28+
let sessionId = localStorage.getItem('session-id');
29+
30+
if (!sessionId) {
31+
sessionId = nanoid(8);
32+
localStorage.setItem('session-id', sessionId);
33+
}
34+
35+
return sessionId;
36+
}
37+
2238
const STORAGE_KEY = 'openai-agents-history';
2339

2440
async function makeApiCall(input: string): Promise<Response> {
2541
try {
2642
const response = await fetch('/run', {
2743
method: 'POST',
2844
headers: {
29-
'Content-Type': 'application/json'
45+
'Content-Type': 'application/json',
46+
'X-Session-Id': getOrCreateSessionId()
3047
},
3148
body: JSON.stringify({ input })
3249
});

examples/openai-agents/src/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ function getErrorStack(error: unknown): string | undefined {
3030
return undefined;
3131
}
3232

33-
async function handleRunRequest(request: Request, env: Env): Promise<Response> {
33+
async function handleRunRequest(
34+
request: Request,
35+
env: Env,
36+
sessionId: string
37+
): Promise<Response> {
3438
console.debug('[openai-example]', 'handleRunRequest called', {
3539
method: request.method,
3640
url: request.url
@@ -58,9 +62,9 @@ async function handleRunRequest(request: Request, env: Env): Promise<Response> {
5862

5963
// Get sandbox instance (reused for both shell and editor)
6064
console.debug('[openai-example]', 'Getting sandbox instance', {
61-
sessionId: 'workspace-session'
65+
sandboxId: `session-${sessionId}`
6266
});
63-
const sandbox = getSandbox(env.Sandbox, 'workspace-session');
67+
const sandbox = getSandbox(env.Sandbox, `session-${sessionId}`);
6468

6569
// Create shell (automatically collects results)
6670
console.debug('[openai-example]', 'Creating SandboxShell');
@@ -163,8 +167,18 @@ export default {
163167
method: request.method
164168
});
165169

170+
const sessionId = request.headers.get('X-Session-Id');
171+
console.log({ sessionId });
172+
if (!sessionId) {
173+
return new Response('Missing X-Session-Id header', { status: 400 });
174+
}
175+
176+
if (url.pathname === '/.well-known/appspecific/com.chrome.devtools.json') {
177+
return Response.json({});
178+
}
179+
166180
if (url.pathname === '/run' && request.method === 'POST') {
167-
return handleRunRequest(request, env);
181+
return handleRunRequest(request, env, sessionId);
168182
}
169183

170184
console.warn('[openai-example]', 'Route not found', {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)