@convex-dev/agent 0.1.10 → 0.1.11

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.
Files changed (132) hide show
  1. package/dist/esm/client/index.d.ts.map +1 -1
  2. package/dist/esm/client/index.js +25 -24
  3. package/dist/esm/client/index.js.map +1 -1
  4. package/dist/esm/mapping.d.ts +1 -0
  5. package/dist/esm/mapping.d.ts.map +1 -1
  6. package/dist/esm/mapping.js +2 -2
  7. package/dist/esm/mapping.js.map +1 -1
  8. package/dist/esm.tsbuildinfo +1 -1
  9. package/package.json +23 -53
  10. package/src/client/index.ts +27 -27
  11. package/src/mapping.ts +9 -2
  12. package/dist/commonjs/client/_generated/_ignore.d.ts +0 -1
  13. package/dist/commonjs/client/_generated/_ignore.d.ts.map +0 -1
  14. package/dist/commonjs/client/_generated/_ignore.js +0 -3
  15. package/dist/commonjs/client/_generated/_ignore.js.map +0 -1
  16. package/dist/commonjs/client/createTool.d.ts +0 -47
  17. package/dist/commonjs/client/createTool.d.ts.map +0 -1
  18. package/dist/commonjs/client/createTool.js +0 -49
  19. package/dist/commonjs/client/createTool.js.map +0 -1
  20. package/dist/commonjs/client/files.d.ts +0 -56
  21. package/dist/commonjs/client/files.d.ts.map +0 -1
  22. package/dist/commonjs/client/files.js +0 -129
  23. package/dist/commonjs/client/files.js.map +0 -1
  24. package/dist/commonjs/client/index.d.ts +0 -1355
  25. package/dist/commonjs/client/index.d.ts.map +0 -1
  26. package/dist/commonjs/client/index.js +0 -1169
  27. package/dist/commonjs/client/index.js.map +0 -1
  28. package/dist/commonjs/client/streaming.d.ts +0 -57
  29. package/dist/commonjs/client/streaming.d.ts.map +0 -1
  30. package/dist/commonjs/client/streaming.js +0 -148
  31. package/dist/commonjs/client/streaming.js.map +0 -1
  32. package/dist/commonjs/client/types.d.ts +0 -352
  33. package/dist/commonjs/client/types.d.ts.map +0 -1
  34. package/dist/commonjs/client/types.js +0 -3
  35. package/dist/commonjs/client/types.js.map +0 -1
  36. package/dist/commonjs/component/_generated/api.d.ts +0 -1938
  37. package/dist/commonjs/component/_generated/api.d.ts.map +0 -1
  38. package/dist/commonjs/component/_generated/api.js +0 -25
  39. package/dist/commonjs/component/_generated/api.js.map +0 -1
  40. package/dist/commonjs/component/_generated/dataModel.d.ts +0 -60
  41. package/dist/commonjs/component/_generated/server.d.ts +0 -149
  42. package/dist/commonjs/component/_generated/server.d.ts.map +0 -1
  43. package/dist/commonjs/component/_generated/server.js +0 -77
  44. package/dist/commonjs/component/_generated/server.js.map +0 -1
  45. package/dist/commonjs/component/apiKeys.d.ts +0 -11
  46. package/dist/commonjs/component/apiKeys.d.ts.map +0 -1
  47. package/dist/commonjs/component/apiKeys.js +0 -72
  48. package/dist/commonjs/component/apiKeys.js.map +0 -1
  49. package/dist/commonjs/component/convex.config.d.ts +0 -3
  50. package/dist/commonjs/component/convex.config.d.ts.map +0 -1
  51. package/dist/commonjs/component/convex.config.js +0 -5
  52. package/dist/commonjs/component/convex.config.js.map +0 -1
  53. package/dist/commonjs/component/files.d.ts +0 -90
  54. package/dist/commonjs/component/files.d.ts.map +0 -1
  55. package/dist/commonjs/component/files.js +0 -157
  56. package/dist/commonjs/component/files.js.map +0 -1
  57. package/dist/commonjs/component/messages.d.ts +0 -1479
  58. package/dist/commonjs/component/messages.d.ts.map +0 -1
  59. package/dist/commonjs/component/messages.js +0 -483
  60. package/dist/commonjs/component/messages.js.map +0 -1
  61. package/dist/commonjs/component/schema.d.ts +0 -4582
  62. package/dist/commonjs/component/schema.d.ts.map +0 -1
  63. package/dist/commonjs/component/schema.js +0 -167
  64. package/dist/commonjs/component/schema.js.map +0 -1
  65. package/dist/commonjs/component/streams.d.ts +0 -242
  66. package/dist/commonjs/component/streams.d.ts.map +0 -1
  67. package/dist/commonjs/component/streams.js +0 -323
  68. package/dist/commonjs/component/streams.js.map +0 -1
  69. package/dist/commonjs/component/threads.d.ts +0 -101
  70. package/dist/commonjs/component/threads.d.ts.map +0 -1
  71. package/dist/commonjs/component/threads.js +0 -188
  72. package/dist/commonjs/component/threads.js.map +0 -1
  73. package/dist/commonjs/component/users.d.ts +0 -52
  74. package/dist/commonjs/component/users.d.ts.map +0 -1
  75. package/dist/commonjs/component/users.js +0 -233
  76. package/dist/commonjs/component/users.js.map +0 -1
  77. package/dist/commonjs/component/vector/index.d.ts +0 -61
  78. package/dist/commonjs/component/vector/index.d.ts.map +0 -1
  79. package/dist/commonjs/component/vector/index.js +0 -153
  80. package/dist/commonjs/component/vector/index.js.map +0 -1
  81. package/dist/commonjs/component/vector/tables.d.ts +0 -58
  82. package/dist/commonjs/component/vector/tables.d.ts.map +0 -1
  83. package/dist/commonjs/component/vector/tables.js +0 -62
  84. package/dist/commonjs/component/vector/tables.js.map +0 -1
  85. package/dist/commonjs/mapping.d.ts +0 -50
  86. package/dist/commonjs/mapping.d.ts.map +0 -1
  87. package/dist/commonjs/mapping.js +0 -272
  88. package/dist/commonjs/mapping.js.map +0 -1
  89. package/dist/commonjs/package.json +0 -3
  90. package/dist/commonjs/react/deltas.d.ts +0 -29
  91. package/dist/commonjs/react/deltas.d.ts.map +0 -1
  92. package/dist/commonjs/react/deltas.js +0 -274
  93. package/dist/commonjs/react/deltas.js.map +0 -1
  94. package/dist/commonjs/react/index.d.ts +0 -95
  95. package/dist/commonjs/react/index.d.ts.map +0 -1
  96. package/dist/commonjs/react/index.js +0 -219
  97. package/dist/commonjs/react/index.js.map +0 -1
  98. package/dist/commonjs/react/optimisticallySendMessage.d.ts +0 -8
  99. package/dist/commonjs/react/optimisticallySendMessage.d.ts.map +0 -1
  100. package/dist/commonjs/react/optimisticallySendMessage.js +0 -43
  101. package/dist/commonjs/react/optimisticallySendMessage.js.map +0 -1
  102. package/dist/commonjs/react/toUIMessages.d.ts +0 -13
  103. package/dist/commonjs/react/toUIMessages.d.ts.map +0 -1
  104. package/dist/commonjs/react/toUIMessages.js +0 -149
  105. package/dist/commonjs/react/toUIMessages.js.map +0 -1
  106. package/dist/commonjs/react/types.d.ts +0 -26
  107. package/dist/commonjs/react/types.d.ts.map +0 -1
  108. package/dist/commonjs/react/types.js +0 -3
  109. package/dist/commonjs/react/types.js.map +0 -1
  110. package/dist/commonjs/react/useSmoothText.d.ts +0 -20
  111. package/dist/commonjs/react/useSmoothText.d.ts.map +0 -1
  112. package/dist/commonjs/react/useSmoothText.js +0 -54
  113. package/dist/commonjs/react/useSmoothText.js.map +0 -1
  114. package/dist/commonjs/shared.d.ts +0 -10
  115. package/dist/commonjs/shared.d.ts.map +0 -1
  116. package/dist/commonjs/shared.js +0 -35
  117. package/dist/commonjs/shared.js.map +0 -1
  118. package/dist/commonjs/validators.d.ts +0 -5716
  119. package/dist/commonjs/validators.d.ts.map +0 -1
  120. package/dist/commonjs/validators.js +0 -319
  121. package/dist/commonjs/validators.js.map +0 -1
  122. package/dist/commonjs/vitest.config.d.ts +0 -3
  123. package/dist/commonjs/vitest.config.d.ts.map +0 -1
  124. package/dist/commonjs/vitest.config.js +0 -9
  125. package/dist/commonjs/vitest.config.js.map +0 -1
  126. package/dist/commonjs.tsbuildinfo +0 -1
  127. package/dist/esm/vitest.config.d.ts +0 -3
  128. package/dist/esm/vitest.config.d.ts.map +0 -1
  129. package/dist/esm/vitest.config.js +0 -7
  130. package/dist/esm/vitest.config.js.map +0 -1
  131. package/react/package.json +0 -5
  132. package/validators/package.json +0 -5
@@ -1071,9 +1071,7 @@ export class Agent<AgentTools extends ToolSet> {
1071
1071
  const targetMessage = contextMessages.find(
1072
1072
  (m) => m._id === args.upToAndIncludingMessageId
1073
1073
  )?.message;
1074
- const messagesToSearch = targetMessage
1075
- ? [targetMessage, ...args.messages]
1076
- : args.messages;
1074
+ const messagesToSearch = targetMessage ? [targetMessage] : args.messages;
1077
1075
  if (!("runAction" in ctx)) {
1078
1076
  throw new Error("searchUserMessages only works in an action");
1079
1077
  }
@@ -1443,18 +1441,16 @@ export class Agent<AgentTools extends ToolSet> {
1443
1441
  }> {
1444
1442
  contextOptions ||= this.options.contextOptions;
1445
1443
  storageOptions ||= this.options.storageOptions;
1446
- // If only a messageId is provided, this will be empty.
1447
- const messages = args.promptMessageId
1448
- ? []
1449
- : promptOrMessagesToCoreMessages(args);
1444
+ // If only a promptMessageId is provided, this will be empty.
1445
+ const messages = promptOrMessagesToCoreMessages(args);
1450
1446
  const userId =
1451
1447
  argsUserId ??
1452
1448
  (threadId &&
1453
1449
  (await ctx.runQuery(this.component.threads.getThread, { threadId }))
1454
1450
  ?.userId);
1455
1451
  assert(
1456
- !args.promptMessageId || !(args.prompt || args.messages),
1457
- "you can't specify a prompt or message if you specify a promptMessageId"
1452
+ !args.promptMessageId || !args.prompt,
1453
+ "you can't specify a prompt if you specify a promptMessageId"
1458
1454
  );
1459
1455
  // If only a messageId is provided, this will add that message to the end.
1460
1456
  const contextMessages = await this.fetchContextMessages(ctx, {
@@ -1464,29 +1460,33 @@ export class Agent<AgentTools extends ToolSet> {
1464
1460
  messages,
1465
1461
  contextOptions,
1466
1462
  });
1467
- // Lazily generate embeddings for the prompt message, if it doesn't have
1468
- // embeddings yet. This can happen if the message was saved in a mutation
1469
- // where the LLM is not available.
1470
- if (
1471
- args.promptMessageId &&
1472
- !contextMessages.at(-1)?.embeddingId &&
1473
- this.options.textEmbedding
1474
- ) {
1475
- await this.generateAndSaveEmbeddings(ctx, {
1476
- messageIds: [args.promptMessageId],
1477
- });
1463
+ // If it was a promptMessageId, pop it off context messages
1464
+ // and add to the end of messages.
1465
+ const promptMessage =
1466
+ !!args.promptMessageId &&
1467
+ contextMessages.at(-1)?._id === args.promptMessageId
1468
+ ? contextMessages.pop()
1469
+ : undefined;
1470
+ if (promptMessage?.message) {
1471
+ messages.push(deserializeMessage(promptMessage.message));
1472
+ // Lazily generate embeddings for the prompt message, if it doesn't have
1473
+ // embeddings yet. This can happen if the message was saved in a mutation
1474
+ // where the LLM is not available.
1475
+ if (!promptMessage.embeddingId && this.options.textEmbedding) {
1476
+ await this.generateAndSaveEmbeddings(ctx, {
1477
+ messageIds: [promptMessage._id],
1478
+ });
1479
+ }
1478
1480
  }
1479
- let messageId = args.promptMessageId;
1480
- let order = args.promptMessageId
1481
- ? contextMessages.at(-1)?.order
1482
- : undefined;
1483
- let stepOrder = args.promptMessageId
1484
- ? contextMessages.at(-1)?.stepOrder
1485
- : undefined;
1481
+ let messageId = promptMessage?._id;
1482
+ let order = promptMessage?.order;
1483
+ let stepOrder = promptMessage?.stepOrder;
1486
1484
  if (
1487
1485
  threadId &&
1488
1486
  messages.length &&
1489
1487
  storageOptions?.saveMessages !== "none" &&
1488
+ // If it was a promptMessageId, we don't want to save it again.
1489
+ (!args.promptMessageId || storageOptions?.saveMessages === "all") &&
1490
1490
  storageOptions?.saveAnyInputMessages !== false
1491
1491
  ) {
1492
1492
  const saveAll = storageOptions?.saveMessages === "all";
package/src/mapping.ts CHANGED
@@ -334,9 +334,17 @@ function encodeBase64(data: ArrayBuffer): string {
334
334
  export function promptOrMessagesToCoreMessages(args: {
335
335
  prompt?: string;
336
336
  messages?: CoreMessage[] | AIMessageWithoutId[];
337
+ promptMessageId?: string;
337
338
  }): CoreMessage[] {
338
339
  const messages: CoreMessage[] = [];
339
- assert(args.prompt || args.messages, "messages or prompt is required");
340
+ assert(
341
+ args.prompt || args.messages || args.promptMessageId,
342
+ "messages or prompt or promptMessageId is required"
343
+ );
344
+ assert(
345
+ !args.promptMessageId || !args.prompt,
346
+ "you can't specify a prompt if you specify a promptMessageId. use messages instead."
347
+ );
340
348
  if (args.messages) {
341
349
  if (
342
350
  args.messages.some(
@@ -357,6 +365,5 @@ export function promptOrMessagesToCoreMessages(args: {
357
365
  if (args.prompt) {
358
366
  messages.push({ role: "user", content: args.prompt });
359
367
  }
360
- assert(messages.length > 0, "Messages must contain at least one message");
361
368
  return messages;
362
369
  }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=_ignore.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_ignore.d.ts","sourceRoot":"","sources":["../../../../src/client/_generated/_ignore.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- "use strict";
2
- // This is only here so convex-test can detect a _generated folder
3
- //# sourceMappingURL=_ignore.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"_ignore.js","sourceRoot":"","sources":["../../../../src/client/_generated/_ignore.ts"],"names":[],"mappings":";AAAA,kEAAkE"}
@@ -1,47 +0,0 @@
1
- import type { Schema, Tool, ToolExecutionOptions, ToolSet } from "ai";
2
- import { z } from "zod";
3
- import type { Agent } from "./index.js";
4
- import type { RunActionCtx } from "./types.js";
5
- export type ToolCtx<TOOLS extends ToolSet = ToolSet> = RunActionCtx & {
6
- agent: Agent<TOOLS>;
7
- userId?: string;
8
- threadId?: string;
9
- messageId?: string;
10
- };
11
- /**
12
- * This is a wrapper around the ai.tool function that adds extra context to the
13
- * tool call, including the action context, userId, threadId, and messageId.
14
- * @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
15
- * but swap parameters for args and handler for execute.
16
- * @returns A tool to be used with the AI SDK.
17
- */
18
- export declare function createTool<PARAMETERS extends ToolParameters, RESULT>(t: {
19
- /**
20
- An optional description of what the tool does.
21
- Will be used by the language model to decide whether to use the tool.
22
- Not used for provider-defined tools.
23
- */
24
- description?: string;
25
- /**
26
- The schema of the input that the tool expects. The language model will use this to generate the input.
27
- It is also used to validate the output of the language model.
28
- Use descriptions to make the input understandable for the language model.
29
- */
30
- args: PARAMETERS;
31
- /**
32
- An async function that is called with the arguments from the tool call and produces a result.
33
- If not provided, the tool will not be executed automatically.
34
-
35
- @args is the input of the tool call.
36
- @options.abortSignal is a signal that can be used to abort the tool call.
37
- */
38
- handler: (ctx: ToolCtx, args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
39
- ctx?: ToolCtx;
40
- }): Tool<PARAMETERS, RESULT> & {
41
- execute: (args: inferParameters<PARAMETERS>, options: ToolExecutionOptions) => PromiseLike<RESULT>;
42
- };
43
- export declare function wrapTools(ctx: ToolCtx, ...toolSets: (ToolSet | undefined)[]): ToolSet;
44
- type ToolParameters = z.ZodTypeAny | Schema<any>;
45
- type inferParameters<PARAMETERS extends ToolParameters> = PARAMETERS extends Schema<any> ? PARAMETERS["_type"] : PARAMETERS extends z.ZodTypeAny ? z.infer<PARAMETERS> : never;
46
- export {};
47
- //# sourceMappingURL=createTool.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTool.d.ts","sourceRoot":"","sources":["../../../src/client/createTool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,IAAI,CAAC;AAEtE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,OAAO,CAAC,KAAK,SAAS,OAAO,GAAG,OAAO,IAAI,YAAY,GAAG;IACpE,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,UAAU,SAAS,cAAc,EAAE,MAAM,EAAE,CAAC,EAAE;IACvE;;;;SAIK;IACL,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;SAIK;IACL,IAAI,EAAE,UAAU,CAAC;IACjB;;;;;;SAMK;IACL,OAAO,EAAE,CACP,GAAG,EAAE,OAAO,EACZ,IAAI,EAAE,eAAe,CAAC,UAAU,CAAC,EACjC,OAAO,EAAE,oBAAoB,KAC1B,WAAW,CAAC,MAAM,CAAC,CAAC;IACzB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,GAAG,IAAI,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IAC7B,OAAO,EAAE,CACP,IAAI,EAAE,eAAe,CAAC,UAAU,CAAC,EACjC,OAAO,EAAE,oBAAoB,KAC1B,WAAW,CAAC,MAAM,CAAC,CAAC;CAC1B,CAqBA;AAED,wBAAgB,SAAS,CACvB,GAAG,EAAE,OAAO,EACZ,GAAG,QAAQ,EAAE,CAAC,OAAO,GAAG,SAAS,CAAC,EAAE,GACnC,OAAO,CAiBT;AAID,KAAK,cAAc,GAAG,CAAC,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;AACjD,KAAK,eAAe,CAAC,UAAU,SAAS,cAAc,IAEpD,UAAU,SAAS,MAAM,CAAC,GAAG,CAAC,GAC1B,UAAU,CAAC,OAAO,CAAC,GACnB,UAAU,SAAS,CAAC,CAAC,UAAU,GAC7B,CAAC,CAAC,KAAK,CAAC,UAAU,CAAC,GACnB,KAAK,CAAC"}
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createTool = createTool;
4
- exports.wrapTools = wrapTools;
5
- const ai_1 = require("ai");
6
- /**
7
- * This is a wrapper around the ai.tool function that adds extra context to the
8
- * tool call, including the action context, userId, threadId, and messageId.
9
- * @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
10
- * but swap parameters for args and handler for execute.
11
- * @returns A tool to be used with the AI SDK.
12
- */
13
- function createTool(t) {
14
- const args = {
15
- __acceptsCtx: true,
16
- ctx: t.ctx,
17
- description: t.description,
18
- parameters: t.args,
19
- async execute(args, options) {
20
- if (!this.ctx) {
21
- throw new Error("To use a Convex tool, you must either provide the ctx" +
22
- " at definition time (dynamically in an action), or use the Agent to" +
23
- " call it (which injects the ctx, userId and threadId)");
24
- }
25
- return t.handler(this.ctx, args, options);
26
- },
27
- };
28
- return (0, ai_1.tool)(args);
29
- }
30
- function wrapTools(ctx, ...toolSets) {
31
- const output = {};
32
- for (const toolSet of toolSets) {
33
- if (!toolSet) {
34
- continue;
35
- }
36
- for (const [name, tool] of Object.entries(toolSet)) {
37
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
- if (!tool.__acceptsCtx) {
39
- output[name] = tool;
40
- }
41
- else {
42
- const out = { ...tool, ctx };
43
- output[name] = out;
44
- }
45
- }
46
- }
47
- return output;
48
- }
49
- //# sourceMappingURL=createTool.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createTool.js","sourceRoot":"","sources":["../../../src/client/createTool.ts"],"names":[],"mappings":";;AAoBA,gCAoDC;AAED,8BAoBC;AA7FD,2BAA0B;AAY1B;;;;;;GAMG;AACH,SAAgB,UAAU,CAA4C,CA0BrE;IAMC,MAAM,IAAI,GAAG;QACX,YAAY,EAAE,IAAI;QAClB,GAAG,EAAE,CAAC,CAAC,GAAG;QACV,WAAW,EAAE,CAAC,CAAC,WAAW;QAC1B,UAAU,EAAE,CAAC,CAAC,IAAI;QAClB,KAAK,CAAC,OAAO,CACX,IAAiC,EACjC,OAA6B;YAE7B,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;gBACd,MAAM,IAAI,KAAK,CACb,uDAAuD;oBACrD,qEAAqE;oBACrE,uDAAuD,CAC1D,CAAC;YACJ,CAAC;YACD,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;QAC5C,CAAC;KACF,CAAC;IACF,OAAO,IAAA,SAAI,EAAC,IAAI,CAAC,CAAC;AACpB,CAAC;AAED,SAAgB,SAAS,CACvB,GAAY,EACZ,GAAG,QAAiC;IAEpC,MAAM,MAAM,GAAG,EAAa,CAAC;IAC7B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,SAAS;QACX,CAAC;QACD,KAAK,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;YACnD,8DAA8D;YAC9D,IAAI,CAAE,IAAY,CAAC,YAAY,EAAE,CAAC;gBAChC,MAAM,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;YACtB,CAAC;iBAAM,CAAC;gBACN,MAAM,GAAG,GAAG,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,CAAC;gBAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;YACrB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -1,56 +0,0 @@
1
- import type { FilePart, ImagePart } from "ai";
2
- import type { Id } from "../component/_generated/dataModel.js";
3
- import type { ActionCtx, AgentComponent, QueryCtx, RunMutationCtx } from "./types.js";
4
- export declare const MAX_FILE_SIZE: number;
5
- type File = {
6
- url: string;
7
- fileId: string;
8
- storageId: Id<"_storage">;
9
- hash: string;
10
- filename: string | undefined;
11
- };
12
- /**
13
- * Store a file in the file storage and return the URL and fileId.
14
- * @param ctx A ctx object from an action.
15
- * @param component The agent component.
16
- * @param blob The blob to store.
17
- * @param filename The filename to store.
18
- * @param sha256 The sha256 hash of the file. If not provided, it will be
19
- * computed. However, to ensure no corruption during transfer, you can
20
- * calculate this on the client to enforce integrity.
21
- * @returns The URL, fileId, and storageId of the stored file.
22
- */
23
- export declare function storeFile(ctx: ActionCtx | RunMutationCtx, component: AgentComponent, blob: Blob, filename?: string, sha256?: string): Promise<{
24
- file: File;
25
- filePart: FilePart;
26
- imagePart: ImagePart | undefined;
27
- }>;
28
- /**
29
- * Get file metadata from the component.
30
- * This also returns filePart (and imagePart if the file is an image),
31
- * which are useful to construct a CoreMessage like
32
- * ```ts
33
- * const { filePart, imagePart } = await getFile(ctx, components.agent, fileId);
34
- * const message: UserMessage = {
35
- * role: "user",
36
- * content: [imagePart ?? filePart],
37
- * };
38
- * ```
39
- * @param ctx A ctx object from an action or query.
40
- * @param component The agent component, usually `components.agent`.
41
- * @param fileId The fileId of the file to get.
42
- * @returns The file metadata and content parts.
43
- */
44
- export declare function getFile(ctx: ActionCtx | QueryCtx, component: AgentComponent, fileId: string): Promise<{
45
- file: {
46
- fileId: string;
47
- url: string;
48
- storageId: Id<"_storage">;
49
- hash: string;
50
- filename: string | undefined;
51
- };
52
- filePart: FilePart;
53
- imagePart: ImagePart | undefined;
54
- }>;
55
- export {};
56
- //# sourceMappingURL=files.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../src/client/files.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,sCAAsC,CAAC;AAC/D,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,QAAQ,EACR,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,aAAa,QAAY,CAAC;AAEvC,KAAK,IAAI,GAAG;IACV,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAsB,SAAS,CAC7B,GAAG,EAAE,SAAS,GAAG,cAAc,EAC/B,SAAS,EAAE,cAAc,EACzB,IAAI,EAAE,IAAI,EACV,QAAQ,CAAC,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC;IACT,IAAI,EAAE,IAAI,CAAC;IACX,QAAQ,EAAE,QAAQ,CAAC;IACnB,SAAS,EAAE,SAAS,GAAG,SAAS,CAAC;CAClC,CAAC,CAiED;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,OAAO,CAC3B,GAAG,EAAE,SAAS,GAAG,QAAQ,EACzB,SAAS,EAAE,cAAc,EACzB,MAAM,EAAE,MAAM;;;;mBAemB,EAAE,CAAC,UAAU,CAAC;;;;cAYrC,QAAQ;eACP,SAAS,GAAG,SAAS;GARjC"}
@@ -1,129 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_FILE_SIZE = void 0;
4
- exports.storeFile = storeFile;
5
- exports.getFile = getFile;
6
- exports.MAX_FILE_SIZE = 1024 * 64;
7
- /**
8
- * Store a file in the file storage and return the URL and fileId.
9
- * @param ctx A ctx object from an action.
10
- * @param component The agent component.
11
- * @param blob The blob to store.
12
- * @param filename The filename to store.
13
- * @param sha256 The sha256 hash of the file. If not provided, it will be
14
- * computed. However, to ensure no corruption during transfer, you can
15
- * calculate this on the client to enforce integrity.
16
- * @returns The URL, fileId, and storageId of the stored file.
17
- */
18
- async function storeFile(ctx, component, blob, filename, sha256) {
19
- if (!("runAction" in ctx)) {
20
- throw new Error("You're trying to save a file that's too large in a mutation. " +
21
- "You can store the file in file storage from an action first, then pass a URL instead. " +
22
- "To have the agent component track the file, you can use `saveFile` from an action then use the fileId with getFile in the mutation. " +
23
- "Read more in the docs.");
24
- }
25
- const hash = sha256 ||
26
- Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", await blob.arrayBuffer())))
27
- .map((b) => b.toString(16).padStart(2, "0"))
28
- .join("");
29
- const reused = await ctx.runMutation(component.files.useExistingFile, {
30
- hash,
31
- filename,
32
- });
33
- if (reused) {
34
- const url = (await ctx.storage.getUrl(reused.storageId));
35
- return {
36
- ...getParts(url, blob.type, filename),
37
- file: {
38
- url,
39
- fileId: reused.fileId,
40
- storageId: reused.storageId,
41
- hash,
42
- filename,
43
- },
44
- };
45
- }
46
- const newStorageId = await ctx.storage.store(blob);
47
- if (sha256) {
48
- const metadata = await ctx.storage.getMetadata(newStorageId);
49
- if (metadata?.sha256 !== sha256) {
50
- throw new Error("Hash mismatch: " + metadata?.sha256 + " != " + sha256);
51
- }
52
- }
53
- const { fileId, storageId } = await ctx.runMutation(component.files.addFile, {
54
- storageId: newStorageId,
55
- hash,
56
- filename,
57
- mimeType: blob.type,
58
- });
59
- const url = (await ctx.storage.getUrl(storageId));
60
- if (storageId !== newStorageId) {
61
- // We're re-using another file's storageId
62
- // Because we try to reuse the file above, this should be very very rare
63
- // and only in the case of racing to check then store the file.
64
- await ctx.storage.delete(newStorageId);
65
- }
66
- return {
67
- ...getParts(url, blob.type, filename),
68
- file: {
69
- url,
70
- fileId,
71
- storageId: storageId,
72
- hash,
73
- filename,
74
- },
75
- };
76
- }
77
- /**
78
- * Get file metadata from the component.
79
- * This also returns filePart (and imagePart if the file is an image),
80
- * which are useful to construct a CoreMessage like
81
- * ```ts
82
- * const { filePart, imagePart } = await getFile(ctx, components.agent, fileId);
83
- * const message: UserMessage = {
84
- * role: "user",
85
- * content: [imagePart ?? filePart],
86
- * };
87
- * ```
88
- * @param ctx A ctx object from an action or query.
89
- * @param component The agent component, usually `components.agent`.
90
- * @param fileId The fileId of the file to get.
91
- * @returns The file metadata and content parts.
92
- */
93
- async function getFile(ctx, component, fileId) {
94
- const file = await ctx.runQuery(component.files.get, { fileId });
95
- if (!file) {
96
- throw new Error(`File not found in component: ${fileId}`);
97
- }
98
- const url = await ctx.storage.getUrl(file.storageId);
99
- if (!url) {
100
- throw new Error(`File not found in storage: ${file.storageId}`);
101
- }
102
- return {
103
- ...getParts(url, file.mimeType, file.filename),
104
- file: {
105
- fileId,
106
- url,
107
- storageId: file.storageId,
108
- hash: file.hash,
109
- filename: file.filename,
110
- },
111
- };
112
- }
113
- function getParts(url, mimeType, filename) {
114
- const filePart = {
115
- type: "file",
116
- data: new URL(url),
117
- mimeType,
118
- filename,
119
- };
120
- const imagePart = mimeType.startsWith("image/")
121
- ? {
122
- type: "image",
123
- image: new URL(url),
124
- mimeType,
125
- }
126
- : undefined;
127
- return { filePart, imagePart };
128
- }
129
- //# sourceMappingURL=files.js.map
@@ -1 +0,0 @@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