@aid-framework/runtime 0.0.0-stage → 0.1.39
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/LICENSE +201 -0
- package/README.md +52 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/agents/deep_agents.d.ts +78 -0
- package/dist/agents/deep_agents.d.ts.map +1 -0
- package/dist/agents/deep_agents.js +477 -0
- package/dist/agents/deep_agents.js.map +1 -0
- package/dist/agents/engine.d.ts +85 -0
- package/dist/agents/engine.d.ts.map +1 -0
- package/dist/agents/engine.js +129 -0
- package/dist/agents/engine.js.map +1 -0
- package/dist/agents/gateway_chat_model.d.ts +151 -0
- package/dist/agents/gateway_chat_model.d.ts.map +1 -0
- package/dist/agents/gateway_chat_model.js +350 -0
- package/dist/agents/gateway_chat_model.js.map +1 -0
- package/dist/agents/guardrail_middleware.d.ts +57 -0
- package/dist/agents/guardrail_middleware.d.ts.map +1 -0
- package/dist/agents/guardrail_middleware.js +85 -0
- package/dist/agents/guardrail_middleware.js.map +1 -0
- package/dist/agents/guardrails.d.ts +124 -0
- package/dist/agents/guardrails.d.ts.map +1 -0
- package/dist/agents/guardrails.js +336 -0
- package/dist/agents/guardrails.js.map +1 -0
- package/dist/agents/index.d.ts +15 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/index.js +15 -0
- package/dist/agents/index.js.map +1 -0
- package/dist/agents/ir.d.ts +144 -0
- package/dist/agents/ir.d.ts.map +1 -0
- package/dist/agents/ir.js +513 -0
- package/dist/agents/ir.js.map +1 -0
- package/dist/agents/loop.d.ts +145 -0
- package/dist/agents/loop.d.ts.map +1 -0
- package/dist/agents/loop.js +1025 -0
- package/dist/agents/loop.js.map +1 -0
- package/dist/agents/memory.d.ts +115 -0
- package/dist/agents/memory.d.ts.map +1 -0
- package/dist/agents/memory.js +258 -0
- package/dist/agents/memory.js.map +1 -0
- package/dist/agents/state.d.ts +137 -0
- package/dist/agents/state.d.ts.map +1 -0
- package/dist/agents/state.js +255 -0
- package/dist/agents/state.js.map +1 -0
- package/dist/concurrency.d.ts +36 -0
- package/dist/concurrency.d.ts.map +1 -0
- package/dist/concurrency.js +86 -0
- package/dist/concurrency.js.map +1 -0
- package/dist/control.d.ts +116 -0
- package/dist/control.d.ts.map +1 -0
- package/dist/control.js +281 -0
- package/dist/control.js.map +1 -0
- package/dist/cost.d.ts +95 -0
- package/dist/cost.d.ts.map +1 -0
- package/dist/cost.js +161 -0
- package/dist/cost.js.map +1 -0
- package/dist/errors.d.ts +213 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +316 -0
- package/dist/errors.js.map +1 -0
- package/dist/evals/dataset.d.ts +45 -0
- package/dist/evals/dataset.d.ts.map +1 -0
- package/dist/evals/dataset.js +147 -0
- package/dist/evals/dataset.js.map +1 -0
- package/dist/evals/gate.d.ts +236 -0
- package/dist/evals/gate.d.ts.map +1 -0
- package/dist/evals/gate.js +572 -0
- package/dist/evals/gate.js.map +1 -0
- package/dist/evals/metrics.d.ts +141 -0
- package/dist/evals/metrics.d.ts.map +1 -0
- package/dist/evals/metrics.js +364 -0
- package/dist/evals/metrics.js.map +1 -0
- package/dist/evals/predicate.d.ts +79 -0
- package/dist/evals/predicate.d.ts.map +1 -0
- package/dist/evals/predicate.js +178 -0
- package/dist/evals/predicate.js.map +1 -0
- package/dist/evals/runner.d.ts +94 -0
- package/dist/evals/runner.d.ts.map +1 -0
- package/dist/evals/runner.js +202 -0
- package/dist/evals/runner.js.map +1 -0
- package/dist/gateway.d.ts +118 -0
- package/dist/gateway.d.ts.map +1 -0
- package/dist/gateway.js +250 -0
- package/dist/gateway.js.map +1 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/json.d.ts +44 -0
- package/dist/internal/json.d.ts.map +1 -0
- package/dist/internal/json.js +90 -0
- package/dist/internal/json.js.map +1 -0
- package/dist/internal/text.d.ts +17 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +22 -0
- package/dist/internal/text.js.map +1 -0
- package/dist/pipeline.d.ts +80 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +159 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/policy.d.ts +52 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +199 -0
- package/dist/policy.js.map +1 -0
- package/dist/prompts.d.ts +86 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +337 -0
- package/dist/prompts.js.map +1 -0
- package/dist/providers/base.d.ts +19 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +13 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/fake.d.ts +98 -0
- package/dist/providers/fake.d.ts.map +1 -0
- package/dist/providers/fake.js +181 -0
- package/dist/providers/fake.js.map +1 -0
- package/dist/providers/http.d.ts +62 -0
- package/dist/providers/http.d.ts.map +1 -0
- package/dist/providers/http.js +245 -0
- package/dist/providers/http.js.map +1 -0
- package/dist/retrieval/chunking.d.ts +88 -0
- package/dist/retrieval/chunking.d.ts.map +1 -0
- package/dist/retrieval/chunking.js +188 -0
- package/dist/retrieval/chunking.js.map +1 -0
- package/dist/retrieval/citations.d.ts +98 -0
- package/dist/retrieval/citations.d.ts.map +1 -0
- package/dist/retrieval/citations.js +134 -0
- package/dist/retrieval/citations.js.map +1 -0
- package/dist/retrieval/connectors.d.ts +74 -0
- package/dist/retrieval/connectors.d.ts.map +1 -0
- package/dist/retrieval/connectors.js +218 -0
- package/dist/retrieval/connectors.js.map +1 -0
- package/dist/retrieval/embedding.d.ts +116 -0
- package/dist/retrieval/embedding.d.ts.map +1 -0
- package/dist/retrieval/embedding.js +282 -0
- package/dist/retrieval/embedding.js.map +1 -0
- package/dist/retrieval/gate.d.ts +52 -0
- package/dist/retrieval/gate.d.ts.map +1 -0
- package/dist/retrieval/gate.js +91 -0
- package/dist/retrieval/gate.js.map +1 -0
- package/dist/retrieval/hybrid.d.ts +76 -0
- package/dist/retrieval/hybrid.d.ts.map +1 -0
- package/dist/retrieval/hybrid.js +150 -0
- package/dist/retrieval/hybrid.js.map +1 -0
- package/dist/retrieval/metrics.d.ts +126 -0
- package/dist/retrieval/metrics.d.ts.map +1 -0
- package/dist/retrieval/metrics.js +200 -0
- package/dist/retrieval/metrics.js.map +1 -0
- package/dist/retrieval/pgvector.d.ts +62 -0
- package/dist/retrieval/pgvector.d.ts.map +1 -0
- package/dist/retrieval/pgvector.js +262 -0
- package/dist/retrieval/pgvector.js.map +1 -0
- package/dist/retrieval/rerank.d.ts +61 -0
- package/dist/retrieval/rerank.d.ts.map +1 -0
- package/dist/retrieval/rerank.js +105 -0
- package/dist/retrieval/rerank.js.map +1 -0
- package/dist/retrieval/retriever.d.ts +125 -0
- package/dist/retrieval/retriever.d.ts.map +1 -0
- package/dist/retrieval/retriever.js +281 -0
- package/dist/retrieval/retriever.js.map +1 -0
- package/dist/retrieval/stores.d.ts +106 -0
- package/dist/retrieval/stores.d.ts.map +1 -0
- package/dist/retrieval/stores.js +142 -0
- package/dist/retrieval/stores.js.map +1 -0
- package/dist/retrieval/types.d.ts +314 -0
- package/dist/retrieval/types.d.ts.map +1 -0
- package/dist/retrieval/types.js +388 -0
- package/dist/retrieval/types.js.map +1 -0
- package/dist/steps.d.ts +156 -0
- package/dist/steps.d.ts.map +1 -0
- package/dist/steps.js +199 -0
- package/dist/steps.js.map +1 -0
- package/dist/structured.d.ts +91 -0
- package/dist/structured.d.ts.map +1 -0
- package/dist/structured.js +155 -0
- package/dist/structured.js.map +1 -0
- package/dist/tools.d.ts +100 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +201 -0
- package/dist/tools.js.map +1 -0
- package/dist/tracing.d.ts +104 -0
- package/dist/tracing.d.ts.map +1 -0
- package/dist/tracing.js +155 -0
- package/dist/tracing.js.map +1 -0
- package/dist/types.d.ts +240 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +357 -0
- package/dist/types.js.map +1 -0
- package/package.json +77 -3
package/dist/prompts.js
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Versioned prompt templates.
|
|
3
|
+
*
|
|
4
|
+
* A prompt is a versioned artifact, not a string constant, for the same reason a
|
|
5
|
+
* model is pinned: a baseline recorded against `support-reply` v2 means nothing if
|
|
6
|
+
* "v2" quietly becomes v3. The registry therefore refuses a duplicate name+version
|
|
7
|
+
* rather than overwriting, and version ordering is defined here rather than left to
|
|
8
|
+
* `Array.prototype.sort` on strings (`'v10' < 'v9'` lexicographically, which is
|
|
9
|
+
* exactly the bug this ordering exists to prevent).
|
|
10
|
+
*/
|
|
11
|
+
import { PromptNotFoundError, PromptRenderError } from './errors.js';
|
|
12
|
+
import { repr } from './internal/json.js';
|
|
13
|
+
import { Message } from './types.js';
|
|
14
|
+
/** One worked example, rendered into the message list between system and user. */
|
|
15
|
+
export class Example {
|
|
16
|
+
input;
|
|
17
|
+
output;
|
|
18
|
+
constructor(init) {
|
|
19
|
+
this.input = init.input;
|
|
20
|
+
this.output = init.output;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse `{name}` placeholders out of a template.
|
|
25
|
+
*
|
|
26
|
+
* `{{` and `}}` are literal braces, matching `str.format`. Field names run to the
|
|
27
|
+
* first `}` or `:`, so `{topic:>10}` declares `topic`.
|
|
28
|
+
*/
|
|
29
|
+
function templateVariables(template) {
|
|
30
|
+
const names = [];
|
|
31
|
+
let index = 0;
|
|
32
|
+
while (index < template.length) {
|
|
33
|
+
const character = template.charAt(index);
|
|
34
|
+
if (character === '{') {
|
|
35
|
+
if (template[index + 1] === '{') {
|
|
36
|
+
index += 2;
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const close = template.indexOf('}', index + 1);
|
|
40
|
+
if (close === -1) {
|
|
41
|
+
throw new PromptRenderError(`unclosed '{' in prompt template: ${repr(template)}`);
|
|
42
|
+
}
|
|
43
|
+
const field = template.slice(index + 1, close);
|
|
44
|
+
const stops = [field.indexOf(':'), field.indexOf('!')].filter((at) => at !== -1);
|
|
45
|
+
const name = field.slice(0, stops.length === 0 ? field.length : Math.min(...stops)).trim();
|
|
46
|
+
if (name.length === 0) {
|
|
47
|
+
throw new PromptRenderError('prompt templates cannot use positional fields');
|
|
48
|
+
}
|
|
49
|
+
if (!names.includes(name))
|
|
50
|
+
names.push(name);
|
|
51
|
+
index = close + 1;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (character === '}') {
|
|
55
|
+
if (template[index + 1] !== '}') {
|
|
56
|
+
throw new PromptRenderError("prompt templates cannot contain a stray '}'");
|
|
57
|
+
}
|
|
58
|
+
index += 2;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
index += 1;
|
|
62
|
+
}
|
|
63
|
+
return names;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Substitute fields in one pass.
|
|
67
|
+
*
|
|
68
|
+
* One pass, not "replace each variable then unescape": doing it in two passes unescapes
|
|
69
|
+
* braces that arrived *inside* a substituted value, which silently mangles data the
|
|
70
|
+
* caller supplied.
|
|
71
|
+
*/
|
|
72
|
+
function formatTemplate(template, values) {
|
|
73
|
+
const parts = [];
|
|
74
|
+
let index = 0;
|
|
75
|
+
while (index < template.length) {
|
|
76
|
+
const character = template.charAt(index);
|
|
77
|
+
if (character === '{') {
|
|
78
|
+
if (template[index + 1] === '{') {
|
|
79
|
+
parts.push('{');
|
|
80
|
+
index += 2;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const close = template.indexOf('}', index + 1);
|
|
84
|
+
if (close === -1) {
|
|
85
|
+
throw new PromptRenderError(`unclosed '{' in prompt template: ${repr(template)}`);
|
|
86
|
+
}
|
|
87
|
+
parts.push(formatField(template.slice(index + 1, close), values));
|
|
88
|
+
index = close + 1;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (character === '}') {
|
|
92
|
+
if (template[index + 1] !== '}') {
|
|
93
|
+
throw new PromptRenderError("prompt templates cannot contain a stray '}'");
|
|
94
|
+
}
|
|
95
|
+
parts.push('}');
|
|
96
|
+
index += 2;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
parts.push(character);
|
|
100
|
+
index += 1;
|
|
101
|
+
}
|
|
102
|
+
return parts.join('');
|
|
103
|
+
}
|
|
104
|
+
/** Render one `{name!conv:spec}` field. */
|
|
105
|
+
function formatField(raw, values) {
|
|
106
|
+
const colon = raw.indexOf(':');
|
|
107
|
+
const bang = raw.indexOf('!');
|
|
108
|
+
let nameEnd = raw.length;
|
|
109
|
+
let conversion;
|
|
110
|
+
let spec = '';
|
|
111
|
+
if (bang !== -1 && (colon === -1 || bang < colon)) {
|
|
112
|
+
nameEnd = bang;
|
|
113
|
+
const specStart = raw.indexOf(':', bang + 1);
|
|
114
|
+
conversion = (specStart === -1 ? raw.slice(bang + 1) : raw.slice(bang + 1, specStart)).trim();
|
|
115
|
+
spec = specStart === -1 ? '' : raw.slice(specStart + 1);
|
|
116
|
+
}
|
|
117
|
+
else if (colon !== -1) {
|
|
118
|
+
nameEnd = colon;
|
|
119
|
+
spec = raw.slice(colon + 1);
|
|
120
|
+
}
|
|
121
|
+
const name = raw.slice(0, nameEnd).trim();
|
|
122
|
+
if (name.length === 0) {
|
|
123
|
+
throw new PromptRenderError('prompt templates cannot use positional fields');
|
|
124
|
+
}
|
|
125
|
+
if (!(name in values)) {
|
|
126
|
+
throw new PromptRenderError(`prompt template is missing a value for ${repr(name)}`);
|
|
127
|
+
}
|
|
128
|
+
return applyFormatSpec(asText(values[name], conversion), spec);
|
|
129
|
+
}
|
|
130
|
+
/** Apply a `!s` / `!r` / `!a` conversion. */
|
|
131
|
+
function asText(value, conversion) {
|
|
132
|
+
if (conversion === undefined || conversion === 's')
|
|
133
|
+
return String(value);
|
|
134
|
+
if (conversion === 'r' || conversion === 'a') {
|
|
135
|
+
return typeof value === 'string' ? repr(value) : String(value);
|
|
136
|
+
}
|
|
137
|
+
throw new PromptRenderError(`prompt templates do not support the ${repr(conversion)} conversion`);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Apply a string format spec: `[[fill]align][width][.precision]`.
|
|
141
|
+
*
|
|
142
|
+
* A documented subset with a loud failure, in the same spirit as the tool-schema
|
|
143
|
+
* validator: numeric specs (`:,.2f`) are meaningless for interpolated prompt text, and
|
|
144
|
+
* leaving the spec in the output would be worse than refusing it.
|
|
145
|
+
*/
|
|
146
|
+
function applyFormatSpec(text, spec) {
|
|
147
|
+
if (spec === '')
|
|
148
|
+
return text;
|
|
149
|
+
const match = /^(?:(.)?([<>^]))?(\d+)?(?:\.(\d+))?$/.exec(spec);
|
|
150
|
+
if (match === null) {
|
|
151
|
+
throw new PromptRenderError(`unsupported format spec ${repr(spec)} in prompt template`);
|
|
152
|
+
}
|
|
153
|
+
const fill = match[1] ?? ' ';
|
|
154
|
+
const align = match[2];
|
|
155
|
+
const width = match[3] === undefined ? 0 : Number(match[3]);
|
|
156
|
+
const precision = match[4] === undefined ? undefined : Number(match[4]);
|
|
157
|
+
const trimmed = precision === undefined ? text : text.slice(0, precision);
|
|
158
|
+
const padding = width - trimmed.length;
|
|
159
|
+
if (align === undefined || padding <= 0)
|
|
160
|
+
return trimmed;
|
|
161
|
+
switch (align) {
|
|
162
|
+
case '<':
|
|
163
|
+
return trimmed + fill.repeat(padding);
|
|
164
|
+
case '>':
|
|
165
|
+
return fill.repeat(padding) + trimmed;
|
|
166
|
+
default: {
|
|
167
|
+
// Python's centre puts the extra space on the right: '{:^5}'.format('ab') -> ' ab '.
|
|
168
|
+
const left = Math.floor(padding / 2);
|
|
169
|
+
return fill.repeat(left) + trimmed + fill.repeat(padding - left);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/** A template plus the metadata a registry keys it by. */
|
|
174
|
+
export class PromptTemplate {
|
|
175
|
+
name;
|
|
176
|
+
version;
|
|
177
|
+
template;
|
|
178
|
+
description;
|
|
179
|
+
examples;
|
|
180
|
+
required;
|
|
181
|
+
constructor(init) {
|
|
182
|
+
if (init.name.length === 0)
|
|
183
|
+
throw new PromptRenderError('a prompt needs a name');
|
|
184
|
+
if (init.version.length === 0)
|
|
185
|
+
throw new PromptRenderError('a prompt needs a version');
|
|
186
|
+
this.name = init.name;
|
|
187
|
+
this.version = init.version;
|
|
188
|
+
this.template = init.template;
|
|
189
|
+
this.description = init.description ?? '';
|
|
190
|
+
this.examples = init.examples ?? [];
|
|
191
|
+
this.required = init.required ?? [];
|
|
192
|
+
}
|
|
193
|
+
/** The variables the template body actually uses. */
|
|
194
|
+
get variables() {
|
|
195
|
+
return templateVariables(this.template);
|
|
196
|
+
}
|
|
197
|
+
/** Fill the template, refusing both missing and unused variables. */
|
|
198
|
+
render(values = {}) {
|
|
199
|
+
// `required` is part of the contract, not documentation: a template may demand a
|
|
200
|
+
// value it never interpolates (a routing hint, say), and Python unions the two
|
|
201
|
+
// sets. Subtracting `required` from `unused` as well is what stops that demanded
|
|
202
|
+
// value being reported as a typo.
|
|
203
|
+
const expected = this.variables;
|
|
204
|
+
const required = new Set([...expected, ...this.required]);
|
|
205
|
+
const missing = [...required].filter((name) => !(name in values)).sort();
|
|
206
|
+
if (missing.length > 0) {
|
|
207
|
+
throw new PromptRenderError(`prompt ${repr(this.name)} v${this.version} is missing variables: ${missing.join(', ')}`);
|
|
208
|
+
}
|
|
209
|
+
const unused = Object.keys(values)
|
|
210
|
+
.filter((name) => !required.has(name))
|
|
211
|
+
.sort();
|
|
212
|
+
if (unused.length > 0) {
|
|
213
|
+
throw new PromptRenderError(`prompt ${repr(this.name)} v${this.version} received unused variables: ${unused.join(', ')}; template uses ${[...expected].sort().join(', ') || '<none>'}`);
|
|
214
|
+
}
|
|
215
|
+
return new RenderedPrompt({
|
|
216
|
+
name: this.name,
|
|
217
|
+
version: this.version,
|
|
218
|
+
text: formatTemplate(this.template, values),
|
|
219
|
+
examples: this.examples,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/** The result of rendering a template. */
|
|
224
|
+
export class RenderedPrompt {
|
|
225
|
+
name;
|
|
226
|
+
version;
|
|
227
|
+
text;
|
|
228
|
+
examples;
|
|
229
|
+
constructor(init) {
|
|
230
|
+
this.name = init.name;
|
|
231
|
+
this.version = init.version;
|
|
232
|
+
this.text = init.text;
|
|
233
|
+
this.examples = init.examples ?? [];
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* The message list this prompt becomes: system, then one pair per example, then
|
|
237
|
+
* the user turn.
|
|
238
|
+
*
|
|
239
|
+
* The examples sit between the system turn and the real question deliberately:
|
|
240
|
+
* they are part of the instructions, and putting them after the question invites
|
|
241
|
+
* a model to answer the last example instead.
|
|
242
|
+
*/
|
|
243
|
+
asMessages(system) {
|
|
244
|
+
const messages = [];
|
|
245
|
+
if (system !== undefined)
|
|
246
|
+
messages.push(Message.system(system));
|
|
247
|
+
for (const example of this.examples) {
|
|
248
|
+
messages.push(Message.user(example.input));
|
|
249
|
+
messages.push(Message.assistant(example.output));
|
|
250
|
+
}
|
|
251
|
+
messages.push(Message.user(this.text));
|
|
252
|
+
return messages;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function versionKey(version) {
|
|
256
|
+
return version.split('.').map((segment) => {
|
|
257
|
+
const stripped = segment.startsWith('v') ? segment.slice(1) : segment;
|
|
258
|
+
if (/^\d+$/.test(stripped))
|
|
259
|
+
return [0, Number.parseInt(stripped, 10), ''];
|
|
260
|
+
return [1, 0, stripped];
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
/** Compare two version strings by dotted-segment order. */
|
|
264
|
+
export function compareVersions(left, right) {
|
|
265
|
+
const a = versionKey(left);
|
|
266
|
+
const b = versionKey(right);
|
|
267
|
+
const length = Math.min(a.length, b.length);
|
|
268
|
+
for (let index = 0; index < length; index += 1) {
|
|
269
|
+
const one = a[index];
|
|
270
|
+
const other = b[index];
|
|
271
|
+
if (one === undefined || other === undefined)
|
|
272
|
+
break;
|
|
273
|
+
if (one[0] !== other[0])
|
|
274
|
+
return one[0] - other[0];
|
|
275
|
+
if (one[1] !== other[1])
|
|
276
|
+
return one[1] - other[1];
|
|
277
|
+
if (one[2] !== other[2])
|
|
278
|
+
return one[2] < other[2] ? -1 : 1;
|
|
279
|
+
}
|
|
280
|
+
return a.length - b.length;
|
|
281
|
+
}
|
|
282
|
+
/** Sort version strings oldest to newest. */
|
|
283
|
+
export function orderedVersions(versions) {
|
|
284
|
+
return [...versions].sort(compareVersions);
|
|
285
|
+
}
|
|
286
|
+
/** A closed set of templates, keyed by name and version. */
|
|
287
|
+
export class PromptRegistry {
|
|
288
|
+
_templates = new Map();
|
|
289
|
+
constructor(templates = []) {
|
|
290
|
+
for (const template of templates)
|
|
291
|
+
this.register(template);
|
|
292
|
+
}
|
|
293
|
+
/** Add a template. A duplicate name+version is a configuration error, not a replace. */
|
|
294
|
+
register(template) {
|
|
295
|
+
const key = this.key(template.name, template.version);
|
|
296
|
+
if (this._templates.has(key)) {
|
|
297
|
+
throw new PromptRenderError(`prompt ${repr(template.name)} v${template.version} is already registered`);
|
|
298
|
+
}
|
|
299
|
+
this._templates.set(key, template);
|
|
300
|
+
}
|
|
301
|
+
/** The registered names, sorted. */
|
|
302
|
+
names() {
|
|
303
|
+
return [...new Set([...this._templates.values()].map((template) => template.name))].sort();
|
|
304
|
+
}
|
|
305
|
+
/** Every version of one prompt, oldest first. */
|
|
306
|
+
versions(name) {
|
|
307
|
+
return orderedVersions([...this._templates.values()]
|
|
308
|
+
.filter((template) => template.name === name)
|
|
309
|
+
.map((template) => template.version));
|
|
310
|
+
}
|
|
311
|
+
/** The newest version of one prompt. */
|
|
312
|
+
latest(name) {
|
|
313
|
+
const versions = this.versions(name);
|
|
314
|
+
const newest = versions.at(-1);
|
|
315
|
+
if (newest === undefined)
|
|
316
|
+
throw new PromptNotFoundError(name, undefined, this.names());
|
|
317
|
+
return this.get(name, newest);
|
|
318
|
+
}
|
|
319
|
+
/** One prompt, by explicit version or the newest. */
|
|
320
|
+
get(name, version) {
|
|
321
|
+
const resolved = version ?? this.versions(name).at(-1);
|
|
322
|
+
if (resolved === undefined)
|
|
323
|
+
throw new PromptNotFoundError(name, version, this.names());
|
|
324
|
+
const template = this._templates.get(this.key(name, resolved));
|
|
325
|
+
if (template === undefined)
|
|
326
|
+
throw new PromptNotFoundError(name, version, this.names());
|
|
327
|
+
return template;
|
|
328
|
+
}
|
|
329
|
+
/** Resolve and render in one step. */
|
|
330
|
+
render(name, values = {}, version) {
|
|
331
|
+
return this.get(name, version).render(values);
|
|
332
|
+
}
|
|
333
|
+
key(name, version) {
|
|
334
|
+
return `${name}\u0000${version}`;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=prompts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompts.js","sourceRoot":"","sources":["../src/prompts.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,kFAAkF;AAClF,MAAM,OAAO,OAAO;IACT,KAAK,CAAS;IACd,MAAM,CAAS;IAExB,YAAY,IAAyD;QACnE,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;IAC5B,CAAC;CACF;AAED;;;;;GAKG;AACH,SAAS,iBAAiB,CAAC,QAAgB;IACzC,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,OAAO,KAAK,GAAG,QAAQ,CAAC,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,QAAQ,CAAC;IAClB,CAAC;IAED,sCAAsC;IACtC,MAAM,CACJ,IAAY,EACZ,MAAM,GAAsC,EAAE,EAC9C,OAAgB;QAEhB,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC;IAEO,GAAG,CAAC,IAAY,EAAE,OAAe;QACvC,OAAO,GAAG,IAAI,SAAS,OAAO,EAAE,CAAC;IACnC,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider seam.
|
|
3
|
+
*
|
|
4
|
+
* A provider is deliberately narrow: given a request and the model it is already
|
|
5
|
+
* pinned to, answer once. Everything interesting -- aliases, capability checks,
|
|
6
|
+
* fallback, budgets, tracing -- lives in the gateway, so adding a provider cannot
|
|
7
|
+
* change what a run costs or which model was called.
|
|
8
|
+
*/
|
|
9
|
+
import type { Capability, CompletionRequest, CompletionResponse, ModelSpec } from '../types.js';
|
|
10
|
+
/** One model vendor. `close` must be idempotent. */
|
|
11
|
+
export interface Provider {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly capabilities: ReadonlySet<Capability>;
|
|
14
|
+
complete(request: CompletionRequest, model: ModelSpec): Promise<CompletionResponse>;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
/** Whether a provider declares a capability. */
|
|
18
|
+
export declare function supports(provider: Provider, capability: Capability): boolean;
|
|
19
|
+
//# sourceMappingURL=base.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../src/providers/base.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEhG,oDAAoD;AACpD,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;IAC/C,QAAQ,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;IACpF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,gDAAgD;AAChD,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,CAE5E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The provider seam.
|
|
3
|
+
*
|
|
4
|
+
* A provider is deliberately narrow: given a request and the model it is already
|
|
5
|
+
* pinned to, answer once. Everything interesting -- aliases, capability checks,
|
|
6
|
+
* fallback, budgets, tracing -- lives in the gateway, so adding a provider cannot
|
|
7
|
+
* change what a run costs or which model was called.
|
|
8
|
+
*/
|
|
9
|
+
/** Whether a provider declares a capability. */
|
|
10
|
+
export function supports(provider, capability) {
|
|
11
|
+
return provider.capabilities.has(capability);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/providers/base.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAYH,gDAAgD;AAChD,MAAM,UAAU,QAAQ,CAAC,QAAkB,EAAE,UAAsB;IACjE,OAAO,QAAQ,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A deterministic provider for tests.
|
|
3
|
+
*
|
|
4
|
+
* This is not a mock in the "returns a canned value" sense. It is a *seeded
|
|
5
|
+
* simulation* of a probabilistic model: given the same seed and the same request it
|
|
6
|
+
* produces byte-identical output, but it deliberately answers correctly only a
|
|
7
|
+
* configurable fraction of the time. That is the whole point -- it lets the eval gate
|
|
8
|
+
* be tested against genuine non-determinism, so a test can prove that run-to-run
|
|
9
|
+
* variation is tolerated while a real regression is caught.
|
|
10
|
+
*
|
|
11
|
+
* ## One documented divergence from the Python runtime
|
|
12
|
+
*
|
|
13
|
+
* The reference implementation draws its accuracy sample from
|
|
14
|
+
* `random.Random("seed:index:prompt").random()`, which is CPython's Mersenne Twister
|
|
15
|
+
* seeded from a string. That seeding is a CPython implementation detail with no
|
|
16
|
+
* portable equivalent, so this runtime draws from a SHA-256 hash of the same triple
|
|
17
|
+
* instead. The properties the gate depends on are preserved:
|
|
18
|
+
*
|
|
19
|
+
* - the draw is a pure function of `(seed, call index, prompt)`, so a recorded
|
|
20
|
+
* baseline reproduces exactly;
|
|
21
|
+
* - the call index is part of the input, so re-running a dataset re-draws every case
|
|
22
|
+
* and an N-run sample genuinely varies.
|
|
23
|
+
*
|
|
24
|
+
* What is *not* claimed is that the two runtimes draw the same number. They do not,
|
|
25
|
+
* and no test in either runtime should assert that they do.
|
|
26
|
+
*/
|
|
27
|
+
import { type Capability, type CompletionRequest, CompletionResponse, type FinishReason, type ModelSpec, ToolCall } from '../types.js';
|
|
28
|
+
import type { Provider } from './base.js';
|
|
29
|
+
/** A responder sees the request and its call index, and returns the model's text. */
|
|
30
|
+
export type Responder = (request: CompletionRequest, index: number) => string;
|
|
31
|
+
/**
|
|
32
|
+
* One scripted model turn: some text, some tool calls, or both.
|
|
33
|
+
*
|
|
34
|
+
* `calls` is `(name, arguments)` pairs rather than `ToolCall` because a script should not
|
|
35
|
+
* have to invent unique call ids. The provider derives `call_<turn>_<position>` from the
|
|
36
|
+
* turn's position in the script, so two turns calling the same tool still produce
|
|
37
|
+
* distinguishable ids.
|
|
38
|
+
*/
|
|
39
|
+
export declare class ScriptedTurn {
|
|
40
|
+
readonly text: string;
|
|
41
|
+
readonly calls: readonly (readonly [string, Readonly<Record<string, unknown>>])[];
|
|
42
|
+
private constructor();
|
|
43
|
+
static answer(text: string): ScriptedTurn;
|
|
44
|
+
static tool(name: string, arguments_?: Readonly<Record<string, unknown>>): ScriptedTurn;
|
|
45
|
+
static tools(...calls: readonly (readonly [string, Readonly<Record<string, unknown>>])[]): ScriptedTurn;
|
|
46
|
+
get finishReason(): FinishReason;
|
|
47
|
+
toolCalls(turn: number): ToolCall[];
|
|
48
|
+
}
|
|
49
|
+
export interface FakeProviderOptions {
|
|
50
|
+
readonly responder?: Responder;
|
|
51
|
+
/** Maps the *user message* to the ideal response. */
|
|
52
|
+
readonly answers?: Readonly<Record<string, string>>;
|
|
53
|
+
/** One turn per successive call; once exhausted the provider behaves normally. */
|
|
54
|
+
readonly script?: readonly ScriptedTurn[];
|
|
55
|
+
readonly accuracy?: number;
|
|
56
|
+
readonly seed?: number;
|
|
57
|
+
readonly name?: string;
|
|
58
|
+
/** One message per successive call; once exhausted the provider behaves normally. */
|
|
59
|
+
readonly failWith?: readonly string[];
|
|
60
|
+
readonly tokensPerChar?: number;
|
|
61
|
+
readonly overheadTokens?: number;
|
|
62
|
+
}
|
|
63
|
+
/** Deterministic, seedable, and optionally inaccurate. */
|
|
64
|
+
export declare class FakeProvider implements Provider {
|
|
65
|
+
private readonly _responder;
|
|
66
|
+
private readonly _answers;
|
|
67
|
+
private readonly _script;
|
|
68
|
+
private readonly _accuracy;
|
|
69
|
+
private readonly _seed;
|
|
70
|
+
private readonly _name;
|
|
71
|
+
private readonly _failures;
|
|
72
|
+
private readonly _tokensPerChar;
|
|
73
|
+
private readonly _overheadTokens;
|
|
74
|
+
private _served;
|
|
75
|
+
/** Every request this provider has seen, in order. */
|
|
76
|
+
readonly calls: CompletionRequest[];
|
|
77
|
+
constructor(options?: FakeProviderOptions);
|
|
78
|
+
get name(): string;
|
|
79
|
+
get capabilities(): ReadonlySet<Capability>;
|
|
80
|
+
complete(request: CompletionRequest, model: ModelSpec): Promise<CompletionResponse>;
|
|
81
|
+
close(): Promise<void>;
|
|
82
|
+
private respondTo;
|
|
83
|
+
/**
|
|
84
|
+
* A stable pseudo-random draw in `[0, 1)` for this `(seed, index, prompt)` triple.
|
|
85
|
+
*
|
|
86
|
+
* See the module comment: this is a hash, not CPython's PRNG, and the divergence is
|
|
87
|
+
* deliberate and documented.
|
|
88
|
+
*/
|
|
89
|
+
private draw;
|
|
90
|
+
private countTokens;
|
|
91
|
+
}
|
|
92
|
+
/** The concatenated message text, which is what a provider keys its answers on. */
|
|
93
|
+
export declare function promptText(request: CompletionRequest): string;
|
|
94
|
+
/** Convenience factory used by the eval-gate tests. */
|
|
95
|
+
export declare function withAccuracy(accuracy: number, options?: {
|
|
96
|
+
readonly seed?: number;
|
|
97
|
+
}): FakeProvider;
|
|
98
|
+
//# sourceMappingURL=fake.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake.d.ts","sourceRoot":"","sources":["../../src/providers/fake.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAIH,OAAO,EACL,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,kBAAkB,EAClB,KAAK,YAAY,EACjB,KAAK,SAAS,EAEd,QAAQ,EACT,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,qFAAqF;AACrF,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;AAE9E;;;;;;;GAOG;AACH,qBAAa,YAAY;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAElF,OAAO,eAMN;IAED,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAExC;IAED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