@noetic-tools/core 5.21.0 → 5.22.0-next.2
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 +6 -1
- package/dist/adapters/openrouter.js +10 -0
- package/dist/builders/decide-builders.d.ts +7 -0
- package/dist/builders/tool-builder.d.ts +25 -1
- package/dist/builders/tool-builder.js +25 -1
- package/dist/builders/workflow-control-nodes.js +1 -0
- package/dist/builders/workflow-hydrator.d.ts +19 -0
- package/dist/builders/workflow-hydrator.js +19 -0
- package/dist/builders/workflow-step.js +7 -1
- package/dist/connections/cron-schedule.js +12 -3
- package/dist/connections/durability.d.ts +1 -2
- package/dist/connections/durability.js +1 -4
- package/dist/connections/gates.js +6 -2
- package/dist/connections/harness-wiring.d.ts +5 -0
- package/dist/connections/harness-wiring.js +76 -10
- package/dist/connections/in-memory-connection.d.ts +4 -0
- package/dist/connections/in-memory-connection.js +10 -0
- package/dist/connections/model-tools.js +5 -1
- package/dist/connections/registry.d.ts +6 -2
- package/dist/connections/registry.js +10 -0
- package/dist/connections/sources.js +261 -20
- package/dist/connections/triggers.d.ts +62 -2
- package/dist/connections/triggers.js +375 -35
- package/dist/harness/agent-harness.d.ts +317 -4
- package/dist/harness/agent-harness.js +1264 -96
- package/dist/harness/connection-gate.d.ts +3 -0
- package/dist/harness/connection-gate.js +4 -0
- package/dist/harness/decide-call.d.ts +15 -0
- package/dist/harness/decide-call.js +57 -7
- package/dist/harness/deps/runtime.d.ts +3 -0
- package/dist/harness/deps/runtime.js +2 -0
- package/dist/harness/flow-version.d.ts +13 -0
- package/dist/harness/flow-version.js +66 -0
- package/dist/harness/harness-options.d.ts +91 -0
- package/dist/harness/llm-client.d.ts +2 -0
- package/dist/harness/llm-client.js +9 -5
- package/dist/harness/model-call.js +8 -0
- package/dist/harness/model-rounds.d.ts +30 -0
- package/dist/harness/model-rounds.js +146 -1
- package/dist/harness/model-tool-round.d.ts +3 -0
- package/dist/harness/model-tool-round.js +249 -22
- package/dist/harness/park-watchers.d.ts +93 -0
- package/dist/harness/park-watchers.js +217 -0
- package/dist/harness/read-decision.d.ts +23 -0
- package/dist/harness/read-decision.js +100 -0
- package/dist/harness/spend.js +4 -0
- package/dist/harness/steer-inbox.js +3 -1
- package/dist/harness/thread-record.d.ts +77 -1
- package/dist/harness/thread-record.js +272 -13
- package/dist/index.d.ts +22 -8
- package/dist/index.js +12 -2
- package/dist/interpreter/acp-resume.d.ts +77 -0
- package/dist/interpreter/acp-resume.js +164 -0
- package/dist/interpreter/acp-tools.d.ts +11 -0
- package/dist/interpreter/acp-tools.js +94 -7
- package/dist/interpreter/connection-steps.d.ts +24 -1
- package/dist/interpreter/connection-steps.js +77 -2
- package/dist/interpreter/execute-acp-agent.js +111 -15
- package/dist/interpreter/execute-action.js +7 -0
- package/dist/interpreter/execute-call.js +30 -4
- package/dist/interpreter/execute-control.js +21 -0
- package/dist/interpreter/execute-decide.js +5 -0
- package/dist/interpreter/execute-invoke-tool.js +149 -5
- package/dist/interpreter/execute-wait.js +67 -9
- package/dist/interpreter/execute.js +4 -0
- package/dist/interpreter/turn-ending.d.ts +15 -0
- package/dist/interpreter/turn-ending.js +46 -0
- package/dist/portable/builders.d.ts +1 -1
- package/dist/portable/builders.js +1 -1
- package/dist/portable/runtime.d.ts +4 -0
- package/dist/portable/runtime.js +2 -0
- package/dist/runtime/durable/parking.d.ts +61 -0
- package/dist/runtime/durable/parking.js +137 -0
- package/dist/runtime/durable/session-store.d.ts +38 -0
- package/dist/runtime/durable/session-store.js +79 -1
- package/dist/runtime/durable/step-ledger.d.ts +121 -2
- package/dist/runtime/durable/step-ledger.js +352 -15
- package/dist/runtime/durable/turn-lease.d.ts +128 -0
- package/dist/runtime/durable/turn-lease.js +307 -0
- package/dist/runtime/event-broadcaster.d.ts +9 -1
- package/dist/runtime/event-broadcaster.js +16 -1
- package/dist/runtime/held-calls.d.ts +192 -0
- package/dist/runtime/held-calls.js +496 -0
- package/dist/runtime/in-memory-storage.d.ts +10 -1
- package/dist/runtime/in-memory-storage.js +20 -2
- package/dist/runtime/message-queue.d.ts +2 -0
- package/dist/runtime/message-queue.js +8 -0
- package/dist/runtime/output-store.d.ts +88 -0
- package/dist/runtime/output-store.js +241 -0
- package/dist/runtime/session-runner.d.ts +18 -2
- package/dist/runtime/session-runner.js +88 -3
- package/dist/runtime/session-streams.d.ts +28 -7
- package/dist/runtime/session-streams.js +112 -67
- package/dist/runtime/tool-approval.d.ts +46 -6
- package/dist/runtime/tool-approval.js +160 -38
- package/dist/runtime/tool-call.d.ts +34 -2
- package/dist/runtime/tool-call.js +182 -13
- package/dist/runtime/tool-context.d.ts +15 -1
- package/dist/runtime/tool-context.js +38 -2
- package/package.json +3 -3
- package/schema/noetic-workflow.schema.json +27 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ConnectionEntries, ConnectionStatus } from '@noetic-tools/types';
|
|
2
2
|
import type { HarnessConnectionsOptions } from '../connections/harness-wiring.js';
|
|
3
3
|
import type { ConnectionRuntime } from '../connections/registry.js';
|
|
4
|
+
import type { TriggerRunner } from '../connections/triggers.js';
|
|
4
5
|
import type { Context, ContextLayer, Step, StorageAdapter, Tool } from './deps/types.js';
|
|
5
6
|
/**
|
|
6
7
|
* A harness's connections, as the harness drives them: providers and triggers,
|
|
@@ -63,6 +64,8 @@ export declare class ConnectionGate {
|
|
|
63
64
|
readonly layers: ContextLayer[];
|
|
64
65
|
bind(root: Context): void;
|
|
65
66
|
};
|
|
67
|
+
/** The harness's triggers: its own, and those added while it runs. */
|
|
68
|
+
get triggers(): TriggerRunner;
|
|
66
69
|
/** Connects providers and starts triggers. Idempotent. */
|
|
67
70
|
start(): Promise<void>;
|
|
68
71
|
/** Each connection's state, identity and pool usage; one entry for `name`, or all. */
|
|
@@ -136,6 +136,10 @@ export class ConnectionGate {
|
|
|
136
136
|
},
|
|
137
137
|
};
|
|
138
138
|
}
|
|
139
|
+
/** The harness's triggers: its own, and those added while it runs. */
|
|
140
|
+
get triggers() {
|
|
141
|
+
return this.#connections.triggers;
|
|
142
|
+
}
|
|
139
143
|
/** Connects providers and starts triggers. Idempotent. */
|
|
140
144
|
async start() {
|
|
141
145
|
await this.#connections.start();
|
|
@@ -6,12 +6,27 @@ export type DecisionsClient = Pick<OpenRouter, 'alpha' | 'models'>;
|
|
|
6
6
|
/** @internal */
|
|
7
7
|
export interface DecideCallerDeps {
|
|
8
8
|
client?: DecisionsClient;
|
|
9
|
+
/**
|
|
10
|
+
* Where native decisions are sent, when not to OpenRouter: the client's own base
|
|
11
|
+
* URL (see `decisionsServerUrl`). The SDK sends them to openrouter.ai whatever the
|
|
12
|
+
* client was built with, so without this a credential for another route would go there.
|
|
13
|
+
*/
|
|
14
|
+
serverUrl?: string;
|
|
9
15
|
callModel: (request: CallModelRequest) => Promise<LLMResponse>;
|
|
10
16
|
/** Per-harness cache: model id → whether it is a decision model (`undefined` while unknown). */
|
|
11
17
|
nativeModels: Map<string, Promise<boolean | undefined>>;
|
|
12
18
|
/** Records each native call before it is sent. Absent without durable storage. */
|
|
13
19
|
spend?: Spend;
|
|
14
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* The base URL native decisions are sent to, given the one the client was built
|
|
23
|
+
* with: that URL without trailing slashes, or undefined for OpenRouter's own (or
|
|
24
|
+
* none), whose decisions endpoint the SDK already knows. Any other base must
|
|
25
|
+
* serve `api/alpha/decisions` under its own path.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare function decisionsServerUrl(clientServerUrl: string | undefined): string | undefined;
|
|
15
30
|
/**
|
|
16
31
|
* Answer a decide request. Decision models (those whose OpenRouter output
|
|
17
32
|
* modality is `decisions`) answer natively with their own distributions; any
|
|
@@ -3,6 +3,33 @@ import { z } from 'zod';
|
|
|
3
3
|
import { isErrorStatus, statusOf } from '../util/status-code.js';
|
|
4
4
|
//#endregion
|
|
5
5
|
//#region Public API
|
|
6
|
+
const OPENROUTER_HOST = 'openrouter.ai';
|
|
7
|
+
/** Whether `url` is OpenRouter's own API, however its host and port are spelled. */
|
|
8
|
+
function isOpenRouter(url) {
|
|
9
|
+
let parsed;
|
|
10
|
+
try {
|
|
11
|
+
parsed = new URL(url);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
// `URL` lower-cases the host and drops a scheme's default port.
|
|
17
|
+
return parsed.protocol === 'https:' && parsed.host === OPENROUTER_HOST;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The base URL native decisions are sent to, given the one the client was built
|
|
21
|
+
* with: that URL without trailing slashes, or undefined for OpenRouter's own (or
|
|
22
|
+
* none), whose decisions endpoint the SDK already knows. Any other base must
|
|
23
|
+
* serve `api/alpha/decisions` under its own path.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export function decisionsServerUrl(clientServerUrl) {
|
|
28
|
+
if (!clientServerUrl || isOpenRouter(clientServerUrl)) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return clientServerUrl.replace(/\/+$/, '');
|
|
32
|
+
}
|
|
6
33
|
/**
|
|
7
34
|
* Answer a decide request. Decision models (those whose OpenRouter output
|
|
8
35
|
* modality is `decisions`) answer natively with their own distributions; any
|
|
@@ -12,7 +39,11 @@ import { isErrorStatus, statusOf } from '../util/status-code.js';
|
|
|
12
39
|
*/
|
|
13
40
|
export async function callDecide(request, deps) {
|
|
14
41
|
if (deps.client && (await isDecisionModel(request.model, deps))) {
|
|
15
|
-
return callNative(request,
|
|
42
|
+
return callNative(request, {
|
|
43
|
+
client: deps.client,
|
|
44
|
+
serverUrl: deps.serverUrl,
|
|
45
|
+
spend: deps.spend,
|
|
46
|
+
});
|
|
16
47
|
}
|
|
17
48
|
return callEstimated(request, deps);
|
|
18
49
|
}
|
|
@@ -54,7 +85,7 @@ async function lookupDecisionModel(model, client) {
|
|
|
54
85
|
}
|
|
55
86
|
//#endregion
|
|
56
87
|
//#region Native path
|
|
57
|
-
async function callNative(request,
|
|
88
|
+
async function callNative(request, route) {
|
|
58
89
|
const questions = {};
|
|
59
90
|
for (const [id, question] of Object.entries(request.questions)) {
|
|
60
91
|
questions[id] = toNativeQuestion(question);
|
|
@@ -65,10 +96,9 @@ async function callNative(request, client, spend) {
|
|
|
65
96
|
questions,
|
|
66
97
|
};
|
|
67
98
|
const response = await sendNative({
|
|
68
|
-
|
|
99
|
+
...route,
|
|
69
100
|
decisionsRequest,
|
|
70
101
|
request,
|
|
71
|
-
spend,
|
|
72
102
|
});
|
|
73
103
|
const answers = {};
|
|
74
104
|
for (const [id, question] of Object.entries(request.questions)) {
|
|
@@ -93,7 +123,7 @@ async function callNative(request, client, spend) {
|
|
|
93
123
|
* response hook save the generation id when the headers arrive, so a call cut off
|
|
94
124
|
* before its body finishes can still be priced.
|
|
95
125
|
*/
|
|
96
|
-
async function sendNative({ client, decisionsRequest, request, spend, }) {
|
|
126
|
+
async function sendNative({ client, serverUrl, decisionsRequest, request, spend, }) {
|
|
97
127
|
const dispatch = await spend?.open({
|
|
98
128
|
ctx: request._billTo,
|
|
99
129
|
kind: SpendKind.Decide,
|
|
@@ -105,11 +135,12 @@ async function sendNative({ client, decisionsRequest, request, spend, }) {
|
|
|
105
135
|
decisionsRequest,
|
|
106
136
|
}, {
|
|
107
137
|
signal: request.signal,
|
|
108
|
-
...(
|
|
138
|
+
...(serverUrl
|
|
109
139
|
? {
|
|
110
|
-
|
|
140
|
+
serverURL: serverUrl,
|
|
111
141
|
}
|
|
112
142
|
: {}),
|
|
143
|
+
...nativeHeaders(dispatch?.headers, request),
|
|
113
144
|
});
|
|
114
145
|
await dispatch?.settle();
|
|
115
146
|
return response;
|
|
@@ -124,6 +155,25 @@ async function sendNative({ client, decisionsRequest, request, spend, }) {
|
|
|
124
155
|
dispatch?.end();
|
|
125
156
|
}
|
|
126
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* The request headers of a native call: the request's own, then the dispatch
|
|
160
|
+
* record's. Header names are not case-sensitive, so a request header that names a
|
|
161
|
+
* dispatch header in any case is dropped: sent beside it, the two values would be
|
|
162
|
+
* joined into one that names no record.
|
|
163
|
+
*/
|
|
164
|
+
function nativeHeaders(dispatchHeaders, request) {
|
|
165
|
+
if (!dispatchHeaders && !request.headers) {
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
const reserved = new Set(Object.keys(dispatchHeaders ?? {}).map((name) => name.toLowerCase()));
|
|
169
|
+
const own = Object.entries(request.headers ?? {}).filter(([name]) => !reserved.has(name.toLowerCase()));
|
|
170
|
+
return {
|
|
171
|
+
headers: {
|
|
172
|
+
...Object.fromEntries(own),
|
|
173
|
+
...dispatchHeaders,
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
127
177
|
/**
|
|
128
178
|
* The native APIs require `instructions` on every question, while Noetic lets a
|
|
129
179
|
* boolean question carry its meaning in `criteria` alone; and TypeSafe needs
|
|
@@ -7,11 +7,14 @@ export type { CheckpointStore } from '../../runtime/durable/checkpoint-store.js'
|
|
|
7
7
|
export { createCheckpointStore } from '../../runtime/durable/checkpoint-store.js';
|
|
8
8
|
export type { RestoreCheckpointOptions } from '../../runtime/durable/harness-checkpoints.js';
|
|
9
9
|
export { captureCheckpoint, clearCheckpoint, restoreFromCheckpoint, } from '../../runtime/durable/harness-checkpoints.js';
|
|
10
|
+
export { DEFAULT_PARK_GRACE_MS, PARK_REASON } from '../../runtime/durable/parking.js';
|
|
10
11
|
export type { SessionStore } from '../../runtime/durable/session-store.js';
|
|
11
12
|
export { createSessionStore } from '../../runtime/durable/session-store.js';
|
|
12
13
|
export { createSpendLog } from '../../runtime/durable/spend-log.js';
|
|
13
14
|
export type { StepLedgerEntry, StepLedgerRetention, StepLedgerStore, } from '../../runtime/durable/step-ledger.js';
|
|
14
15
|
export { createStepLedgerStore, resolveStepLedgerRetention, StepLedger, } from '../../runtime/durable/step-ledger.js';
|
|
16
|
+
export type { TurnLeaseOptions } from '../../runtime/durable/turn-lease.js';
|
|
17
|
+
export { canSwap, DEFAULT_LEASE_TTL_MS, heldElsewhere, heldElsewhereUntil, TurnLease, } from '../../runtime/durable/turn-lease.js';
|
|
15
18
|
export type { EventBroadcaster } from '../../runtime/event-broadcaster.js';
|
|
16
19
|
export { createInMemoryStorage } from '../../runtime/in-memory-storage.js';
|
|
17
20
|
export { customItemGates } from '../../runtime/item-gates.js';
|
|
@@ -4,9 +4,11 @@ export { snapshotCwdState } from '../../runtime/cwd-helpers.js';
|
|
|
4
4
|
export { Drains } from '../../runtime/drain.js';
|
|
5
5
|
export { createCheckpointStore } from '../../runtime/durable/checkpoint-store.js';
|
|
6
6
|
export { captureCheckpoint, clearCheckpoint, restoreFromCheckpoint, } from '../../runtime/durable/harness-checkpoints.js';
|
|
7
|
+
export { DEFAULT_PARK_GRACE_MS, PARK_REASON } from '../../runtime/durable/parking.js';
|
|
7
8
|
export { createSessionStore } from '../../runtime/durable/session-store.js';
|
|
8
9
|
export { createSpendLog } from '../../runtime/durable/spend-log.js';
|
|
9
10
|
export { createStepLedgerStore, resolveStepLedgerRetention, StepLedger, } from '../../runtime/durable/step-ledger.js';
|
|
11
|
+
export { canSwap, DEFAULT_LEASE_TTL_MS, heldElsewhere, heldElsewhereUntil, TurnLease, } from '../../runtime/durable/turn-lease.js';
|
|
10
12
|
export { createInMemoryStorage } from '../../runtime/in-memory-storage.js';
|
|
11
13
|
export { customItemGates } from '../../runtime/item-gates.js';
|
|
12
14
|
export { INTERRUPT_REASON, isSteer } from '../../runtime/message-queue.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The version of a flow that runs JSON workflows, for a harness that was given no
|
|
3
|
+
* `version` (spec 23 § Versions): a hash of every document the flow can run, so
|
|
4
|
+
* an edited document is a new version without the host naming one.
|
|
5
|
+
*/
|
|
6
|
+
import type { Step } from '@noetic-tools/types';
|
|
7
|
+
/**
|
|
8
|
+
* The version of the flows in `graph`: a hash of each JSON workflow document it
|
|
9
|
+
* runs and of the named workflows those can refer to, the same whatever order
|
|
10
|
+
* their keys were written in. Undefined when `graph` runs no JSON workflow, or one
|
|
11
|
+
* of its documents cannot be encoded.
|
|
12
|
+
*/
|
|
13
|
+
export declare function flowVersionOf(graph: Step | undefined): string | undefined;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The version of a flow that runs JSON workflows, for a harness that was given no
|
|
3
|
+
* `version` (spec 23 § Versions): a hash of every document the flow can run, so
|
|
4
|
+
* an edited document is a new version without the host naming one.
|
|
5
|
+
*/
|
|
6
|
+
import { flowSourceOf } from '../builders/workflow-hydrator.js';
|
|
7
|
+
import { stepChildren } from '../interpreter/step-walk.js';
|
|
8
|
+
import { hashText } from '../runtime/durable/step-ledger.js';
|
|
9
|
+
//#region Helpers
|
|
10
|
+
/** Every step in the tree under `root`, itself included, as far as it is statically known. */
|
|
11
|
+
function stepsIn(root) {
|
|
12
|
+
const found = [];
|
|
13
|
+
const seen = new Set();
|
|
14
|
+
const queue = [
|
|
15
|
+
root,
|
|
16
|
+
];
|
|
17
|
+
for (let next = queue.pop(); next !== undefined; next = queue.pop()) {
|
|
18
|
+
if (seen.has(next)) {
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
seen.add(next);
|
|
22
|
+
found.push(next);
|
|
23
|
+
queue.push(...stepChildren(next));
|
|
24
|
+
}
|
|
25
|
+
return found;
|
|
26
|
+
}
|
|
27
|
+
/** A plain object's entries in key order, so two spellings of one document hash alike. */
|
|
28
|
+
function sortKeys(_key, value) {
|
|
29
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
return Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
33
|
+
}
|
|
34
|
+
/** What of a flow's source goes into its version. */
|
|
35
|
+
function describe(source) {
|
|
36
|
+
return {
|
|
37
|
+
document: source.document ?? null,
|
|
38
|
+
ref: source.ref ?? null,
|
|
39
|
+
// A subflow `ref` runs one of these: an edit to it is an edit to the flow.
|
|
40
|
+
workflows: source.workflows ? Object.fromEntries(source.workflows) : null,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region Public API
|
|
45
|
+
/**
|
|
46
|
+
* The version of the flows in `graph`: a hash of each JSON workflow document it
|
|
47
|
+
* runs and of the named workflows those can refer to, the same whatever order
|
|
48
|
+
* their keys were written in. Undefined when `graph` runs no JSON workflow, or one
|
|
49
|
+
* of its documents cannot be encoded.
|
|
50
|
+
*/
|
|
51
|
+
export function flowVersionOf(graph) {
|
|
52
|
+
if (!graph) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
const sources = stepsIn(graph).flatMap((step) => flowSourceOf(step) ?? []);
|
|
56
|
+
if (sources.length === 0) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
return `workflow-${hashText(JSON.stringify(sources.map(describe), sortKeys))}`;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
@@ -33,6 +33,72 @@ export interface StorageEnvironmentConfig {
|
|
|
33
33
|
* never correctness.
|
|
34
34
|
*/
|
|
35
35
|
stepLedgerRetention?: StepLedgerRetention;
|
|
36
|
+
/**
|
|
37
|
+
* The turn lease (spec 23 § One writer per turn). Used only when `adapter`
|
|
38
|
+
* implements `swap`: each session turn then takes its thread's lease, so two
|
|
39
|
+
* harnesses on one storage cannot both drive a turn, and a failed durable write
|
|
40
|
+
* stops the turn instead of being logged.
|
|
41
|
+
*/
|
|
42
|
+
lease?: TurnLeaseConfig;
|
|
43
|
+
/**
|
|
44
|
+
* Park a turn when every step in flight is a wait that can be resumed without
|
|
45
|
+
* loss (spec 23 § Parked turns). The turn's lease is handed back, its response
|
|
46
|
+
* resolves with `parked`, and `harness.wake()` resumes it. Needs a lease, so it
|
|
47
|
+
* has no effect on storage without `swap`. Default `false`: a waiting turn holds
|
|
48
|
+
* its process. `true` uses `DEFAULT_PARK_GRACE_MS`.
|
|
49
|
+
*/
|
|
50
|
+
park?: boolean | ParkConfig;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @public
|
|
54
|
+
* Hears the harness's next wake time when it changes (`harness.onWakeTimeChange`):
|
|
55
|
+
* epoch ms, or `undefined` when there is nothing to wake for.
|
|
56
|
+
*/
|
|
57
|
+
export type WakeTimeListener = (at: number | undefined) => void | Promise<void>;
|
|
58
|
+
/** @public Settings for parking. */
|
|
59
|
+
export interface ParkConfig {
|
|
60
|
+
/**
|
|
61
|
+
* How long every step in flight must stay waiting before the turn parks, in ms.
|
|
62
|
+
* Keeps a turn whose event is about to arrive from parking and waking at once.
|
|
63
|
+
* Default `DEFAULT_PARK_GRACE_MS` (100).
|
|
64
|
+
*/
|
|
65
|
+
graceMs?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Whether this harness wakes the turns it parked: at their deadline, and when an
|
|
68
|
+
* event they wait for arrives on a harness connection. Default `true`, which
|
|
69
|
+
* suits a long-lived process. Set `false` where no process lives between
|
|
70
|
+
* requests; the host then calls `harness.wake()` itself.
|
|
71
|
+
*/
|
|
72
|
+
autoWake?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* How long auto-wake waits after an event before waking the turn, in ms, so a
|
|
75
|
+
* burst of events wakes it once. Default `DEFAULT_WAKE_DEBOUNCE_MS` (250).
|
|
76
|
+
*/
|
|
77
|
+
wakeDebounceMs?: number;
|
|
78
|
+
/**
|
|
79
|
+
* How often auto-wake reads the state of a long operation a parked turn's call
|
|
80
|
+
* waits on, in ms. Default `DEFAULT_TASK_POLL_MS` (5000).
|
|
81
|
+
*/
|
|
82
|
+
taskPollMs?: number;
|
|
83
|
+
}
|
|
84
|
+
/** @public Settings for the turn lease. Every field has a default. */
|
|
85
|
+
export interface TurnLeaseConfig {
|
|
86
|
+
/** This harness's name in the lease record. Default: a random id per harness. */
|
|
87
|
+
holder?: string;
|
|
88
|
+
/**
|
|
89
|
+
* How long a lease lasts between renewals, in ms. It is renewed every third of
|
|
90
|
+
* this while a turn runs; a harness that goes quiet for longer can be replaced.
|
|
91
|
+
* Default `DEFAULT_LEASE_TTL_MS` (30 s).
|
|
92
|
+
*/
|
|
93
|
+
ttlMs?: number;
|
|
94
|
+
/**
|
|
95
|
+
* How long a turn waits for a lease another harness holds, in ms, before it fails
|
|
96
|
+
* with `lease_lost`. A holder that died stops renewing, so waiting one `ttlMs`
|
|
97
|
+
* is enough to take over from it. Default: `ttlMs`.
|
|
98
|
+
*/
|
|
99
|
+
waitMs?: number;
|
|
100
|
+
/** The clock leases are timed by. Default `Date.now`. */
|
|
101
|
+
now?: () => number;
|
|
36
102
|
}
|
|
37
103
|
/**
|
|
38
104
|
* Execution-environment configuration for `AgentHarness`: the storage,
|
|
@@ -57,6 +123,15 @@ export interface AgentEnvironmentConfig {
|
|
|
57
123
|
/** `AgentHarness` constructor options. */
|
|
58
124
|
export interface AgentHarnessOpts<TParams extends Record<string, unknown> = Record<string, unknown>, TItem extends CustomItem = CustomItem> {
|
|
59
125
|
name: string;
|
|
126
|
+
/**
|
|
127
|
+
* Keep each thread's streamed output for display (spec 08 § Display output):
|
|
128
|
+
* `getSessionOutput()` then returns every item the thread streamed, unfinished
|
|
129
|
+
* ones included, from this harness or a later one over the same storage. A root
|
|
130
|
+
* context from `createContext()` also joins its thread, so `run()` on it streams
|
|
131
|
+
* like a turn. Display output is kept apart from checkpoints and is never
|
|
132
|
+
* resumed from. Default false.
|
|
133
|
+
*/
|
|
134
|
+
interactive?: boolean;
|
|
60
135
|
/**
|
|
61
136
|
* Root step tree executed for every turn submitted via `execute()`. The custom items
|
|
62
137
|
* it declares (a `workflow()` step's brand) type `getItemStream`, `getFullStream`,
|
|
@@ -113,6 +188,22 @@ export interface AgentHarnessOpts<TParams extends Record<string, unknown> = Reco
|
|
|
113
188
|
* the progress it missed.
|
|
114
189
|
*/
|
|
115
190
|
emitItemsOnReplay?: boolean;
|
|
191
|
+
/**
|
|
192
|
+
* The version of the flow this harness runs: any string the host changes when the
|
|
193
|
+
* flow changes, such as a release tag or a hash (spec 23 § Versions). Every step a
|
|
194
|
+
* run records carries it, so the trail shows which version handled each step. A
|
|
195
|
+
* turn resumed under another version keeps the steps that still match, and stops
|
|
196
|
+
* as stuck rather than redo one that had a lasting effect. A flow hydrated
|
|
197
|
+
* from a JSON workflow (`hydrateWorkflow`) defaults to a hash of its document, so
|
|
198
|
+
* editing the document is a new version. Any other flow without one behaves as
|
|
199
|
+
* before: a resumed turn discards whatever no longer matches.
|
|
200
|
+
*/
|
|
201
|
+
version?: string;
|
|
202
|
+
/**
|
|
203
|
+
* The clock wait deadlines, turn leases and wake times are read from. Default
|
|
204
|
+
* `Date.now`. A host that drives time itself (a test, a replay) passes its own.
|
|
205
|
+
*/
|
|
206
|
+
now?: () => number;
|
|
116
207
|
/**
|
|
117
208
|
* Approves or refuses calls to tools that declare `needsApproval`, after the
|
|
118
209
|
* `beforeToolCall` layers allow them. Without one, those calls run
|
|
@@ -55,6 +55,8 @@ export declare class LlmGateway {
|
|
|
55
55
|
* prices, and generation lookups.
|
|
56
56
|
*/
|
|
57
57
|
readonly sdkClient?: OpenRouterSdk;
|
|
58
|
+
/** Where native decisions go when the provider's base URL is not OpenRouter's. */
|
|
59
|
+
private readonly decisionsServerUrl?;
|
|
58
60
|
/** Model id → whether it answers natively as a decision model. */
|
|
59
61
|
private readonly decisionModels;
|
|
60
62
|
constructor(options: LlmGatewayOptions);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { frameworkCast } from '@noetic-tools/types';
|
|
2
|
-
import { callDecide } from './decide-call.js';
|
|
2
|
+
import { callDecide, decisionsServerUrl } from './decide-call.js';
|
|
3
3
|
import { HTTPClient, OpenRouter, OpenRouterSdk } from './deps/adapters.js';
|
|
4
4
|
import { someStep } from './deps/interpreter.js';
|
|
5
5
|
import { AgentHarnessModelCaller, noLlmProviderError } from './model-call.js';
|
|
@@ -134,8 +134,7 @@ function generationHook(spend) {
|
|
|
134
134
|
}
|
|
135
135
|
});
|
|
136
136
|
}
|
|
137
|
-
function createSdkClient(
|
|
138
|
-
const resolved = resolveLlmClient(config, readLlmEnv());
|
|
137
|
+
function createSdkClient(resolved, http) {
|
|
139
138
|
if (!resolved) {
|
|
140
139
|
return undefined;
|
|
141
140
|
}
|
|
@@ -164,6 +163,8 @@ export class LlmGateway {
|
|
|
164
163
|
* prices, and generation lookups.
|
|
165
164
|
*/
|
|
166
165
|
sdkClient;
|
|
166
|
+
/** Where native decisions go when the provider's base URL is not OpenRouter's. */
|
|
167
|
+
decisionsServerUrl;
|
|
167
168
|
/** Model id → whether it answers natively as a decision model. */
|
|
168
169
|
decisionModels = new Map();
|
|
169
170
|
constructor(options) {
|
|
@@ -172,9 +173,11 @@ export class LlmGateway {
|
|
|
172
173
|
this.client = options.callModelOverride
|
|
173
174
|
? undefined
|
|
174
175
|
: createClient(options.providerConfig, options.contextCache, http);
|
|
175
|
-
|
|
176
|
+
const resolved = options.callModelOverride
|
|
176
177
|
? undefined
|
|
177
|
-
:
|
|
178
|
+
: resolveLlmClient(options.providerConfig, readLlmEnv());
|
|
179
|
+
this.sdkClient = createSdkClient(resolved, http);
|
|
180
|
+
this.decisionsServerUrl = decisionsServerUrl(resolved?.serverURL);
|
|
178
181
|
}
|
|
179
182
|
callModel(request, host) {
|
|
180
183
|
return new AgentHarnessModelCaller({
|
|
@@ -194,6 +197,7 @@ export class LlmGateway {
|
|
|
194
197
|
}
|
|
195
198
|
return callDecide(request, {
|
|
196
199
|
client: this.sdkClient,
|
|
200
|
+
serverUrl: this.decisionsServerUrl,
|
|
197
201
|
callModel,
|
|
198
202
|
nativeModels: this.decisionModels,
|
|
199
203
|
spend: this.#options.spend,
|
|
@@ -317,6 +317,7 @@ export class AgentHarnessModelCaller {
|
|
|
317
317
|
spans,
|
|
318
318
|
modelSpan: null,
|
|
319
319
|
steers,
|
|
320
|
+
calls: roundLedger?.calls(round),
|
|
320
321
|
});
|
|
321
322
|
await recordRound(spend.keep());
|
|
322
323
|
}
|
|
@@ -528,6 +529,9 @@ export class AgentHarnessModelCaller {
|
|
|
528
529
|
});
|
|
529
530
|
allItems.push(...roundItems);
|
|
530
531
|
replayOutput(conversationInput, roundItems);
|
|
532
|
+
// Let the round's cut records land first: each one prunes what is beneath the
|
|
533
|
+
// round's key, which is where the calls about to run are recorded.
|
|
534
|
+
await checkpoints;
|
|
531
535
|
await runToolRound(this.toolRoundScope(), {
|
|
532
536
|
roundItems,
|
|
533
537
|
request,
|
|
@@ -538,6 +542,7 @@ export class AgentHarnessModelCaller {
|
|
|
538
542
|
spans,
|
|
539
543
|
modelSpan,
|
|
540
544
|
steers,
|
|
545
|
+
calls: roundLedger?.calls(round),
|
|
541
546
|
});
|
|
542
547
|
await recordRound(bill);
|
|
543
548
|
round += 1;
|
|
@@ -575,6 +580,8 @@ export class AgentHarnessModelCaller {
|
|
|
575
580
|
});
|
|
576
581
|
servedTier = sdkResponse.serviceTier ?? undefined;
|
|
577
582
|
allItems.push(...roundItems);
|
|
583
|
+
// As above: no cut record may land after a call of this round is recorded.
|
|
584
|
+
await checkpoints;
|
|
578
585
|
const ranTools = await runToolRound(this.toolRoundScope(), {
|
|
579
586
|
roundItems,
|
|
580
587
|
request,
|
|
@@ -585,6 +592,7 @@ export class AgentHarnessModelCaller {
|
|
|
585
592
|
spans,
|
|
586
593
|
modelSpan,
|
|
587
594
|
steers,
|
|
595
|
+
calls: roundLedger?.calls(round),
|
|
588
596
|
});
|
|
589
597
|
// A steer staged while the final answer streamed gets its own round, so
|
|
590
598
|
// the model answers it inside this call rather than in the next turn.
|
|
@@ -25,6 +25,30 @@
|
|
|
25
25
|
* more.
|
|
26
26
|
*/
|
|
27
27
|
import type { Item, ItemSchemaRegistry, RoundUsage } from '@noetic-tools/types';
|
|
28
|
+
import type { ToolCallResult } from '../runtime/tool-call.js';
|
|
29
|
+
/** A function call as the call ledger identifies it. */
|
|
30
|
+
interface CalledFunction {
|
|
31
|
+
callId: string;
|
|
32
|
+
name: string;
|
|
33
|
+
arguments: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The tool calls of one round, recorded one by one as they finish
|
|
37
|
+
* (spec 23a § Tool calls in a round).
|
|
38
|
+
*/
|
|
39
|
+
export interface CallLedger {
|
|
40
|
+
/**
|
|
41
|
+
* Where in the run this round is: the model step's path and the round's number.
|
|
42
|
+
* The same on a resume, and what tells two calls with one call id apart.
|
|
43
|
+
*/
|
|
44
|
+
readonly scope: string;
|
|
45
|
+
/** The recorded result of this call, when an earlier attempt of the round finished it. */
|
|
46
|
+
take(call: CalledFunction): ToolCallResult | undefined;
|
|
47
|
+
/** Record what the call produced. Resolves whether the record was stored. */
|
|
48
|
+
record(call: CalledFunction, result: ToolCallResult): Promise<boolean>;
|
|
49
|
+
/** Note that the call has a lasting effect: its record and its round's are marked so. */
|
|
50
|
+
noteEffect(call: CalledFunction): void;
|
|
51
|
+
}
|
|
28
52
|
/** What one finished round added to the loop. */
|
|
29
53
|
export interface RoundRecord {
|
|
30
54
|
/** Model output items and tool result items, in the order the loop gathered them. */
|
|
@@ -71,6 +95,11 @@ export declare class RoundLedger {
|
|
|
71
95
|
* stops at the first missing round, so the stale rounds past it are never read.
|
|
72
96
|
*/
|
|
73
97
|
record(round: number, logMark: number, record: RoundRecord): Promise<boolean>;
|
|
98
|
+
/**
|
|
99
|
+
* The call ledger of round `round`. Each call is recorded beneath the round's key,
|
|
100
|
+
* so the round's own record prunes them once it lands.
|
|
101
|
+
*/
|
|
102
|
+
calls(round: number): CallLedger;
|
|
74
103
|
/** Drop the record of round `round`: the loop threw that round away. */
|
|
75
104
|
drop(round: number): Promise<void>;
|
|
76
105
|
/** Note that the rounds this loop recorded were charged to its context. */
|
|
@@ -79,3 +108,4 @@ export declare class RoundLedger {
|
|
|
79
108
|
}
|
|
80
109
|
/** @internal The ledger key of round `round` of the model loop at step path `path`. */
|
|
81
110
|
export declare function roundPath(path: string, round: number): string;
|
|
111
|
+
export {};
|