function e(){l||n(c.id)||(r({id:c.id,factory:()=>new d},{setActive:!0}),l=!0)}import{i as t,f as s,g as a,a as o,b4 as n,b5 as r}from"./platform-core.tlhrBXt0.js";const i=[{id:"fleekdash-default",name:"FleekDash Default Model",description:"Default chat model routed through the FleekDash AI endpoint.",capabilities:{chat:!0,streaming:!0,embeddings:!1,tools:!1,image:!1,audio:!1}}],c={id:"fleekdash-http-gateway",name:t("FleekDash HTTP Gateway"),version:"1.0.0",description:t("Routes chat requests through the legacy /ai/chat endpoint."),icon:"message-square",capabilities:{chat:!0,streaming:!0,embeddings:!1,tools:!1,systemInstructions:!0,structuredOutputs:!1},defaultModel:i[0].id,models:i};class d{metadata=c;async listModels(){return this.metadata.models??i}async sendChat(e){const t=this.transformRequest(e);t.stream=!1;const a=await s.post("/wp-json/fleekdash/v1/ai/chat",t);return this.transformResponse(a,e)}async streamChat(e,s={}){const{signal:a,onDelta:o,onComplete:n,onError:r}=s;try{const t=await this.createStreamingRequest(e,a);if(t.body&&"function"==typeof t.body.getReader){const s=await this.readSseStream(t,e,o);return void n?.(s)}if("function"==typeof t.json){const s=await t.json();return void n?.(this.transformResponse(s,e))}n?.(this.transformResponse(t,e))}catch(i){const e=i instanceof Error?i:new Error(t("Failed to stream chat response"));throw r?.(e),e}}transformRequest(e){const t=[];if(e.history&&e.history.length>0)for(const a of e.history)"user"!==a.role&&"assistant"!==a.role&&"system"!==a.role||t.push({role:a.role,content:a.content});t.push({role:"user",content:e.message});const s={messages:t,stream:!1};return e.options&&Object.assign(s,e.options),s}transformResponse(e,t){if(!e||"object"!=typeof e)return{message:"",conversationId:t.conversationId??"",raw:e};const s=e;return{message:this.extractMessage(s),conversationId:s.conversationId??s.conversation_id??t.conversationId??"",usage:s.usage,raw:e}}async createStreamingRequest(e,t){const s=this.transformRequest(e);s.stream=!0;const o=a()?.FleekDash?.nonce||"";return globalThis.fetch("/wp-json/fleekdash/v1/ai/chat",{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":o},credentials:"same-origin",body:JSON.stringify(s),signal:t})}async readSseStream(e,t,s){const a=e.body?.getReader();if(!a)return this.transformResponse(e,t);const o=new TextDecoder;let n="",r="",i={conversationId:t.conversationId??"",raw:null};try{for(;;){const{done:e,value:c}=await a.read();if(e)break;n+=o.decode(c,{stream:!0});const d=n.split("\n\n");n=d.pop()??"";for(const a of d){const e=this.processSseChunk(a,i,s);if(i=e.latestResponse,r+=e.deltaText,e.isDone)return{message:r,conversationId:i.conversationId??t.conversationId??"",usage:i.usage,raw:i.raw}}}}finally{a.releaseLock()}return{message:r,conversationId:i.conversationId??t.conversationId??"",usage:i.usage,raw:i.raw}}processSseChunk(e,t,s){const a=e.trim();if(!a.startsWith("data: "))return{latestResponse:t,deltaText:"",isDone:!1};const n=a.slice(6).trim();if(!n)return{latestResponse:t,deltaText:"",isDone:!1};if("[DONE]"===n)return{latestResponse:t,deltaText:"",isDone:!0};try{const e=JSON.parse(n),a={...t,conversationId:e.conversationId??t.conversationId,usage:e.usage??t.usage,raw:e},o="text-delta"===e.type&&e.textDelta?e.textDelta:"";return o&&s?.({type:"text-delta",textDelta:o,raw:e}),{latestResponse:a,deltaText:o,isDone:!1}}catch(r){return o("API","[AI] Failed to parse streaming payload",r),{latestResponse:t,deltaText:"",isDone:!1}}}extractMessage(e){return"string"==typeof e.response?e.response:"string"==typeof e.message?e.message:"string"==typeof e.content?e.content:""}}let l=!1;export{e as registerDefaultAiProvider};
//# sourceMappingURL=registerDefaultAiProvider.DFIyHnFa.js.map
