@jupyterlite/ai 0.9.0 → 0.10.0

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 (66) hide show
  1. package/README.md +5 -214
  2. package/lib/agent.d.ts +58 -66
  3. package/lib/agent.js +274 -300
  4. package/lib/approval-buttons.d.ts +19 -82
  5. package/lib/approval-buttons.js +36 -289
  6. package/lib/chat-model-registry.d.ts +6 -0
  7. package/lib/chat-model-registry.js +4 -1
  8. package/lib/chat-model.d.ts +19 -54
  9. package/lib/chat-model.js +243 -303
  10. package/lib/components/clear-button.d.ts +6 -1
  11. package/lib/components/clear-button.js +8 -3
  12. package/lib/components/completion-status.d.ts +5 -0
  13. package/lib/components/completion-status.js +5 -4
  14. package/lib/components/model-select.d.ts +6 -1
  15. package/lib/components/model-select.js +9 -8
  16. package/lib/components/stop-button.d.ts +6 -1
  17. package/lib/components/stop-button.js +8 -3
  18. package/lib/components/token-usage-display.d.ts +5 -0
  19. package/lib/components/token-usage-display.js +2 -2
  20. package/lib/components/tool-select.d.ts +6 -1
  21. package/lib/components/tool-select.js +6 -5
  22. package/lib/index.js +62 -38
  23. package/lib/models/settings-model.d.ts +1 -1
  24. package/lib/providers/built-in-providers.js +38 -19
  25. package/lib/providers/models.d.ts +3 -3
  26. package/lib/providers/provider-registry.d.ts +3 -4
  27. package/lib/providers/provider-registry.js +1 -4
  28. package/lib/tokens.d.ts +5 -6
  29. package/lib/tools/commands.d.ts +2 -1
  30. package/lib/tools/commands.js +37 -46
  31. package/lib/tools/file.js +49 -73
  32. package/lib/tools/notebook.js +370 -445
  33. package/lib/widgets/ai-settings.d.ts +6 -0
  34. package/lib/widgets/ai-settings.js +72 -71
  35. package/lib/widgets/main-area-chat.d.ts +2 -0
  36. package/lib/widgets/main-area-chat.js +5 -2
  37. package/lib/widgets/provider-config-dialog.d.ts +2 -0
  38. package/lib/widgets/provider-config-dialog.js +34 -34
  39. package/package.json +12 -12
  40. package/src/agent.ts +342 -361
  41. package/src/approval-buttons.ts +43 -389
  42. package/src/chat-model-registry.ts +9 -1
  43. package/src/chat-model.ts +355 -370
  44. package/src/completion/completion-provider.ts +2 -3
  45. package/src/components/clear-button.tsx +16 -3
  46. package/src/components/completion-status.tsx +18 -4
  47. package/src/components/model-select.tsx +21 -8
  48. package/src/components/stop-button.tsx +16 -3
  49. package/src/components/token-usage-display.tsx +14 -2
  50. package/src/components/tool-select.tsx +23 -5
  51. package/src/index.ts +80 -36
  52. package/src/models/settings-model.ts +1 -1
  53. package/src/providers/built-in-providers.ts +38 -19
  54. package/src/providers/models.ts +3 -3
  55. package/src/providers/provider-registry.ts +4 -8
  56. package/src/tokens.ts +5 -6
  57. package/src/tools/commands.ts +39 -50
  58. package/src/tools/file.ts +49 -75
  59. package/src/tools/notebook.ts +451 -510
  60. package/src/widgets/ai-settings.tsx +153 -84
  61. package/src/widgets/main-area-chat.ts +8 -2
  62. package/src/widgets/provider-config-dialog.tsx +54 -41
  63. package/style/base.css +13 -73
  64. package/lib/mcp/browser.d.ts +0 -68
  65. package/lib/mcp/browser.js +0 -138
  66. package/src/mcp/browser.ts +0 -220
@@ -1,220 +0,0 @@
1
- /* eslint-disable @typescript-eslint/naming-convention */
2
- /**
3
- * Browser-compatible MCP Server implementation
4
- *
5
- * This is a custom implementation that works around the limitation in
6
- * @openai/agents where MCPServerStreamableHttp doesn't work in browsers
7
- */
8
-
9
- // Type definitions matching openai/agents MCPServer interface
10
- interface MCPServer {
11
- cacheToolsList: boolean;
12
- toolFilter?: any;
13
- connect(): Promise<void>;
14
- readonly name: string;
15
- close(): Promise<void>;
16
- listTools(): Promise<MCPTool[]>;
17
- callTool(
18
- toolName: string,
19
- args: Record<string, unknown> | null
20
- ): Promise<CallToolResultContent>;
21
- invalidateToolsCache(): Promise<void>;
22
- }
23
-
24
- interface MCPTool {
25
- name: string;
26
- description?: string;
27
- inputSchema: {
28
- type: 'object';
29
- properties: Record<string, any>;
30
- required: string[];
31
- additionalProperties: boolean;
32
- };
33
- }
34
-
35
- // CallToolResultContent is an array of content items
36
- type CallToolResultContent = Array<{ type: string; text: string }>;
37
-
38
- interface MCPServerStreamableHttpOptions {
39
- url: string;
40
- cacheToolsList?: boolean;
41
- clientSessionTimeoutSeconds?: number;
42
- name?: string;
43
- logger?: any;
44
- toolFilter?: any;
45
- timeout?: number;
46
- authProvider?: any;
47
- requestInit?: any;
48
- fetch?: any;
49
- reconnectionOptions?: any;
50
- sessionId?: string;
51
- }
52
-
53
- /**
54
- * Browser-compatible MCP Server implementation that works around limitations
55
- * in @openai/agents where MCPServerStreamableHttp doesn't work in browsers.
56
- *
57
- * This class provides a streamable HTTP client transport for MCP (Model Context Protocol)
58
- * servers that can be used in browser environments.
59
- */
60
- export class BrowserMCPServerStreamableHttp implements MCPServer {
61
- readonly name: string;
62
- readonly cacheToolsList: boolean;
63
- readonly toolFilter: any = undefined;
64
-
65
- constructor(options: MCPServerStreamableHttpOptions) {
66
- this._options = options;
67
- this.name = options.name || `browser-mcp-server: ${options.url}`;
68
- this.cacheToolsList = options.cacheToolsList ?? false;
69
- }
70
-
71
- async connect(): Promise<void> {
72
- try {
73
- // Dynamic import to handle cases where MCP SDK isn't available
74
- const { StreamableHTTPClientTransport } = await import(
75
- '@modelcontextprotocol/sdk/client/streamableHttp.js'
76
- );
77
- const { Client } = await import(
78
- '@modelcontextprotocol/sdk/client/index.js'
79
- );
80
-
81
- // Merge CORS-enabled requestInit with user options
82
- const corsRequestInit = {
83
- mode: 'cors' as RequestMode,
84
- credentials: 'omit' as RequestCredentials,
85
- ...this._options.requestInit
86
- };
87
-
88
- this._transport = new StreamableHTTPClientTransport(
89
- new URL(this._options.url),
90
- {
91
- authProvider: this._options.authProvider,
92
- requestInit: corsRequestInit,
93
- fetch: this._options.fetch || fetch,
94
- reconnectionOptions: this._options.reconnectionOptions,
95
- sessionId: this._options.sessionId
96
- }
97
- );
98
-
99
- this._session = new Client({
100
- name: this.name,
101
- version: '1.0.0'
102
- });
103
-
104
- await this._session.connect(this._transport);
105
- } catch (error) {
106
- console.error('Error initializing MCP server:', error);
107
- await this.close();
108
- throw error;
109
- }
110
- }
111
-
112
- async close(): Promise<void> {
113
- if (this._session) {
114
- try {
115
- await this._session.close();
116
- } catch (error) {
117
- console.error('Error closing MCP server session:', error);
118
- }
119
- this._session = null;
120
- }
121
- if (this._transport) {
122
- try {
123
- await this._transport.close();
124
- } catch (error) {
125
- console.error('Error closing MCP server transport:', error);
126
- }
127
- this._transport = null;
128
- }
129
- }
130
-
131
- async listTools(): Promise<MCPTool[]> {
132
- if (!this._session) {
133
- throw new Error('Server not initialized. Call connect() first.');
134
- }
135
-
136
- if (
137
- this.cacheToolsList &&
138
- !this._cacheDirty &&
139
- this._toolsList.length > 0
140
- ) {
141
- return this._toolsList;
142
- }
143
-
144
- try {
145
- const { ListToolsResultSchema } = await import(
146
- '@modelcontextprotocol/sdk/types.js'
147
- );
148
-
149
- const response = await this._session.listTools();
150
-
151
- const parsedResponse = ListToolsResultSchema.parse(response);
152
-
153
- // Map to openai/agents MCPTool type
154
- this._toolsList = parsedResponse.tools.map((tool: any) => ({
155
- name: tool.name,
156
- description: tool.description,
157
- inputSchema: {
158
- type: 'object' as const,
159
- properties: tool.inputSchema?.properties || {},
160
- required: tool.inputSchema?.required || [],
161
- additionalProperties: tool.inputSchema?.additionalProperties ?? false
162
- }
163
- }));
164
-
165
- this._cacheDirty = false;
166
-
167
- return this._toolsList;
168
- } catch (error) {
169
- console.error(`Error listing tools from ${this.name}:`, error);
170
- throw error;
171
- }
172
- }
173
-
174
- async callTool(
175
- toolName: string,
176
- args: Record<string, unknown> | null
177
- ): Promise<CallToolResultContent> {
178
- if (!this._session) {
179
- throw new Error('Server not initialized. Call connect() first.');
180
- }
181
-
182
- try {
183
- const { CallToolResultSchema } = await import(
184
- '@modelcontextprotocol/sdk/types.js'
185
- );
186
-
187
- const response = await this._session.callTool(
188
- {
189
- name: toolName,
190
- arguments: args ?? {}
191
- },
192
- undefined,
193
- {
194
- timeout: this._options.timeout ?? 30000
195
- }
196
- );
197
-
198
- // Parse and validate using MCP SDK schema
199
- const parsed = CallToolResultSchema.parse(response);
200
- const result = parsed.content;
201
-
202
- // Return the content array as expected by openai/agents
203
- // CallToolResultContent is { type: string; text: string }[]
204
- return result as CallToolResultContent;
205
- } catch (error) {
206
- console.error(`Error calling tool ${toolName}:`, error);
207
- throw error;
208
- }
209
- }
210
-
211
- async invalidateToolsCache(): Promise<void> {
212
- this._cacheDirty = true;
213
- }
214
-
215
- private _session: any | null = null;
216
- private _toolsList: MCPTool[] = [];
217
- private _cacheDirty = true;
218
- private _transport: any = null;
219
- private _options: MCPServerStreamableHttpOptions;
220
- }