给 AI / AgentAgent 任务配方

Agent 任务配方

安装依赖、创建服务、编写测试和迁移项目的最小操作模板

最后更新于

配方:添加一个依赖

前置检查:确认仓库使用 Bun,并读取当前 workspace 位置。

bun add zod
bun run typecheck
bun test

验收package.jsonbun.lock 同步;没有意外 lifecycle 变化;相关测试通过。

配方:实现 HTTP 路由

const server = Bun.serve({
  routes: {
    '/api/items/:id': async (request) => {
      const id = request.params.id;
      if (!/^\d+$/.test(id)) {
        return Response.json({ error: 'invalid id' }, { status: 400 });
      }
      return Response.json({ id: Number(id) });
    },
  },
  fetch: () => Response.json({ error: 'not found' }, { status: 404 }),
});

Agent 注意:先查询项目是否已有路由框架、错误格式、鉴权和日志中间件。已有架构优先于这段最小示例。

配方:调用子进程

const proc = Bun.spawn(['git', 'status', '--short'], {
  stdout: 'pipe',
  stderr: 'pipe',
});

const [stdout, stderr, exitCode] = await Promise.all([
  new Response(proc.stdout).text(),
  new Response(proc.stderr).text(),
  proc.exited,
]);

if (exitCode !== 0) throw new Error(stderr);
console.log(stdout);

不要把用户输入拼接进 sh -c 字符串。

配方:文件读写

const configFile = Bun.file('./config.json');
if (!(await configFile.exists())) throw new Error('config.json is missing');

const config = await configFile.json();
await Bun.write('./dist/config.json', JSON.stringify(config, null, 2));

Agent 注意:写入前确认目标路径、现有文件和覆盖授权;重要文件使用原子写入或项目既有工具。

配方:最小迁移探针

bun install
bun run typecheck
bun test
bun run build

把每一步的退出码和失败类型记录下来。不要在第一步失败时把所有错误都归因于 Bun,也不要在四步通过后直接推断生产兼容。