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

createCapability.md

57 lines1.6 KB

1---
2id: createCapability
3title: createCapability
4---
5​
6```ts
7function createCapability<TValue>(): <TName>(name) => Capability<TValue, TName>;
8```
9​
10Defined in: packages/ai/src/activities/chat/middleware/capabilities.ts:114
11​
12Create a capability. Returns a hybrid handle that destructures to
13`[get, provide]` and is itself the identity for `requires`/`provides`.
14​
15Curried so the value type is supplied explicitly while the name literal is
16INFERRED from the argument: `createCapability<T>()('name')`. (A single call
17`createCapability<T>('name')` cannot work — supplying `T` explicitly stops
18TypeScript inferring the name, collapsing it to `string` and defeating the
19compile-time coverage check that keys on the literal name.)
20​
21## Type Parameters
22​
23### TValue
24​
25`TValue` = `unknown`
26​
27## Returns
28​
29\<`TName`\>(`name`) => [`Capability`](../type-aliases/Capability.md)\<`TValue`, `TName`\>
30​
31## Example
32​
33**Provider + consumer middleware**
34​
35```ts
36const counterCapability = createCapability<{ value: number }>()('counter')
37const [getCounter, provideCounter] = counterCapability
38​
39const withCounter = defineChatMiddleware({
40 name: 'counter',
41 provides: [counterCapability],
42 setup(ctx) { provideCounter(ctx, { value: 0 }) },
43})
44​
45const readsCounter = defineChatMiddleware({
46 name: 'reads-counter',
47 requires: [counterCapability],
48 onChunk(ctx) { getCounter(ctx).value++ },
49})
50​
51chat({ adapter, messages, middleware: [withCounter, readsCounter] })
52```
53​
54## Remarks
55​
56Capability `name`s must be unique across your app: compile-time
57coverage tracking keys on the name literal (runtime keys on reference).