fix(claude): expand slash commands when a message has attachments (#9122)

Co-authored-by: Lars Nieuwenhuis <[email protected]>
This commit is contained in:
Theo BrowneandLars Nieuwenhuis authored and GitHub committed 2026-09-03 02:28:33 -07:00
1 parent 5d1b02cdee
commit 18573d60aa
2 files changed
+128 -8

No files matched your search

@@ -272,6 +272,30 @@ async function readFirstPromptMessage(
return next.value;
}
/** Drains the first `count` queued prompts so consecutive turns can be compared. */
async function readPromptMessages(
input:
| {
readonly prompt: AsyncIterable<SDKUserMessage>;
}
| undefined,
count: number,
): Promise<Array<SDKUserMessage>> {
const iterator = input?.prompt[Symbol.asyncIterator]();
if (!iterator) {
return [];
}
const messages: Array<SDKUserMessage> = [];
while (messages.length < count) {
const next = await iterator.next();
if (next.done) {
break;
}
messages.push(next.value);
}
return messages;
}
const THREAD_ID = ThreadId.make("thread-claude-1");
const RESUME_THREAD_ID = ThreadId.make("thread-claude-resume");
const SYNTHETIC_SUBAGENT_MODEL = "claude-synthetic-subagent[expanded]";
@@ -792,10 +816,100 @@ describe("ClaudeAdapterLive", () => {
const promptMessage = yield* Effect.promise(() => readFirstPromptMessage(createInput));
assert.isDefined(promptMessage);
assert.deepEqual(promptMessage?.message.content, [
{
type: "image",
source: {
type: "base64",
media_type: "image/png",
data: "AQIDBA==",
},
},
{
type: "text",
text: "What's in this image?",
},
]);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
);
});
// The Claude CLI reads a streamed user message as a slash-command invocation
// only when the final content block is text. Leading with the text block sent
// every image-carrying turn down the plain-prompt path, so `/skill args`
// reached the agent unexpanded with no error anywhere.
it.effect("puts the command text last so attachments do not suppress expansion", () => {
const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "claude-attachments-"));
const harness = makeHarness({
cwd: "/tmp/project-claude-command-attachments",
baseDir,
});
return Effect.gen(function* () {
yield* Effect.addFinalizer(() =>
Effect.sync(() =>
NodeFS.rmSync(baseDir, {
recursive: true,
force: true,
}),
),
);
const adapter = yield* ClaudeAdapter;
const { attachmentsDir } = yield* ServerConfig;
const imageAttachment = {
type: "image" as const,
id: "thread-claude-attachment-22345678-1234-1234-1234-123456789abc",
name: "screenshot.png",
mimeType: "image/png",
sizeBytes: 4,
};
const fileAttachment = {
type: "file" as const,
id: "thread-claude-attachment-32345678-1234-1234-1234-123456789abc",
name: "notes.pdf",
mimeType: "application/pdf",
sizeBytes: 4,
};
for (const attachment of [imageAttachment, fileAttachment]) {
const attachmentPath = NodePath.join(attachmentsDir, attachmentRelativePath(attachment)!);
NodeFS.mkdirSync(NodePath.dirname(attachmentPath), { recursive: true });
NodeFS.writeFileSync(attachmentPath, Uint8Array.from([1, 2, 3, 4]));
}
const session = yield* adapter.startSession({
threadId: THREAD_ID,
provider: ProviderDriverKind.make("claudeAgent"),
runtimeMode: "full-access",
});
yield* adapter.sendTurn({
threadId: session.threadId,
input: "/flow-patterns hello",
attachments: [],
});
yield* adapter.sendTurn({
threadId: session.threadId,
input: "/flow-patterns hello",
attachments: [imageAttachment],
});
yield* adapter.sendTurn({
threadId: session.threadId,
input: "/flow-patterns hello",
attachments: [fileAttachment],
});
const prompts = yield* Effect.promise(() =>
readPromptMessages(harness.getLastCreateQueryInput(), 3),
);
const commandBlock = {
type: "text" as const,
text: "/flow-patterns hello",
};
assert.deepEqual(prompts[0]?.message.content, [commandBlock]);
assert.deepEqual(prompts[1]?.message.content, [
{
type: "image",
source: {
@@ -804,7 +918,12 @@ describe("ClaudeAdapterLive", () => {
data: "AQIDBA==",
},
},
commandBlock,
]);
// Non-image attachments never become content blocks. Claude reaches them
// through the path line ProviderService writes into the prompt, so the
// text block stays last on its own.
assert.deepEqual(prompts[2]?.message.content, [commandBlock]);
}).pipe(
Effect.provideService(Random.Random, makeDeterministicRandomService()),
Effect.provide(harness.layer),
@@ -1316,12 +1316,8 @@ const buildUserMessageEffect = Effect.fn("buildUserMessageEffect")(function* (
// therefore split into [leading text, "/name trailing text"] so the CLI
// runs it natively and the prose around it survives. See ClaudeSkillDispatch.
const dispatch = planClaudeSkillDispatch(text, dependencies.skillNames);
if (dispatch) {
if (dispatch.leadingText !== undefined) {
sdkContent.push({ type: "text", text: dispatch.leadingText });
}
} else if (text.length > 0) {
sdkContent.push({ type: "text", text });
if (dispatch?.leadingText !== undefined) {
sdkContent.push({ type: "text", text: dispatch.leadingText });
}
for (const attachment of input.attachments ?? []) {
@@ -1371,10 +1367,15 @@ const buildUserMessageEffect = Effect.fn("buildUserMessageEffect")(function* (
);
}
// Images go before the command block: a text block after them still
// expands, a command block followed by an image does not.
// The final text block goes last on purpose. The Claude CLI only reads a
// streamed user message as a slash-command invocation when the last content
// block is text; image blocks ahead of it ride along as preceding input.
// Leading with the text made every image-carrying turn fall back to a plain
// prompt, so a hand-typed `/skill args` reached the agent unexpanded.
if (dispatch) {
sdkContent.push({ type: "text", text: dispatch.commandText });
} else if (text.length > 0) {
sdkContent.push({ type: "text", text });
}
return buildUserMessage({ sdkContent });