@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.
Files changed (45) hide show
  1. package/dist/adapters/openrouter.d.ts +7 -0
  2. package/dist/adapters/openrouter.js +23 -0
  3. package/dist/conditions/conditions.js +3 -0
  4. package/dist/harness/agent-harness.d.ts +63 -9
  5. package/dist/harness/agent-harness.js +222 -22
  6. package/dist/harness/call-spend.d.ts +34 -0
  7. package/dist/harness/call-spend.js +125 -0
  8. package/dist/harness/decide-call.d.ts +3 -0
  9. package/dist/harness/decide-call.js +51 -12
  10. package/dist/harness/deps/adapters.d.ts +1 -1
  11. package/dist/harness/deps/adapters.js +1 -1
  12. package/dist/harness/deps/runtime.d.ts +3 -1
  13. package/dist/harness/deps/runtime.js +3 -1
  14. package/dist/harness/deps/types.d.ts +1 -1
  15. package/dist/harness/model-call.d.ts +3 -0
  16. package/dist/harness/model-call.js +123 -66
  17. package/dist/harness/model-price.d.ts +51 -0
  18. package/dist/harness/model-price.js +158 -0
  19. package/dist/harness/model-rounds.d.ts +13 -0
  20. package/dist/harness/model-rounds.js +42 -2
  21. package/dist/harness/spend.d.ts +144 -0
  22. package/dist/harness/spend.js +534 -0
  23. package/dist/index.d.ts +5 -1
  24. package/dist/index.js +2 -0
  25. package/dist/interpreter/execute-action.js +2 -0
  26. package/dist/interpreter/execute-control.js +1 -0
  27. package/dist/interpreter/execute-decide.js +43 -1
  28. package/dist/interpreter/execute.js +9 -0
  29. package/dist/runtime/context-impl.d.ts +1 -0
  30. package/dist/runtime/context-impl.js +1 -0
  31. package/dist/runtime/drain.d.ts +68 -0
  32. package/dist/runtime/drain.js +280 -0
  33. package/dist/runtime/durable/spend-log.d.ts +58 -0
  34. package/dist/runtime/durable/spend-log.js +133 -0
  35. package/dist/runtime/durable/step-ledger.d.ts +9 -2
  36. package/dist/runtime/durable/step-ledger.js +9 -6
  37. package/dist/runtime/session-runner.d.ts +5 -3
  38. package/dist/runtime/session-runner.js +9 -4
  39. package/dist/until/predicates.d.ts +5 -1
  40. package/dist/until/predicates.js +12 -7
  41. package/dist/util/spent.d.ts +14 -0
  42. package/dist/util/spent.js +67 -0
  43. package/dist/util/status-code.d.ts +5 -0
  44. package/dist/util/status-code.js +8 -0
  45. package/package.json +2 -2
@@ -16,7 +16,7 @@ function mergeInputsToItems(messages) {
16
16
  /**
17
17
  * @internal
18
18
  *
19
- * Per-thread runner. Drains the MessageQueue and drives turns on the shared
19
+ * Per-thread runner. Works through the MessageQueue and drives turns on the shared
20
20
  * session broadcaster. Emits `turn_started` / `turn_completed` /
21
21
  * `inbox_injected` framework events so consumers can correlate queued
22
22
  * messages with the turn that delivered them.
@@ -67,6 +67,10 @@ export class SessionRunner {
67
67
  getStatus() {
68
68
  return this.status;
69
69
  }
70
+ /** The running turn's context, if a turn is running. */
71
+ get context() {
72
+ return this.currentCtx;
73
+ }
70
74
  /** Queue a message. A steer that arrives mid-turn is announced first, so
71
75
  * `steer_accepted` always precedes the `inbox_injected` that applies it. */
72
76
  enqueue(message) {
@@ -144,7 +148,7 @@ export class SessionRunner {
144
148
  // observe it via the broadcaster / lastError, not the abort() return.
145
149
  });
146
150
  }
147
- /** Start the drain loop if not already running. Safe to call many times. */
151
+ /** Start the queue loop if not already running. Safe to call many times. */
148
152
  kick() {
149
153
  if (this.loopPromise) {
150
154
  return;
@@ -152,7 +156,7 @@ export class SessionRunner {
152
156
  if (this.queue.size === 0) {
153
157
  return;
154
158
  }
155
- this.loopPromise = this.drain().finally(() => {
159
+ this.loopPromise = this.runQueue().finally(() => {
156
160
  this.loopPromise = undefined;
157
161
  // If messages arrived during finally, run again.
158
162
  if (this.queue.size > 0) {
@@ -160,7 +164,7 @@ export class SessionRunner {
160
164
  }
161
165
  });
162
166
  }
163
- async drain() {
167
+ async runQueue() {
164
168
  while (this.queue.size > 0) {
165
169
  const messages = this.queue.drainAll();
166
170
  if (messages.length === 0) {
@@ -355,6 +359,7 @@ function buildResponse(text, ctx) {
355
359
  },
356
360
  cost: ctx.cost > 0 ? ctx.cost : undefined,
357
361
  billedCost: ctx.billedCost > 0 ? ctx.billedCost : undefined,
362
+ heldCost: ctx.heldCost > 0 ? ctx.heldCost : undefined,
358
363
  text,
359
364
  lastLayerUsage: ctx.lastLayerUsage,
360
365
  };
@@ -24,7 +24,11 @@ export interface ConvergeConfig {
24
24
  export declare const until: {
25
25
  /** Stops the loop after `n` steps. @public */
26
26
  maxSteps(n: number): Until;
27
- /** Stops the loop when accumulated cost reaches `usd`. @public */
27
+ /**
28
+ * Stops the loop when accumulated cost, plus spend held for cut-off calls with no
29
+ * settled bill, reaches `usd`. Counting the hold keeps a resumed run from spending
30
+ * what its cut-off attempt may already have spent. @public
31
+ */
28
32
  maxCost(usd: number): Until;
29
33
  /** Stops the loop when elapsed time reaches `ms` milliseconds. @public */
30
34
  maxDuration(ms: number): Until;
@@ -14,14 +14,19 @@ export const until = {
14
14
  reason: snap.stepCount >= n ? `Reached max steps (${n})` : undefined,
15
15
  });
16
16
  },
17
- /** Stops the loop when accumulated cost reaches `usd`. @public */
17
+ /**
18
+ * Stops the loop when accumulated cost, plus spend held for cut-off calls with no
19
+ * settled bill, reaches `usd`. Counting the hold keeps a resumed run from spending
20
+ * what its cut-off attempt may already have spent. @public
21
+ */
18
22
  maxCost(usd) {
19
- return (snap) => ({
20
- stop: snap.cost >= usd,
21
- reason: snap.cost >= usd
22
- ? `Cost $${snap.cost.toFixed(2)} exceeded budget $${usd.toFixed(2)}`
23
- : undefined,
24
- });
23
+ return (snap) => {
24
+ const spent = snap.cost + (snap.heldCost ?? 0);
25
+ return {
26
+ stop: spent >= usd,
27
+ reason: spent >= usd ? `Cost $${spent.toFixed(2)} exceeded budget $${usd.toFixed(2)}` : undefined,
28
+ };
29
+ };
25
30
  },
26
31
  /** Stops the loop when elapsed time reaches `ms` milliseconds. @public */
27
32
  maxDuration(ms) {
@@ -0,0 +1,14 @@
1
+ import type { Context, UsageCharge } from '@noetic-tools/types';
2
+ /**
3
+ * Tie `charge` to `error` and return the error to throw. A charge the error already
4
+ * carries — from a call nested inside this one that nobody charged — is added to.
5
+ * A thrown value that is not an object is wrapped in an `Error` to carry the charge;
6
+ * one that spent nothing is returned as is.
7
+ */
8
+ export declare function markSpent(error: unknown, charge: UsageCharge, onCharge?: () => void): unknown;
9
+ /**
10
+ * Charge `ctx` with what the call behind `error` spent, and return that charge. The
11
+ * mark is dropped once charged, so an error rethrown through an outer call is charged
12
+ * only by that call.
13
+ */
14
+ export declare function chargeSpent(ctx: Context, error: unknown): UsageCharge | undefined;
@@ -0,0 +1,67 @@
1
+ import { recordUsage } from '@noetic-tools/types';
2
+ /**
3
+ * What a call had spent when it threw. A model call that fails after the provider
4
+ * billed some of its rounds returns no `LLMResponse`, so the bill rides on the error
5
+ * and the caller charges it. Kept beside the error rather than on it, so a provider
6
+ * error keeps its own shape.
7
+ */
8
+ const spends = new WeakMap();
9
+ /**
10
+ * Tie `charge` to `error` and return the error to throw. A charge the error already
11
+ * carries — from a call nested inside this one that nobody charged — is added to.
12
+ * A thrown value that is not an object is wrapped in an `Error` to carry the charge;
13
+ * one that spent nothing is returned as is.
14
+ */
15
+ export function markSpent(error, charge, onCharge) {
16
+ if (isEmpty(charge)) {
17
+ return error;
18
+ }
19
+ const thrown = typeof error === 'object' && error !== null ? error : new Error(String(error));
20
+ const prior = spends.get(thrown);
21
+ spends.set(thrown, {
22
+ charge: prior ? addCharges(prior.charge, charge) : charge,
23
+ onCharge: () => {
24
+ prior?.onCharge?.();
25
+ onCharge?.();
26
+ },
27
+ });
28
+ return thrown;
29
+ }
30
+ /**
31
+ * Charge `ctx` with what the call behind `error` spent, and return that charge. The
32
+ * mark is dropped once charged, so an error rethrown through an outer call is charged
33
+ * only by that call.
34
+ */
35
+ export function chargeSpent(ctx, error) {
36
+ if (typeof error !== 'object' || error === null) {
37
+ return undefined;
38
+ }
39
+ const spent = spends.get(error);
40
+ if (!spent) {
41
+ return undefined;
42
+ }
43
+ spends.delete(error);
44
+ recordUsage(ctx, spent.charge);
45
+ spent.onCharge?.();
46
+ return spent.charge;
47
+ }
48
+ function isEmpty(charge) {
49
+ return (charge.inputTokens === 0 &&
50
+ charge.outputTokens === 0 &&
51
+ !charge.cachedTokens &&
52
+ !charge.cost &&
53
+ !charge.billedCost);
54
+ }
55
+ function addCharges(a, b) {
56
+ return {
57
+ inputTokens: a.inputTokens + b.inputTokens,
58
+ outputTokens: a.outputTokens + b.outputTokens,
59
+ ...(a.cachedTokens !== undefined || b.cachedTokens !== undefined
60
+ ? {
61
+ cachedTokens: (a.cachedTokens ?? 0) + (b.cachedTokens ?? 0),
62
+ }
63
+ : {}),
64
+ cost: (a.cost ?? 0) + (b.cost ?? 0),
65
+ billedCost: (a.billedCost ?? a.cost ?? 0) + (b.billedCost ?? b.cost ?? 0),
66
+ };
67
+ }
@@ -1,2 +1,7 @@
1
1
  /** The HTTP status an SDK error carries, when it carries one. */
2
2
  export declare function statusOf(e: unknown): number | undefined;
3
+ /**
4
+ * Whether the provider answered with an error status, so it generated and billed
5
+ * nothing. A 2xx whose body failed to parse also carries a status, but was billed.
6
+ */
7
+ export declare function isErrorStatus(e: unknown): boolean;
@@ -5,3 +5,11 @@ export function statusOf(e) {
5
5
  }
6
6
  return typeof e.statusCode === 'number' ? e.statusCode : undefined;
7
7
  }
8
+ /**
9
+ * Whether the provider answered with an error status, so it generated and billed
10
+ * nothing. A 2xx whose body failed to parse also carries a status, but was billed.
11
+ */
12
+ export function isErrorStatus(e) {
13
+ const status = statusOf(e);
14
+ return status !== undefined && status >= 400;
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noetic-tools/core",
3
- "version": "5.17.0",
3
+ "version": "5.19.0",
4
4
  "type": "module",
5
5
  "description": "Step primitives, interpreter, runtime, and context layer contract for Noetic agents.",
6
6
  "license": "Apache-2.0",
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "dependencies": {
71
71
  "@noetic-tools/context": "^1.11.0",
72
- "@noetic-tools/types": "^3.15.0",
72
+ "@noetic-tools/types": "^3.17.0",
73
73
  "@openrouter/agent": "^0.11.0",
74
74
  "@openrouter/sdk": "1.4.25",
75
75
  "jsonata": "2.2.2",