@waniwani/sdk 0.8.7-alpha.2 → 0.8.7-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mcp/index.js CHANGED
@@ -1,7 +1,7 @@
1
- function ne(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function it(){return ne()==="openai"}function st(){return ne()==="mcp-apps"}var C="__start__",b="__end__",Se=Symbol.for("waniwani.flow.interrupt"),ve=Symbol.for("waniwani.flow.widget");function Re(e,t){let n=t?.context,r=[];for(let[o,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let a=i;r.push({question:a.question,field:o,suggestions:a.suggestions,context:a.context,validate:a.validate})}return{__type:Se,questions:r,context:n}}function xe(e,t){return{__type:ve,tool:e,...t}}function Ee(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===Se}function Ie(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ve}import{z as O}from"zod";var re="waniwani/client";function H(e){if(typeof e=="object"&&e!==null)return e[re]}function Ce(e,t,n){return{track(r){return e.track({...r,meta:{...t,...r.meta}})},identify(r,o){return e.identify(r,o,t)},kb:e.kb,_config:n}}function N(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var at=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],ct=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],dt=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],ut=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],lt=["correlationId","openai/requestId"];function P(e){return e?N(e,at):void 0}function be(e){return e?N(e,ct):void 0}function _e(e){return e?N(e,dt):void 0}function Pe(e){return e?N(e,ut):void 0}function We(e){return e?N(e,lt):void 0}var pt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function U(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:r}of pt){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function Me(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function D(e,t){let n=t.split("."),r=e;for(let o of n){if(r==null||typeof r!="object")return;r=r[o]}return r}function ft(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let i=e;for(let a of r)(i[a]==null||typeof i[a]!="object"||Array.isArray(i[a]))&&(i[a]={}),i=i[a];i[o]=n}function Fe(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let i of n){if(o==null||typeof o!="object")return;o=o[i]}o!=null&&typeof o=="object"&&delete o[r]}function oe(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?ft(t,n,r):t[n]=r;return t}function W(e,t){let n={...e};for(let[r,o]of Object.entries(t))o!==null&&typeof o=="object"&&!Array.isArray(o)&&n[r]!==null&&typeof n[r]=="object"&&!Array.isArray(n[r])?n[r]=W(n[r],o):n[r]=o;return n}function ie(e){return e!=null&&e!==""}async function M(e,t){return e.type==="direct"?e.to:e.condition(t)}function Ae(e,t,n,r){if(e.every(c=>ie(D(r,c.field))))return null;let o=e.filter(c=>!ie(D(r,c.field))),i=o.length===1,a=o[0];return{content:i&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...i&&a?{field:a.field}:{}}}}async function L(e,t,n,r,o,i,a){let s=e,c={...t},u=50,l=0;for(;l++<u;){if(s===b)return{content:{status:"complete"},flowTokenContent:{state:c}};let d=n.get(s);if(!d)return{content:{status:"error",error:`Unknown node: "${s}"`}};try{let p=await d({state:c,meta:i,interrupt:Re,showWidget:xe,waniwani:a});if(Ee(p)){for(let T of p.questions)T.validate&&o.set(`${s}:${T.field}`,T.validate);let y=Ae(p.questions,p.context,s,c);if(y)return y;for(let T of p.questions){let R=o.get(`${s}:${T.field}`);if(R)try{let v=D(c,T.field),S=await R(v);S&&typeof S=="object"&&(c=W(c,S))}catch(v){let S=v instanceof Error?v.message:String(v);Fe(c,T.field);let E=p.questions.map(_=>_.field===T.field?{..._,context:_.context?`ERROR: ${S}
1
+ function Q(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Je(){return Q()==="openai"}function Xe(){return Q()==="mcp-apps"}var C="__start__",b="__end__",me=Symbol.for("waniwani.flow.interrupt"),we=Symbol.for("waniwani.flow.widget");function he(e,t){let n=t?.context,r=[];for(let[o,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let a=i;r.push({question:a.question,field:o,suggestions:a.suggestions,context:a.context,validate:a.validate})}return{__type:me,questions:r,context:n}}function ye(e,t){return{__type:we,tool:e,...t}}function Te(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===me}function ke(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===we}import{z as O}from"zod";var ee="waniwani/client";function $(e){if(typeof e=="object"&&e!==null)return e[ee]}function Se(e,t,n){return{track(r){return e.track({...r,meta:{...t,...r.meta}})},identify(r,o){return e.identify(r,o,t)},kb:e.kb,_config:n}}function N(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var Qe=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],et=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],tt=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],nt=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],rt=["correlationId","openai/requestId"];function _(e){return e?N(e,Qe):void 0}function ve(e){return e?N(e,et):void 0}function xe(e){return e?N(e,tt):void 0}function Re(e){return e?N(e,nt):void 0}function Ee(e){return e?N(e,rt):void 0}var ot=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function U(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:r}of ot){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function Ie(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function D(e,t){let n=t.split("."),r=e;for(let o of n){if(r==null||typeof r!="object")return;r=r[o]}return r}function it(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let i=e;for(let a of r)(i[a]==null||typeof i[a]!="object"||Array.isArray(i[a]))&&(i[a]={}),i=i[a];i[o]=n}function Ce(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let i of n){if(o==null||typeof o!="object")return;o=o[i]}o!=null&&typeof o=="object"&&delete o[r]}function te(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?it(t,n,r):t[n]=r;return t}function P(e,t){let n={...e};for(let[r,o]of Object.entries(t))o!==null&&typeof o=="object"&&!Array.isArray(o)&&n[r]!==null&&typeof n[r]=="object"&&!Array.isArray(n[r])?n[r]=P(n[r],o):n[r]=o;return n}function ne(e){return e!=null&&e!==""}async function W(e,t){return e.type==="direct"?e.to:e.condition(t)}function be(e,t,n,r){if(e.every(c=>ne(D(r,c.field))))return null;let o=e.filter(c=>!ne(D(r,c.field))),i=o.length===1,a=o[0];return{content:i&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...i&&a?{field:a.field}:{}}}}async function B(e,t,n,r,o,i,a){let s=e,c={...t},d=50,p=0;for(;p++<d;){if(s===b)return{content:{status:"complete"},flowTokenContent:{state:c}};let u=n.get(s);if(!u)return{content:{status:"error",error:`Unknown node: "${s}"`}};try{let m=await u({state:c,meta:i,interrupt:he,showWidget:ye,waniwani:a});if(Te(m)){for(let f of m.questions)f.validate&&o.set(`${s}:${f.field}`,f.validate);let y=be(m.questions,m.context,s,c);if(y)return y;for(let f of m.questions){let v=o.get(`${s}:${f.field}`);if(v)try{let k=D(c,f.field),x=await v(k);x&&typeof x=="object"&&(c=P(c,x))}catch(k){let x=k instanceof Error?k.message:String(k);Ce(c,f.field);let A=m.questions.map(I=>I.field===f.field?{...I,context:I.context?`ERROR: ${x}
2
2
 
3
- ${_.context}`:`ERROR: ${S}`}:_),q=Ae(E,p.context,s,c);if(q)return q;break}}let g=r.get(s);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await M(g,c);continue}if(Ie(p)){let y=p.field;if(y&&ie(D(c,y))){let g=r.get(s);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await M(g,c);continue}return{content:{status:"widget",tool:p.tool.id,data:p.data,description:p.description,interactive:p.interactive!==!1},flowTokenContent:{step:s,state:c,field:y,widgetId:p.tool.id}}}c=W(c,p);let m=r.get(s);if(!m)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await M(m,c)}catch(f){return{content:{status:"error",error:f instanceof Error?f.message:String(f)},flowTokenContent:{step:s,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var gt="@waniwani/sdk",mt="https://app.waniwani.ai",F=class{get baseUrl(){return(process.env.WANIWANI_API_URL??mt).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");return await this.request("/api/mcp/redis/get",{key:t})??null}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");await this.request("/api/mcp/redis/set",{key:t,value:n})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let r=`${this.baseUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":gt},body:JSON.stringify(n)});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(a||`KV store API error: HTTP ${o.status}`)}return(await o.json()).data}};var j=class{store=new F;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Ne(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Ue(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow,"," including relevant prior context that led to triggering it, if available."," Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs."," The engine will auto-skip steps whose fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=[];for(let[r,o]of Object.entries(e.state)){let i=Me(o);if(i){let a=o.description??"",s=Object.entries(i).map(([c,u])=>{let l=Ne(u);return l?`\`${r}.${c}\` (${l})`:`\`${r}.${c}\``}).join(", ");n.push(a?`\`${r}\` (${a}): ${s}`:`\`${r}\`: ${s}`)}else{let a=Ne(o);n.push(a?`\`${r}\` (${a})`:`\`${r}\``)}}t.push(` Known fields: ${n.join(", ")}.`)}return t.push(" For grouped fields (shown as `group.subfield`), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Check the `interactive` field in the response:"," \u2022 `interactive: true` \u2014 The widget requires user interaction. After calling the display tool,"," STOP and WAIT for the user to interact with the widget. Do NOT call this flow tool again"," until the user has responded. When they do, call with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned."," \u2022 `interactive: false` \u2014 The widget is display-only. Call the display tool, then immediately",' call THIS flow tool again with `action: "continue"`. Do NOT wait for user interaction.',' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","4. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped."),t.join(`
4
- `)}var wt={action:O.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),intent:O.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow, including relevant prior context that led to triggering it, if available. Do not invent missing context.`),stateUpdates:O.record(O.string(),O.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned.")};function De(e){let{config:t,nodes:n,edges:r}=e,o=Ue(t),i=`${t.description}
5
- ${o}`,a=e.store??new j,s=new Map;async function c(d,f,p,m){if(d.action==="start"){let y=typeof d.intent=="string"?d.intent.trim():void 0;if(!y)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};d.intent=y;let g=r.get(C);if(!g)return{content:{status:"error",error:"No start edge"}};let T=oe(d.stateUpdates??{}),R=await M(g,T);return L(R,T,n,r,s,p,m)}if(d.action==="continue"){if(!f)return{content:{status:"error",error:"No session ID available for continue action."}};let y;try{y=await a.get(f)}catch(v){let S=v instanceof Error?v.message:String(v);return{content:{status:"error",error:`Failed to load flow state (session "${f}"): ${S}`}}}if(!y)return{content:{status:"error",error:`Flow state not found for session "${f}". The flow may have expired.`}};let g=y.state,T=y.step;if(!T)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let R=W(g,oe(d.stateUpdates??{}));if(y.widgetId){let v=r.get(T);if(!v)return{content:{status:"error",error:`No edge from step "${T}"`}};let S=await M(v,R);return L(S,R,n,r,s,p,m)}return L(T,R,n,r,s,p,m)}return{content:{status:"error",error:`Unknown action: "${d.action}"`}}}let u={title:t.title,description:i,inputSchema:wt,annotations:t.annotations},l=(async(d,f)=>{let p=f,m=p._meta??{},y=P(m),g=H(p),T=await c(d,y,m,g);if(y)if(T.flowTokenContent&&T.content.status!=="complete")try{await a.set(y,T.flowTokenContent)}catch(v){let S=v instanceof Error?v.message:String(v);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${y}"): ${S}`},null,2)}],_meta:m,isError:!0}}else T.content.status==="complete"&&await a.delete(y);return{content:[{type:"text",text:JSON.stringify(T.content,null,2)}],_meta:m,...T.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:u,handler:l,async register(d){d.registerTool(t.id,u,l)},graph:e.graph}}function je(e,t){let n=["flowchart TD"];n.push(` ${C}((Start))`);for(let[r]of e)n.push(` ${r}[${r}]`);n.push(` ${b}((End))`);for(let[r,o]of t)o.type==="direct"?n.push(` ${r} --> ${o.to}`):n.push(` ${r} -.-> ${r}_branch([?])`);return n.join(`
6
- `)}var A=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===C||t===b)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return je(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return De({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>je(n,r)})}validate(){if(!this.edges.has(C))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(C);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==C&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function Oe(e){return new A(e)}function $e(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ke(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...u)=>{r.push(u)}};await e.register(i);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.name}" did not register a handler`);let s={_meta:{sessionId:o}};async function c(u){return{...u,decodedState:n?await n.get(o):null}}return{async start(u,l){let d=await a({action:"start",intent:u,...l?{stateUpdates:l}:{}},s);return c($e(d))},async continueWith(u){let l=await a({action:"continue",...u?{stateUpdates:u}:{}},s);return c($e(l))},async lastState(){return n?n.get(o):null}}}var V="text/html+skybridge",z="text/html;profile=mcp-app",qe=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Be(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function He(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function se(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0},...e.httpUrl&&{httpUrl:e.httpUrl}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Le(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:i,widgetDomain:a,prefersBorder:s=!0,autoHeight:c=!0}=e,u=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:R}=new URL(o);(R==="localhost"||R==="127.0.0.1")&&(u={...u,connect_domains:[...u.connect_domains||[],`ws://${R}:*`,`wss://${R}:*`],resource_domains:[...u.resource_domains||[],`http://${R}:*`]})}catch{}let l=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,p=`${o.endsWith("/")?o.slice(0,-1):o}${i}`,m=null,y=()=>(m||(m=qe(o,i)),m),g=r;async function T(R){let v=await y();R.registerResource(`${t}-openai-widget`,l,{title:n,description:g,mimeType:V,_meta:{"openai/widgetDescription":g,"openai/widgetPrefersBorder":s}},async S=>({contents:[{uri:S.href,mimeType:V,text:v,_meta:Be({description:g,prefersBorder:s,widgetDomain:a,widgetCSP:u})}]})),R.registerResource(`${t}-mcp-widget`,d,{title:n,description:g,mimeType:z,_meta:{ui:{prefersBorder:s}}},async S=>({contents:[{uri:S.href,mimeType:z,text:v,_meta:He({description:g,prefersBorder:s,widgetDomain:a,widgetCSP:u})}]}))}return{id:t,title:n,description:r,openaiUri:l,mcpUri:d,autoHeight:c,httpUrl:p,register:T}}function ht(e,t){let{resource:n,description:r,inputSchema:o,annotations:i,autoInjectResultText:a=!0}=e,s=e.id??n?.id,c=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let u=n?se({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight,httpUrl:n.httpUrl}):void 0;return{id:s,title:c,description:r,async register(l){l.registerTool(s,{title:c,description:r,inputSchema:o,annotations:i,...u&&{_meta:u}},(async(d,f)=>{let p=f,m=p._meta??{},y=H(p),g=await t(d,{extra:{_meta:m},waniwani:y});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...u,...m,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function yt(e,t){await Promise.all(t.map(n=>n.register?.(e)))}var Y=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Tt="@waniwani/sdk";function Ve(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(i,a,s){let c=r(),u=`${t.replace(/\/$/,"")}${a}`,l={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Tt},d={method:i,headers:l};s!==void 0&&(l["Content-Type"]="application/json",d.body=JSON.stringify(s));let f=await fetch(u,d);if(!f.ok){let m=await f.text().catch(()=>"");throw new Y(m||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(i){return o("POST","/api/mcp/kb/ingest",{files:i})},async search(i,a){return o("POST","/api/mcp/kb/search",{query:i,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var kt="@waniwani/sdk";function Z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??ze,o=Rt(e),i=G(e.meta),a=G(e.metadata),s=xt(e,i),c=I(e.eventId)??r(),u=Et(e.timestamp,n),l=I(e.source)??U(i)??t.source??kt,d=ae(e)?{...e}:void 0,f={...a};return Object.keys(i).length>0&&(f.meta=i),d&&(f.rawLegacy=d),{id:c,type:"mcp.event",name:o,source:l,timestamp:u,correlation:s,properties:St(e,o),metadata:f,rawLegacy:d}}function ze(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function St(e,t){if(!ae(e))return G(e.properties);let n=vt(e,t),r=G(e.properties);return{...n,...r}}function vt(e,t){switch(t){case"tool.called":{let n={};return I(e.toolName)&&(n.name=e.toolName),I(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),I(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return I(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),I(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Rt(e){return ae(e)?e.eventType:e.event}function xt(e,t){let n=I(e.requestId)??be(t),r=I(e.sessionId)??P(t),o=I(e.traceId)??_e(t),i=I(e.externalUserId)??Pe(t),a=I(e.correlationId)??We(t)??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),a&&(s.correlationId=a),i&&(s.externalUserId=i),s}function Et(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function G(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function I(e){if(typeof e=="string"&&e.trim().length!==0)return e}function ae(e){return"eventType"in e}var It="/api/mcp/events/v2/batch";var Ye="@waniwani/sdk",Ct=new Set([401,403]),bt=new Set([408,425,429,500,502,503,504]);function Ge(e){return new ce(e)}var ce=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=Wt(t.apiUrl,t.endpointPath??It),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":Ye},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Mt(i)}}if(Ct.has(n.status))return{kind:"auth",status:n.status};if(bt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Pt(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Ye,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],i=[];for(let a of n){let s=r.get(a.eventId);if(s){if(_t(a)){o.push(s);continue}i.push(s)}}return{retryable:o,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function _t(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function Pt(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function Wt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Mt(e){return e instanceof Error?e.message:String(e)}function Ze(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?Ge({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,a={async identify(s,c,u){o();let l=Z({event:"user.identified",externalUserId:s,properties:c,meta:u});return i?.enqueue(l),{eventId:l.id}},async track(s){o();let c=Z(s);return i?.enqueue(c),{eventId:c.id}},async flush(){o(),await i?.flush()},async shutdown(s){return o(),await i?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&Ft(a,r.shutdownTimeoutMs),a}function Ft(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function J(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",r=t?.apiKey??process.env.WANIWANI_API_KEY,o={endpointPath:t?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:t?.tracking?.flushIntervalMs??1e3,maxBatchSize:t?.tracking?.maxBatchSize??20,maxBufferSize:t?.tracking?.maxBufferSize??1e3,maxRetries:t?.tracking?.maxRetries??3,retryBaseDelayMs:t?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:t?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:t?.tracking?.shutdownTimeoutMs??2e3},i={apiUrl:n,apiKey:r,tracking:o},a=Ze(i),s=Ve(i);return{...a,kb:s,_config:i}}function At(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function Nt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=J(t)),n}return async function(i){let a;try{a=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),c=[];for(let u of a.events){let l=At(u),d=await s.track(l);c.push(d.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let c=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}function X(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var de=X("widget-token",!!process.env.WANIWANI_DEBUG),Ut=120*1e3,Q=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-Ut?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=Dt(this.config.apiUrl,"/api/mcp/widget-tokens");de("minting token from",r);let o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let i=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o),signal:AbortSignal.timeout(5e3)});if(de("mint response:",i.status),!i.ok)return null;let a=await i.json(),s=a.data&&typeof a.data=="object"?a.data:a,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch(i){return de("mint failed:",i),null}}};function Dt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Je="waniwani/sessionId",le="waniwani/geoLocation",pe="waniwani/userLocation";function k(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function $(e){if(!k(e))return;let t=e._meta;return k(t)?t:void 0}function fe(e){if(!k(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>k(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function jt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ge(e,t,n,r,o,i){let a=jt(e,n.toolType),s=$(t);return{event:"tool.called",properties:{name:e,type:a,...r??{},...i?.input!==void 0&&{input:i.input},...i?.output!==void 0&&{output:i.output}},meta:s,source:U(s),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function me(e,t,n){try{await e.track(t)}catch(r){n?.(he(r))}}async function we(e,t){try{await e.flush()}catch(n){t?.(he(n))}}async function Xe(e,t,n,r,o){if(!k(e))return;k(e._meta)||(e._meta={});let i=e._meta,a=k(i.waniwani)?i.waniwani:void 0,s={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let d=await t.getToken();d&&(s.token=d)}catch(d){o?.(he(d))}let c=P(i);c&&(s.sessionId||(s.sessionId=c));let u=et(i);u!==void 0&&(s.geoLocation||(s.geoLocation=u));let l=U($(r));l&&!s.source&&(s.source=l),i.waniwani=s}function Qe(e,t){let n=$(t);if(!n||!k(e))return;k(e._meta)||(e._meta={});let r=e._meta,o=P(n);o&&!r[Je]&&(r[Je]=o);let i=et(n);i&&(r[le]||(r[le]=i),r[pe]||(r[pe]=i))}function et(e){if(!e)return;let t=e[le]??e[pe];if(k(t)||typeof t=="string")return t}function tt(e,t){if(!t||!k(e))return;k(e._meta)||(e._meta={});let n=e._meta;for(let[r,o]of Object.entries(t))r in n||(n[r]=o)}function he(e){return e instanceof Error?e:new Error(String(e))}var ee="/__waniwani/resource";function ue(e,t){let n=e[t];if(typeof n=="string")return n;if(Array.isArray(n)&&typeof n[0]=="string")return n[0]}function Ot(e){if(!k(e))return;let t=e.requestInfo;if(!k(t))return;let n=t.url;if(k(n)&&typeof n.origin=="string"&&n.origin!=="null")return n.origin;let r=t.headers;if(!k(r))return;let o=ue(r,"x-forwarded-host");if(o)return`${ue(r,"x-forwarded-proto")??"https"}://${o}`;let i=ue(r,"host");if(i)return`${i.startsWith("127.0.0.1:")||i.startsWith("localhost:")?"http":"https"}://${i}`}function nt(e,t,n){if(!k(e)||!k(e._meta))return;let o=e._meta.ui;if(!k(o))return;let i=o.resourceUri;if(typeof i!="string"||typeof o.httpUrl=="string"||!n.has(i))return;let a=Ot(t);a&&(o.httpUrl=`${a}${ee}?uri=${encodeURIComponent(i)}`)}function rt(e){return(t,n,r)=>{let o=t,i=n,a=typeof r=="function"?r:void 0;if(o.method!=="GET"){a?.();return}let s=null;if(o.query&&typeof o.query.uri=="string")s=o.query.uri;else if(o.url)try{s=new URL(o.url,"http://localhost").searchParams.get("uri")}catch{}if(!s){i.status(400).json({error:"Missing uri query parameter"});return}let c=e.get(s);if(!c){i.status(404).json({error:"Resource not found"});return}let u=k(o.headers)?o.headers:{};Promise.resolve(c(new URL(s),{requestInfo:{headers:u}})).then(l=>{let d=l?.contents?.[0];if(!d){i.status(404).json({error:"Resource has no content"});return}let f;if(typeof d.text=="string"?f=d.text:typeof d.blob=="string"&&(f=atob(d.blob)),!f){i.status(404).json({error:"Resource has no content"});return}i.setHeader("Content-Type","text/html"),i.setHeader("Cache-Control","private, max-age=300"),i.status(200).send(f)}).catch(l=>{let d=l instanceof Error?l.message:"Unknown error";i.status(500).json({error:d})})}}var K=X("mcp",!!process.env.WANIWANI_DEBUG),ot="https://app.waniwani.ai";function $t(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??J(),i=r.injectWidgetToken!==!1,a=o._config.apiKey?new Q({apiUrl:o._config.apiUrl??ot,apiKey:o._config.apiKey}):null,s=new Map,c=e.registerResource.bind(e);n.registerResource=((...f)=>{let[,p,,m]=f;return typeof p=="string"&&typeof m=="function"&&s.set(p,m),c(...f)});let u=!1,l=e;typeof l.use=="function"&&(l.use(ee,rt(s)),u=!0,K("registered HTTP resource endpoint at",ee));let d=e.registerTool.bind(e);return n.registerTool=((...f)=>{let[p,m,y]=f,g=typeof p=="string"&&p.trim().length>0?p:"unknown";if(typeof y!="function")return d(...f);let T=k(m)&&k(m._meta)?m._meta:void 0,R=y,v=async(S,E)=>{let q=$(E)??{},_=Ce(o,q,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});k(E)&&(E[re]=_);let ye=performance.now(),Te=e.server?.getClientVersion?.();try{let x=await R(S,E),B=Math.round(performance.now()-ye);K(`tool "${g}" handler returned in ${B}ms, running post-processing...`);let te=k(x)&&x.isError===!0;if(te){let ke=fe(x);console.error(`[waniwani] Tool "${g}" returned error${ke?`: ${ke}`:""}`)}return await me(o,ge(g,E,r,{durationMs:B,status:te?"error":"ok",...te&&{errorMessage:fe(x)??"Unknown tool error"}},Te,{input:S,output:x}),r.onError),K(`tool "${g}" tracking done`),r.flushAfterToolCall&&await we(o,r.onError),tt(x,T),u&&nt(x,E,s),Qe(x,E),i&&(await Xe(x,a,o._config.apiUrl??ot,E,r.onError),K(`tool "${g}" widget config injected`)),K(`tool "${g}" post-processing complete, returning result`),x}catch(x){let B=Math.round(performance.now()-ye);throw await me(o,ge(g,E,r,{durationMs:B,status:"error",errorMessage:x instanceof Error?x.message:String(x)},Te,{input:S}),r.onError),r.flushAfterToolCall&&await we(o,r.onError),x}};if(k(m)&&k(m._meta)){let S=m._meta;S["openai/outputTemplate"]&&!S["openai/resultCanProduceWidget"]&&(S["openai/resultCanProduceWidget"]=!0,S["openai/widgetAccessible"]=!0)}return d(p,m,v)}),n}export{b as END,C as START,A as StateGraph,F as WaniwaniKvStore,Oe as createFlow,Ke as createFlowTestHarness,Le as createResource,ht as createTool,Nt as createTrackingRoute,ne as detectPlatform,st as isMCPApps,it as isOpenAI,yt as registerTools,$t as withWaniwani};
3
+ ${I.context}`:`ERROR: ${x}`}:I),R=be(A,m.context,s,c);if(R)return R;break}}let l=r.get(s);if(!l)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await W(l,c);continue}if(ke(m)){let y=m.field;if(y&&ne(D(c,y))){let l=r.get(s);if(!l)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await W(l,c);continue}return{content:{status:"widget",tool:m.tool.id,data:m.data,description:m.description,interactive:m.interactive!==!1},flowTokenContent:{step:s,state:c,field:y,widgetId:m.tool.id}}}c=P(c,m);let T=r.get(s);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${s}"`}};s=await W(T,c)}catch(g){return{content:{status:"error",error:g instanceof Error?g.message:String(g)},flowTokenContent:{step:s,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var st="@waniwani/sdk",at="https://app.waniwani.ai",M=class{get baseUrl(){return(process.env.WANIWANI_API_URL??at).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");return await this.request("/api/mcp/redis/get",{key:t})??null}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");await this.request("/api/mcp/redis/set",{key:t,value:n})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let r=`${this.baseUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":st},body:JSON.stringify(n)});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(a||`KV store API error: HTTP ${o.status}`)}return(await o.json()).data}};var j=class{store=new M;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function _e(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Pe(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow,"," including relevant prior context that led to triggering it, if available."," Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs."," The engine will auto-skip steps whose fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=[];for(let[r,o]of Object.entries(e.state)){let i=Ie(o);if(i){let a=o.description??"",s=Object.entries(i).map(([c,d])=>{let p=_e(d);return p?`\`${r}.${c}\` (${p})`:`\`${r}.${c}\``}).join(", ");n.push(a?`\`${r}\` (${a}): ${s}`:`\`${r}\`: ${s}`)}else{let a=_e(o);n.push(a?`\`${r}\` (${a})`:`\`${r}\``)}}t.push(` Known fields: ${n.join(", ")}.`)}return t.push(" For grouped fields (shown as `group.subfield`), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Check the `interactive` field in the response:"," \u2022 `interactive: true` \u2014 The widget requires user interaction. After calling the display tool,"," STOP and WAIT for the user to interact with the widget. Do NOT call this flow tool again"," until the user has responded. When they do, call with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned."," \u2022 `interactive: false` \u2014 The widget is display-only. Call the display tool, then immediately",' call THIS flow tool again with `action: "continue"`. Do NOT wait for user interaction.',' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","4. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped."),t.join(`
4
+ `)}var ct={action:O.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),intent:O.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow, including relevant prior context that led to triggering it, if available. Do not invent missing context.`),stateUpdates:O.record(O.string(),O.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned.")};function We(e){let{config:t,nodes:n,edges:r}=e,o=Pe(t),i=`${t.description}
5
+ ${o}`,a=e.store??new j,s=new Map;async function c(u,g,m,T){if(u.action==="start"){let y=typeof u.intent=="string"?u.intent.trim():void 0;if(!y)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};u.intent=y;let l=r.get(C);if(!l)return{content:{status:"error",error:"No start edge"}};let f=te(u.stateUpdates??{}),v=await W(l,f);return B(v,f,n,r,s,m,T)}if(u.action==="continue"){if(!g)return{content:{status:"error",error:"No session ID available for continue action."}};let y;try{y=await a.get(g)}catch(k){let x=k instanceof Error?k.message:String(k);return{content:{status:"error",error:`Failed to load flow state (session "${g}"): ${x}`}}}if(!y)return{content:{status:"error",error:`Flow state not found for session "${g}". The flow may have expired.`}};let l=y.state,f=y.step;if(!f)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let v=P(l,te(u.stateUpdates??{}));if(y.widgetId){let k=r.get(f);if(!k)return{content:{status:"error",error:`No edge from step "${f}"`}};let x=await W(k,v);return B(x,v,n,r,s,m,T)}return B(f,v,n,r,s,m,T)}return{content:{status:"error",error:`Unknown action: "${u.action}"`}}}let d={title:t.title,description:i,inputSchema:ct,annotations:t.annotations},p=(async(u,g)=>{let m=g,T=m._meta??{},y=_(T),l=$(m),f=await c(u,y,T,l);if(y)if(f.flowTokenContent&&f.content.status!=="complete")try{await a.set(y,f.flowTokenContent)}catch(k){let x=k instanceof Error?k.message:String(k);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${y}"): ${x}`},null,2)}],_meta:T,isError:!0}}else f.content.status==="complete"&&await a.delete(y);return{content:[{type:"text",text:JSON.stringify(f.content,null,2)}],_meta:T,...f.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:d,handler:p,async register(u){u.registerTool(t.id,d,p)},graph:e.graph}}function Me(e,t){let n=["flowchart TD"];n.push(` ${C}((Start))`);for(let[r]of e)n.push(` ${r}[${r}]`);n.push(` ${b}((End))`);for(let[r,o]of t)o.type==="direct"?n.push(` ${r} --> ${o.to}`):n.push(` ${r} -.-> ${r}_branch([?])`);return n.join(`
6
+ `)}var F=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===C||t===b)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return Me(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return We({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>Me(n,r)})}validate(){if(!this.edges.has(C))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(C);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==C&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function Fe(e){return new F(e)}function Ae(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ne(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...d)=>{r.push(d)}};await e.register(i);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.name}" did not register a handler`);let s={_meta:{sessionId:o}};async function c(d){return{...d,decodedState:n?await n.get(o):null}}return{async start(d,p){let u=await a({action:"start",intent:d,...p?{stateUpdates:p}:{}},s);return c(Ae(u))},async continueWith(d){let p=await a({action:"continue",...d?{stateUpdates:d}:{}},s);return c(Ae(p))},async lastState(){return n?n.get(o):null}}}var q="text/html+skybridge",L="text/html;profile=mcp-app",Ue=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function De(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function je(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function re(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0},...e.httpUrl&&{httpUrl:e.httpUrl}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:i,widgetDomain:a,prefersBorder:s=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:v}=new URL(o);(v==="localhost"||v==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${v}:*`,`wss://${v}:*`],resource_domains:[...d.resource_domains||[],`http://${v}:*`]})}catch{}let p=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,m=`${o.endsWith("/")?o.slice(0,-1):o}${i}`,T=null,y=()=>(T||(T=Ue(o,i)),T),l=r;async function f(v){let k=await y();v.registerResource(`${t}-openai-widget`,p,{title:n,description:l,mimeType:q,_meta:{"openai/widgetDescription":l,"openai/widgetPrefersBorder":s}},async x=>({contents:[{uri:x.href,mimeType:q,text:k,_meta:De({description:l,prefersBorder:s,widgetDomain:a,widgetCSP:d})}]})),v.registerResource(`${t}-mcp-widget`,u,{title:n,description:l,mimeType:L,_meta:{ui:{prefersBorder:s}}},async x=>({contents:[{uri:x.href,mimeType:L,text:k,_meta:je({description:l,prefersBorder:s,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:p,mcpUri:u,autoHeight:c,httpUrl:m,register:f}}function dt(e,t){let{resource:n,description:r,inputSchema:o,annotations:i,autoInjectResultText:a=!0}=e,s=e.id??n?.id,c=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let d=n?re({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight,httpUrl:n.httpUrl}):void 0;return{id:s,title:c,description:r,async register(p){p.registerTool(s,{title:c,description:r,inputSchema:o,annotations:i,...d&&{_meta:d}},(async(u,g)=>{let m=g,T=m._meta??{},y=$(m),l=await t(u,{extra:{_meta:T},waniwani:y});return n&&l.data?{content:[{type:"text",text:l.text}],structuredContent:l.data,_meta:{...d,...T,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:l.text}],...l.data?{structuredContent:l.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ut(e,t){await Promise.all(t.map(n=>n.register?.(e)))}var V=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var pt="@waniwani/sdk";function Ke(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(i,a,s){let c=r(),d=`${t.replace(/\/$/,"")}${a}`,p={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":pt},u={method:i,headers:p};s!==void 0&&(p["Content-Type"]="application/json",u.body=JSON.stringify(s));let g=await fetch(d,u);if(!g.ok){let T=await g.text().catch(()=>"");throw new V(T||`KB API error: HTTP ${g.status}`,g.status)}return(await g.json()).data}return{async ingest(i){return o("POST","/api/mcp/kb/ingest",{files:i})},async search(i,a){return o("POST","/api/mcp/kb/search",{query:i,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var lt="@waniwani/sdk";function z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??$e,o=mt(e),i=H(e.meta),a=H(e.metadata),s=wt(e,i),c=E(e.eventId)??r(),d=ht(e.timestamp,n),p=E(e.source)??U(i)??t.source??lt,u=oe(e)?{...e}:void 0,g={...a};return Object.keys(i).length>0&&(g.meta=i),u&&(g.rawLegacy=u),{id:c,type:"mcp.event",name:o,source:p,timestamp:d,correlation:s,properties:ft(e,o),metadata:g,rawLegacy:u}}function $e(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function ft(e,t){if(!oe(e))return H(e.properties);let n=gt(e,t),r=H(e.properties);return{...n,...r}}function gt(e,t){switch(t){case"tool.called":{let n={};return E(e.toolName)&&(n.name=e.toolName),E(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),E(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return E(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),E(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function mt(e){return oe(e)?e.eventType:e.event}function wt(e,t){let n=E(e.requestId)??ve(t),r=E(e.sessionId)??_(t),o=E(e.traceId)??xe(t),i=E(e.externalUserId)??Re(t),a=E(e.correlationId)??Ee(t)??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),a&&(s.correlationId=a),i&&(s.externalUserId=i),s}function ht(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function H(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function E(e){if(typeof e=="string"&&e.trim().length!==0)return e}function oe(e){return"eventType"in e}var yt="/api/mcp/events/v2/batch";var Be="@waniwani/sdk",Tt=new Set([401,403]),kt=new Set([408,425,429,500,502,503,504]);function qe(e){return new ie(e)}var ie=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=xt(t.apiUrl,t.endpointPath??yt),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":Be},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Rt(i)}}if(Tt.has(n.status))return{kind:"auth",status:n.status};if(kt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await vt(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Be,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],i=[];for(let a of n){let s=r.get(a.eventId);if(s){if(St(a)){o.push(s);continue}i.push(s)}}return{retryable:o,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function St(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function vt(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function xt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Rt(e){return e instanceof Error?e.message:String(e)}function Le(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?qe({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,a={async identify(s,c,d){o();let p=z({event:"user.identified",externalUserId:s,properties:c,meta:d});return i?.enqueue(p),{eventId:p.id}},async track(s){o();let c=z(s);return i?.enqueue(c),{eventId:c.id}},async flush(){o(),await i?.flush()},async shutdown(s){return o(),await i?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&Et(a,r.shutdownTimeoutMs),a}function Et(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function Y(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",r=t?.apiKey??process.env.WANIWANI_API_KEY,o={endpointPath:t?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:t?.tracking?.flushIntervalMs??1e3,maxBatchSize:t?.tracking?.maxBatchSize??20,maxBufferSize:t?.tracking?.maxBufferSize??1e3,maxRetries:t?.tracking?.maxRetries??3,retryBaseDelayMs:t?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:t?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:t?.tracking?.shutdownTimeoutMs??2e3},i={apiUrl:n,apiKey:r,tracking:o},a=Le(i),s=Ke(i);return{...a,kb:s,_config:i}}function It(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function Ct(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Y(t)),n}return async function(i){let a;try{a=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),c=[];for(let d of a.events){let p=It(d),u=await s.track(p);c.push(u.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let c=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}function G(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var se=G("widget-token",!!process.env.WANIWANI_DEBUG),bt=120*1e3,Z=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-bt?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=_t(this.config.apiUrl,"/api/mcp/widget-tokens");se("minting token from",r);let o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let i=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o),signal:AbortSignal.timeout(5e3)});if(se("mint response:",i.status),!i.ok)return null;let a=await i.json(),s=a.data&&typeof a.data=="object"?a.data:a,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch(i){return se("mint failed:",i),null}}};function _t(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Ve="waniwani/sessionId",ae="waniwani/geoLocation",ce="waniwani/userLocation";function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function K(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function de(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>S(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function Pt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ue(e,t,n,r,o,i){let a=Pt(e,n.toolType),s=K(t);return{event:"tool.called",properties:{name:e,type:a,...r??{},...i?.input!==void 0&&{input:i.input},...i?.output!==void 0&&{output:i.output}},meta:s,source:U(s),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function pe(e,t,n){try{await e.track(t)}catch(r){n?.(fe(r))}}async function le(e,t){try{await e.flush()}catch(n){t?.(fe(n))}}async function He(e,t,n,r,o){if(!S(e))return;S(e._meta)||(e._meta={});let i=e._meta,a=S(i.waniwani)?i.waniwani:void 0,s={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(s.token=u)}catch(u){o?.(fe(u))}let c=_(i);c&&(s.sessionId||(s.sessionId=c));let d=Ye(i);d!==void 0&&(s.geoLocation||(s.geoLocation=d));let p=U(K(r));p&&!s.source&&(s.source=p),i.waniwani=s}function ze(e,t){let n=K(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let r=e._meta,o=_(n);o&&!r[Ve]&&(r[Ve]=o);let i=Ye(n);i&&(r[ae]||(r[ae]=i),r[ce]||(r[ce]=i))}function Ye(e){if(!e)return;let t=e[ae]??e[ce];if(S(t)||typeof t=="string")return t}function Ge(e,t){if(!t||!S(e))return;S(e._meta)||(e._meta={});let n=e._meta;for(let[r,o]of Object.entries(t)){let i=n[r];if(!(r in n))n[r]=o;else if(S(i)&&S(o))for(let[a,s]of Object.entries(o))a in i||(i[a]=s)}}function fe(e){return e instanceof Error?e:new Error(String(e))}var J=G("mcp",!!process.env.WANIWANI_DEBUG),Ze="https://app.waniwani.ai";function Wt(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Y(),i=r.injectWidgetToken!==!1,a=o._config.apiKey?new Z({apiUrl:o._config.apiUrl??Ze,apiKey:o._config.apiKey}):null,s=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,p,u]=c,g=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof u!="function")return s(...c);let m=S(p)&&S(p._meta)?p._meta:void 0,T=u,y=async(l,f)=>{let v=K(f)??{},k=Se(o,v,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});S(f)&&(f[ee]=k);let x=performance.now(),A=e.server?.getClientVersion?.();try{let R=await T(l,f),I=Math.round(performance.now()-x);J(`tool "${g}" handler returned in ${I}ms, running post-processing...`);let X=S(R)&&R.isError===!0;if(X){let ge=de(R);console.error(`[waniwani] Tool "${g}" returned error${ge?`: ${ge}`:""}`)}return await pe(o,ue(g,f,r,{durationMs:I,status:X?"error":"ok",...X&&{errorMessage:de(R)??"Unknown tool error"}},A,{input:l,output:R}),r.onError),J(`tool "${g}" tracking done`),r.flushAfterToolCall&&await le(o,r.onError),Ge(R,m),ze(R,f),i&&(await He(R,a,o._config.apiUrl??Ze,f,r.onError),J(`tool "${g}" widget config injected`)),J(`tool "${g}" post-processing complete, returning result`),R}catch(R){let I=Math.round(performance.now()-x);throw await pe(o,ue(g,f,r,{durationMs:I,status:"error",errorMessage:R instanceof Error?R.message:String(R)},A,{input:l}),r.onError),r.flushAfterToolCall&&await le(o,r.onError),R}};if(S(p)&&S(p._meta)){let l=p._meta;l["openai/outputTemplate"]&&!l["openai/resultCanProduceWidget"]&&(l["openai/resultCanProduceWidget"]=!0,l["openai/widgetAccessible"]=!0)}return s(d,p,y)}),n}export{b as END,C as START,F as StateGraph,M as WaniwaniKvStore,Fe as createFlow,Ne as createFlowTestHarness,Oe as createResource,dt as createTool,Ct as createTrackingRoute,Q as detectPlatform,Xe as isMCPApps,Je as isOpenAI,ut as registerTools,Wt as withWaniwani};
7
7
  //# sourceMappingURL=index.js.map