@convex-dev/agent 0.1.16-alpha.1 → 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/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 +13 -13
- 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/index.ts
CHANGED
|
@@ -13,7 +13,13 @@ import type {
|
|
|
13
13
|
ToolSet,
|
|
14
14
|
UserContent,
|
|
15
15
|
} from "ai";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
embedMany,
|
|
18
|
+
generateObject,
|
|
19
|
+
generateText,
|
|
20
|
+
streamObject,
|
|
21
|
+
streamText,
|
|
22
|
+
} from "ai";
|
|
17
23
|
import { assert } from "convex-helpers";
|
|
18
24
|
import {
|
|
19
25
|
internalActionGeneric,
|
|
@@ -76,6 +82,7 @@ import type {
|
|
|
76
82
|
TextArgs,
|
|
77
83
|
Thread,
|
|
78
84
|
UsageHandler,
|
|
85
|
+
UserActionCtx,
|
|
79
86
|
} from "./types.js";
|
|
80
87
|
|
|
81
88
|
export { vMessageDoc, vThreadDoc } from "../component/schema.js";
|
|
@@ -192,7 +199,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
192
199
|
* log the raw request body or response headers to a table, or logs.
|
|
193
200
|
*/
|
|
194
201
|
rawRequestResponseHandler?: RawRequestResponseHandler;
|
|
195
|
-
}
|
|
202
|
+
},
|
|
196
203
|
) {}
|
|
197
204
|
|
|
198
205
|
/**
|
|
@@ -231,7 +238,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
231
238
|
* Overrides any tools passed in the agent constructor.
|
|
232
239
|
*/
|
|
233
240
|
tools?: ThreadTools;
|
|
234
|
-
}
|
|
241
|
+
},
|
|
235
242
|
): Promise<{
|
|
236
243
|
threadId: string;
|
|
237
244
|
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
@@ -271,7 +278,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
271
278
|
* Overrides any tools passed in the agent constructor.
|
|
272
279
|
*/
|
|
273
280
|
tools?: ThreadTools;
|
|
274
|
-
}
|
|
281
|
+
},
|
|
275
282
|
): Promise<{
|
|
276
283
|
threadId: string;
|
|
277
284
|
}>;
|
|
@@ -283,13 +290,13 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
283
290
|
summary?: string;
|
|
284
291
|
usageHandler?: UsageHandler;
|
|
285
292
|
tools?: ThreadTools;
|
|
286
|
-
}
|
|
293
|
+
},
|
|
287
294
|
): Promise<{
|
|
288
295
|
threadId: string;
|
|
289
296
|
thread?: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
290
297
|
}> {
|
|
291
298
|
const threadId = await createThread(ctx, this.component, args);
|
|
292
|
-
if (!("runAction" in ctx)) {
|
|
299
|
+
if (!("runAction" in ctx) || "workflowId" in ctx) {
|
|
293
300
|
return { threadId };
|
|
294
301
|
}
|
|
295
302
|
const { thread } = await this.continueThread(ctx, {
|
|
@@ -334,7 +341,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
334
341
|
* Overrides any tools passed in the agent constructor.
|
|
335
342
|
*/
|
|
336
343
|
tools?: ThreadTools;
|
|
337
|
-
}
|
|
344
|
+
},
|
|
338
345
|
): Promise<{
|
|
339
346
|
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
340
347
|
}> {
|
|
@@ -372,7 +379,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
372
379
|
userId?: string | undefined;
|
|
373
380
|
query: string;
|
|
374
381
|
limit?: number;
|
|
375
|
-
}
|
|
382
|
+
},
|
|
376
383
|
): Promise<ThreadDoc[]> {
|
|
377
384
|
return ctx.runQuery(this.component.threads.searchThreadTitles, {
|
|
378
385
|
userId,
|
|
@@ -416,7 +423,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
416
423
|
tools?: ToolSet;
|
|
417
424
|
},
|
|
418
425
|
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL>,
|
|
419
|
-
options?: Options
|
|
426
|
+
options?: Options,
|
|
420
427
|
): Promise<
|
|
421
428
|
GenerateTextResult<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT> &
|
|
422
429
|
GenerationOutputMetadata
|
|
@@ -427,13 +434,19 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
427
434
|
...options,
|
|
428
435
|
});
|
|
429
436
|
const { args: aiArgs, messageId, order, userId } = context;
|
|
430
|
-
const toolCtx = {
|
|
437
|
+
const toolCtx = {
|
|
438
|
+
...(ctx as UserActionCtx),
|
|
439
|
+
userId,
|
|
440
|
+
threadId,
|
|
441
|
+
messageId,
|
|
442
|
+
agent: this,
|
|
443
|
+
};
|
|
431
444
|
const tools = wrapTools(
|
|
432
445
|
toolCtx,
|
|
433
|
-
args.tools ?? threadTools ?? this.options.tools
|
|
446
|
+
args.tools ?? threadTools ?? this.options.tools,
|
|
434
447
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
435
448
|
const saveOutputMessages = this._shouldSaveOutputMessages(
|
|
436
|
-
options?.storageOptions
|
|
449
|
+
options?.storageOptions,
|
|
437
450
|
);
|
|
438
451
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
439
452
|
try {
|
|
@@ -540,7 +553,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
540
553
|
* iterating over the text, streaming it over HTTP, etc.
|
|
541
554
|
*/
|
|
542
555
|
saveStreamDeltas?: boolean | StreamingOptions;
|
|
543
|
-
}
|
|
556
|
+
},
|
|
544
557
|
): Promise<
|
|
545
558
|
StreamTextResult<
|
|
546
559
|
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
@@ -554,13 +567,19 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
554
567
|
...options,
|
|
555
568
|
});
|
|
556
569
|
const { args: aiArgs, messageId, order, stepOrder, userId } = context;
|
|
557
|
-
const toolCtx = {
|
|
570
|
+
const toolCtx = {
|
|
571
|
+
...(ctx as UserActionCtx),
|
|
572
|
+
userId,
|
|
573
|
+
threadId,
|
|
574
|
+
messageId,
|
|
575
|
+
agent: this,
|
|
576
|
+
};
|
|
558
577
|
const tools = wrapTools(
|
|
559
578
|
toolCtx,
|
|
560
|
-
args.tools ?? threadTools ?? this.options.tools
|
|
579
|
+
args.tools ?? threadTools ?? this.options.tools,
|
|
561
580
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
562
581
|
const saveOutputMessages = this._shouldSaveOutputMessages(
|
|
563
|
-
options?.storageOptions
|
|
582
|
+
options?.storageOptions,
|
|
564
583
|
);
|
|
565
584
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
566
585
|
const streamer =
|
|
@@ -586,7 +605,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
586
605
|
abortSignal: streamer?.abortController.signal ?? aiArgs.abortSignal,
|
|
587
606
|
experimental_transform: mergeTransforms(
|
|
588
607
|
options?.saveStreamDeltas,
|
|
589
|
-
args.experimental_transform
|
|
608
|
+
args.experimental_transform,
|
|
590
609
|
),
|
|
591
610
|
onChunk: async (event) => {
|
|
592
611
|
await streamer?.addParts([event.chunk]);
|
|
@@ -671,7 +690,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
671
690
|
* The {@link ContextOptions} and {@link StorageOptions}
|
|
672
691
|
* options to use for fetching contextual messages and saving input/output messages.
|
|
673
692
|
*/
|
|
674
|
-
options?: Options
|
|
693
|
+
options?: Options,
|
|
675
694
|
): Promise<GenerateObjectResult<T> & GenerationOutputMetadata> {
|
|
676
695
|
const context = await this._saveMessagesAndFetchContext(ctx, args, {
|
|
677
696
|
userId: argsUserId,
|
|
@@ -681,12 +700,12 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
681
700
|
const { args: aiArgs, messageId, order, userId } = context;
|
|
682
701
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
683
702
|
const saveOutputMessages = this._shouldSaveOutputMessages(
|
|
684
|
-
options?.storageOptions
|
|
703
|
+
options?.storageOptions,
|
|
685
704
|
);
|
|
686
705
|
try {
|
|
687
706
|
const result = (await generateObject(
|
|
688
707
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
689
|
-
aiArgs as any
|
|
708
|
+
aiArgs as any,
|
|
690
709
|
)) as GenerateObjectResult<T> & GenerationOutputMetadata;
|
|
691
710
|
|
|
692
711
|
if (threadId && messageId && saveOutputMessages) {
|
|
@@ -755,7 +774,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
755
774
|
* The {@link ContextOptions} and {@link StorageOptions}
|
|
756
775
|
* options to use for fetching contextual messages and saving input/output messages.
|
|
757
776
|
*/
|
|
758
|
-
options?: Options
|
|
777
|
+
options?: Options,
|
|
759
778
|
): Promise<
|
|
760
779
|
StreamObjectResult<DeepPartial<T>, T, never> & GenerationOutputMetadata
|
|
761
780
|
> {
|
|
@@ -768,7 +787,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
768
787
|
const { args: aiArgs, messageId, order, userId } = context;
|
|
769
788
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
770
789
|
const saveOutputMessages = this._shouldSaveOutputMessages(
|
|
771
|
-
options?.storageOptions
|
|
790
|
+
options?.storageOptions,
|
|
772
791
|
);
|
|
773
792
|
const stream = streamObject<T>({
|
|
774
793
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -847,7 +866,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
847
866
|
* action later that calls `agent.generateAndSaveEmbeddings`.
|
|
848
867
|
*/
|
|
849
868
|
skipEmbeddings?: boolean;
|
|
850
|
-
}
|
|
869
|
+
},
|
|
851
870
|
) {
|
|
852
871
|
const { lastMessageId, messages } = await this.saveMessages(ctx, {
|
|
853
872
|
threadId: args.threadId,
|
|
@@ -886,7 +905,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
886
905
|
* action later that calls `agent.generateAndSaveEmbeddings`.
|
|
887
906
|
*/
|
|
888
907
|
skipEmbeddings?: boolean;
|
|
889
|
-
}
|
|
908
|
+
},
|
|
890
909
|
): Promise<{
|
|
891
910
|
lastMessageId: string;
|
|
892
911
|
messages: MessageDoc[];
|
|
@@ -900,25 +919,31 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
900
919
|
const { skipEmbeddings, ...rest } = args;
|
|
901
920
|
if (args.embeddings) {
|
|
902
921
|
embeddings = args.embeddings;
|
|
903
|
-
} else if (skipEmbeddings
|
|
904
|
-
|
|
905
|
-
if (!skipEmbeddings && this.options.textEmbedding) {
|
|
922
|
+
} else if (!skipEmbeddings && this.options.textEmbedding) {
|
|
923
|
+
if (!("runAction" in ctx)) {
|
|
906
924
|
console.warn(
|
|
907
925
|
"You're trying to save messages and generate embeddings, but you're in a mutation. " +
|
|
908
926
|
"Pass `skipEmbeddings: true` to skip generating embeddings in the mutation and skip this warning. " +
|
|
909
927
|
"They will be generated lazily when you generate or stream text / objects. " +
|
|
910
|
-
"You can explicitly generate them asynchronously by using the scheduler to run an action later that calls `agent.generateAndSaveEmbeddings`."
|
|
928
|
+
"You can explicitly generate them asynchronously by using the scheduler to run an action later that calls `agent.generateAndSaveEmbeddings`.",
|
|
929
|
+
);
|
|
930
|
+
} else if ("workflowId" in ctx) {
|
|
931
|
+
console.warn(
|
|
932
|
+
"You're trying to save messages and generate embeddings, but you're in a workflow. " +
|
|
933
|
+
"Pass `skipEmbeddings: true` to skip generating embeddings in the workflow and skip this warning. " +
|
|
934
|
+
"They will be generated lazily when you generate or stream text / objects. " +
|
|
935
|
+
"You can explicitly generate them asynchronously by using the scheduler to run an action later that calls `agent.generateAndSaveEmbeddings`.",
|
|
936
|
+
);
|
|
937
|
+
} else {
|
|
938
|
+
embeddings = await this.generateEmbeddings(
|
|
939
|
+
ctx,
|
|
940
|
+
{
|
|
941
|
+
userId: args.userId,
|
|
942
|
+
threadId: args.threadId,
|
|
943
|
+
},
|
|
944
|
+
args.messages,
|
|
911
945
|
);
|
|
912
946
|
}
|
|
913
|
-
} else {
|
|
914
|
-
embeddings = await this.generateEmbeddings(
|
|
915
|
-
ctx,
|
|
916
|
-
{
|
|
917
|
-
userId: args.userId,
|
|
918
|
-
threadId: args.threadId,
|
|
919
|
-
},
|
|
920
|
-
args.messages
|
|
921
|
-
);
|
|
922
947
|
}
|
|
923
948
|
return saveMessages(ctx, this.component, {
|
|
924
949
|
...rest,
|
|
@@ -944,7 +969,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
944
969
|
paginationOpts: PaginationOptions;
|
|
945
970
|
excludeToolMessages?: boolean;
|
|
946
971
|
statuses?: MessageStatus[];
|
|
947
|
-
}
|
|
972
|
+
},
|
|
948
973
|
): Promise<PaginationResult<MessageDoc>> {
|
|
949
974
|
return listMessages(ctx, this.component, args);
|
|
950
975
|
}
|
|
@@ -964,7 +989,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
964
989
|
streamArgs: StreamArgs | undefined;
|
|
965
990
|
// By default, only streaming messages are included.
|
|
966
991
|
includeStatuses?: ("streaming" | "finished" | "aborted")[];
|
|
967
|
-
}
|
|
992
|
+
},
|
|
968
993
|
): Promise<SyncStreamsReturnValue | undefined> {
|
|
969
994
|
return syncStreams(ctx, this.component, args);
|
|
970
995
|
}
|
|
@@ -990,7 +1015,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
990
1015
|
*/
|
|
991
1016
|
upToAndIncludingMessageId?: string;
|
|
992
1017
|
contextOptions: ContextOptions | undefined;
|
|
993
|
-
}
|
|
1018
|
+
},
|
|
994
1019
|
): Promise<MessageDoc[]> {
|
|
995
1020
|
assert(args.userId || args.threadId, "Specify userId or threadId");
|
|
996
1021
|
const opts = this._mergedContextOptions(args.contextOptions);
|
|
@@ -1001,17 +1026,17 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1001
1026
|
assert("runAction" in ctx);
|
|
1002
1027
|
assert(
|
|
1003
1028
|
this.options.textEmbedding,
|
|
1004
|
-
"A textEmbedding model is required to be set on the Agent that you're doing vector search with"
|
|
1029
|
+
"A textEmbedding model is required to be set on the Agent that you're doing vector search with",
|
|
1005
1030
|
);
|
|
1006
1031
|
return {
|
|
1007
|
-
|
|
1032
|
+
embedding: (
|
|
1008
1033
|
await this.doEmbed(ctx, {
|
|
1009
1034
|
userId: args.userId,
|
|
1010
1035
|
threadId: args.threadId,
|
|
1011
1036
|
values: [text],
|
|
1012
1037
|
})
|
|
1013
1038
|
).embeddings[0],
|
|
1014
|
-
|
|
1039
|
+
embeddingModel: this.options.textEmbedding.modelId,
|
|
1015
1040
|
};
|
|
1016
1041
|
},
|
|
1017
1042
|
});
|
|
@@ -1025,7 +1050,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1025
1050
|
*/
|
|
1026
1051
|
async getThreadMetadata(
|
|
1027
1052
|
ctx: RunQueryCtx,
|
|
1028
|
-
args: { threadId: string }
|
|
1053
|
+
args: { threadId: string },
|
|
1029
1054
|
): Promise<ThreadDoc> {
|
|
1030
1055
|
return getThreadMetadata(ctx, this.component, args);
|
|
1031
1056
|
}
|
|
@@ -1044,11 +1069,11 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1044
1069
|
patch: Partial<
|
|
1045
1070
|
Pick<ThreadDoc, (typeof threadFieldsSupportingPatch)[number]>
|
|
1046
1071
|
>;
|
|
1047
|
-
}
|
|
1072
|
+
},
|
|
1048
1073
|
): Promise<ThreadDoc> {
|
|
1049
1074
|
const thread = await ctx.runMutation(
|
|
1050
1075
|
this.component.threads.updateThread,
|
|
1051
|
-
args
|
|
1076
|
+
args,
|
|
1052
1077
|
);
|
|
1053
1078
|
return thread;
|
|
1054
1079
|
}
|
|
@@ -1067,7 +1092,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1067
1092
|
userId: string | undefined;
|
|
1068
1093
|
threadId: string | undefined;
|
|
1069
1094
|
},
|
|
1070
|
-
messages: CoreMessage[]
|
|
1095
|
+
messages: CoreMessage[],
|
|
1071
1096
|
) {
|
|
1072
1097
|
if (!this.options.textEmbedding) {
|
|
1073
1098
|
return undefined;
|
|
@@ -1122,7 +1147,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1122
1147
|
ctx: RunActionCtx,
|
|
1123
1148
|
args: {
|
|
1124
1149
|
messageIds: string[];
|
|
1125
|
-
}
|
|
1150
|
+
},
|
|
1126
1151
|
) {
|
|
1127
1152
|
const messages = (
|
|
1128
1153
|
await ctx.runQuery(this.component.messages.getMessagesByIds, {
|
|
@@ -1134,16 +1159,20 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1134
1159
|
"Some messages were not found: " +
|
|
1135
1160
|
args.messageIds
|
|
1136
1161
|
.filter((id) => !messages.some((m) => m?._id === id))
|
|
1137
|
-
.join(", ")
|
|
1162
|
+
.join(", "),
|
|
1138
1163
|
);
|
|
1139
1164
|
}
|
|
1165
|
+
await this._generateAndSaveEmbeddings(ctx, messages);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
async _generateAndSaveEmbeddings(ctx: RunActionCtx, messages: MessageDoc[]) {
|
|
1140
1169
|
if (messages.some((m) => !m.message)) {
|
|
1141
1170
|
throw new Error(
|
|
1142
1171
|
"Some messages don't have a message: " +
|
|
1143
|
-
|
|
1144
|
-
.
|
|
1145
|
-
.
|
|
1146
|
-
.join(", ")
|
|
1172
|
+
messages
|
|
1173
|
+
.filter((m) => !m.message)
|
|
1174
|
+
.map((m) => m._id)
|
|
1175
|
+
.join(", "),
|
|
1147
1176
|
);
|
|
1148
1177
|
}
|
|
1149
1178
|
const messagesMissingEmbeddings = messages.filter((m) => !m.embeddingId);
|
|
@@ -1156,17 +1185,17 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1156
1185
|
userId: messagesMissingEmbeddings[0]!.userId,
|
|
1157
1186
|
threadId: messagesMissingEmbeddings[0]!.threadId,
|
|
1158
1187
|
},
|
|
1159
|
-
messagesMissingEmbeddings.map((m) => m!.message!)
|
|
1188
|
+
messagesMissingEmbeddings.map((m) => m!.message!),
|
|
1160
1189
|
);
|
|
1161
1190
|
if (!embeddings) {
|
|
1162
1191
|
if (!this.options.textEmbedding) {
|
|
1163
1192
|
throw new Error(
|
|
1164
|
-
"No embeddings were generated for the messages. You must pass a textEmbedding model to the agent constructor."
|
|
1193
|
+
"No embeddings were generated for the messages. You must pass a textEmbedding model to the agent constructor.",
|
|
1165
1194
|
);
|
|
1166
1195
|
}
|
|
1167
1196
|
throw new Error(
|
|
1168
1197
|
"No embeddings were generated for these messages: " +
|
|
1169
|
-
messagesMissingEmbeddings.map((m) => m!._id).join(", ")
|
|
1198
|
+
messagesMissingEmbeddings.map((m) => m!._id).join(", "),
|
|
1170
1199
|
);
|
|
1171
1200
|
}
|
|
1172
1201
|
await ctx.runMutation(this.component.vector.index.insertBatch, {
|
|
@@ -1181,7 +1210,8 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1181
1210
|
vector: embeddings.vectors[i],
|
|
1182
1211
|
}))
|
|
1183
1212
|
.filter(
|
|
1184
|
-
(v): v is Extract<typeof v, { vector: number[] }> =>
|
|
1213
|
+
(v): v is Extract<typeof v, { vector: number[] }> =>
|
|
1214
|
+
v.vector !== null,
|
|
1185
1215
|
),
|
|
1186
1216
|
});
|
|
1187
1217
|
}
|
|
@@ -1214,7 +1244,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1214
1244
|
* Defaults to the chat provider for the Agent.
|
|
1215
1245
|
*/
|
|
1216
1246
|
provider?: string;
|
|
1217
|
-
}
|
|
1247
|
+
},
|
|
1218
1248
|
): Promise<{ messages: MessageDoc[]; pending?: MessageDoc }> {
|
|
1219
1249
|
const messages = await serializeNewMessagesInStep(
|
|
1220
1250
|
ctx,
|
|
@@ -1223,12 +1253,12 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1223
1253
|
{
|
|
1224
1254
|
provider: args.provider ?? this.options.chat.provider,
|
|
1225
1255
|
model: args.model ?? this.options.chat.modelId,
|
|
1226
|
-
}
|
|
1256
|
+
},
|
|
1227
1257
|
);
|
|
1228
1258
|
const embeddings = await this.generateEmbeddings(
|
|
1229
1259
|
ctx,
|
|
1230
1260
|
{ userId: args.userId, threadId: args.threadId },
|
|
1231
|
-
messages.map((m) => m.message)
|
|
1261
|
+
messages.map((m) => m.message),
|
|
1232
1262
|
);
|
|
1233
1263
|
const saved = await ctx.runMutation(this.component.messages.addMessages, {
|
|
1234
1264
|
userId: args.userId,
|
|
@@ -1259,7 +1289,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1259
1289
|
provider: string | undefined;
|
|
1260
1290
|
result: GenerateObjectResult<unknown>;
|
|
1261
1291
|
metadata?: Omit<MessageWithMetadata, "message">;
|
|
1262
|
-
}
|
|
1292
|
+
},
|
|
1263
1293
|
): Promise<void> {
|
|
1264
1294
|
const { messages } = await serializeObjectResult(
|
|
1265
1295
|
ctx,
|
|
@@ -1268,12 +1298,12 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1268
1298
|
{
|
|
1269
1299
|
model: args.model ?? this.options.chat.modelId,
|
|
1270
1300
|
provider: args.provider ?? this.options.chat.provider,
|
|
1271
|
-
}
|
|
1301
|
+
},
|
|
1272
1302
|
);
|
|
1273
1303
|
const embeddings = await this.generateEmbeddings(
|
|
1274
1304
|
ctx,
|
|
1275
1305
|
{ userId: args.userId, threadId: args.threadId },
|
|
1276
|
-
messages.map((m) => m.message)
|
|
1306
|
+
messages.map((m) => m.message),
|
|
1277
1307
|
);
|
|
1278
1308
|
|
|
1279
1309
|
await ctx.runMutation(this.component.messages.addMessages, {
|
|
@@ -1302,7 +1332,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1302
1332
|
threadId: string;
|
|
1303
1333
|
messageId: string;
|
|
1304
1334
|
result: { kind: "error"; error: string } | { kind: "success" };
|
|
1305
|
-
}
|
|
1335
|
+
},
|
|
1306
1336
|
): Promise<void> {
|
|
1307
1337
|
const result = args.result;
|
|
1308
1338
|
if (result.kind === "success") {
|
|
@@ -1346,12 +1376,12 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1346
1376
|
*/
|
|
1347
1377
|
fileIds?: string[];
|
|
1348
1378
|
};
|
|
1349
|
-
}
|
|
1379
|
+
},
|
|
1350
1380
|
): Promise<void> {
|
|
1351
1381
|
const { message, fileIds } = await serializeMessage(
|
|
1352
1382
|
ctx,
|
|
1353
1383
|
this.component,
|
|
1354
|
-
args.patch.message
|
|
1384
|
+
args.patch.message,
|
|
1355
1385
|
);
|
|
1356
1386
|
await ctx.runMutation(this.component.messages.updateMessage, {
|
|
1357
1387
|
messageId: args.messageId,
|
|
@@ -1376,7 +1406,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1376
1406
|
ctx: RunMutationCtx,
|
|
1377
1407
|
args: {
|
|
1378
1408
|
messageIds: string[];
|
|
1379
|
-
}
|
|
1409
|
+
},
|
|
1380
1410
|
): Promise<void> {
|
|
1381
1411
|
await ctx.runMutation(this.component.messages.deleteByIds, args);
|
|
1382
1412
|
}
|
|
@@ -1391,7 +1421,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1391
1421
|
ctx: RunMutationCtx,
|
|
1392
1422
|
args: {
|
|
1393
1423
|
messageId: string;
|
|
1394
|
-
}
|
|
1424
|
+
},
|
|
1395
1425
|
): Promise<void> {
|
|
1396
1426
|
await ctx.runMutation(this.component.messages.deleteByIds, {
|
|
1397
1427
|
messageIds: [args.messageId],
|
|
@@ -1443,7 +1473,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1443
1473
|
startStepOrder?: number;
|
|
1444
1474
|
endOrder: number;
|
|
1445
1475
|
endStepOrder?: number;
|
|
1446
|
-
}
|
|
1476
|
+
},
|
|
1447
1477
|
): Promise<void> {
|
|
1448
1478
|
await ctx.runMutation(this.component.messages.deleteByOrder, {
|
|
1449
1479
|
threadId: args.threadId,
|
|
@@ -1466,7 +1496,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1466
1496
|
args: {
|
|
1467
1497
|
threadId: string;
|
|
1468
1498
|
pageSize?: number;
|
|
1469
|
-
}
|
|
1499
|
+
},
|
|
1470
1500
|
): Promise<void> {
|
|
1471
1501
|
await ctx.runMutation(this.component.threads.deleteAllForThreadIdAsync, {
|
|
1472
1502
|
threadId: args.threadId,
|
|
@@ -1486,7 +1516,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1486
1516
|
args: {
|
|
1487
1517
|
threadId: string;
|
|
1488
1518
|
pageSize?: number;
|
|
1489
|
-
}
|
|
1519
|
+
},
|
|
1490
1520
|
): Promise<void> {
|
|
1491
1521
|
await ctx.runAction(this.component.threads.deleteAllForThreadIdSync, {
|
|
1492
1522
|
threadId: args.threadId,
|
|
@@ -1515,7 +1545,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1515
1545
|
}: {
|
|
1516
1546
|
userId: string | undefined;
|
|
1517
1547
|
threadId: string | undefined;
|
|
1518
|
-
} & Options
|
|
1548
|
+
} & Options,
|
|
1519
1549
|
): Promise<{
|
|
1520
1550
|
args: T & { model: LanguageModelV1 };
|
|
1521
1551
|
userId: string | undefined;
|
|
@@ -1570,7 +1600,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1570
1600
|
userId,
|
|
1571
1601
|
messages: coreMessages,
|
|
1572
1602
|
metadata: coreMessages.map((_, i) =>
|
|
1573
|
-
i === coreMessages.length - 1 ? { id: args.id } : {}
|
|
1603
|
+
i === coreMessages.length - 1 ? { id: args.id } : {},
|
|
1574
1604
|
),
|
|
1575
1605
|
failPendingSteps: true,
|
|
1576
1606
|
});
|
|
@@ -1585,9 +1615,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1585
1615
|
// embeddings yet. This can happen if the message was saved in a mutation
|
|
1586
1616
|
// where the LLM is not available.
|
|
1587
1617
|
if (!promptMessage.embeddingId && this.options.textEmbedding) {
|
|
1588
|
-
await this.
|
|
1589
|
-
messageIds: [promptMessage._id],
|
|
1590
|
-
});
|
|
1618
|
+
await this._generateAndSaveEmbeddings(ctx, [promptMessage]);
|
|
1591
1619
|
}
|
|
1592
1620
|
}
|
|
1593
1621
|
|
|
@@ -1643,29 +1671,29 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1643
1671
|
threadId: string | undefined;
|
|
1644
1672
|
values: string[];
|
|
1645
1673
|
abortSignal?: AbortSignal;
|
|
1646
|
-
headers?: Record<string, string
|
|
1647
|
-
}
|
|
1674
|
+
headers?: Record<string, string>;
|
|
1675
|
+
},
|
|
1648
1676
|
): Promise<{ embeddings: number[][] }> {
|
|
1649
|
-
const
|
|
1677
|
+
const embeddingModel = this.options.textEmbedding;
|
|
1650
1678
|
assert(
|
|
1651
|
-
|
|
1652
|
-
"a textEmbedding model is required to be set on the Agent that you're doing vector search with"
|
|
1679
|
+
embeddingModel,
|
|
1680
|
+
"a textEmbedding model is required to be set on the Agent that you're doing vector search with",
|
|
1653
1681
|
);
|
|
1654
|
-
const result = await
|
|
1682
|
+
const result = await embedMany({
|
|
1683
|
+
model: embeddingModel,
|
|
1655
1684
|
values: options.values,
|
|
1656
1685
|
abortSignal: options.abortSignal,
|
|
1657
1686
|
headers: options.headers,
|
|
1687
|
+
maxRetries: this.options.maxRetries,
|
|
1658
1688
|
});
|
|
1659
1689
|
if (this.options.usageHandler && result.usage) {
|
|
1660
1690
|
await this.options.usageHandler(ctx, {
|
|
1661
1691
|
userId: options.userId,
|
|
1662
1692
|
threadId: options.threadId,
|
|
1663
1693
|
agentName: this.options.name,
|
|
1664
|
-
model:
|
|
1665
|
-
provider:
|
|
1666
|
-
providerMetadata:
|
|
1667
|
-
? { [embedding.provider]: result.rawResponse }
|
|
1668
|
-
: undefined,
|
|
1694
|
+
model: embeddingModel.modelId,
|
|
1695
|
+
provider: embeddingModel.provider,
|
|
1696
|
+
providerMetadata: undefined,
|
|
1669
1697
|
usage: {
|
|
1670
1698
|
promptTokens: result.usage.tokens,
|
|
1671
1699
|
completionTokens: 0,
|
|
@@ -1682,7 +1710,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1682
1710
|
* able to access localhost URLs.
|
|
1683
1711
|
*/
|
|
1684
1712
|
private async _inlineMessagesFiles(
|
|
1685
|
-
messages: CoreMessage[]
|
|
1713
|
+
messages: CoreMessage[],
|
|
1686
1714
|
): Promise<CoreMessage[]> {
|
|
1687
1715
|
// Process each message to convert localhost URLs to base64
|
|
1688
1716
|
return Promise.all(
|
|
@@ -1700,7 +1728,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1700
1728
|
if (part.type === "image" && part.image instanceof URL) {
|
|
1701
1729
|
assert(
|
|
1702
1730
|
message.role === "user",
|
|
1703
|
-
"Images can only be in user messages"
|
|
1731
|
+
"Images can only be in user messages",
|
|
1704
1732
|
);
|
|
1705
1733
|
if (this._isLocalhostUrl(part.image)) {
|
|
1706
1734
|
const imageData = await this._downloadFile(part.image);
|
|
@@ -1723,7 +1751,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1723
1751
|
}
|
|
1724
1752
|
|
|
1725
1753
|
return part;
|
|
1726
|
-
})
|
|
1754
|
+
}),
|
|
1727
1755
|
);
|
|
1728
1756
|
if (message.role === "user") {
|
|
1729
1757
|
return {
|
|
@@ -1736,7 +1764,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1736
1764
|
content: processedContent as AssistantContent,
|
|
1737
1765
|
};
|
|
1738
1766
|
}
|
|
1739
|
-
})
|
|
1767
|
+
}),
|
|
1740
1768
|
);
|
|
1741
1769
|
}
|
|
1742
1770
|
|
|
@@ -1888,7 +1916,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1888
1916
|
options?: {
|
|
1889
1917
|
contextOptions?: ContextOptions;
|
|
1890
1918
|
storageOptions?: StorageOptions;
|
|
1891
|
-
}
|
|
1919
|
+
},
|
|
1892
1920
|
) {
|
|
1893
1921
|
const maxSteps = spec?.maxSteps ?? this.options.maxSteps;
|
|
1894
1922
|
return internalActionGeneric({
|
|
@@ -1912,7 +1940,7 @@ export class Agent<AgentTools extends ToolSet = ToolSet> {
|
|
|
1912
1940
|
storageOptions ??
|
|
1913
1941
|
options?.storageOptions ??
|
|
1914
1942
|
this.options.storageOptions,
|
|
1915
|
-
}
|
|
1943
|
+
},
|
|
1916
1944
|
);
|
|
1917
1945
|
return {
|
|
1918
1946
|
object: value.object as T,
|
|
@@ -1986,11 +2014,11 @@ export async function createThread(
|
|
|
1986
2014
|
userId?: string;
|
|
1987
2015
|
title?: string;
|
|
1988
2016
|
summary?: string;
|
|
1989
|
-
}
|
|
2017
|
+
},
|
|
1990
2018
|
) {
|
|
1991
2019
|
const { _id: threadId } = await ctx.runMutation(
|
|
1992
2020
|
component.threads.createThread,
|
|
1993
|
-
{ userId: args?.userId, title: args?.title, summary: args?.summary }
|
|
2021
|
+
{ userId: args?.userId, title: args?.title, summary: args?.summary },
|
|
1994
2022
|
);
|
|
1995
2023
|
return threadId;
|
|
1996
2024
|
}
|
|
@@ -2004,7 +2032,7 @@ export async function createThread(
|
|
|
2004
2032
|
export async function getThreadMetadata(
|
|
2005
2033
|
ctx: RunQueryCtx,
|
|
2006
2034
|
component: AgentComponent,
|
|
2007
|
-
args: { threadId: string }
|
|
2035
|
+
args: { threadId: string },
|
|
2008
2036
|
): Promise<ThreadDoc> {
|
|
2009
2037
|
const thread = await ctx.runQuery(component.threads.getThread, {
|
|
2010
2038
|
threadId: args.threadId,
|
|
@@ -2060,7 +2088,7 @@ export async function saveMessages(
|
|
|
2060
2088
|
* The agent name to associate with the messages.
|
|
2061
2089
|
*/
|
|
2062
2090
|
agentName?: string;
|
|
2063
|
-
}
|
|
2091
|
+
},
|
|
2064
2092
|
) {
|
|
2065
2093
|
let embeddings: MessageEmbeddings | undefined;
|
|
2066
2094
|
if (args.embeddings) {
|
|
@@ -2088,7 +2116,7 @@ export async function saveMessages(
|
|
|
2088
2116
|
message,
|
|
2089
2117
|
fileIds,
|
|
2090
2118
|
} as MessageWithMetadata;
|
|
2091
|
-
})
|
|
2119
|
+
}),
|
|
2092
2120
|
),
|
|
2093
2121
|
failPendingSteps: args.failPendingSteps ?? false,
|
|
2094
2122
|
pending: args.pending ?? false,
|
|
@@ -2146,7 +2174,7 @@ export async function saveMessage(
|
|
|
2146
2174
|
* The agent name to associate with the message.
|
|
2147
2175
|
*/
|
|
2148
2176
|
agentName?: string;
|
|
2149
|
-
}
|
|
2177
|
+
},
|
|
2150
2178
|
) {
|
|
2151
2179
|
let embeddings:
|
|
2152
2180
|
| {
|