Skip to content

Commit c02168a

Browse files
lnieuwenhuist3dotgg
authored andcommitted
fix(claude): expand slash commands when a message has attachments
The Claude CLI only treats a streamed user message as a slash-command invocation when the final content block is text; earlier blocks ride along as preceding input blocks and survive the expansion. The adapter appended image blocks after the text, so any turn carrying an attachment lost the command silently. Send the text block last so an image never displaces it.
1 parent 5b8445b commit c02168a

2 files changed

Lines changed: 128 additions & 8 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,30 @@ async function readFirstPromptMessage(
272272
return next.value;
273273
}
274274

275+
/** Drains the first `count` queued prompts so consecutive turns can be compared. */
276+
async function readPromptMessages(
277+
input:
278+
| {
279+
readonly prompt: AsyncIterable<SDKUserMessage>;
280+
}
281+
| undefined,
282+
count: number,
283+
): Promise<Array<SDKUserMessage>> {
284+
const iterator = input?.prompt[Symbol.asyncIterator]();
285+
if (!iterator) {
286+
return [];
287+
}
288+
const messages: Array<SDKUserMessage> = [];
289+
while (messages.length < count) {
290+
const next = await iterator.next();
291+
if (next.done) {
292+
break;
293+
}
294+
messages.push(next.value);
295+
}
296+
return messages;
297+
}
298+
275299
const THREAD_ID = ThreadId.make("thread-claude-1");
276300
const RESUME_THREAD_ID = ThreadId.make("thread-claude-resume");
277301
const SYNTHETIC_SUBAGENT_MODEL = "claude-synthetic-subagent[expanded]";
@@ -792,10 +816,100 @@ describe("ClaudeAdapterLive", () => {
792816
const promptMessage = yield* Effect.promise(() => readFirstPromptMessage(createInput));
793817
assert.isDefined(promptMessage);
794818
assert.deepEqual(promptMessage?.message.content, [
819+
{
820+
type: "image",
821+
source: {
822+
type: "base64",
823+
media_type: "image/png",
824+
data: "AQIDBA==",
825+
},
826+
},
795827
{
796828
type: "text",
797829
text: "What's in this image?",
798830
},
831+
]);
832+
}).pipe(
833+
Effect.provideService(Random.Random, makeDeterministicRandomService()),
834+
Effect.provide(harness.layer),
835+
);
836+
});
837+
838+
// The Claude CLI reads a streamed user message as a slash-command invocation
839+
// only when the final content block is text. Leading with the text block sent
840+
// every image-carrying turn down the plain-prompt path, so `/skill args`
841+
// reached the agent unexpanded with no error anywhere.
842+
it.effect("puts the command text last so attachments do not suppress expansion", () => {
843+
const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "claude-attachments-"));
844+
const harness = makeHarness({
845+
cwd: "/tmp/project-claude-command-attachments",
846+
baseDir,
847+
});
848+
return Effect.gen(function* () {
849+
yield* Effect.addFinalizer(() =>
850+
Effect.sync(() =>
851+
NodeFS.rmSync(baseDir, {
852+
recursive: true,
853+
force: true,
854+
}),
855+
),
856+
);
857+
858+
const adapter = yield* ClaudeAdapter;
859+
const { attachmentsDir } = yield* ServerConfig;
860+
861+
const imageAttachment = {
862+
type: "image" as const,
863+
id: "thread-claude-attachment-22345678-1234-1234-1234-123456789abc",
864+
name: "screenshot.png",
865+
mimeType: "image/png",
866+
sizeBytes: 4,
867+
};
868+
const fileAttachment = {
869+
type: "file" as const,
870+
id: "thread-claude-attachment-32345678-1234-1234-1234-123456789abc",
871+
name: "notes.pdf",
872+
mimeType: "application/pdf",
873+
sizeBytes: 4,
874+
};
875+
for (const attachment of [imageAttachment, fileAttachment]) {
876+
const attachmentPath = NodePath.join(attachmentsDir, attachmentRelativePath(attachment)!);
877+
NodeFS.mkdirSync(NodePath.dirname(attachmentPath), { recursive: true });
878+
NodeFS.writeFileSync(attachmentPath, Uint8Array.from([1, 2, 3, 4]));
879+
}
880+
881+
const session = yield* adapter.startSession({
882+
threadId: THREAD_ID,
883+
provider: ProviderDriverKind.make("claudeAgent"),
884+
runtimeMode: "full-access",
885+
});
886+
887+
yield* adapter.sendTurn({
888+
threadId: session.threadId,
889+
input: "/flow-patterns hello",
890+
attachments: [],
891+
});
892+
yield* adapter.sendTurn({
893+
threadId: session.threadId,
894+
input: "/flow-patterns hello",
895+
attachments: [imageAttachment],
896+
});
897+
yield* adapter.sendTurn({
898+
threadId: session.threadId,
899+
input: "/flow-patterns hello",
900+
attachments: [fileAttachment],
901+
});
902+
903+
const prompts = yield* Effect.promise(() =>
904+
readPromptMessages(harness.getLastCreateQueryInput(), 3),
905+
);
906+
const commandBlock = {
907+
type: "text" as const,
908+
text: "/flow-patterns hello",
909+
};
910+
911+
assert.deepEqual(prompts[0]?.message.content, [commandBlock]);
912+
assert.deepEqual(prompts[1]?.message.content, [
799913
{
800914
type: "image",
801915
source: {
@@ -804,7 +918,12 @@ describe("ClaudeAdapterLive", () => {
804918
data: "AQIDBA==",
805919
},
806920
},
921+
commandBlock,
807922
]);
923+
// Non-image attachments never become content blocks. Claude reaches them
924+
// through the path line ProviderService writes into the prompt, so the
925+
// text block stays last on its own.
926+
assert.deepEqual(prompts[2]?.message.content, [commandBlock]);
808927
}).pipe(
809928
Effect.provideService(Random.Random, makeDeterministicRandomService()),
810929
Effect.provide(harness.layer),

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,12 +1316,8 @@ const buildUserMessageEffect = Effect.fn("buildUserMessageEffect")(function* (
13161316
// therefore split into [leading text, "/name trailing text"] so the CLI
13171317
// runs it natively and the prose around it survives. See ClaudeSkillDispatch.
13181318
const dispatch = planClaudeSkillDispatch(text, dependencies.skillNames);
1319-
if (dispatch) {
1320-
if (dispatch.leadingText !== undefined) {
1321-
sdkContent.push({ type: "text", text: dispatch.leadingText });
1322-
}
1323-
} else if (text.length > 0) {
1324-
sdkContent.push({ type: "text", text });
1319+
if (dispatch?.leadingText !== undefined) {
1320+
sdkContent.push({ type: "text", text: dispatch.leadingText });
13251321
}
13261322

13271323
for (const attachment of input.attachments ?? []) {
@@ -1371,10 +1367,15 @@ const buildUserMessageEffect = Effect.fn("buildUserMessageEffect")(function* (
13711367
);
13721368
}
13731369

1374-
// Images go before the command block: a text block after them still
1375-
// expands, a command block followed by an image does not.
1370+
// The final text block goes last on purpose. The Claude CLI only reads a
1371+
// streamed user message as a slash-command invocation when the last content
1372+
// block is text; image blocks ahead of it ride along as preceding input.
1373+
// Leading with the text made every image-carrying turn fall back to a plain
1374+
// prompt, so a hand-typed `/skill args` reached the agent unexpanded.
13761375
if (dispatch) {
13771376
sdkContent.push({ type: "text", text: dispatch.commandText });
1377+
} else if (text.length > 0) {
1378+
sdkContent.push({ type: "text", text });
13781379
}
13791380

13801381
return buildUserMessage({ sdkContent });

0 commit comments

Comments
 (0)