segmind 0.5.0 → 0.6.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,258 @@
1
+ /**
2
+ * The wire vocabulary of a Segmind realtime session, as typed events.
3
+ *
4
+ * Mirrors Heimdall's `realtime/protocol.py`, session wire protocol v1.
5
+ * Control is JSON text frames both ways. Media may also travel as binary
6
+ * frames whose byte 0 names the stream (`0x01` audio, `0x02` video); the rest
7
+ * is that stream's payload in the codec `session.created.media` declares.
8
+ * The server sends audio as `audio.delta` JSON events.
9
+ */
10
+ /** The wire protocol version this client speaks (`session.created.protocol`). */
11
+ declare const PROTOCOL_VERSION = 1;
12
+ /** Byte 0 of a binary frame. Other values are reserved; the server refuses them. */
13
+ declare const STREAM_TAGS: {
14
+ readonly audio: 1;
15
+ readonly video: 2;
16
+ };
17
+ type SessionState = 'opening' | 'live' | 'closed';
18
+ /** Why a session ended. The server's vocabulary, plus three the client adds. */
19
+ type EndReason = 'client' | 'ceiling' | 'idle' | 'max_length' | 'balance_exhausted' | 'upstream_error' | 'server_shutdown' | 'internal_error' | 'metering_failed'
20
+ /**
21
+ * Close code 4008 with no `session.ended` frame and no close reason: one of
22
+ * the policy limits (ceiling, idle, max_length, balance_exhausted), which
23
+ * one is unknown.
24
+ */
25
+ | 'limit'
26
+ /** The socket closed without a `session.ended` frame. */
27
+ | 'connection_lost'
28
+ /**
29
+ * The session was refused before it opened (auth, credits, model, capacity,
30
+ * a dependency down) or spoke a protocol this client does not. The
31
+ * refusal's `code` and close code are on `SessionEnd`.
32
+ */
33
+ | 'refused';
34
+ /** Error codes the server refuses an open with, before a session exists. */
35
+ type RefusalCode = 'unauthorized' | 'token_expired' | 'token_replayed' | 'account_disabled' | 'auth_unavailable' | 'insufficient_credits' | 'unknown_model' | 'not_a_session_model' | 'model_deprecated' | 'bad_request' | 'capacity' | 'service_unavailable' | 'internal_error'
36
+ /** Client-side: `session.created.protocol` is not `PROTOCOL_VERSION`. */
37
+ | 'unsupported_protocol';
38
+ type TurnEndReason = 'completed' | 'interrupted' | 'error' | 'superseded' | 'session_closed';
39
+ /** One stream in `session.created.media`. */
40
+ interface MediaStreamSpec {
41
+ /** Byte 0 of a binary frame on this stream. */
42
+ tag: number;
43
+ /** `in` the client sends, `out` the server sends, `duplex` both. */
44
+ direction: 'in' | 'out' | 'duplex';
45
+ encoding: string;
46
+ rate?: number;
47
+ channels?: number;
48
+ mime?: string;
49
+ [key: string]: unknown;
50
+ }
51
+ /** `media.audio` as the client uses it: 16-bit little-endian PCM. */
52
+ interface AudioFormat extends MediaStreamSpec {
53
+ encoding: 'pcm_s16le';
54
+ rate: number;
55
+ channels: number;
56
+ }
57
+ interface SessionLimits {
58
+ max_session_seconds: number;
59
+ idle_seconds: number;
60
+ credit_ceiling: number;
61
+ slice_seconds: number;
62
+ minimum_charge?: number;
63
+ }
64
+ /**
65
+ * The measured facts a session is priced from. Which ones appear depends on
66
+ * the model; a voice session reports audio seconds.
67
+ */
68
+ interface UsageFacts {
69
+ input_audio_seconds?: number;
70
+ output_audio_seconds?: number;
71
+ input_tokens?: number;
72
+ output_tokens?: number;
73
+ output_video_seconds?: number;
74
+ text_inputs?: number;
75
+ [fact: string]: number | undefined;
76
+ }
77
+ interface Usage {
78
+ /** Credits moved off the balance so far. */
79
+ cost: number;
80
+ credits_confirmed: boolean;
81
+ /** Present only when the balance moved. */
82
+ remaining_credits?: number;
83
+ usage: UsageFacts;
84
+ sequence: number;
85
+ final: boolean;
86
+ }
87
+ interface SessionCreatedEvent {
88
+ type: 'session.created';
89
+ protocol: number;
90
+ session_id: string;
91
+ request_id: string;
92
+ model: string;
93
+ provider: string;
94
+ media: Record<string, MediaStreamSpec>;
95
+ limits: SessionLimits;
96
+ }
97
+ interface SessionUpdatedEvent {
98
+ type: 'session.updated';
99
+ session: Record<string, unknown>;
100
+ }
101
+ interface SessionEndedEvent {
102
+ type: 'session.ended';
103
+ reason: EndReason;
104
+ usage?: Usage;
105
+ message?: string;
106
+ }
107
+ interface InputSpeechStartedEvent {
108
+ type: 'input.speech_started';
109
+ }
110
+ interface InputSpeechStoppedEvent {
111
+ type: 'input.speech_stopped';
112
+ }
113
+ interface TurnStartedEvent {
114
+ type: 'turn.started';
115
+ turn_id: string;
116
+ input: 'text' | 'speech';
117
+ }
118
+ interface TurnEndedEvent {
119
+ type: 'turn.ended';
120
+ turn_id: string;
121
+ reason: TurnEndReason;
122
+ output_seconds: number;
123
+ time_to_first_byte?: number;
124
+ upstream_usage?: Record<string, unknown>;
125
+ }
126
+ interface AudioDeltaEvent {
127
+ type: 'audio.delta';
128
+ turn_id: string;
129
+ /** base64 of s16le PCM */
130
+ audio: string;
131
+ seconds: number;
132
+ }
133
+ interface TranscriptDeltaEvent {
134
+ type: 'transcript.delta';
135
+ turn_id: string;
136
+ role: 'assistant' | 'user';
137
+ text: string;
138
+ }
139
+ interface UsageEvent extends Usage {
140
+ type: 'segmind.usage';
141
+ }
142
+ interface ErrorEvent {
143
+ type: 'error';
144
+ code: string;
145
+ message: string;
146
+ }
147
+ interface UpstreamEvent {
148
+ type: 'upstream.event';
149
+ provider: string;
150
+ event: Record<string, unknown>;
151
+ }
152
+ interface PongEvent {
153
+ type: 'pong';
154
+ }
155
+ type ServerEvent = SessionCreatedEvent | SessionUpdatedEvent | SessionEndedEvent | InputSpeechStartedEvent | InputSpeechStoppedEvent | TurnStartedEvent | TurnEndedEvent | AudioDeltaEvent | TranscriptDeltaEvent | UsageEvent | ErrorEvent | UpstreamEvent | PongEvent;
156
+ type ServerEventType = ServerEvent['type'];
157
+ interface SessionUpdateParams {
158
+ voice?: string;
159
+ instructions?: string;
160
+ reasoning_effort?: string;
161
+ speed?: number;
162
+ vad?: boolean;
163
+ upstream_events?: boolean;
164
+ [key: string]: unknown;
165
+ }
166
+ type ClientEvent = {
167
+ type: 'session.update';
168
+ session: SessionUpdateParams;
169
+ } | {
170
+ type: 'audio.append';
171
+ audio: string;
172
+ } | {
173
+ type: 'audio.commit';
174
+ } | {
175
+ type: 'text';
176
+ text: string;
177
+ } | {
178
+ type: 'interrupt';
179
+ } | {
180
+ type: 'ping';
181
+ } | {
182
+ type: 'close';
183
+ };
184
+ /** What `POST /v1/realtime/token` returns. */
185
+ interface SessionToken {
186
+ token: string;
187
+ /** Unix seconds. */
188
+ expires_at?: number;
189
+ expires_in?: number;
190
+ model?: string;
191
+ credit_ceiling?: number;
192
+ }
193
+ /**
194
+ * How the host application obtains a session token. The client never sees
195
+ * an API key: the host's backend trades its key (or the signed-in user's
196
+ * session) for a short-lived token scoped to one model and a credit ceiling.
197
+ */
198
+ type SessionTokenProvider = (slug: string) => Promise<SessionToken | string>;
199
+
200
+ interface SegmindErrorOptions {
201
+ status?: number;
202
+ detail?: string;
203
+ requestId?: string;
204
+ body?: unknown;
205
+ cause?: unknown;
206
+ }
207
+ /** Base class of every error the SDK throws (except an `AbortSignal`'s own reason). */
208
+ declare class SegmindError extends Error {
209
+ /** HTTP status, when the error came from a response. */
210
+ readonly status: number | undefined;
211
+ /** The server's error text (`error` in the response body), when it sent one. */
212
+ readonly detail: string | undefined;
213
+ /** The v2 request id the error belongs to, when there is one. */
214
+ readonly requestId: string | undefined;
215
+ /** The parsed response body, or its text when it was not JSON. */
216
+ readonly body: unknown;
217
+ constructor(message: string, options?: SegmindErrorOptions);
218
+ /** Reads the response body and builds the error. Consumes the body. */
219
+ static fromResponse(response: Response, requestId?: string): Promise<SegmindError>;
220
+ }
221
+ /** The request never got a response: DNS, TLS, connection reset, or the client's timeout. */
222
+ declare class APIConnectionError extends SegmindError {
223
+ }
224
+ /**
225
+ * The account cannot pay for the request. The gateway reports this at submit, or
226
+ * as a failed job when the balance is positive but below the model's cost, so
227
+ * this one class covers both. `detail` holds the server's message.
228
+ */
229
+ declare class InsufficientCredits extends SegmindError {
230
+ }
231
+ /**
232
+ * Too many requests, after the SDK's own retries for calls that are safe to
233
+ * repeat. `retryAfterMs` is set when the server sent `Retry-After` and the
234
+ * runtime could read it: a browser only sees the header if the server exposes it.
235
+ */
236
+ declare class RateLimited extends SegmindError {
237
+ readonly retryAfterMs: number | undefined;
238
+ constructor(message: string, options?: SegmindErrorOptions & {
239
+ retryAfterMs?: number;
240
+ });
241
+ }
242
+ /** A v2 request reached `FAILED`. The server's error text is in `detail`. */
243
+ declare class InferenceFailed extends SegmindError {
244
+ /** The status body that reported the failure. */
245
+ readonly statusBody: Record<string, unknown>;
246
+ constructor(requestId: string, statusBody: Record<string, unknown>, detail?: string);
247
+ }
248
+ /**
249
+ * `wait()` ran past its timeout. The request may still finish on the server:
250
+ * `job.status()` or a new `wait()` picks it up again.
251
+ */
252
+ declare class InferenceTimeout extends SegmindError {
253
+ readonly elapsedMs: number;
254
+ constructor(requestId: string, elapsedMs: number);
255
+ }
256
+
257
+ export { APIConnectionError as A, InferenceFailed as I, PROTOCOL_VERSION as P, RateLimited as R, InferenceTimeout as b, InsufficientCredits as c, SegmindError as d, STREAM_TAGS as n };
258
+ export type { ClientEvent as C, EndReason as E, MediaStreamSpec as M, SessionToken as S, TranscriptDeltaEvent as T, UpstreamEvent as U, SessionTokenProvider as a, SessionCreatedEvent as e, AudioFormat as f, ServerEvent as g, AudioDeltaEvent as h, ErrorEvent as i, InputSpeechStartedEvent as j, InputSpeechStoppedEvent as k, PongEvent as l, RefusalCode as m, ServerEventType as o, SessionEndedEvent as p, SessionLimits as q, SessionState as r, SessionUpdateParams as s, SessionUpdatedEvent as t, TurnEndReason as u, TurnEndedEvent as v, TurnStartedEvent as w, Usage as x, UsageEvent as y, UsageFacts as z };