@convex-dev/agent 0.0.15-alpha.2 → 0.0.16-alpha.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.
Files changed (84) hide show
  1. package/README.md +93 -29
  2. package/dist/commonjs/client/index.d.ts +768 -71
  3. package/dist/commonjs/client/index.d.ts.map +1 -1
  4. package/dist/commonjs/client/index.js +81 -42
  5. package/dist/commonjs/client/index.js.map +1 -1
  6. package/dist/commonjs/client/playground.d.ts +474 -0
  7. package/dist/commonjs/client/playground.d.ts.map +1 -0
  8. package/dist/commonjs/client/playground.js +178 -0
  9. package/dist/commonjs/client/playground.js.map +1 -0
  10. package/dist/commonjs/component/apiKeys.d.ts +11 -0
  11. package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
  12. package/dist/commonjs/component/apiKeys.js +69 -0
  13. package/dist/commonjs/component/apiKeys.js.map +1 -0
  14. package/dist/commonjs/component/files.d.ts +31 -0
  15. package/dist/commonjs/component/files.d.ts.map +1 -0
  16. package/dist/commonjs/component/files.js +61 -0
  17. package/dist/commonjs/component/files.js.map +1 -0
  18. package/dist/commonjs/component/messages.d.ts +40 -109
  19. package/dist/commonjs/component/messages.d.ts.map +1 -1
  20. package/dist/commonjs/component/messages.js +44 -260
  21. package/dist/commonjs/component/messages.js.map +1 -1
  22. package/dist/commonjs/component/schema.d.ts +54 -10
  23. package/dist/commonjs/component/schema.d.ts.map +1 -1
  24. package/dist/commonjs/component/schema.js +7 -2
  25. package/dist/commonjs/component/schema.js.map +1 -1
  26. package/dist/commonjs/component/threads.d.ts +95 -0
  27. package/dist/commonjs/component/threads.d.ts.map +1 -0
  28. package/dist/commonjs/component/threads.js +151 -0
  29. package/dist/commonjs/component/threads.js.map +1 -0
  30. package/dist/commonjs/component/users.d.ts +37 -0
  31. package/dist/commonjs/component/users.d.ts.map +1 -0
  32. package/dist/commonjs/component/users.js +118 -0
  33. package/dist/commonjs/component/users.js.map +1 -0
  34. package/dist/commonjs/validators.d.ts +2 -6
  35. package/dist/commonjs/validators.d.ts.map +1 -1
  36. package/dist/commonjs/validators.js +0 -1
  37. package/dist/commonjs/validators.js.map +1 -1
  38. package/dist/esm/client/index.d.ts +768 -71
  39. package/dist/esm/client/index.d.ts.map +1 -1
  40. package/dist/esm/client/index.js +81 -42
  41. package/dist/esm/client/index.js.map +1 -1
  42. package/dist/esm/client/playground.d.ts +474 -0
  43. package/dist/esm/client/playground.d.ts.map +1 -0
  44. package/dist/esm/client/playground.js +178 -0
  45. package/dist/esm/client/playground.js.map +1 -0
  46. package/dist/esm/component/apiKeys.d.ts +11 -0
  47. package/dist/esm/component/apiKeys.d.ts.map +1 -0
  48. package/dist/esm/component/apiKeys.js +69 -0
  49. package/dist/esm/component/apiKeys.js.map +1 -0
  50. package/dist/esm/component/files.d.ts +31 -0
  51. package/dist/esm/component/files.d.ts.map +1 -0
  52. package/dist/esm/component/files.js +61 -0
  53. package/dist/esm/component/files.js.map +1 -0
  54. package/dist/esm/component/messages.d.ts +40 -109
  55. package/dist/esm/component/messages.d.ts.map +1 -1
  56. package/dist/esm/component/messages.js +44 -260
  57. package/dist/esm/component/messages.js.map +1 -1
  58. package/dist/esm/component/schema.d.ts +54 -10
  59. package/dist/esm/component/schema.d.ts.map +1 -1
  60. package/dist/esm/component/schema.js +7 -2
  61. package/dist/esm/component/schema.js.map +1 -1
  62. package/dist/esm/component/threads.d.ts +95 -0
  63. package/dist/esm/component/threads.d.ts.map +1 -0
  64. package/dist/esm/component/threads.js +151 -0
  65. package/dist/esm/component/threads.js.map +1 -0
  66. package/dist/esm/component/users.d.ts +37 -0
  67. package/dist/esm/component/users.d.ts.map +1 -0
  68. package/dist/esm/component/users.js +118 -0
  69. package/dist/esm/component/users.js.map +1 -0
  70. package/dist/esm/validators.d.ts +2 -6
  71. package/dist/esm/validators.d.ts.map +1 -1
  72. package/dist/esm/validators.js +0 -1
  73. package/dist/esm/validators.js.map +1 -1
  74. package/package.json +10 -2
  75. package/src/client/index.ts +150 -117
  76. package/src/client/playground.ts +231 -0
  77. package/src/component/_generated/api.d.ts +319 -107
  78. package/src/component/apiKeys.ts +74 -0
  79. package/src/component/files.ts +72 -0
  80. package/src/component/messages.ts +54 -308
  81. package/src/component/schema.ts +7 -2
  82. package/src/component/threads.ts +184 -0
  83. package/src/component/users.ts +145 -0
  84. package/src/validators.ts +0 -1
@@ -0,0 +1,231 @@
1
+ import {
2
+ paginationOptsValidator,
3
+ queryGeneric,
4
+ mutationGeneric,
5
+ actionGeneric,
6
+ GenericDataModel,
7
+ GenericQueryCtx,
8
+ FilterApi,
9
+ FunctionReference,
10
+ ApiFromModules,
11
+ } from "convex/server";
12
+ import { vThreadDoc, type Agent } from "./index";
13
+ import type { RunQueryCtx, UseApi } from "./types";
14
+ import type { ToolSet } from "ai";
15
+ import { v } from "convex/values";
16
+ import { Mounts } from "../component/_generated/api";
17
+ import {
18
+ paginationResultValidator,
19
+ vContextOptions,
20
+ vMessage,
21
+ } from "../validators";
22
+ import { assert } from "convex-helpers";
23
+
24
+ export type PlaygroundAPI = ApiFromModules<{
25
+ playground: ReturnType<typeof definePlaygroundAPI<GenericDataModel>>;
26
+ }>["playground"];
27
+
28
+ // Playground API definition
29
+ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
30
+ component: UseApi<Mounts>,
31
+ {
32
+ agents,
33
+ userNameLookup,
34
+ }: {
35
+ agents: Agent<ToolSet>[];
36
+ userNameLookup?: (
37
+ ctx: GenericQueryCtx<DataModel>,
38
+ userId: string
39
+ ) => string | Promise<string>;
40
+ }
41
+ ) {
42
+ // Map agent name to instance
43
+ const agentMap: Record<string, Agent<ToolSet>> = Object.fromEntries(
44
+ agents.map((agent) => [agent.options.name, agent])
45
+ );
46
+
47
+ async function validateApiKey(ctx: RunQueryCtx, apiKey: string) {
48
+ await ctx.runQuery(component.apiKeys.validate, { apiKey });
49
+ }
50
+
51
+ // List all agents
52
+ const listAgents = queryGeneric({
53
+ args: {
54
+ apiKey: v.string(),
55
+ },
56
+ handler: async (ctx, args) => {
57
+ await validateApiKey(ctx, args.apiKey);
58
+ return Object.keys(agentMap);
59
+ },
60
+ returns: v.array(v.string()),
61
+ });
62
+
63
+ const listUsers = queryGeneric({
64
+ args: {
65
+ apiKey: v.string(),
66
+ paginationOpts: paginationOptsValidator,
67
+ },
68
+ handler: async (ctx, args) => {
69
+ await validateApiKey(ctx, args.apiKey);
70
+ const users = await ctx.runQuery(component.users.listUsersWithThreads, {
71
+ paginationOpts: args.paginationOpts,
72
+ });
73
+ return {
74
+ ...users,
75
+ page: await Promise.all(
76
+ users.page.map(async (userId) => ({
77
+ id: userId,
78
+ name: userNameLookup ? await userNameLookup(ctx, userId) : userId,
79
+ }))
80
+ ),
81
+ };
82
+ },
83
+ returns: paginationResultValidator(
84
+ v.object({
85
+ id: v.string(),
86
+ name: v.string(),
87
+ })
88
+ ),
89
+ });
90
+
91
+ // List threads for a user (query)
92
+ const listThreads = queryGeneric({
93
+ args: {
94
+ apiKey: v.string(),
95
+ userId: v.string(),
96
+ paginationOpts: paginationOptsValidator,
97
+ },
98
+ handler: async (ctx, args) => {
99
+ await validateApiKey(ctx, args.apiKey);
100
+ const results = await ctx.runQuery(
101
+ component.threads.listThreadsByUserId,
102
+ {
103
+ userId: args.userId,
104
+ paginationOpts: args.paginationOpts,
105
+ order: "desc",
106
+ }
107
+ );
108
+ return {
109
+ ...results,
110
+ page: await Promise.all(
111
+ results.page.map(async (thread) => {
112
+ const {
113
+ page: [last],
114
+ } = await ctx.runQuery(component.messages.listMessagesByThreadId, {
115
+ threadId: thread._id,
116
+ order: "desc",
117
+ paginationOpts: {
118
+ numItems: 1,
119
+ cursor: null,
120
+ },
121
+ });
122
+ return {
123
+ ...thread,
124
+ latestMessage: last?.text,
125
+ lastMessageAt: last?._creationTime,
126
+ };
127
+ })
128
+ ),
129
+ };
130
+ },
131
+ });
132
+
133
+ // List messages for a thread (query)
134
+ const listMessages = queryGeneric({
135
+ args: {
136
+ apiKey: v.string(),
137
+ threadId: v.string(),
138
+ paginationOpts: paginationOptsValidator,
139
+ },
140
+ handler: async (ctx, args) => {
141
+ await validateApiKey(ctx, args.apiKey);
142
+ return ctx.runQuery(component.messages.listMessagesByThreadId, {
143
+ threadId: args.threadId,
144
+ paginationOpts: args.paginationOpts,
145
+ order: "desc",
146
+ statuses: ["success", "failed", "pending"],
147
+ });
148
+ },
149
+ });
150
+
151
+ // Create a thread (mutation)
152
+ const createThread = mutationGeneric({
153
+ args: {
154
+ apiKey: v.string(),
155
+ agentName: v.string(),
156
+ userId: v.string(),
157
+ title: v.optional(v.string()),
158
+ summary: v.optional(v.string()),
159
+ },
160
+ handler: async (ctx, args) => {
161
+ await validateApiKey(ctx, args.apiKey);
162
+ const agent = agentMap[args.agentName];
163
+ if (!agent) throw new Error(`Unknown agent: ${args.agentName}`);
164
+ return agent.createThread(ctx, {
165
+ userId: args.userId,
166
+ title: args.title,
167
+ summary: args.summary,
168
+ });
169
+ },
170
+ });
171
+
172
+ // Send a message (action)
173
+ const generateText = actionGeneric({
174
+ args: {
175
+ apiKey: v.string(),
176
+ agentName: v.string(),
177
+ userId: v.string(),
178
+ threadId: v.string(),
179
+ prompt: v.optional(v.string()),
180
+ messages: v.optional(v.array(vMessage)),
181
+ // Add more args as needed
182
+ },
183
+ handler: async (ctx, args) => {
184
+ await validateApiKey(ctx, args.apiKey);
185
+ const { threadId, userId, messages, prompt, agentName } = args;
186
+ const agent = agentMap[agentName];
187
+ if (!agent) throw new Error(`Unknown agent: ${agentName}`);
188
+ const { thread } = await agent.continueThread(ctx, { threadId, userId });
189
+ // Prefer messages if provided, else prompt
190
+ assert(messages || prompt, "Must provide either messages or prompt");
191
+ assert(!messages || !prompt, "Provide messages or prompt, not both");
192
+ const result = await thread.generateText({ prompt, messages });
193
+ return result;
194
+ },
195
+ });
196
+
197
+ // Fetch prompt context (action)
198
+ const fetchPromptContext = actionGeneric({
199
+ args: {
200
+ apiKey: v.string(),
201
+ agentName: v.string(),
202
+ userId: v.optional(v.string()),
203
+ threadId: v.optional(v.string()),
204
+ messages: v.array(vMessage),
205
+ contextOptions: vContextOptions,
206
+ beforeMessageId: v.optional(v.string()),
207
+ },
208
+ handler: async (ctx, args) => {
209
+ await validateApiKey(ctx, args.apiKey);
210
+ const agent = agentMap[args.agentName];
211
+ if (!agent) throw new Error(`Unknown agent: ${args.agentName}`);
212
+ return agent.fetchContextMessages(ctx, {
213
+ userId: args.userId,
214
+ threadId: args.threadId,
215
+ messages: args.messages,
216
+ contextOptions: args.contextOptions,
217
+ beforeMessageId: args.beforeMessageId,
218
+ });
219
+ },
220
+ });
221
+
222
+ return {
223
+ listUsers,
224
+ listThreads,
225
+ listMessages,
226
+ listAgents,
227
+ createThread,
228
+ generateText,
229
+ fetchPromptContext,
230
+ };
231
+ }