Skip to content

Commit 52c3172

Browse files
feat: update event client (TanStack#128)
* feat: update event client * feat: update event client * ci: apply automated fixes * changeset * update deps --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 9241e53 commit 52c3172

10 files changed

Lines changed: 896 additions & 1045 deletions

File tree

.changeset/fluffy-sites-exist.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/ai-devtools-core': patch
3+
'@tanstack/ai': patch
4+
---
5+
6+
update event client

.github/instructions/copilot-instructions.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Write tests for any new functionality.
2424
When defining new types, first check if the types exist somewhere and re-use them, do not create new types that are similar to existing ones.
2525

2626
When modifying existing functionality, ensure backward compatibility unless there's a strong reason to introduce breaking changes. If breaking changes are necessary, document them clearly in the relevant documentation files.
27-
28-
When subscribing to an event using `aiEventClient.on` in the devtools packages, always add the option `{ withEventTarget: false }` as the second argument to prevent over-subscriptions in the devtools.
27+
2928

3029
Under no circumstances should casting `as any` be used in the codebase. Always strive to find or create the appropriate type definitions. Avoid casting unless absolutely neccessary, and even then, prefer using `satisfies` for type assertions to maintain type safety.
3130

docs/config.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
{
2121
"label": "Devtools",
22-
"to": "getting-started/devtools"
22+
"to": "getting-started/devtools"
2323
}
2424
]
2525
},
@@ -62,6 +62,10 @@
6262
"label": "Connection Adapters",
6363
"to": "guides/connection-adapters"
6464
},
65+
{
66+
"label": "Observability",
67+
"to": "guides/observability"
68+
},
6569
{
6670
"label": "Per-Model Type Safety",
6771
"to": "guides/per-model-type-safety"
@@ -542,4 +546,4 @@
542546
]
543547
}
544548
]
545-
}
549+
}

docs/guides/observability.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Event client
2+
3+
The `@tanstack/ai` package offers you an event client for observability and debugging purposes.
4+
It's a fully type-safe decoupled event-driven system that emits events whenever they are internally
5+
triggered and you can subscribe to those events for observability.
6+
7+
Because the same event client is used for both the TanStack Devtools system and observability locally it will work
8+
by subscribing to the event bus and emitting events to/from the event bus into the listeners by default. If you
9+
want to subscribe to events in production as well you need to pass in a third argument to the `on` function,
10+
the `{ withEventTarget: true }` option.
11+
12+
This will not only emit to the event bus (which is not present in production), but to the current eventTarget that
13+
you will be able to listen to.
14+
15+
## Server events
16+
17+
There are both events that happen on the server and on the client, if you want to listen to either side you just need to
18+
subscribe on the server/client respectfully.
19+
20+
Here is an example for the server:
21+
```ts
22+
import { aiEventClient } from "@tanstack/ai/event-client";
23+
24+
// server.ts file or wherever the root of your server is
25+
aiEventClient.on("chat:started", e => {
26+
// implement whatever you need to here
27+
})
28+
// rest of your server logic
29+
const app = new Server();
30+
app.get()
31+
```
32+
33+
## Client events
34+
35+
Listening on the client is the same approach, just subscribe to the events:
36+
37+
```tsx
38+
// App.tsx
39+
import { aiEventClient } from "@tanstack/ai/event-client";
40+
41+
const App = () => {
42+
useEffect(() => {
43+
const cleanup = aiEventClient.on("client:tool-call-updated", e => {
44+
// do whatever you need to do
45+
})
46+
return cleanup;
47+
},[])
48+
return <div></div>
49+
}
50+
```
51+
52+

docs/reference/variables/aiEventClient.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ title: aiEventClient
99
const aiEventClient: AiEventClient;
1010
```
1111

12-
Defined in: [event-client.ts:387](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/event-client.ts#L387)
12+
Defined in: [event-client.ts:307](https://github.com/TanStack/ai/blob/main/packages/typescript/ai/src/event-client.ts#L307)

examples/ts-solid-chat/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"devDependencies": {
4141
"@solidjs/testing-library": "^0.8.10",
42-
"@tanstack/devtools-event-client": "^0.3.5",
42+
"@tanstack/devtools-event-client": "^0.4.0",
4343
"@tanstack/devtools-vite": "^0.3.11",
4444
"@testing-library/dom": "^10.4.1",
4545
"@types/node": "^24.10.1",

0 commit comments

Comments
 (0)