convex-durable-agents 0.1.1 → 0.1.2
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 +150 -45
- package/dist/client/index.d.ts +51 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +98 -12
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/component.d.ts +8 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/agent.d.ts.map +1 -1
- package/dist/component/agent.js +42 -10
- package/dist/component/agent.js.map +1 -1
- package/dist/component/schema.d.ts +5 -1
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +3 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/streams.d.ts.map +1 -1
- package/dist/component/streams.js +4 -1
- package/dist/component/streams.js.map +1 -1
- package/dist/component/threads.d.ts +16 -2
- package/dist/component/threads.d.ts.map +1 -1
- package/dist/component/threads.js +18 -2
- package/dist/component/threads.js.map +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +8 -2
- package/dist/react/index.js.map +1 -1
- package/package.json +4 -1
- package/src/client/index.ts +168 -27
- package/src/client/setup.test.ts +2 -8
- package/src/component/_generated/component.ts +11 -1
- package/src/component/agent.ts +60 -11
- package/src/component/component.test.ts +25 -7
- package/src/component/schema.ts +3 -0
- package/src/component/streams.ts +4 -1
- package/src/component/threads.ts +20 -2
- package/src/react/index.ts +23 -24
- package/src/test.ts +1 -4
package/README.md
CHANGED
|
@@ -2,20 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/js/convex-durable-agents)
|
|
4
4
|
|
|
5
|
-
A Convex component for building durable AI agents with an async tool loop. The goal of this component is to provide a
|
|
5
|
+
A Convex component for building durable AI agents with an async tool loop. The goal of this component is to provide a
|
|
6
|
+
way to build AI agents that can run indefinitely and survive failures and restarts. It provides some of the
|
|
7
|
+
functionality of the [Convex Agents Component](https://www.convex.dev/components/agent) (such as persistent streaming),
|
|
8
|
+
while deliberately leaving out some of the more advanced features (context management, RAG, rate limiting, etc.). The
|
|
9
|
+
component is built on top of the [AI SDK v6](https://ai-sdk.dev/) SDK and aims to expose its full `streamText` API with
|
|
10
|
+
persistence and durable execution.
|
|
6
11
|
|
|
7
|
-
**Note:** This component is still in early development and is not yet ready for production use. The API will very likely
|
|
12
|
+
**Note:** This component is still in early development and is not yet ready for production use. The API will very likely
|
|
13
|
+
change before a first stable release.
|
|
8
14
|
|
|
9
15
|
## Features
|
|
10
16
|
|
|
11
17
|
- **Async Execution**: Agent tool loop is executed asynchronously to avoid time limits of convex actions
|
|
12
18
|
- **Tool Execution**: via convex actions - support for both sync and async tools
|
|
13
19
|
- **Automatic Retries**: Failed tool calls are automatically retried
|
|
20
|
+
- **Workpool Support**: Optionally route agent and tool execution through `@convex-dev/workpool` for parallelism control
|
|
21
|
+
and retry mechanisms
|
|
14
22
|
|
|
15
23
|
## Roadmap
|
|
16
24
|
|
|
17
25
|
- **Durable Execution**: Agent tool loops survive crashes and dev server restarts
|
|
18
|
-
- **Workpool/Workflow Support**: Support for workpool and workflow execution
|
|
19
26
|
|
|
20
27
|
## Installation
|
|
21
28
|
|
|
@@ -50,16 +57,12 @@ Create a chat handler with your AI model and tools:
|
|
|
50
57
|
|
|
51
58
|
import { z } from "zod";
|
|
52
59
|
import { components, internal } from "./_generated/api";
|
|
53
|
-
import {
|
|
54
|
-
createActionTool,
|
|
55
|
-
defineAgentApi,
|
|
56
|
-
streamHandlerAction,
|
|
57
|
-
} from "convex-durable-agents";
|
|
60
|
+
import { createActionTool, defineAgentApi, streamHandlerAction } from "convex-durable-agents";
|
|
58
61
|
import { openai } from "@ai-sdk/openai";
|
|
59
62
|
|
|
60
63
|
// Define the stream handler with your model and tools
|
|
61
64
|
export const chatAgentHandler = streamHandlerAction(components.durableAgents, {
|
|
62
|
-
model:
|
|
65
|
+
model: "anthropic/claude-haiku-4.5",
|
|
63
66
|
system: "You are a helpful AI assistant.",
|
|
64
67
|
tools: {
|
|
65
68
|
get_weather: createActionTool({
|
|
@@ -84,12 +87,20 @@ export const {
|
|
|
84
87
|
stopThread,
|
|
85
88
|
addToolResult,
|
|
86
89
|
addToolError,
|
|
87
|
-
} = defineAgentApi(components.durableAgents, internal.chat.chatAgentHandler
|
|
90
|
+
} = defineAgentApi(components.durableAgents, internal.chat.chatAgentHandler, {
|
|
91
|
+
// Optional: Add authorization to protect thread access
|
|
92
|
+
authorizationCallback: async (ctx, threadId) => {
|
|
93
|
+
// Example: verify the user owns this thread
|
|
94
|
+
// const identity = await ctx.auth.getUserIdentity();
|
|
95
|
+
// if (!identity) throw new Error("Unauthorized");
|
|
96
|
+
},
|
|
97
|
+
});
|
|
88
98
|
```
|
|
89
99
|
|
|
90
100
|
#### Using Internal API
|
|
91
101
|
|
|
92
|
-
If you want to restrict the agent API to only be callable from other Convex functions (not directly from clients), use
|
|
102
|
+
If you want to restrict the agent API to only be callable from other Convex functions (not directly from clients), use
|
|
103
|
+
`defineInternalAgentApi` instead:
|
|
93
104
|
|
|
94
105
|
```ts
|
|
95
106
|
// convex/chat.ts
|
|
@@ -105,6 +116,7 @@ export const {
|
|
|
105
116
|
```
|
|
106
117
|
|
|
107
118
|
This is useful when you want to:
|
|
119
|
+
|
|
108
120
|
- Add authentication/authorization checks before calling agent functions
|
|
109
121
|
- Wrap agent functions with additional business logic
|
|
110
122
|
- Prevent direct client access to the agent API
|
|
@@ -124,7 +136,7 @@ export const sendMessage = mutation({
|
|
|
124
136
|
handler: async (ctx, args) => {
|
|
125
137
|
const identity = await ctx.auth.getUserIdentity();
|
|
126
138
|
if (!identity) throw new Error("Not authenticated");
|
|
127
|
-
|
|
139
|
+
|
|
128
140
|
// Call the internal agent API
|
|
129
141
|
return ctx.runMutation(internal.chat.sendMessage, args);
|
|
130
142
|
},
|
|
@@ -163,12 +175,12 @@ import { api } from "../convex/_generated/api";
|
|
|
163
175
|
|
|
164
176
|
function ChatView({ threadId }: { threadId: string }) {
|
|
165
177
|
const sendMessage = useMutation(api.chat.sendMessage);
|
|
166
|
-
|
|
178
|
+
|
|
167
179
|
const { messages, status, isRunning } = useThread(
|
|
168
180
|
api.chat.listMessagesWithStreams,
|
|
169
181
|
api.chat.getThread,
|
|
170
182
|
{ threadId },
|
|
171
|
-
{ stream: true }
|
|
183
|
+
{ stream: true },
|
|
172
184
|
);
|
|
173
185
|
|
|
174
186
|
return (
|
|
@@ -178,7 +190,7 @@ function ChatView({ threadId }: { threadId: string }) {
|
|
|
178
190
|
<strong>{msg.role}:</strong> {msg.text}
|
|
179
191
|
</div>
|
|
180
192
|
))}
|
|
181
|
-
|
|
193
|
+
|
|
182
194
|
<input
|
|
183
195
|
onKeyPress={(e) => {
|
|
184
196
|
if (e.key === "Enter" && !isRunning) {
|
|
@@ -196,7 +208,7 @@ function ChatView({ threadId }: { threadId: string }) {
|
|
|
196
208
|
|
|
197
209
|
### Client API
|
|
198
210
|
|
|
199
|
-
#### `defineAgentApi(component, streamHandler)`
|
|
211
|
+
#### `defineAgentApi(component, streamHandler, options?)`
|
|
200
212
|
|
|
201
213
|
Creates the full agent API with **public** functions that can be called directly from clients:
|
|
202
214
|
|
|
@@ -212,20 +224,47 @@ Creates the full agent API with **public** functions that can be called directly
|
|
|
212
224
|
- `addToolResult({ toolCallId, result })` - Add result for async tool
|
|
213
225
|
- `addToolError({ toolCallId, error })` - Add error for async tool
|
|
214
226
|
|
|
215
|
-
|
|
227
|
+
**Options:**
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
type AgentApiOptions = {
|
|
231
|
+
authorizationCallback?: (ctx: QueryCtx | MutationCtx | ActionCtx, threadId: string) => Promise<void> | void;
|
|
232
|
+
workpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
233
|
+
toolExecutionWorkpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
234
|
+
};
|
|
235
|
+
```
|
|
216
236
|
|
|
217
|
-
|
|
237
|
+
- `authorizationCallback` - Called before any operation that accesses an existing thread. Use it to verify the user has
|
|
238
|
+
permission to access the thread. Throw an error to deny access.
|
|
239
|
+
- `workpoolEnqueueAction` - Route agent and tool execution through a workpool for parallelism control
|
|
240
|
+
- `toolExecutionWorkpoolEnqueueAction` - Override workpool for tool execution only (falls back to
|
|
241
|
+
`workpoolEnqueueAction` if not set)
|
|
242
|
+
|
|
243
|
+
**Protected endpoints:** `sendMessage`, `resumeThread`, `stopThread`, `getThread`, `listMessages`,
|
|
244
|
+
`listMessagesWithStreams`, `deleteThread`
|
|
245
|
+
|
|
246
|
+
**Example with ownership check:**
|
|
218
247
|
|
|
219
248
|
```ts
|
|
220
|
-
|
|
249
|
+
defineAgentApi(components.durableAgents, internal.chat.chatAgentHandler, {
|
|
250
|
+
authorizationCallback: async (ctx, threadId) => {
|
|
251
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
252
|
+
if (!identity) throw new Error("Unauthorized");
|
|
221
253
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
254
|
+
// Query your threads table to verify ownership
|
|
255
|
+
const thread = await ctx.runQuery(api.threads.getOwner, { threadId });
|
|
256
|
+
if (thread?.userId !== identity.subject) {
|
|
257
|
+
throw new Error("Access denied");
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
});
|
|
227
261
|
```
|
|
228
262
|
|
|
263
|
+
#### `defineInternalAgentApi(component, streamHandler, options?)`
|
|
264
|
+
|
|
265
|
+
Same as `defineAgentApi` but creates internal functions that can only be called from other Convex functions. Use this
|
|
266
|
+
when you want to add authentication, authorization, or other business logic before calling agent functions.
|
|
267
|
+
|
|
229
268
|
#### `streamHandlerAction(component, options)`
|
|
230
269
|
|
|
231
270
|
Creates the stream handler action:
|
|
@@ -273,20 +312,15 @@ All-in-one hook for thread status and messages:
|
|
|
273
312
|
|
|
274
313
|
```ts
|
|
275
314
|
const {
|
|
276
|
-
messages,
|
|
277
|
-
thread,
|
|
278
|
-
status,
|
|
279
|
-
isLoading,
|
|
280
|
-
isRunning,
|
|
281
|
-
isComplete,
|
|
282
|
-
isFailed,
|
|
283
|
-
isStopped,
|
|
284
|
-
} = useThread(
|
|
285
|
-
api.chat.listMessagesWithStreams,
|
|
286
|
-
api.chat.getThread,
|
|
287
|
-
{ threadId },
|
|
288
|
-
{ stream: true }
|
|
289
|
-
);
|
|
315
|
+
messages, // UIMessage[]
|
|
316
|
+
thread, // ThreadDoc | null
|
|
317
|
+
status, // ThreadStatus
|
|
318
|
+
isLoading, // boolean
|
|
319
|
+
isRunning, // boolean
|
|
320
|
+
isComplete, // boolean
|
|
321
|
+
isFailed, // boolean
|
|
322
|
+
isStopped, // boolean
|
|
323
|
+
} = useThread(api.chat.listMessagesWithStreams, api.chat.getThread, { threadId }, { stream: true });
|
|
290
324
|
```
|
|
291
325
|
|
|
292
326
|
#### `useSmoothText(text, options?)`
|
|
@@ -305,8 +339,9 @@ const [visibleText, { cursor, isStreaming }] = useSmoothText(text, {
|
|
|
305
339
|
Subscribe to thread status changes:
|
|
306
340
|
|
|
307
341
|
```ts
|
|
308
|
-
const { thread, status, isRunning, isComplete, isFailed, isStopped } =
|
|
309
|
-
|
|
342
|
+
const { thread, status, isRunning, isComplete, isFailed, isStopped } = useThreadStatus(api.chat.getThread, {
|
|
343
|
+
threadId,
|
|
344
|
+
});
|
|
310
345
|
```
|
|
311
346
|
|
|
312
347
|
#### `useMessages(query, threadQuery, args)`
|
|
@@ -314,11 +349,7 @@ const { thread, status, isRunning, isComplete, isFailed, isStopped } =
|
|
|
314
349
|
Fetch and transform messages:
|
|
315
350
|
|
|
316
351
|
```ts
|
|
317
|
-
const { messages, isLoading, thread } = useMessages(
|
|
318
|
-
api.chat.listMessages,
|
|
319
|
-
api.chat.getThread,
|
|
320
|
-
{ threadId }
|
|
321
|
-
);
|
|
352
|
+
const { messages, isLoading, thread } = useMessages(api.chat.listMessages, api.chat.getThread, { threadId });
|
|
322
353
|
```
|
|
323
354
|
|
|
324
355
|
## Thread Status
|
|
@@ -331,6 +362,80 @@ Threads can be in one of these states:
|
|
|
331
362
|
- `failed` - An error occurred
|
|
332
363
|
- `stopped` - User stopped the conversation
|
|
333
364
|
|
|
365
|
+
## Workpool Integration
|
|
366
|
+
|
|
367
|
+
For advanced use cases, you can route agent execution through the `@convex-dev/workpool` component. This provides:
|
|
368
|
+
|
|
369
|
+
- **Parallelism Control**: Limit concurrent AI model calls and tool executions
|
|
370
|
+
- **Retry Mechanisms**: Automatic retries with exponential backoff for failed actions
|
|
371
|
+
- **Rate Limiting Protection**: Prevent overwhelming external APIs
|
|
372
|
+
|
|
373
|
+
### Setup
|
|
374
|
+
|
|
375
|
+
1. Install and configure the workpool component:
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
// convex/convex.config.ts
|
|
379
|
+
import { defineApp } from "convex/server";
|
|
380
|
+
import durableAgents from "convex-durable-agents/convex.config.js";
|
|
381
|
+
import workpool from "@convex-dev/workpool/convex.config.js";
|
|
382
|
+
|
|
383
|
+
const app = defineApp();
|
|
384
|
+
app.use(durableAgents);
|
|
385
|
+
app.use(workpool, { name: "agentWorkpool" });
|
|
386
|
+
|
|
387
|
+
export default app;
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
2. Create the workpool bridge:
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
// convex/workpool.ts
|
|
394
|
+
import { Workpool } from "@convex-dev/workpool";
|
|
395
|
+
import { components } from "./_generated/api";
|
|
396
|
+
import { createWorkpoolBridge } from "convex-durable-agents";
|
|
397
|
+
|
|
398
|
+
const pool = new Workpool(components.agentWorkpool, {
|
|
399
|
+
maxParallelism: 5,
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
export const { enqueueWorkpoolAction } = createWorkpoolBridge(pool);
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
3. Pass the workpool to your agent API:
|
|
406
|
+
|
|
407
|
+
```ts
|
|
408
|
+
// convex/chat.ts
|
|
409
|
+
export const {
|
|
410
|
+
createThread,
|
|
411
|
+
sendMessage,
|
|
412
|
+
// ...
|
|
413
|
+
} = defineAgentApi(components.durableAgents, internal.chat.chatAgentHandler, {
|
|
414
|
+
workpoolEnqueueAction: internal.workpool.enqueueWorkpoolAction,
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Separate Workpools for Tools
|
|
419
|
+
|
|
420
|
+
You can use different workpools for the stream handler and tool execution:
|
|
421
|
+
|
|
422
|
+
```ts
|
|
423
|
+
// convex/workpool.ts
|
|
424
|
+
const agentPool = new Workpool(components.agentWorkpool, { maxParallelism: 3 });
|
|
425
|
+
const toolPool = new Workpool(components.toolWorkpool, { maxParallelism: 10 });
|
|
426
|
+
|
|
427
|
+
export const { enqueueWorkpoolAction: enqueueAgentAction } = createWorkpoolBridge(agentPool);
|
|
428
|
+
export const { enqueueWorkpoolAction: enqueueToolAction } = createWorkpoolBridge(toolPool);
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
// convex/chat.ts
|
|
433
|
+
defineAgentApi(components.durableAgents, internal.chat.chatAgentHandler, {
|
|
434
|
+
workpoolEnqueueAction: internal.workpool.enqueueAgentAction,
|
|
435
|
+
toolExecutionWorkpoolEnqueueAction: internal.workpool.enqueueToolAction,
|
|
436
|
+
});
|
|
437
|
+
```
|
|
438
|
+
|
|
334
439
|
## Architecture
|
|
335
440
|
|
|
336
441
|
```
|
package/dist/client/index.d.ts
CHANGED
|
@@ -111,6 +111,10 @@ type StreamHandlerArgs = Omit<Parameters<typeof streamText>[0], "tools" | "messa
|
|
|
111
111
|
/** Optional: Save streaming deltas to the database for real-time client updates */
|
|
112
112
|
saveStreamDeltas?: boolean | StreamingOptions;
|
|
113
113
|
transformMessages?: (messages: ModelMessage[]) => ModelMessage[];
|
|
114
|
+
/** Optional: Function to enqueue actions via workpool (used for both stream handler and tools unless overridden) */
|
|
115
|
+
workpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
116
|
+
/** Optional: Override workpool for tool execution only */
|
|
117
|
+
toolExecutionWorkpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
114
118
|
};
|
|
115
119
|
export declare function streamHandlerAction(component: ComponentApi, { tools, saveStreamDeltas, transformMessages, ...streamTextArgs }: StreamHandlerArgs): RegisteredAction<"internal", {
|
|
116
120
|
threadId: string;
|
|
@@ -189,17 +193,62 @@ export type AgentApi<V extends FunctionVisibility = "public"> = {
|
|
|
189
193
|
error: string;
|
|
190
194
|
}, null>;
|
|
191
195
|
};
|
|
196
|
+
export type AgentApiOptions = {
|
|
197
|
+
/** Optional authorization callback for thread access control */
|
|
198
|
+
authorizationCallback?: (ctx: QueryCtx | MutationCtx | ActionCtx, threadId: string) => Promise<void> | void;
|
|
199
|
+
/** Optional: Function to enqueue actions via workpool (used for both stream handler and tools unless overridden) */
|
|
200
|
+
workpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
201
|
+
/** Optional: Override workpool for tool execution only */
|
|
202
|
+
toolExecutionWorkpoolEnqueueAction?: FunctionReference<"mutation", "internal">;
|
|
203
|
+
};
|
|
192
204
|
/**
|
|
193
205
|
* Define a public agent API that can be called from clients.
|
|
194
206
|
*/
|
|
195
207
|
export declare function defineAgentApi(component: ComponentApi, ref: FunctionReference<"action", "internal" | "public", {
|
|
196
208
|
threadId: string;
|
|
197
|
-
}
|
|
209
|
+
}>, options?: AgentApiOptions): AgentApi<"public">;
|
|
198
210
|
/**
|
|
199
211
|
* Define an internal agent API that can only be called from other Convex functions.
|
|
200
212
|
*/
|
|
201
213
|
export declare function defineInternalAgentApi(component: ComponentApi, ref: FunctionReference<"action", "internal" | "public", {
|
|
202
214
|
threadId: string;
|
|
203
|
-
}
|
|
215
|
+
}>, options?: AgentApiOptions): AgentApi<"internal">;
|
|
216
|
+
/**
|
|
217
|
+
* Type for a Workpool instance that has an enqueueAction method.
|
|
218
|
+
* This is compatible with @convex-dev/workpool's Workpool class.
|
|
219
|
+
*/
|
|
220
|
+
type WorkpoolLike = {
|
|
221
|
+
enqueueAction: (ctx: GenericMutationCtx<GenericDataModel>, fn: FunctionReference<"action", FunctionVisibility, any, any>, fnArgs: any, options?: any) => Promise<any>;
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* Creates a workpool bridge mutation that can be used with defineAgentApi.
|
|
225
|
+
*
|
|
226
|
+
* This helper creates an internal mutation that forwards action execution to your workpool,
|
|
227
|
+
* allowing the agent to use workpool's parallelism controls and retry mechanisms.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```typescript
|
|
231
|
+
* // convex/workpool.ts
|
|
232
|
+
* import { Workpool } from "@convex-dev/workpool";
|
|
233
|
+
* import { components } from "./_generated/api";
|
|
234
|
+
* import { createWorkpoolBridge } from "convex-durable-agents";
|
|
235
|
+
*
|
|
236
|
+
* const pool = new Workpool(components.workpool, { maxParallelism: 5 });
|
|
237
|
+
* export const { enqueueWorkpoolAction } = createWorkpoolBridge(pool);
|
|
238
|
+
*
|
|
239
|
+
* // convex/chat.ts
|
|
240
|
+
* export const { createThread, sendMessage, ... } = defineAgentApi(
|
|
241
|
+
* components.durable_agent,
|
|
242
|
+
* internal.chat.chatAgentHandler,
|
|
243
|
+
* { workpoolEnqueueAction: internal.workpool.enqueueWorkpoolAction }
|
|
244
|
+
* );
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export declare function createWorkpoolBridge(workpool: WorkpoolLike): {
|
|
248
|
+
enqueueWorkpoolAction: RegisteredMutation<"internal", {
|
|
249
|
+
action: string;
|
|
250
|
+
args: any;
|
|
251
|
+
}, Promise<null>>;
|
|
252
|
+
};
|
|
204
253
|
export {};
|
|
205
254
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,YAAY,EAAE,UAAU,EAAm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b,GAAG,EAAE,kBAAkB,CAAC,gBAAgB,CAAC,EACzC,EAAE,EAAE,iBAAiB,CAAC,QAAQ,EAAE,kBAAkB,EAAE,GAAG,EAAE,GAAG,CAAC,EAC7D,MAAM,EAAE,GAAG,EACX,OAAO,CAAC,EAAE,GAAG,KACV,OAAO,CAAC,GAAG,CAAC,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,YAAY;;;;;EAkB1D"}
|
package/dist/client/index.js
CHANGED
|
@@ -10,6 +10,8 @@ const vClientThreadDoc = v.object({
|
|
|
10
10
|
stopSignal: v.boolean(),
|
|
11
11
|
streamId: v.optional(v.union(v.string(), v.null())),
|
|
12
12
|
streamFnHandle: v.string(),
|
|
13
|
+
workpoolEnqueueAction: v.optional(v.string()),
|
|
14
|
+
toolExecutionWorkpoolEnqueueAction: v.optional(v.string()),
|
|
13
15
|
});
|
|
14
16
|
// ============================================================================
|
|
15
17
|
// Tool Definition Helpers
|
|
@@ -248,12 +250,16 @@ export function streamHandlerAction(component, { tools, saveStreamDeltas, transf
|
|
|
248
250
|
},
|
|
249
251
|
returns: v.null(),
|
|
250
252
|
handler: async (ctx, args) => {
|
|
251
|
-
const thread = await ctx.runQuery(component.threads.get, {
|
|
253
|
+
const thread = await ctx.runQuery(component.threads.get, {
|
|
254
|
+
threadId: args.threadId,
|
|
255
|
+
});
|
|
252
256
|
if (thread?.streamId !== args.streamId) {
|
|
253
257
|
throw new Error(`Thread ${args.threadId} streamId mismatch: ${thread?.streamId} !== ${args.streamId}`);
|
|
254
258
|
}
|
|
255
259
|
// Get the current message order for streaming
|
|
256
|
-
const messages = await ctx.runQuery(component.messages.list, {
|
|
260
|
+
const messages = await ctx.runQuery(component.messages.list, {
|
|
261
|
+
threadId: args.threadId,
|
|
262
|
+
});
|
|
257
263
|
const currentOrder = messages.length > 0 ? Math.max(...messages.map((m) => m.order)) + 1 : 0;
|
|
258
264
|
// Set up delta streamer if enabled
|
|
259
265
|
let streamer;
|
|
@@ -433,7 +439,20 @@ async function checkThreadIsIdle(component, ctx, threadId) {
|
|
|
433
439
|
throw new Error(`Thread ${threadId} status=${thread.status}, cannot resume`);
|
|
434
440
|
}
|
|
435
441
|
}
|
|
436
|
-
function
|
|
442
|
+
async function serializeWorkpoolOptions(options) {
|
|
443
|
+
const result = {};
|
|
444
|
+
if (options?.workpoolEnqueueAction) {
|
|
445
|
+
const handle = await createFunctionHandle(options.workpoolEnqueueAction);
|
|
446
|
+
result.workpoolEnqueueAction = handle.toString();
|
|
447
|
+
}
|
|
448
|
+
if (options?.toolExecutionWorkpoolEnqueueAction) {
|
|
449
|
+
const handle = await createFunctionHandle(options.toolExecutionWorkpoolEnqueueAction);
|
|
450
|
+
result.toolExecutionWorkpoolEnqueueAction = handle.toString();
|
|
451
|
+
}
|
|
452
|
+
return result;
|
|
453
|
+
}
|
|
454
|
+
function createAgentApi(component, ref, action, query, mutation, options) {
|
|
455
|
+
const authorize = options?.authorizationCallback;
|
|
437
456
|
return {
|
|
438
457
|
createThread: action({
|
|
439
458
|
args: {
|
|
@@ -443,8 +462,11 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
443
462
|
handler: async (ctx, args) => {
|
|
444
463
|
// Create a function handle that can be scheduled from within the component
|
|
445
464
|
const handle = await createFunctionHandle(ref);
|
|
465
|
+
// Serialize workpool options
|
|
466
|
+
const serializedWorkpool = await serializeWorkpoolOptions(options);
|
|
446
467
|
const thread = await ctx.runMutation(component.threads.create, {
|
|
447
468
|
streamFnHandle: handle,
|
|
469
|
+
...serializedWorkpool,
|
|
448
470
|
});
|
|
449
471
|
if (args.prompt) {
|
|
450
472
|
await ctx.runMutation(component.messages.add, {
|
|
@@ -468,6 +490,8 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
468
490
|
},
|
|
469
491
|
returns: v.null(),
|
|
470
492
|
handler: async (ctx, args) => {
|
|
493
|
+
if (authorize)
|
|
494
|
+
await authorize(ctx, args.threadId);
|
|
471
495
|
await checkThreadIsIdle(component, ctx, args.threadId);
|
|
472
496
|
await ctx.runMutation(component.messages.add, {
|
|
473
497
|
threadId: args.threadId,
|
|
@@ -492,6 +516,8 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
492
516
|
},
|
|
493
517
|
returns: v.null(),
|
|
494
518
|
handler: async (ctx, args) => {
|
|
519
|
+
if (authorize)
|
|
520
|
+
await authorize(ctx, args.threadId);
|
|
495
521
|
const threadId = args.threadId;
|
|
496
522
|
await checkThreadIsIdle(component, ctx, threadId);
|
|
497
523
|
if (args.prompt) {
|
|
@@ -519,6 +545,8 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
519
545
|
},
|
|
520
546
|
returns: v.null(),
|
|
521
547
|
handler: async (ctx, args) => {
|
|
548
|
+
if (authorize)
|
|
549
|
+
await authorize(ctx, args.threadId);
|
|
522
550
|
await ctx.runMutation(component.threads.setStopSignal, {
|
|
523
551
|
threadId: args.threadId,
|
|
524
552
|
stopSignal: true,
|
|
@@ -532,7 +560,11 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
532
560
|
},
|
|
533
561
|
returns: v.union(vClientThreadDoc, v.null()),
|
|
534
562
|
handler: async (ctx, args) => {
|
|
535
|
-
|
|
563
|
+
if (authorize)
|
|
564
|
+
await authorize(ctx, args.threadId);
|
|
565
|
+
return ctx.runQuery(component.threads.get, {
|
|
566
|
+
threadId: args.threadId,
|
|
567
|
+
});
|
|
536
568
|
},
|
|
537
569
|
}),
|
|
538
570
|
listMessages: query({
|
|
@@ -540,19 +572,30 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
540
572
|
threadId: v.string(),
|
|
541
573
|
},
|
|
542
574
|
handler: async (ctx, args) => {
|
|
543
|
-
|
|
575
|
+
if (authorize)
|
|
576
|
+
await authorize(ctx, args.threadId);
|
|
577
|
+
return ctx.runQuery(component.messages.list, {
|
|
578
|
+
threadId: args.threadId,
|
|
579
|
+
});
|
|
544
580
|
},
|
|
545
581
|
}),
|
|
546
582
|
listMessagesWithStreams: query({
|
|
547
583
|
args: {
|
|
548
584
|
threadId: v.string(),
|
|
549
|
-
streamArgs: v.optional(v.union(v.object({
|
|
585
|
+
streamArgs: v.optional(v.union(v.object({
|
|
586
|
+
kind: v.literal("list"),
|
|
587
|
+
startOrder: v.optional(v.number()),
|
|
588
|
+
}), v.object({
|
|
550
589
|
kind: v.literal("deltas"),
|
|
551
590
|
cursors: v.array(v.object({ streamId: v.string(), cursor: v.number() })),
|
|
552
591
|
}))),
|
|
553
592
|
},
|
|
554
593
|
handler: async (ctx, args) => {
|
|
555
|
-
|
|
594
|
+
if (authorize)
|
|
595
|
+
await authorize(ctx, args.threadId);
|
|
596
|
+
const messages = await ctx.runQuery(component.messages.list, {
|
|
597
|
+
threadId: args.threadId,
|
|
598
|
+
});
|
|
556
599
|
if (!args.streamArgs) {
|
|
557
600
|
return { messages };
|
|
558
601
|
}
|
|
@@ -601,7 +644,11 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
601
644
|
},
|
|
602
645
|
returns: v.null(),
|
|
603
646
|
handler: async (ctx, args) => {
|
|
604
|
-
|
|
647
|
+
if (authorize)
|
|
648
|
+
await authorize(ctx, args.threadId);
|
|
649
|
+
await ctx.runMutation(component.threads.remove, {
|
|
650
|
+
threadId: args.threadId,
|
|
651
|
+
});
|
|
605
652
|
return null;
|
|
606
653
|
},
|
|
607
654
|
}),
|
|
@@ -638,13 +685,52 @@ function createAgentApi(component, ref, action, query, mutation) {
|
|
|
638
685
|
/**
|
|
639
686
|
* Define a public agent API that can be called from clients.
|
|
640
687
|
*/
|
|
641
|
-
export function defineAgentApi(component, ref) {
|
|
642
|
-
return createAgentApi(component, ref, actionGeneric, queryGeneric, mutationGeneric);
|
|
688
|
+
export function defineAgentApi(component, ref, options) {
|
|
689
|
+
return createAgentApi(component, ref, actionGeneric, queryGeneric, mutationGeneric, options);
|
|
643
690
|
}
|
|
644
691
|
/**
|
|
645
692
|
* Define an internal agent API that can only be called from other Convex functions.
|
|
646
693
|
*/
|
|
647
|
-
export function defineInternalAgentApi(component, ref) {
|
|
648
|
-
return createAgentApi(component, ref, internalActionGeneric, internalQueryGeneric, internalMutationGeneric);
|
|
694
|
+
export function defineInternalAgentApi(component, ref, options) {
|
|
695
|
+
return createAgentApi(component, ref, internalActionGeneric, internalQueryGeneric, internalMutationGeneric, options);
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* Creates a workpool bridge mutation that can be used with defineAgentApi.
|
|
699
|
+
*
|
|
700
|
+
* This helper creates an internal mutation that forwards action execution to your workpool,
|
|
701
|
+
* allowing the agent to use workpool's parallelism controls and retry mechanisms.
|
|
702
|
+
*
|
|
703
|
+
* @example
|
|
704
|
+
* ```typescript
|
|
705
|
+
* // convex/workpool.ts
|
|
706
|
+
* import { Workpool } from "@convex-dev/workpool";
|
|
707
|
+
* import { components } from "./_generated/api";
|
|
708
|
+
* import { createWorkpoolBridge } from "convex-durable-agents";
|
|
709
|
+
*
|
|
710
|
+
* const pool = new Workpool(components.workpool, { maxParallelism: 5 });
|
|
711
|
+
* export const { enqueueWorkpoolAction } = createWorkpoolBridge(pool);
|
|
712
|
+
*
|
|
713
|
+
* // convex/chat.ts
|
|
714
|
+
* export const { createThread, sendMessage, ... } = defineAgentApi(
|
|
715
|
+
* components.durable_agent,
|
|
716
|
+
* internal.chat.chatAgentHandler,
|
|
717
|
+
* { workpoolEnqueueAction: internal.workpool.enqueueWorkpoolAction }
|
|
718
|
+
* );
|
|
719
|
+
* ```
|
|
720
|
+
*/
|
|
721
|
+
export function createWorkpoolBridge(workpool) {
|
|
722
|
+
return {
|
|
723
|
+
enqueueWorkpoolAction: internalMutationGeneric({
|
|
724
|
+
args: {
|
|
725
|
+
action: v.string(),
|
|
726
|
+
args: v.any(),
|
|
727
|
+
},
|
|
728
|
+
returns: v.null(),
|
|
729
|
+
handler: async (ctx, { action, args }) => {
|
|
730
|
+
await workpool.enqueueAction(ctx, action, args);
|
|
731
|
+
return null;
|
|
732
|
+
},
|
|
733
|
+
}),
|
|
734
|
+
};
|
|
649
735
|
}
|
|
650
736
|
//# sourceMappingURL=index.js.map
|