Skip to content

Commit 4dd4957

Browse files
committed
refactor(sessions): share attachment summary prefix, parameterise tests
1 parent 8252a05 commit 4dd4957

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

packages/core/src/editor/cloud-prompt.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,21 @@ describe("cloud-prompt", () => {
6464
);
6565
});
6666

67-
it("strips the trailing attachment summary from a task description", () => {
68-
expect(
69-
stripTrailingAttachmentSummary("do this\n\nAttached files: a.png, b.txt"),
70-
).toBe("do this");
71-
expect(stripTrailingAttachmentSummary("Attached files: a.png")).toBe("");
72-
expect(stripTrailingAttachmentSummary("do this")).toBe("do this");
73-
expect(
74-
stripTrailingAttachmentSummary("Attached files: a.png\n\nthen do this"),
75-
).toBe("Attached files: a.png\n\nthen do this");
67+
it.each([
68+
[
69+
"text + trailing summary",
70+
"do this\n\nAttached files: a.png, b.txt",
71+
"do this",
72+
],
73+
["summary only", "Attached files: a.png", ""],
74+
["no summary", "do this", "do this"],
75+
[
76+
"summary not at end",
77+
"Attached files: a.png\n\nthen do this",
78+
"Attached files: a.png\n\nthen do this",
79+
],
80+
])("stripTrailingAttachmentSummary: %s", (_label, input, expected) => {
81+
expect(stripTrailingAttachmentSummary(input)).toBe(expected);
7682
});
7783

7884
it("uses resource_link path references for text attachments", async () => {

packages/core/src/editor/cloud-prompt.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ export function getAbsoluteAttachmentPaths(
147147
);
148148
}
149149

150-
const TRAILING_ATTACHMENT_SUMMARY_REGEX = /(?:^|\n)Attached files: [^\n]*$/;
150+
export const ATTACHMENT_SUMMARY_PREFIX = "Attached files: ";
151+
const TRAILING_ATTACHMENT_SUMMARY_REGEX = new RegExp(
152+
`(?:^|\\n)${ATTACHMENT_SUMMARY_PREFIX}[^\\n]*$`,
153+
);
151154

152155
export function stripTrailingAttachmentSummary(text: string): string {
153156
return text.replace(TRAILING_ATTACHMENT_SUMMARY_REGEX, "").trim();
@@ -166,7 +169,7 @@ export function buildCloudTaskDescription(
166169
return strippedPrompt;
167170
}
168171

169-
const attachmentSummary = `Attached files: ${attachmentNames.join(", ")}`;
172+
const attachmentSummary = `${ATTACHMENT_SUMMARY_PREFIX}${attachmentNames.join(", ")}`;
170173
return strippedPrompt
171174
? `${strippedPrompt}\n\n${attachmentSummary}`
172175
: attachmentSummary;

packages/core/src/sessions/cloudPrompt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ContentBlock } from "@agentclientprotocol/sdk";
22
import {
3+
ATTACHMENT_SUMMARY_PREFIX,
34
buildCloudTaskDescription,
45
getAbsoluteAttachmentPaths,
56
stripAbsoluteFileTags,
@@ -87,7 +88,7 @@ function summarizePrompt(text: string, filePaths: string[]): string {
8788
return text.trim();
8889
}
8990

90-
const attachmentSummary = `Attached files: ${filePaths.map(getFileName).join(", ")}`;
91+
const attachmentSummary = `${ATTACHMENT_SUMMARY_PREFIX}${filePaths.map(getFileName).join(", ")}`;
9192
return text.trim()
9293
? `${text.trim()}\n\n${attachmentSummary}`
9394
: attachmentSummary;

0 commit comments

Comments
 (0)