@open-cr-agent/runtime-direct 0.16.0 → 0.19.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/anthropic.js +29 -13
- package/dist/loop.d.ts +4 -0
- package/dist/loop.js +41 -8
- package/dist/openai.js +46 -13
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +11 -2
- package/dist/shared-starts.d.ts +4 -0
- package/dist/shared-starts.js +32 -0
- package/dist/wire.d.ts +5 -0
- package/package.json +4 -4
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/
|
|
3
|
+
The agent runtime of [Open-CR-Agent](https://github.com/ocrahq/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/anthropic.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
|
-
import { errorMessage, parseQuotaError, withoutSecrets, } from "@open-cr-agent/core";
|
|
2
|
+
import { errorMessage, fetchFailure, parseQuotaError, withoutSecrets, } from "@open-cr-agent/core";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { excerpt, onceMore, pricedUsage, retryableStatus, transientStatus, withoutEffort, } from "./wire.js";
|
|
5
5
|
// Room for an answer with its reasoning; the protocol requires a cap.
|
|
@@ -30,18 +30,28 @@ const LEVELS = {
|
|
|
30
30
|
// the first that stored it (ADR-0064). A dialogue of one request has no next
|
|
31
31
|
// step, and storing costs a quarter more than sending, so its conversation
|
|
32
32
|
// goes unmarked; its system prompt stays marked for the other calls of its
|
|
33
|
-
// kind.
|
|
34
|
-
|
|
33
|
+
// kind. The first request waits for the answer to another that is storing
|
|
34
|
+
// the same start to begin (ADR-0072).
|
|
35
|
+
export const anthropicWire = ({ endpoint, model, price, system, oneRequest, starts }) => {
|
|
35
36
|
const client = clientOf(endpoint);
|
|
36
37
|
const secrets = endpoint.apiKey ? [endpoint.apiKey] : [];
|
|
37
38
|
const messages = [];
|
|
38
|
-
|
|
39
|
+
// What the first request stores for the run's other conversations.
|
|
40
|
+
let opening;
|
|
39
41
|
return {
|
|
40
42
|
say: (text, shared = []) => {
|
|
43
|
+
const ends = sharedEnds(text, shared);
|
|
44
|
+
const [first] = ends;
|
|
45
|
+
if (messages.length === 0 && first !== undefined) {
|
|
46
|
+
opening = [endpoint.baseUrl, model, system, text.slice(0, first)].join("\0");
|
|
47
|
+
}
|
|
41
48
|
const marks = oneRequest ? MAX_SHARED_MARKS + 1 : MAX_SHARED_MARKS;
|
|
42
|
-
messages.push({ role: "user", content: textBlocks(text,
|
|
49
|
+
messages.push({ role: "user", content: textBlocks(text, ends, marks) });
|
|
43
50
|
},
|
|
44
51
|
ask: async (tools, settings, signal) => {
|
|
52
|
+
const begun = opening === undefined ? undefined : await starts?.claim(opening, signal);
|
|
53
|
+
opening = undefined;
|
|
54
|
+
const send = (request) => onceMore(() => stream(client, request, price, secrets, signal, begun), signal).finally(begun);
|
|
45
55
|
const request = (sent) => ({
|
|
46
56
|
model,
|
|
47
57
|
max_tokens: MAX_OUTPUT_TOKENS,
|
|
@@ -50,10 +60,10 @@ export const anthropicWire = ({ endpoint, model, price, system, oneRequest }) =>
|
|
|
50
60
|
...(tools.length > 0 ? { tools: tools.map(toolSpec) } : {}),
|
|
51
61
|
...effortParams(sent.effort),
|
|
52
62
|
});
|
|
53
|
-
let reply = await send(request(settings)
|
|
63
|
+
let reply = await send(request(settings));
|
|
54
64
|
let dropped = false;
|
|
55
65
|
if (!reply.ok && reply.error.effortRefused && settings.effort !== undefined) {
|
|
56
|
-
reply = await send(request(withoutEffort(settings))
|
|
66
|
+
reply = await send(request(withoutEffort(settings)));
|
|
57
67
|
dropped = true;
|
|
58
68
|
}
|
|
59
69
|
const effortDropped = dropped ? { effortDropped: true } : {};
|
|
@@ -112,11 +122,13 @@ function effortParams(effort) {
|
|
|
112
122
|
...(effort === "none" ? {} : { thinking: { type: "adaptive" } }),
|
|
113
123
|
};
|
|
114
124
|
}
|
|
125
|
+
function sharedEnds(text, shared) {
|
|
126
|
+
return shared.filter((end) => end > 0 && end < text.length);
|
|
127
|
+
}
|
|
115
128
|
// The text as blocks that end where a shared start does: together they read
|
|
116
129
|
// as the text does. The first `marks` are stored; a later one only ends a
|
|
117
130
|
// block, which is where a request looks for what another stored.
|
|
118
|
-
function textBlocks(text,
|
|
119
|
-
const ends = shared.filter((end) => end > 0 && end < text.length);
|
|
131
|
+
function textBlocks(text, ends, marks) {
|
|
120
132
|
const blocks = [];
|
|
121
133
|
let from = 0;
|
|
122
134
|
for (const [i, end] of ends.entries()) {
|
|
@@ -140,10 +152,13 @@ function withBreakpoint(messages) {
|
|
|
140
152
|
{ ...last, content: [...blocks.slice(0, -1), { ...tail, ...CACHED }] },
|
|
141
153
|
];
|
|
142
154
|
}
|
|
143
|
-
async function stream(client, request, price, secrets, signal) {
|
|
155
|
+
async function stream(client, request, price, secrets, signal, begun) {
|
|
144
156
|
let message;
|
|
145
157
|
try {
|
|
146
|
-
|
|
158
|
+
const answer = client.messages.stream(request, { signal });
|
|
159
|
+
if (begun)
|
|
160
|
+
answer.once("streamEvent", begun);
|
|
161
|
+
message = await answer.finalMessage();
|
|
147
162
|
}
|
|
148
163
|
catch (error) {
|
|
149
164
|
if (signal.aborted)
|
|
@@ -171,12 +186,13 @@ function cutOff(message) {
|
|
|
171
186
|
}
|
|
172
187
|
function failure(error, secrets) {
|
|
173
188
|
// An endpoint may echo the request's headers in an error.
|
|
174
|
-
const
|
|
189
|
+
const text = (message) => excerpt(withoutSecrets(message, secrets));
|
|
175
190
|
if (!(error instanceof Anthropic.APIError) || error.status === undefined) {
|
|
176
191
|
// No status: the connection failed, or the stream was not one the SDK
|
|
177
192
|
// could read to its end.
|
|
178
|
-
return { message:
|
|
193
|
+
return { message: text(fetchFailure(error)), retryable: true, transient: true };
|
|
179
194
|
}
|
|
195
|
+
const shown = text(errorMessage(error));
|
|
180
196
|
const { status } = error;
|
|
181
197
|
const quota = parseQuotaError(shown, status);
|
|
182
198
|
if (quota && quota.retryAfterMs === undefined) {
|
package/dist/loop.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type ModelPrice, type RateLimits, type ReviewContext, type TaskAttempt, type ToolDefinition, type Usage } from "@open-cr-agent/core";
|
|
2
|
+
import type { SharedStarts } from "./shared-starts.js";
|
|
2
3
|
import { type CallSettings, type Endpoint, type Wire } from "./wire.js";
|
|
3
4
|
export interface LoopInput {
|
|
4
5
|
wire: Wire;
|
|
@@ -17,6 +18,9 @@ export interface LoopInput {
|
|
|
17
18
|
timeoutMs: number;
|
|
18
19
|
settings?: CallSettings;
|
|
19
20
|
onEffortRefused?: () => void;
|
|
21
|
+
textTools?: boolean;
|
|
22
|
+
starts?: SharedStarts;
|
|
23
|
+
onToolsRefused?: () => void;
|
|
20
24
|
signal: AbortSignal;
|
|
21
25
|
onUsage?: (spent: Usage) => void;
|
|
22
26
|
limits?: RateLimits;
|
package/dist/loop.js
CHANGED
|
@@ -33,6 +33,7 @@ class Conversation {
|
|
|
33
33
|
};
|
|
34
34
|
texts = [];
|
|
35
35
|
uses = [];
|
|
36
|
+
refusals = [];
|
|
36
37
|
settings;
|
|
37
38
|
constructor(input) {
|
|
38
39
|
this.input = input;
|
|
@@ -41,6 +42,13 @@ class Conversation {
|
|
|
41
42
|
this.dialogue = input.wire(input);
|
|
42
43
|
this.settings = input.settings ?? {};
|
|
43
44
|
}
|
|
45
|
+
// An accepted report answers a refused one for the same file, or one whose
|
|
46
|
+
// file could not be read; a refusal for another file stays on record.
|
|
47
|
+
sentAgain(file) {
|
|
48
|
+
const index = this.refusals.findIndex((r) => r.file === undefined || r.file === file);
|
|
49
|
+
if (index >= 0)
|
|
50
|
+
this.refusals.splice(index, 1);
|
|
51
|
+
}
|
|
44
52
|
// Runs until the done tool, an answer without tools, or `maxSteps` steps
|
|
45
53
|
// in the whole attempt; returns the attempt's outcome so far.
|
|
46
54
|
async turn(message, offered, maxSteps, resume) {
|
|
@@ -77,11 +85,27 @@ class Conversation {
|
|
|
77
85
|
// grep; each call is recorded as it starts, in the order given.
|
|
78
86
|
const outputs = await Promise.all(response.calls.map(async (call) => {
|
|
79
87
|
outcome.toolCalls.push(call.name);
|
|
80
|
-
|
|
81
|
-
done = true;
|
|
82
|
-
return { call, output: await runTool(tools, call, input.context, outcome, uses) };
|
|
88
|
+
return { call, ...(await runTool(tools, call, input.context, outcome, uses)) };
|
|
83
89
|
}));
|
|
84
90
|
dialogue.answer(outputs);
|
|
91
|
+
// A finding sent in the same answer as task_done and refused for its
|
|
92
|
+
// arguments was lost without a word: the task goes on, so the model
|
|
93
|
+
// reads why and can send it again.
|
|
94
|
+
const reports = outputs.filter(({ call }) => call.name === REVIEW_TOOLS.reportFinding);
|
|
95
|
+
const refused = reports.filter((report) => report.refused !== undefined);
|
|
96
|
+
for (const report of reports) {
|
|
97
|
+
if (report.refused === undefined)
|
|
98
|
+
this.sentAgain(reportedFile(report.call));
|
|
99
|
+
}
|
|
100
|
+
for (const { call, refused: fields = "" } of refused) {
|
|
101
|
+
this.refusals.push({ fields, file: reportedFile(call) });
|
|
102
|
+
}
|
|
103
|
+
if (this.refusals.length > 0)
|
|
104
|
+
outcome.refusedFindings = this.refusals.map((r) => r.fields);
|
|
105
|
+
else
|
|
106
|
+
delete outcome.refusedFindings;
|
|
107
|
+
done =
|
|
108
|
+
outputs.some(({ call }) => call.name === REVIEW_TOOLS.taskDone) && refused.length === 0;
|
|
85
109
|
if (done)
|
|
86
110
|
break;
|
|
87
111
|
}
|
|
@@ -102,6 +126,8 @@ class Conversation {
|
|
|
102
126
|
this.settings = withoutEffort(this.settings);
|
|
103
127
|
this.input.onEffortRefused?.();
|
|
104
128
|
}
|
|
129
|
+
if (response.toolsDropped)
|
|
130
|
+
this.input.onToolsRefused?.();
|
|
105
131
|
const quota = response.ok ? undefined : response.error.quota;
|
|
106
132
|
if (!quota || this.signal.aborted || !(await this.input.limits?.waitOut(quota))) {
|
|
107
133
|
return response;
|
|
@@ -114,25 +140,32 @@ class Conversation {
|
|
|
114
140
|
function snapshot(outcome) {
|
|
115
141
|
return { ...outcome, findings: [...outcome.findings], toolCalls: [...outcome.toolCalls] };
|
|
116
142
|
}
|
|
143
|
+
function reportedFile(call) {
|
|
144
|
+
const file = call.input?.file;
|
|
145
|
+
return typeof file === "string" ? file : undefined;
|
|
146
|
+
}
|
|
117
147
|
// A tool's answer, or why it gave none: a wrong call is told to the model,
|
|
118
148
|
// which may correct it, and never ends the attempt.
|
|
119
149
|
async function runTool(tools, call, context, outcome, uses) {
|
|
120
150
|
const tool = tools.get(call.name);
|
|
121
151
|
if (!tool)
|
|
122
|
-
return `Unknown tool: ${call.name}
|
|
152
|
+
return { output: `Unknown tool: ${call.name}`, refused: "unknown tool" };
|
|
123
153
|
if (call.malformed)
|
|
124
|
-
return "Invalid arguments: not JSON";
|
|
154
|
+
return { output: "Invalid arguments: not JSON", refused: "not JSON" };
|
|
125
155
|
const parsed = tool.inputSchema.safeParse(call.input);
|
|
126
156
|
if (!parsed.success) {
|
|
127
|
-
return
|
|
157
|
+
return {
|
|
158
|
+
output: `Invalid arguments: ${parsed.error.issues.map((i) => `${i.path.join(".") || "input"}: ${i.message}`).join("; ")}`,
|
|
159
|
+
refused: parsed.error.issues.map((i) => i.path.join(".") || "input").join(", "),
|
|
160
|
+
};
|
|
128
161
|
}
|
|
129
162
|
uses.push({ name: tool.name, input: parsed.data });
|
|
130
163
|
if (tool.name === REVIEW_TOOLS.reportFinding)
|
|
131
164
|
outcome.findings.push(parsed.data);
|
|
132
165
|
try {
|
|
133
|
-
return await tool.execute(parsed.data, context);
|
|
166
|
+
return { output: await tool.execute(parsed.data, context) };
|
|
134
167
|
}
|
|
135
168
|
catch (error) {
|
|
136
|
-
return `Tool failed: ${errorMessage(error)}
|
|
169
|
+
return { output: `Tool failed: ${errorMessage(error)}` };
|
|
137
170
|
}
|
|
138
171
|
}
|
package/dist/openai.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { fetchFailure, parseQuotaError, parseTextToolCalls, textToolInstructions, textToolResults, withoutSecrets, } from "@open-cr-agent/core";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { excerpt, onceMore, pricedUsage, retryableStatus, transientStatus, } from "./wire.js";
|
|
4
4
|
// The subset of the OpenAI chat completions protocol the loop needs: one
|
|
@@ -38,28 +38,54 @@ const errorBodySchema = z.object({
|
|
|
38
38
|
});
|
|
39
39
|
// How an endpoint names the effort parameters in a 400 that refuses them.
|
|
40
40
|
const EFFORT_PARAMETER = /\breasoning(?:_effort)?\b/i;
|
|
41
|
+
// How one names the tools in a refusal of them: vLLM's "auto" tool choice
|
|
42
|
+
// requires --enable-auto-tool-choice, Ollama's "does not support tools", a
|
|
43
|
+
// gateway's "function calling is not supported".
|
|
44
|
+
const TOOLS_PARAMETER = /\btools?\b|function[_ -]?call/i;
|
|
45
|
+
const REFUSED_PARAMETER = new Set([400, 422]);
|
|
41
46
|
// A conversation in OpenAI's chat completions protocol, with the effort sent
|
|
42
47
|
// the way the provider declares.
|
|
43
48
|
export function openaiWire(style = "openai") {
|
|
44
|
-
return ({ endpoint, model, price, system }) => {
|
|
49
|
+
return ({ endpoint, model, price, system, textTools }) => {
|
|
45
50
|
const messages = [{ role: "system", content: system }];
|
|
51
|
+
// The model calls tools as text (ADR-0070): declared so, or from the
|
|
52
|
+
// request on whose tools the endpoint refused.
|
|
53
|
+
let textual = textTools ?? false;
|
|
54
|
+
const request = (tools, settings) => {
|
|
55
|
+
const base = { model, ...params(settings, style) };
|
|
56
|
+
if (tools.length === 0)
|
|
57
|
+
return { ...base, messages };
|
|
58
|
+
if (!textual)
|
|
59
|
+
return { ...base, messages, tools: tools.map(toolSpec) };
|
|
60
|
+
const instructed = `${system}\n\n${textToolInstructions(tools)}`;
|
|
61
|
+
return { ...base, messages: [{ role: "system", content: instructed }, ...messages.slice(1)] };
|
|
62
|
+
};
|
|
46
63
|
return {
|
|
47
64
|
say: (text) => {
|
|
48
65
|
messages.push({ role: "user", content: text });
|
|
49
66
|
},
|
|
50
67
|
ask: async (tools, settings, signal) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
let response = await chat(endpoint, request(tools, settings), price, signal);
|
|
69
|
+
let dropped = droppedBy(response);
|
|
70
|
+
// Before any call was made: a refusal later is about the conversation.
|
|
71
|
+
const refused = !response.ok && response.error.toolsRefused && !textual;
|
|
72
|
+
if (refused && !messages.some((message) => message.role === "tool")) {
|
|
73
|
+
textual = true;
|
|
74
|
+
response = await chat(endpoint, request(tools, settings), price, signal);
|
|
75
|
+
dropped = { ...dropped, ...droppedBy(response), toolsDropped: true };
|
|
76
|
+
}
|
|
60
77
|
if (!response.ok)
|
|
61
|
-
return {
|
|
78
|
+
return { ok: false, error: response.error, ...dropped };
|
|
62
79
|
const { content, toolCalls, usage } = response;
|
|
80
|
+
if (textual && tools.length > 0) {
|
|
81
|
+
const { text, calls } = parseTextToolCalls(content);
|
|
82
|
+
messages.push({ role: "assistant", content: content || null });
|
|
83
|
+
const requests = calls.map((call, i) => ({
|
|
84
|
+
id: `text_${messages.length}_${i}`,
|
|
85
|
+
...call,
|
|
86
|
+
}));
|
|
87
|
+
return { ok: true, text, calls: requests, usage, ...dropped };
|
|
88
|
+
}
|
|
63
89
|
messages.push({
|
|
64
90
|
role: "assistant",
|
|
65
91
|
content: content || null,
|
|
@@ -68,6 +94,11 @@ export function openaiWire(style = "openai") {
|
|
|
68
94
|
return { ok: true, text: content, calls: toolCalls.map(toolRequest), usage, ...dropped };
|
|
69
95
|
},
|
|
70
96
|
answer: (outputs) => {
|
|
97
|
+
if (textual) {
|
|
98
|
+
const results = outputs.map(({ call, output }) => ({ name: call.name, output }));
|
|
99
|
+
messages.push({ role: "user", content: textToolResults(results) });
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
71
102
|
for (const { call, output } of outputs) {
|
|
72
103
|
messages.push({ role: "tool", tool_call_id: call.id, content: output });
|
|
73
104
|
}
|
|
@@ -75,6 +106,7 @@ export function openaiWire(style = "openai") {
|
|
|
75
106
|
};
|
|
76
107
|
};
|
|
77
108
|
}
|
|
109
|
+
const droppedBy = (response) => response.effortDropped ? { effortDropped: true } : {};
|
|
78
110
|
function params({ effort, ...sampling }, style) {
|
|
79
111
|
if (effort === undefined)
|
|
80
112
|
return sampling;
|
|
@@ -138,7 +170,7 @@ async function send(endpoint, request, price, signal) {
|
|
|
138
170
|
return {
|
|
139
171
|
ok: false,
|
|
140
172
|
error: {
|
|
141
|
-
message:
|
|
173
|
+
message: withoutSecrets(fetchFailure(error), secrets),
|
|
142
174
|
retryable: true,
|
|
143
175
|
transient: true,
|
|
144
176
|
},
|
|
@@ -192,6 +224,7 @@ function failure(response, body, secrets, reportedCode) {
|
|
|
192
224
|
message,
|
|
193
225
|
retryable: retryableStatus(status),
|
|
194
226
|
...(status === 400 && EFFORT_PARAMETER.test(body) ? { effortRefused: true } : {}),
|
|
227
|
+
...(REFUSED_PARAMETER.has(status) && TOOLS_PARAMETER.test(body) ? { toolsRefused: true } : {}),
|
|
195
228
|
...(quota ? { quota } : {}),
|
|
196
229
|
...(!quota && transientStatus(status) ? { transient: true } : {}),
|
|
197
230
|
};
|
package/dist/runtime.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ export declare class DirectRuntime implements AgentRuntime {
|
|
|
9
9
|
private readonly tools;
|
|
10
10
|
private readonly fetch;
|
|
11
11
|
private readonly efforts;
|
|
12
|
+
private readonly toolsAsText;
|
|
13
|
+
private readonly starts;
|
|
12
14
|
private readonly options;
|
|
13
15
|
constructor(options: DirectRuntimeOptions);
|
|
14
16
|
private recorded;
|
package/dist/runtime.js
CHANGED
|
@@ -4,6 +4,7 @@ import { EffortLedger } from "./effort.js";
|
|
|
4
4
|
import { runLoop } from "./loop.js";
|
|
5
5
|
import { openaiWire } from "./openai.js";
|
|
6
6
|
import { RECORD_DIR_ENV, recordingFetch } from "./record.js";
|
|
7
|
+
import { SharedStarts } from "./shared-starts.js";
|
|
7
8
|
// Talks to the declared endpoints itself, in OpenAI's chat completions or
|
|
8
9
|
// Anthropic's Messages protocol as each declares: no OpenCode process, no
|
|
9
10
|
// catalog fetch, no package install, nothing on disk. A model of
|
|
@@ -18,6 +19,10 @@ export class DirectRuntime {
|
|
|
18
19
|
tools;
|
|
19
20
|
fetch;
|
|
20
21
|
efforts;
|
|
22
|
+
// Models whose endpoint refused the tools: their later calls start with
|
|
23
|
+
// the tools as text instead of paying for another refusal (ADR-0070).
|
|
24
|
+
toolsAsText = new Set();
|
|
25
|
+
starts = new SharedStarts();
|
|
21
26
|
options;
|
|
22
27
|
constructor(options) {
|
|
23
28
|
this.options = options;
|
|
@@ -116,13 +121,16 @@ export class DirectRuntime {
|
|
|
116
121
|
return undefined;
|
|
117
122
|
}
|
|
118
123
|
// What one attempt sends besides the conversation, and where a refused
|
|
119
|
-
// effort
|
|
124
|
+
// effort or refused tools are recorded.
|
|
120
125
|
call({ agent, effort, model }) {
|
|
121
126
|
const { providerID } = parseModel(model);
|
|
122
|
-
const
|
|
127
|
+
const provider = this.options.providers?.[providerID];
|
|
128
|
+
const takesSampling = provider?.api !== "anthropic";
|
|
123
129
|
return {
|
|
124
130
|
settings: this.efforts.params(agent, effort, model, takesSampling),
|
|
125
131
|
onEffortRefused: () => this.efforts.refuse(agent, model),
|
|
132
|
+
textTools: provider?.toolCalls === "text" || this.toolsAsText.has(model),
|
|
133
|
+
onToolsRefused: () => void this.toolsAsText.add(model),
|
|
126
134
|
};
|
|
127
135
|
}
|
|
128
136
|
// A model's provider and price, from the configuration's declaration.
|
|
@@ -147,6 +155,7 @@ export class DirectRuntime {
|
|
|
147
155
|
const key = provider.apiKeyEnv ? this.options.env[provider.apiKeyEnv] : undefined;
|
|
148
156
|
return {
|
|
149
157
|
wire: provider.api === "anthropic" ? anthropicWire : openaiWire(provider.effort),
|
|
158
|
+
starts: this.starts,
|
|
150
159
|
endpoint: {
|
|
151
160
|
baseUrl: provider.baseUrl,
|
|
152
161
|
...(key ? { apiKey: key } : {}),
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
// A wait this long means the storing request is slow to be answered, and
|
|
3
|
+
// the requests behind it are worth more sent than held.
|
|
4
|
+
const MAX_WAIT_MS = 10_000;
|
|
5
|
+
// A provider's prompt cache serves a start only once the answer to the
|
|
6
|
+
// request that stored it has begun, so requests sent together each pay to
|
|
7
|
+
// store what one of them could have stored for all. The first request with a
|
|
8
|
+
// start goes at once; those that come before its answer begins wait for
|
|
9
|
+
// that, and read what it stored (ADR-0072).
|
|
10
|
+
export class SharedStarts {
|
|
11
|
+
stored = new Map();
|
|
12
|
+
// The first request with this start gets what to call once its answer has
|
|
13
|
+
// begun, or it has ended without one. Any other gets nothing, after that
|
|
14
|
+
// call, the wait's limit or the signal.
|
|
15
|
+
async claim(start, signal) {
|
|
16
|
+
const storing = this.stored.get(start);
|
|
17
|
+
if (!storing) {
|
|
18
|
+
let begun = () => undefined;
|
|
19
|
+
this.stored.set(start, new Promise((resolve) => {
|
|
20
|
+
begun = resolve;
|
|
21
|
+
}));
|
|
22
|
+
return begun;
|
|
23
|
+
}
|
|
24
|
+
const waited = new AbortController();
|
|
25
|
+
const limit = setTimeout(MAX_WAIT_MS, undefined, {
|
|
26
|
+
signal: AbortSignal.any([signal, waited.signal]),
|
|
27
|
+
}).catch(() => undefined);
|
|
28
|
+
await Promise.race([storing, limit]);
|
|
29
|
+
waited.abort();
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/wire.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Effort, ModelPrice, QuotaError, ToolDefinition, Usage } from "@open-cr-agent/core";
|
|
2
|
+
import type { SharedStarts } from "./shared-starts.js";
|
|
2
3
|
export interface Endpoint {
|
|
3
4
|
baseUrl: string;
|
|
4
5
|
apiKey?: string;
|
|
@@ -21,6 +22,7 @@ export interface CallError {
|
|
|
21
22
|
quota?: QuotaError;
|
|
22
23
|
transient?: boolean;
|
|
23
24
|
effortRefused?: boolean;
|
|
25
|
+
toolsRefused?: boolean;
|
|
24
26
|
}
|
|
25
27
|
export type Answer = ({
|
|
26
28
|
ok: true;
|
|
@@ -32,6 +34,7 @@ export type Answer = ({
|
|
|
32
34
|
error: CallError;
|
|
33
35
|
}) & {
|
|
34
36
|
effortDropped?: true;
|
|
37
|
+
toolsDropped?: true;
|
|
35
38
|
};
|
|
36
39
|
export interface Dialogue {
|
|
37
40
|
say(text: string, shared?: readonly number[]): void;
|
|
@@ -47,6 +50,8 @@ interface DialogueStart {
|
|
|
47
50
|
price: ModelPrice;
|
|
48
51
|
system: string;
|
|
49
52
|
oneRequest?: boolean;
|
|
53
|
+
textTools?: boolean;
|
|
54
|
+
starts?: SharedStarts;
|
|
50
55
|
}
|
|
51
56
|
export type Wire = (start: DialogueStart) => Dialogue;
|
|
52
57
|
export declare function withoutEffort(settings: CallSettings): CallSettings;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-cr-agent/runtime-direct",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "Agent runtime for Open-CR-Agent that calls declared OpenAI-compatible and Anthropic endpoints directly",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-review",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"homepage": "https://ocracloud.com",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/
|
|
15
|
+
"url": "git+https://github.com/ocrahq/Open-CR-Agent.git",
|
|
16
16
|
"directory": "packages/runtime-direct"
|
|
17
17
|
},
|
|
18
|
-
"bugs": "https://github.com/
|
|
18
|
+
"bugs": "https://github.com/ocrahq/Open-CR-Agent/issues",
|
|
19
19
|
"engines": {
|
|
20
20
|
"node": ">=22.19"
|
|
21
21
|
},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@anthropic-ai/sdk": "^1.12.0",
|
|
38
|
-
"@open-cr-agent/core": "0.
|
|
38
|
+
"@open-cr-agent/core": "0.19.0",
|
|
39
39
|
"zod": "^4.6.6"
|
|
40
40
|
},
|
|
41
41
|
"publishConfig": {
|