@open-cr-agent/runtime-direct 0.15.0 → 0.16.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/anthropic.js CHANGED
@@ -8,7 +8,8 @@ const MAX_OUTPUT_TOKENS = 32_000;
8
8
  const CACHE_WRITE_FACTOR = 1.25;
9
9
  const CACHED = { cache_control: { type: "ephemeral" } };
10
10
  // The API takes four cache marks a request: the system prompt's, the newest
11
- // block's, and these.
11
+ // block's, and these. A dialogue of one request leaves its newest block
12
+ // unmarked and has that mark for a shared start.
12
13
  const MAX_SHARED_MARKS = 2;
13
14
  // How the API names the effort parameters in a 400 that refuses them.
14
15
  const EFFORT_PARAMETER = /\b(?:effort|output_config|thinking)\b/i;
@@ -37,7 +38,8 @@ export const anthropicWire = ({ endpoint, model, price, system, oneRequest }) =>
37
38
  const send = (request, signal) => onceMore(() => stream(client, request, price, secrets, signal), signal);
38
39
  return {
39
40
  say: (text, shared = []) => {
40
- messages.push({ role: "user", content: textBlocks(text, shared) });
41
+ const marks = oneRequest ? MAX_SHARED_MARKS + 1 : MAX_SHARED_MARKS;
42
+ messages.push({ role: "user", content: textBlocks(text, shared, marks) });
41
43
  },
42
44
  ask: async (tools, settings, signal) => {
43
45
  const request = (sent) => ({
@@ -110,14 +112,15 @@ function effortParams(effort) {
110
112
  ...(effort === "none" ? {} : { thinking: { type: "adaptive" } }),
111
113
  };
112
114
  }
113
- // The text as blocks that end where a shared start does, each marked but the
114
- // last: together they read as the text does.
115
- function textBlocks(text, shared) {
116
- const ends = shared.filter((end) => end > 0 && end < text.length).slice(-MAX_SHARED_MARKS);
115
+ // The text as blocks that end where a shared start does: together they read
116
+ // as the text does. The first `marks` are stored; a later one only ends a
117
+ // block, which is where a request looks for what another stored.
118
+ function textBlocks(text, shared, marks) {
119
+ const ends = shared.filter((end) => end > 0 && end < text.length);
117
120
  const blocks = [];
118
121
  let from = 0;
119
- for (const end of ends) {
120
- blocks.push({ type: "text", text: text.slice(from, end), ...CACHED });
122
+ for (const [i, end] of ends.entries()) {
123
+ blocks.push({ type: "text", text: text.slice(from, end), ...(i < marks ? CACHED : {}) });
121
124
  from = end;
122
125
  }
123
126
  return [...blocks, { type: "text", text: text.slice(from) }];
package/dist/loop.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type ModelPrice, type ReviewContext, type TaskAttempt, type ToolDefinition, type Usage } from "@open-cr-agent/core";
1
+ import { type ModelPrice, type RateLimits, type ReviewContext, type TaskAttempt, type ToolDefinition, type Usage } from "@open-cr-agent/core";
2
2
  import { type CallSettings, type Endpoint, type Wire } from "./wire.js";
3
3
  export interface LoopInput {
4
4
  wire: Wire;
@@ -19,6 +19,7 @@ export interface LoopInput {
19
19
  onEffortRefused?: () => void;
20
20
  signal: AbortSignal;
21
21
  onUsage?: (spent: Usage) => void;
22
+ limits?: RateLimits;
22
23
  }
23
24
  interface WrapUpTurn {
24
25
  message: string;
package/dist/loop.js CHANGED
@@ -49,12 +49,8 @@ class Conversation {
49
49
  let done = false;
50
50
  dialogue.say(message.text, message.shared);
51
51
  while (outcome.steps < maxSteps) {
52
- const response = await dialogue.ask(offered, this.settings, this.signal);
52
+ const response = await this.ask(offered);
53
53
  outcome.steps += 1;
54
- if (response.effortDropped) {
55
- this.settings = withoutEffort(this.settings);
56
- input.onEffortRefused?.();
57
- }
58
54
  if (!response.ok) {
59
55
  outcome.error = this.timeout.aborted
60
56
  ? { message: `timed out after ${Math.round(input.timeoutMs / 1000)}s`, retryable: true }
@@ -65,6 +61,9 @@ class Conversation {
65
61
  input.onUsage?.(outcome.usage);
66
62
  if (response.text)
67
63
  texts.push(response.text);
64
+ // A completion's tools are only there to match a task's request.
65
+ if (input.oneRequest)
66
+ break;
68
67
  if (response.calls.length === 0) {
69
68
  const stoppedEarly = resume && !outcome.resumed && texts.join("").trim() === "";
70
69
  if (stoppedEarly && outcome.steps < maxSteps) {
@@ -93,6 +92,22 @@ class Conversation {
93
92
  delete outcome.atStepCap;
94
93
  return { ...snapshot(outcome), ...exploredBy(uses) };
95
94
  }
95
+ // One step's answer. A request the endpoint rate-limits is sent again once
96
+ // the model's pause is over: the conversation is kept, where a new attempt
97
+ // would ask and pay for every step before it again.
98
+ async ask(offered) {
99
+ for (;;) {
100
+ const response = await this.dialogue.ask(offered, this.settings, this.signal);
101
+ if (response.effortDropped) {
102
+ this.settings = withoutEffort(this.settings);
103
+ this.input.onEffortRefused?.();
104
+ }
105
+ const quota = response.ok ? undefined : response.error.quota;
106
+ if (!quota || this.signal.aborted || !(await this.input.limits?.waitOut(quota))) {
107
+ return response;
108
+ }
109
+ }
110
+ }
96
111
  }
97
112
  // A later turn adds to the outcome; what an earlier turn returned stays as
98
113
  // it was.
package/dist/runtime.js CHANGED
@@ -31,7 +31,7 @@ export class DirectRuntime {
31
31
  this.efforts = new EffortLedger(this.sampling);
32
32
  this.chains = new ChainRunner(options.models, {
33
33
  refuse: (chain) => this.unreachable(chain),
34
- task: (model, spec, signal, onUsage) => this.attemptTask(model, spec, signal, onUsage),
34
+ task: (model, spec, signal, onUsage, limits) => this.attemptTask(model, spec, signal, onUsage, limits),
35
35
  complete: (model, request, signal) => this.attemptCompletion(model, request, signal),
36
36
  });
37
37
  }
@@ -66,7 +66,7 @@ export class DirectRuntime {
66
66
  complete(request, signal) {
67
67
  return this.chains.complete(request, signal);
68
68
  }
69
- attemptTask(model, spec, signal, onUsage) {
69
+ attemptTask(model, spec, signal, onUsage, limits) {
70
70
  return runLoop({
71
71
  ...this.target(model),
72
72
  ...this.call({ agent: spec.reviewer, effort: spec.effort, model }),
@@ -81,6 +81,7 @@ export class DirectRuntime {
81
81
  timeoutMs: spec.timeoutMs,
82
82
  signal,
83
83
  onUsage,
84
+ limits,
84
85
  });
85
86
  }
86
87
  attemptCompletion(model, request, signal) {
@@ -89,7 +90,9 @@ export class DirectRuntime {
89
90
  ...this.call({ agent: request.agent ?? request.tier, effort: request.effort, model }),
90
91
  system: request.system,
91
92
  user: request.user,
92
- tools: [],
93
+ ...(request.sharedPrefixes ? { shared: request.sharedPrefixes } : {}),
94
+ // As the task that follows opens, so that it reads what this stores.
95
+ tools: request.sharedPrefixes ? this.tools : [],
93
96
  context: NO_CONTEXT,
94
97
  maxSteps: 1,
95
98
  oneRequest: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-cr-agent/runtime-direct",
3
- "version": "0.15.0",
3
+ "version": "0.16.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",
@@ -35,7 +35,7 @@
35
35
  ],
36
36
  "dependencies": {
37
37
  "@anthropic-ai/sdk": "^1.12.0",
38
- "@open-cr-agent/core": "0.15.0",
38
+ "@open-cr-agent/core": "0.16.0",
39
39
  "zod": "^4.6.6"
40
40
  },
41
41
  "publishConfig": {