@ran-sh/dsh-crew 1.1.0 → 1.1.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/package.json
CHANGED
|
@@ -24,6 +24,18 @@ function nonBlank(line) {
|
|
|
24
24
|
function parseProviderMap(source) {
|
|
25
25
|
if (typeof source !== 'string') return { ok: false, code: 'PROVIDER_PROFILE_SCHEMA_UNSUPPORTED' };
|
|
26
26
|
const lines = source.split(/\r?\n/);
|
|
27
|
+
if (source.trim() === '[]') {
|
|
28
|
+
return {
|
|
29
|
+
ok: true,
|
|
30
|
+
style: 'empty-profile',
|
|
31
|
+
lines,
|
|
32
|
+
llmStart: -1,
|
|
33
|
+
blockEnd: lines.length,
|
|
34
|
+
providersLine: -1,
|
|
35
|
+
providerIndent: 0,
|
|
36
|
+
entries: [],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
27
39
|
const llmStart = lines.findIndex((line) => /^-\s+id:\s*llm-pi-ai\s*$/.test(line));
|
|
28
40
|
if (llmStart < 0) return { ok: false, code: 'PROVIDER_PROFILE_SCHEMA_UNSUPPORTED' };
|
|
29
41
|
|
|
@@ -11,14 +11,28 @@ const PROVIDER_ID = /^[A-Za-z0-9][A-Za-z0-9._-]*$/u;
|
|
|
11
11
|
function sha256(value) { return createHash('sha256').update(value, 'utf8').digest('hex'); }
|
|
12
12
|
function indentOf(line) { return (line.match(/^\s*/u) ?? [''])[0].length; }
|
|
13
13
|
function nonBlank(line) { return line.trim() !== '' && !line.trimStart().startsWith('#'); }
|
|
14
|
+
function nextContentLine(lines, start, end = lines.length) {
|
|
15
|
+
for (let index = start; index < end; index += 1) if (nonBlank(lines[index])) return index;
|
|
16
|
+
return -1;
|
|
17
|
+
}
|
|
18
|
+
function unquoteScalar(value) {
|
|
19
|
+
const input = typeof value === 'string' ? value.trim() : '';
|
|
20
|
+
if (!input) return null;
|
|
21
|
+
if (input.startsWith('"') && input.endsWith('"')) {
|
|
22
|
+
try { return JSON.parse(input); } catch { return null; }
|
|
23
|
+
}
|
|
24
|
+
if (input.startsWith("'") && input.endsWith("'")) return input.slice(1, -1).replace(/''/gu, "'");
|
|
25
|
+
return input;
|
|
26
|
+
}
|
|
14
27
|
function scalarField(lines, entry, field) {
|
|
15
|
-
const
|
|
28
|
+
const indent = Number.isInteger(entry?.fieldIndent) ? entry.fieldIndent : null;
|
|
29
|
+
const pattern = new RegExp(`^${indent === null ? '\\s+' : ` {${indent}}`}${field}:\\s*(.*?)\\s*$`);
|
|
16
30
|
for (const line of lines.slice(entry.start, entry.end)) {
|
|
17
31
|
const match = line.match(pattern);
|
|
18
32
|
if (!match) continue;
|
|
19
|
-
const value = match[1].trim();
|
|
33
|
+
const value = (entry?.style === 'flow' ? match[1].replace(/,\s*$/u, '') : match[1]).trim();
|
|
20
34
|
if (!value) return null;
|
|
21
|
-
return value
|
|
35
|
+
return unquoteScalar(value);
|
|
22
36
|
}
|
|
23
37
|
return null;
|
|
24
38
|
}
|
|
@@ -51,6 +65,39 @@ function parseProviderMap(source) {
|
|
|
51
65
|
}
|
|
52
66
|
const providersLine = lines.findIndex((line, index) => index > llmStart && index < blockEnd && /^ {2}providers:\s*(?:\{\s*\})?\s*$/u.test(line));
|
|
53
67
|
if (providersLine < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
68
|
+
const flowOpenLine = nextContentLine(lines, providersLine + 1, blockEnd);
|
|
69
|
+
if (flowOpenLine >= 0 && /^ {4}\{\s*$/u.test(lines[flowOpenLine])) {
|
|
70
|
+
let flowCloseLine = -1;
|
|
71
|
+
const entries = [];
|
|
72
|
+
let index = flowOpenLine + 1;
|
|
73
|
+
while (index < blockEnd) {
|
|
74
|
+
index = nextContentLine(lines, index, blockEnd);
|
|
75
|
+
if (index < 0) break;
|
|
76
|
+
if (/^ {4}\}\s*$/u.test(lines[index])) { flowCloseLine = index; break; }
|
|
77
|
+
const match = lines[index].match(/^ {6}([A-Za-z0-9][A-Za-z0-9._-]*):\s*$/u);
|
|
78
|
+
if (!match) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
79
|
+
const objectOpen = nextContentLine(lines, index + 1, blockEnd);
|
|
80
|
+
if (objectOpen < 0 || !/^ {8}\{\s*$/u.test(lines[objectOpen])) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
81
|
+
let objectClose = -1;
|
|
82
|
+
for (let cursor = objectOpen + 1; cursor < blockEnd; cursor += 1) {
|
|
83
|
+
if (/^ {8}\},?\s*$/u.test(lines[cursor])) { objectClose = cursor; break; }
|
|
84
|
+
}
|
|
85
|
+
if (objectClose < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
86
|
+
entries.push({ id: match[1], start: index, end: objectClose + 1, style: 'flow', fieldIndent: 10, objectOpen, objectClose });
|
|
87
|
+
index = objectClose + 1;
|
|
88
|
+
}
|
|
89
|
+
if (flowCloseLine < 0) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
90
|
+
for (let entryIndex = 0; entryIndex < entries.length - 1; entryIndex += 1) {
|
|
91
|
+
if (!/^ {8}\},\s*$/u.test(lines[entries[entryIndex].objectClose])) return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
92
|
+
}
|
|
93
|
+
if (entries.some((entry) => !validFlowEntryStructure(lines, entry))) {
|
|
94
|
+
return { ok: false, code: 'PROVIDER_SETTINGS_SCHEMA_UNSUPPORTED' };
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
ok: true, style: 'flow', lines, llmStart, blockEnd, providersLine,
|
|
98
|
+
providersBlockEnd: flowCloseLine + 1, flowOpenLine, flowCloseLine, entries,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
54
101
|
let providersBlockEnd = blockEnd;
|
|
55
102
|
for (let index = providersLine + 1; index < blockEnd; index += 1) {
|
|
56
103
|
if (nonBlank(lines[index]) && indentOf(lines[index]) <= 2) { providersBlockEnd = index; break; }
|
|
@@ -74,10 +121,235 @@ function parseProviderMap(source) {
|
|
|
74
121
|
if (nonBlank(next) && indentOf(next) <= providerIndent) break;
|
|
75
122
|
end += 1;
|
|
76
123
|
}
|
|
77
|
-
entries.push({ id: match[1], start: index, end });
|
|
124
|
+
entries.push({ id: match[1], start: index, end, style: 'block', fieldIndent: 6 });
|
|
78
125
|
index = end - 1;
|
|
79
126
|
}
|
|
80
|
-
return { ok: true, lines, llmStart, blockEnd, providersLine, providersBlockEnd, entries };
|
|
127
|
+
return { ok: true, style: 'block', lines, llmStart, blockEnd, providersLine, providersBlockEnd, entries };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function splitFlowItems(source) {
|
|
131
|
+
const items = [];
|
|
132
|
+
let start = 0;
|
|
133
|
+
let braces = 0;
|
|
134
|
+
let brackets = 0;
|
|
135
|
+
let quote = null;
|
|
136
|
+
let escaped = false;
|
|
137
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
138
|
+
const char = source[index];
|
|
139
|
+
if (quote) {
|
|
140
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
141
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
142
|
+
if (char === quote) quote = null;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
146
|
+
if (char === '{') braces += 1;
|
|
147
|
+
else if (char === '}') braces -= 1;
|
|
148
|
+
else if (char === '[') brackets += 1;
|
|
149
|
+
else if (char === ']') brackets -= 1;
|
|
150
|
+
else if (char === ',' && braces === 0 && brackets === 0) {
|
|
151
|
+
items.push(source.slice(start, index).trim());
|
|
152
|
+
start = index + 1;
|
|
153
|
+
}
|
|
154
|
+
if (braces < 0 || brackets < 0) return null;
|
|
155
|
+
}
|
|
156
|
+
if (quote || braces !== 0 || brackets !== 0) return null;
|
|
157
|
+
const tail = source.slice(start).trim();
|
|
158
|
+
if (tail) items.push(tail);
|
|
159
|
+
return items;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseFlowObject(source, depth = 0) {
|
|
163
|
+
if (depth > 8) return null;
|
|
164
|
+
const input = source.trim().replace(/,\s*$/u, '');
|
|
165
|
+
if (!input.startsWith('{') || !input.endsWith('}')) return null;
|
|
166
|
+
const items = splitFlowItems(input.slice(1, -1));
|
|
167
|
+
if (!items) return null;
|
|
168
|
+
const fields = new Map();
|
|
169
|
+
for (const item of items) {
|
|
170
|
+
let splitAt = -1;
|
|
171
|
+
let quote = null;
|
|
172
|
+
let escaped = false;
|
|
173
|
+
for (let index = 0; index < item.length; index += 1) {
|
|
174
|
+
const char = item[index];
|
|
175
|
+
if (quote) {
|
|
176
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
177
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
178
|
+
if (char === quote) quote = null;
|
|
179
|
+
} else if (char === '"' || char === "'") quote = char;
|
|
180
|
+
else if (char === ':') { splitAt = index; break; }
|
|
181
|
+
}
|
|
182
|
+
if (splitAt < 1 || hasUnseparatedFlowMapping(item, splitAt)) return null;
|
|
183
|
+
const key = item.slice(0, splitAt).trim();
|
|
184
|
+
const value = item.slice(splitAt + 1).trim();
|
|
185
|
+
if (!/^[A-Za-z][A-Za-z0-9_-]*$/u.test(key) || !value || fields.has(key)) return null;
|
|
186
|
+
if (value.startsWith('{') && (!value.endsWith('}') || parseFlowObject(value, depth + 1) === null)) return null;
|
|
187
|
+
fields.set(key, value);
|
|
188
|
+
}
|
|
189
|
+
return fields;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function hasUnseparatedFlowMapping(item, firstColon) {
|
|
193
|
+
let braces = 0;
|
|
194
|
+
let brackets = 0;
|
|
195
|
+
let quote = null;
|
|
196
|
+
let escaped = false;
|
|
197
|
+
for (let index = firstColon + 1; index < item.length; index += 1) {
|
|
198
|
+
const char = item[index];
|
|
199
|
+
if (quote) {
|
|
200
|
+
if (quote === '"' && escaped) { escaped = false; continue; }
|
|
201
|
+
if (quote === '"' && char === '\\') { escaped = true; continue; }
|
|
202
|
+
if (char === quote) quote = null;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (char === '"' || char === "'") { quote = char; continue; }
|
|
206
|
+
if (char === '{') braces += 1;
|
|
207
|
+
else if (char === '}') braces -= 1;
|
|
208
|
+
else if (char === '[') brackets += 1;
|
|
209
|
+
else if (char === ']') brackets -= 1;
|
|
210
|
+
else if (char === ':' && braces === 0 && brackets === 0 && /\s/u.test(item[index + 1] ?? '')) return true;
|
|
211
|
+
if (braces < 0 || brackets < 0) return true;
|
|
212
|
+
}
|
|
213
|
+
return quote !== null || braces !== 0 || brackets !== 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function validFlowEntryStructure(lines, entry) {
|
|
217
|
+
const fields = [];
|
|
218
|
+
let cursor = entry.objectOpen + 1;
|
|
219
|
+
while (cursor < entry.objectClose) {
|
|
220
|
+
cursor = nextContentLine(lines, cursor, entry.objectClose);
|
|
221
|
+
if (cursor < 0) break;
|
|
222
|
+
const line = lines[cursor];
|
|
223
|
+
if (indentOf(line) !== entry.fieldIndent) return false;
|
|
224
|
+
const field = line.match(/^\s+([A-Za-z][A-Za-z0-9_-]*):\s*(.*?)\s*$/u);
|
|
225
|
+
if (!field) return false;
|
|
226
|
+
let terminator = cursor;
|
|
227
|
+
if (!field[2]) {
|
|
228
|
+
const listOpen = nextContentLine(lines, cursor + 1, entry.objectClose);
|
|
229
|
+
if (listOpen < 0 || indentOf(lines[listOpen]) !== entry.fieldIndent + 2 || !/^\s*\[\s*$/u.test(lines[listOpen])) return false;
|
|
230
|
+
const items = [];
|
|
231
|
+
let listClose = -1;
|
|
232
|
+
for (let nested = listOpen + 1; nested < entry.objectClose; nested += 1) {
|
|
233
|
+
if (!nonBlank(lines[nested])) continue;
|
|
234
|
+
if (indentOf(lines[nested]) === entry.fieldIndent + 2 && /^\s*\]\s*,?\s*$/u.test(lines[nested])) {
|
|
235
|
+
listClose = nested;
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
if (indentOf(lines[nested]) !== entry.fieldIndent + 4 || !parseFlowObject(lines[nested])) return false;
|
|
239
|
+
items.push(nested);
|
|
240
|
+
}
|
|
241
|
+
if (listClose < 0) return false;
|
|
242
|
+
for (let index = 0; index < items.length - 1; index += 1) if (!/,\s*$/u.test(lines[items[index]])) return false;
|
|
243
|
+
terminator = listClose;
|
|
244
|
+
cursor = listClose + 1;
|
|
245
|
+
} else {
|
|
246
|
+
cursor += 1;
|
|
247
|
+
}
|
|
248
|
+
fields.push({ terminator });
|
|
249
|
+
}
|
|
250
|
+
for (let index = 0; index < fields.length - 1; index += 1) {
|
|
251
|
+
if (!/,\s*$/u.test(lines[fields[index].terminator])) return false;
|
|
252
|
+
}
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function parseFlowModel(source) {
|
|
257
|
+
const fields = parseFlowObject(source);
|
|
258
|
+
if (!fields) return null;
|
|
259
|
+
const allowed = new Set(['id', 'name', 'contextWindow', 'maxTokens', 'input', 'reasoningEfforts', 'compat']);
|
|
260
|
+
if ([...fields.keys()].some((key) => !allowed.has(key))) return null;
|
|
261
|
+
const id = unquoteScalar(fields.get('id'));
|
|
262
|
+
if (!id || id.length > 256 || /[\r\n]/u.test(id)) return null;
|
|
263
|
+
const model = { id };
|
|
264
|
+
if (fields.has('name')) {
|
|
265
|
+
const name = unquoteScalar(fields.get('name'));
|
|
266
|
+
if (!name || name.length > 256 || /[\r\n]/u.test(name)) return null;
|
|
267
|
+
model.name = name;
|
|
268
|
+
}
|
|
269
|
+
for (const [sourceKey, targetKey] of [['contextWindow', 'context_window'], ['maxTokens', 'max_tokens']]) {
|
|
270
|
+
if (!fields.has(sourceKey)) continue;
|
|
271
|
+
const value = Number(unquoteScalar(fields.get(sourceKey)));
|
|
272
|
+
if (!Number.isSafeInteger(value) || value <= 0) return null;
|
|
273
|
+
model[targetKey] = value;
|
|
274
|
+
}
|
|
275
|
+
if (fields.has('input')) {
|
|
276
|
+
const input = fields.get('input').trim();
|
|
277
|
+
if (!input.startsWith('[') || !input.endsWith(']')) return null;
|
|
278
|
+
const modalities = splitFlowItems(input.slice(1, -1));
|
|
279
|
+
if (!modalities) return null;
|
|
280
|
+
const normalized = modalities.map(unquoteScalar);
|
|
281
|
+
if (normalized.some((value) => !['text', 'image'].includes(value))) return null;
|
|
282
|
+
model.input = [...new Set(normalized)];
|
|
283
|
+
}
|
|
284
|
+
if (fields.has('reasoningEfforts')) {
|
|
285
|
+
const efforts = parseFlowObject(fields.get('reasoningEfforts'));
|
|
286
|
+
if (!efforts) return null;
|
|
287
|
+
const normalized = {};
|
|
288
|
+
for (const [key, raw] of efforts) {
|
|
289
|
+
const value = unquoteScalar(raw);
|
|
290
|
+
if (raw === 'null' || raw === '~') normalized[key] = null;
|
|
291
|
+
else if (!value || value.length > 256 || /[\r\n]/u.test(value)) return null;
|
|
292
|
+
else normalized[key] = value;
|
|
293
|
+
}
|
|
294
|
+
model.reasoning_efforts = normalized;
|
|
295
|
+
}
|
|
296
|
+
if (fields.has('compat')) {
|
|
297
|
+
if (fields.get('compat').trim() !== '{}') return null;
|
|
298
|
+
model.compat = {};
|
|
299
|
+
}
|
|
300
|
+
return model;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function readFlowMaterialization(parsed, entry, file) {
|
|
304
|
+
const directFields = new Set();
|
|
305
|
+
for (const line of parsed.lines.slice(entry.objectOpen + 1, entry.objectClose)) {
|
|
306
|
+
if (!nonBlank(line) || indentOf(line) !== entry.fieldIndent) continue;
|
|
307
|
+
const match = line.match(/^\s+([A-Za-z][A-Za-z0-9_-]*):/u);
|
|
308
|
+
if (!match || directFields.has(match[1])) return { ok: false, code: 'PROVIDER_MATERIALIZATION_UNSUPPORTED_FIELDS' };
|
|
309
|
+
directFields.add(match[1]);
|
|
310
|
+
}
|
|
311
|
+
const allowed = new Set(['displayName', 'apiKeyEnv', 'api', 'baseURL', 'models']);
|
|
312
|
+
if ([...directFields].some((field) => !allowed.has(field))) return { ok: false, code: 'PROVIDER_MATERIALIZATION_UNSUPPORTED_FIELDS' };
|
|
313
|
+
const rawDisplayName = scalarField(parsed.lines, entry, 'displayName');
|
|
314
|
+
const displayName = rawDisplayName && rawDisplayName.length <= 256 && !/[\r\n]/u.test(rawDisplayName) ? rawDisplayName : entry.id;
|
|
315
|
+
const rawCredentialRef = scalarField(parsed.lines, entry, 'apiKeyEnv');
|
|
316
|
+
const credential = classifyCredentialReference(rawCredentialRef, { kind: 'env' });
|
|
317
|
+
if (credential.redacted) return { ok: false, code: 'PROVIDER_CREDENTIAL_REFERENCE_UNSAFE' };
|
|
318
|
+
const api = scalarField(parsed.lines, entry, 'api');
|
|
319
|
+
if (api && (!/^[A-Za-z][A-Za-z0-9._-]{0,127}$/u.test(api) || /^(?:sk|pk|rk|token|secret)[_-]/iu.test(api))) return { ok: false, code: 'PROVIDER_API_SCHEMA_UNSUPPORTED' };
|
|
320
|
+
const rawBaseUrl = scalarField(parsed.lines, entry, 'baseURL');
|
|
321
|
+
const baseUrl = rawBaseUrl && rawBaseUrl.length <= 2048 && !/[\r\n]/u.test(rawBaseUrl) ? rawBaseUrl : null;
|
|
322
|
+
if (baseUrl) {
|
|
323
|
+
try {
|
|
324
|
+
const url = new URL(baseUrl);
|
|
325
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search || url.hash || !url.hostname) return { ok: false, code: 'PROVIDER_BASE_URL_UNSAFE' };
|
|
326
|
+
} catch { return { ok: false, code: 'PROVIDER_BASE_URL_UNSAFE' }; }
|
|
327
|
+
}
|
|
328
|
+
const models = [];
|
|
329
|
+
if (directFields.has('models')) {
|
|
330
|
+
const modelsLine = parsed.lines.findIndex((line, index) => index > entry.objectOpen && index < entry.objectClose && /^ {10}models:\s*$/u.test(line));
|
|
331
|
+
const listOpen = nextContentLine(parsed.lines, modelsLine + 1, entry.objectClose);
|
|
332
|
+
if (modelsLine < 0 || listOpen < 0 || !/^ {12}\[\s*$/u.test(parsed.lines[listOpen])) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
333
|
+
let listClose = -1;
|
|
334
|
+
for (let index = listOpen + 1; index < entry.objectClose; index += 1) {
|
|
335
|
+
if (/^ {12}\]\s*,?\s*$/u.test(parsed.lines[index])) { listClose = index; break; }
|
|
336
|
+
if (!nonBlank(parsed.lines[index])) continue;
|
|
337
|
+
if (indentOf(parsed.lines[index]) !== 14) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
338
|
+
const model = parseFlowModel(parsed.lines[index]);
|
|
339
|
+
if (!model || models.some((candidate) => candidate.id === model.id)) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
340
|
+
models.push(model);
|
|
341
|
+
}
|
|
342
|
+
if (listClose < 0) return { ok: false, code: 'PROVIDER_MODEL_SCHEMA_UNSUPPORTED' };
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
ok: true,
|
|
346
|
+
provider: {
|
|
347
|
+
id: entry.id, display_name: displayName,
|
|
348
|
+
...(credential.value ? { credential_ref: credential.value } : {}),
|
|
349
|
+
...(api ? { api } : {}), ...(baseUrl ? { base_url: baseUrl } : {}),
|
|
350
|
+
models: models.slice(0, 256), source_file: file,
|
|
351
|
+
},
|
|
352
|
+
};
|
|
81
353
|
}
|
|
82
354
|
|
|
83
355
|
const SENSITIVE_CREDENTIAL_FIELD = /(?:api[_-]?key|access[_-]?token|refresh[_-]?token|(?:^|[_-])token(?:$|[_-])|secret|password|authorization|credential|private[_-]?key|client[_-]?secret|cookie|bearer|webhook)/iu;
|
|
@@ -142,6 +414,7 @@ export function readProviderSettingsMaterialization(source, { providerId, file =
|
|
|
142
414
|
if (!parsed.ok) return { ok: false, code: parsed.code };
|
|
143
415
|
const entry = parsed.entries.find((candidate) => candidate.id === providerId);
|
|
144
416
|
if (!entry) return { ok: false, code: 'PROVIDER_NOT_FOUND' };
|
|
417
|
+
if (parsed.style === 'flow') return readFlowMaterialization(parsed, entry, file);
|
|
145
418
|
const block = parsed.lines.slice(entry.start, entry.end).map((line) => ` ${line}`);
|
|
146
419
|
const synthetic = ['- id: llm-pi-ai', ' config:', ' providers:', ...block, ''].join('\n');
|
|
147
420
|
const result = readProviderMaterialization(synthetic, { providerId, file });
|
|
@@ -159,6 +432,47 @@ function yamlScalar(value, max = 2048) {
|
|
|
159
432
|
return JSON.stringify(value.trim());
|
|
160
433
|
}
|
|
161
434
|
|
|
435
|
+
function flowModelLine(model) {
|
|
436
|
+
const fields = [`id: ${yamlScalar(model.id, 256)}`];
|
|
437
|
+
const name = yamlScalar(model.name, 256);
|
|
438
|
+
if (name) fields.push(`name: ${name}`);
|
|
439
|
+
if (Number.isSafeInteger(model.context_window) && model.context_window > 0) fields.push(`contextWindow: ${model.context_window}`);
|
|
440
|
+
if (Number.isSafeInteger(model.max_tokens) && model.max_tokens > 0) fields.push(`maxTokens: ${model.max_tokens}`);
|
|
441
|
+
if (Array.isArray(model.input) && model.input.every((value) => value === 'text' || value === 'image')) fields.push(`input: [${[...new Set(model.input)].join(', ')}]`);
|
|
442
|
+
if (model.reasoning_efforts && typeof model.reasoning_efforts === 'object' && !Array.isArray(model.reasoning_efforts)) {
|
|
443
|
+
const efforts = [];
|
|
444
|
+
for (const [key, value] of Object.entries(model.reasoning_efforts)) {
|
|
445
|
+
if (/^[A-Za-z][A-Za-z0-9_-]*$/u.test(key) && (value === null || yamlScalar(value, 256))) efforts.push(`${key}: ${value === null ? 'null' : yamlScalar(value, 256)}`);
|
|
446
|
+
}
|
|
447
|
+
if (efforts.length > 0) fields.push(`reasoningEfforts: { ${efforts.join(', ')} }`);
|
|
448
|
+
}
|
|
449
|
+
if (model.compat && typeof model.compat === 'object' && !Array.isArray(model.compat) && Object.keys(model.compat).length === 0) fields.push('compat: {}');
|
|
450
|
+
return `{ ${fields.join(', ')} }`;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function flowProviderLines(provider) {
|
|
454
|
+
const displayName = yamlScalar(provider.display_name ?? provider.id, 256);
|
|
455
|
+
if (!displayName) return null;
|
|
456
|
+
const credential = provider.credential_ref ? classifyCredentialReference(provider.credential_ref, { kind: 'env' }).value : null;
|
|
457
|
+
if (provider.credential_ref && !credential) return null;
|
|
458
|
+
const api = yamlScalar(provider.api, 128);
|
|
459
|
+
const baseUrl = yamlScalar(provider.base_url, 2048);
|
|
460
|
+
const models = Array.isArray(provider.models)
|
|
461
|
+
? provider.models.filter((model) => model && typeof model.id === 'string' && yamlScalar(model.id, 256)).slice(0, 256)
|
|
462
|
+
: [];
|
|
463
|
+
const fields = [` displayName: ${displayName}`];
|
|
464
|
+
if (api) fields.push(` api: ${api}`);
|
|
465
|
+
if (baseUrl) fields.push(` baseURL: ${baseUrl}`);
|
|
466
|
+
if (credential) fields.push(` apiKeyEnv: ${yamlScalar(credential, 256)}`);
|
|
467
|
+
if (models.length === 0) {
|
|
468
|
+
return [` ${provider.id}:`, ' {', ...fields.map((line, index) => `${line}${index < fields.length - 1 ? ',' : ''}`), ' }'];
|
|
469
|
+
}
|
|
470
|
+
const lines = [` ${provider.id}:`, ' {', ...fields.map((line) => `${line},`), ' models:', ' ['];
|
|
471
|
+
for (let index = 0; index < models.length; index += 1) lines.push(` ${flowModelLine(models[index])}${index < models.length - 1 ? ',' : ''}`);
|
|
472
|
+
lines.push(' ]', ' }');
|
|
473
|
+
return lines;
|
|
474
|
+
}
|
|
475
|
+
|
|
162
476
|
/**
|
|
163
477
|
* Add one already-sanitized provider projection to the Harness user layer.
|
|
164
478
|
* The operation is pure and revision-bound; callers perform the atomic write.
|
|
@@ -172,6 +486,17 @@ export function addProviderSettings(source, { provider, expectedRevision } = {})
|
|
|
172
486
|
const parsed = parseProviderMap(source);
|
|
173
487
|
if (!parsed.ok) return { ok: false, code: parsed.code, revision: currentRevision };
|
|
174
488
|
if (parsed.entries.some((entry) => entry.id === provider.id)) return { ok: false, code: 'PROVIDER_SETTINGS_PROVIDER_EXISTS', revision: currentRevision };
|
|
489
|
+
if (parsed.style === 'flow') {
|
|
490
|
+
const providerLines = flowProviderLines(provider);
|
|
491
|
+
if (!providerLines) return { ok: false, code: 'PROVIDER_MATERIALIZATION_INVALID', revision: currentRevision };
|
|
492
|
+
const nextLines = [...parsed.lines];
|
|
493
|
+
const previous = parsed.entries.at(-1);
|
|
494
|
+
if (previous && !/,\s*$/u.test(nextLines[previous.objectClose])) nextLines[previous.objectClose] = `${nextLines[previous.objectClose]},`;
|
|
495
|
+
nextLines.splice(parsed.flowCloseLine, 0, ...providerLines);
|
|
496
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
497
|
+
const text = nextLines.join(newline);
|
|
498
|
+
return { ok: true, text, added: provider.id, revision: sha256(text) };
|
|
499
|
+
}
|
|
175
500
|
const displayName = yamlScalar(provider.display_name ?? provider.id, 256);
|
|
176
501
|
if (!displayName) return { ok: false, code: 'PROVIDER_MATERIALIZATION_INVALID', revision: currentRevision };
|
|
177
502
|
const lines = [` ${provider.id}:`, ` displayName: ${displayName}`];
|
|
@@ -256,6 +581,23 @@ export function mutateProviderSettings(source, {
|
|
|
256
581
|
}
|
|
257
582
|
const ranges = providerEntry ? [[providerEntry.start, providerEntry.end]] : [];
|
|
258
583
|
const finalProvider = providerEntry && parsed.entries.length === 1;
|
|
584
|
+
if (removeProvider && parsed.style === 'flow' && finalProvider) {
|
|
585
|
+
const lines = [...parsed.lines];
|
|
586
|
+
lines.splice(parsed.providersLine, parsed.flowCloseLine - parsed.providersLine + 1, ' providers: {}');
|
|
587
|
+
if (defaultParsed) {
|
|
588
|
+
const removedBeforeProvider = parsed.providersLine <= defaultParsed.providerLine ? parsed.flowCloseLine - parsed.providersLine : 0;
|
|
589
|
+
const removedBeforeModel = parsed.providersLine <= defaultParsed.modelLine ? parsed.flowCloseLine - parsed.providersLine : 0;
|
|
590
|
+
lines[defaultParsed.providerLine - removedBeforeProvider] = ` provider: ${replacementDefault.trim()}`;
|
|
591
|
+
lines[defaultParsed.modelLine - removedBeforeModel] = ` model: ${replacementModel.trim()}`;
|
|
592
|
+
}
|
|
593
|
+
const newline = source.includes('\r\n') ? '\r\n' : '\n';
|
|
594
|
+
const text = lines.join(newline);
|
|
595
|
+
return {
|
|
596
|
+
ok: true, text, removed: [providerId], remaining: [],
|
|
597
|
+
...(defaultParsed ? { previous_default: { provider: defaultParsed.provider, model: defaultParsed.model } } : {}),
|
|
598
|
+
revision: sha256(text),
|
|
599
|
+
};
|
|
600
|
+
}
|
|
259
601
|
const nextLines = parsed.lines.map((line, index) => {
|
|
260
602
|
if (ranges.some(([start, end]) => index >= start && index < end)) return null;
|
|
261
603
|
if (finalProvider && index === parsed.providersLine) return ' providers: {}';
|
package/src/runtime-identity.mjs
CHANGED
|
@@ -26,7 +26,7 @@ export const PRODUCTION_PROFILE = 'dsh-crew';
|
|
|
26
26
|
export const PRODUCTION_LISTEN_PORT = 3210;
|
|
27
27
|
const RUNTIME_ID = randomUUID();
|
|
28
28
|
|
|
29
|
-
export const RUNTIME_VERSION = '1.1.
|
|
29
|
+
export const RUNTIME_VERSION = '1.1.1';
|
|
30
30
|
export const HUB_PROTOCOL_VERSION = 1;
|
|
31
31
|
|
|
32
32
|
export const HUB_CAPABILITIES = Object.freeze([
|
|
@@ -117,13 +117,22 @@ $crewHeartbeatFile = Join-Path $crewSupervisorRoot 'heartbeat.json'
|
|
|
117
117
|
$crewRequestsDir = Join-Path $crewSupervisorRoot 'restart-requests'
|
|
118
118
|
$crewResultsDir = Join-Path $crewSupervisorRoot 'restart-results'
|
|
119
119
|
|
|
120
|
+
# UTF-8 WITHOUT BOM: Windows PowerShell 5.1's Set-Content -Encoding UTF8
|
|
121
|
+
# writes a BOM, and the Node hub's JSON.parse rejects it (heartbeat/request
|
|
122
|
+
# files would be unreadable -> supervisor judged unavailable).
|
|
123
|
+
function Write-Utf8NoBom {
|
|
124
|
+
param([string] $Path, [string] $Content)
|
|
125
|
+
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
|
|
126
|
+
[System.IO.File]::WriteAllText($Path, $Content, $utf8NoBom)
|
|
127
|
+
}
|
|
128
|
+
|
|
120
129
|
function Write-SupervisorHeartbeat {
|
|
121
130
|
$now = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
|
|
122
131
|
$record = @{ schema_version = 1; pid = $PID; last_seen = $now; protocol_version = 1 } | ConvertTo-Json -Compress
|
|
123
132
|
try {
|
|
124
133
|
if (-not (Test-Path -LiteralPath $crewSupervisorRoot -PathType Container)) { New-Item -ItemType Directory -Path $crewSupervisorRoot -Force | Out-Null }
|
|
125
134
|
$temp = Join-Path $crewSupervisorRoot ("heartbeat.{0}.tmp" -f $PID)
|
|
126
|
-
|
|
135
|
+
Write-Utf8NoBom -Path $temp -Content $record
|
|
127
136
|
Move-Item -LiteralPath $temp -Destination $crewHeartbeatFile -Force
|
|
128
137
|
} catch { /* heartbeat is best-effort */ }
|
|
129
138
|
}
|
|
@@ -157,7 +166,7 @@ function Write-RestartResult {
|
|
|
157
166
|
} | ConvertTo-Json -Depth 5
|
|
158
167
|
$file = Join-Path $crewResultsDir ("{0}.json" -f $Request.request_id)
|
|
159
168
|
$temp = Join-Path $crewResultsDir ("{0}.{1}.tmp" -f $Request.request_id, $PID)
|
|
160
|
-
|
|
169
|
+
Write-Utf8NoBom -Path $temp -Content $result
|
|
161
170
|
Move-Item -LiteralPath $temp -Destination $file -Force
|
|
162
171
|
} catch { /* best effort */ }
|
|
163
172
|
Remove-Item -LiteralPath (Join-Path $crewRequestsDir ("{0}.json" -f $Request.request_id)) -Force -ErrorAction SilentlyContinue
|
|
@@ -253,7 +262,7 @@ function Set-MaintenanceSession {
|
|
|
253
262
|
request_id = $Request.request_id
|
|
254
263
|
} | ConvertTo-Json -Compress
|
|
255
264
|
$temp = Join-Path $crewSupervisorRoot ("maintenance-session.{0}.tmp" -f $PID)
|
|
256
|
-
|
|
265
|
+
Write-Utf8NoBom -Path $temp -Content $session
|
|
257
266
|
Move-Item -LiteralPath $temp -Destination $crewMaintenanceSessionFile -Force
|
|
258
267
|
$back = Read-MaintenanceSession
|
|
259
268
|
if ($back -is [string]) { return $false }
|
|
@@ -304,7 +313,7 @@ function Write-MaintenanceResult {
|
|
|
304
313
|
} | ConvertTo-Json -Depth 5
|
|
305
314
|
$file = Join-Path $crewMaintenanceResultsDir ("{0}.json" -f $Request.request_id)
|
|
306
315
|
$temp = Join-Path $crewMaintenanceResultsDir ("{0}.{1}.tmp" -f $Request.request_id, $PID)
|
|
307
|
-
|
|
316
|
+
Write-Utf8NoBom -Path $temp -Content $result
|
|
308
317
|
Move-Item -LiteralPath $temp -Destination $file -Force
|
|
309
318
|
} catch { /* best effort */ }
|
|
310
319
|
Remove-Item -LiteralPath (Join-Path $crewMaintenanceRequestsDir ("{0}.json" -f $Request.request_id)) -Force -ErrorAction SilentlyContinue
|