@convex-dev/agent 0.0.14 → 0.0.15-alpha.1
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 +152 -34
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +34 -24
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/esm/client/index.d.ts +152 -34
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +34 -24
- package/dist/esm/client/index.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +260 -79
package/src/client/index.ts
CHANGED
|
@@ -27,7 +27,7 @@ import { assert } from "convex-helpers";
|
|
|
27
27
|
import { internalActionGeneric, internalMutationGeneric } from "convex/server";
|
|
28
28
|
import { Infer, v } from "convex/values";
|
|
29
29
|
import { z } from "zod";
|
|
30
|
-
import {
|
|
30
|
+
import { api } from "../component/_generated/api.js";
|
|
31
31
|
import {
|
|
32
32
|
validateVectorDimension,
|
|
33
33
|
type VectorDimension,
|
|
@@ -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,
|
|
@@ -174,7 +174,8 @@ export type UsageHandler = (
|
|
|
174
174
|
|
|
175
175
|
export class Agent<AgentTools extends ToolSet> {
|
|
176
176
|
constructor(
|
|
177
|
-
public component: UseApi<Mounts>,
|
|
177
|
+
// public component: UseApi<Mounts>,
|
|
178
|
+
public component: UseApi<typeof api>,
|
|
178
179
|
public options: {
|
|
179
180
|
/**
|
|
180
181
|
* The name for the agent. This will be attributed on each message
|
|
@@ -250,7 +251,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
250
251
|
* @param args The thread metadata.
|
|
251
252
|
* @returns The threadId of the new thread and the thread object.
|
|
252
253
|
*/
|
|
253
|
-
async createThread(
|
|
254
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
254
255
|
ctx: RunActionCtx,
|
|
255
256
|
args?: {
|
|
256
257
|
/**
|
|
@@ -271,10 +272,15 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
271
272
|
* set in the agent constructor.
|
|
272
273
|
*/
|
|
273
274
|
usageHandler?: UsageHandler;
|
|
275
|
+
/**
|
|
276
|
+
* The tools to use for this thread.
|
|
277
|
+
* Overrides any tools passed in the agent constructor.
|
|
278
|
+
*/
|
|
279
|
+
tools?: ThreadTools;
|
|
274
280
|
}
|
|
275
281
|
): Promise<{
|
|
276
282
|
threadId: string;
|
|
277
|
-
thread: Thread<AgentTools>;
|
|
283
|
+
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
278
284
|
}>;
|
|
279
285
|
/**
|
|
280
286
|
* Start a new thread with the agent. This will have a fresh history, though if
|
|
@@ -285,28 +291,48 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
285
291
|
* @param args The thread metadata.
|
|
286
292
|
* @returns The threadId of the new thread.
|
|
287
293
|
*/
|
|
288
|
-
async createThread(
|
|
294
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
289
295
|
ctx: RunMutationCtx,
|
|
290
296
|
args?: {
|
|
297
|
+
/**
|
|
298
|
+
* The userId to associate with the thread. If not provided, the thread will be
|
|
299
|
+
* anonymous.
|
|
300
|
+
*/
|
|
291
301
|
userId?: string;
|
|
302
|
+
/**
|
|
303
|
+
* The title of the thread. Not currently used.
|
|
304
|
+
*/
|
|
292
305
|
title?: string;
|
|
306
|
+
/**
|
|
307
|
+
* The summary of the thread. Not currently used.
|
|
308
|
+
*/
|
|
293
309
|
summary?: string;
|
|
310
|
+
/**
|
|
311
|
+
* The usage handler to use for this thread. Overrides any handler
|
|
312
|
+
* set in the agent constructor.
|
|
313
|
+
*/
|
|
294
314
|
usageHandler?: UsageHandler;
|
|
315
|
+
/**
|
|
316
|
+
* The tools to use for this thread.
|
|
317
|
+
* Overrides any tools passed in the agent constructor.
|
|
318
|
+
*/
|
|
319
|
+
tools?: ThreadTools;
|
|
295
320
|
}
|
|
296
321
|
): Promise<{
|
|
297
322
|
threadId: string;
|
|
298
323
|
}>;
|
|
299
|
-
async createThread(
|
|
324
|
+
async createThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
300
325
|
ctx: RunActionCtx | RunMutationCtx,
|
|
301
326
|
args?: {
|
|
302
327
|
userId: string;
|
|
303
328
|
title?: string;
|
|
304
329
|
summary?: string;
|
|
305
330
|
usageHandler?: UsageHandler;
|
|
331
|
+
tools?: ThreadTools;
|
|
306
332
|
}
|
|
307
333
|
): Promise<{
|
|
308
334
|
threadId: string;
|
|
309
|
-
thread?: Thread<AgentTools>;
|
|
335
|
+
thread?: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
310
336
|
}> {
|
|
311
337
|
const threadDoc = await ctx.runMutation(
|
|
312
338
|
this.component.messages.createThread,
|
|
@@ -324,6 +350,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
324
350
|
threadId: threadDoc._id,
|
|
325
351
|
userId: args?.userId,
|
|
326
352
|
usageHandler: args?.usageHandler,
|
|
353
|
+
tools: args?.tools,
|
|
327
354
|
});
|
|
328
355
|
return {
|
|
329
356
|
threadId: threadDoc._id,
|
|
@@ -339,7 +366,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
339
366
|
* @param { threadId, userId }: the thread and user to associate the messages with.
|
|
340
367
|
* @returns Functions bound to the userId and threadId on a `{thread}` object.
|
|
341
368
|
*/
|
|
342
|
-
async continueThread(
|
|
369
|
+
async continueThread<ThreadTools extends ToolSet | undefined = undefined>(
|
|
343
370
|
ctx: RunActionCtx,
|
|
344
371
|
args: {
|
|
345
372
|
/**
|
|
@@ -356,9 +383,14 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
356
383
|
* set in the agent constructor.
|
|
357
384
|
*/
|
|
358
385
|
usageHandler?: UsageHandler;
|
|
386
|
+
/**
|
|
387
|
+
* The tools to use for this thread.
|
|
388
|
+
* Overrides any tools passed in the agent constructor.
|
|
389
|
+
*/
|
|
390
|
+
tools?: ThreadTools;
|
|
359
391
|
}
|
|
360
392
|
): Promise<{
|
|
361
|
-
thread: Thread<AgentTools>;
|
|
393
|
+
thread: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
362
394
|
}> {
|
|
363
395
|
return {
|
|
364
396
|
thread: {
|
|
@@ -367,7 +399,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
367
399
|
streamText: this.streamText.bind(this, ctx, args),
|
|
368
400
|
generateObject: this.generateObject.bind(this, ctx, args),
|
|
369
401
|
streamObject: this.streamObject.bind(this, ctx, args),
|
|
370
|
-
} as Thread<AgentTools>,
|
|
402
|
+
} as Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>,
|
|
371
403
|
};
|
|
372
404
|
}
|
|
373
405
|
|
|
@@ -382,17 +414,18 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
382
414
|
async fetchContextMessages(
|
|
383
415
|
ctx: RunQueryCtx | RunActionCtx,
|
|
384
416
|
args: {
|
|
385
|
-
userId
|
|
386
|
-
threadId
|
|
417
|
+
userId: string | undefined;
|
|
418
|
+
threadId: string | undefined;
|
|
387
419
|
messages: CoreMessage[];
|
|
388
420
|
parentMessageId?: string;
|
|
389
|
-
|
|
421
|
+
contextOptions: ContextOptions | undefined;
|
|
422
|
+
}
|
|
390
423
|
): Promise<CoreMessage[]> {
|
|
391
424
|
assert(args.userId || args.threadId, "Specify userId or threadId");
|
|
392
425
|
// Fetch the latest messages from the thread
|
|
393
426
|
const contextMessages: MessageDoc[] = [];
|
|
394
427
|
let included: Set<string> | undefined;
|
|
395
|
-
const opts = this.mergedContextOptions(args);
|
|
428
|
+
const opts = this.mergedContextOptions(args.contextOptions);
|
|
396
429
|
if (opts.searchOptions?.textSearch || opts.searchOptions?.vectorSearch) {
|
|
397
430
|
if (!("runAction" in ctx)) {
|
|
398
431
|
throw new Error("searchUserMessages only works in an action");
|
|
@@ -400,7 +433,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
400
433
|
const searchMessages = await ctx.runAction(
|
|
401
434
|
this.component.messages.searchMessages,
|
|
402
435
|
{
|
|
403
|
-
userId: args.searchOtherThreads
|
|
436
|
+
userId: args.contextOptions?.searchOtherThreads
|
|
437
|
+
? args.userId
|
|
438
|
+
: undefined,
|
|
404
439
|
threadId: args.threadId,
|
|
405
440
|
parentMessageId: args.parentMessageId,
|
|
406
441
|
...(await this.searchOptionsWithDefaults(opts, args.messages)),
|
|
@@ -434,7 +469,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
434
469
|
.map((m) => deserializeMessage(m.message!));
|
|
435
470
|
}
|
|
436
471
|
|
|
437
|
-
|
|
472
|
+
/**
|
|
473
|
+
* Get the embeddings for a set of messages.
|
|
474
|
+
* @param messages The messages to get the embeddings for.
|
|
475
|
+
* @returns The embeddings for the messages.
|
|
476
|
+
*/
|
|
477
|
+
async generateEmbeddings(messages: CoreMessage[]) {
|
|
438
478
|
let embeddings:
|
|
439
479
|
| {
|
|
440
480
|
vectors: (number[] | null)[];
|
|
@@ -512,7 +552,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
512
552
|
lastMessageId: string;
|
|
513
553
|
messageIds: string[];
|
|
514
554
|
}> {
|
|
515
|
-
const embeddings = await this.
|
|
555
|
+
const embeddings = await this.generateEmbeddings(args.messages);
|
|
516
556
|
const result = await ctx.runMutation(this.component.messages.addMessages, {
|
|
517
557
|
threadId: args.threadId,
|
|
518
558
|
userId: args.userId,
|
|
@@ -574,7 +614,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
574
614
|
provider: args.provider ?? this.options.chat.provider,
|
|
575
615
|
model: args.model ?? this.options.chat.modelId,
|
|
576
616
|
});
|
|
577
|
-
const embeddings = await this.
|
|
617
|
+
const embeddings = await this.generateEmbeddings(
|
|
618
|
+
messages.map((m) => m.message)
|
|
619
|
+
);
|
|
578
620
|
if (embeddings) {
|
|
579
621
|
const { model, dimension, vectors } = embeddings;
|
|
580
622
|
for (let i = 0; i < messages.length; i++) {
|
|
@@ -635,7 +677,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
635
677
|
* @returns The result of the generateText function.
|
|
636
678
|
*/
|
|
637
679
|
async generateText<
|
|
638
|
-
TOOLS extends ToolSet,
|
|
680
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
639
681
|
OUTPUT = never,
|
|
640
682
|
OUTPUT_PARTIAL = never,
|
|
641
683
|
>(
|
|
@@ -644,6 +686,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
644
686
|
userId,
|
|
645
687
|
threadId,
|
|
646
688
|
usageHandler,
|
|
689
|
+
tools: threadTools,
|
|
647
690
|
}: {
|
|
648
691
|
userId?: string;
|
|
649
692
|
threadId?: string;
|
|
@@ -652,21 +695,25 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
652
695
|
* set in the agent constructor.
|
|
653
696
|
*/
|
|
654
697
|
usageHandler?: UsageHandler;
|
|
698
|
+
/**
|
|
699
|
+
* The tools to use for this thread. Overrides any tools passed in the agent constructor.
|
|
700
|
+
*/
|
|
701
|
+
tools?: ToolSet;
|
|
655
702
|
},
|
|
656
|
-
args: TextArgs<
|
|
657
|
-
AgentTools,
|
|
658
|
-
TOOLS,
|
|
659
|
-
Parameters<typeof generateText<TOOLS, OUTPUT, OUTPUT_PARTIAL>>[0]
|
|
660
|
-
>
|
|
703
|
+
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL>
|
|
661
704
|
): Promise<
|
|
662
|
-
GenerateTextResult<TOOLS
|
|
705
|
+
GenerateTextResult<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT> &
|
|
706
|
+
GenerationOutputMetadata
|
|
663
707
|
> {
|
|
664
708
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
665
709
|
ctx,
|
|
666
710
|
{ ...args, userId, threadId }
|
|
667
711
|
);
|
|
668
712
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
669
|
-
const tools = wrapTools(
|
|
713
|
+
const tools = wrapTools(
|
|
714
|
+
toolCtx,
|
|
715
|
+
args.tools ?? threadTools ?? this.options.tools
|
|
716
|
+
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
670
717
|
const saveOutputMessages =
|
|
671
718
|
args.saveOutputMessages ??
|
|
672
719
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -679,8 +726,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
679
726
|
maxRetries: this.options.maxRetries,
|
|
680
727
|
...aiArgs,
|
|
681
728
|
model,
|
|
682
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
683
|
-
toolChoice: args.toolChoice as any,
|
|
684
729
|
tools,
|
|
685
730
|
onStepFinish: async (step) => {
|
|
686
731
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
@@ -704,7 +749,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
704
749
|
}
|
|
705
750
|
return args.onStepFinish?.(step);
|
|
706
751
|
},
|
|
707
|
-
})) as GenerateTextResult<
|
|
752
|
+
})) as GenerateTextResult<
|
|
753
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
754
|
+
OUTPUT
|
|
755
|
+
> &
|
|
756
|
+
GenerationOutputMetadata;
|
|
708
757
|
result.messageId = messageId;
|
|
709
758
|
return result;
|
|
710
759
|
} catch (error) {
|
|
@@ -732,7 +781,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
732
781
|
* @returns The result of the streamText function.
|
|
733
782
|
*/
|
|
734
783
|
async streamText<
|
|
735
|
-
TOOLS extends ToolSet,
|
|
784
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
736
785
|
OUTPUT = never,
|
|
737
786
|
PARTIAL_OUTPUT = never,
|
|
738
787
|
>(
|
|
@@ -741,21 +790,34 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
741
790
|
userId,
|
|
742
791
|
threadId,
|
|
743
792
|
usageHandler,
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
793
|
+
/**
|
|
794
|
+
* @deprecated Pass `tools` in the next parameter instead.
|
|
795
|
+
* This is only intended to pass through thread-default tools.
|
|
796
|
+
*/
|
|
797
|
+
tools: threadTools,
|
|
798
|
+
}: {
|
|
799
|
+
userId?: string;
|
|
800
|
+
threadId?: string;
|
|
801
|
+
usageHandler?: UsageHandler;
|
|
802
|
+
tools?: ToolSet;
|
|
803
|
+
},
|
|
804
|
+
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT>
|
|
750
805
|
): Promise<
|
|
751
|
-
StreamTextResult<
|
|
806
|
+
StreamTextResult<
|
|
807
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
808
|
+
PARTIAL_OUTPUT
|
|
809
|
+
> &
|
|
810
|
+
GenerationOutputMetadata
|
|
752
811
|
> {
|
|
753
812
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
754
813
|
ctx,
|
|
755
814
|
{ ...args, userId, threadId }
|
|
756
815
|
);
|
|
757
816
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
758
|
-
const tools = wrapTools(
|
|
817
|
+
const tools = wrapTools(
|
|
818
|
+
toolCtx,
|
|
819
|
+
args.tools ?? threadTools ?? this.options.tools
|
|
820
|
+
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
759
821
|
const saveOutputMessages =
|
|
760
822
|
args.saveOutputMessages ??
|
|
761
823
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -767,8 +829,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
767
829
|
maxRetries: this.options.maxRetries,
|
|
768
830
|
...aiArgs,
|
|
769
831
|
model,
|
|
770
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
771
|
-
toolChoice: args.toolChoice as any,
|
|
772
832
|
tools,
|
|
773
833
|
onChunk: async (chunk) => {
|
|
774
834
|
// console.log("onChunk", chunk);
|
|
@@ -808,7 +868,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
808
868
|
}
|
|
809
869
|
return args.onStepFinish?.(step);
|
|
810
870
|
},
|
|
811
|
-
}) as StreamTextResult<
|
|
871
|
+
}) as StreamTextResult<
|
|
872
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
873
|
+
PARTIAL_OUTPUT
|
|
874
|
+
> &
|
|
875
|
+
GenerationOutputMetadata;
|
|
812
876
|
result.messageId = messageId;
|
|
813
877
|
return result;
|
|
814
878
|
}
|
|
@@ -833,30 +897,28 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
833
897
|
userId: string | undefined;
|
|
834
898
|
threadId: string | undefined;
|
|
835
899
|
parentMessageId?: string;
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
} &
|
|
839
|
-
T
|
|
900
|
+
contextOptions?: ContextOptions;
|
|
901
|
+
storageOptions?: StorageOptions;
|
|
902
|
+
} & T
|
|
840
903
|
): Promise<{
|
|
841
904
|
args: T;
|
|
842
905
|
messageId: string | undefined;
|
|
843
906
|
}> {
|
|
844
|
-
const
|
|
845
|
-
args.
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
args.saveAllInputMessages ??
|
|
849
|
-
this.options.storageOptions?.saveAllInputMessages;
|
|
907
|
+
const contextOptions: ContextOptions | Record<string, unknown> =
|
|
908
|
+
args.contextOptions ?? this.options.contextOptions ?? args;
|
|
909
|
+
const storageOptions: StorageOptions | Record<string, unknown> =
|
|
910
|
+
args.storageOptions ?? this.options.storageOptions ?? args;
|
|
850
911
|
const messages = promptOrMessagesToCoreMessages(args);
|
|
851
912
|
const contextMessages = await this.fetchContextMessages(ctx, {
|
|
852
|
-
messages,
|
|
853
|
-
parentMessageId,
|
|
854
913
|
userId,
|
|
855
914
|
threadId,
|
|
856
|
-
|
|
915
|
+
messages,
|
|
916
|
+
parentMessageId,
|
|
917
|
+
contextOptions,
|
|
857
918
|
});
|
|
858
919
|
let messageId: string | undefined;
|
|
859
|
-
if (threadId &&
|
|
920
|
+
if (threadId && storageOptions?.saveAnyInputMessages !== false) {
|
|
921
|
+
const saveAll = storageOptions?.saveAllInputMessages;
|
|
860
922
|
const coreMessages = saveAll ? messages : messages.slice(-1);
|
|
861
923
|
const saved = await this.saveMessages(ctx, {
|
|
862
924
|
threadId,
|
|
@@ -1055,7 +1117,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1055
1117
|
provider: this.options.chat.provider,
|
|
1056
1118
|
}
|
|
1057
1119
|
);
|
|
1058
|
-
const embeddings = await this.
|
|
1120
|
+
const embeddings = await this.generateEmbeddings([withoutEmbed[0].message]);
|
|
1059
1121
|
const messages = embeddings?.vectors[0]
|
|
1060
1122
|
? [
|
|
1061
1123
|
{
|
|
@@ -1078,10 +1140,10 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1078
1140
|
});
|
|
1079
1141
|
}
|
|
1080
1142
|
|
|
1081
|
-
mergedContextOptions(opts: ContextOptions): ContextOptions {
|
|
1143
|
+
mergedContextOptions(opts: ContextOptions | undefined): ContextOptions {
|
|
1082
1144
|
const searchOptions = {
|
|
1083
1145
|
...this.options.contextOptions?.searchOptions,
|
|
1084
|
-
...opts
|
|
1146
|
+
...opts?.searchOptions,
|
|
1085
1147
|
};
|
|
1086
1148
|
return {
|
|
1087
1149
|
...this.options.contextOptions,
|
|
@@ -1313,33 +1375,137 @@ function wrapTools(
|
|
|
1313
1375
|
|
|
1314
1376
|
type TextArgs<
|
|
1315
1377
|
AgentTools extends ToolSet,
|
|
1316
|
-
TOOLS extends ToolSet,
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1378
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1379
|
+
OUTPUT = never,
|
|
1380
|
+
OUTPUT_PARTIAL = never,
|
|
1381
|
+
> = Omit<
|
|
1382
|
+
Parameters<
|
|
1383
|
+
typeof generateText<
|
|
1384
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
1385
|
+
OUTPUT,
|
|
1386
|
+
OUTPUT_PARTIAL
|
|
1387
|
+
>
|
|
1388
|
+
>[0],
|
|
1389
|
+
"toolChoice" | "tools" | "model"
|
|
1390
|
+
> & {
|
|
1391
|
+
/**
|
|
1392
|
+
* The model to use for the tool calls. This will override the model specified
|
|
1393
|
+
* in the Agent constructor.
|
|
1394
|
+
*/
|
|
1323
1395
|
model?: LanguageModelV1;
|
|
1396
|
+
/**
|
|
1397
|
+
* The tools to use for the tool calls. This will override tools specified
|
|
1398
|
+
* in the Agent constructor or createThread / continueThread.
|
|
1399
|
+
*/
|
|
1400
|
+
tools?: TOOLS;
|
|
1401
|
+
/**
|
|
1402
|
+
* The tool choice to use for the tool calls. This must be one of the tools
|
|
1403
|
+
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1404
|
+
*/
|
|
1405
|
+
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1406
|
+
// Non-AI SDK args
|
|
1407
|
+
/**
|
|
1408
|
+
* The parent message id to use for the tool calls.
|
|
1409
|
+
*/
|
|
1324
1410
|
parentMessageId?: string;
|
|
1325
|
-
|
|
1411
|
+
/**
|
|
1412
|
+
* The context options to use for passing in message history to the LLM.
|
|
1413
|
+
*/
|
|
1414
|
+
contextOptions?: ContextOptions;
|
|
1415
|
+
/**
|
|
1416
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1417
|
+
*/
|
|
1418
|
+
storageOptions?: StorageOptions;
|
|
1419
|
+
} & ContextOptions &
|
|
1420
|
+
StorageOptions;
|
|
1421
|
+
|
|
1422
|
+
type StreamingTextArgs<
|
|
1423
|
+
AgentTools extends ToolSet,
|
|
1424
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1425
|
+
OUTPUT = never,
|
|
1426
|
+
OUTPUT_PARTIAL = never,
|
|
1427
|
+
> = Omit<
|
|
1428
|
+
Parameters<
|
|
1429
|
+
typeof streamText<
|
|
1430
|
+
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
1431
|
+
OUTPUT,
|
|
1432
|
+
OUTPUT_PARTIAL
|
|
1433
|
+
>
|
|
1434
|
+
>[0],
|
|
1435
|
+
"toolChoice" | "tools" | "model"
|
|
1436
|
+
> & {
|
|
1437
|
+
/**
|
|
1438
|
+
* The model to use for the tool calls. This will override the model specified
|
|
1439
|
+
* in the Agent constructor.
|
|
1440
|
+
*/
|
|
1441
|
+
model?: LanguageModelV1;
|
|
1442
|
+
/**
|
|
1443
|
+
* The tools to use for the tool calls. This will override tools specified
|
|
1444
|
+
* in the Agent constructor or createThread / continueThread.
|
|
1445
|
+
*/
|
|
1326
1446
|
tools?: TOOLS;
|
|
1327
|
-
|
|
1447
|
+
/**
|
|
1448
|
+
* The tool choice to use for the tool calls. This must be one of the tools
|
|
1449
|
+
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1450
|
+
*/
|
|
1451
|
+
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1452
|
+
// Non-AI SDK args
|
|
1453
|
+
/**
|
|
1454
|
+
* The parent message id to use for the tool calls.
|
|
1455
|
+
*/
|
|
1456
|
+
parentMessageId?: string;
|
|
1457
|
+
/**
|
|
1458
|
+
* The context options to use for passing in message history to the LLM.
|
|
1459
|
+
*/
|
|
1460
|
+
contextOptions?: ContextOptions;
|
|
1461
|
+
/**
|
|
1462
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1463
|
+
*/
|
|
1464
|
+
storageOptions?: StorageOptions;
|
|
1328
1465
|
} & ContextOptions &
|
|
1329
1466
|
StorageOptions;
|
|
1330
1467
|
|
|
1331
1468
|
type BaseGenerateObjectOptions = StorageOptions &
|
|
1332
1469
|
ContextOptions &
|
|
1333
1470
|
CallSettings & {
|
|
1471
|
+
/**
|
|
1472
|
+
* The model to use for the object generation. This will override the model
|
|
1473
|
+
* specified in the Agent constructor.
|
|
1474
|
+
*/
|
|
1334
1475
|
model?: LanguageModelV1;
|
|
1335
|
-
|
|
1476
|
+
/**
|
|
1477
|
+
* The system prompt to use for the object generation. This will override the
|
|
1478
|
+
* system prompt specified in the Agent constructor.
|
|
1479
|
+
*/
|
|
1336
1480
|
system?: string;
|
|
1481
|
+
/**
|
|
1482
|
+
* The prompt to the LLM to use for the object generation.
|
|
1483
|
+
* Specify this or messages, but not both.
|
|
1484
|
+
*/
|
|
1337
1485
|
prompt?: string;
|
|
1486
|
+
/**
|
|
1487
|
+
* The messages to use for the object generation.
|
|
1488
|
+
* Note: recent messages are automatically added based on the thread it's
|
|
1489
|
+
* associated with and your contextOptions.
|
|
1490
|
+
*/
|
|
1338
1491
|
messages?: CoreMessage[];
|
|
1339
1492
|
experimental_repairText?: RepairTextFunction;
|
|
1340
1493
|
experimental_telemetry?: TelemetrySettings;
|
|
1341
1494
|
providerOptions?: ProviderOptions;
|
|
1342
1495
|
experimental_providerMetadata?: ProviderMetadata;
|
|
1496
|
+
// Non-AI SDK args
|
|
1497
|
+
/**
|
|
1498
|
+
* The parent message id to use for the object generation.
|
|
1499
|
+
*/
|
|
1500
|
+
parentMessageId?: string;
|
|
1501
|
+
/**
|
|
1502
|
+
* The context options to use for passing in message history to the LLM.
|
|
1503
|
+
*/
|
|
1504
|
+
contextOptions?: ContextOptions;
|
|
1505
|
+
/**
|
|
1506
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1507
|
+
*/
|
|
1508
|
+
storageOptions?: StorageOptions;
|
|
1343
1509
|
};
|
|
1344
1510
|
|
|
1345
1511
|
type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
|
|
@@ -1404,7 +1570,7 @@ type ThreadOutputMetadata = GenerationOutputMetadata & {
|
|
|
1404
1570
|
messageId: string;
|
|
1405
1571
|
};
|
|
1406
1572
|
|
|
1407
|
-
interface Thread<
|
|
1573
|
+
interface Thread<DefaultTools extends ToolSet> {
|
|
1408
1574
|
/**
|
|
1409
1575
|
* The target threadId, from the startThread or continueThread initializers.
|
|
1410
1576
|
*/
|
|
@@ -1419,14 +1585,20 @@ interface Thread<AgentTools extends ToolSet> {
|
|
|
1419
1585
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
1420
1586
|
* @returns The result of the generateText function.
|
|
1421
1587
|
*/
|
|
1422
|
-
generateText<
|
|
1588
|
+
generateText<
|
|
1589
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1590
|
+
OUTPUT = never,
|
|
1591
|
+
OUTPUT_PARTIAL = never,
|
|
1592
|
+
>(
|
|
1423
1593
|
args: TextArgs<
|
|
1424
|
-
|
|
1594
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1425
1595
|
TOOLS,
|
|
1426
|
-
|
|
1596
|
+
OUTPUT,
|
|
1597
|
+
OUTPUT_PARTIAL
|
|
1427
1598
|
>
|
|
1428
1599
|
): Promise<
|
|
1429
|
-
GenerateTextResult<TOOLS
|
|
1600
|
+
GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
|
|
1601
|
+
ThreadOutputMetadata
|
|
1430
1602
|
>;
|
|
1431
1603
|
|
|
1432
1604
|
/**
|
|
@@ -1439,14 +1611,23 @@ interface Thread<AgentTools extends ToolSet> {
|
|
|
1439
1611
|
* for the {@link ContextOptions} and {@link StorageOptions}.
|
|
1440
1612
|
* @returns The result of the streamText function.
|
|
1441
1613
|
*/
|
|
1442
|
-
streamText<
|
|
1443
|
-
|
|
1444
|
-
|
|
1614
|
+
streamText<
|
|
1615
|
+
TOOLS extends ToolSet | undefined = undefined,
|
|
1616
|
+
OUTPUT = never,
|
|
1617
|
+
PARTIAL_OUTPUT = never,
|
|
1618
|
+
>(
|
|
1619
|
+
args: StreamingTextArgs<
|
|
1620
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1445
1621
|
TOOLS,
|
|
1446
|
-
|
|
1622
|
+
OUTPUT,
|
|
1623
|
+
PARTIAL_OUTPUT
|
|
1447
1624
|
>
|
|
1448
1625
|
): Promise<
|
|
1449
|
-
StreamTextResult<
|
|
1626
|
+
StreamTextResult<
|
|
1627
|
+
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
1628
|
+
PARTIAL_OUTPUT
|
|
1629
|
+
> &
|
|
1630
|
+
ThreadOutputMetadata
|
|
1450
1631
|
>;
|
|
1451
1632
|
/**
|
|
1452
1633
|
* This behaves like {@link generateObject} from the "ai" package except that
|