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