Changesets · Branches · Tags · Compare
| Changeset | 7a6a62e41ecd4ca95c93d60928765db5b9514c8c743ea932faf688c00e90d8d0 |
|---|---|
| Author | Fred Bricon <fbricon@gmail.com> · 2025-08-28 16:12 +02:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-28 07:12 -07:00 |
| Parents | e17333f9491f |
1 file changed
modified core/llm/llms/Ollama.ts +32 −25
| @@ -422,31 +422,35 @@ | ||
| 422 | 422 | signal, |
| 423 | 423 | }); |
| 424 | 424 | |
| 425 | - function convertChatMessage(res: OllamaChatResponse): ChatMessage { | |
| 425 | + function convertChatMessage(res: OllamaChatResponse): ChatMessage[] { | |
| 426 | 426 | if ("error" in res) { |
| 427 | 427 | throw new Error(res.error); |
| 428 | 428 | } |
| 429 | - if (res.message.role === "tool") { | |
| 429 | + | |
| 430 | + const { role, content, thinking, tool_calls: toolCalls } = res.message; | |
| 431 | + | |
| 432 | + if (role === "tool") { | |
| 430 | 433 | throw new Error( |
| 431 | - "Unexpected message received from ollama with role = tool", | |
| 434 | + "Unexpected message received from Ollama with role = tool", | |
| 432 | 435 | ); |
| 433 | 436 | } |
| 434 | - if (res.message.role === "assistant") { | |
| 435 | - if (res.message.thinking) { | |
| 436 | - const thinkingMessage: ThinkingChatMessage = { | |
| 437 | - role: "thinking", | |
| 438 | - content: res.message.thinking, | |
| 439 | - }; | |
| 440 | - return thinkingMessage; | |
| 437 | + | |
| 438 | + if (role === "assistant") { | |
| 439 | + const thinkingMessage: ThinkingChatMessage | null = thinking | |
| 440 | + ? { role: "thinking", content: thinking } | |
| 441 | + : null; | |
| 442 | + | |
| 443 | + if (thinkingMessage && !content) { | |
| 444 | + // When Streaming you can't have both thinking and content | |
| 445 | + return [thinkingMessage]; | |
| 441 | 446 | } |
| 442 | - const chatMessage: ChatMessage = { | |
| 443 | - role: "assistant", | |
| 444 | - content: res.message.content, | |
| 445 | - }; | |
| 446 | - if (res.message.tool_calls) { | |
| 447 | + // Either not thinking, or not streaming | |
| 448 | + const chatMessage: ChatMessage = { role: "assistant", content }; | |
| 449 | + | |
| 450 | + if (toolCalls?.length) { | |
| 447 | 451 | // Continue handles the response as a tool call delta but |
| 448 | 452 | // But ollama returns the full object in one response with no streaming |
| 449 | - chatMessage.toolCalls = res.message.tool_calls.map((tc) => ({ | |
| 453 | + chatMessage.toolCalls = toolCalls.map((tc) => ({ | |
| 450 | 454 | type: "function", |
| 451 | 455 | id: `tc_${uuidv4()}`, // Generate a proper UUID with a prefix |
| 452 | 456 | function: { |
| @@ -455,13 +459,13 @@ | ||
| 455 | 459 | }, |
| 456 | 460 | })); |
| 457 | 461 | } |
| 458 | - return chatMessage; | |
| 459 | - } else { | |
| 460 | - return { | |
| 461 | - role: res.message.role, | |
| 462 | - content: res.message.content, | |
| 463 | - }; | |
| 462 | + | |
| 463 | + // Return both thinking and chat messages if applicable | |
| 464 | + return thinkingMessage ? [thinkingMessage, chatMessage] : [chatMessage]; | |
| 464 | 465 | } |
| 466 | + | |
| 467 | + // Fallback for all other roles | |
| 468 | + return [{ role, content }]; | |
| 465 | 469 | } |
| 466 | 470 | |
| 467 | 471 | if (chatOptions.stream === false) { |
| @@ -469,7 +473,9 @@ | ||
| 469 | 473 | return; // Aborted by user |
| 470 | 474 | } |
| 471 | 475 | const json = (await response.json()) as OllamaChatResponse; |
| 472 | - yield convertChatMessage(json); | |
| 476 | + for (const msg of convertChatMessage(json)) { | |
| 477 | + yield msg; | |
| 478 | + } | |
| 473 | 479 | } else { |
| 474 | 480 | let buffer = ""; |
| 475 | 481 | for await (const value of streamResponse(response)) { |
| @@ -484,8 +490,9 @@ | ||
| 484 | 490 | if (chunk.trim() !== "") { |
| 485 | 491 | try { |
| 486 | 492 | const j = JSON.parse(chunk) as OllamaChatResponse; |
| 487 | - const chatMessage = convertChatMessage(j); | |
| 488 | - yield chatMessage; | |
| 493 | + for (const msg of convertChatMessage(j)) { | |
| 494 | + yield msg; | |
| 495 | + } | |
| 489 | 496 | } catch (e) { |
| 490 | 497 | throw new Error(`Error parsing Ollama response: ${e} ${chunk}`); |
| 491 | 498 | } |