@open-cr-agent/runtime-opencode 0.7.0 → 0.9.0
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/effort-setup.d.ts +2 -1
- package/dist/effort-setup.js +4 -3
- package/dist/effort.d.ts +4 -1
- package/dist/effort.js +17 -3
- package/dist/opencode-config.d.ts +25 -0
- package/dist/opencode-config.js +18 -1
- package/dist/runtime.js +19 -7
- package/dist/session-outcome.d.ts +5 -1
- package/dist/session-outcome.js +20 -2
- package/dist/session-prompt.d.ts +9 -2
- package/dist/session-prompt.js +49 -31
- package/package.json +4 -4
package/dist/effort-setup.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CustomProvider } from "@open-cr-agent/core";
|
|
2
2
|
import type { OpencodeClient } from "@opencode-ai/sdk/v2";
|
|
3
3
|
import { EffortRoutes } from "./effort.js";
|
|
4
4
|
type ConfigApi = Pick<OpencodeClient["config"], "providers">;
|
|
@@ -8,6 +8,7 @@ export interface EffortSetup {
|
|
|
8
8
|
probe: ConfigApi;
|
|
9
9
|
workspace: ConfigApi;
|
|
10
10
|
file: string;
|
|
11
|
+
secrets?: readonly string[];
|
|
11
12
|
}
|
|
12
13
|
export declare function setUpEfforts(setup: EffortSetup): Promise<EffortRoutes>;
|
|
13
14
|
export {};
|
package/dist/effort-setup.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { errorMessage, withoutSecrets } from "@open-cr-agent/core";
|
|
2
3
|
import { EffortRoutes } from "./effort.js";
|
|
3
4
|
const CATALOG_TIMEOUT_MS = 30_000;
|
|
4
5
|
// The variants derive thinking budgets from the models' output limits,
|
|
5
6
|
// which only OpenCode's catalog knows, and the catalog is loaded only once
|
|
6
7
|
// OpenCode runs: the variants are written after it started and before the
|
|
7
8
|
// workspace is first used. Any failure leaves effort unsent, with a warning
|
|
8
|
-
// per agent, and never fails the review.
|
|
9
|
+
// per agent that gives the cause, and never fails the review.
|
|
9
10
|
export async function setUpEfforts(setup) {
|
|
10
11
|
const { models, custom = {} } = setup;
|
|
11
12
|
const everyModel = new Map(models.map((m) => [m, 0]));
|
|
@@ -23,8 +24,8 @@ export async function setUpEfforts(setup) {
|
|
|
23
24
|
routes.keepLoaded(new Map([...loaded].map(([model, entry]) => [model, entry.variants])));
|
|
24
25
|
return routes;
|
|
25
26
|
}
|
|
26
|
-
catch {
|
|
27
|
-
return
|
|
27
|
+
catch (error) {
|
|
28
|
+
return EffortRoutes.unavailable(`OpenCode's model catalog could not be read (${withoutSecrets(errorMessage(error), setup.secrets ?? [])})`);
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
async function catalogModels(config) {
|
package/dist/effort.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ type Options = Record<string, unknown>;
|
|
|
3
3
|
export declare function effortOptions(model: string, level: Effort, custom?: Readonly<Record<string, CustomProvider>>, outputLimit?: number): Options | undefined;
|
|
4
4
|
export declare class EffortRoutes {
|
|
5
5
|
private readonly routes;
|
|
6
|
+
unsentBecause?: string;
|
|
7
|
+
/** No routes at all, and why. */
|
|
8
|
+
static unavailable(reason: string): EffortRoutes;
|
|
6
9
|
constructor(models: readonly string[], custom?: Readonly<Record<string, CustomProvider>>, known?: ReadonlyMap<string, number>);
|
|
7
10
|
get empty(): boolean;
|
|
8
11
|
providerConfig(): Record<string, {
|
|
@@ -19,7 +22,7 @@ export declare class AppliedEfforts {
|
|
|
19
22
|
private readonly applied;
|
|
20
23
|
private readonly sampling;
|
|
21
24
|
constructor(sampling?: Sampling);
|
|
22
|
-
record(agent: string, effort: Effort, model: string, sent: boolean): boolean;
|
|
25
|
+
record(agent: string, effort: Effort, model: string, sent: boolean, unsentBecause?: string): boolean;
|
|
23
26
|
appliedTo(agent: string): AppliedSettings | undefined;
|
|
24
27
|
private requested;
|
|
25
28
|
}
|
package/dist/effort.js
CHANGED
|
@@ -59,6 +59,14 @@ export function effortOptions(model, level, custom = {}, outputLimit) {
|
|
|
59
59
|
// accept it at a price of 0.
|
|
60
60
|
export class EffortRoutes {
|
|
61
61
|
routes = new Map();
|
|
62
|
+
// Why no level can be sent to any model, when the routes could not be set up.
|
|
63
|
+
unsentBecause;
|
|
64
|
+
/** No routes at all, and why. */
|
|
65
|
+
static unavailable(reason) {
|
|
66
|
+
const routes = new EffortRoutes([]);
|
|
67
|
+
routes.unsentBecause = reason;
|
|
68
|
+
return routes;
|
|
69
|
+
}
|
|
62
70
|
constructor(models, custom = {},
|
|
63
71
|
// Output limits by model, from OpenCode's catalog; 0 when it has none.
|
|
64
72
|
known = new Map()) {
|
|
@@ -123,17 +131,23 @@ export class AppliedEfforts {
|
|
|
123
131
|
constructor(sampling = {}) {
|
|
124
132
|
this.sampling = sampling;
|
|
125
133
|
}
|
|
126
|
-
// Whether the call keeps the configured sampling.
|
|
127
|
-
|
|
134
|
+
// Whether the call keeps the configured sampling. `unsentBecause`: why
|
|
135
|
+
// the level could not be sent, when it is not the capability table.
|
|
136
|
+
record(agent, effort, model, sent, unsentBecause) {
|
|
128
137
|
const keepsSampling = effort === "none";
|
|
129
138
|
const before = this.applied.get(agent);
|
|
130
139
|
const notApplied = [
|
|
131
140
|
...new Set([...(before?.notApplied ?? []), ...(keepsSampling ? [] : this.requested())]),
|
|
132
141
|
];
|
|
133
|
-
const
|
|
142
|
+
const tableSays = !sent && unsentBecause === undefined;
|
|
143
|
+
const unsupported = [
|
|
144
|
+
...new Set([...(before?.unsupported ?? []), ...(tableSays ? [model] : [])]),
|
|
145
|
+
];
|
|
146
|
+
const because = before?.unsentBecause ?? (sent ? undefined : unsentBecause);
|
|
134
147
|
this.applied.set(agent, {
|
|
135
148
|
effort: (before?.effort ?? true) && sent,
|
|
136
149
|
...(unsupported.length > 0 ? { unsupported } : {}),
|
|
150
|
+
...(because !== undefined ? { unsentBecause: because } : {}),
|
|
137
151
|
...(notApplied.length > 0 ? { notApplied } : {}),
|
|
138
152
|
});
|
|
139
153
|
return keepsSampling;
|
|
@@ -3,12 +3,14 @@ import type { ToolServer } from "./tool-server.js";
|
|
|
3
3
|
export declare const MCP_SERVER = "ocra";
|
|
4
4
|
export declare const REVIEW_AGENT = "ocra-reviewer";
|
|
5
5
|
export declare const HELPER_AGENT = "ocra-helper";
|
|
6
|
+
export declare const WRAP_UP_AGENT = "ocra-wrap-up";
|
|
6
7
|
export declare const WITHOUT_SAMPLING = "-effort";
|
|
7
8
|
export declare const HELPER_AGENT_STEPS = 2;
|
|
8
9
|
export declare const OPENCODE_BUILTIN_TOOLS: readonly ["invalid", "question", "bash", "read", "glob", "grep", "edit", "write", "task", "webfetch", "todowrite", "websearch", "skill", "apply_patch"];
|
|
9
10
|
export declare const DISABLED_BUILTINS: {
|
|
10
11
|
[k: string]: boolean;
|
|
11
12
|
};
|
|
13
|
+
export declare function wrapUpTools(helperTools: Record<string, boolean>): Record<string, boolean>;
|
|
12
14
|
export declare function openCodeSampling(sampling?: Sampling): AppliedSampling;
|
|
13
15
|
export declare function openCodeConfig(tools: Pick<ToolServer, "url" | "headers">, helperTools: Record<string, boolean>, custom?: Readonly<Record<string, CustomProvider>>, sampling?: Sampling): {
|
|
14
16
|
share: string;
|
|
@@ -68,6 +70,17 @@ export declare function openCodeConfig(tools: Pick<ToolServer, "url" | "headers"
|
|
|
68
70
|
webfetch: string;
|
|
69
71
|
skill: string;
|
|
70
72
|
};
|
|
73
|
+
} | {
|
|
74
|
+
mode: string;
|
|
75
|
+
prompt: string;
|
|
76
|
+
steps: number;
|
|
77
|
+
tools: Record<string, boolean>;
|
|
78
|
+
permission: {
|
|
79
|
+
edit: string;
|
|
80
|
+
bash: string;
|
|
81
|
+
webfetch: string;
|
|
82
|
+
skill: string;
|
|
83
|
+
};
|
|
71
84
|
};
|
|
72
85
|
} | {
|
|
73
86
|
"ocra-reviewer": {
|
|
@@ -96,5 +109,17 @@ export declare function openCodeConfig(tools: Pick<ToolServer, "url" | "headers"
|
|
|
96
109
|
skill: string;
|
|
97
110
|
};
|
|
98
111
|
};
|
|
112
|
+
"ocra-wrap-up": {
|
|
113
|
+
mode: string;
|
|
114
|
+
prompt: string;
|
|
115
|
+
steps: number;
|
|
116
|
+
tools: Record<string, boolean>;
|
|
117
|
+
permission: {
|
|
118
|
+
edit: string;
|
|
119
|
+
bash: string;
|
|
120
|
+
webfetch: string;
|
|
121
|
+
skill: string;
|
|
122
|
+
};
|
|
123
|
+
};
|
|
99
124
|
};
|
|
100
125
|
};
|
package/dist/opencode-config.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { MAX_AGENT_STEPS, OcraError, } from "@open-cr-agent/core";
|
|
1
|
+
import { MAX_AGENT_STEPS, OcraError, WRAP_UP_STEPS, WRAP_UP_TOOLS, } from "@open-cr-agent/core";
|
|
2
2
|
export const MCP_SERVER = "ocra";
|
|
3
3
|
export const REVIEW_AGENT = "ocra-reviewer";
|
|
4
4
|
export const HELPER_AGENT = "ocra-helper";
|
|
5
|
+
// A review's wrap-up turn (WRAP_UP_MESSAGE): its own agent, since OpenCode
|
|
6
|
+
// caps steps per agent, not per prompt.
|
|
7
|
+
export const WRAP_UP_AGENT = "ocra-wrap-up";
|
|
5
8
|
// The same agents without the configured temperature, for calls that send
|
|
6
9
|
// an effort (ADR-0025): OpenCode sets sampling per agent.
|
|
7
10
|
export const WITHOUT_SAMPLING = "-effort";
|
|
@@ -31,6 +34,13 @@ export const OPENCODE_BUILTIN_TOOLS = [
|
|
|
31
34
|
"apply_patch",
|
|
32
35
|
];
|
|
33
36
|
export const DISABLED_BUILTINS = Object.fromEntries(OPENCODE_BUILTIN_TOOLS.map((t) => [t, false]));
|
|
37
|
+
// The helper's tools (every one off) with only the reporting tools on.
|
|
38
|
+
export function wrapUpTools(helperTools) {
|
|
39
|
+
return {
|
|
40
|
+
...helperTools,
|
|
41
|
+
...Object.fromEntries(WRAP_UP_TOOLS.map((t) => [`${MCP_SERVER}_${t}`, true])),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
34
44
|
// OpenCode 1.18.32 takes a temperature per agent and sends it when the
|
|
35
45
|
// model's catalog entry says the model accepts one; a model declared in
|
|
36
46
|
// configuration is marked so (providerConfig). It has no seed setting: the
|
|
@@ -59,6 +69,13 @@ export function openCodeConfig(tools, helperTools, custom = {}, sampling = {}) {
|
|
|
59
69
|
tools: helperTools,
|
|
60
70
|
permission,
|
|
61
71
|
},
|
|
72
|
+
[WRAP_UP_AGENT]: {
|
|
73
|
+
mode: "primary",
|
|
74
|
+
prompt: REVIEW_AGENT_PROMPT,
|
|
75
|
+
steps: WRAP_UP_STEPS,
|
|
76
|
+
tools: wrapUpTools(helperTools),
|
|
77
|
+
permission,
|
|
78
|
+
},
|
|
62
79
|
};
|
|
63
80
|
return {
|
|
64
81
|
share: "disabled",
|
package/dist/runtime.js
CHANGED
|
@@ -2,12 +2,12 @@ import { rmSync } from "node:fs";
|
|
|
2
2
|
import { mkdir, mkdtemp, rm } from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { ChainRunner, MAX_AGENT_STEPS, OcraError, parseModel, RESUME_MESSAGE, REVIEW_TOOLS, reviewTools, withoutSecrets, } from "@open-cr-agent/core";
|
|
5
|
+
import { ChainRunner, MAX_AGENT_STEPS, OcraError, parseModel, RESUME_MESSAGE, REVIEW_TOOLS, reviewTools, WRAP_UP_MESSAGE, withoutSecrets, } from "@open-cr-agent/core";
|
|
6
6
|
import { createOpencodeClient } from "@opencode-ai/sdk/v2";
|
|
7
7
|
import { resolveOpencodeBinary } from "./binary.js";
|
|
8
8
|
import { AppliedEfforts } from "./effort.js";
|
|
9
9
|
import { setUpEfforts } from "./effort-setup.js";
|
|
10
|
-
import { DISABLED_BUILTINS, HELPER_AGENT, MCP_SERVER, openCodeConfig, openCodeSampling, REVIEW_AGENT, WITHOUT_SAMPLING, } from "./opencode-config.js";
|
|
10
|
+
import { DISABLED_BUILTINS, HELPER_AGENT, MCP_SERVER, openCodeConfig, openCodeSampling, REVIEW_AGENT, WITHOUT_SAMPLING, WRAP_UP_AGENT, wrapUpTools, } from "./opencode-config.js";
|
|
11
11
|
import { startOpencodeServer } from "./opencode-server.js";
|
|
12
12
|
import { credentialValues, missingCredentials, serverEnv } from "./server-env.js";
|
|
13
13
|
import { promptSession } from "./session-prompt.js";
|
|
@@ -79,6 +79,11 @@ export class OpenCodeRuntime {
|
|
|
79
79
|
tools: DISABLED_BUILTINS,
|
|
80
80
|
toolPrefix: `${MCP_SERVER}_`,
|
|
81
81
|
resume: REVIEW_RESUME,
|
|
82
|
+
wrapUp: {
|
|
83
|
+
...this.effortCall(infra, WRAP_UP_AGENT, spec.reviewer, spec.effort, model),
|
|
84
|
+
tools: wrapUpTools(this.helperTools),
|
|
85
|
+
message: WRAP_UP_MESSAGE,
|
|
86
|
+
},
|
|
82
87
|
}, signal, onUsage);
|
|
83
88
|
}
|
|
84
89
|
async attemptCompletion(model, request, signal) {
|
|
@@ -99,7 +104,7 @@ export class OpenCodeRuntime {
|
|
|
99
104
|
if (effort === undefined)
|
|
100
105
|
return { agent: base };
|
|
101
106
|
const route = infra.efforts.route(model, effort);
|
|
102
|
-
const keepsSampling = this.applied.record(agent, effort, model, route !== undefined);
|
|
107
|
+
const keepsSampling = this.applied.record(agent, effort, model, route !== undefined, infra.efforts.unsentBecause);
|
|
103
108
|
return {
|
|
104
109
|
agent: keepsSampling || this.options.sampling?.temperature === undefined
|
|
105
110
|
? base
|
|
@@ -107,8 +112,9 @@ export class OpenCodeRuntime {
|
|
|
107
112
|
...(route?.variant ? { variant: route.variant } : {}),
|
|
108
113
|
};
|
|
109
114
|
}
|
|
110
|
-
prompt(infra, input, signal, onUsage) {
|
|
111
|
-
|
|
115
|
+
async prompt(infra, input, signal, onUsage) {
|
|
116
|
+
const attempt = await promptSession(infra.client.session, input, `${MCP_SERVER}_${REVIEW_TOOLS.reportFinding}`, signal, onUsage ? { onUsage } : {});
|
|
117
|
+
const redacted = (outcome) => outcome.error
|
|
112
118
|
? {
|
|
113
119
|
...outcome,
|
|
114
120
|
error: {
|
|
@@ -116,7 +122,12 @@ export class OpenCodeRuntime {
|
|
|
116
122
|
message: withoutSecrets(outcome.error.message, infra.secrets),
|
|
117
123
|
},
|
|
118
124
|
}
|
|
119
|
-
: outcome
|
|
125
|
+
: outcome;
|
|
126
|
+
const { wrapUp } = attempt;
|
|
127
|
+
return {
|
|
128
|
+
...redacted(attempt),
|
|
129
|
+
...(wrapUp ? { wrapUp: async () => redacted(await wrapUp()) } : {}),
|
|
130
|
+
};
|
|
120
131
|
}
|
|
121
132
|
// The server holds the credentials of the providers it started with; an
|
|
122
133
|
// agent's own chain naming another would fail at OpenCode as "model not
|
|
@@ -173,14 +184,15 @@ export class OpenCodeRuntime {
|
|
|
173
184
|
rmSync(root, { recursive: true, force: true });
|
|
174
185
|
};
|
|
175
186
|
process.on("exit", onExit);
|
|
187
|
+
const secrets = credentialValues(this.options.env, providersOf(this.options), custom);
|
|
176
188
|
const efforts = await setUpEfforts({
|
|
177
189
|
models: chainModels(this.options),
|
|
178
190
|
custom,
|
|
179
191
|
probe: clientFor(probe).config,
|
|
180
192
|
workspace: client.config,
|
|
181
193
|
file: join(dirs.config, "opencode.json"),
|
|
194
|
+
secrets,
|
|
182
195
|
});
|
|
183
|
-
const secrets = credentialValues(this.options.env, providersOf(this.options), custom);
|
|
184
196
|
return { root, tools, server, client, efforts, dispatcher, onExit, secrets };
|
|
185
197
|
}
|
|
186
198
|
catch (error) {
|
|
@@ -32,7 +32,11 @@ declare const sessionMessageSchema: z.ZodObject<{
|
|
|
32
32
|
}, z.core.$strip>>;
|
|
33
33
|
}, z.core.$strip>;
|
|
34
34
|
export type SessionMessage = z.output<typeof sessionMessageSchema>;
|
|
35
|
-
export declare function parseSessionMessages(data: unknown):
|
|
35
|
+
export declare function parseSessionMessages(data: unknown): {
|
|
36
|
+
messages: SessionMessage[];
|
|
37
|
+
unread: number;
|
|
38
|
+
};
|
|
39
|
+
export declare function readSession(data: unknown, reportTool: string, toolPrefix?: string): AttemptOutcome;
|
|
36
40
|
export declare function summarizeSession(messages: readonly SessionMessage[], reportTool: string, toolPrefix?: string): AttemptOutcome;
|
|
37
41
|
export declare function sessionUsage(messages: readonly SessionMessage[]): Usage;
|
|
38
42
|
export {};
|
package/dist/session-outcome.js
CHANGED
|
@@ -35,9 +35,27 @@ const sessionMessageSchema = z.object({
|
|
|
35
35
|
state: z.object({ status: z.string().optional(), input: z.unknown().optional() }).optional(),
|
|
36
36
|
})),
|
|
37
37
|
});
|
|
38
|
-
//
|
|
38
|
+
// The messages ocra can read, and how many it could not: one message in an
|
|
39
|
+
// unexpected shape must not cost the attempt its findings and usage. Throws
|
|
40
|
+
// when the answer is not a list.
|
|
39
41
|
export function parseSessionMessages(data) {
|
|
40
|
-
|
|
42
|
+
const messages = [];
|
|
43
|
+
let unread = 0;
|
|
44
|
+
for (const raw of z.array(z.unknown()).parse(data ?? [])) {
|
|
45
|
+
const parsed = sessionMessageSchema.safeParse(raw);
|
|
46
|
+
if (parsed.success)
|
|
47
|
+
messages.push(parsed.data);
|
|
48
|
+
else
|
|
49
|
+
unread += 1;
|
|
50
|
+
}
|
|
51
|
+
return { messages, unread };
|
|
52
|
+
}
|
|
53
|
+
// The attempt as the session's messages tell it, with the count of those
|
|
54
|
+
// that could not be read.
|
|
55
|
+
export function readSession(data, reportTool, toolPrefix = "") {
|
|
56
|
+
const { messages, unread } = parseSessionMessages(data);
|
|
57
|
+
const outcome = summarizeSession(messages, reportTool, toolPrefix);
|
|
58
|
+
return unread > 0 ? { ...outcome, unreadMessages: unread } : outcome;
|
|
41
59
|
}
|
|
42
60
|
const AUTH_STATUS = new Set([401, 403]);
|
|
43
61
|
const AUTH_MESSAGE = /api key|unauthori[sz]ed|permission denied|forbidden/i;
|
package/dist/session-prompt.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type TaskAttempt, type Usage } from "@open-cr-agent/core";
|
|
2
2
|
import type { OpencodeClient } from "@opencode-ai/sdk/v2";
|
|
3
3
|
export interface ActivityOptions {
|
|
4
4
|
inactivityMs?: number;
|
|
@@ -15,12 +15,19 @@ export interface PromptInput {
|
|
|
15
15
|
tools: Record<string, boolean>;
|
|
16
16
|
toolPrefix?: string;
|
|
17
17
|
resume?: ResumeOptions;
|
|
18
|
+
wrapUp?: Turn;
|
|
18
19
|
}
|
|
19
20
|
interface ResumeOptions {
|
|
20
21
|
doneTool: string;
|
|
21
22
|
maxSteps: number;
|
|
22
23
|
message: string;
|
|
23
24
|
}
|
|
25
|
+
interface Turn {
|
|
26
|
+
agent: string;
|
|
27
|
+
variant?: string;
|
|
28
|
+
tools: Record<string, boolean>;
|
|
29
|
+
message: string;
|
|
30
|
+
}
|
|
24
31
|
type SessionApi = Pick<OpencodeClient["session"], "create" | "prompt" | "messages" | "abort">;
|
|
25
|
-
export declare function promptSession(session: SessionApi, input: PromptInput, reportTool: string, signal: AbortSignal, activity?: ActivityOptions): Promise<
|
|
32
|
+
export declare function promptSession(session: SessionApi, input: PromptInput, reportTool: string, signal: AbortSignal, activity?: ActivityOptions): Promise<TaskAttempt>;
|
|
26
33
|
export {};
|
package/dist/session-prompt.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { emptyUsage, errorMessage, OcraError, parseModel, } from "@open-cr-agent/core";
|
|
2
|
-
import { parseSessionMessages,
|
|
2
|
+
import { parseSessionMessages, readSession, sessionUsage, } from "./session-outcome.js";
|
|
3
3
|
// A session that was cut off has still spent tokens and may have reported
|
|
4
4
|
// findings; this bounds the one extra request that collects them.
|
|
5
5
|
const HARVEST_TIMEOUT_MS = 5_000;
|
|
@@ -35,20 +35,60 @@ export async function promptSession(session, input, reportTool, signal, activity
|
|
|
35
35
|
if (!created.data) {
|
|
36
36
|
throw new OcraError("RUNTIME_FAILED", `OpenCode could not create a session: ${JSON.stringify(created.error)}`);
|
|
37
37
|
}
|
|
38
|
-
const
|
|
38
|
+
const opened = {
|
|
39
|
+
session,
|
|
40
|
+
sessionID: created.data.id,
|
|
41
|
+
input,
|
|
42
|
+
reportTool,
|
|
43
|
+
signal,
|
|
44
|
+
activity,
|
|
45
|
+
};
|
|
46
|
+
const turn = (t) => runTurn(opened, t);
|
|
47
|
+
const review = {
|
|
48
|
+
agent: input.agent,
|
|
49
|
+
...(input.variant ? { variant: input.variant } : {}),
|
|
50
|
+
tools: input.tools,
|
|
51
|
+
};
|
|
52
|
+
let outcome = await turn({ ...review, message: input.user });
|
|
53
|
+
let lastTurnFrom = 0;
|
|
54
|
+
if (!outcome.error && input.resume && stoppedEarly(outcome, input.resume)) {
|
|
55
|
+
lastTurnFrom = outcome.steps;
|
|
56
|
+
// Both turns are in the session: their findings and their spend.
|
|
57
|
+
outcome = { ...(await turn({ ...review, message: input.resume.message })), resumed: true };
|
|
58
|
+
}
|
|
59
|
+
if (input.resume)
|
|
60
|
+
outcome = capped(outcome, outcome.steps - lastTurnFrom, input.resume);
|
|
61
|
+
const { wrapUp } = input;
|
|
62
|
+
if (!wrapUp)
|
|
63
|
+
return outcome;
|
|
64
|
+
const resumed = outcome.resumed;
|
|
65
|
+
return {
|
|
66
|
+
...outcome,
|
|
67
|
+
wrapUp: async () => ({ ...(await turn(wrapUp)), ...(resumed ? { resumed } : {}) }),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// Sends one turn and returns what the whole session has come to, every
|
|
71
|
+
// earlier turn included.
|
|
72
|
+
async function runTurn({ session, sessionID, input, reportTool, signal, activity }, turn) {
|
|
39
73
|
const stop = () => void session.abort({ sessionID }).catch(() => { });
|
|
74
|
+
if (signal.aborted) {
|
|
75
|
+
return {
|
|
76
|
+
...(await harvest(session, sessionID, reportTool, input.toolPrefix)),
|
|
77
|
+
error: { message: "cancelled", retryable: false },
|
|
78
|
+
};
|
|
79
|
+
}
|
|
40
80
|
signal.addEventListener("abort", stop, { once: true });
|
|
41
81
|
const silence = watchActivity(session, sessionID, activity);
|
|
42
82
|
const attempt = AbortSignal.any([signal, silence.signal]);
|
|
43
83
|
try {
|
|
44
84
|
const response = await session.prompt({
|
|
45
85
|
sessionID,
|
|
46
|
-
agent:
|
|
86
|
+
agent: turn.agent,
|
|
47
87
|
model: parseModel(input.model),
|
|
48
|
-
...(
|
|
88
|
+
...(turn.variant ? { variant: turn.variant } : {}),
|
|
49
89
|
system: input.system,
|
|
50
|
-
tools:
|
|
51
|
-
parts: [{ type: "text", text:
|
|
90
|
+
tools: turn.tools,
|
|
91
|
+
parts: [{ type: "text", text: turn.message }],
|
|
52
92
|
}, { signal: attempt });
|
|
53
93
|
// The session ran even when the answer is an error, and it may have
|
|
54
94
|
// spent tokens and reported findings: keep them.
|
|
@@ -58,10 +98,9 @@ export async function promptSession(session, input, reportTool, signal, activity
|
|
|
58
98
|
error: { message: JSON.stringify(response.error), retryable: false },
|
|
59
99
|
};
|
|
60
100
|
}
|
|
61
|
-
let outcome;
|
|
62
101
|
try {
|
|
63
102
|
const messages = await session.messages({ sessionID }, { signal: attempt });
|
|
64
|
-
|
|
103
|
+
return readSession(messages.data, reportTool, input.toolPrefix);
|
|
65
104
|
}
|
|
66
105
|
catch (error) {
|
|
67
106
|
// The session finished; running it again on the next model would pay
|
|
@@ -74,27 +113,6 @@ export async function promptSession(session, input, reportTool, signal, activity
|
|
|
74
113
|
},
|
|
75
114
|
};
|
|
76
115
|
}
|
|
77
|
-
if (!input.resume)
|
|
78
|
-
return outcome;
|
|
79
|
-
if (!stoppedEarly(outcome, input.resume))
|
|
80
|
-
return capped(outcome, outcome.steps, input.resume);
|
|
81
|
-
const again = await session.prompt({
|
|
82
|
-
sessionID,
|
|
83
|
-
agent: input.agent,
|
|
84
|
-
model: parseModel(input.model),
|
|
85
|
-
...(input.variant ? { variant: input.variant } : {}),
|
|
86
|
-
system: input.system,
|
|
87
|
-
tools: input.tools,
|
|
88
|
-
parts: [{ type: "text", text: input.resume.message }],
|
|
89
|
-
}, { signal: attempt });
|
|
90
|
-
// Both turns are in the session: their findings and their spend.
|
|
91
|
-
const resumed = {
|
|
92
|
-
...(await harvest(session, sessionID, reportTool, input.toolPrefix)),
|
|
93
|
-
resumed: true,
|
|
94
|
-
};
|
|
95
|
-
if (again.error)
|
|
96
|
-
resumed.error = { message: JSON.stringify(again.error), retryable: false };
|
|
97
|
-
return capped(resumed, resumed.steps - outcome.steps, input.resume);
|
|
98
116
|
}
|
|
99
117
|
catch (error) {
|
|
100
118
|
// Aborted or cut off by the transport: OpenCode may still be running the
|
|
@@ -131,7 +149,7 @@ function watchActivity(session, sessionID, options) {
|
|
|
131
149
|
polling = true;
|
|
132
150
|
try {
|
|
133
151
|
const messages = await session.messages({ sessionID }, { signal: AbortSignal.timeout(HARVEST_TIMEOUT_MS) });
|
|
134
|
-
const list = parseSessionMessages(messages.data);
|
|
152
|
+
const list = parseSessionMessages(messages.data).messages;
|
|
135
153
|
if (options.onUsage) {
|
|
136
154
|
const spent = sessionUsage(list);
|
|
137
155
|
if (spent.costUsd > reported.costUsd || spent.inputTokens > reported.inputTokens) {
|
|
@@ -167,7 +185,7 @@ function activitySignature(messages) {
|
|
|
167
185
|
async function harvest(session, sessionID, reportTool, toolPrefix) {
|
|
168
186
|
try {
|
|
169
187
|
const messages = await session.messages({ sessionID }, { signal: AbortSignal.timeout(HARVEST_TIMEOUT_MS) });
|
|
170
|
-
return
|
|
188
|
+
return readSession(messages.data, reportTool, toolPrefix);
|
|
171
189
|
}
|
|
172
190
|
catch {
|
|
173
191
|
return emptyOutcome();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-cr-agent/runtime-opencode",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "OpenCode-based agent runtime for Open-CR-Agent",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-review",
|
|
@@ -34,11 +34,11 @@
|
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@modelcontextprotocol/sdk": "^1.31.0",
|
|
37
|
-
"@open-cr-agent/core": "0.
|
|
37
|
+
"@open-cr-agent/core": "0.9.0",
|
|
38
38
|
"@opencode-ai/sdk": "1.18.32",
|
|
39
39
|
"opencode-ai": "1.18.32",
|
|
40
|
-
"undici": "^8.
|
|
41
|
-
"zod": "^4.6.
|
|
40
|
+
"undici": "^8.12.3",
|
|
41
|
+
"zod": "^4.6.6"
|
|
42
42
|
},
|
|
43
43
|
"publishConfig": {
|
|
44
44
|
"access": "public"
|