ai 5.0.0-canary.0 → 5.0.0-canary.10

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 (52) hide show
  1. package/CHANGELOG.md +152 -0
  2. package/dist/index.d.mts +1478 -793
  3. package/dist/index.d.ts +1478 -793
  4. package/dist/index.js +2609 -780
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +2484 -692
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/internal/index.d.mts +730 -0
  9. package/dist/internal/index.d.ts +730 -0
  10. package/dist/internal/index.js +1482 -0
  11. package/dist/internal/index.js.map +1 -0
  12. package/{rsc/dist/rsc-server.mjs → dist/internal/index.mjs} +855 -1555
  13. package/dist/internal/index.mjs.map +1 -0
  14. package/{mcp-stdio/dist → dist/mcp-stdio}/index.js +1 -1
  15. package/dist/mcp-stdio/index.js.map +1 -0
  16. package/{mcp-stdio/dist → dist/mcp-stdio}/index.mjs +1 -1
  17. package/dist/mcp-stdio/index.mjs.map +1 -0
  18. package/{test/dist → dist/test}/index.d.mts +32 -30
  19. package/{test/dist → dist/test}/index.d.ts +32 -30
  20. package/{test/dist → dist/test}/index.js +32 -12
  21. package/dist/test/index.js.map +1 -0
  22. package/{test/dist → dist/test}/index.mjs +30 -10
  23. package/dist/test/index.mjs.map +1 -0
  24. package/package.json +28 -56
  25. package/mcp-stdio/create-child-process.test.ts +0 -92
  26. package/mcp-stdio/create-child-process.ts +0 -21
  27. package/mcp-stdio/dist/index.js.map +0 -1
  28. package/mcp-stdio/dist/index.mjs.map +0 -1
  29. package/mcp-stdio/get-environment.ts +0 -43
  30. package/mcp-stdio/index.ts +0 -4
  31. package/mcp-stdio/mcp-stdio-transport.test.ts +0 -262
  32. package/mcp-stdio/mcp-stdio-transport.ts +0 -157
  33. package/react/dist/index.d.mts +0 -18
  34. package/react/dist/index.d.ts +0 -18
  35. package/react/dist/index.js +0 -39
  36. package/react/dist/index.js.map +0 -1
  37. package/react/dist/index.mjs +0 -17
  38. package/react/dist/index.mjs.map +0 -1
  39. package/rsc/dist/index.d.ts +0 -813
  40. package/rsc/dist/index.mjs +0 -18
  41. package/rsc/dist/rsc-client.d.mts +0 -1
  42. package/rsc/dist/rsc-client.mjs +0 -18
  43. package/rsc/dist/rsc-client.mjs.map +0 -1
  44. package/rsc/dist/rsc-server.d.mts +0 -748
  45. package/rsc/dist/rsc-server.mjs.map +0 -1
  46. package/rsc/dist/rsc-shared.d.mts +0 -101
  47. package/rsc/dist/rsc-shared.mjs +0 -308
  48. package/rsc/dist/rsc-shared.mjs.map +0 -1
  49. package/test/dist/index.js.map +0 -1
  50. package/test/dist/index.mjs.map +0 -1
  51. package/{mcp-stdio/dist → dist/mcp-stdio}/index.d.mts +6 -6
  52. package/{mcp-stdio/dist → dist/mcp-stdio}/index.d.ts +6 -6
@@ -1,262 +0,0 @@
1
- import type { ChildProcess } from 'node:child_process';
2
- import { EventEmitter } from 'node:events';
3
- import { beforeEach, describe, expect, it, vi } from 'vitest';
4
- import { JSONRPCMessage } from '../core/tool/mcp/json-rpc-message';
5
- import { MCPClientError } from '../errors';
6
- import { createChildProcess } from './create-child-process';
7
- import { StdioMCPTransport } from './mcp-stdio-transport';
8
-
9
- vi.mock('./create-child-process', { spy: true });
10
-
11
- interface MockChildProcess {
12
- stdin: EventEmitter & { write?: ReturnType<typeof vi.fn> };
13
- stdout: EventEmitter;
14
- stderr: EventEmitter;
15
- on: ReturnType<typeof vi.fn>;
16
- removeAllListeners: ReturnType<typeof vi.fn>;
17
- }
18
-
19
- describe('StdioMCPTransport', () => {
20
- let transport: StdioMCPTransport;
21
- let mockChildProcess: MockChildProcess;
22
- let mockStdin: EventEmitter & { write?: ReturnType<typeof vi.fn> };
23
- let mockStdout: EventEmitter;
24
-
25
- beforeEach(() => {
26
- vi.clearAllMocks();
27
-
28
- mockStdin = new EventEmitter();
29
- mockStdout = new EventEmitter();
30
- mockChildProcess = {
31
- stdin: mockStdin,
32
- stdout: mockStdout,
33
- stderr: new EventEmitter(),
34
- on: vi.fn(),
35
- removeAllListeners: vi.fn(),
36
- };
37
-
38
- vi.mocked(createChildProcess).mockResolvedValue(
39
- mockChildProcess as unknown as ChildProcess,
40
- );
41
-
42
- transport = new StdioMCPTransport({
43
- command: 'test-command',
44
- args: ['--test'],
45
- });
46
- });
47
-
48
- afterEach(() => {
49
- transport.close();
50
- });
51
-
52
- describe('start', () => {
53
- it('should successfully start the transport', async () => {
54
- const stdinOnSpy = vi.spyOn(mockStdin, 'on');
55
- const stdoutOnSpy = vi.spyOn(mockStdout, 'on');
56
-
57
- mockChildProcess.on.mockImplementation(
58
- (event: string, callback: () => void) => {
59
- if (event === 'spawn') {
60
- callback();
61
- }
62
- },
63
- );
64
-
65
- const startPromise = transport.start();
66
- await expect(startPromise).resolves.toBeUndefined();
67
-
68
- expect(mockChildProcess.on).toHaveBeenCalledWith(
69
- 'error',
70
- expect.any(Function),
71
- );
72
- expect(mockChildProcess.on).toHaveBeenCalledWith(
73
- 'spawn',
74
- expect.any(Function),
75
- );
76
- expect(mockChildProcess.on).toHaveBeenCalledWith(
77
- 'close',
78
- expect.any(Function),
79
- );
80
-
81
- expect(stdinOnSpy).toHaveBeenCalledWith('error', expect.any(Function));
82
- expect(stdoutOnSpy).toHaveBeenCalledWith('error', expect.any(Function));
83
- expect(stdoutOnSpy).toHaveBeenCalledWith('data', expect.any(Function));
84
- });
85
-
86
- it('should throw error if already started', async () => {
87
- mockChildProcess.on.mockImplementation(
88
- (event: string, callback: () => void) => {
89
- if (event === 'spawn') {
90
- callback();
91
- }
92
- },
93
- );
94
- const firstStart = transport.start();
95
- await expect(firstStart).resolves.toBeUndefined();
96
- const secondStart = transport.start();
97
- await expect(secondStart).rejects.toThrow(MCPClientError);
98
- });
99
-
100
- it('should handle spawn errors', async () => {
101
- const error = new Error('Spawn failed');
102
- const onErrorSpy = vi.fn();
103
- transport.onerror = onErrorSpy;
104
-
105
- // simulate `spawn` failure by emitting error event after returning child process
106
- mockChildProcess.on.mockImplementation(
107
- (event: string, callback: (err: Error) => void) => {
108
- if (event === 'error') {
109
- callback(error);
110
- }
111
- },
112
- );
113
-
114
- const startPromise = transport.start();
115
- await expect(startPromise).rejects.toThrow('Spawn failed');
116
- expect(onErrorSpy).toHaveBeenCalledWith(error);
117
- });
118
-
119
- it('should handle child_process import errors', async () => {
120
- vi.mocked(createChildProcess).mockRejectedValue(
121
- new MCPClientError({
122
- message: 'Failed to load child_process module dynamically',
123
- }),
124
- );
125
-
126
- const startPromise = transport.start();
127
- await expect(startPromise).rejects.toThrow(
128
- 'Failed to load child_process module dynamically',
129
- );
130
- });
131
- });
132
-
133
- describe('send', () => {
134
- beforeEach(async () => {
135
- mockChildProcess.on.mockImplementation(
136
- (event: string, callback: () => void) => {
137
- if (event === 'spawn') {
138
- callback();
139
- }
140
- },
141
- );
142
- await transport.start();
143
- });
144
-
145
- it('should successfully send a message', async () => {
146
- const message: JSONRPCMessage = {
147
- jsonrpc: '2.0',
148
- id: '1',
149
- method: 'test',
150
- params: {},
151
- };
152
-
153
- mockStdin.write = vi.fn().mockReturnValue(true);
154
-
155
- await transport.send(message);
156
-
157
- expect(mockStdin.write).toHaveBeenCalledWith(
158
- JSON.stringify(message) + '\n',
159
- );
160
- });
161
-
162
- it('should handle write backpressure', async () => {
163
- const message: JSONRPCMessage = {
164
- jsonrpc: '2.0',
165
- id: '1',
166
- method: 'test',
167
- params: {},
168
- };
169
-
170
- mockStdin.write = vi.fn().mockReturnValue(false);
171
-
172
- const sendPromise = transport.send(message);
173
-
174
- mockStdin.emit('drain');
175
-
176
- await expect(sendPromise).resolves.toBeUndefined();
177
- });
178
-
179
- it('should throw error if transport is not connected', async () => {
180
- await transport.close();
181
-
182
- const message: JSONRPCMessage = {
183
- jsonrpc: '2.0',
184
- id: '1',
185
- method: 'test',
186
- params: {},
187
- };
188
-
189
- await expect(transport.send(message)).rejects.toThrow(MCPClientError);
190
- });
191
- });
192
-
193
- describe('message handling', () => {
194
- const onMessageSpy = vi.fn();
195
-
196
- beforeEach(async () => {
197
- mockChildProcess.on.mockImplementation(
198
- (event: string, callback: () => void) => {
199
- if (event === 'spawn') {
200
- callback();
201
- }
202
- },
203
- );
204
- transport.onmessage = onMessageSpy;
205
- await transport.start();
206
- });
207
-
208
- it('should handle incoming messages correctly', async () => {
209
- const message: JSONRPCMessage = {
210
- jsonrpc: '2.0',
211
- id: '1',
212
- method: 'test',
213
- params: {},
214
- };
215
-
216
- mockStdout.emit('data', Buffer.from(JSON.stringify(message) + '\n'));
217
- expect(onMessageSpy).toHaveBeenCalledWith(message);
218
- });
219
-
220
- it('should handle partial messages correctly', async () => {
221
- const message = {
222
- jsonrpc: '2.0',
223
- id: '1',
224
- method: 'test',
225
- params: {},
226
- };
227
-
228
- const messageStr = JSON.stringify(message);
229
- mockStdout.emit('data', Buffer.from(messageStr.slice(0, 10)));
230
- mockStdout.emit('data', Buffer.from(messageStr.slice(10) + '\n'));
231
- expect(onMessageSpy).toHaveBeenCalledWith(message);
232
- });
233
- });
234
-
235
- describe('close', () => {
236
- const onCloseSpy = vi.fn();
237
-
238
- beforeEach(async () => {
239
- mockChildProcess.on.mockImplementation(
240
- (event: string, callback: (code?: number) => void) => {
241
- if (event === 'spawn') {
242
- callback();
243
- } else if (event === 'close') {
244
- callback(0);
245
- }
246
- },
247
- );
248
- transport.onclose = onCloseSpy;
249
- await transport.start();
250
- });
251
-
252
- it('should close the transport successfully', async () => {
253
- await transport.close();
254
-
255
- expect(mockChildProcess.on).toHaveBeenCalledWith(
256
- 'close',
257
- expect.any(Function),
258
- );
259
- expect(onCloseSpy).toHaveBeenCalled();
260
- });
261
- });
262
- });
@@ -1,157 +0,0 @@
1
- import type { ChildProcess, IOType } from 'node:child_process';
2
- import { Stream } from 'node:stream';
3
- import {
4
- JSONRPCMessage,
5
- JSONRPCMessageSchema,
6
- } from '../core/tool/mcp/json-rpc-message';
7
- import { MCPTransport } from '../core/tool/mcp/mcp-transport';
8
- import { MCPClientError } from '../errors';
9
- import { createChildProcess } from './create-child-process';
10
-
11
- export interface StdioConfig {
12
- command: string;
13
- args?: string[];
14
- env?: Record<string, string>;
15
- stderr?: IOType | Stream | number;
16
- cwd?: string;
17
- }
18
-
19
- export class StdioMCPTransport implements MCPTransport {
20
- private process?: ChildProcess;
21
- private abortController: AbortController = new AbortController();
22
- private readBuffer: ReadBuffer = new ReadBuffer();
23
- private serverParams: StdioConfig;
24
-
25
- onclose?: () => void;
26
- onerror?: (error: unknown) => void;
27
- onmessage?: (message: JSONRPCMessage) => void;
28
-
29
- constructor(server: StdioConfig) {
30
- this.serverParams = server;
31
- }
32
-
33
- async start(): Promise<void> {
34
- if (this.process) {
35
- throw new MCPClientError({
36
- message: 'StdioMCPTransport already started.',
37
- });
38
- }
39
-
40
- return new Promise(async (resolve, reject) => {
41
- try {
42
- const process = await createChildProcess(
43
- this.serverParams,
44
- this.abortController.signal,
45
- );
46
-
47
- this.process = process;
48
-
49
- this.process.on('error', error => {
50
- if (error.name === 'AbortError') {
51
- this.onclose?.();
52
- return;
53
- }
54
-
55
- reject(error);
56
- this.onerror?.(error);
57
- });
58
-
59
- this.process.on('spawn', () => {
60
- resolve();
61
- });
62
-
63
- this.process.on('close', _code => {
64
- this.process = undefined;
65
- this.onclose?.();
66
- });
67
-
68
- this.process.stdin?.on('error', error => {
69
- this.onerror?.(error);
70
- });
71
-
72
- this.process.stdout?.on('data', chunk => {
73
- this.readBuffer.append(chunk);
74
- this.processReadBuffer();
75
- });
76
-
77
- this.process.stdout?.on('error', error => {
78
- this.onerror?.(error);
79
- });
80
- } catch (error) {
81
- reject(error);
82
- this.onerror?.(error);
83
- }
84
- });
85
- }
86
-
87
- private processReadBuffer() {
88
- while (true) {
89
- try {
90
- const message = this.readBuffer.readMessage();
91
- if (message === null) {
92
- break;
93
- }
94
-
95
- this.onmessage?.(message);
96
- } catch (error) {
97
- this.onerror?.(error as Error);
98
- }
99
- }
100
- }
101
-
102
- async close(): Promise<void> {
103
- this.abortController.abort();
104
- this.process = undefined;
105
- this.readBuffer.clear();
106
- }
107
-
108
- send(message: JSONRPCMessage): Promise<void> {
109
- return new Promise(resolve => {
110
- if (!this.process?.stdin) {
111
- throw new MCPClientError({
112
- message: 'StdioClientTransport not connected',
113
- });
114
- }
115
-
116
- const json = serializeMessage(message);
117
- if (this.process.stdin.write(json)) {
118
- resolve();
119
- } else {
120
- this.process.stdin.once('drain', resolve);
121
- }
122
- });
123
- }
124
- }
125
-
126
- class ReadBuffer {
127
- private buffer?: Buffer;
128
-
129
- append(chunk: Buffer): void {
130
- this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
131
- }
132
-
133
- readMessage(): JSONRPCMessage | null {
134
- if (!this.buffer) return null;
135
-
136
- const index = this.buffer.indexOf('\n');
137
- if (index === -1) {
138
- return null;
139
- }
140
-
141
- const line = this.buffer.toString('utf8', 0, index);
142
- this.buffer = this.buffer.subarray(index + 1);
143
- return deserializeMessage(line);
144
- }
145
-
146
- clear(): void {
147
- this.buffer = undefined;
148
- }
149
- }
150
-
151
- function serializeMessage(message: JSONRPCMessage): string {
152
- return JSON.stringify(message) + '\n';
153
- }
154
-
155
- export function deserializeMessage(line: string): JSONRPCMessage {
156
- return JSONRPCMessageSchema.parse(JSON.parse(line));
157
- }
@@ -1,18 +0,0 @@
1
- import * as _ai_sdk_react from '@ai-sdk/react';
2
- import { useChat as useChat$1, useCompletion as useCompletion$1 } from '@ai-sdk/react';
3
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions } from '@ai-sdk/react';
4
-
5
- /**
6
- * @deprecated Use `@ai-sdk/react` instead.
7
- */
8
- declare const useChat: typeof useChat$1;
9
- /**
10
- * @deprecated Use `@ai-sdk/react` instead.
11
- */
12
- declare const useCompletion: typeof useCompletion$1;
13
- /**
14
- * @deprecated Use `@ai-sdk/react` instead.
15
- */
16
- declare const experimental_useObject: <RESULT, INPUT = any>({ api, id, schema, initialValue, fetch, onError, onFinish, headers, credentials, }: _ai_sdk_react.Experimental_UseObjectOptions<RESULT>) => _ai_sdk_react.Experimental_UseObjectHelpers<RESULT, INPUT>;
17
-
18
- export { experimental_useObject, useChat, useCompletion };
@@ -1,18 +0,0 @@
1
- import * as _ai_sdk_react from '@ai-sdk/react';
2
- import { useChat as useChat$1, useCompletion as useCompletion$1 } from '@ai-sdk/react';
3
- export { CreateMessage, Message, UseChatHelpers, UseChatOptions } from '@ai-sdk/react';
4
-
5
- /**
6
- * @deprecated Use `@ai-sdk/react` instead.
7
- */
8
- declare const useChat: typeof useChat$1;
9
- /**
10
- * @deprecated Use `@ai-sdk/react` instead.
11
- */
12
- declare const useCompletion: typeof useCompletion$1;
13
- /**
14
- * @deprecated Use `@ai-sdk/react` instead.
15
- */
16
- declare const experimental_useObject: <RESULT, INPUT = any>({ api, id, schema, initialValue, fetch, onError, onFinish, headers, credentials, }: _ai_sdk_react.Experimental_UseObjectOptions<RESULT>) => _ai_sdk_react.Experimental_UseObjectHelpers<RESULT, INPUT>;
17
-
18
- export { experimental_useObject, useChat, useCompletion };
@@ -1,39 +0,0 @@
1
- 'use client'
2
- "use strict";
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, { get: all[name], enumerable: true });
10
- };
11
- var __copyProps = (to, from, except, desc) => {
12
- if (from && typeof from === "object" || typeof from === "function") {
13
- for (let key of __getOwnPropNames(from))
14
- if (!__hasOwnProp.call(to, key) && key !== except)
15
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
- }
17
- return to;
18
- };
19
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
-
21
- // react/index.ts
22
- var react_exports = {};
23
- __export(react_exports, {
24
- experimental_useObject: () => experimental_useObject,
25
- useChat: () => useChat,
26
- useCompletion: () => useCompletion
27
- });
28
- module.exports = __toCommonJS(react_exports);
29
- var import_react = require("@ai-sdk/react");
30
- var useChat = import_react.useChat;
31
- var useCompletion = import_react.useCompletion;
32
- var experimental_useObject = import_react.experimental_useObject;
33
- // Annotate the CommonJS export names for ESM import in node:
34
- 0 && (module.exports = {
35
- experimental_useObject,
36
- useChat,
37
- useCompletion
38
- });
39
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../index.ts"],"sourcesContent":["import {\n useChat as useChatReact,\n useCompletion as useCompletionReact,\n experimental_useObject as experimental_useObjectReact,\n} from '@ai-sdk/react';\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const useChat = useChatReact;\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const useCompletion = useCompletionReact;\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const experimental_useObject = experimental_useObjectReact;\n\nexport type {\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n CreateMessage,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n Message,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n UseChatOptions,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n UseChatHelpers,\n} from '@ai-sdk/react';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAIO;AAKA,IAAM,UAAU,aAAAA;AAKhB,IAAM,gBAAgB,aAAAC;AAKtB,IAAM,yBAAyB,aAAAC;","names":["useChatReact","useCompletionReact","experimental_useObjectReact"]}
@@ -1,17 +0,0 @@
1
- 'use client'
2
-
3
- // react/index.ts
4
- import {
5
- useChat as useChatReact,
6
- useCompletion as useCompletionReact,
7
- experimental_useObject as experimental_useObjectReact
8
- } from "@ai-sdk/react";
9
- var useChat = useChatReact;
10
- var useCompletion = useCompletionReact;
11
- var experimental_useObject = experimental_useObjectReact;
12
- export {
13
- experimental_useObject,
14
- useChat,
15
- useCompletion
16
- };
17
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../index.ts"],"sourcesContent":["import {\n useChat as useChatReact,\n useCompletion as useCompletionReact,\n experimental_useObject as experimental_useObjectReact,\n} from '@ai-sdk/react';\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const useChat = useChatReact;\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const useCompletion = useCompletionReact;\n\n/**\n * @deprecated Use `@ai-sdk/react` instead.\n */\nexport const experimental_useObject = experimental_useObjectReact;\n\nexport type {\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n CreateMessage,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n Message,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n UseChatOptions,\n\n /**\n * @deprecated Use `@ai-sdk/react` instead.\n */\n UseChatHelpers,\n} from '@ai-sdk/react';\n"],"mappings":";;;AAAA;AAAA,EACE,WAAW;AAAA,EACX,iBAAiB;AAAA,EACjB,0BAA0B;AAAA,OACrB;AAKA,IAAM,UAAU;AAKhB,IAAM,gBAAgB;AAKtB,IAAM,yBAAyB;","names":[]}