@narumitw/pi-goal 0.20.0 → 0.27.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.
package/src/safety.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ export interface ToolFreeRepeatState {
4
+ toolFreeRepeatCount: number;
5
+ lastToolFreeOutputFingerprint?: string;
6
+ }
7
+
8
+ export function nextToolFreeRepeatState(
9
+ current: ToolFreeRepeatState,
10
+ messages: readonly unknown[],
11
+ toolAttempted: boolean,
12
+ ): ToolFreeRepeatState {
13
+ if (toolAttempted) return { toolFreeRepeatCount: 0 };
14
+ const fingerprint = fingerprintVisibleAssistantOutput(messages);
15
+ return {
16
+ toolFreeRepeatCount:
17
+ fingerprint === current.lastToolFreeOutputFingerprint
18
+ ? Math.min(Number.MAX_SAFE_INTEGER, current.toolFreeRepeatCount + 1)
19
+ : 1,
20
+ lastToolFreeOutputFingerprint: fingerprint,
21
+ };
22
+ }
23
+
24
+ export function hasAssistantToolCall(messages: readonly unknown[]) {
25
+ for (const message of messages) {
26
+ if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
27
+ continue;
28
+ }
29
+ if (message.content.some((block) => isRecord(block) && block.type === "toolCall")) return true;
30
+ }
31
+ return false;
32
+ }
33
+
34
+ export function fingerprintVisibleAssistantOutput(messages: readonly unknown[]) {
35
+ const normalized = normalizeVisibleAssistantOutput(messages);
36
+ return createHash("sha256").update(normalized, "utf8").digest("hex");
37
+ }
38
+
39
+ export function normalizeVisibleAssistantOutput(messages: readonly unknown[]) {
40
+ const text: string[] = [];
41
+ for (const message of messages) {
42
+ if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
43
+ continue;
44
+ }
45
+ for (const block of message.content) {
46
+ if (!isRecord(block) || block.type !== "text" || typeof block.text !== "string") continue;
47
+ text.push(block.text);
48
+ }
49
+ }
50
+ const normalized = text
51
+ .join("\n")
52
+ .normalize("NFKC")
53
+ .toLowerCase()
54
+ .replace(/\s+/gu, " ")
55
+ .replace(/[\p{Cc}\p{Cf}]/gu, "")
56
+ .trim();
57
+ return normalized === "" || /^[\p{P}\s]+$/u.test(normalized) ? "" : normalized;
58
+ }
59
+
60
+ function isRecord(value: unknown): value is Record<string, unknown> {
61
+ return typeof value === "object" && value !== null && !Array.isArray(value);
62
+ }
package/src/settings.ts CHANGED
@@ -6,17 +6,23 @@ export const GOAL_SETTINGS_FILE = "pi-goal.json";
6
6
  export const GOAL_TOOL_VISIBILITIES = ["always", "after-first-goal"] as const;
7
7
 
8
8
  export type GoalToolVisibility = (typeof GOAL_TOOL_VISIBILITIES)[number];
9
+ export type ContinuationLimit = number | null;
9
10
 
10
11
  export interface GoalSettings {
11
12
  toolVisibility: GoalToolVisibility;
12
13
  experimental: {
13
14
  goals: boolean;
14
15
  };
16
+ continuationLimits: {
17
+ automaticTurns: ContinuationLimit;
18
+ noProgressTurns: ContinuationLimit;
19
+ };
15
20
  }
16
21
 
17
22
  export const DEFAULT_GOAL_SETTINGS: GoalSettings = {
18
23
  toolVisibility: "always",
19
24
  experimental: { goals: false },
25
+ continuationLimits: { automaticTurns: 25, noProgressTurns: 3 },
20
26
  };
21
27
 
22
28
  export type GoalSettingsLoadResult =
@@ -48,12 +54,47 @@ export function normalizeGoalSettings(value: unknown): GoalSettings | undefined
48
54
  : DEFAULT_GOAL_SETTINGS.experimental.goals;
49
55
  if (typeof goals !== "boolean") return undefined;
50
56
 
57
+ const continuationLimitsValue = Object.hasOwn(value, "continuationLimits")
58
+ ? Reflect.get(value, "continuationLimits")
59
+ : undefined;
60
+ if (
61
+ continuationLimitsValue !== undefined &&
62
+ (typeof continuationLimitsValue !== "object" ||
63
+ continuationLimitsValue === null ||
64
+ Array.isArray(continuationLimitsValue))
65
+ ) {
66
+ return undefined;
67
+ }
68
+ const automaticTurns = continuationLimitsValue
69
+ ? normalizeContinuationLimit(
70
+ Reflect.get(continuationLimitsValue, "automaticTurns"),
71
+ DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns,
72
+ )
73
+ : DEFAULT_GOAL_SETTINGS.continuationLimits.automaticTurns;
74
+ const noProgressTurns = continuationLimitsValue
75
+ ? normalizeContinuationLimit(
76
+ Reflect.get(continuationLimitsValue, "noProgressTurns"),
77
+ DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns,
78
+ )
79
+ : DEFAULT_GOAL_SETTINGS.continuationLimits.noProgressTurns;
80
+ if (automaticTurns === undefined || noProgressTurns === undefined) return undefined;
81
+
51
82
  return {
52
83
  toolVisibility: toolVisibility as GoalToolVisibility,
53
84
  experimental: { goals },
85
+ continuationLimits: { automaticTurns, noProgressTurns },
54
86
  };
55
87
  }
56
88
 
89
+ function normalizeContinuationLimit(
90
+ value: unknown,
91
+ fallback: ContinuationLimit,
92
+ ): ContinuationLimit | undefined {
93
+ if (value === undefined) return fallback;
94
+ if (value === null) return null;
95
+ return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
96
+ }
97
+
57
98
  export function readGoalSettings(
58
99
  settingsPath = join(getAgentDir(), GOAL_SETTINGS_FILE),
59
100
  ): GoalSettingsLoadResult {