@convex-dev/agent 0.1.16-alpha.0 → 0.1.16-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.
- package/README.md +26 -15
- package/dist/client/createTool.d.ts +2 -2
- package/dist/client/createTool.d.ts.map +1 -1
- package/dist/client/createTool.js.map +1 -1
- package/dist/client/files.js +2 -2
- package/dist/client/files.js.map +1 -1
- package/dist/client/index.d.ts +2 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +47 -29
- package/dist/client/index.js.map +1 -1
- package/dist/client/search.d.ts +2 -3
- package/dist/client/search.d.ts.map +1 -1
- package/dist/client/search.js +2 -1
- package/dist/client/search.js.map +1 -1
- package/dist/client/types.d.ts +7 -9
- package/dist/client/types.d.ts.map +1 -1
- package/dist/component/messages.d.ts +1 -1
- package/dist/component/messages.d.ts.map +1 -1
- package/dist/component/messages.js +5 -5
- package/dist/component/messages.js.map +1 -1
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js.map +1 -1
- package/dist/mapping.d.ts.map +1 -1
- package/dist/mapping.js +3 -1
- package/dist/mapping.js.map +1 -1
- package/dist/react/deltas.d.ts.map +1 -1
- package/dist/react/deltas.js +6 -5
- package/dist/react/deltas.js.map +1 -1
- package/dist/validators.js +1 -2
- package/dist/validators.js.map +1 -1
- package/package.json +1 -1
- package/src/client/createTool.ts +9 -6
- package/src/client/files.ts +8 -8
- package/src/client/index.test.ts +14 -12
- package/src/client/index.ts +127 -99
- package/src/client/listMessages.ts +1 -1
- package/src/client/search.ts +14 -14
- package/src/client/streaming.ts +7 -7
- package/src/client/types.ts +29 -23
- package/src/component/apiKeys.ts +1 -1
- package/src/component/files.test.ts +1 -1
- package/src/component/files.ts +6 -6
- package/src/component/messages.test.ts +6 -6
- package/src/component/messages.ts +44 -43
- package/src/component/schema.ts +1 -2
- package/src/component/streams.ts +27 -27
- package/src/component/threads.test.ts +4 -4
- package/src/component/threads.ts +4 -4
- package/src/component/users.test.ts +2 -2
- package/src/component/users.ts +4 -4
- package/src/component/vector/index.ts +11 -11
- package/src/component/vector/tables.ts +6 -6
- package/src/mapping.test.ts +4 -4
- package/src/mapping.ts +18 -17
- package/src/react/deltas.test.ts +16 -16
- package/src/react/deltas.ts +19 -18
- package/src/react/index.ts +11 -11
- package/src/react/optimisticallySendMessage.ts +2 -2
- package/src/react/toUIMessages.test.ts +10 -10
- package/src/react/toUIMessages.ts +6 -6
- package/src/react/useSmoothText.ts +6 -6
- package/src/validators.ts +24 -24
package/src/client/createTool.ts
CHANGED
|
@@ -2,9 +2,12 @@ import type { Schema, Tool, ToolExecutionOptions, ToolSet } from "ai";
|
|
|
2
2
|
import { tool } from "ai";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import type { Agent } from "./index.js";
|
|
5
|
-
import type {
|
|
5
|
+
import type { GenericActionCtx, GenericDataModel } from "convex/server";
|
|
6
6
|
|
|
7
|
-
export type ToolCtx<
|
|
7
|
+
export type ToolCtx<
|
|
8
|
+
DataModel extends GenericDataModel = GenericDataModel,
|
|
9
|
+
TOOLS extends ToolSet = ToolSet,
|
|
10
|
+
> = GenericActionCtx<DataModel> & {
|
|
8
11
|
agent: Agent<TOOLS>;
|
|
9
12
|
userId?: string;
|
|
10
13
|
threadId?: string;
|
|
@@ -41,13 +44,13 @@ export function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
|
|
|
41
44
|
handler: (
|
|
42
45
|
ctx: ToolCtx,
|
|
43
46
|
args: inferParameters<PARAMETERS>,
|
|
44
|
-
options: ToolExecutionOptions
|
|
47
|
+
options: ToolExecutionOptions,
|
|
45
48
|
) => PromiseLike<RESULT>;
|
|
46
49
|
ctx?: ToolCtx;
|
|
47
50
|
}): Tool<PARAMETERS, RESULT> & {
|
|
48
51
|
execute: (
|
|
49
52
|
args: inferParameters<PARAMETERS>,
|
|
50
|
-
options: ToolExecutionOptions
|
|
53
|
+
options: ToolExecutionOptions,
|
|
51
54
|
) => PromiseLike<RESULT>;
|
|
52
55
|
} {
|
|
53
56
|
const args = {
|
|
@@ -57,13 +60,13 @@ export function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
|
|
|
57
60
|
parameters: t.args,
|
|
58
61
|
async execute(
|
|
59
62
|
args: inferParameters<PARAMETERS>,
|
|
60
|
-
options: ToolExecutionOptions
|
|
63
|
+
options: ToolExecutionOptions,
|
|
61
64
|
) {
|
|
62
65
|
if (!this.ctx) {
|
|
63
66
|
throw new Error(
|
|
64
67
|
"To use a Convex tool, you must either provide the ctx" +
|
|
65
68
|
" at definition time (dynamically in an action), or use the Agent to" +
|
|
66
|
-
" call it (which injects the ctx, userId and threadId)"
|
|
69
|
+
" call it (which injects the ctx, userId and threadId)",
|
|
67
70
|
);
|
|
68
71
|
}
|
|
69
72
|
return t.handler(this.ctx, args, options);
|
package/src/client/files.ts
CHANGED
|
@@ -33,26 +33,26 @@ export async function storeFile(
|
|
|
33
33
|
component: AgentComponent,
|
|
34
34
|
blob: Blob,
|
|
35
35
|
filename?: string,
|
|
36
|
-
sha256?: string
|
|
36
|
+
sha256?: string,
|
|
37
37
|
): Promise<{
|
|
38
38
|
file: File;
|
|
39
39
|
filePart: FilePart;
|
|
40
40
|
imagePart: ImagePart | undefined;
|
|
41
41
|
}> {
|
|
42
|
-
if (!("runAction" in ctx)) {
|
|
42
|
+
if (!("runAction" in ctx) || !("storage" in ctx)) {
|
|
43
43
|
throw new Error(
|
|
44
|
-
"You're trying to save a file that's too large in a mutation. " +
|
|
44
|
+
"You're trying to save a file that's too large in a mutation / workflow. " +
|
|
45
45
|
"You can store the file in file storage from an action first, then pass a URL instead. " +
|
|
46
46
|
"To have the agent component track the file, you can use `saveFile` from an action then use the fileId with getFile in the mutation. " +
|
|
47
|
-
"Read more in the docs."
|
|
47
|
+
"Read more in the docs.",
|
|
48
48
|
);
|
|
49
49
|
}
|
|
50
50
|
const hash =
|
|
51
51
|
sha256 ||
|
|
52
52
|
Array.from(
|
|
53
53
|
new Uint8Array(
|
|
54
|
-
await crypto.subtle.digest("SHA-256", await blob.arrayBuffer())
|
|
55
|
-
)
|
|
54
|
+
await crypto.subtle.digest("SHA-256", await blob.arrayBuffer()),
|
|
55
|
+
),
|
|
56
56
|
)
|
|
57
57
|
.map((b) => b.toString(16).padStart(2, "0"))
|
|
58
58
|
.join("");
|
|
@@ -124,7 +124,7 @@ export async function storeFile(
|
|
|
124
124
|
export async function getFile(
|
|
125
125
|
ctx: ActionCtx | QueryCtx,
|
|
126
126
|
component: AgentComponent,
|
|
127
|
-
fileId: string
|
|
127
|
+
fileId: string,
|
|
128
128
|
) {
|
|
129
129
|
const file = await ctx.runQuery(component.files.get, { fileId });
|
|
130
130
|
if (!file) {
|
|
@@ -149,7 +149,7 @@ export async function getFile(
|
|
|
149
149
|
function getParts(
|
|
150
150
|
url: string,
|
|
151
151
|
mimeType: string,
|
|
152
|
-
filename: string | undefined
|
|
152
|
+
filename: string | undefined,
|
|
153
153
|
): {
|
|
154
154
|
filePart: FilePart;
|
|
155
155
|
imagePart: ImagePart | undefined;
|
package/src/client/index.test.ts
CHANGED
|
@@ -113,7 +113,7 @@ export const generateTextWithThread = action({
|
|
|
113
113
|
{
|
|
114
114
|
contextOptions: args.contextOptions,
|
|
115
115
|
storageOptions: args.storageOptions,
|
|
116
|
-
}
|
|
116
|
+
},
|
|
117
117
|
);
|
|
118
118
|
return { text: result.text };
|
|
119
119
|
},
|
|
@@ -313,7 +313,7 @@ describe("Agent thread management", () => {
|
|
|
313
313
|
test("createThread returns threadId (mutation context)", async () => {
|
|
314
314
|
const t = initConvexTest(schema);
|
|
315
315
|
const threadId = await t.run(async (ctx) =>
|
|
316
|
-
agent.createThread(ctx, { userId: "2" }).then(({ threadId }) => threadId)
|
|
316
|
+
agent.createThread(ctx, { userId: "2" }).then(({ threadId }) => threadId),
|
|
317
317
|
);
|
|
318
318
|
expect(threadId).toBeTypeOf("string");
|
|
319
319
|
});
|
|
@@ -321,7 +321,7 @@ describe("Agent thread management", () => {
|
|
|
321
321
|
test("continueThread returns thread object", async () => {
|
|
322
322
|
const t = initConvexTest(schema);
|
|
323
323
|
const threadId = await t.run(async (ctx) =>
|
|
324
|
-
agent.createThread(ctx, { userId: "3" }).then(({ threadId }) => threadId)
|
|
324
|
+
agent.createThread(ctx, { userId: "3" }).then(({ threadId }) => threadId),
|
|
325
325
|
);
|
|
326
326
|
const result = await t.action(testApi.continueThreadAction, {
|
|
327
327
|
threadId,
|
|
@@ -335,14 +335,14 @@ describe("Agent message operations", () => {
|
|
|
335
335
|
test("saveMessage and saveMessages store messages", async () => {
|
|
336
336
|
const t = initConvexTest(schema);
|
|
337
337
|
const threadId = await t.run(async (ctx) =>
|
|
338
|
-
agent.createThread(ctx, { userId: "4" }).then(({ threadId }) => threadId)
|
|
338
|
+
agent.createThread(ctx, { userId: "4" }).then(({ threadId }) => threadId),
|
|
339
339
|
);
|
|
340
340
|
const { messageId } = await t.run(async (ctx) =>
|
|
341
341
|
agent.saveMessage(ctx, {
|
|
342
342
|
threadId,
|
|
343
343
|
userId: "4",
|
|
344
344
|
message: { role: "user", content: "Hello" },
|
|
345
|
-
})
|
|
345
|
+
}),
|
|
346
346
|
);
|
|
347
347
|
expect(messageId).toBeTypeOf("string");
|
|
348
348
|
|
|
@@ -354,7 +354,7 @@ describe("Agent message operations", () => {
|
|
|
354
354
|
{ role: "user", content: "Hi" },
|
|
355
355
|
{ role: "assistant", content: "Hello!" },
|
|
356
356
|
],
|
|
357
|
-
})
|
|
357
|
+
}),
|
|
358
358
|
);
|
|
359
359
|
expect(messages.length).toBe(2);
|
|
360
360
|
expect(lastMessageId).toBe(messages[1]._id);
|
|
@@ -365,7 +365,7 @@ describe("Agent text/object generation", () => {
|
|
|
365
365
|
test("generateText with custom context and storage options", async () => {
|
|
366
366
|
const t = initConvexTest(schema);
|
|
367
367
|
const threadId = await t.run(async (ctx) =>
|
|
368
|
-
agent.createThread(ctx, { userId: "5" }).then(({ threadId }) => threadId)
|
|
368
|
+
agent.createThread(ctx, { userId: "5" }).then(({ threadId }) => threadId),
|
|
369
369
|
);
|
|
370
370
|
const result = await t.action(testApi.generateTextWithThread, {
|
|
371
371
|
threadId,
|
|
@@ -380,7 +380,7 @@ describe("Agent text/object generation", () => {
|
|
|
380
380
|
test("generateObject returns object", async () => {
|
|
381
381
|
const t = initConvexTest(schema);
|
|
382
382
|
const threadId = await t.run(async (ctx) =>
|
|
383
|
-
agent.createThread(ctx, { userId: "6" }).then(({ threadId }) => threadId)
|
|
383
|
+
agent.createThread(ctx, { userId: "6" }).then(({ threadId }) => threadId),
|
|
384
384
|
);
|
|
385
385
|
const result = await t.action(testApi.generateObjectWithThread, {
|
|
386
386
|
threadId,
|
|
@@ -402,7 +402,7 @@ describe("Agent-generated mutations/actions/queries", () => {
|
|
|
402
402
|
test("asTextAction and asObjectAction work via t.action", async () => {
|
|
403
403
|
const t = initConvexTest(schema);
|
|
404
404
|
const threadId = await t.run(async (ctx) =>
|
|
405
|
-
agent.createThread(ctx, { userId: "8" }).then(({ threadId }) => threadId)
|
|
405
|
+
agent.createThread(ctx, { userId: "8" }).then(({ threadId }) => threadId),
|
|
406
406
|
);
|
|
407
407
|
const textResult = await t.action(testApi.generateTextAction, {
|
|
408
408
|
userId: "8",
|
|
@@ -422,7 +422,7 @@ describe("Agent-generated mutations/actions/queries", () => {
|
|
|
422
422
|
test("asSaveMessagesMutation works via t.mutation", async () => {
|
|
423
423
|
const t = initConvexTest(schema);
|
|
424
424
|
const threadId = await t.run(async (ctx) =>
|
|
425
|
-
agent.createThread(ctx, { userId: "9" }).then(({ threadId }) => threadId)
|
|
425
|
+
agent.createThread(ctx, { userId: "9" }).then(({ threadId }) => threadId),
|
|
426
426
|
);
|
|
427
427
|
const result = await t.mutation(testApi.saveMessageMutation, {
|
|
428
428
|
threadId,
|
|
@@ -442,14 +442,16 @@ describe("Agent context and search options", () => {
|
|
|
442
442
|
test("fetchContextMessages returns context messages", async () => {
|
|
443
443
|
const t = initConvexTest(schema);
|
|
444
444
|
const threadId = await t.run(async (ctx) =>
|
|
445
|
-
agent
|
|
445
|
+
agent
|
|
446
|
+
.createThread(ctx, { userId: "10" })
|
|
447
|
+
.then(({ threadId }) => threadId),
|
|
446
448
|
);
|
|
447
449
|
await t.run(async (ctx) =>
|
|
448
450
|
agent.saveMessage(ctx, {
|
|
449
451
|
threadId,
|
|
450
452
|
userId: "10",
|
|
451
453
|
message: { role: "user", content: "Context test" },
|
|
452
|
-
})
|
|
454
|
+
}),
|
|
453
455
|
);
|
|
454
456
|
const context = await t.action(testApi.fetchContextAction, {
|
|
455
457
|
userId: "10",
|