@convex-dev/agent 0.0.15-alpha.1 → 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 +771 -74
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +82 -45
- 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 +771 -74
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +82 -45
- 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 +152 -120
- 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
|
@@ -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 { Mounts } from "../component/_generated/api.js";
|
|
31
31
|
import {
|
|
32
32
|
validateVectorDimension,
|
|
33
33
|
type VectorDimension,
|
|
@@ -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.
|
|
@@ -174,8 +190,7 @@ export type UsageHandler = (
|
|
|
174
190
|
|
|
175
191
|
export class Agent<AgentTools extends ToolSet> {
|
|
176
192
|
constructor(
|
|
177
|
-
|
|
178
|
-
public component: UseApi<typeof api>,
|
|
193
|
+
public component: UseApi<Mounts>,
|
|
179
194
|
public options: {
|
|
180
195
|
/**
|
|
181
196
|
* The name for the agent. This will be attributed on each message
|
|
@@ -335,9 +350,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
335
350
|
thread?: Thread<ThreadTools extends undefined ? AgentTools : ThreadTools>;
|
|
336
351
|
}> {
|
|
337
352
|
const threadDoc = await ctx.runMutation(
|
|
338
|
-
this.component.
|
|
353
|
+
this.component.threads.createThread,
|
|
339
354
|
{
|
|
340
|
-
defaultSystemPrompt: this.options.instructions,
|
|
341
355
|
userId: args?.userId,
|
|
342
356
|
title: args?.title,
|
|
343
357
|
summary: args?.summary,
|
|
@@ -417,7 +431,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
417
431
|
userId: string | undefined;
|
|
418
432
|
threadId: string | undefined;
|
|
419
433
|
messages: CoreMessage[];
|
|
420
|
-
|
|
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;
|
|
421
440
|
contextOptions: ContextOptions | undefined;
|
|
422
441
|
}
|
|
423
442
|
): Promise<CoreMessage[]> {
|
|
@@ -437,7 +456,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
437
456
|
? args.userId
|
|
438
457
|
: undefined,
|
|
439
458
|
threadId: args.threadId,
|
|
440
|
-
|
|
459
|
+
beforeMessageId: args.beforeMessageId,
|
|
441
460
|
...(await this.searchOptionsWithDefaults(opts, args.messages)),
|
|
442
461
|
}
|
|
443
462
|
);
|
|
@@ -447,15 +466,16 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
447
466
|
}
|
|
448
467
|
if (args.threadId && opts.recentMessages !== 0) {
|
|
449
468
|
const { page } = await ctx.runQuery(
|
|
450
|
-
this.component.messages.
|
|
469
|
+
this.component.messages.listMessagesByThreadId,
|
|
451
470
|
{
|
|
452
471
|
threadId: args.threadId,
|
|
453
|
-
|
|
472
|
+
excludeToolMessages:
|
|
473
|
+
opts.includeToolCalls === true ? false : opts.excludeToolMessages,
|
|
454
474
|
paginationOpts: {
|
|
455
475
|
numItems: opts.recentMessages ?? DEFAULT_RECENT_MESSAGES,
|
|
456
476
|
cursor: null,
|
|
457
477
|
},
|
|
458
|
-
|
|
478
|
+
beforeMessageId: args.beforeMessageId,
|
|
459
479
|
order: "desc",
|
|
460
480
|
statuses: ["success"],
|
|
461
481
|
}
|
|
@@ -538,13 +558,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
538
558
|
*/
|
|
539
559
|
pending?: boolean;
|
|
540
560
|
/**
|
|
541
|
-
*
|
|
542
|
-
|
|
543
|
-
parentMessageId?: string;
|
|
544
|
-
/**
|
|
545
|
-
* Whether to mark all pending messages in the thread as failed.
|
|
546
|
-
* This is used to recover from a failure via a retry that wipes the slate clean.
|
|
547
|
-
* Defaults to true.
|
|
561
|
+
* If true, it will fail any pending steps.
|
|
562
|
+
* Defaults to false.
|
|
548
563
|
*/
|
|
549
564
|
failPendingSteps?: boolean;
|
|
550
565
|
}
|
|
@@ -569,9 +584,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
569
584
|
message: serializeMessage(m),
|
|
570
585
|
}) as MessageWithMetadata
|
|
571
586
|
),
|
|
572
|
-
failPendingSteps: args.failPendingSteps ??
|
|
587
|
+
failPendingSteps: args.failPendingSteps ?? false,
|
|
573
588
|
pending: args.pending ?? false,
|
|
574
|
-
parentMessageId: args.parentMessageId,
|
|
575
589
|
});
|
|
576
590
|
return {
|
|
577
591
|
lastMessageId: result.messages.at(-1)!._id,
|
|
@@ -592,7 +606,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
592
606
|
/**
|
|
593
607
|
* The message this step is in response to.
|
|
594
608
|
*/
|
|
595
|
-
|
|
609
|
+
parentMessageId: string;
|
|
596
610
|
/**
|
|
597
611
|
* The step to save, possibly including multiple tool calls.
|
|
598
612
|
*/
|
|
@@ -629,7 +643,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
629
643
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
630
644
|
userId: args.userId,
|
|
631
645
|
threadId: args.threadId,
|
|
632
|
-
|
|
646
|
+
parentMessageId: args.parentMessageId,
|
|
633
647
|
step: { step, messages },
|
|
634
648
|
failPendingSteps: false,
|
|
635
649
|
});
|
|
@@ -700,14 +714,16 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
700
714
|
*/
|
|
701
715
|
tools?: ToolSet;
|
|
702
716
|
},
|
|
703
|
-
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL
|
|
717
|
+
args: TextArgs<AgentTools, TOOLS, OUTPUT, OUTPUT_PARTIAL>,
|
|
718
|
+
options?: Options
|
|
704
719
|
): Promise<
|
|
705
720
|
GenerateTextResult<TOOLS extends undefined ? AgentTools : TOOLS, OUTPUT> &
|
|
706
721
|
GenerationOutputMetadata
|
|
707
722
|
> {
|
|
708
723
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
709
724
|
ctx,
|
|
710
|
-
|
|
725
|
+
args,
|
|
726
|
+
{ userId, threadId, ...options }
|
|
711
727
|
);
|
|
712
728
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
713
729
|
const tools = wrapTools(
|
|
@@ -715,6 +731,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
715
731
|
args.tools ?? threadTools ?? this.options.tools
|
|
716
732
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
717
733
|
const saveOutputMessages =
|
|
734
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
718
735
|
args.saveOutputMessages ??
|
|
719
736
|
this.options.storageOptions?.saveOutputMessages;
|
|
720
737
|
const model = aiArgs.model ?? this.options.chat;
|
|
@@ -732,7 +749,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
732
749
|
await this.saveStep(ctx, {
|
|
733
750
|
userId,
|
|
734
751
|
threadId,
|
|
735
|
-
messageId,
|
|
752
|
+
parentMessageId: messageId,
|
|
736
753
|
step,
|
|
737
754
|
});
|
|
738
755
|
}
|
|
@@ -801,7 +818,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
801
818
|
usageHandler?: UsageHandler;
|
|
802
819
|
tools?: ToolSet;
|
|
803
820
|
},
|
|
804
|
-
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT
|
|
821
|
+
args: StreamingTextArgs<AgentTools, TOOLS, OUTPUT, PARTIAL_OUTPUT>,
|
|
822
|
+
options?: Options
|
|
805
823
|
): Promise<
|
|
806
824
|
StreamTextResult<
|
|
807
825
|
TOOLS extends undefined ? AgentTools : TOOLS,
|
|
@@ -811,7 +829,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
811
829
|
> {
|
|
812
830
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
813
831
|
ctx,
|
|
814
|
-
|
|
832
|
+
args,
|
|
833
|
+
{ userId, threadId, ...options }
|
|
815
834
|
);
|
|
816
835
|
const toolCtx = { ...ctx, userId, threadId, messageId };
|
|
817
836
|
const tools = wrapTools(
|
|
@@ -819,6 +838,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
819
838
|
args.tools ?? threadTools ?? this.options.tools
|
|
820
839
|
) as TOOLS extends undefined ? AgentTools : TOOLS;
|
|
821
840
|
const saveOutputMessages =
|
|
841
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
822
842
|
args.saveOutputMessages ??
|
|
823
843
|
this.options.storageOptions?.saveOutputMessages;
|
|
824
844
|
const model = aiArgs.model ?? this.options.chat;
|
|
@@ -851,7 +871,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
851
871
|
await this.saveStep(ctx, {
|
|
852
872
|
userId,
|
|
853
873
|
threadId,
|
|
854
|
-
messageId,
|
|
874
|
+
parentMessageId: messageId,
|
|
855
875
|
step,
|
|
856
876
|
});
|
|
857
877
|
}
|
|
@@ -886,34 +906,27 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
886
906
|
},
|
|
887
907
|
>(
|
|
888
908
|
ctx: RunActionCtx | RunMutationCtx,
|
|
909
|
+
args: T,
|
|
889
910
|
{
|
|
890
|
-
id,
|
|
891
911
|
userId,
|
|
892
912
|
threadId,
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
...args
|
|
913
|
+
contextOptions,
|
|
914
|
+
storageOptions,
|
|
896
915
|
}: {
|
|
897
916
|
userId: string | undefined;
|
|
898
917
|
threadId: string | undefined;
|
|
899
|
-
|
|
900
|
-
contextOptions?: ContextOptions;
|
|
901
|
-
storageOptions?: StorageOptions;
|
|
902
|
-
} & T
|
|
918
|
+
} & Options
|
|
903
919
|
): Promise<{
|
|
904
920
|
args: T;
|
|
905
921
|
messageId: string | undefined;
|
|
906
922
|
}> {
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
const storageOptions: StorageOptions | Record<string, unknown> =
|
|
910
|
-
args.storageOptions ?? this.options.storageOptions ?? args;
|
|
923
|
+
contextOptions ||= this.options.contextOptions ?? (args as ContextOptions);
|
|
924
|
+
storageOptions ||= this.options.storageOptions ?? (args as StorageOptions);
|
|
911
925
|
const messages = promptOrMessagesToCoreMessages(args);
|
|
912
926
|
const contextMessages = await this.fetchContextMessages(ctx, {
|
|
913
927
|
userId,
|
|
914
928
|
threadId,
|
|
915
929
|
messages,
|
|
916
|
-
parentMessageId,
|
|
917
930
|
contextOptions,
|
|
918
931
|
});
|
|
919
932
|
let messageId: string | undefined;
|
|
@@ -924,11 +937,9 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
924
937
|
threadId,
|
|
925
938
|
userId,
|
|
926
939
|
messages: coreMessages,
|
|
927
|
-
metadata: coreMessages.length === 1 ? [{ id }] : undefined,
|
|
940
|
+
metadata: coreMessages.length === 1 ? [{ id: args.id }] : undefined,
|
|
928
941
|
pending: true,
|
|
929
|
-
|
|
930
|
-
// failPendingSteps: true,
|
|
931
|
-
parentMessageId,
|
|
942
|
+
failPendingSteps: true,
|
|
932
943
|
});
|
|
933
944
|
messageId = saved.lastMessageId;
|
|
934
945
|
}
|
|
@@ -936,7 +947,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
936
947
|
return {
|
|
937
948
|
args: {
|
|
938
949
|
...rest,
|
|
939
|
-
system: system ?? this.options.instructions,
|
|
950
|
+
system: args.system ?? this.options.instructions,
|
|
940
951
|
messages: [...contextMessages, ...messages],
|
|
941
952
|
} as T,
|
|
942
953
|
messageId,
|
|
@@ -962,15 +973,18 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
962
973
|
threadId,
|
|
963
974
|
usageHandler,
|
|
964
975
|
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
965
|
-
args: OurObjectArgs<T
|
|
976
|
+
args: OurObjectArgs<T>,
|
|
977
|
+
options?: Options
|
|
966
978
|
): Promise<GenerateObjectResult<T> & GenerationOutputMetadata> {
|
|
967
979
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
968
980
|
ctx,
|
|
969
|
-
|
|
981
|
+
args,
|
|
982
|
+
{ userId, threadId, ...options }
|
|
970
983
|
);
|
|
971
984
|
const model = aiArgs.model ?? this.options.chat;
|
|
972
985
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
973
986
|
const saveOutputMessages =
|
|
987
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
974
988
|
args.saveOutputMessages ??
|
|
975
989
|
this.options.storageOptions?.saveOutputMessages;
|
|
976
990
|
try {
|
|
@@ -983,7 +997,12 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
983
997
|
} as any)) as GenerateObjectResult<T> & GenerationOutputMetadata;
|
|
984
998
|
|
|
985
999
|
if (threadId && messageId && saveOutputMessages !== false) {
|
|
986
|
-
await this.saveObject(ctx, {
|
|
1000
|
+
await this.saveObject(ctx, {
|
|
1001
|
+
threadId,
|
|
1002
|
+
parentMessageId: messageId,
|
|
1003
|
+
result,
|
|
1004
|
+
userId,
|
|
1005
|
+
});
|
|
987
1006
|
}
|
|
988
1007
|
result.messageId = messageId;
|
|
989
1008
|
if (trackUsage && result.usage) {
|
|
@@ -1028,18 +1047,21 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1028
1047
|
threadId,
|
|
1029
1048
|
usageHandler,
|
|
1030
1049
|
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
1031
|
-
args: OurStreamObjectArgs<T
|
|
1050
|
+
args: OurStreamObjectArgs<T>,
|
|
1051
|
+
options?: Options
|
|
1032
1052
|
): Promise<
|
|
1033
1053
|
StreamObjectResult<DeepPartial<T>, T, never> & GenerationOutputMetadata
|
|
1034
1054
|
> {
|
|
1035
1055
|
// TODO: unify all this shared code between all the generate* and stream* functions
|
|
1036
1056
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
1037
1057
|
ctx,
|
|
1038
|
-
|
|
1058
|
+
args,
|
|
1059
|
+
{ userId, threadId, ...options }
|
|
1039
1060
|
);
|
|
1040
1061
|
const model = aiArgs.model ?? this.options.chat;
|
|
1041
1062
|
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
1042
1063
|
const saveOutputMessages =
|
|
1064
|
+
options?.storageOptions?.saveOutputMessages ??
|
|
1043
1065
|
args.saveOutputMessages ??
|
|
1044
1066
|
this.options.storageOptions?.saveOutputMessages;
|
|
1045
1067
|
const stream = streamObject<T>({
|
|
@@ -1057,7 +1079,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1057
1079
|
await this.saveObject(ctx, {
|
|
1058
1080
|
userId,
|
|
1059
1081
|
threadId,
|
|
1060
|
-
messageId,
|
|
1082
|
+
parentMessageId: messageId,
|
|
1061
1083
|
result: {
|
|
1062
1084
|
object: result.object,
|
|
1063
1085
|
finishReason: "stop",
|
|
@@ -1105,7 +1127,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1105
1127
|
args: {
|
|
1106
1128
|
userId: string | undefined;
|
|
1107
1129
|
threadId: string;
|
|
1108
|
-
|
|
1130
|
+
parentMessageId: string;
|
|
1109
1131
|
result: GenerateObjectResult<unknown>;
|
|
1110
1132
|
metadata?: Omit<MessageWithMetadata, "message">;
|
|
1111
1133
|
}
|
|
@@ -1134,7 +1156,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1134
1156
|
await ctx.runMutation(this.component.messages.addStep, {
|
|
1135
1157
|
userId: args.userId,
|
|
1136
1158
|
threadId: args.threadId,
|
|
1137
|
-
|
|
1159
|
+
parentMessageId: args.parentMessageId,
|
|
1138
1160
|
failPendingSteps: false,
|
|
1139
1161
|
step: { step, messages },
|
|
1140
1162
|
});
|
|
@@ -1227,18 +1249,30 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1227
1249
|
* @param spec Configuration for the agent acting as an action, including
|
|
1228
1250
|
* {@link ContextOptions} and maxSteps.
|
|
1229
1251
|
*/
|
|
1230
|
-
asTextAction(spec?: {
|
|
1252
|
+
asTextAction(spec?: {
|
|
1253
|
+
contextOptions?: ContextOptions;
|
|
1254
|
+
maxSteps?: number;
|
|
1255
|
+
storageOptions?: StorageOptions;
|
|
1256
|
+
}) {
|
|
1231
1257
|
const maxSteps = spec?.maxSteps ?? this.options.maxSteps;
|
|
1232
|
-
const contextOptions =
|
|
1233
|
-
spec?.contextOptions && this.mergedContextOptions(spec.contextOptions);
|
|
1234
|
-
|
|
1235
1258
|
return internalActionGeneric({
|
|
1236
1259
|
args: vTextArgs,
|
|
1237
1260
|
handler: async (ctx, args) => {
|
|
1261
|
+
const { contextOptions, storageOptions, ...rest } = args;
|
|
1238
1262
|
const value = await this.generateText(
|
|
1239
1263
|
ctx,
|
|
1240
1264
|
{ userId: args.userId, threadId: args.threadId },
|
|
1241
|
-
{ 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
|
+
}
|
|
1242
1276
|
);
|
|
1243
1277
|
return value.text;
|
|
1244
1278
|
},
|
|
@@ -1251,21 +1285,36 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1251
1285
|
* the normal parameters to {@link generateObject}, plus {@link ContextOptions}
|
|
1252
1286
|
* and maxSteps.
|
|
1253
1287
|
*/
|
|
1254
|
-
asObjectAction<T>(
|
|
1288
|
+
asObjectAction<T>(
|
|
1289
|
+
spec: OurObjectArgs<T> & { maxSteps?: number },
|
|
1290
|
+
options?: {
|
|
1291
|
+
contextOptions?: ContextOptions;
|
|
1292
|
+
storageOptions?: StorageOptions;
|
|
1293
|
+
}
|
|
1294
|
+
) {
|
|
1255
1295
|
const maxSteps = spec?.maxSteps ?? this.options.maxSteps;
|
|
1256
1296
|
return internalActionGeneric({
|
|
1257
1297
|
args: vSafeObjectArgs,
|
|
1258
1298
|
handler: async (ctx, args) => {
|
|
1299
|
+
const { contextOptions, storageOptions, ...rest } = args;
|
|
1259
1300
|
const value = await this.generateObject(
|
|
1260
1301
|
ctx,
|
|
1261
1302
|
{ userId: args.userId, threadId: args.threadId },
|
|
1262
1303
|
{
|
|
1263
1304
|
...spec,
|
|
1264
1305
|
maxSteps,
|
|
1265
|
-
...
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
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
|
+
}
|
|
1269
1318
|
);
|
|
1270
1319
|
return value.object as T;
|
|
1271
1320
|
},
|
|
@@ -1373,6 +1422,17 @@ function wrapTools(
|
|
|
1373
1422
|
return output;
|
|
1374
1423
|
}
|
|
1375
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
|
+
|
|
1376
1436
|
type TextArgs<
|
|
1377
1437
|
AgentTools extends ToolSet,
|
|
1378
1438
|
TOOLS extends ToolSet | undefined = undefined,
|
|
@@ -1403,20 +1463,7 @@ type TextArgs<
|
|
|
1403
1463
|
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1404
1464
|
*/
|
|
1405
1465
|
toolChoice?: ToolChoice<TOOLS extends undefined ? AgentTools : TOOLS>;
|
|
1406
|
-
|
|
1407
|
-
/**
|
|
1408
|
-
* The parent message id to use for the tool calls.
|
|
1409
|
-
*/
|
|
1410
|
-
parentMessageId?: string;
|
|
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 &
|
|
1466
|
+
} & ContextOptions & // DEPRECATED: pass them in the subsequent parameter instead
|
|
1420
1467
|
StorageOptions;
|
|
1421
1468
|
|
|
1422
1469
|
type StreamingTextArgs<
|
|
@@ -1449,19 +1496,6 @@ type StreamingTextArgs<
|
|
|
1449
1496
|
* specified in the tools array. e.g. {toolName: "getWeather", type: "tool"}
|
|
1450
1497
|
*/
|
|
1451
1498
|
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;
|
|
1465
1499
|
} & ContextOptions &
|
|
1466
1500
|
StorageOptions;
|
|
1467
1501
|
|
|
@@ -1493,19 +1527,6 @@ type BaseGenerateObjectOptions = StorageOptions &
|
|
|
1493
1527
|
experimental_telemetry?: TelemetrySettings;
|
|
1494
1528
|
providerOptions?: ProviderOptions;
|
|
1495
1529
|
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;
|
|
1509
1530
|
};
|
|
1510
1531
|
|
|
1511
1532
|
type GenerateObjectObjectOptions<T extends Record<string, unknown>> =
|
|
@@ -1537,6 +1558,8 @@ type GenerateObjectNoSchemaOptions = BaseGenerateObjectOptions & {
|
|
|
1537
1558
|
mode?: "json";
|
|
1538
1559
|
};
|
|
1539
1560
|
|
|
1561
|
+
// TODO: simplify this to just use the generateObject args, with an optional
|
|
1562
|
+
// model and tool/toolChoice types
|
|
1540
1563
|
type GenerateObjectArgs<T> =
|
|
1541
1564
|
T extends Record<string, unknown>
|
|
1542
1565
|
? GenerateObjectObjectOptions<T>
|
|
@@ -1570,6 +1593,10 @@ type ThreadOutputMetadata = GenerationOutputMetadata & {
|
|
|
1570
1593
|
messageId: string;
|
|
1571
1594
|
};
|
|
1572
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
|
+
*/
|
|
1573
1600
|
interface Thread<DefaultTools extends ToolSet> {
|
|
1574
1601
|
/**
|
|
1575
1602
|
* The target threadId, from the startThread or continueThread initializers.
|
|
@@ -1595,7 +1622,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1595
1622
|
TOOLS,
|
|
1596
1623
|
OUTPUT,
|
|
1597
1624
|
OUTPUT_PARTIAL
|
|
1598
|
-
|
|
1625
|
+
>,
|
|
1626
|
+
options?: Options
|
|
1599
1627
|
): Promise<
|
|
1600
1628
|
GenerateTextResult<TOOLS extends undefined ? DefaultTools : TOOLS, OUTPUT> &
|
|
1601
1629
|
ThreadOutputMetadata
|
|
@@ -1621,7 +1649,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1621
1649
|
TOOLS,
|
|
1622
1650
|
OUTPUT,
|
|
1623
1651
|
PARTIAL_OUTPUT
|
|
1624
|
-
|
|
1652
|
+
>,
|
|
1653
|
+
options?: Options
|
|
1625
1654
|
): Promise<
|
|
1626
1655
|
StreamTextResult<
|
|
1627
1656
|
TOOLS extends undefined ? DefaultTools : TOOLS,
|
|
@@ -1640,7 +1669,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1640
1669
|
* @returns The result of the generateObject function.
|
|
1641
1670
|
*/
|
|
1642
1671
|
generateObject<T>(
|
|
1643
|
-
args: OurObjectArgs<T
|
|
1672
|
+
args: OurObjectArgs<T>,
|
|
1673
|
+
options?: Options
|
|
1644
1674
|
): Promise<GenerateObjectResult<T> & ThreadOutputMetadata>;
|
|
1645
1675
|
/**
|
|
1646
1676
|
* This behaves like {@link generateObject} from the "ai" package except that
|
|
@@ -1653,7 +1683,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1653
1683
|
* @returns The result of the generateObject function.
|
|
1654
1684
|
*/
|
|
1655
1685
|
generateObject(
|
|
1656
|
-
args: GenerateObjectNoSchemaOptions
|
|
1686
|
+
args: GenerateObjectNoSchemaOptions,
|
|
1687
|
+
options?: Options
|
|
1657
1688
|
): Promise<GenerateObjectResult<JSONValue> & ThreadOutputMetadata>;
|
|
1658
1689
|
/**
|
|
1659
1690
|
* This behaves like {@link streamObject} from the "ai" package except that
|
|
@@ -1666,7 +1697,8 @@ interface Thread<DefaultTools extends ToolSet> {
|
|
|
1666
1697
|
* @returns The result of the streamObject function.
|
|
1667
1698
|
*/
|
|
1668
1699
|
streamObject<T>(
|
|
1669
|
-
args: OurStreamObjectArgs<T
|
|
1700
|
+
args: OurStreamObjectArgs<T>,
|
|
1701
|
+
options?: Options
|
|
1670
1702
|
): Promise<
|
|
1671
1703
|
StreamObjectResult<DeepPartial<T>, T, never> & ThreadOutputMetadata
|
|
1672
1704
|
>;
|