@eduardo191labs/opencode-free-pi 0.7.3 → 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 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
 
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.7.3",
3
+ "version": "0.7.4",
4
4
  "description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -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
+ }