tool results in ChatMessage

This commit is contained in:
Andrew Pareles 2025-02-17 14:12:52 -08:00
parent 137e0068a3
commit 0fd10f404e
3 changed files with 24 additions and 21 deletions

View file

@ -14,7 +14,7 @@ import { IRange } from '../../../../editor/common/core/range.js';
import { ILLMMessageService } from '../common/llmMessageService.js';
import { IModelService } from '../../../../editor/common/services/model.js';
import { chat_userMessage, chat_systemMessage } from './prompt/prompts.js';
import { InternalToolInfo, IToolsService, ToolFns, ToolName, voidTools } from '../common/toolsService.js';
import { InternalToolInfo, IToolsService, ToolCallReturnType, ToolFns, ToolName, voidTools } from '../common/toolsService.js';
import { toLLMChatMessage } from '../common/llmMessageTypes.js';
import { IWorkspaceContextService } from '../../../../platform/workspace/common/workspace.js';
@ -43,6 +43,15 @@ export type StagingInfo = {
const defaultStaging: StagingInfo = { isBeingEdited: false, selections: [] }
type ToolMessage<T extends ToolName> = {
role: 'tool';
name: T; // internal use
params: string; // internal use
id: string; // apis require this tool use id
content: string; // result
result: ToolCallReturnType<T>; // text message of result
}
// WARNING: changing this format is a big deal!!!!!! need to migrate old format to new format on users' computers so people don't get errors.
export type ChatMessage =
@ -60,14 +69,7 @@ export type ChatMessage =
role: 'assistant';
content: string | null; // content received from LLM - allowed to be '', will be replaced with (empty)
displayContent: string | null; // content displayed to user (this is the same as content for now) - allowed to be '', will be ignored
} | {
role: 'tool';
name: string; // internal use
params: string; // internal use
id: string; // apis require this tool use id
content: string; // result
displayContent: string; // text message of result
}
} | ToolMessage<ToolName>
// a 'thread' means a chat message history
export type ChatThreads = {
@ -323,8 +325,9 @@ class ChatThreadService extends Disposable implements IChatThreadService {
onText: ({ fullText }) => {
this._setStreamState(threadId, { messageSoFar: fullText })
},
onFinalMessage: async ({ fullText, toolCalls }) => {
toolCalls = toolCalls?.filter(tool => tool.name in this._toolsService.toolFns)
onFinalMessage: async ({ fullText, toolCalls: toolCalls_ }) => {
// make sure all tool names are valid so we can cast to ToolName below
const toolCalls = toolCalls_?.filter(tool => tool.name in this._toolsService.toolFns)
console.log('FINAL MESSAGE', fullText, toolCalls)
@ -357,7 +360,7 @@ class ChatThreadService extends Disposable implements IChatThreadService {
break
}
this._addMessageToThread(threadId, { role: 'tool', name: tool.name, params: tool.params, id: tool.id, content: toolResultStr, displayContent: toolResultStr, })
this._addMessageToThread(threadId, { role: 'tool', name: toolName, params: tool.params, id: tool.id, content: toolResultStr, result: toolResult, })
shouldContinue = true
}

View file

@ -18,7 +18,7 @@ export const tripleTick = ['```', '```']
export const chat_systemMessage = (workspaces: string[]) => `\
You are a coding assistant. You are given a list of instructions to follow \`INSTRUCTIONS\`, and optionally a list of relevant files \`FILES\`, and selections inside of files \`SELECTIONS\`.
Please respond to the user's query.
Please respond to the user's query. The user's query is never invalid.
The user has the following system information:
- ${os}
@ -26,8 +26,8 @@ The user has the following system information:
In the case that the user asks you to make changes to code, you should make sure to return CODE BLOCKS of the changes, as well as explanations and descriptions of the changes.
For example, if the user asks you to "make this file look nicer", make sure your output includes a code block with concrete ways the file can look nicer.
- Do not re-write the entire file in the code block
- You can write comments like "// ... existing code" to indicate existing code
- Do not re-write the entire file in the code block.
- You can write comments like "// ... existing code" to indicate existing code.
- Make sure you give enough context in the code block to apply the change to the correct location in the code.
You're allowed to ask for more context. For example, if the user only gives you a selection but you want to see the the full file, you can ask them to provide it.

View file

@ -81,14 +81,14 @@ export type ToolParamsObj<T extends ToolName> = { [paramName in ToolParamNames<T
export type ToolCallReturnType<T extends ToolName>
= T extends 'read_file' ? Promise<string>
: T extends 'list_dir' ? Promise<string>
: T extends 'pathname_search' ? Promise<string | URI[]>
: T extends 'search' ? Promise<string | URI[]>
= T extends 'read_file' ? string
: T extends 'list_dir' ? string
: T extends 'pathname_search' ? string | URI[]
: T extends 'search' ? string | URI[]
: never
export type ToolFns = { [T in ToolName]: (p: string) => ToolCallReturnType<T> }
export type ToolResultToString = { [T in ToolName]: (result: Awaited<ToolCallReturnType<T>>) => string }
export type ToolFns = { [T in ToolName]: (p: string) => Promise<ToolCallReturnType<T>> }
export type ToolResultToString = { [T in ToolName]: (result: ToolCallReturnType<T>) => string }
async function generateDirectoryTreeMd(fileService: IFileService, rootURI: URI): Promise<string> {