Elffuss BraidHub

kikocis/continue public

fix: split thinking and chat messages when Ollama is not streaming (#7446)

Signed-off-by: Fred Bricon <fbricon@gmail.com>
Changeset7a6a62e41ecd4ca95c93d60928765db5b9514c8c743ea932faf688c00e90d8d0
AuthorFred Bricon <fbricon@gmail.com> · 2025-08-28 16:12 +02:00
CommitterGitHub <noreply@github.com> · 2025-08-28 07:12 -07:00
Parentse17333f9491f

Browse the files at this changeset

1 file changed

modified core/llm/llms/Ollama.ts +32 −25
@@ -422,31 +422,35 @@
422422 signal,
423423 });
424424
425- function convertChatMessage(res: OllamaChatResponse): ChatMessage {
425+ function convertChatMessage(res: OllamaChatResponse): ChatMessage[] {
426426 if ("error" in res) {
427427 throw new Error(res.error);
428428 }
429- if (res.message.role === "tool") {
429+
430+ const { role, content, thinking, tool_calls: toolCalls } = res.message;
431+
432+ if (role === "tool") {
430433 throw new Error(
431- "Unexpected message received from ollama with role = tool",
434+ "Unexpected message received from Ollama with role = tool",
432435 );
433436 }
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];
441446 }
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) {
447451 // Continue handles the response as a tool call delta but
448452 // 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) => ({
450454 type: "function",
451455 id: `tc_${uuidv4()}`, // Generate a proper UUID with a prefix
452456 function: {
@@ -455,13 +459,13 @@
455459 },
456460 }));
457461 }
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];
464465 }
466+
467+ // Fallback for all other roles
468+ return [{ role, content }];
465469 }
466470
467471 if (chatOptions.stream === false) {
@@ -469,7 +473,9 @@
469473 return; // Aborted by user
470474 }
471475 const json = (await response.json()) as OllamaChatResponse;
472- yield convertChatMessage(json);
476+ for (const msg of convertChatMessage(json)) {
477+ yield msg;
478+ }
473479 } else {
474480 let buffer = "";
475481 for await (const value of streamResponse(response)) {
@@ -484,8 +490,9 @@
484490 if (chunk.trim() !== "") {
485491 try {
486492 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+ }
489496 } catch (e) {
490497 throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
491498 }