tenjin-cli 0.1.0-alpha.20 → 0.1.0-alpha.22
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/README.md +18 -3
- package/dist/{chunk-LI44XLMB.js → chunk-2RMXTNLT.js} +5 -5
- package/dist/{chunk-5FYKPQNX.js → chunk-3Q3UBMVB.js} +3 -3
- package/dist/{chunk-3BS55ANY.js → chunk-AG4DWNHO.js} +50 -4
- package/dist/{chunk-3BS55ANY.js.map → chunk-AG4DWNHO.js.map} +1 -1
- package/dist/{chunk-P42QX3QF.js → chunk-B4GA3HAK.js} +2 -2
- package/dist/{chunk-P42QX3QF.js.map → chunk-B4GA3HAK.js.map} +1 -1
- package/dist/{chunk-NSG5TVZQ.js → chunk-CYEUVU3F.js} +14 -10
- package/dist/chunk-CYEUVU3F.js.map +1 -0
- package/dist/{chunk-RPR2C2JO.js → chunk-DA53QKTD.js} +5 -5
- package/dist/{chunk-YEBNE5NW.js → chunk-DHCOGDJB.js} +5 -5
- package/dist/{chunk-BTX2QE2Y.js → chunk-F4IJQBGP.js} +4 -4
- package/dist/chunk-GONDQ27X.js +337 -0
- package/dist/chunk-GONDQ27X.js.map +1 -0
- package/dist/{chunk-TU7XH3JJ.js → chunk-K6AXXYCG.js} +14 -2
- package/dist/chunk-K6AXXYCG.js.map +1 -0
- package/dist/{chunk-2G2WLVHB.js → chunk-LAIZ2AQA.js} +9 -4
- package/dist/chunk-LAIZ2AQA.js.map +1 -0
- package/dist/{chunk-YREKMJPP.js → chunk-NYJCBQL5.js} +65 -6
- package/dist/chunk-NYJCBQL5.js.map +1 -0
- package/dist/{chunk-HH3PDYIG.js → chunk-NZYVSZNF.js} +5 -5
- package/dist/{chunk-4M5FBWT5.js → chunk-QF4SSCQV.js} +10 -1
- package/dist/chunk-QF4SSCQV.js.map +1 -0
- package/dist/{chunk-CCDOV45D.js → chunk-SAN5PFTL.js} +126 -12
- package/dist/chunk-SAN5PFTL.js.map +1 -0
- package/dist/{chunk-LN2HISWU.js → chunk-TU3IK7KK.js} +2 -2
- package/dist/{chunk-4AS52HUG.js → chunk-U5FFVIIF.js} +138 -28
- package/dist/chunk-U5FFVIIF.js.map +1 -0
- package/dist/{chunk-PO4HAXAW.js → chunk-UAT3Q7VF.js} +112 -26
- package/dist/chunk-UAT3Q7VF.js.map +1 -0
- package/dist/chunk-UFCN7NCE.js +130 -0
- package/dist/chunk-UFCN7NCE.js.map +1 -0
- package/dist/{chunk-72UHZRG6.js → chunk-UGLTSPXQ.js} +2 -2
- package/dist/{chunk-5KD5HRAJ.js → chunk-UHEMZEGW.js} +3 -2
- package/dist/chunk-UHEMZEGW.js.map +1 -0
- package/dist/{chunk-2E6GVIRP.js → chunk-UIOZRILX.js} +47 -9
- package/dist/chunk-UIOZRILX.js.map +1 -0
- package/dist/{chunk-7ACHDNJA.js → chunk-WXFVJPZD.js} +13 -4
- package/dist/chunk-WXFVJPZD.js.map +1 -0
- package/dist/{chunk-CXUYCDPX.js → chunk-XBYTP4SP.js} +4 -4
- package/dist/{cli-XJQYUOHI.js → cli-5WM56Y7K.js} +43 -30
- package/dist/{cli-XJQYUOHI.js.map → cli-5WM56Y7K.js.map} +1 -1
- package/dist/{config-KMJLFPSI.js → config-JBF5BZNO.js} +10 -8
- package/dist/{doctor-KMN55D7U.js → doctor-6KHBE77P.js} +25 -16
- package/dist/{doctor-KMN55D7U.js.map → doctor-6KHBE77P.js.map} +1 -1
- package/dist/{fund-LT6ZPGEQ.js → fund-GSFCXEF7.js} +10 -11
- package/dist/{fund-LT6ZPGEQ.js.map → fund-GSFCXEF7.js.map} +1 -1
- package/dist/{hook-command-U5XPEHAJ.js → hook-command-UQZ4DVDL.js} +373 -82
- package/dist/hook-command-UQZ4DVDL.js.map +1 -0
- package/dist/index.js +4 -4
- package/dist/{install-MMNXJ5LM.js → install-TDYC22KI.js} +13 -11
- package/dist/{install-identity-B7FVIWML.js → install-identity-QS4UFF27.js} +7 -7
- package/dist/{install-wallet-XMVUBCHS.js → install-wallet-T737X74E.js} +9 -10
- package/dist/{install-wallet-XMVUBCHS.js.map → install-wallet-T737X74E.js.map} +1 -1
- package/dist/{mcp-TUPRIPSI.js → mcp-JWOOSKLX.js} +624 -69
- package/dist/mcp-JWOOSKLX.js.map +1 -0
- package/dist/{paths-LRMWNTJP.js → paths-MUXHUOCM.js} +8 -2
- package/dist/{pay-XAWW3NV3.js → pay-TDYQFSKY.js} +17 -16
- package/dist/payments-26NVMFR2.js +47 -0
- package/dist/payments-26NVMFR2.js.map +1 -0
- package/dist/{send-HESQYRKM.js → send-4RUBQQKP.js} +9 -10
- package/dist/{send-HESQYRKM.js.map → send-4RUBQQKP.js.map} +1 -1
- package/dist/{status-ANZPRZJJ.js → status-CEHZKHBU.js} +6 -6
- package/dist/{status-line-J3TUWVRQ.js → status-line-QRAO3Y2E.js} +7 -7
- package/dist/{status-line-wiring-HSV7DXO5.js → status-line-wiring-XKUVI5UW.js} +5 -5
- package/dist/tenjin-daemon.mjs +61 -7
- package/dist/{uninstall-GUU34YKP.js → uninstall-SJ6R2W2M.js} +11 -11
- package/dist/{update-KO2NZRJ5.js → update-N2NZS3AA.js} +6 -6
- package/dist/{wallet-3URC5QPK.js → wallet-IB2EFAUP.js} +9 -10
- package/docs/agent-permissions.md +11 -9
- package/docs/safety-model.md +3 -3
- package/package.json +1 -1
- package/dist/chunk-2E6GVIRP.js.map +0 -1
- package/dist/chunk-2G2WLVHB.js.map +0 -1
- package/dist/chunk-3KKKB2AQ.js +0 -40
- package/dist/chunk-3KKKB2AQ.js.map +0 -1
- package/dist/chunk-4AS52HUG.js.map +0 -1
- package/dist/chunk-4M5FBWT5.js.map +0 -1
- package/dist/chunk-5KD5HRAJ.js.map +0 -1
- package/dist/chunk-7ACHDNJA.js.map +0 -1
- package/dist/chunk-CCDOV45D.js.map +0 -1
- package/dist/chunk-NSG5TVZQ.js.map +0 -1
- package/dist/chunk-PO4HAXAW.js.map +0 -1
- package/dist/chunk-TU7XH3JJ.js.map +0 -1
- package/dist/chunk-YREKMJPP.js.map +0 -1
- package/dist/hook-command-U5XPEHAJ.js.map +0 -1
- package/dist/mcp-TUPRIPSI.js.map +0 -1
- /package/dist/{chunk-LI44XLMB.js.map → chunk-2RMXTNLT.js.map} +0 -0
- /package/dist/{chunk-5FYKPQNX.js.map → chunk-3Q3UBMVB.js.map} +0 -0
- /package/dist/{chunk-RPR2C2JO.js.map → chunk-DA53QKTD.js.map} +0 -0
- /package/dist/{chunk-YEBNE5NW.js.map → chunk-DHCOGDJB.js.map} +0 -0
- /package/dist/{chunk-BTX2QE2Y.js.map → chunk-F4IJQBGP.js.map} +0 -0
- /package/dist/{chunk-HH3PDYIG.js.map → chunk-NZYVSZNF.js.map} +0 -0
- /package/dist/{chunk-LN2HISWU.js.map → chunk-TU3IK7KK.js.map} +0 -0
- /package/dist/{chunk-72UHZRG6.js.map → chunk-UGLTSPXQ.js.map} +0 -0
- /package/dist/{chunk-CXUYCDPX.js.map → chunk-XBYTP4SP.js.map} +0 -0
- /package/dist/{config-KMJLFPSI.js.map → config-JBF5BZNO.js.map} +0 -0
- /package/dist/{install-MMNXJ5LM.js.map → install-TDYC22KI.js.map} +0 -0
- /package/dist/{install-identity-B7FVIWML.js.map → install-identity-QS4UFF27.js.map} +0 -0
- /package/dist/{paths-LRMWNTJP.js.map → paths-MUXHUOCM.js.map} +0 -0
- /package/dist/{pay-XAWW3NV3.js.map → pay-TDYQFSKY.js.map} +0 -0
- /package/dist/{status-ANZPRZJJ.js.map → status-CEHZKHBU.js.map} +0 -0
- /package/dist/{status-line-J3TUWVRQ.js.map → status-line-QRAO3Y2E.js.map} +0 -0
- /package/dist/{status-line-wiring-HSV7DXO5.js.map → status-line-wiring-XKUVI5UW.js.map} +0 -0
- /package/dist/{uninstall-GUU34YKP.js.map → uninstall-SJ6R2W2M.js.map} +0 -0
- /package/dist/{update-KO2NZRJ5.js.map → update-N2NZS3AA.js.map} +0 -0
- /package/dist/{wallet-3URC5QPK.js.map → wallet-IB2EFAUP.js.map} +0 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
storeSpecs
|
|
4
|
+
} from "./chunk-UFCN7NCE.js";
|
|
2
5
|
import {
|
|
3
6
|
isPublicAddress
|
|
4
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-LAIZ2AQA.js";
|
|
5
8
|
import {
|
|
6
9
|
requestToolAccess
|
|
7
10
|
} from "./chunk-S2NRVRPF.js";
|
|
8
11
|
import {
|
|
9
12
|
ROUTER_PATH,
|
|
10
13
|
requestDecision
|
|
11
|
-
} from "./chunk-
|
|
12
|
-
import {
|
|
13
|
-
readUsdcBalance
|
|
14
|
-
} from "./chunk-3KKKB2AQ.js";
|
|
14
|
+
} from "./chunk-U5FFVIIF.js";
|
|
15
15
|
import {
|
|
16
16
|
evaluateSpendPolicy,
|
|
17
17
|
readSpendSummary,
|
|
18
18
|
spentOf
|
|
19
|
-
} from "./chunk-
|
|
19
|
+
} from "./chunk-WXFVJPZD.js";
|
|
20
20
|
import {
|
|
21
21
|
REQUEST_TOOL
|
|
22
22
|
} from "./chunk-TGLKKLQG.js";
|
|
@@ -34,32 +34,38 @@ import {
|
|
|
34
34
|
noteSession,
|
|
35
35
|
pruneProgress,
|
|
36
36
|
pruneSessions,
|
|
37
|
+
redirectClaimed,
|
|
37
38
|
sessionDir,
|
|
38
39
|
wasOffered,
|
|
39
40
|
writeProgress
|
|
40
|
-
} from "./chunk-
|
|
41
|
+
} from "./chunk-QF4SSCQV.js";
|
|
41
42
|
import {
|
|
42
43
|
composeUserAgent
|
|
43
|
-
} from "./chunk-
|
|
44
|
+
} from "./chunk-AG4DWNHO.js";
|
|
44
45
|
import {
|
|
45
46
|
routerSettings
|
|
46
|
-
} from "./chunk-
|
|
47
|
+
} from "./chunk-XBYTP4SP.js";
|
|
47
48
|
import "./chunk-2I4QTOLB.js";
|
|
48
49
|
import {
|
|
49
50
|
resolveContextSettings
|
|
50
|
-
} from "./chunk-
|
|
51
|
-
import
|
|
51
|
+
} from "./chunk-UHEMZEGW.js";
|
|
52
|
+
import {
|
|
53
|
+
parseUsdToAtomic,
|
|
54
|
+
toMoney
|
|
55
|
+
} from "./chunk-O2JYBOUO.js";
|
|
52
56
|
import {
|
|
53
57
|
external_exports,
|
|
54
58
|
isSameDeployment,
|
|
55
59
|
loadRawConfig,
|
|
60
|
+
rememberingBalanceReader,
|
|
61
|
+
resolveExperimentalBazaar,
|
|
56
62
|
resolveSettings
|
|
57
|
-
} from "./chunk-
|
|
58
|
-
import "./chunk-
|
|
63
|
+
} from "./chunk-SAN5PFTL.js";
|
|
64
|
+
import "./chunk-B4GA3HAK.js";
|
|
59
65
|
import "./chunk-JLEF5WHM.js";
|
|
60
66
|
import {
|
|
61
67
|
walletPath
|
|
62
|
-
} from "./chunk-
|
|
68
|
+
} from "./chunk-K6AXXYCG.js";
|
|
63
69
|
import "./chunk-ZXTAQYNA.js";
|
|
64
70
|
|
|
65
71
|
// src/router/hooks.ts
|
|
@@ -79,6 +85,14 @@ var MAX_LITERAL_URL_CHARS = 2e3;
|
|
|
79
85
|
var MAX_PENDING_CHARS = 4e3;
|
|
80
86
|
var MAX_OUTCOME_ERROR_CHARS = 1e3;
|
|
81
87
|
var MASK_MARGIN = 4096;
|
|
88
|
+
function subjectOf(pending) {
|
|
89
|
+
return "query" in pending ? pending.query : "url" in pending ? pending.url : pending.question;
|
|
90
|
+
}
|
|
91
|
+
function withSubject(pending, subject) {
|
|
92
|
+
if ("query" in pending) return { tool: pending.tool, query: subject };
|
|
93
|
+
if ("url" in pending) return { tool: pending.tool, url: subject };
|
|
94
|
+
return { tool: pending.tool, question: subject };
|
|
95
|
+
}
|
|
82
96
|
var URL_RE = /\bhttps?:\/\/[^\s<>"'`)\]]+/gi;
|
|
83
97
|
function literalUrlsIn(text) {
|
|
84
98
|
const found = /* @__PURE__ */ new Set();
|
|
@@ -112,7 +126,7 @@ function fit(packet) {
|
|
|
112
126
|
}
|
|
113
127
|
function seal(packet) {
|
|
114
128
|
const pending = packet.pendingCall;
|
|
115
|
-
const subject = pending === void 0 ? "" :
|
|
129
|
+
const subject = pending === void 0 ? "" : subjectOf(pending);
|
|
116
130
|
const sealedSubject = maskWithin(subject, MAX_PENDING_CHARS);
|
|
117
131
|
const bound = (text) => maskWithin(text, MAX_MESSAGE_CHARS).text;
|
|
118
132
|
const sealedCurrent = maskWithin(packet.current.text, MAX_MESSAGE_CHARS);
|
|
@@ -123,9 +137,7 @@ function seal(packet) {
|
|
|
123
137
|
history: packet.history.slice(-MAX_HISTORY).map((message) => ({ role: message.role, text: bound(message.text) })).filter((message) => message.text.length > 0),
|
|
124
138
|
literalUrls: packet.literalUrls.filter((url) => !isLocalTarget(url)).map(mask).filter((url) => url.length <= MAX_LITERAL_URL_CHARS),
|
|
125
139
|
historyStatus: packet.historyStatus,
|
|
126
|
-
...pending === void 0 ? {} : {
|
|
127
|
-
pendingCall: "query" in pending ? { tool: pending.tool, query: sealedSubject.text } : { tool: pending.tool, url: sealedSubject.text }
|
|
128
|
-
},
|
|
140
|
+
...pending === void 0 ? {} : { pendingCall: withSubject(pending, sealedSubject.text) },
|
|
129
141
|
...outcome === void 0 ? {} : {
|
|
130
142
|
nativeOutcome: {
|
|
131
143
|
...outcome,
|
|
@@ -173,7 +185,7 @@ async function buildPromptPacket(transcriptPath, sessionId, prompt) {
|
|
|
173
185
|
}
|
|
174
186
|
async function buildNativePacket(transcriptPath, sessionId, pending, opts = {}) {
|
|
175
187
|
const { agentId, nativeOutcome } = opts;
|
|
176
|
-
const subject =
|
|
188
|
+
const subject = subjectOf(pending);
|
|
177
189
|
const read = await readHistory(transcriptPath, { sessionId });
|
|
178
190
|
const own = agentId === void 0 ? null : await readHistory(subagentTranscriptPath(transcriptPath, sessionId, agentId), {
|
|
179
191
|
sessionId,
|
|
@@ -201,6 +213,7 @@ function subagentTranscriptPath(transcriptPath, sessionId, agentId) {
|
|
|
201
213
|
async function readHistory(path, scope) {
|
|
202
214
|
if (path === void 0 || path.length === 0) return null;
|
|
203
215
|
let raw;
|
|
216
|
+
let tail;
|
|
204
217
|
let file;
|
|
205
218
|
try {
|
|
206
219
|
file = await open(path, "r");
|
|
@@ -209,22 +222,37 @@ async function readHistory(path, scope) {
|
|
|
209
222
|
}
|
|
210
223
|
try {
|
|
211
224
|
const stat = await file.stat();
|
|
212
|
-
if (!stat.isFile()
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
if (
|
|
216
|
-
raw =
|
|
225
|
+
if (!stat.isFile()) return null;
|
|
226
|
+
tail = stat.size > MAX_TRANSCRIPT_BYTES;
|
|
227
|
+
const read = tail ? await readTail(file, stat.size) : await readHead(file);
|
|
228
|
+
if (read === null) return null;
|
|
229
|
+
raw = read;
|
|
217
230
|
} catch {
|
|
218
231
|
return null;
|
|
219
232
|
} finally {
|
|
220
233
|
await file.close();
|
|
221
234
|
}
|
|
222
235
|
try {
|
|
223
|
-
|
|
236
|
+
const messages = parseRows(raw, scope);
|
|
237
|
+
if (tail && !messages.some((message) => message.role === "user")) return null;
|
|
238
|
+
return messages;
|
|
224
239
|
} catch {
|
|
225
240
|
return null;
|
|
226
241
|
}
|
|
227
242
|
}
|
|
243
|
+
async function readHead(file) {
|
|
244
|
+
const buffer = Buffer.alloc(MAX_TRANSCRIPT_BYTES + 1);
|
|
245
|
+
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
|
|
246
|
+
if (bytesRead > MAX_TRANSCRIPT_BYTES) return null;
|
|
247
|
+
return buffer.subarray(0, bytesRead).toString("utf8");
|
|
248
|
+
}
|
|
249
|
+
async function readTail(file, size) {
|
|
250
|
+
const buffer = Buffer.alloc(MAX_TRANSCRIPT_BYTES + 1);
|
|
251
|
+
const { bytesRead } = await file.read(buffer, 0, buffer.length, size - buffer.length);
|
|
252
|
+
const window = buffer.subarray(0, bytesRead);
|
|
253
|
+
const newline = window.indexOf(10);
|
|
254
|
+
return newline === -1 ? null : window.subarray(newline + 1).toString("utf8");
|
|
255
|
+
}
|
|
228
256
|
function parseRows(raw, scope) {
|
|
229
257
|
const { sessionId, agentId } = scope;
|
|
230
258
|
let messages = [];
|
|
@@ -260,12 +288,24 @@ function parseRows(raw, scope) {
|
|
|
260
288
|
return messages;
|
|
261
289
|
}
|
|
262
290
|
var LOCAL_COMMAND_OUTPUT = /^\s*<local-command-(?:stdout|stderr)>/;
|
|
291
|
+
var HANDBACK_PREFIXES = [
|
|
292
|
+
"<task-notification>",
|
|
293
|
+
"<agent-message",
|
|
294
|
+
"<teammate-message",
|
|
295
|
+
"<cross-session-message",
|
|
296
|
+
"Another Claude session sent a message"
|
|
297
|
+
];
|
|
298
|
+
function isHandback(text) {
|
|
299
|
+
const trimmed = text.trimStart();
|
|
300
|
+
return HANDBACK_PREFIXES.some((prefix) => trimmed.startsWith(prefix));
|
|
301
|
+
}
|
|
302
|
+
var HARNESS_ORIGINS = /* @__PURE__ */ new Set(["task-notification", "peer"]);
|
|
263
303
|
function harnessUserRow(row, text) {
|
|
264
304
|
const origin = row.origin;
|
|
265
|
-
if (origin !== null && typeof origin === "object" && origin.kind
|
|
305
|
+
if (origin !== null && typeof origin === "object" && HARNESS_ORIGINS.has(origin.kind)) {
|
|
266
306
|
return true;
|
|
267
307
|
}
|
|
268
|
-
return text
|
|
308
|
+
return isHandback(text) || LOCAL_COMMAND_OUTPUT.test(text);
|
|
269
309
|
}
|
|
270
310
|
function ownSidechainRow(row, agentId) {
|
|
271
311
|
return row.isSidechain === true && row.agentId === agentId && row.isMeta !== true;
|
|
@@ -290,6 +330,54 @@ function textOf(message) {
|
|
|
290
330
|
return parts.join("\n");
|
|
291
331
|
}
|
|
292
332
|
|
|
333
|
+
// src/router/fetch-result.ts
|
|
334
|
+
var OPENING_CHARS = 600;
|
|
335
|
+
var PAGE = String.raw`(?:web ?page|page|site|content|excerpt)`;
|
|
336
|
+
var ONLY = String.raw`(?:only (?:shows?|contains?|includes?|consists? of|has|displays?)|(?:shows?|contains?|includes?|consists? of|has|displays?) only)`;
|
|
337
|
+
var FURNITURE = String.raw`(?:navigation|nav (?:bar|links|menu)|footer|legal links|boilerplate|loading (?:screen|spinner|indicator|message)|title\b(?!\s*(?:,|and\b|&)))`;
|
|
338
|
+
var SAME_SENTENCE = (max) => String.raw`[^.\n]{0,${max}}?`;
|
|
339
|
+
var EMPTY_PAGE_PATTERNS = [
|
|
340
|
+
// "the web page content provided is empty", "\"Web page content:\" appears to be empty"
|
|
341
|
+
new RegExp(
|
|
342
|
+
String.raw`\b(?:web ?page|page) content\b${SAME_SENTENCE(40)}\b(?:is|was|appears to be|seems to be) (?:entirely |completely )?empty\b`,
|
|
343
|
+
"i"
|
|
344
|
+
),
|
|
345
|
+
// "I don't see any web page content provided"
|
|
346
|
+
/\b(?:don't|do not|can't|cannot) see any (?:web ?page |page )?content\b/i,
|
|
347
|
+
// "the webpage content provided only shows YouTube's footer navigation",
|
|
348
|
+
// "contains only navigation elements, footer links", "only contains a title"
|
|
349
|
+
new RegExp(
|
|
350
|
+
String.raw`\b${PAGE}\b${SAME_SENTENCE(60)}\b${ONLY}\b${SAME_SENTENCE(80)}\b${FURNITURE}`,
|
|
351
|
+
"i"
|
|
352
|
+
),
|
|
353
|
+
// "The only information shown is \"Uniswap Interface,\" ... a page title or header"
|
|
354
|
+
new RegExp(
|
|
355
|
+
String.raw`\bthe only (?:information|content|text|thing)s? (?:shown|present|visible|available|provided|included)\b${SAME_SENTENCE(120)}\b${FURNITURE}`,
|
|
356
|
+
"i"
|
|
357
|
+
),
|
|
358
|
+
// "looks like a shell", "appears to be an empty app shell"
|
|
359
|
+
/\b(?:looks like|appears to be|seems to be|is (?:just|only|merely)) an? (?:(?:empty|javascript|js|html|page|app|application|loading) ){0,2}(?:shell|skeleton)\b/i,
|
|
360
|
+
// A page that needs the JavaScript the fetch did not run: React's own
|
|
361
|
+
// noscript line is "You need to enable JavaScript to run this app."
|
|
362
|
+
/\benable JavaScript\b/i,
|
|
363
|
+
/\bJavaScript (?:is )?(?:required|disabled|must be enabled)\b/i,
|
|
364
|
+
new RegExp(String.raw`\b${PAGE}\b${SAME_SENTENCE(60)}\b(?:requires?|needs?) JavaScript\b`, "i"),
|
|
365
|
+
// PyPI's script-less fallback: "A required part of this site couldn't load".
|
|
366
|
+
/\b(?:site|page) (?:couldn't|could not) load\b/i
|
|
367
|
+
];
|
|
368
|
+
function readsAsEmptyPage(result) {
|
|
369
|
+
const opening = result.slice(0, OPENING_CHARS);
|
|
370
|
+
return EMPTY_PAGE_PATTERNS.some((pattern) => pattern.test(opening));
|
|
371
|
+
}
|
|
372
|
+
var SAVED_PDF_RE = /\n\[Binary content \(application\/pdf, [^)\n]{1,24}\) also saved to ([^\]\n]+)\]\s*$/;
|
|
373
|
+
var SAVED_PDF_NAME_RE = /[\\/]([^\\/]+)[\\/]tool-results[\\/]webfetch-\d+-[a-z0-9]+\.pdf$/i;
|
|
374
|
+
var PARENT_SEGMENT_RE = /(?:^|[\\/])\.\.(?:[\\/]|$)/;
|
|
375
|
+
function savedPdfOf(result, sessionId) {
|
|
376
|
+
const path = SAVED_PDF_RE.exec(result)?.[1];
|
|
377
|
+
if (path === void 0 || PARENT_SEGMENT_RE.test(path)) return null;
|
|
378
|
+
return SAVED_PDF_NAME_RE.exec(path)?.[1] === sessionId ? path : null;
|
|
379
|
+
}
|
|
380
|
+
|
|
293
381
|
// src/router/gate.ts
|
|
294
382
|
var GATE_TIMEOUT_MS = 3500;
|
|
295
383
|
|
|
@@ -475,6 +563,20 @@ var ShortfallEventSchema = external_exports.object({
|
|
|
475
563
|
error: external_exports.unknown().optional(),
|
|
476
564
|
is_interrupt: external_exports.boolean().optional()
|
|
477
565
|
});
|
|
566
|
+
var AskEventSchema = external_exports.object({
|
|
567
|
+
hook_event_name: external_exports.literal("PreToolUse").optional(),
|
|
568
|
+
...NativeCallFields,
|
|
569
|
+
tool_name: external_exports.literal("AskUserQuestion")
|
|
570
|
+
});
|
|
571
|
+
var AnswerEventSchema = external_exports.object({
|
|
572
|
+
hook_event_name: external_exports.literal("PostToolUse"),
|
|
573
|
+
session_id: external_exports.string().min(1).max(200),
|
|
574
|
+
transcript_path: external_exports.string().optional(),
|
|
575
|
+
tool_name: external_exports.literal("AskUserQuestion"),
|
|
576
|
+
tool_input: external_exports.record(external_exports.string(), external_exports.unknown()),
|
|
577
|
+
tool_response: external_exports.unknown().optional(),
|
|
578
|
+
cwd: external_exports.string().optional()
|
|
579
|
+
});
|
|
478
580
|
var DelegationEventSchema = external_exports.object({
|
|
479
581
|
session_id: external_exports.string().min(1).max(200),
|
|
480
582
|
transcript_path: external_exports.string().optional(),
|
|
@@ -491,11 +593,31 @@ function decodeEvent(raw) {
|
|
|
491
593
|
eventName: shortfall.data.hook_event_name,
|
|
492
594
|
nativeOutcome: shortfallOf(shortfall.data),
|
|
493
595
|
search: searchOf(shortfall.data),
|
|
596
|
+
savedPdf: savedPdfIn(shortfall.data),
|
|
494
597
|
...nativeCallOf(shortfall.data)
|
|
495
598
|
};
|
|
496
599
|
}
|
|
497
600
|
const native = NativeEventSchema.safeParse(raw);
|
|
498
601
|
if (native.success) return { kind: "native", ...nativeCallOf(native.data) };
|
|
602
|
+
const answer = AnswerEventSchema.safeParse(raw);
|
|
603
|
+
if (answer.success) {
|
|
604
|
+
return {
|
|
605
|
+
kind: "answer",
|
|
606
|
+
sessionId: answer.data.session_id,
|
|
607
|
+
...optional("transcriptPath", answer.data.transcript_path),
|
|
608
|
+
answers: answersOf(answer.data.tool_response, answer.data.tool_input),
|
|
609
|
+
...optional("cwd", answer.data.cwd)
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
const ask = AskEventSchema.safeParse(raw);
|
|
613
|
+
if (ask.success) {
|
|
614
|
+
const question = questionOf(ask.data.tool_input);
|
|
615
|
+
return {
|
|
616
|
+
kind: "ask",
|
|
617
|
+
...callFieldsOf(ask.data),
|
|
618
|
+
pending: question === null ? null : { tool: "AskUserQuestion", question }
|
|
619
|
+
};
|
|
620
|
+
}
|
|
499
621
|
const delegation = DelegationEventSchema.safeParse(raw);
|
|
500
622
|
if (delegation.success) {
|
|
501
623
|
const input = delegation.data.tool_input;
|
|
@@ -521,17 +643,51 @@ function decodeEvent(raw) {
|
|
|
521
643
|
};
|
|
522
644
|
}
|
|
523
645
|
function nativeCallOf(event) {
|
|
646
|
+
return {
|
|
647
|
+
...callFieldsOf(event),
|
|
648
|
+
tool: event.tool_name,
|
|
649
|
+
pending: pendingCallOf(event.tool_name, event.tool_input)
|
|
650
|
+
};
|
|
651
|
+
}
|
|
652
|
+
function callFieldsOf(event) {
|
|
524
653
|
return {
|
|
525
654
|
sessionId: event.session_id,
|
|
526
655
|
...optional("transcriptPath", event.transcript_path),
|
|
527
|
-
tool: event.tool_name,
|
|
528
|
-
pending: pendingCallOf(event.tool_name, event.tool_input),
|
|
529
656
|
...optional("toolUseId", event.tool_use_id),
|
|
530
657
|
...optional("agentId", event.agent_id),
|
|
531
658
|
...optional("agentType", event.agent_type),
|
|
532
659
|
...optional("cwd", event.cwd)
|
|
533
660
|
};
|
|
534
661
|
}
|
|
662
|
+
function questionOf(input) {
|
|
663
|
+
const questions = Array.isArray(input.questions) ? input.questions : [];
|
|
664
|
+
const parts = [];
|
|
665
|
+
for (const entry of questions) {
|
|
666
|
+
if (entry === null || typeof entry !== "object") continue;
|
|
667
|
+
const { question, options } = entry;
|
|
668
|
+
if (typeof question === "string" && question.trim().length > 0) parts.push(question.trim());
|
|
669
|
+
for (const option of Array.isArray(options) ? options : []) {
|
|
670
|
+
if (option === null || typeof option !== "object") continue;
|
|
671
|
+
const { label, description } = option;
|
|
672
|
+
if (typeof label !== "string" || label.trim().length === 0) continue;
|
|
673
|
+
parts.push(
|
|
674
|
+
typeof description === "string" && description.trim().length > 0 ? `${label.trim()}: ${description.trim()}` : label.trim()
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
const joined = parts.join(" ");
|
|
679
|
+
return joined.length > 0 ? joined : null;
|
|
680
|
+
}
|
|
681
|
+
function answersOf(response, input) {
|
|
682
|
+
for (const holder of [response, input]) {
|
|
683
|
+
if (holder === null || typeof holder !== "object" || Array.isArray(holder)) continue;
|
|
684
|
+
const answers = holder.answers;
|
|
685
|
+
if (answers === null || typeof answers !== "object" || Array.isArray(answers)) continue;
|
|
686
|
+
const texts = Object.values(answers).filter((value) => typeof value === "string").map((value) => value.trim()).filter((value) => value.length > 0);
|
|
687
|
+
if (texts.length > 0) return texts.join("\n");
|
|
688
|
+
}
|
|
689
|
+
return null;
|
|
690
|
+
}
|
|
535
691
|
function searchOf(event) {
|
|
536
692
|
if (event.hook_event_name !== "PostToolUse" || event.tool_name !== "WebSearch") return null;
|
|
537
693
|
const response = event.tool_response;
|
|
@@ -539,12 +695,20 @@ function searchOf(event) {
|
|
|
539
695
|
const raw = response;
|
|
540
696
|
return Array.isArray(raw.results) ? { raw, results: raw.results } : null;
|
|
541
697
|
}
|
|
698
|
+
function savedPdfIn(event) {
|
|
699
|
+
if (event.hook_event_name !== "PostToolUse" || event.tool_name !== "WebFetch") return null;
|
|
700
|
+
const response = event.tool_response;
|
|
701
|
+
if (response === null || typeof response !== "object" || Array.isArray(response)) return null;
|
|
702
|
+
const result = response.result;
|
|
703
|
+
return typeof result === "string" ? savedPdfOf(result, event.session_id) : null;
|
|
704
|
+
}
|
|
542
705
|
function optional(key, value) {
|
|
543
706
|
return value === void 0 ? {} : { [key]: value };
|
|
544
707
|
}
|
|
545
708
|
var ACKNOWLEDGEMENTS = /* @__PURE__ */ new Set(["y", "yes", "ok", "okay", "continue", "go", "sure", "thanks"]);
|
|
546
709
|
function promptSkipReason(prompt) {
|
|
547
710
|
const trimmed = prompt.trim();
|
|
711
|
+
if (isHandback(trimmed)) return "handback";
|
|
548
712
|
if (trimmed.startsWith("/")) return "slash";
|
|
549
713
|
const normalized = trimmed.toLowerCase().replace(/[.!,]+$/, "");
|
|
550
714
|
return ACKNOWLEDGEMENTS.has(normalized) ? "acknowledgement" : null;
|
|
@@ -558,6 +722,10 @@ function attributed(hint) {
|
|
|
558
722
|
return `${HINT_SOURCE}: ${toolNamed(hint)}`;
|
|
559
723
|
}
|
|
560
724
|
var ONE_BLOCK = "If this does not cover it, make your own call again: you will not be redirected twice in a row for the same search or URL.";
|
|
725
|
+
var ONE_BLOCK_QUESTION = "If this does not cover it, ask your question again: you will not be redirected twice in a row for the same question.";
|
|
726
|
+
function savedPdfHint(path) {
|
|
727
|
+
return `${HINT_SOURCE}: WebFetch's summary cannot read a PDF, but it saved this one whole to ${path}. Read that file for its text, free (pass pages, such as "1-10", for a long one).`;
|
|
728
|
+
}
|
|
561
729
|
function shortfallOffer(tool, hint) {
|
|
562
730
|
return `${HINT_SOURCE}: your ${tool} call came back short. Optional: ${toolNamed(hint)}`;
|
|
563
731
|
}
|
|
@@ -580,7 +748,11 @@ function shortfallOf(event) {
|
|
|
580
748
|
};
|
|
581
749
|
if (code !== void 0 && isShortStatus(code)) return outcome;
|
|
582
750
|
const success = code === void 0 || code >= 200 && code < 300;
|
|
583
|
-
|
|
751
|
+
if (!success) return null;
|
|
752
|
+
if (bytes !== void 0 && bytes < NEAR_EMPTY_BYTES) return outcome;
|
|
753
|
+
const result = typeof fields.result === "string" ? fields.result : "";
|
|
754
|
+
if (savedPdfOf(result, event.session_id) !== null) return null;
|
|
755
|
+
return readsAsEmptyPage(result) ? { ...outcome, reason: "no_main_content" } : null;
|
|
584
756
|
}
|
|
585
757
|
const results = fields.results;
|
|
586
758
|
if (!Array.isArray(results)) return null;
|
|
@@ -607,7 +779,12 @@ var BALANCE_TIMEOUT_MS = 1e3;
|
|
|
607
779
|
function gateDeadline(deps) {
|
|
608
780
|
return (deps.now?.() ?? Date.now()) + (deps.timeoutMs ?? GATE_TIMEOUT_MS);
|
|
609
781
|
}
|
|
610
|
-
async function
|
|
782
|
+
async function spendShortfall(decision, deps, deadline) {
|
|
783
|
+
const price = `$${toMoney(decision.providerPriceAtomic).usd}`;
|
|
784
|
+
const approval = (detail) => ({
|
|
785
|
+
withheld: "offer needs approval",
|
|
786
|
+
note: `Note: ${price} ${detail}; request will return needs_approval with the command the user runs to approve it \u2014 ask the user first.`
|
|
787
|
+
});
|
|
611
788
|
let rpcUrl;
|
|
612
789
|
try {
|
|
613
790
|
const settings = await resolveContextSettings(hookContext(deps));
|
|
@@ -619,15 +796,22 @@ async function withheldBecause(decision, deps, deadline) {
|
|
|
619
796
|
creator: new URL(decision.endpoint).host,
|
|
620
797
|
sessionSpentAtomic: ledger === null ? 0n : spentOf(ledger)
|
|
621
798
|
});
|
|
622
|
-
if (evaluation.decision !== "allow")
|
|
799
|
+
if (evaluation.decision !== "allow") {
|
|
800
|
+
return approval(
|
|
801
|
+
evaluation.reason === "above_auto_spend" ? `is above this machine's automatic per-call limit ($${toMoney(settings.policy.maxAutoSpendAtomic.toString()).usd})` : "needs approval under this machine's spend limits"
|
|
802
|
+
);
|
|
803
|
+
}
|
|
623
804
|
rpcUrl = settings.rpcUrl;
|
|
624
805
|
} catch {
|
|
625
|
-
return "
|
|
806
|
+
return approval("needs approval under this machine's spend limits");
|
|
626
807
|
}
|
|
627
808
|
if ((deps.env ?? process.env).TENJIN_WALLET_KEY?.trim()) return null;
|
|
628
809
|
const address = await walletAddress(deps.dataDir);
|
|
629
810
|
if (address === null) return null;
|
|
630
|
-
const
|
|
811
|
+
const readBalance = rememberingBalanceReader(deps.dataDir, {
|
|
812
|
+
...deps.now !== void 0 ? { now: deps.now } : {}
|
|
813
|
+
});
|
|
814
|
+
const balance = await readBalance(address, rpcUrl, {
|
|
631
815
|
timeoutMs: Math.min(BALANCE_TIMEOUT_MS, deadline - (deps.now?.() ?? Date.now())),
|
|
632
816
|
...deps.fetchImpl !== void 0 ? { fetchImpl: deps.fetchImpl } : {}
|
|
633
817
|
});
|
|
@@ -638,7 +822,50 @@ async function withheldBecause(decision, deps, deadline) {
|
|
|
638
822
|
);
|
|
639
823
|
return null;
|
|
640
824
|
}
|
|
641
|
-
return balance < BigInt(decision.providerPriceAtomic) ?
|
|
825
|
+
return balance < BigInt(decision.providerPriceAtomic) ? {
|
|
826
|
+
withheld: "wallet needs USDC",
|
|
827
|
+
note: `Note: the wallet holds $${toMoney(balance.toString()).usd}; fund it with \`tenjin wallet fund\`.`
|
|
828
|
+
} : null;
|
|
829
|
+
}
|
|
830
|
+
async function vetOffer(offer, deps, deadline, canAskUser) {
|
|
831
|
+
const shortfall = await spendShortfall(termsOf(offer), deps, deadline);
|
|
832
|
+
if (shortfall === null) {
|
|
833
|
+
const note = await overCapNote(offer.hint, deps, canAskUser);
|
|
834
|
+
return { offer: note === null ? offer : { ...offer, hint: `${offer.hint} ${note}` } };
|
|
835
|
+
}
|
|
836
|
+
if (offer.action === "discovered" && canAskUser) {
|
|
837
|
+
return { offer: { ...offer, hint: `${offer.hint} ${shortfall.note}` } };
|
|
838
|
+
}
|
|
839
|
+
return { withheld: shortfall.withheld };
|
|
840
|
+
}
|
|
841
|
+
var LISTED_PRICE_RE = /about \$(\d+(?:\.\d+)?)(?= per call|: request\(| \(price can vary)|\$(\d+(?:\.\d+)?)(?=\): request\(| via https?:\/\/)/g;
|
|
842
|
+
var QUOTED_RE = /"(?:[^"\\]|\\.)*"/g;
|
|
843
|
+
async function overCapNote(hint, deps, canAskUser) {
|
|
844
|
+
const listed = [...hint.replace(QUOTED_RE, '""').matchAll(LISTED_PRICE_RE)].map(
|
|
845
|
+
(match) => match[1] ?? match[2]
|
|
846
|
+
);
|
|
847
|
+
if (listed.length === 0) return null;
|
|
848
|
+
let cap;
|
|
849
|
+
try {
|
|
850
|
+
cap = (await resolveContextSettings(hookContext(deps))).policy.maxAutoSpendAtomic;
|
|
851
|
+
} catch {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
854
|
+
const over = [
|
|
855
|
+
...new Set(
|
|
856
|
+
listed.filter((usd) => {
|
|
857
|
+
try {
|
|
858
|
+
return BigInt(parseUsdToAtomic(usd)) > cap;
|
|
859
|
+
} catch {
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
})
|
|
863
|
+
)
|
|
864
|
+
];
|
|
865
|
+
if (over.length === 0) return null;
|
|
866
|
+
const prices = over.map((usd) => `$${usd}`).join(" and ");
|
|
867
|
+
const which = over.length > 1 ? "those services" : "that service";
|
|
868
|
+
return `Note: ${prices} ${over.length > 1 ? "are" : "is"} above this machine's automatic per-call limit ($${toMoney(cap.toString()).usd}), so request returns needs_approval for ${which}; ` + (canAskUser ? "prefer one within the limit, or ask the user first." : "use one within the limit.");
|
|
642
869
|
}
|
|
643
870
|
async function walletAddress(dataDir) {
|
|
644
871
|
try {
|
|
@@ -662,40 +889,57 @@ function hookContext(deps) {
|
|
|
662
889
|
async function runPromptHook(raw, deps) {
|
|
663
890
|
const event = decodeEvent(raw);
|
|
664
891
|
if (event?.kind !== "prompt") return { response: null };
|
|
665
|
-
|
|
892
|
+
return offerOnUserText(event, event.prompt, "UserPromptSubmit", deps);
|
|
893
|
+
}
|
|
894
|
+
async function runAnswerHook(raw, deps) {
|
|
895
|
+
const event = decodeEvent(raw);
|
|
896
|
+
if (event?.kind !== "answer" || event.answers === null) return { response: null };
|
|
897
|
+
return offerOnUserText(event, event.answers, "PostToolUse", deps);
|
|
898
|
+
}
|
|
899
|
+
async function offerOnUserText(event, text, hookEventName, deps) {
|
|
900
|
+
const skipped = promptSkipReason(text);
|
|
666
901
|
if (skipped !== null) return { response: null, skipped };
|
|
667
902
|
const router = await routerFor(event.cwd, deps);
|
|
668
903
|
if (router === null) return { response: null };
|
|
669
904
|
const sealed = seal(
|
|
670
|
-
scoped(
|
|
671
|
-
await buildPromptPacket(event.transcriptPath, event.sessionId, event.prompt),
|
|
672
|
-
router.settings
|
|
673
|
-
)
|
|
905
|
+
scoped(await buildPromptPacket(event.transcriptPath, event.sessionId, text), router.settings)
|
|
674
906
|
);
|
|
675
907
|
const footer = await openFooter(deps, event.sessionId, "prompt");
|
|
676
908
|
const deadline = gateDeadline(deps);
|
|
677
|
-
const outcome = await decide(sealed, deps, router.config);
|
|
678
|
-
if (outcome
|
|
909
|
+
const outcome = await decide(sealed, deps, router.config, event.sessionId);
|
|
910
|
+
if (!isOffer(outcome)) {
|
|
679
911
|
await footer.close(outcome);
|
|
680
912
|
return { response: null, ...outcome !== null ? { action: outcome.action } : {} };
|
|
681
913
|
}
|
|
682
|
-
const
|
|
683
|
-
if (withheld !==
|
|
684
|
-
await footer.close(outcome, { withheld });
|
|
685
|
-
return { response: null, action:
|
|
914
|
+
const vetted = await vetOffer(outcome, deps, deadline, true);
|
|
915
|
+
if (vetted.withheld !== void 0) {
|
|
916
|
+
await footer.close(outcome, { withheld: vetted.withheld });
|
|
917
|
+
return { response: null, action: outcome.action, withheld: true };
|
|
686
918
|
}
|
|
687
919
|
await footer.close(outcome);
|
|
688
|
-
return {
|
|
920
|
+
return {
|
|
921
|
+
action: outcome.action,
|
|
922
|
+
id: outcome.id,
|
|
923
|
+
...injection(hookEventName, attributed(vetted.offer.hint))
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
function injection(hookEventName, line) {
|
|
927
|
+
return { response: { hookSpecificOutput: { hookEventName, additionalContext: line } } };
|
|
928
|
+
}
|
|
929
|
+
function isOffer(decision) {
|
|
930
|
+
return decision?.action === "execute" || decision?.action === "discovered";
|
|
689
931
|
}
|
|
690
|
-
function
|
|
932
|
+
function termsOf(offer) {
|
|
933
|
+
if (offer.action === "execute") return offer;
|
|
934
|
+
const { candidate } = offer;
|
|
691
935
|
return {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
936
|
+
provider: candidate.provider,
|
|
937
|
+
providerPriceAtomic: candidate.providerPriceAtomic,
|
|
938
|
+
endpoint: candidate.url
|
|
695
939
|
};
|
|
696
940
|
}
|
|
697
941
|
function isFree(offer) {
|
|
698
|
-
return offer.providerPriceAtomic === "0";
|
|
942
|
+
return offer.action === "execute" && offer.providerPriceAtomic === "0";
|
|
699
943
|
}
|
|
700
944
|
async function routeNativeCall(event, pending, deps, opts = {}) {
|
|
701
945
|
const { nativeOutcome, repeated } = opts;
|
|
@@ -704,6 +948,11 @@ async function routeNativeCall(event, pending, deps, opts = {}) {
|
|
|
704
948
|
if (event.agentId !== void 0 && await requestToolAccess(event.agentType, agentLookup(event.cwd, deps)) !== "allowed") {
|
|
705
949
|
return { offer: null, outcome: { response: null, noRequestTool: true } };
|
|
706
950
|
}
|
|
951
|
+
if (opts.redirected !== void 0 && await opts.redirected()) {
|
|
952
|
+
const footer2 = await openFooter(deps, event.sessionId, opts.operation ?? "search");
|
|
953
|
+
await footer2.close(null, { withheld: "already redirected once" });
|
|
954
|
+
return { offer: null, outcome: { response: null, alreadyRedirected: true } };
|
|
955
|
+
}
|
|
707
956
|
const sealed = seal(
|
|
708
957
|
scoped(
|
|
709
958
|
await buildNativePacket(event.transcriptPath, event.sessionId, pending, {
|
|
@@ -729,10 +978,10 @@ async function routeNativeCall(event, pending, deps, opts = {}) {
|
|
|
729
978
|
);
|
|
730
979
|
return { offer: null, outcome: { response: null } };
|
|
731
980
|
}
|
|
732
|
-
const footer = await openFooter(deps, event.sessionId, "search");
|
|
981
|
+
const footer = await openFooter(deps, event.sessionId, opts.operation ?? "search");
|
|
733
982
|
const deadline = gateDeadline(deps);
|
|
734
|
-
const outcome = await decide(sealed, deps, router.config);
|
|
735
|
-
if (outcome
|
|
983
|
+
const outcome = await decide(sealed, deps, router.config, event.sessionId);
|
|
984
|
+
if (!isOffer(outcome)) {
|
|
736
985
|
await footer.close(outcome);
|
|
737
986
|
return {
|
|
738
987
|
offer: null,
|
|
@@ -743,20 +992,25 @@ async function routeNativeCall(event, pending, deps, opts = {}) {
|
|
|
743
992
|
await footer.close(outcome, { withheld: "free lookup, call runs" });
|
|
744
993
|
return { offer: outcome, free: true, baseUrl: resolveBaseUrl(deps, router.config) };
|
|
745
994
|
}
|
|
746
|
-
const
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
995
|
+
const vetted = await vetOffer(
|
|
996
|
+
outcome,
|
|
997
|
+
deps,
|
|
998
|
+
deadline,
|
|
999
|
+
event.agentId === void 0 && opts.canAskUser !== false
|
|
1000
|
+
);
|
|
1001
|
+
if (vetted.withheld !== void 0) {
|
|
1002
|
+
await footer.close(outcome, { withheld: vetted.withheld });
|
|
1003
|
+
return { offer: null, outcome: { response: null, action: outcome.action, withheld: true } };
|
|
750
1004
|
}
|
|
751
1005
|
if (repeated !== void 0 && await repeated()) {
|
|
752
1006
|
await footer.close(outcome, { withheld: "already redirected once" });
|
|
753
1007
|
return {
|
|
754
1008
|
offer: null,
|
|
755
|
-
outcome: { response: null, action:
|
|
1009
|
+
outcome: { response: null, action: outcome.action, alreadyRedirected: true }
|
|
756
1010
|
};
|
|
757
1011
|
}
|
|
758
1012
|
await footer.close(outcome);
|
|
759
|
-
return { offer:
|
|
1013
|
+
return { offer: vetted.offer };
|
|
760
1014
|
}
|
|
761
1015
|
async function runNativeHook(raw, deps) {
|
|
762
1016
|
const event = decodeEvent(raw);
|
|
@@ -764,7 +1018,8 @@ async function runNativeHook(raw, deps) {
|
|
|
764
1018
|
const target = redirectTarget(event.pending);
|
|
765
1019
|
const routed = await routeNativeCall(event, event.pending, deps, {
|
|
766
1020
|
passFree: true,
|
|
767
|
-
repeated: async () => !await claimRedirect(deps.dataDir, event.sessionId, event.agentId, target, deps.now?.())
|
|
1021
|
+
repeated: async () => !await claimRedirect(deps.dataDir, event.sessionId, event.agentId, target, deps.now?.()),
|
|
1022
|
+
redirected: () => redirectClaimed(deps.dataDir, event.sessionId, event.agentId, target, deps.now?.())
|
|
768
1023
|
});
|
|
769
1024
|
if (routed.offer === null) return routed.outcome;
|
|
770
1025
|
if (routed.free === true) {
|
|
@@ -791,21 +1046,41 @@ async function runNativeHook(raw, deps) {
|
|
|
791
1046
|
if (event.toolUseId !== void 0) {
|
|
792
1047
|
await markOffered(deps.dataDir, event.sessionId, event.toolUseId, deps.now?.());
|
|
793
1048
|
}
|
|
1049
|
+
return redirect(routed.offer);
|
|
1050
|
+
}
|
|
1051
|
+
function redirect(offer, oneBlock = ONE_BLOCK) {
|
|
794
1052
|
return {
|
|
795
1053
|
response: {
|
|
796
1054
|
hookSpecificOutput: {
|
|
797
1055
|
hookEventName: "PreToolUse",
|
|
798
1056
|
permissionDecision: "deny",
|
|
799
1057
|
// THE SERVER'S LINE, attributed and tool-named, then this client's one
|
|
800
|
-
// sentence. The line already carries the id and the exact search
|
|
801
|
-
// that was denied.
|
|
802
|
-
permissionDecisionReason: `${attributed(
|
|
1058
|
+
// sentence. The line already carries the id and the exact search, URL
|
|
1059
|
+
// or question that was denied.
|
|
1060
|
+
permissionDecisionReason: `${attributed(offer.hint)} ${oneBlock}`
|
|
803
1061
|
}
|
|
804
1062
|
},
|
|
805
|
-
action:
|
|
806
|
-
id:
|
|
1063
|
+
action: offer.action,
|
|
1064
|
+
id: offer.id
|
|
807
1065
|
};
|
|
808
1066
|
}
|
|
1067
|
+
async function runAskHook(raw, deps) {
|
|
1068
|
+
const event = decodeEvent(raw);
|
|
1069
|
+
if (event?.kind !== "ask" || event.pending === null) return { response: null };
|
|
1070
|
+
const target = redirectTarget(event.pending);
|
|
1071
|
+
const routed = await routeNativeCall(event, event.pending, deps, {
|
|
1072
|
+
passFree: true,
|
|
1073
|
+
operation: "prompt",
|
|
1074
|
+
canAskUser: false,
|
|
1075
|
+
repeated: async () => !await claimRedirect(deps.dataDir, event.sessionId, event.agentId, target, deps.now?.()),
|
|
1076
|
+
redirected: () => redirectClaimed(deps.dataDir, event.sessionId, event.agentId, target, deps.now?.())
|
|
1077
|
+
});
|
|
1078
|
+
if (routed.offer === null) return routed.outcome;
|
|
1079
|
+
if (routed.free === true) {
|
|
1080
|
+
return { response: null, action: routed.offer.action, id: routed.offer.id, free: true };
|
|
1081
|
+
}
|
|
1082
|
+
return redirect(routed.offer, ONE_BLOCK_QUESTION);
|
|
1083
|
+
}
|
|
809
1084
|
async function runShortfallHook(raw, deps) {
|
|
810
1085
|
const event = decodeEvent(raw);
|
|
811
1086
|
if (event?.kind !== "shortfall") return { response: null };
|
|
@@ -828,9 +1103,22 @@ async function runShortfallHook(raw, deps) {
|
|
|
828
1103
|
augmented: "added"
|
|
829
1104
|
};
|
|
830
1105
|
}
|
|
1106
|
+
if (event.savedPdf !== null) return await pointAtSavedPdf(event, event.savedPdf, deps);
|
|
831
1107
|
const outcome = await offerOnShortfall(event, deps, augment !== null);
|
|
832
1108
|
return augment === null ? outcome : { ...outcome, augmented: "nothing" };
|
|
833
1109
|
}
|
|
1110
|
+
async function pointAtSavedPdf(event, path, deps) {
|
|
1111
|
+
if (await routerFor(event.cwd, deps) === null) return { response: null };
|
|
1112
|
+
return {
|
|
1113
|
+
response: {
|
|
1114
|
+
hookSpecificOutput: {
|
|
1115
|
+
hookEventName: event.eventName,
|
|
1116
|
+
additionalContext: savedPdfHint(path)
|
|
1117
|
+
}
|
|
1118
|
+
},
|
|
1119
|
+
savedPdf: true
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
834
1122
|
async function offerOnShortfall(event, deps, docsJustMissed = false) {
|
|
835
1123
|
const { nativeOutcome, pending, eventName } = event;
|
|
836
1124
|
if (nativeOutcome === null) return { response: null };
|
|
@@ -840,7 +1128,7 @@ async function offerOnShortfall(event, deps, docsJustMissed = false) {
|
|
|
840
1128
|
}
|
|
841
1129
|
const routed = await routeNativeCall(event, pending, deps, { nativeOutcome });
|
|
842
1130
|
if (routed.offer === null) return { ...routed.outcome, nativeOutcome };
|
|
843
|
-
if (docsJustMissed && routed.offer
|
|
1131
|
+
if (docsJustMissed && isFree(routed.offer))
|
|
844
1132
|
return { response: null, nativeOutcome, action: "execute" };
|
|
845
1133
|
return {
|
|
846
1134
|
response: {
|
|
@@ -852,7 +1140,7 @@ async function offerOnShortfall(event, deps, docsJustMissed = false) {
|
|
|
852
1140
|
}
|
|
853
1141
|
},
|
|
854
1142
|
nativeOutcome,
|
|
855
|
-
action:
|
|
1143
|
+
action: routed.offer.action,
|
|
856
1144
|
id: routed.offer.id
|
|
857
1145
|
};
|
|
858
1146
|
}
|
|
@@ -880,15 +1168,15 @@ async function runDelegationHook(raw, deps) {
|
|
|
880
1168
|
if (sealed.packet.historyStatus !== "ok") return { response: null };
|
|
881
1169
|
const footer = await openFooter(deps, event.sessionId, "delegate");
|
|
882
1170
|
const deadline = gateDeadline(deps);
|
|
883
|
-
const outcome = await decide(sealed, deps, router.config);
|
|
884
|
-
if (outcome
|
|
1171
|
+
const outcome = await decide(sealed, deps, router.config, event.sessionId);
|
|
1172
|
+
if (!isOffer(outcome)) {
|
|
885
1173
|
await footer.close(outcome);
|
|
886
1174
|
return { response: null, ...outcome !== null ? { action: outcome.action } : {} };
|
|
887
1175
|
}
|
|
888
|
-
const
|
|
889
|
-
if (withheld !==
|
|
890
|
-
await footer.close(outcome, { withheld });
|
|
891
|
-
return { response: null, action:
|
|
1176
|
+
const vetted = await vetOffer(outcome, deps, deadline, false);
|
|
1177
|
+
if (vetted.withheld !== void 0) {
|
|
1178
|
+
await footer.close(outcome, { withheld: vetted.withheld });
|
|
1179
|
+
return { response: null, action: outcome.action, withheld: true };
|
|
892
1180
|
}
|
|
893
1181
|
await footer.close(outcome);
|
|
894
1182
|
return {
|
|
@@ -904,7 +1192,7 @@ ${delegationOffer(outcome.hint)}`
|
|
|
904
1192
|
}
|
|
905
1193
|
}
|
|
906
1194
|
},
|
|
907
|
-
action:
|
|
1195
|
+
action: outcome.action,
|
|
908
1196
|
id: outcome.id
|
|
909
1197
|
};
|
|
910
1198
|
}
|
|
@@ -934,28 +1222,30 @@ async function openFooter(deps, sessionId, operation) {
|
|
|
934
1222
|
},
|
|
935
1223
|
now()
|
|
936
1224
|
);
|
|
937
|
-
if (withheld === void 0 && decision
|
|
1225
|
+
if (withheld === void 0 && isOffer(decision)) {
|
|
938
1226
|
await bindDecision(deps.dataDir, sessionId, decision.id, now());
|
|
1227
|
+
await storeSpecs(deps.dataDir, decision.specs);
|
|
939
1228
|
}
|
|
940
1229
|
}
|
|
941
1230
|
};
|
|
942
1231
|
}
|
|
943
1232
|
function hookOutcome(decision) {
|
|
944
1233
|
if (decision === null) return "native tools (router unavailable)";
|
|
945
|
-
if (decision
|
|
1234
|
+
if (isOffer(decision)) return `paid lookup offered (${termsOf(decision).provider})`;
|
|
946
1235
|
if (decision.action === "native") return "native tools (no x402 payment)";
|
|
947
1236
|
return "needs input";
|
|
948
1237
|
}
|
|
949
|
-
async function decide({ packet }, deps, config) {
|
|
1238
|
+
async function decide({ packet }, deps, config, sessionId) {
|
|
950
1239
|
const baseUrl = resolveBaseUrl(deps, config);
|
|
951
1240
|
const warn = deps.warn ?? ((line) => process.stderr.write(`${line}
|
|
952
1241
|
`));
|
|
953
1242
|
const outcome = await requestDecision(
|
|
954
1243
|
"hook",
|
|
955
|
-
{ packet },
|
|
1244
|
+
{ packet, sessionId },
|
|
956
1245
|
{
|
|
957
1246
|
ctx: hookContext(deps),
|
|
958
1247
|
baseUrl,
|
|
1248
|
+
acceptsBazaar: resolveExperimentalBazaar(config).value === "on",
|
|
959
1249
|
timeoutMs: deps.timeoutMs ?? GATE_TIMEOUT_MS,
|
|
960
1250
|
...deps.fetchImpl !== void 0 ? { fetchImpl: deps.fetchImpl } : {}
|
|
961
1251
|
}
|
|
@@ -971,6 +1261,7 @@ function nullStream() {
|
|
|
971
1261
|
}
|
|
972
1262
|
function redirectTarget(pending) {
|
|
973
1263
|
if (pending.tool === "WebSearch") return `WebSearch ${pending.query}`;
|
|
1264
|
+
if (pending.tool === "AskUserQuestion") return `AskUserQuestion ${pending.question}`;
|
|
974
1265
|
let url = pending.url;
|
|
975
1266
|
try {
|
|
976
1267
|
url = new URL(url).toString();
|
|
@@ -1012,7 +1303,7 @@ async function runHookCommand(kind, io, deps) {
|
|
|
1012
1303
|
try {
|
|
1013
1304
|
const raw = await (deps.readEvent ?? readStdin)();
|
|
1014
1305
|
const event = JSON.parse(raw);
|
|
1015
|
-
const outcome = kind === "prompt" ? await runPromptHook(event, deps) : kind === "native" ? await runNativeHook(event, deps) : kind === "shortfall" ? await runShortfallHook(event, deps) : await runDelegationHook(event, deps);
|
|
1306
|
+
const outcome = kind === "prompt" ? await runPromptHook(event, deps) : kind === "native" ? await runNativeHook(event, deps) : kind === "shortfall" ? await runShortfallHook(event, deps) : kind === "ask" ? await runAskHook(event, deps) : kind === "answer" ? await runAnswerHook(event, deps) : await runDelegationHook(event, deps);
|
|
1016
1307
|
response = outcome.response;
|
|
1017
1308
|
} catch {
|
|
1018
1309
|
response = null;
|
|
@@ -1049,4 +1340,4 @@ export {
|
|
|
1049
1340
|
STDIN_TIMEOUT_MS,
|
|
1050
1341
|
runHookCommand
|
|
1051
1342
|
};
|
|
1052
|
-
//# sourceMappingURL=hook-command-
|
|
1343
|
+
//# sourceMappingURL=hook-command-UQZ4DVDL.js.map
|