@eduardo191labs/opencode-free-pi 0.7.29 → 0.7.31
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 +21 -69
- package/dist/auth/credential.d.ts +0 -1
- package/dist/auth/credential.js +1 -11
- package/dist/auth/credentials.d.ts +0 -1
- package/dist/auth/credentials.js +0 -8
- package/dist/auth/jwt.d.ts +1 -2
- package/dist/auth/jwt.js +0 -26
- package/dist/catalog/live.js +2 -2
- package/dist/constants.d.ts +0 -3
- package/dist/constants.js +0 -3
- package/dist/deps.d.ts +1 -4
- package/dist/errors/handle.js +0 -1
- package/dist/errors/limit.d.ts +0 -2
- package/dist/errors/limit.js +0 -14
- package/dist/legacy.d.ts +17 -0
- package/dist/legacy.js +58 -0
- package/dist/plugin.js +10 -84
- package/dist/session/overflow.d.ts +0 -1
- package/dist/session/overflow.js +0 -3
- package/dist/session/tools.d.ts +1 -1
- package/dist/session/tools.js +0 -1
- package/dist/state.d.ts +1 -4
- package/dist/state.js +0 -2
- package/dist/tools/api.d.ts +1 -1
- package/dist/tools/api.js +1 -1
- package/dist/tools/web.js +1 -1
- package/dist/types.d.ts +0 -23
- package/dist/utils/numbers.d.ts +0 -1
- package/dist/utils/numbers.js +0 -6
- package/package.json +2 -2
- package/src/auth/credential.ts +1 -13
- package/src/auth/credentials.ts +0 -8
- package/src/auth/jwt.ts +1 -31
- package/src/catalog/live.ts +1 -2
- package/src/constants.ts +0 -4
- package/src/deps.ts +1 -8
- package/src/errors/handle.ts +0 -1
- package/src/errors/limit.ts +0 -18
- package/src/legacy.ts +62 -0
- package/src/plugin.ts +11 -97
- package/src/session/hooks.ts +0 -1
- package/src/session/overflow.ts +0 -4
- package/src/session/tools.ts +0 -1
- package/src/state.ts +0 -6
- package/src/tools/api.ts +0 -1
- package/src/tools/usage.ts +0 -1
- package/src/tools/web.ts +1 -1
- package/src/types.ts +0 -31
- package/src/utils/numbers.ts +0 -8
- package/dist/agents/definitions.d.ts +0 -56
- package/dist/agents/definitions.js +0 -317
- package/dist/tools/subagent.d.ts +0 -29
- package/dist/tools/subagent.js +0 -197
- package/src/agents/definitions.ts +0 -354
- package/src/tools/subagent.ts +0 -271
|
@@ -1,354 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Native OpenCode agents for free-pi.
|
|
3
|
-
*
|
|
4
|
-
* The provider CLI ships four agent personalities (scout, planner, worker,
|
|
5
|
-
* reviewer) with their own prompts and tool access. OpenCode's V2 plugin API
|
|
6
|
-
* cannot add agents through `AgentEditor`, but it loads Markdown agents from the
|
|
7
|
-
* global config directory. Installing those files is the supported way to map
|
|
8
|
-
* the CLI agents onto OpenCode's native `subagent` tool, so child sessions,
|
|
9
|
-
* background reports, permissions and the agent picker keep working normally.
|
|
10
|
-
*
|
|
11
|
-
* They use `mode: all`: OpenCode only lists non-subagent agents in the `/agents`
|
|
12
|
-
* picker, and only agents whose mode is not `primary` can run through the
|
|
13
|
-
* `subagent` tool. `all` satisfies both, so the same personalities are
|
|
14
|
-
* selectable as the main agent and spawnable as children.
|
|
15
|
-
*
|
|
16
|
-
* Files live under `agents/free-pi/`, which makes their IDs `free-pi/scout`,
|
|
17
|
-
* `free-pi/planner`, `free-pi/worker` and `free-pi/reviewer`.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
21
|
-
import { homedir } from "node:os";
|
|
22
|
-
import { dirname, join } from "node:path";
|
|
23
|
-
|
|
24
|
-
const MARKER =
|
|
25
|
-
"<!-- managed by @eduardo191labs/opencode-free-pi; delete this line to keep your own copy -->";
|
|
26
|
-
|
|
27
|
-
export const FREE_PI_AGENT_IDS = [
|
|
28
|
-
"free-pi/scout",
|
|
29
|
-
"free-pi/planner",
|
|
30
|
-
"free-pi/worker",
|
|
31
|
-
"free-pi/reviewer",
|
|
32
|
-
] as const;
|
|
33
|
-
|
|
34
|
-
export type FreePiAgentID = (typeof FREE_PI_AGENT_IDS)[number];
|
|
35
|
-
|
|
36
|
-
export const FREE_PI_SKILL_ID = "free-pi";
|
|
37
|
-
export const FREE_PI_SKILL_NAME = "free-pi";
|
|
38
|
-
export const FREE_PI_SKILL_DESCRIPTION =
|
|
39
|
-
"How free-pi works in OpenCode: its tools, the installed native subagents, allowance and limits, and where sessions and memory live.";
|
|
40
|
-
|
|
41
|
-
export const FREE_PI_SKILL = `# free-pi in OpenCode
|
|
42
|
-
|
|
43
|
-
free-pi is a free, ad-supported provider for OpenCode. You sign in with GitHub
|
|
44
|
-
and ads in the session pay for the inference; there is no model API key. The
|
|
45
|
-
free daily allowance is shared by every free-pi model and every tool that spends
|
|
46
|
-
it.
|
|
47
|
-
|
|
48
|
-
## Tools
|
|
49
|
-
- \`free_pi_usage\` shows today's remaining allowance, credits, token counts and
|
|
50
|
-
the account tier. Read-only.
|
|
51
|
-
- \`free_pi_subagent_read\` reads the transcript of a child session started with
|
|
52
|
-
the \`subagent\` tool: its prompts, every tool call with input and result, and
|
|
53
|
-
its final answer. Read-only; omit \`sessionID\` to list this session's children.
|
|
54
|
-
- \`look_at_image\` asks a vision model about a PNG, JPEG or WebP inside the
|
|
55
|
-
project. Use it when the current model takes text only, or when the image is a
|
|
56
|
-
file on disk and you want a targeted answer instead of attaching it.
|
|
57
|
-
- \`generate_image\` draws one image from a prompt and saves it under \`images/\`.
|
|
58
|
-
- \`web_search\` searches the web for current information.
|
|
59
|
-
- \`docs_lookup\` looks up current library and framework documentation.
|
|
60
|
-
|
|
61
|
-
Every tool except \`free_pi_usage\` spends allowance. Usage limits
|
|
62
|
-
(\`daily_cap\`, \`rolling_cap\`, concurrency) are terminal: stop and continue
|
|
63
|
-
without the tool instead of retrying.
|
|
64
|
-
|
|
65
|
-
## Subagents
|
|
66
|
-
|
|
67
|
-
Use OpenCode's native subagent tool with the agents this plugin installs:
|
|
68
|
-
|
|
69
|
-
| Agent | Use it for |
|
|
70
|
-
| --- | --- |
|
|
71
|
-
| \`free-pi/scout\` | Read-only recon: locate code and return structured findings. |
|
|
72
|
-
| \`free-pi/planner\` | Read-only plan for a change, ready for a worker. |
|
|
73
|
-
| \`free-pi/worker\` | Carry out one full task in its own context window. |
|
|
74
|
-
| \`free-pi/reviewer\` | Read-only review with file and line references. |
|
|
75
|
-
|
|
76
|
-
Subagents inherit the free-pi model, share the same daily allowance, and run as
|
|
77
|
-
child sessions in the background. Keep large exploration in a subagent so it
|
|
78
|
-
does not fill your own context. Use OpenCode's agent picker or the \`subagent\`
|
|
79
|
-
tool; do not try to launch them through shell commands. The \`subagent\` tool
|
|
80
|
-
returns the child \`sessionID\` and OpenCode injects the child's report when it
|
|
81
|
-
goes idle; call \`free_pi_subagent_read\` with that id to see what the child ran
|
|
82
|
-
and why its answer says what it says.
|
|
83
|
-
|
|
84
|
-
## Memory and sessions
|
|
85
|
-
|
|
86
|
-
OpenCode owns memory and context: \`AGENTS.md\` and configured instruction files
|
|
87
|
-
are loaded natively, sessions persist and resume, and compaction folds long
|
|
88
|
-
conversations into a summary. Put durable project facts in \`AGENTS.md\` (or a
|
|
89
|
-
file listed under \`instructions\`) instead of repeating them in every prompt.
|
|
90
|
-
|
|
91
|
-
## Native integrations
|
|
92
|
-
|
|
93
|
-
MCP servers, skills, commands, references and permissions are configured in
|
|
94
|
-
OpenCode as usual; free-pi does not replace them. Prefer those native features
|
|
95
|
-
over running an external CLI.
|
|
96
|
-
`;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The same skill as a native `SKILL.md`. OpenCode discovers it from the global
|
|
100
|
-
* skills directory, so the file, its ID, its permissions and the `skill` tool
|
|
101
|
-
* all belong to OpenCode instead of a runtime registration.
|
|
102
|
-
*/
|
|
103
|
-
export const FREE_PI_SKILL_FILE = `---
|
|
104
|
-
name: ${FREE_PI_SKILL_NAME}
|
|
105
|
-
description: ${FREE_PI_SKILL_DESCRIPTION}
|
|
106
|
-
---
|
|
107
|
-
|
|
108
|
-
${MARKER}
|
|
109
|
-
|
|
110
|
-
${FREE_PI_SKILL}`;
|
|
111
|
-
|
|
112
|
-
const SCOUT = `---
|
|
113
|
-
description: Read-only recon. Locates code, traces how pieces connect, and returns structured findings the lead can use without re-reading the files.
|
|
114
|
-
mode: all
|
|
115
|
-
color: "#4cc9f0"
|
|
116
|
-
permissions:
|
|
117
|
-
- action: edit
|
|
118
|
-
resource: "*"
|
|
119
|
-
effect: deny
|
|
120
|
-
- action: shell
|
|
121
|
-
resource: "*"
|
|
122
|
-
effect: deny
|
|
123
|
-
- action: subagent
|
|
124
|
-
resource: "*"
|
|
125
|
-
effect: deny
|
|
126
|
-
---
|
|
127
|
-
|
|
128
|
-
${MARKER}
|
|
129
|
-
|
|
130
|
-
You are a scout. Quickly investigate a codebase and return structured findings
|
|
131
|
-
that another agent can use without re-reading everything. Your output goes to an
|
|
132
|
-
agent who has NOT seen the files you explored.
|
|
133
|
-
|
|
134
|
-
Use read, grep and glob to locate relevant code; read key sections (not whole
|
|
135
|
-
files); note types, key functions, and how pieces connect. Do not modify files.
|
|
136
|
-
|
|
137
|
-
Output format:
|
|
138
|
-
## Files Retrieved
|
|
139
|
-
1. \`path/to/file.ts\` (lines 10-50) - what is here
|
|
140
|
-
## Key Code
|
|
141
|
-
The critical types, interfaces, or functions, quoted from the files.
|
|
142
|
-
## Architecture
|
|
143
|
-
How the pieces connect, briefly.
|
|
144
|
-
## Start Here
|
|
145
|
-
Which file to look at first and why.
|
|
146
|
-
`;
|
|
147
|
-
|
|
148
|
-
const PLANNER = `---
|
|
149
|
-
description: Turns context and requirements into a concrete implementation plan. Read-only: never edits files or runs commands.
|
|
150
|
-
mode: all
|
|
151
|
-
color: "#b5179e"
|
|
152
|
-
permissions:
|
|
153
|
-
- action: edit
|
|
154
|
-
resource: "*"
|
|
155
|
-
effect: deny
|
|
156
|
-
- action: shell
|
|
157
|
-
resource: "*"
|
|
158
|
-
effect: deny
|
|
159
|
-
- action: subagent
|
|
160
|
-
resource: "*"
|
|
161
|
-
effect: deny
|
|
162
|
-
---
|
|
163
|
-
|
|
164
|
-
${MARKER}
|
|
165
|
-
|
|
166
|
-
You are a planning specialist. You receive context and requirements, then
|
|
167
|
-
produce a clear implementation plan. You must NOT make any changes. Only read,
|
|
168
|
-
analyze, and plan.
|
|
169
|
-
|
|
170
|
-
Output format:
|
|
171
|
-
## Goal
|
|
172
|
-
One sentence.
|
|
173
|
-
## Plan
|
|
174
|
-
Numbered steps, each small and actionable, naming the file or function to change.
|
|
175
|
-
## Files to Modify
|
|
176
|
-
## New Files (if any)
|
|
177
|
-
## Risks
|
|
178
|
-
Keep the plan concrete: a worker agent will execute it verbatim.
|
|
179
|
-
`;
|
|
180
|
-
|
|
181
|
-
const WORKER = `---
|
|
182
|
-
description: Full-capability worker that carries out one delegated task in its own context window and reports what changed.
|
|
183
|
-
mode: all
|
|
184
|
-
color: "#2a9d8f"
|
|
185
|
-
permissions:
|
|
186
|
-
- action: subagent
|
|
187
|
-
resource: "*"
|
|
188
|
-
effect: deny
|
|
189
|
-
---
|
|
190
|
-
|
|
191
|
-
${MARKER}
|
|
192
|
-
|
|
193
|
-
You are a worker agent with full capabilities, working in an isolated context
|
|
194
|
-
window on a task delegated by the lead. Work autonomously to complete it.
|
|
195
|
-
|
|
196
|
-
Do not start other subagents.
|
|
197
|
-
|
|
198
|
-
Output format when finished:
|
|
199
|
-
## Completed
|
|
200
|
-
What was done.
|
|
201
|
-
## Files Changed
|
|
202
|
-
- \`path/to/file.ts\` - what changed
|
|
203
|
-
## Notes (if any)
|
|
204
|
-
Anything the lead should know.
|
|
205
|
-
`;
|
|
206
|
-
|
|
207
|
-
const REVIEWER = `---
|
|
208
|
-
description: Senior code reviewer for quality, security and maintainability. Read-only; reports findings with file and line references.
|
|
209
|
-
mode: all
|
|
210
|
-
color: "#e76f51"
|
|
211
|
-
permissions:
|
|
212
|
-
- action: edit
|
|
213
|
-
resource: "*"
|
|
214
|
-
effect: deny
|
|
215
|
-
- action: shell
|
|
216
|
-
resource: "*"
|
|
217
|
-
effect: deny
|
|
218
|
-
- action: shell
|
|
219
|
-
resource: "git diff*"
|
|
220
|
-
effect: allow
|
|
221
|
-
- action: shell
|
|
222
|
-
resource: "git log*"
|
|
223
|
-
effect: allow
|
|
224
|
-
- action: shell
|
|
225
|
-
resource: "git show*"
|
|
226
|
-
effect: allow
|
|
227
|
-
- action: shell
|
|
228
|
-
resource: "git status*"
|
|
229
|
-
effect: allow
|
|
230
|
-
- action: subagent
|
|
231
|
-
resource: "*"
|
|
232
|
-
effect: deny
|
|
233
|
-
---
|
|
234
|
-
|
|
235
|
-
${MARKER}
|
|
236
|
-
|
|
237
|
-
You are a senior code reviewer. Analyze code for quality, security, and
|
|
238
|
-
maintainability.
|
|
239
|
-
|
|
240
|
-
Bash is for read-only commands only: \`git diff\`, \`git log\`, \`git show\`.
|
|
241
|
-
Do NOT modify files or run builds. Assume tool permissions are not perfectly
|
|
242
|
-
enforceable; keep all bash usage strictly read-only.
|
|
243
|
-
|
|
244
|
-
Strategy: run \`git diff\` to see recent changes (if applicable), read the
|
|
245
|
-
modified files, check for bugs, security issues, and code smells.
|
|
246
|
-
|
|
247
|
-
Output format:
|
|
248
|
-
## Files Reviewed
|
|
249
|
-
## Critical (must fix)
|
|
250
|
-
- \`file.ts:42\` - issue
|
|
251
|
-
## Warnings (should fix)
|
|
252
|
-
## Suggestions (consider)
|
|
253
|
-
## Summary
|
|
254
|
-
Overall assessment in 2-3 sentences. Be specific with file paths and line
|
|
255
|
-
numbers.
|
|
256
|
-
`;
|
|
257
|
-
|
|
258
|
-
const AGENT_FILES: ReadonlyArray<{ id: FreePiAgentID; content: string }> = [
|
|
259
|
-
{ id: "free-pi/scout", content: SCOUT },
|
|
260
|
-
{ id: "free-pi/planner", content: PLANNER },
|
|
261
|
-
{ id: "free-pi/worker", content: WORKER },
|
|
262
|
-
{ id: "free-pi/reviewer", content: REVIEWER },
|
|
263
|
-
];
|
|
264
|
-
|
|
265
|
-
/** OpenCode's user-wide config directory, where it loads custom agents. */
|
|
266
|
-
export function openCodeConfigDir(): string {
|
|
267
|
-
const explicit = process.env.OPENCODE_CONFIG_DIR?.trim();
|
|
268
|
-
if (explicit) return explicit;
|
|
269
|
-
const xdg = process.env.XDG_CONFIG_HOME?.trim();
|
|
270
|
-
if (xdg) return join(xdg, "opencode");
|
|
271
|
-
return join(homedir(), ".config", "opencode");
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export function freePiAgentPath(configDir: string, id: FreePiAgentID): string {
|
|
275
|
-
return join(configDir, "agents", ...id.split("/")) + ".md";
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
export function freePiSkillPath(configDir: string): string {
|
|
279
|
-
return join(configDir, "skills", FREE_PI_SKILL_ID, "SKILL.md");
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
async function fileState(
|
|
283
|
-
path: string,
|
|
284
|
-
content: string,
|
|
285
|
-
): Promise<"write" | "same" | "protected"> {
|
|
286
|
-
try {
|
|
287
|
-
const current = await readFile(path, "utf8");
|
|
288
|
-
if (current === content) return "same";
|
|
289
|
-
// The user replaced or edited this file; never overwrite their work.
|
|
290
|
-
return current.slice(0, 1024).includes(MARKER) ? "write" : "protected";
|
|
291
|
-
} catch {
|
|
292
|
-
return "write";
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
export type AgentInstallResult = {
|
|
297
|
-
readonly installed: FreePiAgentID[];
|
|
298
|
-
readonly skipped: FreePiAgentID[];
|
|
299
|
-
readonly changed: boolean;
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Writes the bundled agents into the OpenCode config directory so OpenCode's
|
|
304
|
-
* own loader and agent picker own them. Existing files without our marker are
|
|
305
|
-
* treated as user-owned and left untouched.
|
|
306
|
-
*/
|
|
307
|
-
export async function installFreePiAgents(
|
|
308
|
-
configDir = openCodeConfigDir(),
|
|
309
|
-
): Promise<AgentInstallResult> {
|
|
310
|
-
const installed: FreePiAgentID[] = [];
|
|
311
|
-
const skipped: FreePiAgentID[] = [];
|
|
312
|
-
|
|
313
|
-
for (const agent of AGENT_FILES) {
|
|
314
|
-
const path = freePiAgentPath(configDir, agent.id);
|
|
315
|
-
const state = await fileState(path, agent.content);
|
|
316
|
-
if (state === "same") continue;
|
|
317
|
-
if (state === "protected") {
|
|
318
|
-
skipped.push(agent.id);
|
|
319
|
-
continue;
|
|
320
|
-
}
|
|
321
|
-
await mkdir(dirname(path), { recursive: true });
|
|
322
|
-
await writeFile(path, agent.content, "utf8");
|
|
323
|
-
installed.push(agent.id);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return { installed, skipped, changed: installed.length > 0 };
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
export type SkillInstallResult = {
|
|
330
|
-
readonly installed: boolean;
|
|
331
|
-
readonly skipped: boolean;
|
|
332
|
-
readonly changed: boolean;
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* Writes the free-pi skill as `skills/free-pi/SKILL.md`, the layout OpenCode's
|
|
337
|
-
* own skill loader reads. A file without our marker is user-owned and stays
|
|
338
|
-
* untouched, exactly like the agent files.
|
|
339
|
-
*/
|
|
340
|
-
export async function installFreePiSkill(
|
|
341
|
-
configDir = openCodeConfigDir(),
|
|
342
|
-
): Promise<SkillInstallResult> {
|
|
343
|
-
const path = freePiSkillPath(configDir);
|
|
344
|
-
const state = await fileState(path, FREE_PI_SKILL_FILE);
|
|
345
|
-
if (state === "same") {
|
|
346
|
-
return { installed: false, skipped: false, changed: false };
|
|
347
|
-
}
|
|
348
|
-
if (state === "protected") {
|
|
349
|
-
return { installed: false, skipped: true, changed: false };
|
|
350
|
-
}
|
|
351
|
-
await mkdir(dirname(path), { recursive: true });
|
|
352
|
-
await writeFile(path, FREE_PI_SKILL_FILE, "utf8");
|
|
353
|
-
return { installed: true, skipped: false, changed: true };
|
|
354
|
-
}
|
package/src/tools/subagent.ts
DELETED
|
@@ -1,271 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `free_pi_subagent_read`, a transcript reader for child sessions.
|
|
3
|
-
*
|
|
4
|
-
* OpenCode's `subagent` tool reports the child's final output and pings the
|
|
5
|
-
* parent when the child goes idle, but the parent cannot open the child's
|
|
6
|
-
* conversation afterwards. This tool fills that gap: it reads the transcript
|
|
7
|
-
* of a session that is really a child of the calling session, so the parent can
|
|
8
|
-
* see every task the subagent ran (tool calls with input and result) and quote
|
|
9
|
-
* its answer.
|
|
10
|
-
*
|
|
11
|
-
* Read-only. The session is checked against its `parentID`, so a model cannot
|
|
12
|
-
* use it to read unrelated sessions.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import type { Plugin } from "@opencode/plugin";
|
|
16
|
-
import type { Deps } from "../deps.js";
|
|
17
|
-
import { done, fail, GATED_TEXT, isGatedSession } from "./api.js";
|
|
18
|
-
|
|
19
|
-
const DEFAULT_MAX_CHARS = 60_000;
|
|
20
|
-
const MIN_MAX_CHARS = 2_000;
|
|
21
|
-
const MAX_MAX_CHARS = 200_000;
|
|
22
|
-
const TOOL_RESULT_CHARS = 2_000;
|
|
23
|
-
const TOOL_INPUT_CHARS = 1_500;
|
|
24
|
-
const USER_TEXT_CHARS = 8_000;
|
|
25
|
-
const ASSISTANT_TEXT_CHARS = 16_000;
|
|
26
|
-
|
|
27
|
-
const INPUT_SCHEMA = {
|
|
28
|
-
type: "object",
|
|
29
|
-
additionalProperties: false,
|
|
30
|
-
properties: {
|
|
31
|
-
sessionID: {
|
|
32
|
-
type: "string",
|
|
33
|
-
description:
|
|
34
|
-
"Child session to read, as returned by the `subagent` tool. Omit it to list this session's children.",
|
|
35
|
-
},
|
|
36
|
-
maxChars: {
|
|
37
|
-
type: "integer",
|
|
38
|
-
minimum: MIN_MAX_CHARS,
|
|
39
|
-
maximum: MAX_MAX_CHARS,
|
|
40
|
-
description: `Transcript budget in characters (default ${DEFAULT_MAX_CHARS}). The newest turns are kept.`,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
} as const;
|
|
44
|
-
|
|
45
|
-
type Part = {
|
|
46
|
-
type?: string;
|
|
47
|
-
text?: string;
|
|
48
|
-
synthetic?: boolean;
|
|
49
|
-
name?: string;
|
|
50
|
-
state?: {
|
|
51
|
-
status?: string;
|
|
52
|
-
input?: unknown;
|
|
53
|
-
content?: unknown;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
type Message = {
|
|
58
|
-
id?: string;
|
|
59
|
-
type?: string;
|
|
60
|
-
role?: string;
|
|
61
|
-
text?: string;
|
|
62
|
-
agent?: string;
|
|
63
|
-
finish?: string;
|
|
64
|
-
error?: unknown;
|
|
65
|
-
content?: unknown;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
function cut(value: string, limit: number): string {
|
|
69
|
-
const text = value.trim();
|
|
70
|
-
if (text.length <= limit) return text;
|
|
71
|
-
return `${text.slice(0, limit)}\n[... ${text.length - limit} characters cut]`;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function stringify(value: unknown): string {
|
|
75
|
-
if (typeof value === "string") return value;
|
|
76
|
-
try {
|
|
77
|
-
return JSON.stringify(value, null, 2) ?? "";
|
|
78
|
-
} catch {
|
|
79
|
-
return String(value ?? "");
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function textOfContent(content: unknown): string {
|
|
84
|
-
if (typeof content === "string") return content;
|
|
85
|
-
if (!Array.isArray(content)) return "";
|
|
86
|
-
return content
|
|
87
|
-
.filter(
|
|
88
|
-
(part: Part) => part?.type === "text" && part.synthetic !== true,
|
|
89
|
-
)
|
|
90
|
-
.map((part: Part) => part.text ?? "")
|
|
91
|
-
.join("\n");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function formatTool(part: Part): string {
|
|
95
|
-
const status = part.state?.status ?? "unknown";
|
|
96
|
-
const header = `[tool ${part.name ?? "?"}] ${status}`;
|
|
97
|
-
const input = stringify(part.state?.input ?? {});
|
|
98
|
-
const output = textOfContent(part.state?.content);
|
|
99
|
-
return [
|
|
100
|
-
header,
|
|
101
|
-
`input: ${cut(input, TOOL_INPUT_CHARS)}`,
|
|
102
|
-
...(output.trim() ? [`result: ${cut(output, TOOL_RESULT_CHARS)}`] : []),
|
|
103
|
-
].join("\n");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function formatMessage(message: Message): string {
|
|
107
|
-
const role = message.role ?? message.type;
|
|
108
|
-
if (role === "user") {
|
|
109
|
-
const text = message.text ?? textOfContent(message.content);
|
|
110
|
-
return text.trim() ? `[user]\n${cut(text, USER_TEXT_CHARS)}` : "";
|
|
111
|
-
}
|
|
112
|
-
if (role !== "assistant") return "";
|
|
113
|
-
|
|
114
|
-
const blocks: string[] = [];
|
|
115
|
-
const content = Array.isArray(message.content) ? message.content : [];
|
|
116
|
-
for (const part of content as Part[]) {
|
|
117
|
-
if (part?.type === "text" && part.synthetic !== true) {
|
|
118
|
-
const text = part.text ?? "";
|
|
119
|
-
if (text.trim()) blocks.push(cut(text, ASSISTANT_TEXT_CHARS));
|
|
120
|
-
} else if (part?.type === "tool") {
|
|
121
|
-
blocks.push(formatTool(part));
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
if (blocks.length === 0) return "";
|
|
125
|
-
const finish =
|
|
126
|
-
message.finish && message.finish !== "stop" ? ` (${message.finish})` : "";
|
|
127
|
-
return `[assistant${message.agent ? ` ${message.agent}` : ""}]${finish}\n${blocks.join("\n\n")}`;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type Transcript = {
|
|
131
|
-
readonly text: string;
|
|
132
|
-
readonly messages: number;
|
|
133
|
-
readonly truncated: boolean;
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Renders a session transcript, keeping the newest turns when it does not fit.
|
|
138
|
-
* The final assistant text is repeated at the end so the parent can quote it
|
|
139
|
-
* even when the middle of the transcript was dropped.
|
|
140
|
-
*/
|
|
141
|
-
export function formatTranscript(
|
|
142
|
-
messages: readonly unknown[],
|
|
143
|
-
maxChars = DEFAULT_MAX_CHARS,
|
|
144
|
-
): Transcript {
|
|
145
|
-
const budget = Math.max(MIN_MAX_CHARS, Math.min(maxChars, MAX_MAX_CHARS));
|
|
146
|
-
const entries = (messages as Message[])
|
|
147
|
-
.map(formatMessage)
|
|
148
|
-
.filter((entry) => entry.length > 0);
|
|
149
|
-
|
|
150
|
-
let text = entries.join("\n\n");
|
|
151
|
-
if (text.length <= budget) {
|
|
152
|
-
return { text, messages: entries.length, truncated: false };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const kept: string[] = [];
|
|
156
|
-
let size = 0;
|
|
157
|
-
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
158
|
-
const entry = entries[index]!;
|
|
159
|
-
if (size + entry.length + 2 > budget) break;
|
|
160
|
-
kept.unshift(entry);
|
|
161
|
-
size += entry.length + 2;
|
|
162
|
-
}
|
|
163
|
-
const dropped = entries.length - kept.length;
|
|
164
|
-
const notice = `[... ${dropped} older transcript block${dropped === 1 ? "" : "s"} omitted to stay inside ${budget} characters; pass a larger maxChars to see them]`;
|
|
165
|
-
text = [notice, ...kept].join("\n\n");
|
|
166
|
-
return { text, messages: entries.length, truncated: true };
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function listLine(session: {
|
|
170
|
-
id: string;
|
|
171
|
-
title?: string;
|
|
172
|
-
agent?: string;
|
|
173
|
-
outcome?: string;
|
|
174
|
-
updated?: number;
|
|
175
|
-
}): string {
|
|
176
|
-
const when = session.updated
|
|
177
|
-
? new Date(session.updated).toISOString()
|
|
178
|
-
: "unknown time";
|
|
179
|
-
return `- ${session.id} - ${session.title ?? "(untitled)"} - agent ${session.agent ?? "?"} - ${session.outcome ?? "unknown"} - updated ${when}`;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async function execute(
|
|
183
|
-
deps: Deps,
|
|
184
|
-
raw: unknown,
|
|
185
|
-
context: { readonly sessionID: string; readonly signal: AbortSignal },
|
|
186
|
-
) {
|
|
187
|
-
if (!isGatedSession(deps, context.sessionID)) return fail(GATED_TEXT);
|
|
188
|
-
const input = (raw ?? {}) as { sessionID?: unknown; maxChars?: unknown };
|
|
189
|
-
const requested =
|
|
190
|
-
typeof input.sessionID === "string" && input.sessionID.trim()
|
|
191
|
-
? input.sessionID.trim()
|
|
192
|
-
: undefined;
|
|
193
|
-
|
|
194
|
-
if (!requested) {
|
|
195
|
-
const children = deps.listSubagentSessions
|
|
196
|
-
? await deps.listSubagentSessions(context.sessionID).catch(() => [])
|
|
197
|
-
: [];
|
|
198
|
-
if (children.length === 0) {
|
|
199
|
-
return done(
|
|
200
|
-
"This session has no child sessions yet. The `subagent` tool returns a sessionID when a child starts.",
|
|
201
|
-
{ ok: true, children: 0 },
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
return done(
|
|
205
|
-
["Child sessions of this session:", ...children.map(listLine)].join("\n"),
|
|
206
|
-
{ ok: true, children: children.length },
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
if (!deps.readSession) {
|
|
211
|
-
return fail("Reading subagent sessions is not available in this OpenCode build.");
|
|
212
|
-
}
|
|
213
|
-
const info = await deps.readSession(requested).catch(() => undefined);
|
|
214
|
-
if (!info) return fail(`No OpenCode session with id ${requested}.`);
|
|
215
|
-
if (info.parentID !== context.sessionID) {
|
|
216
|
-
return fail(
|
|
217
|
-
`${requested} is not a subagent of this session, so its transcript cannot be read here.`,
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const messages = await deps.readContext(requested).catch(() => []);
|
|
222
|
-
if (messages.length === 0) {
|
|
223
|
-
return done(
|
|
224
|
-
`The subagent session ${requested} has no messages yet. It may still be starting.`,
|
|
225
|
-
{ ok: false, sessionID: requested, messages: 0 },
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const budget =
|
|
230
|
-
typeof input.maxChars === "number" && Number.isFinite(input.maxChars)
|
|
231
|
-
? input.maxChars
|
|
232
|
-
: DEFAULT_MAX_CHARS;
|
|
233
|
-
const transcript = formatTranscript(messages, budget);
|
|
234
|
-
const header = [
|
|
235
|
-
`Subagent session ${requested}${info.title ? ` - ${info.title}` : ""}`,
|
|
236
|
-
`agent: ${info.agent ?? "?"} - outcome: ${info.outcome ?? "running"} - stored messages: ${messages.length}`,
|
|
237
|
-
].join("\n");
|
|
238
|
-
|
|
239
|
-
deps.log.info("free_pi_subagent_read answered", {
|
|
240
|
-
sessionID: context.sessionID,
|
|
241
|
-
child: requested,
|
|
242
|
-
messages: transcript.messages,
|
|
243
|
-
truncated: transcript.truncated,
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
return done(`${header}\n\n${transcript.text}`, {
|
|
247
|
-
ok: true,
|
|
248
|
-
sessionID: requested,
|
|
249
|
-
parentID: info.parentID,
|
|
250
|
-
agent: info.agent,
|
|
251
|
-
outcome: info.outcome,
|
|
252
|
-
messages: transcript.messages,
|
|
253
|
-
truncated: transcript.truncated,
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export async function registerSubagentTool(
|
|
258
|
-
ctx: Plugin.Context,
|
|
259
|
-
deps: Deps,
|
|
260
|
-
): Promise<{ dispose: () => Promise<void> }> {
|
|
261
|
-
return ctx.tool.transform((editor) => {
|
|
262
|
-
editor.add({
|
|
263
|
-
name: "free_pi_subagent_read",
|
|
264
|
-
description:
|
|
265
|
-
"Read the transcript of a subagent session started from this session: its prompts, tool calls with inputs and results, and final answer. Pass the child sessionID returned by `subagent`, or omit it to list this session's children. Read-only; only child sessions of the calling session are readable.",
|
|
266
|
-
input: INPUT_SCHEMA,
|
|
267
|
-
options: { codemode: false },
|
|
268
|
-
execute: (input: unknown, context: any) => execute(deps, input, context),
|
|
269
|
-
} as any);
|
|
270
|
-
});
|
|
271
|
-
}
|