@narumitw/pi-goal 0.54.3 → 0.54.5
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 +117 -190
- package/dist/chunks/{chunk-QWIUWKRW.js → chunk-VJ5LR26S.js} +57 -77
- package/dist/chunks/chunk-VJ5LR26S.js.map +7 -0
- package/dist/chunks/{chunk-UQRRW4CY.js → chunk-WQIA35U2.js} +6 -5
- package/dist/chunks/chunk-WQIA35U2.js.map +7 -0
- package/dist/chunks/{menu-WEFDREGC.js → menu-JMGWQZIT.js} +7 -22
- package/dist/chunks/menu-JMGWQZIT.js.map +7 -0
- package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-JG3PFRB5.js} +6 -18
- package/dist/chunks/settings-ui-JG3PFRB5.js.map +7 -0
- package/dist/index.ts +111 -126
- package/dist/index.ts.map +2 -2
- package/docs/managing-goals.md +47 -0
- package/docs/settings.md +91 -0
- package/package.json +55 -58
- package/src/accounting.ts +62 -66
- package/src/command-registration.ts +104 -102
- package/src/command.ts +101 -112
- package/src/commands.ts +465 -501
- package/src/errors.ts +127 -122
- package/src/goal-contract.ts +64 -64
- package/src/goal.ts +25 -25
- package/src/lifecycle.ts +615 -628
- package/src/markers.ts +6 -6
- package/src/menu.ts +565 -591
- package/src/persistence.ts +177 -192
- package/src/prompts.ts +53 -67
- package/src/run-protocol.ts +299 -325
- package/src/runtime.ts +1422 -1495
- package/src/safety.ts +51 -51
- package/src/settings-ui.ts +366 -393
- package/src/settings.ts +134 -147
- package/src/tool-policy.ts +6 -6
- package/src/tools.ts +371 -347
- package/src/wait.ts +47 -51
- package/src/workflow-mutex.ts +68 -68
- package/dist/chunks/chunk-QWIUWKRW.js.map +0 -7
- package/dist/chunks/chunk-UQRRW4CY.js.map +0 -7
- package/dist/chunks/menu-WEFDREGC.js.map +0 -7
- package/dist/chunks/settings-ui-MVYTTBHW.js.map +0 -7
package/src/safety.ts
CHANGED
|
@@ -2,77 +2,77 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import type { ActiveGoal } from "./persistence.js";
|
|
3
3
|
|
|
4
4
|
export interface ToolFreeRepeatState {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
toolFreeRepeatCount: number;
|
|
6
|
+
lastToolFreeOutputFingerprint?: string;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export function queueGoalSafetyReset(goal: ActiveGoal): ActiveGoal {
|
|
10
|
-
|
|
10
|
+
return { ...goal, safetyResetPending: true };
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export function resetGoalSafetyEpoch(goal: ActiveGoal): ActiveGoal {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
return {
|
|
15
|
+
...goal,
|
|
16
|
+
automaticModelTurns: 0,
|
|
17
|
+
toolFreeRepeatCount: 0,
|
|
18
|
+
lastToolFreeOutputFingerprint: undefined,
|
|
19
|
+
safetyPauseCause: undefined,
|
|
20
|
+
safetyResetPending: undefined,
|
|
21
|
+
};
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function nextToolFreeRepeatState(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
current: ToolFreeRepeatState,
|
|
26
|
+
messages: readonly unknown[],
|
|
27
|
+
toolAttempted: boolean,
|
|
28
28
|
): ToolFreeRepeatState {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
if (toolAttempted) return { toolFreeRepeatCount: 0 };
|
|
30
|
+
const fingerprint = fingerprintVisibleAssistantOutput(messages);
|
|
31
|
+
return {
|
|
32
|
+
toolFreeRepeatCount:
|
|
33
|
+
fingerprint === current.lastToolFreeOutputFingerprint
|
|
34
|
+
? Math.min(Number.MAX_SAFE_INTEGER, current.toolFreeRepeatCount + 1)
|
|
35
|
+
: 1,
|
|
36
|
+
lastToolFreeOutputFingerprint: fingerprint,
|
|
37
|
+
};
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
export function hasAssistantToolCall(messages: readonly unknown[]) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
for (const message of messages) {
|
|
42
|
+
if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (message.content.some((block) => isRecord(block) && block.type === "toolCall")) return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function fingerprintVisibleAssistantOutput(messages: readonly unknown[]) {
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
const normalized = normalizeVisibleAssistantOutput(messages);
|
|
52
|
+
return createHash("sha256").update(normalized, "utf8").digest("hex");
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export function normalizeVisibleAssistantOutput(messages: readonly unknown[]) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
56
|
+
const text: string[] = [];
|
|
57
|
+
for (const message of messages) {
|
|
58
|
+
if (!isRecord(message) || message.role !== "assistant" || !Array.isArray(message.content)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const block of message.content) {
|
|
62
|
+
if (!isRecord(block) || block.type !== "text" || typeof block.text !== "string") continue;
|
|
63
|
+
text.push(block.text);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const normalized = text
|
|
67
|
+
.join("\n")
|
|
68
|
+
.normalize("NFKC")
|
|
69
|
+
.toLowerCase()
|
|
70
|
+
.replace(/\s+/gu, " ")
|
|
71
|
+
.replace(/[\p{Cc}\p{Cf}]/gu, "")
|
|
72
|
+
.trim();
|
|
73
|
+
return normalized === "" || /^[\p{P}\s]+$/u.test(normalized) ? "" : normalized;
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
77
|
-
|
|
77
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
78
78
|
}
|