2020117-agent 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/dist/adapters/exec-processor.d.ts +19 -0
- package/dist/adapters/exec-processor.js +75 -0
- package/dist/adapters/http-processor.d.ts +18 -0
- package/dist/adapters/http-processor.js +102 -0
- package/dist/adapters/none-processor.d.ts +13 -0
- package/dist/adapters/none-processor.js +18 -0
- package/dist/adapters/ollama-processor.d.ts +15 -0
- package/dist/adapters/ollama-processor.js +29 -0
- package/dist/adapters/ollama.d.ts +30 -0
- package/dist/adapters/ollama.js +86 -0
- package/dist/agent.d.ts +17 -0
- package/dist/agent.js +663 -0
- package/dist/api.d.ts +78 -0
- package/dist/api.js +267 -0
- package/dist/cashu.d.ts +41 -0
- package/dist/cashu.js +95 -0
- package/dist/customer.d.ts +18 -0
- package/dist/customer.js +200 -0
- package/dist/pipeline.d.ts +16 -0
- package/dist/pipeline.js +227 -0
- package/dist/processor.d.ts +25 -0
- package/dist/processor.js +35 -0
- package/dist/provider.d.ts +17 -0
- package/dist/provider.js +238 -0
- package/dist/swarm.d.ts +68 -0
- package/dist/swarm.js +122 -0
- package/package.json +41 -0
package/dist/swarm.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hyperswarm P2P helper — discover peers and establish encrypted connections
|
|
3
|
+
*
|
|
4
|
+
* Provider: joins a topic (hash of service kind) and listens for connections
|
|
5
|
+
* Customer: joins the same topic to find the provider
|
|
6
|
+
*
|
|
7
|
+
* Wire protocol (newline-delimited JSON) — streaming payment:
|
|
8
|
+
* → { type: "request", id, kind, input, budget } customer sends job with budget
|
|
9
|
+
* ← { type: "offer", id, sats_per_chunk, chunks_per_payment } provider quotes price
|
|
10
|
+
* → { type: "payment", id, token } customer sends micro-token
|
|
11
|
+
* ← { type: "payment_ack", id, amount } provider confirms + accepts
|
|
12
|
+
* ← { type: "accepted", id } provider starts generating
|
|
13
|
+
* ← { type: "chunk", id, data } streaming output (N chunks)
|
|
14
|
+
* ← { type: "pay_required", id, earned, next } provider pauses for payment
|
|
15
|
+
* → { type: "payment", id, token } customer sends next micro-token
|
|
16
|
+
* ← { type: "payment_ack", id, amount } provider confirms
|
|
17
|
+
* ← { type: "chunk", id, data } more chunks...
|
|
18
|
+
* ← { type: "result", id, output, total_sats } final result
|
|
19
|
+
* → { type: "stop", id } customer stops early
|
|
20
|
+
* ← { type: "error", id, message } error
|
|
21
|
+
*/
|
|
22
|
+
import Hyperswarm from 'hyperswarm';
|
|
23
|
+
import { createHash } from 'crypto';
|
|
24
|
+
import { EventEmitter } from 'events';
|
|
25
|
+
/**
|
|
26
|
+
* Create a deterministic topic hash from a service kind number.
|
|
27
|
+
* All providers of kind 5100 join the same topic so customers can find them.
|
|
28
|
+
*/
|
|
29
|
+
export function topicFromKind(kind) {
|
|
30
|
+
return createHash('sha256').update(`2020117-dvm-kind-${kind}`).digest();
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Thin wrapper around Hyperswarm that handles JSON message framing
|
|
34
|
+
*/
|
|
35
|
+
export class SwarmNode extends EventEmitter {
|
|
36
|
+
swarm;
|
|
37
|
+
connections = new Map(); // remotePublicKey hex → socket
|
|
38
|
+
buffers = new Map();
|
|
39
|
+
constructor() {
|
|
40
|
+
super();
|
|
41
|
+
this.swarm = new Hyperswarm();
|
|
42
|
+
this.swarm.on('connection', (socket, info) => {
|
|
43
|
+
const peerId = socket.remotePublicKey?.toString('hex') ?? 'unknown';
|
|
44
|
+
console.log(`[swarm] Peer connected: ${peerId.slice(0, 12)}... (client=${info.client})`);
|
|
45
|
+
this.connections.set(peerId, socket);
|
|
46
|
+
this.buffers.set(peerId, '');
|
|
47
|
+
socket.on('data', (buf) => {
|
|
48
|
+
const existing = this.buffers.get(peerId) ?? '';
|
|
49
|
+
const combined = existing + buf.toString();
|
|
50
|
+
const lines = combined.split('\n');
|
|
51
|
+
// Last element is incomplete (or empty after trailing newline)
|
|
52
|
+
this.buffers.set(peerId, lines.pop());
|
|
53
|
+
for (const line of lines) {
|
|
54
|
+
if (!line.trim())
|
|
55
|
+
continue;
|
|
56
|
+
try {
|
|
57
|
+
const msg = JSON.parse(line);
|
|
58
|
+
this.emit('message', msg, socket, peerId);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
console.warn(`[swarm] Bad JSON from ${peerId.slice(0, 12)}: ${line.slice(0, 80)}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
socket.on('error', (err) => {
|
|
66
|
+
console.error(`[swarm] Socket error (${peerId.slice(0, 12)}): ${err.message}`);
|
|
67
|
+
});
|
|
68
|
+
socket.on('close', () => {
|
|
69
|
+
console.log(`[swarm] Peer disconnected: ${peerId.slice(0, 12)}`);
|
|
70
|
+
this.connections.delete(peerId);
|
|
71
|
+
this.buffers.delete(peerId);
|
|
72
|
+
this.emit('peer-leave', peerId);
|
|
73
|
+
});
|
|
74
|
+
this.emit('peer-join', socket, peerId, info);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** Send a JSON message to a specific peer */
|
|
78
|
+
send(socket, msg) {
|
|
79
|
+
socket.write(JSON.stringify(msg) + '\n');
|
|
80
|
+
}
|
|
81
|
+
/** Broadcast a JSON message to all connected peers */
|
|
82
|
+
broadcast(msg) {
|
|
83
|
+
const data = JSON.stringify(msg) + '\n';
|
|
84
|
+
for (const socket of this.connections.values()) {
|
|
85
|
+
socket.write(data);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Join a topic as server (provider) */
|
|
89
|
+
async listen(topic) {
|
|
90
|
+
const discovery = this.swarm.join(topic, { server: true, client: false });
|
|
91
|
+
await discovery.flushed();
|
|
92
|
+
console.log(`[swarm] Listening on topic: ${topic.toString('hex').slice(0, 16)}...`);
|
|
93
|
+
}
|
|
94
|
+
/** Join a topic as client (customer) */
|
|
95
|
+
async connect(topic) {
|
|
96
|
+
const discovery = this.swarm.join(topic, { server: false, client: true });
|
|
97
|
+
await discovery.flushed();
|
|
98
|
+
console.log(`[swarm] Looking for peers on topic: ${topic.toString('hex').slice(0, 16)}...`);
|
|
99
|
+
}
|
|
100
|
+
/** Wait for the first peer connection (with timeout) */
|
|
101
|
+
waitForPeer(timeoutMs = 15000) {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
reject(new Error(`No peer found within ${timeoutMs}ms`));
|
|
105
|
+
}, timeoutMs);
|
|
106
|
+
this.once('peer-join', (socket, peerId) => {
|
|
107
|
+
clearTimeout(timer);
|
|
108
|
+
resolve({ socket, peerId });
|
|
109
|
+
});
|
|
110
|
+
// If already have connections, resolve immediately
|
|
111
|
+
if (this.connections.size > 0) {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
const [peerId, socket] = this.connections.entries().next().value;
|
|
114
|
+
resolve({ socket, peerId });
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async destroy() {
|
|
119
|
+
await this.swarm.destroy();
|
|
120
|
+
console.log('[swarm] Destroyed');
|
|
121
|
+
}
|
|
122
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "2020117-agent",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "2020117 agent runtime — API polling + Hyperswarm P2P + Cashu streaming payments",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"2020117-agent": "./dist/agent.js",
|
|
8
|
+
"2020117-customer": "./dist/customer.js",
|
|
9
|
+
"2020117-provider": "./dist/provider.js",
|
|
10
|
+
"2020117-pipeline": "./dist/pipeline.js"
|
|
11
|
+
},
|
|
12
|
+
"files": ["dist"],
|
|
13
|
+
"exports": {
|
|
14
|
+
"./processor": "./dist/processor.js",
|
|
15
|
+
"./swarm": "./dist/swarm.js",
|
|
16
|
+
"./cashu": "./dist/cashu.js",
|
|
17
|
+
"./api": "./dist/api.js"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"agent": "node dist/agent.js",
|
|
21
|
+
"customer": "node dist/customer.js",
|
|
22
|
+
"provider": "node dist/provider.js",
|
|
23
|
+
"pipeline": "node dist/pipeline.js",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"prepublishOnly": "tsc",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"dev:agent": "npx tsx src/agent.ts",
|
|
28
|
+
"dev:customer": "npx tsx src/customer.ts",
|
|
29
|
+
"dev:provider": "npx tsx src/provider.ts",
|
|
30
|
+
"dev:pipeline": "npx tsx src/pipeline.ts"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@cashu/cashu-ts": "^3.5.0",
|
|
34
|
+
"hyperswarm": "^4.17.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.0.0",
|
|
38
|
+
"tsx": "^4.19.0",
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
}
|
|
41
|
+
}
|