@eum-agent/llm-protocol-google 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +401 -215
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
//#region src/brand.ts
|
|
3
|
-
/** Official Gemini sparkle, supplied as self-contained SVG. */
|
|
4
4
|
const GEMINI_LOGO = "data:image/svg+xml,%3Csvg%20class%3D%22_footerSpark_dppzc_90%22%20width%3D%2264%22%20height%3D%2264%22%20viewBox%3D%220%200%2064%2064%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20d%3D%22M57.0667%2028.6103C52.1359%2026.4878%2047.8217%2023.576%2044.1223%2019.8784C40.4247%2016.1808%2037.5128%2011.8649%2035.3902%206.9342C34.5754%205.04449%2033.9206%203.10204%2033.4186%201.11049C33.2549%200.459368%2032.6711%200.0010376%2032%200.0010376C31.3288%200.0010376%2030.745%200.459368%2030.5813%201.11049C30.0793%203.10204%2029.4246%205.04267%2028.6097%206.9342C26.4872%2011.8649%2023.5753%2016.1808%2019.8777%2019.8784C16.18%2023.576%2011.864%2026.4878%206.93327%2028.6103C5.04353%2029.4251%203.10105%2030.0799%201.10947%2030.5819C0.458338%2030.7456%200%2031.3294%200%2032.0005C0%2032.6716%200.458338%2033.2555%201.10947%2033.4191C3.10105%2033.9211%205.04172%2034.5759%206.93327%2035.3907C11.864%2037.5132%2016.1782%2040.4251%2019.8777%2044.1226C23.5771%2047.8202%2026.4872%2052.1361%2028.6097%2057.0668C29.4246%2058.9565%2030.0793%2060.899%2030.5813%2062.8905C30.745%2063.5416%2031.3288%2064%2032%2064C32.6711%2064%2033.2549%2063.5416%2033.4186%2062.8905C33.9206%2060.899%2034.5754%2058.9583%2035.3902%2057.0668C37.5128%2052.1361%2040.4247%2047.822%2044.1223%2044.1226C47.8199%2040.4251%2052.1359%2037.5132%2057.0667%2035.3907C58.9564%2034.5759%2060.8989%2033.9211%2062.8905%2033.4191C63.5416%2033.2555%2064%2032.6716%2064%2032.0005C64%2031.3294%2063.5416%2030.7456%2062.8905%2030.5819C60.8989%2030.0799%2058.9582%2029.4251%2057.0667%2028.6103Z%22%20fill%3D%22white%22%3E%3C%2Fpath%3E%3Cmask%20id%3D%22mask0_10859_4894%22%20style%3D%22mask-type%3Aalpha%22%20maskUnits%3D%22userSpaceOnUse%22%20x%3D%220%22%20y%3D%220%22%20width%3D%2264%22%20height%3D%2264%22%3E%3Cpath%20d%3D%22M32%200C32.6711%201.144e-05%2033.2553%200.458263%2033.4189%201.10938C33.9209%203.10093%2034.5758%205.04389%2035.3906%206.93359C37.5131%2011.8639%2040.4247%2016.1796%2044.1221%2019.877C47.8215%2023.5745%2052.1357%2026.4869%2057.0664%2028.6094C58.958%2029.4242%2060.899%2030.0791%2062.8906%2030.5811C63.5415%2030.7448%2063.9998%2031.3281%2064%2031.999C64%2032.6701%2063.5417%2033.2542%2062.8906%2033.418C60.899%2033.9199%2058.9561%2034.5748%2057.0664%2035.3896C52.1358%2037.5121%2047.8196%2040.4237%2044.1221%2044.1211C40.4246%2047.8204%2037.5131%2052.1349%2035.3906%2057.0654C34.5758%2058.957%2033.9209%2060.8981%2033.4189%2062.8896C33.2552%2063.5407%2032.6711%2063.999%2032%2063.999C31.3289%2063.999%2030.7448%2063.5407%2030.5811%2062.8896C30.0791%2060.8981%2029.4242%2058.9551%2028.6094%2057.0654C26.4869%2052.1349%2023.5773%2047.8186%2019.8779%2044.1211C16.1786%2040.4237%2011.8642%2037.5121%206.93359%2035.3896C5.04204%2034.5748%203.10096%2033.9199%201.10938%2033.418C0.458309%2033.2542%200%2032.6701%200%2031.999C0.000201548%2031.3281%200.458463%2030.7448%201.10938%2030.5811C3.10096%2030.0791%205.04386%2029.4242%206.93359%2028.6094C11.8643%2026.4869%2016.1804%2023.5745%2019.8779%2019.877C23.5753%2016.1796%2026.4869%2011.8639%2028.6094%206.93359C29.4242%205.04207%2030.0791%203.10093%2030.5811%201.10938C30.7448%200.45826%2031.3289%200%2032%200Z%22%20fill%3D%22black%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22M32%200C32.6711%201.144e-05%2033.2553%200.458263%2033.4189%201.10938C33.9209%203.10093%2034.5758%205.04389%2035.3906%206.93359C37.5131%2011.8639%2040.4247%2016.1796%2044.1221%2019.877C47.8215%2023.5745%2052.1357%2026.4869%2057.0664%2028.6094C58.958%2029.4242%2060.899%2030.0791%2062.8906%2030.5811C63.5415%2030.7448%2063.9998%2031.3281%2064%2031.999C64%2032.6701%2063.5417%2033.2542%2062.8906%2033.418C60.899%2033.9199%2058.9561%2034.5748%2057.0664%2035.3896C52.1358%2037.5121%2047.8196%2040.4237%2044.1221%2044.1211C40.4246%2047.8204%2037.5131%2052.1349%2035.3906%2057.0654C34.5758%2058.957%2033.9209%2060.8981%2033.4189%2062.8896C33.2552%2063.5407%2032.6711%2063.999%2032%2063.999C31.3289%2063.999%2030.7448%2063.5407%2030.5811%2062.8896C30.0791%2060.8981%2029.4242%2058.9551%2028.6094%2057.0654C26.4869%2052.1349%2023.5773%2047.8186%2019.8779%2044.1211C16.1786%2040.4237%2011.8642%2037.5121%206.93359%2035.3896C5.04204%2034.5748%203.10096%2033.9199%201.10938%2033.418C0.458309%2033.2542%200%2032.6701%200%2031.999C0.000201548%2031.3281%200.458463%2030.7448%201.10938%2030.5811C3.10096%2030.0791%205.04386%2029.4242%206.93359%2028.6094C11.8643%2026.4869%2016.1804%2023.5745%2019.8779%2019.877C23.5753%2016.1796%2026.4869%2011.8639%2028.6094%206.93359C29.4242%205.04207%2030.0791%203.10093%2030.5811%201.10938C30.7448%200.45826%2031.3289%200%2032%200Z%22%20fill%3D%22url%28%23paint0_linear_10859_4894%29%22%3E%3C%2Fpath%3E%3C%2Fmask%3E%3Cg%20mask%3D%22url%28%23mask0_10859_4894%29%22%3E%3Cg%20filter%3D%22url%28%23filter0_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2214.2084%22%20cy%3D%2216.7164%22%20rx%3D%2214.2084%22%20ry%3D%2216.7164%22%20transform%3D%22matrix%280.942343%200.334649%20-0.334656%200.94234%20-7.979%2013.7735%29%22%20fill%3D%22%23FFE432%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter1_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2227.0543%22%20cy%3D%222.55114%22%20rx%3D%2218.3944%22%20ry%3D%2218.7985%22%20fill%3D%22%23FC413D%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter2_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2219.2245%22%20cy%3D%2224.9042%22%20rx%3D%2219.2245%22%20ry%3D%2224.9042%22%20transform%3D%22matrix%280.998807%20-0.0488254%200.0488266%200.998807%20-1.72778%2032.6573%29%22%20fill%3D%22%2300B95C%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter3_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2219.2245%22%20cy%3D%2224.9042%22%20rx%3D%2219.2245%22%20ry%3D%2224.9042%22%20transform%3D%22matrix%280.998807%20-0.0488254%200.0488266%200.998807%20-1.72778%2032.6573%29%22%20fill%3D%22%2300B95C%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter4_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2218.8429%22%20cy%3D%2220.7441%22%20rx%3D%2218.8429%22%20ry%3D%2220.7441%22%20transform%3D%22matrix%280.854301%20-0.519779%200.51979%200.854294%20-7.13574%2047.5078%29%22%20fill%3D%22%2300B95C%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter5_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2266.4617%22%20cy%3D%2224.977%22%20rx%3D%2218.0933%22%20ry%3D%2217.4229%22%20fill%3D%22%233186FF%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter6_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2220.9292%22%20cy%3D%2222.0752%22%20rx%3D%2220.9292%22%20ry%3D%2222.0752%22%20transform%3D%22matrix%280.79599%200.60531%20-0.60532%200.795982%20-2.81885%20-7.43323%29%22%20fill%3D%22%23FBBC04%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter7_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2224.1311%22%20cy%3D%2222.2919%22%20rx%3D%2224.1311%22%20ry%3D%2222.2919%22%20transform%3D%22matrix%280.824037%200.566536%20-0.566546%200.82403%2039.6338%200.310608%29%22%20fill%3D%22%233186FF%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter8_f_10859_4894%29%22%3E%3Cpath%20d%3D%22M54.2255%20-2.30403C57.0195%201.49462%2053.4294%208.8804%2046.2068%2014.1926C38.9842%2019.5048%2030.8642%2020.7318%2028.0702%2016.9331C25.2762%2013.1345%2028.8663%205.74867%2036.0889%200.436486C43.3115%20-4.8757%2051.4315%20-6.10267%2054.2255%20-2.30403Z%22%20fill%3D%22%23749BFF%22%3E%3C%2Fpath%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter9_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2227.5853%22%20cy%3D%2217.1478%22%20rx%3D%2227.5853%22%20ry%3D%2217.1478%22%20transform%3D%22matrix%280.733166%20-0.680049%200.680061%200.733155%20-12.2583%209.49695%29%22%20fill%3D%22%23FC413D%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3Cg%20filter%3D%22url%28%23filter10_f_10859_4894%29%22%3E%3Cellipse%20cx%3D%2214.7819%22%20cy%3D%228.59637%22%20rx%3D%2214.7819%22%20ry%3D%228.59637%22%20transform%3D%22matrix%280.813186%200.582004%20-0.582016%200.813177%206.37842%2030.511%29%22%20fill%3D%22%23FFEE48%22%3E%3C%2Fellipse%3E%3C%2Fg%3E%3C%2Fg%3E%3Cdefs%3E%3Cfilter%20id%3D%22filter0_f_10859_4894%22%20x%3D%22-19.618%22%20y%3D%2212.9027%22%20width%3D%2238.8681%22%20height%3D%2242.7562%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%222.45965%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter1_f_10859_4894%22%20x%3D%22-15.1223%22%20y%3D%22-40.0296%22%20width%3D%2284.3533%22%20height%3D%2285.1615%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%2211.8911%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter2_f_10859_4894%22%20x%3D%22-20.7682%22%20y%3D%2211.4835%22%20width%3D%2278.9161%22%20height%3D%2290.2196%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%2210.1086%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter3_f_10859_4894%22%20x%3D%22-20.7682%22%20y%3D%2211.4835%22%20width%3D%2278.9161%22%20height%3D%2290.2196%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%2210.1086%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter4_f_10859_4894%22%20x%3D%22-19.85%22%20y%3D%2214.9664%22%20width%3D%2279.1886%22%20height%3D%2280.9378%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%2210.1086%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter5_f_10859_4894%22%20x%3D%2229.1561%22%20y%3D%22-11.6582%22%20width%3D%2274.6111%22%20height%3D%2273.2703%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%229.60613%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter6_f_10859_4894%22%20x%3D%22-38.291%22%20y%3D%22-16.2687%22%20width%3D%2277.538%22%20height%3D%2278.1513%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%228.70591%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter7_f_10859_4894%22%20x%3D%227.78038%22%20y%3D%22-6.0981%22%20width%3D%2278.2181%22%20height%3D%2276.8982%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%227.77473%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter8_f_10859_4894%22%20x%3D%2213.2082%22%20y%3D%22-18.425%22%20width%3D%2255.8793%22%20height%3D%2251.4791%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%226.95694%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter9_f_10859_4894%22%20x%3D%22-15.4739%22%20y%3D%22-31.0272%22%20width%3D%2270.2034%22%20height%3D%2268.6735%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%225.87598%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3Cfilter%20id%3D%22filter10_f_10859_4894%22%20x%3D%22-14.173%22%20y%3D%2220.474%22%20width%3D%2255.1373%22%20height%3D%2251.261%22%20filterUnits%3D%22userSpaceOnUse%22%20color-interpolation-filters%3D%22sRGB%22%3E%3CfeFlood%20flood-opacity%3D%220%22%20result%3D%22BackgroundImageFix%22%3E%3C%2FfeFlood%3E%3CfeBlend%20mode%3D%22normal%22%20in%3D%22SourceGraphic%22%20in2%3D%22BackgroundImageFix%22%20result%3D%22shape%22%3E%3C%2FfeBlend%3E%3CfeGaussianBlur%20stdDeviation%3D%227.27253%22%20result%3D%22effect1_foregroundBlur_10859_4894%22%3E%3C%2FfeGaussianBlur%3E%3C%2Ffilter%3E%3ClinearGradient%20id%3D%22paint0_linear_10859_4894%22%20x1%3D%2218.1931%22%20y1%3D%2242.821%22%20x2%3D%2251.4335%22%20y2%3D%2214.7959%22%20gradientUnits%3D%22userSpaceOnUse%22%3E%3Cstop%20stop-color%3D%22%234893FC%22%3E%3C%2Fstop%3E%3Cstop%20offset%3D%220.27%22%20stop-color%3D%22%234893FC%22%3E%3C%2Fstop%3E%3Cstop%20offset%3D%220.776981%22%20stop-color%3D%22%23969DFF%22%3E%3C%2Fstop%3E%3Cstop%20offset%3D%221%22%20stop-color%3D%22%23BD99FE%22%3E%3C%2Fstop%3E%3C%2FlinearGradient%3E%3C%2Fdefs%3E%3C%2Fsvg%3E";
|
|
5
5
|
//#endregion
|
|
6
|
+
//#region src/connection-fetch.ts
|
|
7
|
+
function withConnectionHeaders(fetch, headers) {
|
|
8
|
+
const configured = Object.entries(headers ?? {});
|
|
9
|
+
if (configured.length === 0) return fetch;
|
|
10
|
+
return (input, init) => {
|
|
11
|
+
const merged = new Headers(input instanceof Request ? input.headers : void 0);
|
|
12
|
+
for (const [name, value] of new Headers(init?.headers)) merged.set(name, value);
|
|
13
|
+
for (const [name, value] of configured) merged.set(name, value);
|
|
14
|
+
return fetch(input, {
|
|
15
|
+
...init,
|
|
16
|
+
headers: merged
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
6
21
|
//#region src/facts.ts
|
|
7
|
-
/** Native Interactions limits and parameter spelling, independent of catalogue capacity. */
|
|
8
22
|
const facts = {
|
|
9
23
|
outputLimit: {
|
|
10
24
|
support: "optional",
|
|
@@ -19,15 +33,12 @@ const facts = {
|
|
|
19
33
|
};
|
|
20
34
|
//#endregion
|
|
21
35
|
//#region src/http.ts
|
|
22
|
-
/** Bounded HTTP primitives for the native Google wire. */
|
|
23
|
-
/** Resolves a resource beneath a configured API root and refuses credentials or inference paths. */
|
|
24
36
|
function googleUrl(base, path) {
|
|
25
37
|
if (typeof base !== "string") throw new Error("Google Base URL is required");
|
|
26
38
|
const url = new URL(base);
|
|
27
39
|
if (!["https:", "http:"].includes(url.protocol) || url.username || url.password || url.search || url.hash || /\/(interactions|models)(\/|$)|:generateContent$/.test(url.pathname)) throw new Error("Google Base URL must be an API root without credentials, query or inference path");
|
|
28
40
|
return `${url.href.replace(/\/+$/, "")}/${path}`;
|
|
29
41
|
}
|
|
30
|
-
/** Reads bounded UTF-8 JSON, rejecting byte overflow before allocating the complete response. */
|
|
31
42
|
async function readGoogleJson(response, maxBytes) {
|
|
32
43
|
if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) throw new Error("Invalid Google response byte budget");
|
|
33
44
|
const length = response.headers.get("content-length");
|
|
@@ -56,13 +67,23 @@ async function readGoogleJson(response, maxBytes) {
|
|
|
56
67
|
reader.releaseLock();
|
|
57
68
|
}
|
|
58
69
|
}
|
|
59
|
-
|
|
70
|
+
async function googleContextWindowExceeded(response) {
|
|
71
|
+
try {
|
|
72
|
+
if (response.status !== 400) return false;
|
|
73
|
+
const { body } = await readGoogleJson(response, 65536);
|
|
74
|
+
const message = body?.error?.message;
|
|
75
|
+
return typeof message === "string" && /input token count.*exceeds the maximum number of tokens/i.test(message);
|
|
76
|
+
} catch {
|
|
77
|
+
return false;
|
|
78
|
+
} finally {
|
|
79
|
+
await response.body?.cancel().catch(() => {});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
60
82
|
async function requireGoogleSuccess(response) {
|
|
61
83
|
if (response.ok) return;
|
|
62
84
|
await response.body?.cancel().catch(() => {});
|
|
63
85
|
throw Object.assign(/* @__PURE__ */ new Error(`Google request failed with HTTP ${response.status}`), { status: response.status });
|
|
64
86
|
}
|
|
65
|
-
/** Verifies lossless Base64 encoding before the host performs MIME and image decoding. */
|
|
66
87
|
function googleImageBytes(data) {
|
|
67
88
|
const encoded = z.string().min(1).parse(data);
|
|
68
89
|
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(encoded)) throw new Error("Google image data is not valid Base64");
|
|
@@ -70,9 +91,7 @@ function googleImageBytes(data) {
|
|
|
70
91
|
}
|
|
71
92
|
//#endregion
|
|
72
93
|
//#region src/policy.ts
|
|
73
|
-
/** Native protocol identity, shared by all supported Google model profiles. */
|
|
74
94
|
const GOOGLE_PROTOCOL = "gemini-interactions";
|
|
75
|
-
/** Built-in conversational metadata; new identifiers may use the same native protocol. */
|
|
76
95
|
const GOOGLE_CHAT_MODELS = [
|
|
77
96
|
"gemini-4-argon",
|
|
78
97
|
"gemini-3.8-flash",
|
|
@@ -88,7 +107,6 @@ const GOOGLE_CHAT_MODELS = [
|
|
|
88
107
|
"gemini-2.5-flash",
|
|
89
108
|
"gemini-2.5-flash-lite"
|
|
90
109
|
];
|
|
91
|
-
/** Built-in native image metadata, including ordinary text conversation support. */
|
|
92
110
|
const GOOGLE_IMAGE_MODELS = [
|
|
93
111
|
"gemini-nano-banana-2.1",
|
|
94
112
|
"gemini-3.1-flash-image",
|
|
@@ -97,7 +115,6 @@ const GOOGLE_IMAGE_MODELS = [
|
|
|
97
115
|
"nano-banana-pro-preview",
|
|
98
116
|
"gemini-2.5-flash-image"
|
|
99
117
|
];
|
|
100
|
-
/** Returns executable candidates for one exact upstream model id without network activity. */
|
|
101
118
|
function googleCandidates(model) {
|
|
102
119
|
if (GOOGLE_CHAT_MODELS.includes(model)) return [{
|
|
103
120
|
profile: "google-chat-v1",
|
|
@@ -116,7 +133,6 @@ function googleCandidates(model) {
|
|
|
116
133
|
chat: true
|
|
117
134
|
}];
|
|
118
135
|
}
|
|
119
|
-
/** Validates one explicitly selected native execution profile without trusting remote method names. */
|
|
120
136
|
function validateGoogleExecution(input) {
|
|
121
137
|
if (input.requested.protocolId !== "gemini-interactions" || !["google-chat-v1", "google-image-v1"].includes(input.requested.profile)) throw new Error("This Google execution profile is unsupported");
|
|
122
138
|
return {
|
|
@@ -124,7 +140,6 @@ function validateGoogleExecution(input) {
|
|
|
124
140
|
profile: input.requested.profile
|
|
125
141
|
};
|
|
126
142
|
}
|
|
127
|
-
/** Verified thinking levels for exact native models; unverified controls remain unavailable. */
|
|
128
143
|
function googleEfforts(model) {
|
|
129
144
|
if (model === "gemini-nano-banana-2.1") return [
|
|
130
145
|
"minimal",
|
|
@@ -156,57 +171,6 @@ function googleEfforts(model) {
|
|
|
156
171
|
if (model === "gemini-3.1-flash-lite") return ["high"];
|
|
157
172
|
return [];
|
|
158
173
|
}
|
|
159
|
-
/** Static facts from official model pages, usable before an authenticated catalogue refresh. */
|
|
160
|
-
function googleModelEntry(model) {
|
|
161
|
-
if (!GOOGLE_CHAT_MODELS.includes(model) && !GOOGLE_IMAGE_MODELS.includes(model)) return void 0;
|
|
162
|
-
if (GOOGLE_IMAGE_MODELS.includes(model)) return {
|
|
163
|
-
id: model,
|
|
164
|
-
chat: true,
|
|
165
|
-
tool_call: false,
|
|
166
|
-
attachment: true,
|
|
167
|
-
temperature: false,
|
|
168
|
-
...model === "gemini-nano-banana-2.1" ? {
|
|
169
|
-
reasoning: true,
|
|
170
|
-
reasoning_options: [{
|
|
171
|
-
type: "effort",
|
|
172
|
-
values: [...googleEfforts(model)]
|
|
173
|
-
}],
|
|
174
|
-
limit: {
|
|
175
|
-
input: 131072,
|
|
176
|
-
output: 32768
|
|
177
|
-
}
|
|
178
|
-
} : {},
|
|
179
|
-
modalities: {
|
|
180
|
-
input: ["text", "image"],
|
|
181
|
-
output: ["text", "image"]
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
const efforts = [...googleEfforts(model)];
|
|
185
|
-
return {
|
|
186
|
-
id: model,
|
|
187
|
-
chat: true,
|
|
188
|
-
reasoning: true,
|
|
189
|
-
tool_call: true,
|
|
190
|
-
attachment: true,
|
|
191
|
-
temperature: false,
|
|
192
|
-
reasoning_options: [...model === "gemini-2.5-flash-lite" ? [{ type: "toggle" }] : [], ...efforts.length ? [{
|
|
193
|
-
type: "effort",
|
|
194
|
-
values: efforts
|
|
195
|
-
}] : []],
|
|
196
|
-
modalities: {
|
|
197
|
-
input: ["text", "image"],
|
|
198
|
-
output: ["text"]
|
|
199
|
-
},
|
|
200
|
-
limit: model === "gemini-4-argon" ? {
|
|
201
|
-
context: 1048576,
|
|
202
|
-
output: 65536
|
|
203
|
-
} : {
|
|
204
|
-
input: 1048576,
|
|
205
|
-
output: 65536
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
/** Rejects unsupported native controls, including budget fields belonging to other Google APIs. */
|
|
210
174
|
function validateGoogleParams(model, params) {
|
|
211
175
|
const allowed = /* @__PURE__ */ new Set([
|
|
212
176
|
"thinking",
|
|
@@ -243,7 +207,6 @@ const wideRatios = [
|
|
|
243
207
|
"1:4",
|
|
244
208
|
"4:1"
|
|
245
209
|
];
|
|
246
|
-
/** Strict settings for known image backends; unknown models accept default settings only. */
|
|
247
210
|
function googleImageParamsSchema(model) {
|
|
248
211
|
if (!GOOGLE_IMAGE_MODELS.includes(model)) return z.strictObject({});
|
|
249
212
|
const base = {
|
|
@@ -273,7 +236,6 @@ function fail(code, resultState, message) {
|
|
|
273
236
|
resultState
|
|
274
237
|
});
|
|
275
238
|
}
|
|
276
|
-
/** Describes configured native Google image execution without network activity. */
|
|
277
239
|
function googleGenerationModels(input) {
|
|
278
240
|
return input.configuredModels.filter((row) => row.execution?.protocolId === "gemini-interactions" && ["google-chat-v1", "google-image-v1"].includes(row.execution.profile) && (row.capabilities?.imageGeneration === "supported" || row.capabilities?.imageEdit === "supported" || row.capabilities?.imageGeneration === void 0 && GOOGLE_IMAGE_MODELS.includes(row.id))).map((row) => ({
|
|
279
241
|
id: row.id,
|
|
@@ -294,7 +256,6 @@ function googleGenerationModels(input) {
|
|
|
294
256
|
}]]
|
|
295
257
|
}));
|
|
296
258
|
}
|
|
297
|
-
/** Creates a native image port; one invocation submits at most one generation request. */
|
|
298
259
|
function createGoogleGenerationPort(call, fetcher) {
|
|
299
260
|
return { async generate(request, signal) {
|
|
300
261
|
signal.throwIfAborted();
|
|
@@ -349,66 +310,92 @@ function createGoogleGenerationPort(call, fetcher) {
|
|
|
349
310
|
});
|
|
350
311
|
if (Buffer.byteLength(body) > 20971520) fail("too-large", "not-submitted", "The Google image request exceeds 20 MiB");
|
|
351
312
|
const url = googleUrl(call.config.baseUrl, "interactions");
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
method: "POST",
|
|
356
|
-
headers: {
|
|
357
|
-
"content-type": "application/json",
|
|
358
|
-
"x-goog-api-key": call.secrets.apiKey ?? ""
|
|
359
|
-
},
|
|
360
|
-
body,
|
|
361
|
-
signal,
|
|
362
|
-
redirect: "error"
|
|
363
|
-
});
|
|
364
|
-
} catch {
|
|
365
|
-
fail(signal.aborted ? "cancelled" : "network", "unknown", "The Google image submission result is unknown");
|
|
366
|
-
}
|
|
367
|
-
if (!response.ok) {
|
|
368
|
-
await response.body?.cancel().catch(() => {});
|
|
369
|
-
fail(response.status === 401 ? "credential-unavailable" : response.status === 403 ? "forbidden" : response.status === 429 ? "quota" : "provider", "not-delivered", `Google image generation failed with HTTP ${response.status}`);
|
|
370
|
-
}
|
|
313
|
+
const accounting = call.accountingDispatch?.attempt(0);
|
|
314
|
+
let outcome = "failed";
|
|
315
|
+
let submission = "unknown";
|
|
371
316
|
try {
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
else if (item.type === "image") {
|
|
387
|
-
const mediaType = z.enum([
|
|
388
|
-
"image/png",
|
|
389
|
-
"image/jpeg",
|
|
390
|
-
"image/webp"
|
|
391
|
-
]).parse(item.mime_type);
|
|
392
|
-
if (params.mimeType && mediaType !== params.mimeType) fail("invalid-output", "not-delivered", "Google returned an image in a different format");
|
|
393
|
-
const bytes = googleImageBytes(item.data);
|
|
394
|
-
outputBytes += bytes.byteLength;
|
|
395
|
-
if (outputBytes > 268435456 || assets.length >= 8) fail("too-large", "not-delivered", "Google image output exceeds its delivery budget");
|
|
396
|
-
assets.push({
|
|
397
|
-
mediaType,
|
|
398
|
-
body: (async function* () {
|
|
399
|
-
yield bytes;
|
|
400
|
-
})()
|
|
401
|
-
});
|
|
402
|
-
} else fail("invalid-output", "not-delivered", "Google returned an unsupported generation content type");
|
|
317
|
+
let response;
|
|
318
|
+
try {
|
|
319
|
+
response = await fetcher(url, {
|
|
320
|
+
method: "POST",
|
|
321
|
+
headers: {
|
|
322
|
+
"content-type": "application/json",
|
|
323
|
+
"x-goog-api-key": call.secrets.apiKey ?? ""
|
|
324
|
+
},
|
|
325
|
+
body,
|
|
326
|
+
signal,
|
|
327
|
+
redirect: "error"
|
|
328
|
+
});
|
|
329
|
+
} catch {
|
|
330
|
+
fail(signal.aborted ? "cancelled" : "network", "unknown", "The Google image submission result is unknown");
|
|
403
331
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
332
|
+
submission = "submitted";
|
|
333
|
+
if (!response.ok) {
|
|
334
|
+
await response.body?.cancel().catch(() => {});
|
|
335
|
+
fail(response.status === 401 ? "credential-unavailable" : response.status === 403 ? "forbidden" : response.status === 429 ? "quota" : "provider", "not-delivered", `Google image generation failed with HTTP ${response.status}`);
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
const raw = (await readGoogleJson(response, 402653184)).body;
|
|
339
|
+
const envelope = raw && typeof raw === "object" ? raw : void 0;
|
|
340
|
+
const usage = envelope?.usage && typeof envelope.usage === "object" ? envelope.usage : void 0;
|
|
341
|
+
const reportedUsage = usage ? {
|
|
342
|
+
...typeof usage.total_input_tokens === "number" ? { inputTokens: usage.total_input_tokens } : {},
|
|
343
|
+
...typeof usage.total_output_tokens === "number" && typeof usage.total_thought_tokens === "number" ? { outputTokens: usage.total_output_tokens + usage.total_thought_tokens } : {},
|
|
344
|
+
...typeof usage.total_tokens === "number" ? { totalTokens: usage.total_tokens } : {},
|
|
345
|
+
...typeof usage.total_cached_tokens === "number" ? { cacheReadTokens: usage.total_cached_tokens } : {}
|
|
346
|
+
} : void 0;
|
|
347
|
+
if (reportedUsage) accounting?.report({
|
|
348
|
+
sequence: 1,
|
|
349
|
+
...reportedUsage
|
|
350
|
+
});
|
|
351
|
+
const result = z.object({
|
|
352
|
+
status: z.literal("completed"),
|
|
353
|
+
steps: z.array(z.object({
|
|
354
|
+
type: z.string(),
|
|
355
|
+
content: z.array(z.object({ type: z.string() }).passthrough()).optional()
|
|
356
|
+
}))
|
|
357
|
+
}).parse(raw);
|
|
358
|
+
const assets = [];
|
|
359
|
+
const text = [];
|
|
360
|
+
let outputBytes = 0;
|
|
361
|
+
for (const step of result.steps) {
|
|
362
|
+
if (step.type === "thought") continue;
|
|
363
|
+
if (step.type !== "model_output") fail("invalid-output", "not-delivered", "Google returned an unsupported generation step");
|
|
364
|
+
for (const item of step.content ?? []) if (item.type === "text") text.push(z.string().parse(item.text));
|
|
365
|
+
else if (item.type === "image") {
|
|
366
|
+
const mediaType = z.enum([
|
|
367
|
+
"image/png",
|
|
368
|
+
"image/jpeg",
|
|
369
|
+
"image/webp"
|
|
370
|
+
]).parse(item.mime_type);
|
|
371
|
+
if (params.mimeType && mediaType !== params.mimeType) fail("invalid-output", "not-delivered", "Google returned an image in a different format");
|
|
372
|
+
const bytes = googleImageBytes(item.data);
|
|
373
|
+
outputBytes += bytes.byteLength;
|
|
374
|
+
if (outputBytes > 268435456 || assets.length >= 8) fail("too-large", "not-delivered", "Google image output exceeds its delivery budget");
|
|
375
|
+
assets.push({
|
|
376
|
+
mediaType,
|
|
377
|
+
body: (async function* () {
|
|
378
|
+
yield bytes;
|
|
379
|
+
})()
|
|
380
|
+
});
|
|
381
|
+
} else fail("invalid-output", "not-delivered", "Google returned an unsupported generation content type");
|
|
382
|
+
}
|
|
383
|
+
if (!assets.length) fail("invalid-output", "not-delivered", "Google returned no image");
|
|
384
|
+
outcome = "success";
|
|
385
|
+
return {
|
|
386
|
+
assets,
|
|
387
|
+
...text.length ? { text: text.join("\n") } : {},
|
|
388
|
+
...reportedUsage ? { usage: reportedUsage } : {}
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (error?.name === "GenerationFailure") throw error;
|
|
392
|
+
fail(signal.aborted ? "cancelled" : "invalid-output", "not-delivered", "The Google image response is incomplete or invalid");
|
|
393
|
+
}
|
|
394
|
+
} finally {
|
|
395
|
+
accounting?.end({
|
|
396
|
+
outcome: signal.aborted ? "cancelled" : outcome,
|
|
397
|
+
submission
|
|
398
|
+
});
|
|
412
399
|
}
|
|
413
400
|
} };
|
|
414
401
|
}
|
|
@@ -424,8 +411,7 @@ function imageBlocks(file, fingerprint) {
|
|
|
424
411
|
data: fingerprint ? file.ref.forModel?.id ?? file.ref.id : file.data ?? ""
|
|
425
412
|
}] : []];
|
|
426
413
|
}
|
|
427
|
-
|
|
428
|
-
function googleSteps(request, plan, fingerprint = false, replay, toolHistory = true) {
|
|
414
|
+
function googleSteps(request, plan, fingerprint = false, replay, toolHistory = true, onReplay) {
|
|
429
415
|
const steps = [];
|
|
430
416
|
const callNames = /* @__PURE__ */ new Map();
|
|
431
417
|
const note = (notes) => {
|
|
@@ -443,7 +429,14 @@ function googleSteps(request, plan, fingerprint = false, replay, toolHistory = t
|
|
|
443
429
|
const signatures = replay?.(messageIndex);
|
|
444
430
|
let content = [];
|
|
445
431
|
let outputSignature;
|
|
432
|
+
let outputPartIndex = -1;
|
|
446
433
|
const flush = () => {
|
|
434
|
+
if (content.length && outputSignature) onReplay?.({
|
|
435
|
+
messageIndex,
|
|
436
|
+
partIndex: outputPartIndex,
|
|
437
|
+
kind: "output",
|
|
438
|
+
signature: outputSignature
|
|
439
|
+
});
|
|
447
440
|
if (content.length) steps.push({
|
|
448
441
|
type: message.role === "user" ? "user_input" : "model_output",
|
|
449
442
|
content,
|
|
@@ -457,10 +450,17 @@ function googleSteps(request, plan, fingerprint = false, replay, toolHistory = t
|
|
|
457
450
|
if (signedOutput?.kind === "output") {
|
|
458
451
|
flush();
|
|
459
452
|
outputSignature = signedOutput.signature;
|
|
453
|
+
outputPartIndex = partIndex;
|
|
460
454
|
}
|
|
461
455
|
if (part.type === "thinking") {
|
|
462
456
|
const signature = signatures?.get(partIndex)?.signature;
|
|
463
457
|
if (signature) {
|
|
458
|
+
onReplay?.({
|
|
459
|
+
messageIndex,
|
|
460
|
+
partIndex,
|
|
461
|
+
kind: "thought",
|
|
462
|
+
signature
|
|
463
|
+
});
|
|
464
464
|
flush();
|
|
465
465
|
steps.push({
|
|
466
466
|
type: "thought",
|
|
@@ -529,13 +529,12 @@ function googleSteps(request, plan, fingerprint = false, replay, toolHistory = t
|
|
|
529
529
|
note(request.volatile);
|
|
530
530
|
return steps;
|
|
531
531
|
}
|
|
532
|
-
|
|
533
|
-
function buildGoogleRequest(call, request, support, fingerprint = false) {
|
|
532
|
+
function buildGoogleRequest(call, request, support, fingerprint = false, onReplay) {
|
|
534
533
|
const selected = call.configuredModels.find((row) => row.id === call.model)?.execution;
|
|
535
534
|
if (selected?.protocolId !== call.protocolId || !["google-chat-v1", "google-image-v1"].includes(selected?.profile ?? "")) throw new Error("Google conversation execution profile is not configured");
|
|
536
535
|
validateGoogleParams(call.model, request.params ?? {});
|
|
537
536
|
const row = call.configuredModels.find((one) => one.id === call.model);
|
|
538
|
-
const entry = support.catalogue(call.providerId, call.model)
|
|
537
|
+
const entry = support.catalogue(call.providerId, call.model);
|
|
539
538
|
const params = support.resolveParams({
|
|
540
539
|
facts,
|
|
541
540
|
entry,
|
|
@@ -559,21 +558,22 @@ function buildGoogleRequest(call, request, support, fingerprint = false) {
|
|
|
559
558
|
const wantedEffort = typeof request.params?.effort === "string" ? request.params.effort : params.effort;
|
|
560
559
|
const effort = request.params?.thinking === false || wantedEffort === "none" || wantedEffort === "" ? void 0 : wantedEffort ?? (call.model === "gemini-2.5-flash-lite" && request.params?.thinking === true ? "low" : void 0);
|
|
561
560
|
const toolsSupported = row?.capabilities?.toolCalling === "supported" || row?.capabilities?.toolCalling === void 0 && entry?.tool_call === true;
|
|
562
|
-
const
|
|
561
|
+
const standaloneSummary = request.purpose === "summary" && request.summaryShape !== "continuation";
|
|
562
|
+
const allowTools = toolsSupported && !standaloneSummary;
|
|
563
563
|
const system = [
|
|
564
564
|
...request.system ?? [],
|
|
565
|
-
...nativeImages(call) &&
|
|
565
|
+
...nativeImages(call, support) && !standaloneSummary ? ["Use native image output for image requests; do not call another image tool unless a different target is explicitly requested. If the user explicitly requests links only, emit [[image-display:link]] with the image. The host supplies its real file link after saving."] : [],
|
|
566
566
|
...!toolsSupported ? ["The current model does not support tool calling. Do not claim to execute tools or fabricate tool results."] : []
|
|
567
567
|
];
|
|
568
568
|
const imageParams = request.purpose === "summary" ? void 0 : request.params?.nativeImage;
|
|
569
|
-
if (imageParams !== void 0 && !nativeImages(call)) throw new Error("Native image settings require image output support");
|
|
569
|
+
if (imageParams !== void 0 && !nativeImages(call, support)) throw new Error("Native image settings require image output support");
|
|
570
570
|
const image = imageParams === void 0 ? void 0 : googleImageParamsSchema(call.model).parse(imageParams);
|
|
571
571
|
return {
|
|
572
572
|
body: {
|
|
573
573
|
model: call.model,
|
|
574
|
-
stream: !nativeImages(call),
|
|
574
|
+
stream: !nativeImages(call, support),
|
|
575
575
|
store: false,
|
|
576
|
-
input: googleSteps(request, plan, fingerprint, (index) => replaySignatures(call, request, index, support), allowTools),
|
|
576
|
+
input: googleSteps(request, plan, fingerprint, (index) => replaySignatures(call, request, index, support), allowTools, onReplay),
|
|
577
577
|
...system.length ? { system_instruction: system.join("\n\n") } : {},
|
|
578
578
|
...allowTools && request.tools?.length ? { tools: request.tools.map((tool) => ({
|
|
579
579
|
type: "function",
|
|
@@ -615,10 +615,6 @@ const Replay = z.strictObject({
|
|
|
615
615
|
endPart: z.number().int().nonnegative().optional()
|
|
616
616
|
}))
|
|
617
617
|
});
|
|
618
|
-
/**
|
|
619
|
-
* Binds signature replay to retained input/output history. System instructions and tools are
|
|
620
|
-
* interaction-scoped in the native API and may change between decisions without rewriting history.
|
|
621
|
-
*/
|
|
622
618
|
function googlePrefix(call, request, support) {
|
|
623
619
|
const plan = support.planMedia({
|
|
624
620
|
...request,
|
|
@@ -632,7 +628,6 @@ function googlePrefix(call, request, support) {
|
|
|
632
628
|
input: googleSteps(request, plan, true)
|
|
633
629
|
});
|
|
634
630
|
}
|
|
635
|
-
/** Fingerprints the transcript fields a signed response covers, without host-assigned metadata. */
|
|
636
631
|
function googleResponseFingerprint(content, support) {
|
|
637
632
|
return support.inspectionFingerprint(content.map((part) => {
|
|
638
633
|
if (part.type !== "file") return part;
|
|
@@ -655,7 +650,6 @@ function replaySignatures(call, request, index, support) {
|
|
|
655
650
|
}, support) !== parsed.data.prefixFingerprint) return void 0;
|
|
656
651
|
return new Map(parsed.data.signatures.map((one) => [one.part, one]));
|
|
657
652
|
}
|
|
658
|
-
/** Projects only readable native request fields; opaque signatures and image bytes are never text. */
|
|
659
653
|
function googleRequestText(body) {
|
|
660
654
|
const texts = [];
|
|
661
655
|
if (body.system_instruction) texts.push(body.system_instruction);
|
|
@@ -674,7 +668,6 @@ function googleRequestText(body) {
|
|
|
674
668
|
}
|
|
675
669
|
return texts.join("\n");
|
|
676
670
|
}
|
|
677
|
-
/** Counts opaque retained thought tokens only when their full replay binding is still valid. */
|
|
678
671
|
function googleHiddenInputTokens(call, request, support) {
|
|
679
672
|
return request.messages.reduce((total, message, index) => {
|
|
680
673
|
if (!replaySignatures(call, request, index, support)) return total;
|
|
@@ -682,24 +675,20 @@ function googleHiddenInputTokens(call, request, support) {
|
|
|
682
675
|
return total + (parsed.success ? parsed.data.thoughtTokens : 0);
|
|
683
676
|
}, 0);
|
|
684
677
|
}
|
|
685
|
-
|
|
686
|
-
function nativeImages(call) {
|
|
678
|
+
function nativeImages(call, support) {
|
|
687
679
|
const capabilities = call.configuredModels.find((one) => one.id === call.model)?.capabilities;
|
|
688
|
-
return capabilities === void 0 ?
|
|
680
|
+
return capabilities === void 0 ? support.catalogue(call.providerId, call.model)?.modalities?.output?.includes("image") === true : capabilities.imageGeneration === "supported" || capabilities.imageEdit === "supported";
|
|
689
681
|
}
|
|
690
682
|
//#endregion
|
|
691
683
|
//#region src/display.ts
|
|
692
|
-
/** Removes an invocation-local presentation marker without leaking split chunks to the user. */
|
|
693
684
|
function imageDisplayParser() {
|
|
694
685
|
const marker = "[[image-display:link]]";
|
|
695
686
|
let pending = "";
|
|
696
687
|
let link = false;
|
|
697
688
|
return {
|
|
698
|
-
/** Whether any complete marker requested link-only delivery for this response. */
|
|
699
689
|
get link() {
|
|
700
690
|
return link;
|
|
701
691
|
},
|
|
702
|
-
/** Returns safe visible text, retaining at most one marker prefix between chunks. */
|
|
703
692
|
push(text, flush = false) {
|
|
704
693
|
pending += text;
|
|
705
694
|
let output = "";
|
|
@@ -740,7 +729,6 @@ const Start = z.object({
|
|
|
740
729
|
text: z.string()
|
|
741
730
|
})).optional()
|
|
742
731
|
});
|
|
743
|
-
/** Parses a framed SSE response and cancels its reader when the consumer stops. */
|
|
744
732
|
async function* googleEvents(response) {
|
|
745
733
|
if (!response.body) throw new Error("Google response has no stream");
|
|
746
734
|
const reader = response.body.getReader();
|
|
@@ -770,8 +758,7 @@ async function* googleEvents(response) {
|
|
|
770
758
|
reader.releaseLock();
|
|
771
759
|
}
|
|
772
760
|
}
|
|
773
|
-
|
|
774
|
-
async function* mapGoogleStream(events, call, support, prefixFingerprint, maxTokens, outputSink, signal = new AbortController().signal) {
|
|
761
|
+
async function* mapGoogleStream(events, call, support, prefixFingerprint, maxTokens, outputSink, signal = new AbortController().signal, usageReporter) {
|
|
775
762
|
const steps = /* @__PURE__ */ new Map();
|
|
776
763
|
let imageCount = 0;
|
|
777
764
|
let imageBytes = 0;
|
|
@@ -807,13 +794,26 @@ async function* mapGoogleStream(events, call, support, prefixFingerprint, maxTok
|
|
|
807
794
|
});
|
|
808
795
|
};
|
|
809
796
|
let latestUsage;
|
|
797
|
+
let usageSequence = 0;
|
|
798
|
+
const reportUsage = () => {
|
|
799
|
+
if (!latestUsage) return;
|
|
800
|
+
usageReporter?.report({
|
|
801
|
+
sequence: ++usageSequence,
|
|
802
|
+
...latestUsage.total_input_tokens === void 0 ? {} : { inputTokens: latestUsage.total_input_tokens },
|
|
803
|
+
...latestUsage.total_output_tokens === void 0 || latestUsage.total_thought_tokens === void 0 ? {} : { outputTokens: latestUsage.total_output_tokens + latestUsage.total_thought_tokens },
|
|
804
|
+
...latestUsage.total_cached_tokens === void 0 ? {} : { cacheReadTokens: latestUsage.total_cached_tokens }
|
|
805
|
+
});
|
|
806
|
+
};
|
|
810
807
|
for await (const event of events) {
|
|
811
808
|
if (event.metadata !== void 0) {
|
|
812
809
|
const metadata = z.object({ total_usage: Usage.partial().optional() }).parse(event.metadata);
|
|
813
|
-
if (metadata.total_usage !== void 0)
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
810
|
+
if (metadata.total_usage !== void 0) {
|
|
811
|
+
latestUsage = {
|
|
812
|
+
...latestUsage,
|
|
813
|
+
...metadata.total_usage
|
|
814
|
+
};
|
|
815
|
+
reportUsage();
|
|
816
|
+
}
|
|
817
817
|
}
|
|
818
818
|
if (event.event_type === "step.start") {
|
|
819
819
|
const index = z.number().int().nonnegative().parse(event.index);
|
|
@@ -915,12 +915,14 @@ async function* mapGoogleStream(events, call, support, prefixFingerprint, maxTok
|
|
|
915
915
|
]),
|
|
916
916
|
usage: Usage.partial().optional()
|
|
917
917
|
}).parse(event.interaction);
|
|
918
|
+
latestUsage = {
|
|
919
|
+
...latestUsage,
|
|
920
|
+
...final.usage
|
|
921
|
+
};
|
|
922
|
+
reportUsage();
|
|
918
923
|
const interaction = {
|
|
919
924
|
...final,
|
|
920
|
-
usage: Usage.parse(
|
|
921
|
-
...latestUsage,
|
|
922
|
-
...final.usage
|
|
923
|
-
})
|
|
925
|
+
usage: Usage.parse(latestUsage)
|
|
924
926
|
};
|
|
925
927
|
const usedOutput = interaction.usage.total_output_tokens + interaction.usage.total_thought_tokens;
|
|
926
928
|
if (!Number.isSafeInteger(usedOutput)) throw new Error("Google usage exceeds integer capacity");
|
|
@@ -1018,7 +1020,6 @@ async function* mapGoogleStream(events, call, support, prefixFingerprint, maxTok
|
|
|
1018
1020
|
}
|
|
1019
1021
|
throw support.interruptedFailure();
|
|
1020
1022
|
}
|
|
1021
|
-
/** Normalizes bounded nonstreaming native replies through the same ordered assembler. */
|
|
1022
1023
|
async function* googleJsonEvents(response) {
|
|
1023
1024
|
const interaction = z.object({
|
|
1024
1025
|
status: z.string(),
|
|
@@ -1043,33 +1044,158 @@ async function* googleJsonEvents(response) {
|
|
|
1043
1044
|
}
|
|
1044
1045
|
//#endregion
|
|
1045
1046
|
//#region src/port.ts
|
|
1046
|
-
/** Creates a stateless Google port; request construction and inspection perform no network calls. */
|
|
1047
1047
|
function createGooglePort(call, support, fetcher, options = {}) {
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1048
|
+
function prepare(request) {
|
|
1049
|
+
const emitted = /* @__PURE__ */ new Map();
|
|
1050
|
+
const raw = buildGoogleRequest(call, request, support, false, (one) => {
|
|
1051
|
+
const group = emitted.get(one.messageIndex) ?? [];
|
|
1052
|
+
group.push(one);
|
|
1053
|
+
emitted.set(one.messageIndex, group);
|
|
1054
|
+
});
|
|
1055
|
+
const replaySelections = support.prepareReplaySelections(request, ({ message, messageIndex, part }) => {
|
|
1056
|
+
const emissions = emitted.get(messageIndex);
|
|
1057
|
+
if (!part) return {
|
|
1058
|
+
mode: "google-native",
|
|
1059
|
+
textIncluded: false,
|
|
1060
|
+
group: message.id,
|
|
1061
|
+
...emissions?.length ? { selectedText: JSON.stringify(emissions.map(({ partIndex, kind, signature }) => ({
|
|
1062
|
+
partIndex,
|
|
1063
|
+
kind,
|
|
1064
|
+
signature
|
|
1065
|
+
}))) } : { reason: "not-applicable" }
|
|
1066
|
+
};
|
|
1067
|
+
const included = emissions?.some((one) => one.kind === "thought" && message.content[one.partIndex] === part) === true;
|
|
1068
|
+
return {
|
|
1069
|
+
mode: "google-native-summary",
|
|
1070
|
+
textIncluded: included && part.text !== "",
|
|
1071
|
+
group: message.id,
|
|
1072
|
+
reason: included ? "covered-by-native" : "not-applicable"
|
|
1073
|
+
};
|
|
1074
|
+
});
|
|
1075
|
+
const built = {
|
|
1076
|
+
...raw,
|
|
1077
|
+
body: support.freezePayload(raw.body),
|
|
1078
|
+
capacity: support.freezePayload(raw.capacity ?? {})
|
|
1079
|
+
};
|
|
1080
|
+
const fields = /* @__PURE__ */ new Map();
|
|
1081
|
+
let mediaIndex = 0;
|
|
1082
|
+
const block = (item) => {
|
|
1083
|
+
if ((item.type === "thought" || item.type === "model_output") && typeof item.signature === "string") fields.set(item, /* @__PURE__ */ new Map([["signature", {
|
|
1084
|
+
kind: "replay",
|
|
1085
|
+
value: JSON.stringify({
|
|
1086
|
+
sha256: createHash("sha256").update(item.signature).digest("hex"),
|
|
1087
|
+
byteLength: Buffer.byteLength(item.signature),
|
|
1088
|
+
mode: `google-${item.type}`,
|
|
1089
|
+
model: built.body.model
|
|
1090
|
+
})
|
|
1091
|
+
}]]));
|
|
1092
|
+
if (item.type === "image") {
|
|
1093
|
+
const file = built.plan.sent[mediaIndex++];
|
|
1094
|
+
if (!file || item.data !== (file.data ?? "")) throw new Error("prepared_media_selection_mismatch");
|
|
1095
|
+
const data = file.data ?? "", hash = createHash("sha256");
|
|
1096
|
+
for (let offset = 0; offset < data.length; offset += 65536) hash.update(data.slice(offset, offset + 65536), "base64");
|
|
1097
|
+
fields.set(item, /* @__PURE__ */ new Map([["data", {
|
|
1098
|
+
kind: "media",
|
|
1099
|
+
value: JSON.stringify({
|
|
1100
|
+
sha256: hash.digest("hex"),
|
|
1101
|
+
byteLength: Buffer.byteLength(data, "base64"),
|
|
1102
|
+
variantId: file.variantId,
|
|
1103
|
+
mediaType: file.mediaType,
|
|
1104
|
+
width: file.width,
|
|
1105
|
+
height: file.height
|
|
1106
|
+
})
|
|
1107
|
+
}]]));
|
|
1108
|
+
}
|
|
1109
|
+
for (const key of ["content", "result"]) if (Array.isArray(item[key])) {
|
|
1110
|
+
for (const child of item[key]) if (child && typeof child === "object") block(child);
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
for (const step of built.body.input) block(step);
|
|
1114
|
+
if (mediaIndex !== built.plan.sent.length) throw new Error("prepared_media_selection_mismatch");
|
|
1115
|
+
return {
|
|
1116
|
+
...built,
|
|
1117
|
+
replaySelections,
|
|
1118
|
+
fields,
|
|
1119
|
+
request: {
|
|
1120
|
+
...request,
|
|
1121
|
+
params: support.freezePayload(request.params ?? {})
|
|
1122
|
+
},
|
|
1123
|
+
prefix: googlePrefix(call, request, support),
|
|
1124
|
+
hiddenInputTokens: googleHiddenInputTokens(call, request, support),
|
|
1125
|
+
fingerprint: support.freezePayload({
|
|
1126
|
+
protocol: call.protocolId,
|
|
1127
|
+
connection: call.connectionFingerprint,
|
|
1128
|
+
model: call.model,
|
|
1129
|
+
settings: built.body.generation_config
|
|
1130
|
+
})
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
return support.createPreparedPort({
|
|
1134
|
+
prepare,
|
|
1135
|
+
semantics(payload) {
|
|
1136
|
+
return {
|
|
1137
|
+
replaySelections: payload.replaySelections,
|
|
1138
|
+
mediaSelections: () => support.mediaSelections(payload.plan),
|
|
1139
|
+
format: "request-semantic-v1",
|
|
1140
|
+
protocolId: call.protocolId,
|
|
1141
|
+
adapterVersion: "1",
|
|
1142
|
+
rendererVersion: "google-interactions-v1",
|
|
1143
|
+
parameters: Object.freeze(Object.fromEntries(Object.entries(payload.body).filter(([key]) => ![
|
|
1144
|
+
"model",
|
|
1145
|
+
"input",
|
|
1146
|
+
"system_instruction",
|
|
1147
|
+
"tools",
|
|
1148
|
+
"stream",
|
|
1149
|
+
"store"
|
|
1150
|
+
].includes(key)))),
|
|
1151
|
+
toolDefinitions: payload.body.tools ?? Object.freeze([]),
|
|
1152
|
+
frames: () => support.semanticFrames(payload.body, {
|
|
1153
|
+
omitRoot: ["stream", "store"],
|
|
1154
|
+
fields: payload.fields
|
|
1155
|
+
})
|
|
1156
|
+
};
|
|
1157
|
+
},
|
|
1158
|
+
async inspect(payload, signal) {
|
|
1159
|
+
signal.throwIfAborted();
|
|
1160
|
+
const started = support.performance?.now();
|
|
1161
|
+
const { body, plan, capacity, maxTokens, request, hiddenInputTokens, fingerprint } = payload;
|
|
1162
|
+
support.performance?.observe({
|
|
1163
|
+
stage: "model.project",
|
|
1164
|
+
measurementPurpose: "request",
|
|
1165
|
+
durationMs: support.performance.now() - started,
|
|
1166
|
+
count: 1
|
|
1167
|
+
});
|
|
1168
|
+
const serialization = support.performance?.now();
|
|
1169
|
+
const text = googleRequestText(body);
|
|
1170
|
+
support.performance?.observe({
|
|
1171
|
+
stage: "model.serialize",
|
|
1172
|
+
measurementPurpose: "request",
|
|
1173
|
+
durationMs: support.performance.now() - serialization,
|
|
1174
|
+
count: 1
|
|
1175
|
+
});
|
|
1176
|
+
const inspection = support.inspect({
|
|
1052
1177
|
body,
|
|
1053
1178
|
request,
|
|
1054
1179
|
capacity,
|
|
1055
|
-
textTokens:
|
|
1056
|
-
|
|
1180
|
+
textTokens: 0,
|
|
1181
|
+
textBytes: Buffer.byteLength(text),
|
|
1182
|
+
tokenizer: "utf8-bytes-quarter-v1",
|
|
1057
1183
|
mediaTokens: plan.tokens,
|
|
1058
|
-
hiddenInputTokens
|
|
1184
|
+
hiddenInputTokens,
|
|
1059
1185
|
outputReserve: maxTokens ?? capacity?.output ?? 0,
|
|
1060
1186
|
outputReserveSource: maxTokens === void 0 ? "capacity" : "enforced",
|
|
1061
|
-
fingerprint
|
|
1062
|
-
protocol: call.protocolId,
|
|
1063
|
-
connection: call.connectionFingerprint,
|
|
1064
|
-
model: call.model,
|
|
1065
|
-
settings: body.generation_config,
|
|
1066
|
-
capacity
|
|
1067
|
-
}
|
|
1187
|
+
fingerprint
|
|
1068
1188
|
});
|
|
1189
|
+
const textTokens = await support.measureText(text, signal);
|
|
1190
|
+
signal.throwIfAborted();
|
|
1191
|
+
return {
|
|
1192
|
+
...inspection,
|
|
1193
|
+
inputTokens: inspection.inputTokens + textTokens
|
|
1194
|
+
};
|
|
1069
1195
|
},
|
|
1070
|
-
async *stream(
|
|
1196
|
+
async *stream(built, signal, observer, scope) {
|
|
1071
1197
|
signal.throwIfAborted();
|
|
1072
|
-
const
|
|
1198
|
+
const { request, prefix } = built;
|
|
1073
1199
|
const body = JSON.stringify(built.body);
|
|
1074
1200
|
if (Buffer.byteLength(body) > 20971520) throw support.failures.create({
|
|
1075
1201
|
code: "request-too-large",
|
|
@@ -1079,12 +1205,18 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1079
1205
|
retryable: false
|
|
1080
1206
|
});
|
|
1081
1207
|
const url = googleUrl(call.config.baseUrl, "interactions");
|
|
1082
|
-
const prefix = googlePrefix(call, request, support);
|
|
1083
1208
|
yield* support.withChatAttempts({
|
|
1209
|
+
retainCleanup: scope.retainCleanup,
|
|
1210
|
+
beforeAttempt: (_signal, ordinal) => observer?.beforeAttempt?.(ordinal),
|
|
1211
|
+
accountingDispatch: call.accountingDispatch,
|
|
1212
|
+
effective: {
|
|
1213
|
+
outputLimit: built.body.generation_config.max_output_tokens,
|
|
1214
|
+
effort: built.body.generation_config.thinking_level
|
|
1215
|
+
},
|
|
1084
1216
|
signal,
|
|
1085
1217
|
...options,
|
|
1086
|
-
...nativeImages(call) ? { maxRetries: 0 } : {},
|
|
1087
|
-
async *attempt(attemptSignal, progress, attempt) {
|
|
1218
|
+
...nativeImages(call, support) ? { maxRetries: 0 } : {},
|
|
1219
|
+
async *attempt(attemptSignal, progress, attempt, usageReporter) {
|
|
1088
1220
|
const count = attempt + 1;
|
|
1089
1221
|
const observe = (event) => {
|
|
1090
1222
|
try {
|
|
@@ -1104,11 +1236,45 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1104
1236
|
let firstResponse = false;
|
|
1105
1237
|
let ended = false;
|
|
1106
1238
|
let outcome = "unknown";
|
|
1239
|
+
let timeout;
|
|
1240
|
+
const abortedOutcome = () => {
|
|
1241
|
+
const reason = support.failures.verified(attemptSignal.reason);
|
|
1242
|
+
if (reason?.code !== "stream-idle-timeout") return "cancelled";
|
|
1243
|
+
timeout = reason;
|
|
1244
|
+
return "timeout";
|
|
1245
|
+
};
|
|
1246
|
+
let unconsumed;
|
|
1247
|
+
const markDispatched = () => {
|
|
1248
|
+
try {
|
|
1249
|
+
observer?.dispatched({
|
|
1250
|
+
attempt,
|
|
1251
|
+
presentation: {
|
|
1252
|
+
schema: "chat.dispatch-settings.v1",
|
|
1253
|
+
data: {
|
|
1254
|
+
model: {
|
|
1255
|
+
availability: "resolved",
|
|
1256
|
+
value: built.body.model,
|
|
1257
|
+
origin: "protocol"
|
|
1258
|
+
},
|
|
1259
|
+
effort: built.body.generation_config.thinking_level ? {
|
|
1260
|
+
availability: "resolved",
|
|
1261
|
+
value: built.body.generation_config.thinking_level,
|
|
1262
|
+
origin: "protocol"
|
|
1263
|
+
} : {
|
|
1264
|
+
availability: "resolved",
|
|
1265
|
+
value: "default",
|
|
1266
|
+
origin: "protocol"
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
});
|
|
1271
|
+
} catch {}
|
|
1272
|
+
};
|
|
1107
1273
|
try {
|
|
1108
1274
|
attemptSignal.throwIfAborted();
|
|
1109
1275
|
let response;
|
|
1110
1276
|
try {
|
|
1111
|
-
|
|
1277
|
+
const pending = fetcher(url, {
|
|
1112
1278
|
method: "POST",
|
|
1113
1279
|
headers: {
|
|
1114
1280
|
"content-type": "application/json",
|
|
@@ -1118,6 +1284,9 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1118
1284
|
signal: attemptSignal,
|
|
1119
1285
|
redirect: "error"
|
|
1120
1286
|
});
|
|
1287
|
+
markDispatched();
|
|
1288
|
+
response = await pending;
|
|
1289
|
+
unconsumed = response.body;
|
|
1121
1290
|
} catch {
|
|
1122
1291
|
attemptSignal.throwIfAborted();
|
|
1123
1292
|
throw support.failures.create({
|
|
@@ -1128,6 +1297,7 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1128
1297
|
retryable: true
|
|
1129
1298
|
});
|
|
1130
1299
|
}
|
|
1300
|
+
attemptSignal.throwIfAborted();
|
|
1131
1301
|
firstResponse = true;
|
|
1132
1302
|
observe({
|
|
1133
1303
|
stage: "first-response",
|
|
@@ -1136,9 +1306,19 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1136
1306
|
outcome: "ok"
|
|
1137
1307
|
});
|
|
1138
1308
|
if (!response.ok) {
|
|
1139
|
-
await response
|
|
1140
|
-
|
|
1309
|
+
const exceeded = await googleContextWindowExceeded(response);
|
|
1310
|
+
attemptSignal.throwIfAborted();
|
|
1311
|
+
const retryAfterHeader = response.headers.get("retry-after")?.trim();
|
|
1312
|
+
const retryAfter = retryAfterHeader ? Number(retryAfterHeader) : NaN;
|
|
1141
1313
|
const status = response.status;
|
|
1314
|
+
if (exceeded) throw support.failures.create({
|
|
1315
|
+
code: "context-window-exceeded",
|
|
1316
|
+
stage: "request",
|
|
1317
|
+
category: "request-too-large",
|
|
1318
|
+
source: "protocol",
|
|
1319
|
+
status,
|
|
1320
|
+
retryable: false
|
|
1321
|
+
});
|
|
1142
1322
|
const failure = support.failures.create({
|
|
1143
1323
|
code: status === 413 ? "request-too-large" : "http-error",
|
|
1144
1324
|
stage: "transport",
|
|
@@ -1163,15 +1343,13 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1163
1343
|
retryable: false
|
|
1164
1344
|
});
|
|
1165
1345
|
}
|
|
1166
|
-
try {
|
|
1167
|
-
observer?.dispatched({ attempt });
|
|
1168
|
-
} catch {}
|
|
1169
1346
|
const observed = support.observeResponseProgress(response, progress);
|
|
1347
|
+
unconsumed = void 0;
|
|
1170
1348
|
let final;
|
|
1171
|
-
for await (const chunk of mapGoogleStream(response.headers.get("content-type")?.toLowerCase().includes("text/event-stream") ? googleEvents(observed) : googleJsonEvents(observed), call, support, prefix, built.maxTokens, request.outputSink, attemptSignal)) {
|
|
1349
|
+
for await (const chunk of mapGoogleStream(response.headers.get("content-type")?.toLowerCase().includes("text/event-stream") ? googleEvents(observed) : googleJsonEvents(observed), call, support, prefix, built.maxTokens, request.outputSink, attemptSignal, usageReporter)) {
|
|
1172
1350
|
progress();
|
|
1173
1351
|
if (!built.allowTools && (chunk.kind === "tool-call-delta" || chunk.kind === "final" && chunk.message.content.some((part) => part.type === "tool-use"))) throw new Error("Google returned unsupported tool calls");
|
|
1174
|
-
if (chunk.kind === "final" && nativeImages(call) && chunk.finishReason === "max-tokens") throw new Error("Truncated native image output cannot be automatically continued");
|
|
1352
|
+
if (chunk.kind === "final" && nativeImages(call, support) && chunk.finishReason === "max-tokens") throw new Error("Truncated native image output cannot be automatically continued");
|
|
1175
1353
|
if (chunk.kind === "final") {
|
|
1176
1354
|
final = chunk;
|
|
1177
1355
|
break;
|
|
@@ -1190,20 +1368,28 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1190
1368
|
yield final;
|
|
1191
1369
|
}
|
|
1192
1370
|
} catch (error) {
|
|
1193
|
-
outcome = attemptSignal.aborted ?
|
|
1371
|
+
outcome = attemptSignal.aborted ? abortedOutcome() : "error";
|
|
1194
1372
|
throw error;
|
|
1195
1373
|
} finally {
|
|
1374
|
+
if (unconsumed) {
|
|
1375
|
+
const body = unconsumed;
|
|
1376
|
+
scope.retainCleanup(async () => {
|
|
1377
|
+
await body.cancel();
|
|
1378
|
+
}).catch(() => {});
|
|
1379
|
+
}
|
|
1196
1380
|
if (!firstResponse) observe({
|
|
1197
1381
|
stage: "first-response",
|
|
1198
1382
|
phase: "end",
|
|
1199
1383
|
attempt: count,
|
|
1200
|
-
outcome
|
|
1384
|
+
outcome,
|
|
1385
|
+
...timeout ? { failure: timeout } : {}
|
|
1201
1386
|
});
|
|
1202
1387
|
if (!ended) observe({
|
|
1203
1388
|
stage: "attempt",
|
|
1204
1389
|
phase: "end",
|
|
1205
1390
|
attempt: count,
|
|
1206
|
-
outcome
|
|
1391
|
+
outcome,
|
|
1392
|
+
...timeout ? { failure: timeout } : {}
|
|
1207
1393
|
});
|
|
1208
1394
|
}
|
|
1209
1395
|
},
|
|
@@ -1226,7 +1412,7 @@ function createGooglePort(call, support, fetcher, options = {}) {
|
|
|
1226
1412
|
}
|
|
1227
1413
|
});
|
|
1228
1414
|
}
|
|
1229
|
-
};
|
|
1415
|
+
});
|
|
1230
1416
|
}
|
|
1231
1417
|
//#endregion
|
|
1232
1418
|
//#region src/probe.ts
|
|
@@ -1235,7 +1421,11 @@ const Page = z.object({
|
|
|
1235
1421
|
name: z.string().min(1),
|
|
1236
1422
|
displayName: z.string().optional(),
|
|
1237
1423
|
inputTokenLimit: z.number().int().positive().optional(),
|
|
1238
|
-
outputTokenLimit: z.number().int().positive().optional()
|
|
1424
|
+
outputTokenLimit: z.number().int().positive().optional(),
|
|
1425
|
+
thinking: z.boolean().optional(),
|
|
1426
|
+
temperature: z.number().optional(),
|
|
1427
|
+
maxTemperature: z.number().optional(),
|
|
1428
|
+
supportedGenerationMethods: z.array(z.string()).optional()
|
|
1239
1429
|
})).default([]),
|
|
1240
1430
|
nextPageToken: z.string().optional()
|
|
1241
1431
|
});
|
|
@@ -1256,7 +1446,6 @@ async function pageOf(probe, fetcher, token, remaining, signal) {
|
|
|
1256
1446
|
bytes: result.bytes
|
|
1257
1447
|
};
|
|
1258
1448
|
}
|
|
1259
|
-
/** Reads every native model page within one 30-second, 1000-model, 20-page byte budget. */
|
|
1260
1449
|
async function listGoogleModels(probe, fetcher) {
|
|
1261
1450
|
const offers = /* @__PURE__ */ new Map();
|
|
1262
1451
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -1270,19 +1459,15 @@ async function listGoogleModels(probe, fetcher) {
|
|
|
1270
1459
|
const model = row.name.replace(/^models\//, "");
|
|
1271
1460
|
if (!model || offers.has(model)) continue;
|
|
1272
1461
|
const execution = googleCandidates(model);
|
|
1273
|
-
const
|
|
1462
|
+
const sampled = row.temperature !== void 0 || row.maxTemperature !== void 0;
|
|
1274
1463
|
offers.set(model, {
|
|
1275
1464
|
model,
|
|
1276
1465
|
...row.displayName === void 0 ? {} : { name: row.displayName },
|
|
1277
|
-
...row.inputTokenLimit === void 0 ? {} : {
|
|
1466
|
+
...row.inputTokenLimit === void 0 ? {} : { inputLimit: row.inputTokenLimit },
|
|
1278
1467
|
...row.outputTokenLimit === void 0 ? {} : { maxOutput: row.outputTokenLimit },
|
|
1279
|
-
...
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
toolCall: entry.tool_call,
|
|
1283
|
-
reasoning: entry.reasoning,
|
|
1284
|
-
temperature: entry.temperature
|
|
1285
|
-
} : {},
|
|
1468
|
+
...row.thinking === void 0 ? {} : { reasoning: row.thinking },
|
|
1469
|
+
...sampled ? { temperature: true } : {},
|
|
1470
|
+
...row.supportedGenerationMethods === void 0 ? {} : { chat: row.supportedGenerationMethods.includes("generateContent") },
|
|
1286
1471
|
execution
|
|
1287
1472
|
});
|
|
1288
1473
|
if (offers.size > 1e3) throw new Error("Google model listing exceeds 1000 models");
|
|
@@ -1295,17 +1480,19 @@ async function listGoogleModels(probe, fetcher) {
|
|
|
1295
1480
|
}
|
|
1296
1481
|
throw new Error("Google model listing exceeds 20 pages");
|
|
1297
1482
|
}
|
|
1298
|
-
/** Tests authentication with one read-only page; no model inference is submitted. */
|
|
1299
1483
|
async function testGoogleConnection(probe, fetcher) {
|
|
1300
1484
|
await pageOf(probe, fetcher, void 0, probe.maxResponseBytes ?? 8388608, AbortSignal.any([probe.signal, AbortSignal.timeout(3e4)]));
|
|
1301
1485
|
}
|
|
1302
1486
|
//#endregion
|
|
1303
1487
|
//#region src/support.ts
|
|
1304
|
-
|
|
1488
|
+
function inputLimit(offer) {
|
|
1489
|
+
return Number.isSafeInteger(offer.inputLimit) && Number(offer.inputLimit) > 0 ? offer.inputLimit : void 0;
|
|
1490
|
+
}
|
|
1305
1491
|
function normalizeOffers(input) {
|
|
1306
1492
|
return Object.fromEntries(input.offers.map((offer) => [offer.model, {
|
|
1307
1493
|
id: offer.model,
|
|
1308
1494
|
...offer.name === void 0 ? {} : { name: offer.name },
|
|
1495
|
+
...offer.chat === void 0 ? {} : { chat: offer.chat },
|
|
1309
1496
|
...offer.reasoning === void 0 && !offer.efforts?.length ? {} : { reasoning: offer.efforts?.length ? true : offer.reasoning },
|
|
1310
1497
|
...offer.efforts?.length ? { reasoning_options: [{
|
|
1311
1498
|
type: "effort",
|
|
@@ -1318,14 +1505,14 @@ function normalizeOffers(input) {
|
|
|
1318
1505
|
...offer.input === void 0 ? {} : { input: [...offer.input] },
|
|
1319
1506
|
...offer.output === void 0 ? {} : { output: [...offer.output] }
|
|
1320
1507
|
} },
|
|
1321
|
-
...!offer.contextWindow && !offer.maxOutput ? {} : { limit: {
|
|
1508
|
+
...!offer.contextWindow && inputLimit(offer) === void 0 && !offer.maxOutput ? {} : { limit: {
|
|
1322
1509
|
...offer.contextWindow && offer.contextWindow > 0 ? { context: offer.contextWindow } : {},
|
|
1510
|
+
...inputLimit(offer) === void 0 ? {} : { input: inputLimit(offer) },
|
|
1323
1511
|
...offer.maxOutput && offer.maxOutput > 0 ? { output: offer.maxOutput } : {}
|
|
1324
1512
|
} },
|
|
1325
1513
|
...offer.releaseDate === void 0 ? {} : { release_date: offer.releaseDate }
|
|
1326
1514
|
}]));
|
|
1327
1515
|
}
|
|
1328
|
-
/** The owner-qualified accessor is read per operation; withdrawal never exposes an old service. */
|
|
1329
1516
|
function lazySupport(read) {
|
|
1330
1517
|
return new Proxy({}, { get(_target, key) {
|
|
1331
1518
|
if (key === "normalizeListing") return normalizeOffers;
|
|
@@ -1337,7 +1524,6 @@ function lazySupport(read) {
|
|
|
1337
1524
|
}
|
|
1338
1525
|
//#endregion
|
|
1339
1526
|
//#region src/protocol.ts
|
|
1340
|
-
/** Builds native capabilities using host support and a routed fetch; construction makes no requests. */
|
|
1341
1527
|
function googleProtocolType(support, fetcher) {
|
|
1342
1528
|
return {
|
|
1343
1529
|
type: "gemini-interactions",
|
|
@@ -1390,32 +1576,32 @@ function googleProtocolType(support, fetcher) {
|
|
|
1390
1576
|
chat: {
|
|
1391
1577
|
fileKinds: ["image"],
|
|
1392
1578
|
toolCalling(model, input) {
|
|
1393
|
-
|
|
1579
|
+
const toolCall = support.catalogue(input.providerId, model)?.tool_call;
|
|
1580
|
+
return input.configuredModels.find((row) => row.id === model)?.capabilities?.toolCalling ?? (toolCall === true ? "supported" : toolCall === false ? "unsupported" : "unknown");
|
|
1394
1581
|
},
|
|
1395
1582
|
nativeOutputKinds(model, input) {
|
|
1396
1583
|
const capabilities = input.configuredModels.find((row) => row.id === model)?.capabilities;
|
|
1397
1584
|
const state = capabilities?.imageGeneration;
|
|
1398
|
-
return state === "supported" || capabilities?.imageEdit === "supported" || state === void 0 &&
|
|
1585
|
+
return state === "supported" || capabilities?.imageEdit === "supported" || state === void 0 && support.catalogue(input.providerId, model)?.modalities?.output?.includes("image") ? ["image"] : [];
|
|
1399
1586
|
},
|
|
1400
1587
|
nativeImageParamsSchema: (model) => googleImageParamsSchema(model),
|
|
1401
1588
|
supportsModel(model, input) {
|
|
1402
1589
|
return input.configuredModels.some((row) => row.id === model && row.execution?.protocolId === "gemini-interactions" && ["google-chat-v1", "google-image-v1"].includes(row.execution.profile) && row.capabilities?.chat !== "unsupported");
|
|
1403
1590
|
},
|
|
1404
|
-
createPort: (call) => createGooglePort(call, support, fetcher)
|
|
1591
|
+
createPort: (call) => createGooglePort(call, support, withConnectionHeaders(fetcher, call.headers))
|
|
1405
1592
|
},
|
|
1406
1593
|
image: {
|
|
1407
1594
|
models: googleGenerationModels,
|
|
1408
|
-
createPort: (call) => createGoogleGenerationPort(call, fetcher)
|
|
1595
|
+
createPort: (call) => createGoogleGenerationPort(call, withConnectionHeaders(fetcher, call.headers))
|
|
1409
1596
|
}
|
|
1410
1597
|
},
|
|
1411
1598
|
normalizeListing: (input) => normalizeOffers(input),
|
|
1412
|
-
listModels: (probe) => listGoogleModels(probe, fetcher),
|
|
1413
|
-
testConnection: (probe) => testGoogleConnection(probe, fetcher)
|
|
1599
|
+
listModels: (probe) => listGoogleModels(probe, withConnectionHeaders(fetcher, probe.headers)),
|
|
1600
|
+
testConnection: (probe) => testGoogleConnection(probe, withConnectionHeaders(fetcher, probe.headers))
|
|
1414
1601
|
};
|
|
1415
1602
|
}
|
|
1416
1603
|
//#endregion
|
|
1417
1604
|
//#region src/index.ts
|
|
1418
|
-
/** Protocol owners contribute no Agent tools. */
|
|
1419
1605
|
const TOOL_NAMES = [];
|
|
1420
1606
|
const plugin = {
|
|
1421
1607
|
name: "@eum-agent/llm-protocol-google",
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"en-US": "Google Interactions Protocol",
|
|
5
5
|
"zh-CN": "模型协议 · Gemini"
|
|
6
6
|
},
|
|
7
|
-
"version": "1.
|
|
7
|
+
"version": "1.2.0",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"description": "Native Google Gemini Interactions protocol",
|
|
10
10
|
"license": "MIT",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"assets"
|
|
19
19
|
],
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@eum-agent/plugin": "1.
|
|
21
|
+
"@eum-agent/plugin": "1.2.0"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"zod": "^4.4.3"
|
|
@@ -29,11 +29,11 @@
|
|
|
29
29
|
"zh-CN": "模型协议 · Gemini"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@eum-agent/llm-management": "1.
|
|
32
|
+
"@eum-agent/llm-management": "1.2.0"
|
|
33
33
|
},
|
|
34
34
|
"kind": "extension",
|
|
35
35
|
"foundationDependencies": {
|
|
36
|
-
"@eum-agent/infra-local": "1.0
|
|
36
|
+
"@eum-agent/infra-local": "1.2.0"
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|