-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.d.ts
More file actions
162 lines (151 loc) · 4.67 KB
/
index.d.ts
File metadata and controls
162 lines (151 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* TypeScript definitions for uAgent Node.js Client
*/
export interface UAgentClientConfig {
/** URL of the bridge agent. Default: 'http://localhost:8000' */
bridgeUrl?: string;
/** Request timeout in milliseconds. Default: 35000 */
timeout?: number;
/** Automatically start bridge agent. Default: true */
autoStartBridge?: boolean;
/** Port for bridge agent. Default: 8000 */
bridgePort?: number;
/** Unique seed for per-user bridge agent */
userSeed?: string;
/** Bearer token for Agentverse registration */
agentverseToken?: string;
}
export interface QueryResponse {
/** Whether the query was successful */
success: boolean;
/** Response from the agent (present if success is true) */
response?: string;
/** Error message (present if success is false) */
error?: string;
/** Unique request identifier */
requestId: string;
}
export interface BridgeInfo {
/** Agent name */
name: string;
/** Agent address */
address: string;
/** Port number */
port: number;
/** Seed used */
seed: string;
}
/**
* Client for communicating with uAgents from Node.js
*
* The bridge agent is automatically started when the client is created.
* No need to run `python bridge_agent.py` manually!
*
* @example
* ```typescript
* // Simple usage - shared bridge agent starts automatically!
* const client = new UAgentClient();
*
* // Per-user bridge agent with Agentverse registration
* const client = new UAgentClient({
* userSeed: 'user-123',
* agentverseToken: 'bearer-token'
* });
*
* // Query any agent address
* const result = await client.query(
* 'agent1q2g97humd4d6mgmcg783s2dsncu8hn37r3sgglu6eqa6es07wk3xqlmmy4v',
* 'Search for AI news'
* );
* if (result.success) {
* console.log(result.response);
* }
*
* // Or use ask() for simpler usage
* const response = await client.ask(agentAddress, 'Hello!');
* console.log(response);
*
* // Stop bridge when done (optional, auto-stops on exit)
* client.stopBridge();
* ```
*/
export default class UAgentClient {
/**
* Create a new uAgent client
* Bridge agent starts automatically unless autoStartBridge is false
* @param config - Configuration options
*/
constructor(config?: UAgentClientConfig);
/**
* Get or create a per-user bridge agent
* @param seed - Unique seed for the user
* @param agentverseToken - Bearer token for Agentverse registration
* @param port - Optional port number
* @returns Promise with bridge agent info
*/
createUserBridge(seed: string, agentverseToken: string, port?: number): Promise<BridgeInfo>;
/**
* Send a query to a uAgent and get full response object
*
* @param agentAddress - The address of the target uAgent
* @param query - The message/query to send
* @param requestId - Optional custom request ID
* @returns Promise with response object
*
* @example
* ```typescript
* const result = await client.query(
* 'agent1q2n8a5gnyrywq95xntxf2v3xh4kufsj5gt8umzmfxkf7wn5l7kun7vys96q',
* 'Hello from Node.js!'
* );
* ```
*/
query(agentAddress: string, query: string, requestId?: string): Promise<QueryResponse>;
/**
* Send a query and get only the response string
* Throws an error if the query fails
*
* @param agentAddress - The address of the target uAgent
* @param query - The message/query to send
* @returns Promise with response string
* @throws Error if the query fails
*
* @example
* ```typescript
* try {
* const response = await client.ask(
* 'agent1q2n8a5gnyrywq95xntxf2v3xh4kufsj5gt8umzmfxkf7wn5l7kun7vys96q',
* 'What is 2 + 2?'
* );
* console.log(response); // "4"
* } catch (error) {
* console.error('Query failed:', error.message);
* }
* ```
*/
ask(agentAddress: string, query: string): Promise<string>;
/**
* Check if the bridge agent is available
*
* @returns Promise with true if bridge is available, false otherwise
*
* @example
* ```typescript
* const isAvailable = await client.ping();
* console.log('Bridge available:', isAvailable);
* ```
*/
ping(): Promise<boolean>;
/**
* Wait for bridge agent to be ready
*
* @param maxWaitTime - Maximum time to wait in milliseconds (default: 20000)
* @returns Promise with true if bridge is ready
*/
waitForBridge(maxWaitTime?: number): Promise<boolean>;
/**
* Stop the bridge agent process
* Automatically called on process exit
*/
stopBridge(): void;
}