@convex-dev/agent 0.1.10-alpha.1 → 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.
- package/README.md +1 -1
- package/dist/esm/client/index.d.ts +10 -11
- package/dist/esm/client/index.d.ts.map +1 -1
- package/dist/esm/client/index.js +27 -28
- package/dist/esm/client/index.js.map +1 -1
- package/dist/esm/client/streaming.d.ts.map +1 -1
- package/dist/esm/client/streaming.js +1 -7
- package/dist/esm/client/streaming.js.map +1 -1
- package/dist/esm/component/_generated/api.d.ts +2 -13
- package/dist/esm/component/files.d.ts +1 -1
- package/dist/esm/component/files.d.ts.map +1 -1
- package/dist/esm/component/files.js +6 -4
- package/dist/esm/component/files.js.map +1 -1
- package/dist/esm/component/messages.d.ts +30 -30
- package/dist/esm/component/schema.d.ts +74 -84
- package/dist/esm/component/schema.d.ts.map +1 -1
- package/dist/esm/component/schema.js +2 -3
- package/dist/esm/component/schema.js.map +1 -1
- package/dist/esm/component/streams.d.ts +0 -7
- package/dist/esm/component/streams.d.ts.map +1 -1
- package/dist/esm/component/streams.js +22 -49
- package/dist/esm/component/streams.js.map +1 -1
- package/dist/esm/component/threads.d.ts +8 -8
- package/dist/esm/component/vector/tables.d.ts +3 -5
- package/dist/esm/component/vector/tables.d.ts.map +1 -1
- package/dist/esm/mapping.d.ts +4 -3
- package/dist/esm/mapping.d.ts.map +1 -1
- package/dist/esm/mapping.js +7 -5
- package/dist/esm/mapping.js.map +1 -1
- package/dist/esm/react/deltas.d.ts.map +1 -1
- package/dist/esm/react/deltas.js +2 -2
- package/dist/esm/react/deltas.js.map +1 -1
- package/dist/esm/react/index.d.ts +2 -4
- package/dist/esm/react/index.d.ts.map +1 -1
- package/dist/esm/react/index.js +9 -22
- package/dist/esm/react/index.js.map +1 -1
- package/dist/esm/react/optimisticallySendMessage.js +1 -1
- package/dist/esm/react/optimisticallySendMessage.js.map +1 -1
- package/dist/esm/validators.d.ts +10 -15
- package/dist/esm/validators.d.ts.map +1 -1
- package/dist/esm/validators.js +0 -2
- package/dist/esm/validators.js.map +1 -1
- package/dist/esm.tsbuildinfo +1 -1
- package/package.json +24 -49
- package/src/client/index.ts +42 -39
- package/src/client/streaming.ts +1 -7
- package/src/component/_generated/api.d.ts +2 -13
- package/src/component/files.ts +6 -4
- package/src/component/schema.ts +2 -3
- package/src/component/streams.ts +39 -85
- package/src/component/vector/tables.ts +1 -1
- package/src/mapping.ts +17 -6
- package/src/react/deltas.ts +2 -4
- package/src/react/index.ts +11 -23
- package/src/react/optimisticallySendMessage.ts +1 -1
- package/src/validators.ts +0 -6
- package/dist/commonjs/client/_generated/_ignore.d.ts +0 -1
- package/dist/commonjs/client/_generated/_ignore.d.ts.map +0 -1
- package/dist/commonjs/client/_generated/_ignore.js +0 -3
- package/dist/commonjs/client/_generated/_ignore.js.map +0 -1
- package/dist/commonjs/client/createTool.d.ts +0 -47
- package/dist/commonjs/client/createTool.d.ts.map +0 -1
- package/dist/commonjs/client/createTool.js +0 -46
- package/dist/commonjs/client/createTool.js.map +0 -1
- package/dist/commonjs/client/files.d.ts +0 -56
- package/dist/commonjs/client/files.d.ts.map +0 -1
- package/dist/commonjs/client/files.js +0 -124
- package/dist/commonjs/client/files.js.map +0 -1
- package/dist/commonjs/client/index.d.ts +0 -1356
- package/dist/commonjs/client/index.d.ts.map +0 -1
- package/dist/commonjs/client/index.js +0 -1148
- package/dist/commonjs/client/index.js.map +0 -1
- package/dist/commonjs/client/streaming.d.ts +0 -57
- package/dist/commonjs/client/streaming.d.ts.map +0 -1
- package/dist/commonjs/client/streaming.js +0 -149
- package/dist/commonjs/client/streaming.js.map +0 -1
- package/dist/commonjs/client/types.d.ts +0 -352
- package/dist/commonjs/client/types.d.ts.map +0 -1
- package/dist/commonjs/client/types.js +0 -2
- package/dist/commonjs/client/types.js.map +0 -1
- package/dist/commonjs/component/_generated/api.d.ts +0 -1949
- package/dist/commonjs/component/_generated/api.d.ts.map +0 -1
- package/dist/commonjs/component/_generated/api.js +0 -22
- package/dist/commonjs/component/_generated/api.js.map +0 -1
- package/dist/commonjs/component/_generated/dataModel.d.ts +0 -60
- package/dist/commonjs/component/_generated/server.d.ts +0 -149
- package/dist/commonjs/component/_generated/server.d.ts.map +0 -1
- package/dist/commonjs/component/_generated/server.js +0 -74
- package/dist/commonjs/component/_generated/server.js.map +0 -1
- package/dist/commonjs/component/apiKeys.d.ts +0 -11
- package/dist/commonjs/component/apiKeys.d.ts.map +0 -1
- package/dist/commonjs/component/apiKeys.js +0 -69
- package/dist/commonjs/component/apiKeys.js.map +0 -1
- package/dist/commonjs/component/convex.config.d.ts +0 -3
- package/dist/commonjs/component/convex.config.d.ts.map +0 -1
- package/dist/commonjs/component/convex.config.js +0 -3
- package/dist/commonjs/component/convex.config.js.map +0 -1
- package/dist/commonjs/component/files.d.ts +0 -90
- package/dist/commonjs/component/files.d.ts.map +0 -1
- package/dist/commonjs/component/files.js +0 -150
- package/dist/commonjs/component/files.js.map +0 -1
- package/dist/commonjs/component/messages.d.ts +0 -1479
- package/dist/commonjs/component/messages.d.ts.map +0 -1
- package/dist/commonjs/component/messages.js +0 -478
- package/dist/commonjs/component/messages.js.map +0 -1
- package/dist/commonjs/component/schema.d.ts +0 -4592
- package/dist/commonjs/component/schema.d.ts.map +0 -1
- package/dist/commonjs/component/schema.js +0 -165
- package/dist/commonjs/component/schema.js.map +0 -1
- package/dist/commonjs/component/streams.d.ts +0 -249
- package/dist/commonjs/component/streams.d.ts.map +0 -1
- package/dist/commonjs/component/streams.js +0 -346
- package/dist/commonjs/component/streams.js.map +0 -1
- package/dist/commonjs/component/threads.d.ts +0 -101
- package/dist/commonjs/component/threads.d.ts.map +0 -1
- package/dist/commonjs/component/threads.js +0 -185
- package/dist/commonjs/component/threads.js.map +0 -1
- package/dist/commonjs/component/users.d.ts +0 -52
- package/dist/commonjs/component/users.d.ts.map +0 -1
- package/dist/commonjs/component/users.js +0 -230
- package/dist/commonjs/component/users.js.map +0 -1
- package/dist/commonjs/component/vector/index.d.ts +0 -61
- package/dist/commonjs/component/vector/index.d.ts.map +0 -1
- package/dist/commonjs/component/vector/index.js +0 -148
- package/dist/commonjs/component/vector/index.js.map +0 -1
- package/dist/commonjs/component/vector/tables.d.ts +0 -60
- package/dist/commonjs/component/vector/tables.d.ts.map +0 -1
- package/dist/commonjs/component/vector/tables.js +0 -56
- package/dist/commonjs/component/vector/tables.js.map +0 -1
- package/dist/commonjs/mapping.d.ts +0 -50
- package/dist/commonjs/mapping.d.ts.map +0 -1
- package/dist/commonjs/mapping.js +0 -257
- package/dist/commonjs/mapping.js.map +0 -1
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/react/deltas.d.ts +0 -29
- package/dist/commonjs/react/deltas.d.ts.map +0 -1
- package/dist/commonjs/react/deltas.js +0 -268
- package/dist/commonjs/react/deltas.js.map +0 -1
- package/dist/commonjs/react/index.d.ts +0 -97
- package/dist/commonjs/react/index.d.ts.map +0 -1
- package/dist/commonjs/react/index.js +0 -224
- package/dist/commonjs/react/index.js.map +0 -1
- package/dist/commonjs/react/optimisticallySendMessage.d.ts +0 -8
- package/dist/commonjs/react/optimisticallySendMessage.d.ts.map +0 -1
- package/dist/commonjs/react/optimisticallySendMessage.js +0 -40
- package/dist/commonjs/react/optimisticallySendMessage.js.map +0 -1
- package/dist/commonjs/react/toUIMessages.d.ts +0 -13
- package/dist/commonjs/react/toUIMessages.d.ts.map +0 -1
- package/dist/commonjs/react/toUIMessages.js +0 -146
- package/dist/commonjs/react/toUIMessages.js.map +0 -1
- package/dist/commonjs/react/types.d.ts +0 -26
- package/dist/commonjs/react/types.d.ts.map +0 -1
- package/dist/commonjs/react/types.js +0 -2
- package/dist/commonjs/react/types.js.map +0 -1
- package/dist/commonjs/react/useSmoothText.d.ts +0 -20
- package/dist/commonjs/react/useSmoothText.d.ts.map +0 -1
- package/dist/commonjs/react/useSmoothText.js +0 -51
- package/dist/commonjs/react/useSmoothText.js.map +0 -1
- package/dist/commonjs/shared.d.ts +0 -10
- package/dist/commonjs/shared.d.ts.map +0 -1
- package/dist/commonjs/shared.js +0 -30
- package/dist/commonjs/shared.js.map +0 -1
- package/dist/commonjs/validators.d.ts +0 -5721
- package/dist/commonjs/validators.d.ts.map +0 -1
- package/dist/commonjs/validators.js +0 -317
- package/dist/commonjs/validators.js.map +0 -1
- package/dist/commonjs/vitest.config.d.ts +0 -3
- package/dist/commonjs/vitest.config.d.ts.map +0 -1
- package/dist/commonjs/vitest.config.js +0 -7
- package/dist/commonjs/vitest.config.js.map +0 -1
- package/dist/commonjs.tsbuildinfo +0 -1
- package/dist/esm/vitest.config.d.ts +0 -3
- package/dist/esm/vitest.config.d.ts.map +0 -1
- package/dist/esm/vitest.config.js +0 -7
- package/dist/esm/vitest.config.js.map +0 -1
|
@@ -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,E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C,KAAK,CAAC,UAAU,CAAC,GACnB,KAAK,CAAC"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { tool } from "ai";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
/**
|
|
4
|
-
* This is a wrapper around the ai.tool function that adds extra context to the
|
|
5
|
-
* tool call, including the action context, userId, threadId, and messageId.
|
|
6
|
-
* @param tool The tool. See https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling
|
|
7
|
-
* but swap parameters for args and handler for execute.
|
|
8
|
-
* @returns A tool to be used with the AI SDK.
|
|
9
|
-
*/
|
|
10
|
-
export function createTool(t) {
|
|
11
|
-
const args = {
|
|
12
|
-
__acceptsCtx: true,
|
|
13
|
-
ctx: t.ctx,
|
|
14
|
-
description: t.description,
|
|
15
|
-
parameters: t.args,
|
|
16
|
-
async execute(args, options) {
|
|
17
|
-
if (!this.ctx) {
|
|
18
|
-
throw new Error("To use a Convex tool, you must either provide the ctx" +
|
|
19
|
-
" at definition time (dynamically in an action), or use the Agent to" +
|
|
20
|
-
" call it (which injects the ctx, userId and threadId)");
|
|
21
|
-
}
|
|
22
|
-
return t.handler(this.ctx, args, options);
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
return tool(args);
|
|
26
|
-
}
|
|
27
|
-
export function wrapTools(ctx, ...toolSets) {
|
|
28
|
-
const output = {};
|
|
29
|
-
for (const toolSet of toolSets) {
|
|
30
|
-
if (!toolSet) {
|
|
31
|
-
continue;
|
|
32
|
-
}
|
|
33
|
-
for (const [name, tool] of Object.entries(toolSet)) {
|
|
34
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
-
if (!tool.__acceptsCtx) {
|
|
36
|
-
output[name] = tool;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
const out = { ...tool, ctx };
|
|
40
|
-
output[name] = out;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return output;
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=createTool.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createTool.js","sourceRoot":"","sources":["../../../src/client/createTool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAWxB;;;;;;GAMG;AACH,MAAM,UAAU,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,IAAI,CAAC,IAAI,CAAC,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,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,124 +0,0 @@
|
|
|
1
|
-
export const MAX_FILE_SIZE = 1024 * 64;
|
|
2
|
-
/**
|
|
3
|
-
* Store a file in the file storage and return the URL and fileId.
|
|
4
|
-
* @param ctx A ctx object from an action.
|
|
5
|
-
* @param component The agent component.
|
|
6
|
-
* @param blob The blob to store.
|
|
7
|
-
* @param filename The filename to store.
|
|
8
|
-
* @param sha256 The sha256 hash of the file. If not provided, it will be
|
|
9
|
-
* computed. However, to ensure no corruption during transfer, you can
|
|
10
|
-
* calculate this on the client to enforce integrity.
|
|
11
|
-
* @returns The URL, fileId, and storageId of the stored file.
|
|
12
|
-
*/
|
|
13
|
-
export async function storeFile(ctx, component, blob, filename, sha256) {
|
|
14
|
-
if (!("runAction" in ctx)) {
|
|
15
|
-
throw new Error("You're trying to save a file that's too large in a mutation. " +
|
|
16
|
-
"You can store the file in file storage from an action first, then pass a URL instead. " +
|
|
17
|
-
"To have the agent component track the file, you can use `saveFile` from an action then use the fileId with getFile in the mutation. " +
|
|
18
|
-
"Read more in the docs.");
|
|
19
|
-
}
|
|
20
|
-
const hash = sha256 ||
|
|
21
|
-
Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", await blob.arrayBuffer())))
|
|
22
|
-
.map((b) => b.toString(16).padStart(2, "0"))
|
|
23
|
-
.join("");
|
|
24
|
-
const reused = await ctx.runMutation(component.files.useExistingFile, {
|
|
25
|
-
hash,
|
|
26
|
-
filename,
|
|
27
|
-
});
|
|
28
|
-
if (reused) {
|
|
29
|
-
const url = (await ctx.storage.getUrl(reused.storageId));
|
|
30
|
-
return {
|
|
31
|
-
...getParts(url, blob.type, filename),
|
|
32
|
-
file: {
|
|
33
|
-
url,
|
|
34
|
-
fileId: reused.fileId,
|
|
35
|
-
storageId: reused.storageId,
|
|
36
|
-
hash,
|
|
37
|
-
filename,
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
const newStorageId = await ctx.storage.store(blob);
|
|
42
|
-
if (sha256) {
|
|
43
|
-
const metadata = await ctx.storage.getMetadata(newStorageId);
|
|
44
|
-
if (metadata?.sha256 !== sha256) {
|
|
45
|
-
throw new Error("Hash mismatch: " + metadata?.sha256 + " != " + sha256);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
const { fileId, storageId } = await ctx.runMutation(component.files.addFile, {
|
|
49
|
-
storageId: newStorageId,
|
|
50
|
-
hash,
|
|
51
|
-
filename,
|
|
52
|
-
mimeType: blob.type,
|
|
53
|
-
});
|
|
54
|
-
const url = (await ctx.storage.getUrl(storageId));
|
|
55
|
-
if (storageId !== newStorageId) {
|
|
56
|
-
// We're re-using another file's storageId
|
|
57
|
-
// Because we try to reuse the file above, this should be very very rare
|
|
58
|
-
// and only in the case of racing to check then store the file.
|
|
59
|
-
await ctx.storage.delete(newStorageId);
|
|
60
|
-
}
|
|
61
|
-
return {
|
|
62
|
-
...getParts(url, blob.type, filename),
|
|
63
|
-
file: {
|
|
64
|
-
url,
|
|
65
|
-
fileId,
|
|
66
|
-
storageId: storageId,
|
|
67
|
-
hash,
|
|
68
|
-
filename,
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Get file metadata from the component.
|
|
74
|
-
* This also returns filePart (and imagePart if the file is an image),
|
|
75
|
-
* which are useful to construct a CoreMessage like
|
|
76
|
-
* ```ts
|
|
77
|
-
* const { filePart, imagePart } = await getFile(ctx, components.agent, fileId);
|
|
78
|
-
* const message: UserMessage = {
|
|
79
|
-
* role: "user",
|
|
80
|
-
* content: [imagePart ?? filePart],
|
|
81
|
-
* };
|
|
82
|
-
* ```
|
|
83
|
-
* @param ctx A ctx object from an action or query.
|
|
84
|
-
* @param component The agent component, usually `components.agent`.
|
|
85
|
-
* @param fileId The fileId of the file to get.
|
|
86
|
-
* @returns The file metadata and content parts.
|
|
87
|
-
*/
|
|
88
|
-
export async function getFile(ctx, component, fileId) {
|
|
89
|
-
const file = await ctx.runQuery(component.files.get, { fileId });
|
|
90
|
-
if (!file) {
|
|
91
|
-
throw new Error(`File not found in component: ${fileId}`);
|
|
92
|
-
}
|
|
93
|
-
const url = await ctx.storage.getUrl(file.storageId);
|
|
94
|
-
if (!url) {
|
|
95
|
-
throw new Error(`File not found in storage: ${file.storageId}`);
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
...getParts(url, file.mimeType, file.filename),
|
|
99
|
-
file: {
|
|
100
|
-
fileId,
|
|
101
|
-
url,
|
|
102
|
-
storageId: file.storageId,
|
|
103
|
-
hash: file.hash,
|
|
104
|
-
filename: file.filename,
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
function getParts(url, mimeType, filename) {
|
|
109
|
-
const filePart = {
|
|
110
|
-
type: "file",
|
|
111
|
-
data: new URL(url),
|
|
112
|
-
mimeType,
|
|
113
|
-
filename,
|
|
114
|
-
};
|
|
115
|
-
const imagePart = mimeType.startsWith("image/")
|
|
116
|
-
? {
|
|
117
|
-
type: "image",
|
|
118
|
-
image: new URL(url),
|
|
119
|
-
mimeType,
|
|
120
|
-
}
|
|
121
|
-
: undefined;
|
|
122
|
-
return { filePart, imagePart };
|
|
123
|
-
}
|
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