(c: any, body: RequestBody, rawLlmResponse: any)
| 64 | }; |
| 65 | |
| 66 | const processLlmResponse = (c: any, body: RequestBody, rawLlmResponse: any) => { |
| 67 | const isStreaming = body.stream; |
| 68 | |
| 69 | // Non-streaming |
| 70 | if (!isStreaming && rawLlmResponse?.choices?.length > 0) { |
| 71 | const completion = rawLlmResponse.choices[0]?.message?.content ?? ''; |
| 72 | const toolCalls = rawLlmResponse.choices[0]?.message?.tool_calls ?? []; |
| 73 | const isToolCall = toolCalls.length > 0; |
| 74 | |
| 75 | logger('tool', isToolCall, 'Tool calls found'); |
| 76 | logger('tool.calls', toolCalls); |
| 77 | logger('pipe.completion', completion, 'Pipe completion'); |
| 78 | logger('pipe.response', rawLlmResponse, 'type: (non-streaming)'); |
| 79 | |
| 80 | return c.json({ completion, ...rawLlmResponse }); |
| 81 | } |
| 82 | |
| 83 | // Streaming |
| 84 | if (isStreaming) { |
| 85 | logger('pipe.response', rawLlmResponse, 'type: (streaming)'); |
| 86 | return handleStreamingResponse({ |
| 87 | response: rawLlmResponse, |
| 88 | headers: {}, |
| 89 | c |
| 90 | }); |
| 91 | } |
| 92 | return c.json({ body }); |
| 93 | }; |
| 94 | |
| 95 | const handleGenerateError = (c: any, error: unknown) => { |
| 96 | if (error instanceof ApiErrorZod) { |
no test coverage detected