Official
opencode-v2
OpenCode 2 agent provider: runs opencode serve (2.x) and talks to its /api over HTTP and server-sent events.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/opencode-v2@0.1.5
Permissions in 0.1.5
Files
history.ts4.3 KB
import * as Effect from "effect/Effect";
import type { PluginServices } from "divergence/effect";
import type { PlanEntry, TaskStatus, TranscriptItem } from "divergence/protocol";
import type { OpenCodeV2Agent } from "./agent.ts";
import * as api from "./api.ts";
import * as map from "./map.ts";
const MAX_SUBAGENT_DEPTH = 8;
/** Reads child executions independently of the tool calls that launched them. */
export const transcriptOf: (
agent: OpenCodeV2Agent,
session: string,
active: Record<string, unknown>,
depth?: number,
parent?: string | null,
) => Effect.Effect<{ items: TranscriptItem[]; plan: PlanEntry[] | null }, unknown, PluginServices> = Effect.fn("OpenCodeV2.transcriptOf")(function* (
agent, session, active, depth = 0, parent = null,
) {
const messages = yield* agent.messages(session);
const reports = new Map<string, api.Message & { text: string; metadata: Record<string, unknown> }>();
for (const message of messages) {
const parsed = api.syntheticMessage.safeParse(message);
const metadata = parsed.success ? parsed.data.metadata : undefined;
if (parsed.success && metadata?.source === "subagent" && typeof metadata.childID === "string")
reports.set(metadata.childID, { ...parsed.data, metadata });
}
const subagents = new Map<string, map.Subagent>();
let plan: PlanEntry[] | null = null;
for (const message of messages) {
const assistant = api.assistantMessage.safeParse(message);
if (!assistant.success) continue;
for (const entry of assistant.data.content) {
if (entry.type !== "tool") continue;
if (entry.state.status === "completed") plan = map.todoPlan(entry.name, entry.state.metadata, agent.hostFeatures) ?? plan;
if (depth >= MAX_SUBAGENT_DEPTH || map.toolKind(entry.name) !== "task" || entry.state.status === "streaming") continue;
const child = entry.state.metadata?.sessionID;
if (typeof child !== "string") continue;
const head = (yield* agent.call("GET", `/api/session/${child}`, api.single(api.session))).data;
const { items } = yield* transcriptOf(agent, child, active, depth + 1, child);
const report = reports.get(child);
const launched = entry.time?.ran ?? entry.time?.created ?? message.time.created;
const current = report && report.time.created >= Math.max(head.time.busy ?? 0, launched) ? report : undefined;
const outcome = head.time.idle !== undefined && head.time.idle < launched ? undefined : head.outcome;
const running = Object.hasOwn(active, child) || (head.time.busy !== undefined && head.time.busy > (head.time.idle ?? 0));
const state = current?.metadata.state;
const status: TaskStatus = running ? "running"
: outcome === "failed" ? "failed"
: outcome === "interrupted" ? "cancelled"
: outcome === "succeeded" ? "completed"
: state === "completed" ? "completed"
: state === "cancelled" ? "cancelled"
: state === "error" ? "failed"
: entry.state.metadata?.status === "completed" ? "completed"
: entry.state.status === "error" ? "failed" : "running";
const input = entry.state.input;
// Older foreground tools carry their final report in the result itself.
const result = !running && current ? current.text
: !running && entry.state.status === "completed" && entry.state.metadata?.status === "completed"
? map.resultText(entry.state.content) : "";
const id = map.toolItemId(assistant.data.id, entry.id);
subagents.set(id, {
task: {
id: child,
title: typeof input.description === "string" && input.description ? input.description : "Subagent",
status,
toolCallId: id,
...(parent ? { parentTaskId: parent } : {}),
...(typeof input.agent === "string" ? { name: input.agent } : {}),
...(typeof input.prompt === "string" ? { prompt: input.prompt } : {}),
...(entry.state.metadata?.status === "running" || input.background === true ? { background: true } : {}),
...(result ? { summary: map.subagentResult(result) } : {}),
toolUses: items.filter((item) => item.role === "tool").length,
},
items,
});
}
}
return { items: map.transcript(messages, (messageId, callId) => subagents.get(map.toolItemId(messageId, callId)) ?? null), plan };
});
No comments yet.