@open-cr-agent/runtime-direct 0.6.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/README.md +1 -1
- package/dist/effort.d.ts +0 -1
- package/dist/effort.js +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/loop.d.ts +9 -3
- package/dist/loop.js +96 -61
- package/dist/openai.d.ts +19 -4
- package/dist/openai.js +9 -4
- package/dist/plugin.d.ts +0 -1
- package/dist/plugin.js +0 -1
- package/dist/record.d.ts +16 -0
- package/dist/record.js +96 -0
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.js +26 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
# @open-cr-agent/runtime-direct
|
|
2
2
|
|
|
3
|
-
The agent runtime of [Open-CR-Agent](https://github.com/jma49/Open-CR-Agent), the multi-agent code reviewer, that calls the OpenAI-compatible
|
|
3
|
+
The agent runtime of [Open-CR-Agent](https://github.com/jma49/Open-CR-Agent), the multi-agent code reviewer, that calls the OpenAI-compatible endpoints declared in its configuration itself, with nothing else on the network. Most users want [`@open-cr-agent/cli`](https://www.npmjs.com/package/@open-cr-agent/cli) (the `ocra` command) and the [manual](https://ocracloud.com). All packages share one version. Apache-2.0.
|
package/dist/effort.d.ts
CHANGED
package/dist/effort.js
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/loop.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type ModelPrice, type ReviewContext, type TaskAttempt, type ToolDefinition, type Usage } from "@open-cr-agent/core";
|
|
2
2
|
import { type CallParams, type Endpoint } from "./openai.js";
|
|
3
3
|
export interface LoopInput {
|
|
4
4
|
endpoint: Endpoint;
|
|
@@ -10,11 +10,17 @@ export interface LoopInput {
|
|
|
10
10
|
context: ReviewContext;
|
|
11
11
|
maxSteps: number;
|
|
12
12
|
resume?: string;
|
|
13
|
+
wrapUp?: WrapUpTurn;
|
|
13
14
|
timeoutMs: number;
|
|
14
15
|
params?: CallParams;
|
|
15
16
|
onEffortRefused?: () => void;
|
|
16
17
|
signal: AbortSignal;
|
|
17
18
|
onUsage?: (spent: Usage) => void;
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
interface WrapUpTurn {
|
|
21
|
+
message: string;
|
|
22
|
+
tools: readonly string[];
|
|
23
|
+
steps: number;
|
|
24
|
+
}
|
|
25
|
+
export declare function runLoop(input: LoopInput): Promise<TaskAttempt>;
|
|
26
|
+
export {};
|
package/dist/loop.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { addUsage, emptyUsage, errorMessage, REVIEW_TOOLS, } from "@open-cr-agent/core";
|
|
1
|
+
import { addUsage, emptyUsage, errorMessage, exploredBy, REVIEW_TOOLS, } from "@open-cr-agent/core";
|
|
2
2
|
import { chat, toolSpec, withoutEffort, } from "./openai.js";
|
|
3
3
|
// The agent loop: ask the model, run the tools it calls against the review
|
|
4
4
|
// context, hand the results back, until it calls task_done, answers without
|
|
@@ -6,79 +6,114 @@ import { chat, toolSpec, withoutEffort, } from "./openai.js";
|
|
|
6
6
|
// the endpoint; a finding is what it passes to report_finding, kept as given
|
|
7
7
|
// for the pipeline to validate.
|
|
8
8
|
export async function runLoop(input) {
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
9
|
+
const conversation = new Conversation(input);
|
|
10
|
+
const outcome = await conversation.turn(input.user, input.tools, input.maxSteps, input.resume);
|
|
11
|
+
const { wrapUp } = input;
|
|
12
|
+
if (!wrapUp)
|
|
13
|
+
return outcome;
|
|
14
|
+
const tools = input.tools.filter((t) => wrapUp.tools.includes(t.name));
|
|
15
|
+
return {
|
|
16
|
+
...outcome,
|
|
17
|
+
wrapUp: () => conversation.turn(wrapUp.message, tools, outcome.steps + wrapUp.steps),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// One attempt's messages and outcome, which a later turn continues under the
|
|
21
|
+
// attempt's signal and timeout.
|
|
22
|
+
class Conversation {
|
|
23
|
+
input;
|
|
24
|
+
timeout;
|
|
25
|
+
signal;
|
|
26
|
+
messages;
|
|
27
|
+
outcome = {
|
|
18
28
|
findings: [],
|
|
19
29
|
steps: 0,
|
|
20
30
|
toolCalls: [],
|
|
21
31
|
text: "",
|
|
22
32
|
usage: emptyUsage(),
|
|
23
33
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
if (response.toolCalls.length === 0) {
|
|
55
|
-
const stoppedEarly = input.resume && !outcome.resumed && texts.join("").trim() === "";
|
|
56
|
-
if (stoppedEarly && outcome.steps < input.maxSteps) {
|
|
57
|
-
outcome.resumed = true;
|
|
58
|
-
messages.push({ role: "user", content: input.resume });
|
|
59
|
-
continue;
|
|
34
|
+
texts = [];
|
|
35
|
+
uses = [];
|
|
36
|
+
params;
|
|
37
|
+
constructor(input) {
|
|
38
|
+
this.input = input;
|
|
39
|
+
this.timeout = AbortSignal.timeout(input.timeoutMs);
|
|
40
|
+
this.signal = AbortSignal.any([input.signal, this.timeout]);
|
|
41
|
+
this.messages = [{ role: "system", content: input.system }];
|
|
42
|
+
this.params = input.params ?? {};
|
|
43
|
+
}
|
|
44
|
+
// Runs until the done tool, an answer without tools, or `maxSteps` steps
|
|
45
|
+
// in the whole attempt; returns the attempt's outcome so far.
|
|
46
|
+
async turn(message, offered, maxSteps, resume) {
|
|
47
|
+
const { input, messages, outcome, texts, uses } = this;
|
|
48
|
+
const tools = new Map(offered.map((t) => [t.name, t]));
|
|
49
|
+
const specs = offered.map(toolSpec);
|
|
50
|
+
let done = false;
|
|
51
|
+
messages.push({ role: "user", content: message });
|
|
52
|
+
while (outcome.steps < maxSteps) {
|
|
53
|
+
const response = await chat(input.endpoint, {
|
|
54
|
+
model: input.model,
|
|
55
|
+
messages,
|
|
56
|
+
...(specs.length > 0 ? { tools: specs } : {}),
|
|
57
|
+
...this.params,
|
|
58
|
+
}, input.price, this.signal);
|
|
59
|
+
outcome.steps += 1;
|
|
60
|
+
if (response.effortDropped) {
|
|
61
|
+
this.params = withoutEffort(this.params);
|
|
62
|
+
input.onEffortRefused?.();
|
|
60
63
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (!response.ok) {
|
|
65
|
+
outcome.error = this.timeout.aborted
|
|
66
|
+
? { message: `timed out after ${Math.round(input.timeoutMs / 1000)}s`, retryable: true }
|
|
67
|
+
: response.error;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
outcome.usage = addUsage(outcome.usage, response.usage);
|
|
71
|
+
input.onUsage?.(outcome.usage);
|
|
72
|
+
if (response.content)
|
|
73
|
+
texts.push(response.content);
|
|
67
74
|
messages.push({
|
|
68
|
-
role: "
|
|
69
|
-
|
|
70
|
-
|
|
75
|
+
role: "assistant",
|
|
76
|
+
content: response.content || null,
|
|
77
|
+
...(response.toolCalls.length > 0 ? { tool_calls: response.toolCalls } : {}),
|
|
71
78
|
});
|
|
79
|
+
if (response.toolCalls.length === 0) {
|
|
80
|
+
const stoppedEarly = resume && !outcome.resumed && texts.join("").trim() === "";
|
|
81
|
+
if (stoppedEarly && outcome.steps < maxSteps) {
|
|
82
|
+
outcome.resumed = true;
|
|
83
|
+
messages.push({ role: "user", content: resume });
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
for (const call of response.toolCalls) {
|
|
89
|
+
outcome.toolCalls.push(call.function.name);
|
|
90
|
+
if (call.function.name === REVIEW_TOOLS.taskDone)
|
|
91
|
+
done = true;
|
|
92
|
+
messages.push({
|
|
93
|
+
role: "tool",
|
|
94
|
+
tool_call_id: call.id,
|
|
95
|
+
content: await runTool(tools, call, input.context, outcome, uses),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (done)
|
|
99
|
+
break;
|
|
72
100
|
}
|
|
73
|
-
|
|
74
|
-
|
|
101
|
+
outcome.text = texts.join("\n").trim();
|
|
102
|
+
if (!done && !outcome.error && outcome.steps >= maxSteps)
|
|
103
|
+
outcome.atStepCap = true;
|
|
104
|
+
else
|
|
105
|
+
delete outcome.atStepCap;
|
|
106
|
+
return { ...snapshot(outcome), ...exploredBy(uses) };
|
|
75
107
|
}
|
|
76
|
-
|
|
77
|
-
|
|
108
|
+
}
|
|
109
|
+
// A later turn adds to the outcome; what an earlier turn returned stays as
|
|
110
|
+
// it was.
|
|
111
|
+
function snapshot(outcome) {
|
|
112
|
+
return { ...outcome, findings: [...outcome.findings], toolCalls: [...outcome.toolCalls] };
|
|
78
113
|
}
|
|
79
114
|
// A tool's answer, or why it gave none: a wrong call is told to the model,
|
|
80
115
|
// which may correct it, and never ends the attempt.
|
|
81
|
-
async function runTool(tools, call, context, outcome) {
|
|
116
|
+
async function runTool(tools, call, context, outcome, uses) {
|
|
82
117
|
const tool = tools.get(call.function.name);
|
|
83
118
|
if (!tool)
|
|
84
119
|
return `Unknown tool: ${call.function.name}`;
|
|
@@ -93,6 +128,7 @@ async function runTool(tools, call, context, outcome) {
|
|
|
93
128
|
if (!parsed.success) {
|
|
94
129
|
return `Invalid arguments: ${parsed.error.issues.map((i) => `${i.path.join(".") || "input"}: ${i.message}`).join("; ")}`;
|
|
95
130
|
}
|
|
131
|
+
uses.push({ name: tool.name, input: parsed.data });
|
|
96
132
|
if (tool.name === REVIEW_TOOLS.reportFinding)
|
|
97
133
|
outcome.findings.push(parsed.data);
|
|
98
134
|
try {
|
|
@@ -102,4 +138,3 @@ async function runTool(tools, call, context, outcome) {
|
|
|
102
138
|
return `Tool failed: ${errorMessage(error)}`;
|
|
103
139
|
}
|
|
104
140
|
}
|
|
105
|
-
//# sourceMappingURL=loop.js.map
|
package/dist/openai.d.ts
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
import { type Effort, type ModelPrice, type QuotaError, type ToolDefinition, type Usage } from "@open-cr-agent/core";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
declare const toolCallSchema: z.ZodObject<{
|
|
3
|
+
declare const toolCallSchema: z.ZodPipe<z.ZodObject<{
|
|
4
4
|
id: z.ZodString;
|
|
5
5
|
function: z.ZodObject<{
|
|
6
6
|
name: z.ZodString;
|
|
7
7
|
arguments: z.ZodString;
|
|
8
8
|
}, z.core.$strip>;
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
extra_content: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
10
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
11
|
+
id: string;
|
|
12
|
+
function: {
|
|
13
|
+
name: string;
|
|
14
|
+
arguments: string;
|
|
15
|
+
};
|
|
16
|
+
extra_content?: Record<string, unknown> | undefined;
|
|
17
|
+
type: "function";
|
|
18
|
+
}, {
|
|
19
|
+
id: string;
|
|
20
|
+
function: {
|
|
21
|
+
name: string;
|
|
22
|
+
arguments: string;
|
|
23
|
+
};
|
|
24
|
+
extra_content?: Record<string, unknown> | undefined;
|
|
25
|
+
}>>;
|
|
26
|
+
export type ToolCall = z.output<typeof toolCallSchema>;
|
|
11
27
|
export type ChatMessage = {
|
|
12
28
|
role: "system" | "user";
|
|
13
29
|
content: string;
|
|
@@ -67,4 +83,3 @@ export declare function toolSpec(tool: ToolDefinition): ToolSpec;
|
|
|
67
83
|
export declare function chat(endpoint: Endpoint, request: ChatRequest, price: ModelPrice, signal: AbortSignal): Promise<ChatResponse>;
|
|
68
84
|
export declare function withoutEffort<T extends CallParams>(params: T): T;
|
|
69
85
|
export {};
|
|
70
|
-
//# sourceMappingURL=openai.d.ts.map
|
package/dist/openai.js
CHANGED
|
@@ -3,11 +3,17 @@ import { errorMessage, parseQuotaError, withoutSecrets, } from "@open-cr-agent/c
|
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
// The subset of the OpenAI chat completions protocol the loop needs: one
|
|
5
5
|
// choice, its text and tool calls, and the token counts. Anything else an
|
|
6
|
-
// endpoint adds is dropped at this boundary
|
|
7
|
-
|
|
6
|
+
// endpoint adds is dropped at this boundary, except a tool call's
|
|
7
|
+
// `extra_content`: Gemini 3 puts its thought signature there and refuses the
|
|
8
|
+
// next request unless the call comes back with it.
|
|
9
|
+
const toolCallSchema = z
|
|
10
|
+
.object({
|
|
8
11
|
id: z.string(),
|
|
9
12
|
function: z.object({ name: z.string(), arguments: z.string() }),
|
|
10
|
-
|
|
13
|
+
extra_content: z.record(z.string(), z.unknown()).optional(),
|
|
14
|
+
})
|
|
15
|
+
// A call goes back with its type, which Vertex AI requires.
|
|
16
|
+
.transform((call) => ({ ...call, type: "function" }));
|
|
11
17
|
const choiceSchema = z.object({
|
|
12
18
|
message: z.object({
|
|
13
19
|
content: z.string().nullable().optional(),
|
|
@@ -186,4 +192,3 @@ function excerpt(body) {
|
|
|
186
192
|
return "(empty body)";
|
|
187
193
|
return text.length > BODY_EXCERPT ? `${text.slice(0, BODY_EXCERPT)}…` : text;
|
|
188
194
|
}
|
|
189
|
-
//# sourceMappingURL=openai.js.map
|
package/dist/plugin.d.ts
CHANGED
package/dist/plugin.js
CHANGED
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const RECORD_DIR_ENV = "OCRA_RECORD_DIR";
|
|
3
|
+
export declare const recordingSchema: z.ZodObject<{
|
|
4
|
+
version: z.ZodLiteral<1>;
|
|
5
|
+
request: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
6
|
+
answers: z.ZodArray<z.ZodObject<{
|
|
7
|
+
status: z.ZodNumber;
|
|
8
|
+
body: z.ZodString;
|
|
9
|
+
}, z.core.$strip>>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
export type Recording = z.infer<typeof recordingSchema>;
|
|
12
|
+
export declare function exchangeKey(request: {
|
|
13
|
+
messages?: unknown;
|
|
14
|
+
tools?: unknown;
|
|
15
|
+
}): string;
|
|
16
|
+
export declare function recordingFetch(base: typeof fetch, dir: string, secrets: () => readonly string[], warn: (message: string) => void): typeof fetch;
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { errorMessage, OcraError, withoutSecrets } from "@open-cr-agent/core";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
// Where the direct runtime writes every model exchange, for replaying a run
|
|
7
|
+
// without a model (tests) or reading what a model was sent and answered.
|
|
8
|
+
export const RECORD_DIR_ENV = "OCRA_RECORD_DIR";
|
|
9
|
+
// One file per distinct request, named by exchangeKey(): the request as sent
|
|
10
|
+
// and each answer it got, in order (a request sent again after a transient
|
|
11
|
+
// failure gets a second answer). Headers and the endpoint's address are not
|
|
12
|
+
// kept, and every key is replaced by "<key>".
|
|
13
|
+
export const recordingSchema = z.object({
|
|
14
|
+
version: z.literal(1),
|
|
15
|
+
request: z.record(z.string(), z.unknown()),
|
|
16
|
+
answers: z.array(z.object({ status: z.number().int(), body: z.string() })),
|
|
17
|
+
});
|
|
18
|
+
// What makes two requests the same exchange: the conversation and the tools
|
|
19
|
+
// offered. The model and sampling settings are recorded but do not count, so
|
|
20
|
+
// a replay of a run does not depend on which model of the chain served it.
|
|
21
|
+
export function exchangeKey(request) {
|
|
22
|
+
return createHash("sha256")
|
|
23
|
+
.update(JSON.stringify([request.messages ?? [], request.tools ?? []]))
|
|
24
|
+
.digest("hex")
|
|
25
|
+
.slice(0, 32);
|
|
26
|
+
}
|
|
27
|
+
// Wraps fetch so every chat completion it sends is recorded in `dir`. The
|
|
28
|
+
// directory is created, private to its owner, before any request, so a path
|
|
29
|
+
// that cannot be one fails the run at once rather than every request.
|
|
30
|
+
// `secrets` is asked when each request is sent and again at its write, and
|
|
31
|
+
// every key seen is kept: a key renewed during the run (the ocra Cloud
|
|
32
|
+
// token), even while a request was in flight, is taken out as well. A recording that cannot be written is
|
|
33
|
+
// a warning, never a failed request: the answer was paid for, and a thrown
|
|
34
|
+
// error here would read as a network failure and send the request again.
|
|
35
|
+
export function recordingFetch(base, dir, secrets, warn) {
|
|
36
|
+
try {
|
|
37
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
throw new OcraError("CONFIG_INVALID", `${RECORD_DIR_ENV} names a directory ocra cannot create: ${errorMessage(error)}`);
|
|
41
|
+
}
|
|
42
|
+
const seen = new Set();
|
|
43
|
+
const remember = () => {
|
|
44
|
+
for (const s of secrets())
|
|
45
|
+
seen.add(s);
|
|
46
|
+
return [...seen];
|
|
47
|
+
};
|
|
48
|
+
return async (input, init) => {
|
|
49
|
+
remember();
|
|
50
|
+
const response = await base(input, init);
|
|
51
|
+
if (typeof init?.body !== "string")
|
|
52
|
+
return response;
|
|
53
|
+
const body = await response.text();
|
|
54
|
+
try {
|
|
55
|
+
// Synchronous, so two answers to the same request cannot interleave.
|
|
56
|
+
append(dir, JSON.parse(init.body), response.status, body, remember());
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
warn(`could not record a model exchange in ${RECORD_DIR_ENV}: ${errorMessage(error)}`);
|
|
60
|
+
}
|
|
61
|
+
return new Response(body, {
|
|
62
|
+
status: response.status,
|
|
63
|
+
statusText: response.statusText,
|
|
64
|
+
headers: response.headers,
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function append(dir, request, status, body, secrets) {
|
|
69
|
+
const path = join(dir, `${exchangeKey(request)}.json`);
|
|
70
|
+
const { stream: _, ...sent } = request;
|
|
71
|
+
const recording = existsSync(path)
|
|
72
|
+
? recordingSchema.parse(JSON.parse(readFileSync(path, "utf8")))
|
|
73
|
+
: { version: 1, request: sent, answers: [] };
|
|
74
|
+
recording.answers.push({ status, body });
|
|
75
|
+
const text = `${JSON.stringify(redacted(recording, withEscaped(secrets)), null, 2)}\n`;
|
|
76
|
+
writeFileSync(path, text, { mode: 0o600 });
|
|
77
|
+
}
|
|
78
|
+
// Each secret as it is and as it reads inside a JSON string: an answer's body
|
|
79
|
+
// is the endpoint's JSON text, where a key with a quote or backslash is
|
|
80
|
+
// escaped.
|
|
81
|
+
function withEscaped(secrets) {
|
|
82
|
+
const forms = secrets.flatMap((s) => [s, JSON.stringify(s).slice(1, -1)]);
|
|
83
|
+
return [...new Set(forms.filter((s) => s.length > 0))].sort((a, b) => b.length - a.length);
|
|
84
|
+
}
|
|
85
|
+
// Redacted string by string before serializing, since JSON.stringify escapes
|
|
86
|
+
// a secret with a quote or backslash out of its plain form.
|
|
87
|
+
function redacted(value, secrets) {
|
|
88
|
+
if (typeof value === "string")
|
|
89
|
+
return withoutSecrets(value, secrets);
|
|
90
|
+
if (Array.isArray(value))
|
|
91
|
+
return value.map((v) => redacted(v, secrets));
|
|
92
|
+
if (value !== null && typeof value === "object") {
|
|
93
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [withoutSecrets(k, secrets), redacted(v, secrets)]));
|
|
94
|
+
}
|
|
95
|
+
return value;
|
|
96
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export declare class DirectRuntime implements AgentRuntime {
|
|
|
11
11
|
private readonly efforts;
|
|
12
12
|
private readonly options;
|
|
13
13
|
constructor(options: DirectRuntimeOptions);
|
|
14
|
+
private recorded;
|
|
14
15
|
appliedTo(agent: string): AppliedSettings | undefined;
|
|
15
16
|
runTask(spec: AgentTaskSpec, signal: AbortSignal): AsyncIterable<AgentEvent>;
|
|
16
17
|
complete(request: CompletionRequest, signal: AbortSignal): Promise<CompletionResult>;
|
|
@@ -21,4 +22,3 @@ export declare class DirectRuntime implements AgentRuntime {
|
|
|
21
22
|
private declared;
|
|
22
23
|
private target;
|
|
23
24
|
}
|
|
24
|
-
//# sourceMappingURL=runtime.d.ts.map
|
package/dist/runtime.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ChainRunner, MAX_AGENT_STEPS, OcraError, parseModel, proxiedFetch, RESUME_MESSAGE, reviewTools, } from "@open-cr-agent/core";
|
|
1
|
+
import { ChainRunner, MAX_AGENT_STEPS, OcraError, parseModel, proxiedFetch, RESUME_MESSAGE, reviewTools, WRAP_UP_MESSAGE, WRAP_UP_STEPS, WRAP_UP_TOOLS, } from "@open-cr-agent/core";
|
|
2
2
|
import { EffortLedger } from "./effort.js";
|
|
3
3
|
import { runLoop } from "./loop.js";
|
|
4
|
+
import { RECORD_DIR_ENV, recordingFetch } from "./record.js";
|
|
4
5
|
// Talks to the declared OpenAI-compatible endpoints itself: no OpenCode
|
|
5
6
|
// process, no catalog fetch, no package install, nothing on disk. A model of
|
|
6
7
|
// a provider that is not declared in configuration is refused, since the
|
|
@@ -17,7 +18,7 @@ export class DirectRuntime {
|
|
|
17
18
|
constructor(options) {
|
|
18
19
|
this.options = options;
|
|
19
20
|
this.tools = [...reviewTools, ...options.tools];
|
|
20
|
-
this.fetch = options.fetch ?? proxiedFetch(options.env);
|
|
21
|
+
this.fetch = this.recorded(options.fetch ?? proxiedFetch(options.env));
|
|
21
22
|
const { temperature, seed } = options.sampling ?? {};
|
|
22
23
|
this.sampling = {
|
|
23
24
|
...(temperature === undefined ? {} : { temperature }),
|
|
@@ -30,6 +31,28 @@ export class DirectRuntime {
|
|
|
30
31
|
complete: (model, request, signal) => this.attemptCompletion(model, request, signal),
|
|
31
32
|
});
|
|
32
33
|
}
|
|
34
|
+
// Every exchange goes to the recording directory when one is set, with the
|
|
35
|
+
// keys of all declared providers taken out as they are at each write: the
|
|
36
|
+
// environment may be live, renewing a key while the run lasts. The first
|
|
37
|
+
// recording that fails is said once on stderr, which a review's output
|
|
38
|
+
// (stdout) does not share.
|
|
39
|
+
recorded(base) {
|
|
40
|
+
const dir = this.options.env[RECORD_DIR_ENV];
|
|
41
|
+
if (!dir)
|
|
42
|
+
return base;
|
|
43
|
+
const { env, providers } = this.options;
|
|
44
|
+
const keys = () => Object.values(providers ?? {}).flatMap((p) => {
|
|
45
|
+
const key = p.apiKeyEnv ? env[p.apiKeyEnv] : undefined;
|
|
46
|
+
return key ? [key] : [];
|
|
47
|
+
});
|
|
48
|
+
let warned = false;
|
|
49
|
+
return recordingFetch(base, dir, keys, (message) => {
|
|
50
|
+
if (warned)
|
|
51
|
+
return;
|
|
52
|
+
warned = true;
|
|
53
|
+
process.stderr.write(`ocra: ${message}; the review goes on without recording it\n`);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
33
56
|
appliedTo(agent) {
|
|
34
57
|
return this.efforts.appliedTo(agent);
|
|
35
58
|
}
|
|
@@ -49,6 +72,7 @@ export class DirectRuntime {
|
|
|
49
72
|
context: spec.context,
|
|
50
73
|
maxSteps: MAX_AGENT_STEPS,
|
|
51
74
|
resume: RESUME_MESSAGE,
|
|
75
|
+
wrapUp: { message: WRAP_UP_MESSAGE, tools: WRAP_UP_TOOLS, steps: WRAP_UP_STEPS },
|
|
52
76
|
timeoutMs: spec.timeoutMs,
|
|
53
77
|
signal,
|
|
54
78
|
onUsage,
|
|
@@ -129,4 +153,3 @@ const NO_CONTEXT = {
|
|
|
129
153
|
readDiff: () => undefined,
|
|
130
154
|
searchCode: async () => [],
|
|
131
155
|
};
|
|
132
|
-
//# sourceMappingURL=runtime.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-cr-agent/runtime-direct",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Agent runtime for Open-CR-Agent that calls OpenAI-compatible endpoints directly",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-review",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"!dist/**/*.map"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@open-cr-agent/core": "0.
|
|
37
|
-
"zod": "^4.6.
|
|
36
|
+
"@open-cr-agent/core": "0.9.0",
|
|
37
|
+
"zod": "^4.6.6"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|