@noetic-tools/core 5.17.0 → 5.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/dist/adapters/openrouter.d.ts +7 -0
- package/dist/adapters/openrouter.js +23 -0
- package/dist/conditions/conditions.js +3 -0
- package/dist/harness/agent-harness.d.ts +63 -9
- package/dist/harness/agent-harness.js +222 -22
- package/dist/harness/call-spend.d.ts +34 -0
- package/dist/harness/call-spend.js +125 -0
- package/dist/harness/decide-call.d.ts +3 -0
- package/dist/harness/decide-call.js +51 -12
- package/dist/harness/deps/adapters.d.ts +1 -1
- package/dist/harness/deps/adapters.js +1 -1
- package/dist/harness/deps/runtime.d.ts +3 -1
- package/dist/harness/deps/runtime.js +3 -1
- package/dist/harness/deps/types.d.ts +1 -1
- package/dist/harness/model-call.d.ts +3 -0
- package/dist/harness/model-call.js +123 -66
- package/dist/harness/model-price.d.ts +51 -0
- package/dist/harness/model-price.js +158 -0
- package/dist/harness/model-rounds.d.ts +13 -0
- package/dist/harness/model-rounds.js +42 -2
- package/dist/harness/spend.d.ts +144 -0
- package/dist/harness/spend.js +534 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.js +2 -0
- package/dist/interpreter/execute-action.js +2 -0
- package/dist/interpreter/execute-control.js +1 -0
- package/dist/interpreter/execute-decide.js +43 -1
- package/dist/interpreter/execute.js +9 -0
- package/dist/runtime/context-impl.d.ts +1 -0
- package/dist/runtime/context-impl.js +1 -0
- package/dist/runtime/drain.d.ts +68 -0
- package/dist/runtime/drain.js +280 -0
- package/dist/runtime/durable/spend-log.d.ts +58 -0
- package/dist/runtime/durable/spend-log.js +133 -0
- package/dist/runtime/durable/step-ledger.d.ts +9 -2
- package/dist/runtime/durable/step-ledger.js +9 -6
- package/dist/runtime/session-runner.d.ts +5 -3
- package/dist/runtime/session-runner.js +9 -4
- package/dist/until/predicates.d.ts +5 -1
- package/dist/until/predicates.js +12 -7
- package/dist/util/spent.d.ts +14 -0
- package/dist/util/spent.js +67 -0
- package/dist/util/status-code.d.ts +5 -0
- package/dist/util/status-code.js +8 -0
- package/package.json +2 -2
|
@@ -13,6 +13,11 @@
|
|
|
13
13
|
* one it does not find; every round after that runs live, because a recorded round
|
|
14
14
|
* belongs to the conversation the earlier recorded rounds built. When the step
|
|
15
15
|
* finishes and records, the ledger prunes its rounds.
|
|
16
|
+
*
|
|
17
|
+
* A call that fails charges its context with the rounds it recorded, so a failed run
|
|
18
|
+
* reports what it paid. A retry in place replays those rounds on the same context,
|
|
19
|
+
* so the ledger remembers which rounds were charged and the replay counts them no
|
|
20
|
+
* more.
|
|
16
21
|
*/
|
|
17
22
|
import { frameworkCast } from '@noetic-tools/types';
|
|
18
23
|
import { z } from 'zod';
|
|
@@ -30,6 +35,12 @@ const RoundOutputSchema = z.object({
|
|
|
30
35
|
}),
|
|
31
36
|
cost: z.number(),
|
|
32
37
|
});
|
|
38
|
+
/**
|
|
39
|
+
* Keys of recorded rounds a failed call charged, per execution ledger. Kept in memory
|
|
40
|
+
* only: a new harness resuming the execution starts from a restored context, which
|
|
41
|
+
* never saw that charge.
|
|
42
|
+
*/
|
|
43
|
+
const chargedRounds = new WeakMap();
|
|
33
44
|
//#endregion
|
|
34
45
|
//#region Public API
|
|
35
46
|
/**
|
|
@@ -40,6 +51,8 @@ export class RoundLedger {
|
|
|
40
51
|
ctx;
|
|
41
52
|
dispatch;
|
|
42
53
|
replaying = true;
|
|
54
|
+
/** Rounds this loop recorded. */
|
|
55
|
+
recorded = [];
|
|
43
56
|
constructor(ctx, dispatch) {
|
|
44
57
|
this.ctx = ctx;
|
|
45
58
|
this.dispatch = dispatch;
|
|
@@ -79,6 +92,7 @@ export class RoundLedger {
|
|
|
79
92
|
usage: parsed.data.usage,
|
|
80
93
|
cost: parsed.data.cost,
|
|
81
94
|
appended: schemas.parseMany(entry.items ?? []),
|
|
95
|
+
charged: chargedRounds.get(ledger)?.has(this.key(round)) ?? false,
|
|
82
96
|
};
|
|
83
97
|
}
|
|
84
98
|
catch {
|
|
@@ -93,8 +107,12 @@ export class RoundLedger {
|
|
|
93
107
|
/** Record a finished round. `logMark` is what `mark()` returned when it began. */
|
|
94
108
|
async record(round, logMark, record) {
|
|
95
109
|
this.replaying = false;
|
|
110
|
+
const ledger = this.ctx.ledger;
|
|
111
|
+
if (!ledger) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
96
114
|
const appended = this.ctx.appendedItems().slice(logMark);
|
|
97
|
-
await
|
|
115
|
+
const stored = await ledger.record({
|
|
98
116
|
path: this.key(round),
|
|
99
117
|
stepId: this.dispatch.stepId,
|
|
100
118
|
kind: ROUND_KIND,
|
|
@@ -107,9 +125,31 @@ export class RoundLedger {
|
|
|
107
125
|
: {}),
|
|
108
126
|
completedAt: new Date().toISOString(),
|
|
109
127
|
});
|
|
128
|
+
if (!stored) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
this.recorded.push(round);
|
|
132
|
+
// A new record holds a bill no context has been charged with yet.
|
|
133
|
+
chargedRounds.get(ledger)?.delete(this.key(round));
|
|
134
|
+
}
|
|
135
|
+
/** Note that the rounds this loop recorded were charged to its context. */
|
|
136
|
+
charged() {
|
|
137
|
+
const ledger = this.ctx.ledger;
|
|
138
|
+
if (!ledger || this.recorded.length === 0) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const keys = chargedRounds.get(ledger) ?? new Set();
|
|
142
|
+
for (const round of this.recorded) {
|
|
143
|
+
keys.add(this.key(round));
|
|
144
|
+
}
|
|
145
|
+
chargedRounds.set(ledger, keys);
|
|
110
146
|
}
|
|
111
147
|
key(round) {
|
|
112
|
-
return
|
|
148
|
+
return roundPath(this.dispatch.path, round);
|
|
113
149
|
}
|
|
114
150
|
}
|
|
151
|
+
/** @internal The ledger key of round `round` of the model loop at step path `path`. */
|
|
152
|
+
export function roundPath(path, round) {
|
|
153
|
+
return `${path}/round#${round}`;
|
|
154
|
+
}
|
|
115
155
|
//#endregion
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paid dispatches: a record before every paid provider call, and the bill settled
|
|
3
|
+
* afterwards even when the call was cut off (spec 23 § Paid dispatches).
|
|
4
|
+
*
|
|
5
|
+
* A call's record is written before it is sent, gains the provider's generation id
|
|
6
|
+
* when its response headers arrive, and is deleted when its bill reaches the run.
|
|
7
|
+
* A call that ends any other way — cancelled, steered, timed out, its host killed —
|
|
8
|
+
* leaves the record behind. It is then *pending*: held at its ceiling, and priced
|
|
9
|
+
* through the provider's generation lookup once the provider has billed it.
|
|
10
|
+
*/
|
|
11
|
+
import type { Context, PendingSpend, ReconcileSpendOptions, SpendSettlement, UsageCharge, WriteOffSpendOptions } from '@noetic-tools/types';
|
|
12
|
+
import { SpendKind } from '@noetic-tools/types';
|
|
13
|
+
import type { OpenRouter } from '@openrouter/sdk';
|
|
14
|
+
import type { DispatchRecord, SpendLog } from '../runtime/durable/spend-log.js';
|
|
15
|
+
import type { EventBroadcaster } from './deps/runtime.js';
|
|
16
|
+
import { ContextImpl } from './deps/runtime.js';
|
|
17
|
+
/** @internal Request header that ties a response back to its dispatch. */
|
|
18
|
+
export declare const DISPATCH_HEADER = "x-noetic-dispatch";
|
|
19
|
+
/** @internal Response header carrying OpenRouter's id for the call. */
|
|
20
|
+
export declare const GENERATION_HEADER = "x-generation-id";
|
|
21
|
+
/** @internal The slice of the SDK client spend needs: prices and generation lookups. */
|
|
22
|
+
export type SpendClient = Pick<OpenRouter, 'models' | 'generations'>;
|
|
23
|
+
/** @internal What the harness gives `Spend`. */
|
|
24
|
+
export interface SpendHost {
|
|
25
|
+
readonly agentName: string;
|
|
26
|
+
readonly log: SpendLog;
|
|
27
|
+
client(): SpendClient | undefined;
|
|
28
|
+
/** The root context with a run in flight under `executionId`, if any. */
|
|
29
|
+
liveRoot(executionId: string): Context | undefined;
|
|
30
|
+
/** Add `charge` to the thread's stored session. False when the thread has none. */
|
|
31
|
+
chargeSession(threadId: string, charge: UsageCharge): Promise<boolean>;
|
|
32
|
+
sessionBroadcaster(threadId: string): EventBroadcaster | undefined;
|
|
33
|
+
}
|
|
34
|
+
/** @internal One paid call about to be sent. */
|
|
35
|
+
export interface DispatchParams {
|
|
36
|
+
ctx?: Context;
|
|
37
|
+
kind: SpendKind;
|
|
38
|
+
model: string;
|
|
39
|
+
/** The request body as sent, tools included: hashed, and sized to bound its prompt. */
|
|
40
|
+
body: unknown;
|
|
41
|
+
maxOutputTokens?: number;
|
|
42
|
+
/** The model-loop round, for a `callModel` round's ledger key. */
|
|
43
|
+
round?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* @internal
|
|
47
|
+
* One harness's paid dispatches: the live ones it is sending and the pending ones it
|
|
48
|
+
* holds. Pending records come from storage, so any harness over the same storage can
|
|
49
|
+
* settle a call another harness made.
|
|
50
|
+
*/
|
|
51
|
+
export declare class Spend {
|
|
52
|
+
private readonly host;
|
|
53
|
+
private readonly live;
|
|
54
|
+
private readonly holds;
|
|
55
|
+
/**
|
|
56
|
+
* Generation lookups in flight, so two callers never settle one dispatch twice and
|
|
57
|
+
* a reconcile waits for the ones already running.
|
|
58
|
+
*/
|
|
59
|
+
private readonly lookups;
|
|
60
|
+
private readonly price;
|
|
61
|
+
constructor(host: SpendHost);
|
|
62
|
+
/** Write the record for a call about to be sent. A failed write never blocks the call. */
|
|
63
|
+
open(params: DispatchParams): Promise<Dispatch>;
|
|
64
|
+
/** Save a live dispatch's generation id. The client's response hook awaits it. */
|
|
65
|
+
noteGeneration(dispatchId: string, generationId: string): Promise<void>;
|
|
66
|
+
/** Unsettled dispatches, each with its ceiling. Unreadable ones have none. */
|
|
67
|
+
pending(executionId?: string): Promise<PendingSpend[]>;
|
|
68
|
+
/**
|
|
69
|
+
* Hold the restored root context's pending dispatches on it, then reconcile them in
|
|
70
|
+
* the background. `ctx.heldCost` is set when this resolves.
|
|
71
|
+
*/
|
|
72
|
+
adopt(ctx: Context): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Price every pending dispatch that has a generation id, polling until the provider
|
|
75
|
+
* has, and wait for lookups already running for the same dispatches.
|
|
76
|
+
*/
|
|
77
|
+
reconcile(opts?: ReconcileSpendOptions): Promise<SpendSettlement[]>;
|
|
78
|
+
/** Settle one pending dispatch at an amount the host asserts. */
|
|
79
|
+
writeOff(id: string, opts: WriteOffSpendOptions): Promise<SpendSettlement | undefined>;
|
|
80
|
+
/** USD held for a thread's pending dispatches. */
|
|
81
|
+
heldFor(threadId: string): number;
|
|
82
|
+
/** A live dispatch ended. Called by `Dispatch` only. */
|
|
83
|
+
release(id: string): void;
|
|
84
|
+
/** A dispatch ended without a bill: hold it and start looking for one. Called by `Dispatch` only. */
|
|
85
|
+
leave(record: DispatchRecord, root: ContextImpl | undefined): void;
|
|
86
|
+
/**
|
|
87
|
+
* Stored dispatches not in flight here, all or one execution's. An unreadable record
|
|
88
|
+
* that names no execution may be any execution's, so it is listed with each of them.
|
|
89
|
+
*/
|
|
90
|
+
private unsettled;
|
|
91
|
+
/**
|
|
92
|
+
* Hold `record` at its ceiling, on `ctx` when given. A dispatch already held moves to
|
|
93
|
+
* `ctx`, so an execution restored in this process takes over its cut-off calls.
|
|
94
|
+
*/
|
|
95
|
+
private hold;
|
|
96
|
+
private ceilingOf;
|
|
97
|
+
/** Unreadable records carry no generation id, so they are never priced. */
|
|
98
|
+
private lookup;
|
|
99
|
+
/**
|
|
100
|
+
* Ask the provider for the call's bill until it has one. The provider prices a call
|
|
101
|
+
* some seconds after it ends — 7 to 32 s observed — so a lookup that gave up after a
|
|
102
|
+
* few seconds would leave ordinary calls unknown.
|
|
103
|
+
*/
|
|
104
|
+
private poll;
|
|
105
|
+
/**
|
|
106
|
+
* Settle a held dispatch: drop its record, release its hold, and charge its bill
|
|
107
|
+
* once. The record goes first, so a crash part-way loses a settlement rather than
|
|
108
|
+
* counting one twice. A record already gone was settled by another harness over the
|
|
109
|
+
* same storage, which charged it; one that could not be deleted stays held, to be
|
|
110
|
+
* settled by a later reconcile.
|
|
111
|
+
*/
|
|
112
|
+
private settle;
|
|
113
|
+
/**
|
|
114
|
+
* Charge a bill to one place: the run in flight on its execution, else its thread's
|
|
115
|
+
* session, else the context this harness held it on. A run in flight passes the
|
|
116
|
+
* charge on to its turn's usage, so charging the session too would count it twice.
|
|
117
|
+
*/
|
|
118
|
+
private charge;
|
|
119
|
+
/** Delete a held dispatch's record, unless it is already gone. */
|
|
120
|
+
private take;
|
|
121
|
+
private emit;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* @internal
|
|
125
|
+
* One paid call in flight. Record writes run one at a time, so a generation id saved
|
|
126
|
+
* late never brings back a record that was already deleted.
|
|
127
|
+
*/
|
|
128
|
+
export declare class Dispatch {
|
|
129
|
+
private readonly spend;
|
|
130
|
+
private readonly log;
|
|
131
|
+
readonly record: DispatchRecord;
|
|
132
|
+
private readonly root;
|
|
133
|
+
private writes;
|
|
134
|
+
private ended;
|
|
135
|
+
constructor(spend: Spend, log: SpendLog, record: DispatchRecord, root: ContextImpl | undefined);
|
|
136
|
+
/** Request headers that tie the response back to this dispatch. */
|
|
137
|
+
get headers(): Record<string, string>;
|
|
138
|
+
noteGeneration(generationId: string): Promise<void>;
|
|
139
|
+
/** The call's bill reached the run, or the provider refused it: drop the record. */
|
|
140
|
+
settle(): Promise<void>;
|
|
141
|
+
/** The call is over. Without a settled bill its record stays, pending. */
|
|
142
|
+
end(): void;
|
|
143
|
+
private queue;
|
|
144
|
+
}
|