nolo-cli 0.264.0-alpha.2 → 0.266.0-alpha.1
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/{chunk-LXP53WGF.js → chunk-05wkntba.js} +44 -40
- package/{chunk-XFYFMTOM.js → chunk-08ts2q1n.js} +9 -11
- package/{chromeCommands-MVEEIY4R.js → chunk-0a3m1n6h.js} +182 -238
- package/chunk-0gfvc7g8.js +13 -0
- package/{chunk-CF7PE763.js → chunk-0h0sncy4.js} +3 -4
- package/{chunk-3UH7SS4W.js → chunk-0nsxwrge.js} +232 -241
- package/chunk-0ra8cne4.js +11 -0
- package/{chunk-MRACVEPZ.js → chunk-0sgwk7am.js} +1 -3
- package/{chunk-PEXGRTTQ.js → chunk-0tsfn0dk.js} +44 -45
- package/{chunk-WOAZVUSN.js → chunk-0y8cbwzy.js} +26 -30
- package/chunk-12gz55kk.js +6 -0
- package/{chunk-GLKOYSRU.js → chunk-186gshgw.js} +3 -6
- package/chunk-19w4ss38.js +44 -0
- package/{chunk-RF576RYE.js → chunk-1cakzp6r.js} +23 -23
- package/chunk-1fbfwbzp.js +27 -0
- package/{chunk-4IVMDQBA.js → chunk-1fv9k8zt.js} +103 -68
- package/{chunk-Q5FJJ6XI.js → chunk-1js4cdzp.js} +11 -19
- package/{chunk-YECCIUYU.js → chunk-1nhx0er5.js} +63 -63
- package/chunk-1p152b7g.js +12 -0
- package/{chunk-HXEO5DNV.js → chunk-1ynbq3yr.js} +322 -353
- package/{chunk-6UBGXX2W.js → chunk-1zrg4h5n.js} +3 -4
- package/{chunk-Y5GCAMGP.js → chunk-2g6zsfq8.js} +1 -3
- package/chunk-2hekb0eh.js +17 -0
- package/chunk-2pj6qzq5.js +12 -0
- package/chunk-2tvd4jyp.js +226 -0
- package/chunk-2ygg7azv.js +846 -0
- package/chunk-33405wtv.js +50 -0
- package/chunk-34axejtd.js +30 -0
- package/{tableCommands-FCPDULSU.js → chunk-3700dvt3.js} +224 -276
- package/{pdf-KE3QI2V7.js → chunk-3j08t1nm.js} +12505 -10133
- package/chunk-3j9gq18v.js +111 -0
- package/{chunk-LYPJYBE5.js → chunk-3qfpcdkm.js} +486 -505
- package/{settingThunks-PNC7ZISV.js → chunk-3r2x7h64.js} +189 -272
- package/{agentGrantCommands-2VLLXGIU.js → chunk-3tbj5fxt.js} +61 -56
- package/chunk-40whhjj5.js +168 -0
- package/chunk-4301919y.js +7 -0
- package/chunk-43adz39e.js +77 -0
- package/{chunk-5ZUL4CUO.js → chunk-49v3zc4g.js} +1 -3
- package/chunk-4dd7mxkb.js +72 -0
- package/{chunk-BKP325XS.js → chunk-4hapfzpq.js} +66 -62
- package/chunk-4mm7dz0m.js +95 -0
- package/chunk-4pqxempz.js +440 -0
- package/chunk-4x9zf5pv.js +10 -0
- package/{chunk-CA7LGAHV.js → chunk-511q8qzv.js} +66 -57
- package/{chunk-U7OULUMF.js → chunk-53v9g0np.js} +49 -64
- package/chunk-572mg6q5.js +19 -0
- package/{chunk-QAW6BEWQ.js → chunk-57eyhmwr.js} +17173 -16165
- package/chunk-5hsr0zjm.js +156 -0
- package/chunk-5k883yyd.js +20 -0
- package/chunk-5m0wsee4.js +639 -0
- package/{memberThunks-NEZRAW5K.js → chunk-5r1sq2gt.js} +299 -373
- package/chunk-648rq4np.js +255 -0
- package/{chunk-IYRZKHA6.js → chunk-666yjw5c.js} +63 -86
- package/{chunk-4O22PGNQ.js → chunk-6cdcbmtp.js} +19 -19
- package/chunk-6dbyqcf7.js +59 -0
- package/{agentProviderCommands-2W3VXDLV.js → chunk-6e8mgp1x.js} +82 -105
- package/chunk-6n0pw7sx.js +44 -0
- package/chunk-6q1b7381.js +361 -0
- package/chunk-6sj756ry.js +23 -0
- package/{chunk-4UNN45DS.js → chunk-6t7a7zck.js} +14785 -14454
- package/chunk-6yarwtxh.js +776 -0
- package/chunk-708h542p.js +18 -0
- package/chunk-76f076t8.js +266 -0
- package/chunk-782rrrwv.js +523 -0
- package/{workloadLifecycle-P2SJVV3S.js → chunk-7gk330wp.js} +77 -84
- package/{chunk-EZKIRVLJ.js → chunk-7ks60vxy.js} +146 -128
- package/{chunk-FI4DPLR7.js → chunk-7mrqbxe8.js} +342 -228
- package/chunk-7sztayrd.js +169 -0
- package/chunk-7vgt2mxz.js +15 -0
- package/{localFirstLog-OIETGK66.js → chunk-7z8y4k6y.js} +0 -2
- package/{chunk-KDDXC2IH.js → chunk-82htft2m.js} +49 -11
- package/{updateDialogTitleAction-XOSLNRT3.js → chunk-88vrtxxg.js} +65 -137
- package/{chunk-LS5KAVFQ.js → chunk-8dtjbp79.js} +293 -229
- package/chunk-8fajbkaz.js +50 -0
- package/chunk-8m1ebxpw.js +65 -0
- package/{accountResolver-JYG5L7YP.js → chunk-8mgfeg0w.js} +37 -30
- package/{chunk-LPXBBC32.js → chunk-8myksy35.js} +156 -160
- package/{chunk-BMQ2MJUK.js → chunk-8nrj7dc7.js} +75 -66
- package/{commandRegistry-ENNFT77C.js → chunk-8t8x64ec.js} +228 -286
- package/chunk-8yj4pq79.js +458 -0
- package/{chunk-5MKPNTJP.js → chunk-8z6pepcx.js} +43 -48
- package/{chunk-UYQILGDR.js → chunk-90egc9vb.js} +122 -102
- package/{chunk-EODBQPRM.js → chunk-91q5dc5r.js} +10 -14
- package/{chunk-U6VC4MU4.js → chunk-96jtrqk1.js} +2 -4
- package/{chunk-H57T64RA.js → chunk-996j61av.js} +28 -31
- package/{chunk-Q6FKWH66.js → chunk-9cxgb9jy.js} +137 -243
- package/{chunk-2ZBPWX7N.js → chunk-9q1gbmet.js} +62 -104
- package/chunk-9qtjh73q.js +344 -0
- package/{chunk-SR57FDTX.js → chunk-9raka469.js} +72 -155
- package/{chunk-V6K5U4YE.js → chunk-9vh4v046.js} +32 -41
- package/{chunk-AGIM7IMJ.js → chunk-9yr1fy7j.js} +14 -49
- package/{chunk-FGFJUFPS.js → chunk-a16x0wt6.js} +11 -18
- package/chunk-a5b2rvzs.js +131 -0
- package/{chunk-7DPNBQSV.js → chunk-a7bz6nc3.js} +3 -4
- package/chunk-ac85azv9.js +29 -0
- package/{chunk-VXL2FZTP.js → chunk-ahhbkad4.js} +16 -20
- package/{chunk-37DGW762.js → chunk-anhksj2j.js} +16 -29
- package/{cliAuthorityBrokerServer-27C5YBYP.js → chunk-atvmx76t.js} +180 -186
- package/{chunk-E6A5NHDH.js → chunk-av2mzeqz.js} +16 -17
- package/chunk-avbwrb0z.js +614 -0
- package/chunk-avr6rqe5.js +544 -0
- package/{workContextQueryProjection-CACY5VFE.js → chunk-axx60d5a.js} +7 -7
- package/{chunk-EWQ3HGNZ.js → chunk-b00vzjkb.js} +30 -23
- package/chunk-b0m6ezxr.js +132 -0
- package/chunk-bbd5mgdt.js +47 -0
- package/chunk-bcecft0r.js +28 -0
- package/chunk-beyr4pyp.js +33 -0
- package/{chunk-BR3GT6MR.js → chunk-bge0cagn.js} +7 -18
- package/{offlineMarxistsAgentCommand-MFURGWHL.js → chunk-bh53zg3t.js} +33 -43
- package/chunk-bmx1cw89.js +37 -0
- package/chunk-br2195h4.js +22 -0
- package/chunk-bsqkwveq.js +56 -0
- package/{chunk-QAU6KM2U.js → chunk-c09s2z1m.js} +139 -98
- package/chunk-c235kypv.js +24636 -0
- package/{chunk-2JMYAH5O.js → chunk-c30j6pyh.js} +213 -256
- package/chunk-c3v6r6sq.js +29 -0
- package/{chunk-PDSXYHNI.js → chunk-c5z7qspt.js} +1177 -1196
- package/{chunk-GWB6AGME.js → chunk-c6jvz0mp.js} +34 -50
- package/{chunk-PRJ473FK.js → chunk-cd6h9qqs.js} +14 -12
- package/chunk-cdt0amyy.js +37 -0
- package/{chunk-2AJLG6PY.js → chunk-cfh6tfc3.js} +73 -86
- package/{chunk-WIVVNJUS.js → chunk-ch4ck1yz.js} +38 -68
- package/chunk-chtvrtp5.js +55 -0
- package/{chunk-EYLGAEB4.js → chunk-ckvz7abv.js} +170 -165
- package/{chunk-YZMNK53S.js → chunk-csf18m5w.js} +32 -25
- package/{chunk-C36L6ZID.js → chunk-cwnvf8dt.js} +2 -10
- package/chunk-cy7thb6n.js +19877 -0
- package/{dialogPortabilityCommands-YQJNB2KL.js → chunk-d61q9nah.js} +439 -487
- package/chunk-d6xdy0gp.js +142 -0
- package/{chunk-EBH4RFJ6.js → chunk-dh69vqbd.js} +6390 -6496
- package/{chunk-KJ4J2XQB.js → chunk-dj4ggbc6.js} +17 -19
- package/{chunk-LISC7BD4.js → chunk-djrrhbk3.js} +29 -37
- package/chunk-drqzzzvr.js +822 -0
- package/chunk-e1n7edjk.js +33 -0
- package/chunk-e3atr4hk.js +59 -0
- package/chunk-e9bn2aav.js +13 -0
- package/chunk-ed396rfq.js +87 -0
- package/chunk-eha8ggh6.js +10 -0
- package/{chunk-MALPQFGT.js → chunk-ek122zzt.js} +893 -948
- package/chunk-epnmvek4.js +22 -0
- package/{chunk-S25VJYXV.js → chunk-eqzvk3xm.js} +69 -80
- package/chunk-ex34n71b.js +2874 -0
- package/{chunk-56OAS5RZ.js → chunk-f3ad6q4h.js} +7 -11
- package/{chunk-3PVYUPXK.js → chunk-f8jepva1.js} +6 -7
- package/{chunk-GH34JXNV.js → chunk-f9yvy9t9.js} +114 -92
- package/chunk-fbdz4f0j.js +90 -0
- package/{chunk-J44TXB4J.js → chunk-fcsc2day.js} +216 -164
- package/chunk-fevdhnbh.js +19 -0
- package/chunk-fkrgqq8w.js +3300 -0
- package/{chunk-TSBJVMNW.js → chunk-fs3bvh2p.js} +42 -57
- package/chunk-fvwzyhah.js +134 -0
- package/chunk-g0mpxdp9.js +101 -0
- package/{chunk-LTQ6ECCF.js → chunk-g35f20d1.js} +450 -414
- package/{chunk-3L2WKC3W.js → chunk-g5fhtjqg.js} +13 -16
- package/{chunk-DBEOW57U.js → chunk-gcq01td7.js} +270 -227
- package/{chunk-ADMRXWXJ.js → chunk-ge97b11r.js} +121 -42
- package/chunk-ghjtgbkw.js +180 -0
- package/chunk-gkra36c1.js +16 -0
- package/chunk-gmz86t60.js +1000 -0
- package/{localDialogRead-GWGWSL5K.js → chunk-gszpesze.js} +5 -12
- package/chunk-gvsfec9m.js +16536 -0
- package/{chunk-QSJ3HFEF.js → chunk-h1hzmqph.js} +1 -3
- package/chunk-h30dg154.js +17 -0
- package/chunk-h3hkb7cw.js +241 -0
- package/chunk-h40py996.js +28 -0
- package/{chunk-47T3QP4V.js → chunk-h60gnewh.js} +112 -116
- package/{agentPullCommand-XEGOUYQT.js → chunk-hkwsv9xf.js} +33 -45
- package/chunk-hnyg0gwj.js +28 -0
- package/{chunk-KSV6T4AH.js → chunk-hqqqph5w.js} +12 -4
- package/{chunk-YQJEXXDK.js → chunk-hrngs1wy.js} +157 -246
- package/{chunk-LRLLWD4P.js → chunk-htf69s94.js} +14 -12
- package/chunk-j0yxesh3.js +83 -0
- package/{chunk-VF4K2QE6.js → chunk-j22t9g05.js} +3 -6
- package/{chunk-3R4AYHJR.js → chunk-j4a8m2zn.js} +5 -34
- package/chunk-j9p4jnwg.js +27 -0
- package/{chunk-R72QWIFO.js → chunk-jdycmfsm.js} +9 -9
- package/chunk-jdyvrne0.js +52 -0
- package/{spacePruneCommand-YKIEUIDQ.js → chunk-je0b66qf.js} +44 -62
- package/{chunk-HNMXYYJR.js → chunk-jec44fkj.js} +3 -4
- package/{chunk-526GLZJE.js → chunk-jf1ex00r.js} +107 -73
- package/chunk-jgqdec9b.js +31 -0
- package/{chunk-56GBUMO5.js → chunk-jkw9n4h5.js} +16 -25
- package/{chunk-7422ZLGL.js → chunk-jmnzdwjt.js} +867 -965
- package/chunk-jqq57s0a.js +58 -0
- package/{chunk-OKUSDVRS.js → chunk-jtkn5aj6.js} +27 -38
- package/chunk-jwvzn9r0.js +316 -0
- package/chunk-k1re0fc7.js +107 -0
- package/chunk-k2ep3rb2.js +103 -0
- package/{chunk-JEXVIMYZ.js → chunk-k6ycb24w.js} +5 -8
- package/{chunk-RI7SXDJW.js → chunk-kawvydbk.js} +383 -195
- package/chunk-kf2ghpzh.js +10 -0
- package/{chunk-S6FAHQDH.js → chunk-kgah9e46.js} +28 -59
- package/{chunk-T3MWVUBE.js → chunk-kjjepkvc.js} +71 -82
- package/chunk-kn4dgg4j.js +4113 -0
- package/{chunk-2LFYMWM3.js → chunk-kn5p91n8.js} +43 -53
- package/{chunk-MVOZ22RZ.js → chunk-kntqrbwc.js} +8 -12
- package/{chunk-I6YIA2TY.js → chunk-kq3vr7mx.js} +25 -20
- package/chunk-kz1x1d8n.js +15 -0
- package/{mcpLocalAuthorizeCommand-IKOFKY5U.js → chunk-m22e7tt2.js} +53 -71
- package/{chunk-ZNFJWEN4.js → chunk-m32rwr4d.js} +11 -8
- package/chunk-m39e9m18.js +93 -0
- package/{chunk-4D4QD7FT.js → chunk-m5518371.js} +64 -56
- package/{chunk-LHTXSC2E.js → chunk-mb5hnfyp.js} +9 -11
- package/chunk-mexet11v.js +11 -0
- package/chunk-mg2hgfgh.js +185 -0
- package/chunk-mmdhm4kx.js +10317 -0
- package/chunk-mz002yz8.js +2821 -0
- package/chunk-mzadqnyq.js +56 -0
- package/{chunk-VIDPVFP3.js → chunk-n3r1ebdm.js} +5 -6
- package/chunk-n438qea7.js +33 -0
- package/chunk-n5ffnhnb.js +93 -0
- package/chunk-nbvd87fy.js +9 -0
- package/{chunk-E36V7MPY.js → chunk-ndej7jc9.js} +53 -43
- package/{chunk-B767MA64.js → chunk-nen6rwe4.js} +62 -22
- package/chunk-nf06tr3v.js +16 -0
- package/chunk-ng8z355q.js +16 -0
- package/{chunk-4MEKE5D3.js → chunk-nhtxfv8k.js} +78 -63
- package/{runAgentClientLoop-2SFASHDS.js → chunk-nj62jscj.js} +132 -214
- package/{chunk-GBWGPTA7.js → chunk-nkryrkyp.js} +16 -19
- package/chunk-nmesxem8.js +197 -0
- package/{chunk-YR4J7RFS.js → chunk-nmq0qwbn.js} +117 -164
- package/chunk-nph8brvw.js +7 -0
- package/chunk-nr8fees9.js +10 -0
- package/{chunk-DFI7MVCQ.js → chunk-nvewvf7h.js} +1441 -1427
- package/{chunk-G7Y4GXTY.js → chunk-nyamhphs.js} +70 -79
- package/chunk-nz1c8tcg.js +111 -0
- package/{chunk-GPLUFN2E.js → chunk-nzc5yffh.js} +176 -217
- package/{chunk-G3SUCT4W.js → chunk-p1vg4bx8.js} +2 -4
- package/chunk-p3k3abe8.js +202 -0
- package/chunk-p4zy30q5.js +8 -0
- package/{chunk-ZJACPM5O.js → chunk-p6se1rx0.js} +102 -102
- package/chunk-p9dpshev.js +6968 -0
- package/chunk-pa42wxr0.js +116 -0
- package/{chunk-TA5QI4MM.js → chunk-paxthgqp.js} +56 -107
- package/{readlineWorkspace-ZH5FGDMI.js → chunk-pb35s8d9.js} +5395 -5786
- package/{chunk-XT6C2QZW.js → chunk-pbebzbcq.js} +8 -8
- package/chunk-pbx7e1mb.js +11 -0
- package/chunk-pe5czfn9.js +8 -0
- package/chunk-pextk1j4.js +62 -0
- package/chunk-pf5rywc6.js +575 -0
- package/chunk-pjfhd3fz.js +136 -0
- package/{chunk-ZCMAKAZM.js → chunk-pkhqb0fs.js} +118 -59
- package/chunk-pm5xb9jh.js +26 -0
- package/{chunk-Y3KGWZKF.js → chunk-ppdmmdkx.js} +110 -97
- package/chunk-pqp6y2hs.js +9 -0
- package/{chunk-QHEN7NZ7.js → chunk-ptz8fm2n.js} +12 -17
- package/{chunk-EUHMI5KG.js → chunk-pwqnvbt7.js} +65 -46
- package/chunk-pzr31nz7.js +12 -0
- package/{chunk-WWC7I6TA.js → chunk-q3cx89sw.js} +2 -4
- package/chunk-q5pe7be3.js +78 -0
- package/{chunk-ZHWTBHT3.js → chunk-q6zqbxp2.js} +418 -409
- package/chunk-q7r0e1p3.js +15 -0
- package/chunk-q8t2fecd.js +6 -0
- package/{chunk-2QAZCYOM.js → chunk-q94zx4d0.js} +721 -753
- package/{runtimeDoctorCommands-52MBG5JN.js → chunk-qarz1az1.js} +65 -85
- package/{chunk-4INJGWE3.js → chunk-qdcvr25b.js} +72 -53
- package/{streamAgentChatTurn-KOUWITHH.js → chunk-qxs27wfg.js} +1079 -1475
- package/{chunk-GIXWVN2W.js → chunk-r06ycjjr.js} +50 -47
- package/{chunk-63W5N6P3.js → chunk-r20jw5hb.js} +57 -38
- package/{chunk-I5BYFTYK.js → chunk-r7sfwdka.js} +1201 -1142
- package/{chunk-UMADT6B2.js → chunk-r9sp1yyb.js} +6 -8
- package/{handleSendMessageAction-FHGUFT2X.js → chunk-rbd2vgb3.js} +627 -757
- package/chunk-rc37js46.js +12 -0
- package/{chunk-5YI447UL.js → chunk-rmngk6r3.js} +67 -76
- package/chunk-rnhbnbrq.js +15 -0
- package/{chunk-WRHVVIGI.js → chunk-rsfnmrt5.js} +111 -131
- package/{chunk-YFSM2366.js → chunk-rvvn2rhj.js} +28 -22
- package/{chunk-4Y6CZ2N4.js → chunk-s0fpz7ht.js} +37 -33
- package/{chunk-VI4SWTUO.js → chunk-s1cqzb2r.js} +77 -65
- package/{chunk-6QDYSCPW.js → chunk-s8q81tda.js} +73 -65
- package/chunk-s9hy639e.js +6 -0
- package/{chunk-R5GO3YZP.js → chunk-s9vbwme8.js} +115 -106
- package/chunk-sger1m1b.js +20 -0
- package/chunk-spe6crvk.js +60 -0
- package/chunk-spwsrvh9.js +70 -0
- package/chunk-sr05zpk7.js +573 -0
- package/{chunk-M2EJHDJP.js → chunk-srncrd3f.js} +1 -12
- package/{chunk-QXHFIX7W.js → chunk-sry80bpx.js} +71 -53
- package/{chunk-PSCNXBYA.js → chunk-t6q69p83.js} +3 -4
- package/{chunk-UAD2WT54.js → chunk-t714r4q0.js} +9 -28
- package/chunk-t7vbraqe.js +42 -0
- package/{chunk-CVMMWN6P.js → chunk-td7653rr.js} +10 -20
- package/{chunk-3BQDF4QB.js → chunk-tmytcetz.js} +17 -21
- package/{chunk-AMWMV56B.js → chunk-tta4prwg.js} +16 -15
- package/chunk-tvh8azgq.js +4829 -0
- package/{chunk-ID4G2MAR.js → chunk-v08kbfm8.js} +10 -11
- package/{chunk-OKPOQGQJ.js → chunk-v1x702q8.js} +801 -852
- package/{chunk-KHCLMKZZ.js → chunk-v39fea91.js} +576 -247
- package/{chunk-OW765XBU.js → chunk-vejtd8p2.js} +3 -4
- package/chunk-vep7zkha.js +368 -0
- package/chunk-vfwn1k7n.js +142 -0
- package/chunk-vg68vxhx.js +20 -0
- package/chunk-vgcqgnsm.js +31 -0
- package/{chunk-NF64D32W.js → chunk-vnzyjdea.js} +1 -3
- package/{chunk-ZCLSM5AK.js → chunk-vqgtw1px.js} +800 -717
- package/chunk-vv1gr5xz.js +76 -0
- package/{spaceCommands-Z2CTQD6G.js → chunk-w1qeadpq.js} +176 -242
- package/chunk-w468ez1p.js +13 -0
- package/chunk-w49s0t79.js +14 -0
- package/{mcpLocalManageCommands-KUDZQRM4.js → chunk-w58zhfbn.js} +91 -93
- package/chunk-w5b74aq6.js +11 -0
- package/chunk-w7y41qyw.js +117 -0
- package/chunk-w820gg1w.js +4165 -0
- package/{chunk-DBJAJ4UD.js → chunk-wdsmaqmt.js} +399 -453
- package/{chunk-3EUW3WEO.js → chunk-wfvvnmmj.js} +1 -3
- package/{chunk-QY4NZCDA.js → chunk-wkr04bhk.js} +432 -651
- package/{chunk-KUG52IAP.js → chunk-ws402twv.js} +1 -3
- package/chunk-wzwh311q.js +164 -0
- package/{runtimeModeArgs-X7DL7PUN.js → chunk-x5vtgvwn.js} +6 -5
- package/{agentMachineCommands-COJD2KPZ.js → chunk-x8h2csmq.js} +152 -195
- package/{chunk-BAZCS5XG.js → chunk-x9zfjnrc.js} +59 -47
- package/{chunk-WTIJKWCI.js → chunk-xcarc2rf.js} +50 -56
- package/chunk-xf33gz0a.js +8 -0
- package/{chunk-PVFYPUIM.js → chunk-y7b3gwx1.js} +78 -71
- package/{memoryCommands-SVLAVFCV.js → chunk-y7sphmnh.js} +119 -95
- package/{agentListCommands-436FHOIT.js → chunk-ycvaafhc.js} +260 -374
- package/chunk-yd7zprb0.js +9 -0
- package/chunk-yfz3d27k.js +40 -0
- package/{chunk-MKR6KDD3.js → chunk-ykv9g3m4.js} +141 -125
- package/chunk-ystjgb9e.js +544 -0
- package/chunk-yysxn7ep.js +12 -0
- package/{chunk-AL3TYDIF.js → chunk-z17eyawk.js} +18 -16
- package/{machineCommands-RVB5OG2X.js → chunk-z17q416p.js} +442 -563
- package/chunk-z1n98pcf.js +12 -0
- package/{dialogTraceCommand-HWXGIGZX.js → chunk-z566jx5r.js} +292 -397
- package/chunk-zjxd6942.js +6 -0
- package/{versionChangeNotice-RFABJGJE.js → chunk-zk11x9y5.js} +7 -9
- package/{chunk-TEFD5E2D.js → chunk-zt73yc33.js} +48 -51
- package/chunk-zvs2fra1.js +5 -0
- package/{chunk-SVFFKLMR.js → chunk-zwee84rh.js} +117 -141
- package/chunk-zxgfqv72.js +8 -0
- package/index.js +53 -69
- package/package.json +4 -4
- package/_executeModel-YQNZNR2Y.js +0 -341
- package/action-UK3HXGXF.js +0 -30
- package/addContentAction-CAKFVBDM.js +0 -38
- package/addDialogAgentAction-KLJ2YWWM.js +0 -77
- package/agentDeleteCommand-S5FVPYKT.js +0 -523
- package/agentEmailCommands-ZEFMGVKM.js +0 -742
- package/agentRecordCommands-JZ35AC5A.js +0 -211
- package/agentRun-UIO43TUG.js +0 -251
- package/agentRunCommand-ATLKKD6O.js +0 -249
- package/agentRunControl-U2MV74WR.js +0 -211
- package/agentRuntimeLocal-P4MMGIR6.js +0 -189
- package/agentSlice-HHNKNNYM.js +0 -52
- package/appConfigCommands-W4JQHMOJ.js +0 -417
- package/appDeleteCommands-IXR47IAV.js +0 -183
- package/appDeployCommands-LB6DHDF4.js +0 -232
- package/appDeployStatusCommands-UQ6MJL6D.js +0 -204
- package/appGetCommands-M5JCTDDQ.js +0 -185
- package/appListCommands-TE3ZHE3I.js +0 -186
- package/appMigrateCommands-FOXH6R4B.js +0 -230
- package/appSourceCommands-3ACYB6I7.js +0 -279
- package/authCommand-I5OBJP5S.js +0 -66
- package/authCommands-I6KCGOCN.js +0 -54
- package/builtinSkillRegistry-MIXYSZUU.js +0 -35
- package/chromeOpsCommands-IZRCYDSW.js +0 -579
- package/chunk-24BICQPB.js +0 -25
- package/chunk-2E4BYAUE.js +0 -69
- package/chunk-2O72G3U2.js +0 -17157
- package/chunk-2OROOEOQ.js +0 -229
- package/chunk-2R6M34JP.js +0 -27225
- package/chunk-2SXN7LAY.js +0 -15
- package/chunk-35JIFLIG.js +0 -30
- package/chunk-3VXVE2LF.js +0 -12
- package/chunk-4DBTBNTX.js +0 -477
- package/chunk-4JH2UMJR.js +0 -40
- package/chunk-4RUQ2PFX.js +0 -432
- package/chunk-4SN6J65J.js +0 -38
- package/chunk-5MMAK75Q.js +0 -15
- package/chunk-6NYQHXMV.js +0 -1737
- package/chunk-6R4IY46J.js +0 -14
- package/chunk-7E6YXSFZ.js +0 -441
- package/chunk-7K4J76JQ.js +0 -917
- package/chunk-7LR2IFUH.js +0 -100
- package/chunk-AETZ7AD6.js +0 -274
- package/chunk-ATNTHIZI.js +0 -13
- package/chunk-BPB3T5EU.js +0 -791
- package/chunk-BV3FLP2L.js +0 -133
- package/chunk-BVYHE6QD.js +0 -110
- package/chunk-BWJLEN6X.js +0 -1388
- package/chunk-CHOZJMJS.js +0 -78
- package/chunk-D2LTQAGN.js +0 -464
- package/chunk-DALXH7BG.js +0 -3392
- package/chunk-DBCWXGOC.js +0 -15210
- package/chunk-DLXL4NSL.js +0 -1179
- package/chunk-DSBRIUX5.js +0 -23
- package/chunk-DWLEOOGR.js +0 -8
- package/chunk-E2KZNP6C.js +0 -13
- package/chunk-E5JV4DCB.js +0 -272
- package/chunk-EUGTFFO6.js +0 -42
- package/chunk-FBNVJ2TO.js +0 -294
- package/chunk-FGXG6OLW.js +0 -34
- package/chunk-G22P2J7O.js +0 -1775
- package/chunk-HEVRQGGH.js +0 -106
- package/chunk-HHXF57CL.js +0 -180
- package/chunk-HI5RS2O4.js +0 -2109
- package/chunk-HJTI7RZV.js +0 -94
- package/chunk-HRWRIPTB.js +0 -531
- package/chunk-HYCVEESH.js +0 -2824
- package/chunk-I42VC5KR.js +0 -15
- package/chunk-IB5AR52X.js +0 -15
- package/chunk-IIRJ4P4I.js +0 -24
- package/chunk-IT3ZZRJE.js +0 -12
- package/chunk-IV6VUVLJ.js +0 -88
- package/chunk-J42X32PV.js +0 -472
- package/chunk-JDMWYVUF.js +0 -449
- package/chunk-JKVOKKMQ.js +0 -53
- package/chunk-JRTNUU5Z.js +0 -54
- package/chunk-K7HLFORE.js +0 -226
- package/chunk-KGBASAB5.js +0 -74
- package/chunk-KGKTN2NX.js +0 -25
- package/chunk-KX6YBIHG.js +0 -8
- package/chunk-KYHNSSL2.js +0 -62
- package/chunk-KZO2MTVM.js +0 -140
- package/chunk-LKZPX6YH.js +0 -0
- package/chunk-LLP2NDED.js +0 -201
- package/chunk-LOFRSJX2.js +0 -47
- package/chunk-M75RM77T.js +0 -3970
- package/chunk-MDAYRIP7.js +0 -1889
- package/chunk-MI6HRMCK.js +0 -25
- package/chunk-MTNQNLSE.js +0 -1029
- package/chunk-NH4PUEWK.js +0 -762
- package/chunk-NHLLXINQ.js +0 -8
- package/chunk-NTUNHTJV.js +0 -40
- package/chunk-OJKUU3LP.js +0 -67
- package/chunk-OQH3TAB7.js +0 -299
- package/chunk-P62DHV4F.js +0 -443
- package/chunk-PHIERFHT.js +0 -574
- package/chunk-PKDFU6MY.js +0 -102
- package/chunk-PL5RKESU.js +0 -40
- package/chunk-PSJJBBWV.js +0 -42
- package/chunk-QKM55LP2.js +0 -739
- package/chunk-QQ56R7KS.js +0 -88
- package/chunk-QYPE3TS3.js +0 -43
- package/chunk-REAE2SZK.js +0 -62
- package/chunk-RGQ4UUWZ.js +0 -208
- package/chunk-RIRFHJTW.js +0 -353
- package/chunk-RYMLMTPZ.js +0 -547
- package/chunk-SSGTV4FC.js +0 -303
- package/chunk-SVKWZ3EG.js +0 -607
- package/chunk-T7KODHYS.js +0 -317
- package/chunk-TGVVE5BA.js +0 -3085
- package/chunk-TKLBBR7P.js +0 -779
- package/chunk-TZXPT53C.js +0 -8
- package/chunk-U67V476Y.js +0 -35
- package/chunk-UEILX4EV.js +0 -15
- package/chunk-VIWDGTDA.js +0 -8
- package/chunk-VXUQWCEZ.js +0 -138
- package/chunk-W5GKF5ZW.js +0 -32
- package/chunk-WDWZPASX.js +0 -29
- package/chunk-WX266G2C.js +0 -9594
- package/chunk-YCITCYSJ.js +0 -356
- package/chunk-Z3KTXWWB.js +0 -76
- package/chunk-ZC3CMUKU.js +0 -322
- package/chunk-ZSZBHRLP.js +0 -1710
- package/chunk-ZV7ZY46L.js +0 -1343
- package/cliAgentConfigHelpers-WB2S7PDO.js +0 -47
- package/cliAuthorityBrokerClient-3SKOA7OZ.js +0 -23
- package/cliAuthorityBrokerProcess-BMBVXPKU.js +0 -24
- package/cliAuthorityStoreDriver-6FE3BJNB.js +0 -20
- package/cliEnvHelpers-7V5ZWUJA.js +0 -34
- package/cliExecutor-N3445G2Z.js +0 -45
- package/cliLocalAgentRecordReader-QKKRK53P.js +0 -113
- package/cloudAutomationCommands-S52NKCLL.js +0 -310
- package/contentThunks-ERA42OXW.js +0 -100
- package/cooldownCommands-ZFDAL4MR.js +0 -79
- package/createAgentAutomationAction-DMD2VDPI.js +0 -173
- package/createDialogAction-3SPOJQAK.js +0 -87
- package/createPageAction-AVXUMZVT.js +0 -91
- package/cursorProvider-2RACMEMF.js +0 -6123
- package/db-LTHLW7QA.js +0 -178
- package/dbSlice-WNUHTF4C.js +0 -76
- package/deleteDbKey-KV6MYBAI.js +0 -87
- package/desktopFolderProjects-4POAMVJN.js +0 -45
- package/dialogCommands-4MOLJW4G.js +0 -165
- package/dialogMessageRecord-2HJTEXQX.js +0 -7
- package/dialogNotifyCommand-YL2GFKLO.js +0 -45
- package/dialogSlice-Q7H2YVNO.js +0 -200
- package/dialogUsageCommand-IO2BQKL2.js +0 -31
- package/docCreateCommands-OYLRBK2M.js +0 -255
- package/docDeleteCommands-3NF6QPJU.js +0 -226
- package/docReadCommands-CBAERJAP.js +0 -184
- package/docStore-GWPFDV6Y.js +0 -89
- package/docUpdateCommands-BAMCGX6F.js +0 -326
- package/ebookDownloaderAgentCommand-NOIZ6U7Y.js +0 -675
- package/fetchMessages-P7FSCSXV.js +0 -19
- package/getPricing-KNXVEIUI.js +0 -40
- package/hostCredentialCreate-2VEYBXJV.js +0 -402
- package/i18n-SAK6DDY4.js +0 -51
- package/keys-FXEGMKSG.js +0 -183
- package/legacyMigration-TF4SYJCZ.js +0 -19
- package/levelAuthorityStore-L5SVDM5O.js +0 -10
- package/localAutomationCommands-XSK7SM7I.js +0 -199
- package/localAutomationRuntime-5WBO7HGQ.js +0 -61
- package/localRecall-BDYKXB46.js +0 -17
- package/localRuntimeAdapter-Y6H6XRIH.js +0 -310
- package/localRuntimeAuthority-Q5C2KWGF.js +0 -45
- package/localRuntimeDb-MM3DINZM.js +0 -12
- package/machineInfo-HNPJHQFU.js +0 -14
- package/machineWsRunDispatch-Y5SJGCEE.js +0 -228
- package/models-TOXYINNM.js +0 -10
- package/oauthTokenStore-RUYNYALS.js +0 -24
- package/overlay-JA3SPVUS.js +0 -15
- package/parityCommands-5OCBPVES.js +0 -613
- package/processSpawn-6GEO77KV.js +0 -11
- package/profileConfig-KOWK4HOG.js +0 -49
- package/projectArchive-WE3VVPZ2.js +0 -132
- package/providerPresetApply-YCVSP2WX.js +0 -235
- package/providerRegistry-26PAUYH2.js +0 -24
- package/read-D4WYJQJQ.js +0 -32
- package/readDocTool-3S3OQFOH.js +0 -22
- package/rememberExplicit-ZPI6KK5M.js +0 -16
- package/remove-AHRMUDMH.js +0 -32
- package/removeDialogAgentAction-AALRBFSA.js +0 -81
- package/secretCommands-2PSS3QNZ.js +0 -24
- package/setDialogExtraReferencesAction-SLHQ3GYL.js +0 -39
- package/setPrimaryDialogAgentAction-XKCSGJ7M.js +0 -96
- package/skillDiscovery-IUEZZG2B.js +0 -29
- package/skillSummaryMarker-62UW7HI7.js +0 -9
- package/slateToRenderMarkdown-R3X74MCX.js +0 -7
- package/slateUtils-ESQAJ6RN.js +0 -19
- package/store-WSAVTR32.js +0 -50
- package/switchDialogAgentAction-YJIWQGIR.js +0 -70
- package/tombstones-FQQMEDOW.js +0 -32
- package/toolApiClient-FVXTIIEN.js +0 -42
- package/tools-WN4AVSNS.js +0 -147
- package/tuiRestart-LU2BDLAW.js +0 -50
- package/tuiSummaryLlmCaller-Z2ZVBIGN.js +0 -53
- package/updateCommands-4QP7WJLA.js +0 -63
- package/usageCommand-AZX7D3LB.js +0 -31
- package/usageRebuildCommand-XV5QQCHK.js +0 -623
- package/userDataMerge-HT2ZVHBJ.js +0 -13
- package/windowsSelfUpdate-5OFKSW6Y.js +0 -14
- package/workContext-ZAW6VXT7.js +0 -18
- package/workContextAccess-VU5I4LKZ.js +0 -11
- package/workContextWriteNotice-YCSSMHN4.js +0 -7
- /package/{chunk-BYXBJQAS.js → chunk-0bcc25vg.js} +0 -0
|
@@ -1,302 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
2
|
TOOL_DISCOVERY_CALL_NAME,
|
|
3
3
|
TOOL_DISCOVERY_ID_PREFIX,
|
|
4
|
-
|
|
5
|
-
filterToolDiscoveryHistory,
|
|
6
|
-
isToolDiscoveryCall,
|
|
4
|
+
isHotEagerTool,
|
|
7
5
|
mcpDiscoveryEntry,
|
|
6
|
+
toolDiscoveryReference,
|
|
8
7
|
readToolLoadingReferences,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from "./chunk-ZCLSM5AK.js";
|
|
14
|
-
import {
|
|
15
|
-
isRecord
|
|
16
|
-
} from "./chunk-KUG52IAP.js";
|
|
8
|
+
createToolLoadingState,
|
|
9
|
+
isToolDiscoveryCall,
|
|
10
|
+
filterToolDiscoveryHistory
|
|
11
|
+
} from "./chunk-1fv9k8zt.js";
|
|
17
12
|
import {
|
|
18
|
-
isFireworksKimiModel,
|
|
19
13
|
isNoloHostedProvider,
|
|
14
|
+
isFireworksKimiModel,
|
|
20
15
|
resolvePlatformEffectiveModel
|
|
21
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-fkrgqq8w.js";
|
|
17
|
+
import {
|
|
18
|
+
getModelContextWindow
|
|
19
|
+
} from "./chunk-vqgtw1px.js";
|
|
22
20
|
import {
|
|
23
21
|
asTrimmedLowercaseString
|
|
24
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-h1hzmqph.js";
|
|
23
|
+
import {
|
|
24
|
+
isRecord
|
|
25
|
+
} from "./chunk-ws402twv.js";
|
|
25
26
|
import {
|
|
26
27
|
asOptionalTrimmedString
|
|
27
|
-
} from "./chunk-
|
|
28
|
-
|
|
29
|
-
// packages/integrations/openai/responsesToolDiscovery.ts
|
|
30
|
-
var searchTool = {
|
|
31
|
-
type: "tool_search",
|
|
32
|
-
execution: "client",
|
|
33
|
-
description: "Find tools by name, keywords, category, or capability pack. Pass capabilityPackId to activate an entire authorized pack.",
|
|
34
|
-
parameters: {
|
|
35
|
-
type: "object",
|
|
36
|
-
properties: { query: { type: "string" }, capabilityPackId: { type: "string" } },
|
|
37
|
-
required: ["query"]
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var eagerNames = /* @__PURE__ */ new Set(["loadSkill", "ask_user"]);
|
|
41
|
-
function hasDeferrableResponsesTools(tools) {
|
|
42
|
-
return !!tools?.length && tools.every((tool) => tool.type === "function") && tools.some((tool) => !!tool.function?.name && tool.function.name !== TOOL_DISCOVERY_CALL_NAME && !eagerNames.has(tool.function.name));
|
|
43
|
-
}
|
|
44
|
-
var hostedPrefix = `${TOOL_DISCOVERY_ID_PREFIX}openai-hosted:`;
|
|
45
|
-
function parseJson(value) {
|
|
46
|
-
if (typeof value !== "string") return value;
|
|
47
|
-
try {
|
|
48
|
-
return JSON.parse(value);
|
|
49
|
-
} catch {
|
|
50
|
-
return void 0;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function readSearchItem(item) {
|
|
54
|
-
if (!isRecord(item) || item.type !== "tool_search_call" || item.execution !== "client" || typeof item.call_id !== "string" || !item.call_id || item.status !== void 0 && item.status !== "completed") return void 0;
|
|
55
|
-
const args = parseJson(item.arguments);
|
|
56
|
-
if (!isRecord(args) || typeof args.query !== "string") return void 0;
|
|
57
|
-
return {
|
|
58
|
-
type: "tool_search_call",
|
|
59
|
-
call_id: item.call_id,
|
|
60
|
-
execution: "client",
|
|
61
|
-
arguments: { ...args, query: args.query },
|
|
62
|
-
...typeof item.id === "string" ? { id: item.id } : {},
|
|
63
|
-
...typeof item.status === "string" ? { status: item.status } : {}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
function bridgeResponsesToolSearch(item) {
|
|
67
|
-
const search = readSearchItem(item);
|
|
68
|
-
if (!search) throw new Error("Responses returned an invalid client tool_search_call.");
|
|
69
|
-
return {
|
|
70
|
-
id: TOOL_DISCOVERY_ID_PREFIX + search.call_id,
|
|
71
|
-
type: "function",
|
|
72
|
-
function: { name: TOOL_DISCOVERY_CALL_NAME, arguments: JSON.stringify(search) }
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
var isMcpEntry = (entry) => "mcp" in entry;
|
|
76
|
-
function isFunctionTool(tool) {
|
|
77
|
-
return isRecord(tool) && tool.type === "function" && typeof tool.name === "string" && (tool.description === void 0 || typeof tool.description === "string");
|
|
78
|
-
}
|
|
79
|
-
function readHostedPaths(raw) {
|
|
80
|
-
if (!isRecord(raw) || !Array.isArray(raw.paths) || !raw.paths.every((path) => typeof path === "string")) return;
|
|
81
|
-
return raw.paths;
|
|
82
|
-
}
|
|
83
|
-
function finishResponsesToolDiscovery(calls, plan, output) {
|
|
84
|
-
if (plan?.protocol === "openai-hosted") {
|
|
85
|
-
const next = [];
|
|
86
|
-
const results2 = [];
|
|
87
|
-
const ordinary = new Map(calls.map((call) => [call.id, call]));
|
|
88
|
-
const visible = new Set(plan.visible);
|
|
89
|
-
let paths;
|
|
90
|
-
for (const item of Array.isArray(output) ? output : []) {
|
|
91
|
-
if (item?.type === "tool_search_call") {
|
|
92
|
-
if (item.execution !== "server") throw new Error("OpenAI returned client search in hosted mode.");
|
|
93
|
-
paths = item.call_id === null && item.status === "completed" ? readHostedPaths(item.arguments) : void 0;
|
|
94
|
-
} else if (item?.type === "tool_search_output" && item.execution === "server" && item.call_id === null && item.status === "completed" && paths && Array.isArray(item.tools)) {
|
|
95
|
-
const idBase = hostedPrefix + (typeof item.id === "string" && item.id ? item.id : `round-${plan.replayedCalls.size}`);
|
|
96
|
-
let id = idBase;
|
|
97
|
-
for (let index = 1; plan.replayedCalls.has(id) || next.some((call) => call.id === id); index++) id = `${idBase}-${index}`;
|
|
98
|
-
const names = /* @__PURE__ */ new Set();
|
|
99
|
-
for (const tool of item.tools) {
|
|
100
|
-
if (isFunctionTool(tool) && !visible.has(tool.name)) names.add(tool.name);
|
|
101
|
-
}
|
|
102
|
-
const loaded = [...names].flatMap((name) => {
|
|
103
|
-
const current = plan.state.authorized.get(name);
|
|
104
|
-
if (!current || isMcpEntry(current)) return [];
|
|
105
|
-
visible.add(name);
|
|
106
|
-
return [toolDiscoveryReference(current)];
|
|
107
|
-
});
|
|
108
|
-
next.push({ id, type: "function", function: { name: TOOL_DISCOVERY_CALL_NAME, arguments: JSON.stringify({ paths }) } });
|
|
109
|
-
results2.push({ callId: id, content: JSON.stringify({ loaded }) });
|
|
110
|
-
paths = void 0;
|
|
111
|
-
} else if (item?.type === "function_call") {
|
|
112
|
-
const call = ordinary.get(item.call_id);
|
|
113
|
-
if (!call) continue;
|
|
114
|
-
if (!visible.has(call.function.name) || plan.replayedCalls.has(call.id)) {
|
|
115
|
-
throw new Error("OpenAI Responses called a tool outside its authorized visible catalog.");
|
|
116
|
-
}
|
|
117
|
-
ordinary.delete(call.id);
|
|
118
|
-
next.push(call);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return { calls: next, results: results2 };
|
|
122
|
-
}
|
|
123
|
-
const results = resolveResponsesToolDiscovery(calls, plan);
|
|
124
|
-
const tagged = plan?.protocol === "openai-client" ? calls.map((call) => isToolDiscoveryCall(call) ? { ...call, function: { ...call.function, arguments: JSON.stringify({ ...parseJson(call.function.arguments), protocol: plan.protocol }) } } : call) : calls;
|
|
125
|
-
return { calls: tagged, results };
|
|
126
|
-
}
|
|
127
|
-
function prepareResponsesToolDiscovery(body, compacted, mcpTools, protocol = "deepseek-client") {
|
|
128
|
-
if (!Array.isArray(body.tools) || !Array.isArray(body.input) || !body.tools.length || !body.tools.every(isFunctionTool)) return void 0;
|
|
129
|
-
const functions = body.tools.filter((tool) => tool.name !== TOOL_DISCOVERY_CALL_NAME);
|
|
130
|
-
const mcpEntries = functions.some((tool) => tool.name === "callMcpTool") ? mcpTools?.map(mcpDiscoveryEntry) ?? [] : [];
|
|
131
|
-
const state = createToolLoadingState([...functions, ...mcpEntries]);
|
|
132
|
-
const catalog = state.authorized;
|
|
133
|
-
const eager = new Set([...catalog.keys()].filter((name) => eagerNames.has(name) || name === "callMcpTool" && mcpTools !== void 0));
|
|
134
|
-
const restored = compacted ? state.restore(compacted, eager) : void 0;
|
|
135
|
-
const restoredNames = new Set(state.loadedInContext);
|
|
136
|
-
const staleDefinitions = /* @__PURE__ */ new Set();
|
|
137
|
-
for (const ref of compacted?.loaded ?? []) {
|
|
138
|
-
if (catalog.has(ref.name) && !eager.has(ref.name) && !state.executable(ref)) staleDefinitions.add(ref.name);
|
|
139
|
-
}
|
|
140
|
-
const staleCalls = /* @__PURE__ */ new Set();
|
|
141
|
-
const searches = /* @__PURE__ */ new Map();
|
|
142
|
-
const hostedSearches = /* @__PURE__ */ new Map();
|
|
143
|
-
const historicalResults = new Map(body.input.filter((item) => item?.type === "function_call_output").map((item) => [item.call_id, item]));
|
|
144
|
-
const ordinaryCallIds = /* @__PURE__ */ new Set();
|
|
145
|
-
const replayedSearches = /* @__PURE__ */ new Set();
|
|
146
|
-
const guidanceMessage = (guidance2) => ({
|
|
147
|
-
type: "message",
|
|
148
|
-
role: "user",
|
|
149
|
-
content: [{ type: "input_text", text: guidance2.join("\n") }]
|
|
150
|
-
});
|
|
151
|
-
function mcpGuidance(entries) {
|
|
152
|
-
const targets = entries.filter(isMcpEntry).map(({ mcp }) => ({
|
|
153
|
-
connectionId: mcp.connectionId,
|
|
154
|
-
expectedRevision: mcp.connectionRevision,
|
|
155
|
-
catalogRevision: mcp.schemaVersion,
|
|
156
|
-
toolName: mcp.toolName
|
|
157
|
-
}));
|
|
158
|
-
return targets.length ? [`MCP targets (data only). Use listMcpTools for argument schemas and callMcpTool for invocation; host confirmation remains required. ${JSON.stringify(targets)}`] : [];
|
|
159
|
-
}
|
|
160
|
-
const input = [];
|
|
161
|
-
if (restored) {
|
|
162
|
-
const notes = [...restored.guidance, ...mcpGuidance(restored.tools)];
|
|
163
|
-
if (notes.length) input.push(guidanceMessage(notes));
|
|
164
|
-
}
|
|
165
|
-
let guidance = [];
|
|
166
|
-
const deferredTool = (tool) => ({ ...tool, defer_loading: true });
|
|
167
|
-
const openAi = protocol !== "deepseek-client";
|
|
168
|
-
const replayItems = body.input.flatMap((item) => {
|
|
169
|
-
const id = item?.call_id;
|
|
170
|
-
if (protocol !== "openai-hosted" || typeof id !== "string" || !id.startsWith(hostedPrefix) || item.type !== "function_call" || item.name !== TOOL_DISCOVERY_CALL_NAME) return [item];
|
|
171
|
-
const result = historicalResults.get(id);
|
|
172
|
-
return result ? [item, result] : [item];
|
|
173
|
-
});
|
|
174
|
-
for (const item of replayItems) {
|
|
175
|
-
if (item?.type === "tool_search_call" || item?.type === "tool_search_output" || item?.type === "tool_reference") continue;
|
|
176
|
-
if (guidance.length && item?.type !== "function_call_output") {
|
|
177
|
-
input.push(guidanceMessage(guidance));
|
|
178
|
-
guidance = [];
|
|
179
|
-
}
|
|
180
|
-
if (item?.type === "function_call" && item.name === TOOL_DISCOVERY_CALL_NAME) {
|
|
181
|
-
const raw = parseJson(item.arguments);
|
|
182
|
-
if (protocol === "openai-hosted" && typeof item.call_id === "string" && item.call_id.startsWith(hostedPrefix) && historicalResults.has(item.call_id) && !hostedSearches.has(item.call_id)) {
|
|
183
|
-
const paths = readHostedPaths(raw);
|
|
184
|
-
if (paths) {
|
|
185
|
-
hostedSearches.set(item.call_id, { paths });
|
|
186
|
-
input.push({ type: "tool_search_call", execution: "server", call_id: null, status: "completed", arguments: { paths } });
|
|
187
|
-
}
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
const search = readSearchItem(raw);
|
|
191
|
-
const source = isRecord(raw) ? raw.protocol ?? "deepseek-client" : void 0;
|
|
192
|
-
if (protocol !== "openai-hosted" && source === protocol && search && item.call_id === TOOL_DISCOVERY_ID_PREFIX + search.call_id && historicalResults.has(item.call_id) && !searches.has(item.call_id)) {
|
|
193
|
-
searches.set(item.call_id, search);
|
|
194
|
-
input.push({
|
|
195
|
-
type: search.type,
|
|
196
|
-
call_id: search.call_id,
|
|
197
|
-
execution: search.execution,
|
|
198
|
-
arguments: search.arguments,
|
|
199
|
-
...openAi ? { status: "completed" } : {}
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
continue;
|
|
203
|
-
}
|
|
204
|
-
if (item?.type === "function_call_output" && typeof item.call_id === "string" && item.call_id.startsWith(TOOL_DISCOVERY_ID_PREFIX)) {
|
|
205
|
-
const search = searches.get(item.call_id);
|
|
206
|
-
const hosted = hostedSearches.has(item.call_id);
|
|
207
|
-
if (!search && !hosted) {
|
|
208
|
-
if (ordinaryCallIds.has(item.call_id) && !staleCalls.delete(item.call_id)) input.push(item);
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (replayedSearches.has(item.call_id)) continue;
|
|
212
|
-
replayedSearches.add(item.call_id);
|
|
213
|
-
const refs = readToolLoadingReferences(item.output);
|
|
214
|
-
for (const ref of refs.loaded) {
|
|
215
|
-
if (catalog.has(ref.name) && !state.loadedInContext.has(ref.name) && !eager.has(ref.name) && !state.executable(ref)) {
|
|
216
|
-
staleDefinitions.add(ref.name);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
const projected = state.restore(refs, eager);
|
|
220
|
-
for (const tool of projected.tools) staleDefinitions.delete(tool.name);
|
|
221
|
-
guidance.push(...projected.guidance, ...mcpGuidance(projected.tools));
|
|
222
|
-
input.push({
|
|
223
|
-
type: "tool_search_output",
|
|
224
|
-
call_id: search?.call_id ?? null,
|
|
225
|
-
execution: hosted ? "server" : "client",
|
|
226
|
-
...openAi ? { status: "completed" } : {},
|
|
227
|
-
tools: projected.tools.filter((tool) => !isMcpEntry(tool)).map((tool) => openAi ? deferredTool(tool) : tool)
|
|
228
|
-
});
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
if (item?.type === "function_call") ordinaryCallIds.add(item.call_id);
|
|
232
|
-
if (item?.type === "function_call" && (staleDefinitions.has(item.name) || openAi && (!catalog.has(item.name) || isMcpEntry(catalog.get(item.name))))) {
|
|
233
|
-
staleCalls.add(item.call_id);
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
if (item?.type === "function_call_output" && staleCalls.delete(item.call_id)) continue;
|
|
237
|
-
if (item?.type === "function_call" && catalog.has(item.name) && !state.loadedInContext.has(item.name)) {
|
|
238
|
-
eager.add(item.name);
|
|
239
|
-
}
|
|
240
|
-
input.push(item);
|
|
241
|
-
}
|
|
242
|
-
if (guidance.length) input.push(guidanceMessage(guidance));
|
|
243
|
-
const visible = /* @__PURE__ */ new Set([...eager, ...[...state.loadedInContext].filter((name) => {
|
|
244
|
-
const entry = catalog.get(name);
|
|
245
|
-
return entry && !isMcpEntry(entry);
|
|
246
|
-
})]);
|
|
247
|
-
body.input = input;
|
|
248
|
-
if (eager.size === catalog.size && replayedSearches.size === 0) return void 0;
|
|
249
|
-
const localTools = [...catalog.values()].filter((tool) => !isMcpEntry(tool));
|
|
250
|
-
body.tools = protocol === "openai-hosted" ? localTools.map((tool) => eager.has(tool.name) || restoredNames.has(tool.name) ? tool : deferredTool(tool)) : localTools.filter((tool) => eager.has(tool.name) || restoredNames.has(tool.name));
|
|
251
|
-
body.tools.push(protocol === "openai-hosted" ? { type: "tool_search" } : protocol === "openai-client" ? {
|
|
252
|
-
...searchTool,
|
|
253
|
-
parameters: { ...searchTool.parameters, additionalProperties: false }
|
|
254
|
-
} : searchTool);
|
|
255
|
-
if (openAi) body.parallel_tool_calls = false;
|
|
256
|
-
return {
|
|
257
|
-
state,
|
|
258
|
-
protocol,
|
|
259
|
-
visible,
|
|
260
|
-
replayedCalls: /* @__PURE__ */ new Set([
|
|
261
|
-
...ordinaryCallIds,
|
|
262
|
-
...hostedSearches.keys(),
|
|
263
|
-
...replayedSearches,
|
|
264
|
-
...[...replayedSearches].map((id) => id.slice(TOOL_DISCOVERY_ID_PREFIX.length))
|
|
265
|
-
])
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
function resolveResponsesToolDiscovery(calls, plan) {
|
|
269
|
-
const results = [];
|
|
270
|
-
const seen = new Set(plan?.replayedCalls);
|
|
271
|
-
const visible = /* @__PURE__ */ new Set([...plan?.visible ?? [], ...plan?.state.loadedInContext ?? []]);
|
|
272
|
-
for (const call of calls) {
|
|
273
|
-
const discovery = isToolDiscoveryCall(call);
|
|
274
|
-
const wireCallId = discovery ? call.id.slice(TOOL_DISCOVERY_ID_PREFIX.length) : call.id;
|
|
275
|
-
if (plan && (seen.has(wireCallId) || seen.has(call.id))) {
|
|
276
|
-
throw new Error("Responses returned a duplicate tool call ID.");
|
|
277
|
-
}
|
|
278
|
-
seen.add(wireCallId);
|
|
279
|
-
seen.add(call.id);
|
|
280
|
-
if (!discovery) {
|
|
281
|
-
if (plan && !plan.visible.has(call.function.name)) {
|
|
282
|
-
throw new Error("Responses requested a function before its definition was loaded.");
|
|
283
|
-
}
|
|
284
|
-
continue;
|
|
285
|
-
}
|
|
286
|
-
if (!plan) throw new Error("Responses requested tool discovery on a route without client discovery.");
|
|
287
|
-
const search = readSearchItem(parseJson(call.function.arguments));
|
|
288
|
-
if (!search || call.id !== TOOL_DISCOVERY_ID_PREFIX + search.call_id) {
|
|
289
|
-
throw new Error("Responses returned an invalid tool discovery ID.");
|
|
290
|
-
}
|
|
291
|
-
const packId = search.arguments.capabilityPackId;
|
|
292
|
-
const pack = typeof packId === "string" ? plan.state.activatePack(packId) : { loaded: [] };
|
|
293
|
-
const excluded = /* @__PURE__ */ new Set([...visible, ...pack.loaded.map(({ name }) => name)]);
|
|
294
|
-
const refs = { ...pack, loaded: [...pack.loaded, ...plan.state.search(search.arguments.query, excluded).map(toolDiscoveryReference)] };
|
|
295
|
-
for (const ref of refs.loaded) visible.add(ref.name);
|
|
296
|
-
results.push({ callId: call.id, content: JSON.stringify(refs) });
|
|
297
|
-
}
|
|
298
|
-
return results;
|
|
299
|
-
}
|
|
28
|
+
} from "./chunk-p1vg4bx8.js";
|
|
300
29
|
|
|
301
30
|
// packages/agent-runtime/toolImages.ts
|
|
302
31
|
var EMPTY_TOOL_RESULT_TEXT = "[Tool completed without output]";
|
|
@@ -309,7 +38,8 @@ function toolImages(message) {
|
|
|
309
38
|
return message[TOOL_IMAGES] ?? [];
|
|
310
39
|
}
|
|
311
40
|
function withoutToolImages(message) {
|
|
312
|
-
if (!(TOOL_IMAGES in message))
|
|
41
|
+
if (!(TOOL_IMAGES in message))
|
|
42
|
+
return message;
|
|
313
43
|
const { [TOOL_IMAGES]: _images, ...textOnly } = message;
|
|
314
44
|
return _images?.length ? {
|
|
315
45
|
...textOnly,
|
|
@@ -321,37 +51,40 @@ function withoutToolImages(message) {
|
|
|
321
51
|
}
|
|
322
52
|
var TOOL_IMAGE_NOTICES = Symbol("runtime-only-tool-image-notices");
|
|
323
53
|
function createToolImageNotices() {
|
|
324
|
-
return { text: [], seen:
|
|
54
|
+
return { text: [], seen: new WeakSet };
|
|
325
55
|
}
|
|
326
56
|
function toolImageNotices(message) {
|
|
327
57
|
const holder = message;
|
|
328
58
|
return holder[TOOL_IMAGE_NOTICES] ??= createToolImageNotices();
|
|
329
59
|
}
|
|
330
|
-
var delivery =
|
|
60
|
+
var delivery = new WeakMap;
|
|
331
61
|
function resetToolImageDelivery(images) {
|
|
332
|
-
for (const image of images)
|
|
62
|
+
for (const image of images)
|
|
63
|
+
delivery.delete(image);
|
|
333
64
|
}
|
|
334
65
|
function toolImageDelivery(image) {
|
|
335
66
|
return delivery.get(image);
|
|
336
67
|
}
|
|
337
68
|
var MAX_TOOL_IMAGES_PER_REQUEST = 4;
|
|
338
69
|
var MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST = 8 * 1024 * 1024;
|
|
339
|
-
var formatToolImageBudgetNote = (omittedCount) => `Note: ${omittedCount} image(s) omitted (request budget: ${MAX_TOOL_IMAGES_PER_REQUEST} images / 8 MiB base64)
|
|
70
|
+
var formatToolImageBudgetNote = (omittedCount) => `Note: ${omittedCount} image(s) omitted (request budget: ${MAX_TOOL_IMAGES_PER_REQUEST} images / 8 MiB base64) — they were NOT delivered, do not guess their content.`;
|
|
340
71
|
function createToolImageBudget(maxImagesPerRequest = MAX_TOOL_IMAGES_PER_REQUEST) {
|
|
341
72
|
const limit = Math.min(MAX_TOOL_IMAGES_PER_REQUEST, maxImagesPerRequest);
|
|
342
73
|
let count = 0;
|
|
343
74
|
let bytes = 0;
|
|
344
|
-
const seen =
|
|
75
|
+
const seen = new Set;
|
|
345
76
|
return (image) => {
|
|
346
|
-
const key = `${image.mimeType}\
|
|
347
|
-
if (seen.has(key))
|
|
77
|
+
const key = `${image.mimeType}\x00${image.dataBase64}`;
|
|
78
|
+
if (seen.has(key))
|
|
79
|
+
return "duplicate";
|
|
348
80
|
seen.add(key);
|
|
349
81
|
const size = Buffer.byteLength(image.dataBase64, "utf8");
|
|
350
82
|
if (size > MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST) {
|
|
351
83
|
delivery.set(image, "impossible");
|
|
352
84
|
return "impossible";
|
|
353
85
|
}
|
|
354
|
-
if (count >= limit || bytes + size > MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST)
|
|
86
|
+
if (count >= limit || bytes + size > MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST)
|
|
87
|
+
return "pending";
|
|
355
88
|
count += 1;
|
|
356
89
|
bytes += size;
|
|
357
90
|
delivery.set(image, "delivered");
|
|
@@ -359,1230 +92,1558 @@ function createToolImageBudget(maxImagesPerRequest = MAX_TOOL_IMAGES_PER_REQUEST
|
|
|
359
92
|
};
|
|
360
93
|
}
|
|
361
94
|
function toolImageSource(message, imageIndex = 0) {
|
|
362
|
-
const path = typeof message.content === "string" ? (Array.from(message.content.matchAll(/→ ([^\s\x00-\x1f\x7f]+)/gu))[imageIndex]?.[1] ?? message.content.match(/→ ([^\s\x00-\x1f\x7f]+)/u)?.[1])?.replace(/\]$/, "") :
|
|
95
|
+
const path = typeof message.content === "string" ? (Array.from(message.content.matchAll(/→ ([^\s\x00-\x1f\x7f]+)/gu))[imageIndex]?.[1] ?? message.content.match(/→ ([^\s\x00-\x1f\x7f]+)/u)?.[1])?.replace(/\]$/, "") : undefined;
|
|
363
96
|
return `${(message.tool_call_id ?? "unknown").replace(/[\x00-\x1f\x7f]/g, "")}${path ? ` (${path})` : ""}`;
|
|
364
97
|
}
|
|
365
98
|
function toolImageOmission(source, impossible) {
|
|
366
99
|
return impossible ? `Tool image permanently downgraded to text: ${source}. Impossible to deliver: single image exceeds 8 MiB base64; will not retry. Do not guess its content.` : `Tool image deferred to a later round in this turn: ${source}. Request budget is 4 images / 8 MiB base64.`;
|
|
367
100
|
}
|
|
368
101
|
function createToolImageLedger() {
|
|
369
|
-
const committed =
|
|
370
|
-
const impossible =
|
|
371
|
-
let pending =
|
|
372
|
-
const keyOf = (image) => `${image.mimeType}\
|
|
102
|
+
const committed = new Set;
|
|
103
|
+
const impossible = new Set;
|
|
104
|
+
let pending = new Set;
|
|
105
|
+
const keyOf = (image) => `${image.mimeType}\x00${image.dataBase64}`;
|
|
373
106
|
const candidates = (images) => (images ?? []).filter((image) => {
|
|
374
107
|
const key = keyOf(image);
|
|
375
|
-
if (committed.has(key) || impossible.has(key) || pending.has(key))
|
|
108
|
+
if (committed.has(key) || impossible.has(key) || pending.has(key))
|
|
109
|
+
return false;
|
|
376
110
|
pending.add(key);
|
|
377
111
|
return true;
|
|
378
112
|
});
|
|
379
113
|
return Object.assign(candidates, {
|
|
380
|
-
/** Only after the provider call succeeded: failed sends must stay replayable. */
|
|
381
114
|
commit(images, retained = []) {
|
|
382
|
-
for (const image of images)
|
|
115
|
+
for (const image of images)
|
|
116
|
+
committed.add(keyOf(image));
|
|
383
117
|
pending = new Set(retained.map(keyOf));
|
|
384
118
|
},
|
|
385
|
-
/** Terminal downgrade is distinct from successful delivery. */
|
|
386
119
|
discard(images) {
|
|
387
|
-
for (const image of images)
|
|
120
|
+
for (const image of images)
|
|
121
|
+
impossible.add(keyOf(image));
|
|
388
122
|
},
|
|
389
|
-
/** 当前生命周期不依赖 reset,仅供未来中途放弃批次时使用。 */
|
|
390
123
|
reset() {
|
|
391
|
-
pending =
|
|
124
|
+
pending = new Set;
|
|
392
125
|
}
|
|
393
126
|
});
|
|
394
127
|
}
|
|
395
128
|
function stripToolImages(message) {
|
|
396
|
-
if (!toolImages(message).length)
|
|
129
|
+
if (!toolImages(message).length)
|
|
130
|
+
return message;
|
|
397
131
|
return withoutToolImages(message);
|
|
398
132
|
}
|
|
399
133
|
|
|
400
|
-
// packages/agent-runtime/
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
const parts = [];
|
|
404
|
-
const omitted = [];
|
|
405
|
-
const budget = createToolImageBudget(maxImagesPerRequest);
|
|
406
|
-
let accepted = 0;
|
|
407
|
-
let deferred = 0;
|
|
408
|
-
let lastTool = -1;
|
|
409
|
-
messages.forEach((message, index) => {
|
|
410
|
-
if (message.role === "tool") lastTool = index;
|
|
411
|
-
if (!supportsImages) return;
|
|
412
|
-
for (const [imageIndex, image] of toolImages(message).entries()) {
|
|
413
|
-
const decision = budget(image);
|
|
414
|
-
if (decision === "duplicate") continue;
|
|
415
|
-
const source = toolImageSource(message, imageIndex);
|
|
416
|
-
if (decision !== "delivered") {
|
|
417
|
-
if (decision === "pending") deferred += 1;
|
|
418
|
-
omitted.push(toolImageOmission(source, decision === "impossible"));
|
|
419
|
-
continue;
|
|
420
|
-
}
|
|
421
|
-
accepted += 1;
|
|
422
|
-
parts.push({
|
|
423
|
-
type: "text",
|
|
424
|
-
text: `Image ${accepted} \u2014 from tool result ${source}`
|
|
425
|
-
});
|
|
426
|
-
parts.push({ type: "image_url", image_url: { url: `data:${image.mimeType};base64,${image.dataBase64}` } });
|
|
427
|
-
}
|
|
428
|
-
});
|
|
429
|
-
if (lastTool < 0) return messages;
|
|
430
|
-
if (!parts.length && !omitted.length) return messages;
|
|
431
|
-
const note = omitted.length ? ` Note: ${omitted.length} image(s) omitted. ${omitted.join(" ")}` : "";
|
|
432
|
-
const attachment = {
|
|
433
|
-
role: "user",
|
|
434
|
-
content: [{ type: "text", text: `${INTRO}${deferred ? ` \u672C\u6B21\u6295\u9012 ${accepted} \u5F20\uFF0C\u53E6\u6709 ${deferred} \u5F20\u672A\u6295\u9012\uFF08${deferred} \u5F20 deferred to a later round\uFF09\u3002` : ""}${note}` }, ...parts]
|
|
435
|
-
};
|
|
436
|
-
return [...messages.slice(0, lastTool + 1), attachment, ...messages.slice(lastTool + 1)];
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
// packages/integrations/openai/responsesHelpers.ts
|
|
440
|
-
var RESPONSES_TOP_LEVEL_SCHEMA_KEYS = ["anyOf", "oneOf", "allOf", "enum", "not"];
|
|
441
|
-
var sanitizeResponsesParameters = (parameters) => {
|
|
442
|
-
if (!isRecord(parameters)) {
|
|
443
|
-
return parameters;
|
|
444
|
-
}
|
|
445
|
-
const next = { ...parameters };
|
|
446
|
-
for (const key of RESPONSES_TOP_LEVEL_SCHEMA_KEYS) {
|
|
447
|
-
delete next[key];
|
|
448
|
-
}
|
|
449
|
-
return next;
|
|
450
|
-
};
|
|
451
|
-
var appendTextPart = (parts, text, role) => {
|
|
452
|
-
if (!text) return;
|
|
453
|
-
const last = parts.at(-1);
|
|
454
|
-
const textType = role === "assistant" ? "output_text" : "input_text";
|
|
455
|
-
if (last?.type === textType) {
|
|
456
|
-
last.text += text;
|
|
134
|
+
// packages/agent-runtime/declaredToolNames.ts
|
|
135
|
+
function extractDeclaredToolNames(tools) {
|
|
136
|
+
if (!Array.isArray(tools))
|
|
457
137
|
return;
|
|
138
|
+
const names = new Set;
|
|
139
|
+
for (const t of tools) {
|
|
140
|
+
const fn = t?.function;
|
|
141
|
+
if (fn && typeof fn.name === "string" && fn.name)
|
|
142
|
+
names.add(fn.name);
|
|
458
143
|
}
|
|
459
|
-
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
144
|
+
return names;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// packages/agent-runtime/toolArgsShape.ts
|
|
148
|
+
function scanRawToolArgs(input) {
|
|
149
|
+
const stack = [];
|
|
150
|
+
const commaAtDepth = [];
|
|
151
|
+
let inString = false;
|
|
152
|
+
let escaped = false;
|
|
153
|
+
const closersOf = () => stack.slice().reverse().map((ch) => ch === "{" ? "}" : "]").join("");
|
|
154
|
+
for (let i = 0;i < input.length; i += 1) {
|
|
155
|
+
const ch = input[i];
|
|
156
|
+
if (inString) {
|
|
157
|
+
if (escaped)
|
|
158
|
+
escaped = false;
|
|
159
|
+
else if (ch === "\\")
|
|
160
|
+
escaped = true;
|
|
161
|
+
else if (ch === '"')
|
|
162
|
+
inString = false;
|
|
476
163
|
continue;
|
|
477
164
|
}
|
|
478
|
-
if (
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
165
|
+
if (ch === '"') {
|
|
166
|
+
inString = true;
|
|
167
|
+
} else if (ch === "{" || ch === "[") {
|
|
168
|
+
stack.push(ch);
|
|
169
|
+
commaAtDepth[stack.length] = undefined;
|
|
170
|
+
} else if (ch === "}" || ch === "]") {
|
|
171
|
+
const open = stack.pop();
|
|
172
|
+
if (open !== (ch === "}" ? "{" : "[")) {
|
|
173
|
+
return {
|
|
174
|
+
inString,
|
|
175
|
+
mismatched: true,
|
|
176
|
+
unclosed: false,
|
|
177
|
+
tailArrayComma: null
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
commaAtDepth[stack.length + 1] = undefined;
|
|
181
|
+
} else if (ch === "," && stack[stack.length - 1] === "[") {
|
|
182
|
+
commaAtDepth[stack.length] = { index: i, closers: closersOf() };
|
|
485
183
|
}
|
|
486
184
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
var toResponsesTools = (tools) => {
|
|
494
|
-
if (!Array.isArray(tools) || tools.length === 0) return void 0;
|
|
495
|
-
const normalized = tools.map((tool) => {
|
|
496
|
-
const fn = tool?.function;
|
|
497
|
-
const name = fn?.name ?? tool?.name;
|
|
498
|
-
if (!name) return null;
|
|
499
|
-
const parameters = fn?.parameters ?? tool?.parameters;
|
|
500
|
-
return {
|
|
501
|
-
type: "function",
|
|
502
|
-
name,
|
|
503
|
-
...typeof (fn?.description ?? tool?.description) === "string" ? { description: fn?.description ?? tool?.description } : {},
|
|
504
|
-
parameters: sanitizeResponsesParameters(parameters),
|
|
505
|
-
...fn?.strict !== void 0 || tool?.strict !== void 0 ? { strict: fn?.strict ?? tool?.strict } : {}
|
|
506
|
-
};
|
|
507
|
-
}).filter(Boolean);
|
|
508
|
-
return normalized.length > 0 ? normalized : void 0;
|
|
509
|
-
};
|
|
510
|
-
var convertMessagesToResponsesInput = (messages, options) => {
|
|
511
|
-
const input = [];
|
|
512
|
-
const imageBudget = createToolImageBudget(options?.maxImagesPerRequest);
|
|
513
|
-
let acceptedImages = 0;
|
|
514
|
-
for (const message of messages) {
|
|
515
|
-
if (!message?.role) continue;
|
|
516
|
-
if (message.role === "tool") {
|
|
517
|
-
if (!message.tool_call_id) continue;
|
|
518
|
-
if (!options?.supportsToolImages || !hadToolImages(message)) {
|
|
519
|
-
input.push({
|
|
520
|
-
type: "function_call_output",
|
|
521
|
-
call_id: message.tool_call_id,
|
|
522
|
-
output: normalizeToolOutput(message.content)
|
|
523
|
-
});
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
const images = toolImages(message);
|
|
527
|
-
const text = typeof message.content === "string" ? message.content : normalizeToolOutput(message.content);
|
|
528
|
-
const effectiveText = text.trim() ? text : EMPTY_TOOL_RESULT_TEXT;
|
|
529
|
-
const outputParts = [
|
|
530
|
-
{ type: "input_text", text: effectiveText }
|
|
531
|
-
];
|
|
532
|
-
let toolOmitted = 0;
|
|
533
|
-
for (const image of images) {
|
|
534
|
-
const decision = imageBudget(image);
|
|
535
|
-
if (decision === "duplicate") continue;
|
|
536
|
-
if (decision !== "delivered") {
|
|
537
|
-
toolOmitted += 1;
|
|
538
|
-
continue;
|
|
539
|
-
}
|
|
540
|
-
acceptedImages += 1;
|
|
541
|
-
outputParts.push({
|
|
542
|
-
type: "input_text",
|
|
543
|
-
text: `Image ${acceptedImages}`
|
|
544
|
-
});
|
|
545
|
-
outputParts.push({
|
|
546
|
-
type: "input_image",
|
|
547
|
-
image_url: `data:${image.mimeType};base64,${image.dataBase64}`
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
if (toolOmitted > 0) {
|
|
551
|
-
outputParts.push({
|
|
552
|
-
type: "input_text",
|
|
553
|
-
text: formatToolImageBudgetNote(toolOmitted)
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
const hadPersistedImages = message?.tool_result_metadata?.[TOOL_HAD_IMAGES] === true;
|
|
557
|
-
const hasInjectedParts = outputParts.some((p) => p.type === "input_image") || toolOmitted > 0;
|
|
558
|
-
const shouldUseArray = hadPersistedImages || hasInjectedParts;
|
|
559
|
-
input.push({
|
|
560
|
-
type: "function_call_output",
|
|
561
|
-
call_id: message.tool_call_id,
|
|
562
|
-
output: shouldUseArray ? outputParts : normalizeToolOutput(message.content)
|
|
563
|
-
});
|
|
564
|
-
continue;
|
|
185
|
+
let tailArrayComma = null;
|
|
186
|
+
for (let d = stack.length;d >= 1; d -= 1) {
|
|
187
|
+
const c = commaAtDepth[d];
|
|
188
|
+
if (c && stack[d - 1] === "[") {
|
|
189
|
+
tailArrayComma = c;
|
|
190
|
+
break;
|
|
565
191
|
}
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
inString,
|
|
195
|
+
mismatched: false,
|
|
196
|
+
unclosed: stack.length > 0,
|
|
197
|
+
tailArrayComma
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function scanTruncation(input) {
|
|
201
|
+
const scan = scanRawToolArgs(input);
|
|
202
|
+
return {
|
|
203
|
+
inString: scan.inString,
|
|
204
|
+
malformed: scan.mismatched,
|
|
205
|
+
tailArrayComma: scan.tailArrayComma
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function classifyUnparsableToolArgs(raw) {
|
|
209
|
+
if (typeof raw !== "string")
|
|
210
|
+
return "malformed";
|
|
211
|
+
const scan = scanRawToolArgs(raw);
|
|
212
|
+
if (scan.inString)
|
|
213
|
+
return "truncated";
|
|
214
|
+
return scan.mismatched || scan.unclosed ? "truncated" : "malformed";
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// packages/agent-runtime/outboundHistorySanitize.ts
|
|
218
|
+
var DOWNGRADED_TOOL_TURN_ORIGIN = Symbol("nolo.downgradedToolTurnOrigin");
|
|
219
|
+
function markDowngradedToolTurnOrigin(message) {
|
|
220
|
+
Object.defineProperty(message, DOWNGRADED_TOOL_TURN_ORIGIN, {
|
|
221
|
+
value: true,
|
|
222
|
+
enumerable: false,
|
|
223
|
+
writable: false,
|
|
224
|
+
configurable: true
|
|
225
|
+
});
|
|
226
|
+
return message;
|
|
227
|
+
}
|
|
228
|
+
function transferDowngradedToolTurnOrigin(source, rebuilt) {
|
|
229
|
+
if (hasDowngradedToolTurnOrigin(source))
|
|
230
|
+
markDowngradedToolTurnOrigin(rebuilt);
|
|
231
|
+
return rebuilt;
|
|
232
|
+
}
|
|
233
|
+
function hasDowngradedToolTurnOrigin(message) {
|
|
234
|
+
return typeof message === "object" && message !== null && message[DOWNGRADED_TOOL_TURN_ORIGIN] === true;
|
|
235
|
+
}
|
|
236
|
+
function emptyToolArgsProjectionStats() {
|
|
237
|
+
return {
|
|
238
|
+
badArguments: { truncated: 0, malformed: 0 },
|
|
239
|
+
rewrittenToolCalls: 0,
|
|
240
|
+
rewrittenAssistantMessages: 0,
|
|
241
|
+
rewrittenToolMessages: 0
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function sanitizeForOutbound(messages, tools, options) {
|
|
245
|
+
return sanitizeOutboundHistory(messages, {
|
|
246
|
+
declaredToolNames: extractDeclaredToolNames(tools),
|
|
247
|
+
...options
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
function stableToolCallId(assistantIndex, callIndex) {
|
|
251
|
+
return `call_sanitize_${assistantIndex}_${callIndex}`;
|
|
252
|
+
}
|
|
253
|
+
function isString(v) {
|
|
254
|
+
return typeof v === "string";
|
|
255
|
+
}
|
|
256
|
+
function isNonEmptyString(v) {
|
|
257
|
+
return typeof v === "string" && v.length > 0;
|
|
258
|
+
}
|
|
259
|
+
function joinLines(...parts) {
|
|
260
|
+
return parts.filter(isNonEmptyString).join(`
|
|
261
|
+
`);
|
|
262
|
+
}
|
|
263
|
+
function jsonStringifyArguments(args) {
|
|
264
|
+
if (typeof args === "string")
|
|
265
|
+
return args;
|
|
266
|
+
try {
|
|
267
|
+
return JSON.stringify(args ?? "");
|
|
268
|
+
} catch {
|
|
269
|
+
return "";
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
function renderToolCallAsText(call) {
|
|
273
|
+
const c = call && typeof call === "object" ? call : {};
|
|
274
|
+
const name = isString(c.function?.name) && c.function.name.length > 0 ? c.function.name : "unknown";
|
|
275
|
+
const rawArgs = c.function?.arguments;
|
|
276
|
+
const args = jsonStringifyArguments(rawArgs);
|
|
277
|
+
return `[tool_call: ${name}(${args})]`;
|
|
278
|
+
}
|
|
279
|
+
function renderToolResultAsText(message) {
|
|
280
|
+
const name = isNonEmptyString(message.toolName) ? message.toolName : "tool";
|
|
281
|
+
const body = typeof message.content === "string" ? message.content : "";
|
|
282
|
+
return `[tool_result: ${name}: ${body}]`;
|
|
283
|
+
}
|
|
284
|
+
function normalizeToolCall(call, assistantIndex, callIndex) {
|
|
285
|
+
if (!call || typeof call !== "object")
|
|
286
|
+
return null;
|
|
287
|
+
const c = call;
|
|
288
|
+
const fn = c.function;
|
|
289
|
+
if (!fn || typeof fn !== "object" || !isString(fn.name) || fn.name.length === 0)
|
|
290
|
+
return null;
|
|
291
|
+
const id = isNonEmptyString(c.id) ? c.id : stableToolCallId(assistantIndex, callIndex);
|
|
292
|
+
return {
|
|
293
|
+
id,
|
|
294
|
+
type: isString(c.type) && c.type ? c.type : "function",
|
|
295
|
+
function: { name: fn.name, arguments: jsonStringifyArguments(fn.arguments) },
|
|
296
|
+
...typeof c.extra_content?.google?.thought_signature === "string" ? { extra_content: { google: { thought_signature: c.extra_content.google.thought_signature } } } : {},
|
|
297
|
+
...typeof c.thought_signature === "string" ? { thought_signature: c.thought_signature } : {}
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function classifyAssistantCalls(toolCalls, assistantIndex) {
|
|
301
|
+
const callIds = [];
|
|
302
|
+
const idCounts = new Map;
|
|
303
|
+
const unparsableIndices = new Set;
|
|
304
|
+
const unparsableIds = new Set;
|
|
305
|
+
toolCalls.forEach((rawCall, callIndex) => {
|
|
306
|
+
const c = rawCall && typeof rawCall === "object" ? rawCall : {};
|
|
307
|
+
const id = isNonEmptyString(c.id) ? c.id : stableToolCallId(assistantIndex, callIndex);
|
|
308
|
+
callIds.push(id);
|
|
309
|
+
idCounts.set(id, (idCounts.get(id) ?? 0) + 1);
|
|
310
|
+
if (!hasParsableObjectArguments(c.function?.arguments)) {
|
|
311
|
+
unparsableIndices.add(callIndex);
|
|
312
|
+
unparsableIds.add(id);
|
|
576
313
|
}
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
314
|
+
});
|
|
315
|
+
const duplicateIds = new Set;
|
|
316
|
+
for (const [id, count] of idCounts) {
|
|
317
|
+
if (count > 1)
|
|
318
|
+
duplicateIds.add(id);
|
|
319
|
+
}
|
|
320
|
+
return { callIds, duplicateIds, unparsableIndices, unparsableIds };
|
|
321
|
+
}
|
|
322
|
+
function sanitizeOutboundHistory(messages, options) {
|
|
323
|
+
if (!Array.isArray(messages) || messages.length === 0)
|
|
324
|
+
return messages;
|
|
325
|
+
messages = filterToolDiscoveryHistory(messages, options?.retainToolDiscovery);
|
|
326
|
+
const declared = options?.retainToolDiscovery && options.declaredToolNames ? new Set([...options.declaredToolNames, TOOL_DISCOVERY_CALL_NAME]) : options?.declaredToolNames;
|
|
327
|
+
const keptCallIndicesPerAssistant = new Map;
|
|
328
|
+
const keptToolIndices = new Set;
|
|
329
|
+
let i = 0;
|
|
330
|
+
while (i < messages.length) {
|
|
331
|
+
const m = messages[i];
|
|
332
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
333
|
+
const asstIdx = i;
|
|
334
|
+
const { callIds, duplicateIds, unparsableIndices } = classifyAssistantCalls(m.tool_calls, asstIdx);
|
|
335
|
+
const candidateCallMap = new Map;
|
|
336
|
+
m.tool_calls.forEach((call, callIndex) => {
|
|
337
|
+
const id = callIds[callIndex];
|
|
338
|
+
if (duplicateIds.has(id))
|
|
339
|
+
return;
|
|
340
|
+
if (unparsableIndices.has(callIndex))
|
|
341
|
+
return;
|
|
342
|
+
const name = call?.function?.name;
|
|
343
|
+
if (!isString(name) || name.length === 0)
|
|
344
|
+
return;
|
|
345
|
+
const declaredAllows = !declared || declared.has(name);
|
|
346
|
+
if (!declaredAllows)
|
|
347
|
+
return;
|
|
348
|
+
candidateCallMap.set(id, callIndex);
|
|
583
349
|
});
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
const
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
role: "user",
|
|
597
|
-
content: replayImages
|
|
598
|
-
});
|
|
350
|
+
let j = i + 1;
|
|
351
|
+
const matchedCallIndices = new Set;
|
|
352
|
+
const matchedToolIds = new Set;
|
|
353
|
+
while (j < messages.length && messages[j].role === "tool") {
|
|
354
|
+
const toolMsg = messages[j];
|
|
355
|
+
const toolCallId = isNonEmptyString(toolMsg.tool_call_id) ? toolMsg.tool_call_id : "";
|
|
356
|
+
if (toolCallId && candidateCallMap.has(toolCallId) && !matchedToolIds.has(toolCallId)) {
|
|
357
|
+
matchedToolIds.add(toolCallId);
|
|
358
|
+
matchedCallIndices.add(candidateCallMap.get(toolCallId));
|
|
359
|
+
keptToolIndices.add(j);
|
|
360
|
+
}
|
|
361
|
+
j++;
|
|
599
362
|
}
|
|
363
|
+
keptCallIndicesPerAssistant.set(asstIdx, matchedCallIndices);
|
|
364
|
+
i = j;
|
|
365
|
+
} else {
|
|
366
|
+
i++;
|
|
600
367
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
368
|
+
}
|
|
369
|
+
const out = [];
|
|
370
|
+
let k = 0;
|
|
371
|
+
while (k < messages.length) {
|
|
372
|
+
const m = messages[k];
|
|
373
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
374
|
+
const asstIdx = k;
|
|
375
|
+
const keptIndices = keptCallIndicesPerAssistant.get(asstIdx) ?? new Set;
|
|
376
|
+
const keptToolCalls = [];
|
|
377
|
+
const downgradedCallLines = [];
|
|
378
|
+
m.tool_calls.forEach((call, callIndex) => {
|
|
379
|
+
const name = call?.function?.name;
|
|
380
|
+
const declaredAllows = !declared || isString(name) && declared.has(name);
|
|
381
|
+
const keep = keptIndices.has(callIndex) && declaredAllows && hasParsableObjectArguments(call?.function?.arguments);
|
|
382
|
+
if (keep) {
|
|
383
|
+
const normalized = normalizeToolCall(call, asstIdx, callIndex);
|
|
384
|
+
if (normalized) {
|
|
385
|
+
keptToolCalls.push(normalized);
|
|
386
|
+
} else {
|
|
387
|
+
downgradedCallLines.push(renderToolCallAsText(call));
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
downgradedCallLines.push(renderToolCallAsText(call));
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
if (keptToolCalls.length === 0) {
|
|
394
|
+
const downgradedText = joinLines(...downgradedCallLines);
|
|
395
|
+
const contentWithDowngrade = downgradedText.length > 0 ? combineContentWithText(m.content, downgradedText) : m.content;
|
|
396
|
+
const sanitizedAssistant = transferDowngradedToolTurnOrigin(m, {
|
|
397
|
+
...m,
|
|
398
|
+
content: contentWithDowngrade
|
|
609
399
|
});
|
|
400
|
+
delete sanitizedAssistant.tool_calls;
|
|
401
|
+
markDowngradedToolTurnOrigin(sanitizedAssistant);
|
|
402
|
+
out.push(sanitizedAssistant);
|
|
403
|
+
let nextIdx = k + 1;
|
|
404
|
+
while (nextIdx < messages.length && messages[nextIdx].role === "tool") {
|
|
405
|
+
const downgradedToolMsg = messages[nextIdx];
|
|
406
|
+
out.push(transferDowngradedToolTurnOrigin(downgradedToolMsg, {
|
|
407
|
+
role: "assistant",
|
|
408
|
+
content: renderToolResultAsText(downgradedToolMsg)
|
|
409
|
+
}));
|
|
410
|
+
nextIdx++;
|
|
411
|
+
}
|
|
412
|
+
k = nextIdx;
|
|
413
|
+
continue;
|
|
610
414
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
415
|
+
const sanitizedAssistant = transferDowngradedToolTurnOrigin(m, {
|
|
416
|
+
...m,
|
|
417
|
+
tool_calls: keptToolCalls
|
|
418
|
+
});
|
|
419
|
+
out.push(sanitizedAssistant);
|
|
420
|
+
let nextIdx = k + 1;
|
|
421
|
+
const deferredResultLines = [];
|
|
422
|
+
while (nextIdx < messages.length && messages[nextIdx].role === "tool") {
|
|
423
|
+
if (keptToolIndices.has(nextIdx)) {
|
|
424
|
+
const toolMsg = messages[nextIdx];
|
|
425
|
+
out.push({
|
|
426
|
+
...toolMsg,
|
|
427
|
+
content: toolMsg.content ?? "",
|
|
428
|
+
tool_call_id: toolMsg.tool_call_id
|
|
429
|
+
});
|
|
430
|
+
} else {
|
|
431
|
+
deferredResultLines.push(renderToolResultAsText(messages[nextIdx]));
|
|
432
|
+
}
|
|
433
|
+
nextIdx++;
|
|
622
434
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const parts = [];
|
|
629
|
-
for (const item of response?.output ?? []) {
|
|
630
|
-
if (item?.type !== "reasoning") continue;
|
|
631
|
-
if (typeof item.content === "string") {
|
|
632
|
-
parts.push(item.content);
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
for (const content of item.content ?? []) {
|
|
636
|
-
if (typeof content === "string") parts.push(content);
|
|
637
|
-
else if (typeof content?.text === "string") parts.push(content.text);
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
return parts.join("");
|
|
641
|
-
};
|
|
642
|
-
var toDataUrl = (base64Data, mimeType) => {
|
|
643
|
-
const normalizedMimeType = asOptionalTrimmedString(mimeType) ?? "image/png";
|
|
644
|
-
return `data:${normalizedMimeType};base64,${base64Data}`;
|
|
645
|
-
};
|
|
646
|
-
var extractImagePartsFromResponseOutput = (response) => {
|
|
647
|
-
const images = [];
|
|
648
|
-
for (const item of response?.output ?? []) {
|
|
649
|
-
if (item?.type === "image_generation_call") {
|
|
650
|
-
const result = asOptionalTrimmedString(item.result);
|
|
651
|
-
if (result) {
|
|
652
|
-
const outputFormat = asOptionalTrimmedString(item.output_format);
|
|
653
|
-
images.push({
|
|
654
|
-
type: "image_url",
|
|
655
|
-
image_url: {
|
|
656
|
-
url: toDataUrl(
|
|
657
|
-
result,
|
|
658
|
-
outputFormat ? `image/${outputFormat}` : void 0
|
|
659
|
-
)
|
|
660
|
-
}
|
|
435
|
+
const deferredLines = [...downgradedCallLines, ...deferredResultLines];
|
|
436
|
+
if (deferredLines.length > 0) {
|
|
437
|
+
out.push({
|
|
438
|
+
role: "assistant",
|
|
439
|
+
content: joinLines(...deferredLines)
|
|
661
440
|
});
|
|
662
441
|
}
|
|
442
|
+
k = nextIdx;
|
|
663
443
|
continue;
|
|
664
444
|
}
|
|
665
|
-
if (
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
url: toDataUrl(
|
|
673
|
-
result,
|
|
674
|
-
content.mime_type ?? content.mimeType ?? null
|
|
675
|
-
)
|
|
676
|
-
}
|
|
677
|
-
});
|
|
678
|
-
}
|
|
445
|
+
if (m.role === "tool") {
|
|
446
|
+
out.push(transferDowngradedToolTurnOrigin(m, {
|
|
447
|
+
role: "assistant",
|
|
448
|
+
content: renderToolResultAsText(m)
|
|
449
|
+
}));
|
|
450
|
+
k++;
|
|
451
|
+
continue;
|
|
679
452
|
}
|
|
453
|
+
out.push(transferDowngradedToolTurnOrigin(m, { ...m }));
|
|
454
|
+
k++;
|
|
680
455
|
}
|
|
681
|
-
return
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
for (const item of response?.output ?? []) {
|
|
688
|
-
const isSearch = item?.type === "tool_search_call" && item.execution === "client";
|
|
689
|
-
if (!isSearch && item?.type !== "function_call") continue;
|
|
690
|
-
if (options?.strictSnapshot && (!asOptionalTrimmedString(item.call_id) || !isSearch && !asOptionalTrimmedString(item.name))) {
|
|
691
|
-
throw new Error(`Responses completed snapshot contains a ${item.type} without a valid call_id or name.`);
|
|
692
|
-
}
|
|
693
|
-
let call;
|
|
694
|
-
if (isSearch) {
|
|
695
|
-
call = bridgeResponsesToolSearch(item);
|
|
696
|
-
} else {
|
|
697
|
-
if (!item.call_id || !item.name) continue;
|
|
698
|
-
if (item.name === TOOL_DISCOVERY_CALL_NAME) {
|
|
699
|
-
throw new Error("Responses used a reserved tool discovery name for a function call.");
|
|
700
|
-
}
|
|
701
|
-
call = {
|
|
702
|
-
id: item.call_id,
|
|
703
|
-
type: "function",
|
|
704
|
-
function: {
|
|
705
|
-
name: item.name,
|
|
706
|
-
arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments ?? {})
|
|
707
|
-
}
|
|
708
|
-
};
|
|
709
|
-
}
|
|
710
|
-
if (options?.strictSnapshot) {
|
|
711
|
-
const previous = wireCalls.get(item.call_id);
|
|
712
|
-
if (previous) {
|
|
713
|
-
if (previous.function.name === call.function.name && previous.function.arguments === call.function.arguments) continue;
|
|
714
|
-
throw new Error("Responses returned a conflicting duplicate wire tool call ID.");
|
|
715
|
-
}
|
|
716
|
-
if (projectedIds.has(call.id)) throw new Error("Responses returned a conflicting duplicate projected tool call ID.");
|
|
717
|
-
wireCalls.set(item.call_id, call);
|
|
718
|
-
projectedIds.add(call.id);
|
|
719
|
-
}
|
|
720
|
-
calls.push(call);
|
|
456
|
+
return out;
|
|
457
|
+
}
|
|
458
|
+
function combineContentWithText(base, extra) {
|
|
459
|
+
if (typeof base === "string") {
|
|
460
|
+
return base ? `${base}
|
|
461
|
+
${extra}` : extra;
|
|
721
462
|
}
|
|
722
|
-
if (
|
|
723
|
-
|
|
724
|
-
for (const call of calls) {
|
|
725
|
-
const first = firstById.get(call.id);
|
|
726
|
-
if (!first) firstById.set(call.id, call);
|
|
727
|
-
else if (first.function.name !== call.function.name || first.function.arguments !== call.function.arguments) {
|
|
728
|
-
throw new Error("Responses returned a conflicting duplicate tool call ID.");
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
return [...firstById.values()];
|
|
463
|
+
if (Array.isArray(base)) {
|
|
464
|
+
return [...base, { type: "text", text: extra }];
|
|
732
465
|
}
|
|
733
|
-
return
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
466
|
+
return extra;
|
|
467
|
+
}
|
|
468
|
+
function hasParsableObjectArguments(raw) {
|
|
469
|
+
if (raw === undefined)
|
|
470
|
+
return true;
|
|
471
|
+
if (raw === null || typeof raw === "number" || typeof raw === "boolean") {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
if (typeof raw !== "string")
|
|
475
|
+
return true;
|
|
476
|
+
if (raw.trim() === "")
|
|
477
|
+
return true;
|
|
478
|
+
try {
|
|
479
|
+
const parsed = JSON.parse(raw);
|
|
480
|
+
return parsed !== null && typeof parsed === "object";
|
|
481
|
+
} catch {
|
|
482
|
+
return false;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
var STRICT_JSON_STRING_TOKEN = /^"(?:[^"\\\u0000-\u001F]|\\["\\/bfnrt]|\\u[0-9A-Fa-f]{4})*"$/;
|
|
486
|
+
function isStrictJsonStringToken(value) {
|
|
487
|
+
if (!STRICT_JSON_STRING_TOKEN.test(value))
|
|
488
|
+
return false;
|
|
489
|
+
try {
|
|
490
|
+
return typeof JSON.parse(value) === "string";
|
|
491
|
+
} catch {
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function closersOf(openStack) {
|
|
496
|
+
return openStack.slice().reverse().map((ch) => ch === "{" ? "}" : "]").join("");
|
|
497
|
+
}
|
|
498
|
+
function scanJsonForTailRepair(input) {
|
|
738
499
|
const stack = [];
|
|
739
|
-
const commaAtDepth = [];
|
|
740
500
|
let inString = false;
|
|
741
501
|
let escaped = false;
|
|
742
|
-
|
|
743
|
-
for (let i = 0;
|
|
502
|
+
let lastCommaOutsideString = null;
|
|
503
|
+
for (let i = 0;i < input.length; i += 1) {
|
|
744
504
|
const ch = input[i];
|
|
745
505
|
if (inString) {
|
|
746
|
-
if (escaped)
|
|
747
|
-
|
|
748
|
-
else if (ch ===
|
|
506
|
+
if (escaped)
|
|
507
|
+
escaped = false;
|
|
508
|
+
else if (ch === "\\")
|
|
509
|
+
escaped = true;
|
|
510
|
+
else if (ch === '"')
|
|
511
|
+
inString = false;
|
|
749
512
|
continue;
|
|
750
513
|
}
|
|
751
514
|
if (ch === '"') {
|
|
752
515
|
inString = true;
|
|
753
|
-
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (ch === "{" || ch === "[") {
|
|
754
519
|
stack.push(ch);
|
|
755
|
-
|
|
756
|
-
}
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
if (ch === "}" || ch === "]") {
|
|
757
523
|
const open = stack.pop();
|
|
758
|
-
if (open !== (ch === "}" ? "{" : "["))
|
|
759
|
-
return
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
524
|
+
if (open !== (ch === "}" ? "{" : "["))
|
|
525
|
+
return null;
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
if (ch === ",") {
|
|
529
|
+
lastCommaOutsideString = { index: i, closers: closersOf(stack) };
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return { inString, closers: closersOf(stack), lastCommaOutsideString };
|
|
534
|
+
}
|
|
535
|
+
function repairTruncatedToolArguments(raw) {
|
|
536
|
+
if (typeof raw !== "string")
|
|
537
|
+
return null;
|
|
538
|
+
const withoutTrailingSpace = raw.replace(/\s+$/, "");
|
|
539
|
+
const endsWithComma = withoutTrailingSpace.endsWith(",");
|
|
540
|
+
const trimmed = endsWithComma ? withoutTrailingSpace.slice(0, -1).replace(/\s+$/, "") : withoutTrailingSpace;
|
|
541
|
+
if (trimmed === "" || trimmed.endsWith(","))
|
|
542
|
+
return null;
|
|
543
|
+
const scan = scanJsonForTailRepair(trimmed);
|
|
544
|
+
if (!scan || scan.inString)
|
|
545
|
+
return null;
|
|
546
|
+
const lastChar = trimmed[trimmed.length - 1];
|
|
547
|
+
const tailIsCompleteToken = endsWithComma || lastChar === '"' || lastChar === "}" || lastChar === "]";
|
|
548
|
+
if (!tailIsCompleteToken)
|
|
549
|
+
return null;
|
|
550
|
+
const candidates = [trimmed + scan.closers];
|
|
551
|
+
const lastComma = scan.lastCommaOutsideString;
|
|
552
|
+
if (lastComma && lastComma.index > 0) {
|
|
553
|
+
const tailAfterComma = trimmed.slice(lastComma.index + 1).trim();
|
|
554
|
+
if (isStrictJsonStringToken(tailAfterComma)) {
|
|
555
|
+
const head = trimmed.slice(0, lastComma.index);
|
|
556
|
+
candidates.push(head + lastComma.closers);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
for (const candidate of candidates) {
|
|
560
|
+
try {
|
|
561
|
+
const parsed = JSON.parse(candidate);
|
|
562
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed) && Object.keys(parsed).length > 0) {
|
|
563
|
+
return candidate;
|
|
564
|
+
}
|
|
565
|
+
} catch {}
|
|
566
|
+
}
|
|
567
|
+
return null;
|
|
568
|
+
}
|
|
569
|
+
function downgradeUnparsableToolCalls(messages) {
|
|
570
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
571
|
+
return { messages, downgraded: 0, stats: emptyToolArgsProjectionStats() };
|
|
572
|
+
}
|
|
573
|
+
let hasAnyPoison = false;
|
|
574
|
+
for (const m of messages) {
|
|
575
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls)) {
|
|
576
|
+
for (const call of m.tool_calls) {
|
|
577
|
+
if (!hasParsableObjectArguments(call?.function?.arguments)) {
|
|
578
|
+
hasAnyPoison = true;
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (hasAnyPoison)
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
if (!hasAnyPoison)
|
|
587
|
+
return { messages, downgraded: 0, stats: emptyToolArgsProjectionStats() };
|
|
588
|
+
const poisonedCallsPerAssistant = new Map;
|
|
589
|
+
const poisonedToolIndices = new Set;
|
|
590
|
+
const badArguments = {
|
|
591
|
+
truncated: 0,
|
|
592
|
+
malformed: 0
|
|
593
|
+
};
|
|
594
|
+
let i = 0;
|
|
595
|
+
while (i < messages.length) {
|
|
596
|
+
const m = messages[i];
|
|
597
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
598
|
+
const asstIdx = i;
|
|
599
|
+
const { unparsableIds, unparsableIndices } = classifyAssistantCalls(m.tool_calls, asstIdx);
|
|
600
|
+
for (const badIndex of unparsableIndices) {
|
|
601
|
+
const badCall = m.tool_calls[badIndex];
|
|
602
|
+
badArguments[classifyUnparsableToolArgs(badCall?.function?.arguments)] += 1;
|
|
603
|
+
}
|
|
604
|
+
let j = i + 1;
|
|
605
|
+
while (j < messages.length && messages[j].role === "tool") {
|
|
606
|
+
const toolMsg = messages[j];
|
|
607
|
+
const toolCallId = isNonEmptyString(toolMsg.tool_call_id) ? toolMsg.tool_call_id : "";
|
|
608
|
+
if (toolCallId && unparsableIds.has(toolCallId)) {
|
|
609
|
+
poisonedToolIndices.add(j);
|
|
610
|
+
}
|
|
611
|
+
j++;
|
|
612
|
+
}
|
|
613
|
+
if (unparsableIds.size > 0) {
|
|
614
|
+
poisonedCallsPerAssistant.set(asstIdx, unparsableIds);
|
|
615
|
+
}
|
|
616
|
+
i = j;
|
|
617
|
+
} else {
|
|
618
|
+
i++;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
let downgraded = 0;
|
|
622
|
+
let rewrittenAssistantMessages = 0;
|
|
623
|
+
let rewrittenToolMessages = 0;
|
|
624
|
+
const out = [];
|
|
625
|
+
let k = 0;
|
|
626
|
+
while (k < messages.length) {
|
|
627
|
+
const m = messages[k];
|
|
628
|
+
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
629
|
+
const badIds = poisonedCallsPerAssistant.get(k);
|
|
630
|
+
if (!badIds || badIds.size === 0) {
|
|
631
|
+
out.push(m);
|
|
632
|
+
k++;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
const kept = [];
|
|
636
|
+
const lines = [];
|
|
637
|
+
const { callIds } = classifyAssistantCalls(m.tool_calls, k);
|
|
638
|
+
rewrittenAssistantMessages += 1;
|
|
639
|
+
m.tool_calls.forEach((call, callIndex) => {
|
|
640
|
+
const id = callIds[callIndex];
|
|
641
|
+
if (badIds.has(id)) {
|
|
642
|
+
lines.push(renderToolCallAsText(call));
|
|
643
|
+
downgraded++;
|
|
644
|
+
} else {
|
|
645
|
+
kept.push(call);
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
if (kept.length === 0) {
|
|
649
|
+
const sanitized = transferDowngradedToolTurnOrigin(m, {
|
|
650
|
+
...m,
|
|
651
|
+
content: combineContentWithText(m.content, joinLines(...lines))
|
|
652
|
+
});
|
|
653
|
+
delete sanitized.tool_calls;
|
|
654
|
+
markDowngradedToolTurnOrigin(sanitized);
|
|
655
|
+
out.push(sanitized);
|
|
656
|
+
let nextIdx = k + 1;
|
|
657
|
+
while (nextIdx < messages.length && messages[nextIdx].role === "tool") {
|
|
658
|
+
if (poisonedToolIndices.has(nextIdx)) {
|
|
659
|
+
const poisonedToolMsg = messages[nextIdx];
|
|
660
|
+
out.push(transferDowngradedToolTurnOrigin(poisonedToolMsg, {
|
|
661
|
+
role: "assistant",
|
|
662
|
+
content: renderToolResultAsText(poisonedToolMsg)
|
|
663
|
+
}));
|
|
664
|
+
rewrittenToolMessages += 1;
|
|
665
|
+
} else {
|
|
666
|
+
out.push(messages[nextIdx]);
|
|
667
|
+
}
|
|
668
|
+
nextIdx++;
|
|
669
|
+
}
|
|
670
|
+
k = nextIdx;
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
const sanitized = transferDowngradedToolTurnOrigin(m, {
|
|
674
|
+
...m,
|
|
675
|
+
content: lines.length > 0 ? combineContentWithText(m.content, joinLines(...lines)) : m.content,
|
|
676
|
+
tool_calls: kept
|
|
677
|
+
});
|
|
678
|
+
out.push(sanitized);
|
|
679
|
+
let nextIdx = k + 1;
|
|
680
|
+
const deferredResultLines = [];
|
|
681
|
+
while (nextIdx < messages.length && messages[nextIdx].role === "tool") {
|
|
682
|
+
if (poisonedToolIndices.has(nextIdx)) {
|
|
683
|
+
deferredResultLines.push(renderToolResultAsText(messages[nextIdx]));
|
|
684
|
+
rewrittenToolMessages += 1;
|
|
685
|
+
} else {
|
|
686
|
+
out.push(messages[nextIdx]);
|
|
687
|
+
}
|
|
688
|
+
nextIdx++;
|
|
766
689
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
690
|
+
if (deferredResultLines.length > 0) {
|
|
691
|
+
out.push({
|
|
692
|
+
role: "assistant",
|
|
693
|
+
content: joinLines(...deferredResultLines)
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
k = nextIdx;
|
|
697
|
+
continue;
|
|
770
698
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
699
|
+
if (m.role === "tool") {
|
|
700
|
+
if (poisonedToolIndices.has(k)) {
|
|
701
|
+
out.push(transferDowngradedToolTurnOrigin(m, {
|
|
702
|
+
role: "assistant",
|
|
703
|
+
content: renderToolResultAsText(m)
|
|
704
|
+
}));
|
|
705
|
+
rewrittenToolMessages += 1;
|
|
706
|
+
} else {
|
|
707
|
+
out.push(m);
|
|
708
|
+
}
|
|
709
|
+
k++;
|
|
710
|
+
continue;
|
|
778
711
|
}
|
|
712
|
+
out.push(m);
|
|
713
|
+
k++;
|
|
779
714
|
}
|
|
780
715
|
return {
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
716
|
+
messages: out,
|
|
717
|
+
downgraded,
|
|
718
|
+
stats: {
|
|
719
|
+
badArguments,
|
|
720
|
+
rewrittenToolCalls: downgraded,
|
|
721
|
+
rewrittenAssistantMessages,
|
|
722
|
+
rewrittenToolMessages
|
|
723
|
+
}
|
|
785
724
|
};
|
|
786
725
|
}
|
|
787
|
-
|
|
788
|
-
|
|
726
|
+
|
|
727
|
+
// packages/core/ollamaEndpoint.ts
|
|
728
|
+
function isOllamaEndpoint(endpoint) {
|
|
729
|
+
if (!endpoint || typeof endpoint !== "string")
|
|
730
|
+
return false;
|
|
731
|
+
try {
|
|
732
|
+
const url = new URL(endpoint.includes("://") ? endpoint : `http://${endpoint}`);
|
|
733
|
+
if (url.port === "11434")
|
|
734
|
+
return true;
|
|
735
|
+
const host = url.hostname.toLowerCase();
|
|
736
|
+
return host === "ollama.com" || host.endsWith(".ollama.com") || host === "ollama.ai" || host.endsWith(".ollama.ai");
|
|
737
|
+
} catch {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// packages/ai/llm/usageRequestOptions.ts
|
|
743
|
+
var STREAM_USAGE_PROVIDERS = new Set([
|
|
744
|
+
"google",
|
|
745
|
+
"openrouter",
|
|
746
|
+
"xai",
|
|
747
|
+
"openai",
|
|
748
|
+
"fireworks",
|
|
749
|
+
"mistral",
|
|
750
|
+
"cloudflare",
|
|
751
|
+
"gmi",
|
|
752
|
+
"deepinfra",
|
|
753
|
+
"vultr",
|
|
754
|
+
"deepseek",
|
|
755
|
+
"ollama",
|
|
756
|
+
"ollama-cloud",
|
|
757
|
+
"runinfra",
|
|
758
|
+
"baseten",
|
|
759
|
+
"kimi-code",
|
|
760
|
+
"minimax",
|
|
761
|
+
"opendesign",
|
|
762
|
+
"opencode",
|
|
763
|
+
"opencode-go",
|
|
764
|
+
"longcat"
|
|
765
|
+
]);
|
|
766
|
+
var KIMI_CODE_ENDPOINT_MARKER = "api.kimi.com";
|
|
767
|
+
function extractHostname(endpoint) {
|
|
768
|
+
try {
|
|
769
|
+
const raw = endpoint.includes("://") ? endpoint : `https://${endpoint}`;
|
|
770
|
+
return new URL(raw).hostname.toLowerCase();
|
|
771
|
+
} catch {
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
var isMiniMaxEndpoint = (endpoint) => {
|
|
776
|
+
const host = extractHostname(endpoint);
|
|
777
|
+
return host === "api.minimaxi.com" || host === "api.minimax.chat";
|
|
778
|
+
};
|
|
779
|
+
var isLongCatEndpoint = (endpoint) => {
|
|
780
|
+
const host = extractHostname(endpoint);
|
|
781
|
+
return host === "api.longcat.chat" || host === "api.longcat.ai";
|
|
782
|
+
};
|
|
783
|
+
var isSupportedCustomEndpoint = (endpoint) => {
|
|
784
|
+
if (typeof endpoint !== "string")
|
|
785
|
+
return false;
|
|
786
|
+
const ep = endpoint.toLowerCase();
|
|
787
|
+
return isMiniMaxEndpoint(endpoint) || isLongCatEndpoint(endpoint) || ep.includes(KIMI_CODE_ENDPOINT_MARKER) || ep.includes("open-design.ai") || ep.includes("opencode.ai") || isOllamaEndpoint(endpoint);
|
|
788
|
+
};
|
|
789
|
+
var EXTRA_USAGE_FIELD_PROVIDERS = new Set(["openrouter"]);
|
|
790
|
+
var normalizeProviderName = (providerName) => asTrimmedLowercaseString(providerName);
|
|
791
|
+
var getUsageRequestOptions = (providerName, options) => {
|
|
792
|
+
const normalizedProvider = normalizeProviderName(providerName);
|
|
793
|
+
const api = options?.api ?? "chat-completions";
|
|
794
|
+
const shouldRequestStreamUsage = STREAM_USAGE_PROVIDERS.has(normalizedProvider) || normalizedProvider === "custom" && isSupportedCustomEndpoint(options?.endpoint);
|
|
795
|
+
if (api === "responses") {
|
|
796
|
+
return EXTRA_USAGE_FIELD_PROVIDERS.has(normalizedProvider) ? {
|
|
797
|
+
usage: {
|
|
798
|
+
include: true
|
|
799
|
+
}
|
|
800
|
+
} : {};
|
|
801
|
+
}
|
|
789
802
|
return {
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
803
|
+
...shouldRequestStreamUsage ? {
|
|
804
|
+
stream_options: {
|
|
805
|
+
include_usage: true
|
|
806
|
+
}
|
|
807
|
+
} : {},
|
|
808
|
+
...EXTRA_USAGE_FIELD_PROVIDERS.has(normalizedProvider) ? {
|
|
809
|
+
usage: {
|
|
810
|
+
include: true
|
|
811
|
+
}
|
|
812
|
+
} : {}
|
|
793
813
|
};
|
|
814
|
+
};
|
|
815
|
+
|
|
816
|
+
// packages/core/chat/imageParts.ts
|
|
817
|
+
function isImageUrlPart(part) {
|
|
818
|
+
return isRecord(part) && part.type === "image_url";
|
|
794
819
|
}
|
|
795
|
-
function
|
|
796
|
-
|
|
797
|
-
const scan = scanRawToolArgs(raw);
|
|
798
|
-
if (scan.inString) return "truncated";
|
|
799
|
-
return scan.mismatched || scan.unclosed ? "truncated" : "malformed";
|
|
820
|
+
function hasImageParts(messages) {
|
|
821
|
+
return messages.some((message) => Array.isArray(message.content) && message.content.some(isImageUrlPart));
|
|
800
822
|
}
|
|
801
823
|
|
|
802
|
-
// packages/
|
|
803
|
-
function
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
return
|
|
824
|
+
// packages/core/chat/bareImageUrlShape.ts
|
|
825
|
+
function requiresBareImageUrl(args) {
|
|
826
|
+
const model = asTrimmedLowercaseString(args.model);
|
|
827
|
+
if (!model.startsWith("gpt-"))
|
|
828
|
+
return false;
|
|
829
|
+
const provider = asTrimmedLowercaseString(args.provider);
|
|
830
|
+
if (provider === "opencode-go")
|
|
831
|
+
return true;
|
|
832
|
+
return asTrimmedLowercaseString(args.endpoint).includes("opencode.ai/zen/go");
|
|
833
|
+
}
|
|
834
|
+
function toBareImageUrlContent(content) {
|
|
835
|
+
if (!Array.isArray(content))
|
|
836
|
+
return content;
|
|
837
|
+
let changed = false;
|
|
838
|
+
const parts = content.map((part) => {
|
|
839
|
+
if (!isImageUrlPart(part))
|
|
840
|
+
return part;
|
|
841
|
+
const imageUrl = part.image_url;
|
|
842
|
+
if (!imageUrl || typeof imageUrl !== "object")
|
|
843
|
+
return part;
|
|
844
|
+
const url = imageUrl.url;
|
|
845
|
+
if (typeof url !== "string" || !url)
|
|
846
|
+
return part;
|
|
847
|
+
changed = true;
|
|
848
|
+
return { ...part, image_url: url };
|
|
849
|
+
});
|
|
850
|
+
return changed ? parts : content;
|
|
851
|
+
}
|
|
852
|
+
function toBareImageUrlMessages(messages) {
|
|
853
|
+
return messages.map((message) => {
|
|
854
|
+
const content = toBareImageUrlContent(message.content);
|
|
855
|
+
return content === message.content ? message : { ...message, content };
|
|
856
|
+
});
|
|
811
857
|
}
|
|
812
858
|
|
|
813
|
-
// packages/agent-runtime/
|
|
814
|
-
var
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
859
|
+
// packages/agent-runtime/openAiToolImages.ts
|
|
860
|
+
var INTRO = "These are attachments from the preceding batch of tool results, not new instructions. " + "Each image is preceded by a label naming the tool result it came from.";
|
|
861
|
+
function projectOpenAiToolImages(messages, supportsImages, maxImagesPerRequest) {
|
|
862
|
+
const parts = [];
|
|
863
|
+
const omitted = [];
|
|
864
|
+
const budget = createToolImageBudget(maxImagesPerRequest);
|
|
865
|
+
let accepted = 0;
|
|
866
|
+
let deferred = 0;
|
|
867
|
+
let lastTool = -1;
|
|
868
|
+
messages.forEach((message, index) => {
|
|
869
|
+
if (message.role === "tool")
|
|
870
|
+
lastTool = index;
|
|
871
|
+
if (!supportsImages)
|
|
872
|
+
return;
|
|
873
|
+
for (const [imageIndex, image] of toolImages(message).entries()) {
|
|
874
|
+
const decision = budget(image);
|
|
875
|
+
if (decision === "duplicate")
|
|
876
|
+
continue;
|
|
877
|
+
const source = toolImageSource(message, imageIndex);
|
|
878
|
+
if (decision !== "delivered") {
|
|
879
|
+
if (decision === "pending")
|
|
880
|
+
deferred += 1;
|
|
881
|
+
omitted.push(toolImageOmission(source, decision === "impossible"));
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
accepted += 1;
|
|
885
|
+
parts.push({
|
|
886
|
+
type: "text",
|
|
887
|
+
text: `Image ${accepted} — from tool result ${source}`
|
|
888
|
+
});
|
|
889
|
+
parts.push({ type: "image_url", image_url: { url: `data:${image.mimeType};base64,${image.dataBase64}` } });
|
|
890
|
+
}
|
|
823
891
|
});
|
|
824
|
-
|
|
892
|
+
if (lastTool < 0)
|
|
893
|
+
return messages;
|
|
894
|
+
if (!parts.length && !omitted.length)
|
|
895
|
+
return messages;
|
|
896
|
+
const note = omitted.length ? ` Note: ${omitted.length} image(s) omitted. ${omitted.join(" ")}` : "";
|
|
897
|
+
const attachment = {
|
|
898
|
+
role: "user",
|
|
899
|
+
content: [{ type: "text", text: `${INTRO}${deferred ? ` 本次投递 ${accepted} 张,另有 ${deferred} 张未投递(${deferred} 张 deferred to a later round)。` : ""}${note}` }, ...parts]
|
|
900
|
+
};
|
|
901
|
+
return [...messages.slice(0, lastTool + 1), attachment, ...messages.slice(lastTool + 1)];
|
|
825
902
|
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
903
|
+
|
|
904
|
+
// packages/agent-runtime/openAiCompatibleMessages.ts
|
|
905
|
+
function isGeminiCompatibleTarget(provider, model) {
|
|
906
|
+
const p = (provider ?? "").trim().toLowerCase();
|
|
907
|
+
const m = (model ?? "").trim().toLowerCase();
|
|
908
|
+
return p === "google" || p === "google-antigravity" || p.startsWith("google-") || p.includes("gemini") || m.includes("gemini");
|
|
829
909
|
}
|
|
830
|
-
function
|
|
831
|
-
|
|
910
|
+
function preserveAgentStateFields(source, target, options) {
|
|
911
|
+
const mutableTarget = target;
|
|
912
|
+
if (source.role === "assistant") {
|
|
913
|
+
const shapingCounts = options?.shapingCounts;
|
|
914
|
+
if (!options?.stripReasoningContent && typeof source.reasoning_content === "string") {
|
|
915
|
+
mutableTarget.reasoning_content = source.reasoning_content;
|
|
916
|
+
}
|
|
917
|
+
if (Array.isArray(source.tool_calls)) {
|
|
918
|
+
if (shapingCounts && source.tool_calls.length > 0) {
|
|
919
|
+
shapingCounts.assistantToolTurns += 1;
|
|
920
|
+
const hadUsableReasoning = typeof source.reasoning_content === "string" && source.reasoning_content.length > 0;
|
|
921
|
+
if (options?.stripReasoningContent) {
|
|
922
|
+
if (hadUsableReasoning)
|
|
923
|
+
shapingCounts.reasoningStrippedTurns += 1;
|
|
924
|
+
else
|
|
925
|
+
shapingCounts.missingReasoningTurns += 1;
|
|
926
|
+
} else if (!hadUsableReasoning) {
|
|
927
|
+
shapingCounts.missingReasoningTurns += 1;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
const isExplicitTarget = Boolean(options?.targetProvider || options?.targetModel);
|
|
931
|
+
const allowSignature = options?.allowThoughtSignature ?? (isExplicitTarget ? isGeminiCompatibleTarget(options?.targetProvider, options?.targetModel) : true);
|
|
932
|
+
mutableTarget.tool_calls = source.tool_calls.map((call) => {
|
|
933
|
+
if (!call || typeof call !== "object")
|
|
934
|
+
return call;
|
|
935
|
+
const cleaned = {
|
|
936
|
+
id: call.id,
|
|
937
|
+
type: call.type ?? "function",
|
|
938
|
+
function: call.function
|
|
939
|
+
};
|
|
940
|
+
if (allowSignature) {
|
|
941
|
+
if (call.extra_content !== undefined)
|
|
942
|
+
cleaned.extra_content = call.extra_content;
|
|
943
|
+
if (call.thought_signature !== undefined)
|
|
944
|
+
cleaned.thought_signature = call.thought_signature;
|
|
945
|
+
}
|
|
946
|
+
return cleaned;
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
const hasReplayableToolCalls = Array.isArray(mutableTarget.tool_calls) && mutableTarget.tool_calls.length > 0;
|
|
950
|
+
const replayPlaceholder = options?.replayReasoningPlaceholder;
|
|
951
|
+
if (typeof replayPlaceholder === "string" && replayPlaceholder.length > 0 && !options?.stripReasoningContent && (hasReplayableToolCalls || hasDowngradedToolTurnOrigin(source)) && !(typeof mutableTarget.reasoning_content === "string" && mutableTarget.reasoning_content.length > 0)) {
|
|
952
|
+
mutableTarget.reasoning_content = replayPlaceholder;
|
|
953
|
+
if (shapingCounts)
|
|
954
|
+
shapingCounts.placeholderInjectedTurns += 1;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
if (source.role === "tool") {
|
|
958
|
+
if (TOOL_IMAGES in source) {
|
|
959
|
+
mutableTarget[TOOL_IMAGES] = source[TOOL_IMAGES];
|
|
960
|
+
}
|
|
961
|
+
if (source.tool_result_metadata) {
|
|
962
|
+
Object.defineProperty(mutableTarget, "tool_result_metadata", {
|
|
963
|
+
value: source.tool_result_metadata,
|
|
964
|
+
enumerable: false,
|
|
965
|
+
configurable: true,
|
|
966
|
+
writable: true
|
|
967
|
+
});
|
|
968
|
+
}
|
|
969
|
+
const toolCallId = typeof source.tool_call_id === "string" ? source.tool_call_id.trim() : typeof source.toolCallId === "string" ? source.toolCallId.trim() : "";
|
|
970
|
+
if (toolCallId)
|
|
971
|
+
mutableTarget.tool_call_id = toolCallId;
|
|
972
|
+
}
|
|
973
|
+
return target;
|
|
832
974
|
}
|
|
833
|
-
function
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
975
|
+
function findAgentStatePairingIssues(messages) {
|
|
976
|
+
const issues = [];
|
|
977
|
+
const callIds = new Set;
|
|
978
|
+
const resultIds = new Set;
|
|
979
|
+
for (const message of messages) {
|
|
980
|
+
if (message.role === "assistant" && Array.isArray(message.tool_calls)) {
|
|
981
|
+
for (const call of message.tool_calls) {
|
|
982
|
+
const id = typeof call?.id === "string" ? call.id.trim() : "";
|
|
983
|
+
if (!id) {
|
|
984
|
+
issues.push("assistant tool call is missing id");
|
|
985
|
+
} else if (callIds.has(id)) {
|
|
986
|
+
issues.push(`duplicate assistant tool call id: ${id}`);
|
|
987
|
+
} else {
|
|
988
|
+
callIds.add(id);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
if (message.role === "tool") {
|
|
993
|
+
const id = typeof message.tool_call_id === "string" ? message.tool_call_id.trim() : typeof message.toolCallId === "string" ? message.toolCallId.trim() : "";
|
|
994
|
+
if (!id) {
|
|
995
|
+
issues.push("tool result is missing tool_call_id");
|
|
996
|
+
} else if (resultIds.has(id)) {
|
|
997
|
+
issues.push(`duplicate tool result id: ${id}`);
|
|
998
|
+
} else {
|
|
999
|
+
resultIds.add(id);
|
|
1000
|
+
if (!callIds.has(id))
|
|
1001
|
+
issues.push(`orphan tool result id: ${id}`);
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
for (const id of callIds) {
|
|
1006
|
+
if (!resultIds.has(id))
|
|
1007
|
+
issues.push(`missing tool result id: ${id}`);
|
|
1008
|
+
}
|
|
1009
|
+
return issues;
|
|
840
1010
|
}
|
|
841
|
-
function
|
|
842
|
-
return
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
});
|
|
1011
|
+
function toOpenAiCompatibleMessages(messages, options) {
|
|
1012
|
+
return projectOpenAiToolImages(messages, options?.supportsToolImages === true, options?.maxImagesPerRequest).map((message) => preserveAgentStateFields(message, {
|
|
1013
|
+
role: message.role,
|
|
1014
|
+
content: message.content ?? ""
|
|
1015
|
+
}, options));
|
|
846
1016
|
}
|
|
847
|
-
function
|
|
848
|
-
|
|
1017
|
+
function shouldStripReasoningContentForOutbound(provider, model) {
|
|
1018
|
+
const p = provider?.trim().toLowerCase();
|
|
1019
|
+
const m = model?.trim().toLowerCase();
|
|
1020
|
+
if (!p || !m)
|
|
1021
|
+
return false;
|
|
1022
|
+
if (p === "deepseek" || p === "nolo") {
|
|
1023
|
+
return m === "deepseek-flash" || m === "deepseek-v4-flash" || m === "deepseek-v4-flash-vision-exp" || m === "deepseek-v4-pro";
|
|
1024
|
+
}
|
|
1025
|
+
return false;
|
|
849
1026
|
}
|
|
850
|
-
|
|
851
|
-
|
|
1027
|
+
var REASONING_REPLAY_PLACEHOLDER = "(reasoning not captured for this turn)";
|
|
1028
|
+
function shouldReplayReasoningContentForOutbound(provider, model) {
|
|
1029
|
+
const p = provider?.trim().toLowerCase();
|
|
1030
|
+
const m = model?.trim().toLowerCase();
|
|
1031
|
+
if (!p || !m)
|
|
1032
|
+
return false;
|
|
1033
|
+
return shouldStripReasoningContentForOutbound(p, m) || (p === "opencode-go" || p === "opencode") && m.includes("deepseek");
|
|
852
1034
|
}
|
|
853
|
-
function
|
|
854
|
-
|
|
1035
|
+
function resolveReasoningReplayOptions(provider, model, stripReasoningContent) {
|
|
1036
|
+
if (stripReasoningContent)
|
|
1037
|
+
return {};
|
|
1038
|
+
return shouldReplayReasoningContentForOutbound(provider, model) ? { replayReasoningPlaceholder: REASONING_REPLAY_PLACEHOLDER } : {};
|
|
855
1039
|
}
|
|
856
|
-
|
|
857
|
-
|
|
1040
|
+
|
|
1041
|
+
// packages/integrations/openai/responsesToolDiscovery.ts
|
|
1042
|
+
var searchTool = {
|
|
1043
|
+
type: "tool_search",
|
|
1044
|
+
execution: "client",
|
|
1045
|
+
description: "Find tools by name, keywords, category, or capability pack. Pass capabilityPackId to activate an entire authorized pack.",
|
|
1046
|
+
parameters: {
|
|
1047
|
+
type: "object",
|
|
1048
|
+
properties: { query: { type: "string" }, capabilityPackId: { type: "string" } },
|
|
1049
|
+
required: ["query"]
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
var isEagerTool = (name) => isHotEagerTool(name);
|
|
1053
|
+
function hasDeferrableResponsesTools(tools) {
|
|
1054
|
+
return !!tools?.length && tools.every((tool) => tool.type === "function") && tools.some((tool) => !!tool.function?.name && tool.function.name !== TOOL_DISCOVERY_CALL_NAME && !isEagerTool(tool.function.name));
|
|
858
1055
|
}
|
|
859
|
-
|
|
860
|
-
|
|
1056
|
+
var hostedPrefix = `${TOOL_DISCOVERY_ID_PREFIX}openai-hosted:`;
|
|
1057
|
+
function parseJson(value) {
|
|
1058
|
+
if (typeof value !== "string")
|
|
1059
|
+
return value;
|
|
861
1060
|
try {
|
|
862
|
-
return JSON.
|
|
1061
|
+
return JSON.parse(value);
|
|
863
1062
|
} catch {
|
|
864
|
-
return
|
|
1063
|
+
return;
|
|
865
1064
|
}
|
|
866
1065
|
}
|
|
867
|
-
function
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
const
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
1066
|
+
function readSearchItem(item) {
|
|
1067
|
+
if (!isRecord(item) || item.type !== "tool_search_call" || item.execution !== "client" || typeof item.call_id !== "string" || !item.call_id || item.status !== undefined && item.status !== "completed")
|
|
1068
|
+
return;
|
|
1069
|
+
const args = parseJson(item.arguments);
|
|
1070
|
+
if (!isRecord(args) || typeof args.query !== "string")
|
|
1071
|
+
return;
|
|
1072
|
+
return {
|
|
1073
|
+
type: "tool_search_call",
|
|
1074
|
+
call_id: item.call_id,
|
|
1075
|
+
execution: "client",
|
|
1076
|
+
arguments: { ...args, query: args.query },
|
|
1077
|
+
...typeof item.id === "string" ? { id: item.id } : {},
|
|
1078
|
+
...typeof item.status === "string" ? { status: item.status } : {}
|
|
1079
|
+
};
|
|
878
1080
|
}
|
|
879
|
-
function
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
if (!fn || typeof fn !== "object" || !isString(fn.name) || fn.name.length === 0) return null;
|
|
884
|
-
const id = isNonEmptyString(c.id) ? c.id : stableToolCallId(assistantIndex, callIndex);
|
|
1081
|
+
function bridgeResponsesToolSearch(item) {
|
|
1082
|
+
const search = readSearchItem(item);
|
|
1083
|
+
if (!search)
|
|
1084
|
+
throw new Error("Responses returned an invalid client tool_search_call.");
|
|
885
1085
|
return {
|
|
886
|
-
id,
|
|
887
|
-
type:
|
|
888
|
-
function: { name:
|
|
889
|
-
...typeof c.extra_content?.google?.thought_signature === "string" ? { extra_content: { google: { thought_signature: c.extra_content.google.thought_signature } } } : {},
|
|
890
|
-
...typeof c.thought_signature === "string" ? { thought_signature: c.thought_signature } : {}
|
|
1086
|
+
id: TOOL_DISCOVERY_ID_PREFIX + search.call_id,
|
|
1087
|
+
type: "function",
|
|
1088
|
+
function: { name: TOOL_DISCOVERY_CALL_NAME, arguments: JSON.stringify(search) }
|
|
891
1089
|
};
|
|
892
1090
|
}
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
const unparsableIndices = /* @__PURE__ */ new Set();
|
|
897
|
-
const unparsableIds = /* @__PURE__ */ new Set();
|
|
898
|
-
toolCalls.forEach((rawCall, callIndex) => {
|
|
899
|
-
const c = rawCall && typeof rawCall === "object" ? rawCall : {};
|
|
900
|
-
const id = isNonEmptyString(c.id) ? c.id : stableToolCallId(assistantIndex, callIndex);
|
|
901
|
-
callIds.push(id);
|
|
902
|
-
idCounts.set(id, (idCounts.get(id) ?? 0) + 1);
|
|
903
|
-
if (!hasParsableObjectArguments(c.function?.arguments)) {
|
|
904
|
-
unparsableIndices.add(callIndex);
|
|
905
|
-
unparsableIds.add(id);
|
|
906
|
-
}
|
|
907
|
-
});
|
|
908
|
-
const duplicateIds = /* @__PURE__ */ new Set();
|
|
909
|
-
for (const [id, count] of idCounts) {
|
|
910
|
-
if (count > 1) duplicateIds.add(id);
|
|
911
|
-
}
|
|
912
|
-
return { callIds, duplicateIds, unparsableIndices, unparsableIds };
|
|
1091
|
+
var isMcpEntry = (entry) => ("mcp" in entry);
|
|
1092
|
+
function isFunctionTool(tool) {
|
|
1093
|
+
return isRecord(tool) && tool.type === "function" && typeof tool.name === "string" && (tool.description === undefined || typeof tool.description === "string");
|
|
913
1094
|
}
|
|
914
|
-
function
|
|
915
|
-
if (!Array.isArray(
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
const
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
while (j < messages.length && messages[j].role === "tool") {
|
|
941
|
-
const toolMsg = messages[j];
|
|
942
|
-
const toolCallId = isNonEmptyString(toolMsg.tool_call_id) ? toolMsg.tool_call_id : "";
|
|
943
|
-
if (toolCallId && candidateCallMap.has(toolCallId) && !matchedToolIds.has(toolCallId)) {
|
|
944
|
-
matchedToolIds.add(toolCallId);
|
|
945
|
-
matchedCallIndices.add(candidateCallMap.get(toolCallId));
|
|
946
|
-
keptToolIndices.add(j);
|
|
947
|
-
}
|
|
948
|
-
j++;
|
|
949
|
-
}
|
|
950
|
-
keptCallIndicesPerAssistant.set(asstIdx, matchedCallIndices);
|
|
951
|
-
i = j;
|
|
952
|
-
} else {
|
|
953
|
-
i++;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
const out = [];
|
|
957
|
-
let k = 0;
|
|
958
|
-
while (k < messages.length) {
|
|
959
|
-
const m = messages[k];
|
|
960
|
-
if (m.role === "assistant" && Array.isArray(m.tool_calls) && m.tool_calls.length > 0) {
|
|
961
|
-
const asstIdx = k;
|
|
962
|
-
const keptIndices = keptCallIndicesPerAssistant.get(asstIdx) ?? /* @__PURE__ */ new Set();
|
|
963
|
-
const keptToolCalls = [];
|
|
964
|
-
const downgradedCallLines = [];
|
|
965
|
-
m.tool_calls.forEach((call, callIndex) => {
|
|
966
|
-
const name = call?.function?.name;
|
|
967
|
-
const declaredAllows = !declared || isString(name) && declared.has(name);
|
|
968
|
-
const keep = keptIndices.has(callIndex) && declaredAllows && hasParsableObjectArguments(call?.function?.arguments);
|
|
969
|
-
if (keep) {
|
|
970
|
-
const normalized = normalizeToolCall(call, asstIdx, callIndex);
|
|
971
|
-
if (normalized) {
|
|
972
|
-
keptToolCalls.push(normalized);
|
|
973
|
-
} else {
|
|
974
|
-
downgradedCallLines.push(renderToolCallAsText(call));
|
|
975
|
-
}
|
|
976
|
-
} else {
|
|
977
|
-
downgradedCallLines.push(renderToolCallAsText(call));
|
|
1095
|
+
function readHostedPaths(raw) {
|
|
1096
|
+
if (!isRecord(raw) || !Array.isArray(raw.paths) || !raw.paths.every((path) => typeof path === "string"))
|
|
1097
|
+
return;
|
|
1098
|
+
return raw.paths;
|
|
1099
|
+
}
|
|
1100
|
+
function finishResponsesToolDiscovery(calls, plan, output) {
|
|
1101
|
+
if (plan?.protocol === "openai-hosted") {
|
|
1102
|
+
const next = [];
|
|
1103
|
+
const results = [];
|
|
1104
|
+
const ordinary = new Map(calls.map((call) => [call.id, call]));
|
|
1105
|
+
const visible = new Set(plan.visible);
|
|
1106
|
+
let paths;
|
|
1107
|
+
for (const item of Array.isArray(output) ? output : []) {
|
|
1108
|
+
if (item?.type === "tool_search_call") {
|
|
1109
|
+
if (item.execution !== "server")
|
|
1110
|
+
throw new Error("OpenAI returned client search in hosted mode.");
|
|
1111
|
+
paths = item.call_id === null && item.status === "completed" ? readHostedPaths(item.arguments) : undefined;
|
|
1112
|
+
} else if (item?.type === "tool_search_output" && item.execution === "server" && item.call_id === null && item.status === "completed" && paths && Array.isArray(item.tools)) {
|
|
1113
|
+
const idBase = hostedPrefix + (typeof item.id === "string" && item.id ? item.id : `round-${plan.replayedCalls.size}`);
|
|
1114
|
+
let id = idBase;
|
|
1115
|
+
for (let index = 1;plan.replayedCalls.has(id) || next.some((call) => call.id === id); index++)
|
|
1116
|
+
id = `${idBase}-${index}`;
|
|
1117
|
+
const names = new Set;
|
|
1118
|
+
for (const tool of item.tools) {
|
|
1119
|
+
if (isFunctionTool(tool) && !visible.has(tool.name))
|
|
1120
|
+
names.add(tool.name);
|
|
978
1121
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
content: contentWithDowngrade
|
|
1122
|
+
const loaded = [...names].flatMap((name) => {
|
|
1123
|
+
const current = plan.state.authorized.get(name);
|
|
1124
|
+
if (!current || isMcpEntry(current))
|
|
1125
|
+
return [];
|
|
1126
|
+
visible.add(name);
|
|
1127
|
+
return [toolDiscoveryReference(current)];
|
|
986
1128
|
});
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
content: renderToolResultAsText(downgradedToolMsg)
|
|
997
|
-
})
|
|
998
|
-
);
|
|
999
|
-
nextIdx2++;
|
|
1000
|
-
}
|
|
1001
|
-
k = nextIdx2;
|
|
1002
|
-
continue;
|
|
1003
|
-
}
|
|
1004
|
-
const sanitizedAssistant = transferDowngradedToolTurnOrigin(m, {
|
|
1005
|
-
...m,
|
|
1006
|
-
tool_calls: keptToolCalls
|
|
1007
|
-
});
|
|
1008
|
-
out.push(sanitizedAssistant);
|
|
1009
|
-
let nextIdx = k + 1;
|
|
1010
|
-
const deferredResultLines = [];
|
|
1011
|
-
while (nextIdx < messages.length && messages[nextIdx].role === "tool") {
|
|
1012
|
-
if (keptToolIndices.has(nextIdx)) {
|
|
1013
|
-
const toolMsg = messages[nextIdx];
|
|
1014
|
-
out.push({
|
|
1015
|
-
...toolMsg,
|
|
1016
|
-
content: toolMsg.content ?? "",
|
|
1017
|
-
tool_call_id: toolMsg.tool_call_id
|
|
1018
|
-
});
|
|
1019
|
-
} else {
|
|
1020
|
-
deferredResultLines.push(renderToolResultAsText(messages[nextIdx]));
|
|
1129
|
+
next.push({ id, type: "function", function: { name: TOOL_DISCOVERY_CALL_NAME, arguments: JSON.stringify({ paths }) } });
|
|
1130
|
+
results.push({ callId: id, content: JSON.stringify({ loaded }) });
|
|
1131
|
+
paths = undefined;
|
|
1132
|
+
} else if (item?.type === "function_call") {
|
|
1133
|
+
const call = ordinary.get(item.call_id);
|
|
1134
|
+
if (!call)
|
|
1135
|
+
continue;
|
|
1136
|
+
if (!visible.has(call.function.name) || plan.replayedCalls.has(call.id)) {
|
|
1137
|
+
throw new Error("OpenAI Responses called a tool outside its authorized visible catalog.");
|
|
1021
1138
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
const deferredLines = [...downgradedCallLines, ...deferredResultLines];
|
|
1025
|
-
if (deferredLines.length > 0) {
|
|
1026
|
-
out.push({
|
|
1027
|
-
role: "assistant",
|
|
1028
|
-
content: joinLines(...deferredLines)
|
|
1029
|
-
});
|
|
1139
|
+
ordinary.delete(call.id);
|
|
1140
|
+
next.push(call);
|
|
1030
1141
|
}
|
|
1031
|
-
k = nextIdx;
|
|
1032
|
-
continue;
|
|
1033
|
-
}
|
|
1034
|
-
if (m.role === "tool") {
|
|
1035
|
-
out.push(
|
|
1036
|
-
transferDowngradedToolTurnOrigin(m, {
|
|
1037
|
-
role: "assistant",
|
|
1038
|
-
content: renderToolResultAsText(m)
|
|
1039
|
-
})
|
|
1040
|
-
);
|
|
1041
|
-
k++;
|
|
1042
|
-
continue;
|
|
1043
1142
|
}
|
|
1044
|
-
|
|
1045
|
-
k++;
|
|
1046
|
-
}
|
|
1047
|
-
return out;
|
|
1048
|
-
}
|
|
1049
|
-
function combineContentWithText(base, extra) {
|
|
1050
|
-
if (typeof base === "string") {
|
|
1051
|
-
return base ? `${base}
|
|
1052
|
-
${extra}` : extra;
|
|
1053
|
-
}
|
|
1054
|
-
if (Array.isArray(base)) {
|
|
1055
|
-
return [...base, { type: "text", text: extra }];
|
|
1143
|
+
return { calls: next, results };
|
|
1056
1144
|
}
|
|
1057
|
-
|
|
1145
|
+
const results = resolveResponsesToolDiscovery(calls, plan);
|
|
1146
|
+
const tagged = plan?.protocol === "openai-client" ? calls.map((call) => isToolDiscoveryCall(call) ? { ...call, function: { ...call.function, arguments: JSON.stringify({ ...parseJson(call.function.arguments), protocol: plan.protocol }) } } : call) : calls;
|
|
1147
|
+
return { calls: tagged, results };
|
|
1058
1148
|
}
|
|
1059
|
-
function
|
|
1060
|
-
if (
|
|
1061
|
-
|
|
1062
|
-
|
|
1149
|
+
function prepareResponsesToolDiscovery(body, compacted, mcpTools, protocol = "deepseek-client") {
|
|
1150
|
+
if (!Array.isArray(body.tools) || !Array.isArray(body.input) || !body.tools.length || !body.tools.every(isFunctionTool))
|
|
1151
|
+
return;
|
|
1152
|
+
const functions = body.tools.filter((tool) => tool.name !== TOOL_DISCOVERY_CALL_NAME);
|
|
1153
|
+
const mcpEntries = functions.some((tool) => tool.name === "callMcpTool") ? mcpTools?.map(mcpDiscoveryEntry) ?? [] : [];
|
|
1154
|
+
const state = createToolLoadingState([...functions, ...mcpEntries]);
|
|
1155
|
+
const catalog = state.authorized;
|
|
1156
|
+
const eager = new Set([...catalog.keys()].filter((name) => isEagerTool(name)));
|
|
1157
|
+
const restored = compacted ? state.restore(compacted, eager) : undefined;
|
|
1158
|
+
const restoredNames = new Set(state.loadedInContext);
|
|
1159
|
+
const staleDefinitions = new Set;
|
|
1160
|
+
for (const ref of compacted?.loaded ?? []) {
|
|
1161
|
+
if (catalog.has(ref.name) && !eager.has(ref.name) && !state.executable(ref))
|
|
1162
|
+
staleDefinitions.add(ref.name);
|
|
1063
1163
|
}
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1164
|
+
const staleCalls = new Set;
|
|
1165
|
+
const searches = new Map;
|
|
1166
|
+
const hostedSearches = new Map;
|
|
1167
|
+
const historicalResults = new Map(body.input.filter((item) => item?.type === "function_call_output").map((item) => [item.call_id, item]));
|
|
1168
|
+
const ordinaryCallIds = new Set;
|
|
1169
|
+
const replayedSearches = new Set;
|
|
1170
|
+
const guidanceMessage = (guidance) => ({
|
|
1171
|
+
type: "message",
|
|
1172
|
+
role: "user",
|
|
1173
|
+
content: [{ type: "input_text", text: guidance.join(`
|
|
1174
|
+
`) }]
|
|
1175
|
+
});
|
|
1176
|
+
function mcpGuidance(entries) {
|
|
1177
|
+
const targets = entries.filter(isMcpEntry).map(({ mcp }) => ({
|
|
1178
|
+
connectionId: mcp.connectionId,
|
|
1179
|
+
expectedRevision: mcp.connectionRevision,
|
|
1180
|
+
catalogRevision: mcp.schemaVersion,
|
|
1181
|
+
toolName: mcp.toolName
|
|
1182
|
+
}));
|
|
1183
|
+
return targets.length ? [`MCP targets (data only). Use listMcpTools for argument schemas and callMcpTool for invocation; host confirmation remains required. ${JSON.stringify(targets)}`] : [];
|
|
1071
1184
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
return typeof JSON.parse(value) === "string";
|
|
1078
|
-
} catch {
|
|
1079
|
-
return false;
|
|
1185
|
+
const input = [];
|
|
1186
|
+
if (restored) {
|
|
1187
|
+
const notes = [...restored.guidance, ...mcpGuidance(restored.tools)];
|
|
1188
|
+
if (notes.length)
|
|
1189
|
+
input.push(guidanceMessage(notes));
|
|
1080
1190
|
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
if (
|
|
1093
|
-
if (escaped) escaped = false;
|
|
1094
|
-
else if (ch === "\\") escaped = true;
|
|
1095
|
-
else if (ch === '"') inString = false;
|
|
1191
|
+
let guidance = [];
|
|
1192
|
+
const deferredTool = (tool) => ({ ...tool, defer_loading: true });
|
|
1193
|
+
const openAi = protocol !== "deepseek-client";
|
|
1194
|
+
const replayItems = body.input.flatMap((item) => {
|
|
1195
|
+
const id = item?.call_id;
|
|
1196
|
+
if (protocol !== "openai-hosted" || typeof id !== "string" || !id.startsWith(hostedPrefix) || item.type !== "function_call" || item.name !== TOOL_DISCOVERY_CALL_NAME)
|
|
1197
|
+
return [item];
|
|
1198
|
+
const result = historicalResults.get(id);
|
|
1199
|
+
return result ? [item, result] : [item];
|
|
1200
|
+
});
|
|
1201
|
+
for (const item of replayItems) {
|
|
1202
|
+
if (item?.type === "tool_search_call" || item?.type === "tool_search_output" || item?.type === "tool_reference")
|
|
1096
1203
|
continue;
|
|
1204
|
+
if (guidance.length && item?.type !== "function_call_output") {
|
|
1205
|
+
input.push(guidanceMessage(guidance));
|
|
1206
|
+
guidance = [];
|
|
1097
1207
|
}
|
|
1098
|
-
if (
|
|
1099
|
-
|
|
1208
|
+
if (item?.type === "function_call" && item.name === TOOL_DISCOVERY_CALL_NAME) {
|
|
1209
|
+
const raw = parseJson(item.arguments);
|
|
1210
|
+
if (protocol === "openai-hosted" && typeof item.call_id === "string" && item.call_id.startsWith(hostedPrefix) && historicalResults.has(item.call_id) && !hostedSearches.has(item.call_id)) {
|
|
1211
|
+
const paths = readHostedPaths(raw);
|
|
1212
|
+
if (paths) {
|
|
1213
|
+
hostedSearches.set(item.call_id, { paths });
|
|
1214
|
+
input.push({ type: "tool_search_call", execution: "server", call_id: null, status: "completed", arguments: { paths } });
|
|
1215
|
+
}
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
const search = readSearchItem(raw);
|
|
1219
|
+
const source = isRecord(raw) ? raw.protocol ?? "deepseek-client" : undefined;
|
|
1220
|
+
if (protocol !== "openai-hosted" && source === protocol && search && item.call_id === TOOL_DISCOVERY_ID_PREFIX + search.call_id && historicalResults.has(item.call_id) && !searches.has(item.call_id)) {
|
|
1221
|
+
searches.set(item.call_id, search);
|
|
1222
|
+
input.push({
|
|
1223
|
+
type: search.type,
|
|
1224
|
+
call_id: search.call_id,
|
|
1225
|
+
execution: search.execution,
|
|
1226
|
+
arguments: search.arguments,
|
|
1227
|
+
...openAi ? { status: "completed" } : {}
|
|
1228
|
+
});
|
|
1229
|
+
}
|
|
1100
1230
|
continue;
|
|
1101
1231
|
}
|
|
1102
|
-
if (
|
|
1103
|
-
|
|
1232
|
+
if (item?.type === "function_call_output" && typeof item.call_id === "string" && item.call_id.startsWith(TOOL_DISCOVERY_ID_PREFIX)) {
|
|
1233
|
+
const search = searches.get(item.call_id);
|
|
1234
|
+
const hosted = hostedSearches.has(item.call_id);
|
|
1235
|
+
if (!search && !hosted) {
|
|
1236
|
+
if (ordinaryCallIds.has(item.call_id) && !staleCalls.delete(item.call_id))
|
|
1237
|
+
input.push(item);
|
|
1238
|
+
continue;
|
|
1239
|
+
}
|
|
1240
|
+
if (replayedSearches.has(item.call_id))
|
|
1241
|
+
continue;
|
|
1242
|
+
replayedSearches.add(item.call_id);
|
|
1243
|
+
const refs = readToolLoadingReferences(item.output);
|
|
1244
|
+
for (const ref of refs.loaded) {
|
|
1245
|
+
if (catalog.has(ref.name) && !state.loadedInContext.has(ref.name) && !eager.has(ref.name) && !state.executable(ref)) {
|
|
1246
|
+
staleDefinitions.add(ref.name);
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
const projected = state.restore(refs, eager);
|
|
1250
|
+
for (const tool of projected.tools)
|
|
1251
|
+
staleDefinitions.delete(tool.name);
|
|
1252
|
+
guidance.push(...projected.guidance, ...mcpGuidance(projected.tools));
|
|
1253
|
+
input.push({
|
|
1254
|
+
type: "tool_search_output",
|
|
1255
|
+
call_id: search?.call_id ?? null,
|
|
1256
|
+
execution: hosted ? "server" : "client",
|
|
1257
|
+
...openAi ? { status: "completed" } : {},
|
|
1258
|
+
tools: projected.tools.filter((tool) => !isMcpEntry(tool)).map((tool) => openAi ? deferredTool(tool) : tool)
|
|
1259
|
+
});
|
|
1104
1260
|
continue;
|
|
1105
1261
|
}
|
|
1106
|
-
if (
|
|
1107
|
-
|
|
1108
|
-
|
|
1262
|
+
if (item?.type === "function_call")
|
|
1263
|
+
ordinaryCallIds.add(item.call_id);
|
|
1264
|
+
if (item?.type === "function_call" && (staleDefinitions.has(item.name) || openAi && (!catalog.has(item.name) || isMcpEntry(catalog.get(item.name))))) {
|
|
1265
|
+
staleCalls.add(item.call_id);
|
|
1109
1266
|
continue;
|
|
1110
1267
|
}
|
|
1111
|
-
if (
|
|
1112
|
-
lastCommaOutsideString = { index: i, closers: closersOf(stack) };
|
|
1268
|
+
if (item?.type === "function_call_output" && staleCalls.delete(item.call_id))
|
|
1113
1269
|
continue;
|
|
1270
|
+
if (item?.type === "function_call" && catalog.has(item.name) && !state.loadedInContext.has(item.name)) {
|
|
1271
|
+
eager.add(item.name);
|
|
1114
1272
|
}
|
|
1273
|
+
input.push(item);
|
|
1115
1274
|
}
|
|
1116
|
-
|
|
1275
|
+
if (guidance.length)
|
|
1276
|
+
input.push(guidanceMessage(guidance));
|
|
1277
|
+
const visible = new Set([...eager, ...[...state.loadedInContext].filter((name) => {
|
|
1278
|
+
const entry = catalog.get(name);
|
|
1279
|
+
return entry && !isMcpEntry(entry);
|
|
1280
|
+
})]);
|
|
1281
|
+
body.input = input;
|
|
1282
|
+
if (eager.size === catalog.size && replayedSearches.size === 0)
|
|
1283
|
+
return;
|
|
1284
|
+
const localTools = [...catalog.values()].filter((tool) => !isMcpEntry(tool));
|
|
1285
|
+
body.tools = protocol === "openai-hosted" ? localTools.map((tool) => eager.has(tool.name) || restoredNames.has(tool.name) ? tool : deferredTool(tool)) : localTools.filter((tool) => eager.has(tool.name) || restoredNames.has(tool.name));
|
|
1286
|
+
body.tools.push(protocol === "openai-hosted" ? { type: "tool_search" } : protocol === "openai-client" ? {
|
|
1287
|
+
...searchTool,
|
|
1288
|
+
parameters: { ...searchTool.parameters, additionalProperties: false }
|
|
1289
|
+
} : searchTool);
|
|
1290
|
+
if (openAi)
|
|
1291
|
+
body.parallel_tool_calls = false;
|
|
1292
|
+
return {
|
|
1293
|
+
state,
|
|
1294
|
+
protocol,
|
|
1295
|
+
visible,
|
|
1296
|
+
replayedCalls: new Set([
|
|
1297
|
+
...ordinaryCallIds,
|
|
1298
|
+
...hostedSearches.keys(),
|
|
1299
|
+
...replayedSearches,
|
|
1300
|
+
...[...replayedSearches].map((id) => id.slice(TOOL_DISCOVERY_ID_PREFIX.length))
|
|
1301
|
+
])
|
|
1302
|
+
};
|
|
1117
1303
|
}
|
|
1118
|
-
function
|
|
1119
|
-
|
|
1120
|
-
const
|
|
1121
|
-
const
|
|
1122
|
-
const
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
const tailIsCompleteToken = endsWithComma || lastChar === '"' || lastChar === "}" || lastChar === "]";
|
|
1128
|
-
if (!tailIsCompleteToken) return null;
|
|
1129
|
-
const candidates = [trimmed + scan.closers];
|
|
1130
|
-
const lastComma = scan.lastCommaOutsideString;
|
|
1131
|
-
if (lastComma && lastComma.index > 0) {
|
|
1132
|
-
const tailAfterComma = trimmed.slice(lastComma.index + 1).trim();
|
|
1133
|
-
if (isStrictJsonStringToken(tailAfterComma)) {
|
|
1134
|
-
const head = trimmed.slice(0, lastComma.index);
|
|
1135
|
-
candidates.push(head + lastComma.closers);
|
|
1304
|
+
function resolveResponsesToolDiscovery(calls, plan) {
|
|
1305
|
+
const results = [];
|
|
1306
|
+
const seen = new Set(plan?.replayedCalls);
|
|
1307
|
+
const visible = new Set([...plan?.visible ?? [], ...plan?.state.loadedInContext ?? []]);
|
|
1308
|
+
for (const call of calls) {
|
|
1309
|
+
const discovery = isToolDiscoveryCall(call);
|
|
1310
|
+
const wireCallId = discovery ? call.id.slice(TOOL_DISCOVERY_ID_PREFIX.length) : call.id;
|
|
1311
|
+
if (plan && (seen.has(wireCallId) || seen.has(call.id))) {
|
|
1312
|
+
throw new Error("Responses returned a duplicate tool call ID.");
|
|
1136
1313
|
}
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
return candidate;
|
|
1314
|
+
seen.add(wireCallId);
|
|
1315
|
+
seen.add(call.id);
|
|
1316
|
+
if (!discovery) {
|
|
1317
|
+
if (plan && !plan.visible.has(call.function.name)) {
|
|
1318
|
+
throw new Error("Responses requested a function before its definition was loaded.");
|
|
1143
1319
|
}
|
|
1144
|
-
|
|
1320
|
+
continue;
|
|
1321
|
+
}
|
|
1322
|
+
if (!plan)
|
|
1323
|
+
throw new Error("Responses requested tool discovery on a route without client discovery.");
|
|
1324
|
+
const search = readSearchItem(parseJson(call.function.arguments));
|
|
1325
|
+
if (!search || call.id !== TOOL_DISCOVERY_ID_PREFIX + search.call_id) {
|
|
1326
|
+
throw new Error("Responses returned an invalid tool discovery ID.");
|
|
1145
1327
|
}
|
|
1328
|
+
const packId = search.arguments.capabilityPackId;
|
|
1329
|
+
const pack = typeof packId === "string" ? plan.state.activatePack(packId) : { loaded: [] };
|
|
1330
|
+
const excluded = new Set([...visible, ...pack.loaded.map(({ name }) => name)]);
|
|
1331
|
+
const refs = { ...pack, loaded: [...pack.loaded, ...plan.state.search(search.arguments.query, excluded).map(toolDiscoveryReference)] };
|
|
1332
|
+
for (const ref of refs.loaded)
|
|
1333
|
+
visible.add(ref.name);
|
|
1334
|
+
results.push({ callId: call.id, content: JSON.stringify(refs) });
|
|
1146
1335
|
}
|
|
1147
|
-
return
|
|
1336
|
+
return results;
|
|
1148
1337
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1338
|
+
|
|
1339
|
+
// packages/integrations/openai/responsesHelpers.ts
|
|
1340
|
+
var RESPONSES_TOP_LEVEL_SCHEMA_KEYS = ["anyOf", "oneOf", "allOf", "enum", "not"];
|
|
1341
|
+
var sanitizeResponsesParameters = (parameters) => {
|
|
1342
|
+
if (!isRecord(parameters)) {
|
|
1343
|
+
return parameters;
|
|
1152
1344
|
}
|
|
1153
|
-
|
|
1154
|
-
for (const
|
|
1155
|
-
|
|
1156
|
-
for (const call of m.tool_calls) {
|
|
1157
|
-
if (!hasParsableObjectArguments(call?.function?.arguments)) {
|
|
1158
|
-
hasAnyPoison = true;
|
|
1159
|
-
break;
|
|
1160
|
-
}
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
if (hasAnyPoison) break;
|
|
1345
|
+
const next = { ...parameters };
|
|
1346
|
+
for (const key of RESPONSES_TOP_LEVEL_SCHEMA_KEYS) {
|
|
1347
|
+
delete next[key];
|
|
1164
1348
|
}
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
if (toolCallId && unparsableIds.has(toolCallId)) {
|
|
1187
|
-
poisonedToolIndices.add(j);
|
|
1188
|
-
}
|
|
1189
|
-
j++;
|
|
1190
|
-
}
|
|
1191
|
-
if (unparsableIds.size > 0) {
|
|
1192
|
-
poisonedCallsPerAssistant.set(asstIdx, unparsableIds);
|
|
1349
|
+
return next;
|
|
1350
|
+
};
|
|
1351
|
+
var appendTextPart = (parts, text, role) => {
|
|
1352
|
+
if (!text)
|
|
1353
|
+
return;
|
|
1354
|
+
const last = parts.at(-1);
|
|
1355
|
+
const textType = role === "assistant" ? "output_text" : "input_text";
|
|
1356
|
+
if (last?.type === textType) {
|
|
1357
|
+
last.text += text;
|
|
1358
|
+
return;
|
|
1359
|
+
}
|
|
1360
|
+
parts.push({ type: textType, text });
|
|
1361
|
+
};
|
|
1362
|
+
var normalizeMessageParts = (content, role) => {
|
|
1363
|
+
if (typeof content === "string") {
|
|
1364
|
+
if (!content)
|
|
1365
|
+
return [];
|
|
1366
|
+
return [
|
|
1367
|
+
{
|
|
1368
|
+
type: role === "assistant" ? "output_text" : "input_text",
|
|
1369
|
+
text: content
|
|
1193
1370
|
}
|
|
1194
|
-
|
|
1195
|
-
} else {
|
|
1196
|
-
i++;
|
|
1197
|
-
}
|
|
1371
|
+
];
|
|
1198
1372
|
}
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
if (
|
|
1207
|
-
|
|
1208
|
-
if (!badIds || badIds.size === 0) {
|
|
1209
|
-
out.push(m);
|
|
1210
|
-
k++;
|
|
1373
|
+
const parts = [];
|
|
1374
|
+
for (const part of content ?? []) {
|
|
1375
|
+
const typedPart = part;
|
|
1376
|
+
if (typedPart?.type === "text" && typeof typedPart.text === "string") {
|
|
1377
|
+
appendTextPart(parts, typedPart.text, role);
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
if (typedPart?.type === "image_url" && typeof typedPart.image_url?.url === "string" && typedPart.image_url.url) {
|
|
1381
|
+
if (role === "assistant")
|
|
1211
1382
|
continue;
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
rewrittenAssistantMessages += 1;
|
|
1217
|
-
m.tool_calls.forEach((call, callIndex) => {
|
|
1218
|
-
const id = callIds[callIndex];
|
|
1219
|
-
if (badIds.has(id)) {
|
|
1220
|
-
lines.push(renderToolCallAsText(call));
|
|
1221
|
-
downgraded++;
|
|
1222
|
-
} else {
|
|
1223
|
-
kept.push(call);
|
|
1224
|
-
}
|
|
1383
|
+
parts.push({
|
|
1384
|
+
type: "input_image",
|
|
1385
|
+
image_url: typedPart.image_url.url,
|
|
1386
|
+
detail: typedPart.image_url.detail
|
|
1225
1387
|
});
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
return parts;
|
|
1391
|
+
};
|
|
1392
|
+
var normalizeToolOutput = (content) => {
|
|
1393
|
+
if (typeof content === "string")
|
|
1394
|
+
return content;
|
|
1395
|
+
return JSON.stringify(content ?? "");
|
|
1396
|
+
};
|
|
1397
|
+
var toResponsesTools = (tools) => {
|
|
1398
|
+
if (!Array.isArray(tools) || tools.length === 0)
|
|
1399
|
+
return;
|
|
1400
|
+
const normalized = tools.map((tool) => {
|
|
1401
|
+
const fn = tool?.function;
|
|
1402
|
+
const name = fn?.name ?? tool?.name;
|
|
1403
|
+
if (!name)
|
|
1404
|
+
return null;
|
|
1405
|
+
const parameters = fn?.parameters ?? tool?.parameters;
|
|
1406
|
+
return {
|
|
1407
|
+
type: "function",
|
|
1408
|
+
name,
|
|
1409
|
+
...typeof (fn?.description ?? tool?.description) === "string" ? { description: fn?.description ?? tool?.description } : {},
|
|
1410
|
+
parameters: sanitizeResponsesParameters(parameters),
|
|
1411
|
+
...fn?.strict !== undefined || tool?.strict !== undefined ? { strict: fn?.strict ?? tool?.strict } : {}
|
|
1412
|
+
};
|
|
1413
|
+
}).filter(Boolean);
|
|
1414
|
+
return normalized.length > 0 ? normalized : undefined;
|
|
1415
|
+
};
|
|
1416
|
+
var convertMessagesToResponsesInput = (messages, options) => {
|
|
1417
|
+
const input = [];
|
|
1418
|
+
const imageBudget = createToolImageBudget(options?.maxImagesPerRequest);
|
|
1419
|
+
let acceptedImages = 0;
|
|
1420
|
+
for (const message of messages) {
|
|
1421
|
+
if (!message?.role)
|
|
1422
|
+
continue;
|
|
1423
|
+
if (message.role === "tool") {
|
|
1424
|
+
if (!message.tool_call_id)
|
|
1425
|
+
continue;
|
|
1426
|
+
if (!options?.supportsToolImages || !hadToolImages(message)) {
|
|
1427
|
+
input.push({
|
|
1428
|
+
type: "function_call_output",
|
|
1429
|
+
call_id: message.tool_call_id,
|
|
1430
|
+
output: normalizeToolOutput(message.content)
|
|
1230
1431
|
});
|
|
1231
|
-
delete sanitized2.tool_calls;
|
|
1232
|
-
markDowngradedToolTurnOrigin(sanitized2);
|
|
1233
|
-
out.push(sanitized2);
|
|
1234
|
-
let nextIdx2 = k + 1;
|
|
1235
|
-
while (nextIdx2 < messages.length && messages[nextIdx2].role === "tool") {
|
|
1236
|
-
if (poisonedToolIndices.has(nextIdx2)) {
|
|
1237
|
-
const poisonedToolMsg = messages[nextIdx2];
|
|
1238
|
-
out.push(
|
|
1239
|
-
transferDowngradedToolTurnOrigin(poisonedToolMsg, {
|
|
1240
|
-
role: "assistant",
|
|
1241
|
-
content: renderToolResultAsText(poisonedToolMsg)
|
|
1242
|
-
})
|
|
1243
|
-
);
|
|
1244
|
-
rewrittenToolMessages += 1;
|
|
1245
|
-
} else {
|
|
1246
|
-
out.push(messages[nextIdx2]);
|
|
1247
|
-
}
|
|
1248
|
-
nextIdx2++;
|
|
1249
|
-
}
|
|
1250
|
-
k = nextIdx2;
|
|
1251
1432
|
continue;
|
|
1252
1433
|
}
|
|
1253
|
-
const
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
let
|
|
1260
|
-
const
|
|
1261
|
-
|
|
1262
|
-
if (
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1434
|
+
const images = toolImages(message);
|
|
1435
|
+
const text = typeof message.content === "string" ? message.content : normalizeToolOutput(message.content);
|
|
1436
|
+
const effectiveText = text.trim() ? text : EMPTY_TOOL_RESULT_TEXT;
|
|
1437
|
+
const outputParts = [
|
|
1438
|
+
{ type: "input_text", text: effectiveText }
|
|
1439
|
+
];
|
|
1440
|
+
let toolOmitted = 0;
|
|
1441
|
+
for (const image of images) {
|
|
1442
|
+
const decision = imageBudget(image);
|
|
1443
|
+
if (decision === "duplicate")
|
|
1444
|
+
continue;
|
|
1445
|
+
if (decision !== "delivered") {
|
|
1446
|
+
toolOmitted += 1;
|
|
1447
|
+
continue;
|
|
1267
1448
|
}
|
|
1268
|
-
|
|
1449
|
+
acceptedImages += 1;
|
|
1450
|
+
outputParts.push({
|
|
1451
|
+
type: "input_text",
|
|
1452
|
+
text: `Image ${acceptedImages}`
|
|
1453
|
+
});
|
|
1454
|
+
outputParts.push({
|
|
1455
|
+
type: "input_image",
|
|
1456
|
+
image_url: `data:${image.mimeType};base64,${image.dataBase64}`
|
|
1457
|
+
});
|
|
1269
1458
|
}
|
|
1270
|
-
if (
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1459
|
+
if (toolOmitted > 0) {
|
|
1460
|
+
outputParts.push({
|
|
1461
|
+
type: "input_text",
|
|
1462
|
+
text: formatToolImageBudgetNote(toolOmitted)
|
|
1274
1463
|
});
|
|
1275
1464
|
}
|
|
1276
|
-
|
|
1465
|
+
const hadPersistedImages = message?.tool_result_metadata?.[TOOL_HAD_IMAGES] === true;
|
|
1466
|
+
const hasInjectedParts = outputParts.some((p) => p.type === "input_image") || toolOmitted > 0;
|
|
1467
|
+
const shouldUseArray = hadPersistedImages || hasInjectedParts;
|
|
1468
|
+
input.push({
|
|
1469
|
+
type: "function_call_output",
|
|
1470
|
+
call_id: message.tool_call_id,
|
|
1471
|
+
output: shouldUseArray ? outputParts : normalizeToolOutput(message.content)
|
|
1472
|
+
});
|
|
1277
1473
|
continue;
|
|
1278
1474
|
}
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
}
|
|
1289
|
-
out.push(m);
|
|
1290
|
-
}
|
|
1291
|
-
k++;
|
|
1292
|
-
continue;
|
|
1475
|
+
const role = message.role;
|
|
1476
|
+
const hasReplayableToolCalls = Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
|
|
1477
|
+
const reasoningText = typeof message.reasoning_content === "string" && message.reasoning_content.trim() ? message.reasoning_content : hasReplayableToolCalls && options?.replayReasoningPlaceholder?.trim() ? options.replayReasoningPlaceholder : undefined;
|
|
1478
|
+
if (role === "assistant" && !options?.stripReasoningContent && reasoningText) {
|
|
1479
|
+
input.push({
|
|
1480
|
+
type: "reasoning",
|
|
1481
|
+
content: [
|
|
1482
|
+
{ type: "reasoning_text", text: reasoningText }
|
|
1483
|
+
]
|
|
1484
|
+
});
|
|
1293
1485
|
}
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1486
|
+
const contentParts = normalizeMessageParts(message.content, role);
|
|
1487
|
+
if (contentParts.length > 0) {
|
|
1488
|
+
input.push({
|
|
1489
|
+
type: "message",
|
|
1490
|
+
role,
|
|
1491
|
+
content: contentParts
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
if (role === "assistant" && Array.isArray(message.content)) {
|
|
1495
|
+
const replayImages = message.content.map((part) => part).filter((part) => part?.type === "image_url" && typeof part.image_url?.url === "string" && Boolean(part.image_url.url)).map((part) => ({
|
|
1496
|
+
type: "input_image",
|
|
1497
|
+
image_url: part.image_url.url,
|
|
1498
|
+
detail: part.image_url.detail
|
|
1499
|
+
}));
|
|
1500
|
+
if (replayImages.length > 0) {
|
|
1501
|
+
input.push({
|
|
1502
|
+
type: "message",
|
|
1503
|
+
role: "user",
|
|
1504
|
+
content: replayImages
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
if (role === "assistant" && Array.isArray(message.tool_calls)) {
|
|
1509
|
+
for (const toolCall of message.tool_calls) {
|
|
1510
|
+
if (!toolCall?.id || !toolCall.function?.name)
|
|
1511
|
+
continue;
|
|
1512
|
+
input.push({
|
|
1513
|
+
type: "function_call",
|
|
1514
|
+
call_id: toolCall.id,
|
|
1515
|
+
name: toolCall.function.name,
|
|
1516
|
+
arguments: typeof toolCall.function.arguments === "string" ? toolCall.function.arguments : JSON.stringify(toolCall.function.arguments ?? {})
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1307
1519
|
}
|
|
1308
|
-
};
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
|
-
// packages/core/ollamaEndpoint.ts
|
|
1312
|
-
function isOllamaEndpoint(endpoint) {
|
|
1313
|
-
if (!endpoint || typeof endpoint !== "string") return false;
|
|
1314
|
-
try {
|
|
1315
|
-
const url = new URL(endpoint.includes("://") ? endpoint : `http://${endpoint}`);
|
|
1316
|
-
if (url.port === "11434") return true;
|
|
1317
|
-
const host = url.hostname.toLowerCase();
|
|
1318
|
-
return host === "ollama.com" || host.endsWith(".ollama.com") || host === "ollama.ai" || host.endsWith(".ollama.ai");
|
|
1319
|
-
} catch {
|
|
1320
|
-
return false;
|
|
1321
|
-
}
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
// packages/ai/llm/usageRequestOptions.ts
|
|
1325
|
-
var STREAM_USAGE_PROVIDERS = /* @__PURE__ */ new Set([
|
|
1326
|
-
"google",
|
|
1327
|
-
"openrouter",
|
|
1328
|
-
"xai",
|
|
1329
|
-
"openai",
|
|
1330
|
-
"fireworks",
|
|
1331
|
-
"mistral",
|
|
1332
|
-
"cloudflare",
|
|
1333
|
-
"gmi",
|
|
1334
|
-
// OpenAI-compatible hosted providers expose the terminal usage chunk when
|
|
1335
|
-
// requested, including DeepInfra and Vultr. Keep this capability decision
|
|
1336
|
-
// in the shared seam so proxy and local runtimes cannot drift.
|
|
1337
|
-
"deepinfra",
|
|
1338
|
-
"vultr",
|
|
1339
|
-
// Explicit local-runtime providers with OpenAI-compatible streaming: both
|
|
1340
|
-
// DeepSeek official and Ollama honor stream_options.include_usage on their
|
|
1341
|
-
// chat.completions endpoints. Without them, locally-configured agents lose
|
|
1342
|
-
// token statistics / local billing (the whitelist replaced the previous
|
|
1343
|
-
// always-on include_usage). ollama-cloud is the Ollama Cloud subscription
|
|
1344
|
-
// endpoint (https://ollama.com/v1), an OpenAI-compatible API that likewise
|
|
1345
|
-
// honors stream_options.include_usage.
|
|
1346
|
-
"deepseek",
|
|
1347
|
-
"ollama",
|
|
1348
|
-
"ollama-cloud",
|
|
1349
|
-
// RunInfra hosted fallback channel: OpenAI-compatible chat.completions
|
|
1350
|
-
// endpoint, supports stream_options.include_usage (measured to return
|
|
1351
|
-
// prompt_tokens_details.cached_tokens).
|
|
1352
|
-
"runinfra",
|
|
1353
|
-
"baseten",
|
|
1354
|
-
// Kimi For Coding subscription (api.kimi.com/coding/v1): OpenAI-compatible
|
|
1355
|
-
// chat.completions streaming that only emits the terminal usage frame when
|
|
1356
|
-
// stream_options.include_usage is requested (measured; the official
|
|
1357
|
-
// kimi-code CLI also sends include_usage). Custom agents pointing at the
|
|
1358
|
-
// same endpoint are covered by the endpoint check below, not here.
|
|
1359
|
-
"kimi-code",
|
|
1360
|
-
// MiniMax 官方 API(api.minimaxi.com):OpenAI 兼容流式端点,支持 stream_options.include_usage。
|
|
1361
|
-
"minimax",
|
|
1362
|
-
// OpenDesign (open-design.ai) and OpenCode Go (opencode.ai): OpenAI-compatible
|
|
1363
|
-
// aggregation / subscription endpoints. Verified to emit terminal usage with
|
|
1364
|
-
// prompt_tokens_details.cached_tokens.
|
|
1365
|
-
"opendesign",
|
|
1366
|
-
"opencode",
|
|
1367
|
-
"opencode-go",
|
|
1368
|
-
"longcat"
|
|
1369
|
-
]);
|
|
1370
|
-
var KIMI_CODE_ENDPOINT_MARKER = "api.kimi.com";
|
|
1371
|
-
function extractHostname(endpoint) {
|
|
1372
|
-
try {
|
|
1373
|
-
const raw = endpoint.includes("://") ? endpoint : `https://${endpoint}`;
|
|
1374
|
-
return new URL(raw).hostname.toLowerCase();
|
|
1375
|
-
} catch {
|
|
1376
|
-
return null;
|
|
1377
1520
|
}
|
|
1378
|
-
|
|
1379
|
-
var isMiniMaxEndpoint = (endpoint) => {
|
|
1380
|
-
const host = extractHostname(endpoint);
|
|
1381
|
-
return host === "api.minimaxi.com" || host === "api.minimax.chat";
|
|
1382
|
-
};
|
|
1383
|
-
var isLongCatEndpoint = (endpoint) => {
|
|
1384
|
-
const host = extractHostname(endpoint);
|
|
1385
|
-
return host === "api.longcat.chat" || host === "api.longcat.ai";
|
|
1386
|
-
};
|
|
1387
|
-
var isSupportedCustomEndpoint = (endpoint) => {
|
|
1388
|
-
if (typeof endpoint !== "string") return false;
|
|
1389
|
-
const ep = endpoint.toLowerCase();
|
|
1390
|
-
return isMiniMaxEndpoint(endpoint) || isLongCatEndpoint(endpoint) || ep.includes(KIMI_CODE_ENDPOINT_MARKER) || ep.includes("open-design.ai") || ep.includes("opencode.ai") || isOllamaEndpoint(endpoint);
|
|
1521
|
+
return input;
|
|
1391
1522
|
};
|
|
1392
|
-
var
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
usage: {
|
|
1401
|
-
include: true
|
|
1523
|
+
var extractTextFromResponseOutput = (response) => {
|
|
1524
|
+
const parts = [];
|
|
1525
|
+
for (const item of response?.output ?? []) {
|
|
1526
|
+
if (item?.type !== "message")
|
|
1527
|
+
continue;
|
|
1528
|
+
for (const content of item.content ?? []) {
|
|
1529
|
+
if (content?.type === "output_text" && typeof content.text === "string") {
|
|
1530
|
+
parts.push(content.text);
|
|
1402
1531
|
}
|
|
1403
|
-
}
|
|
1532
|
+
}
|
|
1404
1533
|
}
|
|
1405
|
-
return
|
|
1406
|
-
...shouldRequestStreamUsage ? {
|
|
1407
|
-
stream_options: {
|
|
1408
|
-
include_usage: true
|
|
1409
|
-
}
|
|
1410
|
-
} : {},
|
|
1411
|
-
...EXTRA_USAGE_FIELD_PROVIDERS.has(normalizedProvider) ? {
|
|
1412
|
-
usage: {
|
|
1413
|
-
include: true
|
|
1414
|
-
}
|
|
1415
|
-
} : {}
|
|
1416
|
-
};
|
|
1534
|
+
return parts.join("");
|
|
1417
1535
|
};
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
);
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
// packages/core/chat/bareImageUrlShape.ts
|
|
1430
|
-
function requiresBareImageUrl(args) {
|
|
1431
|
-
const model = asTrimmedLowercaseString(args.model);
|
|
1432
|
-
if (!model.startsWith("gpt-")) return false;
|
|
1433
|
-
const provider = asTrimmedLowercaseString(args.provider);
|
|
1434
|
-
if (provider === "opencode-go") return true;
|
|
1435
|
-
return asTrimmedLowercaseString(args.endpoint).includes("opencode.ai/zen/go");
|
|
1436
|
-
}
|
|
1437
|
-
function toBareImageUrlContent(content) {
|
|
1438
|
-
if (!Array.isArray(content)) return content;
|
|
1439
|
-
let changed = false;
|
|
1440
|
-
const parts = content.map((part) => {
|
|
1441
|
-
if (!isImageUrlPart(part)) return part;
|
|
1442
|
-
const imageUrl = part.image_url;
|
|
1443
|
-
if (!imageUrl || typeof imageUrl !== "object") return part;
|
|
1444
|
-
const url = imageUrl.url;
|
|
1445
|
-
if (typeof url !== "string" || !url) return part;
|
|
1446
|
-
changed = true;
|
|
1447
|
-
return { ...part, image_url: url };
|
|
1448
|
-
});
|
|
1449
|
-
return changed ? parts : content;
|
|
1450
|
-
}
|
|
1451
|
-
function toBareImageUrlMessages(messages) {
|
|
1452
|
-
return messages.map((message) => {
|
|
1453
|
-
const content = toBareImageUrlContent(message.content);
|
|
1454
|
-
return content === message.content ? message : { ...message, content };
|
|
1455
|
-
});
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
// packages/agent-runtime/openAiCompatibleMessages.ts
|
|
1459
|
-
function isGeminiCompatibleTarget(provider, model) {
|
|
1460
|
-
const p = (provider ?? "").trim().toLowerCase();
|
|
1461
|
-
const m = (model ?? "").trim().toLowerCase();
|
|
1462
|
-
return p === "google" || p === "google-antigravity" || p.startsWith("google-") || p.includes("gemini") || m.includes("gemini");
|
|
1463
|
-
}
|
|
1464
|
-
function preserveAgentStateFields(source, target, options) {
|
|
1465
|
-
const mutableTarget = target;
|
|
1466
|
-
if (source.role === "assistant") {
|
|
1467
|
-
const shapingCounts = options?.shapingCounts;
|
|
1468
|
-
if (!options?.stripReasoningContent && typeof source.reasoning_content === "string") {
|
|
1469
|
-
mutableTarget.reasoning_content = source.reasoning_content;
|
|
1470
|
-
}
|
|
1471
|
-
if (Array.isArray(source.tool_calls)) {
|
|
1472
|
-
if (shapingCounts && source.tool_calls.length > 0) {
|
|
1473
|
-
shapingCounts.assistantToolTurns += 1;
|
|
1474
|
-
const hadUsableReasoning = typeof source.reasoning_content === "string" && source.reasoning_content.length > 0;
|
|
1475
|
-
if (options?.stripReasoningContent) {
|
|
1476
|
-
if (hadUsableReasoning) shapingCounts.reasoningStrippedTurns += 1;
|
|
1477
|
-
else shapingCounts.missingReasoningTurns += 1;
|
|
1478
|
-
} else if (!hadUsableReasoning) {
|
|
1479
|
-
shapingCounts.missingReasoningTurns += 1;
|
|
1480
|
-
}
|
|
1481
|
-
}
|
|
1482
|
-
const isExplicitTarget = Boolean(options?.targetProvider || options?.targetModel);
|
|
1483
|
-
const allowSignature = options?.allowThoughtSignature ?? (isExplicitTarget ? isGeminiCompatibleTarget(options?.targetProvider, options?.targetModel) : true);
|
|
1484
|
-
mutableTarget.tool_calls = source.tool_calls.map((call) => {
|
|
1485
|
-
if (!call || typeof call !== "object") return call;
|
|
1486
|
-
const cleaned = {
|
|
1487
|
-
id: call.id,
|
|
1488
|
-
type: call.type ?? "function",
|
|
1489
|
-
function: call.function
|
|
1490
|
-
};
|
|
1491
|
-
if (allowSignature) {
|
|
1492
|
-
if (call.extra_content !== void 0) cleaned.extra_content = call.extra_content;
|
|
1493
|
-
if (call.thought_signature !== void 0) cleaned.thought_signature = call.thought_signature;
|
|
1494
|
-
}
|
|
1495
|
-
return cleaned;
|
|
1496
|
-
});
|
|
1536
|
+
var extractReasoningFromResponseOutput = (response) => {
|
|
1537
|
+
const parts = [];
|
|
1538
|
+
for (const item of response?.output ?? []) {
|
|
1539
|
+
if (item?.type !== "reasoning")
|
|
1540
|
+
continue;
|
|
1541
|
+
if (typeof item.content === "string") {
|
|
1542
|
+
parts.push(item.content);
|
|
1543
|
+
continue;
|
|
1497
1544
|
}
|
|
1498
|
-
const
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
mutableTarget.reasoning_content = replayPlaceholder;
|
|
1504
|
-
if (shapingCounts) shapingCounts.placeholderInjectedTurns += 1;
|
|
1545
|
+
for (const content of item.content ?? []) {
|
|
1546
|
+
if (typeof content === "string")
|
|
1547
|
+
parts.push(content);
|
|
1548
|
+
else if (typeof content?.text === "string")
|
|
1549
|
+
parts.push(content.text);
|
|
1505
1550
|
}
|
|
1506
1551
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1552
|
+
return parts.join("");
|
|
1553
|
+
};
|
|
1554
|
+
var toDataUrl = (base64Data, mimeType) => {
|
|
1555
|
+
const normalizedMimeType = asOptionalTrimmedString(mimeType) ?? "image/png";
|
|
1556
|
+
return `data:${normalizedMimeType};base64,${base64Data}`;
|
|
1557
|
+
};
|
|
1558
|
+
var extractImagePartsFromResponseOutput = (response) => {
|
|
1559
|
+
const images = [];
|
|
1560
|
+
for (const item of response?.output ?? []) {
|
|
1561
|
+
if (item?.type === "image_generation_call") {
|
|
1562
|
+
const result = asOptionalTrimmedString(item.result);
|
|
1563
|
+
if (result) {
|
|
1564
|
+
const outputFormat = asOptionalTrimmedString(item.output_format);
|
|
1565
|
+
images.push({
|
|
1566
|
+
type: "image_url",
|
|
1567
|
+
image_url: {
|
|
1568
|
+
url: toDataUrl(result, outputFormat ? `image/${outputFormat}` : undefined)
|
|
1569
|
+
}
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
continue;
|
|
1510
1573
|
}
|
|
1511
|
-
if (
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1574
|
+
if (item?.type !== "message")
|
|
1575
|
+
continue;
|
|
1576
|
+
for (const content of item.content ?? []) {
|
|
1577
|
+
const result = asOptionalTrimmedString(content?.result);
|
|
1578
|
+
if (content?.type === "output_image" && result) {
|
|
1579
|
+
images.push({
|
|
1580
|
+
type: "image_url",
|
|
1581
|
+
image_url: {
|
|
1582
|
+
url: toDataUrl(result, content.mime_type ?? content.mimeType ?? null)
|
|
1583
|
+
}
|
|
1584
|
+
});
|
|
1585
|
+
}
|
|
1518
1586
|
}
|
|
1519
|
-
const toolCallId = typeof source.tool_call_id === "string" ? source.tool_call_id.trim() : typeof source.toolCallId === "string" ? source.toolCallId.trim() : "";
|
|
1520
|
-
if (toolCallId) mutableTarget.tool_call_id = toolCallId;
|
|
1521
1587
|
}
|
|
1522
|
-
return
|
|
1523
|
-
}
|
|
1524
|
-
|
|
1525
|
-
const
|
|
1526
|
-
const
|
|
1527
|
-
const
|
|
1528
|
-
for (const
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1588
|
+
return images;
|
|
1589
|
+
};
|
|
1590
|
+
var extractToolCallsFromResponseOutput = (response, options) => {
|
|
1591
|
+
const calls = [];
|
|
1592
|
+
const wireCalls = new Map;
|
|
1593
|
+
const projectedIds = new Set;
|
|
1594
|
+
for (const item of response?.output ?? []) {
|
|
1595
|
+
const isSearch = item?.type === "tool_search_call" && item.execution === "client";
|
|
1596
|
+
if (!isSearch && item?.type !== "function_call")
|
|
1597
|
+
continue;
|
|
1598
|
+
if (options?.strictSnapshot && (!asOptionalTrimmedString(item.call_id) || !isSearch && !asOptionalTrimmedString(item.name))) {
|
|
1599
|
+
throw new Error(`Responses completed snapshot contains a ${item.type} without a valid call_id or name.`);
|
|
1600
|
+
}
|
|
1601
|
+
let call;
|
|
1602
|
+
if (isSearch) {
|
|
1603
|
+
call = bridgeResponsesToolSearch(item);
|
|
1604
|
+
} else {
|
|
1605
|
+
if (!item.call_id || !item.name)
|
|
1606
|
+
continue;
|
|
1607
|
+
if (item.name === TOOL_DISCOVERY_CALL_NAME) {
|
|
1608
|
+
throw new Error("Responses used a reserved tool discovery name for a function call.");
|
|
1539
1609
|
}
|
|
1610
|
+
call = {
|
|
1611
|
+
id: item.call_id,
|
|
1612
|
+
type: "function",
|
|
1613
|
+
function: {
|
|
1614
|
+
name: item.name,
|
|
1615
|
+
arguments: typeof item.arguments === "string" ? item.arguments : JSON.stringify(item.arguments ?? {})
|
|
1616
|
+
}
|
|
1617
|
+
};
|
|
1540
1618
|
}
|
|
1541
|
-
if (
|
|
1542
|
-
const
|
|
1543
|
-
if (
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
} else {
|
|
1548
|
-
resultIds.add(id);
|
|
1549
|
-
if (!callIds.has(id)) issues.push(`orphan tool result id: ${id}`);
|
|
1619
|
+
if (options?.strictSnapshot) {
|
|
1620
|
+
const previous = wireCalls.get(item.call_id);
|
|
1621
|
+
if (previous) {
|
|
1622
|
+
if (previous.function.name === call.function.name && previous.function.arguments === call.function.arguments)
|
|
1623
|
+
continue;
|
|
1624
|
+
throw new Error("Responses returned a conflicting duplicate wire tool call ID.");
|
|
1550
1625
|
}
|
|
1626
|
+
if (projectedIds.has(call.id))
|
|
1627
|
+
throw new Error("Responses returned a conflicting duplicate projected tool call ID.");
|
|
1628
|
+
wireCalls.set(item.call_id, call);
|
|
1629
|
+
projectedIds.add(call.id);
|
|
1551
1630
|
}
|
|
1631
|
+
calls.push(call);
|
|
1552
1632
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
);
|
|
1565
|
-
}
|
|
1566
|
-
function shouldStripReasoningContentForOutbound(provider, model) {
|
|
1567
|
-
const p = provider?.trim().toLowerCase();
|
|
1568
|
-
const m = model?.trim().toLowerCase();
|
|
1569
|
-
if (!p || !m) return false;
|
|
1570
|
-
if (p === "deepseek" || p === "nolo") {
|
|
1571
|
-
return m === "deepseek-flash" || m === "deepseek-v4-flash" || m === "deepseek-v4-flash-vision-exp" || m === "deepseek-v4-pro";
|
|
1633
|
+
if (!options?.strictSnapshot && calls.some((call) => call.function.name === TOOL_DISCOVERY_CALL_NAME)) {
|
|
1634
|
+
const firstById = new Map;
|
|
1635
|
+
for (const call of calls) {
|
|
1636
|
+
const first = firstById.get(call.id);
|
|
1637
|
+
if (!first)
|
|
1638
|
+
firstById.set(call.id, call);
|
|
1639
|
+
else if (first.function.name !== call.function.name || first.function.arguments !== call.function.arguments) {
|
|
1640
|
+
throw new Error("Responses returned a conflicting duplicate tool call ID.");
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
return [...firstById.values()];
|
|
1572
1644
|
}
|
|
1573
|
-
return
|
|
1574
|
-
}
|
|
1575
|
-
var REASONING_REPLAY_PLACEHOLDER = "(reasoning not captured for this turn)";
|
|
1576
|
-
function shouldReplayReasoningContentForOutbound(provider, model) {
|
|
1577
|
-
const p = provider?.trim().toLowerCase();
|
|
1578
|
-
const m = model?.trim().toLowerCase();
|
|
1579
|
-
if (!p || !m) return false;
|
|
1580
|
-
return shouldStripReasoningContentForOutbound(p, m) || (p === "opencode-go" || p === "opencode") && m.includes("deepseek");
|
|
1581
|
-
}
|
|
1582
|
-
function resolveReasoningReplayOptions(provider, model, stripReasoningContent) {
|
|
1583
|
-
if (stripReasoningContent) return {};
|
|
1584
|
-
return shouldReplayReasoningContentForOutbound(provider, model) ? { replayReasoningPlaceholder: REASONING_REPLAY_PLACEHOLDER } : {};
|
|
1585
|
-
}
|
|
1645
|
+
return calls;
|
|
1646
|
+
};
|
|
1586
1647
|
|
|
1587
1648
|
// packages/integrations/openai/providerBodyCompatibility.ts
|
|
1588
1649
|
var MOONSHOT_KIMI_K3_MODEL = "kimi-k3";
|
|
@@ -1593,16 +1654,17 @@ var isKimiK3ProviderModel = (provider, model) => {
|
|
|
1593
1654
|
if (asTrimmedLowercaseString(provider) === "moonshot") {
|
|
1594
1655
|
return isFamilyId(normalizedModel);
|
|
1595
1656
|
}
|
|
1596
|
-
if (!isNoloHostedProvider(provider))
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
);
|
|
1657
|
+
if (!isNoloHostedProvider(provider))
|
|
1658
|
+
return false;
|
|
1659
|
+
if (isFamilyId(normalizedModel))
|
|
1660
|
+
return true;
|
|
1661
|
+
const effectiveModel = asTrimmedLowercaseString(resolvePlatformEffectiveModel(model) ?? model);
|
|
1601
1662
|
return isFamilyId(effectiveModel);
|
|
1602
1663
|
};
|
|
1603
1664
|
var isOllamaProviderOrEndpoint = (provider, endpoint) => {
|
|
1604
1665
|
const p = asTrimmedLowercaseString(provider);
|
|
1605
|
-
if (p === "ollama" || p === "ollama-cloud")
|
|
1666
|
+
if (p === "ollama" || p === "ollama-cloud")
|
|
1667
|
+
return true;
|
|
1606
1668
|
return isOllamaEndpoint(endpoint);
|
|
1607
1669
|
};
|
|
1608
1670
|
var applyFunctionToolReasoningCompatibility = (body, model) => {
|
|
@@ -1633,7 +1695,8 @@ var normalizeChatCompletionsBodyForProvider = ({
|
|
|
1633
1695
|
return {
|
|
1634
1696
|
...msg,
|
|
1635
1697
|
tool_calls: msg.tool_calls.map((tc) => {
|
|
1636
|
-
if (!tc || typeof tc !== "object")
|
|
1698
|
+
if (!tc || typeof tc !== "object")
|
|
1699
|
+
return tc;
|
|
1637
1700
|
const { id, type = "function", function: fn } = tc;
|
|
1638
1701
|
return { id, type, function: fn };
|
|
1639
1702
|
})
|
|
@@ -1659,9 +1722,9 @@ var normalizeChatCompletionsBodyForProvider = ({
|
|
|
1659
1722
|
}
|
|
1660
1723
|
if (isOllamaProviderOrEndpoint(provider, endpoint)) {
|
|
1661
1724
|
const existingNumCtx = nextBody.options?.num_ctx ?? nextBody.num_ctx;
|
|
1662
|
-
if (existingNumCtx ===
|
|
1725
|
+
if (existingNumCtx === undefined) {
|
|
1663
1726
|
const cw = getModelContextWindow(model);
|
|
1664
|
-
const safeNumCtx = typeof cw === "number" && cw > 0 ? cw ===
|
|
1727
|
+
const safeNumCtx = typeof cw === "number" && cw > 0 ? cw === 256000 ? 32768 : Math.min(cw, 131072) : 32768;
|
|
1665
1728
|
nextBody.options = {
|
|
1666
1729
|
...nextBody.options,
|
|
1667
1730
|
num_ctx: safeNumCtx
|
|
@@ -1674,53 +1737,4 @@ var normalizeChatCompletionsBodyForProvider = ({
|
|
|
1674
1737
|
return nextBody;
|
|
1675
1738
|
};
|
|
1676
1739
|
|
|
1677
|
-
export {
|
|
1678
|
-
EMPTY_TOOL_RESULT_TEXT,
|
|
1679
|
-
TOOL_HAD_IMAGES,
|
|
1680
|
-
hadToolImages,
|
|
1681
|
-
TOOL_IMAGES,
|
|
1682
|
-
toolImages,
|
|
1683
|
-
withoutToolImages,
|
|
1684
|
-
TOOL_IMAGE_NOTICES,
|
|
1685
|
-
createToolImageNotices,
|
|
1686
|
-
toolImageNotices,
|
|
1687
|
-
resetToolImageDelivery,
|
|
1688
|
-
toolImageDelivery,
|
|
1689
|
-
MAX_TOOL_IMAGES_PER_REQUEST,
|
|
1690
|
-
MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST,
|
|
1691
|
-
createToolImageBudget,
|
|
1692
|
-
toolImageSource,
|
|
1693
|
-
toolImageOmission,
|
|
1694
|
-
createToolImageLedger,
|
|
1695
|
-
stripToolImages,
|
|
1696
|
-
scanTruncation,
|
|
1697
|
-
classifyUnparsableToolArgs,
|
|
1698
|
-
markDowngradedToolTurnOrigin,
|
|
1699
|
-
transferDowngradedToolTurnOrigin,
|
|
1700
|
-
sanitizeForOutbound,
|
|
1701
|
-
renderToolResultAsText,
|
|
1702
|
-
hasParsableObjectArguments,
|
|
1703
|
-
repairTruncatedToolArguments,
|
|
1704
|
-
downgradeUnparsableToolCalls,
|
|
1705
|
-
getUsageRequestOptions,
|
|
1706
|
-
hasImageParts,
|
|
1707
|
-
requiresBareImageUrl,
|
|
1708
|
-
toBareImageUrlMessages,
|
|
1709
|
-
projectOpenAiToolImages,
|
|
1710
|
-
preserveAgentStateFields,
|
|
1711
|
-
findAgentStatePairingIssues,
|
|
1712
|
-
toOpenAiCompatibleMessages,
|
|
1713
|
-
shouldStripReasoningContentForOutbound,
|
|
1714
|
-
resolveReasoningReplayOptions,
|
|
1715
|
-
hasDeferrableResponsesTools,
|
|
1716
|
-
finishResponsesToolDiscovery,
|
|
1717
|
-
prepareResponsesToolDiscovery,
|
|
1718
|
-
toResponsesTools,
|
|
1719
|
-
convertMessagesToResponsesInput,
|
|
1720
|
-
extractTextFromResponseOutput,
|
|
1721
|
-
extractReasoningFromResponseOutput,
|
|
1722
|
-
extractImagePartsFromResponseOutput,
|
|
1723
|
-
extractToolCallsFromResponseOutput,
|
|
1724
|
-
applyFunctionToolReasoningCompatibility,
|
|
1725
|
-
normalizeChatCompletionsBodyForProvider
|
|
1726
|
-
};
|
|
1740
|
+
export { EMPTY_TOOL_RESULT_TEXT, TOOL_HAD_IMAGES, hadToolImages, TOOL_IMAGES, toolImages, withoutToolImages, TOOL_IMAGE_NOTICES, createToolImageNotices, toolImageNotices, resetToolImageDelivery, toolImageDelivery, MAX_TOOL_IMAGES_PER_REQUEST, MAX_TOOL_IMAGE_BASE64_BYTES_PER_REQUEST, createToolImageBudget, toolImageSource, toolImageOmission, createToolImageLedger, stripToolImages, scanTruncation, classifyUnparsableToolArgs, markDowngradedToolTurnOrigin, transferDowngradedToolTurnOrigin, sanitizeForOutbound, renderToolResultAsText, hasParsableObjectArguments, repairTruncatedToolArguments, downgradeUnparsableToolCalls, getUsageRequestOptions, hasImageParts, requiresBareImageUrl, toBareImageUrlMessages, projectOpenAiToolImages, preserveAgentStateFields, findAgentStatePairingIssues, toOpenAiCompatibleMessages, shouldStripReasoningContentForOutbound, resolveReasoningReplayOptions, hasDeferrableResponsesTools, finishResponsesToolDiscovery, prepareResponsesToolDiscovery, toResponsesTools, convertMessagesToResponsesInput, extractTextFromResponseOutput, extractReasoningFromResponseOutput, extractImagePartsFromResponseOutput, extractToolCallsFromResponseOutput, applyFunctionToolReasoningCompatibility, normalizeChatCompletionsBodyForProvider };
|