segmind 0.0.1 → 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,362 @@
1
+ /** Model inputs, forwarded to the model exactly as given. */
2
+ type Params = Record<string, unknown>;
3
+ /** A v2 request's state. */
4
+ type JobState = 'QUEUED' | 'PROCESSING' | 'COMPLETED' | 'FAILED';
5
+ /**
6
+ * The body of a finished v2 request. Every model returns `status`, `output`
7
+ * and `metrics`; the rest is model-specific, so do not assume a fixed key set.
8
+ */
9
+ interface RunResult {
10
+ status?: JobState | string;
11
+ output?: unknown;
12
+ request_id?: string;
13
+ metrics?: Record<string, unknown>;
14
+ [key: string]: unknown;
15
+ }
16
+ /** `GET /v2/requests/{id}/status`. */
17
+ interface JobStatus {
18
+ status: JobState | string;
19
+ request_id?: string;
20
+ status_url?: string;
21
+ response_url?: string;
22
+ metrics?: Record<string, unknown>;
23
+ model?: string;
24
+ error?: string;
25
+ retry_count?: number;
26
+ [key: string]: unknown;
27
+ }
28
+ /** `POST /v2/{slug}` (also the body of a 409 for a repeated `X-Request-ID`). */
29
+ interface SubmitResponse {
30
+ request_id: string;
31
+ status: JobState | string;
32
+ status_url: string;
33
+ response_url: string;
34
+ poll_url?: string;
35
+ [key: string]: unknown;
36
+ }
37
+ interface CallOptions {
38
+ /** Cancels the call. */
39
+ signal?: AbortSignal;
40
+ /** Milliseconds per HTTP request; defaults to the client's `timeout`. */
41
+ timeout?: number;
42
+ }
43
+ interface SubmitOptions {
44
+ signal?: AbortSignal;
45
+ /**
46
+ * Idempotency key sent as `X-Request-ID` ([A-Za-z0-9_-]{8,128}). Submitting
47
+ * again with the same id returns the existing request instead of a new one.
48
+ * Generated when omitted.
49
+ */
50
+ requestId?: string;
51
+ }
52
+ interface WaitOptions {
53
+ signal?: AbortSignal;
54
+ /** Give up after this many milliseconds. Default 600 000 (10 minutes). */
55
+ timeout?: number;
56
+ /** Milliseconds between status checks. Default 1000. */
57
+ interval?: number;
58
+ }
59
+ type RunOptions = SubmitOptions & WaitOptions;
60
+
61
+ /** An OpenAI-style chat message; content may be text or a list of parts. */
62
+ interface ChatMessage {
63
+ role: 'system' | 'developer' | 'user' | 'assistant' | 'tool' | (string & {});
64
+ content: string | Array<Record<string, unknown>>;
65
+ [key: string]: unknown;
66
+ }
67
+ /** Chat inputs: `messages` or `prompt` (at least one), plus any model params. */
68
+ type ChatParams = Params & {
69
+ messages?: ChatMessage[];
70
+ prompt?: string;
71
+ };
72
+ type Raw = Record<string, unknown>;
73
+ /**
74
+ * One reply from any chat model. The gateway returns each provider's own JSON
75
+ * (OpenAI, Anthropic or Gemini shape); this reads the common fields from all
76
+ * three. `raw` is always the provider's body, untouched.
77
+ */
78
+ declare class ChatResponse {
79
+ readonly raw: Raw;
80
+ constructor(raw: Raw);
81
+ /** The assistant's text. Throws when the body holds none (inspect `raw`). */
82
+ get text(): string;
83
+ /** `text` parsed as JSON, for JSON mode and structured outputs. */
84
+ json<T = unknown>(): T;
85
+ /** The provider's token usage (`usage` or Gemini's `usageMetadata`). */
86
+ get usage(): Record<string, unknown> | undefined;
87
+ /** Why generation stopped (`finish_reason`, `stop_reason` or `finishReason`). */
88
+ get finishReason(): string | undefined;
89
+ /** Tool calls in the OpenAI shape, when the model made any. */
90
+ get toolCalls(): unknown[];
91
+ }
92
+
93
+ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
94
+ interface HttpConfig {
95
+ apiKey: string | undefined;
96
+ fetch: typeof fetch;
97
+ timeout: number;
98
+ maxRetries: number;
99
+ headers: Record<string, string>;
100
+ }
101
+ interface HttpRequest {
102
+ method: HttpMethod;
103
+ url: string;
104
+ /** Sent as JSON. */
105
+ body?: unknown;
106
+ headers?: Record<string, string>;
107
+ signal?: AbortSignal;
108
+ /** Milliseconds for this attempt; defaults to the client's timeout. */
109
+ timeout?: number;
110
+ /**
111
+ * Retry connection errors and retryable statuses. Only for requests that
112
+ * cannot bill twice: reads, polls, and v2 submits that carry an
113
+ * `X-Request-ID` (the gateway answers a repeat with 409, not a second job).
114
+ */
115
+ retry?: boolean;
116
+ }
117
+ declare class HttpClient {
118
+ readonly config: HttpConfig;
119
+ constructor(config: HttpConfig);
120
+ /** The response whatever its status, after retries when `retry` is set. */
121
+ fetchRaw(req: HttpRequest): Promise<Response>;
122
+ /** The response, or a `SegmindError` for any status outside 2xx. */
123
+ request(req: HttpRequest): Promise<Response>;
124
+ /** The parsed JSON body of a 2xx response. */
125
+ json<T>(req: HttpRequest): Promise<T>;
126
+ private attempt;
127
+ }
128
+
129
+ declare const DEFAULT_WAIT_TIMEOUT_MS = 600000;
130
+ declare const DEFAULT_WAIT_INTERVAL_MS = 1000;
131
+ /**
132
+ * A submitted v2 request. `wait()` polls until it finishes; `status()` and
133
+ * `result()` read it once.
134
+ */
135
+ declare class Job<T = RunResult> {
136
+ private readonly http;
137
+ /** The body the submit returned. */
138
+ readonly submitResponse: SubmitResponse;
139
+ private readonly transform;
140
+ readonly requestId: string;
141
+ readonly statusUrl: string;
142
+ readonly responseUrl: string;
143
+ constructor(http: HttpClient,
144
+ /** The body the submit returned. */
145
+ submitResponse: SubmitResponse, transform?: (body: RunResult) => T);
146
+ /** The current state, without waiting. */
147
+ status(options?: {
148
+ signal?: AbortSignal;
149
+ }): Promise<JobStatus>;
150
+ /**
151
+ * The finished result. Throws `InferenceFailed` when the request failed, and
152
+ * a `SegmindError` when it has not finished yet (use `wait()` for that).
153
+ */
154
+ result(options?: {
155
+ signal?: AbortSignal;
156
+ }): Promise<T>;
157
+ /** Polls until the request finishes, then returns its result. */
158
+ wait(options?: WaitOptions): Promise<T>;
159
+ /**
160
+ * The gateway serves a FAILED request's body with HTTP 422 on both the status
161
+ * and result URLs. A body that names a terminal state is the answer whatever
162
+ * its status code; anything else outside 2xx is a real error.
163
+ */
164
+ private read;
165
+ }
166
+
167
+ declare const DEFAULT_BASE_URL = "https://api.segmind.com";
168
+ declare const DEFAULT_SPOT_URL = "https://api.spotprod.segmind.com";
169
+ declare const DEFAULT_CONSOLE_URL = "https://cloud.segmind.com/api";
170
+ interface ClientOptions {
171
+ /** Your Segmind API key. Defaults to `SEGMIND_API_KEY` from the environment. */
172
+ apiKey?: string;
173
+ /** The inference API root, without `/v1`. Defaults to `SEGMIND_BASE_URL` or https://api.segmind.com. */
174
+ baseURL?: string;
175
+ /** The catalog and history API. Default https://api.spotprod.segmind.com. */
176
+ spotURL?: string;
177
+ /** The account API. Default https://cloud.segmind.com/api. */
178
+ consoleURL?: string;
179
+ /** Milliseconds per HTTP request. Default 600 000. */
180
+ timeout?: number;
181
+ /** Retries for requests that cannot bill twice. Default 2. */
182
+ maxRetries?: number;
183
+ /** A `fetch` implementation. Defaults to the global one. */
184
+ fetch?: typeof fetch;
185
+ /** Headers added to every request. */
186
+ defaultHeaders?: Record<string, string>;
187
+ /**
188
+ * Allow an API key in a browser or React Native app. Anyone who can open the
189
+ * app can read the key and spend your credits. Prefer calling your own
190
+ * backend, or realtime session tokens.
191
+ */
192
+ dangerouslyAllowBrowser?: boolean;
193
+ }
194
+ /**
195
+ * The Segmind API client.
196
+ *
197
+ * ```ts
198
+ * const segmind = new Segmind(); // reads SEGMIND_API_KEY
199
+ * const result = await segmind.run('seedream-4.5', { prompt: 'A sunset over mountains' });
200
+ * ```
201
+ */
202
+ declare class Segmind {
203
+ readonly baseURL: string;
204
+ readonly spotURL: string;
205
+ readonly consoleURL: string;
206
+ readonly models: Models;
207
+ readonly accounts: Accounts;
208
+ /** @internal */
209
+ readonly http: HttpClient;
210
+ constructor(options?: ClientOptions);
211
+ /**
212
+ * Runs a model and returns its result: submits to the v2 queue, then polls
213
+ * until it finishes. `params` go to the model unchanged.
214
+ */
215
+ run(slug: string, params?: Params, options?: RunOptions): Promise<RunResult>;
216
+ /**
217
+ * One blocking `POST /v1/{slug}`. Returns the fetch `Response`: call
218
+ * `.arrayBuffer()` for media or `.json()` for JSON models. Never retried,
219
+ * because the call bills.
220
+ */
221
+ runSync(slug: string, params?: Params, options?: CallOptions): Promise<Response>;
222
+ /**
223
+ * Submits to the v2 queue and returns a `Job` to wait on. Safe to retry: the
224
+ * submit carries an `X-Request-ID`, and the gateway answers a repeat with
225
+ * the existing request rather than a second one.
226
+ */
227
+ submit(slug: string, params?: Params, options?: SubmitOptions): Promise<Job>;
228
+ /** Chat with an LLM through the v2 queue. Returns a provider-neutral reply. */
229
+ chat(slug: string, params: ChatParams, options?: RunOptions): Promise<ChatResponse>;
230
+ /** Chat with one blocking `POST /v1/{slug}`. */
231
+ chatSync(slug: string, params: ChatParams, options?: CallOptions): Promise<ChatResponse>;
232
+ /** Submits a chat to the v2 queue and returns a `Job` whose result is a `ChatResponse`. */
233
+ submitChat(slug: string, params: ChatParams, options?: SubmitOptions): Promise<Job<ChatResponse>>;
234
+ private submitJob;
235
+ }
236
+ /** One catalog entry. Only the commonly used fields are typed. */
237
+ interface ModelInfo {
238
+ slug: string;
239
+ title?: string;
240
+ type?: string;
241
+ description?: string;
242
+ is_visible?: boolean;
243
+ is_depreciated?: boolean;
244
+ is_new?: boolean;
245
+ coming_soon?: boolean;
246
+ parameters_schema?: Record<string, unknown>;
247
+ [key: string]: unknown;
248
+ }
249
+ /** The catalog response, as the API returns it. */
250
+ interface ModelsListResponse {
251
+ 'Inference Model Information List': ModelInfo[];
252
+ [key: string]: unknown;
253
+ }
254
+ /** The model catalog. */
255
+ declare class Models {
256
+ private readonly client;
257
+ constructor(client: Segmind);
258
+ /** Every model in the catalog, under the key `Inference Model Information List`. */
259
+ list(options?: {
260
+ signal?: AbortSignal;
261
+ }): Promise<ModelsListResponse>;
262
+ }
263
+ /** The account the API key belongs to. */
264
+ declare class Accounts {
265
+ private readonly client;
266
+ constructor(client: Segmind);
267
+ /** Account details for the API key. */
268
+ current(options?: {
269
+ signal?: AbortSignal;
270
+ }): Promise<Record<string, unknown>>;
271
+ }
272
+
273
+ /** An OpenAI-style image part, for the `content` list of a chat message. */
274
+ interface ImageUrlPart {
275
+ type: 'image_url';
276
+ image_url: {
277
+ url: string;
278
+ };
279
+ }
280
+ type ImageInput = string | Blob | ArrayBuffer | Uint8Array;
281
+ /**
282
+ * Builds an image part for a chat message.
283
+ *
284
+ * - An `http(s)://` or `data:` URL passes through.
285
+ * - A file path (Node only) is read and inlined as a base64 `data:` URI.
286
+ * - A `Blob`, `ArrayBuffer` or `Uint8Array` is inlined; pass `mimeType` when
287
+ * it cannot be told from the blob or the bytes.
288
+ *
289
+ * Inlining is the safer route: the gateway cannot fetch URLs on every host.
290
+ */
291
+ declare function imageUrl(input: ImageInput, options?: {
292
+ mimeType?: string;
293
+ }): Promise<ImageUrlPart>;
294
+
295
+ interface SegmindErrorOptions {
296
+ status?: number;
297
+ detail?: string;
298
+ requestId?: string;
299
+ body?: unknown;
300
+ cause?: unknown;
301
+ }
302
+ /** Base class of every error the SDK throws (except an `AbortSignal`'s own reason). */
303
+ declare class SegmindError extends Error {
304
+ /** HTTP status, when the error came from a response. */
305
+ readonly status: number | undefined;
306
+ /** The server's error text (`error` in the response body), when it sent one. */
307
+ readonly detail: string | undefined;
308
+ /** The v2 request id the error belongs to, when there is one. */
309
+ readonly requestId: string | undefined;
310
+ /** The parsed response body, or its text when it was not JSON. */
311
+ readonly body: unknown;
312
+ constructor(message: string, options?: SegmindErrorOptions);
313
+ /** Reads the response body and builds the error. Consumes the body. */
314
+ static fromResponse(response: Response, requestId?: string): Promise<SegmindError>;
315
+ }
316
+ /** The request never got a response: DNS, TLS, connection reset, or the client's timeout. */
317
+ declare class APIConnectionError extends SegmindError {
318
+ }
319
+ /** A v2 request reached `FAILED`. The server's error text is in `detail`. */
320
+ declare class InferenceFailed extends SegmindError {
321
+ /** The status body that reported the failure. */
322
+ readonly statusBody: Record<string, unknown>;
323
+ constructor(requestId: string, statusBody: Record<string, unknown>);
324
+ }
325
+ /**
326
+ * `wait()` ran past its timeout. The request may still finish on the server:
327
+ * `job.status()` or a new `wait()` picks it up again.
328
+ */
329
+ declare class InferenceTimeout extends SegmindError {
330
+ readonly elapsedMs: number;
331
+ constructor(requestId: string, elapsedMs: number);
332
+ }
333
+
334
+ declare const VERSION = "0.1.0";
335
+
336
+ /** `new Segmind().run(...)` with the key from `SEGMIND_API_KEY`. */
337
+ declare const run: (slug: string, params?: Params, options?: RunOptions) => Promise<RunResult>;
338
+ /** `new Segmind().runSync(...)` with the key from `SEGMIND_API_KEY`. */
339
+ declare const runSync: (slug: string, params?: Params, options?: CallOptions) => Promise<Response>;
340
+ /** `new Segmind().submit(...)` with the key from `SEGMIND_API_KEY`. */
341
+ declare const submit: (slug: string, params?: Params, options?: SubmitOptions) => Promise<Job>;
342
+ /** `new Segmind().chat(...)` with the key from `SEGMIND_API_KEY`. */
343
+ declare const chat: (slug: string, params: ChatParams, options?: RunOptions) => Promise<ChatResponse>;
344
+ /** `new Segmind().chatSync(...)` with the key from `SEGMIND_API_KEY`. */
345
+ declare const chatSync: (slug: string, params: ChatParams, options?: CallOptions) => Promise<ChatResponse>;
346
+ /** `new Segmind().submitChat(...)` with the key from `SEGMIND_API_KEY`. */
347
+ declare const submitChat: (slug: string, params: ChatParams, options?: SubmitOptions) => Promise<Job<ChatResponse>>;
348
+ /** `new Segmind().models` with the key from `SEGMIND_API_KEY`. */
349
+ declare const models: {
350
+ list: (options?: {
351
+ signal?: AbortSignal;
352
+ }) => Promise<ModelsListResponse>;
353
+ };
354
+ /** `new Segmind().accounts` with the key from `SEGMIND_API_KEY`. */
355
+ declare const accounts: {
356
+ current: (options?: {
357
+ signal?: AbortSignal;
358
+ }) => Promise<Record<string, unknown>>;
359
+ };
360
+
361
+ export { APIConnectionError, Accounts, ChatResponse, DEFAULT_BASE_URL, DEFAULT_CONSOLE_URL, DEFAULT_SPOT_URL, DEFAULT_WAIT_INTERVAL_MS, DEFAULT_WAIT_TIMEOUT_MS, InferenceFailed, InferenceTimeout, Job, Models, Segmind, SegmindError, VERSION, accounts, chat, chatSync, imageUrl, models, run, runSync, submit, submitChat };
362
+ export type { CallOptions, ChatMessage, ChatParams, ClientOptions, ImageInput, ImageUrlPart, JobState, JobStatus, ModelInfo, ModelsListResponse, Params, RunOptions, RunResult, SubmitOptions, SubmitResponse, WaitOptions };