@hoodcompute/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,418 @@
1
+ /**
2
+ * Internal HTTP transport. Handles authentication headers, timeouts, automatic
3
+ * retries with exponential backoff, and error normalization. Not part of the
4
+ * public API surface.
5
+ * @internal
6
+ */
7
+ interface HttpClientOptions {
8
+ apiKey: string;
9
+ baseURL: string;
10
+ timeout: number;
11
+ maxRetries: number;
12
+ defaultHeaders?: Record<string, string>;
13
+ fetch?: typeof fetch;
14
+ }
15
+ interface RequestOptions {
16
+ method?: string;
17
+ path: string;
18
+ query?: Record<string, string | number | undefined>;
19
+ body?: unknown;
20
+ /** When true, the raw Response is returned without reading the body. */
21
+ stream?: boolean;
22
+ signal?: AbortSignal;
23
+ }
24
+ declare class HttpClient {
25
+ private readonly apiKey;
26
+ private readonly baseURL;
27
+ private readonly timeout;
28
+ private readonly maxRetries;
29
+ private readonly defaultHeaders;
30
+ private readonly fetchImpl;
31
+ constructor(options: HttpClientOptions);
32
+ /** Perform a request and return the parsed JSON body plus response headers. */
33
+ request<T>(options: RequestOptions): Promise<{
34
+ data: T;
35
+ response: Response;
36
+ }>;
37
+ /**
38
+ * Perform a request and return the raw Response. Used by the streaming path,
39
+ * which reads `response.body` directly. Errors are still parsed and thrown.
40
+ */
41
+ raw(options: RequestOptions): Promise<Response>;
42
+ private fetchWithTimeout;
43
+ private toError;
44
+ private buildUrl;
45
+ private buildHeaders;
46
+ private backoff;
47
+ }
48
+
49
+ /**
50
+ * Public type definitions for the HoodCompute SDK.
51
+ *
52
+ * The chat completion request and response shapes mirror the OpenAI Chat
53
+ * Completions API, so existing OpenAI integrations map over cleanly. Fields
54
+ * unique to HoodCompute (job IDs, on-chain settlement, credit accounting) are
55
+ * namespaced under `hoodcompute` or exposed as first-class receipt objects.
56
+ */
57
+ /** Pricing tier a model is grouped under. */
58
+ type ModelTier = "lite" | "standard" | "pro" | "max";
59
+ /** Role of a chat message. */
60
+ type ChatRole = "system" | "user" | "assistant";
61
+ /** A single message in a conversation. */
62
+ interface ChatMessage {
63
+ role: ChatRole;
64
+ content: string;
65
+ }
66
+ /** Parameters accepted by `client.chat.completions.create`. */
67
+ interface ChatCompletionCreateParams {
68
+ /** Model ID to run, for example `qwen3-8b`. See `client.models.list()`. */
69
+ model: string;
70
+ /** Ordered list of messages in the conversation. */
71
+ messages: ChatMessage[];
72
+ /** When `true`, tokens are streamed back as they are generated. */
73
+ stream?: boolean;
74
+ /** Maximum number of tokens to generate. */
75
+ max_tokens?: number;
76
+ /** Sampling temperature, 0.0 to 2.0. */
77
+ temperature?: number;
78
+ /** Nucleus sampling probability. */
79
+ top_p?: number;
80
+ /** Stop sequence or sequences. */
81
+ stop?: string | string[];
82
+ /** Penalize repeated tokens, -2.0 to 2.0. */
83
+ frequency_penalty?: number;
84
+ /** Penalize tokens that have already appeared, -2.0 to 2.0. */
85
+ presence_penalty?: number;
86
+ /** Seed for sampling. Not guaranteed under distributed inference. */
87
+ seed?: number;
88
+ }
89
+ /** Token accounting for a completed job. */
90
+ interface Usage {
91
+ prompt_tokens: number;
92
+ completion_tokens: number;
93
+ total_tokens: number;
94
+ }
95
+ /** A choice in a non-streaming chat completion. */
96
+ interface ChatCompletionChoice {
97
+ index: number;
98
+ message: ChatMessage;
99
+ finish_reason: string | null;
100
+ }
101
+ /**
102
+ * A non-streaming chat completion response.
103
+ *
104
+ * In addition to the OpenAI-compatible fields, HoodCompute attaches the job ID
105
+ * and the on-chain settlement transaction so a completion can be verified
106
+ * independently on Robinhood Chain.
107
+ */
108
+ interface ChatCompletion {
109
+ id: string;
110
+ object: "chat.completion";
111
+ created: number;
112
+ model: string;
113
+ choices: ChatCompletionChoice[];
114
+ usage: Usage;
115
+ /** HoodCompute job ID for this completion. */
116
+ jobId: string;
117
+ /** Robinhood Chain settlement transaction hash, once available. */
118
+ settlementTx: string | null;
119
+ /** Credits charged for this job. */
120
+ creditsCharged: number | null;
121
+ /** Credit balance remaining after this job. */
122
+ creditsRemaining: number | null;
123
+ }
124
+ /** The incremental content in a streaming chunk. */
125
+ interface ChatCompletionDelta {
126
+ role?: ChatRole;
127
+ content?: string;
128
+ }
129
+ /** A choice in a streaming chat completion chunk. */
130
+ interface ChatCompletionChunkChoice {
131
+ index: number;
132
+ delta: ChatCompletionDelta;
133
+ finish_reason: string | null;
134
+ }
135
+ /** A single Server-Sent Event chunk in a streaming completion. */
136
+ interface ChatCompletionChunk {
137
+ id: string;
138
+ object: "chat.completion.chunk";
139
+ created: number;
140
+ model: string;
141
+ choices: ChatCompletionChunkChoice[];
142
+ }
143
+ /**
144
+ * On-chain receipt for a settled job.
145
+ *
146
+ * Every settled job produces a receipt whose transaction hashes resolve on the
147
+ * Robinhood Chain Blockscout explorer. The receipt never contains prompt or
148
+ * completion content.
149
+ */
150
+ interface JobReceipt {
151
+ jobId: string;
152
+ model: string;
153
+ tier: ModelTier;
154
+ creditsCharged: number;
155
+ usdgValue: number;
156
+ workerAddress: string;
157
+ escrowTx: string;
158
+ settlementTx: string;
159
+ blockNumber: number;
160
+ proofHash: string;
161
+ }
162
+ /** Lifecycle status of a job. */
163
+ type JobStatus = "pending" | "processing" | "settling" | "settled" | "failed" | "refunded" | "disputed";
164
+ /** On-chain settlement detail attached to a retrieved job. */
165
+ interface JobOnChain {
166
+ escrowTx: string;
167
+ settlementTx: string;
168
+ escrowAddress: string;
169
+ blockNumber: number;
170
+ proofHash: string;
171
+ }
172
+ /** A job record retrieved from the Jobs API. */
173
+ interface Job {
174
+ id: string;
175
+ object: "job";
176
+ status: JobStatus;
177
+ model: string;
178
+ tier: ModelTier;
179
+ creditsCharged: number;
180
+ usdgValue: number;
181
+ workerAddress: string;
182
+ createdAt: string;
183
+ completedAt: string | null;
184
+ onChain: JobOnChain | null;
185
+ usage: Usage | null;
186
+ }
187
+ /** Parameters for `client.jobs.list`. */
188
+ interface JobListParams {
189
+ /** Number of results to return, up to 100. Defaults to 20. */
190
+ limit?: number;
191
+ /** Return jobs created before this job ID (cursor pagination). */
192
+ before?: string;
193
+ /** Return jobs created after this job ID. */
194
+ after?: string;
195
+ /** Filter by status. */
196
+ status?: JobStatus;
197
+ /** Filter by model ID. */
198
+ model?: string;
199
+ }
200
+ /** A page of jobs. */
201
+ interface JobList {
202
+ object: "list";
203
+ data: Job[];
204
+ hasMore: boolean;
205
+ nextCursor: string | null;
206
+ }
207
+ /** Result of opening a dispute on a job. */
208
+ interface DisputeResult {
209
+ jobId: string;
210
+ disputeOpened: boolean;
211
+ yourHash: string;
212
+ workerHash: string;
213
+ disputeTx: string | null;
214
+ arbitrationWindowHours: number | null;
215
+ }
216
+ /** HoodCompute-specific metadata attached to a model. */
217
+ interface ModelMeta {
218
+ tier: ModelTier;
219
+ creditsPerRequest: number;
220
+ creditsPer1kTokens: number;
221
+ activeWorkers: number;
222
+ medianLatencyMs: number;
223
+ parameters: string;
224
+ contextWindow: number;
225
+ }
226
+ /** A model available on the network. */
227
+ interface Model {
228
+ id: string;
229
+ object: "model";
230
+ created: number;
231
+ ownedBy: string;
232
+ hoodcompute: ModelMeta;
233
+ }
234
+ /** A page of models. */
235
+ interface ModelList {
236
+ object: "list";
237
+ data: Model[];
238
+ }
239
+ /** Account and credit balance for the authenticated key. */
240
+ interface Account {
241
+ wallet: string;
242
+ creditsRemaining: number;
243
+ usdgValue: number;
244
+ lastTopupAt: string | null;
245
+ apiKeyCreatedAt: string | null;
246
+ }
247
+
248
+ /**
249
+ * Server-Sent Events parsing and the streaming chat completion object.
250
+ */
251
+
252
+ /**
253
+ * Lightweight settlement metadata surfaced from a streaming response once it
254
+ * closes. Values are read from response headers as the network makes them
255
+ * available. For the full on-chain receipt, call `client.jobs.getReceipt(jobId)`.
256
+ */
257
+ interface StreamReceipt {
258
+ jobId: string | null;
259
+ settlementTx: string | null;
260
+ escrowTx: string | null;
261
+ workerAddress: string | null;
262
+ creditsRemaining: number | null;
263
+ }
264
+ /**
265
+ * An async-iterable stream of chat completion chunks.
266
+ *
267
+ * Iterate it with `for await` to consume tokens as they arrive. Once iteration
268
+ * finishes, `jobId` and `receipt` are populated from the response.
269
+ *
270
+ * @example
271
+ * const stream = await client.chat.completions.create({ ..., stream: true })
272
+ * for await (const chunk of stream) {
273
+ * process.stdout.write(chunk.choices[0]?.delta?.content ?? "")
274
+ * }
275
+ * console.log(stream.receipt.settlementTx)
276
+ */
277
+ declare class ChatCompletionStream implements AsyncIterable<ChatCompletionChunk> {
278
+ /** HoodCompute job ID for this completion, from the response header. */
279
+ jobId: string | null;
280
+ /** Settlement metadata, populated once the stream finishes. */
281
+ receipt: StreamReceipt;
282
+ private readonly response;
283
+ constructor(response: Response);
284
+ [Symbol.asyncIterator](): AsyncIterator<ChatCompletionChunk>;
285
+ /** Convenience: collect the full concatenated text of the stream. */
286
+ text(): Promise<string>;
287
+ private readReceipt;
288
+ }
289
+
290
+ /**
291
+ * Chat completions. OpenAI-compatible in request and response shape, with the
292
+ * job ID and on-chain settlement attached to every result.
293
+ */
294
+
295
+ declare class Completions {
296
+ private readonly http;
297
+ constructor(http: HttpClient);
298
+ /**
299
+ * Create a chat completion.
300
+ *
301
+ * Pass `stream: true` to receive a {@link ChatCompletionStream}. Otherwise a
302
+ * fully resolved {@link ChatCompletion} is returned.
303
+ */
304
+ create(params: ChatCompletionCreateParams & {
305
+ stream: true;
306
+ }, options?: {
307
+ signal?: AbortSignal;
308
+ }): Promise<ChatCompletionStream>;
309
+ create(params: ChatCompletionCreateParams & {
310
+ stream?: false;
311
+ }, options?: {
312
+ signal?: AbortSignal;
313
+ }): Promise<ChatCompletion>;
314
+ create(params: ChatCompletionCreateParams, options?: {
315
+ signal?: AbortSignal;
316
+ }): Promise<ChatCompletion | ChatCompletionStream>;
317
+ }
318
+ declare class Chat {
319
+ readonly completions: Completions;
320
+ constructor(http: HttpClient);
321
+ }
322
+
323
+ /**
324
+ * Models. Reflects the live worker pool: a model is listed only while at least
325
+ * one worker is hosting it.
326
+ */
327
+
328
+ declare class Models {
329
+ private readonly http;
330
+ constructor(http: HttpClient);
331
+ /** List every model currently available on the network. */
332
+ list(options?: {
333
+ signal?: AbortSignal;
334
+ }): Promise<ModelList>;
335
+ /** Retrieve a single model by ID, including its live worker count. */
336
+ retrieve(modelId: string, options?: {
337
+ signal?: AbortSignal;
338
+ }): Promise<Model>;
339
+ }
340
+
341
+ /**
342
+ * Jobs. Retrieve job status, fetch on-chain receipts, and open disputes.
343
+ */
344
+
345
+ declare class Jobs {
346
+ private readonly http;
347
+ constructor(http: HttpClient);
348
+ /** Retrieve a job by ID, including its on-chain settlement detail. */
349
+ get(jobId: string, options?: {
350
+ signal?: AbortSignal;
351
+ }): Promise<Job>;
352
+ /** List jobs for the authenticated key, most recent first. */
353
+ list(params?: JobListParams, options?: {
354
+ signal?: AbortSignal;
355
+ }): Promise<JobList>;
356
+ /**
357
+ * Fetch the on-chain receipt for a settled job. Throws if the job exists but
358
+ * has not settled yet.
359
+ */
360
+ getReceipt(jobId: string, options?: {
361
+ signal?: AbortSignal;
362
+ }): Promise<JobReceipt>;
363
+ /**
364
+ * Open a dispute on a completed job. Must be called within 60 seconds of
365
+ * receiving the final output token.
366
+ *
367
+ * @param receivedOutputHash SHA-256 of the full response text you received,
368
+ * formatted as `sha256:<hex>`.
369
+ */
370
+ dispute(jobId: string, receivedOutputHash: string, options?: {
371
+ signal?: AbortSignal;
372
+ }): Promise<DisputeResult>;
373
+ }
374
+
375
+ /**
376
+ * Account. The wallet and credit balance tied to the authenticated API key.
377
+ */
378
+
379
+ declare class AccountResource {
380
+ private readonly http;
381
+ constructor(http: HttpClient);
382
+ /** Retrieve the current account, including the live credit balance. */
383
+ get(options?: {
384
+ signal?: AbortSignal;
385
+ }): Promise<Account>;
386
+ }
387
+
388
+ /**
389
+ * The HoodCompute client. Entry point for chat completions, models, jobs, and
390
+ * account access against the decentralized inference network.
391
+ */
392
+
393
+ interface HoodComputeClientOptions {
394
+ /**
395
+ * API key, formatted `hoodc_live_...`. Falls back to the
396
+ * `HOODCOMPUTE_API_KEY` environment variable when omitted.
397
+ */
398
+ apiKey?: string;
399
+ /** API base URL. Defaults to `https://api.hoodcompute.com/v1`. */
400
+ baseURL?: string;
401
+ /** Per-request timeout in milliseconds. Defaults to 120000. */
402
+ timeout?: number;
403
+ /** Automatic retries on 5xx, 429, and connection failures. Defaults to 2. */
404
+ maxRetries?: number;
405
+ /** Headers added to every request. */
406
+ defaultHeaders?: Record<string, string>;
407
+ /** Custom fetch implementation. Defaults to the global `fetch`. */
408
+ fetch?: typeof fetch;
409
+ }
410
+ declare class HoodComputeClient {
411
+ readonly chat: Chat;
412
+ readonly models: Models;
413
+ readonly jobs: Jobs;
414
+ readonly account: AccountResource;
415
+ constructor(options?: HoodComputeClientOptions);
416
+ }
417
+
418
+ export { type Account as A, type ChatCompletion as C, type DisputeResult as D, HoodComputeClient as H, type Job as J, type ModelTier as M, type StreamReceipt as S, type Usage as U, type ChatCompletionChoice as a, type ChatCompletionChunk as b, type ChatCompletionChunkChoice as c, type ChatCompletionCreateParams as d, type ChatCompletionDelta as e, ChatCompletionStream as f, type ChatMessage as g, type ChatRole as h, type HoodComputeClientOptions as i, type JobList as j, type JobListParams as k, type JobOnChain as l, type JobReceipt as m, type JobStatus as n, type Model as o, type ModelList as p, type ModelMeta as q };