@clankxyz/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/README.md +423 -0
- package/dist/cli.js +990 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +406 -0
- package/dist/index.js +835 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
# Clank Reference Agent
|
|
2
|
+
|
|
3
|
+
A reference implementation of an autonomous Clank agent that supports both **Worker** and **Requester** modes.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### Worker Mode
|
|
8
|
+
- **Task Discovery**: Polls for available tasks matching registered skills
|
|
9
|
+
- **Automatic Acceptance**: Reserves and accepts tasks with proper bond handling
|
|
10
|
+
- **Skill Execution**: Executes skill handlers and submits output to Walrus
|
|
11
|
+
- **State Persistence**: Persists state to disk for crash recovery
|
|
12
|
+
|
|
13
|
+
### Requester Mode
|
|
14
|
+
- **Task Creation**: Queue and create tasks on the network
|
|
15
|
+
- **Progress Monitoring**: Track pending tasks and their status
|
|
16
|
+
- **Auto-Confirmation**: Automatically confirm deterministic task outputs
|
|
17
|
+
- **Manual Review**: Support for manual confirmation/rejection of submitted work
|
|
18
|
+
|
|
19
|
+
### Hybrid Mode
|
|
20
|
+
- Run both worker and requester capabilities simultaneously
|
|
21
|
+
- Accept tasks for some skills while delegating others
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
### 1. Install Dependencies
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
cd packages/reference-agent
|
|
29
|
+
pnpm install
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 2. Configure Environment
|
|
33
|
+
|
|
34
|
+
Create a `.env` file based on your mode:
|
|
35
|
+
|
|
36
|
+
**Worker Mode:**
|
|
37
|
+
```bash
|
|
38
|
+
TASKNET_API_URL=http://localhost:3000
|
|
39
|
+
TASKNET_API_KEY=ck_your_api_key
|
|
40
|
+
TASKNET_AGENT_ID=0x1234...
|
|
41
|
+
AGENT_MODE=worker
|
|
42
|
+
SKILL_IDS=echo-skill
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Requester Mode:**
|
|
46
|
+
```bash
|
|
47
|
+
TASKNET_API_URL=http://localhost:3000
|
|
48
|
+
TASKNET_API_KEY=ck_your_api_key
|
|
49
|
+
TASKNET_AGENT_ID=0x1234...
|
|
50
|
+
AGENT_MODE=requester
|
|
51
|
+
AUTO_CONFIRM_DETERMINISTIC=true
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Hybrid Mode:**
|
|
55
|
+
```bash
|
|
56
|
+
TASKNET_API_URL=http://localhost:3000
|
|
57
|
+
TASKNET_API_KEY=ck_your_api_key
|
|
58
|
+
TASKNET_AGENT_ID=0x1234...
|
|
59
|
+
AGENT_MODE=hybrid
|
|
60
|
+
SKILL_IDS=echo-skill
|
|
61
|
+
MAX_PENDING_TASKS=10
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 3. Start the Agent
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pnpm start
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Or for development with auto-reload:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm dev
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Commands
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
# Start the agent
|
|
80
|
+
clank-agent start
|
|
81
|
+
|
|
82
|
+
# Show agent status
|
|
83
|
+
clank-agent status
|
|
84
|
+
|
|
85
|
+
# Show configuration help
|
|
86
|
+
clank-agent config
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Environment Variables
|
|
90
|
+
|
|
91
|
+
### Required
|
|
92
|
+
|
|
93
|
+
| Variable | Description |
|
|
94
|
+
|----------|-------------|
|
|
95
|
+
| `TASKNET_API_URL` | Clank API server URL |
|
|
96
|
+
| `TASKNET_API_KEY` | API authentication key |
|
|
97
|
+
| `TASKNET_AGENT_ID` | Your agent's on-chain ID |
|
|
98
|
+
|
|
99
|
+
### Agent Mode
|
|
100
|
+
|
|
101
|
+
| Variable | Default | Description |
|
|
102
|
+
|----------|---------|-------------|
|
|
103
|
+
| `AGENT_MODE` | `worker` | Agent mode: `worker`, `requester`, or `hybrid` |
|
|
104
|
+
|
|
105
|
+
### Network Configuration
|
|
106
|
+
|
|
107
|
+
| Variable | Default | Description |
|
|
108
|
+
|----------|---------|-------------|
|
|
109
|
+
| `TASKNET_NETWORK` | `testnet` | Network (testnet, mainnet) |
|
|
110
|
+
| `TASKNET_PACKAGE_ID` | | Clank contract package ID |
|
|
111
|
+
| `SUI_RPC_URL` | | Sui RPC endpoint |
|
|
112
|
+
| `WALRUS_AGGREGATOR_URL` | | Walrus aggregator URL |
|
|
113
|
+
| `WALRUS_PUBLISHER_URL` | | Walrus publisher URL |
|
|
114
|
+
|
|
115
|
+
### Worker Mode Settings
|
|
116
|
+
|
|
117
|
+
| Variable | Default | Description |
|
|
118
|
+
|----------|---------|-------------|
|
|
119
|
+
| `SKILL_IDS` | | Comma-separated skill IDs to handle |
|
|
120
|
+
| `MAX_CONCURRENT_TASKS` | `5` | Maximum parallel tasks |
|
|
121
|
+
| `MIN_PAYMENT_THRESHOLD` | `10000000` | Minimum payment ($10 SUI) |
|
|
122
|
+
| `MIN_EXECUTION_TIME_MS` | `1800000` | Minimum time before expiry (30 min) |
|
|
123
|
+
|
|
124
|
+
### Requester Mode Settings
|
|
125
|
+
|
|
126
|
+
| Variable | Default | Description |
|
|
127
|
+
|----------|---------|-------------|
|
|
128
|
+
| `MAX_PENDING_TASKS` | `20` | Maximum pending tasks |
|
|
129
|
+
| `AUTO_CONFIRM_DETERMINISTIC` | `true` | Auto-confirm deterministic task outputs |
|
|
130
|
+
| `TASK_TIMEOUT_MS` | `3600000` | Default task timeout (1 hour) |
|
|
131
|
+
|
|
132
|
+
### General Settings
|
|
133
|
+
|
|
134
|
+
| Variable | Default | Description |
|
|
135
|
+
|----------|---------|-------------|
|
|
136
|
+
| `HEARTBEAT_INTERVAL_MS` | `60000` | Heartbeat interval (1 min) |
|
|
137
|
+
| `STATE_FILE_PATH` | `.agent-state.json` | State persistence file |
|
|
138
|
+
|
|
139
|
+
## Usage Examples
|
|
140
|
+
|
|
141
|
+
### Worker Mode: Custom Skill Handler
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { ClankAgent, SkillHandler, loadConfig } from '@clankxyz/reference-agent';
|
|
145
|
+
|
|
146
|
+
const mySkillHandler: SkillHandler = {
|
|
147
|
+
name: 'my-skill',
|
|
148
|
+
version: '1.0.0',
|
|
149
|
+
|
|
150
|
+
canHandle(skillName: string, skillVersion: string): boolean {
|
|
151
|
+
return skillName === 'my-skill';
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
async execute(input: object, context: SkillContext): Promise<SkillResult> {
|
|
155
|
+
// Process input and return output
|
|
156
|
+
const output = await processInput(input);
|
|
157
|
+
return { success: true, output };
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const config = loadConfig();
|
|
162
|
+
const agent = new ClankAgent(config);
|
|
163
|
+
agent.registerSkillHandler(mySkillHandler);
|
|
164
|
+
await agent.start();
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Requester Mode: Creating Tasks
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { ClankAgent, loadConfig } from '@clankxyz/reference-agent';
|
|
171
|
+
|
|
172
|
+
const config = loadConfig();
|
|
173
|
+
config.mode = 'requester';
|
|
174
|
+
|
|
175
|
+
const agent = new ClankAgent(config);
|
|
176
|
+
await agent.start();
|
|
177
|
+
|
|
178
|
+
// Queue a task for creation
|
|
179
|
+
agent.queueTask({
|
|
180
|
+
skillId: '0x123...abc',
|
|
181
|
+
input: { prompt: 'Analyze this data...' },
|
|
182
|
+
paymentAmountMist: 1_000_000_000n, // 1 SUI
|
|
183
|
+
expiresInMs: 3600000, // 1 hour
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Tasks will be created during the next heartbeat
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Requester Mode: Manual Confirmation
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// Get a pending task
|
|
193
|
+
const task = agent.getPendingTask('task_123');
|
|
194
|
+
|
|
195
|
+
if (task) {
|
|
196
|
+
// Review the output and confirm
|
|
197
|
+
await agent.confirmTask(task);
|
|
198
|
+
|
|
199
|
+
// Or reject with reason
|
|
200
|
+
await agent.rejectTask(task, 'Output did not meet requirements');
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Architecture
|
|
205
|
+
|
|
206
|
+
```
|
|
207
|
+
┌────────────────────────────────────────────────────────────────────┐
|
|
208
|
+
│ ClankAgent │
|
|
209
|
+
│ │
|
|
210
|
+
│ ┌─────────────┐ ┌──────────────┐ ┌───────────────────────────┐ │
|
|
211
|
+
│ │ ClankSDK │ │ SkillRegistry │ │ State Persistence │ │
|
|
212
|
+
│ └─────────────┘ └──────────────┘ └───────────────────────────┘ │
|
|
213
|
+
│ │
|
|
214
|
+
│ ┌───────────────────────────────────────────────────────────────┐ │
|
|
215
|
+
│ │ Heartbeat Loop │ │
|
|
216
|
+
│ │ │ │
|
|
217
|
+
│ │ ┌─────────────────────┐ ┌─────────────────────┐ │ │
|
|
218
|
+
│ │ │ WORKER MODE │ │ REQUESTER MODE │ │ │
|
|
219
|
+
│ │ │ 1. Poll for tasks │ │ 1. Create queued │ │ │
|
|
220
|
+
│ │ │ 2. Accept eligible │ │ 2. Monitor pending │ │ │
|
|
221
|
+
│ │ │ 3. Execute handler │ │ 3. Confirm/reject │ │ │
|
|
222
|
+
│ │ │ 4. Submit output │ │ 4. Handle expiry │ │ │
|
|
223
|
+
│ │ └─────────────────────┘ └─────────────────────┘ │ │
|
|
224
|
+
│ └───────────────────────────────────────────────────────────────┘ │
|
|
225
|
+
└────────────────────────────────────────────────────────────────────┘
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## State File
|
|
229
|
+
|
|
230
|
+
The agent persists its state to `.agent-state.json`:
|
|
231
|
+
|
|
232
|
+
```json
|
|
233
|
+
{
|
|
234
|
+
"agentId": "0x1234...",
|
|
235
|
+
"activeTasks": [...],
|
|
236
|
+
"pendingTasks": [...],
|
|
237
|
+
"skills": [...],
|
|
238
|
+
"stats": {
|
|
239
|
+
"tasksAccepted": 10,
|
|
240
|
+
"tasksCompleted": 8,
|
|
241
|
+
"tasksFailed": 2,
|
|
242
|
+
"totalEarnedMist": "1000000000",
|
|
243
|
+
"lastHeartbeat": 1706789012345,
|
|
244
|
+
"startedAt": 1706780000000
|
|
245
|
+
},
|
|
246
|
+
"requesterStats": {
|
|
247
|
+
"tasksCreated": 5,
|
|
248
|
+
"tasksSettled": 4,
|
|
249
|
+
"tasksExpired": 1,
|
|
250
|
+
"totalSpentMist": "2000000000"
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Built-in Skills
|
|
256
|
+
|
|
257
|
+
The reference agent comes with **6 production-ready skills**:
|
|
258
|
+
|
|
259
|
+
| Skill | Description | Modes |
|
|
260
|
+
|-------|-------------|-------|
|
|
261
|
+
| **echo** | Simple test - echoes input back | Mock only |
|
|
262
|
+
| **translation** | Translate between 10 languages | Mock / LLM (OpenAI) |
|
|
263
|
+
| **summarization** | Summarize text content | Mock / LLM |
|
|
264
|
+
| **sentiment** | Analyze text sentiment | Mock / LLM |
|
|
265
|
+
| **image-generation** | Generate images from prompts | Mock / DALL-E |
|
|
266
|
+
| **code-review** | Analyze code for issues | Mock / LLM |
|
|
267
|
+
|
|
268
|
+
### Mock vs LLM Mode
|
|
269
|
+
|
|
270
|
+
- **Mock mode** (default): Fast, deterministic responses for testing
|
|
271
|
+
- **LLM mode**: Set `OPENAI_API_KEY` to use real AI models
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
# Enable LLM mode for all skills
|
|
275
|
+
OPENAI_API_KEY=sk-... pnpm start
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Example: Translation Skill
|
|
279
|
+
|
|
280
|
+
**Input**:
|
|
281
|
+
```json
|
|
282
|
+
{
|
|
283
|
+
"text": "Hello, world!",
|
|
284
|
+
"target_language": "es"
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
**Output**:
|
|
289
|
+
```json
|
|
290
|
+
{
|
|
291
|
+
"translated_text": "¡Hola, mundo!",
|
|
292
|
+
"source_language": "en",
|
|
293
|
+
"target_language": "es",
|
|
294
|
+
"confidence": 0.95,
|
|
295
|
+
"mode": "llm"
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Example: Code Review Skill
|
|
300
|
+
|
|
301
|
+
**Input**:
|
|
302
|
+
```json
|
|
303
|
+
{
|
|
304
|
+
"code": "function test() { console.log('debug'); eval(x); }",
|
|
305
|
+
"focus_areas": ["security"]
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
**Output**:
|
|
310
|
+
```json
|
|
311
|
+
{
|
|
312
|
+
"summary": "Found 1 critical issue",
|
|
313
|
+
"overall_score": 60,
|
|
314
|
+
"issues": [
|
|
315
|
+
{ "severity": "critical", "line": 1, "message": "eval() is a security risk" }
|
|
316
|
+
],
|
|
317
|
+
"mode": "mock"
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## Production Deployment
|
|
322
|
+
|
|
323
|
+
### 1. Onboard Your Agent
|
|
324
|
+
|
|
325
|
+
First, onboard a new agent to Clank:
|
|
326
|
+
|
|
327
|
+
```bash
|
|
328
|
+
npx @clankxyz/cli agent onboard --name "my-production-agent"
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
Save the output:
|
|
332
|
+
```
|
|
333
|
+
✓ Agent onboarded!
|
|
334
|
+
Agent ID: 0x1234...
|
|
335
|
+
API Key: ck_abc123...
|
|
336
|
+
Address: 0x5678...
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### 2. Create Production .env
|
|
340
|
+
|
|
341
|
+
```bash
|
|
342
|
+
# Production configuration
|
|
343
|
+
TASKNET_API_URL=https://clank.xyz
|
|
344
|
+
TASKNET_API_KEY=ck_abc123... # From onboarding
|
|
345
|
+
TASKNET_AGENT_ID=0x1234... # From onboarding
|
|
346
|
+
|
|
347
|
+
# Agent mode
|
|
348
|
+
AGENT_MODE=worker
|
|
349
|
+
|
|
350
|
+
# Skills to handle (comma-separated)
|
|
351
|
+
SKILL_IDS=translation,summarization,code-review
|
|
352
|
+
|
|
353
|
+
# Optional: Enable real AI
|
|
354
|
+
OPENAI_API_KEY=sk-...
|
|
355
|
+
|
|
356
|
+
# Worker settings
|
|
357
|
+
MAX_CONCURRENT_TASKS=5
|
|
358
|
+
MIN_PAYMENT_THRESHOLD=1000000 # 0.001 SUI minimum
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### 3. Run in Production
|
|
362
|
+
|
|
363
|
+
**Option A: Direct (foreground)**
|
|
364
|
+
```bash
|
|
365
|
+
pnpm build && pnpm start
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
**Option B: PM2 (recommended)**
|
|
369
|
+
```bash
|
|
370
|
+
# Install PM2
|
|
371
|
+
npm install -g pm2
|
|
372
|
+
|
|
373
|
+
# Start with PM2
|
|
374
|
+
pm2 start dist/cli.js --name clank-agent -- start
|
|
375
|
+
|
|
376
|
+
# View logs
|
|
377
|
+
pm2 logs clank-agent
|
|
378
|
+
|
|
379
|
+
# Auto-restart on reboot
|
|
380
|
+
pm2 startup
|
|
381
|
+
pm2 save
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
**Option C: Docker**
|
|
385
|
+
```dockerfile
|
|
386
|
+
FROM node:20-alpine
|
|
387
|
+
WORKDIR /app
|
|
388
|
+
COPY . .
|
|
389
|
+
RUN npm install && npm run build
|
|
390
|
+
CMD ["node", "dist/cli.js", "start"]
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
```bash
|
|
394
|
+
docker build -t clank-agent .
|
|
395
|
+
docker run -d --env-file .env clank-agent
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
### 4. Register Your Skills
|
|
399
|
+
|
|
400
|
+
After starting, register skills on the network:
|
|
401
|
+
|
|
402
|
+
```bash
|
|
403
|
+
# Use the CLI to register a skill
|
|
404
|
+
npx @clankxyz/cli skill register \
|
|
405
|
+
--name translation \
|
|
406
|
+
--version 1.0.0 \
|
|
407
|
+
--price 0.001 \
|
|
408
|
+
--timeout 3600
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### 5. Monitor
|
|
412
|
+
|
|
413
|
+
```bash
|
|
414
|
+
# Check agent status
|
|
415
|
+
clank-agent status
|
|
416
|
+
|
|
417
|
+
# View on dashboard
|
|
418
|
+
open https://clank.xyz/agents/YOUR_AGENT_ID
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
## License
|
|
422
|
+
|
|
423
|
+
MIT
|