@mencraft/lore-headless
Version:
A headless library for chat streaming functionality
3 lines (2 loc) • 8.28 kB
JavaScript
var t,e,a,r,s;!function(t){t.BatchDataIntermediate="batch_data_intermediate",t.TextBatchIntermediate="text_batch_intermediate",t.TextStreamIntermediate="text_stream_intermediate",t.ToolCallIntermediate="tool_call_intermediate",t.BatchDataOutput="batch_data_output",t.TextBatchOutput="text_batch_output",t.TextStreamOutput="text_stream_output",t.ToolCallOutput="tool_call_output",t.End="end",t.Error="error"}(t||(t={})),function(t){t.HYDE_PROMPT="HydePromptNode",t.PROMPT="PromptNode",t.BOOL_PROMPT="BoolPromptNode",t.DEFAULT_RESPONSE="DefaultResponseNode",t.QUERY="QueryNode",t.ROUTER="RouterNode",t.REACT="ReactNode",t.CONDITIONAL="ConditionalNode",t.SEARCH="SearchNode"}(e||(e={})),function(t){t.HYDE_PROMPT="HydePromptNode",t.PROMPT="PromptNode",t.BOOL_PROMPT="BoolPromptNode",t.DEFAULT_RESPONSE="DefaultResponseNode",t.QUERY="QueryNode",t.ROUTER="RouterNode",t.REACT="ReactNode",t.CONDITIONAL="ConditionalNode",t.SEARCH="SearchNode"}(a||(a={})),function(t){t.ASSISTANT="assistant",t.USER="user",t.SYSTEM="system",t.DEVELOPER="developer"}(r||(r={})),function(t){t.STRING="string",t.NUMBER="number",t.INTEGER="integer",t.BOOLEAN="boolean",t.OBJECT="object",t.ARRAY="array"}(s||(s={}));class o{constructor(t="production",e,a){if(e||a){if(!e||!a)throw new Error("Both customChatUrl and customValidationUrl must be provided together");this.chatUrl=e,this.validationUrl=a}else"staging"===t?(this.chatUrl="https://staging-chat.lore.center",this.validationUrl="https://staging-api.lore.center"):"development"===t?(this.chatUrl="http://127.0.0.1:8002",this.validationUrl="http://127.0.0.1:8000"):(this.chatUrl="https://chat-cloudrun-service-778776159944.us-central1.run.app",this.validationUrl="https://api.lore.center")}async post(t,e,a,r={}){if(!e.clientId||!e.token)throw new Error(`Authentication error: ${e.clientId?"Token":"Client ID"} missing`);const s={"Content-Type":"application/json",Accept:"application/json","client-id":e.clientId,"firebase-id-token":e.token},o={...r,method:"POST",headers:{...s,...r.headers},body:a?JSON.stringify(a):void 0},i=!0===r.useValidationUrl?this.validationUrl:this.chatUrl,l=await fetch(`${i}${t}`,o);if(!l.ok)throw new Error(`HTTP error! status: ${l.status}`);const n={status:l.status,statusText:l.statusText,headers:l.headers},c=l.headers.get("content-type");if(null==c?void 0:c.includes("text/event-stream"))return{...n,data:l.body};if(null==c?void 0:c.includes("application/json")){const t=await l.text();if(t)return{...n,data:JSON.parse(t)}}return n}async chatPost(t,e,a,r={}){return this.post(t,e,a,{...r,useValidationUrl:!1})}async adminPost(t,e,a,r={}){if(!e.clientId||!e.token)throw new Error(`Authentication error: ${e.clientId?"Firebase token":"Client ID"} missing`);const s={"Content-Type":"application/json",Accept:"application/json","client-id":e.clientId,"firebase-id-token":e.token},o={...r,method:"POST",headers:{...s,...r.headers},body:a?JSON.stringify(a):void 0},i=this.validationUrl,l=await fetch(`${i}${t}`,o);if(!l.ok)throw new Error(`HTTP error! status: ${l.status}`);const n={status:l.status,statusText:l.statusText,headers:l.headers},c=l.headers.get("content-type");if(null==c?void 0:c.includes("text/event-stream"))return{...n,data:l.body};if(null==c?void 0:c.includes("application/json")){const t=await l.text();if(t)return{...n,data:JSON.parse(t)}}return n}}class i{constructor(t,e,a={}){this.readerRef=null,this.loreClient=t,this.auth=e,this.callbacks=a}setCallbacks(t){this.callbacks={...this.callbacks,...t}}async processChatStream(t){var e,a,r,s,o,i;const l=t.getReader();this.readerRef=l;const n=new TextDecoder;let c="";try{for(;;){const{done:t,value:r}=await l.read();if(t){if(c.trim())try{const t=JSON.parse(c.trim());await this.processEvent(t)}catch(t){}null===(a=(e=this.callbacks).onEnd)||void 0===a||a.call(e);break}for(c+=n.decode(r,{stream:!0});;){const t=c.indexOf("\n");if(-1===t)break;const e=c.slice(0,t).trim();if(c=c.slice(t+1),e)try{const t=JSON.parse(e);await this.processEvent(t)}catch(t){continue}}}}catch(t){null===(s=(r=this.callbacks).onError)||void 0===s||s.call(r,t instanceof Error?t:new Error("Stream processing error"))}finally{l.cancel(),null===(i=(o=this.callbacks).onEnd)||void 0===i||i.call(o)}}handleGraphValidationErrors(t){var e,a,r,s,o,i,l,n,c;return t.test_failure&&(null===(a=(e=this.callbacks).onSystemMessage)||void 0===a||a.call(e,"Graph validation failed due to test failures.")),t.too_many_graphs&&(null===(s=(r=this.callbacks).onSystemMessage)||void 0===s||s.call(r,"Error: Too many graphs detected.")),(null===(o=t.validation_errors)||void 0===o?void 0:o.length)&&(t.validation_errors.forEach((t=>{var e,a;const r=`Validation error in node ${t.node_id}: ${t.message} (${t.error_type})`;null===(a=(e=this.callbacks).onSystemMessage)||void 0===a||a.call(e,r)})),null===(l=(i=this.callbacks).onSystemMessage)||void 0===l||l.call(i,"This graph is not able to run. [Click to go to graph](/graph?graphId="+this.callbacks.graphId+") , load the graph to fix the graph error.")),(null===(n=t.tests)||void 0===n?void 0:n.length)&&t.tests.forEach((t=>{var e,a,r,s,o;if(t.error){const i=t.error_text||(null===(r=null===(a=null===(e=t.events)||void 0===e?void 0:e[0])||void 0===a?void 0:a.event_data)||void 0===r?void 0:r.error)||"Unknown test error";null===(o=(s=this.callbacks).onSystemMessage)||void 0===o||o.call(s,`Test error: ${i}`)}})),!((null===(c=t.validation_errors)||void 0===c?void 0:c.length)||t.test_failure||t.too_many_graphs)}async processEvent(e){var a,r,s,o,i,l,n,c,d,h,u,p,v;switch(null===(r=(a=this.callbacks).onEvent)||void 0===r||r.call(a,e),e.event_type){case t.TextStreamOutput:void 0!==(null===(s=e.event_data)||void 0===s?void 0:s.text)&&(null===(i=(o=this.callbacks).onTextStream)||void 0===i||i.call(o,e.event_data.text));break;case t.ToolCallOutput:e.event_data&&(null===(n=(l=this.callbacks).onToolCall)||void 0===n||n.call(l,e.event_data));break;case t.BatchDataOutput:null===(d=(c=this.callbacks).onBatchData)||void 0===d||d.call(c,e.event_data);break;case t.TextBatchOutput:null===(u=(h=this.callbacks).onTextBatch)||void 0===u||u.call(h,e.event_data);break;case t.End:null===(v=(p=this.callbacks).onEnd)||void 0===v||v.call(p)}}async startGraphChatStream(t,e,a=!1){var r,s;try{if(!a){if(!await this.validateGraph(t,e))return}const r=await this.loreClient.chatPost("/chat_stream",this.auth,t,{headers:{Accept:"text/event-stream","Graph-Id":this.callbacks.graphId||e||"personal"}});if(!r.data)throw new Error("No stream data received");await this.processChatStream(r.data)}catch(t){null===(s=(r=this.callbacks).onError)||void 0===s||s.call(r,t instanceof Error?t:new Error("Failed to start chat stream"))}}async startChatStream(t,e,a=!1){let r=null;if(a?r=await this.loreClient.adminPost("/chat/chat_stream",this.auth,t,{headers:{Accept:"text/event-stream","Graph-Id":this.callbacks.graphId||e||"personal"}}):a||(r=await this.loreClient.chatPost("/chat_stream",this.auth,t,{headers:{Accept:"text/event-stream","Graph-Id":this.callbacks.graphId||e||"personal"}})),!r)throw new Error("No response received");if(!r.data)throw new Error("No stream data received");await this.processChatStream(r.data)}cancelStream(){this.readerRef&&(this.readerRef.cancel(),this.readerRef=null)}async sendFeedback(t,e){return this.loreClient.post("/feedback",this.auth,t,{headers:{...e&&{"Graph-Id":e}},useValidationUrl:!1})}async validateGraph(t,e){var a,r,s,o,i,l,n,c;try{null===(r=(a=this.callbacks).onSystemMessage)||void 0===r||r.call(a,"Verifying graph structure...");const n={graphID:e,tests:[t.query]},c=await this.loreClient.post("/graph/verify",this.auth,n,{headers:{Accept:"text/event-stream","Graph-Id":e},useValidationUrl:!0});if(!c.data)return null===(o=(s=this.callbacks).onSystemMessage)||void 0===o||o.call(s,"Validation failed: No response received"),!1;const d=this.handleGraphValidationErrors(c.data);return d&&(null===(l=(i=this.callbacks).onSystemMessage)||void 0===l||l.call(i,"Graph structure verified successfully.")),d}catch(t){return null===(c=(n=this.callbacks).onError)||void 0===c||c.call(n,t instanceof Error?t:new Error("Graph validation error")),!1}}}export{i as ChatStreamManager,t as EventType,r as LLMRole,o as LoreClient,e as LoreNodeOutputTypes,a as LoreNodeTypes,s as ToolParameterType};
//# sourceMappingURL=index.esm.js.map