tool use is getting close! minor fixes + UI

This commit is contained in:
Andrew Pareles 2025-03-07 03:07:06 -08:00
parent 6e2a803628
commit cd532435c2
5 changed files with 37 additions and 15 deletions

View file

@ -428,9 +428,14 @@ class ChatThreadService extends Disposable implements IChatThreadService {
// 1. validate tool params
let toolParams: ToolCallParams[typeof toolName]
try {
console.log('A')
const params = await this._toolsService.validateParams[toolName](tool.paramsStr)
console.log('B')
toolParams = params
} catch (error) {
console.log('ERR1')
const errorMessage = getErrorMessage(error)
this._addMessageToThread(threadId, { role: 'tool', name: toolName, paramsStr: tool.paramsStr, id: tool.id, content: errorMessage, result: { type: 'error', value: errorMessage }, })
res_()
@ -439,14 +444,22 @@ class ChatThreadService extends Disposable implements IChatThreadService {
// 2. if tool requires approval, await the approval
if (toolNamesThatRequireApproval.has(toolName)) {
console.log('C')
const voidToolId = generateUuid()
console.log('D')
const toolApprovalPromise = new Promise<void>((res, rej) => { this.resRejOfToolAwaitingApproval[voidToolId] = { res, rej } })
console.log('E')
this._addMessageToThread(threadId, { role: 'tool_request', name: toolName, params: toolParams, voidToolId: voidToolId })
try {
console.log('F')
await toolApprovalPromise
// accepted tool
}
catch (e) {
console.log('ERR2')
const errorMessage = 'Tool call was rejected by the user.'
this._addMessageToThread(threadId, { role: 'tool', name: toolName, paramsStr: tool.paramsStr, id: tool.id, content: errorMessage, result: { type: 'error', value: errorMessage }, })
res_()
@ -457,8 +470,10 @@ class ChatThreadService extends Disposable implements IChatThreadService {
// 3. call the tool
let toolResult: ToolResultType[typeof toolName]
try {
console.log('G')
toolResult = await this._toolsService.callTool[toolName](toolParams as any) // typescript is so bad it doesn't even couple the type of ToolResult with the type of the function being called here
} catch (error) {
console.log('ERR3')
const errorMessage = getErrorMessage(error)
this._addMessageToThread(threadId, { role: 'tool', name: toolName, paramsStr: tool.paramsStr, id: tool.id, content: errorMessage, result: { type: 'error', value: errorMessage }, })
res_()
@ -468,7 +483,11 @@ class ChatThreadService extends Disposable implements IChatThreadService {
// 4. stringify the result to give the LLM
let toolResultStr: string
try {
toolResultStr = await this._toolsService.stringOfResult[toolName](toolParams as any, toolResult as any)
console.log('H')
toolResultStr = this._toolsService.stringOfResult[toolName](toolParams as any, toolResult as any)
// if (Math.random() > 0) throw new Error('This is not an allowed repo.')
} catch (error) {
const errorMessage = `Tool call succeeded, but there was an error stringifying the output.\n${getErrorMessage(error)}`
this._addMessageToThread(threadId, { role: 'tool', name: toolName, paramsStr: tool.paramsStr, id: tool.id, content: errorMessage, result: { type: 'error', value: errorMessage }, })
@ -476,6 +495,8 @@ class ChatThreadService extends Disposable implements IChatThreadService {
return
}
console.log('I')
// 5. add to history
this._addMessageToThread(threadId, { role: 'tool', name: toolName, paramsStr: tool.paramsStr, id: tool.id, content: toolResultStr, result: { type: 'success', params: toolParams, value: toolResult }, })
res_()
@ -492,7 +513,9 @@ class ChatThreadService extends Disposable implements IChatThreadService {
if (llmCancelToken === null) break
this._setStreamState(threadId, { streamingToken: llmCancelToken })
console.log('awaiting agentloop')
await awaitable
console.log('done')
}
}

View file

@ -1636,8 +1636,7 @@ class EditCodeService extends Disposable implements IEditCodeService {
latestStreamLocationMutable = null
shouldUpdateOrigStreamStyle = true
oldBlocks = []
addedTrackingZoneOfBlockNum.slice(0, Infinity) // clear the array
console.log('SHOULD BE EMPTY', addedTrackingZoneOfBlockNum)
addedTrackingZoneOfBlockNum.splice(0, Infinity) // clear the array
shouldSendAnotherMessage = true
this._refreshStylesAndDiffsInURI(uri)

View file

@ -1008,12 +1008,12 @@ const ToolError = ({ title, errorMessage }: { title: string, errorMessage: strin
const toolNameToTitle: Record<ToolName, string> = {
'read_file': 'Read file',
'list_dir': 'Inspected folder',
'pathname_search': 'Searched filename',
'search': 'Searched',
'create_uri': 'Created file',
'delete_uri': 'Deleted file',
'edit': 'Edited file',
'list_dir': 'Inspect folder',
'pathname_search': 'Search (path only)',
'search': 'Search (file contents)',
'create_uri': 'Create file',
'delete_uri': 'Delete file',
'edit': 'Edit file',
'terminal_command': 'Ran terminal command'
}
@ -1292,7 +1292,8 @@ const ChatBubble = ({ chatMessage, isLoading, messageIdx }: ChatBubbleProps) =>
/>
}
else if (role === 'tool_request') {
if (!isLoading) return null
const isLastMessage = true // TODO!!! fix this
if (!isLastMessage) return null
const ToolMessageComponent = toolNameToComponent[chatMessage.name].requestWrapper as React.FC<{ toolRequest: any }> // ts isnt smart enough...
return <>
<ToolMessageComponent
@ -1410,7 +1411,8 @@ export const SidebarChat = () => {
return previousMessages.map((message, i) =>
<ChatBubble key={getChatBubbleId(currentThread.id, i)} chatMessage={message} messageIdx={i} />
)
}, [previousMessages])
}, [previousMessages, currentThread])
const streamingChatIdx = pastMessagesHTML.length

View file

@ -459,7 +459,7 @@ export const FeaturesTab = () => {
<div className='w-full'>
<h4 className={`text-base`}>{displayInfoOfFeatureName('Apply')}</h4>
<div className='text-sm italic text-void-fg-3 my-1'>We recommend the smartest model you{`'`}ve got, like Claude 3.7 or Grok 3.</div>
<div className='text-sm italic text-void-fg-3 my-1'>We recommend the smartest model you{`'`}ve got, like Claude 3.7 or GPT 4o.</div>
<ModelDropdown featureName={'Apply'} />
</div>
</div>

View file

@ -316,8 +316,6 @@ export class ToolsService implements IToolsService {
this.validateParams = {
read_file: async (params: string) => {
console.log('read_file')
const o = validateJSON(params)
const { uri: uriStr, pageNumber: pageNumberUnknown } = o
@ -372,12 +370,12 @@ export class ToolsService implements IToolsService {
},
edit: async (params: string) => {
console.log('validating edit!!!')
const o = validateJSON(params)
const { uri: uriStr, changeDescription: changeDescriptionUnknown } = o
const uri = validateURI(uriStr)
const changeDescription = validateStr('changeDescription', changeDescriptionUnknown)
return { uri, changeDescription }
},