Compare commits

...
2 Commits
Author SHA1 Message Date
paseo-bot[bot] eb0442ec01 Move attachment renderer tests to the protocol package 2026-12-06 14:45:34 +00:00
benthecarman df79623083 Render attachments as text for plugin ACP agents
The plugin ACP client JSON-encoded every prompt part that was not text
or an image, so uploaded files, PR and issue attachments, and reviews
reached agents like Antigravity as raw JSON. Pasted text attachments
passed through with their extra fields.

Move the attachment renderer the built-in providers use into the
protocol package and use it in the plugin ACP client too, so every
agent reads attachments the same way. The renderer accepts the
schema's input type because plugin SDK attachments leave the forge
optional.

Co-authored-by: benthecarman <benthecarman@live.com>
2026-12-06 14:15:55 +00:00
6 changed files with 387 additions and 296 deletions
@@ -30,6 +30,7 @@ import {
} from "@agentclientprotocol/sdk";
import { z } from "zod";
import type { JsonValue } from "@getpaseo/protocol/agent-types";
import { renderPromptAttachmentAsText } from "@getpaseo/protocol/prompt-attachment-text";
import {
createBoundedNdJsonStream,
ProviderStreamLimitError,
@@ -2874,8 +2875,9 @@ function toAcpPrompt(
return [{ type: "text", text: `/${prompt.input.name}${suffix}` }];
}
return prompt.input.content.map((content) => {
if (content.type === "text" || content.type === "image") return content;
return { type: "text", text: JSON.stringify(content) };
if (content.type === "image") return content;
if (content.type === "text") return { type: "text", text: content.text };
return { type: "text", text: renderPromptAttachmentAsText(content) };
});
}
+77
View File
@@ -1751,6 +1751,83 @@ readline.createInterface({ input: process.stdin }).on("line", (line) => {
},
);
it("sends attachments to the agent as readable text, not JSON", async () => {
const prompts: unknown[] = [];
const harness = connectorHarness({
handleRequest(instance, message) {
if (message.method !== "session/prompt") return false;
prompts.push((message.params as { prompt: unknown }).prompt);
instance.respond(message, { stopReason: "end_turn" });
return true;
},
});
const registration = runAcpProvider({
id: "attachment-acp",
label: "Attachment ACP",
connector: harness.connector,
});
const connection = await registration.connect({
versions: [1],
capabilities: ["prompt.message"],
});
const events: ProviderEvent[] = [];
connection.onEvent((event) => events.push(event));
try {
await connection.send(openInput());
await waitForEvent(events, (event) => event.type === "session.ready");
await connection.send({
type: "session.prompt",
sessionId: "session-1",
prompt: {
clientMessageId: "first",
delivery: "auto",
input: {
type: "message",
content: [
{ type: "text", text: "Read these" },
{
type: "uploaded_file",
id: "upload-1",
fileName: "notes.txt",
mimeType: "text/plain",
size: 14,
path: "/tmp/uploads/upload-1/notes.txt",
},
{
type: "forge_issue",
mimeType: "application/paseo-forge-issue",
number: 7,
title: "Broken thing",
url: "https://github.com/acme/app/issues/7",
},
{ type: "text", mimeType: "text/plain", title: "Pasted", text: "pasted body" },
],
},
},
});
await waitForEvent(
events,
(event) => event.type === "session.turn" && event.state === "completed",
);
expect(prompts).toEqual([
[
{ type: "text", text: "Read these" },
{
type: "text",
text: "Uploaded file: notes.txt\nPath: /tmp/uploads/upload-1/notes.txt\nMIME: text/plain\nSize: 14 bytes",
},
{
type: "text",
text: "GitHub Issue #7: Broken thing\nhttps://github.com/acme/app/issues/7",
},
{ type: "text", text: "pasted body" },
],
]);
} finally {
await connection.close();
}
});
it.each([
{ status: "failed", output: undefined, error: { message: "Tool failed" } },
{ status: "failed", output: null, error: { message: "Tool failed" } },
@@ -0,0 +1,134 @@
import { describe, expect, it } from "vitest";
import { renderPromptAttachmentAsText } from "./prompt-attachment-text.js";
describe("renderPromptAttachmentAsText", () => {
it("renders github_pr attachments as readable text", () => {
expect(
renderPromptAttachmentAsText({
type: "github_pr",
mimeType: "application/github-pr",
number: 123,
title: "Fix race in worktree setup",
url: "https://github.com/getpaseo/paseo/pull/123",
body: "PR body",
baseRefName: "main",
headRefName: "fix/worktree-race",
}),
).toContain("GitHub PR #123: Fix race in worktree setup");
});
it("renders GitLab change request attachments with MR numbering", () => {
expect(
renderPromptAttachmentAsText({
type: "forge_change_request",
mimeType: "application/paseo-forge-change-request",
forge: "gitlab",
number: 123,
title: "Fix race in worktree setup",
url: "https://gitlab.com/getpaseo/paseo/-/merge_requests/123",
body: "MR body",
projectPath: "getpaseo/paseo",
baseRefName: "main",
headRefName: "fix/worktree-race",
}),
).toContain("GitLab MR !123: Fix race in worktree setup");
});
it.each(["base", "uncommitted", "all"] as const)("renders %s reviews", (mode) => {
expect(
renderPromptAttachmentAsText({
type: "review",
mimeType: "application/paseo-review",
cwd: "/tmp/repo",
mode,
baseRef: "main",
comments: [
{
filePath: "src/index.ts",
side: "new",
lineNumber: 42,
body: "Please guard this nullable value.",
context: {
hunkHeader: "@@ -40,3 +40,4 @@",
targetLine: {
oldLineNumber: null,
newLineNumber: 42,
type: "add",
content: "const value = maybeNull.name;",
},
lines: [
{
oldLineNumber: 41,
newLineNumber: 41,
type: "context",
content: "const before = true;",
},
{
oldLineNumber: null,
newLineNumber: 42,
type: "add",
content: "const value = maybeNull.name;",
},
],
},
},
],
}),
).toBe(
[
`Paseo review attachment (${mode})`,
"CWD: /tmp/repo",
"Base: main",
"",
"Comment 1: src/index.ts:new:42",
"Please guard this nullable value.",
"@@ -40,3 +40,4 @@",
" 41 41 const before = true;",
"> - 42 +const value = maybeNull.name;",
].join("\n"),
);
});
it("renders github_issue attachments as readable text", () => {
expect(
renderPromptAttachmentAsText({
type: "github_issue",
mimeType: "application/github-issue",
number: 55,
title: "Issue",
url: "https://github.com/getpaseo/paseo/issues/55",
}),
).toContain("GitHub Issue #55: Issue");
});
it("renders text attachments as their client-provided prompt text", () => {
expect(
renderPromptAttachmentAsText({
type: "text",
mimeType: "text/plain",
title: "Browser element",
text: "<browser-element>button.primary</browser-element>",
}),
).toBe("<browser-element>button.primary</browser-element>");
});
it("renders uploaded file attachments as local file references", () => {
expect(
renderPromptAttachmentAsText({
type: "uploaded_file",
id: "upload_req-upload",
fileName: "notes.txt",
mimeType: "text/plain",
size: 11,
path: "/tmp/paseo/uploads/upload_req-upload/notes.txt",
}),
).toBe(
[
"Uploaded file: notes.txt",
"Path: /tmp/paseo/uploads/upload_req-upload/notes.txt",
"MIME: text/plain",
"Size: 11 bytes",
].join("\n"),
);
});
});
@@ -0,0 +1,170 @@
import type { z } from "zod";
import { getForgeDefinitionOrNeutral } from "./forge-manifest.js";
import type { AgentAttachmentSchema } from "./messages.js";
// Shared by built-in providers and the plugin ACP client so every agent reads attachments the same way.
// Accepts the schema input so plugin SDK attachments, whose forge is optional, render too.
type RenderableAttachment = z.input<typeof AgentAttachmentSchema>;
const REVIEW_LINE_MARKERS = { add: "+", remove: "-", context: " " } as const;
export function renderPromptAttachmentAsText(attachment: RenderableAttachment): string {
switch (attachment.type) {
case "forge_change_request": {
return renderChangeRequestAttachment({
forge: attachment.forge ?? "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
projectPath: attachment.projectPath,
baseRefName: attachment.baseRefName,
headRefName: attachment.headRefName,
});
}
// COMPAT(githubAttachmentKinds): render legacy wire attachments until
// 2027-01-17, when supported client and daemon floors are >= v0.2.0.
case "github_pr": {
return renderChangeRequestAttachment({
forge: "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
baseRefName: attachment.baseRefName,
headRefName: attachment.headRefName,
});
}
case "forge_issue": {
return renderIssueAttachment({
forge: attachment.forge ?? "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
projectPath: attachment.projectPath,
});
}
// COMPAT(githubAttachmentKinds): render legacy wire attachments until
// 2027-01-17, when supported client and daemon floors are >= v0.2.0.
case "github_issue": {
return renderIssueAttachment({
forge: "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
});
}
case "text": {
return attachment.text;
}
case "review": {
const lines = [`Paseo review attachment (${attachment.mode})`, `CWD: ${attachment.cwd}`];
if (attachment.baseRef) {
lines.push(`Base: ${attachment.baseRef}`);
}
attachment.comments.forEach((comment, index) => {
lines.push(
"",
`Comment ${index + 1}: ${comment.filePath}:${comment.side}:${comment.lineNumber}`,
comment.body,
comment.context.hunkHeader,
);
const target = comment.context.targetLine;
for (const line of comment.context.lines) {
const isTarget =
line.oldLineNumber === target.oldLineNumber &&
line.newLineNumber === target.newLineNumber &&
line.type === target.type &&
line.content === target.content;
const prefix = isTarget ? "> " : " ";
const oldLn = padLineNumber(line.oldLineNumber);
const newLn = padLineNumber(line.newLineNumber);
lines.push(`${prefix}${oldLn} ${newLn} ${REVIEW_LINE_MARKERS[line.type]}${line.content}`);
}
});
return lines.join("\n");
}
case "uploaded_file": {
return [
`Uploaded file: ${attachment.fileName}`,
`Path: ${attachment.path}`,
`MIME: ${attachment.mimeType}`,
`Size: ${attachment.size} bytes`,
].join("\n");
}
default:
throw new Error("unreachable");
}
}
function renderChangeRequestAttachment(input: {
forge: string;
number: number;
title: string;
url: string;
body?: string | null;
projectPath?: string;
baseRefName?: string | null;
headRefName?: string | null;
}): string {
const lines = [
`${formatForgeLabel(input.forge)} ${formatChangeRequestAbbrev(input.forge)} ${formatChangeRequestNumber(input.forge, input.number)}: ${input.title}`,
input.url,
];
if (input.projectPath) {
lines.push(`Project: ${input.projectPath}`);
}
if (input.baseRefName) {
lines.push(`Base: ${input.baseRefName}`);
}
if (input.headRefName) {
lines.push(`Head: ${input.headRefName}`);
}
if (input.body) {
lines.push("", input.body);
}
return lines.join("\n");
}
function renderIssueAttachment(input: {
forge: string;
number: number;
title: string;
url: string;
body?: string | null;
projectPath?: string;
}): string {
const lines = [
`${formatForgeLabel(input.forge)} Issue ${formatIssueNumber(input.forge, input.number)}: ${input.title}`,
input.url,
];
if (input.projectPath) {
lines.push(`Project: ${input.projectPath}`);
}
if (input.body) {
lines.push("", input.body);
}
return lines.join("\n");
}
function formatForgeLabel(forge: string): string {
return getForgeDefinitionOrNeutral(forge).displayName;
}
function formatChangeRequestAbbrev(forge: string): string {
return getForgeDefinitionOrNeutral(forge).changeRequestAbbrev;
}
function formatChangeRequestNumber(forge: string, number: number): string {
return `${getForgeDefinitionOrNeutral(forge).changeRequestNumberPrefix}${number}`;
}
function formatIssueNumber(forge: string, number: number): string {
return `${getForgeDefinitionOrNeutral(forge).issueNumberPrefix}${number}`;
}
function padLineNumber(lineNumber: number | null): string {
return (lineNumber?.toString() ?? "-").padStart(2);
}
@@ -9,7 +9,6 @@ import { MAX_IMAGE_DIMENSION } from "./preprocess-image.js";
import {
buildAgentBranchNameSeed,
buildAgentPrompt,
renderPromptAttachmentAsText,
resolveAgentPrompt,
} from "./prompt-attachments.js";
@@ -44,136 +43,6 @@ describe("prompt attachments", () => {
]);
});
it("renders github_pr attachments as readable text", () => {
expect(
renderPromptAttachmentAsText({
type: "github_pr",
mimeType: "application/github-pr",
number: 123,
title: "Fix race in worktree setup",
url: "https://github.com/getpaseo/paseo/pull/123",
body: "PR body",
baseRefName: "main",
headRefName: "fix/worktree-race",
}),
).toContain("GitHub PR #123: Fix race in worktree setup");
});
it("renders GitLab change request attachments with MR numbering", () => {
expect(
renderPromptAttachmentAsText({
type: "forge_change_request",
mimeType: "application/paseo-forge-change-request",
forge: "gitlab",
number: 123,
title: "Fix race in worktree setup",
url: "https://gitlab.com/getpaseo/paseo/-/merge_requests/123",
body: "MR body",
projectPath: "getpaseo/paseo",
baseRefName: "main",
headRefName: "fix/worktree-race",
}),
).toContain("GitLab MR !123: Fix race in worktree setup");
});
it.each(["base", "uncommitted", "all"] as const)("renders %s reviews", (mode) => {
expect(
renderPromptAttachmentAsText({
type: "review",
mimeType: "application/paseo-review",
cwd: "/tmp/repo",
mode,
baseRef: "main",
comments: [
{
filePath: "src/index.ts",
side: "new",
lineNumber: 42,
body: "Please guard this nullable value.",
context: {
hunkHeader: "@@ -40,3 +40,4 @@",
targetLine: {
oldLineNumber: null,
newLineNumber: 42,
type: "add",
content: "const value = maybeNull.name;",
},
lines: [
{
oldLineNumber: 41,
newLineNumber: 41,
type: "context",
content: "const before = true;",
},
{
oldLineNumber: null,
newLineNumber: 42,
type: "add",
content: "const value = maybeNull.name;",
},
],
},
},
],
}),
).toBe(
[
`Paseo review attachment (${mode})`,
"CWD: /tmp/repo",
"Base: main",
"",
"Comment 1: src/index.ts:new:42",
"Please guard this nullable value.",
"@@ -40,3 +40,4 @@",
" 41 41 const before = true;",
"> - 42 +const value = maybeNull.name;",
].join("\n"),
);
});
it("renders github_issue attachments as readable text", () => {
expect(
renderPromptAttachmentAsText({
type: "github_issue",
mimeType: "application/github-issue",
number: 55,
title: "Issue",
url: "https://github.com/getpaseo/paseo/issues/55",
}),
).toContain("GitHub Issue #55: Issue");
});
it("renders text attachments as their client-provided prompt text", () => {
expect(
renderPromptAttachmentAsText({
type: "text",
mimeType: "text/plain",
title: "Browser element",
text: "<browser-element>button.primary</browser-element>",
}),
).toBe("<browser-element>button.primary</browser-element>");
});
it("renders uploaded file attachments as local file references", () => {
expect(
renderPromptAttachmentAsText({
type: "uploaded_file",
id: "upload_req-upload",
fileName: "notes.txt",
mimeType: "text/plain",
size: 11,
path: "/tmp/paseo/uploads/upload_req-upload/notes.txt",
}),
).toBe(
[
"Uploaded file: notes.txt",
"Path: /tmp/paseo/uploads/upload_req-upload/notes.txt",
"MIME: text/plain",
"Size: 11 bytes",
].join("\n"),
);
});
it("returns undefined when firstAgentContext is empty", () => {
expect(buildAgentBranchNameSeed(undefined)).toBeUndefined();
expect(buildAgentBranchNameSeed({})).toBeUndefined();
@@ -1,17 +1,17 @@
import { getForgeDefinitionOrNeutral } from "@getpaseo/protocol/forge-manifest";
import type {
AgentAttachment,
FirstAgentContext,
UploadedFileAttachment,
UploadedImage,
} from "@getpaseo/protocol/messages";
import { renderPromptAttachmentAsText } from "@getpaseo/protocol/prompt-attachment-text";
import type { Logger } from "pino";
import { readUploadedImageFile } from "../file-upload/index.js";
import type { AgentPromptContentBlock, AgentPromptInput } from "./agent-sdk-types.js";
import { sniffRasterMimeType } from "./image-dimensions.js";
import { preprocessImages } from "./preprocess-image.js";
const REVIEW_LINE_MARKERS = { add: "+", remove: "-", context: " " } as const;
export { renderPromptAttachmentAsText };
export interface ResolvedAgentPrompt {
prompt: AgentPromptInput;
@@ -123,167 +123,6 @@ export async function resolveAgentPrompt(input: {
};
}
export function renderPromptAttachmentAsText(attachment: AgentAttachment): string {
switch (attachment.type) {
case "forge_change_request": {
return renderChangeRequestAttachment({
forge: attachment.forge,
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
projectPath: attachment.projectPath,
baseRefName: attachment.baseRefName,
headRefName: attachment.headRefName,
});
}
// COMPAT(githubAttachmentKinds): render legacy wire attachments until
// 2027-01-17, when supported client and daemon floors are >= v0.2.0.
case "github_pr": {
return renderChangeRequestAttachment({
forge: "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
baseRefName: attachment.baseRefName,
headRefName: attachment.headRefName,
});
}
case "forge_issue": {
return renderIssueAttachment({
forge: attachment.forge,
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
projectPath: attachment.projectPath,
});
}
// COMPAT(githubAttachmentKinds): render legacy wire attachments until
// 2027-01-17, when supported client and daemon floors are >= v0.2.0.
case "github_issue": {
return renderIssueAttachment({
forge: "github",
number: attachment.number,
title: attachment.title,
url: attachment.url,
body: attachment.body,
});
}
case "text": {
return attachment.text;
}
case "review": {
const lines = [`Paseo review attachment (${attachment.mode})`, `CWD: ${attachment.cwd}`];
if (attachment.baseRef) {
lines.push(`Base: ${attachment.baseRef}`);
}
attachment.comments.forEach((comment, index) => {
lines.push(
"",
`Comment ${index + 1}: ${comment.filePath}:${comment.side}:${comment.lineNumber}`,
comment.body,
comment.context.hunkHeader,
);
const target = comment.context.targetLine;
for (const line of comment.context.lines) {
const isTarget =
line.oldLineNumber === target.oldLineNumber &&
line.newLineNumber === target.newLineNumber &&
line.type === target.type &&
line.content === target.content;
const prefix = isTarget ? "> " : " ";
const oldLn = padLineNumber(line.oldLineNumber);
const newLn = padLineNumber(line.newLineNumber);
lines.push(`${prefix}${oldLn} ${newLn} ${REVIEW_LINE_MARKERS[line.type]}${line.content}`);
}
});
return lines.join("\n");
}
case "uploaded_file": {
return [
`Uploaded file: ${attachment.fileName}`,
`Path: ${attachment.path}`,
`MIME: ${attachment.mimeType}`,
`Size: ${attachment.size} bytes`,
].join("\n");
}
default:
throw new Error("unreachable");
}
}
function renderChangeRequestAttachment(input: {
forge: string;
number: number;
title: string;
url: string;
body?: string | null;
projectPath?: string;
baseRefName?: string | null;
headRefName?: string | null;
}): string {
const lines = [
`${formatForgeLabel(input.forge)} ${formatChangeRequestAbbrev(input.forge)} ${formatChangeRequestNumber(input.forge, input.number)}: ${input.title}`,
input.url,
];
if (input.projectPath) {
lines.push(`Project: ${input.projectPath}`);
}
if (input.baseRefName) {
lines.push(`Base: ${input.baseRefName}`);
}
if (input.headRefName) {
lines.push(`Head: ${input.headRefName}`);
}
if (input.body) {
lines.push("", input.body);
}
return lines.join("\n");
}
function renderIssueAttachment(input: {
forge: string;
number: number;
title: string;
url: string;
body?: string | null;
projectPath?: string;
}): string {
const lines = [
`${formatForgeLabel(input.forge)} Issue ${formatIssueNumber(input.forge, input.number)}: ${input.title}`,
input.url,
];
if (input.projectPath) {
lines.push(`Project: ${input.projectPath}`);
}
if (input.body) {
lines.push("", input.body);
}
return lines.join("\n");
}
function formatForgeLabel(forge: string): string {
return getForgeDefinitionOrNeutral(forge).displayName;
}
function formatChangeRequestAbbrev(forge: string): string {
return getForgeDefinitionOrNeutral(forge).changeRequestAbbrev;
}
function formatChangeRequestNumber(forge: string, number: number): string {
return `${getForgeDefinitionOrNeutral(forge).changeRequestNumberPrefix}${number}`;
}
function formatIssueNumber(forge: string, number: number): string {
return `${getForgeDefinitionOrNeutral(forge).issueNumberPrefix}${number}`;
}
function padLineNumber(lineNumber: number | null): string {
return (lineNumber?.toString() ?? "-").padStart(2);
}
export type WorkspaceNamingContext = FirstAgentContext & { imageCount?: number };
export function buildAgentBranchNameSeed(