olakai-cli 0.6.2 → 0.6.5

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,876 @@
1
+ import {
2
+ CLIENT_ID,
3
+ getBaseUrl,
4
+ getValidToken
5
+ } from "./chunk-AVB4N2UN.js";
6
+
7
+ // src/lib/api.ts
8
+ async function requestDeviceCode() {
9
+ const response = await fetch(`${getBaseUrl()}/api/auth/device/code`, {
10
+ method: "POST",
11
+ headers: {
12
+ "Content-Type": "application/json"
13
+ },
14
+ body: JSON.stringify({ client_id: CLIENT_ID })
15
+ });
16
+ if (!response.ok) {
17
+ const error = await response.json();
18
+ throw new Error(error.error_description || error.error || "Failed to request device code");
19
+ }
20
+ return await response.json();
21
+ }
22
+ async function pollForToken(deviceCode) {
23
+ const response = await fetch(`${getBaseUrl()}/api/auth/device/token`, {
24
+ method: "POST",
25
+ headers: {
26
+ "Content-Type": "application/json"
27
+ },
28
+ body: JSON.stringify({
29
+ grant_type: "urn:ietf:params:oauth:grant-type:device_code",
30
+ device_code: deviceCode,
31
+ client_id: CLIENT_ID
32
+ })
33
+ });
34
+ const data = await response.json();
35
+ if (!response.ok) {
36
+ if ("error" in data && data.error === "authorization_pending") {
37
+ return null;
38
+ }
39
+ const errorMsg = "error_description" in data ? data.error_description : "error" in data ? data.error : "Token exchange failed";
40
+ throw new Error(errorMsg || "Token exchange failed");
41
+ }
42
+ return data;
43
+ }
44
+ async function getCurrentUser() {
45
+ const token = getValidToken();
46
+ if (!token) {
47
+ throw new Error("Not logged in. Run 'olakai login' first.");
48
+ }
49
+ const response = await fetch(`${getBaseUrl()}/api/user/me`, {
50
+ headers: {
51
+ Authorization: `Bearer ${token}`
52
+ }
53
+ });
54
+ if (!response.ok) {
55
+ if (response.status === 401) {
56
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
57
+ }
58
+ throw new Error("Failed to get user info");
59
+ }
60
+ return await response.json();
61
+ }
62
+ async function listAgents(options) {
63
+ const token = getValidToken();
64
+ if (!token) {
65
+ throw new Error("Not logged in. Run 'olakai login' first.");
66
+ }
67
+ const params = new URLSearchParams();
68
+ if (options?.includeKpis) {
69
+ params.set("includeKpis", "true");
70
+ }
71
+ const url = `${getBaseUrl()}/api/config/agents${params.toString() ? `?${params}` : ""}`;
72
+ const response = await fetch(url, {
73
+ headers: {
74
+ Authorization: `Bearer ${token}`
75
+ }
76
+ });
77
+ if (!response.ok) {
78
+ if (response.status === 401) {
79
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
80
+ }
81
+ const error = await response.json();
82
+ throw new Error(error.error || "Failed to list agents");
83
+ }
84
+ const data = await response.json();
85
+ return data.agents;
86
+ }
87
+ async function getAgent(id) {
88
+ const token = getValidToken();
89
+ if (!token) {
90
+ throw new Error("Not logged in. Run 'olakai login' first.");
91
+ }
92
+ const response = await fetch(`${getBaseUrl()}/api/config/agents/${id}`, {
93
+ headers: {
94
+ Authorization: `Bearer ${token}`
95
+ }
96
+ });
97
+ if (!response.ok) {
98
+ if (response.status === 401) {
99
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
100
+ }
101
+ if (response.status === 404) {
102
+ throw new Error("Agent not found");
103
+ }
104
+ const error = await response.json();
105
+ throw new Error(error.error || "Failed to get agent");
106
+ }
107
+ return await response.json();
108
+ }
109
+ async function createAgent(payload) {
110
+ const token = getValidToken();
111
+ if (!token) {
112
+ throw new Error("Not logged in. Run 'olakai login' first.");
113
+ }
114
+ const response = await fetch(`${getBaseUrl()}/api/config/agents`, {
115
+ method: "POST",
116
+ headers: {
117
+ Authorization: `Bearer ${token}`,
118
+ "Content-Type": "application/json"
119
+ },
120
+ body: JSON.stringify(payload)
121
+ });
122
+ if (!response.ok) {
123
+ if (response.status === 401) {
124
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
125
+ }
126
+ const errorBody = await response.json().catch(() => ({}));
127
+ if (response.status === 403) {
128
+ throw new Error(
129
+ "Your role can't create monitoring agents on this workspace. Ask an admin to enable employee self-monitoring (or create the agent for you)."
130
+ );
131
+ }
132
+ if (response.status === 400 && errorBody.error === "self_monitor_admin_field_forbidden") {
133
+ const fields = errorBody.forbiddenFields?.join(", ") ?? "admin-only";
134
+ throw new Error(
135
+ `Self-monitoring agents cannot be attached to admin-curated structures (${fields}). The CLI should be sending only name/description/role/source/createApiKey \u2014 this is a CLI bug, please report it.`
136
+ );
137
+ }
138
+ if ((response.status === 402 || response.status === 403) && errorBody.error === "tier_agent_limit_reached") {
139
+ throw new Error(
140
+ errorBody.message ?? `Your account is at its agent limit${errorBody.limit ? ` (${errorBody.limit}/${errorBody.limit} on ${errorBody.tier ?? "FREE"} tier)` : ""}. Ask your admin to upgrade or remove unused agents.`
141
+ );
142
+ }
143
+ if (response.status === 409 && errorBody.error === "name_conflict") {
144
+ throw new Error(
145
+ errorBody.message ?? "An agent with this name already exists on this account. Pick a different name and retry."
146
+ );
147
+ }
148
+ if (response.status === 409) {
149
+ throw new Error("An agent with this name already exists");
150
+ }
151
+ if (response.status === 429) {
152
+ throw new Error(
153
+ errorBody.message ?? "Too many agent-creation requests. Try again in a minute."
154
+ );
155
+ }
156
+ throw new Error(errorBody.error || errorBody.message || "Failed to create agent");
157
+ }
158
+ return await response.json();
159
+ }
160
+ async function listMyAgents(filter) {
161
+ const token = getValidToken();
162
+ if (!token) {
163
+ throw new Error("Not logged in. Run 'olakai login' first.");
164
+ }
165
+ const params = new URLSearchParams();
166
+ if (filter?.source) params.set("source", filter.source);
167
+ if (filter?.name) params.set("name", filter.name);
168
+ const qs = params.toString();
169
+ const url = `${getBaseUrl()}/api/config/agents/mine${qs ? `?${qs}` : ""}`;
170
+ const response = await fetch(url, {
171
+ method: "GET",
172
+ headers: { Authorization: `Bearer ${token}` }
173
+ });
174
+ if (!response.ok) {
175
+ if (response.status === 401) {
176
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
177
+ }
178
+ if (response.status === 403) {
179
+ throw new Error("Your role can't list agents on this workspace.");
180
+ }
181
+ const errorBody = await response.json().catch(() => ({}));
182
+ throw new Error(errorBody.error || "Failed to list your agents");
183
+ }
184
+ const data = await response.json();
185
+ return data.agents;
186
+ }
187
+ async function regenerateAgentApiKey(agentId) {
188
+ const token = getValidToken();
189
+ if (!token) {
190
+ throw new Error("Not logged in. Run 'olakai login' first.");
191
+ }
192
+ const response = await fetch(
193
+ `${getBaseUrl()}/api/config/agents/${agentId}/regenerate-api-key`,
194
+ {
195
+ method: "POST",
196
+ headers: { Authorization: `Bearer ${token}` }
197
+ }
198
+ );
199
+ if (!response.ok) {
200
+ if (response.status === 401) {
201
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
202
+ }
203
+ if (response.status === 403) {
204
+ throw new Error(
205
+ "Only the agent's creator or an ADMIN may rotate its API key."
206
+ );
207
+ }
208
+ if (response.status === 404) {
209
+ throw new Error("Agent not found.");
210
+ }
211
+ if (response.status === 429) {
212
+ throw new Error("Too many key-rotation requests. Try again in a minute.");
213
+ }
214
+ const errorBody = await response.json().catch(() => ({}));
215
+ throw new Error(errorBody.error || "Failed to regenerate API key");
216
+ }
217
+ return await response.json();
218
+ }
219
+ async function updateAgent(id, payload) {
220
+ const token = getValidToken();
221
+ if (!token) {
222
+ throw new Error("Not logged in. Run 'olakai login' first.");
223
+ }
224
+ const response = await fetch(`${getBaseUrl()}/api/config/agents/${id}`, {
225
+ method: "PUT",
226
+ headers: {
227
+ Authorization: `Bearer ${token}`,
228
+ "Content-Type": "application/json"
229
+ },
230
+ body: JSON.stringify(payload)
231
+ });
232
+ if (!response.ok) {
233
+ if (response.status === 401) {
234
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
235
+ }
236
+ if (response.status === 404) {
237
+ throw new Error("Agent not found");
238
+ }
239
+ if (response.status === 409) {
240
+ throw new Error("An agent with this name already exists");
241
+ }
242
+ const error = await response.json();
243
+ throw new Error(error.error || "Failed to update agent");
244
+ }
245
+ return await response.json();
246
+ }
247
+ async function deleteAgent(id) {
248
+ const token = getValidToken();
249
+ if (!token) {
250
+ throw new Error("Not logged in. Run 'olakai login' first.");
251
+ }
252
+ const response = await fetch(`${getBaseUrl()}/api/config/agents/${id}`, {
253
+ method: "DELETE",
254
+ headers: {
255
+ Authorization: `Bearer ${token}`
256
+ }
257
+ });
258
+ if (!response.ok) {
259
+ if (response.status === 401) {
260
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
261
+ }
262
+ if (response.status === 403) {
263
+ throw new Error(
264
+ "Only the agent's creator or an ADMIN may delete it."
265
+ );
266
+ }
267
+ if (response.status === 404) {
268
+ throw new Error("Agent not found");
269
+ }
270
+ const error = await response.json().catch(() => ({}));
271
+ throw new Error(error.error || "Failed to delete agent");
272
+ }
273
+ }
274
+ async function listWorkflows(options) {
275
+ const token = getValidToken();
276
+ if (!token) {
277
+ throw new Error("Not logged in. Run 'olakai login' first.");
278
+ }
279
+ const params = new URLSearchParams();
280
+ if (options?.includeAgents) {
281
+ params.set("includeAgents", "true");
282
+ }
283
+ if (options?.includeInactive) {
284
+ params.set("includeInactive", "true");
285
+ }
286
+ const url = `${getBaseUrl()}/api/config/workflows${params.toString() ? `?${params}` : ""}`;
287
+ const response = await fetch(url, {
288
+ headers: {
289
+ Authorization: `Bearer ${token}`
290
+ }
291
+ });
292
+ if (!response.ok) {
293
+ if (response.status === 401) {
294
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
295
+ }
296
+ const error = await response.json();
297
+ throw new Error(error.error || "Failed to list workflows");
298
+ }
299
+ const data = await response.json();
300
+ return data.workflows;
301
+ }
302
+ async function getWorkflow(id) {
303
+ const token = getValidToken();
304
+ if (!token) {
305
+ throw new Error("Not logged in. Run 'olakai login' first.");
306
+ }
307
+ const response = await fetch(`${getBaseUrl()}/api/config/workflows/${id}`, {
308
+ headers: {
309
+ Authorization: `Bearer ${token}`
310
+ }
311
+ });
312
+ if (!response.ok) {
313
+ if (response.status === 401) {
314
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
315
+ }
316
+ if (response.status === 404) {
317
+ throw new Error("Workflow not found");
318
+ }
319
+ const error = await response.json();
320
+ throw new Error(error.error || "Failed to get workflow");
321
+ }
322
+ return await response.json();
323
+ }
324
+ async function createWorkflow(payload) {
325
+ const token = getValidToken();
326
+ if (!token) {
327
+ throw new Error("Not logged in. Run 'olakai login' first.");
328
+ }
329
+ const response = await fetch(`${getBaseUrl()}/api/config/workflows`, {
330
+ method: "POST",
331
+ headers: {
332
+ Authorization: `Bearer ${token}`,
333
+ "Content-Type": "application/json"
334
+ },
335
+ body: JSON.stringify(payload)
336
+ });
337
+ if (!response.ok) {
338
+ if (response.status === 401) {
339
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
340
+ }
341
+ if (response.status === 409) {
342
+ throw new Error("A workflow with this name already exists");
343
+ }
344
+ const error = await response.json();
345
+ throw new Error(error.error || "Failed to create workflow");
346
+ }
347
+ return await response.json();
348
+ }
349
+ async function updateWorkflow(id, payload) {
350
+ const token = getValidToken();
351
+ if (!token) {
352
+ throw new Error("Not logged in. Run 'olakai login' first.");
353
+ }
354
+ const response = await fetch(`${getBaseUrl()}/api/config/workflows/${id}`, {
355
+ method: "PUT",
356
+ headers: {
357
+ Authorization: `Bearer ${token}`,
358
+ "Content-Type": "application/json"
359
+ },
360
+ body: JSON.stringify(payload)
361
+ });
362
+ if (!response.ok) {
363
+ if (response.status === 401) {
364
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
365
+ }
366
+ if (response.status === 404) {
367
+ throw new Error("Workflow not found");
368
+ }
369
+ if (response.status === 409) {
370
+ throw new Error("A workflow with this name already exists");
371
+ }
372
+ const error = await response.json();
373
+ throw new Error(error.error || "Failed to update workflow");
374
+ }
375
+ return await response.json();
376
+ }
377
+ async function deleteWorkflow(id) {
378
+ const token = getValidToken();
379
+ if (!token) {
380
+ throw new Error("Not logged in. Run 'olakai login' first.");
381
+ }
382
+ const response = await fetch(`${getBaseUrl()}/api/config/workflows/${id}`, {
383
+ method: "DELETE",
384
+ headers: {
385
+ Authorization: `Bearer ${token}`
386
+ }
387
+ });
388
+ if (!response.ok) {
389
+ if (response.status === 401) {
390
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
391
+ }
392
+ if (response.status === 404) {
393
+ throw new Error("Workflow not found");
394
+ }
395
+ const error = await response.json();
396
+ throw new Error(error.error || "Failed to delete workflow");
397
+ }
398
+ }
399
+ async function listKpis(options) {
400
+ const token = getValidToken();
401
+ if (!token) {
402
+ throw new Error("Not logged in. Run 'olakai login' first.");
403
+ }
404
+ const params = new URLSearchParams();
405
+ if (options?.agentId) {
406
+ params.set("agentId", options.agentId);
407
+ }
408
+ if (options?.includeInactive) {
409
+ params.set("includeInactive", "true");
410
+ }
411
+ const url = `${getBaseUrl()}/api/config/kpis${params.toString() ? `?${params}` : ""}`;
412
+ const response = await fetch(url, {
413
+ headers: {
414
+ Authorization: `Bearer ${token}`
415
+ }
416
+ });
417
+ if (!response.ok) {
418
+ if (response.status === 401) {
419
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
420
+ }
421
+ const error = await response.json();
422
+ throw new Error(error.error || "Failed to list KPIs");
423
+ }
424
+ const data = await response.json();
425
+ return data.kpiDefinitions;
426
+ }
427
+ async function getKpi(id) {
428
+ const token = getValidToken();
429
+ if (!token) {
430
+ throw new Error("Not logged in. Run 'olakai login' first.");
431
+ }
432
+ const response = await fetch(`${getBaseUrl()}/api/config/kpis/${id}`, {
433
+ headers: {
434
+ Authorization: `Bearer ${token}`
435
+ }
436
+ });
437
+ if (!response.ok) {
438
+ if (response.status === 401) {
439
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
440
+ }
441
+ if (response.status === 404) {
442
+ throw new Error("KPI definition not found");
443
+ }
444
+ const error = await response.json();
445
+ throw new Error(error.error || "Failed to get KPI");
446
+ }
447
+ return await response.json();
448
+ }
449
+ async function createKpi(payload) {
450
+ const token = getValidToken();
451
+ if (!token) {
452
+ throw new Error("Not logged in. Run 'olakai login' first.");
453
+ }
454
+ const response = await fetch(`${getBaseUrl()}/api/config/kpis`, {
455
+ method: "POST",
456
+ headers: {
457
+ Authorization: `Bearer ${token}`,
458
+ "Content-Type": "application/json"
459
+ },
460
+ body: JSON.stringify(payload)
461
+ });
462
+ if (!response.ok) {
463
+ if (response.status === 401) {
464
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
465
+ }
466
+ if (response.status === 409) {
467
+ throw new Error("A KPI definition with this name already exists");
468
+ }
469
+ const error = await response.json();
470
+ throw new Error(error.error || "Failed to create KPI");
471
+ }
472
+ return await response.json();
473
+ }
474
+ async function updateKpi(id, payload) {
475
+ const token = getValidToken();
476
+ if (!token) {
477
+ throw new Error("Not logged in. Run 'olakai login' first.");
478
+ }
479
+ const response = await fetch(`${getBaseUrl()}/api/config/kpis/${id}`, {
480
+ method: "PUT",
481
+ headers: {
482
+ Authorization: `Bearer ${token}`,
483
+ "Content-Type": "application/json"
484
+ },
485
+ body: JSON.stringify(payload)
486
+ });
487
+ if (!response.ok) {
488
+ if (response.status === 401) {
489
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
490
+ }
491
+ if (response.status === 404) {
492
+ throw new Error("KPI definition not found");
493
+ }
494
+ if (response.status === 409) {
495
+ throw new Error("A KPI definition with this name already exists");
496
+ }
497
+ const error = await response.json();
498
+ throw new Error(error.error || "Failed to update KPI");
499
+ }
500
+ return await response.json();
501
+ }
502
+ async function deleteKpi(id) {
503
+ const token = getValidToken();
504
+ if (!token) {
505
+ throw new Error("Not logged in. Run 'olakai login' first.");
506
+ }
507
+ const response = await fetch(`${getBaseUrl()}/api/config/kpis/${id}`, {
508
+ method: "DELETE",
509
+ headers: {
510
+ Authorization: `Bearer ${token}`
511
+ }
512
+ });
513
+ if (!response.ok) {
514
+ if (response.status === 401) {
515
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
516
+ }
517
+ if (response.status === 404) {
518
+ throw new Error("KPI definition not found");
519
+ }
520
+ const error = await response.json();
521
+ throw new Error(error.error || "Failed to delete KPI");
522
+ }
523
+ }
524
+ async function getKpiContextVariables(agentId, scope) {
525
+ const token = getValidToken();
526
+ if (!token) {
527
+ throw new Error("Not logged in. Run 'olakai login' first.");
528
+ }
529
+ const params = new URLSearchParams();
530
+ if (agentId) {
531
+ params.set("agentId", agentId);
532
+ }
533
+ if (scope) {
534
+ params.set("scope", scope);
535
+ }
536
+ const url = `${getBaseUrl()}/api/config/kpis/context-variables${params.toString() ? `?${params}` : ""}`;
537
+ const response = await fetch(url, {
538
+ headers: {
539
+ Authorization: `Bearer ${token}`
540
+ }
541
+ });
542
+ if (!response.ok) {
543
+ if (response.status === 401) {
544
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
545
+ }
546
+ const error = await response.json();
547
+ throw new Error(error.error || "Failed to get context variables");
548
+ }
549
+ const data = await response.json();
550
+ return data.contextVariables;
551
+ }
552
+ async function validateKpiFormula(formula, agentId, scope) {
553
+ const token = getValidToken();
554
+ if (!token) {
555
+ throw new Error("Not logged in. Run 'olakai login' first.");
556
+ }
557
+ const body = { formula, agentId };
558
+ if (scope) {
559
+ body.scope = scope;
560
+ }
561
+ const response = await fetch(`${getBaseUrl()}/api/config/kpis/validate`, {
562
+ method: "POST",
563
+ headers: {
564
+ Authorization: `Bearer ${token}`,
565
+ "Content-Type": "application/json"
566
+ },
567
+ body: JSON.stringify(body)
568
+ });
569
+ if (!response.ok) {
570
+ if (response.status === 401) {
571
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
572
+ }
573
+ const error = await response.json();
574
+ throw new Error(error.error || "Failed to validate formula");
575
+ }
576
+ return await response.json();
577
+ }
578
+ async function validateMonitoringApiKey(monitoringEndpoint, apiKey) {
579
+ const normalizedEndpoint = monitoringEndpoint.replace(/\/+$/, "");
580
+ const url = `${normalizedEndpoint}/me`;
581
+ const controller = new AbortController();
582
+ const timeout = setTimeout(() => controller.abort(), 5e3);
583
+ try {
584
+ const response = await fetch(url, {
585
+ method: "GET",
586
+ headers: {
587
+ "x-api-key": apiKey
588
+ },
589
+ signal: controller.signal
590
+ });
591
+ if (!response.ok) {
592
+ return null;
593
+ }
594
+ const data = await response.json();
595
+ if (typeof data?.accountId !== "string" || typeof data?.apiKeyId !== "string") {
596
+ return null;
597
+ }
598
+ return { accountId: data.accountId, apiKeyId: data.apiKeyId };
599
+ } catch {
600
+ return null;
601
+ } finally {
602
+ clearTimeout(timeout);
603
+ }
604
+ }
605
+ async function listActivity(options = {}) {
606
+ const token = getValidToken();
607
+ if (!token) {
608
+ throw new Error("Not logged in. Run 'olakai login' first.");
609
+ }
610
+ const params = new URLSearchParams();
611
+ if (options.agentId) params.set("agentId", options.agentId);
612
+ if (options.workflowId) params.set("workflowId", options.workflowId);
613
+ if (options.since) params.set("since", options.since);
614
+ if (options.until) params.set("until", options.until);
615
+ if (options.limit !== void 0) params.set("limit", String(options.limit));
616
+ if (options.offset !== void 0) params.set("offset", String(options.offset));
617
+ if (options.includeContent) params.set("includeContent", "true");
618
+ if (options.includeAnalytics) params.set("includeAnalytics", "true");
619
+ const url = `${getBaseUrl()}/api/activity/prompts${params.toString() ? `?${params}` : ""}`;
620
+ const response = await fetch(url, {
621
+ headers: {
622
+ Authorization: `Bearer ${token}`
623
+ }
624
+ });
625
+ if (!response.ok) {
626
+ if (response.status === 401) {
627
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
628
+ }
629
+ const error = await response.json();
630
+ throw new Error(error.error || "Failed to list activity");
631
+ }
632
+ return await response.json();
633
+ }
634
+ async function getActivity(id, includeContent) {
635
+ const token = getValidToken();
636
+ if (!token) {
637
+ throw new Error("Not logged in. Run 'olakai login' first.");
638
+ }
639
+ const params = new URLSearchParams();
640
+ if (includeContent) params.set("includeContent", "true");
641
+ const url = `${getBaseUrl()}/api/activity/prompts/${id}${params.toString() ? `?${params}` : ""}`;
642
+ const response = await fetch(url, {
643
+ headers: {
644
+ Authorization: `Bearer ${token}`
645
+ }
646
+ });
647
+ if (!response.ok) {
648
+ if (response.status === 401) {
649
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
650
+ }
651
+ if (response.status === 404) {
652
+ throw new Error("Prompt request not found");
653
+ }
654
+ const error = await response.json();
655
+ throw new Error(error.error || "Failed to get activity");
656
+ }
657
+ return await response.json();
658
+ }
659
+ async function getActivityKpis(options) {
660
+ const token = getValidToken();
661
+ if (!token) {
662
+ throw new Error("Not logged in. Run 'olakai login' first.");
663
+ }
664
+ if (!options.agentId && !options.workflowId) {
665
+ throw new Error("Either agentId or workflowId is required");
666
+ }
667
+ const params = new URLSearchParams();
668
+ if (options.agentId) params.set("agentId", options.agentId);
669
+ if (options.workflowId) params.set("workflowId", options.workflowId);
670
+ if (options.since) params.set("since", options.since);
671
+ if (options.until) params.set("until", options.until);
672
+ if (options.period) params.set("period", options.period);
673
+ if (options.includeAtoms) params.set("includeAtoms", "true");
674
+ const url = `${getBaseUrl()}/api/activity/kpis${params.toString() ? `?${params}` : ""}`;
675
+ const response = await fetch(url, {
676
+ headers: {
677
+ Authorization: `Bearer ${token}`
678
+ }
679
+ });
680
+ if (!response.ok) {
681
+ if (response.status === 401) {
682
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
683
+ }
684
+ const error = await response.json();
685
+ throw new Error(error.error || "Failed to get activity KPIs");
686
+ }
687
+ return await response.json();
688
+ }
689
+ async function listSessions(options) {
690
+ const token = getValidToken();
691
+ if (!token) {
692
+ throw new Error("Not logged in. Run 'olakai login' first.");
693
+ }
694
+ const params = new URLSearchParams();
695
+ params.set("agentId", options.agentId);
696
+ if (options.since) params.set("since", options.since);
697
+ if (options.until) params.set("until", options.until);
698
+ if (options.limit !== void 0) params.set("limit", String(options.limit));
699
+ if (options.offset !== void 0) params.set("offset", String(options.offset));
700
+ const url = `${getBaseUrl()}/api/activity/sessions?${params}`;
701
+ const response = await fetch(url, {
702
+ headers: {
703
+ Authorization: `Bearer ${token}`
704
+ }
705
+ });
706
+ if (!response.ok) {
707
+ if (response.status === 401) {
708
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
709
+ }
710
+ const error = await response.json();
711
+ throw new Error(error.error || "Failed to list sessions");
712
+ }
713
+ return await response.json();
714
+ }
715
+ async function listCustomDataConfigs(agentId) {
716
+ const token = getValidToken();
717
+ if (!token) {
718
+ throw new Error("Not logged in. Run 'olakai login' first.");
719
+ }
720
+ const params = new URLSearchParams();
721
+ if (agentId) {
722
+ params.set("agentId", agentId);
723
+ }
724
+ const url = `${getBaseUrl()}/api/config/custom-data${params.toString() ? `?${params}` : ""}`;
725
+ const response = await fetch(url, {
726
+ headers: {
727
+ Authorization: `Bearer ${token}`
728
+ }
729
+ });
730
+ if (!response.ok) {
731
+ if (response.status === 401) {
732
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
733
+ }
734
+ const error = await response.json();
735
+ throw new Error(error.error || "Failed to list custom data configurations");
736
+ }
737
+ const data = await response.json();
738
+ return data.customDataConfigs;
739
+ }
740
+ async function getCustomDataConfig(id) {
741
+ const token = getValidToken();
742
+ if (!token) {
743
+ throw new Error("Not logged in. Run 'olakai login' first.");
744
+ }
745
+ const url = `${getBaseUrl()}/api/config/custom-data/${encodeURIComponent(id)}`;
746
+ const response = await fetch(url, {
747
+ headers: {
748
+ Authorization: `Bearer ${token}`
749
+ }
750
+ });
751
+ if (!response.ok) {
752
+ if (response.status === 401) {
753
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
754
+ }
755
+ if (response.status === 404) {
756
+ throw new Error("Custom data configuration not found");
757
+ }
758
+ const error = await response.json();
759
+ throw new Error(error.error || "Failed to get custom data configuration");
760
+ }
761
+ return await response.json();
762
+ }
763
+ async function createCustomDataConfig(payload) {
764
+ const token = getValidToken();
765
+ if (!token) {
766
+ throw new Error("Not logged in. Run 'olakai login' first.");
767
+ }
768
+ const url = `${getBaseUrl()}/api/config/custom-data`;
769
+ const response = await fetch(url, {
770
+ method: "POST",
771
+ headers: {
772
+ Authorization: `Bearer ${token}`,
773
+ "Content-Type": "application/json"
774
+ },
775
+ body: JSON.stringify(payload)
776
+ });
777
+ if (!response.ok) {
778
+ if (response.status === 401) {
779
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
780
+ }
781
+ if (response.status === 409) {
782
+ throw new Error("A custom data configuration with this name already exists");
783
+ }
784
+ const error = await response.json();
785
+ throw new Error(error.error || "Failed to create custom data configuration");
786
+ }
787
+ return await response.json();
788
+ }
789
+ async function updateCustomDataConfig(id, payload) {
790
+ const token = getValidToken();
791
+ if (!token) {
792
+ throw new Error("Not logged in. Run 'olakai login' first.");
793
+ }
794
+ const url = `${getBaseUrl()}/api/config/custom-data/${encodeURIComponent(id)}`;
795
+ const response = await fetch(url, {
796
+ method: "PUT",
797
+ headers: {
798
+ Authorization: `Bearer ${token}`,
799
+ "Content-Type": "application/json"
800
+ },
801
+ body: JSON.stringify(payload)
802
+ });
803
+ if (!response.ok) {
804
+ if (response.status === 401) {
805
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
806
+ }
807
+ if (response.status === 404) {
808
+ throw new Error("Custom data configuration not found");
809
+ }
810
+ if (response.status === 409) {
811
+ throw new Error("A custom data configuration with this name already exists");
812
+ }
813
+ const error = await response.json();
814
+ throw new Error(error.error || "Failed to update custom data configuration");
815
+ }
816
+ return await response.json();
817
+ }
818
+ async function deleteCustomDataConfig(id) {
819
+ const token = getValidToken();
820
+ if (!token) {
821
+ throw new Error("Not logged in. Run 'olakai login' first.");
822
+ }
823
+ const url = `${getBaseUrl()}/api/config/custom-data/${encodeURIComponent(id)}`;
824
+ const response = await fetch(url, {
825
+ method: "DELETE",
826
+ headers: {
827
+ Authorization: `Bearer ${token}`
828
+ }
829
+ });
830
+ if (!response.ok) {
831
+ if (response.status === 401) {
832
+ throw new Error("Session expired. Run 'olakai login' to authenticate again.");
833
+ }
834
+ if (response.status === 404) {
835
+ throw new Error("Custom data configuration not found");
836
+ }
837
+ const error = await response.json();
838
+ throw new Error(error.error || "Failed to delete custom data configuration");
839
+ }
840
+ }
841
+
842
+ export {
843
+ requestDeviceCode,
844
+ pollForToken,
845
+ getCurrentUser,
846
+ listAgents,
847
+ getAgent,
848
+ createAgent,
849
+ listMyAgents,
850
+ regenerateAgentApiKey,
851
+ updateAgent,
852
+ deleteAgent,
853
+ listWorkflows,
854
+ getWorkflow,
855
+ createWorkflow,
856
+ updateWorkflow,
857
+ deleteWorkflow,
858
+ listKpis,
859
+ getKpi,
860
+ createKpi,
861
+ updateKpi,
862
+ deleteKpi,
863
+ getKpiContextVariables,
864
+ validateKpiFormula,
865
+ validateMonitoringApiKey,
866
+ listActivity,
867
+ getActivity,
868
+ getActivityKpis,
869
+ listSessions,
870
+ listCustomDataConfigs,
871
+ getCustomDataConfig,
872
+ createCustomDataConfig,
873
+ updateCustomDataConfig,
874
+ deleteCustomDataConfig
875
+ };
876
+ //# sourceMappingURL=chunk-GU4HEL24.js.map