@morphllm/morphsdk 0.2.6
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.
- package/README.md +39 -0
- package/dist/chunk-4UVEBIDK.js +358 -0
- package/dist/chunk-4UVEBIDK.js.map +1 -0
- package/dist/chunk-4V46N27D.js +169 -0
- package/dist/chunk-4V46N27D.js.map +1 -0
- package/dist/chunk-4VWJFZVS.js +89 -0
- package/dist/chunk-4VWJFZVS.js.map +1 -0
- package/dist/chunk-5COKN3XD.js +91 -0
- package/dist/chunk-5COKN3XD.js.map +1 -0
- package/dist/chunk-5VQEQSJQ.js +394 -0
- package/dist/chunk-5VQEQSJQ.js.map +1 -0
- package/dist/chunk-63WE2C5R.js +43 -0
- package/dist/chunk-63WE2C5R.js.map +1 -0
- package/dist/chunk-74ZHKB54.js +9 -0
- package/dist/chunk-74ZHKB54.js.map +1 -0
- package/dist/chunk-7PZJQFCY.js +39 -0
- package/dist/chunk-7PZJQFCY.js.map +1 -0
- package/dist/chunk-BILUTNBC.js +83 -0
- package/dist/chunk-BILUTNBC.js.map +1 -0
- package/dist/chunk-G4DJ6VSM.js +78 -0
- package/dist/chunk-G4DJ6VSM.js.map +1 -0
- package/dist/chunk-HGIFACNP.js +59 -0
- package/dist/chunk-HGIFACNP.js.map +1 -0
- package/dist/chunk-OI5YYE36.js +189 -0
- package/dist/chunk-OI5YYE36.js.map +1 -0
- package/dist/chunk-PZ5AY32C.js +10 -0
- package/dist/chunk-PZ5AY32C.js.map +1 -0
- package/dist/chunk-VJK4PH5V.js +105 -0
- package/dist/chunk-VJK4PH5V.js.map +1 -0
- package/dist/chunk-WXBUVKYL.js +128 -0
- package/dist/chunk-WXBUVKYL.js.map +1 -0
- package/dist/chunk-X2K57BH6.js +1 -0
- package/dist/chunk-X2K57BH6.js.map +1 -0
- package/dist/chunk-YQMPVJ2L.js +32 -0
- package/dist/chunk-YQMPVJ2L.js.map +1 -0
- package/dist/chunk-YWS2GRQC.js +97 -0
- package/dist/chunk-YWS2GRQC.js.map +1 -0
- package/dist/chunk-ZQEWQ7LJ.js +97 -0
- package/dist/chunk-ZQEWQ7LJ.js.map +1 -0
- package/dist/client.cjs +1358 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.js +15 -0
- package/dist/client.js.map +1 -0
- package/dist/git/client.cjs +428 -0
- package/dist/git/client.cjs.map +1 -0
- package/dist/git/client.js +8 -0
- package/dist/git/client.js.map +1 -0
- package/dist/git/config.cjs +41 -0
- package/dist/git/config.cjs.map +1 -0
- package/dist/git/config.js +17 -0
- package/dist/git/config.js.map +1 -0
- package/dist/git/index.cjs +438 -0
- package/dist/git/index.cjs.map +1 -0
- package/dist/git/index.js +14 -0
- package/dist/git/index.js.map +1 -0
- package/dist/git/types.cjs +19 -0
- package/dist/git/types.cjs.map +1 -0
- package/dist/git/types.js +1 -0
- package/dist/git/types.js.map +1 -0
- package/dist/index.cjs +1372 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/browser/anthropic.cjs +281 -0
- package/dist/tools/browser/anthropic.cjs.map +1 -0
- package/dist/tools/browser/anthropic.js +72 -0
- package/dist/tools/browser/anthropic.js.map +1 -0
- package/dist/tools/browser/core.cjs +459 -0
- package/dist/tools/browser/core.cjs.map +1 -0
- package/dist/tools/browser/core.js +21 -0
- package/dist/tools/browser/core.js.map +1 -0
- package/dist/tools/browser/index.cjs +497 -0
- package/dist/tools/browser/index.cjs.map +1 -0
- package/dist/tools/browser/index.js +27 -0
- package/dist/tools/browser/index.js.map +1 -0
- package/dist/tools/browser/openai.cjs +297 -0
- package/dist/tools/browser/openai.cjs.map +1 -0
- package/dist/tools/browser/openai.js +85 -0
- package/dist/tools/browser/openai.js.map +1 -0
- package/dist/tools/browser/prompts.cjs +64 -0
- package/dist/tools/browser/prompts.cjs.map +1 -0
- package/dist/tools/browser/prompts.js +10 -0
- package/dist/tools/browser/prompts.js.map +1 -0
- package/dist/tools/browser/types.cjs +19 -0
- package/dist/tools/browser/types.cjs.map +1 -0
- package/dist/tools/browser/types.js +1 -0
- package/dist/tools/browser/types.js.map +1 -0
- package/dist/tools/browser/vercel.cjs +242 -0
- package/dist/tools/browser/vercel.cjs.map +1 -0
- package/dist/tools/browser/vercel.js +49 -0
- package/dist/tools/browser/vercel.js.map +1 -0
- package/dist/tools/codebase_search/anthropic.cjs +267 -0
- package/dist/tools/codebase_search/anthropic.cjs.map +1 -0
- package/dist/tools/codebase_search/anthropic.js +11 -0
- package/dist/tools/codebase_search/anthropic.js.map +1 -0
- package/dist/tools/codebase_search/core.cjs +201 -0
- package/dist/tools/codebase_search/core.cjs.map +1 -0
- package/dist/tools/codebase_search/core.js +11 -0
- package/dist/tools/codebase_search/core.js.map +1 -0
- package/dist/tools/codebase_search/index.cjs +393 -0
- package/dist/tools/codebase_search/index.cjs.map +1 -0
- package/dist/tools/codebase_search/index.js +27 -0
- package/dist/tools/codebase_search/index.js.map +1 -0
- package/dist/tools/codebase_search/openai.cjs +316 -0
- package/dist/tools/codebase_search/openai.cjs.map +1 -0
- package/dist/tools/codebase_search/openai.js +21 -0
- package/dist/tools/codebase_search/openai.js.map +1 -0
- package/dist/tools/codebase_search/prompts.cjs +57 -0
- package/dist/tools/codebase_search/prompts.cjs.map +1 -0
- package/dist/tools/codebase_search/prompts.js +10 -0
- package/dist/tools/codebase_search/prompts.js.map +1 -0
- package/dist/tools/codebase_search/types.cjs +19 -0
- package/dist/tools/codebase_search/types.cjs.map +1 -0
- package/dist/tools/codebase_search/types.js +1 -0
- package/dist/tools/codebase_search/types.js.map +1 -0
- package/dist/tools/codebase_search/vercel.cjs +230 -0
- package/dist/tools/codebase_search/vercel.cjs.map +1 -0
- package/dist/tools/codebase_search/vercel.js +15 -0
- package/dist/tools/codebase_search/vercel.js.map +1 -0
- package/dist/tools/fastapply/anthropic.cjs +335 -0
- package/dist/tools/fastapply/anthropic.cjs.map +1 -0
- package/dist/tools/fastapply/anthropic.js +13 -0
- package/dist/tools/fastapply/anthropic.js.map +1 -0
- package/dist/tools/fastapply/core.cjs +267 -0
- package/dist/tools/fastapply/core.cjs.map +1 -0
- package/dist/tools/fastapply/core.js +15 -0
- package/dist/tools/fastapply/core.js.map +1 -0
- package/dist/tools/fastapply/index.cjs +500 -0
- package/dist/tools/fastapply/index.cjs.map +1 -0
- package/dist/tools/fastapply/index.js +32 -0
- package/dist/tools/fastapply/index.js.map +1 -0
- package/dist/tools/fastapply/openai.cjs +353 -0
- package/dist/tools/fastapply/openai.cjs.map +1 -0
- package/dist/tools/fastapply/openai.js +21 -0
- package/dist/tools/fastapply/openai.js.map +1 -0
- package/dist/tools/fastapply/prompts.cjs +68 -0
- package/dist/tools/fastapply/prompts.cjs.map +1 -0
- package/dist/tools/fastapply/prompts.js +10 -0
- package/dist/tools/fastapply/prompts.js.map +1 -0
- package/dist/tools/fastapply/types.cjs +19 -0
- package/dist/tools/fastapply/types.cjs.map +1 -0
- package/dist/tools/fastapply/types.js +1 -0
- package/dist/tools/fastapply/types.js.map +1 -0
- package/dist/tools/fastapply/vercel.cjs +347 -0
- package/dist/tools/fastapply/vercel.cjs.map +1 -0
- package/dist/tools/fastapply/vercel.js +17 -0
- package/dist/tools/fastapply/vercel.js.map +1 -0
- package/dist/tools/index.cjs +500 -0
- package/dist/tools/index.cjs.map +1 -0
- package/dist/tools/index.js +32 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/modelrouter/core.cjs +286 -0
- package/dist/tools/modelrouter/core.cjs.map +1 -0
- package/dist/tools/modelrouter/core.js +13 -0
- package/dist/tools/modelrouter/core.js.map +1 -0
- package/dist/tools/modelrouter/index.cjs +286 -0
- package/dist/tools/modelrouter/index.cjs.map +1 -0
- package/dist/tools/modelrouter/index.js +13 -0
- package/dist/tools/modelrouter/index.js.map +1 -0
- package/dist/tools/modelrouter/types.cjs +19 -0
- package/dist/tools/modelrouter/types.cjs.map +1 -0
- package/dist/tools/modelrouter/types.js +1 -0
- package/dist/tools/modelrouter/types.js.map +1 -0
- package/dist/tools/utils/resilience.cjs +115 -0
- package/dist/tools/utils/resilience.cjs.map +1 -0
- package/dist/tools/utils/resilience.js +12 -0
- package/dist/tools/utils/resilience.js.map +1 -0
- package/package.json +159 -0
|
@@ -0,0 +1,242 @@
|
|
|
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/vercel.ts
|
|
21
|
+
var vercel_exports = {};
|
|
22
|
+
__export(vercel_exports, {
|
|
23
|
+
browserTool: () => browserTool,
|
|
24
|
+
createBrowserTool: () => createBrowserTool
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(vercel_exports);
|
|
27
|
+
var import_ai = require("ai");
|
|
28
|
+
var import_zod = require("zod");
|
|
29
|
+
|
|
30
|
+
// tools/utils/resilience.ts
|
|
31
|
+
var DEFAULT_RETRY_CONFIG = {
|
|
32
|
+
maxRetries: 3,
|
|
33
|
+
initialDelay: 1e3,
|
|
34
|
+
maxDelay: 3e4,
|
|
35
|
+
backoffMultiplier: 2,
|
|
36
|
+
retryableErrors: ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND"]
|
|
37
|
+
};
|
|
38
|
+
async function fetchWithRetry(url, options, retryConfig = {}) {
|
|
39
|
+
const {
|
|
40
|
+
maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,
|
|
41
|
+
initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,
|
|
42
|
+
maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,
|
|
43
|
+
backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
|
|
44
|
+
retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,
|
|
45
|
+
onRetry
|
|
46
|
+
} = retryConfig;
|
|
47
|
+
let lastError = null;
|
|
48
|
+
let delay = initialDelay;
|
|
49
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(url, options);
|
|
52
|
+
if (response.status === 429 || response.status === 503) {
|
|
53
|
+
if (attempt < maxRetries) {
|
|
54
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
55
|
+
const waitTime = retryAfter ? parseInt(retryAfter) * 1e3 : Math.min(delay, maxDelay);
|
|
56
|
+
const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);
|
|
57
|
+
if (onRetry) {
|
|
58
|
+
onRetry(attempt + 1, error);
|
|
59
|
+
}
|
|
60
|
+
await sleep(waitTime);
|
|
61
|
+
delay *= backoffMultiplier;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return response;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
lastError = error;
|
|
68
|
+
const isRetryable = retryableErrors.some(
|
|
69
|
+
(errType) => lastError?.message?.includes(errType)
|
|
70
|
+
);
|
|
71
|
+
if (!isRetryable || attempt === maxRetries) {
|
|
72
|
+
throw lastError;
|
|
73
|
+
}
|
|
74
|
+
const waitTime = Math.min(delay, maxDelay);
|
|
75
|
+
if (onRetry) {
|
|
76
|
+
onRetry(attempt + 1, lastError);
|
|
77
|
+
}
|
|
78
|
+
await sleep(waitTime);
|
|
79
|
+
delay *= backoffMultiplier;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
throw lastError || new Error("Max retries exceeded");
|
|
83
|
+
}
|
|
84
|
+
async function withTimeout(promise, timeoutMs, errorMessage) {
|
|
85
|
+
let timeoutId;
|
|
86
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
87
|
+
timeoutId = setTimeout(() => {
|
|
88
|
+
reject(new Error(errorMessage || `Operation timed out after ${timeoutMs}ms`));
|
|
89
|
+
}, timeoutMs);
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
const result = await Promise.race([promise, timeoutPromise]);
|
|
93
|
+
clearTimeout(timeoutId);
|
|
94
|
+
return result;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
clearTimeout(timeoutId);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function sleep(ms) {
|
|
101
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// tools/browser/core.ts
|
|
105
|
+
var DEFAULT_CONFIG = {
|
|
106
|
+
apiUrl: process.env.MORPH_ENVIRONMENT === "DEV" ? "http://localhost:8000" : "https://browser.morphllm.com",
|
|
107
|
+
timeout: 12e4,
|
|
108
|
+
// 2 minutes for complex tasks
|
|
109
|
+
debug: false
|
|
110
|
+
};
|
|
111
|
+
async function executeBrowserTask(input, config = {}) {
|
|
112
|
+
const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;
|
|
113
|
+
const timeout = config.timeout || DEFAULT_CONFIG.timeout;
|
|
114
|
+
const debug = config.debug || false;
|
|
115
|
+
if (!input.task || input.task.trim().length === 0) {
|
|
116
|
+
return { success: false, error: "Task description is required" };
|
|
117
|
+
}
|
|
118
|
+
if (input.max_steps !== void 0 && (input.max_steps < 1 || input.max_steps > 50)) {
|
|
119
|
+
return { success: false, error: "max_steps must be between 1 and 50" };
|
|
120
|
+
}
|
|
121
|
+
if (debug) {
|
|
122
|
+
console.log(`[Browser] Task: "${input.task.slice(0, 60)}..." url=${input.url || "none"} maxSteps=${input.max_steps ?? 10}`);
|
|
123
|
+
console.log(`[Browser] Recording: ${input.record_video ? "yes" : "no"} | Calling ${apiUrl}/browser-task`);
|
|
124
|
+
}
|
|
125
|
+
const startTime = Date.now();
|
|
126
|
+
try {
|
|
127
|
+
const headers = { "Content-Type": "application/json" };
|
|
128
|
+
if (config.apiKey) headers["Authorization"] = `Bearer ${config.apiKey}`;
|
|
129
|
+
const fetchPromise = fetchWithRetry(
|
|
130
|
+
`${apiUrl}/browser-task`,
|
|
131
|
+
{
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers,
|
|
134
|
+
body: JSON.stringify({
|
|
135
|
+
task: input.task,
|
|
136
|
+
url: input.url,
|
|
137
|
+
max_steps: input.max_steps ?? 10,
|
|
138
|
+
model: input.model ?? "morph-computer-use-v0",
|
|
139
|
+
viewport_width: input.viewport_width ?? 1280,
|
|
140
|
+
viewport_height: input.viewport_height ?? 720,
|
|
141
|
+
repo_id: input.repo_id,
|
|
142
|
+
commit_id: input.commit_id,
|
|
143
|
+
record_video: input.record_video ?? false,
|
|
144
|
+
video_width: input.video_width ?? input.viewport_width ?? 1280,
|
|
145
|
+
video_height: input.video_height ?? input.viewport_height ?? 720,
|
|
146
|
+
structured_output: input.structured_output
|
|
147
|
+
})
|
|
148
|
+
},
|
|
149
|
+
config.retryConfig
|
|
150
|
+
);
|
|
151
|
+
const response = await withTimeout(
|
|
152
|
+
fetchPromise,
|
|
153
|
+
timeout,
|
|
154
|
+
`Browser task timed out after ${timeout}ms. Consider increasing timeout or reducing max_steps.`
|
|
155
|
+
);
|
|
156
|
+
if (!response.ok) {
|
|
157
|
+
const errorText = await response.text().catch(() => response.statusText);
|
|
158
|
+
if (debug) console.error(`[Browser] Error: ${response.status} - ${errorText}`);
|
|
159
|
+
throw new Error(`HTTP ${response.status}: ${errorText}`);
|
|
160
|
+
}
|
|
161
|
+
const result = await response.json();
|
|
162
|
+
const elapsed = Date.now() - startTime;
|
|
163
|
+
if (debug) {
|
|
164
|
+
console.log(`[Browser] \u2705 ${result.success ? "Success" : "Failed"} in ${elapsed}ms | steps=${result.steps_taken ?? 0} recordingId=${result.recording_id ?? "none"}`);
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (error instanceof Error) {
|
|
169
|
+
if (error.message.includes("ECONNREFUSED") || error.message.includes("fetch failed")) {
|
|
170
|
+
return {
|
|
171
|
+
success: false,
|
|
172
|
+
error: `Cannot connect to browser worker at ${apiUrl}. Ensure the service is running.`
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
success: false,
|
|
177
|
+
error: error.message
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
success: false,
|
|
182
|
+
error: String(error)
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// tools/browser/prompts.ts
|
|
188
|
+
var BROWSER_TOOL_DESCRIPTION = `Execute natural language browser automation tasks using an AI-powered agent. The agent can navigate websites, interact with elements, fill forms, click buttons, and verify functionality.
|
|
189
|
+
|
|
190
|
+
Use this tool to:
|
|
191
|
+
- Test web applications end-to-end
|
|
192
|
+
- Verify UI functionality
|
|
193
|
+
- Automate user workflows
|
|
194
|
+
- Extract information from web pages
|
|
195
|
+
|
|
196
|
+
The agent uses GPT-4o-mini to interpret your task and execute the necessary browser actions. It runs in a remote browser via Browserless.io, so it can access any publicly accessible URL.
|
|
197
|
+
|
|
198
|
+
Important:
|
|
199
|
+
- Provide clear, specific task descriptions
|
|
200
|
+
- Include the starting URL if navigating to a specific page
|
|
201
|
+
- Use remote URLs (e.g., https://3000-xyz.e2b.dev) not localhost
|
|
202
|
+
- Complex tasks may require more max_steps`;
|
|
203
|
+
|
|
204
|
+
// tools/browser/vercel.ts
|
|
205
|
+
function createBrowserTool(config) {
|
|
206
|
+
const schema = import_zod.z.object({
|
|
207
|
+
task: import_zod.z.string().describe('Natural language description of what to do (e.g., "Test checkout flow for buying a pineapple")'),
|
|
208
|
+
url: import_zod.z.string().optional().describe("Starting URL (e.g., https://3000-xyz.e2b.dev)"),
|
|
209
|
+
max_steps: import_zod.z.number().min(1).max(50).default(10).describe("Maximum number of browser actions to take"),
|
|
210
|
+
region: import_zod.z.enum(["sfo", "lon"]).default("sfo").describe("Browserless region: sfo (US West) or lon (Europe)")
|
|
211
|
+
});
|
|
212
|
+
return (0, import_ai.tool)({
|
|
213
|
+
description: BROWSER_TOOL_DESCRIPTION,
|
|
214
|
+
parameters: schema,
|
|
215
|
+
execute: async (params) => {
|
|
216
|
+
const { task, url, max_steps, region } = params;
|
|
217
|
+
const result = await executeBrowserTask(
|
|
218
|
+
{ task, url, max_steps, region },
|
|
219
|
+
config
|
|
220
|
+
);
|
|
221
|
+
if (result.success) {
|
|
222
|
+
return {
|
|
223
|
+
success: true,
|
|
224
|
+
result: result.result,
|
|
225
|
+
steps_taken: result.steps_taken,
|
|
226
|
+
execution_time_ms: result.execution_time_ms
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
success: false,
|
|
231
|
+
error: result.error
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
var browserTool = createBrowserTool();
|
|
237
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
238
|
+
0 && (module.exports = {
|
|
239
|
+
browserTool,
|
|
240
|
+
createBrowserTool
|
|
241
|
+
});
|
|
242
|
+
//# sourceMappingURL=vercel.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../tools/browser/vercel.ts","../../../tools/utils/resilience.ts","../../../tools/browser/core.ts","../../../tools/browser/prompts.ts"],"sourcesContent":["/**\n * Vercel AI SDK adapter for browser automation tool\n */\n\nimport { tool as createTool } from 'ai';\nimport { z } from 'zod';\nimport { executeBrowserTask } from './core.js';\nimport type { BrowserConfig } from './types.js';\nimport { BROWSER_TOOL_DESCRIPTION } from './prompts.js';\n\n/**\n * Create Vercel AI SDK tool for browser automation\n * \n * @param config - Optional browser worker configuration\n * @returns Vercel AI SDK tool\n * \n * @example\n * ```typescript\n * import { generateText } from 'ai';\n * import { anthropic } from '@ai-sdk/anthropic';\n * import { createBrowserTool } from 'morphsdk/tools/browser/vercel';\n * \n * const browserTool = createBrowserTool({\n * apiUrl: 'https://browser-worker.example.com'\n * });\n * \n * const result = await generateText({\n * model: anthropic('claude-sonnet-4-5-20250929'),\n * tools: { browserTask: browserTool },\n * prompt: 'Test the checkout flow at https://3000-abc.e2b.dev',\n * maxSteps: 5\n * });\n * ```\n */\nexport function createBrowserTool(config?: BrowserConfig) {\n const schema = z.object({\n task: z.string().describe('Natural language description of what to do (e.g., \"Test checkout flow for buying a pineapple\")'),\n url: z.string().optional().describe('Starting URL (e.g., https://3000-xyz.e2b.dev)'),\n max_steps: z.number().min(1).max(50).default(10).describe('Maximum number of browser actions to take'),\n region: z.enum(['sfo', 'lon']).default('sfo').describe('Browserless region: sfo (US West) or lon (Europe)'),\n });\n\n return createTool({\n description: BROWSER_TOOL_DESCRIPTION,\n parameters: schema,\n execute: async (params) => {\n const { task, url, max_steps, region } = params;\n const result = await executeBrowserTask(\n { task, url, max_steps, region },\n config\n );\n\n if (result.success) {\n return {\n success: true,\n result: result.result,\n steps_taken: result.steps_taken,\n execution_time_ms: result.execution_time_ms,\n };\n }\n\n return {\n success: false,\n error: result.error,\n };\n },\n });\n}\n\n/**\n * Default browser tool for Vercel AI SDK\n */\nexport const browserTool = createBrowserTool();\n\n","/**\n * Resilience utilities for retry logic and timeout handling\n */\n\nexport interface RetryConfig {\n maxRetries?: number; // Default: 3\n initialDelay?: number; // Default: 1000ms\n maxDelay?: number; // Default: 30000ms\n backoffMultiplier?: number; // Default: 2\n retryableErrors?: string[]; // Default: ['ECONNREFUSED', 'ETIMEDOUT', 'ENOTFOUND']\n onRetry?: (attempt: number, error: Error) => void;\n}\n\nconst DEFAULT_RETRY_CONFIG: Required<Omit<RetryConfig, 'onRetry'>> = {\n maxRetries: 3,\n initialDelay: 1000,\n maxDelay: 30000,\n backoffMultiplier: 2,\n retryableErrors: ['ECONNREFUSED', 'ETIMEDOUT', 'ENOTFOUND'],\n};\n\n/**\n * Retry a fetch request with exponential backoff\n * \n * @param url - Request URL\n * @param options - Fetch options\n * @param retryConfig - Retry configuration\n * @returns Response from fetch\n * \n * @example\n * ```typescript\n * const response = await fetchWithRetry(\n * 'https://api.example.com/data',\n * { method: 'POST', body: JSON.stringify(data) },\n * { maxRetries: 5, initialDelay: 500 }\n * );\n * ```\n */\nexport async function fetchWithRetry(\n url: string,\n options: RequestInit,\n retryConfig: RetryConfig = {}\n): Promise<Response> {\n const {\n maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,\n initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,\n maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,\n backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,\n retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,\n onRetry,\n } = retryConfig;\n\n let lastError: Error | null = null;\n let delay = initialDelay;\n\n for (let attempt = 0; attempt <= maxRetries; attempt++) {\n try {\n const response = await fetch(url, options);\n \n // Retry on 429 (rate limit) or 503 (service unavailable)\n if (response.status === 429 || response.status === 503) {\n if (attempt < maxRetries) {\n // Check for Retry-After header\n const retryAfter = response.headers.get('Retry-After');\n const waitTime = retryAfter \n ? parseInt(retryAfter) * 1000 \n : Math.min(delay, maxDelay);\n \n const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);\n if (onRetry) {\n onRetry(attempt + 1, error);\n }\n \n await sleep(waitTime);\n delay *= backoffMultiplier;\n continue;\n }\n }\n\n return response;\n } catch (error) {\n lastError = error as Error;\n \n // Check if error is retryable\n const isRetryable = retryableErrors.some(errType => \n lastError?.message?.includes(errType)\n );\n\n if (!isRetryable || attempt === maxRetries) {\n throw lastError;\n }\n\n // Exponential backoff\n const waitTime = Math.min(delay, maxDelay);\n if (onRetry) {\n onRetry(attempt + 1, lastError);\n }\n \n await sleep(waitTime);\n delay *= backoffMultiplier;\n }\n }\n\n throw lastError || new Error('Max retries exceeded');\n}\n\n/**\n * Add timeout to any promise\n * \n * @param promise - Promise to wrap with timeout\n * @param timeoutMs - Timeout in milliseconds\n * @param errorMessage - Optional custom error message\n * @returns Promise that rejects if timeout is reached\n * \n * @example\n * ```typescript\n * const result = await withTimeout(\n * fetchData(),\n * 5000,\n * 'Data fetch timed out'\n * );\n * ```\n */\nexport async function withTimeout<T>(\n promise: Promise<T>,\n timeoutMs: number,\n errorMessage?: string\n): Promise<T> {\n let timeoutId: NodeJS.Timeout | number;\n \n const timeoutPromise = new Promise<never>((_, reject) => {\n timeoutId = setTimeout(() => {\n reject(new Error(errorMessage || `Operation timed out after ${timeoutMs}ms`));\n }, timeoutMs);\n });\n\n try {\n const result = await Promise.race([promise, timeoutPromise]);\n clearTimeout(timeoutId!);\n return result;\n } catch (error) {\n clearTimeout(timeoutId!);\n throw error;\n }\n}\n\n/**\n * Sleep for specified milliseconds\n */\nfunction sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Unified error type for all tools\n */\nexport class MorphError extends Error {\n constructor(\n message: string,\n public code: string,\n public statusCode?: number,\n public retryable: boolean = false\n ) {\n super(message);\n this.name = 'MorphError';\n }\n}\n\n\n","/**\n * Core implementation for browser automation tasks\n */\n\nimport { fetchWithRetry, withTimeout } from '../utils/resilience.js';\nimport type {\n BrowserConfig,\n BrowserTaskInput,\n BrowserTaskInputWithSchema,\n BrowserTaskResult,\n BrowserTaskWithPromise,\n BrowserTaskWithPromiseAndSchema,\n RecordingStatus,\n ErrorsResponse,\n} from './types.js';\n\nconst DEFAULT_CONFIG = {\n apiUrl: process.env.MORPH_ENVIRONMENT === 'DEV' \n ? 'http://localhost:8000'\n : 'https://browser.morphllm.com',\n timeout: 120000, // 2 minutes for complex tasks\n debug: false,\n};\n\n/**\n * BrowserClient class for easier usage with instance configuration\n */\nexport class BrowserClient {\n private config: BrowserConfig;\n\n constructor(config: BrowserConfig = {}) {\n this.config = {\n ...DEFAULT_CONFIG,\n ...config,\n };\n }\n\n /**\n * Execute a browser automation task\n */\n async execute(input: BrowserTaskInput): Promise<BrowserTaskResult> {\n return executeBrowserTask(input, this.config);\n }\n\n async createTask(input: BrowserTaskInput): Promise<BrowserTaskWithPromise>;\n async createTask<T>(input: BrowserTaskInputWithSchema<T>): Promise<BrowserTaskWithPromiseAndSchema<T>>;\n async createTask<T>(\n input: BrowserTaskInput | BrowserTaskInputWithSchema<T>\n ): Promise<BrowserTaskWithPromise | BrowserTaskWithPromiseAndSchema<T>> {\n if ('schema' in input) {\n const taskInput: BrowserTaskInput = {\n ...input,\n structured_output: stringifyStructuredOutput(input.schema),\n };\n const result = await executeBrowserTask(taskInput, this.config);\n return wrapTaskResponseWithSchema(result, this.config, input.schema);\n } else {\n const result = await executeBrowserTask(input, this.config);\n return wrapTaskResponse(result, this.config);\n }\n }\n\n /**\n * Execute task with recording and wait for video to be ready\n */\n async executeWithRecording(\n input: BrowserTaskInput & { record_video: true }\n ): Promise<BrowserTaskResult & { recording?: RecordingStatus }> {\n return executeWithRecording(input, this.config);\n }\n\n /**\n * Get recording status and URLs\n */\n async getRecording(recordingId: string): Promise<RecordingStatus> {\n return getRecording(recordingId, this.config);\n }\n\n /**\n * Wait for recording to complete with automatic polling\n */\n async waitForRecording(\n recordingId: string,\n options?: { timeout?: number; pollInterval?: number }\n ): Promise<RecordingStatus> {\n return waitForRecording(recordingId, this.config, options);\n }\n\n /**\n * Get errors from recording with screenshots\n */\n async getErrors(recordingId: string): Promise<ErrorsResponse> {\n return getErrors(recordingId, this.config);\n }\n\n /**\n * Check if browser worker service is healthy\n */\n async checkHealth(): Promise<{\n ok: boolean;\n google_configured: boolean;\n database_configured: boolean;\n s3_configured: boolean;\n error?: string;\n }> {\n return checkHealth(this.config);\n }\n}\n\n/**\n * Execute a natural language browser automation task\n * \n * @param input - Task parameters\n * @param config - Optional configuration (apiKey, apiUrl to override default)\n * @returns Task result with success status and findings\n * \n * @example\n * ```typescript\n * const result = await executeBrowserTask(\n * {\n * task: \"Test checkout flow for buying a pineapple\",\n * url: \"https://3000-abc.e2b.dev\",\n * max_steps: 20,\n * repo_id: \"my-project\",\n * commit_id: \"uuid-here\"\n * },\n * {\n * apiKey: process.env.MORPH_API_KEY,\n * // apiUrl: 'http://localhost:8001' // Override for local testing\n * }\n * );\n * \n * if (result.success) {\n * console.log('Task completed:', result.result);\n * console.log('Replay:', result.replay_url);\n * }\n * ```\n */\nexport async function executeBrowserTask(\n input: BrowserTaskInput,\n config: BrowserConfig = {}\n): Promise<BrowserTaskResult> {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n const timeout = config.timeout || DEFAULT_CONFIG.timeout;\n const debug = config.debug || false;\n\n if (!input.task || input.task.trim().length === 0) {\n return { success: false, error: 'Task description is required' };\n }\n\n if (input.max_steps !== undefined && (input.max_steps < 1 || input.max_steps > 50)) {\n return { success: false, error: 'max_steps must be between 1 and 50' };\n }\n\n if (debug) {\n console.log(`[Browser] Task: \"${input.task.slice(0, 60)}...\" url=${input.url || 'none'} maxSteps=${input.max_steps ?? 10}`);\n console.log(`[Browser] Recording: ${input.record_video ? 'yes' : 'no'} | Calling ${apiUrl}/browser-task`);\n }\n\n const startTime = Date.now();\n\n try {\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n if (config.apiKey) headers['Authorization'] = `Bearer ${config.apiKey}`;\n\n const fetchPromise = fetchWithRetry(\n `${apiUrl}/browser-task`,\n {\n method: 'POST',\n headers,\n body: JSON.stringify({\n task: input.task,\n url: input.url,\n max_steps: input.max_steps ?? 10,\n model: input.model ?? 'morph-computer-use-v0',\n viewport_width: input.viewport_width ?? 1280,\n viewport_height: input.viewport_height ?? 720,\n repo_id: input.repo_id,\n commit_id: input.commit_id,\n record_video: input.record_video ?? false,\n video_width: input.video_width ?? input.viewport_width ?? 1280,\n video_height: input.video_height ?? input.viewport_height ?? 720,\n structured_output: input.structured_output,\n }),\n },\n config.retryConfig\n );\n\n const response = await withTimeout(\n fetchPromise,\n timeout,\n `Browser task timed out after ${timeout}ms. Consider increasing timeout or reducing max_steps.`\n );\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n if (debug) console.error(`[Browser] Error: ${response.status} - ${errorText}`);\n throw new Error(`HTTP ${response.status}: ${errorText}`);\n }\n\n const result: BrowserTaskResult = await response.json();\n const elapsed = Date.now() - startTime;\n \n if (debug) {\n console.log(`[Browser] ✅ ${result.success ? 'Success' : 'Failed'} in ${elapsed}ms | steps=${result.steps_taken ?? 0} recordingId=${result.recording_id ?? 'none'}`);\n }\n\n return result;\n\n } catch (error) {\n if (error instanceof Error) {\n // Handle network errors\n if (error.message.includes('ECONNREFUSED') || error.message.includes('fetch failed')) {\n return {\n success: false,\n error: `Cannot connect to browser worker at ${apiUrl}. Ensure the service is running.`,\n };\n }\n\n return {\n success: false,\n error: error.message,\n };\n }\n\n return {\n success: false,\n error: String(error),\n };\n }\n}\n\n/**\n * Get recording status and video URL\n * \n * @param recordingId - Recording UUID from BrowserTaskResult\n * @param config - Configuration with apiKey\n * @returns Recording status with video URL when ready\n * \n * @example\n * ```typescript\n * const status = await getRecording('uuid-here', { apiKey: 'key' });\n * if (status.status === 'COMPLETED' && status.video_url) {\n * console.log('Video ready:', status.video_url);\n * }\n * ```\n */\nexport async function getRecording(\n recordingId: string,\n config: BrowserConfig = {}\n): Promise<RecordingStatus> {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n const debug = config.debug || false;\n\n if (!config.apiKey) {\n throw new Error('API key required for getRecording');\n }\n\n if (debug) console.log(`[Browser] getRecording: ${recordingId}`);\n\n const response = await fetch(`${apiUrl}/recordings/${recordingId}`, {\n method: 'GET',\n headers: { 'Authorization': `Bearer ${config.apiKey}` },\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n if (debug) console.error(`[Browser] getRecording error: ${response.status} - ${errorText}`);\n throw new Error(`HTTP ${response.status}: ${errorText}`);\n }\n\n const recording = await response.json();\n if (debug) console.log(`[Browser] Recording status: ${recording.status}`);\n\n return recording;\n}\n\n/**\n * Wait for recording to complete with automatic polling\n * \n * @param recordingId - Recording UUID\n * @param config - Configuration with apiKey\n * @param options - Polling options\n * @returns Recording status when completed or errored\n * \n * @example\n * ```typescript\n * const result = await executeBrowserTask({ task: '...', record_video: true }, config);\n * if (result.recording_id) {\n * const recording = await waitForRecording(result.recording_id, config, {\n * timeout: 60000, // 1 minute\n * pollInterval: 2000 // Check every 2 seconds\n * });\n * console.log('Video URL:', recording.video_url);\n * }\n * ```\n */\nexport async function waitForRecording(\n recordingId: string,\n config: BrowserConfig = {},\n options: { timeout?: number; pollInterval?: number } = {}\n): Promise<RecordingStatus> {\n const timeout = options.timeout ?? 60000; // Default 1 minute\n const pollInterval = options.pollInterval ?? 2000; // Default 2 seconds\n const startTime = Date.now();\n\n while (Date.now() - startTime < timeout) {\n const status = await getRecording(recordingId, config);\n \n if (status.status === 'COMPLETED' || status.status === 'ERROR') {\n return status;\n }\n\n // Wait before next poll\n await new Promise(resolve => setTimeout(resolve, pollInterval));\n }\n\n throw new Error(`Recording timeout after ${timeout}ms - status still pending`);\n}\n\n/**\n * Execute task with recording and wait for video to be ready\n * \n * @param input - Task parameters with record_video=true\n * @param config - Configuration with apiKey\n * @returns Task result with ready video URL\n * \n * @example\n * ```typescript\n * const result = await executeWithRecording(\n * {\n * task: \"Test checkout flow\",\n * url: \"https://example.com\",\n * record_video: true,\n * repo_id: \"my-project\"\n * },\n * { apiKey: process.env.MORPH_API_KEY }\n * );\n * \n * console.log('Task result:', result.result);\n * console.log('Video URL:', result.recording?.video_url);\n * ```\n */\nexport async function executeWithRecording(\n input: BrowserTaskInput & { record_video: true },\n config: BrowserConfig = {}\n): Promise<BrowserTaskResult & { recording?: RecordingStatus }> {\n // Execute task with recording\n const taskResult = await executeBrowserTask(input, config);\n\n // If recording was created, wait for it to complete\n if (taskResult.recording_id) {\n try {\n const recording = await waitForRecording(\n taskResult.recording_id,\n config,\n { timeout: 60000, pollInterval: 2000 }\n );\n return {\n ...taskResult,\n recording,\n };\n } catch (error) {\n // Return task result even if recording fails\n return {\n ...taskResult,\n recording: {\n id: taskResult.recording_id,\n status: 'ERROR',\n error: error instanceof Error ? error.message : String(error),\n created_at: new Date().toISOString(),\n },\n };\n }\n }\n\n return taskResult;\n}\n\n/**\n * Get errors from recording with screenshots\n * \n * Screenshots are captured in real-time (500ms after error occurs) during the browser session.\n * \n * @param recordingId - Recording UUID from BrowserTaskResult\n * @param config - Configuration with apiKey\n * @returns Errors with real-time screenshots\n * \n * @example\n * ```typescript\n * const { errors, total_errors } = await getErrors('uuid-here', { apiKey: 'key' });\n * \n * console.log(`Found ${total_errors} errors`);\n * \n * errors.forEach(err => {\n * console.log(`[${err.type}] ${err.message}`);\n * if (err.url) console.log(` URL: ${err.url}`);\n * if (err.screenshot_url) console.log(` Screenshot: ${err.screenshot_url}`);\n * \n * // Download screenshot\n * if (err.screenshot_url) {\n * const response = await fetch(err.screenshot_url);\n * const screenshot = await response.arrayBuffer();\n * // Save or process screenshot\n * }\n * });\n * ```\n */\nexport async function getErrors(\n recordingId: string,\n config: BrowserConfig = {}\n): Promise<ErrorsResponse> {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n const debug = config.debug || false;\n\n if (!config.apiKey) {\n throw new Error('API key required for getErrors');\n }\n\n if (debug) console.log(`[Browser] getErrors: ${recordingId}`);\n\n const response = await fetch(`${apiUrl}/recordings/${recordingId}/errors`, {\n method: 'GET',\n headers: { 'Authorization': `Bearer ${config.apiKey}` },\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n if (debug) console.error(`[Browser] getErrors error: ${response.status} - ${errorText}`);\n throw new Error(`HTTP ${response.status}: ${errorText}`);\n }\n\n const errors = await response.json();\n if (debug) console.log(`[Browser] Found ${errors.total_errors} errors`);\n\n return errors;\n}\n\n/**\n * Helper to serialize Zod schema for API\n */\nfunction stringifyStructuredOutput(schema: any): string {\n try {\n return JSON.stringify({\n type: 'object',\n description: 'Zod schema definition (Zod v3)',\n zodDef: schema._def,\n });\n } catch (error) {\n console.warn('[Browser] Failed to serialize Zod schema:', error);\n return JSON.stringify({\n type: 'object',\n description: 'Schema serialization failed',\n });\n }\n}\n\n/**\n * Parse and validate structured task output\n */\nfunction parseStructuredTaskOutput<T>(\n result: BrowserTaskResult,\n schema: any\n): BrowserTaskResult & { parsed: T | null } {\n if (!result.output) {\n return { ...result, parsed: null };\n }\n\n try {\n const parsed = JSON.parse(result.output);\n const validated = schema.parse(parsed) as T;\n return { ...result, parsed: validated };\n } catch (error) {\n if (error instanceof SyntaxError) {\n return { ...result, parsed: null };\n }\n throw error;\n }\n}\n\n/**\n * Get current task status\n */\nasync function getTaskStatus(\n taskId: string,\n config: BrowserConfig\n): Promise<BrowserTaskResult> {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n const debug = config.debug || false;\n\n if (debug) console.log(`[Browser] getTaskStatus: ${taskId}`);\n\n const headers: Record<string, string> = {};\n if (config.apiKey) headers['Authorization'] = `Bearer ${config.apiKey}`;\n\n const response = await fetch(`${apiUrl}/tasks/${taskId}`, {\n method: 'GET',\n headers,\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => response.statusText);\n if (debug) console.error(`[Browser] getTaskStatus error: ${response.status} - ${errorText}`);\n throw new Error(`HTTP ${response.status}: ${errorText}`);\n }\n\n const result: BrowserTaskResult = await response.json();\n if (debug) console.log(`[Browser] Task status: ${result.status}`);\n\n return result;\n}\n\n/**\n * Generate live URL for watching task execution in real-time\n */\nfunction generateLiveUrl(taskId: string, config: BrowserConfig): string {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n const baseUrl = apiUrl.replace('/api', '');\n return `${baseUrl}/tasks/${taskId}/live`;\n}\n\n/**\n * Poll task until completion\n */\nasync function pollTaskUntilComplete(\n taskId: string,\n config: BrowserConfig,\n pollConfig: { interval?: number; timeout?: number } = {}\n): Promise<BrowserTaskResult> {\n const interval = pollConfig.interval ?? 2000; // 2 seconds\n const timeout = pollConfig.timeout ?? 300000; // 5 minutes\n const startTime = Date.now();\n\n while (Date.now() - startTime < timeout) {\n const status = await getTaskStatus(taskId, config);\n \n if (status.status === 'completed' || status.status === 'failed') {\n return status;\n }\n\n await new Promise(resolve => setTimeout(resolve, interval));\n }\n\n throw new Error(`Task polling timeout after ${timeout}ms`);\n}\n\n/**\n * Wrap task response with convenience methods\n */\nfunction wrapTaskResponse(\n result: BrowserTaskResult,\n config: BrowserConfig\n): BrowserTaskWithPromise {\n if (!result.task_id) {\n throw new Error('task_id is required to wrap response');\n }\n\n return {\n ...result,\n task_id: result.task_id,\n liveUrl: generateLiveUrl(result.task_id, config),\n complete: async (pollConfig?: { interval?: number; timeout?: number }) => {\n return pollTaskUntilComplete(result.task_id!, config, pollConfig);\n },\n };\n}\n\n/**\n * Wrap task response with schema validation\n */\nfunction wrapTaskResponseWithSchema<T>(\n result: BrowserTaskResult,\n config: BrowserConfig,\n schema: any\n): BrowserTaskWithPromiseAndSchema<T> {\n if (!result.task_id) {\n throw new Error('task_id is required to wrap response');\n }\n\n const parsed = result.output\n ? parseStructuredTaskOutput<T>(result, schema)\n : { ...result, parsed: null };\n\n return {\n ...parsed,\n task_id: result.task_id,\n liveUrl: generateLiveUrl(result.task_id, config),\n complete: async (pollConfig?: { interval?: number; timeout?: number }) => {\n const finalResult = await pollTaskUntilComplete(result.task_id!, config, pollConfig);\n return parseStructuredTaskOutput<T>(finalResult, schema);\n },\n };\n}\n\n/**\n * Check if browser worker service is healthy\n * \n * @param config - Optional configuration\n * @returns Health status\n */\nexport async function checkHealth(config: BrowserConfig = {}): Promise<{\n ok: boolean;\n google_configured: boolean;\n database_configured: boolean;\n s3_configured: boolean;\n error?: string;\n}> {\n const apiUrl = config.apiUrl || DEFAULT_CONFIG.apiUrl;\n\n try {\n const response = await fetch(`${apiUrl}/health`, {\n method: 'GET',\n headers: config.apiKey\n ? { 'Authorization': `Bearer ${config.apiKey}` }\n : {},\n });\n\n if (!response.ok) {\n throw new Error(`HTTP ${response.status}`);\n }\n\n const data = await response.json();\n return {\n ok: true,\n google_configured: data.google_configured ?? false,\n database_configured: data.database_configured ?? false,\n s3_configured: data.s3_configured ?? false,\n };\n } catch (error) {\n return {\n ok: false,\n google_configured: false,\n database_configured: false,\n s3_configured: false,\n error: error instanceof Error ? error.message : String(error),\n };\n }\n}\n\n","/**\n * Tool descriptions and prompts for AI models\n */\n\nexport const BROWSER_TOOL_DESCRIPTION = `Execute natural language browser automation tasks using an AI-powered agent. The agent can navigate websites, interact with elements, fill forms, click buttons, and verify functionality.\n\nUse this tool to:\n- Test web applications end-to-end\n- Verify UI functionality\n- Automate user workflows\n- Extract information from web pages\n\nThe agent uses GPT-4o-mini to interpret your task and execute the necessary browser actions. It runs in a remote browser via Browserless.io, so it can access any publicly accessible URL.\n\nImportant:\n- Provide clear, specific task descriptions\n- Include the starting URL if navigating to a specific page\n- Use remote URLs (e.g., https://3000-xyz.e2b.dev) not localhost\n- Complex tasks may require more max_steps`;\n\nexport const BROWSER_SYSTEM_PROMPT = `You have access to browser automation capabilities. When testing or interacting with web applications:\n\n1. Be specific about what you're testing or verifying\n2. Break complex tasks into clear steps\n3. Always provide the full URL including protocol (https://)\n4. For localhost apps, use the remote tunnel URL (e.g., e2b.dev URLs)\n5. Specify the number of steps needed - simple tasks use 5-10, complex flows use 15-30\n\nExample good tasks:\n- \"Go to https://3000-abc.e2b.dev and verify the landing page loads with a hero section\"\n- \"Test guest checkout: add a pineapple to cart, proceed to checkout, fill shipping info, and verify order summary\"\n- \"Navigate to the dashboard and click on the settings tab, then verify the API keys section is visible\"\n\nExample bad tasks:\n- \"test the app\" (too vague)\n- \"go to localhost:3000\" (use remote URL instead)\n- \"do everything\" (not specific enough)`;\n\n"],"mappings":"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","names":["createTool"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BROWSER_TOOL_DESCRIPTION
|
|
3
|
+
} from "../../chunk-7PZJQFCY.js";
|
|
4
|
+
import {
|
|
5
|
+
executeBrowserTask
|
|
6
|
+
} from "../../chunk-4UVEBIDK.js";
|
|
7
|
+
import "../../chunk-4VWJFZVS.js";
|
|
8
|
+
import "../../chunk-PZ5AY32C.js";
|
|
9
|
+
|
|
10
|
+
// tools/browser/vercel.ts
|
|
11
|
+
import { tool as createTool } from "ai";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
function createBrowserTool(config) {
|
|
14
|
+
const schema = z.object({
|
|
15
|
+
task: z.string().describe('Natural language description of what to do (e.g., "Test checkout flow for buying a pineapple")'),
|
|
16
|
+
url: z.string().optional().describe("Starting URL (e.g., https://3000-xyz.e2b.dev)"),
|
|
17
|
+
max_steps: z.number().min(1).max(50).default(10).describe("Maximum number of browser actions to take"),
|
|
18
|
+
region: z.enum(["sfo", "lon"]).default("sfo").describe("Browserless region: sfo (US West) or lon (Europe)")
|
|
19
|
+
});
|
|
20
|
+
return createTool({
|
|
21
|
+
description: BROWSER_TOOL_DESCRIPTION,
|
|
22
|
+
parameters: schema,
|
|
23
|
+
execute: async (params) => {
|
|
24
|
+
const { task, url, max_steps, region } = params;
|
|
25
|
+
const result = await executeBrowserTask(
|
|
26
|
+
{ task, url, max_steps, region },
|
|
27
|
+
config
|
|
28
|
+
);
|
|
29
|
+
if (result.success) {
|
|
30
|
+
return {
|
|
31
|
+
success: true,
|
|
32
|
+
result: result.result,
|
|
33
|
+
steps_taken: result.steps_taken,
|
|
34
|
+
execution_time_ms: result.execution_time_ms
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
success: false,
|
|
39
|
+
error: result.error
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
var browserTool = createBrowserTool();
|
|
45
|
+
export {
|
|
46
|
+
browserTool,
|
|
47
|
+
createBrowserTool
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=vercel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../tools/browser/vercel.ts"],"sourcesContent":["/**\n * Vercel AI SDK adapter for browser automation tool\n */\n\nimport { tool as createTool } from 'ai';\nimport { z } from 'zod';\nimport { executeBrowserTask } from './core.js';\nimport type { BrowserConfig } from './types.js';\nimport { BROWSER_TOOL_DESCRIPTION } from './prompts.js';\n\n/**\n * Create Vercel AI SDK tool for browser automation\n * \n * @param config - Optional browser worker configuration\n * @returns Vercel AI SDK tool\n * \n * @example\n * ```typescript\n * import { generateText } from 'ai';\n * import { anthropic } from '@ai-sdk/anthropic';\n * import { createBrowserTool } from 'morphsdk/tools/browser/vercel';\n * \n * const browserTool = createBrowserTool({\n * apiUrl: 'https://browser-worker.example.com'\n * });\n * \n * const result = await generateText({\n * model: anthropic('claude-sonnet-4-5-20250929'),\n * tools: { browserTask: browserTool },\n * prompt: 'Test the checkout flow at https://3000-abc.e2b.dev',\n * maxSteps: 5\n * });\n * ```\n */\nexport function createBrowserTool(config?: BrowserConfig) {\n const schema = z.object({\n task: z.string().describe('Natural language description of what to do (e.g., \"Test checkout flow for buying a pineapple\")'),\n url: z.string().optional().describe('Starting URL (e.g., https://3000-xyz.e2b.dev)'),\n max_steps: z.number().min(1).max(50).default(10).describe('Maximum number of browser actions to take'),\n region: z.enum(['sfo', 'lon']).default('sfo').describe('Browserless region: sfo (US West) or lon (Europe)'),\n });\n\n return createTool({\n description: BROWSER_TOOL_DESCRIPTION,\n parameters: schema,\n execute: async (params) => {\n const { task, url, max_steps, region } = params;\n const result = await executeBrowserTask(\n { task, url, max_steps, region },\n config\n );\n\n if (result.success) {\n return {\n success: true,\n result: result.result,\n steps_taken: result.steps_taken,\n execution_time_ms: result.execution_time_ms,\n };\n }\n\n return {\n success: false,\n error: result.error,\n };\n },\n });\n}\n\n/**\n * Default browser tool for Vercel AI SDK\n */\nexport const browserTool = createBrowserTool();\n\n"],"mappings":";;;;;;;;;;AAIA,SAAS,QAAQ,kBAAkB;AACnC,SAAS,SAAS;AA6BX,SAAS,kBAAkB,QAAwB;AACxD,QAAM,SAAS,EAAE,OAAO;AAAA,IACtB,MAAM,EAAE,OAAO,EAAE,SAAS,gGAAgG;AAAA,IAC1H,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,SAAS,+CAA+C;AAAA,IACnF,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,EAAE,SAAS,2CAA2C;AAAA,IACrG,QAAQ,EAAE,KAAK,CAAC,OAAO,KAAK,CAAC,EAAE,QAAQ,KAAK,EAAE,SAAS,mDAAmD;AAAA,EAC5G,CAAC;AAED,SAAO,WAAW;AAAA,IAChB,aAAa;AAAA,IACb,YAAY;AAAA,IACZ,SAAS,OAAO,WAAW;AACzB,YAAM,EAAE,MAAM,KAAK,WAAW,OAAO,IAAI;AACzC,YAAM,SAAS,MAAM;AAAA,QACnB,EAAE,MAAM,KAAK,WAAW,OAAO;AAAA,QAC/B;AAAA,MACF;AAEA,UAAI,OAAO,SAAS;AAClB,eAAO;AAAA,UACL,SAAS;AAAA,UACT,QAAQ,OAAO;AAAA,UACf,aAAa,OAAO;AAAA,UACpB,mBAAmB,OAAO;AAAA,QAC5B;AAAA,MACF;AAEA,aAAO;AAAA,QACL,SAAS;AAAA,QACT,OAAO,OAAO;AAAA,MAChB;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAKO,IAAM,cAAc,kBAAkB;","names":[]}
|
|
@@ -0,0 +1,267 @@
|
|
|
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/codebase_search/anthropic.ts
|
|
21
|
+
var anthropic_exports = {};
|
|
22
|
+
__export(anthropic_exports, {
|
|
23
|
+
createCodebaseSearchTool: () => createCodebaseSearchTool
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(anthropic_exports);
|
|
26
|
+
|
|
27
|
+
// tools/utils/resilience.ts
|
|
28
|
+
var DEFAULT_RETRY_CONFIG = {
|
|
29
|
+
maxRetries: 3,
|
|
30
|
+
initialDelay: 1e3,
|
|
31
|
+
maxDelay: 3e4,
|
|
32
|
+
backoffMultiplier: 2,
|
|
33
|
+
retryableErrors: ["ECONNREFUSED", "ETIMEDOUT", "ENOTFOUND"]
|
|
34
|
+
};
|
|
35
|
+
async function fetchWithRetry(url, options, retryConfig = {}) {
|
|
36
|
+
const {
|
|
37
|
+
maxRetries = DEFAULT_RETRY_CONFIG.maxRetries,
|
|
38
|
+
initialDelay = DEFAULT_RETRY_CONFIG.initialDelay,
|
|
39
|
+
maxDelay = DEFAULT_RETRY_CONFIG.maxDelay,
|
|
40
|
+
backoffMultiplier = DEFAULT_RETRY_CONFIG.backoffMultiplier,
|
|
41
|
+
retryableErrors = DEFAULT_RETRY_CONFIG.retryableErrors,
|
|
42
|
+
onRetry
|
|
43
|
+
} = retryConfig;
|
|
44
|
+
let lastError = null;
|
|
45
|
+
let delay = initialDelay;
|
|
46
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
47
|
+
try {
|
|
48
|
+
const response = await fetch(url, options);
|
|
49
|
+
if (response.status === 429 || response.status === 503) {
|
|
50
|
+
if (attempt < maxRetries) {
|
|
51
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
52
|
+
const waitTime = retryAfter ? parseInt(retryAfter) * 1e3 : Math.min(delay, maxDelay);
|
|
53
|
+
const error = new Error(`HTTP ${response.status}: Retrying after ${waitTime}ms`);
|
|
54
|
+
if (onRetry) {
|
|
55
|
+
onRetry(attempt + 1, error);
|
|
56
|
+
}
|
|
57
|
+
await sleep(waitTime);
|
|
58
|
+
delay *= backoffMultiplier;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return response;
|
|
63
|
+
} catch (error) {
|
|
64
|
+
lastError = error;
|
|
65
|
+
const isRetryable = retryableErrors.some(
|
|
66
|
+
(errType) => lastError?.message?.includes(errType)
|
|
67
|
+
);
|
|
68
|
+
if (!isRetryable || attempt === maxRetries) {
|
|
69
|
+
throw lastError;
|
|
70
|
+
}
|
|
71
|
+
const waitTime = Math.min(delay, maxDelay);
|
|
72
|
+
if (onRetry) {
|
|
73
|
+
onRetry(attempt + 1, lastError);
|
|
74
|
+
}
|
|
75
|
+
await sleep(waitTime);
|
|
76
|
+
delay *= backoffMultiplier;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
throw lastError || new Error("Max retries exceeded");
|
|
80
|
+
}
|
|
81
|
+
async function withTimeout(promise, timeoutMs, errorMessage) {
|
|
82
|
+
let timeoutId;
|
|
83
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
84
|
+
timeoutId = setTimeout(() => {
|
|
85
|
+
reject(new Error(errorMessage || `Operation timed out after ${timeoutMs}ms`));
|
|
86
|
+
}, timeoutMs);
|
|
87
|
+
});
|
|
88
|
+
try {
|
|
89
|
+
const result = await Promise.race([promise, timeoutPromise]);
|
|
90
|
+
clearTimeout(timeoutId);
|
|
91
|
+
return result;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
clearTimeout(timeoutId);
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function sleep(ms) {
|
|
98
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// tools/codebase_search/core.ts
|
|
102
|
+
async function executeCodebaseSearch(input, config) {
|
|
103
|
+
const apiKey = config.apiKey || process.env.MORPH_API_KEY;
|
|
104
|
+
if (!apiKey) {
|
|
105
|
+
throw new Error("MORPH_API_KEY not found. Set environment variable or pass in config");
|
|
106
|
+
}
|
|
107
|
+
const searchUrl = config.searchUrl || process.env.MORPH_SEARCH_URL || "http://embedrerank.morphllm.com:8081";
|
|
108
|
+
const timeout = config.timeout || 3e4;
|
|
109
|
+
const debug = config.debug || false;
|
|
110
|
+
if (debug) {
|
|
111
|
+
console.log(`[CodebaseSearch] Query: "${input.query.slice(0, 60)}..." repo=${config.repoId}`);
|
|
112
|
+
console.log(`[CodebaseSearch] URL: ${searchUrl}/v1/codebase_search`);
|
|
113
|
+
}
|
|
114
|
+
const startTime = Date.now();
|
|
115
|
+
try {
|
|
116
|
+
const fetchPromise = fetchWithRetry(
|
|
117
|
+
`${searchUrl}/v1/codebase_search`,
|
|
118
|
+
{
|
|
119
|
+
method: "POST",
|
|
120
|
+
headers: {
|
|
121
|
+
"Content-Type": "application/json",
|
|
122
|
+
"Authorization": `Bearer ${apiKey}`
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
query: input.query,
|
|
126
|
+
repoId: config.repoId,
|
|
127
|
+
targetDirectories: input.target_directories || [],
|
|
128
|
+
limit: input.limit || 10,
|
|
129
|
+
candidateLimit: 50
|
|
130
|
+
})
|
|
131
|
+
},
|
|
132
|
+
config.retryConfig
|
|
133
|
+
);
|
|
134
|
+
const response = await withTimeout(fetchPromise, timeout, `Codebase search timed out after ${timeout}ms`);
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
const errorText = await response.text();
|
|
137
|
+
if (debug) console.error(`[CodebaseSearch] Error: ${response.status} - ${errorText}`);
|
|
138
|
+
return {
|
|
139
|
+
success: false,
|
|
140
|
+
results: [],
|
|
141
|
+
stats: { totalResults: 0, candidatesRetrieved: 0, searchTimeMs: 0 },
|
|
142
|
+
error: `Search failed (${response.status}): ${errorText}`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const data = await response.json();
|
|
146
|
+
const elapsed = Date.now() - startTime;
|
|
147
|
+
if (debug) {
|
|
148
|
+
console.log(`[CodebaseSearch] \u2705 ${data.results?.length || 0} results in ${elapsed}ms`);
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
success: true,
|
|
152
|
+
results: data.results || [],
|
|
153
|
+
stats: data.stats || { totalResults: 0, candidatesRetrieved: 0, searchTimeMs: elapsed }
|
|
154
|
+
};
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (debug) console.error(`[CodebaseSearch] Exception: ${error instanceof Error ? error.message : error}`);
|
|
157
|
+
return {
|
|
158
|
+
success: false,
|
|
159
|
+
results: [],
|
|
160
|
+
stats: { totalResults: 0, candidatesRetrieved: 0, searchTimeMs: 0 },
|
|
161
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// tools/codebase_search/prompts.ts
|
|
167
|
+
var CODEBASE_SEARCH_DESCRIPTION = `Semantic search that finds code by meaning, not exact text.
|
|
168
|
+
|
|
169
|
+
Use this to explore unfamiliar codebases or ask "how/where/what" questions:
|
|
170
|
+
- "How does X work?" - Find implementation details
|
|
171
|
+
- "Where is Y handled?" - Locate specific functionality
|
|
172
|
+
- "What happens when Z?" - Understand flow
|
|
173
|
+
|
|
174
|
+
The tool uses two-stage retrieval (embedding similarity + reranking) to find the most semantically relevant code chunks.
|
|
175
|
+
|
|
176
|
+
Returns code chunks with file paths, line ranges, and full content ranked by relevance.`;
|
|
177
|
+
var CODEBASE_SEARCH_SYSTEM_PROMPT = `You have access to the codebase_search tool that performs semantic code search.
|
|
178
|
+
|
|
179
|
+
When searching:
|
|
180
|
+
- Use natural language queries describing what you're looking for
|
|
181
|
+
- Be specific about functionality, not variable names
|
|
182
|
+
- Use target_directories to narrow search if you know the area
|
|
183
|
+
- Results are ranked by relevance (rerank score is most important)
|
|
184
|
+
|
|
185
|
+
The tool returns:
|
|
186
|
+
- File paths with symbol names (e.g. "src/auth.ts::AuthService@L1-L17")
|
|
187
|
+
- Line ranges for precise navigation
|
|
188
|
+
- Full code content for each match
|
|
189
|
+
- Dual relevance scores: embedding similarity + rerank score
|
|
190
|
+
|
|
191
|
+
Use results to understand code or answer questions. The content is provided in full - avoid re-reading unless you need more context.`;
|
|
192
|
+
|
|
193
|
+
// tools/codebase_search/anthropic.ts
|
|
194
|
+
function createCodebaseSearchTool(config) {
|
|
195
|
+
const toolDefinition = {
|
|
196
|
+
name: "codebase_search",
|
|
197
|
+
description: CODEBASE_SEARCH_DESCRIPTION,
|
|
198
|
+
input_schema: {
|
|
199
|
+
type: "object",
|
|
200
|
+
properties: {
|
|
201
|
+
explanation: {
|
|
202
|
+
type: "string",
|
|
203
|
+
description: "One sentence explanation as to why this tool is being used, and how it contributes to the goal."
|
|
204
|
+
},
|
|
205
|
+
query: {
|
|
206
|
+
type: "string",
|
|
207
|
+
description: 'A complete question about what you want to understand. Ask as if talking to a colleague: "How does X work?", "What happens when Y?", "Where is Z handled?"'
|
|
208
|
+
},
|
|
209
|
+
target_directories: {
|
|
210
|
+
type: "array",
|
|
211
|
+
items: { type: "string" },
|
|
212
|
+
description: "Prefix directory paths to limit search scope (single directory only, no glob patterns). Use [] to search entire repo."
|
|
213
|
+
},
|
|
214
|
+
limit: {
|
|
215
|
+
type: "number",
|
|
216
|
+
description: "Maximum results to return (default: 10)"
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
required: ["query", "target_directories", "explanation"]
|
|
220
|
+
},
|
|
221
|
+
cache_control: { type: "ephemeral" }
|
|
222
|
+
};
|
|
223
|
+
return Object.assign(toolDefinition, {
|
|
224
|
+
execute: async (input) => {
|
|
225
|
+
return executeCodebaseSearch(input, config);
|
|
226
|
+
},
|
|
227
|
+
formatResult: (result) => {
|
|
228
|
+
return formatResult(result);
|
|
229
|
+
},
|
|
230
|
+
getSystemPrompt: () => {
|
|
231
|
+
return CODEBASE_SEARCH_SYSTEM_PROMPT;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
function formatResult(result) {
|
|
236
|
+
if (!result.success) {
|
|
237
|
+
return `Search failed: ${result.error}`;
|
|
238
|
+
}
|
|
239
|
+
if (result.results.length === 0) {
|
|
240
|
+
return "No matching code found. Try rephrasing your query or broadening the search scope.";
|
|
241
|
+
}
|
|
242
|
+
const lines = [];
|
|
243
|
+
lines.push(`Found ${result.results.length} relevant code sections (searched ${result.stats.candidatesRetrieved} candidates in ${result.stats.searchTimeMs}ms):
|
|
244
|
+
`);
|
|
245
|
+
result.results.forEach((r, i) => {
|
|
246
|
+
const relevance = (r.rerankScore * 100).toFixed(1);
|
|
247
|
+
lines.push(`${i + 1}. ${r.filepath} (${relevance}% relevant)`);
|
|
248
|
+
lines.push(` Symbol: ${r.symbolPath}`);
|
|
249
|
+
lines.push(` Language: ${r.language}`);
|
|
250
|
+
lines.push(` Lines: ${r.startLine}-${r.endLine}`);
|
|
251
|
+
lines.push(` Code:`);
|
|
252
|
+
const codeLines = r.content.split("\n");
|
|
253
|
+
codeLines.slice(0, Math.min(codeLines.length, 20)).forEach((line) => {
|
|
254
|
+
lines.push(` ${line}`);
|
|
255
|
+
});
|
|
256
|
+
if (codeLines.length > 20) {
|
|
257
|
+
lines.push(` ... (${codeLines.length - 20} more lines)`);
|
|
258
|
+
}
|
|
259
|
+
lines.push("");
|
|
260
|
+
});
|
|
261
|
+
return lines.join("\n");
|
|
262
|
+
}
|
|
263
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
264
|
+
0 && (module.exports = {
|
|
265
|
+
createCodebaseSearchTool
|
|
266
|
+
});
|
|
267
|
+
//# sourceMappingURL=anthropic.cjs.map
|