|
| 1 | +import OpenAI from 'openai'; |
| 2 | +import { getSandbox } from '@cloudflare/sandbox'; |
| 3 | + |
| 4 | +export { Sandbox } from '@cloudflare/sandbox'; |
| 5 | + |
| 6 | +const API_PATH = '/foo'; |
| 7 | +const MODEL = '@cf/openai/gpt-oss-120b'; |
| 8 | + |
| 9 | +type AIResponse = OpenAI.Responses.Response; |
| 10 | +type ResponseInputItem = OpenAI.Responses.ResponseInputItem; |
| 11 | +type FunctionTool = OpenAI.Responses.FunctionTool; |
| 12 | +type FunctionCall = OpenAI.Responses.ResponseFunctionToolCall; |
| 13 | + |
| 14 | +interface SandboxResult { |
| 15 | + results?: Array<{ text?: string; html?: string; [key: string]: any }>; |
| 16 | + logs?: { stdout?: string[]; stderr?: string[] }; |
| 17 | + error?: string; |
| 18 | +} |
| 19 | + |
| 20 | +async function callCloudflareAPI( |
| 21 | + env: Env, |
| 22 | + input: ResponseInputItem[], |
| 23 | + tools?: FunctionTool[], |
| 24 | + toolChoice: string = 'auto', |
| 25 | +): Promise<AIResponse> { |
| 26 | + const response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${env.CLOUDFLARE_ACCOUNT_ID}/ai/v1/responses`, { |
| 27 | + method: 'POST', |
| 28 | + headers: { |
| 29 | + 'Content-Type': 'application/json', |
| 30 | + Authorization: `Bearer ${env.CLOUDFLARE_API_KEY}`, |
| 31 | + }, |
| 32 | + body: JSON.stringify({ |
| 33 | + model: MODEL, |
| 34 | + input, |
| 35 | + ...(tools && { tools, tool_choice: toolChoice }), |
| 36 | + }), |
| 37 | + }); |
| 38 | + |
| 39 | + if (!response.ok) { |
| 40 | + const errorText = await response.text(); |
| 41 | + throw new Error(`API call failed: ${response.status} - ${errorText}`); |
| 42 | + } |
| 43 | + |
| 44 | + return response.json() as Promise<AIResponse>; |
| 45 | +} |
| 46 | + |
| 47 | +async function executePythonCode(env: Env, code: string): Promise<string> { |
| 48 | + const sandboxId = env.Sandbox.idFromName('default'); |
| 49 | + const sandbox = getSandbox(env.Sandbox, sandboxId.toString()); |
| 50 | + const pythonCtx = await sandbox.createCodeContext({ language: 'python' }); |
| 51 | + const result = (await sandbox.runCode(code, { context: pythonCtx })) as SandboxResult; |
| 52 | + |
| 53 | + // Extract output from results (expressions) |
| 54 | + if (result.results?.length) { |
| 55 | + const outputs = result.results.map((r) => r.text || r.html || JSON.stringify(r)).filter(Boolean); |
| 56 | + if (outputs.length) return outputs.join('\n'); |
| 57 | + } |
| 58 | + |
| 59 | + // Extract output from logs |
| 60 | + let output = ''; |
| 61 | + if (result.logs?.stdout?.length) { |
| 62 | + output = result.logs.stdout.join('\n'); |
| 63 | + } |
| 64 | + if (result.logs?.stderr?.length) { |
| 65 | + if (output) output += '\n'; |
| 66 | + output += 'Error: ' + result.logs.stderr.join('\n'); |
| 67 | + } |
| 68 | + |
| 69 | + return result.error ? `Error: ${result.error}` : output || 'Code executed successfully'; |
| 70 | +} |
| 71 | + |
| 72 | +async function handleAIRequest(input: string, env: Env): Promise<string> { |
| 73 | + const pythonTool: FunctionTool = { |
| 74 | + type: 'function', |
| 75 | + name: 'execute_python', |
| 76 | + description: 'Execute Python code and return the output', |
| 77 | + parameters: { |
| 78 | + type: 'object', |
| 79 | + properties: { |
| 80 | + code: { |
| 81 | + type: 'string', |
| 82 | + description: 'The Python code to execute', |
| 83 | + }, |
| 84 | + }, |
| 85 | + required: ['code'], |
| 86 | + }, |
| 87 | + strict: null, |
| 88 | + }; |
| 89 | + |
| 90 | + // Initial AI request with Python execution tool |
| 91 | + let response = await callCloudflareAPI(env, [{ role: 'user', content: input }], [pythonTool]); |
| 92 | + |
| 93 | + // Check for function call |
| 94 | + const functionCall = response.output?.find( |
| 95 | + (item): item is FunctionCall => item.type === 'function_call' && item.name === 'execute_python', |
| 96 | + ); |
| 97 | + |
| 98 | + if (functionCall?.arguments) { |
| 99 | + try { |
| 100 | + const { code } = JSON.parse(functionCall.arguments) as { code: string }; |
| 101 | + const output = await executePythonCode(env, code); |
| 102 | + |
| 103 | + const functionResult: ResponseInputItem = { |
| 104 | + type: 'function_call_output', |
| 105 | + call_id: functionCall.call_id, |
| 106 | + output, |
| 107 | + } as OpenAI.Responses.ResponseInputItem.FunctionCallOutput; |
| 108 | + |
| 109 | + // Get final response with execution result |
| 110 | + response = await callCloudflareAPI(env, [{ role: 'user', content: input }, functionCall as ResponseInputItem, functionResult]); |
| 111 | + } catch (error) { |
| 112 | + console.error('Sandbox execution failed:', error); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Extract final response text |
| 117 | + const message = response.output?.find((item) => item.type === 'message'); |
| 118 | + const textContent = message?.content?.find((c: any) => c.type === 'output_text'); |
| 119 | + const text = textContent && 'text' in textContent ? textContent.text : undefined; |
| 120 | + |
| 121 | + return text || 'No response generated'; |
| 122 | +} |
| 123 | + |
| 124 | +export default { |
| 125 | + async fetch(request: Request, env: Env): Promise<Response> { |
| 126 | + const url = new URL(request.url); |
| 127 | + |
| 128 | + if (url.pathname !== API_PATH || request.method !== 'POST') { |
| 129 | + return new Response('Not Found', { status: 404 }); |
| 130 | + } |
| 131 | + |
| 132 | + try { |
| 133 | + const { input } = await request.json<{ input?: string }>(); |
| 134 | + |
| 135 | + if (!input) { |
| 136 | + return Response.json({ error: 'Missing input field' }, { status: 400 }); |
| 137 | + } |
| 138 | + |
| 139 | + const output = await handleAIRequest(input, env); |
| 140 | + return Response.json({ output }); |
| 141 | + } catch (error) { |
| 142 | + console.error('Request failed:', error); |
| 143 | + const message = error instanceof Error ? error.message : 'Internal Server Error'; |
| 144 | + return Response.json({ error: message }, { status: 500 }); |
| 145 | + } |
| 146 | + }, |
| 147 | +} satisfies ExportedHandler<Env>; |
0 commit comments