Skip to content

Commit 63ad5b4

Browse files
committed
fix: send structured message attachments
1 parent 3fd4b02 commit 63ad5b4

6 files changed

Lines changed: 92 additions & 2 deletions

File tree

‎extensions/codex/src/app-server/dynamic-tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ function collectMediaUrls(record: Record<string, unknown>): string[] {
427427
return;
428428
}
429429
const attachment = value as Record<string, unknown>;
430-
for (const key of ["media", "mediaUrl", "path", "filePath", "fileUrl"]) {
430+
for (const key of ["media", "mediaUrl", "path", "filePath", "fileUrl", "url"]) {
431431
pushMediaUrl(attachment[key]);
432432
}
433433
};

‎src/agents/pi-embedded-subscribe.handlers.tools.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ function collectMessagingMediaUrlsFromRecord(record: Record<string, unknown>): s
351351
pushUniqueMediaUrl(urls, seen, attachment.path);
352352
pushUniqueMediaUrl(urls, seen, attachment.filePath);
353353
pushUniqueMediaUrl(urls, seen, attachment.fileUrl);
354+
pushUniqueMediaUrl(urls, seen, attachment.url);
354355
};
355356

356357
pushUniqueMediaUrl(urls, seen, record.media);

‎src/agents/tools/message-tool.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,25 @@ function buildSendSchema(options: { includePresentation: boolean; includeDeliver
188188
caption: Type.Optional(Type.String()),
189189
path: Type.Optional(Type.String()),
190190
filePath: Type.Optional(Type.String()),
191+
attachments: Type.Optional(
192+
Type.Array(
193+
Type.Object({
194+
type: Type.Optional(stringEnum(["image", "audio", "video", "file"])),
195+
media: Type.Optional(Type.String()),
196+
mediaUrl: Type.Optional(Type.String()),
197+
path: Type.Optional(Type.String()),
198+
filePath: Type.Optional(Type.String()),
199+
fileUrl: Type.Optional(Type.String()),
200+
url: Type.Optional(Type.String()),
201+
name: Type.Optional(Type.String()),
202+
mimeType: Type.Optional(Type.String()),
203+
}),
204+
{
205+
description:
206+
"Structured media attachments to send with the message. Each item needs media/mediaUrl/path/filePath/fileUrl/url.",
207+
},
208+
),
209+
),
191210
replyTo: Type.Optional(Type.String()),
192211
threadId: Type.Optional(Type.String()),
193212
asVoice: Type.Optional(Type.Boolean()),

‎src/infra/outbound/message-action-param-keys.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { normalizeOptionalString } from "../../shared/string-coerce.js";
33
const STANDARD_MESSAGE_ACTION_PARAM_KEYS = new Set([
44
"accountId",
55
"asDocument",
6+
"attachments",
67
"base64",
78
"bestEffort",
89
"caption",

‎src/infra/outbound/message-action-runner.media.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,41 @@ describe("runMessageAction media behavior", () => {
298298
expect(sendArgs.asVoice).toBe(true);
299299
});
300300

301+
it("sends structured attachments as media urls", async () => {
302+
setActivePluginRegistry(
303+
createTestRegistry([
304+
{
305+
pluginId: "workspace",
306+
source: "test",
307+
plugin: workspacePlugin,
308+
},
309+
]),
310+
);
311+
312+
await withSandbox(async (sandboxDir) => {
313+
const result = await runDrySend({
314+
cfg: workspaceConfig,
315+
actionParams: {
316+
channel: "workspace",
317+
target: "12345678",
318+
message: "track ready",
319+
attachments: [{ path: "./song.mp3" }, { filePath: "/workspace/cover.png" }],
320+
},
321+
sandboxRoot: sandboxDir,
322+
});
323+
324+
expect(result.kind).toBe("send");
325+
if (result.kind !== "send") {
326+
throw new Error("expected send result");
327+
}
328+
expect(result.sendResult?.mediaUrl).toBe(path.join(sandboxDir, "song.mp3"));
329+
expect(result.sendResult?.mediaUrls).toEqual([
330+
path.join(sandboxDir, "song.mp3"),
331+
path.join(sandboxDir, "cover.png"),
332+
]);
333+
});
334+
});
335+
301336
describe("sendAttachment hydration", () => {
302337
const cfg = {
303338
channels: {

‎src/infra/outbound/message-action-runner.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,35 @@ type SendPayloadParts = {
468468
silent?: boolean;
469469
};
470470

471+
function collectMessageAttachmentMediaHints(value: unknown): string[] {
472+
if (!Array.isArray(value)) {
473+
return [];
474+
}
475+
const mediaUrls: string[] = [];
476+
const seen = new Set<string>();
477+
const pushMedia = (entry: unknown) => {
478+
const normalized = normalizeOptionalString(entry);
479+
if (!normalized || seen.has(normalized)) {
480+
return;
481+
}
482+
seen.add(normalized);
483+
mediaUrls.push(normalized);
484+
};
485+
for (const attachment of value) {
486+
if (!attachment || typeof attachment !== "object" || Array.isArray(attachment)) {
487+
continue;
488+
}
489+
const record = attachment as Record<string, unknown>;
490+
pushMedia(record.media);
491+
pushMedia(record.mediaUrl);
492+
pushMedia(record.path);
493+
pushMedia(record.filePath);
494+
pushMedia(record.fileUrl);
495+
pushMedia(record.url);
496+
}
497+
return mediaUrls;
498+
}
499+
471500
function hasExplicitRouteParam(params: Record<string, unknown>): boolean {
472501
for (const key of ["channel", "target", "to", "channelId"]) {
473502
if (normalizeOptionalString(params[key])) {
@@ -692,12 +721,14 @@ async function buildSendPayloadParts(params: {
692721
readStringParam(actionParams, "path", { trim: false }) ??
693722
readStringParam(actionParams, "filePath", { trim: false }) ??
694723
readStringParam(actionParams, "fileUrl", { trim: false });
724+
const attachmentMediaHints = collectMessageAttachmentMediaHints(actionParams.attachments);
725+
const hasMediaHint = Boolean(mediaHint) || attachmentMediaHints.length > 0;
695726
const hasPresentation = hasMessagePresentationBlocks(actionParams.presentation);
696727
const hasInteractive = hasInteractiveReplyBlocks(actionParams.interactive);
697728
const caption = readStringParam(actionParams, "caption", { allowEmpty: true }) ?? "";
698729
let message =
699730
readStringParam(actionParams, "message", {
700-
required: !mediaHint && !hasPresentation && !hasInteractive,
731+
required: !hasMediaHint && !hasPresentation && !hasInteractive,
701732
allowEmpty: true,
702733
}) ?? "";
703734
if (message.includes("\\n")) {
@@ -719,6 +750,9 @@ async function buildSendPayloadParts(params: {
719750
mergedMediaUrls.push(trimmed);
720751
};
721752
pushMedia(mediaHint);
753+
for (const attachmentMediaHint of attachmentMediaHints) {
754+
pushMedia(attachmentMediaHint);
755+
}
722756
for (const url of parsed.mediaUrls ?? []) {
723757
pushMedia(url);
724758
}

0 commit comments

Comments
 (0)