dsh-ntr 0.3.1 → 0.3.3
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/README.en.md +3 -1
- package/README.md +3 -1
- package/cordis.patch.yml +17 -1
- package/lib/bridge.js +2436 -82
- package/lib/client/acp-ui.js +103 -3
- package/lib/client/acp-view.js +24 -4
- package/lib/client/activity-pill.js +156 -0
- package/lib/client/branch-action.js +149 -0
- package/lib/client/composer-mode.js +48 -13
- package/lib/client/context-meter.js +86 -0
- package/lib/client/index.js +279 -16
- package/lib/client/model-seat.js +8 -2
- package/lib/client/panel.js +96 -29
- package/lib/client/released-banner.js +66 -0
- package/lib/client/session-controls.js +38 -13
- package/lib/client/slash-isolation.js +2 -2
- package/lib/client/store.js +380 -13
- package/lib/client.js +1364 -70
- package/lib/client.js.map +1 -1
- package/lib/impl.js +1229 -8
- package/lib/index.js +34 -0
- package/lib/protocol.js +19 -0
- package/lib/tps.js +32 -0
- package/lib/types/bridge.d.ts +850 -18
- package/lib/types/client/acp-ui.d.ts +34 -2
- package/lib/types/client/acp-view.d.ts +13 -2
- package/lib/types/client/activity-pill.d.ts +81 -0
- package/lib/types/client/branch-action.d.ts +47 -0
- package/lib/types/client/composer-mode.d.ts +28 -0
- package/lib/types/client/context-meter.d.ts +51 -0
- package/lib/types/client/model-seat.d.ts +21 -0
- package/lib/types/client/released-banner.d.ts +34 -0
- package/lib/types/client/slash-isolation.d.ts +1 -1
- package/lib/types/client/store.d.ts +108 -1
- package/lib/types/impl.d.ts +94 -7
- package/lib/types/protocol.d.ts +145 -0
- package/lib/types/tps.d.ts +17 -0
- package/package.json +2 -1
- package/src/bridge.ts +2754 -86
- package/src/client/acp-ui.tsx +123 -2
- package/src/client/acp-view.tsx +30 -5
- package/src/client/activity-pill.tsx +201 -0
- package/src/client/branch-action.tsx +178 -0
- package/src/client/composer-mode.tsx +59 -13
- package/src/client/context-meter.tsx +128 -0
- package/src/client/index.tsx +298 -15
- package/src/client/model-seat.tsx +28 -3
- package/src/client/panel.tsx +157 -40
- package/src/client/primitives.d.ts +27 -0
- package/src/client/released-banner.tsx +97 -0
- package/src/client/session-controls.tsx +34 -11
- package/src/client/slash-isolation.ts +2 -1
- package/src/client/store.ts +412 -14
- package/src/impl.ts +1251 -13
- package/src/index.ts +33 -0
- package/src/protocol.ts +145 -0
- package/src/tps.ts +40 -0
package/lib/impl.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* fakes.
|
|
6
6
|
*/
|
|
7
7
|
import { randomUUID } from "./ids.js";
|
|
8
|
-
import { DEFAULT_TAG_HALO } from "./protocol.js";
|
|
8
|
+
import { DEFAULT_IDLE_TIMEOUT_MS, DEFAULT_TAG_HALO, IDLE_RELEASE_MS } from "./protocol.js";
|
|
9
9
|
export { randomUUID };
|
|
10
10
|
/** Structured failure the RPC layer converts to `{ok:false, error}` and `apply()` surfaces at boot. */
|
|
11
11
|
export class AcpError extends Error {
|
|
@@ -158,6 +158,20 @@ export function validateApprovalDeadlines(value) {
|
|
|
158
158
|
}
|
|
159
159
|
return out;
|
|
160
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* Validate the idle-release window the settings page writes.
|
|
163
|
+
*
|
|
164
|
+
* The window is expressed in MILLISECONDS on the shared ladder
|
|
165
|
+
* ({@link IDLE_RELEASE_MS}), so a value the slider can produce is always a
|
|
166
|
+
* value the host runs — the same one-ladder rule the approval deadline card
|
|
167
|
+
* follows.
|
|
168
|
+
*/
|
|
169
|
+
export function validateIdleReleaseMs(raw, code) {
|
|
170
|
+
if (typeof raw !== 'number' || !Number.isInteger(raw) || !IDLE_RELEASE_MS.includes(raw)) {
|
|
171
|
+
throw new AcpError(code, `acp: idleTimeoutMs must be one of ${IDLE_RELEASE_MS.join(', ')}`);
|
|
172
|
+
}
|
|
173
|
+
return raw;
|
|
174
|
+
}
|
|
161
175
|
/**
|
|
162
176
|
* Validate the mode a write asks for. Three values, and the list is the whole
|
|
163
177
|
* vocabulary: an unanswered ask is either answered for you, kept waiting, or
|
|
@@ -502,6 +516,14 @@ export function validateConfig(raw) {
|
|
|
502
516
|
// `abort` a zero window would refuse every ask on arrival — the agent stops
|
|
503
517
|
// at every gate — so it becomes the shortest real wait.
|
|
504
518
|
const effectivePermissionTimeoutMs = normalizeApprovalWindow(permissionTimeoutMode, permissionTimeoutMs);
|
|
519
|
+
// The chat-process idle-release window (the settings page edits it).
|
|
520
|
+
// Same shape as the per-agent field: 0 = never release, otherwise an
|
|
521
|
+
// integer number of milliseconds.
|
|
522
|
+
const idleTimeoutMs = value.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
|
|
523
|
+
if (typeof idleTimeoutMs !== 'number' || !Number.isInteger(idleTimeoutMs)
|
|
524
|
+
|| idleTimeoutMs > 86_400_000 || (idleTimeoutMs !== 0 && idleTimeoutMs < 1_000)) {
|
|
525
|
+
throw new AcpError('bad_config', 'acp: idleTimeoutMs must be 0 (never release) or an integer in [1000, 86400000]');
|
|
526
|
+
}
|
|
505
527
|
const cancelFallbackMs = value.cancelFallbackMs ?? 15_000;
|
|
506
528
|
if (typeof cancelFallbackMs !== 'number' || !Number.isInteger(cancelFallbackMs) || cancelFallbackMs < 0 || cancelFallbackMs > 600_000) {
|
|
507
529
|
throw new AcpError('bad_config', 'acp: cancelFallbackMs must be an integer in [0, 600000] (0 = disable the fallback)');
|
|
@@ -543,6 +565,7 @@ export function validateConfig(raw) {
|
|
|
543
565
|
turnStallMs,
|
|
544
566
|
turnStallToolMs,
|
|
545
567
|
turnStallTickMs,
|
|
568
|
+
idleTimeoutMs,
|
|
546
569
|
};
|
|
547
570
|
}
|
|
548
571
|
// ---- Workspace guard ----
|
|
@@ -1287,6 +1310,1068 @@ export function describeUnhandledUpdate(kind, update) {
|
|
|
1287
1310
|
}
|
|
1288
1311
|
return `${label}\n${dumpJson(update)}`;
|
|
1289
1312
|
}
|
|
1313
|
+
/** Helper: guess MIME type from file extension or URL */
|
|
1314
|
+
export function guessImageMimeType(pathOrUrl) {
|
|
1315
|
+
if (!pathOrUrl)
|
|
1316
|
+
return undefined;
|
|
1317
|
+
const clean = pathOrUrl.split('?')[0].split('#')[0].toLowerCase();
|
|
1318
|
+
if (clean.endsWith('.jpg') || clean.endsWith('.jpeg'))
|
|
1319
|
+
return 'image/jpeg';
|
|
1320
|
+
if (clean.endsWith('.png'))
|
|
1321
|
+
return 'image/png';
|
|
1322
|
+
if (clean.endsWith('.gif'))
|
|
1323
|
+
return 'image/gif';
|
|
1324
|
+
if (clean.endsWith('.webp'))
|
|
1325
|
+
return 'image/webp';
|
|
1326
|
+
if (clean.endsWith('.svg') || clean.endsWith('.svgz'))
|
|
1327
|
+
return 'image/svg+xml';
|
|
1328
|
+
if (clean.endsWith('.bmp'))
|
|
1329
|
+
return 'image/bmp';
|
|
1330
|
+
if (clean.endsWith('.ico'))
|
|
1331
|
+
return 'image/x-icon';
|
|
1332
|
+
if (clean.endsWith('.tiff') || clean.endsWith('.tif'))
|
|
1333
|
+
return 'image/tiff';
|
|
1334
|
+
if (clean.endsWith('.avif'))
|
|
1335
|
+
return 'image/avif';
|
|
1336
|
+
if (clean.endsWith('.heic'))
|
|
1337
|
+
return 'image/heic';
|
|
1338
|
+
if (clean.endsWith('.heif'))
|
|
1339
|
+
return 'image/heif';
|
|
1340
|
+
if (clean.endsWith('.apng'))
|
|
1341
|
+
return 'image/apng';
|
|
1342
|
+
return undefined;
|
|
1343
|
+
}
|
|
1344
|
+
/** Helper: parse Data URI */
|
|
1345
|
+
export function parseImageDataUrl(url) {
|
|
1346
|
+
if (!url.startsWith('data:'))
|
|
1347
|
+
return undefined;
|
|
1348
|
+
const commaIdx = url.indexOf(',');
|
|
1349
|
+
if (commaIdx === -1)
|
|
1350
|
+
return undefined;
|
|
1351
|
+
const header = url.slice(5, commaIdx);
|
|
1352
|
+
const raw = url.slice(commaIdx + 1).trim();
|
|
1353
|
+
const parts = header.split(';');
|
|
1354
|
+
const mimeType = parts[0] && parts[0].trim() !== '' ? parts[0].trim().toLowerCase() : 'image/png';
|
|
1355
|
+
if (!mimeType.startsWith('image/'))
|
|
1356
|
+
return undefined;
|
|
1357
|
+
const isBase64 = parts.some(p => p.trim() === 'base64');
|
|
1358
|
+
let data;
|
|
1359
|
+
if (isBase64) {
|
|
1360
|
+
data = raw.replace(/\s+/g, '');
|
|
1361
|
+
}
|
|
1362
|
+
else {
|
|
1363
|
+
try {
|
|
1364
|
+
const decoded = decodeURIComponent(raw);
|
|
1365
|
+
data = typeof Buffer !== 'undefined' ? Buffer.from(decoded).toString('base64') : btoa(decoded);
|
|
1366
|
+
}
|
|
1367
|
+
catch {
|
|
1368
|
+
data = typeof Buffer !== 'undefined' ? Buffer.from(raw).toString('base64') : btoa(raw);
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return { mimeType, data };
|
|
1372
|
+
}
|
|
1373
|
+
export function isLocalFilePath(str) {
|
|
1374
|
+
if (typeof str !== 'string' || str === '')
|
|
1375
|
+
return false;
|
|
1376
|
+
return isAbsolutePath(str) || str.startsWith('./') || str.startsWith('.\\') || str.startsWith('../') || str.startsWith('..\\');
|
|
1377
|
+
}
|
|
1378
|
+
export function normalizeFileUrlToPath(uri) {
|
|
1379
|
+
if (!uri.startsWith('file://'))
|
|
1380
|
+
return uri;
|
|
1381
|
+
let raw = uri.slice('file://'.length);
|
|
1382
|
+
try {
|
|
1383
|
+
raw = decodeURIComponent(raw);
|
|
1384
|
+
}
|
|
1385
|
+
catch { }
|
|
1386
|
+
if (raw.startsWith('localhost/')) {
|
|
1387
|
+
raw = raw.slice('localhost'.length);
|
|
1388
|
+
}
|
|
1389
|
+
if (/^\/?[a-zA-Z]:/.test(raw)) {
|
|
1390
|
+
return raw.startsWith('/') ? raw.slice(1) : raw;
|
|
1391
|
+
}
|
|
1392
|
+
if (raw.startsWith('/')) {
|
|
1393
|
+
return raw;
|
|
1394
|
+
}
|
|
1395
|
+
return `//${raw}`;
|
|
1396
|
+
}
|
|
1397
|
+
function expandHomePath(p) {
|
|
1398
|
+
if (p === '~' || p.startsWith('~/') || p.startsWith('~\\')) {
|
|
1399
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
1400
|
+
const env = process.env;
|
|
1401
|
+
const home = env.HOME || env.USERPROFILE || (env.HOMEDRIVE && env.HOMEPATH ? `${env.HOMEDRIVE}${env.HOMEPATH}` : undefined);
|
|
1402
|
+
if (home) {
|
|
1403
|
+
const cleanHome = home.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
1404
|
+
return p === '~' ? cleanHome : `${cleanHome}/${p.slice(2).replaceAll('\\', '/')}`;
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
return p;
|
|
1409
|
+
}
|
|
1410
|
+
function readImageFileFromContext(filePath, context) {
|
|
1411
|
+
const target = normalizeFileUrlToPath(filePath);
|
|
1412
|
+
const expanded = expandHomePath(target);
|
|
1413
|
+
const pathsToTry = target === expanded ? [target] : [target, expanded];
|
|
1414
|
+
const builtinFs = typeof process !== 'undefined' && typeof process.getBuiltinModule === 'function'
|
|
1415
|
+
? process.getBuiltinModule('fs')
|
|
1416
|
+
: null;
|
|
1417
|
+
for (const p of pathsToTry) {
|
|
1418
|
+
if (context && typeof context.readFile === 'function') {
|
|
1419
|
+
try {
|
|
1420
|
+
const res = context.readFile(p);
|
|
1421
|
+
if (res !== undefined)
|
|
1422
|
+
return res;
|
|
1423
|
+
}
|
|
1424
|
+
catch { }
|
|
1425
|
+
}
|
|
1426
|
+
if (context && typeof context.fs?.readFileSync === 'function') {
|
|
1427
|
+
try {
|
|
1428
|
+
const res = context.fs.readFileSync(p);
|
|
1429
|
+
if (res !== undefined)
|
|
1430
|
+
return res;
|
|
1431
|
+
}
|
|
1432
|
+
catch { }
|
|
1433
|
+
}
|
|
1434
|
+
if (builtinFs && typeof builtinFs.existsSync === 'function' && typeof builtinFs.readFileSync === 'function') {
|
|
1435
|
+
try {
|
|
1436
|
+
if (builtinFs.existsSync(p))
|
|
1437
|
+
return builtinFs.readFileSync(p);
|
|
1438
|
+
}
|
|
1439
|
+
catch { }
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
return undefined;
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1445
|
+
* Detect image MIME type by examining binary magic numbers.
|
|
1446
|
+
* Supports Buffer, Uint8Array, and base64 strings.
|
|
1447
|
+
*/
|
|
1448
|
+
export function detectImageMimeFromMagic(raw) {
|
|
1449
|
+
if (!raw)
|
|
1450
|
+
return undefined;
|
|
1451
|
+
let bytes;
|
|
1452
|
+
if (typeof raw === 'string') {
|
|
1453
|
+
const trimmed = raw.trim();
|
|
1454
|
+
if (trimmed.startsWith('<svg') || (trimmed.startsWith('<?xml') && trimmed.includes('<svg'))) {
|
|
1455
|
+
return 'image/svg+xml';
|
|
1456
|
+
}
|
|
1457
|
+
try {
|
|
1458
|
+
const prefix = trimmed.slice(0, 64).replace(/\s+/g, '');
|
|
1459
|
+
if (typeof Buffer !== 'undefined') {
|
|
1460
|
+
bytes = Buffer.from(prefix, 'base64');
|
|
1461
|
+
}
|
|
1462
|
+
else if (typeof atob !== 'undefined') {
|
|
1463
|
+
const bin = atob(prefix);
|
|
1464
|
+
bytes = new Uint8Array(bin.length);
|
|
1465
|
+
for (let i = 0; i < bin.length; i++)
|
|
1466
|
+
bytes[i] = bin.charCodeAt(i);
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
catch {
|
|
1470
|
+
return undefined;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
else if (raw instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(raw))) {
|
|
1474
|
+
bytes = raw instanceof Uint8Array ? raw : new Uint8Array(raw);
|
|
1475
|
+
}
|
|
1476
|
+
if (!bytes || bytes.length < 3)
|
|
1477
|
+
return undefined;
|
|
1478
|
+
// PNG: 89 50 4E 47 0D 0A 1A 0A
|
|
1479
|
+
if (bytes.length >= 8 &&
|
|
1480
|
+
bytes[0] === 0x89 && bytes[1] === 0x50 && bytes[2] === 0x4e && bytes[3] === 0x47 &&
|
|
1481
|
+
bytes[4] === 0x0d && bytes[5] === 0x0a && bytes[6] === 0x1a && bytes[7] === 0x0a) {
|
|
1482
|
+
return 'image/png';
|
|
1483
|
+
}
|
|
1484
|
+
// JPEG: FF D8 FF
|
|
1485
|
+
if (bytes[0] === 0xff && bytes[1] === 0xd8 && bytes[2] === 0xff) {
|
|
1486
|
+
return 'image/jpeg';
|
|
1487
|
+
}
|
|
1488
|
+
// GIF: GIF87a or GIF89a (47 49 46 38)
|
|
1489
|
+
if (bytes.length >= 6 &&
|
|
1490
|
+
bytes[0] === 0x47 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x38 &&
|
|
1491
|
+
(bytes[4] === 0x37 || bytes[4] === 0x39) && bytes[5] === 0x61) {
|
|
1492
|
+
return 'image/gif';
|
|
1493
|
+
}
|
|
1494
|
+
// WebP: RIFF....WEBP (52 49 46 46 .... 57 45 42 50)
|
|
1495
|
+
if (bytes.length >= 12 &&
|
|
1496
|
+
bytes[0] === 0x52 && bytes[1] === 0x49 && bytes[2] === 0x46 && bytes[3] === 0x46 &&
|
|
1497
|
+
bytes[8] === 0x57 && bytes[9] === 0x45 && bytes[10] === 0x42 && bytes[11] === 0x50) {
|
|
1498
|
+
return 'image/webp';
|
|
1499
|
+
}
|
|
1500
|
+
// BMP: 42 4D ("BM")
|
|
1501
|
+
if (bytes.length >= 2 && bytes[0] === 0x42 && bytes[1] === 0x4d) {
|
|
1502
|
+
return 'image/bmp';
|
|
1503
|
+
}
|
|
1504
|
+
// ICO: 00 00 01 00
|
|
1505
|
+
if (bytes.length >= 4 && bytes[0] === 0x00 && bytes[1] === 0x00 && bytes[2] === 0x01 && bytes[3] === 0x00) {
|
|
1506
|
+
return 'image/x-icon';
|
|
1507
|
+
}
|
|
1508
|
+
// TIFF: II*. (49 49 2A 00) or MM.* (4D 4D 00 2A)
|
|
1509
|
+
if (bytes.length >= 4 &&
|
|
1510
|
+
((bytes[0] === 0x49 && bytes[1] === 0x49 && bytes[2] === 0x2a && bytes[3] === 0x00) ||
|
|
1511
|
+
(bytes[0] === 0x4d && bytes[1] === 0x4d && bytes[2] === 0x00 && bytes[3] === 0x2a))) {
|
|
1512
|
+
return 'image/tiff';
|
|
1513
|
+
}
|
|
1514
|
+
// AVIF / HEIC: ftyp box at offset 4 (bytes 4..7 === 'ftyp')
|
|
1515
|
+
if (bytes.length >= 12 &&
|
|
1516
|
+
bytes[4] === 0x66 && bytes[5] === 0x74 && bytes[6] === 0x79 && bytes[7] === 0x70) {
|
|
1517
|
+
const brand = String.fromCharCode(bytes[8], bytes[9], bytes[10], bytes[11]).toLowerCase();
|
|
1518
|
+
if (brand === 'avif' || brand === 'avis')
|
|
1519
|
+
return 'image/avif';
|
|
1520
|
+
if (brand === 'heic' || brand === 'heix' || brand === 'hevc' || brand === 'mif1' || brand === 'msf1')
|
|
1521
|
+
return 'image/heic';
|
|
1522
|
+
}
|
|
1523
|
+
return undefined;
|
|
1524
|
+
}
|
|
1525
|
+
function tryResolveFromAttachmentStore(store, id, context, attachmentMeta) {
|
|
1526
|
+
const idWithoutPrefix = id.startsWith('sha256:') ? id.slice('sha256:'.length) : undefined;
|
|
1527
|
+
const idWithPrefix = id.startsWith('sha256:') ? id : `sha256:${id}`;
|
|
1528
|
+
const candidates = [];
|
|
1529
|
+
if (attachmentMeta && typeof attachmentMeta === 'object') {
|
|
1530
|
+
candidates.push(attachmentMeta);
|
|
1531
|
+
}
|
|
1532
|
+
candidates.push({ attachmentId: idWithPrefix });
|
|
1533
|
+
if (idWithoutPrefix !== undefined) {
|
|
1534
|
+
candidates.push({ attachmentId: idWithoutPrefix });
|
|
1535
|
+
}
|
|
1536
|
+
candidates.push({ attachmentId: id });
|
|
1537
|
+
candidates.push({ id: idWithPrefix });
|
|
1538
|
+
candidates.push({ id });
|
|
1539
|
+
if (idWithoutPrefix !== undefined) {
|
|
1540
|
+
candidates.push({ id: idWithoutPrefix });
|
|
1541
|
+
}
|
|
1542
|
+
candidates.push(id);
|
|
1543
|
+
if (idWithoutPrefix !== undefined) {
|
|
1544
|
+
candidates.push(idWithoutPrefix);
|
|
1545
|
+
}
|
|
1546
|
+
// Cordis proxies wrap the underlying service instance; if a method call on the proxy fails
|
|
1547
|
+
// or loses this context, fall back to the unwrapped instance (never the class prototype,
|
|
1548
|
+
// which lacks instance state such as `this.root`).
|
|
1549
|
+
const cordisOriginal = Reflect.get(store, Symbol.for('cordis.original'));
|
|
1550
|
+
// A. LocalAttachmentStore.imageHostPath(ref) -> synchronous local absolute path
|
|
1551
|
+
if (typeof store.imageHostPath === 'function') {
|
|
1552
|
+
const fn = store.imageHostPath;
|
|
1553
|
+
for (const cand of candidates) {
|
|
1554
|
+
if (cand === undefined)
|
|
1555
|
+
continue;
|
|
1556
|
+
try {
|
|
1557
|
+
let hostPath;
|
|
1558
|
+
try {
|
|
1559
|
+
hostPath = fn.call(store, cand);
|
|
1560
|
+
}
|
|
1561
|
+
catch {
|
|
1562
|
+
if (cordisOriginal && typeof cordisOriginal.imageHostPath === 'function') {
|
|
1563
|
+
hostPath = cordisOriginal.imageHostPath.call(cordisOriginal, cand);
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
if (hostPath && typeof hostPath.then === 'function') {
|
|
1567
|
+
if (typeof hostPath.catch === 'function') {
|
|
1568
|
+
hostPath.catch(() => { });
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
else if (typeof hostPath === 'string' && hostPath.trim() !== '') {
|
|
1572
|
+
const fileContent = readImageFileFromContext(hostPath, context);
|
|
1573
|
+
return {
|
|
1574
|
+
path: hostPath,
|
|
1575
|
+
...(fileContent !== undefined ? { data: fileContent } : {}),
|
|
1576
|
+
};
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
catch { }
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
// B. LocalAttachmentStore.fileHostPath(ref) -> fallback for file-type attachments
|
|
1583
|
+
if (typeof store.fileHostPath === 'function') {
|
|
1584
|
+
const fn = store.fileHostPath;
|
|
1585
|
+
for (const cand of candidates) {
|
|
1586
|
+
if (cand === undefined)
|
|
1587
|
+
continue;
|
|
1588
|
+
try {
|
|
1589
|
+
let hostPath;
|
|
1590
|
+
try {
|
|
1591
|
+
hostPath = fn.call(store, cand);
|
|
1592
|
+
}
|
|
1593
|
+
catch {
|
|
1594
|
+
if (cordisOriginal && typeof cordisOriginal.fileHostPath === 'function') {
|
|
1595
|
+
hostPath = cordisOriginal.fileHostPath.call(cordisOriginal, cand);
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
if (hostPath && typeof hostPath.then === 'function') {
|
|
1599
|
+
if (typeof hostPath.catch === 'function') {
|
|
1600
|
+
hostPath.catch(() => { });
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
else if (typeof hostPath === 'string' && hostPath.trim() !== '') {
|
|
1604
|
+
const fileContent = readImageFileFromContext(hostPath, context);
|
|
1605
|
+
return {
|
|
1606
|
+
path: hostPath,
|
|
1607
|
+
...(fileContent !== undefined ? { data: fileContent } : {}),
|
|
1608
|
+
};
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
catch { }
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
// C. LocalAttachmentStore.readImage(ref) -> { ref, data: Uint8Array }
|
|
1615
|
+
if (typeof store.readImage === 'function') {
|
|
1616
|
+
const fn = store.readImage;
|
|
1617
|
+
for (const cand of candidates) {
|
|
1618
|
+
if (cand === undefined)
|
|
1619
|
+
continue;
|
|
1620
|
+
try {
|
|
1621
|
+
let res;
|
|
1622
|
+
try {
|
|
1623
|
+
res = fn.call(store, cand);
|
|
1624
|
+
}
|
|
1625
|
+
catch {
|
|
1626
|
+
if (cordisOriginal && typeof cordisOriginal.readImage === 'function') {
|
|
1627
|
+
res = cordisOriginal.readImage.call(cordisOriginal, cand);
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
if (res && typeof res === 'object') {
|
|
1631
|
+
// If it's a Promise (async), safely attach catch to avoid unhandledRejection crashes
|
|
1632
|
+
if (typeof res.then === 'function') {
|
|
1633
|
+
if (typeof res.catch === 'function') {
|
|
1634
|
+
res.catch(() => { });
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
else {
|
|
1638
|
+
if (res instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(res))) {
|
|
1639
|
+
return res;
|
|
1640
|
+
}
|
|
1641
|
+
const data = res.data;
|
|
1642
|
+
if (data instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) || typeof data === 'string') {
|
|
1643
|
+
return { data };
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
catch { }
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
// D. readAttachment
|
|
1652
|
+
if (typeof store.readAttachment === 'function') {
|
|
1653
|
+
try {
|
|
1654
|
+
let res;
|
|
1655
|
+
try {
|
|
1656
|
+
res = store.readAttachment.call(store, id);
|
|
1657
|
+
}
|
|
1658
|
+
catch {
|
|
1659
|
+
if (cordisOriginal && typeof cordisOriginal.readAttachment === 'function') {
|
|
1660
|
+
res = cordisOriginal.readAttachment.call(cordisOriginal, id);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
if (res && typeof res.then === 'function') {
|
|
1664
|
+
if (typeof res.catch === 'function') {
|
|
1665
|
+
res.catch(() => { });
|
|
1666
|
+
}
|
|
1667
|
+
}
|
|
1668
|
+
else if (res !== undefined) {
|
|
1669
|
+
return res;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
catch { }
|
|
1673
|
+
}
|
|
1674
|
+
// E. getAttachment
|
|
1675
|
+
if (typeof store.getAttachment === 'function') {
|
|
1676
|
+
try {
|
|
1677
|
+
let res;
|
|
1678
|
+
try {
|
|
1679
|
+
res = store.getAttachment.call(store, id);
|
|
1680
|
+
}
|
|
1681
|
+
catch {
|
|
1682
|
+
if (cordisOriginal && typeof cordisOriginal.getAttachment === 'function') {
|
|
1683
|
+
res = cordisOriginal.getAttachment.call(cordisOriginal, id);
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
if (res && typeof res.then === 'function') {
|
|
1687
|
+
if (typeof res.catch === 'function') {
|
|
1688
|
+
res.catch(() => { });
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
else if (res !== undefined) {
|
|
1692
|
+
return res;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
catch { }
|
|
1696
|
+
}
|
|
1697
|
+
return undefined;
|
|
1698
|
+
}
|
|
1699
|
+
function tryStoreFreeDiskAttachment(id, context) {
|
|
1700
|
+
if (typeof id !== 'string')
|
|
1701
|
+
return undefined;
|
|
1702
|
+
let hash;
|
|
1703
|
+
const trimmed = id.trim();
|
|
1704
|
+
if (trimmed.startsWith('sha256:')) {
|
|
1705
|
+
hash = trimmed.slice('sha256:'.length).toLowerCase();
|
|
1706
|
+
}
|
|
1707
|
+
else if (/^[0-9a-f]{64}$/i.test(trimmed)) {
|
|
1708
|
+
hash = trimmed.toLowerCase();
|
|
1709
|
+
}
|
|
1710
|
+
if (!hash || hash.length < 2)
|
|
1711
|
+
return undefined;
|
|
1712
|
+
const prefix = hash.slice(0, 2);
|
|
1713
|
+
const relPaths = [
|
|
1714
|
+
`attachments/v1/objects/${prefix}/${hash}`,
|
|
1715
|
+
`attachments/v1/file-objects/${prefix}/${hash}`,
|
|
1716
|
+
];
|
|
1717
|
+
const candidatePaths = [];
|
|
1718
|
+
if (typeof process !== 'undefined' && process.env) {
|
|
1719
|
+
const env = process.env;
|
|
1720
|
+
if (env.DSH_NTR_STATE_DIR) {
|
|
1721
|
+
const stateDir = env.DSH_NTR_STATE_DIR.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
1722
|
+
const hostDir = stateDir.endsWith('/dsh-ntr') ? stateDir.slice(0, -'/dsh-ntr'.length) : stateDir;
|
|
1723
|
+
for (const rel of relPaths)
|
|
1724
|
+
candidatePaths.push(`${hostDir}/${rel}`);
|
|
1725
|
+
}
|
|
1726
|
+
if (env.DSH_DIR) {
|
|
1727
|
+
const dshDir = env.DSH_DIR.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
1728
|
+
for (const rel of relPaths)
|
|
1729
|
+
candidatePaths.push(`${dshDir}/${rel}`);
|
|
1730
|
+
}
|
|
1731
|
+
const home = env.HOME || env.USERPROFILE || (env.HOMEDRIVE && env.HOMEPATH ? `${env.HOMEDRIVE}${env.HOMEPATH}` : undefined);
|
|
1732
|
+
if (home) {
|
|
1733
|
+
const cleanHome = home.replaceAll('\\', '/').replace(/\/+$/, '');
|
|
1734
|
+
for (const rel of relPaths) {
|
|
1735
|
+
candidatePaths.push(`${cleanHome}/.dsh/${rel}`);
|
|
1736
|
+
candidatePaths.push(`${cleanHome}\\.dsh\\${rel.replaceAll('/', '\\')}`);
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
for (const rel of relPaths)
|
|
1741
|
+
candidatePaths.push(`~/.dsh/${rel}`);
|
|
1742
|
+
for (const p of candidatePaths) {
|
|
1743
|
+
const fileContent = readImageFileFromContext(p, context);
|
|
1744
|
+
if (fileContent !== undefined) {
|
|
1745
|
+
return {
|
|
1746
|
+
path: p,
|
|
1747
|
+
data: fileContent,
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
return undefined;
|
|
1752
|
+
}
|
|
1753
|
+
export function resolveImageAttachmentFromContext(id, context, attachmentMeta, rootContext) {
|
|
1754
|
+
const root = rootContext ?? context;
|
|
1755
|
+
if (context && typeof context === 'object') {
|
|
1756
|
+
const ctx = context;
|
|
1757
|
+
const direct = tryResolveFromAttachmentStore(ctx, id, root, attachmentMeta);
|
|
1758
|
+
if (direct !== undefined)
|
|
1759
|
+
return direct;
|
|
1760
|
+
if (typeof ctx.get === 'function') {
|
|
1761
|
+
try {
|
|
1762
|
+
const svc = ctx.get('attachments') ?? ctx.get('attachmentService');
|
|
1763
|
+
if (svc && typeof svc === 'object') {
|
|
1764
|
+
const res = resolveImageAttachmentFromContext(id, svc, attachmentMeta, root);
|
|
1765
|
+
if (res !== undefined)
|
|
1766
|
+
return res;
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
catch { }
|
|
1770
|
+
}
|
|
1771
|
+
try {
|
|
1772
|
+
if (ctx.attachmentService && typeof ctx.attachmentService === 'object') {
|
|
1773
|
+
const res = resolveImageAttachmentFromContext(id, ctx.attachmentService, attachmentMeta, root);
|
|
1774
|
+
if (res !== undefined)
|
|
1775
|
+
return res;
|
|
1776
|
+
}
|
|
1777
|
+
}
|
|
1778
|
+
catch { }
|
|
1779
|
+
try {
|
|
1780
|
+
const attStore = ctx.attachments;
|
|
1781
|
+
if (attStore) {
|
|
1782
|
+
if (typeof attStore === 'object') {
|
|
1783
|
+
const fromStore = tryResolveFromAttachmentStore(attStore, id, root, attachmentMeta);
|
|
1784
|
+
if (fromStore !== undefined)
|
|
1785
|
+
return fromStore;
|
|
1786
|
+
}
|
|
1787
|
+
if (attStore instanceof Map || typeof attStore.get === 'function') {
|
|
1788
|
+
try {
|
|
1789
|
+
return attStore.get(id);
|
|
1790
|
+
}
|
|
1791
|
+
catch { }
|
|
1792
|
+
}
|
|
1793
|
+
if (typeof attStore === 'object' && Object.prototype.hasOwnProperty.call(attStore, id)) {
|
|
1794
|
+
return attStore[id];
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
catch { }
|
|
1799
|
+
}
|
|
1800
|
+
return tryStoreFreeDiskAttachment(id, root);
|
|
1801
|
+
}
|
|
1802
|
+
function toBase64(data) {
|
|
1803
|
+
if (typeof data === 'string')
|
|
1804
|
+
return data.replace(/\s+/g, '');
|
|
1805
|
+
if (typeof Buffer !== 'undefined')
|
|
1806
|
+
return Buffer.from(data).toString('base64');
|
|
1807
|
+
let binary = '';
|
|
1808
|
+
const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
1809
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
1810
|
+
binary += String.fromCharCode(bytes[i]);
|
|
1811
|
+
}
|
|
1812
|
+
return btoa(binary);
|
|
1813
|
+
}
|
|
1814
|
+
/**
|
|
1815
|
+
* Extract an image payload (Base64 data + MIME type) from an image block and optional context.
|
|
1816
|
+
*/
|
|
1817
|
+
export function resolveImageContent(block, context) {
|
|
1818
|
+
if (!block || typeof block !== 'object')
|
|
1819
|
+
return undefined;
|
|
1820
|
+
const b = block;
|
|
1821
|
+
const attRecord = b.attachment && typeof b.attachment === 'object' ? b.attachment : undefined;
|
|
1822
|
+
const isExplicitImageBlock = b.type === 'image' || b.type === 'image_url' || b.type === 'attachment';
|
|
1823
|
+
const explicitMime = (typeof attRecord?.mediaType === 'string' && attRecord.mediaType.trim().toLowerCase()) ||
|
|
1824
|
+
(typeof attRecord?.mimeType === 'string' && attRecord.mimeType.trim().toLowerCase()) ||
|
|
1825
|
+
(typeof b.mediaType === 'string' && b.mediaType.trim().toLowerCase()) ||
|
|
1826
|
+
(typeof b.mimeType === 'string' && b.mimeType.trim().toLowerCase()) ||
|
|
1827
|
+
(typeof b.contentType === 'string' && b.contentType.trim().toLowerCase()) ||
|
|
1828
|
+
undefined;
|
|
1829
|
+
// 1. Direct base64 data: { data: string | Buffer | Uint8Array, mimeType?: string }
|
|
1830
|
+
if (typeof b.data === 'string' && b.data.trim() !== '') {
|
|
1831
|
+
if (b.data.startsWith('data:')) {
|
|
1832
|
+
const parsed = parseImageDataUrl(b.data);
|
|
1833
|
+
if (parsed) {
|
|
1834
|
+
const mimeType = explicitMime || parsed.mimeType || detectImageMimeFromMagic(parsed.data) || 'image/png';
|
|
1835
|
+
return { type: 'image', data: parsed.data, mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1836
|
+
}
|
|
1837
|
+
return undefined;
|
|
1838
|
+
}
|
|
1839
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.data) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1840
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1841
|
+
return {
|
|
1842
|
+
type: 'image',
|
|
1843
|
+
data: b.data.trim().replace(/\s+/g, ''),
|
|
1844
|
+
mimeType,
|
|
1845
|
+
...(typeof b.uri === 'string' ? { uri: b.uri } : {}),
|
|
1846
|
+
};
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(b.data)) {
|
|
1850
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.data) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1851
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1852
|
+
return { type: 'image', data: b.data.toString('base64'), mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1853
|
+
}
|
|
1854
|
+
}
|
|
1855
|
+
if (b.data instanceof Uint8Array) {
|
|
1856
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.data) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1857
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1858
|
+
return { type: 'image', data: toBase64(b.data), mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
// 2. Anthropic style: source: { type: 'base64', media_type: string, data: string }
|
|
1862
|
+
if (b.source && typeof b.source === 'object') {
|
|
1863
|
+
const src = b.source;
|
|
1864
|
+
const srcMime = (typeof src.media_type === 'string' && src.media_type.trim().toLowerCase()) ||
|
|
1865
|
+
(typeof src.mimeType === 'string' && src.mimeType.trim().toLowerCase()) ||
|
|
1866
|
+
explicitMime ||
|
|
1867
|
+
(typeof src.data === 'string' || src.data instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(src.data)) ? detectImageMimeFromMagic(src.data) : undefined) ||
|
|
1868
|
+
'image/png';
|
|
1869
|
+
if (srcMime.startsWith('image/')) {
|
|
1870
|
+
if (typeof src.data === 'string' && src.data.trim() !== '') {
|
|
1871
|
+
return {
|
|
1872
|
+
type: 'image',
|
|
1873
|
+
data: src.data.trim().replace(/\s+/g, ''),
|
|
1874
|
+
mimeType: srcMime,
|
|
1875
|
+
...(typeof b.uri === 'string' ? { uri: b.uri } : {}),
|
|
1876
|
+
};
|
|
1877
|
+
}
|
|
1878
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(src.data)) {
|
|
1879
|
+
return { type: 'image', data: src.data.toString('base64'), mimeType: srcMime, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1880
|
+
}
|
|
1881
|
+
if (src.data instanceof Uint8Array) {
|
|
1882
|
+
return { type: 'image', data: toBase64(src.data), mimeType: srcMime, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
// 3. Image property: { image: string | Buffer | Uint8Array }
|
|
1887
|
+
if (typeof b.image === 'string' && b.image.trim() !== '') {
|
|
1888
|
+
if (b.image.startsWith('data:')) {
|
|
1889
|
+
const parsed = parseImageDataUrl(b.image);
|
|
1890
|
+
if (parsed)
|
|
1891
|
+
return { type: 'image', data: parsed.data, mimeType: explicitMime || parsed.mimeType || detectImageMimeFromMagic(parsed.data) || 'image/png' };
|
|
1892
|
+
return undefined;
|
|
1893
|
+
}
|
|
1894
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.image) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1895
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1896
|
+
return { type: 'image', data: b.image.trim().replace(/\s+/g, ''), mimeType };
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(b.image)) {
|
|
1900
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.image) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1901
|
+
if (mimeType && mimeType.startsWith('image/'))
|
|
1902
|
+
return { type: 'image', data: b.image.toString('base64'), mimeType };
|
|
1903
|
+
}
|
|
1904
|
+
if (b.image instanceof Uint8Array) {
|
|
1905
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.image) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1906
|
+
if (mimeType && mimeType.startsWith('image/'))
|
|
1907
|
+
return { type: 'image', data: toBase64(b.image), mimeType };
|
|
1908
|
+
}
|
|
1909
|
+
// 4. OpenAI style image_url: { image_url: string | { url: string } }
|
|
1910
|
+
if (b.image_url) {
|
|
1911
|
+
const urlStr = typeof b.image_url === 'string'
|
|
1912
|
+
? b.image_url
|
|
1913
|
+
: (typeof b.image_url === 'object' && b.image_url !== null ? b.image_url.url : undefined);
|
|
1914
|
+
if (typeof urlStr === 'string' && urlStr.trim() !== '') {
|
|
1915
|
+
if (urlStr.startsWith('data:')) {
|
|
1916
|
+
const parsed = parseImageDataUrl(urlStr);
|
|
1917
|
+
if (parsed)
|
|
1918
|
+
return { type: 'image', data: parsed.data, mimeType: explicitMime || parsed.mimeType || detectImageMimeFromMagic(parsed.data) || 'image/png', uri: urlStr };
|
|
1919
|
+
return undefined;
|
|
1920
|
+
}
|
|
1921
|
+
if (urlStr.startsWith('http://') || urlStr.startsWith('https://')) {
|
|
1922
|
+
const mimeType = explicitMime || guessImageMimeType(urlStr) || 'image/png';
|
|
1923
|
+
return { type: 'image', data: '', mimeType, uri: urlStr };
|
|
1924
|
+
}
|
|
1925
|
+
if (urlStr.startsWith('file://') || isLocalFilePath(urlStr)) {
|
|
1926
|
+
const fileContent = readImageFileFromContext(urlStr, context);
|
|
1927
|
+
if (fileContent !== undefined) {
|
|
1928
|
+
let data;
|
|
1929
|
+
let resolvedMime;
|
|
1930
|
+
if (typeof fileContent === 'string') {
|
|
1931
|
+
if (fileContent.startsWith('data:')) {
|
|
1932
|
+
const parsed = parseImageDataUrl(fileContent);
|
|
1933
|
+
if (parsed) {
|
|
1934
|
+
data = parsed.data;
|
|
1935
|
+
resolvedMime = parsed.mimeType;
|
|
1936
|
+
}
|
|
1937
|
+
else {
|
|
1938
|
+
return undefined;
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
else {
|
|
1942
|
+
data = fileContent.trim().replace(/\s+/g, '');
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
else {
|
|
1946
|
+
data = toBase64(fileContent);
|
|
1947
|
+
}
|
|
1948
|
+
const mimeType = explicitMime || resolvedMime || guessImageMimeType(urlStr) || detectImageMimeFromMagic(fileContent) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1949
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1950
|
+
return { type: 'image', data, mimeType, uri: urlStr };
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
// 5. Direct buffer / content on block: { buffer: Buffer } or { content: Buffer }
|
|
1957
|
+
if (b.buffer && (typeof Buffer !== 'undefined' && Buffer.isBuffer(b.buffer) || b.buffer instanceof Uint8Array)) {
|
|
1958
|
+
const data = typeof Buffer !== 'undefined' && Buffer.isBuffer(b.buffer) ? b.buffer.toString('base64') : toBase64(b.buffer);
|
|
1959
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.buffer) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1960
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1961
|
+
return { type: 'image', data, mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
if (b.content && (typeof Buffer !== 'undefined' && Buffer.isBuffer(b.content) || b.content instanceof Uint8Array)) {
|
|
1965
|
+
const data = typeof Buffer !== 'undefined' && Buffer.isBuffer(b.content) ? b.content.toString('base64') : toBase64(b.content);
|
|
1966
|
+
const mimeType = explicitMime || detectImageMimeFromMagic(b.content) || (isExplicitImageBlock ? 'image/png' : undefined);
|
|
1967
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
1968
|
+
return { type: 'image', data, mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
// 6. Data URL or Remote URL in `url`
|
|
1972
|
+
if (typeof b.url === 'string') {
|
|
1973
|
+
if (b.url.startsWith('data:')) {
|
|
1974
|
+
const parsed = parseImageDataUrl(b.url);
|
|
1975
|
+
if (parsed)
|
|
1976
|
+
return { type: 'image', data: parsed.data, mimeType: explicitMime || parsed.mimeType || detectImageMimeFromMagic(parsed.data) || 'image/png', uri: b.url };
|
|
1977
|
+
return undefined;
|
|
1978
|
+
}
|
|
1979
|
+
if (b.url.startsWith('http://') || b.url.startsWith('https://')) {
|
|
1980
|
+
const mimeType = explicitMime || guessImageMimeType(b.url) || 'image/png';
|
|
1981
|
+
return { type: 'image', data: '', mimeType, uri: b.url };
|
|
1982
|
+
}
|
|
1983
|
+
}
|
|
1984
|
+
// 7. File path in `path`, `filePath`, or `url: file://...`
|
|
1985
|
+
let filePath;
|
|
1986
|
+
if (typeof b.path === 'string' && b.path !== '')
|
|
1987
|
+
filePath = b.path;
|
|
1988
|
+
else if (typeof b.filePath === 'string' && b.filePath !== '')
|
|
1989
|
+
filePath = b.filePath;
|
|
1990
|
+
else if (typeof b.url === 'string' && (b.url.startsWith('file://') || isLocalFilePath(b.url)))
|
|
1991
|
+
filePath = b.url;
|
|
1992
|
+
if (filePath !== undefined) {
|
|
1993
|
+
const fileContent = readImageFileFromContext(filePath, context);
|
|
1994
|
+
if (fileContent !== undefined) {
|
|
1995
|
+
let data;
|
|
1996
|
+
let resolvedMime;
|
|
1997
|
+
if (typeof fileContent === 'string') {
|
|
1998
|
+
if (fileContent.startsWith('data:')) {
|
|
1999
|
+
const parsed = parseImageDataUrl(fileContent);
|
|
2000
|
+
if (parsed) {
|
|
2001
|
+
data = parsed.data;
|
|
2002
|
+
resolvedMime = parsed.mimeType;
|
|
2003
|
+
}
|
|
2004
|
+
else {
|
|
2005
|
+
return undefined;
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
else {
|
|
2009
|
+
data = fileContent.trim().replace(/\s+/g, '');
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
else {
|
|
2013
|
+
data = toBase64(fileContent);
|
|
2014
|
+
}
|
|
2015
|
+
const mimeType = explicitMime ||
|
|
2016
|
+
resolvedMime ||
|
|
2017
|
+
guessImageMimeType(filePath) ||
|
|
2018
|
+
detectImageMimeFromMagic(fileContent) ||
|
|
2019
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2020
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2021
|
+
let uri = typeof b.url === 'string' ? b.url : undefined;
|
|
2022
|
+
if (!uri) {
|
|
2023
|
+
if (filePath.startsWith('file://')) {
|
|
2024
|
+
uri = filePath;
|
|
2025
|
+
}
|
|
2026
|
+
else if (isAbsolutePath(filePath)) {
|
|
2027
|
+
if (filePath.startsWith('//') || filePath.startsWith('\\\\')) {
|
|
2028
|
+
uri = `file://${filePath.replace(/^[\/\\]+/, '').replace(/\\/g, '/')}`;
|
|
2029
|
+
}
|
|
2030
|
+
else if (/^[a-zA-Z]:/.test(filePath)) {
|
|
2031
|
+
uri = `file:///${filePath.replace(/\\/g, '/')}`;
|
|
2032
|
+
}
|
|
2033
|
+
else {
|
|
2034
|
+
uri = `file://${filePath.startsWith('/') ? filePath : `/${filePath}`}`;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
else {
|
|
2038
|
+
uri = filePath;
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
return {
|
|
2042
|
+
type: 'image',
|
|
2043
|
+
data,
|
|
2044
|
+
mimeType,
|
|
2045
|
+
uri,
|
|
2046
|
+
};
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
// 8. Attachment resolution via context
|
|
2051
|
+
let attachmentId;
|
|
2052
|
+
if (attRecord && typeof attRecord.attachmentId === 'string' && attRecord.attachmentId.trim() !== '') {
|
|
2053
|
+
attachmentId = attRecord.attachmentId.trim();
|
|
2054
|
+
}
|
|
2055
|
+
else if (typeof b.attachmentId === 'string' && b.attachmentId.trim() !== '') {
|
|
2056
|
+
attachmentId = b.attachmentId.trim();
|
|
2057
|
+
}
|
|
2058
|
+
else if (attRecord && typeof attRecord.id === 'string' && attRecord.id.trim() !== '') {
|
|
2059
|
+
attachmentId = attRecord.id.trim();
|
|
2060
|
+
}
|
|
2061
|
+
else if (typeof b.id === 'string' && b.id.trim() !== '') {
|
|
2062
|
+
attachmentId = b.id.trim();
|
|
2063
|
+
}
|
|
2064
|
+
else if (typeof b.attachment === 'string' && b.attachment.trim() !== '') {
|
|
2065
|
+
attachmentId = b.attachment.trim();
|
|
2066
|
+
}
|
|
2067
|
+
else if (typeof b.url === 'string' && b.url.startsWith('attachment:')) {
|
|
2068
|
+
attachmentId = b.url.replace(/^attachment:\/\/|^attachment:/, '');
|
|
2069
|
+
}
|
|
2070
|
+
else if (typeof b.image_url === 'string' && b.image_url.startsWith('attachment:')) {
|
|
2071
|
+
attachmentId = b.image_url.replace(/^attachment:\/\/|^attachment:/, '');
|
|
2072
|
+
}
|
|
2073
|
+
else if (b.image_url && typeof b.image_url === 'object' && typeof b.image_url.url === 'string' && b.image_url.url.startsWith('attachment:')) {
|
|
2074
|
+
attachmentId = b.image_url.url.replace(/^attachment:\/\/|^attachment:/, '');
|
|
2075
|
+
}
|
|
2076
|
+
if (attachmentId !== undefined) {
|
|
2077
|
+
const rawAtt = resolveImageAttachmentFromContext(attachmentId, context, b.attachment);
|
|
2078
|
+
if (rawAtt !== undefined) {
|
|
2079
|
+
if (typeof rawAtt === 'string') {
|
|
2080
|
+
if (rawAtt.startsWith('data:')) {
|
|
2081
|
+
const parsed = parseImageDataUrl(rawAtt);
|
|
2082
|
+
if (parsed) {
|
|
2083
|
+
const mimeType = explicitMime || parsed.mimeType || detectImageMimeFromMagic(parsed.data) || 'image/png';
|
|
2084
|
+
return {
|
|
2085
|
+
type: 'image',
|
|
2086
|
+
data: parsed.data,
|
|
2087
|
+
mimeType,
|
|
2088
|
+
uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}`,
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
return undefined;
|
|
2092
|
+
}
|
|
2093
|
+
else if (rawAtt.startsWith('file://') || isLocalFilePath(rawAtt)) {
|
|
2094
|
+
const fileContent = readImageFileFromContext(rawAtt, context);
|
|
2095
|
+
if (fileContent !== undefined) {
|
|
2096
|
+
let data;
|
|
2097
|
+
let resolvedMime;
|
|
2098
|
+
if (typeof fileContent === 'string') {
|
|
2099
|
+
if (fileContent.startsWith('data:')) {
|
|
2100
|
+
const parsed = parseImageDataUrl(fileContent);
|
|
2101
|
+
if (parsed) {
|
|
2102
|
+
data = parsed.data;
|
|
2103
|
+
resolvedMime = parsed.mimeType;
|
|
2104
|
+
}
|
|
2105
|
+
else {
|
|
2106
|
+
return undefined;
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
else {
|
|
2110
|
+
data = fileContent.trim().replace(/\s+/g, '');
|
|
2111
|
+
}
|
|
2112
|
+
}
|
|
2113
|
+
else {
|
|
2114
|
+
data = toBase64(fileContent);
|
|
2115
|
+
}
|
|
2116
|
+
const mimeType = explicitMime ||
|
|
2117
|
+
resolvedMime ||
|
|
2118
|
+
(typeof attRecord?.name === 'string' ? guessImageMimeType(attRecord.name) : undefined) ||
|
|
2119
|
+
guessImageMimeType(rawAtt) ||
|
|
2120
|
+
detectImageMimeFromMagic(fileContent) ||
|
|
2121
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2122
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2123
|
+
return { type: 'image', data, mimeType, uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}` };
|
|
2124
|
+
}
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
const mimeType = explicitMime ||
|
|
2128
|
+
(typeof attRecord?.name === 'string' ? guessImageMimeType(attRecord.name) : undefined) ||
|
|
2129
|
+
guessImageMimeType(attachmentId) ||
|
|
2130
|
+
detectImageMimeFromMagic(rawAtt) ||
|
|
2131
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2132
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2133
|
+
return { type: 'image', data: rawAtt.trim().replace(/\s+/g, ''), mimeType, uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}` };
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
if (typeof rawAtt === 'object' && rawAtt !== null) {
|
|
2137
|
+
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(rawAtt)) {
|
|
2138
|
+
const mimeType = explicitMime ||
|
|
2139
|
+
(typeof attRecord?.name === 'string' ? guessImageMimeType(attRecord.name) : undefined) ||
|
|
2140
|
+
guessImageMimeType(attachmentId) ||
|
|
2141
|
+
detectImageMimeFromMagic(rawAtt) ||
|
|
2142
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2143
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2144
|
+
return {
|
|
2145
|
+
type: 'image',
|
|
2146
|
+
data: rawAtt.toString('base64'),
|
|
2147
|
+
mimeType,
|
|
2148
|
+
uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}`,
|
|
2149
|
+
};
|
|
2150
|
+
}
|
|
2151
|
+
}
|
|
2152
|
+
if (rawAtt instanceof Uint8Array) {
|
|
2153
|
+
const mimeType = explicitMime ||
|
|
2154
|
+
(typeof attRecord?.name === 'string' ? guessImageMimeType(attRecord.name) : undefined) ||
|
|
2155
|
+
guessImageMimeType(attachmentId) ||
|
|
2156
|
+
detectImageMimeFromMagic(rawAtt) ||
|
|
2157
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2158
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2159
|
+
return {
|
|
2160
|
+
type: 'image',
|
|
2161
|
+
data: toBase64(rawAtt),
|
|
2162
|
+
mimeType,
|
|
2163
|
+
uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}`,
|
|
2164
|
+
};
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
const attObj = rawAtt;
|
|
2168
|
+
let data;
|
|
2169
|
+
let resolvedMime;
|
|
2170
|
+
const attPath = typeof attObj.path === 'string' && attObj.path !== ''
|
|
2171
|
+
? attObj.path
|
|
2172
|
+
: (typeof attObj.filePath === 'string' && attObj.filePath !== ''
|
|
2173
|
+
? attObj.filePath
|
|
2174
|
+
: (typeof attObj.url === 'string' && (attObj.url.startsWith('file://') || isLocalFilePath(attObj.url)) ? attObj.url : undefined));
|
|
2175
|
+
let fileContentForMagic;
|
|
2176
|
+
if (attPath !== undefined) {
|
|
2177
|
+
const fileContent = readImageFileFromContext(attPath, context);
|
|
2178
|
+
if (fileContent !== undefined) {
|
|
2179
|
+
fileContentForMagic = fileContent;
|
|
2180
|
+
if (typeof fileContent === 'string') {
|
|
2181
|
+
if (fileContent.startsWith('data:')) {
|
|
2182
|
+
const parsed = parseImageDataUrl(fileContent);
|
|
2183
|
+
if (parsed) {
|
|
2184
|
+
data = parsed.data;
|
|
2185
|
+
resolvedMime = parsed.mimeType;
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
else {
|
|
2189
|
+
data = fileContent.trim().replace(/\s+/g, '');
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
else {
|
|
2193
|
+
data = toBase64(fileContent);
|
|
2194
|
+
}
|
|
2195
|
+
if (!resolvedMime)
|
|
2196
|
+
resolvedMime = guessImageMimeType(attPath);
|
|
2197
|
+
}
|
|
2198
|
+
}
|
|
2199
|
+
if (data === undefined) {
|
|
2200
|
+
if (typeof attObj.data === 'string' && attObj.data.trim() !== '') {
|
|
2201
|
+
if (attObj.data.startsWith('data:')) {
|
|
2202
|
+
const parsed = parseImageDataUrl(attObj.data);
|
|
2203
|
+
if (parsed) {
|
|
2204
|
+
data = parsed.data;
|
|
2205
|
+
resolvedMime = parsed.mimeType;
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
else {
|
|
2209
|
+
data = attObj.data.trim().replace(/\s+/g, '');
|
|
2210
|
+
fileContentForMagic = attObj.data;
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.data)) {
|
|
2214
|
+
data = attObj.data.toString('base64');
|
|
2215
|
+
fileContentForMagic = attObj.data;
|
|
2216
|
+
}
|
|
2217
|
+
else if (attObj.data instanceof Uint8Array) {
|
|
2218
|
+
data = toBase64(attObj.data);
|
|
2219
|
+
fileContentForMagic = attObj.data;
|
|
2220
|
+
}
|
|
2221
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.buffer)) {
|
|
2222
|
+
data = attObj.buffer.toString('base64');
|
|
2223
|
+
fileContentForMagic = attObj.buffer;
|
|
2224
|
+
}
|
|
2225
|
+
else if (attObj.buffer instanceof Uint8Array) {
|
|
2226
|
+
data = toBase64(attObj.buffer);
|
|
2227
|
+
fileContentForMagic = attObj.buffer;
|
|
2228
|
+
}
|
|
2229
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.content)) {
|
|
2230
|
+
data = attObj.content.toString('base64');
|
|
2231
|
+
fileContentForMagic = attObj.content;
|
|
2232
|
+
}
|
|
2233
|
+
else if (attObj.content instanceof Uint8Array) {
|
|
2234
|
+
data = toBase64(attObj.content);
|
|
2235
|
+
fileContentForMagic = attObj.content;
|
|
2236
|
+
}
|
|
2237
|
+
else if (typeof attObj.content === 'string') {
|
|
2238
|
+
if (attObj.content.startsWith('data:')) {
|
|
2239
|
+
const parsed = parseImageDataUrl(attObj.content);
|
|
2240
|
+
if (parsed) {
|
|
2241
|
+
data = parsed.data;
|
|
2242
|
+
resolvedMime = parsed.mimeType;
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
else {
|
|
2246
|
+
data = attObj.content.trim().replace(/\s+/g, '');
|
|
2247
|
+
fileContentForMagic = attObj.content;
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
if (data !== undefined) {
|
|
2252
|
+
const mimeType = explicitMime ||
|
|
2253
|
+
(typeof attObj.mediaType === 'string' && attObj.mediaType.toLowerCase()) ||
|
|
2254
|
+
(typeof attObj.mimeType === 'string' && attObj.mimeType.toLowerCase()) ||
|
|
2255
|
+
resolvedMime ||
|
|
2256
|
+
(typeof attRecord?.name === 'string' ? guessImageMimeType(attRecord.name) : undefined) ||
|
|
2257
|
+
(typeof attObj.name === 'string' ? guessImageMimeType(attObj.name) : undefined) ||
|
|
2258
|
+
guessImageMimeType(attachmentId) ||
|
|
2259
|
+
detectImageMimeFromMagic(fileContentForMagic ?? data) ||
|
|
2260
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2261
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2262
|
+
return {
|
|
2263
|
+
type: 'image',
|
|
2264
|
+
data,
|
|
2265
|
+
mimeType,
|
|
2266
|
+
uri: typeof b.uri === 'string' ? b.uri : `attachment://${attachmentId}`,
|
|
2267
|
+
};
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
}
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
// 9. Direct b.attachment object
|
|
2274
|
+
if (b.attachment && typeof b.attachment === 'object') {
|
|
2275
|
+
const attObj = b.attachment;
|
|
2276
|
+
let data;
|
|
2277
|
+
let resolvedMime;
|
|
2278
|
+
const attPath = typeof attObj.path === 'string' && attObj.path !== ''
|
|
2279
|
+
? attObj.path
|
|
2280
|
+
: (typeof attObj.filePath === 'string' && attObj.filePath !== ''
|
|
2281
|
+
? attObj.filePath
|
|
2282
|
+
: (typeof attObj.url === 'string' && (attObj.url.startsWith('file://') || isLocalFilePath(attObj.url)) ? attObj.url : undefined));
|
|
2283
|
+
let fileContentForMagic;
|
|
2284
|
+
if (attPath !== undefined) {
|
|
2285
|
+
const fileContent = readImageFileFromContext(attPath, context);
|
|
2286
|
+
if (fileContent !== undefined) {
|
|
2287
|
+
fileContentForMagic = fileContent;
|
|
2288
|
+
if (typeof fileContent === 'string') {
|
|
2289
|
+
if (fileContent.startsWith('data:')) {
|
|
2290
|
+
const parsed = parseImageDataUrl(fileContent);
|
|
2291
|
+
if (parsed) {
|
|
2292
|
+
data = parsed.data;
|
|
2293
|
+
resolvedMime = parsed.mimeType;
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
else {
|
|
2297
|
+
data = fileContent.trim().replace(/\s+/g, '');
|
|
2298
|
+
}
|
|
2299
|
+
}
|
|
2300
|
+
else {
|
|
2301
|
+
data = toBase64(fileContent);
|
|
2302
|
+
}
|
|
2303
|
+
if (!resolvedMime)
|
|
2304
|
+
resolvedMime = guessImageMimeType(attPath);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
if (data === undefined) {
|
|
2308
|
+
if (typeof attObj.data === 'string' && attObj.data.trim() !== '') {
|
|
2309
|
+
if (attObj.data.startsWith('data:')) {
|
|
2310
|
+
const parsed = parseImageDataUrl(attObj.data);
|
|
2311
|
+
if (parsed) {
|
|
2312
|
+
data = parsed.data;
|
|
2313
|
+
resolvedMime = parsed.mimeType;
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
else {
|
|
2317
|
+
data = attObj.data.trim().replace(/\s+/g, '');
|
|
2318
|
+
fileContentForMagic = attObj.data;
|
|
2319
|
+
}
|
|
2320
|
+
}
|
|
2321
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.data)) {
|
|
2322
|
+
data = attObj.data.toString('base64');
|
|
2323
|
+
fileContentForMagic = attObj.data;
|
|
2324
|
+
}
|
|
2325
|
+
else if (attObj.data instanceof Uint8Array) {
|
|
2326
|
+
data = toBase64(attObj.data);
|
|
2327
|
+
fileContentForMagic = attObj.data;
|
|
2328
|
+
}
|
|
2329
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.buffer)) {
|
|
2330
|
+
data = attObj.buffer.toString('base64');
|
|
2331
|
+
fileContentForMagic = attObj.buffer;
|
|
2332
|
+
}
|
|
2333
|
+
else if (attObj.buffer instanceof Uint8Array) {
|
|
2334
|
+
data = toBase64(attObj.buffer);
|
|
2335
|
+
fileContentForMagic = attObj.buffer;
|
|
2336
|
+
}
|
|
2337
|
+
else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(attObj.content)) {
|
|
2338
|
+
data = attObj.content.toString('base64');
|
|
2339
|
+
fileContentForMagic = attObj.content;
|
|
2340
|
+
}
|
|
2341
|
+
else if (attObj.content instanceof Uint8Array) {
|
|
2342
|
+
data = toBase64(attObj.content);
|
|
2343
|
+
fileContentForMagic = attObj.content;
|
|
2344
|
+
}
|
|
2345
|
+
else if (typeof attObj.content === 'string') {
|
|
2346
|
+
if (attObj.content.startsWith('data:')) {
|
|
2347
|
+
const parsed = parseImageDataUrl(attObj.content);
|
|
2348
|
+
if (parsed) {
|
|
2349
|
+
data = parsed.data;
|
|
2350
|
+
resolvedMime = parsed.mimeType;
|
|
2351
|
+
}
|
|
2352
|
+
}
|
|
2353
|
+
else {
|
|
2354
|
+
data = attObj.content.trim().replace(/\s+/g, '');
|
|
2355
|
+
fileContentForMagic = attObj.content;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
if (data !== undefined) {
|
|
2360
|
+
const mimeType = explicitMime ||
|
|
2361
|
+
(typeof attObj.mediaType === 'string' && attObj.mediaType.toLowerCase()) ||
|
|
2362
|
+
(typeof attObj.mimeType === 'string' && attObj.mimeType.toLowerCase()) ||
|
|
2363
|
+
resolvedMime ||
|
|
2364
|
+
(typeof attObj.name === 'string' ? guessImageMimeType(attObj.name) : undefined) ||
|
|
2365
|
+
(attPath ? guessImageMimeType(attPath) : undefined) ||
|
|
2366
|
+
detectImageMimeFromMagic(fileContentForMagic ?? data) ||
|
|
2367
|
+
(isExplicitImageBlock ? 'image/png' : undefined);
|
|
2368
|
+
if (mimeType && mimeType.startsWith('image/')) {
|
|
2369
|
+
return { type: 'image', data, mimeType, ...(typeof b.uri === 'string' ? { uri: b.uri } : {}) };
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
return undefined;
|
|
2374
|
+
}
|
|
1290
2375
|
/**
|
|
1291
2376
|
* Select the user's own words out of a host-built request.
|
|
1292
2377
|
*
|
|
@@ -1300,9 +2385,10 @@ export function describeUnhandledUpdate(kind, update) {
|
|
|
1300
2385
|
* (tests, headless callers) and counts as user input.
|
|
1301
2386
|
*
|
|
1302
2387
|
* @param messages - the request's ordered messages, exactly as the host built them.
|
|
1303
|
-
* @
|
|
2388
|
+
* @param context - optional host context or attachment resolver for reading attachments.
|
|
2389
|
+
* @returns the last real user message's text and any image content blocks, or why there is none to forward.
|
|
1304
2390
|
*/
|
|
1305
|
-
export function selectForwardPrompt(messages) {
|
|
2391
|
+
export function selectForwardPrompt(messages, context) {
|
|
1306
2392
|
let sawUserRole = false;
|
|
1307
2393
|
let last;
|
|
1308
2394
|
for (const message of messages) {
|
|
@@ -1312,14 +2398,43 @@ export function selectForwardPrompt(messages) {
|
|
|
1312
2398
|
const kind = message.source?.kind;
|
|
1313
2399
|
if (kind !== undefined && kind !== 'user')
|
|
1314
2400
|
continue;
|
|
1315
|
-
const
|
|
2401
|
+
const content = message.content ?? message.parts;
|
|
2402
|
+
const rawBlocks = Array.isArray(content)
|
|
2403
|
+
? content
|
|
2404
|
+
: (typeof content === 'string'
|
|
2405
|
+
? [{ type: 'text', text: content }]
|
|
2406
|
+
: (typeof content === 'object' && content !== null ? [content] : []));
|
|
2407
|
+
const text = rawBlocks
|
|
1316
2408
|
.filter(block => block.type === 'text' && typeof block.text === 'string')
|
|
1317
2409
|
.map(block => block.text)
|
|
1318
2410
|
.join('\n')
|
|
1319
2411
|
.trim();
|
|
1320
|
-
|
|
2412
|
+
const images = [];
|
|
2413
|
+
const contentBlocks = [];
|
|
2414
|
+
for (const block of rawBlocks) {
|
|
2415
|
+
if (!block || typeof block !== 'object')
|
|
2416
|
+
continue;
|
|
2417
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
2418
|
+
if (block.text.trim() !== '') {
|
|
2419
|
+
contentBlocks.push({ type: 'text', text: block.text });
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
else {
|
|
2423
|
+
const resolved = resolveImageContent(block, context);
|
|
2424
|
+
if (resolved !== undefined) {
|
|
2425
|
+
images.push(resolved);
|
|
2426
|
+
contentBlocks.push(resolved);
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
}
|
|
2430
|
+
if (text === '' && images.length === 0)
|
|
1321
2431
|
continue;
|
|
1322
|
-
|
|
2432
|
+
const item = { text, ...(message.id === undefined ? {} : { id: message.id }) };
|
|
2433
|
+
if (images.length > 0) {
|
|
2434
|
+
item.images = images;
|
|
2435
|
+
item.content = contentBlocks;
|
|
2436
|
+
}
|
|
2437
|
+
last = item;
|
|
1323
2438
|
}
|
|
1324
2439
|
if (last !== undefined)
|
|
1325
2440
|
return { kind: 'prompt', ...last };
|
|
@@ -1446,7 +2561,36 @@ export function acpPresetStatusFromRoster(rows, id) {
|
|
|
1446
2561
|
: { id, source: ACP_PRESET_SOURCE, installed: false, problem: broken };
|
|
1447
2562
|
}
|
|
1448
2563
|
// ---- Transcript ----
|
|
1449
|
-
/**
|
|
2564
|
+
/**
|
|
2565
|
+
* The kind of a state-only tick that consecutive repeats may share one entry.
|
|
2566
|
+
*
|
|
2567
|
+
* These are the frames the client folds to nothing (`STATE_ONLY_UPDATES`):
|
|
2568
|
+
* only their latest value is ever read. `session_info_update` qualifies only
|
|
2569
|
+
* while it carries no title — a title is content the replay must keep, so it
|
|
2570
|
+
* never lets a later tick replace its entry.
|
|
2571
|
+
*/
|
|
2572
|
+
function coalescibleStateKind(update) {
|
|
2573
|
+
if (typeof update !== 'object' || update === null || Array.isArray(update))
|
|
2574
|
+
return undefined;
|
|
2575
|
+
const row = update;
|
|
2576
|
+
const kind = row.sessionUpdate;
|
|
2577
|
+
if (kind === 'usage_update' || kind === 'config_option_update')
|
|
2578
|
+
return kind;
|
|
2579
|
+
if (kind !== 'session_info_update')
|
|
2580
|
+
return undefined;
|
|
2581
|
+
const title = row.title;
|
|
2582
|
+
return title === undefined || title === null || title === '' ? kind : undefined;
|
|
2583
|
+
}
|
|
2584
|
+
/**
|
|
2585
|
+
* Bounded log of raw ACP session updates with monotone seq.
|
|
2586
|
+
*
|
|
2587
|
+
* Consecutive ticks of the same state-only kind share one entry (the entry is
|
|
2588
|
+
* replaced in place, not appended): chatty agents emit a usage / phase tick
|
|
2589
|
+
* every few hundred milliseconds, and letting each occupy the ring evicts the
|
|
2590
|
+
* conversation itself. This costs nothing on the live path — `append` still
|
|
2591
|
+
* allocates a fresh seq and the bridge broadcasts that frame — but replay via
|
|
2592
|
+
* `since` only sees the newest tick of each run.
|
|
2593
|
+
*/
|
|
1450
2594
|
export class Transcript {
|
|
1451
2595
|
max;
|
|
1452
2596
|
entries = [];
|
|
@@ -1456,6 +2600,16 @@ export class Transcript {
|
|
|
1456
2600
|
}
|
|
1457
2601
|
append(update) {
|
|
1458
2602
|
const seq = this.next++;
|
|
2603
|
+
const last = this.entries[this.entries.length - 1];
|
|
2604
|
+
const kind = coalescibleStateKind(update);
|
|
2605
|
+
// Replace, don't push: `since(afterSeq)` keeps its filter, so intermediate
|
|
2606
|
+
// seq values are simply absent from replayed entries. Live SSE frames are
|
|
2607
|
+
// broadcast per append (the returned seq), and the client's live fold does
|
|
2608
|
+
// not depend on transcript contiguity — only `head` must keep advancing.
|
|
2609
|
+
if (kind !== undefined && last !== undefined && coalescibleStateKind(last.update) === kind) {
|
|
2610
|
+
this.entries[this.entries.length - 1] = { seq, update };
|
|
2611
|
+
return seq;
|
|
2612
|
+
}
|
|
1459
2613
|
this.entries.push({ seq, update });
|
|
1460
2614
|
if (this.entries.length > this.max)
|
|
1461
2615
|
this.entries.splice(0, this.entries.length - this.max);
|
|
@@ -1650,7 +2804,44 @@ export async function dispatchRpc(manager, endpoint, payload) {
|
|
|
1650
2804
|
case 'session/start':
|
|
1651
2805
|
return ok(await manager.startSession(str(body, 'agentId', 64), optionalStr(body, 'cwd', 4096)));
|
|
1652
2806
|
case 'session/prompt': {
|
|
1653
|
-
|
|
2807
|
+
let promptInput;
|
|
2808
|
+
if (Array.isArray(body.prompt)) {
|
|
2809
|
+
if (body.prompt.length === 0) {
|
|
2810
|
+
throw new AcpError('bad_payload', 'acp: prompt array cannot be empty');
|
|
2811
|
+
}
|
|
2812
|
+
for (const b of body.prompt) {
|
|
2813
|
+
if (!b || typeof b !== 'object')
|
|
2814
|
+
throw new AcpError('bad_payload', 'acp: prompt blocks must be objects');
|
|
2815
|
+
const block = b;
|
|
2816
|
+
if (block.type === 'text') {
|
|
2817
|
+
if (typeof block.text !== 'string')
|
|
2818
|
+
throw new AcpError('bad_payload', 'acp: text block requires string text');
|
|
2819
|
+
if (block.text.length > (1 << 20))
|
|
2820
|
+
throw new AcpError('bad_payload', 'acp: text block exceeds max length');
|
|
2821
|
+
}
|
|
2822
|
+
else if (block.type === 'image') {
|
|
2823
|
+
if (typeof block.data !== 'string' || typeof block.mimeType !== 'string') {
|
|
2824
|
+
throw new AcpError('bad_payload', 'acp: image block requires string data and mimeType');
|
|
2825
|
+
}
|
|
2826
|
+
if (!block.mimeType.toLowerCase().startsWith('image/')) {
|
|
2827
|
+
throw new AcpError('bad_payload', 'acp: image block mimeType must start with image/');
|
|
2828
|
+
}
|
|
2829
|
+
if (block.uri !== undefined && typeof block.uri !== 'string') {
|
|
2830
|
+
throw new AcpError('bad_payload', 'acp: image block uri must be string');
|
|
2831
|
+
}
|
|
2832
|
+
if (block.data.length > (20 << 20))
|
|
2833
|
+
throw new AcpError('bad_payload', 'acp: image block exceeds max data length');
|
|
2834
|
+
}
|
|
2835
|
+
else {
|
|
2836
|
+
throw new AcpError('bad_payload', `acp: unsupported prompt block type: ${String(block.type)}`);
|
|
2837
|
+
}
|
|
2838
|
+
}
|
|
2839
|
+
promptInput = body.prompt;
|
|
2840
|
+
}
|
|
2841
|
+
else {
|
|
2842
|
+
promptInput = str(body, 'text', 1 << 20);
|
|
2843
|
+
}
|
|
2844
|
+
const { turnId } = await manager.prompt(str(body, 'sessionId', 64), promptInput);
|
|
1654
2845
|
return ok({ accepted: true, turnId });
|
|
1655
2846
|
}
|
|
1656
2847
|
case 'session/cancel':
|
|
@@ -1733,6 +2924,26 @@ export async function dispatchRpc(manager, endpoint, payload) {
|
|
|
1733
2924
|
await manager.setChatSessionMode?.(dshSessionId, modeId, optionalStr(body, 'agentId', 64));
|
|
1734
2925
|
return ok({ done: true });
|
|
1735
2926
|
}
|
|
2927
|
+
case 'session/chat-resume': {
|
|
2928
|
+
// The released-session banner's explicit resume: the recovery the next
|
|
2929
|
+
// prompt performs lazily, on the user's gesture instead. Addressed by
|
|
2930
|
+
// DSH chat session id, like the rest of the conversation surfaces.
|
|
2931
|
+
const dshSessionId = str(body, 'dshSessionId', 128);
|
|
2932
|
+
await manager.resumeChatSession?.(dshSessionId);
|
|
2933
|
+
return ok({ done: true });
|
|
2934
|
+
}
|
|
2935
|
+
case 'session/chat-fork-adopt': {
|
|
2936
|
+
// The second half of a branched conversation: bind the DSH session the
|
|
2937
|
+
// host's `sessions.fork` just created to the agent's own `session/fork`
|
|
2938
|
+
// of the parent's live ACP session. Both ids are validated before the
|
|
2939
|
+
// optional call — the same shape rule as `session/chat-list` above.
|
|
2940
|
+
// The adopted ids ride the answer (the protocol's ChatForkAdoptResult);
|
|
2941
|
+
// a manager without the method keeps the plain `done` shape.
|
|
2942
|
+
const dshSessionId = str(body, 'dshSessionId', 128);
|
|
2943
|
+
const parentDshSessionId = str(body, 'parentDshSessionId', 128);
|
|
2944
|
+
const adopted = await manager.adoptForkedSession?.(dshSessionId, parentDshSessionId);
|
|
2945
|
+
return ok(adopted ?? { done: true });
|
|
2946
|
+
}
|
|
1736
2947
|
case 'session/chat-bind': {
|
|
1737
2948
|
// Point one chat conversation at an ACP agent. The plugin's own
|
|
1738
2949
|
// conversation↔agent mapping — NOT a DSH model route (see
|
|
@@ -1820,6 +3031,16 @@ export async function dispatchRpc(manager, endpoint, payload) {
|
|
|
1820
3031
|
await manager.setApprovalDeadlines?.(patch, body.reset === true);
|
|
1821
3032
|
return ok({ deadlines: manager.approvalDeadlines?.() });
|
|
1822
3033
|
}
|
|
3034
|
+
case 'ui/idle-release':
|
|
3035
|
+
// Absent on a host half from before the tab existed: the browser then
|
|
3036
|
+
// renders nothing rather than a number it cannot trust.
|
|
3037
|
+
return ok({ idleRelease: manager.idleRelease?.() });
|
|
3038
|
+
case 'ui/idle-release-set': {
|
|
3039
|
+
const reset = body.reset === true;
|
|
3040
|
+
const idleTimeoutMs = reset ? undefined : validateIdleReleaseMs(body.idleTimeoutMs, 'bad_payload');
|
|
3041
|
+
await manager.setIdleRelease?.(idleTimeoutMs, reset);
|
|
3042
|
+
return ok({ idleRelease: manager.idleRelease?.() });
|
|
3043
|
+
}
|
|
1823
3044
|
case 'agent/set-exposed-models': {
|
|
1824
3045
|
const agentId = str(body, 'agentId', 64);
|
|
1825
3046
|
const raw = body.exposedModels;
|