@xsai/stream-object 0.3.0-beta.7 → 0.3.0-beta.8

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/dist/index.d.ts CHANGED
@@ -701,24 +701,28 @@ interface StreamObjectResult<T extends Schema> extends StreamTextResult {
701
701
  elementStream?: ReadableStream<Infer<T>>;
702
702
  partialObjectStream?: ReadableStream<PartialDeep<Infer<T>>>;
703
703
  }
704
- interface StreamObjectExtraOptions<T extends Schema> {
705
- onFinish?: (result: StreamObjectOnFinishResult<T>) => unknown;
706
- }
707
- declare function streamObject<T extends Schema>(options: StreamObjectExtraOptions<T> & StreamObjectOptions<T> & {
704
+ declare function streamObject<T extends Schema>(options: StreamObjectOptions<T> & {
708
705
  output: 'array';
709
706
  }): Promise<StreamObjectResult<T> & {
710
707
  elementStream: ReadableStream<Infer<T>>;
711
708
  partialObjectStream: undefined;
712
709
  }>;
713
- declare function streamObject<T extends Schema>(options: StreamObjectExtraOptions<T> & StreamObjectOptions<T> & {
710
+ declare function streamObject<T extends Schema>(options: StreamObjectOptions<T> & {
714
711
  output: 'object';
715
712
  }): Promise<StreamObjectResult<T> & {
716
713
  elementStream: undefined;
717
714
  partialObjectStream: ReadableStream<PartialDeep<Infer<T>>>;
718
715
  }>;
719
- declare function streamObject<T extends Schema>(options: StreamObjectExtraOptions<T> & StreamObjectOptions<T>): Promise<StreamObjectResult<T> & {
716
+ declare function streamObject<T extends Schema>(options: StreamObjectOptions<T>): Promise<StreamObjectResult<T> & {
720
717
  elementStream: undefined;
721
718
  partialObjectStream: ReadableStream<PartialDeep<Infer<T>>>;
722
719
  }>;
723
720
 
724
- export { type StreamObjectOnFinishResult, type StreamObjectOptions, type StreamObjectResult, streamObject };
721
+ declare const toElementStream: <T>(stream: ReadableStream<string>) => ReadableStream<T>;
722
+
723
+ declare const toPartialObjectStream: <T>(stream: ReadableStream<string>) => ReadableStream<undefined<T, {
724
+ recurseIntoArrays: false;
725
+ allowUndefinedInNonTupleArrays: true;
726
+ }>>;
727
+
728
+ export { type StreamObjectOnFinishResult, type StreamObjectOptions, type StreamObjectResult, streamObject, toElementStream, toPartialObjectStream };
package/dist/index.js CHANGED
@@ -1,5 +1,16 @@
1
1
  import { streamText } from '@xsai/stream-text';
2
- import { toJsonSchema } from 'xsschema';
2
+ import { toJsonSchema, strictJsonSchema } from 'xsschema';
3
+
4
+ const wrap = (items) => ({
5
+ properties: {
6
+ elements: {
7
+ items,
8
+ type: "array"
9
+ }
10
+ },
11
+ required: ["elements"],
12
+ type: "object"
13
+ });
3
14
 
4
15
  var parse = {};
5
16
 
@@ -231,21 +242,48 @@ function requireParse () {
231
242
 
232
243
  var parseExports = requireParse();
233
244
 
234
- const wrap = (schema) => {
235
- return {
236
- properties: {
237
- elements: {
238
- items: schema,
239
- type: "array"
240
- }
245
+ const toElementStream = (stream) => {
246
+ let partialObjectData = "";
247
+ let index = 0;
248
+ return stream.pipeThrough(new TransformStream({
249
+ flush: (controller) => {
250
+ const data = parseExports.parse(partialObjectData);
251
+ controller.enqueue(data.elements.at(-1));
241
252
  },
242
- required: ["elements"],
243
- type: "object"
244
- };
253
+ transform: (chunk, controller) => {
254
+ partialObjectData += chunk;
255
+ try {
256
+ const data = parseExports.parse(partialObjectData);
257
+ if (Array.isArray(Object.getOwnPropertyDescriptor(data, "elements")?.value) && data.elements.length > index + 1) {
258
+ controller.enqueue(data.elements[index++]);
259
+ }
260
+ } catch {
261
+ }
262
+ }
263
+ }));
264
+ };
265
+
266
+ const toPartialObjectStream = (stream) => {
267
+ let partialObjectData = "";
268
+ let partialObjectSnapshot = {};
269
+ return stream.pipeThrough(new TransformStream({
270
+ transform: (chunk, controller) => {
271
+ partialObjectData += chunk;
272
+ try {
273
+ const data = parseExports.parse(partialObjectData);
274
+ if (JSON.stringify(partialObjectSnapshot) !== JSON.stringify(data)) {
275
+ partialObjectSnapshot = data;
276
+ controller.enqueue(data);
277
+ }
278
+ } catch {
279
+ }
280
+ }
281
+ }));
245
282
  };
283
+
246
284
  async function streamObject(options) {
247
285
  const { schema: schemaValidator } = options;
248
- let schema = await toJsonSchema(schemaValidator);
286
+ let schema = await toJsonSchema(schemaValidator).then((schema2) => strictJsonSchema(schema2));
249
287
  if (options.output === "array")
250
288
  schema = wrap(schema);
251
289
  return streamText({
@@ -265,46 +303,14 @@ async function streamObject(options) {
265
303
  }).then(({ chunkStream, stepStream, textStream }) => {
266
304
  let elementStream;
267
305
  let partialObjectStream;
268
- let index = 0;
269
306
  if (options.output === "array") {
270
307
  let rawElementStream;
271
308
  [rawElementStream, textStream] = textStream.tee();
272
- let partialData = "";
273
- elementStream = rawElementStream.pipeThrough(new TransformStream({
274
- flush: (controller) => {
275
- const data = parseExports.parse(partialData);
276
- controller.enqueue(data.elements.at(-1));
277
- options.onFinish?.({ object: data.elements });
278
- },
279
- transform: (chunk, controller) => {
280
- partialData += chunk;
281
- try {
282
- const data = parseExports.parse(partialData);
283
- if (Array.isArray(Object.getOwnPropertyDescriptor(data, "elements")?.value) && data.elements.length > index + 1) {
284
- controller.enqueue(data.elements[index++]);
285
- }
286
- } catch {
287
- }
288
- }
289
- }));
309
+ elementStream = toElementStream(rawElementStream);
290
310
  } else {
291
311
  let rawPartialObjectStream;
292
312
  [textStream, rawPartialObjectStream] = textStream.tee();
293
- let partialObjectData = "";
294
- let partialObjectSnapshot = {};
295
- partialObjectStream = rawPartialObjectStream.pipeThrough(new TransformStream({
296
- transform: (chunk, controller) => {
297
- partialObjectData += chunk;
298
- try {
299
- const data = parseExports.parse(partialObjectData);
300
- if (JSON.stringify(partialObjectSnapshot) !== JSON.stringify(data)) {
301
- partialObjectSnapshot = data;
302
- controller.enqueue(data);
303
- }
304
- } catch {
305
- }
306
- }
307
- }));
313
+ partialObjectStream = toPartialObjectStream(rawPartialObjectStream);
308
314
  }
309
315
  return {
310
316
  chunkStream,
@@ -316,4 +322,4 @@ async function streamObject(options) {
316
322
  });
317
323
  }
318
324
 
319
- export { streamObject };
325
+ export { streamObject, toElementStream, toPartialObjectStream };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/stream-object",
3
3
  "type": "module",
4
- "version": "0.3.0-beta.7",
4
+ "version": "0.3.0-beta.8",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",
@@ -29,8 +29,8 @@
29
29
  "dist"
30
30
  ],
31
31
  "dependencies": {
32
- "@xsai/stream-text": "~0.3.0-beta.7",
33
- "xsschema": "~0.3.0-beta.7"
32
+ "@xsai/stream-text": "~0.3.0-beta.8",
33
+ "xsschema": "~0.3.0-beta.8"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@valibot/to-json-schema": "^1.0.0",