@vibevibes/sdk 0.2.0 → 0.3.0

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 vibevibes
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 vibevibes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,263 +1,200 @@
1
- # @vibevibes/sdk
2
-
3
- The primitives for building agent-native experiences — shared interactive apps where humans and AI collaborate in real-time through a shared state, shared tools, and a shared canvas.
4
-
5
- ## Install
6
-
7
- ```bash
8
- npm install @vibevibes/sdk
9
- ```
10
-
11
- Peer dependencies: `react` (18 or 19), `zod`. Optional: `yjs`.
12
-
13
- ## Quick Start
14
-
15
- ```tsx
16
- import { defineExperience, defineTool } from "@vibevibes/sdk";
17
- import { z } from "zod";
18
-
19
- const tools = [
20
- defineTool({
21
- name: "counter.increment",
22
- description: "Add to the counter",
23
- input_schema: z.object({
24
- amount: z.number().default(1).describe("Amount to add"),
25
- }),
26
- handler: async (ctx, input) => {
27
- const count = (ctx.state.count || 0) + input.amount;
28
- ctx.setState({ ...ctx.state, count });
29
- return { count };
30
- },
31
- }),
32
- ];
33
-
34
- function Canvas({ sharedState, callTool }) {
35
- return (
36
- <div>
37
- <h1>{sharedState.count || 0}</h1>
38
- <button onClick={() => callTool("counter.increment", { amount: 1 })}>
39
- +1
40
- </button>
41
- </div>
42
- );
43
- }
44
-
45
- export default defineExperience({
46
- manifest: {
47
- id: "counter",
48
- version: "0.0.1",
49
- title: "Counter",
50
- description: "A shared counter",
51
- requested_capabilities: [],
52
- },
53
- Canvas,
54
- tools,
55
- });
56
- ```
57
-
58
- That's a complete experience. Humans click the button. Agents call the same tool via MCP. Both mutate the same state. Both see the same canvas.
59
-
60
- ## Core Concepts
61
-
62
- **Tools are the only way to mutate state.** Every tool has a Zod schema for validation and a handler that calls `ctx.setState()`. Humans use tools via the Canvas. Agents use the same tools via MCP. No backdoors.
63
-
64
- **Canvas is a React component.** It receives the current shared state and a `callTool` function. It re-renders on every state change.
65
-
66
- **Agents are actors, not assistants.** They join rooms, watch for events, react with tools, and persist memory. Same participation model as humans.
67
-
68
- ## Defining Tools
69
-
70
- ```tsx
71
- import { defineTool, quickTool } from "@vibevibes/sdk";
72
-
73
- // Full form
74
- defineTool({
75
- name: "board.place",
76
- description: "Place a piece on the board",
77
- input_schema: z.object({
78
- x: z.number(),
79
- y: z.number(),
80
- piece: z.string(),
81
- }),
82
- handler: async (ctx, input) => {
83
- const board = { ...ctx.state.board };
84
- board[`${input.x},${input.y}`] = input.piece;
85
- ctx.setState({ ...ctx.state, board });
86
- return { placed: true };
87
- },
88
- });
89
-
90
- // Shorthand
91
- quickTool("board.clear", "Clear the board", z.object({}), async (ctx) => {
92
- ctx.setState({ ...ctx.state, board: {} });
93
- });
94
- ```
95
-
96
- ### Tool Handler Context
97
-
98
- ```tsx
99
- type ToolCtx = {
100
- roomId: string;
101
- actorId: string; // Who called this tool
102
- owner?: string; // Owner extracted from actorId
103
- state: Record<string, any>; // Current shared state (read)
104
- setState: (s: Record<string, any>) => void; // Set new state (write, shallow merge)
105
- timestamp: number;
106
- memory: Record<string, any>; // Agent's persistent memory
107
- setMemory: (updates: Record<string, any>) => void;
108
- };
109
- ```
110
-
111
- Always spread existing state: `ctx.setState({ ...ctx.state, key: value })`.
112
-
113
- ## Canvas Props
114
-
115
- ```tsx
116
- type CanvasProps = {
117
- roomId: string;
118
- actorId: string;
119
- sharedState: Record<string, any>;
120
- callTool: (name: string, input: any) => Promise<any>;
121
- participants: string[];
122
- ephemeralState: Record<string, Record<string, any>>;
123
- setEphemeral: (data: Record<string, any>) => void;
124
- };
125
- ```
126
-
127
- ## Hooks
128
-
129
- | Hook | Signature | Purpose |
130
- |------|-----------|---------|
131
- | `useToolCall` | `(callTool) => { call, loading, error }` | Wraps callTool with loading/error tracking |
132
- | `useSharedState` | `(sharedState, key, default?) => value` | Typed accessor for a state key |
133
- | `useOptimisticTool` | `(callTool, sharedState) => { call, state, pending }` | Optimistic updates with rollback |
134
- | `useParticipants` | `(participants) => ParsedParticipant[]` | Parse actor IDs into `{ id, username, type, index }` |
135
- | `useAnimationFrame` | `(sharedState, interpolate?) => displayState` | Buffer state updates to animation frames |
136
- | `useFollow` | `(actorId, participants, ephemeral, setEphemeral) => { follow, unfollow, following, followers }` | Follow-mode protocol |
137
- | `useTypingIndicator` | `(actorId, ephemeral, setEphemeral) => { setTyping, typingUsers }` | Typing indicators |
138
- | `useUndo` | `(sharedState, callTool, opts?) => { undo, redo, canUndo, canRedo }` | Undo/redo via state snapshots. Requires `undoTool(z)` in tools array. |
139
- | `useDebounce` | `(callTool, delayMs?) => debouncedCallTool` | Debounced tool calls (search, text input) |
140
- | `useThrottle` | `(callTool, intervalMs?) => throttledCallTool` | Throttled tool calls (cursors, brushes, sliders) |
141
-
142
- ## Components
143
-
144
- Pre-built, inline-styled (no Tailwind needed):
145
-
146
- | Component | Key Props |
147
- |-----------|-----------|
148
- | `Button` | `onClick, disabled, variant: 'primary'\|'secondary'\|'danger'\|'ghost', size: 'sm'\|'md'\|'lg'` |
149
- | `Card` | `title, style` |
150
- | `Input` | `value, onChange: (value) => void, placeholder, type, disabled` |
151
- | `Badge` | `color: 'gray'\|'blue'\|'green'\|'red'\|'yellow'\|'purple'` |
152
- | `Stack` | `direction: 'row'\|'column', gap, align, justify` |
153
- | `Grid` | `columns, gap` |
154
- | `Slider` | `value, onChange, min, max, step, label` |
155
- | `Textarea` | `value, onChange, placeholder, rows` |
156
- | `Modal` | `open, onClose, title` |
157
- | `ColorPicker` | `value, onChange, presets: string[]` |
158
- | `Dropdown` | `value, onChange, options: [{value, label}], placeholder` |
159
- | `Tabs` | `tabs: [{id, label}], activeTab, onTabChange` |
160
-
161
- ## Agent Slots
162
-
163
- Define named agent roles for multi-agent experiences:
164
-
165
- ```tsx
166
- manifest: {
167
- agentSlots: [
168
- {
169
- role: "game-master",
170
- systemPrompt: "You are the game master. Narrate the world, control NPCs, manage encounters.",
171
- allowedTools: ["world.narrate", "npc.speak", "combat.enemy_turn"],
172
- autoSpawn: true,
173
- maxInstances: 1,
174
- },
175
- ],
176
- }
177
- ```
178
-
179
- ## Room Configuration
180
-
181
- Experiences can declare configurable parameters. Rooms are spawned with specific configs:
182
-
183
- ```tsx
184
- import { defineRoomConfig } from "@vibevibes/sdk";
185
-
186
- roomConfig: defineRoomConfig({
187
- schema: z.object({
188
- mode: z.enum(["combat", "explore"]).describe("Game mode"),
189
- difficulty: z.number().min(1).max(10).default(5),
190
- }),
191
- presets: {
192
- "boss-fight": { mode: "combat", difficulty: 10 },
193
- "peaceful": { mode: "explore", difficulty: 1 },
194
- },
195
- })
196
- ```
197
-
198
- ## Undo/Redo
199
-
200
- ```tsx
201
- import { undoTool, useUndo } from "@vibevibes/sdk";
202
-
203
- // Add to tools array
204
- const tools = [...yourTools, undoTool(z)];
205
-
206
- // In Canvas
207
- const { undo, redo, canUndo, canRedo } = useUndo(sharedState, callTool);
208
- ```
209
-
210
- ## Tests
211
-
212
- Inline tests for tool handlers, run with `npm test`:
213
-
214
- ```tsx
215
- import { defineTest } from "@vibevibes/sdk";
216
-
217
- tests: [
218
- defineTest({
219
- name: "increment adds to count",
220
- run: async ({ tool, ctx, expect }) => {
221
- const inc = tool("counter.increment");
222
- const c = ctx({ state: { count: 5 } });
223
- await inc.handler(c, { amount: 3 });
224
- expect(c.getState().count).toBe(8);
225
- },
226
- }),
227
- ]
228
- ```
229
-
230
- ## Manifest
231
-
232
- ```tsx
233
- type ExperienceManifest = {
234
- id: string;
235
- version: string;
236
- title: string;
237
- description: string;
238
- requested_capabilities: string[]; // e.g. ["room.spawn"]
239
- agentSlots?: AgentSlot[];
240
- category?: string; // "games", "productivity", "creative", etc.
241
- tags?: string[];
242
- netcode?: "default" | "tick" | "p2p-ephemeral";
243
- tickRateMs?: number; // For tick netcode
244
- hotKeys?: string[]; // Keys routed through ephemeral channel
245
- };
246
- ```
247
-
248
- ## How It Works
249
-
250
- ```
251
- Browser (Canvas) <--WebSocket--> Server <--HTTP--> MCP (Agent)
252
- | |
253
- callTool(name, input) validates input (Zod)
254
- runs handler(ctx, input)
255
- ctx.setState(newState)
256
- broadcasts to all clients
257
- ```
258
-
259
- All state lives on the server. The Canvas renders it. Tools are the only mutation path. Both humans and agents use the same tools.
260
-
261
- ## License
262
-
263
- MIT
1
+ # @vibevibes/sdk
2
+
3
+ The primitives for building agent-native experiences — shared interactive apps where humans and AI collaborate in real-time through a shared state, shared tools, and a shared canvas.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @vibevibes/sdk
9
+ ```
10
+
11
+ Peer dependencies: `react` (18 or 19), `zod`. Optional: `yjs`.
12
+
13
+ ## Quick Start
14
+
15
+ ```tsx
16
+ import { defineExperience, defineTool } from "@vibevibes/sdk";
17
+ import { z } from "zod";
18
+
19
+ const tools = [
20
+ defineTool({
21
+ name: "counter.increment",
22
+ description: "Add to the counter",
23
+ input_schema: z.object({
24
+ amount: z.number().default(1).describe("Amount to add"),
25
+ }),
26
+ handler: async (ctx, input) => {
27
+ const count = (ctx.state.count || 0) + input.amount;
28
+ ctx.setState({ ...ctx.state, count });
29
+ return { count };
30
+ },
31
+ }),
32
+ ];
33
+
34
+ function Canvas({ sharedState, callTool }) {
35
+ return (
36
+ <div>
37
+ <h1>{sharedState.count || 0}</h1>
38
+ <button onClick={() => callTool("counter.increment", { amount: 1 })}>
39
+ +1
40
+ </button>
41
+ </div>
42
+ );
43
+ }
44
+
45
+ export default defineExperience({
46
+ manifest: {
47
+ id: "counter",
48
+ version: "0.0.1",
49
+ title: "Counter",
50
+ description: "A shared counter",
51
+ requested_capabilities: [],
52
+ },
53
+ Canvas,
54
+ tools,
55
+ });
56
+ ```
57
+
58
+ That's a complete experience. Humans click the button. Agents call the same tool via MCP. Both mutate the same state. Both see the same canvas.
59
+
60
+ ## Core Concepts
61
+
62
+ **Tools are the only way to mutate state.** Every tool has a Zod schema for validation and a handler that calls `ctx.setState()`. Humans use tools via the Canvas. Agents use the same tools via MCP. No backdoors.
63
+
64
+ **Canvas is a React component.** It receives the current shared state and a `callTool` function. It re-renders on every state change.
65
+
66
+ **Agents are actors, not assistants.** They join rooms, watch for events, react with tools, and persist memory. Same participation model as humans.
67
+
68
+ ## Defining Tools
69
+
70
+ ```tsx
71
+ defineTool({
72
+ name: "board.place",
73
+ description: "Place a piece on the board",
74
+ input_schema: z.object({
75
+ x: z.number(),
76
+ y: z.number(),
77
+ piece: z.string(),
78
+ }),
79
+ handler: async (ctx, input) => {
80
+ const board = { ...ctx.state.board };
81
+ board[`${input.x},${input.y}`] = input.piece;
82
+ ctx.setState({ ...ctx.state, board });
83
+ return { placed: true };
84
+ },
85
+ });
86
+ ```
87
+
88
+ ### Tool Handler Context
89
+
90
+ ```tsx
91
+ type ToolCtx = {
92
+ roomId: string;
93
+ actorId: string; // Who called this tool
94
+ owner?: string; // Owner extracted from actorId
95
+ state: Record<string, any>; // Current shared state (read)
96
+ setState: (s: Record<string, any>) => void; // Set new state (write)
97
+ timestamp: number;
98
+ memory: Record<string, any>; // Agent's persistent memory
99
+ setMemory: (updates: Record<string, any>) => void;
100
+ };
101
+ ```
102
+
103
+ Always spread existing state: `ctx.setState({ ...ctx.state, key: value })`.
104
+
105
+ ## Canvas Props
106
+
107
+ ```tsx
108
+ type CanvasProps = {
109
+ actorId: string;
110
+ sharedState: Record<string, any>;
111
+ callTool: (name: string, input: any) => Promise<any>;
112
+ participants: string[];
113
+ ephemeralState: Record<string, Record<string, any>>;
114
+ setEphemeral: (data: Record<string, any>) => void;
115
+ stream?: (name: string, input: Record<string, unknown>) => void;
116
+ };
117
+ ```
118
+
119
+ ## Agent Slots
120
+
121
+ Define named agent roles for multi-agent experiences:
122
+
123
+ ```tsx
124
+ manifest: {
125
+ agentSlots: [
126
+ {
127
+ role: "game-master",
128
+ systemPrompt: "You are the game master.",
129
+ allowedTools: ["world.narrate", "npc.speak"],
130
+ autoSpawn: true,
131
+ maxInstances: 1,
132
+ },
133
+ ],
134
+ }
135
+ ```
136
+
137
+ ## Tests
138
+
139
+ Inline tests for tool handlers:
140
+
141
+ ```tsx
142
+ import { defineTest } from "@vibevibes/sdk";
143
+
144
+ tests: [
145
+ defineTest({
146
+ name: "increment adds to count",
147
+ run: async ({ tool, ctx, expect }) => {
148
+ const inc = tool("counter.increment");
149
+ const c = ctx({ state: { count: 5 } });
150
+ await inc.handler(c, { amount: 3 });
151
+ expect(c.getState().count).toBe(8);
152
+ },
153
+ }),
154
+ ]
155
+ ```
156
+
157
+ ## Manifest
158
+
159
+ ```tsx
160
+ type ExperienceManifest = {
161
+ id: string;
162
+ version: string;
163
+ title: string;
164
+ description: string;
165
+ requested_capabilities: string[];
166
+ agentSlots?: AgentSlot[];
167
+ participantSlots?: ParticipantSlot[];
168
+ category?: string;
169
+ tags?: string[];
170
+ netcode?: "default" | "tick" | "p2p-ephemeral";
171
+ tickRateMs?: number;
172
+ hotKeys?: string[];
173
+ };
174
+ ```
175
+
176
+ ## How It Works
177
+
178
+ ```
179
+ Browser (Canvas) <--WebSocket--> Server <--HTTP--> MCP (Agent)
180
+ | |
181
+ callTool(name, input) validates input (Zod)
182
+ runs handler(ctx, input)
183
+ ctx.setState(newState)
184
+ broadcasts to all clients
185
+ ```
186
+
187
+ All state lives on the server. The Canvas renders it. Tools are the only mutation path. Both humans and agents use the same tools.
188
+
189
+ ## Ecosystem
190
+
191
+ | Package | Description |
192
+ |---------|-------------|
193
+ | **@vibevibes/sdk** (this) | Define experiences — tools, canvas, state |
194
+ | [@vibevibes/mcp](https://github.com/vibevibes/mcp) | Runtime server — MCP + WebSocket + browser viewer |
195
+ | [create-vibevibes](https://github.com/vibevibes/create) | `npx create-vibevibes my-exp` — scaffold in seconds |
196
+ | [experiences](https://github.com/vibevibes/experiences) | Example experiences — fork and remix |
197
+
198
+ ## License
199
+
200
+ MIT