@parcha/agentrun-kernel 0.1.0-beta.37 → 0.1.0-beta.39

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.
Files changed (70) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/NOTICE +2 -0
  3. package/dist/booking.d.ts +25 -6
  4. package/dist/booking.js +15 -4
  5. package/dist/host/index.d.ts +4 -5
  6. package/dist/host/index.js +3 -3
  7. package/dist/host/o200k.d.ts +11 -0
  8. package/dist/host/o200k.js +185 -0
  9. package/dist/host/overhead.d.ts +105 -3
  10. package/dist/host/overhead.js +276 -20
  11. package/dist/host/proxy.d.ts +97 -16
  12. package/dist/host/proxy.js +420 -108
  13. package/dist/index.d.ts +6 -5
  14. package/dist/index.js +4 -3
  15. package/dist/kernel/cap/admit.d.ts +5 -8
  16. package/dist/kernel/cap/admit.js +13 -19
  17. package/dist/kernel/cap/free.d.ts +43 -0
  18. package/dist/kernel/cap/free.js +71 -0
  19. package/dist/kernel/cap/index.d.ts +7 -6
  20. package/dist/kernel/cap/index.js +6 -5
  21. package/dist/kernel/cap/marks.d.ts +2 -3
  22. package/dist/kernel/cap/park.d.ts +3 -4
  23. package/dist/kernel/cap/park.js +4 -5
  24. package/dist/kernel/cap/port.d.ts +11 -5
  25. package/dist/kernel/cap/port.js +26 -24
  26. package/dist/kernel/cap/price.d.ts +10 -30
  27. package/dist/kernel/cap/price.js +15 -48
  28. package/dist/kernel/cap/recover.d.ts +1 -2
  29. package/dist/kernel/cap/recover.js +2 -4
  30. package/dist/kernel/cap/rule.d.ts +11 -17
  31. package/dist/kernel/cap/rule.js +37 -19
  32. package/dist/kernel/cap/run-cap.d.ts +25 -10
  33. package/dist/kernel/cap/run-cap.js +78 -35
  34. package/dist/kernel/cap/types.d.ts +11 -27
  35. package/dist/kernel/cap/words.d.ts +13 -8
  36. package/dist/kernel/cap/words.js +23 -14
  37. package/dist/kernel/cap/wrap-up.d.ts +4 -0
  38. package/dist/kernel/cap/wrap-up.js +5 -1
  39. package/dist/kernel/registry/computer-surface.generated.d.ts +1 -1
  40. package/dist/kernel/registry/computer-surface.generated.js +18 -27
  41. package/dist/kernel/registry/computer.d.ts +14 -7
  42. package/dist/kernel/registry/computer.js +64 -16
  43. package/dist/kernel/registry/gate.d.ts +2 -2
  44. package/dist/kernel/registry/gate.js +4 -7
  45. package/dist/kernel/registry/hands.d.ts +4 -5
  46. package/dist/kernel/registry/index.d.ts +1 -1
  47. package/dist/kernel/registry/index.js +1 -1
  48. package/dist/kernel/registry/registry.js +6 -9
  49. package/dist/kernel/types.d.ts +8 -1
  50. package/dist/process/recovery.d.ts +4 -1
  51. package/dist/process/recovery.js +15 -3
  52. package/dist/process/resume.d.ts +5 -2
  53. package/dist/process/resume.js +15 -3
  54. package/dist/process/tools.d.ts +28 -1
  55. package/dist/process/tools.js +102 -5
  56. package/dist/run-booking.d.ts +21 -2
  57. package/dist/run-booking.js +215 -18
  58. package/dist/runner.d.ts +82 -21
  59. package/dist/runner.js +271 -56
  60. package/dist/testing.d.ts +10 -4
  61. package/dist/testing.js +7 -5
  62. package/dist/tool-deferral.d.ts +12 -0
  63. package/dist/tool-deferral.js +17 -0
  64. package/dist/types.d.ts +133 -48
  65. package/dist/types.js +8 -0
  66. package/package.json +5 -3
  67. package/vendor/o200k/LICENSE +61 -0
  68. package/vendor/o200k/o200k_base.tiktoken +199998 -0
  69. package/dist/host/mcp-route.d.ts +0 -14
  70. package/dist/host/mcp-route.js +0 -125
@@ -1,6 +1,7 @@
1
1
  import { type IncomingHttpHeaders } from "node:http";
2
- import { type McpClient, type NoteRequest, type SessionMcp } from "../types.js";
2
+ import { type NoteRequest } from "../types.js";
3
3
  import { type Ended } from "../booking.js";
4
+ import type { BilledRequest } from "./overhead.js";
4
5
  /** A route a policy admits: its method, its exact path, and the request headers forwarded with it. */
5
6
  export interface ProxyRoute {
6
7
  readonly method: string;
@@ -19,7 +20,17 @@ export interface ProxyUsage {
19
20
  output: number;
20
21
  cacheRead: number;
21
22
  cacheWrite?: number;
23
+ /** `plan`: the policy's route bills a person's subscription plan, not the tokens (ModelUsage.billing). */
24
+ billing?: "plan";
22
25
  }
26
+ /** A provider credential its host can't give now, for good until a person acts (a sign-in signed out or expired): thrown
27
+ * by `ModelProxyOptions.credential`, its message (words for a person, never a credential) is the request's refusal. */
28
+ export declare class CredentialUnavailable extends Error {
29
+ }
30
+ /** What one provider-native web search costs, in USD, beside the tokens: $10 per 1,000 searches (Anthropic's "Web search tool"
31
+ * and OpenAI's "Web search (all models)" list the same price). The one figure every fee reads: the proxy policies of the
32
+ * Claude and Codex harnesses and the runner's hosted search. */
33
+ export declare const NATIVE_SEARCH_USD = 0.01;
23
34
  /** What one provider's API may carry through the proxy. */
24
35
  export interface ProviderPolicy {
25
36
  /** A name for messages. */
@@ -92,9 +103,23 @@ export interface ProviderPolicy {
92
103
  * through a gateway or any other base, so the key and the request go nowhere the request itself would not. Its count,
93
104
  * plus `margin`, is that request's input bound when it is below the byte bound. Absent: never counted. */
94
105
  countInput?: InputCounter;
106
+ /** A local count of the body `apply` returned, its margin included (`responsesLocalInputTokens`), for a request the
107
+ * provider did not count: no `countInput`, an upstream that is not its origin (a gateway), a side or host request, or a
108
+ * count that failed or said none. That request's input bound when it is below the byte bound. Undefined: the body has
109
+ * no local count (a model whose tokenizer the policy does not have), and keeps its byte bound. Absent: never counted. */
110
+ countInputLocally?(body: Record<string, unknown>): Promise<number | undefined> | number | undefined;
111
+ /** The most input tokens the body `apply` returned can be billed for, from `earlier`, the session's last request on
112
+ * the same model whose answer reported what its whole input billed (`messagesInputAfter`): for a request the provider
113
+ * did not count, its input bound when it is below its other bounds. Undefined: the earlier request says nothing of this
114
+ * one (they differ in more than what this one adds). Absent: never used. */
115
+ inputAfter?(earlier: BilledRequest, body: Record<string, unknown>): number | undefined;
95
116
  /** The least output the body may be limited to and still answer: 1,024 tokens for the answer, plus the least thinking
96
117
  * its provider takes when it thinks. Absent: 1,024. */
97
118
  outputLeast?(body: Record<string, unknown>): number;
119
+ /** The reasoning effort the body asks its model at, as its provider names it (Responses' `reasoning.effort`,
120
+ * Messages' `output_config.effort`): what `outputLeast` read it at, handed to the host beside it (ProxyRequest.effort).
121
+ * Absent, or undefined for a body: it names none. */
122
+ effortOf?(body: Record<string, unknown>): string | undefined;
98
123
  /** The reasoning tokens the body's request may bill that its response does not stream (a provider that streams only a
99
124
  * summary of its reasoning, or none): its effort's or its thinking's budget, else its output limit when it reasons at
100
125
  * an effort the body does not say. A stopped request counts them beside what streamed. Absent or 0: none. */
@@ -117,6 +142,13 @@ export interface ProviderPolicy {
117
142
  addNote?(body: Record<string, unknown>, text: string): Record<string, unknown>;
118
143
  /** The status a request the runner refused gets: one the client does not retry. Default 400. */
119
144
  readonly refusedStatus?: number;
145
+ /** An error answer of the provider's that no retry can mend (a subscription plan's limit reached), read from its status
146
+ * and its body: the words and code its client is answered with instead, at `refusedStatus`, so the client never asks
147
+ * again and its run ends in those words. Undefined: the provider's answer goes through as it came. */
148
+ finalError?(status: number, body: string): {
149
+ message: string;
150
+ code: string;
151
+ } | undefined;
120
152
  }
121
153
  export interface ModelUsageReport extends ProxyUsage {
122
154
  /** The provider's (or its gateway's) own figure when the response gave one (`reportedCostOf`); else the price table's,
@@ -185,22 +217,22 @@ export interface ProxyRequest {
185
217
  outputTokensAsked?: number;
186
218
  /** The least output it may be limited to and still answer (the policy's `outputLeast`). */
187
219
  outputTokensLeast?: number;
220
+ /** The reasoning effort its body names (the policy's `effortOf`), when it names one. */
221
+ effort?: string;
188
222
  /** What it may charge beside its tokens at most (the policy's `feeMaxUsd`), in USD: held with them. */
189
223
  feeUsd?: number;
190
224
  /** A side call, never its turn's own: on another model than the session's (a client's small-model call: a title, a
191
225
  * summary), or the host's own request beside the harness's (`host`). */
192
226
  side?: true;
193
227
  /** On the session's model but no answer of the turn (the policy's `answers` reads it so: a title, a summary, a child's
194
- * request). Still the turn's to book, but a conversation of its own: the cap never prices the session's next request
195
- * from it, nor it from the session's. */
228
+ * request). Still the turn's to book, but no answer the host keeps its wrap-up's reserve back for. */
196
229
  unanswered?: true;
197
- /** It carries the host's note (`ModelProxyHooks.note`), placed in its body: the host's to record once it is admitted. */
230
+ /** It carries the host's note (`ModelProxyHooks.note`), placed in its body: the host's to record once it is answered. */
198
231
  noted?: true;
199
232
  /** The label of the side key it came under (ModelProxy.sideKey): the host's own request, beside the harness's. */
200
233
  host?: string;
201
- /** Its `inputTokensMax` is its provider's count (ProviderPolicy.countInput), not its bytes: a host that compares one
202
- * request's bound with another's (how far a conversation grew) pairs a counted bound only with a counted one. */
203
- inputTokensCounted?: true;
234
+ /** Aborted when its client goes before it is admitted (ModelRequest.signal): a wait for room ends then. */
235
+ signal?: AbortSignal;
204
236
  }
205
237
  export interface ModelProxyHooks {
206
238
  /** Before a paid request leaves; a throw refuses it. What it returns (a ticket) is handed back to this request's
@@ -251,18 +283,13 @@ export interface ModelProxyOptions {
251
283
  free: boolean;
252
284
  status: number;
253
285
  }) => void;
286
+ /** Told the credential a request was sent with when the provider answered 401 to it, so a host whose credential
287
+ * renews (a sign-in's token) can renew it before the next request. Never logged. */
288
+ credentialRefused?: (credential: string) => void | Promise<void>;
254
289
  /** Headers the host adds to every request it forwards (a gateway's spend tags, LiteLLM's `x-litellm-tags`, say):
255
290
  * after the client's, so the client can neither set nor override them, and before the credential's. Checked when the
256
291
  * proxy starts (`hostHeaderProblems`). */
257
292
  headers?: Readonly<Record<string, string>>;
258
- /** The session's remote MCP servers, each mounted here at `/mcp/<server>` behind the session's key (mcp-route.ts),
259
- * for the harness's own MCP client as its adapter declares it (`client`, AdapterCapabilities.mcpClient). One mount for
260
- * every harness: an adapter hands the session's MCP, never a route of its own. */
261
- mcp?: {
262
- session: SessionMcp;
263
- client: McpClient;
264
- keepaliveMs?: number;
265
- };
266
293
  /** For tests: the fetch the proxy forwards with. */
267
294
  fetch?: typeof fetch;
268
295
  /** Told, with CONTENT_FILTERED, once an answer the provider stopped on its content filter has been passed and booked
@@ -274,6 +301,9 @@ export interface ModelProxyOptions {
274
301
  onCut?: (stop: {
275
302
  outputTokens?: number;
276
303
  }) => void;
304
+ /** Told, with its words and code, once the provider's error answer is one no retry mends (`ProviderPolicy.finalError`:
305
+ * a plan's limit reached), as its client is answered those words: the harness ends the turn in them. */
306
+ onFinal?: (message: string, code: string) => void;
277
307
  }
278
308
  /** What is wrong with headers a host would add to forwarded requests: a name that is not a token or is reserved, a value
279
309
  * an HTTP header cannot carry (a line break or other control character, or a character beyond Latin-1, which the fetch
@@ -289,7 +319,7 @@ export interface ModelProxy {
289
319
  * harness. A request under it goes through the same policy, origin, credential, price and hooks as the harness's;
290
320
  * its admission names the label (ModelRequest.host), the policy applies it as the host's (`ProxyRequestContext`),
291
321
  * it is never the turn's answer request (a content-filter stop on it never halts the turn) and never watched. It opens
292
- * the model routes only: the session's MCP door stays behind the session's key. */
322
+ * the model routes only. */
293
323
  sideKey(label: string): string;
294
324
  /** The harness opened its next turn: once the proxy has told it a turn's answer stopped (onCut, onFiltered), it refuses
295
325
  * the session's answer requests, unsent and unbooked, until this is called. */
@@ -301,6 +331,13 @@ export interface ModelProxy {
301
331
  export interface ProxyRequestContext {
302
332
  readonly host?: string;
303
333
  }
334
+ /** The last `"<key>": {...}` object in `text`, parsed: how a policy reads a field of an answer's terminal event when the
335
+ * event's line was longer than USAGE_WINDOW, so the proxy kept only its end. JSON may put whitespace on either side of
336
+ * the colon (a Python gateway's `json.dumps` writes `"usage": {`), and the client parses the event whatever its spacing,
337
+ * so this reads it too: a usage the harness counted is never booked at an unknown cost for its spacing. A key's quoted
338
+ * name inside a JSON string is escaped (`\"usage\"`), so it never reads as one. Undefined when there is none, or the
339
+ * last one does not close in `text` or does not parse. */
340
+ export declare function lastObjectOf(text: string, key: string): unknown;
304
341
  /** The ids a response's headers give its request, by header name, so a run's books can be joined to the provider's (or
305
342
  * a gateway's) request by request; undefined when there are none. Only id-named headers with id-shaped values. */
306
343
  export declare function providerIdsOf(headers: Headers): Record<string, string> | undefined;
@@ -308,6 +345,44 @@ export declare function providerIdsOf(headers: Headers): Record<string, string>
308
345
  * the discount and margin it reports are both exactly 0. Only a positive number counts. A missing, malformed or zero
309
346
  * figure is unknown, never $0: a streamed answer's headers leave before it is priced, and LiteLLM writes 0.0 there. */
310
347
  export declare function reportedCostOf(headers: Headers): number | undefined;
348
+ /** The origin a route with its own fetch names (serve's translation to a model that speaks neither API): nothing is ever
349
+ * sent there, its fetch answers every request, and it writes an answer's cost in its usage only where the provider
350
+ * stated one (OpenRouter's). */
351
+ export declare const ROUTE_ORIGIN = "https://model-route.invalid";
352
+ /** Whether every answer from `origin` states what it cost in its usage's `cost`, in USD (`usageCostOf`): OpenRouter's
353
+ * (https://openrouter.ai/docs/use-cases/usage-accounting, on every API it serves), and a translation route's
354
+ * (`ROUTE_ORIGIN`), which passes OpenRouter's figure on. No other origin's usage is read for a cost. */
355
+ export declare function statesUsageCost(origin: string): boolean;
356
+ /** What an answer says it cost in its own usage, when it says so (OpenRouter's usage accounting, on every answer of every
357
+ * API it serves: https://openrouter.ai/docs/use-cases/usage-accounting): the answer's last `usage` object (a stream's
358
+ * terminal event, a whole body's own), its `cost` in USD. With the person's own provider key (`is_byok`), `cost` is
359
+ * OpenRouter's fee alone and the provider's charge is `cost_details.upstream_inference_cost` (or its prompt and
360
+ * completion parts): the two together, else unknown. Only a positive figure counts, as for a gateway's header
361
+ * (reportedCostOf): a missing, malformed or zero one is unknown, never $0. */
362
+ export declare function usageCostOf(text: string): number | undefined;
363
+ /** A provider's error inside a stream it began with a 200: its words, and whether it is a rate limit. */
364
+ export type StreamError = {
365
+ words: string;
366
+ rateLimit: boolean;
367
+ };
368
+ /** The provider's error its answer, a stream it began with a 200, ends in: a provider answers 200 once it takes the
369
+ * request, before the model's first token (OpenRouter; Anthropic's stream too), so an overload, a rate limit or a failure
370
+ * met after that comes inside the stream, and the status could not say so. Read from the stream's last event that carries
371
+ * data (`[DONE]` read past), and only that one, parsed: an event whose data is not JSON (words a translation wrote) is
372
+ * none. Undefined for any other answer, a whole JSON body's included. */
373
+ export declare function streamErrorOf(text: string): StreamError | undefined;
374
+ /** The words of a stream's last event that carries data when it is an error event (`event: error`) whose data is words,
375
+ * not JSON: how a translation sends a failure it cannot read for the provider's error event (serve's model-route
376
+ * messagesFetch: a provider's error status, a transport error), for its client to throw as the request's error. Never
377
+ * read for a refusal: what such a failure cost is unknown. Undefined for any other stream. */
378
+ export declare function wordsErrorOf(text: string): string | undefined;
379
+ /** Whether a stream shows the model began its output, read from each event's data by its fields, never its words: a
380
+ * Messages block it opened (`content_block_start`), a Responses output item (`response.output_item.added`) or a
381
+ * Responses delta with something in it, a Chat Completions chunk whose delta holds content, reasoning, a refusal or a
382
+ * tool call. Its content need not have streamed: a model's hidden reasoning (a Responses reasoning item) shows none, and
383
+ * neither does a block a translation held until its end. An empty delta (a first chunk's `content: ""`, the one a
384
+ * mid-stream error comes in) is no output. */
385
+ export declare function outputBeganIn(text: string): boolean;
311
386
  export declare class ProxyRefusal extends Error {
312
387
  readonly status: number;
313
388
  constructor(status: number, message: string);
@@ -345,6 +420,12 @@ export declare const COUNT_TIMEOUT_MS = 3000;
345
420
  * the counter's own, or the count fails, times out or says no count. Never throws: a count is only ever a tighter bound.
346
421
  */
347
422
  export declare function countedInputOf(policy: ProviderPolicy, origin: string, body: Record<string, unknown>, headers: Record<string, string>, fetch: typeof globalThis.fetch, say?: (message: string, detail?: Record<string, unknown>) => void, timeoutMs?: number, signal?: AbortSignal): Promise<number | undefined>;
423
+ /**
424
+ * The local count of a request with body `body`, its margin included (`policy.countInputLocally`); undefined (the request
425
+ * keeps its byte bound) when the policy has none, says none for this body, or its count throws or is not a whole number
426
+ * of tokens. Never throws.
427
+ */
428
+ export declare function localInputOf(policy: ProviderPolicy, body: Record<string, unknown>, say?: (message: string, detail?: Record<string, unknown>) => void): Promise<number | undefined>;
348
429
  /** Start a session's model proxy on the loopback interface, admitting `key`. */
349
430
  /**
350
431
  * The most input tokens a request with body `sent` (the body `apply` returned, as it is sent) can be billed for: its UTF-8