@eduardo191labs/opencode-free-pi 0.7.2 → 0.7.4
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 +1 -0
- package/dist/ads/context.js +4 -2
- package/dist/constants.d.ts +10 -0
- package/dist/constants.js +14 -0
- package/dist/session/hooks.js +24 -1
- package/dist/session/replay.js +2 -0
- package/dist/session/trace.d.ts +10 -0
- package/dist/session/trace.js +80 -0
- package/package.json +1 -1
- package/src/ads/context.ts +4 -2
- package/src/constants.ts +15 -0
- package/src/session/hooks.ts +30 -0
- package/src/session/replay.ts +2 -0
- package/src/session/trace.ts +94 -0
package/README.md
CHANGED
|
@@ -455,6 +455,7 @@ Si `XDG_CONFIG_HOME` o `OPENCODE_CONFIG_DIR` están definidos, se respetan.
|
|
|
455
455
|
| `OPENCODE_CONFIG_DIR` | Directorio de configuración de OpenCode. |
|
|
456
456
|
| `FREE_PI_DATA_DIR` | Directorio de datos del plugin. |
|
|
457
457
|
| `FREE_PI_LOG_FILE` | Ruta alternativa del archivo de log del plugin. |
|
|
458
|
+
| `FREE_PI_TRACE=1` | Registra en el log la forma real de cada petición: herramientas enviadas, tamaños y último mensaje. |
|
|
458
459
|
|
|
459
460
|
No hay variables obligatorias.
|
|
460
461
|
|
package/dist/ads/context.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Builds and redacts the ad context sent with a user turn. */
|
|
2
|
-
import { AD_CONTEXT_MAX_CHARS } from "../constants.js";
|
|
2
|
+
import { AD_CONTEXT_MAX_CHARS, TURN_REMINDER_MARKER } from "../constants.js";
|
|
3
3
|
import { cleanText } from "../utils/text.js";
|
|
4
4
|
const AD_SECRET_PATTERNS = [
|
|
5
5
|
[
|
|
@@ -107,7 +107,9 @@ export function latestUserText(messages) {
|
|
|
107
107
|
if (roleOf(message) !== "user")
|
|
108
108
|
continue;
|
|
109
109
|
const text = cleanText(typeof message.text === "string" ? message.text : contentText(message.content));
|
|
110
|
-
if
|
|
110
|
+
// The plugin appends this after the ad turn is resolved; ignore it if the
|
|
111
|
+
// event is reused so the next ad never describes the reminder itself.
|
|
112
|
+
if (text && !text.startsWith(TURN_REMINDER_MARKER))
|
|
111
113
|
return text;
|
|
112
114
|
}
|
|
113
115
|
return "";
|
package/dist/constants.d.ts
CHANGED
|
@@ -68,6 +68,16 @@ export declare const FREE_PI_GUIDANCE_MARKER = "# free-pi working rules";
|
|
|
68
68
|
*/
|
|
69
69
|
export declare const FREE_PI_COMPACTION_GUIDANCE = "# free-pi compaction rules\n- Keep every unresolved user requirement and constraint, using their wording when a later agent must act on them.\n- For each step in the work state, write the conclusion it proved, not only the file or command: \"read X and confirmed Y\", \"build failed with Z\". A list of actions without their findings makes the next agent repeat the work.\n- Keep decisions and their reasons, exact file paths and symbols, commands with the observed result, error strings, and work still pending.\n- Preserve details that explain why the current state looks the way it does; the next agent cannot re-read the history this summary replaces.\n- Do not invent progress or results that are not in the conversation.";
|
|
70
70
|
export declare const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
|
|
71
|
+
/**
|
|
72
|
+
* Repeats the two rules the free models break when the prompt is long: they
|
|
73
|
+
* stop to describe the session state instead of acting, and they answer a
|
|
74
|
+
* request to search from memory. It is sent as the last message of a primary
|
|
75
|
+
* request because the free models read that position, while rules buried in a
|
|
76
|
+
* 100k-token system prompt are easy to ignore. It is not a user instruction and
|
|
77
|
+
* never becomes part of the stored conversation.
|
|
78
|
+
*/
|
|
79
|
+
export declare const TURN_REMINDER = "# free-pi turn check (not a user request)\nYou are in a normal working turn, not a compaction.\n- Never answer with `## Objective`, `## Requirements`, `## Work State` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.\n- If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.\n- If the task is unfinished, call the next tool now instead of describing the state.";
|
|
80
|
+
export declare const TURN_REMINDER_MARKER = "# free-pi turn check";
|
|
71
81
|
/**
|
|
72
82
|
* OpenCode keeps only the latest exchange beside a summary, so after a
|
|
73
83
|
* compaction the turns that mattered a minute ago disappear unless the summary
|
package/dist/constants.js
CHANGED
|
@@ -98,6 +98,20 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
|
|
|
98
98
|
- Preserve details that explain why the current state looks the way it does; the next agent cannot re-read the history this summary replaces.
|
|
99
99
|
- Do not invent progress or results that are not in the conversation.`;
|
|
100
100
|
export const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
|
|
101
|
+
/**
|
|
102
|
+
* Repeats the two rules the free models break when the prompt is long: they
|
|
103
|
+
* stop to describe the session state instead of acting, and they answer a
|
|
104
|
+
* request to search from memory. It is sent as the last message of a primary
|
|
105
|
+
* request because the free models read that position, while rules buried in a
|
|
106
|
+
* 100k-token system prompt are easy to ignore. It is not a user instruction and
|
|
107
|
+
* never becomes part of the stored conversation.
|
|
108
|
+
*/
|
|
109
|
+
export const TURN_REMINDER = `# free-pi turn check (not a user request)
|
|
110
|
+
You are in a normal working turn, not a compaction.
|
|
111
|
+
- Never answer with \`## Objective\`, \`## Requirements\`, \`## Work State\` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.
|
|
112
|
+
- If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.
|
|
113
|
+
- If the task is unfinished, call the next tool now instead of describing the state.`;
|
|
114
|
+
export const TURN_REMINDER_MARKER = "# free-pi turn check";
|
|
101
115
|
/**
|
|
102
116
|
* OpenCode keeps only the latest exchange beside a summary, so after a
|
|
103
117
|
* compaction the turns that mattered a minute ago disappear unless the summary
|
package/dist/session/hooks.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { ensureAdImpression } from "../ads/impression.js";
|
|
3
3
|
import { resolveAdTurn } from "../ads/resolve.js";
|
|
4
4
|
import { resolveToken } from "../auth/token.js";
|
|
5
|
-
import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, CONTEXT_DIET_MIN_TOKENS, CONTEXT_DIET_SHARE, DEFENSE_PROVIDER_ID, FREE_PI_COMPACTION_GUIDANCE, FREE_PI_COMPACTION_MARKER, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER, MAX_OUTPUT_TOKENS, MAX_OUTPUT_TOKENS_CEILING, PROVIDER_ID, RECENT_WINDOW_CARRY_MARKER, RECENT_WINDOW_CARRY_MS, RECENT_WINDOW_MARKER, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, TRANSPORT_RETRY_BASE_MS, TRANSPORT_RETRY_MAX, TRANSPORT_RETRY_MAX_DELAY_MS, } from "../constants.js";
|
|
5
|
+
import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, CONTEXT_DIET_MIN_TOKENS, CONTEXT_DIET_SHARE, DEFENSE_PROVIDER_ID, FREE_PI_COMPACTION_GUIDANCE, FREE_PI_COMPACTION_MARKER, FREE_PI_GUIDANCE, FREE_PI_GUIDANCE_MARKER, MAX_OUTPUT_TOKENS, MAX_OUTPUT_TOKENS_CEILING, PROVIDER_ID, RECENT_WINDOW_CARRY_MARKER, RECENT_WINDOW_CARRY_MS, RECENT_WINDOW_MARKER, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, TRANSPORT_RETRY_BASE_MS, TRANSPORT_RETRY_MAX, TRANSPORT_RETRY_MAX_DELAY_MS, TURN_REMINDER, TURN_REMINDER_MARKER, } from "../constants.js";
|
|
6
6
|
import { currentLiveLimits, learnedContextWindow, noteRejectedPrompt, } from "../catalog/live.js";
|
|
7
7
|
import { closeFreePiSession } from "../api/session.js";
|
|
8
8
|
import { rewriteContextOverflowResponse, rewriteLimitResponse, } from "../errors/limit.js";
|
|
@@ -150,6 +150,26 @@ function appendCarriedWindow(deps, event) {
|
|
|
150
150
|
return;
|
|
151
151
|
appendSystemGuidance(event, carriedWindowGuidance(carried.text), RECENT_WINDOW_CARRY_MARKER);
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* Puts the two rules weak free models forget on long conversations at the end
|
|
155
|
+
* of a primary request. The ad turn is resolved before this runs, so the
|
|
156
|
+
* reminder never leaks into `x-ad-context`. A fresh array keeps it out of the
|
|
157
|
+
* stored conversation and out of a later compaction request.
|
|
158
|
+
*/
|
|
159
|
+
function appendTurnReminder(event) {
|
|
160
|
+
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
161
|
+
const already = messages.some((message) => Array.isArray(message?.content)
|
|
162
|
+
? message.content.some((part) => part?.type === "text" &&
|
|
163
|
+
typeof part.text === "string" &&
|
|
164
|
+
part.text.startsWith(TURN_REMINDER_MARKER))
|
|
165
|
+
: false);
|
|
166
|
+
if (already)
|
|
167
|
+
return;
|
|
168
|
+
event.messages = [
|
|
169
|
+
...messages,
|
|
170
|
+
{ role: "user", content: [{ type: "text", text: TURN_REMINDER }] },
|
|
171
|
+
];
|
|
172
|
+
}
|
|
153
173
|
/**
|
|
154
174
|
* Collapses stale tool payloads before a request goes out. Long free-pi
|
|
155
175
|
* sessions reach 100k-170k token prompts, and at that size the free models
|
|
@@ -247,6 +267,9 @@ async function applyGeneration(deps, event, kind) {
|
|
|
247
267
|
if (turn) {
|
|
248
268
|
rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
|
|
249
269
|
}
|
|
270
|
+
// After the ad turn: the reminder must not be mistaken for the user prompt.
|
|
271
|
+
if (kind === "primary")
|
|
272
|
+
appendTurnReminder(event);
|
|
250
273
|
}
|
|
251
274
|
/** Adds auth and per-turn ad headers to the outgoing model request. */
|
|
252
275
|
async function applyModelRequest(deps, event) {
|
package/dist/session/replay.js
CHANGED
|
@@ -17,6 +17,7 @@ import { closeFreePiSession } from "../api/session.js";
|
|
|
17
17
|
import { overflowCodeOf } from "../errors/limit.js";
|
|
18
18
|
import { sleep, trimMap } from "../utils/async.js";
|
|
19
19
|
import { profileRequest, SPURIOUS_OVERFLOW_TOKENS, } from "./overflow.js";
|
|
20
|
+
import { traceRequestBody } from "./trace.js";
|
|
20
21
|
import { rotateWireSessionID, wireSessionID } from "./wire.js";
|
|
21
22
|
const REPLAY_BODY_LIMIT = 24;
|
|
22
23
|
const REPLAY_MAX_ATTEMPTS = 4;
|
|
@@ -44,6 +45,7 @@ export async function rememberReplayBody(deps, event) {
|
|
|
44
45
|
deps.state.replayBodies.delete(key);
|
|
45
46
|
deps.state.replayBodies.set(key, body);
|
|
46
47
|
trimMap(deps.state.replayBodies, REPLAY_BODY_LIMIT);
|
|
48
|
+
traceRequestBody(deps, event.sessionID, body);
|
|
47
49
|
}
|
|
48
50
|
catch {
|
|
49
51
|
// A non-replayable body simply falls back to the normal retry hook.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic dump of what the plugin actually sends to free-pi.
|
|
3
|
+
*
|
|
4
|
+
* Enabled with `FREE_PI_TRACE=1`. It answers the questions the TUI and the
|
|
5
|
+
* session database cannot: whether the tools travel complete, whether the
|
|
6
|
+
* provider request keeps older reasoning, and whether the per-turn reminder is
|
|
7
|
+
* the last message OpenCode sends. It never runs unless the env var is set.
|
|
8
|
+
*/
|
|
9
|
+
import type { Deps } from "../deps.js";
|
|
10
|
+
export declare function traceRequestBody(deps: Deps, sessionID: string, body: string): void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic dump of what the plugin actually sends to free-pi.
|
|
3
|
+
*
|
|
4
|
+
* Enabled with `FREE_PI_TRACE=1`. It answers the questions the TUI and the
|
|
5
|
+
* session database cannot: whether the tools travel complete, whether the
|
|
6
|
+
* provider request keeps older reasoning, and whether the per-turn reminder is
|
|
7
|
+
* the last message OpenCode sends. It never runs unless the env var is set.
|
|
8
|
+
*/
|
|
9
|
+
function textOf(content) {
|
|
10
|
+
if (typeof content === "string")
|
|
11
|
+
return content;
|
|
12
|
+
if (!Array.isArray(content))
|
|
13
|
+
return "";
|
|
14
|
+
return content
|
|
15
|
+
.map((part) => typeof part?.text === "string"
|
|
16
|
+
? part.text
|
|
17
|
+
: typeof part?.thinking === "string"
|
|
18
|
+
? part.thinking
|
|
19
|
+
: "")
|
|
20
|
+
.join("");
|
|
21
|
+
}
|
|
22
|
+
export function traceRequestBody(deps, sessionID, body) {
|
|
23
|
+
if (process.env.FREE_PI_TRACE !== "1")
|
|
24
|
+
return;
|
|
25
|
+
try {
|
|
26
|
+
const parsed = JSON.parse(body);
|
|
27
|
+
const messages = Array.isArray(parsed.messages) ? parsed.messages : [];
|
|
28
|
+
const roles = {};
|
|
29
|
+
let textChars = 0;
|
|
30
|
+
let reasoningChars = 0;
|
|
31
|
+
let toolChars = 0;
|
|
32
|
+
for (const message of messages) {
|
|
33
|
+
const role = message.role ?? "?";
|
|
34
|
+
roles[role] = (roles[role] ?? 0) + 1;
|
|
35
|
+
if (Array.isArray(message.content)) {
|
|
36
|
+
for (const part of message.content) {
|
|
37
|
+
if (part?.type === "text" && typeof part.text === "string") {
|
|
38
|
+
textChars += part.text.length;
|
|
39
|
+
}
|
|
40
|
+
else if ((part?.type === "reasoning" || part?.type === "thinking") &&
|
|
41
|
+
typeof part.text === "string") {
|
|
42
|
+
reasoningChars += part.text.length;
|
|
43
|
+
}
|
|
44
|
+
else if (part?.type === "tool-result") {
|
|
45
|
+
toolChars += JSON.stringify(part.result ?? "").length;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (typeof message.content === "string") {
|
|
50
|
+
if (role === "tool")
|
|
51
|
+
toolChars += message.content.length;
|
|
52
|
+
else
|
|
53
|
+
textChars += message.content.length;
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(message.tool_calls)) {
|
|
56
|
+
toolChars += JSON.stringify(message.tool_calls).length;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const tools = Array.isArray(parsed.tools)
|
|
60
|
+
? parsed.tools.map((tool) => tool?.function?.name ?? "?")
|
|
61
|
+
: [];
|
|
62
|
+
const last = messages.at(-1);
|
|
63
|
+
deps.log.info("free-pi trace request", {
|
|
64
|
+
sessionID,
|
|
65
|
+
model: parsed.model,
|
|
66
|
+
messages: messages.length,
|
|
67
|
+
roles,
|
|
68
|
+
textChars,
|
|
69
|
+
reasoningChars,
|
|
70
|
+
toolChars,
|
|
71
|
+
tools: tools.length,
|
|
72
|
+
toolNames: tools,
|
|
73
|
+
lastRole: last?.role,
|
|
74
|
+
lastStart: textOf(last?.content).slice(0, 80).replace(/\n/g, " "),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// A body that is not JSON is not a model request worth tracing.
|
|
79
|
+
}
|
|
80
|
+
}
|
package/package.json
CHANGED
package/src/ads/context.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Builds and redacts the ad context sent with a user turn. */
|
|
2
2
|
|
|
3
|
-
import { AD_CONTEXT_MAX_CHARS } from "../constants.js";
|
|
3
|
+
import { AD_CONTEXT_MAX_CHARS, TURN_REMINDER_MARKER } from "../constants.js";
|
|
4
4
|
import { cleanText } from "../utils/text.js";
|
|
5
5
|
|
|
6
6
|
const AD_SECRET_PATTERNS: Array<[string, RegExp]> = [
|
|
@@ -138,7 +138,9 @@ export function latestUserText(messages: readonly AdMessage[]): string {
|
|
|
138
138
|
const text = cleanText(
|
|
139
139
|
typeof message.text === "string" ? message.text : contentText(message.content),
|
|
140
140
|
);
|
|
141
|
-
|
|
141
|
+
// The plugin appends this after the ad turn is resolved; ignore it if the
|
|
142
|
+
// event is reused so the next ad never describes the reminder itself.
|
|
143
|
+
if (text && !text.startsWith(TURN_REMINDER_MARKER)) return text;
|
|
142
144
|
}
|
|
143
145
|
return "";
|
|
144
146
|
}
|
package/src/constants.ts
CHANGED
|
@@ -107,6 +107,21 @@ export const FREE_PI_COMPACTION_GUIDANCE = `# free-pi compaction rules
|
|
|
107
107
|
- Do not invent progress or results that are not in the conversation.`;
|
|
108
108
|
export const FREE_PI_COMPACTION_MARKER = "# free-pi compaction rules";
|
|
109
109
|
|
|
110
|
+
/**
|
|
111
|
+
* Repeats the two rules the free models break when the prompt is long: they
|
|
112
|
+
* stop to describe the session state instead of acting, and they answer a
|
|
113
|
+
* request to search from memory. It is sent as the last message of a primary
|
|
114
|
+
* request because the free models read that position, while rules buried in a
|
|
115
|
+
* 100k-token system prompt are easy to ignore. It is not a user instruction and
|
|
116
|
+
* never becomes part of the stored conversation.
|
|
117
|
+
*/
|
|
118
|
+
export const TURN_REMINDER = `# free-pi turn check (not a user request)
|
|
119
|
+
You are in a normal working turn, not a compaction.
|
|
120
|
+
- Never answer with \`## Objective\`, \`## Requirements\`, \`## Work State\` or a session summary; that format belongs only to a compaction. Earlier sections with those names are history, not an answer template.
|
|
121
|
+
- If the user asked to search, find, analyze or verify something, call the matching tool first (grep/glob/shell for files, websearch/webfetch for the web) and report what it returned.
|
|
122
|
+
- If the task is unfinished, call the next tool now instead of describing the state.`;
|
|
123
|
+
export const TURN_REMINDER_MARKER = "# free-pi turn check";
|
|
124
|
+
|
|
110
125
|
/**
|
|
111
126
|
* OpenCode keeps only the latest exchange beside a summary, so after a
|
|
112
127
|
* compaction the turns that mattered a minute ago disappear unless the summary
|
package/src/session/hooks.ts
CHANGED
|
@@ -31,6 +31,8 @@ import {
|
|
|
31
31
|
TRANSPORT_RETRY_BASE_MS,
|
|
32
32
|
TRANSPORT_RETRY_MAX,
|
|
33
33
|
TRANSPORT_RETRY_MAX_DELAY_MS,
|
|
34
|
+
TURN_REMINDER,
|
|
35
|
+
TURN_REMINDER_MARKER,
|
|
34
36
|
} from "../constants.js";
|
|
35
37
|
import {
|
|
36
38
|
currentLiveLimits,
|
|
@@ -273,6 +275,31 @@ function appendCarriedWindow(deps: Deps, event: any): void {
|
|
|
273
275
|
);
|
|
274
276
|
}
|
|
275
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Puts the two rules weak free models forget on long conversations at the end
|
|
280
|
+
* of a primary request. The ad turn is resolved before this runs, so the
|
|
281
|
+
* reminder never leaks into `x-ad-context`. A fresh array keeps it out of the
|
|
282
|
+
* stored conversation and out of a later compaction request.
|
|
283
|
+
*/
|
|
284
|
+
function appendTurnReminder(event: any): void {
|
|
285
|
+
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
286
|
+
const already = messages.some((message: any) =>
|
|
287
|
+
Array.isArray(message?.content)
|
|
288
|
+
? message.content.some(
|
|
289
|
+
(part: any) =>
|
|
290
|
+
part?.type === "text" &&
|
|
291
|
+
typeof part.text === "string" &&
|
|
292
|
+
part.text.startsWith(TURN_REMINDER_MARKER),
|
|
293
|
+
)
|
|
294
|
+
: false,
|
|
295
|
+
);
|
|
296
|
+
if (already) return;
|
|
297
|
+
event.messages = [
|
|
298
|
+
...messages,
|
|
299
|
+
{ role: "user", content: [{ type: "text", text: TURN_REMINDER }] },
|
|
300
|
+
];
|
|
301
|
+
}
|
|
302
|
+
|
|
276
303
|
/**
|
|
277
304
|
* Collapses stale tool payloads before a request goes out. Long free-pi
|
|
278
305
|
* sessions reach 100k-170k token prompts, and at that size the free models
|
|
@@ -385,6 +412,9 @@ async function applyGeneration(
|
|
|
385
412
|
if (turn) {
|
|
386
413
|
rememberPending(deps, pendingKeyOf(event.sessionID, agent, kind), turn);
|
|
387
414
|
}
|
|
415
|
+
|
|
416
|
+
// After the ad turn: the reminder must not be mistaken for the user prompt.
|
|
417
|
+
if (kind === "primary") appendTurnReminder(event);
|
|
388
418
|
}
|
|
389
419
|
|
|
390
420
|
/** Adds auth and per-turn ad headers to the outgoing model request. */
|
package/src/session/replay.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
profileRequest,
|
|
23
23
|
SPURIOUS_OVERFLOW_TOKENS,
|
|
24
24
|
} from "./overflow.js";
|
|
25
|
+
import { traceRequestBody } from "./trace.js";
|
|
25
26
|
import { rotateWireSessionID, wireSessionID } from "./wire.js";
|
|
26
27
|
|
|
27
28
|
const REPLAY_BODY_LIMIT = 24;
|
|
@@ -56,6 +57,7 @@ export async function rememberReplayBody(
|
|
|
56
57
|
deps.state.replayBodies.delete(key);
|
|
57
58
|
deps.state.replayBodies.set(key, body);
|
|
58
59
|
trimMap(deps.state.replayBodies, REPLAY_BODY_LIMIT);
|
|
60
|
+
traceRequestBody(deps, event.sessionID, body);
|
|
59
61
|
} catch {
|
|
60
62
|
// A non-replayable body simply falls back to the normal retry hook.
|
|
61
63
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diagnostic dump of what the plugin actually sends to free-pi.
|
|
3
|
+
*
|
|
4
|
+
* Enabled with `FREE_PI_TRACE=1`. It answers the questions the TUI and the
|
|
5
|
+
* session database cannot: whether the tools travel complete, whether the
|
|
6
|
+
* provider request keeps older reasoning, and whether the per-turn reminder is
|
|
7
|
+
* the last message OpenCode sends. It never runs unless the env var is set.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Deps } from "../deps.js";
|
|
11
|
+
|
|
12
|
+
type TraceMessage = {
|
|
13
|
+
role?: string;
|
|
14
|
+
content?: unknown;
|
|
15
|
+
tool_calls?: unknown;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function textOf(content: unknown): string {
|
|
19
|
+
if (typeof content === "string") return content;
|
|
20
|
+
if (!Array.isArray(content)) return "";
|
|
21
|
+
return content
|
|
22
|
+
.map((part: any) =>
|
|
23
|
+
typeof part?.text === "string"
|
|
24
|
+
? part.text
|
|
25
|
+
: typeof part?.thinking === "string"
|
|
26
|
+
? part.thinking
|
|
27
|
+
: "",
|
|
28
|
+
)
|
|
29
|
+
.join("");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function traceRequestBody(
|
|
33
|
+
deps: Deps,
|
|
34
|
+
sessionID: string,
|
|
35
|
+
body: string,
|
|
36
|
+
): void {
|
|
37
|
+
if (process.env.FREE_PI_TRACE !== "1") return;
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(body) as {
|
|
40
|
+
model?: string;
|
|
41
|
+
messages?: TraceMessage[];
|
|
42
|
+
tools?: Array<{ function?: { name?: string } }>;
|
|
43
|
+
};
|
|
44
|
+
const messages = Array.isArray(parsed.messages) ? parsed.messages : [];
|
|
45
|
+
const roles: Record<string, number> = {};
|
|
46
|
+
let textChars = 0;
|
|
47
|
+
let reasoningChars = 0;
|
|
48
|
+
let toolChars = 0;
|
|
49
|
+
for (const message of messages) {
|
|
50
|
+
const role = message.role ?? "?";
|
|
51
|
+
roles[role] = (roles[role] ?? 0) + 1;
|
|
52
|
+
if (Array.isArray(message.content)) {
|
|
53
|
+
for (const part of message.content as any[]) {
|
|
54
|
+
if (part?.type === "text" && typeof part.text === "string") {
|
|
55
|
+
textChars += part.text.length;
|
|
56
|
+
} else if (
|
|
57
|
+
(part?.type === "reasoning" || part?.type === "thinking") &&
|
|
58
|
+
typeof part.text === "string"
|
|
59
|
+
) {
|
|
60
|
+
reasoningChars += part.text.length;
|
|
61
|
+
} else if (part?.type === "tool-result") {
|
|
62
|
+
toolChars += JSON.stringify(part.result ?? "").length;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (typeof message.content === "string") {
|
|
67
|
+
if (role === "tool") toolChars += message.content.length;
|
|
68
|
+
else textChars += message.content.length;
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(message.tool_calls)) {
|
|
71
|
+
toolChars += JSON.stringify(message.tool_calls).length;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const tools = Array.isArray(parsed.tools)
|
|
75
|
+
? parsed.tools.map((tool) => tool?.function?.name ?? "?")
|
|
76
|
+
: [];
|
|
77
|
+
const last = messages.at(-1);
|
|
78
|
+
deps.log.info("free-pi trace request", {
|
|
79
|
+
sessionID,
|
|
80
|
+
model: parsed.model,
|
|
81
|
+
messages: messages.length,
|
|
82
|
+
roles,
|
|
83
|
+
textChars,
|
|
84
|
+
reasoningChars,
|
|
85
|
+
toolChars,
|
|
86
|
+
tools: tools.length,
|
|
87
|
+
toolNames: tools,
|
|
88
|
+
lastRole: last?.role,
|
|
89
|
+
lastStart: textOf(last?.content).slice(0, 80).replace(/\n/g, " "),
|
|
90
|
+
});
|
|
91
|
+
} catch {
|
|
92
|
+
// A body that is not JSON is not a model request worth tracing.
|
|
93
|
+
}
|
|
94
|
+
}
|