@eduardo191labs/opencode-free-pi 0.7.0 → 0.7.1
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/dist/session/diet.js +61 -9
- package/package.json +1 -1
- package/src/session/diet.ts +59 -10
package/dist/session/diet.js
CHANGED
|
@@ -32,6 +32,37 @@ function textOfContent(content) {
|
|
|
32
32
|
}
|
|
33
33
|
return text;
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Renders an OpenCode v2 `tool-result` value as text. Media parts (files,
|
|
37
|
+
* images) are never rendered: pruning them would destroy the payload instead of
|
|
38
|
+
* shortening prose.
|
|
39
|
+
*/
|
|
40
|
+
function textOfResult(result) {
|
|
41
|
+
if (!result || typeof result !== "object")
|
|
42
|
+
return undefined;
|
|
43
|
+
if (result.type === "content") {
|
|
44
|
+
if (!Array.isArray(result.value))
|
|
45
|
+
return undefined;
|
|
46
|
+
let text = "";
|
|
47
|
+
for (const part of result.value) {
|
|
48
|
+
if (part?.type === "file")
|
|
49
|
+
return undefined;
|
|
50
|
+
if (typeof part?.text === "string")
|
|
51
|
+
text += part.text;
|
|
52
|
+
}
|
|
53
|
+
return text;
|
|
54
|
+
}
|
|
55
|
+
if (typeof result.value === "string")
|
|
56
|
+
return result.value;
|
|
57
|
+
if (result.value === undefined || result.value === null)
|
|
58
|
+
return undefined;
|
|
59
|
+
try {
|
|
60
|
+
return JSON.stringify(result.value);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
35
66
|
function onlyTextParts(content) {
|
|
36
67
|
if (!Array.isArray(content))
|
|
37
68
|
return false;
|
|
@@ -48,21 +79,42 @@ function stub(text) {
|
|
|
48
79
|
}
|
|
49
80
|
/**
|
|
50
81
|
* The same message with its tool result collapsed, or `undefined` when the
|
|
51
|
-
* message has nothing worth pruning.
|
|
52
|
-
*
|
|
82
|
+
* message has nothing worth pruning. The native v2 shape (`tool-result` parts
|
|
83
|
+
* with `result.value`) is the one OpenCode sends; the OpenAI-style shapes are
|
|
84
|
+
* accepted too so a provider-lowered list is handled the same way.
|
|
53
85
|
*/
|
|
54
86
|
function shrunk(message) {
|
|
55
87
|
const role = messageRole(message);
|
|
56
88
|
const record = message;
|
|
57
89
|
if (role === "tool") {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (
|
|
90
|
+
if (typeof record.content === "string") {
|
|
91
|
+
return record.content.length < MIN_PRUNE_CHARS
|
|
92
|
+
? undefined
|
|
93
|
+
: { ...record, content: stub(record.content) };
|
|
94
|
+
}
|
|
95
|
+
if (!Array.isArray(record.content))
|
|
96
|
+
return undefined;
|
|
97
|
+
const parts = record.content;
|
|
98
|
+
if (parts.length === 0)
|
|
64
99
|
return undefined;
|
|
65
|
-
|
|
100
|
+
// Legacy/OpenAI-style text parts.
|
|
101
|
+
if (onlyTextParts(parts)) {
|
|
102
|
+
const text = textOfContent(parts);
|
|
103
|
+
if (text.length < MIN_PRUNE_CHARS)
|
|
104
|
+
return undefined;
|
|
105
|
+
return { ...record, content: stub(text) };
|
|
106
|
+
}
|
|
107
|
+
let changed = false;
|
|
108
|
+
const next = parts.map((part) => {
|
|
109
|
+
if (part?.type !== "tool-result")
|
|
110
|
+
return part;
|
|
111
|
+
const text = textOfResult(part.result);
|
|
112
|
+
if (!text || text.length < MIN_PRUNE_CHARS)
|
|
113
|
+
return part;
|
|
114
|
+
changed = true;
|
|
115
|
+
return { ...part, result: { type: "text", value: stub(text) } };
|
|
116
|
+
});
|
|
117
|
+
return changed ? { ...record, content: next } : undefined;
|
|
66
118
|
}
|
|
67
119
|
if (role !== "assistant" || !Array.isArray(record.content))
|
|
68
120
|
return undefined;
|
package/package.json
CHANGED
package/src/session/diet.ts
CHANGED
|
@@ -29,8 +29,14 @@ type Part = {
|
|
|
29
29
|
text?: string;
|
|
30
30
|
name?: string;
|
|
31
31
|
state?: { content?: unknown };
|
|
32
|
+
result?: { type?: string; value?: unknown };
|
|
32
33
|
};
|
|
33
34
|
|
|
35
|
+
type ResultPart = Part & { result?: { type?: string; value?: unknown } };
|
|
36
|
+
|
|
37
|
+
/** OpenCode v2 `tool-result` value: text, json, error or content parts. */
|
|
38
|
+
type ToolResultValue = { type?: string; value?: unknown } | undefined;
|
|
39
|
+
|
|
34
40
|
function messageRole(message: unknown): string | undefined {
|
|
35
41
|
const role = (message as { role?: unknown } | undefined)?.role;
|
|
36
42
|
return typeof role === "string" ? role : undefined;
|
|
@@ -47,6 +53,31 @@ function textOfContent(content: unknown): string {
|
|
|
47
53
|
return text;
|
|
48
54
|
}
|
|
49
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Renders an OpenCode v2 `tool-result` value as text. Media parts (files,
|
|
58
|
+
* images) are never rendered: pruning them would destroy the payload instead of
|
|
59
|
+
* shortening prose.
|
|
60
|
+
*/
|
|
61
|
+
function textOfResult(result: ToolResultValue): string | undefined {
|
|
62
|
+
if (!result || typeof result !== "object") return undefined;
|
|
63
|
+
if (result.type === "content") {
|
|
64
|
+
if (!Array.isArray(result.value)) return undefined;
|
|
65
|
+
let text = "";
|
|
66
|
+
for (const part of result.value as Part[]) {
|
|
67
|
+
if (part?.type === "file") return undefined;
|
|
68
|
+
if (typeof part?.text === "string") text += part.text;
|
|
69
|
+
}
|
|
70
|
+
return text;
|
|
71
|
+
}
|
|
72
|
+
if (typeof result.value === "string") return result.value;
|
|
73
|
+
if (result.value === undefined || result.value === null) return undefined;
|
|
74
|
+
try {
|
|
75
|
+
return JSON.stringify(result.value);
|
|
76
|
+
} catch {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
50
81
|
function onlyTextParts(content: unknown): boolean {
|
|
51
82
|
if (!Array.isArray(content)) return false;
|
|
52
83
|
return (content as Part[]).every(
|
|
@@ -67,8 +98,9 @@ function stub(text: string): string {
|
|
|
67
98
|
|
|
68
99
|
/**
|
|
69
100
|
* The same message with its tool result collapsed, or `undefined` when the
|
|
70
|
-
* message has nothing worth pruning.
|
|
71
|
-
*
|
|
101
|
+
* message has nothing worth pruning. The native v2 shape (`tool-result` parts
|
|
102
|
+
* with `result.value`) is the one OpenCode sends; the OpenAI-style shapes are
|
|
103
|
+
* accepted too so a provider-lowered list is handled the same way.
|
|
72
104
|
*/
|
|
73
105
|
function shrunk(message: unknown): unknown | undefined {
|
|
74
106
|
const role = messageRole(message);
|
|
@@ -77,14 +109,31 @@ function shrunk(message: unknown): unknown | undefined {
|
|
|
77
109
|
};
|
|
78
110
|
|
|
79
111
|
if (role === "tool") {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
?
|
|
83
|
-
:
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
112
|
+
if (typeof record.content === "string") {
|
|
113
|
+
return record.content.length < MIN_PRUNE_CHARS
|
|
114
|
+
? undefined
|
|
115
|
+
: { ...record, content: stub(record.content) };
|
|
116
|
+
}
|
|
117
|
+
if (!Array.isArray(record.content)) return undefined;
|
|
118
|
+
const parts = record.content as ResultPart[];
|
|
119
|
+
if (parts.length === 0) return undefined;
|
|
120
|
+
|
|
121
|
+
// Legacy/OpenAI-style text parts.
|
|
122
|
+
if (onlyTextParts(parts)) {
|
|
123
|
+
const text = textOfContent(parts);
|
|
124
|
+
if (text.length < MIN_PRUNE_CHARS) return undefined;
|
|
125
|
+
return { ...record, content: stub(text) };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let changed = false;
|
|
129
|
+
const next = parts.map((part) => {
|
|
130
|
+
if (part?.type !== "tool-result") return part;
|
|
131
|
+
const text = textOfResult(part.result);
|
|
132
|
+
if (!text || text.length < MIN_PRUNE_CHARS) return part;
|
|
133
|
+
changed = true;
|
|
134
|
+
return { ...part, result: { type: "text", value: stub(text) } };
|
|
135
|
+
});
|
|
136
|
+
return changed ? { ...record, content: next } : undefined;
|
|
88
137
|
}
|
|
89
138
|
|
|
90
139
|
if (role !== "assistant" || !Array.isArray(record.content)) return undefined;
|