@agentunion/fastaun 0.5.19 → 0.5.21

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 (129) hide show
  1. package/CHANGELOG.md +176 -78
  2. package/_packed_docs/CHANGELOG.md +176 -78
  3. package/_packed_docs/INDEX.md +288 -269
  4. package/_packed_docs/KITE_DOCS_GUIDE.md +101 -95
  5. package/_packed_docs/agent.md/SCHEMA.md +7 -7
  6. package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/344/270/216/346/215/242owner/347/274/226/346/216/222/346/226/271/346/241/210.md +530 -0
  7. package/_packed_docs/agent.md/working/AID/347/261/273/345/236/213-owner/345/205/263/347/263/273/345/256/214/346/225/264/350/256/276/350/256/241/344/270/216/345/256/236/346/226/275/350/256/241/345/210/222.md +685 -0
  8. package/_packed_docs/agent.md/working/agent.md-schema/344/270/216/347/255/276/345/217/221/351/252/214/347/255/276/346/265/201/347/250/213/346/276/204/346/270/205.md +349 -0
  9. package/_packed_docs/agent.md/working//346/234/254/345/234/260Docker/346/265/213/350/257/225Bug/346/270/205/345/215/225.md +40 -0
  10. package/_packed_docs/agent.md//350/277/234/347/250/213agent.md/347/274/223/345/255/230/344/270/216etag/351/200/217/344/274/240/346/226/271/346/241/210.md +11 -11
  11. package/_packed_docs/protocol/06-/346/234/215/345/212/241/345/215/217/350/256/256.md +372 -331
  12. package/_packed_docs/protocol/08-AUN-E2EE.md +17 -12
  13. package/_packed_docs/protocol/10-Group-/345/255/220/345/215/217/350/256/256.md +676 -676
  14. package/_packed_docs/protocol/11-Storage-/345/255/220/345/215/217/350/256/256.md +8 -12
  15. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/211.md +445 -0
  16. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2112.0.md +161 -0
  17. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2112.1.md +666 -0
  18. package/_packed_docs/protocol/Ownership/345/215/217/350/256/256/345/256/232/344/271/2113.0.md +233 -0
  19. package/_packed_docs/protocol/agent.md/SCHEMA.md +1 -1
  20. package/_packed_docs/sdk/0.5.21-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +368 -0
  21. package/_packed_docs/sdk/0.5.21-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +350 -0
  22. package/_packed_docs/sdk/01-/345/277/253/351/200/237/345/274/200/345/247/213.md +1 -1
  23. package/_packed_docs/sdk/02-WebSocket/345/215/217/350/256/256.md +5 -1
  24. package/_packed_docs/sdk/03-/346/240/270/345/277/203/346/246/202/345/277/265.md +64 -64
  25. package/_packed_docs/sdk/04-/350/277/236/346/216/245/344/270/216/350/256/244/350/257/201.md +70 -63
  26. package/_packed_docs/sdk/05-E2EE/345/212/240/345/257/206/351/200/232/344/277/241.md +43 -40
  27. package/_packed_docs/sdk/06-API/346/211/213/345/206/214.md +179 -19
  28. package/_packed_docs/sdk/08-/346/234/200/344/275/263/345/256/236/350/267/265.md +50 -49
  29. package/_packed_docs/sdk/09-group-rpc-manual.md +261 -254
  30. package/_packed_docs/sdk/09-message-rpc-manual.md +253 -160
  31. package/_packed_docs/sdk/09-storage-rpc-manual.md +18 -26
  32. package/_packed_docs/sdk/09-/345/207/255/350/257/201/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +71 -0
  33. package/_packed_docs/sdk/09-/347/273/204/347/273/207/346/234/215/345/212/241RPC/346/211/213/345/206/214.md +88 -0
  34. package/_packed_docs/sdk/AUN_DOCS_GUIDE.md +105 -92
  35. package/_packed_docs/sdk/INDEX.md +197 -167
  36. package/_packed_docs/sdk/Ownership-SDK/345/256/236/347/216/260/346/214/207/345/215/227.md +169 -0
  37. package/_packed_docs/sdk/Ownership-SDK/347/274/226/346/216/222/346/214/207/345/215/227.md +349 -0
  38. package/_packed_docs/sdk/Ownership-SDK/347/274/226/346/216/222/346/214/207/345/215/2272.1.md +930 -0
  39. package/_packed_docs/sdk/Ownership-SDK/351/233/206/346/210/220/346/214/207/345/215/227.md +270 -0
  40. package/_packed_docs/sdk/Ownership2.1-SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +363 -0
  41. package/_packed_docs/sdk/Ownership2.1-SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +553 -0
  42. package/_packed_docs/sdk/Ownership2.1-TDD/345/210/206/351/230/266/346/256/265/345/256/236/346/226/275/350/256/241/345/210/222.md +708 -0
  43. package/_packed_docs/sdk/Ownership2.1/350/257/246/347/273/206/350/256/276/350/256/241.md +480 -0
  44. package/_packed_docs/sdk/Ownership/347/256/241/347/220/206/344/270/223/351/242/230.md +559 -0
  45. package/_packed_docs/sdk/ProtectedHeaders/347/224/250/346/263/225/344/270/223/351/242/230.md +299 -0
  46. package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/272/224/347/224/250/351/233/206/346/210/220/346/214/207/345/215/227.md +173 -0
  47. package/_packed_docs/sdk//345/207/255/350/257/201/344/270/216/347/273/204/347/273/207/346/234/215/345/212/241SDK/345/274/200/345/217/221/344/270/216/347/247/273/346/244/215/346/214/207/345/215/227.md +211 -0
  48. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273SDK/345/256/236/347/216/260/347/247/273/346/244/215/346/214/207/345/215/227.md +331 -0
  49. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273SDK/351/233/206/346/210/220/346/214/207/345/215/227.md +207 -0
  50. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273/344/273/243/347/240/201/345/256/241/346/237/245/346/212/245/345/221/212.md +252 -0
  51. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273/345/256/236/346/226/275/350/256/241/345/210/222.md +156 -0
  52. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273/346/226/271/346/241/210.md +273 -0
  53. package/_packed_docs//350/267/250/350/256/276/345/244/207/345/267/262/350/257/273/346/234/252/350/257/273/350/257/246/347/273/206/350/256/276/350/256/241.md +211 -0
  54. package/dist/agent-md-schema.d.ts +13 -1
  55. package/dist/agent-md-schema.js +63 -33
  56. package/dist/agent-md-schema.js.map +1 -1
  57. package/dist/agent-md.d.ts +30 -3
  58. package/dist/agent-md.js +293 -152
  59. package/dist/agent-md.js.map +1 -1
  60. package/dist/aid-store.d.ts +42 -0
  61. package/dist/aid-store.js +224 -44
  62. package/dist/aid-store.js.map +1 -1
  63. package/dist/aid.js +18 -13
  64. package/dist/aid.js.map +1 -1
  65. package/dist/auth.js +78 -2
  66. package/dist/auth.js.map +1 -1
  67. package/dist/cert-utils.d.ts +44 -1
  68. package/dist/cert-utils.js +157 -56
  69. package/dist/cert-utils.js.map +1 -1
  70. package/dist/client/delivery.d.ts +68 -1
  71. package/dist/client/delivery.js +1196 -135
  72. package/dist/client/delivery.js.map +1 -1
  73. package/dist/client/rpc-pipeline.d.ts +5 -1
  74. package/dist/client/rpc-pipeline.js +240 -111
  75. package/dist/client/rpc-pipeline.js.map +1 -1
  76. package/dist/client/v2-e2ee.d.ts +12 -2
  77. package/dist/client/v2-e2ee.js +842 -379
  78. package/dist/client/v2-e2ee.js.map +1 -1
  79. package/dist/client.d.ts +26 -2
  80. package/dist/client.js +420 -75
  81. package/dist/client.js.map +1 -1
  82. package/dist/config.d.ts +4 -0
  83. package/dist/config.js +1 -0
  84. package/dist/config.js.map +1 -1
  85. package/dist/events.d.ts +23 -4
  86. package/dist/events.js +171 -15
  87. package/dist/events.js.map +1 -1
  88. package/dist/facades.d.ts +43 -1
  89. package/dist/facades.js +368 -12
  90. package/dist/facades.js.map +1 -1
  91. package/dist/index.d.ts +7 -4
  92. package/dist/index.js +1 -1
  93. package/dist/index.js.map +1 -1
  94. package/dist/keystore/aid-db.d.ts +7 -2
  95. package/dist/keystore/aid-db.js +194 -43
  96. package/dist/keystore/aid-db.js.map +1 -1
  97. package/dist/keystore/index.d.ts +15 -1
  98. package/dist/keystore/local-token-store.d.ts +10 -2
  99. package/dist/keystore/local-token-store.js +81 -16
  100. package/dist/keystore/local-token-store.js.map +1 -1
  101. package/dist/org-protocol.d.ts +45 -0
  102. package/dist/org-protocol.js +829 -22
  103. package/dist/org-protocol.js.map +1 -1
  104. package/dist/ownership-v21.d.ts +40 -9
  105. package/dist/ownership-v21.js +174 -79
  106. package/dist/ownership-v21.js.map +1 -1
  107. package/dist/ownership-workflow.d.ts +22 -0
  108. package/dist/ownership-workflow.js +861 -66
  109. package/dist/ownership-workflow.js.map +1 -1
  110. package/dist/secret-store/file-store.js +84 -0
  111. package/dist/secret-store/file-store.js.map +1 -1
  112. package/dist/seq-tracker.d.ts +1 -0
  113. package/dist/seq-tracker.js +2 -2
  114. package/dist/seq-tracker.js.map +1 -1
  115. package/dist/tools/cross-sdk-agent.d.ts +67 -3
  116. package/dist/tools/cross-sdk-agent.js +575 -42
  117. package/dist/tools/cross-sdk-agent.js.map +1 -1
  118. package/dist/transport.d.ts +4 -0
  119. package/dist/transport.js +108 -44
  120. package/dist/transport.js.map +1 -1
  121. package/dist/types.d.ts +1 -0
  122. package/dist/v2/e2ee/encrypt-group.js +1 -0
  123. package/dist/v2/e2ee/encrypt-group.js.map +1 -1
  124. package/dist/v2/e2ee/encrypt-p2p.js +1 -0
  125. package/dist/v2/e2ee/encrypt-p2p.js.map +1 -1
  126. package/dist/v2/e2ee/types.d.ts +1 -0
  127. package/dist/version.d.ts +1 -1
  128. package/dist/version.js +1 -1
  129. package/package.json +1 -1
package/dist/facades.js CHANGED
@@ -1,9 +1,320 @@
1
1
  import { GroupFSVFS } from './group-fs.js';
2
2
  import { GROUP_INDEX_KEY, groupIndexSettingEtag, parseGroupIndex, prepareGroupSettingsWithIndex, verifyGroupIndex, } from './group-index.js';
3
- import { certMatchesFingerprint } from './cert-utils.js';
4
- import { createCredential, createMemberCredential, createMembershipBinding, signMembershipBinding, prepareOrganizationRequest, signTransferAcceptance, verifyCredential, verifyCredentialSignature, verifyRegistrationReceipt } from './org-protocol.js';
3
+ import { certMatchesFingerprint, publicKeyCurve, publicKeyDerB64 } from './cert-utils.js';
4
+ import { isJsonObject } from './types.js';
5
+ import { ValidationError } from './errors.js';
6
+ import { digest, createCredential, createMemberCredential, createMembershipBinding, signMembershipBinding, prepareOrganizationRequest, signTransferAcceptance, verifyCredential, verifyCredentialSignature, verifyRegistrationReceipt } from './org-protocol.js';
5
7
  import { normalizeGroupId } from './group-id.js';
6
8
  import { validateAIDFormat, validateGroupIDFormat } from './validators.js';
9
+ export class DebugFacade {
10
+ client;
11
+ withTrace;
12
+ tail = Promise.resolve();
13
+ constructor(client, withTrace) {
14
+ this.client = client;
15
+ this.withTrace = withTrace;
16
+ }
17
+ async diag(scope = '') {
18
+ if (scope !== null && typeof scope !== 'string')
19
+ throw new ValidationError('诊断 scope 必须是字符串');
20
+ const selected = ((scope ?? '').trim() || '*').replace(/\.\*$/, '');
21
+ const namespaceScope = (scope ?? '').trim().endsWith('.*');
22
+ const aid = this.client.aid ?? '';
23
+ const report = { scope: selected, aid, passed: 0, failed: 0, skipped: 0, checks: [] };
24
+ if (selected === 'debug' || selected.startsWith('debug.')) {
25
+ report.usage = 'client.debug.diag(scope=""): 空或 * 检查全部;namespace 或 namespace.* 检查命名空间;完整 RPC 名精确匹配;debug 显示本说明。所有服务检查均为 dry-run,不执行业务处理器。';
26
+ return report;
27
+ }
28
+ if (!aid)
29
+ throw new ValidationError('诊断需要当前客户端的 AID');
30
+ const operation = this.tail.then(() => this.withTrace(() => this.run(report, namespaceScope)));
31
+ this.tail = operation.catch(() => undefined);
32
+ return operation;
33
+ }
34
+ async run(report, namespaceScope) {
35
+ const directoryStarted = Date.now();
36
+ let methods;
37
+ try {
38
+ const listing = await this.client.call('debug.diag', { dry_run: true, action: 'list' });
39
+ if (!isJsonObject(listing) || listing.dry_run !== true || listing.executed !== false
40
+ || !Array.isArray(listing.methods) || listing.methods.some((method) => typeof method !== 'string' || !method.trim())) {
41
+ throw new ValidationError('服务端未确认诊断目录为 dry-run');
42
+ }
43
+ methods = [...new Set(listing.methods)];
44
+ }
45
+ catch (error) {
46
+ report.checks.push(this.failure('debug.diag', directoryStarted, error));
47
+ report.failed++;
48
+ return report;
49
+ }
50
+ let selected = report.scope === '*' ? methods.filter((method) => !method.startsWith('debug.')) : methods.includes(report.scope) ? [report.scope]
51
+ : methods.filter((method) => method.startsWith(report.scope + '.'));
52
+ if (selected.length === 0 && report.scope !== '*') {
53
+ if (report.scope.includes('.') && !namespaceScope)
54
+ selected = [report.scope];
55
+ else
56
+ throw new ValidationError(`当前服务未声明该 scope: ${report.scope}`, { data: { stage: 'gateway.catalog' } });
57
+ }
58
+ const runId = `diag-${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
59
+ for (const method of selected) {
60
+ for (const fixture of this.scenarios(method, report.aid, runId)) {
61
+ const scenario = fixture.scenario;
62
+ if (fixture.reason) {
63
+ report.checks.push({ method, scenario, status: 'skipped', duration_ms: 0, reason: fixture.reason, stage: 'sdk.fixture' });
64
+ report.skipped++;
65
+ continue;
66
+ }
67
+ const started = Date.now();
68
+ try {
69
+ const result = await this.client.call('debug.diag', { dry_run: true, method, params: fixture.params });
70
+ if (!isJsonObject(result) || result.dry_run !== true || result.executed !== false || result.method !== method
71
+ || !Array.isArray(result.stages) || result.stages.length === 0
72
+ || result.stages.some((stage) => !isJsonObject(stage) || typeof stage.stage !== 'string' || !stage.stage.trim()
73
+ || (stage.status !== 'passed' && stage.status !== 'skipped'))) {
74
+ throw new ValidationError('服务端未确认该 RPC 为 dry-run 且未执行');
75
+ }
76
+ if (result.status === 'skipped') {
77
+ report.checks.push({ method, scenario, status: 'skipped', duration_ms: Date.now() - started, result,
78
+ reason: typeof result.reason === 'string' && result.reason ? result.reason : '服务端跳过该 dry-run 检查' });
79
+ report.skipped++;
80
+ }
81
+ else {
82
+ report.checks.push({ method, scenario, status: 'passed', duration_ms: Date.now() - started, result });
83
+ report.passed++;
84
+ }
85
+ }
86
+ catch (error) {
87
+ report.checks.push({ ...this.failure(method, started, error), scenario });
88
+ report.failed++;
89
+ }
90
+ }
91
+ }
92
+ return report;
93
+ }
94
+ failure(method, started, error) {
95
+ const check = { method, status: 'failed', duration_ms: Date.now() - started, error: error instanceof Error ? error.message : String(error) };
96
+ const data = error?.data;
97
+ if (data !== undefined && data !== null)
98
+ check.data = data;
99
+ if (isJsonObject(data) && typeof data.stage === 'string')
100
+ check.stage = data.stage;
101
+ return check;
102
+ }
103
+ scenarios(method, aid, runId) {
104
+ const base = this.params(method, aid, runId);
105
+ const cases = (entries) => Object.entries(entries).map(([scenario, params]) => ({ scenario, params: { ...base, ...params } }));
106
+ if (['auth.aid_login1', 'auth.request_cert', 'ca.verify_chain'].includes(method)) {
107
+ const cert = this.client.currentAid?.certPem ?? '';
108
+ if (!cert)
109
+ return [{ scenario: 'fixture_unavailable', reason: '当前客户端内存没有可用的真实身份证书' }];
110
+ if (method === 'auth.aid_login1')
111
+ return [{ scenario: 'existing_certificate', params: { aid, cert, client_nonce: runId } }];
112
+ if (method === 'ca.verify_chain')
113
+ return [{ scenario: 'existing_certificate', params: { aid, cert_pem: cert } }];
114
+ const curve = publicKeyCurve(cert);
115
+ const publicKey = curve ? publicKeyDerB64(cert) : '';
116
+ return curve && publicKey ? [{ scenario: 'existing_public_key', params: { aid, public_key: publicKey, curve } }]
117
+ : [{ scenario: 'fixture_unavailable', reason: '当前内存证书无法提供可用公钥及曲线' }];
118
+ }
119
+ if (method === 'message.thought.put' || method === 'message.thought.get') {
120
+ return [{ scenario: 'self_unsupported', reason: '业务不支持向自身读写私聊 thought,诊断仅允许使用自身 AID' }];
121
+ }
122
+ if (method === 'message.peers')
123
+ return [{ scenario: 'peer_capability', reason: '需要服务端可信协商的 peer pull 能力及设备上下文' }];
124
+ if (['auth.verify', 'message.v2.send', 'group.v2.send', 'message.v2.put_peer_pk', 'group.v2.put_group_pk', 'group.v2.propose_state', 'group.v2.confirm_state'].includes(method)
125
+ || (method.startsWith('auth.') && (method.includes('login') || /renew|rekey|create_aid/.test(method)))
126
+ || method.endsWith('.verify_chain')) {
127
+ return [{ scenario: 'fixture_unavailable', reason: '缺少该场景所需的真实证书、签名、凭证或密钥材料,不伪造诊断材料' }];
128
+ }
129
+ if (method === 'message.send' || method === 'group.send') {
130
+ return cases({
131
+ echo: { payload: { type: 'echo', text: 'echo diag' } },
132
+ text: { payload: { type: 'echo', text: 'echo hello' } },
133
+ unicode: { payload: { type: 'echo', text: 'echo 诊断 🌍' } },
134
+ });
135
+ }
136
+ if (method === 'message.pull' || method === 'message.v2.pull') {
137
+ delete base.sender_aid;
138
+ return cases({
139
+ tail: { cursor_scope: 'global', window_mode: 'tail', after_seq: 0, limit: 1 },
140
+ page: { cursor_scope: 'global', after_seq: 0, limit: 10 },
141
+ });
142
+ }
143
+ if (method === 'group.pull' || method === 'group.v2.pull')
144
+ return cases({ tail: { after_seq: 0, window_mode: 'tail', limit: 1 }, page: { after_seq: 0, limit: 10 } });
145
+ if (method === 'message.history' || method === 'group.history')
146
+ return cases({ latest: { before_seq: 1, limit: 1 }, page: { before_seq: 1, limit: 10 } });
147
+ if (method === 'push.register')
148
+ return [
149
+ { scenario: 'basic', params: { push_server_aid: aid, push_token: 'echo' } },
150
+ { scenario: 'provider', params: { push_server_aid: aid, push_token: 'echo', provider: 'mock', app_id: 'diag' } },
151
+ ];
152
+ if (method === 'push.unregister')
153
+ return [{ scenario: 'default', params: {} }];
154
+ if (method === 'push.ack')
155
+ return ['accepted', 'retryable_failure', 'permanent_failure'].map((status) => ({
156
+ scenario: status, params: { batch_id: '0'.repeat(64), status },
157
+ }));
158
+ if (method === 'stream.create')
159
+ return cases({
160
+ plain: { metadata: { diag: runId } }, unicode: { metadata: { diag: runId, label: '诊断 🌍' } },
161
+ });
162
+ if (method === 'mail.send')
163
+ return cases({
164
+ plain: {}, ccbcc: { cc: [aid], bcc: [aid] },
165
+ attachment: { attachments: [{ filename: 'diag.txt', content_type: 'text/plain', content_b64: 'ZWNobw==', size_bytes: 4, disposition: 'attachment' }] },
166
+ });
167
+ if (method === 'mail.list')
168
+ return cases({
169
+ first_page: { mailbox: 'inbox', limit: 1 }, offset: { mailbox: 'inbox', limit: 10, offset: 1 },
170
+ cursor: { mailbox: 'inbox', limit: 1, cursor: 'eyJjcmVhdGVkX2F0IjoxLCJtZXNzYWdlX2lkIjoiZGlhZyJ9' },
171
+ });
172
+ if (method === 'mail.search')
173
+ return cases({ query: { query: 'diag', limit: 1 }, page: { query: 'diag', mailbox: 'inbox', limit: 10, offset: 1 } });
174
+ if (method === 'mail.mark')
175
+ return cases({ set_flags: { flags: { seen: true, flagged: true } }, clear_flags: { flags: { seen: false, flagged: false } } });
176
+ if (['storage.list_objects', 'storage.list_prefixes', 'group.list', 'group.get_members'].includes(method)) {
177
+ if (method === 'storage.list_prefixes' || method === 'group.list') {
178
+ return cases({ first_page: { size: 1 }, page_size: { size: 10 } });
179
+ }
180
+ return cases({ first_page: { page: 1, size: 1 }, page: { page: 2, size: 10 } });
181
+ }
182
+ if (method === 'storage.put_object' || method === 'storage.append_object')
183
+ return cases({
184
+ text: {}, unicode: { content: 'ZWNobyDor4rmlq0g8J+MjQ==', size_bytes: 16, sha256: '706dee1332856717aaa230a787a0dd496aa27661f7a6b03c5b8d6eda3b1ca3e1' },
185
+ });
186
+ return [{ scenario: 'default', params: base }];
187
+ }
188
+ params(method, aid, runId) {
189
+ if (method === 'org.lifecycle.create')
190
+ return {
191
+ org_id: aid, name: 'diag', exttype: 'company', join_mode: 'application', visibility: 'public',
192
+ owner_aid: aid, request_id: runId, nonce: runId, aad: { org_id: aid },
193
+ };
194
+ const params = { aid };
195
+ if (method.startsWith('message.') || method.startsWith('group.')) {
196
+ Object.assign(params, {
197
+ to: aid, peer_aid: aid, sender_aid: aid, aids: [aid], message_id: runId, message_ids: [runId],
198
+ encrypt: false, context: { type: 'run', id: runId }, payload: { type: 'echo', text: 'echo diag' },
199
+ });
200
+ }
201
+ if (method.startsWith('group.')) {
202
+ Object.assign(params, {
203
+ group_id: `${runId}.${aid.slice(aid.indexOf('.') + 1)}`, requester_aid: aid, sender_aid: aid, to: aid,
204
+ context: { type: 'run', id: runId }, payload: { type: 'echo', text: 'echo diag' }, encrypt: false,
205
+ });
206
+ }
207
+ else if (method.startsWith('storage.')) {
208
+ Object.assign(params, { owner_aid: aid, bucket: 'default', object_key: runId + '/echo.txt', path: runId + '/echo.txt' });
209
+ }
210
+ else if (method.startsWith('stream.')) {
211
+ Object.assign(params, { target_aid: aid, stream_id: runId, content_type: 'text/plain' });
212
+ }
213
+ else if (method.startsWith('mail.')) {
214
+ params.message_id = runId;
215
+ if (method === 'mail.send')
216
+ Object.assign(params, { to: [aid], subject: 'AUN diag', body_text: 'echo' });
217
+ if (['mail.get', 'mail.delete', 'mail.mark', 'mail.move', 'mail.get_attachment'].includes(method))
218
+ params.message_id = runId;
219
+ if (method === 'mail.move')
220
+ params.to_mailbox = 'archive';
221
+ if (method === 'mail.get_attachment')
222
+ params.attachment_id = 1;
223
+ if (method === 'mail.list')
224
+ Object.assign(params, { mailbox: 'inbox', limit: 1 });
225
+ if (method === 'mail.search')
226
+ Object.assign(params, { query: 'diag', limit: 1 });
227
+ }
228
+ if (method === 'proxy.register_services')
229
+ params.services = [{ service_name: 'diag', service_type: 'http', visibility: 'private' }];
230
+ if (method === 'event.subscribe' || method === 'event.unsubscribe')
231
+ params.events = ['message.received', 'group.message_created', 'read_changed'];
232
+ if (method === 'proxy.unregister_services')
233
+ params.service_names = ['diag'];
234
+ if (method.startsWith('auth.'))
235
+ Object.assign(params, { all: true, token: runId, refresh_token: runId, cert_sn: 'root' });
236
+ if (method.startsWith('ca.'))
237
+ params.cert_serial = '0';
238
+ if (method === 'ca.ct_get_entry')
239
+ params.log_id = runId;
240
+ if (method.startsWith('credential.'))
241
+ Object.assign(params, {
242
+ request_id: runId, nonce: runId, registrar_aid: aid, credential_cid: runId, reason_code: 'request_failed', include_chain: true, limit: 1,
243
+ });
244
+ if (method === 'credential.revoke')
245
+ Object.assign(params, { cascade: true, reason: 'diag' });
246
+ if (method === 'credential.revocationList')
247
+ params.cursor = '0';
248
+ if (method.startsWith('org.')) {
249
+ Object.assign(params, {
250
+ org_id: aid, member_aid: aid, owner_aid: aid, target_member_aid: aid, targets: [aid], aad: { org_id: aid },
251
+ expected_version: 1, page: 1, size: 1, request_id: runId, nonce: runId, join_request_id: runId, invite_id: runId,
252
+ });
253
+ if (method.endsWith('.update'))
254
+ params.patch = { name: 'diag' };
255
+ if (method.endsWith('.offboard'))
256
+ params.action = 'cancel';
257
+ if (method.endsWith('.ban'))
258
+ params.duration = 1;
259
+ if (method === 'org.authz.check')
260
+ params.target_method = 'org.member.get';
261
+ if (method === 'org.audit.query')
262
+ Object.assign(params, { limit: 1, fields: ['event_id'] });
263
+ if (method.startsWith('org.membership.'))
264
+ Object.assign(params, { membership_id: runId, slot_request_id: runId });
265
+ if (method === 'org.credential.getStatus')
266
+ params.credential_cid = runId;
267
+ if (method === 'org.lifecycle.transfer')
268
+ params.expires_at = Math.floor(Date.now() / 1000) + 300;
269
+ if (method === 'org.lifecycle.transferAccept' || method === 'org.lifecycle.transfer_accept')
270
+ Object.assign(params, { transfer_id: runId, stage: 'finalize' });
271
+ }
272
+ if (method.startsWith('message.') || method.startsWith('group.')) {
273
+ Object.assign(params, { before_seq: 1, after_seq: 0, limit: 1, up_to_seq: 1, msg_seq: 1 });
274
+ if (method.startsWith('message.')) {
275
+ Object.assign(params, { cursor_scope: 'global', member_aids: [aid] });
276
+ if (['message.pull', 'message.v2.pull', 'message.ack', 'message.v2.ack', 'message.history'].includes(method))
277
+ delete params.sender_aid;
278
+ }
279
+ if (method.endsWith('.read')) {
280
+ const state = { scope: method.startsWith('group.') ? 'group' : 'p2p', topic_key: runId, last_read_message_id: runId };
281
+ if (method.startsWith('group.'))
282
+ state.group_id = params.group_id;
283
+ params.states = [state];
284
+ }
285
+ if (method.endsWith('.read_state'))
286
+ Object.assign(params, { scope: method.startsWith('group.') ? 'group' : 'p2p', topic_key: runId });
287
+ if (method === 'group.set_settings')
288
+ params.settings = {};
289
+ if (method === 'group.update')
290
+ params.metadata = {};
291
+ if (method === 'group.ack_events')
292
+ Object.assign(params, { event_seq: 1, up_to_event_seq: 1 });
293
+ if (method.startsWith('group.fs.')) {
294
+ const path = aid + ':/groupdata/' + runId;
295
+ Object.assign(params, { path, src: path, dst: path + '-copy', grantee_aid: 'role:member', perms: 'rw', visibility: 'private' });
296
+ }
297
+ }
298
+ if (method.startsWith('storage.')) {
299
+ const path = runId + '/echo.txt';
300
+ Object.assign(params, {
301
+ content: 'ZWNobw==', size_bytes: 4, sha256: '092c79e8f80e559e404bcf660c48f3522b67aba9ff1484b0367e1a4ddef7431d',
302
+ conflict_policy: 'reject', name: 'diag', new_name: 'diag-renamed', target: path, new_target: path, expected_type: 'any',
303
+ grantee_aid: aid, perms: 'r', visibility: 'private', operation: 'read', share_id: 'diag-share',
304
+ });
305
+ if (method === 'storage.create_folder' || method === 'storage.fs.mkdir')
306
+ params.path = runId;
307
+ }
308
+ if (method.startsWith('collab.')) {
309
+ const root = aid + ':/' + runId;
310
+ Object.assign(params, {
311
+ collab_root: root, doc: 'echo.txt', source: 'ZWNobw==', onto: 1, rev: 1, src: root, dest: root + '-clone',
312
+ group_aid: aid, grantee_aid: aid, perms: 'w', version: '1.0.0', version_a: '1.0.0', version_b: '1.0.1', from: 1, to: 1,
313
+ });
314
+ }
315
+ return params;
316
+ }
317
+ }
7
318
  const INDEXED_DOCUMENT_SETTING_KEY_NAME_RE = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
8
319
  const INDEXED_DOCUMENT_SETTING_RESERVED_BASES = new Set([
9
320
  'join',
@@ -25,8 +336,8 @@ export function stripNil(params = {}, keepNull = false) {
25
336
  }
26
337
  return out;
27
338
  }
28
- function splitAidStore(params) {
29
- const out = stripNil(params ?? {});
339
+ function splitAidStore(params, keepNull = false) {
340
+ const out = stripNil(params ?? {}, keepNull);
30
341
  const aidStore = out.aidStore ?? out.aid_store;
31
342
  delete out.aidStore;
32
343
  delete out.aid_store;
@@ -81,6 +392,7 @@ export class MessageThoughtFacade extends RpcFacade {
81
392
  }
82
393
  }
83
394
  export class MessageFacade extends RpcFacade {
395
+ peers(params) { return this.call('message.peers', params); }
84
396
  _thought;
85
397
  get thought() {
86
398
  if (!this._thought)
@@ -684,12 +996,30 @@ export class GroupFacade extends RpcFacade {
684
996
  getOnlineMembers(params) { return this.call('group.get_online_members', params); }
685
997
  }
686
998
  export class CredentialFacade extends RpcFacade {
687
- create(options, signer) { return createCredential(options, signer); }
999
+ async current(input) {
1000
+ return this.client._credentialCurrent ? this.client._credentialCurrent(input) : this.client._currentCredential?.(input) ?? null;
1001
+ }
1002
+ create(options, signer) {
1003
+ const credential = createCredential(options, signer);
1004
+ this.client._rememberCredential?.(credential, { issued: true, long_term: !('exp' in options) }, signer.certPem);
1005
+ return credential;
1006
+ }
688
1007
  verifySignature(credential, resolveCertificate, now, audience) {
689
1008
  verifyCredentialSignature(credential, resolveCertificate, now, audience);
1009
+ if (isJsonObject(credential))
1010
+ this.client._rememberCredential?.(credential, {}, resolveCertificate(String(credential.signature.cert_fingerprint)) ?? undefined);
690
1011
  return { signature_valid: true, status_checked: false };
691
1012
  }
692
- verify(credential, context) { return verifyCredential(credential, context); }
1013
+ verify(credential, context) {
1014
+ const ensureCurrent = isJsonObject(credential) ? this.client._assertCredentialNotRevoked?.(credential, context.proofChain) : undefined;
1015
+ const result = verifyCredential(credential, context);
1016
+ if (isJsonObject(credential))
1017
+ this.client._assertCredentialNotRevoked?.(credential, context.proofChain);
1018
+ if (isJsonObject(credential))
1019
+ this.client._rememberCredential?.(credential, { membership_binding: context.membershipBinding }, context.resolveCertificate(String(credential.signature.cert_fingerprint)) ?? undefined);
1020
+ ensureCurrent?.();
1021
+ return result;
1022
+ }
693
1023
  verifyRegistrationReceipt(receipt, credential, request, resolveCertificate, expectedServiceAid, now) {
694
1024
  verifyRegistrationReceipt(receipt, credential, request, resolveCertificate, expectedServiceAid, now ?? Math.trunc(Date.now() / 1000));
695
1025
  return { signature_valid: true, request_bound: true };
@@ -702,7 +1032,7 @@ export class CredentialFacade extends RpcFacade {
702
1032
  }
703
1033
  export class OrgLifecycleFacade extends RpcFacade {
704
1034
  create(params) {
705
- const split = splitAidStore(params);
1035
+ const split = splitAidStore(params, true);
706
1036
  return split.aidStore && this.client.createOrg ? this.client.createOrg(split.params, { aidStore: split.aidStore }) : this.call('org.lifecycle.create', split.params);
707
1037
  }
708
1038
  get(params) { return this.call('org.lifecycle.get', params); }
@@ -711,12 +1041,12 @@ export class OrgLifecycleFacade extends RpcFacade {
711
1041
  suspend(params) { return this.call('org.lifecycle.suspend', params); }
712
1042
  resume(params) { return this.call('org.lifecycle.resume', params); }
713
1043
  dissolve(params) {
714
- const split = splitAidStore(params);
1044
+ const split = splitAidStore(params, true);
715
1045
  return split.aidStore && this.client.dissolveOrg ? this.client.dissolveOrg(split.params, { aidStore: split.aidStore }) : this.call('org.lifecycle.dissolve', split.params);
716
1046
  }
717
1047
  transfer(params) { return this.call('org.lifecycle.transfer', params); }
718
1048
  transferAccept(params) {
719
- const split = splitAidStore(params);
1049
+ const split = splitAidStore(params, true);
720
1050
  return split.aidStore && this.client.completeOrgTransfer ? this.client.completeOrgTransfer(split.params, { aidStore: split.aidStore }) : this.call('org.lifecycle.transferAccept', split.params);
721
1051
  }
722
1052
  signTransferAcceptance(proposal, signer) { return signTransferAcceptance(proposal, signer); }
@@ -724,10 +1054,19 @@ export class OrgLifecycleFacade extends RpcFacade {
724
1054
  export class OrgMemberFacade extends RpcFacade {
725
1055
  createBinding(options, memberSigner, orgSigner) { return createMembershipBinding(options, memberSigner, orgSigner); }
726
1056
  signBinding(binding, signer, party) { return signMembershipBinding(binding, signer, party); }
727
- createCredential(binding, signer, options) { return createMemberCredential(binding, signer, options); }
1057
+ createCredential(binding, signer, options) {
1058
+ const credential = createMemberCredential(binding, signer, options);
1059
+ this.client._rememberCredential?.(credential, { issued: true, long_term: !('exp' in options), membership_binding: binding }, signer.certPem);
1060
+ return credential;
1061
+ }
728
1062
  list(params) { return this.call('org.member.list', params); }
729
1063
  get(params) { return this.call('org.member.get', params); }
730
- join(params) { return this.call('org.member.join', params); }
1064
+ async join(params) {
1065
+ const application = params && isJsonObject(params.membership_binding) && isJsonObject(params.terms)
1066
+ ? structuredClone({ terms: params.terms, membership_binding: params.membership_binding }) : undefined;
1067
+ const result = await this.call('org.member.join', params);
1068
+ return application && isJsonObject(result) ? { ...result, application } : result;
1069
+ }
731
1070
  approveJoin(params) { return this.call('org.member.approveJoin', params); }
732
1071
  renewCredential(params) { return this.call('org.member.renewCredential', params); }
733
1072
  rejectJoin(params) { return this.call('org.member.rejectJoin', params); }
@@ -767,7 +1106,24 @@ export class OrgFacade extends RpcFacade {
767
1106
  this.audit = new OrgAuditFacade(client);
768
1107
  this.authz = new OrgAuthzFacade(client);
769
1108
  }
770
- prepareRequest(method, params, target, signer, options = {}) { return prepareOrganizationRequest(method, params, target, signer, options); }
1109
+ prepareRequest(method, params, target, signer, options = {}) {
1110
+ if (!('request_binding' in params) && !('signature' in params) && !('client_signature' in params) && isJsonObject(params.credentials)) {
1111
+ let credentials = params.credentials;
1112
+ for (const key of ['member_credential', 'approval_credential']) {
1113
+ const credential = credentials[key];
1114
+ if (!isJsonObject(credential))
1115
+ continue;
1116
+ const current = this.client._currentCredential?.(credential);
1117
+ if (!current?.credential || current.credential.cid === credential.cid)
1118
+ continue;
1119
+ if (Array.isArray(params.proof_chain) && Array.isArray(credential.proof_chain) && digest(params.proof_chain) === digest(credential.proof_chain))
1120
+ params = { ...params, proof_chain: current.credential.proof_chain };
1121
+ credentials = { ...credentials, [key]: current.credential, ...(current.membership_binding ? { membership_binding: current.membership_binding } : {}) };
1122
+ params = { ...params, credentials };
1123
+ }
1124
+ }
1125
+ return prepareOrganizationRequest(method, params, target, signer, options);
1126
+ }
771
1127
  health(params) { return this.call('org.health', params); }
772
1128
  status(params) { return this.call('org.status', params); }
773
1129
  }