@convex-dev/agent 0.1.18-alpha.1 → 0.2.0-alpha.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.
Files changed (96) hide show
  1. package/dist/client/createTool.d.ts +31 -10
  2. package/dist/client/createTool.d.ts.map +1 -1
  3. package/dist/client/createTool.js +25 -10
  4. package/dist/client/createTool.js.map +1 -1
  5. package/dist/client/defaultComponent.d.ts +11 -0
  6. package/dist/client/defaultComponent.d.ts.map +1 -0
  7. package/dist/client/defaultComponent.js +7 -0
  8. package/dist/client/defaultComponent.js.map +1 -0
  9. package/dist/client/definePlaygroundAPI.d.ts +174 -199
  10. package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
  11. package/dist/client/definePlaygroundAPI.js +17 -35
  12. package/dist/client/definePlaygroundAPI.js.map +1 -1
  13. package/dist/client/files.d.ts +6 -3
  14. package/dist/client/files.d.ts.map +1 -1
  15. package/dist/client/files.js +7 -7
  16. package/dist/client/files.js.map +1 -1
  17. package/dist/client/index.d.ts +152 -589
  18. package/dist/client/index.d.ts.map +1 -1
  19. package/dist/client/index.js +138 -222
  20. package/dist/client/index.js.map +1 -1
  21. package/dist/client/messages.d.ts +538 -0
  22. package/dist/client/messages.d.ts.map +1 -0
  23. package/dist/client/messages.js +91 -0
  24. package/dist/client/messages.js.map +1 -0
  25. package/dist/client/search.d.ts +30 -7
  26. package/dist/client/search.d.ts.map +1 -1
  27. package/dist/client/search.js +20 -3
  28. package/dist/client/search.js.map +1 -1
  29. package/dist/client/streaming.d.ts +3 -2
  30. package/dist/client/streaming.d.ts.map +1 -1
  31. package/dist/client/streaming.js.map +1 -1
  32. package/dist/client/threads.d.ts +46 -0
  33. package/dist/client/threads.d.ts.map +1 -0
  34. package/dist/client/threads.js +49 -0
  35. package/dist/client/threads.js.map +1 -0
  36. package/dist/client/types.d.ts +39 -42
  37. package/dist/client/types.d.ts.map +1 -1
  38. package/dist/component/_generated/api.d.ts +437 -75
  39. package/dist/component/messages.d.ts +254 -55
  40. package/dist/component/messages.d.ts.map +1 -1
  41. package/dist/component/messages.js +29 -25
  42. package/dist/component/messages.js.map +1 -1
  43. package/dist/component/schema.d.ts +1459 -158
  44. package/dist/component/schema.d.ts.map +1 -1
  45. package/dist/component/schema.js +8 -14
  46. package/dist/component/schema.js.map +1 -1
  47. package/dist/component/streams.d.ts +180 -6
  48. package/dist/component/streams.d.ts.map +1 -1
  49. package/dist/mapping.d.ts +11 -15
  50. package/dist/mapping.d.ts.map +1 -1
  51. package/dist/mapping.js +191 -61
  52. package/dist/mapping.js.map +1 -1
  53. package/dist/react/deltas.d.ts +0 -3
  54. package/dist/react/deltas.d.ts.map +1 -1
  55. package/dist/react/deltas.js +140 -44
  56. package/dist/react/deltas.js.map +1 -1
  57. package/dist/react/optimisticallySendMessage.d.ts.map +1 -1
  58. package/dist/react/optimisticallySendMessage.js +2 -1
  59. package/dist/react/optimisticallySendMessage.js.map +1 -1
  60. package/dist/react/toUIMessages.d.ts +5 -4
  61. package/dist/react/toUIMessages.d.ts.map +1 -1
  62. package/dist/react/toUIMessages.js +103 -40
  63. package/dist/react/toUIMessages.js.map +1 -1
  64. package/dist/validators.d.ts +1978 -1210
  65. package/dist/validators.d.ts.map +1 -1
  66. package/dist/validators.js +90 -54
  67. package/dist/validators.js.map +1 -1
  68. package/package.json +32 -28
  69. package/src/client/createTool.ts +69 -38
  70. package/src/client/defaultComponent.ts +17 -0
  71. package/src/client/definePlaygroundAPI.ts +29 -43
  72. package/src/client/files.ts +7 -8
  73. package/src/client/index.test.ts +20 -18
  74. package/src/client/index.ts +228 -434
  75. package/src/client/messages.ts +191 -0
  76. package/src/client/search.ts +30 -6
  77. package/src/client/streaming.ts +4 -3
  78. package/src/client/threads.ts +78 -0
  79. package/src/client/types.ts +50 -72
  80. package/src/component/_generated/api.d.ts +437 -75
  81. package/src/component/messages.test.ts +182 -40
  82. package/src/component/messages.ts +30 -32
  83. package/src/component/schema.ts +8 -14
  84. package/src/mapping.ts +230 -94
  85. package/src/react/deltas.ts +165 -52
  86. package/src/react/optimisticallySendMessage.ts +4 -1
  87. package/src/react/toUIMessages.test.ts +154 -36
  88. package/src/react/toUIMessages.ts +136 -57
  89. package/src/validators.test.ts +2 -101
  90. package/src/validators.ts +111 -68
  91. package/dist/client/listMessages.d.ts +0 -22
  92. package/dist/client/listMessages.d.ts.map +0 -1
  93. package/dist/client/listMessages.js +0 -25
  94. package/dist/client/listMessages.js.map +0 -1
  95. package/src/client/listMessages.ts +0 -38
  96. package/src/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
@@ -1,24 +1,27 @@
1
1
  import {
2
+ actionGeneric,
3
+ mutationGeneric,
2
4
  paginationOptsValidator,
3
5
  queryGeneric,
4
- mutationGeneric,
5
- actionGeneric,
6
- type GenericDataModel,
7
- type GenericQueryCtx,
8
6
  type ApiFromModules,
9
7
  type GenericActionCtx,
8
+ type GenericDataModel,
9
+ type GenericQueryCtx,
10
10
  } from "convex/server";
11
+ import { v } from "convex/values";
11
12
  import {
13
+ createThread as createThread_,
14
+ listMessages as listMessages_,
15
+ deserializeMessage,
16
+ vContextOptions,
17
+ vMessage,
12
18
  vMessageDoc,
13
- vThreadDoc,
14
19
  vPaginationResult,
15
- vMessage,
16
- vContextOptions,
17
20
  vStorageOptions,
18
- type AgentComponent,
21
+ vThreadDoc,
19
22
  type Agent,
23
+ type AgentComponent,
20
24
  } from "./index.js";
21
- import { v } from "convex/values";
22
25
 
23
26
  export type PlaygroundAPI = ApiFromModules<{
24
27
  playground: ReturnType<typeof definePlaygroundAPI>;
@@ -58,9 +61,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
58
61
  }
59
62
 
60
63
  const isApiKeyValid = queryGeneric({
61
- args: {
62
- apiKey: v.string(),
63
- },
64
+ args: { apiKey: v.string() },
64
65
  handler: async (ctx, args) => {
65
66
  try {
66
67
  await validateApiKey(ctx, args.apiKey);
@@ -104,18 +105,14 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
104
105
  instructions: agent.options.instructions,
105
106
  contextOptions: agent.options.contextOptions,
106
107
  storageOptions: agent.options.storageOptions,
107
- maxSteps: agent.options.maxSteps,
108
- maxRetries: agent.options.maxRetries,
108
+ maxRetries: agent.options.callSettings?.maxRetries,
109
109
  tools: agent.options.tools ? Object.keys(agent.options.tools) : [],
110
110
  }));
111
111
  },
112
112
  });
113
113
 
114
114
  const listUsers = queryGeneric({
115
- args: {
116
- apiKey: v.string(),
117
- paginationOpts: paginationOptsValidator,
118
- },
115
+ args: { apiKey: v.string(), paginationOpts: paginationOptsValidator },
119
116
  handler: async (ctx, args) => {
120
117
  await validateApiKey(ctx, args.apiKey);
121
118
  const users = await ctx.runQuery(component.users.listUsersWithThreads, {
@@ -131,12 +128,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
131
128
  ),
132
129
  };
133
130
  },
134
- returns: vPaginationResult(
135
- v.object({
136
- _id: v.string(),
137
- name: v.string(),
138
- }),
139
- ),
131
+ returns: vPaginationResult(v.object({ _id: v.string(), name: v.string() })),
140
132
  });
141
133
 
142
134
  // List threads for a user (query)
@@ -165,10 +157,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
165
157
  } = await ctx.runQuery(component.messages.listMessagesByThreadId, {
166
158
  threadId: thread._id,
167
159
  order: "desc",
168
- paginationOpts: {
169
- numItems: 1,
170
- cursor: null,
171
- },
160
+ paginationOpts: { numItems: 1, cursor: null },
172
161
  });
173
162
  return {
174
163
  ...thread,
@@ -199,10 +188,9 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
199
188
  },
200
189
  handler: async (ctx, args) => {
201
190
  await validateApiKey(ctx, args.apiKey);
202
- return ctx.runQuery(component.messages.listMessagesByThreadId, {
191
+ return listMessages_(ctx, component, {
203
192
  threadId: args.threadId,
204
193
  paginationOpts: args.paginationOpts,
205
- order: "desc",
206
194
  statuses: ["success", "failed", "pending"],
207
195
  });
208
196
  },
@@ -220,18 +208,13 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
220
208
  agentName: v.optional(v.string()),
221
209
  },
222
210
  handler: async (ctx, args) => {
223
- // if (args.agentName) {
224
- // console.warn(
225
- // "Upgrade to the latest version of @convex-dev/agent-playground"
226
- // );
227
- // }
228
211
  await validateApiKey(ctx, args.apiKey);
229
- const { _id } = await ctx.runMutation(component.threads.createThread, {
212
+ const threadId = await createThread_(ctx, component, {
230
213
  userId: args.userId,
231
214
  title: args.title,
232
215
  summary: args.summary,
233
216
  });
234
- return { threadId: _id };
217
+ return { threadId };
235
218
  },
236
219
  returns: v.object({ threadId: v.string() }),
237
220
  });
@@ -260,6 +243,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
260
243
  contextOptions,
261
244
  storageOptions,
262
245
  system,
246
+ messages,
263
247
  ...rest
264
248
  } = args;
265
249
  await validateApiKey(ctx, apiKey);
@@ -270,13 +254,15 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
270
254
  const namedAgent = agents.find(({ name }) => name === agentName);
271
255
  if (!namedAgent) throw new Error(`Unknown agent: ${agentName}`);
272
256
  const { agent } = namedAgent;
273
- const { thread } = await agent.continueThread(ctx, { threadId, userId });
274
- const { messageId, text } = await thread.generateText(
275
- { ...rest, ...(system ? { system } : {}) },
257
+ const { messageId, text } = await agent.generateText(
258
+ ctx,
259
+ { threadId, userId },
276
260
  {
277
- contextOptions,
278
- storageOptions,
261
+ ...rest,
262
+ ...(system ? { system } : {}),
263
+ ...(messages ? { messages: messages.map(deserializeMessage) } : {}),
279
264
  },
265
+ { contextOptions, storageOptions },
280
266
  );
281
267
  return { messageId, text };
282
268
  },
@@ -310,7 +296,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
310
296
  const messages = await agent.fetchContextMessages(ctx, {
311
297
  userId: args.userId,
312
298
  threadId: args.threadId,
313
- messages: args.messages,
299
+ messages: args.messages.map(deserializeMessage),
314
300
  contextOptions: args.contextOptions,
315
301
  upToAndIncludingMessageId: args.beforeMessageId,
316
302
  });
@@ -22,8 +22,8 @@ type File = {
22
22
  * @param ctx A ctx object from an action.
23
23
  * @param component The agent component.
24
24
  * @param blob The blob to store.
25
- * @param filename The filename to store.
26
- * @param sha256 The sha256 hash of the file. If not provided, it will be
25
+ * @param args.filename The filename to store.
26
+ * @param args.sha256 The sha256 hash of the file. If not provided, it will be
27
27
  * computed. However, to ensure no corruption during transfer, you can
28
28
  * calculate this on the client to enforce integrity.
29
29
  * @returns The URL, fileId, and storageId of the stored file.
@@ -32,8 +32,7 @@ export async function storeFile(
32
32
  ctx: ActionCtx | RunMutationCtx,
33
33
  component: AgentComponent,
34
34
  blob: Blob,
35
- filename?: string,
36
- sha256?: string,
35
+ { filename, sha256 }: { filename?: string; sha256?: string } = {},
37
36
  ): Promise<{
38
37
  file: File;
39
38
  filePart: FilePart;
@@ -148,7 +147,7 @@ export async function getFile(
148
147
 
149
148
  function getParts(
150
149
  url: string,
151
- mimeType: string,
150
+ mediaType: string,
152
151
  filename: string | undefined,
153
152
  ): {
154
153
  filePart: FilePart;
@@ -157,14 +156,14 @@ function getParts(
157
156
  const filePart: FilePart = {
158
157
  type: "file",
159
158
  data: new URL(url),
160
- mimeType,
159
+ mediaType,
161
160
  filename,
162
161
  };
163
- const imagePart: ImagePart | undefined = mimeType.startsWith("image/")
162
+ const imagePart: ImagePart | undefined = mediaType.startsWith("image/")
164
163
  ? {
165
164
  type: "image",
166
165
  image: new URL(url),
167
- mimeType,
166
+ mediaType,
168
167
  }
169
168
  : undefined;
170
169
  return { filePart, imagePart };
@@ -19,11 +19,14 @@ import type {
19
19
  } from "convex/server";
20
20
  import { v } from "convex/values";
21
21
  import { defineSchema } from "convex/server";
22
- import { MockLanguageModelV1 } from "ai/test";
23
- import type { LanguageModelV1, LanguageModelV1StreamPart } from "ai";
24
- import { simulateReadableStream } from "ai";
22
+ import { MockLanguageModelV2 } from "ai/test";
23
+ import type {
24
+ LanguageModelV2,
25
+ LanguageModelV2StreamPart,
26
+ } from "@ai-sdk/provider";
27
+ import { simulateReadableStream, stepCountIs } from "ai";
25
28
  import { components, initConvexTest } from "./setup.test.js";
26
- import { z } from "zod";
29
+ import { z } from "zod/v3";
27
30
 
28
31
  const schema = defineSchema({});
29
32
  type DataModel = DataModelFromSchemaDefinition<typeof schema>;
@@ -252,16 +255,16 @@ describe("filterOutOrphanedToolMessages", () => {
252
255
  });
253
256
  });
254
257
 
255
- function mockModel(): LanguageModelV1 {
256
- return new MockLanguageModelV1({
258
+ function mockModel(): LanguageModelV2 {
259
+ return new MockLanguageModelV2({
257
260
  provider: "mock",
258
261
  modelId: "mock",
259
- defaultObjectGenerationMode: "json",
260
262
  // supportsStructuredOutputs: true,
261
263
  doGenerate: async ({ prompt }) => ({
262
264
  finishReason: "stop",
263
- usage: { completionTokens: 10, promptTokens: 3 },
264
- logprobs: undefined,
265
+ content: [{ type: "text", text: JSON.stringify({ prompt }) }],
266
+ warnings: [],
267
+ usage: { outputTokens: 10, inputTokens: 3, totalTokens: 13 },
265
268
  rawCall: { rawPrompt: null, rawSettings: {} },
266
269
  text: JSON.stringify({ prompt }),
267
270
  }),
@@ -277,10 +280,9 @@ function mockModel(): LanguageModelV1 {
277
280
  {
278
281
  type: "finish",
279
282
  finishReason: "stop",
280
- logprobs: undefined,
281
- usage: { completionTokens: 10, promptTokens: 3 },
283
+ usage: { outputTokens: 10, inputTokens: 3, totalTokens: 13 },
282
284
  },
283
- ] as LanguageModelV1StreamPart[],
285
+ ] as LanguageModelV2StreamPart[],
284
286
  }),
285
287
  rawCall: { rawPrompt: null, rawSettings: {} },
286
288
  }),
@@ -300,8 +302,8 @@ describe("Agent option variations and normal behavior", () => {
300
302
  instructions: "Test instructions",
301
303
  contextOptions: { recentMessages: 5 },
302
304
  storageOptions: { saveMessages: "all" },
303
- maxSteps: 2,
304
- maxRetries: 1,
305
+ stopWhen: stepCountIs(2),
306
+ callSettings: { maxRetries: 1 },
305
307
  usageHandler: async () => {},
306
308
  rawRequestResponseHandler: async () => {},
307
309
  });
@@ -346,7 +348,7 @@ describe("Agent message operations", () => {
346
348
  );
347
349
  expect(messageId).toBeTypeOf("string");
348
350
 
349
- const { lastMessageId, messages } = await t.run(async (ctx) =>
351
+ const { messages } = await t.run(async (ctx) =>
350
352
  agent.saveMessages(ctx, {
351
353
  threadId,
352
354
  userId: "4",
@@ -357,7 +359,7 @@ describe("Agent message operations", () => {
357
359
  }),
358
360
  );
359
361
  expect(messages.length).toBe(2);
360
- expect(lastMessageId).toBe(messages[1]._id);
362
+ expect(messages[1]._id).toBeDefined();
361
363
  });
362
364
  });
363
365
 
@@ -433,8 +435,8 @@ describe("Agent-generated mutations/actions/queries", () => {
433
435
  },
434
436
  ],
435
437
  });
436
- expect(result.lastMessageId).toBeDefined();
437
- expect(result.messageIds.length).toBe(1);
438
+ expect(result.messages.length).toBe(1);
439
+ expect(result.messages[0]._id).toBeDefined();
438
440
  });
439
441
  });
440
442