edgecrab-sdk 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.
@@ -0,0 +1,234 @@
1
+ /** Type definitions for the EdgeCrab Node.js SDK. */
2
+ interface ChatMessage {
3
+ role: 'system' | 'user' | 'assistant' | 'tool';
4
+ content: string;
5
+ name?: string;
6
+ tool_call_id?: string;
7
+ }
8
+ interface ChatCompletionRequest {
9
+ model: string;
10
+ messages: ChatMessage[];
11
+ temperature?: number;
12
+ max_tokens?: number;
13
+ stream?: boolean;
14
+ tools?: Record<string, unknown>[];
15
+ }
16
+ interface UsageInfo {
17
+ prompt_tokens: number;
18
+ completion_tokens: number;
19
+ total_tokens: number;
20
+ }
21
+ interface ChatChoice {
22
+ index: number;
23
+ message: ChatMessage;
24
+ finish_reason: string | null;
25
+ }
26
+ interface ChatCompletionResponse {
27
+ id: string;
28
+ object: string;
29
+ created: number;
30
+ model: string;
31
+ choices: ChatChoice[];
32
+ usage?: UsageInfo;
33
+ }
34
+ interface StreamDelta {
35
+ role?: string;
36
+ content?: string;
37
+ }
38
+ interface StreamChoice {
39
+ index: number;
40
+ delta: StreamDelta;
41
+ finish_reason: string | null;
42
+ }
43
+ interface StreamChunk {
44
+ id: string;
45
+ object: string;
46
+ created: number;
47
+ model: string;
48
+ choices: StreamChoice[];
49
+ }
50
+ interface ModelInfo {
51
+ id: string;
52
+ object: string;
53
+ created?: number;
54
+ owned_by?: string;
55
+ }
56
+ interface HealthResponse {
57
+ status: string;
58
+ version?: string;
59
+ }
60
+ interface AgentOptions {
61
+ model?: string;
62
+ baseUrl?: string;
63
+ apiKey?: string;
64
+ systemPrompt?: string;
65
+ maxTurns?: number;
66
+ temperature?: number;
67
+ maxTokens?: number;
68
+ timeout?: number;
69
+ sessionId?: string;
70
+ streaming?: boolean;
71
+ maxRetries?: number;
72
+ onToken?: (token: string) => void;
73
+ onToolCall?: (name: string, args: Record<string, unknown>) => void;
74
+ onTurn?: (turnNum: number, message: ChatMessage) => void;
75
+ onError?: (error: Error) => void;
76
+ }
77
+ interface AgentResult {
78
+ response: string;
79
+ messages: ChatMessage[];
80
+ sessionId: string;
81
+ model: string;
82
+ turnsUsed: number;
83
+ finishedNaturally: boolean;
84
+ interrupted: boolean;
85
+ maxTurnsExceeded: boolean;
86
+ usage: UsageInfo;
87
+ }
88
+ interface ClientOptions {
89
+ baseUrl?: string;
90
+ apiKey?: string;
91
+ timeout?: number;
92
+ maxRetries?: number;
93
+ retryBaseDelay?: number;
94
+ }
95
+ interface ExportedConversation {
96
+ sessionId: string;
97
+ model: string;
98
+ messages: ChatMessage[];
99
+ turnCount: number;
100
+ usage: UsageInfo;
101
+ }
102
+
103
+ /**
104
+ * High-level Agent abstraction for the EdgeCrab Node.js SDK.
105
+ *
106
+ * Inspired by hermes-agent's AIAgent — provides a batteries-included Agent
107
+ * object that manages conversation state, streaming, and session lifecycle.
108
+ */
109
+
110
+ declare class Agent {
111
+ readonly model: string;
112
+ readonly systemPrompt: string | undefined;
113
+ readonly maxTurns: number;
114
+ readonly temperature: number | undefined;
115
+ readonly maxTokens: number | undefined;
116
+ readonly streaming: boolean;
117
+ sessionId: string;
118
+ onToken: ((token: string) => void) | undefined;
119
+ onToolCall: ((name: string, args: Record<string, unknown>) => void) | undefined;
120
+ onTurn: ((turnNum: number, message: ChatMessage) => void) | undefined;
121
+ onError: ((error: Error) => void) | undefined;
122
+ private messages;
123
+ private turnCount;
124
+ private totalUsage;
125
+ private client;
126
+ private interrupted;
127
+ constructor(options?: AgentOptions);
128
+ /** Signal the agent to stop after the current turn. */
129
+ interrupt(): void;
130
+ /** Clear the interrupt flag so the agent can continue. */
131
+ clearInterrupt(): void;
132
+ /** Whether the agent has been interrupted. */
133
+ get isInterrupted(): boolean;
134
+ /** Send a message and return the assistant's text reply. Maintains history. */
135
+ chat(message: string): Promise<string>;
136
+ private chatStreaming;
137
+ /** Run a full agent conversation. Returns a structured AgentResult. */
138
+ run(message: string, options?: {
139
+ conversationHistory?: ChatMessage[];
140
+ }): Promise<AgentResult>;
141
+ /** Stream response tokens as an async iterable. */
142
+ stream(message: string): AsyncGenerator<string>;
143
+ /** Manually inject a message into the conversation history. */
144
+ addMessage(role: ChatMessage['role'], content: string): void;
145
+ /** Reset conversation state for a new session. */
146
+ reset(): void;
147
+ /** Export the current conversation state as a serializable object. */
148
+ exportConversation(): ExportedConversation;
149
+ /** Restore a conversation state from a previously exported object. */
150
+ importConversation(data: ExportedConversation): void;
151
+ /** Create a fork of this agent with an independent copy of the conversation. */
152
+ clone(): Agent;
153
+ /** Current conversation history (copy). */
154
+ getMessages(): ChatMessage[];
155
+ /** Number of user turns completed. */
156
+ getTurnCount(): number;
157
+ /** Accumulated token usage. */
158
+ getUsage(): UsageInfo;
159
+ /** List available models from the server. */
160
+ listModels(): Promise<ModelInfo[]>;
161
+ /** Check server health. */
162
+ health(): Promise<HealthResponse>;
163
+ private extractResponse;
164
+ private accumulateUsage;
165
+ }
166
+
167
+ /**
168
+ * Low-level HTTP client for the EdgeCrab OpenAI-compatible API.
169
+ */
170
+
171
+ declare class EdgeCrabError extends Error {
172
+ statusCode: number | undefined;
173
+ constructor(message: string, statusCode?: number);
174
+ }
175
+ declare class AuthenticationError extends EdgeCrabError {
176
+ constructor(message: string, statusCode?: number);
177
+ }
178
+ declare class RateLimitError extends EdgeCrabError {
179
+ retryAfter: number | undefined;
180
+ constructor(message: string, retryAfter?: number);
181
+ }
182
+ declare class ServerError extends EdgeCrabError {
183
+ constructor(message: string, statusCode?: number);
184
+ }
185
+ declare class TimeoutError extends EdgeCrabError {
186
+ constructor(message?: string);
187
+ }
188
+ declare class ConnectionError extends EdgeCrabError {
189
+ constructor(message?: string);
190
+ }
191
+ declare class MaxTurnsExceededError extends EdgeCrabError {
192
+ maxTurns: number;
193
+ constructor(maxTurns: number);
194
+ }
195
+ declare class InterruptedError extends EdgeCrabError {
196
+ constructor();
197
+ }
198
+ declare class EdgeCrabClient {
199
+ private baseUrl;
200
+ private headers;
201
+ private timeout;
202
+ private maxRetries;
203
+ private retryBaseDelay;
204
+ constructor(options?: ClientOptions);
205
+ /** Simple chat — send a message, get a reply string. */
206
+ chat(message: string, options?: {
207
+ model?: string;
208
+ system?: string;
209
+ temperature?: number;
210
+ maxTokens?: number;
211
+ }): Promise<string>;
212
+ /** Create a chat completion (non-streaming). */
213
+ createCompletion(messages: ChatMessage[], options?: {
214
+ model?: string;
215
+ temperature?: number;
216
+ maxTokens?: number;
217
+ tools?: Record<string, unknown>[];
218
+ }): Promise<ChatCompletionResponse>;
219
+ private postWithRetry;
220
+ /** Create a streaming chat completion. Returns an async iterator of chunks. */
221
+ streamCompletion(messages: ChatMessage[], options?: {
222
+ model?: string;
223
+ temperature?: number;
224
+ maxTokens?: number;
225
+ tools?: Record<string, unknown>[];
226
+ }): AsyncGenerator<StreamChunk>;
227
+ /** List available models. */
228
+ listModels(): Promise<ModelInfo[]>;
229
+ /** Check server health. */
230
+ health(): Promise<HealthResponse>;
231
+ private fetchJSON;
232
+ }
233
+
234
+ export { Agent, type AgentOptions, type AgentResult, AuthenticationError, type ChatChoice, type ChatCompletionRequest, type ChatCompletionResponse, type ChatMessage, type ClientOptions, ConnectionError, EdgeCrabClient, EdgeCrabError, type ExportedConversation, type HealthResponse, InterruptedError, MaxTurnsExceededError, type ModelInfo, RateLimitError, ServerError, type StreamChoice, type StreamChunk, type StreamDelta, TimeoutError, type UsageInfo };
@@ -0,0 +1,234 @@
1
+ /** Type definitions for the EdgeCrab Node.js SDK. */
2
+ interface ChatMessage {
3
+ role: 'system' | 'user' | 'assistant' | 'tool';
4
+ content: string;
5
+ name?: string;
6
+ tool_call_id?: string;
7
+ }
8
+ interface ChatCompletionRequest {
9
+ model: string;
10
+ messages: ChatMessage[];
11
+ temperature?: number;
12
+ max_tokens?: number;
13
+ stream?: boolean;
14
+ tools?: Record<string, unknown>[];
15
+ }
16
+ interface UsageInfo {
17
+ prompt_tokens: number;
18
+ completion_tokens: number;
19
+ total_tokens: number;
20
+ }
21
+ interface ChatChoice {
22
+ index: number;
23
+ message: ChatMessage;
24
+ finish_reason: string | null;
25
+ }
26
+ interface ChatCompletionResponse {
27
+ id: string;
28
+ object: string;
29
+ created: number;
30
+ model: string;
31
+ choices: ChatChoice[];
32
+ usage?: UsageInfo;
33
+ }
34
+ interface StreamDelta {
35
+ role?: string;
36
+ content?: string;
37
+ }
38
+ interface StreamChoice {
39
+ index: number;
40
+ delta: StreamDelta;
41
+ finish_reason: string | null;
42
+ }
43
+ interface StreamChunk {
44
+ id: string;
45
+ object: string;
46
+ created: number;
47
+ model: string;
48
+ choices: StreamChoice[];
49
+ }
50
+ interface ModelInfo {
51
+ id: string;
52
+ object: string;
53
+ created?: number;
54
+ owned_by?: string;
55
+ }
56
+ interface HealthResponse {
57
+ status: string;
58
+ version?: string;
59
+ }
60
+ interface AgentOptions {
61
+ model?: string;
62
+ baseUrl?: string;
63
+ apiKey?: string;
64
+ systemPrompt?: string;
65
+ maxTurns?: number;
66
+ temperature?: number;
67
+ maxTokens?: number;
68
+ timeout?: number;
69
+ sessionId?: string;
70
+ streaming?: boolean;
71
+ maxRetries?: number;
72
+ onToken?: (token: string) => void;
73
+ onToolCall?: (name: string, args: Record<string, unknown>) => void;
74
+ onTurn?: (turnNum: number, message: ChatMessage) => void;
75
+ onError?: (error: Error) => void;
76
+ }
77
+ interface AgentResult {
78
+ response: string;
79
+ messages: ChatMessage[];
80
+ sessionId: string;
81
+ model: string;
82
+ turnsUsed: number;
83
+ finishedNaturally: boolean;
84
+ interrupted: boolean;
85
+ maxTurnsExceeded: boolean;
86
+ usage: UsageInfo;
87
+ }
88
+ interface ClientOptions {
89
+ baseUrl?: string;
90
+ apiKey?: string;
91
+ timeout?: number;
92
+ maxRetries?: number;
93
+ retryBaseDelay?: number;
94
+ }
95
+ interface ExportedConversation {
96
+ sessionId: string;
97
+ model: string;
98
+ messages: ChatMessage[];
99
+ turnCount: number;
100
+ usage: UsageInfo;
101
+ }
102
+
103
+ /**
104
+ * High-level Agent abstraction for the EdgeCrab Node.js SDK.
105
+ *
106
+ * Inspired by hermes-agent's AIAgent — provides a batteries-included Agent
107
+ * object that manages conversation state, streaming, and session lifecycle.
108
+ */
109
+
110
+ declare class Agent {
111
+ readonly model: string;
112
+ readonly systemPrompt: string | undefined;
113
+ readonly maxTurns: number;
114
+ readonly temperature: number | undefined;
115
+ readonly maxTokens: number | undefined;
116
+ readonly streaming: boolean;
117
+ sessionId: string;
118
+ onToken: ((token: string) => void) | undefined;
119
+ onToolCall: ((name: string, args: Record<string, unknown>) => void) | undefined;
120
+ onTurn: ((turnNum: number, message: ChatMessage) => void) | undefined;
121
+ onError: ((error: Error) => void) | undefined;
122
+ private messages;
123
+ private turnCount;
124
+ private totalUsage;
125
+ private client;
126
+ private interrupted;
127
+ constructor(options?: AgentOptions);
128
+ /** Signal the agent to stop after the current turn. */
129
+ interrupt(): void;
130
+ /** Clear the interrupt flag so the agent can continue. */
131
+ clearInterrupt(): void;
132
+ /** Whether the agent has been interrupted. */
133
+ get isInterrupted(): boolean;
134
+ /** Send a message and return the assistant's text reply. Maintains history. */
135
+ chat(message: string): Promise<string>;
136
+ private chatStreaming;
137
+ /** Run a full agent conversation. Returns a structured AgentResult. */
138
+ run(message: string, options?: {
139
+ conversationHistory?: ChatMessage[];
140
+ }): Promise<AgentResult>;
141
+ /** Stream response tokens as an async iterable. */
142
+ stream(message: string): AsyncGenerator<string>;
143
+ /** Manually inject a message into the conversation history. */
144
+ addMessage(role: ChatMessage['role'], content: string): void;
145
+ /** Reset conversation state for a new session. */
146
+ reset(): void;
147
+ /** Export the current conversation state as a serializable object. */
148
+ exportConversation(): ExportedConversation;
149
+ /** Restore a conversation state from a previously exported object. */
150
+ importConversation(data: ExportedConversation): void;
151
+ /** Create a fork of this agent with an independent copy of the conversation. */
152
+ clone(): Agent;
153
+ /** Current conversation history (copy). */
154
+ getMessages(): ChatMessage[];
155
+ /** Number of user turns completed. */
156
+ getTurnCount(): number;
157
+ /** Accumulated token usage. */
158
+ getUsage(): UsageInfo;
159
+ /** List available models from the server. */
160
+ listModels(): Promise<ModelInfo[]>;
161
+ /** Check server health. */
162
+ health(): Promise<HealthResponse>;
163
+ private extractResponse;
164
+ private accumulateUsage;
165
+ }
166
+
167
+ /**
168
+ * Low-level HTTP client for the EdgeCrab OpenAI-compatible API.
169
+ */
170
+
171
+ declare class EdgeCrabError extends Error {
172
+ statusCode: number | undefined;
173
+ constructor(message: string, statusCode?: number);
174
+ }
175
+ declare class AuthenticationError extends EdgeCrabError {
176
+ constructor(message: string, statusCode?: number);
177
+ }
178
+ declare class RateLimitError extends EdgeCrabError {
179
+ retryAfter: number | undefined;
180
+ constructor(message: string, retryAfter?: number);
181
+ }
182
+ declare class ServerError extends EdgeCrabError {
183
+ constructor(message: string, statusCode?: number);
184
+ }
185
+ declare class TimeoutError extends EdgeCrabError {
186
+ constructor(message?: string);
187
+ }
188
+ declare class ConnectionError extends EdgeCrabError {
189
+ constructor(message?: string);
190
+ }
191
+ declare class MaxTurnsExceededError extends EdgeCrabError {
192
+ maxTurns: number;
193
+ constructor(maxTurns: number);
194
+ }
195
+ declare class InterruptedError extends EdgeCrabError {
196
+ constructor();
197
+ }
198
+ declare class EdgeCrabClient {
199
+ private baseUrl;
200
+ private headers;
201
+ private timeout;
202
+ private maxRetries;
203
+ private retryBaseDelay;
204
+ constructor(options?: ClientOptions);
205
+ /** Simple chat — send a message, get a reply string. */
206
+ chat(message: string, options?: {
207
+ model?: string;
208
+ system?: string;
209
+ temperature?: number;
210
+ maxTokens?: number;
211
+ }): Promise<string>;
212
+ /** Create a chat completion (non-streaming). */
213
+ createCompletion(messages: ChatMessage[], options?: {
214
+ model?: string;
215
+ temperature?: number;
216
+ maxTokens?: number;
217
+ tools?: Record<string, unknown>[];
218
+ }): Promise<ChatCompletionResponse>;
219
+ private postWithRetry;
220
+ /** Create a streaming chat completion. Returns an async iterator of chunks. */
221
+ streamCompletion(messages: ChatMessage[], options?: {
222
+ model?: string;
223
+ temperature?: number;
224
+ maxTokens?: number;
225
+ tools?: Record<string, unknown>[];
226
+ }): AsyncGenerator<StreamChunk>;
227
+ /** List available models. */
228
+ listModels(): Promise<ModelInfo[]>;
229
+ /** Check server health. */
230
+ health(): Promise<HealthResponse>;
231
+ private fetchJSON;
232
+ }
233
+
234
+ export { Agent, type AgentOptions, type AgentResult, AuthenticationError, type ChatChoice, type ChatCompletionRequest, type ChatCompletionResponse, type ChatMessage, type ClientOptions, ConnectionError, EdgeCrabClient, EdgeCrabError, type ExportedConversation, type HealthResponse, InterruptedError, MaxTurnsExceededError, type ModelInfo, RateLimitError, ServerError, type StreamChoice, type StreamChunk, type StreamDelta, TimeoutError, type UsageInfo };