aura-llm 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,371 @@
1
+ /**
2
+ * Aura SDK error hierarchy.
3
+ *
4
+ * Mirrors the Python SDK's `exceptions.py`:
5
+ * AuraError
6
+ * ├── APIError (errors returned by the gateway)
7
+ * │ ├── AuthenticationError (401)
8
+ * │ ├── BadRequestError (400)
9
+ * │ ├── NotFoundError (404)
10
+ * │ └── RateLimitError (429, carries retryAfter)
11
+ * ├── APIConnectionError (network failure)
12
+ * └── APITimeoutError (exceeded the configured timeout)
13
+ *
14
+ * Use `instanceof` to branch on error class.
15
+ */
16
+ interface AuraErrorOptions {
17
+ code?: string;
18
+ param?: string;
19
+ status?: number;
20
+ requestId?: string;
21
+ responseBody?: unknown;
22
+ }
23
+ /** Base class for every error thrown by the SDK. */
24
+ declare class AuraError extends Error {
25
+ /** Gateway error code (e.g. `invalid_model`), when present. */
26
+ readonly code?: string;
27
+ /** Offending request parameter, when the gateway reports one. */
28
+ readonly param?: string;
29
+ /** HTTP status code, when the error originated from a response. */
30
+ readonly status?: number;
31
+ /** Gateway request id for support/correlation, when present. */
32
+ readonly requestId?: string;
33
+ /** Raw parsed response body, for debugging. */
34
+ readonly responseBody?: unknown;
35
+ constructor(message: string, options?: AuraErrorOptions);
36
+ }
37
+ /** An error returned by the Aura API (any non-2xx with a parseable body). */
38
+ declare class APIError extends AuraError {
39
+ }
40
+ /** 401 — invalid or missing API key. */
41
+ declare class AuthenticationError extends APIError {
42
+ constructor(message?: string, options?: AuraErrorOptions);
43
+ }
44
+ /** 400 — the request was malformed or invalid. */
45
+ declare class BadRequestError extends APIError {
46
+ constructor(message: string, options?: AuraErrorOptions);
47
+ }
48
+ /** 404 — resource not found (e.g. an unknown model). */
49
+ declare class NotFoundError extends APIError {
50
+ constructor(message: string, options?: AuraErrorOptions);
51
+ }
52
+ /** 429 — rate limit exceeded. */
53
+ declare class RateLimitError extends APIError {
54
+ /** Seconds to wait before retrying, from the `Retry-After` header. */
55
+ readonly retryAfter?: number;
56
+ constructor(message?: string, options?: AuraErrorOptions & {
57
+ retryAfter?: number;
58
+ });
59
+ }
60
+ /** Failed to connect to the gateway (DNS, refused, reset, …). */
61
+ declare class APIConnectionError extends AuraError {
62
+ constructor(message?: string, options?: AuraErrorOptions);
63
+ }
64
+ /** The request exceeded the configured `timeout`. */
65
+ declare class APITimeoutError extends AuraError {
66
+ constructor(message?: string, options?: AuraErrorOptions);
67
+ }
68
+
69
+ /**
70
+ * Open Responses API types, ported from the Python SDK's `types.py`.
71
+ *
72
+ * These are plain TypeScript interfaces/unions (the gateway returns JSON; we
73
+ * don't validate at runtime beyond shape-narrowing on `type`). Helper
74
+ * functions (`outputText`, `toolCalls`) replace the Python `@property`
75
+ * accessors, since interfaces can't carry methods.
76
+ */
77
+ type ResponseStatus = 'in_progress' | 'completed' | 'failed' | 'incomplete' | 'cancelled';
78
+ type ItemType = 'message' | 'function_call' | 'function_call_output' | 'reasoning';
79
+ type Role = 'user' | 'assistant' | 'system';
80
+ interface TextContent {
81
+ type: 'text';
82
+ text: string;
83
+ }
84
+ interface ImageContent {
85
+ type: 'image';
86
+ url?: string;
87
+ base64?: string;
88
+ media_type?: string;
89
+ }
90
+ type Content = TextContent | ImageContent;
91
+ interface MessageItem {
92
+ type: 'message';
93
+ id?: string;
94
+ role: Role;
95
+ content: Content[];
96
+ status?: string;
97
+ }
98
+ interface FunctionCallItem {
99
+ type: 'function_call';
100
+ id?: string;
101
+ call_id: string;
102
+ name: string;
103
+ arguments: string;
104
+ status?: string;
105
+ }
106
+ interface FunctionCallOutputItem {
107
+ type: 'function_call_output';
108
+ id?: string;
109
+ call_id: string;
110
+ output: string;
111
+ }
112
+ interface ReasoningItem {
113
+ type: 'reasoning';
114
+ id?: string;
115
+ content: TextContent[];
116
+ status?: string;
117
+ }
118
+ type Item = MessageItem | FunctionCallItem | FunctionCallOutputItem | ReasoningItem;
119
+ interface FunctionDefinition {
120
+ name: string;
121
+ description?: string;
122
+ parameters?: Record<string, unknown>;
123
+ }
124
+ interface Tool {
125
+ type: 'function';
126
+ function: FunctionDefinition;
127
+ }
128
+ /** Convenience builder for a function tool (mirrors Tool.function_tool). */
129
+ declare function functionTool(name: string, description?: string, parameters?: Record<string, unknown>): Tool;
130
+ interface Usage {
131
+ input_tokens: number;
132
+ output_tokens: number;
133
+ total_tokens: number;
134
+ input_tokens_details?: Record<string, number>;
135
+ output_tokens_details?: Record<string, number>;
136
+ cost_usd?: number;
137
+ }
138
+ interface ResponseError {
139
+ code: string;
140
+ message: string;
141
+ param?: string;
142
+ }
143
+ interface AuraMetadata {
144
+ request_id?: string;
145
+ model?: string;
146
+ provider?: string;
147
+ gateway_version?: string;
148
+ latency_ms?: number;
149
+ agentic?: Record<string, unknown>;
150
+ }
151
+ interface ResponseMetadata {
152
+ aura?: AuraMetadata;
153
+ }
154
+ interface Response {
155
+ id: string;
156
+ object: 'response';
157
+ created_at: number;
158
+ status: ResponseStatus;
159
+ model: string;
160
+ output: Item[];
161
+ usage?: Usage;
162
+ error?: ResponseError;
163
+ metadata?: ResponseMetadata;
164
+ previous_response_id?: string;
165
+ conversation_id?: string;
166
+ }
167
+ /** Text from the first assistant message output item ('' if none). */
168
+ declare function outputText(response: Response): string;
169
+ /** All function-call items in the output. */
170
+ declare function toolCalls(response: Response): FunctionCallItem[];
171
+ declare const hasToolCalls: (r: Response) => boolean;
172
+ declare const isComplete: (r: Response) => boolean;
173
+ declare const isFailed: (r: Response) => boolean;
174
+ interface StreamEventBase {
175
+ type: string;
176
+ sequence?: number;
177
+ }
178
+ interface ResponseCreatedEvent extends StreamEventBase {
179
+ type: 'response.created';
180
+ response: Response;
181
+ }
182
+ interface ResponseInProgressEvent extends StreamEventBase {
183
+ type: 'response.in_progress';
184
+ response: Response;
185
+ }
186
+ interface ResponseCompletedEvent extends StreamEventBase {
187
+ type: 'response.completed';
188
+ response: Response;
189
+ }
190
+ interface ResponseFailedEvent extends StreamEventBase {
191
+ type: 'response.failed';
192
+ response: Response;
193
+ }
194
+ interface OutputItemAddedEvent extends StreamEventBase {
195
+ type: 'response.output_item.added';
196
+ item: Item;
197
+ output_index: number;
198
+ }
199
+ interface OutputItemDoneEvent extends StreamEventBase {
200
+ type: 'response.output_item.done';
201
+ item: Item;
202
+ output_index: number;
203
+ }
204
+ interface TextDeltaEvent extends StreamEventBase {
205
+ type: 'response.output_text.delta';
206
+ delta: string;
207
+ output_index: number;
208
+ content_index: number;
209
+ }
210
+ interface TextDoneEvent extends StreamEventBase {
211
+ type: 'response.output_text.done';
212
+ text: string;
213
+ output_index: number;
214
+ content_index: number;
215
+ }
216
+ interface FunctionCallDeltaEvent extends StreamEventBase {
217
+ type: 'response.function_call.delta';
218
+ delta: string;
219
+ output_index: number;
220
+ call_id: string;
221
+ }
222
+ interface FunctionCallDoneEvent extends StreamEventBase {
223
+ type: 'response.function_call.done';
224
+ item: FunctionCallItem;
225
+ output_index: number;
226
+ }
227
+ interface ErrorEvent extends StreamEventBase {
228
+ type: 'error';
229
+ error: ResponseError;
230
+ }
231
+ type StreamEvent = ResponseCreatedEvent | ResponseInProgressEvent | ResponseCompletedEvent | ResponseFailedEvent | OutputItemAddedEvent | OutputItemDoneEvent | TextDeltaEvent | TextDoneEvent | FunctionCallDeltaEvent | FunctionCallDoneEvent | ErrorEvent;
232
+ /** All recognized stream event type strings. */
233
+ declare const STREAM_EVENT_TYPES: Set<string>;
234
+ interface InputMessage {
235
+ role: Role;
236
+ content: string | Content[];
237
+ }
238
+ declare const userMessage: (content: string) => InputMessage;
239
+ declare const assistantMessage: (content: string) => InputMessage;
240
+ declare const systemMessage: (content: string) => InputMessage;
241
+ interface CompressionConfig {
242
+ strategy?: string;
243
+ auto_select?: boolean;
244
+ [key: string]: unknown;
245
+ }
246
+ interface ValidationConfig {
247
+ strategy?: string;
248
+ n?: number;
249
+ min_confidence?: number;
250
+ [key: string]: unknown;
251
+ }
252
+ interface ConsistencyConfig {
253
+ style_profile?: string;
254
+ [key: string]: unknown;
255
+ }
256
+
257
+ /**
258
+ * AuraClient — the TypeScript client for the Aura LLM Gateway.
259
+ *
260
+ * Single client (no sync/async split — JS is async by default). Everything
261
+ * returns a Promise; `create({ stream: true })` returns an AsyncIterable of
262
+ * `StreamEvent`. Universal: uses global `fetch` + Web Streams, so it runs on
263
+ * Node 20+, browsers, Deno, Bun, and edge runtimes with no Node-only deps.
264
+ */
265
+
266
+ declare const DEFAULT_BASE_URL = "http://localhost:8080";
267
+ declare const DEFAULT_TIMEOUT = 60000;
268
+ declare const DEFAULT_MAX_RETRIES = 2;
269
+ type FetchFn = typeof fetch;
270
+ interface AuraClientOptions {
271
+ /** API key. Falls back to `AURA_API_KEY` env var (Node/Deno/Bun). */
272
+ apiKey?: string;
273
+ /** Gateway base URL. Falls back to `AURA_BASE_URL`, then localhost:8080. */
274
+ baseUrl?: string;
275
+ /** Per-request timeout in milliseconds (default 60000). */
276
+ timeout?: number;
277
+ /** Max retries for retryable failures (default 2; 0 disables). */
278
+ maxRetries?: number;
279
+ /** Extra headers merged into every request. */
280
+ headers?: Record<string, string>;
281
+ /** Custom fetch implementation (injection for edge runtimes / tests). */
282
+ fetch?: FetchFn;
283
+ /** Lifecycle hooks. */
284
+ onRequest?: (req: {
285
+ method: string;
286
+ url: string;
287
+ headers: Headers;
288
+ }) => void;
289
+ onResponse?: (res: {
290
+ status: number;
291
+ url: string;
292
+ }, durationMs: number) => void;
293
+ onError?: (err: AuraError) => void;
294
+ }
295
+ interface ResponseCreateParams {
296
+ model: string;
297
+ input: string | InputMessage[] | Array<Record<string, unknown>>;
298
+ instructions?: string;
299
+ tools?: Tool[];
300
+ tool_choice?: string;
301
+ temperature?: number;
302
+ max_tokens?: number;
303
+ top_p?: number;
304
+ previous_response_id?: string;
305
+ /** End-user identifier for multi-tenant cost tracking. */
306
+ user?: string;
307
+ compression?: CompressionConfig;
308
+ validation?: ValidationConfig;
309
+ consistency?: ConsistencyConfig;
310
+ /** Escape hatch for params not yet typed. */
311
+ [key: string]: unknown;
312
+ }
313
+ /** Responses API resource. */
314
+ declare class Responses {
315
+ private readonly client;
316
+ constructor(client: AuraClient);
317
+ /** Create a non-streaming response. */
318
+ create(params: ResponseCreateParams & {
319
+ stream?: false;
320
+ }): Promise<Response>;
321
+ /** Create a streaming response (AsyncIterable of events). */
322
+ create(params: ResponseCreateParams & {
323
+ stream: true;
324
+ }): Promise<AsyncIterable<StreamEvent>>;
325
+ private buildPayload;
326
+ }
327
+ declare class AuraClient {
328
+ readonly baseUrl: string;
329
+ readonly timeout: number;
330
+ readonly maxRetries: number;
331
+ readonly responses: Responses;
332
+ private readonly apiKey;
333
+ private readonly headers;
334
+ private readonly fetchFn;
335
+ private readonly opts;
336
+ constructor(options?: AuraClientOptions);
337
+ /** Build an absolute URL + Headers for a request. */
338
+ private prepare;
339
+ /** Exponential backoff with jitter, capped at 30s; honors Retry-After. */
340
+ private backoffMs;
341
+ /** Non-streaming JSON request with retry + typed error mapping. */
342
+ _request<T>(method: string, path: string, body?: unknown): Promise<T>;
343
+ /**
344
+ * Streaming request → AsyncIterable<StreamEvent>. Streams are NOT retried
345
+ * (re-issuing a partial stream would replay events); errors before the
346
+ * first byte still surface as typed errors.
347
+ */
348
+ _stream(path: string, body: unknown): Promise<AsyncIterable<StreamEvent>>;
349
+ /** Map a thrown fetch error to a typed AuraError, or null if not ours. */
350
+ private mapNetworkError;
351
+ }
352
+
353
+ /**
354
+ * Server-Sent Events parsing for the streaming Responses API.
355
+ *
356
+ * Consumes a `ReadableStream<Uint8Array>` (from `fetch` `response.body`) and
357
+ * yields typed `StreamEvent`s as an AsyncIterable — the idiomatic TS shape for
358
+ * `for await (const event of stream)`. Mirrors the Python SDK's
359
+ * `_parse_sse_stream` / `_parse_sse_event` / `_parse_event_data`.
360
+ */
361
+
362
+ /** Parse one SSE block ("event:" / "data:" lines) into a StreamEvent. */
363
+ declare function parseSSEChunk(chunk: string): StreamEvent | null;
364
+ /**
365
+ * Turn a byte stream of SSE into an AsyncIterable of events.
366
+ * Splits on the SSE record separator ("\n\n") and decodes incrementally,
367
+ * giving the caller full back-pressure (we only read as fast as consumed).
368
+ */
369
+ declare function parseSSE(body: ReadableStream<Uint8Array>): AsyncGenerator<StreamEvent, void, unknown>;
370
+
371
+ export { APIConnectionError, APIError, APITimeoutError, AuraClient, type AuraClientOptions, AuraError, type AuraErrorOptions, type AuraMetadata, AuthenticationError, BadRequestError, type CompressionConfig, type ConsistencyConfig, type Content, DEFAULT_BASE_URL, DEFAULT_MAX_RETRIES, DEFAULT_TIMEOUT, type ErrorEvent, type FunctionCallDeltaEvent, type FunctionCallDoneEvent, type FunctionCallItem, type FunctionCallOutputItem, type FunctionDefinition, type ImageContent, type InputMessage, type Item, type ItemType, type MessageItem, NotFoundError, type OutputItemAddedEvent, type OutputItemDoneEvent, RateLimitError, type ReasoningItem, type Response, type ResponseCompletedEvent, type ResponseCreateParams, type ResponseCreatedEvent, type ResponseError, type ResponseFailedEvent, type ResponseInProgressEvent, type ResponseMetadata, type ResponseStatus, Responses, type Role, STREAM_EVENT_TYPES, type StreamEvent, type TextContent, type TextDeltaEvent, type TextDoneEvent, type Tool, type Usage, type ValidationConfig, assistantMessage, functionTool, hasToolCalls, isComplete, isFailed, outputText, parseSSE, parseSSEChunk, systemMessage, toolCalls, userMessage };