@open-cr-agent/runtime-direct 0.13.0 → 0.15.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
@@ -7,6 +7,9 @@ const MAX_OUTPUT_TOKENS = 32_000;
7
7
  // Anthropic charges a quarter above the input rate for input it stores.
8
8
  const CACHE_WRITE_FACTOR = 1.25;
9
9
  const CACHED = { cache_control: { type: "ephemeral" } };
10
+ // The API takes four cache marks a request: the system prompt's, the newest
11
+ // block's, and these.
12
+ const MAX_SHARED_MARKS = 2;
10
13
  // How the API names the effort parameters in a 400 that refuses them.
11
14
  const EFFORT_PARAMETER = /\b(?:effort|output_config|thinking)\b/i;
12
15
  // The API has no level under "low", and its newer models cannot stop
@@ -21,22 +24,27 @@ const LEVELS = {
21
24
  // A conversation in Anthropic's Messages protocol, streamed so a long answer
22
25
  // does not sit behind an idle connection. The system prompt and the
23
26
  // conversation so far are marked for the provider's prompt cache at every
24
- // request: an agent's next step reads what the last one stored.
25
- export const anthropicWire = ({ endpoint, model, price, system }) => {
27
+ // request: an agent's next step reads what the last one stored. A start the
28
+ // run's other conversations open with is marked too, so they read it from
29
+ // the first that stored it (ADR-0064). A dialogue of one request has no next
30
+ // step, and storing costs a quarter more than sending, so its conversation
31
+ // goes unmarked; its system prompt stays marked for the other calls of its
32
+ // kind.
33
+ export const anthropicWire = ({ endpoint, model, price, system, oneRequest }) => {
26
34
  const client = clientOf(endpoint);
27
35
  const secrets = endpoint.apiKey ? [endpoint.apiKey] : [];
28
36
  const messages = [];
29
37
  const send = (request, signal) => onceMore(() => stream(client, request, price, secrets, signal), signal);
30
38
  return {
31
- say: (text) => {
32
- messages.push({ role: "user", content: [{ type: "text", text }] });
39
+ say: (text, shared = []) => {
40
+ messages.push({ role: "user", content: textBlocks(text, shared) });
33
41
  },
34
42
  ask: async (tools, settings, signal) => {
35
43
  const request = (sent) => ({
36
44
  model,
37
45
  max_tokens: MAX_OUTPUT_TOKENS,
38
46
  system: [{ type: "text", text: system, ...CACHED }],
39
- messages: withBreakpoint(messages),
47
+ messages: oneRequest ? messages : withBreakpoint(messages),
40
48
  ...(tools.length > 0 ? { tools: tools.map(toolSpec) } : {}),
41
49
  ...effortParams(sent.effort),
42
50
  });
@@ -102,6 +110,18 @@ function effortParams(effort) {
102
110
  ...(effort === "none" ? {} : { thinking: { type: "adaptive" } }),
103
111
  };
104
112
  }
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);
117
+ const blocks = [];
118
+ let from = 0;
119
+ for (const end of ends) {
120
+ blocks.push({ type: "text", text: text.slice(from, end), ...CACHED });
121
+ from = end;
122
+ }
123
+ return [...blocks, { type: "text", text: text.slice(from) }];
124
+ }
105
125
  // The last block of the conversation carries the cache mark; the stored
106
126
  // messages stay unmarked, so each request has one.
107
127
  function withBreakpoint(messages) {
package/dist/loop.d.ts CHANGED
@@ -7,9 +7,11 @@ export interface LoopInput {
7
7
  price: ModelPrice;
8
8
  system: string;
9
9
  user: string;
10
+ shared?: readonly number[];
10
11
  tools: readonly ToolDefinition[];
11
12
  context: ReviewContext;
12
13
  maxSteps: number;
14
+ oneRequest?: boolean;
13
15
  resume?: string;
14
16
  wrapUp?: WrapUpTurn;
15
17
  timeoutMs: number;
package/dist/loop.js CHANGED
@@ -7,14 +7,14 @@ import { withoutEffort, } from "./wire.js";
7
7
  // for the pipeline to validate.
8
8
  export async function runLoop(input) {
9
9
  const conversation = new Conversation(input);
10
- const outcome = await conversation.turn(input.user, input.tools, input.maxSteps, input.resume);
10
+ const outcome = await conversation.turn({ text: input.user, ...(input.shared ? { shared: input.shared } : {}) }, input.tools, input.maxSteps, input.resume);
11
11
  const { wrapUp } = input;
12
12
  if (!wrapUp)
13
13
  return outcome;
14
14
  const tools = input.tools.filter((t) => wrapUp.tools.includes(t.name));
15
15
  return {
16
16
  ...outcome,
17
- wrapUp: () => conversation.turn(wrapUp.message, tools, outcome.steps + wrapUp.steps),
17
+ wrapUp: () => conversation.turn({ text: wrapUp.message }, tools, outcome.steps + wrapUp.steps),
18
18
  };
19
19
  }
20
20
  // One attempt's dialogue and outcome, which a later turn continues under the
@@ -47,7 +47,7 @@ class Conversation {
47
47
  const { input, dialogue, outcome, texts, uses } = this;
48
48
  const tools = new Map(offered.map((t) => [t.name, t]));
49
49
  let done = false;
50
- dialogue.say(message);
50
+ dialogue.say(message.text, message.shared);
51
51
  while (outcome.steps < maxSteps) {
52
52
  const response = await dialogue.ask(offered, this.settings, this.signal);
53
53
  outcome.steps += 1;
package/dist/runtime.js CHANGED
@@ -72,6 +72,7 @@ export class DirectRuntime {
72
72
  ...this.call({ agent: spec.reviewer, effort: spec.effort, model }),
73
73
  system: spec.systemPrompt,
74
74
  user: spec.userPrompt,
75
+ ...(spec.sharedPrefixes ? { shared: spec.sharedPrefixes } : {}),
75
76
  tools: this.tools,
76
77
  context: spec.context,
77
78
  maxSteps: MAX_AGENT_STEPS,
@@ -91,6 +92,7 @@ export class DirectRuntime {
91
92
  tools: [],
92
93
  context: NO_CONTEXT,
93
94
  maxSteps: 1,
95
+ oneRequest: true,
94
96
  timeoutMs: request.timeoutMs,
95
97
  signal,
96
98
  });
package/dist/wire.d.ts CHANGED
@@ -34,7 +34,7 @@ export type Answer = ({
34
34
  effortDropped?: true;
35
35
  };
36
36
  export interface Dialogue {
37
- say(text: string): void;
37
+ say(text: string, shared?: readonly number[]): void;
38
38
  ask(tools: readonly ToolDefinition[], settings: CallSettings, signal: AbortSignal): Promise<Answer>;
39
39
  answer(outputs: readonly {
40
40
  call: ToolRequest;
@@ -46,6 +46,7 @@ interface DialogueStart {
46
46
  model: string;
47
47
  price: ModelPrice;
48
48
  system: string;
49
+ oneRequest?: boolean;
49
50
  }
50
51
  export type Wire = (start: DialogueStart) => Dialogue;
51
52
  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.13.0",
3
+ "version": "0.15.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.13.0",
38
+ "@open-cr-agent/core": "0.15.0",
39
39
  "zod": "^4.6.6"
40
40
  },
41
41
  "publishConfig": {