add ThinkingChatMessage interface and handle thinking chunks in BaseLLM and Anthropic classes

This commit is contained in:
ferenci84 2025-03-01 14:13:29 +01:00
parent e1008b3d24
commit 44d91ece03
3 changed files with 61 additions and 31 deletions

51
core/index.d.ts vendored
View file

@ -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<TabAutocompleteOptions>;
/** 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;

View file

@ -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`);
}

View file

@ -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");