@tanstack/ai 0.2.2 → 0.3.0
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/README.md +1 -0
- package/dist/esm/activities/chat/index.js +206 -172
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/stream/index.d.ts +1 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +22 -43
- package/dist/esm/activities/chat/stream/processor.js +89 -156
- package/dist/esm/activities/chat/stream/processor.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +22 -21
- package/dist/esm/activities/chat/tools/tool-calls.js +40 -27
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/generateImage/index.js +41 -1
- package/dist/esm/activities/generateImage/index.js.map +1 -1
- package/dist/esm/activities/generateSpeech/index.js +33 -1
- package/dist/esm/activities/generateSpeech/index.js.map +1 -1
- package/dist/esm/activities/generateTranscription/index.js +29 -1
- package/dist/esm/activities/generateTranscription/index.js.map +1 -1
- package/dist/esm/activities/generateVideo/index.js +50 -0
- package/dist/esm/activities/generateVideo/index.js.map +1 -1
- package/dist/esm/activities/summarize/index.js +9 -8
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/event-client.d.ts +378 -254
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/stream-to-response.d.ts +1 -1
- package/dist/esm/stream-to-response.js +5 -3
- package/dist/esm/stream-to-response.js.map +1 -1
- package/dist/esm/types.d.ts +162 -46
- package/package.json +1 -1
- package/src/activities/chat/index.ts +267 -191
- package/src/activities/chat/stream/index.ts +1 -5
- package/src/activities/chat/stream/processor.ts +128 -269
- package/src/activities/chat/tools/tool-calls.ts +55 -49
- package/src/activities/generateImage/index.ts +47 -2
- package/src/activities/generateSpeech/index.ts +37 -1
- package/src/activities/generateTranscription/index.ts +33 -1
- package/src/activities/generateVideo/index.ts +54 -0
- package/src/activities/summarize/index.ts +10 -9
- package/src/event-client.ts +460 -256
- package/src/index.ts +2 -3
- package/src/stream-to-response.ts +8 -6
- package/src/types.ts +209 -65
package/src/index.ts
CHANGED
|
@@ -73,8 +73,8 @@ export {
|
|
|
73
73
|
// All types
|
|
74
74
|
export * from './types'
|
|
75
75
|
|
|
76
|
-
// Event client
|
|
77
|
-
export
|
|
76
|
+
// Event client + event types
|
|
77
|
+
export * from './event-client'
|
|
78
78
|
|
|
79
79
|
// Message converters
|
|
80
80
|
export {
|
|
@@ -106,7 +106,6 @@ export type {
|
|
|
106
106
|
ProcessorResult,
|
|
107
107
|
ProcessorState,
|
|
108
108
|
StreamProcessorEvents,
|
|
109
|
-
StreamProcessorHandlers,
|
|
110
109
|
StreamProcessorOptions,
|
|
111
110
|
ToolCallState,
|
|
112
111
|
ToolResultState,
|
|
@@ -3,7 +3,7 @@ import type { StreamChunk } from './types'
|
|
|
3
3
|
/**
|
|
4
4
|
* Collect all text content from a StreamChunk async iterable and return as a string.
|
|
5
5
|
*
|
|
6
|
-
* This function consumes the entire stream, accumulating content from
|
|
6
|
+
* This function consumes the entire stream, accumulating content from TEXT_MESSAGE_CONTENT events,
|
|
7
7
|
* and returns the final concatenated text.
|
|
8
8
|
*
|
|
9
9
|
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
@@ -26,7 +26,7 @@ export async function streamToText(
|
|
|
26
26
|
let accumulatedContent = ''
|
|
27
27
|
|
|
28
28
|
for await (const chunk of stream) {
|
|
29
|
-
if (chunk.type === '
|
|
29
|
+
if (chunk.type === 'TEXT_MESSAGE_CONTENT' && chunk.delta) {
|
|
30
30
|
accumulatedContent += chunk.delta
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -77,11 +77,12 @@ export function toServerSentEventsStream(
|
|
|
77
77
|
return
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
// Send error
|
|
80
|
+
// Send error event (AG-UI RUN_ERROR)
|
|
81
81
|
controller.enqueue(
|
|
82
82
|
encoder.encode(
|
|
83
83
|
`data: ${JSON.stringify({
|
|
84
|
-
type: '
|
|
84
|
+
type: 'RUN_ERROR',
|
|
85
|
+
timestamp: Date.now(),
|
|
85
86
|
error: {
|
|
86
87
|
message: error.message || 'Unknown error occurred',
|
|
87
88
|
code: error.code,
|
|
@@ -198,11 +199,12 @@ export function toHttpStream(
|
|
|
198
199
|
return
|
|
199
200
|
}
|
|
200
201
|
|
|
201
|
-
// Send error
|
|
202
|
+
// Send error event (AG-UI RUN_ERROR)
|
|
202
203
|
controller.enqueue(
|
|
203
204
|
encoder.encode(
|
|
204
205
|
`${JSON.stringify({
|
|
205
|
-
type: '
|
|
206
|
+
type: 'RUN_ERROR',
|
|
207
|
+
timestamp: Date.now(),
|
|
206
208
|
error: {
|
|
207
209
|
message: error.message || 'Unknown error occurred',
|
|
208
210
|
code: error.code,
|
package/src/types.ts
CHANGED
|
@@ -649,52 +649,75 @@ export interface TextOptions<
|
|
|
649
649
|
abortController?: AbortController
|
|
650
650
|
}
|
|
651
651
|
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
| 'tool_result'
|
|
656
|
-
| 'done'
|
|
657
|
-
| 'error'
|
|
658
|
-
| 'approval-requested'
|
|
659
|
-
| 'tool-input-available'
|
|
660
|
-
| 'thinking'
|
|
661
|
-
|
|
662
|
-
export interface BaseStreamChunk {
|
|
663
|
-
type: StreamChunkType
|
|
664
|
-
id: string
|
|
665
|
-
model: string
|
|
666
|
-
timestamp: number
|
|
667
|
-
}
|
|
652
|
+
// ============================================================================
|
|
653
|
+
// AG-UI Protocol Event Types
|
|
654
|
+
// ============================================================================
|
|
668
655
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
656
|
+
/**
|
|
657
|
+
* AG-UI Protocol event types.
|
|
658
|
+
* Based on the AG-UI specification for agent-user interaction.
|
|
659
|
+
* @see https://docs.ag-ui.com/concepts/events
|
|
660
|
+
*/
|
|
661
|
+
export type AGUIEventType =
|
|
662
|
+
| 'RUN_STARTED'
|
|
663
|
+
| 'RUN_FINISHED'
|
|
664
|
+
| 'RUN_ERROR'
|
|
665
|
+
| 'TEXT_MESSAGE_START'
|
|
666
|
+
| 'TEXT_MESSAGE_CONTENT'
|
|
667
|
+
| 'TEXT_MESSAGE_END'
|
|
668
|
+
| 'TOOL_CALL_START'
|
|
669
|
+
| 'TOOL_CALL_ARGS'
|
|
670
|
+
| 'TOOL_CALL_END'
|
|
671
|
+
| 'STEP_STARTED'
|
|
672
|
+
| 'STEP_FINISHED'
|
|
673
|
+
| 'STATE_SNAPSHOT'
|
|
674
|
+
| 'STATE_DELTA'
|
|
675
|
+
| 'CUSTOM'
|
|
675
676
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
677
|
+
/**
|
|
678
|
+
* Stream chunk/event types (AG-UI protocol).
|
|
679
|
+
*/
|
|
680
|
+
export type StreamChunkType = AGUIEventType
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* Base structure for AG-UI events.
|
|
684
|
+
* Extends AG-UI spec with TanStack AI additions (model field).
|
|
685
|
+
*/
|
|
686
|
+
export interface BaseAGUIEvent {
|
|
687
|
+
type: AGUIEventType
|
|
688
|
+
timestamp: number
|
|
689
|
+
/** Model identifier for multi-model support */
|
|
690
|
+
model?: string
|
|
691
|
+
/** Original provider event for debugging/advanced use cases */
|
|
692
|
+
rawEvent?: unknown
|
|
687
693
|
}
|
|
688
694
|
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
695
|
+
// ============================================================================
|
|
696
|
+
// AG-UI Event Interfaces
|
|
697
|
+
// ============================================================================
|
|
698
|
+
|
|
699
|
+
/**
|
|
700
|
+
* Emitted when a run starts.
|
|
701
|
+
* This is the first event in any streaming response.
|
|
702
|
+
*/
|
|
703
|
+
export interface RunStartedEvent extends BaseAGUIEvent {
|
|
704
|
+
type: 'RUN_STARTED'
|
|
705
|
+
/** Unique identifier for this run */
|
|
706
|
+
runId: string
|
|
707
|
+
/** Optional thread/conversation ID */
|
|
708
|
+
threadId?: string
|
|
693
709
|
}
|
|
694
710
|
|
|
695
|
-
|
|
696
|
-
|
|
711
|
+
/**
|
|
712
|
+
* Emitted when a run completes successfully.
|
|
713
|
+
*/
|
|
714
|
+
export interface RunFinishedEvent extends BaseAGUIEvent {
|
|
715
|
+
type: 'RUN_FINISHED'
|
|
716
|
+
/** Run identifier */
|
|
717
|
+
runId: string
|
|
718
|
+
/** Why the generation stopped */
|
|
697
719
|
finishReason: 'stop' | 'length' | 'content_filter' | 'tool_calls' | null
|
|
720
|
+
/** Token usage statistics */
|
|
698
721
|
usage?: {
|
|
699
722
|
promptTokens: number
|
|
700
723
|
completionTokens: number
|
|
@@ -702,50 +725,171 @@ export interface DoneStreamChunk extends BaseStreamChunk {
|
|
|
702
725
|
}
|
|
703
726
|
}
|
|
704
727
|
|
|
705
|
-
|
|
706
|
-
|
|
728
|
+
/**
|
|
729
|
+
* Emitted when an error occurs during a run.
|
|
730
|
+
*/
|
|
731
|
+
export interface RunErrorEvent extends BaseAGUIEvent {
|
|
732
|
+
type: 'RUN_ERROR'
|
|
733
|
+
/** Run identifier (if available) */
|
|
734
|
+
runId?: string
|
|
735
|
+
/** Error details */
|
|
707
736
|
error: {
|
|
708
737
|
message: string
|
|
709
738
|
code?: string
|
|
710
739
|
}
|
|
711
740
|
}
|
|
712
741
|
|
|
713
|
-
|
|
714
|
-
|
|
742
|
+
/**
|
|
743
|
+
* Emitted when a text message starts.
|
|
744
|
+
*/
|
|
745
|
+
export interface TextMessageStartEvent extends BaseAGUIEvent {
|
|
746
|
+
type: 'TEXT_MESSAGE_START'
|
|
747
|
+
/** Unique identifier for this message */
|
|
748
|
+
messageId: string
|
|
749
|
+
/** Role is always assistant for generated messages */
|
|
750
|
+
role: 'assistant'
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Emitted when text content is generated (streaming tokens).
|
|
755
|
+
*/
|
|
756
|
+
export interface TextMessageContentEvent extends BaseAGUIEvent {
|
|
757
|
+
type: 'TEXT_MESSAGE_CONTENT'
|
|
758
|
+
/** Message identifier */
|
|
759
|
+
messageId: string
|
|
760
|
+
/** The incremental content token */
|
|
761
|
+
delta: string
|
|
762
|
+
/** Full accumulated content so far */
|
|
763
|
+
content?: string
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Emitted when a text message completes.
|
|
768
|
+
*/
|
|
769
|
+
export interface TextMessageEndEvent extends BaseAGUIEvent {
|
|
770
|
+
type: 'TEXT_MESSAGE_END'
|
|
771
|
+
/** Message identifier */
|
|
772
|
+
messageId: string
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Emitted when a tool call starts.
|
|
777
|
+
*/
|
|
778
|
+
export interface ToolCallStartEvent extends BaseAGUIEvent {
|
|
779
|
+
type: 'TOOL_CALL_START'
|
|
780
|
+
/** Unique identifier for this tool call */
|
|
715
781
|
toolCallId: string
|
|
782
|
+
/** Name of the tool being called */
|
|
716
783
|
toolName: string
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
784
|
+
/** Index for parallel tool calls */
|
|
785
|
+
index?: number
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* Emitted when tool call arguments are streaming.
|
|
790
|
+
*/
|
|
791
|
+
export interface ToolCallArgsEvent extends BaseAGUIEvent {
|
|
792
|
+
type: 'TOOL_CALL_ARGS'
|
|
793
|
+
/** Tool call identifier */
|
|
794
|
+
toolCallId: string
|
|
795
|
+
/** Incremental JSON arguments delta */
|
|
796
|
+
delta: string
|
|
797
|
+
/** Full accumulated arguments so far */
|
|
798
|
+
args?: string
|
|
722
799
|
}
|
|
723
800
|
|
|
724
|
-
|
|
725
|
-
|
|
801
|
+
/**
|
|
802
|
+
* Emitted when a tool call completes.
|
|
803
|
+
*/
|
|
804
|
+
export interface ToolCallEndEvent extends BaseAGUIEvent {
|
|
805
|
+
type: 'TOOL_CALL_END'
|
|
806
|
+
/** Tool call identifier */
|
|
726
807
|
toolCallId: string
|
|
808
|
+
/** Name of the tool */
|
|
727
809
|
toolName: string
|
|
728
|
-
input
|
|
810
|
+
/** Final parsed input arguments */
|
|
811
|
+
input?: unknown
|
|
812
|
+
/** Tool execution result (if executed) */
|
|
813
|
+
result?: string
|
|
729
814
|
}
|
|
730
815
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
816
|
+
/**
|
|
817
|
+
* Emitted when a thinking/reasoning step starts.
|
|
818
|
+
*/
|
|
819
|
+
export interface StepStartedEvent extends BaseAGUIEvent {
|
|
820
|
+
type: 'STEP_STARTED'
|
|
821
|
+
/** Unique identifier for this step */
|
|
822
|
+
stepId: string
|
|
823
|
+
/** Type of step (e.g., 'thinking', 'planning') */
|
|
824
|
+
stepType?: string
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Emitted when a thinking/reasoning step finishes.
|
|
829
|
+
*/
|
|
830
|
+
export interface StepFinishedEvent extends BaseAGUIEvent {
|
|
831
|
+
type: 'STEP_FINISHED'
|
|
832
|
+
/** Step identifier */
|
|
833
|
+
stepId: string
|
|
834
|
+
/** Incremental thinking content */
|
|
835
|
+
delta?: string
|
|
836
|
+
/** Full accumulated thinking content */
|
|
837
|
+
content?: string
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Emitted to provide a full state snapshot.
|
|
842
|
+
*/
|
|
843
|
+
export interface StateSnapshotEvent extends BaseAGUIEvent {
|
|
844
|
+
type: 'STATE_SNAPSHOT'
|
|
845
|
+
/** The complete state object */
|
|
846
|
+
state: Record<string, unknown>
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Emitted to provide an incremental state update.
|
|
851
|
+
*/
|
|
852
|
+
export interface StateDeltaEvent extends BaseAGUIEvent {
|
|
853
|
+
type: 'STATE_DELTA'
|
|
854
|
+
/** The state changes to apply */
|
|
855
|
+
delta: Record<string, unknown>
|
|
735
856
|
}
|
|
736
857
|
|
|
737
858
|
/**
|
|
738
|
-
*
|
|
859
|
+
* Custom event for extensibility.
|
|
860
|
+
*/
|
|
861
|
+
export interface CustomEvent extends BaseAGUIEvent {
|
|
862
|
+
type: 'CUSTOM'
|
|
863
|
+
/** Custom event name */
|
|
864
|
+
name: string
|
|
865
|
+
/** Custom event data */
|
|
866
|
+
data?: unknown
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
/**
|
|
870
|
+
* Union of all AG-UI events.
|
|
871
|
+
*/
|
|
872
|
+
export type AGUIEvent =
|
|
873
|
+
| RunStartedEvent
|
|
874
|
+
| RunFinishedEvent
|
|
875
|
+
| RunErrorEvent
|
|
876
|
+
| TextMessageStartEvent
|
|
877
|
+
| TextMessageContentEvent
|
|
878
|
+
| TextMessageEndEvent
|
|
879
|
+
| ToolCallStartEvent
|
|
880
|
+
| ToolCallArgsEvent
|
|
881
|
+
| ToolCallEndEvent
|
|
882
|
+
| StepStartedEvent
|
|
883
|
+
| StepFinishedEvent
|
|
884
|
+
| StateSnapshotEvent
|
|
885
|
+
| StateDeltaEvent
|
|
886
|
+
| CustomEvent
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Chunk returned by the SDK during streaming chat completions.
|
|
890
|
+
* Uses the AG-UI protocol event format.
|
|
739
891
|
*/
|
|
740
|
-
export type StreamChunk =
|
|
741
|
-
| ContentStreamChunk
|
|
742
|
-
| ToolCallStreamChunk
|
|
743
|
-
| ToolResultStreamChunk
|
|
744
|
-
| DoneStreamChunk
|
|
745
|
-
| ErrorStreamChunk
|
|
746
|
-
| ApprovalRequestedStreamChunk
|
|
747
|
-
| ToolInputAvailableStreamChunk
|
|
748
|
-
| ThinkingStreamChunk
|
|
892
|
+
export type StreamChunk = AGUIEvent
|
|
749
893
|
|
|
750
894
|
// Simple streaming format for basic text completions
|
|
751
895
|
// Converted to StreamChunk format by convertTextCompletionStream()
|