Q13: messages carry their attachments; agents read them by file_id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 04:05:11 -04:00
1 parent f15e47eda1
commit 31bf0a1497
6 files changed
+166 -13

No files matched your search

+33 -3
View File
@@ -1,4 +1,5 @@
import { randomUUID } from "node:crypto";
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
@@ -8,6 +9,8 @@ import { ProviderService } from "../../src/main/services/provider-service";
import { providerAdapters } from "../../src/main/services/provider-adapters";
import { TaskExecutionService } from "../../src/main/services/task-execution-service";
import { ChatService } from "../../src/main/services/chat-service";
import { OrganizationService } from "../../src/main/services/organization-service";
import { dispatchAgentTool, type AgentToolContext } from "../../src/main/services/agent-tools";
import { ensureCeoDirectSpace } from "../../src/main/services/ceo-direct-space";
import { claimNextQueuedRun } from "../../src/main/app/task-queue-worker";
import type { AgentTurnResult, ProviderToolTurnInput } from "../../src/main/services/provider-tool-turn";
@@ -51,8 +54,8 @@ async function founderLine() {
const spaceId = ensureCeoDirectSpace(connection.db, orgId)!;
const kickoff = claimNextQueuedRun(connection);
await execution.executeRun(kickoff!.org_id, kickoff!.id);
const say = (bodyMd: string, threadId: string | null) => {
const id = chat.sendMessage({ orgId, spaceId, threadId, senderType: "employee", senderId: founderId, bodyMd, attachmentFileIds: [] });
const say = (bodyMd: string, threadId: string | null, attachmentFileIds: string[] = []) => {
const id = chat.sendMessage({ orgId, spaceId, threadId, senderType: "employee", senderId: founderId, bodyMd, attachmentFileIds });
return (connection.db.prepare("select thread_id from chat_messages where id = ?").get(id) as { thread_id: string }).thread_id;
};
const runWake = async () => {
@@ -77,7 +80,17 @@ async function founderLine() {
const row = connection.db.prepare("select id from chat_messages where body_md = ?").get(bodyMd) as { id: string };
chat.deleteMessage({ orgId, messageId: row.id, actorType: "employee", actorId: founderId });
};
return { connection, wakePrompts, wakeInputs, say, runWake, runManually, edit, remove, onWakeTurn };
const addFile = (name: string, content: string) => {
const path = join(tmpdir(), `ac-attach-${randomUUID()}-${name}`);
writeFileSync(path, content);
const fileId = `fil_${randomUUID()}`;
connection.db.prepare(`
insert into file_objects(id, org_id, name, kind, size_bytes, sha256, storage_path, owner_type, owner_id, created_at)
values (?, ?, ?, 'md', ?, 'x', ?, 'employee', ?, ?)
`).run(fileId, orgId, name, content.length, path, founderId, new Date().toISOString());
return fileId;
};
return { connection, orgId, ceoId, wakePrompts, wakeInputs, say, runWake, runManually, edit, remove, onWakeTurn, addFile };
}
describe("chat wake context", () => {
@@ -257,4 +270,21 @@ describe("chat wake context", () => {
expect(h.connection.db.prepare("select count(*) as n from tasks where title like '%99-1234%' or description like '%99-1234%'").get()).toEqual({ n: 0 });
h.connection.close();
});
test("attachments travel with the message and can be read by file_id", async () => {
const h = await founderLine();
const fileId = h.addFile("spec.md", "# Spec\nShip the booking API by Friday.");
h.say("Please review the attached spec.", null, [fileId]);
await h.runWake();
expect(h.wakePrompts[0]).toContain(`Please review the attached spec. [attachments: spec.md (file_id=${fileId})]`);
const deps = { db: h.connection.db, organization: new OrganizationService(h.connection.db), chat: new ChatService(h.connection.db) };
const ctx: AgentToolContext = {
orgId: h.orgId, agent: { id: h.ceoId, modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, taskId: null, sourceThreadId: null, workspace: null,
};
expect(dispatchAgentTool(deps, ctx, { id: "c1", name: "read_messages", arguments: { with: "Yang" } })).toContain(`[attachments: spec.md (file_id=${fileId})]`);
expect(dispatchAgentTool(deps, ctx, { id: "c2", name: "read_attachment", arguments: { file_id: fileId } })).toContain("Ship the booking API by Friday.");
h.connection.close();
});
});
+62
View File
@@ -0,0 +1,62 @@
import { randomUUID } from "node:crypto";
import { writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { OrganizationService } from "../../src/main/services/organization-service";
import { ChatService } from "../../src/main/services/chat-service";
import { dispatchAgentTool, type AgentToolContext } from "../../src/main/services/agent-tools";
function harness() {
const connection = new DatabaseConnection(join(tmpdir(), `ac-attach-${randomUUID()}.db`));
const organization = new OrganizationService(connection.db);
let orgId = "";
let otherOrgId = "";
connection.transaction(() => {
orgId = organization.createOrganization({ organizationName: "A", employeeName: "Yang", mission: "m", goal: "g" });
otherOrgId = organization.createOrganization({ organizationName: "B", employeeName: "Lee", mission: "m", goal: "g" });
});
const addFile = (owner: string, name: string, content: string | Buffer, scanState = "approved") => {
const path = join(tmpdir(), `ac-attach-${randomUUID()}-${name}`);
writeFileSync(path, content);
const fileId = `fil_${randomUUID()}`;
connection.db.prepare(`
insert into file_objects(id, org_id, name, kind, size_bytes, sha256, storage_path, owner_type, owner_id, created_at, scan_state)
values (?, ?, ?, 'md', 1, 'x', ?, 'employee', null, ?, ?)
`).run(fileId, owner, name, path, new Date().toISOString(), scanState);
return fileId;
};
const ctx: AgentToolContext = {
orgId, agent: { id: "agt_x", modelProviderId: null, modelId: null },
taskProjectId: null, taskGoalId: null, taskId: null, sourceThreadId: null, workspace: null,
};
const read = (fileId: string) => dispatchAgentTool(
{ db: connection.db, organization, chat: new ChatService(connection.db) },
ctx,
{ id: "c", name: "read_attachment", arguments: { file_id: fileId } },
);
return { connection, orgId, otherOrgId, addFile, read };
}
describe("read_attachment", () => {
test("reads text, caps long files, and names binary files", () => {
const h = harness();
expect(h.read(h.addFile(h.orgId, "notes.md", "hello attachment"))).toBe("hello attachment");
const long = h.read(h.addFile(h.orgId, "big.md", "x".repeat(30_000)));
expect(long).toContain("[truncated: showing the first 24000 of 30000 characters]");
expect(h.read(h.addFile(h.orgId, "logo.png", Buffer.from([137, 80, 78, 71, 0, 1, 2])))).toBe("logo.png is a binary file (7 bytes); it has no text to read.");
expect(h.read(h.addFile(h.orgId, "blob.bin", Buffer.from([0x80, 0x81, 0xfe, 0xff])))).toBe("blob.bin is a binary file (4 bytes); it has no text to read.");
expect(h.read(h.addFile(h.orgId, "zh.md", "中文说明"))).toBe("中文说明");
h.connection.close();
});
test("refuses blocked files, unknown ids, and other companies' files", () => {
const h = harness();
expect(h.read(h.addFile(h.orgId, "virus.txt", "bad", "blocked"))).toBe("ERROR: virus.txt was blocked by the file scan and cannot be read.");
expect(h.read("fil_missing")).toBe('ERROR: No file with file_id "fil_missing".');
const foreign = h.addFile(h.otherOrgId, "secret.md", "other company");
expect(h.read(foreign)).toBe(`ERROR: No file with file_id "${foreign}".`);
h.connection.close();
});
});