@teneo-protocol/sdk 1.0.0 → 2.0.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/.github/workflows/publish-npm.yml +8 -6
- package/CHANGELOG.md +265 -0
- package/README.md +406 -53
- package/dist/core/websocket-client.d.ts +13 -0
- package/dist/core/websocket-client.d.ts.map +1 -1
- package/dist/core/websocket-client.js +34 -3
- package/dist/core/websocket-client.js.map +1 -1
- package/dist/handlers/message-handlers/agent-room-operation-response-handler.d.ts +76 -0
- package/dist/handlers/message-handlers/agent-room-operation-response-handler.d.ts.map +1 -0
- package/dist/handlers/message-handlers/agent-room-operation-response-handler.js +70 -0
- package/dist/handlers/message-handlers/agent-room-operation-response-handler.js.map +1 -0
- package/dist/handlers/message-handlers/agent-selected-handler.d.ts +92 -38
- package/dist/handlers/message-handlers/agent-selected-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/agent-status-update-handler.d.ts +904 -0
- package/dist/handlers/message-handlers/agent-status-update-handler.d.ts.map +1 -0
- package/dist/handlers/message-handlers/agent-status-update-handler.js +51 -0
- package/dist/handlers/message-handlers/agent-status-update-handler.js.map +1 -0
- package/dist/handlers/message-handlers/auth-error-handler.d.ts +45 -31
- package/dist/handlers/message-handlers/auth-error-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/auth-message-handler.d.ts +6 -0
- package/dist/handlers/message-handlers/auth-message-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/auth-message-handler.js +65 -5
- package/dist/handlers/message-handlers/auth-message-handler.js.map +1 -1
- package/dist/handlers/message-handlers/auth-required-handler.d.ts +49 -31
- package/dist/handlers/message-handlers/auth-required-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/auth-success-handler.d.ts +6 -0
- package/dist/handlers/message-handlers/auth-success-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/auth-success-handler.js +46 -4
- package/dist/handlers/message-handlers/auth-success-handler.js.map +1 -1
- package/dist/handlers/message-handlers/challenge-handler.d.ts +45 -31
- package/dist/handlers/message-handlers/challenge-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/error-message-handler.d.ts +49 -31
- package/dist/handlers/message-handlers/error-message-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/index.d.ts +5 -0
- package/dist/handlers/message-handlers/index.d.ts.map +1 -1
- package/dist/handlers/message-handlers/index.js +23 -1
- package/dist/handlers/message-handlers/index.js.map +1 -1
- package/dist/handlers/message-handlers/list-available-agents-handler.d.ts +877 -0
- package/dist/handlers/message-handlers/list-available-agents-handler.d.ts.map +1 -0
- package/dist/handlers/message-handlers/list-available-agents-handler.js +38 -0
- package/dist/handlers/message-handlers/list-available-agents-handler.js.map +1 -0
- package/dist/handlers/message-handlers/list-room-agents-handler.d.ts +886 -0
- package/dist/handlers/message-handlers/list-room-agents-handler.d.ts.map +1 -0
- package/dist/handlers/message-handlers/list-room-agents-handler.js +51 -0
- package/dist/handlers/message-handlers/list-room-agents-handler.js.map +1 -0
- package/dist/handlers/message-handlers/list-rooms-response-handler.d.ts +178 -89
- package/dist/handlers/message-handlers/list-rooms-response-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/ping-pong-handler.d.ts +62 -58
- package/dist/handlers/message-handlers/ping-pong-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/regular-message-handler.d.ts +31 -29
- package/dist/handlers/message-handlers/regular-message-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/regular-message-handler.js +1 -0
- package/dist/handlers/message-handlers/regular-message-handler.js.map +1 -1
- package/dist/handlers/message-handlers/room-operation-response-handler.d.ts +328 -0
- package/dist/handlers/message-handlers/room-operation-response-handler.d.ts.map +1 -0
- package/dist/handlers/message-handlers/room-operation-response-handler.js +92 -0
- package/dist/handlers/message-handlers/room-operation-response-handler.js.map +1 -0
- package/dist/handlers/message-handlers/subscribe-response-handler.d.ts +53 -31
- package/dist/handlers/message-handlers/subscribe-response-handler.d.ts.map +1 -1
- package/dist/handlers/message-handlers/types.d.ts +2 -0
- package/dist/handlers/message-handlers/types.d.ts.map +1 -1
- package/dist/handlers/message-handlers/unsubscribe-response-handler.d.ts +53 -31
- package/dist/handlers/message-handlers/unsubscribe-response-handler.d.ts.map +1 -1
- package/dist/managers/agent-room-manager.d.ts +222 -0
- package/dist/managers/agent-room-manager.d.ts.map +1 -0
- package/dist/managers/agent-room-manager.js +508 -0
- package/dist/managers/agent-room-manager.js.map +1 -0
- package/dist/managers/index.d.ts +2 -0
- package/dist/managers/index.d.ts.map +1 -1
- package/dist/managers/index.js +5 -1
- package/dist/managers/index.js.map +1 -1
- package/dist/managers/message-router.d.ts +1 -1
- package/dist/managers/message-router.d.ts.map +1 -1
- package/dist/managers/message-router.js +41 -4
- package/dist/managers/message-router.js.map +1 -1
- package/dist/managers/room-management-manager.d.ts +213 -0
- package/dist/managers/room-management-manager.d.ts.map +1 -0
- package/dist/managers/room-management-manager.js +440 -0
- package/dist/managers/room-management-manager.js.map +1 -0
- package/dist/managers/room-manager.d.ts +4 -4
- package/dist/managers/room-manager.d.ts.map +1 -1
- package/dist/managers/room-manager.js +1 -1
- package/dist/managers/room-manager.js.map +1 -1
- package/dist/teneo-sdk.d.ts +362 -14
- package/dist/teneo-sdk.d.ts.map +1 -1
- package/dist/teneo-sdk.js +497 -7
- package/dist/teneo-sdk.js.map +1 -1
- package/dist/types/config.d.ts +63 -54
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/config.js +9 -5
- package/dist/types/config.js.map +1 -1
- package/dist/types/error-codes.d.ts +2 -0
- package/dist/types/error-codes.d.ts.map +1 -1
- package/dist/types/error-codes.js +3 -0
- package/dist/types/error-codes.js.map +1 -1
- package/dist/types/events.d.ts +132 -68
- package/dist/types/events.d.ts.map +1 -1
- package/dist/types/events.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +27 -2
- package/dist/types/index.js.map +1 -1
- package/dist/types/messages.d.ts +11396 -2559
- package/dist/types/messages.d.ts.map +1 -1
- package/dist/types/messages.js +294 -27
- package/dist/types/messages.js.map +1 -1
- package/dist/types/validation.d.ts.map +1 -1
- package/dist/types/validation.js +1 -1
- package/dist/types/validation.js.map +1 -1
- package/dist/utils/bounded-queue.d.ts +1 -1
- package/dist/utils/bounded-queue.js +6 -6
- package/dist/utils/circuit-breaker.d.ts.map +1 -1
- package/dist/utils/circuit-breaker.js.map +1 -1
- package/dist/utils/event-waiter.d.ts.map +1 -1
- package/dist/utils/event-waiter.js +2 -1
- package/dist/utils/event-waiter.js.map +1 -1
- package/dist/utils/rate-limiter.d.ts.map +1 -1
- package/dist/utils/rate-limiter.js +4 -6
- package/dist/utils/rate-limiter.js.map +1 -1
- package/dist/utils/secure-private-key.d.ts.map +1 -1
- package/dist/utils/secure-private-key.js +9 -15
- package/dist/utils/secure-private-key.js.map +1 -1
- package/dist/utils/signature-verifier.d.ts +2 -2
- package/dist/utils/signature-verifier.d.ts.map +1 -1
- package/dist/utils/signature-verifier.js +5 -5
- package/dist/utils/signature-verifier.js.map +1 -1
- package/examples/.env.example +1 -1
- package/examples/agent-room-management-example.ts +334 -0
- package/examples/claude-agent-x-follower/.env.example +117 -0
- package/examples/claude-agent-x-follower/QUICKSTART.md +243 -0
- package/examples/claude-agent-x-follower/README.md +540 -0
- package/examples/claude-agent-x-follower/index.ts +248 -0
- package/examples/claude-agent-x-follower/package.json +37 -0
- package/examples/claude-agent-x-follower/tsconfig.json +20 -0
- package/examples/n8n-teneo/.env.example +127 -0
- package/examples/n8n-teneo/Dockerfile +42 -0
- package/examples/n8n-teneo/README.md +564 -0
- package/examples/n8n-teneo/docker-compose.yml +71 -0
- package/examples/n8n-teneo/index.ts +177 -0
- package/examples/n8n-teneo/package.json +22 -0
- package/examples/n8n-teneo/tsconfig.json +12 -0
- package/examples/n8n-teneo/workflows/x-timeline.json +66 -0
- package/examples/openai-teneo/.env.example +130 -0
- package/examples/openai-teneo/README.md +635 -0
- package/examples/openai-teneo/index.ts +280 -0
- package/examples/openai-teneo/package.json +24 -0
- package/examples/openai-teneo/tsconfig.json +16 -0
- package/examples/production-dashboard/.env.example +5 -3
- package/examples/production-dashboard/README.md +839 -0
- package/examples/production-dashboard/pnpm-lock.yaml +92 -0
- package/examples/production-dashboard/public/dashboard.html +1150 -504
- package/examples/production-dashboard/server.ts +428 -12
- package/examples/room-management-example.ts +285 -0
- package/examples/usage/.env.example +17 -0
- package/examples/usage/01-connect.ts +116 -0
- package/examples/usage/02-list-agents.ts +153 -0
- package/examples/usage/03-pick-agent.ts +201 -0
- package/examples/usage/04-find-by-capability.ts +237 -0
- package/examples/usage/05-webhook-example.ts +319 -0
- package/examples/usage/06-simple-api-server.ts +396 -0
- package/examples/usage/07-event-listener.ts +402 -0
- package/examples/usage/README.md +383 -0
- package/examples/usage/package.json +42 -0
- package/package.json +13 -3
- package/src/core/websocket-client.ts +43 -9
- package/src/formatters/response-formatter.test.ts +8 -2
- package/src/handlers/message-handlers/agent-room-operation-response-handler.ts +83 -0
- package/src/handlers/message-handlers/agent-status-update-handler.ts +58 -0
- package/src/handlers/message-handlers/auth-message-handler.ts +73 -5
- package/src/handlers/message-handlers/auth-success-handler.ts +58 -6
- package/src/handlers/message-handlers/index.ts +19 -0
- package/src/handlers/message-handlers/list-available-agents-handler.ts +41 -0
- package/src/handlers/message-handlers/list-room-agents-handler.ts +61 -0
- package/src/handlers/message-handlers/regular-message-handler.ts +1 -0
- package/src/handlers/message-handlers/room-operation-response-handler.ts +105 -0
- package/src/handlers/message-handlers/types.ts +6 -0
- package/src/handlers/webhook-handler.test.ts +13 -10
- package/src/managers/agent-room-manager.ts +609 -0
- package/src/managers/index.ts +2 -0
- package/src/managers/message-router.ts +48 -6
- package/src/managers/room-management-manager.ts +523 -0
- package/src/managers/room-manager.ts +12 -6
- package/src/teneo-sdk.ts +543 -10
- package/src/types/config.ts +13 -6
- package/src/types/error-codes.ts +4 -0
- package/src/types/events.ts +24 -0
- package/src/types/index.ts +55 -0
- package/src/types/messages.ts +374 -41
- package/src/types/validation.ts +4 -1
- package/src/utils/bounded-queue.ts +9 -9
- package/src/utils/circuit-breaker.ts +4 -1
- package/src/utils/deduplication-cache.test.ts +2 -6
- package/src/utils/event-waiter.test.ts +4 -1
- package/src/utils/event-waiter.ts +5 -7
- package/src/utils/rate-limiter.test.ts +5 -17
- package/src/utils/rate-limiter.ts +6 -9
- package/src/utils/secure-private-key.test.ts +66 -59
- package/src/utils/secure-private-key.ts +10 -16
- package/src/utils/signature-verifier.test.ts +75 -70
- package/src/utils/signature-verifier.ts +7 -8
- package/src/utils/ssrf-validator.test.ts +3 -3
- package/tests/integration/room-management.test.ts +514 -0
- package/tests/integration/websocket.test.ts +1 -1
- package/tests/unit/handlers/agent-room-operation-response-handler.test.ts +394 -0
- package/tests/unit/handlers/agent-status-update-handler.test.ts +407 -0
- package/tests/unit/handlers/auth-success-handler-rooms.test.ts +699 -0
- package/tests/unit/handlers/list-available-agents-handler.test.ts +256 -0
- package/tests/unit/handlers/list-room-agents-handler.test.ts +294 -0
- package/tests/unit/handlers/room-operation-response-handler.test.ts +527 -0
- package/tests/unit/managers/agent-room-manager.test.ts +534 -0
- package/tests/unit/managers/room-management-manager.test.ts +438 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
# Claude Agent + Teneo SDK - X Timeline Follower
|
|
2
|
+
|
|
3
|
+
A minimal example demonstrating integration between **Claude Agent SDK** and **Teneo Consumer SDK** to fetch and analyze X/Twitter timelines.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This example shows how to:
|
|
8
|
+
1. ✅ Create a Claude Agent with a custom tool
|
|
9
|
+
2. ✅ Use the tool to call Teneo SDK and query X-Agent
|
|
10
|
+
3. ✅ Fetch X/Twitter timeline data
|
|
11
|
+
4. ✅ Let Claude analyze and summarize the results
|
|
12
|
+
|
|
13
|
+
## Architecture
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
┌─────────────────┐
|
|
17
|
+
│ Claude Agent │
|
|
18
|
+
│ (AI Analysis) │
|
|
19
|
+
└────────┬────────┘
|
|
20
|
+
│ uses tool
|
|
21
|
+
▼
|
|
22
|
+
┌─────────────────┐
|
|
23
|
+
│ get_x_timeline │
|
|
24
|
+
│ (Tool) │
|
|
25
|
+
└────────┬────────┘
|
|
26
|
+
│ calls
|
|
27
|
+
▼
|
|
28
|
+
┌─────────────────┐
|
|
29
|
+
│ Teneo SDK │
|
|
30
|
+
│ (X-Agent Comm) │
|
|
31
|
+
└────────┬────────┘
|
|
32
|
+
│ queries
|
|
33
|
+
▼
|
|
34
|
+
┌─────────────────┐
|
|
35
|
+
│ X-Agent │
|
|
36
|
+
│ (Twitter Data) │
|
|
37
|
+
└─────────────────┘
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Prerequisites
|
|
41
|
+
|
|
42
|
+
1. **Node.js** 18+ installed
|
|
43
|
+
2. **Claude Code CLI** authenticated (OR Anthropic API Key)
|
|
44
|
+
3. **Teneo Credentials** (WebSocket URL, Private Key)
|
|
45
|
+
4. **Built Teneo SDK** (run `npm run build` in project root)
|
|
46
|
+
5. **Active X-Agent** running in the Teneo room
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# From project root
|
|
52
|
+
npm install
|
|
53
|
+
|
|
54
|
+
# Build the SDK
|
|
55
|
+
npm run build
|
|
56
|
+
|
|
57
|
+
# Install Claude Agent SDK
|
|
58
|
+
npm install @anthropic-ai/claude-agent-sdk
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Configuration
|
|
62
|
+
|
|
63
|
+
Create `.env` file from the template:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
cp examples/claude-agent-x-follower/.env.example examples/claude-agent-x-follower/.env
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Required Configuration
|
|
70
|
+
|
|
71
|
+
```env
|
|
72
|
+
# Teneo Network Connection
|
|
73
|
+
WS_URL=wss://your-teneo-server.com/ws
|
|
74
|
+
PRIVATE_KEY=0x1234567890123456789012345678901234567890123456789012345678901234
|
|
75
|
+
WALLET_ADDRESS=0x1234567890123456789012345678901234567890 # Optional - auto-derived from private key
|
|
76
|
+
|
|
77
|
+
# Room Configuration
|
|
78
|
+
DEFAULT_ROOM=general # or x-agent-enterprise-v2 for X-Agent features
|
|
79
|
+
|
|
80
|
+
# Claude Configuration (Optional - only needed if Claude Code CLI is not authenticated)
|
|
81
|
+
ANTHROPIC_API_KEY=sk-ant-your-api-key-here
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Optional Configuration
|
|
85
|
+
|
|
86
|
+
```env
|
|
87
|
+
# Security Features
|
|
88
|
+
ENABLE_SIGNATURE_VERIFICATION=false # Enable SEC-2 message signature verification
|
|
89
|
+
TRUSTED_ADDRESSES=0xAddress1,0xAddress2 # Comma-separated list of trusted agent addresses
|
|
90
|
+
|
|
91
|
+
# Performance Tuning
|
|
92
|
+
ENABLE_CACHE=true # Enable agent caching
|
|
93
|
+
CACHE_TIMEOUT=300000 # Cache timeout (5 minutes)
|
|
94
|
+
MAX_CACHE_SIZE=100 # Maximum cache entries
|
|
95
|
+
|
|
96
|
+
# Connection Settings
|
|
97
|
+
ENABLE_RECONNECTION=true # Enable automatic reconnection
|
|
98
|
+
RECONNECT_DELAY=5000 # Reconnection delay (5 seconds)
|
|
99
|
+
MAX_RECONNECT_ATTEMPTS=10 # Maximum reconnection attempts
|
|
100
|
+
|
|
101
|
+
# Logging
|
|
102
|
+
LOG_LEVEL=info # debug | info | warn | error | silent
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Authentication Options:**
|
|
106
|
+
- **Option 1 (Recommended)**: Use Claude Code CLI authentication (if you're using Claude Code)
|
|
107
|
+
- No API key needed - the example uses your local `~/.claude/.credentials.json`
|
|
108
|
+
- Already configured via `pathToClaudeCodeExecutable` in the code
|
|
109
|
+
|
|
110
|
+
- **Option 2**: Use Anthropic API Key
|
|
111
|
+
- Get an API key at https://console.anthropic.com/
|
|
112
|
+
- Add it to `.env` as `ANTHROPIC_API_KEY`
|
|
113
|
+
- Remove the `pathToClaudeCodeExecutable` line from the code
|
|
114
|
+
|
|
115
|
+
**Wallet Address Note:**
|
|
116
|
+
- If `WALLET_ADDRESS` is not provided, it will be automatically derived from your `PRIVATE_KEY` using viem's `privateKeyToAccount`
|
|
117
|
+
|
|
118
|
+
## Usage
|
|
119
|
+
|
|
120
|
+
### Basic Usage
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
# Run with default prompt (fetches @elonmusk timeline)
|
|
124
|
+
npx tsx examples/claude-agent-x-follower/index.ts
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### Custom Prompts
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
# Analyze specific user
|
|
131
|
+
npx tsx examples/claude-agent-x-follower/index.ts "Get timeline for VitalikButerin and analyze sentiment"
|
|
132
|
+
|
|
133
|
+
# Compare multiple users
|
|
134
|
+
npx tsx examples/claude-agent-x-follower/index.ts "Compare the latest tweets from elonmusk and VitalikButerin"
|
|
135
|
+
|
|
136
|
+
# Summarize engagement
|
|
137
|
+
npx tsx examples/claude-agent-x-follower/index.ts "Get timeline for OpenAI and summarize the most engaging tweets"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## Example Output
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
🤖 Claude Agent + Teneo SDK - X Timeline Follower
|
|
144
|
+
|
|
145
|
+
📡 Connecting to Teneo network...
|
|
146
|
+
✅ Connected to Teneo
|
|
147
|
+
|
|
148
|
+
💬 Prompt: "Get timeline for elonmusk and summarize the latest 5 tweets"
|
|
149
|
+
|
|
150
|
+
🧠 Claude is processing...
|
|
151
|
+
|
|
152
|
+
🔧 Tool called: get_x_timeline
|
|
153
|
+
📥 Input: { username: 'elonmusk', count: 20 }
|
|
154
|
+
|
|
155
|
+
🔍 Fetching timeline for @elonmusk (20 tweets)...
|
|
156
|
+
|
|
157
|
+
✅ Tool result: Success
|
|
158
|
+
|
|
159
|
+
============================================================
|
|
160
|
+
📝 CLAUDE RESPONSE:
|
|
161
|
+
============================================================
|
|
162
|
+
I've retrieved the timeline for @elonmusk. Here's a summary of the latest 5 tweets:
|
|
163
|
+
|
|
164
|
+
1. [Tweet about X platform updates] - High engagement with 2.3M views
|
|
165
|
+
2. [SpaceX mission announcement] - 1.8M views, discussing Mars mission
|
|
166
|
+
3. [AI development commentary] - 1.5M views, discussing AGI progress
|
|
167
|
+
4. [Tesla production update] - 1.2M views, manufacturing milestone
|
|
168
|
+
5. [Meme tweet] - 980K views, general humor content
|
|
169
|
+
|
|
170
|
+
The timeline shows a mix of business updates, technical discussions, and
|
|
171
|
+
casual engagement with the community. Average engagement is very high.
|
|
172
|
+
============================================================
|
|
173
|
+
|
|
174
|
+
✅ Done!
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Code Structure
|
|
178
|
+
|
|
179
|
+
### Main Components
|
|
180
|
+
|
|
181
|
+
1. **Teneo SDK Initialization (SDKConfigBuilder Pattern)**
|
|
182
|
+
```typescript
|
|
183
|
+
import { SDKConfigBuilder, TeneoSDK } from '../../dist/index.js';
|
|
184
|
+
|
|
185
|
+
// Load environment variables
|
|
186
|
+
const WS_URL = process.env.WS_URL || '';
|
|
187
|
+
const PRIVATE_KEY = process.env.PRIVATE_KEY || '';
|
|
188
|
+
const WALLET_ADDRESS = process.env.WALLET_ADDRESS || '';
|
|
189
|
+
const DEFAULT_ROOM = process.env.DEFAULT_ROOM || 'general';
|
|
190
|
+
const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
|
|
191
|
+
const ENABLE_SIG_VERIFICATION = process.env.ENABLE_SIGNATURE_VERIFICATION === 'true';
|
|
192
|
+
const TRUSTED_ADDRESSES = process.env.TRUSTED_ADDRESSES?.split(',').filter(Boolean) || [];
|
|
193
|
+
|
|
194
|
+
// Initialize SDK using builder pattern (recommended)
|
|
195
|
+
const config = new SDKConfigBuilder()
|
|
196
|
+
.withWebSocketUrl(WS_URL)
|
|
197
|
+
.withAuthentication(PRIVATE_KEY, WALLET_ADDRESS) // Wallet auto-derived if not provided
|
|
198
|
+
.withRoom(DEFAULT_ROOM)
|
|
199
|
+
.withReconnection({
|
|
200
|
+
enabled: true,
|
|
201
|
+
delay: 5000,
|
|
202
|
+
maxAttempts: 10
|
|
203
|
+
})
|
|
204
|
+
.withResponseFormat({ format: 'both', includeMetadata: true })
|
|
205
|
+
.withLogging(LOG_LEVEL)
|
|
206
|
+
.withCache(true, 300000, 100)
|
|
207
|
+
.withSignatureVerification({
|
|
208
|
+
enabled: ENABLE_SIG_VERIFICATION,
|
|
209
|
+
trustedAddresses: TRUSTED_ADDRESSES,
|
|
210
|
+
requireFor: ['task_response', 'agent_selected'],
|
|
211
|
+
strictMode: false
|
|
212
|
+
})
|
|
213
|
+
.build();
|
|
214
|
+
|
|
215
|
+
const teneoSDK = new TeneoSDK(config);
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
2. **Tool Definition with Zod Schema**
|
|
219
|
+
```typescript
|
|
220
|
+
// Pass Zod shape directly (ZodRawShape), not z.object()
|
|
221
|
+
const getXTimelineTool = tool(
|
|
222
|
+
'get_x_timeline',
|
|
223
|
+
'Fetch X/Twitter timeline using Teneo X-Agent',
|
|
224
|
+
{
|
|
225
|
+
username: z.string().describe('X/Twitter username (without @)'),
|
|
226
|
+
count: z.number().min(1).max(100).default(20)
|
|
227
|
+
},
|
|
228
|
+
async (args) => {
|
|
229
|
+
const { username, count } = args;
|
|
230
|
+
|
|
231
|
+
// Find X-Agent
|
|
232
|
+
const agents = teneoSDK.getAgents();
|
|
233
|
+
const xAgent = agents.find(a =>
|
|
234
|
+
a.name?.toLowerCase().includes('x') ||
|
|
235
|
+
a.id?.toLowerCase().includes('x')
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
if (!xAgent) {
|
|
239
|
+
throw new Error('X-Agent not found. Is it running in the room?');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Send direct command with waitForResponse
|
|
243
|
+
const response = await teneoSDK.sendDirectCommand({
|
|
244
|
+
agent: xAgent.id,
|
|
245
|
+
command: `timeline @${username} ${count}`,
|
|
246
|
+
room: DEFAULT_ROOM
|
|
247
|
+
}, true); // waitForResponse = true
|
|
248
|
+
|
|
249
|
+
return {
|
|
250
|
+
content: [{
|
|
251
|
+
type: 'text',
|
|
252
|
+
text: JSON.stringify({
|
|
253
|
+
success: true,
|
|
254
|
+
timeline: response.humanized
|
|
255
|
+
})
|
|
256
|
+
}]
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
);
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
**Key Features:**
|
|
263
|
+
- Uses `sendDirectCommand` with `waitForResponse: true` for reliable response handling
|
|
264
|
+
- SDK automatically handles message correlation with fallback matching
|
|
265
|
+
- Typical response time: **2-3 seconds**
|
|
266
|
+
- No manual event listener management needed
|
|
267
|
+
|
|
268
|
+
3. **Create MCP Server and Query Claude**
|
|
269
|
+
```typescript
|
|
270
|
+
// Create MCP server with the tool
|
|
271
|
+
const teneoMcpServer = createSdkMcpServer({
|
|
272
|
+
name: 'TeneoXAgent',
|
|
273
|
+
version: '1.0.0',
|
|
274
|
+
tools: [getXTimelineTool]
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// Query Claude - uses local Claude CLI authentication by default
|
|
278
|
+
const result = query({
|
|
279
|
+
prompt: 'Get timeline for elonmusk and summarize',
|
|
280
|
+
options: {
|
|
281
|
+
model: 'claude-3-5-haiku-20241022',
|
|
282
|
+
pathToClaudeCodeExecutable: process.env.HOME + '/.claude/local/claude',
|
|
283
|
+
permissionMode: 'bypassPermissions', // Allow tool use without prompts
|
|
284
|
+
mcpServers: {
|
|
285
|
+
teneo: teneoMcpServer
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// Stream the response
|
|
291
|
+
for await (const message of result) {
|
|
292
|
+
if (message.type === 'result') {
|
|
293
|
+
console.log(message.result);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## Features
|
|
299
|
+
|
|
300
|
+
### Integration Features
|
|
301
|
+
- ✅ **Minimal Code**: ~220 lines of clean TypeScript
|
|
302
|
+
- ✅ **Type-Safe**: Full TypeScript support with Zod validation
|
|
303
|
+
- ✅ **Claude Agent SDK**: Direct integration with Anthropic's Agent SDK
|
|
304
|
+
- ✅ **MCP Server**: Uses Model Context Protocol for tool registration
|
|
305
|
+
- ✅ **Custom Tools**: Easy-to-define tools with Zod schemas
|
|
306
|
+
- ✅ **Fast Response**: Typical 2-3 second end-to-end response time
|
|
307
|
+
- ✅ **Extensible**: Easy to add more tools and capabilities
|
|
308
|
+
|
|
309
|
+
### Modern SDK Features
|
|
310
|
+
- ✅ **Modern SDK Pattern**: Uses SDKConfigBuilder for clean configuration
|
|
311
|
+
- ✅ **Reliable Messaging**: SDK-level fallback matching for message correlation
|
|
312
|
+
- ✅ **Auto-Reconnection**: Automatic reconnection with exponential backoff
|
|
313
|
+
- ✅ **Message Caching**: Agent response caching for better performance
|
|
314
|
+
- ✅ **Signature Verification**: Optional SEC-2 message signature verification
|
|
315
|
+
- ✅ **Wallet Auto-Derivation**: Automatic wallet address derivation from private key
|
|
316
|
+
- ✅ **Error Handling**: Comprehensive error management
|
|
317
|
+
- ✅ **Async/Await**: Modern async patterns with `waitForResponse`
|
|
318
|
+
- ✅ **Configurable**: Environment-based configuration with 15+ options
|
|
319
|
+
|
|
320
|
+
## Extending the Example
|
|
321
|
+
|
|
322
|
+
### Add More Tools
|
|
323
|
+
|
|
324
|
+
```typescript
|
|
325
|
+
const analyzeUserTool = {
|
|
326
|
+
name: 'analyze_x_user',
|
|
327
|
+
description: 'Analyze X user profile and metrics',
|
|
328
|
+
input_schema: { /* ... */ }
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const compareUsersTool = {
|
|
332
|
+
name: 'compare_x_users',
|
|
333
|
+
description: 'Compare two X users',
|
|
334
|
+
input_schema: { /* ... */ }
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const agent = new Agent({
|
|
338
|
+
tools: [getXTimelineTool, analyzeUserTool, compareUsersTool],
|
|
339
|
+
// ...
|
|
340
|
+
});
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Add Event Listeners
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
teneoSDK.on('agent:response', (response) => {
|
|
347
|
+
console.log('Received response from Teneo agent');
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
teneoSDK.on('error', (error) => {
|
|
351
|
+
console.error('Teneo error:', error);
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
## Troubleshooting
|
|
356
|
+
|
|
357
|
+
### Connection Issues
|
|
358
|
+
|
|
359
|
+
```bash
|
|
360
|
+
# Test Teneo connection first
|
|
361
|
+
npx tsx -e "
|
|
362
|
+
import { SDKConfigBuilder, TeneoSDK } from './dist/index.js';
|
|
363
|
+
const config = new SDKConfigBuilder()
|
|
364
|
+
.withWebSocketUrl('wss://...')
|
|
365
|
+
.withAuthentication('0x...')
|
|
366
|
+
.withLogging('info')
|
|
367
|
+
.build();
|
|
368
|
+
const sdk = new TeneoSDK(config);
|
|
369
|
+
await sdk.connect();
|
|
370
|
+
console.log('Connected!');
|
|
371
|
+
"
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Message Format Errors
|
|
375
|
+
|
|
376
|
+
If you see errors like `"Agents should use TypeTaskResponse instead of TypeMessage"` (code: 400):
|
|
377
|
+
|
|
378
|
+
This is a **server-side protocol error** indicating the Teneo server expects a different message format. This typically happens when:
|
|
379
|
+
- The room is configured for agent-to-agent communication rather than consumer SDK communication
|
|
380
|
+
- The server has specific message type requirements for that room
|
|
381
|
+
|
|
382
|
+
**Solutions:**
|
|
383
|
+
1. Try a different room (e.g., `general` instead of `x-agent-enterprise-v2`)
|
|
384
|
+
2. Check with your Teneo administrator about the correct room configuration
|
|
385
|
+
3. Verify your wallet is whitelisted for the room you're trying to access
|
|
386
|
+
|
|
387
|
+
**Note:** This is not a problem with your SDK configuration - the SDK is working correctly!
|
|
388
|
+
|
|
389
|
+
### Claude API Issues
|
|
390
|
+
|
|
391
|
+
- Verify `ANTHROPIC_API_KEY` is set correctly
|
|
392
|
+
- Check API key has sufficient credits
|
|
393
|
+
- Ensure using correct model name
|
|
394
|
+
|
|
395
|
+
### X-Agent Not Responding
|
|
396
|
+
|
|
397
|
+
**Note:** The SDK now includes **intelligent fallback matching** that resolves most timeout issues automatically!
|
|
398
|
+
|
|
399
|
+
If you still see timeout errors like `Message timeout - no response received after 10000ms`, here's how to diagnose:
|
|
400
|
+
|
|
401
|
+
**Step 1: Check if X-Agent is running**
|
|
402
|
+
```javascript
|
|
403
|
+
import { SDKConfigBuilder, TeneoSDK } from './dist/index.js';
|
|
404
|
+
|
|
405
|
+
const config = new SDKConfigBuilder()
|
|
406
|
+
.withWebSocketUrl(process.env.WS_URL)
|
|
407
|
+
.withAuthentication(process.env.PRIVATE_KEY)
|
|
408
|
+
.withRoom('general')
|
|
409
|
+
.build();
|
|
410
|
+
|
|
411
|
+
const sdk = new TeneoSDK(config);
|
|
412
|
+
await sdk.connect();
|
|
413
|
+
|
|
414
|
+
// Check available agents
|
|
415
|
+
const agents = sdk.getAgents();
|
|
416
|
+
console.log('Agents:', agents.length);
|
|
417
|
+
agents.forEach(agent => console.log(` - ${agent.name}`));
|
|
418
|
+
|
|
419
|
+
// If agents.length === 0, no agents are running!
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
**Step 2: Verify Room ID**
|
|
423
|
+
```javascript
|
|
424
|
+
// List all available rooms
|
|
425
|
+
const rooms = sdk.getRooms();
|
|
426
|
+
rooms.forEach(room => {
|
|
427
|
+
console.log(`ID: "${room.id}", Name: ${room.name}`);
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
// Make sure you're using the correct room ID
|
|
431
|
+
// Example: 'x-agent-enterprise-v2' for KOL tracker room
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
**Step 3: Common Issues**
|
|
435
|
+
- ❌ **No agents running**: X-Agent must be deployed and active in the room
|
|
436
|
+
- ❌ **Wrong room**: Default room must be set to the room where X-Agent is running
|
|
437
|
+
- ❌ **Wrong command format**: X-Agent expects `timeline @username count`
|
|
438
|
+
- ❌ **Authentication issues**: Verify your wallet has permission to access the room
|
|
439
|
+
|
|
440
|
+
**Step 4: Test with a simple message**
|
|
441
|
+
```bash
|
|
442
|
+
npx tsx -e "
|
|
443
|
+
import { SDKConfigBuilder, TeneoSDK } from './dist/index.js';
|
|
444
|
+
const config = new SDKConfigBuilder()
|
|
445
|
+
.withWebSocketUrl('wss://...')
|
|
446
|
+
.withAuthentication('0x...')
|
|
447
|
+
.withRoom('general')
|
|
448
|
+
.withLogging('info')
|
|
449
|
+
.build();
|
|
450
|
+
const sdk = new TeneoSDK(config);
|
|
451
|
+
await sdk.connect();
|
|
452
|
+
const response = await sdk.sendMessage('Hello from Teneo SDK', {
|
|
453
|
+
waitForResponse: true,
|
|
454
|
+
timeout: 15000
|
|
455
|
+
});
|
|
456
|
+
console.log('Response:', response);
|
|
457
|
+
sdk.disconnect();
|
|
458
|
+
"
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
## SDK Improvements
|
|
462
|
+
|
|
463
|
+
This example benefits from recent SDK improvements:
|
|
464
|
+
|
|
465
|
+
### 1. Intelligent Fallback Matching
|
|
466
|
+
|
|
467
|
+
The SDK now includes **two-tier message correlation**:
|
|
468
|
+
|
|
469
|
+
**Primary Matching:** Uses `client_request_id` when server supports it
|
|
470
|
+
```typescript
|
|
471
|
+
const responseRequestId = r.raw?.data?.client_request_id;
|
|
472
|
+
if (responseRequestId === requestId) {
|
|
473
|
+
return true; // Match found!
|
|
474
|
+
}
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
**Fallback Matching:** Uses room + time window when server doesn't support request IDs
|
|
478
|
+
```typescript
|
|
479
|
+
// If server doesn't echo client_request_id,
|
|
480
|
+
// match first response from expected room within 60 seconds
|
|
481
|
+
const timeSinceRequest = Date.now() - requestTimestamp;
|
|
482
|
+
const isFromExpectedRoom = responseRoom === message.room;
|
|
483
|
+
const isWithinTimeWindow = timeSinceRequest < 60000;
|
|
484
|
+
|
|
485
|
+
if (isFromExpectedRoom && isWithinTimeWindow && !responseRequestId) {
|
|
486
|
+
return true; // Fallback match!
|
|
487
|
+
}
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
**Benefits:**
|
|
491
|
+
- Works with servers that don't support `client_request_id` echoing
|
|
492
|
+
- Typical response time: 2-3 seconds
|
|
493
|
+
- No manual event listener management
|
|
494
|
+
- Automatic retry and backoff
|
|
495
|
+
|
|
496
|
+
### 2. Enhanced Regular Message Handling
|
|
497
|
+
|
|
498
|
+
The SDK now properly handles both "message" and "task_response" types:
|
|
499
|
+
|
|
500
|
+
```typescript
|
|
501
|
+
// RegularMessageHandler now includes raw field for correlation
|
|
502
|
+
const response: AgentResponse = {
|
|
503
|
+
taskId: message.data?.task_id || `msg-${Date.now()}`,
|
|
504
|
+
agentId: message.from,
|
|
505
|
+
content: message.content,
|
|
506
|
+
raw: message as any, // ← Critical for request correlation
|
|
507
|
+
humanized: message.content
|
|
508
|
+
};
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
**Result:** X-Agent responses (which use "message" type) now work seamlessly with `waitForResponse: true`.
|
|
512
|
+
|
|
513
|
+
## Related Examples
|
|
514
|
+
|
|
515
|
+
- [OpenAI Codex + Teneo](../openai-teneo/) - Intelligent agent routing with Codex
|
|
516
|
+
- [n8n + Teneo](../n8n-teneo/) - Visual workflow automation
|
|
517
|
+
- [Production Dashboard](../production-dashboard/) - Full-featured dashboard
|
|
518
|
+
|
|
519
|
+
### Comparison
|
|
520
|
+
|
|
521
|
+
| Feature | Claude Agent | OpenAI Codex | n8n |
|
|
522
|
+
|---------|--------------|--------------|-----|
|
|
523
|
+
| **Interface** | CLI Tool | REST API | Visual Workflows |
|
|
524
|
+
| **AI Model** | Claude 3.5 | OpenAI Codex | N/A (HTTP only) |
|
|
525
|
+
| **Tool Pattern** | Custom MCP Tools | Agent Selection | Manual Endpoints |
|
|
526
|
+
| **Use Case** | Developer Tool | API Integration | Automation |
|
|
527
|
+
| **Complexity** | ~220 lines | ~280 lines | ~180 lines |
|
|
528
|
+
| **Response Time** | 2-3 seconds | 2-3 seconds | 2-3 seconds |
|
|
529
|
+
|
|
530
|
+
**Note:** All examples use the same **modern SDKConfigBuilder pattern** with:
|
|
531
|
+
- ✅ Automatic wallet derivation from private key
|
|
532
|
+
- ✅ Auto-reconnection with exponential backoff
|
|
533
|
+
- ✅ Agent response caching for performance
|
|
534
|
+
- ✅ Optional SEC-2 signature verification
|
|
535
|
+
- ✅ **SDK-level fallback matching for reliable message correlation**
|
|
536
|
+
- ✅ Comprehensive environment-based configuration
|
|
537
|
+
|
|
538
|
+
## License
|
|
539
|
+
|
|
540
|
+
MIT
|