Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

BoundActivities.md

558 lines12.4 KB

1---
2id: BoundActivities
3title: BoundActivities
4---
5​
6Defined in: packages/ai/src/activities/chat/agents/bound.ts:164
7​
8Activity functions bound to one child run. Each takes the same options as
9the plain function, but `threadId` is optional: the child run fills it.
10Options you pass win over the bound defaults.
11​
12## Properties
13​
14### chat
15​
16```ts
17chat: <TAdapter, TSchema, TStream, TTools, TInterrupts, TContext, TMiddleware, TAgents>(options) => TextActivityResult<TSchema, TStream, TTools>;
18```
19​
20Defined in: packages/ai/src/activities/chat/agents/bound.ts:165
21​
22#### Type Parameters
23​
24##### TAdapter
25​
26`TAdapter` *extends* [`AnyTextAdapter`](../type-aliases/AnyTextAdapter.md)
27​
28##### TSchema
29​
30`TSchema` *extends* [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined` = `undefined`
31​
32##### TStream
33​
34`TStream` *extends* `boolean` = `boolean`
35​
36##### TTools
37​
38`TTools` *extends*
39 \| readonly (
40 \| `Omit`\<[`Tool`](Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
41 \| [`ProviderTool`](ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
42 \| `undefined` =
43 \| readonly (
44 \| `Omit`\<[`Tool`](Tool.md)\<`any`, `any`, `any`, `any`\>, `"execute"`\> & `object` & `object`
45 \| [`ProviderTool`](ProviderTool.md)\<`string`, `TAdapter`\[`"~types"`\]\[`"toolCapabilities"`\]\[`number`\]\>)[]
46 \| `undefined`
47​
48##### TInterrupts
49​
50`TInterrupts` *extends* readonly [`InterruptDefinition`](InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[] = \[\]
51​
52##### TContext
53​
54`TContext` = `unknown`
55​
56##### TMiddleware
57​
58`TMiddleware` *extends* `unknown`[] \| `undefined` = `undefined`
59​
60##### TAgents
61​
62`TAgents` *extends* readonly [`DefinedAgent`](DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[], `any`, `unknown`, [`AgentProduces`](../type-aliases/AgentProduces.md) \| `undefined`\>[] = readonly [`DefinedAgent`](DefinedAgent.md)\<`string`, readonly `SubagentTool`[], [`SchemaInput`](../type-aliases/SchemaInput.md) \| `undefined`, readonly [`InterruptDefinition`](InterruptDefinition.md)\<`any`, `any`, `any`, `any`, `any`\>[], `any`, `unknown`, [`AgentProduces`](../type-aliases/AgentProduces.md) \| `undefined`\>[]
63​
64#### Parameters
65​
66##### options
67​
68`BoundTextActivityOptions`\<`TAdapter`, `TSchema`, `TStream`, `TTools`, `TInterrupts`, `TContext`, `TMiddleware`, `TAgents`\>
69​
70#### Returns
71​
72`TextActivityResult`\<`TSchema`, `TStream`, `TTools`\>
73​
74***
75​
76### decide
77​
78```ts
79decide: <TAdapter, TQuestions>(options) => Promise<{ [K in string | number | symbol]: InferEvaluateAnswer<TQuestions[K]> } & object>;
80```
81​
82Defined in: packages/ai/src/activities/chat/agents/bound.ts:259
83​
84Ask typed questions about `state` and get answers your code can branch on.
85​
86You have state (a ticket, a record, a log) and you need typed answers, not
87prose. Pass questions built with `choice`, `score`, and `boolean`. Then
88branch on `result.queue.value` in ordinary TypeScript.
89​
90The question key `meta` is reserved. Throws if `questions` is empty or uses
91that key.
92​
93#### Type Parameters
94​
95##### TAdapter
96​
97`TAdapter` *extends* [`EvaluateAdapter`](EvaluateAdapter.md)\<`string`, `EvaluateProviderOptions`\<`TAdapter`\>\>
98​
99##### TQuestions
100​
101`TQuestions` *extends* `Record`\<`string`, [`WireQuestion`](../type-aliases/WireQuestion.md)\>
102​
103#### Parameters
104​
105##### options
106​
107`EvaluateActivityOptions`\<`TAdapter`, `TQuestions`\>
108​
109#### Returns
110​
111`Promise`\<\{ \[K in string \| number \| symbol\]: InferEvaluateAnswer\<TQuestions\[K\]\> \} & `object`\>
112​
113#### Example
114​
115**Route a support ticket**
116​
117```ts
118import { decide, choice, score, boolean } from '@tanstack/ai'
119import { typesafeDecider } from '@tanstack/ai-typesafe'
120​
121const result = await decide({
122 adapter: typesafeDecider('jev-latest'),
123 state: ticket,
124 questions: {
125 queue: choice({
126 instructions: 'Which team should handle this ticket?',
127 options: {
128 billing: 'Payments, invoices, refunds',
129 tech: 'Bugs, outages, integrations',
130 sales: 'Pricing, upgrades, new accounts',
131 },
132 }),
133 urgency: score({
134 instructions: 'How urgent is this ticket?',
135 levels: ['low', 'medium', 'high'],
136 }),
137 refund: boolean({
138 instructions: 'Is the customer asking for a refund?',
139 }),
140 },
141})
142​
143result.queue.value
144result.meta.model
145result.meta.usage
146```
147​
148***
149​
150### embed
151​
152```ts
153embed: <TAdapter>(options) => Promise<EmbeddingResult>;
154```
155​
156Defined in: packages/ai/src/activities/chat/agents/bound.ts:257
157​
158Embed activity - generates embedding vectors from text and image inputs.
159​
160Accepts a single item or an array of items; the result always carries an
161`embeddings` array with one vector per input item, in input order.
162​
163#### Type Parameters
164​
165##### TAdapter
166​
167`TAdapter` *extends* [`EmbeddingAdapter`](EmbeddingAdapter.md)\<`string`, `any`, `any`, `any`\>
168​
169#### Parameters
170​
171##### options
172​
173`EmbedOptions`\<`TAdapter`\>
174​
175#### Returns
176​
177`Promise`\<[`EmbeddingResult`](EmbeddingResult.md)\>
178​
179#### Examples
180​
181**Embed a single text**
182​
183```ts
184import { embed } from '@tanstack/ai'
185import { openaiEmbedding } from '@tanstack/ai-openai'
186​
187const result = await embed({
188 adapter: openaiEmbedding('text-embedding-3-small'),
189 input: 'a red guitar',
190})
191​
192console.log(result.embeddings[0].vector)
193```
194​
195**Batch with requested dimensions**
196​
197```ts
198const result = await embed({
199 adapter: openaiEmbedding('text-embedding-3-large'),
200 input: ['a red guitar', 'a blue drum kit'],
201 dimensions: 1024,
202})
203```
204​
205**Multimodal embedding (text + image fused into one vector)**
206​
207```ts
208import { cohereEmbedding } from '@tanstack/ai-cohere'
209​
210// A nested array of parts fuses them into a single vector. The outer array
211// is the item list, so this embeds one fused item into one vector.
212const result = await embed({
213 adapter: cohereEmbedding('embed-v4.0'),
214 input: [
215 [
216 { type: 'text', content: 'product photo' },
217 { type: 'image', source: { type: 'data', value: base64, mimeType: 'image/png' } },
218 ],
219 ],
220 modelOptions: { inputType: 'search_document' },
221})
222```
223​
224***
225​
226### generateAudio
227​
228```ts
229generateAudio: <TAdapter, TStream>(options) => AudioActivityResult<TStream>;
230```
231​
232Defined in: packages/ai/src/activities/chat/agents/bound.ts:212
233​
234#### Type Parameters
235​
236##### TAdapter
237​
238`TAdapter` *extends* [`AudioAdapter`](AudioAdapter.md)\<`string`, `AudioProviderOptions`\<`TAdapter`\>\>
239​
240##### TStream
241​
242`TStream` *extends* `boolean` = `false`
243​
244#### Parameters
245​
246##### options
247​
248`WithOptionalThreadId`\<`AudioActivityOptions`\<`TAdapter`, `TStream`\>\>
249​
250#### Returns
251​
252`AudioActivityResult`\<`TStream`\>
253​
254***
255​
256### generateImage
257​
258```ts
259generateImage: <TAdapter, TStream>(options) => ImageActivityResult<TStream>;
260```
261​
262Defined in: packages/ai/src/activities/chat/agents/bound.ts:200
263​
264#### Type Parameters
265​
266##### TAdapter
267​
268`TAdapter` *extends* [`ImageAdapter`](ImageAdapter.md)\<`string`, `any`, `any`, `any`, [`ModelInputModalitiesByName`](../type-aliases/ModelInputModalitiesByName.md)\>
269​
270##### TStream
271​
272`TStream` *extends* `boolean` = `false`
273​
274#### Parameters
275​
276##### options
277​
278`WithOptionalThreadId`\<`ImageActivityOptions`\<`TAdapter`, `TStream`\>\>
279​
280#### Returns
281​
282`ImageActivityResult`\<`TStream`\>
283​
284***
285​
286### generateLiveVideo
287​
288```ts
289generateLiveVideo: <TAdapter, TStream>(options) => LiveVideoActivityResult<TStream>;
290```
291​
292Defined in: packages/ai/src/activities/chat/agents/bound.ts:248
293​
294#### Type Parameters
295​
296##### TAdapter
297​
298`TAdapter` *extends* [`LiveVideoAdapter`](LiveVideoAdapter.md)\<`string`, `LiveVideoProviderOptions`\<`TAdapter`\>\>
299​
300##### TStream
301​
302`TStream` *extends* `boolean` = `false`
303​
304#### Parameters
305​
306##### options
307​
308`WithOptionalThreadId`\<`LiveVideoActivityOptions`\<`TAdapter`, `TStream`\>\>
309​
310#### Returns
311​
312`LiveVideoActivityResult`\<`TStream`\>
313​
314***
315​
316### generateSpeech
317​
318```ts
319generateSpeech: <TAdapter, TStream>(options) => TTSActivityResult<TStream>;
320```
321​
322Defined in: packages/ai/src/activities/chat/agents/bound.ts:218
323​
324#### Type Parameters
325​
326##### TAdapter
327​
328`TAdapter` *extends* [`TTSAdapter`](TTSAdapter.md)\<`string`, `TTSProviderOptions`\<`TAdapter`\>\>
329​
330##### TStream
331​
332`TStream` *extends* `boolean` = `false`
333​
334#### Parameters
335​
336##### options
337​
338`Omit`\<`TTSActivityOptionsBase`\<`TAdapter`, `TStream`\>, `"threadId"`\> & `object` & `TTSTextOrTurns`
339​
340#### Returns
341​
342`TTSActivityResult`\<`TStream`\>
343​
344***
345​
346### generateTranscription
347​
348```ts
349generateTranscription: <TAdapter, TStream>(options) => TranscriptionActivityResult<TStream>;
350```
351​
352Defined in: packages/ai/src/activities/chat/agents/bound.ts:231
353​
354#### Type Parameters
355​
356##### TAdapter
357​
358`TAdapter` *extends* [`TranscriptionAdapter`](TranscriptionAdapter.md)\<`string`, `TranscriptionProviderOptions`\<`TAdapter`\>\>
359​
360##### TStream
361​
362`TStream` *extends* `boolean` = `false`
363​
364#### Parameters
365​
366##### options
367​
368`WithOptionalThreadId`\<`TranscriptionActivityOptions`\<`TAdapter`, `TStream`\>\>
369​
370#### Returns
371​
372`TranscriptionActivityResult`\<`TStream`\>
373​
374***
375​
376### generateVideo
377​
378```ts
379generateVideo: <TAdapter, TStream>(options) => TStream extends true ? AsyncIterable<AGUIEvent, any, any> : Promise<VideoJobResult>;
380```
381​
382Defined in: packages/ai/src/activities/chat/agents/bound.ts:206
383​
384#### Type Parameters
385​
386##### TAdapter
387​
388`TAdapter` *extends* [`VideoAdapter`](VideoAdapter.md)\<`string`, `any`, `any`, `any`, `any`, `any`\>
389​
390##### TStream
391​
392`TStream` *extends* `boolean` = `false`
393​
394#### Parameters
395​
396##### options
397​
398`WithOptionalThreadId`\<`VideoCreateOptions`\<`TAdapter`, `TStream`\>\>
399​
400#### Returns
401​
402`TStream` *extends* `true` ? `AsyncIterable`\<[`AGUIEvent`](../type-aliases/AGUIEvent.md), `any`, `any`\> : `Promise`\<[`VideoJobResult`](VideoJobResult.md)\>
403​
404***
405​
406### generateVoice
407​
408```ts
409generateVoice: <TAdapter, TStream>(options) => VoiceActivityResult<TStream>;
410```
411​
412Defined in: packages/ai/src/activities/chat/agents/bound.ts:225
413​
414#### Type Parameters
415​
416##### TAdapter
417​
418`TAdapter` *extends* [`VoiceAdapter`](VoiceAdapter.md)\<`string`, `VoiceProviderOptions`\<`TAdapter`\>\>
419​
420##### TStream
421​
422`TStream` *extends* `boolean` = `false`
423​
424#### Parameters
425​
426##### options
427​
428`WithOptionalThreadId`\<`VoiceActivityOptions`\<`TAdapter`, `TStream`\>\>
429​
430#### Returns
431​
432`VoiceActivityResult`\<`TStream`\>
433​
434***
435​
436### generateWorld
437​
438```ts
439generateWorld: <TAdapter, TStream>(options) => WorldActivityResult<TStream>;
440```
441​
442Defined in: packages/ai/src/activities/chat/agents/bound.ts:242
443​
444#### Type Parameters
445​
446##### TAdapter
447​
448`TAdapter` *extends* [`WorldAdapter`](WorldAdapter.md)\<`string`, `WorldProviderOptions`\<`TAdapter`\>\>
449​
450##### TStream
451​
452`TStream` *extends* `boolean` = `false`
453​
454#### Parameters
455​
456##### options
457​
458`WithOptionalThreadId`\<`WorldActivityOptions`\<`TAdapter`, `TStream`\>\>
459​
460#### Returns
461​
462`WorldActivityResult`\<`TStream`\>
463​
464***
465​
466### rerank
467​
468```ts
469rerank: <TAdapter, TDocument>(options) => Promise<RerankResult<TDocument>>;
470```
471​
472Defined in: packages/ai/src/activities/chat/agents/bound.ts:258
473​
474Rerank activity - reorders documents by relevance to a query.
475​
476#### Type Parameters
477​
478##### TAdapter
479​
480`TAdapter` *extends* [`RerankAdapter`](RerankAdapter.md)\<`string`, `RerankProviderOptions`\<`TAdapter`\>\>
481​
482##### TDocument
483​
484`TDocument` *extends* `string` \| `object` = `string`
485​
486#### Parameters
487​
488##### options
489​
490`RerankActivityOptions`\<`TAdapter`, `TDocument`\>
491​
492#### Returns
493​
494`Promise`\<[`RerankResult`](RerankResult.md)\<`TDocument`\>\>
495​
496#### Examples
497​
498**Basic reranking**
499​
500```ts
501import { rerank } from '@tanstack/ai'
502import { cohereRerank } from '@tanstack/ai-cohere'
503​
504const { ranking, rerankedDocuments } = await rerank({
505 adapter: cohereRerank('rerank-v3.5'),
506 query: 'talk about rain',
507 documents: ['sunny day at the beach', 'rainy afternoon in the city'],
508 topN: 2,
509})
510​
511console.log(rerankedDocuments[0]) // 'rainy afternoon in the city'
512```
513​
514**Reranking object documents**
515​
516```ts
517const { ranking } = await rerank({
518 adapter: cohereRerank('rerank-v3.5'),
519 query: 'best laptop for travel',
520 documents: [
521 { id: 1, text: 'A heavy gaming desktop' },
522 { id: 2, text: 'A lightweight ultrabook with all-day battery' },
523 ],
524})
525​
526// ranking[0].document is the original object, fully typed.
527console.log(ranking[0].document.id)
528```
529​
530***
531​
532### summarize
533​
534```ts
535summarize: <TAdapter, TStream>(options) => SummarizeActivityResult<TStream>;
536```
537​
538Defined in: packages/ai/src/activities/chat/agents/bound.ts:194
539​
540#### Type Parameters
541​
542##### TAdapter
543​
544`TAdapter` *extends* [`SummarizeAdapter`](SummarizeAdapter.md)\<`string`, `object`\>
545​
546##### TStream
547​
548`TStream` *extends* `boolean` = `false`
549​
550#### Parameters
551​
552##### options
553​
554`WithOptionalThreadId`\<`SummarizeActivityOptions`\<`TAdapter`, `TStream`\>\>
555​
556#### Returns
557​
558`SummarizeActivityResult`\<`TStream`\>