@tempoxyz/patron 0.0.10 → 0.0.12
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/dist/bin.js +2 -2
- package/dist/chunks/{-SRYAMUCC.js → -KL2FSIN2.js} +20 -10
- package/dist/chunks/chunk-E4XN5ZK6.js +230 -0
- package/dist/chunks/{chunk-DEUBVELW.js → chunk-EDGGHF4L.js} +4 -198
- package/dist/chunks/codex-UEAW7Y4U.js +29 -0
- package/dist/chunks/{hook-process-ZBZ3WF5I.js → hook-process-2Y5LU62B.js} +4 -2
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -4,10 +4,10 @@ const require = __patronCreateRequire(import.meta.url)
|
|
|
4
4
|
|
|
5
5
|
// src/bin.ts
|
|
6
6
|
if (process.argv[2] === "hook") {
|
|
7
|
-
const { runHookProcess } = await import("./chunks/hook-process-
|
|
7
|
+
const { runHookProcess } = await import("./chunks/hook-process-2Y5LU62B.js");
|
|
8
8
|
await runHookProcess().catch(() => void 0);
|
|
9
9
|
process.exit(0);
|
|
10
10
|
} else {
|
|
11
|
-
const { run } = await import("./chunks/-
|
|
11
|
+
const { run } = await import("./chunks/-KL2FSIN2.js");
|
|
12
12
|
process.exitCode = await run(process.argv.slice(2));
|
|
13
13
|
}
|
|
@@ -13,32 +13,34 @@ import {
|
|
|
13
13
|
budgetTokens,
|
|
14
14
|
buyTokens,
|
|
15
15
|
clampTokensPerPage,
|
|
16
|
-
codexHome,
|
|
17
|
-
codexPaths,
|
|
18
16
|
formatUsd,
|
|
19
|
-
hasPatronHandler,
|
|
20
|
-
installCodex,
|
|
21
|
-
isObject,
|
|
22
|
-
isPatronHandler,
|
|
23
17
|
loadIssuer,
|
|
24
18
|
needsRefill,
|
|
25
19
|
normalizePayee,
|
|
26
20
|
parseUsd,
|
|
27
21
|
profileMessage,
|
|
28
22
|
purchasableTokens,
|
|
29
|
-
readJsonObject,
|
|
30
23
|
redeem,
|
|
31
|
-
removeCodex,
|
|
32
24
|
resolveSettings,
|
|
33
25
|
settingsFor,
|
|
34
26
|
startRefill,
|
|
35
27
|
tipTokens,
|
|
36
|
-
tokensToUnits
|
|
28
|
+
tokensToUnits
|
|
29
|
+
} from "./chunk-EDGGHF4L.js";
|
|
30
|
+
import {
|
|
31
|
+
codexHome,
|
|
32
|
+
codexPaths,
|
|
33
|
+
hasPatronHandler,
|
|
34
|
+
installCodex,
|
|
35
|
+
isObject,
|
|
36
|
+
isPatronHandler,
|
|
37
|
+
readJsonObject,
|
|
38
|
+
removeCodex,
|
|
37
39
|
withHooksKey,
|
|
38
40
|
withPatronGroup,
|
|
39
41
|
withoutPatronHandlers,
|
|
40
42
|
writeJsonObject
|
|
41
|
-
} from "./chunk-
|
|
43
|
+
} from "./chunk-E4XN5ZK6.js";
|
|
42
44
|
import {
|
|
43
45
|
AGENT_IDS,
|
|
44
46
|
ensureHome,
|
|
@@ -798,6 +800,14 @@ async function main(deps) {
|
|
|
798
800
|
await status(deps, deps.interactive);
|
|
799
801
|
}
|
|
800
802
|
async function syncAgents(deps, config) {
|
|
803
|
+
if (config.agents.includes("codex")) {
|
|
804
|
+
const { codexPaths: codexPaths2, upgradeCodex } = await import("./codex-UEAW7Y4U.js");
|
|
805
|
+
const paths = codexPaths2(deps.agents.env, deps.agents.home);
|
|
806
|
+
if (await upgradeCodex(paths).catch(() => false)) {
|
|
807
|
+
say(deps, "Updated Patron\u2019s Codex hook; trust it again in /hooks when Codex asks.");
|
|
808
|
+
say(deps);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
801
811
|
const added = (await detectAgents(deps.agents)).filter(
|
|
802
812
|
(agent) => !config.agents.includes(agent)
|
|
803
813
|
);
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { createRequire as __patronCreateRequire } from 'node:module'
|
|
2
|
+
const require = __patronCreateRequire(import.meta.url)
|
|
3
|
+
import {
|
|
4
|
+
writePrivateFile
|
|
5
|
+
} from "./chunk-6TTNL3HT.js";
|
|
6
|
+
|
|
7
|
+
// src/codex.ts
|
|
8
|
+
import { mkdir as mkdir2, readFile as readFile2 } from "node:fs/promises";
|
|
9
|
+
import { dirname as dirname2, join } from "node:path";
|
|
10
|
+
|
|
11
|
+
// src/hook-files.ts
|
|
12
|
+
import { mkdir, readFile } from "node:fs/promises";
|
|
13
|
+
import { dirname } from "node:path";
|
|
14
|
+
function isObject(value) {
|
|
15
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
16
|
+
}
|
|
17
|
+
function isPatronHandler(handler) {
|
|
18
|
+
if (!isObject(handler) || typeof handler.command !== "string") return false;
|
|
19
|
+
return /patron/i.test(handler.command) && /\shook\s*$/.test(handler.command);
|
|
20
|
+
}
|
|
21
|
+
function withoutPatronHandlers(settings, event) {
|
|
22
|
+
const groups = isObject(settings.hooks) ? settings.hooks[event] : void 0;
|
|
23
|
+
if (!isObject(settings.hooks) || !Array.isArray(groups)) return { ...settings };
|
|
24
|
+
let changed = false;
|
|
25
|
+
const kept = [];
|
|
26
|
+
for (const group of groups) {
|
|
27
|
+
if (!isObject(group) || !Array.isArray(group.hooks)) {
|
|
28
|
+
kept.push(group);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const handlers = group.hooks.filter((handler) => !isPatronHandler(handler));
|
|
32
|
+
if (handlers.length === group.hooks.length) kept.push(group);
|
|
33
|
+
else {
|
|
34
|
+
changed = true;
|
|
35
|
+
if (handlers.length > 0) kept.push({ ...group, hooks: handlers });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!changed) return { ...settings };
|
|
39
|
+
return withHooksKey(settings, event, kept);
|
|
40
|
+
}
|
|
41
|
+
function withPatronGroup(settings, event, group) {
|
|
42
|
+
const next = withoutPatronHandlers(settings, event);
|
|
43
|
+
const hooks = isObject(next.hooks) ? next.hooks : {};
|
|
44
|
+
const groups = Array.isArray(hooks[event]) ? hooks[event] : [];
|
|
45
|
+
return withHooksKey(next, event, [...groups, group]);
|
|
46
|
+
}
|
|
47
|
+
function hasPatronHandler(settings, event, options = {}) {
|
|
48
|
+
const groups = isObject(settings.hooks) ? settings.hooks[event] : void 0;
|
|
49
|
+
if (!Array.isArray(groups)) return false;
|
|
50
|
+
return groups.some(
|
|
51
|
+
(group) => isObject(group) && (options.matcher === void 0 || group.matcher === options.matcher) && Array.isArray(group.hooks) && group.hooks.some(
|
|
52
|
+
(handler) => isPatronHandler(handler) && (options.command === void 0 || handler.command === options.command)
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
function withHooksKey(settings, event, entries) {
|
|
57
|
+
const hooks = isObject(settings.hooks) ? { ...settings.hooks } : {};
|
|
58
|
+
if (entries.length > 0) hooks[event] = entries;
|
|
59
|
+
else delete hooks[event];
|
|
60
|
+
const next = { ...settings };
|
|
61
|
+
if (Object.keys(hooks).length > 0) next.hooks = hooks;
|
|
62
|
+
else delete next.hooks;
|
|
63
|
+
return next;
|
|
64
|
+
}
|
|
65
|
+
async function readJsonObject(path) {
|
|
66
|
+
let text;
|
|
67
|
+
try {
|
|
68
|
+
text = await readFile(path, "utf8");
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (error.code === "ENOENT") return {};
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
if (text.trim() === "") return {};
|
|
74
|
+
let value;
|
|
75
|
+
try {
|
|
76
|
+
value = JSON.parse(text);
|
|
77
|
+
} catch {
|
|
78
|
+
throw new Error(`${path} is not valid JSON; fix it, then rerun patron.`);
|
|
79
|
+
}
|
|
80
|
+
if (!isObject(value)) throw new Error(`${path} is not a JSON object; left unchanged.`);
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
async function writeJsonObject(path, value) {
|
|
84
|
+
await mkdir(dirname(path), { recursive: true });
|
|
85
|
+
await writePrivateFile(path, `${JSON.stringify(value, null, 2)}
|
|
86
|
+
`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/codex.ts
|
|
90
|
+
var CODEX_NOTIFY_MARKER = "# added by patron";
|
|
91
|
+
var HOOK_TIMEOUT_SECONDS = 10;
|
|
92
|
+
var EVENT = "Stop";
|
|
93
|
+
function codexHome(env, home) {
|
|
94
|
+
return env.CODEX_HOME || join(home, ".codex");
|
|
95
|
+
}
|
|
96
|
+
function codexPaths(env, home) {
|
|
97
|
+
const root = codexHome(env, home);
|
|
98
|
+
return { config: join(root, "config.toml"), hooks: join(root, "hooks.json") };
|
|
99
|
+
}
|
|
100
|
+
function withCodexStopHook(settings, command) {
|
|
101
|
+
return withPatronGroup(settings, EVENT, stopGroup(command));
|
|
102
|
+
}
|
|
103
|
+
var CODEX_HOOK_STATUS = "Patron: pay the sites this turn read";
|
|
104
|
+
function stopGroup(command) {
|
|
105
|
+
return {
|
|
106
|
+
hooks: [
|
|
107
|
+
{
|
|
108
|
+
command,
|
|
109
|
+
statusMessage: CODEX_HOOK_STATUS,
|
|
110
|
+
timeout: HOOK_TIMEOUT_SECONDS,
|
|
111
|
+
type: "command"
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function hasStopGroup(settings, command) {
|
|
117
|
+
const groups = isObject(settings.hooks) ? settings.hooks[EVENT] : void 0;
|
|
118
|
+
const wanted = JSON.stringify(stopGroup(command));
|
|
119
|
+
return Array.isArray(groups) && groups.some((group) => JSON.stringify(group) === wanted);
|
|
120
|
+
}
|
|
121
|
+
function codexNotifyLine(command) {
|
|
122
|
+
const argv = ["sh", "-c", `${command} "$1"`, "patron"];
|
|
123
|
+
return `notify = [${argv.map((arg) => JSON.stringify(arg)).join(", ")}] ${CODEX_NOTIFY_MARKER}`;
|
|
124
|
+
}
|
|
125
|
+
function withCodexNotify(toml, command) {
|
|
126
|
+
const lines = toml === "" ? [] : toml.split("\n");
|
|
127
|
+
const line = codexNotifyLine(command);
|
|
128
|
+
const index = topLevelNotify(lines);
|
|
129
|
+
if (index === -1)
|
|
130
|
+
return {
|
|
131
|
+
state: "installed",
|
|
132
|
+
text: lines.length === 0 ? `${line}
|
|
133
|
+
` : [line, ...lines].join("\n")
|
|
134
|
+
};
|
|
135
|
+
const current = lines[index] ?? "";
|
|
136
|
+
if (!current.includes(CODEX_NOTIFY_MARKER)) return { state: "taken", text: toml };
|
|
137
|
+
if (current.trimEnd() === line) return { state: "unchanged", text: toml };
|
|
138
|
+
return {
|
|
139
|
+
state: "installed",
|
|
140
|
+
text: lines.map((value, at) => at === index ? line : value).join("\n")
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function withoutCodexNotify(toml) {
|
|
144
|
+
const lines = toml.split("\n");
|
|
145
|
+
const index = topLevelNotify(lines);
|
|
146
|
+
if (index === -1 || !lines[index]?.includes(CODEX_NOTIFY_MARKER)) return void 0;
|
|
147
|
+
return lines.filter((_, at) => at !== index).join("\n");
|
|
148
|
+
}
|
|
149
|
+
function topLevelNotify(lines) {
|
|
150
|
+
for (const [index, line] of lines.entries()) {
|
|
151
|
+
if (/^\s*\[/.test(line)) return -1;
|
|
152
|
+
if (/^\s*notify\s*=/.test(line)) return index;
|
|
153
|
+
}
|
|
154
|
+
return -1;
|
|
155
|
+
}
|
|
156
|
+
async function installCodex(paths, command, platform = process.platform) {
|
|
157
|
+
const settings = await readJsonObject(paths.hooks);
|
|
158
|
+
const hookChanged = !hasStopGroup(settings, command);
|
|
159
|
+
if (hookChanged)
|
|
160
|
+
await writeJsonObject(paths.hooks, withCodexStopHook(settings, command));
|
|
161
|
+
let notify = "taken";
|
|
162
|
+
if (platform !== "win32") {
|
|
163
|
+
const toml = await readText(paths.config);
|
|
164
|
+
const edit = withCodexNotify(toml, command);
|
|
165
|
+
notify = edit.state;
|
|
166
|
+
if (edit.state === "installed") await writeText(paths.config, edit.text);
|
|
167
|
+
}
|
|
168
|
+
const state = hookChanged || notify === "installed" ? "installed" : "unchanged";
|
|
169
|
+
return notify === "taken" ? { note: "open /hooks in Codex and trust it to start paying", state } : { note: "trust it in /hooks when Codex asks", state };
|
|
170
|
+
}
|
|
171
|
+
async function upgradeCodex(paths) {
|
|
172
|
+
const settings = await readJsonObject(paths.hooks);
|
|
173
|
+
const groups = isObject(settings.hooks) ? settings.hooks[EVENT] : void 0;
|
|
174
|
+
const handler = (Array.isArray(groups) ? groups : []).flatMap(
|
|
175
|
+
(group) => isObject(group) && Array.isArray(group.hooks) ? group.hooks : []
|
|
176
|
+
).find(isPatronHandler);
|
|
177
|
+
const command = typeof handler?.command === "string" ? handler.command : void 0;
|
|
178
|
+
if (!command || hasStopGroup(settings, command)) return false;
|
|
179
|
+
await writeJsonObject(paths.hooks, withCodexStopHook(settings, command));
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
async function removeCodex(paths) {
|
|
183
|
+
let removed = false;
|
|
184
|
+
const settings = await readJsonObject(paths.hooks);
|
|
185
|
+
if (hasPatronHandler(settings, EVENT)) {
|
|
186
|
+
await writeJsonObject(paths.hooks, withoutPatronHandlers(settings, EVENT));
|
|
187
|
+
removed = true;
|
|
188
|
+
}
|
|
189
|
+
const toml = await readText(paths.config);
|
|
190
|
+
const without = withoutCodexNotify(toml);
|
|
191
|
+
if (without !== void 0) {
|
|
192
|
+
await writeText(paths.config, without);
|
|
193
|
+
removed = true;
|
|
194
|
+
}
|
|
195
|
+
return removed ? "removed" : "missing";
|
|
196
|
+
}
|
|
197
|
+
async function readText(path) {
|
|
198
|
+
try {
|
|
199
|
+
return await readFile2(path, "utf8");
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (error.code === "ENOENT") return "";
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
async function writeText(path, text) {
|
|
206
|
+
await mkdir2(dirname2(path), { recursive: true });
|
|
207
|
+
await writePrivateFile(path, text);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export {
|
|
211
|
+
isObject,
|
|
212
|
+
isPatronHandler,
|
|
213
|
+
withoutPatronHandlers,
|
|
214
|
+
withPatronGroup,
|
|
215
|
+
hasPatronHandler,
|
|
216
|
+
withHooksKey,
|
|
217
|
+
readJsonObject,
|
|
218
|
+
writeJsonObject,
|
|
219
|
+
CODEX_NOTIFY_MARKER,
|
|
220
|
+
codexHome,
|
|
221
|
+
codexPaths,
|
|
222
|
+
withCodexStopHook,
|
|
223
|
+
CODEX_HOOK_STATUS,
|
|
224
|
+
codexNotifyLine,
|
|
225
|
+
withCodexNotify,
|
|
226
|
+
withoutCodexNotify,
|
|
227
|
+
installCodex,
|
|
228
|
+
upgradeCodex,
|
|
229
|
+
removeCodex
|
|
230
|
+
};
|
|
@@ -12270,7 +12270,7 @@ var $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (inst, def) => {
|
|
|
12270
12270
|
}
|
|
12271
12271
|
return propValues;
|
|
12272
12272
|
});
|
|
12273
|
-
const
|
|
12273
|
+
const isObject2 = isObject;
|
|
12274
12274
|
const catchall = def.catchall;
|
|
12275
12275
|
let value;
|
|
12276
12276
|
const memo2 = globalConfig.memoizer;
|
|
@@ -12278,7 +12278,7 @@ var $ZodObject = /* @__PURE__ */ $constructor("$ZodObject", (inst, def) => {
|
|
|
12278
12278
|
inst._zod.parse = (payload, ctx) => {
|
|
12279
12279
|
value ?? (value = _normalized.value);
|
|
12280
12280
|
const input2 = payload.value;
|
|
12281
|
-
if (!
|
|
12281
|
+
if (!isObject2(input2)) {
|
|
12282
12282
|
payload.issues.push({
|
|
12283
12283
|
expected: "object",
|
|
12284
12284
|
code: "invalid_type",
|
|
@@ -12414,7 +12414,7 @@ var $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) =>
|
|
|
12414
12414
|
return doc.compile();
|
|
12415
12415
|
};
|
|
12416
12416
|
let fastpass;
|
|
12417
|
-
const
|
|
12417
|
+
const isObject2 = isObject;
|
|
12418
12418
|
const jit = !globalConfig.jitless;
|
|
12419
12419
|
const allowsEval2 = allowsEval;
|
|
12420
12420
|
const fastEnabled = jit && allowsEval2.value;
|
|
@@ -12423,7 +12423,7 @@ var $ZodObjectJIT = /* @__PURE__ */ $constructor("$ZodObjectJIT", (inst, def) =>
|
|
|
12423
12423
|
inst._zod.parse = (payload, ctx) => {
|
|
12424
12424
|
value ?? (value = _normalized.value);
|
|
12425
12425
|
const input2 = payload.value;
|
|
12426
|
-
if (!
|
|
12426
|
+
if (!isObject2(input2)) {
|
|
12427
12427
|
payload.issues.push({
|
|
12428
12428
|
expected: "object",
|
|
12429
12429
|
code: "invalid_type",
|
|
@@ -35691,188 +35691,6 @@ function startRefill(script = process.argv[1]) {
|
|
|
35691
35691
|
}
|
|
35692
35692
|
}
|
|
35693
35693
|
|
|
35694
|
-
// src/codex.ts
|
|
35695
|
-
import { mkdir as mkdir2, readFile as readFile3 } from "node:fs/promises";
|
|
35696
|
-
import { dirname as dirname2, join as join2 } from "node:path";
|
|
35697
|
-
|
|
35698
|
-
// src/hook-files.ts
|
|
35699
|
-
import { mkdir, readFile as readFile2 } from "node:fs/promises";
|
|
35700
|
-
import { dirname } from "node:path";
|
|
35701
|
-
function isObject2(value) {
|
|
35702
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
35703
|
-
}
|
|
35704
|
-
function isPatronHandler(handler) {
|
|
35705
|
-
if (!isObject2(handler) || typeof handler.command !== "string") return false;
|
|
35706
|
-
return /patron/i.test(handler.command) && /\shook\s*$/.test(handler.command);
|
|
35707
|
-
}
|
|
35708
|
-
function withoutPatronHandlers(settings, event) {
|
|
35709
|
-
const groups = isObject2(settings.hooks) ? settings.hooks[event] : void 0;
|
|
35710
|
-
if (!isObject2(settings.hooks) || !Array.isArray(groups)) return { ...settings };
|
|
35711
|
-
let changed = false;
|
|
35712
|
-
const kept = [];
|
|
35713
|
-
for (const group of groups) {
|
|
35714
|
-
if (!isObject2(group) || !Array.isArray(group.hooks)) {
|
|
35715
|
-
kept.push(group);
|
|
35716
|
-
continue;
|
|
35717
|
-
}
|
|
35718
|
-
const handlers = group.hooks.filter((handler) => !isPatronHandler(handler));
|
|
35719
|
-
if (handlers.length === group.hooks.length) kept.push(group);
|
|
35720
|
-
else {
|
|
35721
|
-
changed = true;
|
|
35722
|
-
if (handlers.length > 0) kept.push({ ...group, hooks: handlers });
|
|
35723
|
-
}
|
|
35724
|
-
}
|
|
35725
|
-
if (!changed) return { ...settings };
|
|
35726
|
-
return withHooksKey(settings, event, kept);
|
|
35727
|
-
}
|
|
35728
|
-
function withPatronGroup(settings, event, group) {
|
|
35729
|
-
const next = withoutPatronHandlers(settings, event);
|
|
35730
|
-
const hooks = isObject2(next.hooks) ? next.hooks : {};
|
|
35731
|
-
const groups = Array.isArray(hooks[event]) ? hooks[event] : [];
|
|
35732
|
-
return withHooksKey(next, event, [...groups, group]);
|
|
35733
|
-
}
|
|
35734
|
-
function hasPatronHandler(settings, event, options = {}) {
|
|
35735
|
-
const groups = isObject2(settings.hooks) ? settings.hooks[event] : void 0;
|
|
35736
|
-
if (!Array.isArray(groups)) return false;
|
|
35737
|
-
return groups.some(
|
|
35738
|
-
(group) => isObject2(group) && (options.matcher === void 0 || group.matcher === options.matcher) && Array.isArray(group.hooks) && group.hooks.some(
|
|
35739
|
-
(handler) => isPatronHandler(handler) && (options.command === void 0 || handler.command === options.command)
|
|
35740
|
-
)
|
|
35741
|
-
);
|
|
35742
|
-
}
|
|
35743
|
-
function withHooksKey(settings, event, entries) {
|
|
35744
|
-
const hooks = isObject2(settings.hooks) ? { ...settings.hooks } : {};
|
|
35745
|
-
if (entries.length > 0) hooks[event] = entries;
|
|
35746
|
-
else delete hooks[event];
|
|
35747
|
-
const next = { ...settings };
|
|
35748
|
-
if (Object.keys(hooks).length > 0) next.hooks = hooks;
|
|
35749
|
-
else delete next.hooks;
|
|
35750
|
-
return next;
|
|
35751
|
-
}
|
|
35752
|
-
async function readJsonObject(path) {
|
|
35753
|
-
let text;
|
|
35754
|
-
try {
|
|
35755
|
-
text = await readFile2(path, "utf8");
|
|
35756
|
-
} catch (error62) {
|
|
35757
|
-
if (error62.code === "ENOENT") return {};
|
|
35758
|
-
throw error62;
|
|
35759
|
-
}
|
|
35760
|
-
if (text.trim() === "") return {};
|
|
35761
|
-
let value;
|
|
35762
|
-
try {
|
|
35763
|
-
value = JSON.parse(text);
|
|
35764
|
-
} catch {
|
|
35765
|
-
throw new Error(`${path} is not valid JSON; fix it, then rerun patron.`);
|
|
35766
|
-
}
|
|
35767
|
-
if (!isObject2(value)) throw new Error(`${path} is not a JSON object; left unchanged.`);
|
|
35768
|
-
return value;
|
|
35769
|
-
}
|
|
35770
|
-
async function writeJsonObject(path, value) {
|
|
35771
|
-
await mkdir(dirname(path), { recursive: true });
|
|
35772
|
-
await writePrivateFile(path, `${JSON.stringify(value, null, 2)}
|
|
35773
|
-
`);
|
|
35774
|
-
}
|
|
35775
|
-
|
|
35776
|
-
// src/codex.ts
|
|
35777
|
-
var CODEX_NOTIFY_MARKER = "# added by patron";
|
|
35778
|
-
var HOOK_TIMEOUT_SECONDS = 10;
|
|
35779
|
-
var EVENT = "Stop";
|
|
35780
|
-
function codexHome(env, home) {
|
|
35781
|
-
return env.CODEX_HOME || join2(home, ".codex");
|
|
35782
|
-
}
|
|
35783
|
-
function codexPaths(env, home) {
|
|
35784
|
-
const root = codexHome(env, home);
|
|
35785
|
-
return { config: join2(root, "config.toml"), hooks: join2(root, "hooks.json") };
|
|
35786
|
-
}
|
|
35787
|
-
function withCodexStopHook(settings, command) {
|
|
35788
|
-
return withPatronGroup(settings, EVENT, stopGroup(command));
|
|
35789
|
-
}
|
|
35790
|
-
function stopGroup(command) {
|
|
35791
|
-
return { hooks: [{ command, timeout: HOOK_TIMEOUT_SECONDS, type: "command" }] };
|
|
35792
|
-
}
|
|
35793
|
-
function hasStopGroup(settings, command) {
|
|
35794
|
-
const groups = isObject2(settings.hooks) ? settings.hooks[EVENT] : void 0;
|
|
35795
|
-
const wanted = JSON.stringify(stopGroup(command));
|
|
35796
|
-
return Array.isArray(groups) && groups.some((group) => JSON.stringify(group) === wanted);
|
|
35797
|
-
}
|
|
35798
|
-
function codexNotifyLine(command) {
|
|
35799
|
-
const argv = ["sh", "-c", `${command} "$1"`, "patron"];
|
|
35800
|
-
return `notify = [${argv.map((arg) => JSON.stringify(arg)).join(", ")}] ${CODEX_NOTIFY_MARKER}`;
|
|
35801
|
-
}
|
|
35802
|
-
function withCodexNotify(toml, command) {
|
|
35803
|
-
const lines = toml === "" ? [] : toml.split("\n");
|
|
35804
|
-
const line = codexNotifyLine(command);
|
|
35805
|
-
const index = topLevelNotify(lines);
|
|
35806
|
-
if (index === -1)
|
|
35807
|
-
return {
|
|
35808
|
-
state: "installed",
|
|
35809
|
-
text: lines.length === 0 ? `${line}
|
|
35810
|
-
` : [line, ...lines].join("\n")
|
|
35811
|
-
};
|
|
35812
|
-
const current = lines[index] ?? "";
|
|
35813
|
-
if (!current.includes(CODEX_NOTIFY_MARKER)) return { state: "taken", text: toml };
|
|
35814
|
-
if (current.trimEnd() === line) return { state: "unchanged", text: toml };
|
|
35815
|
-
return {
|
|
35816
|
-
state: "installed",
|
|
35817
|
-
text: lines.map((value, at) => at === index ? line : value).join("\n")
|
|
35818
|
-
};
|
|
35819
|
-
}
|
|
35820
|
-
function withoutCodexNotify(toml) {
|
|
35821
|
-
const lines = toml.split("\n");
|
|
35822
|
-
const index = topLevelNotify(lines);
|
|
35823
|
-
if (index === -1 || !lines[index]?.includes(CODEX_NOTIFY_MARKER)) return void 0;
|
|
35824
|
-
return lines.filter((_, at) => at !== index).join("\n");
|
|
35825
|
-
}
|
|
35826
|
-
function topLevelNotify(lines) {
|
|
35827
|
-
for (const [index, line] of lines.entries()) {
|
|
35828
|
-
if (/^\s*\[/.test(line)) return -1;
|
|
35829
|
-
if (/^\s*notify\s*=/.test(line)) return index;
|
|
35830
|
-
}
|
|
35831
|
-
return -1;
|
|
35832
|
-
}
|
|
35833
|
-
async function installCodex(paths, command, platform = process.platform) {
|
|
35834
|
-
const settings = await readJsonObject(paths.hooks);
|
|
35835
|
-
const hookChanged = !hasStopGroup(settings, command);
|
|
35836
|
-
if (hookChanged)
|
|
35837
|
-
await writeJsonObject(paths.hooks, withCodexStopHook(settings, command));
|
|
35838
|
-
let notify = "taken";
|
|
35839
|
-
if (platform !== "win32") {
|
|
35840
|
-
const toml = await readText(paths.config);
|
|
35841
|
-
const edit = withCodexNotify(toml, command);
|
|
35842
|
-
notify = edit.state;
|
|
35843
|
-
if (edit.state === "installed") await writeText(paths.config, edit.text);
|
|
35844
|
-
}
|
|
35845
|
-
const state = hookChanged || notify === "installed" ? "installed" : "unchanged";
|
|
35846
|
-
return notify === "taken" ? { note: "open /hooks in Codex and trust it to start paying", state } : { note: "trust it in /hooks when Codex asks", state };
|
|
35847
|
-
}
|
|
35848
|
-
async function removeCodex(paths) {
|
|
35849
|
-
let removed = false;
|
|
35850
|
-
const settings = await readJsonObject(paths.hooks);
|
|
35851
|
-
if (hasPatronHandler(settings, EVENT)) {
|
|
35852
|
-
await writeJsonObject(paths.hooks, withoutPatronHandlers(settings, EVENT));
|
|
35853
|
-
removed = true;
|
|
35854
|
-
}
|
|
35855
|
-
const toml = await readText(paths.config);
|
|
35856
|
-
const without = withoutCodexNotify(toml);
|
|
35857
|
-
if (without !== void 0) {
|
|
35858
|
-
await writeText(paths.config, without);
|
|
35859
|
-
removed = true;
|
|
35860
|
-
}
|
|
35861
|
-
return removed ? "removed" : "missing";
|
|
35862
|
-
}
|
|
35863
|
-
async function readText(path) {
|
|
35864
|
-
try {
|
|
35865
|
-
return await readFile3(path, "utf8");
|
|
35866
|
-
} catch (error62) {
|
|
35867
|
-
if (error62.code === "ENOENT") return "";
|
|
35868
|
-
throw error62;
|
|
35869
|
-
}
|
|
35870
|
-
}
|
|
35871
|
-
async function writeText(path, text) {
|
|
35872
|
-
await mkdir2(dirname2(path), { recursive: true });
|
|
35873
|
-
await writePrivateFile(path, text);
|
|
35874
|
-
}
|
|
35875
|
-
|
|
35876
35694
|
export {
|
|
35877
35695
|
TOKEN_PRICE_UNITS,
|
|
35878
35696
|
PACK_SIZE,
|
|
@@ -35896,18 +35714,6 @@ export {
|
|
|
35896
35714
|
needsRefill,
|
|
35897
35715
|
parseUsd,
|
|
35898
35716
|
tipTokens,
|
|
35899
|
-
isObject2 as isObject,
|
|
35900
|
-
isPatronHandler,
|
|
35901
|
-
withoutPatronHandlers,
|
|
35902
|
-
withPatronGroup,
|
|
35903
|
-
hasPatronHandler,
|
|
35904
|
-
withHooksKey,
|
|
35905
|
-
readJsonObject,
|
|
35906
|
-
writeJsonObject,
|
|
35907
|
-
codexHome,
|
|
35908
|
-
codexPaths,
|
|
35909
|
-
installCodex,
|
|
35910
|
-
removeCodex,
|
|
35911
35717
|
loadIssuer,
|
|
35912
35718
|
resolveSettings,
|
|
35913
35719
|
settingsFor,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRequire as __patronCreateRequire } from 'node:module'
|
|
2
|
+
const require = __patronCreateRequire(import.meta.url)
|
|
3
|
+
import {
|
|
4
|
+
CODEX_HOOK_STATUS,
|
|
5
|
+
CODEX_NOTIFY_MARKER,
|
|
6
|
+
codexHome,
|
|
7
|
+
codexNotifyLine,
|
|
8
|
+
codexPaths,
|
|
9
|
+
installCodex,
|
|
10
|
+
removeCodex,
|
|
11
|
+
upgradeCodex,
|
|
12
|
+
withCodexNotify,
|
|
13
|
+
withCodexStopHook,
|
|
14
|
+
withoutCodexNotify
|
|
15
|
+
} from "./chunk-E4XN5ZK6.js";
|
|
16
|
+
import "./chunk-6TTNL3HT.js";
|
|
17
|
+
export {
|
|
18
|
+
CODEX_HOOK_STATUS,
|
|
19
|
+
CODEX_NOTIFY_MARKER,
|
|
20
|
+
codexHome,
|
|
21
|
+
codexNotifyLine,
|
|
22
|
+
codexPaths,
|
|
23
|
+
installCodex,
|
|
24
|
+
removeCodex,
|
|
25
|
+
upgradeCodex,
|
|
26
|
+
withCodexNotify,
|
|
27
|
+
withCodexStopHook,
|
|
28
|
+
withoutCodexNotify
|
|
29
|
+
};
|
|
@@ -3,7 +3,6 @@ const require = __patronCreateRequire(import.meta.url)
|
|
|
3
3
|
import {
|
|
4
4
|
FileTokenStore,
|
|
5
5
|
clampTokensPerPage,
|
|
6
|
-
codexHome,
|
|
7
6
|
loadIssuer,
|
|
8
7
|
needsRefill,
|
|
9
8
|
normalizePayee,
|
|
@@ -12,7 +11,10 @@ import {
|
|
|
12
11
|
settingsFor,
|
|
13
12
|
spend,
|
|
14
13
|
startRefill
|
|
15
|
-
} from "./chunk-
|
|
14
|
+
} from "./chunk-EDGGHF4L.js";
|
|
15
|
+
import {
|
|
16
|
+
codexHome
|
|
17
|
+
} from "./chunk-E4XN5ZK6.js";
|
|
16
18
|
import {
|
|
17
19
|
readConfig,
|
|
18
20
|
readUsage,
|