@convex-dev/agent 0.0.16 → 0.0.17-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 (99) hide show
  1. package/README.md +169 -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/component/apiKeys.d.ts +11 -0
  7. package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
  8. package/dist/commonjs/component/apiKeys.js +69 -0
  9. package/dist/commonjs/component/apiKeys.js.map +1 -0
  10. package/dist/commonjs/component/files.d.ts +31 -0
  11. package/dist/commonjs/component/files.d.ts.map +1 -0
  12. package/dist/commonjs/component/files.js +61 -0
  13. package/dist/commonjs/component/files.js.map +1 -0
  14. package/dist/commonjs/component/messages.d.ts +720 -676
  15. package/dist/commonjs/component/messages.d.ts.map +1 -1
  16. package/dist/commonjs/component/messages.js +99 -287
  17. package/dist/commonjs/component/messages.js.map +1 -1
  18. package/dist/commonjs/component/schema.d.ts +1124 -1324
  19. package/dist/commonjs/component/schema.d.ts.map +1 -1
  20. package/dist/commonjs/component/schema.js +26 -14
  21. package/dist/commonjs/component/schema.js.map +1 -1
  22. package/dist/commonjs/component/threads.d.ts +95 -0
  23. package/dist/commonjs/component/threads.d.ts.map +1 -0
  24. package/dist/commonjs/component/threads.js +151 -0
  25. package/dist/commonjs/component/threads.js.map +1 -0
  26. package/dist/commonjs/component/users.d.ts +37 -0
  27. package/dist/commonjs/component/users.d.ts.map +1 -0
  28. package/dist/commonjs/component/users.js +118 -0
  29. package/dist/commonjs/component/users.js.map +1 -0
  30. package/dist/commonjs/component/vector/index.d.ts.map +1 -1
  31. package/dist/commonjs/component/vector/index.js +4 -6
  32. package/dist/commonjs/component/vector/index.js.map +1 -1
  33. package/dist/commonjs/component/vector/tables.d.ts +10 -10
  34. package/dist/commonjs/component/vector/tables.d.ts.map +1 -1
  35. package/dist/commonjs/component/vector/tables.js.map +1 -1
  36. package/dist/commonjs/mapping.d.ts +7 -1
  37. package/dist/commonjs/mapping.d.ts.map +1 -1
  38. package/dist/commonjs/mapping.js +39 -18
  39. package/dist/commonjs/mapping.js.map +1 -1
  40. package/dist/commonjs/validators.d.ts +1669 -2340
  41. package/dist/commonjs/validators.d.ts.map +1 -1
  42. package/dist/commonjs/validators.js +35 -26
  43. package/dist/commonjs/validators.js.map +1 -1
  44. package/dist/esm/client/index.d.ts +806 -189
  45. package/dist/esm/client/index.d.ts.map +1 -1
  46. package/dist/esm/client/index.js +178 -28
  47. package/dist/esm/client/index.js.map +1 -1
  48. package/dist/esm/component/apiKeys.d.ts +11 -0
  49. package/dist/esm/component/apiKeys.d.ts.map +1 -0
  50. package/dist/esm/component/apiKeys.js +69 -0
  51. package/dist/esm/component/apiKeys.js.map +1 -0
  52. package/dist/esm/component/files.d.ts +31 -0
  53. package/dist/esm/component/files.d.ts.map +1 -0
  54. package/dist/esm/component/files.js +61 -0
  55. package/dist/esm/component/files.js.map +1 -0
  56. package/dist/esm/component/messages.d.ts +720 -676
  57. package/dist/esm/component/messages.d.ts.map +1 -1
  58. package/dist/esm/component/messages.js +99 -287
  59. package/dist/esm/component/messages.js.map +1 -1
  60. package/dist/esm/component/schema.d.ts +1124 -1324
  61. package/dist/esm/component/schema.d.ts.map +1 -1
  62. package/dist/esm/component/schema.js +26 -14
  63. package/dist/esm/component/schema.js.map +1 -1
  64. package/dist/esm/component/threads.d.ts +95 -0
  65. package/dist/esm/component/threads.d.ts.map +1 -0
  66. package/dist/esm/component/threads.js +151 -0
  67. package/dist/esm/component/threads.js.map +1 -0
  68. package/dist/esm/component/users.d.ts +37 -0
  69. package/dist/esm/component/users.d.ts.map +1 -0
  70. package/dist/esm/component/users.js +118 -0
  71. package/dist/esm/component/users.js.map +1 -0
  72. package/dist/esm/component/vector/index.d.ts.map +1 -1
  73. package/dist/esm/component/vector/index.js +4 -6
  74. package/dist/esm/component/vector/index.js.map +1 -1
  75. package/dist/esm/component/vector/tables.d.ts +10 -10
  76. package/dist/esm/component/vector/tables.d.ts.map +1 -1
  77. package/dist/esm/component/vector/tables.js.map +1 -1
  78. package/dist/esm/mapping.d.ts +7 -1
  79. package/dist/esm/mapping.d.ts.map +1 -1
  80. package/dist/esm/mapping.js +39 -18
  81. package/dist/esm/mapping.js.map +1 -1
  82. package/dist/esm/validators.d.ts +1669 -2340
  83. package/dist/esm/validators.d.ts.map +1 -1
  84. package/dist/esm/validators.js +35 -26
  85. package/dist/esm/validators.js.map +1 -1
  86. package/package.json +1 -1
  87. package/src/client/index.ts +220 -59
  88. package/src/component/_generated/api.d.ts +601 -400
  89. package/src/component/apiKeys.ts +74 -0
  90. package/src/component/files.ts +72 -0
  91. package/src/component/messages.test.ts +23 -0
  92. package/src/component/messages.ts +127 -341
  93. package/src/component/schema.ts +32 -13
  94. package/src/component/threads.ts +184 -0
  95. package/src/component/users.ts +145 -0
  96. package/src/component/vector/index.ts +5 -6
  97. package/src/component/vector/tables.ts +15 -15
  98. package/src/mapping.ts +65 -32
  99. package/src/validators.ts +47 -28
@@ -0,0 +1,74 @@
1
+ import { v } from "convex/values";
2
+ import { mutation, query } from "./_generated/server";
3
+
4
+ export const issue = mutation({
5
+ args: {
6
+ name: v.optional(v.string()),
7
+ },
8
+ handler: async (ctx, args) => {
9
+ if (args.name) {
10
+ const existingApiKey = await ctx.db
11
+ .query("apiKeys")
12
+ .withIndex("name", (q) => q.eq("name", args.name))
13
+ .first();
14
+ if (existingApiKey) {
15
+ console.warn(`API key ${args.name} already exists, deleting...`);
16
+ await ctx.db.delete(existingApiKey._id);
17
+ }
18
+ }
19
+ const apiKey = await ctx.db.insert("apiKeys", args);
20
+ return apiKey;
21
+ },
22
+ returns: v.id("apiKeys"),
23
+ });
24
+
25
+ export const validate = query({
26
+ args: {
27
+ apiKey: v.id("apiKeys"),
28
+ },
29
+ handler: async (ctx, args) => {
30
+ const apiKey = await ctx.db.get(args.apiKey);
31
+ if (!apiKey) {
32
+ throw new Error("Invalid API key");
33
+ }
34
+ return true;
35
+ },
36
+ returns: v.boolean(),
37
+ });
38
+
39
+ export const destroy = mutation({
40
+ args: v.object({
41
+ apiKey: v.optional(v.id("apiKeys")),
42
+ name: v.optional(v.string()),
43
+ }),
44
+ handler: async (ctx, args) => {
45
+ if (args.apiKey) {
46
+ const apiKey = await ctx.db.get(args.apiKey);
47
+ if (!apiKey) {
48
+ return "missing";
49
+ }
50
+ if (apiKey.name !== args.name) {
51
+ return "name mismatch";
52
+ }
53
+ await ctx.db.delete(args.apiKey);
54
+ } else if (args.name) {
55
+ const apiKey = await ctx.db
56
+ .query("apiKeys")
57
+ .withIndex("name", (q) => q.eq("name", args.name))
58
+ .first();
59
+ if (!apiKey) {
60
+ return "missing";
61
+ }
62
+ await ctx.db.delete(apiKey._id);
63
+ } else {
64
+ return "must provide either apiKey or name";
65
+ }
66
+ return "deleted";
67
+ },
68
+ returns: v.union(
69
+ v.literal("missing"),
70
+ v.literal("deleted"),
71
+ v.literal("name mismatch"),
72
+ v.literal("must provide either apiKey or name")
73
+ ),
74
+ });
@@ -0,0 +1,72 @@
1
+ import { paginator } from "convex-helpers/server/pagination";
2
+ import { mutation, MutationCtx, query } from "./_generated/server.js";
3
+ import { schema, v } from "./schema.js";
4
+ import { Id } from "./_generated/dataModel.js";
5
+
6
+ export const addFile = mutation({
7
+ args: {
8
+ storageId: v.string(),
9
+ hash: v.string(),
10
+ },
11
+ handler: addFileHandler,
12
+ returns: v.id("files"),
13
+ });
14
+
15
+ export async function addFileHandler(
16
+ ctx: MutationCtx,
17
+ args: { storageId: string; hash: string }
18
+ ) {
19
+ const fileId = await ctx.db.insert("files", {
20
+ storageId: args.storageId,
21
+ hash: args.hash,
22
+ refcount: 1,
23
+ });
24
+ return fileId;
25
+ }
26
+
27
+ export const resuseFile = mutation({
28
+ args: {
29
+ fileId: v.id("files"),
30
+ },
31
+ handler: reuseFileHandler,
32
+ returns: v.null(),
33
+ });
34
+
35
+ export async function reuseFileHandler(
36
+ ctx: MutationCtx,
37
+ args: { fileId: Id<"files"> }
38
+ ) {
39
+ const file = await ctx.db.get(args.fileId);
40
+ if (!file) {
41
+ throw new Error("File not found");
42
+ }
43
+ await ctx.db.patch(args.fileId, {
44
+ refcount: file.refcount + 1,
45
+ });
46
+ }
47
+
48
+ export const getFilesToDelete = query({
49
+ args: {
50
+ cursor: v.optional(v.string()),
51
+ limit: v.optional(v.number()),
52
+ },
53
+ handler: async (ctx, args) => {
54
+ const files = await paginator(ctx.db, schema)
55
+ .query("files")
56
+ .withIndex("refcount", (q) => q.eq("refcount", 0))
57
+ .paginate({
58
+ numItems: args.limit ?? 100,
59
+ cursor: args.cursor ?? null,
60
+ });
61
+ return {
62
+ files: files.page,
63
+ continueCursor: files.continueCursor,
64
+ isDone: files.isDone,
65
+ };
66
+ },
67
+ returns: v.object({
68
+ files: v.array(v.doc("files")),
69
+ continueCursor: v.string(),
70
+ isDone: v.boolean(),
71
+ }),
72
+ });
@@ -5,9 +5,32 @@ import { convexTest } from "convex-test";
5
5
  import schema from "./schema.js";
6
6
  import { api } from "./_generated/api.js";
7
7
  import { modules } from "./setup.test.js";
8
+ import { Id } from "./_generated/dataModel.js";
9
+ import { getMaxMessage } from "./messages.js";
8
10
 
9
11
  describe("agent", () => {
10
12
  test("add and subtract", async () => {
11
13
  const t = convexTest(schema, modules);
12
14
  });
15
+ test("getMaxMessage works for threads", async () => {
16
+ const t = convexTest(schema, modules);
17
+ const thread = await t.mutation(api.threads.createThread, {
18
+ userId: "test",
19
+ });
20
+ const { messages } = await t.mutation(api.messages.addMessages, {
21
+ threadId: thread._id as Id<"threads">,
22
+ messages: [
23
+ { message: { role: "user", content: "hello" } },
24
+ { message: { role: "assistant", content: "world" } },
25
+ ],
26
+ });
27
+ const maxMessage = await t.run(async (ctx) => {
28
+ return await getMaxMessage(ctx, thread._id as Id<"threads">, "test");
29
+ });
30
+ expect(maxMessage).toMatchObject({
31
+ _id: messages.at(-1)!._id,
32
+ order: 1,
33
+ stepOrder: 0,
34
+ });
35
+ });
13
36
  });