@ai-sdk/react 0.0.52 → 0.0.53
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.
- package/CHANGELOG.md +7 -0
- package/package.json +6 -2
- package/.eslintrc.js +0 -4
- package/.turbo/turbo-build.log +0 -21
- package/.turbo/turbo-clean.log +0 -4
- package/src/index.ts +0 -4
- package/src/use-assistant.ts +0 -301
- package/src/use-assistant.ui.test.tsx +0 -337
- package/src/use-chat.ts +0 -716
- package/src/use-chat.ui.test.tsx +0 -1542
- package/src/use-completion.ts +0 -213
- package/src/use-completion.ui.test.tsx +0 -166
- package/src/use-object.ts +0 -229
- package/src/use-object.ui.test.tsx +0 -218
- package/tsconfig.json +0 -9
- package/tsup.config.ts +0 -13
- package/turbo.json +0 -8
- package/vitest.config.js +0 -12
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
describeWithTestServer,
|
|
3
|
-
withTestServer,
|
|
4
|
-
} from '@ai-sdk/provider-utils/test';
|
|
5
|
-
import '@testing-library/jest-dom/vitest';
|
|
6
|
-
import { cleanup, render, screen, waitFor } from '@testing-library/react';
|
|
7
|
-
import userEvent from '@testing-library/user-event';
|
|
8
|
-
import { z } from 'zod';
|
|
9
|
-
import { experimental_useObject } from './use-object';
|
|
10
|
-
|
|
11
|
-
describe('text stream', () => {
|
|
12
|
-
let onErrorResult: Error | undefined;
|
|
13
|
-
let onFinishCalls: Array<{
|
|
14
|
-
object: { content: string } | undefined;
|
|
15
|
-
error: Error | undefined;
|
|
16
|
-
}> = [];
|
|
17
|
-
|
|
18
|
-
const TestComponent = () => {
|
|
19
|
-
const { object, error, submit, isLoading, stop } = experimental_useObject({
|
|
20
|
-
api: '/api/use-object',
|
|
21
|
-
schema: z.object({ content: z.string() }),
|
|
22
|
-
onError(error) {
|
|
23
|
-
onErrorResult = error;
|
|
24
|
-
},
|
|
25
|
-
onFinish(event) {
|
|
26
|
-
onFinishCalls.push(event);
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
return (
|
|
31
|
-
<div>
|
|
32
|
-
<div data-testid="loading">{isLoading.toString()}</div>
|
|
33
|
-
<div data-testid="object">{JSON.stringify(object)}</div>
|
|
34
|
-
<div data-testid="error">{error?.toString()}</div>
|
|
35
|
-
<button
|
|
36
|
-
data-testid="submit-button"
|
|
37
|
-
onClick={() => submit('test-input')}
|
|
38
|
-
>
|
|
39
|
-
Generate
|
|
40
|
-
</button>
|
|
41
|
-
<button data-testid="stop-button" onClick={stop}>
|
|
42
|
-
Stop
|
|
43
|
-
</button>
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
beforeEach(() => {
|
|
49
|
-
render(<TestComponent />);
|
|
50
|
-
onErrorResult = undefined;
|
|
51
|
-
onFinishCalls = [];
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
afterEach(() => {
|
|
55
|
-
vi.restoreAllMocks();
|
|
56
|
-
cleanup();
|
|
57
|
-
onErrorResult = undefined;
|
|
58
|
-
onFinishCalls = [];
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describeWithTestServer(
|
|
62
|
-
"when the API returns 'Hello, world!'",
|
|
63
|
-
{
|
|
64
|
-
url: '/api/use-object',
|
|
65
|
-
type: 'stream-values',
|
|
66
|
-
content: ['{ ', '"content": "Hello, ', 'world', '!"'],
|
|
67
|
-
},
|
|
68
|
-
({ call }) => {
|
|
69
|
-
beforeEach(async () => {
|
|
70
|
-
await userEvent.click(screen.getByTestId('submit-button'));
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should render stream', async () => {
|
|
74
|
-
await screen.findByTestId('object');
|
|
75
|
-
expect(screen.getByTestId('object')).toHaveTextContent(
|
|
76
|
-
JSON.stringify({ content: 'Hello, world!' }),
|
|
77
|
-
);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it("should send 'test' to the API", async () => {
|
|
81
|
-
expect(await call(0).getRequestBodyJson()).toBe('test-input');
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('should not have an error', async () => {
|
|
85
|
-
await screen.findByTestId('error');
|
|
86
|
-
expect(screen.getByTestId('error')).toBeEmptyDOMElement();
|
|
87
|
-
expect(onErrorResult).toBeUndefined();
|
|
88
|
-
});
|
|
89
|
-
},
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
describe('isLoading', async () => {
|
|
93
|
-
it(
|
|
94
|
-
'should be true while loading',
|
|
95
|
-
withTestServer(
|
|
96
|
-
{ url: '/api/use-object', type: 'controlled-stream' },
|
|
97
|
-
async ({ streamController }) => {
|
|
98
|
-
streamController.enqueue('{"content": ');
|
|
99
|
-
|
|
100
|
-
await userEvent.click(screen.getByTestId('submit-button'));
|
|
101
|
-
|
|
102
|
-
// wait for element "loading" to have text content "true":
|
|
103
|
-
await waitFor(() => {
|
|
104
|
-
expect(screen.getByTestId('loading')).toHaveTextContent('true');
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
streamController.enqueue('"Hello, world!"}');
|
|
108
|
-
streamController.close();
|
|
109
|
-
|
|
110
|
-
// wait for element "loading" to have text content "false":
|
|
111
|
-
await waitFor(() => {
|
|
112
|
-
expect(screen.getByTestId('loading')).toHaveTextContent('false');
|
|
113
|
-
});
|
|
114
|
-
},
|
|
115
|
-
),
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
describe('stop', async () => {
|
|
120
|
-
it(
|
|
121
|
-
'should abort the stream and not consume any more data',
|
|
122
|
-
withTestServer(
|
|
123
|
-
{ url: '/api/use-object', type: 'controlled-stream' },
|
|
124
|
-
async ({ streamController }) => {
|
|
125
|
-
streamController.enqueue('{"content": "h');
|
|
126
|
-
|
|
127
|
-
userEvent.click(screen.getByTestId('submit-button'));
|
|
128
|
-
|
|
129
|
-
// wait for element "loading" and "object" to have text content:
|
|
130
|
-
await waitFor(() => {
|
|
131
|
-
expect(screen.getByTestId('loading')).toHaveTextContent('true');
|
|
132
|
-
});
|
|
133
|
-
await waitFor(() => {
|
|
134
|
-
expect(screen.getByTestId('object')).toHaveTextContent(
|
|
135
|
-
'{"content":"h"}',
|
|
136
|
-
);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
// click stop button:
|
|
140
|
-
await userEvent.click(screen.getByTestId('stop-button'));
|
|
141
|
-
|
|
142
|
-
// wait for element "loading" to have text content "false":
|
|
143
|
-
await waitFor(() => {
|
|
144
|
-
expect(screen.getByTestId('loading')).toHaveTextContent('false');
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
// this should not be consumed any more:
|
|
148
|
-
streamController.enqueue('ello, world!"}');
|
|
149
|
-
streamController.close();
|
|
150
|
-
|
|
151
|
-
// should only show start of object:
|
|
152
|
-
expect(screen.getByTestId('object')).toHaveTextContent(
|
|
153
|
-
'{"content":"h"}',
|
|
154
|
-
);
|
|
155
|
-
},
|
|
156
|
-
),
|
|
157
|
-
);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
describe('when the API returns a 404', () => {
|
|
161
|
-
it(
|
|
162
|
-
'should render error',
|
|
163
|
-
withTestServer(
|
|
164
|
-
{
|
|
165
|
-
url: '/api/use-object',
|
|
166
|
-
type: 'error',
|
|
167
|
-
status: 404,
|
|
168
|
-
content: 'Not found',
|
|
169
|
-
},
|
|
170
|
-
async () => {
|
|
171
|
-
await userEvent.click(screen.getByTestId('submit-button'));
|
|
172
|
-
|
|
173
|
-
await screen.findByTestId('error');
|
|
174
|
-
expect(screen.getByTestId('error')).toHaveTextContent('Not found');
|
|
175
|
-
expect(onErrorResult).toBeInstanceOf(Error);
|
|
176
|
-
},
|
|
177
|
-
),
|
|
178
|
-
);
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
describe('onFinish', () => {
|
|
182
|
-
it(
|
|
183
|
-
'should be called with an object when the stream finishes and the object matches the schema',
|
|
184
|
-
withTestServer(
|
|
185
|
-
{
|
|
186
|
-
url: '/api/use-object',
|
|
187
|
-
type: 'stream-values',
|
|
188
|
-
content: ['{ ', '"content": "Hello, ', 'world', '!"', '}'],
|
|
189
|
-
},
|
|
190
|
-
async () => {
|
|
191
|
-
await userEvent.click(screen.getByTestId('submit-button'));
|
|
192
|
-
|
|
193
|
-
expect(onFinishCalls).toStrictEqual([
|
|
194
|
-
{ object: { content: 'Hello, world!' }, error: undefined },
|
|
195
|
-
]);
|
|
196
|
-
},
|
|
197
|
-
),
|
|
198
|
-
);
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
it(
|
|
202
|
-
'should be called with an error when the stream finishes and the object does not match the schema',
|
|
203
|
-
withTestServer(
|
|
204
|
-
{
|
|
205
|
-
url: '/api/use-object',
|
|
206
|
-
type: 'stream-values',
|
|
207
|
-
content: ['{ ', '"content-wrong": "Hello, ', 'world', '!"', '}'],
|
|
208
|
-
},
|
|
209
|
-
async () => {
|
|
210
|
-
await userEvent.click(screen.getByTestId('submit-button'));
|
|
211
|
-
|
|
212
|
-
expect(onFinishCalls).toStrictEqual([
|
|
213
|
-
{ object: undefined, error: expect.any(Error) },
|
|
214
|
-
]);
|
|
215
|
-
},
|
|
216
|
-
),
|
|
217
|
-
);
|
|
218
|
-
});
|
package/tsconfig.json
DELETED
package/tsup.config.ts
DELETED
package/turbo.json
DELETED
package/vitest.config.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import react from '@vitejs/plugin-react';
|
|
2
|
-
import { defineConfig } from 'vite';
|
|
3
|
-
|
|
4
|
-
// https://vitejs.dev/config/
|
|
5
|
-
export default defineConfig({
|
|
6
|
-
plugins: [react()],
|
|
7
|
-
test: {
|
|
8
|
-
environment: 'jsdom',
|
|
9
|
-
globals: true,
|
|
10
|
-
include: ['src/**/*.ui.test.ts', 'src/**/*.ui.test.tsx'],
|
|
11
|
-
},
|
|
12
|
-
});
|