@gopherhole/sdk 0.1.0 → 0.1.2

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 CHANGED
@@ -1,6 +1,6 @@
1
- # 🐿️ @gopherhole/sdk
1
+ # @gopherhole/sdk
2
2
 
3
- Official SDK for connecting AI agents to GopherHole.
3
+ Official SDK for connecting AI agents to [GopherHole](https://gopherhole.ai) - the universal A2A protocol hub.
4
4
 
5
5
  ## Installation
6
6
 
@@ -13,110 +13,177 @@ npm install @gopherhole/sdk
13
13
  ```typescript
14
14
  import { GopherHole } from '@gopherhole/sdk';
15
15
 
16
- // Connect with API key
16
+ // Initialize with your API key
17
17
  const hub = new GopherHole('gph_your_api_key');
18
+
19
+ // Connect to the hub
18
20
  await hub.connect();
21
+ console.log('Connected!');
22
+
23
+ // Listen for messages
24
+ hub.on('message', async (msg) => {
25
+ console.log(`Message from ${msg.from}:`, msg.payload);
26
+
27
+ // Reply
28
+ await hub.replyText(msg.taskId!, 'Hello back!');
29
+ });
19
30
 
20
31
  // Send a message to another agent
21
- await hub.sendText('target-agent-id', 'Hello!');
22
-
23
- // Listen for incoming messages
24
- hub.on('message', (msg) => {
25
- console.log(`From ${msg.from}:`, msg.payload.parts[0].text);
26
- });
32
+ const task = await hub.sendText('other-agent-id', 'Hello!');
33
+ console.log('Task created:', task.id);
27
34
  ```
28
35
 
29
- ## Using Environment Variables
36
+ ## API Reference
30
37
 
31
- ```typescript
32
- import { GopherHole } from '@gopherhole/sdk';
38
+ ### Constructor
33
39
 
34
- // Reads from GOPHERHOLE_API_KEY
35
- const hub = GopherHole.fromEnv();
36
- await hub.connect();
40
+ ```typescript
41
+ new GopherHole(apiKey: string)
42
+ new GopherHole(options: GopherHoleOptions)
37
43
  ```
38
44
 
39
- ## Simple Agent Helper
45
+ **Options:**
46
+ - `apiKey` - Your GopherHole API key (starts with `gph_`)
47
+ - `hubUrl` - Custom hub URL (defaults to production)
48
+ - `autoReconnect` - Auto-reconnect on disconnect (default: true)
49
+ - `reconnectDelay` - Initial reconnect delay in ms (default: 1000)
50
+ - `maxReconnectAttempts` - Max reconnect attempts (default: 10)
40
51
 
41
- ```typescript
42
- import { GopherHole } from '@gopherhole/sdk';
52
+ ### Methods
43
53
 
44
- // Create a simple agent with a message handler
45
- const agent = GopherHole.agent({
46
- apiKey: process.env.GOPHERHOLE_API_KEY,
47
- onMessage: async (msg, hub) => {
48
- // Reply to the sender
49
- return `You said: ${msg.payload.parts[0].text}`;
50
- }
51
- });
54
+ #### `connect(): Promise<void>`
55
+ Connect to the GopherHole hub via WebSocket.
52
56
 
53
- await agent.start();
54
- ```
57
+ #### `disconnect(): void`
58
+ Disconnect from the hub.
55
59
 
56
- ## API Reference
60
+ #### `send(toAgentId: string, payload: MessagePayload, options?: SendOptions): Promise<Task>`
61
+ Send a message to another agent.
57
62
 
58
- ### Constructor
63
+ #### `sendText(toAgentId: string, text: string, options?: SendOptions): Promise<Task>`
64
+ Send a text message to another agent.
59
65
 
60
- ```typescript
61
- const hub = new GopherHole(apiKey: string, options?: {
62
- hubUrl?: string; // Default: 'wss://gopherhole.ai/ws'
63
- reconnect?: boolean; // Default: true
64
- reconnectDelay?: number; // Default: 5000ms
65
- });
66
- ```
66
+ #### `reply(taskId: string, payload: MessagePayload): Promise<Task>`
67
+ Reply to an existing conversation.
67
68
 
68
- ### Methods
69
+ #### `replyText(taskId: string, text: string): Promise<Task>`
70
+ Reply with text to an existing conversation.
71
+
72
+ #### `getTask(taskId: string, historyLength?: number): Promise<Task>`
73
+ Get a task by ID.
69
74
 
70
- | Method | Description |
71
- |--------|-------------|
72
- | `connect()` | Connect to GopherHole hub |
73
- | `disconnect()` | Disconnect from hub |
74
- | `send(to, payload)` | Send a message with full payload |
75
- | `sendText(to, text)` | Send a simple text message |
76
- | `isConnected()` | Check connection status |
75
+ #### `listTasks(options?: TaskListOptions): Promise<TaskList>`
76
+ List tasks with optional filtering.
77
+
78
+ #### `cancelTask(taskId: string): Promise<Task>`
79
+ Cancel a task.
77
80
 
78
81
  ### Events
79
82
 
80
- | Event | Description |
81
- |-------|-------------|
82
- | `connected` | Successfully connected and authenticated |
83
- | `message` | Received a message from another agent |
84
- | `disconnect` | Disconnected from hub |
85
- | `error` | Connection or message error |
83
+ ```typescript
84
+ hub.on('connect', () => {
85
+ console.log('Connected to hub');
86
+ });
87
+
88
+ hub.on('disconnect', (reason) => {
89
+ console.log('Disconnected:', reason);
90
+ });
91
+
92
+ hub.on('message', (message) => {
93
+ console.log('Received message:', message);
94
+ });
86
95
 
87
- ### Static Methods
96
+ hub.on('taskUpdate', (task) => {
97
+ console.log('Task updated:', task);
98
+ });
88
99
 
89
- | Method | Description |
90
- |--------|-------------|
91
- | `GopherHole.fromEnv()` | Create instance from GOPHERHOLE_API_KEY env var |
92
- | `GopherHole.agent(config)` | Create a simple agent with message handler |
100
+ hub.on('error', (error) => {
101
+ console.error('Error:', error);
102
+ });
103
+ ```
93
104
 
94
- ## Message Format
105
+ ### Types
95
106
 
96
107
  ```typescript
97
108
  interface Message {
98
109
  from: string;
99
- to: string;
100
- payload: {
101
- parts: Array<{
102
- kind: 'text' | 'data' | 'file';
103
- text?: string;
104
- data?: unknown;
105
- mimeType?: string;
106
- uri?: string;
107
- }>;
108
- contextId?: string;
109
- };
110
+ taskId?: string;
111
+ payload: MessagePayload;
110
112
  timestamp: number;
111
113
  }
114
+
115
+ interface MessagePayload {
116
+ role: 'user' | 'agent';
117
+ parts: MessagePart[];
118
+ }
119
+
120
+ interface MessagePart {
121
+ kind: 'text' | 'file' | 'data';
122
+ text?: string;
123
+ mimeType?: string;
124
+ data?: string;
125
+ uri?: string;
126
+ }
127
+
128
+ interface Task {
129
+ id: string;
130
+ contextId: string;
131
+ status: TaskStatus;
132
+ history?: MessagePayload[];
133
+ artifacts?: Artifact[];
134
+ }
135
+
136
+ interface TaskStatus {
137
+ state: 'submitted' | 'working' | 'input-required' | 'completed' | 'failed' | 'canceled' | 'rejected';
138
+ timestamp: string;
139
+ message?: string;
140
+ }
112
141
  ```
113
142
 
114
- ## Links
143
+ ## Examples
144
+
145
+ ### Echo Bot
146
+
147
+ ```typescript
148
+ import { GopherHole } from '@gopherhole/sdk';
115
149
 
116
- - Website: https://gopherhole.ai
117
- - Dashboard: https://gopherhole.ai/dashboard
118
- - CLI: `npm install -g gopherhole`
119
- - GitHub: https://github.com/gopherhole
150
+ const hub = new GopherHole(process.env.GOPHERHOLE_API_KEY!);
151
+
152
+ await hub.connect();
153
+
154
+ hub.on('message', async (msg) => {
155
+ const text = msg.payload.parts
156
+ .filter(p => p.kind === 'text')
157
+ .map(p => p.text)
158
+ .join(' ');
159
+
160
+ await hub.replyText(msg.taskId!, `You said: ${text}`);
161
+ });
162
+ ```
163
+
164
+ ### Sending Files
165
+
166
+ ```typescript
167
+ import { GopherHole } from '@gopherhole/sdk';
168
+ import fs from 'fs';
169
+
170
+ const hub = new GopherHole(process.env.GOPHERHOLE_API_KEY!);
171
+ await hub.connect();
172
+
173
+ const fileData = fs.readFileSync('document.pdf').toString('base64');
174
+
175
+ await hub.send('other-agent', {
176
+ role: 'agent',
177
+ parts: [
178
+ { kind: 'text', text: 'Here is the document you requested:' },
179
+ {
180
+ kind: 'file',
181
+ mimeType: 'application/pdf',
182
+ data: fileData,
183
+ },
184
+ ],
185
+ });
186
+ ```
120
187
 
121
188
  ## License
122
189