@tanstack/ai 0.0.3 → 0.2.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.
Files changed (146) hide show
  1. package/README.md +26 -0
  2. package/dist/esm/activities/chat/adapter.d.ts +100 -0
  3. package/dist/esm/activities/chat/adapter.js +14 -0
  4. package/dist/esm/activities/chat/adapter.js.map +1 -0
  5. package/dist/esm/{utilities → activities/chat}/agent-loop-strategies.d.ts +4 -4
  6. package/dist/esm/activities/chat/agent-loop-strategies.js.map +1 -0
  7. package/dist/esm/activities/chat/index.d.ts +166 -0
  8. package/dist/esm/{core/chat.js → activities/chat/index.js} +131 -33
  9. package/dist/esm/activities/chat/index.js.map +1 -0
  10. package/dist/esm/{message-converters.d.ts → activities/chat/messages.d.ts} +1 -1
  11. package/dist/esm/{message-converters.js → activities/chat/messages.js} +7 -7
  12. package/dist/esm/activities/chat/messages.js.map +1 -0
  13. package/dist/esm/activities/chat/stream/json-parser.js.map +1 -0
  14. package/dist/esm/{stream → activities/chat/stream}/message-updaters.d.ts +1 -1
  15. package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -0
  16. package/dist/esm/{stream → activities/chat/stream}/processor.d.ts +1 -1
  17. package/dist/esm/{stream → activities/chat/stream}/processor.js +1 -1
  18. package/dist/esm/activities/chat/stream/processor.js.map +1 -0
  19. package/dist/esm/activities/chat/stream/strategies.js.map +1 -0
  20. package/dist/esm/{stream → activities/chat/stream}/types.d.ts +2 -9
  21. package/dist/esm/activities/chat/tools/schema-converter.d.ts +116 -0
  22. package/dist/esm/activities/chat/tools/schema-converter.js +115 -0
  23. package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
  24. package/dist/esm/{tools → activities/chat/tools}/tool-calls.d.ts +1 -1
  25. package/dist/esm/{tools → activities/chat/tools}/tool-calls.js +23 -30
  26. package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -0
  27. package/dist/esm/{tools → activities/chat/tools}/tool-definition.d.ts +22 -18
  28. package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -0
  29. package/dist/esm/activities/generateImage/adapter.d.ts +68 -0
  30. package/dist/esm/activities/generateImage/adapter.js +14 -0
  31. package/dist/esm/activities/generateImage/adapter.js.map +1 -0
  32. package/dist/esm/activities/generateImage/index.d.ts +89 -0
  33. package/dist/esm/activities/generateImage/index.js +15 -0
  34. package/dist/esm/activities/generateImage/index.js.map +1 -0
  35. package/dist/esm/activities/generateSpeech/adapter.d.ts +62 -0
  36. package/dist/esm/activities/generateSpeech/adapter.js +14 -0
  37. package/dist/esm/activities/generateSpeech/adapter.js.map +1 -0
  38. package/dist/esm/activities/generateSpeech/index.d.ts +69 -0
  39. package/dist/esm/activities/generateSpeech/index.js +15 -0
  40. package/dist/esm/activities/generateSpeech/index.js.map +1 -0
  41. package/dist/esm/activities/generateTranscription/adapter.d.ts +62 -0
  42. package/dist/esm/activities/generateTranscription/adapter.js +14 -0
  43. package/dist/esm/activities/generateTranscription/adapter.js.map +1 -0
  44. package/dist/esm/activities/generateTranscription/index.d.ts +71 -0
  45. package/dist/esm/activities/generateTranscription/index.js +15 -0
  46. package/dist/esm/activities/generateTranscription/index.js.map +1 -0
  47. package/dist/esm/activities/generateVideo/adapter.d.ts +80 -0
  48. package/dist/esm/activities/generateVideo/adapter.js +14 -0
  49. package/dist/esm/activities/generateVideo/adapter.js.map +1 -0
  50. package/dist/esm/activities/generateVideo/index.d.ts +136 -0
  51. package/dist/esm/activities/generateVideo/index.js +47 -0
  52. package/dist/esm/activities/generateVideo/index.js.map +1 -0
  53. package/dist/esm/activities/index.d.ts +22 -0
  54. package/dist/esm/activities/index.js +34 -0
  55. package/dist/esm/activities/index.js.map +1 -0
  56. package/dist/esm/activities/summarize/adapter.d.ts +74 -0
  57. package/dist/esm/activities/summarize/adapter.js +14 -0
  58. package/dist/esm/activities/summarize/adapter.js.map +1 -0
  59. package/dist/esm/activities/summarize/index.d.ts +100 -0
  60. package/dist/esm/activities/summarize/index.js +90 -0
  61. package/dist/esm/activities/summarize/index.js.map +1 -0
  62. package/dist/esm/event-client.d.ts +4 -18
  63. package/dist/esm/event-client.js.map +1 -1
  64. package/dist/esm/index.d.ts +16 -14
  65. package/dist/esm/index.js +29 -20
  66. package/dist/esm/stream-to-response.d.ts +95 -0
  67. package/dist/esm/stream-to-response.js +118 -0
  68. package/dist/esm/stream-to-response.js.map +1 -0
  69. package/dist/esm/types.d.ts +370 -133
  70. package/package.json +7 -6
  71. package/src/activities/chat/adapter.ts +150 -0
  72. package/src/{utilities → activities/chat}/agent-loop-strategies.ts +4 -4
  73. package/src/{core/chat.ts → activities/chat/index.ts} +435 -79
  74. package/src/{message-converters.ts → activities/chat/messages.ts} +10 -13
  75. package/src/{stream → activities/chat/stream}/message-updaters.ts +1 -1
  76. package/src/{stream → activities/chat/stream}/processor.ts +2 -5
  77. package/src/{stream → activities/chat/stream}/types.ts +8 -18
  78. package/src/activities/chat/tools/schema-converter.ts +332 -0
  79. package/src/{tools → activities/chat/tools}/tool-calls.ts +63 -44
  80. package/src/{tools → activities/chat/tools}/tool-definition.ts +51 -38
  81. package/src/activities/generateImage/adapter.ts +104 -0
  82. package/src/activities/generateImage/index.ts +162 -0
  83. package/src/activities/generateSpeech/adapter.ts +87 -0
  84. package/src/activities/generateSpeech/index.ts +122 -0
  85. package/src/activities/generateTranscription/adapter.ts +89 -0
  86. package/src/activities/generateTranscription/index.ts +132 -0
  87. package/src/activities/generateVideo/adapter.ts +116 -0
  88. package/src/activities/generateVideo/index.ts +261 -0
  89. package/src/activities/index.ts +164 -0
  90. package/src/activities/summarize/adapter.ts +107 -0
  91. package/src/activities/summarize/index.ts +287 -0
  92. package/src/event-client.ts +5 -21
  93. package/src/index.ts +60 -15
  94. package/src/stream-to-response.ts +237 -0
  95. package/src/types.ts +429 -284
  96. package/dist/esm/base-adapter.d.ts +0 -36
  97. package/dist/esm/base-adapter.js +0 -12
  98. package/dist/esm/base-adapter.js.map +0 -1
  99. package/dist/esm/core/chat-common-options.d.ts +0 -52
  100. package/dist/esm/core/chat.d.ts +0 -30
  101. package/dist/esm/core/chat.js.map +0 -1
  102. package/dist/esm/core/embedding.d.ts +0 -8
  103. package/dist/esm/core/embedding.js +0 -33
  104. package/dist/esm/core/embedding.js.map +0 -1
  105. package/dist/esm/core/summarize.d.ts +0 -9
  106. package/dist/esm/core/summarize.js +0 -36
  107. package/dist/esm/core/summarize.js.map +0 -1
  108. package/dist/esm/message-converters.js.map +0 -1
  109. package/dist/esm/stream/json-parser.js.map +0 -1
  110. package/dist/esm/stream/message-updaters.js.map +0 -1
  111. package/dist/esm/stream/processor.js.map +0 -1
  112. package/dist/esm/stream/strategies.js.map +0 -1
  113. package/dist/esm/tools/tool-calls.js.map +0 -1
  114. package/dist/esm/tools/tool-definition.js.map +0 -1
  115. package/dist/esm/tools/zod-converter.d.ts +0 -30
  116. package/dist/esm/tools/zod-converter.js +0 -36
  117. package/dist/esm/tools/zod-converter.js.map +0 -1
  118. package/dist/esm/utilities/agent-loop-strategies.js.map +0 -1
  119. package/dist/esm/utilities/chat-options.d.ts +0 -6
  120. package/dist/esm/utilities/chat-options.js +0 -7
  121. package/dist/esm/utilities/chat-options.js.map +0 -1
  122. package/dist/esm/utilities/messages.d.ts +0 -30
  123. package/dist/esm/utilities/messages.js +0 -7
  124. package/dist/esm/utilities/messages.js.map +0 -1
  125. package/dist/esm/utilities/stream-to-response.d.ts +0 -48
  126. package/dist/esm/utilities/stream-to-response.js +0 -62
  127. package/dist/esm/utilities/stream-to-response.js.map +0 -1
  128. package/src/base-adapter.ts +0 -86
  129. package/src/core/chat-common-options.ts +0 -55
  130. package/src/core/embedding.ts +0 -54
  131. package/src/core/summarize.ts +0 -56
  132. package/src/tools/zod-converter.ts +0 -85
  133. package/src/utilities/chat-options.ts +0 -35
  134. package/src/utilities/messages.ts +0 -63
  135. package/src/utilities/stream-to-response.ts +0 -116
  136. /package/dist/esm/{utilities → activities/chat}/agent-loop-strategies.js +0 -0
  137. /package/dist/esm/{stream → activities/chat/stream}/index.d.ts +0 -0
  138. /package/dist/esm/{stream → activities/chat/stream}/json-parser.d.ts +0 -0
  139. /package/dist/esm/{stream → activities/chat/stream}/json-parser.js +0 -0
  140. /package/dist/esm/{stream → activities/chat/stream}/message-updaters.js +0 -0
  141. /package/dist/esm/{stream → activities/chat/stream}/strategies.d.ts +0 -0
  142. /package/dist/esm/{stream → activities/chat/stream}/strategies.js +0 -0
  143. /package/dist/esm/{tools → activities/chat/tools}/tool-definition.js +0 -0
  144. /package/src/{stream → activities/chat/stream}/index.ts +0 -0
  145. /package/src/{stream → activities/chat/stream}/json-parser.ts +0 -0
  146. /package/src/{stream → activities/chat/stream}/strategies.ts +0 -0
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Activities Index
3
+ *
4
+ * Central hub for all AI activities. This module exports:
5
+ * - All activity implementations and their types
6
+ * - All adapter interfaces and base classes
7
+ * - Unified type definitions
8
+ *
9
+ * To add a new activity:
10
+ * 1. Create a new directory under activities/ with index.ts and adapter.ts
11
+ * 2. Export the activity and adapter from this file
12
+ */
13
+
14
+ // Import the activity functions
15
+
16
+ // Import adapter types for type definitions
17
+ import type { AnyTextAdapter } from './chat/adapter'
18
+ import type { AnySummarizeAdapter } from './summarize/adapter'
19
+ import type { AnyImageAdapter } from './generateImage/adapter'
20
+ import type { AnyVideoAdapter } from './generateVideo/adapter'
21
+ import type { AnyTTSAdapter } from './generateSpeech/adapter'
22
+ import type { AnyTranscriptionAdapter } from './generateTranscription/adapter'
23
+
24
+ // ===========================
25
+ // Chat Activity
26
+ // ===========================
27
+
28
+ export {
29
+ kind as textKind,
30
+ chat,
31
+ type TextActivityOptions,
32
+ type TextActivityResult,
33
+ } from './chat/index'
34
+
35
+ export {
36
+ BaseTextAdapter,
37
+ type AnyTextAdapter,
38
+ type TextAdapter,
39
+ type TextAdapterConfig,
40
+ type StructuredOutputOptions,
41
+ type StructuredOutputResult,
42
+ } from './chat/adapter'
43
+
44
+ // ===========================
45
+ // Summarize Activity
46
+ // ===========================
47
+
48
+ export {
49
+ kind as summarizeKind,
50
+ summarize,
51
+ type SummarizeActivityOptions,
52
+ type SummarizeActivityResult,
53
+ type SummarizeProviderOptions,
54
+ } from './summarize/index'
55
+
56
+ export {
57
+ BaseSummarizeAdapter,
58
+ type SummarizeAdapter,
59
+ type SummarizeAdapterConfig,
60
+ type AnySummarizeAdapter,
61
+ } from './summarize/adapter'
62
+
63
+ // ===========================
64
+ // Image Activity
65
+ // ===========================
66
+
67
+ export {
68
+ kind as imageKind,
69
+ generateImage,
70
+ type ImageActivityOptions,
71
+ type ImageActivityResult,
72
+ type ImageProviderOptionsForModel,
73
+ type ImageSizeForModel,
74
+ } from './generateImage/index'
75
+
76
+ export {
77
+ BaseImageAdapter,
78
+ type ImageAdapter,
79
+ type ImageAdapterConfig,
80
+ type AnyImageAdapter,
81
+ } from './generateImage/adapter'
82
+
83
+ // ===========================
84
+ // Video Activity (Experimental)
85
+ // ===========================
86
+
87
+ export {
88
+ kind as videoKind,
89
+ generateVideo,
90
+ getVideoJobStatus,
91
+ type VideoActivityOptions,
92
+ type VideoActivityResult,
93
+ type VideoProviderOptions,
94
+ type VideoCreateOptions,
95
+ type VideoStatusOptions,
96
+ type VideoUrlOptions,
97
+ } from './generateVideo/index'
98
+
99
+ export {
100
+ BaseVideoAdapter,
101
+ type VideoAdapter,
102
+ type VideoAdapterConfig,
103
+ type AnyVideoAdapter,
104
+ } from './generateVideo/adapter'
105
+
106
+ // ===========================
107
+ // TTS Activity
108
+ // ===========================
109
+
110
+ export {
111
+ kind as ttsKind,
112
+ generateSpeech,
113
+ type TTSActivityOptions,
114
+ type TTSActivityResult,
115
+ type TTSProviderOptions,
116
+ } from './generateSpeech/index'
117
+
118
+ export {
119
+ BaseTTSAdapter,
120
+ type TTSAdapter,
121
+ type TTSAdapterConfig,
122
+ type AnyTTSAdapter,
123
+ } from './generateSpeech/adapter'
124
+
125
+ // ===========================
126
+ // Transcription Activity
127
+ // ===========================
128
+
129
+ export {
130
+ kind as transcriptionKind,
131
+ generateTranscription,
132
+ type TranscriptionActivityOptions,
133
+ type TranscriptionActivityResult,
134
+ type TranscriptionProviderOptions,
135
+ } from './generateTranscription/index'
136
+
137
+ export {
138
+ BaseTranscriptionAdapter,
139
+ type TranscriptionAdapter,
140
+ type TranscriptionAdapterConfig,
141
+ type AnyTranscriptionAdapter,
142
+ } from './generateTranscription/adapter'
143
+
144
+ // ===========================
145
+ // Adapter Union Types
146
+ // ===========================
147
+
148
+ /** Union of all adapter types that can be passed to chat() */
149
+ export type AIAdapter =
150
+ | AnyTextAdapter
151
+ | AnySummarizeAdapter
152
+ | AnyImageAdapter
153
+ | AnyVideoAdapter
154
+ | AnyTTSAdapter
155
+ | AnyTranscriptionAdapter
156
+
157
+ /** Union type of all adapter kinds */
158
+ export type AdapterKind =
159
+ | 'text'
160
+ | 'summarize'
161
+ | 'image'
162
+ | 'video'
163
+ | 'tts'
164
+ | 'transcription'
@@ -0,0 +1,107 @@
1
+ import type {
2
+ StreamChunk,
3
+ SummarizationOptions,
4
+ SummarizationResult,
5
+ } from '../../types'
6
+
7
+ /**
8
+ * Configuration for summarize adapter instances
9
+ */
10
+ export interface SummarizeAdapterConfig {
11
+ apiKey?: string
12
+ baseUrl?: string
13
+ timeout?: number
14
+ maxRetries?: number
15
+ headers?: Record<string, string>
16
+ }
17
+
18
+ /**
19
+ * Summarize adapter interface with pre-resolved generics.
20
+ *
21
+ * An adapter is created by a provider function: `provider('model')` → `adapter`
22
+ * All type resolution happens at the provider call site, not in this interface.
23
+ *
24
+ * Generic parameters:
25
+ * - TModel: The specific model name (e.g., 'gpt-4o')
26
+ * - TProviderOptions: Provider-specific options (already resolved)
27
+ */
28
+ export interface SummarizeAdapter<
29
+ TModel extends string = string,
30
+ TProviderOptions extends object = Record<string, unknown>,
31
+ > {
32
+ /** Discriminator for adapter kind - used by generate() to determine API shape */
33
+ readonly kind: 'summarize'
34
+ /** Adapter name identifier */
35
+ readonly name: string
36
+ /** The model this adapter is configured for */
37
+ readonly model: TModel
38
+
39
+ /**
40
+ * @internal Type-only properties for inference. Not assigned at runtime.
41
+ */
42
+ '~types': {
43
+ providerOptions: TProviderOptions
44
+ }
45
+
46
+ /**
47
+ * Summarize the given text
48
+ */
49
+ summarize: (options: SummarizationOptions) => Promise<SummarizationResult>
50
+
51
+ /**
52
+ * Stream summarization of the given text.
53
+ * Optional - if not implemented, the activity layer will fall back to
54
+ * non-streaming summarize and yield the result as a single chunk.
55
+ */
56
+ summarizeStream?: (
57
+ options: SummarizationOptions,
58
+ ) => AsyncIterable<StreamChunk>
59
+ }
60
+
61
+ /**
62
+ * A SummarizeAdapter with any/unknown type parameters.
63
+ * Useful as a constraint in generic functions and interfaces.
64
+ */
65
+ export type AnySummarizeAdapter = SummarizeAdapter<any, any>
66
+
67
+ /**
68
+ * Abstract base class for summarize adapters.
69
+ * Extend this class to implement a summarize adapter for a specific provider.
70
+ *
71
+ * Generic parameters match SummarizeAdapter - all pre-resolved by the provider function.
72
+ */
73
+ export abstract class BaseSummarizeAdapter<
74
+ TModel extends string = string,
75
+ TProviderOptions extends object = Record<string, unknown>,
76
+ > implements SummarizeAdapter<TModel, TProviderOptions> {
77
+ readonly kind = 'summarize' as const
78
+ abstract readonly name: string
79
+ readonly model: TModel
80
+
81
+ // Type-only property - never assigned at runtime
82
+ declare '~types': {
83
+ providerOptions: TProviderOptions
84
+ }
85
+
86
+ protected config: SummarizeAdapterConfig
87
+
88
+ constructor(config: SummarizeAdapterConfig = {}, model: TModel) {
89
+ this.config = config
90
+ this.model = model
91
+ }
92
+
93
+ abstract summarize(
94
+ options: SummarizationOptions,
95
+ ): Promise<SummarizationResult>
96
+
97
+ /**
98
+ * Stream summarization of the given text.
99
+ * Override this method in concrete implementations to enable streaming.
100
+ * If not overridden, the activity layer will fall back to non-streaming.
101
+ */
102
+ summarizeStream?(options: SummarizationOptions): AsyncIterable<StreamChunk>
103
+
104
+ protected generateId(): string {
105
+ return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
106
+ }
107
+ }
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Summarize Activity
3
+ *
4
+ * Generates summaries from text input.
5
+ * This is a self-contained module with implementation, types, and JSDoc.
6
+ */
7
+
8
+ import { aiEventClient } from '../../event-client.js'
9
+ import type { SummarizeAdapter } from './adapter'
10
+ import type {
11
+ StreamChunk,
12
+ SummarizationOptions,
13
+ SummarizationResult,
14
+ } from '../../types'
15
+
16
+ // ===========================
17
+ // Activity Kind
18
+ // ===========================
19
+
20
+ /** The adapter kind this activity handles */
21
+ export const kind = 'summarize' as const
22
+
23
+ // ===========================
24
+ // Type Extraction Helpers
25
+ // ===========================
26
+
27
+ /** Extract provider options from a SummarizeAdapter via ~types */
28
+ export type SummarizeProviderOptions<TAdapter> =
29
+ TAdapter extends SummarizeAdapter<any, any>
30
+ ? TAdapter['~types']['providerOptions']
31
+ : object
32
+
33
+ // ===========================
34
+ // Activity Options Type
35
+ // ===========================
36
+
37
+ /**
38
+ * Options for the summarize activity.
39
+ * The model is extracted from the adapter's model property.
40
+ *
41
+ * @template TAdapter - The summarize adapter type
42
+ * @template TStream - Whether to stream the output
43
+ */
44
+ export interface SummarizeActivityOptions<
45
+ TAdapter extends SummarizeAdapter<string, object>,
46
+ TStream extends boolean = false,
47
+ > {
48
+ /** The summarize adapter to use (must be created with a model) */
49
+ adapter: TAdapter & { kind: typeof kind }
50
+ /** The text to summarize */
51
+ text: string
52
+ /** Maximum length of the summary (in words or characters, provider-dependent) */
53
+ maxLength?: number
54
+ /** Style of summary to generate */
55
+ style?: 'bullet-points' | 'paragraph' | 'concise'
56
+ /** Topics or aspects to focus on in the summary */
57
+ focus?: Array<string>
58
+ /** Provider-specific options */
59
+ modelOptions?: SummarizeProviderOptions<TAdapter>
60
+ /**
61
+ * Whether to stream the summarization result.
62
+ * When true, returns an AsyncIterable<StreamChunk> for streaming output.
63
+ * When false or not provided, returns a Promise<SummarizationResult>.
64
+ *
65
+ * @default false
66
+ */
67
+ stream?: TStream
68
+ }
69
+
70
+ // ===========================
71
+ // Activity Result Type
72
+ // ===========================
73
+
74
+ /**
75
+ * Result type for the summarize activity.
76
+ * - If stream is true: AsyncIterable<StreamChunk>
77
+ * - Otherwise: Promise<SummarizationResult>
78
+ */
79
+ export type SummarizeActivityResult<TStream extends boolean> =
80
+ TStream extends true
81
+ ? AsyncIterable<StreamChunk>
82
+ : Promise<SummarizationResult>
83
+
84
+ // ===========================
85
+ // Helper Functions
86
+ // ===========================
87
+
88
+ function createId(prefix: string): string {
89
+ return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`
90
+ }
91
+
92
+ // ===========================
93
+ // Activity Implementation
94
+ // ===========================
95
+
96
+ /**
97
+ * Summarize activity - generates summaries from text.
98
+ *
99
+ * Supports both streaming and non-streaming modes.
100
+ *
101
+ * @example Basic summarization
102
+ * ```ts
103
+ * import { summarize } from '@tanstack/ai'
104
+ * import { openaiSummarize } from '@tanstack/ai-openai'
105
+ *
106
+ * const result = await summarize({
107
+ * adapter: openaiSummarize('gpt-4o-mini'),
108
+ * text: 'Long article text here...'
109
+ * })
110
+ *
111
+ * console.log(result.summary)
112
+ * ```
113
+ *
114
+ * @example Summarization with style
115
+ * ```ts
116
+ * const result = await summarize({
117
+ * adapter: openaiSummarize('gpt-4o-mini'),
118
+ * text: 'Long article text here...',
119
+ * style: 'bullet-points',
120
+ * maxLength: 100
121
+ * })
122
+ * ```
123
+ *
124
+ * @example Focused summarization
125
+ * ```ts
126
+ * const result = await summarize({
127
+ * adapter: openaiSummarize('gpt-4o-mini'),
128
+ * text: 'Long technical document...',
129
+ * focus: ['key findings', 'methodology']
130
+ * })
131
+ * ```
132
+ *
133
+ * @example Streaming summarization
134
+ * ```ts
135
+ * for await (const chunk of summarize({
136
+ * adapter: openaiSummarize('gpt-4o-mini'),
137
+ * text: 'Long article text here...',
138
+ * stream: true
139
+ * })) {
140
+ * if (chunk.type === 'content') {
141
+ * process.stdout.write(chunk.delta)
142
+ * }
143
+ * }
144
+ * ```
145
+ */
146
+ export function summarize<
147
+ TAdapter extends SummarizeAdapter<string, object>,
148
+ TStream extends boolean = false,
149
+ >(
150
+ options: SummarizeActivityOptions<TAdapter, TStream>,
151
+ ): SummarizeActivityResult<TStream> {
152
+ const { stream } = options
153
+
154
+ if (stream) {
155
+ return runStreamingSummarize(
156
+ options as unknown as SummarizeActivityOptions<
157
+ SummarizeAdapter<string, object>,
158
+ true
159
+ >,
160
+ ) as SummarizeActivityResult<TStream>
161
+ }
162
+
163
+ return runSummarize(
164
+ options as unknown as SummarizeActivityOptions<
165
+ SummarizeAdapter<string, object>,
166
+ false
167
+ >,
168
+ ) as SummarizeActivityResult<TStream>
169
+ }
170
+
171
+ /**
172
+ * Run non-streaming summarization
173
+ */
174
+ async function runSummarize(
175
+ options: SummarizeActivityOptions<SummarizeAdapter<string, object>, false>,
176
+ ): Promise<SummarizationResult> {
177
+ const { adapter, text, maxLength, style, focus } = options
178
+ const model = adapter.model
179
+ const requestId = createId('summarize')
180
+ const inputLength = text.length
181
+ const startTime = Date.now()
182
+
183
+ aiEventClient.emit('summarize:started', {
184
+ requestId,
185
+ model,
186
+ inputLength,
187
+ timestamp: startTime,
188
+ })
189
+
190
+ const summarizeOptions: SummarizationOptions = {
191
+ model,
192
+ text,
193
+ maxLength,
194
+ style,
195
+ focus,
196
+ }
197
+
198
+ const result = await adapter.summarize(summarizeOptions)
199
+
200
+ const duration = Date.now() - startTime
201
+ const outputLength = result.summary.length
202
+
203
+ aiEventClient.emit('summarize:completed', {
204
+ requestId,
205
+ model,
206
+ inputLength,
207
+ outputLength,
208
+ duration,
209
+ timestamp: Date.now(),
210
+ })
211
+
212
+ return result
213
+ }
214
+
215
+ /**
216
+ * Run streaming summarization
217
+ * Uses the adapter's native streaming if available, otherwise falls back
218
+ * to non-streaming and yields the result as a single chunk.
219
+ */
220
+ async function* runStreamingSummarize(
221
+ options: SummarizeActivityOptions<SummarizeAdapter<string, object>, true>,
222
+ ): AsyncIterable<StreamChunk> {
223
+ const { adapter, text, maxLength, style, focus } = options
224
+ const model = adapter.model
225
+
226
+ const summarizeOptions: SummarizationOptions = {
227
+ model,
228
+ text,
229
+ maxLength,
230
+ style,
231
+ focus,
232
+ }
233
+
234
+ // Use real streaming if the adapter supports it
235
+ if (adapter.summarizeStream) {
236
+ yield* adapter.summarizeStream(summarizeOptions)
237
+ return
238
+ }
239
+
240
+ // Fall back to non-streaming and yield as a single chunk
241
+ const result = await adapter.summarize(summarizeOptions)
242
+
243
+ // Yield content chunk with the summary
244
+ yield {
245
+ type: 'content',
246
+ id: result.id,
247
+ model: result.model,
248
+ timestamp: Date.now(),
249
+ delta: result.summary,
250
+ content: result.summary,
251
+ role: 'assistant',
252
+ }
253
+
254
+ // Yield done chunk
255
+ yield {
256
+ type: 'done',
257
+ id: result.id,
258
+ model: result.model,
259
+ timestamp: Date.now(),
260
+ finishReason: 'stop',
261
+ usage: result.usage,
262
+ }
263
+ }
264
+
265
+ // ===========================
266
+ // Options Factory
267
+ // ===========================
268
+
269
+ /**
270
+ * Create typed options for the summarize() function without executing.
271
+ */
272
+ export function createSummarizeOptions<
273
+ TAdapter extends SummarizeAdapter<string, object>,
274
+ TStream extends boolean = false,
275
+ >(
276
+ options: SummarizeActivityOptions<TAdapter, TStream>,
277
+ ): SummarizeActivityOptions<TAdapter, TStream> {
278
+ return options
279
+ }
280
+
281
+ // Re-export adapter types
282
+ export type {
283
+ SummarizeAdapter,
284
+ SummarizeAdapterConfig,
285
+ AnySummarizeAdapter,
286
+ } from './adapter'
287
+ export { BaseSummarizeAdapter } from './adapter'
@@ -110,7 +110,7 @@ export interface AIDevtoolsEventMap {
110
110
  duration: number
111
111
  timestamp: number
112
112
  }
113
- 'tanstack-ai-devtools:chat:started': {
113
+ 'tanstack-ai-devtools:text:started': {
114
114
  requestId: string
115
115
  streamId: string
116
116
  provider: string
@@ -122,9 +122,9 @@ export interface AIDevtoolsEventMap {
122
122
  clientId?: string
123
123
  toolNames?: Array<string>
124
124
  options?: Record<string, unknown>
125
- providerOptions?: Record<string, unknown>
125
+ modelOptions?: Record<string, unknown>
126
126
  }
127
- 'tanstack-ai-devtools:chat:completed': {
127
+ 'tanstack-ai-devtools:text:completed': {
128
128
  requestId: string
129
129
  streamId: string
130
130
  model: string
@@ -138,7 +138,7 @@ export interface AIDevtoolsEventMap {
138
138
  }
139
139
  timestamp: number
140
140
  }
141
- 'tanstack-ai-devtools:chat:iteration': {
141
+ 'tanstack-ai-devtools:text:iteration': {
142
142
  requestId: string
143
143
  streamId: string
144
144
  iterationNumber: number
@@ -159,22 +159,6 @@ export interface AIDevtoolsEventMap {
159
159
  timestamp: number
160
160
  }
161
161
 
162
- // Embedding events
163
- 'tanstack-ai-devtools:embedding:started': {
164
- requestId: string
165
- model: string
166
- inputCount: number
167
- timestamp: number
168
- clientId?: string
169
- }
170
- 'tanstack-ai-devtools:embedding:completed': {
171
- requestId: string
172
- model: string
173
- inputCount: number
174
- duration: number
175
- timestamp: number
176
- }
177
-
178
162
  // Summarize events
179
163
  'tanstack-ai-devtools:summarize:started': {
180
164
  requestId: string
@@ -192,7 +176,7 @@ export interface AIDevtoolsEventMap {
192
176
  timestamp: number
193
177
  }
194
178
 
195
- // Chat Client events - from @tanstack/ai-client package
179
+ // Text Client events - from @tanstack/ai-client package
196
180
  'tanstack-ai-devtools:client:created': {
197
181
  clientId: string
198
182
  initialMessageCount: number