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