From 6aec92e16deff9437714a7673bc7d2f36421eb60 Mon Sep 17 00:00:00 2001 From: Yunfei He Date: Sun, 24 May 2026 22:27:27 +0800 Subject: [PATCH] feat(coding-agent): add types and bash execution Co-Authored-By: Claude Opus 4.7 (1M context) --- examples/coding-agent/src/agent.ts | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/coding-agent/src/agent.ts diff --git a/examples/coding-agent/src/agent.ts b/examples/coding-agent/src/agent.ts new file mode 100644 index 0000000..4d7feeb --- /dev/null +++ b/examples/coding-agent/src/agent.ts @@ -0,0 +1,70 @@ +import { exec } from "node:child_process"; + +export interface Message { + role: "user" | "assistant" | "tool"; + content?: string; + tool_calls?: ToolCall[]; + tool_call_id?: string; +} + +export interface ToolCall { + id: string; + type: "function"; + function: { name: string; arguments: string }; +} + +export interface AgentOptions { + onToken: (token: string) => void; + onToolCall: (command: string) => void; + onToolResult: (output: string) => void; + onComplete: () => void; + autoApprove: boolean; + requestApproval: (command: string) => Promise; +} + +const BASH_TOOL = { + type: "function" as const, + function: { + name: "bash", + description: "Execute a bash command in the current working directory", + parameters: { + type: "object", + properties: { + command: { type: "string", description: "The bash command to execute" }, + }, + required: ["command"], + }, + }, +}; + +const SYSTEM_PROMPT = `You are a coding assistant running in a terminal. You have a bash tool to execute commands. +Use it to read files (cat, find, grep), write files (echo, tee), and run programs. +Explain what you're about to do before executing commands. +Keep responses concise. +Working directory: ${process.cwd()}`; + +const MAX_OUTPUT = 10000; + +export function executeBash(command: string): Promise { + return new Promise((resolve) => { + exec( + command, + { + shell: "/bin/bash", + cwd: process.cwd(), + timeout: 30_000, + maxBuffer: 10 * 1024 * 1024, + }, + (error, stdout, stderr) => { + let output = ""; + if (stdout) output += stdout; + if (stderr) output += (output ? "\n" : "") + stderr; + if (error && !output) output = error.message; + if (output.length > MAX_OUTPUT) { + output = output.slice(0, MAX_OUTPUT) + "\n... (truncated)"; + } + resolve(output || "(no output)"); + }, + ); + }); +}