feat(composer): fold large pastes into text attachments (#11442)
This commit is contained in:
1 parent
36caf200c2
commit
68c2277f50
77 files changed
+3278
-465
No files matched your search
@@ -2228,6 +2228,25 @@ routing.layer("ProviderServiceLive routing", (it) => {
|
||||
assert.include(fileOnlyInput.input ?? "", '[Attached file "report.pdf" is saved at: ');
|
||||
assert.deepEqual(fileOnlyInput.attachments, [fileAttachment]);
|
||||
|
||||
const pastedTextAttachment = {
|
||||
type: "file" as const,
|
||||
id: "thread-attach-12345678-1234-1234-1234-123456789abc-txt",
|
||||
name: "pasted-text.txt",
|
||||
mimeType: "text/plain;charset=utf-8",
|
||||
sizeBytes: 32_768,
|
||||
source: { _tag: "pasted-text" as const },
|
||||
};
|
||||
routing.codex.sendTurn.mockClear();
|
||||
yield* provider.sendTurn({
|
||||
threadId: session.threadId,
|
||||
input: "Investigate this crash",
|
||||
attachments: [pastedTextAttachment],
|
||||
});
|
||||
const pastedInput = routing.codex.sendTurn.mock.calls[0]?.[0] as ProviderSendTurnInput;
|
||||
assert.include(pastedInput.input ?? "", '[Pasted text "pasted-text.txt" is saved at: ');
|
||||
assert.include(pastedInput.input ?? "", ". Inspect it as needed.]");
|
||||
assert.deepEqual(pastedInput.attachments, [pastedTextAttachment]);
|
||||
|
||||
yield* provider.stopSession({ threadId: session.threadId });
|
||||
}),
|
||||
);
|
||||
@@ -4683,6 +4702,33 @@ turnAnalytics.layer("ProviderServiceLive turn analytics", (it) => {
|
||||
|
||||
const validation = makeProviderServiceLayer();
|
||||
validation.layer("ProviderServiceLive validation", (it) => {
|
||||
it.effect("rejects input that leaves no room for pasted-text attachment context", () =>
|
||||
Effect.gen(function* () {
|
||||
const provider = yield* ProviderService.ProviderService;
|
||||
const attachment = {
|
||||
type: "file" as const,
|
||||
id: "thread-attach-12345678-1234-1234-1234-123456789abc-txt",
|
||||
name: "pasted-text.txt",
|
||||
mimeType: "text/plain;charset=utf-8",
|
||||
sizeBytes: 32_768,
|
||||
source: { _tag: "pasted-text" as const },
|
||||
};
|
||||
validation.codex.sendTurn.mockClear();
|
||||
|
||||
const failure = yield* provider
|
||||
.sendTurn({
|
||||
threadId: asThreadId("thread-pasted-text-context-limit"),
|
||||
input: "x".repeat(PROVIDER_SEND_TURN_MAX_INPUT_CHARS),
|
||||
attachments: [attachment],
|
||||
})
|
||||
.pipe(Effect.flip);
|
||||
|
||||
assert.instanceOf(failure, ProviderValidationError);
|
||||
assert.include(failure.issue, String(PROVIDER_SEND_TURN_MAX_INPUT_CHARS));
|
||||
assert.equal(validation.codex.sendTurn.mock.calls.length, 0);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects citation-expanded input over the provider character limit", () =>
|
||||
Effect.gen(function* () {
|
||||
const provider = yield* ProviderService.ProviderService;
|
||||
|
||||
@@ -1593,30 +1593,45 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
|
||||
|
||||
// Every attachment gets an on-disk path in the prompt so the model's tools
|
||||
// can dereference the actual file. All attachments then go to the adapter,
|
||||
// and each adapter decides what its provider ingests natively: OpenCode
|
||||
// sends generic files as file parts, the others send images only and rely
|
||||
// on the path line for everything else. Unresolvable ids are skipped here
|
||||
// and surface as adapter errors when the file is read.
|
||||
// and each adapter decides what its provider ingests natively. Folded
|
||||
// clipboard text remains path-only everywhere: eagerly embedding it would
|
||||
// spend the same context the client deliberately preserved by folding it.
|
||||
// Unresolvable ids are skipped here and surface as adapter errors when the
|
||||
// file is read.
|
||||
let inputTextWithAttachmentContext = inputTextWithCitations;
|
||||
const appendAttachmentContext = (context: string | undefined) => {
|
||||
if (context === undefined) return;
|
||||
if (context === undefined) return true;
|
||||
const candidate = inputTextWithAttachmentContext
|
||||
? `${inputTextWithAttachmentContext}\n\n${context}`
|
||||
: context;
|
||||
if (candidate.length <= PROVIDER_SEND_TURN_MAX_INPUT_CHARS) {
|
||||
inputTextWithAttachmentContext = candidate;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
for (const attachment of attachments) {
|
||||
const attachmentPath = resolveAttachmentPath({
|
||||
attachmentsDir: serverConfig.attachmentsDir,
|
||||
attachment,
|
||||
});
|
||||
appendAttachmentContext(
|
||||
const isPastedText =
|
||||
attachment.type === "file" &&
|
||||
"source" in attachment &&
|
||||
attachment.source?._tag === "pasted-text";
|
||||
const appended = appendAttachmentContext(
|
||||
attachmentPath === null
|
||||
? undefined
|
||||
: `[Attached ${attachment.type} "${attachment.name}" is saved at: ${attachmentPath}]`,
|
||||
: isPastedText
|
||||
? `[Pasted text "${attachment.name}" is saved at: ${attachmentPath}. Inspect it as needed.]`
|
||||
: `[Attached ${attachment.type} "${attachment.name}" is saved at: ${attachmentPath}]`,
|
||||
);
|
||||
if (isPastedText && !appended) {
|
||||
return yield* toValidationError(
|
||||
"ProviderService.sendTurn",
|
||||
`Input plus pasted-text attachment context exceeds the ${PROVIDER_SEND_TURN_MAX_INPUT_CHARS} character limit`,
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const attachment of attachments) {
|
||||
const source =
|
||||
|
||||
@@ -279,6 +279,51 @@ it.layer(NodeServices.layer)("buildAntigravityPrompt", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps folded clipboard text out of native context", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeAttachmentFixture();
|
||||
const pastedText = {
|
||||
...textAttachment,
|
||||
name: "pasted-text.txt",
|
||||
mimeType: "text/plain",
|
||||
source: { _tag: "pasted-text" as const },
|
||||
} satisfies ChatAttachment;
|
||||
yield* fixture.write(pastedText, "A very large crash report");
|
||||
|
||||
const prompt = yield* buildAntigravityPrompt({
|
||||
input: "Inspect the pasted text only as needed.",
|
||||
attachments: [pastedText],
|
||||
attachmentsDir: fixture.attachmentsDir,
|
||||
});
|
||||
|
||||
expect(prompt).toEqual([{ type: "text", text: "Inspect the pasted text only as needed." }]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("rejects missing folded clipboard text", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeAttachmentFixture();
|
||||
const pastedText = {
|
||||
...textAttachment,
|
||||
name: "pasted-text.txt",
|
||||
mimeType: "text/plain",
|
||||
source: { _tag: "pasted-text" as const },
|
||||
} satisfies ChatAttachment;
|
||||
|
||||
const error = yield* buildAntigravityPrompt({
|
||||
input: "Inspect the pasted text only as needed.",
|
||||
attachments: [pastedText],
|
||||
attachmentsDir: fixture.attachmentsDir,
|
||||
}).pipe(Effect.flip);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
_tag: "AcpRequestError",
|
||||
code: -32602,
|
||||
errorMessage: "Could not read attachment 'pasted-text.txt'.",
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("sends supported audio files as native audio content", () =>
|
||||
Effect.gen(function* () {
|
||||
const fixture = yield* makeAttachmentFixture();
|
||||
|
||||
@@ -264,6 +264,13 @@ export const buildAntigravityPrompt = Effect.fn("buildAntigravityPrompt")(functi
|
||||
let totalBytes = 0;
|
||||
|
||||
for (const attachment of input.attachments ?? []) {
|
||||
const isPastedText =
|
||||
attachment.type === "file" &&
|
||||
"source" in attachment &&
|
||||
attachment.source?._tag === "pasted-text";
|
||||
// ProviderService has already put the file path in the text block. Keep a
|
||||
// folded clipboard paste lazy so the agent can search or sample it rather
|
||||
// than paying to embed the entire resource in context immediately.
|
||||
const mimeType = attachment.mimeType.toLowerCase().split(";", 1)[0] ?? "";
|
||||
const image = attachment.type === "image" && IMAGE_MIME_TYPES.has(mimeType);
|
||||
const audio = attachment.type === "file" && AUDIO_MIME_TYPES.has(mimeType);
|
||||
@@ -296,6 +303,14 @@ export const buildAntigravityPrompt = Effect.fn("buildAntigravityPrompt")(functi
|
||||
),
|
||||
),
|
||||
);
|
||||
if (isPastedText) {
|
||||
if (info.type !== "File") {
|
||||
return yield* EffectAcpErrors.AcpRequestError.invalidParams(
|
||||
`Could not read attachment '${attachment.name}'.`,
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const size = Number(info.size);
|
||||
const limit = image
|
||||
? PROVIDER_SEND_TURN_MAX_IMAGE_BYTES
|
||||
|
||||
@@ -318,4 +318,18 @@ describe("toOpenCodeFileParts", () => {
|
||||
["application/pdf", "text/markdown", "image/png"],
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps folded clipboard text on the lazy path fallback", () => {
|
||||
const parts = toOpenCodeFileParts({
|
||||
attachments: [
|
||||
{
|
||||
...attachment("text/plain"),
|
||||
source: { _tag: "pasted-text" as const },
|
||||
},
|
||||
],
|
||||
resolveAttachmentPath: () => "/tmp/pasted-text.txt",
|
||||
});
|
||||
|
||||
NodeAssert.deepEqual(parts, []);
|
||||
});
|
||||
});
|
||||
@@ -462,6 +462,13 @@ export function toOpenCodeFileParts(input: {
|
||||
const parts: Array<FilePartInput> = [];
|
||||
|
||||
for (const attachment of input.attachments ?? []) {
|
||||
if (
|
||||
attachment.type === "file" &&
|
||||
"source" in attachment &&
|
||||
attachment.source?._tag === "pasted-text"
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
if (!isOpenCodeNativeFilePart(attachment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user