@openclaw/feishu 2026.5.2-beta.2 → 2026.5.3-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/accounts-Ba3-WP1z.js +423 -0
- package/dist/api.js +2280 -0
- package/dist/app-registration-B8qc1MCM.js +184 -0
- package/dist/audio-preflight.runtime-BPlzkO3l.js +7 -0
- package/dist/card-interaction-BfRLgvw_.js +96 -0
- package/dist/channel-CSD_Jt8I.js +1668 -0
- package/dist/channel-entry.js +22 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-DYsXcD36.js +700 -0
- package/dist/client-DBVoQL5w.js +157 -0
- package/dist/contract-api.js +9 -0
- package/dist/conversation-id-DWS3Ep2A.js +139 -0
- package/dist/directory.static-f3EeoRJd.js +44 -0
- package/dist/drive-C5eJLJr7.js +883 -0
- package/dist/index.js +68 -0
- package/dist/monitor-CT189QfR.js +60 -0
- package/dist/monitor.account-dJV2jO8C.js +4990 -0
- package/dist/monitor.state-DYM02ipp.js +100 -0
- package/dist/policy-D6c-wMPl.js +118 -0
- package/dist/probe-BNzzU_uR.js +149 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/runtime-CG0DuRCy.js +8 -0
- package/dist/runtime-api.js +14 -0
- package/dist/secret-contract-Dm4Z_zQN.js +119 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/security-audit-DqJdocrN.js +11 -0
- package/dist/security-audit-shared-ByuMx9cJ.js +38 -0
- package/dist/security-contract-api.js +2 -0
- package/dist/send-DowxxbpH.js +1218 -0
- package/dist/session-conversation-B4nrW-vo.js +27 -0
- package/dist/session-key-api.js +2 -0
- package/dist/setup-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/subagent-hooks-C3UhPVLV.js +227 -0
- package/dist/subagent-hooks-api.js +23 -0
- package/dist/targets-JMFJRKSe.js +48 -0
- package/dist/thread-bindings-BmS6TLes.js +222 -0
- package/package.json +15 -6
- package/api.ts +0 -31
- package/channel-entry.ts +0 -20
- package/channel-plugin-api.ts +0 -1
- package/contract-api.ts +0 -16
- package/index.ts +0 -82
- package/runtime-api.ts +0 -55
- package/secret-contract-api.ts +0 -5
- package/security-contract-api.ts +0 -1
- package/session-key-api.ts +0 -1
- package/setup-api.ts +0 -3
- package/setup-entry.test.ts +0 -14
- package/setup-entry.ts +0 -13
- package/src/accounts.test.ts +0 -459
- package/src/accounts.ts +0 -326
- package/src/app-registration.ts +0 -331
- package/src/approval-auth.test.ts +0 -24
- package/src/approval-auth.ts +0 -25
- package/src/async.test.ts +0 -35
- package/src/async.ts +0 -104
- package/src/audio-preflight.runtime.ts +0 -9
- package/src/bitable.test.ts +0 -131
- package/src/bitable.ts +0 -762
- package/src/bot-content.ts +0 -474
- package/src/bot-group-name.test.ts +0 -108
- package/src/bot-runtime-api.ts +0 -12
- package/src/bot-sender-name.ts +0 -125
- package/src/bot.broadcast.test.ts +0 -463
- package/src/bot.card-action.test.ts +0 -577
- package/src/bot.checkBotMentioned.test.ts +0 -265
- package/src/bot.helpers.test.ts +0 -118
- package/src/bot.stripBotMention.test.ts +0 -126
- package/src/bot.test.ts +0 -3040
- package/src/bot.ts +0 -1559
- package/src/card-action.ts +0 -447
- package/src/card-interaction.test.ts +0 -129
- package/src/card-interaction.ts +0 -159
- package/src/card-test-helpers.ts +0 -47
- package/src/card-ux-approval.ts +0 -65
- package/src/card-ux-launcher.test.ts +0 -99
- package/src/card-ux-launcher.ts +0 -121
- package/src/card-ux-shared.ts +0 -33
- package/src/channel-runtime-api.ts +0 -16
- package/src/channel.runtime.ts +0 -47
- package/src/channel.test.ts +0 -959
- package/src/channel.ts +0 -1313
- package/src/chat-schema.ts +0 -25
- package/src/chat.test.ts +0 -196
- package/src/chat.ts +0 -188
- package/src/client.test.ts +0 -433
- package/src/client.ts +0 -290
- package/src/comment-dispatcher-runtime-api.ts +0 -6
- package/src/comment-dispatcher.test.ts +0 -169
- package/src/comment-dispatcher.ts +0 -107
- package/src/comment-handler-runtime-api.ts +0 -3
- package/src/comment-handler.test.ts +0 -486
- package/src/comment-handler.ts +0 -309
- package/src/comment-reaction.test.ts +0 -166
- package/src/comment-reaction.ts +0 -259
- package/src/comment-shared.test.ts +0 -182
- package/src/comment-shared.ts +0 -406
- package/src/comment-target.ts +0 -44
- package/src/config-schema.test.ts +0 -309
- package/src/config-schema.ts +0 -333
- package/src/conversation-id.test.ts +0 -18
- package/src/conversation-id.ts +0 -199
- package/src/dedup-runtime-api.ts +0 -1
- package/src/dedup.ts +0 -141
- package/src/directory.static.ts +0 -61
- package/src/directory.test.ts +0 -136
- package/src/directory.ts +0 -124
- package/src/doc-schema.ts +0 -182
- package/src/docx-batch-insert.test.ts +0 -91
- package/src/docx-batch-insert.ts +0 -223
- package/src/docx-color-text.ts +0 -154
- package/src/docx-table-ops.test.ts +0 -53
- package/src/docx-table-ops.ts +0 -316
- package/src/docx-types.ts +0 -38
- package/src/docx.account-selection.test.ts +0 -79
- package/src/docx.test.ts +0 -685
- package/src/docx.ts +0 -1616
- package/src/drive-schema.ts +0 -92
- package/src/drive.test.ts +0 -1219
- package/src/drive.ts +0 -829
- package/src/dynamic-agent.ts +0 -137
- package/src/event-types.ts +0 -45
- package/src/external-keys.test.ts +0 -20
- package/src/external-keys.ts +0 -19
- package/src/lifecycle.test-support.ts +0 -220
- package/src/media.test.ts +0 -900
- package/src/media.ts +0 -861
- package/src/mention-target.types.ts +0 -5
- package/src/mention.ts +0 -114
- package/src/message-action-contract.ts +0 -13
- package/src/monitor-state-runtime-api.ts +0 -7
- package/src/monitor-transport-runtime-api.ts +0 -7
- package/src/monitor.account.ts +0 -468
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +0 -219
- package/src/monitor.bot-identity.ts +0 -86
- package/src/monitor.bot-menu-handler.ts +0 -165
- package/src/monitor.bot-menu.lifecycle.test-support.ts +0 -224
- package/src/monitor.bot-menu.test.ts +0 -178
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +0 -264
- package/src/monitor.card-action.lifecycle.test-support.ts +0 -373
- package/src/monitor.cleanup.test.ts +0 -376
- package/src/monitor.comment-notice-handler.ts +0 -105
- package/src/monitor.comment.test.ts +0 -937
- package/src/monitor.comment.ts +0 -1386
- package/src/monitor.lifecycle.test.ts +0 -4
- package/src/monitor.message-handler.ts +0 -339
- package/src/monitor.reaction.lifecycle.test-support.ts +0 -68
- package/src/monitor.reaction.test.ts +0 -713
- package/src/monitor.startup.test.ts +0 -192
- package/src/monitor.startup.ts +0 -74
- package/src/monitor.state.defaults.test.ts +0 -46
- package/src/monitor.state.ts +0 -170
- package/src/monitor.synthetic-error.ts +0 -18
- package/src/monitor.test-mocks.ts +0 -45
- package/src/monitor.transport.ts +0 -424
- package/src/monitor.ts +0 -100
- package/src/monitor.webhook-e2e.test.ts +0 -272
- package/src/monitor.webhook-security.test.ts +0 -264
- package/src/monitor.webhook.test-helpers.ts +0 -116
- package/src/outbound-runtime-api.ts +0 -1
- package/src/outbound.test.ts +0 -935
- package/src/outbound.ts +0 -718
- package/src/perm-schema.ts +0 -52
- package/src/perm.ts +0 -170
- package/src/pins.ts +0 -108
- package/src/policy.test.ts +0 -334
- package/src/policy.ts +0 -236
- package/src/post.test.ts +0 -105
- package/src/post.ts +0 -275
- package/src/probe.test.ts +0 -275
- package/src/probe.ts +0 -166
- package/src/processing-claims.ts +0 -59
- package/src/qr-terminal.ts +0 -1
- package/src/reactions.ts +0 -123
- package/src/reasoning-preview.test.ts +0 -59
- package/src/reasoning-preview.ts +0 -20
- package/src/reply-dispatcher-runtime-api.ts +0 -7
- package/src/reply-dispatcher.test.ts +0 -1144
- package/src/reply-dispatcher.ts +0 -650
- package/src/runtime.ts +0 -9
- package/src/secret-contract.ts +0 -145
- package/src/secret-input.ts +0 -1
- package/src/security-audit-shared.ts +0 -69
- package/src/security-audit.test.ts +0 -61
- package/src/security-audit.ts +0 -1
- package/src/send-result.ts +0 -29
- package/src/send-target.test.ts +0 -80
- package/src/send-target.ts +0 -35
- package/src/send.reply-fallback.test.ts +0 -292
- package/src/send.test.ts +0 -550
- package/src/send.ts +0 -800
- package/src/sequential-key.test.ts +0 -72
- package/src/sequential-key.ts +0 -28
- package/src/sequential-queue.test.ts +0 -92
- package/src/sequential-queue.ts +0 -16
- package/src/session-conversation.ts +0 -42
- package/src/session-route.ts +0 -48
- package/src/setup-core.ts +0 -51
- package/src/setup-surface.test.ts +0 -174
- package/src/setup-surface.ts +0 -581
- package/src/streaming-card.test.ts +0 -190
- package/src/streaming-card.ts +0 -490
- package/src/subagent-hooks.test.ts +0 -603
- package/src/subagent-hooks.ts +0 -397
- package/src/targets.ts +0 -97
- package/src/test-support/lifecycle-test-support.ts +0 -453
- package/src/thread-bindings.test.ts +0 -143
- package/src/thread-bindings.ts +0 -330
- package/src/tool-account-routing.test.ts +0 -187
- package/src/tool-account.test.ts +0 -44
- package/src/tool-account.ts +0 -93
- package/src/tool-factory-test-harness.ts +0 -79
- package/src/tool-result.test.ts +0 -32
- package/src/tool-result.ts +0 -16
- package/src/tools-config.test.ts +0 -21
- package/src/tools-config.ts +0 -22
- package/src/types.ts +0 -104
- package/src/typing.test.ts +0 -144
- package/src/typing.ts +0 -214
- package/src/wiki-schema.ts +0 -55
- package/src/wiki.ts +0 -227
- package/subagent-hooks-api.ts +0 -31
- package/tsconfig.json +0 -16
package/src/media.ts
DELETED
|
@@ -1,861 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { Readable } from "node:stream";
|
|
4
|
-
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
5
|
-
import { mediaKindFromMime } from "openclaw/plugin-sdk/media-mime";
|
|
6
|
-
import { MEDIA_FFMPEG_MAX_AUDIO_DURATION_SECS, runFfmpeg } from "openclaw/plugin-sdk/media-runtime";
|
|
7
|
-
import {
|
|
8
|
-
resolvePreferredOpenClawTmpDir,
|
|
9
|
-
withTempDownloadPath,
|
|
10
|
-
} from "openclaw/plugin-sdk/temp-path";
|
|
11
|
-
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/text-runtime";
|
|
12
|
-
import type { ClawdbotConfig } from "../runtime-api.js";
|
|
13
|
-
import { resolveFeishuRuntimeAccount } from "./accounts.js";
|
|
14
|
-
import { createFeishuClient } from "./client.js";
|
|
15
|
-
import { requestFeishuApi } from "./comment-shared.js";
|
|
16
|
-
import { normalizeFeishuExternalKey } from "./external-keys.js";
|
|
17
|
-
import { getFeishuRuntime } from "./runtime.js";
|
|
18
|
-
import { assertFeishuMessageApiSuccess, toFeishuSendResult } from "./send-result.js";
|
|
19
|
-
import { resolveFeishuSendTarget } from "./send-target.js";
|
|
20
|
-
|
|
21
|
-
const FEISHU_MEDIA_HTTP_TIMEOUT_MS = 120_000;
|
|
22
|
-
const FEISHU_VOICE_FILE_NAME = "voice.ogg";
|
|
23
|
-
const FEISHU_VOICE_SAMPLE_RATE_HZ = 48_000;
|
|
24
|
-
const FEISHU_VOICE_BITRATE = "64k";
|
|
25
|
-
|
|
26
|
-
const FEISHU_TRANSCODABLE_AUDIO_EXTS = new Set([
|
|
27
|
-
".aac",
|
|
28
|
-
".aiff",
|
|
29
|
-
".alac",
|
|
30
|
-
".amr",
|
|
31
|
-
".caf",
|
|
32
|
-
".flac",
|
|
33
|
-
".m4a",
|
|
34
|
-
".mp3",
|
|
35
|
-
".oga",
|
|
36
|
-
".wav",
|
|
37
|
-
".webm",
|
|
38
|
-
".wma",
|
|
39
|
-
]);
|
|
40
|
-
|
|
41
|
-
export type DownloadImageResult = {
|
|
42
|
-
buffer: Buffer;
|
|
43
|
-
contentType?: string;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export type DownloadMessageResourceResult = {
|
|
47
|
-
buffer: Buffer;
|
|
48
|
-
contentType?: string;
|
|
49
|
-
fileName?: string;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
function createConfiguredFeishuMediaClient(params: { cfg: ClawdbotConfig; accountId?: string }): {
|
|
53
|
-
account: ReturnType<typeof resolveFeishuRuntimeAccount>;
|
|
54
|
-
client: ReturnType<typeof createFeishuClient>;
|
|
55
|
-
} {
|
|
56
|
-
const account = resolveFeishuRuntimeAccount({ cfg: params.cfg, accountId: params.accountId });
|
|
57
|
-
if (!account.configured) {
|
|
58
|
-
throw new Error(`Feishu account "${account.accountId}" not configured`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
account,
|
|
63
|
-
client: createFeishuClient({
|
|
64
|
-
...account,
|
|
65
|
-
httpTimeoutMs: FEISHU_MEDIA_HTTP_TIMEOUT_MS,
|
|
66
|
-
}),
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
type FeishuUploadResponse =
|
|
71
|
-
| Awaited<ReturnType<Lark.Client["im"]["image"]["create"]>>
|
|
72
|
-
| Awaited<ReturnType<Lark.Client["im"]["file"]["create"]>>;
|
|
73
|
-
|
|
74
|
-
type FeishuDownloadResponse =
|
|
75
|
-
| Awaited<ReturnType<Lark.Client["im"]["image"]["get"]>>
|
|
76
|
-
| Awaited<ReturnType<Lark.Client["im"]["file"]["get"]>>
|
|
77
|
-
| Awaited<ReturnType<Lark.Client["im"]["messageResource"]["get"]>>;
|
|
78
|
-
|
|
79
|
-
type FeishuHeaderMap = Record<string, string | string[]>;
|
|
80
|
-
type FeishuMessageResourceDownloadType = "image" | "file" | "media";
|
|
81
|
-
|
|
82
|
-
function asHeaderMap(value: object | undefined): FeishuHeaderMap | undefined {
|
|
83
|
-
if (!value) {
|
|
84
|
-
return undefined;
|
|
85
|
-
}
|
|
86
|
-
const entries = Object.entries(value);
|
|
87
|
-
if (entries.every(([, entry]) => typeof entry === "string" || Array.isArray(entry))) {
|
|
88
|
-
return Object.fromEntries(entries) as FeishuHeaderMap;
|
|
89
|
-
}
|
|
90
|
-
return undefined;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function extractFeishuUploadKey(
|
|
94
|
-
response: FeishuUploadResponse,
|
|
95
|
-
params: {
|
|
96
|
-
key: "image_key" | "file_key";
|
|
97
|
-
errorPrefix: string;
|
|
98
|
-
},
|
|
99
|
-
): string {
|
|
100
|
-
if (!response) {
|
|
101
|
-
throw new Error(`${params.errorPrefix}: empty response`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const wrappedResponse = response as {
|
|
105
|
-
image_key?: string;
|
|
106
|
-
file_key?: string;
|
|
107
|
-
code?: number;
|
|
108
|
-
msg?: string;
|
|
109
|
-
data?: Partial<Record<"image_key" | "file_key", string>>;
|
|
110
|
-
};
|
|
111
|
-
if (wrappedResponse.code !== undefined && wrappedResponse.code !== 0) {
|
|
112
|
-
throw new Error(
|
|
113
|
-
`${params.errorPrefix}: ${wrappedResponse.msg || `code ${wrappedResponse.code}`}`,
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const key =
|
|
118
|
-
params.key === "image_key"
|
|
119
|
-
? (wrappedResponse.image_key ?? wrappedResponse.data?.image_key)
|
|
120
|
-
: (wrappedResponse.file_key ?? wrappedResponse.data?.file_key);
|
|
121
|
-
if (!key) {
|
|
122
|
-
throw new Error(`${params.errorPrefix}: no ${params.key} returned`);
|
|
123
|
-
}
|
|
124
|
-
return key;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function readHeaderValue(
|
|
128
|
-
headers: Record<string, unknown> | undefined,
|
|
129
|
-
name: string,
|
|
130
|
-
): string | undefined {
|
|
131
|
-
if (!headers) {
|
|
132
|
-
return undefined;
|
|
133
|
-
}
|
|
134
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
135
|
-
if (normalizeLowercaseStringOrEmpty(key) !== normalizeLowercaseStringOrEmpty(name)) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (typeof value === "string" && value.trim()) {
|
|
139
|
-
return value.trim();
|
|
140
|
-
}
|
|
141
|
-
if (Array.isArray(value)) {
|
|
142
|
-
const first = value.find((entry) => typeof entry === "string" && entry.trim());
|
|
143
|
-
if (typeof first === "string") {
|
|
144
|
-
return first.trim();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return undefined;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function readHttpStatusFromError(error: unknown): number | undefined {
|
|
152
|
-
if (!error || typeof error !== "object") {
|
|
153
|
-
return undefined;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const response = (error as { response?: unknown }).response;
|
|
157
|
-
if (response && typeof response === "object") {
|
|
158
|
-
const status = (response as { status?: unknown }).status;
|
|
159
|
-
if (typeof status === "number") {
|
|
160
|
-
return status;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const status = (error as { status?: unknown }).status;
|
|
165
|
-
return typeof status === "number" ? status : undefined;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function isHttpStatusError(error: unknown, status: number): boolean {
|
|
169
|
-
return readHttpStatusFromError(error) === status;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function containsEastAsianScript(value: string): boolean {
|
|
173
|
-
return /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u.test(value);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function recoverUtf8FileNameFromLatin1Header(value: string): string {
|
|
177
|
-
const recovered = Buffer.from(value, "latin1").toString("utf8");
|
|
178
|
-
if (recovered !== value && !recovered.includes("\uFFFD") && containsEastAsianScript(recovered)) {
|
|
179
|
-
return recovered;
|
|
180
|
-
}
|
|
181
|
-
return value;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function decodeDispositionFileName(value: string): string | undefined {
|
|
185
|
-
const utf8Match = value.match(/filename\*=UTF-8''([^;]+)/i);
|
|
186
|
-
if (utf8Match?.[1]) {
|
|
187
|
-
try {
|
|
188
|
-
return decodeURIComponent(utf8Match[1].trim().replace(/^"(.*)"$/, "$1"));
|
|
189
|
-
} catch {
|
|
190
|
-
return utf8Match[1].trim().replace(/^"(.*)"$/, "$1");
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const plainMatch = value.match(/filename="?([^";]+)"?/i);
|
|
195
|
-
const plainFileName = plainMatch?.[1]?.trim();
|
|
196
|
-
return plainFileName ? recoverUtf8FileNameFromLatin1Header(plainFileName) : undefined;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function extractFeishuDownloadMetadata(response: FeishuDownloadResponse): {
|
|
200
|
-
contentType?: string;
|
|
201
|
-
fileName?: string;
|
|
202
|
-
} {
|
|
203
|
-
const responseWithOptionalFields = response as FeishuDownloadResponse & {
|
|
204
|
-
header?: object;
|
|
205
|
-
contentType?: string;
|
|
206
|
-
mime_type?: string;
|
|
207
|
-
data?: {
|
|
208
|
-
contentType?: string;
|
|
209
|
-
mime_type?: string;
|
|
210
|
-
file_name?: string;
|
|
211
|
-
fileName?: string;
|
|
212
|
-
};
|
|
213
|
-
file_name?: string;
|
|
214
|
-
fileName?: string;
|
|
215
|
-
};
|
|
216
|
-
const headers =
|
|
217
|
-
asHeaderMap(responseWithOptionalFields.headers) ??
|
|
218
|
-
asHeaderMap(responseWithOptionalFields.header);
|
|
219
|
-
|
|
220
|
-
const contentType =
|
|
221
|
-
readHeaderValue(headers, "content-type") ??
|
|
222
|
-
responseWithOptionalFields.contentType ??
|
|
223
|
-
responseWithOptionalFields.mime_type ??
|
|
224
|
-
responseWithOptionalFields.data?.contentType ??
|
|
225
|
-
responseWithOptionalFields.data?.mime_type;
|
|
226
|
-
|
|
227
|
-
const disposition = readHeaderValue(headers, "content-disposition");
|
|
228
|
-
const fileName =
|
|
229
|
-
(disposition ? decodeDispositionFileName(disposition) : undefined) ??
|
|
230
|
-
responseWithOptionalFields.file_name ??
|
|
231
|
-
responseWithOptionalFields.fileName ??
|
|
232
|
-
responseWithOptionalFields.data?.file_name ??
|
|
233
|
-
responseWithOptionalFields.data?.fileName;
|
|
234
|
-
|
|
235
|
-
return { contentType, fileName };
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
async function readReadableBuffer(stream: Readable): Promise<Buffer> {
|
|
239
|
-
const chunks: Buffer[] = [];
|
|
240
|
-
for await (const chunk of stream) {
|
|
241
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
242
|
-
}
|
|
243
|
-
return Buffer.concat(chunks);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
async function readFeishuResponseBuffer(params: {
|
|
247
|
-
response: FeishuDownloadResponse;
|
|
248
|
-
tmpDirPrefix: string;
|
|
249
|
-
errorPrefix: string;
|
|
250
|
-
}): Promise<Buffer> {
|
|
251
|
-
const { response } = params;
|
|
252
|
-
if (Buffer.isBuffer(response)) {
|
|
253
|
-
return response;
|
|
254
|
-
}
|
|
255
|
-
if (response instanceof ArrayBuffer) {
|
|
256
|
-
return Buffer.from(response);
|
|
257
|
-
}
|
|
258
|
-
const responseWithOptionalFields = response as FeishuDownloadResponse & {
|
|
259
|
-
code?: number;
|
|
260
|
-
msg?: string;
|
|
261
|
-
data?: Buffer | ArrayBuffer;
|
|
262
|
-
[Symbol.asyncIterator]?: () => AsyncIterator<Buffer | Uint8Array | string>;
|
|
263
|
-
};
|
|
264
|
-
if (responseWithOptionalFields.code !== undefined && responseWithOptionalFields.code !== 0) {
|
|
265
|
-
throw new Error(
|
|
266
|
-
`${params.errorPrefix}: ${responseWithOptionalFields.msg || `code ${responseWithOptionalFields.code}`}`,
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
if (responseWithOptionalFields.data && Buffer.isBuffer(responseWithOptionalFields.data)) {
|
|
271
|
-
return responseWithOptionalFields.data;
|
|
272
|
-
}
|
|
273
|
-
if (responseWithOptionalFields.data instanceof ArrayBuffer) {
|
|
274
|
-
return Buffer.from(responseWithOptionalFields.data);
|
|
275
|
-
}
|
|
276
|
-
if (typeof response.getReadableStream === "function") {
|
|
277
|
-
return readReadableBuffer(response.getReadableStream());
|
|
278
|
-
}
|
|
279
|
-
if (typeof response.writeFile === "function") {
|
|
280
|
-
return await withTempDownloadPath({ prefix: params.tmpDirPrefix }, async (tmpPath) => {
|
|
281
|
-
await response.writeFile(tmpPath);
|
|
282
|
-
return await fs.promises.readFile(tmpPath);
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
if (responseWithOptionalFields[Symbol.asyncIterator]) {
|
|
286
|
-
const asyncIterable = responseWithOptionalFields as AsyncIterable<Buffer | Uint8Array | string>;
|
|
287
|
-
const chunks: Buffer[] = [];
|
|
288
|
-
for await (const chunk of asyncIterable) {
|
|
289
|
-
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
290
|
-
}
|
|
291
|
-
return Buffer.concat(chunks);
|
|
292
|
-
}
|
|
293
|
-
if (response instanceof Readable) {
|
|
294
|
-
return readReadableBuffer(response);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
const keys = Object.keys(response as object);
|
|
298
|
-
throw new Error(`${params.errorPrefix}: unexpected response format. Keys: [${keys.join(", ")}]`);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Download an image from Feishu using image_key.
|
|
303
|
-
* Used for downloading images sent in messages.
|
|
304
|
-
*/
|
|
305
|
-
export async function downloadImageFeishu(params: {
|
|
306
|
-
cfg: ClawdbotConfig;
|
|
307
|
-
imageKey: string;
|
|
308
|
-
accountId?: string;
|
|
309
|
-
}): Promise<DownloadImageResult> {
|
|
310
|
-
const { cfg, imageKey, accountId } = params;
|
|
311
|
-
const normalizedImageKey = normalizeFeishuExternalKey(imageKey);
|
|
312
|
-
if (!normalizedImageKey) {
|
|
313
|
-
throw new Error("Feishu image download failed: invalid image_key");
|
|
314
|
-
}
|
|
315
|
-
const { client } = createConfiguredFeishuMediaClient({ cfg, accountId });
|
|
316
|
-
|
|
317
|
-
const response = await client.im.image.get({
|
|
318
|
-
path: { image_key: normalizedImageKey },
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
const buffer = await readFeishuResponseBuffer({
|
|
322
|
-
response,
|
|
323
|
-
tmpDirPrefix: "openclaw-feishu-img-",
|
|
324
|
-
errorPrefix: "Feishu image download failed",
|
|
325
|
-
});
|
|
326
|
-
const meta = extractFeishuDownloadMetadata(response);
|
|
327
|
-
return { buffer, contentType: meta.contentType };
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
async function downloadMessageResourceWithType(params: {
|
|
331
|
-
client: ReturnType<typeof createFeishuClient>;
|
|
332
|
-
messageId: string;
|
|
333
|
-
fileKey: string;
|
|
334
|
-
type: FeishuMessageResourceDownloadType;
|
|
335
|
-
}): Promise<DownloadMessageResourceResult> {
|
|
336
|
-
const response = await params.client.im.messageResource.get({
|
|
337
|
-
path: { message_id: params.messageId, file_key: params.fileKey },
|
|
338
|
-
params: { type: params.type },
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
const buffer = await readFeishuResponseBuffer({
|
|
342
|
-
response,
|
|
343
|
-
tmpDirPrefix: "openclaw-feishu-resource-",
|
|
344
|
-
errorPrefix: "Feishu message resource download failed",
|
|
345
|
-
});
|
|
346
|
-
return { buffer, ...extractFeishuDownloadMetadata(response) };
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Download a message resource (file/image/audio/video) from Feishu.
|
|
351
|
-
* Used for downloading files, audio, and video from messages.
|
|
352
|
-
*/
|
|
353
|
-
export async function downloadMessageResourceFeishu(params: {
|
|
354
|
-
cfg: ClawdbotConfig;
|
|
355
|
-
messageId: string;
|
|
356
|
-
fileKey: string;
|
|
357
|
-
type: "image" | "file";
|
|
358
|
-
accountId?: string;
|
|
359
|
-
}): Promise<DownloadMessageResourceResult> {
|
|
360
|
-
const { cfg, messageId, fileKey, type, accountId } = params;
|
|
361
|
-
const normalizedFileKey = normalizeFeishuExternalKey(fileKey);
|
|
362
|
-
if (!normalizedFileKey) {
|
|
363
|
-
throw new Error("Feishu message resource download failed: invalid file_key");
|
|
364
|
-
}
|
|
365
|
-
const { client } = createConfiguredFeishuMediaClient({ cfg, accountId });
|
|
366
|
-
|
|
367
|
-
try {
|
|
368
|
-
return await downloadMessageResourceWithType({
|
|
369
|
-
client,
|
|
370
|
-
messageId,
|
|
371
|
-
fileKey: normalizedFileKey,
|
|
372
|
-
type,
|
|
373
|
-
});
|
|
374
|
-
} catch (err) {
|
|
375
|
-
if (type !== "file" || !isHttpStatusError(err, 502)) {
|
|
376
|
-
throw err;
|
|
377
|
-
}
|
|
378
|
-
try {
|
|
379
|
-
return await downloadMessageResourceWithType({
|
|
380
|
-
client,
|
|
381
|
-
messageId,
|
|
382
|
-
fileKey: normalizedFileKey,
|
|
383
|
-
type: "media",
|
|
384
|
-
});
|
|
385
|
-
} catch {
|
|
386
|
-
throw err;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
export type UploadImageResult = {
|
|
392
|
-
imageKey: string;
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
export type UploadFileResult = {
|
|
396
|
-
fileKey: string;
|
|
397
|
-
};
|
|
398
|
-
|
|
399
|
-
export type SendMediaResult = {
|
|
400
|
-
messageId: string;
|
|
401
|
-
chatId: string;
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
/**
|
|
405
|
-
* Upload an image to Feishu and get an image_key for sending.
|
|
406
|
-
* Supports: JPEG, PNG, WEBP, GIF, TIFF, BMP, ICO
|
|
407
|
-
*/
|
|
408
|
-
export async function uploadImageFeishu(params: {
|
|
409
|
-
cfg: ClawdbotConfig;
|
|
410
|
-
image: Buffer | string; // Buffer or file path
|
|
411
|
-
imageType?: "message" | "avatar";
|
|
412
|
-
accountId?: string;
|
|
413
|
-
}): Promise<UploadImageResult> {
|
|
414
|
-
const { cfg, image, imageType = "message", accountId } = params;
|
|
415
|
-
const { client } = createConfiguredFeishuMediaClient({ cfg, accountId });
|
|
416
|
-
|
|
417
|
-
// SDK accepts Buffer directly or fs.ReadStream for file paths
|
|
418
|
-
// Using Readable.from(buffer) causes issues with form-data library
|
|
419
|
-
// See: https://github.com/larksuite/node-sdk/issues/121
|
|
420
|
-
const imageData = typeof image === "string" ? fs.createReadStream(image) : image;
|
|
421
|
-
|
|
422
|
-
const response = await requestFeishuApi(
|
|
423
|
-
() =>
|
|
424
|
-
client.im.image.create({
|
|
425
|
-
data: {
|
|
426
|
-
image_type: imageType,
|
|
427
|
-
image: imageData,
|
|
428
|
-
},
|
|
429
|
-
}),
|
|
430
|
-
"Feishu image upload failed",
|
|
431
|
-
{ includeNestedErrorLogId: true },
|
|
432
|
-
);
|
|
433
|
-
|
|
434
|
-
return {
|
|
435
|
-
imageKey: extractFeishuUploadKey(response, {
|
|
436
|
-
key: "image_key",
|
|
437
|
-
errorPrefix: "Feishu image upload failed",
|
|
438
|
-
}),
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Sanitize a filename for safe use in Feishu multipart/form-data uploads.
|
|
444
|
-
* Strips control characters and multipart-injection vectors (CWE-93) while
|
|
445
|
-
* preserving the original UTF-8 display name (Chinese, emoji, etc.).
|
|
446
|
-
*
|
|
447
|
-
* Previous versions percent-encoded non-ASCII characters, but the Feishu
|
|
448
|
-
* `im.file.create` API uses `file_name` as a literal display name — it does
|
|
449
|
-
* NOT decode percent-encoding — so encoded filenames appeared as garbled text
|
|
450
|
-
* in chat (regression in v2026.3.2).
|
|
451
|
-
*/
|
|
452
|
-
export function sanitizeFileNameForUpload(fileName: string): string {
|
|
453
|
-
return fileName.replace(/[\p{Cc}"\\]/gu, "_");
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* Upload a file to Feishu and get a file_key for sending.
|
|
458
|
-
* Max file size: 30MB
|
|
459
|
-
*/
|
|
460
|
-
export async function uploadFileFeishu(params: {
|
|
461
|
-
cfg: ClawdbotConfig;
|
|
462
|
-
file: Buffer | string; // Buffer or file path
|
|
463
|
-
fileName: string;
|
|
464
|
-
fileType: "opus" | "mp4" | "pdf" | "doc" | "xls" | "ppt" | "stream";
|
|
465
|
-
duration?: number; // Required for audio/video files, in milliseconds
|
|
466
|
-
accountId?: string;
|
|
467
|
-
}): Promise<UploadFileResult> {
|
|
468
|
-
const { cfg, file, fileName, fileType, duration, accountId } = params;
|
|
469
|
-
const { client } = createConfiguredFeishuMediaClient({ cfg, accountId });
|
|
470
|
-
|
|
471
|
-
// SDK accepts Buffer directly or fs.ReadStream for file paths
|
|
472
|
-
// Using Readable.from(buffer) causes issues with form-data library
|
|
473
|
-
// See: https://github.com/larksuite/node-sdk/issues/121
|
|
474
|
-
const fileData = typeof file === "string" ? fs.createReadStream(file) : file;
|
|
475
|
-
|
|
476
|
-
const safeFileName = sanitizeFileNameForUpload(fileName);
|
|
477
|
-
|
|
478
|
-
const response = await requestFeishuApi(
|
|
479
|
-
() =>
|
|
480
|
-
client.im.file.create({
|
|
481
|
-
data: {
|
|
482
|
-
file_type: fileType,
|
|
483
|
-
file_name: safeFileName,
|
|
484
|
-
file: fileData,
|
|
485
|
-
...(duration !== undefined && { duration }),
|
|
486
|
-
},
|
|
487
|
-
}),
|
|
488
|
-
"Feishu file upload failed",
|
|
489
|
-
{ includeNestedErrorLogId: true },
|
|
490
|
-
);
|
|
491
|
-
|
|
492
|
-
return {
|
|
493
|
-
fileKey: extractFeishuUploadKey(response, {
|
|
494
|
-
key: "file_key",
|
|
495
|
-
errorPrefix: "Feishu file upload failed",
|
|
496
|
-
}),
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Send an image message using an image_key
|
|
502
|
-
*/
|
|
503
|
-
export async function sendImageFeishu(params: {
|
|
504
|
-
cfg: ClawdbotConfig;
|
|
505
|
-
to: string;
|
|
506
|
-
imageKey: string;
|
|
507
|
-
replyToMessageId?: string;
|
|
508
|
-
replyInThread?: boolean;
|
|
509
|
-
accountId?: string;
|
|
510
|
-
}): Promise<SendMediaResult> {
|
|
511
|
-
const { cfg, to, imageKey, replyToMessageId, replyInThread, accountId } = params;
|
|
512
|
-
const { client, receiveId, receiveIdType } = resolveFeishuSendTarget({
|
|
513
|
-
cfg,
|
|
514
|
-
to,
|
|
515
|
-
accountId,
|
|
516
|
-
});
|
|
517
|
-
const content = JSON.stringify({ image_key: imageKey });
|
|
518
|
-
|
|
519
|
-
if (replyToMessageId) {
|
|
520
|
-
const response = await requestFeishuApi(
|
|
521
|
-
() =>
|
|
522
|
-
client.im.message.reply({
|
|
523
|
-
path: { message_id: replyToMessageId },
|
|
524
|
-
data: {
|
|
525
|
-
content,
|
|
526
|
-
msg_type: "image",
|
|
527
|
-
...(replyInThread ? { reply_in_thread: true } : {}),
|
|
528
|
-
},
|
|
529
|
-
}),
|
|
530
|
-
"Feishu image reply failed",
|
|
531
|
-
{ includeNestedErrorLogId: true },
|
|
532
|
-
);
|
|
533
|
-
assertFeishuMessageApiSuccess(response, "Feishu image reply failed");
|
|
534
|
-
return toFeishuSendResult(response, receiveId);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
const response = await requestFeishuApi(
|
|
538
|
-
() =>
|
|
539
|
-
client.im.message.create({
|
|
540
|
-
params: { receive_id_type: receiveIdType },
|
|
541
|
-
data: {
|
|
542
|
-
receive_id: receiveId,
|
|
543
|
-
content,
|
|
544
|
-
msg_type: "image",
|
|
545
|
-
},
|
|
546
|
-
}),
|
|
547
|
-
"Feishu image send failed",
|
|
548
|
-
{ includeNestedErrorLogId: true },
|
|
549
|
-
);
|
|
550
|
-
assertFeishuMessageApiSuccess(response, "Feishu image send failed");
|
|
551
|
-
return toFeishuSendResult(response, receiveId);
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
/**
|
|
555
|
-
* Send a file message using a file_key
|
|
556
|
-
*/
|
|
557
|
-
export async function sendFileFeishu(params: {
|
|
558
|
-
cfg: ClawdbotConfig;
|
|
559
|
-
to: string;
|
|
560
|
-
fileKey: string;
|
|
561
|
-
/** Use "audio" for audio, "media" for video (mp4), "file" for documents */
|
|
562
|
-
msgType?: "file" | "audio" | "media";
|
|
563
|
-
replyToMessageId?: string;
|
|
564
|
-
replyInThread?: boolean;
|
|
565
|
-
accountId?: string;
|
|
566
|
-
}): Promise<SendMediaResult> {
|
|
567
|
-
const { cfg, to, fileKey, replyToMessageId, replyInThread, accountId } = params;
|
|
568
|
-
const msgType = params.msgType ?? "file";
|
|
569
|
-
const { client, receiveId, receiveIdType } = resolveFeishuSendTarget({
|
|
570
|
-
cfg,
|
|
571
|
-
to,
|
|
572
|
-
accountId,
|
|
573
|
-
});
|
|
574
|
-
const content = JSON.stringify({ file_key: fileKey });
|
|
575
|
-
|
|
576
|
-
if (replyToMessageId) {
|
|
577
|
-
const response = await requestFeishuApi(
|
|
578
|
-
() =>
|
|
579
|
-
client.im.message.reply({
|
|
580
|
-
path: { message_id: replyToMessageId },
|
|
581
|
-
data: {
|
|
582
|
-
content,
|
|
583
|
-
msg_type: msgType,
|
|
584
|
-
...(replyInThread ? { reply_in_thread: true } : {}),
|
|
585
|
-
},
|
|
586
|
-
}),
|
|
587
|
-
"Feishu file reply failed",
|
|
588
|
-
{ includeNestedErrorLogId: true },
|
|
589
|
-
);
|
|
590
|
-
assertFeishuMessageApiSuccess(response, "Feishu file reply failed");
|
|
591
|
-
return toFeishuSendResult(response, receiveId);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
const response = await requestFeishuApi(
|
|
595
|
-
() =>
|
|
596
|
-
client.im.message.create({
|
|
597
|
-
params: { receive_id_type: receiveIdType },
|
|
598
|
-
data: {
|
|
599
|
-
receive_id: receiveId,
|
|
600
|
-
content,
|
|
601
|
-
msg_type: msgType,
|
|
602
|
-
},
|
|
603
|
-
}),
|
|
604
|
-
"Feishu file send failed",
|
|
605
|
-
{ includeNestedErrorLogId: true },
|
|
606
|
-
);
|
|
607
|
-
assertFeishuMessageApiSuccess(response, "Feishu file send failed");
|
|
608
|
-
return toFeishuSendResult(response, receiveId);
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
/**
|
|
612
|
-
* Helper to detect file type from extension
|
|
613
|
-
*/
|
|
614
|
-
export function detectFileType(
|
|
615
|
-
fileName: string,
|
|
616
|
-
): "opus" | "mp4" | "pdf" | "doc" | "xls" | "ppt" | "stream" {
|
|
617
|
-
const ext = normalizeLowercaseStringOrEmpty(path.extname(fileName));
|
|
618
|
-
switch (ext) {
|
|
619
|
-
case ".opus":
|
|
620
|
-
case ".ogg":
|
|
621
|
-
return "opus";
|
|
622
|
-
case ".mp4":
|
|
623
|
-
case ".mov":
|
|
624
|
-
case ".avi":
|
|
625
|
-
return "mp4";
|
|
626
|
-
case ".pdf":
|
|
627
|
-
return "pdf";
|
|
628
|
-
case ".doc":
|
|
629
|
-
case ".docx":
|
|
630
|
-
return "doc";
|
|
631
|
-
case ".xls":
|
|
632
|
-
case ".xlsx":
|
|
633
|
-
return "xls";
|
|
634
|
-
case ".ppt":
|
|
635
|
-
case ".pptx":
|
|
636
|
-
return "ppt";
|
|
637
|
-
default:
|
|
638
|
-
return "stream";
|
|
639
|
-
}
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
function resolveFeishuOutboundMediaKind(params: { fileName: string; contentType?: string }): {
|
|
643
|
-
fileType?: "opus" | "mp4" | "pdf" | "doc" | "xls" | "ppt" | "stream";
|
|
644
|
-
msgType: "image" | "file" | "audio" | "media";
|
|
645
|
-
} {
|
|
646
|
-
const { fileName, contentType } = params;
|
|
647
|
-
const ext = normalizeLowercaseStringOrEmpty(path.extname(fileName));
|
|
648
|
-
const mimeKind = mediaKindFromMime(contentType);
|
|
649
|
-
|
|
650
|
-
const isImageExt = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".ico", ".tiff"].includes(
|
|
651
|
-
ext,
|
|
652
|
-
);
|
|
653
|
-
if (isImageExt || mimeKind === "image") {
|
|
654
|
-
return { msgType: "image" };
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
if (
|
|
658
|
-
ext === ".opus" ||
|
|
659
|
-
ext === ".ogg" ||
|
|
660
|
-
contentType === "audio/ogg" ||
|
|
661
|
-
contentType === "audio/opus"
|
|
662
|
-
) {
|
|
663
|
-
return { fileType: "opus", msgType: "audio" };
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
if (
|
|
667
|
-
[".mp4", ".mov", ".avi"].includes(ext) ||
|
|
668
|
-
contentType === "video/mp4" ||
|
|
669
|
-
contentType === "video/quicktime" ||
|
|
670
|
-
contentType === "video/x-msvideo"
|
|
671
|
-
) {
|
|
672
|
-
return { fileType: "mp4", msgType: "media" };
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
const fileType = detectFileType(fileName);
|
|
676
|
-
return {
|
|
677
|
-
fileType,
|
|
678
|
-
msgType:
|
|
679
|
-
fileType === "stream"
|
|
680
|
-
? "file"
|
|
681
|
-
: fileType === "opus"
|
|
682
|
-
? "audio"
|
|
683
|
-
: fileType === "mp4"
|
|
684
|
-
? "media"
|
|
685
|
-
: "file",
|
|
686
|
-
};
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
function isFeishuNativeVoiceAudio(params: { fileName: string; contentType?: string }): boolean {
|
|
690
|
-
const ext = normalizeLowercaseStringOrEmpty(path.extname(params.fileName));
|
|
691
|
-
const contentType = normalizeLowercaseStringOrEmpty(params.contentType);
|
|
692
|
-
return (
|
|
693
|
-
ext === ".opus" || ext === ".ogg" || contentType === "audio/ogg" || contentType === "audio/opus"
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
function isLikelyTranscodableAudio(params: { fileName: string; contentType?: string }): boolean {
|
|
698
|
-
const ext = normalizeLowercaseStringOrEmpty(path.extname(params.fileName));
|
|
699
|
-
const contentType = normalizeLowercaseStringOrEmpty(params.contentType);
|
|
700
|
-
return FEISHU_TRANSCODABLE_AUDIO_EXTS.has(ext) || mediaKindFromMime(contentType) === "audio";
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
async function transcodeToFeishuVoiceOpus(params: {
|
|
704
|
-
buffer: Buffer;
|
|
705
|
-
fileName: string;
|
|
706
|
-
contentType?: string;
|
|
707
|
-
}): Promise<{ buffer: Buffer; fileName: string; contentType: string }> {
|
|
708
|
-
const tempRoot = resolvePreferredOpenClawTmpDir();
|
|
709
|
-
await fs.promises.mkdir(tempRoot, { recursive: true, mode: 0o700 });
|
|
710
|
-
const tempDir = await fs.promises.mkdtemp(path.join(tempRoot, "feishu-voice-"));
|
|
711
|
-
try {
|
|
712
|
-
const ext = normalizeLowercaseStringOrEmpty(path.extname(params.fileName));
|
|
713
|
-
const inputExt = ext && ext.length <= 12 ? ext : ".audio";
|
|
714
|
-
const inputPath = path.join(tempDir, `input${inputExt}`);
|
|
715
|
-
const outputPath = path.join(tempDir, FEISHU_VOICE_FILE_NAME);
|
|
716
|
-
await fs.promises.writeFile(inputPath, params.buffer, { mode: 0o600 });
|
|
717
|
-
await runFfmpeg([
|
|
718
|
-
"-hide_banner",
|
|
719
|
-
"-loglevel",
|
|
720
|
-
"error",
|
|
721
|
-
"-y",
|
|
722
|
-
"-i",
|
|
723
|
-
inputPath,
|
|
724
|
-
"-vn",
|
|
725
|
-
"-sn",
|
|
726
|
-
"-dn",
|
|
727
|
-
"-t",
|
|
728
|
-
String(MEDIA_FFMPEG_MAX_AUDIO_DURATION_SECS),
|
|
729
|
-
"-ar",
|
|
730
|
-
String(FEISHU_VOICE_SAMPLE_RATE_HZ),
|
|
731
|
-
"-ac",
|
|
732
|
-
"1",
|
|
733
|
-
"-c:a",
|
|
734
|
-
"libopus",
|
|
735
|
-
"-b:a",
|
|
736
|
-
FEISHU_VOICE_BITRATE,
|
|
737
|
-
outputPath,
|
|
738
|
-
]);
|
|
739
|
-
return {
|
|
740
|
-
buffer: await fs.promises.readFile(outputPath),
|
|
741
|
-
fileName: FEISHU_VOICE_FILE_NAME,
|
|
742
|
-
contentType: "audio/ogg",
|
|
743
|
-
};
|
|
744
|
-
} finally {
|
|
745
|
-
await fs.promises.rm(tempDir, { recursive: true, force: true });
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
async function prepareFeishuVoiceMedia(params: {
|
|
750
|
-
buffer: Buffer;
|
|
751
|
-
fileName: string;
|
|
752
|
-
contentType?: string;
|
|
753
|
-
audioAsVoice?: boolean;
|
|
754
|
-
}): Promise<{ buffer: Buffer; fileName: string; contentType?: string }> {
|
|
755
|
-
if (isFeishuNativeVoiceAudio(params)) {
|
|
756
|
-
return params;
|
|
757
|
-
}
|
|
758
|
-
if (params.audioAsVoice !== true || !isLikelyTranscodableAudio(params)) {
|
|
759
|
-
return params;
|
|
760
|
-
}
|
|
761
|
-
try {
|
|
762
|
-
return await transcodeToFeishuVoiceOpus(params);
|
|
763
|
-
} catch (err) {
|
|
764
|
-
console.warn(
|
|
765
|
-
`[feishu] audioAsVoice transcode failed; sending ${params.fileName} as a file attachment:`,
|
|
766
|
-
err,
|
|
767
|
-
);
|
|
768
|
-
return params;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
/**
|
|
773
|
-
* Upload and send media (image or file) from URL, local path, or buffer.
|
|
774
|
-
* When mediaUrl is a local path, mediaLocalRoots (from core outbound context)
|
|
775
|
-
* must be passed so loadWebMedia allows the path (post CVE-2026-26321).
|
|
776
|
-
*/
|
|
777
|
-
export async function sendMediaFeishu(params: {
|
|
778
|
-
cfg: ClawdbotConfig;
|
|
779
|
-
to: string;
|
|
780
|
-
mediaUrl?: string;
|
|
781
|
-
mediaBuffer?: Buffer;
|
|
782
|
-
fileName?: string;
|
|
783
|
-
replyToMessageId?: string;
|
|
784
|
-
replyInThread?: boolean;
|
|
785
|
-
accountId?: string;
|
|
786
|
-
/** Allowed roots for local path reads; required for local filePath to work. */
|
|
787
|
-
mediaLocalRoots?: readonly string[];
|
|
788
|
-
/** When true, transcode compatible audio to Feishu native Ogg/Opus voice bubbles. */
|
|
789
|
-
audioAsVoice?: boolean;
|
|
790
|
-
}): Promise<SendMediaResult> {
|
|
791
|
-
const {
|
|
792
|
-
cfg,
|
|
793
|
-
to,
|
|
794
|
-
mediaUrl,
|
|
795
|
-
mediaBuffer,
|
|
796
|
-
fileName,
|
|
797
|
-
replyToMessageId,
|
|
798
|
-
replyInThread,
|
|
799
|
-
accountId,
|
|
800
|
-
mediaLocalRoots,
|
|
801
|
-
audioAsVoice,
|
|
802
|
-
} = params;
|
|
803
|
-
const account = resolveFeishuRuntimeAccount({ cfg, accountId });
|
|
804
|
-
if (!account.configured) {
|
|
805
|
-
throw new Error(`Feishu account "${account.accountId}" not configured`);
|
|
806
|
-
}
|
|
807
|
-
const mediaMaxBytes = (account.config?.mediaMaxMb ?? 30) * 1024 * 1024;
|
|
808
|
-
|
|
809
|
-
let buffer: Buffer;
|
|
810
|
-
let name: string;
|
|
811
|
-
let contentType: string | undefined;
|
|
812
|
-
|
|
813
|
-
if (mediaBuffer) {
|
|
814
|
-
buffer = mediaBuffer;
|
|
815
|
-
name = fileName ?? "file";
|
|
816
|
-
} else if (mediaUrl) {
|
|
817
|
-
const loaded = await getFeishuRuntime().media.loadWebMedia(mediaUrl, {
|
|
818
|
-
maxBytes: mediaMaxBytes,
|
|
819
|
-
optimizeImages: false,
|
|
820
|
-
localRoots: mediaLocalRoots?.length ? mediaLocalRoots : undefined,
|
|
821
|
-
});
|
|
822
|
-
buffer = loaded.buffer;
|
|
823
|
-
name = fileName ?? loaded.fileName ?? "file";
|
|
824
|
-
contentType = loaded.contentType;
|
|
825
|
-
} else {
|
|
826
|
-
throw new Error("Either mediaUrl or mediaBuffer must be provided");
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
const prepared = await prepareFeishuVoiceMedia({
|
|
830
|
-
buffer,
|
|
831
|
-
fileName: name,
|
|
832
|
-
contentType,
|
|
833
|
-
audioAsVoice,
|
|
834
|
-
});
|
|
835
|
-
buffer = prepared.buffer;
|
|
836
|
-
name = prepared.fileName;
|
|
837
|
-
contentType = prepared.contentType;
|
|
838
|
-
|
|
839
|
-
const routing = resolveFeishuOutboundMediaKind({ fileName: name, contentType });
|
|
840
|
-
|
|
841
|
-
if (routing.msgType === "image") {
|
|
842
|
-
const { imageKey } = await uploadImageFeishu({ cfg, image: buffer, accountId });
|
|
843
|
-
return sendImageFeishu({ cfg, to, imageKey, replyToMessageId, replyInThread, accountId });
|
|
844
|
-
}
|
|
845
|
-
const { fileKey } = await uploadFileFeishu({
|
|
846
|
-
cfg,
|
|
847
|
-
file: buffer,
|
|
848
|
-
fileName: name,
|
|
849
|
-
fileType: routing.fileType ?? "stream",
|
|
850
|
-
accountId,
|
|
851
|
-
});
|
|
852
|
-
return sendFileFeishu({
|
|
853
|
-
cfg,
|
|
854
|
-
to,
|
|
855
|
-
fileKey,
|
|
856
|
-
msgType: routing.msgType,
|
|
857
|
-
replyToMessageId,
|
|
858
|
-
replyInThread,
|
|
859
|
-
accountId,
|
|
860
|
-
});
|
|
861
|
-
}
|