Convex Agents Files by sstobo/convex-skills
npx skills add https://github.com/sstobo/convex-skills --skill 'Convex Agents Files'文件和图像让智能体能够理解和生成视觉内容。涵盖上传、存储、附加到消息以及管理文件生命周期。
import { storeFile } from "@convex-dev/agent";
export const uploadFile = action({
args: { fileData: v.string(), filename: v.string(), mimeType: v.string() },
handler: async (ctx, { fileData, filename, mimeType }) => {
const bytes = Buffer.from(fileData, "base64");
const { file } = await storeFile(
ctx,
components.agent,
new Blob([bytes], { type: mimeType }),
{ filename, sha256: "hash" }
);
return {
fileId: file.fileId,
url: file.url,
storageId: file.storageId,
};
},
});
先上传,然后发送带附件的消息:
import { saveMessage, getFile } from "@convex-dev/agent";
// 步骤 1:保存带文件的消息
export const submitFileQuestion = mutation({
args: { threadId: v.string(), fileId: v.string(), question: v.string() },
handler: async (ctx, { threadId, fileId, question }) => {
const { imagePart, filePart } = await getFile(ctx, components.agent, fileId);
const { messageId } = await saveMessage(ctx, components.agent, {
threadId,
message: {
role: "user",
content: [
imagePart ?? filePart,
{ type: "text", text: question },
],
},
metadata: { fileIds: [fileId] },
});
return { messageId };
},
});
// 步骤 2:生成响应
export const generateFileResponse = action({
args: { threadId: v.string(), promptMessageId: v.string() },
handler: async (ctx, { threadId, promptMessageId }) => {
const { thread } = await myAgent.continueThread(ctx, { threadId });
await thread.generateText({ promptMessageId });
},
});
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
在生成过程中直接传递文件:
export const analyzeImageInline = action({
args: { threadId: v.string(), imageData: v.string(), question: v.string() },
handler: async (ctx, { threadId, imageData, question }) => {
const { thread } = await myAgent.continueThread(ctx, { threadId });
await thread.generateText({
message: {
role: "user",
content: [
{
type: "image",
image: Buffer.from(imageData, "base64"),
mimeType: "image/png",
},
{ type: "text", text: question },
],
},
});
},
});
export const generateAndSaveImage = action({
args: { threadId: v.string(), prompt: v.string() },
handler: async (ctx, { threadId, prompt }) => {
const { image } = await generateImage({
model: openai.image("dall-e-2"),
prompt,
});
const { file } = await storeFile(ctx, components.agent, image, {
filename: `generated-${Date.now()}.png`,
});
return { fileId: file.fileId };
},
});
每周安装次数
–
代码仓库
GitHub 星标数
23
首次出现时间
–
安全审计
Files and images let agents understand and generate visual content. Covers uploading, storing, attaching to messages, and managing file lifecycle.
import { storeFile } from "@convex-dev/agent";
export const uploadFile = action({
args: { fileData: v.string(), filename: v.string(), mimeType: v.string() },
handler: async (ctx, { fileData, filename, mimeType }) => {
const bytes = Buffer.from(fileData, "base64");
const { file } = await storeFile(
ctx,
components.agent,
new Blob([bytes], { type: mimeType }),
{ filename, sha256: "hash" }
);
return {
fileId: file.fileId,
url: file.url,
storageId: file.storageId,
};
},
});
Upload first, then send message with attachment:
import { saveMessage, getFile } from "@convex-dev/agent";
// Step 1: Save message with file
export const submitFileQuestion = mutation({
args: { threadId: v.string(), fileId: v.string(), question: v.string() },
handler: async (ctx, { threadId, fileId, question }) => {
const { imagePart, filePart } = await getFile(ctx, components.agent, fileId);
const { messageId } = await saveMessage(ctx, components.agent, {
threadId,
message: {
role: "user",
content: [
imagePart ?? filePart,
{ type: "text", text: question },
],
},
metadata: { fileIds: [fileId] },
});
return { messageId };
},
});
// Step 2: Generate response
export const generateFileResponse = action({
args: { threadId: v.string(), promptMessageId: v.string() },
handler: async (ctx, { threadId, promptMessageId }) => {
const { thread } = await myAgent.continueThread(ctx, { threadId });
await thread.generateText({ promptMessageId });
},
});
Pass file directly in generation:
export const analyzeImageInline = action({
args: { threadId: v.string(), imageData: v.string(), question: v.string() },
handler: async (ctx, { threadId, imageData, question }) => {
const { thread } = await myAgent.continueThread(ctx, { threadId });
await thread.generateText({
message: {
role: "user",
content: [
{
type: "image",
image: Buffer.from(imageData, "base64"),
mimeType: "image/png",
},
{ type: "text", text: question },
],
},
});
},
});
export const generateAndSaveImage = action({
args: { threadId: v.string(), prompt: v.string() },
handler: async (ctx, { threadId, prompt }) => {
const { image } = await generateImage({
model: openai.image("dall-e-2"),
prompt,
});
const { file } = await storeFile(ctx, components.agent, image, {
filename: `generated-${Date.now()}.png`,
});
return { fileId: file.fileId };
},
});
Weekly Installs
–
Repository
GitHub Stars
23
First Seen
–
Security Audits
AI Elements:基于shadcn/ui的AI原生应用组件库,快速构建对话界面
58,500 周安装