@triflux/remote 10.50.1 → 10.50.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.
@@ -1,132 +0,0 @@
1
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
2
- import { createRequire } from "node:module";
3
- import { dirname } from "node:path";
4
- import { sanitizeCodexProfileConfig } from "./codex-profile-config.mjs";
5
-
6
- const require = createRequire(import.meta.url);
7
- let TOML = null;
8
- try {
9
- TOML = require("@iarna/toml");
10
- } catch {
11
- TOML = null;
12
- }
13
-
14
- function walk(value, visitor) {
15
- if (!value || typeof value !== "object") return;
16
- for (const [key, child] of Object.entries(value)) {
17
- visitor(value, key, child);
18
- walk(child, visitor);
19
- }
20
- }
21
-
22
- export function parseToml(source) {
23
- if (!TOML) return parseTomlFallback(source);
24
- return TOML.parse(source || "");
25
- }
26
-
27
- export function stringifyToml(value) {
28
- if (!TOML) return stringifyTomlFallback(value);
29
- return TOML.stringify(value);
30
- }
31
-
32
- function parseTomlFallback(source = "") {
33
- const root = {};
34
- let current = root;
35
- for (const rawLine of String(source).split(/\r?\n/)) {
36
- const line = rawLine.trim();
37
- if (!line || line.startsWith("#")) continue;
38
- const section = line.match(/^\[([^\]]+)\]$/);
39
- if (section) {
40
- current = root;
41
- for (const part of section[1].split(".")) {
42
- current[part] ??= {};
43
- current = current[part];
44
- }
45
- continue;
46
- }
47
- const assignment = line.match(/^([A-Za-z0-9_-]+)\s*=\s*"([^"]*)"$/);
48
- if (assignment) {
49
- current[assignment[1]] = assignment[2];
50
- }
51
- }
52
- return root;
53
- }
54
-
55
- function stringifyTomlFallback(value) {
56
- const lines = [];
57
- for (const [key, child] of Object.entries(value)) {
58
- if (child && typeof child === "object") continue;
59
- lines.push(`${key} = "${String(child)}"`);
60
- }
61
- function writeSections(prefix, obj) {
62
- for (const [key, child] of Object.entries(obj)) {
63
- if (!child || typeof child !== "object") continue;
64
- const section = [...prefix, key];
65
- lines.push("", `[${section.join(".")}]`);
66
- for (const [childKey, childValue] of Object.entries(child)) {
67
- if (childValue && typeof childValue === "object") continue;
68
- lines.push(`${childKey} = "${String(childValue)}"`);
69
- }
70
- writeSections(section, child);
71
- }
72
- }
73
- writeSections([], value);
74
- return `${lines.join("\n")}\n`;
75
- }
76
-
77
- export function detectTopLevelCodexConfig(source) {
78
- const parsed = parseToml(source);
79
- return {
80
- hasTopLevelSandboxOrApproval:
81
- Object.hasOwn(parsed, "sandbox") ||
82
- Object.hasOwn(parsed, "approval_mode"),
83
- };
84
- }
85
-
86
- export function patchMcpApprovalMode(source) {
87
- const parsed = parseToml(source);
88
- let count = 0;
89
- walk(parsed.mcp_servers, (parent, key, value) => {
90
- if (key === "approval_mode" && value === "approve") {
91
- parent[key] = "full-auto";
92
- count += 1;
93
- }
94
- });
95
- return {
96
- changed: count > 0,
97
- count,
98
- toml: count > 0 ? stringifyToml(parsed) : source,
99
- };
100
- }
101
-
102
- export function patchCodexConfigFile(path, { now = () => new Date() } = {}) {
103
- if (!path || !existsSync(path)) return { changed: false, count: 0 };
104
- const source = readFileSync(path, "utf8");
105
- const approvalPatched = patchMcpApprovalMode(source);
106
- const profileSanitized = sanitizeCodexProfileConfig(approvalPatched.toml, {
107
- codexHome: dirname(path),
108
- });
109
- const changed = approvalPatched.changed || profileSanitized.changed;
110
- if (!changed) {
111
- return {
112
- changed: false,
113
- count: 0,
114
- removedProfiles: [],
115
- migratedProfiles: [],
116
- };
117
- }
118
- const stamp = now()
119
- .toISOString()
120
- .replace(/[-:]/g, "")
121
- .replace(/\..*$/, "")
122
- .replace("T", "-");
123
- writeFileSync(`${path}.bak-${stamp}`, source);
124
- writeFileSync(path, profileSanitized.toml);
125
- return {
126
- changed: true,
127
- count: approvalPatched.count,
128
- removedProfiles: profileSanitized.removedProfiles,
129
- migratedProfiles: profileSanitized.migratedProfiles,
130
- toml: profileSanitized.toml,
131
- };
132
- }