Files
2026-08-17 13:59:12 +08:00

8 lines
2.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use strict";const p=require("../../common/vendor.js");function k(e,n){const r=String(e||"").replace(/\/+$/,""),t=String(n||"").replace(/^\/+/,"");return t?`${r}/${t}`:r}function v(e,n){if(!n||typeof n!="object")return e;const r=Object.keys(n);if(!r.length)return e;const t=r.map(c=>`${encodeURIComponent(c)}=${encodeURIComponent(n[c]??"")}`).join("&");return e+(e.includes("?")?"&":"?")+t}function m(e){if(typeof e=="string")return e;if(!e)return"";if(typeof TextDecoder<"u"&&e instanceof ArrayBuffer)return new TextDecoder("utf-8").decode(new Uint8Array(e));if(e instanceof ArrayBuffer){const n=new Uint8Array(e);let r="";for(let t=0;t<n.length;t++)r+=String.fromCharCode(n[t]);try{return decodeURIComponent(escape(r))}catch{return r}}return""}function h(e,n){let r=e,t=r.indexOf(`
`);for(;t!==-1;){const c=r.slice(0,t);r=r.slice(t+2);let o="message",i="";if(c.split(`
`).forEach(s=>{s.startsWith("event:")?o=s.slice(6).trim():s.startsWith("data:")&&(i+=s.slice(5).trim())}),i)try{n(o,JSON.parse(i))}catch{n(o,i)}t=r.indexOf(`
`)}return r}function w(e){const n={};if(!e)return n;const r=/^(早餐|喝的|午餐|晚餐|提示|少碰)[:]\s*(.*)$/gm;let t;for(;(t=r.exec(e))!==null;){const o={早餐:"breakfast",喝的:"drinks",午餐:"lunch",晚餐:"dinner",提示:"tips",少碰:"avoid"}[t[1]],i=(t[2]||"").trim();o==="avoid"?n.avoid=i.split(/[、,;\s]+/).map(s=>s.trim()).filter(Boolean):n[o]=i}const c=e.match(/(?:^|\n)(早餐|喝的|午餐|晚餐|提示|少碰)[:]\s*([^\n]*)$/);if(c){const o={早餐:"breakfast",喝的:"drinks",午餐:"lunch",晚餐:"dinner",提示:"tips",少碰:"avoid"}[c[1]],i=(c[2]||"").trim();o==="avoid"?i&&(n.avoid=i.split(/[、,;\s]+/).map(s=>s.trim()).filter(Boolean)):n[o]=i}return n}function C(e){const{baseUrl:n,url:r,method:t="GET",data:c={},onEvent:o,fallback:i}=e;return new Promise((s,l)=>{const y=p.index.getStorageSync("token")||"";let f="",u=k(n,r);t==="GET"&&(u=v(u,c));const d=p.wx$1.request({url:u,method:t,data:t==="POST"?c:{},enableChunked:!0,timeout:6e4,header:{token:y,"content-type":"application/x-www-form-urlencoded","Cache-Control":"no-cache"},success:a=>{a&&a.data&&(f+=m(a.data),f=h(f,o)),s(a)},fail:a=>l(a)});d&&typeof d.onChunkReceived=="function"?d.onChunkReceived(a=>{f+=m(a.data),f=h(f,o)}):typeof i=="function"?i().then(s).catch(l):l(new Error("当前环境不支持流式请求"))})}exports.parsePartialDietPlainText=w;exports.requestAiStream=C;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/tongji/utils/aiStreamRequest.js.map