@narumitw/pi-goal 0.28.0 → 0.34.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/README.md +24 -20
- package/package.json +10 -5
- package/src/accounting.ts +1 -3
- package/src/commands.ts +68 -5
- package/src/goal.ts +25 -14
- package/src/menu.ts +436 -0
- package/src/queue.ts +25 -1
- package/src/runtime.ts +87 -8
- package/src/settings-ui.ts +762 -0
- package/src/settings.ts +51 -42
package/src/settings.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { basename, dirname, join } from "node:path";
|
|
4
4
|
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
5
5
|
|
|
@@ -23,24 +23,20 @@ export interface GoalSettings {
|
|
|
23
23
|
export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
|
|
24
24
|
toolVisibility: "always",
|
|
25
25
|
experimental: { goals: false },
|
|
26
|
-
continuationLimits: { automaticTurns:
|
|
26
|
+
continuationLimits: { automaticTurns: null, noProgressTurns: 3 },
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
export const DEFAULT_GOAL_SETTINGS_DOCUMENT = `${JSON.stringify(DEFAULT_GOAL_SETTINGS, null, 2)}\n`;
|
|
30
|
-
|
|
31
29
|
export type GoalSettingsLoadResult =
|
|
32
30
|
| { kind: "missing" }
|
|
33
31
|
| { kind: "invalid"; reason: string }
|
|
34
32
|
| { kind: "loaded"; settings: GoalSettings };
|
|
35
33
|
|
|
36
|
-
export type
|
|
37
|
-
| Exclude<GoalSettingsLoadResult, { kind: "missing" }>
|
|
38
|
-
| { kind: "create-failed"; reason: string };
|
|
34
|
+
export type GoalSettingsLoadIssue = Extract<GoalSettingsLoadResult, { kind: "invalid" }>;
|
|
39
35
|
|
|
40
|
-
interface
|
|
36
|
+
interface GoalSettingsSaveFileSystem {
|
|
41
37
|
mkdirSync: typeof mkdirSync;
|
|
42
38
|
writeFileSync: typeof writeFileSync;
|
|
43
|
-
|
|
39
|
+
renameSync: typeof renameSync;
|
|
44
40
|
rmSync: typeof rmSync;
|
|
45
41
|
}
|
|
46
42
|
|
|
@@ -109,47 +105,58 @@ function normalizeContinuationLimit(
|
|
|
109
105
|
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
110
106
|
}
|
|
111
107
|
|
|
112
|
-
export function
|
|
108
|
+
export function saveGoalSettings(
|
|
109
|
+
settings: GoalSettings,
|
|
113
110
|
settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
|
|
114
|
-
overrides: Partial<
|
|
115
|
-
)
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
111
|
+
overrides: Partial<GoalSettingsSaveFileSystem> = {},
|
|
112
|
+
) {
|
|
113
|
+
const normalized = normalizeGoalSettings(settings);
|
|
114
|
+
if (!normalized) throw new Error("Refusing to save invalid pi-goal settings.");
|
|
115
|
+
|
|
116
|
+
let raw: Record<string, unknown> = {};
|
|
117
|
+
try {
|
|
118
|
+
const contents = readFileSync(settingsPath, "utf8");
|
|
119
|
+
const parsed = JSON.parse(contents) as unknown;
|
|
120
|
+
if (!normalizeGoalSettings(parsed)) {
|
|
121
|
+
throw new Error(`${settingsPath}: invalid settings shape`);
|
|
122
|
+
}
|
|
123
|
+
raw = ownRecord(parsed) ?? {};
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (!isNodeError(error) || error.code !== "ENOENT") {
|
|
126
|
+
throw new Error(`Cannot save invalid settings file: ${formatError(error)}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
118
129
|
|
|
119
|
-
const
|
|
130
|
+
const experimental = ownRecord(raw.experimental) ?? {};
|
|
131
|
+
const continuationLimits = ownRecord(raw.continuationLimits) ?? {};
|
|
132
|
+
const document = `${JSON.stringify(
|
|
133
|
+
{
|
|
134
|
+
...raw,
|
|
135
|
+
toolVisibility: normalized.toolVisibility,
|
|
136
|
+
experimental: { ...experimental, goals: normalized.experimental.goals },
|
|
137
|
+
continuationLimits: {
|
|
138
|
+
...continuationLimits,
|
|
139
|
+
automaticTurns: normalized.continuationLimits.automaticTurns,
|
|
140
|
+
noProgressTurns: normalized.continuationLimits.noProgressTurns,
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
null,
|
|
144
|
+
2,
|
|
145
|
+
)}\n`;
|
|
146
|
+
const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
|
|
120
147
|
const temporaryPath = join(
|
|
121
148
|
dirname(settingsPath),
|
|
122
149
|
`.${basename(settingsPath)}.${randomUUID()}.tmp`,
|
|
123
150
|
);
|
|
124
151
|
try {
|
|
125
152
|
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
126
|
-
fs.writeFileSync(temporaryPath,
|
|
127
|
-
|
|
128
|
-
flag: "wx",
|
|
129
|
-
});
|
|
130
|
-
try {
|
|
131
|
-
fs.linkSync(temporaryPath, settingsPath);
|
|
132
|
-
} catch (error) {
|
|
133
|
-
if (!isAlreadyExistsError(error)) throw error;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const published = readGoalSettings(settingsPath);
|
|
137
|
-
return published.kind === "missing"
|
|
138
|
-
? {
|
|
139
|
-
kind: "create-failed",
|
|
140
|
-
reason: `${settingsPath}: settings file disappeared during initialization`,
|
|
141
|
-
}
|
|
142
|
-
: published;
|
|
143
|
-
} catch (error) {
|
|
144
|
-
return {
|
|
145
|
-
kind: "create-failed",
|
|
146
|
-
reason: `${settingsPath}: ${formatError(error)}`,
|
|
147
|
-
};
|
|
153
|
+
fs.writeFileSync(temporaryPath, document, { encoding: "utf8", flag: "wx" });
|
|
154
|
+
fs.renameSync(temporaryPath, settingsPath);
|
|
148
155
|
} finally {
|
|
149
156
|
try {
|
|
150
157
|
fs.rmSync(temporaryPath, { force: true });
|
|
151
158
|
} catch {
|
|
152
|
-
// Best-effort cleanup must not replace the
|
|
159
|
+
// Best-effort cleanup must not replace the save result.
|
|
153
160
|
}
|
|
154
161
|
}
|
|
155
162
|
}
|
|
@@ -175,12 +182,14 @@ export function readGoalSettings(
|
|
|
175
182
|
}
|
|
176
183
|
}
|
|
177
184
|
|
|
178
|
-
function
|
|
179
|
-
return
|
|
185
|
+
function ownRecord(value: unknown): Record<string, unknown> | undefined {
|
|
186
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
187
|
+
? (value as Record<string, unknown>)
|
|
188
|
+
: undefined;
|
|
180
189
|
}
|
|
181
190
|
|
|
182
|
-
function
|
|
183
|
-
return
|
|
191
|
+
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
192
|
+
return error instanceof Error && "code" in error;
|
|
184
193
|
}
|
|
185
194
|
|
|
186
195
|
function formatError(error: unknown) {
|