Gemini AI Stream Parser

A reusable function to parse streaming JSON responses from the Google Gemini API.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/538628/1613479/Gemini%20AI%20Stream%20Parser.js

  1. window.parseGeminiStream = async function(reader, onChunk) { //Processes the Gemini API stream
  2. for (let buffer = '', partialMarkdown = '';;) { //Loop indefinitely to process the stream
  3. const { value, done } = await reader.read(); //Read the next chunk from the stream
  4. if (done) break; //Exit the loop if the stream is finished
  5. buffer += new TextDecoder().decode(value, { stream: true }); //Decode binary chunk to string and append to buffer
  6. for (var startIdx = 0, openBrace, balance, closeBrace, item, inString; (openBrace = buffer.indexOf('{', startIdx)) !== -1;) { //Find the start of a JSON object
  7. for (balance = 1, closeBrace = openBrace + 1, inString = false; balance && closeBrace < buffer.length; closeBrace++) { //Find the corresponding end of the JSON object
  8. if (buffer[closeBrace] === '"' && buffer[closeBrace - 1] !== '\\') inString = !inString; //Toggle string state on unescaped quotes to handle code blocks with {} symbols in JSON
  9. if (!inString) balance += (buffer[closeBrace] === '{') - (buffer[closeBrace] === '}'); //Count braces only outside strings to avoid breaking on code symbols like {}
  10. }
  11. if (balance) break; //If the object is incomplete, wait for more chunks
  12. item = JSON.parse(buffer.substring(openBrace, closeBrace)); //Parse the complete JSON object
  13. partialMarkdown += item.candidates?.[0]?.content?.parts?.[0]?.text || ''; //Append the text from the current chunk
  14. onChunk(item, partialMarkdown); //Execute the callback with the parsed data and accumulated text
  15. startIdx = closeBrace; //Move the starting position for the next search
  16. }
  17. buffer = buffer.substring(startIdx); //Discard the processed part of the buffer
  18. }
  19. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址