@ai-sdk/rsc 2.0.47 → 2.0.49

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.
@@ -1,179 +0,0 @@
1
- import { delay } from '@ai-sdk/provider-utils';
2
- import { convertArrayToReadableStream } from '@ai-sdk/provider-utils/test';
3
- import { createStreamableValue } from './create-streamable-value';
4
- import { STREAMABLE_VALUE_TYPE, StreamableValue } from './streamable-value';
5
- import { it, expect } from 'vitest';
6
-
7
- async function getRawChunks(streamableValue: StreamableValue<any, any>) {
8
- const chunks = [];
9
- let currentValue = streamableValue;
10
-
11
- while (true) {
12
- const { next, ...otherFields } = currentValue;
13
-
14
- chunks.push(otherFields);
15
-
16
- if (!next) break;
17
-
18
- currentValue = await next;
19
- }
20
-
21
- return chunks;
22
- }
23
-
24
- it('should return latest value', async () => {
25
- const streamableValue = createStreamableValue(1).update(2).update(3).done(4);
26
-
27
- expect(streamableValue.value.curr).toStrictEqual(4);
28
- });
29
-
30
- it('should be able to stream any JSON values', async () => {
31
- const streamable = createStreamableValue();
32
- streamable.update({ v: 123 });
33
-
34
- expect(streamable.value.curr).toStrictEqual({ v: 123 });
35
-
36
- streamable.done();
37
- });
38
-
39
- it('should support .error()', async () => {
40
- const streamable = createStreamableValue();
41
- streamable.error('This is an error');
42
-
43
- expect(streamable.value).toStrictEqual({
44
- error: 'This is an error',
45
- type: STREAMABLE_VALUE_TYPE,
46
- });
47
- });
48
-
49
- it('should directly emit the final value when reading .value', async () => {
50
- const streamable = createStreamableValue('1');
51
- streamable.update('2');
52
- streamable.update('3');
53
-
54
- expect(streamable.value.curr).toStrictEqual('3');
55
-
56
- streamable.done('4');
57
-
58
- expect(streamable.value.curr).toStrictEqual('4');
59
- });
60
-
61
- it('should be able to append strings as patch', async () => {
62
- const streamable = createStreamableValue();
63
- const value = streamable.value;
64
-
65
- streamable.update('hello');
66
- streamable.update('hello world');
67
- streamable.update('hello world!');
68
- streamable.update('new string');
69
- streamable.done('new string with patch!');
70
-
71
- expect(await getRawChunks(value)).toStrictEqual([
72
- { curr: undefined, type: STREAMABLE_VALUE_TYPE },
73
- { curr: 'hello' },
74
- { diff: [0, ' world'] },
75
- { diff: [0, '!'] },
76
- { curr: 'new string' },
77
- { diff: [0, ' with patch!'] },
78
- ]);
79
- });
80
-
81
- it('should be able to call .append() to send patches', async () => {
82
- const streamable = createStreamableValue();
83
- const value = streamable.value;
84
-
85
- streamable.append('hello');
86
- streamable.append(' world');
87
- streamable.append('!');
88
- streamable.done();
89
-
90
- expect(await getRawChunks(value)).toStrictEqual([
91
- { curr: undefined, type: STREAMABLE_VALUE_TYPE },
92
- { curr: 'hello' },
93
- { diff: [0, ' world'] },
94
- { diff: [0, '!'] },
95
- {},
96
- ]);
97
- });
98
-
99
- it('should be able to mix .update() and .append() with optimized payloads', async () => {
100
- const streamable = createStreamableValue('hello');
101
- const value = streamable.value;
102
-
103
- streamable.append(' world');
104
- streamable.update('hello world!!');
105
- streamable.update('some new');
106
- streamable.update('some new string');
107
- streamable.append(' with patch!');
108
- streamable.done();
109
-
110
- expect(await getRawChunks(value)).toStrictEqual([
111
- { curr: 'hello', type: STREAMABLE_VALUE_TYPE },
112
- { diff: [0, ' world'] },
113
- { diff: [0, '!!'] },
114
- { curr: 'some new' },
115
- { diff: [0, ' string'] },
116
- { diff: [0, ' with patch!'] },
117
- {},
118
- ]);
119
- });
120
-
121
- it('should behave like .update() with .append() and .done()', async () => {
122
- const streamable = createStreamableValue('hello');
123
- const value = streamable.value;
124
-
125
- streamable.append(' world');
126
- streamable.done('fin');
127
-
128
- expect(await getRawChunks(value)).toStrictEqual([
129
- { curr: 'hello', type: STREAMABLE_VALUE_TYPE },
130
- { diff: [0, ' world'] },
131
- { curr: 'fin' },
132
- ]);
133
- });
134
-
135
- it('should be able to accept readableStream as the source', async () => {
136
- const streamable = createStreamableValue(
137
- convertArrayToReadableStream(['hello', ' world', '!']),
138
- );
139
- const value = streamable.value;
140
-
141
- expect(await getRawChunks(value)).toStrictEqual([
142
- { curr: undefined, type: STREAMABLE_VALUE_TYPE },
143
- { curr: 'hello' },
144
- { diff: [0, ' world'] },
145
- { diff: [0, '!'] },
146
- {},
147
- ]);
148
- });
149
-
150
- it('should accept readableStream with JSON payloads', async () => {
151
- const streamable = createStreamableValue(
152
- convertArrayToReadableStream([{ v: 1 }, { v: 2 }, { v: 3 }]),
153
- );
154
- const value = streamable.value;
155
-
156
- expect(await getRawChunks(value)).toStrictEqual([
157
- { curr: undefined, type: STREAMABLE_VALUE_TYPE },
158
- { curr: { v: 1 } },
159
- { curr: { v: 2 } },
160
- { curr: { v: 3 } },
161
- {},
162
- ]);
163
- });
164
-
165
- it('should lock the streamable if from readableStream', async () => {
166
- const streamable = createStreamableValue(
167
- new ReadableStream({
168
- async start(controller) {
169
- await delay();
170
- controller.enqueue('hello');
171
- controller.close();
172
- },
173
- }),
174
- );
175
-
176
- expect(() => streamable.update('world')).toThrowErrorMatchingInlineSnapshot(
177
- '[Error: .update(): Value stream is locked and cannot be updated.]',
178
- );
179
- });
@@ -1,165 +0,0 @@
1
- import { delay } from '@ai-sdk/provider-utils';
2
- import { createStreamableValue } from './create-streamable-value';
3
- import { readStreamableValue } from './read-streamable-value';
4
- import { it, expect } from 'vitest';
5
-
6
- it('should return an async iterable', () => {
7
- const streamable = createStreamableValue();
8
- const result = readStreamableValue(streamable.value);
9
- streamable.done();
10
-
11
- expect(result).toBeDefined();
12
- expect(result[Symbol.asyncIterator]).toBeDefined();
13
- });
14
-
15
- it('should support reading streamed values and errors', async () => {
16
- const streamable = createStreamableValue(1);
17
-
18
- (async () => {
19
- await delay();
20
- streamable.update(2);
21
- await delay();
22
- streamable.update(3);
23
- await delay();
24
- streamable.error('This is an error');
25
- })();
26
-
27
- const values = [];
28
-
29
- try {
30
- for await (const v of readStreamableValue(streamable.value)) {
31
- values.push(v);
32
- }
33
- expect.fail('should not be reached');
34
- } catch (e) {
35
- expect(e).toStrictEqual('This is an error');
36
- }
37
-
38
- expect(values).toStrictEqual([1, 2, 3]);
39
- });
40
-
41
- it('should be able to read values asynchronously with different value types', async () => {
42
- const streamable = createStreamableValue({});
43
-
44
- (async () => {
45
- await delay();
46
- streamable.update([1]);
47
- streamable.update(['2']);
48
- streamable.done({ 3: 3 });
49
- })();
50
-
51
- const values = [];
52
- for await (const v of readStreamableValue(streamable.value)) {
53
- values.push(v);
54
- }
55
-
56
- expect(values).toStrictEqual([{}, [1], ['2'], { '3': 3 }]);
57
- });
58
-
59
- it('should be able to replay errors', async () => {
60
- const streamable = createStreamableValue(0);
61
-
62
- (async () => {
63
- await delay();
64
- streamable.update(1);
65
- streamable.update(2);
66
- streamable.error({ customErrorMessage: 'this is an error' });
67
- })();
68
-
69
- const values = [];
70
-
71
- try {
72
- for await (const v of readStreamableValue(streamable.value)) {
73
- values.push(v);
74
- }
75
-
76
- expect.fail('should not be reached');
77
- } catch (e) {
78
- expect(e).toStrictEqual({
79
- customErrorMessage: 'this is an error',
80
- });
81
- }
82
- expect(values).toStrictEqual([0, 1, 2]);
83
- });
84
-
85
- it('should be able to append strings as patch', async () => {
86
- const streamable = createStreamableValue();
87
- const value = streamable.value;
88
-
89
- streamable.update('hello');
90
- streamable.update('hello world');
91
- streamable.update('hello world!');
92
- streamable.update('new string');
93
- streamable.done('new string with patch!');
94
-
95
- const values = [];
96
- for await (const v of readStreamableValue(value)) {
97
- values.push(v);
98
- }
99
-
100
- expect(values).toStrictEqual([
101
- 'hello',
102
- 'hello world',
103
- 'hello world!',
104
- 'new string',
105
- 'new string with patch!',
106
- ]);
107
- });
108
-
109
- it('should be able to call .append() to send patches', async () => {
110
- const streamable = createStreamableValue();
111
- const value = streamable.value;
112
-
113
- streamable.append('hello');
114
- streamable.append(' world');
115
- streamable.append('!');
116
- streamable.done();
117
-
118
- const values = [];
119
- for await (const v of readStreamableValue(value)) {
120
- values.push(v);
121
- }
122
-
123
- expect(values).toStrictEqual(['hello', 'hello world', 'hello world!']);
124
- });
125
-
126
- it('should be able to mix .update() and .append() with optimized payloads', async () => {
127
- const streamable = createStreamableValue('hello');
128
- const value = streamable.value;
129
-
130
- streamable.append(' world');
131
- streamable.update('hello world!!');
132
- streamable.update('some new');
133
- streamable.update('some new string');
134
- streamable.append(' with patch!');
135
- streamable.done();
136
-
137
- const values = [];
138
- for await (const v of readStreamableValue(value)) {
139
- values.push(v);
140
- }
141
-
142
- expect(values).toStrictEqual([
143
- 'hello',
144
- 'hello world',
145
- 'hello world!!',
146
- 'some new',
147
- 'some new string',
148
- 'some new string with patch!',
149
- ]);
150
- });
151
-
152
- it('should behave like .update() with .append() and .done()', async () => {
153
- const streamable = createStreamableValue('hello');
154
- const value = streamable.value;
155
-
156
- streamable.append(' world');
157
- streamable.done('fin');
158
-
159
- const values = [];
160
- for await (const v of readStreamableValue(value)) {
161
- values.push(v);
162
- }
163
-
164
- expect(values).toStrictEqual(['hello', 'hello world', 'fin']);
165
- });
@@ -1,17 +0,0 @@
1
- import { expectTypeOf } from 'vitest';
2
- import { describe, it } from 'vitest';
3
- import type { StreamableValue } from '.';
4
-
5
- describe('StreamableValue type', () => {
6
- it('should yield a type error when assigning a wrong value', () => {
7
- expectTypeOf<StreamableValue<string>>().not.toEqualTypeOf<
8
- StreamableValue<boolean>
9
- >();
10
-
11
- expectTypeOf<StreamableValue<string>>().not.toEqualTypeOf<string>();
12
-
13
- expectTypeOf<
14
- StreamableValue<string>
15
- >().not.toEqualTypeOf<'THIS IS NOT A STREAMABLE VALUE'>();
16
- });
17
- });