@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,257 @@
|
|
|
1
|
+
import {
|
|
2
|
+
paginationOptsValidator,
|
|
3
|
+
queryGeneric,
|
|
4
|
+
mutationGeneric,
|
|
5
|
+
actionGeneric,
|
|
6
|
+
GenericDataModel,
|
|
7
|
+
GenericQueryCtx,
|
|
8
|
+
ApiFromModules,
|
|
9
|
+
} from "convex/server";
|
|
10
|
+
import {
|
|
11
|
+
vMessageDoc,
|
|
12
|
+
vThreadDoc,
|
|
13
|
+
paginationResultValidator,
|
|
14
|
+
vMessage,
|
|
15
|
+
vContextOptions,
|
|
16
|
+
vStorageOptions,
|
|
17
|
+
type AgentComponent,
|
|
18
|
+
type Agent,
|
|
19
|
+
} from "./index";
|
|
20
|
+
import type { ToolSet } from "ai";
|
|
21
|
+
import { v } from "convex/values";
|
|
22
|
+
|
|
23
|
+
export type PlaygroundAPI = ApiFromModules<{
|
|
24
|
+
playground: ReturnType<typeof definePlaygroundAPI>;
|
|
25
|
+
}>["playground"];
|
|
26
|
+
|
|
27
|
+
// Playground API definition
|
|
28
|
+
export function definePlaygroundAPI(
|
|
29
|
+
component: AgentComponent,
|
|
30
|
+
{
|
|
31
|
+
agents,
|
|
32
|
+
userNameLookup,
|
|
33
|
+
}: {
|
|
34
|
+
agents: Agent<ToolSet>[];
|
|
35
|
+
userNameLookup?: <DataModel extends GenericDataModel>(
|
|
36
|
+
ctx: GenericQueryCtx<DataModel>,
|
|
37
|
+
userId: string
|
|
38
|
+
) => string | Promise<string>;
|
|
39
|
+
}
|
|
40
|
+
) {
|
|
41
|
+
// Map agent name to instance
|
|
42
|
+
const agentMap: Record<string, Agent<ToolSet>> = Object.fromEntries(
|
|
43
|
+
agents.map((agent) => [agent.options.name, agent])
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
async function validateApiKey(ctx: RunQueryCtx, apiKey: string) {
|
|
47
|
+
await ctx.runQuery(component.apiKeys.validate, { apiKey });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isApiKeyValid = queryGeneric({
|
|
51
|
+
args: {
|
|
52
|
+
apiKey: v.string(),
|
|
53
|
+
},
|
|
54
|
+
handler: async (ctx, args) => {
|
|
55
|
+
try {
|
|
56
|
+
await validateApiKey(ctx, args.apiKey);
|
|
57
|
+
return true;
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
59
|
+
} catch (e) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
returns: v.boolean(),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// List all agents
|
|
67
|
+
const listAgents = queryGeneric({
|
|
68
|
+
args: {
|
|
69
|
+
apiKey: v.string(),
|
|
70
|
+
},
|
|
71
|
+
handler: async (ctx, args) => {
|
|
72
|
+
await validateApiKey(ctx, args.apiKey);
|
|
73
|
+
return Object.keys(agentMap);
|
|
74
|
+
},
|
|
75
|
+
returns: v.array(v.string()),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const listUsers = queryGeneric({
|
|
79
|
+
args: {
|
|
80
|
+
apiKey: v.string(),
|
|
81
|
+
paginationOpts: paginationOptsValidator,
|
|
82
|
+
},
|
|
83
|
+
handler: async (ctx, args) => {
|
|
84
|
+
await validateApiKey(ctx, args.apiKey);
|
|
85
|
+
const users = await ctx.runQuery(component.users.listUsersWithThreads, {
|
|
86
|
+
paginationOpts: args.paginationOpts,
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
...users,
|
|
90
|
+
page: await Promise.all(
|
|
91
|
+
users.page.map(async (userId) => ({
|
|
92
|
+
_id: userId,
|
|
93
|
+
name: userNameLookup ? await userNameLookup(ctx, userId) : userId,
|
|
94
|
+
}))
|
|
95
|
+
),
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
returns: paginationResultValidator(
|
|
99
|
+
v.object({
|
|
100
|
+
_id: v.string(),
|
|
101
|
+
name: v.string(),
|
|
102
|
+
})
|
|
103
|
+
),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// List threads for a user (query)
|
|
107
|
+
const listThreads = queryGeneric({
|
|
108
|
+
args: {
|
|
109
|
+
apiKey: v.string(),
|
|
110
|
+
userId: v.string(),
|
|
111
|
+
paginationOpts: paginationOptsValidator,
|
|
112
|
+
},
|
|
113
|
+
handler: async (ctx, args) => {
|
|
114
|
+
await validateApiKey(ctx, args.apiKey);
|
|
115
|
+
const results = await ctx.runQuery(
|
|
116
|
+
component.threads.listThreadsByUserId,
|
|
117
|
+
{
|
|
118
|
+
userId: args.userId,
|
|
119
|
+
paginationOpts: args.paginationOpts,
|
|
120
|
+
order: "desc",
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
return {
|
|
124
|
+
...results,
|
|
125
|
+
page: await Promise.all(
|
|
126
|
+
results.page.map(async (thread) => {
|
|
127
|
+
const {
|
|
128
|
+
page: [last],
|
|
129
|
+
} = await ctx.runQuery(component.messages.listMessagesByThreadId, {
|
|
130
|
+
threadId: thread._id,
|
|
131
|
+
order: "desc",
|
|
132
|
+
paginationOpts: {
|
|
133
|
+
numItems: 1,
|
|
134
|
+
cursor: null,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
...thread,
|
|
139
|
+
latestMessage: last?.text,
|
|
140
|
+
lastMessageAt: last?._creationTime,
|
|
141
|
+
};
|
|
142
|
+
})
|
|
143
|
+
),
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
returns: paginationResultValidator(
|
|
147
|
+
v.object({
|
|
148
|
+
...vThreadDoc.fields,
|
|
149
|
+
latestMessage: v.optional(v.string()),
|
|
150
|
+
lastMessageAt: v.optional(v.number()),
|
|
151
|
+
})
|
|
152
|
+
),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// List messages for a thread (query)
|
|
156
|
+
const listMessages = queryGeneric({
|
|
157
|
+
args: {
|
|
158
|
+
apiKey: v.string(),
|
|
159
|
+
threadId: v.string(),
|
|
160
|
+
paginationOpts: paginationOptsValidator,
|
|
161
|
+
},
|
|
162
|
+
handler: async (ctx, args) => {
|
|
163
|
+
await validateApiKey(ctx, args.apiKey);
|
|
164
|
+
return ctx.runQuery(component.messages.listMessagesByThreadId, {
|
|
165
|
+
threadId: args.threadId,
|
|
166
|
+
paginationOpts: args.paginationOpts,
|
|
167
|
+
order: "desc",
|
|
168
|
+
statuses: ["success", "failed", "pending"],
|
|
169
|
+
});
|
|
170
|
+
},
|
|
171
|
+
returns: paginationResultValidator(vMessageDoc),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Create a thread (mutation)
|
|
175
|
+
const createThread = mutationGeneric({
|
|
176
|
+
args: {
|
|
177
|
+
apiKey: v.string(),
|
|
178
|
+
agentName: v.string(),
|
|
179
|
+
userId: v.string(),
|
|
180
|
+
title: v.optional(v.string()),
|
|
181
|
+
summary: v.optional(v.string()),
|
|
182
|
+
},
|
|
183
|
+
handler: async (ctx, args) => {
|
|
184
|
+
await validateApiKey(ctx, args.apiKey);
|
|
185
|
+
const agent = agentMap[args.agentName];
|
|
186
|
+
if (!agent) throw new Error(`Unknown agent: ${args.agentName}`);
|
|
187
|
+
return agent.createThread(ctx, {
|
|
188
|
+
userId: args.userId,
|
|
189
|
+
title: args.title,
|
|
190
|
+
summary: args.summary,
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
returns: v.object({ threadId: v.string() }),
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Send a message (action)
|
|
197
|
+
const generateText = actionGeneric({
|
|
198
|
+
args: {
|
|
199
|
+
apiKey: v.string(),
|
|
200
|
+
agentName: v.string(),
|
|
201
|
+
userId: v.string(),
|
|
202
|
+
threadId: v.string(),
|
|
203
|
+
// Args passed through to generateText
|
|
204
|
+
prompt: v.optional(v.string()),
|
|
205
|
+
messages: v.optional(v.array(vMessage)),
|
|
206
|
+
contextOptions: v.optional(vContextOptions),
|
|
207
|
+
storageOptions: v.optional(vStorageOptions),
|
|
208
|
+
},
|
|
209
|
+
handler: async (ctx, args) => {
|
|
210
|
+
const { apiKey, agentName, userId, threadId, ...rest } = args;
|
|
211
|
+
await validateApiKey(ctx, apiKey);
|
|
212
|
+
const agent = agentMap[agentName];
|
|
213
|
+
if (!agent) throw new Error(`Unknown agent: ${agentName}`);
|
|
214
|
+
const { thread } = await agent.continueThread(ctx, { threadId, userId });
|
|
215
|
+
const result = await thread.generateText(rest);
|
|
216
|
+
return result;
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Fetch prompt context (action)
|
|
221
|
+
const fetchPromptContext = actionGeneric({
|
|
222
|
+
args: {
|
|
223
|
+
apiKey: v.string(),
|
|
224
|
+
agentName: v.string(),
|
|
225
|
+
userId: v.optional(v.string()),
|
|
226
|
+
threadId: v.optional(v.string()),
|
|
227
|
+
messages: v.array(vMessage),
|
|
228
|
+
contextOptions: vContextOptions,
|
|
229
|
+
beforeMessageId: v.optional(v.string()),
|
|
230
|
+
},
|
|
231
|
+
handler: async (ctx, args) => {
|
|
232
|
+
await validateApiKey(ctx, args.apiKey);
|
|
233
|
+
const agent = agentMap[args.agentName];
|
|
234
|
+
if (!agent) throw new Error(`Unknown agent: ${args.agentName}`);
|
|
235
|
+
return agent.fetchContextMessages(ctx, {
|
|
236
|
+
userId: args.userId,
|
|
237
|
+
threadId: args.threadId,
|
|
238
|
+
messages: args.messages,
|
|
239
|
+
contextOptions: args.contextOptions,
|
|
240
|
+
beforeMessageId: args.beforeMessageId,
|
|
241
|
+
});
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
isApiKeyValid,
|
|
247
|
+
listUsers,
|
|
248
|
+
listThreads,
|
|
249
|
+
listMessages,
|
|
250
|
+
listAgents,
|
|
251
|
+
createThread,
|
|
252
|
+
generateText,
|
|
253
|
+
fetchPromptContext,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
type RunQueryCtx = { runQuery: GenericQueryCtx<GenericDataModel>["runQuery"] };
|