@the-ai-company/cbio-node-runtime 1.41.0 → 1.43.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.
@@ -0,0 +1,155 @@
1
+ import http from "node:http";
2
+ import {
3
+ createIdentity,
4
+ createVault,
5
+ createAgentClient,
6
+ createVaultService,
7
+ handleVaultHttpDispatch,
8
+ AgentDispatchHttpTransport,
9
+ MemoryStorageProvider,
10
+ } from "../src/runtime/index.js";
11
+ import { LocalSigner } from "../src/protocol/crypto.js";
12
+
13
+ /**
14
+ * This example demonstrates the A/B Process Architecture (Process Isolation).
15
+ *
16
+ * - Process B (The Vault): Hosts the actual secrets and performs the HTTP dispatch.
17
+ * - Process A (The Agent): Signs requests and sends them to Process B. A never sees the secret.
18
+ */
19
+
20
+ // --- Process B: The Vault Server Logic ---
21
+ async function startVaultServer(port: number) {
22
+ const ownerIdentity = createIdentity({ nickname: "vault-owner" });
23
+ const storage = new MemoryStorageProvider();
24
+
25
+ // Create a real vault in memory
26
+ const { core } = await createVault(storage, {
27
+ vaultId: "vault-isolated-server",
28
+ ownerIdentity,
29
+ });
30
+
31
+ // Wrap as a Service
32
+ const service = createVaultService((core as any)._deps);
33
+
34
+ const server = http.createServer(async (req, res) => {
35
+ if (req.method === "POST" && req.url === "/dispatch") {
36
+ let body = "";
37
+ for await (const chunk of req) body += chunk;
38
+
39
+ console.log("[Process B] Received dispatch request from Agent");
40
+
41
+ try {
42
+ const result = await handleVaultHttpDispatch(service, JSON.parse(body));
43
+ res.writeHead(200, { "Content-Type": "application/json" });
44
+ res.end(JSON.stringify(result));
45
+ } catch (err) {
46
+ const message = err instanceof Error ? err.message : String(err);
47
+ res.writeHead(500);
48
+ res.end(JSON.stringify({ ok: false, error: { code: "SERVER_ERROR", message } }));
49
+ }
50
+ } else {
51
+ res.writeHead(404).end();
52
+ }
53
+ });
54
+
55
+ return new Promise<{ server: http.Server; ownerIdentity: any; vault: any }>((resolve) => {
56
+ server.listen(port, () => {
57
+ console.log(`[Process B] Vault Server listening on port ${port}`);
58
+ resolve({ server, ownerIdentity, vault: core });
59
+ });
60
+ });
61
+ }
62
+
63
+ // --- Process A: The LLM Agent Logic ---
64
+ async function runAgentDemo(port: number, agentIdentity: any, capability: any) {
65
+ // Process A ONLY knows the remote URL and its own Agent Identity.
66
+ // It has NO access to the Vault's master key or storage.
67
+ const transport = new AgentDispatchHttpTransport(`http://localhost:${port}/dispatch`);
68
+
69
+ const agentClient = createAgentClient({
70
+ agentIdentity,
71
+ capability,
72
+ transport,
73
+ signer: new LocalSigner(agentIdentity),
74
+ });
75
+
76
+ console.log("[Process A] LLM Agent requesting secret-backed dispatch...");
77
+
78
+ try {
79
+ const result = await agentClient.dispatch({
80
+ secretAlias: "api-token",
81
+ targetUrl: "https://httpbin.org/post",
82
+ method: "POST",
83
+ body: JSON.stringify({ message: "Hello from isolated Process A" }),
84
+ });
85
+
86
+ console.log("[Process A] Dispatch Result Status:", result.status);
87
+ console.log("[Process A] (Secret was injected by Process B and never touched Process A's memory)");
88
+ } catch (err) {
89
+ const message = err instanceof Error ? err.message : String(err);
90
+ console.error("[Process A] Dispatch failed:", message);
91
+ }
92
+ }
93
+
94
+ // --- Orchestration ---
95
+ async function main() {
96
+ const PORT = 3456;
97
+
98
+ // 1. Start the "Vault Server" (Process B)
99
+ const { ownerIdentity, vault, server } = await startVaultServer(PORT);
100
+
101
+ // 2. Setup: Owner (in Process B's context) grants permission to an Agent
102
+ const agentIdentity = createIdentity({ nickname: "llm-agent-1" });
103
+
104
+ // Owner registers the agent and a capability (simulated local call for setup)
105
+ await vault.registerAgentIdentity({
106
+ vaultId: vault.vaultId,
107
+ owner: { kind: "owner", id: ownerIdentity.identityId },
108
+ agentIdentity: {
109
+ vaultId: vault.vaultId,
110
+ agentId: agentIdentity.identityId,
111
+ publicKey: agentIdentity.publicKey,
112
+ },
113
+ proof: { signature: "setup-proof", ownerId: ownerIdentity.identityId, requestedAt: new Date().toISOString() },
114
+ });
115
+
116
+ // Owner writes a secret (simulated local call for setup)
117
+ const secret = await vault.writeSecret({
118
+ kind: "owner.write_secret",
119
+ vaultId: vault.vaultId,
120
+ owner: { kind: "owner", id: ownerIdentity.identityId },
121
+ alias: "api-token",
122
+ plaintext: "SK-PROD-12345",
123
+ targetBindings: [{ kind: "site", targetId: "httpbin.org", targetUrl: "https://httpbin.org/post", methods: ["POST"] }],
124
+ requestedAt: new Date().toISOString(),
125
+ proof: { signature: "setup-proof", ownerId: ownerIdentity.identityId, requestedAt: new Date().toISOString() },
126
+ });
127
+
128
+ const capability = {
129
+ vaultId: vault.vaultId,
130
+ capabilityId: "cap-llm-1",
131
+ agentId: agentIdentity.identityId,
132
+ secretIds: [secret.secretId.value],
133
+ secretAliases: ["api-token"],
134
+ operation: "dispatch_http" as const,
135
+ allowedTargets: ["https://httpbin.org/post"],
136
+ allowedMethods: ["POST"],
137
+ issuedAt: new Date().toISOString(),
138
+ };
139
+
140
+ await vault.registerCapability({
141
+ vaultId: vault.vaultId,
142
+ owner: { kind: "owner", id: ownerIdentity.identityId },
143
+ capability,
144
+ proof: { signature: "setup-proof", ownerId: ownerIdentity.identityId, requestedAt: new Date().toISOString() },
145
+ });
146
+
147
+ // 3. Run the "LLM Agent" (Process A)
148
+ await runAgentDemo(PORT, agentIdentity, capability);
149
+
150
+ // 4. Cleanup
151
+ server.close();
152
+ console.log("Demo finished.");
153
+ }
154
+
155
+ main().catch(console.error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@the-ai-company/cbio-node-runtime",
3
- "version": "1.41.0",
3
+ "version": "1.43.0",
4
4
  "description": "Node.js runtime for cbio identity and credential vault. Library only, no CLI or TUI.",
5
5
  "type": "module",
6
6
  "main": "./dist/runtime/index.js",