@agentunion/fastaun 0.5.24 → 0.5.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/_packed_docs/CHANGELOG.md +29 -0
  3. package/_packed_docs/TS/346/263/250/345/206/214/350/207/252/345/212/250/347/273/221/345/256/232Owner/347/232/204E2E/351/252/214/350/257/201/344/273/243/347/240/201.md +196 -0
  4. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2112.1.md +2 -2
  5. package/_packed_docs/sdk/0.5.24-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +401 -0
  6. package/_packed_docs/sdk/0.5.24-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +354 -0
  7. package/_packed_docs/sdk/AUN_DOCS_GUIDE.md +4 -0
  8. package/_packed_docs/sdk/INDEX.md +9 -0
  9. package/dist/agent-md.d.ts +1 -0
  10. package/dist/agent-md.js +38 -10
  11. package/dist/agent-md.js.map +1 -1
  12. package/dist/auth.d.ts +1 -0
  13. package/dist/auth.js +44 -10
  14. package/dist/auth.js.map +1 -1
  15. package/dist/client/delivery.d.ts +3 -0
  16. package/dist/client/delivery.js +197 -47
  17. package/dist/client/delivery.js.map +1 -1
  18. package/dist/client/rpc-pipeline.js +10 -0
  19. package/dist/client/rpc-pipeline.js.map +1 -1
  20. package/dist/client/v2-e2ee.js +17 -5
  21. package/dist/client/v2-e2ee.js.map +1 -1
  22. package/dist/client.d.ts +1 -0
  23. package/dist/client.js +43 -25
  24. package/dist/client.js.map +1 -1
  25. package/dist/group-fs.js +5 -0
  26. package/dist/group-fs.js.map +1 -1
  27. package/dist/ownership-v21.d.ts +2 -0
  28. package/dist/ownership-v21.js +61 -30
  29. package/dist/ownership-v21.js.map +1 -1
  30. package/dist/tools/cross-sdk-agent.d.ts +1 -1
  31. package/dist/version.d.ts +1 -1
  32. package/dist/version.js +1 -1
  33. package/package.json +1 -1
package/dist/client.d.ts CHANGED
@@ -121,6 +121,7 @@ export declare class AUNClient {
121
121
  /** Gateway URL */
122
122
  private _gatewayUrl;
123
123
  private _gatewayCandidates;
124
+ private _peerGatewayCache;
124
125
  /** 是否正在关闭 */
125
126
  private _closing;
126
127
  private _disconnecting;
package/dist/client.js CHANGED
@@ -352,7 +352,9 @@ function createAgentMdManagerForRuntime(opts) {
352
352
  entitySignerResolver: opts.fetchEntitySigner,
353
353
  gatewayResolver: async (aid) => {
354
354
  const gatewayUrl = await opts.gateway.resolve(aid);
355
- opts.gateway.set(gatewayUrl);
355
+ const localAid = opts.ownerAid();
356
+ if (localAid && aid.slice(aid.indexOf('.') + 1).trim().toLowerCase() === localAid.slice(localAid.indexOf('.') + 1).trim().toLowerCase())
357
+ opts.gateway.set(gatewayUrl);
356
358
  return gatewayUrl;
357
359
  },
358
360
  accessTokenResolver: async (aid, gatewayUrl) => {
@@ -446,7 +448,7 @@ function createAgentMdManagerForRuntime(opts) {
446
448
  }
447
449
  if (!opts.gateway.get()) {
448
450
  try {
449
- opts.gateway.set(await opts.gateway.resolve(target));
451
+ opts.gateway.set(await opts.gateway.resolve(opts.ownerAid() ?? target));
450
452
  }
451
453
  catch { /* best effort before cert fetch */ }
452
454
  }
@@ -763,6 +765,7 @@ export class AUNClient {
763
765
  /** Gateway URL */
764
766
  _gatewayUrl = null;
765
767
  _gatewayCandidates = [];
768
+ _peerGatewayCache = new Map();
766
769
  /** 是否正在关闭 */
767
770
  _closing = false;
768
771
  _disconnecting = false;
@@ -1360,6 +1363,7 @@ export class AUNClient {
1360
1363
  this._certCache.clear();
1361
1364
  this._gatewayUrl = null;
1362
1365
  this._gatewayCandidates = [];
1366
+ this._peerGatewayCache.clear();
1363
1367
  this._deviceId = aid.deviceId || getDeviceId(nextConfig.aunPath);
1364
1368
  this._slotId = aid.slotId || 'default';
1365
1369
  const debugFlag = nextConfig.debug;
@@ -1522,6 +1526,7 @@ export class AUNClient {
1522
1526
  }
1523
1527
  /** 关闭连接 */
1524
1528
  async close() {
1529
+ this._peerGatewayCache.clear();
1525
1530
  return this._lifecycle.close();
1526
1531
  }
1527
1532
  /**
@@ -2667,9 +2672,11 @@ export class AUNClient {
2667
2672
  on(event, handler) {
2668
2673
  const tStart = Date.now();
2669
2674
  this._clientLog.debug(`on enter: event=${event}`);
2675
+ const messageEvent = event === 'message.received' || event === 'message.recalled' || event === 'message.batch';
2676
+ const hadMessageConsumer = messageEvent && [...this._dispatcher._snapshotApplicationHandlers(['message.received', 'message.recalled', 'message.batch']).values()].some((handlers) => handlers.length > 0);
2670
2677
  const result = this._dispatcher.subscribe(event, handler);
2671
- if (event === 'message.received' || event === 'message.recalled' || event === 'message.batch') {
2672
- if (this._p2pPeerPullEnabled)
2678
+ if (messageEvent) {
2679
+ if (this._p2pPeerPullEnabled && !hadMessageConsumer)
2673
2680
  this._delivery.refreshPendingPeers();
2674
2681
  this._delivery.requestBackgroundPullRecovery();
2675
2682
  }
@@ -4701,20 +4708,15 @@ export class AUNClient {
4701
4708
  return gateways[0];
4702
4709
  }
4703
4710
  async _resolveOwnershipCertificateGateway(aid) {
4704
- const issuer = aid.slice(aid.indexOf('.') + 1);
4705
- if (issuer === this._aid?.slice(this._aid.indexOf('.') + 1))
4711
+ const issuer = aid.slice(aid.indexOf('.') + 1).trim().toLowerCase();
4712
+ if (issuer === this._aid?.slice(this._aid.indexOf('.') + 1).trim().toLowerCase())
4706
4713
  return this._resolveGatewayForAid(aid);
4707
- const port = this._configModel.discoveryPort ? `:${this._configModel.discoveryPort}` : '';
4708
- let lastError;
4709
- for (const host of [aid, `gateway.${issuer}`]) {
4710
- try {
4711
- return await this._discovery.discover(`https://${host}${port}/.well-known/aun-gateway`);
4712
- }
4713
- catch (error) {
4714
- lastError = error;
4715
- }
4714
+ try {
4715
+ return await this._resolveGatewayForAid(aid);
4716
+ }
4717
+ catch (error) {
4718
+ throw ownershipFailure('unavailable', error instanceof Error ? error.message : 'signer Gateway discovery failed', true);
4716
4719
  }
4717
- throw ownershipFailure('unavailable', lastError instanceof Error ? lastError.message : 'signer Gateway discovery failed', true);
4718
4720
  }
4719
4721
  /** 发现并缓存全部 Gateway 候选,供首连和自动重连故障转移使用。 */
4720
4722
  async _resolveGatewayCandidatesForAid(aid) {
@@ -4722,17 +4724,26 @@ export class AUNClient {
4722
4724
  if (!resolvedAid) {
4723
4725
  throw new StateError('gateway discovery requires a loaded AID');
4724
4726
  }
4727
+ const dotIdx = resolvedAid.indexOf('.');
4728
+ const issuerDomain = (dotIdx >= 0 ? resolvedAid.slice(dotIdx + 1) : resolvedAid).trim().toLowerCase();
4729
+ const currentAid = this._aid;
4730
+ const sameIssuer = currentAid !== null && issuerDomain === currentAid.slice(currentAid.indexOf('.') + 1).trim().toLowerCase();
4725
4731
  const discovery = this._discovery;
4726
4732
  const tokenStore = this._tokenStore;
4727
4733
  const discoveryIsCurrent = () => (this._discovery === discovery
4728
4734
  && this._tokenStore === tokenStore
4729
4735
  && !this._closing);
4730
- if (this._gatewayCandidates.length > 0)
4736
+ if (sameIssuer && this._gatewayCandidates.length > 0)
4731
4737
  return [...this._gatewayCandidates];
4732
- if (this._gatewayUrl) {
4738
+ if (sameIssuer && this._gatewayUrl) {
4733
4739
  this._gatewayCandidates = [this._gatewayUrl];
4734
4740
  return [...this._gatewayCandidates];
4735
4741
  }
4742
+ if (!sameIssuer) {
4743
+ const cached = this._peerGatewayCache.get(issuerDomain);
4744
+ if (cached)
4745
+ return [...cached];
4746
+ }
4736
4747
  try {
4737
4748
  const loadMetadata = tokenStore.loadMetadata;
4738
4749
  const metadata = typeof loadMetadata === 'function'
@@ -4744,16 +4755,19 @@ export class AUNClient {
4744
4755
  const cachedGateway = cachedList[0]
4745
4756
  ?? (isJsonObject(metadata) ? String(metadata.gateway_url ?? '').trim() : '');
4746
4757
  if (cachedGateway) {
4747
- this._gatewayCandidates = [...new Set([cachedGateway, ...cachedList])];
4748
- this._gatewayUrl = this._gatewayCandidates[0];
4749
- return [...this._gatewayCandidates];
4758
+ const gatewayUrls = [...new Set([cachedGateway, ...cachedList])];
4759
+ if (sameIssuer) {
4760
+ this._gatewayCandidates = gatewayUrls;
4761
+ this._gatewayUrl = gatewayUrls[0];
4762
+ }
4763
+ else
4764
+ this._peerGatewayCache.set(issuerDomain, gatewayUrls);
4765
+ return [...gatewayUrls];
4750
4766
  }
4751
4767
  }
4752
4768
  catch {
4753
4769
  // 缓存读取失败不影响发现流程。
4754
4770
  }
4755
- const dotIdx = resolvedAid.indexOf('.');
4756
- const issuerDomain = dotIdx >= 0 ? resolvedAid.slice(dotIdx + 1) : resolvedAid;
4757
4771
  const portSuffix = this._configModel.discoveryPort ? `:${this._configModel.discoveryPort}` : '';
4758
4772
  const aidUrl = `https://${resolvedAid}${portSuffix}/.well-known/aun-gateway`;
4759
4773
  const gatewayDomainUrl = `https://gateway.${issuerDomain}${portSuffix}/.well-known/aun-gateway`;
@@ -4773,9 +4787,13 @@ export class AUNClient {
4773
4787
  const gatewayUrls = [...new Set(discovered.map((item) => String(item ?? '').trim()).filter(Boolean))];
4774
4788
  if (gatewayUrls.length === 0)
4775
4789
  throw new ValidationError('gateway discovery returned no candidates');
4776
- this._gatewayCandidates = gatewayUrls;
4777
4790
  const gateway = gatewayUrls[0];
4778
- this._gatewayUrl = gateway;
4791
+ if (sameIssuer) {
4792
+ this._gatewayCandidates = gatewayUrls;
4793
+ this._gatewayUrl = gateway;
4794
+ }
4795
+ else
4796
+ this._peerGatewayCache.set(issuerDomain, gatewayUrls);
4779
4797
  try {
4780
4798
  const saveMetadata = tokenStore.saveMetadata;
4781
4799
  if (typeof saveMetadata === 'function') {