Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { userError } from "../user-errors.ts";
import { basename, join } from "node:path";
import { pipeline } from "node:stream/promises";
import type { BotsResponse, SearchResponse, WireBot, WireGroup } from "../../shared/wire.ts";
import { NO_SUCH_BOT } from "../../shared/bot-read.ts";
import { closeUnfinishedCodeFence } from "../../shared/markdown-code-fence.ts";
import { KEPT_ORIGINALS_DIR, keptOriginal } from "../attachment-originals.ts";
import {
ATTACHMENTS_DIR, attachmentStorageUsage, clearAttachments, deleteAttachment, extensionForMime, FILE_MAX_BYTES, IMAGE_MAX_BYTES,
parseAudioRange, readAttachmentAsync, saveFile, saveImageUpload, validateAttachmentUploadId, type SavedAttachment,
} from "../attachments.ts";
import { saveXmindUpload, XMIND_MIME } from "../xmind.ts";
import { deliveredCopy } from "../bot-attachment.ts";
import { memberBot, memberGroup, type Viewer, type VisibleSet } from "../bot-visibility.ts";
import type { ComputerControl } from "../computer-control.ts";
import { speakerOf, type PersonName } from "../replies.ts";
import type { Coordinator } from "../coordinator.ts";
import type { WorkspaceAccess } from "../enterprise.ts";
import type { GroupUsageReader } from "../group-thread-usage.ts";
import { readMessage, searchMessagesAsync } from "../message-db.ts";
import { threadGoalPage } from "../message-db-goals.ts";
import { cappedMediaRange, MessageMediaLinks, STREAMABLE_MEDIA_MIMES } from "../message-media-links.ts";
import {
canOpenOutsideWorkspaceMessageFile, listMessageFolder, MESSAGE_FILE_DOWNLOAD_MAX_BYTES, MESSAGE_FILE_MAX_BYTES, messageAttachmentName, messageFileDisposition, messageFileDownloadName, messageFolderEntryName,
messageImageTargetAt, messageReferencesFile, openMessageFile, openMessageFolderEntry, openOutsideWorkspaceMessageFile, referencedFileName,
} from "../message-file.ts";
import { ownerOrAdmin, resolveRequestAuth, type RequestAuth, type ResolveOptions } from "../request-auth.ts";
import type { queuedSteerSnapshot } from "../steer-queue.ts";
import type { BotRecord, GroupRecord, Message, Store } from "../store.ts";
import { answerAsleep, backgroundWhileAsleep } from "../cloud-power.ts";
import { heldTail } from "../host/snapshot-tail.ts";
import { createJobRoutes } from "./jobs.ts";
import { PASS, type RouteContext, type RouteHandler } from "./table.ts";
const UPLOADED_NAME = /^[A-Za-z0-9-]+\.[A-Za-z0-9]{1,10}$/;
export type MessagePage = { messages: Array<Message | Record<string, unknown>>; hasMore?: boolean; activeLeafId: string | null };
export interface ThreadRouteDeps {
store: Pick<Store,
| "activeLeaf" | "activePath" | "bot" | "botByThread" | "bots" | "groupByThread" | "groupTaskByThread" | "groups" | "messagesFor"
| "onChange" | "sections" | "taskByThread" | "tasks">;
coordinator: Pick<Coordinator, "job" | "node" | "stopHandoff">;
stopRefusal?(auth: RequestAuth, threadId: string): string | null;
groupThreadIsWorking(groupId: string, threadId: string): boolean;
/** What people's lines are called in this conversation (replies.ts PersonName). */
userName(threadId: string): PersonName;
sessions: ResolveOptions["sessions"];
groupUsageReader: Pick<GroupUsageReader, "refresh">;
hostedWorkspace: boolean;
sessionCookie: string;
loopback: { trust: ResolveOptions["loopbackTrust"]; localBrowsers: boolean };
lendingEnabled(): boolean;
workspaceAccess(): WorkspaceAccess | null;
desktopMutationToken(): string | undefined;
companionMutationToken(): string | undefined;
cliOwnerToken(): string | undefined;
defaultPage: number;
pageSize(raw: string | null): number | null | undefined;
messagePage(threadId: string, limit: number | undefined): MessagePage;
messagePageBefore(threadId: string, before: string, limit: number): MessagePage | null;
messageWindow(threadId: string, messageId: string, limit: number): { messages: Array<Message | Record<string, unknown>>; hasMore: boolean; activeLeafId: string | null } | null;
publicBotQueuedMessages(): ReturnType<typeof queuedSteerSnapshot>;
publicGroupState(record: GroupRecord): WireGroup;
wireBot(bot: BotRecord): WireBot;
botComputerControlSnapshot(botId: string): ReturnType<ComputerControl["snapshot"]>;
messageFileRootsForThread(senderId: string, threadId: string): string[];
viewerFor(auth: RequestAuth): Viewer;
/** A room's unread mark as this person has read it (a Team Cloud); the room as it is anywhere else. */
groupFor?(auth: RequestAuth, group: WireGroup): WireGroup;
/** The bots in this person's own order (a Team Cloud's User); the team's order anywhere else. */
botOrderFor?(auth: RequestAuth, bots: readonly BotRecord[]): readonly BotRecord[];
visibleTo(viewer: Viewer): VisibleSet;
/** A Team Cloud: gives a member their own MausBot once, before their first list of bots. */
memberDefaultBot?(email: string): unknown;
/** Who uploaded each file: only its uploader discards it, and only while nothing uses it. */
uploads: { note(auth: RequestAuth, name: string): void; owns(auth: RequestAuth, name: string): boolean };
attachmentInUse(name: string): boolean;
/** The stored files a Settings clear must leave (pictures, banner, routine and calendar-call files), null until those load; absent on a Team Cloud, which has no clear. */
keptAttachments?(): ReadonlySet<string> | null;
/** Directories a self-hosted owner still cannot open via single outside-workspace file links (app data dir, user data). */
ownerOutsideFileRefuse?: readonly string[];
}
export function createThreadRoutes(deps: ThreadRouteDeps): RouteHandler {
const {
store, userName: userNameFor, sessions, groupUsageReader, lendingEnabled,
hostedWorkspace: HOSTED_WORKSPACE, sessionCookie: SESSION_COOKIE, loopback: LOOPBACK, defaultPage: DEFAULT_PAGE,
pageSize, messagePage, messagePageBefore, messageWindow, publicBotQueuedMessages, publicGroupState, wireBot,
botComputerControlSnapshot, messageFileRootsForThread, viewerFor, visibleTo,
} = deps;
const mediaLinks = new MessageMediaLinks();
// tasks stays explicit: clients expect [] where wireBot() would omit the key.
const shownBot = (bot: BotRecord, visible: VisibleSet, limit: number | undefined, tail?: number) => {
const wired = wireBot(bot);
// Narrowed first: on a Team Cloud the selected conversation may be someone else's, and its words never go out.
const shown = memberBot({ ...wired, tasks: wired.tasks ?? [] }, visible);
const threadId = shown.threadId;
if (typeof threadId !== "string") return { ...shown, messages: [], hasMore: false, activeLeafId: null };
const cursor = shown.tasks?.find((task) => task.threadId === threadId)?.lastReadMessageId;
return { ...shown, ...(tail !== undefined && limit !== undefined && tail < limit ? heldTail((size) => messagePage(threadId, size), tail, limit, cursor) : messagePage(threadId, limit)) };
};
const jobs = createJobRoutes({ store, coordinator: deps.coordinator, groupThreadIsWorking: deps.groupThreadIsWorking, viewerFor, visibleTo, sessions, ...(deps.stopRefusal ? { stopRefusal: deps.stopRefusal } : {}) });
const handle = async ({ req, res, url, path, method, auth, json, readBody }: RouteContext): Promise<unknown> => {
let m: RegExpMatchArray | null = null;
const member = auth.kind === "session" && auth.session.membershipAuthority === "team" ? auth.session.email : undefined;
if (member && method === "GET" && path === "/api/bots") deps.memberDefaultBot?.(member);
const visible = visibleTo(viewerFor(auth));
const workspaceAccess = deps.workspaceAccess();
const desktopMutationToken = deps.desktopMutationToken();
const companionMutationToken = deps.companionMutationToken();
const cliOwnerToken = deps.cliOwnerToken();
if (method === "GET" && path === "/api/bots") {
if (backgroundWhileAsleep(path, req.headers)) return answerAsleep(res, json);
try { groupUsageReader.refresh(); } catch { }
const limit = pageSize(url.searchParams.get("messages"));
if (limit === null) return json(res, 400, { error: "messages must be a non-negative whole number" });
const tail = pageSize(url.searchParams.get("tail"));
if (tail === null) return json(res, 400, { error: "tail must be a non-negative whole number" });
const open = new Set(url.searchParams.get("open")?.split(",") ?? []);
const shownBots = (deps.botOrderFor?.(auth, store.bots) ?? store.bots).filter((bot) => visible.bot(bot.id));
const queued = publicBotQueuedMessages();
return json(res, 200, {
bots: shownBots.map((bot) => shownBot(bot, visible, limit, open.has(bot.id) ? undefined : tail)),
botQueuedMessages: visible.everything ? queued : Object.fromEntries(Object.entries(queued).filter(([threadId]) => visible.thread(threadId))),
sections: visible.sections(store.sections),
groups: store.groups.filter((g) => visible.group(g.id)).map((g) => {
const group = publicGroupState(g);
const room = { ...(deps.groupFor?.(auth, group) ?? group), ...messagePage(g.threadId, limit) };
return visible.everything || !visible.member ? room : memberGroup(room);
}),
computerControl: Object.fromEntries(
shownBots.map((bot) => {
const snapshot = botComputerControlSnapshot(bot.id);
// A plea from someone else's private chat (a Team Cloud) names its site and reason: held is all that is shared.
if (snapshot.help && visible.hides(snapshot.help.threadId)) return [bot.id, { held: snapshot.held, helpReason: null }];
return [bot.id, { held: snapshot.held, helpReason: snapshot.helpReason, ...(snapshot.help ? { help: snapshot.help } : {}) }];
}),
),
together: { newBot: true },
} satisfies BotsResponse<MessagePage["messages"][number]>);
}
m = path.match(/^\/api\/bots\/([\w-]+)$/);
if (m && method === "GET") {
const bot = store.bot(m[1]);
if (!bot || !visible.bot(bot.id)) return json(res, 404, { error: NO_SUCH_BOT });
const limit = pageSize(url.searchParams.get("messages"));
if (limit === null) return json(res, 400, { error: "messages must be a non-negative whole number" });
return json(res, 200, { bot: shownBot(bot, visible, limit) });
}
m = path.match(/^\/api\/threads\/([\w-]+)\/goals$/);
if (m && method === "GET") {
const threadId = m[1]!;
res.setHeader("cache-control", "private, no-store");
if (!visible.thread(threadId) || (!store.botByThread(threadId) && !store.groupByThread(threadId))) {
return json(res, 404, { error: "no such conversation" });
}
if (backgroundWhileAsleep(path, req.headers)) return answerAsleep(res, json);
const limit = pageSize(url.searchParams.get("limit"));
if (limit === null || limit === 0) return json(res, 400, { error: "limit must be a positive whole number" });
const page = threadGoalPage(threadId, limit ?? DEFAULT_PAGE, url.searchParams.get("before"));
return page ? json(res, 200, page) : json(res, 404, { error: "no such goal" });
}
m = path.match(/^\/api\/threads\/([\w-]+)\/messages$/);
if (m && method === "GET") {
const threadId = m[1];
if (!store.botByThread(threadId) && !store.groupByThread(threadId)) {
return json(res, 404, { error: "no such conversation" });
}
const limit = pageSize(url.searchParams.get("limit"));
if (limit === null) return json(res, 400, { error: "limit must be a non-negative whole number" });
const before = url.searchParams.get("before");
const around = url.searchParams.get("around");
if (before && around) return json(res, 400, { error: "before and around cannot be combined" });
if (around) {
const window = messageWindow(threadId, around, limit ?? DEFAULT_PAGE);
if (!window) return json(res, 404, { error: "no such message" });
return json(res, 200, window);
}
// An unknown cursor must not answer with the newest page: the client would paginate in a circle.
if (before) {
const page = messagePageBefore(threadId, before, limit ?? DEFAULT_PAGE);
return page ? json(res, 200, page) : json(res, 404, { error: "no such message" });
}
return json(res, 200, messagePage(threadId, limit ?? DEFAULT_PAGE));
}
m = path.match(/^\/api\/threads\/([\w-]+)\/messages\/([\w-]+)\/image$/);
if (m && method === "GET") {
// messagesFor caches a ThreadState for any id it is handed, so unknown ids 404 first.
if (!store.botByThread(m[1]) && !store.groupByThread(m[1])) {
return json(res, 404, { error: "no such conversation" });
}
const message = store.messagesFor(m[1]).find((msg) => msg.id === m![2]);
if (!message?.png) return json(res, 404, { error: "no image on that message" });
const bytes = Buffer.from(message.png, "base64");
res.writeHead(200, {
"content-type": message.mime ?? "image/png",
"content-length": String(bytes.byteLength),
"cache-control": "private, no-store",
});
return res.end(bytes);
}
m = path.match(/^\/api\/threads\/([\w-]+)\/messages\/([\w-]+)\/tool$/);
if (m && method === "GET") {
if (!store.botByThread(m[1]) && !store.groupByThread(m[1])) return json(res, 404, userError("serverError.chat.missingConversation"));
const message = readMessage(m[1], m[2]);
if (message?.tool?.output === undefined) return json(res, 404, { error: "no tool output on that message" });
return json(res, 200, { output: message.tool.output });
}
// Only a file this exact stored message grants; deliberately not a general path reader.
m = path.match(/^\/api\/threads\/([\w-]+)\/messages\/([\w-]+)\/file$/);
const streamsMessageImage = Boolean(
m && method === "GET" && url.searchParams.get("preview") === "1",
);
// A <video> element can only GET, so a POST first trades its body for a link id.
const mediaId = m && method === "GET" && !streamsMessageImage ? url.searchParams.get("media") : null;
if (m && (method === "POST" || streamsMessageImage || mediaId)) {
if (method === "POST" && !String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) {
return json(res, 415, { error: "content-type must be application/json" });
}
const threadId = m[1]!;
const directBot = store.botByThread(threadId);
const group = directBot ? undefined : store.groupByThread(threadId);
if (!directBot && !group) return json(res, 404, { error: "no such conversation" });
const message = store.messagesFor(threadId).find((candidate) => candidate.id === m![2]);
if (!message) return json(res, 404, { error: "no such message" });
if (!visible.thread(threadId)) return json(res, 404, { error: "no such conversation" });
// A non-text card (a hand-off result is an activity message) shares only what it explicitly attaches.
const attachmentsOnly = message.kind !== "text";
if ((attachmentsOnly && !message.attachments?.length) || (!message.text && !message.attachments?.length)) {
return json(res, 403, { error: "that message does not share this file" });
}
const messageText = message.text ?? "";
// Only this viewer's link for this message; every check below runs again on what it stands for.
const mediaGrant = mediaId ? mediaLinks.use(mediaId, threadId, message.id, auth) : null;
if (mediaId && !mediaGrant) return json(res, 404, { error: "that video link has expired", code: "media_link_expired" });
const body = method === "POST" ? await readBody(req) : null;
// Access may have been revoked while the body was in flight; ask again before any file is opened.
if (body && !visibleTo(viewerFor(auth)).thread(threadId)) return json(res, 404, { error: "no such conversation" });
const asksMediaLink = body?.media === true;
const rawReference = streamsMessageImage ? url.searchParams.get("ref") : null;
if (streamsMessageImage && (!rawReference || !/^\d+$/.test(rawReference))) {
return json(res, 400, { error: "ref must identify a rendered image" });
}
const href = mediaGrant
? mediaGrant.path
: method === "POST"
? (typeof body.path === "string" ? body.path : "")
: messageImageTargetAt(messageText, Number(rawReference));
if (!href) return json(res, 400, { error: "path is required" });
let roots: string[];
let downloadName: string | undefined;
const botAttachment = message.role === "bot"
? message.attachments?.find((attachment) => attachment.path === href)
: undefined;
if (attachmentsOnly && !botAttachment) {
return json(res, 403, { error: "that message does not share this file" });
}
if (message.role === "user") {
downloadName = messageAttachmentName(messageText, href) ?? undefined;
if (!downloadName) {
return json(res, 403, { error: "that message does not share this file" });
}
roots = [ATTACHMENTS_DIR];
} else if (botAttachment) {
downloadName = botAttachment.kind === "file" ? botAttachment.name : undefined;
roots = [ATTACHMENTS_DIR];
} else {
if (!messageReferencesFile(messageText, href)) {
return json(res, 403, { error: "that bot message does not link to this file" });
}
const senderId = directBot?.id ?? message.from?.botId;
// Membership is not consulted: removing a bot must not break files it already shared.
if (!senderId) {
return json(res, 403, { error: "the file's bot author could not be verified" });
}
const runThread = message.automation?.executionThreadId;
const fromRun = runThread && store.botByThread(runThread)?.id === senderId ? messageFileRootsForThread(senderId, runThread) : [];
roots = [...new Set([...messageFileRootsForThread(senderId, threadId), ...fromRun])];
}
const folderEntry = mediaGrant
? mediaGrant.entry
: method === "POST" && body.entry !== undefined
? messageFolderEntryName(body.entry)
: undefined;
if (folderEntry === null) return json(res, 400, { error: "entry must be one file name" });
// An inline image is read whole by the browser; a saved file is streamed below, so it may be larger.
const maxBytes = streamsMessageImage ? MESSAGE_FILE_MAX_BYTES : MESSAGE_FILE_DOWNLOAD_MAX_BYTES;
const ownerCanOpenOutside = Boolean(deps.ownerOutsideFileRefuse) && !HOSTED_WORKSPACE && ownerOrAdmin(auth) && method === "POST" && message.role === "bot" && !botAttachment && !folderEntry && !asksMediaLink;
let file: Awaited<ReturnType<typeof openMessageFile>> | undefined;
try {
try {
// What the person sent, even when a bot has since saved over the stored file.
const kept = message.role === "user" && !folderEntry ? keptOriginal(href, ATTACHMENTS_DIR) : null;
file = folderEntry
? await openMessageFolderEntry(href, folderEntry, roots, maxBytes)
: kept ? await openMessageFile(kept, [KEPT_ORIGINALS_DIR], maxBytes) : await openMessageFile(href, roots, maxBytes);
// Says the bytes are exactly what the person sent, so a comparison may call them that.
if (kept) res.setHeader("x-openmausbot-kept-original", "1");
} catch (error) {
const code = (error as { code?: unknown } | null)?.code;
const status = (error as { status?: unknown } | null)?.status;
const name = message.role === "bot" && !botAttachment && !folderEntry && (code === "outside_workspace" || status === 404)
? referencedFileName(href)
: null;
const copy = name ? deliveredCopy(store.messagesFor(threadId), message, name, href) : null;
if (copy) {
file = await openMessageFile(copy.path, [ATTACHMENTS_DIR], maxBytes);
downloadName = copy.name;
} else if (code === "outside_workspace" && ownerCanOpenOutside) {
if (body?.allowOutsideWorkspace === true && body?.listFolder !== true) {
file = await openOutsideWorkspaceMessageFile(href, deps.ownerOutsideFileRefuse!, maxBytes);
} else if (await canOpenOutsideWorkspaceMessageFile(href, deps.ownerOutsideFileRefuse!)) {
return json(res, 403, { error: error instanceof Error ? error.message : "the linked file is outside this conversation's workspace", code: "outside_workspace", canOpenOutside: true });
} else {
throw error;
}
} else {
// Only a client that asks for listings gets one; older clients keep the 400.
if (body?.listFolder !== true || folderEntry || code !== "directory") throw error;
const listing = await listMessageFolder(href, roots);
res.setHeader("x-openmausbot-folder", "1");
res.setHeader("cache-control", "private, no-store");
return json(res, 200, listing);
}
}
if (!file || res.destroyed) return;
if ((streamsMessageImage || botAttachment?.kind === "image") && !file.mime.startsWith("image/")) {
return json(res, 415, { error: "only images can be previewed here" });
}
if ((asksMediaLink || mediaGrant) && !STREAMABLE_MEDIA_MIMES.has(file.mime)) {
return json(res, 415, { error: "only videos and audio can be played here" });
}
if (asksMediaLink) {
const id = mediaLinks.issue({ threadId, messageId: message.id, path: href, ...(folderEntry ? { entry: folderEntry } : {}) }, auth);
res.setHeader("x-openmausbot-media", "1");
res.setHeader("cache-control", "private, no-store");
return json(res, 200, { url: `/api/threads/${encodeURIComponent(threadId)}/messages/${encodeURIComponent(message.id)}/file?media=${id}`, mime: file.mime, bytes: file.bytes });
}
const inline = streamsMessageImage || Boolean(mediaGrant);
const rawRange = Array.isArray(req.headers.range) ? req.headers.range[0] : req.headers.range;
// Without a Range header (Save As) the whole file streams; a player's range never ties up more than one chunk.
const range = mediaGrant ? cappedMediaRange(parseAudioRange(rawRange, file.bytes)) : { kind: "none" as const };
if (range.kind === "unsatisfiable") {
res.writeHead(416, { "content-range": `bytes */${file.bytes}`, "cache-control": "private, no-store", "x-content-type-options": "nosniff" });
return res.end();
}
const [start, end] = range.kind === "range" ? [range.start, range.end] : [0, file.bytes - 1];
res.writeHead(range.kind === "range" ? 206 : 200, {
"content-type": file.mime,
"content-length": String(end - start + 1),
...(range.kind === "range" ? { "content-range": `bytes ${start}-${end}/${file.bytes}` } : {}),
...(mediaGrant ? { "accept-ranges": "bytes" } : {}),
"content-disposition": mediaGrant
? messageFileDisposition(messageFileDownloadName(downloadName, file.name), "inline")
: inline ? "inline" : messageFileDisposition(messageFileDownloadName(downloadName, file.name)),
"cache-control": "private, no-store",
"cdn-cache-control": "no-store",
"cloudflare-cdn-cache-control": "no-store",
pragma: "no-cache",
vary: "Authorization",
"x-content-type-options": "nosniff",
...(inline
? {
"cross-origin-resource-policy": "same-origin",
"referrer-policy": "no-referrer",
}
: {}),
});
if (file.bytes === 0) return res.end();
try {
await pipeline(file.handle.createReadStream({ start, end, autoClose: true }), res);
} catch {
res.destroy();
}
} finally {
await file?.handle.close();
}
return;
}
// Raw bytes, not base64 JSON; retrying a UUID uploadId returns the same committed path.
if (method === "POST" && path === "/api/attachments") {
let uploadId: string | undefined;
try {
uploadId = validateAttachmentUploadId(url.searchParams.get("uploadId") ?? undefined);
} catch (error) {
req.resume();
throw error;
}
const rawType = Array.isArray(req.headers["content-type"]) ? req.headers["content-type"][0] : req.headers["content-type"];
const mime = rawType?.split(";")[0]?.trim().toLowerCase();
if (!mime || !extensionForMime(mime)) {
return json(res, 400, { error: "content-type must be an image type" });
}
const rawLength = Array.isArray(req.headers["content-length"])
? req.headers["content-length"][0]
: req.headers["content-length"];
const declaredLength = rawLength === undefined ? undefined : Number(rawLength);
if (declaredLength !== undefined && (!Number.isSafeInteger(declaredLength) || declaredLength < 0)) {
req.resume();
return json(res, 400, { error: "content-length must be a non-negative integer" });
}
if (declaredLength !== undefined && declaredLength > IMAGE_MAX_BYTES) {
req.resume();
return json(res, 413, { error: `image exceeds ${IMAGE_MAX_BYTES} bytes` });
}
const saved = await new Promise<SavedAttachment>((resolve, reject) => {
const chunks: Buffer[] = [];
let received = 0;
let settled = false;
const fail = (status: number, msg: string) => {
if (settled) return;
settled = true;
reject(Object.assign(new Error(msg), { status }));
};
req.on("data", (chunk: Buffer) => {
if (settled) return;
received += chunk.byteLength;
if (received > IMAGE_MAX_BYTES) return fail(413, `image exceeds ${IMAGE_MAX_BYTES} bytes`);
chunks.push(chunk);
});
req.on("end", async () => {
if (settled) return;
settled = true;
try {
resolve(await saveImageUpload(Buffer.concat(chunks), mime, uploadId));
} catch (e) {
reject(e instanceof Error ? e : new Error(String(e)));
}
});
req.on("error", (e) => fail(400, e instanceof Error ? e.message : String(e)));
});
deps.uploads.note(auth, basename(saved.path));
return json(res, 201, saved);
}
// Raw bytes; a UUID uploadId is stable across retries, and the quota rejects rather than delete files old prompts reference.
if (method === "POST" && path === "/api/files") {
let uploadId: string | undefined;
try {
uploadId = validateAttachmentUploadId(url.searchParams.get("uploadId") ?? undefined);
} catch (error) {
req.resume();
throw error;
}
const name = url.searchParams.get("name");
if (!name) {
req.resume();
return json(res, 400, { error: "name is required" });
}
const rawType = Array.isArray(req.headers["content-type"])
? req.headers["content-type"][0]
: req.headers["content-type"];
const rawLength = Array.isArray(req.headers["content-length"])
? req.headers["content-length"][0]
: req.headers["content-length"];
const declaredLength = rawLength === undefined ? undefined : Number(rawLength);
if (declaredLength !== undefined && (!Number.isSafeInteger(declaredLength) || declaredLength < 0)) {
req.resume();
return json(res, 400, { error: "content-length must be a non-negative integer" });
}
if (declaredLength !== undefined && declaredLength > FILE_MAX_BYTES) {
req.resume();
return json(res, 413, { error: `file exceeds ${FILE_MAX_BYTES} bytes` });
}
try {
// Returning must not destroy the socket: the caller still sends the 4xx when the byte count crosses the limit.
const chunks = req.iterator({ destroyOnReturn: false }) as AsyncIterable<Buffer>;
const saved = rawType?.split(";")[0]!.trim().toLowerCase() === XMIND_MIME
? await saveXmindUpload(chunks, name, { uploadId })
: await saveFile(chunks, name, rawType ?? "", { uploadId, expectedBytes: declaredLength, keepOriginal: true });
deps.uploads.note(auth, basename(saved.path));
return json(res, 201, saved);
} catch (error) {
req.resume();
throw error;
}
}
// Not under /api/attachments/: that GET is open to clients. This path keeps the default admin scope.
if (path === "/api/attachment-storage" && deps.keptAttachments) {
if (method === "GET") return json(res, 200, attachmentStorageUsage());
if (method === "POST") {
// Until routines load their files are unknown, and a clear could take them.
const kept = deps.keptAttachments();
if (!kept) return json(res, 503, { error: "Routines are still loading. Try again in a moment." });
return json(res, 200, { ...clearAttachments(kept), ...attachmentStorageUsage() });
}
}
// Name-locked to the attachments dir: readAttachment refuses anything but a bare generated filename.
m = path.match(/^\/api\/attachments\/([\w.-]+)$/);
if (m && method === "DELETE") {
const name = m[1]!;
if (!UPLOADED_NAME.test(name) || !deps.uploads.owns(auth, name)) return json(res, 404, { error: "no such attachment" });
if (deps.attachmentInUse(name)) return json(res, 409, { ...userError("serverError.attachment.alreadyInMessage") });
deleteAttachment(join(ATTACHMENTS_DIR, name));
return json(res, 200, { ok: true });
}
if (m && method === "GET") {
const attachment = await readAttachmentAsync(m[1]!);
if (!attachment) return json(res, 404, { error: "no such attachment" });
const size = attachment.bytes.byteLength;
// Visibility is decided per request, so the browser must never reuse one member's copy for another identity.
const headers = {
"content-type": attachment.mime,
"cache-control": "private, no-store",
"x-content-type-options": "nosniff",
};
if (attachment.mime.startsWith("audio/")) {
const rawRange = Array.isArray(req.headers.range) ? req.headers.range[0] : req.headers.range;
const range = parseAudioRange(rawRange, size);
if (range.kind === "unsatisfiable") {
res.writeHead(416, { ...headers, "content-range": `bytes */${size}` });
return res.end();
}
if (range.kind === "range") {
res.writeHead(206, {
...headers,
"content-length": String(range.end - range.start + 1),
"content-range": `bytes ${range.start}-${range.end}/${size}`,
});
return res.end(attachment.bytes.subarray(range.start, range.end + 1));
}
}
res.writeHead(200, { ...headers, "content-length": String(size) });
return res.end(attachment.bytes);
}
if (method === "GET" && path === "/api/search") {
const q = url.searchParams.get("q") ?? "";
const rawLimit = url.searchParams.get("limit");
const limit = rawLimit ? Math.min(Math.max(Number(rawLimit) || 0, 1), 100) : 40;
const threadId = url.searchParams.get("threadId")?.trim() || undefined;
if (threadId && ((!store.botByThread(threadId) && !store.groupByThread(threadId)) || !visible.thread(threadId))) {
return json(res, 404, { error: "no such conversation" });
}
const activePaths = new Map<string, Set<string>>();
const onActivePath = (threadId: string, messageId: string) => {
let ids = activePaths.get(threadId);
if (!ids) activePaths.set(threadId, (ids = new Set(store.activePath(threadId).map((m) => m.id))));
return ids.has(messageId);
};
const found = await searchMessagesAsync(q, visible.everything ? limit : Math.min(limit * 10, 1_000), threadId);
// A scan may outlive a revocation or audience edit: authorize again after the await.
if (HOSTED_WORKSPACE && auth.kind === "session") {
const failure = workspaceAccess ? await workspaceAccess.authorize(req, auth)
: { status: 503, error: "Workspace sign-in is unavailable." };
if (failure) return json(res, failure.status, { error: failure.error });
}
const current = resolveRequestAuth(req, {
sessions, cookieName: SESSION_COOKIE, streamPath: "/api/events", url,
loopbackMutationToken: desktopMutationToken, companionMutationToken,
features: { sharedComputers: lendingEnabled() }, loopbackTrust: LOOPBACK.trust, cliOwnerToken, trustLocalBrowser: LOOPBACK.localBrowsers,
});
if (!current.auth) return json(res, current.status, { error: current.error });
const currentVisible = visibleTo(viewerFor(current.auth));
if (threadId && !currentVisible.thread(threadId)) return json(res, 404, { error: "no such conversation" });
const hits = found
.filter((hit) => currentVisible.thread(hit.threadId))
.slice(0, limit)
.map((hit) => {
const bot = store.botByThread(hit.threadId);
const group = bot ? undefined : store.groupByThread(hit.threadId);
if (!bot && !group) return null;
const active = onActivePath(hit.threadId, hit.messageId);
if (bot) {
const task = store.taskByThread(bot.id, hit.threadId);
return { ...hit, botId: bot.id, name: bot.name, task: task?.title, onActivePath: active };
}
if (group) {
const task = store.groupTaskByThread(group.id, hit.threadId);
return { ...hit, groupId: group.id, name: group.name, task: task?.title, onActivePath: active };
}
return null;
})
.filter((hit): hit is NonNullable<typeof hit> => hit !== null);
return json(res, 200, { hits } satisfies SearchResponse);
}
m = path.match(/^\/api\/threads\/([\w-]+)\/export$/);
if (m && method === "GET") {
const threadId = m[1];
const bot = store.botByThread(threadId);
const group = bot ? undefined : store.groupByThread(threadId);
if (!bot && !group) return json(res, 404, { error: "no such conversation" });
const format = url.searchParams.get("format") ?? "markdown";
if (format !== "markdown" && format !== "json") {
return json(res, 400, { error: "format must be markdown or json" });
}
const title = bot
? (store.taskByThread(bot.id, threadId)?.title || bot.name)
: (store.groupTaskByThread(group!.id, threadId)?.title || group!.name);
const filename = (title.replace(/[^\w\- ]+/g, "").trim() || "conversation").slice(0, 60);
const messages = store.activePath(threadId);
if (format === "json") {
const slim = messages.map(({ png: _png, mime: _mime, ...rest }) => rest);
res.writeHead(200, {
"content-type": "application/json",
"content-disposition": `attachment; filename="${filename}.json"`,
});
return res.end(JSON.stringify({ name: title, threadId, messages: slim }, null, 2));
}
const userName = userNameFor(threadId);
const lines: string[] = [`# ${title}`, ""];
for (const msg of messages) {
const sentBy = speakerOf(userName, msg);
const who = msg.role === "user"
? msg.via === "api" ? `${sentBy} (via the local API)` : sentBy
: (msg.from?.name ?? bot?.name ?? "Bot");
if ((msg.kind === "text" || msg.kind === "widget" || msg.kind === "app") && msg.text) lines.push(`**${who}:**`, "", closeUnfinishedCodeFence(msg.text), "");
else if (msg.kind === "activity" && msg.tool) lines.push(`> ${msg.tool.name}`, "");
else if (msg.kind === "screen") lines.push("> [screen capture]", "");
else if (msg.kind === "options" && msg.card) {
lines.push(`> ${msg.card.title}${msg.card.answered ? ` — answered: ${msg.card.answered}` : ""}`, "");
}
}
res.writeHead(200, {
"content-type": "text/markdown; charset=utf-8",
"content-disposition": `attachment; filename="${filename}.md"`,
});
return res.end(lines.join("\n"));
}
return PASS;
};
return async (ctx) => (await jobs(ctx) !== PASS || await handle(ctx) !== PASS ? undefined : PASS);
}