diff --git a/core/index.d.ts b/core/index.d.ts index 7cbf0e25b..e69fa8ee2 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -43,13 +43,13 @@ export interface IndexingProgressUpdate { desc: string; shouldClearIndexes?: boolean; status: - | "loading" - | "indexing" - | "done" - | "failed" - | "paused" - | "disabled" - | "cancelled"; + | "loading" + | "indexing" + | "done" + | "failed" + | "paused" + | "disabled" + | "cancelled"; debugInfo?: string; } @@ -353,6 +353,12 @@ export interface UserChatMessage { content: MessageContent; } +export interface ThinkingChatMessage { + role: "thinking"; + content: MessageContent; + toolCalls?: ToolCallDelta[]; +} + export interface AssistantChatMessage { role: "assistant"; content: MessageContent; @@ -367,6 +373,7 @@ export interface SystemChatMessage { export type ChatMessage = | UserChatMessage | AssistantChatMessage + | ThinkingChatMessage | SystemChatMessage | ToolResultChatMessage; @@ -675,10 +682,10 @@ export interface IDE { getCurrentFile(): Promise< | undefined | { - isUntitled: boolean; - path: string; - contents: string; - } + isUntitled: boolean; + path: string; + contents: string; + } >; getLastFileSaveTimestamp?(): number; @@ -862,11 +869,11 @@ export interface CustomCommand { export interface Prediction { type: "content"; content: - | string - | { - type: "text"; - text: string; - }[]; + | string + | { + type: "text"; + text: string; + }[]; } export interface ToolExtras { @@ -1204,9 +1211,9 @@ export interface Config { embeddingsProvider?: EmbeddingsProviderDescription | ILLM; /** The model that Continue will use for tab autocompletions. */ tabAutocompleteModel?: - | CustomLLM - | ModelDescription - | (CustomLLM | ModelDescription)[]; + | CustomLLM + | ModelDescription + | (CustomLLM | ModelDescription)[]; /** Options for tab autocomplete */ tabAutocompleteOptions?: Partial; /** UI styles customization */ @@ -1298,9 +1305,9 @@ export type PackageDetailsSuccess = PackageDetails & { export type PackageDocsResult = { packageInfo: ParsedPackageInfo; } & ( - | { error: string; details?: never } - | { details: PackageDetailsSuccess; error?: never } -); + | { error: string; details?: never } + | { details: PackageDetailsSuccess; error?: never } + ); export interface TerminalOptions { reuseTerminal?: boolean; diff --git a/core/llm/index.ts b/core/llm/index.ts index ccbad6699..b08d8b214 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -762,6 +762,7 @@ export abstract class BaseLLM implements ILLM { } } + let thinking = ""; let completion = ""; try { @@ -812,6 +813,13 @@ export abstract class BaseLLM implements ILLM { )) { completion += chunk.content; yield chunk; + completion += chunk.content; + yield chunk; + } + + if (chunk.role === "thinking") { + thinking += chunk.content; + } } } } @@ -823,6 +831,18 @@ export abstract class BaseLLM implements ILLM { this._logTokensGenerated(completionOptions.model, prompt, completion); if (logEnabled && this.writeLog) { + if (thinking) { + await this.writeLog(`Thinking:\n${thinking}\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. + */ await this.writeLog(`Completion:\n${completion}\n\n`); } diff --git a/core/llm/llms/Anthropic.ts b/core/llm/llms/Anthropic.ts index dcd13d38e..bdbd67639 100644 --- a/core/llm/llms/Anthropic.ts +++ b/core/llm/llms/Anthropic.ts @@ -32,9 +32,9 @@ class Anthropic extends BaseLLM { })), tool_choice: options.toolChoice ? { - type: "tool", - name: options.toolChoice.function.name, - } + type: "tool", + name: options.toolChoice.function.name, + } : undefined, }; @@ -174,12 +174,12 @@ class Anthropic extends BaseLLM { messages: msgs, system: shouldCacheSystemMessage ? [ - { - type: "text", - text: this.systemMessage, - cache_control: { type: "ephemeral" }, - }, - ] + { + type: "text", + text: this.systemMessage, + cache_control: { type: "ephemeral" }, + }, + ] : systemMessage, }), signal, @@ -223,6 +223,9 @@ class Anthropic extends BaseLLM { case "text_delta": yield { role: "assistant", content: value.delta.text }; break; + case "thinking_delta": + yield { role: "thinking", content: value.delta.thinking }; + break; case "input_json_delta": if (!lastToolUseId || !lastToolUseName) { throw new Error("No tool use found");