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