| 2664 | * ``` |
| 2665 | */ |
| 2666 | export function chat< |
| 2667 | TAdapter extends AnyTextAdapter, |
| 2668 | TSchema extends SchemaInput | undefined = undefined, |
| 2669 | TStream extends boolean = boolean, |
| 2670 | const TTools extends TextActivityOptions< |
| 2671 | TAdapter, |
| 2672 | TSchema, |
| 2673 | TStream, |
| 2674 | any |
| 2675 | >['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], |
| 2676 | const TMiddleware extends TextActivityOptions< |
| 2677 | TAdapter, |
| 2678 | TSchema, |
| 2679 | TStream, |
| 2680 | any |
| 2681 | >['middleware'] = TextActivityOptions< |
| 2682 | TAdapter, |
| 2683 | TSchema, |
| 2684 | TStream, |
| 2685 | any |
| 2686 | >['middleware'], |
| 2687 | >( |
| 2688 | options: TextActivityOptionsWithContext< |
| 2689 | TAdapter, |
| 2690 | TSchema, |
| 2691 | TStream, |
| 2692 | TTools, |
| 2693 | TMiddleware |
| 2694 | >, |
| 2695 | ): TextActivityResult<TSchema, TStream> { |
| 2696 | validateCapabilities(options.middleware ?? [], options.adapter) |
| 2697 | |
| 2698 | const { outputSchema, stream } = options |
| 2699 | |
| 2700 | // outputSchema + stream:true is the only branch that streams structured |
| 2701 | // output. Without an explicit `stream: true`, schema-bearing calls run the |
| 2702 | // agent loop and resolve to a typed Promise<InferSchemaType<TSchema>>. |
| 2703 | if (outputSchema && stream === true) { |
| 2704 | return runStreamingStructuredOutput({ |
| 2705 | ...options, |
| 2706 | outputSchema, |
| 2707 | stream, |
| 2708 | }) as TextActivityResult<TSchema, TStream> |
| 2709 | } |
| 2710 | |
| 2711 | // If outputSchema is provided, run agentic structured output (Promise<T>) |
| 2712 | if (outputSchema) { |
| 2713 | return runAgenticStructuredOutput({ |
| 2714 | ...options, |
| 2715 | outputSchema, |
| 2716 | }) as TextActivityResult<TSchema, TStream> |
| 2717 | } |
| 2718 | |
| 2719 | // If stream is explicitly false, run non-streaming text |
| 2720 | if (stream === false) { |
| 2721 | return runNonStreamingText({ |
| 2722 | ...options, |
| 2723 | outputSchema: undefined, |