Skip to content

Commit 54bf476

Browse files
committed
fix(feishu): persist durable attachment reference on inbound message (#108534)
When a Feishu file/image/audio/video message is downloaded, buildFeishuAgentBody previously only carried the inline <media:...> placeholder (or truncated preview) into the agent-facing body. The FeishuMediaInfo entries produced alongside toInboundMediaFacts — including the exact local path under ~/.openclaw/media/inbound — were dropped from the persisted message body. Reporter scenario: a 411 KB SQL file was truncated inline; a follow-up turn had no durable reference and had to scan the shared inbound media directory to guess which file belonged to the message. Fix: append a small <attachments> block listing each downloaded attachment's path, kind, and contentType (when available). Inline preview and toInboundMediaFacts shape are unchanged. Scope is intentionally narrower than #19330 (shared inbound isolation).
1 parent 957cc81 commit 54bf476

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎extensions/feishu/src/bot-agent-body.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
22
import type { FeishuPermissionError } from "./bot-sender-name.js";
3-
import type { FeishuMessageContext } from "./types.js";
3+
import type { FeishuMediaInfo, FeishuMessageContext } from "./types.js";
44

55
const MAX_MENTION_CONTEXT_NAME_LENGTH = 80;
66

@@ -17,6 +17,27 @@ function formatMentionNameForAgentContext(name: string): string {
1717
return JSON.stringify(bounded || "unknown");
1818
}
1919

20+
function inferAttachmentKind(placeholder: string | undefined): string {
21+
if (!placeholder) return "file";
22+
const match = /^<media:([^>]+)>$/.exec(placeholder);
23+
return match ? match[1] : "file";
24+
}
25+
26+
function formatAttachmentsBlock(mediaList: readonly FeishuMediaInfo[]): string {
27+
const lines: string[] = ["<attachments>"];
28+
for (const media of mediaList) {
29+
if (!media?.path) continue;
30+
lines.push(`- path: ${media.path}`);
31+
lines.push(` kind: ${inferAttachmentKind(media.placeholder)}`);
32+
if (media.contentType) {
33+
lines.push(` contentType: ${media.contentType}`);
34+
}
35+
}
36+
lines.push("</attachments>");
37+
// Only emit the block if at least one attachment produced a path entry.
38+
return lines.length > 2 ? lines.join("\n") : "";
39+
}
40+
2041
export function buildFeishuAgentBody(params: {
2142
ctx: Pick<
2243
FeishuMessageContext,
@@ -25,8 +46,9 @@ export function buildFeishuAgentBody(params: {
2546
quotedContent?: string;
2647
permissionErrorForAgent?: FeishuPermissionError;
2748
botOpenId?: string;
49+
mediaList?: readonly FeishuMediaInfo[];
2850
}): string {
29-
const { ctx, quotedContent, permissionErrorForAgent, botOpenId } = params;
51+
const { ctx, quotedContent, permissionErrorForAgent, botOpenId, mediaList } = params;
3052
let messageBody = ctx.content;
3153
if (quotedContent) {
3254
messageBody = `[Replying to: "${quotedContent}"]\n\n${ctx.content}`;
@@ -56,5 +78,14 @@ export function buildFeishuAgentBody(params: {
5678
const grantUrl = permissionErrorForAgent.grantUrl ?? "";
5779
messageBody += `\n\n[System: The bot encountered a Feishu API permission error. Please inform the user about this issue and provide the permission grant URL for the admin to authorize. Permission grant URL: ${grantUrl}]`;
5880
}
81+
if (mediaList && mediaList.length > 0) {
82+
// Persist a durable, machine-readable reference to each downloaded attachment
83+
// so follow-up turns can locate the file even when the inline preview was
84+
// truncated (see #108534).
85+
const attachmentsBlock = formatAttachmentsBlock(mediaList);
86+
if (attachmentsBlock) {
87+
messageBody += `\n\n${attachmentsBlock}`;
88+
}
89+
}
5990
return messageBody;
6091
}

‎extensions/feishu/src/bot.helpers.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,46 @@ describe("buildFeishuAgentBody", () => {
6161
expect(body).not.toContain("\ud83d");
6262
expect(body).not.toContain("\ude00");
6363
});
64+
65+
it("appends a durable attachments block for inbound media (#108534)", () => {
66+
const body = buildFeishuAgentBody({
67+
ctx: {
68+
content: "<media:document> (large-schema.sql)",
69+
senderName: "Sender Name",
70+
senderOpenId: "ou-sender",
71+
messageId: "msg-42",
72+
},
73+
mediaList: [
74+
{
75+
path: "/home/user/.openclaw/media/inbound/abc-123.sql",
76+
contentType: "application/sql",
77+
placeholder: "<media:document>",
78+
},
79+
],
80+
});
81+
82+
expect(body).toContain("<attachments>");
83+
expect(body).toContain("- path: /home/user/.openclaw/media/inbound/abc-123.sql");
84+
expect(body).toContain("kind: document");
85+
expect(body).toContain("contentType: application/sql");
86+
expect(body).toContain("</attachments>");
87+
// Inline preview remains untouched.
88+
expect(body).toContain("<media:document> (large-schema.sql)");
89+
});
90+
91+
it("omits the attachments block when mediaList is empty", () => {
92+
const body = buildFeishuAgentBody({
93+
ctx: {
94+
content: "plain text",
95+
senderName: "Sender Name",
96+
senderOpenId: "ou-sender",
97+
messageId: "msg-42",
98+
},
99+
mediaList: [],
100+
});
101+
102+
expect(body).not.toContain("<attachments>");
103+
});
64104
});
65105

66106
describe("parseMessageContent media placeholders", () => {

‎extensions/feishu/src/bot.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ export async function handleFeishuMessage(params: {
979979
quotedContent,
980980
permissionErrorForAgent,
981981
botOpenId,
982+
mediaList,
982983
});
983984
const envelopeFrom = isGroup ? `${ctx.chatId}:${ctx.senderOpenId}` : ctx.senderOpenId;
984985
if (permissionErrorForAgent) {

0 commit comments

Comments
 (0)