@ikenga/contract 0.6.0 → 0.9.0
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 +56 -11
- package/dist/canvas/Canvas.d.ts +7 -0
- package/dist/canvas/Canvas.d.ts.map +1 -0
- package/dist/canvas/Canvas.js +115 -0
- package/dist/canvas/Canvas.js.map +1 -0
- package/dist/canvas/canvas.css +579 -0
- package/dist/canvas/index.d.ts +7 -0
- package/dist/canvas/index.d.ts.map +1 -0
- package/dist/canvas/index.js +4 -0
- package/dist/canvas/index.js.map +1 -0
- package/dist/canvas/types.d.ts +45 -0
- package/dist/canvas/types.d.ts.map +1 -0
- package/dist/canvas/types.js +2 -0
- package/dist/canvas/types.js.map +1 -0
- package/dist/canvas/use-drag-snap.d.ts +33 -0
- package/dist/canvas/use-drag-snap.d.ts.map +1 -0
- package/dist/canvas/use-drag-snap.js +73 -0
- package/dist/canvas/use-drag-snap.js.map +1 -0
- package/dist/canvas/use-pan-zoom.d.ts +32 -0
- package/dist/canvas/use-pan-zoom.d.ts.map +1 -0
- package/dist/canvas/use-pan-zoom.js +161 -0
- package/dist/canvas/use-pan-zoom.js.map +1 -0
- package/dist/engine/index.d.ts +2 -0
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +2 -0
- package/dist/engine/index.js.map +1 -1
- package/dist/engine/portability.d.ts +113 -0
- package/dist/engine/portability.d.ts.map +1 -0
- package/dist/engine/portability.js +17 -0
- package/dist/engine/portability.js.map +1 -0
- package/dist/engine/subagent-transcoder.d.ts +24 -0
- package/dist/engine/subagent-transcoder.d.ts.map +1 -0
- package/dist/engine/subagent-transcoder.js +341 -0
- package/dist/engine/subagent-transcoder.js.map +1 -0
- package/dist/engine.d.ts +574 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +85 -0
- package/dist/engine.js.map +1 -0
- package/dist/host-verbs.d.ts +194 -0
- package/dist/host-verbs.d.ts.map +1 -0
- package/dist/host-verbs.js +15 -0
- package/dist/host-verbs.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/manifest.d.ts +376 -19
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +95 -4
- package/dist/manifest.js.map +1 -1
- package/dist/registry.d.ts +364 -36
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +9 -0
- package/dist/registry.js.map +1 -1
- package/dist/scopes.js +1 -1
- package/dist/scopes.js.map +1 -1
- package/package.json +36 -10
- package/schemas/registry/index-v1.json +11 -0
- package/src/canvas/Canvas.tsx +161 -0
- package/src/canvas/canvas.css +579 -0
- package/src/canvas/index.ts +14 -0
- package/src/canvas/types.ts +48 -0
- package/src/canvas/use-drag-snap.ts +107 -0
- package/src/canvas/use-pan-zoom.ts +211 -0
- package/src/engine/index.ts +2 -0
- package/src/engine/portability.ts +123 -0
- package/src/engine/subagent-transcoder.test.ts +306 -0
- package/src/engine/subagent-transcoder.ts +333 -0
- package/src/host-verbs.ts +207 -0
- package/src/index.ts +1 -0
- package/src/manifest.test.ts +97 -0
- package/src/manifest.ts +109 -4
- package/src/registry.ts +9 -0
- package/src/scopes.ts +1 -1
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import {
|
|
4
|
+
codexTomlToMd,
|
|
5
|
+
mdToCodexToml,
|
|
6
|
+
mdToGeminiCommandToml,
|
|
7
|
+
} from './subagent-transcoder.js';
|
|
8
|
+
|
|
9
|
+
// Tiny TOML parser shared between tests — independent of the transcoder's
|
|
10
|
+
// internal parser so round-trip checks have something to compare against.
|
|
11
|
+
// Only handles the keys ADR §5 lists, which is what the transcoder emits.
|
|
12
|
+
function parseSimpleToml(toml: string): Record<string, unknown> {
|
|
13
|
+
const out: Record<string, unknown> = {};
|
|
14
|
+
const lines = toml.split('\n');
|
|
15
|
+
let i = 0;
|
|
16
|
+
let table: Record<string, unknown> = out;
|
|
17
|
+
while (i < lines.length) {
|
|
18
|
+
const raw = lines[i] ?? '';
|
|
19
|
+
const t = raw.trim();
|
|
20
|
+
if (t === '' || t.startsWith('#')) { i++; continue; }
|
|
21
|
+
const tableMatch = /^\[([^\]]+)\]$/.exec(t);
|
|
22
|
+
if (tableMatch) {
|
|
23
|
+
const name = (tableMatch[1] ?? '').trim();
|
|
24
|
+
const sub: Record<string, unknown> = {};
|
|
25
|
+
out[name] = sub;
|
|
26
|
+
table = sub;
|
|
27
|
+
i++;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const eq = raw.indexOf('=');
|
|
31
|
+
const key = raw.slice(0, eq).trim();
|
|
32
|
+
const restRaw = raw.slice(eq + 1);
|
|
33
|
+
const rest = restRaw.trim();
|
|
34
|
+
if (rest.startsWith('"""')) {
|
|
35
|
+
const afterOpen = rest.slice(3);
|
|
36
|
+
if (afterOpen.endsWith('"""') && afterOpen.length >= 3) {
|
|
37
|
+
table[key] = unescape(afterOpen.slice(0, -3));
|
|
38
|
+
i++;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const buf: string[] = [];
|
|
42
|
+
if (afterOpen !== '') buf.push(afterOpen);
|
|
43
|
+
i++;
|
|
44
|
+
while (i < lines.length) {
|
|
45
|
+
const ln = lines[i] ?? '';
|
|
46
|
+
if (ln.endsWith('"""')) {
|
|
47
|
+
const trailing = ln.slice(0, -3);
|
|
48
|
+
if (trailing === '' && buf.length > 0) {
|
|
49
|
+
table[key] = unescape(buf.join('\n') + '\n');
|
|
50
|
+
} else {
|
|
51
|
+
buf.push(trailing);
|
|
52
|
+
table[key] = unescape(buf.join('\n'));
|
|
53
|
+
}
|
|
54
|
+
i++;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
buf.push(ln);
|
|
58
|
+
i++;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (rest.startsWith('"') && rest.endsWith('"')) {
|
|
63
|
+
table[key] = JSON.parse(rest);
|
|
64
|
+
} else if (rest.startsWith('[') && rest.endsWith(']')) {
|
|
65
|
+
const inner = rest.slice(1, -1).trim();
|
|
66
|
+
if (inner === '') {
|
|
67
|
+
table[key] = [];
|
|
68
|
+
} else {
|
|
69
|
+
table[key] = inner.split(',').map((p) => {
|
|
70
|
+
const v = p.trim();
|
|
71
|
+
if (v.startsWith('"') && v.endsWith('"')) return JSON.parse(v);
|
|
72
|
+
if (/^-?\d+(\.\d+)?$/.test(v)) return Number(v);
|
|
73
|
+
return v;
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
} else if (rest === 'true') table[key] = true;
|
|
77
|
+
else if (rest === 'false') table[key] = false;
|
|
78
|
+
else if (/^-?\d+(\.\d+)?$/.test(rest)) table[key] = Number(rest);
|
|
79
|
+
else table[key] = rest;
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function unescape(s: string): string {
|
|
86
|
+
return s.replace(/"\\""\\""/g, '"""');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function deepEqualKeys(a: Record<string, unknown>, b: Record<string, unknown>): boolean {
|
|
90
|
+
const ak = Object.keys(a).sort();
|
|
91
|
+
const bk = Object.keys(b).sort();
|
|
92
|
+
if (ak.join(',') !== bk.join(',')) return false;
|
|
93
|
+
for (const k of ak) {
|
|
94
|
+
const va = a[k];
|
|
95
|
+
const vb = b[k];
|
|
96
|
+
if (Array.isArray(va) && Array.isArray(vb)) {
|
|
97
|
+
if (va.length !== vb.length) return false;
|
|
98
|
+
for (let i = 0; i < va.length; i++) {
|
|
99
|
+
if (JSON.stringify(va[i]) !== JSON.stringify(vb[i])) return false;
|
|
100
|
+
}
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (typeof va === 'object' && va !== null && typeof vb === 'object' && vb !== null) {
|
|
104
|
+
if (!deepEqualKeys(va as Record<string, unknown>, vb as Record<string, unknown>)) return false;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (va !== vb) return false;
|
|
108
|
+
}
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ---------------- TOML → MD → TOML round-trip ----------------
|
|
113
|
+
|
|
114
|
+
test('TOML → MD → TOML round-trip preserves canonical keys', () => {
|
|
115
|
+
const original = [
|
|
116
|
+
'name = "my-agent"',
|
|
117
|
+
'description = "Does the thing"',
|
|
118
|
+
'tools = ["bash", "read"]',
|
|
119
|
+
'model = "claude-sonnet-4"',
|
|
120
|
+
'system_prompt = """',
|
|
121
|
+
'You are a helpful agent.',
|
|
122
|
+
'Be terse.',
|
|
123
|
+
'"""',
|
|
124
|
+
].join('\n') + '\n';
|
|
125
|
+
const md = codexTomlToMd(original);
|
|
126
|
+
const reemitted = mdToCodexToml(md);
|
|
127
|
+
const a = parseSimpleToml(original);
|
|
128
|
+
const b = parseSimpleToml(reemitted);
|
|
129
|
+
assert.ok(deepEqualKeys(a, b), `mismatch:\n${JSON.stringify(a)}\nvs\n${JSON.stringify(b)}`);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ---------------- MD → TOML → MD round-trip with every ADR §5 key ----------------
|
|
133
|
+
|
|
134
|
+
test('MD → TOML → MD round-trip preserves every ADR §5 key', () => {
|
|
135
|
+
const md = [
|
|
136
|
+
'---',
|
|
137
|
+
'name: kitchen-sink',
|
|
138
|
+
'description: Every key in ADR §5',
|
|
139
|
+
'tools: [bash, read, write]',
|
|
140
|
+
'model: claude-opus-4-7',
|
|
141
|
+
'developer_instructions: Internal notes for Codex',
|
|
142
|
+
'sandbox_mode: workspace-write',
|
|
143
|
+
'temperature: 0.7',
|
|
144
|
+
'max_turns: 20',
|
|
145
|
+
'timeout_mins: 5',
|
|
146
|
+
'---',
|
|
147
|
+
'',
|
|
148
|
+
'You are an agent that does everything.',
|
|
149
|
+
'',
|
|
150
|
+
'Be concise.',
|
|
151
|
+
].join('\n');
|
|
152
|
+
const toml = mdToCodexToml(md);
|
|
153
|
+
const back = codexTomlToMd(toml);
|
|
154
|
+
// Re-parse the returned MD's frontmatter into TOML again so we can
|
|
155
|
+
// diff structurally — frontmatter ordering is allowed to drift.
|
|
156
|
+
const reToml = mdToCodexToml(back);
|
|
157
|
+
const a = parseSimpleToml(toml);
|
|
158
|
+
const b = parseSimpleToml(reToml);
|
|
159
|
+
for (const k of [
|
|
160
|
+
'name', 'description', 'tools', 'model',
|
|
161
|
+
'developer_instructions', 'sandbox_mode',
|
|
162
|
+
'temperature', 'max_turns', 'timeout_mins',
|
|
163
|
+
'system_prompt',
|
|
164
|
+
]) {
|
|
165
|
+
assert.ok(k in a, `lost key ${k} in first emit`);
|
|
166
|
+
assert.ok(k in b, `lost key ${k} in re-emit`);
|
|
167
|
+
assert.equal(JSON.stringify(a[k]), JSON.stringify(b[k]), `key ${k} drifted`);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// ---------------- Gemini command round-trip ----------------
|
|
172
|
+
|
|
173
|
+
test('mdToGeminiCommandToml round-trips body byte-equal modulo trailing newline', () => {
|
|
174
|
+
const body = 'Summarize the diff in 3 bullets.\nThen suggest tests.';
|
|
175
|
+
const md = [
|
|
176
|
+
'---',
|
|
177
|
+
'name: summarize-diff',
|
|
178
|
+
'description: Slash command for PR summaries',
|
|
179
|
+
'---',
|
|
180
|
+
'',
|
|
181
|
+
body,
|
|
182
|
+
].join('\n');
|
|
183
|
+
const toml = mdToGeminiCommandToml(md);
|
|
184
|
+
const parsed = parseSimpleToml(toml);
|
|
185
|
+
assert.equal(parsed.name, 'summarize-diff');
|
|
186
|
+
assert.equal(parsed.description, 'Slash command for PR summaries');
|
|
187
|
+
assert.equal(parsed.prompt, body);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ---------------- Empty frontmatter ----------------
|
|
191
|
+
|
|
192
|
+
test('mdToCodexToml handles empty frontmatter (no `---` block)', () => {
|
|
193
|
+
const md = 'Just a body, no frontmatter.';
|
|
194
|
+
const toml = mdToCodexToml(md);
|
|
195
|
+
const parsed = parseSimpleToml(toml);
|
|
196
|
+
assert.equal(parsed.system_prompt, 'Just a body, no frontmatter.');
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ---------------- Multi-line description ----------------
|
|
200
|
+
|
|
201
|
+
test('mdToCodexToml handles multi-line YAML body becoming system_prompt', () => {
|
|
202
|
+
const md = [
|
|
203
|
+
'---',
|
|
204
|
+
'name: multi',
|
|
205
|
+
'description: short',
|
|
206
|
+
'---',
|
|
207
|
+
'',
|
|
208
|
+
'Line one.',
|
|
209
|
+
'Line two.',
|
|
210
|
+
'Line three.',
|
|
211
|
+
].join('\n');
|
|
212
|
+
const toml = mdToCodexToml(md);
|
|
213
|
+
const parsed = parseSimpleToml(toml);
|
|
214
|
+
assert.equal(parsed.name, 'multi');
|
|
215
|
+
assert.equal(parsed.description, 'short');
|
|
216
|
+
assert.equal(parsed.system_prompt, 'Line one.\nLine two.\nLine three.');
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// ---------------- Inline list values ----------------
|
|
220
|
+
|
|
221
|
+
test('inline list value (tools: [bash, read]) round-trips', () => {
|
|
222
|
+
const md = [
|
|
223
|
+
'---',
|
|
224
|
+
'name: t',
|
|
225
|
+
'description: d',
|
|
226
|
+
'tools: [bash, read, write]',
|
|
227
|
+
'---',
|
|
228
|
+
'',
|
|
229
|
+
'body',
|
|
230
|
+
].join('\n');
|
|
231
|
+
const toml = mdToCodexToml(md);
|
|
232
|
+
const parsed = parseSimpleToml(toml);
|
|
233
|
+
assert.deepEqual(parsed.tools, ['bash', 'read', 'write']);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// ---------------- Block-style list values ----------------
|
|
237
|
+
|
|
238
|
+
test('block-style YAML list (`- a` lines) parses identically to inline', () => {
|
|
239
|
+
const md = [
|
|
240
|
+
'---',
|
|
241
|
+
'name: t',
|
|
242
|
+
'description: d',
|
|
243
|
+
'tools:',
|
|
244
|
+
' - bash',
|
|
245
|
+
' - read',
|
|
246
|
+
'---',
|
|
247
|
+
'',
|
|
248
|
+
'body',
|
|
249
|
+
].join('\n');
|
|
250
|
+
const toml = mdToCodexToml(md);
|
|
251
|
+
const parsed = parseSimpleToml(toml);
|
|
252
|
+
assert.deepEqual(parsed.tools, ['bash', 'read']);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// ---------------- Markdown body containing triple quotes ----------------
|
|
256
|
+
|
|
257
|
+
test('body containing triple quotes survives MD → TOML → MD', () => {
|
|
258
|
+
const body = 'Use """fenced""" blocks for code in the response.';
|
|
259
|
+
const md = [
|
|
260
|
+
'---',
|
|
261
|
+
'name: q',
|
|
262
|
+
'description: d',
|
|
263
|
+
'---',
|
|
264
|
+
'',
|
|
265
|
+
body,
|
|
266
|
+
].join('\n');
|
|
267
|
+
const toml = mdToCodexToml(md);
|
|
268
|
+
const parsed = parseSimpleToml(toml);
|
|
269
|
+
assert.equal(parsed.system_prompt, body);
|
|
270
|
+
// And full round-trip
|
|
271
|
+
const back = codexTomlToMd(toml);
|
|
272
|
+
const toml2 = mdToCodexToml(back);
|
|
273
|
+
const parsed2 = parseSimpleToml(toml2);
|
|
274
|
+
assert.equal(parsed2.system_prompt, body);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
// ---------------- Codex extras preserved ----------------
|
|
278
|
+
|
|
279
|
+
test('Codex extras (developer_instructions, sandbox_mode) survive round-trip', () => {
|
|
280
|
+
const original = [
|
|
281
|
+
'name = "codex-only"',
|
|
282
|
+
'description = "uses extras"',
|
|
283
|
+
'sandbox_mode = "read-only"',
|
|
284
|
+
'developer_instructions = """',
|
|
285
|
+
'Use the existing helper module.',
|
|
286
|
+
'Do not network.',
|
|
287
|
+
'"""',
|
|
288
|
+
'system_prompt = """',
|
|
289
|
+
'You are Codex.',
|
|
290
|
+
'"""',
|
|
291
|
+
].join('\n') + '\n';
|
|
292
|
+
const md = codexTomlToMd(original);
|
|
293
|
+
const reemitted = mdToCodexToml(md);
|
|
294
|
+
const a = parseSimpleToml(original);
|
|
295
|
+
const b = parseSimpleToml(reemitted);
|
|
296
|
+
assert.equal(a.sandbox_mode, b.sandbox_mode);
|
|
297
|
+
assert.equal(a.developer_instructions, b.developer_instructions);
|
|
298
|
+
assert.equal(a.system_prompt, b.system_prompt);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
// ---------------- Unterminated frontmatter rejected ----------------
|
|
302
|
+
|
|
303
|
+
test('unterminated YAML frontmatter throws', () => {
|
|
304
|
+
const md = '---\nname: bad\n';
|
|
305
|
+
assert.throws(() => mdToCodexToml(md), /unterminated/);
|
|
306
|
+
});
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subagent format transcoder — ADR-012 §5.
|
|
3
|
+
*
|
|
4
|
+
* Translates between the canonical Markdown + YAML-frontmatter shape used
|
|
5
|
+
* by Claude Code and Gemini CLI and the TOML shape used by Codex CLI.
|
|
6
|
+
* Zero external dependencies: hand-rolled mini-parsers for YAML
|
|
7
|
+
* frontmatter and the TOML keys ADR §5 lists.
|
|
8
|
+
*
|
|
9
|
+
* Supported keys (top-level):
|
|
10
|
+
* - Canonical (round-trips): name, description, tools, model, system_prompt
|
|
11
|
+
* - Codex extras (preserved when present): developer_instructions,
|
|
12
|
+
* sandbox_mode, mcp_servers, skills
|
|
13
|
+
* - Claude/Gemini extras (preserved in YAML, ignored by Codex):
|
|
14
|
+
* temperature, max_turns, timeout_mins
|
|
15
|
+
*
|
|
16
|
+
* Nested table support is limited to `[mcp_servers]` — that's the only
|
|
17
|
+
* realistic surface for subagents. Arrays of tables, inline tables, and
|
|
18
|
+
* comments are not parsed; the transcoder operates on files we generate
|
|
19
|
+
* or files following ADR §5's spec.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
// ---------- Public API ----------
|
|
23
|
+
|
|
24
|
+
export function mdToCodexToml(md: string): string {
|
|
25
|
+
const { frontmatter, body } = parseFrontmatter(md);
|
|
26
|
+
const fm = { ...frontmatter };
|
|
27
|
+
// Body becomes system_prompt unless the frontmatter already has one
|
|
28
|
+
// (in which case body wins — same convention as Gemini/Claude tooling).
|
|
29
|
+
if (body.length > 0) fm.system_prompt = body;
|
|
30
|
+
return emitToml(fm);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function codexTomlToMd(toml: string): string {
|
|
34
|
+
const data = parseToml(toml);
|
|
35
|
+
const body = typeof data.system_prompt === 'string' ? data.system_prompt : '';
|
|
36
|
+
const fmOnly: Record<string, unknown> = { ...data };
|
|
37
|
+
delete fmOnly.system_prompt;
|
|
38
|
+
return emitFrontmatter(fmOnly) + body;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function mdToGeminiCommandToml(md: string): string {
|
|
42
|
+
// Gemini commands are slash commands, not subagents: the body is `prompt`
|
|
43
|
+
// (not `system_prompt`) and frontmatter keys live at the top level.
|
|
44
|
+
const { frontmatter, body } = parseFrontmatter(md);
|
|
45
|
+
const fm: Record<string, unknown> = { ...frontmatter };
|
|
46
|
+
if (body.length > 0) fm.prompt = body;
|
|
47
|
+
return emitToml(fm);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ---------- YAML frontmatter (read) ----------
|
|
51
|
+
|
|
52
|
+
interface Frontmatter {
|
|
53
|
+
frontmatter: Record<string, unknown>;
|
|
54
|
+
body: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const FRONT_DELIM = /^---\s*$/;
|
|
58
|
+
|
|
59
|
+
function parseFrontmatter(md: string): Frontmatter {
|
|
60
|
+
const lines = md.split('\n');
|
|
61
|
+
if (lines.length === 0 || !FRONT_DELIM.test(lines[0] ?? '')) {
|
|
62
|
+
return { frontmatter: {}, body: md };
|
|
63
|
+
}
|
|
64
|
+
let closeIdx = -1;
|
|
65
|
+
for (let i = 1; i < lines.length; i++) {
|
|
66
|
+
if (FRONT_DELIM.test(lines[i] ?? '')) {
|
|
67
|
+
closeIdx = i;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (closeIdx === -1) {
|
|
72
|
+
throw new Error('subagent transcoder: unterminated YAML frontmatter (missing closing `---`)');
|
|
73
|
+
}
|
|
74
|
+
const yamlLines = lines.slice(1, closeIdx);
|
|
75
|
+
const bodyLines = lines.slice(closeIdx + 1);
|
|
76
|
+
// Strip exactly one leading blank line between `---` and body, if present.
|
|
77
|
+
if (bodyLines.length > 0 && bodyLines[0] === '') bodyLines.shift();
|
|
78
|
+
return {
|
|
79
|
+
frontmatter: parseYamlBlock(yamlLines),
|
|
80
|
+
body: bodyLines.join('\n'),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function parseYamlBlock(lines: string[]): Record<string, unknown> {
|
|
85
|
+
const out: Record<string, unknown> = {};
|
|
86
|
+
let i = 0;
|
|
87
|
+
while (i < lines.length) {
|
|
88
|
+
const raw = lines[i] ?? '';
|
|
89
|
+
if (raw.trim() === '') { i++; continue; }
|
|
90
|
+
if (/^\s/.test(raw)) {
|
|
91
|
+
throw new Error(`subagent transcoder: unexpected indented line in YAML frontmatter: ${JSON.stringify(raw)}`);
|
|
92
|
+
}
|
|
93
|
+
const colon = raw.indexOf(':');
|
|
94
|
+
if (colon < 0) {
|
|
95
|
+
throw new Error(`subagent transcoder: malformed YAML line (no \`:\`): ${JSON.stringify(raw)}`);
|
|
96
|
+
}
|
|
97
|
+
const key = raw.slice(0, colon).trim();
|
|
98
|
+
const rest = raw.slice(colon + 1).trim();
|
|
99
|
+
if (rest === '') {
|
|
100
|
+
// Block-style list: subsequent ` - item` lines.
|
|
101
|
+
const list: string[] = [];
|
|
102
|
+
i++;
|
|
103
|
+
while (i < lines.length && /^\s+-\s/.test(lines[i] ?? '')) {
|
|
104
|
+
const item = (lines[i] ?? '').replace(/^\s+-\s+/, '');
|
|
105
|
+
list.push(parseScalar(item) as string);
|
|
106
|
+
i++;
|
|
107
|
+
}
|
|
108
|
+
out[key] = list;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
out[key] = parseScalar(rest);
|
|
112
|
+
i++;
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function parseScalar(s: string): unknown {
|
|
118
|
+
const t = s.trim();
|
|
119
|
+
if (t.startsWith('[') && t.endsWith(']')) {
|
|
120
|
+
// Inline list: `[a, b, "c, d"]` — supports quoted items with commas.
|
|
121
|
+
const inner = t.slice(1, -1);
|
|
122
|
+
return splitInlineList(inner).map((p) => parseScalar(p));
|
|
123
|
+
}
|
|
124
|
+
if (t.startsWith('"') && t.endsWith('"') && t.length >= 2) {
|
|
125
|
+
// Double-quoted: honor JSON escape sequences (\n, \t, \\, \").
|
|
126
|
+
try {
|
|
127
|
+
return JSON.parse(t);
|
|
128
|
+
} catch {
|
|
129
|
+
return t.slice(1, -1);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (t.startsWith("'") && t.endsWith("'") && t.length >= 2) {
|
|
133
|
+
// Single-quoted YAML strings are literal.
|
|
134
|
+
return t.slice(1, -1);
|
|
135
|
+
}
|
|
136
|
+
if (t === 'true') return true;
|
|
137
|
+
if (t === 'false') return false;
|
|
138
|
+
if (t === 'null' || t === '~' || t === '') return null;
|
|
139
|
+
if (/^-?\d+(\.\d+)?$/.test(t)) return Number(t);
|
|
140
|
+
return t;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function splitInlineList(inner: string): string[] {
|
|
144
|
+
const out: string[] = [];
|
|
145
|
+
let buf = '';
|
|
146
|
+
let inQuote: string | null = null;
|
|
147
|
+
for (const ch of inner) {
|
|
148
|
+
if (inQuote) {
|
|
149
|
+
buf += ch;
|
|
150
|
+
if (ch === inQuote) inQuote = null;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (ch === '"' || ch === "'") { inQuote = ch; buf += ch; continue; }
|
|
154
|
+
if (ch === ',') { out.push(buf.trim()); buf = ''; continue; }
|
|
155
|
+
buf += ch;
|
|
156
|
+
}
|
|
157
|
+
if (buf.trim() !== '') out.push(buf.trim());
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ---------- YAML frontmatter (write) ----------
|
|
162
|
+
|
|
163
|
+
function emitFrontmatter(fm: Record<string, unknown>): string {
|
|
164
|
+
const keys = Object.keys(fm);
|
|
165
|
+
if (keys.length === 0) return '---\n---\n';
|
|
166
|
+
const parts: string[] = ['---'];
|
|
167
|
+
for (const k of keys) {
|
|
168
|
+
const v = fm[k];
|
|
169
|
+
if (Array.isArray(v)) {
|
|
170
|
+
// Always emit lists in inline form for stability — round-trip with
|
|
171
|
+
// block-style lists still works because the reader accepts both.
|
|
172
|
+
parts.push(`${k}: [${v.map((it) => emitYamlScalar(it)).join(', ')}]`);
|
|
173
|
+
} else if (v !== undefined && v !== null) {
|
|
174
|
+
parts.push(`${k}: ${emitYamlScalar(v)}`);
|
|
175
|
+
} else if (v === null) {
|
|
176
|
+
parts.push(`${k}: null`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
parts.push('---');
|
|
180
|
+
parts.push('');
|
|
181
|
+
return parts.join('\n');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function emitYamlScalar(v: unknown): string {
|
|
185
|
+
if (typeof v === 'number' || typeof v === 'boolean') return String(v);
|
|
186
|
+
if (v === null || v === undefined) return 'null';
|
|
187
|
+
const s = String(v);
|
|
188
|
+
// Quote when content has YAML-significant chars or leading/trailing space.
|
|
189
|
+
if (/[:#\[\]{},&*?|<>=!%@`]/.test(s) || /^\s|\s$/.test(s) || s === '') {
|
|
190
|
+
return JSON.stringify(s);
|
|
191
|
+
}
|
|
192
|
+
return s;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ---------- TOML (write) ----------
|
|
196
|
+
|
|
197
|
+
function emitToml(data: Record<string, unknown>): string {
|
|
198
|
+
const lines: string[] = [];
|
|
199
|
+
const nested: Array<[string, Record<string, unknown>]> = [];
|
|
200
|
+
for (const k of Object.keys(data)) {
|
|
201
|
+
const v = data[k];
|
|
202
|
+
if (isPlainObject(v)) {
|
|
203
|
+
nested.push([k, v]);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
lines.push(`${k} = ${emitTomlValue(v, k)}`);
|
|
207
|
+
}
|
|
208
|
+
for (const [name, table] of nested) {
|
|
209
|
+
lines.push('');
|
|
210
|
+
lines.push(`[${name}]`);
|
|
211
|
+
for (const tk of Object.keys(table)) {
|
|
212
|
+
lines.push(`${tk} = ${emitTomlValue((table as Record<string, unknown>)[tk], tk)}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return lines.join('\n') + '\n';
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function emitTomlValue(v: unknown, key: string): string {
|
|
219
|
+
if (typeof v === 'string') {
|
|
220
|
+
// Triple-quoted for the prompt-bearing keys + anything multi-line.
|
|
221
|
+
if (key === 'system_prompt' || key === 'prompt' || key === 'developer_instructions' || v.includes('\n')) {
|
|
222
|
+
// Triple-quoted multi-line string per TOML spec: the immediately
|
|
223
|
+
// following newline after the opening `"""` is stripped, so we
|
|
224
|
+
// preserve the body byte-for-byte by placing closing `"""` directly
|
|
225
|
+
// after the body (no separator newline).
|
|
226
|
+
const escaped = v.replace(/"""/g, '"\\""\\""');
|
|
227
|
+
return `"""\n${escaped}"""`;
|
|
228
|
+
}
|
|
229
|
+
return JSON.stringify(v);
|
|
230
|
+
}
|
|
231
|
+
if (typeof v === 'number' || typeof v === 'boolean') return String(v);
|
|
232
|
+
if (v === null || v === undefined) return '""';
|
|
233
|
+
if (Array.isArray(v)) {
|
|
234
|
+
return `[${v.map((it) => emitTomlValue(it, key)).join(', ')}]`;
|
|
235
|
+
}
|
|
236
|
+
// Fallback — should not be hit because objects are routed to nested tables.
|
|
237
|
+
return JSON.stringify(v);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
241
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ---------- TOML (read) ----------
|
|
245
|
+
|
|
246
|
+
function parseToml(toml: string): Record<string, unknown> {
|
|
247
|
+
const out: Record<string, unknown> = {};
|
|
248
|
+
const lines = toml.split('\n');
|
|
249
|
+
let i = 0;
|
|
250
|
+
let currentTable: Record<string, unknown> = out;
|
|
251
|
+
while (i < lines.length) {
|
|
252
|
+
const raw = lines[i] ?? '';
|
|
253
|
+
const trimmed = raw.trim();
|
|
254
|
+
if (trimmed === '' || trimmed.startsWith('#')) { i++; continue; }
|
|
255
|
+
const tableMatch = /^\[([^\]]+)\]$/.exec(trimmed);
|
|
256
|
+
if (tableMatch) {
|
|
257
|
+
const name = (tableMatch[1] ?? '').trim();
|
|
258
|
+
const tbl: Record<string, unknown> = {};
|
|
259
|
+
out[name] = tbl;
|
|
260
|
+
currentTable = tbl;
|
|
261
|
+
i++;
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const eq = raw.indexOf('=');
|
|
265
|
+
if (eq < 0) {
|
|
266
|
+
throw new Error(`subagent transcoder: malformed TOML line: ${JSON.stringify(raw)}`);
|
|
267
|
+
}
|
|
268
|
+
const key = raw.slice(0, eq).trim();
|
|
269
|
+
const restRaw = raw.slice(eq + 1);
|
|
270
|
+
const rest = restRaw.trim();
|
|
271
|
+
if (rest.startsWith('"""')) {
|
|
272
|
+
// Multi-line triple-quoted string. ADR §5 emits these with the
|
|
273
|
+
// opening `"""` on its own line; we accept both shapes.
|
|
274
|
+
const { value, consumed } = parseTripleQuoted(lines, i, eq);
|
|
275
|
+
currentTable[key] = value;
|
|
276
|
+
i += consumed;
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
currentTable[key] = parseTomlScalar(rest);
|
|
280
|
+
i++;
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function parseTripleQuoted(lines: string[], startIdx: number, eqPos: number): { value: string; consumed: number } {
|
|
286
|
+
const first = lines[startIdx] ?? '';
|
|
287
|
+
// Strip the `key = """` prefix from the first line.
|
|
288
|
+
const afterOpen = first.slice(eqPos + 1).trim().slice(3);
|
|
289
|
+
let consumed = 1;
|
|
290
|
+
// Closing on the same line: `key = """text"""`
|
|
291
|
+
if (afterOpen.endsWith('"""') && afterOpen.length >= 3) {
|
|
292
|
+
return { value: unescapeTriple(afterOpen.slice(0, -3)), consumed };
|
|
293
|
+
}
|
|
294
|
+
// Per TOML spec, the newline immediately following the opening `"""`
|
|
295
|
+
// is stripped. We then collect content until the closing `"""` and
|
|
296
|
+
// include the body byte-for-byte (no synthetic trailing `\n`).
|
|
297
|
+
const buf: string[] = [];
|
|
298
|
+
if (afterOpen !== '') buf.push(afterOpen);
|
|
299
|
+
for (let j = startIdx + 1; j < lines.length; j++) {
|
|
300
|
+
const ln = lines[j] ?? '';
|
|
301
|
+
consumed++;
|
|
302
|
+
if (ln.endsWith('"""')) {
|
|
303
|
+
const trailing = ln.slice(0, -3);
|
|
304
|
+
// Closing on its own line — the `\n` before `"""` is part of the
|
|
305
|
+
// body; otherwise the trailing chunk is the body's last line.
|
|
306
|
+
if (trailing === '' && buf.length > 0) {
|
|
307
|
+
return { value: unescapeTriple(buf.join('\n') + '\n'), consumed };
|
|
308
|
+
}
|
|
309
|
+
buf.push(trailing);
|
|
310
|
+
return { value: unescapeTriple(buf.join('\n')), consumed };
|
|
311
|
+
}
|
|
312
|
+
buf.push(ln);
|
|
313
|
+
}
|
|
314
|
+
throw new Error('subagent transcoder: unterminated TOML triple-quoted string');
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function unescapeTriple(s: string): string {
|
|
318
|
+
// Inverse of the `"""` → `"\""\""` escape applied during emit.
|
|
319
|
+
return s.replace(/"\\""\\""/g, '"""');
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function parseTomlScalar(s: string): unknown {
|
|
323
|
+
if (s.startsWith('"') && s.endsWith('"')) {
|
|
324
|
+
return JSON.parse(s);
|
|
325
|
+
}
|
|
326
|
+
if (s.startsWith('[') && s.endsWith(']')) {
|
|
327
|
+
return splitInlineList(s.slice(1, -1)).map((p) => parseTomlScalar(p));
|
|
328
|
+
}
|
|
329
|
+
if (s === 'true') return true;
|
|
330
|
+
if (s === 'false') return false;
|
|
331
|
+
if (/^-?\d+(\.\d+)?$/.test(s)) return Number(s);
|
|
332
|
+
return s;
|
|
333
|
+
}
|