-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
328 lines (288 loc) · 11.6 KB
/
Copy pathagent.ts
File metadata and controls
328 lines (288 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Single-file Harbor agent harness: --agent-import-path agent:AutoAgent
*
* LLM-agnostic via Vercel AI SDK. Change MODEL to switch providers:
* "openai/gpt-5" | "anthropic/claude-opus-4-1" | "google/gemini-2.5-pro"
*
* The MODEL string is passed directly to the Vercel AI SDK which resolves
* it to the correct provider automatically (via @ai-sdk/* packages).
*
* Top section: editable harness (meta-agent modifies this).
* Bottom section: fixed adapter (Harbor integration + ATIF serialization).
*/
import { ToolLoopAgent, tool, stepCountIs } from "ai";
import type { LanguageModel } from "ai";
import { z } from "zod";
import { readFileSync, writeFileSync, mkdirSync, copyFileSync } from "node:fs";
import { join } from "node:path";
import { initStagehand, createBrowserTools, closeStagehand } from "./src/tools/browser.js";
import { loadMCPConfig, connectMCPServers, mergeMCPTools, closeMCPServers, type MCPClientHandle } from "./src/tools/mcp.js";
import { createTerminalTools } from "./src/tools/terminal.js";
import { createComputerTools } from "./src/tools/computer.js";
import { toTOON, truncateAXI } from "./src/utils/toon.js";
import { execSync } from "node:child_process";
import type { BaseEnvironment, AgentContext, ExecResult } from "./src/harbor.js";
import { type AgentRunResult, toAtif } from "./src/atif.js";
// ============================================================================
// EDITABLE HARNESS SECTION — the meta-agent modifies everything above the
// "FIXED ADAPTER BOUNDARY" comment below.
// ============================================================================
const SYSTEM_PROMPT = "You are an agent that executes tasks";
/**
* Model identifier in "provider/model-name" format.
* Vercel AI SDK resolves this to the correct provider package automatically.
* Requires the corresponding @ai-sdk/<provider> package to be installed.
*
* Examples:
* "openai/gpt-5"
* "anthropic/claude-opus-4-1"
* "google/gemini-2.5-pro"
*/
import { google } from "@ai-sdk/google";
const MODEL: LanguageModel = google("gemini-2.5-flash") as unknown as LanguageModel;
const MODEL_NAME = "google/gemini-2.5-flash";
const MAX_TURNS = 30;
/**
* Create tools for the agent. Add new tools here.
*/
function createTools(environment: BaseEnvironment) {
return {
run_shell: tool({
description:
"Run a shell command in the task environment. Returns stdout and stderr.",
inputSchema: z.object({
command: z.string().describe("The shell command to execute"),
}),
execute: async (args) => {
try {
const result = await environment.exec(args.command, 120);
let out = "";
if (result.stdout) out += truncateAXI(result.stdout, 2000);
if (result.stderr) {
out += out
? `\nerror: ${truncateAXI(result.stderr, 1000)}`
: `error: ${truncateAXI(result.stderr, 1000)}`;
}
const final = out || "status: success (no output)";
return final + "\nhelp[1]: Run `run_shell` with `ls` to check the filesystem if unsure.";
} catch (err: any) {
return `error: ${err.message || err}`;
}
},
}),
};
}
/**
* Build the agent. Modify to add more tools, sub-agents, or change config.
*/
async function createAgent(environment: BaseEnvironment) {
const baseTools = createTools(environment);
// Browser tools (Stagehand) — gracefully unavailable if Chromium is missing
let browserTools: Record<string, any> = {};
try {
const stagehand = await initStagehand();
browserTools = createBrowserTools(stagehand);
console.log(`[Agent] Browser tools enabled: ${Object.keys(browserTools).join(", ")}`);
} catch (e: any) {
console.warn(`[Agent] Browser tools unavailable: ${e.message}`);
}
// MCP tools (Dynamic discovery)
let mcpTools: Record<string, any> = {};
let mcpHandles: MCPClientHandle[] = [];
try {
const configs = loadMCPConfig();
if (configs.length > 0) {
mcpHandles = await connectMCPServers(configs);
mcpTools = mergeMCPTools(mcpHandles);
console.log(`[Agent] MCP tools enabled: ${Object.keys(mcpTools).join(", ")}`);
}
} catch (e: any) {
console.warn(`[Agent] MCP tools unavailable: ${e.message}`);
}
// Computer-use tools (Gracefully unavailable if DISPLAY is not set/ready)
let computerTools: Record<string, any> = {};
try {
computerTools = createComputerTools();
console.log(`[Agent] Computer tools enabled: ${Object.keys(computerTools).join(", ")}`);
} catch (e: any) {
console.warn(`[Agent] Computer tools unavailable: ${e.message}`);
}
// Terminal tools (Persistent sessions)
const terminalTools = createTerminalTools();
return {
agent: new ToolLoopAgent({
model: MODEL,
instructions: SYSTEM_PROMPT,
tools: { ...baseTools, ...browserTools, ...terminalTools, ...computerTools, ...mcpTools },
stopWhen: stepCountIs(MAX_TURNS),
}),
mcpHandles,
};
}
/**
* Run the agent on a task and return (result, durationMs).
*/
async function runTask(
environment: BaseEnvironment,
instruction: string,
): Promise<{ result: AgentRunResult; durationMs: number }> {
// Feature 2: Start desktop if needed
try {
await environment.exec("bash scripts/start-desktop.sh", 15);
} catch (e) {
// Silently continue if desktop fails to start
}
const { agent, mcpHandles } = await createAgent(environment);
const t0 = Date.now();
// Feature 4: AXI - Ambient Context
const ambientContext = `
## Ambient Context
cwd: ${process.cwd().replace(process.env.HOME || "", "~")}
bin: autoharness (agent-engineer)
tools: ${Object.keys((agent as any).tools).join(", ")}
`;
const result = await agent.generate({ prompt: ambientContext + "\nTASK: " + instruction });
const durationMs = Date.now() - t0;
// Clean up resources after each task
await closeStagehand();
await closeMCPServers(mcpHandles);
return { result: result as unknown as AgentRunResult, durationMs };
}
// ============================================================================
// FIXED ADAPTER BOUNDARY — Do not modify below this line.
// Harbor integration + ATIF trajectory serialization.
// ============================================================================
/**
* Local environment implementation for running inside Docker containers.
* Executes commands via child_process and handles file operations locally.
*/
class LocalEnvironment implements BaseEnvironment {
async exec(
command: string,
timeoutSec: number = 120,
env?: Record<string, string>,
): Promise<ExecResult> {
try {
const stdout = execSync(command, {
timeout: timeoutSec * 1000,
encoding: "utf-8",
env: { ...process.env, ...env },
maxBuffer: 10 * 1024 * 1024, // 10 MB
});
return { stdout: stdout ?? "", stderr: "", exitCode: 0 };
} catch (err: unknown) {
const execErr = err as {
stdout?: string;
stderr?: string;
status?: number;
};
return {
stdout: execErr.stdout ?? "",
stderr: execErr.stderr ?? "",
exitCode: execErr.status ?? 1,
};
}
}
async uploadFile(sourcePath: string, targetPath: string): Promise<void> {
const dir = targetPath.substring(0, targetPath.lastIndexOf("/"));
if (dir) mkdirSync(dir, { recursive: true });
copyFileSync(sourcePath, targetPath);
}
}
/**
* Container entrypoint — reads instruction, runs agent, writes ATIF trajectory.
*/
async function runInContainer(): Promise<void> {
const instruction = readFileSync("/task/instruction.md", "utf-8").trim();
const environment = new LocalEnvironment();
const { result, durationMs } = await runTask(environment, instruction);
// Serialize ATIF trajectory
const atif = toAtif(result, MODEL_NAME, durationMs);
const trajDir = "/logs/agent";
mkdirSync(trajDir, { recursive: true });
writeFileSync(
join(trajDir, "trajectory.json"),
JSON.stringify(atif, null, 2),
"utf-8",
);
// Print summary
const fm = atif.final_metrics;
console.log(
`turns=${fm.extra.num_turns} duration_ms=${durationMs} ` +
`input=${fm.total_prompt_tokens} output=${fm.total_completion_tokens}`,
);
}
// ---------------------------------------------------------------------------
// AutoAgent class — Harbor BaseAgent adapter.
// In Harbor's Python runner, this class is instantiated and its run() method
// is called. In the TypeScript port, when running inside a container, we
// use runInContainer() directly as the entrypoint.
// ---------------------------------------------------------------------------
export class AutoAgent {
static readonly SUPPORTS_ATIF = true;
private readonly _logsDir: string;
private readonly _extraEnv: Record<string, string>;
constructor(
logsDir: string = "/logs/agent",
extraEnv?: Record<string, string>,
) {
this._logsDir = logsDir;
this._extraEnv = extraEnv ? { ...extraEnv } : {};
mkdirSync(this._logsDir, { recursive: true });
}
static agentName(): string {
return "autoharness";
}
version(): string {
return "0.1.0";
}
async setup(_environment: BaseEnvironment): Promise<void> {
// No-op: override in subclasses if needed
}
async run(
instruction: string,
environment: BaseEnvironment,
context: AgentContext,
): Promise<void> {
// Ensure task directory exists
await environment.exec("mkdir -p /task");
// Write instruction
const instrPath = join(this._logsDir, "instruction.md");
writeFileSync(instrPath, instruction, "utf-8");
await environment.uploadFile(instrPath, "/task/instruction.md");
// Run the agent
const { result, durationMs } = await runTask(environment, instruction);
// Serialize trajectory
const atif = toAtif(result, MODEL_NAME, durationMs);
const trajPath = join(this._logsDir, "trajectory.json");
writeFileSync(trajPath, JSON.stringify(atif, null, 2), "utf-8");
// Update context with token usage
try {
const fm = atif.final_metrics;
context.nInputTokens = fm.total_prompt_tokens;
context.nOutputTokens = fm.total_completion_tokens;
context.nCacheTokens = fm.total_cached_tokens;
} catch {
// Ignore context update errors
}
// Print summary
console.log(
`turns=${result.steps.length} duration_ms=${durationMs} ` +
`input=${result.usage.promptTokens} output=${result.usage.completionTokens}`,
);
}
}
// ---------------------------------------------------------------------------
// Container entrypoint
// ---------------------------------------------------------------------------
const isMainModule =
typeof process !== "undefined" &&
process.argv[1] &&
(process.argv[1].endsWith("agent.ts") ||
process.argv[1].endsWith("agent.js"));
if (isMainModule) {
runInContainer().catch((err) => {
console.error("Agent failed:", err);
process.exit(1);
});
}