@zapier/zapier-sdk 0.70.4 → 0.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +133 -15
- package/dist/api/approval-review-stream.d.ts +25 -0
- package/dist/api/approval-review-stream.d.ts.map +1 -0
- package/dist/api/approval-review-stream.js +104 -0
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +215 -27
- package/dist/api/types.d.ts +13 -3
- package/dist/api/types.d.ts.map +1 -1
- package/dist/constants.d.ts +1 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +6 -0
- package/dist/experimental.cjs +593 -33
- package/dist/experimental.d.mts +86 -2
- package/dist/experimental.d.ts +88 -4
- package/dist/experimental.d.ts.map +1 -1
- package/dist/experimental.js +10 -0
- package/dist/experimental.mjs +593 -34
- package/dist/{index-BNaiNmM-.d.mts → index-B43uST61.d.mts} +181 -12
- package/dist/{index-BNaiNmM-.d.ts → index-B43uST61.d.ts} +181 -12
- package/dist/index.cjs +592 -32
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +592 -33
- package/dist/plugins/api/index.d.ts.map +1 -1
- package/dist/plugins/api/index.js +2 -1
- package/dist/plugins/codeSubstrate/deleteWorkflow/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/disableWorkflow/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/enableWorkflow/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/getDurableRun/schemas.d.ts +4 -4
- package/dist/plugins/codeSubstrate/getWorkflow/index.d.ts +2 -2
- package/dist/plugins/codeSubstrate/getWorkflow/schemas.d.ts +1 -1
- package/dist/plugins/codeSubstrate/getWorkflowRun/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/getWorkflowVersion/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/listWorkflowRuns/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/listWorkflowVersions/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/listWorkflows/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/listWorkflows/schemas.d.ts +2 -2
- package/dist/plugins/codeSubstrate/publishWorkflowVersion/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/shared-schemas.d.ts +2 -2
- package/dist/plugins/codeSubstrate/triggerWorkflow/index.d.ts +1 -1
- package/dist/plugins/codeSubstrate/updateWorkflow/index.d.ts +1 -1
- package/dist/plugins/createConnection/index.d.ts +189 -0
- package/dist/plugins/createConnection/index.d.ts.map +1 -0
- package/dist/plugins/createConnection/index.js +71 -0
- package/dist/plugins/createConnection/schemas.d.ts +21 -0
- package/dist/plugins/createConnection/schemas.d.ts.map +1 -0
- package/dist/plugins/createConnection/schemas.js +38 -0
- package/dist/plugins/getConnectionStartUrl/index.d.ts +206 -0
- package/dist/plugins/getConnectionStartUrl/index.d.ts.map +1 -0
- package/dist/plugins/getConnectionStartUrl/index.js +39 -0
- package/dist/plugins/getConnectionStartUrl/schemas.d.ts +15 -0
- package/dist/plugins/getConnectionStartUrl/schemas.d.ts.map +1 -0
- package/dist/plugins/getConnectionStartUrl/schemas.js +23 -0
- package/dist/plugins/waitForNewConnection/index.d.ts +209 -0
- package/dist/plugins/waitForNewConnection/index.d.ts.map +1 -0
- package/dist/plugins/waitForNewConnection/index.js +75 -0
- package/dist/plugins/waitForNewConnection/schemas.d.ts +17 -0
- package/dist/plugins/waitForNewConnection/schemas.d.ts.map +1 -0
- package/dist/plugins/waitForNewConnection/schemas.js +39 -0
- package/dist/sdk.d.ts +126 -0
- package/dist/sdk.d.ts.map +1 -1
- package/dist/sdk.js +8 -0
- package/dist/types/errors.d.ts +13 -4
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +2 -0
- package/dist/types/sdk.d.ts +1 -0
- package/dist/types/sdk.d.ts.map +1 -1
- package/dist/types/sdk.js +5 -1
- package/dist/utils/open-url.d.ts.map +1 -1
- package/dist/utils/open-url.js +7 -0
- package/dist/utils/should-open-browser.d.ts +24 -0
- package/dist/utils/should-open-browser.d.ts.map +1 -0
- package/dist/utils/should-open-browser.js +55 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @zapier/zapier-sdk
|
|
2
2
|
|
|
3
|
+
## 0.71.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 3474ff3: Add `createConnection` and two lower-level building blocks for connecting an app from code, end to end:
|
|
8
|
+
- `createConnection({ app, browser?, timeoutMs?, pollIntervalMs? })` — high-level: mints a connection URL, prints it (and opens it in a browser when it's safe to do so), waits for the user to finish, and returns the new connection. `browser` is `"auto"` (default — opens locally, skips CI / SSH / headless Linux), `"always"`, or `"never"`. The URL is always printed, so a skipped or failed open falls back to copy/paste.
|
|
9
|
+
- `getConnectionStartUrl({ app })` — low-level: mints a short-lived, signed start URL bound to the current user/account. Returns `{ url, startedAt, expiresAt, app }`.
|
|
10
|
+
- `waitForNewConnection({ app, startedAt, timeoutMs?, pollIntervalMs? })` — low-level: polls until a connection for the app created at or after `startedAt` appears, then returns `{ id, app, title? }`. Throws `ZapierTimeoutError` after `timeoutMs` (default 5 minutes).
|
|
11
|
+
|
|
12
|
+
`createConnection` is the right call for most cases. Reach for the two low-level methods when you want to hand off the URL without blocking (call `getConnectionStartUrl` alone), or do something custom between minting the URL and waiting — email it, post it to Slack, render a QR code — then call `waitForNewConnection`.
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- 5ca445d: Add private-beta support for streaming auto-mode approval review messages and handling terminal failed approvals through the SDK and CLI. Auto mode is only enabled service-side for approved beta users.
|
|
17
|
+
|
|
3
18
|
## 0.70.4
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -46,10 +46,13 @@
|
|
|
46
46
|
- [`triggerWorkflow`](#triggerworkflow--experimental)
|
|
47
47
|
- [`updateWorkflow`](#updateworkflow--experimental)
|
|
48
48
|
- [Connections](#connections)
|
|
49
|
+
- [`createConnection`](#createconnection)
|
|
49
50
|
- [`findFirstConnection`](#findfirstconnection)
|
|
50
51
|
- [`findUniqueConnection`](#finduniqueconnection)
|
|
51
52
|
- [`getConnection`](#getconnection)
|
|
53
|
+
- [`getConnectionStartUrl`](#getconnectionstarturl)
|
|
52
54
|
- [`listConnections`](#listconnections)
|
|
55
|
+
- [`waitForNewConnection`](#waitfornewconnection)
|
|
53
56
|
- [HTTP Requests](#http-requests)
|
|
54
57
|
- [`fetch`](#fetch)
|
|
55
58
|
- [Tables](#tables)
|
|
@@ -349,21 +352,22 @@ console.log(emojiData.emoji);
|
|
|
349
352
|
|
|
350
353
|
The `createZapierSdk(...)` factory function is the main entry point for the SDK. It provides methods for managing connections, listing apps, running actions, and more.
|
|
351
354
|
|
|
352
|
-
| Name
|
|
353
|
-
|
|
|
354
|
-
| `credentials`
|
|
355
|
-
| `debug`
|
|
356
|
-
| `baseUrl`
|
|
357
|
-
| `trackingBaseUrl`
|
|
358
|
-
| `maxNetworkRetries`
|
|
359
|
-
| `maxNetworkRetryDelayMs`
|
|
360
|
-
| `maxConcurrentRequests`
|
|
361
|
-
| `approvalTimeoutMs`
|
|
362
|
-
| `maxApprovalRetries`
|
|
363
|
-
| `approvalMode`
|
|
364
|
-
| `
|
|
365
|
-
| `
|
|
366
|
-
| `
|
|
355
|
+
| Name | Type | Required | Default | Possible Values | Description |
|
|
356
|
+
| -------------------------------- | -------------------------- | -------- | ------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
357
|
+
| `credentials` | `string, object, function` | ❌ | — | — | Authentication credentials. Can be a string (token or API key), a client credentials object ({ clientId, clientSecret }), a PKCE object ({ clientId }), or a function returning any of those. |
|
|
358
|
+
| `debug` | `boolean` | ❌ | — | — | Enable debug logging. |
|
|
359
|
+
| `baseUrl` | `string` | ❌ | — | — | Base URL for Zapier API endpoints. |
|
|
360
|
+
| `trackingBaseUrl` | `string` | ❌ | — | — | Base URL for Zapier tracking endpoints. |
|
|
361
|
+
| `maxNetworkRetries` | `number` | ❌ | — | — | Max retries for rate-limited requests (default: 3). |
|
|
362
|
+
| `maxNetworkRetryDelayMs` | `number` | ❌ | — | — | Max delay in ms to wait for retry (default: 60000). |
|
|
363
|
+
| `maxConcurrentRequests` | `number, literal` | ❌ | — | — | Max concurrent in-flight HTTP requests (default: 200, max: 10000). |
|
|
364
|
+
| `approvalTimeoutMs` | `number` | ❌ | — | — | Timeout in ms for approval polling. Default: 600000 (10 min). |
|
|
365
|
+
| `maxApprovalRetries` | `number` | ❌ | — | — | Maximum number of sequential approval rounds per request (one per gating policy) before giving up. Default: 2. |
|
|
366
|
+
| `approvalMode` | `string` | ❌ | — | `disabled`, `poll`, `throw` | Approval flow behavior for manual approvals. "poll" creates the approval, opens it in a browser, polls until resolved, and retries the original request. "throw" creates the manual approval and throws a ZapierApprovalError with the approval URL so the caller can surface it. Server-created auto-mode approvals always poll until they reach a terminal status and retry the original request on approval, even when this option is "throw". "disabled" throws a ZapierApprovalError on approval-required responses without creating an approval. Resolution order is: explicit option, then ZAPIER_APPROVAL_MODE, then the default behavior (poll for interactive TTY, throw otherwise). |
|
|
367
|
+
| `openAutoModeApprovalsInBrowser` | `boolean` | ❌ | — | — | By default, auto-mode approvals do not open in a browser. Enable this option to open the approval URL and watch the approval process. Resolution order is: explicit option, then ZAPIER_OPEN_AUTO_MODE_APPROVALS_IN_BROWSER, then false. |
|
|
368
|
+
| `canIncludeSharedConnections` | `boolean` | ❌ | — | — | Allow listing shared connections. |
|
|
369
|
+
| `canIncludeSharedTables` | `boolean` | ❌ | — | — | Allow listing shared tables. |
|
|
370
|
+
| `canDeleteTables` | `boolean` | ❌ | — | — | Allow deleting tables. |
|
|
367
371
|
|
|
368
372
|
## Named Connections
|
|
369
373
|
|
|
@@ -1802,6 +1806,39 @@ const result = await zapier.updateWorkflow({
|
|
|
1802
1806
|
|
|
1803
1807
|
### Connections
|
|
1804
1808
|
|
|
1809
|
+
#### `createConnection`
|
|
1810
|
+
|
|
1811
|
+
Create a new app connection, end-to-end. Mints the start URL via `get-connection-start-url`, prints it to stderr, opportunistically opens it in a browser when it looks safe to do so (skipping CI / SSH / headless-Linux by default — pass `--browser always` to force, `--browser never` to suppress), then polls via `wait-for-new-connection` until the user completes OAuth and the new connection appears. Returns the connection.
|
|
1812
|
+
|
|
1813
|
+
This is the right command for most callers. Reach for the lower-level building blocks when you want either of: (a) hand off the URL and _not_ block on completion — call `get-connection-start-url` alone, no `wait-for-new-connection` needed, or (b) do something custom between minting the URL and waiting — call `get-connection-start-url`, do your work (email or DM the URL, render a QR code, etc.), then `wait-for-new-connection`.
|
|
1814
|
+
|
|
1815
|
+
**Parameters:**
|
|
1816
|
+
|
|
1817
|
+
| Name | Type | Required | Default | Possible Values | Description |
|
|
1818
|
+
| -------------------- | -------- | -------- | -------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
1819
|
+
| `options` | `object` | ✅ | — | — | |
|
|
1820
|
+
| ↳ `app` | `string` | ✅ | — | — | App slug (e.g., 'github'), implementation name (e.g., 'SlackCLIAPI'), or versioned ID (e.g., 'github@1.2.3') |
|
|
1821
|
+
| ↳ `browser` | `string` | ❌ | `"auto"` | `auto`, `always`, `never` | When to auto-open the URL in a browser. `auto` (default) opens in local sessions and skips opening in CI / SSH / headless-Linux. `always` forces the open attempt. `never` skips it. The URL is always printed to stderr regardless — a failed or skipped open degrades gracefully to copy-paste. |
|
|
1822
|
+
| ↳ `timeoutMs` | `number` | ❌ | — | — | How long to wait for the user to complete the connection flow before giving up. Default 5 minutes (300_000). |
|
|
1823
|
+
| ↳ `pollIntervalMs` | `number` | ❌ | — | — | Delay before the first poll request, in ms. Default 3 seconds (3_000). Subsequent polling cadence is managed by the SDK's polling primitive (backoff with sane defaults). |
|
|
1824
|
+
|
|
1825
|
+
**Returns:** `Promise<ConnectionItem>`
|
|
1826
|
+
|
|
1827
|
+
| Name | Type | Required | Possible Values | Description |
|
|
1828
|
+
| ----------- | -------- | -------- | --------------- | ------------------------------------------------------------------------------------ |
|
|
1829
|
+
| `data` | `object` | ✅ | — | |
|
|
1830
|
+
| ↳ `id` | `string` | ✅ | — | The new connection's ID. Public UUID when available, falling back to the numeric ID. |
|
|
1831
|
+
| ↳ `app` | `string` | ✅ | — | Versionless app key the connection was created for (e.g., 'SlackCLIAPI'). |
|
|
1832
|
+
| ↳ `title` | `string` | ❌ | — | Human-readable connection title set by the auth flow, when available. |
|
|
1833
|
+
|
|
1834
|
+
**Example:**
|
|
1835
|
+
|
|
1836
|
+
```typescript
|
|
1837
|
+
const result = await zapier.createConnection({
|
|
1838
|
+
app: "example-app",
|
|
1839
|
+
});
|
|
1840
|
+
```
|
|
1841
|
+
|
|
1805
1842
|
#### `findFirstConnection`
|
|
1806
1843
|
|
|
1807
1844
|
Find the first connection matching the criteria
|
|
@@ -1964,6 +2001,47 @@ Execute getConnection
|
|
|
1964
2001
|
const { data: connection } = await zapier.getConnection();
|
|
1965
2002
|
```
|
|
1966
2003
|
|
|
2004
|
+
#### `getConnectionStartUrl`
|
|
2005
|
+
|
|
2006
|
+
Mint a short-lived URL that begins an SDK-initiated connection flow. The URL is signed by zapier.com and bound to the current user/account — opening it in a different browser session will fail the binding check. Returns the URL as data so the caller decides what to do with it.
|
|
2007
|
+
|
|
2008
|
+
Use this directly (rather than the higher-level `create-connection`) when you want either of: (a) hand off the URL and _not_ block waiting for completion — call this alone, skip `wait-for-new-connection` entirely, or (b) do something custom between minting the URL and waiting for the connection — call this, then email or DM the URL, render it as a QR code for mobile sign-in, etc., then call `wait-for-new-connection`. For the common case where you'd just print and poll back-to-back, `create-connection` is one call.
|
|
2009
|
+
|
|
2010
|
+
Pair with `wait-for-new-connection` to detect completion: pass the `startedAt` returned here straight through (it's the server's mint time, so polling isn't affected by client clock skew). Example (JS):
|
|
2011
|
+
|
|
2012
|
+
```ts
|
|
2013
|
+
const {
|
|
2014
|
+
data: { url, app, startedAt },
|
|
2015
|
+
} = await zapier.getConnectionStartUrl({ app: "slack" });
|
|
2016
|
+
// hand `url` off — print it, DM it, email it, render a button, whatever
|
|
2017
|
+
const { data: conn } = await zapier.waitForNewConnection({ app, startedAt });
|
|
2018
|
+
```
|
|
2019
|
+
|
|
2020
|
+
**Parameters:**
|
|
2021
|
+
|
|
2022
|
+
| Name | Type | Required | Default | Possible Values | Description |
|
|
2023
|
+
| --------- | -------- | -------- | ------- | --------------- | ------------------------------------------------------------------------------------------------------------ |
|
|
2024
|
+
| `options` | `object` | ✅ | — | — | |
|
|
2025
|
+
| ↳ `app` | `string` | ✅ | — | — | App slug (e.g., 'github'), implementation name (e.g., 'SlackCLIAPI'), or versioned ID (e.g., 'github@1.2.3') |
|
|
2026
|
+
|
|
2027
|
+
**Returns:** `Promise<ConnectionStartUrlItem>`
|
|
2028
|
+
|
|
2029
|
+
| Name | Type | Required | Possible Values | Description |
|
|
2030
|
+
| --------------- | -------- | -------- | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
2031
|
+
| `data` | `object` | ✅ | — | |
|
|
2032
|
+
| ↳ `url` | `string` | ✅ | — | URL the user should open in their browser to complete the auth flow. Single-use, time-limited. |
|
|
2033
|
+
| ↳ `expiresAt` | `number` | ✅ | — | Unix timestamp (seconds) after which the URL's signature is rejected by zapier.com. |
|
|
2034
|
+
| ↳ `startedAt` | `number` | ✅ | — | Unix timestamp (seconds) when the server minted the URL. Use it as the `startedAt` for `wait-for-new-connection` so polling is anchored to server time rather than a possibly-skewed client clock. |
|
|
2035
|
+
| ↳ `app` | `string` | ✅ | — | Versionless app key the URL was minted for (e.g., 'SlackCLIAPI'). Useful for downstream filtering. |
|
|
2036
|
+
|
|
2037
|
+
**Example:**
|
|
2038
|
+
|
|
2039
|
+
```typescript
|
|
2040
|
+
const result = await zapier.getConnectionStartUrl({
|
|
2041
|
+
app: "example-app",
|
|
2042
|
+
});
|
|
2043
|
+
```
|
|
2044
|
+
|
|
1967
2045
|
#### `listConnections`
|
|
1968
2046
|
|
|
1969
2047
|
List available connections with optional filtering
|
|
@@ -2036,6 +2114,46 @@ for await (const connection of zapier.listConnections().items()) {
|
|
|
2036
2114
|
}
|
|
2037
2115
|
```
|
|
2038
2116
|
|
|
2117
|
+
#### `waitForNewConnection`
|
|
2118
|
+
|
|
2119
|
+
Wait for a new connection to appear for the given app. Polls `/api/v0/connections` with server-side `ordering=-date` until the most recent matching row's `date` is at or after the started-at timestamp, then returns it. Pair with `get-connection-start-url` — that mints the URL the user opens, this waits for the resulting connection to land. Errors with a timeout after the configured timeout (default 5 min). Example (JS):
|
|
2120
|
+
|
|
2121
|
+
```ts
|
|
2122
|
+
const {
|
|
2123
|
+
data: { url, app, startedAt },
|
|
2124
|
+
} = await zapier.getConnectionStartUrl({ app: "slack" });
|
|
2125
|
+
// show `url` to the user via the channel they're reading from
|
|
2126
|
+
const { data: conn } = await zapier.waitForNewConnection({ app, startedAt });
|
|
2127
|
+
```
|
|
2128
|
+
|
|
2129
|
+
**Parameters:**
|
|
2130
|
+
|
|
2131
|
+
| Name | Type | Required | Default | Possible Values | Description |
|
|
2132
|
+
| -------------------- | -------- | -------- | ------- | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
2133
|
+
| `options` | `object` | ✅ | — | — | |
|
|
2134
|
+
| ↳ `app` | `string` | ✅ | — | — | App slug (e.g., 'github'), implementation name (e.g., 'SlackCLIAPI'), or versioned ID (e.g., 'github@1.2.3') |
|
|
2135
|
+
| ↳ `startedAt` | `number` | ✅ | — | — | Unix timestamp (seconds). Only connections whose `date` is at or after this value count as 'new'. Prefer the `startedAt` returned by `get-connection-start-url` — it's server-stamped, so the comparison isn't thrown off by client clock skew. If you mint the timestamp yourself, capture it _before_ showing the start URL so a fast OAuth completion isn't missed. |
|
|
2136
|
+
| ↳ `timeoutMs` | `number` | ❌ | — | — | How long to wait before giving up. Default 5 minutes (300_000). |
|
|
2137
|
+
| ↳ `pollIntervalMs` | `number` | ❌ | — | — | Delay before the first poll request, in ms. Default 3 seconds (3_000). Subsequent polling cadence is managed by the SDK's polling primitive (backoff with sane defaults). |
|
|
2138
|
+
|
|
2139
|
+
**Returns:** `Promise<ConnectionItem>`
|
|
2140
|
+
|
|
2141
|
+
| Name | Type | Required | Possible Values | Description |
|
|
2142
|
+
| ----------- | -------- | -------- | --------------- | ------------------------------------------------------------------------------------ |
|
|
2143
|
+
| `data` | `object` | ✅ | — | |
|
|
2144
|
+
| ↳ `id` | `string` | ✅ | — | The new connection's ID. Public UUID when available, falling back to the numeric ID. |
|
|
2145
|
+
| ↳ `app` | `string` | ✅ | — | Versionless app key the connection was created for (e.g., 'SlackCLIAPI'). |
|
|
2146
|
+
| ↳ `title` | `string` | ❌ | — | Human-readable connection title set by the auth flow, when available. |
|
|
2147
|
+
|
|
2148
|
+
**Example:**
|
|
2149
|
+
|
|
2150
|
+
```typescript
|
|
2151
|
+
const { data: connection } = await zapier.waitForNewConnection({
|
|
2152
|
+
app: "example-app",
|
|
2153
|
+
startedAt: 100,
|
|
2154
|
+
});
|
|
2155
|
+
```
|
|
2156
|
+
|
|
2039
2157
|
### HTTP Requests
|
|
2040
2158
|
|
|
2041
2159
|
#### `fetch`
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FetchStreamInit } from "./types";
|
|
2
|
+
import type { JsonSseMessage } from "./sse-parser";
|
|
3
|
+
interface ApprovalReviewMessagePayload {
|
|
4
|
+
kind: "message";
|
|
5
|
+
message: string;
|
|
6
|
+
data: unknown;
|
|
7
|
+
}
|
|
8
|
+
interface ApprovalReviewDecisionPayload {
|
|
9
|
+
kind: "decision";
|
|
10
|
+
decision: "approved" | "denied";
|
|
11
|
+
reason?: string;
|
|
12
|
+
data: unknown;
|
|
13
|
+
}
|
|
14
|
+
export type ApprovalReviewStreamPayload = ApprovalReviewMessagePayload | ApprovalReviewDecisionPayload;
|
|
15
|
+
interface ConsumeApprovalReviewStreamOptions {
|
|
16
|
+
approvalId: string;
|
|
17
|
+
streamUrl: string;
|
|
18
|
+
signal: AbortSignal;
|
|
19
|
+
stream: (url: string, init?: FetchStreamInit) => AsyncGenerator<JsonSseMessage>;
|
|
20
|
+
emitEvent: (type: string, payload: Record<string, unknown>) => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function consumeApprovalReviewStream({ approvalId, streamUrl, signal, stream, emitEvent, }: ConsumeApprovalReviewStreamOptions): Promise<void>;
|
|
23
|
+
export declare function parseApprovalReviewStreamPayload(parsed: unknown, toolNames: Map<string, string>): ApprovalReviewStreamPayload | undefined;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=approval-review-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approval-review-stream.d.ts","sourceRoot":"","sources":["../../src/api/approval-review-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAmCnD,UAAU,4BAA4B;IACpC,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;CACf;AAED,UAAU,6BAA6B;IACrC,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,UAAU,GAAG,QAAQ,CAAC;IAChC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,MAAM,2BAA2B,GACnC,4BAA4B,GAC5B,6BAA6B,CAAC;AAElC,UAAU,kCAAkC;IAC1C,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,WAAW,CAAC;IACpB,MAAM,EAAE,CACN,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,eAAe,KACnB,cAAc,CAAC,cAAc,CAAC,CAAC;IACpC,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;CACrE;AAED,wBAAsB,2BAA2B,CAAC,EAChD,UAAU,EACV,SAAS,EACT,MAAM,EACN,MAAM,EACN,SAAS,GACV,EAAE,kCAAkC,GAAG,OAAO,CAAC,IAAI,CAAC,CA0CpD;AAED,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAC7B,2BAA2B,GAAG,SAAS,CA8BzC"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { isAbortError } from "../utils/abort-utils";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
const ApprovalReviewChunkSchema = z.discriminatedUnion("type", [
|
|
4
|
+
z
|
|
5
|
+
.object({
|
|
6
|
+
type: z.literal("text-delta"),
|
|
7
|
+
delta: z.string(),
|
|
8
|
+
})
|
|
9
|
+
.passthrough(),
|
|
10
|
+
z
|
|
11
|
+
.object({
|
|
12
|
+
type: z.literal("tool-input-available"),
|
|
13
|
+
toolCallId: z.string(),
|
|
14
|
+
toolName: z.string(),
|
|
15
|
+
})
|
|
16
|
+
.passthrough(),
|
|
17
|
+
z
|
|
18
|
+
.object({
|
|
19
|
+
type: z.literal("tool-output-available"),
|
|
20
|
+
toolCallId: z.string(),
|
|
21
|
+
output: z.unknown(),
|
|
22
|
+
})
|
|
23
|
+
.passthrough(),
|
|
24
|
+
]);
|
|
25
|
+
const ReportDecisionOutputSchema = z
|
|
26
|
+
.object({
|
|
27
|
+
success: z.boolean().optional(),
|
|
28
|
+
decision: z.enum(["approved", "denied"]),
|
|
29
|
+
reason: z.string().optional(),
|
|
30
|
+
})
|
|
31
|
+
.passthrough();
|
|
32
|
+
export async function consumeApprovalReviewStream({ approvalId, streamUrl, signal, stream, emitEvent, }) {
|
|
33
|
+
try {
|
|
34
|
+
const toolNames = new Map();
|
|
35
|
+
for await (const frame of stream(streamUrl, {
|
|
36
|
+
method: "GET",
|
|
37
|
+
headers: {
|
|
38
|
+
"Accept-Encoding": "identity",
|
|
39
|
+
},
|
|
40
|
+
signal,
|
|
41
|
+
})) {
|
|
42
|
+
if (!frame.parsed)
|
|
43
|
+
continue;
|
|
44
|
+
const payload = parseApprovalReviewStreamPayload(frame.data, toolNames);
|
|
45
|
+
if (!payload)
|
|
46
|
+
continue;
|
|
47
|
+
if (payload.kind === "message") {
|
|
48
|
+
emitEvent("approval:review_message", {
|
|
49
|
+
approvalId,
|
|
50
|
+
streamUrl,
|
|
51
|
+
data: payload.data,
|
|
52
|
+
message: payload.message,
|
|
53
|
+
});
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
emitEvent("approval:review_decision", {
|
|
57
|
+
approvalId,
|
|
58
|
+
streamUrl,
|
|
59
|
+
data: payload.data,
|
|
60
|
+
decision: payload.decision,
|
|
61
|
+
...(payload.reason ? { reason: payload.reason } : {}),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
if (isAbortError(err) || signal.aborted)
|
|
67
|
+
return;
|
|
68
|
+
emitEvent("approval:review_stream_error", {
|
|
69
|
+
approvalId,
|
|
70
|
+
streamUrl,
|
|
71
|
+
message: err instanceof Error ? err.message : String(err),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function parseApprovalReviewStreamPayload(parsed, toolNames) {
|
|
76
|
+
const chunk = ApprovalReviewChunkSchema.safeParse(parsed);
|
|
77
|
+
if (!chunk.success)
|
|
78
|
+
return undefined;
|
|
79
|
+
switch (chunk.data.type) {
|
|
80
|
+
case "text-delta": {
|
|
81
|
+
const message = chunk.data.delta.trim();
|
|
82
|
+
return message.length > 0
|
|
83
|
+
? { kind: "message", data: parsed, message }
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
case "tool-input-available":
|
|
87
|
+
toolNames.set(chunk.data.toolCallId, chunk.data.toolName);
|
|
88
|
+
return undefined;
|
|
89
|
+
case "tool-output-available": {
|
|
90
|
+
if (toolNames.get(chunk.data.toolCallId) !== "report_decision") {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
const decision = ReportDecisionOutputSchema.safeParse(chunk.data.output);
|
|
94
|
+
if (!decision.success)
|
|
95
|
+
return undefined;
|
|
96
|
+
return {
|
|
97
|
+
kind: "decision",
|
|
98
|
+
data: parsed,
|
|
99
|
+
decision: decision.data.decision,
|
|
100
|
+
...(decision.data.reason ? { reason: decision.data.reason } : {}),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
package/dist/api/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAIjB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,SAAS,EACT,gBAAgB,EAIjB,MAAM,SAAS,CAAC;AA2jDjB,eAAO,MAAM,eAAe,GAAI,SAAS,gBAAgB,KAAG,SAW3D,CAAC"}
|