@automagik/omni 2.260424.3 → 2.260428.1

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,2199 @@
1
+ // ../../node_modules/.bun/openapi-fetch@0.13.8/node_modules/openapi-fetch/dist/index.js
2
+ var PATH_PARAM_RE = /\{[^{}]+\}/g;
3
+ var supportsRequestInitExt = () => {
4
+ return typeof process === "object" && Number.parseInt(process?.versions?.node?.substring(0, 2)) >= 18 && process.versions.undici;
5
+ };
6
+ function randomID() {
7
+ return Math.random().toString(36).slice(2, 11);
8
+ }
9
+ function createClient(clientOptions) {
10
+ let {
11
+ baseUrl = "",
12
+ Request: CustomRequest = globalThis.Request,
13
+ fetch: baseFetch = globalThis.fetch,
14
+ querySerializer: globalQuerySerializer,
15
+ bodySerializer: globalBodySerializer,
16
+ headers: baseHeaders,
17
+ requestInitExt = undefined,
18
+ ...baseOptions
19
+ } = { ...clientOptions };
20
+ requestInitExt = supportsRequestInitExt() ? requestInitExt : undefined;
21
+ baseUrl = removeTrailingSlash(baseUrl);
22
+ const middlewares = [];
23
+ async function coreFetch(schemaPath, fetchOptions) {
24
+ const {
25
+ baseUrl: localBaseUrl,
26
+ fetch: fetch2 = baseFetch,
27
+ Request = CustomRequest,
28
+ headers,
29
+ params = {},
30
+ parseAs = "json",
31
+ querySerializer: requestQuerySerializer,
32
+ bodySerializer = globalBodySerializer ?? defaultBodySerializer,
33
+ body,
34
+ ...init
35
+ } = fetchOptions || {};
36
+ let finalBaseUrl = baseUrl;
37
+ if (localBaseUrl) {
38
+ finalBaseUrl = removeTrailingSlash(localBaseUrl) ?? baseUrl;
39
+ }
40
+ let querySerializer = typeof globalQuerySerializer === "function" ? globalQuerySerializer : createQuerySerializer(globalQuerySerializer);
41
+ if (requestQuerySerializer) {
42
+ querySerializer = typeof requestQuerySerializer === "function" ? requestQuerySerializer : createQuerySerializer({
43
+ ...typeof globalQuerySerializer === "object" ? globalQuerySerializer : {},
44
+ ...requestQuerySerializer
45
+ });
46
+ }
47
+ const serializedBody = body === undefined ? undefined : bodySerializer(body, mergeHeaders(baseHeaders, headers, params.header));
48
+ const finalHeaders = mergeHeaders(serializedBody === undefined || serializedBody instanceof FormData ? {} : {
49
+ "Content-Type": "application/json"
50
+ }, baseHeaders, headers, params.header);
51
+ const requestInit = {
52
+ redirect: "follow",
53
+ ...baseOptions,
54
+ ...init,
55
+ body: serializedBody,
56
+ headers: finalHeaders
57
+ };
58
+ let id;
59
+ let options;
60
+ let request = new CustomRequest(createFinalURL(schemaPath, { baseUrl: finalBaseUrl, params, querySerializer }), requestInit);
61
+ let response;
62
+ for (const key in init) {
63
+ if (!(key in request)) {
64
+ request[key] = init[key];
65
+ }
66
+ }
67
+ if (middlewares.length) {
68
+ id = randomID();
69
+ options = Object.freeze({
70
+ baseUrl: finalBaseUrl,
71
+ fetch: fetch2,
72
+ parseAs,
73
+ querySerializer,
74
+ bodySerializer
75
+ });
76
+ for (const m of middlewares) {
77
+ if (m && typeof m === "object" && typeof m.onRequest === "function") {
78
+ const result = await m.onRequest({
79
+ request,
80
+ schemaPath,
81
+ params,
82
+ options,
83
+ id
84
+ });
85
+ if (result) {
86
+ if (result instanceof CustomRequest) {
87
+ request = result;
88
+ } else if (result instanceof Response) {
89
+ response = result;
90
+ break;
91
+ } else {
92
+ throw new Error("onRequest: must return new Request() or Response() when modifying the request");
93
+ }
94
+ }
95
+ }
96
+ }
97
+ }
98
+ if (!response) {
99
+ try {
100
+ response = await fetch2(request, requestInitExt);
101
+ } catch (error2) {
102
+ let errorAfterMiddleware = error2;
103
+ if (middlewares.length) {
104
+ for (let i = middlewares.length - 1;i >= 0; i--) {
105
+ const m = middlewares[i];
106
+ if (m && typeof m === "object" && typeof m.onError === "function") {
107
+ const result = await m.onError({
108
+ request,
109
+ error: errorAfterMiddleware,
110
+ schemaPath,
111
+ params,
112
+ options,
113
+ id
114
+ });
115
+ if (result) {
116
+ if (result instanceof Response) {
117
+ errorAfterMiddleware = undefined;
118
+ response = result;
119
+ break;
120
+ }
121
+ if (result instanceof Error) {
122
+ errorAfterMiddleware = result;
123
+ continue;
124
+ }
125
+ throw new Error("onError: must return new Response() or instance of Error");
126
+ }
127
+ }
128
+ }
129
+ }
130
+ if (errorAfterMiddleware) {
131
+ throw errorAfterMiddleware;
132
+ }
133
+ }
134
+ if (middlewares.length) {
135
+ for (let i = middlewares.length - 1;i >= 0; i--) {
136
+ const m = middlewares[i];
137
+ if (m && typeof m === "object" && typeof m.onResponse === "function") {
138
+ const result = await m.onResponse({
139
+ request,
140
+ response,
141
+ schemaPath,
142
+ params,
143
+ options,
144
+ id
145
+ });
146
+ if (result) {
147
+ if (!(result instanceof Response)) {
148
+ throw new Error("onResponse: must return new Response() when modifying the response");
149
+ }
150
+ response = result;
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ if (response.status === 204 || request.method === "HEAD" || response.headers.get("Content-Length") === "0") {
157
+ return response.ok ? { data: undefined, response } : { error: undefined, response };
158
+ }
159
+ if (response.ok) {
160
+ if (parseAs === "stream") {
161
+ return { data: response.body, response };
162
+ }
163
+ return { data: await response[parseAs](), response };
164
+ }
165
+ let error = await response.text();
166
+ try {
167
+ error = JSON.parse(error);
168
+ } catch {}
169
+ return { error, response };
170
+ }
171
+ return {
172
+ request(method, url, init) {
173
+ return coreFetch(url, { ...init, method: method.toUpperCase() });
174
+ },
175
+ GET(url, init) {
176
+ return coreFetch(url, { ...init, method: "GET" });
177
+ },
178
+ PUT(url, init) {
179
+ return coreFetch(url, { ...init, method: "PUT" });
180
+ },
181
+ POST(url, init) {
182
+ return coreFetch(url, { ...init, method: "POST" });
183
+ },
184
+ DELETE(url, init) {
185
+ return coreFetch(url, { ...init, method: "DELETE" });
186
+ },
187
+ OPTIONS(url, init) {
188
+ return coreFetch(url, { ...init, method: "OPTIONS" });
189
+ },
190
+ HEAD(url, init) {
191
+ return coreFetch(url, { ...init, method: "HEAD" });
192
+ },
193
+ PATCH(url, init) {
194
+ return coreFetch(url, { ...init, method: "PATCH" });
195
+ },
196
+ TRACE(url, init) {
197
+ return coreFetch(url, { ...init, method: "TRACE" });
198
+ },
199
+ use(...middleware) {
200
+ for (const m of middleware) {
201
+ if (!m) {
202
+ continue;
203
+ }
204
+ if (typeof m !== "object" || !(("onRequest" in m) || ("onResponse" in m) || ("onError" in m))) {
205
+ throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
206
+ }
207
+ middlewares.push(m);
208
+ }
209
+ },
210
+ eject(...middleware) {
211
+ for (const m of middleware) {
212
+ const i = middlewares.indexOf(m);
213
+ if (i !== -1) {
214
+ middlewares.splice(i, 1);
215
+ }
216
+ }
217
+ }
218
+ };
219
+ }
220
+ function serializePrimitiveParam(name, value, options) {
221
+ if (value === undefined || value === null) {
222
+ return "";
223
+ }
224
+ if (typeof value === "object") {
225
+ throw new Error("Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.");
226
+ }
227
+ return `${name}=${options?.allowReserved === true ? value : encodeURIComponent(value)}`;
228
+ }
229
+ function serializeObjectParam(name, value, options) {
230
+ if (!value || typeof value !== "object") {
231
+ return "";
232
+ }
233
+ const values = [];
234
+ const joiner = {
235
+ simple: ",",
236
+ label: ".",
237
+ matrix: ";"
238
+ }[options.style] || "&";
239
+ if (options.style !== "deepObject" && options.explode === false) {
240
+ for (const k in value) {
241
+ values.push(k, options.allowReserved === true ? value[k] : encodeURIComponent(value[k]));
242
+ }
243
+ const final2 = values.join(",");
244
+ switch (options.style) {
245
+ case "form": {
246
+ return `${name}=${final2}`;
247
+ }
248
+ case "label": {
249
+ return `.${final2}`;
250
+ }
251
+ case "matrix": {
252
+ return `;${name}=${final2}`;
253
+ }
254
+ default: {
255
+ return final2;
256
+ }
257
+ }
258
+ }
259
+ for (const k in value) {
260
+ const finalName = options.style === "deepObject" ? `${name}[${k}]` : k;
261
+ values.push(serializePrimitiveParam(finalName, value[k], options));
262
+ }
263
+ const final = values.join(joiner);
264
+ return options.style === "label" || options.style === "matrix" ? `${joiner}${final}` : final;
265
+ }
266
+ function serializeArrayParam(name, value, options) {
267
+ if (!Array.isArray(value)) {
268
+ return "";
269
+ }
270
+ if (options.explode === false) {
271
+ const joiner2 = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[options.style] || ",";
272
+ const final = (options.allowReserved === true ? value : value.map((v) => encodeURIComponent(v))).join(joiner2);
273
+ switch (options.style) {
274
+ case "simple": {
275
+ return final;
276
+ }
277
+ case "label": {
278
+ return `.${final}`;
279
+ }
280
+ case "matrix": {
281
+ return `;${name}=${final}`;
282
+ }
283
+ default: {
284
+ return `${name}=${final}`;
285
+ }
286
+ }
287
+ }
288
+ const joiner = { simple: ",", label: ".", matrix: ";" }[options.style] || "&";
289
+ const values = [];
290
+ for (const v of value) {
291
+ if (options.style === "simple" || options.style === "label") {
292
+ values.push(options.allowReserved === true ? v : encodeURIComponent(v));
293
+ } else {
294
+ values.push(serializePrimitiveParam(name, v, options));
295
+ }
296
+ }
297
+ return options.style === "label" || options.style === "matrix" ? `${joiner}${values.join(joiner)}` : values.join(joiner);
298
+ }
299
+ function createQuerySerializer(options) {
300
+ return function querySerializer(queryParams) {
301
+ const search = [];
302
+ if (queryParams && typeof queryParams === "object") {
303
+ for (const name in queryParams) {
304
+ const value = queryParams[name];
305
+ if (value === undefined || value === null) {
306
+ continue;
307
+ }
308
+ if (Array.isArray(value)) {
309
+ if (value.length === 0) {
310
+ continue;
311
+ }
312
+ search.push(serializeArrayParam(name, value, {
313
+ style: "form",
314
+ explode: true,
315
+ ...options?.array,
316
+ allowReserved: options?.allowReserved || false
317
+ }));
318
+ continue;
319
+ }
320
+ if (typeof value === "object") {
321
+ search.push(serializeObjectParam(name, value, {
322
+ style: "deepObject",
323
+ explode: true,
324
+ ...options?.object,
325
+ allowReserved: options?.allowReserved || false
326
+ }));
327
+ continue;
328
+ }
329
+ search.push(serializePrimitiveParam(name, value, options));
330
+ }
331
+ }
332
+ return search.join("&");
333
+ };
334
+ }
335
+ function defaultPathSerializer(pathname, pathParams) {
336
+ let nextURL = pathname;
337
+ for (const match of pathname.match(PATH_PARAM_RE) ?? []) {
338
+ let name = match.substring(1, match.length - 1);
339
+ let explode = false;
340
+ let style = "simple";
341
+ if (name.endsWith("*")) {
342
+ explode = true;
343
+ name = name.substring(0, name.length - 1);
344
+ }
345
+ if (name.startsWith(".")) {
346
+ style = "label";
347
+ name = name.substring(1);
348
+ } else if (name.startsWith(";")) {
349
+ style = "matrix";
350
+ name = name.substring(1);
351
+ }
352
+ if (!pathParams || pathParams[name] === undefined || pathParams[name] === null) {
353
+ continue;
354
+ }
355
+ const value = pathParams[name];
356
+ if (Array.isArray(value)) {
357
+ nextURL = nextURL.replace(match, serializeArrayParam(name, value, { style, explode }));
358
+ continue;
359
+ }
360
+ if (typeof value === "object") {
361
+ nextURL = nextURL.replace(match, serializeObjectParam(name, value, { style, explode }));
362
+ continue;
363
+ }
364
+ if (style === "matrix") {
365
+ nextURL = nextURL.replace(match, `;${serializePrimitiveParam(name, value)}`);
366
+ continue;
367
+ }
368
+ nextURL = nextURL.replace(match, style === "label" ? `.${encodeURIComponent(value)}` : encodeURIComponent(value));
369
+ }
370
+ return nextURL;
371
+ }
372
+ function defaultBodySerializer(body, headers) {
373
+ if (body instanceof FormData) {
374
+ return body;
375
+ }
376
+ if (headers) {
377
+ const contentType = headers.get instanceof Function ? headers.get("Content-Type") ?? headers.get("content-type") : headers["Content-Type"] ?? headers["content-type"];
378
+ if (contentType === "application/x-www-form-urlencoded") {
379
+ return new URLSearchParams(body).toString();
380
+ }
381
+ }
382
+ return JSON.stringify(body);
383
+ }
384
+ function createFinalURL(pathname, options) {
385
+ let finalURL = `${options.baseUrl}${pathname}`;
386
+ if (options.params?.path) {
387
+ finalURL = defaultPathSerializer(finalURL, options.params.path);
388
+ }
389
+ let search = options.querySerializer(options.params.query ?? {});
390
+ if (search.startsWith("?")) {
391
+ search = search.substring(1);
392
+ }
393
+ if (search) {
394
+ finalURL += `?${search}`;
395
+ }
396
+ return finalURL;
397
+ }
398
+ function mergeHeaders(...allHeaders) {
399
+ const finalHeaders = new Headers;
400
+ for (const h of allHeaders) {
401
+ if (!h || typeof h !== "object") {
402
+ continue;
403
+ }
404
+ const iterator = h instanceof Headers ? h.entries() : Object.entries(h);
405
+ for (const [k, v] of iterator) {
406
+ if (v === null) {
407
+ finalHeaders.delete(k);
408
+ } else if (Array.isArray(v)) {
409
+ for (const v2 of v) {
410
+ finalHeaders.append(k, v2);
411
+ }
412
+ } else if (v !== undefined) {
413
+ finalHeaders.set(k, v);
414
+ }
415
+ }
416
+ }
417
+ return finalHeaders;
418
+ }
419
+ function removeTrailingSlash(url) {
420
+ if (url.endsWith("/")) {
421
+ return url.substring(0, url.length - 1);
422
+ }
423
+ return url;
424
+ }
425
+
426
+ // src/errors.ts
427
+ function parseErrorObject(error, status) {
428
+ if (error instanceof Error && !("error" in error)) {
429
+ return null;
430
+ }
431
+ if ("error" in error) {
432
+ const rawError = error.error;
433
+ if (typeof rawError === "string") {
434
+ return new OmniApiError(rawError, "API_ERROR", undefined, status);
435
+ }
436
+ if (rawError && typeof rawError === "object") {
437
+ const apiError = rawError;
438
+ return new OmniApiError(apiError.message ?? `API error (status ${status ?? "unknown"})`, apiError.code ?? "API_ERROR", apiError.details, status);
439
+ }
440
+ }
441
+ if ("message" in error) {
442
+ const msg = error.message;
443
+ if (typeof msg === "string") {
444
+ return new OmniApiError(msg, "API_ERROR", undefined, status);
445
+ }
446
+ }
447
+ return null;
448
+ }
449
+
450
+ class OmniApiError extends Error {
451
+ code;
452
+ details;
453
+ status;
454
+ constructor(message, code, details, status) {
455
+ super(message);
456
+ this.name = "OmniApiError";
457
+ this.code = code;
458
+ this.details = details;
459
+ this.status = status;
460
+ }
461
+ static from(error, status) {
462
+ if (error && typeof error === "object") {
463
+ const result = parseErrorObject(error, status);
464
+ if (result)
465
+ return result;
466
+ }
467
+ if (error instanceof Error) {
468
+ return new OmniApiError(error.message, "UNKNOWN_ERROR", undefined, status);
469
+ }
470
+ return new OmniApiError(typeof error === "string" ? error : `API error (status ${status ?? "unknown"})`, "UNKNOWN_ERROR", undefined, status);
471
+ }
472
+ toJSON() {
473
+ return {
474
+ name: this.name,
475
+ code: this.code,
476
+ message: this.message,
477
+ details: this.details,
478
+ status: this.status
479
+ };
480
+ }
481
+ }
482
+
483
+ class OmniConfigError extends Error {
484
+ constructor(message) {
485
+ super(message);
486
+ this.name = "OmniConfigError";
487
+ }
488
+ }
489
+
490
+ // src/client.ts
491
+ function throwIfError(response, error) {
492
+ if (!response.ok) {
493
+ throw OmniApiError.from(error, response.status);
494
+ }
495
+ }
496
+ function createOmniClient(config) {
497
+ if (!config.baseUrl) {
498
+ throw new OmniConfigError("baseUrl is required");
499
+ }
500
+ if (!config.apiKey) {
501
+ throw new OmniConfigError("apiKey is required");
502
+ }
503
+ const baseUrl = config.baseUrl.replace(/\/$/, "");
504
+ const authMiddleware = {
505
+ async onRequest({ request }) {
506
+ request.headers.set("x-api-key", config.apiKey);
507
+ request.headers.set("Accept-Encoding", "identity");
508
+ if (config.cliVersion) {
509
+ request.headers.set("x-omni-cli-version", config.cliVersion);
510
+ }
511
+ return request;
512
+ }
513
+ };
514
+ const apiFetch = (url, init) => {
515
+ const headers = new Headers(init?.headers);
516
+ headers.set("x-api-key", config.apiKey);
517
+ headers.set("Accept-Encoding", "identity");
518
+ if (config.cliVersion) {
519
+ headers.set("x-omni-cli-version", config.cliVersion);
520
+ }
521
+ return fetch(url, {
522
+ ...init,
523
+ headers
524
+ });
525
+ };
526
+ const client = createClient({ baseUrl: `${baseUrl}/api/v2` });
527
+ client.use(authMiddleware);
528
+ return {
529
+ auth: {
530
+ async validate() {
531
+ const resp = await apiFetch(`${baseUrl}/api/v2/auth/validate`, {
532
+ method: "POST"
533
+ });
534
+ const json = await resp.json();
535
+ if (!resp.ok)
536
+ throw OmniApiError.from(json, resp.status);
537
+ return json?.data ?? { valid: false, keyPrefix: "", keyName: "", scopes: [] };
538
+ }
539
+ },
540
+ instances: {
541
+ async list(params) {
542
+ const { data, error, response } = await client.GET("/instances", {
543
+ params: { query: params }
544
+ });
545
+ throwIfError(response, error);
546
+ return {
547
+ items: data?.items ?? [],
548
+ meta: data?.meta ?? { hasMore: false }
549
+ };
550
+ },
551
+ async get(id) {
552
+ const { data, error, response } = await client.GET("/instances/{id}", {
553
+ params: { path: { id } }
554
+ });
555
+ throwIfError(response, error);
556
+ if (!data?.data)
557
+ throw new OmniApiError("Instance not found", "NOT_FOUND", undefined, 404);
558
+ return data.data;
559
+ },
560
+ async create(body) {
561
+ const { data, error, response } = await client.POST("/instances", { body });
562
+ throwIfError(response, error);
563
+ if (!data?.data)
564
+ throw new OmniApiError("Failed to create instance", "CREATE_FAILED", undefined, response.status);
565
+ return data.data;
566
+ },
567
+ async update(id, body) {
568
+ const { error, response } = await client.PATCH("/instances/{id}", {
569
+ params: { path: { id } },
570
+ body
571
+ });
572
+ throwIfError(response, error);
573
+ },
574
+ async delete(id) {
575
+ const { error, response } = await client.DELETE("/instances/{id}", {
576
+ params: { path: { id } }
577
+ });
578
+ throwIfError(response, error);
579
+ },
580
+ async status(id) {
581
+ const { data, error, response } = await client.GET("/instances/{id}/status", {
582
+ params: { path: { id } }
583
+ });
584
+ throwIfError(response, error);
585
+ return data?.data ?? { state: "unknown", isConnected: false };
586
+ },
587
+ async qr(id) {
588
+ const { data, error, response } = await client.GET("/instances/{id}/qr", {
589
+ params: { path: { id } }
590
+ });
591
+ throwIfError(response, error);
592
+ return data?.data ?? { qr: null, expiresAt: null, message: "No QR code available" };
593
+ },
594
+ async connect(id, body) {
595
+ const { data, error, response } = await client.POST("/instances/{id}/connect", {
596
+ params: { path: { id } },
597
+ body: body ?? {}
598
+ });
599
+ throwIfError(response, error);
600
+ return data?.data ?? { status: "connecting", message: "Connection initiated" };
601
+ },
602
+ async disconnect(id) {
603
+ const { error, response } = await client.POST("/instances/{id}/disconnect", {
604
+ params: { path: { id } }
605
+ });
606
+ throwIfError(response, error);
607
+ },
608
+ async restart(id, forceNewQr) {
609
+ const { data, error, response } = await client.POST("/instances/{id}/restart", {
610
+ params: { path: { id }, query: forceNewQr ? { forceNewQr: "true" } : undefined }
611
+ });
612
+ throwIfError(response, error);
613
+ return data?.data ?? { status: "restarting", message: "Restart initiated" };
614
+ },
615
+ async logout(id) {
616
+ const { error, response } = await client.POST("/instances/{id}/logout", {
617
+ params: { path: { id } }
618
+ });
619
+ throwIfError(response, error);
620
+ },
621
+ async pair(id, body) {
622
+ const { data, error, response } = await client.POST("/instances/{id}/pair", {
623
+ params: { path: { id } },
624
+ body
625
+ });
626
+ throwIfError(response, error);
627
+ return data?.data ?? { code: "", phoneNumber: "", message: "", expiresIn: 0 };
628
+ },
629
+ async syncProfile(id) {
630
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/sync/profile`, {
631
+ method: "POST"
632
+ });
633
+ const json = await resp.json();
634
+ if (!resp.ok)
635
+ throw OmniApiError.from(json, resp.status);
636
+ return json?.data ?? { type: "profile", status: "unknown", profile: null };
637
+ },
638
+ async startSync(id, body) {
639
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/sync`, {
640
+ method: "POST",
641
+ headers: { "Content-Type": "application/json" },
642
+ body: JSON.stringify(body)
643
+ });
644
+ const json = await resp.json();
645
+ if (!resp.ok)
646
+ throw OmniApiError.from(json, resp.status);
647
+ return json?.data ?? { jobId: "", instanceId: id, type: body.type, status: "pending", config: {}, message: "" };
648
+ },
649
+ async listSyncs(id, params) {
650
+ const query = new URLSearchParams;
651
+ if (params?.status)
652
+ query.set("status", params.status);
653
+ if (params?.limit)
654
+ query.set("limit", String(params.limit));
655
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/sync?${query}`, {});
656
+ const json = await resp.json();
657
+ if (!resp.ok)
658
+ throw OmniApiError.from(json, resp.status);
659
+ return { items: json?.items ?? [], meta: json?.meta ?? { hasMore: false, cursor: null } };
660
+ },
661
+ async getSyncStatus(id, jobId) {
662
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/sync/${jobId}`, {});
663
+ const json = await resp.json();
664
+ if (!resp.ok)
665
+ throw OmniApiError.from(json, resp.status);
666
+ if (!json?.data)
667
+ throw new OmniApiError("Sync job not found", "NOT_FOUND", undefined, 404);
668
+ return json.data;
669
+ },
670
+ async listContacts(id, params) {
671
+ const query = new URLSearchParams;
672
+ if (params?.limit)
673
+ query.set("limit", String(params.limit));
674
+ if (params?.cursor)
675
+ query.set("cursor", params.cursor);
676
+ if (params?.guildId)
677
+ query.set("guildId", params.guildId);
678
+ if (params?.search)
679
+ query.set("search", params.search);
680
+ if (params?.excludeGroups)
681
+ query.set("excludeGroups", "true");
682
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/contacts?${query}`, {});
683
+ const json = await resp.json();
684
+ if (!resp.ok)
685
+ throw OmniApiError.from(json, resp.status);
686
+ return { items: json?.items ?? [], meta: json?.meta ?? { totalFetched: 0, hasMore: false } };
687
+ },
688
+ async listGroups(id, params) {
689
+ const query = new URLSearchParams;
690
+ if (params?.limit)
691
+ query.set("limit", String(params.limit));
692
+ if (params?.cursor)
693
+ query.set("cursor", params.cursor);
694
+ if (params?.search)
695
+ query.set("search", params.search);
696
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/groups?${query}`, {});
697
+ const json = await resp.json();
698
+ if (!resp.ok)
699
+ throw OmniApiError.from(json, resp.status);
700
+ return { items: json?.items ?? [], meta: json?.meta ?? { totalFetched: 0, hasMore: false } };
701
+ },
702
+ async listGroupMembers(id, groupJid) {
703
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/groups/${encodeURIComponent(groupJid)}/members`, {});
704
+ const json = await resp.json();
705
+ if (!resp.ok)
706
+ throw OmniApiError.from(json, resp.status);
707
+ return { members: json?.members ?? [] };
708
+ },
709
+ async getUserProfile(id, userId) {
710
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${id}/users/${userId}/profile`, {});
711
+ const json = await resp.json();
712
+ if (!resp.ok)
713
+ throw OmniApiError.from(json, resp.status);
714
+ if (!json?.data)
715
+ throw new OmniApiError("User profile not found", "NOT_FOUND", undefined, 404);
716
+ return json.data;
717
+ }
718
+ },
719
+ chats: {
720
+ async list(params) {
721
+ const query = new URLSearchParams;
722
+ const setIfDefined = (key, value) => {
723
+ if (value !== undefined)
724
+ query.set(key, String(value));
725
+ };
726
+ setIfDefined("instanceId", params?.instanceId);
727
+ setIfDefined("channel", params?.channel);
728
+ setIfDefined("chatType", params?.chatType);
729
+ setIfDefined("excludeChatTypes", params?.excludeChatTypes);
730
+ setIfDefined("search", params?.search);
731
+ setIfDefined("includeArchived", params?.includeArchived);
732
+ setIfDefined("unreadOnly", params?.unreadOnly);
733
+ setIfDefined("sort", params?.sort);
734
+ setIfDefined("limit", params?.limit);
735
+ setIfDefined("cursor", params?.cursor);
736
+ setIfDefined("pendingOnly", params?.pendingOnly);
737
+ setIfDefined("attentionOnly", params?.attentionOnly);
738
+ setIfDefined("label", params?.label);
739
+ setIfDefined("includeHidden", params?.includeHidden);
740
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats?${query}`, {});
741
+ const json = await resp.json();
742
+ if (!resp.ok)
743
+ throw OmniApiError.from(json, resp.status);
744
+ return { items: json?.items ?? [], meta: json?.meta ?? { hasMore: false, cursor: null } };
745
+ },
746
+ async get(id) {
747
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}`, {});
748
+ const json = await resp.json();
749
+ if (!resp.ok)
750
+ throw OmniApiError.from(json, resp.status);
751
+ if (!json?.data)
752
+ throw new OmniApiError("Chat not found", "NOT_FOUND", undefined, 404);
753
+ return json.data;
754
+ },
755
+ async create(body) {
756
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats`, {
757
+ method: "POST",
758
+ headers: { "Content-Type": "application/json" },
759
+ body: JSON.stringify(body)
760
+ });
761
+ const json = await resp.json();
762
+ if (!resp.ok)
763
+ throw OmniApiError.from(json, resp.status);
764
+ if (!json?.data)
765
+ throw new OmniApiError("Failed to create chat", "CREATE_FAILED", undefined, resp.status);
766
+ return json.data;
767
+ },
768
+ async update(id, body) {
769
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}`, {
770
+ method: "PATCH",
771
+ headers: { "Content-Type": "application/json" },
772
+ body: JSON.stringify(body)
773
+ });
774
+ const json = await resp.json();
775
+ if (!resp.ok)
776
+ throw OmniApiError.from(json, resp.status);
777
+ if (!json?.data)
778
+ throw new OmniApiError("Failed to update chat", "UPDATE_FAILED", undefined, resp.status);
779
+ return json.data;
780
+ },
781
+ async delete(id) {
782
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}`, {
783
+ method: "DELETE"
784
+ });
785
+ if (!resp.ok)
786
+ throw OmniApiError.from(await resp.json(), resp.status);
787
+ },
788
+ async archive(id) {
789
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/archive`, {
790
+ method: "POST"
791
+ });
792
+ const json = await resp.json();
793
+ if (!resp.ok)
794
+ throw OmniApiError.from(json, resp.status);
795
+ if (!json?.data)
796
+ throw new OmniApiError("Failed to archive chat", "ARCHIVE_FAILED", undefined, resp.status);
797
+ return json.data;
798
+ },
799
+ async unarchive(id) {
800
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/unarchive`, {
801
+ method: "POST"
802
+ });
803
+ const json = await resp.json();
804
+ if (!resp.ok)
805
+ throw OmniApiError.from(json, resp.status);
806
+ if (!json?.data)
807
+ throw new OmniApiError("Failed to unarchive chat", "UNARCHIVE_FAILED", undefined, resp.status);
808
+ return json.data;
809
+ },
810
+ async getMessages(id, params) {
811
+ const query = new URLSearchParams;
812
+ if (params?.limit)
813
+ query.set("limit", String(params.limit));
814
+ if (params?.before)
815
+ query.set("before", params.before);
816
+ if (params?.after)
817
+ query.set("after", params.after);
818
+ if (params?.mediaOnly)
819
+ query.set("mediaOnly", "true");
820
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/messages?${query}`, {});
821
+ const json = await resp.json();
822
+ if (!resp.ok)
823
+ throw OmniApiError.from(json, resp.status);
824
+ return json?.items ?? [];
825
+ },
826
+ async listParticipants(id) {
827
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/participants`, {});
828
+ const json = await resp.json();
829
+ if (!resp.ok)
830
+ throw OmniApiError.from(json, resp.status);
831
+ return json?.items ?? [];
832
+ },
833
+ async addParticipant(id, body) {
834
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/participants`, {
835
+ method: "POST",
836
+ headers: { "Content-Type": "application/json" },
837
+ body: JSON.stringify(body)
838
+ });
839
+ const json = await resp.json();
840
+ if (!resp.ok)
841
+ throw OmniApiError.from(json, resp.status);
842
+ if (!json?.data)
843
+ throw new OmniApiError("Failed to add participant", "ADD_FAILED", undefined, resp.status);
844
+ return json.data;
845
+ },
846
+ async removeParticipant(id, platformUserId) {
847
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/participants/${platformUserId}`, {
848
+ method: "DELETE"
849
+ });
850
+ if (!resp.ok)
851
+ throw OmniApiError.from(await resp.json(), resp.status);
852
+ },
853
+ async hide(id) {
854
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/hide`, {
855
+ method: "POST"
856
+ });
857
+ const json = await resp.json();
858
+ if (!resp.ok)
859
+ throw OmniApiError.from(json, resp.status);
860
+ if (!json?.data)
861
+ throw new OmniApiError("Failed to hide chat", "HIDE_FAILED", undefined, resp.status);
862
+ return json.data;
863
+ },
864
+ async unhide(id) {
865
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/unhide`, {
866
+ method: "POST"
867
+ });
868
+ const json = await resp.json();
869
+ if (!resp.ok)
870
+ throw OmniApiError.from(json, resp.status);
871
+ if (!json?.data)
872
+ throw new OmniApiError("Failed to unhide chat", "UNHIDE_FAILED", undefined, resp.status);
873
+ return json.data;
874
+ },
875
+ async addLabel(id, label) {
876
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/label`, {
877
+ method: "POST",
878
+ headers: { "Content-Type": "application/json" },
879
+ body: JSON.stringify({ label })
880
+ });
881
+ const json = await resp.json();
882
+ if (!resp.ok)
883
+ throw OmniApiError.from(json, resp.status);
884
+ if (!json?.data)
885
+ throw new OmniApiError("Failed to add label", "LABEL_FAILED", undefined, resp.status);
886
+ return json.data;
887
+ },
888
+ async removeLabel(id, label) {
889
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/label`, {
890
+ method: "DELETE",
891
+ headers: { "Content-Type": "application/json" },
892
+ body: JSON.stringify({ label })
893
+ });
894
+ const json = await resp.json();
895
+ if (!resp.ok)
896
+ throw OmniApiError.from(json, resp.status);
897
+ if (!json?.data)
898
+ throw new OmniApiError("Failed to remove label", "UNLABEL_FAILED", undefined, resp.status);
899
+ return json.data;
900
+ },
901
+ async markRead(id, body) {
902
+ const resp = await apiFetch(`${baseUrl}/api/v2/chats/${id}/read`, {
903
+ method: "POST",
904
+ headers: { "Content-Type": "application/json" },
905
+ body: JSON.stringify(body)
906
+ });
907
+ const json = await resp.json();
908
+ if (!resp.ok)
909
+ throw OmniApiError.from(json, resp.status);
910
+ return json?.data ?? { chatId: id, instanceId: body.instanceId };
911
+ }
912
+ },
913
+ messages: {
914
+ async get(messageId) {
915
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/${messageId}`, {});
916
+ const json = await resp.json();
917
+ if (!resp.ok)
918
+ throw OmniApiError.from(json, resp.status);
919
+ return json?.data;
920
+ },
921
+ async send(body) {
922
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send`, {
923
+ method: "POST",
924
+ headers: { "Content-Type": "application/json" },
925
+ body: JSON.stringify(body)
926
+ });
927
+ const json = await resp.json();
928
+ if (!resp.ok)
929
+ throw OmniApiError.from(json, resp.status);
930
+ return json?.data ?? { messageId: "", status: "sent" };
931
+ },
932
+ async sendMedia(body) {
933
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/media`, {
934
+ method: "POST",
935
+ headers: { "Content-Type": "application/json" },
936
+ body: JSON.stringify(body)
937
+ });
938
+ const json = await resp.json();
939
+ if (!resp.ok)
940
+ throw OmniApiError.from(json, resp.status);
941
+ return json?.data ?? { messageId: "", status: "sent" };
942
+ },
943
+ async sendReaction(body) {
944
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/reaction`, {
945
+ method: "POST",
946
+ headers: { "Content-Type": "application/json" },
947
+ body: JSON.stringify(body)
948
+ });
949
+ const json = await resp.json();
950
+ if (!resp.ok)
951
+ throw OmniApiError.from(json, resp.status);
952
+ return { success: json?.success ?? true, messageId: json?.data?.messageId };
953
+ },
954
+ async sendSticker(body) {
955
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/sticker`, {
956
+ method: "POST",
957
+ headers: { "Content-Type": "application/json" },
958
+ body: JSON.stringify(body)
959
+ });
960
+ const json = await resp.json();
961
+ if (!resp.ok)
962
+ throw OmniApiError.from(json, resp.status);
963
+ return json?.data ?? { messageId: "", status: "sent" };
964
+ },
965
+ async sendContact(body) {
966
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/contact`, {
967
+ method: "POST",
968
+ headers: { "Content-Type": "application/json" },
969
+ body: JSON.stringify(body)
970
+ });
971
+ const json = await resp.json();
972
+ if (!resp.ok)
973
+ throw OmniApiError.from(json, resp.status);
974
+ return json?.data ?? { messageId: "", status: "sent" };
975
+ },
976
+ async sendLocation(body) {
977
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/location`, {
978
+ method: "POST",
979
+ headers: { "Content-Type": "application/json" },
980
+ body: JSON.stringify(body)
981
+ });
982
+ const json = await resp.json();
983
+ if (!resp.ok)
984
+ throw OmniApiError.from(json, resp.status);
985
+ return json?.data ?? { messageId: "", status: "sent" };
986
+ },
987
+ async sendPoll(body) {
988
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/poll`, {
989
+ method: "POST",
990
+ headers: { "Content-Type": "application/json" },
991
+ body: JSON.stringify(body)
992
+ });
993
+ const json = await resp.json();
994
+ if (!resp.ok)
995
+ throw OmniApiError.from(json, resp.status);
996
+ return json?.data ?? { messageId: "", status: "sent" };
997
+ },
998
+ async sendEmbed(body) {
999
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/embed`, {
1000
+ method: "POST",
1001
+ headers: { "Content-Type": "application/json" },
1002
+ body: JSON.stringify(body)
1003
+ });
1004
+ const json = await resp.json();
1005
+ if (!resp.ok)
1006
+ throw OmniApiError.from(json, resp.status);
1007
+ return json?.data ?? { messageId: "", status: "sent" };
1008
+ },
1009
+ async sendPresence(body) {
1010
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/presence`, {
1011
+ method: "POST",
1012
+ headers: { "Content-Type": "application/json" },
1013
+ body: JSON.stringify(body)
1014
+ });
1015
+ const json = await resp.json();
1016
+ if (!resp.ok)
1017
+ throw OmniApiError.from(json, resp.status);
1018
+ return json?.data ?? {
1019
+ instanceId: body.instanceId,
1020
+ chatId: body.to,
1021
+ type: body.type,
1022
+ duration: body.duration ?? 5000
1023
+ };
1024
+ },
1025
+ async markRead(messageId, body) {
1026
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/${messageId}/read`, {
1027
+ method: "POST",
1028
+ headers: { "Content-Type": "application/json" },
1029
+ body: JSON.stringify(body)
1030
+ });
1031
+ const json = await resp.json();
1032
+ if (!resp.ok)
1033
+ throw OmniApiError.from(json, resp.status);
1034
+ return json?.data ?? { messageId };
1035
+ },
1036
+ async batchMarkRead(body) {
1037
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/read`, {
1038
+ method: "POST",
1039
+ headers: { "Content-Type": "application/json" },
1040
+ body: JSON.stringify(body)
1041
+ });
1042
+ const json = await resp.json();
1043
+ if (!resp.ok)
1044
+ throw OmniApiError.from(json, resp.status);
1045
+ return json?.data ?? { chatId: body.chatId, instanceId: body.instanceId, messageCount: body.messageIds.length };
1046
+ },
1047
+ async listVoices() {
1048
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/tts/voices`, {});
1049
+ const json = await resp.json();
1050
+ if (!resp.ok)
1051
+ throw OmniApiError.from(json, resp.status);
1052
+ return json?.data?.voices ?? [];
1053
+ },
1054
+ async sendTts(body) {
1055
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/tts`, {
1056
+ method: "POST",
1057
+ headers: { "Content-Type": "application/json" },
1058
+ body: JSON.stringify(body)
1059
+ });
1060
+ const json = await resp.json();
1061
+ if (!resp.ok)
1062
+ throw OmniApiError.from(json, resp.status);
1063
+ return json?.data ?? { messageId: "", status: "sent", audioSizeKb: 0, durationMs: 0, timestamp: Date.now() };
1064
+ },
1065
+ async sendForward(body) {
1066
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/send/forward`, {
1067
+ method: "POST",
1068
+ headers: { "Content-Type": "application/json" },
1069
+ body: JSON.stringify(body)
1070
+ });
1071
+ const json = await resp.json();
1072
+ if (!resp.ok)
1073
+ throw OmniApiError.from(json, resp.status);
1074
+ return json?.data ?? { messageId: "", status: "sent" };
1075
+ },
1076
+ async removeReaction(id, body) {
1077
+ const resp = await apiFetch(`${baseUrl}/api/v2/messages/${id}/reactions`, {
1078
+ method: "DELETE",
1079
+ headers: { "Content-Type": "application/json" },
1080
+ body: JSON.stringify(body)
1081
+ });
1082
+ const json = await resp.json();
1083
+ if (!resp.ok)
1084
+ throw OmniApiError.from(json, resp.status);
1085
+ return { success: json?.success ?? true };
1086
+ }
1087
+ },
1088
+ events: {
1089
+ async list(params) {
1090
+ const { data, error, response } = await client.GET("/events", {
1091
+ params: { query: params }
1092
+ });
1093
+ throwIfError(response, error);
1094
+ return {
1095
+ items: data?.items ?? [],
1096
+ meta: data?.meta ?? { hasMore: false }
1097
+ };
1098
+ },
1099
+ async get(id) {
1100
+ const { data, error, response } = await client.GET("/events/{id}", {
1101
+ params: { path: { id } }
1102
+ });
1103
+ throwIfError(response, error);
1104
+ if (!data?.data)
1105
+ throw new OmniApiError("Event not found", "NOT_FOUND", undefined, 404);
1106
+ return data.data;
1107
+ },
1108
+ async analytics(params) {
1109
+ const { data, error, response } = await client.GET("/events/analytics", {
1110
+ params: { query: params }
1111
+ });
1112
+ throwIfError(response, error);
1113
+ if (!data)
1114
+ throw new OmniApiError("Analytics data not found", "NOT_FOUND", undefined, 404);
1115
+ return data;
1116
+ }
1117
+ },
1118
+ persons: {
1119
+ async search(params) {
1120
+ const { data, error, response } = await client.GET("/persons", {
1121
+ params: { query: params }
1122
+ });
1123
+ throwIfError(response, error);
1124
+ return data?.items ?? [];
1125
+ },
1126
+ async get(id) {
1127
+ const { data, error, response } = await client.GET("/persons/{id}", {
1128
+ params: { path: { id } }
1129
+ });
1130
+ throwIfError(response, error);
1131
+ if (!data?.data)
1132
+ throw new OmniApiError("Person not found", "NOT_FOUND", undefined, 404);
1133
+ return data.data;
1134
+ },
1135
+ async presence(id) {
1136
+ const resp = await apiFetch(`${baseUrl}/api/v2/persons/${id}/presence`, {});
1137
+ const json = await resp.json();
1138
+ if (!resp.ok)
1139
+ throw OmniApiError.from(json, resp.status);
1140
+ if (!json?.data)
1141
+ throw new OmniApiError("Person not found", "NOT_FOUND", undefined, 404);
1142
+ return json.data;
1143
+ },
1144
+ async update(id, data) {
1145
+ const resp = await apiFetch(`${baseUrl}/api/v2/persons/${id}`, {
1146
+ method: "PATCH",
1147
+ headers: { "Content-Type": "application/json" },
1148
+ body: JSON.stringify(data)
1149
+ });
1150
+ const json = await resp.json();
1151
+ if (!resp.ok)
1152
+ throw OmniApiError.from(json, resp.status);
1153
+ if (!json?.data)
1154
+ throw new OmniApiError("Person not found", "NOT_FOUND", undefined, 404);
1155
+ return json.data;
1156
+ },
1157
+ async link(identityA, identityB) {
1158
+ const resp = await apiFetch(`${baseUrl}/api/v2/persons/link`, {
1159
+ method: "POST",
1160
+ headers: { "Content-Type": "application/json" },
1161
+ body: JSON.stringify({ identityA, identityB })
1162
+ });
1163
+ const json = await resp.json();
1164
+ if (!resp.ok)
1165
+ throw OmniApiError.from(json, resp.status);
1166
+ if (!json?.data)
1167
+ throw new OmniApiError("Link failed", "INTERNAL_ERROR", undefined, 500);
1168
+ return json.data;
1169
+ },
1170
+ async unlink(identityId, reason) {
1171
+ const resp = await apiFetch(`${baseUrl}/api/v2/persons/unlink`, {
1172
+ method: "POST",
1173
+ headers: { "Content-Type": "application/json" },
1174
+ body: JSON.stringify({ identityId, reason })
1175
+ });
1176
+ const json = await resp.json();
1177
+ if (!resp.ok)
1178
+ throw OmniApiError.from(json, resp.status);
1179
+ if (!json?.data)
1180
+ throw new OmniApiError("Unlink failed", "INTERNAL_ERROR", undefined, 500);
1181
+ return json.data;
1182
+ },
1183
+ async merge(sourcePersonId, targetPersonId, reason) {
1184
+ const resp = await apiFetch(`${baseUrl}/api/v2/persons/merge`, {
1185
+ method: "POST",
1186
+ headers: { "Content-Type": "application/json" },
1187
+ body: JSON.stringify({ sourcePersonId, targetPersonId, reason })
1188
+ });
1189
+ const json = await resp.json();
1190
+ if (!resp.ok)
1191
+ throw OmniApiError.from(json, resp.status);
1192
+ if (!json?.data)
1193
+ throw new OmniApiError("Merge failed", "INTERNAL_ERROR", undefined, 500);
1194
+ return json.data;
1195
+ }
1196
+ },
1197
+ access: {
1198
+ async listRules(params) {
1199
+ const { data, error, response } = await client.GET("/access/rules", {
1200
+ params: { query: params }
1201
+ });
1202
+ throwIfError(response, error);
1203
+ return data?.items ?? [];
1204
+ },
1205
+ async createRule(body) {
1206
+ const { error, response } = await client.POST("/access/rules", { body });
1207
+ throwIfError(response, error);
1208
+ },
1209
+ async deleteRule(id) {
1210
+ const { error, response } = await client.DELETE("/access/rules/{id}", {
1211
+ params: { path: { id } }
1212
+ });
1213
+ throwIfError(response, error);
1214
+ },
1215
+ async checkAccess(params) {
1216
+ const { data, error, response } = await client.POST("/access/check", {
1217
+ body: params
1218
+ });
1219
+ throwIfError(response, error);
1220
+ return data?.data ?? { allowed: true, reason: "Default allow" };
1221
+ },
1222
+ async listPairingRequests(instanceId) {
1223
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${instanceId}/pairing-requests`, {});
1224
+ const json = await resp.json();
1225
+ if (!resp.ok)
1226
+ throw OmniApiError.from(json, resp.status);
1227
+ return json?.items ?? [];
1228
+ },
1229
+ async actionPairingRequest(instanceId, requestId, body) {
1230
+ const resp = await apiFetch(`${baseUrl}/api/v2/instances/${instanceId}/pairing-requests/${requestId}/action`, {
1231
+ method: "POST",
1232
+ headers: { "Content-Type": "application/json" },
1233
+ body: JSON.stringify(body)
1234
+ });
1235
+ const json = await resp.json();
1236
+ if (!resp.ok)
1237
+ throw OmniApiError.from(json, resp.status);
1238
+ return json?.data ?? { action: body.action };
1239
+ }
1240
+ },
1241
+ settings: {
1242
+ async list(params) {
1243
+ const { data, error, response } = await client.GET("/settings", {
1244
+ params: { query: params }
1245
+ });
1246
+ throwIfError(response, error);
1247
+ return data?.items ?? [];
1248
+ },
1249
+ async get(key) {
1250
+ const resp = await apiFetch(`${baseUrl}/api/v2/settings/${encodeURIComponent(key)}`, {});
1251
+ const json = await resp.json();
1252
+ if (!resp.ok)
1253
+ throw OmniApiError.from(json, resp.status);
1254
+ if (!json?.data)
1255
+ throw new OmniApiError("Setting not found", "NOT_FOUND", undefined, 404);
1256
+ return json.data;
1257
+ },
1258
+ async set(key, value, reason) {
1259
+ const resp = await apiFetch(`${baseUrl}/api/v2/settings/${encodeURIComponent(key)}`, {
1260
+ method: "PUT",
1261
+ headers: { "Content-Type": "application/json" },
1262
+ body: JSON.stringify({ value, reason })
1263
+ });
1264
+ const json = await resp.json();
1265
+ if (!resp.ok)
1266
+ throw OmniApiError.from(json, resp.status);
1267
+ if (!json?.data)
1268
+ throw new OmniApiError("Failed to set setting", "UPDATE_FAILED", undefined, resp.status);
1269
+ return json.data;
1270
+ }
1271
+ },
1272
+ providers: {
1273
+ async list(params) {
1274
+ const { data, error, response } = await client.GET("/providers", {
1275
+ params: { query: params }
1276
+ });
1277
+ throwIfError(response, error);
1278
+ return data?.items ?? [];
1279
+ },
1280
+ async get(id) {
1281
+ const { data, error, response } = await client.GET("/providers/{id}", {
1282
+ params: { path: { id } }
1283
+ });
1284
+ throwIfError(response, error);
1285
+ if (!data?.data)
1286
+ throw new OmniApiError("Provider not found", "NOT_FOUND", undefined, 404);
1287
+ return data.data;
1288
+ },
1289
+ async create(body) {
1290
+ const resp = await apiFetch(`${baseUrl}/api/v2/providers`, {
1291
+ method: "POST",
1292
+ headers: { "Content-Type": "application/json" },
1293
+ body: JSON.stringify(body)
1294
+ });
1295
+ if (!resp.ok) {
1296
+ const errorData = await resp.json().catch(() => ({}));
1297
+ throw new OmniApiError(errorData.error ?? resp.statusText, "CREATE_FAILED", undefined, resp.status);
1298
+ }
1299
+ const data = await resp.json();
1300
+ return data.data;
1301
+ },
1302
+ async update(id, body) {
1303
+ const resp = await apiFetch(`${baseUrl}/api/v2/providers/${id}`, {
1304
+ method: "PATCH",
1305
+ headers: { "Content-Type": "application/json" },
1306
+ body: JSON.stringify(body)
1307
+ });
1308
+ if (!resp.ok) {
1309
+ const errorData = await resp.json().catch(() => ({}));
1310
+ throw new OmniApiError(errorData.error ?? resp.statusText, "UPDATE_FAILED", undefined, resp.status);
1311
+ }
1312
+ const data = await resp.json();
1313
+ return data.data;
1314
+ },
1315
+ async delete(id) {
1316
+ const { error, response } = await client.DELETE("/providers/{id}", {
1317
+ params: { path: { id } }
1318
+ });
1319
+ throwIfError(response, error);
1320
+ },
1321
+ async checkHealth(id) {
1322
+ const { data, error, response } = await client.POST("/providers/{id}/health", {
1323
+ params: { path: { id } }
1324
+ });
1325
+ throwIfError(response, error);
1326
+ return {
1327
+ healthy: data?.healthy ?? false,
1328
+ latency: data?.latency ?? 0,
1329
+ error: data?.error ?? undefined
1330
+ };
1331
+ },
1332
+ async listAgents(id) {
1333
+ const resp = await apiFetch(`${baseUrl}/api/v2/providers/${id}/agents`, {
1334
+ method: "GET"
1335
+ });
1336
+ if (!resp.ok) {
1337
+ throw new OmniApiError(`Failed to list agents: ${resp.statusText}`, "FETCH_ERROR", undefined, resp.status);
1338
+ }
1339
+ const data = await resp.json();
1340
+ return data.items ?? [];
1341
+ },
1342
+ async listTeams(id) {
1343
+ const resp = await apiFetch(`${baseUrl}/api/v2/providers/${id}/teams`, {
1344
+ method: "GET"
1345
+ });
1346
+ if (!resp.ok) {
1347
+ throw new OmniApiError(`Failed to list teams: ${resp.statusText}`, "FETCH_ERROR", undefined, resp.status);
1348
+ }
1349
+ const data = await resp.json();
1350
+ return data.items ?? [];
1351
+ },
1352
+ async listWorkflows(id) {
1353
+ const resp = await apiFetch(`${baseUrl}/api/v2/providers/${id}/workflows`, {
1354
+ method: "GET"
1355
+ });
1356
+ if (!resp.ok) {
1357
+ throw new OmniApiError(`Failed to list workflows: ${resp.statusText}`, "FETCH_ERROR", undefined, resp.status);
1358
+ }
1359
+ const data = await resp.json();
1360
+ return data.items ?? [];
1361
+ }
1362
+ },
1363
+ routes: {
1364
+ async list(instanceId, params) {
1365
+ const { data, error, response } = await client.GET("/instances/{instanceId}/routes", {
1366
+ params: { path: { instanceId }, query: params }
1367
+ });
1368
+ throwIfError(response, error);
1369
+ return data?.items ?? [];
1370
+ },
1371
+ async get(instanceId, id) {
1372
+ const { data, error, response } = await client.GET("/instances/{instanceId}/routes/{id}", {
1373
+ params: { path: { instanceId, id } }
1374
+ });
1375
+ throwIfError(response, error);
1376
+ if (!data?.data)
1377
+ throw new OmniApiError("Route not found", "NOT_FOUND", undefined, 404);
1378
+ return data.data;
1379
+ },
1380
+ async create(instanceId, body) {
1381
+ const { data, error, response } = await client.POST("/instances/{instanceId}/routes", {
1382
+ params: { path: { instanceId } },
1383
+ body
1384
+ });
1385
+ throwIfError(response, error);
1386
+ if (!data?.data)
1387
+ throw new OmniApiError("Failed to create route", "CREATE_FAILED", undefined, response.status);
1388
+ return data.data;
1389
+ },
1390
+ async update(instanceId, id, body) {
1391
+ const { data, error, response } = await client.PATCH("/instances/{instanceId}/routes/{id}", {
1392
+ params: { path: { instanceId, id } },
1393
+ body
1394
+ });
1395
+ throwIfError(response, error);
1396
+ if (!data?.data)
1397
+ throw new OmniApiError("Failed to update route", "UPDATE_FAILED", undefined, response.status);
1398
+ return data.data;
1399
+ },
1400
+ async delete(instanceId, id) {
1401
+ const { error, response } = await client.DELETE("/instances/{instanceId}/routes/{id}", {
1402
+ params: { path: { instanceId, id } }
1403
+ });
1404
+ throwIfError(response, error);
1405
+ },
1406
+ async getMetrics() {
1407
+ const { data, error, response } = await client.GET("/routes/metrics");
1408
+ throwIfError(response, error);
1409
+ if (!data?.data)
1410
+ throw new OmniApiError("Failed to get metrics", "FETCH_ERROR", undefined, response.status);
1411
+ return data.data;
1412
+ }
1413
+ },
1414
+ logs: {
1415
+ async recent(params) {
1416
+ const { data, error, response } = await client.GET("/logs/recent", {
1417
+ params: { query: params }
1418
+ });
1419
+ throwIfError(response, error);
1420
+ return { items: data?.items ?? [], meta: data?.meta ?? { total: 0, bufferSize: 0, limit: 100 } };
1421
+ }
1422
+ },
1423
+ automations: {
1424
+ async list(params) {
1425
+ const { data, error, response } = await client.GET("/automations", {
1426
+ params: { query: params }
1427
+ });
1428
+ throwIfError(response, error);
1429
+ return data?.items ?? [];
1430
+ },
1431
+ async get(id) {
1432
+ const { data, error, response } = await client.GET("/automations/{id}", {
1433
+ params: { path: { id } }
1434
+ });
1435
+ throwIfError(response, error);
1436
+ if (!data?.data)
1437
+ throw new OmniApiError("Automation not found", "NOT_FOUND", undefined, 404);
1438
+ return data.data;
1439
+ },
1440
+ async create(body) {
1441
+ const resp = await apiFetch(`${baseUrl}/api/v2/automations`, {
1442
+ method: "POST",
1443
+ headers: { "Content-Type": "application/json" },
1444
+ body: JSON.stringify(body)
1445
+ });
1446
+ const json = await resp.json();
1447
+ if (!resp.ok)
1448
+ throw OmniApiError.from(json, resp.status);
1449
+ if (!json?.data)
1450
+ throw new OmniApiError("Failed to create automation", "CREATE_FAILED", undefined, resp.status);
1451
+ return json.data;
1452
+ },
1453
+ async update(id, body) {
1454
+ const resp = await apiFetch(`${baseUrl}/api/v2/automations/${id}`, {
1455
+ method: "PATCH",
1456
+ headers: { "Content-Type": "application/json" },
1457
+ body: JSON.stringify(body)
1458
+ });
1459
+ const json = await resp.json();
1460
+ if (!resp.ok)
1461
+ throw OmniApiError.from(json, resp.status);
1462
+ if (!json?.data)
1463
+ throw new OmniApiError("Failed to update automation", "UPDATE_FAILED", undefined, resp.status);
1464
+ return json.data;
1465
+ },
1466
+ async delete(id) {
1467
+ const { error, response } = await client.DELETE("/automations/{id}", {
1468
+ params: { path: { id } }
1469
+ });
1470
+ throwIfError(response, error);
1471
+ },
1472
+ async enable(id) {
1473
+ const { data, error, response } = await client.POST("/automations/{id}/enable", {
1474
+ params: { path: { id } }
1475
+ });
1476
+ throwIfError(response, error);
1477
+ if (!data?.data)
1478
+ throw new OmniApiError("Failed to enable automation", "ENABLE_FAILED", undefined, response.status);
1479
+ return data.data;
1480
+ },
1481
+ async disable(id) {
1482
+ const { data, error, response } = await client.POST("/automations/{id}/disable", {
1483
+ params: { path: { id } }
1484
+ });
1485
+ throwIfError(response, error);
1486
+ if (!data?.data)
1487
+ throw new OmniApiError("Failed to disable automation", "DISABLE_FAILED", undefined, response.status);
1488
+ return data.data;
1489
+ },
1490
+ async test(id, body) {
1491
+ const { data, error, response } = await client.POST("/automations/{id}/test", {
1492
+ params: { path: { id } },
1493
+ body
1494
+ });
1495
+ throwIfError(response, error);
1496
+ return data ?? { matched: false };
1497
+ },
1498
+ async execute(id, body) {
1499
+ const { data, error, response } = await client.POST("/automations/{id}/execute", {
1500
+ params: { path: { id } },
1501
+ body
1502
+ });
1503
+ throwIfError(response, error);
1504
+ return data ?? { automationId: id, triggered: false, results: [] };
1505
+ },
1506
+ async getLogs(id, params) {
1507
+ const { data, error, response } = await client.GET("/automations/{id}/logs", {
1508
+ params: { path: { id }, query: params }
1509
+ });
1510
+ throwIfError(response, error);
1511
+ return { items: data?.items ?? [], meta: data?.meta ?? { hasMore: false } };
1512
+ }
1513
+ },
1514
+ deadLetters: {
1515
+ async list(params) {
1516
+ const { data, error, response } = await client.GET("/dead-letters", {
1517
+ params: { query: params }
1518
+ });
1519
+ throwIfError(response, error);
1520
+ return { items: data?.items ?? [], meta: data?.meta ?? { hasMore: false } };
1521
+ },
1522
+ async get(id) {
1523
+ const { data, error, response } = await client.GET("/dead-letters/{id}", {
1524
+ params: { path: { id } }
1525
+ });
1526
+ throwIfError(response, error);
1527
+ if (!data?.data)
1528
+ throw new OmniApiError("Dead letter not found", "NOT_FOUND", undefined, 404);
1529
+ return data.data;
1530
+ },
1531
+ async stats() {
1532
+ const { data, error, response } = await client.GET("/dead-letters/stats");
1533
+ throwIfError(response, error);
1534
+ return data?.data ?? { pending: 0, retrying: 0, resolved: 0, abandoned: 0, total: 0 };
1535
+ },
1536
+ async retry(id) {
1537
+ const { data, error, response } = await client.POST("/dead-letters/{id}/retry", {
1538
+ params: { path: { id } }
1539
+ });
1540
+ throwIfError(response, error);
1541
+ return data ?? { success: true };
1542
+ },
1543
+ async resolve(id, body) {
1544
+ const { data, error, response } = await client.POST("/dead-letters/{id}/resolve", {
1545
+ params: { path: { id } },
1546
+ body
1547
+ });
1548
+ throwIfError(response, error);
1549
+ if (!data?.data)
1550
+ throw new OmniApiError("Failed to resolve dead letter", "RESOLVE_FAILED", undefined, response.status);
1551
+ return data.data;
1552
+ },
1553
+ async abandon(id) {
1554
+ const { data, error, response } = await client.POST("/dead-letters/{id}/abandon", {
1555
+ params: { path: { id } }
1556
+ });
1557
+ throwIfError(response, error);
1558
+ if (!data?.data)
1559
+ throw new OmniApiError("Failed to abandon dead letter", "ABANDON_FAILED", undefined, response.status);
1560
+ return data.data;
1561
+ }
1562
+ },
1563
+ eventOps: {
1564
+ async metrics() {
1565
+ const { data, error, response } = await client.GET("/event-ops/metrics");
1566
+ throwIfError(response, error);
1567
+ return data?.data ?? {};
1568
+ },
1569
+ async startReplay(body) {
1570
+ const { data, error, response } = await client.POST("/event-ops/replay", { body });
1571
+ throwIfError(response, error);
1572
+ if (!data?.data)
1573
+ throw new OmniApiError("Failed to start replay", "REPLAY_FAILED", undefined, response.status);
1574
+ return data.data;
1575
+ },
1576
+ async listReplays() {
1577
+ const { data, error, response } = await client.GET("/event-ops/replay");
1578
+ throwIfError(response, error);
1579
+ return data?.items ?? [];
1580
+ },
1581
+ async getReplay(id) {
1582
+ const { data, error, response } = await client.GET("/event-ops/replay/{id}", {
1583
+ params: { path: { id } }
1584
+ });
1585
+ throwIfError(response, error);
1586
+ if (!data?.data)
1587
+ throw new OmniApiError("Replay session not found", "NOT_FOUND", undefined, 404);
1588
+ return data.data;
1589
+ },
1590
+ async cancelReplay(id) {
1591
+ const { error, response } = await client.DELETE("/event-ops/replay/{id}", {
1592
+ params: { path: { id } }
1593
+ });
1594
+ throwIfError(response, error);
1595
+ }
1596
+ },
1597
+ webhooks: {
1598
+ async listSources(params) {
1599
+ const { data, error, response } = await client.GET("/webhook-sources", {
1600
+ params: { query: params }
1601
+ });
1602
+ throwIfError(response, error);
1603
+ return data?.items ?? [];
1604
+ },
1605
+ async getSource(id) {
1606
+ const { data, error, response } = await client.GET("/webhook-sources/{id}", {
1607
+ params: { path: { id } }
1608
+ });
1609
+ throwIfError(response, error);
1610
+ if (!data?.data)
1611
+ throw new OmniApiError("Webhook source not found", "NOT_FOUND", undefined, 404);
1612
+ return data.data;
1613
+ },
1614
+ async createSource(body) {
1615
+ const { data, error, response } = await client.POST("/webhook-sources", { body });
1616
+ throwIfError(response, error);
1617
+ if (!data?.data)
1618
+ throw new OmniApiError("Failed to create webhook source", "CREATE_FAILED", undefined, response.status);
1619
+ return data.data;
1620
+ },
1621
+ async updateSource(id, body) {
1622
+ const { data, error, response } = await client.PATCH("/webhook-sources/{id}", {
1623
+ params: { path: { id } },
1624
+ body
1625
+ });
1626
+ throwIfError(response, error);
1627
+ if (!data?.data)
1628
+ throw new OmniApiError("Failed to update webhook source", "UPDATE_FAILED", undefined, response.status);
1629
+ return data.data;
1630
+ },
1631
+ async deleteSource(id) {
1632
+ const { error, response } = await client.DELETE("/webhook-sources/{id}", {
1633
+ params: { path: { id } }
1634
+ });
1635
+ throwIfError(response, error);
1636
+ },
1637
+ async trigger(body) {
1638
+ const { data, error, response } = await client.POST("/events/trigger", { body });
1639
+ throwIfError(response, error);
1640
+ return data ?? { eventId: "", eventType: body.eventType };
1641
+ }
1642
+ },
1643
+ payloads: {
1644
+ async listForEvent(eventId) {
1645
+ const { data, error, response } = await client.GET("/events/{eventId}/payloads", {
1646
+ params: { path: { eventId } }
1647
+ });
1648
+ throwIfError(response, error);
1649
+ return data?.items ?? [];
1650
+ },
1651
+ async getStage(eventId, stage) {
1652
+ const { data, error, response } = await client.GET("/events/{eventId}/payloads/{stage}", {
1653
+ params: { path: { eventId, stage } }
1654
+ });
1655
+ throwIfError(response, error);
1656
+ if (!data?.data)
1657
+ throw new OmniApiError("Payload not found", "NOT_FOUND", undefined, 404);
1658
+ return data.data;
1659
+ },
1660
+ async delete(eventId, body) {
1661
+ const { data, error, response } = await client.DELETE("/events/{eventId}/payloads", {
1662
+ params: { path: { eventId } },
1663
+ body
1664
+ });
1665
+ throwIfError(response, error);
1666
+ return data ?? { deleted: 0 };
1667
+ },
1668
+ async listConfigs() {
1669
+ const { data, error, response } = await client.GET("/payload-config");
1670
+ throwIfError(response, error);
1671
+ return data?.items ?? [];
1672
+ },
1673
+ async updateConfig(eventType, body) {
1674
+ const { data, error, response } = await client.PUT("/payload-config/{eventType}", {
1675
+ params: { path: { eventType } },
1676
+ body
1677
+ });
1678
+ throwIfError(response, error);
1679
+ if (!data?.data)
1680
+ throw new OmniApiError("Failed to update payload config", "UPDATE_FAILED", undefined, response.status);
1681
+ return data.data;
1682
+ },
1683
+ async stats() {
1684
+ const { data, error, response } = await client.GET("/payload-stats");
1685
+ throwIfError(response, error);
1686
+ return data?.data ?? { totalPayloads: 0, totalSizeBytes: 0, byStage: {} };
1687
+ }
1688
+ },
1689
+ batchJobs: {
1690
+ async create(body) {
1691
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs`, {
1692
+ method: "POST",
1693
+ headers: { "Content-Type": "application/json" },
1694
+ body: JSON.stringify(body)
1695
+ });
1696
+ const json = await resp.json();
1697
+ if (!resp.ok)
1698
+ throw OmniApiError.from(json, resp.status);
1699
+ if (!json?.data)
1700
+ throw new OmniApiError("Failed to create batch job", "CREATE_FAILED", undefined, resp.status);
1701
+ return json.data;
1702
+ },
1703
+ async get(id) {
1704
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs/${id}`, {});
1705
+ const json = await resp.json();
1706
+ if (!resp.ok)
1707
+ throw OmniApiError.from(json, resp.status);
1708
+ if (!json?.data)
1709
+ throw new OmniApiError("Batch job not found", "NOT_FOUND", undefined, 404);
1710
+ return json.data;
1711
+ },
1712
+ async getStatus(id) {
1713
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs/${id}/status`, {});
1714
+ const json = await resp.json();
1715
+ if (!resp.ok)
1716
+ throw OmniApiError.from(json, resp.status);
1717
+ if (!json?.data)
1718
+ throw new OmniApiError("Batch job not found", "NOT_FOUND", undefined, 404);
1719
+ return json.data;
1720
+ },
1721
+ async list(params) {
1722
+ const query = new URLSearchParams;
1723
+ if (params?.instanceId)
1724
+ query.set("instanceId", params.instanceId);
1725
+ if (params?.status)
1726
+ query.set("status", params.status.join(","));
1727
+ if (params?.jobType)
1728
+ query.set("jobType", params.jobType.join(","));
1729
+ if (params?.limit)
1730
+ query.set("limit", String(params.limit));
1731
+ if (params?.cursor)
1732
+ query.set("cursor", params.cursor);
1733
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs?${query}`, {});
1734
+ const json = await resp.json();
1735
+ if (!resp.ok)
1736
+ throw OmniApiError.from(json, resp.status);
1737
+ return { items: json?.items ?? [], meta: json?.meta ?? { hasMore: false, cursor: null } };
1738
+ },
1739
+ async cancel(id) {
1740
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs/${id}/cancel`, {
1741
+ method: "POST"
1742
+ });
1743
+ const json = await resp.json();
1744
+ if (!resp.ok)
1745
+ throw OmniApiError.from(json, resp.status);
1746
+ if (!json?.data)
1747
+ throw new OmniApiError("Failed to cancel batch job", "CANCEL_FAILED", undefined, resp.status);
1748
+ return json.data;
1749
+ },
1750
+ async estimate(body) {
1751
+ const resp = await apiFetch(`${baseUrl}/api/v2/batch-jobs/estimate`, {
1752
+ method: "POST",
1753
+ headers: { "Content-Type": "application/json" },
1754
+ body: JSON.stringify(body)
1755
+ });
1756
+ const json = await resp.json();
1757
+ if (!resp.ok)
1758
+ throw OmniApiError.from(json, resp.status);
1759
+ if (!json?.data)
1760
+ throw new OmniApiError("Failed to estimate", "ESTIMATE_FAILED", undefined, resp.status);
1761
+ return json.data;
1762
+ }
1763
+ },
1764
+ keys: {
1765
+ async create(body) {
1766
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys`, {
1767
+ method: "POST",
1768
+ headers: { "Content-Type": "application/json" },
1769
+ body: JSON.stringify(body)
1770
+ });
1771
+ const json = await resp.json();
1772
+ if (!resp.ok)
1773
+ throw OmniApiError.from(json, resp.status);
1774
+ if (!json?.data)
1775
+ throw new OmniApiError("Failed to create API key", "CREATE_FAILED", undefined, resp.status);
1776
+ return json.data;
1777
+ },
1778
+ async list(params) {
1779
+ const query = new URLSearchParams;
1780
+ if (params?.status)
1781
+ query.set("status", params.status);
1782
+ if (params?.limit)
1783
+ query.set("limit", String(params.limit));
1784
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys?${query}`, {});
1785
+ const json = await resp.json();
1786
+ if (!resp.ok)
1787
+ throw OmniApiError.from(json, resp.status);
1788
+ return { items: json?.items ?? [], meta: json?.meta ?? { total: 0 } };
1789
+ },
1790
+ async get(id) {
1791
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys/${id}`, {});
1792
+ const json = await resp.json();
1793
+ if (!resp.ok)
1794
+ throw OmniApiError.from(json, resp.status);
1795
+ if (!json?.data)
1796
+ throw new OmniApiError("API key not found", "NOT_FOUND", undefined, 404);
1797
+ return json.data;
1798
+ },
1799
+ async update(id, body) {
1800
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys/${id}`, {
1801
+ method: "PATCH",
1802
+ headers: { "Content-Type": "application/json" },
1803
+ body: JSON.stringify(body)
1804
+ });
1805
+ const json = await resp.json();
1806
+ if (!resp.ok)
1807
+ throw OmniApiError.from(json, resp.status);
1808
+ if (!json?.data)
1809
+ throw new OmniApiError("Failed to update API key", "UPDATE_FAILED", undefined, resp.status);
1810
+ return json.data;
1811
+ },
1812
+ async revoke(id, body) {
1813
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys/${id}/revoke`, {
1814
+ method: "POST",
1815
+ headers: { "Content-Type": "application/json" },
1816
+ body: JSON.stringify(body ?? {})
1817
+ });
1818
+ const json = await resp.json();
1819
+ if (!resp.ok)
1820
+ throw OmniApiError.from(json, resp.status);
1821
+ if (!json?.data)
1822
+ throw new OmniApiError("Failed to revoke API key", "REVOKE_FAILED", undefined, resp.status);
1823
+ return json.data;
1824
+ },
1825
+ async delete(id) {
1826
+ const resp = await apiFetch(`${baseUrl}/api/v2/keys/${id}`, {
1827
+ method: "DELETE"
1828
+ });
1829
+ if (!resp.ok)
1830
+ throw OmniApiError.from(await resp.json(), resp.status);
1831
+ }
1832
+ },
1833
+ context: {
1834
+ async get() {
1835
+ const resp = await apiFetch(`${baseUrl}/api/v2/context`, {});
1836
+ const json = await resp.json();
1837
+ if (!resp.ok)
1838
+ throw OmniApiError.from(json, resp.status);
1839
+ return json?.data ?? { instanceId: null, chatId: null, messageId: null, activeInstanceId: null, updatedAt: null };
1840
+ },
1841
+ async set(body) {
1842
+ const resp = await apiFetch(`${baseUrl}/api/v2/context`, {
1843
+ method: "POST",
1844
+ headers: { "Content-Type": "application/json" },
1845
+ body: JSON.stringify(body)
1846
+ });
1847
+ if (!resp.ok)
1848
+ throw OmniApiError.from(await resp.json(), resp.status);
1849
+ },
1850
+ async use(instanceId) {
1851
+ const resp = await apiFetch(`${baseUrl}/api/v2/context/use`, {
1852
+ method: "POST",
1853
+ headers: { "Content-Type": "application/json" },
1854
+ body: JSON.stringify({ instanceId })
1855
+ });
1856
+ if (!resp.ok)
1857
+ throw OmniApiError.from(await resp.json(), resp.status);
1858
+ },
1859
+ async clear() {
1860
+ const resp = await apiFetch(`${baseUrl}/api/v2/context`, {
1861
+ method: "DELETE"
1862
+ });
1863
+ if (!resp.ok)
1864
+ throw OmniApiError.from(await resp.json(), resp.status);
1865
+ }
1866
+ },
1867
+ media: {
1868
+ async tts(body) {
1869
+ const resp = await apiFetch(`${baseUrl}/api/v2/media/tts`, {
1870
+ method: "POST",
1871
+ headers: { "Content-Type": "application/json" },
1872
+ body: JSON.stringify(body)
1873
+ });
1874
+ const json = await resp.json();
1875
+ if (!resp.ok)
1876
+ throw OmniApiError.from(json, resp.status);
1877
+ const data = json?.data;
1878
+ if (!data) {
1879
+ throw new OmniApiError("TTS response missing data", "INVALID_RESPONSE", undefined, 500);
1880
+ }
1881
+ return {
1882
+ provider: data.provider,
1883
+ mimeType: data.mimeType,
1884
+ durationMs: data.durationMs,
1885
+ sizeBytes: data.sizeBytes,
1886
+ audio: Buffer.from(data.audioBase64, "base64")
1887
+ };
1888
+ },
1889
+ async stt(body) {
1890
+ const resp = await apiFetch(`${baseUrl}/api/v2/media/stt`, {
1891
+ method: "POST",
1892
+ headers: { "Content-Type": "application/json" },
1893
+ body: JSON.stringify({
1894
+ audioBase64: body.audio.toString("base64"),
1895
+ mimeType: body.mimeType,
1896
+ provider: body.provider,
1897
+ language: body.language,
1898
+ timestamps: body.timestamps,
1899
+ model: body.model
1900
+ })
1901
+ });
1902
+ const json = await resp.json();
1903
+ if (!resp.ok)
1904
+ throw OmniApiError.from(json, resp.status);
1905
+ const data = json?.data;
1906
+ if (!data) {
1907
+ throw new OmniApiError("STT response missing data", "INVALID_RESPONSE", undefined, 500);
1908
+ }
1909
+ return {
1910
+ provider: data.provider,
1911
+ text: data.text,
1912
+ segments: data.segments,
1913
+ detectedLanguage: data.detectedLanguage,
1914
+ processingMs: data.processingMs
1915
+ };
1916
+ },
1917
+ async imagine(body) {
1918
+ const resp = await apiFetch(`${baseUrl}/api/v2/media/imagine`, {
1919
+ method: "POST",
1920
+ headers: { "Content-Type": "application/json" },
1921
+ body: JSON.stringify(body)
1922
+ });
1923
+ const json = await resp.json();
1924
+ if (!resp.ok)
1925
+ throw OmniApiError.from(json, resp.status);
1926
+ const data = json?.data;
1927
+ if (!data) {
1928
+ throw new OmniApiError("Imagine response missing data", "INVALID_RESPONSE", undefined, 500);
1929
+ }
1930
+ return data;
1931
+ },
1932
+ async vision(body) {
1933
+ const resp = await apiFetch(`${baseUrl}/api/v2/media/vision`, {
1934
+ method: "POST",
1935
+ headers: { "Content-Type": "application/json" },
1936
+ body: JSON.stringify({
1937
+ mediaBase64: body.media.toString("base64"),
1938
+ mimeType: body.mimeType,
1939
+ provider: body.provider,
1940
+ prompt: body.prompt,
1941
+ language: body.language,
1942
+ maxTokens: body.maxTokens
1943
+ })
1944
+ });
1945
+ const json = await resp.json();
1946
+ if (!resp.ok)
1947
+ throw OmniApiError.from(json, resp.status);
1948
+ const data = json?.data;
1949
+ if (!data) {
1950
+ throw new OmniApiError("Vision response missing data", "INVALID_RESPONSE", undefined, 500);
1951
+ }
1952
+ return {
1953
+ provider: data.provider,
1954
+ text: data.text,
1955
+ processingMs: data.processingMs
1956
+ };
1957
+ },
1958
+ async film(body) {
1959
+ const resp = await apiFetch(`${baseUrl}/api/v2/media/film`, {
1960
+ method: "POST",
1961
+ headers: { "Content-Type": "application/json" },
1962
+ body: JSON.stringify(body)
1963
+ });
1964
+ const json = await resp.json();
1965
+ if (!resp.ok)
1966
+ throw OmniApiError.from(json, resp.status);
1967
+ const data = json?.data;
1968
+ if (!data) {
1969
+ throw new OmniApiError("Film response missing data", "INVALID_RESPONSE", undefined, 500);
1970
+ }
1971
+ return data;
1972
+ }
1973
+ },
1974
+ turns: {
1975
+ async close(body, extraHeaders) {
1976
+ const resp = await apiFetch(`${baseUrl}/api/v2/turns/close`, {
1977
+ method: "POST",
1978
+ headers: { "Content-Type": "application/json", ...extraHeaders },
1979
+ body: JSON.stringify(body)
1980
+ });
1981
+ const json = await resp.json();
1982
+ if (!resp.ok)
1983
+ throw OmniApiError.from(json, resp.status);
1984
+ return json?.data ?? {};
1985
+ },
1986
+ async list(params) {
1987
+ const qs = new URLSearchParams;
1988
+ if (params?.status)
1989
+ qs.set("status", params.status);
1990
+ if (params?.instanceId)
1991
+ qs.set("instanceId", params.instanceId);
1992
+ if (params?.chatId)
1993
+ qs.set("chatId", params.chatId);
1994
+ if (params?.agentId)
1995
+ qs.set("agentId", params.agentId);
1996
+ if (params?.limit !== undefined)
1997
+ qs.set("limit", String(params.limit));
1998
+ if (params?.offset !== undefined)
1999
+ qs.set("offset", String(params.offset));
2000
+ const query = qs.toString();
2001
+ const url = `${baseUrl}/api/v2/turns${query ? `?${query}` : ""}`;
2002
+ const resp = await apiFetch(url);
2003
+ const json = await resp.json();
2004
+ if (!resp.ok)
2005
+ throw OmniApiError.from(json, resp.status);
2006
+ return json?.data ?? { items: [], total: 0, limit: 50, offset: 0 };
2007
+ },
2008
+ async get(id) {
2009
+ const resp = await apiFetch(`${baseUrl}/api/v2/turns/${id}`);
2010
+ const json = await resp.json();
2011
+ if (!resp.ok)
2012
+ throw OmniApiError.from(json, resp.status);
2013
+ if (!json?.data)
2014
+ throw new OmniApiError("Turn not found", "NOT_FOUND", undefined, 404);
2015
+ return json.data;
2016
+ },
2017
+ async forceClose(id, reason) {
2018
+ const resp = await apiFetch(`${baseUrl}/api/v2/turns/${id}/close`, {
2019
+ method: "POST",
2020
+ headers: { "Content-Type": "application/json" },
2021
+ body: JSON.stringify({ reason })
2022
+ });
2023
+ const json = await resp.json();
2024
+ if (!resp.ok)
2025
+ throw OmniApiError.from(json, resp.status);
2026
+ if (!json?.data)
2027
+ throw new OmniApiError("Turn not found or already closed", "NOT_FOUND", undefined, 404);
2028
+ return json.data;
2029
+ },
2030
+ async bulkClose(reason) {
2031
+ const resp = await apiFetch(`${baseUrl}/api/v2/turns/close-all`, {
2032
+ method: "POST",
2033
+ headers: { "Content-Type": "application/json" },
2034
+ body: JSON.stringify({ confirm: true, reason })
2035
+ });
2036
+ const json = await resp.json();
2037
+ if (!resp.ok)
2038
+ throw OmniApiError.from(json, resp.status);
2039
+ if (!json?.data)
2040
+ throw new OmniApiError("Bulk close failed", "INTERNAL_ERROR", undefined, 500);
2041
+ return json.data;
2042
+ },
2043
+ async stats() {
2044
+ const resp = await apiFetch(`${baseUrl}/api/v2/turns/stats`);
2045
+ const json = await resp.json();
2046
+ if (!resp.ok)
2047
+ throw OmniApiError.from(json, resp.status);
2048
+ if (!json?.data)
2049
+ throw new OmniApiError("Stats unavailable", "INTERNAL_ERROR", undefined, 500);
2050
+ return json.data;
2051
+ }
2052
+ },
2053
+ agents: {
2054
+ async list(params) {
2055
+ const { data, error, response } = await client.GET("/agents", {
2056
+ params: { query: params }
2057
+ });
2058
+ throwIfError(response, error);
2059
+ return {
2060
+ items: data?.items ?? [],
2061
+ meta: data?.meta ?? { hasMore: false, cursor: null }
2062
+ };
2063
+ },
2064
+ async get(id) {
2065
+ const { data, error, response } = await client.GET("/agents/{id}", {
2066
+ params: { path: { id } }
2067
+ });
2068
+ throwIfError(response, error);
2069
+ if (!data?.data)
2070
+ throw new OmniApiError("Agent not found", "NOT_FOUND", undefined, 404);
2071
+ return data.data;
2072
+ },
2073
+ async create(body) {
2074
+ const { data, error, response } = await client.POST("/agents", {
2075
+ body
2076
+ });
2077
+ throwIfError(response, error);
2078
+ if (!data?.data)
2079
+ throw new OmniApiError("Failed to create agent", "CREATE_FAILED", undefined, response.status);
2080
+ return data.data;
2081
+ },
2082
+ async update(id, body) {
2083
+ const { data, error, response } = await client.PATCH("/agents/{id}", {
2084
+ params: { path: { id } },
2085
+ body
2086
+ });
2087
+ throwIfError(response, error);
2088
+ if (!data?.data)
2089
+ throw new OmniApiError("Failed to update agent", "UPDATE_FAILED", undefined, response.status);
2090
+ return data.data;
2091
+ },
2092
+ async delete(id) {
2093
+ const { error, response } = await client.DELETE("/agents/{id}", {
2094
+ params: { path: { id } }
2095
+ });
2096
+ throwIfError(response, error);
2097
+ }
2098
+ },
2099
+ followUp: {
2100
+ async getAgent(id) {
2101
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/agents/${id}`);
2102
+ const json = await resp.json();
2103
+ if (!resp.ok)
2104
+ throw OmniApiError.from(json, resp.status);
2105
+ return json?.data ?? null;
2106
+ },
2107
+ async setAgent(id, config2) {
2108
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/agents/${id}`, {
2109
+ method: "PUT",
2110
+ headers: { "Content-Type": "application/json" },
2111
+ body: JSON.stringify(config2)
2112
+ });
2113
+ const json = await resp.json();
2114
+ if (!resp.ok)
2115
+ throw OmniApiError.from(json, resp.status);
2116
+ return json?.data ?? null;
2117
+ },
2118
+ async unsetAgent(id) {
2119
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/agents/${id}`, { method: "DELETE" });
2120
+ if (!resp.ok)
2121
+ throw OmniApiError.from(await resp.json().catch(() => ({})), resp.status);
2122
+ },
2123
+ async getInstance(id) {
2124
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/instances/${id}`);
2125
+ const json = await resp.json();
2126
+ if (!resp.ok)
2127
+ throw OmniApiError.from(json, resp.status);
2128
+ return json?.data ?? null;
2129
+ },
2130
+ async setInstance(id, config2) {
2131
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/instances/${id}`, {
2132
+ method: "PUT",
2133
+ headers: { "Content-Type": "application/json" },
2134
+ body: JSON.stringify(config2)
2135
+ });
2136
+ const json = await resp.json();
2137
+ if (!resp.ok)
2138
+ throw OmniApiError.from(json, resp.status);
2139
+ return json?.data ?? null;
2140
+ },
2141
+ async unsetInstance(id) {
2142
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/instances/${id}`, { method: "DELETE" });
2143
+ if (!resp.ok)
2144
+ throw OmniApiError.from(await resp.json().catch(() => ({})), resp.status);
2145
+ },
2146
+ async getChat(id) {
2147
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/chats/${id}`);
2148
+ const json = await resp.json();
2149
+ if (!resp.ok)
2150
+ throw OmniApiError.from(json, resp.status);
2151
+ return json?.data ?? null;
2152
+ },
2153
+ async setChat(id, config2) {
2154
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/chats/${id}`, {
2155
+ method: "PUT",
2156
+ headers: { "Content-Type": "application/json" },
2157
+ body: JSON.stringify(config2)
2158
+ });
2159
+ const json = await resp.json();
2160
+ if (!resp.ok)
2161
+ throw OmniApiError.from(json, resp.status);
2162
+ return json?.data ?? null;
2163
+ },
2164
+ async unsetChat(id) {
2165
+ const resp = await apiFetch(`${baseUrl}/api/v2/follow-up/chats/${id}`, { method: "DELETE" });
2166
+ if (!resp.ok)
2167
+ throw OmniApiError.from(await resp.json().catch(() => ({})), resp.status);
2168
+ }
2169
+ },
2170
+ system: {
2171
+ async health() {
2172
+ const healthClient = createClient({ baseUrl: `${baseUrl}/api/v2` });
2173
+ const noCompressionMiddleware = {
2174
+ async onRequest({ request }) {
2175
+ request.headers.set("Accept-Encoding", "identity");
2176
+ if (config.cliVersion) {
2177
+ request.headers.set("x-omni-cli-version", config.cliVersion);
2178
+ }
2179
+ return request;
2180
+ }
2181
+ };
2182
+ healthClient.use(noCompressionMiddleware);
2183
+ const { data, error, response } = await healthClient.GET("/health");
2184
+ throwIfError(response, error);
2185
+ return data ?? { status: "unhealthy" };
2186
+ }
2187
+ },
2188
+ raw: client
2189
+ };
2190
+ }
2191
+
2192
+ // src/index.ts
2193
+ var VERSION = "0.0.1";
2194
+ export {
2195
+ createOmniClient,
2196
+ VERSION,
2197
+ OmniConfigError,
2198
+ OmniApiError
2199
+ };