@convex-dev/agent 0.0.15-alpha.2 → 0.0.16-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 +93 -29
- package/dist/commonjs/client/index.d.ts +768 -71
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +81 -42
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/playground.d.ts +474 -0
- package/dist/commonjs/client/playground.d.ts.map +1 -0
- package/dist/commonjs/client/playground.js +178 -0
- package/dist/commonjs/client/playground.js.map +1 -0
- package/dist/commonjs/component/apiKeys.d.ts +11 -0
- package/dist/commonjs/component/apiKeys.d.ts.map +1 -0
- package/dist/commonjs/component/apiKeys.js +69 -0
- package/dist/commonjs/component/apiKeys.js.map +1 -0
- package/dist/commonjs/component/files.d.ts +31 -0
- package/dist/commonjs/component/files.d.ts.map +1 -0
- package/dist/commonjs/component/files.js +61 -0
- package/dist/commonjs/component/files.js.map +1 -0
- package/dist/commonjs/component/messages.d.ts +40 -109
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/messages.js +44 -260
- package/dist/commonjs/component/messages.js.map +1 -1
- package/dist/commonjs/component/schema.d.ts +54 -10
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/component/schema.js +7 -2
- package/dist/commonjs/component/schema.js.map +1 -1
- package/dist/commonjs/component/threads.d.ts +95 -0
- package/dist/commonjs/component/threads.d.ts.map +1 -0
- package/dist/commonjs/component/threads.js +151 -0
- package/dist/commonjs/component/threads.js.map +1 -0
- package/dist/commonjs/component/users.d.ts +37 -0
- package/dist/commonjs/component/users.d.ts.map +1 -0
- package/dist/commonjs/component/users.js +118 -0
- package/dist/commonjs/component/users.js.map +1 -0
- package/dist/commonjs/validators.d.ts +2 -6
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +0 -1
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +768 -71
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +81 -42
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/playground.d.ts +474 -0
- package/dist/esm/client/playground.d.ts.map +1 -0
- package/dist/esm/client/playground.js +178 -0
- package/dist/esm/client/playground.js.map +1 -0
- package/dist/esm/component/apiKeys.d.ts +11 -0
- package/dist/esm/component/apiKeys.d.ts.map +1 -0
- package/dist/esm/component/apiKeys.js +69 -0
- package/dist/esm/component/apiKeys.js.map +1 -0
- package/dist/esm/component/files.d.ts +31 -0
- package/dist/esm/component/files.d.ts.map +1 -0
- package/dist/esm/component/files.js +61 -0
- package/dist/esm/component/files.js.map +1 -0
- package/dist/esm/component/messages.d.ts +40 -109
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/messages.js +44 -260
- package/dist/esm/component/messages.js.map +1 -1
- package/dist/esm/component/schema.d.ts +54 -10
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +7 -2
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/threads.d.ts +95 -0
- package/dist/esm/component/threads.d.ts.map +1 -0
- package/dist/esm/component/threads.js +151 -0
- package/dist/esm/component/threads.js.map +1 -0
- package/dist/esm/component/users.d.ts +37 -0
- package/dist/esm/component/users.d.ts.map +1 -0
- package/dist/esm/component/users.js +118 -0
- package/dist/esm/component/users.js.map +1 -0
- package/dist/esm/validators.d.ts +2 -6
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +0 -1
- package/dist/esm/validators.js.map +1 -1
- package/package.json +10 -2
- package/src/client/index.ts +150 -117
- package/src/client/playground.ts +231 -0
- package/src/component/_generated/api.d.ts +319 -107
- package/src/component/apiKeys.ts +74 -0
- package/src/component/files.ts +72 -0
- package/src/component/messages.ts +54 -308
- package/src/component/schema.ts +7 -2
- package/src/component/threads.ts +184 -0
- package/src/component/users.ts +145 -0
- package/src/validators.ts +0 -1
package/src/client/index.ts
CHANGED
|
@@ -56,9 +56,9 @@ import {
|
|
|
56
56
|
type Usage,
|
|
57
57
|
vSafeObjectArgs,
|
|
58
58
|
vTextArgs,
|
|
59
|
+
vThreadStatus,
|
|
59
60
|
} from "../validators.js";
|
|
60
61
|
import type {
|
|
61
|
-
OpaqueIds,
|
|
62
62
|
RunActionCtx,
|
|
63
63
|
RunMutationCtx,
|
|
64
64
|
RunQueryCtx,
|
|
@@ -66,6 +66,7 @@ import type {
|
|
|
66
66
|
} from "./types.js";
|
|
67
67
|
import schema from "../component/schema.js";
|
|
68
68
|
|
|
69
|
+
export { extractText, isTool };
|
|
69
70
|
export type { Usage, ProviderMetadata };
|
|
70
71
|
export {
|
|
71
72
|
vUsage,
|
|
@@ -77,27 +78,42 @@ export {
|
|
|
77
78
|
vMessage,
|
|
78
79
|
} from "../validators.js";
|
|
79
80
|
|
|
80
|
-
export
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
export const vThreadDoc = v.object({
|
|
82
|
+
_id: v.string(),
|
|
83
|
+
_creationTime: v.number(),
|
|
84
|
+
userId: v.optional(v.string()), // Unset for anonymous
|
|
85
|
+
title: v.optional(v.string()),
|
|
86
|
+
summary: v.optional(v.string()),
|
|
87
|
+
status: vThreadStatus,
|
|
88
|
+
});
|
|
89
|
+
export type ThreadDoc = Infer<typeof vThreadDoc>;
|
|
90
|
+
|
|
91
|
+
export const vMessageDoc = v.object({
|
|
92
|
+
_id: v.string(),
|
|
93
|
+
_creationTime: v.number(),
|
|
94
|
+
...schema.tables.messages.validator.fields,
|
|
95
|
+
// Overwrite all the types that have a v.id validator
|
|
96
|
+
// Outside of the component, they are strings
|
|
97
|
+
threadId: v.string(),
|
|
98
|
+
parentMessageId: v.optional(v.string()),
|
|
99
|
+
stepId: v.optional(v.string()),
|
|
100
|
+
embeddingId: v.optional(v.string()),
|
|
101
|
+
fileId: v.optional(v.string()),
|
|
102
|
+
});
|
|
103
|
+
export type MessageDoc = Infer<typeof vMessageDoc>;
|
|
90
104
|
|
|
91
105
|
/**
|
|
92
106
|
* Options to configure what messages are fetched as context,
|
|
93
107
|
* automatically with thread.generateText, or directly via search.
|
|
94
108
|
*/
|
|
95
109
|
export type ContextOptions = {
|
|
110
|
+
/** @deprecated Use excludeToolMessages instead. */
|
|
111
|
+
includeToolCalls?: boolean;
|
|
96
112
|
/**
|
|
97
113
|
* Whether to include tool messages in the context.
|
|
98
114
|
* By default, tool calls and results are not included.
|
|
99
115
|
*/
|
|
100
|
-
|
|
116
|
+
excludeToolMessages?: boolean;
|
|
101
117
|
/**
|
|
102
118
|
* How many recent messages to include. These are added after the search
|
|
103
119
|
* messages, and do not count against the search limit.
|
|
@@ -334,9 +350,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
334
350
|
thread?: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
335
351
|
}> {
|
|
336
352
|
const threadDoc = await ctx.runMutation(
|
|
337
|
-
this.component.
|
|
353
|
+
this.component.threads.createThread,
|
|
338
354
|
{
|
|
339
|
-
defaultSystemPrompt: this.options.instructions,
|
|
340
355
|
userId: args?.userId,
|
|
341
356
|
title: args?.title,
|
|
342
357
|
summary: args?.summary,
|
|
@@ -416,7 +431,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
416
431
|
userId: string | undefined;
|
|
417
432
|
threadId: string | undefined;
|
|
418
433
|
messages: CoreMessage[];
|
|
419
|
-
|
|
434
|
+
/**
|
|
435
|
+
* If provided, it will search for messages before this message.
|
|
436
|
+
* Note: if this is far in the past, the search results may be more
|
|
437
|
+
* limited, as it's post-filtering the results.
|
|
438
|
+
*/
|
|
439
|
+
beforeMessageId?: string;
|
|
420
440
|
contextOptions: ContextOptions | undefined;
|
|
421
441
|
}
|
|
422
442
|
): Promise<CoreMessage[]> {
|
|
@@ -436,7 +456,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
436
456
|
? args.userId
|
|
437
457
|
: undefined,
|
|
438
458
|
threadId: args.threadId,
|
|
439
|
-
|
|
459
|
+
beforeMessageId: args.beforeMessageId,
|
|
440
460
|
...(await this.searchOptionsWithDefaults(opts, args.messages)),
|
|
441
461
|
}
|
|
442
462
|
);
|
|
@@ -446,15 +466,16 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
446
466
|
}
|
|
447
467
|
if (args.threadId && opts.recentMessages !== 0) {
|
|
448
468
|
const { page } = await ctx.runQuery(
|
|
449
|
-
this.component.messages.
|
|
469
|
+
this.component.messages.listMessagesByThreadId,
|
|
450
470
|
{
|
|
451
471
|
threadId: args.threadId,
|
|
452
|
-
|
|
472
|
+
excludeToolMessages:
|
|
473
|
+
opts.includeToolCalls === true ? false : opts.excludeToolMessages,
|
|
453
474
|
paginationOpts: {
|
|
454
475
|
numItems: opts.recentMessages ?? DEFAULT_RECENT_MESSAGES,
|
|
455
476
|
cursor: null,
|
|
456
477
|
},
|
|
457
|
-
|
|
478
|
+
beforeMessageId: args.beforeMessageId,
|
|
458
479
|
order: "desc",
|
|
459
480
|
statuses: ["success"],
|
|
460
481
|
}
|
|
@@ -537,13 +558,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
537
558
|
*/
|
|
538
559
|
pending?: boolean;
|
|
539
560
|
/**
|
|
540
|
-
*
|
|
541
|
-
|
|
542
|
-
parentMessageId?: string;
|
|
543
|
-
/**
|
|
544
|
-
* Whether to mark all pending messages in the thread as failed.
|
|
545
|
-
* This is used to recover from a failure via a retry that wipes the slate clean.
|
|
546
|
-
* Defaults to true.
|
|
561
|
+
* If true, it will fail any pending steps.
|
|
562
|
+
* Defaults to false.
|
|
547
563
|
*/
|
|
548
564
|
failPendingSteps?: boolean;
|
|
549
565
|
}
|
|
@@ -568,9 +584,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
568
584
|
message: serializeMessage(m),
|
|
569
585
|
}) as MessageWithMetadata
|
|
570
586
|
),
|
|
571
|
-
failPendingSteps: args.failPendingSteps ??
|
|
587
|
+
failPendingSteps: args.failPendingSteps ?? false,
|
|
572
588
|
pending: args.pending ?? false,
|
|
573
|
-
parentMessageId: args.parentMessageId,
|
|
574
589
|
});
|
|
575
590
|
return {
|
|
576
591
|
lastMessageId: result.messages.at(-1)!._id,
|
|
@@ -591,7 +606,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
591
606
|
/**
|
|
592
607
|
* The message this step is in response to.
|
|
593
608
|
*/
|
|
594
|
-
|
|
609
|
+
parentMessageId: string;
|
|
595
610
|
/**
|
|
596
611
|
* The step to save, possibly including multiple tool calls.
|
|
597
612
|
*/
|
|
@@ -628,7 +643,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
628
643
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
629
644
|
userId: args.userId,
|
|
630
645
|
threadId: args.threadId,
|
|
631
|
-
|
|
646
|
+
parentMessageId: args.parentMessageId,
|
|
632
647
|
step: { step, messages },
|
|
633
648
|
failPendingSteps: false,
|
|
634
649
|
});
|
|
@@ -699,14 +714,16 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
699
714
|
*/
|
|
700
715
|
tools?: ToolSet;
|
|
701
716
|
},
|
|
702
|
-
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL
|
|
717
|
+
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL>,
|
|
718
|
+
options?: Options
|
|
703
719
|
): Promise<
|
|
704
720
|
GenerateTextResult<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT> &
|
|
705
721
|
GenerationOutputMetadata
|
|
706
722
|
> {
|
|
707
723
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
708
724
|
ctx,
|
|
709
|
-
|
|
725
|
+
args,
|
|
726
|
+
{ userId, threadId, ...options }
|
|
710
727
|
);
|
|
711
728
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
712
729
|
const tools = wrapTools(
|
|
@@ -714,6 +731,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
714
731
|
args.tools ?? threadTools ?? this.options.tools
|
|
715
732
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
716
733
|
const saveOutputMessages =
|
|
734
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
717
735
|
args.saveOutputMessages ??
|
|
718
736
|
this.options.storageOptions?.saveOutputMessages;
|
|
719
737
|
const model = aiArgs.model ?? this.options.chat;
|
|
@@ -731,7 +749,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
731
749
|
await this.saveStep(ctx, {
|
|
732
750
|
userId,
|
|
733
751
|
threadId,
|
|
734
|
-
messageId,
|
|
752
|
+
parentMessageId: messageId,
|
|
735
753
|
step,
|
|
736
754
|
});
|
|
737
755
|
}
|
|
@@ -800,7 +818,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
800
818
|
usageHandler?: UsageHandler;
|
|
801
819
|
tools?: ToolSet;
|
|
802
820
|
},
|
|
803
|
-
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT
|
|
821
|
+
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT>,
|
|
822
|
+
options?: Options
|
|
804
823
|
): Promise<
|
|
805
824
|
StreamTextResult<
|
|
806
825
|
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
@@ -810,7 +829,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
810
829
|
> {
|
|
811
830
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
812
831
|
ctx,
|
|
813
|
-
|
|
832
|
+
args,
|
|
833
|
+
{ userId, threadId, ...options }
|
|
814
834
|
);
|
|
815
835
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
816
836
|
const tools = wrapTools(
|
|
@@ -818,6 +838,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
818
838
|
args.tools ?? threadTools ?? this.options.tools
|
|
819
839
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
820
840
|
const saveOutputMessages =
|
|
841
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
821
842
|
args.saveOutputMessages ??
|
|
822
843
|
this.options.storageOptions?.saveOutputMessages;
|
|
823
844
|
const model = aiArgs.model ?? this.options.chat;
|
|
@@ -850,7 +871,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
850
871
|
await this.saveStep(ctx, {
|
|
851
872
|
userId,
|
|
852
873
|
threadId,
|
|
853
|
-
messageId,
|
|
874
|
+
parentMessageId: messageId,
|
|
854
875
|
step,
|
|
855
876
|
});
|
|
856
877
|
}
|
|
@@ -885,34 +906,27 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
885
906
|
},
|
|
886
907
|
>(
|
|
887
908
|
ctx: RunActionCtx | RunMutationCtx,
|
|
909
|
+
args: T,
|
|
888
910
|
{
|
|
889
|
-
id,
|
|
890
911
|
userId,
|
|
891
912
|
threadId,
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
...args
|
|
913
|
+
contextOptions,
|
|
914
|
+
storageOptions,
|
|
895
915
|
}: {
|
|
896
916
|
userId: string | undefined;
|
|
897
917
|
threadId: string | undefined;
|
|
898
|
-
|
|
899
|
-
contextOptions?: ContextOptions;
|
|
900
|
-
storageOptions?: StorageOptions;
|
|
901
|
-
} & T
|
|
918
|
+
} & Options
|
|
902
919
|
): Promise<{
|
|
903
920
|
args: T;
|
|
904
921
|
messageId: string | undefined;
|
|
905
922
|
}> {
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
const storageOptions: StorageOptions | Record<string, unknown> =
|
|
909
|
-
args.storageOptions ?? this.options.storageOptions ?? args;
|
|
923
|
+
contextOptions ||= this.options.contextOptions ?? (args as ContextOptions);
|
|
924
|
+
storageOptions ||= this.options.storageOptions ?? (args as StorageOptions);
|
|
910
925
|
const messages = promptOrMessagesToCoreMessages(args);
|
|
911
926
|
const contextMessages = await this.fetchContextMessages(ctx, {
|
|
912
927
|
userId,
|
|
913
928
|
threadId,
|
|
914
929
|
messages,
|
|
915
|
-
parentMessageId,
|
|
916
930
|
contextOptions,
|
|
917
931
|
});
|
|
918
932
|
let messageId: string | undefined;
|
|
@@ -923,11 +937,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
923
937
|
threadId,
|
|
924
938
|
userId,
|
|
925
939
|
messages: coreMessages,
|
|
926
|
-
metadata: coreMessages.length === 1 ? [{ id }] : undefined,
|
|
940
|
+
metadata: coreMessages.length === 1 ? [{ id: args.id }] : undefined,
|
|
927
941
|
pending: true,
|
|
928
|
-
|
|
929
|
-
// failPendingSteps: true,
|
|
930
|
-
parentMessageId,
|
|
942
|
+
failPendingSteps: true,
|
|
931
943
|
});
|
|
932
944
|
messageId = saved.lastMessageId;
|
|
933
945
|
}
|
|
@@ -935,7 +947,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
935
947
|
return {
|
|
936
948
|
args: {
|
|
937
949
|
...rest,
|
|
938
|
-
system: system ?? this.options.instructions,
|
|
950
|
+
system: args.system ?? this.options.instructions,
|
|
939
951
|
messages: [...contextMessages, ...messages],
|
|
940
952
|
} as T,
|
|
941
953
|
messageId,
|
|
@@ -961,15 +973,18 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
961
973
|
threadId,
|
|
962
974
|
usageHandler,
|
|
963
975
|
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
964
|
-
args: OurObjectArgs<T
|
|
976
|
+
args: OurObjectArgs<T>,
|
|
977
|
+
options?: Options
|
|
965
978
|
): Promise<GenerateObjectResult<T> & GenerationOutputMetadata> {
|
|
966
979
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
967
980
|
ctx,
|
|
968
|
-
|
|
981
|
+
args,
|
|
982
|
+
{ userId, threadId, ...options }
|
|
969
983
|
);
|
|
970
984
|
const model = aiArgs.model ?? this.options.chat;
|
|
971
985
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
972
986
|
const saveOutputMessages =
|
|
987
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
973
988
|
args.saveOutputMessages ??
|
|
974
989
|
this.options.storageOptions?.saveOutputMessages;
|
|
975
990
|
try {
|
|
@@ -982,7 +997,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
982
997
|
} as any)) as GenerateObjectResult<T> & GenerationOutputMetadata;
|
|
983
998
|
|
|
984
999
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
985
|
-
await this.saveObject(ctx, {
|
|
1000
|
+
await this.saveObject(ctx, {
|
|
1001
|
+
threadId,
|
|
1002
|
+
parentMessageId: messageId,
|
|
1003
|
+
result,
|
|
1004
|
+
userId,
|
|
1005
|
+
});
|
|
986
1006
|
}
|
|
987
1007
|
result.messageId = messageId;
|
|
988
1008
|
if (trackUsage && result.usage) {
|
|
@@ -1027,18 +1047,21 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1027
1047
|
threadId,
|
|
1028
1048
|
usageHandler,
|
|
1029
1049
|
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
1030
|
-
args: OurStreamObjectArgs<T
|
|
1050
|
+
args: OurStreamObjectArgs<T>,
|
|
1051
|
+
options?: Options
|
|
1031
1052
|
): Promise<
|
|
1032
1053
|
StreamObjectResult<DeepPartial<T>, T, never> & GenerationOutputMetadata
|
|
1033
1054
|
> {
|
|
1034
1055
|
// TODO: unify all this shared code between all the generate* and stream* functions
|
|
1035
1056
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
1036
1057
|
ctx,
|
|
1037
|
-
|
|
1058
|
+
args,
|
|
1059
|
+
{ userId, threadId, ...options }
|
|
1038
1060
|
);
|
|
1039
1061
|
const model = aiArgs.model ?? this.options.chat;
|
|
1040
1062
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
1041
1063
|
const saveOutputMessages =
|
|
1064
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
1042
1065
|
args.saveOutputMessages ??
|
|
1043
1066
|
this.options.storageOptions?.saveOutputMessages;
|
|
1044
1067
|
const stream = streamObject<T>({
|
|
@@ -1056,7 +1079,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1056
1079
|
await this.saveObject(ctx, {
|
|
1057
1080
|
userId,
|
|
1058
1081
|
threadId,
|
|
1059
|
-
messageId,
|
|
1082
|
+
parentMessageId: messageId,
|
|
1060
1083
|
result: {
|
|
1061
1084
|
object: result.object,
|
|
1062
1085
|
finishReason: "stop",
|
|
@@ -1104,7 +1127,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1104
1127
|
args: {
|
|
1105
1128
|
userId: string | undefined;
|
|
1106
1129
|
threadId: string;
|
|
1107
|
-
|
|
1130
|
+
parentMessageId: string;
|
|
1108
1131
|
result: GenerateObjectResult<unknown>;
|
|
1109
1132
|
metadata?: Omit<MessageWithMetadata, "message">;
|
|
1110
1133
|
}
|
|
@@ -1133,7 +1156,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1133
1156
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
1134
1157
|
userId: args.userId,
|
|
1135
1158
|
threadId: args.threadId,
|
|
1136
|
-
|
|
1159
|
+
parentMessageId: args.parentMessageId,
|
|
1137
1160
|
failPendingSteps: false,
|
|
1138
1161
|
step: { step, messages },
|
|
1139
1162
|
});
|
|
@@ -1226,18 +1249,30 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1226
1249
|
* @param spec Configuration for the agent acting as an action, including
|
|
1227
1250
|
* {@link ContextOptions} and maxSteps.
|
|
1228
1251
|
*/
|
|
1229
|
-
asTextAction(spec?: {
|
|
1252
|
+
asTextAction(spec?: {
|
|
1253
|
+
contextOptions?: ContextOptions;
|
|
1254
|
+
maxSteps?: number;
|
|
1255
|
+
storageOptions?: StorageOptions;
|
|
1256
|
+
}) {
|
|
1230
1257
|
const maxSteps = spec?.maxSteps ?? this.options.maxSteps;
|
|
1231
|
-
const contextOptions =
|
|
1232
|
-
spec?.contextOptions && this.mergedContextOptions(spec.contextOptions);
|
|
1233
|
-
|
|
1234
1258
|
return internalActionGeneric({
|
|
1235
1259
|
args: vTextArgs,
|
|
1236
1260
|
handler: async (ctx, args) => {
|
|
1261
|
+
const { contextOptions, storageOptions, ...rest } = args;
|
|
1237
1262
|
const value = await this.generateText(
|
|
1238
1263
|
ctx,
|
|
1239
1264
|
{ userId: args.userId, threadId: args.threadId },
|
|
1240
|
-
{ maxSteps, ...
|
|
1265
|
+
{ maxSteps, ...rest },
|
|
1266
|
+
{
|
|
1267
|
+
contextOptions:
|
|
1268
|
+
contextOptions ??
|
|
1269
|
+
spec?.contextOptions ??
|
|
1270
|
+
this.options.contextOptions,
|
|
1271
|
+
storageOptions:
|
|
1272
|
+
storageOptions ??
|
|
1273
|
+
spec?.storageOptions ??
|
|
1274
|
+
this.options.storageOptions,
|
|
1275
|
+
}
|
|
1241
1276
|
);
|
|
1242
1277
|
return value.text;
|
|
1243
1278
|
},
|
|
@@ -1250,21 +1285,36 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1250
1285
|
* the normal parameters to {@link generateObject}, plus {@link ContextOptions}
|
|
1251
1286
|
* and maxSteps.
|
|
1252
1287
|
*/
|
|
1253
|
-
asObjectAction<T>(
|
|
1288
|
+
asObjectAction<T>(
|
|
1289
|
+
spec: OurObjectArgs<T> & { maxSteps?: number },
|
|
1290
|
+
options?: {
|
|
1291
|
+
contextOptions?: ContextOptions;
|
|
1292
|
+
storageOptions?: StorageOptions;
|
|
1293
|
+
}
|
|
1294
|
+
) {
|
|
1254
1295
|
const maxSteps = spec?.maxSteps ?? this.options.maxSteps;
|
|
1255
1296
|
return internalActionGeneric({
|
|
1256
1297
|
args: vSafeObjectArgs,
|
|
1257
1298
|
handler: async (ctx, args) => {
|
|
1299
|
+
const { contextOptions, storageOptions, ...rest } = args;
|
|
1258
1300
|
const value = await this.generateObject(
|
|
1259
1301
|
ctx,
|
|
1260
1302
|
{ userId: args.userId, threadId: args.threadId },
|
|
1261
1303
|
{
|
|
1262
1304
|
...spec,
|
|
1263
1305
|
maxSteps,
|
|
1264
|
-
...
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1306
|
+
...rest,
|
|
1307
|
+
} as unknown as OurObjectArgs<unknown>,
|
|
1308
|
+
{
|
|
1309
|
+
contextOptions:
|
|
1310
|
+
contextOptions ??
|
|
1311
|
+
options?.contextOptions ??
|
|
1312
|
+
this.options.contextOptions,
|
|
1313
|
+
storageOptions:
|
|
1314
|
+
storageOptions ??
|
|
1315
|
+
options?.storageOptions ??
|
|
1316
|
+
this.options.storageOptions,
|
|
1317
|
+
}
|
|
1268
1318
|
);
|
|
1269
1319
|
return value.object as T;
|
|
1270
1320
|
},
|
|
@@ -1372,6 +1422,17 @@ function wrapTools(
|
|
|
1372
1422
|
return output;
|
|
1373
1423
|
}
|
|
1374
1424
|
|
|
1425
|
+
type Options = {
|
|
1426
|
+
/**
|
|
1427
|
+
* The context options to use for passing in message history to the LLM.
|
|
1428
|
+
*/
|
|
1429
|
+
contextOptions?: ContextOptions;
|
|
1430
|
+
/**
|
|
1431
|
+
* The storage options to use for saving the input and output messages to the thread.
|
|
1432
|
+
*/
|
|
1433
|
+
storageOptions?: StorageOptions;
|
|
1434
|
+
};
|
|
1435
|
+
|
|
1375
1436
|
type TextArgs<
|
|
1376
1437
|
AgentTools extends ToolSet,
|
|
1377
1438
|
TOOLS extends ToolSet | undefined = undefined,
|
|
@@ -1402,20 +1463,7 @@ type TextArgs<
|
|
|
1402
1463
|
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1403
1464
|
*/
|
|
1404
1465
|
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1405
|
-
|
|
1406
|
-
/**
|
|
1407
|
-
* The parent message id to use for the tool calls.
|
|
1408
|
-
*/
|
|
1409
|
-
parentMessageId?: string;
|
|
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 &
|
|
1466
|
+
} & ContextOptions & // DEPRECATED: pass them in the subsequent parameter instead
|
|
1419
1467
|
StorageOptions;
|
|
1420
1468
|
|
|
1421
1469
|
type StreamingTextArgs<
|
|
@@ -1448,19 +1496,6 @@ type StreamingTextArgs<
|
|
|
1448
1496
|
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1449
1497
|
*/
|
|
1450
1498
|
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;
|
|
1464
1499
|
} & ContextOptions &
|
|
1465
1500
|
StorageOptions;
|
|
1466
1501
|
|
|
@@ -1492,19 +1527,6 @@ type BaseGenerateObjectOptions = StorageOptions &
|
|
|
1492
1527
|
experimental_telemetry?: TelemetrySettings;
|
|
1493
1528
|
providerOptions?: ProviderOptions;
|
|
1494
1529
|
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;
|
|
1508
1530
|
};
|
|
1509
1531
|
|
|
1510
1532
|
type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
|
|
@@ -1536,6 +1558,8 @@ type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
|
|
|
1536
1558
|
mode?: "json";
|
|
1537
1559
|
};
|
|
1538
1560
|
|
|
1561
|
+
// TODO: simplify this to just use the generateObject args, with an optional
|
|
1562
|
+
// model and tool/toolChoice types
|
|
1539
1563
|
type GenerateObjectArgs<T> =
|
|
1540
1564
|
T extends Record<string, unknown>
|
|
1541
1565
|
? GenerateObjectObjectOptions<T>
|
|
@@ -1569,6 +1593,10 @@ type ThreadOutputMetadata = GenerationOutputMetadata & {
|
|
|
1569
1593
|
messageId: string;
|
|
1570
1594
|
};
|
|
1571
1595
|
|
|
1596
|
+
/**
|
|
1597
|
+
* The interface for a thread returned from {@link createThread} or {@link continueThread}.
|
|
1598
|
+
* This is contextual to a thread and/or user.
|
|
1599
|
+
*/
|
|
1572
1600
|
interface Thread<DefaultTools extends ToolSet> {
|
|
1573
1601
|
/**
|
|
1574
1602
|
* The target threadId, from the startThread or continueThread initializers.
|
|
@@ -1594,7 +1622,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1594
1622
|
TOOLS,
|
|
1595
1623
|
OUTPUT,
|
|
1596
1624
|
OUTPUT_PARTIAL
|
|
1597
|
-
|
|
1625
|
+
>,
|
|
1626
|
+
options?: Options
|
|
1598
1627
|
): Promise<
|
|
1599
1628
|
GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
|
|
1600
1629
|
ThreadOutputMetadata
|
|
@@ -1620,7 +1649,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1620
1649
|
TOOLS,
|
|
1621
1650
|
OUTPUT,
|
|
1622
1651
|
PARTIAL_OUTPUT
|
|
1623
|
-
|
|
1652
|
+
>,
|
|
1653
|
+
options?: Options
|
|
1624
1654
|
): Promise<
|
|
1625
1655
|
StreamTextResult<
|
|
1626
1656
|
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
@@ -1639,7 +1669,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1639
1669
|
* @returns The result of the generateObject function.
|
|
1640
1670
|
*/
|
|
1641
1671
|
generateObject<T>(
|
|
1642
|
-
args: OurObjectArgs<T
|
|
1672
|
+
args: OurObjectArgs<T>,
|
|
1673
|
+
options?: Options
|
|
1643
1674
|
): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
|
|
1644
1675
|
/**
|
|
1645
1676
|
* This behaves like {@link generateObject} from the "ai" package except that
|
|
@@ -1652,7 +1683,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1652
1683
|
* @returns The result of the generateObject function.
|
|
1653
1684
|
*/
|
|
1654
1685
|
generateObject(
|
|
1655
|
-
args: GenerateObjectNoSchemaOptions
|
|
1686
|
+
args: GenerateObjectNoSchemaOptions,
|
|
1687
|
+
options?: Options
|
|
1656
1688
|
): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
|
|
1657
1689
|
/**
|
|
1658
1690
|
* This behaves like {@link streamObject} from the "ai" package except that
|
|
@@ -1665,7 +1697,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1665
1697
|
* @returns The result of the streamObject function.
|
|
1666
1698
|
*/
|
|
1667
1699
|
streamObject<T>(
|
|
1668
|
-
args: OurStreamObjectArgs<T
|
|
1700
|
+
args: OurStreamObjectArgs<T>,
|
|
1701
|
+
options?: Options
|
|
1669
1702
|
): Promise<
|
|
1670
1703
|
StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata
|
|
1671
1704
|
>;
|