From 3553dfaae44e9655fb0fcda5da3b70e14d159747 Mon Sep 17 00:00:00 2001 From: "Owen W. Taylor" Date: Tue, 11 Feb 2025 23:14:36 -0500 Subject: [PATCH] Add rich logging of LLM interactions Instead of treating LLM logging as simply writing lines of text sequentially, emit a stream of typed data objects that include rich information about the interaction, such as timestamps, message types, tool calls, etc. These are then collected by a new custom webview in the vscode extension (the "Continue Console"), allowing a user or developer to browse through the different interactions and view them in detail. In progress interactions are now streamed live, instead of being held back to avoid concurrency issues. This webview could be also exposed for IntelliJ or file logging could be reimplemented on top of the new framework. --- binary/src/index.ts | 4 +- core/config/ConfigHandler.ts | 12 +- core/config/load.ts | 21 +- .../profile/ControlPlaneProfileLoader.ts | 5 +- core/config/profile/LocalProfileLoader.ts | 6 +- core/config/profile/PlatformProfileLoader.ts | 10 +- core/config/profile/doLoadConfig.ts | 7 +- core/config/types.ts | 92 ++++- core/config/yaml/loadYaml.ts | 9 +- core/config/yaml/models.ts | 28 +- .../providers/_context-providers.test.ts | 4 +- core/core.ts | 5 +- core/index.d.ts | 92 ++++- core/indexing/docs/DocsService.skip.ts | 3 +- core/llm/index.test.ts | 7 +- core/llm/index.ts | 369 +++++++++++++----- core/llm/llms/index.ts | 5 +- core/llm/logger.ts | 43 ++ core/test/fixtures.ts | 3 +- extensions/vscode/package.json | 34 ++ .../src/ContinueConsoleWebviewViewProvider.ts | 172 ++++++++ extensions/vscode/src/commands.ts | 8 + .../vscode/src/extension/VsCodeExtension.ts | 32 +- gui/indexConsole.html | 13 + gui/src/components/console/Details.tsx | 116 ++++++ gui/src/components/console/End.tsx | 37 ++ gui/src/components/console/Expander.tsx | 25 ++ gui/src/components/console/Layout.tsx | 30 ++ gui/src/components/console/List.tsx | 53 +++ gui/src/components/console/ListItem.tsx | 55 +++ gui/src/components/console/Message.tsx | 106 +++++ gui/src/components/console/Result.tsx | 37 ++ gui/src/components/console/ResultGroup.tsx | 19 + gui/src/components/console/Start.tsx | 73 ++++ gui/src/components/console/StatusIcon.tsx | 34 ++ gui/src/console.tsx | 12 + gui/src/hooks/useLLMLog.ts | 225 +++++++++++ gui/src/hooks/useLLMSummary.ts | 76 ++++ gui/src/indexConsole.css | 9 + gui/vite.config.ts | 5 + 40 files changed, 1727 insertions(+), 169 deletions(-) create mode 100644 core/llm/logger.ts create mode 100644 extensions/vscode/src/ContinueConsoleWebviewViewProvider.ts create mode 100644 gui/indexConsole.html create mode 100644 gui/src/components/console/Details.tsx create mode 100644 gui/src/components/console/End.tsx create mode 100644 gui/src/components/console/Expander.tsx create mode 100644 gui/src/components/console/Layout.tsx create mode 100644 gui/src/components/console/List.tsx create mode 100644 gui/src/components/console/ListItem.tsx create mode 100644 gui/src/components/console/Message.tsx create mode 100644 gui/src/components/console/Result.tsx create mode 100644 gui/src/components/console/ResultGroup.tsx create mode 100644 gui/src/components/console/Start.tsx create mode 100644 gui/src/components/console/StatusIcon.tsx create mode 100644 gui/src/console.tsx create mode 100644 gui/src/hooks/useLLMLog.ts create mode 100644 gui/src/hooks/useLLMSummary.ts create mode 100644 gui/src/indexConsole.css diff --git a/binary/src/index.ts b/binary/src/index.ts index 32bede332..08adf99f1 100644 --- a/binary/src/index.ts +++ b/binary/src/index.ts @@ -33,9 +33,7 @@ program.action(async () => { const ide = new IpcIde(messenger); const promptLogsPath = getPromptLogsPath(); - new Core(messenger, ide, async (text) => { - fs.appendFileSync(promptLogsPath, text + "\n\n"); - }); + new Core(messenger, ide); console.log("[binary] Core started"); } catch (e) { diff --git a/core/config/ConfigHandler.ts b/core/config/ConfigHandler.ts index 8b469d437..2d4a471d5 100644 --- a/core/config/ConfigHandler.ts +++ b/core/config/ConfigHandler.ts @@ -12,6 +12,7 @@ import { IDE, IdeSettings, ILLM, + ILLMLogger, } from "../index.js"; import Ollama from "../llm/llms/Ollama.js"; import { GlobalContext } from "../util/GlobalContext.js"; @@ -48,12 +49,11 @@ export class ConfigHandler { constructor( private readonly ide: IDE, private ideSettingsPromise: Promise, - private readonly writeLog: (text: string) => Promise, + private llmLogger: ILLMLogger, sessionInfoPromise: Promise, ) { this.ide = ide; this.ideSettingsPromise = ideSettingsPromise; - this.writeLog = writeLog; this.controlPlaneClient = new ControlPlaneClient( sessionInfoPromise, ideSettingsPromise, @@ -65,7 +65,7 @@ export class ConfigHandler { ide, ideSettingsPromise, this.controlPlaneClient, - writeLog, + this.llmLogger, ), this.ide, ); @@ -184,7 +184,7 @@ export class ConfigHandler { this.controlPlaneClient, this.ide, this.ideSettingsPromise, - this.writeLog, + this.llmLogger, assistant.rawYaml, orgScopeId, ); @@ -229,7 +229,7 @@ export class ConfigHandler { this.controlPlaneClient, this.ide, this.ideSettingsPromise, - this.writeLog, + this.llmLogger, this.reloadConfig.bind(this), ); @@ -292,7 +292,7 @@ export class ConfigHandler { this.ide, this.ideSettingsPromise, this.controlPlaneClient, - this.writeLog, + this.llmLogger, assistant, ); }); diff --git a/core/config/load.ts b/core/config/load.ts index 1a3acd6c6..b85359641 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -27,6 +27,7 @@ import { IdeSettings, IdeType, ILLM, + ILLMLogger, LLMOptions, ModelDescription, RerankerDescription, @@ -222,7 +223,7 @@ async function intermediateToFinalConfig( ideSettings: IdeSettings, ideInfo: IdeInfo, uniqueId: string, - writeLog: (log: string) => Promise, + llmLogger: ILLMLogger, workOsAccessToken: string | undefined, loadPromptFiles: boolean = true, allowFreeTrial: boolean = true, @@ -238,7 +239,7 @@ async function intermediateToFinalConfig( ide.readFile.bind(ide), uniqueId, ideSettings, - writeLog, + llmLogger, config.completionOptions, config.systemMessage, ); @@ -260,7 +261,7 @@ async function intermediateToFinalConfig( ide.readFile.bind(ide), uniqueId, ideSettings, - writeLog, + llmLogger, copyOf(config.completionOptions), config.systemMessage, ); @@ -280,7 +281,7 @@ async function intermediateToFinalConfig( } else { const llm = new CustomLLMClass({ ...desc, - options: { ...desc.options, writeLog } as any, + options: { ...desc.options, logger: llmLogger } as any, }); if (llm.model === "AUTODETECT") { try { @@ -289,7 +290,11 @@ async function intermediateToFinalConfig( (modelName) => new CustomLLMClass({ ...desc, - options: { ...desc.options, model: modelName, writeLog }, + options: { + ...desc.options, + model: modelName, + logger: llmLogger, + }, }), ); @@ -343,7 +348,7 @@ async function intermediateToFinalConfig( ide.readFile.bind(ide), uniqueId, ideSettings, - writeLog, + llmLogger, config.completionOptions, config.systemMessage, ); @@ -831,7 +836,7 @@ async function loadContinueConfigFromJson( ideSettings: IdeSettings, ideInfo: IdeInfo, uniqueId: string, - writeLog: (log: string) => Promise, + llmLogger: ILLMLogger, workOsAccessToken: string | undefined, overrideConfigJson: SerializedContinueConfig | undefined, ): Promise> { @@ -931,7 +936,7 @@ async function loadContinueConfigFromJson( ideSettings, ideInfo, uniqueId, - writeLog, + llmLogger, workOsAccessToken, ); return { diff --git a/core/config/profile/ControlPlaneProfileLoader.ts b/core/config/profile/ControlPlaneProfileLoader.ts index 85130676b..21ea27e14 100644 --- a/core/config/profile/ControlPlaneProfileLoader.ts +++ b/core/config/profile/ControlPlaneProfileLoader.ts @@ -7,6 +7,7 @@ import { ContinueConfig, IDE, IdeSettings, + ILLMLogger, SerializedContinueConfig, } from "../../index.js"; import { ProfileDescription } from "../ProfileLifecycleManager.js"; @@ -27,7 +28,7 @@ export default class ControlPlaneProfileLoader implements IProfileLoader { private readonly controlPlaneClient: ControlPlaneClient, private readonly ide: IDE, private ideSettingsPromise: Promise, - private writeLog: (message: string) => Promise, + private llmLogger: ILLMLogger, private readonly onReload: () => void, ) { this.description = { @@ -65,7 +66,7 @@ export default class ControlPlaneProfileLoader implements IProfileLoader { this.ide, this.ideSettingsPromise, this.controlPlaneClient, - this.writeLog, + this.llmLogger, serializedConfig, undefined, undefined, diff --git a/core/config/profile/LocalProfileLoader.ts b/core/config/profile/LocalProfileLoader.ts index 464d16804..f49c8bb7a 100644 --- a/core/config/profile/LocalProfileLoader.ts +++ b/core/config/profile/LocalProfileLoader.ts @@ -1,7 +1,7 @@ import { ConfigResult, parseConfigYaml } from "@continuedev/config-yaml"; import { ControlPlaneClient } from "../../control-plane/client.js"; -import { ContinueConfig, IDE, IdeSettings } from "../../index.js"; +import { ContinueConfig, IDE, IdeSettings, ILLMLogger } from "../../index.js"; import { ProfileDescription } from "../ProfileLifecycleManager.js"; import { getPrimaryConfigFilePath } from "../../util/paths.js"; @@ -17,7 +17,7 @@ export default class LocalProfileLoader implements IProfileLoader { private ide: IDE, private ideSettingsPromise: Promise, private controlPlaneClient: ControlPlaneClient, - private writeLog: (message: string) => Promise, + private llmLogger: ILLMLogger, private overrideAssistantFile?: | { path: string; content: string } | undefined, @@ -59,7 +59,7 @@ export default class LocalProfileLoader implements IProfileLoader { this.ide, this.ideSettingsPromise, this.controlPlaneClient, - this.writeLog, + this.llmLogger, undefined, undefined, undefined, diff --git a/core/config/profile/PlatformProfileLoader.ts b/core/config/profile/PlatformProfileLoader.ts index 468d3511a..cf9efb399 100644 --- a/core/config/profile/PlatformProfileLoader.ts +++ b/core/config/profile/PlatformProfileLoader.ts @@ -2,7 +2,7 @@ import { AssistantUnrolled, ConfigResult } from "@continuedev/config-yaml"; import { ControlPlaneClient } from "../../control-plane/client.js"; import { getControlPlaneEnv } from "../../control-plane/env.js"; -import { ContinueConfig, IDE, IdeSettings } from "../../index.js"; +import { ContinueConfig, IDE, IdeSettings, ILLMLogger } from "../../index.js"; import { ProfileDescription } from "../ProfileLifecycleManager.js"; import doLoadConfig from "./doLoadConfig.js"; @@ -30,7 +30,7 @@ export default class PlatformProfileLoader implements IProfileLoader { private readonly controlPlaneClient: ControlPlaneClient, private readonly ide: IDE, private ideSettingsPromise: Promise, - private writeLog: (message: string) => Promise, + private llmLogger: ILLMLogger, readonly description: ProfileDescription, private readonly orgScopeId: string | null, ) {} @@ -44,7 +44,7 @@ export default class PlatformProfileLoader implements IProfileLoader { controlPlaneClient: ControlPlaneClient, ide: IDE, ideSettingsPromise: Promise, - writeLog: (message: string) => Promise, + llmLogger: ILLMLogger, rawYaml: string, orgScopeId: string | null, ): Promise { @@ -74,7 +74,7 @@ export default class PlatformProfileLoader implements IProfileLoader { controlPlaneClient, ide, ideSettingsPromise, - writeLog, + llmLogger, description, orgScopeId, ); @@ -93,7 +93,7 @@ export default class PlatformProfileLoader implements IProfileLoader { this.ide, this.ideSettingsPromise, this.controlPlaneClient, - this.writeLog, + this.llmLogger, undefined, this.configResult.config, { diff --git a/core/config/profile/doLoadConfig.ts b/core/config/profile/doLoadConfig.ts index b3d0a7ea8..626546b74 100644 --- a/core/config/profile/doLoadConfig.ts +++ b/core/config/profile/doLoadConfig.ts @@ -12,6 +12,7 @@ import { ContinueRcJson, IDE, IdeSettings, + ILLMLogger, SerializedContinueConfig, Tool, } from "../../"; @@ -39,7 +40,7 @@ export default async function doLoadConfig( ide: IDE, ideSettingsPromise: Promise, controlPlaneClient: ControlPlaneClient, - writeLog: (message: string) => Promise, + llmLogger: ILLMLogger, overrideConfigJson: SerializedContinueConfig | undefined, overrideConfigYaml: AssistantUnrolled | undefined, platformConfigMetadata: PlatformConfigMetadata | undefined, @@ -74,7 +75,7 @@ export default async function doLoadConfig( ideSettings, ideInfo, uniqueId, - writeLog, + llmLogger, workOsAccessToken, overrideConfigYaml, platformConfigMetadata, @@ -92,7 +93,7 @@ export default async function doLoadConfig( ideSettings, ideInfo, uniqueId, - writeLog, + llmLogger, workOsAccessToken, overrideConfigJson, ); diff --git a/core/config/types.ts b/core/config/types.ts index 2a68c4c35..3cf978540 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -90,7 +90,7 @@ declare global { requestOptions?: RequestOptions; promptTemplates?: Record; templateMessages?: (messages: ChatMessage[]) => string; - writeLog?: (str: string) => Promise; + llmLogger?: ILLMLogger; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; apiBase?: string; @@ -433,6 +433,94 @@ declare global { export type ToastType = "info" | "error" | "warning"; + export interface LLMInteractionBase { + interactionId: string; + timestamp: number; + } + + export interface LLMInteractionStartChat extends LLMInteractionBase { + kind: "startChat"; + messages: ChatMessage[]; + options: CompletionOptions; + } + + export interface LLMInteractionStartComplete extends LLMInteractionBase { + kind: "startComplete"; + prompt: string; + options: CompletionOptions; + } + + export interface LLMInteractionStartFim extends LLMInteractionBase { + kind: "startFim"; + prefix: string; + suffix: string; + options: CompletionOptions; + } + + export interface LLMInteractionChunk extends LLMInteractionBase { + kind: "chunk"; + chunk: string; + } + + export interface LLMInteractionMessage extends LLMInteractionBase { + kind: "message"; + message: ChatMessage; + } + + export interface LLMInteractionEnd extends LLMInteractionBase { + promptTokens: number; + generatedTokens: number; + thinkingTokens: number; + } + + export interface LLMInteractionSuccess extends LLMInteractionEnd { + kind: "success"; + } + + export interface LLMInteractionCancel extends LLMInteractionEnd { + kind: "cancel"; + } + + export interface LLMInteractionError extends LLMInteractionEnd { + kind: "error"; + name: string; + message: string; + } + + export type LLMInteractionItem = + | LLMInteractionStartChat + | LLMInteractionStartComplete + | LLMInteractionStartFim + | LLMInteractionChunk + | LLMInteractionMessage + | LLMInteractionSuccess + | LLMInteractionCancel + | LLMInteractionError; + + // When we log a LLM interaction, we want to add the interactionId and timestamp + // in the logger code, so we need a type that omits these members from *each* + // member of the union. This can be done by using the distributive behavior of + // conditional types in Typescript. + // + // www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types + // https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s + type DistributiveOmit = T extends unknown + ? Omit + : never; + + export type LLMInteractionItemDetails = DistributiveOmit< + LLMInteractionItem, + "interactionId" | "timestamp" + >; + + export interface ILLMInteractionLog { + logItem(item: LLMInteractionItemDetails): void; + } + + export interface ILLMLogger { + createInteractionLog(): ILLMInteractionLog; + } + export interface LLMOptions { model: string; @@ -446,7 +534,7 @@ declare global { template?: TemplateType; promptTemplates?: Record; templateMessages?: (messages: ChatMessage[]) => string; - writeLog?: (str: string) => Promise; + logger?: ILLMLogger; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; aiGatewaySlug?: string; diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index f157bf21d..1edb48604 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -20,6 +20,7 @@ import { IDE, IdeInfo, IdeSettings, + ILLMLogger, } from "../.."; import { slashFromCustomCommand } from "../../commands"; import { AllRerankers } from "../../context/allRerankers"; @@ -116,7 +117,7 @@ async function configYamlToContinueConfig( ideSettings: IdeSettings, ideInfo: IdeInfo, uniqueId: string, - writeLog: (log: string) => Promise, + llmLogger: ILLMLogger, workOsAccessToken: string | undefined, platformConfigMetadata: PlatformConfigMetadata | undefined, allowFreeTrial: boolean = true, @@ -216,7 +217,7 @@ async function configYamlToContinueConfig( ide, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config: continueConfig, }); @@ -412,7 +413,7 @@ export async function loadContinueConfigFromYaml( ideSettings: IdeSettings, ideInfo: IdeInfo, uniqueId: string, - writeLog: (log: string) => Promise, + llmLogger: ILLMLogger, workOsAccessToken: string | undefined, overrideConfigYaml: AssistantUnrolled | undefined, platformConfigMetadata: PlatformConfigMetadata | undefined, @@ -451,7 +452,7 @@ export async function loadContinueConfigFromYaml( ideSettings, ideInfo, uniqueId, - writeLog, + llmLogger, workOsAccessToken, platformConfigMetadata, ); diff --git a/core/config/yaml/models.ts b/core/config/yaml/models.ts index 6eabfd040..fa9c92cac 100644 --- a/core/config/yaml/models.ts +++ b/core/config/yaml/models.ts @@ -1,6 +1,12 @@ import { ModelConfig } from "@continuedev/config-yaml"; -import { ContinueConfig, IDE, IdeSettings, LLMOptions } from "../.."; +import { + ContinueConfig, + IDE, + IdeSettings, + ILLMLogger, + LLMOptions, +} from "../.."; import { BaseLLM } from "../../llm"; import { LLMClasses } from "../../llm/llms"; import { PlatformConfigMetadata } from "../profile/PlatformProfileLoader"; @@ -25,14 +31,14 @@ async function modelConfigToBaseLLM({ model, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }: { model: ModelConfig; uniqueId: string; ideSettings: IdeSettings; - writeLog: (log: string) => Promise; + llmLogger: ILLMLogger; platformConfigMetadata: PlatformConfigMetadata | undefined; config: ContinueConfig; }): Promise { @@ -54,7 +60,7 @@ async function modelConfigToBaseLLM({ model.defaultCompletionOptions?.maxTokens ?? cls.defaultOptions?.completionOptions?.maxTokens, }, - writeLog, + logger: llmLogger, uniqueId, title: model.name, systemMessage: config.systemMessage, @@ -141,7 +147,7 @@ async function autodetectModels({ ide, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }: { @@ -150,7 +156,7 @@ async function autodetectModels({ ide: IDE; uniqueId: string; ideSettings: IdeSettings; - writeLog: (log: string) => Promise; + llmLogger: ILLMLogger; platformConfigMetadata: PlatformConfigMetadata | undefined; config: ContinueConfig; }): Promise { @@ -171,7 +177,7 @@ async function autodetectModels({ }, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }); @@ -189,7 +195,7 @@ export async function llmsFromModelConfig({ ide, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }: { @@ -197,7 +203,7 @@ export async function llmsFromModelConfig({ ide: IDE; uniqueId: string; ideSettings: IdeSettings; - writeLog: (log: string) => Promise; + llmLogger: ILLMLogger; platformConfigMetadata: PlatformConfigMetadata | undefined; config: ContinueConfig; }): Promise { @@ -205,7 +211,7 @@ export async function llmsFromModelConfig({ model, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }); @@ -220,7 +226,7 @@ export async function llmsFromModelConfig({ ide, uniqueId, ideSettings, - writeLog, + llmLogger, platformConfigMetadata, config, }); diff --git a/core/context/providers/_context-providers.test.ts b/core/context/providers/_context-providers.test.ts index 21f436b75..d8eb90ef9 100644 --- a/core/context/providers/_context-providers.test.ts +++ b/core/context/providers/_context-providers.test.ts @@ -7,6 +7,7 @@ import { IContextProvider, } from "../.."; import { ConfigHandler } from "../../config/ConfigHandler"; +import { LLMLogger } from "../../llm/logger"; import { TEST_DIR } from "../../test/testDir"; import FileSystemIde from "../../util/filesystem"; @@ -26,10 +27,11 @@ async function getContextProviderExtras( ): Promise { const ide = new FileSystemIde(TEST_DIR); const ideSettingsPromise = ide.getIdeSettings(); + const llmLogger = new LLMLogger(); const configHandler = new ConfigHandler( ide, ideSettingsPromise, - async (text) => {}, + llmLogger, Promise.resolve(undefined), ); const { config } = await configHandler.loadConfig(); diff --git a/core/core.ts b/core/core.ts index f948491fc..d59900cec 100644 --- a/core/core.ts +++ b/core/core.ts @@ -46,6 +46,7 @@ import { isLocalAssistantFile } from "./config/loadLocalAssistants"; import { MCPManagerSingleton } from "./context/mcp"; import { shouldIgnore } from "./indexing/shouldIgnore"; import { walkDirCache } from "./indexing/walkDir"; +import { LLMLogger } from "./llm/logger"; import { llmStreamChat } from "./llm/streamChat"; import type { FromCoreProtocol, ToCoreProtocol } from "./protocol"; import type { IMessenger, Message } from "./protocol/messenger"; @@ -58,6 +59,7 @@ export class Core { codebaseIndexingState: IndexingProgressUpdate; private docsService: DocsService; private globalContext = new GlobalContext(); + llmLogger = new LLMLogger(); private readonly indexingPauseToken = new PauseToken( this.globalContext.get("indexingPaused") === true, @@ -85,7 +87,6 @@ export class Core { constructor( private readonly messenger: IMessenger, private readonly ide: IDE, - private readonly onWrite: (text: string) => Promise = async () => {}, ) { // Ensure .continue directory is created migrateV1DevDataFiles(); @@ -106,7 +107,7 @@ export class Core { this.configHandler = new ConfigHandler( this.ide, ideSettingsPromise, - this.onWrite, + this.llmLogger, sessionInfoPromise, ); diff --git a/core/index.d.ts b/core/index.d.ts index dd96d1699..e7fb25ef9 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -90,7 +90,7 @@ export interface ILLM extends LLMOptions { requestOptions?: RequestOptions; promptTemplates?: Record; templateMessages?: (messages: ChatMessage[]) => string; - writeLog?: (str: string) => Promise; + llmLogger?: ILLMLogger; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; apiBase?: string; @@ -483,6 +483,94 @@ export interface LLMFullCompletionOptions extends BaseCompletionOptions { export type ToastType = "info" | "error" | "warning"; +export interface LLMInteractionBase { + interactionId: string; + timestamp: number; +} + +export interface LLMInteractionStartChat extends LLMInteractionBase { + kind: "startChat"; + messages: ChatMessage[]; + options: CompletionOptions; +} + +export interface LLMInteractionStartComplete extends LLMInteractionBase { + kind: "startComplete"; + prompt: string; + options: CompletionOptions; +} + +export interface LLMInteractionStartFim extends LLMInteractionBase { + kind: "startFim"; + prefix: string; + suffix: string; + options: CompletionOptions; +} + +export interface LLMInteractionChunk extends LLMInteractionBase { + kind: "chunk"; + chunk: string; +} + +export interface LLMInteractionMessage extends LLMInteractionBase { + kind: "message"; + message: ChatMessage; +} + +export interface LLMInteractionEnd extends LLMInteractionBase { + promptTokens: number; + generatedTokens: number; + thinkingTokens: number; +} + +export interface LLMInteractionSuccess extends LLMInteractionEnd { + kind: "success"; +} + +export interface LLMInteractionCancel extends LLMInteractionEnd { + kind: "cancel"; +} + +export interface LLMInteractionError extends LLMInteractionEnd { + kind: "error"; + name: string; + message: string; +} + +export type LLMInteractionItem = + | LLMInteractionStartChat + | LLMInteractionStartComplete + | LLMInteractionStartFim + | LLMInteractionChunk + | LLMInteractionMessage + | LLMInteractionSuccess + | LLMInteractionCancel + | LLMInteractionError; + +// When we log a LLM interaction, we want to add the interactionId and timestamp +// in the logger code, so we need a type that omits these members from *each* +// member of the union. This can be done by using the distributive behavior of +// conditional types in Typescript. +// +// www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types +// https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s +type DistributiveOmit = T extends unknown + ? Omit + : never; + +export type LLMInteractionItemDetails = DistributiveOmit< + LLMInteractionItem, + "interactionId" | "timestamp" +>; + +export interface ILLMInteractionLog { + logItem(item: LLMInteractionItemDetails): void; +} + +export interface ILLMLogger { + createInteractionLog(): ILLMInteractionLog; +} + export interface LLMOptions { model: string; @@ -496,7 +584,7 @@ export interface LLMOptions { template?: TemplateType; promptTemplates?: Record; templateMessages?: (messages: ChatMessage[]) => string; - writeLog?: (str: string) => Promise; + logger?: ILLMLogger; llmRequestHook?: (model: string, prompt: string) => any; rules?: Rule[]; apiKey?: string; diff --git a/core/indexing/docs/DocsService.skip.ts b/core/indexing/docs/DocsService.skip.ts index 6021fc260..9677abfef 100644 --- a/core/indexing/docs/DocsService.skip.ts +++ b/core/indexing/docs/DocsService.skip.ts @@ -3,6 +3,7 @@ // */ // import { ConfigHandler } from "../../config/ConfigHandler.js"; // import { SiteIndexingConfig } from "../../index.js"; +// import { LLMLogger } from "../../llm/logger.js"; // import FileSystemIde from "../../util/filesystem.js"; // import { editConfigJson } from "../../util/paths.js"; @@ -48,7 +49,7 @@ // configHandler = new ConfigHandler( // ide, // ideSettingsPromise, -// async () => {}, +// new LLMLogger(), // Promise.resolve(undefined), // ); diff --git a/core/llm/index.test.ts b/core/llm/index.test.ts index 404902e91..7f535d1fd 100644 --- a/core/llm/index.test.ts +++ b/core/llm/index.test.ts @@ -1,6 +1,7 @@ import { ChatMessage, LLMOptions } from ".."; import { BaseLLM } from "."; +import { LLMLogger } from "./logger"; class DummyLLM extends BaseLLM { static providerName = "openai"; @@ -30,7 +31,7 @@ describe("BaseLLM", () => { const templatMessagesFunction = (messages: ChatMessage[]) => { return messages[0]?.content.toString() ?? ""; }; - const writeLogFunction = async () => {}; + const llmLogger = new LLMLogger(); const options: LLMOptions = { model: "gpt-3.5-turbo", uniqueId: "testId", @@ -43,7 +44,7 @@ describe("BaseLLM", () => { requestOptions: {}, promptTemplates: {}, templateMessages: templatMessagesFunction, - writeLog: writeLogFunction, + logger: llmLogger, llmRequestHook: () => {}, apiKey: "testApiKey", aiGatewaySlug: "testSlug", @@ -67,7 +68,7 @@ describe("BaseLLM", () => { expect(instance.requestOptions).toEqual({}); expect(instance.promptTemplates).toEqual({}); expect(instance.templateMessages).toEqual(templatMessagesFunction); - expect(instance.writeLog).toBe(writeLogFunction); + expect(instance.logger).toBe(llmLogger); expect(instance.apiKey).toBe("testApiKey"); expect(instance.aiGatewaySlug).toBe("testSlug"); expect(instance.apiBase).toBe("https://api.example.com/"); diff --git a/core/llm/index.ts b/core/llm/index.ts index 22f977c2f..7544b5292 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -16,6 +16,8 @@ import { Chunk, CompletionOptions, ILLM, + ILLMInteractionLog, + ILLMLogger, LLMFullCompletionOptions, LLMOptions, ModelCapability, @@ -72,6 +74,8 @@ export function isModelInstaller(provider: any): provider is ModelInstaller { return provider && typeof provider.installModel === "function"; } +type InteractionStatus = "in_progress" | "success" | "error" | "cancelled"; + export abstract class BaseLLM implements ILLM { static providerName: string; static defaultOptions: Partial | undefined = undefined; @@ -129,7 +133,7 @@ export abstract class BaseLLM implements ILLM { template?: TemplateType; promptTemplates?: Record; templateMessages?: (messages: ChatMessage[]) => string; - writeLog?: (str: string) => Promise; + logger?: ILLMLogger; llmRequestHook?: (model: string, prompt: string) => any; apiKey?: string; @@ -220,7 +224,7 @@ export abstract class BaseLLM implements ILLM { options.template, ) ?? undefined; - this.writeLog = options.writeLog; + this.logger = options.logger; this.llmRequestHook = options.llmRequestHook; this.apiKey = options.apiKey; @@ -345,13 +349,17 @@ export abstract class BaseLLM implements ILLM { ); } - private _logTokensGenerated( + private _logEnd( model: string, prompt: string, completion: string, - ) { + thinking: string | undefined, + interaction: ILLMInteractionLog | undefined, + error?: any, + ): InteractionStatus { let promptTokens = this.countTokens(prompt); let generatedTokens = this.countTokens(completion); + let thinkingTokens = thinking ? this.countTokens(thinking) : 0; void Telemetry.capture( "tokens_generated", @@ -380,6 +388,37 @@ export abstract class BaseLLM implements ILLM { generatedTokens: generatedTokens, }, }); + + if (error !== undefined) { + if (error === "cancel" || error.name === "AbortError") { + interaction?.logItem({ + kind: "cancel", + promptTokens, + generatedTokens, + thinkingTokens, + }); + return "cancelled"; + } else { + console.log(error); + interaction?.logItem({ + kind: "error", + name: error.name, + message: error.message, + promptTokens, + generatedTokens, + thinkingTokens, + }); + return "error"; + } + } else { + interaction?.logItem({ + kind: "success", + promptTokens, + generatedTokens, + thinkingTokens, + }); + return "success"; + } } fetch(url: RequestInfo | URL, init?: RequestInit): Promise { @@ -535,52 +574,89 @@ export abstract class BaseLLM implements ILLM { ): AsyncGenerator { const { completionOptions, logEnabled } = this._parseCompletionOptions(options); + const interaction = logEnabled + ? this.logger?.createInteractionLog() + : undefined; + let status: InteractionStatus = "in_progress"; const fimLog = `Prefix: ${prefix}\nSuffix: ${suffix}`; if (logEnabled) { - if (this.writeLog) { - await this.writeLog( - this._compilePromptForLog(fimLog, completionOptions), - ); - } + interaction?.logItem({ + kind: "startFim", + prefix, + suffix, + options: completionOptions, + }); if (this.llmRequestHook) { this.llmRequestHook(completionOptions.model, fimLog); } } let completion = ""; - - if (this.shouldUseOpenAIAdapter("streamFim") && this.openaiAdapter) { - const stream = this.openaiAdapter.fimStream( - toFimBody(prefix, suffix, completionOptions), - signal, - ); - for await (const chunk of stream) { - const result = fromChatCompletionChunk(chunk); - if (result) { - const content = renderChatMessage(result); - completion += content; - yield content; + try { + if (this.shouldUseOpenAIAdapter("streamFim") && this.openaiAdapter) { + const stream = this.openaiAdapter.fimStream( + toFimBody(prefix, suffix, completionOptions), + signal, + ); + for await (const chunk of stream) { + const result = fromChatCompletionChunk(chunk); + if (result) { + const content = renderChatMessage(result); + interaction?.logItem({ + kind: "chunk", + chunk: content, + }); + completion += content; + yield content; + } + } + } else { + for await (const chunk of this._streamFim( + prefix, + suffix, + signal, + completionOptions, + )) { + interaction?.logItem({ + kind: "chunk", + chunk, + }); + completion += chunk; + yield chunk; } } - } else { - for await (const chunk of this._streamFim( - prefix, - suffix, - signal, - completionOptions, - )) { - completion += chunk; - yield chunk; + + status = this._logEnd( + completionOptions.model, + fimLog, + completion, + undefined, + interaction, + ); + } catch (e) { + status = this._logEnd( + completionOptions.model, + fimLog, + completion, + undefined, + interaction, + e, + ); + throw e; + } finally { + if (status === "in_progress") { + this._logEnd( + completionOptions.model, + fimLog, + completion, + undefined, + interaction, + "cancel", + ); } } - this._logTokensGenerated(completionOptions.model, fimLog, completion); - - if (logEnabled && this.writeLog) { - await this.writeLog(`Completion:\n${completion}\n\n`); - } - return { prompt: fimLog, completion, @@ -595,6 +671,10 @@ export abstract class BaseLLM implements ILLM { ) { const { completionOptions, logEnabled, raw } = this._parseCompletionOptions(options); + const interaction = logEnabled + ? this.logger?.createInteractionLog() + : undefined; + let status: InteractionStatus = "in_progress"; let prompt = pruneRawPromptFromTop( completionOptions.model, @@ -608,11 +688,11 @@ export abstract class BaseLLM implements ILLM { } if (logEnabled) { - if (this.writeLog) { - await this.writeLog( - this._compilePromptForLog(prompt, completionOptions), - ); - } + interaction?.logItem({ + kind: "startComplete", + prompt, + options: completionOptions, + }); if (this.llmRequestHook) { this.llmRequestHook(completionOptions.model, prompt); } @@ -640,6 +720,10 @@ export abstract class BaseLLM implements ILLM { )) { const content = chunk.choices[0]?.text ?? ""; completion += content; + interaction?.logItem({ + kind: "chunk", + chunk: content, + }); yield content; } } @@ -650,14 +734,40 @@ export abstract class BaseLLM implements ILLM { completionOptions, )) { completion += chunk; + interaction?.logItem({ + kind: "chunk", + chunk, + }); yield chunk; } } + status = this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + ); + } catch (e) { + status = this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + e, + ); + throw e; } finally { - this._logTokensGenerated(completionOptions.model, prompt, completion); - - if (logEnabled && this.writeLog) { - await this.writeLog(`Completion:\n${completion}\n\n`); + if (status === "in_progress") { + this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + "cancel", + ); } } @@ -676,6 +786,10 @@ export abstract class BaseLLM implements ILLM { ) { const { completionOptions, logEnabled, raw } = this._parseCompletionOptions(options); + const interaction = logEnabled + ? this.logger?.createInteractionLog() + : undefined; + let status: InteractionStatus = "in_progress"; let prompt = pruneRawPromptFromTop( completionOptions.model, @@ -689,34 +803,65 @@ export abstract class BaseLLM implements ILLM { } if (logEnabled) { - if (this.writeLog) { - await this.writeLog( - this._compilePromptForLog(prompt, completionOptions), - ); - } + interaction?.logItem({ + kind: "startComplete", + prompt: prompt, + options: completionOptions, + }); if (this.llmRequestHook) { this.llmRequestHook(completionOptions.model, prompt); } } - let completion: string; - if (this.shouldUseOpenAIAdapter("complete") && this.openaiAdapter) { - const result = await this.openaiAdapter.completionNonStream( - { - ...toCompleteBody(prompt, completionOptions), - stream: false, - }, - signal, + let completion: string = ""; + + try { + if (this.shouldUseOpenAIAdapter("complete") && this.openaiAdapter) { + const result = await this.openaiAdapter.completionNonStream( + { + ...toCompleteBody(prompt, completionOptions), + stream: false, + }, + signal, + ); + completion = result.choices[0].text; + } else { + completion = await this._complete(prompt, signal, completionOptions); + } + + interaction?.logItem({ + kind: "chunk", + chunk: completion, + }); + + status = this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, ); - completion = result.choices[0].text; - } else { - completion = await this._complete(prompt, signal, completionOptions); - } - - this._logTokensGenerated(completionOptions.model, prompt, completion); - - if (logEnabled && this.writeLog) { - await this.writeLog(`Completion:\n${completion}\n\n`); + } catch (e) { + status = this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + e, + ); + throw e; + } finally { + if (status === "in_progress") { + this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + "cancel", + ); + } } return completion; @@ -761,6 +906,10 @@ export abstract class BaseLLM implements ILLM { ): AsyncGenerator { let { completionOptions, logEnabled } = this._parseCompletionOptions(options); + const interaction = logEnabled + ? this.logger?.createInteractionLog() + : undefined; + let status: InteractionStatus = "in_progress"; completionOptions = this._modifyCompletionOptions(completionOptions); @@ -770,11 +919,11 @@ export abstract class BaseLLM implements ILLM { ? this.templateMessages(messages) : this._formatChatMessages(messages); if (logEnabled) { - if (this.writeLog) { - await this.writeLog( - this._compilePromptForLog(prompt, completionOptions), - ); - } + interaction?.logItem({ + kind: "startChat", + messages, + options: completionOptions, + }); if (this.llmRequestHook) { this.llmRequestHook(completionOptions.model, prompt); } @@ -791,6 +940,10 @@ export abstract class BaseLLM implements ILLM { completionOptions, )) { completion += chunk; + interaction?.logItem({ + kind: "chunk", + chunk: chunk, + }); yield { role: "assistant", content: chunk }; } } else { @@ -820,6 +973,10 @@ export abstract class BaseLLM implements ILLM { const result = fromChatCompletionChunk(chunk); if (result) { completion += result.content; + interaction?.logItem({ + kind: "message", + message: result, + }); yield result; } } @@ -832,38 +989,56 @@ export abstract class BaseLLM implements ILLM { )) { if (chunk.role === "assistant") { completion += chunk.content; - yield chunk; + } else if (chunk.role === "thinking") { + thinking += chunk.content; } - if (chunk.role === "thinking") { - thinking += chunk.content; - yield chunk; - } + interaction?.logItem({ + kind: "message", + message: chunk, + }); + yield chunk; } } } - } catch (error) { - console.log(error); - throw error; - } - - this._logTokensGenerated(completionOptions.model, prompt, completion); - - if (logEnabled && this.writeLog) { - if (thinking) { - await this.writeLog(`Thinking:\n${thinking}\n\n`); + status = this._logEnd( + completionOptions.model, + prompt, + completion, + thinking, + interaction, + ); + } catch (e) { + status = this._logEnd( + completionOptions.model, + prompt, + completion, + thinking, + interaction, + e, + ); + throw e; + } finally { + if (status === "in_progress") { + this._logEnd( + completionOptions.model, + prompt, + completion, + undefined, + interaction, + "cancel", + ); } - /* - TODO: According to: https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking - During tool use, you must pass thinking and redacted_thinking blocks back to the API, - and you must include the complete unmodified block back to the API. This is critical - for maintaining the model's reasoning flow and conversation integrity. - - On the other hand, adding thinking and redacted_thinking blocks are ignored on subsequent - requests when not using tools, so it's the simplest option to always add to history. - */ - await this.writeLog(`Completion:\n${completion}\n\n`); } + /* + TODO: According to: https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking + During tool use, you must pass thinking and redacted_thinking blocks back to the API, + and you must include the complete unmodified block back to the API. This is critical + for maintaining the model's reasoning flow and conversation integrity. + + On the other hand, adding thinking and redacted_thinking blocks are ignored on subsequent + requests when not using tools, so it's the simplest option to always add to history. + */ return { modelTitle: this.title ?? completionOptions.model, diff --git a/core/llm/llms/index.ts b/core/llm/llms/index.ts index 8e3f68b2e..cef09098c 100644 --- a/core/llm/llms/index.ts +++ b/core/llm/llms/index.ts @@ -2,6 +2,7 @@ import { BaseCompletionOptions, IdeSettings, ILLM, + ILLMLogger, LLMOptions, ModelDescription, } from "../.."; @@ -115,7 +116,7 @@ export async function llmFromDescription( readFile: (filepath: string) => Promise, uniqueId: string, ideSettings: IdeSettings, - writeLog: (log: string) => Promise, + llmLogger: ILLMLogger, completionOptions?: BaseCompletionOptions, systemMessage?: string, ): Promise { @@ -145,7 +146,7 @@ export async function llmFromDescription( cls.defaultOptions?.completionOptions?.maxTokens, }, systemMessage, - writeLog, + logger: llmLogger, uniqueId, }; diff --git a/core/llm/logger.ts b/core/llm/logger.ts new file mode 100644 index 000000000..ae4699a45 --- /dev/null +++ b/core/llm/logger.ts @@ -0,0 +1,43 @@ +import { + ILLMLogger, + ILLMInteractionLog, + LLMInteractionItem, + LLMInteractionItemDetails, +} from ".."; + +type LLMLogItemFunction = (item: LLMInteractionItem) => void; + +export class LLMLogger implements ILLMLogger { + private nextId = 0; + + public createInteractionLog(): LLMInteractionLog { + return new LLMInteractionLog(this, (this.nextId++).toString()); + } + + private logItemListeners: LLMLogItemFunction[] = []; + + onLogItem(listener: LLMLogItemFunction) { + this.logItemListeners.push(listener); + } + + public _logItem(item: LLMInteractionItem) { + for (const listener of this.logItemListeners) { + listener(item); + } + } +} + +export class LLMInteractionLog implements ILLMInteractionLog { + constructor( + private logger: LLMLogger, + public interactionId: string, + ) {} + + logItem(item: LLMInteractionItemDetails) { + this.logger._logItem({ + ...item, + interactionId: this.interactionId, + timestamp: Date.now(), + }); + } +} diff --git a/core/test/fixtures.ts b/core/test/fixtures.ts index ae54b03db..a394273e9 100644 --- a/core/test/fixtures.ts +++ b/core/test/fixtures.ts @@ -1,6 +1,7 @@ import { ConfigHandler } from "../config/ConfigHandler"; import { ControlPlaneClient } from "../control-plane/client"; import Mock from "../llm/llms/Mock"; +import { LLMLogger } from "../llm/logger"; import FileSystemIde from "../util/filesystem"; import { TEST_DIR } from "./testDir"; @@ -17,7 +18,7 @@ export const testControlPlaneClient = new ControlPlaneClient( export const testConfigHandler = new ConfigHandler( testIde, ideSettingsPromise, - async (text) => {}, + new LLMLogger(), Promise.resolve(undefined), ); diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 459957c8d..6ac6afd30 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -127,6 +127,11 @@ "default": false, "markdownDescription": "Pause Continue's codebase index on start." }, + "continue.enableConsole": { + "type": "boolean", + "default": false, + "markdownDescription": "Enable a console to log and explore model inputs and outputs. It can be found in the bottom panel." + }, "continue.remoteConfigServerUrl": { "type": "string", "default": null, @@ -252,6 +257,13 @@ "title": "View History", "group": "Continue" }, + { + "command": "continue.clearConsole", + "category": "Continue", + "title": "Clear Console", + "icon": "$(clear-all)", + "group": "Continue" + }, { "command": "continue.navigateTo", "category": "Continue", @@ -505,6 +517,11 @@ "command": "continue.openConfigPage", "group": "navigation@4", "when": "view == continue.continueGUIView" + }, + { + "command": "continue.clearConsole", + "group": "navigation@1", + "when": "view == continue.continueConsoleView" } ], "editor/title": [ @@ -533,6 +550,13 @@ "title": "Continue", "icon": "media/sidebar-icon.png" } + ], + "panel": [ + { + "id": "continueConsole", + "title": "Continue Console", + "icon": "$(window)" + } ] }, "views": { @@ -544,6 +568,16 @@ "icon": "media/sidebar-icon.png", "visibility": "visible" } + ], + "continueConsole": [ + { + "type": "webview", + "id": "continue.continueConsoleView", + "name": "Continue Console", + "icon": "$(window)", + "visibility": "visible", + "when": "config.continue.enableConsole" + } ] }, "jsonValidation": [ diff --git a/extensions/vscode/src/ContinueConsoleWebviewViewProvider.ts b/extensions/vscode/src/ContinueConsoleWebviewViewProvider.ts new file mode 100644 index 000000000..a509b6e70 --- /dev/null +++ b/extensions/vscode/src/ContinueConsoleWebviewViewProvider.ts @@ -0,0 +1,172 @@ +import { LLMInteractionItem } from "core"; +import { EXTENSION_NAME } from "core/control-plane/env"; +import { LLMLogger } from "core/llm/logger"; +import * as vscode from "vscode"; + +import { getExtensionUri, getNonce } from "./util/vscode"; + +interface FromConsoleView { + type: "start" | "stop"; + uuid: string; +} + +export class ContinueConsoleWebviewViewProvider + implements vscode.WebviewViewProvider +{ + public static readonly viewType = "continue.continueConsoleView"; + + resolveWebviewView( + webviewView: vscode.WebviewView, + _context: vscode.WebviewViewResolveContext, + _token: vscode.CancellationToken, + ): void | Thenable { + this._webviewView = webviewView; + this._webview = webviewView.webview; + this._webviewView.onDidDispose(() => { + this._webviewView = undefined; + this._webview = undefined; + }); + webviewView.webview.html = this.getSidebarContent( + this.extensionContext, + webviewView, + ); + this._webview.onDidReceiveMessage((message: FromConsoleView) => { + if (message.type === "start") { + this._currentUuid = message.uuid; + this._webview?.postMessage({ + type: "init", + uuid: this._currentUuid, + items: this._items, + }); + } + }); + this._webviewView.onDidDispose(() => { + this._webview = undefined; + this._webviewView = undefined; + this._currentUuid = undefined; + }); + } + + private _webview?: vscode.Webview; + private _webviewView?: vscode.WebviewView; + private _currentUuid?: string; + private _items: LLMInteractionItem[] = []; + private _saveLog; + + constructor( + private readonly windowId: string, + private readonly extensionContext: vscode.ExtensionContext, + private readonly llmLogger: LLMLogger, + ) { + const config = vscode.workspace.getConfiguration(EXTENSION_NAME); + this._saveLog = config.get("enableConsole"); + + vscode.workspace.onDidChangeConfiguration((e) => { + if (e.affectsConfiguration(`${EXTENSION_NAME}.enableConsole`)) { + const config = vscode.workspace.getConfiguration(EXTENSION_NAME); + this._saveLog = config.get("enableConsole"); + if (!this._saveLog) { + this.clearLog(); + } + } + }); + + llmLogger.onLogItem((item) => { + if (!this._saveLog) { + return; + } + + this._items.push(item); + if (this._currentUuid) { + this._webview?.postMessage({ + type: "item", + uuid: this._currentUuid, + item, + }); + } + }); + } + + clearLog() { + this._items = []; + if (this._currentUuid) { + this._webview?.postMessage({ + type: "clear", + uuid: this._currentUuid, + }); + } + } + + private getSidebarContent( + context: vscode.ExtensionContext | undefined, + panel: vscode.WebviewPanel | vscode.WebviewView, + page: string | undefined = undefined, + ): string { + const extensionUri = getExtensionUri(); + let scriptUri: string; + let styleMainUri: string; + + const inDevelopmentMode = + context?.extensionMode === vscode.ExtensionMode.Development; + if (!inDevelopmentMode) { + scriptUri = panel.webview + .asWebviewUri( + vscode.Uri.joinPath(extensionUri, "gui/assets/indexConsole.js"), + ) + .toString(); + styleMainUri = panel.webview + .asWebviewUri( + vscode.Uri.joinPath(extensionUri, "gui/assets/indexConsole.css"), + ) + .toString(); + } else { + scriptUri = "http://localhost:5173/src/console.tsx"; + styleMainUri = "http://localhost:5173/src/indexConsole.css"; + } + + panel.webview.options = { + enableScripts: true, + localResourceRoots: [ + vscode.Uri.joinPath(extensionUri, "gui"), + vscode.Uri.joinPath(extensionUri, "assets"), + ], + enableCommandUris: true, + portMapping: [ + { + webviewPort: 65433, + extensionHostPort: 65433, + }, + ], + }; + + const nonce = getNonce(); + + return ` + + + + + + + + Continue + + +
+ + ${ + inDevelopmentMode + ? `` + : "" + } + + + `; + } +} diff --git a/extensions/vscode/src/commands.ts b/extensions/vscode/src/commands.ts index 32d240d5f..9009d2b58 100644 --- a/extensions/vscode/src/commands.ts +++ b/extensions/vscode/src/commands.ts @@ -27,6 +27,7 @@ import { setupStatusBar, StatusBarStatus, } from "./autocomplete/statusBar"; +import { ContinueConsoleWebviewViewProvider } from "./ContinueConsoleWebviewViewProvider"; import { ContinueGUIWebviewViewProvider } from "./ContinueGUIWebviewViewProvider"; import { VerticalDiffManager } from "./diff/vertical/manager"; @@ -318,6 +319,7 @@ const getCommandsMap: ( ide: VsCodeIde, extensionContext: vscode.ExtensionContext, sidebar: ContinueGUIWebviewViewProvider, + consoleView: ContinueConsoleWebviewViewProvider, configHandler: ConfigHandler, verticalDiffManager: VerticalDiffManager, continueServerClientPromise: Promise, @@ -329,6 +331,7 @@ const getCommandsMap: ( ide, extensionContext, sidebar, + consoleView, configHandler, verticalDiffManager, continueServerClientPromise, @@ -672,6 +675,9 @@ const getCommandsMap: ( "If there are any grammar or spelling mistakes in this writing, fix them. Do not make other large changes to the writing.", ); }, + "continue.clearConsole": async () => { + consoleView.clearLog(); + }, "continue.viewLogs": async () => { captureCommandTelemetry("viewLogs"); vscode.commands.executeCommand("workbench.action.toggleDevTools"); @@ -1088,6 +1094,7 @@ export function registerAllCommands( ide: VsCodeIde, extensionContext: vscode.ExtensionContext, sidebar: ContinueGUIWebviewViewProvider, + consoleView: ContinueConsoleWebviewViewProvider, configHandler: ConfigHandler, verticalDiffManager: VerticalDiffManager, continueServerClientPromise: Promise, @@ -1103,6 +1110,7 @@ export function registerAllCommands( ide, extensionContext, sidebar, + consoleView, configHandler, verticalDiffManager, continueServerClientPromise, diff --git a/extensions/vscode/src/extension/VsCodeExtension.ts b/extensions/vscode/src/extension/VsCodeExtension.ts index ee28c6c79..c955f8d51 100644 --- a/extensions/vscode/src/extension/VsCodeExtension.ts +++ b/extensions/vscode/src/extension/VsCodeExtension.ts @@ -21,6 +21,7 @@ import { StatusBarStatus, } from "../autocomplete/statusBar"; import { registerAllCommands } from "../commands"; +import { ContinueConsoleWebviewViewProvider } from "../ContinueConsoleWebviewViewProvider"; import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider"; import { VerticalDiffManager } from "../diff/vertical/manager"; import { registerAllCodeLensProviders } from "../lang-server/codeLens"; @@ -48,6 +49,7 @@ export class VsCodeExtension { private configHandler: ConfigHandler; private extensionContext: vscode.ExtensionContext; private ide: VsCodeIde; + private consoleView: ContinueConsoleWebviewViewProvider; private sidebar: ContinueGUIWebviewViewProvider; private windowId: string; private editDecorationManager: EditDecorationManager; @@ -106,10 +108,6 @@ export class VsCodeExtension { ); resolveWebviewProtocol(this.sidebar.webviewProtocol); - // Config Handler with output channel - const outputChannel = vscode.window.createOutputChannel( - "Continue - LLM Prompt/Completion", - ); const inProcessMessenger = new InProcessMessenger< ToCoreProtocol, FromCoreProtocol @@ -125,19 +123,12 @@ export class VsCodeExtension { this.editDecorationManager, ); - this.core = new Core(inProcessMessenger, this.ide, async (log: string) => { - outputChannel.appendLine( - "==========================================================================", - ); - outputChannel.appendLine( - "==========================================================================", - ); - outputChannel.append(log); - }); + this.core = new Core(inProcessMessenger, this.ide); this.configHandler = this.core.configHandler; resolveConfigHandler?.(this.configHandler); this.configHandler.loadConfig(); + this.verticalDiffManager = new VerticalDiffManager( this.configHandler, this.sidebar.webviewProtocol, @@ -244,12 +235,27 @@ export class VsCodeExtension { this.fileSearch, ); + // LLM Log view + this.consoleView = new ContinueConsoleWebviewViewProvider( + this.windowId, + this.extensionContext, + this.core.llmLogger, + ); + + context.subscriptions.push( + vscode.window.registerWebviewViewProvider( + "continue.continueConsoleView", + this.consoleView, + ), + ); + // Commands registerAllCommands( context, this.ide, context, this.sidebar, + this.consoleView, this.configHandler, this.verticalDiffManager, this.core.continueServerClientPromise, diff --git a/gui/indexConsole.html b/gui/indexConsole.html new file mode 100644 index 000000000..aaa69903b --- /dev/null +++ b/gui/indexConsole.html @@ -0,0 +1,13 @@ + + + + + + + Continue + + +
+ + + diff --git a/gui/src/components/console/Details.tsx b/gui/src/components/console/Details.tsx new file mode 100644 index 000000000..a9edfa8a6 --- /dev/null +++ b/gui/src/components/console/Details.tsx @@ -0,0 +1,116 @@ +import { useEffect, useRef } from "react"; +import { LLMInteraction } from "../../hooks/useLLMLog"; +import useLLMSummary from "../../hooks/useLLMSummary"; +import End from "./End"; +import ResultGroup from "./ResultGroup"; +import Start from "./Start"; +import StatusIcon from "./StatusIcon"; + +export interface DetailsProps { + interaction: LLMInteraction; +} + +function renderCell(children: React.ReactNode) { + return ( +
+ {children} +
+ ); +} + +/** + * A cell of statistics at the top of the details view + */ +function Cell({ + label, + value, + format, +}: { + label: string; + value: any; + format?: (value: any) => string; +}) { + return renderCell( + value != undefined ? `${label}: ${format ? format(value) : value}` : "", + ); +} + +/** + * A cell of statistics at the top of the details view with custom content + */ +function CustomCell({ children }: { children: React.ReactNode }) { + return renderCell(children); +} + +function formatSeconds(milliseconds: number) { + return (milliseconds / 1000).toFixed(2) + "s"; +} + +export default function Details({ interaction }: DetailsProps) { + const scrollTop = useRef(null); + const lastResult = useRef(null); + const summary = useLLMSummary(interaction); + + useEffect(() => { + if (interaction.end) { + return; + } + + const last = interaction.results[interaction.results.length - 1]; + if (last != lastResult.current) { + lastResult.current = last; + const lastChild = scrollTop.current?.lastChild; + if (lastChild) { + (lastChild as HTMLElement).scrollIntoView({ + behavior: "auto", + block: "end", + }); + } + } + }); + + return ( +
+
+
+ + + Result: + {summary.result} + +
+
+ + + +
+
+ + + v.toFixed(1)} + > +
+
+
+ {interaction.start ? : ""} +
+ {interaction.results.map((group, i) => { + return ; + })} +
+ {interaction.end ? : ""} +
+
+ ); +} diff --git a/gui/src/components/console/End.tsx b/gui/src/components/console/End.tsx new file mode 100644 index 000000000..382ac4bda --- /dev/null +++ b/gui/src/components/console/End.tsx @@ -0,0 +1,37 @@ +import { + LLMInteractionCancel, + LLMInteractionError, + LLMInteractionSuccess, +} from "core"; +import Expander from "./Expander"; +import Message from "./Message"; + +export interface StartProps { + item: LLMInteractionSuccess | LLMInteractionError | LLMInteractionCancel; +} + +export default function Start({ item }: StartProps) { + //
+ switch (item.kind) { + case "success": + return <>; + case "error": + return ( +
+ + Error + + {item.message} +
+ ); + break; + case "cancel": + return ( +
+ + Cancelled + +
+ ); + } +} diff --git a/gui/src/components/console/Expander.tsx b/gui/src/components/console/Expander.tsx new file mode 100644 index 000000000..f7d958463 --- /dev/null +++ b/gui/src/components/console/Expander.tsx @@ -0,0 +1,25 @@ +import { ChevronDownIcon, ChevronRightIcon } from "@heroicons/react/24/outline"; +import { ReactNode, useState } from "react"; + +export interface ExpanderProps { + label: string; + children: ReactNode; +} + +export default function Expander({ label, children }: ExpanderProps) { + const [expanded, setExpanded] = useState(false); + + return ( +
+
setExpanded(!expanded)}> + {expanded ? ( + + ) : ( + + )}{" "} + {label} +
+ {expanded &&
{children}
} +
+ ); +} diff --git a/gui/src/components/console/Layout.tsx b/gui/src/components/console/Layout.tsx new file mode 100644 index 000000000..d1dcd9266 --- /dev/null +++ b/gui/src/components/console/Layout.tsx @@ -0,0 +1,30 @@ +import { useState } from "react"; +import useLLMLog from "../../hooks/useLLMLog"; +import Details from "./Details"; +import List from "./List"; + +export default function Layout() { + const llmLog = useLLMLog(); + + const [selectedId, setSelectedId] = useState(undefined); + + const interaction = selectedId + ? llmLog.interactions.get(selectedId) + : undefined; + + return llmLog.loading ? ( +
Loading...
+ ) : ( +
+ { + setSelectedId(interactionId); + }} + > + {interaction && ( +
+ )} +
+ ); +} diff --git a/gui/src/components/console/List.tsx b/gui/src/components/console/List.tsx new file mode 100644 index 000000000..b10a15684 --- /dev/null +++ b/gui/src/components/console/List.tsx @@ -0,0 +1,53 @@ +import { useEffect, useRef, useState } from "react"; +import { LLMLog } from "../../hooks/useLLMLog"; +import ListItem from "./ListItem"; + +export interface ListProps { + llmLog: LLMLog; + onClickInteraction: (interactionId: string) => void; +} + +export default function List({ llmLog, onClickInteraction }: ListProps) { + const topRef = useRef(null); + const [selectedId, setSelectedId] = useState(undefined); + + const lastSize = useRef(0); + + useEffect(() => { + if (llmLog.order.length != lastSize.current) { + setSelectedId(llmLog.order[llmLog.order.length - 1]); + onClickInteraction(llmLog.order[llmLog.order.length - 1]); + + lastSize.current = llmLog.order.length; + const lastChild = topRef.current?.lastChild; + if (lastChild) { + (lastChild as HTMLElement).scrollIntoView({ + behavior: "smooth", + block: "end", + }); + } + } + }); + + return ( +
    + {llmLog.order.map((id) => ( + { + topRef.current?.focus(); + setSelectedId(interactionId); + onClickInteraction(interactionId); + }} + selected={id == selectedId} + > + ))} +
+ ); +} diff --git a/gui/src/components/console/ListItem.tsx b/gui/src/components/console/ListItem.tsx new file mode 100644 index 000000000..bbd4a4be3 --- /dev/null +++ b/gui/src/components/console/ListItem.tsx @@ -0,0 +1,55 @@ +import { LLMInteraction } from "../../hooks/useLLMLog"; +import useLLMSummary from "../../hooks/useLLMSummary"; +import StatusIcon from "./StatusIcon"; + +export interface ListItemProps { + interactionId: string; + interaction: LLMInteraction; + selected: boolean; + onClickInteraction: (interactionId: string) => void; +} + +function formatTimestamp(timestamp: number) { + const date = new Date(timestamp); + + const hours = date.getHours().toString().padStart(2, "0"); + const minutes = date.getMinutes().toString().padStart(2, "0"); + const seconds = date.getSeconds(); + const milliseconds = date.getMilliseconds(); + + // Format seconds with one decimal place + const secondsFormatted = `${seconds}.${Math.floor(milliseconds / 100)}`; + + return `${hours}:${minutes}:${secondsFormatted.padStart(4, "0")}`; +} + +export default function ListItem({ + interactionId, + interaction, + onClickInteraction, + selected, +}: ListItemProps) { + const summary = useLLMSummary(interaction); + + return ( +
  • onClickInteraction(interactionId)} + > + {" "} + + {interaction.start ? formatTimestamp(interaction.start.timestamp) : ""} + + {summary.type} +
  • + ); +} diff --git a/gui/src/components/console/Message.tsx b/gui/src/components/console/Message.tsx new file mode 100644 index 000000000..8c5c35bc6 --- /dev/null +++ b/gui/src/components/console/Message.tsx @@ -0,0 +1,106 @@ +import { + AssistantChatMessage, + ChatMessage, + ThinkingChatMessage, + UserChatMessage, +} from "core"; +import { memo } from "react"; + +export interface MessageProps { + message: ChatMessage; +} + +function renderMessageText(text: string) { + return {text}; +} + +export function renderMessageRole(role: ChatMessage["role"]) { + return ( +
    + + {role} + +
    + ); +} + +function renderMessageContent( + message: AssistantChatMessage | UserChatMessage | ThinkingChatMessage, +) { + if (typeof message.content == "string") { + return renderMessageText(message.content); + } else { + return message.content.map((part) => { + if (part.type == "text") { + return renderMessageText(part.text); + } else { + return
    Image: {part.imageUrl.url}
    ; + } + }); + } +} + +export function renderMessage(message: ChatMessage, includeRole: boolean) { + switch (message.role) { + case "assistant": + return ( + <> + {includeRole ? renderMessageRole(message.role) : ""} + {message.toolCalls + ? message.toolCalls.map((toolCall) => ( +
    Tool call: {JSON.stringify(toolCall, undefined, 2)}
    + )) + : ""} + {renderMessageContent(message)} + + ); + break; + case "thinking": + return ( + <> + {includeRole ? renderMessageRole(message.role) : ""} + {message.toolCalls + ? message.toolCalls.map((toolCall) => ( +
    Tool call: {JSON.stringify(toolCall, undefined, 2)}
    + )) + : ""} + {renderMessageContent(message)} + {message.redactedThinking && ( +
    Redacted Thinking: {message.redactedThinking}
    + )} + {message.signature &&
    Signature: {message.signature}
    } + + ); + break; + case "user": + return ( + <> + {includeRole ? renderMessageRole(message.role) : ""} + {renderMessageContent(message)} + + ); + break; + case "system": + return ( + <> + {includeRole ? renderMessageRole(message.role) : ""} + {renderMessageText(message.content)} + + ); + case "tool": + return ( + <> + {includeRole ? renderMessageRole(message.role) : ""} +
    Tool Call ID: {message.toolCallId}
    + {renderMessageText(message.content)} + + ); + break; + } +} + +const Message = memo(function Message({ message }: MessageProps) { + return renderMessage(message, true); +}); + +export default Message; diff --git a/gui/src/components/console/Result.tsx b/gui/src/components/console/Result.tsx new file mode 100644 index 000000000..6122fa2a8 --- /dev/null +++ b/gui/src/components/console/Result.tsx @@ -0,0 +1,37 @@ +import { memo } from "react"; +import { LLMResult } from "../../hooks/useLLMLog"; +import { renderMessage } from "./Message"; + +interface ResultProps { + result: LLMResult; + prevResult: LLMResult | undefined; +} + +const Result = memo(function Result({ result, prevResult }: ResultProps) { + switch (result.kind) { + case "chunk": + return {result.chunk}; + break; + case "message": + switch (result.message.role) { + case "assistant": + case "thinking": + const includeRole = !( + prevResult?.kind === "message" && + prevResult.message.role === result.message.role + ); + return renderMessage(result.message, includeRole); + default: + // We don't expect anything but AssistantChatMessages and ThinkingChatMessages in the reply + // from the LLM output, but log them if they do occur. + return ( +
    + {renderMessage(result.message, true)} +
    + ); + } + break; + } +}); + +export default Result; diff --git a/gui/src/components/console/ResultGroup.tsx b/gui/src/components/console/ResultGroup.tsx new file mode 100644 index 000000000..b7ed2933c --- /dev/null +++ b/gui/src/components/console/ResultGroup.tsx @@ -0,0 +1,19 @@ +import { memo } from "react"; +import { LLMResult } from "../../hooks/useLLMLog"; +import Result from "./Result"; + +interface ResultGroupProps { + group: LLMResult[]; +} + +const ResultGroup = memo(function ResultGroup({ group }: ResultGroupProps) { + return ( + <> + {group.map((result, i) => ( + + ))} + + ); +}); + +export default ResultGroup; diff --git a/gui/src/components/console/Start.tsx b/gui/src/components/console/Start.tsx new file mode 100644 index 000000000..a28601aaa --- /dev/null +++ b/gui/src/components/console/Start.tsx @@ -0,0 +1,73 @@ +import { + LLMInteractionStartChat, + LLMInteractionStartComplete, + LLMInteractionStartFim, +} from "core"; +import Expander from "./Expander"; +import Message from "./Message"; + +export interface StartProps { + item: + | LLMInteractionStartChat + | LLMInteractionStartComplete + | LLMInteractionStartFim; +} + +export default function Start({ item }: StartProps) { + return ( +
    + {(() => { + switch (item.kind) { + case "startChat": + return ( + <> + +
    + {item.messages.map((message, i) => ( + + ))} +
    +
    + +
    +                    {JSON.stringify(item.options, undefined, 2)}
    +                  
    +
    + + ); + break; + case "startComplete": + return ( + <> + +
    {item.prompt}
    +
    + +
    +                    {JSON.stringify(item.options, undefined, 2)}
    +                  
    +
    + + ); + break; + case "startFim": + return ( + <> + +
    {item.prefix}
    +
    + +
    {item.suffix}
    +
    + +
    +                    {JSON.stringify(item.options, undefined, 2)}
    +                  
    +
    + + ); + } + })()} +
    + ); +} diff --git a/gui/src/components/console/StatusIcon.tsx b/gui/src/components/console/StatusIcon.tsx new file mode 100644 index 000000000..51cbe7a91 --- /dev/null +++ b/gui/src/components/console/StatusIcon.tsx @@ -0,0 +1,34 @@ +import { + CheckIcon, + EllipsisHorizontalIcon, + StopCircleIcon, + XCircleIcon, +} from "@heroicons/react/24/outline"; +import { LLMInteraction } from "../../hooks/useLLMLog"; + +export interface StatusIconProps { + interaction: LLMInteraction; +} + +export default function StatusIcon({ interaction }: StatusIconProps) { + if (interaction.end) { + switch (interaction.end.kind) { + case "success": + return ( + + ); + case "cancel": + return ( + + ); + case "error": + return ( + + ); + } + } else { + return ( + + ); + } +} diff --git a/gui/src/console.tsx b/gui/src/console.tsx new file mode 100644 index 000000000..beb7c4e53 --- /dev/null +++ b/gui/src/console.tsx @@ -0,0 +1,12 @@ +import React from "react"; +import ReactDOM from "react-dom/client"; +import Layout from "./components/console/Layout"; +import "./indexConsole.css"; + +(async () => { + ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( + + + , + ); +})(); diff --git a/gui/src/hooks/useLLMLog.ts b/gui/src/hooks/useLLMLog.ts new file mode 100644 index 000000000..0df2c56e1 --- /dev/null +++ b/gui/src/hooks/useLLMLog.ts @@ -0,0 +1,225 @@ +import { + LLMInteractionCancel, + LLMInteractionChunk, + LLMInteractionError, + LLMInteractionItem, + LLMInteractionMessage, + LLMInteractionStartChat, + LLMInteractionStartComplete, + LLMInteractionStartFim, + LLMInteractionSuccess, +} from "core"; +import { useEffect, useReducer } from "react"; + +declare const vscode: any; + +export type LLMResult = LLMInteractionMessage | LLMInteractionChunk; + +const MAX_GROUP_LENGTH = 32; + +interface ToConsoleViewInit { + type: "init"; + uuid: string; + items: LLMInteractionItem[]; +} + +interface ToConsoleViewItem { + type: "item"; + uuid: string; + item: LLMInteractionItem; +} + +interface ToConsoleViewClear { + type: "clear"; + uuid: string; +} + +type ToConsoleView = ToConsoleViewInit | ToConsoleViewItem | ToConsoleViewClear; + +/** + * Represents a linear list of LLMInteractionItem, transformed into + * a form that is more convenient for rendering. + */ +export interface LLMInteraction { + start?: + | LLMInteractionStartChat + | LLMInteractionStartComplete + | LLMInteractionStartFim; + + // We use an array-of-arrays for efficiency when rendering a streamed result + // with lots and lots of separate tokens; instead of having a linear list + // of 1024 Result components, we have 32 ResultGroup components each rendering + // 32 Results. + // + // Inline content can split between one group and the next - we'll let the + // browser engine sort that out. + results: LLMResult[][]; + + end?: LLMInteractionSuccess | LLMInteractionError | LLMInteractionCancel; +} + +export interface LLMLog { + loading: boolean; + interactions: Map; + order: string[]; +} + +/** + * Appends a new result item (chunk or message) to an interaction, + * existing iteractions must already have been copied with + * copyInteractionForMutation(). + */ +function appendItemToInteractionResult( + interaction: LLMInteraction, + item: LLMInteractionMessage | LLMInteractionChunk, +) { + let lastGroup = interaction.results[interaction.results.length - 1]; + + if (lastGroup == undefined || lastGroup.length == MAX_GROUP_LENGTH) { + interaction.results.push([item]); + } else { + lastGroup.push(item); + } +} + +/** + * Makes a copy of an interaction that can be mutated without changing + * any objects/arrays that are part of the old state. We always make + * a copy of the last (open) result group, so that we can simply append + * to it; this is very slightly inefficient for the case where we're + * handling a single end item with no results, but makes things simpler. + */ +function copyInteractionForMutation(oldInteraction: LLMInteraction) { + const oldResults = oldInteraction.results; + let oldLastGroup = oldInteraction.results[oldInteraction.results.length - 1]; + let newResults; + + if (oldLastGroup == undefined || oldLastGroup.length == MAX_GROUP_LENGTH) { + newResults = [...oldInteraction.results]; + } else { + newResults = oldInteraction.results.slice(0, -1); + newResults.push([...oldLastGroup]); + } + + return { ...oldInteraction, results: newResults }; +} + +function appendItemsToLLMLog( + oldLog: LLMLog, + items: LLMInteractionItem[], +): LLMLog { + const oldInteractions = oldLog.interactions; + const newInteractions: Map = new Map(); + let order = oldLog.order; + let interactionsAdded = false; + + // Add the new items to the log, making mutable copies of old + // LLMInteraction as necessary + for (const item of items) { + let interaction = newInteractions.get(item.interactionId); + if (interaction === undefined) { + const oldInteraction = oldInteractions.get(item.interactionId); + if (oldInteraction) { + interaction = copyInteractionForMutation(oldInteraction); + } else { + interaction = { + results: [], + }; + + if (interactionsAdded) { + order.push(item.interactionId); + } else { + order = [...order, item.interactionId]; + interactionsAdded = true; + } + } + newInteractions.set(item.interactionId, interaction); + } + + switch (item.kind) { + case "startChat": + case "startComplete": + case "startFim": + interaction.start = item; + break; + case "chunk": + case "message": + appendItemToInteractionResult(interaction, item); + break; + case "success": + case "error": + case "cancel": + interaction.end = item; + break; + } + } + + // Copy over unchanged interactions + for (const interactionId of oldInteractions.keys()) { + if (!newInteractions.has(interactionId)) { + newInteractions.set(interactionId, oldInteractions.get(interactionId)!); + } + } + + return { + loading: false, + interactions: newInteractions, + order, + }; +} + +/** + * Hook to accumulate log data structures based on messages passed + * from the core. Note that each call site will create an independent + * data structure, so this should be only used once at a toplevel + * component. + * @returns currently log datastructure. + */ +export default function useLLMLog() { + const [llmLog, dispatchLlmLog] = useReducer( + (llmLog: LLMLog, message: ToConsoleView) => { + switch (message.type) { + case "init": + return appendItemsToLLMLog(llmLog, message.items); + case "item": + return appendItemsToLLMLog(llmLog, [message.item]); + case "clear": + return { + loading: false, + interactions: new Map(), + order: [], + }; + } + }, + { + loading: true, + interactions: new Map(), + order: [], + }, + ); + + useEffect(function () { + // The uuid here marks the "generation" when the webview is + // reloaded, so we don't get confused if there are inflight + // messages from the previous generation. In particular, this + // avoids problems when React.StrictMode runs this effect + // twice - we don't want to process two "init" messages. + const uuid = crypto.randomUUID(); + const onMessage = (event: MessageEvent) => { + if (event.data.uuid !== uuid) { + return; + } + + dispatchLlmLog(event.data); + }; + window.addEventListener("message", onMessage); + vscode.postMessage({ type: "start", uuid }); + + return () => { + vscode.postMessage({ type: "stop", uuid }); + window.removeEventListener("message", onMessage); + }; + }, []); + + return llmLog; +} diff --git a/gui/src/hooks/useLLMSummary.ts b/gui/src/hooks/useLLMSummary.ts new file mode 100644 index 000000000..b5016e767 --- /dev/null +++ b/gui/src/hooks/useLLMSummary.ts @@ -0,0 +1,76 @@ +import { useMemo } from "react"; +import { LLMInteraction } from "./useLLMLog"; + +/** + * Hook returning memoized information about single logged prompt/response + * query to the LLM. + */ +export default function useLLMSummary(interaction: LLMInteraction) { + return useMemo(() => { + if (interaction.start == undefined) { + return { + result: "", + type: "", + }; + } + + const type = interaction.start.kind.slice(5); + + let result; + switch (interaction.end?.kind) { + case "cancel": + result = "Cancelled"; + break; + case "error": + result = "Error"; + break; + case "success": + result = "Success"; + break; + case undefined: + result = ""; + } + + let totalTime, + toFirstToken, + tokensPerSecond, + promptTokens, + generatedTokens, + thinkingTokens; + if (interaction.start != undefined && interaction.results.length > 0) { + const firstGroup = interaction.results[0]; + const firstItem = firstGroup ? firstGroup[0] : undefined; + toFirstToken = firstItem + ? firstItem.timestamp - interaction.start.timestamp + : undefined; + } + if (interaction.end != undefined) { + totalTime = interaction.end.timestamp - interaction.start.timestamp; + promptTokens = interaction.end.promptTokens; + generatedTokens = interaction.end.generatedTokens; + thinkingTokens = interaction.end.thinkingTokens; + if (toFirstToken != undefined) { + tokensPerSecond = + (generatedTokens + thinkingTokens) / + ((totalTime - toFirstToken) / 1000); + } + } else { + const lastGroup = interaction.results[interaction.results.length - 1]; + const lastItem = lastGroup ? lastGroup[lastGroup.length - 1] : undefined; + totalTime = lastItem + ? lastItem.timestamp - interaction.start.timestamp + : undefined; + } + + return { + result, + type, + totalTime, + toFirstToken, + tokensPerSecond, + promptTokens, + generatedTokens, + thinkingTokens, + }; + }, [interaction]); +} diff --git a/gui/src/indexConsole.css b/gui/src/indexConsole.css new file mode 100644 index 000000000..c5e41252f --- /dev/null +++ b/gui/src/indexConsole.css @@ -0,0 +1,9 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +html, +body, +#root { + height: 100%; +} diff --git a/gui/vite.config.ts b/gui/vite.config.ts index 6c5cd6d75..22ce4b733 100644 --- a/gui/vite.config.ts +++ b/gui/vite.config.ts @@ -1,4 +1,5 @@ import react from "@vitejs/plugin-react-swc"; +import { resolve } from "path"; import tailwindcss from "tailwindcss"; import { defineConfig } from "vitest/config"; @@ -9,6 +10,10 @@ export default defineConfig({ // Change the output .js filename to not include a hash rollupOptions: { // external: ["vscode-webview"], + input: { + index: resolve(__dirname, "index.html"), + indexConsole: resolve(__dirname, "indexConsole.html"), + }, output: { entryFileNames: `assets/[name].js`, chunkFileNames: `assets/[name].js`,