@tempoxyz/patron 0.0.29 → 0.0.30

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.
@@ -8,7 +8,7 @@ import {
8
8
  parseHookInput,
9
9
  payableDomain,
10
10
  turnEnd
11
- } from "./chunk-HQ2ZLUTT.js";
11
+ } from "./chunk-23MZSDN6.js";
12
12
  import {
13
13
  FileTokenStore,
14
14
  codexHome,
@@ -16,20 +16,19 @@ import {
16
16
  redeem,
17
17
  refillSize,
18
18
  storedIssuer
19
- } from "./chunk-SAEZ54SS.js";
19
+ } from "./chunk-4SXIRXUL.js";
20
20
  import {
21
21
  settingsFor
22
- } from "./chunk-OQXACACU.js";
22
+ } from "./chunk-NDBTADXZ.js";
23
+ import "./chunk-ROVOCDKE.js";
23
24
  import {
25
+ clampTokensPerPage,
24
26
  readConfig,
25
27
  readUsage,
26
28
  updateUsage,
29
+ withLock,
27
30
  writePrivateFile
28
- } from "./chunk-ZOQJ4CZE.js";
29
- import {
30
- clampTokensPerPage,
31
- withLock
32
- } from "./chunk-EISHX7Q3.js";
31
+ } from "./chunk-AKOXBXV3.js";
33
32
  import "./chunk-GTMLTZRN.js";
34
33
 
35
34
  // src/hook.ts
@@ -7,20 +7,23 @@ import {
7
7
  isCursorPayload,
8
8
  parseHookInput,
9
9
  turnEnd
10
- } from "./chunk-HQ2ZLUTT.js";
10
+ } from "./chunk-23MZSDN6.js";
11
11
  import {
12
+ claimRedemptions,
13
+ redemptionWait,
12
14
  startDrain,
13
15
  startRefill
14
- } from "./chunk-3CYJJJR3.js";
16
+ } from "./chunk-TC2WEQ25.js";
15
17
  import {
16
18
  resolveSettings
17
- } from "./chunk-OQXACACU.js";
19
+ } from "./chunk-NDBTADXZ.js";
18
20
  import {
19
21
  tryLock
20
- } from "./chunk-EISHX7Q3.js";
22
+ } from "./chunk-AKOXBXV3.js";
21
23
  import "./chunk-GTMLTZRN.js";
22
24
 
23
25
  // src/hook-process.ts
26
+ import { utimes as utimes2 } from "node:fs/promises";
24
27
  import { homedir } from "node:os";
25
28
  import { join as join2 } from "node:path";
26
29
 
@@ -41,7 +44,9 @@ function queuePaths(home) {
41
44
  return {
42
45
  dir: join(home, "pay-queue"),
43
46
  lock: join(home, "pay.lock"),
44
- start: join(home, "pay.start")
47
+ start: join(home, "pay.start"),
48
+ /** Held by a drainer waiting out a hold on redemptions (spacing.ts); touched each check. */
49
+ wait: join(home, "pay-wait.lock")
45
50
  };
46
51
  }
47
52
  var MAX_QUEUED = 500;
@@ -82,10 +87,15 @@ async function take(path) {
82
87
  }
83
88
  }
84
89
  async function drainerRunning(home, now = Date.now()) {
85
- return stat(queuePaths(home).lock).then(
86
- (stats) => now - stats.mtimeMs <= DRAIN_STALE_MS,
87
- () => false
88
- );
90
+ const paths = queuePaths(home);
91
+ for (const path of [paths.lock, paths.wait]) {
92
+ const live = await stat(path).then(
93
+ (stats) => now - stats.mtimeMs <= DRAIN_STALE_MS,
94
+ () => false
95
+ );
96
+ if (live) return true;
97
+ }
98
+ return false;
89
99
  }
90
100
  async function claimStart(home) {
91
101
  return tryLock(queuePaths(home).start, START_STALE_MS).catch(() => void 0);
@@ -124,11 +134,16 @@ async function drainHeld(dir, lock, options, deadline) {
124
134
  return name;
125
135
  }
126
136
  };
137
+ let held = false;
127
138
  const worker = async () => {
128
139
  for (; ; ) {
129
- if (Date.now() >= deadline) return;
140
+ if (Date.now() >= deadline || held) return;
130
141
  const name = await next();
131
142
  if (!name) return;
143
+ if (options.ready && !await options.ready().catch(() => false)) {
144
+ held = true;
145
+ return;
146
+ }
132
147
  const event = await take(join(dir, name));
133
148
  if (!event) continue;
134
149
  await options.pay(event).catch(() => void 0);
@@ -172,9 +187,10 @@ async function runHookProcess(argument = process.argv[3]) {
172
187
  if (!claim) return;
173
188
  if (await startDrain()) return;
174
189
  await claim();
190
+ if (!await claimRedemptions(settings.home, Date.now(), HOOK_TIMEOUT_MS)) return;
175
191
  const own = await take(path);
176
192
  if (!own) return;
177
- }
193
+ } else if (!await claimRedemptions(settings.home, Date.now(), HOOK_TIMEOUT_MS)) return;
178
194
  await payEvent(
179
195
  event,
180
196
  settings,
@@ -182,8 +198,13 @@ async function runHookProcess(argument = process.argv[3]) {
182
198
  started + HOOK_TIMEOUT_MS - Date.now()
183
199
  );
184
200
  }
201
+ var HOLD_WAIT_MAX_MS = 30 * 6e4;
202
+ var HOLD_RECHECK_MS = 3e4;
185
203
  async function runPayProcess() {
186
- setTimeout(() => process.exit(0), DRAIN_MAX_MS + PAY_TIMEOUT_MS + 5e3).unref();
204
+ const hardStop = setTimeout(
205
+ () => process.exit(0),
206
+ DRAIN_MAX_MS + PAY_TIMEOUT_MS + 5e3
207
+ ).unref();
187
208
  const settings = resolveSettings();
188
209
  const raw = await readStdin(HOOK_TIMEOUT_MS);
189
210
  if (raw.trim()) await enqueue(settings.home, { raw });
@@ -195,14 +216,39 @@ async function runPayProcess() {
195
216
  };
196
217
  await drainQueue(settings.home, {
197
218
  deadline: Date.now() + DRAIN_MAX_MS,
198
- pay: (event) => payEvent(event, settings, refill, PAY_TIMEOUT_MS)
219
+ pay: (event) => payEvent(event, settings, refill, PAY_TIMEOUT_MS),
220
+ ready: () => claimRedemptions(settings.home, Date.now(), PAY_TIMEOUT_MS)
199
221
  });
222
+ if (await queuedCount(settings.home) === 0) return;
223
+ if (await redemptionWait(settings.home) > 0) {
224
+ clearTimeout(hardStop);
225
+ setTimeout(() => process.exit(0), HOLD_WAIT_MAX_MS + 5e3).unref();
226
+ if (!await waitForHold(settings.home)) return;
227
+ }
200
228
  if (!await drainerRunning(settings.home) && await queuedCount(settings.home) > 0)
201
229
  await startDrain();
202
230
  }
231
+ async function waitForHold(home) {
232
+ const path = queuePaths(home).wait;
233
+ const release = await tryLock(path, DRAIN_STALE_MS).catch(() => void 0);
234
+ if (!release) return false;
235
+ try {
236
+ const deadline = Date.now() + HOLD_WAIT_MAX_MS;
237
+ for (; ; ) {
238
+ const wait = await redemptionWait(home);
239
+ if (wait <= 0) return true;
240
+ if (Date.now() + wait > deadline) return false;
241
+ await new Promise((resolve) => setTimeout(resolve, Math.min(wait, HOLD_RECHECK_MS)));
242
+ const now = /* @__PURE__ */ new Date();
243
+ await utimes2(path, now, now).catch(() => void 0);
244
+ }
245
+ } finally {
246
+ await release();
247
+ }
248
+ }
203
249
  async function payEvent(event, settings, refill, timeoutMs) {
204
250
  if (timeoutMs <= 0) return;
205
- const { runHook } = await import("./hook-HYV4BSCN.js");
251
+ const { runHook } = await import("./hook-IE7INCPX.js");
206
252
  await runHook(
207
253
  {
208
254
  readInput: async () => event.raw,
@@ -1,11 +1,10 @@
1
1
  import { createRequire as __patronCreateRequire } from 'node:module'
2
2
  const require = __patronCreateRequire(import.meta.url)
3
+ import "./chunk-ROVOCDKE.js";
3
4
  import {
5
+ accessKeyLimitUnits,
4
6
  statePaths
5
- } from "./chunk-ZOQJ4CZE.js";
6
- import {
7
- accessKeyLimitUnits
8
- } from "./chunk-EISHX7Q3.js";
7
+ } from "./chunk-AKOXBXV3.js";
9
8
  import "./chunk-GTMLTZRN.js";
10
9
 
11
10
  // src/wallet.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempoxyz/patron",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "description": "Pay the websites your AI agents read, privately",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,56 +0,0 @@
1
- import { createRequire as __patronCreateRequire } from 'node:module'
2
- const require = __patronCreateRequire(import.meta.url)
3
-
4
- // src/refill-process.ts
5
- import { spawn } from "node:child_process";
6
- import { createRequire } from "node:module";
7
- function startRefill(options = {}, script = process.argv[1]) {
8
- try {
9
- if (!script) return;
10
- const [command, refillArgs] = refillCommand(script);
11
- const args = options.delayed ? [...refillArgs, "--delay"] : refillArgs;
12
- const child = spawn(command, args, {
13
- detached: true,
14
- env: process.env,
15
- stdio: "ignore"
16
- });
17
- child.on("error", () => void 0);
18
- child.unref();
19
- } catch {
20
- }
21
- }
22
- function refillCommand(script) {
23
- try {
24
- createRequire(script).resolve("mppx");
25
- return [process.execPath, [...process.execArgv, script, "refill"]];
26
- } catch {
27
- return [
28
- process.platform === "win32" ? "npx.cmd" : "npx",
29
- ["-y", "@tempoxyz/patron", "refill"]
30
- ];
31
- }
32
- }
33
- function startDrain(script = process.argv[1]) {
34
- return new Promise((resolve) => {
35
- try {
36
- if (!script) return resolve(false);
37
- const child = spawn(process.execPath, [...process.execArgv, script, "pay"], {
38
- detached: true,
39
- env: process.env,
40
- stdio: "ignore"
41
- });
42
- child.on("error", () => resolve(false));
43
- child.on("spawn", () => {
44
- child.unref();
45
- resolve(true);
46
- });
47
- } catch {
48
- resolve(false);
49
- }
50
- });
51
- }
52
-
53
- export {
54
- startRefill,
55
- startDrain
56
- };