@tanstack/ai 0.0.2 → 0.1.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 +165 -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/{tools → activities/chat/tools}/tool-calls.d.ts +1 -1
- package/dist/esm/{tools → activities/chat/tools}/tool-calls.js +9 -5
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -0
- package/dist/esm/{tools → activities/chat/tools}/tool-definition.d.ts +14 -14
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -0
- package/dist/esm/activities/chat/tools/zod-converter.d.ts +69 -0
- package/dist/esm/activities/chat/tools/zod-converter.js +99 -0
- package/dist/esm/activities/chat/tools/zod-converter.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 -45
- package/dist/esm/event-client.js +0 -49
- package/dist/esm/event-client.js.map +1 -1
- package/dist/esm/index.d.ts +16 -14
- package/dist/esm/index.js +28 -19
- 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 +347 -129
- package/package.json +6 -2
- 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} +427 -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/{tools → activities/chat/tools}/tool-calls.ts +36 -11
- package/src/{tools → activities/chat/tools}/tool-definition.ts +36 -27
- package/src/activities/chat/tools/zod-converter.ts +235 -0
- 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 -101
- package/src/index.ts +58 -15
- package/src/stream-to-response.ts +237 -0
- package/src/types.ts +404 -280
- 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,116 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
VideoGenerationOptions,
|
|
3
|
+
VideoJobResult,
|
|
4
|
+
VideoStatusResult,
|
|
5
|
+
VideoUrlResult,
|
|
6
|
+
} from '../../types'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for video adapter instances
|
|
10
|
+
*
|
|
11
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
12
|
+
*/
|
|
13
|
+
export interface VideoAdapterConfig {
|
|
14
|
+
apiKey?: string
|
|
15
|
+
baseUrl?: string
|
|
16
|
+
timeout?: number
|
|
17
|
+
maxRetries?: number
|
|
18
|
+
headers?: Record<string, string>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Video adapter interface with pre-resolved generics.
|
|
23
|
+
*
|
|
24
|
+
* An adapter is created by a provider function: `provider('model')` → `adapter`
|
|
25
|
+
* All type resolution happens at the provider call site, not in this interface.
|
|
26
|
+
*
|
|
27
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
28
|
+
*
|
|
29
|
+
* Generic parameters:
|
|
30
|
+
* - TModel: The specific model name (e.g., 'sora-2')
|
|
31
|
+
* - TProviderOptions: Provider-specific options (already resolved)
|
|
32
|
+
*/
|
|
33
|
+
export interface VideoAdapter<
|
|
34
|
+
TModel extends string = string,
|
|
35
|
+
TProviderOptions extends object = Record<string, unknown>,
|
|
36
|
+
> {
|
|
37
|
+
/** Discriminator for adapter kind - used to determine API shape */
|
|
38
|
+
readonly kind: 'video'
|
|
39
|
+
/** Adapter name identifier */
|
|
40
|
+
readonly name: string
|
|
41
|
+
/** The model this adapter is configured for */
|
|
42
|
+
readonly model: TModel
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @internal Type-only properties for inference. Not assigned at runtime.
|
|
46
|
+
*/
|
|
47
|
+
'~types': {
|
|
48
|
+
providerOptions: TProviderOptions
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Create a new video generation job.
|
|
53
|
+
* Returns a job ID that can be used to poll for status and retrieve the video.
|
|
54
|
+
*/
|
|
55
|
+
createVideoJob: (
|
|
56
|
+
options: VideoGenerationOptions<TProviderOptions>,
|
|
57
|
+
) => Promise<VideoJobResult>
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get the current status of a video generation job.
|
|
61
|
+
*/
|
|
62
|
+
getVideoStatus: (jobId: string) => Promise<VideoStatusResult>
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get the URL to download/view the generated video.
|
|
66
|
+
* Should only be called after status is 'completed'.
|
|
67
|
+
*/
|
|
68
|
+
getVideoUrl: (jobId: string) => Promise<VideoUrlResult>
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A VideoAdapter with any/unknown type parameters.
|
|
73
|
+
* Useful as a constraint in generic functions and interfaces.
|
|
74
|
+
*/
|
|
75
|
+
export type AnyVideoAdapter = VideoAdapter<any, any>
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Abstract base class for video generation adapters.
|
|
79
|
+
* Extend this class to implement a video adapter for a specific provider.
|
|
80
|
+
*
|
|
81
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
82
|
+
*
|
|
83
|
+
* Generic parameters match VideoAdapter - all pre-resolved by the provider function.
|
|
84
|
+
*/
|
|
85
|
+
export abstract class BaseVideoAdapter<
|
|
86
|
+
TModel extends string = string,
|
|
87
|
+
TProviderOptions extends object = Record<string, unknown>,
|
|
88
|
+
> implements VideoAdapter<TModel, TProviderOptions> {
|
|
89
|
+
readonly kind = 'video' as const
|
|
90
|
+
abstract readonly name: string
|
|
91
|
+
readonly model: TModel
|
|
92
|
+
|
|
93
|
+
// Type-only property - never assigned at runtime
|
|
94
|
+
declare '~types': {
|
|
95
|
+
providerOptions: TProviderOptions
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
protected config: VideoAdapterConfig
|
|
99
|
+
|
|
100
|
+
constructor(config: VideoAdapterConfig = {}, model: TModel) {
|
|
101
|
+
this.config = config
|
|
102
|
+
this.model = model
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
abstract createVideoJob(
|
|
106
|
+
options: VideoGenerationOptions<TProviderOptions>,
|
|
107
|
+
): Promise<VideoJobResult>
|
|
108
|
+
|
|
109
|
+
abstract getVideoStatus(jobId: string): Promise<VideoStatusResult>
|
|
110
|
+
|
|
111
|
+
abstract getVideoUrl(jobId: string): Promise<VideoUrlResult>
|
|
112
|
+
|
|
113
|
+
protected generateId(): string {
|
|
114
|
+
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Video Activity (Experimental)
|
|
3
|
+
*
|
|
4
|
+
* Generates videos from text prompts using a jobs/polling architecture.
|
|
5
|
+
* This is a self-contained module with implementation, types, and JSDoc.
|
|
6
|
+
*
|
|
7
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { VideoAdapter } from './adapter'
|
|
11
|
+
import type {
|
|
12
|
+
VideoJobResult,
|
|
13
|
+
VideoStatusResult,
|
|
14
|
+
VideoUrlResult,
|
|
15
|
+
} from '../../types'
|
|
16
|
+
|
|
17
|
+
// ===========================
|
|
18
|
+
// Activity Kind
|
|
19
|
+
// ===========================
|
|
20
|
+
|
|
21
|
+
/** The adapter kind this activity handles */
|
|
22
|
+
export const kind = 'video' as const
|
|
23
|
+
|
|
24
|
+
// ===========================
|
|
25
|
+
// Type Extraction Helpers
|
|
26
|
+
// ===========================
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extract provider options from a VideoAdapter via ~types.
|
|
30
|
+
*/
|
|
31
|
+
export type VideoProviderOptions<TAdapter> =
|
|
32
|
+
TAdapter extends VideoAdapter<any, any>
|
|
33
|
+
? TAdapter['~types']['providerOptions']
|
|
34
|
+
: object
|
|
35
|
+
|
|
36
|
+
// ===========================
|
|
37
|
+
// Activity Options Types
|
|
38
|
+
// ===========================
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Base options shared by all video activity operations.
|
|
42
|
+
* The model is extracted from the adapter's model property.
|
|
43
|
+
*/
|
|
44
|
+
interface VideoActivityBaseOptions<
|
|
45
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
46
|
+
> {
|
|
47
|
+
/** The video adapter to use (must be created with a model) */
|
|
48
|
+
adapter: TAdapter & { kind: typeof kind }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Options for creating a new video generation job.
|
|
53
|
+
* The model is extracted from the adapter's model property.
|
|
54
|
+
*
|
|
55
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
56
|
+
*/
|
|
57
|
+
export interface VideoCreateOptions<
|
|
58
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
59
|
+
> extends VideoActivityBaseOptions<TAdapter> {
|
|
60
|
+
/** Request type - create a new job (default if not specified) */
|
|
61
|
+
request?: 'create'
|
|
62
|
+
/** Text description of the desired video */
|
|
63
|
+
prompt: string
|
|
64
|
+
/** Video size in WIDTHxHEIGHT format (e.g., "1280x720") */
|
|
65
|
+
size?: string
|
|
66
|
+
/** Video duration in seconds */
|
|
67
|
+
duration?: number
|
|
68
|
+
/** Provider-specific options for video generation */
|
|
69
|
+
modelOptions?: VideoProviderOptions<TAdapter>
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Options for polling the status of a video generation job.
|
|
74
|
+
*
|
|
75
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
76
|
+
*/
|
|
77
|
+
export interface VideoStatusOptions<
|
|
78
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
79
|
+
> extends VideoActivityBaseOptions<TAdapter> {
|
|
80
|
+
/** Request type - get job status */
|
|
81
|
+
request: 'status'
|
|
82
|
+
/** The job ID to check status for */
|
|
83
|
+
jobId: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Options for getting the URL of a completed video.
|
|
88
|
+
*
|
|
89
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
90
|
+
*/
|
|
91
|
+
export interface VideoUrlOptions<
|
|
92
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
93
|
+
> extends VideoActivityBaseOptions<TAdapter> {
|
|
94
|
+
/** Request type - get video URL */
|
|
95
|
+
request: 'url'
|
|
96
|
+
/** The job ID to get URL for */
|
|
97
|
+
jobId: string
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Union type for all video activity options.
|
|
102
|
+
* Discriminated by the `request` field.
|
|
103
|
+
*
|
|
104
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
105
|
+
*/
|
|
106
|
+
export type VideoActivityOptions<
|
|
107
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
108
|
+
TRequest extends 'create' | 'status' | 'url' = 'create',
|
|
109
|
+
> = TRequest extends 'status'
|
|
110
|
+
? VideoStatusOptions<TAdapter>
|
|
111
|
+
: TRequest extends 'url'
|
|
112
|
+
? VideoUrlOptions<TAdapter>
|
|
113
|
+
: VideoCreateOptions<TAdapter>
|
|
114
|
+
|
|
115
|
+
// ===========================
|
|
116
|
+
// Activity Result Types
|
|
117
|
+
// ===========================
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Result type for the video activity, based on request type.
|
|
121
|
+
*
|
|
122
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
123
|
+
*/
|
|
124
|
+
export type VideoActivityResult<
|
|
125
|
+
TRequest extends 'create' | 'status' | 'url' = 'create',
|
|
126
|
+
> = TRequest extends 'status'
|
|
127
|
+
? Promise<VideoStatusResult>
|
|
128
|
+
: TRequest extends 'url'
|
|
129
|
+
? Promise<VideoUrlResult>
|
|
130
|
+
: Promise<VideoJobResult>
|
|
131
|
+
|
|
132
|
+
// ===========================
|
|
133
|
+
// Activity Implementation
|
|
134
|
+
// ===========================
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Generate video - creates a video generation job from a text prompt.
|
|
138
|
+
*
|
|
139
|
+
* Uses AI video generation models to create videos based on natural language descriptions.
|
|
140
|
+
* Unlike image generation, video generation is asynchronous and requires polling for completion.
|
|
141
|
+
*
|
|
142
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
143
|
+
*
|
|
144
|
+
* @example Create a video generation job
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { generateVideo } from '@tanstack/ai'
|
|
147
|
+
* import { openaiVideo } from '@tanstack/ai-openai'
|
|
148
|
+
*
|
|
149
|
+
* // Start a video generation job
|
|
150
|
+
* const { jobId } = await generateVideo({
|
|
151
|
+
* adapter: openaiVideo('sora-2'),
|
|
152
|
+
* prompt: 'A cat chasing a dog in a sunny park'
|
|
153
|
+
* })
|
|
154
|
+
*
|
|
155
|
+
* console.log('Job started:', jobId)
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
export async function generateVideo<
|
|
159
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
160
|
+
>(options: VideoCreateOptions<TAdapter>): Promise<VideoJobResult> {
|
|
161
|
+
const { adapter, prompt, size, duration, modelOptions } = options
|
|
162
|
+
const model = adapter.model
|
|
163
|
+
|
|
164
|
+
return adapter.createVideoJob({
|
|
165
|
+
model,
|
|
166
|
+
prompt,
|
|
167
|
+
size,
|
|
168
|
+
duration,
|
|
169
|
+
modelOptions,
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get video job status - returns the current status, progress, and URL if available.
|
|
175
|
+
*
|
|
176
|
+
* This function combines status checking and URL retrieval. If the job is completed,
|
|
177
|
+
* it will automatically fetch and include the video URL.
|
|
178
|
+
*
|
|
179
|
+
* @experimental Video generation is an experimental feature and may change.
|
|
180
|
+
*
|
|
181
|
+
* @example Check job status
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { getVideoJobStatus } from '@tanstack/ai'
|
|
184
|
+
* import { openaiVideo } from '@tanstack/ai-openai'
|
|
185
|
+
*
|
|
186
|
+
* const result = await getVideoJobStatus({
|
|
187
|
+
* adapter: openaiVideo('sora-2'),
|
|
188
|
+
* jobId: 'job-123'
|
|
189
|
+
* })
|
|
190
|
+
*
|
|
191
|
+
* console.log('Status:', result.status)
|
|
192
|
+
* console.log('Progress:', result.progress)
|
|
193
|
+
* if (result.url) {
|
|
194
|
+
* console.log('Video URL:', result.url)
|
|
195
|
+
* }
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
export async function getVideoJobStatus<
|
|
199
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
200
|
+
>(options: {
|
|
201
|
+
adapter: TAdapter & { kind: typeof kind }
|
|
202
|
+
jobId: string
|
|
203
|
+
}): Promise<{
|
|
204
|
+
status: 'pending' | 'processing' | 'completed' | 'failed'
|
|
205
|
+
progress?: number
|
|
206
|
+
url?: string
|
|
207
|
+
error?: string
|
|
208
|
+
}> {
|
|
209
|
+
const { adapter, jobId } = options
|
|
210
|
+
|
|
211
|
+
// Get status first
|
|
212
|
+
const statusResult = await adapter.getVideoStatus(jobId)
|
|
213
|
+
|
|
214
|
+
// If completed, also get the URL
|
|
215
|
+
if (statusResult.status === 'completed') {
|
|
216
|
+
try {
|
|
217
|
+
const urlResult = await adapter.getVideoUrl(jobId)
|
|
218
|
+
return {
|
|
219
|
+
status: statusResult.status,
|
|
220
|
+
progress: statusResult.progress,
|
|
221
|
+
url: urlResult.url,
|
|
222
|
+
}
|
|
223
|
+
} catch (error) {
|
|
224
|
+
// If URL fetch fails, still return status
|
|
225
|
+
return {
|
|
226
|
+
status: statusResult.status,
|
|
227
|
+
progress: statusResult.progress,
|
|
228
|
+
error:
|
|
229
|
+
error instanceof Error ? error.message : 'Failed to get video URL',
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Return status for non-completed jobs
|
|
235
|
+
return {
|
|
236
|
+
status: statusResult.status,
|
|
237
|
+
progress: statusResult.progress,
|
|
238
|
+
error: statusResult.error,
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ===========================
|
|
243
|
+
// Options Factory
|
|
244
|
+
// ===========================
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Create typed options for the generateVideo() function without executing.
|
|
248
|
+
*/
|
|
249
|
+
export function createVideoOptions<
|
|
250
|
+
TAdapter extends VideoAdapter<string, object>,
|
|
251
|
+
>(options: VideoCreateOptions<TAdapter>): VideoCreateOptions<TAdapter> {
|
|
252
|
+
return options
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Re-export adapter types
|
|
256
|
+
export type {
|
|
257
|
+
VideoAdapter,
|
|
258
|
+
VideoAdapterConfig,
|
|
259
|
+
AnyVideoAdapter,
|
|
260
|
+
} from './adapter'
|
|
261
|
+
export { BaseVideoAdapter } from './adapter'
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Activities Index
|
|
3
|
+
*
|
|
4
|
+
* Central hub for all AI activities. This module exports:
|
|
5
|
+
* - All activity implementations and their types
|
|
6
|
+
* - All adapter interfaces and base classes
|
|
7
|
+
* - Unified type definitions
|
|
8
|
+
*
|
|
9
|
+
* To add a new activity:
|
|
10
|
+
* 1. Create a new directory under activities/ with index.ts and adapter.ts
|
|
11
|
+
* 2. Export the activity and adapter from this file
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Import the activity functions
|
|
15
|
+
|
|
16
|
+
// Import adapter types for type definitions
|
|
17
|
+
import type { AnyTextAdapter } from './chat/adapter'
|
|
18
|
+
import type { AnySummarizeAdapter } from './summarize/adapter'
|
|
19
|
+
import type { AnyImageAdapter } from './generateImage/adapter'
|
|
20
|
+
import type { AnyVideoAdapter } from './generateVideo/adapter'
|
|
21
|
+
import type { AnyTTSAdapter } from './generateSpeech/adapter'
|
|
22
|
+
import type { AnyTranscriptionAdapter } from './generateTranscription/adapter'
|
|
23
|
+
|
|
24
|
+
// ===========================
|
|
25
|
+
// Chat Activity
|
|
26
|
+
// ===========================
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
kind as textKind,
|
|
30
|
+
chat,
|
|
31
|
+
type TextActivityOptions,
|
|
32
|
+
type TextActivityResult,
|
|
33
|
+
} from './chat/index'
|
|
34
|
+
|
|
35
|
+
export {
|
|
36
|
+
BaseTextAdapter,
|
|
37
|
+
type AnyTextAdapter,
|
|
38
|
+
type TextAdapter,
|
|
39
|
+
type TextAdapterConfig,
|
|
40
|
+
type StructuredOutputOptions,
|
|
41
|
+
type StructuredOutputResult,
|
|
42
|
+
} from './chat/adapter'
|
|
43
|
+
|
|
44
|
+
// ===========================
|
|
45
|
+
// Summarize Activity
|
|
46
|
+
// ===========================
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
kind as summarizeKind,
|
|
50
|
+
summarize,
|
|
51
|
+
type SummarizeActivityOptions,
|
|
52
|
+
type SummarizeActivityResult,
|
|
53
|
+
type SummarizeProviderOptions,
|
|
54
|
+
} from './summarize/index'
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
BaseSummarizeAdapter,
|
|
58
|
+
type SummarizeAdapter,
|
|
59
|
+
type SummarizeAdapterConfig,
|
|
60
|
+
type AnySummarizeAdapter,
|
|
61
|
+
} from './summarize/adapter'
|
|
62
|
+
|
|
63
|
+
// ===========================
|
|
64
|
+
// Image Activity
|
|
65
|
+
// ===========================
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
kind as imageKind,
|
|
69
|
+
generateImage,
|
|
70
|
+
type ImageActivityOptions,
|
|
71
|
+
type ImageActivityResult,
|
|
72
|
+
type ImageProviderOptionsForModel,
|
|
73
|
+
type ImageSizeForModel,
|
|
74
|
+
} from './generateImage/index'
|
|
75
|
+
|
|
76
|
+
export {
|
|
77
|
+
BaseImageAdapter,
|
|
78
|
+
type ImageAdapter,
|
|
79
|
+
type ImageAdapterConfig,
|
|
80
|
+
type AnyImageAdapter,
|
|
81
|
+
} from './generateImage/adapter'
|
|
82
|
+
|
|
83
|
+
// ===========================
|
|
84
|
+
// Video Activity (Experimental)
|
|
85
|
+
// ===========================
|
|
86
|
+
|
|
87
|
+
export {
|
|
88
|
+
kind as videoKind,
|
|
89
|
+
generateVideo,
|
|
90
|
+
getVideoJobStatus,
|
|
91
|
+
type VideoActivityOptions,
|
|
92
|
+
type VideoActivityResult,
|
|
93
|
+
type VideoProviderOptions,
|
|
94
|
+
type VideoCreateOptions,
|
|
95
|
+
type VideoStatusOptions,
|
|
96
|
+
type VideoUrlOptions,
|
|
97
|
+
} from './generateVideo/index'
|
|
98
|
+
|
|
99
|
+
export {
|
|
100
|
+
BaseVideoAdapter,
|
|
101
|
+
type VideoAdapter,
|
|
102
|
+
type VideoAdapterConfig,
|
|
103
|
+
type AnyVideoAdapter,
|
|
104
|
+
} from './generateVideo/adapter'
|
|
105
|
+
|
|
106
|
+
// ===========================
|
|
107
|
+
// TTS Activity
|
|
108
|
+
// ===========================
|
|
109
|
+
|
|
110
|
+
export {
|
|
111
|
+
kind as ttsKind,
|
|
112
|
+
generateSpeech,
|
|
113
|
+
type TTSActivityOptions,
|
|
114
|
+
type TTSActivityResult,
|
|
115
|
+
type TTSProviderOptions,
|
|
116
|
+
} from './generateSpeech/index'
|
|
117
|
+
|
|
118
|
+
export {
|
|
119
|
+
BaseTTSAdapter,
|
|
120
|
+
type TTSAdapter,
|
|
121
|
+
type TTSAdapterConfig,
|
|
122
|
+
type AnyTTSAdapter,
|
|
123
|
+
} from './generateSpeech/adapter'
|
|
124
|
+
|
|
125
|
+
// ===========================
|
|
126
|
+
// Transcription Activity
|
|
127
|
+
// ===========================
|
|
128
|
+
|
|
129
|
+
export {
|
|
130
|
+
kind as transcriptionKind,
|
|
131
|
+
generateTranscription,
|
|
132
|
+
type TranscriptionActivityOptions,
|
|
133
|
+
type TranscriptionActivityResult,
|
|
134
|
+
type TranscriptionProviderOptions,
|
|
135
|
+
} from './generateTranscription/index'
|
|
136
|
+
|
|
137
|
+
export {
|
|
138
|
+
BaseTranscriptionAdapter,
|
|
139
|
+
type TranscriptionAdapter,
|
|
140
|
+
type TranscriptionAdapterConfig,
|
|
141
|
+
type AnyTranscriptionAdapter,
|
|
142
|
+
} from './generateTranscription/adapter'
|
|
143
|
+
|
|
144
|
+
// ===========================
|
|
145
|
+
// Adapter Union Types
|
|
146
|
+
// ===========================
|
|
147
|
+
|
|
148
|
+
/** Union of all adapter types that can be passed to chat() */
|
|
149
|
+
export type AIAdapter =
|
|
150
|
+
| AnyTextAdapter
|
|
151
|
+
| AnySummarizeAdapter
|
|
152
|
+
| AnyImageAdapter
|
|
153
|
+
| AnyVideoAdapter
|
|
154
|
+
| AnyTTSAdapter
|
|
155
|
+
| AnyTranscriptionAdapter
|
|
156
|
+
|
|
157
|
+
/** Union type of all adapter kinds */
|
|
158
|
+
export type AdapterKind =
|
|
159
|
+
| 'text'
|
|
160
|
+
| 'summarize'
|
|
161
|
+
| 'image'
|
|
162
|
+
| 'video'
|
|
163
|
+
| 'tts'
|
|
164
|
+
| 'transcription'
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
StreamChunk,
|
|
3
|
+
SummarizationOptions,
|
|
4
|
+
SummarizationResult,
|
|
5
|
+
} from '../../types'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Configuration for summarize adapter instances
|
|
9
|
+
*/
|
|
10
|
+
export interface SummarizeAdapterConfig {
|
|
11
|
+
apiKey?: string
|
|
12
|
+
baseUrl?: string
|
|
13
|
+
timeout?: number
|
|
14
|
+
maxRetries?: number
|
|
15
|
+
headers?: Record<string, string>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Summarize adapter interface with pre-resolved generics.
|
|
20
|
+
*
|
|
21
|
+
* An adapter is created by a provider function: `provider('model')` → `adapter`
|
|
22
|
+
* All type resolution happens at the provider call site, not in this interface.
|
|
23
|
+
*
|
|
24
|
+
* Generic parameters:
|
|
25
|
+
* - TModel: The specific model name (e.g., 'gpt-4o')
|
|
26
|
+
* - TProviderOptions: Provider-specific options (already resolved)
|
|
27
|
+
*/
|
|
28
|
+
export interface SummarizeAdapter<
|
|
29
|
+
TModel extends string = string,
|
|
30
|
+
TProviderOptions extends object = Record<string, unknown>,
|
|
31
|
+
> {
|
|
32
|
+
/** Discriminator for adapter kind - used by generate() to determine API shape */
|
|
33
|
+
readonly kind: 'summarize'
|
|
34
|
+
/** Adapter name identifier */
|
|
35
|
+
readonly name: string
|
|
36
|
+
/** The model this adapter is configured for */
|
|
37
|
+
readonly model: TModel
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @internal Type-only properties for inference. Not assigned at runtime.
|
|
41
|
+
*/
|
|
42
|
+
'~types': {
|
|
43
|
+
providerOptions: TProviderOptions
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Summarize the given text
|
|
48
|
+
*/
|
|
49
|
+
summarize: (options: SummarizationOptions) => Promise<SummarizationResult>
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Stream summarization of the given text.
|
|
53
|
+
* Optional - if not implemented, the activity layer will fall back to
|
|
54
|
+
* non-streaming summarize and yield the result as a single chunk.
|
|
55
|
+
*/
|
|
56
|
+
summarizeStream?: (
|
|
57
|
+
options: SummarizationOptions,
|
|
58
|
+
) => AsyncIterable<StreamChunk>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A SummarizeAdapter with any/unknown type parameters.
|
|
63
|
+
* Useful as a constraint in generic functions and interfaces.
|
|
64
|
+
*/
|
|
65
|
+
export type AnySummarizeAdapter = SummarizeAdapter<any, any>
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Abstract base class for summarize adapters.
|
|
69
|
+
* Extend this class to implement a summarize adapter for a specific provider.
|
|
70
|
+
*
|
|
71
|
+
* Generic parameters match SummarizeAdapter - all pre-resolved by the provider function.
|
|
72
|
+
*/
|
|
73
|
+
export abstract class BaseSummarizeAdapter<
|
|
74
|
+
TModel extends string = string,
|
|
75
|
+
TProviderOptions extends object = Record<string, unknown>,
|
|
76
|
+
> implements SummarizeAdapter<TModel, TProviderOptions> {
|
|
77
|
+
readonly kind = 'summarize' as const
|
|
78
|
+
abstract readonly name: string
|
|
79
|
+
readonly model: TModel
|
|
80
|
+
|
|
81
|
+
// Type-only property - never assigned at runtime
|
|
82
|
+
declare '~types': {
|
|
83
|
+
providerOptions: TProviderOptions
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected config: SummarizeAdapterConfig
|
|
87
|
+
|
|
88
|
+
constructor(config: SummarizeAdapterConfig = {}, model: TModel) {
|
|
89
|
+
this.config = config
|
|
90
|
+
this.model = model
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
abstract summarize(
|
|
94
|
+
options: SummarizationOptions,
|
|
95
|
+
): Promise<SummarizationResult>
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Stream summarization of the given text.
|
|
99
|
+
* Override this method in concrete implementations to enable streaming.
|
|
100
|
+
* If not overridden, the activity layer will fall back to non-streaming.
|
|
101
|
+
*/
|
|
102
|
+
summarizeStream?(options: SummarizationOptions): AsyncIterable<StreamChunk>
|
|
103
|
+
|
|
104
|
+
protected generateId(): string {
|
|
105
|
+
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`
|
|
106
|
+
}
|
|
107
|
+
}
|