@waniwani/sdk 0.4.3-beta.7 → 0.4.3-beta.8

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.
@@ -1,4 +1,4 @@
1
1
  "use client";
2
- "use client";import{q as g,r as u,s as b,t as h}from"./chunk-HVAT4SNH.js";import"./chunk-V3ZDECK5.js";import{memo as x,useMemo as f,useContext as N,useState as v,useEffect as y}from"react";import{jsx as r,jsxs as k}from"react/jsx-runtime";var w=g("block","before:content-[counter(line)]","before:inline-block","before:[counter-increment:line]","before:w-6","before:mr-4","before:text-[13px]","before:text-right","before:text-muted-foreground/50","before:font-mono","before:select-none"),C=x(({children:a,result:e,language:o,className:s,...m})=>{let i=f(()=>({backgroundColor:e.bg,color:e.fg}),[e.bg,e.fg]);return r("pre",{className:g(s,"p-4 text-sm dark:bg-(--shiki-dark-bg)!"),"data-language":o,"data-streamdown":"code-block-body",style:i,...m,children:r("code",{className:"[counter-increment:line_0] [counter-reset:line]",children:e.tokens.map((c,l)=>r("span",{className:w,children:c.map((t,n)=>r("span",{className:"dark:bg-(--shiki-dark-bg)! dark:text-(--shiki-dark)!",style:{color:t.color,backgroundColor:t.bgColor,...t.htmlStyle},...t.htmlAttrs,children:t.content},n))},l))})})},(a,e)=>a.result===e.result&&a.language===e.language&&a.className===e.className),S=({className:a,language:e,style:o,...s})=>r("div",{className:g("my-4 w-full overflow-hidden rounded-xl border border-border",a),"data-language":e,"data-streamdown":"code-block",style:{contentVisibility:"auto",containIntrinsicSize:"auto 200px",...o},...s}),j=({language:a,children:e})=>k("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-language":a,"data-streamdown":"code-block-header",children:[r("span",{className:"ml-1 font-mono lowercase",children:a}),r("div",{className:"flex items-center gap-2",children:e})]}),E=({code:a,language:e,className:o,children:s,...m})=>{let{shikiTheme:i}=N(h),c=b(),l=f(()=>({bg:"transparent",fg:"inherit",tokens:a.split(`
2
+ "use client";import{q as g,r as u,s as b,t as h}from"./chunk-5JBHQYU5.js";import"./chunk-V3ZDECK5.js";import{memo as x,useMemo as f,useContext as N,useState as v,useEffect as y}from"react";import{jsx as r,jsxs as k}from"react/jsx-runtime";var w=g("block","before:content-[counter(line)]","before:inline-block","before:[counter-increment:line]","before:w-6","before:mr-4","before:text-[13px]","before:text-right","before:text-muted-foreground/50","before:font-mono","before:select-none"),C=x(({children:a,result:e,language:o,className:s,...m})=>{let i=f(()=>({backgroundColor:e.bg,color:e.fg}),[e.bg,e.fg]);return r("pre",{className:g(s,"p-4 text-sm dark:bg-(--shiki-dark-bg)!"),"data-language":o,"data-streamdown":"code-block-body",style:i,...m,children:r("code",{className:"[counter-increment:line_0] [counter-reset:line]",children:e.tokens.map((c,l)=>r("span",{className:w,children:c.map((t,n)=>r("span",{className:"dark:bg-(--shiki-dark-bg)! dark:text-(--shiki-dark)!",style:{color:t.color,backgroundColor:t.bgColor,...t.htmlStyle},...t.htmlAttrs,children:t.content},n))},l))})})},(a,e)=>a.result===e.result&&a.language===e.language&&a.className===e.className),S=({className:a,language:e,style:o,...s})=>r("div",{className:g("my-4 w-full overflow-hidden rounded-xl border border-border",a),"data-language":e,"data-streamdown":"code-block",style:{contentVisibility:"auto",containIntrinsicSize:"auto 200px",...o},...s}),j=({language:a,children:e})=>k("div",{className:"flex items-center justify-between bg-muted/80 p-3 text-muted-foreground text-xs","data-language":a,"data-streamdown":"code-block-header",children:[r("span",{className:"ml-1 font-mono lowercase",children:a}),r("div",{className:"flex items-center gap-2",children:e})]}),E=({code:a,language:e,className:o,children:s,...m})=>{let{shikiTheme:i}=N(h),c=b(),l=f(()=>({bg:"transparent",fg:"inherit",tokens:a.split(`
3
3
  `).map(d=>[{content:d,color:"inherit",bgColor:"transparent",htmlStyle:{},offset:0}])}),[a]),[t,n]=v(l);return y(()=>{if(!c){n(l);return}let d=c.highlight({code:a,language:e,themes:i},p=>{n(p)});if(d){n(d);return}n(l)},[a,e,i,c,l]),r(u.Provider,{value:{code:a},children:k(S,{language:e,children:[r(j,{language:e,children:s}),r(C,{className:o,language:e,result:t,...m})]})})};export{E as CodeBlock};
4
- //# sourceMappingURL=code-block-37QAKDTI-M2QCKVTI.js.map
4
+ //# sourceMappingURL=code-block-37QAKDTI-IIYKFI5S.js.map
package/dist/mcp/index.js CHANGED
@@ -1,6 +1,6 @@
1
- function $(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function _e(){return $()==="openai"}function Me(){return $()==="mcp-apps"}var I="__start__",C="__end__",te=Symbol.for("waniwani.flow.interrupt"),ne=Symbol.for("waniwani.flow.widget");function re(e,t){let n=t?.context,r=[];for(let[o,a]of Object.entries(e))if(typeof a=="object"&&a!==null&&"question"in a){let i=a;r.push({question:i.question,field:o,suggestions:i.suggestions,context:i.context,validate:i.validate})}return{__type:te,questions:r,context:n}}function oe(e,t){return{__type:ne,tool:e,...t}}function ie(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===te}function se(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ne}import{z as A}from"zod";var Fe=["waniwani/sessionId","openai/sessionId","sessionId","conversationId","anthropic/sessionId"];function ae(e){if(e)for(let t of Fe){let n=e[t];if(typeof n=="string"&&n.length>0)return n}}function L(e){return e!=null&&e!==""}async function _(e,t){return e.type==="direct"?e.to:e.condition(t)}function ce(e,t,n,r){if(e.every(u=>L(r[u.field])))return null;let o=e.filter(u=>!L(r[u.field])),a=o.length===1,i=o[0];return{content:a&&i?{status:"interrupt",question:i.question,field:i.field,...i.suggestions?{suggestions:i.suggestions}:{},...i.context||t?{context:i.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...a&&i?{field:i.field}:{}}}}async function W(e,t,n,r,o,a){let i=e,s={...t},u=50,c=0;for(;c++<u;){if(i===C)return{content:{status:"complete"},flowTokenContent:{state:s}};let f=n.get(i);if(!f)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await f({state:s,meta:a,interrupt:re,showWidget:oe});if(ie(d)){for(let g of d.questions)g.validate&&o.set(`${i}:${g.field}`,g.validate);let p=ce(d.questions,d.context,i,s);if(p)return p;for(let g of d.questions){let x=o.get(`${i}:${g.field}`);if(x)try{let k=s[g.field],R=await x(k);R&&typeof R=="object"&&(s={...s,...R})}catch(k){let R=k instanceof Error?k.message:String(k);delete s[g.field];let T=d.questions.map(E=>E.field===g.field?{...E,context:E.context?`ERROR: ${R}
1
+ function L(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Pe(){return L()==="openai"}function We(){return L()==="mcp-apps"}var I="__start__",C="__end__",re=Symbol.for("waniwani.flow.interrupt"),oe=Symbol.for("waniwani.flow.widget");function ie(e,t){let n=t?.context,r=[];for(let[o,a]of Object.entries(e))if(typeof a=="object"&&a!==null&&"question"in a){let i=a;r.push({question:i.question,field:o,suggestions:i.suggestions,context:i.context,validate:i.validate})}return{__type:re,questions:r,context:n}}function se(e,t){return{__type:oe,tool:e,...t}}function ae(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===re}function ce(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===oe}import{z as U}from"zod";var Ae=["waniwani/sessionId","openai/sessionId","sessionId","conversationId","anthropic/sessionId"];function F(e){if(e)for(let t of Ae){let n=e[t];if(typeof n=="string"&&n.length>0)return n}}function K(e){return e!=null&&e!==""}async function _(e,t){return e.type==="direct"?e.to:e.condition(t)}function de(e,t,n,r){if(e.every(u=>K(r[u.field])))return null;let o=e.filter(u=>!K(r[u.field])),a=o.length===1,i=o[0];return{content:a&&i?{status:"interrupt",question:i.question,field:i.field,...i.suggestions?{suggestions:i.suggestions}:{},...i.context||t?{context:i.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...a&&i?{field:i.field}:{}}}}async function A(e,t,n,r,o,a){let i=e,s={...t},u=50,c=0;for(;c++<u;){if(i===C)return{content:{status:"complete"},flowTokenContent:{state:s}};let f=n.get(i);if(!f)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await f({state:s,meta:a,interrupt:ie,showWidget:se});if(ae(d)){for(let g of d.questions)g.validate&&o.set(`${i}:${g.field}`,g.validate);let p=de(d.questions,d.context,i,s);if(p)return p;for(let g of d.questions){let x=o.get(`${i}:${g.field}`);if(x)try{let k=s[g.field],R=await x(k);R&&typeof R=="object"&&(s={...s,...R})}catch(k){let R=k instanceof Error?k.message:String(k);delete s[g.field];let T=d.questions.map(E=>E.field===g.field?{...E,context:E.context?`ERROR: ${R}
2
2
 
3
- ${E.context}`:`ERROR: ${R}`}:E),b=ce(T,d.context,i,s);if(b)return b;break}}let w=r.get(i);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(w,s);continue}if(se(d)){let p=d.field;if(p&&L(s[p])){let w=r.get(i);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(w,s);continue}return{content:{status:"widget",tool:d.tool.id,data:d.data,description:d.description,...d.interactive===!1?{interactive:!1}:{}},flowTokenContent:{step:i,state:s,field:p,widgetId:d.tool.id}}}s={...s,...d};let y=r.get(i);if(!y)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(y,s)}catch(l){return{content:{status:"error",error:l instanceof Error?l.message:String(l)},flowTokenContent:{step:i,state:s}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Pe="@waniwani/sdk",We="https://app.waniwani.ai",F=class{baseUrl;apiKey;constructor(t){this.baseUrl=(t?.baseUrl??process.env.WANIWANI_BASE_URL??We).replace(/\/$/,""),this.apiKey=t?.apiKey??process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)return null;try{return await this.request("/api/mcp/redis/get",{key:t})??null}catch{return null}}async set(t,n){if(this.apiKey)try{await this.request("/api/mcp/redis/set",{key:t,value:n})}catch{}}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch{}}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":Pe},body:JSON.stringify(n)});if(!o.ok){let i=await o.text().catch(()=>"");throw new Error(i||`Flow state API error: HTTP ${o.status}`)}return(await o.json()).data}};function Ae(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 de(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. 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=Object.entries(e.state).map(([r,o])=>{let a=Ae(o);return a?`\`${r}\` (${a})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("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."," If the response includes `interactive: false`, the widget is display-only:"," call the display tool, show the widget, then immediately call THIS flow tool again with",' `action: "continue"`. In that case, do NOT wait for the user'," to click or use the widget, and do NOT ask them to interact with it unless the"," description explicitly says otherwise."," Otherwise, present the widget result to the user. When the user makes a choice or interacts"," with the widget, call THIS flow tool again with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"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 Ue={action:A.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:A.record(A.string(),A.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 ue(e){let{config:t,nodes:n,edges:r}=e,o=de(t),a=`${t.description}
5
- ${o}`,i=e.store??new F,s=new Map;async function u(c,f,l){if(c.action==="start"){let d=r.get(I);if(!d)return{content:{status:"error",error:"No start edge"}};let y={...c.stateUpdates??{}},p=await _(d,y);return W(p,y,n,r,s,l)}if(c.action==="continue"){if(!f)return{content:{status:"error",error:"No session ID available for continue action."}};let d=await i.get(f);if(!d)return{content:{status:"error",error:"Flow state not found. The flow may have expired."}};let y=d.state,p=d.step;if(!p)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let w={...y,...c.stateUpdates??{}};if(d.widgetId){let g=r.get(p);if(!g)return{content:{status:"error",error:`No edge from step "${p}"`}};let x=await _(g,w);return W(x,w,n,r,s,l)}return W(p,w,n,r,s,l)}return{content:{status:"error",error:`Unknown action: "${c.action}"`}}}return{id:t.id,title:t.title,description:a,async register(c){c.registerTool(t.id,{title:t.title,description:a,inputSchema:Ue,annotations:t.annotations},(async(f,l)=>{let y=l._meta??{},p=ae(y),w=await u(f,p,y);return w.flowTokenContent&&p&&await i.set(p,w.flowTokenContent),{content:[{type:"text",text:JSON.stringify(w.content,null,2)}],_meta:y,...w.content.status==="error"?{isError:!0}:{}}}))}}}var M=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===C)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}compile(t){return this.validate(),ue({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==C&&!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!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==C&&!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 le(e){return new M(e)}function pe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function fe(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,a={registerTool:(...c)=>{r.push(c)}};await e.register(a);let i=r[0]?.[2];if(!i)throw new Error(`Flow "${e.id}" did not register a handler`);let s={_meta:{sessionId:o}};async function u(c){return{...c,decodedState:n?await n.get(o):null}}return{async start(c){let f=await i({action:"start",...c?{stateUpdates:c}:{}},s);return u(pe(f))},async continueWith(c){let f=await i({action:"continue",...c?{stateUpdates:c}:{}},s);return u(pe(f))},async lastState(){return n?n.get(o):null}}}var U="text/html+skybridge",D="text/html;profile=mcp-app",ge=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function me(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 H(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}}}}}function we(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:u=!0}=e,c=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:g}=new URL(o);(g==="localhost"||g==="127.0.0.1")&&(c={...c,connect_domains:[...c.connect_domains||[],`ws://${g}:*`,`wss://${g}:*`],resource_domains:[...c.resource_domains||[],`http://${g}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,l=`ui://widgets/ext-apps/${t}.html`,d=null,y=()=>(d||(d=ge(o,a)),d),p=r;async function w(g){let x=await y();g.registerResource(`${t}-openai-widget`,f,{title:n,description:p,mimeType:U,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":s}},async k=>({contents:[{uri:k.href,mimeType:U,text:x,_meta:me({description:p,prefersBorder:s,widgetDomain:i,widgetCSP:c})}]})),g.registerResource(`${t}-mcp-widget`,l,{title:n,description:p,mimeType:D,_meta:{ui:{prefersBorder:s}}},async k=>({contents:[{uri:k.href,mimeType:D,text:x,_meta:he({description:p,prefersBorder:s,widgetCSP:c})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:l,autoHeight:u,register:w}}function De(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:i=!0}=e,s=e.id??n?.id,u=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!u)throw new Error("createTool: `title` is required when no resource is provided");let c=n?H({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:s,title:u,description:r,async register(f){f.registerTool(s,{title:u,description:r,inputSchema:o,annotations:a,...c&&{_meta:c}},(async(l,d)=>{let p=d._meta??{},w=await t(l,{extra:{_meta:p}});return n&&w.data?{content:[{type:"text",text:w.text}],structuredContent:w.data,_meta:{...c,...p,...i===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:w.text}],...w.data?{structuredContent:w.data}:{},...i===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Ne(e,t){await Promise.all(t.map(n=>n.register(e)))}var N=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Oe="@waniwani/sdk";function ye(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let u=r(),c=`${t.replace(/\/$/,"")}${i}`,f={Authorization:`Bearer ${u}`,"X-WaniWani-SDK":Oe},l={method:a,headers:f};s!==void 0&&(f["Content-Type"]="application/json",l.body=JSON.stringify(s));let d=await fetch(c,l);if(!d.ok){let p=await d.text().catch(()=>"");throw new N(p||`KB API error: HTTP ${d.status}`,d.status)}return(await d.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}import{AsyncLocalStorage as Be}from"async_hooks";var K=new Be;function Te(){return K.getStore()?.meta}var je="@waniwani/sdk";function B(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??ke,o=Le(e),a=O(e.meta),i=O(e.metadata),s=He(e,a),u=S(e.eventId)??r(),c=Ke(e.timestamp,n),f=S(e.source)??t.source??je,l=V(e)?{...e}:void 0,d={...i};return Object.keys(a).length>0&&(d.meta=a),l&&(d.rawLegacy=l),{id:u,type:"mcp.event",name:o,source:f,timestamp:c,correlation:s,properties:qe(e,o),metadata:d,rawLegacy:l}}function ke(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function qe(e,t){if(!V(e))return O(e.properties);let n=$e(e,t),r=O(e.properties);return{...n,...r}}function $e(e,t){switch(t){case"tool.called":{let n={};return S(e.toolName)&&(n.name=e.toolName),S(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),S(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return S(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),S(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Le(e){return V(e)?e.eventType:e.event}function He(e,t){let n=S(e.requestId)??P(t,["openai/requestId","requestId","mcp/requestId"]),r=S(e.sessionId)??P(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=S(e.traceId)??P(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=S(e.externalUserId)??P(t,["openai/userId","externalUserId","userId","actorId"]),i=S(e.correlationId)??P(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function Ke(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 P(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function O(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function S(e){if(typeof e=="string"&&e.trim().length!==0)return e}function V(e){return"eventType"in e}var Ve="/api/mcp/events/v2/batch";var Se="@waniwani/sdk",ze=new Set([401,403]),Ye=new Set([408,425,429,500,502,503,504]);function ve(e){return new z(e)}var z=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=Ze(t.baseUrl,t.endpointPath??Ve),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":Se},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ge(a)}}if(ze.has(n.status))return{kind:"auth",status:n.status};if(Ye.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Je(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:Se,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Xe(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}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 Xe(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 Je(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function Ze(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ge(e){return e instanceof Error?e.message:String(e)}function Re(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?ve({baseUrl: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,i={async identify(s,u,c){o();let f=xe(c),l=B({event:"user.identified",externalUserId:s,properties:u,meta:f});return a?.enqueue(l),{eventId:l.id}},async track(s){o();let u=xe(s.meta),c=B({...s,meta:u});return a?.enqueue(c),{eventId:c.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&Qe(i,r.shutdownTimeoutMs),i}function xe(e){let t=Te();if(!(!t&&!e))return t?e?{...t,...e}:t:e}function Qe(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 Ee(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=Re(o),i=ye(o);return{...a,kb:i,_config:o}}function et(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 tt(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=Ee(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.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(),u=[];for(let c of i.events){let f=et(c),l=await s.track(f);u.push(l.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:u.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let u=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:u}),{status:500,headers:{"Content-Type":"application/json"}})}}}var j=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?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=nt(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,u=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(u)?null:(this.cached={token:s.token,expiresAt:u},s.token)}catch{return null}}};function nt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Y="waniwani/userLocation";function v(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function q(e){if(!v(e))return;let t=e._meta;return v(t)?t:void 0}function X(e){if(!v(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>v(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function rt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function J(e,t,n,r,o){let a=rt(e,n.toolType),i=q(t);return{event:"tool.called",properties:{name:e,type:a,...r??{}},meta:i,metadata:{source:"withWaniwani",...n.metadata??{},...o&&{clientInfo:o}}}}async function Z(e,t,n){try{await e.track(t)}catch(r){n?.(Q(r))}}async function G(e,t){try{await e.flush()}catch(n){t?.(Q(n))}}async function Ie(e,t,n,r){if(!v(e))return;v(e._meta)||(e._meta={});let o=e._meta,a={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(a.token=i)}catch(i){r?.(Q(i))}o.waniwani=a}function Ce(e,t){let n=q(t);if(!n)return;let r=n[Y];if(!r||!v(e))return;v(e._meta)||(e._meta={});let o=e._meta;o[Y]||(o[Y]=r)}function Q(e){return e instanceof Error?e:new Error(String(e))}var be="https://app.waniwani.ai";function ot(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client,o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let u=r._config.apiKey;return u?(a=new j({baseUrl:r._config.baseUrl??be,apiKey:u}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...u)=>{let[c,f,l]=u,d=typeof c=="string"&&c.trim().length>0?c:"unknown";if(typeof l!="function")return s(...u);let y=l;return s(c,f,async(w,g)=>{let x=q(g)??{};return K.run({meta:x},async()=>{let k=performance.now(),R=e.server?.getClientVersion?.();try{let T=await y(w,g),b=Math.round(performance.now()-k),E=v(T)&&T.isError===!0;if(E){let ee=X(T);console.error(`[waniwani] Tool "${d}" returned error${ee?`: ${ee}`:""}`)}return await Z(r,J(d,g,t,{durationMs:b,status:E?"error":"ok",...E&&{errorMessage:X(T)??"Unknown tool error"}},R),t.onError),t.flushAfterToolCall&&await G(r,t.onError),o&&await Ie(T,i(),r._config.baseUrl??be,t.onError),Ce(T,g),T}catch(T){let b=Math.round(performance.now()-k);throw await Z(r,J(d,g,t,{durationMs:b,status:"error",errorMessage:T instanceof Error?T.message:String(T)},R),t.onError),t.flushAfterToolCall&&await G(r,t.onError),T}})})}),n}export{C as END,I as START,M as StateGraph,le as createFlow,fe as createFlowTestHarness,we as createResource,De as createTool,tt as createTrackingRoute,$ as detectPlatform,Me as isMCPApps,_e as isOpenAI,Ne as registerTools,ot as withWaniwani};
3
+ ${E.context}`:`ERROR: ${R}`}:E),b=de(T,d.context,i,s);if(b)return b;break}}let w=r.get(i);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(w,s);continue}if(ce(d)){let p=d.field;if(p&&K(s[p])){let w=r.get(i);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(w,s);continue}return{content:{status:"widget",tool:d.tool.id,data:d.data,description:d.description,...d.interactive===!1?{interactive:!1}:{}},flowTokenContent:{step:i,state:s,field:p,widgetId:d.tool.id}}}s={...s,...d};let y=r.get(i);if(!y)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await _(y,s)}catch(l){return{content:{status:"error",error:l instanceof Error?l.message:String(l)},flowTokenContent:{step:i,state:s}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Ue="@waniwani/sdk",De="https://app.waniwani.ai",P=class{baseUrl;apiKey;constructor(t){this.baseUrl=(t?.baseUrl??process.env.WANIWANI_BASE_URL??De).replace(/\/$/,""),this.apiKey=t?.apiKey??process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)return null;try{return await this.request("/api/mcp/redis/get",{key:t})??null}catch{return null}}async set(t,n){if(this.apiKey)try{await this.request("/api/mcp/redis/set",{key:t,value:n})}catch{}}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch{}}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":Ue},body:JSON.stringify(n)});if(!o.ok){let i=await o.text().catch(()=>"");throw new Error(i||`Flow state API error: HTTP ${o.status}`)}return(await o.json()).data}};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. 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=Object.entries(e.state).map(([r,o])=>{let a=Ne(o);return a?`\`${r}\` (${a})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("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."," If the response includes `interactive: false`, the widget is display-only:"," call the display tool, show the widget, then immediately call THIS flow tool again with",' `action: "continue"`. In that case, do NOT wait for the user'," to click or use the widget, and do NOT ask them to interact with it unless the"," description explicitly says otherwise."," Otherwise, present the widget result to the user. When the user makes a choice or interacts"," with the widget, call THIS flow tool again with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"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 Oe={action:U.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:U.record(U.string(),U.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 le(e){let{config:t,nodes:n,edges:r}=e,o=ue(t),a=`${t.description}
5
+ ${o}`,i=e.store??new P,s=new Map;async function u(c,f,l){if(c.action==="start"){let d=r.get(I);if(!d)return{content:{status:"error",error:"No start edge"}};let y={...c.stateUpdates??{}},p=await _(d,y);return A(p,y,n,r,s,l)}if(c.action==="continue"){if(!f)return{content:{status:"error",error:"No session ID available for continue action."}};let d=await i.get(f);if(!d)return{content:{status:"error",error:"Flow state not found. The flow may have expired."}};let y=d.state,p=d.step;if(!p)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let w={...y,...c.stateUpdates??{}};if(d.widgetId){let g=r.get(p);if(!g)return{content:{status:"error",error:`No edge from step "${p}"`}};let x=await _(g,w);return A(x,w,n,r,s,l)}return A(p,w,n,r,s,l)}return{content:{status:"error",error:`Unknown action: "${c.action}"`}}}return{id:t.id,title:t.title,description:a,async register(c){c.registerTool(t.id,{title:t.title,description:a,inputSchema:Oe,annotations:t.annotations},(async(f,l)=>{let y=l._meta??{},p=F(y),w=await u(f,p,y);return w.flowTokenContent&&p&&await i.set(p,w.flowTokenContent),{content:[{type:"text",text:JSON.stringify(w.content,null,2)}],_meta:y,...w.content.status==="error"?{isError:!0}:{}}}))}}}var M=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===C)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}compile(t){return this.validate(),le({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==C&&!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!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==C&&!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 pe(e){return new M(e)}function fe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function ge(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,a={registerTool:(...c)=>{r.push(c)}};await e.register(a);let i=r[0]?.[2];if(!i)throw new Error(`Flow "${e.id}" did not register a handler`);let s={_meta:{sessionId:o}};async function u(c){return{...c,decodedState:n?await n.get(o):null}}return{async start(c){let f=await i({action:"start",...c?{stateUpdates:c}:{}},s);return u(fe(f))},async continueWith(c){let f=await i({action:"continue",...c?{stateUpdates:c}:{}},s);return u(fe(f))},async lastState(){return n?n.get(o):null}}}var D="text/html+skybridge",N="text/html;profile=mcp-app",me=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function he(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function we(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 H(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}}}}}function ye(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:u=!0}=e,c=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:g}=new URL(o);(g==="localhost"||g==="127.0.0.1")&&(c={...c,connect_domains:[...c.connect_domains||[],`ws://${g}:*`,`wss://${g}:*`],resource_domains:[...c.resource_domains||[],`http://${g}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,l=`ui://widgets/ext-apps/${t}.html`,d=null,y=()=>(d||(d=me(o,a)),d),p=r;async function w(g){let x=await y();g.registerResource(`${t}-openai-widget`,f,{title:n,description:p,mimeType:D,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":s}},async k=>({contents:[{uri:k.href,mimeType:D,text:x,_meta:he({description:p,prefersBorder:s,widgetDomain:i,widgetCSP:c})}]})),g.registerResource(`${t}-mcp-widget`,l,{title:n,description:p,mimeType:N,_meta:{ui:{prefersBorder:s}}},async k=>({contents:[{uri:k.href,mimeType:N,text:x,_meta:we({description:p,prefersBorder:s,widgetCSP:c})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:l,autoHeight:u,register:w}}function Be(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:i=!0}=e,s=e.id??n?.id,u=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!u)throw new Error("createTool: `title` is required when no resource is provided");let c=n?H({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:s,title:u,description:r,async register(f){f.registerTool(s,{title:u,description:r,inputSchema:o,annotations:a,...c&&{_meta:c}},(async(l,d)=>{let p=d._meta??{},w=await t(l,{extra:{_meta:p}});return n&&w.data?{content:[{type:"text",text:w.text}],structuredContent:w.data,_meta:{...c,...p,...i===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:w.text}],...w.data?{structuredContent:w.data}:{},...i===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function je(e,t){await Promise.all(t.map(n=>n.register(e)))}var O=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var qe="@waniwani/sdk";function Te(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let u=r(),c=`${t.replace(/\/$/,"")}${i}`,f={Authorization:`Bearer ${u}`,"X-WaniWani-SDK":qe},l={method:a,headers:f};s!==void 0&&(f["Content-Type"]="application/json",l.body=JSON.stringify(s));let d=await fetch(c,l);if(!d.ok){let p=await d.text().catch(()=>"");throw new O(p||`KB API error: HTTP ${d.status}`,d.status)}return(await d.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}import{AsyncLocalStorage as $e}from"async_hooks";var V=new $e;function ke(){return V.getStore()?.meta}var Le="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Se,o=Ve(e),a=B(e.meta),i=B(e.metadata),s=ze(e,a),u=S(e.eventId)??r(),c=Ye(e.timestamp,n),f=S(e.source)??t.source??Le,l=z(e)?{...e}:void 0,d={...i};return Object.keys(a).length>0&&(d.meta=a),l&&(d.rawLegacy=l),{id:u,type:"mcp.event",name:o,source:f,timestamp:c,correlation:s,properties:Ke(e,o),metadata:d,rawLegacy:l}}function Se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ke(e,t){if(!z(e))return B(e.properties);let n=He(e,t),r=B(e.properties);return{...n,...r}}function He(e,t){switch(t){case"tool.called":{let n={};return S(e.toolName)&&(n.name=e.toolName),S(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),S(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return S(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),S(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Ve(e){return z(e)?e.eventType:e.event}function ze(e,t){let n=S(e.requestId)??W(t,["openai/requestId","requestId","mcp/requestId"]),r=S(e.sessionId)??W(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=S(e.traceId)??W(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=S(e.externalUserId)??W(t,["openai/userId","externalUserId","userId","actorId"]),i=S(e.correlationId)??W(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function Ye(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 W(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function B(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function S(e){if(typeof e=="string"&&e.trim().length!==0)return e}function z(e){return"eventType"in e}var Xe="/api/mcp/events/v2/batch";var ve="@waniwani/sdk",Je=new Set([401,403]),Ge=new Set([408,425,429,500,502,503,504]);function xe(e){return new Y(e)}var Y=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=et(t.baseUrl,t.endpointPath??Xe),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":ve},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:tt(a)}}if(Je.has(n.status))return{kind:"auth",status:n.status};if(Ge.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Qe(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:ve,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Ze(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}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 Ze(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 Qe(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function et(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function tt(e){return e instanceof Error?e.message:String(e)}function Ee(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?xe({baseUrl: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,i={async identify(s,u,c){o();let f=Re(c),l=j({event:"user.identified",externalUserId:s,properties:u,meta:f});return a?.enqueue(l),{eventId:l.id}},async track(s){o();let u=Re(s.meta),c=j({...s,meta:u});return a?.enqueue(c),{eventId:c.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&nt(i,r.shutdownTimeoutMs),i}function Re(e){let t=ke();if(!(!t&&!e))return t?e?{...t,...e}:t:e}function nt(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 Ie(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=Ee(o),i=Te(o);return{...a,kb:i,_config:o}}function rt(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 ot(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=Ie(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.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(),u=[];for(let c of i.events){let f=rt(c),l=await s.track(f);u.push(l.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:u.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let u=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:u}),{status:500,headers:{"Content-Type":"application/json"}})}}}var q=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?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=it(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,u=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(u)?null:(this.cached={token:s.token,expiresAt:u},s.token)}catch{return null}}};function it(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Ce="waniwani/sessionId",X="waniwani/geoLocation",J="waniwani/userLocation";function v(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function $(e){if(!v(e))return;let t=e._meta;return v(t)?t:void 0}function G(e){if(!v(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>v(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function st(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Z(e,t,n,r,o){let a=st(e,n.toolType),i=$(t);return{event:"tool.called",properties:{name:e,type:a,...r??{}},meta:i,metadata:{source:"withWaniwani",...n.metadata??{},...o&&{clientInfo:o}}}}async function Q(e,t,n){try{await e.track(t)}catch(r){n?.(te(r))}}async function ee(e,t){try{await e.flush()}catch(n){t?.(te(n))}}async function be(e,t,n,r){if(!v(e))return;v(e._meta)||(e._meta={});let o=e._meta,a=v(o.waniwani)?o.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let c=await t.getToken();c&&(i.token=c)}catch(c){r?.(te(c))}let s=F(o);s&&(i.sessionId||(i.sessionId=s));let u=Me(o);u!==void 0&&(i.geoLocation||(i.geoLocation=u)),o.waniwani=i}function _e(e,t){let n=$(t);if(!n||!v(e))return;v(e._meta)||(e._meta={});let r=e._meta,o=F(n);o&&!r[Ce]&&(r[Ce]=o);let a=Me(n);a&&(r[X]||(r[X]=a),r[J]||(r[J]=a))}function Me(e){if(!e)return;let t=e[X]??e[J];if(v(t)||typeof t=="string")return t}function te(e){return e instanceof Error?e:new Error(String(e))}var Fe="https://app.waniwani.ai";function at(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client,o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let u=r._config.apiKey;return u?(a=new q({baseUrl:r._config.baseUrl??Fe,apiKey:u}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...u)=>{let[c,f,l]=u,d=typeof c=="string"&&c.trim().length>0?c:"unknown";if(typeof l!="function")return s(...u);let y=l;return s(c,f,async(w,g)=>{let x=$(g)??{};return V.run({meta:x},async()=>{let k=performance.now(),R=e.server?.getClientVersion?.();try{let T=await y(w,g),b=Math.round(performance.now()-k),E=v(T)&&T.isError===!0;if(E){let ne=G(T);console.error(`[waniwani] Tool "${d}" returned error${ne?`: ${ne}`:""}`)}return await Q(r,Z(d,g,t,{durationMs:b,status:E?"error":"ok",...E&&{errorMessage:G(T)??"Unknown tool error"}},R),t.onError),t.flushAfterToolCall&&await ee(r,t.onError),_e(T,g),o&&await be(T,i(),r._config.baseUrl??Fe,t.onError),T}catch(T){let b=Math.round(performance.now()-k);throw await Q(r,Z(d,g,t,{durationMs:b,status:"error",errorMessage:T instanceof Error?T.message:String(T)},R),t.onError),t.flushAfterToolCall&&await ee(r,t.onError),T}})})}),n}export{C as END,I as START,M as StateGraph,pe as createFlow,ge as createFlowTestHarness,ye as createResource,Be as createTool,ot as createTrackingRoute,L as detectPlatform,We as isMCPApps,Pe as isOpenAI,je as registerTools,at as withWaniwani};
6
6
  //# sourceMappingURL=index.js.map