omp-config-crypt 0.9.1 → 0.9.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/package.json +1 -1
- package/src/provider-store.js +47 -11
package/package.json
CHANGED
package/src/provider-store.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import fs from "node:fs";
|
|
9
9
|
import path from "node:path";
|
|
10
10
|
import os from "node:os";
|
|
11
|
+
import { parseDocument } from "yaml";
|
|
11
12
|
|
|
12
13
|
export function getAgentDir(customRoot) {
|
|
13
14
|
return customRoot || process.env.OMP_CFG_ROOT || path.join(os.homedir(), ".omp", "agent");
|
|
@@ -230,6 +231,27 @@ export function ensureLocalExtension(ompDir) {
|
|
|
230
231
|
*/
|
|
231
232
|
export function parseYamlModels(str) {
|
|
232
233
|
if (!str || typeof str !== "string") return {};
|
|
234
|
+
const providers = parseBlockYamlModels(str);
|
|
235
|
+
if (Object.keys(providers).length) return providers;
|
|
236
|
+
// Flow-style (brace/bracket) YAML: delegate to the yaml package.
|
|
237
|
+
return parseFlowYamlModels(str);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function parseFlowYamlModels(str) {
|
|
241
|
+
let doc;
|
|
242
|
+
try {
|
|
243
|
+
doc = parseDocument(str, { indent: true });
|
|
244
|
+
if (doc.errors.length) return {};
|
|
245
|
+
} catch {
|
|
246
|
+
return {};
|
|
247
|
+
}
|
|
248
|
+
const data = doc.toJS();
|
|
249
|
+
const providers = data && typeof data === "object" && !Array.isArray(data) ? data.providers : null;
|
|
250
|
+
if (!providers || typeof providers !== "object" || Array.isArray(providers)) return {};
|
|
251
|
+
return providers;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function parseBlockYamlModels(str) {
|
|
233
255
|
const lines = str.split("\n");
|
|
234
256
|
const providers = {};
|
|
235
257
|
let curProvider = null;
|
|
@@ -555,26 +577,40 @@ function assertSupportedProviders(providers) {
|
|
|
555
577
|
function assertPublicSource(agentDir) {
|
|
556
578
|
const file = ["models.yml", "models.yaml"].map(name => path.join(agentDir, name)).find(name => fs.existsSync(name));
|
|
557
579
|
if (!file) return;
|
|
580
|
+
const text = fs.readFileSync(file, "utf8");
|
|
581
|
+
// The fixed-field formatter cannot preserve arbitrary YAML. Reject unfamiliar structure,
|
|
582
|
+
// unless the yaml package can parse it into the providers mapping (the next write
|
|
583
|
+
// re-serializes everything in canonical block style anyway).
|
|
584
|
+
try {
|
|
585
|
+
scanBlockSource(text);
|
|
586
|
+
} catch (blockError) {
|
|
587
|
+
if (!parseFlowYamlModels(text) || !Object.keys(parseFlowYamlModels(text)).length) throw blockError;
|
|
588
|
+
assertSupportedProviders(parseFlowYamlModels(text));
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
assertSupportedProviders(readPublicProviders(agentDir));
|
|
592
|
+
}
|
|
593
|
+
function scanBlockSource(text) {
|
|
558
594
|
// The fixed-field formatter cannot preserve arbitrary YAML. Reject unfamiliar structure.
|
|
559
595
|
let section = "", provider = "";
|
|
560
|
-
for (const line of
|
|
561
|
-
const
|
|
562
|
-
if (!
|
|
596
|
+
for (const line of text.split("\n")) {
|
|
597
|
+
const trimmed = line.trim();
|
|
598
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
563
599
|
const indent = line.search(/\S/);
|
|
564
|
-
const field =
|
|
565
|
-
if (indent === 0 &&
|
|
566
|
-
if (indent === 2 && /^[A-Za-z0-9][A-Za-z0-9_-]*:$/.test(
|
|
600
|
+
const field = trimmed.replace(/^-\s*/, "").split(":")[0];
|
|
601
|
+
if (indent === 0 && trimmed === "providers:") { section = "providers"; continue; }
|
|
602
|
+
if (indent === 2 && /^[A-Za-z0-9][A-Za-z0-9_-]*:$/.test(trimmed)) { provider = field; section = "provider"; continue; }
|
|
567
603
|
if (provider && indent === 4 && PROVIDER_FIELDS.has(field)) {
|
|
568
|
-
section = field === "models" &&
|
|
569
|
-
if (section === "provider" && field !== "apiKey" && !
|
|
604
|
+
section = field === "models" && trimmed === "models:" ? "models" : "provider";
|
|
605
|
+
if (section === "provider" && field !== "apiKey" && !trimmed.slice(trimmed.indexOf(":") + 1).trim()) unsupported(`${provider}.${field}`);
|
|
570
606
|
continue;
|
|
571
607
|
}
|
|
572
608
|
if (section === "models" || section === "model" || section === "compat") {
|
|
573
|
-
if (indent === 6 &&
|
|
609
|
+
if (indent === 6 && trimmed.startsWith("- ") && field === "id") { section = "model"; continue; }
|
|
574
610
|
if (indent === 8 && MODEL_FIELDS.has(field)) { section = field === "compat" ? "compat" : "model"; continue; }
|
|
575
|
-
if (indent === 10 && section === "compat" && /^[A-Za-z0-9_-]+:\s*\S/.test(
|
|
611
|
+
if (indent === 10 && section === "compat" && /^[A-Za-z0-9_-]+:\s*\S/.test(trimmed)) continue;
|
|
576
612
|
}
|
|
577
613
|
unsupported(`${provider || "root"}.${field}`);
|
|
578
614
|
}
|
|
579
|
-
assertSupportedProviders(readPublicProviders(agentDir));
|
|
580
615
|
}
|
|
616
|
+
|