@gobing-ai/ts-ai-runner 0.5.29 → 0.5.32
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 +61 -6
- package/dist/agents/shims.d.ts.map +1 -1
- package/dist/agents/shims.js +9 -1
- package/dist/decision/decision-maker.d.ts +22 -1
- package/dist/decision/decision-maker.d.ts.map +1 -1
- package/dist/decision/decision-maker.js +32 -10
- package/dist/decision/sampled-driver.d.ts +115 -0
- package/dist/decision/sampled-driver.d.ts.map +1 -0
- package/dist/decision/sampled-driver.js +365 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/package.json +4 -5
- package/src/agents/shims.ts +9 -1
- package/src/decision/decision-maker.ts +53 -11
- package/src/decision/sampled-driver.ts +454 -0
- package/src/index.ts +1 -0
- package/dist/decision/typesafe-driver.d.ts +0 -24
- package/dist/decision/typesafe-driver.d.ts.map +0 -1
- package/dist/decision/typesafe-driver.js +0 -139
- package/src/decision/typesafe-driver.ts +0 -181
|
@@ -1,181 +0,0 @@
|
|
|
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
|
-
|
|
73
|
-
// The neutral contract allows `null` state ("no caller context") and the SDK type
|
|
74
|
-
// accepts it, but the wire drops a null field and the API answers 422 "state: Field
|
|
75
|
-
// required" — so every fully parameterless decision used to fall back on this backend.
|
|
76
|
-
// Same null→'' mapping the laya and fm drivers already apply; structured state passes
|
|
77
|
-
// through untouched (the wire accepts an object or array).
|
|
78
|
-
let result: SystemOneResult<Record<string, SdkQuestion>>;
|
|
79
|
-
try {
|
|
80
|
-
const sdkQuestions = Object.fromEntries(
|
|
81
|
-
Object.entries(questions).map(([name, question]) => [name, toSdkQuestion(question)]),
|
|
82
|
-
);
|
|
83
|
-
result = await client.systemOne({ state: state ?? '', questions: sdkQuestions, model });
|
|
84
|
-
} catch (err) {
|
|
85
|
-
translateError(err);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// R6: the SDK echoes the request's question names, so the mapped
|
|
89
|
-
// record keeps the caller's keys in correspondence. R8: `model`
|
|
90
|
-
// and `usage` on the result are deliberately not surfaced.
|
|
91
|
-
if (!result?.answers || typeof result.answers !== 'object' || Array.isArray(result.answers)) {
|
|
92
|
-
throw new DecisionBackendError('Invalid decision response: expected answers map', undefined);
|
|
93
|
-
}
|
|
94
|
-
const answers = Object.fromEntries(
|
|
95
|
-
Object.entries(result.answers).map(([name, answer]) => [name, fromSdkAnswer(answer)]),
|
|
96
|
-
);
|
|
97
|
-
validateAnswers(questions, answers);
|
|
98
|
-
return answers;
|
|
99
|
-
},
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** Neutral question → SDK wire question (R4). `null` is "undescribed" on the wire. */
|
|
104
|
-
function toSdkQuestion(question: Question): SdkQuestion {
|
|
105
|
-
switch (question.kind) {
|
|
106
|
-
case 'choice':
|
|
107
|
-
return choice(question.prompt ?? null, question.labels);
|
|
108
|
-
case 'score':
|
|
109
|
-
return score(question.prompt ?? null, question.rubric);
|
|
110
|
-
case 'noul': {
|
|
111
|
-
const outcomes =
|
|
112
|
-
question.yes === undefined && question.no === undefined
|
|
113
|
-
? undefined
|
|
114
|
-
: { true: question.yes, false: question.no };
|
|
115
|
-
return noul(question.prompt ?? null, outcomes);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/** SDK wire response → neutral answer (R5). Noul gets no confidence — the wire has none and none is invented. */
|
|
121
|
-
function fromSdkAnswer(answer: SdkResultFor<SdkQuestion>): Answer {
|
|
122
|
-
if (!answer || typeof answer !== 'object')
|
|
123
|
-
throw new DecisionBackendError('Invalid decision response: expected answer object', undefined);
|
|
124
|
-
switch (answer.type) {
|
|
125
|
-
case 'choice':
|
|
126
|
-
return {
|
|
127
|
-
kind: 'choice',
|
|
128
|
-
label: answer.choice,
|
|
129
|
-
confidence: answer.confidence,
|
|
130
|
-
probabilities: answer.probabilities,
|
|
131
|
-
};
|
|
132
|
-
case 'score':
|
|
133
|
-
return {
|
|
134
|
-
kind: 'score',
|
|
135
|
-
score: answer.score,
|
|
136
|
-
confidence: answer.confidence,
|
|
137
|
-
legend: answer.legend,
|
|
138
|
-
probabilities: answer.probabilities,
|
|
139
|
-
};
|
|
140
|
-
case 'noul':
|
|
141
|
-
return { kind: 'noul', probability: answer.noul };
|
|
142
|
-
default:
|
|
143
|
-
throw new DecisionBackendError('Invalid decision response: unknown answer kind', undefined);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* R7, per the design doc's error table. Ordered subclass-before-base:
|
|
149
|
-
* `APITimeoutError` extends `APIConnectionError`, and the HTTP subclasses
|
|
150
|
-
* extend `APIError`. Rows the table does not name fall to the nearest home:
|
|
151
|
-
* 4xx (`NotFoundError` included) is a `DecisionRequestError`, and any local
|
|
152
|
-
* `TypeSafeError` (empty questions, invalid client config) lands there too
|
|
153
|
-
* with `status: undefined` — so no SDK class ever escapes. Foreign errors are
|
|
154
|
-
* rethrown untouched; they are not this package's to translate.
|
|
155
|
-
*/
|
|
156
|
-
function translateError(err: unknown): never {
|
|
157
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
158
|
-
if (err instanceof RateLimitError) {
|
|
159
|
-
throw new DecisionRateLimitError(message, err.status, err.retryAfterMs, { cause: err });
|
|
160
|
-
}
|
|
161
|
-
if (err instanceof AuthenticationError || err instanceof PermissionDeniedError) {
|
|
162
|
-
throw new DecisionAuthError(message, err.status, { cause: err });
|
|
163
|
-
}
|
|
164
|
-
if (err instanceof APITimeoutError) {
|
|
165
|
-
throw new DecisionTimeoutError(message, err.timeoutMs, { cause: err });
|
|
166
|
-
}
|
|
167
|
-
if (err instanceof APIConnectionError) {
|
|
168
|
-
throw new DecisionConnectionError(message, { cause: err });
|
|
169
|
-
}
|
|
170
|
-
if (err instanceof APIError) {
|
|
171
|
-
if (err.status < 500) {
|
|
172
|
-
const raw = typeof err.body === 'string' ? err.body : (JSON.stringify(err.body) ?? '');
|
|
173
|
-
throw new DecisionRequestError(message, err.status, raw.slice(0, 200) || undefined, { cause: err });
|
|
174
|
-
}
|
|
175
|
-
throw new DecisionBackendError(message, err.status, { cause: err });
|
|
176
|
-
}
|
|
177
|
-
if (err instanceof TypeSafeError) {
|
|
178
|
-
throw new DecisionRequestError(message, undefined, message, { cause: err });
|
|
179
|
-
}
|
|
180
|
-
throw err;
|
|
181
|
-
}
|