@convex-dev/agent 0.0.15-alpha.2 → 0.0.16-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 +93 -29
- package/dist/commonjs/client/index.d.ts +768 -71
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +81 -42
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/playground.d.ts +474 -0
- package/dist/commonjs/client/playground.d.ts.map +1 -0
- package/dist/commonjs/client/playground.js +178 -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 +40 -109
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +44 -260
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +54 -10
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +7 -2
- 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/validators.d.ts +2 -6
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +0 -1
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +768 -71
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +81 -42
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/playground.d.ts +474 -0
- package/dist/esm/client/playground.d.ts.map +1 -0
- package/dist/esm/client/playground.js +178 -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 +40 -109
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +44 -260
- package/dist/esm/component/messages.js.map +1 -1
- package/dist/esm/component/schema.d.ts +54 -10
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +7 -2
- 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/validators.d.ts +2 -6
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +0 -1
- package/dist/esm/validators.js.map +1 -1
- package/package.json +10 -2
- package/src/client/index.ts +150 -117
- package/src/client/playground.ts +231 -0
- package/src/component/_generated/api.d.ts +319 -107
- package/src/component/apiKeys.ts +74 -0
- package/src/component/files.ts +72 -0
- package/src/component/messages.ts +54 -308
- package/src/component/schema.ts +7 -2
- package/src/component/threads.ts +184 -0
- package/src/component/users.ts +145 -0
- package/src/validators.ts +0 -1
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { assert, omit, pick } from "convex-helpers";
|
|
2
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
3
|
+
import { partial } from "convex-helpers/validators";
|
|
4
|
+
import { ObjectType } from "convex/values";
|
|
5
|
+
import { paginationResultValidator } from "../validators.js";
|
|
6
|
+
import { api, internal } from "./_generated/api.js";
|
|
7
|
+
import { Doc } from "./_generated/dataModel.js";
|
|
8
|
+
import {
|
|
9
|
+
action,
|
|
10
|
+
internalMutation,
|
|
11
|
+
mutation,
|
|
12
|
+
MutationCtx,
|
|
13
|
+
query,
|
|
14
|
+
} from "./_generated/server.js";
|
|
15
|
+
import { schema, v } from "./schema.js";
|
|
16
|
+
import { paginationOptsValidator } from "convex/server";
|
|
17
|
+
import { deleteMessage } from "./messages.js";
|
|
18
|
+
import { ThreadDoc, vThreadDoc } from "../client/index.js";
|
|
19
|
+
|
|
20
|
+
function publicThreadOrNull(thread: Doc<"threads"> | null): ThreadDoc | null {
|
|
21
|
+
if (thread === null) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return publicThread(thread);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function publicThread(thread: Doc<"threads">): ThreadDoc {
|
|
28
|
+
return omit(thread, ["defaultSystemPrompt", "parentThreadIds", "order"]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const getThread = query({
|
|
32
|
+
args: { threadId: v.id("threads") },
|
|
33
|
+
handler: async (ctx, args) => {
|
|
34
|
+
return publicThreadOrNull(await ctx.db.get(args.threadId));
|
|
35
|
+
},
|
|
36
|
+
returns: v.union(vThreadDoc, v.null()),
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const listThreadsByUserId = query({
|
|
40
|
+
args: {
|
|
41
|
+
userId: v.string(),
|
|
42
|
+
order: v.optional(v.union(v.literal("asc"), v.literal("desc"))),
|
|
43
|
+
paginationOpts: v.optional(paginationOptsValidator),
|
|
44
|
+
},
|
|
45
|
+
handler: async (ctx, args) => {
|
|
46
|
+
const threads = await paginator(ctx.db, schema)
|
|
47
|
+
.query("threads")
|
|
48
|
+
.withIndex("userId", (q) => q.eq("userId", args.userId))
|
|
49
|
+
.order(args.order ?? "desc")
|
|
50
|
+
.paginate(args.paginationOpts ?? { cursor: null, numItems: 100 });
|
|
51
|
+
return {
|
|
52
|
+
...threads,
|
|
53
|
+
page: threads.page.map(publicThread),
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
returns: paginationResultValidator(vThreadDoc),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const vThread = schema.tables.threads.validator;
|
|
60
|
+
|
|
61
|
+
export const createThread = mutation({
|
|
62
|
+
args: omit(vThread.fields, ["order", "status"]),
|
|
63
|
+
handler: async (ctx, args) => {
|
|
64
|
+
const threadId = await ctx.db.insert("threads", {
|
|
65
|
+
...args,
|
|
66
|
+
status: "active",
|
|
67
|
+
});
|
|
68
|
+
return publicThread((await ctx.db.get(threadId))!);
|
|
69
|
+
},
|
|
70
|
+
returns: vThreadDoc,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const updateThread = mutation({
|
|
74
|
+
args: {
|
|
75
|
+
threadId: v.id("threads"),
|
|
76
|
+
patch: v.object(
|
|
77
|
+
partial(pick(vThread.fields, ["title", "summary", "status"]))
|
|
78
|
+
),
|
|
79
|
+
},
|
|
80
|
+
handler: async (ctx, args) => {
|
|
81
|
+
const thread = await ctx.db.get(args.threadId);
|
|
82
|
+
assert(thread, `Thread ${args.threadId} not found`);
|
|
83
|
+
await ctx.db.patch(args.threadId, args.patch);
|
|
84
|
+
return publicThread((await ctx.db.get(args.threadId))!);
|
|
85
|
+
},
|
|
86
|
+
returns: vThreadDoc,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// When we expose this, we need to also hide all the messages and steps
|
|
90
|
+
// export const archiveThread = mutation({
|
|
91
|
+
// args: { threadId: v.id("threads") },
|
|
92
|
+
// handler: async (ctx, args) => {
|
|
93
|
+
// const thread = await ctx.db.get(args.threadId);
|
|
94
|
+
// assert(thread, `Thread ${args.threadId} not found`);
|
|
95
|
+
// await ctx.db.patch(args.threadId, { status: "archived" });
|
|
96
|
+
// return publicThread((await ctx.db.get(args.threadId))!);
|
|
97
|
+
// },
|
|
98
|
+
// returns: vThreadDoc,
|
|
99
|
+
// });
|
|
100
|
+
|
|
101
|
+
// TODO: delete thread
|
|
102
|
+
|
|
103
|
+
const deleteThreadArgs = {
|
|
104
|
+
threadId: v.id("threads"),
|
|
105
|
+
cursor: v.optional(v.string()),
|
|
106
|
+
limit: v.optional(v.number()),
|
|
107
|
+
};
|
|
108
|
+
type DeleteThreadArgs = ObjectType<typeof deleteThreadArgs>;
|
|
109
|
+
const deleteThreadReturns = {
|
|
110
|
+
cursor: v.string(),
|
|
111
|
+
isDone: v.boolean(),
|
|
112
|
+
};
|
|
113
|
+
type DeleteThreadReturns = ObjectType<typeof deleteThreadReturns>;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Use this to delete a thread and everything it contains.
|
|
117
|
+
* It will try to delete all pages synchronously.
|
|
118
|
+
* If it times out or fails, you'll have to run it again.
|
|
119
|
+
*/
|
|
120
|
+
export const deleteAllForThreadIdSync = action({
|
|
121
|
+
args: deleteThreadArgs,
|
|
122
|
+
handler: async (ctx, args) => {
|
|
123
|
+
let cursor = args.cursor;
|
|
124
|
+
while (true) {
|
|
125
|
+
const result = await ctx.runMutation(
|
|
126
|
+
internal.threads._deletePageForThreadId,
|
|
127
|
+
{ threadId: args.threadId, cursor, limit: args.limit }
|
|
128
|
+
);
|
|
129
|
+
if (result.isDone) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
cursor = result.cursor;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
returns: v.null(),
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
export const _deletePageForThreadId = internalMutation({
|
|
139
|
+
args: deleteThreadArgs,
|
|
140
|
+
handler: deletePageForThreadIdHandler,
|
|
141
|
+
returns: deleteThreadReturns,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Use this to delete a thread and everything it contains.
|
|
146
|
+
* It will continue deleting pages asynchronously.
|
|
147
|
+
*/
|
|
148
|
+
export const deleteAllForThreadIdAsync = mutation({
|
|
149
|
+
args: deleteThreadArgs,
|
|
150
|
+
handler: async (ctx, args) => {
|
|
151
|
+
const result = await deletePageForThreadIdHandler(ctx, args);
|
|
152
|
+
if (!result.isDone) {
|
|
153
|
+
await ctx.scheduler.runAfter(0, api.threads.deleteAllForThreadIdAsync, {
|
|
154
|
+
threadId: args.threadId,
|
|
155
|
+
cursor: result.cursor,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
},
|
|
160
|
+
returns: deleteThreadReturns,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
async function deletePageForThreadIdHandler(
|
|
164
|
+
ctx: MutationCtx,
|
|
165
|
+
args: DeleteThreadArgs
|
|
166
|
+
): Promise<DeleteThreadReturns> {
|
|
167
|
+
const messages = await paginator(ctx.db, schema)
|
|
168
|
+
.query("messages")
|
|
169
|
+
.withIndex("threadId_status_tool_order_stepOrder", (q) =>
|
|
170
|
+
q.eq("threadId", args.threadId)
|
|
171
|
+
)
|
|
172
|
+
.paginate({
|
|
173
|
+
numItems: args.limit ?? 100,
|
|
174
|
+
cursor: args.cursor ?? null,
|
|
175
|
+
});
|
|
176
|
+
await Promise.all(messages.page.map((m) => deleteMessage(ctx, m)));
|
|
177
|
+
if (messages.isDone) {
|
|
178
|
+
await ctx.db.delete(args.threadId);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
cursor: messages.continueCursor,
|
|
182
|
+
isDone: messages.isDone,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { paginator } from "convex-helpers/server/pagination";
|
|
2
|
+
import { nullable } from "convex-helpers/validators";
|
|
3
|
+
import { ObjectType } from "convex/values";
|
|
4
|
+
import { internal } from "./_generated/api.js";
|
|
5
|
+
import {
|
|
6
|
+
action,
|
|
7
|
+
internalMutation,
|
|
8
|
+
mutation,
|
|
9
|
+
MutationCtx,
|
|
10
|
+
query,
|
|
11
|
+
} from "./_generated/server.js";
|
|
12
|
+
import { schema, v } from "./schema.js";
|
|
13
|
+
import { deleteMessage } from "./messages.js";
|
|
14
|
+
import { paginationOptsValidator } from "convex/server";
|
|
15
|
+
import { stream } from "convex-helpers/server/stream";
|
|
16
|
+
import { paginationResultValidator } from "../validators.js";
|
|
17
|
+
|
|
18
|
+
// Note: it only searches for users with threads
|
|
19
|
+
export const listUsersWithThreads = query({
|
|
20
|
+
args: {
|
|
21
|
+
paginationOpts: paginationOptsValidator,
|
|
22
|
+
},
|
|
23
|
+
handler: async (ctx, args) => {
|
|
24
|
+
const results = await stream(ctx.db, schema)
|
|
25
|
+
.query("threads")
|
|
26
|
+
.withIndex("userId", (q) => q)
|
|
27
|
+
.filterWith(async (q) => !!q.userId)
|
|
28
|
+
.distinct(["userId"])
|
|
29
|
+
.paginate(args.paginationOpts);
|
|
30
|
+
return {
|
|
31
|
+
...results,
|
|
32
|
+
page: results.page.map((t) => t.userId).filter((t): t is string => !!t),
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
returns: paginationResultValidator(v.string()),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const deleteAllForUserId = action({
|
|
39
|
+
args: { userId: v.string() },
|
|
40
|
+
handler: async (ctx, args) => {
|
|
41
|
+
let messagesCursor = null;
|
|
42
|
+
let threadsCursor = null;
|
|
43
|
+
let isDone = false;
|
|
44
|
+
while (!isDone) {
|
|
45
|
+
const result: {
|
|
46
|
+
messagesCursor: string;
|
|
47
|
+
threadsCursor: string | null;
|
|
48
|
+
isDone: boolean;
|
|
49
|
+
} = await ctx.runMutation(internal.users._deletePageForUserId, {
|
|
50
|
+
userId: args.userId,
|
|
51
|
+
messagesCursor,
|
|
52
|
+
threadsCursor,
|
|
53
|
+
});
|
|
54
|
+
messagesCursor = result.messagesCursor;
|
|
55
|
+
threadsCursor = result.threadsCursor;
|
|
56
|
+
isDone = result.isDone;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
returns: v.null(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
export const deleteAllForUserIdAsync = mutation({
|
|
63
|
+
args: {
|
|
64
|
+
userId: v.string(),
|
|
65
|
+
},
|
|
66
|
+
handler: async (ctx, args) => {
|
|
67
|
+
const isDone = await deleteAllForUserIdAsyncHandler(ctx, {
|
|
68
|
+
userId: args.userId,
|
|
69
|
+
messagesCursor: null,
|
|
70
|
+
threadsCursor: null,
|
|
71
|
+
});
|
|
72
|
+
return isDone;
|
|
73
|
+
},
|
|
74
|
+
returns: v.boolean(),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const deleteAllArgs = {
|
|
78
|
+
userId: v.string(),
|
|
79
|
+
messagesCursor: nullable(v.string()),
|
|
80
|
+
threadsCursor: nullable(v.string()),
|
|
81
|
+
};
|
|
82
|
+
type DeleteAllArgs = ObjectType<typeof deleteAllArgs>;
|
|
83
|
+
const deleteAllReturns = {
|
|
84
|
+
messagesCursor: v.string(),
|
|
85
|
+
threadsCursor: nullable(v.string()),
|
|
86
|
+
isDone: v.boolean(),
|
|
87
|
+
};
|
|
88
|
+
type DeleteAllReturns = ObjectType<typeof deleteAllReturns>;
|
|
89
|
+
|
|
90
|
+
export const _deleteAllForUserIdAsync = internalMutation({
|
|
91
|
+
args: deleteAllArgs,
|
|
92
|
+
handler: deleteAllForUserIdAsyncHandler,
|
|
93
|
+
returns: v.boolean(),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
async function deleteAllForUserIdAsyncHandler(
|
|
97
|
+
ctx: MutationCtx,
|
|
98
|
+
args: DeleteAllArgs
|
|
99
|
+
): Promise<boolean> {
|
|
100
|
+
const result = await deletePageForUserId(ctx, args);
|
|
101
|
+
if (!result.isDone) {
|
|
102
|
+
await ctx.scheduler.runAfter(0, internal.users._deleteAllForUserIdAsync, {
|
|
103
|
+
userId: args.userId,
|
|
104
|
+
messagesCursor: result.messagesCursor,
|
|
105
|
+
threadsCursor: result.threadsCursor,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return result.isDone;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const _deletePageForUserId = internalMutation({
|
|
112
|
+
args: deleteAllArgs,
|
|
113
|
+
handler: deletePageForUserId,
|
|
114
|
+
returns: deleteAllReturns,
|
|
115
|
+
});
|
|
116
|
+
async function deletePageForUserId(
|
|
117
|
+
ctx: MutationCtx,
|
|
118
|
+
args: DeleteAllArgs
|
|
119
|
+
): Promise<DeleteAllReturns> {
|
|
120
|
+
const threads = await paginator(ctx.db, schema)
|
|
121
|
+
.query("threads")
|
|
122
|
+
.withIndex("userId", (q) => q.eq("userId", args.userId))
|
|
123
|
+
.order("desc")
|
|
124
|
+
.paginate({
|
|
125
|
+
numItems: 100,
|
|
126
|
+
cursor: args.threadsCursor ?? null,
|
|
127
|
+
});
|
|
128
|
+
await Promise.all(threads.page.map((c) => ctx.db.delete(c._id)));
|
|
129
|
+
const messages = await paginator(ctx.db, schema)
|
|
130
|
+
.query("messages")
|
|
131
|
+
.withIndex("userId_status_tool_order_stepOrder", (q) =>
|
|
132
|
+
q.eq("userId", args.userId)
|
|
133
|
+
)
|
|
134
|
+
.order("desc")
|
|
135
|
+
.paginate({
|
|
136
|
+
numItems: 100,
|
|
137
|
+
cursor: args.messagesCursor ?? null,
|
|
138
|
+
});
|
|
139
|
+
await Promise.all(messages.page.map((m) => deleteMessage(ctx, m)));
|
|
140
|
+
return {
|
|
141
|
+
messagesCursor: messages.continueCursor,
|
|
142
|
+
threadsCursor: threads.continueCursor,
|
|
143
|
+
isDone: messages.isDone,
|
|
144
|
+
};
|
|
145
|
+
}
|
package/src/validators.ts
CHANGED
|
@@ -356,7 +356,6 @@ const vCommonArgs = {
|
|
|
356
356
|
threadId: v.optional(v.string()),
|
|
357
357
|
contextOptions: v.optional(vContextOptions),
|
|
358
358
|
storageOptions: v.optional(vStorageOptions),
|
|
359
|
-
parentMessageId: v.optional(v.string()),
|
|
360
359
|
providerOptions,
|
|
361
360
|
experimental_providerMetadata,
|
|
362
361
|
...vCallSettingsFields,
|