@jupyterlite/ai 0.8.1 → 0.9.0-a1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/lib/agent.d.ts +243 -0
  2. package/lib/agent.js +627 -0
  3. package/lib/chat-model.d.ts +195 -0
  4. package/lib/chat-model.js +591 -0
  5. package/lib/completion/completion-provider.d.ts +93 -0
  6. package/lib/completion/completion-provider.js +235 -0
  7. package/lib/completion/index.d.ts +1 -0
  8. package/lib/completion/index.js +1 -0
  9. package/lib/components/clear-button.d.ts +18 -0
  10. package/lib/components/clear-button.js +31 -0
  11. package/lib/components/index.d.ts +3 -0
  12. package/lib/components/index.js +3 -0
  13. package/lib/components/model-select.d.ts +19 -0
  14. package/lib/components/model-select.js +154 -0
  15. package/lib/components/stop-button.d.ts +3 -3
  16. package/lib/components/stop-button.js +8 -9
  17. package/lib/components/token-usage-display.d.ts +45 -0
  18. package/lib/components/token-usage-display.js +74 -0
  19. package/lib/components/tool-select.d.ts +27 -0
  20. package/lib/components/tool-select.js +130 -0
  21. package/lib/icons.d.ts +3 -1
  22. package/lib/icons.js +10 -13
  23. package/lib/index.d.ts +5 -5
  24. package/lib/index.js +341 -169
  25. package/lib/mcp/browser.d.ts +68 -0
  26. package/lib/mcp/browser.js +132 -0
  27. package/lib/models/settings-model.d.ts +70 -0
  28. package/lib/models/settings-model.js +296 -0
  29. package/lib/providers/built-in-providers.d.ts +9 -0
  30. package/lib/providers/built-in-providers.js +266 -0
  31. package/lib/providers/models.d.ts +37 -0
  32. package/lib/providers/models.js +28 -0
  33. package/lib/providers/provider-registry.d.ts +94 -0
  34. package/lib/providers/provider-registry.js +155 -0
  35. package/lib/tokens.d.ts +167 -86
  36. package/lib/tokens.js +25 -12
  37. package/lib/tools/commands.d.ts +11 -0
  38. package/lib/tools/commands.js +126 -0
  39. package/lib/tools/file.d.ts +27 -0
  40. package/lib/tools/file.js +262 -0
  41. package/lib/tools/notebook.d.ts +41 -0
  42. package/lib/tools/notebook.js +779 -0
  43. package/lib/tools/tool-registry.d.ts +35 -0
  44. package/lib/tools/tool-registry.js +55 -0
  45. package/lib/widgets/ai-settings.d.ts +49 -0
  46. package/lib/widgets/ai-settings.js +580 -0
  47. package/lib/widgets/chat-wrapper.d.ts +144 -0
  48. package/lib/widgets/chat-wrapper.js +390 -0
  49. package/lib/widgets/provider-config-dialog.d.ts +14 -0
  50. package/lib/widgets/provider-config-dialog.js +112 -0
  51. package/package.json +151 -40
  52. package/schema/settings-model.json +159 -0
  53. package/src/agent.ts +836 -0
  54. package/src/chat-model.ts +771 -0
  55. package/src/completion/completion-provider.ts +346 -0
  56. package/src/completion/index.ts +1 -0
  57. package/src/components/clear-button.tsx +56 -0
  58. package/src/components/index.ts +3 -0
  59. package/src/components/model-select.tsx +245 -0
  60. package/src/components/stop-button.tsx +11 -11
  61. package/src/components/token-usage-display.tsx +130 -0
  62. package/src/components/tool-select.tsx +218 -0
  63. package/src/icons.ts +12 -14
  64. package/src/index.ts +485 -232
  65. package/src/mcp/browser.ts +213 -0
  66. package/src/models/settings-model.ts +413 -0
  67. package/src/providers/built-in-providers.ts +294 -0
  68. package/src/providers/models.ts +79 -0
  69. package/src/providers/provider-registry.ts +189 -0
  70. package/src/tokens.ts +217 -90
  71. package/src/tools/commands.ts +151 -0
  72. package/src/tools/file.ts +307 -0
  73. package/src/tools/notebook.ts +987 -0
  74. package/src/tools/tool-registry.ts +63 -0
  75. package/src/types.d.ts +4 -0
  76. package/src/widgets/ai-settings.tsx +1233 -0
  77. package/src/widgets/chat-wrapper.tsx +543 -0
  78. package/src/widgets/provider-config-dialog.tsx +272 -0
  79. package/style/base.css +335 -14
  80. package/style/icons/jupyternaut-lite.svg +1 -1
  81. package/lib/base-completer.d.ts +0 -49
  82. package/lib/base-completer.js +0 -14
  83. package/lib/chat-handler.d.ts +0 -56
  84. package/lib/chat-handler.js +0 -201
  85. package/lib/completion-provider.d.ts +0 -34
  86. package/lib/completion-provider.js +0 -32
  87. package/lib/default-prompts.d.ts +0 -2
  88. package/lib/default-prompts.js +0 -31
  89. package/lib/default-providers/Anthropic/completer.d.ts +0 -12
  90. package/lib/default-providers/Anthropic/completer.js +0 -46
  91. package/lib/default-providers/Anthropic/settings-schema.json +0 -70
  92. package/lib/default-providers/ChromeAI/completer.d.ts +0 -12
  93. package/lib/default-providers/ChromeAI/completer.js +0 -56
  94. package/lib/default-providers/ChromeAI/instructions.d.ts +0 -6
  95. package/lib/default-providers/ChromeAI/instructions.js +0 -42
  96. package/lib/default-providers/ChromeAI/settings-schema.json +0 -18
  97. package/lib/default-providers/Gemini/completer.d.ts +0 -12
  98. package/lib/default-providers/Gemini/completer.js +0 -48
  99. package/lib/default-providers/Gemini/instructions.d.ts +0 -2
  100. package/lib/default-providers/Gemini/instructions.js +0 -9
  101. package/lib/default-providers/Gemini/settings-schema.json +0 -64
  102. package/lib/default-providers/MistralAI/completer.d.ts +0 -13
  103. package/lib/default-providers/MistralAI/completer.js +0 -52
  104. package/lib/default-providers/MistralAI/instructions.d.ts +0 -2
  105. package/lib/default-providers/MistralAI/instructions.js +0 -18
  106. package/lib/default-providers/MistralAI/settings-schema.json +0 -75
  107. package/lib/default-providers/Ollama/completer.d.ts +0 -12
  108. package/lib/default-providers/Ollama/completer.js +0 -43
  109. package/lib/default-providers/Ollama/instructions.d.ts +0 -2
  110. package/lib/default-providers/Ollama/instructions.js +0 -70
  111. package/lib/default-providers/Ollama/settings-schema.json +0 -143
  112. package/lib/default-providers/OpenAI/completer.d.ts +0 -12
  113. package/lib/default-providers/OpenAI/completer.js +0 -43
  114. package/lib/default-providers/OpenAI/settings-schema.json +0 -628
  115. package/lib/default-providers/WebLLM/completer.d.ts +0 -21
  116. package/lib/default-providers/WebLLM/completer.js +0 -127
  117. package/lib/default-providers/WebLLM/instructions.d.ts +0 -6
  118. package/lib/default-providers/WebLLM/instructions.js +0 -32
  119. package/lib/default-providers/WebLLM/settings-schema.json +0 -19
  120. package/lib/default-providers/index.d.ts +0 -2
  121. package/lib/default-providers/index.js +0 -179
  122. package/lib/provider.d.ts +0 -144
  123. package/lib/provider.js +0 -412
  124. package/lib/settings/base.json +0 -7
  125. package/lib/settings/index.d.ts +0 -3
  126. package/lib/settings/index.js +0 -3
  127. package/lib/settings/panel.d.ts +0 -226
  128. package/lib/settings/panel.js +0 -510
  129. package/lib/settings/textarea.d.ts +0 -2
  130. package/lib/settings/textarea.js +0 -18
  131. package/lib/settings/utils.d.ts +0 -2
  132. package/lib/settings/utils.js +0 -4
  133. package/lib/types/ai-model.d.ts +0 -24
  134. package/lib/types/ai-model.js +0 -5
  135. package/schema/chat.json +0 -28
  136. package/schema/provider-registry.json +0 -29
  137. package/schema/system-prompts.json +0 -22
  138. package/src/base-completer.ts +0 -75
  139. package/src/chat-handler.ts +0 -262
  140. package/src/completion-provider.ts +0 -64
  141. package/src/default-prompts.ts +0 -33
  142. package/src/default-providers/Anthropic/completer.ts +0 -59
  143. package/src/default-providers/ChromeAI/completer.ts +0 -73
  144. package/src/default-providers/ChromeAI/instructions.ts +0 -45
  145. package/src/default-providers/Gemini/completer.ts +0 -61
  146. package/src/default-providers/Gemini/instructions.ts +0 -9
  147. package/src/default-providers/MistralAI/completer.ts +0 -69
  148. package/src/default-providers/MistralAI/instructions.ts +0 -18
  149. package/src/default-providers/Ollama/completer.ts +0 -54
  150. package/src/default-providers/Ollama/instructions.ts +0 -70
  151. package/src/default-providers/OpenAI/completer.ts +0 -54
  152. package/src/default-providers/WebLLM/completer.ts +0 -151
  153. package/src/default-providers/WebLLM/instructions.ts +0 -33
  154. package/src/default-providers/index.ts +0 -211
  155. package/src/global.d.ts +0 -9
  156. package/src/provider.ts +0 -514
  157. package/src/settings/index.ts +0 -3
  158. package/src/settings/panel.tsx +0 -773
  159. package/src/settings/textarea.tsx +0 -33
  160. package/src/settings/utils.ts +0 -5
  161. package/src/types/ai-model.ts +0 -37
  162. package/src/types/service-worker.d.ts +0 -6
package/lib/tokens.d.ts CHANGED
@@ -1,145 +1,226 @@
1
- import { BaseChatModel } from '@langchain/core/language_models/chat_models';
2
- import { ReadonlyPartialJSONObject, Token } from '@lumino/coreutils';
1
+ import { Token } from '@lumino/coreutils';
3
2
  import { ISignal } from '@lumino/signaling';
4
- import { JSONSchema7 } from 'json-schema';
5
- import { IBaseCompleter } from './base-completer';
6
- import { AIChatModel, AICompleter } from './types/ai-model';
7
- export declare const PLUGIN_IDS: {
8
- chat: string;
9
- chatCommandRegistry: string;
10
- completer: string;
11
- providerRegistry: string;
12
- settingsConnector: string;
13
- systemPrompts: string;
14
- };
15
- export type ModelRole = 'chat' | 'completer';
16
- export interface IDict<T = any> {
17
- [key: string]: T;
18
- }
19
- export interface IType<T> {
20
- new (...args: any[]): T;
3
+ import { FunctionTool } from '@openai/agents';
4
+ import { LanguageModel } from 'ai';
5
+ import { AgentManager } from './agent';
6
+ import type { AISettingsModel } from './models/settings-model';
7
+ import type { IModelOptions } from './providers/models';
8
+ /**
9
+ * Type definition for a tool
10
+ */
11
+ export type ITool = FunctionTool<any, any, any>;
12
+ /**
13
+ * Interface for token usage statistics from AI model interactions
14
+ */
15
+ export interface ITokenUsage {
16
+ /**
17
+ * Number of input tokens consumed (prompt tokens)
18
+ */
19
+ inputTokens: number;
20
+ /**
21
+ * Number of output tokens generated (completion tokens)
22
+ */
23
+ outputTokens: number;
21
24
  }
22
25
  /**
23
- * The provider interface.
26
+ * Interface for a named tool (tool with a name identifier)
24
27
  */
25
- export interface IAIProvider {
28
+ export interface INamedTool {
26
29
  /**
27
- * The name of the provider.
30
+ * The unique name of the tool
28
31
  */
29
32
  name: string;
30
33
  /**
31
- * The chat model class to use.
34
+ * The tool instance
32
35
  */
33
- chat?: IType<BaseChatModel>;
36
+ tool: ITool;
37
+ }
38
+ /**
39
+ * The tool registry interface for managing AI tools
40
+ */
41
+ export interface IToolRegistry {
34
42
  /**
35
- * The completer class to use.
43
+ * The registered tools as a record (name -> tool mapping).
36
44
  */
37
- completer?: IType<IBaseCompleter>;
45
+ readonly tools: Record<string, ITool>;
38
46
  /**
39
- * the settings schema for the provider.
47
+ * The registered named tools array.
40
48
  */
41
- settingsSchema?: any;
49
+ readonly namedTools: INamedTool[];
42
50
  /**
43
- * The instructions to be displayed in the settings, as helper to use the provider.
44
- * A markdown renderer is used to render the instructions.
51
+ * A signal triggered when the tools have changed.
45
52
  */
46
- instructions?: string;
53
+ readonly toolsChanged: ISignal<IToolRegistry, void>;
47
54
  /**
48
- * A function that extract the error message from the provider API error.
49
- * Default to `(error) => error.message`.
55
+ * Add a new tool to the registry.
50
56
  */
51
- errorMessage?: (error: any) => string;
57
+ add(name: string, tool: ITool): void;
52
58
  /**
53
- * Compatibility check function, to determine if the provider is compatible with the
54
- * current environment.
59
+ * Get a tool for a given name.
60
+ * Return null if the name is not provided or if there is no registered tool with the
61
+ * given name.
55
62
  */
56
- compatibilityCheck?: () => Promise<string | null>;
63
+ get(name: string | null): ITool | null;
57
64
  /**
58
- * Whether to expose or not the chat model.
59
- *
60
- * ### CAUTION
61
- * This flag will expose the whole chat model API, which may contain private keys.
62
- * Be sure to use it with a model that does not expose sensitive information in the
63
- * API.
65
+ * Remove a tool from the registry by name.
64
66
  */
65
- exposeChatModel?: boolean;
67
+ remove(name: string): boolean;
66
68
  }
67
69
  /**
68
- * The provider registry interface.
70
+ * The tool registry token.
69
71
  */
70
- export interface IAIProviderRegistry {
72
+ export declare const IToolRegistry: Token<IToolRegistry>;
73
+ /**
74
+ * Token for the chat provider registry.
75
+ */
76
+ export declare const IChatProviderRegistry: Token<IChatProviderRegistry>;
77
+ /**
78
+ * Token for the completion provider registry.
79
+ */
80
+ export declare const ICompletionProviderRegistry: Token<ICompletionProviderRegistry>;
81
+ /**
82
+ * Interface for a provider factory function that creates chat models
83
+ */
84
+ export interface IChatProviderFactory {
85
+ (options: IModelOptions): any;
86
+ }
87
+ /**
88
+ * Interface for a provider factory function that creates completion models
89
+ */
90
+ export interface ICompletionProviderFactory {
91
+ (options: IModelOptions): LanguageModel;
92
+ }
93
+ /**
94
+ * Base information about a registered provider
95
+ */
96
+ export interface IBaseProviderInfo {
71
97
  /**
72
- * Get the list of provider names.
98
+ * Unique identifier for the provider
73
99
  */
74
- readonly providers: string[];
100
+ id: string;
75
101
  /**
76
- * Add a new provider.
102
+ * Display name for the provider
77
103
  */
78
- add(provider: IAIProvider): void;
104
+ name: string;
79
105
  /**
80
- * Get the current provider name.
106
+ * Whether this provider requires an API key
81
107
  */
82
- currentName(role: ModelRole): string;
108
+ requiresApiKey: boolean;
83
109
  /**
84
- * Get the current completer of the completion provider.
110
+ * Default model names for this provider
85
111
  */
86
- currentCompleter: AICompleter | null;
112
+ defaultModels: string[];
87
113
  /**
88
- * Getter/setter for the completer system prompt.
114
+ * Whether this provider supports custom base URLs
89
115
  */
90
- completerSystemPrompt: string;
116
+ supportsBaseURL?: boolean;
91
117
  /**
92
- * Get the current llm chat model.
118
+ * Whether this provider supports custom headers
93
119
  */
94
- currentChatModel: AIChatModel | null;
120
+ supportsHeaders?: boolean;
95
121
  /**
96
- * Getter/setter for the chat system prompt.
122
+ * Whether this provider supports tool calling
97
123
  */
98
- chatSystemPrompt: string;
124
+ supportsToolCalling?: boolean;
99
125
  /**
100
- * Get the settings schema of a given provider.
126
+ * Additional provider-specific configuration schema
101
127
  */
102
- getSettingsSchema(provider: string): JSONSchema7;
128
+ customSettings?: Record<string, any>;
129
+ }
130
+ /**
131
+ * Information about a chat provider
132
+ */
133
+ export interface IChatProviderInfo extends IBaseProviderInfo {
103
134
  /**
104
- * Get the instructions of a given provider.
135
+ * Factory function for creating chat models
105
136
  */
106
- getInstructions(provider: string): string | undefined;
137
+ factory: IChatProviderFactory;
138
+ }
139
+ /**
140
+ * Information about a completion provider
141
+ */
142
+ export interface ICompletionProviderInfo extends IBaseProviderInfo {
143
+ /**
144
+ * Factory function for creating completion models
145
+ */
146
+ factory: ICompletionProviderFactory;
147
+ }
148
+ /**
149
+ * Registry for chat AI providers
150
+ */
151
+ export interface IChatProviderRegistry {
152
+ /**
153
+ * The registered providers as a record (id -> info mapping).
154
+ */
155
+ readonly providers: Record<string, IChatProviderInfo>;
156
+ /**
157
+ * A signal triggered when providers have changed.
158
+ */
159
+ readonly providersChanged: ISignal<IChatProviderRegistry, void>;
160
+ /**
161
+ * Register a new chat provider.
162
+ */
163
+ registerProvider(info: IChatProviderInfo): void;
164
+ /**
165
+ * Unregister a chat provider.
166
+ */
167
+ unregisterProvider(id: string): boolean;
168
+ /**
169
+ * Get provider info by id.
170
+ */
171
+ getProviderInfo(id: string): IChatProviderInfo | null;
172
+ /**
173
+ * Create a chat model instance for the given provider.
174
+ */
175
+ createChatModel(id: string, options: IModelOptions): any | null;
176
+ /**
177
+ * Get all available provider IDs.
178
+ */
179
+ getAvailableProviders(): string[];
180
+ }
181
+ /**
182
+ * Registry for completion providers
183
+ */
184
+ export interface ICompletionProviderRegistry {
107
185
  /**
108
- * Get the compatibility check function of a given provider.
186
+ * The registered providers as a record (id -> info mapping).
109
187
  */
110
- getCompatibilityCheck(provider: string): (() => Promise<string | null>) | undefined;
188
+ readonly providers: Record<string, ICompletionProviderInfo>;
111
189
  /**
112
- * Format an error message from the current provider.
190
+ * A signal triggered when providers have changed.
113
191
  */
114
- formatErrorMessage(error: any): string;
192
+ readonly providersChanged: ISignal<ICompletionProviderRegistry, void>;
115
193
  /**
116
- * Set the completer provider.
117
- * Creates the provider if the name has changed, otherwise only updates its config.
118
- *
119
- * @param options - An object with the name and the settings of the provider to use.
194
+ * Register a new completion provider.
120
195
  */
121
- setCompleterProvider(settings: ReadonlyPartialJSONObject): void;
196
+ registerProvider(info: ICompletionProviderInfo): void;
122
197
  /**
123
- * Set the chat provider.
124
- * Creates the provider if the name has changed, otherwise only updates its config.
125
- *
126
- * @param options - An object with the name and the settings of the provider to use.
198
+ * Unregister a completion provider.
127
199
  */
128
- setChatProvider(settings: ReadonlyPartialJSONObject): void;
200
+ unregisterProvider(id: string): boolean;
129
201
  /**
130
- * A signal emitting when the provider or its settings has changed.
202
+ * Get provider info by id.
131
203
  */
132
- readonly providerChanged: ISignal<IAIProviderRegistry, ModelRole>;
204
+ getProviderInfo(id: string): ICompletionProviderInfo | null;
133
205
  /**
134
- * Get the current chat error;
206
+ * Create a completion model instance for the given provider.
135
207
  */
136
- readonly chatError: string;
208
+ createCompletionModel(id: string, options: IModelOptions): LanguageModel | null;
137
209
  /**
138
- * get the current completer error.
210
+ * Get all available provider IDs.
139
211
  */
140
- readonly completerError: string;
212
+ getAvailableProviders(): string[];
141
213
  }
142
214
  /**
143
- * The provider registry token.
215
+ * Token for the AI settings model.
216
+ */
217
+ export declare const IAISettingsModel: Token<AISettingsModel>;
218
+ /**
219
+ * Token for the agent manager.
220
+ */
221
+ export declare const IAgentManager: Token<AgentManager>;
222
+ /**
223
+ * The string that replaces a secret key in settings.
144
224
  */
145
- export declare const IAIProviderRegistry: Token<IAIProviderRegistry>;
225
+ export declare const SECRETS_NAMESPACE = "@jupyterlite/ai:providers";
226
+ export declare const SECRETS_REPLACEMENT = "***";
package/lib/tokens.js CHANGED
@@ -1,13 +1,26 @@
1
1
  import { Token } from '@lumino/coreutils';
2
- export const PLUGIN_IDS = {
3
- chat: '@jupyterlite/ai:chat',
4
- chatCommandRegistry: '@jupyterlite/ai:autocompletion-registry',
5
- completer: '@jupyterlite/ai:completer',
6
- providerRegistry: '@jupyterlite/ai:provider-registry',
7
- settingsConnector: '@jupyterlite/ai:settings-connector',
8
- systemPrompts: '@jupyterlite/ai:system-prompts'
9
- };
10
- /**
11
- * The provider registry token.
12
- */
13
- export const IAIProviderRegistry = new Token('@jupyterlite/ai:provider-registry', 'Provider for chat and completion LLM provider');
2
+ /**
3
+ * The tool registry token.
4
+ */
5
+ export const IToolRegistry = new Token('@jupyterlite/ai:tool-registry', 'Tool registry for AI agent functionality');
6
+ /**
7
+ * Token for the chat provider registry.
8
+ */
9
+ export const IChatProviderRegistry = new Token('@jupyterlite/ai:chat-provider-registry', 'Registry for chat AI providers');
10
+ /**
11
+ * Token for the completion provider registry.
12
+ */
13
+ export const ICompletionProviderRegistry = new Token('@jupyterlite/ai:completion-provider-registry', 'Registry for completion providers');
14
+ /**
15
+ * Token for the AI settings model.
16
+ */
17
+ export const IAISettingsModel = new Token('@jupyterlite/ai:IAISettingsModel');
18
+ /**
19
+ * Token for the agent manager.
20
+ */
21
+ export const IAgentManager = new Token('@jupyterlite/ai:agent-manager');
22
+ /**
23
+ * The string that replaces a secret key in settings.
24
+ */
25
+ export const SECRETS_NAMESPACE = '@jupyterlite/ai:providers';
26
+ export const SECRETS_REPLACEMENT = '***';
@@ -0,0 +1,11 @@
1
+ import { CommandRegistry } from '@lumino/commands';
2
+ import { ITool } from '../tokens';
3
+ import { AISettingsModel } from '../models/settings-model';
4
+ /**
5
+ * Create a tool to discover all available commands and their metadata
6
+ */
7
+ export declare function createDiscoverCommandsTool(commands: CommandRegistry): ITool;
8
+ /**
9
+ * Create a tool to execute a specific JupyterLab command
10
+ */
11
+ export declare function createExecuteCommandTool(commands: CommandRegistry, settingsModel: AISettingsModel): ITool;
@@ -0,0 +1,126 @@
1
+ import { tool } from '@openai/agents';
2
+ import { z } from 'zod';
3
+ /**
4
+ * Create a tool to discover all available commands and their metadata
5
+ */
6
+ export function createDiscoverCommandsTool(commands) {
7
+ return tool({
8
+ name: 'discover_commands',
9
+ description: 'Discover all available JupyterLab commands with their metadata, arguments, and descriptions',
10
+ parameters: z.object({
11
+ // currently unused, but could be used to filter commands by a search term
12
+ query: z
13
+ .string()
14
+ .optional()
15
+ .nullable()
16
+ .describe('Optional search query to filter commands')
17
+ }),
18
+ execute: async () => {
19
+ try {
20
+ const commandList = [];
21
+ // Get all command IDs
22
+ const commandIds = commands.listCommands();
23
+ for (const id of commandIds) {
24
+ try {
25
+ // Get command metadata using various CommandRegistry methods
26
+ const description = await commands.describedBy(id);
27
+ const label = commands.label(id);
28
+ const caption = commands.caption(id);
29
+ const usage = commands.usage(id);
30
+ commandList.push({
31
+ id,
32
+ label: label || undefined,
33
+ caption: caption || undefined,
34
+ description: usage || undefined,
35
+ args: description?.args || undefined
36
+ });
37
+ }
38
+ catch (error) {
39
+ // Some commands might not have descriptions, skip them
40
+ commandList.push({ id });
41
+ }
42
+ }
43
+ return {
44
+ success: true,
45
+ commandCount: commandList.length,
46
+ commands: commandList
47
+ };
48
+ }
49
+ catch (error) {
50
+ return {
51
+ success: false,
52
+ error: `Failed to discover commands: ${error instanceof Error ? error.message : String(error)}`
53
+ };
54
+ }
55
+ }
56
+ });
57
+ }
58
+ /**
59
+ * Create a tool to execute a specific JupyterLab command
60
+ */
61
+ export function createExecuteCommandTool(commands, settingsModel) {
62
+ return tool({
63
+ name: 'execute_command',
64
+ description: 'Execute a specific JupyterLab command with optional arguments',
65
+ parameters: z.object({
66
+ commandId: z.string().describe('The ID of the command to execute'),
67
+ args: z
68
+ .any()
69
+ .optional()
70
+ .describe('Optional arguments to pass to the command')
71
+ }),
72
+ needsApproval: async (_context, { commandId }) => {
73
+ // Use configurable list of commands requiring approval
74
+ const commandsRequiringApproval = settingsModel.config.commandsRequiringApproval;
75
+ return commandsRequiringApproval.some(cmd => commandId.includes(cmd) || cmd.includes(commandId));
76
+ },
77
+ execute: async (input) => {
78
+ const { commandId, args } = input;
79
+ // Check if command exists
80
+ if (!commands.hasCommand(commandId)) {
81
+ return {
82
+ success: false,
83
+ error: `Command '${commandId}' does not exist. Use 'discover_commands' to see available commands.`
84
+ };
85
+ }
86
+ try {
87
+ // Execute the command
88
+ const result = await commands.execute(commandId, args);
89
+ // Handle Widget objects specially (including subclasses like DocumentWidget)
90
+ let serializedResult;
91
+ if (result &&
92
+ typeof result === 'object' &&
93
+ (result.constructor?.name?.includes('Widget') || result.id)) {
94
+ serializedResult = {
95
+ type: result.constructor?.name || 'Widget',
96
+ id: result.id,
97
+ title: result.title?.label || result.title,
98
+ className: result.className
99
+ };
100
+ }
101
+ else {
102
+ // For other objects, try JSON serialization with fallback
103
+ try {
104
+ serializedResult = JSON.parse(JSON.stringify(result));
105
+ }
106
+ catch {
107
+ serializedResult = result
108
+ ? '[Complex object - cannot serialize]'
109
+ : 'Command executed successfully';
110
+ }
111
+ }
112
+ return {
113
+ success: true,
114
+ commandId,
115
+ result: serializedResult
116
+ };
117
+ }
118
+ catch (error) {
119
+ return {
120
+ success: false,
121
+ error: `Failed to execute command '${commandId}': ${error instanceof Error ? error.message : String(error)}`
122
+ };
123
+ }
124
+ }
125
+ });
126
+ }
@@ -0,0 +1,27 @@
1
+ import { CommandRegistry } from '@lumino/commands';
2
+ import { IDocumentManager } from '@jupyterlab/docmanager';
3
+ import { ITool } from '../tokens';
4
+ /**
5
+ * Create a tool for creating new files of various types
6
+ */
7
+ export declare function createNewFileTool(docManager: IDocumentManager): ITool;
8
+ /**
9
+ * Create a tool for opening files
10
+ */
11
+ export declare function createOpenFileTool(docManager: IDocumentManager): ITool;
12
+ /**
13
+ * Create a tool for deleting files
14
+ */
15
+ export declare function createDeleteFileTool(docManager: IDocumentManager): ITool;
16
+ /**
17
+ * Create a tool for renaming files
18
+ */
19
+ export declare function createRenameFileTool(docManager: IDocumentManager): ITool;
20
+ /**
21
+ * Create a tool for copying files
22
+ */
23
+ export declare function createCopyFileTool(docManager: IDocumentManager): ITool;
24
+ /**
25
+ * Create a tool for navigating to directories in the file browser
26
+ */
27
+ export declare function createNavigateToDirectoryTool(commands: CommandRegistry): ITool;