@librechat/agents 4.0.6 → 4.1.1

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 (180) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +6 -4
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/decisions/breaker.cjs +59 -0
  4. package/dist/cjs/decisions/breaker.cjs.map +1 -0
  5. package/dist/cjs/decisions/core.cjs +44 -0
  6. package/dist/cjs/decisions/dialect.cjs +65 -12
  7. package/dist/cjs/decisions/dialect.cjs.map +1 -1
  8. package/dist/cjs/decisions/hook.cjs +19 -0
  9. package/dist/cjs/decisions/hook.cjs.map +1 -0
  10. package/dist/cjs/decisions/http.cjs +2 -1
  11. package/dist/cjs/decisions/http.cjs.map +1 -1
  12. package/dist/cjs/decisions/index.cjs +6 -0
  13. package/dist/cjs/decisions/node.cjs +315 -0
  14. package/dist/cjs/decisions/node.cjs.map +1 -0
  15. package/dist/cjs/decisions/probe.cjs +56 -0
  16. package/dist/cjs/decisions/probe.cjs.map +1 -0
  17. package/dist/cjs/decisions/redact.cjs +109 -0
  18. package/dist/cjs/decisions/redact.cjs.map +1 -0
  19. package/dist/cjs/decisions/structuredChat.cjs +2 -3
  20. package/dist/cjs/decisions/structuredChat.cjs.map +1 -1
  21. package/dist/cjs/decisions/transport.cjs +56 -23
  22. package/dist/cjs/decisions/transport.cjs.map +1 -1
  23. package/dist/cjs/decisions/types.cjs +2 -0
  24. package/dist/cjs/decisions/types.cjs.map +1 -1
  25. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -0
  26. package/dist/cjs/llm/bedrock/cachePoints.cjs +20 -0
  27. package/dist/cjs/llm/bedrock/cachePoints.cjs.map +1 -1
  28. package/dist/cjs/llm/bedrock/index.cjs +8 -5
  29. package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
  30. package/dist/cjs/main.cjs +45 -0
  31. package/dist/cjs/rerank/bm25.cjs +110 -0
  32. package/dist/cjs/rerank/bm25.cjs.map +1 -0
  33. package/dist/cjs/rerank/cache.cjs +50 -0
  34. package/dist/cjs/rerank/cache.cjs.map +1 -0
  35. package/dist/cjs/rerank/core.cjs +29 -0
  36. package/dist/cjs/rerank/crossEncoder.cjs +172 -0
  37. package/dist/cjs/rerank/crossEncoder.cjs.map +1 -0
  38. package/dist/cjs/rerank/fallback.cjs +280 -0
  39. package/dist/cjs/rerank/fallback.cjs.map +1 -0
  40. package/dist/cjs/rerank/index.cjs +8 -0
  41. package/dist/cjs/rerank/outcome.cjs +382 -0
  42. package/dist/cjs/rerank/outcome.cjs.map +1 -0
  43. package/dist/cjs/rerank/pin.cjs +74 -0
  44. package/dist/cjs/rerank/pin.cjs.map +1 -0
  45. package/dist/cjs/rerank/semaphore.cjs +38 -0
  46. package/dist/cjs/rerank/semaphore.cjs.map +1 -0
  47. package/dist/cjs/rerank/systemone.cjs +476 -50
  48. package/dist/cjs/rerank/systemone.cjs.map +1 -1
  49. package/dist/cjs/session/AgentSession.cjs +1 -1
  50. package/dist/cjs/session/AgentSession.cjs.map +1 -1
  51. package/dist/cjs/tools/local/LocalCodingTools.cjs +67 -19
  52. package/dist/cjs/tools/local/LocalCodingTools.cjs.map +1 -1
  53. package/dist/cjs/tools/local/LocalExecutionTools.cjs +12 -1
  54. package/dist/cjs/tools/local/LocalExecutionTools.cjs.map +1 -1
  55. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs +63 -16
  56. package/dist/cjs/tools/local/resolveLocalExecutionTools.cjs.map +1 -1
  57. package/dist/cjs/tools/local/toolResultCaps.cjs +32 -0
  58. package/dist/cjs/tools/local/toolResultCaps.cjs.map +1 -0
  59. package/dist/cjs/utils/schema.cjs +19 -0
  60. package/dist/cjs/utils/schema.cjs.map +1 -1
  61. package/dist/esm/agents/AgentContext.mjs +7 -5
  62. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  63. package/dist/esm/decisions/breaker.mjs +56 -0
  64. package/dist/esm/decisions/breaker.mjs.map +1 -0
  65. package/dist/esm/decisions/core.mjs +11 -0
  66. package/dist/esm/decisions/dialect.mjs +65 -12
  67. package/dist/esm/decisions/dialect.mjs.map +1 -1
  68. package/dist/esm/decisions/hook.mjs +19 -0
  69. package/dist/esm/decisions/hook.mjs.map +1 -0
  70. package/dist/esm/decisions/http.mjs +2 -1
  71. package/dist/esm/decisions/http.mjs.map +1 -1
  72. package/dist/esm/decisions/index.mjs +6 -0
  73. package/dist/esm/decisions/node.mjs +313 -0
  74. package/dist/esm/decisions/node.mjs.map +1 -0
  75. package/dist/esm/decisions/probe.mjs +54 -0
  76. package/dist/esm/decisions/probe.mjs.map +1 -0
  77. package/dist/esm/decisions/redact.mjs +108 -0
  78. package/dist/esm/decisions/redact.mjs.map +1 -0
  79. package/dist/esm/decisions/structuredChat.mjs +2 -3
  80. package/dist/esm/decisions/structuredChat.mjs.map +1 -1
  81. package/dist/esm/decisions/transport.mjs +56 -23
  82. package/dist/esm/decisions/transport.mjs.map +1 -1
  83. package/dist/esm/decisions/types.mjs +2 -0
  84. package/dist/esm/decisions/types.mjs.map +1 -1
  85. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -1
  86. package/dist/esm/llm/bedrock/cachePoints.mjs +20 -1
  87. package/dist/esm/llm/bedrock/cachePoints.mjs.map +1 -1
  88. package/dist/esm/llm/bedrock/index.mjs +7 -4
  89. package/dist/esm/llm/bedrock/index.mjs.map +1 -1
  90. package/dist/esm/main.mjs +15 -3
  91. package/dist/esm/rerank/bm25.mjs +108 -0
  92. package/dist/esm/rerank/bm25.mjs.map +1 -0
  93. package/dist/esm/rerank/cache.mjs +50 -0
  94. package/dist/esm/rerank/cache.mjs.map +1 -0
  95. package/dist/esm/rerank/core.mjs +9 -0
  96. package/dist/esm/rerank/crossEncoder.mjs +172 -0
  97. package/dist/esm/rerank/crossEncoder.mjs.map +1 -0
  98. package/dist/esm/rerank/fallback.mjs +278 -0
  99. package/dist/esm/rerank/fallback.mjs.map +1 -0
  100. package/dist/esm/rerank/index.mjs +8 -0
  101. package/dist/esm/rerank/outcome.mjs +360 -0
  102. package/dist/esm/rerank/outcome.mjs.map +1 -0
  103. package/dist/esm/rerank/pin.mjs +73 -0
  104. package/dist/esm/rerank/pin.mjs.map +1 -0
  105. package/dist/esm/rerank/semaphore.mjs +38 -0
  106. package/dist/esm/rerank/semaphore.mjs.map +1 -0
  107. package/dist/esm/rerank/systemone.mjs +476 -50
  108. package/dist/esm/rerank/systemone.mjs.map +1 -1
  109. package/dist/esm/session/AgentSession.mjs +1 -1
  110. package/dist/esm/session/AgentSession.mjs.map +1 -1
  111. package/dist/esm/tools/local/LocalCodingTools.mjs +67 -19
  112. package/dist/esm/tools/local/LocalCodingTools.mjs.map +1 -1
  113. package/dist/esm/tools/local/LocalExecutionTools.mjs +12 -1
  114. package/dist/esm/tools/local/LocalExecutionTools.mjs.map +1 -1
  115. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs +63 -17
  116. package/dist/esm/tools/local/resolveLocalExecutionTools.mjs.map +1 -1
  117. package/dist/esm/tools/local/toolResultCaps.mjs +31 -0
  118. package/dist/esm/tools/local/toolResultCaps.mjs.map +1 -0
  119. package/dist/esm/utils/schema.mjs +19 -0
  120. package/dist/esm/utils/schema.mjs.map +1 -1
  121. package/dist/types/decisions/breaker.d.ts +37 -0
  122. package/dist/types/decisions/core.d.ts +19 -0
  123. package/dist/types/decisions/hook.d.ts +9 -0
  124. package/dist/types/decisions/index.d.ts +3 -8
  125. package/dist/types/decisions/node.d.ts +90 -0
  126. package/dist/types/decisions/probe.d.ts +37 -0
  127. package/dist/types/decisions/redact.d.ts +22 -0
  128. package/dist/types/decisions/structuredChat.d.ts +1 -0
  129. package/dist/types/decisions/transport.d.ts +16 -3
  130. package/dist/types/decisions/types.d.ts +43 -4
  131. package/dist/types/llm/bedrock/cachePoints.d.ts +7 -0
  132. package/dist/types/llm/bedrock/index.d.ts +2 -1
  133. package/dist/types/rerank/bm25.d.ts +46 -0
  134. package/dist/types/rerank/cache.d.ts +40 -0
  135. package/dist/types/rerank/core.d.ts +24 -0
  136. package/dist/types/rerank/crossEncoder.d.ts +48 -0
  137. package/dist/types/rerank/fallback.d.ts +67 -0
  138. package/dist/types/rerank/index.d.ts +1 -3
  139. package/dist/types/rerank/outcome.d.ts +144 -0
  140. package/dist/types/rerank/pin.d.ts +48 -0
  141. package/dist/types/rerank/semaphore.d.ts +9 -0
  142. package/dist/types/rerank/systemone.d.ts +51 -5
  143. package/dist/types/rerank/types.d.ts +116 -1
  144. package/dist/types/tools/local/resolveLocalExecutionTools.d.ts +6 -0
  145. package/dist/types/tools/local/toolResultCaps.d.ts +16 -0
  146. package/dist/types/types/tools.d.ts +16 -0
  147. package/package.json +30 -2
  148. package/src/agents/AgentContext.ts +33 -4
  149. package/src/decisions/breaker.ts +110 -0
  150. package/src/decisions/core.ts +23 -0
  151. package/src/decisions/dialect.ts +134 -20
  152. package/src/decisions/hook.ts +32 -0
  153. package/src/decisions/http.ts +4 -1
  154. package/src/decisions/index.ts +3 -12
  155. package/src/decisions/node.ts +671 -0
  156. package/src/decisions/probe.ts +114 -0
  157. package/src/decisions/redact.ts +222 -0
  158. package/src/decisions/structuredChat.ts +6 -8
  159. package/src/decisions/transport.ts +104 -22
  160. package/src/decisions/types.ts +46 -5
  161. package/src/llm/bedrock/cachePoints.ts +47 -0
  162. package/src/llm/bedrock/index.ts +35 -13
  163. package/src/rerank/bm25.ts +190 -0
  164. package/src/rerank/cache.ts +103 -0
  165. package/src/rerank/core.ts +47 -0
  166. package/src/rerank/crossEncoder.ts +289 -0
  167. package/src/rerank/fallback.ts +625 -0
  168. package/src/rerank/index.ts +1 -3
  169. package/src/rerank/outcome.ts +767 -0
  170. package/src/rerank/pin.ts +157 -0
  171. package/src/rerank/semaphore.ts +47 -0
  172. package/src/rerank/systemone.ts +914 -88
  173. package/src/rerank/types.ts +143 -1
  174. package/src/session/AgentSession.ts +3 -1
  175. package/src/tools/local/LocalCodingTools.ts +139 -19
  176. package/src/tools/local/LocalExecutionTools.ts +21 -1
  177. package/src/tools/local/resolveLocalExecutionTools.ts +116 -11
  178. package/src/tools/local/toolResultCaps.ts +60 -0
  179. package/src/types/tools.ts +16 -0
  180. package/src/utils/schema.ts +29 -1
@@ -0,0 +1,110 @@
1
+ import type {
2
+ DecisionModel,
3
+ DecisionResult,
4
+ DecisionFailure,
5
+ DecisionRequest,
6
+ } from './types';
7
+ import { DecisionError } from './types';
8
+
9
+ /** Failures that belong to the credential, not the request: the next request would be refused the same way. */
10
+ export const FATAL_DECISION_FAILURES: ReadonlySet<DecisionFailure> =
11
+ new Set<DecisionFailure>(['unauthorized', 'quota']);
12
+
13
+ export function isFatalDecisionError(error: unknown): error is DecisionError {
14
+ return (
15
+ error instanceof DecisionError && FATAL_DECISION_FAILURES.has(error.failure)
16
+ );
17
+ }
18
+
19
+ /** A fuse: `open` once `trip` has recorded an error, until `reset`. */
20
+ export interface DecisionBreaker {
21
+ readonly open: boolean;
22
+ /** The first error that tripped the breaker, or `null` while it is closed. */
23
+ readonly error: DecisionError | null;
24
+ /** Counts resets; a call captures it before its request so a stale failure cannot reopen a reset breaker. */
25
+ readonly generation: number;
26
+ /**
27
+ * Records `error` unless the breaker is already open; the first error wins.
28
+ * With `generation`, records it only if no `reset` happened since that
29
+ * generation was read.
30
+ */
31
+ trip(error: DecisionError, generation?: number): void;
32
+ reset(): void;
33
+ }
34
+
35
+ export interface BreakerDecisionModel extends DecisionModel {
36
+ readonly breaker: DecisionBreaker;
37
+ }
38
+
39
+ export function createBreaker(): DecisionBreaker {
40
+ let tripped: DecisionError | null = null;
41
+ let generation = 0;
42
+ return {
43
+ get open(): boolean {
44
+ return tripped !== null;
45
+ },
46
+ get error(): DecisionError | null {
47
+ return tripped;
48
+ },
49
+ get generation(): number {
50
+ return generation;
51
+ },
52
+ trip(error: DecisionError, seen?: number): void {
53
+ if (seen !== undefined && seen !== generation) {
54
+ return;
55
+ }
56
+ tripped ??= error;
57
+ },
58
+ reset(): void {
59
+ tripped = null;
60
+ generation += 1;
61
+ },
62
+ };
63
+ }
64
+
65
+ /**
66
+ * The model, fused: after one `unauthorized` or `quota` failure every later
67
+ * call rejects at once, without a request, with the same failure, status, and
68
+ * `circuitOpen: true`, until `breaker.reset()`. Rate limits, server errors,
69
+ * timeouts, and network failures never trip it; the transport retries those.
70
+ * A fatal failure from a call that started before a `reset()` does not reopen
71
+ * it; a minter that shares one run between calls must not let a call made
72
+ * after a reset join a run from before it or reuse a token such a run minted
73
+ * (`commandToken` takes the breaker for this). Share one breaker between
74
+ * models that share a credential. A refusal sends nothing and reports no
75
+ * request; `reportsRequests` is kept.
76
+ */
77
+ export function withBreaker(
78
+ model: DecisionModel,
79
+ breaker: DecisionBreaker = createBreaker()
80
+ ): BreakerDecisionModel {
81
+ return {
82
+ id: model.id,
83
+ model: model.model,
84
+ ...(model.reportsRequests === true ? { reportsRequests: true } : {}),
85
+ breaker,
86
+ async decide(request: DecisionRequest): Promise<DecisionResult> {
87
+ const tripped = breaker.error;
88
+ if (tripped !== null) {
89
+ throw new DecisionError(
90
+ tripped.failure,
91
+ `decision model circuit open (${tripped.failure})`,
92
+ {
93
+ provider: tripped.provider,
94
+ status: tripped.status,
95
+ circuitOpen: true,
96
+ }
97
+ );
98
+ }
99
+ const generation = breaker.generation;
100
+ try {
101
+ return await model.decide(request);
102
+ } catch (error) {
103
+ if (isFatalDecisionError(error)) {
104
+ breaker.trip(error, generation);
105
+ }
106
+ throw error;
107
+ }
108
+ },
109
+ };
110
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Light `@librechat/agents/decisions` entry: the System One HTTP decision
3
+ * stack with no LangChain or other third-party runtime import. The
4
+ * structured-chat adapter stays on the root entry because it binds a
5
+ * LangChain chat model.
6
+ */
7
+ export * from './types';
8
+ export * from './questions';
9
+ export * from './presets';
10
+ export {
11
+ createHttpDecisionModel,
12
+ parseEnvelope,
13
+ HTTP_PROVIDER_ID,
14
+ } from './http';
15
+ export type { HttpDecisionModelOptions } from './http';
16
+ export { toWireQuestion, readAnswer } from './dialect';
17
+ export { createTransport, failureForStatus, retryAfterMs } from './transport';
18
+ export type { DecisionFetch, Transport, TransportOptions } from './transport';
19
+ export { withDecisionDeadline } from './deadline';
20
+ export type { AwaitWithinDeadline } from './deadline';
21
+ export * from './breaker';
22
+ export * from './probe';
23
+ export { redact } from './redact';
@@ -5,6 +5,115 @@ import type {
5
5
  } from './types';
6
6
  import { isDecisionObject } from './types';
7
7
 
8
+ /**
9
+ * Hosts in front of System One (ClickHouse gateway, OpenRouter) round every reported number to
10
+ * two decimals, so each value can sit up to half a hundredth from the measured one, and never
11
+ * outside `[0, 1]`. A `systemone` answer whose probabilities and score all sit on that grid is read
12
+ * under this rounding model: its distribution must be one that some measured distribution summing
13
+ * to exactly one rounds to, and its score must round from the expectation of such a distribution.
14
+ * The model is chosen once per answer, so a single off-grid number puts the whole answer, score
15
+ * and distribution alike, under the four-decimal bounds, as it does every `port` answer. `FLOAT_SLACK` absorbs
16
+ * binary floating-point error at the exact bounds; anything further out is an inconsistent answer.
17
+ */
18
+ const REPORTED_HALF_STEP = 0.005;
19
+ const REPORTED_STEPS = Math.round(1 / (2 * REPORTED_HALF_STEP));
20
+ const FLOAT_SLACK = 1e-9;
21
+
22
+ /** Whether two-decimal rounding could have produced this number. */
23
+ function isTwoDecimal(value: number): boolean {
24
+ const steps = value * REPORTED_STEPS;
25
+ return Math.abs(steps - Math.round(steps)) <= FLOAT_SLACK * REPORTED_STEPS;
26
+ }
27
+
28
+ /** The measured value a rounded one can come from: within a half-step, and inside `[0, 1]`. */
29
+ function measuredRange(value: number): [number, number] {
30
+ return [
31
+ Math.max(0, value - REPORTED_HALF_STEP),
32
+ Math.min(1, value + REPORTED_HALF_STEP),
33
+ ];
34
+ }
35
+
36
+ /** Whether a measured distribution summing to exactly one could round to these values. */
37
+ function roundsFromUnitSum(values: readonly number[]): boolean {
38
+ let lowest = 0;
39
+ let highest = 0;
40
+ for (const value of values) {
41
+ const [low, high] = measuredRange(value);
42
+ lowest += low;
43
+ highest += high;
44
+ }
45
+ return lowest <= 1 + FLOAT_SLACK && highest >= 1 - FLOAT_SLACK;
46
+ }
47
+
48
+ /** The four-decimal bound on how far a distribution may sum from one. */
49
+ function sumsToOne(values: readonly number[]): boolean {
50
+ const total = values.reduce((sum, value) => sum + value, 0);
51
+ const tolerance = Math.min(
52
+ 0.02,
53
+ Math.max(0.01, values.length * 0.00005 + 0.001)
54
+ );
55
+ return Math.abs(total - 1) <= tolerance;
56
+ }
57
+
58
+ /**
59
+ * The lowest and highest expectation of a measured distribution summing to one that rounds to
60
+ * `byLevel` (indexed by level): every level starts at its lowest measured value, and the mass left
61
+ * to reach one fills the lowest levels first for the minimum and the highest first for the maximum.
62
+ */
63
+ function expectationRange(byLevel: readonly number[]): [number, number] {
64
+ const ranges = byLevel.map(measuredRange);
65
+ let base = 0;
66
+ let spare = 1;
67
+ for (const [level, [low]] of ranges.entries()) {
68
+ base += level * low;
69
+ spare -= low;
70
+ }
71
+ const fill = (levels: readonly number[]): number => {
72
+ let expectation = base;
73
+ let left = Math.max(0, spare);
74
+ for (const level of levels) {
75
+ if (left <= 0) {
76
+ break;
77
+ }
78
+ const [low, high] = ranges[level];
79
+ const added = Math.min(high - low, left);
80
+ expectation += level * added;
81
+ left -= added;
82
+ }
83
+ return expectation;
84
+ };
85
+ const ascending = ranges.map((_, level) => level);
86
+ return [fill(ascending), fill([...ascending].reverse())];
87
+ }
88
+
89
+ /** Whether a two-decimal score rounds from the expectation of a distribution behind `byLevel`. */
90
+ function roundsFromExpectation(
91
+ score: number,
92
+ byLevel: readonly number[]
93
+ ): boolean {
94
+ const [lowest, highest] = expectationRange(byLevel);
95
+ return (
96
+ score >= lowest - REPORTED_HALF_STEP - FLOAT_SLACK &&
97
+ score <= highest + REPORTED_HALF_STEP + FLOAT_SLACK
98
+ );
99
+ }
100
+
101
+ /** The four-decimal bound on how far a score may sit from its distribution's expectation. */
102
+ function nearExpectation(score: number, byLevel: readonly number[]): boolean {
103
+ const levels = byLevel.length;
104
+ const expectation = byLevel.reduce(
105
+ (sum, probability, level) => sum + level * probability,
106
+ 0
107
+ );
108
+ const tolerance = Math.min(
109
+ 0.02,
110
+ 0.00005 * (1 + (levels * (levels - 1)) / 2) + 0.001
111
+ );
112
+ return (
113
+ Number.isFinite(expectation) && Math.abs(score - expectation) <= tolerance
114
+ );
115
+ }
116
+
8
117
  interface WireQuestion {
9
118
  type: string;
10
119
  instructions: DecisionQuestion['instructions'];
@@ -49,7 +158,10 @@ function isProbability(value: unknown): value is number {
49
158
  );
50
159
  }
51
160
 
52
- /** `undefined` means malformed; `null` means no distribution was reported. */
161
+ /**
162
+ * `undefined` means malformed; `null` means no distribution was reported. Whether the values sum
163
+ * to one depends on the rounding model of the whole answer, so `readAnswer` checks that.
164
+ */
53
165
  function probabilitiesOf(
54
166
  raw: unknown,
55
167
  answerType: 'choice' | 'score',
@@ -74,7 +186,6 @@ function probabilitiesOf(
74
186
  return undefined;
75
187
  }
76
188
  const probabilities: Record<string, number> = Object.create(null);
77
- let total = 0;
78
189
  let maximum = 0;
79
190
  for (const [key, value] of entries) {
80
191
  if (!isProbability(value)) {
@@ -90,7 +201,6 @@ function probabilitiesOf(
90
201
  return undefined;
91
202
  }
92
203
  probabilities[key] = value;
93
- total += value;
94
204
  maximum = Math.max(maximum, value);
95
205
  }
96
206
  if (
@@ -100,11 +210,7 @@ function probabilitiesOf(
100
210
  ) {
101
211
  return undefined;
102
212
  }
103
- const roundingTolerance = Math.min(
104
- 0.02,
105
- Math.max(0.01, entries.length * 0.00005 + 0.001)
106
- );
107
- return Math.abs(total - 1) <= roundingTolerance ? probabilities : undefined;
213
+ return probabilities;
108
214
  }
109
215
 
110
216
  /** A validated answer, or `null` for one that cannot safely be read. */
@@ -147,6 +253,19 @@ export function readAnswer(
147
253
  if (probabilities === undefined) {
148
254
  return null;
149
255
  }
256
+ const values = probabilities === null ? [] : Object.values(probabilities);
257
+ /** One rounding model for the answer: its score and its distribution are read under it together. */
258
+ const rounded =
259
+ dialect === 'systemone' &&
260
+ values.every(isTwoDecimal) &&
261
+ (record.type !== 'score' ||
262
+ (typeof record.score === 'number' && isTwoDecimal(record.score)));
263
+ if (
264
+ probabilities !== null &&
265
+ !(rounded ? roundsFromUnitSum(values) : sumsToOne(values))
266
+ ) {
267
+ return null;
268
+ }
150
269
  if (record.type === 'choice') {
151
270
  if (
152
271
  typeof record.choice !== 'string' ||
@@ -174,20 +293,15 @@ export function readAnswer(
174
293
  return null;
175
294
  }
176
295
  if (probabilities !== null) {
177
- let weightedScore = 0;
178
- let levels = 0;
296
+ /** Score keys are the consecutive levels `0..n-1`, checked by `probabilitiesOf`. */
297
+ const byLevel: number[] = [];
179
298
  for (const [level, probability] of Object.entries(probabilities)) {
180
- weightedScore += Number(level) * probability;
181
- levels += 1;
299
+ byLevel[Number(level)] = probability;
182
300
  }
183
- const roundingTolerance = Math.min(
184
- 0.02,
185
- 0.00005 * (1 + (levels * (levels - 1)) / 2) + 0.001
186
- );
187
- if (
188
- !Number.isFinite(weightedScore) ||
189
- Math.abs(record.score - weightedScore) > roundingTolerance
190
- ) {
301
+ const consistent = rounded
302
+ ? roundsFromExpectation(record.score, byLevel)
303
+ : nearExpectation(record.score, byLevel);
304
+ if (!consistent) {
191
305
  return null;
192
306
  }
193
307
  }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Calls a caller-supplied hook whose failure must not reach the caller's
3
+ * work: a synchronous throw is dropped, and so is the rejection of a returned
4
+ * promise, which a `void`-typed hook may still produce (TypeScript accepts an
5
+ * `async` function there) and which would otherwise be unhandled. Returns the
6
+ * hook's synchronous result, or `undefined` when it threw or returned a
7
+ * promise; nothing waits for that promise.
8
+ */
9
+ export function callHook(hook: () => unknown): unknown {
10
+ let result: unknown;
11
+ try {
12
+ result = hook();
13
+ } catch {
14
+ return undefined;
15
+ }
16
+ if (
17
+ result === null ||
18
+ (typeof result !== 'object' && typeof result !== 'function')
19
+ ) {
20
+ return result;
21
+ }
22
+ try {
23
+ const then: unknown = (result as { then?: unknown }).then;
24
+ if (typeof then !== 'function') {
25
+ return result;
26
+ }
27
+ then.call(result, undefined, () => {});
28
+ } catch {
29
+ // A thenable whose `then` throws is as unusable as a rejection.
30
+ }
31
+ return undefined;
32
+ }
@@ -119,6 +119,7 @@ export function createHttpDecisionModel(
119
119
  return {
120
120
  id: providerId,
121
121
  model,
122
+ reportsRequests: true,
122
123
  async decide(request: DecisionRequest): Promise<DecisionResult> {
123
124
  return send(
124
125
  () => {
@@ -168,7 +169,9 @@ export function createHttpDecisionModel(
168
169
  },
169
170
  request.signal,
170
171
  request.label ?? 'decide',
171
- request.timeoutMs
172
+ request.timeoutMs,
173
+ request.onRequest,
174
+ request.onResponse
172
175
  );
173
176
  },
174
177
  };
@@ -1,14 +1,5 @@
1
- export * from './types';
2
- export * from './questions';
3
- export * from './presets';
4
- export {
5
- createHttpDecisionModel,
6
- parseEnvelope,
7
- HTTP_PROVIDER_ID,
8
- } from './http';
1
+ export * from './core';
9
2
  export { createStructuredChatDecisionModel } from './structuredChat';
10
- export type { HttpDecisionModelOptions } from './http';
11
3
  export type { StructuredChatDecisionModelOptions } from './structuredChat';
12
- export { toWireQuestion, readAnswer } from './dialect';
13
- export { createTransport } from './transport';
14
- export type { DecisionFetch, Transport, TransportOptions } from './transport';
4
+ export { commandToken, COMMAND_TOKEN_PROVIDER } from './node';
5
+ export type { CommandTokenOptions } from './node';