@convex-dev/agent 0.1.18 → 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 (91) 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 +58 -13
  10. package/dist/client/definePlaygroundAPI.d.ts.map +1 -1
  11. package/dist/client/definePlaygroundAPI.js +7 -20
  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 +143 -584
  18. package/dist/client/index.d.ts.map +1 -1
  19. package/dist/client/index.js +128 -212
  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 +249 -47
  40. package/dist/component/messages.d.ts.map +1 -1
  41. package/dist/component/messages.js +28 -24
  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/toUIMessages.d.ts +5 -4
  58. package/dist/react/toUIMessages.d.ts.map +1 -1
  59. package/dist/react/toUIMessages.js +103 -40
  60. package/dist/react/toUIMessages.js.map +1 -1
  61. package/dist/validators.d.ts +1978 -1210
  62. package/dist/validators.d.ts.map +1 -1
  63. package/dist/validators.js +90 -54
  64. package/dist/validators.js.map +1 -1
  65. package/package.json +32 -28
  66. package/src/client/createTool.ts +69 -38
  67. package/src/client/defaultComponent.ts +17 -0
  68. package/src/client/definePlaygroundAPI.ts +17 -31
  69. package/src/client/files.ts +7 -8
  70. package/src/client/index.test.ts +20 -18
  71. package/src/client/index.ts +218 -424
  72. package/src/client/messages.ts +191 -0
  73. package/src/client/search.ts +30 -6
  74. package/src/client/streaming.ts +4 -3
  75. package/src/client/threads.ts +78 -0
  76. package/src/client/types.ts +50 -72
  77. package/src/component/_generated/api.d.ts +437 -75
  78. package/src/component/messages.test.ts +182 -40
  79. package/src/component/messages.ts +27 -31
  80. package/src/component/schema.ts +8 -14
  81. package/src/mapping.ts +230 -94
  82. package/src/react/deltas.ts +165 -52
  83. package/src/react/toUIMessages.test.ts +154 -36
  84. package/src/react/toUIMessages.ts +136 -57
  85. package/src/validators.test.ts +2 -101
  86. package/src/validators.ts +111 -68
  87. package/dist/client/listMessages.d.ts +0 -22
  88. package/dist/client/listMessages.d.ts.map +0 -1
  89. package/dist/client/listMessages.js +0 -25
  90. package/dist/client/listMessages.js.map +0 -1
  91. package/src/client/listMessages.ts +0 -38
@@ -1,27 +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,
20
- listMessages as listMessages_,
21
- createThread as createThread_,
23
+ type AgentComponent,
22
24
  } from "./index.js";
23
- import { v } from "convex/values";
24
- import { deserializeMessage } from "../mapping.js";
25
25
 
26
26
  export type PlaygroundAPI = ApiFromModules<{
27
27
  playground: ReturnType<typeof definePlaygroundAPI>;
@@ -61,9 +61,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
61
61
  }
62
62
 
63
63
  const isApiKeyValid = queryGeneric({
64
- args: {
65
- apiKey: v.string(),
66
- },
64
+ args: { apiKey: v.string() },
67
65
  handler: async (ctx, args) => {
68
66
  try {
69
67
  await validateApiKey(ctx, args.apiKey);
@@ -107,18 +105,14 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
107
105
  instructions: agent.options.instructions,
108
106
  contextOptions: agent.options.contextOptions,
109
107
  storageOptions: agent.options.storageOptions,
110
- maxSteps: agent.options.maxSteps,
111
- maxRetries: agent.options.maxRetries,
108
+ maxRetries: agent.options.callSettings?.maxRetries,
112
109
  tools: agent.options.tools ? Object.keys(agent.options.tools) : [],
113
110
  }));
114
111
  },
115
112
  });
116
113
 
117
114
  const listUsers = queryGeneric({
118
- args: {
119
- apiKey: v.string(),
120
- paginationOpts: paginationOptsValidator,
121
- },
115
+ args: { apiKey: v.string(), paginationOpts: paginationOptsValidator },
122
116
  handler: async (ctx, args) => {
123
117
  await validateApiKey(ctx, args.apiKey);
124
118
  const users = await ctx.runQuery(component.users.listUsersWithThreads, {
@@ -134,12 +128,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
134
128
  ),
135
129
  };
136
130
  },
137
- returns: vPaginationResult(
138
- v.object({
139
- _id: v.string(),
140
- name: v.string(),
141
- }),
142
- ),
131
+ returns: vPaginationResult(v.object({ _id: v.string(), name: v.string() })),
143
132
  });
144
133
 
145
134
  // List threads for a user (query)
@@ -168,10 +157,7 @@ export function definePlaygroundAPI<DataModel extends GenericDataModel>(
168
157
  } = await ctx.runQuery(component.messages.listMessagesByThreadId, {
169
158
  threadId: thread._id,
170
159
  order: "desc",
171
- paginationOpts: {
172
- numItems: 1,
173
- cursor: null,
174
- },
160
+ paginationOpts: { numItems: 1, cursor: null },
175
161
  });
176
162
  return {
177
163
  ...thread,
@@ -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