@morphllm/morphsdk 0.2.93 → 0.2.94

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 (86) hide show
  1. package/dist/{chunk-IJ54DTJ3.js → chunk-4WO7PJNT.js} +11 -11
  2. package/dist/{chunk-EYGBUH2R.js → chunk-AV6YV2MH.js} +2 -2
  3. package/dist/{chunk-LMUZ3NGC.js → chunk-BVVDDTI7.js} +2 -2
  4. package/dist/chunk-ESRZJRTQ.js +359 -0
  5. package/dist/chunk-ESRZJRTQ.js.map +1 -0
  6. package/dist/{chunk-GU6DACME.js → chunk-FIA6LBW2.js} +2 -2
  7. package/dist/{chunk-4WLGDYWQ.js → chunk-FMWNVTJJ.js} +2 -2
  8. package/dist/{chunk-PBLPZ6AU.js → chunk-IH3KN4AT.js} +2 -2
  9. package/dist/{chunk-MIIJWDOQ.js → chunk-M7GFXRKL.js} +13 -3
  10. package/dist/chunk-M7GFXRKL.js.map +1 -0
  11. package/dist/{chunk-4KMBU6T3.js → chunk-R7WN43L2.js} +4 -4
  12. package/dist/{chunk-PUGIOVSP.js → chunk-TXYCM4NP.js} +2 -2
  13. package/dist/chunk-UJ7LVT5G.js +177 -0
  14. package/dist/chunk-UJ7LVT5G.js.map +1 -0
  15. package/dist/{chunk-FNLNDMIX.js → chunk-WSQMWVSD.js} +2 -2
  16. package/dist/chunk-YJ354BA2.js +46 -0
  17. package/dist/chunk-YJ354BA2.js.map +1 -0
  18. package/dist/client.cjs +546 -4
  19. package/dist/client.cjs.map +1 -1
  20. package/dist/client.d.ts +2 -0
  21. package/dist/client.js +12 -9
  22. package/dist/index.cjs +546 -4
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.ts +2 -0
  25. package/dist/index.js +12 -9
  26. package/dist/tools/browser/anthropic.cjs +5 -1
  27. package/dist/tools/browser/anthropic.cjs.map +1 -1
  28. package/dist/tools/browser/anthropic.js +5 -2
  29. package/dist/tools/browser/core.cjs +544 -2
  30. package/dist/tools/browser/core.cjs.map +1 -1
  31. package/dist/tools/browser/core.d.ts +6 -0
  32. package/dist/tools/browser/core.js +4 -1
  33. package/dist/tools/browser/errors.cjs +208 -0
  34. package/dist/tools/browser/errors.cjs.map +1 -0
  35. package/dist/tools/browser/errors.d.ts +158 -0
  36. package/dist/tools/browser/errors.js +22 -0
  37. package/dist/tools/browser/errors.js.map +1 -0
  38. package/dist/tools/browser/index.cjs +562 -2
  39. package/dist/tools/browser/index.cjs.map +1 -1
  40. package/dist/tools/browser/index.d.ts +3 -0
  41. package/dist/tools/browser/index.js +27 -4
  42. package/dist/tools/browser/index.js.map +1 -1
  43. package/dist/tools/browser/openai.cjs +5 -1
  44. package/dist/tools/browser/openai.cjs.map +1 -1
  45. package/dist/tools/browser/openai.js +5 -2
  46. package/dist/tools/browser/profiles/core.cjs +639 -0
  47. package/dist/tools/browser/profiles/core.cjs.map +1 -0
  48. package/dist/tools/browser/profiles/core.d.ts +179 -0
  49. package/dist/tools/browser/profiles/core.js +27 -0
  50. package/dist/tools/browser/profiles/core.js.map +1 -0
  51. package/dist/tools/browser/profiles/index.cjs +639 -0
  52. package/dist/tools/browser/profiles/index.cjs.map +1 -0
  53. package/dist/tools/browser/profiles/index.d.ts +4 -0
  54. package/dist/tools/browser/profiles/index.js +27 -0
  55. package/dist/tools/browser/profiles/index.js.map +1 -0
  56. package/dist/tools/browser/profiles/types.cjs +74 -0
  57. package/dist/tools/browser/profiles/types.cjs.map +1 -0
  58. package/dist/tools/browser/profiles/types.d.ts +176 -0
  59. package/dist/tools/browser/profiles/types.js +16 -0
  60. package/dist/tools/browser/profiles/types.js.map +1 -0
  61. package/dist/tools/browser/types.cjs.map +1 -1
  62. package/dist/tools/browser/types.d.ts +2 -0
  63. package/dist/tools/browser/vercel.cjs +5 -1
  64. package/dist/tools/browser/vercel.cjs.map +1 -1
  65. package/dist/tools/browser/vercel.js +5 -2
  66. package/dist/tools/fastapply/index.js +3 -3
  67. package/dist/tools/index.js +3 -3
  68. package/dist/tools/warp_grep/anthropic.js +4 -4
  69. package/dist/tools/warp_grep/client.js +3 -3
  70. package/dist/tools/warp_grep/gemini.js +3 -3
  71. package/dist/tools/warp_grep/harness.js +2 -2
  72. package/dist/tools/warp_grep/index.js +3 -3
  73. package/dist/tools/warp_grep/openai.js +4 -4
  74. package/dist/tools/warp_grep/providers/local.js +2 -2
  75. package/dist/tools/warp_grep/vercel.js +4 -4
  76. package/package.json +7 -2
  77. package/dist/chunk-MIIJWDOQ.js.map +0 -1
  78. /package/dist/{chunk-IJ54DTJ3.js.map → chunk-4WO7PJNT.js.map} +0 -0
  79. /package/dist/{chunk-EYGBUH2R.js.map → chunk-AV6YV2MH.js.map} +0 -0
  80. /package/dist/{chunk-LMUZ3NGC.js.map → chunk-BVVDDTI7.js.map} +0 -0
  81. /package/dist/{chunk-GU6DACME.js.map → chunk-FIA6LBW2.js.map} +0 -0
  82. /package/dist/{chunk-4WLGDYWQ.js.map → chunk-FMWNVTJJ.js.map} +0 -0
  83. /package/dist/{chunk-PBLPZ6AU.js.map → chunk-IH3KN4AT.js.map} +0 -0
  84. /package/dist/{chunk-4KMBU6T3.js.map → chunk-R7WN43L2.js.map} +0 -0
  85. /package/dist/{chunk-PUGIOVSP.js.map → chunk-TXYCM4NP.js.map} +0 -0
  86. /package/dist/{chunk-FNLNDMIX.js.map → chunk-WSQMWVSD.js.map} +0 -0
@@ -0,0 +1,639 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // tools/browser/profiles/core.ts
21
+ var core_exports = {};
22
+ __export(core_exports, {
23
+ ProfilesClient: () => ProfilesClient,
24
+ createProfile: () => createProfile,
25
+ deleteProfile: () => deleteProfile,
26
+ getProfile: () => getProfile,
27
+ getProfileState: () => getProfileState,
28
+ listProfiles: () => listProfiles,
29
+ saveProfileSession: () => saveProfileSession,
30
+ startProfileSession: () => startProfileSession,
31
+ updateProfile: () => updateProfile
32
+ });
33
+ module.exports = __toCommonJS(core_exports);
34
+
35
+ // tools/utils/resilience.ts
36
+ var DEFAULT_RETRY_CONFIG = {
37
+ maxRetries: 3,
38
+ initialDelay: 1e3,
39
+ maxDelay: 3e4,
40
+ backoffMultiplier: 2,
41
+ retryableErrors: ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND"]
42
+ };
43
+ async function fetchWithRetry(url, options, retryConfig = {}) {
44
+ const {
45
+ maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,
46
+ initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,
47
+ maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,
48
+ backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
49
+ retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,
50
+ onRetry
51
+ } = retryConfig;
52
+ let lastError = null;
53
+ let delay = initialDelay;
54
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
55
+ try {
56
+ const response = await fetch(url, options);
57
+ if (response.status === 429 || response.status === 503) {
58
+ if (attempt < maxRetries) {
59
+ const retryAfter = response.headers.get("Retry-After");
60
+ const waitTime = retryAfter ? parseInt(retryAfter) * 1e3 : Math.min(delay, maxDelay);
61
+ const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);
62
+ if (onRetry) {
63
+ onRetry(attempt + 1, error);
64
+ }
65
+ await sleep(waitTime);
66
+ delay *= backoffMultiplier;
67
+ continue;
68
+ }
69
+ }
70
+ return response;
71
+ } catch (error) {
72
+ lastError = error;
73
+ const isRetryable = retryableErrors.some(
74
+ (errType) => lastError?.message?.includes(errType)
75
+ );
76
+ if (!isRetryable || attempt === maxRetries) {
77
+ throw lastError;
78
+ }
79
+ const waitTime = Math.min(delay, maxDelay);
80
+ if (onRetry) {
81
+ onRetry(attempt + 1, lastError);
82
+ }
83
+ await sleep(waitTime);
84
+ delay *= backoffMultiplier;
85
+ }
86
+ }
87
+ throw lastError || new Error("Max retries exceeded");
88
+ }
89
+ function sleep(ms) {
90
+ return new Promise((resolve) => setTimeout(resolve, ms));
91
+ }
92
+
93
+ // tools/browser/errors.ts
94
+ var MorphError = class extends Error {
95
+ /** Error code for programmatic handling */
96
+ code;
97
+ /** Original cause of the error, if any */
98
+ cause;
99
+ constructor(message, code, cause) {
100
+ super(message);
101
+ this.name = "MorphError";
102
+ this.code = code;
103
+ this.cause = cause;
104
+ if (Error.captureStackTrace) {
105
+ Error.captureStackTrace(this, this.constructor);
106
+ }
107
+ }
108
+ /**
109
+ * Returns a JSON representation of the error for logging.
110
+ */
111
+ toJSON() {
112
+ return {
113
+ name: this.name,
114
+ message: this.message,
115
+ code: this.code,
116
+ cause: this.cause?.message
117
+ };
118
+ }
119
+ };
120
+ var MorphValidationError = class extends MorphError {
121
+ /** The field that failed validation */
122
+ field;
123
+ constructor(message, field) {
124
+ super(message, "validation_error");
125
+ this.name = "MorphValidationError";
126
+ this.field = field;
127
+ }
128
+ toJSON() {
129
+ return {
130
+ ...super.toJSON(),
131
+ field: this.field
132
+ };
133
+ }
134
+ };
135
+ var MorphAPIError = class extends MorphError {
136
+ /** HTTP status code */
137
+ statusCode;
138
+ /** Request ID for debugging (if available) */
139
+ requestId;
140
+ /** Raw response body */
141
+ rawResponse;
142
+ constructor(message, code, statusCode, options) {
143
+ super(message, code, options?.cause);
144
+ this.name = "MorphAPIError";
145
+ this.statusCode = statusCode;
146
+ this.requestId = options?.requestId;
147
+ this.rawResponse = options?.rawResponse;
148
+ }
149
+ toJSON() {
150
+ return {
151
+ ...super.toJSON(),
152
+ statusCode: this.statusCode,
153
+ requestId: this.requestId
154
+ };
155
+ }
156
+ };
157
+ var MorphAuthenticationError = class extends MorphAPIError {
158
+ constructor(message = "Authentication required. Please provide a valid API key.") {
159
+ super(message, "authentication_required", 401);
160
+ this.name = "MorphAuthenticationError";
161
+ }
162
+ };
163
+ var MorphRateLimitError = class extends MorphAPIError {
164
+ /** When the rate limit resets (Unix timestamp) */
165
+ resetAt;
166
+ /** Number of seconds until reset */
167
+ retryAfter;
168
+ constructor(message = "Rate limit exceeded. Please retry later.", options) {
169
+ super(message, "rate_limit_exceeded", 429, { requestId: options?.requestId });
170
+ this.name = "MorphRateLimitError";
171
+ this.resetAt = options?.resetAt;
172
+ this.retryAfter = options?.retryAfter;
173
+ }
174
+ toJSON() {
175
+ return {
176
+ ...super.toJSON(),
177
+ resetAt: this.resetAt,
178
+ retryAfter: this.retryAfter
179
+ };
180
+ }
181
+ };
182
+ var MorphNotFoundError = class extends MorphAPIError {
183
+ /** The type of resource that was not found */
184
+ resourceType;
185
+ /** The ID of the resource that was not found */
186
+ resourceId;
187
+ constructor(resourceType, resourceId) {
188
+ const message = resourceId ? `${resourceType} '${resourceId}' not found` : `${resourceType} not found`;
189
+ super(message, "resource_not_found", 404);
190
+ this.name = "MorphNotFoundError";
191
+ this.resourceType = resourceType;
192
+ this.resourceId = resourceId;
193
+ }
194
+ toJSON() {
195
+ return {
196
+ ...super.toJSON(),
197
+ resourceType: this.resourceType,
198
+ resourceId: this.resourceId
199
+ };
200
+ }
201
+ };
202
+ var MorphProfileLimitError = class extends MorphAPIError {
203
+ /** Current number of profiles */
204
+ currentCount;
205
+ /** Maximum allowed profiles for the plan */
206
+ maxAllowed;
207
+ constructor(message = "Profile limit exceeded for your plan.", options) {
208
+ super(message, "profile_limit_exceeded", 403, { requestId: options?.requestId });
209
+ this.name = "MorphProfileLimitError";
210
+ this.currentCount = options?.currentCount;
211
+ this.maxAllowed = options?.maxAllowed;
212
+ }
213
+ toJSON() {
214
+ return {
215
+ ...super.toJSON(),
216
+ currentCount: this.currentCount,
217
+ maxAllowed: this.maxAllowed
218
+ };
219
+ }
220
+ };
221
+ function parseAPIError(statusCode, responseText, requestId) {
222
+ let errorData = {};
223
+ try {
224
+ errorData = JSON.parse(responseText);
225
+ } catch {
226
+ }
227
+ const message = errorData.detail || errorData.message || responseText || "Unknown error";
228
+ const code = errorData.code;
229
+ switch (statusCode) {
230
+ case 401:
231
+ return new MorphAuthenticationError(message);
232
+ case 403:
233
+ if (code === "profile_limit_exceeded" || message.toLowerCase().includes("limit")) {
234
+ return new MorphProfileLimitError(message, { requestId });
235
+ }
236
+ return new MorphAPIError(message, "insufficient_permissions", statusCode, { requestId, rawResponse: responseText });
237
+ case 404:
238
+ if (message.toLowerCase().includes("profile")) {
239
+ return new MorphNotFoundError("Profile", void 0);
240
+ }
241
+ if (message.toLowerCase().includes("session")) {
242
+ return new MorphNotFoundError("Session", void 0);
243
+ }
244
+ return new MorphAPIError(message, "resource_not_found", statusCode, { requestId, rawResponse: responseText });
245
+ case 429:
246
+ return new MorphRateLimitError(message, { requestId });
247
+ case 422:
248
+ return new MorphAPIError(message, "validation_error", statusCode, { requestId, rawResponse: responseText });
249
+ case 500:
250
+ case 502:
251
+ case 503:
252
+ case 504:
253
+ return new MorphAPIError(message, "service_unavailable", statusCode, { requestId, rawResponse: responseText });
254
+ default:
255
+ return new MorphAPIError(message, "network_error", statusCode, { requestId, rawResponse: responseText });
256
+ }
257
+ }
258
+
259
+ // tools/browser/profiles/types.ts
260
+ function transformProfile(api) {
261
+ return {
262
+ id: api.id,
263
+ name: api.name,
264
+ repoId: api.repo_id,
265
+ cookieDomains: api.cookie_domains,
266
+ lastUsedAt: api.last_used_at,
267
+ createdAt: api.created_at,
268
+ updatedAt: api.updated_at
269
+ };
270
+ }
271
+ function transformCreateInput(input) {
272
+ return {
273
+ name: input.name,
274
+ repo_id: input.repoId
275
+ };
276
+ }
277
+ function transformSession(api) {
278
+ return {
279
+ sessionId: api.session_id,
280
+ debugUrl: api.debug_url || api.debugUrl || ""
281
+ };
282
+ }
283
+ function transformSaveInput(input) {
284
+ return {
285
+ session_id: input.sessionId,
286
+ profile_id: input.profileId
287
+ };
288
+ }
289
+ function transformStateResponse(api) {
290
+ return {
291
+ profileId: api.profile_id,
292
+ stateUrl: api.state_url,
293
+ expiresIn: api.expires_in
294
+ };
295
+ }
296
+
297
+ // tools/browser/profiles/core.ts
298
+ var DEFAULT_API_URL = process.env.MORPH_ENVIRONMENT === "DEV" ? "http://localhost:8000" : "https://browser.morphllm.com";
299
+ var ProfilesClient = class {
300
+ config;
301
+ constructor(config) {
302
+ this.config = config;
303
+ }
304
+ /**
305
+ * Create a new browser profile.
306
+ *
307
+ * @param input - Profile creation parameters
308
+ * @returns The created profile
309
+ * @throws {MorphValidationError} If input validation fails
310
+ * @throws {MorphProfileLimitError} If profile limit is exceeded
311
+ * @throws {MorphAuthenticationError} If API key is missing or invalid
312
+ *
313
+ * @example
314
+ * ```typescript
315
+ * const profile = await morph.browser.profiles.createProfile({
316
+ * name: 'LinkedIn Production',
317
+ * repoId: 'repo-uuid-here'
318
+ * });
319
+ * ```
320
+ */
321
+ async createProfile(input) {
322
+ return createProfile(input, this.config);
323
+ }
324
+ /**
325
+ * List all profiles for the authenticated user.
326
+ *
327
+ * @param repoId - Optional repository ID to filter by
328
+ * @returns Array of profiles
329
+ *
330
+ * @example
331
+ * ```typescript
332
+ * // List all profiles
333
+ * const allProfiles = await morph.browser.profiles.listProfiles();
334
+ *
335
+ * // List profiles for a specific repo
336
+ * const repoProfiles = await morph.browser.profiles.listProfiles('repo-uuid');
337
+ * ```
338
+ */
339
+ async listProfiles(repoId) {
340
+ return listProfiles(this.config, repoId);
341
+ }
342
+ /**
343
+ * Get a profile by ID with convenience methods.
344
+ *
345
+ * @param id - Profile ID
346
+ * @returns Profile with attached methods
347
+ * @throws {MorphNotFoundError} If profile is not found
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * const profile = await morph.browser.profiles.getProfile('profile-id');
352
+ * const state = await profile.getState();
353
+ * await profile.delete();
354
+ * ```
355
+ */
356
+ async getProfile(id) {
357
+ return getProfile(id, this.config);
358
+ }
359
+ /**
360
+ * Update a profile's name.
361
+ *
362
+ * @param id - Profile ID
363
+ * @param input - Update parameters
364
+ * @returns Updated profile
365
+ */
366
+ async updateProfile(id, input) {
367
+ return updateProfile(id, input, this.config);
368
+ }
369
+ /**
370
+ * Delete a profile.
371
+ *
372
+ * @param id - Profile ID
373
+ * @throws {MorphNotFoundError} If profile is not found
374
+ */
375
+ async deleteProfile(id) {
376
+ return deleteProfile(id, this.config);
377
+ }
378
+ /**
379
+ * Start a browser session for profile setup.
380
+ *
381
+ * Returns a live URL where the user can sign into accounts.
382
+ * After signing in, call `saveSession` to persist the state.
383
+ *
384
+ * @param input - Optional session parameters
385
+ * @returns Session with debug URL
386
+ *
387
+ * @example
388
+ * ```typescript
389
+ * const session = await morph.browser.profiles.startSession();
390
+ * console.log('Sign in at:', session.debugUrl);
391
+ * // Open debugUrl in browser, user signs in...
392
+ * await morph.browser.profiles.saveSession(session.sessionId, profile.id);
393
+ * ```
394
+ */
395
+ async startSession(input) {
396
+ return startProfileSession(this.config, input);
397
+ }
398
+ /**
399
+ * Save browser state from a session to a profile.
400
+ *
401
+ * Call this after the user is done signing into accounts.
402
+ * Extracts cookies, localStorage, and sessionStorage.
403
+ *
404
+ * @param sessionId - Browser session ID from startSession
405
+ * @param profileId - Profile ID to save state to
406
+ * @returns Updated profile with cookie domains
407
+ */
408
+ async saveSession(sessionId, profileId) {
409
+ return saveProfileSession({ sessionId, profileId }, this.config);
410
+ }
411
+ /**
412
+ * Get the presigned URL for a profile's state.
413
+ *
414
+ * Use this to download the raw state JSON for debugging
415
+ * or to restore state manually.
416
+ *
417
+ * @param profileId - Profile ID
418
+ * @returns State URL with expiry information
419
+ */
420
+ async getProfileState(profileId) {
421
+ return getProfileState(profileId, this.config);
422
+ }
423
+ };
424
+ function validateCreateInput(input) {
425
+ if (!input.name || typeof input.name !== "string") {
426
+ throw new MorphValidationError("name is required", "name");
427
+ }
428
+ const trimmedName = input.name.trim();
429
+ if (trimmedName.length === 0) {
430
+ throw new MorphValidationError("name cannot be empty", "name");
431
+ }
432
+ if (trimmedName.length > 100) {
433
+ throw new MorphValidationError("name must be 100 characters or less", "name");
434
+ }
435
+ if (!input.repoId || typeof input.repoId !== "string") {
436
+ throw new MorphValidationError("repoId is required", "repoId");
437
+ }
438
+ if (input.repoId.trim().length === 0) {
439
+ throw new MorphValidationError("repoId cannot be empty", "repoId");
440
+ }
441
+ }
442
+ function validateId(id, fieldName) {
443
+ if (!id || typeof id !== "string") {
444
+ throw new MorphValidationError(`${fieldName} is required`, fieldName);
445
+ }
446
+ if (id.trim().length === 0) {
447
+ throw new MorphValidationError(`${fieldName} cannot be empty`, fieldName);
448
+ }
449
+ }
450
+ async function createProfile(input, config = {}) {
451
+ validateCreateInput(input);
452
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
453
+ const headers = buildHeaders(config);
454
+ const response = await fetchWithRetry(
455
+ `${apiUrl}/profiles`,
456
+ {
457
+ method: "POST",
458
+ headers,
459
+ body: JSON.stringify(transformCreateInput(input))
460
+ },
461
+ config.retryConfig
462
+ );
463
+ if (!response.ok) {
464
+ const errorText = await response.text().catch(() => response.statusText);
465
+ const requestId = response.headers.get("x-request-id") || void 0;
466
+ throw parseAPIError(response.status, errorText, requestId);
467
+ }
468
+ const apiProfile = await response.json();
469
+ return transformProfile(apiProfile);
470
+ }
471
+ async function listProfiles(config = {}, repoId) {
472
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
473
+ const headers = buildHeaders(config);
474
+ const url = repoId ? `${apiUrl}/profiles?repo_id=${encodeURIComponent(repoId)}` : `${apiUrl}/profiles`;
475
+ const response = await fetchWithRetry(
476
+ url,
477
+ { method: "GET", headers },
478
+ config.retryConfig
479
+ );
480
+ if (!response.ok) {
481
+ const errorText = await response.text().catch(() => response.statusText);
482
+ const requestId = response.headers.get("x-request-id") || void 0;
483
+ throw parseAPIError(response.status, errorText, requestId);
484
+ }
485
+ const data = await response.json();
486
+ return data.profiles.map(transformProfile);
487
+ }
488
+ async function getProfile(id, config = {}) {
489
+ validateId(id, "id");
490
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
491
+ const headers = buildHeaders(config);
492
+ const response = await fetchWithRetry(
493
+ `${apiUrl}/profiles/${encodeURIComponent(id)}`,
494
+ { method: "GET", headers },
495
+ config.retryConfig
496
+ );
497
+ if (!response.ok) {
498
+ const errorText = await response.text().catch(() => response.statusText);
499
+ const requestId = response.headers.get("x-request-id") || void 0;
500
+ throw parseAPIError(response.status, errorText, requestId);
501
+ }
502
+ const apiProfile = await response.json();
503
+ const profile = transformProfile(apiProfile);
504
+ return {
505
+ ...profile,
506
+ getState: () => getProfileState(id, config),
507
+ delete: () => deleteProfile(id, config)
508
+ };
509
+ }
510
+ async function updateProfile(id, input, config = {}) {
511
+ validateId(id, "id");
512
+ if (input.name !== void 0) {
513
+ if (typeof input.name !== "string") {
514
+ throw new MorphValidationError("name must be a string", "name");
515
+ }
516
+ if (input.name.trim().length === 0) {
517
+ throw new MorphValidationError("name cannot be empty", "name");
518
+ }
519
+ if (input.name.length > 100) {
520
+ throw new MorphValidationError("name must be 100 characters or less", "name");
521
+ }
522
+ }
523
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
524
+ const headers = buildHeaders(config);
525
+ const response = await fetchWithRetry(
526
+ `${apiUrl}/profiles/${encodeURIComponent(id)}`,
527
+ {
528
+ method: "PATCH",
529
+ headers,
530
+ body: JSON.stringify(input)
531
+ },
532
+ config.retryConfig
533
+ );
534
+ if (!response.ok) {
535
+ const errorText = await response.text().catch(() => response.statusText);
536
+ const requestId = response.headers.get("x-request-id") || void 0;
537
+ throw parseAPIError(response.status, errorText, requestId);
538
+ }
539
+ const apiProfile = await response.json();
540
+ return transformProfile(apiProfile);
541
+ }
542
+ async function deleteProfile(id, config = {}) {
543
+ validateId(id, "id");
544
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
545
+ const headers = buildHeaders(config);
546
+ const response = await fetchWithRetry(
547
+ `${apiUrl}/profiles/${encodeURIComponent(id)}`,
548
+ { method: "DELETE", headers },
549
+ config.retryConfig
550
+ );
551
+ if (!response.ok) {
552
+ const errorText = await response.text().catch(() => response.statusText);
553
+ const requestId = response.headers.get("x-request-id") || void 0;
554
+ throw parseAPIError(response.status, errorText, requestId);
555
+ }
556
+ }
557
+ async function startProfileSession(config = {}, input) {
558
+ if (!config.apiKey) {
559
+ throw new MorphAuthenticationError();
560
+ }
561
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
562
+ const headers = buildHeaders(config);
563
+ const body = input?.profileId ? { profile_id: input.profileId } : {};
564
+ const response = await fetchWithRetry(
565
+ `${apiUrl}/profiles/session/start`,
566
+ {
567
+ method: "POST",
568
+ headers,
569
+ body: JSON.stringify(body)
570
+ },
571
+ config.retryConfig
572
+ );
573
+ if (!response.ok) {
574
+ const errorText = await response.text().catch(() => response.statusText);
575
+ const requestId = response.headers.get("x-request-id") || void 0;
576
+ throw parseAPIError(response.status, errorText, requestId);
577
+ }
578
+ const apiSession = await response.json();
579
+ return transformSession(apiSession);
580
+ }
581
+ async function saveProfileSession(input, config = {}) {
582
+ validateId(input.sessionId, "sessionId");
583
+ validateId(input.profileId, "profileId");
584
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
585
+ const headers = buildHeaders(config);
586
+ const response = await fetchWithRetry(
587
+ `${apiUrl}/profiles/session/save`,
588
+ {
589
+ method: "POST",
590
+ headers,
591
+ body: JSON.stringify(transformSaveInput(input))
592
+ },
593
+ config.retryConfig
594
+ );
595
+ if (!response.ok) {
596
+ const errorText = await response.text().catch(() => response.statusText);
597
+ const requestId = response.headers.get("x-request-id") || void 0;
598
+ throw parseAPIError(response.status, errorText, requestId);
599
+ }
600
+ const apiProfile = await response.json();
601
+ return transformProfile(apiProfile);
602
+ }
603
+ async function getProfileState(profileId, config = {}) {
604
+ validateId(profileId, "profileId");
605
+ const apiUrl = config.apiUrl || DEFAULT_API_URL;
606
+ const headers = buildHeaders(config);
607
+ const response = await fetchWithRetry(
608
+ `${apiUrl}/profiles/${encodeURIComponent(profileId)}/state`,
609
+ { method: "GET", headers },
610
+ config.retryConfig
611
+ );
612
+ if (!response.ok) {
613
+ const errorText = await response.text().catch(() => response.statusText);
614
+ const requestId = response.headers.get("x-request-id") || void 0;
615
+ throw parseAPIError(response.status, errorText, requestId);
616
+ }
617
+ const apiState = await response.json();
618
+ return transformStateResponse(apiState);
619
+ }
620
+ function buildHeaders(config) {
621
+ const headers = { "Content-Type": "application/json" };
622
+ if (config.apiKey) {
623
+ headers["Authorization"] = `Bearer ${config.apiKey}`;
624
+ }
625
+ return headers;
626
+ }
627
+ // Annotate the CommonJS export names for ESM import in node:
628
+ 0 && (module.exports = {
629
+ ProfilesClient,
630
+ createProfile,
631
+ deleteProfile,
632
+ getProfile,
633
+ getProfileState,
634
+ listProfiles,
635
+ saveProfileSession,
636
+ startProfileSession,
637
+ updateProfile
638
+ });
639
+ //# sourceMappingURL=core.cjs.map