@convex-dev/agent 0.0.14-alpha.5 → 0.0.15-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 +179 -64
- package/dist/commonjs/client/index.d.ts +151 -31
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +37 -24
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/component/messages.d.ts +1 -0
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +4 -1
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/esm/client/index.d.ts +151 -31
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +37 -24
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/component/messages.d.ts +1 -0
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +4 -1
- package/dist/esm/component/messages.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +265 -78
- package/src/component/_generated/api.d.ts +1 -0
- package/src/component/messages.ts +4 -1
package/src/client/index.ts
CHANGED
|
@@ -66,7 +66,7 @@ import type {
|
|
|
66
66
|
} from "./types.js";
|
|
67
67
|
import schema from "../component/schema.js";
|
|
68
68
|
|
|
69
|
-
export {
|
|
69
|
+
export type { Usage, ProviderMetadata };
|
|
70
70
|
export {
|
|
71
71
|
vUsage,
|
|
72
72
|
vProviderMetadata,
|
|
@@ -250,7 +250,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
250
250
|
* @param args The thread metadata.
|
|
251
251
|
* @returns The threadId of the new thread and the thread object.
|
|
252
252
|
*/
|
|
253
|
-
async createThread(
|
|
253
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
254
254
|
ctx: RunActionCtx,
|
|
255
255
|
args?: {
|
|
256
256
|
/**
|
|
@@ -271,10 +271,15 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
271
271
|
* set in the agent constructor.
|
|
272
272
|
*/
|
|
273
273
|
usageHandler?: UsageHandler;
|
|
274
|
+
/**
|
|
275
|
+
* The tools to use for this thread.
|
|
276
|
+
* Overrides any tools passed in the agent constructor.
|
|
277
|
+
*/
|
|
278
|
+
tools?: ThreadTools;
|
|
274
279
|
}
|
|
275
280
|
): Promise<{
|
|
276
281
|
threadId: string;
|
|
277
|
-
thread: Thread<AgentTools>;
|
|
282
|
+
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
278
283
|
}>;
|
|
279
284
|
/**
|
|
280
285
|
* Start a new thread with the agent. This will have a fresh history, though if
|
|
@@ -285,28 +290,48 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
285
290
|
* @param args The thread metadata.
|
|
286
291
|
* @returns The threadId of the new thread.
|
|
287
292
|
*/
|
|
288
|
-
async createThread(
|
|
293
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
289
294
|
ctx: RunMutationCtx,
|
|
290
295
|
args?: {
|
|
296
|
+
/**
|
|
297
|
+
* The userId to associate with the thread. If not provided, the thread will be
|
|
298
|
+
* anonymous.
|
|
299
|
+
*/
|
|
291
300
|
userId?: string;
|
|
301
|
+
/**
|
|
302
|
+
* The title of the thread. Not currently used.
|
|
303
|
+
*/
|
|
292
304
|
title?: string;
|
|
305
|
+
/**
|
|
306
|
+
* The summary of the thread. Not currently used.
|
|
307
|
+
*/
|
|
293
308
|
summary?: string;
|
|
309
|
+
/**
|
|
310
|
+
* The usage handler to use for this thread. Overrides any handler
|
|
311
|
+
* set in the agent constructor.
|
|
312
|
+
*/
|
|
294
313
|
usageHandler?: UsageHandler;
|
|
314
|
+
/**
|
|
315
|
+
* The tools to use for this thread.
|
|
316
|
+
* Overrides any tools passed in the agent constructor.
|
|
317
|
+
*/
|
|
318
|
+
tools?: ThreadTools;
|
|
295
319
|
}
|
|
296
320
|
): Promise<{
|
|
297
321
|
threadId: string;
|
|
298
322
|
}>;
|
|
299
|
-
async createThread(
|
|
323
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
300
324
|
ctx: RunActionCtx | RunMutationCtx,
|
|
301
325
|
args?: {
|
|
302
326
|
userId: string;
|
|
303
327
|
title?: string;
|
|
304
328
|
summary?: string;
|
|
305
329
|
usageHandler?: UsageHandler;
|
|
330
|
+
tools?: ThreadTools;
|
|
306
331
|
}
|
|
307
332
|
): Promise<{
|
|
308
333
|
threadId: string;
|
|
309
|
-
thread?: Thread<AgentTools>;
|
|
334
|
+
thread?: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
310
335
|
}> {
|
|
311
336
|
const threadDoc = await ctx.runMutation(
|
|
312
337
|
this.component.messages.createThread,
|
|
@@ -324,6 +349,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
324
349
|
threadId: threadDoc._id,
|
|
325
350
|
userId: args?.userId,
|
|
326
351
|
usageHandler: args?.usageHandler,
|
|
352
|
+
tools: args?.tools,
|
|
327
353
|
});
|
|
328
354
|
return {
|
|
329
355
|
threadId: threadDoc._id,
|
|
@@ -339,7 +365,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
339
365
|
* @param { threadId, userId }: the thread and user to associate the messages with.
|
|
340
366
|
* @returns Functions bound to the userId and threadId on a `{thread}` object.
|
|
341
367
|
*/
|
|
342
|
-
async continueThread(
|
|
368
|
+
async continueThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
343
369
|
ctx: RunActionCtx,
|
|
344
370
|
args: {
|
|
345
371
|
/**
|
|
@@ -356,9 +382,14 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
356
382
|
* set in the agent constructor.
|
|
357
383
|
*/
|
|
358
384
|
usageHandler?: UsageHandler;
|
|
385
|
+
/**
|
|
386
|
+
* The tools to use for this thread.
|
|
387
|
+
* Overrides any tools passed in the agent constructor.
|
|
388
|
+
*/
|
|
389
|
+
tools?: ThreadTools;
|
|
359
390
|
}
|
|
360
391
|
): Promise<{
|
|
361
|
-
thread: Thread<AgentTools>;
|
|
392
|
+
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
362
393
|
}> {
|
|
363
394
|
return {
|
|
364
395
|
thread: {
|
|
@@ -367,7 +398,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
367
398
|
streamText: this.streamText.bind(this, ctx, args),
|
|
368
399
|
generateObject: this.generateObject.bind(this, ctx, args),
|
|
369
400
|
streamObject: this.streamObject.bind(this, ctx, args),
|
|
370
|
-
} as Thread<AgentTools>,
|
|
401
|
+
} as Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>,
|
|
371
402
|
};
|
|
372
403
|
}
|
|
373
404
|
|
|
@@ -382,17 +413,18 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
382
413
|
async fetchContextMessages(
|
|
383
414
|
ctx: RunQueryCtx | RunActionCtx,
|
|
384
415
|
args: {
|
|
385
|
-
userId
|
|
386
|
-
threadId
|
|
416
|
+
userId: string | undefined;
|
|
417
|
+
threadId: string | undefined;
|
|
387
418
|
messages: CoreMessage[];
|
|
388
419
|
parentMessageId?: string;
|
|
389
|
-
|
|
420
|
+
contextOptions: ContextOptions | undefined;
|
|
421
|
+
}
|
|
390
422
|
): Promise<CoreMessage[]> {
|
|
391
423
|
assert(args.userId || args.threadId, "Specify userId or threadId");
|
|
392
424
|
// Fetch the latest messages from the thread
|
|
393
425
|
const contextMessages: MessageDoc[] = [];
|
|
394
426
|
let included: Set<string> | undefined;
|
|
395
|
-
const opts = this.mergedContextOptions(args);
|
|
427
|
+
const opts = this.mergedContextOptions(args.contextOptions);
|
|
396
428
|
if (opts.searchOptions?.textSearch || opts.searchOptions?.vectorSearch) {
|
|
397
429
|
if (!("runAction" in ctx)) {
|
|
398
430
|
throw new Error("searchUserMessages only works in an action");
|
|
@@ -400,7 +432,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
400
432
|
const searchMessages = await ctx.runAction(
|
|
401
433
|
this.component.messages.searchMessages,
|
|
402
434
|
{
|
|
403
|
-
userId: args.searchOtherThreads
|
|
435
|
+
userId: args.contextOptions?.searchOtherThreads
|
|
436
|
+
? args.userId
|
|
437
|
+
: undefined,
|
|
404
438
|
threadId: args.threadId,
|
|
405
439
|
parentMessageId: args.parentMessageId,
|
|
406
440
|
...(await this.searchOptionsWithDefaults(opts, args.messages)),
|
|
@@ -434,7 +468,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
434
468
|
.map((m) => deserializeMessage(m.message!));
|
|
435
469
|
}
|
|
436
470
|
|
|
437
|
-
|
|
471
|
+
/**
|
|
472
|
+
* Get the embeddings for a set of messages.
|
|
473
|
+
* @param messages The messages to get the embeddings for.
|
|
474
|
+
* @returns The embeddings for the messages.
|
|
475
|
+
*/
|
|
476
|
+
async generateEmbeddings(messages: CoreMessage[]) {
|
|
438
477
|
let embeddings:
|
|
439
478
|
| {
|
|
440
479
|
vectors: (number[] | null)[];
|
|
@@ -512,7 +551,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
512
551
|
lastMessageId: string;
|
|
513
552
|
messageIds: string[];
|
|
514
553
|
}> {
|
|
515
|
-
const embeddings = await this.
|
|
554
|
+
const embeddings = await this.generateEmbeddings(args.messages);
|
|
516
555
|
const result = await ctx.runMutation(this.component.messages.addMessages, {
|
|
517
556
|
threadId: args.threadId,
|
|
518
557
|
userId: args.userId,
|
|
@@ -547,6 +586,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
547
586
|
async saveStep<TOOLS extends ToolSet>(
|
|
548
587
|
ctx: RunMutationCtx,
|
|
549
588
|
args: {
|
|
589
|
+
userId?: string;
|
|
550
590
|
threadId: string;
|
|
551
591
|
/**
|
|
552
592
|
* The message this step is in response to.
|
|
@@ -573,7 +613,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
573
613
|
provider: args.provider ?? this.options.chat.provider,
|
|
574
614
|
model: args.model ?? this.options.chat.modelId,
|
|
575
615
|
});
|
|
576
|
-
const embeddings = await this.
|
|
616
|
+
const embeddings = await this.generateEmbeddings(
|
|
617
|
+
messages.map((m) => m.message)
|
|
618
|
+
);
|
|
577
619
|
if (embeddings) {
|
|
578
620
|
const { model, dimension, vectors } = embeddings;
|
|
579
621
|
for (let i = 0; i < messages.length; i++) {
|
|
@@ -584,6 +626,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
584
626
|
}
|
|
585
627
|
}
|
|
586
628
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
629
|
+
userId: args.userId,
|
|
587
630
|
threadId: args.threadId,
|
|
588
631
|
messageId: args.messageId,
|
|
589
632
|
step: { step, messages },
|
|
@@ -633,7 +676,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
633
676
|
* @returns The result of the generateText function.
|
|
634
677
|
*/
|
|
635
678
|
async generateText<
|
|
636
|
-
TOOLS extends ToolSet,
|
|
679
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
637
680
|
OUTPUT = never,
|
|
638
681
|
OUTPUT_PARTIAL = never,
|
|
639
682
|
>(
|
|
@@ -642,6 +685,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
642
685
|
userId,
|
|
643
686
|
threadId,
|
|
644
687
|
usageHandler,
|
|
688
|
+
tools: threadTools,
|
|
645
689
|
}: {
|
|
646
690
|
userId?: string;
|
|
647
691
|
threadId?: string;
|
|
@@ -650,21 +694,25 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
650
694
|
* set in the agent constructor.
|
|
651
695
|
*/
|
|
652
696
|
usageHandler?: UsageHandler;
|
|
697
|
+
/**
|
|
698
|
+
* The tools to use for this thread. Overrides any tools passed in the agent constructor.
|
|
699
|
+
*/
|
|
700
|
+
tools?: ToolSet;
|
|
653
701
|
},
|
|
654
|
-
args: TextArgs<
|
|
655
|
-
AgentTools,
|
|
656
|
-
TOOLS,
|
|
657
|
-
Parameters<typeof generateText<TOOLS, OUTPUT, OUTPUT_PARTIAL>>[0]
|
|
658
|
-
>
|
|
702
|
+
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL>
|
|
659
703
|
): Promise<
|
|
660
|
-
GenerateTextResult<TOOLS
|
|
704
|
+
GenerateTextResult<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT> &
|
|
705
|
+
GenerationOutputMetadata
|
|
661
706
|
> {
|
|
662
707
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
663
708
|
ctx,
|
|
664
709
|
{ ...args, userId, threadId }
|
|
665
710
|
);
|
|
666
711
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
667
|
-
const tools = wrapTools(
|
|
712
|
+
const tools = wrapTools(
|
|
713
|
+
toolCtx,
|
|
714
|
+
args.tools ?? threadTools ?? this.options.tools
|
|
715
|
+
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
668
716
|
const saveOutputMessages =
|
|
669
717
|
args.saveOutputMessages ??
|
|
670
718
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -677,12 +725,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
677
725
|
maxRetries: this.options.maxRetries,
|
|
678
726
|
...aiArgs,
|
|
679
727
|
model,
|
|
680
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
681
|
-
toolChoice: args.toolChoice as any,
|
|
682
728
|
tools,
|
|
683
729
|
onStepFinish: async (step) => {
|
|
684
730
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
685
731
|
await this.saveStep(ctx, {
|
|
732
|
+
userId,
|
|
686
733
|
threadId,
|
|
687
734
|
messageId,
|
|
688
735
|
step,
|
|
@@ -701,7 +748,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
701
748
|
}
|
|
702
749
|
return args.onStepFinish?.(step);
|
|
703
750
|
},
|
|
704
|
-
})) as GenerateTextResult<
|
|
751
|
+
})) as GenerateTextResult<
|
|
752
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
753
|
+
OUTPUT
|
|
754
|
+
> &
|
|
755
|
+
GenerationOutputMetadata;
|
|
705
756
|
result.messageId = messageId;
|
|
706
757
|
return result;
|
|
707
758
|
} catch (error) {
|
|
@@ -729,7 +780,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
729
780
|
* @returns The result of the streamText function.
|
|
730
781
|
*/
|
|
731
782
|
async streamText<
|
|
732
|
-
TOOLS extends ToolSet,
|
|
783
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
733
784
|
OUTPUT = never,
|
|
734
785
|
PARTIAL_OUTPUT = never,
|
|
735
786
|
>(
|
|
@@ -738,21 +789,34 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
738
789
|
userId,
|
|
739
790
|
threadId,
|
|
740
791
|
usageHandler,
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
792
|
+
/**
|
|
793
|
+
* @deprecated Pass `tools` in the next parameter instead.
|
|
794
|
+
* This is only intended to pass through thread-default tools.
|
|
795
|
+
*/
|
|
796
|
+
tools: threadTools,
|
|
797
|
+
}: {
|
|
798
|
+
userId?: string;
|
|
799
|
+
threadId?: string;
|
|
800
|
+
usageHandler?: UsageHandler;
|
|
801
|
+
tools?: ToolSet;
|
|
802
|
+
},
|
|
803
|
+
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT>
|
|
747
804
|
): Promise<
|
|
748
|
-
StreamTextResult<
|
|
805
|
+
StreamTextResult<
|
|
806
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
807
|
+
PARTIAL_OUTPUT
|
|
808
|
+
> &
|
|
809
|
+
GenerationOutputMetadata
|
|
749
810
|
> {
|
|
750
811
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
751
812
|
ctx,
|
|
752
813
|
{ ...args, userId, threadId }
|
|
753
814
|
);
|
|
754
815
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
755
|
-
const tools = wrapTools(
|
|
816
|
+
const tools = wrapTools(
|
|
817
|
+
toolCtx,
|
|
818
|
+
args.tools ?? threadTools ?? this.options.tools
|
|
819
|
+
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
756
820
|
const saveOutputMessages =
|
|
757
821
|
args.saveOutputMessages ??
|
|
758
822
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -764,8 +828,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
764
828
|
maxRetries: this.options.maxRetries,
|
|
765
829
|
...aiArgs,
|
|
766
830
|
model,
|
|
767
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
768
|
-
toolChoice: args.toolChoice as any,
|
|
769
831
|
tools,
|
|
770
832
|
onChunk: async (chunk) => {
|
|
771
833
|
// console.log("onChunk", chunk);
|
|
@@ -786,6 +848,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
786
848
|
// TODO: compare delta to the output. internally drop the deltas when committing
|
|
787
849
|
if (threadId && messageId) {
|
|
788
850
|
await this.saveStep(ctx, {
|
|
851
|
+
userId,
|
|
789
852
|
threadId,
|
|
790
853
|
messageId,
|
|
791
854
|
step,
|
|
@@ -804,7 +867,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
804
867
|
}
|
|
805
868
|
return args.onStepFinish?.(step);
|
|
806
869
|
},
|
|
807
|
-
}) as StreamTextResult<
|
|
870
|
+
}) as StreamTextResult<
|
|
871
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
872
|
+
PARTIAL_OUTPUT
|
|
873
|
+
> &
|
|
874
|
+
GenerationOutputMetadata;
|
|
808
875
|
result.messageId = messageId;
|
|
809
876
|
return result;
|
|
810
877
|
}
|
|
@@ -829,30 +896,28 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
829
896
|
userId: string | undefined;
|
|
830
897
|
threadId: string | undefined;
|
|
831
898
|
parentMessageId?: string;
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
} &
|
|
835
|
-
T
|
|
899
|
+
contextOptions?: ContextOptions;
|
|
900
|
+
storageOptions?: StorageOptions;
|
|
901
|
+
} & T
|
|
836
902
|
): Promise<{
|
|
837
903
|
args: T;
|
|
838
904
|
messageId: string | undefined;
|
|
839
905
|
}> {
|
|
840
|
-
const
|
|
841
|
-
args.
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
args.saveAllInputMessages ??
|
|
845
|
-
this.options.storageOptions?.saveAllInputMessages;
|
|
906
|
+
const contextOptions: ContextOptions | Record<string, unknown> =
|
|
907
|
+
args.contextOptions ?? this.options.contextOptions ?? args;
|
|
908
|
+
const storageOptions: StorageOptions | Record<string, unknown> =
|
|
909
|
+
args.storageOptions ?? this.options.storageOptions ?? args;
|
|
846
910
|
const messages = promptOrMessagesToCoreMessages(args);
|
|
847
911
|
const contextMessages = await this.fetchContextMessages(ctx, {
|
|
848
|
-
messages,
|
|
849
|
-
parentMessageId,
|
|
850
912
|
userId,
|
|
851
913
|
threadId,
|
|
852
|
-
|
|
914
|
+
messages,
|
|
915
|
+
parentMessageId,
|
|
916
|
+
contextOptions,
|
|
853
917
|
});
|
|
854
918
|
let messageId: string | undefined;
|
|
855
|
-
if (threadId &&
|
|
919
|
+
if (threadId && storageOptions?.saveAnyInputMessages !== false) {
|
|
920
|
+
const saveAll = storageOptions?.saveAllInputMessages;
|
|
856
921
|
const coreMessages = saveAll ? messages : messages.slice(-1);
|
|
857
922
|
const saved = await this.saveMessages(ctx, {
|
|
858
923
|
threadId,
|
|
@@ -917,7 +982,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
917
982
|
} as any)) as GenerateObjectResult<T> & GenerationOutputMetadata;
|
|
918
983
|
|
|
919
984
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
920
|
-
await this.saveObject(ctx, { threadId, messageId, result });
|
|
985
|
+
await this.saveObject(ctx, { threadId, messageId, result, userId });
|
|
921
986
|
}
|
|
922
987
|
result.messageId = messageId;
|
|
923
988
|
if (trackUsage && result.usage) {
|
|
@@ -989,6 +1054,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
989
1054
|
onFinish: async (result) => {
|
|
990
1055
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
991
1056
|
await this.saveObject(ctx, {
|
|
1057
|
+
userId,
|
|
992
1058
|
threadId,
|
|
993
1059
|
messageId,
|
|
994
1060
|
result: {
|
|
@@ -1036,6 +1102,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1036
1102
|
async saveObject(
|
|
1037
1103
|
ctx: RunMutationCtx,
|
|
1038
1104
|
args: {
|
|
1105
|
+
userId: string | undefined;
|
|
1039
1106
|
threadId: string;
|
|
1040
1107
|
messageId: string;
|
|
1041
1108
|
result: GenerateObjectResult<unknown>;
|
|
@@ -1049,7 +1116,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1049
1116
|
provider: this.options.chat.provider,
|
|
1050
1117
|
}
|
|
1051
1118
|
);
|
|
1052
|
-
const embeddings = await this.
|
|
1119
|
+
const embeddings = await this.generateEmbeddings([withoutEmbed[0].message]);
|
|
1053
1120
|
const messages = embeddings?.vectors[0]
|
|
1054
1121
|
? [
|
|
1055
1122
|
{
|
|
@@ -1064,6 +1131,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1064
1131
|
: withoutEmbed;
|
|
1065
1132
|
|
|
1066
1133
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
1134
|
+
userId: args.userId,
|
|
1067
1135
|
threadId: args.threadId,
|
|
1068
1136
|
messageId: args.messageId,
|
|
1069
1137
|
failPendingSteps: false,
|
|
@@ -1071,10 +1139,10 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1071
1139
|
});
|
|
1072
1140
|
}
|
|
1073
1141
|
|
|
1074
|
-
mergedContextOptions(opts: ContextOptions): ContextOptions {
|
|
1142
|
+
mergedContextOptions(opts: ContextOptions | undefined): ContextOptions {
|
|
1075
1143
|
const searchOptions = {
|
|
1076
1144
|
...this.options.contextOptions?.searchOptions,
|
|
1077
|
-
...opts
|
|
1145
|
+
...opts?.searchOptions,
|
|
1078
1146
|
};
|
|
1079
1147
|
return {
|
|
1080
1148
|
...this.options.contextOptions,
|
|
@@ -1306,33 +1374,137 @@ function wrapTools(
|
|
|
1306
1374
|
|
|
1307
1375
|
type TextArgs<
|
|
1308
1376
|
AgentTools extends ToolSet,
|
|
1309
|
-
TOOLS extends ToolSet,
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1377
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1378
|
+
OUTPUT = never,
|
|
1379
|
+
OUTPUT_PARTIAL = never,
|
|
1380
|
+
> = Omit<
|
|
1381
|
+
Parameters<
|
|
1382
|
+
typeof generateText<
|
|
1383
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
1384
|
+
OUTPUT,
|
|
1385
|
+
OUTPUT_PARTIAL
|
|
1386
|
+
>
|
|
1387
|
+
>[0],
|
|
1388
|
+
"toolChoice" | "tools" | "model"
|
|
1389
|
+
> & {
|
|
1390
|
+
/**
|
|
1391
|
+
* The model to use for the tool calls. This will override the model specified
|
|
1392
|
+
* in the Agent constructor.
|
|
1393
|
+
*/
|
|
1316
1394
|
model?: LanguageModelV1;
|
|
1395
|
+
/**
|
|
1396
|
+
* The tools to use for the tool calls. This will override tools specified
|
|
1397
|
+
* in the Agent constructor or createThread / continueThread.
|
|
1398
|
+
*/
|
|
1399
|
+
tools?: TOOLS;
|
|
1400
|
+
/**
|
|
1401
|
+
* The tool choice to use for the tool calls. This must be one of the tools
|
|
1402
|
+
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1403
|
+
*/
|
|
1404
|
+
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1405
|
+
// Non-AI SDK args
|
|
1406
|
+
/**
|
|
1407
|
+
* The parent message id to use for the tool calls.
|
|
1408
|
+
*/
|
|
1317
1409
|
parentMessageId?: string;
|
|
1318
|
-
|
|
1410
|
+
/**
|
|
1411
|
+
* The context options to use for passing in message history to the LLM.
|
|
1412
|
+
*/
|
|
1413
|
+
contextOptions?: ContextOptions;
|
|
1414
|
+
/**
|
|
1415
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1416
|
+
*/
|
|
1417
|
+
storageOptions?: StorageOptions;
|
|
1418
|
+
} & ContextOptions &
|
|
1419
|
+
StorageOptions;
|
|
1420
|
+
|
|
1421
|
+
type StreamingTextArgs<
|
|
1422
|
+
AgentTools extends ToolSet,
|
|
1423
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1424
|
+
OUTPUT = never,
|
|
1425
|
+
OUTPUT_PARTIAL = never,
|
|
1426
|
+
> = Omit<
|
|
1427
|
+
Parameters<
|
|
1428
|
+
typeof streamText<
|
|
1429
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
1430
|
+
OUTPUT,
|
|
1431
|
+
OUTPUT_PARTIAL
|
|
1432
|
+
>
|
|
1433
|
+
>[0],
|
|
1434
|
+
"toolChoice" | "tools" | "model"
|
|
1435
|
+
> & {
|
|
1436
|
+
/**
|
|
1437
|
+
* The model to use for the tool calls. This will override the model specified
|
|
1438
|
+
* in the Agent constructor.
|
|
1439
|
+
*/
|
|
1440
|
+
model?: LanguageModelV1;
|
|
1441
|
+
/**
|
|
1442
|
+
* The tools to use for the tool calls. This will override tools specified
|
|
1443
|
+
* in the Agent constructor or createThread / continueThread.
|
|
1444
|
+
*/
|
|
1319
1445
|
tools?: TOOLS;
|
|
1320
|
-
|
|
1446
|
+
/**
|
|
1447
|
+
* The tool choice to use for the tool calls. This must be one of the tools
|
|
1448
|
+
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1449
|
+
*/
|
|
1450
|
+
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1451
|
+
// Non-AI SDK args
|
|
1452
|
+
/**
|
|
1453
|
+
* The parent message id to use for the tool calls.
|
|
1454
|
+
*/
|
|
1455
|
+
parentMessageId?: string;
|
|
1456
|
+
/**
|
|
1457
|
+
* The context options to use for passing in message history to the LLM.
|
|
1458
|
+
*/
|
|
1459
|
+
contextOptions?: ContextOptions;
|
|
1460
|
+
/**
|
|
1461
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1462
|
+
*/
|
|
1463
|
+
storageOptions?: StorageOptions;
|
|
1321
1464
|
} & ContextOptions &
|
|
1322
1465
|
StorageOptions;
|
|
1323
1466
|
|
|
1324
1467
|
type BaseGenerateObjectOptions = StorageOptions &
|
|
1325
1468
|
ContextOptions &
|
|
1326
1469
|
CallSettings & {
|
|
1470
|
+
/**
|
|
1471
|
+
* The model to use for the object generation. This will override the model
|
|
1472
|
+
* specified in the Agent constructor.
|
|
1473
|
+
*/
|
|
1327
1474
|
model?: LanguageModelV1;
|
|
1328
|
-
|
|
1475
|
+
/**
|
|
1476
|
+
* The system prompt to use for the object generation. This will override the
|
|
1477
|
+
* system prompt specified in the Agent constructor.
|
|
1478
|
+
*/
|
|
1329
1479
|
system?: string;
|
|
1480
|
+
/**
|
|
1481
|
+
* The prompt to the LLM to use for the object generation.
|
|
1482
|
+
* Specify this or messages, but not both.
|
|
1483
|
+
*/
|
|
1330
1484
|
prompt?: string;
|
|
1485
|
+
/**
|
|
1486
|
+
* The messages to use for the object generation.
|
|
1487
|
+
* Note: recent messages are automatically added based on the thread it's
|
|
1488
|
+
* associated with and your contextOptions.
|
|
1489
|
+
*/
|
|
1331
1490
|
messages?: CoreMessage[];
|
|
1332
1491
|
experimental_repairText?: RepairTextFunction;
|
|
1333
1492
|
experimental_telemetry?: TelemetrySettings;
|
|
1334
1493
|
providerOptions?: ProviderOptions;
|
|
1335
1494
|
experimental_providerMetadata?: ProviderMetadata;
|
|
1495
|
+
// Non-AI SDK args
|
|
1496
|
+
/**
|
|
1497
|
+
* The parent message id to use for the object generation.
|
|
1498
|
+
*/
|
|
1499
|
+
parentMessageId?: string;
|
|
1500
|
+
/**
|
|
1501
|
+
* The context options to use for passing in message history to the LLM.
|
|
1502
|
+
*/
|
|
1503
|
+
contextOptions?: ContextOptions;
|
|
1504
|
+
/**
|
|
1505
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1506
|
+
*/
|
|
1507
|
+
storageOptions?: StorageOptions;
|
|
1336
1508
|
};
|
|
1337
1509
|
|
|
1338
1510
|
type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
|
|
@@ -1397,7 +1569,7 @@ type ThreadOutputMetadata = GenerationOutputMetadata & {
|
|
|
1397
1569
|
messageId: string;
|
|
1398
1570
|
};
|
|
1399
1571
|
|
|
1400
|
-
interface Thread<
|
|
1572
|
+
interface Thread<DefaultTools extends ToolSet> {
|
|
1401
1573
|
/**
|
|
1402
1574
|
* The target threadId, from the startThread or continueThread initializers.
|
|
1403
1575
|
*/
|
|
@@ -1412,14 +1584,20 @@ interface Thread<AgentTools extends ToolSet> {
|
|
|
1412
1584
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
1413
1585
|
* @returns The result of the generateText function.
|
|
1414
1586
|
*/
|
|
1415
|
-
generateText<
|
|
1587
|
+
generateText<
|
|
1588
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1589
|
+
OUTPUT = never,
|
|
1590
|
+
OUTPUT_PARTIAL = never,
|
|
1591
|
+
>(
|
|
1416
1592
|
args: TextArgs<
|
|
1417
|
-
|
|
1593
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1418
1594
|
TOOLS,
|
|
1419
|
-
|
|
1595
|
+
OUTPUT,
|
|
1596
|
+
OUTPUT_PARTIAL
|
|
1420
1597
|
>
|
|
1421
1598
|
): Promise<
|
|
1422
|
-
GenerateTextResult<TOOLS
|
|
1599
|
+
GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
|
|
1600
|
+
ThreadOutputMetadata
|
|
1423
1601
|
>;
|
|
1424
1602
|
|
|
1425
1603
|
/**
|
|
@@ -1432,14 +1610,23 @@ interface Thread<AgentTools extends ToolSet> {
|
|
|
1432
1610
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
1433
1611
|
* @returns The result of the streamText function.
|
|
1434
1612
|
*/
|
|
1435
|
-
streamText<
|
|
1436
|
-
|
|
1437
|
-
|
|
1613
|
+
streamText<
|
|
1614
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1615
|
+
OUTPUT = never,
|
|
1616
|
+
PARTIAL_OUTPUT = never,
|
|
1617
|
+
>(
|
|
1618
|
+
args: StreamingTextArgs<
|
|
1619
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1438
1620
|
TOOLS,
|
|
1439
|
-
|
|
1621
|
+
OUTPUT,
|
|
1622
|
+
PARTIAL_OUTPUT
|
|
1440
1623
|
>
|
|
1441
1624
|
): Promise<
|
|
1442
|
-
StreamTextResult<
|
|
1625
|
+
StreamTextResult<
|
|
1626
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1627
|
+
PARTIAL_OUTPUT
|
|
1628
|
+
> &
|
|
1629
|
+
ThreadOutputMetadata
|
|
1443
1630
|
>;
|
|
1444
1631
|
/**
|
|
1445
1632
|
* This behaves like {@link generateObject} from the "ai" package except that
|
|
@@ -471,6 +471,7 @@ async function getMaxMessage(
|
|
|
471
471
|
}
|
|
472
472
|
|
|
473
473
|
const addStepArgs = {
|
|
474
|
+
userId: v.optional(v.string()),
|
|
474
475
|
threadId: v.id("threads"),
|
|
475
476
|
messageId: v.id("messages"),
|
|
476
477
|
step: vStepWithMessages,
|
|
@@ -515,9 +516,11 @@ async function addStepHandler(
|
|
|
515
516
|
step,
|
|
516
517
|
});
|
|
517
518
|
await addMessagesHandler(ctx, {
|
|
519
|
+
userId: args.userId,
|
|
518
520
|
threadId: args.threadId,
|
|
519
|
-
parentMessageId: args.messageId,
|
|
520
521
|
stepId,
|
|
522
|
+
parentMessageId: args.messageId,
|
|
523
|
+
agentName: parentMessage.agentName,
|
|
521
524
|
messages,
|
|
522
525
|
pending: step.finishReason === "stop" ? false : true,
|
|
523
526
|
failPendingSteps: false,
|