Q28: agents list, read, and update company documents

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 05:56:55 -04:00
1 parent ec9d74d128
commit 84fed8e2f2
7 files changed
+177 -4

No files matched your search

+1 -1
View File
@@ -58,7 +58,7 @@ export function createDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, a
* every deliverable and document lands in the Approvals queue. AI CEO mode
* ships without review — that autonomy is the mode's contract.
*/
function requestDeliverableReview(
export function requestDeliverableReview(
deps: AgentToolDeps,
ctx: AgentToolContext,
input: { title: string; details: string; sourceEntityType: "file" | "document"; sourceEntityId: string },
+100
View File
@@ -0,0 +1,100 @@
import { optionalEnum, optionalLimit, optionalText, requireString } from "./agent-tool-args";
import { requestDeliverableReview } from "./agent-deliverable-tools";
import type { AgentToolContext, AgentToolDeps } from "./agent-tools";
import { DocumentService } from "./document-service";
import type { JsonRecord } from "./provider-http";
import type { ToolDefinition } from "./provider-tool-turn";
import { relevantItems } from "./relevance";
import { PAGE_TOOL_PROPERTIES, pageText, readPageArgs } from "./text-paging";
export const DOCUMENT_TYPES = ["prd", "spec", "memo", "report", "runbook", "proposal", "policy", "guide"] as const;
interface DocumentRow {
id: string;
title: string;
doc_type: string;
status: string;
content: string;
updated_at: string;
}
export const DOCUMENT_TOOLS: ToolDefinition[] = [
{
name: "list_documents",
description: "Find company documents (PRDs, specs, reports, ...) by words in their title or body, or by type. Returns doc_ids for read_document.",
parameters: {
type: "object",
additionalProperties: false,
properties: {
query: { type: "string", description: "Words to look for; omit to list the newest documents." },
doc_type: { type: "string", enum: [...DOCUMENT_TYPES] },
limit: { type: "number", description: "Most documents to return (default 10, max 30)." },
},
required: [],
},
},
{
name: "read_document",
description: "Read a company document by doc_id — whole, a range of lines, or only the lines matching a search.",
parameters: {
type: "object",
additionalProperties: false,
properties: { doc_id: { type: "string" }, ...PAGE_TOOL_PROPERTIES },
required: ["doc_id"],
},
},
{
name: "update_document",
description: "Replace a company document's content (and optionally its title). Say why in reason.",
parameters: {
type: "object",
additionalProperties: false,
properties: {
doc_id: { type: "string" },
content: { type: "string", description: "The full new content in markdown." },
title: { type: "string" },
reason: { type: "string", description: "What changed and why." },
},
required: ["doc_id", "content", "reason"],
},
},
];
export function listDocumentsTool(deps: AgentToolDeps, ctx: AgentToolContext, args: JsonRecord): string {
const query = optionalText(args.query, "query");
const docType = args.doc_type === undefined ? null : optionalEnum(args.doc_type, "doc_type", DOCUMENT_TYPES, "");
const limit = optionalLimit(args.limit, "limit", 10, 30);
const rows = deps.db.prepare(`
select id, title, doc_type, status, content, updated_at from documents
where org_id = ? and (? is null or doc_type = ?) order by updated_at desc
`).all(ctx.orgId, docType, docType) as unknown as DocumentRow[];
const found = query ? relevantItems(rows, (row) => `${row.title} ${row.content}`, query, limit) : rows.slice(0, limit);
if (found.length === 0) return query ? `No documents match "${query}".` : "No documents yet.";
return found.map((row) => `${row.title} (doc_id=${row.id}, ${row.doc_type}, ${row.status}, ${row.content.length} chars, updated ${row.updated_at})`).join("\n");
}
export function readDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, args: JsonRecord): string {
const id = requireString(args.doc_id, "doc_id");
const doc = deps.db.prepare("select title, doc_type, status, content from documents where id = ? and org_id = ?")
.get(id, ctx.orgId) as { title: string; doc_type: string; status: string; content: string } | undefined;
if (!doc) throw new Error(`No document with doc_id "${id}".`);
return `${doc.title} (${doc.doc_type}, ${doc.status})\n\n${pageText(doc.title, doc.content, readPageArgs(args))}`;
}
export function updateDocumentTool(deps: AgentToolDeps, ctx: AgentToolContext, args: JsonRecord): string {
const id = requireString(args.doc_id, "doc_id");
const doc = deps.db.prepare("select title, doc_type, status from documents where id = ? and org_id = ?")
.get(id, ctx.orgId) as { title: string; doc_type: string; status: string } | undefined;
if (!doc) throw new Error(`No document with doc_id "${id}".`);
const content = requireString(args.content, "content");
const title = optionalText(args.title, "title") ?? doc.title;
const reason = requireString(args.reason, "reason");
new DocumentService(deps.db).update({ orgId: ctx.orgId, documentId: id, title, content, status: doc.status });
const review = requestDeliverableReview(deps, ctx, {
title: `Review ${doc.doc_type} update: ${title}`,
details: `Document "${title}" was updated by an agent run (${content.length} chars). Reason: ${reason}`,
sourceEntityType: "document",
sourceEntityId: id,
});
return `Updated document "${title}" (doc_id=${id}).${review ? " The update is queued for board review in Approvals." : ""}`;
}
+9 -1
View File
@@ -12,6 +12,7 @@ import { createDocumentTool, writeDeliverableTool } from "./agent-deliverable-to
import { companyStatusTool, GOAL_TOOLS, updateGoalTool } from "./agent-goal-tools";
import { ATTACHMENT_TOOLS, readAttachmentTool } from "./agent-attachment-tools";
import { KNOWLEDGE_TOOLS, recordKnowledgeTool, reviseKnowledgeTool, searchKnowledgeTool } from "./agent-knowledge-tools";
import { DOCUMENT_TOOLS, DOCUMENT_TYPES, listDocumentsTool, readDocumentTool, updateDocumentTool } from "./agent-document-tools";
import { sendMessageTool } from "./agent-send-message";
import { withSavepoint } from "../database/connection";
import { optionalString, optionalStringArray, requireString } from "./agent-tool-args";
@@ -160,7 +161,7 @@ export const AGENT_TOOLS: ToolDefinition[] = [
additionalProperties: false,
properties: {
title: { type: "string" },
doc_type: { type: "string", enum: ["prd", "spec", "memo", "report", "runbook", "proposal", "policy", "guide"] },
doc_type: { type: "string", enum: [...DOCUMENT_TYPES] },
content: { type: "string", description: "Full document content in markdown." },
},
required: ["title", "doc_type", "content"],
@@ -231,6 +232,7 @@ export const AGENT_TOOLS: ToolDefinition[] = [
...GOAL_TOOLS,
...ATTACHMENT_TOOLS,
...KNOWLEDGE_TOOLS,
...DOCUMENT_TOOLS,
];
export function dispatchAgentTool(
@@ -255,6 +257,12 @@ export function dispatchAgentTool(
return readMessagesTool(deps, ctx, call.arguments);
case "create_document":
return createDocumentTool(deps, ctx, call.arguments);
case "list_documents":
return listDocumentsTool(deps, ctx, call.arguments);
case "read_document":
return readDocumentTool(deps, ctx, call.arguments);
case "update_document":
return updateDocumentTool(deps, ctx, call.arguments);
case "list_connectors":
return listConnectorsTool(deps, ctx);
case "grant_connector":
+1 -1
View File
@@ -106,7 +106,7 @@ export function buildAgentInstructions(input: AgentInstructionContext): string {
`You are ${agent.name}, ${agent.title || agent.role} in ${agent.department} at ${input.companyName}. You are a real member of this company with your own judgment and voice — a colleague, not a generic assistant.`,
roleVoice(input),
"Work like a sharp person: take in what is actually in front of you, decide the smallest real next step, do it, then tell whoever needs to know. Do not narrate busywork.",
`Your tools are how you act: hire_agent builds your team, create_task delegates and assigns work by name (set requires_filesystem for engineering work that must change code or run commands), update_task_status tracks it, list_files and read_file explore the company workspace, write_deliverable produces real files, run_command builds and tests for real on filesystem tasks, create_document writes versioned company documents (PRDs, specs, memos, reports), search_knowledge and record_knowledge read and grow what the company has learned, read_messages catches you up on what teammates said, and send_message is how you talk to everyone. web_search and fetch_url reach the live web — look up APIs, docs, and facts instead of guessing them.`,
`Your tools are how you act: hire_agent builds your team, create_task delegates and assigns work by name (set requires_filesystem for engineering work that must change code or run commands), update_task_status tracks it, list_files and read_file explore the company workspace, write_deliverable produces real files, run_command builds and tests for real on filesystem tasks, create_document writes company documents (PRDs, specs, memos, reports) and list_documents, read_document, update_document find and revise them, search_knowledge and record_knowledge read and grow what the company has learned, read_messages catches you up on what teammates said, and send_message is how you talk to everyone. web_search and fetch_url reach the live web — look up APIs, docs, and facts instead of guessing them.`,
...(input.language === "zh"
? ["公司的工作语言是简体中文:你写的每一条消息、任务标题与描述、文档和交付物都用中文表达(代码、命令、API 名称等技术标识保持原样)。工具名称和工具参数的字段名仍按定义使用英文。"]
: []),
+2 -1
View File
@@ -1,3 +1,4 @@
import { optionalText } from "./agent-tool-args";
import type { JsonRecord } from "./provider-http";
const PAGE_CHAR_LIMIT = 24_000;
@@ -18,7 +19,7 @@ export const PAGE_TOOL_PROPERTIES = {
} as const;
export function readPageArgs(args: JsonRecord): PageRequest {
const search = typeof args.search === "string" && args.search.trim() ? args.search.trim() : null;
const search = optionalText(args.search, "search");
return {
startLine: positiveInteger(args.start_line, "start_line") ?? 1,
startColumn: positiveInteger(args.start_column, "start_column") ?? 1,