@agirails/sdk 2.2.2 → 2.3.0
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/README.md +20 -23
- package/dist/ACTPClient.d.ts +7 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +56 -1
- package/dist/ACTPClient.js.map +1 -1
- package/dist/abi/AgentRegistry.json +133 -0
- package/dist/adapters/X402Adapter.d.ts +34 -7
- package/dist/adapters/X402Adapter.d.ts.map +1 -1
- package/dist/adapters/X402Adapter.js +36 -8
- package/dist/adapters/X402Adapter.js.map +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/adapters/index.js.map +1 -1
- package/dist/cli/commands/diff.d.ts +11 -0
- package/dist/cli/commands/diff.d.ts.map +1 -0
- package/dist/cli/commands/diff.js +115 -0
- package/dist/cli/commands/diff.js.map +1 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +51 -2
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/publish.d.ts +11 -0
- package/dist/cli/commands/publish.d.ts.map +1 -0
- package/dist/cli/commands/publish.js +170 -0
- package/dist/cli/commands/publish.js.map +1 -0
- package/dist/cli/commands/pull.d.ts +12 -0
- package/dist/cli/commands/pull.d.ts.map +1 -0
- package/dist/cli/commands/pull.js +99 -0
- package/dist/cli/commands/pull.js.map +1 -0
- package/dist/cli/index.js +7 -0
- package/dist/cli/index.js.map +1 -1
- package/dist/config/agirailsmd.d.ts +94 -0
- package/dist/config/agirailsmd.d.ts.map +1 -0
- package/dist/config/agirailsmd.js +209 -0
- package/dist/config/agirailsmd.js.map +1 -0
- package/dist/config/networks.d.ts +2 -0
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +10 -4
- package/dist/config/networks.js.map +1 -1
- package/dist/config/publishPipeline.d.ts +61 -0
- package/dist/config/publishPipeline.d.ts.map +1 -0
- package/dist/config/publishPipeline.js +192 -0
- package/dist/config/publishPipeline.js.map +1 -0
- package/dist/config/syncOperations.d.ts +67 -0
- package/dist/config/syncOperations.d.ts.map +1 -0
- package/dist/config/syncOperations.js +208 -0
- package/dist/config/syncOperations.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +23 -86
- package/dist/level0/request.js.map +1 -1
- package/dist/level1/Agent.d.ts +0 -11
- package/dist/level1/Agent.d.ts.map +1 -1
- package/dist/level1/Agent.js +15 -32
- package/dist/level1/Agent.js.map +1 -1
- package/dist/registry/AgentRegistryClient.d.ts +75 -0
- package/dist/registry/AgentRegistryClient.d.ts.map +1 -0
- package/dist/registry/AgentRegistryClient.js +160 -0
- package/dist/registry/AgentRegistryClient.js.map +1 -0
- package/dist/runtime/MockRuntime.d.ts.map +1 -1
- package/dist/runtime/MockRuntime.js +3 -1
- package/dist/runtime/MockRuntime.js.map +1 -1
- package/dist/types/adapter.d.ts +39 -0
- package/dist/types/adapter.d.ts.map +1 -1
- package/dist/types/adapter.js +7 -0
- package/dist/types/adapter.js.map +1 -1
- package/dist/types/x402.d.ts +23 -0
- package/dist/types/x402.d.ts.map +1 -1
- package/dist/types/x402.js.map +1 -1
- package/dist/wallet/keystore.d.ts +16 -0
- package/dist/wallet/keystore.d.ts.map +1 -0
- package/dist/wallet/keystore.js +132 -0
- package/dist/wallet/keystore.js.map +1 -0
- package/package.json +2 -1
- package/src/ACTPClient.ts +63 -1
- package/src/abi/AgentRegistry.json +133 -0
- package/src/adapters/X402Adapter.ts +94 -16
- package/src/adapters/index.ts +9 -1
- package/src/cli/commands/diff.ts +141 -0
- package/src/cli/commands/init.ts +65 -4
- package/src/cli/commands/publish.ts +209 -0
- package/src/cli/commands/pull.ts +124 -0
- package/src/cli/index.ts +8 -0
- package/src/config/agirailsmd.ts +262 -0
- package/src/config/networks.ts +12 -4
- package/src/config/publishPipeline.ts +276 -0
- package/src/config/syncOperations.ts +279 -0
- package/src/index.ts +3 -0
- package/src/level0/request.ts +27 -88
- package/src/level1/Agent.ts +16 -32
- package/src/registry/AgentRegistryClient.ts +202 -0
- package/src/runtime/MockRuntime.ts +3 -1
- package/src/types/adapter.ts +14 -0
- package/src/types/x402.ts +32 -0
- package/src/wallet/keystore.ts +119 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AGIRAILS.md Parser + Canonical Hash
|
|
3
|
+
*
|
|
4
|
+
* Parses AGIRAILS.md files (YAML frontmatter + markdown body),
|
|
5
|
+
* computes deterministic canonical hashes for on-chain verification.
|
|
6
|
+
*
|
|
7
|
+
* ## Canonical Hash Algorithm
|
|
8
|
+
*
|
|
9
|
+
* 1. Parse YAML frontmatter into a plain object
|
|
10
|
+
* 2. **Strip publish metadata keys** (config_hash, published_at, config_cid, arweave_tx)
|
|
11
|
+
* — these are written back by the publish pipeline and must not affect the hash
|
|
12
|
+
* 3. Canonicalize frontmatter:
|
|
13
|
+
* - Object keys: sorted lexicographically (recursive)
|
|
14
|
+
* - Primitive arrays: sorted lexicographically by `String(value).localeCompare()`
|
|
15
|
+
* - Object arrays: order preserved (semantic ordering matters)
|
|
16
|
+
* - Date objects: converted to ISO-8601 string (`.toISOString()`)
|
|
17
|
+
* - null/undefined: preserved as-is
|
|
18
|
+
* 4. `structuredHash = keccak256(JSON.stringify(canonical))`
|
|
19
|
+
* 5. Normalize body: CRLF→LF, strip trailing whitespace per line, trim
|
|
20
|
+
* 6. `bodyHash = keccak256(normalizedBody)`
|
|
21
|
+
* 7. `configHash = keccak256(structuredHash ++ bodyHash)` (byte concatenation)
|
|
22
|
+
*
|
|
23
|
+
* @module config/agirailsmd
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { ethers } from 'ethers';
|
|
27
|
+
import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';
|
|
28
|
+
|
|
29
|
+
// ============================================================================
|
|
30
|
+
// Types
|
|
31
|
+
// ============================================================================
|
|
32
|
+
|
|
33
|
+
export interface AgirailsMdConfig {
|
|
34
|
+
/** Parsed YAML frontmatter as a plain object */
|
|
35
|
+
frontmatter: Record<string, unknown>;
|
|
36
|
+
/** Markdown body (everything after the closing ---) */
|
|
37
|
+
body: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AgirailsMdHashResult {
|
|
41
|
+
/** Final configHash = keccak256(structuredHash + bodyHash) */
|
|
42
|
+
configHash: string;
|
|
43
|
+
/** Hash of the canonical JSON representation of frontmatter */
|
|
44
|
+
structuredHash: string;
|
|
45
|
+
/** Hash of the normalized markdown body */
|
|
46
|
+
bodyHash: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Publish Metadata (excluded from hash computation)
|
|
51
|
+
// ============================================================================
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Frontmatter keys written by the publish pipeline.
|
|
55
|
+
* These are stripped before hash computation to prevent self-reference drift:
|
|
56
|
+
* publish writes config_hash back → changes frontmatter → changes hash → never in sync.
|
|
57
|
+
*/
|
|
58
|
+
export const PUBLISH_METADATA_KEYS = [
|
|
59
|
+
'config_hash',
|
|
60
|
+
'published_at',
|
|
61
|
+
'config_cid',
|
|
62
|
+
'arweave_tx',
|
|
63
|
+
'template_source',
|
|
64
|
+
] as const;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Strip publish metadata keys from a frontmatter object.
|
|
68
|
+
* Returns a shallow copy with the metadata keys removed.
|
|
69
|
+
*/
|
|
70
|
+
export function stripPublishMetadata(
|
|
71
|
+
frontmatter: Record<string, unknown>
|
|
72
|
+
): Record<string, unknown> {
|
|
73
|
+
const stripped = { ...frontmatter };
|
|
74
|
+
for (const key of PUBLISH_METADATA_KEYS) {
|
|
75
|
+
delete stripped[key];
|
|
76
|
+
}
|
|
77
|
+
return stripped;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ============================================================================
|
|
81
|
+
// Parser
|
|
82
|
+
// ============================================================================
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Parse an AGIRAILS.md file into frontmatter + body.
|
|
86
|
+
*
|
|
87
|
+
* @param content - Raw file content (string)
|
|
88
|
+
* @returns Parsed config with frontmatter object and body string
|
|
89
|
+
* @throws Error if content has no valid YAML frontmatter
|
|
90
|
+
*/
|
|
91
|
+
export function parseAgirailsMd(content: string): AgirailsMdConfig {
|
|
92
|
+
const trimmed = content.trimStart();
|
|
93
|
+
|
|
94
|
+
if (!trimmed.startsWith('---')) {
|
|
95
|
+
throw new Error('AGIRAILS.md must start with YAML frontmatter (---)');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Find closing ---
|
|
99
|
+
const closingIndex = trimmed.indexOf('\n---', 3);
|
|
100
|
+
if (closingIndex === -1) {
|
|
101
|
+
throw new Error('AGIRAILS.md frontmatter is not closed (missing closing ---)');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const yamlContent = trimmed.slice(4, closingIndex); // skip opening ---\n
|
|
105
|
+
const body = trimmed.slice(closingIndex + 4); // skip \n---
|
|
106
|
+
|
|
107
|
+
// Parse YAML
|
|
108
|
+
let frontmatter: Record<string, unknown>;
|
|
109
|
+
try {
|
|
110
|
+
frontmatter = parseYaml(yamlContent);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
113
|
+
throw new Error(`Failed to parse YAML frontmatter: ${message}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (typeof frontmatter !== 'object' || frontmatter === null) {
|
|
117
|
+
throw new Error('YAML frontmatter must be an object');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
frontmatter,
|
|
122
|
+
body: body.startsWith('\n') ? body.slice(1) : body,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ============================================================================
|
|
127
|
+
// Canonical Hash
|
|
128
|
+
// ============================================================================
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Recursively canonicalize a value for deterministic JSON serialization.
|
|
132
|
+
*
|
|
133
|
+
* - Object keys: sorted lexicographically
|
|
134
|
+
* - Primitive arrays: sorted by String(x).localeCompare()
|
|
135
|
+
* - Object arrays: order preserved
|
|
136
|
+
* - Date objects: converted to ISO-8601 string
|
|
137
|
+
* - null/undefined: preserved
|
|
138
|
+
*/
|
|
139
|
+
export function canonicalize(value: unknown): unknown {
|
|
140
|
+
if (value === null || value === undefined) {
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Handle Date objects deterministically (YAML parser may auto-create these)
|
|
145
|
+
if (value instanceof Date) {
|
|
146
|
+
return value.toISOString();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
// Canonicalize each element, then sort arrays of primitives lexicographically
|
|
151
|
+
const canonicalized = value.map(canonicalize);
|
|
152
|
+
|
|
153
|
+
// Only sort arrays of primitives (strings, numbers, booleans)
|
|
154
|
+
// Arrays of objects maintain order (e.g., onboarding questions have semantic ordering)
|
|
155
|
+
const allPrimitive = canonicalized.every(
|
|
156
|
+
(item) => typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean'
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
if (allPrimitive) {
|
|
160
|
+
return canonicalized.sort((a, b) => String(a).localeCompare(String(b)));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return canonicalized;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (typeof value === 'object') {
|
|
167
|
+
const sorted: Record<string, unknown> = {};
|
|
168
|
+
const keys = Object.keys(value as Record<string, unknown>).sort();
|
|
169
|
+
for (const key of keys) {
|
|
170
|
+
sorted[key] = canonicalize((value as Record<string, unknown>)[key]);
|
|
171
|
+
}
|
|
172
|
+
return sorted;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Normalize markdown body for deterministic hashing.
|
|
180
|
+
* - Strip trailing whitespace from each line
|
|
181
|
+
* - Ensure \n line endings
|
|
182
|
+
* - Trim leading/trailing whitespace
|
|
183
|
+
*/
|
|
184
|
+
function normalizeBody(body: string): string {
|
|
185
|
+
return body
|
|
186
|
+
.replace(/\r\n/g, '\n') // CRLF → LF
|
|
187
|
+
.replace(/\r/g, '\n') // CR → LF
|
|
188
|
+
.split('\n')
|
|
189
|
+
.map((line) => line.trimEnd()) // strip trailing whitespace per line
|
|
190
|
+
.join('\n')
|
|
191
|
+
.trim(); // trim leading/trailing
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Compute the canonical config hash from raw AGIRAILS.md content.
|
|
196
|
+
*
|
|
197
|
+
* @param content - Raw AGIRAILS.md file content
|
|
198
|
+
* @returns Hash result with configHash, structuredHash, and bodyHash
|
|
199
|
+
*/
|
|
200
|
+
export function computeConfigHash(content: string): AgirailsMdHashResult {
|
|
201
|
+
const { frontmatter, body } = parseAgirailsMd(content);
|
|
202
|
+
return computeConfigHashFromParts(frontmatter, body);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Compute the canonical config hash from parsed parts.
|
|
207
|
+
*
|
|
208
|
+
* Publish metadata keys (config_hash, published_at, config_cid, arweave_tx)
|
|
209
|
+
* are automatically stripped before hashing to prevent self-reference drift.
|
|
210
|
+
*
|
|
211
|
+
* @param frontmatter - Parsed YAML frontmatter object
|
|
212
|
+
* @param body - Markdown body string
|
|
213
|
+
* @returns Hash result with configHash, structuredHash, and bodyHash
|
|
214
|
+
*/
|
|
215
|
+
export function computeConfigHashFromParts(
|
|
216
|
+
frontmatter: Record<string, unknown>,
|
|
217
|
+
body: string
|
|
218
|
+
): AgirailsMdHashResult {
|
|
219
|
+
// Step 0: Strip publish metadata to prevent self-reference drift
|
|
220
|
+
const stripped = stripPublishMetadata(frontmatter);
|
|
221
|
+
|
|
222
|
+
// Step 1: Canonical JSON of frontmatter (with metadata stripped)
|
|
223
|
+
const canonical = canonicalize(stripped);
|
|
224
|
+
const canonicalJson = JSON.stringify(canonical);
|
|
225
|
+
const structuredHash = ethers.keccak256(ethers.toUtf8Bytes(canonicalJson));
|
|
226
|
+
|
|
227
|
+
// Step 2: Normalized body hash
|
|
228
|
+
const normalized = normalizeBody(body);
|
|
229
|
+
const bodyHash = ethers.keccak256(ethers.toUtf8Bytes(normalized));
|
|
230
|
+
|
|
231
|
+
// Step 3: Combined hash
|
|
232
|
+
const configHash = ethers.keccak256(
|
|
233
|
+
ethers.concat([ethers.getBytes(structuredHash), ethers.getBytes(bodyHash)])
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
return { configHash, structuredHash, bodyHash };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ============================================================================
|
|
240
|
+
// Serializer
|
|
241
|
+
// ============================================================================
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Serialize config back to AGIRAILS.md format.
|
|
245
|
+
*
|
|
246
|
+
* @param frontmatter - YAML frontmatter object
|
|
247
|
+
* @param body - Markdown body string
|
|
248
|
+
* @returns Complete AGIRAILS.md file content
|
|
249
|
+
*/
|
|
250
|
+
export function serializeAgirailsMd(
|
|
251
|
+
frontmatter: Record<string, unknown>,
|
|
252
|
+
body: string
|
|
253
|
+
): string {
|
|
254
|
+
const yamlStr = stringifyYaml(frontmatter, {
|
|
255
|
+
lineWidth: 120,
|
|
256
|
+
singleQuote: false,
|
|
257
|
+
}).trimEnd();
|
|
258
|
+
|
|
259
|
+
const normalizedBody = body.startsWith('\n') ? body : `\n${body}`;
|
|
260
|
+
|
|
261
|
+
return `---\n${yamlStr}\n---\n${normalizedBody}`;
|
|
262
|
+
}
|
package/src/config/networks.ts
CHANGED
|
@@ -32,9 +32,11 @@ export interface NetworkConfig {
|
|
|
32
32
|
agentRegistry?: string; // AIP-7 Agent Registry (optional until deployed)
|
|
33
33
|
identityRegistry?: string; // AIP-7 ERC-1056 DID Registry (optional until deployed)
|
|
34
34
|
archiveTreasury?: string; // AIP-7 Archive Treasury for Arweave funding (optional until deployed)
|
|
35
|
+
x402Relay?: string; // X402Relay for atomic payment fee splitting (optional until deployed)
|
|
35
36
|
};
|
|
36
37
|
eas: {
|
|
37
38
|
deliverySchemaUID: string; // AIP-4 delivery proof schema
|
|
39
|
+
configSnapshotSchemaUID?: string; // AGIRAILS.md config snapshot schema
|
|
38
40
|
};
|
|
39
41
|
gasSettings: {
|
|
40
42
|
maxFeePerGas: bigint;
|
|
@@ -70,7 +72,9 @@ export const BASE_SEPOLIA: NetworkConfig = {
|
|
|
70
72
|
// AIP-7 Identity Registry - ERC-1056 DID Registry (deployed 2026-01-09)
|
|
71
73
|
identityRegistry: '0xF64F748C7802a68Cb936a9213881fE74e83FDA97',
|
|
72
74
|
// AIP-7 Archive Treasury - Arweave funding (deployed 2026-01-09)
|
|
73
|
-
archiveTreasury: '0xeB75DE7cF5ce77ab15BB0fFa3a2A79e6aaa554B0'
|
|
75
|
+
archiveTreasury: '0xeB75DE7cF5ce77ab15BB0fFa3a2A79e6aaa554B0',
|
|
76
|
+
// X402Relay - atomic payment fee splitting (TODO: deploy and set address)
|
|
77
|
+
// x402Relay: '0x...',
|
|
74
78
|
},
|
|
75
79
|
eas: {
|
|
76
80
|
// Deployed 2025-11-23 - AIP-4 delivery proof schema
|
|
@@ -104,7 +108,9 @@ export const BASE_MAINNET: NetworkConfig = {
|
|
|
104
108
|
easSchemaRegistry: '0x4200000000000000000000000000000000000020',
|
|
105
109
|
// AIP-7 contracts
|
|
106
110
|
agentRegistry: '0xbf9Aa0FC291A06A4dFA943c3E0Ad41E7aE20DF02',
|
|
107
|
-
archiveTreasury: '0x64B8f93fef2D2E749F5E88586753343F73246012'
|
|
111
|
+
archiveTreasury: '0x64B8f93fef2D2E749F5E88586753343F73246012',
|
|
112
|
+
// X402Relay - atomic payment fee splitting (TODO: deploy and set address)
|
|
113
|
+
// x402Relay: '0x...',
|
|
108
114
|
},
|
|
109
115
|
eas: {
|
|
110
116
|
// Registered 2026-02-03
|
|
@@ -158,10 +164,12 @@ export function getNetwork(network: string): NetworkConfig {
|
|
|
158
164
|
easSchemaRegistry: config.contracts.easSchemaRegistry,
|
|
159
165
|
agentRegistry: config.contracts.agentRegistry,
|
|
160
166
|
identityRegistry: config.contracts.identityRegistry,
|
|
161
|
-
archiveTreasury: config.contracts.archiveTreasury
|
|
167
|
+
archiveTreasury: config.contracts.archiveTreasury,
|
|
168
|
+
x402Relay: config.contracts.x402Relay
|
|
162
169
|
},
|
|
163
170
|
eas: {
|
|
164
|
-
deliverySchemaUID: config.eas.deliverySchemaUID
|
|
171
|
+
deliverySchemaUID: config.eas.deliverySchemaUID,
|
|
172
|
+
configSnapshotSchemaUID: config.eas.configSnapshotSchemaUID
|
|
165
173
|
},
|
|
166
174
|
gasSettings: {
|
|
167
175
|
maxFeePerGas: config.gasSettings.maxFeePerGas,
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publish Pipeline - AGIRAILS.md → IPFS → Arweave → On-Chain
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates the full publish flow:
|
|
5
|
+
* 1. Read AGIRAILS.md → parse → compute configHash
|
|
6
|
+
* 2. Upload to Filebase (IPFS pinning)
|
|
7
|
+
* 3. Upload to Arweave (permanent storage) [optional]
|
|
8
|
+
* 4. Call AgentRegistry.publishConfig(cid, hash) on-chain
|
|
9
|
+
* 5. Update AGIRAILS.md frontmatter with config_hash and published_at
|
|
10
|
+
*
|
|
11
|
+
* @module config/publishPipeline
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
15
|
+
import { Signer, keccak256, toUtf8Bytes } from 'ethers';
|
|
16
|
+
import { parseAgirailsMd, computeConfigHash, computeConfigHashFromParts, serializeAgirailsMd } from './agirailsmd';
|
|
17
|
+
import { AgentRegistryClient } from '../registry/AgentRegistryClient';
|
|
18
|
+
import { AgentRegistry } from '../protocol/AgentRegistry';
|
|
19
|
+
import { FilebaseClient } from '../storage/FilebaseClient';
|
|
20
|
+
import { ArweaveClient } from '../storage/ArweaveClient';
|
|
21
|
+
import { ServiceDescriptor } from '../types';
|
|
22
|
+
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// Types
|
|
25
|
+
// ============================================================================
|
|
26
|
+
|
|
27
|
+
export interface PublishOptions {
|
|
28
|
+
/** Path to AGIRAILS.md file */
|
|
29
|
+
path: string;
|
|
30
|
+
/** Network name (for registry address lookup) */
|
|
31
|
+
network: string;
|
|
32
|
+
/** AgentRegistry contract address */
|
|
33
|
+
registryAddress: string;
|
|
34
|
+
/** Signer for on-chain transactions */
|
|
35
|
+
signer: Signer;
|
|
36
|
+
/** Filebase client for IPFS upload */
|
|
37
|
+
filebaseClient: FilebaseClient;
|
|
38
|
+
/** Arweave client for permanent storage (optional) */
|
|
39
|
+
arweaveClient?: ArweaveClient;
|
|
40
|
+
/** Skip Arweave upload (dev mode) */
|
|
41
|
+
skipArweave?: boolean;
|
|
42
|
+
/** Dry run - compute and show but don't execute */
|
|
43
|
+
dryRun?: boolean;
|
|
44
|
+
/** Gas settings */
|
|
45
|
+
gasSettings?: {
|
|
46
|
+
maxFeePerGas?: bigint;
|
|
47
|
+
maxPriorityFeePerGas?: bigint;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PublishResult {
|
|
52
|
+
/** IPFS CID of the uploaded AGIRAILS.md */
|
|
53
|
+
cid: string;
|
|
54
|
+
/** Canonical config hash (bytes32) */
|
|
55
|
+
configHash: string;
|
|
56
|
+
/** On-chain transaction hash */
|
|
57
|
+
txHash?: string;
|
|
58
|
+
/** Arweave transaction ID (if uploaded) */
|
|
59
|
+
arweaveTxId?: string;
|
|
60
|
+
/** Whether this was a dry run */
|
|
61
|
+
dryRun: boolean;
|
|
62
|
+
/** Whether the agent was auto-registered during this publish */
|
|
63
|
+
registered?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ============================================================================
|
|
67
|
+
// Registration Helpers
|
|
68
|
+
// ============================================================================
|
|
69
|
+
|
|
70
|
+
export const PENDING_ENDPOINT = 'https://pending.agirails.io';
|
|
71
|
+
|
|
72
|
+
/** Default values for capabilities-to-services conversion */
|
|
73
|
+
const SERVICE_DEFAULTS = {
|
|
74
|
+
schemaURI: '',
|
|
75
|
+
minPrice: 0n,
|
|
76
|
+
maxPrice: 1_000_000_000n, // 1000 USDC
|
|
77
|
+
avgCompletionTime: 3600, // 1 hour
|
|
78
|
+
metadataCID: '',
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** Max safe USDC value before BigInt conversion loses precision */
|
|
82
|
+
const MAX_SAFE_USDC = Math.floor(Number.MAX_SAFE_INTEGER / 1_000_000);
|
|
83
|
+
|
|
84
|
+
/** Validate service type format (must match contract requirements) */
|
|
85
|
+
function validateServiceType(serviceType: string, source: string): void {
|
|
86
|
+
if (!serviceType) {
|
|
87
|
+
throw new Error(`Empty service type in ${source}`);
|
|
88
|
+
}
|
|
89
|
+
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(serviceType)) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
`Invalid service type "${serviceType}" in ${source}. ` +
|
|
92
|
+
'Must be lowercase alphanumeric with hyphens (e.g., "text-generation").'
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Convert human-readable USDC to 6-decimal base units with overflow check */
|
|
98
|
+
function usdcToBaseUnits(value: number, fieldName: string): bigint {
|
|
99
|
+
if (value < 0) throw new Error(`${fieldName} cannot be negative`);
|
|
100
|
+
if (value > MAX_SAFE_USDC) throw new Error(`${fieldName} exceeds maximum safe value (${MAX_SAFE_USDC} USDC)`);
|
|
101
|
+
return BigInt(Math.round(value * 1_000_000));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Extract registration params from AGIRAILS.md frontmatter.
|
|
106
|
+
*
|
|
107
|
+
* Supports two formats:
|
|
108
|
+
* - `services`: full ServiceDescriptor objects with pricing
|
|
109
|
+
* - `capabilities`: simple string list, auto-converted with defaults
|
|
110
|
+
*
|
|
111
|
+
* @throws Error if neither services nor capabilities are present
|
|
112
|
+
*/
|
|
113
|
+
function extractRegistrationParams(
|
|
114
|
+
frontmatter: Record<string, unknown>
|
|
115
|
+
): { endpoint: string; serviceDescriptors: ServiceDescriptor[] } {
|
|
116
|
+
// Endpoint: use frontmatter field or placeholder
|
|
117
|
+
const endpoint = typeof frontmatter.endpoint === 'string' && frontmatter.endpoint
|
|
118
|
+
? frontmatter.endpoint
|
|
119
|
+
: PENDING_ENDPOINT;
|
|
120
|
+
|
|
121
|
+
// Try explicit services first
|
|
122
|
+
if (Array.isArray(frontmatter.services) && frontmatter.services.length > 0) {
|
|
123
|
+
const serviceDescriptors = (frontmatter.services as Record<string, unknown>[]).map(svc => {
|
|
124
|
+
const serviceType = String(svc.type || svc.service_type || '').trim().toLowerCase();
|
|
125
|
+
validateServiceType(serviceType, 'services');
|
|
126
|
+
|
|
127
|
+
// Parse price range: "1.0-100.0" or separate min/max
|
|
128
|
+
let minPrice = SERVICE_DEFAULTS.minPrice;
|
|
129
|
+
let maxPrice = SERVICE_DEFAULTS.maxPrice;
|
|
130
|
+
if (typeof svc.price === 'string' && svc.price.includes('-')) {
|
|
131
|
+
const [min, max] = svc.price.split('-').map(Number);
|
|
132
|
+
minPrice = usdcToBaseUnits(min, 'min_price');
|
|
133
|
+
maxPrice = usdcToBaseUnits(max, 'max_price');
|
|
134
|
+
} else {
|
|
135
|
+
if (svc.min_price !== undefined) minPrice = usdcToBaseUnits(Number(svc.min_price), 'min_price');
|
|
136
|
+
if (svc.max_price !== undefined) maxPrice = usdcToBaseUnits(Number(svc.max_price), 'max_price');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
serviceTypeHash: keccak256(toUtf8Bytes(serviceType)),
|
|
141
|
+
serviceType,
|
|
142
|
+
schemaURI: String(svc.schema_uri || svc.schemaURI || SERVICE_DEFAULTS.schemaURI),
|
|
143
|
+
minPrice,
|
|
144
|
+
maxPrice,
|
|
145
|
+
avgCompletionTime: Number(svc.avg_completion_time || svc.avgCompletionTime || SERVICE_DEFAULTS.avgCompletionTime),
|
|
146
|
+
metadataCID: String(svc.metadata_cid || svc.metadataCID || SERVICE_DEFAULTS.metadataCID),
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
return { endpoint, serviceDescriptors };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Fallback: convert capabilities list to services with defaults
|
|
153
|
+
if (Array.isArray(frontmatter.capabilities) && frontmatter.capabilities.length > 0) {
|
|
154
|
+
const serviceDescriptors = (frontmatter.capabilities as string[]).map(cap => {
|
|
155
|
+
const serviceType = String(cap).trim().toLowerCase();
|
|
156
|
+
validateServiceType(serviceType, 'capabilities');
|
|
157
|
+
return {
|
|
158
|
+
serviceTypeHash: keccak256(toUtf8Bytes(serviceType)),
|
|
159
|
+
serviceType,
|
|
160
|
+
schemaURI: SERVICE_DEFAULTS.schemaURI,
|
|
161
|
+
minPrice: SERVICE_DEFAULTS.minPrice,
|
|
162
|
+
maxPrice: SERVICE_DEFAULTS.maxPrice,
|
|
163
|
+
avgCompletionTime: SERVICE_DEFAULTS.avgCompletionTime,
|
|
164
|
+
metadataCID: SERVICE_DEFAULTS.metadataCID,
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
return { endpoint, serviceDescriptors };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
throw new Error(
|
|
171
|
+
'AGIRAILS.md must have "services" or "capabilities" in frontmatter for agent registration.\n' +
|
|
172
|
+
'Add at least one, e.g.:\n' +
|
|
173
|
+
' capabilities:\n' +
|
|
174
|
+
' - text-generation\n'
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ============================================================================
|
|
179
|
+
// Pipeline
|
|
180
|
+
// ============================================================================
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Execute the full publish pipeline.
|
|
184
|
+
*
|
|
185
|
+
* @param options - Publish configuration
|
|
186
|
+
* @returns Publish result with CID, hash, and transaction hashes
|
|
187
|
+
*/
|
|
188
|
+
export async function publishAgirailsMd(options: PublishOptions): Promise<PublishResult> {
|
|
189
|
+
const {
|
|
190
|
+
path,
|
|
191
|
+
registryAddress,
|
|
192
|
+
signer,
|
|
193
|
+
filebaseClient,
|
|
194
|
+
arweaveClient,
|
|
195
|
+
skipArweave = false,
|
|
196
|
+
dryRun = false,
|
|
197
|
+
gasSettings,
|
|
198
|
+
} = options;
|
|
199
|
+
|
|
200
|
+
// Step 1: Read and parse
|
|
201
|
+
const content = readFileSync(path, 'utf-8');
|
|
202
|
+
const { frontmatter, body } = parseAgirailsMd(content);
|
|
203
|
+
const { configHash } = computeConfigHash(content);
|
|
204
|
+
|
|
205
|
+
if (dryRun) {
|
|
206
|
+
return {
|
|
207
|
+
cid: '(dry-run)',
|
|
208
|
+
configHash,
|
|
209
|
+
dryRun: true,
|
|
210
|
+
registered: false,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Step 2: Upload raw AGIRAILS.md to IPFS via Filebase
|
|
215
|
+
// Upload the actual markdown file (not a JSON wrapper) so CID points to the real file
|
|
216
|
+
const ipfsResult = await filebaseClient.uploadBinary(
|
|
217
|
+
Buffer.from(content, 'utf-8'),
|
|
218
|
+
'text/markdown',
|
|
219
|
+
{ metadata: { type: 'agirails-config', version: '1.0' } }
|
|
220
|
+
);
|
|
221
|
+
const cid = ipfsResult.cid;
|
|
222
|
+
|
|
223
|
+
// Step 3: Upload to Arweave (optional)
|
|
224
|
+
// Arweave stores the JSON-structured form for archival querying.
|
|
225
|
+
// uploadJSON already sets Content-Type: application/json and Protocol: AGIRAILS as defaults.
|
|
226
|
+
let arweaveTxId: string | undefined;
|
|
227
|
+
if (!skipArweave && arweaveClient) {
|
|
228
|
+
const arweaveResult = await arweaveClient.uploadJSON(
|
|
229
|
+
{ frontmatter, body, _format: 'agirails.md.v1' },
|
|
230
|
+
[
|
|
231
|
+
{ name: 'Type', value: 'agent-config' },
|
|
232
|
+
{ name: 'ConfigHash', value: configHash },
|
|
233
|
+
{ name: 'IPFS-CID', value: cid },
|
|
234
|
+
]
|
|
235
|
+
);
|
|
236
|
+
arweaveTxId = arweaveResult.txId;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Step 4: Auto-register if needed, then publish on-chain
|
|
240
|
+
const registry = new AgentRegistry(registryAddress, signer, gasSettings);
|
|
241
|
+
const registryClient = new AgentRegistryClient(registryAddress, signer, gasSettings);
|
|
242
|
+
let registered = false;
|
|
243
|
+
|
|
244
|
+
const signerAddress = await signer.getAddress();
|
|
245
|
+
const profile = await registry.getAgent(signerAddress);
|
|
246
|
+
|
|
247
|
+
if (!profile) {
|
|
248
|
+
// Not registered — extract params from frontmatter and auto-register
|
|
249
|
+
const regParams = extractRegistrationParams(frontmatter);
|
|
250
|
+
await registry.registerAgent(regParams);
|
|
251
|
+
registered = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const { txHash } = await registryClient.publishConfig(cid, configHash);
|
|
255
|
+
|
|
256
|
+
// Step 5: Update frontmatter with publish metadata
|
|
257
|
+
const updatedFrontmatter = {
|
|
258
|
+
...frontmatter,
|
|
259
|
+
config_hash: configHash,
|
|
260
|
+
published_at: new Date().toISOString(),
|
|
261
|
+
config_cid: cid,
|
|
262
|
+
...(arweaveTxId ? { arweave_tx: arweaveTxId } : {}),
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const updatedContent = serializeAgirailsMd(updatedFrontmatter, body);
|
|
266
|
+
writeFileSync(path, updatedContent, 'utf-8');
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
cid,
|
|
270
|
+
configHash,
|
|
271
|
+
txHash,
|
|
272
|
+
arweaveTxId,
|
|
273
|
+
dryRun: false,
|
|
274
|
+
registered,
|
|
275
|
+
};
|
|
276
|
+
}
|