@usestatemachines/sdk 0.5.0 → 0.6.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.
Files changed (259) hide show
  1. package/README.md +177 -521
  2. package/agents/SKILL.md +38 -47
  3. package/agents/index.js +7 -7
  4. package/agents/llms.txt +50 -46
  5. package/agents/prompt.md +11 -11
  6. package/esm/extensions/client.d.ts +104 -127
  7. package/esm/extensions/client.js +162 -322
  8. package/esm/extensions/credentials.d.ts +13 -0
  9. package/esm/extensions/credentials.js +73 -0
  10. package/esm/extensions/environment.d.ts +25 -0
  11. package/esm/extensions/environment.js +1 -0
  12. package/esm/extensions/errors.d.ts +42 -75
  13. package/esm/extensions/errors.js +125 -110
  14. package/esm/extensions/index.d.ts +6 -11
  15. package/esm/extensions/index.js +2 -7
  16. package/esm/extensions/wait.d.ts +17 -18
  17. package/esm/extensions/wait.js +60 -56
  18. package/esm/extensions/yaml.d.ts +6 -0
  19. package/esm/extensions/yaml.js +137 -0
  20. package/esm/funcs/{catalog-release.d.ts → apps-list.d.ts} +1 -1
  21. package/esm/funcs/{environments-get-connection.js → apps-list.js} +11 -16
  22. package/esm/funcs/{credential-get.d.ts → apps-versions.d.ts} +1 -1
  23. package/esm/funcs/{workspaces-events.js → apps-versions.js} +6 -7
  24. package/esm/funcs/{environments-get-connection.d.ts → audit-events-list.d.ts} +1 -1
  25. package/esm/funcs/audit-events-list.js +93 -0
  26. package/esm/funcs/{workspaces-events.d.ts → environments-connect.d.ts} +1 -1
  27. package/esm/funcs/{environments-reveal.js → environments-connect.js} +7 -6
  28. package/esm/funcs/environments-create.d.ts +2 -1
  29. package/esm/funcs/environments-create.js +5 -10
  30. package/esm/funcs/{environments-events.d.ts → environments-delete.d.ts} +1 -1
  31. package/esm/funcs/{environments-stop.js → environments-delete.js} +7 -6
  32. package/esm/funcs/environments-get.d.ts +2 -1
  33. package/esm/funcs/environments-get.js +2 -1
  34. package/esm/funcs/environments-list.d.ts +1 -1
  35. package/esm/funcs/environments-list.js +11 -16
  36. package/esm/funcs/environments-pause.d.ts +12 -0
  37. package/esm/funcs/{environments-suspend.js → environments-pause.js} +6 -5
  38. package/esm/funcs/environments-resume.d.ts +2 -1
  39. package/esm/funcs/environments-resume.js +2 -1
  40. package/esm/funcs/environments-update.d.ts +12 -0
  41. package/esm/funcs/{environments-events.js → environments-update.js} +9 -13
  42. package/esm/funcs/me.d.ts +11 -0
  43. package/esm/funcs/{catalog-get.js → me.js} +5 -5
  44. package/esm/funcs/{catalog-get.d.ts → requests-body.d.ts} +1 -1
  45. package/esm/funcs/{environments-get-request-payload.js → requests-body.js} +5 -9
  46. package/esm/funcs/{environments-get-request-stats.d.ts → requests-get.d.ts} +2 -1
  47. package/esm/funcs/{catalog-release.js → requests-get.js} +7 -6
  48. package/esm/funcs/{environments-stop.d.ts → requests-list.d.ts} +1 -1
  49. package/esm/funcs/{environments-list-requests.js → requests-list.js} +9 -14
  50. package/esm/funcs/requests-stats.d.ts +12 -0
  51. package/esm/funcs/{environments-get-request-stats.js → requests-stats.js} +10 -14
  52. package/esm/funcs/snapshots-create.d.ts +12 -0
  53. package/esm/funcs/{snapshots-capture.js → snapshots-create.js} +6 -11
  54. package/esm/funcs/snapshots-delete.d.ts +2 -1
  55. package/esm/funcs/snapshots-delete.js +2 -1
  56. package/esm/funcs/snapshots-get.d.ts +2 -1
  57. package/esm/funcs/snapshots-get.js +2 -1
  58. package/esm/funcs/snapshots-list.d.ts +1 -1
  59. package/esm/funcs/snapshots-list.js +9 -13
  60. package/esm/funcs/snapshots-update.d.ts +2 -1
  61. package/esm/funcs/snapshots-update.js +2 -1
  62. package/esm/funcs/workspaces-get.d.ts +2 -1
  63. package/esm/funcs/workspaces-get.js +2 -1
  64. package/esm/funcs/workspaces-list.d.ts +1 -1
  65. package/esm/funcs/workspaces-list.js +3 -1
  66. package/esm/lib/config.d.ts +3 -3
  67. package/esm/lib/config.js +3 -3
  68. package/esm/models/actor.d.ts +2 -3
  69. package/esm/models/actor.js +2 -3
  70. package/esm/models/{api-profile.d.ts → api.d.ts} +4 -4
  71. package/esm/models/{api-profile.js → api.js} +4 -5
  72. package/esm/models/app.d.ts +9 -1
  73. package/esm/models/app.js +7 -1
  74. package/esm/models/{event.d.ts → audit-event.d.ts} +6 -6
  75. package/esm/models/{event.js → audit-event.js} +7 -6
  76. package/esm/models/{request-payload-preview.d.ts → body-preview.d.ts} +4 -4
  77. package/esm/models/{request-payload-preview.js → body-preview.js} +5 -4
  78. package/esm/models/create-environment.d.ts +51 -0
  79. package/esm/models/create-environment.js +35 -0
  80. package/esm/models/credentials.d.ts +31 -0
  81. package/esm/models/credentials.js +31 -0
  82. package/esm/models/environment-failure.d.ts +22 -0
  83. package/esm/models/environment-failure.js +25 -0
  84. package/esm/models/environment-status.d.ts +14 -0
  85. package/esm/models/environment-status.js +15 -0
  86. package/esm/models/environment.d.ts +27 -27
  87. package/esm/models/environment.js +28 -27
  88. package/esm/models/error.d.ts +38 -0
  89. package/esm/models/error.js +39 -0
  90. package/esm/models/errors/{error-response.d.ts → error.d.ts} +5 -5
  91. package/esm/models/errors/{error-response.js → error.js} +4 -4
  92. package/esm/models/errors/index.d.ts +1 -1
  93. package/esm/models/errors/index.js +1 -1
  94. package/esm/models/index.d.ts +17 -20
  95. package/esm/models/index.js +17 -20
  96. package/esm/models/me.d.ts +66 -0
  97. package/esm/models/me.js +75 -0
  98. package/esm/models/operations/create-credentials.d.ts +25 -0
  99. package/esm/models/operations/create-credentials.js +20 -0
  100. package/esm/models/operations/create-environment.d.ts +14 -0
  101. package/esm/models/operations/create-environment.js +18 -0
  102. package/esm/models/operations/create-snapshot.d.ts +25 -0
  103. package/esm/models/operations/create-snapshot.js +25 -0
  104. package/esm/models/operations/delete-environment.d.ts +13 -0
  105. package/esm/models/operations/delete-environment.js +17 -0
  106. package/esm/models/operations/delete-snapshot.d.ts +0 -12
  107. package/esm/models/operations/delete-snapshot.js +0 -9
  108. package/esm/models/operations/get-environment.d.ts +0 -12
  109. package/esm/models/operations/get-environment.js +0 -9
  110. package/esm/models/operations/get-request-body.d.ts +21 -0
  111. package/esm/models/operations/{get-environment-request-payload.js → get-request-body.js} +3 -4
  112. package/esm/models/operations/get-request-stats.d.ts +51 -0
  113. package/esm/models/operations/get-request-stats.js +40 -0
  114. package/esm/models/operations/get-request.d.ts +11 -0
  115. package/esm/models/operations/get-request.js +11 -0
  116. package/esm/models/operations/get-snapshot.d.ts +0 -12
  117. package/esm/models/operations/get-snapshot.js +0 -9
  118. package/esm/models/operations/get-workspace.d.ts +0 -12
  119. package/esm/models/operations/get-workspace.js +0 -9
  120. package/esm/models/operations/index.d.ts +13 -16
  121. package/esm/models/operations/index.js +13 -16
  122. package/esm/models/operations/list-apps.d.ts +26 -0
  123. package/esm/models/operations/list-apps.js +23 -0
  124. package/esm/models/operations/list-audit-events.d.ts +30 -0
  125. package/esm/models/operations/list-audit-events.js +25 -0
  126. package/esm/models/operations/list-environments.d.ts +6 -21
  127. package/esm/models/operations/list-environments.js +3 -16
  128. package/esm/models/operations/list-requests.d.ts +68 -0
  129. package/esm/models/operations/list-requests.js +53 -0
  130. package/esm/models/operations/list-snapshots.d.ts +8 -6
  131. package/esm/models/operations/list-snapshots.js +4 -3
  132. package/esm/models/operations/list-versions.d.ts +28 -0
  133. package/esm/models/operations/list-versions.js +24 -0
  134. package/esm/models/operations/list-workspaces.d.ts +8 -4
  135. package/esm/models/operations/list-workspaces.js +7 -4
  136. package/esm/models/operations/pause-environment.d.ts +13 -0
  137. package/esm/models/operations/pause-environment.js +17 -0
  138. package/esm/models/operations/resume-environment.d.ts +0 -12
  139. package/esm/models/operations/resume-environment.js +0 -9
  140. package/esm/models/operations/update-environment.d.ts +29 -0
  141. package/esm/models/operations/update-environment.js +20 -0
  142. package/esm/models/operations/update-snapshot.d.ts +0 -14
  143. package/esm/models/operations/update-snapshot.js +0 -10
  144. package/esm/models/permission.d.ts +17 -0
  145. package/esm/models/permission.js +18 -0
  146. package/esm/models/principal.d.ts +20 -0
  147. package/esm/models/principal.js +24 -0
  148. package/esm/models/request-detail.d.ts +39 -0
  149. package/esm/models/request-detail.js +38 -0
  150. package/esm/models/{environment-request-stats.d.ts → request-stats.d.ts} +3 -3
  151. package/esm/models/{environment-request-stats.js → request-stats.js} +3 -3
  152. package/esm/models/request.d.ts +30 -0
  153. package/esm/models/{environment-request.js → request.js} +11 -11
  154. package/esm/models/snapshot-status.d.ts +13 -0
  155. package/esm/models/snapshot-status.js +14 -0
  156. package/esm/models/snapshot.d.ts +47 -0
  157. package/esm/models/snapshot.js +54 -0
  158. package/esm/models/version.d.ts +27 -0
  159. package/esm/models/version.js +31 -0
  160. package/esm/models/workspace.d.ts +2 -4
  161. package/esm/models/workspace.js +2 -4
  162. package/esm/sdk/apps.d.ts +6 -0
  163. package/esm/sdk/apps.js +15 -0
  164. package/esm/sdk/audit-events.d.ts +5 -0
  165. package/esm/sdk/audit-events.js +11 -0
  166. package/esm/sdk/environments.d.ts +8 -13
  167. package/esm/sdk/environments.js +18 -38
  168. package/esm/sdk/requests.d.ts +9 -0
  169. package/esm/sdk/requests.js +23 -0
  170. package/esm/sdk/sdk.d.ts +11 -8
  171. package/esm/sdk/sdk.js +16 -11
  172. package/esm/sdk/snapshots.d.ts +6 -5
  173. package/esm/sdk/snapshots.js +7 -7
  174. package/esm/sdk/workspaces.d.ts +1 -2
  175. package/esm/sdk/workspaces.js +0 -4
  176. package/openapi.json +1697 -2507
  177. package/package.json +15 -13
  178. package/esm/extensions/apps.d.ts +0 -18
  179. package/esm/extensions/apps.js +0 -39
  180. package/esm/extensions/connection.d.ts +0 -42
  181. package/esm/extensions/connection.js +0 -133
  182. package/esm/extensions/running-environment.d.ts +0 -46
  183. package/esm/extensions/running-environment.js +0 -66
  184. package/esm/funcs/create-environment-definition.d.ts +0 -11
  185. package/esm/funcs/create-environment-definition.js +0 -94
  186. package/esm/funcs/credential-get.js +0 -76
  187. package/esm/funcs/environments-get-request-payload.d.ts +0 -11
  188. package/esm/funcs/environments-get-request.d.ts +0 -11
  189. package/esm/funcs/environments-get-request.js +0 -96
  190. package/esm/funcs/environments-list-requests.d.ts +0 -11
  191. package/esm/funcs/environments-reveal.d.ts +0 -11
  192. package/esm/funcs/environments-suspend.d.ts +0 -11
  193. package/esm/funcs/snapshots-capture.d.ts +0 -11
  194. package/esm/models/app-instance-snapshot.d.ts +0 -15
  195. package/esm/models/app-instance-snapshot.js +0 -19
  196. package/esm/models/catalog.d.ts +0 -12
  197. package/esm/models/catalog.js +0 -15
  198. package/esm/models/connection-secret.d.ts +0 -30
  199. package/esm/models/connection-secret.js +0 -31
  200. package/esm/models/connection.d.ts +0 -40
  201. package/esm/models/connection.js +0 -44
  202. package/esm/models/create-environment-input.d.ts +0 -39
  203. package/esm/models/create-environment-input.js +0 -25
  204. package/esm/models/credential-kind.d.ts +0 -9
  205. package/esm/models/credential-kind.js +0 -10
  206. package/esm/models/credential.d.ts +0 -48
  207. package/esm/models/credential.js +0 -44
  208. package/esm/models/environment-app.d.ts +0 -45
  209. package/esm/models/environment-app.js +0 -47
  210. package/esm/models/environment-lifetime.d.ts +0 -10
  211. package/esm/models/environment-lifetime.js +0 -14
  212. package/esm/models/environment-request-detail.d.ts +0 -40
  213. package/esm/models/environment-request-detail.js +0 -39
  214. package/esm/models/environment-request.d.ts +0 -31
  215. package/esm/models/environment-snapshot.d.ts +0 -40
  216. package/esm/models/environment-snapshot.js +0 -42
  217. package/esm/models/error-response.d.ts +0 -57
  218. package/esm/models/error-response.js +0 -59
  219. package/esm/models/event-page.d.ts +0 -11
  220. package/esm/models/event-page.js +0 -16
  221. package/esm/models/operations/capture-snapshot.d.ts +0 -41
  222. package/esm/models/operations/capture-snapshot.js +0 -36
  223. package/esm/models/operations/create-environment-definition.d.ts +0 -153
  224. package/esm/models/operations/create-environment-definition.js +0 -93
  225. package/esm/models/operations/create-environments.d.ts +0 -27
  226. package/esm/models/operations/create-environments.js +0 -27
  227. package/esm/models/operations/get-catalog.d.ts +0 -13
  228. package/esm/models/operations/get-catalog.js +0 -13
  229. package/esm/models/operations/get-connection.d.ts +0 -25
  230. package/esm/models/operations/get-connection.js +0 -21
  231. package/esm/models/operations/get-credential.d.ts +0 -13
  232. package/esm/models/operations/get-credential.js +0 -13
  233. package/esm/models/operations/get-environment-request-payload.d.ts +0 -23
  234. package/esm/models/operations/get-environment-request-stats.d.ts +0 -70
  235. package/esm/models/operations/get-environment-request-stats.js +0 -55
  236. package/esm/models/operations/get-environment-request.d.ts +0 -25
  237. package/esm/models/operations/get-environment-request.js +0 -21
  238. package/esm/models/operations/get-release.d.ts +0 -23
  239. package/esm/models/operations/get-release.js +0 -20
  240. package/esm/models/operations/list-environment-events.d.ts +0 -15
  241. package/esm/models/operations/list-environment-events.js +0 -13
  242. package/esm/models/operations/list-environment-requests.d.ts +0 -75
  243. package/esm/models/operations/list-environment-requests.js +0 -59
  244. package/esm/models/operations/list-workspace-events.d.ts +0 -15
  245. package/esm/models/operations/list-workspace-events.js +0 -13
  246. package/esm/models/operations/reveal-connection.d.ts +0 -59
  247. package/esm/models/operations/reveal-connection.js +0 -55
  248. package/esm/models/operations/stop-environment.d.ts +0 -25
  249. package/esm/models/operations/stop-environment.js +0 -26
  250. package/esm/models/operations/suspend-environment.d.ts +0 -25
  251. package/esm/models/operations/suspend-environment.js +0 -26
  252. package/esm/models/release-actor.d.ts +0 -11
  253. package/esm/models/release-actor.js +0 -15
  254. package/esm/models/release.d.ts +0 -37
  255. package/esm/models/release.js +0 -39
  256. package/esm/sdk/catalog.d.ts +0 -6
  257. package/esm/sdk/catalog.js +0 -15
  258. package/esm/sdk/credential.d.ts +0 -5
  259. package/esm/sdk/credential.js +0 -11
@@ -0,0 +1,73 @@
1
+ const HTTP_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
2
+ // Checked on the path part only. Covers protocol-relative starts, backslashes, control
3
+ // characters and encoded dots, slashes and backslashes, including double encoding.
4
+ const UNSAFE_PATH = /^\/\/|\\|%(?:25)*(?:2e|2f|5c)|\p{Cc}/iu;
5
+ const OUTSIDE = 'Credentials.fetch only reaches paths under the app url.';
6
+ export function bindCredentials(data, apis, fetcher) {
7
+ const base = URL.canParse(data.url) ? new URL(data.url) : null;
8
+ if (!base || !/^https?:$/.test(base.protocol) || base.search || base.hash) {
9
+ throw new TypeError('The credentials url is not a plain http(s) URL.');
10
+ }
11
+ if (!base.pathname.endsWith('/')) {
12
+ base.pathname += '/';
13
+ }
14
+ const send = async (path, init = {}) => {
15
+ const url = resolve(base, path);
16
+ const headers = new Headers(init.headers);
17
+ const actor = actorHeader(apis, `/${url.pathname.slice(base.pathname.length)}`, data.token);
18
+ // Ours go last so a caller header of the same name cannot replace them.
19
+ for (const [name, value] of Object.entries(data.headers)) {
20
+ headers.set(name, value);
21
+ }
22
+ if (actor) {
23
+ headers.set(actor.name, actor.value);
24
+ }
25
+ const request = new Request(url, { ...init, headers, redirect: 'manual' });
26
+ // Called on `globalThis` at use time: a stored unbound `fetch` throws "Illegal invocation" in Workers.
27
+ return fetcher ? fetcher(request) : globalThis.fetch(request);
28
+ };
29
+ return Object.defineProperty({ ...data }, 'fetch', {
30
+ value: send,
31
+ enumerable: false,
32
+ });
33
+ }
34
+ /** The longest `pathTemplate` that prefixes the path names the API; `{...}` matches one segment. */
35
+ function actorHeader(apis, path, token) {
36
+ let match = null;
37
+ for (const api of apis) {
38
+ const pattern = api.pathTemplate
39
+ .replace(/\/+$/, '')
40
+ .split(/\{[^}]*\}/)
41
+ .map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
42
+ .join('[^/]+');
43
+ if (new RegExp(`^${pattern}(?:/|$)`).test(path) &&
44
+ api.pathTemplate.length > (match?.pathTemplate.length ?? -1)) {
45
+ match = api;
46
+ }
47
+ }
48
+ if (!match || !HTTP_TOKEN.test(match.actorHeader)) {
49
+ return null;
50
+ }
51
+ return match.actorHeader.toLowerCase() === 'authorization'
52
+ ? { name: 'Authorization', value: `Bearer ${token}` }
53
+ : { name: match.actorHeader, value: token };
54
+ }
55
+ function resolve(base, path) {
56
+ const pathPart = path.split(/[?#]/, 1)[0] ?? '';
57
+ if (UNSAFE_PATH.test(pathPart)) {
58
+ throw new TypeError(OUTSIDE);
59
+ }
60
+ // A leading `/` means the root of the app, not of the gateway.
61
+ const relative = path.startsWith('/') ? `.${path}` : path;
62
+ // `URL` errors carry the input, query included, so they are replaced rather than rethrown.
63
+ const url = URL.canParse(relative, base) ? new URL(relative, base) : null;
64
+ if (!url ||
65
+ url.origin !== base.origin ||
66
+ url.username ||
67
+ url.password ||
68
+ !url.pathname.startsWith(base.pathname) ||
69
+ UNSAFE_PATH.test(url.pathname)) {
70
+ throw new TypeError(OUTSIDE);
71
+ }
72
+ return url;
73
+ }
@@ -0,0 +1,25 @@
1
+ import type { Environment as EnvironmentData } from '../models/environment.js';
2
+ import type { Snapshot } from '../models/snapshot.js';
3
+ import type { ConnectOptions, CreateSnapshotOptions, ResumeOptions, SignalOptions } from './client.js';
4
+ import type { Credentials } from './credentials.js';
5
+ /**
6
+ * An environment as the API returned it, with helpers bound to it. The helpers are not
7
+ * enumerable, so `JSON.stringify` and spreading see only the API's fields.
8
+ */
9
+ export type Environment = EnvironmentData & EnvironmentHelpers;
10
+ export interface EnvironmentHelpers {
11
+ /** Credentials for one app; `actorId` defaults to the app's default actor. */
12
+ connect(app: string, options?: ConnectOptions): Promise<Credentials>;
13
+ pause(options?: SignalOptions): Promise<Environment>;
14
+ /** Waits until `running` unless `wait` is false. */
15
+ resume(options?: ResumeOptions): Promise<Environment>;
16
+ delete(options?: SignalOptions): Promise<Environment>;
17
+ /** Reads the environment again. */
18
+ refresh(): Promise<Environment>;
19
+ readonly snapshots: {
20
+ /** Waits until `ready` unless `wait` is false. */
21
+ create(input?: {
22
+ name?: string | null;
23
+ }, options?: CreateSnapshotOptions): Promise<Snapshot>;
24
+ };
25
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,96 +1,63 @@
1
- import type { Environment } from '../models/environment.js';
2
- import type { EnvironmentSnapshot } from '../models/environment-snapshot.js';
3
- import type { Code } from '../models/error-response.js';
4
- /** What `environments.start()` adds to the error it rethrows after a failed wait. */
5
- export interface StartFailure {
6
- /** The environment the start created. */
7
- readonly environmentId?: string;
8
- /** Whether the stop request succeeded. Disposal may still be in progress. */
9
- readonly stopped?: boolean;
10
- /** Why the stop request failed, when it did. */
11
- readonly stopError?: unknown;
12
- }
13
- interface Details {
1
+ import type { Environment as EnvironmentData } from '../models/environment.js';
2
+ import type { ErrorCode } from '../models/error.js';
3
+ import type { Snapshot } from '../models/snapshot.js';
4
+ export interface ErrorDetails {
14
5
  readonly message: string;
15
6
  readonly cause?: unknown;
7
+ /** The HTTP status, or `null` when no response arrived or the SDK refused the input. */
16
8
  readonly status: number | null;
17
- readonly code: Code | null;
9
+ readonly code: ErrorCode | null;
18
10
  readonly requestId: string | null;
11
+ /** Messages keyed by the dotted path of the field they are about. */
19
12
  readonly fields: Record<string, string[]> | null;
20
- readonly retryAfter: number | null;
21
- readonly retryable: boolean;
22
- readonly idempotencyKey?: string;
13
+ /** How long the server asked the client to wait. */
14
+ readonly retryAfterMs: number | null;
23
15
  }
24
16
  /**
25
- * Any failed API call: an error response, an invalid response, no response at all,
26
- * or input the SDK rejected before sending (`status` null for the last three). The
27
- * message never includes a response body.
17
+ * Any failed call: an error response, an invalid response, no response at all, or input
18
+ * the SDK refused before sending. The message never includes a response body.
28
19
  */
29
- export declare class StateMachinesError extends Error implements Details, StartFailure {
30
- readonly name = "StateMachinesError";
31
- readonly environmentId?: string;
32
- readonly stopped?: boolean;
33
- readonly stopError?: unknown;
34
- /** Set when `snapshots.capture()` fails after the snapshot was created. */
35
- readonly snapshotId?: string;
20
+ export declare class StateMachinesError extends Error implements ErrorDetails {
21
+ readonly name: string;
36
22
  readonly status: number | null;
37
- readonly code: Code | null;
23
+ readonly code: ErrorCode | null;
38
24
  readonly requestId: string | null;
39
25
  readonly fields: Record<string, string[]> | null;
40
- /** Milliseconds the server asked the client to wait. */
41
- readonly retryAfter: number | null;
42
- /** Transient: a 502, 503 or 504, or the request never got a response. */
43
- readonly retryable: boolean;
44
- /** Set on create and capture failures. A replay with it returns the same result, so use it when the outcome is unknown. */
45
- readonly idempotencyKey?: string;
46
- /** Wraps any thrown value. Wrapping a `StateMachinesError` copies it, keeping its cause. */
47
- constructor(error: unknown, extra?: {
48
- idempotencyKey?: string;
49
- });
26
+ readonly retryAfterMs: number | null;
27
+ constructor(details: ErrorDetails);
50
28
  }
51
- export declare function toStateMachinesError(error: unknown): StateMachinesError;
52
- export declare class EnvironmentFailedError extends Error implements StartFailure {
53
- readonly environment: Environment;
29
+ export declare function isTransient(error: StateMachinesError): boolean;
30
+ /** An environment the wait cannot bring to the target status because it failed. */
31
+ export declare class EnvironmentFailedError extends StateMachinesError {
32
+ readonly environment: EnvironmentData;
54
33
  readonly name = "EnvironmentFailedError";
55
- readonly environmentId?: string;
56
- readonly stopped?: boolean;
57
- readonly stopError?: unknown;
58
- constructor(environment: Environment);
59
- }
60
- export declare class DisposalIncompleteError extends Error {
61
- readonly environment: Environment;
62
- readonly name = "DisposalIncompleteError";
63
- constructor(environment: Environment);
34
+ constructor(environment: EnvironmentData);
64
35
  }
65
- export declare class TimeoutError extends Error implements StartFailure {
36
+ /** A snapshot that ended `failed` while the SDK waited for it. */
37
+ export declare class SnapshotFailedError extends StateMachinesError {
38
+ readonly snapshot: Snapshot;
39
+ readonly name = "SnapshotFailedError";
40
+ constructor(snapshot: Snapshot);
41
+ }
42
+ /** The resource reached a status from which the awaited one needs a call of yours. */
43
+ export declare class UnexpectedStatusError extends Error {
44
+ readonly resource: EnvironmentData | Snapshot;
45
+ readonly name = "UnexpectedStatusError";
46
+ readonly status: string;
47
+ constructor(resource: EnvironmentData | Snapshot, expected: string);
48
+ }
49
+ /** The wait passed its deadline. The work it waited for may still finish. */
50
+ export declare class WaitTimeoutError extends Error {
51
+ readonly resource: EnvironmentData | Snapshot | null;
66
52
  readonly timeoutMs: number;
67
- readonly environment: Environment | null;
68
- readonly name = "TimeoutError";
69
- readonly environmentId?: string;
70
- readonly stopped?: boolean;
71
- readonly stopError?: unknown;
72
- constructor(timeoutMs: number, environment: Environment | null);
53
+ readonly name = "WaitTimeoutError";
54
+ constructor(resource: EnvironmentData | Snapshot | null, timeoutMs: number, id: string);
73
55
  }
74
56
  /** Thrown from a hook so the generated retry loop stops, whatever the abort reason. */
75
57
  export declare class CallerAbortedError extends Error {
76
58
  readonly name = "CallerAbortedError";
77
59
  constructor(reason: unknown);
78
60
  }
79
- /** A snapshot that ended `failed`, or was deleted, while the SDK waited for `ready`. */
80
- export declare class SnapshotFailedError extends Error {
81
- readonly snapshot: EnvironmentSnapshot;
82
- readonly name = "SnapshotFailedError";
83
- readonly snapshotId: string;
84
- readonly idempotencyKey?: string;
85
- constructor(snapshot: EnvironmentSnapshot);
86
- }
87
- /** The wait for `ready` passed its deadline. The capture may still finish on the server. */
88
- export declare class SnapshotTimeoutError extends Error {
89
- readonly timeoutMs: number;
90
- readonly snapshot: EnvironmentSnapshot | null;
91
- readonly snapshotId: string;
92
- readonly name = "SnapshotTimeoutError";
93
- readonly idempotencyKey?: string;
94
- constructor(timeoutMs: number, snapshot: EnvironmentSnapshot | null, snapshotId: string);
95
- }
96
- export {};
61
+ /** The SDK refused input before sending it, as the API would with `invalid_request`. */
62
+ export declare function invalidInput(message: string, fields: Record<string, string[]>): StateMachinesError;
63
+ export declare function toStateMachinesError(error: unknown): StateMachinesError;
@@ -1,11 +1,10 @@
1
- import { ErrorResponse } from '../models/errors/error-response.js';
1
+ import { ErrorT } from '../models/errors/error.js';
2
2
  import { ConnectionError, RequestTimeoutError, } from '../models/errors/http-client-errors.js';
3
3
  import { ResponseValidationError } from '../models/errors/response-validation-error.js';
4
4
  import { StateMachinesHTTPError } from '../models/errors/state-machines-http-error.js';
5
5
  /**
6
- * Any failed API call: an error response, an invalid response, no response at all,
7
- * or input the SDK rejected before sending (`status` null for the last three). The
8
- * message never includes a response body.
6
+ * Any failed call: an error response, an invalid response, no response at all, or input
7
+ * the SDK refused before sending. The message never includes a response body.
9
8
  */
10
9
  export class StateMachinesError extends Error {
11
10
  name = 'StateMachinesError';
@@ -13,84 +12,159 @@ export class StateMachinesError extends Error {
13
12
  code;
14
13
  requestId;
15
14
  fields;
16
- /** Milliseconds the server asked the client to wait. */
17
- retryAfter;
18
- /** Transient: a 502, 503 or 504, or the request never got a response. */
19
- retryable;
20
- /** Set on create and capture failures. A replay with it returns the same result, so use it when the outcome is unknown. */
21
- idempotencyKey;
22
- /** Wraps any thrown value. Wrapping a `StateMachinesError` copies it, keeping its cause. */
23
- constructor(error, extra = {}) {
24
- const details = error instanceof StateMachinesError ? error : describe(error);
15
+ retryAfterMs;
16
+ constructor(details) {
25
17
  super(details.message, details.cause === undefined ? undefined : { cause: details.cause });
26
18
  this.status = details.status;
27
19
  this.code = details.code;
28
20
  this.requestId = details.requestId;
29
21
  this.fields = details.fields;
30
- this.retryAfter = details.retryAfter;
31
- this.retryable = details.retryable;
32
- const idempotencyKey = extra.idempotencyKey ?? details.idempotencyKey;
33
- if (idempotencyKey !== undefined) {
34
- this.idempotencyKey = idempotencyKey;
35
- }
22
+ this.retryAfterMs = details.retryAfterMs;
36
23
  }
37
24
  }
38
- export function toStateMachinesError(error) {
39
- return error instanceof StateMachinesError
40
- ? error
41
- : new StateMachinesError(error);
25
+ // A 502, 503 or 504, or a request that never got a response: the poller tries again.
26
+ const transient = new WeakSet();
27
+ export function isTransient(error) {
28
+ return transient.has(error);
42
29
  }
43
- // Matches the generated retry codes (.speakeasy/overlay.yaml): Cloudflare answers 502 and 504
44
- // when the API origin is unreachable, and the API answers 503 when it is saturated.
45
- const retryableStatuses = new Set([502, 503, 504]);
46
- function describe(error) {
47
- const empty = {
48
- status: null,
49
- code: null,
50
- requestId: null,
51
- fields: null,
52
- retryAfter: null,
53
- retryable: false,
54
- };
30
+ function transientError(details) {
31
+ const error = new StateMachinesError(details);
32
+ transient.add(error);
33
+ return error;
34
+ }
35
+ const noResponse = {
36
+ status: null,
37
+ code: null,
38
+ requestId: null,
39
+ fields: null,
40
+ retryAfterMs: null,
41
+ };
42
+ /** An environment the wait cannot bring to the target status because it failed. */
43
+ export class EnvironmentFailedError extends StateMachinesError {
44
+ environment;
45
+ name = 'EnvironmentFailedError';
46
+ constructor(environment) {
47
+ const failure = environment.failure;
48
+ super({
49
+ ...noResponse,
50
+ message: failure
51
+ ? `Environment ${environment.id} failed: ${failure.message} (${failure.code}, app ${failure.app}).`
52
+ : `Environment ${environment.id} failed.`,
53
+ });
54
+ this.environment = environment;
55
+ }
56
+ }
57
+ /** A snapshot that ended `failed` while the SDK waited for it. */
58
+ export class SnapshotFailedError extends StateMachinesError {
59
+ snapshot;
60
+ name = 'SnapshotFailedError';
61
+ constructor(snapshot) {
62
+ const failure = snapshot.failure;
63
+ super({
64
+ ...noResponse,
65
+ message: failure
66
+ ? `Snapshot ${snapshot.id} failed: ${failure.message} (${failure.code}).`
67
+ : `Snapshot ${snapshot.id} failed.`,
68
+ });
69
+ this.snapshot = snapshot;
70
+ }
71
+ }
72
+ /** The resource reached a status from which the awaited one needs a call of yours. */
73
+ export class UnexpectedStatusError extends Error {
74
+ resource;
75
+ name = 'UnexpectedStatusError';
76
+ status;
77
+ constructor(resource, expected) {
78
+ super(`${resource.id} is ${resource.status}; it cannot become ${expected} without another call.`);
79
+ this.resource = resource;
80
+ this.status = resource.status;
81
+ }
82
+ }
83
+ /** The wait passed its deadline. The work it waited for may still finish. */
84
+ export class WaitTimeoutError extends Error {
85
+ resource;
86
+ timeoutMs;
87
+ name = 'WaitTimeoutError';
88
+ constructor(resource, timeoutMs, id) {
89
+ super(`Timed out after ${timeoutMs} ms waiting for ${id}.`);
90
+ this.resource = resource;
91
+ this.timeoutMs = timeoutMs;
92
+ }
93
+ }
94
+ /** Thrown from a hook so the generated retry loop stops, whatever the abort reason. */
95
+ export class CallerAbortedError extends Error {
96
+ name = 'CallerAbortedError';
97
+ constructor(reason) {
98
+ super('The caller aborted the request.', { cause: reason });
99
+ }
100
+ }
101
+ /** The SDK refused input before sending it, as the API would with `invalid_request`. */
102
+ export function invalidInput(message, fields) {
103
+ return new StateMachinesError({
104
+ ...noResponse,
105
+ message,
106
+ code: 'invalid_request',
107
+ fields,
108
+ });
109
+ }
110
+ export function toStateMachinesError(error) {
111
+ if (error instanceof StateMachinesError) {
112
+ return error;
113
+ }
55
114
  if (error instanceof StateMachinesHTTPError) {
56
- return describeResponse(error);
115
+ return fromResponse(error);
57
116
  }
58
117
  if (error instanceof SyntaxError) {
59
118
  // A malformed JSON body; V8 quotes the body in the parser's message.
60
- return { ...empty, message: 'Invalid response from the State API.' };
119
+ return new StateMachinesError({
120
+ ...noResponse,
121
+ message: 'Invalid response from the State Machines API.',
122
+ });
61
123
  }
62
124
  if (error instanceof Error) {
63
125
  // A `TimeoutError` DOMException is an attempt timeout that fired while the body was read.
64
126
  const retryable = error instanceof ConnectionError ||
65
127
  error instanceof RequestTimeoutError ||
66
128
  error.name === 'TimeoutError';
67
- return { ...empty, message: error.message, cause: error, retryable };
129
+ const details = { ...noResponse, message: error.message, cause: error };
130
+ return retryable
131
+ ? transientError(details)
132
+ : new StateMachinesError(details);
68
133
  }
69
- return { ...empty, message: String(error), cause: error };
134
+ return new StateMachinesError({
135
+ ...noResponse,
136
+ message: String(error),
137
+ cause: error,
138
+ });
70
139
  }
71
- function describeResponse(error) {
140
+ // The generated retry codes (.speakeasy/overlay.yaml): Cloudflare answers 502 and 504 when
141
+ // the API origin is unreachable, and the API answers 503 when it is saturated.
142
+ const retryableStatuses = new Set([502, 503, 504]);
143
+ function fromResponse(error) {
72
144
  const status = error.statusCode;
73
- const body = error instanceof ErrorResponse ? error.error : undefined;
74
- const success = status >= 200 && status < 300;
75
- let message = `HTTP ${status} from the State API`;
76
- if (success || error instanceof ResponseValidationError) {
77
- message = `Invalid response from the State API (HTTP ${status})`;
145
+ const body = error instanceof ErrorT ? error.error : undefined;
146
+ let message = `HTTP ${status} from the State Machines API`;
147
+ if ((status >= 200 && status < 300) ||
148
+ error instanceof ResponseValidationError) {
149
+ message = `Invalid response from the State Machines API (HTTP ${status})`;
78
150
  }
79
151
  else if (body?.message) {
80
152
  message = body.message;
81
153
  }
82
- return {
154
+ // No cause: generated HTTP errors hold the request, with its token, and the raw body.
155
+ const details = {
83
156
  message,
84
- // No cause: generated HTTP errors hold the request (with the API key) and the raw body.
85
157
  status,
86
158
  code: body?.code ?? null,
87
- requestId: body?.requestId ?? error.headers.get('x-request-id'),
159
+ requestId: body?.requestId ?? error.headers.get('request-id'),
88
160
  fields: body?.fields ?? null,
89
- retryAfter: retryAfter(error.headers),
90
- retryable: retryableStatuses.has(status),
161
+ retryAfterMs: retryAfterMs(error.headers),
91
162
  };
163
+ return retryableStatuses.has(status)
164
+ ? transientError(details)
165
+ : new StateMachinesError(details);
92
166
  }
93
- function retryAfter(headers) {
167
+ function retryAfterMs(headers) {
94
168
  const value = headers.get('retry-after')?.trim();
95
169
  if (!value) {
96
170
  return null;
@@ -102,62 +176,3 @@ function retryAfter(headers) {
102
176
  const date = Date.parse(value);
103
177
  return Number.isNaN(date) ? null : Math.max(0, date - Date.now());
104
178
  }
105
- export class EnvironmentFailedError extends Error {
106
- environment;
107
- name = 'EnvironmentFailedError';
108
- constructor(environment) {
109
- super(`Environment ${environment.id} is ${environment.status} (${environment.stopReason ?? 'no stop reason'}).`);
110
- this.environment = environment;
111
- }
112
- }
113
- export class DisposalIncompleteError extends Error {
114
- environment;
115
- name = 'DisposalIncompleteError';
116
- constructor(environment) {
117
- super(`Environment ${environment.id} ended, but not every sandbox was confirmed disposed.`);
118
- this.environment = environment;
119
- }
120
- }
121
- export class TimeoutError extends Error {
122
- timeoutMs;
123
- environment;
124
- name = 'TimeoutError';
125
- constructor(timeoutMs, environment) {
126
- super(`Timed out after ${timeoutMs} ms.`);
127
- this.timeoutMs = timeoutMs;
128
- this.environment = environment;
129
- }
130
- }
131
- /** Thrown from a hook so the generated retry loop stops, whatever the abort reason. */
132
- export class CallerAbortedError extends Error {
133
- name = 'CallerAbortedError';
134
- constructor(reason) {
135
- super('The caller aborted the request.', { cause: reason });
136
- }
137
- }
138
- /** A snapshot that ended `failed`, or was deleted, while the SDK waited for `ready`. */
139
- export class SnapshotFailedError extends Error {
140
- snapshot;
141
- name = 'SnapshotFailedError';
142
- snapshotId;
143
- constructor(snapshot) {
144
- super(snapshot.status === 'failed'
145
- ? `Snapshot ${snapshot.id} is failed (${snapshot.errorCode ?? 'no error code'}).`
146
- : `Snapshot ${snapshot.id} was deleted.`);
147
- this.snapshot = snapshot;
148
- this.snapshotId = snapshot.id;
149
- }
150
- }
151
- /** The wait for `ready` passed its deadline. The capture may still finish on the server. */
152
- export class SnapshotTimeoutError extends Error {
153
- timeoutMs;
154
- snapshot;
155
- snapshotId;
156
- name = 'SnapshotTimeoutError';
157
- constructor(timeoutMs, snapshot, snapshotId) {
158
- super(`Timed out after ${timeoutMs} ms waiting for snapshot ${snapshotId}.`);
159
- this.timeoutMs = timeoutMs;
160
- this.snapshot = snapshot;
161
- this.snapshotId = snapshotId;
162
- }
163
- }
@@ -1,13 +1,8 @@
1
- export type { Fetcher, HTTPClientOptions } from '../lib/http.js';
2
- export { HTTPClient } from '../lib/http.js';
3
- export { ConnectionError, ErrorResponse, HTTPClientError, InvalidRequestError, RequestAbortedError, RequestTimeoutError, ResponseValidationError, SDKValidationError, StateMachinesHTTPError, UnexpectedClientError, UnexpectedResponseError, } from '../models/errors/index.js';
4
- export type { ApiProfile, App, AppInstanceSnapshot, Catalog, Connection as ConnectionDetails, ConnectionSecret, CreateEnvironmentInput, Credential, Environment, EnvironmentApp, EnvironmentRequest, EnvironmentRequestDetail, EnvironmentRequestStats, EnvironmentSnapshot, Event, EventPage, Release, ReleaseActor, Workspace, } from '../models/index.js';
5
- export { Code as ErrorCode, Disposal, EnvironmentStatus, StopReason, } from '../models/index.js';
6
- export { Status as EnvironmentListStatus } from '../models/operations/list-environments.js';
7
- export type { AppInput, EnvironmentInput, SnapshotEnvironmentInput, } from './apps.js';
8
- export type { CallOptions, CaptureInput, CaptureOptions, ConnectOptions, CreateOptions, LifecycleOptions, ListEnvironmentsQuery, ListSnapshotsQuery, Page, StartOptions, StateMachinesOptions, } from './client.js';
1
+ export type { Actor, Api, App, AuditEvent, BodyPreview, CreateEnvironment, CreateEnvironmentApp, EnvironmentApp, EnvironmentFailure, EnvironmentFailureCode, EnvironmentStatus, ErrorCode, Me, Permission, Principal, RequestDetail, RequestStats, RequestT as Request, Snapshot, SnapshotStatus, Version, Workspace, } from '../models/index.js';
2
+ export type { ConnectOptions, CreateEnvironmentOptions, CreateSnapshotOptions, ListAuditEventsQuery, ListEnvironmentsQuery, ListRequestsQuery, ListSnapshotsQuery, ListWorkspacesQuery, Page, PageQuery, RequestStatsQuery, ResumeOptions, SignalOptions, StateMachinesOptions, } from './client.js';
9
3
  export { StateMachines } from './client.js';
10
- export { Connection, formatActorHeader } from './connection.js';
11
- export { DisposalIncompleteError, EnvironmentFailedError, SnapshotFailedError, SnapshotTimeoutError, StateMachinesError, TimeoutError, } from './errors.js';
12
- export { RunningEnvironment } from './running-environment.js';
4
+ export type { Credentials } from './credentials.js';
5
+ export type { Environment, EnvironmentHelpers } from './environment.js';
6
+ export { EnvironmentFailedError, SnapshotFailedError, StateMachinesError, UnexpectedStatusError, WaitTimeoutError, } from './errors.js';
13
7
  export type { WaitOptions } from './wait.js';
8
+ export { parseEnvironmentYaml } from './yaml.js';
@@ -1,8 +1,3 @@
1
- export { HTTPClient } from '../lib/http.js';
2
- export { ConnectionError, ErrorResponse, HTTPClientError, InvalidRequestError, RequestAbortedError, RequestTimeoutError, ResponseValidationError, SDKValidationError, StateMachinesHTTPError, UnexpectedClientError, UnexpectedResponseError, } from '../models/errors/index.js';
3
- export { Code as ErrorCode, Disposal, EnvironmentStatus, StopReason, } from '../models/index.js';
4
- export { Status as EnvironmentListStatus } from '../models/operations/list-environments.js';
5
1
  export { StateMachines } from './client.js';
6
- export { Connection, formatActorHeader } from './connection.js';
7
- export { DisposalIncompleteError, EnvironmentFailedError, SnapshotFailedError, SnapshotTimeoutError, StateMachinesError, TimeoutError, } from './errors.js';
8
- export { RunningEnvironment } from './running-environment.js';
2
+ export { EnvironmentFailedError, SnapshotFailedError, StateMachinesError, UnexpectedStatusError, WaitTimeoutError, } from './errors.js';
3
+ export { parseEnvironmentYaml } from './yaml.js';
@@ -1,29 +1,28 @@
1
- import type { Environment } from '../models/environment.js';
2
- import type { EnvironmentSnapshot } from '../models/environment-snapshot.js';
3
- export interface WaitOptions {
4
- /** Overall deadline for the wait. Defaults to 10 minutes. */
1
+ import type { Environment as EnvironmentData } from '../models/environment.js';
2
+ import type { EnvironmentStatus } from '../models/environment-status.js';
3
+ import type { Snapshot } from '../models/snapshot.js';
4
+ import type { SnapshotStatus } from '../models/snapshot-status.js';
5
+ export interface WaitOptions<Status extends string> {
6
+ status: Status;
7
+ /** Deadline for the whole wait. Defaults to 10 minutes. */
5
8
  timeoutMs?: number;
6
9
  /** Aborting rejects the wait with `signal.reason`. */
7
10
  signal?: AbortSignal;
8
11
  }
9
- type Read<T> = (signal: AbortSignal) => Promise<T>;
10
- type Decide<T> = (value: T) => boolean;
11
12
  export declare const defaultTimeoutMs = 600000;
12
- export declare function poll(read: Read<Environment>, done: Decide<Environment>, options?: WaitOptions): Promise<Environment>;
13
+ export type KnownEnvironmentStatus = (typeof EnvironmentStatus)[keyof typeof EnvironmentStatus];
14
+ export type KnownSnapshotStatus = (typeof SnapshotStatus)[keyof typeof SnapshotStatus];
15
+ /** True at the target; false while the target is still reachable; throws otherwise. */
16
+ export declare function environmentAt(target: KnownEnvironmentStatus): (environment: EnvironmentData) => boolean;
17
+ export declare function snapshotAt(target: KnownSnapshotStatus): (snapshot: Snapshot) => boolean;
13
18
  /**
14
- * Reads until `done` returns true. `done` throws to end the wait with a failure.
19
+ * Reads until `done` returns true; `done` throws to end the wait with a failure.
15
20
  * Transient read failures are retried with backoff until the deadline.
16
21
  */
17
- export declare function pollUntil<T>(read: Read<T>, done: Decide<T>, options: WaitOptions | undefined, timeout: (timeoutMs: number, last: T | null) => Error): Promise<T>;
18
- /** Resolves on `running`. Never suspends or stops the environment. */
19
- export declare function ready(environment: Environment): boolean;
20
- /** Resolves on `suspended`, when the environment can resume. */
21
- export declare function suspended(environment: Environment): boolean;
22
- /** Resolves once the environment ended and every app's sandbox is confirmed disposed. */
23
- export declare function disposed(environment: Environment): boolean;
24
- /** Resolves on `ready`. A failed or deleted snapshot ends the wait. */
25
- export declare function snapshotReady(snapshot: EnvironmentSnapshot): boolean;
22
+ export declare function poll<T extends EnvironmentData | Snapshot>(id: string, read: (signal: AbortSignal) => Promise<T>, done: (value: T) => boolean, options?: {
23
+ timeoutMs?: number;
24
+ signal?: AbortSignal;
25
+ }): Promise<T>;
26
26
  /** 1 s × 1.5ⁿ, capped at 5 s, with ±20% jitter. */
27
27
  export declare function backoff(attempt: number): number;
28
28
  export declare function sleep(ms: number, signal: AbortSignal | null | undefined): Promise<void>;
29
- export {};