@convex-dev/agent 0.0.14-alpha.1 → 0.0.14-alpha.3
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/dist/commonjs/client/index.d.ts +27 -27
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +25 -38
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/component/messages.d.ts +18 -18
- package/dist/commonjs/component/schema.d.ts +30 -30
- package/dist/commonjs/validators.d.ts +37 -37
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +6 -4
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/esm/client/index.d.ts +27 -27
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +25 -38
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/component/messages.d.ts +18 -18
- package/dist/esm/component/schema.d.ts +30 -30
- package/dist/esm/validators.d.ts +37 -37
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +6 -4
- package/dist/esm/validators.js.map +1 -1
- package/package.json +1 -1
- package/src/client/index.ts +64 -44
- package/src/component/_generated/api.d.ts +13 -13
- package/src/validators.ts +9 -5
package/src/client/index.ts
CHANGED
|
@@ -53,7 +53,7 @@ import {
|
|
|
53
53
|
type ProviderMetadata,
|
|
54
54
|
type ProviderOptions,
|
|
55
55
|
type SearchOptions,
|
|
56
|
-
Usage,
|
|
56
|
+
type Usage,
|
|
57
57
|
vSafeObjectArgs,
|
|
58
58
|
vTextArgs,
|
|
59
59
|
} from "../validators.js";
|
|
@@ -66,6 +66,16 @@ import type {
|
|
|
66
66
|
} from "./types.js";
|
|
67
67
|
import schema from "../component/schema.js";
|
|
68
68
|
|
|
69
|
+
export {
|
|
70
|
+
vUsage,
|
|
71
|
+
vProviderMetadata,
|
|
72
|
+
vUserMessage,
|
|
73
|
+
vAssistantMessage,
|
|
74
|
+
vToolMessage,
|
|
75
|
+
vSystemMessage,
|
|
76
|
+
vMessage,
|
|
77
|
+
} from "../validators.js";
|
|
78
|
+
|
|
69
79
|
export type ThreadDoc = OpaqueIds<
|
|
70
80
|
{ _id: string; _creationTime: number } & Infer<
|
|
71
81
|
typeof schema.tables.threads.validator
|
|
@@ -261,6 +271,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
261
271
|
* The summary of the thread. Not currently used.
|
|
262
272
|
*/
|
|
263
273
|
summary?: string;
|
|
274
|
+
/**
|
|
275
|
+
* The usage handler to use for this thread. Overrides any handler
|
|
276
|
+
* set in the agent constructor.
|
|
277
|
+
*/
|
|
278
|
+
usageHandler?: UsageHandler;
|
|
264
279
|
}
|
|
265
280
|
): Promise<{
|
|
266
281
|
threadId: string;
|
|
@@ -282,6 +297,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
282
297
|
parentThreadIds?: string[];
|
|
283
298
|
title?: string;
|
|
284
299
|
summary?: string;
|
|
300
|
+
usageHandler?: UsageHandler;
|
|
285
301
|
}
|
|
286
302
|
): Promise<{
|
|
287
303
|
threadId: string;
|
|
@@ -293,6 +309,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
293
309
|
parentThreadIds?: string[];
|
|
294
310
|
title?: string;
|
|
295
311
|
summary?: string;
|
|
312
|
+
usageHandler?: UsageHandler;
|
|
296
313
|
}
|
|
297
314
|
): Promise<{
|
|
298
315
|
threadId: string;
|
|
@@ -314,6 +331,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
314
331
|
const { thread } = await this.continueThread(ctx, {
|
|
315
332
|
threadId: threadDoc._id,
|
|
316
333
|
userId: args?.userId,
|
|
334
|
+
usageHandler: args?.usageHandler,
|
|
317
335
|
});
|
|
318
336
|
return {
|
|
319
337
|
threadId: threadDoc._id,
|
|
@@ -321,14 +339,6 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
321
339
|
};
|
|
322
340
|
}
|
|
323
341
|
|
|
324
|
-
/**
|
|
325
|
-
* Continues a thread using this agent. Note: threads can be continued
|
|
326
|
-
* by different agents. This is a convenience around calling the various
|
|
327
|
-
* generate and stream functions with explicit userId and threadId parameters.
|
|
328
|
-
* @param ctx The ctx object passed to the action handler
|
|
329
|
-
* @param { threadId, userId }: the thread and user to associate the messages with.
|
|
330
|
-
* @returns Functions bound to the userId and threadId on a `{thread}` object.
|
|
331
|
-
*/
|
|
332
342
|
/**
|
|
333
343
|
* Continues a thread using this agent. Note: threads can be continued
|
|
334
344
|
* by different agents. This is a convenience around calling the various
|
|
@@ -339,13 +349,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
339
349
|
*/
|
|
340
350
|
async continueThread(
|
|
341
351
|
ctx: RunActionCtx,
|
|
342
|
-
{
|
|
343
|
-
threadId,
|
|
344
|
-
userId,
|
|
345
|
-
}: {
|
|
346
|
-
/**
|
|
347
|
-
* The associated thread created by {@link createThread}
|
|
348
|
-
*/
|
|
352
|
+
args: {
|
|
349
353
|
/**
|
|
350
354
|
* The associated thread created by {@link createThread}
|
|
351
355
|
*/
|
|
@@ -355,34 +359,28 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
355
359
|
* relevant messages from the same user as context for the LLM calls.
|
|
356
360
|
*/
|
|
357
361
|
userId?: string;
|
|
362
|
+
/**
|
|
363
|
+
* The usage handler to use for this thread. Overrides any handler
|
|
364
|
+
* set in the agent constructor.
|
|
365
|
+
*/
|
|
366
|
+
usageHandler?: UsageHandler;
|
|
358
367
|
}
|
|
359
368
|
): Promise<{
|
|
360
369
|
thread: Thread<AgentTools>;
|
|
361
370
|
}> {
|
|
362
371
|
return {
|
|
363
372
|
thread: {
|
|
364
|
-
threadId,
|
|
365
|
-
generateText: this.generateText.bind(this, ctx,
|
|
366
|
-
streamText: this.streamText.bind(this, ctx,
|
|
367
|
-
generateObject: this.generateObject.bind(this, ctx,
|
|
368
|
-
|
|
369
|
-
threadId,
|
|
370
|
-
}),
|
|
371
|
-
streamObject: this.streamObject.bind(this, ctx, { userId, threadId }),
|
|
373
|
+
threadId: args.threadId,
|
|
374
|
+
generateText: this.generateText.bind(this, ctx, args),
|
|
375
|
+
streamText: this.streamText.bind(this, ctx, args),
|
|
376
|
+
generateObject: this.generateObject.bind(this, ctx, args),
|
|
377
|
+
streamObject: this.streamObject.bind(this, ctx, args),
|
|
372
378
|
} as Thread<AgentTools>,
|
|
373
379
|
};
|
|
374
380
|
}
|
|
375
381
|
|
|
376
382
|
/**
|
|
377
|
-
*
|
|
378
|
-
* @param ctx Either a query, mutation, or action ctx.
|
|
379
|
-
* If it is not an action context, you can't do text or
|
|
380
|
-
* vector search.
|
|
381
|
-
* @param args The associated thread, user, message
|
|
382
|
-
* @returns
|
|
383
|
-
*/
|
|
384
|
-
/**
|
|
385
|
-
*
|
|
383
|
+
* Fetch the context messages for a thread.
|
|
386
384
|
* @param ctx Either a query, mutation, or action ctx.
|
|
387
385
|
* If it is not an action context, you can't do text or
|
|
388
386
|
* vector search.
|
|
@@ -651,9 +649,15 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
651
649
|
{
|
|
652
650
|
userId,
|
|
653
651
|
threadId,
|
|
652
|
+
usageHandler,
|
|
654
653
|
}: {
|
|
655
654
|
userId?: string;
|
|
656
655
|
threadId?: string;
|
|
656
|
+
/**
|
|
657
|
+
* The usage handler to use for this thread. Overrides any handler
|
|
658
|
+
* set in the agent constructor.
|
|
659
|
+
*/
|
|
660
|
+
usageHandler?: UsageHandler;
|
|
657
661
|
},
|
|
658
662
|
args: TextArgs<
|
|
659
663
|
AgentTools,
|
|
@@ -673,6 +677,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
673
677
|
args.saveOutputMessages ??
|
|
674
678
|
this.options.storageOptions?.saveOutputMessages;
|
|
675
679
|
const model = aiArgs.model ?? this.options.chat;
|
|
680
|
+
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
676
681
|
try {
|
|
677
682
|
const result = (await generateText({
|
|
678
683
|
// Can be overridden
|
|
@@ -691,8 +696,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
691
696
|
step,
|
|
692
697
|
});
|
|
693
698
|
}
|
|
694
|
-
if (
|
|
695
|
-
await
|
|
699
|
+
if (trackUsage && step.usage) {
|
|
700
|
+
await trackUsage(ctx, {
|
|
696
701
|
userId,
|
|
697
702
|
threadId,
|
|
698
703
|
agentName: this.options.name,
|
|
@@ -737,7 +742,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
737
742
|
PARTIAL_OUTPUT = never,
|
|
738
743
|
>(
|
|
739
744
|
ctx: RunActionCtx,
|
|
740
|
-
{
|
|
745
|
+
{
|
|
746
|
+
userId,
|
|
747
|
+
threadId,
|
|
748
|
+
usageHandler,
|
|
749
|
+
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
741
750
|
args: TextArgs<
|
|
742
751
|
AgentTools,
|
|
743
752
|
TOOLS,
|
|
@@ -756,6 +765,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
756
765
|
args.saveOutputMessages ??
|
|
757
766
|
this.options.storageOptions?.saveOutputMessages;
|
|
758
767
|
const model = aiArgs.model ?? this.options.chat;
|
|
768
|
+
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
759
769
|
const result = streamText({
|
|
760
770
|
// Can be overridden
|
|
761
771
|
maxSteps: this.options.maxSteps,
|
|
@@ -789,8 +799,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
789
799
|
step,
|
|
790
800
|
});
|
|
791
801
|
}
|
|
792
|
-
if (
|
|
793
|
-
await
|
|
802
|
+
if (trackUsage && step.usage) {
|
|
803
|
+
await trackUsage(ctx, {
|
|
794
804
|
userId,
|
|
795
805
|
threadId,
|
|
796
806
|
agentName: this.options.name,
|
|
@@ -889,7 +899,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
889
899
|
*/
|
|
890
900
|
async generateObject<T>(
|
|
891
901
|
ctx: RunActionCtx,
|
|
892
|
-
{
|
|
902
|
+
{
|
|
903
|
+
userId,
|
|
904
|
+
threadId,
|
|
905
|
+
usageHandler,
|
|
906
|
+
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
893
907
|
args: OurObjectArgs<T>
|
|
894
908
|
): Promise<GenerateObjectResult<T> & GenerationOutputMetadata> {
|
|
895
909
|
const { args: aiArgs, messageId } = await this.saveMessagesAndFetchContext(
|
|
@@ -897,6 +911,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
897
911
|
{ ...args, userId, threadId }
|
|
898
912
|
);
|
|
899
913
|
const model = aiArgs.model ?? this.options.chat;
|
|
914
|
+
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
900
915
|
const saveOutputMessages =
|
|
901
916
|
args.saveOutputMessages ??
|
|
902
917
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -913,8 +928,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
913
928
|
await this.saveObject(ctx, { threadId, messageId, result });
|
|
914
929
|
}
|
|
915
930
|
result.messageId = messageId;
|
|
916
|
-
if (
|
|
917
|
-
await
|
|
931
|
+
if (trackUsage && result.usage) {
|
|
932
|
+
await trackUsage(ctx, {
|
|
918
933
|
userId,
|
|
919
934
|
threadId,
|
|
920
935
|
agentName: this.options.name,
|
|
@@ -950,7 +965,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
950
965
|
*/
|
|
951
966
|
async streamObject<T>(
|
|
952
967
|
ctx: RunActionCtx,
|
|
953
|
-
{
|
|
968
|
+
{
|
|
969
|
+
userId,
|
|
970
|
+
threadId,
|
|
971
|
+
usageHandler,
|
|
972
|
+
}: { userId?: string; threadId?: string; usageHandler?: UsageHandler },
|
|
954
973
|
args: OurStreamObjectArgs<T>
|
|
955
974
|
): Promise<
|
|
956
975
|
StreamObjectResult<DeepPartial<T>, T, never> & GenerationOutputMetadata
|
|
@@ -961,6 +980,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
961
980
|
{ ...args, userId, threadId }
|
|
962
981
|
);
|
|
963
982
|
const model = aiArgs.model ?? this.options.chat;
|
|
983
|
+
const trackUsage = usageHandler ?? this.options.usageHandler;
|
|
964
984
|
const saveOutputMessages =
|
|
965
985
|
args.saveOutputMessages ??
|
|
966
986
|
this.options.storageOptions?.saveOutputMessages;
|
|
@@ -994,8 +1014,8 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
994
1014
|
},
|
|
995
1015
|
});
|
|
996
1016
|
}
|
|
997
|
-
if (
|
|
998
|
-
await
|
|
1017
|
+
if (trackUsage && result.usage) {
|
|
1018
|
+
await trackUsage(ctx, {
|
|
999
1019
|
userId,
|
|
1000
1020
|
threadId,
|
|
1001
1021
|
agentName: this.options.name,
|
|
@@ -155,11 +155,11 @@ export type Mounts = {
|
|
|
155
155
|
};
|
|
156
156
|
model?: string;
|
|
157
157
|
provider?: string;
|
|
158
|
-
providerMetadata?: Record<string, any
|
|
158
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
159
159
|
reasoning?: string;
|
|
160
160
|
sources?: Array<{
|
|
161
161
|
id: string;
|
|
162
|
-
providerMetadata?: Record<string, any
|
|
162
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
163
163
|
sourceType: "url";
|
|
164
164
|
title?: string;
|
|
165
165
|
url: string;
|
|
@@ -313,7 +313,7 @@ export type Mounts = {
|
|
|
313
313
|
reasoning?: string;
|
|
314
314
|
sources?: Array<{
|
|
315
315
|
id: string;
|
|
316
|
-
providerMetadata?: Record<string, any
|
|
316
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
317
317
|
sourceType: "url";
|
|
318
318
|
title?: string;
|
|
319
319
|
url: string;
|
|
@@ -466,7 +466,7 @@ export type Mounts = {
|
|
|
466
466
|
reasoning?: string;
|
|
467
467
|
sources?: Array<{
|
|
468
468
|
id: string;
|
|
469
|
-
providerMetadata?: Record<string, any
|
|
469
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
470
470
|
sourceType: "url";
|
|
471
471
|
title?: string;
|
|
472
472
|
url: string;
|
|
@@ -623,11 +623,11 @@ export type Mounts = {
|
|
|
623
623
|
};
|
|
624
624
|
model?: string;
|
|
625
625
|
provider?: string;
|
|
626
|
-
providerMetadata?: Record<string, any
|
|
626
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
627
627
|
reasoning?: string;
|
|
628
628
|
sources?: Array<{
|
|
629
629
|
id: string;
|
|
630
|
-
providerMetadata?: Record<string, any
|
|
630
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
631
631
|
sourceType: "url";
|
|
632
632
|
title?: string;
|
|
633
633
|
url: string;
|
|
@@ -661,7 +661,7 @@ export type Mounts = {
|
|
|
661
661
|
| "unknown";
|
|
662
662
|
isContinued: boolean;
|
|
663
663
|
logprobs?: any;
|
|
664
|
-
providerMetadata?: Record<string, any
|
|
664
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
665
665
|
providerOptions?: Record<string, any>;
|
|
666
666
|
reasoning?: string;
|
|
667
667
|
reasoningDetails?: Array<any>;
|
|
@@ -805,7 +805,7 @@ export type Mounts = {
|
|
|
805
805
|
};
|
|
806
806
|
sources?: Array<{
|
|
807
807
|
id: string;
|
|
808
|
-
providerMetadata?: Record<string, any
|
|
808
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
809
809
|
sourceType: "url";
|
|
810
810
|
title?: string;
|
|
811
811
|
url: string;
|
|
@@ -871,7 +871,7 @@ export type Mounts = {
|
|
|
871
871
|
| "unknown";
|
|
872
872
|
isContinued: boolean;
|
|
873
873
|
logprobs?: any;
|
|
874
|
-
providerMetadata?: Record<string, any
|
|
874
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
875
875
|
providerOptions?: Record<string, any>;
|
|
876
876
|
reasoning?: string;
|
|
877
877
|
reasoningDetails?: Array<any>;
|
|
@@ -1015,7 +1015,7 @@ export type Mounts = {
|
|
|
1015
1015
|
};
|
|
1016
1016
|
sources?: Array<{
|
|
1017
1017
|
id: string;
|
|
1018
|
-
providerMetadata?: Record<string, any
|
|
1018
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
1019
1019
|
sourceType: "url";
|
|
1020
1020
|
title?: string;
|
|
1021
1021
|
url: string;
|
|
@@ -1294,7 +1294,7 @@ export type Mounts = {
|
|
|
1294
1294
|
reasoning?: string;
|
|
1295
1295
|
sources?: Array<{
|
|
1296
1296
|
id: string;
|
|
1297
|
-
providerMetadata?: Record<string, any
|
|
1297
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
1298
1298
|
sourceType: "url";
|
|
1299
1299
|
title?: string;
|
|
1300
1300
|
url: string;
|
|
@@ -1504,7 +1504,7 @@ export type Mounts = {
|
|
|
1504
1504
|
reasoning?: string;
|
|
1505
1505
|
sources?: Array<{
|
|
1506
1506
|
id: string;
|
|
1507
|
-
providerMetadata?: Record<string, any
|
|
1507
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
1508
1508
|
sourceType: "url";
|
|
1509
1509
|
title?: string;
|
|
1510
1510
|
url: string;
|
|
@@ -1662,7 +1662,7 @@ export type Mounts = {
|
|
|
1662
1662
|
reasoning?: string;
|
|
1663
1663
|
sources?: Array<{
|
|
1664
1664
|
id: string;
|
|
1665
|
-
providerMetadata?: Record<string, any
|
|
1665
|
+
providerMetadata?: Record<string, Record<string, any>>;
|
|
1666
1666
|
sourceType: "url";
|
|
1667
1667
|
title?: string;
|
|
1668
1668
|
url: string;
|
package/src/validators.ts
CHANGED
|
@@ -5,8 +5,12 @@ import { vVectorDimension } from "./component/vector/tables";
|
|
|
5
5
|
|
|
6
6
|
const providerOptions = v.optional(v.record(v.string(), v.any()));
|
|
7
7
|
export type ProviderOptions = Infer<typeof providerOptions>;
|
|
8
|
+
const providerMetadata = v.optional(
|
|
9
|
+
v.record(v.string(), v.record(v.string(), v.any()))
|
|
10
|
+
);
|
|
11
|
+
export { providerMetadata as vProviderMetadata };
|
|
12
|
+
export type ProviderMetadata = Infer<typeof providerMetadata>;
|
|
8
13
|
const experimental_providerMetadata = providerOptions;
|
|
9
|
-
export type ProviderMetadata = Infer<typeof experimental_providerMetadata>;
|
|
10
14
|
|
|
11
15
|
export const vThreadStatus = v.union(
|
|
12
16
|
v.literal("active"),
|
|
@@ -161,7 +165,7 @@ export const vSource = v.object({
|
|
|
161
165
|
id: v.string(),
|
|
162
166
|
url: v.string(),
|
|
163
167
|
title: v.optional(v.string()),
|
|
164
|
-
providerMetadata
|
|
168
|
+
providerMetadata,
|
|
165
169
|
});
|
|
166
170
|
|
|
167
171
|
export const vRequest = v.object({
|
|
@@ -238,7 +242,7 @@ export const vMessageWithMetadata = v.object({
|
|
|
238
242
|
finishReason: v.optional(vFinishReason),
|
|
239
243
|
model: v.optional(v.string()),
|
|
240
244
|
provider: v.optional(v.string()),
|
|
241
|
-
providerMetadata
|
|
245
|
+
providerMetadata,
|
|
242
246
|
sources: v.optional(v.array(vSource)),
|
|
243
247
|
reasoning: v.optional(v.string()),
|
|
244
248
|
usage: v.optional(vUsage),
|
|
@@ -261,7 +265,7 @@ export const vStep = v.object({
|
|
|
261
265
|
finishReason: vFinishReason,
|
|
262
266
|
isContinued: v.boolean(),
|
|
263
267
|
logprobs: v.optional(v.any()),
|
|
264
|
-
providerMetadata
|
|
268
|
+
providerMetadata,
|
|
265
269
|
providerOptions,
|
|
266
270
|
reasoning: v.optional(v.string()),
|
|
267
271
|
reasoningDetails: v.optional(v.array(v.any())),
|
|
@@ -295,7 +299,7 @@ export const vObjectResult = v.object({
|
|
|
295
299
|
object: v.any(),
|
|
296
300
|
error: v.optional(v.string()),
|
|
297
301
|
warnings: v.optional(v.array(vLanguageModelV1CallWarning)),
|
|
298
|
-
providerMetadata
|
|
302
|
+
providerMetadata,
|
|
299
303
|
experimental_providerMetadata,
|
|
300
304
|
});
|
|
301
305
|
export type ObjectResult = Infer<typeof vObjectResult>;
|