-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow passing context to tool calls
With this change we enable passing optional context to tool calls. This is then used for tool calls in the context of a chat to pass on the chat request model. This way tool call handlers can retrieve additional information as a context or update the chat model directly.
- Loading branch information
Showing
5 changed files
with
89 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2025 EclipseSource GmbH. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { ToolRequest } from '@theia/ai-core'; | ||
import { injectable } from '@theia/core/shared/inversify'; | ||
import { ChatRequestModelImpl } from './chat-model'; | ||
|
||
export interface ChatToolRequest extends ToolRequest { | ||
handler: ( | ||
arg_string: string, | ||
context: ChatRequestModelImpl, | ||
) => Promise<unknown>; | ||
} | ||
|
||
/** | ||
* Wraps tool requests in a chat context. | ||
* | ||
* This service extracts tool requests from a given chat request model and wraps their | ||
* handler functions to provide additional context, such as the chat request model. | ||
*/ | ||
@injectable() | ||
export class ChatToolRequestService { | ||
|
||
getChatToolRequests(request: ChatRequestModelImpl): ChatToolRequest[] { | ||
const toolRequests = request.message.toolRequests.size > 0 ? [...request.message.toolRequests.values()] : undefined; | ||
if (!toolRequests) { | ||
return []; | ||
} | ||
return this.toChatToolRequests(toolRequests, request); | ||
} | ||
|
||
toChatToolRequests(toolRequests: ToolRequest[] | undefined, request: ChatRequestModelImpl): ChatToolRequest[] { | ||
if (!toolRequests) { | ||
return []; | ||
} | ||
return toolRequests.map(toolRequest => this.toChatToolRequest(toolRequest, request)); | ||
} | ||
|
||
protected toChatToolRequest(toolRequest: ToolRequest, request: ChatRequestModelImpl): ChatToolRequest { | ||
return { | ||
...toolRequest, | ||
handler: async (arg_string: string) => toolRequest.handler(arg_string, request) | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters