@dexto/tools-plan 1.6.0 → 1.6.1
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/errors.cjs +126 -0
- package/dist/errors.js +99 -64
- package/dist/index.cjs +36 -0
- package/dist/index.d.cts +224 -0
- package/dist/index.js +9 -12
- package/dist/plan-service-getter.cjs +16 -0
- package/dist/plan-service-getter.js +0 -1
- package/dist/plan-service.cjs +247 -0
- package/dist/plan-service.js +201 -215
- package/dist/plan-service.test.cjs +227 -0
- package/dist/plan-service.test.js +200 -222
- package/dist/tool-factory-config.cjs +38 -0
- package/dist/tool-factory-config.js +13 -30
- package/dist/tool-factory.cjs +71 -0
- package/dist/tool-factory.js +39 -35
- package/dist/tool-factory.test.cjs +96 -0
- package/dist/tool-factory.test.js +90 -95
- package/dist/tools/plan-create-tool.cjs +102 -0
- package/dist/tools/plan-create-tool.d.ts.map +1 -1
- package/dist/tools/plan-create-tool.js +77 -82
- package/dist/tools/plan-create-tool.test.cjs +174 -0
- package/dist/tools/plan-create-tool.test.js +142 -134
- package/dist/tools/plan-read-tool.cjs +65 -0
- package/dist/tools/plan-read-tool.d.ts.map +1 -1
- package/dist/tools/plan-read-tool.js +39 -41
- package/dist/tools/plan-read-tool.test.cjs +109 -0
- package/dist/tools/plan-read-tool.test.js +78 -87
- package/dist/tools/plan-review-tool.cjs +98 -0
- package/dist/tools/plan-review-tool.d.ts.map +1 -1
- package/dist/tools/plan-review-tool.js +73 -87
- package/dist/tools/plan-update-tool.cjs +92 -0
- package/dist/tools/plan-update-tool.d.ts.map +1 -1
- package/dist/tools/plan-update-tool.js +65 -73
- package/dist/tools/plan-update-tool.test.cjs +203 -0
- package/dist/tools/plan-update-tool.test.js +171 -154
- package/dist/types.cjs +44 -0
- package/dist/types.js +17 -24
- package/package.json +7 -6
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var plan_service_exports = {};
|
|
30
|
+
__export(plan_service_exports, {
|
|
31
|
+
PlanService: () => PlanService
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(plan_service_exports);
|
|
34
|
+
var fs = __toESM(require("node:fs/promises"), 1);
|
|
35
|
+
var path = __toESM(require("node:path"), 1);
|
|
36
|
+
var import_node_fs = require("node:fs");
|
|
37
|
+
var import_types = require("./types.js");
|
|
38
|
+
var import_errors = require("./errors.js");
|
|
39
|
+
const PLAN_FILENAME = "plan.md";
|
|
40
|
+
const META_FILENAME = "plan-meta.json";
|
|
41
|
+
class PlanService {
|
|
42
|
+
basePath;
|
|
43
|
+
logger;
|
|
44
|
+
constructor(options, logger) {
|
|
45
|
+
this.basePath = options.basePath;
|
|
46
|
+
this.logger = logger;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Resolves and validates a session directory path.
|
|
50
|
+
* Prevents path traversal attacks by ensuring the resolved path stays within basePath.
|
|
51
|
+
*/
|
|
52
|
+
resolveSessionDir(sessionId) {
|
|
53
|
+
const base = path.resolve(this.basePath);
|
|
54
|
+
const resolved = path.resolve(base, sessionId);
|
|
55
|
+
const rel = path.relative(base, resolved);
|
|
56
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
57
|
+
throw import_errors.PlanError.invalidSessionId(sessionId);
|
|
58
|
+
}
|
|
59
|
+
return resolved;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Gets the directory path for a session's plan
|
|
63
|
+
*/
|
|
64
|
+
getPlanDir(sessionId) {
|
|
65
|
+
return this.resolveSessionDir(sessionId);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the path to the plan content file.
|
|
69
|
+
* Public accessor for tools that need to display the path.
|
|
70
|
+
*/
|
|
71
|
+
getPlanPath(sessionId) {
|
|
72
|
+
return path.join(this.getPlanDir(sessionId), PLAN_FILENAME);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Gets the path to the plan metadata file
|
|
76
|
+
*/
|
|
77
|
+
getMetaPath(sessionId) {
|
|
78
|
+
return path.join(this.getPlanDir(sessionId), META_FILENAME);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Checks if a plan exists for the given session
|
|
82
|
+
*/
|
|
83
|
+
async exists(sessionId) {
|
|
84
|
+
const planPath = this.getPlanPath(sessionId);
|
|
85
|
+
return (0, import_node_fs.existsSync)(planPath);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Creates a new plan for the session
|
|
89
|
+
*
|
|
90
|
+
* @throws PlanError.planAlreadyExists if plan already exists
|
|
91
|
+
* @throws PlanError.storageError on filesystem errors
|
|
92
|
+
*/
|
|
93
|
+
async create(sessionId, content, options) {
|
|
94
|
+
if (await this.exists(sessionId)) {
|
|
95
|
+
throw import_errors.PlanError.planAlreadyExists(sessionId);
|
|
96
|
+
}
|
|
97
|
+
const planDir = this.getPlanDir(sessionId);
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
const meta = {
|
|
100
|
+
sessionId,
|
|
101
|
+
status: "draft",
|
|
102
|
+
title: options?.title,
|
|
103
|
+
createdAt: now,
|
|
104
|
+
updatedAt: now
|
|
105
|
+
};
|
|
106
|
+
try {
|
|
107
|
+
await fs.mkdir(planDir, { recursive: true });
|
|
108
|
+
await Promise.all([
|
|
109
|
+
fs.writeFile(this.getPlanPath(sessionId), content, "utf-8"),
|
|
110
|
+
fs.writeFile(this.getMetaPath(sessionId), JSON.stringify(meta, null, 2), "utf-8")
|
|
111
|
+
]);
|
|
112
|
+
this.logger.debug(`Created plan for session ${sessionId}`);
|
|
113
|
+
return { content, meta };
|
|
114
|
+
} catch (error) {
|
|
115
|
+
throw import_errors.PlanError.storageError("create", sessionId, error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Reads the plan for the given session
|
|
120
|
+
*
|
|
121
|
+
* @returns The plan or null if not found
|
|
122
|
+
*/
|
|
123
|
+
async read(sessionId) {
|
|
124
|
+
if (!await this.exists(sessionId)) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
const [content, metaContent] = await Promise.all([
|
|
129
|
+
fs.readFile(this.getPlanPath(sessionId), "utf-8"),
|
|
130
|
+
fs.readFile(this.getMetaPath(sessionId), "utf-8")
|
|
131
|
+
]);
|
|
132
|
+
const metaParsed = JSON.parse(metaContent);
|
|
133
|
+
const metaResult = import_types.PlanMetaSchema.safeParse(metaParsed);
|
|
134
|
+
if (!metaResult.success) {
|
|
135
|
+
this.logger.warn(`Invalid plan metadata for session ${sessionId}, using defaults`);
|
|
136
|
+
return {
|
|
137
|
+
content,
|
|
138
|
+
meta: {
|
|
139
|
+
sessionId,
|
|
140
|
+
status: "draft",
|
|
141
|
+
createdAt: Date.now(),
|
|
142
|
+
updatedAt: Date.now()
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return { content, meta: metaResult.data };
|
|
147
|
+
} catch (error) {
|
|
148
|
+
const err = error;
|
|
149
|
+
if (err.code === "ENOENT") {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if (error instanceof SyntaxError) {
|
|
153
|
+
this.logger.error(`Failed to read plan for session ${sessionId}: ${error.message}`);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
this.logger.error(
|
|
157
|
+
`Failed to read plan for session ${sessionId}: ${err.message ?? String(err)}`
|
|
158
|
+
);
|
|
159
|
+
throw import_errors.PlanError.storageError("read", sessionId, err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Updates the plan content for the given session
|
|
164
|
+
*
|
|
165
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
166
|
+
* @throws PlanError.storageError on filesystem errors
|
|
167
|
+
*/
|
|
168
|
+
async update(sessionId, content) {
|
|
169
|
+
const existing = await this.read(sessionId);
|
|
170
|
+
if (!existing) {
|
|
171
|
+
throw import_errors.PlanError.planNotFound(sessionId);
|
|
172
|
+
}
|
|
173
|
+
const oldContent = existing.content;
|
|
174
|
+
const now = Date.now();
|
|
175
|
+
const updatedMeta = {
|
|
176
|
+
...existing.meta,
|
|
177
|
+
updatedAt: now
|
|
178
|
+
};
|
|
179
|
+
try {
|
|
180
|
+
await Promise.all([
|
|
181
|
+
fs.writeFile(this.getPlanPath(sessionId), content, "utf-8"),
|
|
182
|
+
fs.writeFile(
|
|
183
|
+
this.getMetaPath(sessionId),
|
|
184
|
+
JSON.stringify(updatedMeta, null, 2),
|
|
185
|
+
"utf-8"
|
|
186
|
+
)
|
|
187
|
+
]);
|
|
188
|
+
this.logger.debug(`Updated plan for session ${sessionId}`);
|
|
189
|
+
return {
|
|
190
|
+
oldContent,
|
|
191
|
+
newContent: content,
|
|
192
|
+
meta: updatedMeta
|
|
193
|
+
};
|
|
194
|
+
} catch (error) {
|
|
195
|
+
throw import_errors.PlanError.storageError("update", sessionId, error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Updates the plan metadata (status, title)
|
|
200
|
+
*
|
|
201
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
202
|
+
* @throws PlanError.storageError on filesystem errors
|
|
203
|
+
*/
|
|
204
|
+
async updateMeta(sessionId, updates) {
|
|
205
|
+
const existing = await this.read(sessionId);
|
|
206
|
+
if (!existing) {
|
|
207
|
+
throw import_errors.PlanError.planNotFound(sessionId);
|
|
208
|
+
}
|
|
209
|
+
const updatedMeta = {
|
|
210
|
+
...existing.meta,
|
|
211
|
+
...updates,
|
|
212
|
+
updatedAt: Date.now()
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
await fs.writeFile(
|
|
216
|
+
this.getMetaPath(sessionId),
|
|
217
|
+
JSON.stringify(updatedMeta, null, 2),
|
|
218
|
+
"utf-8"
|
|
219
|
+
);
|
|
220
|
+
this.logger.debug(`Updated plan metadata for session ${sessionId}`);
|
|
221
|
+
return updatedMeta;
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw import_errors.PlanError.storageError("update metadata", sessionId, error);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Deletes the plan for the given session
|
|
228
|
+
*
|
|
229
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
230
|
+
* @throws PlanError.storageError on filesystem errors
|
|
231
|
+
*/
|
|
232
|
+
async delete(sessionId) {
|
|
233
|
+
if (!await this.exists(sessionId)) {
|
|
234
|
+
throw import_errors.PlanError.planNotFound(sessionId);
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
await fs.rm(this.getPlanDir(sessionId), { recursive: true, force: true });
|
|
238
|
+
this.logger.debug(`Deleted plan for session ${sessionId}`);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
throw import_errors.PlanError.storageError("delete", sessionId, error);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
245
|
+
0 && (module.exports = {
|
|
246
|
+
PlanService
|
|
247
|
+
});
|
package/dist/plan-service.js
CHANGED
|
@@ -1,227 +1,213 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
basePath;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { PlanMetaSchema } from "./types.js";
|
|
5
|
+
import { PlanError } from "./errors.js";
|
|
6
|
+
const PLAN_FILENAME = "plan.md";
|
|
7
|
+
const META_FILENAME = "plan-meta.json";
|
|
8
|
+
class PlanService {
|
|
9
|
+
basePath;
|
|
10
|
+
logger;
|
|
11
|
+
constructor(options, logger) {
|
|
12
|
+
this.basePath = options.basePath;
|
|
13
|
+
this.logger = logger;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Resolves and validates a session directory path.
|
|
17
|
+
* Prevents path traversal attacks by ensuring the resolved path stays within basePath.
|
|
18
|
+
*/
|
|
19
|
+
resolveSessionDir(sessionId) {
|
|
20
|
+
const base = path.resolve(this.basePath);
|
|
21
|
+
const resolved = path.resolve(base, sessionId);
|
|
22
|
+
const rel = path.relative(base, resolved);
|
|
23
|
+
if (rel.startsWith("..") || path.isAbsolute(rel)) {
|
|
24
|
+
throw PlanError.invalidSessionId(sessionId);
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
return resolved;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Gets the directory path for a session's plan
|
|
30
|
+
*/
|
|
31
|
+
getPlanDir(sessionId) {
|
|
32
|
+
return this.resolveSessionDir(sessionId);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Gets the path to the plan content file.
|
|
36
|
+
* Public accessor for tools that need to display the path.
|
|
37
|
+
*/
|
|
38
|
+
getPlanPath(sessionId) {
|
|
39
|
+
return path.join(this.getPlanDir(sessionId), PLAN_FILENAME);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Gets the path to the plan metadata file
|
|
43
|
+
*/
|
|
44
|
+
getMetaPath(sessionId) {
|
|
45
|
+
return path.join(this.getPlanDir(sessionId), META_FILENAME);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Checks if a plan exists for the given session
|
|
49
|
+
*/
|
|
50
|
+
async exists(sessionId) {
|
|
51
|
+
const planPath = this.getPlanPath(sessionId);
|
|
52
|
+
return existsSync(planPath);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Creates a new plan for the session
|
|
56
|
+
*
|
|
57
|
+
* @throws PlanError.planAlreadyExists if plan already exists
|
|
58
|
+
* @throws PlanError.storageError on filesystem errors
|
|
59
|
+
*/
|
|
60
|
+
async create(sessionId, content, options) {
|
|
61
|
+
if (await this.exists(sessionId)) {
|
|
62
|
+
throw PlanError.planAlreadyExists(sessionId);
|
|
39
63
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
64
|
+
const planDir = this.getPlanDir(sessionId);
|
|
65
|
+
const now = Date.now();
|
|
66
|
+
const meta = {
|
|
67
|
+
sessionId,
|
|
68
|
+
status: "draft",
|
|
69
|
+
title: options?.title,
|
|
70
|
+
createdAt: now,
|
|
71
|
+
updatedAt: now
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
await fs.mkdir(planDir, { recursive: true });
|
|
75
|
+
await Promise.all([
|
|
76
|
+
fs.writeFile(this.getPlanPath(sessionId), content, "utf-8"),
|
|
77
|
+
fs.writeFile(this.getMetaPath(sessionId), JSON.stringify(meta, null, 2), "utf-8")
|
|
78
|
+
]);
|
|
79
|
+
this.logger.debug(`Created plan for session ${sessionId}`);
|
|
80
|
+
return { content, meta };
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw PlanError.storageError("create", sessionId, error);
|
|
45
83
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Reads the plan for the given session
|
|
87
|
+
*
|
|
88
|
+
* @returns The plan or null if not found
|
|
89
|
+
*/
|
|
90
|
+
async read(sessionId) {
|
|
91
|
+
if (!await this.exists(sessionId)) {
|
|
92
|
+
return null;
|
|
52
93
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Creates a new plan for the session
|
|
68
|
-
*
|
|
69
|
-
* @throws PlanError.planAlreadyExists if plan already exists
|
|
70
|
-
* @throws PlanError.storageError on filesystem errors
|
|
71
|
-
*/
|
|
72
|
-
async create(sessionId, content, options) {
|
|
73
|
-
// Check if plan already exists
|
|
74
|
-
if (await this.exists(sessionId)) {
|
|
75
|
-
throw PlanError.planAlreadyExists(sessionId);
|
|
76
|
-
}
|
|
77
|
-
const planDir = this.getPlanDir(sessionId);
|
|
78
|
-
const now = Date.now();
|
|
79
|
-
// Create metadata
|
|
80
|
-
const meta = {
|
|
94
|
+
try {
|
|
95
|
+
const [content, metaContent] = await Promise.all([
|
|
96
|
+
fs.readFile(this.getPlanPath(sessionId), "utf-8"),
|
|
97
|
+
fs.readFile(this.getMetaPath(sessionId), "utf-8")
|
|
98
|
+
]);
|
|
99
|
+
const metaParsed = JSON.parse(metaContent);
|
|
100
|
+
const metaResult = PlanMetaSchema.safeParse(metaParsed);
|
|
101
|
+
if (!metaResult.success) {
|
|
102
|
+
this.logger.warn(`Invalid plan metadata for session ${sessionId}, using defaults`);
|
|
103
|
+
return {
|
|
104
|
+
content,
|
|
105
|
+
meta: {
|
|
81
106
|
sessionId,
|
|
82
|
-
status:
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
107
|
+
status: "draft",
|
|
108
|
+
createdAt: Date.now(),
|
|
109
|
+
updatedAt: Date.now()
|
|
110
|
+
}
|
|
86
111
|
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
112
|
+
}
|
|
113
|
+
return { content, meta: metaResult.data };
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const err = error;
|
|
116
|
+
if (err.code === "ENOENT") {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
if (error instanceof SyntaxError) {
|
|
120
|
+
this.logger.error(`Failed to read plan for session ${sessionId}: ${error.message}`);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
this.logger.error(
|
|
124
|
+
`Failed to read plan for session ${sessionId}: ${err.message ?? String(err)}`
|
|
125
|
+
);
|
|
126
|
+
throw PlanError.storageError("read", sessionId, err);
|
|
101
127
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
fs.readFile(this.getPlanPath(sessionId), 'utf-8'),
|
|
114
|
-
fs.readFile(this.getMetaPath(sessionId), 'utf-8'),
|
|
115
|
-
]);
|
|
116
|
-
const metaParsed = JSON.parse(metaContent);
|
|
117
|
-
const metaResult = PlanMetaSchema.safeParse(metaParsed);
|
|
118
|
-
if (!metaResult.success) {
|
|
119
|
-
this.logger.warn(`Invalid plan metadata for session ${sessionId}, using defaults`);
|
|
120
|
-
// Return with minimal metadata if parsing fails
|
|
121
|
-
return {
|
|
122
|
-
content,
|
|
123
|
-
meta: {
|
|
124
|
-
sessionId,
|
|
125
|
-
status: 'draft',
|
|
126
|
-
createdAt: Date.now(),
|
|
127
|
-
updatedAt: Date.now(),
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
return { content, meta: metaResult.data };
|
|
132
|
-
}
|
|
133
|
-
catch (error) {
|
|
134
|
-
const err = error;
|
|
135
|
-
// ENOENT means file doesn't exist - return null (expected case)
|
|
136
|
-
if (err.code === 'ENOENT') {
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
// JSON parse errors (SyntaxError) mean corrupted data - treat as not found
|
|
140
|
-
// but log for debugging
|
|
141
|
-
if (error instanceof SyntaxError) {
|
|
142
|
-
this.logger.error(`Failed to read plan for session ${sessionId}: ${error.message}`);
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
// For real I/O errors (permission denied, disk issues), throw to surface the issue
|
|
146
|
-
this.logger.error(`Failed to read plan for session ${sessionId}: ${err.message ?? String(err)}`);
|
|
147
|
-
throw PlanError.storageError('read', sessionId, err);
|
|
148
|
-
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Updates the plan content for the given session
|
|
131
|
+
*
|
|
132
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
133
|
+
* @throws PlanError.storageError on filesystem errors
|
|
134
|
+
*/
|
|
135
|
+
async update(sessionId, content) {
|
|
136
|
+
const existing = await this.read(sessionId);
|
|
137
|
+
if (!existing) {
|
|
138
|
+
throw PlanError.planNotFound(sessionId);
|
|
149
139
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
this.logger.debug(`Updated plan for session ${sessionId}`);
|
|
174
|
-
return {
|
|
175
|
-
oldContent,
|
|
176
|
-
newContent: content,
|
|
177
|
-
meta: updatedMeta,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
throw PlanError.storageError('update', sessionId, error);
|
|
182
|
-
}
|
|
140
|
+
const oldContent = existing.content;
|
|
141
|
+
const now = Date.now();
|
|
142
|
+
const updatedMeta = {
|
|
143
|
+
...existing.meta,
|
|
144
|
+
updatedAt: now
|
|
145
|
+
};
|
|
146
|
+
try {
|
|
147
|
+
await Promise.all([
|
|
148
|
+
fs.writeFile(this.getPlanPath(sessionId), content, "utf-8"),
|
|
149
|
+
fs.writeFile(
|
|
150
|
+
this.getMetaPath(sessionId),
|
|
151
|
+
JSON.stringify(updatedMeta, null, 2),
|
|
152
|
+
"utf-8"
|
|
153
|
+
)
|
|
154
|
+
]);
|
|
155
|
+
this.logger.debug(`Updated plan for session ${sessionId}`);
|
|
156
|
+
return {
|
|
157
|
+
oldContent,
|
|
158
|
+
newContent: content,
|
|
159
|
+
meta: updatedMeta
|
|
160
|
+
};
|
|
161
|
+
} catch (error) {
|
|
162
|
+
throw PlanError.storageError("update", sessionId, error);
|
|
183
163
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Updates the plan metadata (status, title)
|
|
167
|
+
*
|
|
168
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
169
|
+
* @throws PlanError.storageError on filesystem errors
|
|
170
|
+
*/
|
|
171
|
+
async updateMeta(sessionId, updates) {
|
|
172
|
+
const existing = await this.read(sessionId);
|
|
173
|
+
if (!existing) {
|
|
174
|
+
throw PlanError.planNotFound(sessionId);
|
|
175
|
+
}
|
|
176
|
+
const updatedMeta = {
|
|
177
|
+
...existing.meta,
|
|
178
|
+
...updates,
|
|
179
|
+
updatedAt: Date.now()
|
|
180
|
+
};
|
|
181
|
+
try {
|
|
182
|
+
await fs.writeFile(
|
|
183
|
+
this.getMetaPath(sessionId),
|
|
184
|
+
JSON.stringify(updatedMeta, null, 2),
|
|
185
|
+
"utf-8"
|
|
186
|
+
);
|
|
187
|
+
this.logger.debug(`Updated plan metadata for session ${sessionId}`);
|
|
188
|
+
return updatedMeta;
|
|
189
|
+
} catch (error) {
|
|
190
|
+
throw PlanError.storageError("update metadata", sessionId, error);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Deletes the plan for the given session
|
|
195
|
+
*
|
|
196
|
+
* @throws PlanError.planNotFound if plan doesn't exist
|
|
197
|
+
* @throws PlanError.storageError on filesystem errors
|
|
198
|
+
*/
|
|
199
|
+
async delete(sessionId) {
|
|
200
|
+
if (!await this.exists(sessionId)) {
|
|
201
|
+
throw PlanError.planNotFound(sessionId);
|
|
208
202
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
*/
|
|
215
|
-
async delete(sessionId) {
|
|
216
|
-
if (!(await this.exists(sessionId))) {
|
|
217
|
-
throw PlanError.planNotFound(sessionId);
|
|
218
|
-
}
|
|
219
|
-
try {
|
|
220
|
-
await fs.rm(this.getPlanDir(sessionId), { recursive: true, force: true });
|
|
221
|
-
this.logger.debug(`Deleted plan for session ${sessionId}`);
|
|
222
|
-
}
|
|
223
|
-
catch (error) {
|
|
224
|
-
throw PlanError.storageError('delete', sessionId, error);
|
|
225
|
-
}
|
|
203
|
+
try {
|
|
204
|
+
await fs.rm(this.getPlanDir(sessionId), { recursive: true, force: true });
|
|
205
|
+
this.logger.debug(`Deleted plan for session ${sessionId}`);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
throw PlanError.storageError("delete", sessionId, error);
|
|
226
208
|
}
|
|
209
|
+
}
|
|
227
210
|
}
|
|
211
|
+
export {
|
|
212
|
+
PlanService
|
|
213
|
+
};
|