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

createCapability.md

75 lines1.8 KB

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