@monkeytestai/monkeytester 0.0.0-stage → 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/CHANGELOG.md +179 -0
- package/LICENSE +661 -0
- package/NOTICE +12 -0
- package/README.md +196 -2
- package/dist/cli.d.ts +43 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +774 -0
- package/dist/cli.js.map +1 -0
- package/dist/diff/index.d.ts +78 -0
- package/dist/diff/index.d.ts.map +1 -0
- package/dist/diff/index.js +119 -0
- package/dist/diff/index.js.map +1 -0
- package/dist/events.d.ts +66 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +77 -0
- package/dist/events.js.map +1 -0
- package/dist/explorer/index.d.ts +131 -0
- package/dist/explorer/index.d.ts.map +1 -0
- package/dist/explorer/index.js +675 -0
- package/dist/explorer/index.js.map +1 -0
- package/dist/explorer/queue.d.ts +94 -0
- package/dist/explorer/queue.d.ts.map +1 -0
- package/dist/explorer/queue.js +277 -0
- package/dist/explorer/queue.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/llm/coerce.d.ts +59 -0
- package/dist/llm/coerce.d.ts.map +1 -0
- package/dist/llm/coerce.js +246 -0
- package/dist/llm/coerce.js.map +1 -0
- package/dist/llm/index.d.ts +79 -0
- package/dist/llm/index.d.ts.map +1 -0
- package/dist/llm/index.js +392 -0
- package/dist/llm/index.js.map +1 -0
- package/dist/llm/presets.d.ts +61 -0
- package/dist/llm/presets.d.ts.map +1 -0
- package/dist/llm/presets.js +101 -0
- package/dist/llm/presets.js.map +1 -0
- package/dist/monkey.d.ts +151 -0
- package/dist/monkey.d.ts.map +1 -0
- package/dist/monkey.js +323 -0
- package/dist/monkey.js.map +1 -0
- package/dist/persistence/index.d.ts +118 -0
- package/dist/persistence/index.d.ts.map +1 -0
- package/dist/persistence/index.js +196 -0
- package/dist/persistence/index.js.map +1 -0
- package/dist/planner/faults.d.ts +24 -0
- package/dist/planner/faults.d.ts.map +1 -0
- package/dist/planner/faults.js +128 -0
- package/dist/planner/faults.js.map +1 -0
- package/dist/planner/forms.d.ts +33 -0
- package/dist/planner/forms.d.ts.map +1 -0
- package/dist/planner/forms.js +163 -0
- package/dist/planner/forms.js.map +1 -0
- package/dist/planner/index.d.ts +31 -0
- package/dist/planner/index.d.ts.map +1 -0
- package/dist/planner/index.js +252 -0
- package/dist/planner/index.js.map +1 -0
- package/dist/planner/salvage.d.ts +16 -0
- package/dist/planner/salvage.d.ts.map +1 -0
- package/dist/planner/salvage.js +107 -0
- package/dist/planner/salvage.js.map +1 -0
- package/dist/runner/actions.d.ts +91 -0
- package/dist/runner/actions.d.ts.map +1 -0
- package/dist/runner/actions.js +659 -0
- package/dist/runner/actions.js.map +1 -0
- package/dist/runner/evidence.d.ts +37 -0
- package/dist/runner/evidence.d.ts.map +1 -0
- package/dist/runner/evidence.js +122 -0
- package/dist/runner/evidence.js.map +1 -0
- package/dist/runner/http5xx.d.ts +40 -0
- package/dist/runner/http5xx.d.ts.map +1 -0
- package/dist/runner/http5xx.js +189 -0
- package/dist/runner/http5xx.js.map +1 -0
- package/dist/runner/index.d.ts +88 -0
- package/dist/runner/index.d.ts.map +1 -0
- package/dist/runner/index.js +600 -0
- package/dist/runner/index.js.map +1 -0
- package/dist/runner/replay.d.ts +42 -0
- package/dist/runner/replay.d.ts.map +1 -0
- package/dist/runner/replay.js +458 -0
- package/dist/runner/replay.js.map +1 -0
- package/dist/runner/timeout.d.ts +16 -0
- package/dist/runner/timeout.d.ts.map +1 -0
- package/dist/runner/timeout.js +38 -0
- package/dist/runner/timeout.js.map +1 -0
- package/dist/trace/fingerprint.d.ts +78 -0
- package/dist/trace/fingerprint.d.ts.map +1 -0
- package/dist/trace/fingerprint.js +273 -0
- package/dist/trace/fingerprint.js.map +1 -0
- package/dist/trace/serialize.d.ts +15 -0
- package/dist/trace/serialize.d.ts.map +1 -0
- package/dist/trace/serialize.js +86 -0
- package/dist/trace/serialize.js.map +1 -0
- package/dist/triager/index.d.ts +86 -0
- package/dist/triager/index.d.ts.map +1 -0
- package/dist/triager/index.js +322 -0
- package/dist/triager/index.js.map +1 -0
- package/dist/types.d.ts +497 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +10 -0
- package/dist/types.js.map +1 -0
- package/dist/verify/index.d.ts +52 -0
- package/dist/verify/index.d.ts.map +1 -0
- package/dist/verify/index.js +140 -0
- package/dist/verify/index.js.map +1 -0
- package/dist/verify/verdict.d.ts +34 -0
- package/dist/verify/verdict.d.ts.map +1 -0
- package/dist/verify/verdict.js +89 -0
- package/dist/verify/verdict.js.map +1 -0
- package/package.json +90 -4
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Coerce the shapes models actually return into the types our phases store.
|
|
3
|
+
*
|
|
4
|
+
* Structured-output calls still validate with Zod. These helpers run first so
|
|
5
|
+
* a clear mismatch (a login surface sent as `{url, label}`, a boolean sent as
|
|
6
|
+
* `"false"`, one object where an array was requested) becomes the internal
|
|
7
|
+
* value instead of failing the whole audit. Unreadable values stay unreadable
|
|
8
|
+
* and the caller decides whether to fall back.
|
|
9
|
+
*
|
|
10
|
+
* Call sites that validate model JSON:
|
|
11
|
+
* - explorer.summariseSite — site type, journeys, auth surfaces
|
|
12
|
+
* - explorer.summarisePage — per-page summary, auth flag, interactive list
|
|
13
|
+
* - planner.generate — loose object; salvage coerces each flow
|
|
14
|
+
* - triager.polish — bug title / observed / expected / notes
|
|
15
|
+
* - runner.resolveAction — one Playwright tool call (also used when healing)
|
|
16
|
+
* Forms, fault flows, and verify verdicts do not call a model.
|
|
17
|
+
*/
|
|
18
|
+
const PROSE_KEYS = ['summary', 'text', 'description', 'content', 'name', 'title', 'label', 'value'];
|
|
19
|
+
const LABEL_KEYS = ['label', 'name', 'title', 'text', 'value'];
|
|
20
|
+
const LOCATION_KEYS = ['url', 'href', 'path'];
|
|
21
|
+
const ENVELOPE_KEYS = ['data', 'result', 'output', 'json', 'response', 'payload'];
|
|
22
|
+
/** Trimmed string, or the decimal form of a finite number. Booleans are not strings. */
|
|
23
|
+
export function coerceToString(value) {
|
|
24
|
+
if (typeof value === 'string') {
|
|
25
|
+
const trimmed = value.trim().replace(/\s+/g, ' ');
|
|
26
|
+
return trimmed || undefined;
|
|
27
|
+
}
|
|
28
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
29
|
+
return String(value);
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* One human-readable line from a model object.
|
|
34
|
+
* `{ label, url }` becomes `"Login (https://…/login)"`. A bare label or URL is kept as-is.
|
|
35
|
+
*/
|
|
36
|
+
export function stringFromRecord(record) {
|
|
37
|
+
const label = firstString(record, LABEL_KEYS);
|
|
38
|
+
const loc = firstString(record, LOCATION_KEYS);
|
|
39
|
+
if (label && loc && label !== loc)
|
|
40
|
+
return `${label} (${loc})`;
|
|
41
|
+
if (label)
|
|
42
|
+
return label;
|
|
43
|
+
if (loc)
|
|
44
|
+
return loc;
|
|
45
|
+
return firstString(record, ['description', 'summary', 'content']);
|
|
46
|
+
}
|
|
47
|
+
function soleItem(value) {
|
|
48
|
+
if (Array.isArray(value) && value.length === 1)
|
|
49
|
+
return value[0];
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
function firstString(record, keys) {
|
|
53
|
+
for (const key of keys) {
|
|
54
|
+
const text = coerceToString(record[key]);
|
|
55
|
+
if (text)
|
|
56
|
+
return text;
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Normalise a string-list field.
|
|
62
|
+
*
|
|
63
|
+
* Accepts a string, a number, an array (including one nested level), or one
|
|
64
|
+
* object with a label / name / url / path. Null entries are dropped. Duplicate
|
|
65
|
+
* lines are dropped. An unreadable non-list value returns `undefined` so the
|
|
66
|
+
* field can be omitted instead of failing the rest of the object. An array
|
|
67
|
+
* whose entries are all unreadable returns `[]`.
|
|
68
|
+
*/
|
|
69
|
+
export function coerceStringList(value, opts = {}) {
|
|
70
|
+
if (value == null)
|
|
71
|
+
return undefined;
|
|
72
|
+
const minLength = opts.minLength ?? 1;
|
|
73
|
+
const maxItems = opts.maxItems ?? 12;
|
|
74
|
+
const collected = collectStrings(value);
|
|
75
|
+
if (!collected)
|
|
76
|
+
return undefined;
|
|
77
|
+
const out = [];
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
for (const item of collected) {
|
|
80
|
+
const text = item.trim().replace(/\s+/g, ' ');
|
|
81
|
+
if (text.length < minLength)
|
|
82
|
+
continue;
|
|
83
|
+
if (seen.has(text))
|
|
84
|
+
continue;
|
|
85
|
+
seen.add(text);
|
|
86
|
+
out.push(text);
|
|
87
|
+
if (out.length >= maxItems)
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
function collectStrings(value) {
|
|
93
|
+
if (Array.isArray(value)) {
|
|
94
|
+
const out = [];
|
|
95
|
+
for (const item of value) {
|
|
96
|
+
if (item == null)
|
|
97
|
+
continue;
|
|
98
|
+
const nested = collectStrings(item);
|
|
99
|
+
if (nested)
|
|
100
|
+
out.push(...nested);
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
const text = textFromItem(value);
|
|
105
|
+
if (text)
|
|
106
|
+
return [text];
|
|
107
|
+
if (typeof value === 'string' || typeof value === 'number')
|
|
108
|
+
return [];
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
function textFromItem(value) {
|
|
112
|
+
const direct = coerceToString(value);
|
|
113
|
+
if (direct)
|
|
114
|
+
return direct;
|
|
115
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
116
|
+
return stringFromRecord(value);
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
/** Join prose that arrived as a string, a number, a list of lines, or `{text}` / `{summary}`. */
|
|
121
|
+
export function coerceProse(value) {
|
|
122
|
+
if (typeof value === 'string') {
|
|
123
|
+
const trimmed = value.trim().replace(/\s+/g, ' ');
|
|
124
|
+
return trimmed || undefined;
|
|
125
|
+
}
|
|
126
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
127
|
+
return String(value);
|
|
128
|
+
if (Array.isArray(value)) {
|
|
129
|
+
const parts = value
|
|
130
|
+
.map(part => {
|
|
131
|
+
if (typeof part === 'string' || typeof part === 'number')
|
|
132
|
+
return coerceProse(part);
|
|
133
|
+
if (part && typeof part === 'object' && !Array.isArray(part)) {
|
|
134
|
+
return proseFromRecord(part);
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
})
|
|
138
|
+
.filter((part) => Boolean(part));
|
|
139
|
+
const joined = parts.join(' ').replace(/\s+/g, ' ').trim();
|
|
140
|
+
return joined || undefined;
|
|
141
|
+
}
|
|
142
|
+
if (value && typeof value === 'object')
|
|
143
|
+
return proseFromRecord(value);
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
function proseFromRecord(record) {
|
|
147
|
+
for (const key of PROSE_KEYS) {
|
|
148
|
+
const child = record[key];
|
|
149
|
+
if (typeof child === 'string' || typeof child === 'number') {
|
|
150
|
+
const text = coerceProse(child);
|
|
151
|
+
if (text)
|
|
152
|
+
return text;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
export function clipText(value, max) {
|
|
158
|
+
if (value.length <= max)
|
|
159
|
+
return value;
|
|
160
|
+
const sliced = value.slice(0, Math.max(0, max - 1)).trimEnd();
|
|
161
|
+
return `${sliced}…`;
|
|
162
|
+
}
|
|
163
|
+
/** `"true"`, `"yes"`, `1`, and the real booleans. Anything else is undefined. */
|
|
164
|
+
export function coerceBoolean(value) {
|
|
165
|
+
if (typeof value === 'boolean')
|
|
166
|
+
return value;
|
|
167
|
+
if (typeof value === 'number') {
|
|
168
|
+
if (value === 1)
|
|
169
|
+
return true;
|
|
170
|
+
if (value === 0)
|
|
171
|
+
return false;
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
if (typeof value === 'string') {
|
|
175
|
+
const token = value.trim().toLowerCase();
|
|
176
|
+
if (token === 'true' || token === 'yes' || token === 'y' || token === '1')
|
|
177
|
+
return true;
|
|
178
|
+
if (token === 'false' || token === 'no' || token === 'n' || token === '0')
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Case-insensitive enum match. Spaces and hyphens become underscores before
|
|
185
|
+
* alias lookup, so `"Happy Path"` can map to `happy_path`.
|
|
186
|
+
*/
|
|
187
|
+
export function coerceEnum(value, allowed, aliases = {}) {
|
|
188
|
+
if (typeof value !== 'string' && typeof value !== 'number')
|
|
189
|
+
return undefined;
|
|
190
|
+
const token = String(value).trim().toLowerCase().replace(/[\s-]+/g, '_');
|
|
191
|
+
if (!token)
|
|
192
|
+
return undefined;
|
|
193
|
+
if (aliases[token])
|
|
194
|
+
return aliases[token];
|
|
195
|
+
return allowed.find(item => item === token);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Models sometimes wrap the object we asked for: a one-element array, a JSON
|
|
199
|
+
* string, or `{ result: { … } }`. Return the inner object when it carries one
|
|
200
|
+
* of `hintKeys`. Otherwise return the value unchanged.
|
|
201
|
+
*/
|
|
202
|
+
export function unwrapModelObject(value, hintKeys) {
|
|
203
|
+
let current = value;
|
|
204
|
+
if (typeof current === 'string') {
|
|
205
|
+
const trimmed = current.trim();
|
|
206
|
+
const wrapped = (trimmed.startsWith('{') && trimmed.endsWith('}'))
|
|
207
|
+
|| (trimmed.startsWith('[') && trimmed.endsWith(']'));
|
|
208
|
+
if (!wrapped)
|
|
209
|
+
return value;
|
|
210
|
+
try {
|
|
211
|
+
current = JSON.parse(trimmed);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (Array.isArray(current)) {
|
|
218
|
+
if (current.length === 1)
|
|
219
|
+
current = current[0];
|
|
220
|
+
else
|
|
221
|
+
return current;
|
|
222
|
+
}
|
|
223
|
+
if (!current || typeof current !== 'object' || Array.isArray(current))
|
|
224
|
+
return current;
|
|
225
|
+
const record = current;
|
|
226
|
+
if (hintKeys.some(key => record[key] != null && record[key] !== ''))
|
|
227
|
+
return record;
|
|
228
|
+
for (const key of ENVELOPE_KEYS) {
|
|
229
|
+
const inner = soleItem(record[key]);
|
|
230
|
+
if (inner && typeof inner === 'object' && !Array.isArray(inner)) {
|
|
231
|
+
const obj = inner;
|
|
232
|
+
if (hintKeys.some(hint => obj[hint] != null && obj[hint] !== ''))
|
|
233
|
+
return obj;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const nested = Object.values(record).filter((item) => Boolean(item) && typeof item === 'object' && !Array.isArray(item));
|
|
237
|
+
if (nested.length === 1 && hintKeys.some(hint => nested[0][hint] != null && nested[0][hint] !== '')) {
|
|
238
|
+
return nested[0];
|
|
239
|
+
}
|
|
240
|
+
return record;
|
|
241
|
+
}
|
|
242
|
+
/** Technical detail for server logs. User-facing events stay on `message`. */
|
|
243
|
+
export function logModelDetail(scope, detail) {
|
|
244
|
+
console.warn(`[monkeytester] ${scope}: ${detail}`);
|
|
245
|
+
}
|
|
246
|
+
//# sourceMappingURL=coerce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coerce.js","sourceRoot":"","sources":["../../src/llm/coerce.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,UAAU,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAU,CAAC;AAC7G,MAAM,U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CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC;QACpG,OAAO,MAAM,CAAC,CAAC,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,MAAc;IAC1D,OAAO,CAAC,IAAI,CAAC,kBAAkB,KAAK,KAAK,MAAM,EAAE,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-provider LLM wrapper used by every phase of the engine.
|
|
3
|
+
*
|
|
4
|
+
* Supported providers:
|
|
5
|
+
* - openai — OpenAI Chat Completions (JSON mode + auto prompt cache)
|
|
6
|
+
* - gemini — Google Generative AI (JSON response MIME)
|
|
7
|
+
* - anthropic — Anthropic Messages API (JSON via prompt + parse,
|
|
8
|
+
* with explicit `cache_control` breakpoints)
|
|
9
|
+
* - mistral — Mistral Chat Completions (JSON object format)
|
|
10
|
+
* - groq — Groq (OpenAI-compatible, very fast Llama / Mixtral)
|
|
11
|
+
* - openai_compatible — Any OpenAI-compatible endpoint via baseURL
|
|
12
|
+
* (Ollama, vLLM, LM Studio, OpenRouter, Together, …)
|
|
13
|
+
*
|
|
14
|
+
* Why a wrapper:
|
|
15
|
+
* - Each phase (explorer / planner / runner / triager) can pick its own
|
|
16
|
+
* provider+model, so teams can mix a smart planner with a cheap runner.
|
|
17
|
+
* - All calls go through `generateJSON()` which parses + validates with zod,
|
|
18
|
+
* so a flaky LLM response never crashes a downstream phase.
|
|
19
|
+
* - Provider-specific quirks (markdown fences, missing JSON mode, …) are
|
|
20
|
+
* handled here once instead of in every prompt site.
|
|
21
|
+
*
|
|
22
|
+
* Prompt caching:
|
|
23
|
+
* - Callers indicate how a prompt is structured via `cacheable` (the long
|
|
24
|
+
* stable prefix) + `prompt` (the per-call dynamic suffix). Providers that
|
|
25
|
+
* support caching see real savings on repeat runs (planner against the
|
|
26
|
+
* same site, triager polishing the same evidence corpus).
|
|
27
|
+
* - Token + cache metrics are returned through `LLM.lastUsage` so SaaS hosts
|
|
28
|
+
* can render "saved $X via prompt cache" in their UI.
|
|
29
|
+
*/
|
|
30
|
+
import { z } from 'zod';
|
|
31
|
+
import type { LLMConfig } from '../types.js';
|
|
32
|
+
export interface GenerateJSONOptions<T> {
|
|
33
|
+
/** System-level guidance prepended to every prompt. */
|
|
34
|
+
system?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The long, stable, *cacheable* prefix of the user prompt.
|
|
37
|
+
*
|
|
38
|
+
* This is the part that is identical across many calls — the system context,
|
|
39
|
+
* the site map, the evidence corpus. Providers that support prompt caching
|
|
40
|
+
* (OpenAI auto, Anthropic ephemeral) use this string as the cache key. Keep
|
|
41
|
+
* it well above 1k tokens for OpenAI auto-cache hits.
|
|
42
|
+
*
|
|
43
|
+
* If omitted, only `prompt` is sent and no caching applies.
|
|
44
|
+
*/
|
|
45
|
+
cacheable?: string;
|
|
46
|
+
/** The dynamic per-call user prompt. Concatenated after `cacheable`. */
|
|
47
|
+
prompt: string;
|
|
48
|
+
/** Zod schema the response is parsed against. Throws on mismatch. */
|
|
49
|
+
schema: z.ZodType<T>;
|
|
50
|
+
/** Free-form key for log/error context (e.g. "explorer.summarisePage"). */
|
|
51
|
+
label?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Token usage + cache stats from the last `generateJSON` call. Populated
|
|
55
|
+
* provider-by-provider where the API exposes it. Read by hosts that want to
|
|
56
|
+
* render "you saved $X by hitting the prompt cache" in their dashboards.
|
|
57
|
+
*/
|
|
58
|
+
export interface LLMUsage {
|
|
59
|
+
/** Total input tokens billed for this call (provider-reported). */
|
|
60
|
+
inputTokens?: number;
|
|
61
|
+
/** Of `inputTokens`, how many were served from the prompt cache. */
|
|
62
|
+
cachedInputTokens?: number;
|
|
63
|
+
/** Output / completion tokens. */
|
|
64
|
+
outputTokens?: number;
|
|
65
|
+
/** Whether this call wrote new content into the prompt cache. */
|
|
66
|
+
cacheWrite?: boolean;
|
|
67
|
+
}
|
|
68
|
+
export interface LLM {
|
|
69
|
+
readonly providerLabel: string;
|
|
70
|
+
/**
|
|
71
|
+
* Token + cache stats from the last `generateJSON` call.
|
|
72
|
+
* `undefined` until at least one call has completed; `{}` when the provider
|
|
73
|
+
* does not expose usage.
|
|
74
|
+
*/
|
|
75
|
+
readonly lastUsage?: LLMUsage;
|
|
76
|
+
generateJSON<T>(opts: GenerateJSONOptions<T>): Promise<T>;
|
|
77
|
+
}
|
|
78
|
+
export declare function createLLM(config: LLMConfig): LLM;
|
|
79
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/llm/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAMH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAgB,MAAM,aAAa,CAAC;AAE3D,MAAM,WAAW,mBAAmB,CAAC,CAAC;IACpC,uDAAuD;IACvD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;;;OASG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IACrB,2EAA2E;IAC3E,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,oEAAoE;IACpE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,kCAAkC;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iEAAiE;IACjE,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,GAAG;IAClB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,QAAQ,CAAC;IAC9B,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,mBAAmB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAC3D;AAgZD,wBAAgB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,GAAG,CA6ChD"}
|