@kubb/studio 5.2.6 → 5.2.7

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.
package/README.md CHANGED
@@ -96,6 +96,38 @@ nothing can read it back.
96
96
  member can approve. A host that pairs a shared or tier-limited agent passes `clientId: 'kubb-agent'`
97
97
  and an `agentKind`, whose codes only an admin can approve.
98
98
 
99
+ ## Asynchronous jobs
100
+
101
+ CI and automation queue work with `createJob` and poll with `waitForJob`. Both send the
102
+ organization CI API key as `x-api-key`. They do not open a WebSocket.
103
+
104
+ | Step | Call | What it does |
105
+ | ------ | -------------------- | --------------------------------------------------------------------- |
106
+ | Queue | `POST /api/jobs` | Accepts a `generation` or `snapshot` job and returns `202` with an id |
107
+ | Status | `GET /api/jobs/{id}` | Returns the job until `success` or `failed` |
108
+
109
+ ```typescript
110
+ import { createJob, waitForJob } from '@kubb/studio'
111
+
112
+ const job = await createJob({
113
+ studioUrl: 'https://kubb.studio',
114
+ token: process.env.KUBB_TOKEN!,
115
+ type: 'snapshot',
116
+ agentId: agent.id,
117
+ name: '@scope/package',
118
+ version: '1.0.0',
119
+ })
120
+
121
+ const finished = await waitForJob({
122
+ studioUrl: 'https://kubb.studio',
123
+ token: process.env.KUBB_TOKEN!,
124
+ id: job.id,
125
+ })
126
+
127
+ if (finished.status === 'failed') throw new Error(finished.error)
128
+ const snapshot = finished.snapshot
129
+ ```
130
+
99
131
  ## Protocol
100
132
 
101
133
  `@kubb/studio/protocol` holds the WebSocket message types shared by both ends, so the agent and
package/dist/index.cjs CHANGED
@@ -569,9 +569,57 @@ async function disconnect({ sessionId, token, studioUrl, slug }) {
569
569
  console.warn((0, node_util.styleText)("yellow", `[${tag}] Failed to notify Studio of disconnection: ${getErrorMessage(error)}`));
570
570
  }
571
571
  }
572
+ /**
573
+ * Queues a generation or snapshot job on Studio (`POST /api/jobs`).
574
+ *
575
+ * Returns as soon as Studio accepts the job (`202`). Poll with {@link waitForJob} until it finishes.
576
+ * Authenticates with the organization CI API key via `x-api-key`.
577
+ *
578
+ * @example Snapshot job
579
+ * ```ts
580
+ * const job = await createJob({
581
+ * studioUrl: 'https://kubb.studio',
582
+ * token: process.env.KUBB_TOKEN!,
583
+ * type: 'snapshot',
584
+ * agentId: agent.id,
585
+ * name: '@kubb/demo',
586
+ * version: '1.0.0',
587
+ * })
588
+ * const finished = await waitForJob({ studioUrl, token, id: job.id })
589
+ * ```
590
+ */
591
+ async function createJob({ studioUrl, token, type, agentId, name, version, config }) {
592
+ const { job } = await (0, ofetch.ofetch)(`${studioUrl}/api/jobs`, {
593
+ method: "POST",
594
+ headers: { "x-api-key": token },
595
+ body: {
596
+ type,
597
+ agentId,
598
+ name,
599
+ version,
600
+ config
601
+ }
602
+ });
603
+ return job;
604
+ }
605
+ /**
606
+ * Polls `GET /api/jobs/{id}` until the job reaches `success` or `failed`.
607
+ *
608
+ * A `failed` job resolves normally. Check `job.status` and `job.error`. Throws only when the
609
+ * deadline passes before Studio finishes.
610
+ */
611
+ async function waitForJob({ studioUrl, token, id, timeoutMs = 6e4 }) {
612
+ const deadline = Date.now() + timeoutMs;
613
+ for (;;) {
614
+ const { job } = await (0, ofetch.ofetch)(`${studioUrl}/api/jobs/${id}`, { headers: { "x-api-key": token } });
615
+ if (job.status === "success" || job.status === "failed") return job;
616
+ if (Date.now() >= deadline) throw new Error("Timed out waiting for the Studio job");
617
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
618
+ }
619
+ }
572
620
  //#endregion
573
621
  //#region package.json
574
- var version = "5.2.6";
622
+ var version = "5.2.7";
575
623
  //#endregion
576
624
  //#region src/hooks.ts
577
625
  /**
@@ -2451,11 +2499,13 @@ exports.InvalidAgentTokenError = InvalidAgentTokenError;
2451
2499
  exports.PairingCanceledError = PairingCanceledError;
2452
2500
  exports.createClient = createClient;
2453
2501
  exports.createFileStorage = createFileStorage;
2502
+ exports.createJob = createJob;
2454
2503
  exports.createJobId = require_protocol.createJobId;
2455
2504
  exports.defaultStudioUrl = defaultStudioUrl;
2456
2505
  exports.pollForPairingToken = pollForPairingToken;
2457
2506
  exports.runConnection = runConnection;
2458
2507
  exports.setStorage = setStorage;
2459
2508
  exports.startPairing = startPairing;
2509
+ exports.waitForJob = waitForJob;
2460
2510
 
2461
2511
  //# sourceMappingURL=index.cjs.map