@parcha/agentrun-kernel 0.1.0-beta.33 → 0.1.0-beta.35
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/CHANGELOG.md +42 -0
- package/dist/host/index.d.ts +4 -4
- package/dist/host/index.js +3 -3
- package/dist/host/overhead.d.ts +75 -4
- package/dist/host/overhead.js +220 -10
- package/dist/host/pinned-binary.d.ts +27 -2
- package/dist/host/pinned-binary.js +97 -8
- package/dist/host/proxy.d.ts +40 -4
- package/dist/host/proxy.js +73 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/kernel/cap/index.d.ts +2 -2
- package/dist/kernel/cap/index.js +2 -2
- package/dist/kernel/cap/park.js +4 -2
- package/dist/kernel/cap/port.d.ts +3 -2
- package/dist/kernel/cap/port.js +7 -2
- package/dist/kernel/cap/price.js +5 -2
- package/dist/kernel/cap/run-cap.d.ts +21 -5
- package/dist/kernel/cap/run-cap.js +42 -4
- package/dist/kernel/cap/types.d.ts +7 -0
- package/dist/kernel/cap/words.d.ts +9 -0
- package/dist/kernel/cap/words.js +16 -1
- package/dist/kernel/registry/defs.d.ts +3 -6
- package/dist/kernel/registry/defs.js +6 -11
- package/dist/kernel/registry/hands.d.ts +3 -22
- package/dist/kernel/registry/index.d.ts +1 -3
- package/dist/kernel/registry/index.js +3 -3
- package/dist/kernel/registry/registry.d.ts +2 -5
- package/dist/kernel/registry/registry.js +8 -10
- package/dist/process/tools.d.ts +9 -0
- package/dist/process/tools.js +19 -6
- package/dist/run-booking.js +4 -1
- package/dist/runner.d.ts +27 -1
- package/dist/runner.js +55 -8
- package/dist/testing.d.ts +9 -2
- package/dist/testing.js +63 -18
- package/dist/types.d.ts +32 -3
- package/dist/types.js +2 -0
- package/package.json +3 -3
- package/dist/kernel/registry/browser.d.ts +0 -11
- package/dist/kernel/registry/browser.js +0 -14
package/dist/host/proxy.d.ts
CHANGED
|
@@ -70,14 +70,20 @@ export interface ProviderPolicy {
|
|
|
70
70
|
* smaller one for a client's background calls). The runner is asked about, and the price taken for, that model.
|
|
71
71
|
* Absent, or undefined for a body: the session's model. */
|
|
72
72
|
modelOf?(body: Record<string, unknown>): string | undefined;
|
|
73
|
-
/** The input tokens the provider bills for the body `apply` returned
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
73
|
+
/** The input tokens the provider bills for the body `apply` returned beyond its bytes: a prompt it adds itself, such as
|
|
74
|
+
* a tool-use system prompt, and each image at what it is billed as in place of the bytes its data takes in the body
|
|
75
|
+
* (`providerInputOverhead`), which can make it less than 0. Added to the body's bytes for `inputTokensMax`; the sum
|
|
76
|
+
* below 0 is no bound (unmeasured). Undefined: the body has no input bound the policy can state (a hosted tool that
|
|
77
|
+
* reads pages into the input), so `inputTokensMax` is undefined, never 0 or the body's bytes alone. Absent: none. */
|
|
77
78
|
serverInputOverhead?(body: Record<string, unknown>): number | undefined;
|
|
78
79
|
/** The output tokens the body `apply` returned asks for at most (its own limit, as `apply` shaped it). Absent or
|
|
79
80
|
* undefined: the body does not say. */
|
|
80
81
|
outputAsked?(body: Record<string, unknown>): number | undefined;
|
|
82
|
+
/** The provider's own count of a request's input tokens (Anthropic's free count_tokens), which the proxy asks before
|
|
83
|
+
* each of the session's answer requests (`countedInputOf`) when its upstream is exactly `origin`, the provider's own: never
|
|
84
|
+
* through a gateway or any other base, so the key and the request go nowhere the request itself would not. Its count,
|
|
85
|
+
* plus `margin`, is that request's input bound when it is below the byte bound. Absent: never counted. */
|
|
86
|
+
countInput?: InputCounter;
|
|
81
87
|
/** The least output the body may be limited to and still answer: 1,024 tokens for the answer, plus the least thinking
|
|
82
88
|
* its provider takes when it thinks. Absent: 1,024. */
|
|
83
89
|
outputLeast?(body: Record<string, unknown>): number;
|
|
@@ -178,6 +184,9 @@ export interface ProxyRequest {
|
|
|
178
184
|
noted?: true;
|
|
179
185
|
/** The label of the side key it came under (ModelProxy.sideKey): the host's own request, beside the harness's. */
|
|
180
186
|
host?: string;
|
|
187
|
+
/** Its `inputTokensMax` is its provider's count (ProviderPolicy.countInput), not its bytes: a host that compares one
|
|
188
|
+
* request's bound with another's (how far a conversation grew) pairs a counted bound only with a counted one. */
|
|
189
|
+
inputTokensCounted?: true;
|
|
181
190
|
}
|
|
182
191
|
export interface ModelProxyHooks {
|
|
183
192
|
/** Before a paid request leaves; a throw refuses it. What it returns (a ticket) is handed back to this request's
|
|
@@ -200,6 +209,10 @@ export interface ModelProxyOptions {
|
|
|
200
209
|
hooks: ModelProxyHooks;
|
|
201
210
|
/** The session's model, as the runner names it to `beforeRequest`. The policy fixes the body's model. */
|
|
202
211
|
model: string;
|
|
212
|
+
/** This proxy's route among the session's, told to `beforeRequest` with each request (ModelRequest.route) so the host
|
|
213
|
+
* prices it at this proxy's price: a proxy beside the session's for another tier's model names its tier. Absent: the
|
|
214
|
+
* session's own. */
|
|
215
|
+
route?: string;
|
|
203
216
|
/** The provider's base: its origin and, for a gateway that serves the API under a path, that path
|
|
204
217
|
* (`https://api.example.com`, `https://gateway.example/api`). A route's path is appended to it. A base with userinfo, a
|
|
205
218
|
* query or a fragment is refused, so the key goes only where the base reads. */
|
|
@@ -293,6 +306,29 @@ export declare function matchRoute(routes: readonly ProxyRoute[], method: string
|
|
|
293
306
|
export declare function parseStrict(text: string): unknown;
|
|
294
307
|
/** Whether `given` is `key`, in time that does not depend on where they differ. */
|
|
295
308
|
export declare const sameKey: (given: string, key: string) => boolean;
|
|
309
|
+
/** A provider's endpoint that counts a request's input tokens (ProviderPolicy.countInput). */
|
|
310
|
+
export interface InputCounter {
|
|
311
|
+
/** The provider's own base (`https://api.anthropic.com`): counted only when the session's upstream is exactly it. */
|
|
312
|
+
readonly origin: string;
|
|
313
|
+
/** The counting endpoint's path under it (`/v1/messages/count_tokens`). */
|
|
314
|
+
readonly path: string;
|
|
315
|
+
/** The body to count, from the request's (the fields the endpoint takes). */
|
|
316
|
+
body(body: Record<string, unknown>): Record<string, unknown>;
|
|
317
|
+
/** The headers to send it, from the request's as the proxy sends them (its credential and version headers among them). */
|
|
318
|
+
headers(sent: Record<string, string>): Record<string, string>;
|
|
319
|
+
/** The input tokens its answer says, or undefined when it says none. */
|
|
320
|
+
tokensOf(answer: unknown): number | undefined;
|
|
321
|
+
/** What the provider bills beside the count, which the count may leave out (a tool-use system prompt): added to it. */
|
|
322
|
+
margin?(body: Record<string, unknown>): number;
|
|
323
|
+
}
|
|
324
|
+
/** How long a count may take before the request goes on its byte bound: a count never holds a request up for long. */
|
|
325
|
+
export declare const COUNT_TIMEOUT_MS = 3000;
|
|
326
|
+
/**
|
|
327
|
+
* The exact input tokens of a request with body `body`, by its provider's own count (`policy.countInput`), plus its
|
|
328
|
+
* margin; undefined (the request keeps its byte bound) when the policy has no counter, the upstream `origin` is not
|
|
329
|
+
* the counter's own, or the count fails, times out or says no count. Never throws: a count is only ever a tighter bound.
|
|
330
|
+
*/
|
|
331
|
+
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>;
|
|
296
332
|
/** Start a session's model proxy on the loopback interface, admitting `key`. */
|
|
297
333
|
/**
|
|
298
334
|
* The most input tokens a request with body `sent` (the body `apply` returned, as it is sent) can be billed for: its UTF-8
|
package/dist/host/proxy.js
CHANGED
|
@@ -582,6 +582,41 @@ function baseOf(base) {
|
|
|
582
582
|
throw new Error("agentrun-kernel: a model proxy's base is an origin and a path, with no userinfo, query, fragment or backslash");
|
|
583
583
|
return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
|
|
584
584
|
}
|
|
585
|
+
/** How long a count may take before the request goes on its byte bound: a count never holds a request up for long. */
|
|
586
|
+
export const COUNT_TIMEOUT_MS = 3_000;
|
|
587
|
+
/**
|
|
588
|
+
* The exact input tokens of a request with body `body`, by its provider's own count (`policy.countInput`), plus its
|
|
589
|
+
* margin; undefined (the request keeps its byte bound) when the policy has no counter, the upstream `origin` is not
|
|
590
|
+
* the counter's own, or the count fails, times out or says no count. Never throws: a count is only ever a tighter bound.
|
|
591
|
+
*/
|
|
592
|
+
export async function countedInputOf(policy, origin, body, headers, fetch, say = () => { }, timeoutMs = COUNT_TIMEOUT_MS, signal) {
|
|
593
|
+
const counter = policy.countInput;
|
|
594
|
+
if (!counter)
|
|
595
|
+
return undefined;
|
|
596
|
+
try {
|
|
597
|
+
if (baseOf(counter.origin) !== origin)
|
|
598
|
+
return undefined;
|
|
599
|
+
// Cut at its timeout, or when `signal` aborts (its client went).
|
|
600
|
+
const cut = signal ? AbortSignal.any([AbortSignal.timeout(timeoutMs), signal]) : AbortSignal.timeout(timeoutMs);
|
|
601
|
+
const response = await fetch(`${origin}${counter.path}`, { method: "POST", headers: { ...counter.headers(headers), "content-type": "application/json" }, body: JSON.stringify(counter.body(body)), signal: cut });
|
|
602
|
+
if (!response.ok) {
|
|
603
|
+
await response.body?.cancel().catch(() => undefined);
|
|
604
|
+
say(`model proxy (${policy.id}): the provider's token count answered ${response.status}; the request keeps its byte bound`);
|
|
605
|
+
return undefined;
|
|
606
|
+
}
|
|
607
|
+
const tokens = counter.tokensOf(await response.json());
|
|
608
|
+
const margin = counter.margin ? counter.margin(body) : 0;
|
|
609
|
+
if (!Number.isSafeInteger(tokens) || tokens < 0 || !Number.isSafeInteger(margin) || margin < 0) {
|
|
610
|
+
say(`model proxy (${policy.id}): the provider's token count said no count; the request keeps its byte bound`);
|
|
611
|
+
return undefined;
|
|
612
|
+
}
|
|
613
|
+
return tokens + margin;
|
|
614
|
+
}
|
|
615
|
+
catch (error) {
|
|
616
|
+
say(`model proxy (${policy.id}): the provider's token count failed; the request keeps its byte bound`, { error: String(error?.message ?? error).slice(0, 200) });
|
|
617
|
+
return undefined;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
585
620
|
/** Start a session's model proxy on the loopback interface, admitting `key`. */
|
|
586
621
|
/**
|
|
587
622
|
* The most input tokens a request with body `sent` (the body `apply` returned, as it is sent) can be billed for: its UTF-8
|
|
@@ -598,9 +633,13 @@ export function inputTokensMaxOf(policy, body, sent, say = () => { }) {
|
|
|
598
633
|
const overhead = policy.serverInputOverhead ? policy.serverInputOverhead(body) : 0;
|
|
599
634
|
if (overhead === undefined)
|
|
600
635
|
return undefined;
|
|
601
|
-
if (typeof overhead !== "number" || !Number.isSafeInteger(overhead)
|
|
636
|
+
if (typeof overhead !== "number" || !Number.isSafeInteger(overhead))
|
|
602
637
|
throw new Error(`not a token count: ${String(overhead).slice(0, 40)}`);
|
|
603
|
-
|
|
638
|
+
// Below 0 only by images' data the body carries (counted at what they are billed as instead): never past the bytes.
|
|
639
|
+
const bound = Buffer.byteLength(sent, "utf8") + overhead;
|
|
640
|
+
if (bound < 0)
|
|
641
|
+
throw new Error(`the policy counted ${-overhead} more bytes as billed apart than the body's ${Buffer.byteLength(sent, "utf8")}`);
|
|
642
|
+
return bound;
|
|
604
643
|
}
|
|
605
644
|
catch (error) {
|
|
606
645
|
say(`model proxy (${policy.id}): the policy could not say what the provider adds to the body's input; its input is unmeasured`, { error: String(error?.message ?? error).slice(0, 200) });
|
|
@@ -856,6 +895,36 @@ export async function startModelProxy(options, key) {
|
|
|
856
895
|
// admitted or booked, until the harness opens its next turn.
|
|
857
896
|
if (answering && !route.free && stopTold !== undefined)
|
|
858
897
|
return refuse(res, refusedStatus, `refused: the turn's answer was ${stopTold}, and the turn ends there`, req);
|
|
898
|
+
// An answer request on an upstream that is its provider's own (policy.countInput): its input bound is the provider's
|
|
899
|
+
// exact count when that is below its bytes, so a small cap admits a first turn its bytes would not. Each answer request
|
|
900
|
+
// is counted, not only the first: the cap prices a step by the last one's tokens plus how far its bound grew, so a
|
|
901
|
+
// counted bound beside a byte bound would read the bytes as growth. A count that fails or times out leaves the byte
|
|
902
|
+
// bound, and never holds the request up for long. A noted request is counted with its note, which bounds it without
|
|
903
|
+
// the note too (the note only adds).
|
|
904
|
+
// Only a bound that is a number is lowered: an unmeasured one (a fee with no bound, a hosted tool's reads) stays so.
|
|
905
|
+
let inputTokensCounted = false;
|
|
906
|
+
if (answering && !route.free && host === undefined && inputTokensMax !== undefined && policy.countInput) {
|
|
907
|
+
// A client that goes while its request is counted is gone before anything is admitted: the count is cut, and the
|
|
908
|
+
// request is neither admitted nor sent (nothing is booked, and no paid answer starts for no one).
|
|
909
|
+
const gone = new AbortController();
|
|
910
|
+
const onGone = () => gone.abort(new Error("the client went while its request was counted"));
|
|
911
|
+
res.once("close", onGone);
|
|
912
|
+
let counted;
|
|
913
|
+
try {
|
|
914
|
+
counted = await countedInputOf(policy, origin, body, headers, forward, say, COUNT_TIMEOUT_MS, gone.signal);
|
|
915
|
+
}
|
|
916
|
+
finally {
|
|
917
|
+
res.off("close", onGone);
|
|
918
|
+
}
|
|
919
|
+
if (gone.signal.aborted || res.destroyed)
|
|
920
|
+
return;
|
|
921
|
+
if (counted !== undefined && inputTokensMax !== undefined && counted < inputTokensMax) {
|
|
922
|
+
inputTokensMax = counted;
|
|
923
|
+
inputTokensCounted = true;
|
|
924
|
+
if (inputTokensMaxWithoutNote !== undefined)
|
|
925
|
+
inputTokensMaxWithoutNote = Math.min(inputTokensMaxWithoutNote, counted);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
859
928
|
// This request's own admission, handed back to its booking (requests may be in flight together).
|
|
860
929
|
let ticket;
|
|
861
930
|
try {
|
|
@@ -867,6 +936,8 @@ export async function startModelProxy(options, key) {
|
|
|
867
936
|
...(outputTokensAsked === undefined ? {} : { outputTokensAsked }),
|
|
868
937
|
...(outputTokensLeast === undefined ? {} : { outputTokensLeast }),
|
|
869
938
|
...(host === undefined ? {} : { host }),
|
|
939
|
+
...(inputTokensCounted ? { inputTokensCounted: true } : {}),
|
|
940
|
+
...(options.route === undefined ? {} : { route: options.route }),
|
|
870
941
|
});
|
|
871
942
|
}
|
|
872
943
|
catch (error) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { createHarnessRunner, modelPortFor, sessionSpendOf, SUBMIT_TOOL, RUNNER_RECORDS } from "./runner.js";
|
|
1
|
+
export { answeredUnpriced, createHarnessRunner, modelPortFor, sessionSpendOf, SUBMIT_TOOL, RUNNER_RECORDS } from "./runner.js";
|
|
2
2
|
export { runBooking, type RunSessions } from "./run-booking.js";
|
|
3
3
|
export type { Admission, BeforeRequest, Refusal, HarnessRunner, HarnessRunnerOptions, InFlight, ModelBooking, ModelPortEvent, ModelPortOptions, SessionCost } from "./runner.js";
|
|
4
4
|
export { DELEGATE_RESULT_SCHEMA, DELEGATE_TOOL, delegateProblems, delegateTool } from "./delegate.js";
|
|
@@ -8,7 +8,7 @@ export { toolSchemaProblems } from "./schema-check.js";
|
|
|
8
8
|
export { READ_SKILL_FILE, readSkillFileTool, SKILL_BYTES, skillProblems } from "./skill-files.js";
|
|
9
9
|
export type { HostSkill, SkillFileList } from "./skill-files.js";
|
|
10
10
|
export type { HarnessRunReason } from "./errors.js";
|
|
11
|
-
export { CONTENT_FILTERED, HOST_RESULT_MAX_CHARS } from "./types.js";
|
|
11
|
+
export { CONTENT_FILTERED, HOST_RESULT_MAX_CHARS, hostedTool } from "./types.js";
|
|
12
12
|
export { bookRequest, isConnectCode, isGatewayTimeout, rejectionCode } from "./booking.js";
|
|
13
13
|
export type { Booked, Ended, Priced } from "./booking.js";
|
|
14
14
|
export type { AdapterCapabilities, AdapterEvent, AdapterSession, AgentAdapter, AdapterFacts, GateAnswer, GateCall, SessionPort, Wire, HarnessEvent, HostTool, McpCallResult, McpClient, McpTool, ToolImage, JsonValue, ModelPort, ModelTicket, ModelRequest, ModelUsage, NoteRequest, NodeRequest, SessionIO, SessionMcp, SessionRecord, SessionSpec, SessionState, SessionStore, SessionWriter, TurnStatus, } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { createHarnessRunner, modelPortFor, sessionSpendOf, SUBMIT_TOOL, RUNNER_RECORDS } from "./runner.js";
|
|
1
|
+
export { answeredUnpriced, createHarnessRunner, modelPortFor, sessionSpendOf, SUBMIT_TOOL, RUNNER_RECORDS } from "./runner.js";
|
|
2
2
|
export { runBooking } from "./run-booking.js";
|
|
3
3
|
export { DELEGATE_RESULT_SCHEMA, DELEGATE_TOOL, delegateProblems, delegateTool } from "./delegate.js";
|
|
4
4
|
export { HarnessRunError, SessionFenced, TurnParked } from "./errors.js";
|
|
5
5
|
export { toolSchemaProblems } from "./schema-check.js";
|
|
6
6
|
export { READ_SKILL_FILE, readSkillFileTool, SKILL_BYTES, skillProblems } from "./skill-files.js";
|
|
7
|
-
export { CONTENT_FILTERED, HOST_RESULT_MAX_CHARS } from "./types.js";
|
|
7
|
+
export { CONTENT_FILTERED, HOST_RESULT_MAX_CHARS, hostedTool } from "./types.js";
|
|
8
8
|
export { bookRequest, isConnectCode, isGatewayTimeout, rejectionCode } from "./booking.js";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { CapRefused, createCap, DECISION_IMAGE_TOKENS, DECISION_OUTPUT_TOKENS, DecisionPriceUnreadable, END_MARK_WAIT_MS, END_NOT_WRITTEN, MARK_NOT_WRITTEN, openCap } from "./admit.js";
|
|
2
2
|
export { capRule, noteFor, type Judged, type JudgeState, type NoteState } from "./rule.js";
|
|
3
3
|
export { beforeRequestOf, type BeforeRequestOptions } from "./port.js";
|
|
4
|
-
export { DECISION_SESSION, paidCalls, runCap, TOOL_SESSION, type RunCap, type RunCapOptions, type RunDecisions, type RunTools } from "./run-cap.js";
|
|
4
|
+
export { DECISION_SESSION, overCapOf, paidCalls, runCap, TOOL_SESSION, type RunCap, type RunCapOptions, type RunDecisions, type RunTools } from "./run-cap.js";
|
|
5
5
|
export { ANSWER_TOKENS_LEAST, CACHE_WARM_MS, CAP_TOLERANCE, FIT_TOLERANCE, STOP_ALLOWANCE_TOKENS, THINKING_BUDGET_LEAST, expectedInput, inputBound, mostOutput, pricingOf, ratesFor, requestPrice } from "./price.js";
|
|
6
|
-
export { CAP_REFUSAL_CODE, SPEND_CAP_REACHED, SUGGESTED_RAISE_FACTOR, centsMoney, leastMoney, money, overCapWords, raiseFigure, raiseTo, suggestedRaise } from "./words.js";
|
|
6
|
+
export { CAP_REFUSAL_CODE, SPEND_CAP_REACHED, SUGGESTED_RAISE_FACTOR, centsMoney, leastMoney, money, OVER_CAP_END, overCapEnd, overCapMessage, overCapWords, paidRefusal, raiseFigure, raiseTo, suggestedRaise } from "./words.js";
|
|
7
7
|
export { checkRaise, followUpOf, parkOf, takeRaises, type BudgetPending, type Park, type RaiseCheck, type RaiseState } from "./park.js";
|
|
8
8
|
export { WRAP_UP_FACTOR, WRAP_UP_LEAST_FACTOR, WRAP_UP_STEP0_ANSWER_TOKENS, typicalCost, wrapUpDue, wrapUpWords } from "./wrap-up.js";
|
|
9
9
|
export { CapStop, createStepWaits, type StepWaits } from "./steps.js";
|
package/dist/kernel/cap/index.js
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
export { CapRefused, createCap, DECISION_IMAGE_TOKENS, DECISION_OUTPUT_TOKENS, DecisionPriceUnreadable, END_MARK_WAIT_MS, END_NOT_WRITTEN, MARK_NOT_WRITTEN, openCap } from "./admit.js";
|
|
4
4
|
export { capRule, noteFor } from "./rule.js";
|
|
5
5
|
export { beforeRequestOf } from "./port.js";
|
|
6
|
-
export { DECISION_SESSION, paidCalls, runCap, TOOL_SESSION } from "./run-cap.js";
|
|
6
|
+
export { DECISION_SESSION, overCapOf, paidCalls, runCap, TOOL_SESSION } from "./run-cap.js";
|
|
7
7
|
export { ANSWER_TOKENS_LEAST, CACHE_WARM_MS, CAP_TOLERANCE, FIT_TOLERANCE, STOP_ALLOWANCE_TOKENS, THINKING_BUDGET_LEAST, expectedInput, inputBound, mostOutput, pricingOf, ratesFor, requestPrice } from "./price.js";
|
|
8
|
-
export { CAP_REFUSAL_CODE, SPEND_CAP_REACHED, SUGGESTED_RAISE_FACTOR, centsMoney, leastMoney, money, overCapWords, raiseFigure, raiseTo, suggestedRaise } from "./words.js";
|
|
8
|
+
export { CAP_REFUSAL_CODE, SPEND_CAP_REACHED, SUGGESTED_RAISE_FACTOR, centsMoney, leastMoney, money, OVER_CAP_END, overCapEnd, overCapMessage, overCapWords, paidRefusal, raiseFigure, raiseTo, suggestedRaise } from "./words.js";
|
|
9
9
|
export { checkRaise, followUpOf, parkOf, takeRaises } from "./park.js";
|
|
10
10
|
export { WRAP_UP_FACTOR, WRAP_UP_LEAST_FACTOR, WRAP_UP_STEP0_ANSWER_TOKENS, typicalCost, wrapUpDue, wrapUpWords } from "./wrap-up.js";
|
|
11
11
|
export { CapStop, createStepWaits } from "./steps.js";
|
package/dist/kernel/cap/park.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CAP_TOLERANCE } from "./price.js";
|
|
2
|
-
import { CAP_REFUSAL_CODE, money, suggestedRaise } from "./words.js";
|
|
2
|
+
import { CAP_REFUSAL_CODE, money, SPEND_CAP_REACHED, suggestedRaise } from "./words.js";
|
|
3
3
|
const micro = (usd) => Math.round(usd * 1e6) / 1e6;
|
|
4
4
|
/** A bound to a millionth, rounded up (never under what it bounds), as main's overshoot bound is (run-view.ts microUp);
|
|
5
5
|
* no bound is 0, never -0 (the ceiling of a hair under 0 is -0, which a card would print as "-0"). */
|
|
@@ -9,7 +9,9 @@ export function parkOf(refusal, capUsd) {
|
|
|
9
9
|
if (refusal.forGood || !refusal.figures)
|
|
10
10
|
return { outcome: "failed", error: refusal.message };
|
|
11
11
|
const { spentUsd, needsUsd, leastCapUsd, boundUsd } = refusal.figures;
|
|
12
|
-
|
|
12
|
+
// Its words without the refusal's code: a model request's ("spend_cap_reached (out of budget): ") or a paid step's
|
|
13
|
+
// ("spend_cap_reached: "), so a pause reads its sentence alone, whichever step was refused.
|
|
14
|
+
const words = [CAP_REFUSAL_CODE, `${SPEND_CAP_REACHED}: `].reduce((said, code) => (said.startsWith(code) ? said.slice(code.length) : said), refusal.message);
|
|
13
15
|
return {
|
|
14
16
|
outcome: "parked",
|
|
15
17
|
pending: { kind: "budget", cap_usd: capUsd, spent_usd: micro(spentUsd), needs_usd: micro(needsUsd), least_cap_usd: leastCapUsd, suggested_cap_usd: suggestedRaise(capUsd, spentUsd, needsUsd), bound_usd: microUp(boundUsd ?? 0) },
|
|
@@ -3,8 +3,9 @@ import type { CapMode, CapModel, Previous } from "./types.js";
|
|
|
3
3
|
export interface BeforeRequestOptions {
|
|
4
4
|
/** The run's cap, read before every request. Undefined: no cap, nothing refused. */
|
|
5
5
|
capUsd(): number | undefined;
|
|
6
|
-
/** A model as the cap prices it, by its id
|
|
7
|
-
|
|
6
|
+
/** A model as the cap prices it, by its id and the route its request came by (ModelRequest.route; absent, the
|
|
7
|
+
* session's own): one id two routes price apart is priced as its route books it. Undefined: a model with no price here. */
|
|
8
|
+
modelOf(id: string, route?: string): CapModel | undefined;
|
|
8
9
|
/** The session's previous request on a model, as its usage reported it: none prices the input uncached. */
|
|
9
10
|
previousOf?(model: string): Previous | undefined;
|
|
10
11
|
/** What the run spent beside the session's model requests, which the runner's booking does not hold: its priced tools'
|
package/dist/kernel/cap/port.js
CHANGED
|
@@ -15,7 +15,12 @@ export function beforeRequestOf(options) {
|
|
|
15
15
|
const capUsd = options.capUsd();
|
|
16
16
|
if (capUsd === undefined)
|
|
17
17
|
return undefined;
|
|
18
|
-
|
|
18
|
+
// `owed`: money already owed at the provider: booked and held, never asked of the rule; only for charges incurred
|
|
19
|
+
// outside a call (a browser session's release, the time a person used). It counts in the run's spend and holds like
|
|
20
|
+
// any paid step, so every refusable step after it sees it. Only a figure is money: one that is not goes to the rule.
|
|
21
|
+
if (request.paid?.owed && isFigure(request.feeUsd ?? 0))
|
|
22
|
+
return { holdUsd: request.feeUsd ?? 0 };
|
|
23
|
+
const model = options.modelOf(request.model, request.route) ?? unpriced(request.model);
|
|
19
24
|
// A request the host sends beside the harness's own (its host label: a learned workflow's node) is another
|
|
20
25
|
// conversation's prompt: nothing of the session's cache is read for it, so it is priced as a cache miss.
|
|
21
26
|
const previous = request.host === undefined ? options.previousOf?.(request.model) : undefined;
|
|
@@ -34,7 +39,7 @@ export function beforeRequestOf(options) {
|
|
|
34
39
|
// neither (no hold, no price or no measure) cannot be kept within the cap: refused for good, as an unmeasured input.
|
|
35
40
|
let inFlight = 0;
|
|
36
41
|
for (const flight of request.inFlight) {
|
|
37
|
-
const figure = isFigure(flight.holdUsd) ? flight.holdUsd : worstOf(options.modelOf(flight.model), flight.inputTokensMax, flight.outputTokensMax);
|
|
42
|
+
const figure = isFigure(flight.holdUsd) ? flight.holdUsd : worstOf(options.modelOf(flight.model, flight.route), flight.inputTokensMax, flight.outputTokensMax);
|
|
38
43
|
if (figure === undefined)
|
|
39
44
|
return { refuse: unmeasured(capUsd), code: SPEND_CAP_REACHED };
|
|
40
45
|
inFlight += figure;
|
package/dist/kernel/cap/price.js
CHANGED
|
@@ -70,8 +70,11 @@ export function inputBound(request) {
|
|
|
70
70
|
const measured = request.inputTokens;
|
|
71
71
|
if (typeof measured !== "number" || !Number.isFinite(measured) || measured < 0)
|
|
72
72
|
return undefined;
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
// Below 0 only by images' data counted at their billed size instead; a shape that says more such bytes than were
|
|
74
|
+
// measured is no bound.
|
|
75
|
+
const added = request.shape ? providerInputOverhead(request.model.api, { model: request.model.id, ...request.shape }) : 0;
|
|
76
|
+
const bound = measured + added + (request.extraInputTokens ?? 0);
|
|
77
|
+
return bound < 0 ? undefined : bound;
|
|
75
78
|
}
|
|
76
79
|
/** The output a request is sent with at most: what it asks (its model's ceiling when it says none), and its thinking budget
|
|
77
80
|
* on an API whose thinking takes one, within the ceiling. Undefined: no ceiling and none asked. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type RunSessions } from "../../run-booking.js";
|
|
2
2
|
import { type BeforeRequest } from "../../runner.js";
|
|
3
|
-
import type { SessionWriter } from "../../types.js";
|
|
3
|
+
import type { SessionRecord, SessionWriter } from "../../types.js";
|
|
4
4
|
import type { Admission, CapMode, CapModel, DecisionCapRequest, Previous, Settled } from "./types.js";
|
|
5
5
|
/** The session a run's workflow decisions book under. */
|
|
6
6
|
export declare const DECISION_SESSION = "decisions";
|
|
@@ -11,8 +11,8 @@ export interface RunCapOptions {
|
|
|
11
11
|
capUsd(): number | undefined;
|
|
12
12
|
readonly mode?: CapMode;
|
|
13
13
|
readonly now?: () => number;
|
|
14
|
-
/** The run's model requests' models (its decisions' are the cap's own). */
|
|
15
|
-
modelOf(id: string): CapModel | undefined;
|
|
14
|
+
/** The run's model requests' models, by id and route (BeforeRequestOptions.modelOf; its decisions' are the cap's own). */
|
|
15
|
+
modelOf(id: string, route?: string): CapModel | undefined;
|
|
16
16
|
previousOf?(model: string): Previous | undefined;
|
|
17
17
|
/** The run's priced tools' charges and holds beside its requests (beforeRequestOf's), when it has any. */
|
|
18
18
|
spentUsd?(): number | null;
|
|
@@ -52,7 +52,10 @@ export interface RunCap {
|
|
|
52
52
|
export interface RunTools {
|
|
53
53
|
/** Admits the paid request `id` at its price (`holdUsd`): its hold, or CapRefused. `call`: the tool call it runs under,
|
|
54
54
|
* named on its runner.request (none for a step under no call, a person's browser time); `chargedBy`, who charges
|
|
55
|
-
* for it, named there as `charged_by
|
|
55
|
+
* for it, named there as `charged_by`; `tool` and `tag`, the tool whose charge it is and its session (ModelRequest.paid);
|
|
56
|
+
* `owed`, money its provider is already owed: held and booked, never refused. `hold`: a hold-only step: money
|
|
57
|
+
* reserved, not spent; given back on a lost claim, never booked as cut. `ref`: the one booking it names: a ref already
|
|
58
|
+
* booked (on the run's records, or on this claim) books nothing again, and the answer says `booked`. */
|
|
56
59
|
admit(request: {
|
|
57
60
|
readonly id: string;
|
|
58
61
|
readonly name: string;
|
|
@@ -60,7 +63,14 @@ export interface RunTools {
|
|
|
60
63
|
readonly basis: string;
|
|
61
64
|
readonly call?: string;
|
|
62
65
|
readonly chargedBy?: string;
|
|
63
|
-
|
|
66
|
+
readonly tool?: string;
|
|
67
|
+
readonly tag?: string;
|
|
68
|
+
readonly owed?: true;
|
|
69
|
+
readonly hold?: true;
|
|
70
|
+
readonly ref?: string;
|
|
71
|
+
}): Promise<Admission & {
|
|
72
|
+
readonly booked?: true;
|
|
73
|
+
}>;
|
|
64
74
|
/** Settles it at what it charged (null: at its hold; 0: nothing was billed). `givenBack`: its hold let go unused,
|
|
65
75
|
* nothing sent (at 0), its record marked so (ModelUsage.givenBack): never read as a $0 charge or an unpriced one. */
|
|
66
76
|
settle(id: string, usd: number | null, options?: {
|
|
@@ -71,6 +81,12 @@ export interface RunTools {
|
|
|
71
81
|
}
|
|
72
82
|
/** The session a run's paid tool calls book under. */
|
|
73
83
|
export declare const TOOL_SESSION = "paid-tools";
|
|
84
|
+
/** How a run ends once its booked spend passed its cap (overCapEnd), read from its model session's `records` and what
|
|
85
|
+
* its other sessions spent (`othersUsd`): each request at the figure the cap counts it at (sessionSpendOf's figures: one
|
|
86
|
+
* cut by a crash at its hold plus its bound), and the bound of the requests since the spend last fit, so a cache miss
|
|
87
|
+
* within that bound (a cut request's possible one included) pauses the run instead. Undefined while it fits, by such a
|
|
88
|
+
* miss, or with a spend unknown. */
|
|
89
|
+
export declare function overCapOf(records: readonly SessionRecord[], othersUsd: number, capUsd: number): string | undefined;
|
|
74
90
|
export declare function runCap(options: RunCapOptions): RunCap;
|
|
75
91
|
/** A run's paid tool calls booked on `writer` (its TOOL_SESSION) under `before`, the run's one rule (runCap's, or the
|
|
76
92
|
* adapter loop's runBooking over its spec's check): each held at its price before it is sent (main's paidCall, by the
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// spend of both from the journal: a decision held and never answered counts at its hold and its bound, as any request.
|
|
7
7
|
// Decisions book under their own session (DECISION_SESSION), priced as main holds them (decisionAsk).
|
|
8
8
|
import { runBooking } from "../../run-booking.js";
|
|
9
|
-
import { modelPortFor } from "../../runner.js";
|
|
9
|
+
import { modelPortFor, RUNNER_RECORDS, sessionSpendOf } from "../../runner.js";
|
|
10
|
+
import { overCapEnd } from "./words.js";
|
|
10
11
|
import { decisionAsk } from "./admit.js";
|
|
11
12
|
import { CapRefused, DecisionPriceUnreadable } from "./errors.js";
|
|
12
13
|
import { beforeRequestOf } from "./port.js";
|
|
@@ -14,6 +15,23 @@ import { beforeRequestOf } from "./port.js";
|
|
|
14
15
|
export const DECISION_SESSION = "decisions";
|
|
15
16
|
/** The session a run's paid tool calls book under. */
|
|
16
17
|
export const TOOL_SESSION = "paid-tools";
|
|
18
|
+
/** How a run ends once its booked spend passed its cap (overCapEnd), read from its model session's `records` and what
|
|
19
|
+
* its other sessions spent (`othersUsd`): each request at the figure the cap counts it at (sessionSpendOf's figures: one
|
|
20
|
+
* cut by a crash at its hold plus its bound), and the bound of the requests since the spend last fit, so a cache miss
|
|
21
|
+
* within that bound (a cut request's possible one included) pauses the run instead. Undefined while it fits, by such a
|
|
22
|
+
* miss, or with a spend unknown. */
|
|
23
|
+
export function overCapOf(records, othersUsd, capUsd) {
|
|
24
|
+
const read = sessionSpendOf(records);
|
|
25
|
+
if (read.spentUsd === null)
|
|
26
|
+
return undefined;
|
|
27
|
+
let running = othersUsd;
|
|
28
|
+
let bound = 0;
|
|
29
|
+
for (const { turn, usd } of read.figures) {
|
|
30
|
+
running += usd ?? 0;
|
|
31
|
+
bound = running <= capUsd ? 0 : bound + (read.bounds.get(turn) ?? 0);
|
|
32
|
+
}
|
|
33
|
+
return overCapEnd(othersUsd + read.spentUsd, capUsd, bound);
|
|
34
|
+
}
|
|
17
35
|
export function runCap(options) {
|
|
18
36
|
const mode = options.mode ?? "expected";
|
|
19
37
|
/** Each decision under way's model, as the rule prices it (decisionAsk), by the model key its request books under. */
|
|
@@ -22,7 +40,7 @@ export function runCap(options) {
|
|
|
22
40
|
capUsd: options.capUsd,
|
|
23
41
|
mode,
|
|
24
42
|
...(options.now ? { now: options.now } : {}),
|
|
25
|
-
modelOf: (id) => deciding.get(id) ?? options.modelOf(id),
|
|
43
|
+
modelOf: (id, route) => deciding.get(id) ?? options.modelOf(id, route),
|
|
26
44
|
...(options.previousOf ? { previousOf: (model) => (deciding.has(model) ? undefined : options.previousOf(model)) } : {}),
|
|
27
45
|
...(options.spentUsd ? { spentUsd: options.spentUsd } : {}),
|
|
28
46
|
...(options.heldUsd ? { heldUsd: options.heldUsd } : {}),
|
|
@@ -62,9 +80,29 @@ export function runCap(options) {
|
|
|
62
80
|
* rule's tool judgement), flushed first when `flush` is given, and settled at what it charged. */
|
|
63
81
|
export async function paidCalls(writer, before, options = {}) {
|
|
64
82
|
const steps = await stepsOn(writer, before, options.flush, "paid call", () => { });
|
|
83
|
+
// Each `ref` the run's paid steps have booked (on its records, or admitted on this claim): a ref names one booking, so a
|
|
84
|
+
// second admit of it books nothing (`booked`) and opens no step.
|
|
85
|
+
let refs;
|
|
86
|
+
const refsOf = () => (refs ??= writer.read().then((records) => new Set(records.filter((r) => r.kind === RUNNER_RECORDS.request).map((r) => r.data?.ref).filter((ref) => typeof ref === "string"))));
|
|
87
|
+
const book = (request) =>
|
|
88
|
+
// A hold that is no figure (an unpriced browser provider's) goes to the rule as given, which refuses it for good.
|
|
89
|
+
steps.book(request.id, { model: `paid:${request.name}`, inputTokensMax: 0, ...(request.holdUsd !== 0 ? { feeUsd: request.holdUsd } : {}), paid: { name: request.name, basis: request.basis, ...(request.call === undefined ? {} : { call: request.call }), ...(request.chargedBy === undefined ? {} : { charged_by: request.chargedBy }), ...(request.tool === undefined ? {} : { tool: request.tool }), ...(request.tag === undefined ? {} : { tag: request.tag }), ...(request.owed ? { owed: true } : {}), ...(request.hold ? { hold: true } : {}), ...(request.ref === undefined ? {} : { ref: request.ref }) } });
|
|
65
90
|
return {
|
|
66
|
-
|
|
67
|
-
|
|
91
|
+
admit: async (request) => {
|
|
92
|
+
if (request.ref === undefined)
|
|
93
|
+
return book(request);
|
|
94
|
+
const booked = await refsOf();
|
|
95
|
+
if (booked.has(request.ref))
|
|
96
|
+
return { id: request.id, heldUsd: 0, boundUsd: 0, worstUsd: 0, stoppedUsd: () => 0, booked: true };
|
|
97
|
+
booked.add(request.ref);
|
|
98
|
+
try {
|
|
99
|
+
return await book(request);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
booked.delete(request.ref);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
},
|
|
68
106
|
settle: (id, usd, options) => steps.settle(id, options?.givenBack ? 0 : usd, options),
|
|
69
107
|
spentUsd: steps.spentUsd,
|
|
70
108
|
heldUsd: steps.heldUsd,
|
|
@@ -111,6 +111,13 @@ export interface RefusalFigures {
|
|
|
111
111
|
readonly needsUsd: number;
|
|
112
112
|
readonly leastCapUsd: number;
|
|
113
113
|
readonly boundUsd?: number;
|
|
114
|
+
/** A paid call's refusal: the call (its name, hold and basis), so a pause that lands after the refusal can say it again
|
|
115
|
+
* with the run's spend as it is then (paidRefusal). */
|
|
116
|
+
readonly step?: {
|
|
117
|
+
readonly name: string;
|
|
118
|
+
readonly holdUsd: number;
|
|
119
|
+
readonly basis: string;
|
|
120
|
+
};
|
|
114
121
|
}
|
|
115
122
|
/** How a step ended, for settle: a model request's as booking.ts reads it; a paid call's charge (null: it did not say). */
|
|
116
123
|
export type Settled = Ended | {
|
|
@@ -31,6 +31,15 @@ export declare function paidRefusal(cap: number, spent: number, held: number, na
|
|
|
31
31
|
* by no more than `boundUsd` (the bound of the requests since the cost last fit), a cache miss did it, and the most it
|
|
32
32
|
* could have. None at or under the cap, with no cap, or with no known cost. */
|
|
33
33
|
export declare function overCapWords(spentUsd: number | null, capUsd: number | undefined, boundUsd?: number): string | undefined;
|
|
34
|
+
/** How a run ends whose booked cost `spentUsd` passed `capUsd`: failed, in these words (main's run end), the answer that
|
|
35
|
+
* crossed it booked. None at or under the cap, with no cap, with no known cost, or when it passed by no more than
|
|
36
|
+
* `boundUsd` (a cache miss within the bound it was admitted at: the run pauses at its next request instead). */
|
|
37
|
+
export declare function overCapEnd(spentUsd: number | null, capUsd: number | undefined, boundUsd?: number): string | undefined;
|
|
38
|
+
/** How overCapEnd's words begin, and only theirs: a reader of a run's summary serves one that does as the error
|
|
39
|
+
* spend_cap_reached, its message the words after `spend_cap_reached: ` (overCapMessage). */
|
|
40
|
+
export declare const OVER_CAP_END = "spend_cap_reached: The run cost ";
|
|
41
|
+
/** The message of a run's end in overCapEnd's words: what follows its code; undefined for any other summary. */
|
|
42
|
+
export declare const overCapMessage: (summary: string | null | undefined) => string | undefined;
|
|
34
43
|
export declare const unmeasured: (cap: number) => string;
|
|
35
44
|
export declare const noPrice: (cap: number) => string;
|
|
36
45
|
export declare const noCeiling: (cap: number) => string;
|
package/dist/kernel/cap/words.js
CHANGED
|
@@ -53,7 +53,7 @@ export function paidRefusal(cap, spent, held, name, holdUsd, basis) {
|
|
|
53
53
|
const leastCapUsd = raiseFigure(cap, spent + held + holdUsd);
|
|
54
54
|
return {
|
|
55
55
|
text: `${SPEND_CAP_REACHED}: Not sent: this run has ${spentOf(spent, held, cap)}, and the next step, ${name}, could cost up to ${money(holdUsd)} (${basis}). Raise the cap to at least $${leastCapUsd.toFixed(2)} to continue.`,
|
|
56
|
-
figures: { spentUsd: spent + held, needsUsd: holdUsd, leastCapUsd },
|
|
56
|
+
figures: { spentUsd: spent + held, needsUsd: holdUsd, leastCapUsd, step: { name, holdUsd, basis } },
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
/** An amount rounded up to the cent, so "at most" holds. */
|
|
@@ -67,6 +67,21 @@ export function overCapWords(spentUsd, capUsd, boundUsd = 0) {
|
|
|
67
67
|
const byMiss = boundUsd > 0 && spentUsd - capUsd <= boundUsd + FIT_TOLERANCE;
|
|
68
68
|
return byMiss ? `Went ${centsUp(spentUsd - capUsd)} over its cap on a cache miss (at most ${centsUp(boundUsd)}).` : `Spent ${centsMoney(spentUsd)}, ${centsUp(spentUsd - capUsd)} over the run's cap of ${centsMoney(capUsd)}`;
|
|
69
69
|
}
|
|
70
|
+
/** How a run ends whose booked cost `spentUsd` passed `capUsd`: failed, in these words (main's run end), the answer that
|
|
71
|
+
* crossed it booked. None at or under the cap, with no cap, with no known cost, or when it passed by no more than
|
|
72
|
+
* `boundUsd` (a cache miss within the bound it was admitted at: the run pauses at its next request instead). */
|
|
73
|
+
export function overCapEnd(spentUsd, capUsd, boundUsd = 0) {
|
|
74
|
+
if (capUsd === undefined || typeof spentUsd !== "number" || spentUsd <= capUsd + FIT_TOLERANCE)
|
|
75
|
+
return undefined;
|
|
76
|
+
if (boundUsd > 0 && spentUsd - capUsd <= boundUsd + FIT_TOLERANCE)
|
|
77
|
+
return undefined;
|
|
78
|
+
return `${OVER_CAP_END}${centsMoney(spentUsd)}, ${centsUp(spentUsd - capUsd)} over its cap of ${centsMoney(capUsd)}.`;
|
|
79
|
+
}
|
|
80
|
+
/** How overCapEnd's words begin, and only theirs: a reader of a run's summary serves one that does as the error
|
|
81
|
+
* spend_cap_reached, its message the words after `spend_cap_reached: ` (overCapMessage). */
|
|
82
|
+
export const OVER_CAP_END = `${SPEND_CAP_REACHED}: The run cost `;
|
|
83
|
+
/** The message of a run's end in overCapEnd's words: what follows its code; undefined for any other summary. */
|
|
84
|
+
export const overCapMessage = (summary) => typeof summary === "string" && summary.startsWith(OVER_CAP_END) ? summary.slice(`${SPEND_CAP_REACHED}: `.length) : undefined;
|
|
70
85
|
export const unmeasured = (cap) => `${SPEND_CAP_REACHED}: Not sent: the size of the next model request's input is unknown, so the run's cap of ${money(cap)} cannot be kept: the run is out of budget.`;
|
|
71
86
|
export const noPrice = (cap) => `${SPEND_CAP_REACHED}: Not sent: the next model request's model has no price here, so the run's cap of ${money(cap)} cannot be kept: the run is out of budget.`;
|
|
72
87
|
export const noCeiling = (cap) => `${SPEND_CAP_REACHED}: Not sent: the next model request's model has no output ceiling here and its output is priced, so what a request may cost has no bound and the run's cap of ${money(cap)} cannot be kept: the run is out of budget.`;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { type BrowserContract } from "./browser.js";
|
|
2
1
|
import type { CapabilityKind } from "./hands.js";
|
|
3
2
|
/** Where the run's shell is: on a computer (bash and the background tools run there), the run's folder's own exec with no
|
|
4
3
|
* computer (bash only, asked first: a host gives it only where that exec is not its own machine, as serve's Archil
|
|
@@ -35,8 +34,6 @@ export declare function hostToolDefs(options: {
|
|
|
35
34
|
}): readonly ToolDef[];
|
|
36
35
|
/** The kinds, in the order their tools follow the host tools. */
|
|
37
36
|
export declare const CAPABILITY_KINDS: readonly CapabilityKind[];
|
|
38
|
-
/** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts)
|
|
39
|
-
* phone's (phone.ts)
|
|
40
|
-
export declare function capabilityToolDefs(kind: CapabilityKind
|
|
41
|
-
readonly browserTools?: readonly BrowserContract[];
|
|
42
|
-
}): readonly ToolDef[];
|
|
37
|
+
/** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts) or the
|
|
38
|
+
* phone's (phone.ts). */
|
|
39
|
+
export declare function capabilityToolDefs(kind: CapabilityKind): readonly ToolDef[];
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
// edit and bash are pi's (pi 1.5.0's coding tools, MIT), with serve's additions: bash's timeout words and read's image
|
|
3
3
|
// sentence on a computer. The five background tools are serve's (background.ts). A test holds every one of them, in
|
|
4
4
|
// order, to serve's own on main. A computer of a kind past files and a shell adds its own tools after them
|
|
5
|
-
// (capabilityToolDefs): the desktop's, the phone's
|
|
6
|
-
import { browserToolDefs } from "./browser.js";
|
|
5
|
+
// (capabilityToolDefs): the desktop's, the phone's.
|
|
7
6
|
import { DESKTOP_TOOL_DEFS } from "./desktop.js";
|
|
8
7
|
import { PHONE_TOOL_DEFS } from "./phone.js";
|
|
9
8
|
import { BACKGROUND_INPUT_BYTES, BACKGROUND_LIST_MAX, BACKGROUND_LOG_BYTES, BACKGROUND_MAX, BACKGROUND_PAGE_BYTES, BACKGROUND_SIGNALS, BASH_DEFAULT_TIMEOUT_S } from "./limits.js";
|
|
@@ -141,13 +140,9 @@ export function hostToolDefs(options) {
|
|
|
141
140
|
return defs;
|
|
142
141
|
}
|
|
143
142
|
/** The kinds, in the order their tools follow the host tools. */
|
|
144
|
-
export const CAPABILITY_KINDS = Object.freeze(["desktop", "phone"
|
|
145
|
-
/** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts)
|
|
146
|
-
* phone's (phone.ts)
|
|
147
|
-
export function capabilityToolDefs(kind
|
|
148
|
-
|
|
149
|
-
return DESKTOP_TOOL_DEFS;
|
|
150
|
-
if (kind === "phone")
|
|
151
|
-
return PHONE_TOOL_DEFS;
|
|
152
|
-
return browserToolDefs(options.browserTools);
|
|
143
|
+
export const CAPABILITY_KINDS = Object.freeze(["desktop", "phone"]);
|
|
144
|
+
/** A computer kind's own tools, which a run of that kind sees after its host tools: the desktop's (desktop.ts) or the
|
|
145
|
+
* phone's (phone.ts). */
|
|
146
|
+
export function capabilityToolDefs(kind) {
|
|
147
|
+
return kind === "desktop" ? DESKTOP_TOOL_DEFS : PHONE_TOOL_DEFS;
|
|
153
148
|
}
|
|
@@ -61,7 +61,7 @@ export type Processes = {
|
|
|
61
61
|
list(): Promise<readonly ProcessInfo[]>;
|
|
62
62
|
};
|
|
63
63
|
/** A computer's kind past files and a shell: each adds its own tools to the run's (defs.ts capabilityToolDefs). */
|
|
64
|
-
export type CapabilityKind = "desktop" | "phone"
|
|
64
|
+
export type CapabilityKind = "desktop" | "phone";
|
|
65
65
|
/** A kind's tool refused in a run whose computer is not of that kind (or lacks the port): the words name the setting. */
|
|
66
66
|
export declare const needsKindWords: (tool: string, kind: CapabilityKind) => string;
|
|
67
67
|
/** A screenshot as a tool's answer: the words, and the PNG in the answer's images. */
|
|
@@ -173,20 +173,6 @@ export type Phone = {
|
|
|
173
173
|
open(id: string): Promise<void>;
|
|
174
174
|
};
|
|
175
175
|
};
|
|
176
|
-
/** A connection to a browser's DevTools protocol (pi-browser's CdpLink, in shape). */
|
|
177
|
-
export type CdpSession = {
|
|
178
|
-
send(method: string, params?: object, sessionId?: string): Promise<unknown>;
|
|
179
|
-
on(listener: (event: {
|
|
180
|
-
method?: string;
|
|
181
|
-
params?: unknown;
|
|
182
|
-
sessionId?: string;
|
|
183
|
-
}) => void): () => void;
|
|
184
|
-
close(): void;
|
|
185
|
-
};
|
|
186
|
-
/** A browser computer: a connection to its CDP. The browser's tools and take-over are the host's (pi-browser's). */
|
|
187
|
-
export type Browser = {
|
|
188
|
-
connect(): Promise<CdpSession>;
|
|
189
|
-
};
|
|
190
176
|
/** The agent's hand-over of a desktop or a phone to the person, through the run's hold: ask opens it (what the person is
|
|
191
177
|
* to do, how both will know it is done, when it expires, ISO time) and resolves with its first answer, or expired. An
|
|
192
178
|
* abort of `signal` (a stop, a lost claim) rejects it. */
|
|
@@ -241,8 +227,6 @@ export type Hands = {
|
|
|
241
227
|
readonly input?: Input;
|
|
242
228
|
/** A phone's screen, touch, typing and apps; absent where the computer is no phone. */
|
|
243
229
|
readonly phone?: Phone;
|
|
244
|
-
/** A browser's CDP; absent where the computer is no browser. */
|
|
245
|
-
readonly browser?: Browser;
|
|
246
230
|
/** A desktop's or a phone's hand-over to the person (the run's hold); absent where there is none. */
|
|
247
231
|
readonly handoff?: Handoff;
|
|
248
232
|
};
|
|
@@ -273,8 +257,5 @@ export type ToolCall = {
|
|
|
273
257
|
export declare function answerOf(call: ToolCall, run: () => Promise<ToolAnswer>): Promise<ToolAnswer>;
|
|
274
258
|
/** A host tool's run. */
|
|
275
259
|
export type Executor = (args: unknown, call: ToolCall) => Promise<ToolAnswer>;
|
|
276
|
-
/** What the host gives the registry, by tool name: read, write and edit, and bash's own run, which the registry bounds
|
|
277
|
-
|
|
278
|
-
export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash", Executor>>> & {
|
|
279
|
-
readonly browser?: Readonly<Record<string, Executor>>;
|
|
280
|
-
};
|
|
260
|
+
/** What the host gives the registry, by tool name: read, write and edit, and bash's own run, which the registry bounds. */
|
|
261
|
+
export type HostExecutors = Readonly<Partial<Record<"read" | "write" | "edit" | "bash", Executor>>>;
|