You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is this the intended setup for a Cloudflare Worker? I'm particularly interested in how the streaming is handled.
I'm running into an issue when following the cookbook instructions for the prompt: "Before any tool, tell the user 'I will be right back'." The tool initiates correctly, but there's no response afterward—it just ends the session.
/*──────────────────────── Manual SSE chat handler ─────────────────*/
async functionhandleChat(req: Request,env: Env,ctx: ExecutionContext): Promise<Response>{/* Parse JSON body */const{ message, lastResponseId, trace_id }=(awaitreq.json())as{message: string;lastResponseId?: string;trace_id?: string;};/* Get or generate trace_id for grouping conversations */consttraceId=trace_id||`trace_${Math.random().toString(36).substring(2,34)}`;/* Initialise tracing/exporter once */awaitinitTracing(env.OPENAI_API_KEY);/* Dynamically import SDK pieces that may use crypto or fs */const{ Agent, Runner, withTrace }=awaitimport("@openai/agents");/* Build agent & runner */constagent=newAgent({name: "Site Assistant",instructions: "You will answer questions by using the search_tool(query).",outputType: "text",model: "gpt-4.1",// tools: [searchTool],});construnner=newRunner({tracingDisabled: false,// default});
let runResult: any;awaitwithTrace('cf-assistant-ui',async()=>{runResult=awaitrunner.run(agent,message,{stream: true,previousResponseId: lastResponseId,});},{traceId: traceId,groupId: traceId,// Use same ID for grouping conversations});/* Create manual SSE stream */conststream=newReadableStream({asyncstart(controller){constencoder=newTextEncoder();try{forawait(constevofrunResult){// Handle raw model stream events which contain the delta textif(ev.type==='raw_model_stream_event'){// Check if this is an output_text_delta eventif(ev.data&&ev.data.type==='output_text_delta'&&ev.data.delta){consttextDelta=(evasany).data.delta;controller.enqueue(encoder.encode(`event: text-delta\ndata: ${textDelta}\n\n`));}// Handle completionelseif(ev.data&&ev.data.type==='response_done'){controller.enqueue(encoder.encode(`event: lastResponseId\ndata: ${ev.data.response.id}\n\n`));controller.enqueue(encoder.encode(`event: trace_id\ndata: ${traceId}\n\n`));controller.enqueue(encoder.encode('event: done\ndata: [DONE]\n\n'));break;}}}}catch(error){console.error('Error processing stream:',error);controller.enqueue(encoder.encode(`event: error\ndata: ${String(error)}\n\n`));}finally{controller.close();}}});returnnewResponse(stream,{headers: {'Content-Type': 'text/event-stream','Cache-Control': 'no-cache','Connection': 'keep-alive','Access-Control-Allow-Origin': '*',}});}
I'd be happy to add the full script if you prefer. The reason for the tracing extra code was from debugging why the tracing stopped for me.
The text was updated successfully, but these errors were encountered:
Hey @sideshot! We are still improving the Cloudflare Worker setup. @threepointone has done some helpful contributions to it and #50 should further improve the situation including tracing set up.
Is this the intended setup for a Cloudflare Worker? I'm particularly interested in how the streaming is handled.
I'm running into an issue when following the cookbook instructions for the prompt: "Before any tool, tell the user 'I will be right back'." The tool initiates correctly, but there's no response afterward—it just ends the session.
I'd be happy to add the full script if you prefer. The reason for the tracing extra code was from debugging why the tracing stopped for me.
The text was updated successfully, but these errors were encountered: