Skip to content

Commit 6f16b37

Browse files
fix(ai): infer provider options for media activities
1 parent 33acdd4 commit 6f16b37

4 files changed

Lines changed: 33 additions & 27 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/ai': patch
3+
---
4+
5+
Fix provider-specific model options inference for TTS, transcription, and summarize activities.

packages/ai/src/activities/generateSpeech/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ export const kind = 'tts' as const
3535
/**
3636
* Extract provider options from a TTSAdapter via ~types.
3737
*/
38-
export type TTSProviderOptions<TAdapter> =
39-
TAdapter extends TTSAdapter<any, any>
40-
? TAdapter['~types']['providerOptions']
41-
: object
38+
export type TTSProviderOptions<TAdapter> = TAdapter extends {
39+
'~types': { providerOptions: infer P extends object }
40+
}
41+
? P
42+
: object
4243

4344
// ===========================
4445
// Activity Options Type

packages/ai/src/activities/generateTranscription/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ export const kind = 'transcription' as const
3535
/**
3636
* Extract provider options from a TranscriptionAdapter via ~types.
3737
*/
38-
export type TranscriptionProviderOptions<TAdapter> =
39-
TAdapter extends TranscriptionAdapter<any, any>
40-
? TAdapter['~types']['providerOptions']
41-
: object
38+
export type TranscriptionProviderOptions<TAdapter> = TAdapter extends {
39+
'~types': { providerOptions: infer P extends object }
40+
}
41+
? P
42+
: object
4243

4344
// ===========================
4445
// Activity Options Type

packages/ai/src/activities/summarize/index.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ export const kind = 'summarize' as const
2525
// ===========================
2626

2727
/** Extract provider options from a SummarizeAdapter via ~types */
28-
export type SummarizeProviderOptions<TAdapter> =
29-
TAdapter extends SummarizeAdapter<any, any>
30-
? TAdapter['~types']['providerOptions']
31-
: object
28+
export type SummarizeProviderOptions<TAdapter> = TAdapter extends {
29+
'~types': { providerOptions: infer P extends object }
30+
}
31+
? P
32+
: object
3233

3334
// ===========================
3435
// Activity Options Type
@@ -42,7 +43,7 @@ export type SummarizeProviderOptions<TAdapter> =
4243
* @template TStream - Whether to stream the output
4344
*/
4445
export interface SummarizeActivityOptions<
45-
TAdapter extends SummarizeAdapter<string, object>,
46+
TAdapter extends SummarizeAdapter<string, SummarizeProviderOptions<TAdapter>>,
4647
TStream extends boolean = false,
4748
> {
4849
/** The summarize adapter to use (must be created with a model) */
@@ -150,7 +151,7 @@ function createId(prefix: string): string {
150151
* ```
151152
*/
152153
export function summarize<
153-
TAdapter extends SummarizeAdapter<string, object>,
154+
TAdapter extends SummarizeAdapter<string, SummarizeProviderOptions<TAdapter>>,
154155
TStream extends boolean = false,
155156
>(
156157
options: SummarizeActivityOptions<TAdapter, TStream>,
@@ -159,26 +160,22 @@ export function summarize<
159160

160161
if (stream) {
161162
return runStreamingSummarize(
162-
options as SummarizeActivityOptions<
163-
SummarizeAdapter<string, object>,
164-
true
165-
>,
163+
options as SummarizeActivityOptions<TAdapter, true>,
166164
) as SummarizeActivityResult<TStream>
167165
}
168166

169167
return runSummarize(
170-
options as SummarizeActivityOptions<
171-
SummarizeAdapter<string, object>,
172-
false
173-
>,
168+
options as SummarizeActivityOptions<TAdapter, false>,
174169
) as SummarizeActivityResult<TStream>
175170
}
176171

177172
/**
178173
* Run non-streaming summarization
179174
*/
180-
async function runSummarize(
181-
options: SummarizeActivityOptions<SummarizeAdapter<string, object>, false>,
175+
async function runSummarize<
176+
TAdapter extends SummarizeAdapter<string, SummarizeProviderOptions<TAdapter>>,
177+
>(
178+
options: SummarizeActivityOptions<TAdapter, false>,
182179
): Promise<SummarizationResult> {
183180
const { adapter, text, maxLength, style, focus, modelOptions } = options
184181
const model = adapter.model
@@ -247,8 +244,10 @@ async function runSummarize(
247244
* Uses the adapter's native streaming if available, otherwise falls back
248245
* to non-streaming and yields the result as a single chunk.
249246
*/
250-
async function* runStreamingSummarize(
251-
options: SummarizeActivityOptions<SummarizeAdapter<string, object>, true>,
247+
async function* runStreamingSummarize<
248+
TAdapter extends SummarizeAdapter<string, SummarizeProviderOptions<TAdapter>>,
249+
>(
250+
options: SummarizeActivityOptions<TAdapter, true>,
252251
): AsyncIterable<StreamChunk> {
253252
const { adapter, text, maxLength, style, focus, modelOptions } = options
254253
const model = adapter.model
@@ -296,7 +295,7 @@ async function* runStreamingSummarize(
296295
* Create typed options for the summarize() function without executing.
297296
*/
298297
export function createSummarizeOptions<
299-
TAdapter extends SummarizeAdapter<string, object>,
298+
TAdapter extends SummarizeAdapter<string, SummarizeProviderOptions<TAdapter>>,
300299
TStream extends boolean = false,
301300
>(
302301
options: SummarizeActivityOptions<TAdapter, TStream>,

0 commit comments

Comments
 (0)