@narumitw/pi-goal 0.54.4 → 0.54.6
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 +41 -166
- package/dist/chunks/{chunk-UQRRW4CY.js → chunk-6T3MHA45.js} +7 -6
- package/dist/chunks/chunk-6T3MHA45.js.map +7 -0
- package/dist/chunks/{chunk-QWIUWKRW.js → chunk-WLIH47XO.js} +57 -77
- package/dist/chunks/chunk-WLIH47XO.js.map +7 -0
- package/dist/chunks/{menu-WEFDREGC.js → menu-RGCRCQJK.js} +7 -22
- package/dist/chunks/menu-RGCRCQJK.js.map +7 -0
- package/dist/chunks/{settings-ui-MVYTTBHW.js → settings-ui-BAWNTINU.js} +6 -18
- package/dist/chunks/settings-ui-BAWNTINU.js.map +7 -0
- package/dist/index.ts +92 -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 -55
- 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 +360 -371
- 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/errors.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
isContextOverflow,
|
|
3
|
+
isRetryableAssistantError,
|
|
4
|
+
type AssistantMessage as PiAssistantMessage,
|
|
5
|
+
type Usage,
|
|
6
6
|
} from "@earendil-works/pi-ai";
|
|
7
7
|
import { stripTerminalSequences } from "@earendil-works/pi-tui";
|
|
8
8
|
import { assistantUsageTokens, nonNegativeFiniteNumber } from "./accounting.js";
|
|
@@ -10,167 +10,172 @@ import { assistantUsageTokens, nonNegativeFiniteNumber } from "./accounting.js";
|
|
|
10
10
|
export type AgentStopReason = "stop" | "length" | "toolUse" | "error" | "aborted";
|
|
11
11
|
|
|
12
12
|
export interface AssistantMessageLike {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
role: "assistant";
|
|
14
|
+
stopReason?: AgentStopReason;
|
|
15
|
+
errorMessage?: string;
|
|
16
|
+
content?: PiAssistantMessage["content"];
|
|
17
|
+
api?: PiAssistantMessage["api"];
|
|
18
|
+
provider?: PiAssistantMessage["provider"];
|
|
19
|
+
model?: string;
|
|
20
|
+
usage?: Usage;
|
|
21
|
+
timestamp?: number;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
const USAGE_LIMIT_GOAL_ERROR_PATTERNS = [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
/usage[_\s-]*(?:limit|cap)|chatgpt.{0,32}usage/i,
|
|
26
|
+
/quota.{0,32}(?:reached|exceeded|exhausted|depleted)|(?:reached|exceeded|exhausted|depleted).{0,32}quota/i,
|
|
27
|
+
/insufficient[_\s-]*(?:quota|credits?)|out of credits|out of budget|available balance|payment required/i,
|
|
28
|
+
/(?:credit|balance).{0,32}(?:low|exhausted|depleted)|billing/i,
|
|
29
29
|
] as const;
|
|
30
|
-
const NON_RETRYABLE_GOAL_ERROR_RE =
|
|
31
|
-
/multi-auth rotation failed|credentials tried|unauthori[sz]ed|invalid api key/i;
|
|
30
|
+
const NON_RETRYABLE_GOAL_ERROR_RE = /multi-auth rotation failed|credentials tried|unauthori[sz]ed|invalid api key/i;
|
|
32
31
|
const RETRYABLE_GOAL_ERROR_PATTERNS = [
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
/overloaded|rate.?limit|too many requests|\b(?:429|500|502|503|504)\b|service.?unavailable|server.?error|internal.?error/i,
|
|
33
|
+
/provider.?returned.?error|you can retry your request|try your request again|please retry your request/i,
|
|
34
|
+
/network.?error|connection.?(?:error|refused|lost)|other side closed|fetch failed|upstream.?connect|reset before headers|socket hang up/i,
|
|
35
|
+
/timed? out|timeout|terminated|websocket.?(?:closed|error)|ended without|stream (?:ended|closed|terminated) before|premature (?:close|end)|incomplete (?:stream|response)|unexpected end of (?:stream|response)|http2 request did not get a response|retry delay/i,
|
|
36
|
+
/context[_\s-]*length[_\s-]*exceeded|input exceeds the context window/i,
|
|
38
37
|
] as const;
|
|
39
38
|
|
|
40
39
|
export function safeTerminalText(value: string) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
40
|
+
return [...stripTerminalSequences(value)]
|
|
41
|
+
.map((character) => {
|
|
42
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
43
|
+
if (character === "\n") return character;
|
|
44
|
+
return codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f) ? " " : character;
|
|
45
|
+
})
|
|
46
|
+
.join("")
|
|
47
|
+
.trim();
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
export function safeGoalMenuText(value: string, maxCharacters = 120) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
? sanitized
|
|
56
|
-
: `${characters.slice(0, maxCharacters).join("")}…`;
|
|
51
|
+
const sanitized = safeTerminalText(value).replace(/\s+/gu, " ").trim();
|
|
52
|
+
const characters = [...sanitized];
|
|
53
|
+
return characters.length <= maxCharacters ? sanitized : `${characters.slice(0, maxCharacters).join("")}…`;
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
export function notifyTerminal(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
57
|
+
ui: { notify: (message: string, level?: "info" | "warning" | "error") => void },
|
|
58
|
+
message: string,
|
|
59
|
+
level?: "info" | "warning" | "error",
|
|
63
60
|
) {
|
|
64
|
-
|
|
61
|
+
ui.notify(safeTerminalText(message), level);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* True when an error came from an ExtensionContext invalidated by session
|
|
66
|
+
* replacement or reload. Pi throws this from every ctx getter and pi method on
|
|
67
|
+
* the stale runner, so deferred callbacks must treat it as cancellation.
|
|
68
|
+
*/
|
|
69
|
+
export function isStaleContextError(error: unknown): boolean {
|
|
70
|
+
return (
|
|
71
|
+
error instanceof Error && error.message.includes("This extension ctx is stale after session replacement or reload")
|
|
72
|
+
);
|
|
65
73
|
}
|
|
66
74
|
|
|
67
75
|
export function formatError(error: unknown) {
|
|
68
|
-
|
|
76
|
+
return truncateNotification(error instanceof Error ? error.message : String(error));
|
|
69
77
|
}
|
|
70
78
|
|
|
71
79
|
export function truncateNotification(value: string) {
|
|
72
|
-
|
|
73
|
-
|
|
80
|
+
const safe = [...safeTerminalText(value)];
|
|
81
|
+
return safe.length > 160 ? `${safe.slice(0, 157).join("")}...` : safe.join("");
|
|
74
82
|
}
|
|
75
83
|
|
|
76
84
|
export function isUsageLimitedGoalInterruption(assistant: AssistantMessageLike) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
85
|
+
const errorMessage = assistant.errorMessage;
|
|
86
|
+
return (
|
|
87
|
+
assistant.stopReason === "error" &&
|
|
88
|
+
typeof errorMessage === "string" &&
|
|
89
|
+
USAGE_LIMIT_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(errorMessage))
|
|
90
|
+
);
|
|
83
91
|
}
|
|
84
92
|
|
|
85
93
|
export function isRetryableGoalInterruption(assistant: AssistantMessageLike) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
isRetryableAssistantError(toPiAssistantMessage(assistant)) ||
|
|
96
|
-
RETRYABLE_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(assistant.errorMessage ?? ""))
|
|
97
|
-
);
|
|
94
|
+
if (assistant.stopReason !== "error" || !assistant.errorMessage) return false;
|
|
95
|
+
if (isUsageLimitedGoalInterruption(assistant) || NON_RETRYABLE_GOAL_ERROR_RE.test(assistant.errorMessage)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return (
|
|
99
|
+
isGoalContextOverflow(assistant) ||
|
|
100
|
+
isRetryableAssistantError(toPiAssistantMessage(assistant)) ||
|
|
101
|
+
RETRYABLE_GOAL_ERROR_PATTERNS.some((pattern) => pattern.test(assistant.errorMessage ?? ""))
|
|
102
|
+
);
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
export function isGoalContextOverflow(assistant: AssistantMessageLike) {
|
|
101
|
-
|
|
106
|
+
return isContextOverflow(toPiAssistantMessage(assistant));
|
|
102
107
|
}
|
|
103
108
|
|
|
104
109
|
export function findFinalAssistantMessage(messages: unknown[]): AssistantMessageLike | undefined {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
110
|
+
for (let index = messages.length - 1; index >= 0; index--) {
|
|
111
|
+
const message = messages[index];
|
|
112
|
+
if (!message || typeof message !== "object") continue;
|
|
113
|
+
const candidate = message as Record<string, unknown>;
|
|
114
|
+
if (candidate.role !== "assistant") continue;
|
|
115
|
+
const assistant: AssistantMessageLike = {
|
|
116
|
+
role: "assistant",
|
|
117
|
+
stopReason: isAgentStopReason(candidate.stopReason) ? candidate.stopReason : undefined,
|
|
118
|
+
errorMessage: typeof candidate.errorMessage === "string" ? candidate.errorMessage : undefined,
|
|
119
|
+
};
|
|
120
|
+
if (Array.isArray(candidate.content)) {
|
|
121
|
+
assistant.content = candidate.content as PiAssistantMessage["content"];
|
|
122
|
+
}
|
|
123
|
+
if (typeof candidate.api === "string") assistant.api = candidate.api;
|
|
124
|
+
if (typeof candidate.provider === "string") assistant.provider = candidate.provider;
|
|
125
|
+
if (typeof candidate.model === "string") assistant.model = candidate.model;
|
|
126
|
+
if (typeof candidate.timestamp === "number") assistant.timestamp = candidate.timestamp;
|
|
127
|
+
const usage = normalizeUsage(candidate.usage);
|
|
128
|
+
if (usage) assistant.usage = usage;
|
|
129
|
+
return assistant;
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
127
132
|
}
|
|
128
133
|
|
|
129
134
|
function toPiAssistantMessage(assistant: AssistantMessageLike): PiAssistantMessage {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
135
|
+
return {
|
|
136
|
+
role: "assistant",
|
|
137
|
+
content: assistant.content ?? [],
|
|
138
|
+
api: assistant.api ?? "openai-responses",
|
|
139
|
+
provider: assistant.provider ?? "unknown",
|
|
140
|
+
model: assistant.model ?? "unknown",
|
|
141
|
+
usage: assistant.usage ?? zeroUsage(),
|
|
142
|
+
stopReason: assistant.stopReason ?? "error",
|
|
143
|
+
errorMessage: assistant.errorMessage,
|
|
144
|
+
timestamp: assistant.timestamp ?? Date.now(),
|
|
145
|
+
};
|
|
141
146
|
}
|
|
142
147
|
|
|
143
148
|
function zeroUsage(): Usage {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
return {
|
|
150
|
+
input: 0,
|
|
151
|
+
output: 0,
|
|
152
|
+
cacheRead: 0,
|
|
153
|
+
cacheWrite: 0,
|
|
154
|
+
totalTokens: 0,
|
|
155
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
156
|
+
};
|
|
152
157
|
}
|
|
153
158
|
|
|
154
159
|
function isAgentStopReason(value: unknown): value is AgentStopReason {
|
|
155
|
-
|
|
160
|
+
return ["stop", "length", "toolUse", "error", "aborted"].includes(String(value));
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
function normalizeUsage(value: unknown): Usage | undefined {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
164
|
+
if (!value || typeof value !== "object") return undefined;
|
|
165
|
+
const usage = value as Partial<Usage>;
|
|
166
|
+
if (typeof usage.input !== "number" || typeof usage.output !== "number") return undefined;
|
|
167
|
+
return {
|
|
168
|
+
input: nonNegativeFiniteNumber(usage.input),
|
|
169
|
+
output: nonNegativeFiniteNumber(usage.output),
|
|
170
|
+
cacheRead: nonNegativeFiniteNumber(usage.cacheRead),
|
|
171
|
+
cacheWrite: nonNegativeFiniteNumber(usage.cacheWrite),
|
|
172
|
+
totalTokens: assistantUsageTokens(usage),
|
|
173
|
+
cost: {
|
|
174
|
+
input: usage.cost?.input ?? 0,
|
|
175
|
+
output: usage.cost?.output ?? 0,
|
|
176
|
+
cacheRead: usage.cost?.cacheRead ?? 0,
|
|
177
|
+
cacheWrite: usage.cost?.cacheWrite ?? 0,
|
|
178
|
+
total: usage.cost?.total ?? 0,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
176
181
|
}
|
package/src/goal-contract.ts
CHANGED
|
@@ -5,111 +5,111 @@ export const GOAL_CONTRACT_MESSAGE_TYPE = "goal-contract";
|
|
|
5
5
|
export const GOAL_CONTRACT_VERSION = 2;
|
|
6
6
|
|
|
7
7
|
const INACTIVE_GOAL_CONTRACT_CONTENT = [
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
"Goal mode is inactive.",
|
|
9
|
+
"This Goal contract supersedes every earlier goal-contract message.",
|
|
10
|
+
"Do not treat an earlier Goal objective, goal_id, Goal-mode rule, or summary of them as current unless a later Goal contract explicitly reactivates Goal mode.",
|
|
11
11
|
].join("\n");
|
|
12
12
|
|
|
13
13
|
interface ContractMessage {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
role?: string;
|
|
15
|
+
customType?: string;
|
|
16
|
+
content?: unknown;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
interface ContractSessionEntry extends ContractMessage {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
type?: string;
|
|
21
|
+
message?: unknown;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export function createGoalContextContract(goal: GoalPromptContext) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
return {
|
|
26
|
+
role: "custom" as const,
|
|
27
|
+
customType: GOAL_CONTRACT_MESSAGE_TYPE,
|
|
28
|
+
content: [
|
|
29
|
+
"This Goal contract supersedes every earlier goal-contract message.",
|
|
30
|
+
"Only the objective and goal_id in this latest Goal contract are current.",
|
|
31
|
+
buildGoalContextPrompt(goal),
|
|
32
|
+
].join("\n\n"),
|
|
33
|
+
display: false,
|
|
34
|
+
details: { version: GOAL_CONTRACT_VERSION, state: "active", goalId: goal.id },
|
|
35
|
+
timestamp: 0,
|
|
36
|
+
};
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export function createInactiveGoalContextContract() {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
return {
|
|
41
|
+
role: "custom" as const,
|
|
42
|
+
customType: GOAL_CONTRACT_MESSAGE_TYPE,
|
|
43
|
+
content: INACTIVE_GOAL_CONTRACT_CONTENT,
|
|
44
|
+
display: false,
|
|
45
|
+
details: { version: GOAL_CONTRACT_VERSION, state: "inactive" },
|
|
46
|
+
timestamp: 0,
|
|
47
|
+
};
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function reconcileGoalContextContract(messages: unknown[], goal: GoalPromptContext) {
|
|
51
|
-
|
|
51
|
+
return reconcileContract(messages, createGoalContextContract(goal));
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export function reconcileInactiveGoalContextContract(messages: unknown[]) {
|
|
55
|
-
|
|
55
|
+
return reconcileContract(messages, createInactiveGoalContextContract());
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export function hasGoalContextContract(entries: unknown[], goal: GoalPromptContext) {
|
|
59
|
-
|
|
59
|
+
return latestGoalContractContent(entries) === createGoalContextContract(goal).content;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
export function hasInactiveGoalContextContract(entries: unknown[]) {
|
|
63
|
-
|
|
63
|
+
return latestGoalContractContent(entries) === INACTIVE_GOAL_CONTRACT_CONTENT;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
export function hasGoalContextContractHistory(entries: unknown[]) {
|
|
67
|
-
|
|
67
|
+
return entries.some(isGoalContextContract);
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
export function isGoalContextContract(message: unknown) {
|
|
71
|
-
|
|
71
|
+
return unwrapMessage(message).customType === GOAL_CONTRACT_MESSAGE_TYPE;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
function reconcileContract(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
75
|
+
messages: unknown[],
|
|
76
|
+
expected: {
|
|
77
|
+
role: "custom";
|
|
78
|
+
customType: string;
|
|
79
|
+
content: string;
|
|
80
|
+
display: boolean;
|
|
81
|
+
details: object;
|
|
82
|
+
timestamp: number;
|
|
83
|
+
},
|
|
84
84
|
) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
if (latestGoalContractContent(messages) === expected.content) return messages;
|
|
86
|
+
const summaryBoundary = leadingSummaryBoundary(messages);
|
|
87
|
+
if (!hasGoalContextContractHistory(messages) && summaryBoundary > 0) {
|
|
88
|
+
return [...messages.slice(0, summaryBoundary), expected, ...messages.slice(summaryBoundary)];
|
|
89
|
+
}
|
|
90
|
+
return [...messages, expected];
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
function latestGoalContractContent(messages: readonly unknown[]) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
94
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
95
|
+
const message = messages[index];
|
|
96
|
+
if (isGoalContextContract(message)) return unwrapMessage(message).content;
|
|
97
|
+
}
|
|
98
|
+
return undefined;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
function leadingSummaryBoundary(messages: readonly unknown[]) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
let index = 0;
|
|
103
|
+
while (index < messages.length) {
|
|
104
|
+
const role = unwrapMessage(messages[index]).role;
|
|
105
|
+
if (role !== "compactionSummary" && role !== "branchSummary") break;
|
|
106
|
+
index += 1;
|
|
107
|
+
}
|
|
108
|
+
return index;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
function unwrapMessage(message: unknown): ContractMessage {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
const entry = message as ContractSessionEntry | undefined;
|
|
113
|
+
if (entry?.type === "custom_message") return entry;
|
|
114
|
+
return (entry?.message ?? message ?? {}) as ContractMessage;
|
|
115
115
|
}
|
package/src/goal.ts
CHANGED
|
@@ -7,46 +7,46 @@ import { GoalRuntime } from "./runtime.js";
|
|
|
7
7
|
import { registerGoalTools } from "./tools.js";
|
|
8
8
|
|
|
9
9
|
interface GoalOptions {
|
|
10
|
-
|
|
10
|
+
settingsPath?: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
function registerGoalRuntime(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
const runtime = new GoalRuntime(pi);
|
|
15
|
+
const commands = new GoalCommandController(runtime);
|
|
16
|
+
const runController = new GoalRunController(runtime, commands);
|
|
17
|
+
|
|
18
|
+
// Keep registration order explicit: managed-run bus listeners exist before tools,
|
|
19
|
+
// command routing, and session lifecycle bind the per-factory runtime.
|
|
20
|
+
runController.register(pi);
|
|
21
|
+
registerGoalTools(pi, runtime);
|
|
22
|
+
registerGoalCommand(pi, runtime, commands, options);
|
|
23
|
+
registerGoalLifecycle(pi, runtime, runController, options);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export default function goal(pi: ExtensionAPI, options: GoalOptions = {}) {
|
|
27
|
-
|
|
27
|
+
registerGoalRuntime(pi, options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
assistantUsageTokens,
|
|
32
|
+
cumulativeAssistantTokens,
|
|
33
|
+
formatDuration,
|
|
34
|
+
formatTokenCount,
|
|
35
35
|
} from "./accounting.js";
|
|
36
36
|
|
|
37
37
|
export {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
completeGoalArguments,
|
|
39
|
+
parseCommand,
|
|
40
|
+
parseTokenBudget,
|
|
41
|
+
validateObjective,
|
|
42
42
|
} from "./command.js";
|
|
43
43
|
|
|
44
44
|
export { buildGoalSystemPrompt } from "./prompts.js";
|
|
45
45
|
|
|
46
46
|
export {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
findFinalAssistantMessage,
|
|
48
|
+
formatStatus,
|
|
49
|
+
isContradictoryCompletionSummary,
|
|
50
|
+
isRetryableGoalInterruption,
|
|
51
|
+
isUsageLimitedGoalInterruption,
|
|
52
52
|
} from "./runtime.js";
|