@usestatemachines/cli 0.0.0-stage → 0.1.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.
Files changed (169) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +34 -2
  3. package/dist/agents/bundled.d.ts +5 -0
  4. package/dist/agents/bundled.js +6 -0
  5. package/dist/agents/destinations.d.ts +13 -0
  6. package/dist/agents/destinations.js +16 -0
  7. package/dist/api-internal.d.ts +62 -0
  8. package/dist/api-internal.js +348 -0
  9. package/dist/bin.d.ts +2 -0
  10. package/dist/bin.js +4 -0
  11. package/dist/command.d.ts +119 -0
  12. package/dist/command.js +195 -0
  13. package/dist/commands/api/command.d.ts +15 -0
  14. package/dist/commands/api/command.js +38 -0
  15. package/dist/commands/api/run.d.ts +4 -0
  16. package/dist/commands/api/run.js +24 -0
  17. package/dist/commands/apps/command.d.ts +19 -0
  18. package/dist/commands/apps/command.js +29 -0
  19. package/dist/commands/apps/list.d.ts +8 -0
  20. package/dist/commands/apps/list.js +10 -0
  21. package/dist/commands/connect/command.d.ts +34 -0
  22. package/dist/commands/connect/command.js +70 -0
  23. package/dist/commands/connect/run.d.ts +10 -0
  24. package/dist/commands/connect/run.js +71 -0
  25. package/dist/commands/doctor/command.d.ts +29 -0
  26. package/dist/commands/doctor/command.js +36 -0
  27. package/dist/commands/doctor/run.d.ts +13 -0
  28. package/dist/commands/doctor/run.js +12 -0
  29. package/dist/commands/env/command.d.ts +5 -0
  30. package/dist/commands/env/command.js +19 -0
  31. package/dist/commands/env/pull.d.ts +6 -0
  32. package/dist/commands/env/pull.js +6 -0
  33. package/dist/commands/environments/command.d.ts +121 -0
  34. package/dist/commands/environments/command.js +170 -0
  35. package/dist/commands/environments/create.d.ts +9 -0
  36. package/dist/commands/environments/create.js +42 -0
  37. package/dist/commands/environments/delete.d.ts +7 -0
  38. package/dist/commands/environments/delete.js +48 -0
  39. package/dist/commands/environments/get.d.ts +39 -0
  40. package/dist/commands/environments/get.js +5 -0
  41. package/dist/commands/environments/list.d.ts +41 -0
  42. package/dist/commands/environments/list.js +18 -0
  43. package/dist/commands/environments/wait.d.ts +39 -0
  44. package/dist/commands/environments/wait.js +21 -0
  45. package/dist/commands/init/command.d.ts +68 -0
  46. package/dist/commands/init/command.js +90 -0
  47. package/dist/commands/init/run.d.ts +8 -0
  48. package/dist/commands/init/run.js +422 -0
  49. package/dist/commands/link/command.d.ts +13 -0
  50. package/dist/commands/link/command.js +32 -0
  51. package/dist/commands/link/run.d.ts +12 -0
  52. package/dist/commands/link/run.js +7 -0
  53. package/dist/commands/login/command.d.ts +1 -0
  54. package/dist/commands/login/command.js +43 -0
  55. package/dist/commands/login/run.d.ts +30 -0
  56. package/dist/commands/login/run.js +43 -0
  57. package/dist/commands/login/schema.d.ts +33 -0
  58. package/dist/commands/login/schema.js +33 -0
  59. package/dist/commands/logout/command.d.ts +1 -0
  60. package/dist/commands/logout/command.js +24 -0
  61. package/dist/commands/logout/run.d.ts +10 -0
  62. package/dist/commands/logout/run.js +19 -0
  63. package/dist/commands/logout/schema.d.ts +10 -0
  64. package/dist/commands/logout/schema.js +10 -0
  65. package/dist/commands/profiles/command.d.ts +2 -0
  66. package/dist/commands/profiles/command.js +29 -0
  67. package/dist/commands/profiles/list.d.ts +20 -0
  68. package/dist/commands/profiles/list.js +10 -0
  69. package/dist/commands/profiles/schema.d.ts +24 -0
  70. package/dist/commands/profiles/schema.js +19 -0
  71. package/dist/commands/profiles/use.d.ts +7 -0
  72. package/dist/commands/profiles/use.js +7 -0
  73. package/dist/commands/requests/command.d.ts +39 -0
  74. package/dist/commands/requests/command.js +65 -0
  75. package/dist/commands/requests/get.d.ts +32 -0
  76. package/dist/commands/requests/get.js +15 -0
  77. package/dist/commands/requests/list.d.ts +27 -0
  78. package/dist/commands/requests/list.js +25 -0
  79. package/dist/commands/snapshots/command.d.ts +48 -0
  80. package/dist/commands/snapshots/command.js +80 -0
  81. package/dist/commands/snapshots/create.d.ts +7 -0
  82. package/dist/commands/snapshots/create.js +26 -0
  83. package/dist/commands/snapshots/delete.d.ts +34 -0
  84. package/dist/commands/snapshots/delete.js +8 -0
  85. package/dist/commands/snapshots/get.d.ts +35 -0
  86. package/dist/commands/snapshots/get.js +5 -0
  87. package/dist/commands/snapshots/list.d.ts +37 -0
  88. package/dist/commands/snapshots/list.js +10 -0
  89. package/dist/commands/unlink/command.d.ts +1 -0
  90. package/dist/commands/unlink/command.js +15 -0
  91. package/dist/commands/unlink/run.d.ts +7 -0
  92. package/dist/commands/unlink/run.js +8 -0
  93. package/dist/commands/whoami/command.d.ts +28 -0
  94. package/dist/commands/whoami/command.js +44 -0
  95. package/dist/commands/whoami/run.d.ts +5 -0
  96. package/dist/commands/whoami/run.js +39 -0
  97. package/dist/commands/workspaces/command.d.ts +33 -0
  98. package/dist/commands/workspaces/command.js +73 -0
  99. package/dist/commands/workspaces/list.d.ts +5 -0
  100. package/dist/commands/workspaces/list.js +42 -0
  101. package/dist/commands/workspaces/use.d.ts +5 -0
  102. package/dist/commands/workspaces/use.js +18 -0
  103. package/dist/config/project.d.ts +21 -0
  104. package/dist/config/project.js +105 -0
  105. package/dist/config/schema.d.ts +616 -0
  106. package/dist/config/schema.js +282 -0
  107. package/dist/context-types.d.ts +89 -0
  108. package/dist/context-types.js +56 -0
  109. package/dist/context.d.ts +13 -0
  110. package/dist/context.js +290 -0
  111. package/dist/credentials/sealing.d.ts +10 -0
  112. package/dist/credentials/sealing.js +72 -0
  113. package/dist/credentials/store.d.ts +17 -0
  114. package/dist/credentials/store.js +156 -0
  115. package/dist/doctor.d.ts +4 -0
  116. package/dist/doctor.js +239 -0
  117. package/dist/env-file.d.ts +10 -0
  118. package/dist/env-file.js +254 -0
  119. package/dist/errors.d.ts +784 -0
  120. package/dist/errors.js +459 -0
  121. package/dist/flags.d.ts +22 -0
  122. package/dist/flags.js +56 -0
  123. package/dist/generated/internal.d.ts +138 -0
  124. package/dist/generated/internal.js +2627 -0
  125. package/dist/generated/retry.d.ts +1 -0
  126. package/dist/generated/retry.js +11 -0
  127. package/dist/help.d.ts +408 -0
  128. package/dist/help.js +220 -0
  129. package/dist/io-types.d.ts +127 -0
  130. package/dist/io-types.js +1 -0
  131. package/dist/io.d.ts +13 -0
  132. package/dist/io.js +1017 -0
  133. package/dist/keys.d.ts +50 -0
  134. package/dist/keys.js +691 -0
  135. package/dist/link.d.ts +6 -0
  136. package/dist/link.js +58 -0
  137. package/dist/login.d.ts +92 -0
  138. package/dist/login.js +965 -0
  139. package/dist/main.d.ts +2 -0
  140. package/dist/main.js +126 -0
  141. package/dist/native-helper.d.ts +1 -0
  142. package/dist/native-helper.js +2 -0
  143. package/dist/output.d.ts +8 -0
  144. package/dist/output.js +135 -0
  145. package/dist/parser.d.ts +28 -0
  146. package/dist/parser.js +250 -0
  147. package/dist/program-types.d.ts +67 -0
  148. package/dist/program-types.js +14 -0
  149. package/dist/programs.d.ts +2 -0
  150. package/dist/programs.js +44 -0
  151. package/dist/project-roots.d.ts +3 -0
  152. package/dist/project-roots.js +26 -0
  153. package/dist/prompt.d.ts +3 -0
  154. package/dist/prompt.js +61 -0
  155. package/dist/registry.d.ts +2 -0
  156. package/dist/registry.js +44 -0
  157. package/dist/resources/create.d.ts +9 -0
  158. package/dist/resources/create.js +55 -0
  159. package/dist/resources/read.d.ts +15 -0
  160. package/dist/resources/read.js +69 -0
  161. package/dist/resources/schema.d.ts +177 -0
  162. package/dist/resources/schema.js +158 -0
  163. package/dist/text.d.ts +3 -0
  164. package/dist/text.js +19 -0
  165. package/dist/update.d.ts +22 -0
  166. package/dist/update.js +126 -0
  167. package/dist/version.d.ts +2 -0
  168. package/dist/version.js +2 -0
  169. package/package.json +52 -3
@@ -0,0 +1,282 @@
1
+ import { z } from 'zod';
2
+ import { apiOriginSchema, loginIdSchema, organizationIdSchema, profileNameSchema, tokenIdSchema, workspaceIdSchema, } from '../context-types.js';
3
+ import { CliError } from '../errors.js';
4
+ import { parseCreateKey } from '../generated/internal.js';
5
+ import { environmentTargetSchema } from '../program-types.js';
6
+ export const secretRefSchema = z.strictObject({
7
+ store: z.enum(['file', 'keychain']),
8
+ origin: apiOriginSchema,
9
+ entry: z.string().min(1),
10
+ });
11
+ export const ownerSchema = z.strictObject({
12
+ hostname: z.string(),
13
+ pid: z.number().int().positive(),
14
+ nonce: z.string().min(1),
15
+ });
16
+ export const sessionSchema = z.strictObject({
17
+ origin: apiOriginSchema,
18
+ organization: z.strictObject({ id: organizationIdSchema, name: z.string() }),
19
+ email: z.string(),
20
+ tokenId: tokenIdSchema,
21
+ token: secretRefSchema,
22
+ idleExpiresAt: z.iso.datetime({ offset: true }),
23
+ expiresAt: z.iso.datetime({ offset: true }),
24
+ runKeys: z.array(z.strictObject({
25
+ id: z.string().min(1),
26
+ workspaceId: workspaceIdSchema,
27
+ expiresAt: z.iso.datetime({ offset: true }),
28
+ secret: secretRefSchema,
29
+ })),
30
+ });
31
+ const keyIssuerSchema = z.strictObject({
32
+ profile: profileNameSchema,
33
+ generation: z.number().int().nonnegative(),
34
+ origin: apiOriginSchema,
35
+ tokenId: tokenIdSchema,
36
+ token: secretRefSchema,
37
+ });
38
+ const keyRequestSchema = z.unknown().transform((value, context) => {
39
+ try {
40
+ return parseCreateKey(value);
41
+ }
42
+ catch {
43
+ context.addIssue({ code: 'custom', message: 'Invalid key request.' });
44
+ return z.NEVER;
45
+ }
46
+ });
47
+ const keyOperationBase = z.strictObject({
48
+ id: z.string().min(1),
49
+ owner: ownerSchema.nullable(),
50
+ issuer: keyIssuerSchema,
51
+ workspaceId: workspaceIdSchema,
52
+ target: z.discriminatedUnion('kind', [
53
+ z.strictObject({ kind: z.literal('run') }),
54
+ z.strictObject({
55
+ kind: z.literal('environment'),
56
+ file: environmentTargetSchema,
57
+ }),
58
+ ]),
59
+ request: keyRequestSchema,
60
+ idempotencyKey: z.string().min(1),
61
+ secret: secretRefSchema,
62
+ });
63
+ const keyOperationSchema = z.discriminatedUnion('phase', [
64
+ keyOperationBase.extend({ phase: z.literal('creating') }),
65
+ keyOperationBase.extend({
66
+ phase: z.literal('staged'),
67
+ keyId: z.string().min(1),
68
+ expiresAt: z.iso.datetime({ offset: true }),
69
+ }),
70
+ ]);
71
+ const keyRevocationSchema = z.strictObject({
72
+ id: z.string().min(1),
73
+ keyId: z.string().min(1),
74
+ origin: apiOriginSchema,
75
+ tokenId: tokenIdSchema,
76
+ token: secretRefSchema,
77
+ secret: secretRefSchema.nullable(),
78
+ });
79
+ const loginPublicSchema = z.strictObject({
80
+ loginId: loginIdSchema,
81
+ userCode: z.string(),
82
+ verificationUrl: z.url(),
83
+ expiresAt: z.iso.datetime({ offset: true }),
84
+ interval: z.number().int().positive(),
85
+ });
86
+ export const pendingPayloadSchema = z.strictObject({
87
+ version: z.literal(1),
88
+ loginId: loginIdSchema,
89
+ origin: apiOriginSchema,
90
+ expiresAt: z.iso.datetime({ offset: true }),
91
+ interval: z.number().int().positive(),
92
+ deviceCode: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
93
+ privateKey: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
94
+ });
95
+ export const attemptSchema = z.strictObject({
96
+ id: z.string().min(1),
97
+ profile: profileNameSchema,
98
+ generation: z.number().int().nonnegative(),
99
+ origin: apiOriginSchema,
100
+ owner: ownerSchema.nullable(),
101
+ login: loginPublicSchema.nullable(),
102
+ secret: secretRefSchema.nullable(),
103
+ createdAt: z.iso.datetime({ offset: true }),
104
+ });
105
+ export const profileSchema = z.strictObject({
106
+ name: profileNameSchema,
107
+ generation: z.number().int().nonnegative(),
108
+ session: sessionSchema.nullable(),
109
+ defaultWorkspace: workspaceIdSchema.nullable(),
110
+ });
111
+ const revokeJobSchema = z.strictObject({
112
+ id: z.string().min(1),
113
+ origin: apiOriginSchema,
114
+ tokenId: tokenIdSchema,
115
+ token: secretRefSchema,
116
+ });
117
+ const installSchema = z.strictObject({
118
+ kind: z.literal('install'),
119
+ id: z.string().min(1),
120
+ attemptId: z.string().min(1),
121
+ profile: profileNameSchema,
122
+ generation: z.number().int().nonnegative(),
123
+ loginId: loginIdSchema,
124
+ candidate: sessionSchema,
125
+ });
126
+ export const configSchema = z
127
+ .strictObject({
128
+ version: z.literal(1),
129
+ defaultProfile: profileNameSchema.nullable(),
130
+ profiles: z.array(profileSchema),
131
+ attempts: z.array(attemptSchema),
132
+ intents: z.array(z.discriminatedUnion('kind', [
133
+ installSchema,
134
+ z.strictObject({
135
+ kind: z.literal('pending'),
136
+ id: z.string().min(1),
137
+ attemptId: z.string().min(1),
138
+ token: secretRefSchema,
139
+ }),
140
+ ])),
141
+ acknowledgements: z.array(z.strictObject({
142
+ id: z.string().min(1),
143
+ loginId: loginIdSchema,
144
+ origin: apiOriginSchema,
145
+ tokenId: tokenIdSchema,
146
+ token: secretRefSchema,
147
+ })),
148
+ revocations: z.array(revokeJobSchema),
149
+ keyOperations: z.array(keyOperationSchema),
150
+ keyRevocations: z.array(keyRevocationSchema),
151
+ environments: z.array(z.strictObject({
152
+ repositoryRoot: z.string().min(1),
153
+ path: z.string().min(1),
154
+ workspaceId: workspaceIdSchema,
155
+ keyId: z.string().min(1),
156
+ expiresAt: z.iso.datetime({ offset: true }),
157
+ issuer: keyIssuerSchema,
158
+ })),
159
+ cleanup: z.array(secretRefSchema),
160
+ })
161
+ .superRefine((value, context) => {
162
+ const sessions = [
163
+ ...value.profiles.flatMap((profile) => profile.session === null ? [] : [profile.session]),
164
+ ...value.intents.flatMap((intent) => intent.kind === 'install' ? [intent.candidate] : []),
165
+ ];
166
+ const bindings = [
167
+ ...sessions.flatMap((session) => [
168
+ { origin: session.origin, token: session.token },
169
+ ...session.runKeys.map((key) => ({
170
+ origin: session.origin,
171
+ token: key.secret,
172
+ })),
173
+ ]),
174
+ ...value.keyOperations.flatMap((operation) => [
175
+ operation.issuer,
176
+ { origin: operation.issuer.origin, token: operation.secret },
177
+ ]),
178
+ ...value.keyRevocations.flatMap((job) => [
179
+ job,
180
+ ...(job.secret === null
181
+ ? []
182
+ : [{ origin: job.origin, token: job.secret }]),
183
+ ]),
184
+ ...value.environments.map((environment) => environment.issuer),
185
+ ...value.acknowledgements,
186
+ ...value.revocations,
187
+ ...value.attempts.flatMap((attempt) => attempt.secret === null
188
+ ? []
189
+ : [{ origin: attempt.origin, token: attempt.secret }]),
190
+ ];
191
+ if (bindings.some((binding) => binding.origin !== binding.token.origin)) {
192
+ context.addIssue({
193
+ code: 'custom',
194
+ message: 'Credential origin mismatch.',
195
+ });
196
+ }
197
+ for (const intent of value.intents) {
198
+ if (intent.kind === 'pending') {
199
+ const attempt = value.attempts.find((candidate) => candidate.id === intent.attemptId);
200
+ if (attempt !== undefined && attempt.origin !== intent.token.origin) {
201
+ context.addIssue({
202
+ code: 'custom',
203
+ message: 'Pending credential origin mismatch.',
204
+ });
205
+ }
206
+ }
207
+ }
208
+ for (const operation of value.keyOperations) {
209
+ if (operation.request.workspaceId !== operation.workspaceId ||
210
+ operation.request.kind !==
211
+ (operation.target.kind === 'run' ? 'cli_run' : 'env_pull')) {
212
+ context.addIssue({
213
+ code: 'custom',
214
+ message: 'Key request binding mismatch.',
215
+ });
216
+ }
217
+ }
218
+ for (const keys of [
219
+ value.profiles.map((profile) => profile.name),
220
+ value.attempts.map((attempt) => attempt.id),
221
+ value.attempts.map((attempt) => attempt.profile),
222
+ value.intents.map((intent) => intent.id),
223
+ value.acknowledgements.map((job) => job.id),
224
+ value.revocations.map((job) => job.id),
225
+ value.keyOperations.map((operation) => operation.id),
226
+ value.keyRevocations.map((job) => job.id),
227
+ value.environments.map((environment) => JSON.stringify([
228
+ environment.repositoryRoot,
229
+ environment.issuer.origin,
230
+ environment.workspaceId,
231
+ ])),
232
+ ...sessions.map((session) => session.runKeys.map((key) => key.workspaceId)),
233
+ value.keyOperations
234
+ .filter((operation) => operation.target.kind === 'run')
235
+ .map((operation) => JSON.stringify([
236
+ operation.issuer.origin,
237
+ operation.issuer.tokenId,
238
+ operation.workspaceId,
239
+ ])),
240
+ ]) {
241
+ if (new Set(keys).size !== keys.length) {
242
+ context.addIssue({ code: 'custom', message: 'Duplicate record.' });
243
+ }
244
+ }
245
+ if (value.defaultProfile !== null &&
246
+ !value.profiles.some((profile) => profile.name === value.defaultProfile && profile.session !== null)) {
247
+ context.addIssue({ code: 'custom', message: 'Missing default profile.' });
248
+ }
249
+ });
250
+ export function emptyConfig() {
251
+ return {
252
+ version: 1,
253
+ defaultProfile: null,
254
+ profiles: [],
255
+ attempts: [],
256
+ intents: [],
257
+ acknowledgements: [],
258
+ revocations: [],
259
+ keyOperations: [],
260
+ keyRevocations: [],
261
+ environments: [],
262
+ cleanup: [],
263
+ };
264
+ }
265
+ export function parseVersioned(text, schema) {
266
+ let input;
267
+ try {
268
+ input = JSON.parse(text);
269
+ }
270
+ catch {
271
+ throw new CliError('cli.config_invalid');
272
+ }
273
+ const version = z.object({ version: z.number() }).safeParse(input);
274
+ if (version.success && version.data.version > 1) {
275
+ throw new CliError('cli.config_too_new');
276
+ }
277
+ const result = schema.safeParse(input);
278
+ if (!result.success) {
279
+ throw new CliError('cli.config_invalid');
280
+ }
281
+ return result.data;
282
+ }
@@ -0,0 +1,89 @@
1
+ import type { StateMachines } from '@usestatemachines/sdk';
2
+ import { z } from 'zod';
3
+ import type { AnonymousApi, AuthenticatedInternalApi } from './api-internal.js';
4
+ import type { ProjectRepository } from './config/project.js';
5
+ import type { EffectOptions, Io } from './io-types.js';
6
+ import type { Accounts } from './login.js';
7
+ import type { Programs } from './program-types.js';
8
+ export declare const apiOriginSchema: z.core.$ZodBranded<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>, "ApiOrigin", "out">;
9
+ export declare const productionOrigin: string & z.$brand<"ApiOrigin">;
10
+ export declare const profileNameSchema: z.core.$ZodBranded<z.ZodString, "ProfileName", "out">;
11
+ export declare const workspaceIdSchema: z.core.$ZodBranded<z.ZodString, "WorkspaceId", "out">;
12
+ export declare const organizationIdSchema: z.core.$ZodBranded<z.ZodString, "OrganizationId", "out">;
13
+ export declare const tokenIdSchema: z.core.$ZodBranded<z.ZodString, "TokenId", "out">;
14
+ export declare const loginIdSchema: z.core.$ZodBranded<z.ZodString, "LoginId", "out">;
15
+ export type ApiOrigin = z.infer<typeof apiOriginSchema>;
16
+ export type ProfileName = z.infer<typeof profileNameSchema>;
17
+ export type WorkspaceId = z.infer<typeof workspaceIdSchema>;
18
+ export type OrganizationId = z.infer<typeof organizationIdSchema>;
19
+ export type TokenId = z.infer<typeof tokenIdSchema>;
20
+ export type LoginId = z.infer<typeof loginIdSchema>;
21
+ export declare function profileNameFromEnvironment(value: string | undefined): ProfileName | null;
22
+ export declare function apiOriginFromEnvironment(value: string | undefined): ApiOrigin | null;
23
+ export interface Globals {
24
+ readonly profile: ProfileName | null;
25
+ readonly workspace: string | null;
26
+ readonly format: 'text' | 'json' | null;
27
+ readonly noInput: boolean;
28
+ readonly yes: boolean;
29
+ readonly stream: boolean;
30
+ }
31
+ export interface Presentation {
32
+ readonly mode: 'interactive' | 'non-interactive';
33
+ readonly format: 'text' | 'json';
34
+ readonly stream: boolean;
35
+ }
36
+ export type Account = {
37
+ readonly kind: 'workspace-key';
38
+ readonly origin: ApiOrigin;
39
+ readonly workspaceId: WorkspaceId;
40
+ readonly source: 'environment';
41
+ } | {
42
+ readonly kind: 'cli-token';
43
+ readonly origin: ApiOrigin;
44
+ readonly profile: ProfileName;
45
+ readonly tokenId: TokenId;
46
+ readonly organizationId: OrganizationId;
47
+ };
48
+ export interface Workspace {
49
+ readonly id: WorkspaceId;
50
+ readonly name: string;
51
+ readonly source: 'flag' | 'environment' | 'link' | 'default' | 'credential';
52
+ assertMember(actualWorkspaceId: string): void;
53
+ }
54
+ export interface PublicContext {
55
+ readonly io: Io;
56
+ readonly globals: Globals;
57
+ readonly presentation: Presentation;
58
+ readonly accounts: Pick<Accounts, 'login' | 'logout' | 'list' | 'use' | 'setWorkspace' | 'reconcile' | 'inspect'>;
59
+ readonly projects: ProjectRepository;
60
+ readonly version: string;
61
+ anonymousApi(origin: ApiOrigin): AnonymousApi;
62
+ resolveAccount(options?: EffectOptions): Promise<AccountContext>;
63
+ }
64
+ export interface AccountContext extends PublicContext {
65
+ readonly account: Account;
66
+ readonly sdk: StateMachines;
67
+ readonly internal: AuthenticatedInternalApi;
68
+ forWorkspace(workspace: Workspace): WorkspaceContext;
69
+ resolveWorkspace(options?: EffectOptions): Promise<Workspace | null>;
70
+ findWorkspace(value: string, options?: EffectOptions): Promise<Pick<Workspace, 'id' | 'name'>>;
71
+ }
72
+ export interface WorkspaceContext extends AccountContext {
73
+ readonly workspace: Workspace;
74
+ readonly programs: Programs;
75
+ }
76
+ export interface Contexts {
77
+ readonly public: PublicContext;
78
+ readonly account: AccountContext;
79
+ readonly workspace: WorkspaceContext;
80
+ }
81
+ export type ContextNeed = keyof Contexts;
82
+ export type ContextResolvers = {
83
+ readonly [Need in ContextNeed]: () => Promise<Contexts[Need]>;
84
+ };
85
+ export interface ContextSession {
86
+ readonly contexts: ContextResolvers;
87
+ readonly presentation: Presentation;
88
+ close(): Promise<void>;
89
+ }
@@ -0,0 +1,56 @@
1
+ import { z } from 'zod';
2
+ import { CliError } from './errors.js';
3
+ export const apiOriginSchema = z
4
+ .string()
5
+ .transform((value, context) => {
6
+ try {
7
+ const origin = value.endsWith('/') ? value.slice(0, -1) : value;
8
+ const url = new URL(origin);
9
+ const local = url.hostname === 'localhost' || url.hostname === '127.0.0.1';
10
+ if (/[{}]/.test(origin) ||
11
+ origin !== url.origin ||
12
+ (url.protocol !== 'https:' && !(url.protocol === 'http:' && local))) {
13
+ context.addIssue({
14
+ code: 'custom',
15
+ message: 'Use an HTTPS API origin without a path.',
16
+ });
17
+ return z.NEVER;
18
+ }
19
+ return origin;
20
+ }
21
+ catch {
22
+ context.addIssue({ code: 'custom', message: 'Use a valid API origin.' });
23
+ return z.NEVER;
24
+ }
25
+ })
26
+ .brand();
27
+ export const productionOrigin = apiOriginSchema.parse('https://api.usestatemachines.com');
28
+ export const profileNameSchema = z.string().min(1).brand();
29
+ export const workspaceIdSchema = z.string().min(1).brand();
30
+ export const organizationIdSchema = z.string().min(1).brand();
31
+ export const tokenIdSchema = z.string().min(1).brand();
32
+ export const loginIdSchema = z.string().min(1).brand();
33
+ export function profileNameFromEnvironment(value) {
34
+ if (value === undefined) {
35
+ return null;
36
+ }
37
+ const result = profileNameSchema.safeParse(value);
38
+ if (!result.success) {
39
+ throw new CliError('cli.invalid_arguments', {
40
+ message: 'STATEMACHINES_PROFILE must name a profile.',
41
+ });
42
+ }
43
+ return result.data;
44
+ }
45
+ export function apiOriginFromEnvironment(value) {
46
+ if (value === undefined) {
47
+ return null;
48
+ }
49
+ const result = apiOriginSchema.safeParse(value);
50
+ if (!result.success) {
51
+ throw new CliError('cli.invalid_arguments', {
52
+ message: 'STATEMACHINES_BASE_URL must be an HTTPS origin, or HTTP on localhost.',
53
+ });
54
+ }
55
+ return result.data;
56
+ }
@@ -0,0 +1,13 @@
1
+ import { type Workspace as ApiWorkspace, StateMachines } from '@usestatemachines/sdk';
2
+ import { type ContextSession, type Globals, type Presentation } from './context-types.js';
3
+ import type { Io } from './io-types.js';
4
+ export declare function allWorkspaces(sdk: StateMachines): Promise<ApiWorkspace[]>;
5
+ export declare function createContextSession(input: {
6
+ io: Io;
7
+ globals: Globals;
8
+ presentation: Presentation;
9
+ version: string;
10
+ agent: {
11
+ readonly name: string;
12
+ } | null;
13
+ }): Promise<ContextSession>;