@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.
- package/README.md +159 -98
- package/dist/commonjs/client/index.d.ts +806 -189
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +178 -28
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/playground.d.ts +609 -0
- package/dist/commonjs/client/playground.d.ts.map +1 -0
- package/dist/commonjs/client/playground.js +200 -0
- package/dist/commonjs/client/playground.js.map +1 -0
- package/dist/commonjs/component/apiKeys.d.ts +11 -0
- package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
- package/dist/commonjs/component/apiKeys.js +69 -0
- package/dist/commonjs/component/apiKeys.js.map +1 -0
- package/dist/commonjs/component/files.d.ts +31 -0
- package/dist/commonjs/component/files.d.ts.map +1 -0
- package/dist/commonjs/component/files.js +61 -0
- package/dist/commonjs/component/files.js.map +1 -0
- package/dist/commonjs/component/messages.d.ts +720 -676
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +99 -287
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +1124 -1324
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +26 -14
- package/dist/commonjs/component/schema.js.map +1 -1
- package/dist/commonjs/component/threads.d.ts +95 -0
- package/dist/commonjs/component/threads.d.ts.map +1 -0
- package/dist/commonjs/component/threads.js +151 -0
- package/dist/commonjs/component/threads.js.map +1 -0
- package/dist/commonjs/component/users.d.ts +37 -0
- package/dist/commonjs/component/users.d.ts.map +1 -0
- package/dist/commonjs/component/users.js +118 -0
- package/dist/commonjs/component/users.js.map +1 -0
- package/dist/commonjs/component/vector/index.d.ts.map +1 -1
- package/dist/commonjs/component/vector/index.js +4 -6
- package/dist/commonjs/component/vector/index.js.map +1 -1
- package/dist/commonjs/component/vector/tables.d.ts +10 -10
- package/dist/commonjs/component/vector/tables.d.ts.map +1 -1
- package/dist/commonjs/component/vector/tables.js.map +1 -1
- package/dist/commonjs/mapping.d.ts +7 -1
- package/dist/commonjs/mapping.d.ts.map +1 -1
- package/dist/commonjs/mapping.js +39 -18
- package/dist/commonjs/mapping.js.map +1 -1
- package/dist/commonjs/validators.d.ts +1669 -2340
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +35 -26
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +806 -189
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +178 -28
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/playground.d.ts +609 -0
- package/dist/esm/client/playground.d.ts.map +1 -0
- package/dist/esm/client/playground.js +200 -0
- package/dist/esm/client/playground.js.map +1 -0
- package/dist/esm/component/apiKeys.d.ts +11 -0
- package/dist/esm/component/apiKeys.d.ts.map +1 -0
- package/dist/esm/component/apiKeys.js +69 -0
- package/dist/esm/component/apiKeys.js.map +1 -0
- package/dist/esm/component/files.d.ts +31 -0
- package/dist/esm/component/files.d.ts.map +1 -0
- package/dist/esm/component/files.js +61 -0
- package/dist/esm/component/files.js.map +1 -0
- package/dist/esm/component/messages.d.ts +720 -676
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +99 -287
- package/dist/esm/component/messages.js.map +1 -1
- package/dist/esm/component/schema.d.ts +1124 -1324
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +26 -14
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/threads.d.ts +95 -0
- package/dist/esm/component/threads.d.ts.map +1 -0
- package/dist/esm/component/threads.js +151 -0
- package/dist/esm/component/threads.js.map +1 -0
- package/dist/esm/component/users.d.ts +37 -0
- package/dist/esm/component/users.d.ts.map +1 -0
- package/dist/esm/component/users.js +118 -0
- package/dist/esm/component/users.js.map +1 -0
- package/dist/esm/component/vector/index.d.ts.map +1 -1
- package/dist/esm/component/vector/index.js +4 -6
- package/dist/esm/component/vector/index.js.map +1 -1
- package/dist/esm/component/vector/tables.d.ts +10 -10
- package/dist/esm/component/vector/tables.d.ts.map +1 -1
- package/dist/esm/component/vector/tables.js.map +1 -1
- package/dist/esm/mapping.d.ts +7 -1
- package/dist/esm/mapping.d.ts.map +1 -1
- package/dist/esm/mapping.js +39 -18
- package/dist/esm/mapping.js.map +1 -1
- package/dist/esm/validators.d.ts +1669 -2340
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +35 -26
- package/dist/esm/validators.js.map +1 -1
- package/package.json +8 -1
- package/src/client/index.ts +220 -59
- package/src/client/playground.ts +257 -0
- package/src/component/_generated/api.d.ts +601 -400
- package/src/component/apiKeys.ts +74 -0
- package/src/component/files.ts +72 -0
- package/src/component/messages.test.ts +23 -0
- package/src/component/messages.ts +127 -341
- package/src/component/schema.ts +32 -13
- package/src/component/threads.ts +184 -0
- package/src/component/users.ts +145 -0
- package/src/component/vector/index.ts +5 -6
- package/src/component/vector/tables.ts +15 -15
- package/src/mapping.ts +65 -32
- 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
|
});
|