@oobe-protocol-labs/synapse-sap-sdk 0.1.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/LICENSE +21 -0
- package/README.md +882 -0
- package/dist/cjs/constants/index.js +43 -0
- package/dist/cjs/constants/index.js.map +1 -0
- package/dist/cjs/constants/limits.js +161 -0
- package/dist/cjs/constants/limits.js.map +1 -0
- package/dist/cjs/constants/programs.js +78 -0
- package/dist/cjs/constants/programs.js.map +1 -0
- package/dist/cjs/constants/seeds.js +57 -0
- package/dist/cjs/constants/seeds.js.map +1 -0
- package/dist/cjs/core/client.js +391 -0
- package/dist/cjs/core/client.js.map +1 -0
- package/dist/cjs/core/connection.js +319 -0
- package/dist/cjs/core/connection.js.map +1 -0
- package/dist/cjs/core/index.js +24 -0
- package/dist/cjs/core/index.js.map +1 -0
- package/dist/cjs/errors/index.js +334 -0
- package/dist/cjs/errors/index.js.map +1 -0
- package/dist/cjs/events/index.js +136 -0
- package/dist/cjs/events/index.js.map +1 -0
- package/dist/cjs/idl/index.js +63 -0
- package/dist/cjs/idl/index.js.map +1 -0
- package/dist/cjs/idl/synapse_agent_sap.json +9710 -0
- package/dist/cjs/index.js +147 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules/agent.js +272 -0
- package/dist/cjs/modules/agent.js.map +1 -0
- package/dist/cjs/modules/attestation.js +147 -0
- package/dist/cjs/modules/attestation.js.map +1 -0
- package/dist/cjs/modules/base.js +128 -0
- package/dist/cjs/modules/base.js.map +1 -0
- package/dist/cjs/modules/escrow.js +246 -0
- package/dist/cjs/modules/escrow.js.map +1 -0
- package/dist/cjs/modules/feedback.js +166 -0
- package/dist/cjs/modules/feedback.js.map +1 -0
- package/dist/cjs/modules/index.js +35 -0
- package/dist/cjs/modules/index.js.map +1 -0
- package/dist/cjs/modules/indexing.js +375 -0
- package/dist/cjs/modules/indexing.js.map +1 -0
- package/dist/cjs/modules/ledger.js +234 -0
- package/dist/cjs/modules/ledger.js.map +1 -0
- package/dist/cjs/modules/tools.js +319 -0
- package/dist/cjs/modules/tools.js.map +1 -0
- package/dist/cjs/modules/vault.js +410 -0
- package/dist/cjs/modules/vault.js.map +1 -0
- package/dist/cjs/pda/index.js +377 -0
- package/dist/cjs/pda/index.js.map +1 -0
- package/dist/cjs/plugin/index.js +934 -0
- package/dist/cjs/plugin/index.js.map +1 -0
- package/dist/cjs/plugin/protocols.js +282 -0
- package/dist/cjs/plugin/protocols.js.map +1 -0
- package/dist/cjs/plugin/schemas.js +831 -0
- package/dist/cjs/plugin/schemas.js.map +1 -0
- package/dist/cjs/postgres/adapter.js +715 -0
- package/dist/cjs/postgres/adapter.js.map +1 -0
- package/dist/cjs/postgres/index.js +50 -0
- package/dist/cjs/postgres/index.js.map +1 -0
- package/dist/cjs/postgres/serializers.js +381 -0
- package/dist/cjs/postgres/serializers.js.map +1 -0
- package/dist/cjs/postgres/sync.js +221 -0
- package/dist/cjs/postgres/sync.js.map +1 -0
- package/dist/cjs/postgres/types.js +44 -0
- package/dist/cjs/postgres/types.js.map +1 -0
- package/dist/cjs/registries/builder.js +414 -0
- package/dist/cjs/registries/builder.js.map +1 -0
- package/dist/cjs/registries/discovery.js +362 -0
- package/dist/cjs/registries/discovery.js.map +1 -0
- package/dist/cjs/registries/index.js +51 -0
- package/dist/cjs/registries/index.js.map +1 -0
- package/dist/cjs/registries/session.js +433 -0
- package/dist/cjs/registries/session.js.map +1 -0
- package/dist/cjs/registries/x402.js +577 -0
- package/dist/cjs/registries/x402.js.map +1 -0
- package/dist/cjs/types/accounts.js +13 -0
- package/dist/cjs/types/accounts.js.map +1 -0
- package/dist/cjs/types/common.js +13 -0
- package/dist/cjs/types/common.js.map +1 -0
- package/dist/cjs/types/enums.js +174 -0
- package/dist/cjs/types/enums.js.map +1 -0
- package/dist/cjs/types/index.js +36 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/instructions.js +92 -0
- package/dist/cjs/types/instructions.js.map +1 -0
- package/dist/cjs/utils/hash.js +58 -0
- package/dist/cjs/utils/hash.js.map +1 -0
- package/dist/cjs/utils/index.js +27 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/serialization.js +105 -0
- package/dist/cjs/utils/serialization.js.map +1 -0
- package/dist/cjs/utils/validation.js +36 -0
- package/dist/cjs/utils/validation.js.map +1 -0
- package/dist/esm/constants/index.js +29 -0
- package/dist/esm/constants/index.js.map +1 -0
- package/dist/esm/constants/limits.js +158 -0
- package/dist/esm/constants/limits.js.map +1 -0
- package/dist/esm/constants/programs.js +75 -0
- package/dist/esm/constants/programs.js.map +1 -0
- package/dist/esm/constants/seeds.js +54 -0
- package/dist/esm/constants/seeds.js.map +1 -0
- package/dist/esm/core/client.js +384 -0
- package/dist/esm/core/client.js.map +1 -0
- package/dist/esm/core/connection.js +315 -0
- package/dist/esm/core/connection.js.map +1 -0
- package/dist/esm/core/index.js +19 -0
- package/dist/esm/core/index.js.map +1 -0
- package/dist/esm/errors/index.js +325 -0
- package/dist/esm/errors/index.js.map +1 -0
- package/dist/esm/events/index.js +132 -0
- package/dist/esm/events/index.js.map +1 -0
- package/dist/esm/idl/index.js +57 -0
- package/dist/esm/idl/index.js.map +1 -0
- package/dist/esm/idl/synapse_agent_sap.json +9710 -0
- package/dist/esm/index.js +70 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/modules/agent.js +268 -0
- package/dist/esm/modules/agent.js.map +1 -0
- package/dist/esm/modules/attestation.js +143 -0
- package/dist/esm/modules/attestation.js.map +1 -0
- package/dist/esm/modules/base.js +124 -0
- package/dist/esm/modules/base.js.map +1 -0
- package/dist/esm/modules/escrow.js +242 -0
- package/dist/esm/modules/escrow.js.map +1 -0
- package/dist/esm/modules/feedback.js +162 -0
- package/dist/esm/modules/feedback.js.map +1 -0
- package/dist/esm/modules/index.js +23 -0
- package/dist/esm/modules/index.js.map +1 -0
- package/dist/esm/modules/indexing.js +371 -0
- package/dist/esm/modules/indexing.js.map +1 -0
- package/dist/esm/modules/ledger.js +230 -0
- package/dist/esm/modules/ledger.js.map +1 -0
- package/dist/esm/modules/tools.js +315 -0
- package/dist/esm/modules/tools.js.map +1 -0
- package/dist/esm/modules/vault.js +406 -0
- package/dist/esm/modules/vault.js.map +1 -0
- package/dist/esm/pda/index.js +357 -0
- package/dist/esm/pda/index.js.map +1 -0
- package/dist/esm/plugin/index.js +927 -0
- package/dist/esm/plugin/index.js.map +1 -0
- package/dist/esm/plugin/protocols.js +279 -0
- package/dist/esm/plugin/protocols.js.map +1 -0
- package/dist/esm/plugin/schemas.js +828 -0
- package/dist/esm/plugin/schemas.js.map +1 -0
- package/dist/esm/postgres/adapter.js +678 -0
- package/dist/esm/postgres/adapter.js.map +1 -0
- package/dist/esm/postgres/index.js +27 -0
- package/dist/esm/postgres/index.js.map +1 -0
- package/dist/esm/postgres/serializers.js +362 -0
- package/dist/esm/postgres/serializers.js.map +1 -0
- package/dist/esm/postgres/sync.js +217 -0
- package/dist/esm/postgres/sync.js.map +1 -0
- package/dist/esm/postgres/types.js +41 -0
- package/dist/esm/postgres/types.js.map +1 -0
- package/dist/esm/registries/builder.js +410 -0
- package/dist/esm/registries/builder.js.map +1 -0
- package/dist/esm/registries/discovery.js +358 -0
- package/dist/esm/registries/discovery.js.map +1 -0
- package/dist/esm/registries/index.js +44 -0
- package/dist/esm/registries/index.js.map +1 -0
- package/dist/esm/registries/session.js +429 -0
- package/dist/esm/registries/session.js.map +1 -0
- package/dist/esm/registries/x402.js +573 -0
- package/dist/esm/registries/x402.js.map +1 -0
- package/dist/esm/types/accounts.js +12 -0
- package/dist/esm/types/accounts.js.map +1 -0
- package/dist/esm/types/common.js +12 -0
- package/dist/esm/types/common.js.map +1 -0
- package/dist/esm/types/enums.js +171 -0
- package/dist/esm/types/enums.js.map +1 -0
- package/dist/esm/types/index.js +25 -0
- package/dist/esm/types/index.js.map +1 -0
- package/dist/esm/types/instructions.js +89 -0
- package/dist/esm/types/instructions.js.map +1 -0
- package/dist/esm/utils/hash.js +53 -0
- package/dist/esm/utils/hash.js.map +1 -0
- package/dist/esm/utils/index.js +19 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/serialization.js +98 -0
- package/dist/esm/utils/serialization.js.map +1 -0
- package/dist/esm/utils/validation.js +33 -0
- package/dist/esm/utils/validation.js.map +1 -0
- package/dist/types/constants/index.d.ts +27 -0
- package/dist/types/constants/index.d.ts.map +1 -0
- package/dist/types/constants/limits.d.ts +149 -0
- package/dist/types/constants/limits.d.ts.map +1 -0
- package/dist/types/constants/programs.d.ts +69 -0
- package/dist/types/constants/programs.d.ts.map +1 -0
- package/dist/types/constants/seeds.d.ts +61 -0
- package/dist/types/constants/seeds.d.ts.map +1 -0
- package/dist/types/core/client.d.ts +323 -0
- package/dist/types/core/client.d.ts.map +1 -0
- package/dist/types/core/connection.d.ts +279 -0
- package/dist/types/core/connection.d.ts.map +1 -0
- package/dist/types/core/index.d.ts +20 -0
- package/dist/types/core/index.d.ts.map +1 -0
- package/dist/types/errors/index.d.ts +276 -0
- package/dist/types/errors/index.d.ts.map +1 -0
- package/dist/types/events/index.d.ts +248 -0
- package/dist/types/events/index.d.ts.map +1 -0
- package/dist/types/idl/index.d.ts +70 -0
- package/dist/types/idl/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +68 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/modules/agent.d.ts +166 -0
- package/dist/types/modules/agent.d.ts.map +1 -0
- package/dist/types/modules/attestation.d.ts +96 -0
- package/dist/types/modules/attestation.d.ts.map +1 -0
- package/dist/types/modules/base.d.ts +126 -0
- package/dist/types/modules/base.d.ts.map +1 -0
- package/dist/types/modules/escrow.d.ts +151 -0
- package/dist/types/modules/escrow.d.ts.map +1 -0
- package/dist/types/modules/feedback.d.ts +105 -0
- package/dist/types/modules/feedback.d.ts.map +1 -0
- package/dist/types/modules/index.d.ts +24 -0
- package/dist/types/modules/index.d.ts.map +1 -0
- package/dist/types/modules/indexing.d.ts +200 -0
- package/dist/types/modules/indexing.d.ts.map +1 -0
- package/dist/types/modules/ledger.d.ts +150 -0
- package/dist/types/modules/ledger.d.ts.map +1 -0
- package/dist/types/modules/tools.d.ts +182 -0
- package/dist/types/modules/tools.d.ts.map +1 -0
- package/dist/types/modules/vault.d.ts +240 -0
- package/dist/types/modules/vault.d.ts.map +1 -0
- package/dist/types/pda/index.d.ts +296 -0
- package/dist/types/pda/index.d.ts.map +1 -0
- package/dist/types/plugin/index.d.ts +171 -0
- package/dist/types/plugin/index.d.ts.map +1 -0
- package/dist/types/plugin/protocols.d.ts +152 -0
- package/dist/types/plugin/protocols.d.ts.map +1 -0
- package/dist/types/plugin/schemas.d.ts +823 -0
- package/dist/types/plugin/schemas.d.ts.map +1 -0
- package/dist/types/postgres/adapter.d.ts +355 -0
- package/dist/types/postgres/adapter.d.ts.map +1 -0
- package/dist/types/postgres/index.d.ts +24 -0
- package/dist/types/postgres/index.d.ts.map +1 -0
- package/dist/types/postgres/serializers.d.ts +30 -0
- package/dist/types/postgres/serializers.d.ts.map +1 -0
- package/dist/types/postgres/sync.d.ts +132 -0
- package/dist/types/postgres/sync.d.ts.map +1 -0
- package/dist/types/postgres/types.d.ts +167 -0
- package/dist/types/postgres/types.d.ts.map +1 -0
- package/dist/types/registries/builder.d.ts +340 -0
- package/dist/types/registries/builder.d.ts.map +1 -0
- package/dist/types/registries/discovery.d.ts +333 -0
- package/dist/types/registries/discovery.d.ts.map +1 -0
- package/dist/types/registries/index.d.ts +48 -0
- package/dist/types/registries/index.d.ts.map +1 -0
- package/dist/types/registries/session.d.ts +323 -0
- package/dist/types/registries/session.d.ts.map +1 -0
- package/dist/types/registries/x402.d.ts +463 -0
- package/dist/types/registries/x402.d.ts.map +1 -0
- package/dist/types/types/accounts.d.ts +565 -0
- package/dist/types/types/accounts.d.ts.map +1 -0
- package/dist/types/types/common.d.ts +166 -0
- package/dist/types/types/common.d.ts.map +1 -0
- package/dist/types/types/enums.d.ts +238 -0
- package/dist/types/types/enums.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +28 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/instructions.d.ts +366 -0
- package/dist/types/types/instructions.d.ts.map +1 -0
- package/dist/types/utils/hash.d.ts +48 -0
- package/dist/types/utils/hash.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +19 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/serialization.d.ts +69 -0
- package/dist/types/utils/serialization.d.ts.map +1 -0
- package/dist/types/utils/validation.d.ts +29 -0
- package/dist/types/utils/validation.d.ts.map +1 -0
- package/package.json +178 -0
- package/src/constants/index.ts +44 -0
- package/src/constants/limits.ts +165 -0
- package/src/constants/programs.ts +83 -0
- package/src/constants/seeds.ts +66 -0
- package/src/core/client.ts +416 -0
- package/src/core/connection.ts +409 -0
- package/src/core/index.ts +20 -0
- package/src/errors/index.ts +346 -0
- package/src/events/index.ts +335 -0
- package/src/idl/index.ts +76 -0
- package/src/idl/synapse_agent_sap.json +9710 -0
- package/src/index.ts +253 -0
- package/src/modules/agent.ts +319 -0
- package/src/modules/attestation.ts +168 -0
- package/src/modules/base.ts +158 -0
- package/src/modules/escrow.ts +308 -0
- package/src/modules/feedback.ts +186 -0
- package/src/modules/index.ts +24 -0
- package/src/modules/indexing.ts +444 -0
- package/src/modules/ledger.ts +262 -0
- package/src/modules/tools.ts +411 -0
- package/src/modules/vault.ts +533 -0
- package/src/pda/index.ts +512 -0
- package/src/plugin/index.ts +1202 -0
- package/src/plugin/protocols.ts +404 -0
- package/src/plugin/schemas.ts +909 -0
- package/src/postgres/adapter.ts +904 -0
- package/src/postgres/index.ts +59 -0
- package/src/postgres/schema.sql +683 -0
- package/src/postgres/serializers.ts +485 -0
- package/src/postgres/sync.ts +254 -0
- package/src/postgres/types.ts +245 -0
- package/src/registries/builder.ts +607 -0
- package/src/registries/discovery.ts +572 -0
- package/src/registries/index.ts +77 -0
- package/src/registries/session.ts +613 -0
- package/src/registries/x402.ts +906 -0
- package/src/types/accounts.ts +618 -0
- package/src/types/common.ts +187 -0
- package/src/types/enums.ts +214 -0
- package/src/types/index.ts +92 -0
- package/src/types/instructions.ts +413 -0
- package/src/utils/hash.ts +57 -0
- package/src/utils/index.ts +19 -0
- package/src/utils/serialization.ts +98 -0
- package/src/utils/validation.ts +36 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module postgres/sync
|
|
4
|
+
* @description Real-time and scheduled sync engine for the SAP PostgreSQL
|
|
5
|
+
* adapter. Provides cron-like periodic sync and WebSocket-based
|
|
6
|
+
* live event streaming.
|
|
7
|
+
*
|
|
8
|
+
* @category Postgres
|
|
9
|
+
* @since v0.1.0
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { SapSyncEngine } from "@synapse-sap/sdk/postgres";
|
|
14
|
+
* import { SapPostgres } from "@synapse-sap/sdk/postgres";
|
|
15
|
+
* import { SapClient } from "@synapse-sap/sdk";
|
|
16
|
+
* import { Pool } from "pg";
|
|
17
|
+
*
|
|
18
|
+
* const pool = new Pool({ connectionString: "postgresql://..." });
|
|
19
|
+
* const sap = SapClient.from(provider);
|
|
20
|
+
* const pg = new SapPostgres(pool, sap);
|
|
21
|
+
* const sync = new SapSyncEngine(pg, sap);
|
|
22
|
+
*
|
|
23
|
+
* // One-shot full sync
|
|
24
|
+
* await sync.run();
|
|
25
|
+
*
|
|
26
|
+
* // Periodic sync (every 60 seconds)
|
|
27
|
+
* sync.start(60_000);
|
|
28
|
+
*
|
|
29
|
+
* // Live event streaming via WebSocket
|
|
30
|
+
* await sync.startEventStream();
|
|
31
|
+
*
|
|
32
|
+
* // Graceful shutdown
|
|
33
|
+
* await sync.stop();
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.SapSyncEngine = void 0;
|
|
38
|
+
const events_1 = require("../events");
|
|
39
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
40
|
+
// Sync Engine
|
|
41
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
42
|
+
/**
|
|
43
|
+
* @name SapSyncEngine
|
|
44
|
+
* @description Orchestrates periodic and real-time sync between
|
|
45
|
+
* Solana on-chain state and the PostgreSQL mirror.
|
|
46
|
+
*
|
|
47
|
+
* Two operational modes:
|
|
48
|
+
*
|
|
49
|
+
* 1. **Periodic sync** — polls all account types at a configurable
|
|
50
|
+
* interval and upserts into PostgreSQL.
|
|
51
|
+
*
|
|
52
|
+
* 2. **Event streaming** — subscribes to SAP program logs via
|
|
53
|
+
* WebSocket and inserts events in real-time.
|
|
54
|
+
*
|
|
55
|
+
* Both modes can run simultaneously.
|
|
56
|
+
*
|
|
57
|
+
* @category Postgres
|
|
58
|
+
* @since v0.1.0
|
|
59
|
+
*/
|
|
60
|
+
class SapSyncEngine {
|
|
61
|
+
pg;
|
|
62
|
+
client;
|
|
63
|
+
debug;
|
|
64
|
+
intervalId = null;
|
|
65
|
+
logSubId = null;
|
|
66
|
+
running = false;
|
|
67
|
+
constructor(pg, client, debug = false) {
|
|
68
|
+
this.pg = pg;
|
|
69
|
+
this.client = client;
|
|
70
|
+
this.debug = debug;
|
|
71
|
+
}
|
|
72
|
+
// ═════════════════════════════════════════════
|
|
73
|
+
// One-shot Sync
|
|
74
|
+
// ═════════════════════════════════════════════
|
|
75
|
+
/**
|
|
76
|
+
* @name run
|
|
77
|
+
* @description Execute a single full sync cycle.
|
|
78
|
+
* @param options - Optional sync configuration.
|
|
79
|
+
* @returns Sync result summary.
|
|
80
|
+
* @since v0.1.0
|
|
81
|
+
*/
|
|
82
|
+
async run(options) {
|
|
83
|
+
this.log("Starting one-shot sync...");
|
|
84
|
+
const result = await this.pg.syncAll(options);
|
|
85
|
+
this.log(`One-shot sync complete: ${result.totalRecords} records`);
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
// ═════════════════════════════════════════════
|
|
89
|
+
// Periodic Sync
|
|
90
|
+
// ═════════════════════════════════════════════
|
|
91
|
+
/**
|
|
92
|
+
* @name start
|
|
93
|
+
* @description Start periodic sync at the given interval.
|
|
94
|
+
* Safe to call when already running — resets the interval.
|
|
95
|
+
*
|
|
96
|
+
* @param intervalMs - Sync interval in milliseconds (default: 60_000).
|
|
97
|
+
* @param options - Optional sync configuration.
|
|
98
|
+
* @since v0.1.0
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* // Sync every 30 seconds
|
|
103
|
+
* sync.start(30_000);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
start(intervalMs = 60_000, options) {
|
|
107
|
+
this.stopInterval();
|
|
108
|
+
this.running = true;
|
|
109
|
+
this.log(`Starting periodic sync every ${intervalMs}ms`);
|
|
110
|
+
// Run immediately, then repeat
|
|
111
|
+
this.pg.syncAll(options).catch((err) => this.log(`Sync error: ${err}`));
|
|
112
|
+
this.intervalId = setInterval(() => {
|
|
113
|
+
if (!this.running)
|
|
114
|
+
return;
|
|
115
|
+
this.pg.syncAll(options).catch((err) => this.log(`Sync error: ${err}`));
|
|
116
|
+
}, intervalMs);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @name stop
|
|
120
|
+
* @description Stop all sync activity (periodic + event stream).
|
|
121
|
+
* @since v0.1.0
|
|
122
|
+
*/
|
|
123
|
+
async stop() {
|
|
124
|
+
this.running = false;
|
|
125
|
+
this.stopInterval();
|
|
126
|
+
await this.stopEventStream();
|
|
127
|
+
this.log("Sync engine stopped");
|
|
128
|
+
}
|
|
129
|
+
stopInterval() {
|
|
130
|
+
if (this.intervalId) {
|
|
131
|
+
clearInterval(this.intervalId);
|
|
132
|
+
this.intervalId = null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
// ═════════════════════════════════════════════
|
|
136
|
+
// Real-time Event Stream
|
|
137
|
+
// ═════════════════════════════════════════════
|
|
138
|
+
/**
|
|
139
|
+
* @name startEventStream
|
|
140
|
+
* @description Subscribe to SAP program logs via WebSocket and
|
|
141
|
+
* insert parsed events into the `sap_events` table in real-time.
|
|
142
|
+
*
|
|
143
|
+
* @since v0.1.0
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* // Start streaming events
|
|
148
|
+
* await sync.startEventStream();
|
|
149
|
+
*
|
|
150
|
+
* // Later, stop the stream
|
|
151
|
+
* await sync.stopEventStream();
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
async startEventStream() {
|
|
155
|
+
if (this.logSubId !== null) {
|
|
156
|
+
this.log("Event stream already running");
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const connection = this.client.program.provider.connection;
|
|
160
|
+
const programId = this.client.program.programId;
|
|
161
|
+
const eventParser = new events_1.EventParser(this.client.program);
|
|
162
|
+
this.logSubId = connection.onLogs(programId, async (logInfo) => {
|
|
163
|
+
try {
|
|
164
|
+
const events = eventParser.parseLogs(logInfo.logs);
|
|
165
|
+
for (const event of events) {
|
|
166
|
+
const data = event.data;
|
|
167
|
+
const agentPda = data.agent ?? data.agentPda ?? undefined;
|
|
168
|
+
const wallet = data.wallet ?? data.owner ?? undefined;
|
|
169
|
+
await this.pg.syncEvent(event.name, logInfo.signature, 0, // slot populated from getSlot if needed
|
|
170
|
+
data, agentPda, wallet);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
catch (err) {
|
|
174
|
+
this.log(`Event parse error: ${err}`);
|
|
175
|
+
}
|
|
176
|
+
}, "confirmed");
|
|
177
|
+
this.log("Event stream started");
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* @name stopEventStream
|
|
181
|
+
* @description Unsubscribe from the program log stream.
|
|
182
|
+
* @since v0.1.0
|
|
183
|
+
*/
|
|
184
|
+
async stopEventStream() {
|
|
185
|
+
if (this.logSubId !== null) {
|
|
186
|
+
const connection = this.client.program.provider.connection;
|
|
187
|
+
await connection.removeOnLogsListener(this.logSubId);
|
|
188
|
+
this.logSubId = null;
|
|
189
|
+
this.log("Event stream stopped");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// ═════════════════════════════════════════════
|
|
193
|
+
// Status
|
|
194
|
+
// ═════════════════════════════════════════════
|
|
195
|
+
/**
|
|
196
|
+
* @name isRunning
|
|
197
|
+
* @description Check whether the periodic sync is active.
|
|
198
|
+
* @since v0.1.0
|
|
199
|
+
*/
|
|
200
|
+
isRunning() {
|
|
201
|
+
return this.running;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @name isStreaming
|
|
205
|
+
* @description Check whether the event stream is active.
|
|
206
|
+
* @since v0.1.0
|
|
207
|
+
*/
|
|
208
|
+
isStreaming() {
|
|
209
|
+
return this.logSubId !== null;
|
|
210
|
+
}
|
|
211
|
+
// ═════════════════════════════════════════════
|
|
212
|
+
// Internal
|
|
213
|
+
// ═════════════════════════════════════════════
|
|
214
|
+
log(msg) {
|
|
215
|
+
if (this.debug) {
|
|
216
|
+
console.log(`[SapSyncEngine] ${msg}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
exports.SapSyncEngine = SapSyncEngine;
|
|
221
|
+
//# sourceMappingURL=sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../../../src/postgres/sync.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;;;AAGH,sCAAwC;AAIxC,sEAAsE;AACtE,eAAe;AACf,sEAAsE;AAEtE;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,aAAa;IACP,EAAE,CAAc;IAChB,MAAM,CAAY;IAClB,KAAK,CAAU;IAExB,UAAU,GAA0C,IAAI,CAAC;IACzD,QAAQ,GAAkB,IAAI,CAAC;IAC/B,OAAO,GA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AE,CAAC;YACf,OAAO,CAAC,GAAG,CAAC,mBAAmB,GAAG,EAAE,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;CACF;AA/LD,sCA+LC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module postgres/types
|
|
4
|
+
* @description PostgreSQL adapter type definitions for SAP v2.
|
|
5
|
+
*
|
|
6
|
+
* Defines configuration, row shapes, and query result types
|
|
7
|
+
* for the PostgreSQL off-chain mirror.
|
|
8
|
+
*
|
|
9
|
+
* @category Postgres
|
|
10
|
+
* @since v0.1.0
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.SAP_TABLE_MAP = void 0;
|
|
14
|
+
/**
|
|
15
|
+
* @name SapTableName
|
|
16
|
+
* @description Mapping of account types to PostgreSQL table names.
|
|
17
|
+
* @category Postgres
|
|
18
|
+
* @since v0.1.0
|
|
19
|
+
*/
|
|
20
|
+
exports.SAP_TABLE_MAP = {
|
|
21
|
+
global_registry: "sap_global_registry",
|
|
22
|
+
agents: "sap_agents",
|
|
23
|
+
agent_stats: "sap_agent_stats",
|
|
24
|
+
feedbacks: "sap_feedbacks",
|
|
25
|
+
capability_indexes: "sap_capability_indexes",
|
|
26
|
+
protocol_indexes: "sap_protocol_indexes",
|
|
27
|
+
plugin_slots: "sap_plugin_slots",
|
|
28
|
+
memory_entries: "sap_memory_entries",
|
|
29
|
+
memory_chunks: "sap_memory_chunks",
|
|
30
|
+
memory_vaults: "sap_memory_vaults",
|
|
31
|
+
sessions: "sap_sessions",
|
|
32
|
+
epoch_pages: "sap_epoch_pages",
|
|
33
|
+
vault_delegates: "sap_vault_delegates",
|
|
34
|
+
tools: "sap_tools",
|
|
35
|
+
checkpoints: "sap_checkpoints",
|
|
36
|
+
escrows: "sap_escrows",
|
|
37
|
+
tool_category_indexes: "sap_tool_category_indexes",
|
|
38
|
+
attestations: "sap_attestations",
|
|
39
|
+
memory_buffers: "sap_memory_buffers",
|
|
40
|
+
memory_digests: "sap_memory_digests",
|
|
41
|
+
memory_ledgers: "sap_memory_ledgers",
|
|
42
|
+
ledger_pages: "sap_ledger_pages",
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/postgres/types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAwEH;;;;;GAKG;AACU,QAAA,aAAa,GAAmC;IAC3D,eAAe,EAAE,qBAAqB;IACtC,MAAM,EAAE,YAAY;IACpB,WAAW,EAAE,iBAAiB;IAC9B,SAAS,EAAE,eAAe;IAC1B,kBAAkB,EAAE,wBAAwB;IAC5C,gBAAgB,EAAE,sBAAsB;IACxC,YAAY,EAAE,kBAAkB;IAChC,cAAc,EAAE,oBAAoB;IACpC,aAAa,EAAE,mBAAmB;IAClC,aAAa,EAAE,mBAAmB;IAClC,QAAQ,EAAE,cAAc;IACxB,WAAW,EAAE,iBAAiB;IAC9B,eAAe,EAAE,qBAAqB;IACtC,KAAK,EAAE,WAAW;IAClB,WAAW,EAAE,iBAAiB;IAC9B,OAAO,EAAE,aAAa;IACtB,qBAAqB,EAAE,2BAA2B;IAClD,YAAY,EAAE,kBAAkB;IAChC,cAAc,EAAE,oBAAoB;IACpC,cAAc,EAAE,oBAAoB;IACpC,cAAc,EAAE,oBAAoB;IACpC,YAAY,EAAE,kBAAkB;CACjC,CAAC"}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module registries/builder
|
|
4
|
+
* @description Fluent agent registration builder.
|
|
5
|
+
*
|
|
6
|
+
* Provides a chainable API for registering a fully-configured agent
|
|
7
|
+
* in a single flow — identity, capabilities, pricing tiers, tools,
|
|
8
|
+
* x402 endpoint, and discovery indexes.
|
|
9
|
+
*
|
|
10
|
+
* Instead of calling 5+ separate instructions manually, developers
|
|
11
|
+
* use a single builder chain that validates inputs and batches
|
|
12
|
+
* the registration cleanly.
|
|
13
|
+
*
|
|
14
|
+
* @category Registries
|
|
15
|
+
* @since v0.1.0
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const result = await client.builder
|
|
20
|
+
* .agent("SwapBot")
|
|
21
|
+
* .description("AI-powered Jupiter swap agent")
|
|
22
|
+
* .x402Endpoint("https://swapbot.example.com/x402")
|
|
23
|
+
* .addCapability("jupiter:swap", { protocol: "jupiter", version: "6.0" })
|
|
24
|
+
* .addCapability("jupiter:quote", { protocol: "jupiter", version: "6.0" })
|
|
25
|
+
* .addPricingTier({
|
|
26
|
+
* tierId: "standard",
|
|
27
|
+
* pricePerCall: 1000,
|
|
28
|
+
* rateLimit: 60,
|
|
29
|
+
* tokenType: "sol",
|
|
30
|
+
* settlementMode: "x402",
|
|
31
|
+
* })
|
|
32
|
+
* .addProtocol("jupiter")
|
|
33
|
+
* .register();
|
|
34
|
+
*
|
|
35
|
+
* // Or register with tools:
|
|
36
|
+
* const result = await client.builder
|
|
37
|
+
* .agent("DataBot")
|
|
38
|
+
* .description("Real-time DeFi data feeds")
|
|
39
|
+
* .addTool({ name: "getPrice", protocol: "pyth", category: "data", ... })
|
|
40
|
+
* .registerWithTools();
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.AgentBuilder = void 0;
|
|
45
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
46
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
47
|
+
const pda_1 = require("../pda");
|
|
48
|
+
const utils_1 = require("../utils");
|
|
49
|
+
const constants_1 = require("../constants");
|
|
50
|
+
const types_1 = require("../types");
|
|
51
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
52
|
+
// Agent Builder
|
|
53
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
54
|
+
/**
|
|
55
|
+
* @name AgentBuilder
|
|
56
|
+
* @description Fluent builder for registering a fully-configured agent on-chain.
|
|
57
|
+
*
|
|
58
|
+
* Chains identity setters, capability/pricing/protocol adders, and tool
|
|
59
|
+
* definitions into a single validated registration flow. Validates all
|
|
60
|
+
* inputs against on-chain limits before sending transactions.
|
|
61
|
+
*
|
|
62
|
+
* @category Registries
|
|
63
|
+
* @since v0.1.0
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const result = await client.builder
|
|
68
|
+
* .agent("SwapBot")
|
|
69
|
+
* .description("AI-powered Jupiter swap agent")
|
|
70
|
+
* .x402Endpoint("https://swapbot.example.com/x402")
|
|
71
|
+
* .addCapability("jupiter:swap", { protocol: "jupiter", version: "6.0" })
|
|
72
|
+
* .addPricingTier({ tierId: "standard", pricePerCall: 1000, rateLimit: 60 })
|
|
73
|
+
* .register();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
class AgentBuilder {
|
|
77
|
+
program;
|
|
78
|
+
wallet;
|
|
79
|
+
_name = "";
|
|
80
|
+
_description = "";
|
|
81
|
+
_agentId = null;
|
|
82
|
+
_agentUri = null;
|
|
83
|
+
_x402Endpoint = null;
|
|
84
|
+
_capabilities = [];
|
|
85
|
+
_pricing = [];
|
|
86
|
+
_protocols = [];
|
|
87
|
+
_tools = [];
|
|
88
|
+
constructor(program) {
|
|
89
|
+
this.program = program;
|
|
90
|
+
this.wallet = program.provider.wallet.publicKey;
|
|
91
|
+
}
|
|
92
|
+
// ── Fluent Setters ───────────────────────────────────
|
|
93
|
+
/**
|
|
94
|
+
* @name agent
|
|
95
|
+
* @description Set the agent display name.
|
|
96
|
+
* @param name - Display name (max 64 characters).
|
|
97
|
+
* @returns `this` for chaining.
|
|
98
|
+
* @since v0.1.0
|
|
99
|
+
*/
|
|
100
|
+
agent(name) {
|
|
101
|
+
(0, utils_1.assert)(name.length <= constants_1.LIMITS.MAX_NAME_LEN, `Name exceeds ${constants_1.LIMITS.MAX_NAME_LEN} chars`);
|
|
102
|
+
this._name = name;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @name description
|
|
107
|
+
* @description Set the agent description.
|
|
108
|
+
* @param desc - Description text (max 256 characters).
|
|
109
|
+
* @returns `this` for chaining.
|
|
110
|
+
* @since v0.1.0
|
|
111
|
+
*/
|
|
112
|
+
description(desc) {
|
|
113
|
+
(0, utils_1.assert)(desc.length <= constants_1.LIMITS.MAX_DESC_LEN, `Description exceeds ${constants_1.LIMITS.MAX_DESC_LEN} chars`);
|
|
114
|
+
this._description = desc;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @name agentId
|
|
119
|
+
* @description Set a DID-style agent identifier.
|
|
120
|
+
* @param id - Agent identifier string.
|
|
121
|
+
* @returns `this` for chaining.
|
|
122
|
+
* @since v0.1.0
|
|
123
|
+
*/
|
|
124
|
+
agentId(id) {
|
|
125
|
+
(0, utils_1.assert)(id.length <= constants_1.LIMITS.MAX_AGENT_ID_LEN, `Agent ID exceeds ${constants_1.LIMITS.MAX_AGENT_ID_LEN} chars`);
|
|
126
|
+
this._agentId = id;
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @name agentUri
|
|
131
|
+
* @description Set an agent metadata URI.
|
|
132
|
+
* @param uri - Metadata URI string.
|
|
133
|
+
* @returns `this` for chaining.
|
|
134
|
+
* @since v0.1.0
|
|
135
|
+
*/
|
|
136
|
+
agentUri(uri) {
|
|
137
|
+
(0, utils_1.assert)(uri.length <= constants_1.LIMITS.MAX_URI_LEN, `URI exceeds ${constants_1.LIMITS.MAX_URI_LEN} chars`);
|
|
138
|
+
this._agentUri = uri;
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* @name x402Endpoint
|
|
143
|
+
* @description Set the x402 payment endpoint URL.
|
|
144
|
+
* @param url - The x402 endpoint URL for the agent.
|
|
145
|
+
* @returns `this` for chaining.
|
|
146
|
+
* @since v0.1.0
|
|
147
|
+
*/
|
|
148
|
+
x402Endpoint(url) {
|
|
149
|
+
(0, utils_1.assert)(url.length <= constants_1.LIMITS.MAX_URI_LEN, `x402 endpoint exceeds ${constants_1.LIMITS.MAX_URI_LEN} chars`);
|
|
150
|
+
this._x402Endpoint = url;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* @name addCapability
|
|
155
|
+
* @description Add a capability to the agent.
|
|
156
|
+
*
|
|
157
|
+
* @param id - Capability identifier string (e.g. `"jupiter:swap"`).
|
|
158
|
+
* @param opts - Optional capability metadata.
|
|
159
|
+
* @param opts.protocol - Protocol the capability belongs to.
|
|
160
|
+
* @param opts.version - Capability version string.
|
|
161
|
+
* @param opts.description - Human-readable capability description.
|
|
162
|
+
* @returns `this` for chaining.
|
|
163
|
+
* @since v0.1.0
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* builder.addCapability("jupiter:swap", { protocol: "jupiter", version: "6.0" })
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
addCapability(id, opts) {
|
|
171
|
+
(0, utils_1.assert)(this._capabilities.length < constants_1.LIMITS.MAX_CAPABILITIES, `Max ${constants_1.LIMITS.MAX_CAPABILITIES} capabilities`);
|
|
172
|
+
this._capabilities.push({
|
|
173
|
+
id,
|
|
174
|
+
description: opts?.description ?? null,
|
|
175
|
+
protocolId: opts?.protocol ?? null,
|
|
176
|
+
version: opts?.version ?? null,
|
|
177
|
+
});
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* @name addPricingTier
|
|
182
|
+
* @description Add a pricing tier for the agent's services.
|
|
183
|
+
* Supports flat-rate, volume-curve, and configurable settlement modes.
|
|
184
|
+
*
|
|
185
|
+
* @param input - Pricing tier configuration.
|
|
186
|
+
* @returns `this` for chaining.
|
|
187
|
+
* @since v0.1.0
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* builder.addPricingTier({
|
|
192
|
+
* tierId: "standard",
|
|
193
|
+
* pricePerCall: 1000,
|
|
194
|
+
* rateLimit: 60,
|
|
195
|
+
* tokenType: "sol",
|
|
196
|
+
* settlementMode: "x402",
|
|
197
|
+
* })
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
addPricingTier(input) {
|
|
201
|
+
(0, utils_1.assert)(this._pricing.length < constants_1.LIMITS.MAX_PRICING_TIERS, `Max ${constants_1.LIMITS.MAX_PRICING_TIERS} pricing tiers`);
|
|
202
|
+
const tokenType = this.resolveTokenType(input.tokenType ?? "sol");
|
|
203
|
+
const settlementMode = input.settlementMode
|
|
204
|
+
? this.resolveSettlementMode(input.settlementMode)
|
|
205
|
+
: null;
|
|
206
|
+
const volumeCurve = input.volumeCurve
|
|
207
|
+
? input.volumeCurve.map((v) => ({
|
|
208
|
+
afterCalls: v.afterCalls,
|
|
209
|
+
pricePerCall: new anchor_1.BN(v.pricePerCall.toString()),
|
|
210
|
+
}))
|
|
211
|
+
: null;
|
|
212
|
+
this._pricing.push({
|
|
213
|
+
tierId: input.tierId,
|
|
214
|
+
pricePerCall: new anchor_1.BN(input.pricePerCall.toString()),
|
|
215
|
+
minPricePerCall: input.minPricePerCall
|
|
216
|
+
? new anchor_1.BN(input.minPricePerCall.toString())
|
|
217
|
+
: null,
|
|
218
|
+
maxPricePerCall: input.maxPricePerCall
|
|
219
|
+
? new anchor_1.BN(input.maxPricePerCall.toString())
|
|
220
|
+
: null,
|
|
221
|
+
rateLimit: input.rateLimit,
|
|
222
|
+
maxCallsPerSession: input.maxCallsPerSession ?? 0,
|
|
223
|
+
burstLimit: input.burstLimit ?? null,
|
|
224
|
+
tokenType,
|
|
225
|
+
tokenMint: input.tokenMint ?? null,
|
|
226
|
+
tokenDecimals: input.tokenDecimals ?? null,
|
|
227
|
+
settlementMode,
|
|
228
|
+
minEscrowDeposit: input.minEscrowDeposit
|
|
229
|
+
? new anchor_1.BN(input.minEscrowDeposit.toString())
|
|
230
|
+
: null,
|
|
231
|
+
batchIntervalSec: input.batchIntervalSec ?? null,
|
|
232
|
+
volumeCurve,
|
|
233
|
+
});
|
|
234
|
+
return this;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* @name addProtocol
|
|
238
|
+
* @description Add a protocol the agent supports.
|
|
239
|
+
* Duplicates are silently ignored.
|
|
240
|
+
*
|
|
241
|
+
* @param protocolId - Protocol identifier string (e.g. `"jupiter"`).
|
|
242
|
+
* @returns `this` for chaining.
|
|
243
|
+
* @since v0.1.0
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```ts
|
|
247
|
+
* builder.addProtocol("jupiter")
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
addProtocol(protocolId) {
|
|
251
|
+
(0, utils_1.assert)(this._protocols.length < constants_1.LIMITS.MAX_PROTOCOLS, `Max ${constants_1.LIMITS.MAX_PROTOCOLS} protocols`);
|
|
252
|
+
if (!this._protocols.includes(protocolId)) {
|
|
253
|
+
this._protocols.push(protocolId);
|
|
254
|
+
}
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @name addTool
|
|
259
|
+
* @description Add a tool to be published after registration.
|
|
260
|
+
* Only used with {@link AgentBuilder.registerWithTools}.
|
|
261
|
+
*
|
|
262
|
+
* @param tool - Tool configuration.
|
|
263
|
+
* @returns `this` for chaining.
|
|
264
|
+
* @since v0.1.0
|
|
265
|
+
*/
|
|
266
|
+
addTool(tool) {
|
|
267
|
+
this._tools.push(tool);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
// ── Registration ─────────────────────────────────────
|
|
271
|
+
/**
|
|
272
|
+
* @name register
|
|
273
|
+
* @description Register the agent on-chain.
|
|
274
|
+
* Validates all inputs before sending the transaction.
|
|
275
|
+
*
|
|
276
|
+
* @returns A {@link RegisterResult} with the transaction signature and derived PDAs.
|
|
277
|
+
* @throws If validation fails (missing name, description, or limit exceeded).
|
|
278
|
+
* @since v0.1.0
|
|
279
|
+
*/
|
|
280
|
+
async register() {
|
|
281
|
+
this.validate();
|
|
282
|
+
const [agentPda] = (0, pda_1.deriveAgent)(this.wallet);
|
|
283
|
+
const [statsPda] = (0, pda_1.deriveAgentStats)(agentPda);
|
|
284
|
+
const [globalPda] = (0, pda_1.deriveGlobalRegistry)();
|
|
285
|
+
const txSignature = await this.methods
|
|
286
|
+
.registerAgent(this._name, this._description, this._capabilities, this._pricing, this._protocols, this._agentId, this._agentUri, this._x402Endpoint)
|
|
287
|
+
.accounts({
|
|
288
|
+
wallet: this.wallet,
|
|
289
|
+
agent: agentPda,
|
|
290
|
+
agentStats: statsPda,
|
|
291
|
+
globalRegistry: globalPda,
|
|
292
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
293
|
+
})
|
|
294
|
+
.rpc();
|
|
295
|
+
return { txSignature, agentPda, statsPda };
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* @name registerWithTools
|
|
299
|
+
* @description Register the agent AND publish all configured tools.
|
|
300
|
+
* Sends agent registration first, then tool publications sequentially.
|
|
301
|
+
*
|
|
302
|
+
* @returns A {@link RegisterWithToolsResult} with agent and tool transaction signatures.
|
|
303
|
+
* @throws If validation fails or any tool publication fails.
|
|
304
|
+
* @since v0.1.0
|
|
305
|
+
*/
|
|
306
|
+
async registerWithTools() {
|
|
307
|
+
const result = await this.register();
|
|
308
|
+
const toolSignatures = [];
|
|
309
|
+
const [globalPda] = (0, pda_1.deriveGlobalRegistry)();
|
|
310
|
+
for (const tool of this._tools) {
|
|
311
|
+
const toolNameHash = (0, utils_1.hashToArray)((0, utils_1.sha256)(tool.name));
|
|
312
|
+
const [toolPda] = (0, pda_1.deriveTool)(result.agentPda, new Uint8Array(toolNameHash));
|
|
313
|
+
const httpMethod = constants_1.HTTP_METHOD_VALUES[(tool.httpMethod ?? "post")] ?? 1;
|
|
314
|
+
const category = constants_1.TOOL_CATEGORY_VALUES[(tool.category ?? "Custom")] ?? 9;
|
|
315
|
+
const txSignature = await this.methods
|
|
316
|
+
.publishTool(tool.name, toolNameHash, (0, utils_1.hashToArray)((0, utils_1.sha256)(tool.protocol)), (0, utils_1.hashToArray)((0, utils_1.sha256)(tool.description)), (0, utils_1.hashToArray)((0, utils_1.sha256)(tool.inputSchema)), (0, utils_1.hashToArray)((0, utils_1.sha256)(tool.outputSchema)), httpMethod, category, tool.paramsCount, tool.requiredParams, tool.isCompound ?? false)
|
|
317
|
+
.accounts({
|
|
318
|
+
wallet: this.wallet,
|
|
319
|
+
agent: result.agentPda,
|
|
320
|
+
tool: toolPda,
|
|
321
|
+
globalRegistry: globalPda,
|
|
322
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
323
|
+
})
|
|
324
|
+
.rpc();
|
|
325
|
+
toolSignatures.push({ name: tool.name, txSignature });
|
|
326
|
+
}
|
|
327
|
+
return { ...result, toolSignatures };
|
|
328
|
+
}
|
|
329
|
+
// ── Reset ────────────────────────────────────────────
|
|
330
|
+
/**
|
|
331
|
+
* @name reset
|
|
332
|
+
* @description Reset the builder to its initial state.
|
|
333
|
+
* Clears all configured values so the builder can be reused.
|
|
334
|
+
*
|
|
335
|
+
* @returns `this` for chaining.
|
|
336
|
+
* @since v0.1.0
|
|
337
|
+
*/
|
|
338
|
+
reset() {
|
|
339
|
+
this._name = "";
|
|
340
|
+
this._description = "";
|
|
341
|
+
this._agentId = null;
|
|
342
|
+
this._agentUri = null;
|
|
343
|
+
this._x402Endpoint = null;
|
|
344
|
+
this._capabilities = [];
|
|
345
|
+
this._pricing = [];
|
|
346
|
+
this._protocols = [];
|
|
347
|
+
this._tools = [];
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
// ── Validation ───────────────────────────────────────
|
|
351
|
+
/**
|
|
352
|
+
* @name validate
|
|
353
|
+
* @description Validate all builder inputs against on-chain limits.
|
|
354
|
+
* @throws If any required field is missing or any limit is exceeded.
|
|
355
|
+
* @private
|
|
356
|
+
*/
|
|
357
|
+
validate() {
|
|
358
|
+
(0, utils_1.assert)(this._name.length > 0, "Agent name is required");
|
|
359
|
+
(0, utils_1.assert)(this._description.length > 0, "Agent description is required");
|
|
360
|
+
(0, utils_1.assert)(this._capabilities.length <= constants_1.LIMITS.MAX_CAPABILITIES, `Max ${constants_1.LIMITS.MAX_CAPABILITIES} capabilities`);
|
|
361
|
+
(0, utils_1.assert)(this._pricing.length <= constants_1.LIMITS.MAX_PRICING_TIERS, `Max ${constants_1.LIMITS.MAX_PRICING_TIERS} pricing tiers`);
|
|
362
|
+
(0, utils_1.assert)(this._protocols.length <= constants_1.LIMITS.MAX_PROTOCOLS, `Max ${constants_1.LIMITS.MAX_PROTOCOLS} protocols`);
|
|
363
|
+
}
|
|
364
|
+
// ── Helpers ──────────────────────────────────────────
|
|
365
|
+
/**
|
|
366
|
+
* @name resolveTokenType
|
|
367
|
+
* @description Resolve a token type string to the on-chain enum variant.
|
|
368
|
+
* @param t - Token type string (`"sol"`, `"usdc"`, or `"spl"`).
|
|
369
|
+
* @returns The corresponding {@link TokenTypeKind} enum value.
|
|
370
|
+
* @private
|
|
371
|
+
*/
|
|
372
|
+
resolveTokenType(t) {
|
|
373
|
+
switch (t) {
|
|
374
|
+
case "sol":
|
|
375
|
+
return types_1.TokenType.Sol;
|
|
376
|
+
case "usdc":
|
|
377
|
+
return types_1.TokenType.Usdc;
|
|
378
|
+
case "spl":
|
|
379
|
+
return types_1.TokenType.Spl;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* @name resolveSettlementMode
|
|
384
|
+
* @description Resolve a settlement mode string to the on-chain enum variant.
|
|
385
|
+
* @param m - Settlement mode string (`"instant"`, `"escrow"`, `"batched"`, or `"x402"`).
|
|
386
|
+
* @returns The corresponding {@link SettlementModeKind} enum value.
|
|
387
|
+
* @private
|
|
388
|
+
*/
|
|
389
|
+
resolveSettlementMode(m) {
|
|
390
|
+
switch (m) {
|
|
391
|
+
case "instant":
|
|
392
|
+
return types_1.SettlementMode.Instant;
|
|
393
|
+
case "escrow":
|
|
394
|
+
return types_1.SettlementMode.Escrow;
|
|
395
|
+
case "batched":
|
|
396
|
+
return types_1.SettlementMode.Batched;
|
|
397
|
+
case "x402":
|
|
398
|
+
return types_1.SettlementMode.X402;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* @name methods
|
|
403
|
+
* @description Accessor for the Anchor program methods namespace.
|
|
404
|
+
* @returns The program methods object for building RPC calls.
|
|
405
|
+
* @private
|
|
406
|
+
*/
|
|
407
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
408
|
+
get methods() {
|
|
409
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
410
|
+
return this.program.methods;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
exports.AgentBuilder = AgentBuilder;
|
|
414
|
+
//# sourceMappingURL=builder.js.map
|