The @theia/ai-chat extension provides the concept of a language model chat to Theia.
It serves as the basis for @theia/ai-chat-ui to provide the Chat UI.
When implementing tool handlers, there are two patterns depending on whether your tool requires chat-specific features:
For tools that only need basic context like cancellation support:
import { ToolInvocationContext, ToolProvider, ToolRequest } from '@theia/ai-core';
@injectable()
export class MyGenericTool implements ToolProvider {
getTool(): ToolRequest {
return {
id: 'myTool',
name: 'myTool',
description: 'A generic tool',
parameters: { type: 'object', properties: {} },
handler: async (args: string, ctx?: ToolInvocationContext) => {
if (ctx?.cancellationToken?.isCancellationRequested) {
return JSON.stringify({ error: 'Operation cancelled' });
}
// Tool implementation
return 'result';
}
};
}
}
For tools that need access to the chat session, request model, or response:
import { assertChatContext, ChatToolContext } from '@theia/ai-chat';
import { ToolInvocationContext, ToolProvider, ToolRequest } from '@theia/ai-core';
@injectable()
export class MyChatTool implements ToolProvider {
getTool(): ToolRequest {
return {
id: 'myChatTool',
name: 'myChatTool',
description: 'A chat-bound tool',
parameters: { type: 'object', properties: {} },
handler: async (args: string, ctx?: ToolInvocationContext) => {
assertChatContext(ctx); // Throws if not in chat context
if (ctx.cancellationToken?.isCancellationRequested) {
return JSON.stringify({ error: 'Operation cancelled' });
}
// Access chat-specific features
const sessionId = ctx.request.session.id;
ctx.request.session.changeSet.addElements(...);
return 'result';
}
};
}
}
The assertChatContext() function serves as both a runtime validator and TypeScript type guard, ensuring the context is a ChatToolContext with request and response properties.
"Theia" is a trademark of the Eclipse Foundation https://www.eclipse.org/theia