@tryhakim/voice 1.0.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.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +188 -0
  3. package/dist/audio/speech-stream-ws.d.ts +40 -0
  4. package/dist/audio/speech-stream-ws.d.ts.map +1 -0
  5. package/dist/audio/speech-stream-ws.js +565 -0
  6. package/dist/audio/speech-stream-ws.js.map +1 -0
  7. package/dist/audio/speech.d.ts +46 -0
  8. package/dist/audio/speech.d.ts.map +1 -0
  9. package/dist/audio/speech.js +125 -0
  10. package/dist/audio/speech.js.map +1 -0
  11. package/dist/audio/stream.d.ts +38 -0
  12. package/dist/audio/stream.d.ts.map +1 -0
  13. package/dist/audio/stream.js +452 -0
  14. package/dist/audio/stream.js.map +1 -0
  15. package/dist/audio/to-blob.d.ts +10 -0
  16. package/dist/audio/to-blob.d.ts.map +1 -0
  17. package/dist/audio/to-blob.js +78 -0
  18. package/dist/audio/to-blob.js.map +1 -0
  19. package/dist/audio/transcriptions.d.ts +35 -0
  20. package/dist/audio/transcriptions.d.ts.map +1 -0
  21. package/dist/audio/transcriptions.js +105 -0
  22. package/dist/audio/transcriptions.js.map +1 -0
  23. package/dist/audio/translate-stream-ws.d.ts +37 -0
  24. package/dist/audio/translate-stream-ws.d.ts.map +1 -0
  25. package/dist/audio/translate-stream-ws.js +665 -0
  26. package/dist/audio/translate-stream-ws.js.map +1 -0
  27. package/dist/audio/translate.d.ts +36 -0
  28. package/dist/audio/translate.d.ts.map +1 -0
  29. package/dist/audio/translate.js +39 -0
  30. package/dist/audio/translate.js.map +1 -0
  31. package/dist/audio/voices.d.ts +47 -0
  32. package/dist/audio/voices.d.ts.map +1 -0
  33. package/dist/audio/voices.js +91 -0
  34. package/dist/audio/voices.js.map +1 -0
  35. package/dist/chat/completions.d.ts +76 -0
  36. package/dist/chat/completions.d.ts.map +1 -0
  37. package/dist/chat/completions.js +114 -0
  38. package/dist/chat/completions.js.map +1 -0
  39. package/dist/chat/index.d.ts +2 -0
  40. package/dist/chat/index.d.ts.map +1 -0
  41. package/dist/chat/index.js +2 -0
  42. package/dist/chat/index.js.map +1 -0
  43. package/dist/chat/sse.d.ts +12 -0
  44. package/dist/chat/sse.d.ts.map +1 -0
  45. package/dist/chat/sse.js +142 -0
  46. package/dist/chat/sse.js.map +1 -0
  47. package/dist/client.d.ts +63 -0
  48. package/dist/client.d.ts.map +1 -0
  49. package/dist/client.js +80 -0
  50. package/dist/client.js.map +1 -0
  51. package/dist/errors.d.ts +99 -0
  52. package/dist/errors.d.ts.map +1 -0
  53. package/dist/errors.js +158 -0
  54. package/dist/errors.js.map +1 -0
  55. package/dist/index.d.ts +27 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +25 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/jobs.d.ts +34 -0
  60. package/dist/jobs.d.ts.map +1 -0
  61. package/dist/jobs.js +62 -0
  62. package/dist/jobs.js.map +1 -0
  63. package/dist/observability.d.ts +28 -0
  64. package/dist/observability.d.ts.map +1 -0
  65. package/dist/observability.js +107 -0
  66. package/dist/observability.js.map +1 -0
  67. package/dist/settings.d.ts +49 -0
  68. package/dist/settings.d.ts.map +1 -0
  69. package/dist/settings.js +84 -0
  70. package/dist/settings.js.map +1 -0
  71. package/dist/transport.d.ts +82 -0
  72. package/dist/transport.d.ts.map +1 -0
  73. package/dist/transport.js +289 -0
  74. package/dist/transport.js.map +1 -0
  75. package/dist/types.d.ts +969 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +11 -0
  78. package/dist/types.js.map +1 -0
  79. package/dist/usage.d.ts +43 -0
  80. package/dist/usage.d.ts.map +1 -0
  81. package/dist/usage.js +85 -0
  82. package/dist/usage.js.map +1 -0
  83. package/dist/version.d.ts +13 -0
  84. package/dist/version.d.ts.map +1 -0
  85. package/dist/version.js +13 -0
  86. package/dist/version.js.map +1 -0
  87. package/dist/webhooks.d.ts +84 -0
  88. package/dist/webhooks.d.ts.map +1 -0
  89. package/dist/webhooks.js +157 -0
  90. package/dist/webhooks.js.map +1 -0
  91. package/package.json +57 -0
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Server-Sent Events (`text/event-stream`) parser for the chat
3
+ * completions streaming surface.
4
+ *
5
+ * Splits a `ReadableStream<Uint8Array>` from `fetch` into discrete
6
+ * SSE events (delimited by `\n\n`), then for each event:
7
+ *
8
+ * - skips heartbeat comments (`: …`)
9
+ * - parses the `data: …` payload into the caller-supplied chunk
10
+ * shape (`ChatCompletionChunk` in practice)
11
+ * - terminates on `data: [DONE]`
12
+ * - surfaces `event: error\ndata: {…}` payloads as a thrown
13
+ * `HakimError` so a mid-stream upstream failure doesn't get
14
+ * swallowed by the iterator
15
+ *
16
+ * Kept generic on the chunk type so we can reuse it for any future
17
+ * SSE surface (e.g. a server-events stream on a webhook console)
18
+ * without copy-paste.
19
+ */
20
+ import { errorFromPayload, HakimError } from '../errors.js';
21
+ /**
22
+ * Iterate the SSE wire and yield parsed chunks of type `T`.
23
+ *
24
+ * Terminates cleanly on `data: [DONE]` or when the underlying
25
+ * `ReadableStream` closes. Throws when the wire carries an
26
+ * `event: error` envelope whose `data` is a Hakim error JSON
27
+ * payload.
28
+ */
29
+ export async function* parseSseStream(body, opts = {}) {
30
+ const reader = body.getReader();
31
+ const decoder = new TextDecoder('utf-8');
32
+ let buffered = '';
33
+ try {
34
+ while (true) {
35
+ const { done, value } = await reader.read();
36
+ if (done) {
37
+ // Flush any tail event the server emitted without the
38
+ // trailing blank line. Spec-lenient.
39
+ if (buffered.trim().length > 0) {
40
+ const tail = parseSseEvent(buffered, opts.requestId);
41
+ if (tail === DONE_SENTINEL)
42
+ return;
43
+ if (tail !== undefined)
44
+ yield tail;
45
+ }
46
+ return;
47
+ }
48
+ if (value)
49
+ buffered += decoder.decode(value, { stream: true });
50
+ // SSE frames are blank-line-delimited (`\n\n`). Slice them
51
+ // out one at a time so the inner parser sees one frame per
52
+ // call.
53
+ let sep = buffered.indexOf('\n\n');
54
+ while (sep !== -1) {
55
+ const frame = buffered.slice(0, sep);
56
+ buffered = buffered.slice(sep + 2);
57
+ const parsed = parseSseEvent(frame, opts.requestId);
58
+ if (parsed === DONE_SENTINEL)
59
+ return;
60
+ if (parsed !== undefined)
61
+ yield parsed;
62
+ sep = buffered.indexOf('\n\n');
63
+ }
64
+ }
65
+ }
66
+ finally {
67
+ // Release the lock so the underlying stream can be GC'd or
68
+ // re-used when the caller bails out (e.g. an `await break`
69
+ // out of the loop or a thrown error).
70
+ try {
71
+ reader.releaseLock();
72
+ }
73
+ catch {
74
+ /* already released */
75
+ }
76
+ }
77
+ }
78
+ /**
79
+ * Returned in place of a chunk when the frame was the terminal
80
+ * `data: [DONE]` marker — signals the outer loop to stop iterating
81
+ * without yielding `undefined` (which a consumer might mistake for
82
+ * a real value).
83
+ */
84
+ const DONE_SENTINEL = Symbol('hakim-sse-done');
85
+ function parseSseEvent(frame, requestId) {
86
+ let eventType;
87
+ const dataLines = [];
88
+ for (const rawLine of frame.split('\n')) {
89
+ const line = rawLine.replace(/\r$/, '');
90
+ if (line.length === 0)
91
+ continue;
92
+ if (line.startsWith(':'))
93
+ continue;
94
+ if (line.startsWith('event:')) {
95
+ eventType = line.slice(6).trim();
96
+ continue;
97
+ }
98
+ if (line.startsWith('data:')) {
99
+ dataLines.push(line.slice(5).replace(/^ /, ''));
100
+ continue;
101
+ }
102
+ }
103
+ if (dataLines.length === 0)
104
+ return undefined;
105
+ const payload = dataLines.join('\n');
106
+ if (payload === '[DONE]')
107
+ return DONE_SENTINEL;
108
+ if (eventType === 'error') {
109
+ let body;
110
+ try {
111
+ const parsed = JSON.parse(payload);
112
+ body = parsed.error;
113
+ }
114
+ catch {
115
+ /* fall through to the unstructured path below */
116
+ }
117
+ if (body) {
118
+ throw errorFromPayload(body, 500, requestId);
119
+ }
120
+ throw new HakimError({
121
+ type: 'service_unavailable',
122
+ code: 'upstream_error',
123
+ message: `Hakim SDK: streaming error envelope was not JSON-parseable (${payload.slice(0, 200)}).`,
124
+ status: 500,
125
+ requestId,
126
+ });
127
+ }
128
+ try {
129
+ return JSON.parse(payload);
130
+ }
131
+ catch (err) {
132
+ throw new HakimError({
133
+ type: 'api_error',
134
+ code: 'malformed_sse_chunk',
135
+ message: `Hakim SDK: failed to parse SSE chunk JSON: ${err instanceof Error ? err.message : String(err)}`,
136
+ status: 500,
137
+ requestId,
138
+ cause: err,
139
+ });
140
+ }
141
+ }
142
+ //# sourceMappingURL=sse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sse.js","sourceRoot":"","sources":["../../src/chat/sse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,gBAAgB,EAA6B,UAAU,EAAE,MAAM,cAAc,CAAC;AAEvF;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,cAAc,CACnC,IAAgC,EAChC,OAA2C,EAAE;IAE7C,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;IAChC,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;IACzC,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,IAAI,EAAE,CAAC;gBACT,sDAAsD;gBACtD,qCAAqC;gBACrC,IAAI,QAAQ,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;oBAC/B,MAAM,IAAI,GAAG,aAAa,CAAI,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;oBACxD,IAAI,IAAI,KAAK,aAAa;wBAAE,OAAO;oBACnC,IAAI,IAAI,KAAK,SAAS;wBAAE,MAAM,IAAI,CAAC;gBACrC,CAA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G,MAAM,EAAE,GAAG;YACX,SAAS;SACV,CAAC,CAAC;IACL,CAAC;IAED,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAM,CAAC;IAClC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,IAAI,UAAU,CAAC;YACnB,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE,qBAAqB;YAC3B,OAAO,EAAE,8CACP,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CACjD,EAAE;YACF,MAAM,EAAE,GAAG;YACX,SAAS;YACT,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Top-level `Hakim` client. Usage:
3
+ *
4
+ * const hakim = new Hakim({ apiKey: process.env.HAKIM_API_KEY! });
5
+ * await hakim.audio.speech.create({ model: 'hakim-fast-v1', … });
6
+ *
7
+ * Configurable knobs (all optional):
8
+ *
9
+ * - `baseURL` — override the API host (defaults to prod).
10
+ * Also reads HAKIM_BASE_URL env var.
11
+ * - `timeoutMs` — per-request timeout. Defaults to 120 s so
12
+ * long TTS synthesis doesn't spuriously abort.
13
+ * - `maxRetries` — transient-failure retries. Defaults to 2.
14
+ * - `userAgentSuffix` — appended to the SDK UA (e.g. 'my-app/1.0.0').
15
+ * - `fetchImpl` — inject a custom fetch (for tests or edge runtimes).
16
+ */
17
+ import { SpeechAPI } from './audio/speech.js';
18
+ import { TranscriptionsAPI } from './audio/transcriptions.js';
19
+ import { TranslateAPI } from './audio/translate.js';
20
+ import { VoicesAPI } from './audio/voices.js';
21
+ import { ChatCompletionsAPI } from './chat/completions.js';
22
+ import { JobsAPI } from './jobs.js';
23
+ import { NotificationsAPI, SettingsAPI } from './settings.js';
24
+ import { Transport } from './transport.js';
25
+ import { UsageAPI } from './usage.js';
26
+ import { WebhooksAPI } from './webhooks.js';
27
+ export interface HakimOptions {
28
+ apiKey?: string;
29
+ baseURL?: string;
30
+ timeoutMs?: number;
31
+ maxRetries?: number;
32
+ userAgentSuffix?: string;
33
+ fetchImpl?: typeof fetch;
34
+ /** Internal testing knob: override the random source used for
35
+ * backoff jitter. Don't use in application code. */
36
+ random?: () => number;
37
+ /** Internal testing knob: override sleep between retries. */
38
+ sleep?: (ms: number) => Promise<void>;
39
+ /** Internal testing knob: override idempotency key generation. */
40
+ generateIdempotencyKey?: () => string;
41
+ }
42
+ export declare class Hakim {
43
+ readonly audio: {
44
+ speech: SpeechAPI;
45
+ transcriptions: TranscriptionsAPI;
46
+ translate: TranslateAPI;
47
+ voices: VoicesAPI;
48
+ };
49
+ readonly voices: VoicesAPI;
50
+ readonly chat: {
51
+ completions: ChatCompletionsAPI;
52
+ };
53
+ readonly usage: UsageAPI;
54
+ readonly webhooks: WebhooksAPI;
55
+ readonly jobs: JobsAPI;
56
+ readonly settings: SettingsAPI;
57
+ readonly notifications: NotificationsAPI;
58
+ /** Expose the transport for advanced callers (custom endpoints,
59
+ * debugging). Stability of this interface is NOT guaranteed. */
60
+ readonly _transport: Transport;
61
+ constructor(options?: HakimOptions);
62
+ }
63
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,W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BAEnB,OAAO,GAAE,YAAiB;CA2CvC"}
package/dist/client.js ADDED
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Top-level `Hakim` client. Usage:
3
+ *
4
+ * const hakim = new Hakim({ apiKey: process.env.HAKIM_API_KEY! });
5
+ * await hakim.audio.speech.create({ model: 'hakim-fast-v1', … });
6
+ *
7
+ * Configurable knobs (all optional):
8
+ *
9
+ * - `baseURL` — override the API host (defaults to prod).
10
+ * Also reads HAKIM_BASE_URL env var.
11
+ * - `timeoutMs` — per-request timeout. Defaults to 120 s so
12
+ * long TTS synthesis doesn't spuriously abort.
13
+ * - `maxRetries` — transient-failure retries. Defaults to 2.
14
+ * - `userAgentSuffix` — appended to the SDK UA (e.g. 'my-app/1.0.0').
15
+ * - `fetchImpl` — inject a custom fetch (for tests or edge runtimes).
16
+ */
17
+ import { SpeechAPI } from './audio/speech.js';
18
+ import { TranscriptionsAPI } from './audio/transcriptions.js';
19
+ import { TranslateAPI } from './audio/translate.js';
20
+ import { VoicesAPI } from './audio/voices.js';
21
+ import { ChatCompletionsAPI } from './chat/completions.js';
22
+ import { JobsAPI } from './jobs.js';
23
+ import { NotificationsAPI, SettingsAPI } from './settings.js';
24
+ import { Transport } from './transport.js';
25
+ import { UsageAPI } from './usage.js';
26
+ import { WebhooksAPI } from './webhooks.js';
27
+ const DEFAULT_BASE_URL = 'https://api.tryhakim.ai';
28
+ const DEFAULT_TIMEOUT_MS = 120_000;
29
+ const DEFAULT_MAX_RETRIES = 2;
30
+ export class Hakim {
31
+ audio;
32
+ voices;
33
+ chat;
34
+ usage;
35
+ webhooks;
36
+ jobs;
37
+ settings;
38
+ notifications;
39
+ /** Expose the transport for advanced callers (custom endpoints,
40
+ * debugging). Stability of this interface is NOT guaranteed. */
41
+ _transport;
42
+ constructor(options = {}) {
43
+ const env = typeof process !== 'undefined' ? (process.env ?? {}) : {};
44
+ const apiKey = options.apiKey ?? env.HAKIM_API_KEY ?? env.HAKIM_API_TOKEN;
45
+ if (!apiKey) {
46
+ throw new TypeError('Hakim SDK: missing API key. Pass `apiKey` to new Hakim({...}) or set the HAKIM_API_KEY env var.');
47
+ }
48
+ const baseURL = options.baseURL ?? env.HAKIM_BASE_URL ?? DEFAULT_BASE_URL;
49
+ this._transport = new Transport({
50
+ apiKey,
51
+ baseURL,
52
+ timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
53
+ maxRetries: options.maxRetries ?? DEFAULT_MAX_RETRIES,
54
+ userAgentSuffix: options.userAgentSuffix,
55
+ ...(options.fetchImpl !== undefined ? { fetchImpl: options.fetchImpl } : {}),
56
+ ...(options.random !== undefined ? { random: options.random } : {}),
57
+ ...(options.sleep !== undefined ? { sleep: options.sleep } : {}),
58
+ ...(options.generateIdempotencyKey !== undefined
59
+ ? { generateIdempotencyKey: options.generateIdempotencyKey }
60
+ : {}),
61
+ });
62
+ const voicesApi = new VoicesAPI(this._transport);
63
+ this.audio = {
64
+ speech: new SpeechAPI(this._transport),
65
+ transcriptions: new TranscriptionsAPI(this._transport),
66
+ translate: new TranslateAPI(this._transport),
67
+ voices: voicesApi,
68
+ };
69
+ this.voices = voicesApi;
70
+ this.chat = {
71
+ completions: new ChatCompletionsAPI(this._transport),
72
+ };
73
+ this.usage = new UsageAPI(this._transport);
74
+ this.webhooks = new WebhooksAPI(this._transport);
75
+ this.jobs = new JobsAPI(this._transport);
76
+ this.settings = new SettingsAPI(this._transport);
77
+ this.notifications = new NotificationsAPI(this._transport);
78
+ }
79
+ }
80
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,S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AI,CAAC,QAAQ,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACzC,IAAI,CAAC,QAAQ,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACjD,IAAI,CAAC,aAAa,GAAG,IAAI,gBAAgB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC7D,CAAC;CACF"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * SDK error hierarchy (mirrors `@hakim/schemas` `ApiError.type` values).
3
+ *
4
+ * Every HTTP-level failure surfaces as a `HakimError` subclass so callers
5
+ * can `instanceof` on either the base class or a specific subclass:
6
+ *
7
+ * try {
8
+ * await hakim.audio.speech.create({ … });
9
+ * } catch (err) {
10
+ * if (err instanceof RateLimitError) await sleep(err.retryAfterMs);
11
+ * else if (err instanceof HakimError) console.error(err.requestId, err);
12
+ * else throw err;
13
+ * }
14
+ *
15
+ * Transport-level failures (DNS, connection reset, AbortError) are
16
+ * wrapped in `ConnectionError` so the caller never has to distinguish
17
+ * them from Node's own error strings.
18
+ */
19
+ export type HakimErrorType = 'invalid_request_error' | 'authentication_error' | 'permission_error' | 'not_found' | 'rate_limit_error' | 'quota_exceeded' | 'api_error' | 'service_unavailable' | 'idempotency_conflict' | 'connection_error';
20
+ export interface HakimApiErrorPayload {
21
+ type: HakimErrorType;
22
+ code: string;
23
+ message: string;
24
+ param?: string;
25
+ request_id?: string;
26
+ retry_after_seconds?: number;
27
+ upgrade_url?: string;
28
+ docs_url?: string;
29
+ }
30
+ export interface HakimErrorInit {
31
+ type: HakimErrorType;
32
+ code: string;
33
+ message: string;
34
+ status: number;
35
+ requestId: string | undefined;
36
+ param?: string;
37
+ cause?: unknown;
38
+ retryAfterMs?: number;
39
+ retryAfterSeconds?: number;
40
+ upgradeUrl?: string;
41
+ docsUrl?: string;
42
+ }
43
+ export declare class HakimError extends Error {
44
+ readonly type: HakimErrorType;
45
+ readonly code: string;
46
+ readonly status: number;
47
+ readonly requestId: string | undefined;
48
+ readonly param: string | undefined;
49
+ readonly cause: unknown;
50
+ /** For 429 / 503: milliseconds the server asked us to wait. */
51
+ readonly retryAfterMs: number | undefined;
52
+ constructor(init: HakimErrorInit);
53
+ }
54
+ export declare class InvalidRequestError extends HakimError {
55
+ constructor(init: HakimErrorInit);
56
+ }
57
+ export declare class AuthenticationError extends HakimError {
58
+ constructor(init: HakimErrorInit);
59
+ }
60
+ export declare class PermissionError extends HakimError {
61
+ /**
62
+ * M3 Phase 11 — set when the caller hit the Free-tier paywall
63
+ * (code === 'feature_requires_paid_plan'). Consumers can deep-link
64
+ * users to the billing page without re-parsing the payload.
65
+ */
66
+ readonly upgradeUrl: string | undefined;
67
+ /** Docs link explaining the specific restriction. */
68
+ readonly docsUrl: string | undefined;
69
+ constructor(init: HakimErrorInit);
70
+ }
71
+ export declare class NotFoundError extends HakimError {
72
+ constructor(init: HakimErrorInit);
73
+ }
74
+ export declare class QuotaExceededError extends HakimError {
75
+ /** Server-suggested seconds until retry. Mirrors `Retry-After`. */
76
+ readonly retryAfterSeconds: number | undefined;
77
+ /** Dashboard link to upgrade plan / update payment method. */
78
+ readonly upgradeUrl: string | undefined;
79
+ /** Docs link explaining the specific 402 reason code. */
80
+ readonly docsUrl: string | undefined;
81
+ constructor(init: HakimErrorInit);
82
+ }
83
+ export declare class RateLimitError extends HakimError {
84
+ constructor(init: HakimErrorInit);
85
+ }
86
+ export declare class IdempotencyConflictError extends HakimError {
87
+ constructor(init: HakimErrorInit);
88
+ }
89
+ export declare class ServiceUnavailableError extends HakimError {
90
+ constructor(init: HakimErrorInit);
91
+ }
92
+ export declare class ConnectionError extends HakimError {
93
+ constructor(init: Omit<HakimErrorInit, 'type' | 'code' | 'status'> & {
94
+ code?: string;
95
+ });
96
+ }
97
+ /** Factory: construct the right subclass from the wire payload. */
98
+ export declare function errorFromPayload(payload: HakimApiErrorPayload, status: number, requestId: string | undefined, retryAfterMs?: number): HakimError;
99
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,MAAM,cAAc,GACtB,uBAAuB,GACvB,sBAAsB,GACtB,kBAAkB,GAClB,WAAW,GACX,kBAAkB,GAClB,gBAAgB,GAChB,WAAW,GACX,qBAAqB,GACrB,sBAAsB,GACtB,kBAAkB,CAAC;AAEvB,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IAKpB,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,GAAG,SAAS,CAAC;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,UAAW,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAA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MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC,GAAG;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE;CAWvF;AAED,mEAAmE;AACnE,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,oBAAoB,EAC7B,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,YAAY,CAAC,EAAE,MAAM,GACpB,UAAU,CAoCZ"}
package/dist/errors.js ADDED
@@ -0,0 +1,158 @@
1
+ /**
2
+ * SDK error hierarchy (mirrors `@hakim/schemas` `ApiError.type` values).
3
+ *
4
+ * Every HTTP-level failure surfaces as a `HakimError` subclass so callers
5
+ * can `instanceof` on either the base class or a specific subclass:
6
+ *
7
+ * try {
8
+ * await hakim.audio.speech.create({ … });
9
+ * } catch (err) {
10
+ * if (err instanceof RateLimitError) await sleep(err.retryAfterMs);
11
+ * else if (err instanceof HakimError) console.error(err.requestId, err);
12
+ * else throw err;
13
+ * }
14
+ *
15
+ * Transport-level failures (DNS, connection reset, AbortError) are
16
+ * wrapped in `ConnectionError` so the caller never has to distinguish
17
+ * them from Node's own error strings.
18
+ */
19
+ export class HakimError extends Error {
20
+ type;
21
+ code;
22
+ status;
23
+ requestId;
24
+ param;
25
+ cause;
26
+ /** For 429 / 503: milliseconds the server asked us to wait. */
27
+ retryAfterMs;
28
+ constructor(init) {
29
+ super(init.message);
30
+ this.name = 'HakimError';
31
+ this.type = init.type;
32
+ this.code = init.code;
33
+ this.status = init.status;
34
+ this.requestId = init.requestId;
35
+ this.param = init.param;
36
+ this.cause = init.cause;
37
+ this.retryAfterMs = init.retryAfterMs;
38
+ }
39
+ }
40
+ export class InvalidRequestError extends HakimError {
41
+ constructor(init) {
42
+ super(init);
43
+ this.name = 'InvalidRequestError';
44
+ }
45
+ }
46
+ export class AuthenticationError extends HakimError {
47
+ constructor(init) {
48
+ super(init);
49
+ this.name = 'AuthenticationError';
50
+ }
51
+ }
52
+ export class PermissionError extends HakimError {
53
+ /**
54
+ * M3 Phase 11 — set when the caller hit the Free-tier paywall
55
+ * (code === 'feature_requires_paid_plan'). Consumers can deep-link
56
+ * users to the billing page without re-parsing the payload.
57
+ */
58
+ upgradeUrl;
59
+ /** Docs link explaining the specific restriction. */
60
+ docsUrl;
61
+ constructor(init) {
62
+ super(init);
63
+ this.name = 'PermissionError';
64
+ this.upgradeUrl = init.upgradeUrl;
65
+ this.docsUrl = init.docsUrl;
66
+ }
67
+ }
68
+ export class NotFoundError extends HakimError {
69
+ constructor(init) {
70
+ super(init);
71
+ this.name = 'NotFoundError';
72
+ }
73
+ }
74
+ export class QuotaExceededError extends HakimError {
75
+ /** Server-suggested seconds until retry. Mirrors `Retry-After`. */
76
+ retryAfterSeconds;
77
+ /** Dashboard link to upgrade plan / update payment method. */
78
+ upgradeUrl;
79
+ /** Docs link explaining the specific 402 reason code. */
80
+ docsUrl;
81
+ constructor(init) {
82
+ super(init);
83
+ this.name = 'QuotaExceededError';
84
+ this.retryAfterSeconds = init.retryAfterSeconds;
85
+ this.upgradeUrl = init.upgradeUrl;
86
+ this.docsUrl = init.docsUrl;
87
+ }
88
+ }
89
+ export class RateLimitError extends HakimError {
90
+ constructor(init) {
91
+ super(init);
92
+ this.name = 'RateLimitError';
93
+ }
94
+ }
95
+ export class IdempotencyConflictError extends HakimError {
96
+ constructor(init) {
97
+ super(init);
98
+ this.name = 'IdempotencyConflictError';
99
+ }
100
+ }
101
+ export class ServiceUnavailableError extends HakimError {
102
+ constructor(init) {
103
+ super(init);
104
+ this.name = 'ServiceUnavailableError';
105
+ }
106
+ }
107
+ export class ConnectionError extends HakimError {
108
+ constructor(init) {
109
+ super({
110
+ type: 'connection_error',
111
+ code: init.code ?? 'connection_failed',
112
+ message: init.message,
113
+ status: 0,
114
+ requestId: init.requestId,
115
+ cause: init.cause,
116
+ });
117
+ this.name = 'ConnectionError';
118
+ }
119
+ }
120
+ /** Factory: construct the right subclass from the wire payload. */
121
+ export function errorFromPayload(payload, status, requestId, retryAfterMs) {
122
+ const init = {
123
+ type: payload.type,
124
+ code: payload.code,
125
+ message: payload.message,
126
+ status,
127
+ requestId: payload.request_id ?? requestId,
128
+ ...(payload.param !== undefined ? { param: payload.param } : {}),
129
+ ...(retryAfterMs !== undefined ? { retryAfterMs } : {}),
130
+ ...(payload.retry_after_seconds !== undefined
131
+ ? { retryAfterSeconds: payload.retry_after_seconds }
132
+ : {}),
133
+ ...(payload.upgrade_url !== undefined ? { upgradeUrl: payload.upgrade_url } : {}),
134
+ ...(payload.docs_url !== undefined ? { docsUrl: payload.docs_url } : {}),
135
+ };
136
+ switch (payload.type) {
137
+ case 'invalid_request_error':
138
+ return new InvalidRequestError(init);
139
+ case 'authentication_error':
140
+ return new AuthenticationError(init);
141
+ case 'permission_error':
142
+ return new PermissionError(init);
143
+ case 'not_found':
144
+ return new NotFoundError(init);
145
+ case 'quota_exceeded':
146
+ return new QuotaExceededError(init);
147
+ case 'rate_limit_error':
148
+ return new RateLimitError(init);
149
+ case 'idempotency_conflict':
150
+ return new IdempotencyConflictError(init);
151
+ case 'service_unavailable':
152
+ return new ServiceUnavailableError(init);
153
+ case 'api_error':
154
+ default:
155
+ return new HakimError(init);
156
+ }
157
+ }
158
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AA2CH,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC1B,IAAI,CAAiB;IACrB,IAAI,CAAS;IACb,MAAM,CAAS;IACf,SAAS,CAAqB;IAC9B,KAAK,CAAqB;IACjB,KAAK,CAAU;IACjC,+DAA+D;IACtD,YAAY,CAAqB;IAE1C,YAAY,IAAoB;QAC9B,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,C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;QACjC,KAAK,gBAAgB;YACnB,OAAO,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAC;QACtC,KAAK,kBAAkB;YACrB,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC;QAClC,KAAK,sBAAsB;YACzB,OAAO,IAAI,wBAAwB,CAAC,IAAI,CAAC,CAAC;QAC5C,KAAK,qBAAqB;YACxB,OAAO,IAAI,uBAAuB,CAAC,IAAI,CAAC,CAAC;QAC3C,KAAK,WAAW,CAAC;QACjB;YACE,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;AACH,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * `@tryhakim/voice` — official Hakim API client for Node.js.
3
+ *
4
+ * import { Hakim } from '@tryhakim/voice';
5
+ *
6
+ * const hakim = new Hakim({ apiKey: process.env.HAKIM_API_KEY });
7
+ * const speech = await hakim.audio.speech.create({
8
+ * model: 'hakim-fast-v1',
9
+ * input: 'مرحبا بالعالم',
10
+ * // Pass the `id` from `hakim.audio.voices.list()` for stable
11
+ * // production traffic. Slugs (e.g. `'omar'`) are accepted but
12
+ * // are best for prototyping — they may collide between presets
13
+ * // and clones, and may shift if the voice catalogue is renamed.
14
+ * voice: 'cmokbc2b1001pvu39wmj61b7h',
15
+ * });
16
+ *
17
+ * See the package README for a tour of every namespace.
18
+ */
19
+ export { Hakim, type HakimOptions } from './client.js';
20
+ export { HakimError, InvalidRequestError, AuthenticationError, PermissionError, NotFoundError, QuotaExceededError, RateLimitError, IdempotencyConflictError, ServiceUnavailableError, ConnectionError, type HakimErrorType, type HakimApiErrorPayload, } from './errors.js';
21
+ export type { SpeechRequest, TranscriptionRequest, TranscriptionRequestCommon, TranscriptionStreamOptions, TranscriptionStreamHandle, TranscriptionStreamEvent, TranscriptionPartialEvent, TranscriptionFinalEvent, TranscriptionUsageEvent, TranscriptionErrorEvent, SpeechStreamOptions, SpeechStreamCreateRequest, SpeechStreamHandle, SpeechStreamEvent, SpeechStreamStartedEvent, SpeechStreamAudioEvent, SpeechStreamDoneEvent, SpeechStreamUsageEvent, SpeechStreamErrorEvent, TranslateStreamOptions, TranslateStreamHandle, TranslateStreamEvent, TranslateStreamCreatedEvent, TranslateStreamTranscriptionDeltaEvent, TranslateStreamTranscriptionDoneEvent, TranslateStreamTranslationDeltaEvent, TranslateStreamTranslationDoneEvent, TranslateStreamSpeechStartedEvent, TranslateStreamSpeechAudioEvent, TranslateStreamSpeechDoneEvent, TranslateStreamSessionUsageEvent, TranslateStreamErrorEvent, VoicesListQuery, VoiceCreateRequest, UsageEventsQuery, AudioInput, ChatModel, ChatRole, ChatMessage, ChatMessageContent, ChatTextContentPart, ChatReasoningOption, ChatCompletionRequest, ChatCompletionResponse, ChatCompletionChoice, ChatCompletionChunk, ChatCompletionChunkChoice, ChatCompletionChunkDelta, ChatCompletionUsage, ChatFinishReason, Webhook, WebhookCreated, WebhookCreateRequest, WebhookUpdateRequest, WebhooksListResponse, WebhookDelivery, WebhookDeliveriesListQuery, WebhookDeliveriesListResponse, WebhookDeliveryStatus, WebhookEventKey, Job, JobType, JobStatus, JobsListQuery, JobsListResponse, SpeechResponse, SpeechStreamResponse, TranscriptionResult, TranscriptionJsonResponse, TranscriptionAsyncAccepted, VoicesListResponse, Voice, UsageSummary, UsageEvent, UsageEventsList, ResponseMeta, TTSModel, ResponseFormat, SampleRate, STTModel, } from './types.js';
22
+ export { DEFAULT_TTS_MODEL, DEFAULT_STT_MODEL } from './types.js';
23
+ export type { STTResponseFormat, STTTimestamps, STTLanguage, BaseLanguageCode, ArabicDialectCode, VoiceKind, VoiceLanguage, VoiceGender, VoiceStatus, UsageKind, Profile, ProfileUpdateRequest, OrganizationSettings, OrganizationSettingsUpdateRequest, NotificationPreferences, NotificationPreferencesUpdateRequest, UserLocale, } from './types.js';
24
+ export { verifyWebhookSignature, type VerifyWebhookSignatureOptions, type WebhookSignatureVerifyResult, } from './webhooks.js';
25
+ export { ChatCompletionsAPI, type ChatCompletionCreateResponse, type ChatCompletionStreamResponse, type StreamingUsagePreflight, } from './chat/index.js';
26
+ export { SDK_VERSION, SDK_NAME } from './version.js';
27
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,wBAAwB,EACxB,uBAAuB,EACvB,eAAe,EACf,KAAK,cAAc,EACnB,KAAK,oBAAoB,GAC1B,MAAM,aAAa,CAAC;AACrB,YAAY,EAEV,aAAa,EACb,oBAAoB,EACpB,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,wBAAwB,EACxB,yBAAyB,EACzB,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,mBAAmB,EACnB,yBAAyB,EACzB,kBAAkB,EAClB,iBAAiB,EACjB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,sBAAsB,EACtB,sBAAsB,EACtB,qBAAqB,EACrB,oBAAoB,EACpB,2BAA2B,EAC3B,sCAAsC,EACtC,qCAAqC,EACrC,oCAAoC,EACpC,mCAAmC,EACnC,iCAAiC,EACjC,+BAA+B,EAC/B,8BAA8B,EAC9B,gCAAgC,EAChC,yBAAyB,EACzB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,UAAU,EAEV,SAAS,EACT,QAAQ,EACR,WAAW,EACX,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,yBAAyB,EACzB,wBAAwB,EACxB,mBAAmB,EACnB,gBAAgB,EAEhB,OAAO,EACP,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,EACpB,eAAe,EACf,0BAA0B,EAC1B,6BAA6B,EAC7B,qBAAqB,EACrB,eAAe,EAEf,GAAG,EACH,OAAO,EACP,SAAS,EACT,aAAa,EACb,gBAAgB,EAEhB,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,yBAAyB,EACzB,0BAA0B,EAC1B,kBAAkB,EAClB,KAAK,EACL,YAAY,EACZ,UAAU,EACV,eAAe,EACf,YAAY,EAEZ,QAAQ,EACR,cAAc,EACd,UAAU,EACV,QAAQ,GACT,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAClE,YAAY,EACV,iBAAiB,EACjB,aAAa,EACb,WAAW,EACX,gBAAgB,EAChB,iBAAiB,EACjB,SAAS,EACT,aAAa,EACb,WAAW,EACX,WAAW,EACX,SAAS,EAET,OAAO,EACP,oBAAoB,EACpB,oBAAoB,EACpB,iCAAiC,EACjC,uBAAuB,EACvB,oCAAoC,EACpC,UAAU,GACX,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,sBAAsB,EACtB,KAAK,6BAA6B,EAClC,KAAK,4BAA4B,GAClC,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,kBAAkB,EAClB,KAAK,4BAA4B,EACjC,KAAK,4BAA4B,EACjC,KAAK,uBAAuB,GAC7B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * `@tryhakim/voice` — official Hakim API client for Node.js.
3
+ *
4
+ * import { Hakim } from '@tryhakim/voice';
5
+ *
6
+ * const hakim = new Hakim({ apiKey: process.env.HAKIM_API_KEY });
7
+ * const speech = await hakim.audio.speech.create({
8
+ * model: 'hakim-fast-v1',
9
+ * input: 'مرحبا بالعالم',
10
+ * // Pass the `id` from `hakim.audio.voices.list()` for stable
11
+ * // production traffic. Slugs (e.g. `'omar'`) are accepted but
12
+ * // are best for prototyping — they may collide between presets
13
+ * // and clones, and may shift if the voice catalogue is renamed.
14
+ * voice: 'cmokbc2b1001pvu39wmj61b7h',
15
+ * });
16
+ *
17
+ * See the package README for a tour of every namespace.
18
+ */
19
+ export { Hakim } from './client.js';
20
+ export { HakimError, InvalidRequestError, AuthenticationError, PermissionError, NotFoundError, QuotaExceededError, RateLimitError, IdempotencyConflictError, ServiceUnavailableError, ConnectionError, } from './errors.js';
21
+ export { DEFAULT_TTS_MODEL, DEFAULT_STT_MODEL } from './types.js';
22
+ export { verifyWebhookSignature, } from './webhooks.js';
23
+ export { ChatCompletionsAPI, } from './chat/index.js';
24
+ export { SDK_VERSION, SDK_NAME } from './version.js';
25
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,EAAE,KAAK,EAAqB,MAAM,aAAa,CAAC;AACvD,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,kBAAkB,EAClB,cAAc,EACd,wBAAwB,EACxB,uBAAuB,EACvB,eAAe,GAGhB,MAAM,aAAa,CAAC;AAyFrB,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAqBlE,OAAO,EACL,sBAAsB,GAGvB,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,kBAAkB,GAInB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC"}