@narumitw/pi-goal 0.15.0 → 0.16.0

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.
@@ -0,0 +1,84 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { getAgentDir } from "@earendil-works/pi-coding-agent";
4
+
5
+ export const GOAL_SETTINGS_FILE = "pi-goal.json";
6
+ export const GOAL_TOOL_VISIBILITIES = ["always", "after-first-goal"] as const;
7
+
8
+ export type GoalToolVisibility = (typeof GOAL_TOOL_VISIBILITIES)[number];
9
+
10
+ export interface GoalSettings {
11
+ toolVisibility: GoalToolVisibility;
12
+ experimental: {
13
+ goals: boolean;
14
+ };
15
+ }
16
+
17
+ export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
18
+ toolVisibility: "always",
19
+ experimental: { goals: false },
20
+ };
21
+
22
+ export type GoalSettingsLoadResult =
23
+ | { kind: "missing" }
24
+ | { kind: "invalid"; reason: string }
25
+ | { kind: "loaded"; settings: GoalSettings };
26
+
27
+ export function normalizeGoalSettings(value: unknown): GoalSettings | undefined {
28
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined;
29
+ const toolVisibility = Object.hasOwn(value, "toolVisibility")
30
+ ? Reflect.get(value, "toolVisibility")
31
+ : DEFAULT_GOAL_SETTINGS.toolVisibility;
32
+ if (!GOAL_TOOL_VISIBILITIES.includes(toolVisibility as GoalToolVisibility)) return undefined;
33
+
34
+ const experimentalValue = Object.hasOwn(value, "experimental")
35
+ ? Reflect.get(value, "experimental")
36
+ : undefined;
37
+ if (
38
+ experimentalValue !== undefined &&
39
+ (typeof experimentalValue !== "object" ||
40
+ experimentalValue === null ||
41
+ Array.isArray(experimentalValue))
42
+ ) {
43
+ return undefined;
44
+ }
45
+ const goals =
46
+ experimentalValue && Object.hasOwn(experimentalValue, "goals")
47
+ ? Reflect.get(experimentalValue, "goals")
48
+ : DEFAULT_GOAL_SETTINGS.experimental.goals;
49
+ if (typeof goals !== "boolean") return undefined;
50
+
51
+ return {
52
+ toolVisibility: toolVisibility as GoalToolVisibility,
53
+ experimental: { goals },
54
+ };
55
+ }
56
+
57
+ export function readGoalSettings(
58
+ settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
59
+ ): GoalSettingsLoadResult {
60
+ let contents: string;
61
+ try {
62
+ contents = readFileSync(settingsPath, "utf8");
63
+ } catch (error: unknown) {
64
+ if (isNodeError(error) && error.code === "ENOENT") return { kind: "missing" };
65
+ return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
66
+ }
67
+
68
+ try {
69
+ const settings = normalizeGoalSettings(JSON.parse(contents) as unknown);
70
+ return settings
71
+ ? { kind: "loaded", settings }
72
+ : { kind: "invalid", reason: `${settingsPath}: invalid settings shape` };
73
+ } catch (error: unknown) {
74
+ return { kind: "invalid", reason: `${settingsPath}: ${formatError(error)}` };
75
+ }
76
+ }
77
+
78
+ function isNodeError(error: unknown): error is NodeJS.ErrnoException {
79
+ return error instanceof Error && "code" in error;
80
+ }
81
+
82
+ function formatError(error: unknown) {
83
+ return error instanceof Error ? error.message : String(error);
84
+ }