gong-code 0.1.0
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 +38 -0
- package/dist/SKILL-22jccbka.md +1 -0
- package/dist/chunk-01wdn84e.js +8 -0
- package/dist/chunk-04bc74vz.js +243 -0
- package/dist/chunk-05t2vqsb.js +490 -0
- package/dist/chunk-0727wret.js +159 -0
- package/dist/chunk-07rervty.js +200 -0
- package/dist/chunk-0b9nxvyg.js +478 -0
- package/dist/chunk-0e428b73.js +600 -0
- package/dist/chunk-0hqfheht.js +3439 -0
- package/dist/chunk-0pnk52c2.js +116 -0
- package/dist/chunk-0sbddf6m.js +68 -0
- package/dist/chunk-0vf7xb84.js +176 -0
- package/dist/chunk-0yz17yjy.js +1061 -0
- package/dist/chunk-0zscb6c7.js +10 -0
- package/dist/chunk-10p21kq6.js +378 -0
- package/dist/chunk-17k78pmz.js +120 -0
- package/dist/chunk-19jvvqzp.js +1390 -0
- package/dist/chunk-1d5czqnr.js +173 -0
- package/dist/chunk-1gjysfnf.js +13876 -0
- package/dist/chunk-1jjfv0wd.js +467 -0
- package/dist/chunk-1jjzp2pa.js +617 -0
- package/dist/chunk-1m38dj2k.js +155 -0
- package/dist/chunk-1pgttway.js +211 -0
- package/dist/chunk-1vvnx0gb.js +8035 -0
- package/dist/chunk-1ztm9yps.js +49145 -0
- package/dist/chunk-208x1t0m.js +88 -0
- package/dist/chunk-20xqs3yk.js +843 -0
- package/dist/chunk-2b2c0wnn.js +584 -0
- package/dist/chunk-2g07117j.js +541 -0
- package/dist/chunk-2g6p2t3w.js +780 -0
- package/dist/chunk-2kjeaeg7.js +28 -0
- package/dist/chunk-2n0s2dhc.js +12 -0
- package/dist/chunk-2p0hvt8k.js +785 -0
- package/dist/chunk-2qmxg9y7.js +40174 -0
- package/dist/chunk-2qpy8kne.js +28 -0
- package/dist/chunk-2rgbf62a.js +40 -0
- package/dist/chunk-2tx0s41y.js +150 -0
- package/dist/chunk-2x8zk5cz.js +101 -0
- package/dist/chunk-2yqy25z4.js +48 -0
- package/dist/chunk-318g78ty.js +552 -0
- package/dist/chunk-31gfg8tz.js +290 -0
- package/dist/chunk-32n0s532.js +1317 -0
- package/dist/chunk-35dw2r9g.js +137 -0
- package/dist/chunk-36b5zqvn.js +1865 -0
- package/dist/chunk-38kpx69j.js +97 -0
- package/dist/chunk-3b01vp8p.js +815 -0
- package/dist/chunk-3f76a1ek.js +317 -0
- package/dist/chunk-3fd1hkhh.js +4254 -0
- package/dist/chunk-3ffxa7zj.js +8 -0
- package/dist/chunk-3gcj7w95.js +8 -0
- package/dist/chunk-3jf3k40v.js +970 -0
- package/dist/chunk-3k51qfp9.js +9301 -0
- package/dist/chunk-3mhygd2v.js +1180 -0
- package/dist/chunk-3p6y0gvb.js +117 -0
- package/dist/chunk-3r60xdt5.js +93 -0
- package/dist/chunk-43agf6xv.js +8 -0
- package/dist/chunk-46zr418r.js +15 -0
- package/dist/chunk-48k2bs4w.js +118 -0
- package/dist/chunk-4p987a4z.js +84 -0
- package/dist/chunk-4px25pe0.js +776 -0
- package/dist/chunk-4s93jyky.js +113 -0
- package/dist/chunk-4xv8bedf.js +58 -0
- package/dist/chunk-4y382mzd.js +346 -0
- package/dist/chunk-52dzw4bc.js +246 -0
- package/dist/chunk-52qqmh5g.js +110 -0
- package/dist/chunk-53grnhp7.js +8 -0
- package/dist/chunk-59craaxx.js +4249 -0
- package/dist/chunk-5b833jqs.js +96 -0
- package/dist/chunk-5cp2q9sh.js +58 -0
- package/dist/chunk-5dbaxmts.js +87 -0
- package/dist/chunk-5f5sxgp0.js +96 -0
- package/dist/chunk-5mkfderj.js +195 -0
- package/dist/chunk-5qw0mpvq.js +389 -0
- package/dist/chunk-5rxmszm8.js +48 -0
- package/dist/chunk-5ygxa3hr.js +65 -0
- package/dist/chunk-61d5dqq2.js +71 -0
- package/dist/chunk-6350958y.js +272 -0
- package/dist/chunk-65z8hg8j.js +1337 -0
- package/dist/chunk-6aq85gdq.js +8 -0
- package/dist/chunk-6cjh9exg.js +40751 -0
- package/dist/chunk-6e41p5m6.js +187 -0
- package/dist/chunk-6jb6xcnq.js +10511 -0
- package/dist/chunk-6jta29r1.js +20 -0
- package/dist/chunk-6q7f2rrv.js +4757 -0
- package/dist/chunk-6sg0ec6v.js +41 -0
- package/dist/chunk-6v0rhx4e.js +328 -0
- package/dist/chunk-6x5mz95v.js +751 -0
- package/dist/chunk-6xqp51qr.js +87 -0
- package/dist/chunk-6xvyx1s9.js +6957 -0
- package/dist/chunk-6ywh7wgp.js +468 -0
- package/dist/chunk-75ne77gm.js +1920 -0
- package/dist/chunk-77cb06js.js +602 -0
- package/dist/chunk-78n9kfp8.js +1645 -0
- package/dist/chunk-7be87rww.js +277 -0
- package/dist/chunk-7e1qjk3s.js +125 -0
- package/dist/chunk-7e7nxnss.js +360 -0
- package/dist/chunk-7g8156qp.js +55 -0
- package/dist/chunk-7nz71s42.js +3353 -0
- package/dist/chunk-7r078t2x.js +113 -0
- package/dist/chunk-7shx41hj.js +3094 -0
- package/dist/chunk-7yexhdjs.js +216 -0
- package/dist/chunk-83kmfcjv.js +256 -0
- package/dist/chunk-846rr9n1.js +107 -0
- package/dist/chunk-84ncsm1r.js +183 -0
- package/dist/chunk-867x8aq0.js +8936 -0
- package/dist/chunk-8byh30kb.js +1947 -0
- package/dist/chunk-8c8f6gnt.js +432 -0
- package/dist/chunk-8gtdseev.js +454 -0
- package/dist/chunk-8jfh9rq6.js +642 -0
- package/dist/chunk-8n4n4m67.js +800 -0
- package/dist/chunk-8za61tze.js +847 -0
- package/dist/chunk-9029sbq1.js +57 -0
- package/dist/chunk-94dm5rrm.js +673 -0
- package/dist/chunk-9bs8n985.js +329 -0
- package/dist/chunk-9c23776j.js +763 -0
- package/dist/chunk-9e93g81n.js +298 -0
- package/dist/chunk-9f3d61y7.js +548 -0
- package/dist/chunk-9gc3andd.js +422 -0
- package/dist/chunk-9kn3tgpb.js +130 -0
- package/dist/chunk-9m5rn7hk.js +61 -0
- package/dist/chunk-9p4hsd6a.js +50 -0
- package/dist/chunk-9qasfk8n.js +758 -0
- package/dist/chunk-9snp5mn2.js +533 -0
- package/dist/chunk-9tcvras3.js +2331 -0
- package/dist/chunk-a0186ngk.js +57 -0
- package/dist/chunk-a2m8f0aj.js +268 -0
- package/dist/chunk-a2rcafav.js +716 -0
- package/dist/chunk-a9hjdzar.js +160 -0
- package/dist/chunk-aak6ts5n.js +526 -0
- package/dist/chunk-aca3w84n.js +267 -0
- package/dist/chunk-ae76ded0.js +30 -0
- package/dist/chunk-afnjhn3j.js +21933 -0
- package/dist/chunk-ahj70tf0.js +880 -0
- package/dist/chunk-apjsxssq.js +169 -0
- package/dist/chunk-av49wjj1.js +43 -0
- package/dist/chunk-axggebfy.js +206 -0
- package/dist/chunk-az7jpbv8.js +16 -0
- package/dist/chunk-azz7sep6.js +328 -0
- package/dist/chunk-b4tpz2g1.js +93 -0
- package/dist/chunk-bczf2eyq.js +115 -0
- package/dist/chunk-bd95f637.js +208 -0
- package/dist/chunk-bdhdmkya.js +10 -0
- package/dist/chunk-bp0ynk17.js +32 -0
- package/dist/chunk-bq75gxjs.js +90 -0
- package/dist/chunk-btk5jaq6.js +10464 -0
- package/dist/chunk-bv4c48a5.js +120 -0
- package/dist/chunk-byfb105n.js +341 -0
- package/dist/chunk-c0bdmy7w.js +4223 -0
- package/dist/chunk-c20aqkz8.js +641 -0
- package/dist/chunk-c4jg3s2c.js +163 -0
- package/dist/chunk-c61q5cer.js +258 -0
- package/dist/chunk-c9f761sy.js +386 -0
- package/dist/chunk-ca6jkz06.js +157 -0
- package/dist/chunk-ccyg9ap9.js +171374 -0
- package/dist/chunk-cfej8cc7.js +371 -0
- package/dist/chunk-cjqxdnf9.js +23 -0
- package/dist/chunk-ckrs789w.js +8 -0
- package/dist/chunk-cqeq13wg.js +48 -0
- package/dist/chunk-crfj3w5e.js +22821 -0
- package/dist/chunk-ct5x6z7g.js +8066 -0
- package/dist/chunk-cv5h8n8c.js +101 -0
- package/dist/chunk-cwe8h159.js +1563 -0
- package/dist/chunk-cwm234zz.js +908 -0
- package/dist/chunk-d1jkfp45.js +1580 -0
- package/dist/chunk-d3hb1qdr.js +126 -0
- package/dist/chunk-d70d0zer.js +713 -0
- package/dist/chunk-d91tx29f.js +105 -0
- package/dist/chunk-d952pymh.js +20 -0
- package/dist/chunk-d9af3z8k.js +720 -0
- package/dist/chunk-da1n53c2.js +47 -0
- package/dist/chunk-df5c1x8m.js +732 -0
- package/dist/chunk-dkv64xys.js +71 -0
- package/dist/chunk-dm7166v6.js +251 -0
- package/dist/chunk-dr0y5a61.js +258 -0
- package/dist/chunk-drzb0rrd.js +306 -0
- package/dist/chunk-dw1v1q7w.js +37 -0
- package/dist/chunk-e224qet4.js +348 -0
- package/dist/chunk-e50ckbv0.js +1099 -0
- package/dist/chunk-e5gn6by6.js +1165 -0
- package/dist/chunk-e7m3f74m.js +37 -0
- package/dist/chunk-e8ghgwaj.js +295 -0
- package/dist/chunk-ed8k4ya2.js +41 -0
- package/dist/chunk-eh0vnqdz.js +76 -0
- package/dist/chunk-epwrzr35.js +4301 -0
- package/dist/chunk-er9h6sw5.js +5379 -0
- package/dist/chunk-eta5aekm.js +79 -0
- package/dist/chunk-ev5gxscq.js +63 -0
- package/dist/chunk-evp72hv5.js +212 -0
- package/dist/chunk-ex41stfq.js +127 -0
- package/dist/chunk-ezpa510h.js +62 -0
- package/dist/chunk-f0fm6dey.js +477 -0
- package/dist/chunk-f239sbhc.js +152 -0
- package/dist/chunk-f2bj47xf.js +109 -0
- package/dist/chunk-f4t24rq4.js +258 -0
- package/dist/chunk-f9ghvzy8.js +106 -0
- package/dist/chunk-f9rahzgr.js +99 -0
- package/dist/chunk-ff75qzty.js +521 -0
- package/dist/chunk-fj0ctt6q.js +264 -0
- package/dist/chunk-fj7wxdt3.js +221 -0
- package/dist/chunk-fnnpqv92.js +714 -0
- package/dist/chunk-fpd3zzx1.js +26696 -0
- package/dist/chunk-fqv2cn76.js +227 -0
- package/dist/chunk-fqw9j3bj.js +439 -0
- package/dist/chunk-frbcqhz0.js +4984 -0
- package/dist/chunk-fzkj59sg.js +174 -0
- package/dist/chunk-fzpzbpbw.js +248 -0
- package/dist/chunk-g52cpmd1.js +686 -0
- package/dist/chunk-g98m2qe6.js +31 -0
- package/dist/chunk-g9zgq4vy.js +715 -0
- package/dist/chunk-gbpt7tm8.js +687 -0
- package/dist/chunk-gg52jka8.js +373 -0
- package/dist/chunk-gjttphax.js +5793 -0
- package/dist/chunk-gs7vjaas.js +65 -0
- package/dist/chunk-gv8n85j2.js +8 -0
- package/dist/chunk-gxdwm6pt.js +650 -0
- package/dist/chunk-gy8wajg2.js +1014 -0
- package/dist/chunk-gz3647m0.js +200 -0
- package/dist/chunk-gzg07mqh.js +16 -0
- package/dist/chunk-h3zc60tq.js +44 -0
- package/dist/chunk-h4p1qt16.js +74 -0
- package/dist/chunk-h8219f18.js +282 -0
- package/dist/chunk-h9agq92v.js +331 -0
- package/dist/chunk-hhjtpwbj.js +16 -0
- package/dist/chunk-hpgpwzra.js +548 -0
- package/dist/chunk-hsnjvn19.js +80 -0
- package/dist/chunk-hsp9qrry.js +417 -0
- package/dist/chunk-hwz6aq2m.js +3939 -0
- package/dist/chunk-hzph0hbh.js +272 -0
- package/dist/chunk-hzqkss6v.js +153 -0
- package/dist/chunk-j0t31f39.js +1149 -0
- package/dist/chunk-j1qm6n1v.js +30 -0
- package/dist/chunk-j6e4b522.js +81 -0
- package/dist/chunk-j7qfbvxk.js +84 -0
- package/dist/chunk-j7qv3hvm.js +1635 -0
- package/dist/chunk-j8mxc6k6.js +213 -0
- package/dist/chunk-jevj87jn.js +224 -0
- package/dist/chunk-jk2ps88q.js +815 -0
- package/dist/chunk-jz7tbdyv.js +1599 -0
- package/dist/chunk-k36jpjvh.js +300 -0
- package/dist/chunk-k4nmrt3w.js +61 -0
- package/dist/chunk-kavn03r9.js +16606 -0
- package/dist/chunk-kkgav8dm.js +54 -0
- package/dist/chunk-kkq26g5c.js +33 -0
- package/dist/chunk-kp6nepx4.js +106 -0
- package/dist/chunk-ksefybc8.js +1551 -0
- package/dist/chunk-kv147680.js +3436 -0
- package/dist/chunk-kya46axt.js +833 -0
- package/dist/chunk-kyz233ny.js +227 -0
- package/dist/chunk-kzs75xmj.js +143 -0
- package/dist/chunk-m0vrez9w.js +266 -0
- package/dist/chunk-m1wadav3.js +118 -0
- package/dist/chunk-m4rfx3cj.js +221 -0
- package/dist/chunk-mc9zaggs.js +6374 -0
- package/dist/chunk-mcg5ttj4.js +105 -0
- package/dist/chunk-mf4r7918.js +63 -0
- package/dist/chunk-mh9khrt4.js +286 -0
- package/dist/chunk-mhc4szw7.js +22 -0
- package/dist/chunk-mkq0yzp0.js +38 -0
- package/dist/chunk-mr58jv4w.js +256 -0
- package/dist/chunk-mrrx3bmt.js +168 -0
- package/dist/chunk-mtyk3zz6.js +87 -0
- package/dist/chunk-mvgxntv6.js +62 -0
- package/dist/chunk-mwnjydcm.js +102 -0
- package/dist/chunk-mwxt0m3f.js +862 -0
- package/dist/chunk-mz1pxck8.js +998 -0
- package/dist/chunk-n3agghys.js +214 -0
- package/dist/chunk-n6q02ya9.js +442 -0
- package/dist/chunk-nt3y91vh.js +443 -0
- package/dist/chunk-nt837qt9.js +21 -0
- package/dist/chunk-ntgmegfs.js +39 -0
- package/dist/chunk-nwk16bbd.js +30 -0
- package/dist/chunk-p367ay90.js +672 -0
- package/dist/chunk-p7m3x9qk.js +48 -0
- package/dist/chunk-pagmjwp5.js +132 -0
- package/dist/chunk-pbd4r8ek.js +161 -0
- package/dist/chunk-pdkpssgv.js +2422 -0
- package/dist/chunk-pf6z1e35.js +32 -0
- package/dist/chunk-pfzejvpt.js +213 -0
- package/dist/chunk-pktxyhvk.js +400 -0
- package/dist/chunk-pp5xhveq.js +124 -0
- package/dist/chunk-ppnd7a8x.js +38 -0
- package/dist/chunk-pqh2w7dr.js +2202 -0
- package/dist/chunk-pr878d0z.js +240 -0
- package/dist/chunk-psf0y7hy.js +140 -0
- package/dist/chunk-pz2zz2f7.js +1586 -0
- package/dist/chunk-q0xkrxy8.js +890 -0
- package/dist/chunk-q5b0kpr3.js +14369 -0
- package/dist/chunk-q7z3t531.js +334 -0
- package/dist/chunk-qdjjtgwt.js +3183 -0
- package/dist/chunk-qezv7msv.js +26 -0
- package/dist/chunk-qmxn7f86.js +2643 -0
- package/dist/chunk-qqc3b97c.js +15 -0
- package/dist/chunk-qwrp63wh.js +145 -0
- package/dist/chunk-qyvqbst3.js +537 -0
- package/dist/chunk-qztvd4hd.js +465 -0
- package/dist/chunk-r0bvez4y.js +235 -0
- package/dist/chunk-r3z0896k.js +2831 -0
- package/dist/chunk-r4cxb6t7.js +32 -0
- package/dist/chunk-rb8k68m7.js +118 -0
- package/dist/chunk-rdprgm4t.js +42 -0
- package/dist/chunk-rex82hys.js +1710 -0
- package/dist/chunk-rjjakkw2.js +2560 -0
- package/dist/chunk-rka6gcv3.js +304 -0
- package/dist/chunk-rkz12ghm.js +144 -0
- package/dist/chunk-rnc7m1qs.js +436 -0
- package/dist/chunk-rx6z23h0.js +165 -0
- package/dist/chunk-s76582j1.js +28 -0
- package/dist/chunk-sez03qd9.js +7140 -0
- package/dist/chunk-sjvdn2ep.js +130 -0
- package/dist/chunk-smwhyy8p.js +115 -0
- package/dist/chunk-snw7nh0d.js +695 -0
- package/dist/chunk-sqx9c057.js +193 -0
- package/dist/chunk-sscms68s.js +16 -0
- package/dist/chunk-stknnmsb.js +272 -0
- package/dist/chunk-sywdxbs6.js +853 -0
- package/dist/chunk-syxy4pf2.js +782 -0
- package/dist/chunk-t0hpqsqn.js +6157 -0
- package/dist/chunk-tjm70vnw.js +123 -0
- package/dist/chunk-trzh0msn.js +154 -0
- package/dist/chunk-v1z17cpg.js +723 -0
- package/dist/chunk-vsfj449x.js +333 -0
- package/dist/chunk-vvkq20tt.js +287 -0
- package/dist/chunk-vxgdzx6p.js +338 -0
- package/dist/chunk-vxqhjyfz.js +15 -0
- package/dist/chunk-vzsstfmb.js +80 -0
- package/dist/chunk-w1jew5sn.js +69 -0
- package/dist/chunk-w3kwr76v.js +789 -0
- package/dist/chunk-w40netr9.js +59 -0
- package/dist/chunk-w99cpfdg.js +559 -0
- package/dist/chunk-wacn14d2.js +271 -0
- package/dist/chunk-wbhrqrq3.js +3033 -0
- package/dist/chunk-wfg9g0p7.js +17612 -0
- package/dist/chunk-wj3vjsbx.js +93 -0
- package/dist/chunk-wjhq855a.js +372 -0
- package/dist/chunk-wr06gqxh.js +114 -0
- package/dist/chunk-ws0z2y1g.js +195 -0
- package/dist/chunk-wt62wqcj.js +98 -0
- package/dist/chunk-wx4v7ddx.js +42 -0
- package/dist/chunk-wy0t3vb2.js +6101 -0
- package/dist/chunk-wysz7qk4.js +280 -0
- package/dist/chunk-x63fx1vd.js +192 -0
- package/dist/chunk-x8b7vft8.js +132 -0
- package/dist/chunk-x8jhkgxb.js +346 -0
- package/dist/chunk-x9z4q5k5.js +275 -0
- package/dist/chunk-xjd7e9jq.js +126 -0
- package/dist/chunk-xjr0n27e.js +42 -0
- package/dist/chunk-xpwyw7cd.js +24 -0
- package/dist/chunk-xw4ycnyz.js +37 -0
- package/dist/chunk-y66bqywr.js +17303 -0
- package/dist/chunk-ycarry7d.js +157 -0
- package/dist/chunk-ycr0hp6v.js +1486 -0
- package/dist/chunk-yqmaw6hf.js +496 -0
- package/dist/chunk-yrtm7d23.js +602 -0
- package/dist/chunk-yts879rw.js +48 -0
- package/dist/chunk-yvmhx96e.js +347 -0
- package/dist/chunk-ywq00rg4.js +246 -0
- package/dist/chunk-yygeg5mj.js +752 -0
- package/dist/chunk-z0exw850.js +726 -0
- package/dist/chunk-z2dp53wn.js +17 -0
- package/dist/chunk-z4rzc9nd.js +103 -0
- package/dist/chunk-zb0akt77.js +684 -0
- package/dist/chunk-zb2xrj5t.js +1789 -0
- package/dist/chunk-zbptn0ky.js +423 -0
- package/dist/chunk-zd45wbmf.js +1090 -0
- package/dist/chunk-zjkvspz9.js +2050 -0
- package/dist/chunk-zke1sp3x.js +1524 -0
- package/dist/chunk-zv2cvfsv.js +58 -0
- package/dist/chunk-zy4tmqst.js +644 -0
- package/dist/chunk-zygzzzvk.js +192 -0
- package/dist/cli-21v3v6ny.md +1 -0
- package/dist/cli.js +246 -0
- package/dist/server-n5f2q89z.md +1 -0
- package/package.json +161 -0
|
@@ -0,0 +1,1180 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
decodeJwtExpiry
|
|
4
|
+
} from "./chunk-f239sbhc.js";
|
|
5
|
+
import {
|
|
6
|
+
getSessionIngressAuthHeaders,
|
|
7
|
+
getSessionIngressAuthToken,
|
|
8
|
+
init_sessionActivity,
|
|
9
|
+
init_sessionIngressAuth,
|
|
10
|
+
registerSessionActivityCallback,
|
|
11
|
+
unregisterSessionActivityCallback
|
|
12
|
+
} from "./chunk-jevj87jn.js";
|
|
13
|
+
import {
|
|
14
|
+
createAxiosInstance,
|
|
15
|
+
init_proxy
|
|
16
|
+
} from "./chunk-z0exw850.js";
|
|
17
|
+
import {
|
|
18
|
+
errorMessage,
|
|
19
|
+
getErrnoCode,
|
|
20
|
+
init_debug,
|
|
21
|
+
init_errors,
|
|
22
|
+
init_slowOperations,
|
|
23
|
+
jsonParse,
|
|
24
|
+
jsonStringify,
|
|
25
|
+
logForDebugging
|
|
26
|
+
} from "./chunk-rex82hys.js";
|
|
27
|
+
import {
|
|
28
|
+
getClaudeCodeUserAgent,
|
|
29
|
+
sleep
|
|
30
|
+
} from "./chunk-y66bqywr.js";
|
|
31
|
+
import {
|
|
32
|
+
init_diagLogs,
|
|
33
|
+
logForDiagnosticsNoPII
|
|
34
|
+
} from "./chunk-65z8hg8j.js";
|
|
35
|
+
import {
|
|
36
|
+
axios_default,
|
|
37
|
+
init_axios
|
|
38
|
+
} from "./chunk-q5b0kpr3.js";
|
|
39
|
+
|
|
40
|
+
// src/cli/transports/ccrClient.ts
|
|
41
|
+
import { randomUUID } from "crypto";
|
|
42
|
+
init_debug();
|
|
43
|
+
init_diagLogs();
|
|
44
|
+
init_errors();
|
|
45
|
+
init_proxy();
|
|
46
|
+
init_sessionActivity();
|
|
47
|
+
init_sessionIngressAuth();
|
|
48
|
+
|
|
49
|
+
// src/cli/transports/SerialBatchEventUploader.ts
|
|
50
|
+
init_slowOperations();
|
|
51
|
+
|
|
52
|
+
class RetryableError extends Error {
|
|
53
|
+
retryAfterMs;
|
|
54
|
+
constructor(message, retryAfterMs) {
|
|
55
|
+
super(message);
|
|
56
|
+
this.retryAfterMs = retryAfterMs;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class SerialBatchEventUploader {
|
|
61
|
+
pending = [];
|
|
62
|
+
pendingAtClose = 0;
|
|
63
|
+
draining = false;
|
|
64
|
+
closed = false;
|
|
65
|
+
backpressureResolvers = [];
|
|
66
|
+
sleepResolve = null;
|
|
67
|
+
flushResolvers = [];
|
|
68
|
+
droppedBatches = 0;
|
|
69
|
+
config;
|
|
70
|
+
constructor(config) {
|
|
71
|
+
this.config = config;
|
|
72
|
+
}
|
|
73
|
+
get droppedBatchCount() {
|
|
74
|
+
return this.droppedBatches;
|
|
75
|
+
}
|
|
76
|
+
get pendingCount() {
|
|
77
|
+
return this.closed ? this.pendingAtClose : this.pending.length;
|
|
78
|
+
}
|
|
79
|
+
async enqueue(events) {
|
|
80
|
+
if (this.closed)
|
|
81
|
+
return;
|
|
82
|
+
const items = Array.isArray(events) ? events : [events];
|
|
83
|
+
if (items.length === 0)
|
|
84
|
+
return;
|
|
85
|
+
while (this.pending.length + items.length > this.config.maxQueueSize && !this.closed) {
|
|
86
|
+
await new Promise((resolve) => {
|
|
87
|
+
this.backpressureResolvers.push(resolve);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
if (this.closed)
|
|
91
|
+
return;
|
|
92
|
+
this.pending.push(...items);
|
|
93
|
+
this.drain();
|
|
94
|
+
}
|
|
95
|
+
flush() {
|
|
96
|
+
if (this.pending.length === 0 && !this.draining) {
|
|
97
|
+
return Promise.resolve();
|
|
98
|
+
}
|
|
99
|
+
this.drain();
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
this.flushResolvers.push(resolve);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
close() {
|
|
105
|
+
if (this.closed)
|
|
106
|
+
return;
|
|
107
|
+
this.closed = true;
|
|
108
|
+
this.pendingAtClose = this.pending.length;
|
|
109
|
+
this.pending = [];
|
|
110
|
+
this.sleepResolve?.();
|
|
111
|
+
this.sleepResolve = null;
|
|
112
|
+
for (const resolve of this.backpressureResolvers)
|
|
113
|
+
resolve();
|
|
114
|
+
this.backpressureResolvers = [];
|
|
115
|
+
for (const resolve of this.flushResolvers)
|
|
116
|
+
resolve();
|
|
117
|
+
this.flushResolvers = [];
|
|
118
|
+
}
|
|
119
|
+
async drain() {
|
|
120
|
+
if (this.draining || this.closed)
|
|
121
|
+
return;
|
|
122
|
+
this.draining = true;
|
|
123
|
+
let failures = 0;
|
|
124
|
+
try {
|
|
125
|
+
while (this.pending.length > 0 && !this.closed) {
|
|
126
|
+
const batch = this.takeBatch();
|
|
127
|
+
if (batch.length === 0)
|
|
128
|
+
continue;
|
|
129
|
+
try {
|
|
130
|
+
await this.config.send(batch);
|
|
131
|
+
failures = 0;
|
|
132
|
+
} catch (err) {
|
|
133
|
+
failures++;
|
|
134
|
+
if (this.config.maxConsecutiveFailures !== undefined && failures >= this.config.maxConsecutiveFailures) {
|
|
135
|
+
this.droppedBatches++;
|
|
136
|
+
this.config.onBatchDropped?.(batch.length, failures);
|
|
137
|
+
failures = 0;
|
|
138
|
+
this.releaseBackpressure();
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
this.pending = batch.concat(this.pending);
|
|
142
|
+
const retryAfterMs = err instanceof RetryableError ? err.retryAfterMs : undefined;
|
|
143
|
+
await this.sleep(this.retryDelay(failures, retryAfterMs));
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
this.releaseBackpressure();
|
|
147
|
+
}
|
|
148
|
+
} finally {
|
|
149
|
+
this.draining = false;
|
|
150
|
+
if (this.pending.length === 0) {
|
|
151
|
+
for (const resolve of this.flushResolvers)
|
|
152
|
+
resolve();
|
|
153
|
+
this.flushResolvers = [];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
takeBatch() {
|
|
158
|
+
const { maxBatchSize, maxBatchBytes } = this.config;
|
|
159
|
+
if (maxBatchBytes === undefined) {
|
|
160
|
+
return this.pending.splice(0, maxBatchSize);
|
|
161
|
+
}
|
|
162
|
+
let bytes = 0;
|
|
163
|
+
let count = 0;
|
|
164
|
+
while (count < this.pending.length && count < maxBatchSize) {
|
|
165
|
+
let itemBytes;
|
|
166
|
+
try {
|
|
167
|
+
itemBytes = Buffer.byteLength(jsonStringify(this.pending[count]));
|
|
168
|
+
} catch {
|
|
169
|
+
this.pending.splice(count, 1);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (count > 0 && bytes + itemBytes > maxBatchBytes)
|
|
173
|
+
break;
|
|
174
|
+
bytes += itemBytes;
|
|
175
|
+
count++;
|
|
176
|
+
}
|
|
177
|
+
return this.pending.splice(0, count);
|
|
178
|
+
}
|
|
179
|
+
retryDelay(failures, retryAfterMs) {
|
|
180
|
+
const jitter = Math.random() * this.config.jitterMs;
|
|
181
|
+
if (retryAfterMs !== undefined) {
|
|
182
|
+
const clamped = Math.max(this.config.baseDelayMs, Math.min(retryAfterMs, this.config.maxDelayMs));
|
|
183
|
+
return clamped + jitter;
|
|
184
|
+
}
|
|
185
|
+
const exponential = Math.min(this.config.baseDelayMs * 2 ** (failures - 1), this.config.maxDelayMs);
|
|
186
|
+
return exponential + jitter;
|
|
187
|
+
}
|
|
188
|
+
releaseBackpressure() {
|
|
189
|
+
const resolvers = this.backpressureResolvers;
|
|
190
|
+
this.backpressureResolvers = [];
|
|
191
|
+
for (const resolve of resolvers)
|
|
192
|
+
resolve();
|
|
193
|
+
}
|
|
194
|
+
sleep(ms) {
|
|
195
|
+
return new Promise((resolve) => {
|
|
196
|
+
this.sleepResolve = resolve;
|
|
197
|
+
setTimeout((self, resolve2) => {
|
|
198
|
+
self.sleepResolve = null;
|
|
199
|
+
resolve2();
|
|
200
|
+
}, ms, this, resolve);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/cli/transports/WorkerStateUploader.ts
|
|
206
|
+
class WorkerStateUploader {
|
|
207
|
+
inflight = null;
|
|
208
|
+
pending = null;
|
|
209
|
+
closed = false;
|
|
210
|
+
config;
|
|
211
|
+
constructor(config) {
|
|
212
|
+
this.config = config;
|
|
213
|
+
}
|
|
214
|
+
enqueue(patch) {
|
|
215
|
+
if (this.closed)
|
|
216
|
+
return;
|
|
217
|
+
this.pending = this.pending ? coalescePatches(this.pending, patch) : patch;
|
|
218
|
+
this.drain();
|
|
219
|
+
}
|
|
220
|
+
close() {
|
|
221
|
+
this.closed = true;
|
|
222
|
+
this.pending = null;
|
|
223
|
+
}
|
|
224
|
+
async drain() {
|
|
225
|
+
if (this.inflight || this.closed)
|
|
226
|
+
return;
|
|
227
|
+
if (!this.pending)
|
|
228
|
+
return;
|
|
229
|
+
const payload = this.pending;
|
|
230
|
+
this.pending = null;
|
|
231
|
+
this.inflight = this.sendWithRetry(payload).then(() => {
|
|
232
|
+
this.inflight = null;
|
|
233
|
+
if (this.pending && !this.closed) {
|
|
234
|
+
this.drain();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
async sendWithRetry(payload) {
|
|
239
|
+
let current = payload;
|
|
240
|
+
let failures = 0;
|
|
241
|
+
while (!this.closed) {
|
|
242
|
+
const ok = await this.config.send(current);
|
|
243
|
+
if (ok)
|
|
244
|
+
return;
|
|
245
|
+
failures++;
|
|
246
|
+
await sleep(this.retryDelay(failures));
|
|
247
|
+
if (this.pending && !this.closed) {
|
|
248
|
+
current = coalescePatches(current, this.pending);
|
|
249
|
+
this.pending = null;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
retryDelay(failures) {
|
|
254
|
+
const exponential = Math.min(this.config.baseDelayMs * 2 ** (failures - 1), this.config.maxDelayMs);
|
|
255
|
+
const jitter = Math.random() * this.config.jitterMs;
|
|
256
|
+
return exponential + jitter;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function coalescePatches(base, overlay) {
|
|
260
|
+
const merged = { ...base };
|
|
261
|
+
for (const [key, value] of Object.entries(overlay)) {
|
|
262
|
+
if ((key === "external_metadata" || key === "internal_metadata") && merged[key] && typeof merged[key] === "object" && typeof value === "object" && value !== null) {
|
|
263
|
+
merged[key] = {
|
|
264
|
+
...merged[key],
|
|
265
|
+
...value
|
|
266
|
+
};
|
|
267
|
+
} else {
|
|
268
|
+
merged[key] = value;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return merged;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/cli/transports/ccrClient.ts
|
|
275
|
+
var DEFAULT_HEARTBEAT_INTERVAL_MS = 20000;
|
|
276
|
+
var STREAM_EVENT_FLUSH_INTERVAL_MS = 100;
|
|
277
|
+
function alwaysValidStatus() {
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
class CCRInitError extends Error {
|
|
282
|
+
reason;
|
|
283
|
+
constructor(reason) {
|
|
284
|
+
super(`CCRClient init failed: ${reason}`);
|
|
285
|
+
this.reason = reason;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
var MAX_CONSECUTIVE_AUTH_FAILURES = 10;
|
|
289
|
+
function createStreamAccumulator() {
|
|
290
|
+
return { byMessage: new Map, scopeToMessage: new Map };
|
|
291
|
+
}
|
|
292
|
+
function scopeKey(m) {
|
|
293
|
+
return `${m.session_id}:${m.parent_tool_use_id ?? ""}`;
|
|
294
|
+
}
|
|
295
|
+
function accumulateStreamEvents(buffer, state) {
|
|
296
|
+
const out = [];
|
|
297
|
+
const touched = new Map;
|
|
298
|
+
for (const msg of buffer) {
|
|
299
|
+
switch (msg.event.type) {
|
|
300
|
+
case "message_start": {
|
|
301
|
+
const id = msg.event.message.id;
|
|
302
|
+
const prevId = state.scopeToMessage.get(scopeKey(msg));
|
|
303
|
+
if (prevId)
|
|
304
|
+
state.byMessage.delete(prevId);
|
|
305
|
+
state.scopeToMessage.set(scopeKey(msg), id);
|
|
306
|
+
state.byMessage.set(id, []);
|
|
307
|
+
out.push(msg);
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
case "content_block_delta": {
|
|
311
|
+
if (msg.event.delta.type !== "text_delta") {
|
|
312
|
+
out.push(msg);
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
const messageId = state.scopeToMessage.get(scopeKey(msg));
|
|
316
|
+
const blocks = messageId ? state.byMessage.get(messageId) : undefined;
|
|
317
|
+
if (!blocks) {
|
|
318
|
+
out.push(msg);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
const chunks = blocks[msg.event.index] ??= [];
|
|
322
|
+
chunks.push(msg.event.delta.text);
|
|
323
|
+
const existing = touched.get(chunks);
|
|
324
|
+
if (existing) {
|
|
325
|
+
existing.event.delta.text = chunks.join("");
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
const snapshot = {
|
|
329
|
+
type: "stream_event",
|
|
330
|
+
uuid: msg.uuid,
|
|
331
|
+
session_id: msg.session_id,
|
|
332
|
+
parent_tool_use_id: msg.parent_tool_use_id,
|
|
333
|
+
event: {
|
|
334
|
+
type: "content_block_delta",
|
|
335
|
+
index: msg.event.index,
|
|
336
|
+
delta: { type: "text_delta", text: chunks.join("") }
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
touched.set(chunks, snapshot);
|
|
340
|
+
out.push(snapshot);
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
default:
|
|
344
|
+
out.push(msg);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
function clearStreamAccumulatorForMessage(state, assistant) {
|
|
350
|
+
state.byMessage.delete(assistant.message.id);
|
|
351
|
+
const scope = scopeKey(assistant);
|
|
352
|
+
if (state.scopeToMessage.get(scope) === assistant.message.id) {
|
|
353
|
+
state.scopeToMessage.delete(scope);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
class CCRClient {
|
|
358
|
+
workerEpoch = 0;
|
|
359
|
+
heartbeatIntervalMs;
|
|
360
|
+
heartbeatJitterFraction;
|
|
361
|
+
heartbeatTimer = null;
|
|
362
|
+
heartbeatInFlight = false;
|
|
363
|
+
closed = false;
|
|
364
|
+
consecutiveAuthFailures = 0;
|
|
365
|
+
currentState = null;
|
|
366
|
+
sessionBaseUrl;
|
|
367
|
+
sessionId;
|
|
368
|
+
http = createAxiosInstance({ keepAlive: true });
|
|
369
|
+
streamEventBuffer = [];
|
|
370
|
+
streamEventTimer = null;
|
|
371
|
+
streamTextAccumulator = createStreamAccumulator();
|
|
372
|
+
workerState;
|
|
373
|
+
eventUploader;
|
|
374
|
+
internalEventUploader;
|
|
375
|
+
deliveryUploader;
|
|
376
|
+
onEpochMismatch;
|
|
377
|
+
getAuthHeaders;
|
|
378
|
+
constructor(transport, sessionUrl, opts) {
|
|
379
|
+
this.onEpochMismatch = opts?.onEpochMismatch ?? (() => {
|
|
380
|
+
process.exit(1);
|
|
381
|
+
});
|
|
382
|
+
this.heartbeatIntervalMs = opts?.heartbeatIntervalMs ?? DEFAULT_HEARTBEAT_INTERVAL_MS;
|
|
383
|
+
this.heartbeatJitterFraction = opts?.heartbeatJitterFraction ?? 0;
|
|
384
|
+
this.getAuthHeaders = opts?.getAuthHeaders ?? getSessionIngressAuthHeaders;
|
|
385
|
+
if (sessionUrl.protocol !== "http:" && sessionUrl.protocol !== "https:") {
|
|
386
|
+
throw new Error(`CCRClient: Expected http(s) URL, got ${sessionUrl.protocol}`);
|
|
387
|
+
}
|
|
388
|
+
const pathname = sessionUrl.pathname.replace(/\/$/, "");
|
|
389
|
+
this.sessionBaseUrl = `${sessionUrl.protocol}//${sessionUrl.host}${pathname}`;
|
|
390
|
+
this.sessionId = pathname.split("/").pop() || "";
|
|
391
|
+
this.workerState = new WorkerStateUploader({
|
|
392
|
+
send: (body) => this.request("put", "/worker", { worker_epoch: this.workerEpoch, ...body }, "PUT worker").then((r) => r.ok),
|
|
393
|
+
baseDelayMs: 500,
|
|
394
|
+
maxDelayMs: 30000,
|
|
395
|
+
jitterMs: 500
|
|
396
|
+
});
|
|
397
|
+
this.eventUploader = new SerialBatchEventUploader({
|
|
398
|
+
maxBatchSize: 100,
|
|
399
|
+
maxBatchBytes: 10 * 1024 * 1024,
|
|
400
|
+
maxQueueSize: 1e5,
|
|
401
|
+
send: async (batch) => {
|
|
402
|
+
const result = await this.request("post", "/worker/events", { worker_epoch: this.workerEpoch, events: batch }, "client events");
|
|
403
|
+
if (!result.ok) {
|
|
404
|
+
throw new RetryableError("client event POST failed", result.retryAfterMs);
|
|
405
|
+
}
|
|
406
|
+
},
|
|
407
|
+
baseDelayMs: 500,
|
|
408
|
+
maxDelayMs: 30000,
|
|
409
|
+
jitterMs: 500
|
|
410
|
+
});
|
|
411
|
+
this.internalEventUploader = new SerialBatchEventUploader({
|
|
412
|
+
maxBatchSize: 100,
|
|
413
|
+
maxBatchBytes: 10 * 1024 * 1024,
|
|
414
|
+
maxQueueSize: 200,
|
|
415
|
+
send: async (batch) => {
|
|
416
|
+
const result = await this.request("post", "/worker/internal-events", { worker_epoch: this.workerEpoch, events: batch }, "internal events");
|
|
417
|
+
if (!result.ok) {
|
|
418
|
+
throw new RetryableError("internal event POST failed", result.retryAfterMs);
|
|
419
|
+
}
|
|
420
|
+
},
|
|
421
|
+
baseDelayMs: 500,
|
|
422
|
+
maxDelayMs: 30000,
|
|
423
|
+
jitterMs: 500
|
|
424
|
+
});
|
|
425
|
+
this.deliveryUploader = new SerialBatchEventUploader({
|
|
426
|
+
maxBatchSize: 64,
|
|
427
|
+
maxQueueSize: 64,
|
|
428
|
+
send: async (batch) => {
|
|
429
|
+
const result = await this.request("post", "/worker/events/delivery", {
|
|
430
|
+
worker_epoch: this.workerEpoch,
|
|
431
|
+
updates: batch.map((d) => ({
|
|
432
|
+
event_id: d.eventId,
|
|
433
|
+
status: d.status
|
|
434
|
+
}))
|
|
435
|
+
}, "delivery batch");
|
|
436
|
+
if (!result.ok) {
|
|
437
|
+
throw new RetryableError("delivery POST failed", result.retryAfterMs);
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
baseDelayMs: 500,
|
|
441
|
+
maxDelayMs: 30000,
|
|
442
|
+
jitterMs: 500
|
|
443
|
+
});
|
|
444
|
+
transport.setOnEvent((event) => {
|
|
445
|
+
this.reportDelivery(event.event_id, "received");
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
async initialize(epoch) {
|
|
449
|
+
const startMs = Date.now();
|
|
450
|
+
if (Object.keys(this.getAuthHeaders()).length === 0) {
|
|
451
|
+
throw new CCRInitError("no_auth_headers");
|
|
452
|
+
}
|
|
453
|
+
if (epoch === undefined) {
|
|
454
|
+
const rawEpoch = process.env.CLAUDE_CODE_WORKER_EPOCH;
|
|
455
|
+
epoch = rawEpoch ? parseInt(rawEpoch, 10) : NaN;
|
|
456
|
+
}
|
|
457
|
+
if (isNaN(epoch)) {
|
|
458
|
+
throw new CCRInitError("missing_epoch");
|
|
459
|
+
}
|
|
460
|
+
this.workerEpoch = epoch;
|
|
461
|
+
const restoredPromise = this.getWorkerState();
|
|
462
|
+
const result = await this.request("put", "/worker", {
|
|
463
|
+
worker_status: "idle",
|
|
464
|
+
worker_epoch: this.workerEpoch,
|
|
465
|
+
external_metadata: {
|
|
466
|
+
pending_action: null,
|
|
467
|
+
task_summary: null
|
|
468
|
+
}
|
|
469
|
+
}, "PUT worker (init)");
|
|
470
|
+
if (!result.ok) {
|
|
471
|
+
throw new CCRInitError("worker_register_failed");
|
|
472
|
+
}
|
|
473
|
+
this.currentState = "idle";
|
|
474
|
+
this.startHeartbeat();
|
|
475
|
+
registerSessionActivityCallback(() => {
|
|
476
|
+
this.writeEvent({ type: "keep_alive" });
|
|
477
|
+
});
|
|
478
|
+
logForDebugging(`CCRClient: initialized, epoch=${this.workerEpoch}`);
|
|
479
|
+
logForDiagnosticsNoPII("info", "cli_worker_lifecycle_initialized", {
|
|
480
|
+
epoch: this.workerEpoch,
|
|
481
|
+
duration_ms: Date.now() - startMs
|
|
482
|
+
});
|
|
483
|
+
const { metadata, durationMs } = await restoredPromise;
|
|
484
|
+
if (!this.closed) {
|
|
485
|
+
logForDiagnosticsNoPII("info", "cli_worker_state_restored", {
|
|
486
|
+
duration_ms: durationMs,
|
|
487
|
+
had_state: metadata !== null
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
return metadata;
|
|
491
|
+
}
|
|
492
|
+
async getWorkerState() {
|
|
493
|
+
const startMs = Date.now();
|
|
494
|
+
const authHeaders = this.getAuthHeaders();
|
|
495
|
+
if (Object.keys(authHeaders).length === 0) {
|
|
496
|
+
return { metadata: null, durationMs: 0 };
|
|
497
|
+
}
|
|
498
|
+
const data = await this.getWithRetry(`${this.sessionBaseUrl}/worker`, authHeaders, "worker_state");
|
|
499
|
+
return {
|
|
500
|
+
metadata: data?.worker?.external_metadata ?? null,
|
|
501
|
+
durationMs: Date.now() - startMs
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
async request(method, path, body, label, { timeout = 1e4 } = {}) {
|
|
505
|
+
const authHeaders = this.getAuthHeaders();
|
|
506
|
+
if (Object.keys(authHeaders).length === 0)
|
|
507
|
+
return { ok: false };
|
|
508
|
+
try {
|
|
509
|
+
const response = await this.http[method](`${this.sessionBaseUrl}${path}`, body, {
|
|
510
|
+
headers: {
|
|
511
|
+
...authHeaders,
|
|
512
|
+
"Content-Type": "application/json",
|
|
513
|
+
"anthropic-version": "2023-06-01",
|
|
514
|
+
"User-Agent": getClaudeCodeUserAgent()
|
|
515
|
+
},
|
|
516
|
+
validateStatus: alwaysValidStatus,
|
|
517
|
+
timeout
|
|
518
|
+
});
|
|
519
|
+
if (response.status >= 200 && response.status < 300) {
|
|
520
|
+
this.consecutiveAuthFailures = 0;
|
|
521
|
+
return { ok: true };
|
|
522
|
+
}
|
|
523
|
+
if (response.status === 409) {
|
|
524
|
+
this.handleEpochMismatch();
|
|
525
|
+
}
|
|
526
|
+
if (response.status === 401 || response.status === 403) {
|
|
527
|
+
const tok = getSessionIngressAuthToken();
|
|
528
|
+
const exp = tok ? decodeJwtExpiry(tok) : null;
|
|
529
|
+
if (exp !== null && exp * 1000 < Date.now()) {
|
|
530
|
+
logForDebugging(`CCRClient: session_token expired (exp=${new Date(exp * 1000).toISOString()}) \u2014 no refresh was delivered, exiting`, { level: "error" });
|
|
531
|
+
logForDiagnosticsNoPII("error", "cli_worker_token_expired_no_refresh");
|
|
532
|
+
this.onEpochMismatch();
|
|
533
|
+
}
|
|
534
|
+
this.consecutiveAuthFailures++;
|
|
535
|
+
if (this.consecutiveAuthFailures >= MAX_CONSECUTIVE_AUTH_FAILURES) {
|
|
536
|
+
logForDebugging(`CCRClient: ${this.consecutiveAuthFailures} consecutive auth failures with a valid-looking token \u2014 server-side auth unrecoverable, exiting`, { level: "error" });
|
|
537
|
+
logForDiagnosticsNoPII("error", "cli_worker_auth_failures_exhausted");
|
|
538
|
+
this.onEpochMismatch();
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
logForDebugging(`CCRClient: ${label} returned ${response.status}`, {
|
|
542
|
+
level: "warn"
|
|
543
|
+
});
|
|
544
|
+
logForDiagnosticsNoPII("warn", "cli_worker_request_failed", {
|
|
545
|
+
method,
|
|
546
|
+
path,
|
|
547
|
+
status: response.status
|
|
548
|
+
});
|
|
549
|
+
if (response.status === 429) {
|
|
550
|
+
const raw = response.headers?.["retry-after"];
|
|
551
|
+
const seconds = typeof raw === "string" ? parseInt(raw, 10) : NaN;
|
|
552
|
+
if (!isNaN(seconds) && seconds >= 0) {
|
|
553
|
+
return { ok: false, retryAfterMs: seconds * 1000 };
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return { ok: false };
|
|
557
|
+
} catch (error) {
|
|
558
|
+
logForDebugging(`CCRClient: ${label} failed: ${errorMessage(error)}`, {
|
|
559
|
+
level: "warn"
|
|
560
|
+
});
|
|
561
|
+
logForDiagnosticsNoPII("warn", "cli_worker_request_error", {
|
|
562
|
+
method,
|
|
563
|
+
path,
|
|
564
|
+
error_code: getErrnoCode(error)
|
|
565
|
+
});
|
|
566
|
+
return { ok: false };
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
reportState(state, details) {
|
|
570
|
+
if (state === this.currentState && !details)
|
|
571
|
+
return;
|
|
572
|
+
this.currentState = state;
|
|
573
|
+
this.workerState.enqueue({
|
|
574
|
+
worker_status: state,
|
|
575
|
+
requires_action_details: details ? {
|
|
576
|
+
tool_name: details.tool_name,
|
|
577
|
+
action_description: details.action_description,
|
|
578
|
+
request_id: details.request_id
|
|
579
|
+
} : null
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
reportMetadata(metadata) {
|
|
583
|
+
this.workerState.enqueue({ external_metadata: metadata });
|
|
584
|
+
}
|
|
585
|
+
handleEpochMismatch() {
|
|
586
|
+
logForDebugging("CCRClient: Epoch mismatch (409), shutting down", {
|
|
587
|
+
level: "error"
|
|
588
|
+
});
|
|
589
|
+
logForDiagnosticsNoPII("error", "cli_worker_epoch_mismatch");
|
|
590
|
+
this.onEpochMismatch();
|
|
591
|
+
}
|
|
592
|
+
startHeartbeat() {
|
|
593
|
+
this.stopHeartbeat();
|
|
594
|
+
const schedule = () => {
|
|
595
|
+
const jitter = this.heartbeatIntervalMs * this.heartbeatJitterFraction * (2 * Math.random() - 1);
|
|
596
|
+
this.heartbeatTimer = setTimeout(tick, this.heartbeatIntervalMs + jitter);
|
|
597
|
+
};
|
|
598
|
+
const tick = () => {
|
|
599
|
+
this.sendHeartbeat();
|
|
600
|
+
if (this.heartbeatTimer === null)
|
|
601
|
+
return;
|
|
602
|
+
schedule();
|
|
603
|
+
};
|
|
604
|
+
schedule();
|
|
605
|
+
}
|
|
606
|
+
stopHeartbeat() {
|
|
607
|
+
if (this.heartbeatTimer) {
|
|
608
|
+
clearTimeout(this.heartbeatTimer);
|
|
609
|
+
this.heartbeatTimer = null;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
async sendHeartbeat() {
|
|
613
|
+
if (this.heartbeatInFlight)
|
|
614
|
+
return;
|
|
615
|
+
this.heartbeatInFlight = true;
|
|
616
|
+
try {
|
|
617
|
+
const result = await this.request("post", "/worker/heartbeat", { session_id: this.sessionId, worker_epoch: this.workerEpoch }, "Heartbeat", { timeout: 5000 });
|
|
618
|
+
if (result.ok) {
|
|
619
|
+
logForDebugging("CCRClient: Heartbeat sent");
|
|
620
|
+
}
|
|
621
|
+
} finally {
|
|
622
|
+
this.heartbeatInFlight = false;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
async writeEvent(message) {
|
|
626
|
+
if (message.type === "stream_event") {
|
|
627
|
+
this.streamEventBuffer.push(message);
|
|
628
|
+
if (!this.streamEventTimer) {
|
|
629
|
+
this.streamEventTimer = setTimeout(() => void this.flushStreamEventBuffer(), STREAM_EVENT_FLUSH_INTERVAL_MS);
|
|
630
|
+
}
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
await this.flushStreamEventBuffer();
|
|
634
|
+
if (message.type === "assistant") {
|
|
635
|
+
clearStreamAccumulatorForMessage(this.streamTextAccumulator, message);
|
|
636
|
+
}
|
|
637
|
+
await this.eventUploader.enqueue(this.toClientEvent(message));
|
|
638
|
+
}
|
|
639
|
+
toClientEvent(message) {
|
|
640
|
+
const msg = message;
|
|
641
|
+
return {
|
|
642
|
+
payload: {
|
|
643
|
+
...msg,
|
|
644
|
+
uuid: typeof msg.uuid === "string" ? msg.uuid : randomUUID()
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
async flushStreamEventBuffer() {
|
|
649
|
+
if (this.streamEventTimer) {
|
|
650
|
+
clearTimeout(this.streamEventTimer);
|
|
651
|
+
this.streamEventTimer = null;
|
|
652
|
+
}
|
|
653
|
+
if (this.streamEventBuffer.length === 0)
|
|
654
|
+
return;
|
|
655
|
+
const buffered = this.streamEventBuffer;
|
|
656
|
+
this.streamEventBuffer = [];
|
|
657
|
+
const payloads = accumulateStreamEvents(buffered, this.streamTextAccumulator);
|
|
658
|
+
await this.eventUploader.enqueue(payloads.map((payload) => ({ payload, ephemeral: true })));
|
|
659
|
+
}
|
|
660
|
+
async writeInternalEvent(eventType, payload, {
|
|
661
|
+
isCompaction = false,
|
|
662
|
+
agentId
|
|
663
|
+
} = {}) {
|
|
664
|
+
const event = {
|
|
665
|
+
payload: {
|
|
666
|
+
type: eventType,
|
|
667
|
+
...payload,
|
|
668
|
+
uuid: typeof payload.uuid === "string" ? payload.uuid : randomUUID()
|
|
669
|
+
},
|
|
670
|
+
...isCompaction && { is_compaction: true },
|
|
671
|
+
...agentId && { agent_id: agentId }
|
|
672
|
+
};
|
|
673
|
+
await this.internalEventUploader.enqueue(event);
|
|
674
|
+
}
|
|
675
|
+
flushInternalEvents() {
|
|
676
|
+
return this.internalEventUploader.flush();
|
|
677
|
+
}
|
|
678
|
+
async flush() {
|
|
679
|
+
await this.flushStreamEventBuffer();
|
|
680
|
+
return this.eventUploader.flush();
|
|
681
|
+
}
|
|
682
|
+
async readInternalEvents() {
|
|
683
|
+
return this.paginatedGet("/worker/internal-events", {}, "internal_events");
|
|
684
|
+
}
|
|
685
|
+
async readSubagentInternalEvents() {
|
|
686
|
+
return this.paginatedGet("/worker/internal-events", { subagents: "true" }, "subagent_events");
|
|
687
|
+
}
|
|
688
|
+
async paginatedGet(path, params, context) {
|
|
689
|
+
const authHeaders = this.getAuthHeaders();
|
|
690
|
+
if (Object.keys(authHeaders).length === 0)
|
|
691
|
+
return null;
|
|
692
|
+
const allEvents = [];
|
|
693
|
+
let cursor;
|
|
694
|
+
do {
|
|
695
|
+
const url = new URL(`${this.sessionBaseUrl}${path}`);
|
|
696
|
+
for (const [k, v] of Object.entries(params)) {
|
|
697
|
+
url.searchParams.set(k, v);
|
|
698
|
+
}
|
|
699
|
+
if (cursor) {
|
|
700
|
+
url.searchParams.set("cursor", cursor);
|
|
701
|
+
}
|
|
702
|
+
const page = await this.getWithRetry(url.toString(), authHeaders, context);
|
|
703
|
+
if (!page)
|
|
704
|
+
return null;
|
|
705
|
+
allEvents.push(...page.data ?? []);
|
|
706
|
+
cursor = page.next_cursor;
|
|
707
|
+
} while (cursor);
|
|
708
|
+
logForDebugging(`CCRClient: Read ${allEvents.length} internal events from ${path}${params.subagents ? " (subagents)" : ""}`);
|
|
709
|
+
return allEvents;
|
|
710
|
+
}
|
|
711
|
+
async getWithRetry(url, authHeaders, context) {
|
|
712
|
+
for (let attempt = 1;attempt <= 10; attempt++) {
|
|
713
|
+
let response;
|
|
714
|
+
try {
|
|
715
|
+
response = await this.http.get(url, {
|
|
716
|
+
headers: {
|
|
717
|
+
...authHeaders,
|
|
718
|
+
"anthropic-version": "2023-06-01",
|
|
719
|
+
"User-Agent": getClaudeCodeUserAgent()
|
|
720
|
+
},
|
|
721
|
+
validateStatus: alwaysValidStatus,
|
|
722
|
+
timeout: 30000
|
|
723
|
+
});
|
|
724
|
+
} catch (error) {
|
|
725
|
+
logForDebugging(`CCRClient: GET ${url} failed (attempt ${attempt}/10): ${errorMessage(error)}`, { level: "warn" });
|
|
726
|
+
if (attempt < 10) {
|
|
727
|
+
const delay = Math.min(500 * 2 ** (attempt - 1), 30000) + Math.random() * 500;
|
|
728
|
+
await sleep(delay);
|
|
729
|
+
}
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
if (response.status >= 200 && response.status < 300) {
|
|
733
|
+
return response.data;
|
|
734
|
+
}
|
|
735
|
+
if (response.status === 409) {
|
|
736
|
+
this.handleEpochMismatch();
|
|
737
|
+
}
|
|
738
|
+
logForDebugging(`CCRClient: GET ${url} returned ${response.status} (attempt ${attempt}/10)`, { level: "warn" });
|
|
739
|
+
if (attempt < 10) {
|
|
740
|
+
const delay = Math.min(500 * 2 ** (attempt - 1), 30000) + Math.random() * 500;
|
|
741
|
+
await sleep(delay);
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
logForDebugging("CCRClient: GET retries exhausted", { level: "error" });
|
|
745
|
+
logForDiagnosticsNoPII("error", "cli_worker_get_retries_exhausted", {
|
|
746
|
+
context
|
|
747
|
+
});
|
|
748
|
+
return null;
|
|
749
|
+
}
|
|
750
|
+
reportDelivery(eventId, status) {
|
|
751
|
+
this.deliveryUploader.enqueue({ eventId, status });
|
|
752
|
+
}
|
|
753
|
+
getWorkerEpoch() {
|
|
754
|
+
return this.workerEpoch;
|
|
755
|
+
}
|
|
756
|
+
get internalEventsPending() {
|
|
757
|
+
return this.internalEventUploader.pendingCount;
|
|
758
|
+
}
|
|
759
|
+
close() {
|
|
760
|
+
this.closed = true;
|
|
761
|
+
this.stopHeartbeat();
|
|
762
|
+
unregisterSessionActivityCallback();
|
|
763
|
+
if (this.streamEventTimer) {
|
|
764
|
+
clearTimeout(this.streamEventTimer);
|
|
765
|
+
this.streamEventTimer = null;
|
|
766
|
+
}
|
|
767
|
+
this.streamEventBuffer = [];
|
|
768
|
+
this.streamTextAccumulator.byMessage.clear();
|
|
769
|
+
this.streamTextAccumulator.scopeToMessage.clear();
|
|
770
|
+
this.workerState.close();
|
|
771
|
+
this.eventUploader.close();
|
|
772
|
+
this.internalEventUploader.close();
|
|
773
|
+
this.deliveryUploader.close();
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// src/cli/transports/SSETransport.ts
|
|
778
|
+
init_axios();
|
|
779
|
+
init_debug();
|
|
780
|
+
init_diagLogs();
|
|
781
|
+
init_errors();
|
|
782
|
+
init_sessionIngressAuth();
|
|
783
|
+
init_slowOperations();
|
|
784
|
+
var RECONNECT_BASE_DELAY_MS = 1000;
|
|
785
|
+
var RECONNECT_MAX_DELAY_MS = 30000;
|
|
786
|
+
var RECONNECT_GIVE_UP_MS = 600000;
|
|
787
|
+
var LIVENESS_TIMEOUT_MS = 45000;
|
|
788
|
+
var PERMANENT_HTTP_CODES = new Set([401, 403, 404]);
|
|
789
|
+
var POST_MAX_RETRIES = 10;
|
|
790
|
+
var POST_BASE_DELAY_MS = 500;
|
|
791
|
+
var POST_MAX_DELAY_MS = 8000;
|
|
792
|
+
var STREAM_DECODE_OPTS = { stream: true };
|
|
793
|
+
function alwaysValidStatus2() {
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
796
|
+
function parseSSEFrames(buffer) {
|
|
797
|
+
const frames = [];
|
|
798
|
+
let pos = 0;
|
|
799
|
+
let idx;
|
|
800
|
+
while ((idx = buffer.indexOf(`
|
|
801
|
+
|
|
802
|
+
`, pos)) !== -1) {
|
|
803
|
+
const rawFrame = buffer.slice(pos, idx);
|
|
804
|
+
pos = idx + 2;
|
|
805
|
+
if (!rawFrame.trim())
|
|
806
|
+
continue;
|
|
807
|
+
const frame = {};
|
|
808
|
+
let isComment = false;
|
|
809
|
+
for (const line of rawFrame.split(`
|
|
810
|
+
`)) {
|
|
811
|
+
if (line.startsWith(":")) {
|
|
812
|
+
isComment = true;
|
|
813
|
+
continue;
|
|
814
|
+
}
|
|
815
|
+
const colonIdx = line.indexOf(":");
|
|
816
|
+
if (colonIdx === -1)
|
|
817
|
+
continue;
|
|
818
|
+
const field = line.slice(0, colonIdx);
|
|
819
|
+
const value = line[colonIdx + 1] === " " ? line.slice(colonIdx + 2) : line.slice(colonIdx + 1);
|
|
820
|
+
switch (field) {
|
|
821
|
+
case "event":
|
|
822
|
+
frame.event = value;
|
|
823
|
+
break;
|
|
824
|
+
case "id":
|
|
825
|
+
frame.id = value;
|
|
826
|
+
break;
|
|
827
|
+
case "data":
|
|
828
|
+
frame.data = frame.data ? frame.data + `
|
|
829
|
+
` + value : value;
|
|
830
|
+
break;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
if (frame.data || isComment) {
|
|
834
|
+
frames.push(frame);
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return { frames, remaining: buffer.slice(pos) };
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
class SSETransport {
|
|
841
|
+
url;
|
|
842
|
+
state = "idle";
|
|
843
|
+
onData;
|
|
844
|
+
onCloseCallback;
|
|
845
|
+
onEventCallback;
|
|
846
|
+
headers;
|
|
847
|
+
sessionId;
|
|
848
|
+
refreshHeaders;
|
|
849
|
+
getAuthHeaders;
|
|
850
|
+
abortController = null;
|
|
851
|
+
lastSequenceNum = 0;
|
|
852
|
+
seenSequenceNums = new Set;
|
|
853
|
+
reconnectAttempts = 0;
|
|
854
|
+
reconnectStartTime = null;
|
|
855
|
+
reconnectTimer = null;
|
|
856
|
+
livenessTimer = null;
|
|
857
|
+
postUrl;
|
|
858
|
+
constructor(url, headers = {}, sessionId, refreshHeaders, initialSequenceNum, getAuthHeaders) {
|
|
859
|
+
this.url = url;
|
|
860
|
+
this.headers = headers;
|
|
861
|
+
this.sessionId = sessionId;
|
|
862
|
+
this.refreshHeaders = refreshHeaders;
|
|
863
|
+
this.getAuthHeaders = getAuthHeaders ?? getSessionIngressAuthHeaders;
|
|
864
|
+
this.postUrl = convertSSEUrlToPostUrl(url);
|
|
865
|
+
if (initialSequenceNum !== undefined && initialSequenceNum > 0) {
|
|
866
|
+
this.lastSequenceNum = initialSequenceNum;
|
|
867
|
+
}
|
|
868
|
+
logForDebugging(`SSETransport: SSE URL = ${url.href}`);
|
|
869
|
+
logForDebugging(`SSETransport: POST URL = ${this.postUrl}`);
|
|
870
|
+
logForDiagnosticsNoPII("info", "cli_sse_transport_initialized");
|
|
871
|
+
}
|
|
872
|
+
getLastSequenceNum() {
|
|
873
|
+
return this.lastSequenceNum;
|
|
874
|
+
}
|
|
875
|
+
async connect() {
|
|
876
|
+
if (this.state !== "idle" && this.state !== "reconnecting") {
|
|
877
|
+
logForDebugging(`SSETransport: Cannot connect, current state is ${this.state}`, { level: "error" });
|
|
878
|
+
logForDiagnosticsNoPII("error", "cli_sse_connect_failed");
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
this.state = "reconnecting";
|
|
882
|
+
const connectStartTime = Date.now();
|
|
883
|
+
const sseUrl = new URL(this.url.href);
|
|
884
|
+
if (this.lastSequenceNum > 0) {
|
|
885
|
+
sseUrl.searchParams.set("from_sequence_num", String(this.lastSequenceNum));
|
|
886
|
+
}
|
|
887
|
+
const authHeaders = this.getAuthHeaders();
|
|
888
|
+
const headers = {
|
|
889
|
+
...this.headers,
|
|
890
|
+
...authHeaders,
|
|
891
|
+
Accept: "text/event-stream",
|
|
892
|
+
"anthropic-version": "2023-06-01",
|
|
893
|
+
"User-Agent": getClaudeCodeUserAgent()
|
|
894
|
+
};
|
|
895
|
+
if (authHeaders["Cookie"]) {
|
|
896
|
+
delete headers["Authorization"];
|
|
897
|
+
}
|
|
898
|
+
if (this.lastSequenceNum > 0) {
|
|
899
|
+
headers["Last-Event-ID"] = String(this.lastSequenceNum);
|
|
900
|
+
}
|
|
901
|
+
logForDebugging(`SSETransport: Opening ${sseUrl.href}`);
|
|
902
|
+
logForDiagnosticsNoPII("info", "cli_sse_connect_opening");
|
|
903
|
+
this.abortController = new AbortController;
|
|
904
|
+
try {
|
|
905
|
+
const response = await fetch(sseUrl.href, {
|
|
906
|
+
headers,
|
|
907
|
+
signal: this.abortController.signal
|
|
908
|
+
});
|
|
909
|
+
if (!response.ok) {
|
|
910
|
+
const isPermanent = PERMANENT_HTTP_CODES.has(response.status);
|
|
911
|
+
logForDebugging(`SSETransport: HTTP ${response.status}${isPermanent ? " (permanent)" : ""}`, { level: "error" });
|
|
912
|
+
logForDiagnosticsNoPII("error", "cli_sse_connect_http_error", {
|
|
913
|
+
status: response.status
|
|
914
|
+
});
|
|
915
|
+
if (isPermanent) {
|
|
916
|
+
this.state = "closed";
|
|
917
|
+
this.onCloseCallback?.(response.status);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
this.handleConnectionError();
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
if (!response.body) {
|
|
924
|
+
logForDebugging("SSETransport: No response body");
|
|
925
|
+
this.handleConnectionError();
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
const connectDuration = Date.now() - connectStartTime;
|
|
929
|
+
logForDebugging("SSETransport: Connected");
|
|
930
|
+
logForDiagnosticsNoPII("info", "cli_sse_connect_connected", {
|
|
931
|
+
duration_ms: connectDuration
|
|
932
|
+
});
|
|
933
|
+
this.state = "connected";
|
|
934
|
+
this.reconnectAttempts = 0;
|
|
935
|
+
this.reconnectStartTime = null;
|
|
936
|
+
this.resetLivenessTimer();
|
|
937
|
+
await this.readStream(response.body);
|
|
938
|
+
} catch (error) {
|
|
939
|
+
if (this.abortController?.signal.aborted) {
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
logForDebugging(`SSETransport: Connection error: ${errorMessage(error)}`, { level: "error" });
|
|
943
|
+
logForDiagnosticsNoPII("error", "cli_sse_connect_error");
|
|
944
|
+
this.handleConnectionError();
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
async readStream(body) {
|
|
948
|
+
const reader = body.getReader();
|
|
949
|
+
const decoder = new TextDecoder;
|
|
950
|
+
let buffer = "";
|
|
951
|
+
try {
|
|
952
|
+
while (true) {
|
|
953
|
+
const { done, value } = await reader.read();
|
|
954
|
+
if (done)
|
|
955
|
+
break;
|
|
956
|
+
buffer += decoder.decode(value, STREAM_DECODE_OPTS);
|
|
957
|
+
const { frames, remaining } = parseSSEFrames(buffer);
|
|
958
|
+
buffer = remaining;
|
|
959
|
+
for (const frame of frames) {
|
|
960
|
+
this.resetLivenessTimer();
|
|
961
|
+
if (frame.id) {
|
|
962
|
+
const seqNum = parseInt(frame.id, 10);
|
|
963
|
+
if (!isNaN(seqNum)) {
|
|
964
|
+
if (this.seenSequenceNums.has(seqNum)) {
|
|
965
|
+
logForDebugging(`SSETransport: DUPLICATE frame seq=${seqNum} (lastSequenceNum=${this.lastSequenceNum}, seenCount=${this.seenSequenceNums.size})`, { level: "warn" });
|
|
966
|
+
logForDiagnosticsNoPII("warn", "cli_sse_duplicate_sequence");
|
|
967
|
+
} else {
|
|
968
|
+
this.seenSequenceNums.add(seqNum);
|
|
969
|
+
if (this.seenSequenceNums.size > 1000) {
|
|
970
|
+
const threshold = this.lastSequenceNum - 200;
|
|
971
|
+
for (const s of this.seenSequenceNums) {
|
|
972
|
+
if (s < threshold) {
|
|
973
|
+
this.seenSequenceNums.delete(s);
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
if (seqNum > this.lastSequenceNum) {
|
|
979
|
+
this.lastSequenceNum = seqNum;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
if (frame.event && frame.data) {
|
|
984
|
+
this.handleSSEFrame(frame.event, frame.data);
|
|
985
|
+
} else if (frame.data) {
|
|
986
|
+
logForDebugging("SSETransport: Frame has data: but no event: field \u2014 dropped", { level: "warn" });
|
|
987
|
+
logForDiagnosticsNoPII("warn", "cli_sse_frame_missing_event_field");
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
} catch (error) {
|
|
992
|
+
if (this.abortController?.signal.aborted)
|
|
993
|
+
return;
|
|
994
|
+
logForDebugging(`SSETransport: Stream read error: ${errorMessage(error)}`, { level: "error" });
|
|
995
|
+
logForDiagnosticsNoPII("error", "cli_sse_stream_read_error");
|
|
996
|
+
} finally {
|
|
997
|
+
reader.releaseLock();
|
|
998
|
+
}
|
|
999
|
+
if (this.state !== "closing" && this.state !== "closed") {
|
|
1000
|
+
logForDebugging("SSETransport: Stream ended, reconnecting");
|
|
1001
|
+
this.handleConnectionError();
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
handleSSEFrame(eventType, data) {
|
|
1005
|
+
if (eventType !== "client_event") {
|
|
1006
|
+
logForDebugging(`SSETransport: Unexpected SSE event type '${eventType}' on worker stream`, { level: "warn" });
|
|
1007
|
+
logForDiagnosticsNoPII("warn", "cli_sse_unexpected_event_type", {
|
|
1008
|
+
event_type: eventType
|
|
1009
|
+
});
|
|
1010
|
+
return;
|
|
1011
|
+
}
|
|
1012
|
+
let ev;
|
|
1013
|
+
try {
|
|
1014
|
+
ev = jsonParse(data);
|
|
1015
|
+
} catch (error) {
|
|
1016
|
+
logForDebugging(`SSETransport: Failed to parse client_event data: ${errorMessage(error)}`, { level: "error" });
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const payload = ev.payload;
|
|
1020
|
+
if (payload && typeof payload === "object" && "type" in payload) {
|
|
1021
|
+
const sessionLabel = this.sessionId ? ` session=${this.sessionId}` : "";
|
|
1022
|
+
logForDebugging(`SSETransport: Event seq=${ev.sequence_num} event_id=${ev.event_id} event_type=${ev.event_type} payload_type=${String(payload.type)}${sessionLabel}`);
|
|
1023
|
+
logForDiagnosticsNoPII("info", "cli_sse_message_received");
|
|
1024
|
+
this.onData?.(jsonStringify(payload) + `
|
|
1025
|
+
`);
|
|
1026
|
+
} else {
|
|
1027
|
+
logForDebugging(`SSETransport: Ignoring client_event with no type in payload: event_id=${ev.event_id}`);
|
|
1028
|
+
}
|
|
1029
|
+
this.onEventCallback?.(ev);
|
|
1030
|
+
}
|
|
1031
|
+
handleConnectionError() {
|
|
1032
|
+
this.clearLivenessTimer();
|
|
1033
|
+
if (this.state === "closing" || this.state === "closed")
|
|
1034
|
+
return;
|
|
1035
|
+
this.abortController?.abort();
|
|
1036
|
+
this.abortController = null;
|
|
1037
|
+
const now = Date.now();
|
|
1038
|
+
if (!this.reconnectStartTime) {
|
|
1039
|
+
this.reconnectStartTime = now;
|
|
1040
|
+
}
|
|
1041
|
+
const elapsed = now - this.reconnectStartTime;
|
|
1042
|
+
if (elapsed < RECONNECT_GIVE_UP_MS) {
|
|
1043
|
+
if (this.reconnectTimer) {
|
|
1044
|
+
clearTimeout(this.reconnectTimer);
|
|
1045
|
+
this.reconnectTimer = null;
|
|
1046
|
+
}
|
|
1047
|
+
if (this.refreshHeaders) {
|
|
1048
|
+
const freshHeaders = this.refreshHeaders();
|
|
1049
|
+
Object.assign(this.headers, freshHeaders);
|
|
1050
|
+
logForDebugging("SSETransport: Refreshed headers for reconnect");
|
|
1051
|
+
}
|
|
1052
|
+
this.state = "reconnecting";
|
|
1053
|
+
this.reconnectAttempts++;
|
|
1054
|
+
const baseDelay = Math.min(RECONNECT_BASE_DELAY_MS * Math.pow(2, this.reconnectAttempts - 1), RECONNECT_MAX_DELAY_MS);
|
|
1055
|
+
const delay = Math.max(0, baseDelay + baseDelay * 0.25 * (2 * Math.random() - 1));
|
|
1056
|
+
logForDebugging(`SSETransport: Reconnecting in ${Math.round(delay)}ms (attempt ${this.reconnectAttempts}, ${Math.round(elapsed / 1000)}s elapsed)`);
|
|
1057
|
+
logForDiagnosticsNoPII("error", "cli_sse_reconnect_attempt", {
|
|
1058
|
+
reconnectAttempts: this.reconnectAttempts
|
|
1059
|
+
});
|
|
1060
|
+
this.reconnectTimer = setTimeout(() => {
|
|
1061
|
+
this.reconnectTimer = null;
|
|
1062
|
+
this.connect();
|
|
1063
|
+
}, delay);
|
|
1064
|
+
} else {
|
|
1065
|
+
logForDebugging(`SSETransport: Reconnection time budget exhausted after ${Math.round(elapsed / 1000)}s`, { level: "error" });
|
|
1066
|
+
logForDiagnosticsNoPII("error", "cli_sse_reconnect_exhausted", {
|
|
1067
|
+
reconnectAttempts: this.reconnectAttempts,
|
|
1068
|
+
elapsedMs: elapsed
|
|
1069
|
+
});
|
|
1070
|
+
this.state = "closed";
|
|
1071
|
+
this.onCloseCallback?.();
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
onLivenessTimeout = () => {
|
|
1075
|
+
this.livenessTimer = null;
|
|
1076
|
+
logForDebugging("SSETransport: Liveness timeout, reconnecting", {
|
|
1077
|
+
level: "error"
|
|
1078
|
+
});
|
|
1079
|
+
logForDiagnosticsNoPII("error", "cli_sse_liveness_timeout");
|
|
1080
|
+
this.abortController?.abort();
|
|
1081
|
+
this.handleConnectionError();
|
|
1082
|
+
};
|
|
1083
|
+
resetLivenessTimer() {
|
|
1084
|
+
this.clearLivenessTimer();
|
|
1085
|
+
this.livenessTimer = setTimeout(this.onLivenessTimeout, LIVENESS_TIMEOUT_MS);
|
|
1086
|
+
}
|
|
1087
|
+
clearLivenessTimer() {
|
|
1088
|
+
if (this.livenessTimer) {
|
|
1089
|
+
clearTimeout(this.livenessTimer);
|
|
1090
|
+
this.livenessTimer = null;
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
async write(message) {
|
|
1094
|
+
const authHeaders = this.getAuthHeaders();
|
|
1095
|
+
if (Object.keys(authHeaders).length === 0) {
|
|
1096
|
+
logForDebugging("SSETransport: No session token available for POST");
|
|
1097
|
+
logForDiagnosticsNoPII("warn", "cli_sse_post_no_token");
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
const headers = {
|
|
1101
|
+
...authHeaders,
|
|
1102
|
+
"Content-Type": "application/json",
|
|
1103
|
+
"anthropic-version": "2023-06-01",
|
|
1104
|
+
"User-Agent": getClaudeCodeUserAgent()
|
|
1105
|
+
};
|
|
1106
|
+
logForDebugging(`SSETransport: POST body keys=${Object.keys(message).join(",")}`);
|
|
1107
|
+
for (let attempt = 1;attempt <= POST_MAX_RETRIES; attempt++) {
|
|
1108
|
+
try {
|
|
1109
|
+
const response = await axios_default.post(this.postUrl, message, {
|
|
1110
|
+
headers,
|
|
1111
|
+
validateStatus: alwaysValidStatus2
|
|
1112
|
+
});
|
|
1113
|
+
if (response.status === 200 || response.status === 201) {
|
|
1114
|
+
logForDebugging(`SSETransport: POST success type=${message.type}`);
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
logForDebugging(`SSETransport: POST ${response.status} body=${jsonStringify(response.data).slice(0, 200)}`);
|
|
1118
|
+
if (response.status >= 400 && response.status < 500 && response.status !== 429) {
|
|
1119
|
+
logForDebugging(`SSETransport: POST returned ${response.status} (client error), not retrying`);
|
|
1120
|
+
logForDiagnosticsNoPII("warn", "cli_sse_post_client_error", {
|
|
1121
|
+
status: response.status
|
|
1122
|
+
});
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
logForDebugging(`SSETransport: POST returned ${response.status}, attempt ${attempt}/${POST_MAX_RETRIES}`);
|
|
1126
|
+
logForDiagnosticsNoPII("warn", "cli_sse_post_retryable_error", {
|
|
1127
|
+
status: response.status,
|
|
1128
|
+
attempt
|
|
1129
|
+
});
|
|
1130
|
+
} catch (error) {
|
|
1131
|
+
const axiosError = error;
|
|
1132
|
+
logForDebugging(`SSETransport: POST error: ${axiosError.message}, attempt ${attempt}/${POST_MAX_RETRIES}`);
|
|
1133
|
+
logForDiagnosticsNoPII("warn", "cli_sse_post_network_error", {
|
|
1134
|
+
attempt
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
if (attempt === POST_MAX_RETRIES) {
|
|
1138
|
+
logForDebugging(`SSETransport: POST failed after ${POST_MAX_RETRIES} attempts, continuing`);
|
|
1139
|
+
logForDiagnosticsNoPII("warn", "cli_sse_post_retries_exhausted");
|
|
1140
|
+
return;
|
|
1141
|
+
}
|
|
1142
|
+
const delayMs = Math.min(POST_BASE_DELAY_MS * Math.pow(2, attempt - 1), POST_MAX_DELAY_MS);
|
|
1143
|
+
await sleep(delayMs);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
isConnectedStatus() {
|
|
1147
|
+
return this.state === "connected";
|
|
1148
|
+
}
|
|
1149
|
+
isClosedStatus() {
|
|
1150
|
+
return this.state === "closed";
|
|
1151
|
+
}
|
|
1152
|
+
setOnData(callback) {
|
|
1153
|
+
this.onData = callback;
|
|
1154
|
+
}
|
|
1155
|
+
setOnClose(callback) {
|
|
1156
|
+
this.onCloseCallback = callback;
|
|
1157
|
+
}
|
|
1158
|
+
setOnEvent(callback) {
|
|
1159
|
+
this.onEventCallback = callback;
|
|
1160
|
+
}
|
|
1161
|
+
close() {
|
|
1162
|
+
if (this.reconnectTimer) {
|
|
1163
|
+
clearTimeout(this.reconnectTimer);
|
|
1164
|
+
this.reconnectTimer = null;
|
|
1165
|
+
}
|
|
1166
|
+
this.clearLivenessTimer();
|
|
1167
|
+
this.state = "closing";
|
|
1168
|
+
this.abortController?.abort();
|
|
1169
|
+
this.abortController = null;
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
function convertSSEUrlToPostUrl(sseUrl) {
|
|
1173
|
+
let pathname = sseUrl.pathname;
|
|
1174
|
+
if (pathname.endsWith("/stream")) {
|
|
1175
|
+
pathname = pathname.slice(0, -"/stream".length);
|
|
1176
|
+
}
|
|
1177
|
+
return `${sseUrl.protocol}//${sseUrl.host}${pathname}`;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
export { SerialBatchEventUploader, CCRInitError, CCRClient, SSETransport };
|