From 6f13d263062b7e746f5c682dd7f82e5ffcac9fea Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 29 Apr 2025 11:29:22 -0700 Subject: [PATCH 1/3] render title response Co-authored-by: sh4shii --- core/util/chatDescriber.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/util/chatDescriber.ts b/core/util/chatDescriber.ts index a10edbe72..7235de0a3 100644 --- a/core/util/chatDescriber.ts +++ b/core/util/chatDescriber.ts @@ -4,6 +4,7 @@ import { removeCodeBlocksAndTrim, removeQuotesAndEscapes } from "."; import type { FromCoreProtocol, ToCoreProtocol } from "../protocol"; import type { IMessenger } from "../protocol/messenger"; +import { renderChatMessage } from "./messageContent"; export class ChatDescriber { static maxTokens = 12; @@ -42,7 +43,7 @@ export class ChatDescriber { ); // Set the title - return removeQuotesAndEscapes(titleResponse.content.toString()); + return removeQuotesAndEscapes(renderChatMessage(titleResponse)); } // // TODO: Allow the user to manually set specific/tailored prompts to generate their titles From 611588a67d421717c8fdcc383a1f705dba5428d0 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 29 Apr 2025 11:39:12 -0700 Subject: [PATCH 2/3] render chat --- core/llm/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/llm/index.ts b/core/llm/index.ts index 1f7684652..aa115fb71 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -71,7 +71,11 @@ export class LLMError extends Error { } export function isModelInstaller(provider: any): provider is ModelInstaller { - return provider && typeof provider.installModel === "function" && typeof provider.isInstallingModel === "function";; + return ( + provider && + typeof provider.installModel === "function" && + typeof provider.isInstallingModel === "function" + ); } type InteractionStatus = "in_progress" | "success" | "error" | "cancelled"; @@ -816,8 +820,8 @@ export abstract class BaseLLM implements ILLM { options: LLMFullCompletionOptions = {}, ) { let completion = ""; - for await (const chunk of this.streamChat(messages, signal, options)) { - completion += chunk.content; + for await (const message of this.streamChat(messages, signal, options)) { + completion += renderChatMessage(message); } return { role: "assistant" as const, content: completion }; } From 5a77cf48f0f263fc2bc657bcf2f88129d96f8a18 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 29 Apr 2025 13:53:46 -0700 Subject: [PATCH 3/3] fix tests --- core/llm/llms/Mock.ts | 8 +++++++- core/util/chatDescriber.test.ts | 34 ++++++--------------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/core/llm/llms/Mock.ts b/core/llm/llms/Mock.ts index 03e268074..eb0f031c9 100644 --- a/core/llm/llms/Mock.ts +++ b/core/llm/llms/Mock.ts @@ -1,7 +1,11 @@ import { ChatMessage, CompletionOptions, LLMOptions } from "../../index.js"; import { BaseLLM } from "../index.js"; -type MockMessage = ChatMessage | "REPEAT_LAST_MSG" | "REPEAT_SYSTEM_MSG"; +type MockMessage = + | ChatMessage + | "REPEAT_LAST_MSG" + | "REPEAT_SYSTEM_MSG" + | "ERROR"; class MockLLM extends BaseLLM { public completion: string = "Test Completion"; @@ -49,6 +53,8 @@ class MockLLM extends BaseLLM { messages.find((m) => m.role === "system")?.content || "", }; break; + case "ERROR": + throw new Error("Intentional error"); default: yield message; } diff --git a/core/util/chatDescriber.test.ts b/core/util/chatDescriber.test.ts index 0e8851120..315cb3fea 100644 --- a/core/util/chatDescriber.test.ts +++ b/core/util/chatDescriber.test.ts @@ -34,43 +34,21 @@ describe("ChatDescriber", () => { const message = "Test message"; const completionOptions: LLMFullCompletionOptions = { temperature: 0.7 }; - testLLM.chat = jest.fn().mockResolvedValue({ content: "Test response" }); + testLLM.chatStreams = [[{ role: "assistant", content: "Test response" }]]; await ChatDescriber.describe(testLLM, completionOptions, message); expect(completionOptions.maxTokens).toBe(ChatDescriber.maxTokens); }); - it("should call model.chat with correct parameters", async () => { - const message = "Test message"; - const cleanedMessage = "Test message"; - const expectedPrompt = ChatDescriber.prompt + cleanedMessage; - const completionOptions: LLMFullCompletionOptions = {}; - - testLLM.chat = jest.fn().mockResolvedValue({ content: "Test response" }); - - await ChatDescriber.describe(testLLM, completionOptions, message); - - expect(testLLM.chat).toHaveBeenCalledWith( - [ - { - role: "user", - content: expectedPrompt, - }, - ], - expect.any(AbortSignal), - completionOptions, - ); - }); - it("should return processed content from the model response", async () => { const message = "Test message"; const modelResponseContent = "Model response content"; const expectedResult = "Model response content"; - testLLM.chat = jest - .fn() - .mockResolvedValue({ content: modelResponseContent }); + testLLM.chatStreams = [ + [{ role: "assistant", content: modelResponseContent }], + ]; const result = await ChatDescriber.describe(testLLM, {}, message); @@ -81,11 +59,11 @@ describe("ChatDescriber", () => { const message = "Test message"; const completionOptions: LLMFullCompletionOptions = {}; - testLLM.chat = jest.fn().mockRejectedValue(new Error("Chat error")); + testLLM.chatStreams = [["ERROR"]]; await expect( ChatDescriber.describe(testLLM, completionOptions, message), - ).rejects.toThrow("Chat error"); + ).rejects.toThrow(); }); }); });