@intuned/runtime 1.0.5

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 (110) hide show
  1. package/.babelrc +21 -0
  2. package/.eslintignore +10 -0
  3. package/.eslintrc.js +39 -0
  4. package/CHANGELOG.md +7 -0
  5. package/InterfaceTemplate/index.playwright.ts +5 -0
  6. package/InterfaceTemplate/utils.ts +39 -0
  7. package/bin/intuned-api-run +2 -0
  8. package/bin/intuned-auth-session-check +2 -0
  9. package/bin/intuned-auth-session-create +2 -0
  10. package/bin/intuned-auth-session-load +2 -0
  11. package/bin/intuned-auth-session-refresh +2 -0
  12. package/bin/intuned-browser-save-state +2 -0
  13. package/bin/intuned-browser-start +2 -0
  14. package/bin/intuned-build +2 -0
  15. package/bin/intuned-ts-check +2 -0
  16. package/dist/commands/api/run.d.ts +6 -0
  17. package/dist/commands/api/run.js +115 -0
  18. package/dist/commands/auth-sessions/load.d.ts +2 -0
  19. package/dist/commands/auth-sessions/load.js +32 -0
  20. package/dist/commands/auth-sessions/run-check.d.ts +2 -0
  21. package/dist/commands/auth-sessions/run-check.js +62 -0
  22. package/dist/commands/auth-sessions/run-create.d.ts +2 -0
  23. package/dist/commands/auth-sessions/run-create.js +101 -0
  24. package/dist/commands/browser/save-state.d.ts +2 -0
  25. package/dist/commands/browser/save-state.js +17 -0
  26. package/dist/commands/browser/start-browser.d.ts +2 -0
  27. package/dist/commands/browser/start-browser.js +14 -0
  28. package/dist/commands/build.d.ts +1 -0
  29. package/dist/commands/build.js +84 -0
  30. package/dist/commands/common/browserUtils.d.ts +14 -0
  31. package/dist/commands/common/browserUtils.js +58 -0
  32. package/dist/commands/common/getFirstLineNumber.d.ts +9 -0
  33. package/dist/commands/common/getFirstLineNumber.js +103 -0
  34. package/dist/commands/common/getFirstLineNumber.test.js +228 -0
  35. package/dist/commands/common/sendMessageToClient.d.ts +1 -0
  36. package/dist/commands/common/sendMessageToClient.js +10 -0
  37. package/dist/commands/common/tsNodeImport.d.ts +1 -0
  38. package/dist/commands/common/tsNodeImport.js +20 -0
  39. package/dist/commands/common/utils/fileUtils.d.ts +6 -0
  40. package/dist/commands/common/utils/fileUtils.js +33 -0
  41. package/dist/commands/common/utils/settings.d.ts +2 -0
  42. package/dist/commands/common/utils/settings.js +28 -0
  43. package/dist/commands/common/utils/template.d.ts +2 -0
  44. package/dist/commands/common/utils/template.js +31 -0
  45. package/dist/commands/common/utils/unixSocket.d.ts +9 -0
  46. package/dist/commands/common/utils/unixSocket.js +44 -0
  47. package/dist/commands/interface/run.d.ts +1 -0
  48. package/dist/commands/interface/run.js +214 -0
  49. package/dist/commands/ts-check.d.ts +2 -0
  50. package/dist/commands/ts-check.js +56 -0
  51. package/dist/common/Logger/Logger/index.d.ts +12 -0
  52. package/dist/common/Logger/Logger/index.js +60 -0
  53. package/dist/common/Logger/Logger/types.d.ts +8 -0
  54. package/dist/common/Logger/Logger/types.js +5 -0
  55. package/dist/common/Logger/index.d.ts +12 -0
  56. package/dist/common/Logger/index.js +60 -0
  57. package/dist/common/Logger/types.d.ts +8 -0
  58. package/dist/common/Logger/types.js +5 -0
  59. package/dist/common/assets/browser_scripts.js +2214 -0
  60. package/dist/common/asyncLocalStorage/index.d.ts +16 -0
  61. package/dist/common/asyncLocalStorage/index.js +17 -0
  62. package/dist/common/cleanEnvironmentVariables.d.ts +1 -0
  63. package/dist/common/cleanEnvironmentVariables.js +16 -0
  64. package/dist/common/constants.d.ts +1 -0
  65. package/dist/common/constants.js +7 -0
  66. package/dist/common/contextStorageStateHelpers.d.ts +21 -0
  67. package/dist/common/contextStorageStateHelpers.js +81 -0
  68. package/dist/common/formatZodError.d.ts +2 -0
  69. package/dist/common/formatZodError.js +18 -0
  70. package/dist/common/getPlaywrightConstructs.d.ts +30 -0
  71. package/dist/common/getPlaywrightConstructs.js +196 -0
  72. package/dist/common/jwtTokenManager.d.ts +16 -0
  73. package/dist/common/jwtTokenManager.js +81 -0
  74. package/dist/common/runApi/errors.d.ts +65 -0
  75. package/dist/common/runApi/errors.js +156 -0
  76. package/dist/common/runApi/index.d.ts +12 -0
  77. package/dist/common/runApi/index.js +265 -0
  78. package/dist/common/runApi/types.d.ts +702 -0
  79. package/dist/common/runApi/types.js +74 -0
  80. package/dist/common/settingsSchema.d.ts +19 -0
  81. package/dist/common/settingsSchema.js +17 -0
  82. package/dist/common/telemetry.d.ts +3 -0
  83. package/dist/common/telemetry.js +32 -0
  84. package/dist/index.d.ts +4 -0
  85. package/dist/index.js +69 -0
  86. package/dist/runtime/RunError.d.ts +5 -0
  87. package/dist/runtime/RunError.js +19 -0
  88. package/dist/runtime/downloadDirectory.d.ts +1 -0
  89. package/dist/runtime/downloadDirectory.js +19 -0
  90. package/dist/runtime/enums.d.js +5 -0
  91. package/dist/runtime/enums.d.ts +11 -0
  92. package/dist/runtime/enums.js +18 -0
  93. package/dist/runtime/executionHelpers.test.js +53 -0
  94. package/dist/runtime/export.d.js +5 -0
  95. package/dist/runtime/export.d.ts +202 -0
  96. package/dist/runtime/extendPayload.d.ts +2 -0
  97. package/dist/runtime/extendPayload.js +21 -0
  98. package/dist/runtime/extendTimeout.d.ts +1 -0
  99. package/dist/runtime/extendTimeout.js +30 -0
  100. package/dist/runtime/index.d.ts +7 -0
  101. package/dist/runtime/index.js +53 -0
  102. package/dist/runtime/requestMoreInfo.d.ts +18 -0
  103. package/dist/runtime/requestMoreInfo.js +25 -0
  104. package/dist/runtime/runInfo.d.ts +2 -0
  105. package/dist/runtime/runInfo.js +21 -0
  106. package/package.json +136 -0
  107. package/template.tsconfig.json +14 -0
  108. package/tsconfig.eslint.json +5 -0
  109. package/tsconfig.json +24 -0
  110. package/typedoc.json +49 -0
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.runAutomationErrorCodes = exports.maxLevelsExceededErrorCode = exports.invalidCheckErrorCode = exports.invalidApiErrorCode = exports.internalInvalidInputErrorCode = exports.automationError = exports.authRequiredErrorCode = exports.authCheckNotFoundErrorCode = exports.authCheckFailedErrorCode = exports.apiNotFoundErrorCode = exports.abortedErrorCode = exports.RunAutomationError = exports.MaxLevelsExceededError = exports.InvalidCheckError = exports.InvalidApiError = exports.InternalInvalidInputError = exports.AutomationError = exports.AuthRequiredError = exports.AuthCheckNotFoundError = exports.AuthCheckFailedError = exports.ApiNotFoundError = exports.AbortedError = void 0;
7
+ var _runtime = require("../../runtime");
8
+ var playwright = _interopRequireWildcard(require("playwright-core"));
9
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
10
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
11
+ const apiNotFoundErrorCode = exports.apiNotFoundErrorCode = "APINotFoundError";
12
+ const invalidApiErrorCode = exports.invalidApiErrorCode = "InvalidAPIError";
13
+ const invalidCheckErrorCode = exports.invalidCheckErrorCode = "InvalidCheckError";
14
+ const abortedErrorCode = exports.abortedErrorCode = "AbortedError";
15
+ const authRequiredErrorCode = exports.authRequiredErrorCode = "AuthRequiredError";
16
+ const authCheckNotFoundErrorCode = exports.authCheckNotFoundErrorCode = "AuthCheckNotFoundError";
17
+ const authCheckFailedErrorCode = exports.authCheckFailedErrorCode = "AuthCheckFailedError";
18
+ const maxLevelsExceededErrorCode = exports.maxLevelsExceededErrorCode = "MaxLevelsExceededError";
19
+ const automationError = exports.automationError = "AutomationError";
20
+ const internalInvalidInputErrorCode = exports.internalInvalidInputErrorCode = "InternalInvalidInputError";
21
+ const runAutomationErrorCodes = exports.runAutomationErrorCodes = [apiNotFoundErrorCode, invalidApiErrorCode, invalidCheckErrorCode, abortedErrorCode, authRequiredErrorCode, authCheckNotFoundErrorCode, authCheckFailedErrorCode, maxLevelsExceededErrorCode, automationError, internalInvalidInputErrorCode];
22
+ class RunAutomationError {
23
+ wrapped = false;
24
+ get json() {
25
+ var _this$cause;
26
+ return {
27
+ code: this.code,
28
+ details: this.details,
29
+ cause: (_this$cause = this.cause) === null || _this$cause === void 0 ? void 0 : _this$cause.json
30
+ };
31
+ }
32
+ }
33
+ exports.RunAutomationError = RunAutomationError;
34
+ class ApiNotFoundError extends RunAutomationError {
35
+ constructor(apiName) {
36
+ super();
37
+ this.code = apiNotFoundErrorCode;
38
+ this.statusCode = 404;
39
+ this.message = `API ${apiName} not found`;
40
+ }
41
+ }
42
+ exports.ApiNotFoundError = ApiNotFoundError;
43
+ class InvalidApiError extends RunAutomationError {
44
+ constructor(message) {
45
+ super();
46
+ this.code = invalidApiErrorCode;
47
+ this.statusCode = 400;
48
+ this.message = `API is invalid: ${message}`;
49
+ }
50
+ }
51
+ exports.InvalidApiError = InvalidApiError;
52
+ class InvalidCheckError extends RunAutomationError {
53
+ constructor(message, cause) {
54
+ super();
55
+ this.code = invalidCheckErrorCode;
56
+ this.statusCode = 400;
57
+ this.message = message;
58
+ this.cause = cause;
59
+ }
60
+ }
61
+ exports.InvalidCheckError = InvalidCheckError;
62
+ class AbortedError extends RunAutomationError {
63
+ constructor() {
64
+ super();
65
+ this.code = abortedErrorCode;
66
+ this.statusCode = 200;
67
+ this.message = "Operation was aborted";
68
+ }
69
+ }
70
+ exports.AbortedError = AbortedError;
71
+ class AuthRequiredError extends RunAutomationError {
72
+ constructor() {
73
+ super();
74
+ this.code = authRequiredErrorCode;
75
+ this.statusCode = 401;
76
+ this.message = "Authentication is required";
77
+ this.wrapped = true;
78
+ }
79
+ }
80
+ exports.AuthRequiredError = AuthRequiredError;
81
+ class AuthCheckNotFoundError extends RunAutomationError {
82
+ constructor() {
83
+ super();
84
+ this.code = authCheckNotFoundErrorCode;
85
+ this.statusCode = 404;
86
+ this.message = "Auth check not found";
87
+ }
88
+ }
89
+ exports.AuthCheckNotFoundError = AuthCheckNotFoundError;
90
+ class AuthCheckFailedError extends RunAutomationError {
91
+ constructor() {
92
+ super();
93
+ this.code = authCheckFailedErrorCode;
94
+ this.statusCode = 401;
95
+ this.message = "auth session check failed";
96
+ this.wrapped = true;
97
+ }
98
+ }
99
+ exports.AuthCheckFailedError = AuthCheckFailedError;
100
+ class MaxLevelsExceededError extends RunAutomationError {
101
+ constructor(levels) {
102
+ super();
103
+ this.code = maxLevelsExceededErrorCode;
104
+ this.statusCode = 400;
105
+ this.message = `Max levels exceeded. Only ${levels} levels are supported`;
106
+ this.details = {
107
+ levels
108
+ };
109
+ }
110
+ }
111
+ exports.MaxLevelsExceededError = MaxLevelsExceededError;
112
+ class AutomationError extends RunAutomationError {
113
+ constructor(error) {
114
+ super();
115
+ this.error = error;
116
+ this.code = automationError;
117
+ if (error instanceof playwright.errors.TimeoutError) {
118
+ this.statusCode = 500;
119
+ this.message = `[${error.name}] ${error.message}`;
120
+ this.details = {
121
+ name: error.name,
122
+ message: error.message
123
+ };
124
+ return;
125
+ }
126
+ if (error instanceof _runtime.RunError) {
127
+ this.wrapped = true;
128
+ this.statusCode = error.options.status_code ?? 500;
129
+ this.message = `[${error.options.error_code ?? error.name}] ${error.message}`;
130
+ this.details = {
131
+ name: error.name,
132
+ statusCode: error.options.status_code,
133
+ errorCode: error.options.error_code,
134
+ message: error.message,
135
+ options: error.options
136
+ };
137
+ }
138
+ this.statusCode = 500;
139
+ this.message = `[${(error === null || error === void 0 ? void 0 : error.name) ?? error}] ${error === null || error === void 0 ? void 0 : error.message}`;
140
+ this.details = {
141
+ name: error === null || error === void 0 ? void 0 : error.name,
142
+ message: error === null || error === void 0 ? void 0 : error.message
143
+ };
144
+ }
145
+ }
146
+ exports.AutomationError = AutomationError;
147
+ class InternalInvalidInputError extends RunAutomationError {
148
+ constructor(message, details) {
149
+ super();
150
+ this.code = internalInvalidInputErrorCode;
151
+ this.statusCode = 500;
152
+ this.message = message;
153
+ this.details = details;
154
+ }
155
+ }
156
+ exports.InternalInvalidInputError = InternalInvalidInputError;
@@ -0,0 +1,12 @@
1
+ import { Result } from "neverthrow";
2
+ import { RunAutomationError } from "./errors";
3
+ import { Page, BrowserContext } from "playwright-core";
4
+ import { ExtendedRunApiParameters, RunApiResult, RunApiResultWithSessionOk } from "./types";
5
+ export * from "./types";
6
+ export * from "./errors";
7
+ export declare function runApiGenerator<ResultType = any, _YieldType = any, _NextType = any>(params: ExtendedRunApiParameters & {
8
+ retrieveSession: true;
9
+ }): AsyncGenerator<_YieldType, RunApiResult<ResultType, RunApiResultWithSessionOk>, _NextType>;
10
+ export declare function runApiGenerator<ResultType = any, _YieldType = any, _NextType = any>(params: ExtendedRunApiParameters): AsyncGenerator<_YieldType, RunApiResult<ResultType>, _NextType>;
11
+ export declare function runApi<ResultType = any>(params: ExtendedRunApiParameters): Promise<RunApiResult<ResultType>>;
12
+ export declare function checkAuthSessionWithRetries(page: Page, context: BrowserContext, checkFn: (..._: any) => Promise<boolean>, retries?: number): Promise<Result<boolean, RunAutomationError>>;
@@ -0,0 +1,265 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ runApiGenerator: true,
8
+ runApi: true,
9
+ checkAuthSessionWithRetries: true
10
+ };
11
+ exports.checkAuthSessionWithRetries = checkAuthSessionWithRetries;
12
+ exports.runApi = runApi;
13
+ exports.runApiGenerator = runApiGenerator;
14
+ var _downloadDirectory = require("../../runtime/downloadDirectory");
15
+ var _asyncLocalStorage = require("../asyncLocalStorage");
16
+ var _fsExtra = _interopRequireWildcard(require("fs-extra"));
17
+ var fs = _fsExtra;
18
+ var _contextStorageStateHelpers = require("../contextStorageStateHelpers");
19
+ var _neverthrow = require("neverthrow");
20
+ var _errors = require("./errors");
21
+ Object.keys(_errors).forEach(function (key) {
22
+ if (key === "default" || key === "__esModule") return;
23
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
24
+ if (key in exports && exports[key] === _errors[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _errors[key];
29
+ }
30
+ });
31
+ });
32
+ var _constants = require("../constants");
33
+ var _getPlaywrightConstructs = require("../getPlaywrightConstructs");
34
+ var _types = require("./types");
35
+ Object.keys(_types).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
38
+ if (key in exports && exports[key] === _types[key]) return;
39
+ Object.defineProperty(exports, key, {
40
+ enumerable: true,
41
+ get: function () {
42
+ return _types[key];
43
+ }
44
+ });
45
+ });
46
+ var _formatZodError = require("../formatZodError");
47
+ var _cleanEnvironmentVariables = require("../cleanEnvironmentVariables");
48
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
49
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
50
+ async function* runApiGenerator({
51
+ retrieveSession = false,
52
+ abortSignal,
53
+ importFunction,
54
+ ...input
55
+ }) {
56
+ let traceStarted = false;
57
+ const inputParseResult = _types.runApiParametersSchema.safeParse(input);
58
+ if (!inputParseResult.success) {
59
+ return (0, _neverthrow.err)(new _errors.InternalInvalidInputError("Input validation failed", (0, _formatZodError.formatZodError)(inputParseResult.error)));
60
+ }
61
+ const {
62
+ automationFunction,
63
+ runOptions,
64
+ tracing,
65
+ auth
66
+ } = inputParseResult.data;
67
+ const abortSymbol = Symbol("abort");
68
+ const abortPromise = new Promise(resolve => {
69
+ if (!abortSignal) return;
70
+ abortSignal.addEventListener("abort", () => {
71
+ resolve(abortSymbol);
72
+ });
73
+ });
74
+ let context;
75
+ let downloadsPath;
76
+ async function saveTraceIfNeeded({
77
+ errorMessage
78
+ }) {
79
+ if (!tracing.enabled || !traceStarted) {
80
+ return;
81
+ }
82
+ try {
83
+ var _context;
84
+ await ((_context = context) === null || _context === void 0 ? void 0 : _context.tracing.stop({
85
+ path: tracing.filePath
86
+ }));
87
+ } catch (error) {
88
+ console.log(errorMessage, error === null || error === void 0 ? void 0 : error.message);
89
+ await (0, _fsExtra.remove)(tracing.filePath);
90
+ }
91
+ }
92
+ async function* runAutomation() {
93
+ var _getExecutionContext;
94
+ let page;
95
+ const validatedModuleResult = await importUsingImportFunction(automationFunction.name, importFunction);
96
+ if (validatedModuleResult.isErr()) {
97
+ return (0, _neverthrow.err)(validatedModuleResult.error);
98
+ }
99
+ const importedModule = validatedModuleResult.value;
100
+ let checkFn;
101
+ if (auth !== null && auth !== void 0 && auth.runCheck) {
102
+ if (!auth.session) {
103
+ return (0, _neverthrow.err)(new _errors.AuthRequiredError());
104
+ }
105
+ const checkImportResult = await importUsingImportFunction(`${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`, importFunction);
106
+ if (checkImportResult.isErr()) {
107
+ return (0, _neverthrow.err)(checkImportResult.error);
108
+ }
109
+ if (checkImportResult.value.type !== "async") {
110
+ return (0, _neverthrow.err)(new _errors.InvalidCheckError("Check function is not an async function"));
111
+ }
112
+ checkFn = checkImportResult.value.func;
113
+ }
114
+ if (auth && auth.session.type === "state") {
115
+ const state = auth.session.state;
116
+ if (state === undefined || state === null) {
117
+ return (0, _neverthrow.err)(new _errors.AuthRequiredError());
118
+ }
119
+ }
120
+ if (runOptions.environment === "standalone") {
121
+ downloadsPath = (0, _downloadDirectory.getDownloadDirectoryPath)();
122
+ const {
123
+ headless,
124
+ proxy
125
+ } = runOptions;
126
+ ({
127
+ page,
128
+ context
129
+ } = await (0, _getPlaywrightConstructs.getProductionPlaywrightConstructs)({
130
+ headless,
131
+ proxy,
132
+ downloadsPath,
133
+ storageState: auth === null || auth === void 0 ? void 0 : auth.session
134
+ }));
135
+ } else {
136
+ const {
137
+ mode,
138
+ cdpAddress
139
+ } = runOptions;
140
+ ({
141
+ page,
142
+ context
143
+ } = await (0, _getPlaywrightConstructs.getPlaywrightConstructsForMode)(mode, cdpAddress, auth === null || auth === void 0 ? void 0 : auth.session));
144
+ }
145
+ if (tracing.enabled) {
146
+ await context.tracing.start({
147
+ screenshots: true,
148
+ snapshots: true,
149
+ sources: true
150
+ });
151
+ traceStarted = true;
152
+ }
153
+ (0, _cleanEnvironmentVariables.cleanEnvironmentVariables)();
154
+ if (checkFn !== undefined) {
155
+ console.log("Running auth check");
156
+ const authCheckResult = await checkAuthSessionWithRetries(page, context, checkFn, 2);
157
+ if (authCheckResult.isErr()) {
158
+ const error = authCheckResult.error;
159
+ if (["APINotFoundError", "InvalidAPIError"].includes(error.code)) {
160
+ return (0, _neverthrow.err)(new _errors.InvalidCheckError(`Auth session check function failed`, error));
161
+ }
162
+ return authCheckResult;
163
+ }
164
+ if (!authCheckResult.value) {
165
+ return (0, _neverthrow.err)(new _errors.AuthCheckFailedError());
166
+ }
167
+ }
168
+ console.log("Running automation");
169
+ const automationFunctionParameters = [...(automationFunction.params !== undefined ? [automationFunction.params] : []), page, context];
170
+ let result;
171
+ if (importedModule.type === "async-generator") {
172
+ const generator = importedModule.generator(...automationFunctionParameters);
173
+ let next = undefined;
174
+ while (true) {
175
+ const generatorResult = await generator.next(...(next ? [next] : []));
176
+ if (!generatorResult.done) {
177
+ next = yield generatorResult.value;
178
+ continue;
179
+ }
180
+ result = generatorResult.value;
181
+ break;
182
+ }
183
+ } else {
184
+ result = await importedModule.func(...automationFunctionParameters);
185
+ }
186
+ return (0, _neverthrow.ok)({
187
+ result,
188
+ extendedPayloads: (_getExecutionContext = (0, _asyncLocalStorage.getExecutionContext)()) === null || _getExecutionContext === void 0 ? void 0 : _getExecutionContext.extendedPayloads,
189
+ session: retrieveSession ? await (0, _contextStorageStateHelpers.getStorageState)(context) : undefined
190
+ });
191
+ }
192
+ try {
193
+ const generator = runAutomation();
194
+ let next = undefined;
195
+ while (true) {
196
+ const result = await Promise.race([generator.next(await next), abortPromise]);
197
+ if (result === abortSymbol) {
198
+ return (0, _neverthrow.err)(new _errors.AbortedError());
199
+ }
200
+ if (!result.done) {
201
+ next = yield result.value;
202
+ continue;
203
+ }
204
+ return result.value;
205
+ }
206
+ } catch (error) {
207
+ return (0, _neverthrow.err)(new _errors.AutomationError(error));
208
+ } finally {
209
+ var _context2;
210
+ await saveTraceIfNeeded({
211
+ errorMessage: "failed to save trace"
212
+ });
213
+ await ((_context2 = context) === null || _context2 === void 0 ? void 0 : _context2.close());
214
+ if (downloadsPath !== undefined) {
215
+ await fs.remove(downloadsPath);
216
+ }
217
+ }
218
+ }
219
+ async function runApi(params) {
220
+ const generator = runApiGenerator(params);
221
+ const {
222
+ value,
223
+ done
224
+ } = await generator.next();
225
+ if (done) {
226
+ return value;
227
+ }
228
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("Expected API to be async function, got async generator"));
229
+ }
230
+ async function checkAuthSessionWithRetries(page, context, checkFn, retries = 3) {
231
+ if (retries === 0) {
232
+ return (0, _neverthrow.ok)(false);
233
+ }
234
+ let tryNumber = 0;
235
+ console.log("Checking auth session with retries", `${_constants.AUTH_SESSIONS_FOLDER_NAME}/check`);
236
+ while (retries > tryNumber) {
237
+ const result = await checkFn(page, context);
238
+ if (result) return (0, _neverthrow.ok)(true);
239
+ tryNumber++;
240
+ }
241
+ return (0, _neverthrow.ok)(false);
242
+ }
243
+ async function importUsingImportFunction(path, importFunction) {
244
+ try {
245
+ const imported = importFunction ? await importFunction(path) : await (specifier => new Promise(r => r(specifier)).then(s => _interopRequireWildcard(require(s))))(`../../../${path}.ts`);
246
+ if (!imported || !imported.default || !imported.default.constructor) {
247
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default export"));
248
+ }
249
+ if (imported.default.constructor.name === "AsyncGeneratorFunction") {
250
+ return (0, _neverthrow.ok)({
251
+ type: "async-generator",
252
+ generator: imported.default
253
+ });
254
+ }
255
+ if (imported.default.constructor.name === "AsyncFunction") {
256
+ return (0, _neverthrow.ok)({
257
+ type: "async",
258
+ func: imported.default
259
+ });
260
+ }
261
+ return (0, _neverthrow.err)(new _errors.InvalidApiError("API file path does not have a default async function/generator export"));
262
+ } catch (error) {
263
+ return (0, _neverthrow.err)(new _errors.ApiNotFoundError(path));
264
+ }
265
+ }