@gobing-ai/ts-ai-runner 0.4.69 → 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,134 @@
|
|
|
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
|
+
import { APIConnectionError, APIError, APITimeoutError, AuthenticationError, choice, noul, PermissionDeniedError, RateLimitError, score, TypeSafeClient, TypeSafeError, } from '@typesafe-ai/sdk';
|
|
8
|
+
import { DecisionAuthError, DecisionBackendError, DecisionConnectionError, DecisionRateLimitError, DecisionRequestError, DecisionTimeoutError, } from './errors.js';
|
|
9
|
+
import { validateAnswers, validateQuestions } from './validation.js';
|
|
10
|
+
/**
|
|
11
|
+
* Build the TypeSafe driver: exactly one `TypeSafeClient` per driver instance
|
|
12
|
+
* (R2), the API key always passed explicitly so the SDK's `TYPESAFE_API_KEY`
|
|
13
|
+
* self-resolution never runs (R1). All transport failures — at construction
|
|
14
|
+
* or at ask time — come back as `DecisionError`s, never SDK classes (R7).
|
|
15
|
+
*/
|
|
16
|
+
export function createTypesafeDriver(config) {
|
|
17
|
+
let client;
|
|
18
|
+
try {
|
|
19
|
+
client = new TypeSafeClient({
|
|
20
|
+
apiKey: config.apiKey,
|
|
21
|
+
// baseURL is forwarded as-is. SDK 0.6.0 exports no default
|
|
22
|
+
// base-URL constant (`ENV` holds env-var names only), so an
|
|
23
|
+
// omitted baseURL still lets the SDK self-resolve
|
|
24
|
+
// `TYPESAFE_BASE_URL` — documented residual for 0073.
|
|
25
|
+
baseURL: config.baseURL,
|
|
26
|
+
defaultModel: config.model,
|
|
27
|
+
timeout: config.timeoutMs,
|
|
28
|
+
retry: config.maxRetries === undefined ? undefined : { maxRetries: config.maxRetries },
|
|
29
|
+
fetch: config.fetch,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
translateError(err);
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
name: 'typesafe',
|
|
37
|
+
async ask({ state, questions, model }) {
|
|
38
|
+
validateQuestions(questions);
|
|
39
|
+
let result;
|
|
40
|
+
try {
|
|
41
|
+
const sdkQuestions = Object.fromEntries(Object.entries(questions).map(([name, question]) => [name, toSdkQuestion(question)]));
|
|
42
|
+
result = await client.systemOne({ state, questions: sdkQuestions, model });
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
translateError(err);
|
|
46
|
+
}
|
|
47
|
+
// R6: the SDK echoes the request's question names, so the mapped
|
|
48
|
+
// record keeps the caller's keys in correspondence. R8: `model`
|
|
49
|
+
// and `usage` on the result are deliberately not surfaced.
|
|
50
|
+
if (!result?.answers || typeof result.answers !== 'object' || Array.isArray(result.answers)) {
|
|
51
|
+
throw new DecisionBackendError('Invalid decision response: expected answers map', undefined);
|
|
52
|
+
}
|
|
53
|
+
const answers = Object.fromEntries(Object.entries(result.answers).map(([name, answer]) => [name, fromSdkAnswer(answer)]));
|
|
54
|
+
validateAnswers(questions, answers);
|
|
55
|
+
return answers;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/** Neutral question → SDK wire question (R4). `null` is "undescribed" on the wire. */
|
|
60
|
+
function toSdkQuestion(question) {
|
|
61
|
+
switch (question.kind) {
|
|
62
|
+
case 'choice':
|
|
63
|
+
return choice(question.prompt ?? null, question.labels);
|
|
64
|
+
case 'score':
|
|
65
|
+
return score(question.prompt ?? null, question.rubric);
|
|
66
|
+
case 'noul': {
|
|
67
|
+
const outcomes = question.yes === undefined && question.no === undefined
|
|
68
|
+
? undefined
|
|
69
|
+
: { true: question.yes, false: question.no };
|
|
70
|
+
return noul(question.prompt ?? null, outcomes);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** SDK wire response → neutral answer (R5). Noul gets no confidence — the wire has none and none is invented. */
|
|
75
|
+
function fromSdkAnswer(answer) {
|
|
76
|
+
if (!answer || typeof answer !== 'object')
|
|
77
|
+
throw new DecisionBackendError('Invalid decision response: expected answer object', undefined);
|
|
78
|
+
switch (answer.type) {
|
|
79
|
+
case 'choice':
|
|
80
|
+
return {
|
|
81
|
+
kind: 'choice',
|
|
82
|
+
label: answer.choice,
|
|
83
|
+
confidence: answer.confidence,
|
|
84
|
+
probabilities: answer.probabilities,
|
|
85
|
+
};
|
|
86
|
+
case 'score':
|
|
87
|
+
return {
|
|
88
|
+
kind: 'score',
|
|
89
|
+
score: answer.score,
|
|
90
|
+
confidence: answer.confidence,
|
|
91
|
+
legend: answer.legend,
|
|
92
|
+
probabilities: answer.probabilities,
|
|
93
|
+
};
|
|
94
|
+
case 'noul':
|
|
95
|
+
return { kind: 'noul', probability: answer.noul };
|
|
96
|
+
default:
|
|
97
|
+
throw new DecisionBackendError('Invalid decision response: unknown answer kind', undefined);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* R7, per the design doc's error table. Ordered subclass-before-base:
|
|
102
|
+
* `APITimeoutError` extends `APIConnectionError`, and the HTTP subclasses
|
|
103
|
+
* extend `APIError`. Rows the table does not name fall to the nearest home:
|
|
104
|
+
* 4xx (`NotFoundError` included) is a `DecisionRequestError`, and any local
|
|
105
|
+
* `TypeSafeError` (empty questions, invalid client config) lands there too
|
|
106
|
+
* with `status: undefined` — so no SDK class ever escapes. Foreign errors are
|
|
107
|
+
* rethrown untouched; they are not this package's to translate.
|
|
108
|
+
*/
|
|
109
|
+
function translateError(err) {
|
|
110
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
111
|
+
if (err instanceof RateLimitError) {
|
|
112
|
+
throw new DecisionRateLimitError(message, err.status, err.retryAfterMs, { cause: err });
|
|
113
|
+
}
|
|
114
|
+
if (err instanceof AuthenticationError || err instanceof PermissionDeniedError) {
|
|
115
|
+
throw new DecisionAuthError(message, err.status, { cause: err });
|
|
116
|
+
}
|
|
117
|
+
if (err instanceof APITimeoutError) {
|
|
118
|
+
throw new DecisionTimeoutError(message, err.timeoutMs, { cause: err });
|
|
119
|
+
}
|
|
120
|
+
if (err instanceof APIConnectionError) {
|
|
121
|
+
throw new DecisionConnectionError(message, { cause: err });
|
|
122
|
+
}
|
|
123
|
+
if (err instanceof APIError) {
|
|
124
|
+
if (err.status < 500) {
|
|
125
|
+
const raw = typeof err.body === 'string' ? err.body : (JSON.stringify(err.body) ?? '');
|
|
126
|
+
throw new DecisionRequestError(message, err.status, raw.slice(0, 200) || undefined, { cause: err });
|
|
127
|
+
}
|
|
128
|
+
throw new DecisionBackendError(message, err.status, { cause: err });
|
|
129
|
+
}
|
|
130
|
+
if (err instanceof TypeSafeError) {
|
|
131
|
+
throw new DecisionRequestError(message, undefined, message, { cause: err });
|
|
132
|
+
}
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Question } from './types';
|
|
2
|
+
/** Validate caller data before either a custom driver or the SDK consumes it. */
|
|
3
|
+
export declare function validateQuestions(questions: unknown): asserts questions is Record<string, Question>;
|
|
4
|
+
/** Verify the correspondence that permits the facade's typed answer narrowing. */
|
|
5
|
+
export declare function validateAnswers(questions: Record<string, Question>, answers: unknown): void;
|
|
6
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/decision/validation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAoBxC,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAoCnG;AAUD,kFAAkF;AAClF,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CA8B3F"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { DecisionBackendError, DecisionRequestError } from './errors.js';
|
|
2
|
+
function record(value) {
|
|
3
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function json(value, ancestors = new Set()) {
|
|
6
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
7
|
+
return true;
|
|
8
|
+
if (typeof value === 'number')
|
|
9
|
+
return Number.isFinite(value);
|
|
10
|
+
if (typeof value !== 'object' || ancestors.has(value))
|
|
11
|
+
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
|
+
function desc(value) {
|
|
18
|
+
return (value === null || typeof value === 'string' || typeof value === 'object') && json(value);
|
|
19
|
+
}
|
|
20
|
+
/** Validate caller data before either a custom driver or the SDK consumes it. */
|
|
21
|
+
export function validateQuestions(questions) {
|
|
22
|
+
const fail = (name) => {
|
|
23
|
+
throw new DecisionRequestError(`Invalid decision question: ${name}`, undefined, undefined);
|
|
24
|
+
};
|
|
25
|
+
if (!record(questions) || Object.keys(questions).length === 0)
|
|
26
|
+
fail('expected a nonempty question map');
|
|
27
|
+
for (const [name, question] of Object.entries(questions)) {
|
|
28
|
+
if (!record(question))
|
|
29
|
+
fail(name);
|
|
30
|
+
if (question.prompt !== undefined && !desc(question.prompt))
|
|
31
|
+
fail(name);
|
|
32
|
+
switch (question.kind) {
|
|
33
|
+
case 'choice':
|
|
34
|
+
if (!record(question.labels) ||
|
|
35
|
+
Object.keys(question.labels).length === 0 ||
|
|
36
|
+
!Object.values(question.labels).every(desc))
|
|
37
|
+
fail(name);
|
|
38
|
+
break;
|
|
39
|
+
case 'score':
|
|
40
|
+
if (!Array.isArray(question.rubric) ||
|
|
41
|
+
question.rubric.length < 2 ||
|
|
42
|
+
!Array.from(question.rubric).every(desc))
|
|
43
|
+
fail(name);
|
|
44
|
+
break;
|
|
45
|
+
case 'noul':
|
|
46
|
+
if ((question.yes !== undefined && !desc(question.yes)) ||
|
|
47
|
+
(question.no !== undefined && !desc(question.no)))
|
|
48
|
+
fail(name);
|
|
49
|
+
break;
|
|
50
|
+
default:
|
|
51
|
+
fail(name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function bounded(value, max = 1) {
|
|
56
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= max;
|
|
57
|
+
}
|
|
58
|
+
function keysMatch(value, keys) {
|
|
59
|
+
return record(value) && Object.keys(value).length === keys.length && keys.every((key) => Object.hasOwn(value, key));
|
|
60
|
+
}
|
|
61
|
+
/** Verify the correspondence that permits the facade's typed answer narrowing. */
|
|
62
|
+
export function validateAnswers(questions, answers) {
|
|
63
|
+
const fail = (name) => {
|
|
64
|
+
throw new DecisionBackendError(`Invalid decision response: ${name}`, undefined);
|
|
65
|
+
};
|
|
66
|
+
if (!keysMatch(answers, Object.keys(questions)))
|
|
67
|
+
fail('answer names do not match questions');
|
|
68
|
+
for (const [name, question] of Object.entries(questions)) {
|
|
69
|
+
const answer = answers[name];
|
|
70
|
+
if (!record(answer) || answer.kind !== question.kind)
|
|
71
|
+
fail(name);
|
|
72
|
+
if (question.kind === 'noul') {
|
|
73
|
+
if (!bounded(answer.probability))
|
|
74
|
+
fail(name);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const keys = question.kind === 'choice' ? Object.keys(question.labels) : Array.from(question.rubric.keys(), String);
|
|
78
|
+
if (!bounded(answer.confidence) ||
|
|
79
|
+
!keysMatch(answer.probabilities, keys) ||
|
|
80
|
+
!Object.values(answer.probabilities).every((p) => bounded(p)))
|
|
81
|
+
fail(name);
|
|
82
|
+
if (question.kind === 'choice') {
|
|
83
|
+
if (typeof answer.label !== 'string' || !Object.hasOwn(question.labels, answer.label))
|
|
84
|
+
fail(name);
|
|
85
|
+
}
|
|
86
|
+
else if (!bounded(answer.score, question.rubric.length - 1) ||
|
|
87
|
+
!keysMatch(answer.legend, keys) ||
|
|
88
|
+
!Object.values(answer.legend).every(desc)) {
|
|
89
|
+
fail(name);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/index.d.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';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,sBAAsB,CAAC;AACrC,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,2BAA2B,CAAC;AAC1C,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,sBAAsB,CAAC;AACrC,cAAc,SAAS,CAAC;AACxB,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,9 @@ export * from './agent-spec.js';
|
|
|
3
3
|
export * from './agents/auth-shims.js';
|
|
4
4
|
export * from './agents/shims.js';
|
|
5
5
|
export * from './ai-runner.js';
|
|
6
|
+
export * from './decision/decision-maker.js';
|
|
7
|
+
export * from './decision/errors.js';
|
|
8
|
+
export * from './decision/types.js';
|
|
6
9
|
export * from './doctor-runner.js';
|
|
7
10
|
export * from './events.js';
|
|
8
11
|
export * from './identity.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gobing-ai/ts-ai-runner",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "@gobing-ai/ts-ai-runner — Coding-agent shims, detection, doctor checks, and prompt execution.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -47,12 +47,13 @@
|
|
|
47
47
|
"release": "echo 'Manual publish is disabled. Releases go through GitHub Actions via Trusted Publishing — push a tag: git tag @gobing-ai/ts-ai-runner-v<version> && git push --tags' && exit 1"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@gobing-ai/ts-infra": "^0.
|
|
51
|
-
"@gobing-ai/ts-runtime": "^0.
|
|
50
|
+
"@gobing-ai/ts-infra": "^0.5.1",
|
|
51
|
+
"@gobing-ai/ts-runtime": "^0.5.1",
|
|
52
|
+
"@typesafe-ai/sdk": "0.6.0"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@types/bun": "1.3.14",
|
|
55
|
-
"@gobing-ai/ts-db": "^0.
|
|
56
|
+
"@gobing-ai/ts-db": "^0.5.1"
|
|
56
57
|
},
|
|
57
58
|
"publishConfig": {
|
|
58
59
|
"access": "public"
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public decision surface — the only type callers see. `choice`, `score`,
|
|
3
|
+
* and `noul` are implemented once, here, as `ask()` with a single-entry
|
|
4
|
+
* question map; drivers implement `ask` and nothing else, so the sugar can
|
|
5
|
+
* never drift from the batch path. This file imports no SDK (R9).
|
|
6
|
+
*/
|
|
7
|
+
import { getProcessEnv } from '@gobing-ai/ts-runtime';
|
|
8
|
+
import { DecisionConfigError } from './errors';
|
|
9
|
+
import type {
|
|
10
|
+
AnswersFor,
|
|
11
|
+
ChoiceAnswer,
|
|
12
|
+
DecisionDriver,
|
|
13
|
+
DecisionState,
|
|
14
|
+
Desc,
|
|
15
|
+
NoulAnswer,
|
|
16
|
+
Question,
|
|
17
|
+
ScoreAnswer,
|
|
18
|
+
} from './types';
|
|
19
|
+
import { q } from './types';
|
|
20
|
+
import { createTypesafeDriver } from './typesafe-driver';
|
|
21
|
+
import { validateAnswers, validateQuestions } from './validation';
|
|
22
|
+
|
|
23
|
+
/** Public decision surface: batch `ask` plus the three single-question sugar methods. */
|
|
24
|
+
export interface DecisionMaker {
|
|
25
|
+
/** Name of the backing driver (e.g. `"typesafe"`, or a custom driver's name). */
|
|
26
|
+
readonly driver: string;
|
|
27
|
+
/** Evaluate N named questions against one shared state in a single driver request. */
|
|
28
|
+
ask<const Q extends Record<string, Question>>(req: {
|
|
29
|
+
state: DecisionState;
|
|
30
|
+
questions: Q;
|
|
31
|
+
model?: string;
|
|
32
|
+
}): Promise<AnswersFor<Q>>;
|
|
33
|
+
/** Pick one label. Sugar over `ask` with exactly one choice question. */
|
|
34
|
+
choice<const L extends string>(
|
|
35
|
+
state: DecisionState,
|
|
36
|
+
prompt: Desc,
|
|
37
|
+
labels: Record<L, Desc>,
|
|
38
|
+
): Promise<ChoiceAnswer<L>>;
|
|
39
|
+
/** Score against a rubric. Sugar over `ask` with exactly one score question. */
|
|
40
|
+
score(state: DecisionState, prompt: Desc, rubric: readonly [Desc, Desc, ...Desc[]]): Promise<ScoreAnswer>;
|
|
41
|
+
/** Yes/no judgment. Sugar over `ask` with exactly one noul question. */
|
|
42
|
+
noul(state: DecisionState, prompt?: Desc, outcomes?: { yes?: Desc; no?: Desc }): Promise<NoulAnswer>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Factory options: driver selection, credential injection, and transport tuning. */
|
|
46
|
+
export interface DecisionMakerOptions {
|
|
47
|
+
/** Custom driver — when supplied, the default TypeSafe driver is never constructed and no key is required. */
|
|
48
|
+
driver?: DecisionDriver;
|
|
49
|
+
/** Injected env record; defaults to `getProcessEnv()` (doctor-runner convention). */
|
|
50
|
+
env?: Record<string, string | undefined>;
|
|
51
|
+
/** Explicit key — wins over `env.TYPESAFE_API_KEY`. */
|
|
52
|
+
apiKey?: string;
|
|
53
|
+
/** Forwarded to the TypeSafe driver; default is the SDK's model default. */
|
|
54
|
+
model?: string;
|
|
55
|
+
baseURL?: string;
|
|
56
|
+
timeoutMs?: number;
|
|
57
|
+
maxRetries?: number;
|
|
58
|
+
/** Injected fetch for tests. */
|
|
59
|
+
fetch?: typeof fetch;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Resolve the key per R7: `options.apiKey ?? (options.env ?? getProcessEnv()).TYPESAFE_API_KEY`.
|
|
64
|
+
* Throws before any driver construction or request.
|
|
65
|
+
*/
|
|
66
|
+
function resolveApiKey(options: DecisionMakerOptions): string {
|
|
67
|
+
const env = options.env ?? getProcessEnv();
|
|
68
|
+
const apiKey = options.apiKey ?? env.TYPESAFE_API_KEY;
|
|
69
|
+
if (!apiKey) {
|
|
70
|
+
throw new DecisionConfigError(
|
|
71
|
+
'Missing TYPESAFE_API_KEY — set it in the environment or pass options.apiKey.',
|
|
72
|
+
'TYPESAFE_API_KEY',
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return apiKey;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Build a DecisionMaker. `options.driver` wins when supplied; otherwise the
|
|
80
|
+
* TypeSafe driver is constructed lazily on first use, after key resolution —
|
|
81
|
+
* so a custom driver never requires a key and a missing key fails before any
|
|
82
|
+
* request (R6/R7).
|
|
83
|
+
*/
|
|
84
|
+
export function createDecisionMaker(options: DecisionMakerOptions = {}): DecisionMaker {
|
|
85
|
+
// Lazy driver slot: resolved on first use, at most once. A caller-supplied
|
|
86
|
+
// driver never reaches key resolution or default-driver construction (R6).
|
|
87
|
+
let defaultDriver: DecisionDriver | undefined;
|
|
88
|
+
const resolveDriver = (): DecisionDriver => {
|
|
89
|
+
if (options.driver) return options.driver;
|
|
90
|
+
defaultDriver ??= createTypesafeDriver({
|
|
91
|
+
apiKey: resolveApiKey(options),
|
|
92
|
+
model: options.model,
|
|
93
|
+
baseURL: options.baseURL,
|
|
94
|
+
timeoutMs: options.timeoutMs,
|
|
95
|
+
maxRetries: options.maxRetries,
|
|
96
|
+
fetch: options.fetch,
|
|
97
|
+
});
|
|
98
|
+
return defaultDriver;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const ask = async <const Q extends Record<string, Question>>(req: {
|
|
102
|
+
state: DecisionState;
|
|
103
|
+
questions: Q;
|
|
104
|
+
model?: string;
|
|
105
|
+
}): Promise<AnswersFor<Q>> => {
|
|
106
|
+
// R3: the questions map reaches the driver untouched — same reference,
|
|
107
|
+
// no reordering, renaming, or dropping.
|
|
108
|
+
const driver = resolveDriver();
|
|
109
|
+
validateQuestions(req.questions);
|
|
110
|
+
const answers = await driver.ask(req);
|
|
111
|
+
validateAnswers(req.questions, answers);
|
|
112
|
+
// Runtime validation above establishes each question/answer correspondence.
|
|
113
|
+
return answers as AnswersFor<Q>;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
driver: options.driver?.name ?? 'typesafe',
|
|
118
|
+
ask,
|
|
119
|
+
// R4/R5: each sugar method is one `ask` with exactly one `q.*` question,
|
|
120
|
+
// unwrapping the single answer — no second driver call, no duplicated
|
|
121
|
+
// request assembly.
|
|
122
|
+
choice: (state, prompt, labels) =>
|
|
123
|
+
ask({ state, questions: { question: q.choice(prompt, labels) } }).then((a) => a.question),
|
|
124
|
+
score: (state, prompt, rubric) =>
|
|
125
|
+
ask({ state, questions: { question: q.score(prompt, rubric) } }).then((a) => a.question),
|
|
126
|
+
noul: (state, prompt, outcomes) =>
|
|
127
|
+
ask({ state, questions: { question: q.noul(prompt, outcomes) } }).then((a) => a.question),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `DecisionError` taxonomy. Both the facade and the driver throw from this set;
|
|
3
|
+
* no raw SDK error class escapes the package. Keeping every class in one file
|
|
4
|
+
* makes that invariant checkable by reading a single file.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Base class for every decision-surface error. */
|
|
8
|
+
export class DecisionError extends Error {
|
|
9
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = new.target.name;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Missing configuration — e.g. no `TYPESAFE_API_KEY` in the injected env. Carries the variable name. */
|
|
16
|
+
export class DecisionConfigError extends DecisionError {
|
|
17
|
+
constructor(
|
|
18
|
+
message: string,
|
|
19
|
+
readonly variable: string,
|
|
20
|
+
options?: { cause?: unknown },
|
|
21
|
+
) {
|
|
22
|
+
super(message, options);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Authentication or permission failure upstream. Carries the HTTP status. */
|
|
27
|
+
export class DecisionAuthError extends DecisionError {
|
|
28
|
+
constructor(
|
|
29
|
+
message: string,
|
|
30
|
+
readonly status: number | undefined,
|
|
31
|
+
options?: { cause?: unknown },
|
|
32
|
+
) {
|
|
33
|
+
super(message, options);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Rate limited upstream. Carries the status and the parsed retry-after hint. */
|
|
38
|
+
export class DecisionRateLimitError extends DecisionError {
|
|
39
|
+
constructor(
|
|
40
|
+
message: string,
|
|
41
|
+
readonly status: number | undefined,
|
|
42
|
+
readonly retryAfterMs: number | undefined,
|
|
43
|
+
options?: { cause?: unknown },
|
|
44
|
+
) {
|
|
45
|
+
super(message, options);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Request timed out. Carries the configured timeout in milliseconds. */
|
|
50
|
+
export class DecisionTimeoutError extends DecisionError {
|
|
51
|
+
constructor(
|
|
52
|
+
message: string,
|
|
53
|
+
readonly timeoutMs: number | undefined,
|
|
54
|
+
options?: { cause?: unknown },
|
|
55
|
+
) {
|
|
56
|
+
super(message, options);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The backend was unreachable. Carries the underlying cause. */
|
|
61
|
+
export class DecisionConnectionError extends DecisionError {
|
|
62
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
63
|
+
super(message, options);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The request was rejected as malformed (4xx other than auth/rate-limit). Carries status and a body summary. */
|
|
68
|
+
export class DecisionRequestError extends DecisionError {
|
|
69
|
+
constructor(
|
|
70
|
+
message: string,
|
|
71
|
+
readonly status: number | undefined,
|
|
72
|
+
readonly bodySummary: string | undefined,
|
|
73
|
+
options?: { cause?: unknown },
|
|
74
|
+
) {
|
|
75
|
+
super(message, options);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Upstream server failure (5xx). Carries the HTTP status. */
|
|
80
|
+
export class DecisionBackendError extends DecisionError {
|
|
81
|
+
constructor(
|
|
82
|
+
message: string,
|
|
83
|
+
readonly status: number | undefined,
|
|
84
|
+
options?: { cause?: unknown },
|
|
85
|
+
) {
|
|
86
|
+
super(message, options);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-neutral decision vocabulary — deliberately not re-exported from
|
|
3
|
+
* `@typesafe-ai/sdk`. Re-exporting the SDK's response types would bind every
|
|
4
|
+
* caller to the vendor; the neutral names here (`kind`, `labels`, `rubric`,
|
|
5
|
+
* `probability`) keep vendor mapping explicit and confined to the driver.
|
|
6
|
+
* This file imports nothing — no SDK, no workspace package (boundary rule).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** Any JSON-serializable value. */
|
|
10
|
+
export type Json = string | number | boolean | null | Json[] | { [k: string]: Json };
|
|
11
|
+
|
|
12
|
+
/** Caller-supplied decision context: free text, a structured object, or a list. */
|
|
13
|
+
export type DecisionState = string | Json[] | { [k: string]: Json } | null;
|
|
14
|
+
|
|
15
|
+
/** A description; `null` means "left undescribed" (e.g. a label needing no rationale). */
|
|
16
|
+
export type Desc = string | Json[] | { [k: string]: Json } | null;
|
|
17
|
+
|
|
18
|
+
/** Pick one label from a fixed set. Labels are the answer's type parameter. */
|
|
19
|
+
export type ChoiceQuestion<L extends string> = { kind: 'choice'; prompt?: Desc; labels: Record<L, Desc> };
|
|
20
|
+
|
|
21
|
+
/** Score against a rubric; at least two levels, enforced at compile time. */
|
|
22
|
+
export type ScoreQuestion = { kind: 'score'; prompt?: Desc; rubric: readonly [Desc, Desc, ...Desc[]] };
|
|
23
|
+
|
|
24
|
+
/** Binary yes/no judgment with optional per-outcome descriptions. */
|
|
25
|
+
export type NoulQuestion = { kind: 'noul'; prompt?: Desc; yes?: Desc; no?: Desc };
|
|
26
|
+
|
|
27
|
+
/** Any question the decision surface accepts. */
|
|
28
|
+
export type Question = ChoiceQuestion<string> | ScoreQuestion | NoulQuestion;
|
|
29
|
+
|
|
30
|
+
/** Selected label, calibration confidence, and a probability per label. */
|
|
31
|
+
export type ChoiceAnswer<L extends string> = {
|
|
32
|
+
kind: 'choice';
|
|
33
|
+
label: L;
|
|
34
|
+
confidence: number;
|
|
35
|
+
probabilities: Record<L, number>;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** Rubric score, confidence, per-level legend, and a probability per level. */
|
|
39
|
+
export type ScoreAnswer = {
|
|
40
|
+
kind: 'score';
|
|
41
|
+
score: number;
|
|
42
|
+
confidence: number;
|
|
43
|
+
legend: Record<number, Desc>;
|
|
44
|
+
probabilities: Record<number, number>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Bare yes-probability — no `confidence`, on purpose. The wire response
|
|
49
|
+
* returns only a probability; synthesizing a confidence would fabricate
|
|
50
|
+
* calibration data. The asymmetry is load-bearing: later code cannot paper
|
|
51
|
+
* over it.
|
|
52
|
+
*/
|
|
53
|
+
export type NoulAnswer = { kind: 'noul'; probability: number };
|
|
54
|
+
|
|
55
|
+
/** Any answer the decision surface returns — discriminate on `kind`. */
|
|
56
|
+
export type Answer = ChoiceAnswer<string> | ScoreAnswer | NoulAnswer;
|
|
57
|
+
|
|
58
|
+
/** The answer type a question of shape `Q` decodes to. */
|
|
59
|
+
export type AnswerFor<Q> =
|
|
60
|
+
Q extends ChoiceQuestion<infer L>
|
|
61
|
+
? ChoiceAnswer<L>
|
|
62
|
+
: Q extends ScoreQuestion
|
|
63
|
+
? ScoreAnswer
|
|
64
|
+
: Q extends NoulQuestion
|
|
65
|
+
? NoulAnswer
|
|
66
|
+
: never;
|
|
67
|
+
|
|
68
|
+
/** Conditional answer record for a keyed question map. */
|
|
69
|
+
export type AnswersFor<Q extends Record<string, Question>> = { readonly [K in keyof Q]: AnswerFor<Q[K]> };
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Question builders for the batch path. Namespaced under `q` because
|
|
73
|
+
* `choice` / `score` / `noul` are already `DecisionMaker` method names.
|
|
74
|
+
*/
|
|
75
|
+
export const q = {
|
|
76
|
+
/**
|
|
77
|
+
* Build a choice question. `const` on the type parameter keeps an inline
|
|
78
|
+
* label map from widening to `string` — the caller keeps the union.
|
|
79
|
+
*/
|
|
80
|
+
choice: <const L extends string>(prompt: Desc, labels: Record<L, Desc>): ChoiceQuestion<L> => ({
|
|
81
|
+
kind: 'choice',
|
|
82
|
+
prompt,
|
|
83
|
+
labels,
|
|
84
|
+
}),
|
|
85
|
+
/** Build a score question from a rubric of at least two levels. */
|
|
86
|
+
score: (prompt: Desc, rubric: readonly [Desc, Desc, ...Desc[]]): ScoreQuestion => ({
|
|
87
|
+
kind: 'score',
|
|
88
|
+
prompt,
|
|
89
|
+
rubric,
|
|
90
|
+
}),
|
|
91
|
+
/** Build a yes/no question; outcomes are optional and default undescribed. */
|
|
92
|
+
noul: (prompt?: Desc, outcomes?: { yes?: Desc; no?: Desc }): NoulQuestion => ({
|
|
93
|
+
kind: 'noul',
|
|
94
|
+
prompt,
|
|
95
|
+
...outcomes,
|
|
96
|
+
}),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The one-method internal seam every backend driver implements. The loose
|
|
101
|
+
* `Record<string, Answer>` return is narrowed to `AnswersFor<Q>` by a single
|
|
102
|
+
* documented cast at the facade boundary — drivers stay trivial to write.
|
|
103
|
+
*/
|
|
104
|
+
export interface DecisionDriver {
|
|
105
|
+
readonly name: string;
|
|
106
|
+
ask(req: {
|
|
107
|
+
state: DecisionState;
|
|
108
|
+
questions: Record<string, Question>;
|
|
109
|
+
model?: string;
|
|
110
|
+
}): Promise<Record<string, Answer>>;
|
|
111
|
+
}
|