@waniwani/sdk 0.4.0 → 0.4.3

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,6 +1,6 @@
1
- function q(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Ce(){return q()==="openai"}function Fe(){return q()==="mcp-apps"}var x="__start__",E="__end__",J=Symbol.for("waniwani.flow.interrupt"),X=Symbol.for("waniwani.flow.widget");function Z(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:J,questions:r,context:n}}function G(e,t){return{__type:X,tool:e,...t}}function Q(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===J}function ee(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===X}import{z as M}from"zod";function $(e){return e!=null&&e!==""}async function R(e,t){return e.type==="direct"?e.to:e.condition(t)}function te(e,t,n,r){if(e.every(c=>$(r[c.field])))return null;let o=e.filter(c=>!$(r[c.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},c=50,l=0;for(;l++<c;){if(i===E)return{content:{status:"complete"},flowTokenContent:{state:s}};let w=n.get(i);if(!w)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await w({state:s,meta:a,interrupt:Z,showWidget:G});if(Q(d)){for(let p of d.questions)p.validate&&o.set(`${i}:${p.field}`,p.validate);let u=te(d.questions,d.context,i,s);if(u)return u;for(let p of d.questions){let S=o.get(`${i}:${p.field}`);if(S)try{let y=s[p.field],k=await S(y);k&&typeof k=="object"&&(s={...s,...k})}catch(y){let k=y instanceof Error?y.message:String(y);delete s[p.field];let Ie=d.questions.map(b=>b.field===p.field?{...b,context:b.context?`ERROR: ${k}
1
+ function q(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Ce(){return q()==="openai"}function Fe(){return q()==="mcp-apps"}var x="__start__",E="__end__",J=Symbol.for("waniwani.flow.interrupt"),X=Symbol.for("waniwani.flow.widget");function Z(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:J,questions:r,context:n}}function G(e,t){return{__type:X,tool:e,...t}}function Q(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===J}function ee(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===X}import{z as M}from"zod";var be=["waniwani/sessionId","openai/sessionId","sessionId","conversationId","anthropic/sessionId"];function te(e){if(e)for(let t of be){let n=e[t];if(typeof n=="string"&&n.length>0)return n}}function $(e){return e!=null&&e!==""}async function R(e,t){return e.type==="direct"?e.to:e.condition(t)}function ne(e,t,n,r){if(e.every(c=>$(r[c.field])))return null;let o=e.filter(c=>!$(r[c.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},c=50,l=0;for(;l++<c;){if(i===E)return{content:{status:"complete"},flowTokenContent:{state:s}};let w=n.get(i);if(!w)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await w({state:s,meta:a,interrupt:Z,showWidget:G});if(Q(d)){for(let p of d.questions)p.validate&&o.set(`${i}:${p.field}`,p.validate);let u=ne(d.questions,d.context,i,s);if(u)return u;for(let p of d.questions){let S=o.get(`${i}:${p.field}`);if(S)try{let y=s[p.field],k=await S(y);k&&typeof k=="object"&&(s={...s,...k})}catch(y){let k=y instanceof Error?y.message:String(y);delete s[p.field];let Ie=d.questions.map(b=>b.field===p.field?{...b,context:b.context?`ERROR: ${k}
2
2
 
3
- ${b.context}`:`ERROR: ${k}`}:b),Y=te(Ie,d.context,i,s);if(Y)return Y;break}}let g=r.get(i);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(g,s);continue}if(ee(d)){let u=d.field;if(u&&$(s[u])){let g=r.get(i);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(g,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:u,widgetId:d.tool.id}}}s={...s,...d};let T=r.get(i);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(T,s)}catch(f){return{content:{status:"error",error:f instanceof Error?f.message:String(f)},flowTokenContent:{step:i,state:s}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}import{randomBytes as be}from"crypto";var _e="@waniwani/sdk",Me="https://app.waniwani.ai";function ne(){return be(4).toString("hex")}var _=class{baseUrl;apiKey;constructor(t){this.baseUrl=(t?.baseUrl??process.env.WANIWANI_BASE_URL??Me).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":_e},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 Pe(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 re(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=Pe(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"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `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"` and the same `flowToken`. 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"`, `flowToken` = the `flowToken` from the response,'," `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. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. 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
- `)}import{deflateSync as We,inflateSync as Ae}from"zlib";function oe(e){let t=JSON.stringify(e);return We(t).toString("base64")}function I(e){try{let t=Buffer.from(e,"base64"),n=Ae(t).toString("utf-8");return JSON.parse(n)}catch{return null}}var De=["openai/sessionId","sessionId","conversationId","anthropic/sessionId"];function ie(e){if(e)for(let t of De){let n=e[t];if(typeof n=="string"&&n.length>0)return n}}async function se(e,t,n){if(n){let r=await t.get(n);if(r)return r}if(e.flowToken){let r=await t.get(e.flowToken);if(r)return r;let o=I(e.flowToken);if(o)return o}return null}var Ue={action:M.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:M.record(M.string(),M.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."),flowToken:M.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function ae(e){let{config:t,nodes:n,edges:r}=e,o=re(t),a=`${t.description}
5
- ${o}`,i=e.store??new _,s=new Map;async function c(l,w,f){if(l.action==="start"){let d=r.get(x);if(!d)return{content:{status:"error",error:"No start edge"}};let T={...l.stateUpdates??{}},u=await R(d,T);return W(u,T,n,r,s,f)}if(l.action==="continue"){let d=await se(l,i,w);if(!d)return{content:{status:"error",error:"Flow state not found for continue action. Pass back the flowToken from the previous response exactly as received."}};let T=d.state,u=d.step;if(!u)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let g={...T,...l.stateUpdates??{}};if(d.widgetId){let p=r.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let S=await R(p,g);return W(S,g,n,r,s,f)}return W(u,g,n,r,s,f)}return{content:{status:"error",error:`Unknown action: "${l.action}"`}}}return{id:t.id,title:t.title,description:a,async register(l){l.registerTool(t.id,{title:t.title,description:a,inputSchema:Ue,annotations:t.annotations},(async(w,f)=>{let T=f._meta??{},u=ie(T),g=await c(w,u,T),p;if(g.flowTokenContent)if(u)await i.set(u,g.flowTokenContent),p=u;else{let k=ne();await i.set(k,g.flowTokenContent),p=k}let S={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(S,null,2)}],_meta:T,...g.content.status==="error"?{isError:!0}:{}}}))}}}var C=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===x||t===E)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(),ae({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(x))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(x);if(t?.type==="direct"&&t.to!==E&&!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!==x&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==E&&!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 ce(e){return new C(e)}function de(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function le(e,t){if(!e)return null;if(t){let n=await t.get(e);if(n)return n}return I(e)}async function ue(e,t){let n=t?.stateStore,r=[],o={registerTool:(...c)=>{r.push(c)}};await e.register(o);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.id}" did not register a handler`);let i;async function s(c){return i=c.flowToken,{...c,decodedState:await le(i,n)}}return{async start(c){let l=await a({action:"start",...c?{stateUpdates:c}:{}},{});return s(de(l))},async continueWith(c){if(!i)throw new Error("No flowToken \u2014 call start() first");let l=await a({action:"continue",flowToken:i,...c?{stateUpdates:c}:{}},{});return s(de(l))},async lastState(){return le(i,n)}}}var A="text/html+skybridge",D="text/html;profile=mcp-app",pe=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function fe(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function ge(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 L(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 me(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:c=!0}=e,l=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:p}=new URL(o);(p==="localhost"||p==="127.0.0.1")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${p}:*`,`wss://${p}:*`],resource_domains:[...l.resource_domains||[],`http://${p}:*`]})}catch{}let w=`ui://widgets/apps-sdk/${t}.html`,f=`ui://widgets/ext-apps/${t}.html`,d=null,T=()=>(d||(d=pe(o,a)),d),u=r;async function g(p){let S=await T();p.registerResource(`${t}-openai-widget`,w,{title:n,description:u,mimeType:A,_meta:{"openai/widgetDescription":u,"openai/widgetPrefersBorder":s}},async y=>({contents:[{uri:y.href,mimeType:A,text:S,_meta:fe({description:u,prefersBorder:s,widgetDomain:i,widgetCSP:l})}]})),p.registerResource(`${t}-mcp-widget`,f,{title:n,description:u,mimeType:D,_meta:{ui:{prefersBorder:s}}},async y=>({contents:[{uri:y.href,mimeType:D,text:S,_meta:ge({description:u,prefersBorder:s,widgetCSP:l})}]}))}return{id:t,title:n,description:r,openaiUri:w,mcpUri:f,autoHeight:c,register:g}}function Ne(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:i=!0}=e,s=e.id??n?.id,c=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let l=n?L({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:s,title:c,description:r,async register(w){w.registerTool(s,{title:c,description:r,inputSchema:o,annotations:a,...l&&{_meta:l}},(async(f,d)=>{let u=d._meta??{},g=await t(f,{extra:{_meta:u}});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...l,...u,...i===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...i===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Oe(e,t){await Promise.all(t.map(n=>n.register(e)))}var U=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Be="@waniwani/sdk";function he(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 c=r(),l=`${t.replace(/\/$/,"")}${i}`,w={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Be},f={method:a,headers:w};s!==void 0&&(w["Content-Type"]="application/json",f.body=JSON.stringify(s));let d=await fetch(l,f);if(!d.ok){let u=await d.text().catch(()=>"");throw new U(u||`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")}}}var je="@waniwani/sdk";function O(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??we,o=Le(e),a=N(e.meta),i=N(e.metadata),s=Ke(e,a),c=v(e.eventId)??r(),l=He(e.timestamp,n),w=v(e.source)??t.source??je,f=K(e)?{...e}:void 0,d={...i};return Object.keys(a).length>0&&(d.meta=a),f&&(d.rawLegacy=f),{id:c,type:"mcp.event",name:o,source:w,timestamp:l,correlation:s,properties:qe(e,o),metadata:d,rawLegacy:f}}function we(){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(!K(e))return N(e.properties);let n=$e(e,t),r=N(e.properties);return{...n,...r}}function $e(e,t){switch(t){case"tool.called":{let n={};return v(e.toolName)&&(n.name=e.toolName),v(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),v(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return v(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),v(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Le(e){return K(e)?e.eventType:e.event}function Ke(e,t){let n=v(e.requestId)??P(t,["openai/requestId","requestId","mcp/requestId"]),r=v(e.sessionId)??P(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=v(e.traceId)??P(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=v(e.externalUserId)??P(t,["openai/userId","externalUserId","userId","actorId"]),i=v(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 He(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 N(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function K(e){return"eventType"in e}var Ve="/api/mcp/events/v2/batch";var ye="@waniwani/sdk",ze=new Set([401,403]),Ye=new Set([408,425,429,500,502,503,504]);function Te(e){return new H(e)}var H=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":ye},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 Xe(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ye,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Je(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 Je(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 Xe(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 ke(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?Te({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,c){o();let l=O({event:"user.identified",externalUserId:s,properties:c});return a?.enqueue(l),{eventId:l.id}},async track(s){o();let c=O(s);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 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 B(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=ke(o),i=he(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=B(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(),c=[];for(let l of i.events){let w=et(l),f=await s.track(w);c.push(f.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let c=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}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,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch{return null}}};function nt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Se="https://app.waniwani.ai";function rt(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??B(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let c=r._config.apiKey;return c?(a=new j({baseUrl:r._config.baseUrl??Se,apiKey:c}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[l,w,f]=c,d=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof f!="function")return s(...c);let T=f;return s(l,w,async(g,p)=>{let S=performance.now();try{let y=await T(g,p),k=Math.round(performance.now()-S);return await xe(r,ve(d,p,t,{durationMs:k,status:"ok"}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),o&&await ot(y,i(),r._config.baseUrl??Se,t.onError),st(y,p),y}catch(y){let k=Math.round(performance.now()-S);throw await xe(r,ve(d,p,t,{durationMs:k,status:"error",errorMessage:y instanceof Error?y.message:String(y)}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),y}})}),n}async function ot(e,t,n,r){if(!F(e))return;F(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?.(z(i))}o.waniwani=a}function ve(e,t,n,r){let o=it(e,n.toolType),a=Re(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function it(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Re(e){if(!F(e))return;let t=e._meta;if(F(t))return t}function F(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function xe(e,t,n){try{await e.track(t)}catch(r){n?.(z(r))}}async function Ee(e,t){try{await e.flush()}catch(n){t?.(z(n))}}function z(e){return e instanceof Error?e:new Error(String(e))}var V="waniwani/userLocation";function st(e,t){let n=Re(t);if(!n)return;let r=n[V];if(!r||!F(e))return;F(e._meta)||(e._meta={});let o=e._meta;o[V]||(o[V]=r)}export{E as END,x as START,C as StateGraph,ce as createFlow,ue as createFlowTestHarness,me as createResource,Ne as createTool,tt as createTrackingRoute,I as decodeFlowToken,q as detectPlatform,oe as encodeFlowToken,Fe as isMCPApps,Ce as isOpenAI,Oe as registerTools,rt as withWaniwani};
3
+ ${b.context}`:`ERROR: ${k}`}:b),Y=ne(Ie,d.context,i,s);if(Y)return Y;break}}let g=r.get(i);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(g,s);continue}if(ee(d)){let u=d.field;if(u&&$(s[u])){let g=r.get(i);if(!g)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(g,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:u,widgetId:d.tool.id}}}s={...s,...d};let T=r.get(i);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await R(T,s)}catch(f){return{content:{status:"error",error:f instanceof Error?f.message:String(f)},flowTokenContent:{step:i,state:s}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}import{randomBytes as _e}from"crypto";var Me="@waniwani/sdk",Pe="https://app.waniwani.ai";function re(){return _e(4).toString("hex")}var _=class{baseUrl;apiKey;constructor(t){this.baseUrl=(t?.baseUrl??process.env.WANIWANI_BASE_URL??Pe).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":Me},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 We(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 oe(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=We(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"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `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"` and the same `flowToken`. 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"`, `flowToken` = the `flowToken` from the response,'," `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. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. 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
+ `)}import{deflateSync as Ae,inflateSync as De}from"zlib";function ie(e){let t=JSON.stringify(e);return Ae(t).toString("base64")}function I(e){try{let t=Buffer.from(e,"base64"),n=De(t).toString("utf-8");return JSON.parse(n)}catch{return null}}async function se(e,t,n){if(n){let r=await t.get(n);if(r)return r}if(e.flowToken){let r=await t.get(e.flowToken);if(r)return r;let o=I(e.flowToken);if(o)return o}return null}var Ue={action:M.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:M.record(M.string(),M.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."),flowToken:M.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function ae(e){let{config:t,nodes:n,edges:r}=e,o=oe(t),a=`${t.description}
5
+ ${o}`,i=e.store??new _,s=new Map;async function c(l,w,f){if(l.action==="start"){let d=r.get(x);if(!d)return{content:{status:"error",error:"No start edge"}};let T={...l.stateUpdates??{}},u=await R(d,T);return W(u,T,n,r,s,f)}if(l.action==="continue"){let d=await se(l,i,w);if(!d)return{content:{status:"error",error:"Flow state not found for continue action. Pass back the flowToken from the previous response exactly as received."}};let T=d.state,u=d.step;if(!u)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let g={...T,...l.stateUpdates??{}};if(d.widgetId){let p=r.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let S=await R(p,g);return W(S,g,n,r,s,f)}return W(u,g,n,r,s,f)}return{content:{status:"error",error:`Unknown action: "${l.action}"`}}}return{id:t.id,title:t.title,description:a,async register(l){l.registerTool(t.id,{title:t.title,description:a,inputSchema:Ue,annotations:t.annotations},(async(w,f)=>{let T=f._meta??{},u=te(T),g=await c(w,u,T),p;if(g.flowTokenContent)if(u)await i.set(u,g.flowTokenContent),p=u;else{let k=re();await i.set(k,g.flowTokenContent),p=k}let S={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(S,null,2)}],_meta:T,...g.content.status==="error"?{isError:!0}:{}}}))}}}var C=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===x||t===E)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(),ae({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(x))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(x);if(t?.type==="direct"&&t.to!==E&&!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!==x&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==E&&!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 ce(e){return new C(e)}function de(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function le(e,t){if(!e)return null;if(t){let n=await t.get(e);if(n)return n}return I(e)}async function ue(e,t){let n=t?.stateStore,r=[],o={registerTool:(...c)=>{r.push(c)}};await e.register(o);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.id}" did not register a handler`);let i;async function s(c){return i=c.flowToken,{...c,decodedState:await le(i,n)}}return{async start(c){let l=await a({action:"start",...c?{stateUpdates:c}:{}},{});return s(de(l))},async continueWith(c){if(!i)throw new Error("No flowToken \u2014 call start() first");let l=await a({action:"continue",flowToken:i,...c?{stateUpdates:c}:{}},{});return s(de(l))},async lastState(){return le(i,n)}}}var A="text/html+skybridge",D="text/html;profile=mcp-app",pe=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function fe(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function ge(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 L(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 me(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:c=!0}=e,l=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:p}=new URL(o);(p==="localhost"||p==="127.0.0.1")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${p}:*`,`wss://${p}:*`],resource_domains:[...l.resource_domains||[],`http://${p}:*`]})}catch{}let w=`ui://widgets/apps-sdk/${t}.html`,f=`ui://widgets/ext-apps/${t}.html`,d=null,T=()=>(d||(d=pe(o,a)),d),u=r;async function g(p){let S=await T();p.registerResource(`${t}-openai-widget`,w,{title:n,description:u,mimeType:A,_meta:{"openai/widgetDescription":u,"openai/widgetPrefersBorder":s}},async y=>({contents:[{uri:y.href,mimeType:A,text:S,_meta:fe({description:u,prefersBorder:s,widgetDomain:i,widgetCSP:l})}]})),p.registerResource(`${t}-mcp-widget`,f,{title:n,description:u,mimeType:D,_meta:{ui:{prefersBorder:s}}},async y=>({contents:[{uri:y.href,mimeType:D,text:S,_meta:ge({description:u,prefersBorder:s,widgetCSP:l})}]}))}return{id:t,title:n,description:r,openaiUri:w,mcpUri:f,autoHeight:c,register:g}}function Ne(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:i=!0}=e,s=e.id??n?.id,c=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let l=n?L({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:s,title:c,description:r,async register(w){w.registerTool(s,{title:c,description:r,inputSchema:o,annotations:a,...l&&{_meta:l}},(async(f,d)=>{let u=d._meta??{},g=await t(f,{extra:{_meta:u}});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...l,...u,...i===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...i===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Oe(e,t){await Promise.all(t.map(n=>n.register(e)))}var U=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Be="@waniwani/sdk";function he(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 c=r(),l=`${t.replace(/\/$/,"")}${i}`,w={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Be},f={method:a,headers:w};s!==void 0&&(w["Content-Type"]="application/json",f.body=JSON.stringify(s));let d=await fetch(l,f);if(!d.ok){let u=await d.text().catch(()=>"");throw new U(u||`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")}}}var je="@waniwani/sdk";function O(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??we,o=Le(e),a=N(e.meta),i=N(e.metadata),s=Ke(e,a),c=v(e.eventId)??r(),l=He(e.timestamp,n),w=v(e.source)??t.source??je,f=K(e)?{...e}:void 0,d={...i};return Object.keys(a).length>0&&(d.meta=a),f&&(d.rawLegacy=f),{id:c,type:"mcp.event",name:o,source:w,timestamp:l,correlation:s,properties:qe(e,o),metadata:d,rawLegacy:f}}function we(){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(!K(e))return N(e.properties);let n=$e(e,t),r=N(e.properties);return{...n,...r}}function $e(e,t){switch(t){case"tool.called":{let n={};return v(e.toolName)&&(n.name=e.toolName),v(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),v(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return v(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),v(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Le(e){return K(e)?e.eventType:e.event}function Ke(e,t){let n=v(e.requestId)??P(t,["openai/requestId","requestId","mcp/requestId"]),r=v(e.sessionId)??P(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=v(e.traceId)??P(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=v(e.externalUserId)??P(t,["openai/userId","externalUserId","userId","actorId"]),i=v(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 He(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 N(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function K(e){return"eventType"in e}var Ve="/api/mcp/events/v2/batch";var ye="@waniwani/sdk",ze=new Set([401,403]),Ye=new Set([408,425,429,500,502,503,504]);function Te(e){return new H(e)}var H=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":ye},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 Xe(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ye,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Je(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 Je(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 Xe(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 ke(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?Te({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,c){o();let l=O({event:"user.identified",externalUserId:s,properties:c});return a?.enqueue(l),{eventId:l.id}},async track(s){o();let c=O(s);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 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 B(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=ke(o),i=he(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=B(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(),c=[];for(let l of i.events){let w=et(l),f=await s.track(w);c.push(f.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let c=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}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,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch{return null}}};function nt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Se="https://app.waniwani.ai";function rt(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??B(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let c=r._config.apiKey;return c?(a=new j({baseUrl:r._config.baseUrl??Se,apiKey:c}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[l,w,f]=c,d=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof f!="function")return s(...c);let T=f;return s(l,w,async(g,p)=>{let S=performance.now();try{let y=await T(g,p),k=Math.round(performance.now()-S);return await xe(r,ve(d,p,t,{durationMs:k,status:"ok"}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),o&&await ot(y,i(),r._config.baseUrl??Se,t.onError),st(y,p),y}catch(y){let k=Math.round(performance.now()-S);throw await xe(r,ve(d,p,t,{durationMs:k,status:"error",errorMessage:y instanceof Error?y.message:String(y)}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),y}})}),n}async function ot(e,t,n,r){if(!F(e))return;F(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?.(z(i))}o.waniwani=a}function ve(e,t,n,r){let o=it(e,n.toolType),a=Re(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function it(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Re(e){if(!F(e))return;let t=e._meta;if(F(t))return t}function F(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function xe(e,t,n){try{await e.track(t)}catch(r){n?.(z(r))}}async function Ee(e,t){try{await e.flush()}catch(n){t?.(z(n))}}function z(e){return e instanceof Error?e:new Error(String(e))}var V="waniwani/userLocation";function st(e,t){let n=Re(t);if(!n)return;let r=n[V];if(!r||!F(e))return;F(e._meta)||(e._meta={});let o=e._meta;o[V]||(o[V]=r)}export{E as END,x as START,C as StateGraph,ce as createFlow,ue as createFlowTestHarness,me as createResource,Ne as createTool,tt as createTrackingRoute,I as decodeFlowToken,q as detectPlatform,ie as encodeFlowToken,Fe as isMCPApps,Ce as isOpenAI,Oe as registerTools,rt as withWaniwani};
6
6
  //# sourceMappingURL=index.js.map