@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.
Files changed (315) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +882 -0
  3. package/dist/cjs/constants/index.js +43 -0
  4. package/dist/cjs/constants/index.js.map +1 -0
  5. package/dist/cjs/constants/limits.js +161 -0
  6. package/dist/cjs/constants/limits.js.map +1 -0
  7. package/dist/cjs/constants/programs.js +78 -0
  8. package/dist/cjs/constants/programs.js.map +1 -0
  9. package/dist/cjs/constants/seeds.js +57 -0
  10. package/dist/cjs/constants/seeds.js.map +1 -0
  11. package/dist/cjs/core/client.js +391 -0
  12. package/dist/cjs/core/client.js.map +1 -0
  13. package/dist/cjs/core/connection.js +319 -0
  14. package/dist/cjs/core/connection.js.map +1 -0
  15. package/dist/cjs/core/index.js +24 -0
  16. package/dist/cjs/core/index.js.map +1 -0
  17. package/dist/cjs/errors/index.js +334 -0
  18. package/dist/cjs/errors/index.js.map +1 -0
  19. package/dist/cjs/events/index.js +136 -0
  20. package/dist/cjs/events/index.js.map +1 -0
  21. package/dist/cjs/idl/index.js +63 -0
  22. package/dist/cjs/idl/index.js.map +1 -0
  23. package/dist/cjs/idl/synapse_agent_sap.json +9710 -0
  24. package/dist/cjs/index.js +147 -0
  25. package/dist/cjs/index.js.map +1 -0
  26. package/dist/cjs/modules/agent.js +272 -0
  27. package/dist/cjs/modules/agent.js.map +1 -0
  28. package/dist/cjs/modules/attestation.js +147 -0
  29. package/dist/cjs/modules/attestation.js.map +1 -0
  30. package/dist/cjs/modules/base.js +128 -0
  31. package/dist/cjs/modules/base.js.map +1 -0
  32. package/dist/cjs/modules/escrow.js +246 -0
  33. package/dist/cjs/modules/escrow.js.map +1 -0
  34. package/dist/cjs/modules/feedback.js +166 -0
  35. package/dist/cjs/modules/feedback.js.map +1 -0
  36. package/dist/cjs/modules/index.js +35 -0
  37. package/dist/cjs/modules/index.js.map +1 -0
  38. package/dist/cjs/modules/indexing.js +375 -0
  39. package/dist/cjs/modules/indexing.js.map +1 -0
  40. package/dist/cjs/modules/ledger.js +234 -0
  41. package/dist/cjs/modules/ledger.js.map +1 -0
  42. package/dist/cjs/modules/tools.js +319 -0
  43. package/dist/cjs/modules/tools.js.map +1 -0
  44. package/dist/cjs/modules/vault.js +410 -0
  45. package/dist/cjs/modules/vault.js.map +1 -0
  46. package/dist/cjs/pda/index.js +377 -0
  47. package/dist/cjs/pda/index.js.map +1 -0
  48. package/dist/cjs/plugin/index.js +934 -0
  49. package/dist/cjs/plugin/index.js.map +1 -0
  50. package/dist/cjs/plugin/protocols.js +282 -0
  51. package/dist/cjs/plugin/protocols.js.map +1 -0
  52. package/dist/cjs/plugin/schemas.js +831 -0
  53. package/dist/cjs/plugin/schemas.js.map +1 -0
  54. package/dist/cjs/postgres/adapter.js +715 -0
  55. package/dist/cjs/postgres/adapter.js.map +1 -0
  56. package/dist/cjs/postgres/index.js +50 -0
  57. package/dist/cjs/postgres/index.js.map +1 -0
  58. package/dist/cjs/postgres/serializers.js +381 -0
  59. package/dist/cjs/postgres/serializers.js.map +1 -0
  60. package/dist/cjs/postgres/sync.js +221 -0
  61. package/dist/cjs/postgres/sync.js.map +1 -0
  62. package/dist/cjs/postgres/types.js +44 -0
  63. package/dist/cjs/postgres/types.js.map +1 -0
  64. package/dist/cjs/registries/builder.js +414 -0
  65. package/dist/cjs/registries/builder.js.map +1 -0
  66. package/dist/cjs/registries/discovery.js +362 -0
  67. package/dist/cjs/registries/discovery.js.map +1 -0
  68. package/dist/cjs/registries/index.js +51 -0
  69. package/dist/cjs/registries/index.js.map +1 -0
  70. package/dist/cjs/registries/session.js +433 -0
  71. package/dist/cjs/registries/session.js.map +1 -0
  72. package/dist/cjs/registries/x402.js +577 -0
  73. package/dist/cjs/registries/x402.js.map +1 -0
  74. package/dist/cjs/types/accounts.js +13 -0
  75. package/dist/cjs/types/accounts.js.map +1 -0
  76. package/dist/cjs/types/common.js +13 -0
  77. package/dist/cjs/types/common.js.map +1 -0
  78. package/dist/cjs/types/enums.js +174 -0
  79. package/dist/cjs/types/enums.js.map +1 -0
  80. package/dist/cjs/types/index.js +36 -0
  81. package/dist/cjs/types/index.js.map +1 -0
  82. package/dist/cjs/types/instructions.js +92 -0
  83. package/dist/cjs/types/instructions.js.map +1 -0
  84. package/dist/cjs/utils/hash.js +58 -0
  85. package/dist/cjs/utils/hash.js.map +1 -0
  86. package/dist/cjs/utils/index.js +27 -0
  87. package/dist/cjs/utils/index.js.map +1 -0
  88. package/dist/cjs/utils/serialization.js +105 -0
  89. package/dist/cjs/utils/serialization.js.map +1 -0
  90. package/dist/cjs/utils/validation.js +36 -0
  91. package/dist/cjs/utils/validation.js.map +1 -0
  92. package/dist/esm/constants/index.js +29 -0
  93. package/dist/esm/constants/index.js.map +1 -0
  94. package/dist/esm/constants/limits.js +158 -0
  95. package/dist/esm/constants/limits.js.map +1 -0
  96. package/dist/esm/constants/programs.js +75 -0
  97. package/dist/esm/constants/programs.js.map +1 -0
  98. package/dist/esm/constants/seeds.js +54 -0
  99. package/dist/esm/constants/seeds.js.map +1 -0
  100. package/dist/esm/core/client.js +384 -0
  101. package/dist/esm/core/client.js.map +1 -0
  102. package/dist/esm/core/connection.js +315 -0
  103. package/dist/esm/core/connection.js.map +1 -0
  104. package/dist/esm/core/index.js +19 -0
  105. package/dist/esm/core/index.js.map +1 -0
  106. package/dist/esm/errors/index.js +325 -0
  107. package/dist/esm/errors/index.js.map +1 -0
  108. package/dist/esm/events/index.js +132 -0
  109. package/dist/esm/events/index.js.map +1 -0
  110. package/dist/esm/idl/index.js +57 -0
  111. package/dist/esm/idl/index.js.map +1 -0
  112. package/dist/esm/idl/synapse_agent_sap.json +9710 -0
  113. package/dist/esm/index.js +70 -0
  114. package/dist/esm/index.js.map +1 -0
  115. package/dist/esm/modules/agent.js +268 -0
  116. package/dist/esm/modules/agent.js.map +1 -0
  117. package/dist/esm/modules/attestation.js +143 -0
  118. package/dist/esm/modules/attestation.js.map +1 -0
  119. package/dist/esm/modules/base.js +124 -0
  120. package/dist/esm/modules/base.js.map +1 -0
  121. package/dist/esm/modules/escrow.js +242 -0
  122. package/dist/esm/modules/escrow.js.map +1 -0
  123. package/dist/esm/modules/feedback.js +162 -0
  124. package/dist/esm/modules/feedback.js.map +1 -0
  125. package/dist/esm/modules/index.js +23 -0
  126. package/dist/esm/modules/index.js.map +1 -0
  127. package/dist/esm/modules/indexing.js +371 -0
  128. package/dist/esm/modules/indexing.js.map +1 -0
  129. package/dist/esm/modules/ledger.js +230 -0
  130. package/dist/esm/modules/ledger.js.map +1 -0
  131. package/dist/esm/modules/tools.js +315 -0
  132. package/dist/esm/modules/tools.js.map +1 -0
  133. package/dist/esm/modules/vault.js +406 -0
  134. package/dist/esm/modules/vault.js.map +1 -0
  135. package/dist/esm/pda/index.js +357 -0
  136. package/dist/esm/pda/index.js.map +1 -0
  137. package/dist/esm/plugin/index.js +927 -0
  138. package/dist/esm/plugin/index.js.map +1 -0
  139. package/dist/esm/plugin/protocols.js +279 -0
  140. package/dist/esm/plugin/protocols.js.map +1 -0
  141. package/dist/esm/plugin/schemas.js +828 -0
  142. package/dist/esm/plugin/schemas.js.map +1 -0
  143. package/dist/esm/postgres/adapter.js +678 -0
  144. package/dist/esm/postgres/adapter.js.map +1 -0
  145. package/dist/esm/postgres/index.js +27 -0
  146. package/dist/esm/postgres/index.js.map +1 -0
  147. package/dist/esm/postgres/serializers.js +362 -0
  148. package/dist/esm/postgres/serializers.js.map +1 -0
  149. package/dist/esm/postgres/sync.js +217 -0
  150. package/dist/esm/postgres/sync.js.map +1 -0
  151. package/dist/esm/postgres/types.js +41 -0
  152. package/dist/esm/postgres/types.js.map +1 -0
  153. package/dist/esm/registries/builder.js +410 -0
  154. package/dist/esm/registries/builder.js.map +1 -0
  155. package/dist/esm/registries/discovery.js +358 -0
  156. package/dist/esm/registries/discovery.js.map +1 -0
  157. package/dist/esm/registries/index.js +44 -0
  158. package/dist/esm/registries/index.js.map +1 -0
  159. package/dist/esm/registries/session.js +429 -0
  160. package/dist/esm/registries/session.js.map +1 -0
  161. package/dist/esm/registries/x402.js +573 -0
  162. package/dist/esm/registries/x402.js.map +1 -0
  163. package/dist/esm/types/accounts.js +12 -0
  164. package/dist/esm/types/accounts.js.map +1 -0
  165. package/dist/esm/types/common.js +12 -0
  166. package/dist/esm/types/common.js.map +1 -0
  167. package/dist/esm/types/enums.js +171 -0
  168. package/dist/esm/types/enums.js.map +1 -0
  169. package/dist/esm/types/index.js +25 -0
  170. package/dist/esm/types/index.js.map +1 -0
  171. package/dist/esm/types/instructions.js +89 -0
  172. package/dist/esm/types/instructions.js.map +1 -0
  173. package/dist/esm/utils/hash.js +53 -0
  174. package/dist/esm/utils/hash.js.map +1 -0
  175. package/dist/esm/utils/index.js +19 -0
  176. package/dist/esm/utils/index.js.map +1 -0
  177. package/dist/esm/utils/serialization.js +98 -0
  178. package/dist/esm/utils/serialization.js.map +1 -0
  179. package/dist/esm/utils/validation.js +33 -0
  180. package/dist/esm/utils/validation.js.map +1 -0
  181. package/dist/types/constants/index.d.ts +27 -0
  182. package/dist/types/constants/index.d.ts.map +1 -0
  183. package/dist/types/constants/limits.d.ts +149 -0
  184. package/dist/types/constants/limits.d.ts.map +1 -0
  185. package/dist/types/constants/programs.d.ts +69 -0
  186. package/dist/types/constants/programs.d.ts.map +1 -0
  187. package/dist/types/constants/seeds.d.ts +61 -0
  188. package/dist/types/constants/seeds.d.ts.map +1 -0
  189. package/dist/types/core/client.d.ts +323 -0
  190. package/dist/types/core/client.d.ts.map +1 -0
  191. package/dist/types/core/connection.d.ts +279 -0
  192. package/dist/types/core/connection.d.ts.map +1 -0
  193. package/dist/types/core/index.d.ts +20 -0
  194. package/dist/types/core/index.d.ts.map +1 -0
  195. package/dist/types/errors/index.d.ts +276 -0
  196. package/dist/types/errors/index.d.ts.map +1 -0
  197. package/dist/types/events/index.d.ts +248 -0
  198. package/dist/types/events/index.d.ts.map +1 -0
  199. package/dist/types/idl/index.d.ts +70 -0
  200. package/dist/types/idl/index.d.ts.map +1 -0
  201. package/dist/types/index.d.ts +68 -0
  202. package/dist/types/index.d.ts.map +1 -0
  203. package/dist/types/modules/agent.d.ts +166 -0
  204. package/dist/types/modules/agent.d.ts.map +1 -0
  205. package/dist/types/modules/attestation.d.ts +96 -0
  206. package/dist/types/modules/attestation.d.ts.map +1 -0
  207. package/dist/types/modules/base.d.ts +126 -0
  208. package/dist/types/modules/base.d.ts.map +1 -0
  209. package/dist/types/modules/escrow.d.ts +151 -0
  210. package/dist/types/modules/escrow.d.ts.map +1 -0
  211. package/dist/types/modules/feedback.d.ts +105 -0
  212. package/dist/types/modules/feedback.d.ts.map +1 -0
  213. package/dist/types/modules/index.d.ts +24 -0
  214. package/dist/types/modules/index.d.ts.map +1 -0
  215. package/dist/types/modules/indexing.d.ts +200 -0
  216. package/dist/types/modules/indexing.d.ts.map +1 -0
  217. package/dist/types/modules/ledger.d.ts +150 -0
  218. package/dist/types/modules/ledger.d.ts.map +1 -0
  219. package/dist/types/modules/tools.d.ts +182 -0
  220. package/dist/types/modules/tools.d.ts.map +1 -0
  221. package/dist/types/modules/vault.d.ts +240 -0
  222. package/dist/types/modules/vault.d.ts.map +1 -0
  223. package/dist/types/pda/index.d.ts +296 -0
  224. package/dist/types/pda/index.d.ts.map +1 -0
  225. package/dist/types/plugin/index.d.ts +171 -0
  226. package/dist/types/plugin/index.d.ts.map +1 -0
  227. package/dist/types/plugin/protocols.d.ts +152 -0
  228. package/dist/types/plugin/protocols.d.ts.map +1 -0
  229. package/dist/types/plugin/schemas.d.ts +823 -0
  230. package/dist/types/plugin/schemas.d.ts.map +1 -0
  231. package/dist/types/postgres/adapter.d.ts +355 -0
  232. package/dist/types/postgres/adapter.d.ts.map +1 -0
  233. package/dist/types/postgres/index.d.ts +24 -0
  234. package/dist/types/postgres/index.d.ts.map +1 -0
  235. package/dist/types/postgres/serializers.d.ts +30 -0
  236. package/dist/types/postgres/serializers.d.ts.map +1 -0
  237. package/dist/types/postgres/sync.d.ts +132 -0
  238. package/dist/types/postgres/sync.d.ts.map +1 -0
  239. package/dist/types/postgres/types.d.ts +167 -0
  240. package/dist/types/postgres/types.d.ts.map +1 -0
  241. package/dist/types/registries/builder.d.ts +340 -0
  242. package/dist/types/registries/builder.d.ts.map +1 -0
  243. package/dist/types/registries/discovery.d.ts +333 -0
  244. package/dist/types/registries/discovery.d.ts.map +1 -0
  245. package/dist/types/registries/index.d.ts +48 -0
  246. package/dist/types/registries/index.d.ts.map +1 -0
  247. package/dist/types/registries/session.d.ts +323 -0
  248. package/dist/types/registries/session.d.ts.map +1 -0
  249. package/dist/types/registries/x402.d.ts +463 -0
  250. package/dist/types/registries/x402.d.ts.map +1 -0
  251. package/dist/types/types/accounts.d.ts +565 -0
  252. package/dist/types/types/accounts.d.ts.map +1 -0
  253. package/dist/types/types/common.d.ts +166 -0
  254. package/dist/types/types/common.d.ts.map +1 -0
  255. package/dist/types/types/enums.d.ts +238 -0
  256. package/dist/types/types/enums.d.ts.map +1 -0
  257. package/dist/types/types/index.d.ts +28 -0
  258. package/dist/types/types/index.d.ts.map +1 -0
  259. package/dist/types/types/instructions.d.ts +366 -0
  260. package/dist/types/types/instructions.d.ts.map +1 -0
  261. package/dist/types/utils/hash.d.ts +48 -0
  262. package/dist/types/utils/hash.d.ts.map +1 -0
  263. package/dist/types/utils/index.d.ts +19 -0
  264. package/dist/types/utils/index.d.ts.map +1 -0
  265. package/dist/types/utils/serialization.d.ts +69 -0
  266. package/dist/types/utils/serialization.d.ts.map +1 -0
  267. package/dist/types/utils/validation.d.ts +29 -0
  268. package/dist/types/utils/validation.d.ts.map +1 -0
  269. package/package.json +178 -0
  270. package/src/constants/index.ts +44 -0
  271. package/src/constants/limits.ts +165 -0
  272. package/src/constants/programs.ts +83 -0
  273. package/src/constants/seeds.ts +66 -0
  274. package/src/core/client.ts +416 -0
  275. package/src/core/connection.ts +409 -0
  276. package/src/core/index.ts +20 -0
  277. package/src/errors/index.ts +346 -0
  278. package/src/events/index.ts +335 -0
  279. package/src/idl/index.ts +76 -0
  280. package/src/idl/synapse_agent_sap.json +9710 -0
  281. package/src/index.ts +253 -0
  282. package/src/modules/agent.ts +319 -0
  283. package/src/modules/attestation.ts +168 -0
  284. package/src/modules/base.ts +158 -0
  285. package/src/modules/escrow.ts +308 -0
  286. package/src/modules/feedback.ts +186 -0
  287. package/src/modules/index.ts +24 -0
  288. package/src/modules/indexing.ts +444 -0
  289. package/src/modules/ledger.ts +262 -0
  290. package/src/modules/tools.ts +411 -0
  291. package/src/modules/vault.ts +533 -0
  292. package/src/pda/index.ts +512 -0
  293. package/src/plugin/index.ts +1202 -0
  294. package/src/plugin/protocols.ts +404 -0
  295. package/src/plugin/schemas.ts +909 -0
  296. package/src/postgres/adapter.ts +904 -0
  297. package/src/postgres/index.ts +59 -0
  298. package/src/postgres/schema.sql +683 -0
  299. package/src/postgres/serializers.ts +485 -0
  300. package/src/postgres/sync.ts +254 -0
  301. package/src/postgres/types.ts +245 -0
  302. package/src/registries/builder.ts +607 -0
  303. package/src/registries/discovery.ts +572 -0
  304. package/src/registries/index.ts +77 -0
  305. package/src/registries/session.ts +613 -0
  306. package/src/registries/x402.ts +906 -0
  307. package/src/types/accounts.ts +618 -0
  308. package/src/types/common.ts +187 -0
  309. package/src/types/enums.ts +214 -0
  310. package/src/types/index.ts +92 -0
  311. package/src/types/instructions.ts +413 -0
  312. package/src/utils/hash.ts +57 -0
  313. package/src/utils/index.ts +19 -0
  314. package/src/utils/serialization.ts +98 -0
  315. package/src/utils/validation.ts +36 -0
@@ -0,0 +1,1202 @@
1
+ /**
2
+ * @module plugin
3
+ * @description SynapsePlugin adapter for SAP v2.
4
+ *
5
+ * Exposes 52 tools across 8 on-chain protocol domains as a drop-in
6
+ * `SynapsePlugin` for `SynapseAgentKit`:
7
+ *
8
+ * ```ts
9
+ * import { SynapseAgentKit } from '@oobe-protocol-labs/synapse-client-sdk/ai/plugins';
10
+ * import { createSAPPlugin } from '@synapse-sap/sdk/plugin';
11
+ *
12
+ * const sapPlugin = createSAPPlugin({ provider });
13
+ * const kit = new SynapseAgentKit({ rpcUrl })
14
+ * .use(sapPlugin);
15
+ *
16
+ * const tools = kit.getTools(); // → StructuredTool[] (LangChain)
17
+ * ```
18
+ *
19
+ * Architecture:
20
+ * 1. Schemas (Zod) → runtime validation + LLM-friendly descriptions
21
+ * 2. Protocols → 8 domain method registries (agent, feedback, …)
22
+ * 3. Executor → dispatches tool calls to SapClient module methods
23
+ * 4. Serialization → PublicKey↔string, BN↔string bridging
24
+ *
25
+ * @category Plugin
26
+ * @since v0.1.0
27
+ */
28
+
29
+ import { type AnchorProvider } from "@coral-xyz/anchor";
30
+ import { PublicKey } from "@solana/web3.js";
31
+ import BN from "bn.js";
32
+ import { SapClient } from "../core/client";
33
+ import {
34
+ SAP_PROTOCOLS,
35
+ type PluginProtocol,
36
+ type ProtocolMethod,
37
+ } from "./protocols";
38
+ import type {
39
+ RegisterAgentArgs,
40
+ UpdateAgentArgs,
41
+ GiveFeedbackArgs,
42
+ UpdateFeedbackArgs,
43
+ CreateAttestationArgs,
44
+ CreateEscrowArgs,
45
+ Settlement,
46
+ InscribeMemoryArgs,
47
+ InscribeToolSchemaArgs,
48
+ UpdateToolArgs,
49
+ VolumeCurveBreakpoint,
50
+ } from "../types";
51
+ import { deriveAgent, deriveVault, deriveSession, deriveLedger } from "../pda";
52
+ import { hashToArray, sha256 } from "../utils";
53
+
54
+ // ═══════════════════════════════════════════════════════════════════
55
+ // Public Types
56
+ // ═══════════════════════════════════════════════════════════════════
57
+
58
+ /**
59
+ * Plugin metadata exposed via {@link SynapsePlugin.meta}.
60
+ *
61
+ * @interface PluginMeta
62
+ * @name PluginMeta
63
+ * @description Describes a SynapsePlugin’s identity, version, and
64
+ * discovery tags for the SynapseAgentKit plugin registry.
65
+ * @category Plugin
66
+ * @since v0.1.0
67
+ */
68
+ export interface PluginMeta {
69
+ readonly id: string;
70
+ readonly name: string;
71
+ readonly version: string;
72
+ readonly description: string;
73
+ readonly tags: readonly string[];
74
+ }
75
+
76
+ /**
77
+ * Context injected by SynapseAgentKit during `install()`.
78
+ *
79
+ * @interface PluginContext
80
+ * @name PluginContext
81
+ * @description Provides the RPC transport and original AgentKit configuration
82
+ * to the plugin at installation time.
83
+ * @category Plugin
84
+ * @since v0.1.0
85
+ */
86
+ export interface PluginContext {
87
+ /** The SynapseClient providing RPC transport. */
88
+ readonly client: {
89
+ readonly transport: unknown;
90
+ };
91
+ /** Original AgentKitConfig. */
92
+ readonly config: Record<string, unknown>;
93
+ }
94
+
95
+ /**
96
+ * Result of `install()` containing the tool executor and optional teardown.
97
+ *
98
+ * @interface PluginInstallResult
99
+ * @name PluginInstallResult
100
+ * @description Returned by {@link SynapsePlugin.install}. The `executor`
101
+ * dispatches incoming tool calls to the correct SapClient module method.
102
+ * The optional `teardown` callback is invoked on `kit.destroy()`.
103
+ * @category Plugin
104
+ * @since v0.1.0
105
+ */
106
+ export interface PluginInstallResult {
107
+ /** Main executor dispatching method calls. */
108
+ executor: (method: ProtocolMethod, input: unknown) => Promise<unknown>;
109
+ /** Optional teardown for cleanup on `kit.destroy()`. */
110
+ teardown?: () => Promise<void>;
111
+ }
112
+
113
+ /**
114
+ * The SynapsePlugin interface (duck-typed for zero external deps).
115
+ *
116
+ * @interface SynapsePlugin
117
+ * @name SynapsePlugin
118
+ * @description Core contract for SynapseAgentKit plugins. Provides metadata,
119
+ * protocol definitions, and an `install()` entry point that yields
120
+ * a tool executor.
121
+ * @category Plugin
122
+ * @since v0.1.0
123
+ */
124
+ export interface SynapsePlugin {
125
+ readonly meta: PluginMeta;
126
+ readonly protocols: readonly PluginProtocol[];
127
+ install(context: PluginContext): PluginInstallResult;
128
+ }
129
+
130
+ /**
131
+ * Configuration for the SAP plugin.
132
+ *
133
+ * @interface SAPPluginConfig
134
+ * @name SAPPluginConfig
135
+ * @description Options passed to {@link createSAPPlugin} to instantiate
136
+ * the SAP v2 plugin with an Anchor provider and optional program ID override.
137
+ * @category Plugin
138
+ * @since v0.1.0
139
+ */
140
+ export interface SAPPluginConfig {
141
+ /**
142
+ * Anchor provider with wallet signer.
143
+ * Required for all write operations (transactions).
144
+ */
145
+ readonly provider: AnchorProvider;
146
+
147
+ /**
148
+ * Override the SAP program ID.
149
+ * Defaults to the canonical program ID from constants.
150
+ */
151
+ readonly programId?: PublicKey;
152
+ }
153
+
154
+ // ═══════════════════════════════════════════════════════════════════
155
+ // Version
156
+ // ═══════════════════════════════════════════════════════════════════
157
+
158
+ /**
159
+ * Current SAP plugin version string.
160
+ *
161
+ * @name SAP_PLUGIN_VERSION
162
+ * @description Semver version embedded in the plugin’s {@link PluginMeta}.
163
+ * @internal
164
+ * @since v0.1.0
165
+ */
166
+ const SAP_PLUGIN_VERSION = "0.1.0";
167
+
168
+ // ═══════════════════════════════════════════════════════════════════
169
+ // Serialization Helpers
170
+ // ═══════════════════════════════════════════════════════════════════
171
+
172
+ /**
173
+ * Convert a base58 string to PublicKey.
174
+ *
175
+ * @name toPubkey
176
+ * @description Returns `undefined` for nullish input, otherwise wraps
177
+ * the base58 string in a Solana `PublicKey`.
178
+ * @param value - Base58-encoded public key or nullish
179
+ * @returns A `PublicKey` instance, or `undefined`
180
+ * @internal
181
+ * @since v0.1.0
182
+ */
183
+ function toPubkey(value: string | null | undefined): PublicKey | undefined {
184
+ return value ? new PublicKey(value) : undefined;
185
+ }
186
+
187
+ /**
188
+ * Convert a string amount to BN.
189
+ *
190
+ * @name toBN
191
+ * @description Returns `undefined` for nullish input, otherwise parses
192
+ * the string as a `BN` (big number) instance.
193
+ * @param value - Numeric string or nullish
194
+ * @returns A `BN` instance, or `undefined`
195
+ * @internal
196
+ * @since v0.1.0
197
+ */
198
+ function toBN(value: string | null | undefined): BN | undefined {
199
+ return value ? new BN(value) : undefined;
200
+ }
201
+
202
+ /**
203
+ * Convert a hex string to a Node.js Buffer.
204
+ *
205
+ * @name hexToBuffer
206
+ * @description Used for encrypted data payloads before sending to
207
+ * on-chain instructions.
208
+ * @param hex - Hex-encoded string
209
+ * @returns Buffer of decoded bytes
210
+ * @internal
211
+ * @since v0.1.0
212
+ */
213
+ function hexToBuffer(hex: string): Buffer {
214
+ return Buffer.from(hex, "hex");
215
+ }
216
+
217
+ /**
218
+ * Convert a hex string to Uint8Array for PDA seed derivation.
219
+ *
220
+ * @name hexToBytes
221
+ * @description Wraps `Buffer.from(hex, 'hex')` into a `Uint8Array`
222
+ * suitable for Solana PDA seed arrays.
223
+ * @param hex - Hex-encoded string
224
+ * @returns Uint8Array of decoded bytes
225
+ * @internal
226
+ * @since v0.1.0
227
+ */
228
+ function hexToBytes(hex: string): Uint8Array {
229
+ return new Uint8Array(Buffer.from(hex, "hex"));
230
+ }
231
+
232
+ /**
233
+ * Map token type strings to Anchor enum variant objects.
234
+ *
235
+ * @name toTokenType
236
+ * @description Converts `"sol"`, `"usdc"`, or `"spl"` to the
237
+ * corresponding Anchor enum variant. Defaults to `{ sol: {} }`.
238
+ * @param s - Token type string
239
+ * @returns Anchor-compatible enum variant object
240
+ * @internal
241
+ * @since v0.1.0
242
+ */
243
+ function toTokenType(s: string): object {
244
+ const map: Record<string, object> = {
245
+ sol: { sol: {} },
246
+ usdc: { usdc: {} },
247
+ spl: { spl: {} },
248
+ };
249
+ return map[s] ?? { sol: {} };
250
+ }
251
+
252
+ /**
253
+ * Map settlement mode strings to Anchor enum variant objects.
254
+ *
255
+ * @name toSettlementMode
256
+ * @description Converts `"instant"`, `"escrow"`, `"batched"`, or `"x402"`
257
+ * to the corresponding Anchor enum variant. Returns `null` for nullish input.
258
+ * @param s - Settlement mode string or nullish
259
+ * @returns Anchor-compatible enum variant object, or `null`
260
+ * @internal
261
+ * @since v0.1.0
262
+ */
263
+ function toSettlementMode(s: string | null | undefined): object | null {
264
+ if (!s) return null;
265
+ const map: Record<string, object> = {
266
+ instant: { instant: {} },
267
+ escrow: { escrow: {} },
268
+ batched: { batched: {} },
269
+ x402: { x402: {} },
270
+ };
271
+ return map[s] ?? null;
272
+ }
273
+
274
+ /**
275
+ * Map HTTP method strings to numeric enum values.
276
+ *
277
+ * @name toHttpMethodNum
278
+ * @description Converts lowercase method names (`"get"`, `"post"`, etc.)
279
+ * to their on-chain numeric representation. Defaults to `0` (GET).
280
+ * @param s - HTTP method string
281
+ * @returns Numeric enum value
282
+ * @internal
283
+ * @since v0.1.0
284
+ */
285
+ function toHttpMethodNum(s: string): number {
286
+ const map: Record<string, number> = {
287
+ get: 0,
288
+ post: 1,
289
+ put: 2,
290
+ delete: 3,
291
+ compound: 4,
292
+ };
293
+ return map[s] ?? 0;
294
+ }
295
+
296
+ /**
297
+ * Map category strings to numeric enum values.
298
+ *
299
+ * @name toCategoryNum
300
+ * @description Converts category names (`"swap"`, `"lend"`, etc.)
301
+ * to their on-chain numeric representation. Defaults to `9` (custom).
302
+ * @param s - Category string
303
+ * @returns Numeric enum value
304
+ * @internal
305
+ * @since v0.1.0
306
+ */
307
+ function toCategoryNum(s: string): number {
308
+ const map: Record<string, number> = {
309
+ swap: 0,
310
+ lend: 1,
311
+ stake: 2,
312
+ nft: 3,
313
+ payment: 4,
314
+ data: 5,
315
+ governance: 6,
316
+ bridge: 7,
317
+ analytics: 8,
318
+ custom: 9,
319
+ };
320
+ return map[s] ?? 9;
321
+ }
322
+
323
+ /**
324
+ * Map schema type strings to numeric enum values.
325
+ *
326
+ * @name toSchemaTypeNum
327
+ * @description Converts `"input"`, `"output"`, or `"description"`
328
+ * to their on-chain numeric representation. Defaults to `0` (input).
329
+ * @param s - Schema type string
330
+ * @returns Numeric enum value
331
+ * @internal
332
+ * @since v0.1.0
333
+ */
334
+ function toSchemaTypeNum(s: string): number {
335
+ const map: Record<string, number> = { input: 0, output: 1, description: 2 };
336
+ return map[s] ?? 0;
337
+ }
338
+
339
+ /**
340
+ * Map compression type strings to numeric enum values.
341
+ *
342
+ * @name toCompressionNum
343
+ * @description Converts `"none"`, `"deflate"`, `"gzip"`, or `"brotli"`
344
+ * to their on-chain numeric representation. Defaults to `0` (none).
345
+ * @param s - Compression type string
346
+ * @returns Numeric enum value
347
+ * @internal
348
+ * @since v0.1.0
349
+ */
350
+ function toCompressionNum(s: string): number {
351
+ const map: Record<string, number> = {
352
+ none: 0,
353
+ deflate: 1,
354
+ gzip: 2,
355
+ brotli: 3,
356
+ };
357
+ return map[s] ?? 0;
358
+ }
359
+
360
+ /**
361
+ * Convert a JSON-serialized pricing tier from plugin input
362
+ * into the SDK’s PricingTier shape with BN/PublicKey/enum conversions.
363
+ *
364
+ * @name toPricingTier
365
+ * @description Transforms raw JSON input (string amounts, string enums)
366
+ * into the native Anchor-compatible shape with `BN`, `PublicKey`,
367
+ * and enum variant objects.
368
+ * @param raw - Raw pricing tier object from Zod-validated plugin input
369
+ * @returns Anchor-compatible PricingTier object
370
+ * @internal
371
+ * @since v0.1.0
372
+ */
373
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
374
+ function toPricingTier(raw: any): any {
375
+ return {
376
+ tierId: raw.tierId,
377
+ pricePerCall: new BN(raw.pricePerCall),
378
+ minPricePerCall: toBN(raw.minPricePerCall) ?? null,
379
+ maxPricePerCall: toBN(raw.maxPricePerCall) ?? null,
380
+ rateLimit: raw.rateLimit,
381
+ maxCallsPerSession: raw.maxCallsPerSession,
382
+ burstLimit: raw.burstLimit ?? null,
383
+ tokenType: toTokenType(raw.tokenType),
384
+ tokenMint: toPubkey(raw.tokenMint) ?? null,
385
+ tokenDecimals: raw.tokenDecimals ?? null,
386
+ settlementMode: toSettlementMode(raw.settlementMode),
387
+ minEscrowDeposit: toBN(raw.minEscrowDeposit) ?? null,
388
+ batchIntervalSec: raw.batchIntervalSec ?? null,
389
+ volumeCurve: raw.volumeCurve
390
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
391
+ ? raw.volumeCurve.map((v: any) => ({
392
+ afterCalls: v.afterCalls,
393
+ pricePerCall: new BN(v.pricePerCall),
394
+ }))
395
+ : null,
396
+ };
397
+ }
398
+
399
+ /**
400
+ * Serialize an on-chain account object to a JSON-safe shape.
401
+ *
402
+ * @name serializeAccount
403
+ * @description Recursively converts `PublicKey` → base58 string,
404
+ * `BN` → decimal string, and nested objects/arrays. Used to
405
+ * return tool output that is JSON-serializable for LLMs.
406
+ * @param obj - Raw account data record from Anchor deserialization
407
+ * @returns JSON-safe record with all Solana types converted to strings
408
+ * @internal
409
+ * @since v0.1.0
410
+ */
411
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
412
+ function serializeAccount(obj: Record<string, any>): Record<string, any> {
413
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
414
+ const result: Record<string, any> = {};
415
+ for (const [key, value] of Object.entries(obj)) {
416
+ if (value === null || value === undefined) {
417
+ result[key] = null;
418
+ } else if (value instanceof PublicKey) {
419
+ result[key] = value.toBase58();
420
+ } else if (BN.isBN(value)) {
421
+ result[key] = value.toString();
422
+ } else if (Array.isArray(value)) {
423
+ result[key] = value.map((item) => {
424
+ if (item instanceof PublicKey) return item.toBase58();
425
+ if (BN.isBN(item)) return item.toString();
426
+ if (typeof item === "object" && item !== null)
427
+ return serializeAccount(item);
428
+ return item;
429
+ });
430
+ } else if (typeof value === "object") {
431
+ result[key] = serializeAccount(value);
432
+ } else {
433
+ result[key] = value;
434
+ }
435
+ }
436
+ return result;
437
+ }
438
+
439
+ // ═══════════════════════════════════════════════════════════════════
440
+ // Executor Builder
441
+ // ═══════════════════════════════════════════════════════════════════
442
+
443
+ /**
444
+ * Build the executor function that dispatches incoming tool calls
445
+ * to the appropriate SapClient module method.
446
+ *
447
+ * @name buildExecutor
448
+ * @description Creates a closure over a `SapClient` instance that routes
449
+ * each incoming `ProtocolMethod` to the correct per-protocol dispatcher
450
+ * based on `method.protocol`.
451
+ * @param client - Initialized SapClient for the configured provider
452
+ * @returns Async executor function `(method, input) => Promise<unknown>`
453
+ * @internal
454
+ * @since v0.1.0
455
+ */
456
+ function buildExecutor(
457
+ client: SapClient,
458
+ ): (method: ProtocolMethod, input: unknown) => Promise<unknown> {
459
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
460
+ return async (method: ProtocolMethod, raw: any): Promise<unknown> => {
461
+ switch (method.protocol) {
462
+ // ────────────────────── Agent ──────────────────────
463
+ case "sap-agent":
464
+ return executeAgent(client, method.name, raw);
465
+
466
+ // ────────────────────── Feedback ───────────────────
467
+ case "sap-feedback":
468
+ return executeFeedback(client, method.name, raw);
469
+
470
+ // ────────────────────── Attestation ────────────────
471
+ case "sap-attestation":
472
+ return executeAttestation(client, method.name, raw);
473
+
474
+ // ────────────────────── Escrow ─────────────────────
475
+ case "sap-escrow":
476
+ return executeEscrow(client, method.name, raw);
477
+
478
+ // ────────────────────── Tools ──────────────────────
479
+ case "sap-tools":
480
+ return executeTools(client, method.name, raw);
481
+
482
+ // ────────────────────── Vault ──────────────────────
483
+ case "sap-vault":
484
+ return executeVault(client, method.name, raw);
485
+
486
+ // ────────────────────── Indexing ───────────────────
487
+ case "sap-indexing":
488
+ return executeIndexing(client, method.name, raw);
489
+
490
+ // ────────────────────── Ledger ─────────────────────
491
+ case "sap-ledger":
492
+ return executeLedger(client, method.name, raw);
493
+
494
+ default:
495
+ throw new Error(`Unknown SAP protocol: ${method.protocol}`);
496
+ }
497
+ };
498
+ }
499
+
500
+ // ═══════════════════════════════════════════════════════════════════
501
+ // Per-Protocol Dispatchers
502
+ // ═══════════════════════════════════════════════════════════════════
503
+
504
+ /**
505
+ * Dispatch tool calls for the **sap-agent** protocol.
506
+ *
507
+ * @name executeAgent
508
+ * @description Routes agent-domain method names to `client.agent.*` calls,
509
+ * handling argument conversion and account serialization.
510
+ * @param client - SapClient instance
511
+ * @param name - Method name (e.g. `"registerAgent"`)
512
+ * @param input - Zod-validated input from the LLM tool call
513
+ * @returns Transaction signature or serialized account data
514
+ * @internal
515
+ * @since v0.1.0
516
+ */
517
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
518
+ async function executeAgent(client: SapClient, name: string, input: any) {
519
+ switch (name) {
520
+ case "registerAgent": {
521
+ const args: RegisterAgentArgs = {
522
+ name: input.name,
523
+ description: input.description,
524
+ capabilities: input.capabilities,
525
+ pricing: (input.pricing ?? []).map(toPricingTier),
526
+ protocols: input.protocols ?? [],
527
+ agentId: input.agentId ?? null,
528
+ agentUri: input.agentUri ?? null,
529
+ x402Endpoint: input.x402Endpoint ?? null,
530
+ };
531
+ const tx = await client.agent.register(args);
532
+ return { txSignature: tx };
533
+ }
534
+
535
+ case "updateAgent": {
536
+ const args: UpdateAgentArgs = {
537
+ name: input.name ?? null,
538
+ description: input.description ?? null,
539
+ capabilities: input.capabilities ?? null,
540
+ pricing: input.pricing ? input.pricing.map(toPricingTier) : null,
541
+ protocols: input.protocols ?? null,
542
+ agentId: input.agentId ?? null,
543
+ agentUri: input.agentUri ?? null,
544
+ x402Endpoint: input.x402Endpoint ?? null,
545
+ };
546
+ const tx = await client.agent.update(args);
547
+ return { txSignature: tx };
548
+ }
549
+
550
+ case "deactivateAgent": {
551
+ const tx = await client.agent.deactivate();
552
+ return { txSignature: tx };
553
+ }
554
+
555
+ case "reactivateAgent": {
556
+ const tx = await client.agent.reactivate();
557
+ return { txSignature: tx };
558
+ }
559
+
560
+ case "reportCalls": {
561
+ const tx = await client.agent.reportCalls(input.callsServed);
562
+ return { txSignature: tx };
563
+ }
564
+
565
+ case "updateReputation": {
566
+ const tx = await client.agent.updateReputation(
567
+ input.avgLatencyMs,
568
+ input.uptimePercent,
569
+ );
570
+ return { txSignature: tx };
571
+ }
572
+
573
+ case "fetchAgent": {
574
+ const wallet = toPubkey(input.wallet);
575
+ const data = await client.agent.fetch(wallet);
576
+ return serializeAccount(data as unknown as Record<string, unknown>);
577
+ }
578
+
579
+ case "fetchGlobalRegistry": {
580
+ const data = await client.agent.fetchGlobalRegistry();
581
+ return serializeAccount(data as unknown as Record<string, unknown>);
582
+ }
583
+
584
+ default:
585
+ throw new Error(`Unknown sap-agent method: ${name}`);
586
+ }
587
+ }
588
+
589
+ /**
590
+ * Dispatch tool calls for the **sap-feedback** protocol.
591
+ *
592
+ * @name executeFeedback
593
+ * @description Routes feedback-domain method names to `client.feedback.*` calls.
594
+ * @param client - SapClient instance
595
+ * @param name - Method name (e.g. `"giveFeedback"`)
596
+ * @param input - Zod-validated input from the LLM tool call
597
+ * @returns Transaction signature or serialized account data
598
+ * @internal
599
+ * @since v0.1.0
600
+ */
601
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
602
+ async function executeFeedback(client: SapClient, name: string, input: any) {
603
+ switch (name) {
604
+ case "giveFeedback": {
605
+ const agentWallet = new PublicKey(input.agentWallet);
606
+ const args: GiveFeedbackArgs = {
607
+ score: input.score,
608
+ tag: input.tag,
609
+ commentHash: input.commentHash ?? null,
610
+ };
611
+ const tx = await client.feedback.give(agentWallet, args);
612
+ return { txSignature: tx };
613
+ }
614
+
615
+ case "updateFeedback": {
616
+ const agentWallet = new PublicKey(input.agentWallet);
617
+ const args: UpdateFeedbackArgs = {
618
+ newScore: input.newScore,
619
+ newTag: input.newTag ?? null,
620
+ commentHash: input.commentHash ?? null,
621
+ };
622
+ const tx = await client.feedback.update(agentWallet, args);
623
+ return { txSignature: tx };
624
+ }
625
+
626
+ case "revokeFeedback": {
627
+ const tx = await client.feedback.revoke(
628
+ new PublicKey(input.agentWallet),
629
+ );
630
+ return { txSignature: tx };
631
+ }
632
+
633
+ case "fetchFeedback": {
634
+ const [agentPda] = deriveAgent(new PublicKey(input.agentWallet));
635
+ const reviewer = toPubkey(input.reviewer);
636
+ const data = await client.feedback.fetch(agentPda, reviewer);
637
+ return serializeAccount(data as unknown as Record<string, unknown>);
638
+ }
639
+
640
+ default:
641
+ throw new Error(`Unknown sap-feedback method: ${name}`);
642
+ }
643
+ }
644
+
645
+ /**
646
+ * Dispatch tool calls for the **sap-attestation** protocol.
647
+ *
648
+ * @name executeAttestation
649
+ * @description Routes attestation-domain method names to `client.attestation.*` calls.
650
+ * @param client - SapClient instance
651
+ * @param name - Method name (e.g. `"createAttestation"`)
652
+ * @param input - Zod-validated input from the LLM tool call
653
+ * @returns Transaction signature or serialized account data
654
+ * @internal
655
+ * @since v0.1.0
656
+ */
657
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
658
+ async function executeAttestation(client: SapClient, name: string, input: any) {
659
+ switch (name) {
660
+ case "createAttestation": {
661
+ const args: CreateAttestationArgs = {
662
+ attestationType: input.attestationType,
663
+ metadataHash: input.metadataHash,
664
+ expiresAt: new BN(input.expiresAt),
665
+ };
666
+ const tx = await client.attestation.create(
667
+ new PublicKey(input.agentWallet),
668
+ args,
669
+ );
670
+ return { txSignature: tx };
671
+ }
672
+
673
+ case "revokeAttestation": {
674
+ const tx = await client.attestation.revoke(
675
+ new PublicKey(input.agentWallet),
676
+ );
677
+ return { txSignature: tx };
678
+ }
679
+
680
+ case "fetchAttestation": {
681
+ const [agentPda] = deriveAgent(new PublicKey(input.agentWallet));
682
+ const attester = toPubkey(input.attester);
683
+ const data = await client.attestation.fetch(agentPda, attester);
684
+ return serializeAccount(data as unknown as Record<string, unknown>);
685
+ }
686
+
687
+ default:
688
+ throw new Error(`Unknown sap-attestation method: ${name}`);
689
+ }
690
+ }
691
+
692
+ /**
693
+ * Dispatch tool calls for the **sap-escrow** protocol.
694
+ *
695
+ * @name executeEscrow
696
+ * @description Routes escrow-domain method names to `client.escrow.*` calls,
697
+ * including BN conversion for amounts and volume curves.
698
+ * @param client - SapClient instance
699
+ * @param name - Method name (e.g. `"createEscrow"`)
700
+ * @param input - Zod-validated input from the LLM tool call
701
+ * @returns Transaction signature or serialized account data
702
+ * @internal
703
+ * @since v0.1.0
704
+ */
705
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
706
+ async function executeEscrow(client: SapClient, name: string, input: any) {
707
+ switch (name) {
708
+ case "createEscrow": {
709
+ const args: CreateEscrowArgs = {
710
+ pricePerCall: new BN(input.pricePerCall),
711
+ maxCalls: new BN(input.maxCalls),
712
+ initialDeposit: new BN(input.initialDeposit),
713
+ expiresAt: new BN(input.expiresAt),
714
+ volumeCurve: (input.volumeCurve ?? []).map(
715
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
716
+ (v: any): VolumeCurveBreakpoint => ({
717
+ afterCalls: v.afterCalls,
718
+ pricePerCall: new BN(v.pricePerCall),
719
+ }),
720
+ ),
721
+ tokenMint: toPubkey(input.tokenMint) ?? null,
722
+ tokenDecimals: input.tokenDecimals ?? 9,
723
+ };
724
+ const tx = await client.escrow.create(
725
+ new PublicKey(input.agentWallet),
726
+ args,
727
+ );
728
+ return { txSignature: tx };
729
+ }
730
+
731
+ case "depositEscrow": {
732
+ const tx = await client.escrow.deposit(
733
+ new PublicKey(input.agentWallet),
734
+ new BN(input.amount),
735
+ );
736
+ return { txSignature: tx };
737
+ }
738
+
739
+ case "settleEscrow": {
740
+ const tx = await client.escrow.settle(
741
+ new PublicKey(input.depositorWallet),
742
+ new BN(input.callsToSettle),
743
+ input.serviceHash,
744
+ );
745
+ return { txSignature: tx };
746
+ }
747
+
748
+ case "withdrawEscrow": {
749
+ const tx = await client.escrow.withdraw(
750
+ new PublicKey(input.agentWallet),
751
+ new BN(input.amount),
752
+ );
753
+ return { txSignature: tx };
754
+ }
755
+
756
+ case "batchSettle": {
757
+ const settlements: Settlement[] = input.settlements.map(
758
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
759
+ (s: any) => ({
760
+ callsToSettle: new BN(s.callsToSettle),
761
+ serviceHash: s.serviceHash,
762
+ }),
763
+ );
764
+ const tx = await client.escrow.settleBatch(
765
+ new PublicKey(input.depositorWallet),
766
+ settlements,
767
+ );
768
+ return { txSignature: tx };
769
+ }
770
+
771
+ case "fetchEscrow": {
772
+ const [agentPda] = deriveAgent(new PublicKey(input.agentWallet));
773
+ const depositor = toPubkey(input.depositor);
774
+ const data = await client.escrow.fetch(agentPda, depositor);
775
+ return serializeAccount(data as unknown as Record<string, unknown>);
776
+ }
777
+
778
+ default:
779
+ throw new Error(`Unknown sap-escrow method: ${name}`);
780
+ }
781
+ }
782
+
783
+ /**
784
+ * Dispatch tool calls for the **sap-tools** protocol.
785
+ *
786
+ * @name executeTools
787
+ * @description Routes tools-domain method names to `client.tools.*` calls,
788
+ * handling schema hashing and enum conversions.
789
+ * @param client - SapClient instance
790
+ * @param name - Method name (e.g. `"publishToolByName"`)
791
+ * @param input - Zod-validated input from the LLM tool call
792
+ * @returns Transaction signature or serialized account data
793
+ * @internal
794
+ * @since v0.1.0
795
+ */
796
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
797
+ async function executeTools(client: SapClient, name: string, input: any) {
798
+ switch (name) {
799
+ case "publishToolByName": {
800
+ const tx = await client.tools.publishByName(
801
+ input.toolName,
802
+ input.protocolId,
803
+ input.description,
804
+ input.inputSchema,
805
+ input.outputSchema,
806
+ toHttpMethodNum(input.httpMethod),
807
+ toCategoryNum(input.category),
808
+ input.paramsCount,
809
+ input.requiredParams,
810
+ input.isCompound ?? false,
811
+ );
812
+ return { txSignature: tx };
813
+ }
814
+
815
+ case "inscribeToolSchema": {
816
+ const args: InscribeToolSchemaArgs = {
817
+ schemaType: toSchemaTypeNum(input.schemaType),
818
+ schemaData: Buffer.from(input.schemaData, "utf-8"),
819
+ schemaHash: hashToArray(sha256(input.schemaData)),
820
+ compression: toCompressionNum(input.compression ?? "none"),
821
+ };
822
+ const tx = await client.tools.inscribeSchema(input.toolName, args);
823
+ return { txSignature: tx };
824
+ }
825
+
826
+ case "updateTool": {
827
+ const args: UpdateToolArgs = {
828
+ descriptionHash: null,
829
+ inputSchemaHash: null,
830
+ outputSchemaHash: null,
831
+ httpMethod:
832
+ input.httpMethod != null ? toHttpMethodNum(input.httpMethod) : null,
833
+ category:
834
+ input.category != null ? toCategoryNum(input.category) : null,
835
+ paramsCount: input.paramsCount ?? null,
836
+ requiredParams: input.requiredParams ?? null,
837
+ };
838
+ const tx = await client.tools.update(input.toolName, args);
839
+ return { txSignature: tx };
840
+ }
841
+
842
+ case "deactivateTool": {
843
+ const tx = await client.tools.deactivate(input.toolName);
844
+ return { txSignature: tx };
845
+ }
846
+
847
+ case "reactivateTool": {
848
+ const tx = await client.tools.reactivate(input.toolName);
849
+ return { txSignature: tx };
850
+ }
851
+
852
+ case "reportInvocations": {
853
+ const tx = await client.tools.reportInvocations(
854
+ input.toolName,
855
+ input.invocations,
856
+ );
857
+ return { txSignature: tx };
858
+ }
859
+
860
+ case "fetchTool": {
861
+ const [agentPda] = deriveAgent(new PublicKey(input.agentWallet));
862
+ const data = await client.tools.fetch(agentPda, input.toolName);
863
+ return serializeAccount(data as unknown as Record<string, unknown>);
864
+ }
865
+
866
+ default:
867
+ throw new Error(`Unknown sap-tools method: ${name}`);
868
+ }
869
+ }
870
+
871
+ /**
872
+ * Dispatch tool calls for the **sap-vault** protocol.
873
+ *
874
+ * @name executeVault
875
+ * @description Routes vault-domain method names to `client.vault.*` calls,
876
+ * handling hex-to-bytes conversion and PDA derivation for sessions.
877
+ * @param client - SapClient instance
878
+ * @param name - Method name (e.g. `"initVault"`)
879
+ * @param input - Zod-validated input from the LLM tool call
880
+ * @returns Transaction signature or serialized account data
881
+ * @internal
882
+ * @since v0.1.0
883
+ */
884
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
885
+ async function executeVault(client: SapClient, name: string, input: any) {
886
+ switch (name) {
887
+ case "initVault": {
888
+ const tx = await client.vault.initVault(input.vaultNonce);
889
+ return { txSignature: tx };
890
+ }
891
+
892
+ case "openSession": {
893
+ const tx = await client.vault.openSession(input.sessionHash);
894
+ return { txSignature: tx };
895
+ }
896
+
897
+ case "inscribeMemory": {
898
+ const args: InscribeMemoryArgs = {
899
+ sequence: input.sequence,
900
+ encryptedData: hexToBuffer(input.encryptedData),
901
+ nonce: input.nonce,
902
+ contentHash: input.contentHash,
903
+ totalFragments: input.totalFragments ?? 1,
904
+ fragmentIndex: input.fragmentIndex ?? 0,
905
+ compression: input.compression ?? 0,
906
+ epochIndex: input.epochIndex ?? 0,
907
+ };
908
+ const tx = await client.vault.inscribe(args);
909
+ return { txSignature: tx };
910
+ }
911
+
912
+ case "closeSession": {
913
+ const [agentPda] = deriveAgent(client.walletPubkey);
914
+ const [vaultPda] = deriveVault(agentPda);
915
+ const sessionHashBytes = hexToBytes(input.sessionHash);
916
+ const [sessionPda] = deriveSession(vaultPda, sessionHashBytes);
917
+ const tx = await client.vault.closeSession(vaultPda, sessionPda);
918
+ return { txSignature: tx };
919
+ }
920
+
921
+ case "closeVault": {
922
+ const tx = await client.vault.closeVault();
923
+ return { txSignature: tx };
924
+ }
925
+
926
+ case "rotateNonce": {
927
+ const tx = await client.vault.rotateNonce(input.newNonce);
928
+ return { txSignature: tx };
929
+ }
930
+
931
+ case "addDelegate": {
932
+ const tx = await client.vault.addDelegate(
933
+ new PublicKey(input.delegatePubkey),
934
+ input.permissions,
935
+ BigInt(input.expiresAt),
936
+ );
937
+ return { txSignature: tx };
938
+ }
939
+
940
+ case "revokeDelegate": {
941
+ const tx = await client.vault.revokeDelegate(
942
+ new PublicKey(input.delegatePubkey),
943
+ );
944
+ return { txSignature: tx };
945
+ }
946
+
947
+ case "fetchVault": {
948
+ const wallet = toPubkey(input.agentWallet) ?? client.walletPubkey;
949
+ const [agentPda] = deriveAgent(wallet);
950
+ const data = await client.vault.fetchVault(agentPda);
951
+ return serializeAccount(data as unknown as Record<string, unknown>);
952
+ }
953
+
954
+ case "fetchSession": {
955
+ const wallet = new PublicKey(input.agentWallet);
956
+ const [agentPda] = deriveAgent(wallet);
957
+ const [vaultPda] = deriveVault(agentPda);
958
+ const sessionHashBytes = hexToBytes(input.sessionHash);
959
+ const data = await client.vault.fetchSession(vaultPda, sessionHashBytes);
960
+ return serializeAccount(data as unknown as Record<string, unknown>);
961
+ }
962
+
963
+ default:
964
+ throw new Error(`Unknown sap-vault method: ${name}`);
965
+ }
966
+ }
967
+
968
+ /**
969
+ * Dispatch tool calls for the **sap-indexing** protocol.
970
+ *
971
+ * @name executeIndexing
972
+ * @description Routes indexing-domain method names to `client.indexing.*` calls
973
+ * for capability and protocol index management.
974
+ * @param client - SapClient instance
975
+ * @param name - Method name (e.g. `"initCapabilityIndex"`)
976
+ * @param input - Zod-validated input from the LLM tool call
977
+ * @returns Transaction signature or serialized account data
978
+ * @internal
979
+ * @since v0.1.0
980
+ */
981
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
982
+ async function executeIndexing(client: SapClient, name: string, input: any) {
983
+ switch (name) {
984
+ case "initCapabilityIndex": {
985
+ const tx = await client.indexing.initCapabilityIndex(input.capabilityId);
986
+ return { txSignature: tx };
987
+ }
988
+ case "addToCapabilityIndex": {
989
+ const tx = await client.indexing.addToCapabilityIndex(input.capabilityId);
990
+ return { txSignature: tx };
991
+ }
992
+ case "removeFromCapabilityIndex": {
993
+ const tx = await client.indexing.removeFromCapabilityIndex(
994
+ input.capabilityId,
995
+ );
996
+ return { txSignature: tx };
997
+ }
998
+ case "initProtocolIndex": {
999
+ const tx = await client.indexing.initProtocolIndex(input.protocolId);
1000
+ return { txSignature: tx };
1001
+ }
1002
+ case "addToProtocolIndex": {
1003
+ const tx = await client.indexing.addToProtocolIndex(input.protocolId);
1004
+ return { txSignature: tx };
1005
+ }
1006
+ case "removeFromProtocolIndex": {
1007
+ const tx = await client.indexing.removeFromProtocolIndex(
1008
+ input.protocolId,
1009
+ );
1010
+ return { txSignature: tx };
1011
+ }
1012
+ case "fetchCapabilityIndex": {
1013
+ const data = await client.indexing.fetchCapabilityIndex(
1014
+ input.capabilityId,
1015
+ );
1016
+ return serializeAccount(data as unknown as Record<string, unknown>);
1017
+ }
1018
+ case "fetchProtocolIndex": {
1019
+ const data = await client.indexing.fetchProtocolIndex(input.protocolId);
1020
+ return serializeAccount(data as unknown as Record<string, unknown>);
1021
+ }
1022
+ default:
1023
+ throw new Error(`Unknown sap-indexing method: ${name}`);
1024
+ }
1025
+ }
1026
+
1027
+ /**
1028
+ * Dispatch tool calls for the **sap-ledger** protocol.
1029
+ *
1030
+ * @name executeLedger
1031
+ * @description Routes ledger-domain method names to `client.ledger.*` calls,
1032
+ * deriving session PDAs from hex-encoded session hashes.
1033
+ * @param client - SapClient instance
1034
+ * @param name - Method name (e.g. `"initLedger"`)
1035
+ * @param input - Zod-validated input from the LLM tool call
1036
+ * @returns Transaction signature or serialized account data
1037
+ * @internal
1038
+ * @since v0.1.0
1039
+ */
1040
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1041
+ async function executeLedger(client: SapClient, name: string, input: any) {
1042
+ const resolveSessionPda = (
1043
+ sessionHash: string,
1044
+ agentWallet?: string | null,
1045
+ ) => {
1046
+ const wallet = toPubkey(agentWallet) ?? client.walletPubkey;
1047
+ const [agentPda] = deriveAgent(wallet);
1048
+ const [vaultPda] = deriveVault(agentPda);
1049
+ const sessionHashBytes = hexToBytes(sessionHash);
1050
+ const [sessionPda] = deriveSession(vaultPda, sessionHashBytes);
1051
+ return sessionPda;
1052
+ };
1053
+
1054
+ switch (name) {
1055
+ case "initLedger": {
1056
+ const sessionPda = resolveSessionPda(
1057
+ input.sessionHash,
1058
+ input.agentWallet,
1059
+ );
1060
+ const tx = await client.ledger.init(sessionPda);
1061
+ return { txSignature: tx };
1062
+ }
1063
+
1064
+ case "writeLedger": {
1065
+ const sessionPda = resolveSessionPda(
1066
+ input.sessionHash,
1067
+ input.agentWallet,
1068
+ );
1069
+ const data = hexToBuffer(input.data);
1070
+ const tx = await client.ledger.write(sessionPda, data, input.contentHash);
1071
+ return { txSignature: tx };
1072
+ }
1073
+
1074
+ case "sealLedger": {
1075
+ const sessionPda = resolveSessionPda(
1076
+ input.sessionHash,
1077
+ input.agentWallet,
1078
+ );
1079
+ const tx = await client.ledger.seal(sessionPda);
1080
+ return { txSignature: tx };
1081
+ }
1082
+
1083
+ case "closeLedger": {
1084
+ const sessionPda = resolveSessionPda(
1085
+ input.sessionHash,
1086
+ input.agentWallet,
1087
+ );
1088
+ const tx = await client.ledger.close(sessionPda);
1089
+ return { txSignature: tx };
1090
+ }
1091
+
1092
+ case "fetchLedger": {
1093
+ const sessionPda = resolveSessionPda(
1094
+ input.sessionHash,
1095
+ input.agentWallet,
1096
+ );
1097
+ const data = await client.ledger.fetchLedger(sessionPda);
1098
+ return serializeAccount(data as unknown as Record<string, unknown>);
1099
+ }
1100
+
1101
+ case "fetchLedgerPage": {
1102
+ const sessionPda = resolveSessionPda(
1103
+ input.sessionHash,
1104
+ input.agentWallet,
1105
+ );
1106
+ const [ledgerPda] = deriveLedger(sessionPda);
1107
+ const data = await client.ledger.fetchPage(ledgerPda, input.pageIndex);
1108
+ return serializeAccount(data as unknown as Record<string, unknown>);
1109
+ }
1110
+
1111
+ default:
1112
+ throw new Error(`Unknown sap-ledger method: ${name}`);
1113
+ }
1114
+ }
1115
+
1116
+ // ═══════════════════════════════════════════════════════════════════
1117
+ // Plugin Factory
1118
+ // ═══════════════════════════════════════════════════════════════════
1119
+
1120
+ /**
1121
+ * Create a SynapsePlugin for SAP v2.
1122
+ *
1123
+ * @name createSAPPlugin
1124
+ * @description Factory function that returns a fully configured
1125
+ * {@link SynapsePlugin} exposing 52 tools across 8 on-chain
1126
+ * protocol domains. The plugin can be installed into a
1127
+ * `SynapseAgentKit` instance or used standalone.
1128
+ * @param config - Plugin configuration with Anchor provider and optional program ID
1129
+ * @returns A configured {@link SynapsePlugin} instance
1130
+ * @category Plugin
1131
+ * @since v0.1.0
1132
+ *
1133
+ * @example
1134
+ * ```ts
1135
+ * import { createSAPPlugin } from '@synapse-sap/sdk/plugin';
1136
+ *
1137
+ * const sapPlugin = createSAPPlugin({ provider });
1138
+ *
1139
+ * // Use with SynapseAgentKit:
1140
+ * kit.use(sapPlugin);
1141
+ *
1142
+ * // Or standalone:
1143
+ * const executor = sapPlugin.install({ client, config: {} }).executor;
1144
+ * await executor(someMethod, someInput);
1145
+ * ```
1146
+ */
1147
+ export function createSAPPlugin(config: SAPPluginConfig): SynapsePlugin {
1148
+ return {
1149
+ meta: {
1150
+ id: "sap",
1151
+ name: "SAP v2 — Solana Agent Protocol",
1152
+ version: SAP_PLUGIN_VERSION,
1153
+ description:
1154
+ "On-chain agent identity, reputation, tool registry, encrypted memory vault, x402 escrow, attestation, discovery indexes, and unified memory ledger on Solana",
1155
+ tags: [
1156
+ "solana",
1157
+ "agent",
1158
+ "identity",
1159
+ "reputation",
1160
+ "x402",
1161
+ "escrow",
1162
+ "vault",
1163
+ "memory",
1164
+ "tools",
1165
+ "attestation",
1166
+ "erc-8004",
1167
+ ],
1168
+ },
1169
+
1170
+ protocols: SAP_PROTOCOLS,
1171
+
1172
+ install(_context: PluginContext): PluginInstallResult {
1173
+ const client = SapClient.from(config.provider, config.programId);
1174
+ const executor = buildExecutor(client);
1175
+
1176
+ return { executor };
1177
+ },
1178
+ };
1179
+ }
1180
+
1181
+ /**
1182
+ * Pre-built plugin object for static use patterns.
1183
+ *
1184
+ * @name SAPPlugin
1185
+ * @description Convenience namespace exposing a `configure` method
1186
+ * that delegates to {@link createSAPPlugin}. Useful for concise
1187
+ * one-liner instantiation.
1188
+ * @category Plugin
1189
+ * @since v0.1.0
1190
+ *
1191
+ * @example
1192
+ * ```ts
1193
+ * import { SAPPlugin } from '@synapse-sap/sdk/plugin';
1194
+ *
1195
+ * const plugin = SAPPlugin.configure({ provider });
1196
+ * kit.use(plugin);
1197
+ * ```
1198
+ */
1199
+ export const SAPPlugin = {
1200
+ /** Create a configured SynapsePlugin from an AnchorProvider. */
1201
+ configure: createSAPPlugin,
1202
+ } as const;