@gobing-ai/ts-ai-runner 0.5.0 → 0.5.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.
- package/README.md +114 -0
- package/dist/decision/decision-maker.d.ts +45 -0
- package/dist/decision/decision-maker.d.ts.map +1 -0
- package/dist/decision/decision-maker.js +67 -0
- package/dist/decision/errors.d.ts +62 -0
- package/dist/decision/errors.d.ts.map +1 -0
- package/dist/decision/errors.js +70 -0
- package/dist/decision/types.d.ts +105 -0
- package/dist/decision/types.d.ts.map +1 -0
- package/dist/decision/types.js +34 -0
- package/dist/decision/typesafe-driver.d.ts +24 -0
- package/dist/decision/typesafe-driver.d.ts.map +1 -0
- package/dist/decision/typesafe-driver.js +134 -0
- package/dist/decision/validation.d.ts +6 -0
- package/dist/decision/validation.d.ts.map +1 -0
- package/dist/decision/validation.js +92 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/package.json +5 -4
- package/src/decision/decision-maker.ts +129 -0
- package/src/decision/errors.ts +88 -0
- package/src/decision/types.ts +111 -0
- package/src/decision/typesafe-driver.ts +175 -0
- package/src/decision/validation.ts +100 -0
- package/src/index.ts +3 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The TypeSafe backend driver — the only decision file that imports
|
|
3
|
+
* `@typesafe-ai/sdk` (0069 pin). Client wiring (R1/R2), neutral⇄SDK question
|
|
4
|
+
* and answer mapping (R4/R5), and the error translation table (R7) live here;
|
|
5
|
+
* everything above this file stays vendor-neutral.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { Question as SdkQuestion, ResultFor as SdkResultFor, SystemOneResult } from '@typesafe-ai/sdk';
|
|
9
|
+
import {
|
|
10
|
+
APIConnectionError,
|
|
11
|
+
APIError,
|
|
12
|
+
APITimeoutError,
|
|
13
|
+
AuthenticationError,
|
|
14
|
+
choice,
|
|
15
|
+
noul,
|
|
16
|
+
PermissionDeniedError,
|
|
17
|
+
RateLimitError,
|
|
18
|
+
score,
|
|
19
|
+
TypeSafeClient,
|
|
20
|
+
TypeSafeError,
|
|
21
|
+
} from '@typesafe-ai/sdk';
|
|
22
|
+
import {
|
|
23
|
+
DecisionAuthError,
|
|
24
|
+
DecisionBackendError,
|
|
25
|
+
DecisionConnectionError,
|
|
26
|
+
DecisionRateLimitError,
|
|
27
|
+
DecisionRequestError,
|
|
28
|
+
DecisionTimeoutError,
|
|
29
|
+
} from './errors';
|
|
30
|
+
import type { Answer, DecisionDriver, Question } from './types';
|
|
31
|
+
import { validateAnswers, validateQuestions } from './validation';
|
|
32
|
+
|
|
33
|
+
/** Configuration the facade resolves (key per R7) and forwards to the TypeSafe driver. */
|
|
34
|
+
export interface TypesafeDriverConfig {
|
|
35
|
+
apiKey: string;
|
|
36
|
+
model?: string;
|
|
37
|
+
baseURL?: string;
|
|
38
|
+
timeoutMs?: number;
|
|
39
|
+
maxRetries?: number;
|
|
40
|
+
fetch?: typeof fetch;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Build the TypeSafe driver: exactly one `TypeSafeClient` per driver instance
|
|
45
|
+
* (R2), the API key always passed explicitly so the SDK's `TYPESAFE_API_KEY`
|
|
46
|
+
* self-resolution never runs (R1). All transport failures — at construction
|
|
47
|
+
* or at ask time — come back as `DecisionError`s, never SDK classes (R7).
|
|
48
|
+
*/
|
|
49
|
+
export function createTypesafeDriver(config: TypesafeDriverConfig): DecisionDriver {
|
|
50
|
+
let client: TypeSafeClient;
|
|
51
|
+
try {
|
|
52
|
+
client = new TypeSafeClient({
|
|
53
|
+
apiKey: config.apiKey,
|
|
54
|
+
// baseURL is forwarded as-is. SDK 0.6.0 exports no default
|
|
55
|
+
// base-URL constant (`ENV` holds env-var names only), so an
|
|
56
|
+
// omitted baseURL still lets the SDK self-resolve
|
|
57
|
+
// `TYPESAFE_BASE_URL` — documented residual for 0073.
|
|
58
|
+
baseURL: config.baseURL,
|
|
59
|
+
defaultModel: config.model,
|
|
60
|
+
timeout: config.timeoutMs,
|
|
61
|
+
retry: config.maxRetries === undefined ? undefined : { maxRetries: config.maxRetries },
|
|
62
|
+
fetch: config.fetch,
|
|
63
|
+
});
|
|
64
|
+
} catch (err) {
|
|
65
|
+
translateError(err);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
name: 'typesafe',
|
|
70
|
+
async ask({ state, questions, model }) {
|
|
71
|
+
validateQuestions(questions);
|
|
72
|
+
let result: SystemOneResult<Record<string, SdkQuestion>>;
|
|
73
|
+
try {
|
|
74
|
+
const sdkQuestions = Object.fromEntries(
|
|
75
|
+
Object.entries(questions).map(([name, question]) => [name, toSdkQuestion(question)]),
|
|
76
|
+
);
|
|
77
|
+
result = await client.systemOne({ state, questions: sdkQuestions, model });
|
|
78
|
+
} catch (err) {
|
|
79
|
+
translateError(err);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// R6: the SDK echoes the request's question names, so the mapped
|
|
83
|
+
// record keeps the caller's keys in correspondence. R8: `model`
|
|
84
|
+
// and `usage` on the result are deliberately not surfaced.
|
|
85
|
+
if (!result?.answers || typeof result.answers !== 'object' || Array.isArray(result.answers)) {
|
|
86
|
+
throw new DecisionBackendError('Invalid decision response: expected answers map', undefined);
|
|
87
|
+
}
|
|
88
|
+
const answers = Object.fromEntries(
|
|
89
|
+
Object.entries(result.answers).map(([name, answer]) => [name, fromSdkAnswer(answer)]),
|
|
90
|
+
);
|
|
91
|
+
validateAnswers(questions, answers);
|
|
92
|
+
return answers;
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Neutral question → SDK wire question (R4). `null` is "undescribed" on the wire. */
|
|
98
|
+
function toSdkQuestion(question: Question): SdkQuestion {
|
|
99
|
+
switch (question.kind) {
|
|
100
|
+
case 'choice':
|
|
101
|
+
return choice(question.prompt ?? null, question.labels);
|
|
102
|
+
case 'score':
|
|
103
|
+
return score(question.prompt ?? null, question.rubric);
|
|
104
|
+
case 'noul': {
|
|
105
|
+
const outcomes =
|
|
106
|
+
question.yes === undefined && question.no === undefined
|
|
107
|
+
? undefined
|
|
108
|
+
: { true: question.yes, false: question.no };
|
|
109
|
+
return noul(question.prompt ?? null, outcomes);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** SDK wire response → neutral answer (R5). Noul gets no confidence — the wire has none and none is invented. */
|
|
115
|
+
function fromSdkAnswer(answer: SdkResultFor<SdkQuestion>): Answer {
|
|
116
|
+
if (!answer || typeof answer !== 'object')
|
|
117
|
+
throw new DecisionBackendError('Invalid decision response: expected answer object', undefined);
|
|
118
|
+
switch (answer.type) {
|
|
119
|
+
case 'choice':
|
|
120
|
+
return {
|
|
121
|
+
kind: 'choice',
|
|
122
|
+
label: answer.choice,
|
|
123
|
+
confidence: answer.confidence,
|
|
124
|
+
probabilities: answer.probabilities,
|
|
125
|
+
};
|
|
126
|
+
case 'score':
|
|
127
|
+
return {
|
|
128
|
+
kind: 'score',
|
|
129
|
+
score: answer.score,
|
|
130
|
+
confidence: answer.confidence,
|
|
131
|
+
legend: answer.legend,
|
|
132
|
+
probabilities: answer.probabilities,
|
|
133
|
+
};
|
|
134
|
+
case 'noul':
|
|
135
|
+
return { kind: 'noul', probability: answer.noul };
|
|
136
|
+
default:
|
|
137
|
+
throw new DecisionBackendError('Invalid decision response: unknown answer kind', undefined);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* R7, per the design doc's error table. Ordered subclass-before-base:
|
|
143
|
+
* `APITimeoutError` extends `APIConnectionError`, and the HTTP subclasses
|
|
144
|
+
* extend `APIError`. Rows the table does not name fall to the nearest home:
|
|
145
|
+
* 4xx (`NotFoundError` included) is a `DecisionRequestError`, and any local
|
|
146
|
+
* `TypeSafeError` (empty questions, invalid client config) lands there too
|
|
147
|
+
* with `status: undefined` — so no SDK class ever escapes. Foreign errors are
|
|
148
|
+
* rethrown untouched; they are not this package's to translate.
|
|
149
|
+
*/
|
|
150
|
+
function translateError(err: unknown): never {
|
|
151
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
152
|
+
if (err instanceof RateLimitError) {
|
|
153
|
+
throw new DecisionRateLimitError(message, err.status, err.retryAfterMs, { cause: err });
|
|
154
|
+
}
|
|
155
|
+
if (err instanceof AuthenticationError || err instanceof PermissionDeniedError) {
|
|
156
|
+
throw new DecisionAuthError(message, err.status, { cause: err });
|
|
157
|
+
}
|
|
158
|
+
if (err instanceof APITimeoutError) {
|
|
159
|
+
throw new DecisionTimeoutError(message, err.timeoutMs, { cause: err });
|
|
160
|
+
}
|
|
161
|
+
if (err instanceof APIConnectionError) {
|
|
162
|
+
throw new DecisionConnectionError(message, { cause: err });
|
|
163
|
+
}
|
|
164
|
+
if (err instanceof APIError) {
|
|
165
|
+
if (err.status < 500) {
|
|
166
|
+
const raw = typeof err.body === 'string' ? err.body : (JSON.stringify(err.body) ?? '');
|
|
167
|
+
throw new DecisionRequestError(message, err.status, raw.slice(0, 200) || undefined, { cause: err });
|
|
168
|
+
}
|
|
169
|
+
throw new DecisionBackendError(message, err.status, { cause: err });
|
|
170
|
+
}
|
|
171
|
+
if (err instanceof TypeSafeError) {
|
|
172
|
+
throw new DecisionRequestError(message, undefined, message, { cause: err });
|
|
173
|
+
}
|
|
174
|
+
throw err;
|
|
175
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { DecisionBackendError, DecisionRequestError } from './errors';
|
|
2
|
+
import type { Question } from './types';
|
|
3
|
+
|
|
4
|
+
function record(value: unknown): value is Record<string, unknown> {
|
|
5
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function json(value: unknown, ancestors = new Set<unknown>()): boolean {
|
|
9
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean') return true;
|
|
10
|
+
if (typeof value === 'number') return Number.isFinite(value);
|
|
11
|
+
if (typeof value !== 'object' || ancestors.has(value)) return false;
|
|
12
|
+
ancestors.add(value);
|
|
13
|
+
const valid = Object.values(value).every((entry) => json(entry, ancestors));
|
|
14
|
+
ancestors.delete(value);
|
|
15
|
+
return valid;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function desc(value: unknown): boolean {
|
|
19
|
+
return (value === null || typeof value === 'string' || typeof value === 'object') && json(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Validate caller data before either a custom driver or the SDK consumes it. */
|
|
23
|
+
export function validateQuestions(questions: unknown): asserts questions is Record<string, Question> {
|
|
24
|
+
const fail: (name: string) => never = (name) => {
|
|
25
|
+
throw new DecisionRequestError(`Invalid decision question: ${name}`, undefined, undefined);
|
|
26
|
+
};
|
|
27
|
+
if (!record(questions) || Object.keys(questions).length === 0) fail('expected a nonempty question map');
|
|
28
|
+
for (const [name, question] of Object.entries(questions)) {
|
|
29
|
+
if (!record(question)) fail(name);
|
|
30
|
+
if (question.prompt !== undefined && !desc(question.prompt)) fail(name);
|
|
31
|
+
switch (question.kind) {
|
|
32
|
+
case 'choice':
|
|
33
|
+
if (
|
|
34
|
+
!record(question.labels) ||
|
|
35
|
+
Object.keys(question.labels).length === 0 ||
|
|
36
|
+
!Object.values(question.labels).every(desc)
|
|
37
|
+
)
|
|
38
|
+
fail(name);
|
|
39
|
+
break;
|
|
40
|
+
case 'score':
|
|
41
|
+
if (
|
|
42
|
+
!Array.isArray(question.rubric) ||
|
|
43
|
+
question.rubric.length < 2 ||
|
|
44
|
+
!Array.from(question.rubric).every(desc)
|
|
45
|
+
)
|
|
46
|
+
fail(name);
|
|
47
|
+
break;
|
|
48
|
+
case 'noul':
|
|
49
|
+
if (
|
|
50
|
+
(question.yes !== undefined && !desc(question.yes)) ||
|
|
51
|
+
(question.no !== undefined && !desc(question.no))
|
|
52
|
+
)
|
|
53
|
+
fail(name);
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
fail(name);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function bounded(value: unknown, max = 1): boolean {
|
|
62
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= max;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function keysMatch(value: unknown, keys: string[]): value is Record<string, unknown> {
|
|
66
|
+
return record(value) && Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Verify the correspondence that permits the facade's typed answer narrowing. */
|
|
70
|
+
export function validateAnswers(questions: Record<string, Question>, answers: unknown): void {
|
|
71
|
+
const fail: (name: string) => never = (name) => {
|
|
72
|
+
throw new DecisionBackendError(`Invalid decision response: ${name}`, undefined);
|
|
73
|
+
};
|
|
74
|
+
if (!keysMatch(answers, Object.keys(questions))) fail('answer names do not match questions');
|
|
75
|
+
for (const [name, question] of Object.entries(questions)) {
|
|
76
|
+
const answer = answers[name];
|
|
77
|
+
if (!record(answer) || answer.kind !== question.kind) fail(name);
|
|
78
|
+
if (question.kind === 'noul') {
|
|
79
|
+
if (!bounded(answer.probability)) fail(name);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const keys =
|
|
83
|
+
question.kind === 'choice' ? Object.keys(question.labels) : Array.from(question.rubric.keys(), String);
|
|
84
|
+
if (
|
|
85
|
+
!bounded(answer.confidence) ||
|
|
86
|
+
!keysMatch(answer.probabilities, keys) ||
|
|
87
|
+
!Object.values(answer.probabilities).every((p) => bounded(p))
|
|
88
|
+
)
|
|
89
|
+
fail(name);
|
|
90
|
+
if (question.kind === 'choice') {
|
|
91
|
+
if (typeof answer.label !== 'string' || !Object.hasOwn(question.labels, answer.label)) fail(name);
|
|
92
|
+
} else if (
|
|
93
|
+
!bounded(answer.score, question.rubric.length - 1) ||
|
|
94
|
+
!keysMatch(answer.legend, keys) ||
|
|
95
|
+
!Object.values(answer.legend).every(desc)
|
|
96
|
+
) {
|
|
97
|
+
fail(name);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,9 @@ export * from './agent-spec';
|
|
|
3
3
|
export * from './agents/auth-shims';
|
|
4
4
|
export * from './agents/shims';
|
|
5
5
|
export * from './ai-runner';
|
|
6
|
+
export * from './decision/decision-maker';
|
|
7
|
+
export * from './decision/errors';
|
|
8
|
+
export * from './decision/types';
|
|
6
9
|
export * from './doctor-runner';
|
|
7
10
|
export * from './events';
|
|
8
11
|
export * from './identity';
|