@tanstack/ai 0.0.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/esm/activities/chat/adapter.d.ts +100 -0
- package/dist/esm/activities/chat/adapter.js +14 -0
- package/dist/esm/activities/chat/adapter.js.map +1 -0
- package/dist/esm/{utilities → activities/chat}/agent-loop-strategies.d.ts +4 -4
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +1 -0
- package/dist/esm/activities/chat/index.d.ts +166 -0
- package/dist/esm/{core/chat.js → activities/chat/index.js} +131 -33
- package/dist/esm/activities/chat/index.js.map +1 -0
- package/dist/esm/{message-converters.d.ts → activities/chat/messages.d.ts} +1 -1
- package/dist/esm/{message-converters.js → activities/chat/messages.js} +7 -7
- package/dist/esm/activities/chat/messages.js.map +1 -0
- package/dist/esm/activities/chat/stream/json-parser.js.map +1 -0
- package/dist/esm/{stream → activities/chat/stream}/message-updaters.d.ts +1 -1
- package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -0
- package/dist/esm/{stream → activities/chat/stream}/processor.d.ts +1 -1
- package/dist/esm/{stream → activities/chat/stream}/processor.js +1 -1
- package/dist/esm/activities/chat/stream/processor.js.map +1 -0
- package/dist/esm/activities/chat/stream/strategies.js.map +1 -0
- package/dist/esm/{stream → activities/chat/stream}/types.d.ts +2 -9
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +116 -0
- package/dist/esm/activities/chat/tools/schema-converter.js +115 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
- package/dist/esm/{tools → activities/chat/tools}/tool-calls.d.ts +1 -1
- package/dist/esm/{tools → activities/chat/tools}/tool-calls.js +23 -30
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -0
- package/dist/esm/{tools → activities/chat/tools}/tool-definition.d.ts +22 -18
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -0
- package/dist/esm/activities/generateImage/adapter.d.ts +68 -0
- package/dist/esm/activities/generateImage/adapter.js +14 -0
- package/dist/esm/activities/generateImage/adapter.js.map +1 -0
- package/dist/esm/activities/generateImage/index.d.ts +89 -0
- package/dist/esm/activities/generateImage/index.js +15 -0
- package/dist/esm/activities/generateImage/index.js.map +1 -0
- package/dist/esm/activities/generateSpeech/adapter.d.ts +62 -0
- package/dist/esm/activities/generateSpeech/adapter.js +14 -0
- package/dist/esm/activities/generateSpeech/adapter.js.map +1 -0
- package/dist/esm/activities/generateSpeech/index.d.ts +69 -0
- package/dist/esm/activities/generateSpeech/index.js +15 -0
- package/dist/esm/activities/generateSpeech/index.js.map +1 -0
- package/dist/esm/activities/generateTranscription/adapter.d.ts +62 -0
- package/dist/esm/activities/generateTranscription/adapter.js +14 -0
- package/dist/esm/activities/generateTranscription/adapter.js.map +1 -0
- package/dist/esm/activities/generateTranscription/index.d.ts +71 -0
- package/dist/esm/activities/generateTranscription/index.js +15 -0
- package/dist/esm/activities/generateTranscription/index.js.map +1 -0
- package/dist/esm/activities/generateVideo/adapter.d.ts +80 -0
- package/dist/esm/activities/generateVideo/adapter.js +14 -0
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -0
- package/dist/esm/activities/generateVideo/index.d.ts +136 -0
- package/dist/esm/activities/generateVideo/index.js +47 -0
- package/dist/esm/activities/generateVideo/index.js.map +1 -0
- package/dist/esm/activities/index.d.ts +22 -0
- package/dist/esm/activities/index.js +34 -0
- package/dist/esm/activities/index.js.map +1 -0
- package/dist/esm/activities/summarize/adapter.d.ts +74 -0
- package/dist/esm/activities/summarize/adapter.js +14 -0
- package/dist/esm/activities/summarize/adapter.js.map +1 -0
- package/dist/esm/activities/summarize/index.d.ts +100 -0
- package/dist/esm/activities/summarize/index.js +90 -0
- package/dist/esm/activities/summarize/index.js.map +1 -0
- package/dist/esm/event-client.d.ts +4 -18
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +16 -14
- package/dist/esm/index.js +29 -20
- package/dist/esm/stream-to-response.d.ts +95 -0
- package/dist/esm/stream-to-response.js +118 -0
- package/dist/esm/stream-to-response.js.map +1 -0
- package/dist/esm/types.d.ts +370 -133
- package/package.json +7 -6
- package/src/activities/chat/adapter.ts +150 -0
- package/src/{utilities → activities/chat}/agent-loop-strategies.ts +4 -4
- package/src/{core/chat.ts → activities/chat/index.ts} +435 -79
- package/src/{message-converters.ts → activities/chat/messages.ts} +10 -13
- package/src/{stream → activities/chat/stream}/message-updaters.ts +1 -1
- package/src/{stream → activities/chat/stream}/processor.ts +2 -5
- package/src/{stream → activities/chat/stream}/types.ts +8 -18
- package/src/activities/chat/tools/schema-converter.ts +332 -0
- package/src/{tools → activities/chat/tools}/tool-calls.ts +63 -44
- package/src/{tools → activities/chat/tools}/tool-definition.ts +51 -38
- package/src/activities/generateImage/adapter.ts +104 -0
- package/src/activities/generateImage/index.ts +162 -0
- package/src/activities/generateSpeech/adapter.ts +87 -0
- package/src/activities/generateSpeech/index.ts +122 -0
- package/src/activities/generateTranscription/adapter.ts +89 -0
- package/src/activities/generateTranscription/index.ts +132 -0
- package/src/activities/generateVideo/adapter.ts +116 -0
- package/src/activities/generateVideo/index.ts +261 -0
- package/src/activities/index.ts +164 -0
- package/src/activities/summarize/adapter.ts +107 -0
- package/src/activities/summarize/index.ts +287 -0
- package/src/event-client.ts +5 -21
- package/src/index.ts +60 -15
- package/src/stream-to-response.ts +237 -0
- package/src/types.ts +429 -284
- package/dist/esm/base-adapter.d.ts +0 -36
- package/dist/esm/base-adapter.js +0 -12
- package/dist/esm/base-adapter.js.map +0 -1
- package/dist/esm/core/chat-common-options.d.ts +0 -52
- package/dist/esm/core/chat.d.ts +0 -30
- package/dist/esm/core/chat.js.map +0 -1
- package/dist/esm/core/embedding.d.ts +0 -8
- package/dist/esm/core/embedding.js +0 -33
- package/dist/esm/core/embedding.js.map +0 -1
- package/dist/esm/core/summarize.d.ts +0 -9
- package/dist/esm/core/summarize.js +0 -36
- package/dist/esm/core/summarize.js.map +0 -1
- package/dist/esm/message-converters.js.map +0 -1
- package/dist/esm/stream/json-parser.js.map +0 -1
- package/dist/esm/stream/message-updaters.js.map +0 -1
- package/dist/esm/stream/processor.js.map +0 -1
- package/dist/esm/stream/strategies.js.map +0 -1
- package/dist/esm/tools/tool-calls.js.map +0 -1
- package/dist/esm/tools/tool-definition.js.map +0 -1
- package/dist/esm/tools/zod-converter.d.ts +0 -30
- package/dist/esm/tools/zod-converter.js +0 -36
- package/dist/esm/tools/zod-converter.js.map +0 -1
- package/dist/esm/utilities/agent-loop-strategies.js.map +0 -1
- package/dist/esm/utilities/chat-options.d.ts +0 -6
- package/dist/esm/utilities/chat-options.js +0 -7
- package/dist/esm/utilities/chat-options.js.map +0 -1
- package/dist/esm/utilities/messages.d.ts +0 -30
- package/dist/esm/utilities/messages.js +0 -7
- package/dist/esm/utilities/messages.js.map +0 -1
- package/dist/esm/utilities/stream-to-response.d.ts +0 -48
- package/dist/esm/utilities/stream-to-response.js +0 -62
- package/dist/esm/utilities/stream-to-response.js.map +0 -1
- package/src/base-adapter.ts +0 -86
- package/src/core/chat-common-options.ts +0 -55
- package/src/core/embedding.ts +0 -54
- package/src/core/summarize.ts +0 -56
- package/src/tools/zod-converter.ts +0 -85
- package/src/utilities/chat-options.ts +0 -35
- package/src/utilities/messages.ts +0 -63
- package/src/utilities/stream-to-response.ts +0 -116
- /package/dist/esm/{utilities → activities/chat}/agent-loop-strategies.js +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/index.d.ts +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/json-parser.d.ts +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/json-parser.js +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/message-updaters.js +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/strategies.d.ts +0 -0
- /package/dist/esm/{stream → activities/chat/stream}/strategies.js +0 -0
- /package/dist/esm/{tools → activities/chat/tools}/tool-definition.js +0 -0
- /package/src/{stream → activities/chat/stream}/index.ts +0 -0
- /package/src/{stream → activities/chat/stream}/json-parser.ts +0 -0
- /package/src/{stream → activities/chat/stream}/strategies.ts +0 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { chat, kind } from "./chat/index.js";
|
|
2
|
+
import { BaseTextAdapter } from "./chat/adapter.js";
|
|
3
|
+
import { summarize, kind as kind2 } from "./summarize/index.js";
|
|
4
|
+
import { BaseSummarizeAdapter } from "./summarize/adapter.js";
|
|
5
|
+
import { generateImage, kind as kind3 } from "./generateImage/index.js";
|
|
6
|
+
import { BaseImageAdapter } from "./generateImage/adapter.js";
|
|
7
|
+
import { generateVideo, getVideoJobStatus, kind as kind4 } from "./generateVideo/index.js";
|
|
8
|
+
import { BaseVideoAdapter } from "./generateVideo/adapter.js";
|
|
9
|
+
import { generateSpeech, kind as kind5 } from "./generateSpeech/index.js";
|
|
10
|
+
import { BaseTTSAdapter } from "./generateSpeech/adapter.js";
|
|
11
|
+
import { generateTranscription, kind as kind6 } from "./generateTranscription/index.js";
|
|
12
|
+
import { BaseTranscriptionAdapter } from "./generateTranscription/adapter.js";
|
|
13
|
+
export {
|
|
14
|
+
BaseImageAdapter,
|
|
15
|
+
BaseSummarizeAdapter,
|
|
16
|
+
BaseTTSAdapter,
|
|
17
|
+
BaseTextAdapter,
|
|
18
|
+
BaseTranscriptionAdapter,
|
|
19
|
+
BaseVideoAdapter,
|
|
20
|
+
chat,
|
|
21
|
+
generateImage,
|
|
22
|
+
generateSpeech,
|
|
23
|
+
generateTranscription,
|
|
24
|
+
generateVideo,
|
|
25
|
+
getVideoJobStatus,
|
|
26
|
+
kind3 as imageKind,
|
|
27
|
+
summarize,
|
|
28
|
+
kind2 as summarizeKind,
|
|
29
|
+
kind as textKind,
|
|
30
|
+
kind6 as transcriptionKind,
|
|
31
|
+
kind5 as ttsKind,
|
|
32
|
+
kind4 as videoKind
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { StreamChunk, SummarizationOptions, SummarizationResult } from '../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for summarize adapter instances
|
|
4
|
+
*/
|
|
5
|
+
export interface SummarizeAdapterConfig {
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
maxRetries?: number;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Summarize adapter interface with pre-resolved generics.
|
|
14
|
+
*
|
|
15
|
+
* An adapter is created by a provider function: `provider('model')` → `adapter`
|
|
16
|
+
* All type resolution happens at the provider call site, not in this interface.
|
|
17
|
+
*
|
|
18
|
+
* Generic parameters:
|
|
19
|
+
* - TModel: The specific model name (e.g., 'gpt-4o')
|
|
20
|
+
* - TProviderOptions: Provider-specific options (already resolved)
|
|
21
|
+
*/
|
|
22
|
+
export interface SummarizeAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>> {
|
|
23
|
+
/** Discriminator for adapter kind - used by generate() to determine API shape */
|
|
24
|
+
readonly kind: 'summarize';
|
|
25
|
+
/** Adapter name identifier */
|
|
26
|
+
readonly name: string;
|
|
27
|
+
/** The model this adapter is configured for */
|
|
28
|
+
readonly model: TModel;
|
|
29
|
+
/**
|
|
30
|
+
* @internal Type-only properties for inference. Not assigned at runtime.
|
|
31
|
+
*/
|
|
32
|
+
'~types': {
|
|
33
|
+
providerOptions: TProviderOptions;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Summarize the given text
|
|
37
|
+
*/
|
|
38
|
+
summarize: (options: SummarizationOptions) => Promise<SummarizationResult>;
|
|
39
|
+
/**
|
|
40
|
+
* Stream summarization of the given text.
|
|
41
|
+
* Optional - if not implemented, the activity layer will fall back to
|
|
42
|
+
* non-streaming summarize and yield the result as a single chunk.
|
|
43
|
+
*/
|
|
44
|
+
summarizeStream?: (options: SummarizationOptions) => AsyncIterable<StreamChunk>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A SummarizeAdapter with any/unknown type parameters.
|
|
48
|
+
* Useful as a constraint in generic functions and interfaces.
|
|
49
|
+
*/
|
|
50
|
+
export type AnySummarizeAdapter = SummarizeAdapter<any, any>;
|
|
51
|
+
/**
|
|
52
|
+
* Abstract base class for summarize adapters.
|
|
53
|
+
* Extend this class to implement a summarize adapter for a specific provider.
|
|
54
|
+
*
|
|
55
|
+
* Generic parameters match SummarizeAdapter - all pre-resolved by the provider function.
|
|
56
|
+
*/
|
|
57
|
+
export declare abstract class BaseSummarizeAdapter<TModel extends string = string, TProviderOptions extends object = Record<string, unknown>> implements SummarizeAdapter<TModel, TProviderOptions> {
|
|
58
|
+
readonly kind: "summarize";
|
|
59
|
+
abstract readonly name: string;
|
|
60
|
+
readonly model: TModel;
|
|
61
|
+
'~types': {
|
|
62
|
+
providerOptions: TProviderOptions;
|
|
63
|
+
};
|
|
64
|
+
protected config: SummarizeAdapterConfig;
|
|
65
|
+
constructor(config: SummarizeAdapterConfig | undefined, model: TModel);
|
|
66
|
+
abstract summarize(options: SummarizationOptions): Promise<SummarizationResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Stream summarization of the given text.
|
|
69
|
+
* Override this method in concrete implementations to enable streaming.
|
|
70
|
+
* If not overridden, the activity layer will fall back to non-streaming.
|
|
71
|
+
*/
|
|
72
|
+
summarizeStream?(options: SummarizationOptions): AsyncIterable<StreamChunk>;
|
|
73
|
+
protected generateId(): string;
|
|
74
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class BaseSummarizeAdapter {
|
|
2
|
+
constructor(config = {}, model) {
|
|
3
|
+
this.kind = "summarize";
|
|
4
|
+
this.config = config;
|
|
5
|
+
this.model = model;
|
|
6
|
+
}
|
|
7
|
+
generateId() {
|
|
8
|
+
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
BaseSummarizeAdapter
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sources":["../../../../src/activities/summarize/adapter.ts"],"sourcesContent":["import type {\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from '../../types'\n\n/**\n * Configuration for summarize adapter instances\n */\nexport interface SummarizeAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Summarize adapter interface with pre-resolved generics.\n *\n * An adapter is created by a provider function: `provider('model')` → `adapter`\n * All type resolution happens at the provider call site, not in this interface.\n *\n * Generic parameters:\n * - TModel: The specific model name (e.g., 'gpt-4o')\n * - TProviderOptions: Provider-specific options (already resolved)\n */\nexport interface SummarizeAdapter<\n TModel extends string = string,\n TProviderOptions extends object = Record<string, unknown>,\n> {\n /** Discriminator for adapter kind - used by generate() to determine API shape */\n readonly kind: 'summarize'\n /** Adapter name identifier */\n readonly name: string\n /** The model this adapter is configured for */\n readonly model: TModel\n\n /**\n * @internal Type-only properties for inference. Not assigned at runtime.\n */\n '~types': {\n providerOptions: TProviderOptions\n }\n\n /**\n * Summarize the given text\n */\n summarize: (options: SummarizationOptions) => Promise<SummarizationResult>\n\n /**\n * Stream summarization of the given text.\n * Optional - if not implemented, the activity layer will fall back to\n * non-streaming summarize and yield the result as a single chunk.\n */\n summarizeStream?: (\n options: SummarizationOptions,\n ) => AsyncIterable<StreamChunk>\n}\n\n/**\n * A SummarizeAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnySummarizeAdapter = SummarizeAdapter<any, any>\n\n/**\n * Abstract base class for summarize adapters.\n * Extend this class to implement a summarize adapter for a specific provider.\n *\n * Generic parameters match SummarizeAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseSummarizeAdapter<\n TModel extends string = string,\n TProviderOptions extends object = Record<string, unknown>,\n> implements SummarizeAdapter<TModel, TProviderOptions> {\n readonly kind = 'summarize' as const\n abstract readonly name: string\n readonly model: TModel\n\n // Type-only property - never assigned at runtime\n declare '~types': {\n providerOptions: TProviderOptions\n }\n\n protected config: SummarizeAdapterConfig\n\n constructor(config: SummarizeAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract summarize(\n options: SummarizationOptions,\n ): Promise<SummarizationResult>\n\n /**\n * Stream summarization of the given text.\n * Override this method in concrete implementations to enable streaming.\n * If not overridden, the activity layer will fall back to non-streaming.\n */\n summarizeStream?(options: SummarizationOptions): AsyncIterable<StreamChunk>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AAwEO,MAAe,qBAGkC;AAAA,EAYtD,YAAY,SAAiC,CAAA,GAAI,OAAe;AAXhE,SAAS,OAAO;AAYd,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAaU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { SummarizeAdapter } from './adapter.js';
|
|
2
|
+
import { StreamChunk, SummarizationResult } from '../../types.js';
|
|
3
|
+
/** The adapter kind this activity handles */
|
|
4
|
+
export declare const kind: "summarize";
|
|
5
|
+
/** Extract provider options from a SummarizeAdapter via ~types */
|
|
6
|
+
export type SummarizeProviderOptions<TAdapter> = TAdapter extends SummarizeAdapter<any, any> ? TAdapter['~types']['providerOptions'] : object;
|
|
7
|
+
/**
|
|
8
|
+
* Options for the summarize activity.
|
|
9
|
+
* The model is extracted from the adapter's model property.
|
|
10
|
+
*
|
|
11
|
+
* @template TAdapter - The summarize adapter type
|
|
12
|
+
* @template TStream - Whether to stream the output
|
|
13
|
+
*/
|
|
14
|
+
export interface SummarizeActivityOptions<TAdapter extends SummarizeAdapter<string, object>, TStream extends boolean = false> {
|
|
15
|
+
/** The summarize adapter to use (must be created with a model) */
|
|
16
|
+
adapter: TAdapter & {
|
|
17
|
+
kind: typeof kind;
|
|
18
|
+
};
|
|
19
|
+
/** The text to summarize */
|
|
20
|
+
text: string;
|
|
21
|
+
/** Maximum length of the summary (in words or characters, provider-dependent) */
|
|
22
|
+
maxLength?: number;
|
|
23
|
+
/** Style of summary to generate */
|
|
24
|
+
style?: 'bullet-points' | 'paragraph' | 'concise';
|
|
25
|
+
/** Topics or aspects to focus on in the summary */
|
|
26
|
+
focus?: Array<string>;
|
|
27
|
+
/** Provider-specific options */
|
|
28
|
+
modelOptions?: SummarizeProviderOptions<TAdapter>;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to stream the summarization result.
|
|
31
|
+
* When true, returns an AsyncIterable<StreamChunk> for streaming output.
|
|
32
|
+
* When false or not provided, returns a Promise<SummarizationResult>.
|
|
33
|
+
*
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
stream?: TStream;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Result type for the summarize activity.
|
|
40
|
+
* - If stream is true: AsyncIterable<StreamChunk>
|
|
41
|
+
* - Otherwise: Promise<SummarizationResult>
|
|
42
|
+
*/
|
|
43
|
+
export type SummarizeActivityResult<TStream extends boolean> = TStream extends true ? AsyncIterable<StreamChunk> : Promise<SummarizationResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Summarize activity - generates summaries from text.
|
|
46
|
+
*
|
|
47
|
+
* Supports both streaming and non-streaming modes.
|
|
48
|
+
*
|
|
49
|
+
* @example Basic summarization
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { summarize } from '@tanstack/ai'
|
|
52
|
+
* import { openaiSummarize } from '@tanstack/ai-openai'
|
|
53
|
+
*
|
|
54
|
+
* const result = await summarize({
|
|
55
|
+
* adapter: openaiSummarize('gpt-4o-mini'),
|
|
56
|
+
* text: 'Long article text here...'
|
|
57
|
+
* })
|
|
58
|
+
*
|
|
59
|
+
* console.log(result.summary)
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @example Summarization with style
|
|
63
|
+
* ```ts
|
|
64
|
+
* const result = await summarize({
|
|
65
|
+
* adapter: openaiSummarize('gpt-4o-mini'),
|
|
66
|
+
* text: 'Long article text here...',
|
|
67
|
+
* style: 'bullet-points',
|
|
68
|
+
* maxLength: 100
|
|
69
|
+
* })
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @example Focused summarization
|
|
73
|
+
* ```ts
|
|
74
|
+
* const result = await summarize({
|
|
75
|
+
* adapter: openaiSummarize('gpt-4o-mini'),
|
|
76
|
+
* text: 'Long technical document...',
|
|
77
|
+
* focus: ['key findings', 'methodology']
|
|
78
|
+
* })
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Streaming summarization
|
|
82
|
+
* ```ts
|
|
83
|
+
* for await (const chunk of summarize({
|
|
84
|
+
* adapter: openaiSummarize('gpt-4o-mini'),
|
|
85
|
+
* text: 'Long article text here...',
|
|
86
|
+
* stream: true
|
|
87
|
+
* })) {
|
|
88
|
+
* if (chunk.type === 'content') {
|
|
89
|
+
* process.stdout.write(chunk.delta)
|
|
90
|
+
* }
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function summarize<TAdapter extends SummarizeAdapter<string, object>, TStream extends boolean = false>(options: SummarizeActivityOptions<TAdapter, TStream>): SummarizeActivityResult<TStream>;
|
|
95
|
+
/**
|
|
96
|
+
* Create typed options for the summarize() function without executing.
|
|
97
|
+
*/
|
|
98
|
+
export declare function createSummarizeOptions<TAdapter extends SummarizeAdapter<string, object>, TStream extends boolean = false>(options: SummarizeActivityOptions<TAdapter, TStream>): SummarizeActivityOptions<TAdapter, TStream>;
|
|
99
|
+
export type { SummarizeAdapter, SummarizeAdapterConfig, AnySummarizeAdapter, } from './adapter.js';
|
|
100
|
+
export { BaseSummarizeAdapter } from './adapter.js';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { aiEventClient } from "../../event-client.js";
|
|
2
|
+
const kind = "summarize";
|
|
3
|
+
function createId(prefix) {
|
|
4
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
5
|
+
}
|
|
6
|
+
function summarize(options) {
|
|
7
|
+
const { stream } = options;
|
|
8
|
+
if (stream) {
|
|
9
|
+
return runStreamingSummarize(
|
|
10
|
+
options
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
return runSummarize(
|
|
14
|
+
options
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
async function runSummarize(options) {
|
|
18
|
+
const { adapter, text, maxLength, style, focus } = options;
|
|
19
|
+
const model = adapter.model;
|
|
20
|
+
const requestId = createId("summarize");
|
|
21
|
+
const inputLength = text.length;
|
|
22
|
+
const startTime = Date.now();
|
|
23
|
+
aiEventClient.emit("summarize:started", {
|
|
24
|
+
requestId,
|
|
25
|
+
model,
|
|
26
|
+
inputLength,
|
|
27
|
+
timestamp: startTime
|
|
28
|
+
});
|
|
29
|
+
const summarizeOptions = {
|
|
30
|
+
model,
|
|
31
|
+
text,
|
|
32
|
+
maxLength,
|
|
33
|
+
style,
|
|
34
|
+
focus
|
|
35
|
+
};
|
|
36
|
+
const result = await adapter.summarize(summarizeOptions);
|
|
37
|
+
const duration = Date.now() - startTime;
|
|
38
|
+
const outputLength = result.summary.length;
|
|
39
|
+
aiEventClient.emit("summarize:completed", {
|
|
40
|
+
requestId,
|
|
41
|
+
model,
|
|
42
|
+
inputLength,
|
|
43
|
+
outputLength,
|
|
44
|
+
duration,
|
|
45
|
+
timestamp: Date.now()
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
async function* runStreamingSummarize(options) {
|
|
50
|
+
const { adapter, text, maxLength, style, focus } = options;
|
|
51
|
+
const model = adapter.model;
|
|
52
|
+
const summarizeOptions = {
|
|
53
|
+
model,
|
|
54
|
+
text,
|
|
55
|
+
maxLength,
|
|
56
|
+
style,
|
|
57
|
+
focus
|
|
58
|
+
};
|
|
59
|
+
if (adapter.summarizeStream) {
|
|
60
|
+
yield* adapter.summarizeStream(summarizeOptions);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const result = await adapter.summarize(summarizeOptions);
|
|
64
|
+
yield {
|
|
65
|
+
type: "content",
|
|
66
|
+
id: result.id,
|
|
67
|
+
model: result.model,
|
|
68
|
+
timestamp: Date.now(),
|
|
69
|
+
delta: result.summary,
|
|
70
|
+
content: result.summary,
|
|
71
|
+
role: "assistant"
|
|
72
|
+
};
|
|
73
|
+
yield {
|
|
74
|
+
type: "done",
|
|
75
|
+
id: result.id,
|
|
76
|
+
model: result.model,
|
|
77
|
+
timestamp: Date.now(),
|
|
78
|
+
finishReason: "stop",
|
|
79
|
+
usage: result.usage
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function createSummarizeOptions(options) {
|
|
83
|
+
return options;
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
createSummarizeOptions,
|
|
87
|
+
kind,
|
|
88
|
+
summarize
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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 '../../event-client.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:started', {\n requestId,\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:completed', {\n requestId,\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 and yield as a single chunk\n const result = await adapter.summarize(summarizeOptions)\n\n // Yield content chunk with the summary\n yield {\n type: 'content',\n id: result.id,\n model: result.model,\n timestamp: Date.now(),\n delta: result.summary,\n content: result.summary,\n role: 'assistant',\n }\n\n // Yield done chunk\n yield {\n type: 'done',\n id: result.id,\n model: result.model,\n timestamp: Date.now(),\n finishReason: 'stop',\n usage: result.usage,\n }\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":";AAoBO,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,qBAAqB;AAAA,IACtC;AAAA,IACA;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,uBAAuB;AAAA,IACxC;AAAA,IACA;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,QAAM,SAAS,MAAM,QAAQ,UAAU,gBAAgB;AAGvD,QAAM;AAAA,IACJ,MAAM;AAAA,IACN,IAAI,OAAO;AAAA,IACX,OAAO,OAAO;AAAA,IACd,WAAW,KAAK,IAAA;AAAA,IAChB,OAAO,OAAO;AAAA,IACd,SAAS,OAAO;AAAA,IAChB,MAAM;AAAA,EAAA;AAIR,QAAM;AAAA,IACJ,MAAM;AAAA,IACN,IAAI,OAAO;AAAA,IACX,OAAO,OAAO;AAAA,IACd,WAAW,KAAK,IAAA;AAAA,IAChB,cAAc;AAAA,IACd,OAAO,OAAO;AAAA,EAAA;AAElB;AASO,SAAS,uBAId,SAC6C;AAC7C,SAAO;AACT;"}
|
|
@@ -98,7 +98,7 @@ export interface AIDevtoolsEventMap {
|
|
|
98
98
|
duration: number;
|
|
99
99
|
timestamp: number;
|
|
100
100
|
};
|
|
101
|
-
'tanstack-ai-devtools:
|
|
101
|
+
'tanstack-ai-devtools:text:started': {
|
|
102
102
|
requestId: string;
|
|
103
103
|
streamId: string;
|
|
104
104
|
provider: string;
|
|
@@ -110,9 +110,9 @@ export interface AIDevtoolsEventMap {
|
|
|
110
110
|
clientId?: string;
|
|
111
111
|
toolNames?: Array<string>;
|
|
112
112
|
options?: Record<string, unknown>;
|
|
113
|
-
|
|
113
|
+
modelOptions?: Record<string, unknown>;
|
|
114
114
|
};
|
|
115
|
-
'tanstack-ai-devtools:
|
|
115
|
+
'tanstack-ai-devtools:text:completed': {
|
|
116
116
|
requestId: string;
|
|
117
117
|
streamId: string;
|
|
118
118
|
model: string;
|
|
@@ -126,7 +126,7 @@ export interface AIDevtoolsEventMap {
|
|
|
126
126
|
};
|
|
127
127
|
timestamp: number;
|
|
128
128
|
};
|
|
129
|
-
'tanstack-ai-devtools:
|
|
129
|
+
'tanstack-ai-devtools:text:iteration': {
|
|
130
130
|
requestId: string;
|
|
131
131
|
streamId: string;
|
|
132
132
|
iterationNumber: number;
|
|
@@ -146,20 +146,6 @@ export interface AIDevtoolsEventMap {
|
|
|
146
146
|
};
|
|
147
147
|
timestamp: number;
|
|
148
148
|
};
|
|
149
|
-
'tanstack-ai-devtools:embedding:started': {
|
|
150
|
-
requestId: string;
|
|
151
|
-
model: string;
|
|
152
|
-
inputCount: number;
|
|
153
|
-
timestamp: number;
|
|
154
|
-
clientId?: string;
|
|
155
|
-
};
|
|
156
|
-
'tanstack-ai-devtools:embedding:completed': {
|
|
157
|
-
requestId: string;
|
|
158
|
-
model: string;
|
|
159
|
-
inputCount: number;
|
|
160
|
-
duration: number;
|
|
161
|
-
timestamp: number;
|
|
162
|
-
};
|
|
163
149
|
'tanstack-ai-devtools:summarize:started': {
|
|
164
150
|
requestId: string;
|
|
165
151
|
model: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-client.js","sources":["../../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\n\n/**\n * Tool call states - track the lifecycle of a tool call\n * Must match @tanstack/ai-client ToolCallState\n */\ntype ToolCallState =\n | 'awaiting-input' // Received start but no arguments yet\n | 'input-streaming' // Partial arguments received\n | 'input-complete' // All arguments received\n | 'approval-requested' // Waiting for user approval\n | 'approval-responded' // User has approved/denied\n\n/**\n * Tool result states - track the lifecycle of a tool result\n * Must match @tanstack/ai-client ToolResultState\n */\ntype ToolResultState =\n | 'streaming' // Placeholder for future streamed output\n | 'complete' // Result is complete\n | 'error' // Error occurred\n\nexport interface AIDevtoolsEventMap {\n // AI Stream events - from @tanstack/ai package\n 'tanstack-ai-devtools:stream:started': {\n streamId: string\n model: string\n provider: string\n timestamp: number\n clientId?: string\n }\n 'tanstack-ai-devtools:stream:chunk:content': {\n streamId: string\n messageId?: string\n content: string\n delta?: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:tool-call': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n index: number\n arguments: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:tool-result': {\n streamId: string\n messageId?: string\n toolCallId: string\n result: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:done': {\n streamId: string\n messageId?: string\n finishReason: string | null\n usage?: {\n promptTokens: number\n completionTokens: number\n totalTokens: number\n }\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:error': {\n streamId: string\n messageId?: string\n error: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:thinking': {\n streamId: string\n messageId?: string\n content: string\n delta?: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:approval-requested': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:tool-input-available': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n input: any\n timestamp: number\n }\n 'tanstack-ai-devtools:tool:call-completed': {\n requestId: string\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n result: any\n duration: number\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:ended': {\n requestId: string\n streamId: string\n totalChunks: number\n duration: number\n timestamp: number\n }\n 'tanstack-ai-devtools:
|
|
1
|
+
{"version":3,"file":"event-client.js","sources":["../../src/event-client.ts"],"sourcesContent":["import { EventClient } from '@tanstack/devtools-event-client'\n\n/**\n * Tool call states - track the lifecycle of a tool call\n * Must match @tanstack/ai-client ToolCallState\n */\ntype ToolCallState =\n | 'awaiting-input' // Received start but no arguments yet\n | 'input-streaming' // Partial arguments received\n | 'input-complete' // All arguments received\n | 'approval-requested' // Waiting for user approval\n | 'approval-responded' // User has approved/denied\n\n/**\n * Tool result states - track the lifecycle of a tool result\n * Must match @tanstack/ai-client ToolResultState\n */\ntype ToolResultState =\n | 'streaming' // Placeholder for future streamed output\n | 'complete' // Result is complete\n | 'error' // Error occurred\n\nexport interface AIDevtoolsEventMap {\n // AI Stream events - from @tanstack/ai package\n 'tanstack-ai-devtools:stream:started': {\n streamId: string\n model: string\n provider: string\n timestamp: number\n clientId?: string\n }\n 'tanstack-ai-devtools:stream:chunk:content': {\n streamId: string\n messageId?: string\n content: string\n delta?: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:tool-call': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n index: number\n arguments: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:tool-result': {\n streamId: string\n messageId?: string\n toolCallId: string\n result: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:done': {\n streamId: string\n messageId?: string\n finishReason: string | null\n usage?: {\n promptTokens: number\n completionTokens: number\n totalTokens: number\n }\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:error': {\n streamId: string\n messageId?: string\n error: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:chunk:thinking': {\n streamId: string\n messageId?: string\n content: string\n delta?: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:approval-requested': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:tool-input-available': {\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n input: any\n timestamp: number\n }\n 'tanstack-ai-devtools:tool:call-completed': {\n requestId: string\n streamId: string\n messageId?: string\n toolCallId: string\n toolName: string\n result: any\n duration: number\n timestamp: number\n }\n 'tanstack-ai-devtools:stream:ended': {\n requestId: string\n streamId: string\n totalChunks: number\n duration: number\n timestamp: number\n }\n 'tanstack-ai-devtools:text:started': {\n requestId: string\n streamId: string\n provider: string\n model: string\n messageCount: number\n hasTools: boolean\n streaming: boolean\n timestamp: number\n clientId?: string\n toolNames?: Array<string>\n options?: Record<string, unknown>\n modelOptions?: Record<string, unknown>\n }\n 'tanstack-ai-devtools:text:completed': {\n requestId: string\n streamId: string\n model: string\n content: string\n messageId?: string\n finishReason?: string\n usage?: {\n promptTokens: number\n completionTokens: number\n totalTokens: number\n }\n timestamp: number\n }\n 'tanstack-ai-devtools:text:iteration': {\n requestId: string\n streamId: string\n iterationNumber: number\n messageCount: number\n toolCallCount: number\n timestamp: number\n }\n 'tanstack-ai-devtools:usage:tokens': {\n requestId: string\n streamId: string\n model: string\n messageId?: string\n usage: {\n promptTokens: number\n completionTokens: number\n totalTokens: number\n }\n timestamp: number\n }\n\n // Summarize events\n 'tanstack-ai-devtools:summarize:started': {\n requestId: string\n model: string\n inputLength: number\n timestamp: number\n clientId?: string\n }\n 'tanstack-ai-devtools:summarize:completed': {\n requestId: string\n model: string\n inputLength: number\n outputLength: number\n duration: number\n timestamp: number\n }\n\n // Text Client events - from @tanstack/ai-client package\n 'tanstack-ai-devtools:client:created': {\n clientId: string\n initialMessageCount: number\n timestamp: number\n }\n 'tanstack-ai-devtools:client:message-appended': {\n clientId: string\n messageId: string\n role: 'user' | 'assistant' | 'system' | 'tool'\n contentPreview: string\n timestamp: number\n }\n 'tanstack-ai-devtools:client:message-sent': {\n clientId: string\n messageId: string\n content: string\n timestamp: number\n }\n 'tanstack-ai-devtools:client:loading-changed': {\n clientId: string\n isLoading: boolean\n timestamp: number\n }\n 'tanstack-ai-devtools:client:error-changed': {\n clientId: string\n error: string | null\n timestamp: number\n }\n 'tanstack-ai-devtools:client:messages-cleared': {\n clientId: string\n timestamp: number\n }\n 'tanstack-ai-devtools:client:reloaded': {\n clientId: string\n fromMessageIndex: number\n timestamp: number\n }\n 'tanstack-ai-devtools:client:stopped': {\n clientId: string\n timestamp: number\n }\n 'tanstack-ai-devtools:tool:result-added': {\n clientId: string\n toolCallId: string\n toolName: string\n output: any\n state: 'output-available' | 'output-error'\n timestamp: number\n }\n 'tanstack-ai-devtools:tool:approval-responded': {\n clientId: string\n approvalId: string\n toolCallId: string\n approved: boolean\n timestamp: number\n }\n 'tanstack-ai-devtools:processor:text-updated': {\n streamId: string\n content: string\n timestamp: number\n }\n 'tanstack-ai-devtools:processor:tool-call-state-changed': {\n streamId: string\n toolCallId: string\n toolName: string\n state: ToolCallState\n arguments: any\n timestamp: number\n }\n 'tanstack-ai-devtools:processor:tool-result-state-changed': {\n streamId: string\n toolCallId: string\n content: any\n state: ToolResultState\n error?: string\n timestamp: number\n }\n 'tanstack-ai-devtools:client:assistant-message-updated': {\n clientId: string\n messageId: string\n content: string\n timestamp: number\n }\n 'tanstack-ai-devtools:client:tool-call-updated': {\n clientId: string\n messageId: string\n toolCallId: string\n toolName: string\n state: ToolCallState\n arguments: any\n timestamp: number\n }\n 'tanstack-ai-devtools:client:approval-requested': {\n clientId: string\n messageId: string\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n timestamp: number\n }\n}\n\nclass AiEventClient extends EventClient<AIDevtoolsEventMap> {\n constructor() {\n super({\n pluginId: 'tanstack-ai-devtools',\n })\n }\n}\n\nconst aiEventClient = new AiEventClient()\n\nexport { aiEventClient }\n"],"names":[],"mappings":";AA0RA,MAAM,sBAAsB,YAAgC;AAAA,EAC1D,cAAc;AACZ,UAAM;AAAA,MACJ,UAAU;AAAA,IAAA,CACX;AAAA,EACH;AACF;AAEA,MAAM,gBAAgB,IAAI,cAAA;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
export { chat } from './
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
export {
|
|
1
|
+
export { chat, summarize, generateImage, generateVideo, getVideoJobStatus, generateSpeech, generateTranscription, } from './activities/index.js';
|
|
2
|
+
export { createChatOptions } from './activities/chat/index.js';
|
|
3
|
+
export { createSummarizeOptions } from './activities/summarize/index.js';
|
|
4
|
+
export { createImageOptions } from './activities/generateImage/index.js';
|
|
5
|
+
export { createVideoOptions } from './activities/generateVideo/index.js';
|
|
6
|
+
export { createSpeechOptions } from './activities/generateSpeech/index.js';
|
|
7
|
+
export { createTranscriptionOptions } from './activities/generateTranscription/index.js';
|
|
8
|
+
export type { AIAdapter, ImageAdapter, AnyImageAdapter, TextAdapter, AnyTextAdapter, AnySummarizeAdapter, SummarizeAdapter, AnyTTSAdapter, TTSAdapter, AnyTranscriptionAdapter, TranscriptionAdapter, AnyVideoAdapter, VideoAdapter, } from './activities/index.js';
|
|
9
|
+
export { toolDefinition, type ToolDefinition, type ToolDefinitionInstance, type ToolDefinitionConfig, type ServerTool, type ClientTool, type AnyClientTool, type InferToolName, type InferToolInput, type InferToolOutput, } from './activities/chat/tools/tool-definition.js';
|
|
10
|
+
export { convertSchemaToJsonSchema } from './activities/chat/tools/schema-converter.js';
|
|
11
|
+
export { streamToText, toServerSentEventsStream, toStreamResponse, toHttpStream, } from './stream-to-response.js';
|
|
12
|
+
export { ToolCallManager } from './activities/chat/tools/tool-calls.js';
|
|
13
|
+
export { maxIterations, untilFinishReason, combineStrategies, } from './activities/chat/agent-loop-strategies.js';
|
|
10
14
|
export * from './types.js';
|
|
11
|
-
export { chatOptions } from './utilities/chat-options.js';
|
|
12
|
-
export { messages } from './utilities/messages.js';
|
|
13
15
|
export { aiEventClient } from './event-client.js';
|
|
14
|
-
export { convertMessagesToModelMessages, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage,
|
|
15
|
-
export { StreamProcessor, createReplayStream, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, PartialJSONParser, defaultJSONParser, parsePartialJSON, } from './stream.js';
|
|
16
|
-
export type { ChunkStrategy, ChunkRecording, InternalToolCallState, ProcessorResult, ProcessorState, StreamProcessorEvents, StreamProcessorHandlers, StreamProcessorOptions, ToolCallState, ToolResultState, JSONParser, } from './stream.js';
|
|
16
|
+
export { convertMessagesToModelMessages, generateMessageId, uiMessageToModelMessages, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, } from './activities/chat/messages.js';
|
|
17
|
+
export { StreamProcessor, createReplayStream, ImmediateStrategy, PunctuationStrategy, BatchStrategy, WordBoundaryStrategy, CompositeStrategy, PartialJSONParser, defaultJSONParser, parsePartialJSON, } from './activities/chat/stream/index.js';
|
|
18
|
+
export type { ChunkStrategy, ChunkRecording, InternalToolCallState, ProcessorResult, ProcessorState, StreamProcessorEvents, StreamProcessorHandlers, StreamProcessorOptions, ToolCallState, ToolResultState, JSONParser, } from './activities/chat/stream/index.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { chat } from "./
|
|
2
|
-
import { summarize } from "./
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
1
|
+
import { chat, createChatOptions } from "./activities/chat/index.js";
|
|
2
|
+
import { createSummarizeOptions, summarize } from "./activities/summarize/index.js";
|
|
3
|
+
import { createImageOptions, generateImage } from "./activities/generateImage/index.js";
|
|
4
|
+
import { createVideoOptions, generateVideo, getVideoJobStatus } from "./activities/generateVideo/index.js";
|
|
5
|
+
import { createSpeechOptions, generateSpeech } from "./activities/generateSpeech/index.js";
|
|
6
|
+
import { createTranscriptionOptions, generateTranscription } from "./activities/generateTranscription/index.js";
|
|
7
|
+
import { toolDefinition } from "./activities/chat/tools/tool-definition.js";
|
|
8
|
+
import { convertSchemaToJsonSchema } from "./activities/chat/tools/schema-converter.js";
|
|
9
|
+
import { streamToText, toHttpStream, toServerSentEventsStream, toStreamResponse } from "./stream-to-response.js";
|
|
10
|
+
import { ToolCallManager } from "./activities/chat/tools/tool-calls.js";
|
|
11
|
+
import { combineStrategies, maxIterations, untilFinishReason } from "./activities/chat/agent-loop-strategies.js";
|
|
12
12
|
import { aiEventClient } from "./event-client.js";
|
|
13
|
-
import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./
|
|
14
|
-
import { StreamProcessor, createReplayStream } from "./stream/processor.js";
|
|
15
|
-
import { BatchStrategy, CompositeStrategy, ImmediateStrategy, PunctuationStrategy, WordBoundaryStrategy } from "./stream/strategies.js";
|
|
16
|
-
import { PartialJSONParser, defaultJSONParser, parsePartialJSON } from "./stream/json-parser.js";
|
|
13
|
+
import { convertMessagesToModelMessages, generateMessageId, modelMessageToUIMessage, modelMessagesToUIMessages, normalizeToUIMessage, uiMessageToModelMessages } from "./activities/chat/messages.js";
|
|
14
|
+
import { StreamProcessor, createReplayStream } from "./activities/chat/stream/processor.js";
|
|
15
|
+
import { BatchStrategy, CompositeStrategy, ImmediateStrategy, PunctuationStrategy, WordBoundaryStrategy } from "./activities/chat/stream/strategies.js";
|
|
16
|
+
import { PartialJSONParser, defaultJSONParser, parsePartialJSON } from "./activities/chat/stream/json-parser.js";
|
|
17
17
|
export {
|
|
18
|
-
BaseAdapter,
|
|
19
18
|
BatchStrategy,
|
|
20
19
|
CompositeStrategy,
|
|
21
20
|
ImmediateStrategy,
|
|
@@ -26,21 +25,31 @@ export {
|
|
|
26
25
|
WordBoundaryStrategy,
|
|
27
26
|
aiEventClient,
|
|
28
27
|
chat,
|
|
29
|
-
chatOptions,
|
|
30
28
|
combineStrategies,
|
|
31
29
|
convertMessagesToModelMessages,
|
|
32
|
-
|
|
30
|
+
convertSchemaToJsonSchema,
|
|
31
|
+
createChatOptions,
|
|
32
|
+
createImageOptions,
|
|
33
33
|
createReplayStream,
|
|
34
|
+
createSpeechOptions,
|
|
35
|
+
createSummarizeOptions,
|
|
36
|
+
createTranscriptionOptions,
|
|
37
|
+
createVideoOptions,
|
|
34
38
|
defaultJSONParser,
|
|
35
|
-
|
|
39
|
+
generateImage,
|
|
36
40
|
generateMessageId,
|
|
41
|
+
generateSpeech,
|
|
42
|
+
generateTranscription,
|
|
43
|
+
generateVideo,
|
|
44
|
+
getVideoJobStatus,
|
|
37
45
|
maxIterations,
|
|
38
|
-
messages,
|
|
39
46
|
modelMessageToUIMessage,
|
|
40
47
|
modelMessagesToUIMessages,
|
|
41
48
|
normalizeToUIMessage,
|
|
42
49
|
parsePartialJSON,
|
|
50
|
+
streamToText,
|
|
43
51
|
summarize,
|
|
52
|
+
toHttpStream,
|
|
44
53
|
toServerSentEventsStream,
|
|
45
54
|
toStreamResponse,
|
|
46
55
|
toolDefinition,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { StreamChunk } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Collect all text content from a StreamChunk async iterable and return as a string.
|
|
4
|
+
*
|
|
5
|
+
* This function consumes the entire stream, accumulating content from 'content' type chunks,
|
|
6
|
+
* and returns the final concatenated text.
|
|
7
|
+
*
|
|
8
|
+
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
9
|
+
* @returns Promise<string> - The accumulated text content
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const stream = chat({
|
|
14
|
+
* adapter: openaiText(),
|
|
15
|
+
* model: 'gpt-4o',
|
|
16
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
17
|
+
* });
|
|
18
|
+
* const text = await streamToText(stream);
|
|
19
|
+
* console.log(text); // "Hello! How can I help you today?"
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare function streamToText(stream: AsyncIterable<StreamChunk>): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Convert a StreamChunk async iterable to a ReadableStream in Server-Sent Events format
|
|
25
|
+
*
|
|
26
|
+
* This creates a ReadableStream that emits chunks in SSE format:
|
|
27
|
+
* - Each chunk is prefixed with "data: "
|
|
28
|
+
* - Each chunk is followed by "\n\n"
|
|
29
|
+
* - Stream ends with "data: [DONE]\n\n"
|
|
30
|
+
*
|
|
31
|
+
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
32
|
+
* @param abortController - Optional AbortController to abort when stream is cancelled
|
|
33
|
+
* @returns ReadableStream in Server-Sent Events format
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
|
|
38
|
+
* const readableStream = toServerSentEventsStream(stream);
|
|
39
|
+
* // Use with Response, or any API that accepts ReadableStream
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function toServerSentEventsStream(stream: AsyncIterable<StreamChunk>, abortController?: AbortController): ReadableStream<Uint8Array>;
|
|
43
|
+
/**
|
|
44
|
+
* Convert a StreamChunk async iterable to a ReadableStream in HTTP stream format (newline-delimited JSON)
|
|
45
|
+
*
|
|
46
|
+
* This creates a ReadableStream that emits chunks as newline-delimited JSON:
|
|
47
|
+
* - Each chunk is JSON.stringify'd and followed by "\n"
|
|
48
|
+
* - No SSE formatting (no "data: " prefix)
|
|
49
|
+
*
|
|
50
|
+
* This format is compatible with `fetchHttpStream` connection adapter.
|
|
51
|
+
*
|
|
52
|
+
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
53
|
+
* @param abortController - Optional AbortController to abort when stream is cancelled
|
|
54
|
+
* @returns ReadableStream in HTTP stream format (newline-delimited JSON)
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
|
|
59
|
+
* const readableStream = toHttpStream(stream);
|
|
60
|
+
* // Use with Response for HTTP streaming (not SSE)
|
|
61
|
+
* return new Response(readableStream, {
|
|
62
|
+
* headers: { 'Content-Type': 'application/x-ndjson' }
|
|
63
|
+
* });
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export declare function toHttpStream(stream: AsyncIterable<StreamChunk>, abortController?: AbortController): ReadableStream<Uint8Array>;
|
|
67
|
+
/**
|
|
68
|
+
* Create a streaming HTTP response from a StreamChunk async iterable
|
|
69
|
+
* Includes proper headers for Server-Sent Events
|
|
70
|
+
*
|
|
71
|
+
* @deprecated Use `toServerSentEventsStream` instead. This function will be removed in a future version.
|
|
72
|
+
*
|
|
73
|
+
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
74
|
+
* @param init - Optional Response initialization options
|
|
75
|
+
* @param abortController - Optional AbortController to abort when client disconnects
|
|
76
|
+
* @returns Response object with SSE headers and streaming body
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* export async function POST(request: Request) {
|
|
81
|
+
* const { messages } = await request.json();
|
|
82
|
+
* const abortController = new AbortController();
|
|
83
|
+
* const stream = chat({
|
|
84
|
+
* adapter: openaiText(),
|
|
85
|
+
* model: "gpt-4o",
|
|
86
|
+
* messages,
|
|
87
|
+
* options: { abortSignal: abortController.signal }
|
|
88
|
+
* });
|
|
89
|
+
* return toStreamResponse(stream, undefined, abortController);
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function toStreamResponse(stream: AsyncIterable<StreamChunk>, init?: ResponseInit & {
|
|
94
|
+
abortController?: AbortController;
|
|
95
|
+
}): Response;
|