* Run streaming structured output via the TextEngine, with the engine's * `finalStructuredOutput.yieldChunks: true` mode. The agent loop's * RUN_STARTED/RUN_FINISHED are suppressed; the structured-output finalization * step's pair brackets the run for the consumer. * * Standard Schema *validati
( options: TextActivityOptions<AnyTextAdapter, TSchema, true, TContext>, )
| 3080 | * those are programmer errors, not runtime conditions. |
| 3081 | */ |
| 3082 | function runStreamingStructuredOutput< |
| 3083 | TSchema extends SchemaInput, |
| 3084 | TContext = unknown, |
| 3085 | >( |
| 3086 | options: TextActivityOptions<AnyTextAdapter, TSchema, true, TContext>, |
| 3087 | ): StructuredOutputStream<InferSchemaType<TSchema>> { |
| 3088 | const { outputSchema } = options |
| 3089 | |
| 3090 | if (!outputSchema) { |
| 3091 | throw new Error('outputSchema is required for streaming structured output') |
| 3092 | } |
| 3093 | |
| 3094 | // forStructuredOutput strict-converts the schema once at the activity |
| 3095 | // boundary, capturing the null-widening map so the engine can un-widen the |
| 3096 | // provider's response before it reaches the consumer. Adapters can re-convert |
| 3097 | // if their wire format diverges, but the default flow hands them a |
| 3098 | // strict-ready schema. |
| 3099 | const { jsonSchema, nullWideningMap } = |
| 3100 | convertSchemaForStructuredOutput(outputSchema) |
| 3101 | if (!jsonSchema) { |
| 3102 | throw new Error('Failed to convert output schema to JSON Schema') |
| 3103 | } |
| 3104 | const normalize = (data: unknown): unknown => |
| 3105 | undoNullWidening(data, nullWideningMap) |
| 3106 | |
| 3107 | // The implementation generator yields the broader internal type |
| 3108 | // (`StreamChunk | StructuredOutputCompleteEvent<T>`) so agent-loop |
| 3109 | // CustomEvents can flow through; the public-facing type narrows to |
| 3110 | // `Exclude<StreamChunk, CustomEvent> | StructuredOutputCompleteEvent<T>` |
| 3111 | // which lets consumers narrow `chunk.value` cleanly. The widen→narrow |
| 3112 | // is contained here so consumers see only the strict type. |
| 3113 | return runStreamingStructuredOutputImpl( |
| 3114 | options, |
| 3115 | jsonSchema, |
| 3116 | normalize, |
| 3117 | ) as StructuredOutputStream<InferSchemaType<TSchema>> |
| 3118 | } |
| 3119 | |
| 3120 | /** |
| 3121 | * Internal generator return type — broader than the public |
no test coverage detected