@convex-dev/agent 0.1.5-alpha.2 → 0.1.5
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 +1 -0
- package/dist/commonjs/client/index.d.ts +30 -2
- package/dist/commonjs/client/index.d.ts.map +1 -1
- package/dist/commonjs/client/index.js +47 -2
- package/dist/commonjs/client/index.js.map +1 -1
- package/dist/commonjs/client/types.d.ts +13 -5
- package/dist/commonjs/client/types.d.ts.map +1 -1
- package/dist/commonjs/component/_generated/api.d.ts +2327 -4
- package/dist/commonjs/component/_generated/dataModel.d.ts +60 -0
- package/dist/commonjs/component/_generated/server.d.ts +97 -12
- package/dist/commonjs/component/messages.d.ts +24 -0
- package/dist/commonjs/component/messages.d.ts.map +1 -1
- package/dist/commonjs/component/schema.d.ts +119 -15
- package/dist/commonjs/component/schema.d.ts.map +1 -1
- package/dist/commonjs/react/types.d.ts +3 -3
- package/dist/commonjs/react/types.d.ts.map +1 -1
- package/dist/commonjs/react/useSmoothText.d.ts.map +1 -1
- package/dist/commonjs/react/useSmoothText.js +1 -0
- package/dist/commonjs/react/useSmoothText.js.map +1 -1
- package/dist/commonjs/validators.d.ts +222 -34
- package/dist/commonjs/validators.d.ts.map +1 -1
- package/dist/commonjs/validators.js +1 -0
- package/dist/commonjs/validators.js.map +1 -1
- package/dist/commonjs.tsbuildinfo +1 -1
- package/dist/esm/client/index.d.ts +30 -2
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +47 -2
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/types.d.ts +13 -5
- package/dist/esm/client/types.d.ts.map +1 -1
- package/dist/esm/component/_generated/api.d.ts +2327 -4
- package/dist/esm/component/_generated/dataModel.d.ts +60 -0
- package/dist/esm/component/_generated/server.d.ts +97 -12
- package/dist/esm/component/messages.d.ts +24 -0
- package/dist/esm/component/messages.d.ts.map +1 -1
- package/dist/esm/component/schema.d.ts +119 -15
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/react/types.d.ts +3 -3
- package/dist/esm/react/types.d.ts.map +1 -1
- package/dist/esm/react/useSmoothText.d.ts.map +1 -1
- package/dist/esm/react/useSmoothText.js +1 -0
- package/dist/esm/react/useSmoothText.js.map +1 -1
- package/dist/esm/validators.d.ts +222 -34
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +1 -0
- package/dist/esm/validators.js.map +1 -1
- package/dist/esm.tsbuildinfo +1 -1
- package/package.json +10 -7
- package/src/client/index.ts +64 -4
- package/src/client/types.ts +19 -9
- package/src/component/_generated/api.d.ts +22 -0
- package/src/react/types.ts +3 -3
- package/src/react/useSmoothText.ts +4 -0
- package/src/validators.ts +1 -0
package/src/client/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
internalMutationGeneric,
|
|
17
17
|
type PaginationOptions,
|
|
18
18
|
type PaginationResult,
|
|
19
|
+
type WithoutSystemFields,
|
|
19
20
|
} from "convex/server";
|
|
20
21
|
import { v } from "convex/values";
|
|
21
22
|
import type { MessageDoc, ThreadDoc } from "../component/schema.js";
|
|
@@ -323,6 +324,14 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
323
324
|
return {
|
|
324
325
|
thread: {
|
|
325
326
|
threadId: args.threadId,
|
|
327
|
+
getMetadata: this.getThreadMetadata.bind(this, ctx, {
|
|
328
|
+
threadId: args.threadId,
|
|
329
|
+
}),
|
|
330
|
+
updateMetadata: (patch: Partial<WithoutSystemFields<ThreadDoc>>) =>
|
|
331
|
+
ctx.runMutation(this.component.threads.updateThread, {
|
|
332
|
+
threadId: args.threadId,
|
|
333
|
+
patch,
|
|
334
|
+
}),
|
|
326
335
|
generateText: this.generateText.bind(this, ctx, args),
|
|
327
336
|
streamText: this.streamText.bind(this, ctx, args),
|
|
328
337
|
generateObject: this.generateObject.bind(this, ctx, args),
|
|
@@ -331,6 +340,43 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
331
340
|
};
|
|
332
341
|
}
|
|
333
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Get the metadata for a thread.
|
|
345
|
+
* @param ctx A ctx object from a query, mutation, or action.
|
|
346
|
+
* @param args.threadId The thread to get the metadata for.
|
|
347
|
+
* @returns The metadata for the thread.
|
|
348
|
+
*/
|
|
349
|
+
async getThreadMetadata(
|
|
350
|
+
ctx: RunQueryCtx,
|
|
351
|
+
args: { threadId: string }
|
|
352
|
+
): Promise<ThreadDoc> {
|
|
353
|
+
const thread = await ctx.runQuery(this.component.threads.getThread, {
|
|
354
|
+
threadId: args.threadId,
|
|
355
|
+
});
|
|
356
|
+
if (!thread) {
|
|
357
|
+
throw new Error("Thread not found");
|
|
358
|
+
}
|
|
359
|
+
return thread;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Update the metadata for a thread.
|
|
364
|
+
* @param ctx A ctx object from a mutation or action.
|
|
365
|
+
* @param args.threadId The thread to update the metadata for.
|
|
366
|
+
* @param args.patch The patch to apply to the thread.
|
|
367
|
+
* @returns The updated thread metadata.
|
|
368
|
+
*/
|
|
369
|
+
async updateThreadMetadata(
|
|
370
|
+
ctx: RunMutationCtx,
|
|
371
|
+
args: { threadId: string; patch: Partial<WithoutSystemFields<ThreadDoc>> }
|
|
372
|
+
): Promise<ThreadDoc> {
|
|
373
|
+
const thread = await ctx.runMutation(
|
|
374
|
+
this.component.threads.updateThread,
|
|
375
|
+
args
|
|
376
|
+
);
|
|
377
|
+
return thread;
|
|
378
|
+
}
|
|
379
|
+
|
|
334
380
|
/**
|
|
335
381
|
* List messages from a thread.
|
|
336
382
|
* @param ctx A ctx object from a query, mutation, or action.
|
|
@@ -711,9 +757,11 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
711
757
|
if (args.skipEmbeddings || !("runAction" in ctx)) {
|
|
712
758
|
embeddings = undefined;
|
|
713
759
|
if (!args.skipEmbeddings && this.options.textEmbedding) {
|
|
714
|
-
|
|
715
|
-
"You're trying to save messages and generate embeddings, but you're in a mutation.
|
|
716
|
-
"
|
|
760
|
+
console.warn(
|
|
761
|
+
"You're trying to save messages and generate embeddings, but you're in a mutation. " +
|
|
762
|
+
"Pass `skipEmbeddings: true` to skip generating embeddings in the mutation and skip this warning. " +
|
|
763
|
+
"They will be generated lazily when you generate or stream text / objects. " +
|
|
764
|
+
"You can explicitly generate them asynchronously by using the scheduler to run an action later that calls `agent.generateAndSaveEmbeddings`."
|
|
717
765
|
);
|
|
718
766
|
}
|
|
719
767
|
} else {
|
|
@@ -1110,7 +1158,7 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1110
1158
|
maxRetries?: number;
|
|
1111
1159
|
},
|
|
1112
1160
|
>(
|
|
1113
|
-
ctx: RunActionCtx
|
|
1161
|
+
ctx: RunActionCtx,
|
|
1114
1162
|
args: T,
|
|
1115
1163
|
{
|
|
1116
1164
|
userId: argsUserId,
|
|
@@ -1151,6 +1199,18 @@ export class Agent<AgentTools extends ToolSet> {
|
|
|
1151
1199
|
messages,
|
|
1152
1200
|
contextOptions,
|
|
1153
1201
|
});
|
|
1202
|
+
// Lazily generate embeddings for the prompt message, if it doesn't have
|
|
1203
|
+
// embeddings yet. This can happen if the message was saved in a mutation
|
|
1204
|
+
// where the LLM is not available.
|
|
1205
|
+
if (
|
|
1206
|
+
args.promptMessageId &&
|
|
1207
|
+
!contextMessages.at(-1)?.embeddingId &&
|
|
1208
|
+
this.options.textEmbedding
|
|
1209
|
+
) {
|
|
1210
|
+
await this.generateAndSaveEmbeddings(ctx, {
|
|
1211
|
+
messageIds: [args.promptMessageId],
|
|
1212
|
+
});
|
|
1213
|
+
}
|
|
1154
1214
|
let messageId = args.promptMessageId;
|
|
1155
1215
|
let order = args.promptMessageId
|
|
1156
1216
|
? contextMessages.at(-1)?.order
|
package/src/client/types.ts
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
CoreMessage,
|
|
3
3
|
DeepPartial,
|
|
4
|
+
generateObject,
|
|
4
5
|
GenerateObjectResult,
|
|
6
|
+
generateText,
|
|
5
7
|
GenerateTextResult,
|
|
6
8
|
JSONValue,
|
|
9
|
+
LanguageModelV1,
|
|
7
10
|
RepairTextFunction,
|
|
11
|
+
streamObject,
|
|
8
12
|
StreamObjectResult,
|
|
13
|
+
streamText,
|
|
9
14
|
StreamTextResult,
|
|
10
15
|
TelemetrySettings,
|
|
11
16
|
ToolChoice,
|
|
12
17
|
ToolSet,
|
|
13
18
|
} from "ai";
|
|
14
|
-
import type {
|
|
15
|
-
generateObject,
|
|
16
|
-
generateText,
|
|
17
|
-
LanguageModelV1,
|
|
18
|
-
streamObject,
|
|
19
|
-
streamText,
|
|
20
|
-
} from "ai";
|
|
21
19
|
import type {
|
|
22
20
|
Expand,
|
|
23
21
|
FunctionReference,
|
|
@@ -25,8 +23,12 @@ import type {
|
|
|
25
23
|
GenericDataModel,
|
|
26
24
|
GenericMutationCtx,
|
|
27
25
|
GenericQueryCtx,
|
|
26
|
+
WithoutSystemFields,
|
|
28
27
|
} from "convex/server";
|
|
29
28
|
import type { GenericId } from "convex/values";
|
|
29
|
+
import type { Schema } from "zod";
|
|
30
|
+
import type { Mounts } from "../component/_generated/api.js";
|
|
31
|
+
import type { ThreadDoc } from "../component/schema.js";
|
|
30
32
|
import type {
|
|
31
33
|
CallSettings,
|
|
32
34
|
ProviderMetadata,
|
|
@@ -35,8 +37,6 @@ import type {
|
|
|
35
37
|
StreamMessage,
|
|
36
38
|
Usage,
|
|
37
39
|
} from "../validators.js";
|
|
38
|
-
import type { Mounts } from "../component/_generated/api.js";
|
|
39
|
-
import type { Schema } from "zod";
|
|
40
40
|
import type { StreamingOptions } from "./streaming.js";
|
|
41
41
|
|
|
42
42
|
/**
|
|
@@ -311,6 +311,16 @@ export interface Thread<DefaultTools extends ToolSet> {
|
|
|
311
311
|
* The target threadId, from the startThread or continueThread initializers.
|
|
312
312
|
*/
|
|
313
313
|
threadId: string;
|
|
314
|
+
/**
|
|
315
|
+
* Get the metadata for the thread.
|
|
316
|
+
*/
|
|
317
|
+
getMetadata: () => Promise<ThreadDoc>;
|
|
318
|
+
/**
|
|
319
|
+
* Update the metadata for the thread.
|
|
320
|
+
*/
|
|
321
|
+
updateMetadata: (
|
|
322
|
+
patch: Partial<WithoutSystemFields<ThreadDoc>>
|
|
323
|
+
) => Promise<ThreadDoc>;
|
|
314
324
|
/**
|
|
315
325
|
* This behaves like {@link generateText} from the "ai" package except that
|
|
316
326
|
* it add context based on the userId and threadId and saves the input and
|
|
@@ -143,6 +143,7 @@ export type Mounts = {
|
|
|
143
143
|
}
|
|
144
144
|
| {
|
|
145
145
|
data: string | ArrayBuffer;
|
|
146
|
+
filename?: string;
|
|
146
147
|
mimeType: string;
|
|
147
148
|
providerOptions?: Record<string, Record<string, any>>;
|
|
148
149
|
type: "file";
|
|
@@ -162,6 +163,7 @@ export type Mounts = {
|
|
|
162
163
|
}
|
|
163
164
|
| {
|
|
164
165
|
data: string | ArrayBuffer;
|
|
166
|
+
filename?: string;
|
|
165
167
|
mimeType: string;
|
|
166
168
|
providerOptions?: Record<string, Record<string, any>>;
|
|
167
169
|
type: "file";
|
|
@@ -283,6 +285,7 @@ export type Mounts = {
|
|
|
283
285
|
}
|
|
284
286
|
| {
|
|
285
287
|
data: string | ArrayBuffer;
|
|
288
|
+
filename?: string;
|
|
286
289
|
mimeType: string;
|
|
287
290
|
providerOptions?: Record<string, Record<string, any>>;
|
|
288
291
|
type: "file";
|
|
@@ -302,6 +305,7 @@ export type Mounts = {
|
|
|
302
305
|
}
|
|
303
306
|
| {
|
|
304
307
|
data: string | ArrayBuffer;
|
|
308
|
+
filename?: string;
|
|
305
309
|
mimeType: string;
|
|
306
310
|
providerOptions?: Record<string, Record<string, any>>;
|
|
307
311
|
type: "file";
|
|
@@ -423,6 +427,7 @@ export type Mounts = {
|
|
|
423
427
|
}
|
|
424
428
|
| {
|
|
425
429
|
data: string | ArrayBuffer;
|
|
430
|
+
filename?: string;
|
|
426
431
|
mimeType: string;
|
|
427
432
|
providerOptions?: Record<string, Record<string, any>>;
|
|
428
433
|
type: "file";
|
|
@@ -442,6 +447,7 @@ export type Mounts = {
|
|
|
442
447
|
}
|
|
443
448
|
| {
|
|
444
449
|
data: string | ArrayBuffer;
|
|
450
|
+
filename?: string;
|
|
445
451
|
mimeType: string;
|
|
446
452
|
providerOptions?: Record<string, Record<string, any>>;
|
|
447
453
|
type: "file";
|
|
@@ -589,6 +595,7 @@ export type Mounts = {
|
|
|
589
595
|
}
|
|
590
596
|
| {
|
|
591
597
|
data: string | ArrayBuffer;
|
|
598
|
+
filename?: string;
|
|
592
599
|
mimeType: string;
|
|
593
600
|
providerOptions?: Record<
|
|
594
601
|
string,
|
|
@@ -614,6 +621,7 @@ export type Mounts = {
|
|
|
614
621
|
}
|
|
615
622
|
| {
|
|
616
623
|
data: string | ArrayBuffer;
|
|
624
|
+
filename?: string;
|
|
617
625
|
mimeType: string;
|
|
618
626
|
providerOptions?: Record<
|
|
619
627
|
string,
|
|
@@ -761,6 +769,7 @@ export type Mounts = {
|
|
|
761
769
|
}
|
|
762
770
|
| {
|
|
763
771
|
data: string | ArrayBuffer;
|
|
772
|
+
filename?: string;
|
|
764
773
|
mimeType: string;
|
|
765
774
|
providerOptions?: Record<
|
|
766
775
|
string,
|
|
@@ -786,6 +795,7 @@ export type Mounts = {
|
|
|
786
795
|
}
|
|
787
796
|
| {
|
|
788
797
|
data: string | ArrayBuffer;
|
|
798
|
+
filename?: string;
|
|
789
799
|
mimeType: string;
|
|
790
800
|
providerOptions?: Record<
|
|
791
801
|
string,
|
|
@@ -937,6 +947,7 @@ export type Mounts = {
|
|
|
937
947
|
}
|
|
938
948
|
| {
|
|
939
949
|
data: string | ArrayBuffer;
|
|
950
|
+
filename?: string;
|
|
940
951
|
mimeType: string;
|
|
941
952
|
providerOptions?: Record<string, Record<string, any>>;
|
|
942
953
|
type: "file";
|
|
@@ -956,6 +967,7 @@ export type Mounts = {
|
|
|
956
967
|
}
|
|
957
968
|
| {
|
|
958
969
|
data: string | ArrayBuffer;
|
|
970
|
+
filename?: string;
|
|
959
971
|
mimeType: string;
|
|
960
972
|
providerOptions?: Record<string, Record<string, any>>;
|
|
961
973
|
type: "file";
|
|
@@ -1088,6 +1100,7 @@ export type Mounts = {
|
|
|
1088
1100
|
}
|
|
1089
1101
|
| {
|
|
1090
1102
|
data: string | ArrayBuffer;
|
|
1103
|
+
filename?: string;
|
|
1091
1104
|
mimeType: string;
|
|
1092
1105
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1093
1106
|
type: "file";
|
|
@@ -1107,6 +1120,7 @@ export type Mounts = {
|
|
|
1107
1120
|
}
|
|
1108
1121
|
| {
|
|
1109
1122
|
data: string | ArrayBuffer;
|
|
1123
|
+
filename?: string;
|
|
1110
1124
|
mimeType: string;
|
|
1111
1125
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1112
1126
|
type: "file";
|
|
@@ -1236,6 +1250,7 @@ export type Mounts = {
|
|
|
1236
1250
|
}
|
|
1237
1251
|
| {
|
|
1238
1252
|
data: string | ArrayBuffer;
|
|
1253
|
+
filename?: string;
|
|
1239
1254
|
mimeType: string;
|
|
1240
1255
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1241
1256
|
type: "file";
|
|
@@ -1255,6 +1270,7 @@ export type Mounts = {
|
|
|
1255
1270
|
}
|
|
1256
1271
|
| {
|
|
1257
1272
|
data: string | ArrayBuffer;
|
|
1273
|
+
filename?: string;
|
|
1258
1274
|
mimeType: string;
|
|
1259
1275
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1260
1276
|
type: "file";
|
|
@@ -1402,6 +1418,7 @@ export type Mounts = {
|
|
|
1402
1418
|
}
|
|
1403
1419
|
| {
|
|
1404
1420
|
data: string | ArrayBuffer;
|
|
1421
|
+
filename?: string;
|
|
1405
1422
|
mimeType: string;
|
|
1406
1423
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1407
1424
|
type: "file";
|
|
@@ -1421,6 +1438,7 @@ export type Mounts = {
|
|
|
1421
1438
|
}
|
|
1422
1439
|
| {
|
|
1423
1440
|
data: string | ArrayBuffer;
|
|
1441
|
+
filename?: string;
|
|
1424
1442
|
mimeType: string;
|
|
1425
1443
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1426
1444
|
type: "file";
|
|
@@ -1566,6 +1584,7 @@ export type Mounts = {
|
|
|
1566
1584
|
}
|
|
1567
1585
|
| {
|
|
1568
1586
|
data: string | ArrayBuffer;
|
|
1587
|
+
filename?: string;
|
|
1569
1588
|
mimeType: string;
|
|
1570
1589
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1571
1590
|
type: "file";
|
|
@@ -1585,6 +1604,7 @@ export type Mounts = {
|
|
|
1585
1604
|
}
|
|
1586
1605
|
| {
|
|
1587
1606
|
data: string | ArrayBuffer;
|
|
1607
|
+
filename?: string;
|
|
1588
1608
|
mimeType: string;
|
|
1589
1609
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1590
1610
|
type: "file";
|
|
@@ -1717,6 +1737,7 @@ export type Mounts = {
|
|
|
1717
1737
|
}
|
|
1718
1738
|
| {
|
|
1719
1739
|
data: string | ArrayBuffer;
|
|
1740
|
+
filename?: string;
|
|
1720
1741
|
mimeType: string;
|
|
1721
1742
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1722
1743
|
type: "file";
|
|
@@ -1736,6 +1757,7 @@ export type Mounts = {
|
|
|
1736
1757
|
}
|
|
1737
1758
|
| {
|
|
1738
1759
|
data: string | ArrayBuffer;
|
|
1760
|
+
filename?: string;
|
|
1739
1761
|
mimeType: string;
|
|
1740
1762
|
providerOptions?: Record<string, Record<string, any>>;
|
|
1741
1763
|
type: "file";
|
package/src/react/types.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type {
|
|
|
5
5
|
PaginationOptions,
|
|
6
6
|
PaginationResult,
|
|
7
7
|
} from "convex/server";
|
|
8
|
-
import type { MessageDoc } from "../client";
|
|
9
|
-
import type { SyncStreamsReturnValue } from "../client/types";
|
|
10
|
-
import type { StreamArgs } from "../validators";
|
|
8
|
+
import type { MessageDoc } from "../client/index.js";
|
|
9
|
+
import type { SyncStreamsReturnValue } from "../client/types.js";
|
|
10
|
+
import type { StreamArgs } from "../validators.js";
|
|
11
11
|
|
|
12
12
|
export type ThreadQuery<
|
|
13
13
|
Args = unknown,
|
|
@@ -47,6 +47,10 @@ export function useSmoothText(
|
|
|
47
47
|
(2 * latestCharsPerMs + smoothState.current.charsPerMs) / 3,
|
|
48
48
|
smoothState.current.charsPerMs * 2
|
|
49
49
|
);
|
|
50
|
+
smoothState.current.tick = Math.max(
|
|
51
|
+
smoothState.current.tick,
|
|
52
|
+
Date.now() - 2 * MS_PER_FRAME
|
|
53
|
+
);
|
|
50
54
|
|
|
51
55
|
function update() {
|
|
52
56
|
if (smoothState.current.cursor >= text.length) {
|
package/src/validators.ts
CHANGED