| 126 | |
| 127 | // Helper function to create cached stream response |
| 128 | function createCachedStreamResponse(xml: string): Response { |
| 129 | const toolCallId = `cached-${Date.now()}` |
| 130 | |
| 131 | const stream = createUIMessageStream({ |
| 132 | execute: async ({ writer }) => { |
| 133 | writer.write({ type: "start" }) |
| 134 | writer.write({ |
| 135 | type: "tool-input-start", |
| 136 | toolCallId, |
| 137 | toolName: "display_diagram", |
| 138 | }) |
| 139 | writer.write({ |
| 140 | type: "tool-input-delta", |
| 141 | toolCallId, |
| 142 | inputTextDelta: xml, |
| 143 | }) |
| 144 | writer.write({ |
| 145 | type: "tool-input-available", |
| 146 | toolCallId, |
| 147 | toolName: "display_diagram", |
| 148 | input: { xml }, |
| 149 | }) |
| 150 | writer.write({ type: "finish" }) |
| 151 | }, |
| 152 | }) |
| 153 | |
| 154 | return createUIMessageStreamResponse({ stream }) |
| 155 | } |
| 156 | |
| 157 | // Inner handler function |
| 158 | async function handleChatRequest(req: Request): Promise<Response> { |