@gobing-ai/ts-ai-runner 0.8.9 → 0.9.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.
@@ -1,110 +0,0 @@
1
- import { DecisionBackendError, DecisionRequestError } from './errors';
2
- import type { Question } from './types';
3
-
4
- /** Probability-mass tolerance: |total - 1| at or below this counts as a normalized distribution. */
5
- const DISTRIBUTION_MASS_TOLERANCE = 1e-6;
6
-
7
- function record(value: unknown): value is Record<string, unknown> {
8
- return value !== null && typeof value === 'object' && !Array.isArray(value);
9
- }
10
-
11
- function json(value: unknown, ancestors = new Set<unknown>()): boolean {
12
- if (value === null || typeof value === 'string' || typeof value === 'boolean') return true;
13
- if (typeof value === 'number') return Number.isFinite(value);
14
- if (typeof value !== 'object' || ancestors.has(value)) return false;
15
- ancestors.add(value);
16
- const valid = Object.values(value).every((entry) => json(entry, ancestors));
17
- ancestors.delete(value);
18
- return valid;
19
- }
20
-
21
- function desc(value: unknown): boolean {
22
- return (value === null || typeof value === 'string' || typeof value === 'object') && json(value);
23
- }
24
-
25
- /** Validate caller data before either a custom driver or the SDK consumes it. */
26
- export function validateQuestions(questions: unknown): asserts questions is Record<string, Question> {
27
- const fail: (name: string) => never = (name) => {
28
- throw new DecisionRequestError(`Invalid decision question: ${name}`, undefined, undefined);
29
- };
30
- if (!record(questions) || Object.keys(questions).length === 0) fail('expected a nonempty question map');
31
- for (const [name, question] of Object.entries(questions)) {
32
- if (!record(question)) fail(name);
33
- if (question.prompt !== undefined && !desc(question.prompt)) fail(name);
34
- switch (question.kind) {
35
- case 'choice':
36
- if (
37
- !record(question.labels) ||
38
- Object.keys(question.labels).length === 0 ||
39
- !Object.values(question.labels).every(desc)
40
- )
41
- fail(name);
42
- break;
43
- case 'score':
44
- if (
45
- !Array.isArray(question.rubric) ||
46
- question.rubric.length < 2 ||
47
- !Array.from(question.rubric).every(desc)
48
- )
49
- fail(name);
50
- break;
51
- case 'noul':
52
- if (
53
- (question.yes !== undefined && !desc(question.yes)) ||
54
- (question.no !== undefined && !desc(question.no))
55
- )
56
- fail(name);
57
- break;
58
- default:
59
- fail(name);
60
- }
61
- }
62
- }
63
-
64
- function bounded(value: unknown, max = 1): boolean {
65
- return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= max;
66
- }
67
-
68
- function keysMatch(value: unknown, keys: string[]): value is Record<string, unknown> {
69
- return record(value) && Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
70
- }
71
-
72
- /** Verify the correspondence that permits the facade's typed answer narrowing. */
73
- export function validateAnswers(questions: Record<string, Question>, answers: unknown): void {
74
- const fail: (name: string) => never = (name) => {
75
- throw new DecisionBackendError(`Invalid decision response: ${name}`, undefined);
76
- };
77
- if (!keysMatch(answers, Object.keys(questions))) fail('answer names do not match questions');
78
- for (const [name, question] of Object.entries(questions)) {
79
- const answer = answers[name];
80
- if (!record(answer) || answer.kind !== question.kind) fail(name);
81
- if (question.kind === 'noul') {
82
- if (!bounded(answer.probability)) fail(name);
83
- continue;
84
- }
85
- const keys =
86
- question.kind === 'choice' ? Object.keys(question.labels) : Array.from(question.rubric.keys(), String);
87
- if (
88
- !bounded(answer.confidence) ||
89
- !keysMatch(answer.probabilities, keys) ||
90
- !Object.values(answer.probabilities).every((p) => bounded(p))
91
- )
92
- fail(name);
93
- if (question.kind === 'choice') {
94
- if (typeof answer.label !== 'string' || !Object.hasOwn(question.labels, answer.label)) fail(name);
95
- } else if (
96
- !bounded(answer.score, question.rubric.length - 1) ||
97
- !keysMatch(answer.legend, keys) ||
98
- !Object.values(answer.legend).every(desc)
99
- ) {
100
- fail(name);
101
- }
102
- // Placed after the per-entry checks so their errors keep precedence; every entry is a
103
- // bounded number here. Inclusive at exactly 1e-6.
104
- const total = Object.values(answer.probabilities as Record<string, unknown>).reduce<number>(
105
- (sum, p) => sum + (p as number),
106
- 0,
107
- );
108
- if (Math.abs(total - 1) > DISTRIBUTION_MASS_TOLERANCE) fail(name);
109
- }
110
- }