@jixing475/magpie-marvis 0.0.0-stage → 0.3.0

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.
Files changed (79) hide show
  1. package/AGENTS.md +255 -0
  2. package/CHANGELOG.md +726 -0
  3. package/CHECKLIST.md +161 -0
  4. package/CONTRIBUTING.md +83 -0
  5. package/GUIDE.md +275 -0
  6. package/HOOKS.md +1333 -0
  7. package/LICENSE +21 -0
  8. package/MULTI-PROVIDER.md +414 -0
  9. package/NOTICE.md +46 -0
  10. package/OFFICIAL-SOURCES.md +142 -0
  11. package/README-TEMPLATE.md +271 -0
  12. package/README.md +300 -3
  13. package/SCORING.md +151 -0
  14. package/UPDATING.md +660 -0
  15. package/examples/split/README.md +66 -0
  16. package/examples/split/index.mjs +75 -0
  17. package/examples/split/src/hooks/config.mjs +18 -0
  18. package/examples/split/src/hooks/models.mjs +49 -0
  19. package/examples/split/src/hooks/usage.mjs +55 -0
  20. package/extract-contract.py +56 -0
  21. package/index.mjs +48 -0
  22. package/package.json +95 -4
  23. package/panel/first-line.css +112 -0
  24. package/panel/first-line.html +37 -0
  25. package/panel/first-line.js +137 -0
  26. package/registry-entry.json +11 -0
  27. package/scripts/check-magpie-stable.mjs +109 -0
  28. package/scripts/check-maintenance.mjs +146 -0
  29. package/scripts/check.mjs +521 -0
  30. package/scripts/cli.mjs +54 -0
  31. package/scripts/contract-diff.mjs +112 -0
  32. package/scripts/create.mjs +270 -0
  33. package/scripts/npm-name.mjs +19 -0
  34. package/scripts/probe-served-model.mjs +172 -0
  35. package/scripts/probe-vision.mjs +173 -0
  36. package/scripts/publish-npm.mjs +44 -0
  37. package/scripts/release.mjs +442 -0
  38. package/scripts/score.mjs +280 -0
  39. package/src/account-name.mjs +145 -0
  40. package/src/auth.mjs +27 -0
  41. package/src/context.mjs +369 -0
  42. package/src/hooks/config.mjs +236 -0
  43. package/src/hooks/loader.mjs +593 -0
  44. package/src/hooks/methods.mjs +398 -0
  45. package/src/hooks/models.mjs +25 -0
  46. package/src/hooks/refresh.mjs +149 -0
  47. package/src/hooks/usage.mjs +117 -0
  48. package/src/marvis/beacon-mac.mjs +213 -0
  49. package/src/marvis/beacon-shim.mjs +143 -0
  50. package/src/marvis/browser.mjs +598 -0
  51. package/src/marvis/desktop-mac.mjs +232 -0
  52. package/src/marvis/desktop.mjs +273 -0
  53. package/src/marvis/device.mjs +85 -0
  54. package/src/marvis/expiry.mjs +60 -0
  55. package/src/marvis/fingerprint.mjs +168 -0
  56. package/src/marvis/images.mjs +230 -0
  57. package/src/marvis/login.mjs +512 -0
  58. package/src/marvis/profile.mjs +113 -0
  59. package/src/marvis/protocol.mjs +160 -0
  60. package/src/marvis/tool-schema.mjs +97 -0
  61. package/src/quota.mjs +212 -0
  62. package/src/refresh-lock.mjs +115 -0
  63. package/tests/device-registry.test.mjs +48 -0
  64. package/tests/e2e-live.test.mjs +120 -0
  65. package/tests/image-input.test.mjs +314 -0
  66. package/tests/legacy-model-config.test.mjs +349 -0
  67. package/tests/legacy-model-loader.test.mjs +306 -0
  68. package/tests/loader-model-support.test.mjs +823 -0
  69. package/tests/manual-paste-login.test.mjs +228 -0
  70. package/tests/model-alias.test.mjs +59 -0
  71. package/tests/model-catalog-support.test.mjs +241 -0
  72. package/tests/mount.test.mjs +259 -0
  73. package/tests/oauth-exchange.test.mjs +362 -0
  74. package/tests/panel-manifest.test.mjs +73 -0
  75. package/tests/plugin-options.test.mjs +100 -0
  76. package/tests/profile.test.mjs +96 -0
  77. package/tests/refresh-contract.test.mjs +318 -0
  78. package/tests/request-identity.test.mjs +18 -0
  79. package/tests/tool-schema.test.mjs +66 -0
@@ -0,0 +1,168 @@
1
+ // 设备指纹 —— uskey / qimei36 的来源
2
+ //
3
+ // 这两个值都是 Marvis 客户端在本地生成的设备标识,随每个请求放进
4
+ // `Ual-Access-MarvisExt` 头里。本模块负责给出符合上游格式的值。
5
+ //
6
+ // 2026-11-17 之后,`/v1/chat/completions` 会校验 uskey 是否按**本次请求的 bus_info**
7
+ // 由 Beacon SDK 现签。随机串第一次就会 4100404。
8
+ //
9
+ // 出票顺序:
10
+ // 1. macOS:复用客户端自带的 beacon_napi.node(见 beacon-mac.mjs,无需 C 桥)
11
+ // 2. native/beacon_shim.dll + beacon_wrapper.dll(官方 Windows 客户端只有 DLL,没有 napi)
12
+ // 3. native/beacon_napi.node(旧的 Node 插件形态,可选)
13
+ // 4. 随机串(没有 SDK 时的兜底;线上会被 4100404)
14
+ //
15
+ // DLL 不随仓库分发。桥的源码在 native/marvis_beacon_shim.c。
16
+ //
17
+ // ⚠ `qimei36` 取不到真值时返回空串,由调用方整个不发该字段 ——
18
+ // 这个字段上游要求与设备一致,不适合用占位值冒充。
19
+
20
+ import { randomBytes } from "node:crypto"
21
+ import { createRequire } from "node:module"
22
+ import { existsSync } from "node:fs"
23
+ import { dirname, join } from "node:path"
24
+ import { fileURLToPath } from "node:url"
25
+ import { shimAvailable, shimInit, shimQimei, shimUskey } from "./beacon-shim.mjs"
26
+ import {
27
+ macAvailable,
28
+ macErrorText,
29
+ macInit,
30
+ macNativeDir,
31
+ macQimei,
32
+ macUskey,
33
+ } from "./beacon-mac.mjs"
34
+
35
+ /** 与 beacon_shim 初始化用的 appKey / 版本。可用环境变量覆盖。 */
36
+ const SHIM_APP_KEY = process.env.MARVIS_QIMEI_APPKEY || "0WIN06Y06DKV1IUQ"
37
+ const SHIM_APP_VERSION = process.env.MARVIS_APP_VERSION || "1.60.3400.35"
38
+
39
+ const require = createRequire(import.meta.url)
40
+
41
+ const HERE = dirname(fileURLToPath(import.meta.url))
42
+ /** 插件根目录下的 native/ —— 可选 */
43
+ export const NATIVE_DIR = join(HERE, "..", "..", "native")
44
+
45
+ /** 四个文件一起才工作(beacon_napi.node 靠同目录兄弟 DLL 动态加载) */
46
+ const REQUIRED = ["beacon_napi.node", "qimei.dll", "beacon_sdk.dll", "beacon_wrapper.dll"]
47
+
48
+ /** 上游 appKey —— 来自 Marvis 的 resources/.env 的 MARVIS_QIMEI_APPKEY */
49
+ const APP_KEY = "0WIN06Y06DKV1IUQ"
50
+ const APP_VERSION = "1.60.3300.408"
51
+
52
+ /** uskey 的字节长度 */
53
+ export const USKEY_BYTES = 376
54
+
55
+ /** @type {{ getUskey: Function, getQimei: Function } | null} */
56
+ let beacon = null
57
+ let beaconTried = false
58
+
59
+ /**
60
+ * 尝试加载**可选**的 Beacon SDK。失败不抛、不阻塞 —— 返回 null 即走本地生成。
61
+ * @returns {{ getUskey: Function, getQimei: Function } | null}
62
+ */
63
+ function tryBeacon() {
64
+ if (beaconTried) return beacon
65
+ beaconTried = true
66
+ try {
67
+ if (!REQUIRED.every((f) => existsSync(join(NATIVE_DIR, f)))) return null
68
+ const mod = require(join(NATIVE_DIR, "beacon_napi.node"))
69
+ if (mod.init({ appKey: APP_KEY, appVersion: APP_VERSION }) !== 0) return null
70
+ beacon = mod
71
+ return mod
72
+ } catch {
73
+ return null
74
+ }
75
+ }
76
+
77
+ /**
78
+ * 按本次请求上下文出一张 uskey。
79
+ *
80
+ * @param {{ guid?: string, openid?: string } | string} [acct]
81
+ * 旧调用 `uskeyOf("marvis_client")` 仍然接受,但没有 busInfo 时签不出上下文票。
82
+ * @param {{ busInfo?: string }} [identity]
83
+ * @returns {Promise<string>}
84
+ */
85
+ export async function uskeyOf(acct = {}, identity = {}) {
86
+ const busInfo = typeof acct === "string" ? "" : String(identity?.busInfo || "")
87
+ const businessId = typeof acct === "string" ? acct : "marvis"
88
+
89
+ // macOS:客户端自带 N-API 插件,不需要 C 桥,也不需要环境变量
90
+ if (busInfo && process.platform === "darwin" && macAvailable()) {
91
+ if (await macInit(SHIM_APP_KEY, SHIM_APP_VERSION)) {
92
+ const ticket = macUskey(busInfo, businessId)
93
+ if (ticket) return ticket
94
+ }
95
+ }
96
+
97
+ if (busInfo && (await shimInit(SHIM_APP_KEY, SHIM_APP_VERSION))) {
98
+ const ticket = shimUskey(busInfo)
99
+ if (ticket) return ticket
100
+ }
101
+
102
+ const b = tryBeacon()
103
+ if (b) {
104
+ try {
105
+ const real = String(b.getUskey(businessId, busInfo || { reqScene: 1, taskType: "user" }) ?? "")
106
+ if (real && real !== "null") return real
107
+ } catch {
108
+ /* 拿不到就落到本地生成,不阻塞请求 */
109
+ }
110
+ }
111
+ return localUskey()
112
+ }
113
+
114
+ /**
115
+ * 本地生成 uskey:与上游格式一致的 base64 串。
116
+ * @returns {string}
117
+ */
118
+ export function localUskey() {
119
+ return randomBytes(USKEY_BYTES).toString("base64")
120
+ }
121
+
122
+ /**
123
+ * `localUskey()` 的别名,保留旧名字方便迁移。
124
+ * @returns {string}
125
+ */
126
+ export function syntheticUskey() {
127
+ return localUskey()
128
+ }
129
+
130
+ /**
131
+ * 设备 qimei36 —— **取不到真值就返回空串**。
132
+ * 调用方据此决定要不要把该字段放进请求。
133
+ * @returns {string} 真值或空串
134
+ */
135
+ export function qimei36() {
136
+ if (process.platform === "darwin" && macAvailable()) {
137
+ const fromMac = macQimei()
138
+ if (fromMac) return fromMac
139
+ }
140
+ const fromShim = shimQimei()
141
+ if (fromShim) return fromShim
142
+ const b = tryBeacon()
143
+ if (!b) return ""
144
+ try {
145
+ return String(b.getQimei() ?? "")
146
+ } catch {
147
+ return ""
148
+ }
149
+ }
150
+
151
+ /** 当前是否在用 SDK 签指纹(诊断用) */
152
+ export function usingNativeFingerprint() {
153
+ if (process.platform === "darwin" && macAvailable() && macQimei()) return true
154
+ return shimAvailable() || tryBeacon() !== null
155
+ }
156
+
157
+ /** 给用户看的说明 */
158
+ export function fingerprintHint() {
159
+ if (usingNativeFingerprint()) {
160
+ return process.platform === "darwin"
161
+ ? `使用 Marvis 客户端自带的 Beacon SDK 现签设备指纹(${macNativeDir()})`
162
+ : "使用 native/ 里的 Beacon SDK 现签设备指纹"
163
+ }
164
+ if (process.platform === "darwin") {
165
+ return `没找到可用的 Marvis 客户端出票组件:${macErrorText() || "未安装 Marvis.app"}`
166
+ }
167
+ return "本地生成设备指纹(无需 native/ 目录)"
168
+ }
@@ -0,0 +1,230 @@
1
+ // 图片输入归一 —— Marvis 只接受 OpenAI image_url + data:image/...;base64 形态。
2
+ //
3
+ // 这里处理三种输入:
4
+ // 1. http(s) 图片 URL:插件下载后转 data URI(不带任何 Marvis 鉴权头);
5
+ // 2. 裸 base64:识别常见图片头后补 data URI;
6
+ // 3. Anthropic { type:"image", source:{...} }:显式转成 OpenAI image_url。
7
+ // 无法识别或下载失败时抛出 InvalidImageError,绝不静默丢图。
8
+
9
+ import { lookup as defaultLookup } from "node:dns/promises"
10
+ import { isIP } from "node:net"
11
+
12
+ export class InvalidImageError extends Error {}
13
+
14
+ const MAX_IMAGE_BYTES = 20 * 1024 * 1024
15
+ const DOWNLOAD_TIMEOUT_MS = 20_000
16
+ const SUPPORTED_MIME = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"])
17
+
18
+ function fail(message) {
19
+ throw new InvalidImageError(message)
20
+ }
21
+
22
+ function sniffMime(bytes) {
23
+ if (bytes.length >= 8 && bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return "image/png"
24
+ if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) return "image/jpeg"
25
+ if (bytes.length >= 6 && ["GIF87a", "GIF89a"].includes(bytes.subarray(0, 6).toString("ascii"))) return "image/gif"
26
+ if (bytes.length >= 12 && bytes.subarray(0, 4).toString("ascii") === "RIFF" && bytes.subarray(8, 12).toString("ascii") === "WEBP") return "image/webp"
27
+ return ""
28
+ }
29
+
30
+ function normalizedMime(bytes, declared = "") {
31
+ const mime = sniffMime(bytes)
32
+ if (mime) return mime
33
+ const clean = String(declared).split(";", 1)[0].trim().toLowerCase()
34
+ if (clean === "image/jpg") return "image/jpeg"
35
+ if (SUPPORTED_MIME.has(clean)) return clean
36
+ return ""
37
+ }
38
+
39
+ function decodeBase64(input) {
40
+ const compact = String(input ?? "").replace(/\s+/g, "").replace(/-/g, "+").replace(/_/g, "/")
41
+ if (!compact || !/^[A-Za-z0-9+/]*={0,2}$/.test(compact)) fail("裸 base64 图片格式无效;请改用完整的 data:image/...;base64,...。")
42
+ if (compact.length > Math.ceil(MAX_IMAGE_BYTES / 3) * 4 + 4) fail("图片超过 20 MiB 上限,未发往 Marvis。")
43
+ const unpadded = compact.replace(/=+$/, "")
44
+ if (unpadded.length % 4 === 1) fail("裸 base64 图片长度无效;请改用完整的 data URI。")
45
+ const padded = unpadded + "=".repeat((4 - (unpadded.length % 4)) % 4)
46
+ const bytes = Buffer.from(padded, "base64")
47
+ if (!bytes.length || bytes.length > MAX_IMAGE_BYTES) fail("图片为空或超过 20 MiB 上限,未发往 Marvis。")
48
+ return bytes
49
+ }
50
+
51
+ function base64DataUri(data, declaredMime = "") {
52
+ const bytes = decodeBase64(data)
53
+ const mime = normalizedMime(bytes, declaredMime)
54
+ if (!mime) fail("无法识别图片类型(仅支持 PNG / JPEG / GIF / WebP);请改用带正确 media type 的 data URI。")
55
+ return `data:${mime};base64,${bytes.toString("base64")}`
56
+ }
57
+
58
+ async function readLimited(response) {
59
+ if (!response.body) return Buffer.alloc(0)
60
+ const reader = response.body.getReader()
61
+ const chunks = []
62
+ let total = 0
63
+ while (true) {
64
+ const { done, value } = await reader.read()
65
+ if (done) break
66
+ const chunk = Buffer.from(value)
67
+ total += chunk.length
68
+ if (total > MAX_IMAGE_BYTES) {
69
+ await reader.cancel().catch(() => {})
70
+ fail("远程图片超过 20 MiB 上限,未发往 Marvis。")
71
+ }
72
+ chunks.push(chunk)
73
+ }
74
+ return Buffer.concat(chunks, total)
75
+ }
76
+
77
+ function isPrivateIp(ip) {
78
+ if (ip.includes(":")) {
79
+ const lower = ip.toLowerCase()
80
+ if (lower === "::1" || lower === "::") return true
81
+ if (lower.startsWith("fe80:") || lower.startsWith("fc") || lower.startsWith("fd")) return true
82
+ const mapped = lower.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/)
83
+ if (mapped) return isPrivateIp(mapped[1])
84
+ return false
85
+ }
86
+ const parts = ip.split(".").map(Number)
87
+ if (parts.length !== 4 || parts.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return true
88
+ const [a, b] = parts
89
+ if (a === 0 || a === 10 || a === 127) return true // 本机 / 私网 / loopback
90
+ if (a === 100 && b >= 64 && b <= 127) return true // CGNAT
91
+ if (a === 169 && b === 254) return true // link-local,含云元数据 169.254.169.254
92
+ if (a === 172 && b >= 16 && b <= 31) return true
93
+ if (a === 192 && b === 168) return true
94
+ if (a === 192 && b === 0) return true // 192.0.0/24 与 192.0.2/24 文档段一并拒绝
95
+ if (a === 198 && (b === 18 || b === 19 || b === 51)) return true
96
+ if (a === 203 && b === 0) return true
97
+ if (a >= 224) return true // 组播 / 保留
98
+ return false
99
+ }
100
+
101
+ async function assertPublicHttpUrl(parsed, lookupImpl) {
102
+ if (parsed.username || parsed.password) fail("图片 URL 不允许携带账号信息,未发往 Marvis。")
103
+ const host = parsed.hostname.replace(/^\[|\]$/g, "")
104
+
105
+ // 字面 IP:SSRF 面最大,一条都不放。
106
+ if (isIP(host)) {
107
+ if (isPrivateIp(host)) fail("图片地址指向本机 / 内网 / 保留地址,已拦截,未发往 Marvis。")
108
+ return
109
+ }
110
+
111
+ let records
112
+ try {
113
+ records = await lookupImpl(host, { all: true, verbatim: true })
114
+ } catch {
115
+ fail("图片域名无法解析,未发往 Marvis。")
116
+ }
117
+ const resolved = records || []
118
+ if (!resolved.length) fail("图片域名没有可用解析结果,未发往 Marvis。")
119
+
120
+ // ⚠ 域名解析里**混进保留地址不整体拒**(issue #34):开了 DNS 过滤 / sinkhole 的机器上,
121
+ // 同一域名常见「N 条公网 + 若干条 0.0.0.0」,逐条否决会把完全正常的外链图一起拦掉
122
+ // (实测 cdn.jsdelivr.net → 3 条 Cloudflare IPv6 + 2 条 0.0.0.0,curl 走 IPv6 能取到图)。
123
+ // 只要**还有一条公网记录**就放行;一条不剩才拒。
124
+ // 字面 IP 那条分支仍然严格拒绝 —— SSRF 面没有变化。
125
+ // 残余风险:严格防 DNS rebinding 需要连接层固定已校验地址,当前实现不覆盖,在此明示。
126
+ const publicRecords = resolved.filter((record) => !isPrivateIp(record.address))
127
+ if (!publicRecords.length) {
128
+ fail("图片地址指向本机 / 内网 / 保留地址,已拦截,未发往 Marvis。")
129
+ }
130
+ }
131
+
132
+ function normalizeDataUri(url) {
133
+ const comma = url.indexOf(",")
134
+ if (comma < 0) fail("data URI 缺少数据段,未发往 Marvis。")
135
+ const header = url.slice(5, comma)
136
+ if (!/;base64/i.test(header)) fail("只支持 base64 形态的 data:image URI,未发往 Marvis。")
137
+ const declared = header.split(";")[0]
138
+ return base64DataUri(url.slice(comma + 1), declared)
139
+ }
140
+
141
+ async function downloadDataUri(url, fetchImpl, lookupImpl = defaultLookup) {
142
+ let parsed
143
+ try {
144
+ parsed = new URL(url)
145
+ } catch {
146
+ fail("图片 URL 无效,未发往 Marvis。")
147
+ }
148
+ if (!["http:", "https:"].includes(parsed.protocol)) fail("图片 URL 只支持 http / https,未发往 Marvis。")
149
+ await assertPublicHttpUrl(parsed, lookupImpl)
150
+
151
+ let response
152
+ try {
153
+ // 不跟随重定向:公网 URL 跳到内网是常见 SSRF 路径,直接拒绝。
154
+ response = await fetchImpl(parsed.href, { signal: AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS), redirect: "error" })
155
+ } catch {
156
+ fail("远程图片下载失败、超时或发生重定向,未发往 Marvis。")
157
+ }
158
+ if (!response.ok) fail(`远程图片下载失败(HTTP ${response.status}),未发往 Marvis。`)
159
+ const declaredLength = Number(response.headers.get("content-length") ?? 0)
160
+ if (declaredLength > MAX_IMAGE_BYTES) fail("远程图片超过 20 MiB 上限,未发往 Marvis。")
161
+ let bytes
162
+ try {
163
+ bytes = await readLimited(response)
164
+ } catch (err) {
165
+ if (err instanceof InvalidImageError) throw err
166
+ fail("读取远程图片失败,未发往 Marvis。")
167
+ }
168
+ if (!bytes.length) fail("远程图片内容为空,未发往 Marvis。")
169
+ const mime = normalizedMime(bytes, response.headers.get("content-type") ?? "")
170
+ if (!mime) fail("远程地址返回的不是受支持的 PNG / JPEG / GIF / WebP 图片,未发往 Marvis。")
171
+ return `data:${mime};base64,${bytes.toString("base64")}`
172
+ }
173
+
174
+ async function normalizeUrl(value, fetchImpl, lookupImpl) {
175
+ const url = String(value ?? "").trim()
176
+ if (!url) fail("图片地址为空,未发往 Marvis。")
177
+ // data URI 同样过大小 / base64 / 图片头校验后重编码,不再原样透传。
178
+ if (/^data:image\//i.test(url)) return normalizeDataUri(url)
179
+ if (/^https?:\/\//i.test(url)) return downloadDataUri(url, fetchImpl, lookupImpl)
180
+ return base64DataUri(url)
181
+ }
182
+
183
+ async function normalizePart(part, fetchImpl, lookupImpl) {
184
+ if (!part || typeof part !== "object" || Array.isArray(part)) return part
185
+ if (part.type === "image_url") {
186
+ const current = typeof part.image_url === "string" ? part.image_url : part.image_url?.url
187
+ const url = await normalizeUrl(current, fetchImpl, lookupImpl)
188
+ if (url === current) return part
189
+ const imageUrl = typeof part.image_url === "object" && part.image_url !== null ? part.image_url : {}
190
+ return { ...part, image_url: { ...imageUrl, url } }
191
+ }
192
+ if (part.type === "image") {
193
+ const source = part.source
194
+ if (source?.type === "base64" && source.data) {
195
+ return { type: "image_url", image_url: { url: base64DataUri(source.data, source.media_type) } }
196
+ }
197
+ if (source?.type === "url" && source.url) {
198
+ return { type: "image_url", image_url: { url: await downloadDataUri(source.url, fetchImpl, lookupImpl) } }
199
+ }
200
+ fail("Anthropic image part 缺少可转换的 base64 / url source;为避免静默丢图,这次请求已拦截。")
201
+ }
202
+ return part
203
+ }
204
+
205
+ /** 归一 messages 里的图片 part;无改动时返回原数组。 */
206
+ export async function normalizeMessages(messages, fetchImpl = globalThis.fetch, lookupImpl = defaultLookup) {
207
+ if (!Array.isArray(messages)) return messages
208
+ let changed = false
209
+ const out = []
210
+ for (const message of messages) {
211
+ if (!message || typeof message !== "object" || !Array.isArray(message.content)) {
212
+ out.push(message)
213
+ continue
214
+ }
215
+ let messageChanged = false
216
+ const content = []
217
+ for (const part of message.content) {
218
+ const next = await normalizePart(part, fetchImpl, lookupImpl)
219
+ if (next !== part) messageChanged = true
220
+ content.push(next)
221
+ }
222
+ if (messageChanged) {
223
+ out.push({ ...message, content })
224
+ changed = true
225
+ } else {
226
+ out.push(message)
227
+ }
228
+ }
229
+ return changed ? out : messages
230
+ }