Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow passing context to tool calls #14751

Merged
merged 1 commit into from
Jan 22, 2025
Merged

Conversation

planger
Copy link
Contributor

@planger planger commented Jan 21, 2025

What it does

With this change we enable passing optional context to tool calls.

For tool calls in the context of a chat, we pass the chat request model. This way tool call handlers can retrieve additional information as a context or update the chat model directly. Note that this can also be used to receive the cancellation token via request.response.cancellationToken to be able to cancel long-running tool calls if the user hits cancel in the chat.

How to test

Create a tool call that receives and processes the optional second argument of its handler function.
E.g.

@injectable()
export class FileContentFunction implements ToolProvider {
    static ID = FILE_CONTENT_FUNCTION_ID;

    getTool(): ToolRequest {
        return {
            id: FileContentFunction.ID,
            name: FileContentFunction.ID,
            description: `The relative path to the target file within the workspace. This path is resolved from the workspace root, and only files within the workspace boundaries
             are accessible. Attempting to access paths outside the workspace will result in an error.`,
            parameters: {
                type: 'object',
                properties: {
                    file: {
                        type: 'string',
                        description: `Return the content of a specified file within the workspace. The file path must be provided relative to the workspace root. Only files within
                         workspace boundaries are accessible; attempting to access files outside the workspace will return an error.`,
                    }
                },
                required: ['file']
            },
            handler: (arg_string: string, ctx: ChatRequestModelImpl) => {
                // if it is a handler that should work in the context of a chat and outside too, it can receive `unknown`
                // and use an instanceof check or ChatRequestModel.is() to switch between cases
                const file = this.parseArg(arg_string);
                return this.getFileContent(file);
            }
        };
    }
...

Follow-ups

None

Breaking changes

  • This PR introduces breaking changes and requires careful review. If yes, the breaking changes section in the changelog has been updated.

Attribution

None

Review checklist

Reminder for reviewers

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.
Copy link
Contributor

@JonasHelming JonasHelming left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works well, thank you!

@planger planger merged commit d493ea9 into master Jan 22, 2025
11 checks passed
@planger planger added this to the 1.58.0 milestone Jan 22, 2025
@planger planger deleted the planger/pass-ctx-to-tools branch January 22, 2025 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants