@acosmi/sdk-ts 1.0.1 → 1.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/index.mjs CHANGED
@@ -1403,9 +1403,18 @@ function skillScopes() {
1403
1403
  }
1404
1404
 
1405
1405
  // src/store.ts
1406
+ var fileLockDefaults = {
1407
+ /** 获取锁的超时上限 (毫秒). refresh 流程含网络 < 30s, 30s 已远超正常完成时间. */
1408
+ acquireTimeoutMs: 3e4,
1409
+ /** 旧锁判定阈值 (毫秒). 锁文件 mtime 早于此值视为 stale 进程崩溃残留, 自动 break. */
1410
+ staleMs: 6e4,
1411
+ /** 重试间隔基数 (毫秒). 真实间隔 = base + random(0, jitter). */
1412
+ retryBaseMs: 30,
1413
+ retryJitterMs: 70
1414
+ };
1406
1415
  var FileTokenStore = class {
1407
1416
  path;
1408
- /** 简单串行化锁 (替代 Go sync.Mutex) */
1417
+ /** 进程内串行化 (Promise chain) — 与跨进程 flock 配合, 避免单进程内并发持锁产生死锁式互等. */
1409
1418
  chain = Promise.resolve();
1410
1419
  constructor(path) {
1411
1420
  if (typeof process === "undefined" || !process.versions || !process.versions.node) {
@@ -1424,7 +1433,9 @@ var FileTokenStore = class {
1424
1433
  this.path = path.join(os.homedir(), ".acosmi", "tokens.json");
1425
1434
  return this.path;
1426
1435
  }
1427
- withLock(fn) {
1436
+ /** 进程内串行 (维持 v1.0.1 单进程语义). flock 之外另一层防御: 如果用户自己就是单进程
1437
+ * 并发场景, 不必每次都进 flock 旁路文件 IO. */
1438
+ withChain(fn) {
1428
1439
  const next = this.chain.then(fn, fn);
1429
1440
  this.chain = next.then(
1430
1441
  () => void 0,
@@ -1432,19 +1443,106 @@ var FileTokenStore = class {
1432
1443
  );
1433
1444
  return next;
1434
1445
  }
1446
+ /**
1447
+ * 跨进程临界区. 用 sidecar `<path>.lock` 文件 + O_EXCL 创建语义实现互斥:
1448
+ * - 创建成功 = 持有锁; 写入 pid+timestamp 便于诊断
1449
+ * - 创建失败 (EEXIST) = 别的进程持锁, backoff 重试
1450
+ * - 锁文件 mtime > staleMs = 进程崩溃残留, unlink 后重试
1451
+ * - acquireTimeoutMs 超时 = 抛错 (caller 应作为 transient error 处理, 上层 retry)
1452
+ *
1453
+ * 注意: O_EXCL 在 NFS 上不保证原子; FileTokenStore 适用于本地文件系统 (典型用户家目录).
1454
+ * 真要跨机共享 token, 应实现自定义 Keychain / 数据库 store, 不要用 FileTokenStore.
1455
+ */
1456
+ async withLock(fn) {
1457
+ const fs = await import('fs/promises');
1458
+ const pathMod = await import('path');
1459
+ const p = await this.resolvePath();
1460
+ const lockPath = `${p}.lock`;
1461
+ const dir = pathMod.dirname(p);
1462
+ await fs.mkdir(dir, { recursive: true, mode: 448 });
1463
+ const startMs = Date.now();
1464
+ let release = null;
1465
+ while (true) {
1466
+ try {
1467
+ const fh = await fs.open(lockPath, "wx", 384);
1468
+ try {
1469
+ await fh.writeFile(`${process.pid}
1470
+ ${Date.now()}
1471
+ `, "utf8");
1472
+ } finally {
1473
+ await fh.close();
1474
+ }
1475
+ release = async () => {
1476
+ try {
1477
+ await fs.unlink(lockPath);
1478
+ } catch {
1479
+ }
1480
+ };
1481
+ break;
1482
+ } catch (e) {
1483
+ if (!isAlreadyExistsError(e)) throw e;
1484
+ let stale = false;
1485
+ try {
1486
+ const st = await fs.stat(lockPath);
1487
+ if (Date.now() - st.mtimeMs > fileLockDefaults.staleMs) stale = true;
1488
+ } catch {
1489
+ continue;
1490
+ }
1491
+ if (stale) {
1492
+ try {
1493
+ await fs.unlink(lockPath);
1494
+ } catch {
1495
+ }
1496
+ continue;
1497
+ }
1498
+ if (Date.now() - startMs > fileLockDefaults.acquireTimeoutMs) {
1499
+ throw new Error(
1500
+ `acquire token file lock timeout (${fileLockDefaults.acquireTimeoutMs}ms): ${lockPath}`
1501
+ );
1502
+ }
1503
+ const waitMs = fileLockDefaults.retryBaseMs + Math.random() * fileLockDefaults.retryJitterMs;
1504
+ await new Promise((r) => setTimeout(r, waitMs));
1505
+ }
1506
+ }
1507
+ try {
1508
+ return await fn();
1509
+ } finally {
1510
+ if (release) await release();
1511
+ }
1512
+ }
1513
+ /**
1514
+ * 写入 token. 流程:
1515
+ * 1. mkdir -p (默认路径目录)
1516
+ * 2. 写入 `<path>.tmp.<pid>`
1517
+ * 3. fsync 后 rename 到正式路径 (POSIX 上 rename(2) 同分区原子, Windows 上 ReplaceFile)
1518
+ *
1519
+ * 选择 atomic rename 而非直接 writeFile: 多进程并发或本进程崩溃时, 读端永远看到的是
1520
+ * 完整的旧/新 JSON, 不会读到截断半文件 (Client.create.store.load 可能在另一进程
1521
+ * 写入中间触发, atomic rename 避免它解析失败).
1522
+ */
1435
1523
  save(tokens) {
1436
- return this.withLock(async () => {
1524
+ return this.withChain(async () => {
1437
1525
  const fs = await import('fs/promises');
1438
- const path = await import('path');
1526
+ const pathMod = await import('path');
1439
1527
  const p = await this.resolvePath();
1440
- const dir = path.dirname(p);
1528
+ const dir = pathMod.dirname(p);
1441
1529
  await fs.mkdir(dir, { recursive: true, mode: 448 });
1530
+ const tmp = `${p}.tmp.${process.pid}.${Date.now()}.${Math.floor(Math.random() * 1e6)}`;
1442
1531
  const data = JSON.stringify(tokens, null, 2);
1443
- await fs.writeFile(p, data, { encoding: "utf8", mode: 384 });
1532
+ await fs.writeFile(tmp, data, { encoding: "utf8", mode: 384 });
1533
+ try {
1534
+ await fs.rename(tmp, p);
1535
+ } catch (e) {
1536
+ try {
1537
+ await fs.unlink(tmp);
1538
+ } catch {
1539
+ }
1540
+ throw e;
1541
+ }
1444
1542
  });
1445
1543
  }
1446
1544
  load() {
1447
- return this.withLock(async () => {
1545
+ return this.withChain(async () => {
1448
1546
  const fs = await import('fs/promises');
1449
1547
  const p = await this.resolvePath();
1450
1548
  try {
@@ -1459,7 +1557,7 @@ var FileTokenStore = class {
1459
1557
  });
1460
1558
  }
1461
1559
  clear() {
1462
- return this.withLock(async () => {
1560
+ return this.withChain(async () => {
1463
1561
  const fs = await import('fs/promises');
1464
1562
  const p = await this.resolvePath();
1465
1563
  try {
@@ -1477,6 +1575,12 @@ function isNotExistError(e) {
1477
1575
  }
1478
1576
  return false;
1479
1577
  }
1578
+ function isAlreadyExistsError(e) {
1579
+ if (typeof e === "object" && e !== null && "code" in e) {
1580
+ return e.code === "EEXIST";
1581
+ }
1582
+ return false;
1583
+ }
1480
1584
  function newFileTokenStore(path) {
1481
1585
  return new FileTokenStore(path);
1482
1586
  }
@@ -2343,61 +2447,67 @@ var Client = class _Client {
2343
2447
  if (!tokenSetIsExpired(tokens)) {
2344
2448
  return tokens.access_token;
2345
2449
  }
2346
- return this.withMu(async () => {
2347
- if (this.tokens == null) {
2348
- throw new Error("not authorized, call login() first");
2349
- }
2350
- if (!tokenSetIsExpired(this.tokens)) {
2351
- return this.tokens.access_token;
2352
- }
2353
- if (this.meta == null) {
2450
+ return this.withMu(
2451
+ () => this.storeWithLock(async () => {
2452
+ await this.syncFromDisk();
2453
+ if (this.tokens == null) {
2454
+ throw new Error("not authorized, call login() first");
2455
+ }
2456
+ if (!tokenSetIsExpired(this.tokens)) {
2457
+ return this.tokens.access_token;
2458
+ }
2459
+ if (this.meta == null) {
2460
+ try {
2461
+ this.meta = await discover(this.serverURL, signal);
2462
+ } catch (e) {
2463
+ throw new Error(
2464
+ `discover for refresh: ${e instanceof Error ? e.message : String(e)}`
2465
+ );
2466
+ }
2467
+ }
2468
+ let tokenResp;
2354
2469
  try {
2355
- this.meta = await discover(this.serverURL, signal);
2470
+ tokenResp = await refreshToken(this.meta, this.tokens.client_id, this.tokens.refresh_token, signal);
2356
2471
  } catch (e) {
2357
- throw new Error(
2358
- `discover for refresh: ${e instanceof Error ? e.message : String(e)}`
2359
- );
2472
+ throw new Error(`refresh token: ${e instanceof Error ? e.message : String(e)}`);
2360
2473
  }
2361
- }
2362
- let tokenResp;
2363
- try {
2364
- tokenResp = await refreshToken(this.meta, this.tokens.client_id, this.tokens.refresh_token, signal);
2365
- } catch (e) {
2366
- throw new Error(`refresh token: ${e instanceof Error ? e.message : String(e)}`);
2367
- }
2368
- this.tokens = newTokenSet(tokenResp, this.tokens.client_id, this.serverURL);
2369
- try {
2370
- await this.store.save(this.tokens);
2371
- } catch (e) {
2372
- console.warn(`[acosmi-sdk] warning: save refreshed token failed: ${e instanceof Error ? e.message : String(e)}`);
2373
- }
2374
- return this.tokens.access_token;
2375
- });
2474
+ this.tokens = newTokenSet(tokenResp, this.tokens.client_id, this.serverURL);
2475
+ try {
2476
+ await this.store.save(this.tokens);
2477
+ } catch (e) {
2478
+ console.warn(`[acosmi-sdk] warning: save refreshed token failed: ${e instanceof Error ? e.message : String(e)}`);
2479
+ }
2480
+ return this.tokens.access_token;
2481
+ })
2482
+ );
2376
2483
  }
2377
2484
  /** 强制刷新 token (用于 401 重试) */
2378
2485
  async forceRefresh(signal) {
2379
- return this.withMu(async () => {
2380
- if (this.tokens == null) {
2381
- throw new Error("no tokens to refresh");
2382
- }
2383
- if (this.meta == null) {
2384
- this.meta = await discover(this.serverURL, signal);
2385
- }
2386
- const tokenResp = await refreshToken(
2387
- this.meta,
2388
- this.tokens.client_id,
2389
- this.tokens.refresh_token,
2390
- signal
2391
- );
2392
- this.tokens = newTokenSet(tokenResp, this.tokens.client_id, this.serverURL);
2393
- try {
2394
- await this.store.save(this.tokens);
2395
- } catch (e) {
2396
- console.warn(
2397
- `[acosmi-sdk] warning: save refreshed token failed: ${e instanceof Error ? e.message : String(e)}`
2486
+ return this.withMu(
2487
+ () => this.storeWithLock(async () => {
2488
+ await this.syncFromDisk();
2489
+ if (this.tokens == null) {
2490
+ throw new Error("no tokens to refresh");
2491
+ }
2492
+ if (this.meta == null) {
2493
+ this.meta = await discover(this.serverURL, signal);
2494
+ }
2495
+ const tokenResp = await refreshToken(
2496
+ this.meta,
2497
+ this.tokens.client_id,
2498
+ this.tokens.refresh_token,
2499
+ signal
2398
2500
  );
2399
- }
2400
- });
2501
+ this.tokens = newTokenSet(tokenResp, this.tokens.client_id, this.serverURL);
2502
+ try {
2503
+ await this.store.save(this.tokens);
2504
+ } catch (e) {
2505
+ console.warn(
2506
+ `[acosmi-sdk] warning: save refreshed token failed: ${e instanceof Error ? e.message : String(e)}`
2507
+ );
2508
+ }
2509
+ })
2510
+ );
2401
2511
  }
2402
2512
  /** 互斥锁 helper (替代 Go sync.Mutex) */
2403
2513
  withMu(fn) {
@@ -2408,6 +2518,29 @@ var Client = class _Client {
2408
2518
  );
2409
2519
  return next;
2410
2520
  }
2521
+ /** v1.0.2: 跨进程临界区 helper. store.withLock 可选, 缺省则直接调用 fn (LocalStorage /
2522
+ * InMemory 单进程无需). */
2523
+ async storeWithLock(fn) {
2524
+ const lock = this.store.withLock;
2525
+ if (typeof lock === "function") {
2526
+ return lock.call(this.store, fn);
2527
+ }
2528
+ return fn();
2529
+ }
2530
+ /** v1.0.2: 从磁盘同步 token (refresh 前). 别的进程 rotation 后磁盘 refresh_token 已变,
2531
+ * 本进程内存仍是旧 R0; 不同步直接 refresh 必撞网关 400. load 失败保留内存继续 (容错). */
2532
+ async syncFromDisk() {
2533
+ let onDisk = null;
2534
+ try {
2535
+ onDisk = await this.store.load();
2536
+ } catch {
2537
+ return;
2538
+ }
2539
+ if (!onDisk) return;
2540
+ if (this.tokens == null || onDisk.refresh_token !== this.tokens.refresh_token) {
2541
+ this.tokens = onDisk;
2542
+ }
2543
+ }
2411
2544
  // ===========================================================================
2412
2545
  // Managed Models
2413
2546
  // ===========================================================================
@@ -3945,6 +4078,6 @@ Client.prototype.getBugReport = async function(bugID, signal) {
3945
4078
  return resp.data;
3946
4079
  };
3947
4080
 
3948
- export { AnthropicAdapter, BucketClassCommercial, BucketClassGeneric, BusinessError, Client, DefaultRetryPolicy, ErrAuthDenied, ErrBrowserOpen, ErrDiscovery, ErrRegistration, ErrSSLProxy, ErrTimeout, ErrTokenExchange, EventAuthURL, EventComplete, EventError, FileTokenStore, FilterStatusAdminBypass, FilterStatusDisabledByFlag, FilterStatusFallbackMissingUser, FilterStatusFallbackNoBuckets, FilterStatusFallbackTkdistError, FilterStatusFallbackTkdistSkew, FilterStatusInternalBypass, FilterStatusOK, FilterStatusUnknown, HTTPError, InMemoryTokenStore, LocalStorageTokenStore, ModelNotFoundError, NetworkError, OpenAIAdapter, OrderTerminalError, ProviderFormat, RateLimitError, ScopeAI, ScopeAccount, ScopeEntitlements, ScopeModels, ScopeModelsChat, ScopeProfile, ScopeSkillStore, ScopeSkills, ScopeTokenPackages, ScopeTools, ScopeToolsExecute, ScopeWallet, ScopeWalletReadonly, ServerToolTypeWebSearch, StreamError, ThinkingHigh, ThinkingHighMinMaxTokens, ThinkingMax, ThinkingMaxFallbackMaxTokens, ThinkingOff, allScopes, anthropicResponseTextContent, anthropicResponseThinkingContent, anthropicResponseToolUseBlocks, apiResponseBusinessError, apiResponseGetMessage, authorize, bucketInfoIsCommercial, bucketRowIsCommercial, buildBetas, commerceScopes, computeBackoff, defaultRetryable, defaultSafeToRetry, discover, effectivePolicy, exchangeCode, extractAnthropicBlockMeta, getAdapter, getAdapterForModel, isSSLError, modelScopes, newFileTokenStore, newThinkingConfig, newTokenSet, newWebSearchTool, parseNotificationEvent, parseSettlement, parseSourcesEvent, refreshToken, register, revokeToken, sanitize_exports as sanitize, skillScopes, tokenSetIsExpired, uniqueMerge };
4081
+ export { AnthropicAdapter, BucketClassCommercial, BucketClassGeneric, BusinessError, Client, DefaultRetryPolicy, ErrAuthDenied, ErrBrowserOpen, ErrDiscovery, ErrRegistration, ErrSSLProxy, ErrTimeout, ErrTokenExchange, EventAuthURL, EventComplete, EventError, FileTokenStore, FilterStatusAdminBypass, FilterStatusDisabledByFlag, FilterStatusFallbackMissingUser, FilterStatusFallbackNoBuckets, FilterStatusFallbackTkdistError, FilterStatusFallbackTkdistSkew, FilterStatusInternalBypass, FilterStatusOK, FilterStatusUnknown, HTTPError, InMemoryTokenStore, LocalStorageTokenStore, ModelNotFoundError, NetworkError, OpenAIAdapter, OrderTerminalError, ProviderFormat, RateLimitError, ScopeAI, ScopeAccount, ScopeEntitlements, ScopeModels, ScopeModelsChat, ScopeProfile, ScopeSkillStore, ScopeSkills, ScopeTokenPackages, ScopeTools, ScopeToolsExecute, ScopeWallet, ScopeWalletReadonly, ServerToolTypeWebSearch, StreamError, ThinkingHigh, ThinkingHighMinMaxTokens, ThinkingMax, ThinkingMaxFallbackMaxTokens, ThinkingOff, allScopes, anthropicResponseTextContent, anthropicResponseThinkingContent, anthropicResponseToolUseBlocks, apiResponseBusinessError, apiResponseGetMessage, authorize, bucketInfoIsCommercial, bucketRowIsCommercial, buildBetas, commerceScopes, computeBackoff, defaultRetryable, defaultSafeToRetry, discover, effectivePolicy, exchangeCode, extractAnthropicBlockMeta, fileLockDefaults, getAdapter, getAdapterForModel, isSSLError, modelScopes, newFileTokenStore, newThinkingConfig, newTokenSet, newWebSearchTool, parseNotificationEvent, parseSettlement, parseSourcesEvent, refreshToken, register, revokeToken, sanitize_exports as sanitize, skillScopes, tokenSetIsExpired, uniqueMerge };
3949
4082
  //# sourceMappingURL=index.mjs.map
3950
4083
  //# sourceMappingURL=index.mjs.map