content.ts

 1import { mkdir, writeFile } from "node:fs/promises";
 2import { dirname, join } from "node:path";
 3import { ensureWorkspacePath } from "../agent/tools/path-utils.js";
 4
 5export interface WorkspaceContent {
 6  filePath: string;
 7  content: string;
 8  bytes: number;
 9}
10
11export async function writeWorkspaceFile(
12  workspacePath: string,
13  relativePath: string,
14  content: string,
15): Promise<WorkspaceContent> {
16  const filePath = join(workspacePath, relativePath);
17  ensureWorkspacePath(workspacePath, filePath);
18  await mkdir(dirname(filePath), { recursive: true });
19  await writeFile(filePath, content, "utf8");
20
21  return {
22    filePath,
23    content,
24    bytes: Buffer.byteLength(content, "utf8"),
25  };
26}