@tanstack/ai 0.6.2 → 0.8.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/index.d.ts +20 -0
- package/dist/esm/activities/chat/index.js +248 -213
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +66 -0
- package/dist/esm/activities/chat/middleware/compose.js +327 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -0
- package/dist/esm/activities/chat/middleware/index.d.ts +2 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +89 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +76 -0
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +1 -0
- package/dist/esm/activities/chat/middleware/types.d.ts +307 -0
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +16 -1
- package/dist/esm/activities/chat/tools/tool-calls.js +148 -64
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/generateImage/index.d.ts +19 -6
- package/dist/esm/activities/generateImage/index.js +12 -3
- package/dist/esm/activities/generateImage/index.js.map +1 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +19 -6
- package/dist/esm/activities/generateSpeech/index.js +12 -3
- package/dist/esm/activities/generateSpeech/index.js.map +1 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +30 -6
- package/dist/esm/activities/generateTranscription/index.js +14 -3
- package/dist/esm/activities/generateTranscription/index.js.map +1 -1
- package/dist/esm/activities/generateVideo/index.d.ts +45 -7
- package/dist/esm/activities/generateVideo/index.js +91 -2
- package/dist/esm/activities/generateVideo/index.js.map +1 -1
- package/dist/esm/activities/stream-generation-result.d.ts +14 -0
- package/dist/esm/activities/stream-generation-result.js +40 -0
- package/dist/esm/activities/stream-generation-result.js.map +1 -0
- package/dist/esm/activities/summarize/index.js +3 -18
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/middlewares/content-guard.d.ts +77 -0
- package/dist/esm/middlewares/content-guard.js +155 -0
- package/dist/esm/middlewares/content-guard.js.map +1 -0
- package/dist/esm/middlewares/index.d.ts +2 -0
- package/dist/esm/middlewares/index.js +7 -0
- package/dist/esm/middlewares/index.js.map +1 -0
- package/dist/esm/middlewares/tool-cache.d.ts +1 -0
- package/dist/esm/realtime/index.d.ts +30 -0
- package/dist/esm/realtime/index.js +8 -0
- package/dist/esm/realtime/index.js.map +1 -0
- package/dist/esm/realtime/types.d.ts +234 -0
- package/package.json +6 -6
- package/src/activities/chat/index.ts +322 -256
- package/src/activities/chat/middleware/compose.ts +392 -0
- package/src/activities/chat/middleware/index.ts +17 -0
- package/src/activities/chat/middleware/tool-cache-middleware.ts +189 -0
- package/src/activities/chat/middleware/types.ts +419 -0
- package/src/activities/chat/tools/tool-calls.ts +225 -87
- package/src/activities/generateImage/index.ts +50 -8
- package/src/activities/generateSpeech/index.ts +42 -8
- package/src/activities/generateTranscription/index.ts +60 -10
- package/src/activities/generateVideo/index.ts +174 -7
- package/src/activities/stream-generation-result.ts +62 -0
- package/src/activities/summarize/index.ts +4 -23
- package/src/index.ts +41 -2
- package/src/middlewares/content-guard.ts +285 -0
- package/src/middlewares/index.ts +13 -0
- package/src/middlewares/tool-cache.ts +6 -0
- package/src/realtime/index.ts +38 -0
- package/src/realtime/types.ts +294 -0
- package/dist/esm/event-client.d.ts +0 -394
- package/dist/esm/event-client.js +0 -13
- package/dist/esm/event-client.js.map +0 -1
- package/src/event-client.ts +0 -497
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import { aiEventClient } from "
|
|
1
|
+
import { aiEventClient } from "@tanstack/ai-event-client";
|
|
2
2
|
const kind = "video";
|
|
3
3
|
function createId(prefix) {
|
|
4
4
|
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
function generateVideo(options) {
|
|
7
|
+
if (options.stream) {
|
|
8
|
+
return runStreamingVideoGeneration(
|
|
9
|
+
options
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return runCreateVideoJob(options);
|
|
13
|
+
}
|
|
14
|
+
async function runCreateVideoJob(options) {
|
|
7
15
|
const { adapter, prompt, size, duration, modelOptions } = options;
|
|
8
16
|
const model = adapter.model;
|
|
9
17
|
return adapter.createVideoJob({
|
|
@@ -14,6 +22,87 @@ async function generateVideo(options) {
|
|
|
14
22
|
modelOptions
|
|
15
23
|
});
|
|
16
24
|
}
|
|
25
|
+
function sleep(ms) {
|
|
26
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
27
|
+
}
|
|
28
|
+
async function* runStreamingVideoGeneration(options) {
|
|
29
|
+
const { adapter, prompt, size, duration, modelOptions } = options;
|
|
30
|
+
const model = adapter.model;
|
|
31
|
+
const runId = options.runId ?? createId("run");
|
|
32
|
+
const pollingInterval = options.pollingInterval ?? 2e3;
|
|
33
|
+
const maxDuration = options.maxDuration ?? 6e5;
|
|
34
|
+
yield {
|
|
35
|
+
type: "RUN_STARTED",
|
|
36
|
+
runId,
|
|
37
|
+
timestamp: Date.now()
|
|
38
|
+
};
|
|
39
|
+
try {
|
|
40
|
+
const jobResult = await adapter.createVideoJob({
|
|
41
|
+
model,
|
|
42
|
+
prompt,
|
|
43
|
+
size,
|
|
44
|
+
duration,
|
|
45
|
+
modelOptions
|
|
46
|
+
});
|
|
47
|
+
yield {
|
|
48
|
+
type: "CUSTOM",
|
|
49
|
+
name: "video:job:created",
|
|
50
|
+
value: { jobId: jobResult.jobId },
|
|
51
|
+
timestamp: Date.now()
|
|
52
|
+
};
|
|
53
|
+
const startTime = Date.now();
|
|
54
|
+
while (Date.now() - startTime < maxDuration) {
|
|
55
|
+
await sleep(pollingInterval);
|
|
56
|
+
const statusResult = await adapter.getVideoStatus(jobResult.jobId);
|
|
57
|
+
yield {
|
|
58
|
+
type: "CUSTOM",
|
|
59
|
+
name: "video:status",
|
|
60
|
+
value: {
|
|
61
|
+
jobId: jobResult.jobId,
|
|
62
|
+
status: statusResult.status,
|
|
63
|
+
progress: statusResult.progress,
|
|
64
|
+
error: statusResult.error
|
|
65
|
+
},
|
|
66
|
+
timestamp: Date.now()
|
|
67
|
+
};
|
|
68
|
+
if (statusResult.status === "completed") {
|
|
69
|
+
const urlResult = await adapter.getVideoUrl(jobResult.jobId);
|
|
70
|
+
yield {
|
|
71
|
+
type: "CUSTOM",
|
|
72
|
+
name: "generation:result",
|
|
73
|
+
value: {
|
|
74
|
+
jobId: jobResult.jobId,
|
|
75
|
+
status: "completed",
|
|
76
|
+
url: urlResult.url,
|
|
77
|
+
expiresAt: urlResult.expiresAt
|
|
78
|
+
},
|
|
79
|
+
timestamp: Date.now()
|
|
80
|
+
};
|
|
81
|
+
yield {
|
|
82
|
+
type: "RUN_FINISHED",
|
|
83
|
+
runId,
|
|
84
|
+
finishReason: "stop",
|
|
85
|
+
timestamp: Date.now()
|
|
86
|
+
};
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (statusResult.status === "failed") {
|
|
90
|
+
throw new Error(statusResult.error || "Video generation failed");
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
throw new Error("Video generation timed out");
|
|
94
|
+
} catch (error) {
|
|
95
|
+
yield {
|
|
96
|
+
type: "RUN_ERROR",
|
|
97
|
+
runId,
|
|
98
|
+
error: {
|
|
99
|
+
message: error.message || "Video generation failed",
|
|
100
|
+
code: error.code
|
|
101
|
+
},
|
|
102
|
+
timestamp: Date.now()
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
17
106
|
async function getVideoJobStatus(options) {
|
|
18
107
|
const { adapter, jobId } = options;
|
|
19
108
|
const requestId = createId("video-status");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/activities/generateVideo/index.ts"],"sourcesContent":["/**\n * Video Activity (Experimental)\n *\n * Generates videos from text prompts using a jobs/polling architecture.\n * This is a self-contained module with implementation, types, and JSDoc.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\n\nimport { aiEventClient } from '../../event-client.js'\nimport type { VideoAdapter } from './adapter'\nimport type {\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'video' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/**\n * Extract provider options from a VideoAdapter via ~types.\n */\nexport type VideoProviderOptions<TAdapter> =\n TAdapter extends VideoAdapter<any, any, any, any>\n ? TAdapter['~types']['providerOptions']\n : object\n\n/**\n * Extract the size type for a VideoAdapter's model via ~types.\n */\nexport type VideoSizeForAdapter<TAdapter> =\n TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap>\n ? TModel extends keyof TSizeMap\n ? TSizeMap[TModel]\n : string\n : string\n\n// ===========================\n// Activity Options Types\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n// ===========================\n\n/**\n * Base options shared by all video activity operations.\n * The model is extracted from the adapter's model property.\n */\ninterface VideoActivityBaseOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> {\n /** The video adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n}\n\n/**\n * Options for creating a new video generation job.\n * The model is extracted from the adapter's model property.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoCreateOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> = VideoActivityBaseOptions<TAdapter> & {\n /** Request type - create a new job (default if not specified) */\n request?: 'create'\n /** Text description of the desired video */\n prompt: string\n /** Video size — format depends on the provider (e.g., \"16:9\", \"1280x720\") */\n size?: VideoSizeForAdapter<TAdapter>\n /** Video duration in seconds */\n duration?: number\n} & ({} extends VideoProviderOptions<TAdapter>\n ? {\n /** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>\n }\n : {\n /** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>\n })\n\n/**\n * Options for polling the status of a video generation job.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoStatusOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get job status */\n request: 'status'\n /** The job ID to check status for */\n jobId: string\n}\n\n/**\n * Options for getting the URL of a completed video.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoUrlOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get video URL */\n request: 'url'\n /** The job ID to get URL for */\n jobId: string\n}\n\n/**\n * Union type for all video activity options.\n * Discriminated by the `request` field.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TRequest extends 'create' | 'status' | 'url' = 'create',\n> = TRequest extends 'status'\n ? VideoStatusOptions<TAdapter>\n : TRequest extends 'url'\n ? VideoUrlOptions<TAdapter>\n : VideoCreateOptions<TAdapter>\n\n// ===========================\n// Activity Result Types\n// ===========================\n\n/**\n * Result type for the video activity, based on request type.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityResult<\n TRequest extends 'create' | 'status' | 'url' = 'create',\n> = TRequest extends 'status'\n ? Promise<VideoStatusResult>\n : TRequest extends 'url'\n ? Promise<VideoUrlResult>\n : Promise<VideoJobResult>\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Generate video - creates a video generation job from a text prompt.\n *\n * Uses AI video generation models to create videos based on natural language descriptions.\n * Unlike image generation, video generation is asynchronous and requires polling for completion.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Create a video generation job\n * ```ts\n * import { generateVideo } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * // Start a video generation job\n * const { jobId } = await generateVideo({\n * adapter: openaiVideo('sora-2'),\n * prompt: 'A cat chasing a dog in a sunny park'\n * })\n *\n * console.log('Job started:', jobId)\n * ```\n */\nexport async function generateVideo<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult> {\n const { adapter, prompt, size, duration, modelOptions } = options\n const model = adapter.model\n\n return adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\n })\n}\n\n/**\n * Get video job status - returns the current status, progress, and URL if available.\n *\n * This function combines status checking and URL retrieval. If the job is completed,\n * it will automatically fetch and include the video URL.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Check job status\n * ```ts\n * import { getVideoJobStatus } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * const result = await getVideoJobStatus({\n * adapter: openaiVideo('sora-2'),\n * jobId: 'job-123'\n * })\n *\n * console.log('Status:', result.status)\n * console.log('Progress:', result.progress)\n * if (result.url) {\n * console.log('Video URL:', result.url)\n * }\n * ```\n */\nexport async function getVideoJobStatus<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: {\n adapter: TAdapter & { kind: typeof kind }\n jobId: string\n}): Promise<{\n status: 'pending' | 'processing' | 'completed' | 'failed'\n progress?: number\n url?: string\n error?: string\n}> {\n const { adapter, jobId } = options\n const requestId = createId('video-status')\n const startTime = Date.now()\n\n aiEventClient.emit('video:request:started', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n timestamp: startTime,\n })\n\n // Get status first\n const statusResult = await adapter.getVideoStatus(jobId)\n\n // If completed, also get the URL\n if (statusResult.status === 'completed') {\n try {\n const urlResult = await adapter.getVideoUrl(jobId)\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n }\n } catch (error) {\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n // If URL fetch fails, still return status\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\n }\n }\n }\n\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n\n // Return status for non-completed jobs\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n }\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the generateVideo() function without executing.\n */\nexport function createVideoOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n VideoAdapter,\n VideoAdapterConfig,\n AnyVideoAdapter,\n} from './adapter'\nexport { BaseVideoAdapter } from './adapter'\n"],"names":[],"mappings":";AAsBO,MAAM,OAAO;AA2BpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AA6HA,eAAsB,cAEpB,SAAgE;AAChE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,iBAAiB;AAC1D,QAAM,QAAQ,QAAQ;AAEtB,SAAO,QAAQ,eAAe;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,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A,MAAA;AAAA,IAE/C;AAAA,EACF;AAEA,gBAAc,KAAK,2BAA2B;AAAA,IAC5C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB,OAAO,QAAQ;AAAA,IACf,aAAa;AAAA,IACb;AAAA,IACA,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,IACpB,UAAU,KAAK,IAAA,IAAQ;AAAA,IACvB,WAAW,KAAK,IAAA;AAAA,EAAI,CACrB;AAGD,SAAO;AAAA,IACL,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,EAAA;AAExB;AASO,SAAS,mBAEd,SAAqE;AACrE,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/activities/generateVideo/index.ts"],"sourcesContent":["/**\n * Video Activity (Experimental)\n *\n * Generates videos from text prompts using a jobs/polling architecture.\n * This is a self-contained module with implementation, types, and JSDoc.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\n\nimport { aiEventClient } from '@tanstack/ai-event-client'\nimport type { VideoAdapter } from './adapter'\nimport type {\n StreamChunk,\n VideoJobResult,\n VideoStatusResult,\n VideoUrlResult,\n} from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'video' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/**\n * Extract provider options from a VideoAdapter via ~types.\n */\nexport type VideoProviderOptions<TAdapter> =\n TAdapter extends VideoAdapter<any, any, any, any>\n ? TAdapter['~types']['providerOptions']\n : object\n\n/**\n * Extract the size type for a VideoAdapter's model via ~types.\n */\nexport type VideoSizeForAdapter<TAdapter> =\n TAdapter extends VideoAdapter<infer TModel, any, any, infer TSizeMap>\n ? TModel extends keyof TSizeMap\n ? TSizeMap[TModel]\n : string\n : string\n\n// ===========================\n// Activity Options Types\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n// ===========================\n\n/**\n * Base options shared by all video activity operations.\n * The model is extracted from the adapter's model property.\n */\ninterface VideoActivityBaseOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> {\n /** The video adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n}\n\n/**\n * Options for creating a new video generation job.\n * The model is extracted from the adapter's model property.\n *\n * @template TAdapter - The video adapter type\n * @template TStream - Whether to stream the output\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoCreateOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n> = VideoActivityBaseOptions<TAdapter> & {\n /** Request type - create a new job (default if not specified) */\n request?: 'create'\n /** Text description of the desired video */\n prompt: string\n /** Video size — format depends on the provider (e.g., \"16:9\", \"1280x720\") */\n size?: VideoSizeForAdapter<TAdapter>\n /** Video duration in seconds */\n duration?: number\n /**\n * Whether to stream the video generation lifecycle.\n * When true, returns an AsyncIterable<StreamChunk> that handles the full\n * job lifecycle: create job, poll for status, yield updates, and yield final result.\n * When false or not provided, returns a Promise<VideoJobResult>.\n *\n * @default false\n */\n stream?: TStream\n /** Polling interval in milliseconds (stream mode only). @default 2000 */\n pollingInterval?: number\n /** Maximum time to wait before timing out in milliseconds (stream mode only). @default 600000 */\n maxDuration?: number\n /** Custom run ID (stream mode only) */\n runId?: string\n} & ({} extends VideoProviderOptions<TAdapter>\n ? {\n /** Provider-specific options for video generation */ modelOptions?: VideoProviderOptions<TAdapter>\n }\n : {\n /** Provider-specific options for video generation */ modelOptions: VideoProviderOptions<TAdapter>\n })\n\n/**\n * Options for polling the status of a video generation job.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoStatusOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get job status */\n request: 'status'\n /** The job ID to check status for */\n jobId: string\n}\n\n/**\n * Options for getting the URL of a completed video.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport interface VideoUrlOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n> extends VideoActivityBaseOptions<TAdapter> {\n /** Request type - get video URL */\n request: 'url'\n /** The job ID to get URL for */\n jobId: string\n}\n\n/**\n * Union type for all video activity options.\n * Discriminated by the `request` field.\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TRequest extends 'create' | 'status' | 'url' = 'create',\n TStream extends boolean = false,\n> = TRequest extends 'status'\n ? VideoStatusOptions<TAdapter>\n : TRequest extends 'url'\n ? VideoUrlOptions<TAdapter>\n : VideoCreateOptions<TAdapter, TStream>\n\n// ===========================\n// Activity Result Types\n// ===========================\n\n/**\n * Result type for the video activity, based on request type and streaming.\n * - If stream is true (create request): AsyncIterable<StreamChunk>\n * - Otherwise: Promise<VideoJobResult | VideoStatusResult | VideoUrlResult>\n *\n * @experimental Video generation is an experimental feature and may change.\n */\nexport type VideoActivityResult<\n TRequest extends 'create' | 'status' | 'url' = 'create',\n TStream extends boolean = false,\n> = TRequest extends 'status'\n ? Promise<VideoStatusResult>\n : TRequest extends 'url'\n ? Promise<VideoUrlResult>\n : TStream extends true\n ? AsyncIterable<StreamChunk>\n : Promise<VideoJobResult>\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Generate video - creates a video generation job from a text prompt.\n *\n * Uses AI video generation models to create videos based on natural language descriptions.\n * Unlike image generation, video generation is asynchronous and requires polling for completion.\n *\n * When `stream: true` is passed, handles the full job lifecycle automatically:\n * create job → poll for status → stream updates → yield final result.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Create a video generation job\n * ```ts\n * import { generateVideo } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * // Start a video generation job\n * const { jobId } = await generateVideo({\n * adapter: openaiVideo('sora-2'),\n * prompt: 'A cat chasing a dog in a sunny park'\n * })\n *\n * console.log('Job started:', jobId)\n * ```\n *\n * @example Stream the full video generation lifecycle\n * ```ts\n * import { generateVideo, toServerSentEventsResponse } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * const stream = generateVideo({\n * adapter: openaiVideo('sora-2'),\n * prompt: 'A cat chasing a dog in a sunny park',\n * stream: true,\n * pollingInterval: 3000,\n * })\n *\n * return toServerSentEventsResponse(stream)\n * ```\n */\nexport function generateVideo<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n>(\n options: VideoCreateOptions<TAdapter, TStream>,\n): VideoActivityResult<'create', TStream> {\n if (options.stream) {\n return runStreamingVideoGeneration(\n options as VideoCreateOptions<TAdapter, true>,\n ) as VideoActivityResult<'create', TStream>\n }\n\n return runCreateVideoJob(options) as VideoActivityResult<'create', TStream>\n}\n\n/**\n * Internal implementation of non-streaming video job creation.\n */\nasync function runCreateVideoJob<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter, boolean>): Promise<VideoJobResult> {\n const { adapter, prompt, size, duration, modelOptions } = options\n const model = adapter.model\n\n return adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\n })\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n\n/**\n * Internal streaming implementation for video generation.\n * Handles the full job lifecycle: create job → poll for status → stream updates → yield final result.\n */\nasync function* runStreamingVideoGeneration<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: VideoCreateOptions<TAdapter, true>): AsyncIterable<StreamChunk> {\n const { adapter, prompt, size, duration, modelOptions } = options\n const model = adapter.model\n const runId = options.runId ?? createId('run')\n const pollingInterval = options.pollingInterval ?? 2000\n const maxDuration = options.maxDuration ?? 600_000\n\n yield {\n type: 'RUN_STARTED',\n runId,\n timestamp: Date.now(),\n }\n\n try {\n // Create the video generation job\n const jobResult = await adapter.createVideoJob({\n model,\n prompt,\n size,\n duration,\n modelOptions,\n })\n\n yield {\n type: 'CUSTOM',\n name: 'video:job:created',\n value: { jobId: jobResult.jobId },\n timestamp: Date.now(),\n }\n\n // Poll for completion\n const startTime = Date.now()\n while (Date.now() - startTime < maxDuration) {\n await sleep(pollingInterval)\n\n const statusResult = await adapter.getVideoStatus(jobResult.jobId)\n\n yield {\n type: 'CUSTOM',\n name: 'video:status',\n value: {\n jobId: jobResult.jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n },\n timestamp: Date.now(),\n }\n\n if (statusResult.status === 'completed') {\n const urlResult = await adapter.getVideoUrl(jobResult.jobId)\n\n yield {\n type: 'CUSTOM',\n name: 'generation:result',\n value: {\n jobId: jobResult.jobId,\n status: 'completed',\n url: urlResult.url,\n expiresAt: urlResult.expiresAt,\n },\n timestamp: Date.now(),\n }\n\n yield {\n type: 'RUN_FINISHED',\n runId,\n finishReason: 'stop',\n timestamp: Date.now(),\n }\n return\n }\n\n if (statusResult.status === 'failed') {\n throw new Error(statusResult.error || 'Video generation failed')\n }\n }\n\n throw new Error('Video generation timed out')\n } catch (error: any) {\n yield {\n type: 'RUN_ERROR',\n runId,\n error: {\n message: error.message || 'Video generation failed',\n code: error.code,\n },\n timestamp: Date.now(),\n }\n }\n}\n\n/**\n * Get video job status - returns the current status, progress, and URL if available.\n *\n * This function combines status checking and URL retrieval. If the job is completed,\n * it will automatically fetch and include the video URL.\n *\n * @experimental Video generation is an experimental feature and may change.\n *\n * @example Check job status\n * ```ts\n * import { getVideoJobStatus } from '@tanstack/ai'\n * import { openaiVideo } from '@tanstack/ai-openai'\n *\n * const result = await getVideoJobStatus({\n * adapter: openaiVideo('sora-2'),\n * jobId: 'job-123'\n * })\n *\n * console.log('Status:', result.status)\n * console.log('Progress:', result.progress)\n * if (result.url) {\n * console.log('Video URL:', result.url)\n * }\n * ```\n */\nexport async function getVideoJobStatus<\n TAdapter extends VideoAdapter<string, any, any, any>,\n>(options: {\n adapter: TAdapter & { kind: typeof kind }\n jobId: string\n}): Promise<{\n status: 'pending' | 'processing' | 'completed' | 'failed'\n progress?: number\n url?: string\n error?: string\n}> {\n const { adapter, jobId } = options\n const requestId = createId('video-status')\n const startTime = Date.now()\n\n aiEventClient.emit('video:request:started', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n timestamp: startTime,\n })\n\n // Get status first\n const statusResult = await adapter.getVideoStatus(jobId)\n\n // If completed, also get the URL\n if (statusResult.status === 'completed') {\n try {\n const urlResult = await adapter.getVideoUrl(jobId)\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n url: urlResult.url,\n }\n } catch (error) {\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n // If URL fetch fails, still return status\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error:\n error instanceof Error ? error.message : 'Failed to get video URL',\n }\n }\n }\n\n aiEventClient.emit('video:request:completed', {\n requestId,\n provider: adapter.name,\n model: adapter.model,\n requestType: 'status',\n jobId,\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n duration: Date.now() - startTime,\n timestamp: Date.now(),\n })\n\n // Return status for non-completed jobs\n return {\n status: statusResult.status,\n progress: statusResult.progress,\n error: statusResult.error,\n }\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the generateVideo() function without executing.\n */\nexport function createVideoOptions<\n TAdapter extends VideoAdapter<string, any, any, any>,\n TStream extends boolean = false,\n>(\n options: VideoCreateOptions<TAdapter, TStream>,\n): VideoCreateOptions<TAdapter, TStream> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n VideoAdapter,\n VideoAdapterConfig,\n AnyVideoAdapter,\n} from './adapter'\nexport { BaseVideoAdapter } from './adapter'\n"],"names":[],"mappings":";AAuBO,MAAM,OAAO;AA2BpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAwKO,SAAS,cAId,SACwC;AACxC,MAAI,QAAQ,QAAQ;AAClB,WAAO;AAAA,MACL;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO,kBAAkB,OAAO;AAClC;AAKA,eAAe,kBAEb,SAAyE;AACzE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,iBAAiB;AAC1D,QAAM,QAAQ,QAAQ;AAEtB,SAAO,QAAQ,eAAe;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACD;AACH;AAEA,SAAS,MAAM,IAA2B;AACxC,SAAO,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,EAAE,CAAC;AACzD;AAMA,gBAAgB,4BAEd,SAAyE;AACzE,QAAM,EAAE,SAAS,QAAQ,MAAM,UAAU,iBAAiB;AAC1D,QAAM,QAAQ,QAAQ;AACtB,QAAM,QAAQ,QAAQ,SAAS,SAAS,KAAK;AAC7C,QAAM,kBAAkB,QAAQ,mBAAmB;AACnD,QAAM,cAAc,QAAQ,eAAe;AAE3C,QAAM;AAAA,IACJ,MAAM;AAAA,IACN;AAAA,IACA,WAAW,KAAK,IAAA;AAAA,EAAI;AAGtB,MAAI;AAEF,UAAM,YAAY,MAAM,QAAQ,eAAe;AAAA,MAC7C;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACD;AAED,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,MAAM;AAAA,MACN,OAAO,EAAE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AAQ;AAAA,IAClB,OAAO,QAAQ;AAAA,IACf,aAAa;AAAA,IACb;AAAA,IACA,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,IACpB,UAAU,KAAK,IAAA,IAAQ;AAAA,IACvB,WAAW,KAAK,IAAA;AAAA,EAAI,CACrB;AAGD,SAAO;AAAA,IACL,QAAQ,aAAa;AAAA,IACrB,UAAU,aAAa;AAAA,IACvB,OAAO,aAAa;AAAA,EAAA;AAExB;AASO,SAAS,mBAId,SACuC;AACvC,SAAO;AACT;"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StreamChunk } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wrap a one-shot generation result as a StreamChunk async iterable.
|
|
4
|
+
*
|
|
5
|
+
* This allows non-streaming activities (image, speech, transcription, summarize)
|
|
6
|
+
* to be sent over the same streaming transport as chat.
|
|
7
|
+
*
|
|
8
|
+
* @param generator - An async function that performs the generation and returns the result
|
|
9
|
+
* @param options - Optional configuration (runId)
|
|
10
|
+
* @returns An AsyncIterable of StreamChunks with RUN_STARTED, CUSTOM(generation:result), and RUN_FINISHED events
|
|
11
|
+
*/
|
|
12
|
+
export declare function streamGenerationResult<TResult>(generator: () => Promise<TResult>, options?: {
|
|
13
|
+
runId?: string;
|
|
14
|
+
}): AsyncIterable<StreamChunk>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
function createId(prefix) {
|
|
2
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
3
|
+
}
|
|
4
|
+
async function* streamGenerationResult(generator, options) {
|
|
5
|
+
const runId = createId("run");
|
|
6
|
+
yield {
|
|
7
|
+
type: "RUN_STARTED",
|
|
8
|
+
runId,
|
|
9
|
+
timestamp: Date.now()
|
|
10
|
+
};
|
|
11
|
+
try {
|
|
12
|
+
const result = await generator();
|
|
13
|
+
yield {
|
|
14
|
+
type: "CUSTOM",
|
|
15
|
+
name: "generation:result",
|
|
16
|
+
value: result,
|
|
17
|
+
timestamp: Date.now()
|
|
18
|
+
};
|
|
19
|
+
yield {
|
|
20
|
+
type: "RUN_FINISHED",
|
|
21
|
+
runId,
|
|
22
|
+
finishReason: "stop",
|
|
23
|
+
timestamp: Date.now()
|
|
24
|
+
};
|
|
25
|
+
} catch (error) {
|
|
26
|
+
yield {
|
|
27
|
+
type: "RUN_ERROR",
|
|
28
|
+
runId,
|
|
29
|
+
error: {
|
|
30
|
+
message: error.message || "Generation failed",
|
|
31
|
+
code: error.code
|
|
32
|
+
},
|
|
33
|
+
timestamp: Date.now()
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
streamGenerationResult
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=stream-generation-result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-generation-result.js","sources":["../../../src/activities/stream-generation-result.ts"],"sourcesContent":["/**\n * Internal helper for wrapping one-shot generation results as StreamChunk\n * async iterables. NOT exported from the package — used only by activity\n * implementations to support `stream: true`.\n */\n\nimport type { StreamChunk } from '../types'\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n\n/**\n * Wrap a one-shot generation result as a StreamChunk async iterable.\n *\n * This allows non-streaming activities (image, speech, transcription, summarize)\n * to be sent over the same streaming transport as chat.\n *\n * @param generator - An async function that performs the generation and returns the result\n * @param options - Optional configuration (runId)\n * @returns An AsyncIterable of StreamChunks with RUN_STARTED, CUSTOM(generation:result), and RUN_FINISHED events\n */\nexport async function* streamGenerationResult<TResult>(\n generator: () => Promise<TResult>,\n options?: { runId?: string },\n): AsyncIterable<StreamChunk> {\n const runId = options?.runId ?? createId('run')\n\n yield {\n type: 'RUN_STARTED',\n runId,\n timestamp: Date.now(),\n }\n\n try {\n const result = await generator()\n\n yield {\n type: 'CUSTOM',\n name: 'generation:result',\n value: result as unknown,\n timestamp: Date.now(),\n }\n\n yield {\n type: 'RUN_FINISHED',\n runId,\n finishReason: 'stop',\n timestamp: Date.now(),\n }\n } catch (error: any) {\n yield {\n type: 'RUN_ERROR',\n runId,\n error: {\n message: error.message || 'Generation failed',\n code: error.code,\n },\n timestamp: Date.now(),\n }\n }\n}\n"],"names":[],"mappings":"AAQA,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAYA,gBAAuB,uBACrB,WACA,SAC4B;AAC5B,QAAM,QAA0B,SAAS,KAAK;AAE9C,QAAM;AAAA,IACJ,MAAM;AAAA,IACN;AAAA,IACA,WAAW,KAAK,IAAA;AAAA,EAAI;AAGtB,MAAI;AACF,UAAM,SAAS,MAAM,UAAA;AAErB,UAAM;AAAA,MACJ,MAAM;AAAA,MACN,MAAM;AAAA,MACN,OAAO;AAAA,MACP,WAAW,KAAK,IAAA;AAAA,IAAI;AAGtB,UAAM;AAAA,MACJ,MAAM;AAAA,MACN;AAAA,MACA,cAAc;AAAA,MACd,WAAW,KAAK,IAAA;AAAA,IAAI;AAAA,EAExB,SAAS,OAAY;AACnB,UAAM;AAAA,MACJ,MAAM;AAAA,MACN;AAAA,MACA,OAAO;AAAA,QACL,SAAS,MAAM,WAAW;AAAA,QAC1B,MAAM,MAAM;AAAA,MAAA;AAAA,MAEd,WAAW,KAAK,IAAA;AAAA,IAAI;AAAA,EAExB;AACF;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { aiEventClient } from "
|
|
1
|
+
import { aiEventClient } from "@tanstack/ai-event-client";
|
|
2
|
+
import { streamGenerationResult } from "../stream-generation-result.js";
|
|
2
3
|
const kind = "summarize";
|
|
3
4
|
function createId(prefix) {
|
|
4
5
|
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
@@ -62,23 +63,7 @@ async function* runStreamingSummarize(options) {
|
|
|
62
63
|
yield* adapter.summarizeStream(summarizeOptions);
|
|
63
64
|
return;
|
|
64
65
|
}
|
|
65
|
-
|
|
66
|
-
yield {
|
|
67
|
-
type: "TEXT_MESSAGE_CONTENT",
|
|
68
|
-
messageId: result.id,
|
|
69
|
-
model: result.model,
|
|
70
|
-
timestamp: Date.now(),
|
|
71
|
-
delta: result.summary,
|
|
72
|
-
content: result.summary
|
|
73
|
-
};
|
|
74
|
-
yield {
|
|
75
|
-
type: "RUN_FINISHED",
|
|
76
|
-
runId: result.id,
|
|
77
|
-
model: result.model,
|
|
78
|
-
timestamp: Date.now(),
|
|
79
|
-
finishReason: "stop",
|
|
80
|
-
usage: result.usage
|
|
81
|
-
};
|
|
66
|
+
yield* streamGenerationResult(() => adapter.summarize(summarizeOptions));
|
|
82
67
|
}
|
|
83
68
|
function createSummarizeOptions(options) {
|
|
84
69
|
return options;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../src/activities/summarize/index.ts"],"sourcesContent":["/**\n * Summarize Activity\n *\n * Generates summaries from text input.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { aiEventClient } from '
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../src/activities/summarize/index.ts"],"sourcesContent":["/**\n * Summarize Activity\n *\n * Generates summaries from text input.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { aiEventClient } from '@tanstack/ai-event-client'\nimport { streamGenerationResult } from '../stream-generation-result.js'\nimport type { SummarizeAdapter } from './adapter'\nimport type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '../../types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'summarize' as const\n\n// ===========================\n// Type Extraction Helpers\n// ===========================\n\n/** Extract provider options from a SummarizeAdapter via ~types */\nexport type SummarizeProviderOptions<TAdapter> =\n TAdapter extends SummarizeAdapter<any, any>\n ? TAdapter['~types']['providerOptions']\n : object\n\n// ===========================\n// Activity Options Type\n// ===========================\n\n/**\n * Options for the summarize activity.\n * The model is extracted from the adapter's model property.\n *\n * @template TAdapter - The summarize adapter type\n * @template TStream - Whether to stream the output\n */\nexport interface SummarizeActivityOptions<\n TAdapter extends SummarizeAdapter<string, object>,\n TStream extends boolean = false,\n> {\n /** The summarize adapter to use (must be created with a model) */\n adapter: TAdapter & { kind: typeof kind }\n /** The text to summarize */\n text: string\n /** Maximum length of the summary (in words or characters, provider-dependent) */\n maxLength?: number\n /** Style of summary to generate */\n style?: 'bullet-points' | 'paragraph' | 'concise'\n /** Topics or aspects to focus on in the summary */\n focus?: Array<string>\n /** Provider-specific options */\n modelOptions?: SummarizeProviderOptions<TAdapter>\n /**\n * Whether to stream the summarization result.\n * When true, returns an AsyncIterable<StreamChunk> for streaming output.\n * When false or not provided, returns a Promise<SummarizationResult>.\n *\n * @default false\n */\n stream?: TStream\n}\n\n// ===========================\n// Activity Result Type\n// ===========================\n\n/**\n * Result type for the summarize activity.\n * - If stream is true: AsyncIterable<StreamChunk>\n * - Otherwise: Promise<SummarizationResult>\n */\nexport type SummarizeActivityResult<TStream extends boolean> =\n TStream extends true\n ? AsyncIterable<StreamChunk>\n : Promise<SummarizationResult>\n\n// ===========================\n// Helper Functions\n// ===========================\n\nfunction createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n}\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Summarize activity - generates summaries from text.\n *\n * Supports both streaming and non-streaming modes.\n *\n * @example Basic summarization\n * ```ts\n * import { summarize } from '@tanstack/ai'\n * import { openaiSummarize } from '@tanstack/ai-openai'\n *\n * const result = await summarize({\n * adapter: openaiSummarize('gpt-4o-mini'),\n * text: 'Long article text here...'\n * })\n *\n * console.log(result.summary)\n * ```\n *\n * @example Summarization with style\n * ```ts\n * const result = await summarize({\n * adapter: openaiSummarize('gpt-4o-mini'),\n * text: 'Long article text here...',\n * style: 'bullet-points',\n * maxLength: 100\n * })\n * ```\n *\n * @example Focused summarization\n * ```ts\n * const result = await summarize({\n * adapter: openaiSummarize('gpt-4o-mini'),\n * text: 'Long technical document...',\n * focus: ['key findings', 'methodology']\n * })\n * ```\n *\n * @example Streaming summarization\n * ```ts\n * for await (const chunk of summarize({\n * adapter: openaiSummarize('gpt-4o-mini'),\n * text: 'Long article text here...',\n * stream: true\n * })) {\n * if (chunk.type === 'content') {\n * process.stdout.write(chunk.delta)\n * }\n * }\n * ```\n */\nexport function summarize<\n TAdapter extends SummarizeAdapter<string, object>,\n TStream extends boolean = false,\n>(\n options: SummarizeActivityOptions<TAdapter, TStream>,\n): SummarizeActivityResult<TStream> {\n const { stream } = options\n\n if (stream) {\n return runStreamingSummarize(\n options as unknown as SummarizeActivityOptions<\n SummarizeAdapter<string, object>,\n true\n >,\n ) as SummarizeActivityResult<TStream>\n }\n\n return runSummarize(\n options as unknown as SummarizeActivityOptions<\n SummarizeAdapter<string, object>,\n false\n >,\n ) as SummarizeActivityResult<TStream>\n}\n\n/**\n * Run non-streaming summarization\n */\nasync function runSummarize(\n options: SummarizeActivityOptions<SummarizeAdapter<string, object>, false>,\n): Promise<SummarizationResult> {\n const { adapter, text, maxLength, style, focus } = options\n const model = adapter.model\n const requestId = createId('summarize')\n const inputLength = text.length\n const startTime = Date.now()\n\n aiEventClient.emit('summarize:request:started', {\n requestId,\n provider: adapter.name,\n model,\n inputLength,\n timestamp: startTime,\n })\n\n const summarizeOptions: SummarizationOptions = {\n model,\n text,\n maxLength,\n style,\n focus,\n }\n\n const result = await adapter.summarize(summarizeOptions)\n\n const duration = Date.now() - startTime\n const outputLength = result.summary.length\n\n aiEventClient.emit('summarize:request:completed', {\n requestId,\n provider: adapter.name,\n model,\n inputLength,\n outputLength,\n duration,\n timestamp: Date.now(),\n })\n\n return result\n}\n\n/**\n * Run streaming summarization\n * Uses the adapter's native streaming if available, otherwise falls back\n * to non-streaming and yields the result as a single chunk.\n */\nasync function* runStreamingSummarize(\n options: SummarizeActivityOptions<SummarizeAdapter<string, object>, true>,\n): AsyncIterable<StreamChunk> {\n const { adapter, text, maxLength, style, focus } = options\n const model = adapter.model\n\n const summarizeOptions: SummarizationOptions = {\n model,\n text,\n maxLength,\n style,\n focus,\n }\n\n // Use real streaming if the adapter supports it\n if (adapter.summarizeStream) {\n yield* adapter.summarizeStream(summarizeOptions)\n return\n }\n\n // Fall back to non-streaming — wrap result with streamGenerationResult\n yield* streamGenerationResult(() => adapter.summarize(summarizeOptions))\n}\n\n// ===========================\n// Options Factory\n// ===========================\n\n/**\n * Create typed options for the summarize() function without executing.\n */\nexport function createSummarizeOptions<\n TAdapter extends SummarizeAdapter<string, object>,\n TStream extends boolean = false,\n>(\n options: SummarizeActivityOptions<TAdapter, TStream>,\n): SummarizeActivityOptions<TAdapter, TStream> {\n return options\n}\n\n// Re-export adapter types\nexport type {\n SummarizeAdapter,\n SummarizeAdapterConfig,\n AnySummarizeAdapter,\n} from './adapter'\nexport { BaseSummarizeAdapter } from './adapter'\n"],"names":[],"mappings":";;AAqBO,MAAM,OAAO;AAmEpB,SAAS,SAAS,QAAwB;AACxC,SAAO,GAAG,MAAM,IAAI,KAAK,IAAA,CAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,MAAM,GAAG,CAAC,CAAC;AAC1E;AAwDO,SAAS,UAId,SACkC;AAClC,QAAM,EAAE,WAAW;AAEnB,MAAI,QAAQ;AACV,WAAO;AAAA,MACL;AAAA,IAAA;AAAA,EAKJ;AAEA,SAAO;AAAA,IACL;AAAA,EAAA;AAKJ;AAKA,eAAe,aACb,SAC8B;AAC9B,QAAM,EAAE,SAAS,MAAM,WAAW,OAAO,UAAU;AACnD,QAAM,QAAQ,QAAQ;AACtB,QAAM,YAAY,SAAS,WAAW;AACtC,QAAM,cAAc,KAAK;AACzB,QAAM,YAAY,KAAK,IAAA;AAEvB,gBAAc,KAAK,6BAA6B;AAAA,IAC9C;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA;AAAA,IACA,WAAW;AAAA,EAAA,CACZ;AAED,QAAM,mBAAyC;AAAA,IAC7C;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAGF,QAAM,SAAS,MAAM,QAAQ,UAAU,gBAAgB;AAEvD,QAAM,WAAW,KAAK,IAAA,IAAQ;AAC9B,QAAM,eAAe,OAAO,QAAQ;AAEpC,gBAAc,KAAK,+BAA+B;AAAA,IAChD;AAAA,IACA,UAAU,QAAQ;AAAA,IAClB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW,KAAK,IAAA;AAAA,EAAI,CACrB;AAED,SAAO;AACT;AAOA,gBAAgB,sBACd,SAC4B;AAC5B,QAAM,EAAE,SAAS,MAAM,WAAW,OAAO,UAAU;AACnD,QAAM,QAAQ,QAAQ;AAEtB,QAAM,mBAAyC;AAAA,IAC7C;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA;AAIF,MAAI,QAAQ,iBAAiB;AAC3B,WAAO,QAAQ,gBAAgB,gBAAgB;AAC/C;AAAA,EACF;AAGA,SAAO,uBAAuB,MAAM,QAAQ,UAAU,gBAAgB,CAAC;AACzE;AASO,SAAS,uBAId,SAC6C;AAC7C,SAAO;AACT;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -11,9 +11,11 @@ export { convertSchemaToJsonSchema } from './activities/chat/tools/schema-conver
|
|
|
11
11
|
export { streamToText, toServerSentEventsStream, toServerSentEventsResponse, toHttpStream, toHttpResponse, } from './stream-to-response.js';
|
|
12
12
|
export { ToolCallManager } from './activities/chat/tools/tool-calls.js';
|
|
13
13
|
export { maxIterations, untilFinishReason, combineStrategies, } from './activities/chat/agent-loop-strategies.js';
|
|
14
|
+
export type { ChatMiddleware, ChatMiddlewareContext, ChatMiddlewarePhase, ChatMiddlewareConfig, ToolCallHookContext, BeforeToolCallDecision, AfterToolCallInfo, IterationInfo, ToolPhaseCompleteInfo, UsageInfo, FinishInfo, AbortInfo, ErrorInfo, } from './activities/chat/middleware/index.js';
|
|
14
15
|
export * from './types.js';
|
|
15
16
|
export { detectImageMimeType } from './utils.js';
|
|
16
|
-
export
|
|
17
|
+
export { realtimeToken } from './realtime/index.js';
|
|
18
|
+
export type { RealtimeToken, RealtimeTokenAdapter, RealtimeTokenOptions, RealtimeSessionConfig, VADConfig, RealtimeMessage, RealtimeMessagePart, RealtimeTextPart, RealtimeAudioPart, RealtimeToolCallPart, RealtimeToolResultPart, RealtimeImagePart, RealtimeStatus, RealtimeMode, AudioVisualization, RealtimeEvent, RealtimeEventPayloads, RealtimeEventHandler, RealtimeErrorCode, RealtimeError, } from './realtime/index.js';
|
|
17
19
|
export { convertMessagesToModelMessages, generateMessageId, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, } from './activities/chat/messages.js';
|
|
18
20
|
export { StreamProcessor, createReplayStream, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, PartialJSONParser, defaultJSONParser, parsePartialJSON, } from './activities/chat/stream/index.js';
|
|
19
21
|
export type { ChunkStrategy, ChunkRecording, InternalToolCallState, ProcessorResult, ProcessorState, StreamProcessorEvents, StreamProcessorOptions, ToolCallState, ToolResultState, JSONParser, } from './activities/chat/stream/index.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { streamToText, toHttpResponse, toHttpStream, toServerSentEventsResponse,
|
|
|
10
10
|
import { ToolCallManager } from "./activities/chat/tools/tool-calls.js";
|
|
11
11
|
import { combineStrategies, maxIterations, untilFinishReason } from "./activities/chat/agent-loop-strategies.js";
|
|
12
12
|
import { detectImageMimeType } from "./utils.js";
|
|
13
|
-
import {
|
|
13
|
+
import { realtimeToken } from "./realtime/index.js";
|
|
14
14
|
import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./activities/chat/messages.js";
|
|
15
15
|
import { createModel, extendAdapter } from "./extend-adapter.js";
|
|
16
16
|
import { StreamProcessor, createReplayStream } from "./activities/chat/stream/processor.js";
|
|
@@ -25,7 +25,6 @@ export {
|
|
|
25
25
|
StreamProcessor,
|
|
26
26
|
ToolCallManager,
|
|
27
27
|
WordBoundaryStrategy,
|
|
28
|
-
aiEventClient,
|
|
29
28
|
chat,
|
|
30
29
|
combineStrategies,
|
|
31
30
|
convertMessagesToModelMessages,
|
|
@@ -52,6 +51,7 @@ export {
|
|
|
52
51
|
modelMessagesToUIMessages,
|
|
53
52
|
normalizeToUIMessage,
|
|
54
53
|
parsePartialJSON,
|
|
54
|
+
realtimeToken,
|
|
55
55
|
streamToText,
|
|
56
56
|
summarize,
|
|
57
57
|
toHttpResponse,
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ChatMiddleware } from '../activities/chat/middleware/types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A content guard rule — either a regex pattern with replacement, or a transform function.
|
|
4
|
+
*/
|
|
5
|
+
export type ContentGuardRule = {
|
|
6
|
+
pattern: RegExp;
|
|
7
|
+
replacement: string;
|
|
8
|
+
} | {
|
|
9
|
+
fn: (text: string) => string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Information passed to the onFiltered callback.
|
|
13
|
+
*/
|
|
14
|
+
export interface ContentFilteredInfo {
|
|
15
|
+
/** The message ID being filtered */
|
|
16
|
+
messageId: string;
|
|
17
|
+
/** The original text before filtering */
|
|
18
|
+
original: string;
|
|
19
|
+
/** The filtered text after rules applied */
|
|
20
|
+
filtered: string;
|
|
21
|
+
/** Which strategy was used */
|
|
22
|
+
strategy: 'delta' | 'buffered';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for the content guard middleware.
|
|
26
|
+
*/
|
|
27
|
+
export interface ContentGuardMiddlewareOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Rules to apply to text content. Each rule is either a regex pattern
|
|
30
|
+
* with a replacement string, or a custom transform function.
|
|
31
|
+
* Rules are applied in order. Each rule receives the output of the previous.
|
|
32
|
+
*/
|
|
33
|
+
rules: Array<ContentGuardRule>;
|
|
34
|
+
/**
|
|
35
|
+
* Matching strategy:
|
|
36
|
+
* - 'delta': Apply rules to each delta as it arrives. Fast, real-time,
|
|
37
|
+
* but patterns spanning chunk boundaries may be missed.
|
|
38
|
+
* - 'buffered': Accumulate content and apply rules to settled portions,
|
|
39
|
+
* holding back a look-behind buffer to catch cross-boundary patterns.
|
|
40
|
+
*
|
|
41
|
+
* @default 'buffered'
|
|
42
|
+
*/
|
|
43
|
+
strategy?: 'delta' | 'buffered';
|
|
44
|
+
/**
|
|
45
|
+
* Number of characters to hold back before emitting (buffered strategy only).
|
|
46
|
+
* Should be at least as long as the longest pattern you expect to match.
|
|
47
|
+
* Buffer is flushed when the stream ends.
|
|
48
|
+
*
|
|
49
|
+
* @default 50
|
|
50
|
+
*/
|
|
51
|
+
bufferSize?: number;
|
|
52
|
+
/**
|
|
53
|
+
* If true, drop the entire chunk when any rule changes the content.
|
|
54
|
+
* @default false
|
|
55
|
+
*/
|
|
56
|
+
blockOnMatch?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Callback when content is filtered by any rule.
|
|
59
|
+
*/
|
|
60
|
+
onFiltered?: (info: ContentFilteredInfo) => void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Creates a middleware that filters or transforms streamed text content.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { contentGuardMiddleware } from '@tanstack/ai/middlewares'
|
|
68
|
+
*
|
|
69
|
+
* const guard = contentGuardMiddleware({
|
|
70
|
+
* rules: [
|
|
71
|
+
* { pattern: /\b\d{3}-\d{2}-\d{4}\b/g, replacement: '[SSN REDACTED]' },
|
|
72
|
+
* ],
|
|
73
|
+
* strategy: 'buffered',
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function contentGuardMiddleware(options: ContentGuardMiddlewareOptions): ChatMiddleware;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
function applyRules(text, rules) {
|
|
2
|
+
let result = text;
|
|
3
|
+
for (const rule of rules) {
|
|
4
|
+
if ("pattern" in rule) {
|
|
5
|
+
result = result.replace(rule.pattern, rule.replacement);
|
|
6
|
+
} else {
|
|
7
|
+
result = rule.fn(result);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
function contentGuardMiddleware(options) {
|
|
13
|
+
const {
|
|
14
|
+
rules,
|
|
15
|
+
strategy = "buffered",
|
|
16
|
+
bufferSize = 50,
|
|
17
|
+
blockOnMatch = false,
|
|
18
|
+
onFiltered
|
|
19
|
+
} = options;
|
|
20
|
+
if (strategy === "delta") {
|
|
21
|
+
return createDeltaStrategy(rules, blockOnMatch, onFiltered);
|
|
22
|
+
}
|
|
23
|
+
return createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered);
|
|
24
|
+
}
|
|
25
|
+
function createDeltaStrategy(rules, blockOnMatch, onFiltered) {
|
|
26
|
+
return {
|
|
27
|
+
name: "content-guard",
|
|
28
|
+
onChunk(_ctx, chunk) {
|
|
29
|
+
if (chunk.type !== "TEXT_MESSAGE_CONTENT") return;
|
|
30
|
+
const original = chunk.delta;
|
|
31
|
+
const filtered = applyRules(original, rules);
|
|
32
|
+
if (filtered === original) return;
|
|
33
|
+
if (onFiltered) {
|
|
34
|
+
onFiltered({
|
|
35
|
+
messageId: chunk.messageId,
|
|
36
|
+
original,
|
|
37
|
+
filtered,
|
|
38
|
+
strategy: "delta"
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
if (blockOnMatch) return null;
|
|
42
|
+
return {
|
|
43
|
+
...chunk,
|
|
44
|
+
delta: filtered,
|
|
45
|
+
content: void 0
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered) {
|
|
51
|
+
let rawAccumulated = "";
|
|
52
|
+
let emittedFilteredLength = 0;
|
|
53
|
+
let lastMessageId = "";
|
|
54
|
+
function resetState() {
|
|
55
|
+
rawAccumulated = "";
|
|
56
|
+
emittedFilteredLength = 0;
|
|
57
|
+
lastMessageId = "";
|
|
58
|
+
}
|
|
59
|
+
function flushBuffer() {
|
|
60
|
+
if (rawAccumulated.length === 0) return null;
|
|
61
|
+
const filtered = applyRules(rawAccumulated, rules);
|
|
62
|
+
if (blockOnMatch && filtered !== rawAccumulated) {
|
|
63
|
+
if (onFiltered) {
|
|
64
|
+
onFiltered({
|
|
65
|
+
messageId: lastMessageId,
|
|
66
|
+
original: rawAccumulated,
|
|
67
|
+
filtered,
|
|
68
|
+
strategy: "buffered"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
resetState();
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const remaining = filtered.slice(emittedFilteredLength);
|
|
75
|
+
if (remaining.length > 0) {
|
|
76
|
+
if (filtered !== rawAccumulated && onFiltered) {
|
|
77
|
+
onFiltered({
|
|
78
|
+
messageId: lastMessageId,
|
|
79
|
+
original: rawAccumulated,
|
|
80
|
+
filtered,
|
|
81
|
+
strategy: "buffered"
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const flushed = {
|
|
85
|
+
type: "TEXT_MESSAGE_CONTENT",
|
|
86
|
+
messageId: lastMessageId,
|
|
87
|
+
delta: remaining,
|
|
88
|
+
content: filtered,
|
|
89
|
+
timestamp: Date.now()
|
|
90
|
+
};
|
|
91
|
+
resetState();
|
|
92
|
+
return flushed;
|
|
93
|
+
}
|
|
94
|
+
resetState();
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
name: "content-guard",
|
|
99
|
+
onStart() {
|
|
100
|
+
resetState();
|
|
101
|
+
},
|
|
102
|
+
onChunk(_ctx, chunk) {
|
|
103
|
+
if (chunk.type === "TEXT_MESSAGE_END" || chunk.type === "RUN_FINISHED") {
|
|
104
|
+
const flushed = flushBuffer();
|
|
105
|
+
if (flushed) return [flushed, chunk];
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (chunk.type !== "TEXT_MESSAGE_CONTENT") return;
|
|
109
|
+
const pending = [];
|
|
110
|
+
if (lastMessageId && chunk.messageId !== lastMessageId) {
|
|
111
|
+
const flushed = flushBuffer();
|
|
112
|
+
if (flushed) pending.push(flushed);
|
|
113
|
+
}
|
|
114
|
+
rawAccumulated += chunk.delta;
|
|
115
|
+
lastMessageId = chunk.messageId;
|
|
116
|
+
const filtered = applyRules(rawAccumulated, rules);
|
|
117
|
+
const safeFilteredEnd = Math.max(0, filtered.length - bufferSize);
|
|
118
|
+
if (safeFilteredEnd <= emittedFilteredLength) {
|
|
119
|
+
return pending.length > 0 ? pending : null;
|
|
120
|
+
}
|
|
121
|
+
if (blockOnMatch && filtered !== rawAccumulated) {
|
|
122
|
+
if (onFiltered) {
|
|
123
|
+
onFiltered({
|
|
124
|
+
messageId: chunk.messageId,
|
|
125
|
+
original: rawAccumulated,
|
|
126
|
+
filtered,
|
|
127
|
+
strategy: "buffered"
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return pending.length > 0 ? pending : null;
|
|
131
|
+
}
|
|
132
|
+
const newDelta = filtered.slice(emittedFilteredLength, safeFilteredEnd);
|
|
133
|
+
if (filtered !== rawAccumulated && onFiltered) {
|
|
134
|
+
onFiltered({
|
|
135
|
+
messageId: chunk.messageId,
|
|
136
|
+
original: rawAccumulated,
|
|
137
|
+
filtered,
|
|
138
|
+
strategy: "buffered"
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
emittedFilteredLength = safeFilteredEnd;
|
|
142
|
+
const emitChunk = {
|
|
143
|
+
...chunk,
|
|
144
|
+
delta: newDelta,
|
|
145
|
+
content: filtered.slice(0, safeFilteredEnd)
|
|
146
|
+
};
|
|
147
|
+
pending.push(emitChunk);
|
|
148
|
+
return pending.length === 1 ? pending[0] : pending;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
export {
|
|
153
|
+
contentGuardMiddleware
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=content-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-guard.js","sources":["../../../src/middlewares/content-guard.ts"],"sourcesContent":["import type {\n ChatMiddleware,\n ChatMiddlewareContext,\n} from '../activities/chat/middleware/types'\nimport type { StreamChunk } from '../types'\n\n/**\n * A content guard rule — either a regex pattern with replacement, or a transform function.\n */\nexport type ContentGuardRule =\n | { pattern: RegExp; replacement: string }\n | { fn: (text: string) => string }\n\n/**\n * Information passed to the onFiltered callback.\n */\nexport interface ContentFilteredInfo {\n /** The message ID being filtered */\n messageId: string\n /** The original text before filtering */\n original: string\n /** The filtered text after rules applied */\n filtered: string\n /** Which strategy was used */\n strategy: 'delta' | 'buffered'\n}\n\n/**\n * Options for the content guard middleware.\n */\nexport interface ContentGuardMiddlewareOptions {\n /**\n * Rules to apply to text content. Each rule is either a regex pattern\n * with a replacement string, or a custom transform function.\n * Rules are applied in order. Each rule receives the output of the previous.\n */\n rules: Array<ContentGuardRule>\n\n /**\n * Matching strategy:\n * - 'delta': Apply rules to each delta as it arrives. Fast, real-time,\n * but patterns spanning chunk boundaries may be missed.\n * - 'buffered': Accumulate content and apply rules to settled portions,\n * holding back a look-behind buffer to catch cross-boundary patterns.\n *\n * @default 'buffered'\n */\n strategy?: 'delta' | 'buffered'\n\n /**\n * Number of characters to hold back before emitting (buffered strategy only).\n * Should be at least as long as the longest pattern you expect to match.\n * Buffer is flushed when the stream ends.\n *\n * @default 50\n */\n bufferSize?: number\n\n /**\n * If true, drop the entire chunk when any rule changes the content.\n * @default false\n */\n blockOnMatch?: boolean\n\n /**\n * Callback when content is filtered by any rule.\n */\n onFiltered?: (info: ContentFilteredInfo) => void\n}\n\n/**\n * Apply all rules to a string, returning the transformed result.\n */\nfunction applyRules(text: string, rules: Array<ContentGuardRule>): string {\n let result = text\n for (const rule of rules) {\n if ('pattern' in rule) {\n result = result.replace(rule.pattern, rule.replacement)\n } else {\n result = rule.fn(result)\n }\n }\n return result\n}\n\n/**\n * Creates a middleware that filters or transforms streamed text content.\n *\n * @example\n * ```ts\n * import { contentGuardMiddleware } from '@tanstack/ai/middlewares'\n *\n * const guard = contentGuardMiddleware({\n * rules: [\n * { pattern: /\\b\\d{3}-\\d{2}-\\d{4}\\b/g, replacement: '[SSN REDACTED]' },\n * ],\n * strategy: 'buffered',\n * })\n * ```\n */\nexport function contentGuardMiddleware(\n options: ContentGuardMiddlewareOptions,\n): ChatMiddleware {\n const {\n rules,\n strategy = 'buffered',\n bufferSize = 50,\n blockOnMatch = false,\n onFiltered,\n } = options\n\n if (strategy === 'delta') {\n return createDeltaStrategy(rules, blockOnMatch, onFiltered)\n }\n return createBufferedStrategy(rules, bufferSize, blockOnMatch, onFiltered)\n}\n\nfunction createDeltaStrategy(\n rules: Array<ContentGuardRule>,\n blockOnMatch: boolean,\n onFiltered?: (info: ContentFilteredInfo) => void,\n): ChatMiddleware {\n return {\n name: 'content-guard',\n\n onChunk(_ctx: ChatMiddlewareContext, chunk: StreamChunk) {\n if (chunk.type !== 'TEXT_MESSAGE_CONTENT') return\n\n const original = chunk.delta\n const filtered = applyRules(original, rules)\n\n if (filtered === original) return // unchanged, pass through\n\n if (onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original,\n filtered,\n strategy: 'delta',\n })\n }\n\n if (blockOnMatch) return null // drop chunk\n\n return {\n ...chunk,\n delta: filtered,\n content: undefined,\n } as StreamChunk\n },\n }\n}\n\nfunction createBufferedStrategy(\n rules: Array<ContentGuardRule>,\n bufferSize: number,\n blockOnMatch: boolean,\n onFiltered?: (info: ContentFilteredInfo) => void,\n): ChatMiddleware {\n let rawAccumulated = ''\n let emittedFilteredLength = 0\n let lastMessageId = ''\n\n function resetState() {\n rawAccumulated = ''\n emittedFilteredLength = 0\n lastMessageId = ''\n }\n\n function flushBuffer(): StreamChunk | null {\n if (rawAccumulated.length === 0) return null\n\n const filtered = applyRules(rawAccumulated, rules)\n\n if (blockOnMatch && filtered !== rawAccumulated) {\n if (onFiltered) {\n onFiltered({\n messageId: lastMessageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n resetState()\n return null\n }\n\n const remaining = filtered.slice(emittedFilteredLength)\n if (remaining.length > 0) {\n if (filtered !== rawAccumulated && onFiltered) {\n onFiltered({\n messageId: lastMessageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n\n const flushed = {\n type: 'TEXT_MESSAGE_CONTENT',\n messageId: lastMessageId,\n delta: remaining,\n content: filtered,\n timestamp: Date.now(),\n } as StreamChunk\n\n resetState()\n return flushed\n }\n\n resetState()\n return null\n }\n\n return {\n name: 'content-guard',\n\n onStart() {\n resetState()\n },\n\n onChunk(_ctx: ChatMiddlewareContext, chunk: StreamChunk) {\n // Flush buffer on stream end events\n if (chunk.type === 'TEXT_MESSAGE_END' || chunk.type === 'RUN_FINISHED') {\n const flushed = flushBuffer()\n if (flushed) return [flushed, chunk]\n return // pass through end event\n }\n\n if (chunk.type !== 'TEXT_MESSAGE_CONTENT') return // pass through\n\n // Flush buffer on message boundary change\n const pending: Array<StreamChunk> = []\n if (lastMessageId && chunk.messageId !== lastMessageId) {\n const flushed = flushBuffer()\n if (flushed) pending.push(flushed)\n }\n\n rawAccumulated += chunk.delta\n lastMessageId = chunk.messageId\n\n // Apply rules to full accumulated text, buffer in filtered space\n const filtered = applyRules(rawAccumulated, rules)\n const safeFilteredEnd = Math.max(0, filtered.length - bufferSize)\n\n if (safeFilteredEnd <= emittedFilteredLength) {\n return pending.length > 0 ? pending : null\n }\n\n if (blockOnMatch && filtered !== rawAccumulated) {\n if (onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n return pending.length > 0 ? pending : null\n }\n\n const newDelta = filtered.slice(emittedFilteredLength, safeFilteredEnd)\n\n if (filtered !== rawAccumulated && onFiltered) {\n onFiltered({\n messageId: chunk.messageId,\n original: rawAccumulated,\n filtered,\n strategy: 'buffered',\n })\n }\n\n emittedFilteredLength = safeFilteredEnd\n\n const emitChunk = {\n ...chunk,\n delta: newDelta,\n content: filtered.slice(0, safeFilteredEnd),\n } as StreamChunk\n\n pending.push(emitChunk)\n return pending.length === 1 ? pending[0]! : pending\n },\n }\n}\n"],"names":[],"mappings":"AAyEA,SAAS,WAAW,MAAc,OAAwC;AACxE,MAAI,SAAS;AACb,aAAW,QAAQ,OAAO;AACxB,QAAI,aAAa,MAAM;AACrB,eAAS,OAAO,QAAQ,KAAK,SAAS,KAAK,WAAW;AAAA,IACxD,OAAO;AACL,eAAS,KAAK,GAAG,MAAM;AAAA,IACzB;AAAA,EACF;AACA,SAAO;AACT;AAiBO,SAAS,uBACd,SACgB;AAChB,QAAM;AAAA,IACJ;AAAA,IACA,WAAW;AAAA,IACX,aAAa;AAAA,IACb,eAAe;AAAA,IACf;AAAA,EAAA,IACE;AAEJ,MAAI,aAAa,SAAS;AACxB,WAAO,oBAAoB,OAAO,cAAc,UAAU;AAAA,EAC5D;AACA,SAAO,uBAAuB,OAAO,YAAY,cAAc,UAAU;AAC3E;AAEA,SAAS,oBACP,OACA,cACA,YACgB;AAChB,SAAO;AAAA,IACL,MAAM;AAAA,IAEN,QAAQ,MAA6B,OAAoB;AACvD,UAAI,MAAM,SAAS,uBAAwB;AAE3C,YAAM,WAAW,MAAM;AACvB,YAAM,WAAW,WAAW,UAAU,KAAK;AAE3C,UAAI,aAAa,SAAU;AAE3B,UAAI,YAAY;AACd,mBAAW;AAAA,UACT,WAAW,MAAM;AAAA,UACjB;AAAA,UACA;AAAA,UACA,UAAU;AAAA,QAAA,CAC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C;AAAA,MACF;AAEA,UAAI,MAAM,SAAS,uBAAwB;AAG3C,YAAM,UAA8B,CAAA;AACpC,UAAI,iBAAiB,MAAM,cAAc,eAAe;AACtD,cAAM,UAAU,YAAA;AAChB,YAAI,QAAS,SAAQ,KAAK,OAAO;AAAA,MACnC;AAEA,wBAAkB,MAAM;AACxB,sBAAgB,MAAM;AAGtB,YAAM,WAAW,WAAW,gBAAgB,KAAK;AACjD,YAAM,kBAAkB,KAAK,IAAI,GAAG,SAAS,SAAS,UAAU;AAEhE,UAAI,mBAAmB,uBAAuB;AAC5C,eAAO,QAAQ,SAAS,IAAI,UAAU;AAAA,MACxC;AAEA,UAAI,gBAAgB,aAAa,gBAAgB;AAC/C,YAAI,YAAY;AACd,qBAAW;AAAA,YACT,WAAW,MAAM;AAAA,YACjB,UAAU;AAAA,YACV;AAAA,YACA,UAAU;AAAA,UAAA,CACX;AAAA,QACH;AACA,eAAO,QAAQ,SAAS,IAAI,UAAU;AAAA,MACxC;AAEA,YAAM,WAAW,SAAS,MAAM,uBAAuB,eAAe;AAEtE,UAAI,aAAa,kBAAkB,YAAY;AAC7C,mBAAW;AAAA,UACT,WAAW,MAAM;AAAA,UACjB,UAAU;AAAA,UACV;AAAA,UACA,UAAU;AAAA,QAAA,CACX;AAAA,MACH;AAEA,8BAAwB;AAExB,YAAM,YAAY;AAAA,QAChB,GAAG;AAAA,QACH,OAAO;AAAA,QACP,SAAS,SAAS,MAAM,GAAG,eAAe;AAAA,MAAA;AAG5C,cAAQ,KAAK,SAAS;AACtB,aAAO,QAAQ,WAAW,IAAI,QAAQ,CAAC,IAAK;AAAA,IAC9C;AAAA,EAAA;AAEJ;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { toolCacheMiddleware, type ToolCacheMiddlewareOptions, type ToolCacheStorage, type ToolCacheEntry, } from './tool-cache.js';
|
|
2
|
+
export { contentGuardMiddleware, type ContentGuardMiddlewareOptions, type ContentGuardRule, type ContentFilteredInfo, } from './content-guard.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { toolCacheMiddleware, type ToolCacheMiddlewareOptions, type ToolCacheStorage, type ToolCacheEntry, } from '../activities/chat/middleware/tool-cache-middleware.js';
|