UNPKG

@mencraft/lore-headless

Version:
3 lines (2 loc) 11.8 kB
import{useState as t,useRef as e,useCallback as a}from"react";var r,n,s,o,i;!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"}(r||(r={})),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"}(n||(n={})),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"}(s||(s={})),function(t){t.ASSISTANT="assistant",t.USER="user",t.SYSTEM="system",t.DEVELOPER="developer"}(o||(o={})),function(t){t.STRING="string",t.NUMBER="number",t.INTEGER="integer",t.BOOLEAN="boolean",t.OBJECT="object",t.ARRAY="array"}(i||(i={}));class l{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,n,s,o;const i=t.getReader();this.readerRef=i;const l=new TextDecoder;let c="";try{for(;;){const{done:t,value:r}=await i.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+=l.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===(n=(r=this.callbacks).onError)||void 0===n||n.call(r,t instanceof Error?t:new Error("Stream processing error"))}finally{i.cancel(),null===(o=(s=this.callbacks).onEnd)||void 0===o||o.call(s)}}handleGraphValidationErrors(t){var e,a,r,n,s,o,i,l,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===(n=(r=this.callbacks).onSystemMessage)||void 0===n||n.call(r,"Error: Too many graphs detected.")),(null===(s=t.validation_errors)||void 0===s?void 0:s.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===(i=(o=this.callbacks).onSystemMessage)||void 0===i||i.call(o,"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===(l=t.tests)||void 0===l?void 0:l.length)&&t.tests.forEach((t=>{var e,a,r,n,s;if(t.error){const o=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===(s=(n=this.callbacks).onSystemMessage)||void 0===s||s.call(n,`Test error: ${o}`)}})),!((null===(c=t.validation_errors)||void 0===c?void 0:c.length)||t.test_failure||t.too_many_graphs)}async processEvent(t){var e,a,n,s,o,i,l,c,d,h,u,p,m;switch(null===(a=(e=this.callbacks).onEvent)||void 0===a||a.call(e,t),t.event_type){case r.TextStreamOutput:void 0!==(null===(n=t.event_data)||void 0===n?void 0:n.text)&&(null===(o=(s=this.callbacks).onTextStream)||void 0===o||o.call(s,t.event_data.text));break;case r.ToolCallOutput:t.event_data&&(null===(l=(i=this.callbacks).onToolCall)||void 0===l||l.call(i,t.event_data));break;case r.BatchDataOutput:null===(d=(c=this.callbacks).onBatchData)||void 0===d||d.call(c,t.event_data);break;case r.TextBatchOutput:null===(u=(h=this.callbacks).onTextBatch)||void 0===u||u.call(h,t.event_data);break;case r.End:null===(m=(p=this.callbacks).onEnd)||void 0===m||m.call(p)}}async startGraphChatStream(t,e,a=!1){var r,n;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===(n=(r=this.callbacks).onError)||void 0===n||n.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,n,s,o,i,l,c;try{null===(r=(a=this.callbacks).onSystemMessage)||void 0===r||r.call(a,"Verifying graph structure...");const l={graphID:e,tests:[t.query]},c=await this.loreClient.post("/graph/verify",this.auth,l,{headers:{Accept:"text/event-stream","Graph-Id":e},useValidationUrl:!0});if(!c.data)return null===(s=(n=this.callbacks).onSystemMessage)||void 0===s||s.call(n,"Validation failed: No response received"),!1;const d=this.handleGraphValidationErrors(c.data);return d&&(null===(i=(o=this.callbacks).onSystemMessage)||void 0===i||i.call(o,"Graph structure verified successfully.")),d}catch(t){return null===(c=(l=this.callbacks).onError)||void 0===c||c.call(l,t instanceof Error?t:new Error("Graph validation error")),!1}}}const c=()=>Date.now().toString(36)+Math.random().toString(36).substring(2),d=(r,n,i={})=>{const[d,h]=t([]),[u,p]=t(!1),m=e(null),v=e(null);if(m.current)m.current.setCallbacks(i);else{const t={...i,onToolCall:t=>{h((e=>{const a=e[e.length-1],r=[...e];"assistant"===(null==a?void 0:a.role)&&"content"in a&&a.content;const n={id:c(),content:`Tool called: ${t.tool_name}`,role:o.ASSISTANT,timestamp:new Date};return r.push(n),r}))},onBatchData:t=>{var e;switch(t.node_type){case s.QUERY:{const e=t;Object.entries(Object.keys(e.query_result).filter((t=>t.startsWith("image."))).reduce(((t,a)=>{const[,r,n]=a.split(".");return t[r]||(t[r]={image_name:"",storage_type:"",url:"",uuid:""}),t[r][n]=e.query_result[a][0],t}),{})).map((([,t])=>t)).forEach((t=>{"bucket"===t.storage_type&&h((e=>[...e,{id:c(),imageId:t.uuid,storageType:t.storage_type,role:o.ASSISTANT,timestamp:new Date,url:t.url}]))}));Object.entries(Object.keys(e.query_result).filter((t=>t.startsWith("file."))).reduce(((t,a)=>{const r=a.replace("file.","");return Object.prototype.hasOwnProperty.call(t,"file")||(t.file={}),t.file[r]=e.query_result[a][0],t}),{})).map((([,t])=>t)).forEach((t=>{t.filename&&t.media_type&&h((e=>[...e,{id:c(),filename:t.filename,mediaType:t.media_type,role:o.ASSISTANT,timestamp:new Date}]))}));break}}null===(e=i.onBatchData)||void 0===e||e.call(i,t)},onTextStream:t=>{var e;h((e=>{const a=[...e];if(v.current){const e=a.findIndex((t=>t.id===v.current));if(-1!==e){const r=a[e];return"content"in r&&(a[e]={...r,content:r.content+t}),a}}const r=c();return v.current=r,[...a,{id:r,role:o.ASSISTANT,content:t,timestamp:new Date,isStreaming:!1}]})),null===(e=i.onTextStream)||void 0===e||e.call(i,t)},onSystemMessage:t=>{var e;h((e=>[...e,{id:c(),role:o.SYSTEM,content:t,timestamp:new Date}])),null===(e=i.onSystemMessage)||void 0===e||e.call(i,t)},onError:t=>{var e;h((e=>[...e,{id:c(),role:"error",content:t.message,timestamp:new Date}])),p(!1),v.current=null,null===(e=i.onError)||void 0===e||e.call(i,t)},onEnd:()=>{var t;p(!1),v.current&&(h((t=>{const e=[...t],a=e.findIndex((t=>t.id===v.current));if(-1!==a){const t=e[a];"isStreaming"in t&&(e[a]={...t,isStreaming:!1})}return e})),v.current=null),null===(t=i.onEnd)||void 0===t||t.call(i)}};m.current=new l(r,n,t)}const f=a(((t,e,a=!1)=>m.current?(h((e=>[...e,{id:c(),role:o.USER,content:t.query,timestamp:new Date}])),p(!0),m.current.startGraphChatStream(t,e,a)):Promise.reject(new Error("Chat stream manager not initialized"))),[h]),S=a(((t,e,a)=>m.current?(h((e=>[...e,{id:c(),role:o.USER,content:t.query,timestamp:new Date}])),p(!0),m.current.startChatStream(t,e,a)):Promise.reject(new Error("Chat stream manager not initialized"))),[h]),E=a((async(t,e)=>m.current?m.current.validateGraph(t,e):Promise.reject(new Error("Chat stream manager not initialized"))),[]);return{messages:d,isStreaming:u,startChatStream:S,cancelStream:a((()=>{m.current&&(m.current.cancelStream(),p(!1),v.current&&(h((t=>{const e=[...t],a=e.findIndex((t=>t.id===v.current));if(-1!==a){const t=e[a];"content"in t&&(e[a]={...t,isStreaming:!1,content:t.content+" [cancelled]"})}return e})),v.current=null))}),[h]),startGraphChatStream:f,clearMessages:a((()=>{h([])}),[h]),startVerification:E}},h=(e,r,n)=>{const[s,o]=t(!1),[i,c]=t(null),[d,h]=t(null),u=a((async t=>{o(!0),c(null);try{const a=new l(e,r),s=await a.sendFeedback(t,n);return h(s.data),s}catch(t){const e=t instanceof Error?t:new Error("Failed to send feedback");throw c(e),e}finally{o(!1)}}),[e,r,n]);return{sendFeedback:u,loading:s,error:i,data:d}};class u{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 n={"Content-Type":"application/json",Accept:"application/json","client-id":e.clientId,"firebase-id-token":e.token},s={...r,method:"POST",headers:{...n,...r.headers},body:a?JSON.stringify(a):void 0},o=!0===r.useValidationUrl?this.validationUrl:this.chatUrl,i=await fetch(`${o}${t}`,s);if(!i.ok)throw new Error(`HTTP error! status: ${i.status}`);const l={status:i.status,statusText:i.statusText,headers:i.headers},c=i.headers.get("content-type");if(null==c?void 0:c.includes("text/event-stream"))return{...l,data:i.body};if(null==c?void 0:c.includes("application/json")){const t=await i.text();if(t)return{...l,data:JSON.parse(t)}}return l}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 n={"Content-Type":"application/json",Accept:"application/json","client-id":e.clientId,"firebase-id-token":e.token},s={...r,method:"POST",headers:{...n,...r.headers},body:a?JSON.stringify(a):void 0},o=this.validationUrl,i=await fetch(`${o}${t}`,s);if(!i.ok)throw new Error(`HTTP error! status: ${i.status}`);const l={status:i.status,statusText:i.statusText,headers:i.headers},c=i.headers.get("content-type");if(null==c?void 0:c.includes("text/event-stream"))return{...l,data:i.body};if(null==c?void 0:c.includes("application/json")){const t=await i.text();if(t)return{...l,data:JSON.parse(t)}}return l}}export{r as EventType,u as LoreClient,i as ToolParameterType,d as useChatStream,h as useFeedback}; //# sourceMappingURL=react.esm.js.map