@tanstack/ai 0.16.0 → 0.18.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/dist/esm/activities/chat/adapter.d.ts +14 -0
- package/dist/esm/activities/chat/adapter.js.map +1 -1
- package/dist/esm/activities/chat/index.d.ts +27 -8
- package/dist/esm/activities/chat/index.js +245 -14
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/messages.js +26 -2
- package/dist/esm/activities/chat/messages.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.js +1 -1
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +12 -1
- package/dist/esm/activities/chat/tools/schema-converter.js +5 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -1
- package/dist/esm/activities/error-payload.d.ts +0 -8
- package/dist/esm/activities/error-payload.js +20 -2
- package/dist/esm/activities/error-payload.js.map +1 -1
- package/dist/esm/activities/generateImage/adapter.d.ts +2 -2
- package/dist/esm/activities/generateImage/adapter.js.map +1 -1
- package/dist/esm/activities/generateVideo/adapter.d.ts +2 -2
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
- package/dist/esm/activities/index.d.ts +1 -0
- package/dist/esm/activities/index.js +2 -0
- package/dist/esm/activities/index.js.map +1 -1
- package/dist/esm/activities/stream-generation-result.js +0 -2
- package/dist/esm/activities/stream-generation-result.js.map +1 -1
- package/dist/esm/activities/summarize/adapter.d.ts +4 -4
- package/dist/esm/activities/summarize/adapter.js.map +1 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +45 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js +148 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -0
- package/dist/esm/activities/summarize/index.d.ts +1 -0
- package/dist/esm/activities/summarize/index.js +4 -2
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.d.ts +123 -11
- package/dist/esm/utilities/ag-ui-wire.d.ts +44 -0
- package/dist/esm/utilities/ag-ui-wire.js +96 -0
- package/dist/esm/utilities/ag-ui-wire.js.map +1 -0
- package/dist/esm/utilities/chat-params.d.ts +80 -0
- package/dist/esm/utilities/chat-params.js +96 -0
- package/dist/esm/utilities/chat-params.js.map +1 -0
- package/package.json +3 -3
- package/skills/ai-core/ag-ui-protocol/SKILL.md +46 -3
- package/skills/ai-core/structured-outputs/SKILL.md +92 -1
- package/src/activities/chat/adapter.ts +17 -0
- package/src/activities/chat/index.ts +401 -35
- package/src/activities/chat/messages.ts +44 -4
- package/src/activities/chat/middleware/compose.ts +1 -1
- package/src/activities/chat/middleware/types.ts +12 -1
- package/src/activities/chat/tools/schema-converter.ts +14 -0
- package/src/activities/error-payload.ts +31 -2
- package/src/activities/generateImage/adapter.ts +8 -2
- package/src/activities/generateVideo/adapter.ts +8 -2
- package/src/activities/index.ts +5 -0
- package/src/activities/stream-generation-result.ts +4 -6
- package/src/activities/summarize/adapter.ts +8 -4
- package/src/activities/summarize/chat-stream-summarize.ts +238 -0
- package/src/activities/summarize/index.ts +12 -9
- package/src/index.ts +11 -0
- package/src/types.ts +146 -11
- package/src/utilities/ag-ui-wire.ts +182 -0
- package/src/utilities/chat-params.ts +199 -0
|
@@ -5,16 +5,45 @@
|
|
|
5
5
|
* Accepts Error instances, objects with string-ish `message`/`code`, or bare
|
|
6
6
|
* strings; always returns a shape safe to serialize. Never leaks the full
|
|
7
7
|
* error object (which may carry request/response state from an SDK).
|
|
8
|
+
*
|
|
9
|
+
* Abort-shaped errors (DOM `AbortError`, OpenAI `APIUserAbortError`,
|
|
10
|
+
* OpenRouter `RequestAbortedError`) are normalized to a stable
|
|
11
|
+
* `{ message: 'Request aborted', code: 'aborted' }` shape so callers can
|
|
12
|
+
* discriminate user-initiated cancellation from other failures without
|
|
13
|
+
* matching on provider-specific message strings.
|
|
8
14
|
*/
|
|
15
|
+
const ABORT_ERROR_NAMES = new Set([
|
|
16
|
+
'AbortError',
|
|
17
|
+
'APIUserAbortError',
|
|
18
|
+
'RequestAbortedError',
|
|
19
|
+
])
|
|
20
|
+
|
|
21
|
+
// HTTP status codes carried as numbers (e.g. `error.status = 429`) are a
|
|
22
|
+
// common variant on SDK error classes; coerce so the resulting `code` field
|
|
23
|
+
// is stable as a string for downstream consumers.
|
|
24
|
+
function normalizeCode(codeField: unknown): string | undefined {
|
|
25
|
+
if (typeof codeField === 'string') return codeField
|
|
26
|
+
if (typeof codeField === 'number' && Number.isFinite(codeField)) {
|
|
27
|
+
return String(codeField)
|
|
28
|
+
}
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
9
32
|
export function toRunErrorPayload(
|
|
10
33
|
error: unknown,
|
|
11
34
|
fallbackMessage = 'Unknown error occurred',
|
|
12
35
|
): { message: string; code: string | undefined } {
|
|
36
|
+
if (error && typeof error === 'object') {
|
|
37
|
+
const name = (error as { name?: unknown }).name
|
|
38
|
+
if (typeof name === 'string' && ABORT_ERROR_NAMES.has(name)) {
|
|
39
|
+
return { message: 'Request aborted', code: 'aborted' }
|
|
40
|
+
}
|
|
41
|
+
}
|
|
13
42
|
if (error instanceof Error) {
|
|
14
43
|
const codeField = (error as Error & { code?: unknown }).code
|
|
15
44
|
return {
|
|
16
45
|
message: error.message || fallbackMessage,
|
|
17
|
-
code:
|
|
46
|
+
code: normalizeCode(codeField),
|
|
18
47
|
}
|
|
19
48
|
}
|
|
20
49
|
if (typeof error === 'object' && error !== null) {
|
|
@@ -25,7 +54,7 @@ export function toRunErrorPayload(
|
|
|
25
54
|
typeof messageField === 'string' && messageField.length > 0
|
|
26
55
|
? messageField
|
|
27
56
|
: fallbackMessage,
|
|
28
|
-
code:
|
|
57
|
+
code: normalizeCode(codeField),
|
|
29
58
|
}
|
|
30
59
|
}
|
|
31
60
|
if (typeof error === 'string' && error.length > 0) {
|
|
@@ -34,7 +34,10 @@ export interface ImageAdapter<
|
|
|
34
34
|
TModel extends string = string,
|
|
35
35
|
TProviderOptions extends object = Record<string, unknown>,
|
|
36
36
|
TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
|
|
37
|
-
TModelSizeByName extends Record<string, string> = Record<
|
|
37
|
+
TModelSizeByName extends Record<string, string | undefined> = Record<
|
|
38
|
+
string,
|
|
39
|
+
string
|
|
40
|
+
>,
|
|
38
41
|
> {
|
|
39
42
|
/** Discriminator for adapter kind - used by generate() to determine API shape */
|
|
40
43
|
readonly kind: 'image'
|
|
@@ -76,7 +79,10 @@ export abstract class BaseImageAdapter<
|
|
|
76
79
|
TModel extends string = string,
|
|
77
80
|
TProviderOptions extends object = Record<string, unknown>,
|
|
78
81
|
TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
|
|
79
|
-
TModelSizeByName extends Record<string, string> = Record<
|
|
82
|
+
TModelSizeByName extends Record<string, string | undefined> = Record<
|
|
83
|
+
string,
|
|
84
|
+
string
|
|
85
|
+
>,
|
|
80
86
|
> implements ImageAdapter<
|
|
81
87
|
TModel,
|
|
82
88
|
TProviderOptions,
|
|
@@ -36,7 +36,10 @@ export interface VideoAdapter<
|
|
|
36
36
|
TModel extends string = string,
|
|
37
37
|
TProviderOptions extends object = Record<string, unknown>,
|
|
38
38
|
TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
|
|
39
|
-
TModelSizeByName extends Record<string, string> = Record<
|
|
39
|
+
TModelSizeByName extends Record<string, string | undefined> = Record<
|
|
40
|
+
string,
|
|
41
|
+
string
|
|
42
|
+
>,
|
|
40
43
|
> {
|
|
41
44
|
/** Discriminator for adapter kind - used to determine API shape */
|
|
42
45
|
readonly kind: 'video'
|
|
@@ -92,7 +95,10 @@ export abstract class BaseVideoAdapter<
|
|
|
92
95
|
TModel extends string = string,
|
|
93
96
|
TProviderOptions extends object = Record<string, unknown>,
|
|
94
97
|
TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,
|
|
95
|
-
TModelSizeByName extends Record<string, string> = Record<
|
|
98
|
+
TModelSizeByName extends Record<string, string | undefined> = Record<
|
|
99
|
+
string,
|
|
100
|
+
string
|
|
101
|
+
>,
|
|
96
102
|
> implements VideoAdapter<
|
|
97
103
|
TModel,
|
|
98
104
|
TProviderOptions,
|
package/src/activities/index.ts
CHANGED
|
@@ -60,6 +60,11 @@ export {
|
|
|
60
60
|
type SummarizeAdapterConfig,
|
|
61
61
|
type AnySummarizeAdapter,
|
|
62
62
|
} from './summarize/adapter'
|
|
63
|
+
export {
|
|
64
|
+
ChatStreamSummarizeAdapter,
|
|
65
|
+
type ChatStreamCapable,
|
|
66
|
+
type InferTextProviderOptions,
|
|
67
|
+
} from './summarize/chat-stream-summarize'
|
|
63
68
|
|
|
64
69
|
// ===========================
|
|
65
70
|
// Image Activity
|
|
@@ -34,7 +34,7 @@ export async function* streamGenerationResult<TResult>(
|
|
|
34
34
|
runId,
|
|
35
35
|
threadId,
|
|
36
36
|
timestamp: Date.now(),
|
|
37
|
-
}
|
|
37
|
+
}
|
|
38
38
|
|
|
39
39
|
try {
|
|
40
40
|
const result = await generator()
|
|
@@ -44,7 +44,7 @@ export async function* streamGenerationResult<TResult>(
|
|
|
44
44
|
name: 'generation:result',
|
|
45
45
|
value: result as unknown,
|
|
46
46
|
timestamp: Date.now(),
|
|
47
|
-
}
|
|
47
|
+
}
|
|
48
48
|
|
|
49
49
|
yield {
|
|
50
50
|
type: EventType.RUN_FINISHED,
|
|
@@ -52,18 +52,16 @@ export async function* streamGenerationResult<TResult>(
|
|
|
52
52
|
threadId,
|
|
53
53
|
finishReason: 'stop',
|
|
54
54
|
timestamp: Date.now(),
|
|
55
|
-
}
|
|
55
|
+
}
|
|
56
56
|
} catch (error: unknown) {
|
|
57
57
|
const payload = toRunErrorPayload(error, 'Generation failed')
|
|
58
58
|
yield {
|
|
59
59
|
type: EventType.RUN_ERROR,
|
|
60
|
-
runId,
|
|
61
|
-
threadId,
|
|
62
60
|
message: payload.message,
|
|
63
61
|
code: payload.code,
|
|
64
62
|
// Deprecated nested form for backward compatibility
|
|
65
63
|
error: payload,
|
|
66
64
|
timestamp: Date.now(),
|
|
67
|
-
}
|
|
65
|
+
}
|
|
68
66
|
}
|
|
69
67
|
}
|
|
@@ -46,7 +46,9 @@ export interface SummarizeAdapter<
|
|
|
46
46
|
/**
|
|
47
47
|
* Summarize the given text
|
|
48
48
|
*/
|
|
49
|
-
summarize: (
|
|
49
|
+
summarize: (
|
|
50
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
51
|
+
) => Promise<SummarizationResult>
|
|
50
52
|
|
|
51
53
|
/**
|
|
52
54
|
* Stream summarization of the given text.
|
|
@@ -54,7 +56,7 @@ export interface SummarizeAdapter<
|
|
|
54
56
|
* non-streaming summarize and yield the result as a single chunk.
|
|
55
57
|
*/
|
|
56
58
|
summarizeStream?: (
|
|
57
|
-
options: SummarizationOptions
|
|
59
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
58
60
|
) => AsyncIterable<StreamChunk>
|
|
59
61
|
}
|
|
60
62
|
|
|
@@ -91,7 +93,7 @@ export abstract class BaseSummarizeAdapter<
|
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
abstract summarize(
|
|
94
|
-
options: SummarizationOptions
|
|
96
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
95
97
|
): Promise<SummarizationResult>
|
|
96
98
|
|
|
97
99
|
/**
|
|
@@ -99,7 +101,9 @@ export abstract class BaseSummarizeAdapter<
|
|
|
99
101
|
* Override this method in concrete implementations to enable streaming.
|
|
100
102
|
* If not overridden, the activity layer will fall back to non-streaming.
|
|
101
103
|
*/
|
|
102
|
-
summarizeStream?(
|
|
104
|
+
summarizeStream?(
|
|
105
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
106
|
+
): AsyncIterable<StreamChunk>
|
|
103
107
|
|
|
104
108
|
protected generateId(): string {
|
|
105
109
|
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { EventType } from '@ag-ui/core'
|
|
2
|
+
import { toRunErrorPayload } from '../error-payload'
|
|
3
|
+
import { BaseSummarizeAdapter } from './adapter'
|
|
4
|
+
import type {
|
|
5
|
+
StreamChunk,
|
|
6
|
+
SummarizationOptions,
|
|
7
|
+
SummarizationResult,
|
|
8
|
+
TextOptions,
|
|
9
|
+
} from '../../types'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Minimal contract for a text adapter that supports `chatStream`. Lets
|
|
13
|
+
* `ChatStreamSummarizeAdapter` work with any text adapter without coupling
|
|
14
|
+
* to a specific implementation.
|
|
15
|
+
*
|
|
16
|
+
* The provider-options shape is intentionally `any` here — the wrapper only
|
|
17
|
+
* forwards `modelOptions` straight through, so a text adapter with a richer
|
|
18
|
+
* per-model options type (e.g. `ResolveProviderOptions<TModel>`) is still
|
|
19
|
+
* acceptable. Summarize-level type safety is enforced via
|
|
20
|
+
* `SummarizationOptions<TProviderOptions>` on the wrapper itself.
|
|
21
|
+
*/
|
|
22
|
+
export interface ChatStreamCapable {
|
|
23
|
+
chatStream: (options: TextOptions<any>) => AsyncIterable<StreamChunk>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Extract the per-model `modelOptions` type a text adapter accepts. Used by
|
|
28
|
+
* provider summarize factories so their `modelOptions` IntelliSense matches
|
|
29
|
+
* what the underlying text adapter actually understands.
|
|
30
|
+
*/
|
|
31
|
+
export type InferTextProviderOptions<TAdapter> = TAdapter extends {
|
|
32
|
+
'~types': { providerOptions: infer P }
|
|
33
|
+
}
|
|
34
|
+
? P extends object
|
|
35
|
+
? P
|
|
36
|
+
: object
|
|
37
|
+
: object
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Summarize adapter that wraps any `ChatStreamCapable` text adapter and
|
|
41
|
+
* prompts it for summarization. Not tied to any wire format.
|
|
42
|
+
*/
|
|
43
|
+
export class ChatStreamSummarizeAdapter<
|
|
44
|
+
TModel extends string,
|
|
45
|
+
TProviderOptions extends object = Record<string, unknown>,
|
|
46
|
+
> extends BaseSummarizeAdapter<TModel, TProviderOptions> {
|
|
47
|
+
readonly name: string
|
|
48
|
+
|
|
49
|
+
private textAdapter: ChatStreamCapable
|
|
50
|
+
|
|
51
|
+
constructor(
|
|
52
|
+
textAdapter: ChatStreamCapable,
|
|
53
|
+
model: TModel,
|
|
54
|
+
name: string = 'chat-stream-summarize',
|
|
55
|
+
) {
|
|
56
|
+
super({}, model)
|
|
57
|
+
this.name = name
|
|
58
|
+
this.textAdapter = textAdapter
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async summarize(
|
|
62
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
63
|
+
): Promise<SummarizationResult> {
|
|
64
|
+
const systemPrompt = this.buildSummarizationPrompt(options)
|
|
65
|
+
|
|
66
|
+
let summary = ''
|
|
67
|
+
const id = this.generateId()
|
|
68
|
+
let model = options.model
|
|
69
|
+
let usage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 }
|
|
70
|
+
|
|
71
|
+
options.logger.request(
|
|
72
|
+
`activity=summarize provider=${this.name} model=${options.model} text-length=${options.text.length} maxLength=${options.maxLength ?? 'unset'}`,
|
|
73
|
+
{ provider: this.name, model: options.model },
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
for await (const chunk of this.textAdapter.chatStream(
|
|
78
|
+
this.buildTextOptions(options, systemPrompt),
|
|
79
|
+
)) {
|
|
80
|
+
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
|
|
81
|
+
if (chunk.content) {
|
|
82
|
+
summary = chunk.content
|
|
83
|
+
} else if (chunk.delta) {
|
|
84
|
+
// Append delta only when present — a content-less chunk with no
|
|
85
|
+
// delta would otherwise concat literal `'undefined'`.
|
|
86
|
+
summary += chunk.delta
|
|
87
|
+
}
|
|
88
|
+
model = chunk.model || model
|
|
89
|
+
}
|
|
90
|
+
if (chunk.type === 'RUN_FINISHED') {
|
|
91
|
+
if (chunk.usage) {
|
|
92
|
+
usage = chunk.usage
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Surface failures: the underlying chatStream emits RUN_ERROR instead
|
|
96
|
+
// of throwing, so without this branch summarize() would return an
|
|
97
|
+
// empty summary and pretend a failed run succeeded.
|
|
98
|
+
if (chunk.type === 'RUN_ERROR') {
|
|
99
|
+
const message =
|
|
100
|
+
(chunk.error && typeof chunk.error.message === 'string'
|
|
101
|
+
? chunk.error.message
|
|
102
|
+
: null) ?? 'Summarization failed'
|
|
103
|
+
const code =
|
|
104
|
+
chunk.error && typeof chunk.error.code === 'string'
|
|
105
|
+
? chunk.error.code
|
|
106
|
+
: undefined
|
|
107
|
+
const err = new Error(message)
|
|
108
|
+
if (code) {
|
|
109
|
+
;(err as Error & { code?: string }).code = code
|
|
110
|
+
}
|
|
111
|
+
throw err
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} catch (error: unknown) {
|
|
115
|
+
// Narrow before logging: raw SDK errors can carry request metadata
|
|
116
|
+
// (including auth headers) which we must never surface to user loggers.
|
|
117
|
+
options.logger.errors(`${this.name}.summarize fatal`, {
|
|
118
|
+
error: toRunErrorPayload(error, `${this.name}.summarize failed`),
|
|
119
|
+
source: `${this.name}.summarize`,
|
|
120
|
+
})
|
|
121
|
+
throw error
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { id, model, summary, usage }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async *summarizeStream(
|
|
128
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
129
|
+
): AsyncIterable<StreamChunk> {
|
|
130
|
+
const systemPrompt = this.buildSummarizationPrompt(options)
|
|
131
|
+
|
|
132
|
+
options.logger.request(
|
|
133
|
+
`activity=summarizeStream provider=${this.name} model=${options.model} text-length=${options.text.length} maxLength=${options.maxLength ?? 'unset'}`,
|
|
134
|
+
{ provider: this.name, model: options.model },
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
const id = this.generateId()
|
|
138
|
+
let summary = ''
|
|
139
|
+
let model = options.model
|
|
140
|
+
let usage: SummarizationResult['usage'] = {
|
|
141
|
+
promptTokens: 0,
|
|
142
|
+
completionTokens: 0,
|
|
143
|
+
totalTokens: 0,
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
for await (const chunk of this.textAdapter.chatStream(
|
|
148
|
+
this.buildTextOptions(options, systemPrompt),
|
|
149
|
+
)) {
|
|
150
|
+
// Accumulate the same way `summarize()` does so consumers see deltas
|
|
151
|
+
// AND the terminal `generation:result` event below carries the same
|
|
152
|
+
// final summary that non-streaming returns.
|
|
153
|
+
if (chunk.type === 'TEXT_MESSAGE_CONTENT') {
|
|
154
|
+
if (chunk.content) {
|
|
155
|
+
summary = chunk.content
|
|
156
|
+
} else if (chunk.delta) {
|
|
157
|
+
summary += chunk.delta
|
|
158
|
+
}
|
|
159
|
+
if (chunk.model) model = chunk.model
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Emit the GenerationClient-shaped result event just before the
|
|
163
|
+
// terminal RUN_FINISHED so subscribers (useSummarize) populate
|
|
164
|
+
// `result` before flipping `status` to success.
|
|
165
|
+
if (chunk.type === 'RUN_FINISHED') {
|
|
166
|
+
if (chunk.usage) usage = chunk.usage
|
|
167
|
+
if (chunk.model) model = chunk.model
|
|
168
|
+
yield {
|
|
169
|
+
type: EventType.CUSTOM,
|
|
170
|
+
name: 'generation:result',
|
|
171
|
+
value: { id, model, summary, usage } satisfies SummarizationResult,
|
|
172
|
+
model,
|
|
173
|
+
timestamp: Date.now(),
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
yield chunk
|
|
178
|
+
}
|
|
179
|
+
} catch (error: unknown) {
|
|
180
|
+
options.logger.errors(`${this.name}.summarizeStream fatal`, {
|
|
181
|
+
error: toRunErrorPayload(error, `${this.name}.summarizeStream failed`),
|
|
182
|
+
source: `${this.name}.summarizeStream`,
|
|
183
|
+
})
|
|
184
|
+
throw error
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Build the TextOptions passed to the underlying chatStream. Provider
|
|
190
|
+
* `modelOptions` from the summarize call are forwarded as-is so knobs like
|
|
191
|
+
* Anthropic cache headers, Gemini safety settings, or Ollama tuning params
|
|
192
|
+
* still reach the wire layer.
|
|
193
|
+
*/
|
|
194
|
+
protected buildTextOptions(
|
|
195
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
196
|
+
systemPrompt: string,
|
|
197
|
+
): TextOptions<TProviderOptions> {
|
|
198
|
+
return {
|
|
199
|
+
model: options.model,
|
|
200
|
+
messages: [{ role: 'user', content: options.text }],
|
|
201
|
+
systemPrompts: [systemPrompt],
|
|
202
|
+
maxTokens: options.maxLength,
|
|
203
|
+
temperature: 0.3,
|
|
204
|
+
modelOptions: options.modelOptions,
|
|
205
|
+
logger: options.logger,
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
protected buildSummarizationPrompt(
|
|
210
|
+
options: SummarizationOptions<TProviderOptions>,
|
|
211
|
+
): string {
|
|
212
|
+
let prompt = 'You are a professional summarizer. '
|
|
213
|
+
|
|
214
|
+
switch (options.style) {
|
|
215
|
+
case 'bullet-points':
|
|
216
|
+
prompt += 'Provide a summary in bullet point format. '
|
|
217
|
+
break
|
|
218
|
+
case 'paragraph':
|
|
219
|
+
prompt += 'Provide a summary in paragraph format. '
|
|
220
|
+
break
|
|
221
|
+
case 'concise':
|
|
222
|
+
prompt += 'Provide a very concise summary in 1-2 sentences. '
|
|
223
|
+
break
|
|
224
|
+
default:
|
|
225
|
+
prompt += 'Provide a clear and concise summary. '
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (options.focus && options.focus.length > 0) {
|
|
229
|
+
prompt += `Focus on the following aspects: ${options.focus.join(', ')}. `
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (options.maxLength) {
|
|
233
|
+
prompt += `Keep the summary under ${options.maxLength} tokens. `
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return prompt
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -11,11 +11,7 @@ import { resolveDebugOption } from '../../logger/resolve'
|
|
|
11
11
|
import type { InternalLogger } from '../../logger/internal-logger'
|
|
12
12
|
import type { DebugOption } from '../../logger/types'
|
|
13
13
|
import type { SummarizeAdapter } from './adapter'
|
|
14
|
-
import type {
|
|
15
|
-
StreamChunk,
|
|
16
|
-
SummarizationOptions,
|
|
17
|
-
SummarizationResult,
|
|
18
|
-
} from '../../types'
|
|
14
|
+
import type { StreamChunk, SummarizationResult } from '../../types'
|
|
19
15
|
|
|
20
16
|
// ===========================
|
|
21
17
|
// Activity Kind
|
|
@@ -184,7 +180,7 @@ export function summarize<
|
|
|
184
180
|
async function runSummarize(
|
|
185
181
|
options: SummarizeActivityOptions<SummarizeAdapter<string, object>, false>,
|
|
186
182
|
): Promise<SummarizationResult> {
|
|
187
|
-
const { adapter, text, maxLength, style, focus } = options
|
|
183
|
+
const { adapter, text, maxLength, style, focus, modelOptions } = options
|
|
188
184
|
const model = adapter.model
|
|
189
185
|
const requestId = createId('summarize')
|
|
190
186
|
const inputLength = text.length
|
|
@@ -205,12 +201,13 @@ async function runSummarize(
|
|
|
205
201
|
inputLength,
|
|
206
202
|
})
|
|
207
203
|
|
|
208
|
-
const summarizeOptions
|
|
204
|
+
const summarizeOptions = {
|
|
209
205
|
model,
|
|
210
206
|
text,
|
|
211
207
|
maxLength,
|
|
212
208
|
style,
|
|
213
209
|
focus,
|
|
210
|
+
modelOptions,
|
|
214
211
|
logger,
|
|
215
212
|
}
|
|
216
213
|
|
|
@@ -253,7 +250,7 @@ async function runSummarize(
|
|
|
253
250
|
async function* runStreamingSummarize(
|
|
254
251
|
options: SummarizeActivityOptions<SummarizeAdapter<string, object>, true>,
|
|
255
252
|
): AsyncIterable<StreamChunk> {
|
|
256
|
-
const { adapter, text, maxLength, style, focus } = options
|
|
253
|
+
const { adapter, text, maxLength, style, focus, modelOptions } = options
|
|
257
254
|
const model = adapter.model
|
|
258
255
|
const logger: InternalLogger = resolveDebugOption(options.debug)
|
|
259
256
|
|
|
@@ -263,12 +260,13 @@ async function* runStreamingSummarize(
|
|
|
263
260
|
stream: true,
|
|
264
261
|
})
|
|
265
262
|
|
|
266
|
-
const summarizeOptions
|
|
263
|
+
const summarizeOptions = {
|
|
267
264
|
model,
|
|
268
265
|
text,
|
|
269
266
|
maxLength,
|
|
270
267
|
style,
|
|
271
268
|
focus,
|
|
269
|
+
modelOptions,
|
|
272
270
|
logger,
|
|
273
271
|
}
|
|
274
272
|
|
|
@@ -313,3 +311,8 @@ export type {
|
|
|
313
311
|
AnySummarizeAdapter,
|
|
314
312
|
} from './adapter'
|
|
315
313
|
export { BaseSummarizeAdapter } from './adapter'
|
|
314
|
+
export {
|
|
315
|
+
ChatStreamSummarizeAdapter,
|
|
316
|
+
type ChatStreamCapable,
|
|
317
|
+
type InferTextProviderOptions,
|
|
318
|
+
} from './chat-stream-summarize'
|
package/src/index.ts
CHANGED
|
@@ -168,6 +168,17 @@ export type {
|
|
|
168
168
|
JSONParser,
|
|
169
169
|
} from './activities/chat/stream/index'
|
|
170
170
|
|
|
171
|
+
// Chat utilities
|
|
172
|
+
export {
|
|
173
|
+
chatParamsFromRequest,
|
|
174
|
+
chatParamsFromRequestBody,
|
|
175
|
+
mergeAgentTools,
|
|
176
|
+
} from './utilities/chat-params'
|
|
177
|
+
|
|
178
|
+
// AG-UI wire serialization (used internally by @tanstack/ai-client)
|
|
179
|
+
export { uiMessagesToWire } from './utilities/ag-ui-wire'
|
|
180
|
+
export type { WireMessage } from './utilities/ag-ui-wire'
|
|
181
|
+
|
|
171
182
|
// Adapter extension utilities
|
|
172
183
|
export { createModel, extendAdapter } from './extend-adapter'
|
|
173
184
|
export type { ExtendedModelDef } from './extend-adapter'
|