theorum 0.1.2
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 +21 -0
- package/README.md +338 -0
- package/docs/AGENT_PROFILE_CONTRACT.md +161 -0
- package/docs/CLI_SPEC.md +183 -0
- package/docs/SECRETS.md +55 -0
- package/esm/_dnt.polyfills.d.ts +11 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/_dnt.shims.d.ts +5 -0
- package/esm/_dnt.shims.js +61 -0
- package/esm/mod.d.ts +37 -0
- package/esm/mod.js +35 -0
- package/esm/package.json +3 -0
- package/esm/src/guardrails/error.d.ts +35 -0
- package/esm/src/guardrails/error.js +116 -0
- package/esm/src/guardrails/injection.d.ts +12 -0
- package/esm/src/guardrails/injection.js +220 -0
- package/esm/src/guardrails/keys.d.ts +12 -0
- package/esm/src/guardrails/keys.js +132 -0
- package/esm/src/guardrails/mod.d.ts +14 -0
- package/esm/src/guardrails/mod.js +14 -0
- package/esm/src/guardrails/sanitize.d.ts +22 -0
- package/esm/src/guardrails/sanitize.js +133 -0
- package/esm/src/guardrails/sensitive.d.ts +12 -0
- package/esm/src/guardrails/sensitive.js +88 -0
- package/esm/src/kernel/engine/boundary.d.ts +10 -0
- package/esm/src/kernel/engine/boundary.js +55 -0
- package/esm/src/kernel/engine/delta.d.ts +8 -0
- package/esm/src/kernel/engine/delta.js +362 -0
- package/esm/src/kernel/engine/hash.d.ts +1 -0
- package/esm/src/kernel/engine/hash.js +9 -0
- package/esm/src/kernel/engine/record.d.ts +2 -0
- package/esm/src/kernel/engine/record.js +7 -0
- package/esm/src/kernel/engine/repair.d.ts +9 -0
- package/esm/src/kernel/engine/repair.js +38 -0
- package/esm/src/kernel/engine/runner.d.ts +14 -0
- package/esm/src/kernel/engine/runner.js +731 -0
- package/esm/src/kernel/engine/tree.d.ts +2 -0
- package/esm/src/kernel/engine/tree.js +17 -0
- package/esm/src/kernel/mod.d.ts +16 -0
- package/esm/src/kernel/mod.js +15 -0
- package/esm/src/kernel/registry/catalog.d.ts +24 -0
- package/esm/src/kernel/registry/catalog.js +213 -0
- package/esm/src/kernel/registry/profiles.d.ts +36 -0
- package/esm/src/kernel/registry/profiles.js +111 -0
- package/esm/src/kernel/registry/resolve.d.ts +20 -0
- package/esm/src/kernel/registry/resolve.js +235 -0
- package/esm/src/kernel/registry/schemas.d.ts +14 -0
- package/esm/src/kernel/registry/schemas.js +23 -0
- package/esm/src/kernel/registry/tools.d.ts +12 -0
- package/esm/src/kernel/registry/tools.js +36 -0
- package/esm/src/kernel/types.d.ts +497 -0
- package/esm/src/kernel/types.js +10 -0
- package/esm/src/observability/mod.d.ts +12 -0
- package/esm/src/observability/mod.js +10 -0
- package/esm/src/observability/spans.d.ts +16 -0
- package/esm/src/observability/spans.js +56 -0
- package/esm/src/observability/trace-attach.d.ts +16 -0
- package/esm/src/observability/trace-attach.js +81 -0
- package/esm/src/observability/trace-record.d.ts +112 -0
- package/esm/src/observability/trace-record.js +140 -0
- package/esm/src/observability/trace-usage.d.ts +3 -0
- package/esm/src/observability/trace-usage.js +32 -0
- package/esm/src/observability/trace.d.ts +23 -0
- package/esm/src/observability/trace.js +121 -0
- package/esm/src/providers/attachments.d.ts +17 -0
- package/esm/src/providers/attachments.js +156 -0
- package/esm/src/providers/gemini-tape.d.ts +3 -0
- package/esm/src/providers/gemini-tape.js +46 -0
- package/esm/src/providers/google-tap.d.ts +3 -0
- package/esm/src/providers/google-tap.js +48 -0
- package/esm/src/providers/interactions.d.ts +5 -0
- package/esm/src/providers/interactions.js +127 -0
- package/esm/src/providers/media.d.ts +5 -0
- package/esm/src/providers/media.js +125 -0
- package/esm/src/providers/mod.d.ts +15 -0
- package/esm/src/providers/mod.js +13 -0
- package/esm/src/providers/openrouter-payload.d.ts +24 -0
- package/esm/src/providers/openrouter-payload.js +177 -0
- package/esm/src/providers/openrouter.d.ts +17 -0
- package/esm/src/providers/openrouter.js +332 -0
- package/esm/src/providers/provider.d.ts +13 -0
- package/esm/src/providers/provider.js +123 -0
- package/esm/src/providers/sse.d.ts +7 -0
- package/esm/src/providers/sse.js +53 -0
- package/esm/src/providers/tts.d.ts +24 -0
- package/esm/src/providers/tts.js +144 -0
- package/package.json +48 -0
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import { asRecord } from './record.js';
|
|
2
|
+
function deltaText(delta) {
|
|
3
|
+
if (typeof delta.text === 'string') {
|
|
4
|
+
return delta.text;
|
|
5
|
+
}
|
|
6
|
+
const { content } = delta;
|
|
7
|
+
if (typeof content === 'string') {
|
|
8
|
+
return content;
|
|
9
|
+
}
|
|
10
|
+
const nested = asRecord(content);
|
|
11
|
+
if (nested && typeof nested.text === 'string') {
|
|
12
|
+
return nested.text;
|
|
13
|
+
}
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
function deltaMedia(delta) {
|
|
17
|
+
const mimeType = delta.mimeType ?? delta.mime_type;
|
|
18
|
+
const { data } = delta;
|
|
19
|
+
if (typeof mimeType === 'string' && typeof data === 'string' && data) {
|
|
20
|
+
return { type: 'media', media: { mimeType, data } };
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
function eventIfText(type, delta) {
|
|
25
|
+
const text = deltaText(delta);
|
|
26
|
+
if (!text) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
return [{ type, text }];
|
|
30
|
+
}
|
|
31
|
+
function eventsFromImage(delta) {
|
|
32
|
+
const media = deltaMedia(delta);
|
|
33
|
+
if (media) {
|
|
34
|
+
return [media];
|
|
35
|
+
}
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
function eventsFromDelta(deltaValue) {
|
|
39
|
+
const delta = asRecord(deltaValue);
|
|
40
|
+
if (!delta) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
const deltaType = String(delta.type ?? '');
|
|
44
|
+
if (deltaType === 'thought_summary' || deltaType === 'thought') {
|
|
45
|
+
return eventIfText('thought', delta);
|
|
46
|
+
}
|
|
47
|
+
if (deltaType === 'text') {
|
|
48
|
+
return eventIfText('text', delta);
|
|
49
|
+
}
|
|
50
|
+
if (deltaType === 'image') {
|
|
51
|
+
return eventsFromImage(delta);
|
|
52
|
+
}
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
function mediaFromRecord(rec) {
|
|
56
|
+
const mimeType = rec.mimeType ?? rec.mime_type;
|
|
57
|
+
const { data } = rec;
|
|
58
|
+
if (typeof mimeType === 'string' && typeof data === 'string' && data) {
|
|
59
|
+
return { type: 'media', media: { mimeType, data } };
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
function mediaFromOutputItem(item) {
|
|
64
|
+
const rec = asRecord(item);
|
|
65
|
+
if (!rec) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
if (rec.type !== 'image' && rec.type !== 'media') {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
return mediaFromRecord(rec);
|
|
72
|
+
}
|
|
73
|
+
function mediaFromOutputs(outputs) {
|
|
74
|
+
if (!Array.isArray(outputs)) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
for (const item of outputs) {
|
|
78
|
+
const media = mediaFromOutputItem(item);
|
|
79
|
+
if (media) {
|
|
80
|
+
return media;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
function mediaFromComplete(event) {
|
|
86
|
+
const interaction = asRecord(event.interaction) ?? event;
|
|
87
|
+
const direct = asRecord(interaction.output_image);
|
|
88
|
+
if (direct) {
|
|
89
|
+
const media = mediaFromRecord(direct);
|
|
90
|
+
if (media) {
|
|
91
|
+
return media;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return mediaFromOutputs(interaction.outputs);
|
|
95
|
+
}
|
|
96
|
+
function sourceFromMaps(maps) {
|
|
97
|
+
const uri = maps.uri ?? maps.googleMapsUri ?? maps.google_maps_uri;
|
|
98
|
+
if (typeof uri !== 'string' || !uri) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const title = maps.title ?? maps.name;
|
|
102
|
+
return {
|
|
103
|
+
type: 'maps',
|
|
104
|
+
uri,
|
|
105
|
+
title: typeof title === 'string' && title ? title : uri,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function sourceFromWeb(web) {
|
|
109
|
+
const uri = web.uri;
|
|
110
|
+
if (typeof uri !== 'string' || !uri) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const title = web.title ?? web.domain;
|
|
114
|
+
return {
|
|
115
|
+
type: 'web',
|
|
116
|
+
uri,
|
|
117
|
+
title: typeof title === 'string' && title ? title : uri,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function pushUniqueSource(sources, source) {
|
|
121
|
+
if (!source) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (!sources.some((item) => item.uri === source.uri && item.type === source.type)) {
|
|
125
|
+
sources.push(source);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function groundingChunks(metadata) {
|
|
129
|
+
const chunks = metadata.groundingChunks ?? metadata.grounding_chunks;
|
|
130
|
+
if (Array.isArray(chunks)) {
|
|
131
|
+
return chunks;
|
|
132
|
+
}
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
function searchHtml(metadata) {
|
|
136
|
+
const searchEntryPoint = asRecord(metadata.searchEntryPoint ?? metadata.search_entry_point);
|
|
137
|
+
const renderedContent = searchEntryPoint?.renderedContent ?? searchEntryPoint?.rendered_content;
|
|
138
|
+
if (typeof renderedContent === 'string' && renderedContent.trim()) {
|
|
139
|
+
return renderedContent;
|
|
140
|
+
}
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
function sourcesFromChunks(chunks) {
|
|
144
|
+
const sources = [];
|
|
145
|
+
for (const chunk of chunks) {
|
|
146
|
+
const record = asRecord(chunk);
|
|
147
|
+
if (!record) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const maps = asRecord(record.maps);
|
|
151
|
+
if (maps) {
|
|
152
|
+
pushUniqueSource(sources, sourceFromMaps(maps));
|
|
153
|
+
}
|
|
154
|
+
const web = asRecord(record.web);
|
|
155
|
+
if (web) {
|
|
156
|
+
pushUniqueSource(sources, sourceFromWeb(web));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return sources;
|
|
160
|
+
}
|
|
161
|
+
function mergeGrounding(a, b) {
|
|
162
|
+
if (!a) {
|
|
163
|
+
return b;
|
|
164
|
+
}
|
|
165
|
+
if (!b) {
|
|
166
|
+
return a;
|
|
167
|
+
}
|
|
168
|
+
const chunks = [...(a.chunks ?? []), ...(b.chunks ?? [])];
|
|
169
|
+
const sources = [...a.sources];
|
|
170
|
+
for (const source of b.sources) {
|
|
171
|
+
pushUniqueSource(sources, source);
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
metadata: b.metadata ?? a.metadata,
|
|
175
|
+
...(chunks.length > 0 ? { chunks } : {}),
|
|
176
|
+
searchHtml: b.searchHtml ?? a.searchHtml,
|
|
177
|
+
sources,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function groundingFromMetadata(metadataValue) {
|
|
181
|
+
const metadata = asRecord(metadataValue);
|
|
182
|
+
if (!metadata) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
const chunks = groundingChunks(metadata);
|
|
186
|
+
const sources = sourcesFromChunks(chunks);
|
|
187
|
+
const html = searchHtml(metadata);
|
|
188
|
+
if (chunks.length === 0 && sources.length === 0 && !html) {
|
|
189
|
+
return { metadata, sources };
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
metadata,
|
|
193
|
+
...(chunks.length > 0 ? { chunks } : {}),
|
|
194
|
+
...(html ? { searchHtml: html } : {}),
|
|
195
|
+
sources,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function groundingMetadataFromRecord(record) {
|
|
199
|
+
return record.groundingMetadata ?? record.grounding_metadata;
|
|
200
|
+
}
|
|
201
|
+
function groundingFromStep(stepValue) {
|
|
202
|
+
const step = asRecord(stepValue);
|
|
203
|
+
if (!step) {
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
let grounding = groundingFromMetadata(groundingMetadataFromRecord(step));
|
|
207
|
+
const content = step.content;
|
|
208
|
+
if (Array.isArray(content)) {
|
|
209
|
+
for (const block of content) {
|
|
210
|
+
grounding = mergeGrounding(grounding, groundingFromMetadata(groundingMetadataFromRecord(asRecord(block) ?? {})));
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return grounding;
|
|
214
|
+
}
|
|
215
|
+
function groundingFromEvent(event) {
|
|
216
|
+
let grounding = groundingFromMetadata(groundingMetadataFromRecord(event));
|
|
217
|
+
grounding = mergeGrounding(grounding, groundingFromStep(event.step));
|
|
218
|
+
grounding = mergeGrounding(grounding, groundingFromStep(event.delta));
|
|
219
|
+
const interaction = asRecord(event.interaction);
|
|
220
|
+
if (interaction) {
|
|
221
|
+
grounding = mergeGrounding(grounding, groundingFromMetadata(groundingMetadataFromRecord(interaction)));
|
|
222
|
+
}
|
|
223
|
+
const steps = interaction?.steps;
|
|
224
|
+
if (Array.isArray(steps)) {
|
|
225
|
+
for (const step of steps) {
|
|
226
|
+
grounding = mergeGrounding(grounding, groundingFromStep(step));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (!grounding) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
return { type: 'grounding', grounding };
|
|
233
|
+
}
|
|
234
|
+
const INPUT_KEYS = [
|
|
235
|
+
'total_input_tokens',
|
|
236
|
+
'totalInputTokens',
|
|
237
|
+
'prompt_token_count',
|
|
238
|
+
'promptTokenCount',
|
|
239
|
+
'prompt_tokens',
|
|
240
|
+
'input_tokens',
|
|
241
|
+
'inputTokens',
|
|
242
|
+
'input',
|
|
243
|
+
];
|
|
244
|
+
const OUTPUT_KEYS = [
|
|
245
|
+
'total_output_tokens',
|
|
246
|
+
'totalOutputTokens',
|
|
247
|
+
'candidates_token_count',
|
|
248
|
+
'candidatesTokenCount',
|
|
249
|
+
'candidates_tokens',
|
|
250
|
+
'completion_tokens',
|
|
251
|
+
'output_tokens',
|
|
252
|
+
'outputTokens',
|
|
253
|
+
'output',
|
|
254
|
+
];
|
|
255
|
+
const THINKING_KEYS = [
|
|
256
|
+
'total_thought_tokens',
|
|
257
|
+
'totalThoughtTokens',
|
|
258
|
+
'thoughts_token_count',
|
|
259
|
+
'thoughtsTokenCount',
|
|
260
|
+
'thoughts_tokens',
|
|
261
|
+
'thinking_tokens',
|
|
262
|
+
'thinkingTokens',
|
|
263
|
+
'thinking',
|
|
264
|
+
];
|
|
265
|
+
const TOOL_KEYS = [
|
|
266
|
+
'total_tool_use_tokens',
|
|
267
|
+
'totalToolUseTokens',
|
|
268
|
+
'tool_use_token_count',
|
|
269
|
+
'toolUseTokenCount',
|
|
270
|
+
'tool_tokens',
|
|
271
|
+
'toolTokens',
|
|
272
|
+
'toolUse',
|
|
273
|
+
];
|
|
274
|
+
const TOTAL_KEYS = [
|
|
275
|
+
'total_tokens',
|
|
276
|
+
'totalTokens',
|
|
277
|
+
'total_token_count',
|
|
278
|
+
'totalTokenCount',
|
|
279
|
+
'total',
|
|
280
|
+
];
|
|
281
|
+
function pickNumericField(record, keys) {
|
|
282
|
+
for (const key of keys) {
|
|
283
|
+
const val = record[key];
|
|
284
|
+
if (val !== undefined && val !== null) {
|
|
285
|
+
const n = Number(val);
|
|
286
|
+
if (!Number.isNaN(n)) {
|
|
287
|
+
return n;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return 0;
|
|
292
|
+
}
|
|
293
|
+
function extractUsageTokens(raw) {
|
|
294
|
+
if (!raw || typeof raw !== 'object') {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
const r = raw;
|
|
298
|
+
const input = pickNumericField(r, INPUT_KEYS);
|
|
299
|
+
const output = pickNumericField(r, OUTPUT_KEYS);
|
|
300
|
+
const thinking = pickNumericField(r, THINKING_KEYS);
|
|
301
|
+
const toolUse = pickNumericField(r, TOOL_KEYS);
|
|
302
|
+
const total = pickNumericField(r, TOTAL_KEYS) || input + output + thinking + toolUse;
|
|
303
|
+
if (input > 0 || output > 0 || thinking > 0 || toolUse > 0 || total > 0) {
|
|
304
|
+
return { input, output, thinking, toolUse, total };
|
|
305
|
+
}
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
const USAGE_KEYS = ['usage', 'usage_metadata', 'usageMetadata'];
|
|
309
|
+
function findUsageObject(target, event) {
|
|
310
|
+
for (const key of USAGE_KEYS) {
|
|
311
|
+
if (target[key])
|
|
312
|
+
return target[key];
|
|
313
|
+
}
|
|
314
|
+
for (const key of USAGE_KEYS) {
|
|
315
|
+
if (event[key])
|
|
316
|
+
return event[key];
|
|
317
|
+
}
|
|
318
|
+
return undefined;
|
|
319
|
+
}
|
|
320
|
+
function extractTokenEvent(event, interaction) {
|
|
321
|
+
const target = interaction ?? asRecord(event.interaction) ?? event;
|
|
322
|
+
const usage = extractUsageTokens(findUsageObject(target, event));
|
|
323
|
+
const interactionId = typeof target.id === 'string' ? target.id : undefined;
|
|
324
|
+
if (!usage) {
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
return {
|
|
328
|
+
type: 'tokens',
|
|
329
|
+
tokens: usage,
|
|
330
|
+
...(interactionId ? { interactionId } : {}),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function eventsFromComplete(event, alreadyText) {
|
|
334
|
+
const interaction = asRecord(event.interaction) ?? event;
|
|
335
|
+
const outputText = interaction.output_text;
|
|
336
|
+
const events = [];
|
|
337
|
+
if (!alreadyText && typeof outputText === 'string' && outputText) {
|
|
338
|
+
events.push({ type: 'text', text: outputText });
|
|
339
|
+
}
|
|
340
|
+
const media = mediaFromComplete(event);
|
|
341
|
+
if (media) {
|
|
342
|
+
events.push(media);
|
|
343
|
+
}
|
|
344
|
+
const tokenEvent = extractTokenEvent(event, interaction);
|
|
345
|
+
if (tokenEvent) {
|
|
346
|
+
events.push(tokenEvent);
|
|
347
|
+
}
|
|
348
|
+
const groundingEvent = groundingFromEvent(event);
|
|
349
|
+
if (groundingEvent) {
|
|
350
|
+
events.push(groundingEvent);
|
|
351
|
+
}
|
|
352
|
+
return events;
|
|
353
|
+
}
|
|
354
|
+
function tryStructured(text) {
|
|
355
|
+
try {
|
|
356
|
+
return { type: 'structured', structured: JSON.parse(text) };
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
return undefined;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
export { eventsFromComplete, eventsFromDelta, extractTokenEvent, extractUsageTokens, groundingFromEvent, tryStructured, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function sha256(text: string): Promise<string>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const HEX_PAD = 2;
|
|
2
|
+
const HEX_RADIX = 16;
|
|
3
|
+
function hexSha256(bytes) {
|
|
4
|
+
return [...bytes].map((b) => b.toString(HEX_RADIX).padStart(HEX_PAD, '0')).join('');
|
|
5
|
+
}
|
|
6
|
+
export async function sha256(text) {
|
|
7
|
+
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
8
|
+
return hexSha256(new Uint8Array(buf));
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Profile, TurnHistoryMessage, TurnRepairRequest } from '../types.js';
|
|
2
|
+
declare const MAX_REPAIR_HISTORY_EXCHANGES = 2;
|
|
3
|
+
declare function scopeHistory(history: TurnHistoryMessage[] | undefined): TurnHistoryMessage[];
|
|
4
|
+
declare function synthesizeRepairPrompt(args: {
|
|
5
|
+
profile: Profile;
|
|
6
|
+
repair: TurnRepairRequest;
|
|
7
|
+
history?: TurnHistoryMessage[];
|
|
8
|
+
}): string;
|
|
9
|
+
export { MAX_REPAIR_HISTORY_EXCHANGES, scopeHistory, synthesizeRepairPrompt };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const MAX_REPAIR_HISTORY_EXCHANGES = 2;
|
|
2
|
+
const MAX_REPAIR_HISTORY_MESSAGES = MAX_REPAIR_HISTORY_EXCHANGES * 2;
|
|
3
|
+
function scopeHistory(history) {
|
|
4
|
+
if (!history || history.length === 0) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
return history.slice(-MAX_REPAIR_HISTORY_MESSAGES);
|
|
8
|
+
}
|
|
9
|
+
function formatHistoryBlock(messages) {
|
|
10
|
+
if (messages.length === 0) {
|
|
11
|
+
return '';
|
|
12
|
+
}
|
|
13
|
+
const lines = messages.map((m) => `${m.role === 'user' ? 'User' : 'Assistant'}: ${m.content}`);
|
|
14
|
+
return `### RECENT CONVERSATION CONTEXT (LAST ${messages.length} TURNS)\n${lines.join('\n')}\n\n`;
|
|
15
|
+
}
|
|
16
|
+
function synthesizeRepairPrompt(args) {
|
|
17
|
+
const { profile, repair, history } = args;
|
|
18
|
+
const guidance = repair.guidance ||
|
|
19
|
+
profile.outputs.validation?.repairGuidance ||
|
|
20
|
+
'Revise the previous output so it satisfies the validator rejection. Preserve the intended user-facing substance unless the guidance says otherwise.';
|
|
21
|
+
const historyBlock = formatHistoryBlock(scopeHistory(history));
|
|
22
|
+
let prompt = `## OUTPUT REPAIR REQUEST\n\n`;
|
|
23
|
+
prompt += `The previous assistant output was rejected by a host validator and must be revised.\n\n`;
|
|
24
|
+
prompt += `### PREVIOUS OUTPUT\n\`\`\`\n${repair.previousOutput.trim()}\n\`\`\`\n\n`;
|
|
25
|
+
prompt += `### VALIDATOR REJECTION\n${repair.rejection.trim()}\n\n`;
|
|
26
|
+
if (guidance.trim()) {
|
|
27
|
+
prompt += `### REPAIR GUIDANCE\n${guidance.trim()}\n\n`;
|
|
28
|
+
}
|
|
29
|
+
if (historyBlock) {
|
|
30
|
+
prompt += historyBlock;
|
|
31
|
+
}
|
|
32
|
+
prompt += `### INSTRUCTIONS\n`;
|
|
33
|
+
prompt += `1. Inspect the previous output and validator rejection.\n`;
|
|
34
|
+
prompt += `2. Apply the repair guidance without inventing unsupported facts.\n`;
|
|
35
|
+
prompt += `3. Return only the corrected assistant output required by the active profile.`;
|
|
36
|
+
return prompt;
|
|
37
|
+
}
|
|
38
|
+
export { MAX_REPAIR_HISTORY_EXCHANGES, scopeHistory, synthesizeRepairPrompt };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic turn runner for THEORUM.
|
|
3
|
+
*
|
|
4
|
+
* `runTurn` resolves a profile, sanitizes input, binds canary boundaries,
|
|
5
|
+
* streams provider events, executes allowed tools, applies validation and
|
|
6
|
+
* egress repair loops, writes traces, and emits one terminal `done` event.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { type TraceSink } from '../../observability/trace.js';
|
|
11
|
+
import type { ModelProvider, TurnEvent, TurnRequest } from '../types.js';
|
|
12
|
+
/** Execute one host turn against a provider adapter. */
|
|
13
|
+
declare function runTurn(req: TurnRequest, provider: ModelProvider, sink?: TraceSink): AsyncGenerator<TurnEvent>;
|
|
14
|
+
export { runTurn };
|