Add bots files handling logic

This commit is contained in:
2026-10-21 17:56:09 +01:00
parent b0af4db1bf
commit d17a4a7bdd
97 changed files with 5843 additions and 41 deletions
@@ -0,0 +1,62 @@
import { z } from "zod";
import { AppData } from "@/src/data/app-data";
import listBotFiles from "@/src/functions/backend/db/bot-files/list-bot-files";
import type { HarnessTool } from "../../types";
import { fileRef, formatBytes, type StorageToolContext } from "./storage-tool-context";
type Args = {
folder?: string;
search?: string;
recursive?: boolean;
scope?: string;
};
/**
* Lists a bot's files: one folder's, or everything under it, or whatever
* matches a name. Each file comes with its ref so the bot can read it.
*/
export default function listFilesTool(context: StorageToolContext): HarnessTool<Args> {
return {
name: "list_files",
description: `List the files you have saved, newest first: one folder's with "folder", everything with "recursive", or the ones whose name or path has "search". Each line has its path, size and ref (e.g. [file:12]) to read it.`,
schema: z.object({
folder: z.string().max(AppData.BotFilePathMaxChars).optional(),
search: z.string().max(120).optional(),
recursive: z.boolean().optional(),
scope: z
.string()
.optional()
.describe(`"bot:<id>" or "workspace:<id>", to look in one of your directories only`),
}),
run: async ({ args }) => {
const folder = args.folder?.trim() || "";
const files = await listBotFiles({
scopes: context.scopes,
folder,
recursive: args.recursive,
search: args.search,
});
if (!files.length) {
return folder
? `Nothing in ${folder}.`
: `You have no files yet. Write one with write_file.`;
}
const lines = files.map((file) => {
const where = file.scope == "workspace" ? ` (workspace ${file.owner_id})` : "";
const ingested = file.ingested_at ? "" : " - not searchable yet";
return `- ${file.path} - ${formatBytes({ bytes: Number(file.size) || 0 })}${where}${ingested} ${fileRef({ file_id: file.id! })}`;
});
const more =
files.length >= AppData.BotFileListMaxItems
? `\n(Showing the first ${AppData.BotFileListMaxItems}. Narrow it with "folder" or "search".)`
: "";
return `${files.length} file(s):\n${lines.join("\n")}${more}`;
},
};
}