windmill-client 1.618.2 → 1.618.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,927 @@
1
+ import { OpenAPI } from "./core/OpenAPI.mjs";
2
+ import { AppService, HelpersService, JobService, MetricsService, OidcService, ResourceService, UserService, VariableService } from "./services.gen.mjs";
3
+ import { datatable, ducklake } from "./sqlUtils.mjs";
4
+
5
+ //#region src/client.ts
6
+ const SHARED_FOLDER = "/shared";
7
+ let mockedApi = void 0;
8
+ /**
9
+ * Initialize the Windmill client with authentication token and base URL
10
+ * @param token - Authentication token (defaults to WM_TOKEN env variable)
11
+ * @param baseUrl - API base URL (defaults to BASE_INTERNAL_URL or BASE_URL env variable)
12
+ */
13
+ function setClient(token, baseUrl) {
14
+ if (baseUrl === void 0) baseUrl = getEnv("BASE_INTERNAL_URL") ?? getEnv("BASE_URL") ?? "http://localhost:8000";
15
+ if (token === void 0) token = getEnv("WM_TOKEN") ?? "no_token";
16
+ OpenAPI.WITH_CREDENTIALS = true;
17
+ OpenAPI.TOKEN = token;
18
+ OpenAPI.BASE = baseUrl + "/api";
19
+ }
20
+ function getPublicBaseUrl() {
21
+ return getEnv("WM_BASE_URL") ?? "http://localhost:3000";
22
+ }
23
+ const getEnv = (key) => {
24
+ if (typeof window === "undefined") return process?.env?.[key];
25
+ return window?.process?.env?.[key];
26
+ };
27
+ /**
28
+ * Create a client configuration from env variables
29
+ * @returns client configuration
30
+ */
31
+ function getWorkspace() {
32
+ return getEnv("WM_WORKSPACE") ?? "no_workspace";
33
+ }
34
+ /**
35
+ * Get a resource value by path
36
+ * @param path path of the resource, default to internal state path
37
+ * @param undefinedIfEmpty if the resource does not exist, return undefined instead of throwing an error
38
+ * @returns resource value
39
+ */
40
+ async function getResource(path, undefinedIfEmpty) {
41
+ path = parseResourceSyntax(path) ?? path ?? getStatePath();
42
+ const mockedApi$1 = await getMockedApi();
43
+ if (mockedApi$1) if (mockedApi$1.resources[path]) return mockedApi$1.resources[path];
44
+ else console.log(`MockedAPI present, but resource not found at ${path}, falling back to real API`);
45
+ const workspace = getWorkspace();
46
+ try {
47
+ return await ResourceService.getResourceValueInterpolated({
48
+ workspace,
49
+ path
50
+ });
51
+ } catch (e) {
52
+ if (undefinedIfEmpty && e.status === 404) return void 0;
53
+ else throw Error(`Resource not found at ${path} or not visible to you: ${e.body}`);
54
+ }
55
+ }
56
+ /**
57
+ * Get the true root job id
58
+ * @param jobId job id to get the root job id from (default to current job)
59
+ * @returns root job id
60
+ */
61
+ async function getRootJobId(jobId) {
62
+ const workspace = getWorkspace();
63
+ jobId = jobId ?? getEnv("WM_JOB_ID");
64
+ if (jobId === void 0) throw Error("Job ID not set");
65
+ return await JobService.getRootJobId({
66
+ workspace,
67
+ id: jobId
68
+ });
69
+ }
70
+ /**
71
+ * @deprecated Use runScriptByPath or runScriptByHash instead
72
+ */
73
+ async function runScript(path = null, hash_ = null, args = null, verbose = false) {
74
+ console.warn("runScript is deprecated. Use runScriptByPath or runScriptByHash instead.");
75
+ if (path && hash_) throw new Error("path and hash_ are mutually exclusive");
76
+ return _runScriptInternal(path, hash_, args, verbose);
77
+ }
78
+ async function _runScriptInternal(path = null, hash_ = null, args = null, verbose = false) {
79
+ args = args || {};
80
+ if (verbose) {
81
+ if (path) console.info(`running \`${path}\` synchronously with args:`, args);
82
+ else if (hash_) console.info(`running script with hash \`${hash_}\` synchronously with args:`, args);
83
+ }
84
+ const jobId = await _runScriptAsyncInternal(path, hash_, args);
85
+ return await waitJob(jobId, verbose);
86
+ }
87
+ /**
88
+ * Run a script synchronously by its path and wait for the result
89
+ * @param path - Script path in Windmill
90
+ * @param args - Arguments to pass to the script
91
+ * @param verbose - Enable verbose logging
92
+ * @returns Script execution result
93
+ */
94
+ async function runScriptByPath(path, args = null, verbose = false) {
95
+ return _runScriptInternal(path, null, args, verbose);
96
+ }
97
+ /**
98
+ * Run a script synchronously by its hash and wait for the result
99
+ * @param hash_ - Script hash in Windmill
100
+ * @param args - Arguments to pass to the script
101
+ * @param verbose - Enable verbose logging
102
+ * @returns Script execution result
103
+ */
104
+ async function runScriptByHash(hash_, args = null, verbose = false) {
105
+ return _runScriptInternal(null, hash_, args, verbose);
106
+ }
107
+ /**
108
+ * Append a text to the result stream
109
+ * @param text text to append to the result stream
110
+ */
111
+ function appendToResultStream(text) {
112
+ console.log("WM_STREAM: " + text.replace(/\n/g, "\\n"));
113
+ }
114
+ /**
115
+ * Stream to the result stream
116
+ * @param stream stream to stream to the result stream
117
+ */
118
+ async function streamResult(stream) {
119
+ for await (const text of stream) appendToResultStream(text);
120
+ }
121
+ /**
122
+ * Run a flow synchronously by its path and wait for the result
123
+ * @param path - Flow path in Windmill
124
+ * @param args - Arguments to pass to the flow
125
+ * @param verbose - Enable verbose logging
126
+ * @returns Flow execution result
127
+ */
128
+ async function runFlow(path = null, args = null, verbose = false) {
129
+ args = args || {};
130
+ if (verbose) console.info(`running \`${path}\` synchronously with args:`, args);
131
+ const jobId = await runFlowAsync(path, args, null, false);
132
+ return await waitJob(jobId, verbose);
133
+ }
134
+ /**
135
+ * Wait for a job to complete and return its result
136
+ * @param jobId - ID of the job to wait for
137
+ * @param verbose - Enable verbose logging
138
+ * @returns Job result when completed
139
+ */
140
+ async function waitJob(jobId, verbose = false) {
141
+ while (true) {
142
+ const resultRes = await getResultMaybe(jobId);
143
+ const started = resultRes.started;
144
+ const completed = resultRes.completed;
145
+ const success = resultRes.success;
146
+ if (!started && verbose) console.info(`job ${jobId} has not started yet`);
147
+ if (completed) {
148
+ const result = resultRes.result;
149
+ if (success) return result;
150
+ else {
151
+ const error = result.error;
152
+ throw new Error(`Job ${jobId} was not successful: ${JSON.stringify(error)}`);
153
+ }
154
+ }
155
+ if (verbose) console.info(`sleeping 0.5 seconds for jobId: ${jobId}`);
156
+ await new Promise((resolve) => setTimeout(resolve, 500));
157
+ }
158
+ }
159
+ /**
160
+ * Get the result of a completed job
161
+ * @param jobId - ID of the completed job
162
+ * @returns Job result
163
+ */
164
+ async function getResult(jobId) {
165
+ const workspace = getWorkspace();
166
+ return await JobService.getCompletedJobResult({
167
+ workspace,
168
+ id: jobId
169
+ });
170
+ }
171
+ /**
172
+ * Get the result of a job if completed, or its current status
173
+ * @param jobId - ID of the job
174
+ * @returns Object with started, completed, success, and result properties
175
+ */
176
+ async function getResultMaybe(jobId) {
177
+ const workspace = getWorkspace();
178
+ return await JobService.getCompletedJobResultMaybe({
179
+ workspace,
180
+ id: jobId
181
+ });
182
+ }
183
+ const STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/gm;
184
+ const ARGUMENT_NAMES = /([^\s,]+)/g;
185
+ function getParamNames(func) {
186
+ const fnStr = func.toString().replace(STRIP_COMMENTS, "");
187
+ let result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES);
188
+ if (result === null) result = [];
189
+ return result;
190
+ }
191
+ /**
192
+ * Wrap a function to execute as a Windmill task within a flow context
193
+ * @param f - Function to wrap as a task
194
+ * @returns Async wrapper function that executes as a Windmill job
195
+ */
196
+ function task(f) {
197
+ return async (...y) => {
198
+ const args = {};
199
+ const paramNames = getParamNames(f);
200
+ y.forEach((x, i) => args[paramNames[i]] = x);
201
+ let req = await fetch(`${OpenAPI.BASE}/w/${getWorkspace()}/jobs/run/workflow_as_code/${getEnv("WM_JOB_ID")}/${f.name}`, {
202
+ method: "POST",
203
+ headers: {
204
+ "Content-Type": "application/json",
205
+ Authorization: `Bearer ${getEnv("WM_TOKEN")}`
206
+ },
207
+ body: JSON.stringify({ args })
208
+ });
209
+ let jobId = await req.text();
210
+ console.log(`Started task ${f.name} as job ${jobId}`);
211
+ let r = await waitJob(jobId);
212
+ console.log(`Task ${f.name} (${jobId}) completed`);
213
+ return r;
214
+ };
215
+ }
216
+ /**
217
+ * @deprecated Use runScriptByPathAsync or runScriptByHashAsync instead
218
+ */
219
+ async function runScriptAsync(path, hash_, args, scheduledInSeconds = null) {
220
+ console.warn("runScriptAsync is deprecated. Use runScriptByPathAsync or runScriptByHashAsync instead.");
221
+ if (path && hash_) throw new Error("path and hash_ are mutually exclusive");
222
+ return _runScriptAsyncInternal(path, hash_, args, scheduledInSeconds);
223
+ }
224
+ async function _runScriptAsyncInternal(path = null, hash_ = null, args = null, scheduledInSeconds = null) {
225
+ args = args || {};
226
+ const params = {};
227
+ if (scheduledInSeconds) params["scheduled_in_secs"] = scheduledInSeconds;
228
+ let parentJobId = getEnv("WM_JOB_ID");
229
+ if (parentJobId !== void 0) params["parent_job"] = parentJobId;
230
+ let rootJobId = getEnv("WM_ROOT_FLOW_JOB_ID");
231
+ if (rootJobId != void 0 && rootJobId != "") params["root_job"] = rootJobId;
232
+ let endpoint;
233
+ if (path) endpoint = `/w/${getWorkspace()}/jobs/run/p/${path}`;
234
+ else if (hash_) endpoint = `/w/${getWorkspace()}/jobs/run/h/${hash_}`;
235
+ else throw new Error("path or hash_ must be provided");
236
+ let url = new URL(OpenAPI.BASE + endpoint);
237
+ url.search = new URLSearchParams(params).toString();
238
+ return fetch(url, {
239
+ method: "POST",
240
+ headers: {
241
+ "Content-Type": "application/json",
242
+ Authorization: `Bearer ${OpenAPI.TOKEN}`
243
+ },
244
+ body: JSON.stringify(args)
245
+ }).then((res) => res.text());
246
+ }
247
+ /**
248
+ * Run a script asynchronously by its path
249
+ * @param path - Script path in Windmill
250
+ * @param args - Arguments to pass to the script
251
+ * @param scheduledInSeconds - Schedule execution for a future time (in seconds)
252
+ * @returns Job ID of the created job
253
+ */
254
+ async function runScriptByPathAsync(path, args = null, scheduledInSeconds = null) {
255
+ return _runScriptAsyncInternal(path, null, args, scheduledInSeconds);
256
+ }
257
+ /**
258
+ * Run a script asynchronously by its hash
259
+ * @param hash_ - Script hash in Windmill
260
+ * @param args - Arguments to pass to the script
261
+ * @param scheduledInSeconds - Schedule execution for a future time (in seconds)
262
+ * @returns Job ID of the created job
263
+ */
264
+ async function runScriptByHashAsync(hash_, args = null, scheduledInSeconds = null) {
265
+ return _runScriptAsyncInternal(null, hash_, args, scheduledInSeconds);
266
+ }
267
+ /**
268
+ * Run a flow asynchronously by its path
269
+ * @param path - Flow path in Windmill
270
+ * @param args - Arguments to pass to the flow
271
+ * @param scheduledInSeconds - Schedule execution for a future time (in seconds)
272
+ * @param doNotTrackInParent - If false, tracks state in parent job (only use when fully awaiting the job)
273
+ * @returns Job ID of the created job
274
+ */
275
+ async function runFlowAsync(path, args, scheduledInSeconds = null, doNotTrackInParent = true) {
276
+ args = args || {};
277
+ const params = {};
278
+ if (scheduledInSeconds) params["scheduled_in_secs"] = scheduledInSeconds;
279
+ if (!doNotTrackInParent) {
280
+ let parentJobId = getEnv("WM_JOB_ID");
281
+ if (parentJobId !== void 0) params["parent_job"] = parentJobId;
282
+ let rootJobId = getEnv("WM_ROOT_FLOW_JOB_ID");
283
+ if (rootJobId != void 0 && rootJobId != "") params["root_job"] = rootJobId;
284
+ }
285
+ let endpoint;
286
+ if (path) endpoint = `/w/${getWorkspace()}/jobs/run/f/${path}`;
287
+ else throw new Error("path must be provided");
288
+ let url = new URL(OpenAPI.BASE + endpoint);
289
+ url.search = new URLSearchParams(params).toString();
290
+ return fetch(url, {
291
+ method: "POST",
292
+ headers: {
293
+ "Content-Type": "application/json",
294
+ Authorization: `Bearer ${OpenAPI.TOKEN}`
295
+ },
296
+ body: JSON.stringify(args)
297
+ }).then((res) => res.text());
298
+ }
299
+ /**
300
+ * Resolve a resource value in case the default value was picked because the input payload was undefined
301
+ * @param obj resource value or path of the resource under the format `$res:path`
302
+ * @returns resource value
303
+ */
304
+ async function resolveDefaultResource(obj) {
305
+ if (typeof obj === "string" && obj.startsWith("$res:")) return await getResource(obj.substring(5), true);
306
+ else return obj;
307
+ }
308
+ /**
309
+ * Get the state file path from environment variables
310
+ * @returns State path string
311
+ */
312
+ function getStatePath() {
313
+ const state_path = getEnv("WM_STATE_PATH_NEW") ?? getEnv("WM_STATE_PATH");
314
+ if (state_path === void 0) throw Error("State path not set");
315
+ return state_path;
316
+ }
317
+ /**
318
+ * Set a resource value by path
319
+ * @param path path of the resource to set, default to state path
320
+ * @param value new value of the resource to set
321
+ * @param initializeToTypeIfNotExist if the resource does not exist, initialize it with this type
322
+ */
323
+ async function setResource(value, path, initializeToTypeIfNotExist) {
324
+ path = parseResourceSyntax(path) ?? path ?? getStatePath();
325
+ const mockedApi$1 = await getMockedApi();
326
+ if (mockedApi$1) {
327
+ mockedApi$1.resources[path] = value;
328
+ return;
329
+ }
330
+ const workspace = getWorkspace();
331
+ if (await ResourceService.existsResource({
332
+ workspace,
333
+ path
334
+ })) await ResourceService.updateResourceValue({
335
+ workspace,
336
+ path,
337
+ requestBody: { value }
338
+ });
339
+ else if (initializeToTypeIfNotExist) await ResourceService.createResource({
340
+ workspace,
341
+ requestBody: {
342
+ path,
343
+ value,
344
+ resource_type: initializeToTypeIfNotExist
345
+ }
346
+ });
347
+ else throw Error(`Resource at path ${path} does not exist and no type was provided to initialize it`);
348
+ }
349
+ /**
350
+ * Set the state
351
+ * @param state state to set
352
+ * @deprecated use setState instead
353
+ */
354
+ async function setInternalState(state) {
355
+ await setResource(state, void 0, "state");
356
+ }
357
+ /**
358
+ * Set the state
359
+ * @param state state to set
360
+ * @param path Optional state resource path override. Defaults to `getStatePath()`.
361
+ */
362
+ async function setState(state, path) {
363
+ await setResource(state, path ?? getStatePath(), "state");
364
+ }
365
+ /**
366
+ * Set the progress
367
+ * Progress cannot go back and limited to 0% to 99% range
368
+ * @param percent Progress to set in %
369
+ * @param jobId? Job to set progress for
370
+ */
371
+ async function setProgress(percent, jobId) {
372
+ const workspace = getWorkspace();
373
+ let flowId = getEnv("WM_FLOW_JOB_ID");
374
+ if (jobId) {
375
+ const job = await JobService.getJob({
376
+ id: jobId ?? "NO_JOB_ID",
377
+ workspace,
378
+ noLogs: true
379
+ });
380
+ flowId = job.parent_job;
381
+ }
382
+ await MetricsService.setJobProgress({
383
+ id: jobId ?? getEnv("WM_JOB_ID") ?? "NO_JOB_ID",
384
+ workspace,
385
+ requestBody: {
386
+ percent: Math.floor(percent),
387
+ flow_job_id: flowId == "" ? void 0 : flowId
388
+ }
389
+ });
390
+ }
391
+ /**
392
+ * Get the progress
393
+ * @param jobId? Job to get progress from
394
+ * @returns Optional clamped between 0 and 100 progress value
395
+ */
396
+ async function getProgress(jobId) {
397
+ return await MetricsService.getJobProgress({
398
+ id: jobId ?? getEnv("WM_JOB_ID") ?? "NO_JOB_ID",
399
+ workspace: getWorkspace()
400
+ });
401
+ }
402
+ /**
403
+ * Set a flow user state
404
+ * @param key key of the state
405
+ * @param value value of the state
406
+
407
+ */
408
+ async function setFlowUserState(key, value, errorIfNotPossible) {
409
+ if (value === void 0) value = null;
410
+ const workspace = getWorkspace();
411
+ try {
412
+ await JobService.setFlowUserState({
413
+ workspace,
414
+ id: await getRootJobId(),
415
+ key,
416
+ requestBody: value
417
+ });
418
+ } catch (e) {
419
+ if (errorIfNotPossible) throw Error(`Error setting flow user state at ${key}: ${e.body}`);
420
+ else console.error(`Error setting flow user state at ${key}: ${e.body}`);
421
+ }
422
+ }
423
+ /**
424
+ * Get a flow user state
425
+ * @param path path of the variable
426
+
427
+ */
428
+ async function getFlowUserState(key, errorIfNotPossible) {
429
+ const workspace = getWorkspace();
430
+ try {
431
+ return await JobService.getFlowUserState({
432
+ workspace,
433
+ id: await getRootJobId(),
434
+ key
435
+ });
436
+ } catch (e) {
437
+ if (errorIfNotPossible) throw Error(`Error setting flow user state at ${key}: ${e.body}`);
438
+ else console.error(`Error setting flow user state at ${key}: ${e.body}`);
439
+ }
440
+ }
441
+ /**
442
+ * Get the internal state
443
+ * @deprecated use getState instead
444
+ */
445
+ async function getInternalState() {
446
+ return await getResource(getStatePath(), true);
447
+ }
448
+ /**
449
+ * Get the state shared across executions
450
+ * @param path Optional state resource path override. Defaults to `getStatePath()`.
451
+ */
452
+ async function getState(path) {
453
+ return await getResource(path ?? getStatePath(), true);
454
+ }
455
+ /**
456
+ * Get a variable by path
457
+ * @param path path of the variable
458
+ * @returns variable value
459
+ */
460
+ async function getVariable(path) {
461
+ path = parseVariableSyntax(path) ?? path;
462
+ const mockedApi$1 = await getMockedApi();
463
+ if (mockedApi$1) if (mockedApi$1.variables[path]) return mockedApi$1.variables[path];
464
+ else console.log(`MockedAPI present, but variable not found at ${path}, falling back to real API`);
465
+ const workspace = getWorkspace();
466
+ try {
467
+ return await VariableService.getVariableValue({
468
+ workspace,
469
+ path
470
+ });
471
+ } catch (e) {
472
+ throw Error(`Variable not found at ${path} or not visible to you: ${e.body}`);
473
+ }
474
+ }
475
+ /**
476
+ * Set a variable by path, create if not exist
477
+ * @param path path of the variable
478
+ * @param value value of the variable
479
+ * @param isSecretIfNotExist if the variable does not exist, create it as secret or not (default: false)
480
+ * @param descriptionIfNotExist if the variable does not exist, create it with this description (default: "")
481
+ */
482
+ async function setVariable(path, value, isSecretIfNotExist, descriptionIfNotExist) {
483
+ path = parseVariableSyntax(path) ?? path;
484
+ const mockedApi$1 = await getMockedApi();
485
+ if (mockedApi$1) {
486
+ mockedApi$1.variables[path] = value;
487
+ return;
488
+ }
489
+ const workspace = getWorkspace();
490
+ if (await VariableService.existsVariable({
491
+ workspace,
492
+ path
493
+ })) await VariableService.updateVariable({
494
+ workspace,
495
+ path,
496
+ requestBody: { value }
497
+ });
498
+ else await VariableService.createVariable({
499
+ workspace,
500
+ requestBody: {
501
+ path,
502
+ value,
503
+ is_secret: isSecretIfNotExist ?? false,
504
+ description: descriptionIfNotExist ?? ""
505
+ }
506
+ });
507
+ }
508
+ /**
509
+ * Build a PostgreSQL connection URL from a database resource
510
+ * @param path - Path to the database resource
511
+ * @returns PostgreSQL connection URL string
512
+ */
513
+ async function databaseUrlFromResource(path) {
514
+ const resource = await getResource(path);
515
+ return `postgresql://${resource.user}:${resource.password}@${resource.host}:${resource.port}/${resource.dbname}?sslmode=${resource.sslmode}`;
516
+ }
517
+ /**
518
+ * Get S3 client settings from a resource or workspace default
519
+ * @param s3_resource_path - Path to S3 resource (uses workspace default if undefined)
520
+ * @returns S3 client configuration settings
521
+ */
522
+ async function denoS3LightClientSettings(s3_resource_path) {
523
+ const workspace = getWorkspace();
524
+ const s3Resource = await HelpersService.s3ResourceInfo({
525
+ workspace,
526
+ requestBody: { s3_resource_path: parseResourceSyntax(s3_resource_path) ?? s3_resource_path }
527
+ });
528
+ let settings = { ...s3Resource };
529
+ return settings;
530
+ }
531
+ /**
532
+ * Load the content of a file stored in S3. If the s3ResourcePath is undefined, it will default to the workspace S3 resource.
533
+ *
534
+ * ```typescript
535
+ * let fileContent = await wmill.loadS3FileContent(inputFile)
536
+ * // if the file is a raw text file, it can be decoded and printed directly:
537
+ * const text = new TextDecoder().decode(fileContentStream)
538
+ * console.log(text);
539
+ * ```
540
+ */
541
+ async function loadS3File(s3object, s3ResourcePath = void 0) {
542
+ const fileContentBlob = await loadS3FileStream(s3object, s3ResourcePath);
543
+ if (fileContentBlob === void 0) return void 0;
544
+ const reader = fileContentBlob.stream().getReader();
545
+ const chunks = [];
546
+ while (true) {
547
+ const { value: chunk, done } = await reader.read();
548
+ if (done) break;
549
+ chunks.push(chunk);
550
+ }
551
+ let fileContentLength = 0;
552
+ chunks.forEach((item) => {
553
+ fileContentLength += item.length;
554
+ });
555
+ let fileContent = new Uint8Array(fileContentLength);
556
+ let offset = 0;
557
+ chunks.forEach((chunk) => {
558
+ fileContent.set(chunk, offset);
559
+ offset += chunk.length;
560
+ });
561
+ return fileContent;
562
+ }
563
+ /**
564
+ * Load the content of a file stored in S3 as a stream. If the s3ResourcePath is undefined, it will default to the workspace S3 resource.
565
+ *
566
+ * ```typescript
567
+ * let fileContentBlob = await wmill.loadS3FileStream(inputFile)
568
+ * // if the content is plain text, the blob can be read directly:
569
+ * console.log(await fileContentBlob.text());
570
+ * ```
571
+ */
572
+ async function loadS3FileStream(s3object, s3ResourcePath = void 0) {
573
+ let s3Obj = s3object && parseS3Object(s3object);
574
+ let params = {};
575
+ params["file_key"] = s3Obj.s3;
576
+ if (s3ResourcePath !== void 0) params["s3_resource_path"] = s3ResourcePath;
577
+ if (s3Obj.storage !== void 0) params["storage"] = s3Obj.storage;
578
+ const queryParams = new URLSearchParams(params);
579
+ const response = await fetch(`${OpenAPI.BASE}/w/${getWorkspace()}/job_helpers/download_s3_file?${queryParams}`, {
580
+ method: "GET",
581
+ headers: { Authorization: `Bearer ${OpenAPI.TOKEN}` }
582
+ });
583
+ if (!response.ok) {
584
+ const errorText = await response.text();
585
+ throw new Error(`Failed to load S3 file: ${response.status} ${response.statusText} - ${errorText}`);
586
+ }
587
+ return response.blob();
588
+ }
589
+ /**
590
+ * Persist a file to the S3 bucket. If the s3ResourcePath is undefined, it will default to the workspace S3 resource.
591
+ *
592
+ * ```typescript
593
+ * const s3object = await writeS3File(s3Object, "Hello Windmill!")
594
+ * const fileContentAsUtf8Str = (await s3object.toArray()).toString('utf-8')
595
+ * console.log(fileContentAsUtf8Str)
596
+ * ```
597
+ */
598
+ async function writeS3File(s3object, fileContent, s3ResourcePath = void 0, contentType = void 0, contentDisposition = void 0) {
599
+ let fileContentBlob;
600
+ if (typeof fileContent === "string") fileContentBlob = new Blob([fileContent], { type: "text/plain" });
601
+ else fileContentBlob = fileContent;
602
+ let s3Obj = s3object && parseS3Object(s3object);
603
+ const response = await HelpersService.fileUpload({
604
+ workspace: getWorkspace(),
605
+ fileKey: s3Obj?.s3,
606
+ fileExtension: void 0,
607
+ s3ResourcePath,
608
+ requestBody: fileContentBlob,
609
+ storage: s3Obj?.storage,
610
+ contentType,
611
+ contentDisposition
612
+ });
613
+ return {
614
+ s3: response.file_key,
615
+ ...s3Obj?.storage && { storage: s3Obj?.storage }
616
+ };
617
+ }
618
+ /**
619
+ * Sign S3 objects to be used by anonymous users in public apps
620
+ * @param s3objects s3 objects to sign
621
+ * @returns signed s3 objects
622
+ */
623
+ async function signS3Objects(s3objects) {
624
+ const signedKeys = await AppService.signS3Objects({
625
+ workspace: getWorkspace(),
626
+ requestBody: { s3_objects: s3objects.map(parseS3Object) }
627
+ });
628
+ return signedKeys;
629
+ }
630
+ /**
631
+ * Sign S3 object to be used by anonymous users in public apps
632
+ * @param s3object s3 object to sign
633
+ * @returns signed s3 object
634
+ */
635
+ async function signS3Object(s3object) {
636
+ const [signedObject] = await signS3Objects([s3object]);
637
+ return signedObject;
638
+ }
639
+ /**
640
+ * Generate a presigned public URL for an array of S3 objects.
641
+ * If an S3 object is not signed yet, it will be signed first.
642
+ * @param s3Objects s3 objects to sign
643
+ * @returns list of signed public URLs
644
+ */
645
+ async function getPresignedS3PublicUrls(s3Objects, { baseUrl } = {}) {
646
+ baseUrl ??= getPublicBaseUrl();
647
+ const s3Objs = s3Objects.map(parseS3Object);
648
+ const s3ObjsToSign = s3Objs.map((s3Obj, index) => [s3Obj, index]).filter(([s3Obj, _]) => s3Obj.presigned === void 0);
649
+ if (s3ObjsToSign.length > 0) {
650
+ const signedS3Objs = await signS3Objects(s3ObjsToSign.map(([s3Obj, _]) => s3Obj));
651
+ for (let i = 0; i < s3ObjsToSign.length; i++) {
652
+ const [_, originalIndex] = s3ObjsToSign[i];
653
+ s3Objs[originalIndex] = parseS3Object(signedS3Objs[i]);
654
+ }
655
+ }
656
+ const signedUrls = [];
657
+ for (const s3Obj of s3Objs) {
658
+ const { s3, presigned, storage = "_default_" } = s3Obj;
659
+ const signedUrl = `${baseUrl}/api/w/${getWorkspace()}/s3_proxy/${storage}/${s3}?${presigned}`;
660
+ signedUrls.push(signedUrl);
661
+ }
662
+ return signedUrls;
663
+ }
664
+ /**
665
+ * Generate a presigned public URL for an S3 object. If the S3 object is not signed yet, it will be signed first.
666
+ * @param s3Object s3 object to sign
667
+ * @returns signed public URL
668
+ */
669
+ async function getPresignedS3PublicUrl(s3Objects, { baseUrl } = {}) {
670
+ const [s3Object] = await getPresignedS3PublicUrls([s3Objects], { baseUrl });
671
+ return s3Object;
672
+ }
673
+ /**
674
+ * Get URLs needed for resuming a flow after this step
675
+ * @param approver approver name
676
+ * @param flowLevel if true, generate resume URLs for the parent flow instead of the specific step.
677
+ * This allows pre-approvals that can be consumed by any later suspend step in the same flow.
678
+ * @returns approval page UI URL, resume and cancel API URLs for resuming the flow
679
+ */
680
+ async function getResumeUrls(approver, flowLevel) {
681
+ const nonce = Math.floor(Math.random() * 4294967295);
682
+ const workspace = getWorkspace();
683
+ return await JobService.getResumeUrls({
684
+ workspace,
685
+ resumeId: nonce,
686
+ approver,
687
+ flowLevel,
688
+ id: getEnv("WM_JOB_ID") ?? "NO_JOB_ID"
689
+ });
690
+ }
691
+ /**
692
+ * @deprecated use getResumeUrls instead
693
+ */
694
+ function getResumeEndpoints(approver) {
695
+ return getResumeUrls(approver);
696
+ }
697
+ /**
698
+ * Get an OIDC jwt token for auth to external services (e.g: Vault, AWS) (ee only)
699
+ * @param audience audience of the token
700
+ * @param expiresIn Optional number of seconds until the token expires
701
+ * @returns jwt token
702
+ */
703
+ async function getIdToken(audience, expiresIn) {
704
+ const workspace = getWorkspace();
705
+ return await OidcService.getOidcToken({
706
+ workspace,
707
+ audience,
708
+ expiresIn
709
+ });
710
+ }
711
+ /**
712
+ * Convert a base64-encoded string to Uint8Array
713
+ * @param data - Base64-encoded string
714
+ * @returns Decoded Uint8Array
715
+ */
716
+ function base64ToUint8Array(data) {
717
+ return Uint8Array.from(atob(data), (c) => c.charCodeAt(0));
718
+ }
719
+ /**
720
+ * Convert a Uint8Array to base64-encoded string
721
+ * @param arrayBuffer - Uint8Array to encode
722
+ * @returns Base64-encoded string
723
+ */
724
+ function uint8ArrayToBase64(arrayBuffer) {
725
+ let base64 = "";
726
+ const encodings = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
727
+ const bytes = new Uint8Array(arrayBuffer);
728
+ const byteLength = bytes.byteLength;
729
+ const byteRemainder = byteLength % 3;
730
+ const mainLength = byteLength - byteRemainder;
731
+ let a, b, c, d;
732
+ let chunk;
733
+ for (let i = 0; i < mainLength; i = i + 3) {
734
+ chunk = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
735
+ a = (chunk & 16515072) >> 18;
736
+ b = (chunk & 258048) >> 12;
737
+ c = (chunk & 4032) >> 6;
738
+ d = chunk & 63;
739
+ base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d];
740
+ }
741
+ if (byteRemainder == 1) {
742
+ chunk = bytes[mainLength];
743
+ a = (chunk & 252) >> 2;
744
+ b = (chunk & 3) << 4;
745
+ base64 += encodings[a] + encodings[b] + "==";
746
+ } else if (byteRemainder == 2) {
747
+ chunk = bytes[mainLength] << 8 | bytes[mainLength + 1];
748
+ a = (chunk & 64512) >> 10;
749
+ b = (chunk & 1008) >> 4;
750
+ c = (chunk & 15) << 2;
751
+ base64 += encodings[a] + encodings[b] + encodings[c] + "=";
752
+ }
753
+ return base64;
754
+ }
755
+ /**
756
+ * Get email from workspace username
757
+ * This method is particularly useful for apps that require the email address of the viewer.
758
+ * Indeed, in the viewer context, WM_USERNAME is set to the username of the viewer but WM_EMAIL is set to the email of the creator of the app.
759
+ * @param username
760
+ * @returns email address
761
+ */
762
+ async function usernameToEmail(username) {
763
+ const workspace = getWorkspace();
764
+ return await UserService.usernameToEmail({
765
+ username,
766
+ workspace
767
+ });
768
+ }
769
+ /**
770
+ * Sends an interactive approval request via Slack, allowing optional customization of the message, approver, and form fields.
771
+ *
772
+ * **[Enterprise Edition Only]** To include form fields in the Slack approval request, go to **Advanced -> Suspend -> Form**
773
+ * and define a form. Learn more at [Windmill Documentation](https://www.windmill.dev/docs/flows/flow_approval#form).
774
+ *
775
+ * @param {Object} options - The configuration options for the Slack approval request.
776
+ * @param {string} options.slackResourcePath - The path to the Slack resource in Windmill.
777
+ * @param {string} options.channelId - The Slack channel ID where the approval request will be sent.
778
+ * @param {string} [options.message] - Optional custom message to include in the Slack approval request.
779
+ * @param {string} [options.approver] - Optional user ID or name of the approver for the request.
780
+ * @param {DefaultArgs} [options.defaultArgsJson] - Optional object defining or overriding the default arguments to a form field.
781
+ * @param {Enums} [options.dynamicEnumsJson] - Optional object overriding the enum default values of an enum form field.
782
+ *
783
+ * @returns {Promise<void>} Resolves when the Slack approval request is successfully sent.
784
+ *
785
+ * @throws {Error} If the function is not called within a flow or flow preview.
786
+ * @throws {Error} If the `JobService.getSlackApprovalPayload` call fails.
787
+ *
788
+ * **Usage Example:**
789
+ * ```typescript
790
+ * await requestInteractiveSlackApproval({
791
+ * slackResourcePath: "/u/alex/my_slack_resource",
792
+ * channelId: "admins-slack-channel",
793
+ * message: "Please approve this request",
794
+ * approver: "approver123",
795
+ * defaultArgsJson: { key1: "value1", key2: 42 },
796
+ * dynamicEnumsJson: { foo: ["choice1", "choice2"], bar: ["optionA", "optionB"] },
797
+ * });
798
+ * ```
799
+ *
800
+ * **Note:** This function requires execution within a Windmill flow or flow preview.
801
+ */
802
+ async function requestInteractiveSlackApproval({ slackResourcePath, channelId, message, approver, defaultArgsJson, dynamicEnumsJson }) {
803
+ const workspace = getWorkspace();
804
+ const flowJobId = getEnv("WM_FLOW_JOB_ID");
805
+ if (!flowJobId) throw new Error("You can't use this function in a standalone script or flow step preview. Please use it in a flow or a flow preview.");
806
+ const flowStepId = getEnv("WM_FLOW_STEP_ID");
807
+ if (!flowStepId) throw new Error("This function can only be called as a flow step");
808
+ const params = {
809
+ slackResourcePath,
810
+ channelId,
811
+ flowStepId
812
+ };
813
+ if (message) params.message = message;
814
+ if (approver) params.approver = approver;
815
+ if (defaultArgsJson) params.defaultArgsJson = JSON.stringify(defaultArgsJson);
816
+ if (dynamicEnumsJson) params.dynamicEnumsJson = JSON.stringify(dynamicEnumsJson);
817
+ await JobService.getSlackApprovalPayload({
818
+ workspace,
819
+ ...params,
820
+ id: getEnv("WM_JOB_ID") ?? "NO_JOB_ID"
821
+ });
822
+ }
823
+ /**
824
+ * Sends an interactive approval request via Teams, allowing optional customization of the message, approver, and form fields.
825
+ *
826
+ * **[Enterprise Edition Only]** To include form fields in the Teams approval request, go to **Advanced -> Suspend -> Form**
827
+ * and define a form. Learn more at [Windmill Documentation](https://www.windmill.dev/docs/flows/flow_approval#form).
828
+ *
829
+ * @param {Object} options - The configuration options for the Teams approval request.
830
+ * @param {string} options.teamName - The Teams team name where the approval request will be sent.
831
+ * @param {string} options.channelName - The Teams channel name where the approval request will be sent.
832
+ * @param {string} [options.message] - Optional custom message to include in the Teams approval request.
833
+ * @param {string} [options.approver] - Optional user ID or name of the approver for the request.
834
+ * @param {DefaultArgs} [options.defaultArgsJson] - Optional object defining or overriding the default arguments to a form field.
835
+ * @param {Enums} [options.dynamicEnumsJson] - Optional object overriding the enum default values of an enum form field.
836
+ *
837
+ * @returns {Promise<void>} Resolves when the Teams approval request is successfully sent.
838
+ *
839
+ * @throws {Error} If the function is not called within a flow or flow preview.
840
+ * @throws {Error} If the `JobService.getTeamsApprovalPayload` call fails.
841
+ *
842
+ * **Usage Example:**
843
+ * ```typescript
844
+ * await requestInteractiveTeamsApproval({
845
+ * teamName: "admins-teams",
846
+ * channelName: "admins-teams-channel",
847
+ * message: "Please approve this request",
848
+ * approver: "approver123",
849
+ * defaultArgsJson: { key1: "value1", key2: 42 },
850
+ * dynamicEnumsJson: { foo: ["choice1", "choice2"], bar: ["optionA", "optionB"] },
851
+ * });
852
+ * ```
853
+ *
854
+ * **Note:** This function requires execution within a Windmill flow or flow preview.
855
+ */
856
+ async function requestInteractiveTeamsApproval({ teamName, channelName, message, approver, defaultArgsJson, dynamicEnumsJson }) {
857
+ const workspace = getWorkspace();
858
+ const flowJobId = getEnv("WM_FLOW_JOB_ID");
859
+ if (!flowJobId) throw new Error("You can't use this function in a standalone script or flow step preview. Please use it in a flow or a flow preview.");
860
+ const flowStepId = getEnv("WM_FLOW_STEP_ID");
861
+ if (!flowStepId) throw new Error("This function can only be called as a flow step");
862
+ const params = {
863
+ teamName,
864
+ channelName,
865
+ flowStepId
866
+ };
867
+ if (message) params.message = message;
868
+ if (approver) params.approver = approver;
869
+ if (defaultArgsJson) params.defaultArgsJson = JSON.stringify(defaultArgsJson);
870
+ if (dynamicEnumsJson) params.dynamicEnumsJson = JSON.stringify(dynamicEnumsJson);
871
+ await JobService.getTeamsApprovalPayload({
872
+ workspace,
873
+ ...params,
874
+ id: getEnv("WM_JOB_ID") ?? "NO_JOB_ID"
875
+ });
876
+ }
877
+ async function getMockedApi() {
878
+ if (mockedApi) return mockedApi;
879
+ const mockedPath = getEnv("WM_MOCKED_API_FILE");
880
+ if (mockedPath) console.info("Using mocked API from", mockedPath);
881
+ else return void 0;
882
+ try {
883
+ const fs = await import("node:fs/promises");
884
+ const file = await fs.readFile(mockedPath, "utf-8");
885
+ try {
886
+ mockedApi = JSON.parse(file);
887
+ if (!mockedApi.variables) mockedApi.variables = {};
888
+ if (!mockedApi.resources) mockedApi.resources = {};
889
+ return mockedApi;
890
+ } catch {
891
+ console.warn("Error parsing mocked API file at path", mockedPath);
892
+ }
893
+ } catch {
894
+ console.warn("Error reading mocked API file at path", mockedPath);
895
+ }
896
+ if (!mockedApi) {
897
+ console.warn("No mocked API file path provided at env variable WM_MOCKED_API_FILE. Using empty mocked API.");
898
+ mockedApi = {
899
+ variables: {},
900
+ resources: {}
901
+ };
902
+ return mockedApi;
903
+ }
904
+ }
905
+ function parseResourceSyntax(s) {
906
+ if (s?.startsWith("$res:")) return s.substring(5);
907
+ if (s?.startsWith("res://")) return s.substring(6);
908
+ }
909
+ /**
910
+ * Parse an S3 object from URI string or record format
911
+ * @param s3Object - S3 object as URI string (s3://storage/key) or record
912
+ * @returns S3 object record with storage and s3 key
913
+ */
914
+ function parseS3Object(s3Object) {
915
+ if (typeof s3Object === "object") return s3Object;
916
+ const match = s3Object.match(/^s3:\/\/([^/]*)\/(.*)$/);
917
+ return {
918
+ storage: match?.[1] || void 0,
919
+ s3: match?.[2] ?? ""
920
+ };
921
+ }
922
+ function parseVariableSyntax(s) {
923
+ if (s.startsWith("var://")) return s.substring(6);
924
+ }
925
+
926
+ //#endregion
927
+ export { SHARED_FOLDER, appendToResultStream, base64ToUint8Array, databaseUrlFromResource, denoS3LightClientSettings, getFlowUserState, getIdToken, getInternalState, getPresignedS3PublicUrl, getPresignedS3PublicUrls, getProgress, getResource, getResult, getResultMaybe, getResumeEndpoints, getResumeUrls, getRootJobId, getState, getStatePath, getVariable, getWorkspace, loadS3File, loadS3FileStream, parseS3Object, requestInteractiveSlackApproval, requestInteractiveTeamsApproval, resolveDefaultResource, runFlow, runFlowAsync, runScript, runScriptAsync, runScriptByHash, runScriptByHashAsync, runScriptByPath, runScriptByPathAsync, setClient, setFlowUserState, setInternalState, setProgress, setResource, setState, setVariable, signS3Object, signS3Objects, streamResult, task, uint8ArrayToBase64, usernameToEmail, waitJob, writeS3File };