@anuma/sdk 7.0.0 → 7.0.2
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/expo/index.cjs +421 -111
- package/dist/expo/index.d.mts +25 -24
- package/dist/expo/index.d.ts +25 -24
- package/dist/expo/index.mjs +420 -111
- package/dist/index.cjs +216 -192
- package/dist/index.mjs +216 -192
- package/dist/memory/context.d.mts +8 -0
- package/dist/memory/context.d.ts +8 -0
- package/dist/react/index.cjs +442 -132
- package/dist/react/index.d.mts +30 -44
- package/dist/react/index.d.ts +30 -44
- package/dist/react/index.mjs +441 -132
- package/dist/server/index.cjs +431 -106
- package/dist/server/index.d.mts +30 -21
- package/dist/server/index.d.ts +30 -21
- package/dist/server/index.mjs +430 -106
- package/dist/telemetry/index.d.mts +6 -0
- package/dist/telemetry/index.d.ts +6 -0
- package/dist/tools/selection/index.cjs +28 -10
- package/dist/tools/selection/index.mjs +28 -10
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1349,6 +1349,192 @@ function attachFileContextToLastUserMessage(messages, fileContext) {
|
|
|
1349
1349
|
return result;
|
|
1350
1350
|
}
|
|
1351
1351
|
|
|
1352
|
+
// src/lib/logger.ts
|
|
1353
|
+
var consoleLogger = {
|
|
1354
|
+
debug: console.log.bind(console),
|
|
1355
|
+
info: console.info.bind(console),
|
|
1356
|
+
warn: console.warn.bind(console),
|
|
1357
|
+
error: console.error.bind(console)
|
|
1358
|
+
};
|
|
1359
|
+
var noopLogger = {
|
|
1360
|
+
debug: () => {
|
|
1361
|
+
},
|
|
1362
|
+
info: () => {
|
|
1363
|
+
},
|
|
1364
|
+
warn: () => {
|
|
1365
|
+
},
|
|
1366
|
+
error: () => {
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1369
|
+
var LOGGER_STATE_KEY = /* @__PURE__ */ Symbol.for("@anuma/sdk/logger/v1");
|
|
1370
|
+
var loggerGlobal = globalThis;
|
|
1371
|
+
var fallbackLoggerState = { logger: consoleLogger };
|
|
1372
|
+
var fallbackWarned = false;
|
|
1373
|
+
function getLoggerState() {
|
|
1374
|
+
try {
|
|
1375
|
+
return loggerGlobal[LOGGER_STATE_KEY] ?? (loggerGlobal[LOGGER_STATE_KEY] = fallbackLoggerState);
|
|
1376
|
+
} catch {
|
|
1377
|
+
if (!fallbackWarned) {
|
|
1378
|
+
fallbackWarned = true;
|
|
1379
|
+
fallbackLoggerState.logger.warn(
|
|
1380
|
+
"@anuma/sdk: the global object rejects new properties. This entrypoint keeps its own logger, so setLogger does not reach other entrypoints."
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
return fallbackLoggerState;
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
function setLogger(logger) {
|
|
1387
|
+
getLoggerState().logger = logger;
|
|
1388
|
+
}
|
|
1389
|
+
function getLogger() {
|
|
1390
|
+
return getLoggerState().logger;
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
// src/react/useEncryption.ts
|
|
1394
|
+
var EncryptionKeyMissingError = class extends Error {
|
|
1395
|
+
constructor(address, version) {
|
|
1396
|
+
super(
|
|
1397
|
+
`Encryption key (${version}) not found for ${address}. Please sign a message to generate your encryption key.`
|
|
1398
|
+
);
|
|
1399
|
+
this.name = "EncryptionKeyMissingError";
|
|
1400
|
+
this.address = address;
|
|
1401
|
+
this.version = version;
|
|
1402
|
+
}
|
|
1403
|
+
};
|
|
1404
|
+
var encryptionKeyStore = /* @__PURE__ */ new Map();
|
|
1405
|
+
var cryptoKeyCache = /* @__PURE__ */ new Map();
|
|
1406
|
+
var clearAllEncryptionStateCallbacks = /* @__PURE__ */ new Set();
|
|
1407
|
+
function onClearAllEncryptionState(callback) {
|
|
1408
|
+
clearAllEncryptionStateCallbacks.add(callback);
|
|
1409
|
+
return () => {
|
|
1410
|
+
clearAllEncryptionStateCallbacks.delete(callback);
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
function getStoredKeyByVersion(address, version) {
|
|
1414
|
+
const keys = encryptionKeyStore.get(address);
|
|
1415
|
+
if (!keys) return null;
|
|
1416
|
+
const value = version === "v2" ? keys.legacy : keys.current;
|
|
1417
|
+
return value ?? null;
|
|
1418
|
+
}
|
|
1419
|
+
function hexNibble(code) {
|
|
1420
|
+
if (code >= 48 && code <= 57) return code - 48;
|
|
1421
|
+
if (code >= 65 && code <= 70) return code - 55;
|
|
1422
|
+
if (code >= 97 && code <= 102) return code - 87;
|
|
1423
|
+
return -1;
|
|
1424
|
+
}
|
|
1425
|
+
function hexToBytes(hex) {
|
|
1426
|
+
if (typeof hex !== "string") {
|
|
1427
|
+
throw new TypeError(
|
|
1428
|
+
"hexToBytes: expected a hex string. Pass raw signature bytes to deriveKeyFromSignatureBytes instead of stringifying them."
|
|
1429
|
+
);
|
|
1430
|
+
}
|
|
1431
|
+
const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
1432
|
+
if (cleanHex.length === 0 || cleanHex.length % 2 !== 0) {
|
|
1433
|
+
throw new Error(
|
|
1434
|
+
`hexToBytes: invalid hex string (expected a non-empty even length, got ${cleanHex.length})`
|
|
1435
|
+
);
|
|
1436
|
+
}
|
|
1437
|
+
const bytes = new Uint8Array(cleanHex.length / 2);
|
|
1438
|
+
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
1439
|
+
const hi = hexNibble(cleanHex.charCodeAt(i));
|
|
1440
|
+
const lo = hexNibble(cleanHex.charCodeAt(i + 1));
|
|
1441
|
+
if (hi < 0 || lo < 0) {
|
|
1442
|
+
throw new Error(`hexToBytes: invalid hex string (non-hex character at index ${i})`);
|
|
1443
|
+
}
|
|
1444
|
+
bytes[i / 2] = hi << 4 | lo;
|
|
1445
|
+
}
|
|
1446
|
+
return bytes;
|
|
1447
|
+
}
|
|
1448
|
+
var SHARED_TEXT_ENCODER = new TextEncoder();
|
|
1449
|
+
var SHARED_TEXT_DECODER = new TextDecoder();
|
|
1450
|
+
async function getEncryptionKey(address, version = "v3") {
|
|
1451
|
+
const cacheKey = `${address}:${version}`;
|
|
1452
|
+
const cachedKey = cryptoKeyCache.get(cacheKey);
|
|
1453
|
+
if (cachedKey) {
|
|
1454
|
+
return cachedKey;
|
|
1455
|
+
}
|
|
1456
|
+
const keyHex = getStoredKeyByVersion(address, version);
|
|
1457
|
+
if (!keyHex) {
|
|
1458
|
+
throw new EncryptionKeyMissingError(address, version);
|
|
1459
|
+
}
|
|
1460
|
+
const keyBytes = hexToBytes(keyHex);
|
|
1461
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
1462
|
+
"raw",
|
|
1463
|
+
keyBytes.buffer,
|
|
1464
|
+
{ name: "AES-GCM" },
|
|
1465
|
+
false,
|
|
1466
|
+
["encrypt", "decrypt"]
|
|
1467
|
+
);
|
|
1468
|
+
cryptoKeyCache.set(cacheKey, cryptoKey);
|
|
1469
|
+
return cryptoKey;
|
|
1470
|
+
}
|
|
1471
|
+
async function decryptData(encryptedHex, address, version = "v3") {
|
|
1472
|
+
const key = await getEncryptionKey(address, version);
|
|
1473
|
+
return decryptDataWithKey(encryptedHex, key);
|
|
1474
|
+
}
|
|
1475
|
+
function hasEncryptionKey(address, version) {
|
|
1476
|
+
return getStoredKeyByVersion(address, version ?? "v3") !== null;
|
|
1477
|
+
}
|
|
1478
|
+
async function decryptDataWithKey(encryptedHex, key) {
|
|
1479
|
+
const combined = hexToBytes(encryptedHex);
|
|
1480
|
+
const iv = combined.subarray(0, 12);
|
|
1481
|
+
const encryptedData = combined.subarray(12);
|
|
1482
|
+
const decryptedData = await crypto.subtle.decrypt(
|
|
1483
|
+
{
|
|
1484
|
+
name: "AES-GCM",
|
|
1485
|
+
iv
|
|
1486
|
+
},
|
|
1487
|
+
key,
|
|
1488
|
+
encryptedData
|
|
1489
|
+
);
|
|
1490
|
+
return SHARED_TEXT_DECODER.decode(decryptedData);
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
// src/lib/db/encryption-utils.ts
|
|
1494
|
+
function isEncrypted(value) {
|
|
1495
|
+
const prefix = value.startsWith("enc:v3:") ? "enc:v3:" : value.startsWith("enc:v2:") ? "enc:v2:" : null;
|
|
1496
|
+
if (!prefix) return false;
|
|
1497
|
+
const payload = value.slice(prefix.length);
|
|
1498
|
+
return payload.length >= 56 && /^[0-9a-f]+$/i.test(payload);
|
|
1499
|
+
}
|
|
1500
|
+
function hasEncryptedToolPayload(event) {
|
|
1501
|
+
return [event.arguments, event.output].some(
|
|
1502
|
+
(value) => typeof value === "string" && isEncrypted(value)
|
|
1503
|
+
);
|
|
1504
|
+
}
|
|
1505
|
+
function detectEncryptionVersion(value) {
|
|
1506
|
+
if (value.startsWith("enc:v3:")) {
|
|
1507
|
+
return { version: "v3", encryptedData: value.slice("enc:v3:".length) };
|
|
1508
|
+
}
|
|
1509
|
+
if (value.startsWith("enc:v2:")) {
|
|
1510
|
+
return { version: "v2", encryptedData: value.slice("enc:v2:".length) };
|
|
1511
|
+
}
|
|
1512
|
+
return null;
|
|
1513
|
+
}
|
|
1514
|
+
async function decryptFieldDetailed(value, address) {
|
|
1515
|
+
if (!value) return { status: "plaintext", value };
|
|
1516
|
+
const detected = detectEncryptionVersion(value);
|
|
1517
|
+
if (!detected) return { status: "plaintext", value };
|
|
1518
|
+
if (detected.encryptedData.length < 56 || !/^[0-9a-f]+$/i.test(detected.encryptedData)) {
|
|
1519
|
+
return { status: "invalid_payload", value, version: detected.version };
|
|
1520
|
+
}
|
|
1521
|
+
try {
|
|
1522
|
+
const plaintext = await decryptData(detected.encryptedData, address, detected.version);
|
|
1523
|
+
return { status: "ok", value: plaintext, version: detected.version };
|
|
1524
|
+
} catch (error) {
|
|
1525
|
+
if (error instanceof EncryptionKeyMissingError) {
|
|
1526
|
+
getLogger().warn("Failed to decrypt field (key missing for version):", error.message);
|
|
1527
|
+
return { status: "key_missing", value, version: detected.version };
|
|
1528
|
+
}
|
|
1529
|
+
getLogger().warn("Failed to decrypt field (auth mismatch or crypto error):", error);
|
|
1530
|
+
return { status: "auth_mismatch", value, version: detected.version };
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
async function decryptField(value, address) {
|
|
1534
|
+
const result = await decryptFieldDetailed(value, address);
|
|
1535
|
+
return result.value;
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1352
1538
|
// src/lib/chat/reservationReceipts.ts
|
|
1353
1539
|
var ACTIONS = [
|
|
1354
1540
|
{
|
|
@@ -1370,7 +1556,8 @@ function extractReservationReceipts(toolCallEvents) {
|
|
|
1370
1556
|
for (const event of toolCallEvents ?? []) {
|
|
1371
1557
|
const action = ACTIONS.find((a) => event.name?.endsWith(a.tool));
|
|
1372
1558
|
const output = event.output ?? "";
|
|
1373
|
-
if (!action || !output || NOT_AN_ATTEMPT.test(output))
|
|
1559
|
+
if (!action || !output || hasEncryptedToolPayload(event) || NOT_AN_ATTEMPT.test(output))
|
|
1560
|
+
continue;
|
|
1374
1561
|
const kept = latest.get(action.kind)?.status;
|
|
1375
1562
|
if (kept === "made") continue;
|
|
1376
1563
|
const receipt = toReceipt(action, event.arguments ?? "", output);
|
|
@@ -1619,187 +1806,6 @@ function getImageModel(r) {
|
|
|
1619
1806
|
return r.image_model;
|
|
1620
1807
|
}
|
|
1621
1808
|
|
|
1622
|
-
// src/lib/logger.ts
|
|
1623
|
-
var consoleLogger = {
|
|
1624
|
-
debug: console.log.bind(console),
|
|
1625
|
-
info: console.info.bind(console),
|
|
1626
|
-
warn: console.warn.bind(console),
|
|
1627
|
-
error: console.error.bind(console)
|
|
1628
|
-
};
|
|
1629
|
-
var noopLogger = {
|
|
1630
|
-
debug: () => {
|
|
1631
|
-
},
|
|
1632
|
-
info: () => {
|
|
1633
|
-
},
|
|
1634
|
-
warn: () => {
|
|
1635
|
-
},
|
|
1636
|
-
error: () => {
|
|
1637
|
-
}
|
|
1638
|
-
};
|
|
1639
|
-
var LOGGER_STATE_KEY = /* @__PURE__ */ Symbol.for("@anuma/sdk/logger/v1");
|
|
1640
|
-
var loggerGlobal = globalThis;
|
|
1641
|
-
var fallbackLoggerState = { logger: consoleLogger };
|
|
1642
|
-
var fallbackWarned = false;
|
|
1643
|
-
function getLoggerState() {
|
|
1644
|
-
try {
|
|
1645
|
-
return loggerGlobal[LOGGER_STATE_KEY] ?? (loggerGlobal[LOGGER_STATE_KEY] = fallbackLoggerState);
|
|
1646
|
-
} catch {
|
|
1647
|
-
if (!fallbackWarned) {
|
|
1648
|
-
fallbackWarned = true;
|
|
1649
|
-
fallbackLoggerState.logger.warn(
|
|
1650
|
-
"@anuma/sdk: the global object rejects new properties. This entrypoint keeps its own logger, so setLogger does not reach other entrypoints."
|
|
1651
|
-
);
|
|
1652
|
-
}
|
|
1653
|
-
return fallbackLoggerState;
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
function setLogger(logger) {
|
|
1657
|
-
getLoggerState().logger = logger;
|
|
1658
|
-
}
|
|
1659
|
-
function getLogger() {
|
|
1660
|
-
return getLoggerState().logger;
|
|
1661
|
-
}
|
|
1662
|
-
|
|
1663
|
-
// src/react/useEncryption.ts
|
|
1664
|
-
var EncryptionKeyMissingError = class extends Error {
|
|
1665
|
-
constructor(address, version) {
|
|
1666
|
-
super(
|
|
1667
|
-
`Encryption key (${version}) not found for ${address}. Please sign a message to generate your encryption key.`
|
|
1668
|
-
);
|
|
1669
|
-
this.name = "EncryptionKeyMissingError";
|
|
1670
|
-
this.address = address;
|
|
1671
|
-
this.version = version;
|
|
1672
|
-
}
|
|
1673
|
-
};
|
|
1674
|
-
var encryptionKeyStore = /* @__PURE__ */ new Map();
|
|
1675
|
-
var cryptoKeyCache = /* @__PURE__ */ new Map();
|
|
1676
|
-
var clearAllEncryptionStateCallbacks = /* @__PURE__ */ new Set();
|
|
1677
|
-
function onClearAllEncryptionState(callback) {
|
|
1678
|
-
clearAllEncryptionStateCallbacks.add(callback);
|
|
1679
|
-
return () => {
|
|
1680
|
-
clearAllEncryptionStateCallbacks.delete(callback);
|
|
1681
|
-
};
|
|
1682
|
-
}
|
|
1683
|
-
function getStoredKeyByVersion(address, version) {
|
|
1684
|
-
const keys = encryptionKeyStore.get(address);
|
|
1685
|
-
if (!keys) return null;
|
|
1686
|
-
const value = version === "v2" ? keys.legacy : keys.current;
|
|
1687
|
-
return value ?? null;
|
|
1688
|
-
}
|
|
1689
|
-
function hexNibble(code) {
|
|
1690
|
-
if (code >= 48 && code <= 57) return code - 48;
|
|
1691
|
-
if (code >= 65 && code <= 70) return code - 55;
|
|
1692
|
-
if (code >= 97 && code <= 102) return code - 87;
|
|
1693
|
-
return -1;
|
|
1694
|
-
}
|
|
1695
|
-
function hexToBytes(hex) {
|
|
1696
|
-
if (typeof hex !== "string") {
|
|
1697
|
-
throw new TypeError(
|
|
1698
|
-
"hexToBytes: expected a hex string. Pass raw signature bytes to deriveKeyFromSignatureBytes instead of stringifying them."
|
|
1699
|
-
);
|
|
1700
|
-
}
|
|
1701
|
-
const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
1702
|
-
if (cleanHex.length === 0 || cleanHex.length % 2 !== 0) {
|
|
1703
|
-
throw new Error(
|
|
1704
|
-
`hexToBytes: invalid hex string (expected a non-empty even length, got ${cleanHex.length})`
|
|
1705
|
-
);
|
|
1706
|
-
}
|
|
1707
|
-
const bytes = new Uint8Array(cleanHex.length / 2);
|
|
1708
|
-
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
1709
|
-
const hi = hexNibble(cleanHex.charCodeAt(i));
|
|
1710
|
-
const lo = hexNibble(cleanHex.charCodeAt(i + 1));
|
|
1711
|
-
if (hi < 0 || lo < 0) {
|
|
1712
|
-
throw new Error(`hexToBytes: invalid hex string (non-hex character at index ${i})`);
|
|
1713
|
-
}
|
|
1714
|
-
bytes[i / 2] = hi << 4 | lo;
|
|
1715
|
-
}
|
|
1716
|
-
return bytes;
|
|
1717
|
-
}
|
|
1718
|
-
var SHARED_TEXT_ENCODER = new TextEncoder();
|
|
1719
|
-
var SHARED_TEXT_DECODER = new TextDecoder();
|
|
1720
|
-
async function getEncryptionKey(address, version = "v3") {
|
|
1721
|
-
const cacheKey = `${address}:${version}`;
|
|
1722
|
-
const cachedKey = cryptoKeyCache.get(cacheKey);
|
|
1723
|
-
if (cachedKey) {
|
|
1724
|
-
return cachedKey;
|
|
1725
|
-
}
|
|
1726
|
-
const keyHex = getStoredKeyByVersion(address, version);
|
|
1727
|
-
if (!keyHex) {
|
|
1728
|
-
throw new EncryptionKeyMissingError(address, version);
|
|
1729
|
-
}
|
|
1730
|
-
const keyBytes = hexToBytes(keyHex);
|
|
1731
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
1732
|
-
"raw",
|
|
1733
|
-
keyBytes.buffer,
|
|
1734
|
-
{ name: "AES-GCM" },
|
|
1735
|
-
false,
|
|
1736
|
-
["encrypt", "decrypt"]
|
|
1737
|
-
);
|
|
1738
|
-
cryptoKeyCache.set(cacheKey, cryptoKey);
|
|
1739
|
-
return cryptoKey;
|
|
1740
|
-
}
|
|
1741
|
-
async function decryptData(encryptedHex, address, version = "v3") {
|
|
1742
|
-
const key = await getEncryptionKey(address, version);
|
|
1743
|
-
return decryptDataWithKey(encryptedHex, key);
|
|
1744
|
-
}
|
|
1745
|
-
function hasEncryptionKey(address, version) {
|
|
1746
|
-
return getStoredKeyByVersion(address, version ?? "v3") !== null;
|
|
1747
|
-
}
|
|
1748
|
-
async function decryptDataWithKey(encryptedHex, key) {
|
|
1749
|
-
const combined = hexToBytes(encryptedHex);
|
|
1750
|
-
const iv = combined.subarray(0, 12);
|
|
1751
|
-
const encryptedData = combined.subarray(12);
|
|
1752
|
-
const decryptedData = await crypto.subtle.decrypt(
|
|
1753
|
-
{
|
|
1754
|
-
name: "AES-GCM",
|
|
1755
|
-
iv
|
|
1756
|
-
},
|
|
1757
|
-
key,
|
|
1758
|
-
encryptedData
|
|
1759
|
-
);
|
|
1760
|
-
return SHARED_TEXT_DECODER.decode(decryptedData);
|
|
1761
|
-
}
|
|
1762
|
-
|
|
1763
|
-
// src/lib/db/encryption-utils.ts
|
|
1764
|
-
function isEncrypted(value) {
|
|
1765
|
-
const prefix = value.startsWith("enc:v3:") ? "enc:v3:" : value.startsWith("enc:v2:") ? "enc:v2:" : null;
|
|
1766
|
-
if (!prefix) return false;
|
|
1767
|
-
const payload = value.slice(prefix.length);
|
|
1768
|
-
return payload.length >= 56 && /^[0-9a-f]+$/i.test(payload);
|
|
1769
|
-
}
|
|
1770
|
-
function detectEncryptionVersion(value) {
|
|
1771
|
-
if (value.startsWith("enc:v3:")) {
|
|
1772
|
-
return { version: "v3", encryptedData: value.slice("enc:v3:".length) };
|
|
1773
|
-
}
|
|
1774
|
-
if (value.startsWith("enc:v2:")) {
|
|
1775
|
-
return { version: "v2", encryptedData: value.slice("enc:v2:".length) };
|
|
1776
|
-
}
|
|
1777
|
-
return null;
|
|
1778
|
-
}
|
|
1779
|
-
async function decryptFieldDetailed(value, address) {
|
|
1780
|
-
if (!value) return { status: "plaintext", value };
|
|
1781
|
-
const detected = detectEncryptionVersion(value);
|
|
1782
|
-
if (!detected) return { status: "plaintext", value };
|
|
1783
|
-
if (detected.encryptedData.length < 56 || !/^[0-9a-f]+$/i.test(detected.encryptedData)) {
|
|
1784
|
-
return { status: "invalid_payload", value, version: detected.version };
|
|
1785
|
-
}
|
|
1786
|
-
try {
|
|
1787
|
-
const plaintext = await decryptData(detected.encryptedData, address, detected.version);
|
|
1788
|
-
return { status: "ok", value: plaintext, version: detected.version };
|
|
1789
|
-
} catch (error) {
|
|
1790
|
-
if (error instanceof EncryptionKeyMissingError) {
|
|
1791
|
-
getLogger().warn("Failed to decrypt field (key missing for version):", error.message);
|
|
1792
|
-
return { status: "key_missing", value, version: detected.version };
|
|
1793
|
-
}
|
|
1794
|
-
getLogger().warn("Failed to decrypt field (auth mismatch or crypto error):", error);
|
|
1795
|
-
return { status: "auth_mismatch", value, version: detected.version };
|
|
1796
|
-
}
|
|
1797
|
-
}
|
|
1798
|
-
async function decryptField(value, address) {
|
|
1799
|
-
const result = await decryptFieldDetailed(value, address);
|
|
1800
|
-
return result.value;
|
|
1801
|
-
}
|
|
1802
|
-
|
|
1803
1809
|
// src/lib/db/chat/lazyDecrypt.ts
|
|
1804
1810
|
var LRU_CAPACITY = 256;
|
|
1805
1811
|
var titleCache = /* @__PURE__ */ new Map();
|
|
@@ -2171,13 +2177,35 @@ async function generateEmbedding(text2, options) {
|
|
|
2171
2177
|
if (total === void 0) return embedOne(text2, options, void 0);
|
|
2172
2178
|
return withDeadline((signal) => embedOne(text2, options, signal), total, "embedding");
|
|
2173
2179
|
}
|
|
2180
|
+
var inFlightByCache = /* @__PURE__ */ new WeakMap();
|
|
2174
2181
|
async function embedOne(text2, options, outer) {
|
|
2175
|
-
const {
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2182
|
+
const { cache } = options;
|
|
2183
|
+
if (!cache) return Array.from(await requestEmbedding(text2, options, outer));
|
|
2184
|
+
const cached = cache.get(text2);
|
|
2185
|
+
if (cached) return Array.from(cached);
|
|
2186
|
+
const inFlight = inFlightByCache.get(cache) ?? /* @__PURE__ */ new Map();
|
|
2187
|
+
inFlightByCache.set(cache, inFlight);
|
|
2188
|
+
const pending = inFlight.get(text2);
|
|
2189
|
+
if (pending) {
|
|
2190
|
+
try {
|
|
2191
|
+
return Array.from(await pending);
|
|
2192
|
+
} catch (err) {
|
|
2193
|
+
if (outer?.aborted) throw err;
|
|
2194
|
+
}
|
|
2180
2195
|
}
|
|
2196
|
+
const request = requestEmbedding(text2, options, outer);
|
|
2197
|
+
inFlight.set(text2, request);
|
|
2198
|
+
try {
|
|
2199
|
+
const embedding = await request;
|
|
2200
|
+
cache.set(text2, embedding);
|
|
2201
|
+
return Array.from(embedding);
|
|
2202
|
+
} finally {
|
|
2203
|
+
if (inFlight.get(text2) === request) inFlight.delete(text2);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
async function requestEmbedding(text2, options, outer) {
|
|
2207
|
+
const { baseUrl = BASE_URL, model } = options;
|
|
2208
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_EMBEDDING_REQUEST_TIMEOUT_MS;
|
|
2181
2209
|
const headers = {
|
|
2182
2210
|
...await resolveAuthHeaders(options, outer),
|
|
2183
2211
|
...taskTypeHeader(options.taskType)
|
|
@@ -2212,11 +2240,7 @@ async function embedOne(text2, options, outer) {
|
|
|
2212
2240
|
totalTokens: response.data.usage.total_tokens ?? 0
|
|
2213
2241
|
});
|
|
2214
2242
|
}
|
|
2215
|
-
|
|
2216
|
-
if (cache) {
|
|
2217
|
-
cache.set(text2, f32Embedding);
|
|
2218
|
-
}
|
|
2219
|
-
return Array.from(f32Embedding);
|
|
2243
|
+
return Float32Array.from(embedding);
|
|
2220
2244
|
}
|
|
2221
2245
|
var DEFAULT_EMBEDDING_BATCH_SIZE = 100;
|
|
2222
2246
|
var DEFAULT_EMBEDDING_BATCH_CONCURRENCY = 3;
|