@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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What one model call's rounds used and cost. Every round the provider answered
|
|
3
|
+
* counts, including one the loop rejected and asked again: the provider billed it.
|
|
4
|
+
* A rejected round's bill is held and folded into the round the loop goes on from,
|
|
5
|
+
* so `rounds` lists the rounds the conversation kept, and `rounds[0]` still reports
|
|
6
|
+
* on the first view the model answered.
|
|
7
|
+
*/
|
|
8
|
+
export class CallSpend {
|
|
9
|
+
rounds = [];
|
|
10
|
+
/** Bills counted since the last round the loop went on from. */
|
|
11
|
+
held;
|
|
12
|
+
/** Count a round the provider answered, whether or not the loop keeps it. */
|
|
13
|
+
count(usage, cost) {
|
|
14
|
+
this.held = addBills(this.held, {
|
|
15
|
+
usage,
|
|
16
|
+
cost,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The loop goes on from the round just counted, or from a steered round that has
|
|
21
|
+
* no bill of its own. Returns its bill, held rejected rounds included, for the
|
|
22
|
+
* round ledger to record.
|
|
23
|
+
*/
|
|
24
|
+
keep() {
|
|
25
|
+
const bill = this.held ?? {
|
|
26
|
+
usage: {
|
|
27
|
+
inputTokens: 0,
|
|
28
|
+
outputTokens: 0,
|
|
29
|
+
},
|
|
30
|
+
cost: 0,
|
|
31
|
+
};
|
|
32
|
+
if (this.held) {
|
|
33
|
+
this.rounds.push({
|
|
34
|
+
...this.held,
|
|
35
|
+
replayed: false,
|
|
36
|
+
});
|
|
37
|
+
this.held = undefined;
|
|
38
|
+
}
|
|
39
|
+
return bill;
|
|
40
|
+
}
|
|
41
|
+
/** Count a round replayed from the round ledger. */
|
|
42
|
+
replay({ usage, cost }) {
|
|
43
|
+
this.rounds.push({
|
|
44
|
+
usage,
|
|
45
|
+
cost,
|
|
46
|
+
replayed: true,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
totals() {
|
|
50
|
+
const bills = this.held
|
|
51
|
+
? [
|
|
52
|
+
...this.rounds,
|
|
53
|
+
this.held,
|
|
54
|
+
]
|
|
55
|
+
: this.rounds;
|
|
56
|
+
const usage = sumUsage(bills.map((bill) => bill.usage));
|
|
57
|
+
const cost = sum(bills.map((bill) => bill.cost));
|
|
58
|
+
const replayed = this.rounds.filter((round) => round.replayed);
|
|
59
|
+
return {
|
|
60
|
+
usage: {
|
|
61
|
+
inputTokens: usage.inputTokens,
|
|
62
|
+
outputTokens: usage.outputTokens,
|
|
63
|
+
cachedTokens: usage.cachedTokens,
|
|
64
|
+
},
|
|
65
|
+
rounds: bills.map((bill) => bill.usage),
|
|
66
|
+
cost: cost > 0 ? cost : undefined,
|
|
67
|
+
...(replayed.length > 0
|
|
68
|
+
? {
|
|
69
|
+
billedCost: cost - sum(replayed.map((round) => round.cost)),
|
|
70
|
+
}
|
|
71
|
+
: {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* What a call that throws must charge: every round paid for now. A replayed round
|
|
76
|
+
* was charged by the attempt that paid for it.
|
|
77
|
+
*/
|
|
78
|
+
charge() {
|
|
79
|
+
const paid = this.rounds.filter((round) => !round.replayed);
|
|
80
|
+
const bill = addBills(this.held, ...paid) ?? {
|
|
81
|
+
usage: {
|
|
82
|
+
inputTokens: 0,
|
|
83
|
+
outputTokens: 0,
|
|
84
|
+
},
|
|
85
|
+
cost: 0,
|
|
86
|
+
};
|
|
87
|
+
return {
|
|
88
|
+
...bill.usage,
|
|
89
|
+
cost: bill.cost,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function addBills(...bills) {
|
|
94
|
+
const present = bills.filter((bill) => bill !== undefined);
|
|
95
|
+
if (present.length === 0) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
usage: sumUsage(present.map((bill) => bill.usage)),
|
|
100
|
+
cost: sum(present.map((bill) => bill.cost)),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function sum(values) {
|
|
104
|
+
return values.reduce((total, value) => total + value, 0);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Add rounds' usage. A cache figure stays `undefined` when no round reported one, so
|
|
108
|
+
* callers can tell "this provider says nothing about caching" from "nothing was cached".
|
|
109
|
+
*/
|
|
110
|
+
function sumUsage(rounds) {
|
|
111
|
+
const [only] = rounds;
|
|
112
|
+
if (rounds.length === 1 && only) {
|
|
113
|
+
return only;
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
inputTokens: sum(rounds.map((r) => r.inputTokens)),
|
|
117
|
+
outputTokens: sum(rounds.map((r) => r.outputTokens)),
|
|
118
|
+
cachedTokens: sumReported(rounds.map((r) => r.cachedTokens)),
|
|
119
|
+
cacheWriteTokens: sumReported(rounds.map((r) => r.cacheWriteTokens)),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function sumReported(values) {
|
|
123
|
+
const reported = values.filter((value) => value !== undefined);
|
|
124
|
+
return reported.length > 0 ? sum(reported) : undefined;
|
|
125
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { CallModelRequest, DecideRequest, DecideResponse, LLMResponse } from '@noetic-tools/types';
|
|
2
2
|
import type { OpenRouter } from '@openrouter/sdk';
|
|
3
|
+
import type { Spend } from './spend.js';
|
|
3
4
|
/** @internal The slice of the SDK client the decide caller uses. */
|
|
4
5
|
export type DecisionsClient = Pick<OpenRouter, 'alpha' | 'models'>;
|
|
5
6
|
/** @internal */
|
|
@@ -8,6 +9,8 @@ export interface DecideCallerDeps {
|
|
|
8
9
|
callModel: (request: CallModelRequest) => Promise<LLMResponse>;
|
|
9
10
|
/** Per-harness cache: model id → whether it is a decision model (`undefined` while unknown). */
|
|
10
11
|
nativeModels: Map<string, Promise<boolean | undefined>>;
|
|
12
|
+
/** Records each native call before it is sent. Absent without durable storage. */
|
|
13
|
+
spend?: Spend;
|
|
11
14
|
}
|
|
12
15
|
/**
|
|
13
16
|
* Answer a decide request. Decision models (those whose OpenRouter output
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { choiceValues, createMessage, DecideOutputError, extractAssistantText, frameworkCast, scoreLevels, } from '@noetic-tools/types';
|
|
1
|
+
import { choiceValues, createMessage, DecideOutputError, extractAssistantText, frameworkCast, SpendKind, scoreLevels, } from '@noetic-tools/types';
|
|
2
2
|
import { z } from 'zod';
|
|
3
|
-
import { statusOf } from '../util/status-code.js';
|
|
3
|
+
import { isErrorStatus, statusOf } from '../util/status-code.js';
|
|
4
4
|
//#endregion
|
|
5
5
|
//#region Public API
|
|
6
6
|
/**
|
|
@@ -12,7 +12,7 @@ import { statusOf } from '../util/status-code.js';
|
|
|
12
12
|
*/
|
|
13
13
|
export async function callDecide(request, deps) {
|
|
14
14
|
if (deps.client && (await isDecisionModel(request.model, deps))) {
|
|
15
|
-
return callNative(request, deps.client);
|
|
15
|
+
return callNative(request, deps.client, deps.spend);
|
|
16
16
|
}
|
|
17
17
|
return callEstimated(request, deps);
|
|
18
18
|
}
|
|
@@ -54,19 +54,21 @@ async function lookupDecisionModel(model, client) {
|
|
|
54
54
|
}
|
|
55
55
|
//#endregion
|
|
56
56
|
//#region Native path
|
|
57
|
-
async function callNative(request, client) {
|
|
57
|
+
async function callNative(request, client, spend) {
|
|
58
58
|
const questions = {};
|
|
59
59
|
for (const [id, question] of Object.entries(request.questions)) {
|
|
60
60
|
questions[id] = toNativeQuestion(question);
|
|
61
61
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
const decisionsRequest = {
|
|
63
|
+
model: request.model,
|
|
64
|
+
state: toNativeState(request.instructions, request.state),
|
|
65
|
+
questions,
|
|
66
|
+
};
|
|
67
|
+
const response = await sendNative({
|
|
68
|
+
client,
|
|
69
|
+
decisionsRequest,
|
|
70
|
+
request,
|
|
71
|
+
spend,
|
|
70
72
|
});
|
|
71
73
|
const answers = {};
|
|
72
74
|
for (const [id, question] of Object.entries(request.questions)) {
|
|
@@ -86,6 +88,42 @@ async function callNative(request, client) {
|
|
|
86
88
|
cost: response.usage.cost,
|
|
87
89
|
};
|
|
88
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Send one native call under a dispatch record. The dispatch header lets the client's
|
|
93
|
+
* response hook save the generation id when the headers arrive, so a call cut off
|
|
94
|
+
* before its body finishes can still be priced.
|
|
95
|
+
*/
|
|
96
|
+
async function sendNative({ client, decisionsRequest, request, spend, }) {
|
|
97
|
+
const dispatch = await spend?.open({
|
|
98
|
+
ctx: request._billTo,
|
|
99
|
+
kind: SpendKind.Decide,
|
|
100
|
+
model: request.model,
|
|
101
|
+
body: decisionsRequest,
|
|
102
|
+
});
|
|
103
|
+
try {
|
|
104
|
+
const response = await client.alpha.decisions.create({
|
|
105
|
+
decisionsRequest,
|
|
106
|
+
}, {
|
|
107
|
+
signal: request.signal,
|
|
108
|
+
...(dispatch
|
|
109
|
+
? {
|
|
110
|
+
headers: dispatch.headers,
|
|
111
|
+
}
|
|
112
|
+
: {}),
|
|
113
|
+
});
|
|
114
|
+
await dispatch?.settle();
|
|
115
|
+
return response;
|
|
116
|
+
}
|
|
117
|
+
catch (e) {
|
|
118
|
+
if (isErrorStatus(e)) {
|
|
119
|
+
await dispatch?.settle();
|
|
120
|
+
}
|
|
121
|
+
throw e;
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
dispatch?.end();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
89
127
|
/**
|
|
90
128
|
* The native APIs require `instructions` on every question, while Noetic lets a
|
|
91
129
|
* boolean question carry its meaning in `criteria` alone; and TypeSafe needs
|
|
@@ -251,6 +289,7 @@ async function callEstimated(request, deps) {
|
|
|
251
289
|
outputSchema: schema,
|
|
252
290
|
signal: request.signal,
|
|
253
291
|
parentSpan: request.parentSpan,
|
|
292
|
+
_billTo: request._billTo,
|
|
254
293
|
})));
|
|
255
294
|
const runs = settled.flatMap((r) => r.status === 'fulfilled'
|
|
256
295
|
? [
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { OpenRouter } from '@openrouter/agent';
|
|
2
|
-
export { OpenRouter as OpenRouterSdk } from '@openrouter/sdk';
|
|
2
|
+
export { HTTPClient, OpenRouter as OpenRouterSdk } from '@openrouter/sdk';
|
|
3
3
|
export { createInMemoryFsAdapter } from '../../adapters/in-memory-fs-adapter.js';
|
|
4
4
|
export { createInMemoryShellAdapter } from '../../adapters/in-memory-shell-adapter.js';
|
|
5
5
|
export { createInMemorySubprocessAdapter } from '../../adapters/in-memory-subprocess-adapter.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { OpenRouter } from '@openrouter/agent';
|
|
2
|
-
export { OpenRouter as OpenRouterSdk } from '@openrouter/sdk';
|
|
2
|
+
export { HTTPClient, OpenRouter as OpenRouterSdk } from '@openrouter/sdk';
|
|
3
3
|
export { createInMemoryFsAdapter } from '../../adapters/in-memory-fs-adapter.js';
|
|
4
4
|
export { createInMemoryShellAdapter } from '../../adapters/in-memory-shell-adapter.js';
|
|
5
5
|
export { createInMemorySubprocessAdapter } from '../../adapters/in-memory-subprocess-adapter.js';
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
export type { ChannelWarning } from '../../runtime/channel-store.js';
|
|
2
2
|
export { ChannelStore, warnToConsole } from '../../runtime/channel-store.js';
|
|
3
|
-
export { ContextImpl, collectContextTree, rootIdOf } from '../../runtime/context-impl.js';
|
|
3
|
+
export { ContextImpl, collectContextTree, laneBase, rootIdOf } from '../../runtime/context-impl.js';
|
|
4
4
|
export { snapshotCwdState } from '../../runtime/cwd-helpers.js';
|
|
5
|
+
export { Drains } from '../../runtime/drain.js';
|
|
5
6
|
export type { CheckpointStore } from '../../runtime/durable/index.js';
|
|
6
7
|
export { createCheckpointStore } from '../../runtime/durable/checkpoint-store.js';
|
|
7
8
|
export type { RestoreCheckpointOptions } from '../../runtime/durable/harness-checkpoints.js';
|
|
8
9
|
export { captureCheckpoint, clearCheckpoint, restoreFromCheckpoint, } from '../../runtime/durable/harness-checkpoints.js';
|
|
9
10
|
export type { SessionStore } from '../../runtime/durable/session-store.js';
|
|
10
11
|
export { createSessionStore } from '../../runtime/durable/session-store.js';
|
|
12
|
+
export { createSpendLog } from '../../runtime/durable/spend-log.js';
|
|
11
13
|
export type { StepLedgerEntry, StepLedgerRetention, StepLedgerStore, } from '../../runtime/durable/step-ledger.js';
|
|
12
14
|
export { createStepLedgerStore, resolveStepLedgerRetention, StepLedger, } from '../../runtime/durable/step-ledger.js';
|
|
13
15
|
export type { EventBroadcaster } from '../../runtime/event-broadcaster.js';
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { ChannelStore, warnToConsole } from '../../runtime/channel-store.js';
|
|
2
|
-
export { ContextImpl, collectContextTree, rootIdOf } from '../../runtime/context-impl.js';
|
|
2
|
+
export { ContextImpl, collectContextTree, laneBase, rootIdOf } from '../../runtime/context-impl.js';
|
|
3
3
|
export { snapshotCwdState } from '../../runtime/cwd-helpers.js';
|
|
4
|
+
export { Drains } from '../../runtime/drain.js';
|
|
4
5
|
export { createCheckpointStore } from '../../runtime/durable/checkpoint-store.js';
|
|
5
6
|
export { captureCheckpoint, clearCheckpoint, restoreFromCheckpoint, } from '../../runtime/durable/harness-checkpoints.js';
|
|
6
7
|
export { createSessionStore } from '../../runtime/durable/session-store.js';
|
|
8
|
+
export { createSpendLog } from '../../runtime/durable/spend-log.js';
|
|
7
9
|
export { createStepLedgerStore, resolveStepLedgerRetention, StepLedger, } from '../../runtime/durable/step-ledger.js';
|
|
8
10
|
export { createInMemoryStorage } from '../../runtime/in-memory-storage.js';
|
|
9
11
|
export { customItemGates } from '../../runtime/item-gates.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { ContextCacheConfig, ContextCacheStore, ContextData, ContextLayer, ExecutionContext, ProjectionPolicy, StorageAdapter, } from '@noetic-tools/context';
|
|
2
|
-
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, ExecuteInput, ExecuteOptions, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, Item, ItemSchemaConfig, ItemSchemaExtensions, LLMResponse, LlmProviderConfig, OnSteerParams, RecallLayerOutput, RecallOptions, SessionScope, SessionUsage, ShellAdapter, Span, SteeringDecision, SteerVerdict, Step, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, } from '@noetic-tools/types';
|
|
2
|
+
export type { AcpKeepAlive, AcpLiveSession, AcpSessionInfo, AgentConfig, AgentHarnessContract, AgentHooks, CallModelRequest, CancelOptions, Channel, ChannelHandle, Context, CustomItem, CustomItemOf, CustomItemStreamEvent, CustomItemsBrand, CustomStreamingItem, CwdState, DecideRequest, DecideResponse, DecisionRecord, DeclaredItems, DeliveryMode, DetachedHandle, DrainReport, ExecuteInput, ExecuteOptions, ExecutionScope, ExecutionStatus, ExternalChannel, FsAdapter, HarnessResponse, HarnessStatus, Item, ItemSchemaConfig, ItemSchemaExtensions, LLMResponse, LlmProviderConfig, OnSteerParams, PendingSpend, RecallLayerOutput, RecallOptions, ReconcileSpendOptions, SessionScope, SessionUsage, ShellAdapter, Span, SpendSettlement, SteeringDecision, SteerVerdict, Step, StreamEvent, StreamingItem, SubprocessAdapter, Tool, ToolApprover, TraceExporter, UsageCharge, WriteOffSpendOptions, } from '@noetic-tools/types';
|
|
3
3
|
export { SteeringAction } from '@noetic-tools/types';
|
|
4
4
|
export type { ZodType } from 'zod';
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AgentHarnessContract, CallModelRequest, ItemSchemaRegistry, LLMResponse, TraceExporter } from '@noetic-tools/types';
|
|
2
2
|
import { NoeticConfigError } from '@noetic-tools/types';
|
|
3
3
|
import type { OpenRouter } from '@openrouter/agent';
|
|
4
|
+
import type { Spend } from './spend.js';
|
|
4
5
|
interface StreamIdleWatchdog {
|
|
5
6
|
/** Bump the deadline because a stream event just arrived. */
|
|
6
7
|
reset: () => void;
|
|
@@ -24,6 +25,8 @@ interface AgentHarnessModelCallerOpts {
|
|
|
24
25
|
streamIdleTimeoutMs: number;
|
|
25
26
|
harness: AgentHarnessContract;
|
|
26
27
|
traceExporter: TraceExporter;
|
|
28
|
+
/** Records each paid round before it is sent. Absent without durable storage. */
|
|
29
|
+
spend?: Spend;
|
|
27
30
|
}
|
|
28
31
|
export declare class AgentHarnessModelCaller {
|
|
29
32
|
private readonly opts;
|