@waniwani/sdk 0.11.23 → 0.11.26

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
- var C="__start__",_="__end__",ke=Symbol.for("waniwani.flow.interrupt"),Se=Symbol.for("waniwani.flow.widget");function ve(e,t){let n=t?.context,o=[];for(let[r,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let s=i;o.push({question:s.question,field:r,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:ke,questions:o,context:n}}function Re(e,t){return{__type:Se,tool:typeof e=="string"?e:e.id,...t}}function xe(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ke}function Ee(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===Se}import{z as A}from"zod";var ie="waniwani/client";function H(e){if(typeof e=="object"&&e!==null)return e[ie]}function Ie(e,t,n){return{track(o){return e.track({...o,meta:{...t,...o.meta}})},identify(o,r){return e.identify(o,r,t)},kb:e.kb,_config:n}}function K(e,t){for(let n of t){let o=e[n];if(typeof o=="string"&&o.length>0)return o}}var ht=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Tt=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],kt=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],St=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],vt=["correlationId","openai/requestId"];var be="waniwani/flow";function F(e){return e?K(e,ht):void 0}function Ce(e){return e?K(e,Tt):void 0}function _e(e){return e?K(e,kt):void 0}function Fe(e){return e?K(e,St):void 0}function Pe(e){return e?K(e,vt):void 0}var Rt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}];function j(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:o}of Rt){let r=e[n];if(typeof r=="string"&&r.length>0)return o}}function Ne(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function $(e,t){let n=t.split("."),o=e;for(let r of n){if(o==null||typeof o!="object")return;o=o[r]}return o}function xt(e,t,n){let o=t.split("."),r=o.pop();if(!r)return;let i=e;for(let s of o)(i[s]==null||typeof i[s]!="object"||Array.isArray(i[s]))&&(i[s]={}),i=i[s];i[r]=n}function Ae(e,t){let n=t.split("."),o=n.pop();if(!o)return;let r=e;for(let i of n){if(r==null||typeof r!="object")return;r=r[i]}r!=null&&typeof r=="object"&&delete r[o]}function V(e){let t={};for(let[n,o]of Object.entries(e))n.includes(".")?xt(t,n,o):o!==null&&typeof o=="object"&&!Array.isArray(o)?t=N(t,{[n]:o}):t[n]=o;return t}function N(e,t){let n={...e};for(let[o,r]of Object.entries(t))r!==null&&typeof r=="object"&&!Array.isArray(r)&&n[o]!==null&&typeof n[o]=="object"&&!Array.isArray(n[o])?n[o]=N(n[o],r):n[o]=r;return n}function se(e){return e!=null&&e!==""}async function M(e,t){return e.type==="direct"?e.to:e.condition(t)}function We(e,t,n,o){if(e.every(c=>se($(o,c.field))))return null;let r=e.filter(c=>!se($(o,c.field))),i=r.length===1,s=r[0];return{content:i&&s?{status:"interrupt",question:s.question,field:s.field,...s.suggestions?{suggestions:s.suggestions}:{},...s.context||t?{context:s.context??t}:{}}:{status:"interrupt",questions:r,...t?{context:t}:{}},flowTokenContent:{step:n,state:o,...i&&s?{field:s.field}:{}}}}async function L(e,t,n,o,r,i,s,a){let c=e,d={...t},u=[],p=50,l=0;for(;l++<p;){if(c===_)return{content:{status:"complete"},flowTokenContent:{state:d},nodesVisited:u};let R=n.get(c);if(!R)return{content:{status:"error",error:`Unknown node: "${c}"`},nodesVisited:u};a?.get(c)?.hideFromFunnel||u.push(c);try{let w=await R({state:d,meta:i,interrupt:ve,showWidget:Re,waniwani:s});if(xe(w)){for(let g of w.questions)g.validate&&r.set(`${c}:${g.field}`,g.validate);let v=We(w.questions,w.context,c,d);if(v)return{...v,nodesVisited:u};for(let g of w.questions){let E=r.get(`${c}:${g.field}`);if(E)try{let k=$(d,g.field),x=await E(k);x&&typeof x=="object"&&(d=N(d,x))}catch(k){let x=k instanceof Error?k.message:String(k);Ae(d,g.field);let b=w.questions.map(W=>W.field===g.field?{...W,context:W.context?`ERROR: ${x}
1
+ var C="__start__",_="__end__",Se=Symbol.for("waniwani.flow.interrupt"),ve=Symbol.for("waniwani.flow.widget");function Re(e,t){let n=t?.context,o=[];for(let[r,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let s=i;o.push({question:s.question,field:r,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:Se,questions:o,context:n}}function xe(e,t){return{__type:ve,tool:typeof e=="string"?e:e.id,...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 A}from"zod";var se="waniwani/client";function V(e){if(typeof e=="object"&&e!==null)return e[se]}function be(e,t,n){return{track(o){return e.track({...o,meta:{...t,...o.meta}})},identify(o,r){return e.identify(o,r,t)},kb:e.kb,_config:n}}function j(e,t){for(let n of t){let o=e[n];if(typeof o=="string"&&o.length>0)return o}}var ht=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Tt=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],kt=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],St=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],vt=["correlationId","openai/requestId"];var U="waniwani/flow";function F(e){return e?j(e,ht):void 0}function Ce(e){return e?j(e,Tt):void 0}function _e(e){return e?j(e,kt):void 0}function Fe(e){return e?j(e,St):void 0}function Pe(e){return e?j(e,vt):void 0}var Rt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}];function $(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:o}of Rt){let r=e[n];if(typeof r=="string"&&r.length>0)return o}}function Ne(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function L(e,t){let n=t.split("."),o=e;for(let r of n){if(o==null||typeof o!="object")return;o=o[r]}return o}function xt(e,t,n){let o=t.split("."),r=o.pop();if(!r)return;let i=e;for(let s of o)(i[s]==null||typeof i[s]!="object"||Array.isArray(i[s]))&&(i[s]={}),i=i[s];i[r]=n}function Ae(e,t){let n=t.split("."),o=n.pop();if(!o)return;let r=e;for(let i of n){if(r==null||typeof r!="object")return;r=r[i]}r!=null&&typeof r=="object"&&delete r[o]}function z(e){let t={};for(let[n,o]of Object.entries(e))n.includes(".")?xt(t,n,o):o!==null&&typeof o=="object"&&!Array.isArray(o)?t=N(t,{[n]:o}):t[n]=o;return t}function N(e,t){let n={...e};for(let[o,r]of Object.entries(t))r!==null&&typeof r=="object"&&!Array.isArray(r)&&n[o]!==null&&typeof n[o]=="object"&&!Array.isArray(n[o])?n[o]=N(n[o],r):n[o]=r;return n}function ae(e){return e!=null&&e!==""}async function M(e,t){return e.type==="direct"?e.to:e.condition(t)}function We(e,t,n,o){if(e.every(c=>ae(L(o,c.field))))return null;let r=e.filter(c=>!ae(L(o,c.field))),i=r.length===1,s=r[0];return{content:i&&s?{status:"interrupt",question:s.question,field:s.field,...s.suggestions?{suggestions:s.suggestions}:{},...s.context||t?{context:s.context??t}:{}}:{status:"interrupt",questions:r,...t?{context:t}:{}},flowTokenContent:{step:n,state:o,...i&&s?{field:s.field}:{}}}}async function q(e,t,n,o,r,i,s,a){let c=e,d={...t},u=[],f=50,l=0;for(;l++<f;){if(c===_)return{content:{status:"complete"},flowTokenContent:{state:d},nodesVisited:u};let v=n.get(c);if(!v)return{content:{status:"error",error:`Unknown node: "${c}"`},nodesVisited:u};a?.get(c)?.hideFromFunnel||u.push(c);try{let w=await v({state:d,meta:i,interrupt:Re,showWidget:xe,waniwani:s});if(Ee(w)){for(let g of w.questions)g.validate&&r.set(`${c}:${g.field}`,g.validate);let R=We(w.questions,w.context,c,d);if(R)return{...R,nodesVisited:u};for(let g of w.questions){let E=r.get(`${c}:${g.field}`);if(E)try{let k=L(d,g.field),x=await E(k);x&&typeof x=="object"&&(d=N(d,x))}catch(k){let x=k instanceof Error?k.message:String(k);Ae(d,g.field);let b=w.questions.map(W=>W.field===g.field?{...W,context:W.context?`ERROR: ${x}
2
2
 
3
- ${W.context}`:`ERROR: ${x}`}:W),P=We(b,w.context,c,d);if(P)return{...P,nodesVisited:u};break}}let T=o.get(c);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(T,d);continue}if(Ee(w)){let v=w.field;if(v&&se($(d,v))){let T=o.get(c);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(T,d);continue}return{content:{status:"widget",tool:w.tool,data:w.data,description:w.description,interactive:w.interactive!==!1},flowTokenContent:{step:c,state:d,field:v,widgetId:w.tool},nodesVisited:u}}d=N(d,w);let h=o.get(c);if(!h)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(h,d)}catch(f){return{content:{status:"error",error:f instanceof Error?f.message:String(f)},flowTokenContent:{step:c,state:d},nodesVisited:u}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"},nodesVisited:u}}function ae(e){return typeof e=="object"&&e!==null&&e.__ww_enc===1&&typeof e.ct=="string"&&typeof e.iv=="string"}var Me=new Map;async function Ue(e){let t=Me.get(e);if(t)return t;let n=Buffer.from(e,"base64");if(n.length!==32)throw new Error("[WaniWani KV] WANIWANI_ENCRYPTION_KEY must be a base64-encoded 32-byte (256-bit) key.");let o=await globalThis.crypto.subtle.importKey("raw",n,{name:"AES-GCM"},!1,["encrypt","decrypt"]);return Me.set(e,o),o}async function De(e,t){let n=await Ue(t),o=globalThis.crypto.getRandomValues(new Uint8Array(12)),r=new TextEncoder().encode(JSON.stringify(e)),i=await globalThis.crypto.subtle.encrypt({name:"AES-GCM",iv:o},n,r);return{__ww_enc:1,ct:Buffer.from(i).toString("base64"),iv:Buffer.from(o).toString("base64")}}async function Oe(e,t){let n=await Ue(t),o=Buffer.from(e.ct,"base64"),r=Buffer.from(e.iv,"base64"),i;try{i=await globalThis.crypto.subtle.decrypt({name:"AES-GCM",iv:r},n,o)}catch{throw new Error("[WaniWani KV] Decryption failed. The encryption key may be incorrect or the data may be corrupted.")}return JSON.parse(new TextDecoder().decode(i))}var Et={debug:0,warn:1,error:2,none:3};function It(){let e=process.env.WANIWANI_LOG_LEVEL;return e&&e in Et?e:process.env.WANIWANI_DEBUG?"debug":"none"}function U(e,t){return t??It()==="debug"?(...o)=>console.log(`[waniwani:${e}]`,...o):()=>{}}var bt="@waniwani/sdk",Ct="https://app.waniwani.ai",_t=U("kv"),D=class{get baseUrl(){return(process.env.WANIWANI_API_URL??Ct).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}get encryptionKey(){return process.env.WANIWANI_ENCRYPTION_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let n=await this.request("/api/mcp/redis/get",{key:t});if(n==null)return null;if(ae(n)){if(!this.encryptionKey)throw new Error("[WaniWani KV] Encrypted data found but WANIWANI_ENCRYPTION_KEY is not set.");return Oe(n,this.encryptionKey)}return n}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let o=this.encryptionKey;_t(`set "${t}" \u2014 encryption ${o?"enabled":"disabled (no WANIWANI_ENCRYPTION_KEY)"}`);let r=o?await De(n,o):n;await this.request("/api/mcp/redis/set",{key:t,value:r})}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 o=`${this.baseUrl}${t}`,r=await fetch(o,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":bt},body:JSON.stringify(n)});if(!r.ok){let s=await r.text().catch(()=>"");throw new Error(s||`KV store API error: HTTP ${r.status}`)}return(await r.json()).data}};var z=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,n){this.map.set(t,n)}async delete(t){this.map.delete(t)}};var q=class{store=new D;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Ft(e){let t=e.toString();return t.includes("showWidget")?"widget":t.includes("interrupt")?"interrupt":"action"}function Pt(e,t){let n=e.toString(),o=[];for(let r of t){let i=`"${r}"`,s=`'${r}'`,a=`\`${r}\``;(n.includes(i)||n.includes(s)||n.includes(a))&&o.push(r)}return o}function Ke(e,t,n,o){let r=new Set([...t.keys(),_]),i=[];for(let[a,c]of t){let d=o.get(a);i.push({id:a,type:Ft(c),label:d?.label??a,...d?.hideFromFunnel?{hideFromFunnel:!0}:{}})}let s=[];for(let[a,c]of n)if(c.type==="direct")s.push({from:a,to:c.to,type:"direct"});else{let d=Pt(c.condition,r);s.push({from:a,to:d,type:"conditional"})}return{flowId:e.id,title:e.title,nodes:i,edges:s}}function je(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let o=Object.keys(n.entries).map(r=>`"${r}"`).join(" | ");return t?`${o} \u2014 ${t}`:o}return t}function $e(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."," Do NOT invent missing intent."," Optionally include `context` \u2014 the situation or environment that led the user to start"," this flow (e.g. what page they are on, what they were doing, or what triggered the request)."," Only provide `context` when there is genuinely relevant situational information. 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.omitIntentPII&&t.push(" Do NOT include PII in `intent` or `context` \u2014 no names, emails, phones, addresses, IDs, ages, or birthdates.",' Summarize the goal abstractly (e.g. "user wants a quote", not "Jane Doe wants a quote").'),e.state){let n=[];for(let[o,r]of Object.entries(e.state)){let i=Ne(r);if(i){let s=r.description??"",a=Object.entries(i).map(([c,d])=>{let u=je(d);return u?`\`${o}.${c}\` (${u})`:`\`${o}.${c}\``}).join(", ");n.push(s?`\`${o}\` (${s}): ${a}`:`\`${o}\`: ${a}`)}else{let s=je(r);n.push(s?`\`${o}\` (${s})`:`\`${o}\``)}}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.","5. CORRECTION: If the user wants to CHANGE a previously-answered field",' (e.g. "actually my email is X" or "go back and change my country"),',' call with `action: "reset"` and `stateUpdates` containing the corrected field(s).'," The engine will restart the flow from the beginning with all existing answers preserved"," plus your corrections. Steps with filled answers will be auto-skipped."," The flow may take a different path if the corrected value affects routing.",' Do NOT use "reset" for the CURRENT question \u2014 use "continue" for that.',"6. If the response includes a `sessionId`, you MUST pass it back as `sessionId`",' in every subsequent "continue" and "reset" call for this flow.'),t.join(`
4
- `)}function ce(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function Le(e){let t=qe(e),n=ce(t.waniwani)?t.waniwani:{};return e.meta({...t,waniwani:{...n,redacted:!0}})}function Nt(e){let n=qe(e).waniwani;return ce(n)&&n.redacted===!0}function qe(e){let t=e.meta;if(typeof t!="function")return{};let n=t.call(e);return ce(n)?n:{}}function Be(e){if(!e)return[];let t=[];for(let[n,o]of Object.entries(e))Nt(o)&&t.push(n);return t}var Y="waniwani/redactedStateUpdateFields";function At(e){let t=e.omitIntentPII?" Do not include PII (names, emails, phones, addresses, IDs, ages, birthdates) \u2014 summarize abstractly.":"",o=e.state&&Object.keys(e.state).length>0?A.object(e.state).partial().passthrough():A.record(A.string(),A.unknown());return{action:A.enum(["start","continue","reset"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget), "reset" to restart from the beginning with a correction to a previously-collected field'),intent:A.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow. Do not invent missing intent.${t}`),context:A.string().optional().describe(`Optional when action is "start". Describe the situation or environment that led the user to start this flow \u2014 e.g. what page they are on, what they were doing, or what triggered the request. Do not invent missing context.${t}`),stateUpdates:o.optional().describe(`State field values to set before processing the next node. Pass the user's answer (keyed by the field name from the response) and any other values the user mentioned. For nested state fields, use dot-paths like "driver.name".`),sessionId:A.string().optional().describe('Session identifier. If the response includes a `sessionId`, pass it back on every subsequent "continue" and "reset" call for this flow.')}}function Wt(e){if(process.env.WANIWANI_API_KEY)return new q;throw new Error(`[waniwani] createFlow "${e}": no flow store configured. Pass { store } to .compile() \u2014 use MemoryKvStore from "@waniwani/sdk/mcp" for local development, or plug in a Redis/Upstash/Cloudflare KV adapter for production. Alternatively, set WANIWANI_API_KEY to use hosted flow state on app.waniwani.ai.`)}function He(e){let{config:t,nodes:n,edges:o}=e,r=At(t),i=Ke(t,n,o,e.nodeOptions),s=$e(t),a=`${t.description}
5
- ${s}`,c=e.store??Wt(t.id),d=new Map;async function u(f,w,h,v){if(f.action==="start"){let T=typeof f.intent=="string"?f.intent.trim():void 0;if(!T)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.`}};if(f.intent=T,typeof f.context=="string"){let x=f.context.trim();f.context=x||void 0}let g=o.get(C);if(!g)return{content:{status:"error",error:"No start edge"}};let E=V(f.stateUpdates??{}),k=await M(g,E);return L(k,E,n,o,d,h,v,e.nodeOptions)}if(f.action==="continue"){if(!w)return{content:{status:"error",error:"No session ID available for continue action."}};let T;try{T=await c.get(w)}catch(x){let b=x instanceof Error?x.message:String(x);return{content:{status:"error",error:`Failed to load flow state (session "${w}"): ${b}`}}}if(!T)return{content:{status:"error",error:`Flow state not found for session "${w}". The flow may have expired.`}};let g=T.state,E=T.step;if(!E)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let k=N(g,V(f.stateUpdates??{}));if(T.widgetId){let x=o.get(E);if(!x)return{content:{status:"error",error:`No edge from step "${E}"`}};let b=await M(x,k);return L(b,k,n,o,d,h,v,e.nodeOptions)}return L(E,k,n,o,d,h,v,e.nodeOptions)}if(f.action==="reset"){if(!w)return{content:{status:"error",error:"No session ID available for reset action."}};let T;try{T=await c.get(w)}catch(b){let P=b instanceof Error?b.message:String(b);return{content:{status:"error",error:`Failed to load flow state (session "${w}"): ${P}`}}}if(!T)return{content:{status:"error",error:`Flow state not found for session "${w}". The flow may have completed or expired. Use action "start" to begin a new flow.`}};if(!f.stateUpdates||Object.keys(f.stateUpdates).length===0)return{content:{status:"error",error:'Missing "stateUpdates" for action "reset". Include the corrected field(s).'}};let g=o.get(C);if(!g)return{content:{status:"error",error:"No start edge"}};let E=T.state,k=N(E,V(f.stateUpdates)),x=await M(g,k);return L(x,k,n,o,d,h,v,e.nodeOptions)}return{content:{status:"error",error:`Unknown action: "${f.action}"`}}}let p=Be(t.state),l={title:t.title,description:a,inputSchema:r,annotations:t.annotations,...p.length>0&&{_meta:{[Y]:p}}},R=(async(f,w)=>{let h=w,v=h._meta??{},T=F(v),g=T??f.sessionId;!g&&f.action==="start"&&(g=crypto.randomUUID(),v["waniwani/sessionId"]=g);let E=H(h),k=await u(f,g,v,E);if(g)if(k.flowTokenContent&&k.content.status!=="complete")try{await c.set(g,k.flowTokenContent)}catch(P){let W=P instanceof Error?P.message:String(P);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${g}"): ${W}`},null,2)}],_meta:v,isError:!0}}else k.content.status==="complete"&&await c.delete(g);let x=!T&&g?{...k.content,sessionId:g}:k.content,b=[{type:"text",text:JSON.stringify(x,null,2)}];return k.nodesVisited?.length&&(v[be]={flowId:t.id,nodesVisited:k.nodesVisited}),{content:b,_meta:v,...k.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:l,handler:R,async register(f){let w={...l,_meta:{...l._meta,_flowGraph:i}};f.registerTool(t.id,w,R)},graph:e.graph,flowGraph:i}}function Ve(e,t){let n=["flowchart TD"];n.push(` ${C}((Start))`);for(let[o]of e)n.push(` ${o}[${o}]`);n.push(` ${_}((End))`);for(let[o,r]of t)r.type==="direct"?n.push(` ${o} --> ${r.to}`):n.push(` ${o} -.-> ${o}_branch([?])`);return n.join(`
6
- `)}var O=class{nodes=new Map;edges=new Map;nodeOptions=new Map;config;constructor(t){this.config=t}addNode(t,n,o){let r=typeof t=="string"?t:t.id,i=typeof t=="string"?n:t.run,s=typeof t=="string"?o?.label:t.label,a=typeof t=="string"?o?.hideFromFunnel:t.hideFromFunnel;if(r===C||r===_)throw new Error(`"${r}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(r))throw new Error(`Node "${r}" already exists`);return this.nodes.set(r,i),(s!==void 0||a!==void 0)&&this.nodeOptions.set(r,{label:s??r,hideFromFunnel:a}),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 Ve(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),o=new Map(this.edges);return He({config:this.config,nodes:n,edges:o,store:t?.store,graph:()=>Ve(n,o),nodeOptions:new Map(this.nodeOptions)})}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!==_&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,o]of this.edges){if(n!==C&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(o.type==="direct"&&o.to!==_&&!this.nodes.has(o.to))throw new Error(`Edge from "${n}" references non-existent node: "${o.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 ze(e){return new O(e)}function de(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ye(e,t){let n=t?.stateStore,o=[],r=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...d)=>{o.push(d)}};await e.register(i);let s=o[0]?.[2];if(!s)throw new Error(`Flow "${e.name}" did not register a handler`);let a={_meta:{sessionId:r}};async function c(d){return{...d,decodedState:n?await n.get(r):null}}return{async start(d,u,p){let l=await s({action:"start",intent:d,...p?{context:p}:{},...u?{stateUpdates:u}:{}},a);return c(de(l))},async continueWith(d){let u=await s({action:"continue",...d?{stateUpdates:d}:{}},a);return c(de(u))},async resetWith(d){let u=await s({action:"reset",stateUpdates:d},a);return c(de(u))},async lastState(){return n?n.get(r):null}}}var G=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};var Mt="@waniwani/sdk";function Ge(e){let{apiUrl:t,apiKey:n}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function r(i,s,a){let c=o(),d=`${t.replace(/\/$/,"")}${s}`,u={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Mt},p={method:i,headers:u};a!==void 0&&(u["Content-Type"]="application/json",p.body=JSON.stringify(a));let l=await fetch(d,p);if(!l.ok){let f=await l.text().catch(()=>"");throw new G(f||`KB API error: HTTP ${l.status}`,l.status)}return(await l.json()).data}return{async ingest(i){return r("POST","/api/mcp/kb/ingest",{files:i})},async search(i,s){return r("POST","/api/mcp/kb/search",{query:i,...s})},async sources(){return r("GET","/api/mcp/kb/sources")}}}var Ut="@waniwani/sdk";function J(e,t={}){let n=t.now??(()=>new Date),o=t.generateId??Ze,r=Kt(e),i=Z(e.meta),s=Z(e.metadata),a=jt(e,i),c=I(e.eventId)??o(),d=$t(e.timestamp,n),u=I(e.source)??j(i)??t.source??Ut,p=ue(e)?{...e}:void 0,l={...s};return Object.keys(i).length>0&&(l.meta=i),p&&(l.rawLegacy=p),{id:c,type:"mcp.event",name:r,source:u,timestamp:d,correlation:a,properties:Dt(e,r),metadata:l,rawLegacy:p}}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 Dt(e,t){if(!ue(e))return Z(e.properties);let n=Ot(e,t),o=Z(e.properties);return{...n,...o}}function Ot(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 Kt(e){return ue(e)?e.eventType:e.event}function jt(e,t){let n=I(e.requestId)??Ce(t),o=I(e.sessionId)??F(t),r=I(e.traceId)??_e(t),i=I(e.externalUserId)??Fe(t),s=I(e.correlationId)??Pe(t)??n,a={};return o&&(a.sessionId=o),r&&(a.traceId=r),n&&(a.requestId=n),s&&(a.correlationId=s),i&&(a.externalUserId=i),a}function $t(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 Z(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function I(e){if(typeof e=="string"&&e.trim().length!==0)return e}function ue(e){return"eventType"in e}var Lt="/api/mcp/events/v2/batch";var Je="@waniwani/sdk",qt=new Set([401,403]),Bt=new Set([408,425,429,500,502,503,504]);function Xe(e){return new le(e)}var le=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=zt(t.apiUrl,t.endpointPath??Lt),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(o=>setTimeout(o,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,o=this.flush();if(!Number.isFinite(n)||n<=0)return await o,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let r=Symbol("shutdown-timeout");return await Promise.race([o.then(()=>"flushed"),this.sleep(n).then(()=>r)])===r?(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,o=t;for(;o.length>0&&!this.isStopped;){this.inFlightCount=o.length;let r=await this.sendBatchOnce(o);switch(this.inFlightCount=0,r.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(r.status,o.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",o.length,r.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",o.length,r.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(r.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",r.permanent.length),r.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",r.retryable.length);return}o=r.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":Je},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Yt(i)}}if(qt.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 o=await Vt(n);if(!o?.rejected||o.rejected.length===0)return{kind:"success"};let r=this.classifyRejectedEvents(t,o.rejected);return r.retryable.length===0&&r.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:r.retryable,permanent:r.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Je,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let o=new Map(t.map(s=>[s.id,s])),r=[],i=[];for(let s of n){let a=o.get(s.eventId);if(a){if(Ht(s)){r.push(a);continue}i.push(a)}}return{retryable:r,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let o=this.buffer.length;this.buffer.splice(0,o),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+o)}};function Ht(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 zt(e,t){let n=e.endsWith("/")?e:`${e}/`,o=t.startsWith("/")?t.slice(1):t;return`${n}${o}`}function Yt(e){return e instanceof Error?e.message:String(e)}function Qe(e){let{apiUrl:t,apiKey:n,tracking:o}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?Xe({apiUrl:t,apiKey:n,endpointPath:o.endpointPath,flushIntervalMs:o.flushIntervalMs,maxBatchSize:o.maxBatchSize,maxBufferSize:o.maxBufferSize,maxRetries:o.maxRetries,retryBaseDelayMs:o.retryBaseDelayMs,retryMaxDelayMs:o.retryMaxDelayMs,shutdownTimeoutMs:o.shutdownTimeoutMs}):void 0,s={async identify(a,c,d){r();let u=J({event:"user.identified",externalUserId:a,properties:c,meta:d});return i?.enqueue(u),{eventId:u.id}},async track(a){r();let c=J(a);return i?.enqueue(c),{eventId:c.id}},async flush(){r(),await i?.flush()},async shutdown(a){return r(),await i?.shutdown({timeoutMs:a?.timeoutMs??o.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&Gt(s,o.shutdownTimeoutMs),s}function Gt(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 X(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",o=t?.apiKey??process.env.WANIWANI_API_KEY,r={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:o,tracking:r},s=Qe(i),a=Ge(i);return{...s,kb:a,_config:i}}function Zt(e){let t=e.event_type??"widget_click",o=t.startsWith("widget_")?t:`widget_${t}`,r={...e.metadata??{}};return e.event_name&&(r.event_name=e.event_name),{event:o,properties:r,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function Jt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function o(){return n||(n=X(t)),n}return async function(i){let s;try{s=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(s.events)||s.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=o(),c=[];for(let d of s.events){let u=Zt(d),p=await a.track(u);c.push(p.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let c=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var pe=U("widget-token"),Xt=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-Xt?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let o=Qt(this.config.apiUrl,"/api/mcp/widget-tokens");pe("minting token from",o);let r={};t&&(r.sessionId=t),n&&(r.traceId=n);try{let i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(r),signal:AbortSignal.timeout(5e3)});if(pe("mint response:",i.status),!i.ok)return null;let s=await i.json(),a=s.data&&typeof s.data=="object"?s.data:s,c=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(c)?null:(this.cached={token:a.token,expiresAt:c},a.token)}catch(i){return pe("mint failed:",i),null}}};function Qt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}async function et(e){if(typeof globalThis.crypto?.subtle?.digest=="function"){let n=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(e));return Array.from(new Uint8Array(n)).map(o=>o.toString(16).padStart(2,"0")).join("")}let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n)|0;return`simple-${Math.abs(t).toString(36)}`}function en(e){return et(JSON.stringify({nodes:e.nodes,edges:e.edges}))}async function tt(e){if(e.length===0)return null;let t=[...e].sort((r,i)=>r.flowId.localeCompare(i.flowId)),n=await et(JSON.stringify(t.map(r=>({flowId:r.flowId,nodes:r.nodes,edges:r.edges})))),o=await Promise.all(e.map(async r=>({flowId:r.flowId,title:r.title,configHash:await en(r),nodes:r.nodes,edges:r.edges})));return{compositeHash:n,flows:o}}var nt="waniwani/sessionId",ee="waniwani/geoLocation",te="waniwani/userLocation",tn="openai/userLocation",nn=[tn,ee,te];function ot(e,t){if(t.length===0)return e;let n;for(let o of nn){let r=e[o];if(!S(r))continue;let i;for(let s of t)s in r&&(i||(i={...r}),delete i[s]);i&&(n||(n={...e}),n[o]=i)}return n??e}function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function B(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function fe(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(o=>S(o)&&o.type==="text"&&typeof o.text=="string")?.text:void 0}function on(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ge(e,t,n,o,r,i){let s=on(e,n.toolType),a=n.stripLocationFields,c=a&&a.length>0,d=B(t),u=d&&c?ot(d,a):d,p=i?.input!==void 0&&n.redactInput?n.redactInput(i.input):i?.input,l=c&&S(i?.output)&&S(i.output._meta)?{...i.output,_meta:ot(i.output._meta,a)}:i?.output;return{event:"tool.called",properties:{name:e,type:s,...o??{},...p!==void 0&&{input:p},...l!==void 0&&{output:l}},meta:u,source:j(u),metadata:{...n.metadata??{},...r&&{clientInfo:r},...n.funnelSync&&{funnelSync:n.funnelSync}}}}async function we(e,t,n){try{await e.track(t)}catch(o){n?.(ye(o))}}async function me(e,t){try{await e.flush()}catch(n){t?.(ye(n))}}async function it(e,t,n,o,r){if(!S(e))return;S(e._meta)||(e._meta={});let i=e._meta,s=S(i.waniwani)?i.waniwani:void 0,a={...s??{},endpoint:s?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let p=await t.getToken();p&&(a.token=p)}catch(p){r?.(ye(p))}let c=F(i);c&&(a.sessionId||(a.sessionId=c));let d=ct(i);d!==void 0&&(a.geoLocation||(a.geoLocation=d));let u=j(B(o));u&&!a.source&&(a.source=u),i.waniwani=a}var rt=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function st(e,t){if(!t||!S(e))return;let n=!1;for(let r of rt)if(r in t){n=!0;break}if(!n)return;S(e._meta)||(e._meta={});let o=e._meta;for(let r of rt)r in t&&(r in o||(o[r]=t[r]))}function at(e,t){let n=B(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let o=e._meta,r=F(n);r&&!o[nt]&&(o[nt]=r);let i=ct(n);i&&(o[ee]||(o[ee]=i),o[te]||(o[te]=i))}function ct(e){if(!e)return;let t=e[ee]??e[te];if(S(t)||typeof t=="string")return t}function ye(e){return e instanceof Error?e:new Error(String(e))}function dt(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function ut(e){if(typeof e.sessionId=="string"&&e.sessionId)return e.sessionId;if(dt(e.requestInfo)){let t=e.requestInfo.headers;if(dt(t)){let n={};for(let i of Object.keys(t))n[i.toLowerCase()]=t[i];let o=n["mcp-session-id"];if(typeof o=="string"&&o)return o;let r=n["x-waniwani-session-id"];if(typeof r=="string"&&r)return r}}}var pt=Symbol.for("waniwani.wrappedHandler"),ne=U("mcp"),ft="https://app.waniwani.ai",rn="REDACTED";function sn(e){if(!e)return;let t=e[Y];if(!Array.isArray(t)||t.length===0)return;let n=new Set(t.filter(o=>typeof o=="string"));if(n.size!==0)return o=>{if(!S(o))return o;let r=o.stateUpdates;if(!S(r))return o;let i=!1,s={...r};for(let a of n)a in s&&(s[a]=rn,i=!0);return i?{...o,stateUpdates:s}:o}}function lt(e,t,n,o){let{server:r,tracker:i,opts:s,tokenCache:a,injectToken:c}=n,d=s.applyFieldRedactions===!0?sn(o):void 0,u=async(p,l)=>{let R=d?{...s,redactInput:d,funnelSync:n.funnelSync}:{...s,funnelSync:n.funnelSync},f=B(l)??{};if(!F(f)&&S(l)){let g=ut(l);g&&(f["waniwani/sessionId"]=g,l._meta=f)}let h=Ie(i,f,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});S(l)&&(l[ie]=h);let v=performance.now(),T=r.server?.getClientVersion?.();try{let g=await t(p,l),E=Math.round(performance.now()-v);ne(`tool "${e}" handler returned in ${E}ms, running post-processing...`);let k=S(g)&&g.isError===!0;if(k){let x=fe(g);console.error(`[waniwani] Tool "${e}" returned error${x?`: ${x}`:""}`)}return await we(i,ge(e,l,R,{durationMs:E,status:k?"error":"ok",...k&&{errorMessage:fe(g)??"Unknown tool error"}},T,{input:p,output:g}),s.onError),ne(`tool "${e}" tracking done`),s.flushAfterToolCall&&await me(i,s.onError),at(g,l),st(g,o),c&&(await it(g,a,i._config.apiUrl??ft,l,s.onError),ne(`tool "${e}" widget config injected`)),ne(`tool "${e}" post-processing complete, returning result`),g}catch(g){let E=Math.round(performance.now()-v);throw await we(i,ge(e,l,R,{durationMs:E,status:"error",errorMessage:g instanceof Error?g.message:String(g)},T,{input:p}),s.onError),s.flushAfterToolCall&&await me(i,s.onError),g}};return u[pt]=!0,u}async function an(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let o=t??{},r=o.client??X(),i=o.injectWidgetToken!==!1,s=r._config.apiKey?new Q({apiUrl:r._config.apiUrl??ft,apiKey:r._config.apiKey}):null,a={server:e,tracker:r,opts:o,tokenCache:s,injectToken:i,funnelSync:null},c=e.registerTool.bind(e);n.registerTool=((...u)=>{let[p,l,R]=u;if(typeof R!="function")return c(...u);let f=typeof p=="string"&&p.trim().length>0?p:"unknown",w=S(l)&&S(l._meta)?l._meta:void 0,h=lt(f,R,a,w);return c(p,l,h)});let d=e._registeredTools;if(S(d))for(let[u,p]of Object.entries(d)){if(!S(p))continue;let l=p.handler;if(typeof l!="function"||l[pt])continue;let R=S(p._meta)?p._meta:void 0;p.handler=lt(u,l,a,R)}if(r._config.apiKey){let u=e._registeredTools,p=[];if(u&&typeof u=="object"){for(let l of Object.values(u))if(l&&typeof l=="object"){let R=l._meta,f=R&&typeof R=="object"?R._flowGraph:void 0;f?.nodes?.length&&p.push(f)}}p.length>0&&(a.funnelSync=await tt(p))}return n}function he(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function cn(){return he()==="openai"}function dn(){return he()==="mcp-apps"}var oe="text/html+skybridge",re="text/html;profile=mcp-app",gt=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function wt(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function mt(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 Te(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.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function yt(e){let{id:t,title:n,description:o,baseUrl:r,htmlPath:i,widgetDomain:s,prefersBorder:a=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[r],resource_domains:[r]};if(process.env.NODE_ENV==="development")try{let{hostname:h}=new URL(r);(h==="localhost"||h==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${h}:*`,`wss://${h}:*`],resource_domains:[...d.resource_domains||[],`http://${h}:*`]})}catch{}let u=`ui://widgets/apps-sdk/${t}.html`,p=`ui://widgets/ext-apps/${t}.html`,l=null,R=()=>(l||(l=gt(r,i)),l),f=o;async function w(h){let v=await R();h.registerResource(`${t}-openai-widget`,u,{title:n,description:f,mimeType:oe,_meta:{"openai/widgetDescription":f,"openai/widgetPrefersBorder":a}},async T=>({contents:[{uri:T.href,mimeType:oe,text:v,_meta:wt({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]})),h.registerResource(`${t}-mcp-widget`,p,{title:n,description:f,mimeType:re,_meta:{ui:{prefersBorder:a}}},async T=>({contents:[{uri:T.href,mimeType:re,text:v,_meta:mt({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]}))}return{id:t,title:n,description:o,openaiUri:u,mcpUri:p,autoHeight:c,register:w}}function un(e,t){let{resource:n,description:o,inputSchema:r,annotations:i,autoInjectResultText:s=!0}=e,a=e.id??n?.id,c=e.title??n?.title;if(!a)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?Te({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:a,title:c,description:o,async register(u){u.registerTool(a,{title:c,description:o,inputSchema:r,annotations:i,...d&&{_meta:d}},(async(p,l)=>{let R=l,f=R._meta??{},w=H(R),h=await t(p,{extra:{_meta:f},waniwani:w});return n&&h.data?{content:[{type:"text",text:h.text}],structuredContent:h.data,_meta:{...d,...f,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:h.text}],...h.data?{structuredContent:h.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ln(e,t){await Promise.all(t.map(n=>n.register(e)))}export{_ as END,z as MemoryKvStore,C as START,O as StateGraph,D as WaniwaniKvStore,ze as createFlow,Ye as createFlowTestHarness,yt as createResource,un as createTool,Jt as createTrackingRoute,he as detectPlatform,dn as isMCPApps,cn as isOpenAI,Le as redacted,ln as registerTools,an as withWaniwani};
3
+ ${W.context}`:`ERROR: ${x}`}:W),P=We(b,w.context,c,d);if(P)return{...P,nodesVisited:u};break}}let T=o.get(c);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(T,d);continue}if(Ie(w)){let R=w.field;if(R&&ae(L(d,R))){let T=o.get(c);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(T,d);continue}return{content:{status:"widget",tool:w.tool,data:w.data,description:w.description,interactive:w.interactive!==!1},flowTokenContent:{step:c,state:d,field:R,widgetId:w.tool},nodesVisited:u}}d=N(d,w);let h=o.get(c);if(!h)return{content:{status:"error",error:`No outgoing edge from node "${c}"`},nodesVisited:u};c=await M(h,d)}catch(p){return{content:{status:"error",error:p instanceof Error?p.message:String(p)},flowTokenContent:{step:c,state:d},nodesVisited:u}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"},nodesVisited:u}}function ce(e){return typeof e=="object"&&e!==null&&e.__ww_enc===1&&typeof e.ct=="string"&&typeof e.iv=="string"}var Me=new Map;async function Ue(e){let t=Me.get(e);if(t)return t;let n=Buffer.from(e,"base64");if(n.length!==32)throw new Error("[WaniWani KV] WANIWANI_ENCRYPTION_KEY must be a base64-encoded 32-byte (256-bit) key.");let o=await globalThis.crypto.subtle.importKey("raw",n,{name:"AES-GCM"},!1,["encrypt","decrypt"]);return Me.set(e,o),o}async function De(e,t){let n=await Ue(t),o=globalThis.crypto.getRandomValues(new Uint8Array(12)),r=new TextEncoder().encode(JSON.stringify(e)),i=await globalThis.crypto.subtle.encrypt({name:"AES-GCM",iv:o},n,r);return{__ww_enc:1,ct:Buffer.from(i).toString("base64"),iv:Buffer.from(o).toString("base64")}}async function Oe(e,t){let n=await Ue(t),o=Buffer.from(e.ct,"base64"),r=Buffer.from(e.iv,"base64"),i;try{i=await globalThis.crypto.subtle.decrypt({name:"AES-GCM",iv:r},n,o)}catch{throw new Error("[WaniWani KV] Decryption failed. The encryption key may be incorrect or the data may be corrupted.")}return JSON.parse(new TextDecoder().decode(i))}var Et={debug:0,warn:1,error:2,none:3};function It(){let e=process.env.WANIWANI_LOG_LEVEL;return e&&e in Et?e:process.env.WANIWANI_DEBUG?"debug":"none"}function D(e,t){return t??It()==="debug"?(...o)=>console.log(`[waniwani:${e}]`,...o):()=>{}}var bt="@waniwani/sdk",Ct="https://app.waniwani.ai",_t=D("kv"),O=class{get baseUrl(){return(process.env.WANIWANI_API_URL??Ct).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}get encryptionKey(){return process.env.WANIWANI_ENCRYPTION_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let n=await this.request("/api/mcp/redis/get",{key:t});if(n==null)return null;if(ce(n)){if(!this.encryptionKey)throw new Error("[WaniWani KV] Encrypted data found but WANIWANI_ENCRYPTION_KEY is not set.");return Oe(n,this.encryptionKey)}return n}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let o=this.encryptionKey;_t(`set "${t}" \u2014 encryption ${o?"enabled":"disabled (no WANIWANI_ENCRYPTION_KEY)"}`);let r=o?await De(n,o):n;await this.request("/api/mcp/redis/set",{key:t,value:r})}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 o=`${this.baseUrl}${t}`,r=await fetch(o,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":bt},body:JSON.stringify(n)});if(!r.ok){let s=await r.text().catch(()=>"");throw new Error(s||`KV store API error: HTTP ${r.status}`)}return(await r.json()).data}};var Y=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,n){this.map.set(t,n)}async delete(t){this.map.delete(t)}};var B=class{store=new O;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Ft(e){let t=e.toString();return t.includes("showWidget")?"widget":t.includes("interrupt")?"interrupt":"action"}function Pt(e,t){let n=e.toString(),o=[];for(let r of t){let i=`"${r}"`,s=`'${r}'`,a=`\`${r}\``;(n.includes(i)||n.includes(s)||n.includes(a))&&o.push(r)}return o}function Ke(e,t,n,o){let r=new Set([...t.keys(),_]),i=[];for(let[a,c]of t){let d=o.get(a);i.push({id:a,type:Ft(c),label:d?.label??a,...d?.hideFromFunnel?{hideFromFunnel:!0}:{}})}let s=[];for(let[a,c]of n)if(c.type==="direct")s.push({from:a,to:c.to,type:"direct"});else{let d=Pt(c.condition,r);s.push({from:a,to:d,type:"conditional"})}return{flowId:e.id,title:e.title,nodes:i,edges:s}}function je(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let o=Object.keys(n.entries).map(r=>`"${r}"`).join(" | ");return t?`${o} \u2014 ${t}`:o}return t}function $e(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."," Do NOT invent missing intent."," Optionally include `context` \u2014 the situation or environment that led the user to start"," this flow (e.g. what page they are on, what they were doing, or what triggered the request)."," Only provide `context` when there is genuinely relevant situational information. 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.omitIntentPII&&t.push(" Do NOT include PII in `intent` or `context` \u2014 no names, emails, phones, addresses, IDs, ages, or birthdates.",' Summarize the goal abstractly (e.g. "user wants a quote", not "Jane Doe wants a quote").'),e.state){let n=[];for(let[o,r]of Object.entries(e.state)){let i=Ne(r);if(i){let s=r.description??"",a=Object.entries(i).map(([c,d])=>{let u=je(d);return u?`\`${o}.${c}\` (${u})`:`\`${o}.${c}\``}).join(", ");n.push(s?`\`${o}\` (${s}): ${a}`:`\`${o}\`: ${a}`)}else{let s=je(r);n.push(s?`\`${o}\` (${s})`:`\`${o}\``)}}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.","5. CORRECTION: If the user wants to CHANGE a previously-answered field",' (e.g. "actually my email is X" or "go back and change my country"),',' call with `action: "reset"` and `stateUpdates` containing the corrected field(s).'," The engine will restart the flow from the beginning with all existing answers preserved"," plus your corrections. Steps with filled answers will be auto-skipped."," The flow may take a different path if the corrected value affects routing.",' Do NOT use "reset" for the CURRENT question \u2014 use "continue" for that.',"6. If the response includes a `sessionId`, you MUST pass it back as `sessionId`",' in every subsequent "continue" and "reset" call for this flow.'),t.join(`
4
+ `)}function de(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function Le(e){let t=qe(e),n=de(t.waniwani)?t.waniwani:{};return e.meta({...t,waniwani:{...n,redacted:!0}})}function Nt(e){let n=qe(e).waniwani;return de(n)&&n.redacted===!0}function qe(e){let t=e.meta;if(typeof t!="function")return{};let n=t.call(e);return de(n)?n:{}}function Be(e){if(!e)return[];let t=[];for(let[n,o]of Object.entries(e))Nt(o)&&t.push(n);return t}var G="waniwani/redactedStateUpdateFields";function At(e){let t=e.omitIntentPII?" Do not include PII (names, emails, phones, addresses, IDs, ages, birthdates) \u2014 summarize abstractly.":"",o=e.state&&Object.keys(e.state).length>0?A.object(e.state).partial().passthrough():A.record(A.string(),A.unknown());return{action:A.enum(["start","continue","reset"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget), "reset" to restart from the beginning with a correction to a previously-collected field'),intent:A.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow. Do not invent missing intent.${t}`),context:A.string().optional().describe(`Optional when action is "start". Describe the situation or environment that led the user to start this flow \u2014 e.g. what page they are on, what they were doing, or what triggered the request. Do not invent missing context.${t}`),stateUpdates:o.optional().describe(`State field values to set before processing the next node. Pass the user's answer (keyed by the field name from the response) and any other values the user mentioned. For nested state fields, use dot-paths like "driver.name".`),sessionId:A.string().optional().describe('Session identifier. If the response includes a `sessionId`, pass it back on every subsequent "continue" and "reset" call for this flow.')}}function Wt(e){if(process.env.WANIWANI_API_KEY)return new B;throw new Error(`[waniwani] createFlow "${e}": no flow store configured. Pass { store } to .compile() \u2014 use MemoryKvStore from "@waniwani/sdk/mcp" for local development, or plug in a Redis/Upstash/Cloudflare KV adapter for production. Alternatively, set WANIWANI_API_KEY to use hosted flow state on app.waniwani.ai.`)}function He(e){let{config:t,nodes:n,edges:o}=e,r=At(t),i=Ke(t,n,o,e.nodeOptions),s=$e(t),a=`${t.description}
5
+ ${s}`,c=e.store??Wt(t.id),d=new Map;async function u(p,w,h,R){if(p.action==="start"){let T=typeof p.intent=="string"?p.intent.trim():void 0;if(!T)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.`}};if(p.intent=T,typeof p.context=="string"){let x=p.context.trim();p.context=x||void 0}let g=o.get(C);if(!g)return{content:{status:"error",error:"No start edge"}};let E=z(p.stateUpdates??{}),k=await M(g,E);return q(k,E,n,o,d,h,R,e.nodeOptions)}if(p.action==="continue"){if(!w)return{content:{status:"error",error:"No session ID available for continue action."}};let T;try{T=await c.get(w)}catch(x){let b=x instanceof Error?x.message:String(x);return{content:{status:"error",error:`Failed to load flow state (session "${w}"): ${b}`}}}if(!T)return{content:{status:"error",error:`Flow state not found for session "${w}". The flow may have expired.`}};let g=T.state,E=T.step;if(!E)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let k=N(g,z(p.stateUpdates??{}));if(T.widgetId){let x=o.get(E);if(!x)return{content:{status:"error",error:`No edge from step "${E}"`}};let b=await M(x,k);return q(b,k,n,o,d,h,R,e.nodeOptions)}return q(E,k,n,o,d,h,R,e.nodeOptions)}if(p.action==="reset"){if(!w)return{content:{status:"error",error:"No session ID available for reset action."}};let T;try{T=await c.get(w)}catch(b){let P=b instanceof Error?b.message:String(b);return{content:{status:"error",error:`Failed to load flow state (session "${w}"): ${P}`}}}if(!T)return{content:{status:"error",error:`Flow state not found for session "${w}". The flow may have completed or expired. Use action "start" to begin a new flow.`}};if(!p.stateUpdates||Object.keys(p.stateUpdates).length===0)return{content:{status:"error",error:'Missing "stateUpdates" for action "reset". Include the corrected field(s).'}};let g=o.get(C);if(!g)return{content:{status:"error",error:"No start edge"}};let E=T.state,k=N(E,z(p.stateUpdates)),x=await M(g,k);return q(x,k,n,o,d,h,R,e.nodeOptions)}return{content:{status:"error",error:`Unknown action: "${p.action}"`}}}let f=Be(t.state),l={title:t.title,description:a,inputSchema:r,annotations:t.annotations,...f.length>0&&{_meta:{[G]:f}}},v=(async(p,w)=>{let h=w,R=h._meta??{},T=F(R),g=T??p.sessionId;!g&&p.action==="start"&&(g=crypto.randomUUID(),R["waniwani/sessionId"]=g);let E=V(h),k=await u(p,g,R,E);if(g)if(k.flowTokenContent&&k.content.status!=="complete")try{await c.set(g,k.flowTokenContent)}catch(P){let W=P instanceof Error?P.message:String(P);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${g}"): ${W}`},null,2)}],_meta:R,isError:!0}}else k.content.status==="complete"&&await c.delete(g);let x=!T&&g?{...k.content,sessionId:g}:k.content,b=[{type:"text",text:JSON.stringify(x,null,2)}];return k.nodesVisited?.length&&(R[U]={flowId:t.id,nodesVisited:k.nodesVisited}),{content:b,_meta:R,...k.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:l,handler:v,async register(p){let w={...l,_meta:{...l._meta,_flowGraph:i}};p.registerTool(t.id,w,v)},graph:e.graph,flowGraph:i}}function Ve(e,t){let n=["flowchart TD"];n.push(` ${C}((Start))`);for(let[o]of e)n.push(` ${o}[${o}]`);n.push(` ${_}((End))`);for(let[o,r]of t)r.type==="direct"?n.push(` ${o} --> ${r.to}`):n.push(` ${o} -.-> ${o}_branch([?])`);return n.join(`
6
+ `)}var K=class{nodes=new Map;edges=new Map;nodeOptions=new Map;config;constructor(t){this.config=t}addNode(t,n,o){let r=typeof t=="string"?t:t.id,i=typeof t=="string"?n:t.run,s=typeof t=="string"?o?.label:t.label,a=typeof t=="string"?o?.hideFromFunnel:t.hideFromFunnel;if(r===C||r===_)throw new Error(`"${r}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(r))throw new Error(`Node "${r}" already exists`);return this.nodes.set(r,i),(s!==void 0||a!==void 0)&&this.nodeOptions.set(r,{label:s??r,hideFromFunnel:a}),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 Ve(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),o=new Map(this.edges);return He({config:this.config,nodes:n,edges:o,store:t?.store,graph:()=>Ve(n,o),nodeOptions:new Map(this.nodeOptions)})}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!==_&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,o]of this.edges){if(n!==C&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(o.type==="direct"&&o.to!==_&&!this.nodes.has(o.to))throw new Error(`Edge from "${n}" references non-existent node: "${o.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 ze(e){return new K(e)}function ue(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ye(e,t){let n=t?.stateStore,o=[],r=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...d)=>{o.push(d)}};await e.register(i);let s=o[0]?.[2];if(!s)throw new Error(`Flow "${e.name}" did not register a handler`);let a={_meta:{sessionId:r}};async function c(d){return{...d,decodedState:n?await n.get(r):null}}return{async start(d,u,f){let l=await s({action:"start",intent:d,...f?{context:f}:{},...u?{stateUpdates:u}:{}},a);return c(ue(l))},async continueWith(d){let u=await s({action:"continue",...d?{stateUpdates:d}:{}},a);return c(ue(u))},async resetWith(d){let u=await s({action:"reset",stateUpdates:d},a);return c(ue(u))},async lastState(){return n?n.get(r):null}}}var Z=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};var Mt="@waniwani/sdk";function Ge(e){let{apiUrl:t,apiKey:n}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function r(i,s,a){let c=o(),d=`${t.replace(/\/$/,"")}${s}`,u={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Mt},f={method:i,headers:u};a!==void 0&&(u["Content-Type"]="application/json",f.body=JSON.stringify(a));let l=await fetch(d,f);if(!l.ok){let p=await l.text().catch(()=>"");throw new Z(p||`KB API error: HTTP ${l.status}`,l.status)}return(await l.json()).data}return{async ingest(i){return r("POST","/api/mcp/kb/ingest",{files:i})},async search(i,s){return r("POST","/api/mcp/kb/search",{query:i,...s})},async sources(){return r("GET","/api/mcp/kb/sources")}}}var Ut="@waniwani/sdk";function X(e,t={}){let n=t.now??(()=>new Date),o=t.generateId??Ze,r=Kt(e),i=J(e.meta),s=J(e.metadata),a=jt(e,i),c=I(e.eventId)??o(),d=$t(e.timestamp,n),u=I(e.source)??$(i)??t.source??Ut,f=pe(e)?{...e}:void 0,l={...s};return Object.keys(i).length>0&&(l.meta=i),f&&(l.rawLegacy=f),{id:c,type:"mcp.event",name:r,source:u,timestamp:d,correlation:a,properties:Dt(e,r),metadata:l,rawLegacy:f}}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 Dt(e,t){if(!pe(e))return J(e.properties);let n=Ot(e,t),o=J(e.properties);return{...n,...o}}function Ot(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 Kt(e){return pe(e)?e.eventType:e.event}function jt(e,t){let n=I(e.requestId)??Ce(t),o=I(e.sessionId)??F(t),r=I(e.traceId)??_e(t),i=I(e.externalUserId)??Fe(t),s=I(e.correlationId)??Pe(t)??n,a={};return o&&(a.sessionId=o),r&&(a.traceId=r),n&&(a.requestId=n),s&&(a.correlationId=s),i&&(a.externalUserId=i),a}function $t(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 J(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function I(e){if(typeof e=="string"&&e.trim().length!==0)return e}function pe(e){return"eventType"in e}var Lt="/api/mcp/events/v2/batch";var Je="@waniwani/sdk",qt=new Set([401,403]),Bt=new Set([408,425,429,500,502,503,504]);function Xe(e){return new le(e)}var le=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=zt(t.apiUrl,t.endpointPath??Lt),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(o=>setTimeout(o,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,o=this.flush();if(!Number.isFinite(n)||n<=0)return await o,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let r=Symbol("shutdown-timeout");return await Promise.race([o.then(()=>"flushed"),this.sleep(n).then(()=>r)])===r?(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,o=t;for(;o.length>0&&!this.isStopped;){this.inFlightCount=o.length;let r=await this.sendBatchOnce(o);switch(this.inFlightCount=0,r.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(r.status,o.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",o.length,r.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",o.length,r.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(r.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",r.permanent.length),r.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",r.retryable.length);return}o=r.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":Je},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Yt(i)}}if(qt.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 o=await Vt(n);if(!o?.rejected||o.rejected.length===0)return{kind:"success"};let r=this.classifyRejectedEvents(t,o.rejected);return r.retryable.length===0&&r.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:r.retryable,permanent:r.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:Je,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let o=new Map(t.map(s=>[s.id,s])),r=[],i=[];for(let s of n){let a=o.get(s.eventId);if(a){if(Ht(s)){r.push(a);continue}i.push(a)}}return{retryable:r,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let o=this.buffer.length;this.buffer.splice(0,o),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+o)}};function Ht(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 zt(e,t){let n=e.endsWith("/")?e:`${e}/`,o=t.startsWith("/")?t.slice(1):t;return`${n}${o}`}function Yt(e){return e instanceof Error?e.message:String(e)}function Qe(e){let{apiUrl:t,apiKey:n,tracking:o}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?Xe({apiUrl:t,apiKey:n,endpointPath:o.endpointPath,flushIntervalMs:o.flushIntervalMs,maxBatchSize:o.maxBatchSize,maxBufferSize:o.maxBufferSize,maxRetries:o.maxRetries,retryBaseDelayMs:o.retryBaseDelayMs,retryMaxDelayMs:o.retryMaxDelayMs,shutdownTimeoutMs:o.shutdownTimeoutMs}):void 0,s={async identify(a,c,d){r();let u=X({event:"user.identified",externalUserId:a,properties:c,meta:d});return i?.enqueue(u),{eventId:u.id}},async track(a){r();let c=X(a);return i?.enqueue(c),{eventId:c.id}},async flush(){r(),await i?.flush()},async shutdown(a){return r(),await i?.shutdown({timeoutMs:a?.timeoutMs??o.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&Gt(s,o.shutdownTimeoutMs),s}function Gt(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 Q(e){let t=e,n=t?.apiUrl??"https://app.waniwani.ai",o=t?.apiKey??process.env.WANIWANI_API_KEY,r={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:o,tracking:r},s=Qe(i),a=Ge(i);return{...s,kb:a,_config:i}}function Zt(e){let t=e.event_type??"widget_click",o=t.startsWith("widget_")?t:`widget_${t}`,r={...e.metadata??{}};return e.event_name&&(r.event_name=e.event_name),{event:o,properties:r,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function Jt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function o(){return n||(n=Q(t)),n}return async function(i){let s;try{s=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(s.events)||s.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=o(),c=[];for(let d of s.events){let u=Zt(d),f=await a.track(u);c.push(f.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let c=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var fe=D("widget-token"),Xt=120*1e3,ee=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-Xt?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let o=Qt(this.config.apiUrl,"/api/mcp/widget-tokens");fe("minting token from",o);let r={};t&&(r.sessionId=t),n&&(r.traceId=n);try{let i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(r),signal:AbortSignal.timeout(5e3)});if(fe("mint response:",i.status),!i.ok)return null;let s=await i.json(),a=s.data&&typeof s.data=="object"?s.data:s,c=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(c)?null:(this.cached={token:a.token,expiresAt:c},a.token)}catch(i){return fe("mint failed:",i),null}}};function Qt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}async function et(e){if(typeof globalThis.crypto?.subtle?.digest=="function"){let n=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(e));return Array.from(new Uint8Array(n)).map(o=>o.toString(16).padStart(2,"0")).join("")}let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n)|0;return`simple-${Math.abs(t).toString(36)}`}function en(e){return et(JSON.stringify({nodes:e.nodes,edges:e.edges}))}async function tt(e){if(e.length===0)return null;let t=[...e].sort((r,i)=>r.flowId.localeCompare(i.flowId)),n=await et(JSON.stringify(t.map(r=>({flowId:r.flowId,nodes:r.nodes,edges:r.edges})))),o=await Promise.all(e.map(async r=>({flowId:r.flowId,title:r.title,configHash:await en(r),nodes:r.nodes,edges:r.edges})));return{compositeHash:n,flows:o}}var nt="waniwani/sessionId",te="waniwani/geoLocation",ne="waniwani/userLocation",tn="openai/userLocation",nn=[tn,te,ne];function ot(e,t){if(t.length===0)return e;let n;for(let o of nn){let r=e[o];if(!S(r))continue;let i;for(let s of t)s in r&&(i||(i={...r}),delete i[s]);i&&(n||(n={...e}),n[o]=i)}return n??e}function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function H(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function ge(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(o=>S(o)&&o.type==="text"&&typeof o.text=="string")?.text:void 0}function on(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function we(e,t,n,o,r,i){let s=on(e,n.toolType),a=n.stripLocationFields,c=a&&a.length>0,d=H(t),u=d&&c?ot(d,a):d,f=i?.input!==void 0&&n.redactInput?n.redactInput(i.input):i?.input,l=c&&S(i?.output)&&S(i.output._meta)?{...i.output,_meta:ot(i.output._meta,a)}:i?.output,p=(S(i?.output)&&S(i.output._meta)?i.output._meta:void 0)?.[U],w=p&&u?{...u,[U]:p}:p?{[U]:p}:u;return{event:"tool.called",properties:{name:e,type:s,...o??{},...f!==void 0&&{input:f},...l!==void 0&&{output:l}},meta:w,source:$(u),metadata:{...n.metadata??{},...r&&{clientInfo:r},...n.funnelSync&&{funnelSync:n.funnelSync}}}}async function me(e,t,n){try{await e.track(t)}catch(o){n?.(he(o))}}async function ye(e,t){try{await e.flush()}catch(n){t?.(he(n))}}async function it(e,t,n,o,r){if(!S(e))return;S(e._meta)||(e._meta={});let i=e._meta,s=S(i.waniwani)?i.waniwani:void 0,a={...s??{},endpoint:s?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let f=await t.getToken();f&&(a.token=f)}catch(f){r?.(he(f))}let c=F(i);c&&(a.sessionId||(a.sessionId=c));let d=ct(i);d!==void 0&&(a.geoLocation||(a.geoLocation=d));let u=$(H(o));u&&!a.source&&(a.source=u),i.waniwani=a}var rt=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function st(e,t){if(!t||!S(e))return;let n=!1;for(let r of rt)if(r in t){n=!0;break}if(!n)return;S(e._meta)||(e._meta={});let o=e._meta;for(let r of rt)r in t&&(r in o||(o[r]=t[r]))}function at(e,t){let n=H(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let o=e._meta,r=F(n);r&&!o[nt]&&(o[nt]=r);let i=ct(n);i&&(o[te]||(o[te]=i),o[ne]||(o[ne]=i))}function ct(e){if(!e)return;let t=e[te]??e[ne];if(S(t)||typeof t=="string")return t}function he(e){return e instanceof Error?e:new Error(String(e))}function dt(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function ut(e){if(typeof e.sessionId=="string"&&e.sessionId)return e.sessionId;if(dt(e.requestInfo)){let t=e.requestInfo.headers;if(dt(t)){let n={};for(let i of Object.keys(t))n[i.toLowerCase()]=t[i];let o=n["mcp-session-id"];if(typeof o=="string"&&o)return o;let r=n["x-waniwani-session-id"];if(typeof r=="string"&&r)return r}}}var lt=Symbol.for("waniwani.wrappedHandler"),oe=D("mcp"),ft="https://app.waniwani.ai",rn="REDACTED";function sn(e){if(!e)return;let t=e[G];if(!Array.isArray(t)||t.length===0)return;let n=new Set(t.filter(o=>typeof o=="string"));if(n.size!==0)return o=>{if(!S(o))return o;let r=o.stateUpdates;if(!S(r))return o;let i=!1,s={...r};for(let a of n)a in s&&(s[a]=rn,i=!0);return i?{...o,stateUpdates:s}:o}}function pt(e,t,n,o){let{server:r,tracker:i,opts:s,tokenCache:a,injectToken:c}=n,d=s.applyFieldRedactions===!0?sn(o):void 0,u=async(f,l)=>{let v=d?{...s,redactInput:d,funnelSync:n.funnelSync}:{...s,funnelSync:n.funnelSync},p=H(l)??{};if(!F(p)&&S(l)){let g=ut(l);g&&(p["waniwani/sessionId"]=g,l._meta=p)}let h=be(i,p,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});S(l)&&(l[se]=h);let R=performance.now(),T=r.server?.getClientVersion?.();try{let g=await t(f,l),E=Math.round(performance.now()-R);oe(`tool "${e}" handler returned in ${E}ms, running post-processing...`);let k=S(g)&&g.isError===!0;if(k){let x=ge(g);console.error(`[waniwani] Tool "${e}" returned error${x?`: ${x}`:""}`)}return await me(i,we(e,l,v,{durationMs:E,status:k?"error":"ok",...k&&{errorMessage:ge(g)??"Unknown tool error"}},T,{input:f,output:g}),s.onError),oe(`tool "${e}" tracking done`),s.flushAfterToolCall&&await ye(i,s.onError),at(g,l),st(g,o),c&&(await it(g,a,i._config.apiUrl??ft,l,s.onError),oe(`tool "${e}" widget config injected`)),oe(`tool "${e}" post-processing complete, returning result`),g}catch(g){let E=Math.round(performance.now()-R);throw await me(i,we(e,l,v,{durationMs:E,status:"error",errorMessage:g instanceof Error?g.message:String(g)},T,{input:f}),s.onError),s.flushAfterToolCall&&await ye(i,s.onError),g}};return u[lt]=!0,u}async function an(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let o=t??{},r=o.client??Q(),i=o.injectWidgetToken!==!1,s=r._config.apiKey?new ee({apiUrl:r._config.apiUrl??ft,apiKey:r._config.apiKey}):null,a={server:e,tracker:r,opts:o,tokenCache:s,injectToken:i,funnelSync:null},c=e.registerTool.bind(e);n.registerTool=((...u)=>{let[f,l,v]=u;if(typeof v!="function")return c(...u);let p=typeof f=="string"&&f.trim().length>0?f:"unknown",w=S(l)&&S(l._meta)?l._meta:void 0,h=pt(p,v,a,w);return c(f,l,h)});let d=e._registeredTools;if(S(d))for(let[u,f]of Object.entries(d)){if(!S(f))continue;let l=f.handler;if(typeof l!="function"||l[lt])continue;let v=S(f._meta)?f._meta:void 0;f.handler=pt(u,l,a,v)}if(r._config.apiKey){let u=e._registeredTools,f=[];if(u&&typeof u=="object"){for(let l of Object.values(u))if(l&&typeof l=="object"){let v=l._meta,p=v&&typeof v=="object"?v._flowGraph:void 0;p?.nodes?.length&&f.push(p)}}f.length>0&&(a.funnelSync=await tt(f))}return n}function Te(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function cn(){return Te()==="openai"}function dn(){return Te()==="mcp-apps"}var re="text/html+skybridge",ie="text/html;profile=mcp-app",gt=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function wt(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function mt(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 ke(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.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function yt(e){let{id:t,title:n,description:o,baseUrl:r,htmlPath:i,widgetDomain:s,prefersBorder:a=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[r],resource_domains:[r]};if(process.env.NODE_ENV==="development")try{let{hostname:h}=new URL(r);(h==="localhost"||h==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${h}:*`,`wss://${h}:*`],resource_domains:[...d.resource_domains||[],`http://${h}:*`]})}catch{}let u=`ui://widgets/apps-sdk/${t}.html`,f=`ui://widgets/ext-apps/${t}.html`,l=null,v=()=>(l||(l=gt(r,i)),l),p=o;async function w(h){let R=await v();h.registerResource(`${t}-openai-widget`,u,{title:n,description:p,mimeType:re,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":a}},async T=>({contents:[{uri:T.href,mimeType:re,text:R,_meta:wt({description:p,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]})),h.registerResource(`${t}-mcp-widget`,f,{title:n,description:p,mimeType:ie,_meta:{ui:{prefersBorder:a}}},async T=>({contents:[{uri:T.href,mimeType:ie,text:R,_meta:mt({description:p,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]}))}return{id:t,title:n,description:o,openaiUri:u,mcpUri:f,autoHeight:c,register:w}}function un(e,t){let{resource:n,description:o,inputSchema:r,annotations:i,autoInjectResultText:s=!0}=e,a=e.id??n?.id,c=e.title??n?.title;if(!a)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?ke({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:a,title:c,description:o,async register(u){u.registerTool(a,{title:c,description:o,inputSchema:r,annotations:i,...d&&{_meta:d}},(async(f,l)=>{let v=l,p=v._meta??{},w=V(v),h=await t(f,{extra:{_meta:p},waniwani:w});return n&&h.data?{content:[{type:"text",text:h.text}],structuredContent:h.data,_meta:{...d,...p,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:h.text}],...h.data?{structuredContent:h.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function pn(e,t){await Promise.all(t.map(n=>n.register(e)))}export{_ as END,Y as MemoryKvStore,C as START,K as StateGraph,O as WaniwaniKvStore,ze as createFlow,Ye as createFlowTestHarness,yt as createResource,un as createTool,Jt as createTrackingRoute,Te as detectPlatform,dn as isMCPApps,cn as isOpenAI,Le as redacted,pn as registerTools,an as withWaniwani};
7
7
  //# sourceMappingURL=index.js.map