@arnilo/prism-coding-agent 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/write.js ADDED
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Write tool: create or overwrite a file on the host filesystem.
3
+ *
4
+ * Behavioral port of pi's core/tools/write for @arnilo/prism-coding-agent, adapted to Prism's
5
+ * `ToolDefinition` contract. Faithfully ports the create-parent-dirs → write → serialize-per-path
6
+ * flow. Drops pi's TUI (`renderCall`/`renderResult`, the incremental syntax-highlight cache, key hints).
7
+ *
8
+ * Deviations from pi (documented):
9
+ * - **Confirmation message carries the absolute path + real byte count + line count** (pi returns the
10
+ * caller-supplied path and `content.length`, which is a UTF-16 *code-unit* count mislabeled "bytes").
11
+ * The plan's acceptance criteria call for absolute path + byte/line counts; this is strictly more
12
+ * informative and the byte count is now UTF-8-correct.
13
+ * - Abort + all fs failures return a Prism `error` result (pi throws/rejects). Abort is checked before
14
+ * each filesystem operation (mkdir, writeFile); if the write completes it is reported as success
15
+ * (pi would throw "Operation aborted" even after a successful write — misleading, so dropped).
16
+ */
17
+ import { Buffer } from "node:buffer";
18
+ import { mkdir as fsMkdir, writeFile as fsWriteFile } from "node:fs/promises";
19
+ import { dirname } from "node:path";
20
+ import { resolveToCwd } from "./path-utils.js";
21
+ import { withFileMutationQueue } from "./file-mutation-queue.js";
22
+ const defaultWriteOperations = {
23
+ writeFile: (path, content) => fsWriteFile(path, content, "utf-8"),
24
+ mkdir: (dir) => fsMkdir(dir, { recursive: true }).then(() => { }),
25
+ };
26
+ function errorResult(toolCallId, message) {
27
+ return {
28
+ toolCallId,
29
+ name: "write",
30
+ content: [{ type: "text", text: message }],
31
+ error: { message },
32
+ };
33
+ }
34
+ function countLines(content) {
35
+ if (content.length === 0)
36
+ return 0;
37
+ return content.split("\n").length;
38
+ }
39
+ export function createWriteTool(cwd, options) {
40
+ const ops = options?.operations ?? defaultWriteOperations;
41
+ return {
42
+ name: "write",
43
+ description: "Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates parent directories.",
44
+ parameters: {
45
+ type: "object",
46
+ properties: {
47
+ path: { type: "string", description: "Path to the file to write (relative or absolute)" },
48
+ content: { type: "string", description: "Content to write to the file" },
49
+ },
50
+ required: ["path", "content"],
51
+ additionalProperties: false,
52
+ },
53
+ async execute(args, context) {
54
+ const toolCallId = context.toolCallId;
55
+ const path = typeof args.path === "string" ? args.path : "";
56
+ const content = typeof args.content === "string" ? args.content : undefined;
57
+ if (path.length === 0) {
58
+ return errorResult(toolCallId, "path is required and must be a non-empty string.");
59
+ }
60
+ if (content === undefined) {
61
+ return errorResult(toolCallId, "content is required and must be a string.");
62
+ }
63
+ try {
64
+ const absolutePath = resolveToCwd(path, cwd);
65
+ const dir = dirname(absolutePath);
66
+ return await withFileMutationQueue(absolutePath, async () => {
67
+ // Check abort before each fs op — do not start a new operation once aborted. We intentionally
68
+ // do NOT throw from an abort listener: that could release the mutation queue mid-operation.
69
+ if (context.signal?.aborted)
70
+ return errorResult(toolCallId, "Operation aborted");
71
+ await ops.mkdir(dir);
72
+ if (context.signal?.aborted)
73
+ return errorResult(toolCallId, "Operation aborted");
74
+ await ops.writeFile(absolutePath, content);
75
+ const bytes = Buffer.byteLength(content, "utf-8");
76
+ const lines = countLines(content);
77
+ return {
78
+ toolCallId,
79
+ name: "write",
80
+ content: [
81
+ {
82
+ type: "text",
83
+ text: `Successfully wrote ${bytes} bytes (${lines} lines) to ${absolutePath}`,
84
+ },
85
+ ],
86
+ metadata: { bytes, lines, path: absolutePath },
87
+ };
88
+ });
89
+ }
90
+ catch (error) {
91
+ const message = error instanceof Error ? error.message : String(error);
92
+ return errorResult(toolCallId, message);
93
+ }
94
+ },
95
+ };
96
+ }
97
+ //# sourceMappingURL=write.js.map
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@arnilo/prism-coding-agent",
3
+ "version": "0.0.3",
4
+ "description": "Optional coding-agent tools (shell, read, write, edit) package for Prism.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "!dist/__tests__",
17
+ "!dist/**/*.map",
18
+ "README.md",
19
+ "CHANGELOG.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsc -p tsconfig.json",
23
+ "typecheck": "tsc -p tsconfig.json --noEmit",
24
+ "test": "node --test dist/__tests__/*.test.js",
25
+ "pack:dry-run": "npm pack --dry-run"
26
+ },
27
+ "dependencies": {
28
+ "diff": "^8.0.4"
29
+ },
30
+ "peerDependencies": {
31
+ "@arnilo/prism": "0.0.3"
32
+ },
33
+ "devDependencies": {
34
+ "@arnilo/prism": "file:../.."
35
+ },
36
+ "engines": {
37
+ "node": ">=20"
38
+ },
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/ashiqrniloy/prism.git",
43
+ "directory": "packages/coding-agent"
44
+ },
45
+ "bugs": {
46
+ "url": "https://github.com/ashiqrniloy/prism/issues"
47
+ },
48
+ "homepage": "https://github.com/ashiqrniloy/prism/tree/main/packages/coding-agent#readme",
49
+ "keywords": [
50
+ "prism",
51
+ "coding",
52
+ "tools",
53
+ "shell",
54
+ "agent",
55
+ "llm"
56
+ ],
57
+ "sideEffects": false,
58
+ "publishConfig": {
59
+ "access": "public"
60
+ }
61
+ }