@agentnext/react 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -26
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,26 +1,94 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
React
|
|
4
|
-
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
# @agentnext/react
|
|
2
|
+
|
|
3
|
+
React bindings for AgentNext — register actions from components and consume the live action registry via hooks.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- `AgentProvider` — context provider that exposes the action registry to your component tree
|
|
8
|
+
- `useAgentAction` — registers an action (and exposes it to WebMCP) when a component mounts, and cleans up on unmount
|
|
9
|
+
- `useAgentRegistry` — reads the currently registered actions from context
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @agentnext/react @agentnext/core @agentnext/webmcp zod
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
> **Peer dependencies:** `react` ≥ 18 and `react-dom` ≥ 18
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### 1. Wrap your app with `AgentProvider`
|
|
22
|
+
|
|
23
|
+
Place `AgentProvider` at the root of your component tree so hooks further down can access the registry:
|
|
24
|
+
|
|
25
|
+
```tsx
|
|
26
|
+
import { AgentProvider } from "@agentnext/react";
|
|
27
|
+
|
|
28
|
+
export default function App() {
|
|
29
|
+
return (
|
|
30
|
+
<AgentProvider>
|
|
31
|
+
<Router />
|
|
32
|
+
</AgentProvider>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Register an action from a component with `useAgentAction`
|
|
38
|
+
|
|
39
|
+
Call `useAgentAction` inside any component that "owns" an action. The action is registered with the global registry and exposed to WebMCP when the component mounts, and deregistered when it unmounts.
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { z } from "zod";
|
|
43
|
+
import { action } from "@agentnext/core";
|
|
44
|
+
import { useAgentAction } from "@agentnext/react";
|
|
45
|
+
|
|
46
|
+
const addToCart = action({
|
|
47
|
+
name: "add_to_cart",
|
|
48
|
+
description: "Adds a product to the shopping cart.",
|
|
49
|
+
input: z.object({
|
|
50
|
+
productId: z.string(),
|
|
51
|
+
quantity: z.number().int().min(1),
|
|
52
|
+
}),
|
|
53
|
+
execute: async ({ productId, quantity }) => {
|
|
54
|
+
await cart.add(productId, quantity);
|
|
55
|
+
return { success: true };
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export function ProductPage() {
|
|
60
|
+
useAgentAction(addToCart);
|
|
61
|
+
|
|
62
|
+
return <button onClick={() => cart.add("p1", 1)}>Add to cart</button>;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 3. Read the registry with `useAgentRegistry`
|
|
67
|
+
|
|
68
|
+
Use `useAgentRegistry` to list all currently registered actions, e.g. for an inspector UI:
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
import { useAgentRegistry } from "@agentnext/react";
|
|
72
|
+
|
|
73
|
+
export function ActionList() {
|
|
74
|
+
const { actions } = useAgentRegistry();
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<ul>
|
|
78
|
+
{actions.map((a) => (
|
|
79
|
+
<li key={a.name}>
|
|
80
|
+
<strong>{a.name}</strong> — {a.description}
|
|
81
|
+
</li>
|
|
82
|
+
))}
|
|
83
|
+
</ul>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API Reference
|
|
89
|
+
|
|
90
|
+
| Export | Description |
|
|
91
|
+
| -------------------- | ------------------------------------------------------------------------- |
|
|
92
|
+
| `AgentProvider` | Context provider. Wrap your app root with this. |
|
|
93
|
+
| `useAgentAction(action)` | Registers an `ActionDefinition` on mount; cleans up on unmount. |
|
|
94
|
+
| `useAgentRegistry()` | Returns `{ actions: ActionDefinition[] }` from the nearest `AgentProvider`. |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentnext/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "React integration for AgentNext — AgentProvider, useAgentAction, useAgentRegistry",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "AgentNext",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"prepublishOnly": "npm run build"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@agentnext/core": "^0.1.
|
|
39
|
-
"@agentnext/webmcp": "^0.1.
|
|
38
|
+
"@agentnext/core": "^0.1.3",
|
|
39
|
+
"@agentnext/webmcp": "^0.1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"react": "^18.0.0 || ^19.0.0",
|