chore: surface media URL in command prompts and tests

This commit is contained in:
Peter Steinberger
2025-11-25 05:20:50 +01:00
parent 6d41df2941
commit 54c763a679
2 changed files with 25 additions and 6 deletions

View File

@@ -166,10 +166,18 @@ export async function getReplyFromConfig(
const prefixedBody = bodyPrefix const prefixedBody = bodyPrefix
? `${bodyPrefix}${sessionCtx.BodyStripped ?? sessionCtx.Body ?? ""}` ? `${bodyPrefix}${sessionCtx.BodyStripped ?? sessionCtx.Body ?? ""}`
: (sessionCtx.BodyStripped ?? sessionCtx.Body); : (sessionCtx.BodyStripped ?? sessionCtx.Body);
const mediaNote =
ctx.MediaPath && ctx.MediaPath.length
? `[media attached: ${ctx.MediaPath}${ctx.MediaType ? ` (${ctx.MediaType})` : ""}${ctx.MediaUrl ? ` | ${ctx.MediaUrl}` : ""}]`
: undefined;
// For command prompts we prepend the media note so Claude et al. see it; text replies stay clean.
const commandBody = mediaNote
? `${mediaNote}\n${prefixedBody ?? ""}`.trim()
: prefixedBody;
const templatingCtx: TemplateContext = { const templatingCtx: TemplateContext = {
...sessionCtx, ...sessionCtx,
Body: prefixedBody, Body: commandBody,
BodyStripped: prefixedBody, BodyStripped: commandBody,
}; };
// Optional allowlist by origin number (E.164 without whatsapp: prefix) // Optional allowlist by origin number (E.164 without whatsapp: prefix)
@@ -273,6 +281,12 @@ export async function getReplyFromConfig(
); );
const rawStdout = stdout.trim(); const rawStdout = stdout.trim();
let trimmed = rawStdout; let trimmed = rawStdout;
let mediaFromCommand: string | undefined;
const mediaMatch = /MEDIA:\s*(.+)$/im.exec(rawStdout);
if (mediaMatch?.[1]) {
mediaFromCommand = mediaMatch[1].trim();
trimmed = rawStdout.replace(mediaMatch[0], "").trim();
}
if (stderr?.trim()) { if (stderr?.trim()) {
logVerbose(`Command auto-reply stderr: ${stderr.trim()}`); logVerbose(`Command auto-reply stderr: ${stderr.trim()}`);
} }
@@ -311,7 +325,10 @@ export async function getReplyFromConfig(
); );
return undefined; return undefined;
} }
return trimmed ? { text: trimmed, mediaUrl: reply.mediaUrl } : undefined; const mediaUrl = mediaFromCommand ?? reply.mediaUrl;
return trimmed || mediaUrl
? { text: trimmed || undefined, mediaUrl }
: undefined;
} catch (err) { } catch (err) {
const elapsed = Date.now() - started; const elapsed = Date.now() - started;
const anyErr = err as { killed?: boolean; signal?: string }; const anyErr = err as { killed?: boolean; signal?: string };

View File

@@ -94,8 +94,8 @@ describe("config and templating", () => {
const cfg = { const cfg = {
inbound: { inbound: {
reply: { reply: {
mode: "text" as const, mode: "command" as const,
text: "{{MediaPath}} {{MediaType}} {{MediaUrl}}", command: ["echo", "{{Body}}"],
}, },
}, },
}; };
@@ -111,7 +111,9 @@ describe("config and templating", () => {
undefined, undefined,
cfg, cfg,
); );
expect(result?.text).toBe("/tmp/a.jpg image/jpeg http://example.com/a.jpg"); expect(result?.text).toContain("/tmp/a.jpg");
expect(result?.text).toContain("image/jpeg");
expect(result?.text).toContain("http://example.com/a.jpg");
}); });
it("getReplyFromConfig runs command and manages session store", async () => { it("getReplyFromConfig runs command and manages session store", async () => {