@agentunion/fastaun 0.5.8 → 0.5.9

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 (38) hide show
  1. package/CHANGELOG.md +32 -0
  2. package/_packed_docs/CHANGELOG.md +32 -0
  3. package/_packed_docs/agent.md/examples//347/276/244/347/273/204-/345/274/200/345/217/221/345/233/242/351/230/237.md +21 -0
  4. package/dist/agent-md-schema.js +3 -0
  5. package/dist/agent-md-schema.js.map +1 -1
  6. package/dist/aid-store.js +1 -3
  7. package/dist/aid-store.js.map +1 -1
  8. package/dist/auth.d.ts +3 -1
  9. package/dist/auth.js +64 -13
  10. package/dist/auth.js.map +1 -1
  11. package/dist/client/delivery.d.ts +4 -0
  12. package/dist/client/delivery.js +121 -20
  13. package/dist/client/delivery.js.map +1 -1
  14. package/dist/client/group-state.js +2 -2
  15. package/dist/client/group-state.js.map +1 -1
  16. package/dist/client/lifecycle.js +20 -8
  17. package/dist/client/lifecycle.js.map +1 -1
  18. package/dist/client/rpc-pipeline.d.ts +8 -0
  19. package/dist/client/rpc-pipeline.js +150 -32
  20. package/dist/client/rpc-pipeline.js.map +1 -1
  21. package/dist/client/v2-e2ee.js +12 -4
  22. package/dist/client/v2-e2ee.js.map +1 -1
  23. package/dist/client.js +87 -11
  24. package/dist/client.js.map +1 -1
  25. package/dist/discovery.d.ts +1 -0
  26. package/dist/discovery.js +3 -0
  27. package/dist/discovery.js.map +1 -1
  28. package/dist/facades.js +7 -3
  29. package/dist/facades.js.map +1 -1
  30. package/dist/index.d.ts +1 -1
  31. package/dist/index.js.map +1 -1
  32. package/dist/net.d.ts +3 -0
  33. package/dist/net.js +13 -0
  34. package/dist/net.js.map +1 -1
  35. package/dist/version.d.ts +1 -1
  36. package/dist/version.js +1 -1
  37. package/package.json +1 -1
  38. package/_packed_docs//345/217/221/345/270/203/346/212/245/345/221/212-0.5.6.md +0 -260
package/dist/client.js CHANGED
@@ -852,6 +852,7 @@ export class AUNClient {
852
852
  this._peerCache.clear();
853
853
  this._certCache.clear();
854
854
  this._gatewayUrl = null;
855
+ this._gatewayCandidates = [];
855
856
  this._deviceId = aid.deviceId || getDeviceId(nextConfig.aunPath);
856
857
  this._slotId = aid.slotId || 'default';
857
858
  const debugFlag = nextConfig.debug;
@@ -1059,7 +1060,12 @@ export class AUNClient {
1059
1060
  const target = message.slice(agentPrefix.length).trim();
1060
1061
  if (!target || target.toLowerCase() !== String(this._aid ?? '').trim().toLowerCase())
1061
1062
  return false;
1062
- await this._agentMdManager.upload(buildDefaultAgentMd(target));
1063
+ let agentMd = buildDefaultAgentMd(target);
1064
+ try {
1065
+ agentMd = this._agentMdManager.readContent(target);
1066
+ }
1067
+ catch { }
1068
+ await this._agentMdManager.upload(agentMd);
1063
1069
  return true;
1064
1070
  }
1065
1071
  async _runGroupIdentityOperation(groupId, operation) {
@@ -1091,6 +1097,10 @@ export class AUNClient {
1091
1097
  }
1092
1098
  this._aidStore = store;
1093
1099
  const payload = { ...params };
1100
+ if (payload.group_name === undefined && payload.groupName !== undefined) {
1101
+ payload.group_name = payload.groupName;
1102
+ delete payload.groupName;
1103
+ }
1094
1104
  const isNamed = Boolean(String(payload.group_name ?? '').trim());
1095
1105
  payload._defer_group_ready_postprocess = true;
1096
1106
  if (!isNamed) {
@@ -1116,7 +1126,21 @@ export class AUNClient {
1116
1126
  delete postprocessParams._defer_group_ready_postprocess;
1117
1127
  return await this._groupState.postprocessResult('group.create', postprocessParams, result);
1118
1128
  }
1119
- const keyPair = new CryptoProvider().generateIdentity();
1129
+ const pendingKey = `create:${String(payload.group_name).trim().toLowerCase()}`;
1130
+ const keystore = store._keystore;
1131
+ let keyPair = null;
1132
+ if (keystore && typeof keystore.loadPendingGroupBind === 'function') {
1133
+ keyPair = keystore.loadPendingGroupBind(pendingKey);
1134
+ }
1135
+ if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1136
+ keyPair = new CryptoProvider().generateIdentity();
1137
+ if (keystore && typeof keystore.savePendingGroupBind === 'function') {
1138
+ keystore.savePendingGroupBind(pendingKey, keyPair);
1139
+ }
1140
+ }
1141
+ if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1142
+ throw new ValidationError('createGroup: failed to generate or load key pair');
1143
+ }
1120
1144
  payload.public_key = keyPair.public_key_der_b64;
1121
1145
  payload.curve = keyPair.curve;
1122
1146
  const result = await this.call('group.create', payload);
@@ -1138,6 +1162,9 @@ export class AUNClient {
1138
1162
  throw new ValidationError(imported.error.message);
1139
1163
  }
1140
1164
  await this._uploadGroupAgentMd(store, groupAid, payload, group);
1165
+ if (keystore && typeof keystore.clearPendingGroupBind === 'function') {
1166
+ keystore.clearPendingGroupBind(pendingKey);
1167
+ }
1141
1168
  const postprocessParams = { ...payload };
1142
1169
  delete postprocessParams._defer_group_ready_postprocess;
1143
1170
  return await this._groupState.postprocessResult('group.create', postprocessParams, result);
@@ -1193,11 +1220,15 @@ export class AUNClient {
1193
1220
  }
1194
1221
  this._aidStore = store;
1195
1222
  const groupId = String(params.group_id ?? params.group_aid ?? '').trim();
1223
+ const pendingKey = `bind:${groupId}`;
1196
1224
  const keystore = store._keystore; // AIDStore._keystore: LocalIdentityStore
1197
1225
  let keyPair = null;
1198
1226
  // 优先复用 pending 槽位中已暂存的待绑定密钥(崩溃/重试幂等)
1199
1227
  if (groupId && keystore && typeof keystore.loadPendingGroupBind === 'function') {
1200
- keyPair = keystore.loadPendingGroupBind(groupId);
1228
+ keyPair = keystore.loadPendingGroupBind(pendingKey);
1229
+ if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1230
+ keyPair = keystore.loadPendingGroupBind(groupId);
1231
+ }
1201
1232
  }
1202
1233
  if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1203
1234
  // 未命中 pending,生成新密钥
@@ -1209,7 +1240,7 @@ export class AUNClient {
1209
1240
  };
1210
1241
  // 发 RPC 前先落盘暂存,确保崩溃后重试能复用同一密钥
1211
1242
  if (groupId && keystore && typeof keystore.savePendingGroupBind === 'function') {
1212
- keystore.savePendingGroupBind(groupId, keyPair);
1243
+ keystore.savePendingGroupBind(pendingKey, keyPair);
1213
1244
  }
1214
1245
  }
1215
1246
  if (!keyPair) {
@@ -1244,6 +1275,7 @@ export class AUNClient {
1244
1275
  await this._uploadGroupAgentMd(store, groupAid, payload, group);
1245
1276
  // 落盘成功,清除 pending 槽位
1246
1277
  if (groupId && keystore && typeof keystore.clearPendingGroupBind === 'function') {
1278
+ keystore.clearPendingGroupBind(pendingKey);
1247
1279
  keystore.clearPendingGroupBind(groupId);
1248
1280
  }
1249
1281
  return result;
@@ -1333,7 +1365,21 @@ export class AUNClient {
1333
1365
  throw new ValidationError(`renewGroupAid: cannot determine old public key for ${groupAid}`);
1334
1366
  }
1335
1367
  // 生成新密钥
1336
- const newKeyPair = new CryptoProvider().generateIdentity();
1368
+ const keystore = store._keystore;
1369
+ const pendingKey = `renew:${groupId}`;
1370
+ let newKeyPair = null;
1371
+ if (keystore && typeof keystore.loadPendingGroupBind === 'function') {
1372
+ newKeyPair = keystore.loadPendingGroupBind(pendingKey);
1373
+ }
1374
+ if (!newKeyPair || !newKeyPair.public_key_der_b64 || !newKeyPair.private_key_pem) {
1375
+ newKeyPair = new CryptoProvider().generateIdentity();
1376
+ if (keystore && typeof keystore.savePendingGroupBind === 'function') {
1377
+ keystore.savePendingGroupBind(pendingKey, newKeyPair);
1378
+ }
1379
+ }
1380
+ if (!newKeyPair || !newKeyPair.public_key_der_b64 || !newKeyPair.private_key_pem) {
1381
+ throw new ValidationError('renewGroupAid: failed to generate or load key pair');
1382
+ }
1337
1383
  const newPublicKey = newKeyPair.public_key_der_b64;
1338
1384
  const newPrivateKey = newKeyPair.private_key_pem;
1339
1385
  const curve = newKeyPair.curve || 'P-256';
@@ -1389,6 +1435,9 @@ export class AUNClient {
1389
1435
  throw new ValidationError(`renewGroupAid failed to persist group identity: ${imported.error.message}`);
1390
1436
  }
1391
1437
  await this._uploadGroupAgentMd(store, returnedGroupAid, payload, group);
1438
+ if (keystore && typeof keystore.clearPendingGroupBind === 'function') {
1439
+ keystore.clearPendingGroupBind(pendingKey);
1440
+ }
1392
1441
  return result;
1393
1442
  }
1394
1443
  async completeGroupTransfer(params = {}, options = {}) {
@@ -1399,12 +1448,26 @@ export class AUNClient {
1399
1448
  if (!store) {
1400
1449
  throw new ValidationError('completeGroupTransfer requires aidStore');
1401
1450
  }
1402
- const keyPair = new CryptoProvider().generateIdentity();
1403
1451
  const payload = { ...params };
1404
1452
  const groupId = String(params.group_id ?? '').trim();
1405
1453
  if (!groupId) {
1406
1454
  throw new ValidationError('completeGroupTransfer requires group_id');
1407
1455
  }
1456
+ const pendingKey = `complete:${groupId}`;
1457
+ const keystore = store._keystore;
1458
+ let keyPair = null;
1459
+ if (keystore && typeof keystore.loadPendingGroupBind === 'function') {
1460
+ keyPair = keystore.loadPendingGroupBind(pendingKey);
1461
+ }
1462
+ if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1463
+ keyPair = new CryptoProvider().generateIdentity();
1464
+ if (keystore && typeof keystore.savePendingGroupBind === 'function') {
1465
+ keystore.savePendingGroupBind(pendingKey, keyPair);
1466
+ }
1467
+ }
1468
+ if (!keyPair || !keyPair.public_key_der_b64 || !keyPair.private_key_pem) {
1469
+ throw new ValidationError('completeGroupTransfer: failed to generate or load key pair');
1470
+ }
1408
1471
  let groupAid = String(params.group_aid ?? '').trim();
1409
1472
  if (!groupAid) {
1410
1473
  const info = await this.call('group.get_info', { group_id: groupId, required: ['member'] });
@@ -1461,6 +1524,9 @@ export class AUNClient {
1461
1524
  throw new ValidationError(imported.error.message);
1462
1525
  }
1463
1526
  await this._uploadGroupAgentMd(store, returnedGroupAid, payload, group);
1527
+ if (keystore && typeof keystore.clearPendingGroupBind === 'function') {
1528
+ keystore.clearPendingGroupBind(pendingKey);
1529
+ }
1464
1530
  return result;
1465
1531
  }
1466
1532
  /** 证书公钥校验:确保服务端返回的证书公钥 == 本地公钥(DER base64) */
@@ -3065,6 +3131,11 @@ export class AUNClient {
3065
3131
  if (!resolvedAid) {
3066
3132
  throw new StateError('gateway discovery requires a loaded AID');
3067
3133
  }
3134
+ const discovery = this._discovery;
3135
+ const tokenStore = this._tokenStore;
3136
+ const discoveryIsCurrent = () => (this._discovery === discovery
3137
+ && this._tokenStore === tokenStore
3138
+ && !this._closing);
3068
3139
  if (this._gatewayCandidates.length > 0)
3069
3140
  return [...this._gatewayCandidates];
3070
3141
  if (this._gatewayUrl) {
@@ -3072,9 +3143,9 @@ export class AUNClient {
3072
3143
  return [...this._gatewayCandidates];
3073
3144
  }
3074
3145
  try {
3075
- const loadMetadata = this._tokenStore.loadMetadata;
3146
+ const loadMetadata = tokenStore.loadMetadata;
3076
3147
  const metadata = typeof loadMetadata === 'function'
3077
- ? loadMetadata.call(this._tokenStore, resolvedAid)
3148
+ ? loadMetadata.call(tokenStore, resolvedAid)
3078
3149
  : null;
3079
3150
  const cachedList = isJsonObject(metadata) && Array.isArray(metadata.gateway_urls)
3080
3151
  ? metadata.gateway_urls.map((item) => String(item ?? '').trim()).filter(Boolean)
@@ -3101,12 +3172,13 @@ export class AUNClient {
3101
3172
  try {
3102
3173
  // discoverAll 是生产路径;若调用方注入了旧版/测试 discovery
3103
3174
  // 替身而只提供 discover,则保留单候选兼容,不影响多候选发现。
3104
- const discovery = this._discovery;
3105
3175
  const hasInjectedDiscover = Object.prototype.hasOwnProperty.call(discovery, 'discover')
3106
3176
  && typeof discovery.discover === 'function';
3107
3177
  const discovered = hasInjectedDiscover
3108
3178
  ? [await discovery.discover(url)]
3109
3179
  : await discovery.discoverAll(url);
3180
+ if (!discoveryIsCurrent())
3181
+ throw new StateError('gateway discovery superseded by identity change');
3110
3182
  const gatewayUrls = [...new Set(discovered.map((item) => String(item ?? '').trim()).filter(Boolean))];
3111
3183
  if (gatewayUrls.length === 0)
3112
3184
  throw new ValidationError('gateway discovery returned no candidates');
@@ -3114,9 +3186,9 @@ export class AUNClient {
3114
3186
  const gateway = gatewayUrls[0];
3115
3187
  this._gatewayUrl = gateway;
3116
3188
  try {
3117
- const saveMetadata = this._tokenStore.saveMetadata;
3189
+ const saveMetadata = tokenStore.saveMetadata;
3118
3190
  if (typeof saveMetadata === 'function') {
3119
- saveMetadata.call(this._tokenStore, resolvedAid, {
3191
+ saveMetadata.call(tokenStore, resolvedAid, {
3120
3192
  gateway_url: gateway,
3121
3193
  gateway_urls: gatewayUrls,
3122
3194
  gateway_cached_at: Date.now(),
@@ -3129,6 +3201,8 @@ export class AUNClient {
3129
3201
  return [...gatewayUrls];
3130
3202
  }
3131
3203
  catch (err) {
3204
+ if (!discoveryIsCurrent())
3205
+ throw new StateError('gateway discovery superseded by identity change');
3132
3206
  lastErr = err;
3133
3207
  this._clientLog.warn(`gateway discovery failed: aid=${resolvedAid} url=${url} err=${formatCaughtError(err)}`);
3134
3208
  }
@@ -3610,6 +3684,8 @@ export class AUNClient {
3610
3684
  if (!owner && !task && !this._reconnectActive)
3611
3685
  return;
3612
3686
  owner?.abort();
3687
+ this._discovery?._cancelPending?.();
3688
+ this._auth?._cancelPending?.();
3613
3689
  this._stopBackgroundTasks();
3614
3690
  try {
3615
3691
  await this._transport.close();