Skip to content

Commit dab7982

Browse files
committed
Add a tool registry
1 parent 3abc778 commit dab7982

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ import { ISecretsManager, SecretsManager } from 'jupyter-secrets-manager';
2323

2424
import { ChatHandler, welcomeMessage } from './chat-handler';
2525
import { CompletionProvider } from './completion-provider';
26+
import { stopItem, toolSelect } from './components';
2627
import { defaultProviderPlugins } from './default-providers';
2728
import { AIProviderRegistry } from './provider';
2829
import { aiSettingsRenderer, textArea } from './settings';
29-
import { IAIProviderRegistry, PLUGIN_IDS } from './tokens';
30-
import { stopItem } from './components/stop-button';
30+
import { IAIProviderRegistry, IToolRegistry, PLUGIN_IDS } from './tokens';
3131

3232
const chatCommandRegistryPlugin: JupyterFrontEndPlugin<IChatCommandRegistry> = {
3333
id: PLUGIN_IDS.chatCommandRegistry,
@@ -301,12 +301,24 @@ const systemPromptsPlugin: JupyterFrontEndPlugin<void> = {
301301
}
302302
};
303303

304+
const toolRegistryPlugin: JupyterFrontEndPlugin<IToolRegistry> = {
305+
id: PLUGIN_IDS.toolRegistry,
306+
autoStart: true,
307+
provides: IToolRegistry,
308+
activate: (app: JupyterFrontEnd): IToolRegistry => {
309+
const registry = new ToolsRegistry();
310+
registry.add(testTool);
311+
return registry;
312+
}
313+
};
314+
304315
export default [
305316
providerRegistryPlugin,
306317
chatCommandRegistryPlugin,
307318
chatPlugin,
308319
completerPlugin,
309320
systemPromptsPlugin,
321+
toolRegistryPlugin,
310322
...defaultProviderPlugins
311323
];
312324

src/tokens.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
2+
import { StructuredToolInterface } from '@langchain/core/tools';
23
import { ReadonlyPartialJSONObject, Token } from '@lumino/coreutils';
34
import { ISignal } from '@lumino/signaling';
45
import { JSONSchema7 } from 'json-schema';
@@ -12,7 +13,8 @@ export const PLUGIN_IDS = {
1213
completer: '@jupyterlite/ai:completer',
1314
providerRegistry: '@jupyterlite/ai:provider-registry',
1415
settingsConnector: '@jupyterlite/ai:settings-connector',
15-
systemPrompts: '@jupyterlite/ai:system-prompts'
16+
systemPrompts: '@jupyterlite/ai:system-prompts',
17+
toolRegistry: '@jupyterlite/ai:tool-registry'
1618
};
1719

1820
export type ModelRole = 'chat' | 'completer';
@@ -157,10 +159,41 @@ export interface IAIProviderRegistry {
157159
readonly completerError: string;
158160
}
159161

162+
/**
163+
* The type describing a tool used in langgraph.
164+
*/
165+
export type Tool = StructuredToolInterface;
166+
167+
/**
168+
* The tool registry interface.
169+
*/
170+
export interface IToolRegistry {
171+
/**
172+
* Get the registered tool names.
173+
*/
174+
readonly toolNames: string[];
175+
/**
176+
* A signal triggered when the tools has changed;
177+
*/
178+
readonly toolsChanged: ISignal<IToolRegistry, void>;
179+
/**
180+
* Add a new tool.
181+
*/
182+
add(provider: Tool): void;
183+
}
184+
160185
/**
161186
* The provider registry token.
162187
*/
163188
export const IAIProviderRegistry = new Token<IAIProviderRegistry>(
164189
'@jupyterlite/ai:provider-registry',
165190
'Provider for chat and completion LLM provider'
166191
);
192+
193+
/**
194+
* The tool registry token.
195+
*/
196+
export const IToolRegistry = new Token<IToolRegistry>(
197+
'@jupyterlite/ai:tool-registry',
198+
'Tool registry for AI agent'
199+
);

src/tool-registry.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ISignal, Signal } from '@lumino/signaling';
2+
import { IToolRegistry, Tool } from './tokens';
3+
4+
export class ToolsRegistry implements IToolRegistry {
5+
get toolNames(): string[] {
6+
return this._tools.map(tool => tool.name);
7+
}
8+
9+
get toolsChanged(): ISignal<IToolRegistry, void> {
10+
return this._toolsChanged;
11+
}
12+
13+
add(tool: Tool): void {
14+
const index = this._tools.findIndex(t => t.name === tool.name);
15+
if (index === -1) {
16+
this._tools.push(tool);
17+
this._toolsChanged.emit();
18+
}
19+
}
20+
21+
private _tools: Tool[] = [];
22+
private _toolsChanged = new Signal<IToolRegistry, void>(this);
23+
}

0 commit comments

Comments
 (0)