clikcode 1.0.3 → 1.0.5
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 +25 -14
- package/dist/chunk-23TDEBGZ.js +71 -0
- package/dist/chunk-25T3GVS7.js +425 -0
- package/dist/chunk-2BBELGHY.js +11 -0
- package/dist/chunk-2EOL5PZL.js +208 -0
- package/dist/chunk-2GAB36IE.js +26 -0
- package/dist/chunk-2OLS7V66.js +2440 -0
- package/dist/chunk-2QFO2URB.js +179 -0
- package/dist/chunk-2QWPQRSU.js +254 -0
- package/dist/chunk-2YPAPAR3.js +184 -0
- package/dist/chunk-2ZAG5372.js +33 -0
- package/dist/chunk-32IKAIZC.js +12507 -0
- package/dist/chunk-34AZEGYW.js +95 -0
- package/dist/chunk-34JQYQCA.js +175 -0
- package/dist/chunk-34ZISPAO.js +67 -0
- package/dist/chunk-3AAAARE6.js +46 -0
- package/dist/chunk-3AJ6Q3OY.js +510 -0
- package/dist/chunk-3B2CHX7M.js +884 -0
- package/dist/chunk-3BUFORRX.js +59 -0
- package/dist/chunk-3QLHYYFK.js +30 -0
- package/dist/chunk-3RXBMYKZ.js +140 -0
- package/dist/chunk-3TFNIJWS.js +52 -0
- package/dist/chunk-3W3IPDFP.js +119 -0
- package/dist/chunk-3WMS7SEE.js +417 -0
- package/dist/chunk-435ZNSZ3.js +23 -0
- package/dist/chunk-4AQ5NINV.js +39 -0
- package/dist/chunk-4GVEJ5FK.js +168 -0
- package/dist/chunk-4GXAO3UY.js +262 -0
- package/dist/chunk-4L2ZZM4Q.js +332 -0
- package/dist/chunk-4LPDG57B.js +17 -0
- package/dist/chunk-4NN3XQQT.js +223 -0
- package/dist/chunk-4QZEZME7.js +54 -0
- package/dist/chunk-4R6CSC3S.js +38 -0
- package/dist/chunk-4RHXTL3W.js +514 -0
- package/dist/chunk-4SJMPTVQ.js +18 -0
- package/dist/chunk-4VY7G6TX.js +114 -0
- package/dist/chunk-5642PBF7.js +124 -0
- package/dist/chunk-5B6TVO4A.js +33 -0
- package/dist/chunk-5BJLDUPL.js +62 -0
- package/dist/chunk-5HMFFJWU.js +19 -0
- package/dist/chunk-5IKLK33T.js +69 -0
- package/dist/chunk-5NQ5FFMX.js +44 -0
- package/dist/chunk-5QNNW5L6.js +176 -0
- package/dist/chunk-5SM5UAWN.js +239 -0
- package/dist/chunk-5WYJTA6O.js +74 -0
- package/dist/chunk-5ZSR2Y5T.js +227 -0
- package/dist/chunk-5ZWUM5GH.js +39 -0
- package/dist/chunk-64E7D4HY.js +364 -0
- package/dist/chunk-6CK466BC.js +99 -0
- package/dist/chunk-6ES45HHO.js +138 -0
- package/dist/chunk-6HYEZFGL.js +277 -0
- package/dist/chunk-6Y7HLXGW.js +29 -0
- package/dist/chunk-6YKK4FYR.js +258 -0
- package/dist/chunk-74WNMLMS.js +117 -0
- package/dist/chunk-7AULZ7OD.js +55 -0
- package/dist/chunk-7DOKGSUN.js +101 -0
- package/dist/chunk-7IH5LTIZ.js +195 -0
- package/dist/chunk-7ISTS5NS.js +21 -0
- package/dist/chunk-7NMUTGOW.js +527 -0
- package/dist/chunk-7PNHUZEL.js +153 -0
- package/dist/chunk-7TSQLONI.js +88 -0
- package/dist/chunk-AE7TOLD5.js +30 -0
- package/dist/chunk-AEMHSXKA.js +130 -0
- package/dist/chunk-AKRYTS2D.js +211 -0
- package/dist/chunk-AMNSE36L.js +59 -0
- package/dist/chunk-AZVD74ZV.js +23 -0
- package/dist/chunk-B35DVOTK.js +1981 -0
- package/dist/chunk-B4HNJRAH.js +626 -0
- package/dist/chunk-B5WYPPHS.js +13 -0
- package/dist/chunk-BBOVDXQM.js +59 -0
- package/dist/chunk-BD3ECZLK.js +62 -0
- package/dist/chunk-BDGSOT74.js +4578 -0
- package/dist/chunk-BF5VEOPK.js +31 -0
- package/dist/chunk-BFFRW4XH.js +47 -0
- package/dist/chunk-BFPC6PEW.js +208 -0
- package/dist/chunk-BGEMY2JJ.js +376 -0
- package/dist/chunk-BISGNHKD.js +35 -0
- package/dist/chunk-BMRAEBRY.js +21 -0
- package/dist/chunk-BNTXAZXW.js +127 -0
- package/dist/chunk-BOQBXGSN.js +75 -0
- package/dist/chunk-BTJDSTQP.js +213 -0
- package/dist/chunk-BUOPJQP5.js +218 -0
- package/dist/chunk-BXFJS65Q.js +52 -0
- package/dist/chunk-BXPQFSX5.js +59 -0
- package/dist/chunk-C57LIWNY.js +1269 -0
- package/dist/chunk-C6YVCCWR.js +74 -0
- package/dist/chunk-CAADDF6M.js +167 -0
- package/dist/chunk-CBANJFBO.js +69 -0
- package/dist/chunk-CERF3RIB.js +597 -0
- package/dist/chunk-COAS5ETR.js +46 -0
- package/dist/chunk-CQ7J6UQ7.js +78 -0
- package/dist/chunk-CRUVYGO2.js +26 -0
- package/dist/chunk-CVFVFRGN.js +58 -0
- package/dist/chunk-D2UK66HM.js +37 -0
- package/dist/chunk-D2W7SA4R.js +18 -0
- package/dist/chunk-DA44ZENT.js +501 -0
- package/dist/chunk-DAJ76TOC.js +99 -0
- package/dist/chunk-DCZ6CIUM.js +147 -0
- package/dist/chunk-DF35KMXM.js +1314 -0
- package/dist/chunk-DGASQT26.js +151 -0
- package/dist/chunk-DJT57C5P.js +55 -0
- package/dist/chunk-DKDPZTD3.js +44 -0
- package/dist/chunk-DQYVQ2N7.js +77 -0
- package/dist/chunk-DRTP2RTY.js +266 -0
- package/dist/chunk-DW2BMUKY.js +61 -0
- package/dist/chunk-DW5T5ZVU.js +307 -0
- package/dist/chunk-DXKNDNFC.js +164 -0
- package/dist/chunk-DZEQ7UYR.js +291 -0
- package/dist/chunk-E4F4OUAC.js +463 -0
- package/dist/chunk-EE4B6A7W.js +69 -0
- package/dist/chunk-EISKIYWG.js +23 -0
- package/dist/chunk-EKAQK7UQ.js +235 -0
- package/dist/chunk-EOBTJV32.js +123 -0
- package/dist/chunk-ESF3DIZY.js +325 -0
- package/dist/chunk-EVOHOZ4U.js +45 -0
- package/dist/chunk-EXN4KHLB.js +110 -0
- package/dist/chunk-F252T6Y3.js +78 -0
- package/dist/chunk-F3DP4ZA2.js +40 -0
- package/dist/chunk-F6F47QAY.js +58 -0
- package/dist/chunk-FA3Y23ZW.js +57 -0
- package/dist/chunk-FBKXE4EJ.js +640 -0
- package/dist/chunk-FCGEB3H6.js +23 -0
- package/dist/chunk-FF7ZVYJF.js +3732 -0
- package/dist/chunk-FFGKN6L3.js +39 -0
- package/dist/chunk-FFGU4GCC.js +94 -0
- package/dist/chunk-FSFDAOVP.js +74 -0
- package/dist/chunk-FT5TX7OX.js +1573 -0
- package/dist/chunk-G3QP67Z7.js +232 -0
- package/dist/chunk-G5ORJKV2.js +179 -0
- package/dist/chunk-GFNC62PE.js +111 -0
- package/dist/chunk-GGK43Q3N.js +67 -0
- package/dist/chunk-GGYKY4GF.js +11537 -0
- package/dist/chunk-GGYNHGNJ.js +55 -0
- package/dist/chunk-GLY7RM3C.js +354 -0
- package/dist/chunk-GQBZU6EO.js +20 -0
- package/dist/chunk-GSXENUZL.js +9 -0
- package/dist/chunk-GTOAT5D3.js +40 -0
- package/dist/chunk-GWCMDPQU.js +1007 -0
- package/dist/chunk-GXYSMYHJ.js +152 -0
- package/dist/chunk-GYMAI5NY.js +14 -0
- package/dist/chunk-H2VZMKPE.js +30 -0
- package/dist/chunk-H4THUNBM.js +2091 -0
- package/dist/chunk-HGRVSNUZ.js +361 -0
- package/dist/chunk-HI4VY473.js +27 -0
- package/dist/chunk-HOJRSD3Q.js +447 -0
- package/dist/chunk-HRX25W35.js +82 -0
- package/dist/chunk-HSE4AQYV.js +55 -0
- package/dist/chunk-HU5SSUFX.js +383 -0
- package/dist/chunk-I3GDBJLH.js +196 -0
- package/dist/chunk-I75EDU75.js +684 -0
- package/dist/chunk-IBA7DMIO.js +20 -0
- package/dist/chunk-IC6N4NZA.js +231 -0
- package/dist/chunk-IF43HIGV.js +284 -0
- package/dist/chunk-IGZ6C2MC.js +439 -0
- package/dist/chunk-IMLTL2MY.js +192 -0
- package/dist/chunk-IMZL6SGR.js +33 -0
- package/dist/chunk-IS4USJPY.js +516 -0
- package/dist/chunk-ISFSPN2F.js +82 -0
- package/dist/chunk-IXE7GTFI.js +55 -0
- package/dist/chunk-IZ77QJQG.js +36 -0
- package/dist/chunk-J2PACWRQ.js +34 -0
- package/dist/chunk-J6EGFVQX.js +180 -0
- package/dist/chunk-JEKOD43D.js +22 -0
- package/dist/chunk-JGIWATSG.js +30 -0
- package/dist/chunk-JYBONKD4.js +43 -0
- package/dist/chunk-K2NLG2HK.js +454 -0
- package/dist/chunk-KCQ4TBLS.js +164 -0
- package/dist/chunk-KD5KIVMS.js +176 -0
- package/dist/chunk-KHW6TX6I.js +63 -0
- package/dist/chunk-KI5GZFCE.js +520 -0
- package/dist/chunk-KN34B7NO.js +105 -0
- package/dist/chunk-KYUHIFP3.js +136 -0
- package/dist/chunk-KZL5VS4B.js +300 -0
- package/dist/chunk-L3VW3QIH.js +48 -0
- package/dist/chunk-LA6MG4ET.js +2110 -0
- package/dist/chunk-LAVFOA3S.js +66 -0
- package/dist/chunk-LB4OA77N.js +13 -0
- package/dist/chunk-LBFRF2K4.js +196 -0
- package/dist/chunk-LM6EIA4B.js +93 -0
- package/dist/chunk-LPCNRGJ7.js +692 -0
- package/dist/chunk-LSLNL5T3.js +168 -0
- package/dist/chunk-LVRRWP4W.js +568 -0
- package/dist/chunk-LWCFBRHL.js +33 -0
- package/dist/chunk-LXFTMGZZ.js +166 -0
- package/dist/chunk-M33LVA4M.js +151 -0
- package/dist/chunk-M3FO5UMK.js +4944 -0
- package/dist/chunk-M5QNDBAY.js +66 -0
- package/dist/chunk-M6ND6KBE.js +1291 -0
- package/dist/chunk-M7H3WQTE.js +76 -0
- package/dist/chunk-MCBRVPA7.js +191 -0
- package/dist/chunk-MCSDWZET.js +91 -0
- package/dist/chunk-MDT5XDXL.js +289 -0
- package/dist/chunk-MJG3IQY4.js +91 -0
- package/dist/chunk-MOU3N2UL.js +44 -0
- package/dist/chunk-MP4KZOQC.js +3451 -0
- package/dist/chunk-MPAJ446F.js +66 -0
- package/dist/chunk-MVJZZ6AL.js +16 -0
- package/dist/chunk-MXS6S2PY.js +253 -0
- package/dist/chunk-N3SFDYM7.js +291 -0
- package/dist/chunk-NAOX3JUB.js +13 -0
- package/dist/chunk-NEKKIBCX.js +283 -0
- package/dist/chunk-NKDATMEM.js +133 -0
- package/dist/chunk-NR4FMC4K.js +395 -0
- package/dist/chunk-NSOHOJBR.js +221 -0
- package/dist/chunk-NTDB6QKF.js +29 -0
- package/dist/chunk-NWEYRBWK.js +25 -0
- package/dist/chunk-O2YSNYWD.js +23 -0
- package/dist/chunk-O3EW7LAY.js +54 -0
- package/dist/chunk-O7BNJIXU.js +366 -0
- package/dist/chunk-OARL2NXZ.js +116 -0
- package/dist/chunk-OHKZUEJW.js +351 -0
- package/dist/chunk-OKU45BXA.js +647 -0
- package/dist/chunk-OL4JBD3J.js +59 -0
- package/dist/chunk-OLUO5DDR.js +192 -0
- package/dist/chunk-OQ245JLA.js +196 -0
- package/dist/chunk-P72UZAQI.js +289 -0
- package/dist/chunk-PAL2ENB6.js +20 -0
- package/dist/chunk-PAXYQZH7.js +53 -0
- package/dist/chunk-PDPEM5VW.js +51 -0
- package/dist/chunk-PEWCDI2C.js +668 -0
- package/dist/chunk-PGZ54EDX.js +27 -0
- package/dist/chunk-PRWVLDZF.js +73 -0
- package/dist/chunk-PSQQ75VJ.js +52 -0
- package/dist/chunk-PUOVIFHW.js +118 -0
- package/dist/chunk-PWBF2X3V.js +107 -0
- package/dist/chunk-PWXII6HF.js +43 -0
- package/dist/chunk-Q35VLJRT.js +80 -0
- package/dist/chunk-QJP7EYGL.js +128 -0
- package/dist/chunk-QPQKX2MK.js +8836 -0
- package/dist/chunk-QQRLPY25.js +1053 -0
- package/dist/chunk-QUS4ER2W.js +407 -0
- package/dist/chunk-QYHI3YET.js +29 -0
- package/dist/chunk-QYRU2UQI.js +32 -0
- package/dist/chunk-R6XVVQGC.js +13 -0
- package/dist/chunk-R7QN6ONS.js +483 -0
- package/dist/chunk-RD23MPGC.js +221 -0
- package/dist/chunk-RDDWQBEM.js +185 -0
- package/dist/chunk-RDLLDYFO.js +104 -0
- package/dist/chunk-RE4TGORR.js +84 -0
- package/dist/chunk-RJLHVYQY.js +217 -0
- package/dist/chunk-RJLPTWHW.js +905 -0
- package/dist/chunk-RKUVLRZI.js +295 -0
- package/dist/chunk-ROGXPGYR.js +37 -0
- package/dist/chunk-RSZGEL5U.js +163 -0
- package/dist/chunk-RVUOIJXN.js +909 -0
- package/dist/chunk-RW7F4JRP.js +11 -0
- package/dist/chunk-S4UO3AO3.js +53 -0
- package/dist/chunk-S5JAAF6S.js +585 -0
- package/dist/chunk-SNU37TQI.js +56 -0
- package/dist/chunk-SQVUDO2O.js +31 -0
- package/dist/chunk-SYVHIYQT.js +66 -0
- package/dist/chunk-SZBQMTG2.js +85 -0
- package/dist/chunk-T5VCXA3J.js +25 -0
- package/dist/chunk-TK6NSP56.js +30 -0
- package/dist/chunk-TKWVMA6L.js +22 -0
- package/dist/chunk-TQDKJHWX.js +59 -0
- package/dist/chunk-TQDM7S5N.js +127 -0
- package/dist/chunk-TULAXS77.js +23 -0
- package/dist/chunk-TVT45XNH.js +4625 -0
- package/dist/chunk-TWCVV2JO.js +467 -0
- package/dist/chunk-U4DKAD6S.js +47 -0
- package/dist/chunk-UA5K2BTI.js +564 -0
- package/dist/chunk-UAMKCMOK.js +40 -0
- package/dist/chunk-UBMVADBF.js +51 -0
- package/dist/chunk-UFQ5A5PW.js +645 -0
- package/dist/chunk-UOD2SOLL.js +1301 -0
- package/dist/chunk-UTSB4LOW.js +156 -0
- package/dist/chunk-UVXNIUTN.js +412 -0
- package/dist/chunk-UXVPXYPE.js +90 -0
- package/dist/chunk-UYEEMIWR.js +374 -0
- package/dist/chunk-VBGM5ZBW.js +54 -0
- package/dist/chunk-VEWPCZPG.js +762 -0
- package/dist/chunk-VIRDLZ5E.js +108 -0
- package/dist/chunk-VMPO7WKN.js +52 -0
- package/dist/chunk-W2NVSDO5.js +5122 -0
- package/dist/chunk-W3YYPBEA.js +176 -0
- package/dist/chunk-W5AYOICN.js +280 -0
- package/dist/chunk-W5GZOIUF.js +27 -0
- package/dist/chunk-WHKNLZ3C.js +76 -0
- package/dist/chunk-WKBEPPQ5.js +1409 -0
- package/dist/chunk-WM3Z6QKQ.js +151 -0
- package/dist/chunk-WMI2TPMA.js +31 -0
- package/dist/chunk-WNX5VD7S.js +150 -0
- package/dist/chunk-WOVTYST3.js +60 -0
- package/dist/chunk-WPB5WEZJ.js +546 -0
- package/dist/chunk-WUO5EOAT.js +44 -0
- package/dist/chunk-WWLJMP6D.js +133 -0
- package/dist/chunk-WZMLH3XP.js +58 -0
- package/dist/chunk-WZYVLUVQ.js +79 -0
- package/dist/chunk-X2OCPVU5.js +135 -0
- package/dist/chunk-X3CL6DTR.js +95 -0
- package/dist/chunk-X6WF7JEV.js +354 -0
- package/dist/chunk-XAHGBPWT.js +44 -0
- package/dist/chunk-XCKQ5BM2.js +228 -0
- package/dist/chunk-XIBYREEY.js +106 -0
- package/dist/chunk-XKW5NDMR.js +378 -0
- package/dist/chunk-XRDALAO3.js +100 -0
- package/dist/chunk-XUP22EAS.js +41 -0
- package/dist/chunk-XWAC3MKW.js +243 -0
- package/dist/chunk-Y3R7JMXB.js +59 -0
- package/dist/chunk-Y54LTP4W.js +82 -0
- package/dist/chunk-Y7UECI4M.js +99 -0
- package/dist/chunk-YACGDFHC.js +47 -0
- package/dist/chunk-YHNIQPKY.js +71 -0
- package/dist/chunk-YMBR6HYP.js +111 -0
- package/dist/chunk-YMCH3L2S.js +26 -0
- package/dist/chunk-Z4ZU6T7V.js +99 -0
- package/dist/chunk-ZDPF67ZZ.js +173 -0
- package/dist/chunk-ZFC2Q67K.js +101 -0
- package/dist/chunk-ZGHNNSCT.js +114 -0
- package/dist/chunk-ZGX3ERAT.js +173 -0
- package/dist/chunk-ZI3YHEBY.js +37 -0
- package/dist/chunk-ZN7G6JLS.js +639 -0
- package/dist/chunk-ZNPFQMXT.js +47 -0
- package/dist/chunk-ZOU3LV4U.js +63 -0
- package/dist/chunk-ZT7B4H2V.js +117 -0
- package/dist/chunk-ZZCBDNZI.js +58 -0
- package/dist/cli.js +498 -112593
- package/dist/computer-tools.js +36642 -21258
- package/dist/harness-catalog.cjs +88 -43
- package/dist/index.js +1 -1
- package/dist/local-model-supervisor.cjs +555 -0
- package/package.json +3 -4
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
MCP_SERVERS_KEY,
|
|
5
|
+
mcpConfigFilePath
|
|
6
|
+
} from "./chunk-DQYVQ2N7.js";
|
|
7
|
+
import {
|
|
8
|
+
atomicWriteFile
|
|
9
|
+
} from "./chunk-F6F47QAY.js";
|
|
10
|
+
import {
|
|
11
|
+
quiet
|
|
12
|
+
} from "./chunk-RDDWQBEM.js";
|
|
13
|
+
import {
|
|
14
|
+
stateDirectory
|
|
15
|
+
} from "./chunk-MPAJ446F.js";
|
|
16
|
+
|
|
17
|
+
// src/harness/mcp-registry.ts
|
|
18
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
19
|
+
import { dirname } from "node:path";
|
|
20
|
+
function isRemoteTarget(target) {
|
|
21
|
+
return /^https?:\/\//i.test(target);
|
|
22
|
+
}
|
|
23
|
+
function mcpConfigEntry(entry) {
|
|
24
|
+
return isRemoteTarget(entry.target) ? { url: entry.target, ...nonEmpty(entry.headers) ? { headers: { ...entry.headers } } : {} } : {
|
|
25
|
+
command: entry.target,
|
|
26
|
+
...entry.args?.length ? { args: [...entry.args] } : {},
|
|
27
|
+
...nonEmpty(entry.env) ? { env: { ...entry.env } } : {}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function nonEmpty(record) {
|
|
31
|
+
return !!record && Object.keys(record).length > 0;
|
|
32
|
+
}
|
|
33
|
+
async function readRoot(path) {
|
|
34
|
+
const existing = await readFile(path, "utf8").catch(quiet("harness/mcp-registry"));
|
|
35
|
+
if (!existing?.trim()) return void 0;
|
|
36
|
+
const parsed = JSON.parse(existing);
|
|
37
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error(`${path} is not a JSON object`);
|
|
38
|
+
return parsed;
|
|
39
|
+
}
|
|
40
|
+
async function writeMcpConfigEntry(path, key, entry) {
|
|
41
|
+
const root = await readRoot(path) ?? {};
|
|
42
|
+
const current = root[key];
|
|
43
|
+
const servers = current && typeof current === "object" && !Array.isArray(current) ? { ...current } : {};
|
|
44
|
+
servers[entry.name] = mcpConfigEntry(entry);
|
|
45
|
+
root[key] = servers;
|
|
46
|
+
await mkdir(dirname(path), { recursive: true });
|
|
47
|
+
await writeFile(path, `${JSON.stringify(root, null, 2)}
|
|
48
|
+
`, "utf8");
|
|
49
|
+
}
|
|
50
|
+
async function recordSharedMcpServer(entry) {
|
|
51
|
+
await writeMcpConfigEntry(mcpConfigFilePath(stateDirectory()), MCP_SERVERS_KEY, entry);
|
|
52
|
+
}
|
|
53
|
+
async function removeMcpConfigEntry(path, key, name) {
|
|
54
|
+
const root = await readRoot(path);
|
|
55
|
+
const servers = root?.[key];
|
|
56
|
+
if (!root || !servers || typeof servers !== "object" || Array.isArray(servers) || !(name in servers)) return false;
|
|
57
|
+
delete servers[name];
|
|
58
|
+
await writeFile(path, `${JSON.stringify(root, null, 2)}
|
|
59
|
+
`, "utf8");
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/harness/mcp-sign-in.ts
|
|
64
|
+
import { mkdir as mkdir2, readFile as readFile2 } from "node:fs/promises";
|
|
65
|
+
import { join } from "node:path";
|
|
66
|
+
var CACHE_FILE = "mcp-sign-in.json";
|
|
67
|
+
var CACHE_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
68
|
+
var PROBE_TIMEOUT_MS = 4e3;
|
|
69
|
+
function carriesCredentials(headers) {
|
|
70
|
+
return Object.entries(headers ?? {}).some(([name, value]) => value.trim() !== "" && /^(authorization|proxy-authorization|cookie)$|api[-_]?key|token|secret|auth/i.test(name));
|
|
71
|
+
}
|
|
72
|
+
function signInFromResponse(status, wwwAuthenticate) {
|
|
73
|
+
return (status === 401 || status === 403) && !!wwwAuthenticate?.trim() ? "sign-in" : "open";
|
|
74
|
+
}
|
|
75
|
+
async function mcpChallenge(url, options = {}) {
|
|
76
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
77
|
+
const ask = async (init) => {
|
|
78
|
+
const response = await fetchImpl(url, { ...init, redirect: "follow", signal: AbortSignal.timeout(options.timeoutMs ?? PROBE_TIMEOUT_MS) });
|
|
79
|
+
const result = { status: response.status, challenge: response.headers.get("www-authenticate") };
|
|
80
|
+
await response.body?.cancel().catch(quiet("harness/mcp-sign-in"));
|
|
81
|
+
return result;
|
|
82
|
+
};
|
|
83
|
+
const posted = await ask({
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { ...options.headers, "content-type": "application/json", accept: "application/json, text/event-stream" },
|
|
86
|
+
body: JSON.stringify({
|
|
87
|
+
jsonrpc: "2.0",
|
|
88
|
+
id: 1,
|
|
89
|
+
method: "initialize",
|
|
90
|
+
params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: options.clientName ?? "clikcode-sign-in-check", version: "1" } }
|
|
91
|
+
})
|
|
92
|
+
});
|
|
93
|
+
if (posted.status === 404 || posted.status === 405) return ask({ method: "GET", headers: { ...options.headers, accept: "text/event-stream" } });
|
|
94
|
+
return posted;
|
|
95
|
+
}
|
|
96
|
+
async function probeRemoteMcp(url, fetchImpl = fetch, timeoutMs = PROBE_TIMEOUT_MS) {
|
|
97
|
+
try {
|
|
98
|
+
const { status, challenge } = await mcpChallenge(url, { fetchImpl, timeoutMs });
|
|
99
|
+
return signInFromResponse(status, challenge);
|
|
100
|
+
} catch {
|
|
101
|
+
return "unknown";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async function readCache(stateDir) {
|
|
105
|
+
try {
|
|
106
|
+
const parsed = JSON.parse(await readFile2(join(stateDir, "cache", CACHE_FILE), "utf8"));
|
|
107
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
|
108
|
+
} catch {
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function mcpServerNeedsSignIn(entry, options) {
|
|
113
|
+
if (!isRemoteTarget(entry.target)) return "open";
|
|
114
|
+
if (options.headersReach !== false && carriesCredentials(entry.headers)) return "open";
|
|
115
|
+
const now = options.now ?? Date.now();
|
|
116
|
+
const cache = await readCache(options.stateDir);
|
|
117
|
+
const hit = cache[entry.target];
|
|
118
|
+
if (hit && now - hit.at < CACHE_TTL_MS && now >= hit.at) return hit.answer;
|
|
119
|
+
const answer = await (options.probe ?? probeRemoteMcp)(entry.target);
|
|
120
|
+
if (answer !== "unknown") {
|
|
121
|
+
const fresh = await readCache(options.stateDir);
|
|
122
|
+
fresh[entry.target] = { answer, at: now };
|
|
123
|
+
await mkdir2(join(options.stateDir, "cache"), { recursive: true });
|
|
124
|
+
await atomicWriteFile(join(options.stateDir, "cache", CACHE_FILE), `${JSON.stringify(fresh, null, 2)}
|
|
125
|
+
`).catch(quiet("harness/mcp-sign-in"));
|
|
126
|
+
}
|
|
127
|
+
return answer;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/agent/mcp/oauth.ts
|
|
131
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
132
|
+
import { readFile as readFile3, rm, stat } from "node:fs/promises";
|
|
133
|
+
import { createServer } from "node:http";
|
|
134
|
+
import { join as join2 } from "node:path";
|
|
135
|
+
var MCP_SIGN_IN_REQUIRED = "MCP_SIGN_IN_REQUIRED";
|
|
136
|
+
var McpSignInRequired = class extends Error {
|
|
137
|
+
constructor(server) {
|
|
138
|
+
super(mcpSignInNote(server));
|
|
139
|
+
this.server = server;
|
|
140
|
+
this.name = "McpSignInRequired";
|
|
141
|
+
}
|
|
142
|
+
server;
|
|
143
|
+
code = MCP_SIGN_IN_REQUIRED;
|
|
144
|
+
};
|
|
145
|
+
function mcpSignInNote(server) {
|
|
146
|
+
return `${server} needs sign-in: run clikcode mcp login ${server}`;
|
|
147
|
+
}
|
|
148
|
+
function isMcpSignInRequired(error) {
|
|
149
|
+
return error?.code === MCP_SIGN_IN_REQUIRED;
|
|
150
|
+
}
|
|
151
|
+
function safeName(name) {
|
|
152
|
+
const clean = name.replace(/[^a-zA-Z0-9._-]/g, "_").slice(0, 64);
|
|
153
|
+
return clean === name ? clean : `${clean}-${createHash("sha256").update(name).digest("hex").slice(0, 8)}`;
|
|
154
|
+
}
|
|
155
|
+
function mcpOAuthFile(stateDir, name) {
|
|
156
|
+
return join2(stateDir, "mcp-oauth", `${safeName(name)}.json`);
|
|
157
|
+
}
|
|
158
|
+
async function readMcpOAuth(stateDir, name, url) {
|
|
159
|
+
try {
|
|
160
|
+
const parsed = JSON.parse(await readFile3(mcpOAuthFile(stateDir, name), "utf8"));
|
|
161
|
+
return parsed && typeof parsed === "object" && parsed.server === url ? parsed : void 0;
|
|
162
|
+
} catch {
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function writeMcpOAuth(stateDir, name, record) {
|
|
167
|
+
await atomicWriteFile(mcpOAuthFile(stateDir, name), `${JSON.stringify({ ...record, updatedAt: (/* @__PURE__ */ new Date()).toISOString() }, null, 2)}
|
|
168
|
+
`);
|
|
169
|
+
}
|
|
170
|
+
async function forgetMcpOAuth(stateDir, name) {
|
|
171
|
+
const file = mcpOAuthFile(stateDir, name);
|
|
172
|
+
const existed = await stat(file).then(() => true, () => false);
|
|
173
|
+
await rm(file, { force: true });
|
|
174
|
+
return existed;
|
|
175
|
+
}
|
|
176
|
+
async function mcpOAuthState(stateDir, spec) {
|
|
177
|
+
const record = await readMcpOAuth(stateDir, spec.name, spec.url);
|
|
178
|
+
if (record?.tokens && !record.needsSignIn) return "signed-in";
|
|
179
|
+
if (record?.needsSignIn || spec.oauth) return "needs-sign-in";
|
|
180
|
+
return "none";
|
|
181
|
+
}
|
|
182
|
+
function parseWwwAuthenticate(header) {
|
|
183
|
+
if (!header?.trim()) return { params: {} };
|
|
184
|
+
const scheme = /^\s*([A-Za-z][\w-]*)/.exec(header)?.[1];
|
|
185
|
+
const params = {};
|
|
186
|
+
for (const match of header.matchAll(/([A-Za-z_][\w-]*)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+))/g)) {
|
|
187
|
+
params[match[1].toLowerCase()] = (match[2] ?? match[3] ?? "").replace(/\\(.)/g, "$1");
|
|
188
|
+
}
|
|
189
|
+
return { ...scheme ? { scheme } : {}, params };
|
|
190
|
+
}
|
|
191
|
+
async function getJson(url, fetchImpl) {
|
|
192
|
+
try {
|
|
193
|
+
const response = await fetchImpl(url, { headers: { accept: "application/json", "mcp-protocol-version": "2025-06-18" }, signal: AbortSignal.timeout(1e4) });
|
|
194
|
+
if (!response.ok) {
|
|
195
|
+
await response.body?.cancel().catch(quiet("agent/mcp/oauth"));
|
|
196
|
+
return void 0;
|
|
197
|
+
}
|
|
198
|
+
const parsed = await response.json();
|
|
199
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : void 0;
|
|
200
|
+
} catch {
|
|
201
|
+
return void 0;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function wellKnown(base, suffix) {
|
|
205
|
+
const url = new URL(base);
|
|
206
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
207
|
+
const at = (p) => `${url.origin}/.well-known/${suffix}${p}`;
|
|
208
|
+
return path ? [at(path), at("")] : [at("")];
|
|
209
|
+
}
|
|
210
|
+
function authorizationServerUrls(issuer) {
|
|
211
|
+
const url = new URL(issuer);
|
|
212
|
+
const path = url.pathname.replace(/\/+$/, "");
|
|
213
|
+
if (!path) return [`${url.origin}/.well-known/oauth-authorization-server`, `${url.origin}/.well-known/openid-configuration`];
|
|
214
|
+
return [
|
|
215
|
+
`${url.origin}/.well-known/oauth-authorization-server${path}`,
|
|
216
|
+
`${url.origin}/.well-known/openid-configuration${path}`,
|
|
217
|
+
`${url.origin}${path}/.well-known/openid-configuration`
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
async function discoverMcpOAuth(spec, challenge, fetchImpl = fetch) {
|
|
221
|
+
const { params } = parseWwwAuthenticate(challenge);
|
|
222
|
+
let protectedResource;
|
|
223
|
+
for (const candidate of [...params.resource_metadata ? [params.resource_metadata] : [], ...wellKnown(spec.url, "oauth-protected-resource")]) {
|
|
224
|
+
protectedResource = await getJson(new URL(candidate, spec.url).toString(), fetchImpl);
|
|
225
|
+
if (protectedResource) break;
|
|
226
|
+
}
|
|
227
|
+
const servers = Array.isArray(protectedResource?.authorization_servers) ? protectedResource.authorization_servers.filter((item) => typeof item === "string") : [];
|
|
228
|
+
const issuer = servers[0] ?? new URL(spec.url).origin;
|
|
229
|
+
let metadata;
|
|
230
|
+
for (const candidate of authorizationServerUrls(issuer)) {
|
|
231
|
+
metadata = await getJson(candidate, fetchImpl);
|
|
232
|
+
if (metadata?.authorization_endpoint && metadata.token_endpoint) break;
|
|
233
|
+
metadata = void 0;
|
|
234
|
+
}
|
|
235
|
+
if (!metadata && servers.length) throw new Error(`the authorization server ${issuer} publishes no metadata`);
|
|
236
|
+
const origin = new URL(issuer).origin;
|
|
237
|
+
const endpoints = metadata ?? { authorization_endpoint: `${origin}/authorize`, token_endpoint: `${origin}/token`, registration_endpoint: `${origin}/register` };
|
|
238
|
+
const methods = metadata?.code_challenge_methods_supported;
|
|
239
|
+
if (Array.isArray(methods) && methods.length && !methods.includes("S256")) {
|
|
240
|
+
throw new Error(`the authorization server ${issuer} does not support PKCE S256`);
|
|
241
|
+
}
|
|
242
|
+
const prmScopes = Array.isArray(protectedResource?.scopes_supported) ? protectedResource.scopes_supported.filter((item) => typeof item === "string") : [];
|
|
243
|
+
const scope = params.scope || spec.oauth?.scope || (prmScopes.length ? prmScopes.join(" ") : void 0);
|
|
244
|
+
return {
|
|
245
|
+
resource: typeof protectedResource?.resource === "string" ? protectedResource.resource : spec.url,
|
|
246
|
+
issuer,
|
|
247
|
+
authorizationEndpoint: endpoints.authorization_endpoint,
|
|
248
|
+
tokenEndpoint: endpoints.token_endpoint,
|
|
249
|
+
...endpoints.registration_endpoint ? { registrationEndpoint: endpoints.registration_endpoint } : {},
|
|
250
|
+
...scope ? { scope } : {},
|
|
251
|
+
...metadata?.token_endpoint_auth_methods_supported ? { authMethods: metadata.token_endpoint_auth_methods_supported } : {}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
var TokenRefused = class extends Error {
|
|
255
|
+
};
|
|
256
|
+
function base64url(buffer) {
|
|
257
|
+
return buffer.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
258
|
+
}
|
|
259
|
+
function pkcePair() {
|
|
260
|
+
const verifier = base64url(randomBytes(32));
|
|
261
|
+
return { verifier, challenge: base64url(createHash("sha256").update(verifier).digest()) };
|
|
262
|
+
}
|
|
263
|
+
async function tokenRequest(record, form, fetchImpl, now) {
|
|
264
|
+
const client = record.client;
|
|
265
|
+
const body = new URLSearchParams({ ...form, ...record.resource ? { resource: record.resource } : {} });
|
|
266
|
+
const headers = { "content-type": "application/x-www-form-urlencoded", accept: "application/json" };
|
|
267
|
+
if (client.secret && client.authMethod === "client_secret_basic") {
|
|
268
|
+
headers.authorization = `Basic ${Buffer.from(`${encodeURIComponent(client.id)}:${encodeURIComponent(client.secret)}`).toString("base64")}`;
|
|
269
|
+
} else {
|
|
270
|
+
body.set("client_id", client.id);
|
|
271
|
+
if (client.secret) body.set("client_secret", client.secret);
|
|
272
|
+
}
|
|
273
|
+
const response = await fetchImpl(record.tokenEndpoint, { method: "POST", headers, body: body.toString(), signal: AbortSignal.timeout(3e4) });
|
|
274
|
+
const text = await response.text().catch(() => "");
|
|
275
|
+
let parsed = {};
|
|
276
|
+
try {
|
|
277
|
+
parsed = JSON.parse(text);
|
|
278
|
+
} catch {
|
|
279
|
+
}
|
|
280
|
+
if (!response.ok || typeof parsed.access_token !== "string") {
|
|
281
|
+
const reason = [parsed.error, parsed.error_description].filter((item) => typeof item === "string").join(": ") || `HTTP ${response.status}`;
|
|
282
|
+
if (response.status >= 400 && response.status < 500) throw new TokenRefused(`the token endpoint refused: ${reason}`);
|
|
283
|
+
throw new Error(`the token endpoint answered ${reason}`);
|
|
284
|
+
}
|
|
285
|
+
const expiresIn = Number(parsed.expires_in);
|
|
286
|
+
return {
|
|
287
|
+
accessToken: parsed.access_token,
|
|
288
|
+
...typeof parsed.refresh_token === "string" ? { refreshToken: parsed.refresh_token } : {},
|
|
289
|
+
...Number.isFinite(expiresIn) && expiresIn > 0 ? { expiresAt: now + expiresIn * 1e3 } : {},
|
|
290
|
+
...typeof parsed.scope === "string" ? { scope: parsed.scope } : {}
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
var REFRESH_EARLY_MS = 6e4;
|
|
294
|
+
function mcpOAuthProvider(stateDir, spec, options = {}) {
|
|
295
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
296
|
+
const now = options.now ?? Date.now;
|
|
297
|
+
let refreshing;
|
|
298
|
+
const read = () => readMcpOAuth(stateDir, spec.name, spec.url);
|
|
299
|
+
const markNeedsSignIn = async () => {
|
|
300
|
+
const record = await read();
|
|
301
|
+
await writeMcpOAuth(stateDir, spec.name, { ...record ?? { server: spec.url }, tokens: void 0, needsSignIn: true }).catch(quiet("agent/mcp/oauth"));
|
|
302
|
+
};
|
|
303
|
+
const refresh = () => refreshing ??= (async () => {
|
|
304
|
+
const record = await read();
|
|
305
|
+
if (!record?.tokens?.refreshToken || !record.client || !record.tokenEndpoint) return "refused";
|
|
306
|
+
try {
|
|
307
|
+
const tokens = await tokenRequest(record, { grant_type: "refresh_token", refresh_token: record.tokens.refreshToken }, fetchImpl, now());
|
|
308
|
+
await writeMcpOAuth(stateDir, spec.name, { ...record, tokens: { refreshToken: record.tokens.refreshToken, ...tokens }, needsSignIn: false });
|
|
309
|
+
return "ok";
|
|
310
|
+
} catch (error) {
|
|
311
|
+
if (error instanceof TokenRefused) {
|
|
312
|
+
await markNeedsSignIn();
|
|
313
|
+
return "refused";
|
|
314
|
+
}
|
|
315
|
+
return "failed";
|
|
316
|
+
}
|
|
317
|
+
})().finally(() => {
|
|
318
|
+
refreshing = void 0;
|
|
319
|
+
});
|
|
320
|
+
const header = (record) => record?.tokens && !record.needsSignIn ? `Bearer ${record.tokens.accessToken}` : void 0;
|
|
321
|
+
return {
|
|
322
|
+
async authorization() {
|
|
323
|
+
const record = await read();
|
|
324
|
+
if (!record?.tokens || record.needsSignIn) return void 0;
|
|
325
|
+
const expiresAt = record.tokens.expiresAt;
|
|
326
|
+
if (expiresAt !== void 0 && now() >= expiresAt - REFRESH_EARLY_MS && record.tokens.refreshToken) {
|
|
327
|
+
const outcome = await refresh();
|
|
328
|
+
if (outcome === "refused") throw new McpSignInRequired(spec.name);
|
|
329
|
+
return header(await read()) ?? header(record);
|
|
330
|
+
}
|
|
331
|
+
return header(record);
|
|
332
|
+
},
|
|
333
|
+
async unauthorized(challenge, sent, final) {
|
|
334
|
+
const record = await read();
|
|
335
|
+
const current = header(record);
|
|
336
|
+
if (!final && current && current !== sent) return "retry";
|
|
337
|
+
if (!final && record?.tokens?.refreshToken) {
|
|
338
|
+
const outcome = await refresh();
|
|
339
|
+
if (outcome === "ok") return "retry";
|
|
340
|
+
if (outcome === "refused") return "sign-in";
|
|
341
|
+
return "fail";
|
|
342
|
+
}
|
|
343
|
+
const bearer = parseWwwAuthenticate(challenge).scheme?.toLowerCase() === "bearer";
|
|
344
|
+
if (!bearer && !record && !spec.oauth) return "fail";
|
|
345
|
+
await markNeedsSignIn();
|
|
346
|
+
return "sign-in";
|
|
347
|
+
},
|
|
348
|
+
async stamp() {
|
|
349
|
+
try {
|
|
350
|
+
const info = await stat(mcpOAuthFile(stateDir, spec.name));
|
|
351
|
+
return `${info.mtimeMs}:${info.size}`;
|
|
352
|
+
} catch {
|
|
353
|
+
return "";
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function parsePastedRedirect(text) {
|
|
359
|
+
const trimmed = text.trim();
|
|
360
|
+
if (!trimmed) return {};
|
|
361
|
+
if (!/(?:^|[?&#])(?:code|error)=/.test(trimmed)) return { code: trimmed };
|
|
362
|
+
const at = trimmed.search(/[?#]/);
|
|
363
|
+
const query = (at < 0 ? trimmed : trimmed.slice(at + 1)).replace(/#/g, "&");
|
|
364
|
+
const params = new URLSearchParams(query);
|
|
365
|
+
return {
|
|
366
|
+
...params.get("code") ? { code: params.get("code") } : {},
|
|
367
|
+
...params.get("state") ? { state: params.get("state") } : {},
|
|
368
|
+
...params.get("error") ? { error: [params.get("error"), params.get("error_description")].filter(Boolean).join(": ") } : {}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
var CALLBACK_PAGE = (title, body) => `<!doctype html><meta charset="utf-8"><title>${title}</title><body style="font-family:system-ui;margin:3em"><h2>${title}</h2><p>${body}</p></body>`;
|
|
372
|
+
function listen(server, port) {
|
|
373
|
+
return new Promise((resolve, reject) => {
|
|
374
|
+
const onError = (error) => {
|
|
375
|
+
server.off("listening", onListening);
|
|
376
|
+
reject(error);
|
|
377
|
+
};
|
|
378
|
+
const onListening = () => {
|
|
379
|
+
server.off("error", onError);
|
|
380
|
+
resolve(server.address().port);
|
|
381
|
+
};
|
|
382
|
+
server.once("error", onError);
|
|
383
|
+
server.once("listening", onListening);
|
|
384
|
+
server.listen(port, "127.0.0.1");
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
async function signInMcpServer(options) {
|
|
388
|
+
const { stateDir, spec, ui } = options;
|
|
389
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
390
|
+
const now = options.now ?? Date.now;
|
|
391
|
+
const { status, challenge } = await mcpChallenge(spec.url, { headers: spec.headers, fetchImpl, timeoutMs: 15e3, clientName: "clikcode" });
|
|
392
|
+
if (status !== 401 && status !== 403 && !spec.oauth && !parseWwwAuthenticate(challenge).params.resource_metadata) {
|
|
393
|
+
throw new Error(`${spec.name} does not ask for a sign-in (it answered HTTP ${status})`);
|
|
394
|
+
}
|
|
395
|
+
const discovery = await discoverMcpOAuth(spec, challenge, fetchImpl);
|
|
396
|
+
const previous = await readMcpOAuth(stateDir, spec.name, spec.url);
|
|
397
|
+
const server = createServer();
|
|
398
|
+
const configuredPort = spec.oauth?.callbackPort;
|
|
399
|
+
const reusable = !spec.oauth?.clientId && previous?.client?.registered && previous.issuer === discovery.issuer ? previous.client : void 0;
|
|
400
|
+
const reusePort = reusable?.redirectUri ? Number(new URL(reusable.redirectUri).port) : void 0;
|
|
401
|
+
let port;
|
|
402
|
+
try {
|
|
403
|
+
port = await listen(server, configuredPort ?? reusePort ?? 0);
|
|
404
|
+
} catch (error) {
|
|
405
|
+
if (configuredPort !== void 0) throw new Error(`port ${configuredPort} for the sign-in callback is in use: ${error.message}`);
|
|
406
|
+
port = await listen(server, 0);
|
|
407
|
+
}
|
|
408
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`;
|
|
409
|
+
try {
|
|
410
|
+
let client;
|
|
411
|
+
if (spec.oauth?.clientId) {
|
|
412
|
+
client = { id: spec.oauth.clientId, redirectUri };
|
|
413
|
+
} else if (reusable && reusable.redirectUri === redirectUri) {
|
|
414
|
+
client = reusable;
|
|
415
|
+
} else {
|
|
416
|
+
if (!discovery.registrationEndpoint) {
|
|
417
|
+
throw new Error(`${spec.name}'s authorization server does not register clients; add it again with --client-id <id>`);
|
|
418
|
+
}
|
|
419
|
+
client = await registerClient(discovery, redirectUri, fetchImpl);
|
|
420
|
+
}
|
|
421
|
+
const pkce = pkcePair();
|
|
422
|
+
const state = base64url(randomBytes(16));
|
|
423
|
+
const authorize = new URL(discovery.authorizationEndpoint);
|
|
424
|
+
for (const [key, value] of Object.entries({
|
|
425
|
+
response_type: "code",
|
|
426
|
+
client_id: client.id,
|
|
427
|
+
redirect_uri: redirectUri,
|
|
428
|
+
code_challenge: pkce.challenge,
|
|
429
|
+
code_challenge_method: "S256",
|
|
430
|
+
state,
|
|
431
|
+
resource: discovery.resource,
|
|
432
|
+
...discovery.scope ? { scope: discovery.scope } : {}
|
|
433
|
+
})) authorize.searchParams.set(key, value);
|
|
434
|
+
const code = await waitForCode({ server, state, ui, url: authorize.toString(), name: spec.name, timeoutMs: options.timeoutMs ?? 10 * 6e4 });
|
|
435
|
+
const record = {
|
|
436
|
+
server: spec.url,
|
|
437
|
+
resource: discovery.resource,
|
|
438
|
+
issuer: discovery.issuer,
|
|
439
|
+
authorizationEndpoint: discovery.authorizationEndpoint,
|
|
440
|
+
tokenEndpoint: discovery.tokenEndpoint,
|
|
441
|
+
...discovery.registrationEndpoint ? { registrationEndpoint: discovery.registrationEndpoint } : {},
|
|
442
|
+
...discovery.scope ? { scope: discovery.scope } : {},
|
|
443
|
+
client
|
|
444
|
+
};
|
|
445
|
+
const tokens = await tokenRequest(record, { grant_type: "authorization_code", code, redirect_uri: redirectUri, code_verifier: pkce.verifier }, fetchImpl, now()).catch((error) => {
|
|
446
|
+
throw new Error(`${spec.name}: ${error.message}`);
|
|
447
|
+
});
|
|
448
|
+
await writeMcpOAuth(stateDir, spec.name, { ...record, tokens, needsSignIn: false });
|
|
449
|
+
} finally {
|
|
450
|
+
server.closeAllConnections();
|
|
451
|
+
await new Promise((resolve) => server.close(resolve));
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
async function registerClient(discovery, redirectUri, fetchImpl) {
|
|
455
|
+
const methods = discovery.authMethods;
|
|
456
|
+
const method = !methods?.length || methods.includes("none") ? "none" : methods.includes("client_secret_post") ? "client_secret_post" : methods[0];
|
|
457
|
+
const response = await fetchImpl(discovery.registrationEndpoint, {
|
|
458
|
+
method: "POST",
|
|
459
|
+
headers: { "content-type": "application/json", accept: "application/json" },
|
|
460
|
+
body: JSON.stringify({
|
|
461
|
+
client_name: "ClikCode",
|
|
462
|
+
redirect_uris: [redirectUri],
|
|
463
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
464
|
+
response_types: ["code"],
|
|
465
|
+
token_endpoint_auth_method: method,
|
|
466
|
+
...discovery.scope ? { scope: discovery.scope } : {}
|
|
467
|
+
}),
|
|
468
|
+
signal: AbortSignal.timeout(3e4)
|
|
469
|
+
});
|
|
470
|
+
const text = await response.text().catch(() => "");
|
|
471
|
+
let parsed = {};
|
|
472
|
+
try {
|
|
473
|
+
parsed = JSON.parse(text);
|
|
474
|
+
} catch {
|
|
475
|
+
}
|
|
476
|
+
if (!response.ok || typeof parsed.client_id !== "string") {
|
|
477
|
+
const reason = [parsed.error, parsed.error_description].filter((item) => typeof item === "string").join(": ") || `HTTP ${response.status}`;
|
|
478
|
+
throw new Error(`client registration was refused (${reason}); add the server again with --client-id <id>`);
|
|
479
|
+
}
|
|
480
|
+
return {
|
|
481
|
+
id: parsed.client_id,
|
|
482
|
+
...typeof parsed.client_secret === "string" ? { secret: parsed.client_secret } : {},
|
|
483
|
+
authMethod: typeof parsed.token_endpoint_auth_method === "string" ? parsed.token_endpoint_auth_method : method,
|
|
484
|
+
redirectUri,
|
|
485
|
+
registered: true
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
function waitForCode(input) {
|
|
489
|
+
const { server, state, ui } = input;
|
|
490
|
+
const done = new AbortController();
|
|
491
|
+
return new Promise((resolve, reject) => {
|
|
492
|
+
const finish = (error, code) => {
|
|
493
|
+
if (done.signal.aborted) return;
|
|
494
|
+
done.abort();
|
|
495
|
+
clearTimeout(timer);
|
|
496
|
+
ui.signal?.removeEventListener("abort", onCancel);
|
|
497
|
+
if (error) reject(error);
|
|
498
|
+
else resolve(code);
|
|
499
|
+
};
|
|
500
|
+
const onCancel = () => finish(Object.assign(new Error(`Sign-in to ${input.name} cancelled`), { name: "AbortError" }));
|
|
501
|
+
const timer = setTimeout(() => finish(new Error(`Sign-in to ${input.name} timed out`)), input.timeoutMs);
|
|
502
|
+
timer.unref?.();
|
|
503
|
+
if (ui.signal?.aborted) return onCancel();
|
|
504
|
+
ui.signal?.addEventListener("abort", onCancel, { once: true });
|
|
505
|
+
const accept = (answer) => {
|
|
506
|
+
if (answer.state !== void 0 && answer.state !== state) return "that answer belongs to a different sign-in";
|
|
507
|
+
if (answer.error) {
|
|
508
|
+
finish(new Error(`${input.name} refused the sign-in: ${answer.error}`));
|
|
509
|
+
return answer.error;
|
|
510
|
+
}
|
|
511
|
+
if (!answer.code) return "no code in that answer";
|
|
512
|
+
finish(void 0, answer.code);
|
|
513
|
+
return void 0;
|
|
514
|
+
};
|
|
515
|
+
server.on("request", (request, response) => {
|
|
516
|
+
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
517
|
+
if (url.pathname !== "/callback") {
|
|
518
|
+
response.writeHead(404).end();
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const answer = {
|
|
522
|
+
...url.searchParams.get("code") ? { code: url.searchParams.get("code") } : {},
|
|
523
|
+
// The loopback answer must carry this sign-in's state: anything else
|
|
524
|
+
// on this machine could otherwise hand us a code of its choosing.
|
|
525
|
+
state: url.searchParams.get("state") ?? "",
|
|
526
|
+
...url.searchParams.get("error") ? { error: [url.searchParams.get("error"), url.searchParams.get("error_description")].filter(Boolean).join(": ") } : {}
|
|
527
|
+
};
|
|
528
|
+
const problem = accept(answer);
|
|
529
|
+
response.writeHead(problem ? 400 : 200, { "content-type": "text/html; charset=utf-8" });
|
|
530
|
+
response.end(problem ? CALLBACK_PAGE("Sign-in did not finish", `ClikCode could not use this answer: ${problem.replace(/[<>&]/g, "")}.`) : CALLBACK_PAGE(`Signed in to ${input.name.replace(/[<>&]/g, "")}`, "You can close this tab and go back to ClikCode."));
|
|
531
|
+
});
|
|
532
|
+
ui.show({ url: input.url });
|
|
533
|
+
void (async () => {
|
|
534
|
+
let prompt = "Or paste the address your browser ended on (or its code)";
|
|
535
|
+
while (!done.signal.aborted) {
|
|
536
|
+
let text;
|
|
537
|
+
try {
|
|
538
|
+
text = await ui.ask(prompt, done.signal);
|
|
539
|
+
} catch {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (done.signal.aborted) return;
|
|
543
|
+
const problem = accept(parsePastedRedirect(text));
|
|
544
|
+
if (!problem) return;
|
|
545
|
+
prompt = `${problem[0].toUpperCase()}${problem.slice(1)} \xB7 paste the address again`;
|
|
546
|
+
}
|
|
547
|
+
})();
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export {
|
|
552
|
+
isRemoteTarget,
|
|
553
|
+
recordSharedMcpServer,
|
|
554
|
+
removeMcpConfigEntry,
|
|
555
|
+
carriesCredentials,
|
|
556
|
+
mcpServerNeedsSignIn,
|
|
557
|
+
McpSignInRequired,
|
|
558
|
+
mcpSignInNote,
|
|
559
|
+
isMcpSignInRequired,
|
|
560
|
+
forgetMcpOAuth,
|
|
561
|
+
mcpOAuthState,
|
|
562
|
+
mcpOAuthProvider,
|
|
563
|
+
signInMcpServer
|
|
564
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
parseQuery,
|
|
5
|
+
parseSince,
|
|
6
|
+
recencyBoost,
|
|
7
|
+
searchConversations,
|
|
8
|
+
titleMatch
|
|
9
|
+
} from "./chunk-WNX5VD7S.js";
|
|
10
|
+
import "./chunk-WM3Z6QKQ.js";
|
|
11
|
+
import "./chunk-BISGNHKD.js";
|
|
12
|
+
import "./chunk-XCKQ5BM2.js";
|
|
13
|
+
import "./chunk-NWEYRBWK.js";
|
|
14
|
+
import "./chunk-BUOPJQP5.js";
|
|
15
|
+
import "./chunk-3B2CHX7M.js";
|
|
16
|
+
import "./chunk-RVUOIJXN.js";
|
|
17
|
+
import "./chunk-QUS4ER2W.js";
|
|
18
|
+
import "./chunk-DQYVQ2N7.js";
|
|
19
|
+
import "./chunk-NKDATMEM.js";
|
|
20
|
+
import "./chunk-XKW5NDMR.js";
|
|
21
|
+
import "./chunk-W3YYPBEA.js";
|
|
22
|
+
import "./chunk-XIBYREEY.js";
|
|
23
|
+
import "./chunk-EE4B6A7W.js";
|
|
24
|
+
import "./chunk-O3EW7LAY.js";
|
|
25
|
+
import "./chunk-KHW6TX6I.js";
|
|
26
|
+
import "./chunk-YMCH3L2S.js";
|
|
27
|
+
import "./chunk-G3QP67Z7.js";
|
|
28
|
+
import "./chunk-4L2ZZM4Q.js";
|
|
29
|
+
import "./chunk-F6F47QAY.js";
|
|
30
|
+
import "./chunk-RDDWQBEM.js";
|
|
31
|
+
import "./chunk-MPAJ446F.js";
|
|
32
|
+
import "./chunk-CBANJFBO.js";
|
|
33
|
+
import "./chunk-PWXII6HF.js";
|
|
34
|
+
export {
|
|
35
|
+
parseQuery,
|
|
36
|
+
parseSince,
|
|
37
|
+
recencyBoost,
|
|
38
|
+
searchConversations,
|
|
39
|
+
titleMatch
|
|
40
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { createRequire as __clikcodeCreateRequire } from 'node:module';
|
|
2
|
+
const require = __clikcodeCreateRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
LineBuffer
|
|
5
|
+
} from "./chunk-XIBYREEY.js";
|
|
6
|
+
import {
|
|
7
|
+
swallowed
|
|
8
|
+
} from "./chunk-RDDWQBEM.js";
|
|
9
|
+
|
|
10
|
+
// src/worker/protocol.ts
|
|
11
|
+
import { StringDecoder } from "node:string_decoder";
|
|
12
|
+
var cancelOrigin = (client, trigger) => ({ client, pid: process.pid, trigger });
|
|
13
|
+
var cancelOriginFields = (command) => command.origin ? { client: command.origin.client, clientPid: command.origin.pid, trigger: command.origin.trigger } : { client: "unknown", trigger: "unknown" };
|
|
14
|
+
var MOVED_REASON = "the chat moved to another machine";
|
|
15
|
+
function isTranscriptActivity(event) {
|
|
16
|
+
return event.kind !== "thinking";
|
|
17
|
+
}
|
|
18
|
+
function shownSettingsKey(session) {
|
|
19
|
+
return [session.id, session.route, session.accountId, session.provider, session.model, session.effort, session.nativeHarness].join("|");
|
|
20
|
+
}
|
|
21
|
+
var FRAME_SEPARATOR = "\n";
|
|
22
|
+
function encodeFrame(message) {
|
|
23
|
+
return `${JSON.stringify(message)}${FRAME_SEPARATOR}`;
|
|
24
|
+
}
|
|
25
|
+
var FrameDecoder = class {
|
|
26
|
+
text = new StringDecoder("utf8");
|
|
27
|
+
lines = new LineBuffer();
|
|
28
|
+
/** The messages this chunk completed, in order. */
|
|
29
|
+
push(chunk) {
|
|
30
|
+
const messages = [];
|
|
31
|
+
for (const frame of this.lines.push(typeof chunk === "string" ? chunk : this.text.write(chunk))) {
|
|
32
|
+
if (!frame) continue;
|
|
33
|
+
try {
|
|
34
|
+
messages.push(JSON.parse(frame));
|
|
35
|
+
} catch (error) {
|
|
36
|
+
swallowed("worker/protocol", error);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return messages;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
cancelOrigin,
|
|
45
|
+
cancelOriginFields,
|
|
46
|
+
MOVED_REASON,
|
|
47
|
+
isTranscriptActivity,
|
|
48
|
+
shownSettingsKey,
|
|
49
|
+
encodeFrame,
|
|
50
|
+
FrameDecoder
|
|
51
|
+
};
|