@waniwani/sdk 0.2.2-beta.1 → 0.2.2-beta.2

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,4 +1,4 @@
1
1
  function U(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function fe(){return U()==="openai"}function he(){return U()==="mcp-apps"}var S="__start__",E="__end__",O=Symbol.for("waniwani.flow.interrupt"),H=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:O,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:O,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:H,resource:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===O}function X(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===H}import{z as I}from"zod";var C="text/html+skybridge",_="text/html;profile=mcp-app",J=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Z(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function G(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 R(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}}}}}import{deflateSync as me,inflateSync as we}from"zlib";function Q(e){let t=JSON.stringify(e);return me(t).toString("base64")}function ee(e){try{let t=Buffer.from(e,"base64"),n=we(t).toString("utf-8");return JSON.parse(n)}catch{return null}}function ye(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 Te(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=ye(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"`: A widget UI is being shown. The user will interact with the widget.'," When the user makes a choice, call 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.",'6. CONVERSATIONAL STEPS: When a response includes a `"conversational"` field:'," - Do NOT immediately call `continue`. Instead, engage in back-and-forth conversation."," - The user may ask follow-up questions, explore options, or request changes.",' - Only call `action: "continue"` when the user explicitly wants to move on',` (e.g., "looks good", "let's continue", "I'll go with X", or selects an option).`," - If `conversational` is a string, use it as guidance for what topics to discuss."," - While conversing, do NOT call the tool \u2014 just respond naturally to the user."),t.join(`
2
2
  `)}function ke(e){if(e.flowToken){let t=ee(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function M(e,t){return e.type==="direct"?e.to:e.condition(t)}function q(e){return e!=null&&e!==""}function te(e,t,n,r,o){if(e.every(g=>q(r[g.field])))return null;let a=e.filter(g=>!q(r[g.field])),i=a.length===1,s=a[0],d=o?typeof o=="string"?o:!0:void 0;return{payload:i&&s?{status:"interrupt",question:s.question,field:s.field,...s.suggestions?{suggestions:s.suggestions}:{},...s.context||t?{context:s.context??t}:{},...d?{conversational:d}:{}}:{status:"interrupt",questions:a,...t?{context:t}:{},...d?{conversational:d}:{}},flowMeta:{step:n,state:r,...i&&s?{field:s.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function B(e,t,n,r,o,a){let i=e,s={...t},d=50,f=0;for(;f++<d;){if(i===E)return{payload:{status:"complete"},flowMeta:{state:s}};let g=n.get(i);if(!g)return{payload:{status:"error",error:`Unknown node: "${i}"`}};try{let u=await g(s,a);if(Y(u)){let c=te(u.questions,u.context,i,s,r.get(i)?.conversational);if(c)return c;let p=o.get(i);if(!p)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await M(p,s);continue}if(X(u)){let c=u.field??r.get(i)?.field;if(c&&q(s[c])){let w=o.get(i);if(!w)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await M(w,s);continue}let p=u.resource,T=r.get(i)?.conversational;return{payload:{status:"widget",description:u.description,...T?{conversational:typeof T=="string"?T:!0}:{}},data:u.data,widgetMeta:R({openaiTemplateUri:p.openaiUri,mcpTemplateUri:p.mcpUri,invoking:"Loading...",invoked:"Loaded",autoHeight:p.autoHeight}),flowMeta:{step:i,state:s,field:c,widgetId:p.id}}}s={...s,...u};let l=o.get(i);if(!l)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await M(l,s)}catch(u){return{payload:{status:"error",error:u instanceof Error?u.message:String(u)},flowMeta:{step:i,state:s}}}}return{payload:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var ve={action:I.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:I.record(I.string(),I.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:I.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function ne(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,a=Te(t),i=`${t.description}
3
- ${a}`;async function s(d,f){let g=ke(d),u=g.state;if(d.action==="start"){let l=o.get(S);if(!l)return{payload:{status:"error",error:"No start edge"}};let c={...u,...d.stateUpdates??{}},p=await M(l,c);return B(p,c,n,r,o,f)}if(d.action==="continue"){let l=g.step;if(!l)return{payload:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let c={...u,...d.stateUpdates??{}};if(g.questions){let p=te(g.questions,g.interruptContext,l,c,r.get(l)?.conversational);if(p)return p}if(g.questions||g.widgetId){let p=o.get(l);if(!p)return{payload:{status:"error",error:`No edge from step "${l}"`}};let T=await M(p,c);return B(T,c,n,r,o,f)}return B(l,c,n,r,o,f)}return{payload:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:i,async register(d){d.registerTool(t.id,{title:t.title,description:i,inputSchema:ve,annotations:t.annotations},(async(f,g)=>{let l=g._meta??{},c=await s(f,l),p=c.flowMeta?Q({step:c.flowMeta.step??"",state:c.flowMeta.state,field:c.flowMeta.field,widgetId:c.flowMeta.widgetId,questions:c.flowMeta.questions,interruptContext:c.flowMeta.interruptContext}):void 0,T={...c.payload,...p?{flowToken:p,flowId:t.id,...c.flowMeta?.widgetId?{widgetId:c.flowMeta.widgetId}:{}}:{}},w=[{type:"text",text:JSON.stringify(T,null,2)}],v={...c.widgetMeta??{},...l};return c.widgetMeta?{content:w,structuredContent:c.data,_meta:v}:{content:w,...Object.keys(v).length>0?{_meta:v}:{}}}))}}}var x=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===S||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`);let o,a={};if(typeof n=="function")o=n;else if(r)o=r,a=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,a),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),ne({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(S))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(S);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!==S&&!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 re(e){return new x(e)}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:d=!0}=e,f=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(f={...f,connect_domains:[...f.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...f.resource_domains||[],`http://${w}:*`]})}catch{}let g=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,l=null,c=()=>(l||(l=J(o,a)),l),p=r;async function T(w){let v=await c();w.registerResource(`${t}-openai-widget`,g,{title:n,description:p,mimeType:C,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":s}},async k=>({contents:[{uri:k.href,mimeType:C,text:v,_meta:Z({description:p,prefersBorder:s,widgetDomain:i,widgetCSP:f})}]})),w.registerResource(`${t}-mcp-widget`,u,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:s}}},async k=>({contents:[{uri:k.href,mimeType:_,text:v,_meta:G({description:p,prefersBorder:s,widgetCSP:f})}]}))}return{id:t,title:n,description:r,openaiUri:g,mcpUri:u,autoHeight:d,register:T}}function Se(e,t){let{resource:n,description:r,inputSchema:o,annotations:a}=e,i=e.id??n?.id,s=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!s)throw new Error("createTool: `title` is required when no resource is provided");let d=n?R({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:s,description:r,async register(f){f.registerTool(i,{title:s,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(g,u)=>{let c=u._meta??{},p=await t(g,{extra:{_meta:c}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...d,...c}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var P=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(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 d=r(),f=`${t.replace(/\/$/,"")}${i}`,g={Authorization:`Bearer ${d}`,"X-WaniWani-SDK":xe},u={method:a,headers:g};s!==void 0&&(g["Content-Type"]="application/json",u.body=JSON.stringify(s));let l=await fetch(f,u);if(!l.ok){let p=await l.text().catch(()=>"");throw new P(p||`KB API error: HTTP ${l.status}`,l.status)}return(await l.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 Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),a=W(e.meta),i=W(e.metadata),s=Ce(e,a),d=y(e.eventId)??r(),f=_e(e.timestamp,n),g=y(e.source)??t.source??Re,u=$(e)?{...e}:void 0,l={...i};return Object.keys(a).length>0&&(l.meta=a),u&&(l.rawLegacy=u),{id:d,type:"mcp.event",name:o,source:g,timestamp:f,correlation:s,properties:Ie(e,o),metadata:l,rawLegacy:u}}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 Ie(e,t){if(!$(e))return W(e.properties);let n=Me(e,t),r=W(e.properties);return{...n,...r}}function Me(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function Ce(e,t){let n=y(e.requestId)??b(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??b(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??b(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=y(e.externalUserId)??b(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??b(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 _e(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 b(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function W(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Ae=new Set([408,425,429,500,502,503,504]);function de(e){return new L(e)}var L=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=De(t.baseUrl,t.endpointPath??Pe),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":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ue(a)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Ae.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(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:ae,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(Fe(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 Fe(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 Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function ce(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?de({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 track(s){o();let d=j(s);return a?.enqueue(d),{eventId:d.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&&Oe(i,r.shutdownTimeoutMs),i}function Oe(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 A(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=ce(o),i=ie(o);return{...a,kb:i,_config:o}}function Be(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 qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=A(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(),d=[];for(let f of i.events){let g=Be(f),u=await s.track(g);d.push(u.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:d.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let d=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:d}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=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=je(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,d=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(d)?null:(this.cached={token:s.token,expiresAt:d},s.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??A(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=r._config.apiKey;return d?(a=new F({baseUrl:r._config.baseUrl??ue,apiKey:d}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[f,g,u]=d,l=typeof f=="string"&&f.trim().length>0?f:"unknown";if(typeof u!="function")return s(...d);let c=u;return s(f,g,async(T,w)=>{let v=performance.now();try{let k=await c(T,w),D=Math.round(performance.now()-v);return await pe(r,le(l,w,t,{durationMs:D,status:"ok"}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),o&&await Le(k,i(),r._config.baseUrl??ue,t.onError),k}catch(k){let D=Math.round(performance.now()-v);throw await pe(r,le(l,w,t,{durationMs:D,status:"error",errorMessage:k instanceof Error?k.message:String(k)}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),k}})}),n}async function Le(e,t,n,r){if(!N(e))return;N(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?.(V(i))}o.waniwani=a}function le(e,t,n,r){let o=Ve(e,n.toolType),a=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!N(e))return;let t=e._meta;if(N(t))return t}function N(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function pe(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function ge(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{E as END,S as START,x as StateGraph,re as createFlow,oe as createResource,Se as createTool,qe as createTrackingRoute,U as detectPlatform,z as interrupt,he as isMCPApps,fe as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
3
+ ${a}`;async function s(d,f){let g=ke(d),u=g.state;if(d.action==="start"){let l=o.get(S);if(!l)return{payload:{status:"error",error:"No start edge"}};let c={...u,...d.stateUpdates??{}},p=await M(l,c);return B(p,c,n,r,o,f)}if(d.action==="continue"){let l=g.step;if(!l)return{payload:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let c={...u,...d.stateUpdates??{}};if(g.questions){let p=te(g.questions,g.interruptContext,l,c,r.get(l)?.conversational);if(p)return p}if(g.questions||g.widgetId){let p=o.get(l);if(!p)return{payload:{status:"error",error:`No edge from step "${l}"`}};let T=await M(p,c);return B(T,c,n,r,o,f)}return B(l,c,n,r,o,f)}return{payload:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:i,async register(d){d.registerTool(t.id,{title:t.title,description:i,inputSchema:ve,annotations:t.annotations,_meta:{"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0}},(async(f,g)=>{let l=g._meta??{},c=await s(f,l),p=c.flowMeta?Q({step:c.flowMeta.step??"",state:c.flowMeta.state,field:c.flowMeta.field,widgetId:c.flowMeta.widgetId,questions:c.flowMeta.questions,interruptContext:c.flowMeta.interruptContext}):void 0,T={...c.payload,...p?{flowToken:p,flowId:t.id,...c.flowMeta?.widgetId?{widgetId:c.flowMeta.widgetId}:{}}:{}},w=[{type:"text",text:JSON.stringify(T,null,2)}],v={...c.widgetMeta??{},...l};return c.widgetMeta?{content:w,structuredContent:c.data,_meta:v}:{content:w,...Object.keys(v).length>0?{_meta:v}:{}}}))}}}var x=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===S||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`);let o,a={};if(typeof n=="function")o=n;else if(r)o=r,a=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,a),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),ne({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(S))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(S);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!==S&&!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 re(e){return new x(e)}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:d=!0}=e,f=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(f={...f,connect_domains:[...f.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...f.resource_domains||[],`http://${w}:*`]})}catch{}let g=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,l=null,c=()=>(l||(l=J(o,a)),l),p=r;async function T(w){let v=await c();w.registerResource(`${t}-openai-widget`,g,{title:n,description:p,mimeType:C,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":s}},async k=>({contents:[{uri:k.href,mimeType:C,text:v,_meta:Z({description:p,prefersBorder:s,widgetDomain:i,widgetCSP:f})}]})),w.registerResource(`${t}-mcp-widget`,u,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:s}}},async k=>({contents:[{uri:k.href,mimeType:_,text:v,_meta:G({description:p,prefersBorder:s,widgetCSP:f})}]}))}return{id:t,title:n,description:r,openaiUri:g,mcpUri:u,autoHeight:d,register:T}}function Se(e,t){let{resource:n,description:r,inputSchema:o,annotations:a}=e,i=e.id??n?.id,s=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!s)throw new Error("createTool: `title` is required when no resource is provided");let d=n?R({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:s,description:r,async register(f){f.registerTool(i,{title:s,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(g,u)=>{let c=u._meta??{},p=await t(g,{extra:{_meta:c}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...d,...c}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var P=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(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 d=r(),f=`${t.replace(/\/$/,"")}${i}`,g={Authorization:`Bearer ${d}`,"X-WaniWani-SDK":xe},u={method:a,headers:g};s!==void 0&&(g["Content-Type"]="application/json",u.body=JSON.stringify(s));let l=await fetch(f,u);if(!l.ok){let p=await l.text().catch(()=>"");throw new P(p||`KB API error: HTTP ${l.status}`,l.status)}return(await l.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 Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),a=W(e.meta),i=W(e.metadata),s=Ce(e,a),d=y(e.eventId)??r(),f=_e(e.timestamp,n),g=y(e.source)??t.source??Re,u=$(e)?{...e}:void 0,l={...i};return Object.keys(a).length>0&&(l.meta=a),u&&(l.rawLegacy=u),{id:d,type:"mcp.event",name:o,source:g,timestamp:f,correlation:s,properties:Ie(e,o),metadata:l,rawLegacy:u}}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 Ie(e,t){if(!$(e))return W(e.properties);let n=Me(e,t),r=W(e.properties);return{...n,...r}}function Me(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function Ce(e,t){let n=y(e.requestId)??b(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??b(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??b(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=y(e.externalUserId)??b(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??b(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 _e(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 b(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function W(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Ae=new Set([408,425,429,500,502,503,504]);function de(e){return new L(e)}var L=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=De(t.baseUrl,t.endpointPath??Pe),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":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ue(a)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Ae.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(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:ae,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(Fe(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 Fe(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 Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function ce(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?de({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 track(s){o();let d=j(s);return a?.enqueue(d),{eventId:d.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&&Oe(i,r.shutdownTimeoutMs),i}function Oe(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 A(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=ce(o),i=ie(o);return{...a,kb:i,_config:o}}function Be(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 qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=A(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(),d=[];for(let f of i.events){let g=Be(f),u=await s.track(g);d.push(u.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:d.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let d=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:d}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=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=je(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,d=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(d)?null:(this.cached={token:s.token,expiresAt:d},s.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??A(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=r._config.apiKey;return d?(a=new F({baseUrl:r._config.baseUrl??ue,apiKey:d}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[f,g,u]=d,l=typeof f=="string"&&f.trim().length>0?f:"unknown";if(typeof u!="function")return s(...d);let c=u;return s(f,g,async(T,w)=>{let v=performance.now();try{let k=await c(T,w),D=Math.round(performance.now()-v);return await pe(r,le(l,w,t,{durationMs:D,status:"ok"}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),o&&await Le(k,i(),r._config.baseUrl??ue,t.onError),k}catch(k){let D=Math.round(performance.now()-v);throw await pe(r,le(l,w,t,{durationMs:D,status:"error",errorMessage:k instanceof Error?k.message:String(k)}),t.onError),t.flushAfterToolCall&&await ge(r,t.onError),k}})}),n}async function Le(e,t,n,r){if(!N(e))return;N(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?.(V(i))}o.waniwani=a}function le(e,t,n,r){let o=Ve(e,n.toolType),a=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!N(e))return;let t=e._meta;if(N(t))return t}function N(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function pe(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function ge(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{E as END,S as START,x as StateGraph,re as createFlow,oe as createResource,Se as createTool,qe as createTrackingRoute,U as detectPlatform,z as interrupt,he as isMCPApps,fe as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
4
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/mcp/react/widgets/platform.ts","../../src/mcp/server/flows/@types.ts","../../src/mcp/server/flows/compile.ts","../../src/mcp/server/resources/meta.ts","../../src/mcp/server/flows/flow-token.ts","../../src/mcp/server/flows/state-graph.ts","../../src/mcp/server/flows/create-flow.ts","../../src/mcp/server/resources/create-resource.ts","../../src/mcp/server/tools/create-tool.ts","../../src/error.ts","../../src/kb/client.ts","../../src/tracking/mapper.ts","../../src/tracking/transport.ts","../../src/tracking/index.ts","../../src/waniwani.ts","../../src/mcp/server/tracking-route.ts","../../src/mcp/server/widget-token.ts","../../src/mcp/server/with-waniwani.ts"],"sourcesContent":["/**\n * Widget platform types\n */\nexport type WidgetPlatform = \"openai\" | \"mcp-apps\";\n\n/**\n * Detects which platform the widget is running on.\n *\n * OpenAI injects a global `window.openai` object.\n * MCP Apps runs in a sandboxed iframe and uses postMessage.\n */\nexport function detectPlatform(): WidgetPlatform {\n\tif (typeof window !== \"undefined\" && \"openai\" in window) {\n\t\treturn \"openai\";\n\t}\n\treturn \"mcp-apps\";\n}\n\n/**\n * Check if running on OpenAI platform\n */\nexport function isOpenAI(): boolean {\n\treturn detectPlatform() === \"openai\";\n}\n\n/**\n * Check if running on MCP Apps platform\n */\nexport function isMCPApps(): boolean {\n\treturn detectPlatform() === \"mcp-apps\";\n}\n","import type { z } from \"zod\";\nimport type { McpServer, RegisteredResource } from \"../resources/types\";\n\nexport type { McpServer };\n\n// ============================================================================\n// Sentinel constants\n// ============================================================================\n\nexport const START = \"__start__\" as const;\nexport const END = \"__end__\" as const;\n\n// ============================================================================\n// Signal types — returned by node handlers to control flow behavior\n// ============================================================================\n\nconst INTERRUPT = Symbol.for(\"waniwani.flow.interrupt\");\nconst WIDGET = Symbol.for(\"waniwani.flow.widget\");\n\n/** A single question within an interrupt step */\nexport type InterruptQuestion = {\n\t/** Question to ask the user */\n\tquestion: string;\n\t/** State key where the answer will be stored */\n\tfield: string;\n\t/** Optional suggestions to present as options */\n\tsuggestions?: string[];\n\t/** Hidden context/instructions for this specific question (not shown to user directly) */\n\tcontext?: string;\n};\n\n/**\n * Interrupt signal — pauses the flow and asks the user one or more questions.\n * Single-question and multi-question interrupts use the same type.\n */\nexport type InterruptSignal = {\n\treadonly __type: typeof INTERRUPT;\n\t/** Questions to ask — ask all in one conversational message */\n\tquestions: InterruptQuestion[];\n\t/** Overall hidden context/instructions for the assistant (not shown to user directly) */\n\tcontext?: string;\n};\n\nexport type WidgetSignal = {\n\treadonly __type: typeof WIDGET;\n\t/** The resource to display */\n\tresource: RegisteredResource;\n\t/** Data to pass to the widget as structuredContent */\n\tdata: Record<string, unknown>;\n\t/** Description of what the widget does (for the AI's context) */\n\tdescription?: string;\n\t/**\n\t * State key this widget fills — enables auto-skip when the field is already in state.\n\t * Pass this so the engine can skip the widget step when the answer is already known.\n\t */\n\tfield?: string;\n};\n\n/**\n * Create an interrupt signal — pauses the flow and asks the user a question.\n *\n * Accepts a single question (shorthand) or multiple questions (array form).\n * Both produce the same signal type.\n *\n * @example Single question\n * ```ts\n * return interrupt({ question: \"Your email?\", field: \"email\" })\n * ```\n *\n * @example Multiple questions (asked together in one message)\n * ```ts\n * return interrupt({ questions: [\n * { question: \"How many employees?\", field: \"headcount\" },\n * { question: \"Average age?\", field: \"averageAge\" },\n * ]})\n * ```\n */\nexport function interrupt(\n\tconfig:\n\t\t| {\n\t\t\t\tquestion: string;\n\t\t\t\tfield: string;\n\t\t\t\tsuggestions?: string[];\n\t\t\t\tcontext?: string;\n\t\t }\n\t\t| { questions: InterruptQuestion[]; context?: string },\n): InterruptSignal {\n\tif (\"questions\" in config) {\n\t\treturn {\n\t\t\t__type: INTERRUPT,\n\t\t\tquestions: config.questions,\n\t\t\tcontext: config.context,\n\t\t};\n\t}\n\tconst { question, field, context, suggestions } = config;\n\treturn {\n\t\t__type: INTERRUPT,\n\t\tquestions: [{ question, field, context, suggestions }],\n\t};\n}\n\n/**\n * Create a widget signal — pauses the flow and renders a widget UI.\n *\n * Pass `field` to enable auto-skip: if the field is already in state, the widget\n * step will be skipped automatically.\n */\nexport function showWidget(\n\tresource: RegisteredResource,\n\tconfig: {\n\t\tdata: Record<string, unknown>;\n\t\tdescription?: string;\n\t\tfield?: string;\n\t},\n): WidgetSignal {\n\treturn { __type: WIDGET, resource, ...config };\n}\n\nexport function isInterrupt(value: unknown): value is InterruptSignal {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"__type\" in value &&\n\t\t(value as InterruptSignal).__type === INTERRUPT\n\t);\n}\n\nexport function isWidget(value: unknown): value is WidgetSignal {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"__type\" in value &&\n\t\t(value as WidgetSignal).__type === WIDGET\n\t);\n}\n\n// ============================================================================\n// Node & edge definitions\n// ============================================================================\n\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * Optional config for handler-based nodes.\n * Provides metadata used by the engine (e.g., auto-skip on widget steps).\n *\n * For declarative nodes, config is inferred automatically — no need to pass this.\n */\nexport type NodeConfig<\n\tTState extends Record<string, unknown> = Record<string, unknown>,\n> = {\n\t/**\n\t * State key this node fills.\n\t * When set on a handler-based widget node and the field is already in state,\n\t * the node is auto-skipped. (Alternatively, pass `field` to `showWidget()`.)\n\t */\n\tfield?: Extract<keyof TState, string>;\n\t/**\n\t * Mark this node as conversational — the AI will engage in back-and-forth\n\t * conversation before advancing to the next node.\n\t *\n\t * - `true` — generic conversational behavior\n\t * - `string` — specific guidance for the AI (e.g., \"Help the user compare plans\")\n\t */\n\tconversational?: boolean | string;\n};\n\n// ============================================================================\n// Declarative node configs — shorthand for common patterns, no handler needed\n// ============================================================================\n\n/**\n * Node handler — a single function type for all node kinds.\n * The return value determines behavior:\n * - `Partial<TState>` → action node (state merged, auto-advance)\n * - `InterruptSignal` → interrupt (pause, ask user one or more questions)\n * - `WidgetSignal` → widget step (pause, show widget)\n */\nexport type NodeHandler<TState> = (\n\tstate: Partial<TState>,\n\tmeta?: Record<string, unknown>,\n) => MaybePromise<Partial<TState> | InterruptSignal | WidgetSignal>;\n\n/**\n * Condition function for conditional edges.\n * Receives current state, returns the name of the next node.\n */\nexport type ConditionFn<TState> = (\n\tstate: Partial<TState>,\n) => string | Promise<string>;\n\nexport type Edge<TState> =\n\t| { type: \"direct\"; to: string }\n\t| { type: \"conditional\"; condition: ConditionFn<TState> };\n\n// ============================================================================\n// Flow config & compiled output\n// ============================================================================\n\nexport type FlowConfig = {\n\t/** Unique identifier for the flow (becomes the MCP tool name) */\n\tid: string;\n\t/** Display title */\n\ttitle: string;\n\t/** Description for the AI (explains when to use this flow) */\n\tdescription: string;\n\t/**\n\t * Define the flow's state — each field the flow collects.\n\t * Keys are the field names used in `interrupt({ field })` or `NodeConfig.field`,\n\t * values are Zod schemas with `.describe()`.\n\t *\n\t * The state definition serves two purposes:\n\t * 1. Type inference — `TState` is automatically derived, no explicit generic needed\n\t * 2. AI protocol — field names, types, and descriptions are included in the tool\n\t * description so the AI can pre-fill answers via `_meta.flow.state`\n\t *\n\t * @example\n\t * ```ts\n\t * state: {\n\t * country: z.string().describe(\"Country the business is based in\"),\n\t * status: z.enum([\"registered\", \"unregistered\"]).describe(\"Business registration status\"),\n\t * }\n\t * ```\n\t */\n\tstate: Record<string, z.ZodType>;\n\t/** Optional tool annotations */\n\tannotations?: {\n\t\treadOnlyHint?: boolean;\n\t\tidempotentHint?: boolean;\n\t\topenWorldHint?: boolean;\n\t\tdestructiveHint?: boolean;\n\t};\n};\n\n/**\n * Infer the runtime state type from a flow's state schema definition.\n *\n * @example\n * ```ts\n * const config = {\n * state: {\n * country: z.enum([\"FR\", \"DE\"]),\n * status: z.enum([\"registered\", \"unregistered\"]),\n * }\n * };\n * type MyState = InferFlowState<typeof config.state>;\n * // { country: \"FR\" | \"DE\"; status: \"registered\" | \"unregistered\" }\n * ```\n */\nexport type InferFlowState<T extends Record<string, z.ZodType>> = {\n\t[K in keyof T]: z.infer<T[K]>;\n};\n\n/**\n * A compiled flow — can be registered on an McpServer.\n */\nexport type RegisteredFlow = {\n\tid: string;\n\ttitle: string;\n\tdescription: string;\n\tregister: (server: McpServer) => Promise<void>;\n};\n","import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport type { RequestHandlerExtra } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport type {\n\tServerNotification,\n\tServerRequest,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { z } from \"zod\";\nimport { buildToolMeta } from \"../resources/meta\";\nimport type {\n\tEdge,\n\tFlowConfig,\n\tMcpServer,\n\tNodeConfig,\n\tNodeHandler,\n\tRegisteredFlow,\n} from \"./@types\";\nimport { END, isInterrupt, isWidget, START } from \"./@types\";\nimport type { FlowTokenData } from \"./flow-token\";\nimport { decodeFlowToken, encodeFlowToken } from \"./flow-token\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\ninterface CompileInput<TState extends Record<string, unknown>> {\n\tconfig: FlowConfig;\n\tnodes: Map<string, NodeHandler<TState>>;\n\tnodeConfigs: Map<string, NodeConfig<TState>>;\n\tedges: Map<string, Edge<TState>>;\n}\n\ntype FlowToolInput = {\n\taction: \"start\" | \"continue\";\n\tstateUpdates?: Record<string, unknown>;\n\tflowToken?: string;\n};\n\ntype FlowPayload = {\n\tstatus: \"widget\" | \"interrupt\" | \"complete\" | \"error\";\n\t[key: string]: unknown;\n};\n\ntype ExecutionResult = {\n\tpayload: FlowPayload;\n\tdata?: Record<string, unknown>;\n\twidgetMeta?: Record<string, unknown>;\n\tflowMeta?: {\n\t\tstep?: string;\n\t\tstate: Record<string, unknown>;\n\t\tfield?: string;\n\t\twidgetId?: string;\n\t\t/** Cached interrupt questions — avoids re-executing the handler on partial answers */\n\t\tquestions?: Array<{\n\t\t\tquestion: string;\n\t\t\tfield: string;\n\t\t\tsuggestions?: string[];\n\t\t\tcontext?: string;\n\t\t}>;\n\t\t/** Cached overall interrupt context */\n\t\tinterruptContext?: string;\n\t};\n};\n\n// ============================================================================\n// Flow protocol — embedded in tool description\n// ============================================================================\n\n/** Extract a human-readable label from a Zod schema for the AI protocol */\nfunction describeZodField(schema: z.ZodType): string {\n\tconst desc = schema.description ?? \"\";\n\tconst def = (\n\t\tschema as unknown as {\n\t\t\t_zod: { def: { type: string; entries?: Record<string, string> } };\n\t\t}\n\t)._zod?.def;\n\n\tif (def?.type === \"enum\" && def.entries) {\n\t\tconst vals = Object.keys(def.entries)\n\t\t\t.map((v) => `\"${v}\"`)\n\t\t\t.join(\" | \");\n\t\treturn desc ? `${vals} — ${desc}` : vals;\n\t}\n\n\treturn desc;\n}\n\nfunction buildFlowProtocol(config: FlowConfig): string {\n\tconst lines = [\n\t\t\"\",\n\t\t\"## FLOW EXECUTION PROTOCOL\",\n\t\t\"\",\n\t\t\"This tool implements a multi-step conversational flow. Follow this protocol exactly:\",\n\t\t\"\",\n\t\t'1. Call with `action: \"start\"` to begin. If the user\\'s message already',\n\t\t\" contains answers to likely questions, extract them into `stateUpdates`\",\n\t\t\" as `{ field: value }` pairs. The engine will auto-skip steps whose\",\n\t\t\" fields are already filled.\",\n\t\t\" Only extract values the user explicitly stated — do NOT guess or invent values.\",\n\t];\n\n\tif (config.state) {\n\t\tconst fieldList = Object.entries(config.state)\n\t\t\t.map(([key, schema]) => {\n\t\t\t\tconst info = describeZodField(schema);\n\t\t\t\treturn info ? `\\`${key}\\` (${info})` : `\\`${key}\\``;\n\t\t\t})\n\t\t\t.join(\", \");\n\t\tlines.push(` Known fields: ${fieldList}.`);\n\t}\n\n\tlines.push(\n\t\t\"2. The response JSON `status` field tells you what to do next:\",\n\t\t' - `\"interrupt\"`: Pause and ask the user. Two forms:',\n\t\t\" a. Single question: `{ question, field, context? }` — ask `question`, store answer in `field`.\",\n\t\t\" b. Multi-question: `{ questions: [{question, field}, ...], context? }` — ask ALL questions\",\n\t\t\" in one conversational message, collect all answers.\",\n\t\t\" `context` (if present) is hidden AI instructions — use to shape your response, do NOT show verbatim.\",\n\t\t\" Then call again with:\",\n\t\t' `action: \"continue\"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),',\n\t\t\" `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.\",\n\t\t' - `\"widget\"`: A widget UI is being shown. The user will interact with the widget.',\n\t\t\" When the user makes a choice, call again with:\",\n\t\t' `action: \"continue\"`, `flowToken` = the `flowToken` from the response,',\n\t\t\" `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.\",\n\t\t' - `\"complete\"`: The flow is done. Present the result to the user.',\n\t\t' - `\"error\"`: Something went wrong. Show the `error` message.',\n\t\t\"\",\n\t\t\"3. ALWAYS pass back the `flowToken` string exactly as received — it is an opaque token, do not modify it.\",\n\t\t\"4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.\",\n\t\t\"5. Include only the fields the user actually answered in `stateUpdates` — do NOT guess missing ones.\",\n\t\t\" If the user did not answer all pending questions, the engine will re-prompt for the remaining ones.\",\n\t\t\" If the user mentioned values for other known fields, include those too —\",\n\t\t\" they will be applied immediately and those steps will be auto-skipped.\",\n\t\t'6. CONVERSATIONAL STEPS: When a response includes a `\"conversational\"` field:',\n\t\t\" - Do NOT immediately call `continue`. Instead, engage in back-and-forth conversation.\",\n\t\t\" - The user may ask follow-up questions, explore options, or request changes.\",\n\t\t' - Only call `action: \"continue\"` when the user explicitly wants to move on',\n\t\t' (e.g., \"looks good\", \"let\\'s continue\", \"I\\'ll go with X\", or selects an option).',\n\t\t\" - If `conversational` is a string, use it as guidance for what topics to discuss.\",\n\t\t\" - While conversing, do NOT call the tool — just respond naturally to the user.\",\n\t);\n\n\treturn lines.join(\"\\n\");\n}\n\nfunction getInputMeta(args: FlowToolInput): FlowTokenData & {\n\tstate: Record<string, unknown>;\n} {\n\tif (args.flowToken) {\n\t\tconst decoded = decodeFlowToken(args.flowToken);\n\t\tif (decoded) return decoded;\n\t}\n\treturn { step: undefined as unknown as string, state: {} };\n}\n\n// ============================================================================\n// Edge resolution\n// ============================================================================\n\nasync function resolveNextNode<TState extends Record<string, unknown>>(\n\tedge: Edge<TState>,\n\tstate: Partial<TState>,\n): Promise<string> {\n\tif (edge.type === \"direct\") return edge.to;\n\treturn edge.condition(state);\n}\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Check whether a state value counts as \"filled\" (not empty/missing). */\nfunction isFilled(v: unknown): boolean {\n\treturn v !== undefined && v !== null && v !== \"\";\n}\n\n// ============================================================================\n// Interrupt result builder\n// ============================================================================\n\ntype InterruptQuestionData = {\n\tquestion: string;\n\tfield: string;\n\tsuggestions?: string[];\n\tcontext?: string;\n};\n\n/**\n * Build an interrupt ExecutionResult from a list of questions and current state.\n * Filters out already-answered questions and caches the full question list in\n * flowMeta so partial-answer continues can filter without re-executing the handler.\n *\n * Returns `null` when all questions are already filled (caller should advance).\n */\nfunction buildInterruptResult<TState extends Record<string, unknown>>(\n\tquestions: InterruptQuestionData[],\n\tcontext: string | undefined,\n\tcurrentNode: string,\n\tstate: TState,\n\tconversational?: boolean | string,\n): ExecutionResult | null {\n\t// All filled — caller should advance to the next node\n\tif (questions.every((q) => isFilled(state[q.field as keyof TState]))) {\n\t\treturn null;\n\t}\n\n\t// Filter out questions whose fields are already answered\n\tconst unanswered = questions.filter(\n\t\t(q) => !isFilled(state[q.field as keyof TState]),\n\t);\n\n\t// Single-question shorthand: unwrap for cleaner AI payload\n\tconst isSingle = unanswered.length === 1;\n\tconst q0 = unanswered[0];\n\tconst conversationalValue = conversational\n\t\t? typeof conversational === \"string\"\n\t\t\t? conversational\n\t\t\t: true\n\t\t: undefined;\n\tconst payload =\n\t\tisSingle && q0\n\t\t\t? {\n\t\t\t\t\tstatus: \"interrupt\" as const,\n\t\t\t\t\tquestion: q0.question,\n\t\t\t\t\tfield: q0.field,\n\t\t\t\t\t...(q0.suggestions ? { suggestions: q0.suggestions } : {}),\n\t\t\t\t\t...(q0.context || context ? { context: q0.context ?? context } : {}),\n\t\t\t\t\t...(conversationalValue\n\t\t\t\t\t\t? { conversational: conversationalValue }\n\t\t\t\t\t\t: {}),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tstatus: \"interrupt\" as const,\n\t\t\t\t\tquestions: unanswered,\n\t\t\t\t\t...(context ? { context } : {}),\n\t\t\t\t\t...(conversationalValue\n\t\t\t\t\t\t? { conversational: conversationalValue }\n\t\t\t\t\t\t: {}),\n\t\t\t\t};\n\n\treturn {\n\t\tpayload,\n\t\tflowMeta: {\n\t\t\tstep: currentNode,\n\t\t\tstate,\n\t\t\t...(isSingle && q0 ? { field: q0.field } : {}),\n\t\t\t// Cache the full question list so partial-answer continues\n\t\t\t// can filter without re-executing the node handler.\n\t\t\tquestions,\n\t\t\t...(context ? { interruptContext: context } : {}),\n\t\t},\n\t};\n}\n\n// ============================================================================\n// Execution engine\n// ============================================================================\n\nasync function executeFrom<TState extends Record<string, unknown>>(\n\tstartNodeName: string,\n\tstartState: TState,\n\tnodes: Map<string, NodeHandler<TState>>,\n\tnodeConfigs: Map<string, NodeConfig<TState>>,\n\tedges: Map<string, Edge<TState>>,\n\tmeta?: Record<string, unknown>,\n): Promise<ExecutionResult> {\n\tlet currentNode = startNodeName;\n\tlet state = { ...startState };\n\n\t// Safety limit to prevent infinite loops\n\tconst MAX_ITERATIONS = 50;\n\tlet iterations = 0;\n\n\twhile (iterations++ < MAX_ITERATIONS) {\n\t\t// Reached END\n\t\tif (currentNode === END) {\n\t\t\treturn {\n\t\t\t\tpayload: { status: \"complete\" },\n\t\t\t\tflowMeta: { state },\n\t\t\t};\n\t\t}\n\n\t\tconst handler = nodes.get(currentNode);\n\t\tif (!handler) {\n\t\t\treturn {\n\t\t\t\tpayload: {\n\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\terror: `Unknown node: \"${currentNode}\"`,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = await handler(state, meta);\n\n\t\t\t// Interrupt signal — pause and ask the user one or more questions\n\t\t\tif (isInterrupt(result)) {\n\t\t\t\tconst interruptResult = buildInterruptResult(\n\t\t\t\t\tresult.questions,\n\t\t\t\t\tresult.context,\n\t\t\t\t\tcurrentNode,\n\t\t\t\t\tstate,\n\t\t\t\t\tnodeConfigs.get(currentNode)?.conversational,\n\t\t\t\t);\n\t\t\t\tif (interruptResult) return interruptResult;\n\n\t\t\t\t// All questions filled — auto-skip to next node\n\t\t\t\tconst edge = edges.get(currentNode);\n\t\t\t\tif (!edge) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Widget signal — pause and show widget\n\t\t\tif (isWidget(result)) {\n\t\t\t\t// Auto-skip: use field from the signal, fall back to nodeConfig\n\t\t\t\tconst widgetField = result.field ?? nodeConfigs.get(currentNode)?.field;\n\t\t\t\tif (widgetField) {\n\t\t\t\t\tif (isFilled(state[widgetField as keyof TState])) {\n\t\t\t\t\t\tconst edge = edges.get(currentNode);\n\t\t\t\t\t\tif (!edge) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst resource = result.resource;\n\t\t\t\tconst nodeConversational = nodeConfigs.get(currentNode)?.conversational;\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"widget\",\n\t\t\t\t\t\tdescription: result.description,\n\t\t\t\t\t\t...(nodeConversational\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tconversational:\n\t\t\t\t\t\t\t\t\t\ttypeof nodeConversational === \"string\"\n\t\t\t\t\t\t\t\t\t\t\t? nodeConversational\n\t\t\t\t\t\t\t\t\t\t\t: true,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t},\n\t\t\t\t\tdata: result.data,\n\t\t\t\t\twidgetMeta: buildToolMeta({\n\t\t\t\t\t\topenaiTemplateUri: resource.openaiUri,\n\t\t\t\t\t\tmcpTemplateUri: resource.mcpUri,\n\t\t\t\t\t\tinvoking: \"Loading...\",\n\t\t\t\t\t\tinvoked: \"Loaded\",\n\t\t\t\t\t\tautoHeight: resource.autoHeight,\n\t\t\t\t\t}),\n\t\t\t\t\tflowMeta: {\n\t\t\t\t\t\tstep: currentNode,\n\t\t\t\t\t\tstate,\n\t\t\t\t\t\tfield: widgetField,\n\t\t\t\t\t\twidgetId: resource.id,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Action node — merge state and auto-advance\n\t\t\tstate = { ...state, ...result } as TState;\n\n\t\t\tconst edge = edges.get(currentNode);\n\t\t\tif (!edge) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\treturn {\n\t\t\t\tpayload: { status: \"error\", error: message },\n\t\t\t\tflowMeta: { step: currentNode, state },\n\t\t\t};\n\t\t}\n\t}\n\n\treturn {\n\t\tpayload: {\n\t\t\tstatus: \"error\",\n\t\t\terror: \"Flow exceeded maximum iterations (possible infinite loop)\",\n\t\t},\n\t};\n}\n\n// ============================================================================\n// Compile\n// ============================================================================\n\nconst inputSchema = {\n\taction: z\n\t\t.enum([\"start\", \"continue\"])\n\t\t.describe(\n\t\t\t'\"start\" to begin the flow, \"continue\" to resume after a pause (interrupt or widget)',\n\t\t),\n\tstateUpdates: z\n\t\t.record(z.string(), z.unknown())\n\t\t.optional()\n\t\t.describe(\n\t\t\t\"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.\",\n\t\t),\n\tflowToken: z\n\t\t.string()\n\t\t.optional()\n\t\t.describe(\n\t\t\t\"Opaque flow token from the previous response. Pass back exactly as received.\",\n\t\t),\n};\n\nexport function compileFlow<TState extends Record<string, unknown>>(\n\tinput: CompileInput<TState>,\n): RegisteredFlow {\n\tconst { config, nodes, nodeConfigs, edges } = input;\n\tconst protocol = buildFlowProtocol(config);\n\tconst fullDescription = `${config.description}\\n${protocol}`;\n\tasync function handleToolCall(\n\t\targs: FlowToolInput,\n\t\tmeta?: Record<string, unknown>,\n\t): Promise<ExecutionResult> {\n\t\tconst inputMeta = getInputMeta(args);\n\t\tconst state = inputMeta.state as TState;\n\n\t\tif (args.action === \"start\") {\n\t\t\tconst startEdge = edges.get(START);\n\t\t\tif (!startEdge) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror: \"No start edge\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Merge pre-filled answers and any stateUpdates\n\t\t\tconst startState = {\n\t\t\t\t...state,\n\t\t\t\t...(args.stateUpdates ?? {}),\n\t\t\t} as TState;\n\n\t\t\tconst firstNode = await resolveNextNode(startEdge, startState);\n\t\t\treturn executeFrom(\n\t\t\t\tfirstNode,\n\t\t\t\tstartState,\n\t\t\t\tnodes,\n\t\t\t\tnodeConfigs,\n\t\t\t\tedges,\n\t\t\t\tmeta,\n\t\t\t);\n\t\t}\n\n\t\tif (args.action === \"continue\") {\n\t\t\tconst step = inputMeta.step;\n\t\t\tif (!step) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t'Missing or invalid \"flowToken\" for continue action.' +\n\t\t\t\t\t\t\t\" Pass back the flowToken from the previous response exactly as received.\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst updatedState = {\n\t\t\t\t...state,\n\t\t\t\t...(args.stateUpdates ?? {}),\n\t\t\t} as TState;\n\n\t\t\t// If cached interrupt questions exist, check for unanswered questions\n\t\t\t// without re-executing the node handler (avoids side-effect replay).\n\t\t\tif (inputMeta.questions) {\n\t\t\t\tconst interruptResult = buildInterruptResult(\n\t\t\t\t\tinputMeta.questions,\n\t\t\t\t\tinputMeta.interruptContext,\n\t\t\t\t\tstep,\n\t\t\t\t\tupdatedState,\n\t\t\t\t\tnodeConfigs.get(step)?.conversational,\n\t\t\t\t);\n\t\t\t\tif (interruptResult) return interruptResult;\n\t\t\t\t// All questions answered — fall through to advance\n\t\t\t}\n\n\t\t\t// Advance to next node when: all cached questions are answered, or\n\t\t\t// this is a widget continue (widgets never have cached questions and\n\t\t\t// re-executing the handler would cause a stuck loop for field-less widgets).\n\t\t\t// Otherwise the AI may have dropped _meta.flow.questions — re-execute\n\t\t\t// from the current step so the handler can re-check unanswered questions.\n\t\t\tif (inputMeta.questions || inputMeta.widgetId) {\n\t\t\t\tconst edge = edges.get(step);\n\t\t\t\tif (!edge) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\terror: `No edge from step \"${step}\"`,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tconst nextNode = await resolveNextNode(edge, updatedState);\n\t\t\t\treturn executeFrom(\n\t\t\t\t\tnextNode,\n\t\t\t\t\tupdatedState,\n\t\t\t\t\tnodes,\n\t\t\t\t\tnodeConfigs,\n\t\t\t\t\tedges,\n\t\t\t\t\tmeta,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn executeFrom(step, updatedState, nodes, nodeConfigs, edges, meta);\n\t\t}\n\n\t\treturn {\n\t\t\tpayload: {\n\t\t\t\tstatus: \"error\",\n\t\t\t\terror: `Unknown action: \"${args.action}\"`,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn {\n\t\tid: config.id,\n\t\ttitle: config.title,\n\t\tdescription: fullDescription,\n\n\t\tasync register(server: McpServer): Promise<void> {\n\t\t\tserver.registerTool(\n\t\t\t\tconfig.id,\n\t\t\t\t{\n\t\t\t\t\ttitle: config.title,\n\t\t\t\t\tdescription: fullDescription,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tannotations: config.annotations,\n\t\t\t\t},\n\t\t\t\t(async (args: FlowToolInput, extra: unknown) => {\n\t\t\t\t\tconst requestExtra = extra as RequestHandlerExtra<\n\t\t\t\t\t\tServerRequest,\n\t\t\t\t\t\tServerNotification\n\t\t\t\t\t>;\n\t\t\t\t\tconst _meta: Record<string, unknown> = requestExtra._meta ?? {};\n\n\t\t\t\t\tconst result = await handleToolCall(args, _meta);\n\n\t\t\t\t\t// Encode flow state as opaque token for the model to pass back\n\t\t\t\t\tconst flowToken = result.flowMeta\n\t\t\t\t\t\t? encodeFlowToken({\n\t\t\t\t\t\t\t\tstep: result.flowMeta.step ?? \"\",\n\t\t\t\t\t\t\t\tstate: result.flowMeta.state,\n\t\t\t\t\t\t\t\tfield: result.flowMeta.field,\n\t\t\t\t\t\t\t\twidgetId: result.flowMeta.widgetId,\n\t\t\t\t\t\t\t\tquestions: result.flowMeta.questions,\n\t\t\t\t\t\t\t\tinterruptContext: result.flowMeta.interruptContext,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\t// Text content includes the payload + flowToken for the model\n\t\t\t\t\t// Also includes flowId and widgetId so widget iframes can use them directly\n\t\t\t\t\tconst textPayload = {\n\t\t\t\t\t\t...result.payload,\n\t\t\t\t\t\t...(flowToken\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tflowToken,\n\t\t\t\t\t\t\t\t\tflowId: config.id,\n\t\t\t\t\t\t\t\t\t...(result.flowMeta?.widgetId\n\t\t\t\t\t\t\t\t\t\t? { widgetId: result.flowMeta.widgetId }\n\t\t\t\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t};\n\t\t\t\t\tconst content = [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\ttext: JSON.stringify(textPayload, null, 2),\n\t\t\t\t\t\t},\n\t\t\t\t\t];\n\n\t\t\t\t\t// _meta carries widget URIs and tracking config for the host/iframe\n\t\t\t\t\tconst responseMeta = {\n\t\t\t\t\t\t...(result.widgetMeta ?? {}),\n\t\t\t\t\t\t..._meta,\n\t\t\t\t\t};\n\n\t\t\t\t\t// Widget response — include structuredContent + widget metadata\n\t\t\t\t\tif (result.widgetMeta) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t\tstructuredContent: result.data,\n\t\t\t\t\t\t\t_meta: responseMeta,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Non-widget response (interrupt, complete, error)\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t...(Object.keys(responseMeta).length > 0\n\t\t\t\t\t\t\t? { _meta: responseMeta }\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t};\n\t\t\t\t}) as unknown as ToolCallback<typeof inputSchema>,\n\t\t\t);\n\t\t},\n\t};\n}\n","import type { WidgetCSP } from \"./types\";\n\n/**\n * MIME types for widget resources.\n * OpenAI Apps SDK uses \"text/html+skybridge\"\n * MCP Apps uses \"text/html;profile=mcp-app\"\n */\nexport const MIME_TYPE_OPENAI = \"text/html+skybridge\";\nexport const MIME_TYPE_MCP = \"text/html;profile=mcp-app\";\n\n// ---- HTML fetching ----\n\nexport const fetchHtml = async (\n\tbaseUrl: string,\n\tpath: string,\n): Promise<string> => {\n\tconst normalizedBase = baseUrl.endsWith(\"/\") ? baseUrl.slice(0, -1) : baseUrl;\n\tconst result = await fetch(`${normalizedBase}${path}`);\n\treturn await result.text();\n};\n\n// ---- OpenAI resource metadata ----\n\ninterface OpenAIResourceMeta {\n\t[key: string]: unknown;\n\t\"openai/widgetDescription\"?: string;\n\t\"openai/widgetPrefersBorder\"?: boolean;\n\t\"openai/widgetDomain\"?: string;\n\t\"openai/widgetCSP\"?: WidgetCSP;\n}\n\nexport function buildOpenAIResourceMeta(config: {\n\tdescription?: string;\n\tprefersBorder?: boolean;\n\twidgetDomain: string;\n\twidgetCSP?: WidgetCSP;\n}): OpenAIResourceMeta {\n\treturn {\n\t\t\"openai/widgetDescription\": config.description,\n\t\t\"openai/widgetPrefersBorder\": config.prefersBorder,\n\t\t\"openai/widgetDomain\": config.widgetDomain,\n\t\t...(config.widgetCSP && { \"openai/widgetCSP\": config.widgetCSP }),\n\t};\n}\n\n// ---- MCP Apps resource metadata ----\n\ninterface McpAppsResourceMeta {\n\t[key: string]: unknown;\n\tui?: {\n\t\tcsp?: {\n\t\t\tconnectDomains?: string[];\n\t\t\tresourceDomains?: string[];\n\t\t\tframeDomains?: string[];\n\t\t\tredirectDomains?: string[];\n\t\t};\n\t\tdomain?: string;\n\t\tprefersBorder?: boolean;\n\t};\n}\n\nexport function buildMcpAppsResourceMeta(config: {\n\tdescription?: string;\n\tprefersBorder?: boolean;\n\twidgetDomain?: string;\n\twidgetCSP?: WidgetCSP;\n}): McpAppsResourceMeta {\n\tconst csp = config.widgetCSP\n\t\t? {\n\t\t\t\tconnectDomains: config.widgetCSP.connect_domains,\n\t\t\t\tresourceDomains: config.widgetCSP.resource_domains,\n\t\t\t\tframeDomains: config.widgetCSP.frame_domains,\n\t\t\t\tredirectDomains: config.widgetCSP.redirect_domains,\n\t\t\t}\n\t\t: undefined;\n\n\treturn {\n\t\tui: {\n\t\t\t...(csp && { csp }),\n\t\t\t...(config.widgetDomain && { domain: config.widgetDomain }),\n\t\t\t...(config.prefersBorder !== undefined && {\n\t\t\t\tprefersBorder: config.prefersBorder,\n\t\t\t}),\n\t\t},\n\t};\n}\n\n// ---- Tool metadata (references resource URIs) ----\n\nexport function buildToolMeta(config: {\n\topenaiTemplateUri?: string;\n\tmcpTemplateUri?: string;\n\tinvoking: string;\n\tinvoked: string;\n\tautoHeight?: boolean;\n}) {\n\treturn {\n\t\t// OpenAI metadata\n\t\t...(config.openaiTemplateUri && {\n\t\t\t\"openai/outputTemplate\": config.openaiTemplateUri,\n\t\t}),\n\t\t\"openai/toolInvocation/invoking\": config.invoking,\n\t\t\"openai/toolInvocation/invoked\": config.invoked,\n\t\t\"openai/widgetAccessible\": true,\n\t\t\"openai/resultCanProduceWidget\": true,\n\t\t// MCP Apps metadata\n\t\t...(config.mcpTemplateUri && {\n\t\t\tui: {\n\t\t\t\tresourceUri: config.mcpTemplateUri,\n\t\t\t\t...(config.autoHeight && { autoHeight: true }),\n\t\t\t},\n\t\t}),\n\t};\n}\n","/**\n * Opaque compressed token for flow state round-tripping.\n *\n * Flow state is compressed (zlib deflate) then base64-encoded. This produces\n * a token that:\n * 1. Looks like random noise — models can't mentally decode it\n * 2. Is smaller than raw base64 (~30% compression on typical state)\n * 3. The model treats it as an opaque string and passes it back unchanged\n *\n * Only encoded/decoded on the server (Node.js). Browser code should never\n * need to decode flow tokens.\n */\n\nimport { deflateSync, inflateSync } from \"node:zlib\";\n\nexport type FlowTokenData = {\n\tstep: string;\n\tstate: Record<string, unknown>;\n\tfield?: string;\n\twidgetId?: string;\n\tquestions?: Array<{\n\t\tquestion: string;\n\t\tfield: string;\n\t\tsuggestions?: string[];\n\t\tcontext?: string;\n\t}>;\n\tinterruptContext?: string;\n};\n\nexport function encodeFlowToken(data: FlowTokenData): string {\n\tconst json = JSON.stringify(data);\n\tconst compressed = deflateSync(json);\n\treturn compressed.toString(\"base64\");\n}\n\nexport function decodeFlowToken(token: string): FlowTokenData | null {\n\ttry {\n\t\tconst compressed = Buffer.from(token, \"base64\");\n\t\tconst json = inflateSync(compressed).toString(\"utf-8\");\n\t\treturn JSON.parse(json) as FlowTokenData;\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import type {\n\tConditionFn,\n\tEdge,\n\tFlowConfig,\n\tNodeConfig,\n\tNodeHandler,\n\tRegisteredFlow,\n} from \"./@types\";\nimport { END, START } from \"./@types\";\nimport { compileFlow } from \"./compile\";\n\n/**\n * A LangGraph-inspired state graph builder for MCP tools.\n *\n * @example\n * ```ts\n * const flow = new StateGraph<MyState>({\n * id: \"onboarding\",\n * title: \"User Onboarding\",\n * description: \"Guides users through onboarding\",\n * })\n * .addNode(\"ask_name\", (state) => interrupt({ question: \"What's your name?\", field: \"name\" }))\n * .addNode(\"greet\", (state) => ({ greeting: `Hello ${state.name}!` }))\n * .addEdge(START, \"ask_name\")\n * .addEdge(\"ask_name\", \"greet\")\n * .addEdge(\"greet\", END)\n * .compile();\n * ```\n */\nexport class StateGraph<TState extends Record<string, unknown>> {\n\tprivate nodes = new Map<string, NodeHandler<TState>>();\n\tprivate nodeConfigs = new Map<string, NodeConfig<TState>>();\n\tprivate edges = new Map<string, Edge<TState>>();\n\tprivate config: FlowConfig;\n\n\tconstructor(config: FlowConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Add a node with just a handler.\n\t */\n\taddNode(name: string, handler: NodeHandler<TState>): this;\n\t/**\n\t * Add a node with config and a handler.\n\t * Pass `field` to enable auto-skip on widget nodes.\n\t */\n\taddNode(\n\t\tname: string,\n\t\tconfig: NodeConfig<TState>,\n\t\thandler: NodeHandler<TState>,\n\t): this;\n\taddNode(\n\t\tname: string,\n\t\tconfigOrHandler: NodeConfig<TState> | NodeHandler<TState>,\n\t\tmaybeHandler?: NodeHandler<TState>,\n\t): this {\n\t\tif (name === START || name === END) {\n\t\t\tthrow new Error(\n\t\t\t\t`\"${name}\" is a reserved name and cannot be used as a node name`,\n\t\t\t);\n\t\t}\n\t\tif (this.nodes.has(name)) {\n\t\t\tthrow new Error(`Node \"${name}\" already exists`);\n\t\t}\n\n\t\tlet handler: NodeHandler<TState>;\n\t\tlet nodeConfig: NodeConfig<TState> = {};\n\n\t\tif (typeof configOrHandler === \"function\") {\n\t\t\t// addNode(name, handler)\n\t\t\thandler = configOrHandler;\n\t\t} else if (maybeHandler) {\n\t\t\t// addNode(name, nodeConfig, handler)\n\t\t\thandler = maybeHandler;\n\t\t\tnodeConfig = configOrHandler as NodeConfig<TState>;\n\t\t} else {\n\t\t\tthrow new Error(`Node \"${name}\" requires a handler function.`);\n\t\t}\n\n\t\tthis.nodes.set(name, handler);\n\t\tthis.nodeConfigs.set(name, nodeConfig);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add a direct edge between two nodes.\n\t *\n\t * Use `START` as `from` to set the entry point.\n\t * Use `END` as `to` to mark a terminal node.\n\t */\n\taddEdge(from: string, to: string): this {\n\t\tif (this.edges.has(from)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Node \"${from}\" already has an outgoing edge. Use addConditionalEdge for branching.`,\n\t\t\t);\n\t\t}\n\t\tthis.edges.set(from, { type: \"direct\", to });\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add a conditional edge from a node.\n\t *\n\t * The condition function receives current state and returns the name of the next node.\n\t */\n\taddConditionalEdge(from: string, condition: ConditionFn<TState>): this {\n\t\tif (this.edges.has(from)) {\n\t\t\tthrow new Error(`Node \"${from}\" already has an outgoing edge.`);\n\t\t}\n\t\tthis.edges.set(from, { type: \"conditional\", condition });\n\t\treturn this;\n\t}\n\n\t/**\n\t * Compile the graph into a RegisteredFlow that can be registered on an McpServer.\n\t *\n\t * Validates the graph structure and returns a registration-compatible object.\n\t */\n\tcompile(): RegisteredFlow {\n\t\tthis.validate();\n\n\t\treturn compileFlow<TState>({\n\t\t\tconfig: this.config,\n\t\t\tnodes: new Map(this.nodes),\n\t\t\tnodeConfigs: new Map(this.nodeConfigs),\n\t\t\tedges: new Map(this.edges),\n\t\t});\n\t}\n\n\tprivate validate(): void {\n\t\t// Must have a START edge\n\t\tif (!this.edges.has(START)) {\n\t\t\tthrow new Error(\n\t\t\t\t'Flow must have an entry point. Add an edge from START: .addEdge(START, \"first_node\")',\n\t\t\t);\n\t\t}\n\n\t\t// START edge target must exist\n\t\tconst startEdge = this.edges.get(START);\n\t\tif (\n\t\t\tstartEdge?.type === \"direct\" &&\n\t\t\tstartEdge.to !== END &&\n\t\t\t!this.nodes.has(startEdge.to)\n\t\t) {\n\t\t\tthrow new Error(\n\t\t\t\t`START edge references non-existent node: \"${startEdge.to}\"`,\n\t\t\t);\n\t\t}\n\n\t\t// All static edge targets must reference existing nodes (or END)\n\t\tfor (const [from, edge] of this.edges) {\n\t\t\tif (from !== START && !this.nodes.has(from)) {\n\t\t\t\tthrow new Error(`Edge from non-existent node: \"${from}\"`);\n\t\t\t}\n\t\t\tif (\n\t\t\t\tedge.type === \"direct\" &&\n\t\t\t\tedge.to !== END &&\n\t\t\t\t!this.nodes.has(edge.to)\n\t\t\t) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Edge from \"${from}\" references non-existent node: \"${edge.to}\"`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// Every node must have an outgoing edge\n\t\tfor (const [name] of this.nodes) {\n\t\t\tif (!this.edges.has(name)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Node \"${name}\" has no outgoing edge. Add one with .addEdge(\"${name}\", ...) or .addConditionalEdge(\"${name}\", ...)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n}\n","import type { z } from \"zod\";\nimport type { FlowConfig, InferFlowState } from \"./@types\";\nimport { StateGraph } from \"./state-graph\";\n\n/**\n * Create a new flow graph — convenience factory for `new StateGraph()`.\n *\n * The state type is automatically inferred from the `state` definition —\n * no explicit generic parameter needed.\n *\n * @example\n * ```ts\n * import { createFlow, interrupt, START, END } from \"@waniwani/sdk/mcp\";\n * import { z } from \"zod\";\n *\n * const flow = createFlow({\n * id: \"onboarding\",\n * title: \"User Onboarding\",\n * description: \"Guides users through onboarding. Use when a user wants to get started.\",\n * state: {\n * name: z.string().describe(\"The user's name\"),\n * email: z.string().describe(\"The user's email address\"),\n * },\n * })\n * .addNode(\"ask_name\", () => interrupt({ question: \"What's your name?\", field: \"name\" }))\n * .addNode(\"ask_email\", () => interrupt({ question: \"What's your email?\", field: \"email\" }))\n * .addEdge(START, \"ask_name\")\n * .addEdge(\"ask_name\", \"ask_email\")\n * .addEdge(\"ask_email\", END)\n * .compile();\n * ```\n */\nexport function createFlow<const TSchema extends Record<string, z.ZodType>>(\n\tconfig: Omit<FlowConfig, \"state\"> & { state: TSchema },\n): StateGraph<InferFlowState<TSchema>> {\n\treturn new StateGraph<InferFlowState<TSchema>>(config);\n}\n","import {\n\tbuildMcpAppsResourceMeta,\n\tbuildOpenAIResourceMeta,\n\tfetchHtml,\n\tMIME_TYPE_MCP,\n\tMIME_TYPE_OPENAI,\n} from \"./meta\";\nimport type { McpServer, RegisteredResource, ResourceConfig } from \"./types\";\n\n/**\n * Creates a reusable UI resource (HTML template) that can be attached\n * to tools or flow nodes.\n *\n * @example\n * ```ts\n * const pricingUI = createResource({\n * id: \"pricing_table\",\n * title: \"Pricing Table\",\n * baseUrl: \"https://my-app.com\",\n * htmlPath: \"/widgets/pricing\",\n * widgetDomain: \"my-app.com\",\n * });\n *\n * await pricingUI.register(server);\n * ```\n */\nexport function createResource(config: ResourceConfig): RegisteredResource {\n\tconst {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\t\tbaseUrl,\n\t\thtmlPath,\n\t\twidgetDomain,\n\t\tprefersBorder = true,\n\t\tautoHeight = true,\n\t} = config;\n\n\t// Auto-generate CSP from baseUrl if not explicitly provided\n\tlet widgetCSP = config.widgetCSP ?? {\n\t\tconnect_domains: [baseUrl],\n\t\tresource_domains: [baseUrl],\n\t};\n\n\t// In development with localhost, add extra CSP domains for\n\t// Next.js dev features (WebSocket HMR, Turbopack font serving)\n\tif (process.env.NODE_ENV === \"development\") {\n\t\ttry {\n\t\t\tconst { hostname } = new URL(baseUrl);\n\t\t\tif (hostname === \"localhost\" || hostname === \"127.0.0.1\") {\n\t\t\t\twidgetCSP = {\n\t\t\t\t\t...widgetCSP,\n\t\t\t\t\tconnect_domains: [\n\t\t\t\t\t\t...(widgetCSP.connect_domains || []),\n\t\t\t\t\t\t`ws://${hostname}:*`,\n\t\t\t\t\t\t`wss://${hostname}:*`,\n\t\t\t\t\t],\n\t\t\t\t\tresource_domains: [\n\t\t\t\t\t\t...(widgetCSP.resource_domains || []),\n\t\t\t\t\t\t`http://${hostname}:*`,\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t} catch {\n\t\t\t// Invalid baseUrl — skip dev CSP additions\n\t\t}\n\t}\n\n\tconst openaiUri = `ui://widgets/apps-sdk/${id}.html`;\n\tconst mcpUri = `ui://widgets/ext-apps/${id}.html`;\n\n\t// Lazy HTML — fetched once, shared across all calls\n\tlet htmlPromise: Promise<string> | null = null;\n\tconst getHtml = () => {\n\t\tif (!htmlPromise) htmlPromise = fetchHtml(baseUrl, htmlPath);\n\t\treturn htmlPromise;\n\t};\n\n\t// Use description for UI metadata\n\tconst uiDescription = description;\n\n\tasync function register(server: McpServer): Promise<void> {\n\t\tconst html = await getHtml();\n\n\t\t// Register OpenAI Apps SDK resource\n\t\tserver.registerResource(\n\t\t\t`${id}-openai-widget`,\n\t\t\topenaiUri,\n\t\t\t{\n\t\t\t\ttitle,\n\t\t\t\tdescription: uiDescription,\n\t\t\t\tmimeType: MIME_TYPE_OPENAI,\n\t\t\t\t_meta: {\n\t\t\t\t\t\"openai/widgetDescription\": uiDescription,\n\t\t\t\t\t\"openai/widgetPrefersBorder\": prefersBorder,\n\t\t\t\t},\n\t\t\t},\n\t\t\tasync (uri) => ({\n\t\t\t\tcontents: [\n\t\t\t\t\t{\n\t\t\t\t\t\turi: uri.href,\n\t\t\t\t\t\tmimeType: MIME_TYPE_OPENAI,\n\t\t\t\t\t\ttext: html,\n\t\t\t\t\t\t_meta: buildOpenAIResourceMeta({\n\t\t\t\t\t\t\tdescription: uiDescription,\n\t\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t\t\twidgetDomain,\n\t\t\t\t\t\t\twidgetCSP,\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}),\n\t\t);\n\n\t\t// Register MCP Apps resource\n\t\tserver.registerResource(\n\t\t\t`${id}-mcp-widget`,\n\t\t\tmcpUri,\n\t\t\t{\n\t\t\t\ttitle,\n\t\t\t\tdescription: uiDescription,\n\t\t\t\tmimeType: MIME_TYPE_MCP,\n\t\t\t\t_meta: {\n\t\t\t\t\tui: {\n\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tasync (uri) => ({\n\t\t\t\tcontents: [\n\t\t\t\t\t{\n\t\t\t\t\t\turi: uri.href,\n\t\t\t\t\t\tmimeType: MIME_TYPE_MCP,\n\t\t\t\t\t\ttext: html,\n\t\t\t\t\t\t_meta: buildMcpAppsResourceMeta({\n\t\t\t\t\t\t\tdescription: uiDescription,\n\t\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t\t\twidgetCSP,\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}),\n\t\t);\n\t}\n\n\treturn {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\t\topenaiUri,\n\t\tmcpUri,\n\t\tautoHeight,\n\t\tregister,\n\t};\n}\n","import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport type { ShapeOutput } from \"@modelcontextprotocol/sdk/server/zod-compat.js\";\nimport type { RequestHandlerExtra } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport type {\n\tServerNotification,\n\tServerRequest,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport type { z } from \"zod\";\nimport { buildToolMeta } from \"../resources/meta\";\nimport type {\n\tMcpServer,\n\tRegisteredTool,\n\tToolConfig,\n\tToolHandler,\n} from \"./types\";\n\n/**\n * Creates an MCP tool with minimal boilerplate.\n *\n * When `config.resource` is provided, the tool returns `structuredContent` + widget metadata.\n * Without a resource, the tool returns plain text content.\n *\n * @example\n * ```ts\n * // Widget tool (with resource)\n * const pricingTool = createTool({\n * resource: pricingUI,\n * description: \"Show pricing comparison\",\n * inputSchema: { postalCode: z.string() },\n * }, async ({ postalCode }) => ({\n * text: \"Pricing loaded\",\n * data: { postalCode, prices: [] },\n * }));\n *\n * // Plain tool (no resource)\n * const searchTool = createTool({\n * id: \"search\",\n * title: \"Search\",\n * description: \"Search the knowledge base\",\n * inputSchema: { query: z.string() },\n * }, async ({ query }) => ({\n * text: `Results for \"${query}\"`,\n * }));\n * ```\n */\nexport function createTool<TInput extends z.ZodRawShape>(\n\tconfig: ToolConfig<TInput>,\n\thandler: ToolHandler<TInput>,\n): RegisteredTool {\n\tconst { resource, description, inputSchema, annotations } = config;\n\n\tconst id = config.id ?? resource?.id;\n\tconst title = config.title ?? resource?.title;\n\n\tif (!id) {\n\t\tthrow new Error(\n\t\t\t\"createTool: `id` is required when no resource is provided\",\n\t\t);\n\t}\n\tif (!title) {\n\t\tthrow new Error(\n\t\t\t\"createTool: `title` is required when no resource is provided\",\n\t\t);\n\t}\n\n\t// Build widget metadata only when resource is present\n\tconst toolMeta = resource\n\t\t? buildToolMeta({\n\t\t\t\topenaiTemplateUri: resource.openaiUri,\n\t\t\t\tmcpTemplateUri: resource.mcpUri,\n\t\t\t\tinvoking: config.invoking ?? \"Loading...\",\n\t\t\t\tinvoked: config.invoked ?? \"Loaded\",\n\t\t\t\tautoHeight: resource.autoHeight,\n\t\t\t})\n\t\t: undefined;\n\n\treturn {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\n\t\tasync register(server: McpServer): Promise<void> {\n\t\t\tserver.registerTool(\n\t\t\t\tid,\n\t\t\t\t{\n\t\t\t\t\ttitle,\n\t\t\t\t\tdescription,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tannotations,\n\t\t\t\t\t...(toolMeta && { _meta: toolMeta }),\n\t\t\t\t},\n\t\t\t\t(async (args: ShapeOutput<TInput>, extra: unknown) => {\n\t\t\t\t\tconst requestExtra = extra as RequestHandlerExtra<\n\t\t\t\t\t\tServerRequest,\n\t\t\t\t\t\tServerNotification\n\t\t\t\t\t>;\n\t\t\t\t\tconst _meta: Record<string, unknown> = requestExtra._meta ?? {};\n\n\t\t\t\t\tconst result = await handler(args, { extra: { _meta } });\n\n\t\t\t\t\t// Widget tool: return structuredContent + widget metadata\n\t\t\t\t\tif (resource && result.data) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: result.text }],\n\t\t\t\t\t\t\tstructuredContent: result.data,\n\t\t\t\t\t\t\t_meta: {\n\t\t\t\t\t\t\t\t...toolMeta,\n\t\t\t\t\t\t\t\t..._meta,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Plain tool: return text content only\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: result.text }],\n\t\t\t\t\t};\n\t\t\t\t}) as unknown as ToolCallback<TInput>,\n\t\t\t);\n\t\t},\n\t};\n}\n\n/**\n * Registers multiple tools on the server\n */\nexport async function registerTools(\n\tserver: McpServer,\n\ttools: RegisteredTool[],\n): Promise<void> {\n\tawait Promise.all(tools.map((t) => t.register(server)));\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// KB Client — thin HTTP wrapper for knowledge base API\n\nimport { WaniWaniError } from \"../error.js\";\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tKbClient,\n\tKbIngestFile,\n\tKbIngestResult,\n\tKbSearchOptions,\n\tKbSource,\n\tSearchResult,\n} from \"./types.js\";\n\nconst SDK_NAME = \"@waniwani/sdk\";\n\nexport function createKbClient(config: InternalConfig): KbClient {\n\tconst { baseUrl, apiKey } = config;\n\n\tfunction requireApiKey(): string {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(\"WANIWANI_API_KEY is not set\");\n\t\t}\n\t\treturn apiKey;\n\t}\n\n\tasync function request<T>(\n\t\tmethod: \"GET\" | \"POST\",\n\t\tpath: string,\n\t\tbody?: unknown,\n\t): Promise<T> {\n\t\tconst key = requireApiKey();\n\t\tconst url = `${baseUrl.replace(/\\/$/, \"\")}${path}`;\n\n\t\tconst headers: Record<string, string> = {\n\t\t\tAuthorization: `Bearer ${key}`,\n\t\t\t\"X-WaniWani-SDK\": SDK_NAME,\n\t\t};\n\n\t\tconst init: RequestInit = { method, headers };\n\n\t\tif (body !== undefined) {\n\t\t\theaders[\"Content-Type\"] = \"application/json\";\n\t\t\tinit.body = JSON.stringify(body);\n\t\t}\n\n\t\tconst response = await fetch(url, init);\n\n\t\tif (!response.ok) {\n\t\t\tconst text = await response.text().catch(() => \"\");\n\t\t\tthrow new WaniWaniError(\n\t\t\t\ttext || `KB API error: HTTP ${response.status}`,\n\t\t\t\tresponse.status,\n\t\t\t);\n\t\t}\n\n\t\tconst json = (await response.json()) as { data: T };\n\t\treturn json.data;\n\t}\n\n\treturn {\n\t\tasync ingest(files: KbIngestFile[]): Promise<KbIngestResult> {\n\t\t\treturn request<KbIngestResult>(\"POST\", \"/api/mcp/kb/ingest\", {\n\t\t\t\tfiles,\n\t\t\t});\n\t\t},\n\n\t\tasync search(\n\t\t\tquery: string,\n\t\t\toptions?: KbSearchOptions,\n\t\t): Promise<SearchResult[]> {\n\t\t\treturn request<SearchResult[]>(\"POST\", \"/api/mcp/kb/search\", {\n\t\t\t\tquery,\n\t\t\t\t...options,\n\t\t\t});\n\t\t},\n\n\t\tasync sources(): Promise<KbSource[]> {\n\t\t\treturn request<KbSource[]>(\"GET\", \"/api/mcp/kb/sources\");\n\t\t},\n\t};\n}\n","import type { EventType, LegacyTrackEvent, TrackInput } from \"./@types.js\";\nimport type { V2CorrelationIds, V2EventEnvelope } from \"./v2-types.js\";\n\nconst DEFAULT_SOURCE = \"@waniwani/sdk\";\n\nexport interface MapTrackEventOptions {\n\tnow?: () => Date;\n\tgenerateId?: () => string;\n\tsource?: string;\n}\n\nexport function mapTrackEventToV2(\n\tinput: TrackInput,\n\toptions: MapTrackEventOptions = {},\n): V2EventEnvelope {\n\tconst now = options.now ?? (() => new Date());\n\tconst generateId = options.generateId ?? createEventId;\n\tconst eventName = resolveEventName(input);\n\tconst meta = toRecord(input.meta);\n\tconst metadata = toRecord(input.metadata);\n\tconst correlation = resolveCorrelationIds(input, meta);\n\tconst eventId = takeNonEmptyString(input.eventId) ?? generateId();\n\tconst timestamp = normalizeTimestamp(input.timestamp, now);\n\tconst source =\n\t\ttakeNonEmptyString(input.source) ?? options.source ?? DEFAULT_SOURCE;\n\tconst rawLegacy = isLegacyTrackEvent(input) ? { ...input } : undefined;\n\n\tconst mappedMetadata: Record<string, unknown> = {\n\t\t...metadata,\n\t};\n\tif (Object.keys(meta).length > 0) mappedMetadata.meta = meta;\n\tif (rawLegacy) mappedMetadata.rawLegacy = rawLegacy;\n\n\treturn {\n\t\tid: eventId,\n\t\ttype: \"mcp.event\",\n\t\tname: eventName,\n\t\tsource,\n\t\ttimestamp,\n\t\tcorrelation,\n\t\tproperties: mapProperties(input, eventName),\n\t\tmetadata: mappedMetadata,\n\t\trawLegacy,\n\t};\n}\n\nexport function createEventId(): string {\n\tif (\n\t\ttypeof crypto !== \"undefined\" &&\n\t\ttypeof crypto.randomUUID === \"function\"\n\t) {\n\t\treturn `evt_${crypto.randomUUID()}`;\n\t}\n\n\treturn `evt_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;\n}\n\nfunction mapProperties(\n\tinput: TrackInput,\n\teventName: EventType,\n): Record<string, unknown> {\n\tif (!isLegacyTrackEvent(input)) {\n\t\treturn toRecord(input.properties);\n\t}\n\n\tconst legacyProperties = mapLegacyProperties(input, eventName);\n\tconst explicitProperties = toRecord(input.properties);\n\treturn {\n\t\t...legacyProperties,\n\t\t...explicitProperties,\n\t};\n}\n\nfunction mapLegacyProperties(\n\tinput: LegacyTrackEvent,\n\teventName: EventType,\n): Record<string, unknown> {\n\tswitch (eventName) {\n\t\tcase \"tool.called\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (takeNonEmptyString(input.toolName)) properties.name = input.toolName;\n\t\t\tif (takeNonEmptyString(input.toolType)) properties.type = input.toolType;\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"quote.succeeded\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (typeof input.quoteAmount === \"number\") {\n\t\t\t\tproperties.amount = input.quoteAmount;\n\t\t\t}\n\t\t\tif (takeNonEmptyString(input.quoteCurrency)) {\n\t\t\t\tproperties.currency = input.quoteCurrency;\n\t\t\t}\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"link.clicked\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (takeNonEmptyString(input.linkUrl)) properties.url = input.linkUrl;\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"purchase.completed\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (typeof input.purchaseAmount === \"number\") {\n\t\t\t\tproperties.amount = input.purchaseAmount;\n\t\t\t}\n\t\t\tif (takeNonEmptyString(input.purchaseCurrency)) {\n\t\t\t\tproperties.currency = input.purchaseCurrency;\n\t\t\t}\n\t\t\treturn properties;\n\t\t}\n\t\tdefault:\n\t\t\treturn {};\n\t}\n}\n\nfunction resolveEventName(input: TrackInput): EventType {\n\tif (isLegacyTrackEvent(input)) return input.eventType;\n\treturn input.event;\n}\n\nfunction resolveCorrelationIds(\n\tinput: TrackInput,\n\tmeta: Record<string, unknown>,\n): V2CorrelationIds {\n\tconst requestId =\n\t\ttakeNonEmptyString(input.requestId) ??\n\t\tpickFirstString(meta, [\"openai/requestId\", \"requestId\", \"mcp/requestId\"]);\n\n\tconst sessionId =\n\t\ttakeNonEmptyString(input.sessionId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/sessionId\",\n\t\t\t\"sessionId\",\n\t\t\t\"conversationId\",\n\t\t\t\"anthropic/sessionId\",\n\t\t]);\n\n\tconst traceId =\n\t\ttakeNonEmptyString(input.traceId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/traceId\",\n\t\t\t\"traceId\",\n\t\t\t\"mcp/traceId\",\n\t\t\t\"openai/requestId\",\n\t\t\t\"requestId\",\n\t\t]);\n\n\tconst externalUserId =\n\t\ttakeNonEmptyString(input.externalUserId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/userId\",\n\t\t\t\"externalUserId\",\n\t\t\t\"userId\",\n\t\t\t\"actorId\",\n\t\t]);\n\n\tconst correlationId =\n\t\ttakeNonEmptyString(input.correlationId) ??\n\t\tpickFirstString(meta, [\"correlationId\", \"openai/requestId\"]) ??\n\t\trequestId;\n\n\tconst correlation: V2CorrelationIds = {};\n\tif (sessionId) correlation.sessionId = sessionId;\n\tif (traceId) correlation.traceId = traceId;\n\tif (requestId) correlation.requestId = requestId;\n\tif (correlationId) correlation.correlationId = correlationId;\n\tif (externalUserId) correlation.externalUserId = externalUserId;\n\treturn correlation;\n}\n\nfunction normalizeTimestamp(\n\tinput: string | Date | undefined,\n\tnow: () => Date,\n): string {\n\tif (input instanceof Date) return input.toISOString();\n\tif (typeof input === \"string\") {\n\t\tconst date = new Date(input);\n\t\tif (!Number.isNaN(date.getTime())) return date.toISOString();\n\t}\n\treturn now().toISOString();\n}\n\nfunction pickFirstString(\n\trecord: Record<string, unknown>,\n\tkeys: readonly string[],\n): string | undefined {\n\tfor (const key of keys) {\n\t\tconst value = record[key];\n\t\tif (typeof value === \"string\" && value.trim().length > 0) {\n\t\t\treturn value;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction toRecord(value: unknown): Record<string, unknown> {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) {\n\t\treturn {};\n\t}\n\treturn value as Record<string, unknown>;\n}\n\nfunction takeNonEmptyString(value: unknown): string | undefined {\n\tif (typeof value !== \"string\") return undefined;\n\tif (value.trim().length === 0) return undefined;\n\treturn value;\n}\n\nfunction isLegacyTrackEvent(input: TrackInput): input is LegacyTrackEvent {\n\treturn \"eventType\" in input;\n}\n","import type {\n\tTrackingShutdownOptions,\n\tTrackingShutdownResult,\n} from \"./@types.js\";\nimport type {\n\tV2BatchRejectedEvent,\n\tV2BatchRequest,\n\tV2BatchResponse,\n\tV2EventEnvelope,\n} from \"./v2-types.js\";\n\nconst DEFAULT_ENDPOINT_PATH = \"/api/mcp/events/v2/batch\";\nconst DEFAULT_FLUSH_INTERVAL_MS = 1_000;\nconst DEFAULT_MAX_BATCH_SIZE = 20;\nconst DEFAULT_MAX_BUFFER_SIZE = 1_000;\nconst DEFAULT_MAX_RETRIES = 3;\nconst DEFAULT_RETRY_BASE_DELAY_MS = 200;\nconst DEFAULT_RETRY_MAX_DELAY_MS = 2_000;\nconst DEFAULT_SHUTDOWN_TIMEOUT_MS = 2_000;\nconst SDK_NAME = \"@waniwani/sdk\";\n\nconst AUTH_FAILURE_STATUS = new Set([401, 403]);\nconst RETRYABLE_STATUS = new Set([408, 425, 429, 500, 502, 503, 504]);\n\ninterface Logger {\n\twarn: (message: string, ...args: unknown[]) => void;\n\terror: (message: string, ...args: unknown[]) => void;\n}\n\nexport interface V2TransportOptions {\n\tbaseUrl: string;\n\tapiKey: string;\n\tendpointPath?: string;\n\tflushIntervalMs?: number;\n\tmaxBatchSize?: number;\n\tmaxBufferSize?: number;\n\tmaxRetries?: number;\n\tretryBaseDelayMs?: number;\n\tretryMaxDelayMs?: number;\n\tshutdownTimeoutMs?: number;\n\tsdkVersion?: string;\n\tfetchFn?: typeof fetch;\n\tlogger?: Logger;\n\tnow?: () => Date;\n\tsleep?: (delayMs: number) => Promise<void>;\n}\n\nexport interface V2BatchTransport {\n\tenqueue: (event: V2EventEnvelope) => void;\n\tflush: () => Promise<void>;\n\tshutdown: (\n\t\toptions?: TrackingShutdownOptions,\n\t) => Promise<TrackingShutdownResult>;\n\tpendingEvents: () => number;\n}\n\ntype SendBatchResult =\n\t| { kind: \"success\" }\n\t| { kind: \"retryable\"; reason: string }\n\t| { kind: \"permanent\"; reason: string }\n\t| { kind: \"auth\"; status: number }\n\t| {\n\t\t\tkind: \"partial\";\n\t\t\tretryable: V2EventEnvelope[];\n\t\t\tpermanent: V2EventEnvelope[];\n\t };\n\nexport function createV2BatchTransport(\n\toptions: V2TransportOptions,\n): V2BatchTransport {\n\treturn new BatchingV2Transport(options);\n}\n\nclass BatchingV2Transport implements V2BatchTransport {\n\tprivate readonly endpointUrl: string;\n\tprivate readonly flushIntervalMs: number;\n\tprivate readonly maxBatchSize: number;\n\tprivate readonly maxBufferSize: number;\n\tprivate readonly maxRetries: number;\n\tprivate readonly retryBaseDelayMs: number;\n\tprivate readonly retryMaxDelayMs: number;\n\tprivate readonly shutdownTimeoutMs: number;\n\tprivate readonly sdkVersion?: string;\n\tprivate readonly fetchFn: typeof fetch;\n\tprivate readonly logger: Logger;\n\tprivate readonly now: () => Date;\n\tprivate readonly sleep: (delayMs: number) => Promise<void>;\n\tprivate readonly apiKey: string;\n\n\tprivate readonly buffer: V2EventEnvelope[] = [];\n\tprivate flushTimer: ReturnType<typeof setInterval> | undefined;\n\tprivate flushScheduled = false;\n\tprivate flushScheduledTimer: ReturnType<typeof setTimeout> | undefined;\n\tprivate flushInFlight: Promise<void> | undefined;\n\tprivate inFlightCount = 0;\n\tprivate isStopped = false;\n\tprivate isShuttingDown = false;\n\n\tconstructor(options: V2TransportOptions) {\n\t\tthis.endpointUrl = joinUrl(\n\t\t\toptions.baseUrl,\n\t\t\toptions.endpointPath ?? DEFAULT_ENDPOINT_PATH,\n\t\t);\n\t\tthis.flushIntervalMs = options.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;\n\t\tthis.maxBatchSize = options.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE;\n\t\tthis.maxBufferSize = options.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE;\n\t\tthis.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;\n\t\tthis.retryBaseDelayMs =\n\t\t\toptions.retryBaseDelayMs ?? DEFAULT_RETRY_BASE_DELAY_MS;\n\t\tthis.retryMaxDelayMs =\n\t\t\toptions.retryMaxDelayMs ?? DEFAULT_RETRY_MAX_DELAY_MS;\n\t\tthis.shutdownTimeoutMs =\n\t\t\toptions.shutdownTimeoutMs ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;\n\t\tthis.fetchFn = options.fetchFn ?? fetch;\n\t\tthis.logger = options.logger ?? console;\n\t\tthis.now = options.now ?? (() => new Date());\n\t\tthis.sleep =\n\t\t\toptions.sleep ??\n\t\t\t((delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs)));\n\t\tthis.apiKey = options.apiKey;\n\t\tthis.sdkVersion = options.sdkVersion;\n\n\t\tif (this.flushIntervalMs > 0) {\n\t\t\tthis.flushTimer = setInterval(() => {\n\t\t\t\tvoid this.flush();\n\t\t\t}, this.flushIntervalMs);\n\t\t}\n\t}\n\n\tenqueue(event: V2EventEnvelope): void {\n\t\tif (this.isStopped || this.isShuttingDown) {\n\t\t\tthis.logger.warn(\n\t\t\t\t\"[WaniWani] Tracking transport is stopped, dropping event %s\",\n\t\t\t\tevent.id,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif (this.buffer.length >= this.maxBufferSize) {\n\t\t\tconst dropCount = this.buffer.length - this.maxBufferSize + 1;\n\t\t\tthis.buffer.splice(0, dropCount);\n\t\t\tthis.logger.warn(\n\t\t\t\t\"[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)\",\n\t\t\t\tdropCount,\n\t\t\t);\n\t\t}\n\n\t\tthis.buffer.push(event);\n\n\t\tif (this.buffer.length >= this.maxBatchSize) {\n\t\t\tvoid this.flush();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.scheduleMicroFlush();\n\t}\n\n\tpendingEvents(): number {\n\t\treturn this.buffer.length + this.inFlightCount;\n\t}\n\n\tasync flush(): Promise<void> {\n\t\tif (this.flushInFlight) return this.flushInFlight;\n\t\tthis.flushInFlight = this.flushLoop().finally(() => {\n\t\t\tthis.flushInFlight = undefined;\n\t\t});\n\t\treturn this.flushInFlight;\n\t}\n\n\tasync shutdown(\n\t\toptions?: TrackingShutdownOptions,\n\t): Promise<TrackingShutdownResult> {\n\t\tthis.isShuttingDown = true;\n\t\tif (this.flushTimer) {\n\t\t\tclearInterval(this.flushTimer);\n\t\t\tthis.flushTimer = undefined;\n\t\t}\n\t\tif (this.flushScheduledTimer) {\n\t\t\tclearTimeout(this.flushScheduledTimer);\n\t\t\tthis.flushScheduledTimer = undefined;\n\t\t\tthis.flushScheduled = false;\n\t\t}\n\n\t\tconst timeoutMs = options?.timeoutMs ?? this.shutdownTimeoutMs;\n\t\tconst flushPromise = this.flush();\n\n\t\tif (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {\n\t\t\tawait flushPromise;\n\t\t\tthis.isStopped = true;\n\t\t\treturn { timedOut: false, pendingEvents: this.pendingEvents() };\n\t\t}\n\n\t\tconst timeoutSignal = Symbol(\"shutdown-timeout\");\n\t\tconst result = await Promise.race([\n\t\t\tflushPromise.then(() => \"flushed\" as const),\n\t\t\tthis.sleep(timeoutMs).then(() => timeoutSignal),\n\t\t]);\n\n\t\tif (result === timeoutSignal) {\n\t\t\tthis.isStopped = true;\n\t\t\treturn { timedOut: true, pendingEvents: this.pendingEvents() };\n\t\t}\n\n\t\tthis.isStopped = true;\n\t\treturn { timedOut: false, pendingEvents: this.pendingEvents() };\n\t}\n\n\tprivate scheduleMicroFlush(): void {\n\t\tif (this.flushScheduled) return;\n\t\tthis.flushScheduled = true;\n\t\tthis.flushScheduledTimer = setTimeout(() => {\n\t\t\tthis.flushScheduledTimer = undefined;\n\t\t\tthis.flushScheduled = false;\n\t\t\tvoid this.flush();\n\t\t}, 0);\n\t}\n\n\tprivate async flushLoop(): Promise<void> {\n\t\twhile (this.buffer.length > 0 && !this.isStopped) {\n\t\t\tconst batch = this.buffer.splice(0, this.maxBatchSize);\n\t\t\tawait this.sendBatchWithRetry(batch);\n\t\t}\n\t}\n\n\tprivate async sendBatchWithRetry(batch: V2EventEnvelope[]): Promise<void> {\n\t\tlet attempt = 0;\n\t\tlet pendingBatch = batch;\n\n\t\twhile (pendingBatch.length > 0 && !this.isStopped) {\n\t\t\tthis.inFlightCount = pendingBatch.length;\n\t\t\tconst result = await this.sendBatchOnce(pendingBatch);\n\t\t\tthis.inFlightCount = 0;\n\n\t\t\tswitch (result.kind) {\n\t\t\t\tcase \"success\":\n\t\t\t\t\treturn;\n\t\t\t\tcase \"auth\":\n\t\t\t\t\tthis.stopTransportForAuthFailure(result.status, pendingBatch.length);\n\t\t\t\t\treturn;\n\t\t\t\tcase \"permanent\":\n\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) after permanent failure: %s\",\n\t\t\t\t\t\tpendingBatch.length,\n\t\t\t\t\t\tresult.reason,\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\tcase \"retryable\":\n\t\t\t\t\tif (attempt >= this.maxRetries) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) after retry exhaustion: %s\",\n\t\t\t\t\t\t\tpendingBatch.length,\n\t\t\t\t\t\t\tresult.reason,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tawait this.sleep(this.backoffDelayMs(attempt));\n\t\t\t\t\tattempt += 1;\n\t\t\t\t\tcontinue;\n\t\t\t\tcase \"partial\":\n\t\t\t\t\tif (result.permanent.length > 0) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) rejected as permanent\",\n\t\t\t\t\t\t\tresult.permanent.length,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tif (result.retryable.length === 0) return;\n\t\t\t\t\tif (attempt >= this.maxRetries) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d retryable event(s) after retry exhaustion\",\n\t\t\t\t\t\t\tresult.retryable.length,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tpendingBatch = result.retryable;\n\t\t\t\t\tawait this.sleep(this.backoffDelayMs(attempt));\n\t\t\t\t\tattempt += 1;\n\t\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async sendBatchOnce(\n\t\tevents: V2EventEnvelope[],\n\t): Promise<SendBatchResult> {\n\t\tlet response: Response;\n\n\t\ttry {\n\t\t\tresponse = await this.fetchFn(this.endpointUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\tAuthorization: `Bearer ${this.apiKey}`,\n\t\t\t\t\t\"X-WaniWani-SDK\": SDK_NAME,\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify(this.makeBatchRequest(events)),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\treturn {\n\t\t\t\tkind: \"retryable\",\n\t\t\t\treason: getErrorMessage(error),\n\t\t\t};\n\t\t}\n\n\t\tif (AUTH_FAILURE_STATUS.has(response.status)) {\n\t\t\treturn { kind: \"auth\", status: response.status };\n\t\t}\n\n\t\tif (RETRYABLE_STATUS.has(response.status)) {\n\t\t\treturn {\n\t\t\t\tkind: \"retryable\",\n\t\t\t\treason: `HTTP ${response.status}`,\n\t\t\t};\n\t\t}\n\n\t\tif (!response.ok) {\n\t\t\treturn {\n\t\t\t\tkind: \"permanent\",\n\t\t\t\treason: `HTTP ${response.status}`,\n\t\t\t};\n\t\t}\n\n\t\tconst data = await parseJsonResponse<V2BatchResponse>(response);\n\t\tif (!data?.rejected || data.rejected.length === 0) {\n\t\t\treturn { kind: \"success\" };\n\t\t}\n\n\t\tconst partial = this.classifyRejectedEvents(events, data.rejected);\n\t\tif (partial.retryable.length === 0 && partial.permanent.length === 0) {\n\t\t\treturn { kind: \"success\" };\n\t\t}\n\n\t\treturn {\n\t\t\tkind: \"partial\",\n\t\t\tretryable: partial.retryable,\n\t\t\tpermanent: partial.permanent,\n\t\t};\n\t}\n\n\tprivate makeBatchRequest(events: V2EventEnvelope[]): V2BatchRequest {\n\t\treturn {\n\t\t\tsentAt: this.now().toISOString(),\n\t\t\tsource: {\n\t\t\t\tsdk: SDK_NAME,\n\t\t\t\tversion: this.sdkVersion ?? \"0.0.0\",\n\t\t\t},\n\t\t\tevents,\n\t\t};\n\t}\n\n\tprivate classifyRejectedEvents(\n\t\tevents: V2EventEnvelope[],\n\t\trejected: V2BatchRejectedEvent[],\n\t): {\n\t\tretryable: V2EventEnvelope[];\n\t\tpermanent: V2EventEnvelope[];\n\t} {\n\t\tconst byId = new Map(events.map((event) => [event.id, event]));\n\t\tconst retryable: V2EventEnvelope[] = [];\n\t\tconst permanent: V2EventEnvelope[] = [];\n\n\t\tfor (const rejectedEvent of rejected) {\n\t\t\tconst event = byId.get(rejectedEvent.eventId);\n\t\t\tif (!event) continue;\n\t\t\tif (isRetryableRejectedEvent(rejectedEvent)) {\n\t\t\t\tretryable.push(event);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpermanent.push(event);\n\t\t}\n\n\t\treturn { retryable, permanent };\n\t}\n\n\tprivate backoffDelayMs(attempt: number): number {\n\t\tconst rawDelay = this.retryBaseDelayMs * 2 ** attempt;\n\t\treturn Math.min(rawDelay, this.retryMaxDelayMs);\n\t}\n\n\tprivate stopTransportForAuthFailure(\n\t\tstatus: number,\n\t\trejectedCount: number,\n\t): void {\n\t\tthis.isStopped = true;\n\t\tconst buffered = this.buffer.length;\n\t\tthis.buffer.splice(0, buffered);\n\t\tthis.logger.error(\n\t\t\t\"[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)\",\n\t\t\tstatus,\n\t\t\trejectedCount + buffered,\n\t\t);\n\t}\n}\n\nfunction isRetryableRejectedEvent(\n\trejectedEvent: V2BatchRejectedEvent,\n): boolean {\n\tif (rejectedEvent.retryable === true) return true;\n\tconst code = rejectedEvent.code.toLowerCase();\n\treturn (\n\t\tcode.includes(\"timeout\") ||\n\t\tcode.includes(\"temporary\") ||\n\t\tcode.includes(\"unavailable\") ||\n\t\tcode.includes(\"rate_limit\") ||\n\t\tcode.includes(\"transient\") ||\n\t\tcode.includes(\"server\")\n\t);\n}\n\nasync function parseJsonResponse<T>(\n\tresponse: Response,\n): Promise<T | undefined> {\n\tconst body = await response.text();\n\tif (!body) return undefined;\n\ttry {\n\t\treturn JSON.parse(body) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction joinUrl(baseUrl: string, endpointPath: string): string {\n\tconst normalizedBase = baseUrl.endsWith(\"/\") ? baseUrl : `${baseUrl}/`;\n\tconst normalizedPath = endpointPath.startsWith(\"/\")\n\t\t? endpointPath.slice(1)\n\t\t: endpointPath;\n\treturn `${normalizedBase}${normalizedPath}`;\n}\n\nfunction getErrorMessage(error: unknown): string {\n\tif (error instanceof Error) return error.message;\n\treturn String(error);\n}\n","// Tracking Module\n\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tTrackInput,\n\tTrackingClient,\n\tTrackingShutdownOptions,\n} from \"./@types.js\";\nimport { mapTrackEventToV2 } from \"./mapper.js\";\nimport { createV2BatchTransport } from \"./transport.js\";\n\n// Re-export types\nexport type {\n\tEventType,\n\tLegacyTrackEvent,\n\tLinkClickedProperties,\n\tPurchaseCompletedProperties,\n\tQuoteSucceededProperties,\n\tToolCalledProperties,\n\tTrackEvent,\n\tTrackInput,\n\tTrackingClient,\n\tTrackingConfig,\n\tTrackingShutdownOptions,\n\tTrackingShutdownResult,\n} from \"./@types.js\";\nexport { createEventId, mapTrackEventToV2 } from \"./mapper.js\";\nexport type {\n\tV2BatchRejectedEvent,\n\tV2BatchRequest,\n\tV2BatchResponse,\n\tV2CorrelationIds,\n\tV2EnvelopeType,\n\tV2EventEnvelope,\n} from \"./v2-types.js\";\n\nexport function createTrackingClient(config: InternalConfig): TrackingClient {\n\tconst { baseUrl, apiKey, tracking } = config;\n\n\tfunction requireApiKey(): string {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(\"WANIWANI_API_KEY is not set\");\n\t\t}\n\t\treturn apiKey;\n\t}\n\n\tconst transport = apiKey\n\t\t? createV2BatchTransport({\n\t\t\t\tbaseUrl,\n\t\t\t\tapiKey,\n\t\t\t\tendpointPath: tracking.endpointPath,\n\t\t\t\tflushIntervalMs: tracking.flushIntervalMs,\n\t\t\t\tmaxBatchSize: tracking.maxBatchSize,\n\t\t\t\tmaxBufferSize: tracking.maxBufferSize,\n\t\t\t\tmaxRetries: tracking.maxRetries,\n\t\t\t\tretryBaseDelayMs: tracking.retryBaseDelayMs,\n\t\t\t\tretryMaxDelayMs: tracking.retryMaxDelayMs,\n\t\t\t\tshutdownTimeoutMs: tracking.shutdownTimeoutMs,\n\t\t\t})\n\t\t: undefined;\n\n\tconst client: TrackingClient = {\n\t\tasync track(event: TrackInput): Promise<{ eventId: string }> {\n\t\t\trequireApiKey();\n\t\t\tconst mappedEvent = mapTrackEventToV2(event);\n\t\t\ttransport?.enqueue(mappedEvent);\n\t\t\treturn { eventId: mappedEvent.id };\n\t\t},\n\t\tasync flush(): Promise<void> {\n\t\t\trequireApiKey();\n\t\t\tawait transport?.flush();\n\t\t},\n\t\tasync shutdown(options?: TrackingShutdownOptions) {\n\t\t\trequireApiKey();\n\t\t\treturn (\n\t\t\t\t(await transport?.shutdown({\n\t\t\t\t\ttimeoutMs: options?.timeoutMs ?? tracking.shutdownTimeoutMs,\n\t\t\t\t})) ?? { timedOut: false, pendingEvents: 0 }\n\t\t\t);\n\t\t},\n\t};\n\n\tif (transport) {\n\t\tattachShutdownHooks(client, tracking.shutdownTimeoutMs);\n\t}\n\treturn client;\n}\n\nfunction attachShutdownHooks(\n\tclient: TrackingClient,\n\tdefaultTimeoutMs: number,\n): void {\n\tif (\n\t\ttypeof process === \"undefined\" ||\n\t\ttypeof process.once !== \"function\" ||\n\t\ttypeof process.on !== \"function\"\n\t) {\n\t\treturn;\n\t}\n\n\tconst shutdown = () => {\n\t\tvoid client.shutdown({ timeoutMs: defaultTimeoutMs });\n\t};\n\n\tprocess.once(\"beforeExit\", shutdown);\n\tprocess.once(\"SIGINT\", shutdown);\n\tprocess.once(\"SIGTERM\", shutdown);\n}\n","// WaniWani SDK - Main Entry\n\nimport { createKbClient } from \"./kb/client.js\";\nimport { createTrackingClient } from \"./tracking/index.js\";\nimport type { WaniWaniClient, WaniWaniConfig } from \"./types.js\";\n\n/**\n * Create a WaniWani SDK client\n *\n * @param config - Configuration options\n * @returns A fully typed WaniWani client\n *\n * @example\n * ```typescript\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani({ apiKey: \"...\" });\n *\n * // Next.js route handler\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: { systemPrompt: \"You are a helpful assistant.\" },\n * });\n * ```\n */\nexport function waniwani(config?: WaniWaniConfig): WaniWaniClient {\n\tconst baseUrl = config?.baseUrl ?? \"https://app.waniwani.ai\";\n\tconst apiKey = config?.apiKey ?? process.env.WANIWANI_API_KEY;\n\tconst trackingConfig = {\n\t\tendpointPath: config?.tracking?.endpointPath ?? \"/api/mcp/events/v2/batch\",\n\t\tflushIntervalMs: config?.tracking?.flushIntervalMs ?? 1_000,\n\t\tmaxBatchSize: config?.tracking?.maxBatchSize ?? 20,\n\t\tmaxBufferSize: config?.tracking?.maxBufferSize ?? 1_000,\n\t\tmaxRetries: config?.tracking?.maxRetries ?? 3,\n\t\tretryBaseDelayMs: config?.tracking?.retryBaseDelayMs ?? 200,\n\t\tretryMaxDelayMs: config?.tracking?.retryMaxDelayMs ?? 2_000,\n\t\tshutdownTimeoutMs: config?.tracking?.shutdownTimeoutMs ?? 2_000,\n\t};\n\n\tconst internalConfig = { baseUrl, apiKey, tracking: trackingConfig };\n\n\t// Compose client from modules\n\tconst trackingClient = createTrackingClient(internalConfig);\n\tconst kbClient = createKbClient(internalConfig);\n\n\treturn {\n\t\t...trackingClient,\n\t\tkb: kbClient,\n\t\t_config: internalConfig,\n\t};\n}\n","/**\n * Server-side API route handler for widget tracking events.\n *\n * Receives batched events from the `useWaniwani` React hook and forwards them\n * to the WaniWani backend using the server-side SDK.\n *\n * @example Next.js App Router\n * ```typescript\n * // app/api/waniwani/track/route.ts\n * import { createTrackingRoute } from \"@waniwani/sdk/mcp\";\n *\n * const handler = createTrackingRoute({\n * apiKey: process.env.WANIWANI_API_KEY,\n * baseUrl: process.env.WANIWANI_BASE_URL,\n * });\n *\n * export { handler as POST };\n * ```\n */\n\nimport type { EventType, TrackInput } from \"../../tracking/@types.js\";\nimport type { WaniWaniConfig } from \"../../types.js\";\nimport { waniwani } from \"../../waniwani.js\";\n\nexport interface TrackingRouteOptions {\n\t/** API key for the WaniWani backend. Defaults to WANIWANI_API_KEY env var. */\n\tapiKey?: string;\n\t/** Base URL for the WaniWani backend. Defaults to https://app.waniwani.ai. */\n\tbaseUrl?: string;\n}\n\n/** Shape of a single event from the WidgetTransport client. */\ninterface WidgetEventPayload {\n\tevent_id?: string;\n\tevent_type?: string;\n\ttimestamp?: string;\n\tsource?: string;\n\tsession_id?: string;\n\ttrace_id?: string;\n\tuser_id?: string;\n\tevent_name?: string;\n\tmetadata?: Record<string, unknown>;\n\t[key: string]: unknown;\n}\n\n/** Batch payload sent by WidgetTransport. */\ninterface BatchPayload {\n\tevents: WidgetEventPayload[];\n\tsentAt?: string;\n}\n\n/**\n * Map a WidgetEvent from the client to the SDK's TrackInput format.\n */\nfunction mapWidgetEvent(ev: WidgetEventPayload): TrackInput {\n\tconst eventType = ev.event_type ?? \"widget_click\";\n\n\t// For manual tracking methods (track, identify, step, conversion),\n\t// use \"widget_<type>\" as the event name. Auto-capture events already\n\t// have the \"widget_\" prefix.\n\tconst isAutoCapture = eventType.startsWith(\"widget_\");\n\tconst eventName: EventType = (\n\t\tisAutoCapture ? eventType : `widget_${eventType}`\n\t) as EventType;\n\n\t// Merge metadata + any extra properties from the event\n\tconst properties: Record<string, unknown> = {\n\t\t...(ev.metadata ?? {}),\n\t};\n\tif (ev.event_name) properties.event_name = ev.event_name;\n\n\treturn {\n\t\tevent: eventName,\n\t\tproperties,\n\t\tsessionId: ev.session_id,\n\t\ttraceId: ev.trace_id,\n\t\texternalUserId: ev.user_id,\n\t\teventId: ev.event_id,\n\t\ttimestamp: ev.timestamp,\n\t\tsource: ev.source ?? \"widget\",\n\t} as TrackInput;\n}\n\n/**\n * Creates a POST handler that receives tracking events from `useWaniwani`\n * and forwards them to the WaniWani backend.\n */\nexport function createTrackingRoute(options?: TrackingRouteOptions) {\n\tconst config: WaniWaniConfig = {\n\t\tapiKey: options?.apiKey,\n\t\tbaseUrl: options?.baseUrl,\n\t};\n\n\t// Lazy singleton — created on first request\n\tlet client: ReturnType<typeof waniwani> | undefined;\n\n\tfunction getClient() {\n\t\tif (!client) {\n\t\t\tclient = waniwani(config);\n\t\t}\n\t\treturn client;\n\t}\n\n\treturn async function handler(request: Request): Promise<Response> {\n\t\tlet body: BatchPayload;\n\t\ttry {\n\t\t\tbody = (await request.json()) as BatchPayload;\n\t\t} catch {\n\t\t\treturn new Response(JSON.stringify({ error: \"Invalid JSON\" }), {\n\t\t\t\tstatus: 400,\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t});\n\t\t}\n\n\t\tif (!Array.isArray(body.events) || body.events.length === 0) {\n\t\t\treturn new Response(\n\t\t\t\tJSON.stringify({ error: \"Missing or empty events array\" }),\n\t\t\t\t{\n\t\t\t\t\tstatus: 400,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\ttry {\n\t\t\tconst c = getClient();\n\t\t\tconst results: string[] = [];\n\n\t\t\tfor (const ev of body.events) {\n\t\t\t\tconst trackInput = mapWidgetEvent(ev);\n\t\t\t\tconst result = await c.track(trackInput);\n\t\t\t\tresults.push(result.eventId);\n\t\t\t}\n\n\t\t\tawait c.flush();\n\n\t\t\treturn new Response(\n\t\t\t\tJSON.stringify({ ok: true, accepted: results.length }),\n\t\t\t\t{\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t},\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\treturn new Response(JSON.stringify({ error: message }), {\n\t\t\t\tstatus: 500,\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t});\n\t\t}\n\t};\n}\n","/**\n * Widget token minting — fetches short-lived JWTs from the WaniWani backend\n * so browser widgets can POST events directly.\n */\n\ninterface WidgetTokenConfig {\n\tbaseUrl: string;\n\tapiKey: string;\n}\n\ninterface WidgetTokenResult {\n\ttoken: string;\n\texpiresAt: string;\n}\n\ninterface CachedToken {\n\ttoken: string;\n\t/** Unix ms when the token expires */\n\texpiresAt: number;\n}\n\n/** Re-mint when < 2 minutes remain to avoid using nearly-expired tokens. */\nconst EXPIRY_BUFFER_MS = 2 * 60 * 1000;\n\nexport class WidgetTokenCache {\n\tprivate cached: CachedToken | null = null;\n\tprivate pending: Promise<string | null> | null = null;\n\tprivate readonly config: WidgetTokenConfig;\n\n\tconstructor(config: WidgetTokenConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Get a valid widget token. Returns a cached token if still fresh,\n\t * otherwise mints a new one. Returns `null` on failure.\n\t */\n\tasync getToken(sessionId?: string, traceId?: string): Promise<string | null> {\n\t\tif (this.cached && Date.now() < this.cached.expiresAt - EXPIRY_BUFFER_MS) {\n\t\t\treturn this.cached.token;\n\t\t}\n\n\t\t// Deduplicate concurrent requests\n\t\tif (this.pending) return this.pending;\n\n\t\tthis.pending = this.mint(sessionId, traceId).finally(() => {\n\t\t\tthis.pending = null;\n\t\t});\n\n\t\treturn this.pending;\n\t}\n\n\tprivate async mint(\n\t\tsessionId?: string,\n\t\ttraceId?: string,\n\t): Promise<string | null> {\n\t\tconst url = joinUrl(this.config.baseUrl, \"/api/mcp/widget-tokens\");\n\n\t\tconst body: Record<string, string> = {};\n\t\tif (sessionId) body.sessionId = sessionId;\n\t\tif (traceId) body.traceId = traceId;\n\n\t\ttry {\n\t\t\tconst response = await fetch(url, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\tAuthorization: `Bearer ${this.config.apiKey}`,\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify(body),\n\t\t\t});\n\n\t\t\tif (!response.ok) return null;\n\n\t\t\tconst json = (await response.json()) as Record<string, unknown>;\n\n\t\t\t// Support both flat { token, expiresAt } and nested { data: { token, expiresAt } }\n\t\t\tconst result = (\n\t\t\t\tjson.data && typeof json.data === \"object\" ? json.data : json\n\t\t\t) as WidgetTokenResult;\n\n\t\t\tconst expiresAtMs = new Date(result.expiresAt).getTime();\n\t\t\tif (!result.token || Number.isNaN(expiresAtMs)) return null;\n\n\t\t\tthis.cached = {\n\t\t\t\ttoken: result.token,\n\t\t\t\texpiresAt: expiresAtMs,\n\t\t\t};\n\n\t\t\treturn result.token;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n\nfunction joinUrl(baseUrl: string, path: string): string {\n\tconst base = baseUrl.endsWith(\"/\") ? baseUrl.slice(0, -1) : baseUrl;\n\treturn `${base}${path}`;\n}\n","import type { ToolCalledProperties, TrackInput } from \"../../tracking/index.js\";\nimport type { WaniWaniClient, WaniWaniConfig } from \"../../types.js\";\nimport { waniwani } from \"../../waniwani.js\";\nimport type { McpServer } from \"./tools/types\";\nimport { WidgetTokenCache } from \"./widget-token.js\";\n\ntype WaniwaniTracker = Pick<WaniWaniClient, \"flush\" | \"track\" | \"_config\">;\ntype UnknownRecord = Record<string, unknown>;\n\ntype WrappedServer = McpServer & {\n\t__waniwaniWrapped?: true;\n};\n\n/**\n * Options for withWaniwani().\n */\nexport type WithWaniwaniOptions = {\n\t/**\n\t * Optional pre-built WaniWani client.\n\t * When omitted, a new client is created from `config`.\n\t */\n\tclient?: WaniwaniTracker;\n\t/**\n\t * WaniWani client config used when `client` is omitted.\n\t */\n\tconfig?: WaniWaniConfig;\n\t/**\n\t * Optional explicit tool type. Defaults to `\"other\"`.\n\t */\n\ttoolType?:\n\t\t| ToolCalledProperties[\"type\"]\n\t\t| ((toolName: string) => ToolCalledProperties[\"type\"] | undefined);\n\t/**\n\t * Optional metadata merged into every tracked event.\n\t */\n\tmetadata?: UnknownRecord;\n\t/**\n\t * Flush tracking transport after each tool call.\n\t */\n\tflushAfterToolCall?: boolean;\n\t/**\n\t * Optional error callback for non-fatal tracking errors.\n\t */\n\tonError?: (error: Error) => void;\n\t/**\n\t * Inject widget tracking config into tool response `_meta.waniwani` so browser\n\t * widgets can send events directly to the WaniWani backend.\n\t *\n\t * Always injects `endpoint`. Injects `token` when an API key is configured\n\t * and token minting succeeds.\n\t *\n\t * @default true\n\t */\n\tinjectWidgetToken?: boolean;\n};\n\nconst DEFAULT_BASE_URL = \"https://app.waniwani.ai\";\n\n/**\n * Wrap an MCP server so tool handlers automatically emit `tool.called` events.\n *\n * The wrapper intercepts `server.registerTool(...)`, tracks each invocation,\n * then forwards execution to the original tool handler.\n *\n * When `injectWidgetToken` is enabled (default), tracking config is injected\n * into tool response `_meta.waniwani` so browser widgets can post events\n * directly to the WaniWani backend without a server-side proxy.\n */\nexport function withWaniwani(\n\tserver: McpServer,\n\toptions: WithWaniwaniOptions = {},\n): McpServer {\n\tconst wrappedServer = server as WrappedServer;\n\tif (wrappedServer.__waniwaniWrapped) {\n\t\treturn wrappedServer;\n\t}\n\n\twrappedServer.__waniwaniWrapped = true;\n\n\tconst tracker = options.client ?? waniwani(options.config);\n\tconst injectToken = options.injectWidgetToken !== false;\n\n\t// Lazy-init token cache — only created if we have an API key\n\tlet tokenCache: WidgetTokenCache | null = null;\n\n\tfunction getTokenCache(): WidgetTokenCache | null {\n\t\tif (tokenCache) return tokenCache;\n\t\tconst apiKey = tracker._config.apiKey;\n\t\tif (!apiKey) return null;\n\t\ttokenCache = new WidgetTokenCache({\n\t\t\tbaseUrl: tracker._config.baseUrl ?? DEFAULT_BASE_URL,\n\t\t\tapiKey,\n\t\t});\n\t\treturn tokenCache;\n\t}\n\n\tconst originalRegisterTool = server.registerTool.bind(server) as (\n\t\t...args: unknown[]\n\t) => unknown;\n\n\twrappedServer.registerTool = ((...args: unknown[]) => {\n\t\tconst [toolNameRaw, config, handlerRaw] = args;\n\t\tconst toolName =\n\t\t\ttypeof toolNameRaw === \"string\" && toolNameRaw.trim().length > 0\n\t\t\t\t? toolNameRaw\n\t\t\t\t: \"unknown\";\n\n\t\tif (typeof handlerRaw !== \"function\") {\n\t\t\treturn originalRegisterTool(...args);\n\t\t}\n\n\t\tconst handler = handlerRaw as (\n\t\t\tinput: unknown,\n\t\t\textra: unknown,\n\t\t) => Promise<unknown> | unknown;\n\n\t\tconst wrappedHandler = async (input: unknown, extra: unknown) => {\n\t\t\tconst startTime = performance.now();\n\t\t\ttry {\n\t\t\t\tconst result = await handler(input, extra);\n\t\t\t\tconst durationMs = Math.round(performance.now() - startTime);\n\n\t\t\t\tawait safeTrack(\n\t\t\t\t\ttracker,\n\t\t\t\t\tbuildTrackInput(toolName, extra, options, {\n\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\tstatus: \"ok\",\n\t\t\t\t\t}),\n\t\t\t\t\toptions.onError,\n\t\t\t\t);\n\n\t\t\t\tif (options.flushAfterToolCall) {\n\t\t\t\t\tawait safeFlush(tracker, options.onError);\n\t\t\t\t}\n\n\t\t\t\tif (injectToken) {\n\t\t\t\t\tawait injectWidgetConfig(\n\t\t\t\t\t\tresult,\n\t\t\t\t\t\tgetTokenCache(),\n\t\t\t\t\t\ttracker._config.baseUrl ?? DEFAULT_BASE_URL,\n\t\t\t\t\t\toptions.onError,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn result;\n\t\t\t} catch (error) {\n\t\t\t\tconst durationMs = Math.round(performance.now() - startTime);\n\n\t\t\t\tawait safeTrack(\n\t\t\t\t\ttracker,\n\t\t\t\t\tbuildTrackInput(toolName, extra, options, {\n\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\terror instanceof Error ? error.message : String(error),\n\t\t\t\t\t}),\n\t\t\t\t\toptions.onError,\n\t\t\t\t);\n\n\t\t\t\tif (options.flushAfterToolCall) {\n\t\t\t\t\tawait safeFlush(tracker, options.onError);\n\t\t\t\t}\n\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t};\n\n\t\treturn originalRegisterTool(toolNameRaw, config, wrappedHandler);\n\t}) as McpServer[\"registerTool\"];\n\n\treturn wrappedServer;\n}\n\nasync function injectWidgetConfig(\n\tresult: unknown,\n\tcache: WidgetTokenCache | null,\n\tbaseUrl: string,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\tif (!isRecord(result)) return;\n\n\tif (!isRecord(result._meta)) {\n\t\t(result as UnknownRecord)._meta = {};\n\t}\n\n\tconst meta = (result as UnknownRecord)._meta as UnknownRecord;\n\tconst waniwaniConfig: UnknownRecord = {\n\t\tendpoint: `${baseUrl.replace(/\\/$/, \"\")}/api/mcp/events/v2/batch`,\n\t};\n\n\tif (cache) {\n\t\ttry {\n\t\t\tconst token = await cache.getToken();\n\t\t\tif (token) {\n\t\t\t\twaniwaniConfig.token = token;\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tonError?.(toError(error));\n\t\t}\n\t}\n\n\tmeta.waniwani = waniwaniConfig;\n}\n\nfunction buildTrackInput(\n\ttoolName: string,\n\textra: unknown,\n\toptions: WithWaniwaniOptions,\n\ttiming?: { durationMs: number; status: string; errorMessage?: string },\n): TrackInput {\n\tconst toolType = resolveToolType(toolName, options.toolType);\n\tconst meta = extractMeta(extra);\n\n\treturn {\n\t\tevent: \"tool.called\",\n\t\tproperties: {\n\t\t\tname: toolName,\n\t\t\ttype: toolType,\n\t\t\t...(timing ?? {}),\n\t\t},\n\t\tmeta,\n\t\tmetadata: {\n\t\t\tsource: \"withWaniwani\",\n\t\t\t...(options.metadata ?? {}),\n\t\t},\n\t};\n}\n\nfunction resolveToolType(\n\ttoolName: string,\n\ttoolTypeOption: WithWaniwaniOptions[\"toolType\"],\n): ToolCalledProperties[\"type\"] {\n\tif (typeof toolTypeOption === \"function\") {\n\t\treturn toolTypeOption(toolName) ?? \"other\";\n\t}\n\treturn toolTypeOption ?? \"other\";\n}\n\nfunction extractMeta(extra: unknown): UnknownRecord | undefined {\n\tif (!isRecord(extra)) return undefined;\n\n\tconst meta = extra._meta;\n\tif (!isRecord(meta)) return undefined;\n\n\treturn meta;\n}\n\nfunction isRecord(value: unknown): value is UnknownRecord {\n\treturn Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nasync function safeTrack(\n\ttracker: Pick<WaniWaniClient, \"track\">,\n\tinput: TrackInput,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\ttry {\n\t\tawait tracker.track(input);\n\t} catch (error) {\n\t\tonError?.(toError(error));\n\t}\n}\n\nasync function safeFlush(\n\ttracker: Pick<WaniWaniClient, \"flush\">,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\ttry {\n\t\tawait tracker.flush();\n\t} catch (error) {\n\t\tonError?.(toError(error));\n\t}\n}\n\nfunction toError(error: unknown): Error {\n\tif (error instanceof Error) {\n\t\treturn error;\n\t}\n\treturn new Error(String(error));\n}\n"],"mappings":"AAWO,SAASA,GAAiC,CAChD,OAAI,OAAO,OAAW,KAAe,WAAY,OACzC,SAED,UACR,CAKO,SAASC,IAAoB,CACnC,OAAOD,EAAe,IAAM,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,EAAuB,CACvC,OAAIA,aAAiB,MACbA,EAED,IAAI,MAAM,OAAOA,CAAK,CAAC,CAC/B","names":["detectPlatform","isOpenAI","isMCPApps","START","END","INTERRUPT","WIDGET","interrupt","config","question","field","context","suggestions","showWidget","resource","isInterrupt","value","isWidget","z","MIME_TYPE_OPENAI","MIME_TYPE_MCP","fetchHtml","baseUrl","path","normalizedBase","buildOpenAIResourceMeta","config","buildMcpAppsResourceMeta","csp","buildToolMeta","deflateSync","inflateSync","encodeFlowToken","data","json","decodeFlowToken","token","compressed","describeZodField","schema","desc","def","vals","v","buildFlowProtocol","config","lines","fieldList","key","info","getInputMeta","args","decoded","decodeFlowToken","resolveNextNode","edge","state","isFilled","buildInterruptResult","questions","context","currentNode","conversational","q","unanswered","isSingle","q0","conversationalValue","executeFrom","startNodeName","startState","nodes","nodeConfigs","edges","meta","MAX_ITERATIONS","iterations","END","handler","result","isInterrupt","interruptResult","isWidget","widgetField","resource","nodeConversational","buildToolMeta","error","inputSchema","z","compileFlow","input","protocol","fullDescription","handleToolCall","inputMeta","startEdge","START","firstNode","step","updatedState","nextNode","server","extra","_meta","flowToken","encodeFlowToken","textPayload","content","responseMeta","StateGraph","config","name","configOrHandler","maybeHandler","START","END","handler","nodeConfig","from","to","condition","compileFlow","startEdge","edge","createFlow","config","StateGraph","createResource","config","id","title","description","baseUrl","htmlPath","widgetDomain","prefersBorder","autoHeight","widgetCSP","hostname","openaiUri","mcpUri","htmlPromise","getHtml","fetchHtml","uiDescription","register","server","html","MIME_TYPE_OPENAI","uri","buildOpenAIResourceMeta","MIME_TYPE_MCP","buildMcpAppsResourceMeta","createTool","config","handler","resource","description","inputSchema","annotations","id","title","toolMeta","buildToolMeta","server","args","extra","_meta","result","registerTools","tools","t","WaniWaniError","message","status","SDK_NAME","createKbClient","config","baseUrl","apiKey","requireApiKey","request","method","path","body","key","url","headers","init","response","text","WaniWaniError","files","query","options","DEFAULT_SOURCE","mapTrackEventToV2","input","options","now","generateId","createEventId","eventName","resolveEventName","meta","toRecord","metadata","correlation","resolveCorrelationIds","eventId","takeNonEmptyString","timestamp","normalizeTimestamp","source","rawLegacy","isLegacyTrackEvent","mappedMetadata","mapProperties","legacyProperties","mapLegacyProperties","explicitProperties","properties","requestId","pickFirstString","sessionId","traceId","externalUserId","correlationId","date","record","keys","key","value","DEFAULT_ENDPOINT_PATH","SDK_NAME","AUTH_FAILURE_STATUS","RETRYABLE_STATUS","createV2BatchTransport","options","BatchingV2Transport","joinUrl","DEFAULT_ENDPOINT_PATH","delayMs","resolve","event","dropCount","timeoutMs","flushPromise","timeoutSignal","batch","attempt","pendingBatch","result","events","response","error","getErrorMessage","data","parseJsonResponse","partial","rejected","byId","retryable","permanent","rejectedEvent","isRetryableRejectedEvent","rawDelay","status","rejectedCount","buffered","code","body","baseUrl","endpointPath","normalizedBase","normalizedPath","createTrackingClient","config","baseUrl","apiKey","tracking","requireApiKey","transport","createV2BatchTransport","client","event","mappedEvent","mapTrackEventToV2","options","attachShutdownHooks","defaultTimeoutMs","shutdown","waniwani","config","baseUrl","apiKey","trackingConfig","internalConfig","trackingClient","createTrackingClient","kbClient","createKbClient","mapWidgetEvent","ev","eventType","eventName","properties","createTrackingRoute","options","config","client","getClient","waniwani","request","body","c","results","trackInput","result","error","message","WidgetTokenCache","config","sessionId","traceId","url","joinUrl","body","response","json","result","expiresAtMs","baseUrl","path","DEFAULT_BASE_URL","withWaniwani","server","options","wrappedServer","tracker","waniwani","injectToken","tokenCache","getTokenCache","apiKey","WidgetTokenCache","originalRegisterTool","args","toolNameRaw","config","handlerRaw","toolName","handler","input","extra","startTime","result","durationMs","safeTrack","buildTrackInput","safeFlush","injectWidgetConfig","error","cache","baseUrl","onError","isRecord","meta","waniwaniConfig","token","toError","timing","toolType","resolveToolType","extractMeta","toolTypeOption","value"]}
1
+ {"version":3,"sources":["../../src/mcp/react/widgets/platform.ts","../../src/mcp/server/flows/@types.ts","../../src/mcp/server/flows/compile.ts","../../src/mcp/server/resources/meta.ts","../../src/mcp/server/flows/flow-token.ts","../../src/mcp/server/flows/state-graph.ts","../../src/mcp/server/flows/create-flow.ts","../../src/mcp/server/resources/create-resource.ts","../../src/mcp/server/tools/create-tool.ts","../../src/error.ts","../../src/kb/client.ts","../../src/tracking/mapper.ts","../../src/tracking/transport.ts","../../src/tracking/index.ts","../../src/waniwani.ts","../../src/mcp/server/tracking-route.ts","../../src/mcp/server/widget-token.ts","../../src/mcp/server/with-waniwani.ts"],"sourcesContent":["/**\n * Widget platform types\n */\nexport type WidgetPlatform = \"openai\" | \"mcp-apps\";\n\n/**\n * Detects which platform the widget is running on.\n *\n * OpenAI injects a global `window.openai` object.\n * MCP Apps runs in a sandboxed iframe and uses postMessage.\n */\nexport function detectPlatform(): WidgetPlatform {\n\tif (typeof window !== \"undefined\" && \"openai\" in window) {\n\t\treturn \"openai\";\n\t}\n\treturn \"mcp-apps\";\n}\n\n/**\n * Check if running on OpenAI platform\n */\nexport function isOpenAI(): boolean {\n\treturn detectPlatform() === \"openai\";\n}\n\n/**\n * Check if running on MCP Apps platform\n */\nexport function isMCPApps(): boolean {\n\treturn detectPlatform() === \"mcp-apps\";\n}\n","import type { z } from \"zod\";\nimport type { McpServer, RegisteredResource } from \"../resources/types\";\n\nexport type { McpServer };\n\n// ============================================================================\n// Sentinel constants\n// ============================================================================\n\nexport const START = \"__start__\" as const;\nexport const END = \"__end__\" as const;\n\n// ============================================================================\n// Signal types — returned by node handlers to control flow behavior\n// ============================================================================\n\nconst INTERRUPT = Symbol.for(\"waniwani.flow.interrupt\");\nconst WIDGET = Symbol.for(\"waniwani.flow.widget\");\n\n/** A single question within an interrupt step */\nexport type InterruptQuestion = {\n\t/** Question to ask the user */\n\tquestion: string;\n\t/** State key where the answer will be stored */\n\tfield: string;\n\t/** Optional suggestions to present as options */\n\tsuggestions?: string[];\n\t/** Hidden context/instructions for this specific question (not shown to user directly) */\n\tcontext?: string;\n};\n\n/**\n * Interrupt signal — pauses the flow and asks the user one or more questions.\n * Single-question and multi-question interrupts use the same type.\n */\nexport type InterruptSignal = {\n\treadonly __type: typeof INTERRUPT;\n\t/** Questions to ask — ask all in one conversational message */\n\tquestions: InterruptQuestion[];\n\t/** Overall hidden context/instructions for the assistant (not shown to user directly) */\n\tcontext?: string;\n};\n\nexport type WidgetSignal = {\n\treadonly __type: typeof WIDGET;\n\t/** The resource to display */\n\tresource: RegisteredResource;\n\t/** Data to pass to the widget as structuredContent */\n\tdata: Record<string, unknown>;\n\t/** Description of what the widget does (for the AI's context) */\n\tdescription?: string;\n\t/**\n\t * State key this widget fills — enables auto-skip when the field is already in state.\n\t * Pass this so the engine can skip the widget step when the answer is already known.\n\t */\n\tfield?: string;\n};\n\n/**\n * Create an interrupt signal — pauses the flow and asks the user a question.\n *\n * Accepts a single question (shorthand) or multiple questions (array form).\n * Both produce the same signal type.\n *\n * @example Single question\n * ```ts\n * return interrupt({ question: \"Your email?\", field: \"email\" })\n * ```\n *\n * @example Multiple questions (asked together in one message)\n * ```ts\n * return interrupt({ questions: [\n * { question: \"How many employees?\", field: \"headcount\" },\n * { question: \"Average age?\", field: \"averageAge\" },\n * ]})\n * ```\n */\nexport function interrupt(\n\tconfig:\n\t\t| {\n\t\t\t\tquestion: string;\n\t\t\t\tfield: string;\n\t\t\t\tsuggestions?: string[];\n\t\t\t\tcontext?: string;\n\t\t }\n\t\t| { questions: InterruptQuestion[]; context?: string },\n): InterruptSignal {\n\tif (\"questions\" in config) {\n\t\treturn {\n\t\t\t__type: INTERRUPT,\n\t\t\tquestions: config.questions,\n\t\t\tcontext: config.context,\n\t\t};\n\t}\n\tconst { question, field, context, suggestions } = config;\n\treturn {\n\t\t__type: INTERRUPT,\n\t\tquestions: [{ question, field, context, suggestions }],\n\t};\n}\n\n/**\n * Create a widget signal — pauses the flow and renders a widget UI.\n *\n * Pass `field` to enable auto-skip: if the field is already in state, the widget\n * step will be skipped automatically.\n */\nexport function showWidget(\n\tresource: RegisteredResource,\n\tconfig: {\n\t\tdata: Record<string, unknown>;\n\t\tdescription?: string;\n\t\tfield?: string;\n\t},\n): WidgetSignal {\n\treturn { __type: WIDGET, resource, ...config };\n}\n\nexport function isInterrupt(value: unknown): value is InterruptSignal {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"__type\" in value &&\n\t\t(value as InterruptSignal).__type === INTERRUPT\n\t);\n}\n\nexport function isWidget(value: unknown): value is WidgetSignal {\n\treturn (\n\t\ttypeof value === \"object\" &&\n\t\tvalue !== null &&\n\t\t\"__type\" in value &&\n\t\t(value as WidgetSignal).__type === WIDGET\n\t);\n}\n\n// ============================================================================\n// Node & edge definitions\n// ============================================================================\n\nexport type MaybePromise<T> = T | Promise<T>;\n\n/**\n * Optional config for handler-based nodes.\n * Provides metadata used by the engine (e.g., auto-skip on widget steps).\n *\n * For declarative nodes, config is inferred automatically — no need to pass this.\n */\nexport type NodeConfig<\n\tTState extends Record<string, unknown> = Record<string, unknown>,\n> = {\n\t/**\n\t * State key this node fills.\n\t * When set on a handler-based widget node and the field is already in state,\n\t * the node is auto-skipped. (Alternatively, pass `field` to `showWidget()`.)\n\t */\n\tfield?: Extract<keyof TState, string>;\n\t/**\n\t * Mark this node as conversational — the AI will engage in back-and-forth\n\t * conversation before advancing to the next node.\n\t *\n\t * - `true` — generic conversational behavior\n\t * - `string` — specific guidance for the AI (e.g., \"Help the user compare plans\")\n\t */\n\tconversational?: boolean | string;\n};\n\n// ============================================================================\n// Declarative node configs — shorthand for common patterns, no handler needed\n// ============================================================================\n\n/**\n * Node handler — a single function type for all node kinds.\n * The return value determines behavior:\n * - `Partial<TState>` → action node (state merged, auto-advance)\n * - `InterruptSignal` → interrupt (pause, ask user one or more questions)\n * - `WidgetSignal` → widget step (pause, show widget)\n */\nexport type NodeHandler<TState> = (\n\tstate: Partial<TState>,\n\tmeta?: Record<string, unknown>,\n) => MaybePromise<Partial<TState> | InterruptSignal | WidgetSignal>;\n\n/**\n * Condition function for conditional edges.\n * Receives current state, returns the name of the next node.\n */\nexport type ConditionFn<TState> = (\n\tstate: Partial<TState>,\n) => string | Promise<string>;\n\nexport type Edge<TState> =\n\t| { type: \"direct\"; to: string }\n\t| { type: \"conditional\"; condition: ConditionFn<TState> };\n\n// ============================================================================\n// Flow config & compiled output\n// ============================================================================\n\nexport type FlowConfig = {\n\t/** Unique identifier for the flow (becomes the MCP tool name) */\n\tid: string;\n\t/** Display title */\n\ttitle: string;\n\t/** Description for the AI (explains when to use this flow) */\n\tdescription: string;\n\t/**\n\t * Define the flow's state — each field the flow collects.\n\t * Keys are the field names used in `interrupt({ field })` or `NodeConfig.field`,\n\t * values are Zod schemas with `.describe()`.\n\t *\n\t * The state definition serves two purposes:\n\t * 1. Type inference — `TState` is automatically derived, no explicit generic needed\n\t * 2. AI protocol — field names, types, and descriptions are included in the tool\n\t * description so the AI can pre-fill answers via `_meta.flow.state`\n\t *\n\t * @example\n\t * ```ts\n\t * state: {\n\t * country: z.string().describe(\"Country the business is based in\"),\n\t * status: z.enum([\"registered\", \"unregistered\"]).describe(\"Business registration status\"),\n\t * }\n\t * ```\n\t */\n\tstate: Record<string, z.ZodType>;\n\t/** Optional tool annotations */\n\tannotations?: {\n\t\treadOnlyHint?: boolean;\n\t\tidempotentHint?: boolean;\n\t\topenWorldHint?: boolean;\n\t\tdestructiveHint?: boolean;\n\t};\n};\n\n/**\n * Infer the runtime state type from a flow's state schema definition.\n *\n * @example\n * ```ts\n * const config = {\n * state: {\n * country: z.enum([\"FR\", \"DE\"]),\n * status: z.enum([\"registered\", \"unregistered\"]),\n * }\n * };\n * type MyState = InferFlowState<typeof config.state>;\n * // { country: \"FR\" | \"DE\"; status: \"registered\" | \"unregistered\" }\n * ```\n */\nexport type InferFlowState<T extends Record<string, z.ZodType>> = {\n\t[K in keyof T]: z.infer<T[K]>;\n};\n\n/**\n * A compiled flow — can be registered on an McpServer.\n */\nexport type RegisteredFlow = {\n\tid: string;\n\ttitle: string;\n\tdescription: string;\n\tregister: (server: McpServer) => Promise<void>;\n};\n","import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport type { RequestHandlerExtra } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport type {\n\tServerNotification,\n\tServerRequest,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { z } from \"zod\";\nimport { buildToolMeta } from \"../resources/meta\";\nimport type {\n\tEdge,\n\tFlowConfig,\n\tMcpServer,\n\tNodeConfig,\n\tNodeHandler,\n\tRegisteredFlow,\n} from \"./@types\";\nimport { END, isInterrupt, isWidget, START } from \"./@types\";\nimport type { FlowTokenData } from \"./flow-token\";\nimport { decodeFlowToken, encodeFlowToken } from \"./flow-token\";\n\n// ============================================================================\n// Types\n// ============================================================================\n\ninterface CompileInput<TState extends Record<string, unknown>> {\n\tconfig: FlowConfig;\n\tnodes: Map<string, NodeHandler<TState>>;\n\tnodeConfigs: Map<string, NodeConfig<TState>>;\n\tedges: Map<string, Edge<TState>>;\n}\n\ntype FlowToolInput = {\n\taction: \"start\" | \"continue\";\n\tstateUpdates?: Record<string, unknown>;\n\tflowToken?: string;\n};\n\ntype FlowPayload = {\n\tstatus: \"widget\" | \"interrupt\" | \"complete\" | \"error\";\n\t[key: string]: unknown;\n};\n\ntype ExecutionResult = {\n\tpayload: FlowPayload;\n\tdata?: Record<string, unknown>;\n\twidgetMeta?: Record<string, unknown>;\n\tflowMeta?: {\n\t\tstep?: string;\n\t\tstate: Record<string, unknown>;\n\t\tfield?: string;\n\t\twidgetId?: string;\n\t\t/** Cached interrupt questions — avoids re-executing the handler on partial answers */\n\t\tquestions?: Array<{\n\t\t\tquestion: string;\n\t\t\tfield: string;\n\t\t\tsuggestions?: string[];\n\t\t\tcontext?: string;\n\t\t}>;\n\t\t/** Cached overall interrupt context */\n\t\tinterruptContext?: string;\n\t};\n};\n\n// ============================================================================\n// Flow protocol — embedded in tool description\n// ============================================================================\n\n/** Extract a human-readable label from a Zod schema for the AI protocol */\nfunction describeZodField(schema: z.ZodType): string {\n\tconst desc = schema.description ?? \"\";\n\tconst def = (\n\t\tschema as unknown as {\n\t\t\t_zod: { def: { type: string; entries?: Record<string, string> } };\n\t\t}\n\t)._zod?.def;\n\n\tif (def?.type === \"enum\" && def.entries) {\n\t\tconst vals = Object.keys(def.entries)\n\t\t\t.map((v) => `\"${v}\"`)\n\t\t\t.join(\" | \");\n\t\treturn desc ? `${vals} — ${desc}` : vals;\n\t}\n\n\treturn desc;\n}\n\nfunction buildFlowProtocol(config: FlowConfig): string {\n\tconst lines = [\n\t\t\"\",\n\t\t\"## FLOW EXECUTION PROTOCOL\",\n\t\t\"\",\n\t\t\"This tool implements a multi-step conversational flow. Follow this protocol exactly:\",\n\t\t\"\",\n\t\t'1. Call with `action: \"start\"` to begin. If the user\\'s message already',\n\t\t\" contains answers to likely questions, extract them into `stateUpdates`\",\n\t\t\" as `{ field: value }` pairs. The engine will auto-skip steps whose\",\n\t\t\" fields are already filled.\",\n\t\t\" Only extract values the user explicitly stated — do NOT guess or invent values.\",\n\t];\n\n\tif (config.state) {\n\t\tconst fieldList = Object.entries(config.state)\n\t\t\t.map(([key, schema]) => {\n\t\t\t\tconst info = describeZodField(schema);\n\t\t\t\treturn info ? `\\`${key}\\` (${info})` : `\\`${key}\\``;\n\t\t\t})\n\t\t\t.join(\", \");\n\t\tlines.push(` Known fields: ${fieldList}.`);\n\t}\n\n\tlines.push(\n\t\t\"2. The response JSON `status` field tells you what to do next:\",\n\t\t' - `\"interrupt\"`: Pause and ask the user. Two forms:',\n\t\t\" a. Single question: `{ question, field, context? }` — ask `question`, store answer in `field`.\",\n\t\t\" b. Multi-question: `{ questions: [{question, field}, ...], context? }` — ask ALL questions\",\n\t\t\" in one conversational message, collect all answers.\",\n\t\t\" `context` (if present) is hidden AI instructions — use to shape your response, do NOT show verbatim.\",\n\t\t\" Then call again with:\",\n\t\t' `action: \"continue\"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),',\n\t\t\" `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.\",\n\t\t' - `\"widget\"`: A widget UI is being shown. The user will interact with the widget.',\n\t\t\" When the user makes a choice, call again with:\",\n\t\t' `action: \"continue\"`, `flowToken` = the `flowToken` from the response,',\n\t\t\" `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.\",\n\t\t' - `\"complete\"`: The flow is done. Present the result to the user.',\n\t\t' - `\"error\"`: Something went wrong. Show the `error` message.',\n\t\t\"\",\n\t\t\"3. ALWAYS pass back the `flowToken` string exactly as received — it is an opaque token, do not modify it.\",\n\t\t\"4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.\",\n\t\t\"5. Include only the fields the user actually answered in `stateUpdates` — do NOT guess missing ones.\",\n\t\t\" If the user did not answer all pending questions, the engine will re-prompt for the remaining ones.\",\n\t\t\" If the user mentioned values for other known fields, include those too —\",\n\t\t\" they will be applied immediately and those steps will be auto-skipped.\",\n\t\t'6. CONVERSATIONAL STEPS: When a response includes a `\"conversational\"` field:',\n\t\t\" - Do NOT immediately call `continue`. Instead, engage in back-and-forth conversation.\",\n\t\t\" - The user may ask follow-up questions, explore options, or request changes.\",\n\t\t' - Only call `action: \"continue\"` when the user explicitly wants to move on',\n\t\t' (e.g., \"looks good\", \"let\\'s continue\", \"I\\'ll go with X\", or selects an option).',\n\t\t\" - If `conversational` is a string, use it as guidance for what topics to discuss.\",\n\t\t\" - While conversing, do NOT call the tool — just respond naturally to the user.\",\n\t);\n\n\treturn lines.join(\"\\n\");\n}\n\nfunction getInputMeta(args: FlowToolInput): FlowTokenData & {\n\tstate: Record<string, unknown>;\n} {\n\tif (args.flowToken) {\n\t\tconst decoded = decodeFlowToken(args.flowToken);\n\t\tif (decoded) return decoded;\n\t}\n\treturn { step: undefined as unknown as string, state: {} };\n}\n\n// ============================================================================\n// Edge resolution\n// ============================================================================\n\nasync function resolveNextNode<TState extends Record<string, unknown>>(\n\tedge: Edge<TState>,\n\tstate: Partial<TState>,\n): Promise<string> {\n\tif (edge.type === \"direct\") return edge.to;\n\treturn edge.condition(state);\n}\n\n// ============================================================================\n// Helpers\n// ============================================================================\n\n/** Check whether a state value counts as \"filled\" (not empty/missing). */\nfunction isFilled(v: unknown): boolean {\n\treturn v !== undefined && v !== null && v !== \"\";\n}\n\n// ============================================================================\n// Interrupt result builder\n// ============================================================================\n\ntype InterruptQuestionData = {\n\tquestion: string;\n\tfield: string;\n\tsuggestions?: string[];\n\tcontext?: string;\n};\n\n/**\n * Build an interrupt ExecutionResult from a list of questions and current state.\n * Filters out already-answered questions and caches the full question list in\n * flowMeta so partial-answer continues can filter without re-executing the handler.\n *\n * Returns `null` when all questions are already filled (caller should advance).\n */\nfunction buildInterruptResult<TState extends Record<string, unknown>>(\n\tquestions: InterruptQuestionData[],\n\tcontext: string | undefined,\n\tcurrentNode: string,\n\tstate: TState,\n\tconversational?: boolean | string,\n): ExecutionResult | null {\n\t// All filled — caller should advance to the next node\n\tif (questions.every((q) => isFilled(state[q.field as keyof TState]))) {\n\t\treturn null;\n\t}\n\n\t// Filter out questions whose fields are already answered\n\tconst unanswered = questions.filter(\n\t\t(q) => !isFilled(state[q.field as keyof TState]),\n\t);\n\n\t// Single-question shorthand: unwrap for cleaner AI payload\n\tconst isSingle = unanswered.length === 1;\n\tconst q0 = unanswered[0];\n\tconst conversationalValue = conversational\n\t\t? typeof conversational === \"string\"\n\t\t\t? conversational\n\t\t\t: true\n\t\t: undefined;\n\tconst payload =\n\t\tisSingle && q0\n\t\t\t? {\n\t\t\t\t\tstatus: \"interrupt\" as const,\n\t\t\t\t\tquestion: q0.question,\n\t\t\t\t\tfield: q0.field,\n\t\t\t\t\t...(q0.suggestions ? { suggestions: q0.suggestions } : {}),\n\t\t\t\t\t...(q0.context || context ? { context: q0.context ?? context } : {}),\n\t\t\t\t\t...(conversationalValue\n\t\t\t\t\t\t? { conversational: conversationalValue }\n\t\t\t\t\t\t: {}),\n\t\t\t\t}\n\t\t\t: {\n\t\t\t\t\tstatus: \"interrupt\" as const,\n\t\t\t\t\tquestions: unanswered,\n\t\t\t\t\t...(context ? { context } : {}),\n\t\t\t\t\t...(conversationalValue\n\t\t\t\t\t\t? { conversational: conversationalValue }\n\t\t\t\t\t\t: {}),\n\t\t\t\t};\n\n\treturn {\n\t\tpayload,\n\t\tflowMeta: {\n\t\t\tstep: currentNode,\n\t\t\tstate,\n\t\t\t...(isSingle && q0 ? { field: q0.field } : {}),\n\t\t\t// Cache the full question list so partial-answer continues\n\t\t\t// can filter without re-executing the node handler.\n\t\t\tquestions,\n\t\t\t...(context ? { interruptContext: context } : {}),\n\t\t},\n\t};\n}\n\n// ============================================================================\n// Execution engine\n// ============================================================================\n\nasync function executeFrom<TState extends Record<string, unknown>>(\n\tstartNodeName: string,\n\tstartState: TState,\n\tnodes: Map<string, NodeHandler<TState>>,\n\tnodeConfigs: Map<string, NodeConfig<TState>>,\n\tedges: Map<string, Edge<TState>>,\n\tmeta?: Record<string, unknown>,\n): Promise<ExecutionResult> {\n\tlet currentNode = startNodeName;\n\tlet state = { ...startState };\n\n\t// Safety limit to prevent infinite loops\n\tconst MAX_ITERATIONS = 50;\n\tlet iterations = 0;\n\n\twhile (iterations++ < MAX_ITERATIONS) {\n\t\t// Reached END\n\t\tif (currentNode === END) {\n\t\t\treturn {\n\t\t\t\tpayload: { status: \"complete\" },\n\t\t\t\tflowMeta: { state },\n\t\t\t};\n\t\t}\n\n\t\tconst handler = nodes.get(currentNode);\n\t\tif (!handler) {\n\t\t\treturn {\n\t\t\t\tpayload: {\n\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\terror: `Unknown node: \"${currentNode}\"`,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\ttry {\n\t\t\tconst result = await handler(state, meta);\n\n\t\t\t// Interrupt signal — pause and ask the user one or more questions\n\t\t\tif (isInterrupt(result)) {\n\t\t\t\tconst interruptResult = buildInterruptResult(\n\t\t\t\t\tresult.questions,\n\t\t\t\t\tresult.context,\n\t\t\t\t\tcurrentNode,\n\t\t\t\t\tstate,\n\t\t\t\t\tnodeConfigs.get(currentNode)?.conversational,\n\t\t\t\t);\n\t\t\t\tif (interruptResult) return interruptResult;\n\n\t\t\t\t// All questions filled — auto-skip to next node\n\t\t\t\tconst edge = edges.get(currentNode);\n\t\t\t\tif (!edge) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Widget signal — pause and show widget\n\t\t\tif (isWidget(result)) {\n\t\t\t\t// Auto-skip: use field from the signal, fall back to nodeConfig\n\t\t\t\tconst widgetField = result.field ?? nodeConfigs.get(currentNode)?.field;\n\t\t\t\tif (widgetField) {\n\t\t\t\t\tif (isFilled(state[widgetField as keyof TState])) {\n\t\t\t\t\t\tconst edge = edges.get(currentNode);\n\t\t\t\t\t\tif (!edge) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\t\t\t\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst resource = result.resource;\n\t\t\t\tconst nodeConversational = nodeConfigs.get(currentNode)?.conversational;\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"widget\",\n\t\t\t\t\t\tdescription: result.description,\n\t\t\t\t\t\t...(nodeConversational\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tconversational:\n\t\t\t\t\t\t\t\t\t\ttypeof nodeConversational === \"string\"\n\t\t\t\t\t\t\t\t\t\t\t? nodeConversational\n\t\t\t\t\t\t\t\t\t\t\t: true,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t},\n\t\t\t\t\tdata: result.data,\n\t\t\t\t\twidgetMeta: buildToolMeta({\n\t\t\t\t\t\topenaiTemplateUri: resource.openaiUri,\n\t\t\t\t\t\tmcpTemplateUri: resource.mcpUri,\n\t\t\t\t\t\tinvoking: \"Loading...\",\n\t\t\t\t\t\tinvoked: \"Loaded\",\n\t\t\t\t\t\tautoHeight: resource.autoHeight,\n\t\t\t\t\t}),\n\t\t\t\t\tflowMeta: {\n\t\t\t\t\t\tstep: currentNode,\n\t\t\t\t\t\tstate,\n\t\t\t\t\t\tfield: widgetField,\n\t\t\t\t\t\twidgetId: resource.id,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Action node — merge state and auto-advance\n\t\t\tstate = { ...state, ...result } as TState;\n\n\t\t\tconst edge = edges.get(currentNode);\n\t\t\tif (!edge) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror: `No outgoing edge from node \"${currentNode}\"`,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\t\t\tcurrentNode = await resolveNextNode(edge, state);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : String(error);\n\t\t\treturn {\n\t\t\t\tpayload: { status: \"error\", error: message },\n\t\t\t\tflowMeta: { step: currentNode, state },\n\t\t\t};\n\t\t}\n\t}\n\n\treturn {\n\t\tpayload: {\n\t\t\tstatus: \"error\",\n\t\t\terror: \"Flow exceeded maximum iterations (possible infinite loop)\",\n\t\t},\n\t};\n}\n\n// ============================================================================\n// Compile\n// ============================================================================\n\nconst inputSchema = {\n\taction: z\n\t\t.enum([\"start\", \"continue\"])\n\t\t.describe(\n\t\t\t'\"start\" to begin the flow, \"continue\" to resume after a pause (interrupt or widget)',\n\t\t),\n\tstateUpdates: z\n\t\t.record(z.string(), z.unknown())\n\t\t.optional()\n\t\t.describe(\n\t\t\t\"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.\",\n\t\t),\n\tflowToken: z\n\t\t.string()\n\t\t.optional()\n\t\t.describe(\n\t\t\t\"Opaque flow token from the previous response. Pass back exactly as received.\",\n\t\t),\n};\n\nexport function compileFlow<TState extends Record<string, unknown>>(\n\tinput: CompileInput<TState>,\n): RegisteredFlow {\n\tconst { config, nodes, nodeConfigs, edges } = input;\n\tconst protocol = buildFlowProtocol(config);\n\tconst fullDescription = `${config.description}\\n${protocol}`;\n\tasync function handleToolCall(\n\t\targs: FlowToolInput,\n\t\tmeta?: Record<string, unknown>,\n\t): Promise<ExecutionResult> {\n\t\tconst inputMeta = getInputMeta(args);\n\t\tconst state = inputMeta.state as TState;\n\n\t\tif (args.action === \"start\") {\n\t\t\tconst startEdge = edges.get(START);\n\t\t\tif (!startEdge) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror: \"No start edge\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Merge pre-filled answers and any stateUpdates\n\t\t\tconst startState = {\n\t\t\t\t...state,\n\t\t\t\t...(args.stateUpdates ?? {}),\n\t\t\t} as TState;\n\n\t\t\tconst firstNode = await resolveNextNode(startEdge, startState);\n\t\t\treturn executeFrom(\n\t\t\t\tfirstNode,\n\t\t\t\tstartState,\n\t\t\t\tnodes,\n\t\t\t\tnodeConfigs,\n\t\t\t\tedges,\n\t\t\t\tmeta,\n\t\t\t);\n\t\t}\n\n\t\tif (args.action === \"continue\") {\n\t\t\tconst step = inputMeta.step;\n\t\t\tif (!step) {\n\t\t\t\treturn {\n\t\t\t\t\tpayload: {\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t'Missing or invalid \"flowToken\" for continue action.' +\n\t\t\t\t\t\t\t\" Pass back the flowToken from the previous response exactly as received.\",\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst updatedState = {\n\t\t\t\t...state,\n\t\t\t\t...(args.stateUpdates ?? {}),\n\t\t\t} as TState;\n\n\t\t\t// If cached interrupt questions exist, check for unanswered questions\n\t\t\t// without re-executing the node handler (avoids side-effect replay).\n\t\t\tif (inputMeta.questions) {\n\t\t\t\tconst interruptResult = buildInterruptResult(\n\t\t\t\t\tinputMeta.questions,\n\t\t\t\t\tinputMeta.interruptContext,\n\t\t\t\t\tstep,\n\t\t\t\t\tupdatedState,\n\t\t\t\t\tnodeConfigs.get(step)?.conversational,\n\t\t\t\t);\n\t\t\t\tif (interruptResult) return interruptResult;\n\t\t\t\t// All questions answered — fall through to advance\n\t\t\t}\n\n\t\t\t// Advance to next node when: all cached questions are answered, or\n\t\t\t// this is a widget continue (widgets never have cached questions and\n\t\t\t// re-executing the handler would cause a stuck loop for field-less widgets).\n\t\t\t// Otherwise the AI may have dropped _meta.flow.questions — re-execute\n\t\t\t// from the current step so the handler can re-check unanswered questions.\n\t\t\tif (inputMeta.questions || inputMeta.widgetId) {\n\t\t\t\tconst edge = edges.get(step);\n\t\t\t\tif (!edge) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tpayload: {\n\t\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\t\terror: `No edge from step \"${step}\"`,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tconst nextNode = await resolveNextNode(edge, updatedState);\n\t\t\t\treturn executeFrom(\n\t\t\t\t\tnextNode,\n\t\t\t\t\tupdatedState,\n\t\t\t\t\tnodes,\n\t\t\t\t\tnodeConfigs,\n\t\t\t\t\tedges,\n\t\t\t\t\tmeta,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn executeFrom(step, updatedState, nodes, nodeConfigs, edges, meta);\n\t\t}\n\n\t\treturn {\n\t\t\tpayload: {\n\t\t\t\tstatus: \"error\",\n\t\t\t\terror: `Unknown action: \"${args.action}\"`,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn {\n\t\tid: config.id,\n\t\ttitle: config.title,\n\t\tdescription: fullDescription,\n\n\t\tasync register(server: McpServer): Promise<void> {\n\t\t\tserver.registerTool(\n\t\t\t\tconfig.id,\n\t\t\t\t{\n\t\t\t\t\ttitle: config.title,\n\t\t\t\t\tdescription: fullDescription,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tannotations: config.annotations,\n\t\t\t\t\t_meta: {\n\t\t\t\t\t\t\"openai/widgetAccessible\": true,\n\t\t\t\t\t\t\"openai/resultCanProduceWidget\": true,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t(async (args: FlowToolInput, extra: unknown) => {\n\t\t\t\t\tconst requestExtra = extra as RequestHandlerExtra<\n\t\t\t\t\t\tServerRequest,\n\t\t\t\t\t\tServerNotification\n\t\t\t\t\t>;\n\t\t\t\t\tconst _meta: Record<string, unknown> = requestExtra._meta ?? {};\n\n\t\t\t\t\tconst result = await handleToolCall(args, _meta);\n\n\t\t\t\t\t// Encode flow state as opaque token for the model to pass back\n\t\t\t\t\tconst flowToken = result.flowMeta\n\t\t\t\t\t\t? encodeFlowToken({\n\t\t\t\t\t\t\t\tstep: result.flowMeta.step ?? \"\",\n\t\t\t\t\t\t\t\tstate: result.flowMeta.state,\n\t\t\t\t\t\t\t\tfield: result.flowMeta.field,\n\t\t\t\t\t\t\t\twidgetId: result.flowMeta.widgetId,\n\t\t\t\t\t\t\t\tquestions: result.flowMeta.questions,\n\t\t\t\t\t\t\t\tinterruptContext: result.flowMeta.interruptContext,\n\t\t\t\t\t\t\t})\n\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\t// Text content includes the payload + flowToken for the model\n\t\t\t\t\t// Also includes flowId and widgetId so widget iframes can use them directly\n\t\t\t\t\tconst textPayload = {\n\t\t\t\t\t\t...result.payload,\n\t\t\t\t\t\t...(flowToken\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\tflowToken,\n\t\t\t\t\t\t\t\t\tflowId: config.id,\n\t\t\t\t\t\t\t\t\t...(result.flowMeta?.widgetId\n\t\t\t\t\t\t\t\t\t\t? { widgetId: result.flowMeta.widgetId }\n\t\t\t\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t};\n\t\t\t\t\tconst content = [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\t\ttext: JSON.stringify(textPayload, null, 2),\n\t\t\t\t\t\t},\n\t\t\t\t\t];\n\n\t\t\t\t\t// _meta carries widget URIs and tracking config for the host/iframe\n\t\t\t\t\tconst responseMeta = {\n\t\t\t\t\t\t...(result.widgetMeta ?? {}),\n\t\t\t\t\t\t..._meta,\n\t\t\t\t\t};\n\n\t\t\t\t\t// Widget response — include structuredContent + widget metadata\n\t\t\t\t\tif (result.widgetMeta) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t\tstructuredContent: result.data,\n\t\t\t\t\t\t\t_meta: responseMeta,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Non-widget response (interrupt, complete, error)\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\t...(Object.keys(responseMeta).length > 0\n\t\t\t\t\t\t\t? { _meta: responseMeta }\n\t\t\t\t\t\t\t: {}),\n\t\t\t\t\t};\n\t\t\t\t}) as unknown as ToolCallback<typeof inputSchema>,\n\t\t\t);\n\t\t},\n\t};\n}\n","import type { WidgetCSP } from \"./types\";\n\n/**\n * MIME types for widget resources.\n * OpenAI Apps SDK uses \"text/html+skybridge\"\n * MCP Apps uses \"text/html;profile=mcp-app\"\n */\nexport const MIME_TYPE_OPENAI = \"text/html+skybridge\";\nexport const MIME_TYPE_MCP = \"text/html;profile=mcp-app\";\n\n// ---- HTML fetching ----\n\nexport const fetchHtml = async (\n\tbaseUrl: string,\n\tpath: string,\n): Promise<string> => {\n\tconst normalizedBase = baseUrl.endsWith(\"/\") ? baseUrl.slice(0, -1) : baseUrl;\n\tconst result = await fetch(`${normalizedBase}${path}`);\n\treturn await result.text();\n};\n\n// ---- OpenAI resource metadata ----\n\ninterface OpenAIResourceMeta {\n\t[key: string]: unknown;\n\t\"openai/widgetDescription\"?: string;\n\t\"openai/widgetPrefersBorder\"?: boolean;\n\t\"openai/widgetDomain\"?: string;\n\t\"openai/widgetCSP\"?: WidgetCSP;\n}\n\nexport function buildOpenAIResourceMeta(config: {\n\tdescription?: string;\n\tprefersBorder?: boolean;\n\twidgetDomain: string;\n\twidgetCSP?: WidgetCSP;\n}): OpenAIResourceMeta {\n\treturn {\n\t\t\"openai/widgetDescription\": config.description,\n\t\t\"openai/widgetPrefersBorder\": config.prefersBorder,\n\t\t\"openai/widgetDomain\": config.widgetDomain,\n\t\t...(config.widgetCSP && { \"openai/widgetCSP\": config.widgetCSP }),\n\t};\n}\n\n// ---- MCP Apps resource metadata ----\n\ninterface McpAppsResourceMeta {\n\t[key: string]: unknown;\n\tui?: {\n\t\tcsp?: {\n\t\t\tconnectDomains?: string[];\n\t\t\tresourceDomains?: string[];\n\t\t\tframeDomains?: string[];\n\t\t\tredirectDomains?: string[];\n\t\t};\n\t\tdomain?: string;\n\t\tprefersBorder?: boolean;\n\t};\n}\n\nexport function buildMcpAppsResourceMeta(config: {\n\tdescription?: string;\n\tprefersBorder?: boolean;\n\twidgetDomain?: string;\n\twidgetCSP?: WidgetCSP;\n}): McpAppsResourceMeta {\n\tconst csp = config.widgetCSP\n\t\t? {\n\t\t\t\tconnectDomains: config.widgetCSP.connect_domains,\n\t\t\t\tresourceDomains: config.widgetCSP.resource_domains,\n\t\t\t\tframeDomains: config.widgetCSP.frame_domains,\n\t\t\t\tredirectDomains: config.widgetCSP.redirect_domains,\n\t\t\t}\n\t\t: undefined;\n\n\treturn {\n\t\tui: {\n\t\t\t...(csp && { csp }),\n\t\t\t...(config.widgetDomain && { domain: config.widgetDomain }),\n\t\t\t...(config.prefersBorder !== undefined && {\n\t\t\t\tprefersBorder: config.prefersBorder,\n\t\t\t}),\n\t\t},\n\t};\n}\n\n// ---- Tool metadata (references resource URIs) ----\n\nexport function buildToolMeta(config: {\n\topenaiTemplateUri?: string;\n\tmcpTemplateUri?: string;\n\tinvoking: string;\n\tinvoked: string;\n\tautoHeight?: boolean;\n}) {\n\treturn {\n\t\t// OpenAI metadata\n\t\t...(config.openaiTemplateUri && {\n\t\t\t\"openai/outputTemplate\": config.openaiTemplateUri,\n\t\t}),\n\t\t\"openai/toolInvocation/invoking\": config.invoking,\n\t\t\"openai/toolInvocation/invoked\": config.invoked,\n\t\t\"openai/widgetAccessible\": true,\n\t\t\"openai/resultCanProduceWidget\": true,\n\t\t// MCP Apps metadata\n\t\t...(config.mcpTemplateUri && {\n\t\t\tui: {\n\t\t\t\tresourceUri: config.mcpTemplateUri,\n\t\t\t\t...(config.autoHeight && { autoHeight: true }),\n\t\t\t},\n\t\t}),\n\t};\n}\n","/**\n * Opaque compressed token for flow state round-tripping.\n *\n * Flow state is compressed (zlib deflate) then base64-encoded. This produces\n * a token that:\n * 1. Looks like random noise — models can't mentally decode it\n * 2. Is smaller than raw base64 (~30% compression on typical state)\n * 3. The model treats it as an opaque string and passes it back unchanged\n *\n * Only encoded/decoded on the server (Node.js). Browser code should never\n * need to decode flow tokens.\n */\n\nimport { deflateSync, inflateSync } from \"node:zlib\";\n\nexport type FlowTokenData = {\n\tstep: string;\n\tstate: Record<string, unknown>;\n\tfield?: string;\n\twidgetId?: string;\n\tquestions?: Array<{\n\t\tquestion: string;\n\t\tfield: string;\n\t\tsuggestions?: string[];\n\t\tcontext?: string;\n\t}>;\n\tinterruptContext?: string;\n};\n\nexport function encodeFlowToken(data: FlowTokenData): string {\n\tconst json = JSON.stringify(data);\n\tconst compressed = deflateSync(json);\n\treturn compressed.toString(\"base64\");\n}\n\nexport function decodeFlowToken(token: string): FlowTokenData | null {\n\ttry {\n\t\tconst compressed = Buffer.from(token, \"base64\");\n\t\tconst json = inflateSync(compressed).toString(\"utf-8\");\n\t\treturn JSON.parse(json) as FlowTokenData;\n\t} catch {\n\t\treturn null;\n\t}\n}\n","import type {\n\tConditionFn,\n\tEdge,\n\tFlowConfig,\n\tNodeConfig,\n\tNodeHandler,\n\tRegisteredFlow,\n} from \"./@types\";\nimport { END, START } from \"./@types\";\nimport { compileFlow } from \"./compile\";\n\n/**\n * A LangGraph-inspired state graph builder for MCP tools.\n *\n * @example\n * ```ts\n * const flow = new StateGraph<MyState>({\n * id: \"onboarding\",\n * title: \"User Onboarding\",\n * description: \"Guides users through onboarding\",\n * })\n * .addNode(\"ask_name\", (state) => interrupt({ question: \"What's your name?\", field: \"name\" }))\n * .addNode(\"greet\", (state) => ({ greeting: `Hello ${state.name}!` }))\n * .addEdge(START, \"ask_name\")\n * .addEdge(\"ask_name\", \"greet\")\n * .addEdge(\"greet\", END)\n * .compile();\n * ```\n */\nexport class StateGraph<TState extends Record<string, unknown>> {\n\tprivate nodes = new Map<string, NodeHandler<TState>>();\n\tprivate nodeConfigs = new Map<string, NodeConfig<TState>>();\n\tprivate edges = new Map<string, Edge<TState>>();\n\tprivate config: FlowConfig;\n\n\tconstructor(config: FlowConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Add a node with just a handler.\n\t */\n\taddNode(name: string, handler: NodeHandler<TState>): this;\n\t/**\n\t * Add a node with config and a handler.\n\t * Pass `field` to enable auto-skip on widget nodes.\n\t */\n\taddNode(\n\t\tname: string,\n\t\tconfig: NodeConfig<TState>,\n\t\thandler: NodeHandler<TState>,\n\t): this;\n\taddNode(\n\t\tname: string,\n\t\tconfigOrHandler: NodeConfig<TState> | NodeHandler<TState>,\n\t\tmaybeHandler?: NodeHandler<TState>,\n\t): this {\n\t\tif (name === START || name === END) {\n\t\t\tthrow new Error(\n\t\t\t\t`\"${name}\" is a reserved name and cannot be used as a node name`,\n\t\t\t);\n\t\t}\n\t\tif (this.nodes.has(name)) {\n\t\t\tthrow new Error(`Node \"${name}\" already exists`);\n\t\t}\n\n\t\tlet handler: NodeHandler<TState>;\n\t\tlet nodeConfig: NodeConfig<TState> = {};\n\n\t\tif (typeof configOrHandler === \"function\") {\n\t\t\t// addNode(name, handler)\n\t\t\thandler = configOrHandler;\n\t\t} else if (maybeHandler) {\n\t\t\t// addNode(name, nodeConfig, handler)\n\t\t\thandler = maybeHandler;\n\t\t\tnodeConfig = configOrHandler as NodeConfig<TState>;\n\t\t} else {\n\t\t\tthrow new Error(`Node \"${name}\" requires a handler function.`);\n\t\t}\n\n\t\tthis.nodes.set(name, handler);\n\t\tthis.nodeConfigs.set(name, nodeConfig);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add a direct edge between two nodes.\n\t *\n\t * Use `START` as `from` to set the entry point.\n\t * Use `END` as `to` to mark a terminal node.\n\t */\n\taddEdge(from: string, to: string): this {\n\t\tif (this.edges.has(from)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Node \"${from}\" already has an outgoing edge. Use addConditionalEdge for branching.`,\n\t\t\t);\n\t\t}\n\t\tthis.edges.set(from, { type: \"direct\", to });\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add a conditional edge from a node.\n\t *\n\t * The condition function receives current state and returns the name of the next node.\n\t */\n\taddConditionalEdge(from: string, condition: ConditionFn<TState>): this {\n\t\tif (this.edges.has(from)) {\n\t\t\tthrow new Error(`Node \"${from}\" already has an outgoing edge.`);\n\t\t}\n\t\tthis.edges.set(from, { type: \"conditional\", condition });\n\t\treturn this;\n\t}\n\n\t/**\n\t * Compile the graph into a RegisteredFlow that can be registered on an McpServer.\n\t *\n\t * Validates the graph structure and returns a registration-compatible object.\n\t */\n\tcompile(): RegisteredFlow {\n\t\tthis.validate();\n\n\t\treturn compileFlow<TState>({\n\t\t\tconfig: this.config,\n\t\t\tnodes: new Map(this.nodes),\n\t\t\tnodeConfigs: new Map(this.nodeConfigs),\n\t\t\tedges: new Map(this.edges),\n\t\t});\n\t}\n\n\tprivate validate(): void {\n\t\t// Must have a START edge\n\t\tif (!this.edges.has(START)) {\n\t\t\tthrow new Error(\n\t\t\t\t'Flow must have an entry point. Add an edge from START: .addEdge(START, \"first_node\")',\n\t\t\t);\n\t\t}\n\n\t\t// START edge target must exist\n\t\tconst startEdge = this.edges.get(START);\n\t\tif (\n\t\t\tstartEdge?.type === \"direct\" &&\n\t\t\tstartEdge.to !== END &&\n\t\t\t!this.nodes.has(startEdge.to)\n\t\t) {\n\t\t\tthrow new Error(\n\t\t\t\t`START edge references non-existent node: \"${startEdge.to}\"`,\n\t\t\t);\n\t\t}\n\n\t\t// All static edge targets must reference existing nodes (or END)\n\t\tfor (const [from, edge] of this.edges) {\n\t\t\tif (from !== START && !this.nodes.has(from)) {\n\t\t\t\tthrow new Error(`Edge from non-existent node: \"${from}\"`);\n\t\t\t}\n\t\t\tif (\n\t\t\t\tedge.type === \"direct\" &&\n\t\t\t\tedge.to !== END &&\n\t\t\t\t!this.nodes.has(edge.to)\n\t\t\t) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Edge from \"${from}\" references non-existent node: \"${edge.to}\"`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\t// Every node must have an outgoing edge\n\t\tfor (const [name] of this.nodes) {\n\t\t\tif (!this.edges.has(name)) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`Node \"${name}\" has no outgoing edge. Add one with .addEdge(\"${name}\", ...) or .addConditionalEdge(\"${name}\", ...)`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n}\n","import type { z } from \"zod\";\nimport type { FlowConfig, InferFlowState } from \"./@types\";\nimport { StateGraph } from \"./state-graph\";\n\n/**\n * Create a new flow graph — convenience factory for `new StateGraph()`.\n *\n * The state type is automatically inferred from the `state` definition —\n * no explicit generic parameter needed.\n *\n * @example\n * ```ts\n * import { createFlow, interrupt, START, END } from \"@waniwani/sdk/mcp\";\n * import { z } from \"zod\";\n *\n * const flow = createFlow({\n * id: \"onboarding\",\n * title: \"User Onboarding\",\n * description: \"Guides users through onboarding. Use when a user wants to get started.\",\n * state: {\n * name: z.string().describe(\"The user's name\"),\n * email: z.string().describe(\"The user's email address\"),\n * },\n * })\n * .addNode(\"ask_name\", () => interrupt({ question: \"What's your name?\", field: \"name\" }))\n * .addNode(\"ask_email\", () => interrupt({ question: \"What's your email?\", field: \"email\" }))\n * .addEdge(START, \"ask_name\")\n * .addEdge(\"ask_name\", \"ask_email\")\n * .addEdge(\"ask_email\", END)\n * .compile();\n * ```\n */\nexport function createFlow<const TSchema extends Record<string, z.ZodType>>(\n\tconfig: Omit<FlowConfig, \"state\"> & { state: TSchema },\n): StateGraph<InferFlowState<TSchema>> {\n\treturn new StateGraph<InferFlowState<TSchema>>(config);\n}\n","import {\n\tbuildMcpAppsResourceMeta,\n\tbuildOpenAIResourceMeta,\n\tfetchHtml,\n\tMIME_TYPE_MCP,\n\tMIME_TYPE_OPENAI,\n} from \"./meta\";\nimport type { McpServer, RegisteredResource, ResourceConfig } from \"./types\";\n\n/**\n * Creates a reusable UI resource (HTML template) that can be attached\n * to tools or flow nodes.\n *\n * @example\n * ```ts\n * const pricingUI = createResource({\n * id: \"pricing_table\",\n * title: \"Pricing Table\",\n * baseUrl: \"https://my-app.com\",\n * htmlPath: \"/widgets/pricing\",\n * widgetDomain: \"my-app.com\",\n * });\n *\n * await pricingUI.register(server);\n * ```\n */\nexport function createResource(config: ResourceConfig): RegisteredResource {\n\tconst {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\t\tbaseUrl,\n\t\thtmlPath,\n\t\twidgetDomain,\n\t\tprefersBorder = true,\n\t\tautoHeight = true,\n\t} = config;\n\n\t// Auto-generate CSP from baseUrl if not explicitly provided\n\tlet widgetCSP = config.widgetCSP ?? {\n\t\tconnect_domains: [baseUrl],\n\t\tresource_domains: [baseUrl],\n\t};\n\n\t// In development with localhost, add extra CSP domains for\n\t// Next.js dev features (WebSocket HMR, Turbopack font serving)\n\tif (process.env.NODE_ENV === \"development\") {\n\t\ttry {\n\t\t\tconst { hostname } = new URL(baseUrl);\n\t\t\tif (hostname === \"localhost\" || hostname === \"127.0.0.1\") {\n\t\t\t\twidgetCSP = {\n\t\t\t\t\t...widgetCSP,\n\t\t\t\t\tconnect_domains: [\n\t\t\t\t\t\t...(widgetCSP.connect_domains || []),\n\t\t\t\t\t\t`ws://${hostname}:*`,\n\t\t\t\t\t\t`wss://${hostname}:*`,\n\t\t\t\t\t],\n\t\t\t\t\tresource_domains: [\n\t\t\t\t\t\t...(widgetCSP.resource_domains || []),\n\t\t\t\t\t\t`http://${hostname}:*`,\n\t\t\t\t\t],\n\t\t\t\t};\n\t\t\t}\n\t\t} catch {\n\t\t\t// Invalid baseUrl — skip dev CSP additions\n\t\t}\n\t}\n\n\tconst openaiUri = `ui://widgets/apps-sdk/${id}.html`;\n\tconst mcpUri = `ui://widgets/ext-apps/${id}.html`;\n\n\t// Lazy HTML — fetched once, shared across all calls\n\tlet htmlPromise: Promise<string> | null = null;\n\tconst getHtml = () => {\n\t\tif (!htmlPromise) htmlPromise = fetchHtml(baseUrl, htmlPath);\n\t\treturn htmlPromise;\n\t};\n\n\t// Use description for UI metadata\n\tconst uiDescription = description;\n\n\tasync function register(server: McpServer): Promise<void> {\n\t\tconst html = await getHtml();\n\n\t\t// Register OpenAI Apps SDK resource\n\t\tserver.registerResource(\n\t\t\t`${id}-openai-widget`,\n\t\t\topenaiUri,\n\t\t\t{\n\t\t\t\ttitle,\n\t\t\t\tdescription: uiDescription,\n\t\t\t\tmimeType: MIME_TYPE_OPENAI,\n\t\t\t\t_meta: {\n\t\t\t\t\t\"openai/widgetDescription\": uiDescription,\n\t\t\t\t\t\"openai/widgetPrefersBorder\": prefersBorder,\n\t\t\t\t},\n\t\t\t},\n\t\t\tasync (uri) => ({\n\t\t\t\tcontents: [\n\t\t\t\t\t{\n\t\t\t\t\t\turi: uri.href,\n\t\t\t\t\t\tmimeType: MIME_TYPE_OPENAI,\n\t\t\t\t\t\ttext: html,\n\t\t\t\t\t\t_meta: buildOpenAIResourceMeta({\n\t\t\t\t\t\t\tdescription: uiDescription,\n\t\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t\t\twidgetDomain,\n\t\t\t\t\t\t\twidgetCSP,\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}),\n\t\t);\n\n\t\t// Register MCP Apps resource\n\t\tserver.registerResource(\n\t\t\t`${id}-mcp-widget`,\n\t\t\tmcpUri,\n\t\t\t{\n\t\t\t\ttitle,\n\t\t\t\tdescription: uiDescription,\n\t\t\t\tmimeType: MIME_TYPE_MCP,\n\t\t\t\t_meta: {\n\t\t\t\t\tui: {\n\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tasync (uri) => ({\n\t\t\t\tcontents: [\n\t\t\t\t\t{\n\t\t\t\t\t\turi: uri.href,\n\t\t\t\t\t\tmimeType: MIME_TYPE_MCP,\n\t\t\t\t\t\ttext: html,\n\t\t\t\t\t\t_meta: buildMcpAppsResourceMeta({\n\t\t\t\t\t\t\tdescription: uiDescription,\n\t\t\t\t\t\t\tprefersBorder,\n\t\t\t\t\t\t\twidgetCSP,\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t}),\n\t\t);\n\t}\n\n\treturn {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\t\topenaiUri,\n\t\tmcpUri,\n\t\tautoHeight,\n\t\tregister,\n\t};\n}\n","import type { ToolCallback } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport type { ShapeOutput } from \"@modelcontextprotocol/sdk/server/zod-compat.js\";\nimport type { RequestHandlerExtra } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport type {\n\tServerNotification,\n\tServerRequest,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport type { z } from \"zod\";\nimport { buildToolMeta } from \"../resources/meta\";\nimport type {\n\tMcpServer,\n\tRegisteredTool,\n\tToolConfig,\n\tToolHandler,\n} from \"./types\";\n\n/**\n * Creates an MCP tool with minimal boilerplate.\n *\n * When `config.resource` is provided, the tool returns `structuredContent` + widget metadata.\n * Without a resource, the tool returns plain text content.\n *\n * @example\n * ```ts\n * // Widget tool (with resource)\n * const pricingTool = createTool({\n * resource: pricingUI,\n * description: \"Show pricing comparison\",\n * inputSchema: { postalCode: z.string() },\n * }, async ({ postalCode }) => ({\n * text: \"Pricing loaded\",\n * data: { postalCode, prices: [] },\n * }));\n *\n * // Plain tool (no resource)\n * const searchTool = createTool({\n * id: \"search\",\n * title: \"Search\",\n * description: \"Search the knowledge base\",\n * inputSchema: { query: z.string() },\n * }, async ({ query }) => ({\n * text: `Results for \"${query}\"`,\n * }));\n * ```\n */\nexport function createTool<TInput extends z.ZodRawShape>(\n\tconfig: ToolConfig<TInput>,\n\thandler: ToolHandler<TInput>,\n): RegisteredTool {\n\tconst { resource, description, inputSchema, annotations } = config;\n\n\tconst id = config.id ?? resource?.id;\n\tconst title = config.title ?? resource?.title;\n\n\tif (!id) {\n\t\tthrow new Error(\n\t\t\t\"createTool: `id` is required when no resource is provided\",\n\t\t);\n\t}\n\tif (!title) {\n\t\tthrow new Error(\n\t\t\t\"createTool: `title` is required when no resource is provided\",\n\t\t);\n\t}\n\n\t// Build widget metadata only when resource is present\n\tconst toolMeta = resource\n\t\t? buildToolMeta({\n\t\t\t\topenaiTemplateUri: resource.openaiUri,\n\t\t\t\tmcpTemplateUri: resource.mcpUri,\n\t\t\t\tinvoking: config.invoking ?? \"Loading...\",\n\t\t\t\tinvoked: config.invoked ?? \"Loaded\",\n\t\t\t\tautoHeight: resource.autoHeight,\n\t\t\t})\n\t\t: undefined;\n\n\treturn {\n\t\tid,\n\t\ttitle,\n\t\tdescription,\n\n\t\tasync register(server: McpServer): Promise<void> {\n\t\t\tserver.registerTool(\n\t\t\t\tid,\n\t\t\t\t{\n\t\t\t\t\ttitle,\n\t\t\t\t\tdescription,\n\t\t\t\t\tinputSchema,\n\t\t\t\t\tannotations,\n\t\t\t\t\t...(toolMeta && { _meta: toolMeta }),\n\t\t\t\t},\n\t\t\t\t(async (args: ShapeOutput<TInput>, extra: unknown) => {\n\t\t\t\t\tconst requestExtra = extra as RequestHandlerExtra<\n\t\t\t\t\t\tServerRequest,\n\t\t\t\t\t\tServerNotification\n\t\t\t\t\t>;\n\t\t\t\t\tconst _meta: Record<string, unknown> = requestExtra._meta ?? {};\n\n\t\t\t\t\tconst result = await handler(args, { extra: { _meta } });\n\n\t\t\t\t\t// Widget tool: return structuredContent + widget metadata\n\t\t\t\t\tif (resource && result.data) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: result.text }],\n\t\t\t\t\t\t\tstructuredContent: result.data,\n\t\t\t\t\t\t\t_meta: {\n\t\t\t\t\t\t\t\t...toolMeta,\n\t\t\t\t\t\t\t\t..._meta,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Plain tool: return text content only\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: result.text }],\n\t\t\t\t\t};\n\t\t\t\t}) as unknown as ToolCallback<TInput>,\n\t\t\t);\n\t\t},\n\t};\n}\n\n/**\n * Registers multiple tools on the server\n */\nexport async function registerTools(\n\tserver: McpServer,\n\ttools: RegisteredTool[],\n): Promise<void> {\n\tawait Promise.all(tools.map((t) => t.register(server)));\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// KB Client — thin HTTP wrapper for knowledge base API\n\nimport { WaniWaniError } from \"../error.js\";\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tKbClient,\n\tKbIngestFile,\n\tKbIngestResult,\n\tKbSearchOptions,\n\tKbSource,\n\tSearchResult,\n} from \"./types.js\";\n\nconst SDK_NAME = \"@waniwani/sdk\";\n\nexport function createKbClient(config: InternalConfig): KbClient {\n\tconst { baseUrl, apiKey } = config;\n\n\tfunction requireApiKey(): string {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(\"WANIWANI_API_KEY is not set\");\n\t\t}\n\t\treturn apiKey;\n\t}\n\n\tasync function request<T>(\n\t\tmethod: \"GET\" | \"POST\",\n\t\tpath: string,\n\t\tbody?: unknown,\n\t): Promise<T> {\n\t\tconst key = requireApiKey();\n\t\tconst url = `${baseUrl.replace(/\\/$/, \"\")}${path}`;\n\n\t\tconst headers: Record<string, string> = {\n\t\t\tAuthorization: `Bearer ${key}`,\n\t\t\t\"X-WaniWani-SDK\": SDK_NAME,\n\t\t};\n\n\t\tconst init: RequestInit = { method, headers };\n\n\t\tif (body !== undefined) {\n\t\t\theaders[\"Content-Type\"] = \"application/json\";\n\t\t\tinit.body = JSON.stringify(body);\n\t\t}\n\n\t\tconst response = await fetch(url, init);\n\n\t\tif (!response.ok) {\n\t\t\tconst text = await response.text().catch(() => \"\");\n\t\t\tthrow new WaniWaniError(\n\t\t\t\ttext || `KB API error: HTTP ${response.status}`,\n\t\t\t\tresponse.status,\n\t\t\t);\n\t\t}\n\n\t\tconst json = (await response.json()) as { data: T };\n\t\treturn json.data;\n\t}\n\n\treturn {\n\t\tasync ingest(files: KbIngestFile[]): Promise<KbIngestResult> {\n\t\t\treturn request<KbIngestResult>(\"POST\", \"/api/mcp/kb/ingest\", {\n\t\t\t\tfiles,\n\t\t\t});\n\t\t},\n\n\t\tasync search(\n\t\t\tquery: string,\n\t\t\toptions?: KbSearchOptions,\n\t\t): Promise<SearchResult[]> {\n\t\t\treturn request<SearchResult[]>(\"POST\", \"/api/mcp/kb/search\", {\n\t\t\t\tquery,\n\t\t\t\t...options,\n\t\t\t});\n\t\t},\n\n\t\tasync sources(): Promise<KbSource[]> {\n\t\t\treturn request<KbSource[]>(\"GET\", \"/api/mcp/kb/sources\");\n\t\t},\n\t};\n}\n","import type { EventType, LegacyTrackEvent, TrackInput } from \"./@types.js\";\nimport type { V2CorrelationIds, V2EventEnvelope } from \"./v2-types.js\";\n\nconst DEFAULT_SOURCE = \"@waniwani/sdk\";\n\nexport interface MapTrackEventOptions {\n\tnow?: () => Date;\n\tgenerateId?: () => string;\n\tsource?: string;\n}\n\nexport function mapTrackEventToV2(\n\tinput: TrackInput,\n\toptions: MapTrackEventOptions = {},\n): V2EventEnvelope {\n\tconst now = options.now ?? (() => new Date());\n\tconst generateId = options.generateId ?? createEventId;\n\tconst eventName = resolveEventName(input);\n\tconst meta = toRecord(input.meta);\n\tconst metadata = toRecord(input.metadata);\n\tconst correlation = resolveCorrelationIds(input, meta);\n\tconst eventId = takeNonEmptyString(input.eventId) ?? generateId();\n\tconst timestamp = normalizeTimestamp(input.timestamp, now);\n\tconst source =\n\t\ttakeNonEmptyString(input.source) ?? options.source ?? DEFAULT_SOURCE;\n\tconst rawLegacy = isLegacyTrackEvent(input) ? { ...input } : undefined;\n\n\tconst mappedMetadata: Record<string, unknown> = {\n\t\t...metadata,\n\t};\n\tif (Object.keys(meta).length > 0) mappedMetadata.meta = meta;\n\tif (rawLegacy) mappedMetadata.rawLegacy = rawLegacy;\n\n\treturn {\n\t\tid: eventId,\n\t\ttype: \"mcp.event\",\n\t\tname: eventName,\n\t\tsource,\n\t\ttimestamp,\n\t\tcorrelation,\n\t\tproperties: mapProperties(input, eventName),\n\t\tmetadata: mappedMetadata,\n\t\trawLegacy,\n\t};\n}\n\nexport function createEventId(): string {\n\tif (\n\t\ttypeof crypto !== \"undefined\" &&\n\t\ttypeof crypto.randomUUID === \"function\"\n\t) {\n\t\treturn `evt_${crypto.randomUUID()}`;\n\t}\n\n\treturn `evt_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`;\n}\n\nfunction mapProperties(\n\tinput: TrackInput,\n\teventName: EventType,\n): Record<string, unknown> {\n\tif (!isLegacyTrackEvent(input)) {\n\t\treturn toRecord(input.properties);\n\t}\n\n\tconst legacyProperties = mapLegacyProperties(input, eventName);\n\tconst explicitProperties = toRecord(input.properties);\n\treturn {\n\t\t...legacyProperties,\n\t\t...explicitProperties,\n\t};\n}\n\nfunction mapLegacyProperties(\n\tinput: LegacyTrackEvent,\n\teventName: EventType,\n): Record<string, unknown> {\n\tswitch (eventName) {\n\t\tcase \"tool.called\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (takeNonEmptyString(input.toolName)) properties.name = input.toolName;\n\t\t\tif (takeNonEmptyString(input.toolType)) properties.type = input.toolType;\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"quote.succeeded\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (typeof input.quoteAmount === \"number\") {\n\t\t\t\tproperties.amount = input.quoteAmount;\n\t\t\t}\n\t\t\tif (takeNonEmptyString(input.quoteCurrency)) {\n\t\t\t\tproperties.currency = input.quoteCurrency;\n\t\t\t}\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"link.clicked\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (takeNonEmptyString(input.linkUrl)) properties.url = input.linkUrl;\n\t\t\treturn properties;\n\t\t}\n\t\tcase \"purchase.completed\": {\n\t\t\tconst properties: Record<string, unknown> = {};\n\t\t\tif (typeof input.purchaseAmount === \"number\") {\n\t\t\t\tproperties.amount = input.purchaseAmount;\n\t\t\t}\n\t\t\tif (takeNonEmptyString(input.purchaseCurrency)) {\n\t\t\t\tproperties.currency = input.purchaseCurrency;\n\t\t\t}\n\t\t\treturn properties;\n\t\t}\n\t\tdefault:\n\t\t\treturn {};\n\t}\n}\n\nfunction resolveEventName(input: TrackInput): EventType {\n\tif (isLegacyTrackEvent(input)) return input.eventType;\n\treturn input.event;\n}\n\nfunction resolveCorrelationIds(\n\tinput: TrackInput,\n\tmeta: Record<string, unknown>,\n): V2CorrelationIds {\n\tconst requestId =\n\t\ttakeNonEmptyString(input.requestId) ??\n\t\tpickFirstString(meta, [\"openai/requestId\", \"requestId\", \"mcp/requestId\"]);\n\n\tconst sessionId =\n\t\ttakeNonEmptyString(input.sessionId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/sessionId\",\n\t\t\t\"sessionId\",\n\t\t\t\"conversationId\",\n\t\t\t\"anthropic/sessionId\",\n\t\t]);\n\n\tconst traceId =\n\t\ttakeNonEmptyString(input.traceId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/traceId\",\n\t\t\t\"traceId\",\n\t\t\t\"mcp/traceId\",\n\t\t\t\"openai/requestId\",\n\t\t\t\"requestId\",\n\t\t]);\n\n\tconst externalUserId =\n\t\ttakeNonEmptyString(input.externalUserId) ??\n\t\tpickFirstString(meta, [\n\t\t\t\"openai/userId\",\n\t\t\t\"externalUserId\",\n\t\t\t\"userId\",\n\t\t\t\"actorId\",\n\t\t]);\n\n\tconst correlationId =\n\t\ttakeNonEmptyString(input.correlationId) ??\n\t\tpickFirstString(meta, [\"correlationId\", \"openai/requestId\"]) ??\n\t\trequestId;\n\n\tconst correlation: V2CorrelationIds = {};\n\tif (sessionId) correlation.sessionId = sessionId;\n\tif (traceId) correlation.traceId = traceId;\n\tif (requestId) correlation.requestId = requestId;\n\tif (correlationId) correlation.correlationId = correlationId;\n\tif (externalUserId) correlation.externalUserId = externalUserId;\n\treturn correlation;\n}\n\nfunction normalizeTimestamp(\n\tinput: string | Date | undefined,\n\tnow: () => Date,\n): string {\n\tif (input instanceof Date) return input.toISOString();\n\tif (typeof input === \"string\") {\n\t\tconst date = new Date(input);\n\t\tif (!Number.isNaN(date.getTime())) return date.toISOString();\n\t}\n\treturn now().toISOString();\n}\n\nfunction pickFirstString(\n\trecord: Record<string, unknown>,\n\tkeys: readonly string[],\n): string | undefined {\n\tfor (const key of keys) {\n\t\tconst value = record[key];\n\t\tif (typeof value === \"string\" && value.trim().length > 0) {\n\t\t\treturn value;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nfunction toRecord(value: unknown): Record<string, unknown> {\n\tif (!value || typeof value !== \"object\" || Array.isArray(value)) {\n\t\treturn {};\n\t}\n\treturn value as Record<string, unknown>;\n}\n\nfunction takeNonEmptyString(value: unknown): string | undefined {\n\tif (typeof value !== \"string\") return undefined;\n\tif (value.trim().length === 0) return undefined;\n\treturn value;\n}\n\nfunction isLegacyTrackEvent(input: TrackInput): input is LegacyTrackEvent {\n\treturn \"eventType\" in input;\n}\n","import type {\n\tTrackingShutdownOptions,\n\tTrackingShutdownResult,\n} from \"./@types.js\";\nimport type {\n\tV2BatchRejectedEvent,\n\tV2BatchRequest,\n\tV2BatchResponse,\n\tV2EventEnvelope,\n} from \"./v2-types.js\";\n\nconst DEFAULT_ENDPOINT_PATH = \"/api/mcp/events/v2/batch\";\nconst DEFAULT_FLUSH_INTERVAL_MS = 1_000;\nconst DEFAULT_MAX_BATCH_SIZE = 20;\nconst DEFAULT_MAX_BUFFER_SIZE = 1_000;\nconst DEFAULT_MAX_RETRIES = 3;\nconst DEFAULT_RETRY_BASE_DELAY_MS = 200;\nconst DEFAULT_RETRY_MAX_DELAY_MS = 2_000;\nconst DEFAULT_SHUTDOWN_TIMEOUT_MS = 2_000;\nconst SDK_NAME = \"@waniwani/sdk\";\n\nconst AUTH_FAILURE_STATUS = new Set([401, 403]);\nconst RETRYABLE_STATUS = new Set([408, 425, 429, 500, 502, 503, 504]);\n\ninterface Logger {\n\twarn: (message: string, ...args: unknown[]) => void;\n\terror: (message: string, ...args: unknown[]) => void;\n}\n\nexport interface V2TransportOptions {\n\tbaseUrl: string;\n\tapiKey: string;\n\tendpointPath?: string;\n\tflushIntervalMs?: number;\n\tmaxBatchSize?: number;\n\tmaxBufferSize?: number;\n\tmaxRetries?: number;\n\tretryBaseDelayMs?: number;\n\tretryMaxDelayMs?: number;\n\tshutdownTimeoutMs?: number;\n\tsdkVersion?: string;\n\tfetchFn?: typeof fetch;\n\tlogger?: Logger;\n\tnow?: () => Date;\n\tsleep?: (delayMs: number) => Promise<void>;\n}\n\nexport interface V2BatchTransport {\n\tenqueue: (event: V2EventEnvelope) => void;\n\tflush: () => Promise<void>;\n\tshutdown: (\n\t\toptions?: TrackingShutdownOptions,\n\t) => Promise<TrackingShutdownResult>;\n\tpendingEvents: () => number;\n}\n\ntype SendBatchResult =\n\t| { kind: \"success\" }\n\t| { kind: \"retryable\"; reason: string }\n\t| { kind: \"permanent\"; reason: string }\n\t| { kind: \"auth\"; status: number }\n\t| {\n\t\t\tkind: \"partial\";\n\t\t\tretryable: V2EventEnvelope[];\n\t\t\tpermanent: V2EventEnvelope[];\n\t };\n\nexport function createV2BatchTransport(\n\toptions: V2TransportOptions,\n): V2BatchTransport {\n\treturn new BatchingV2Transport(options);\n}\n\nclass BatchingV2Transport implements V2BatchTransport {\n\tprivate readonly endpointUrl: string;\n\tprivate readonly flushIntervalMs: number;\n\tprivate readonly maxBatchSize: number;\n\tprivate readonly maxBufferSize: number;\n\tprivate readonly maxRetries: number;\n\tprivate readonly retryBaseDelayMs: number;\n\tprivate readonly retryMaxDelayMs: number;\n\tprivate readonly shutdownTimeoutMs: number;\n\tprivate readonly sdkVersion?: string;\n\tprivate readonly fetchFn: typeof fetch;\n\tprivate readonly logger: Logger;\n\tprivate readonly now: () => Date;\n\tprivate readonly sleep: (delayMs: number) => Promise<void>;\n\tprivate readonly apiKey: string;\n\n\tprivate readonly buffer: V2EventEnvelope[] = [];\n\tprivate flushTimer: ReturnType<typeof setInterval> | undefined;\n\tprivate flushScheduled = false;\n\tprivate flushScheduledTimer: ReturnType<typeof setTimeout> | undefined;\n\tprivate flushInFlight: Promise<void> | undefined;\n\tprivate inFlightCount = 0;\n\tprivate isStopped = false;\n\tprivate isShuttingDown = false;\n\n\tconstructor(options: V2TransportOptions) {\n\t\tthis.endpointUrl = joinUrl(\n\t\t\toptions.baseUrl,\n\t\t\toptions.endpointPath ?? DEFAULT_ENDPOINT_PATH,\n\t\t);\n\t\tthis.flushIntervalMs = options.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS;\n\t\tthis.maxBatchSize = options.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE;\n\t\tthis.maxBufferSize = options.maxBufferSize ?? DEFAULT_MAX_BUFFER_SIZE;\n\t\tthis.maxRetries = options.maxRetries ?? DEFAULT_MAX_RETRIES;\n\t\tthis.retryBaseDelayMs =\n\t\t\toptions.retryBaseDelayMs ?? DEFAULT_RETRY_BASE_DELAY_MS;\n\t\tthis.retryMaxDelayMs =\n\t\t\toptions.retryMaxDelayMs ?? DEFAULT_RETRY_MAX_DELAY_MS;\n\t\tthis.shutdownTimeoutMs =\n\t\t\toptions.shutdownTimeoutMs ?? DEFAULT_SHUTDOWN_TIMEOUT_MS;\n\t\tthis.fetchFn = options.fetchFn ?? fetch;\n\t\tthis.logger = options.logger ?? console;\n\t\tthis.now = options.now ?? (() => new Date());\n\t\tthis.sleep =\n\t\t\toptions.sleep ??\n\t\t\t((delayMs) => new Promise((resolve) => setTimeout(resolve, delayMs)));\n\t\tthis.apiKey = options.apiKey;\n\t\tthis.sdkVersion = options.sdkVersion;\n\n\t\tif (this.flushIntervalMs > 0) {\n\t\t\tthis.flushTimer = setInterval(() => {\n\t\t\t\tvoid this.flush();\n\t\t\t}, this.flushIntervalMs);\n\t\t}\n\t}\n\n\tenqueue(event: V2EventEnvelope): void {\n\t\tif (this.isStopped || this.isShuttingDown) {\n\t\t\tthis.logger.warn(\n\t\t\t\t\"[WaniWani] Tracking transport is stopped, dropping event %s\",\n\t\t\t\tevent.id,\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif (this.buffer.length >= this.maxBufferSize) {\n\t\t\tconst dropCount = this.buffer.length - this.maxBufferSize + 1;\n\t\t\tthis.buffer.splice(0, dropCount);\n\t\t\tthis.logger.warn(\n\t\t\t\t\"[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)\",\n\t\t\t\tdropCount,\n\t\t\t);\n\t\t}\n\n\t\tthis.buffer.push(event);\n\n\t\tif (this.buffer.length >= this.maxBatchSize) {\n\t\t\tvoid this.flush();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.scheduleMicroFlush();\n\t}\n\n\tpendingEvents(): number {\n\t\treturn this.buffer.length + this.inFlightCount;\n\t}\n\n\tasync flush(): Promise<void> {\n\t\tif (this.flushInFlight) return this.flushInFlight;\n\t\tthis.flushInFlight = this.flushLoop().finally(() => {\n\t\t\tthis.flushInFlight = undefined;\n\t\t});\n\t\treturn this.flushInFlight;\n\t}\n\n\tasync shutdown(\n\t\toptions?: TrackingShutdownOptions,\n\t): Promise<TrackingShutdownResult> {\n\t\tthis.isShuttingDown = true;\n\t\tif (this.flushTimer) {\n\t\t\tclearInterval(this.flushTimer);\n\t\t\tthis.flushTimer = undefined;\n\t\t}\n\t\tif (this.flushScheduledTimer) {\n\t\t\tclearTimeout(this.flushScheduledTimer);\n\t\t\tthis.flushScheduledTimer = undefined;\n\t\t\tthis.flushScheduled = false;\n\t\t}\n\n\t\tconst timeoutMs = options?.timeoutMs ?? this.shutdownTimeoutMs;\n\t\tconst flushPromise = this.flush();\n\n\t\tif (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {\n\t\t\tawait flushPromise;\n\t\t\tthis.isStopped = true;\n\t\t\treturn { timedOut: false, pendingEvents: this.pendingEvents() };\n\t\t}\n\n\t\tconst timeoutSignal = Symbol(\"shutdown-timeout\");\n\t\tconst result = await Promise.race([\n\t\t\tflushPromise.then(() => \"flushed\" as const),\n\t\t\tthis.sleep(timeoutMs).then(() => timeoutSignal),\n\t\t]);\n\n\t\tif (result === timeoutSignal) {\n\t\t\tthis.isStopped = true;\n\t\t\treturn { timedOut: true, pendingEvents: this.pendingEvents() };\n\t\t}\n\n\t\tthis.isStopped = true;\n\t\treturn { timedOut: false, pendingEvents: this.pendingEvents() };\n\t}\n\n\tprivate scheduleMicroFlush(): void {\n\t\tif (this.flushScheduled) return;\n\t\tthis.flushScheduled = true;\n\t\tthis.flushScheduledTimer = setTimeout(() => {\n\t\t\tthis.flushScheduledTimer = undefined;\n\t\t\tthis.flushScheduled = false;\n\t\t\tvoid this.flush();\n\t\t}, 0);\n\t}\n\n\tprivate async flushLoop(): Promise<void> {\n\t\twhile (this.buffer.length > 0 && !this.isStopped) {\n\t\t\tconst batch = this.buffer.splice(0, this.maxBatchSize);\n\t\t\tawait this.sendBatchWithRetry(batch);\n\t\t}\n\t}\n\n\tprivate async sendBatchWithRetry(batch: V2EventEnvelope[]): Promise<void> {\n\t\tlet attempt = 0;\n\t\tlet pendingBatch = batch;\n\n\t\twhile (pendingBatch.length > 0 && !this.isStopped) {\n\t\t\tthis.inFlightCount = pendingBatch.length;\n\t\t\tconst result = await this.sendBatchOnce(pendingBatch);\n\t\t\tthis.inFlightCount = 0;\n\n\t\t\tswitch (result.kind) {\n\t\t\t\tcase \"success\":\n\t\t\t\t\treturn;\n\t\t\t\tcase \"auth\":\n\t\t\t\t\tthis.stopTransportForAuthFailure(result.status, pendingBatch.length);\n\t\t\t\t\treturn;\n\t\t\t\tcase \"permanent\":\n\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) after permanent failure: %s\",\n\t\t\t\t\t\tpendingBatch.length,\n\t\t\t\t\t\tresult.reason,\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\tcase \"retryable\":\n\t\t\t\t\tif (attempt >= this.maxRetries) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) after retry exhaustion: %s\",\n\t\t\t\t\t\t\tpendingBatch.length,\n\t\t\t\t\t\t\tresult.reason,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tawait this.sleep(this.backoffDelayMs(attempt));\n\t\t\t\t\tattempt += 1;\n\t\t\t\t\tcontinue;\n\t\t\t\tcase \"partial\":\n\t\t\t\t\tif (result.permanent.length > 0) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d event(s) rejected as permanent\",\n\t\t\t\t\t\t\tresult.permanent.length,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tif (result.retryable.length === 0) return;\n\t\t\t\t\tif (attempt >= this.maxRetries) {\n\t\t\t\t\t\tthis.logger.error(\n\t\t\t\t\t\t\t\"[WaniWani] Dropping %d retryable event(s) after retry exhaustion\",\n\t\t\t\t\t\t\tresult.retryable.length,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tpendingBatch = result.retryable;\n\t\t\t\t\tawait this.sleep(this.backoffDelayMs(attempt));\n\t\t\t\t\tattempt += 1;\n\t\t\t\t\tcontinue;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate async sendBatchOnce(\n\t\tevents: V2EventEnvelope[],\n\t): Promise<SendBatchResult> {\n\t\tlet response: Response;\n\n\t\ttry {\n\t\t\tresponse = await this.fetchFn(this.endpointUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\tAuthorization: `Bearer ${this.apiKey}`,\n\t\t\t\t\t\"X-WaniWani-SDK\": SDK_NAME,\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify(this.makeBatchRequest(events)),\n\t\t\t});\n\t\t} catch (error) {\n\t\t\treturn {\n\t\t\t\tkind: \"retryable\",\n\t\t\t\treason: getErrorMessage(error),\n\t\t\t};\n\t\t}\n\n\t\tif (AUTH_FAILURE_STATUS.has(response.status)) {\n\t\t\treturn { kind: \"auth\", status: response.status };\n\t\t}\n\n\t\tif (RETRYABLE_STATUS.has(response.status)) {\n\t\t\treturn {\n\t\t\t\tkind: \"retryable\",\n\t\t\t\treason: `HTTP ${response.status}`,\n\t\t\t};\n\t\t}\n\n\t\tif (!response.ok) {\n\t\t\treturn {\n\t\t\t\tkind: \"permanent\",\n\t\t\t\treason: `HTTP ${response.status}`,\n\t\t\t};\n\t\t}\n\n\t\tconst data = await parseJsonResponse<V2BatchResponse>(response);\n\t\tif (!data?.rejected || data.rejected.length === 0) {\n\t\t\treturn { kind: \"success\" };\n\t\t}\n\n\t\tconst partial = this.classifyRejectedEvents(events, data.rejected);\n\t\tif (partial.retryable.length === 0 && partial.permanent.length === 0) {\n\t\t\treturn { kind: \"success\" };\n\t\t}\n\n\t\treturn {\n\t\t\tkind: \"partial\",\n\t\t\tretryable: partial.retryable,\n\t\t\tpermanent: partial.permanent,\n\t\t};\n\t}\n\n\tprivate makeBatchRequest(events: V2EventEnvelope[]): V2BatchRequest {\n\t\treturn {\n\t\t\tsentAt: this.now().toISOString(),\n\t\t\tsource: {\n\t\t\t\tsdk: SDK_NAME,\n\t\t\t\tversion: this.sdkVersion ?? \"0.0.0\",\n\t\t\t},\n\t\t\tevents,\n\t\t};\n\t}\n\n\tprivate classifyRejectedEvents(\n\t\tevents: V2EventEnvelope[],\n\t\trejected: V2BatchRejectedEvent[],\n\t): {\n\t\tretryable: V2EventEnvelope[];\n\t\tpermanent: V2EventEnvelope[];\n\t} {\n\t\tconst byId = new Map(events.map((event) => [event.id, event]));\n\t\tconst retryable: V2EventEnvelope[] = [];\n\t\tconst permanent: V2EventEnvelope[] = [];\n\n\t\tfor (const rejectedEvent of rejected) {\n\t\t\tconst event = byId.get(rejectedEvent.eventId);\n\t\t\tif (!event) continue;\n\t\t\tif (isRetryableRejectedEvent(rejectedEvent)) {\n\t\t\t\tretryable.push(event);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tpermanent.push(event);\n\t\t}\n\n\t\treturn { retryable, permanent };\n\t}\n\n\tprivate backoffDelayMs(attempt: number): number {\n\t\tconst rawDelay = this.retryBaseDelayMs * 2 ** attempt;\n\t\treturn Math.min(rawDelay, this.retryMaxDelayMs);\n\t}\n\n\tprivate stopTransportForAuthFailure(\n\t\tstatus: number,\n\t\trejectedCount: number,\n\t): void {\n\t\tthis.isStopped = true;\n\t\tconst buffered = this.buffer.length;\n\t\tthis.buffer.splice(0, buffered);\n\t\tthis.logger.error(\n\t\t\t\"[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)\",\n\t\t\tstatus,\n\t\t\trejectedCount + buffered,\n\t\t);\n\t}\n}\n\nfunction isRetryableRejectedEvent(\n\trejectedEvent: V2BatchRejectedEvent,\n): boolean {\n\tif (rejectedEvent.retryable === true) return true;\n\tconst code = rejectedEvent.code.toLowerCase();\n\treturn (\n\t\tcode.includes(\"timeout\") ||\n\t\tcode.includes(\"temporary\") ||\n\t\tcode.includes(\"unavailable\") ||\n\t\tcode.includes(\"rate_limit\") ||\n\t\tcode.includes(\"transient\") ||\n\t\tcode.includes(\"server\")\n\t);\n}\n\nasync function parseJsonResponse<T>(\n\tresponse: Response,\n): Promise<T | undefined> {\n\tconst body = await response.text();\n\tif (!body) return undefined;\n\ttry {\n\t\treturn JSON.parse(body) as T;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction joinUrl(baseUrl: string, endpointPath: string): string {\n\tconst normalizedBase = baseUrl.endsWith(\"/\") ? baseUrl : `${baseUrl}/`;\n\tconst normalizedPath = endpointPath.startsWith(\"/\")\n\t\t? endpointPath.slice(1)\n\t\t: endpointPath;\n\treturn `${normalizedBase}${normalizedPath}`;\n}\n\nfunction getErrorMessage(error: unknown): string {\n\tif (error instanceof Error) return error.message;\n\treturn String(error);\n}\n","// Tracking Module\n\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tTrackInput,\n\tTrackingClient,\n\tTrackingShutdownOptions,\n} from \"./@types.js\";\nimport { mapTrackEventToV2 } from \"./mapper.js\";\nimport { createV2BatchTransport } from \"./transport.js\";\n\n// Re-export types\nexport type {\n\tEventType,\n\tLegacyTrackEvent,\n\tLinkClickedProperties,\n\tPurchaseCompletedProperties,\n\tQuoteSucceededProperties,\n\tToolCalledProperties,\n\tTrackEvent,\n\tTrackInput,\n\tTrackingClient,\n\tTrackingConfig,\n\tTrackingShutdownOptions,\n\tTrackingShutdownResult,\n} from \"./@types.js\";\nexport { createEventId, mapTrackEventToV2 } from \"./mapper.js\";\nexport type {\n\tV2BatchRejectedEvent,\n\tV2BatchRequest,\n\tV2BatchResponse,\n\tV2CorrelationIds,\n\tV2EnvelopeType,\n\tV2EventEnvelope,\n} from \"./v2-types.js\";\n\nexport function createTrackingClient(config: InternalConfig): TrackingClient {\n\tconst { baseUrl, apiKey, tracking } = config;\n\n\tfunction requireApiKey(): string {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(\"WANIWANI_API_KEY is not set\");\n\t\t}\n\t\treturn apiKey;\n\t}\n\n\tconst transport = apiKey\n\t\t? createV2BatchTransport({\n\t\t\t\tbaseUrl,\n\t\t\t\tapiKey,\n\t\t\t\tendpointPath: tracking.endpointPath,\n\t\t\t\tflushIntervalMs: tracking.flushIntervalMs,\n\t\t\t\tmaxBatchSize: tracking.maxBatchSize,\n\t\t\t\tmaxBufferSize: tracking.maxBufferSize,\n\t\t\t\tmaxRetries: tracking.maxRetries,\n\t\t\t\tretryBaseDelayMs: tracking.retryBaseDelayMs,\n\t\t\t\tretryMaxDelayMs: tracking.retryMaxDelayMs,\n\t\t\t\tshutdownTimeoutMs: tracking.shutdownTimeoutMs,\n\t\t\t})\n\t\t: undefined;\n\n\tconst client: TrackingClient = {\n\t\tasync track(event: TrackInput): Promise<{ eventId: string }> {\n\t\t\trequireApiKey();\n\t\t\tconst mappedEvent = mapTrackEventToV2(event);\n\t\t\ttransport?.enqueue(mappedEvent);\n\t\t\treturn { eventId: mappedEvent.id };\n\t\t},\n\t\tasync flush(): Promise<void> {\n\t\t\trequireApiKey();\n\t\t\tawait transport?.flush();\n\t\t},\n\t\tasync shutdown(options?: TrackingShutdownOptions) {\n\t\t\trequireApiKey();\n\t\t\treturn (\n\t\t\t\t(await transport?.shutdown({\n\t\t\t\t\ttimeoutMs: options?.timeoutMs ?? tracking.shutdownTimeoutMs,\n\t\t\t\t})) ?? { timedOut: false, pendingEvents: 0 }\n\t\t\t);\n\t\t},\n\t};\n\n\tif (transport) {\n\t\tattachShutdownHooks(client, tracking.shutdownTimeoutMs);\n\t}\n\treturn client;\n}\n\nfunction attachShutdownHooks(\n\tclient: TrackingClient,\n\tdefaultTimeoutMs: number,\n): void {\n\tif (\n\t\ttypeof process === \"undefined\" ||\n\t\ttypeof process.once !== \"function\" ||\n\t\ttypeof process.on !== \"function\"\n\t) {\n\t\treturn;\n\t}\n\n\tconst shutdown = () => {\n\t\tvoid client.shutdown({ timeoutMs: defaultTimeoutMs });\n\t};\n\n\tprocess.once(\"beforeExit\", shutdown);\n\tprocess.once(\"SIGINT\", shutdown);\n\tprocess.once(\"SIGTERM\", shutdown);\n}\n","// WaniWani SDK - Main Entry\n\nimport { createKbClient } from \"./kb/client.js\";\nimport { createTrackingClient } from \"./tracking/index.js\";\nimport type { WaniWaniClient, WaniWaniConfig } from \"./types.js\";\n\n/**\n * Create a WaniWani SDK client\n *\n * @param config - Configuration options\n * @returns A fully typed WaniWani client\n *\n * @example\n * ```typescript\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani({ apiKey: \"...\" });\n *\n * // Next.js route handler\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: { systemPrompt: \"You are a helpful assistant.\" },\n * });\n * ```\n */\nexport function waniwani(config?: WaniWaniConfig): WaniWaniClient {\n\tconst baseUrl = config?.baseUrl ?? \"https://app.waniwani.ai\";\n\tconst apiKey = config?.apiKey ?? process.env.WANIWANI_API_KEY;\n\tconst trackingConfig = {\n\t\tendpointPath: config?.tracking?.endpointPath ?? \"/api/mcp/events/v2/batch\",\n\t\tflushIntervalMs: config?.tracking?.flushIntervalMs ?? 1_000,\n\t\tmaxBatchSize: config?.tracking?.maxBatchSize ?? 20,\n\t\tmaxBufferSize: config?.tracking?.maxBufferSize ?? 1_000,\n\t\tmaxRetries: config?.tracking?.maxRetries ?? 3,\n\t\tretryBaseDelayMs: config?.tracking?.retryBaseDelayMs ?? 200,\n\t\tretryMaxDelayMs: config?.tracking?.retryMaxDelayMs ?? 2_000,\n\t\tshutdownTimeoutMs: config?.tracking?.shutdownTimeoutMs ?? 2_000,\n\t};\n\n\tconst internalConfig = { baseUrl, apiKey, tracking: trackingConfig };\n\n\t// Compose client from modules\n\tconst trackingClient = createTrackingClient(internalConfig);\n\tconst kbClient = createKbClient(internalConfig);\n\n\treturn {\n\t\t...trackingClient,\n\t\tkb: kbClient,\n\t\t_config: internalConfig,\n\t};\n}\n","/**\n * Server-side API route handler for widget tracking events.\n *\n * Receives batched events from the `useWaniwani` React hook and forwards them\n * to the WaniWani backend using the server-side SDK.\n *\n * @example Next.js App Router\n * ```typescript\n * // app/api/waniwani/track/route.ts\n * import { createTrackingRoute } from \"@waniwani/sdk/mcp\";\n *\n * const handler = createTrackingRoute({\n * apiKey: process.env.WANIWANI_API_KEY,\n * baseUrl: process.env.WANIWANI_BASE_URL,\n * });\n *\n * export { handler as POST };\n * ```\n */\n\nimport type { EventType, TrackInput } from \"../../tracking/@types.js\";\nimport type { WaniWaniConfig } from \"../../types.js\";\nimport { waniwani } from \"../../waniwani.js\";\n\nexport interface TrackingRouteOptions {\n\t/** API key for the WaniWani backend. Defaults to WANIWANI_API_KEY env var. */\n\tapiKey?: string;\n\t/** Base URL for the WaniWani backend. Defaults to https://app.waniwani.ai. */\n\tbaseUrl?: string;\n}\n\n/** Shape of a single event from the WidgetTransport client. */\ninterface WidgetEventPayload {\n\tevent_id?: string;\n\tevent_type?: string;\n\ttimestamp?: string;\n\tsource?: string;\n\tsession_id?: string;\n\ttrace_id?: string;\n\tuser_id?: string;\n\tevent_name?: string;\n\tmetadata?: Record<string, unknown>;\n\t[key: string]: unknown;\n}\n\n/** Batch payload sent by WidgetTransport. */\ninterface BatchPayload {\n\tevents: WidgetEventPayload[];\n\tsentAt?: string;\n}\n\n/**\n * Map a WidgetEvent from the client to the SDK's TrackInput format.\n */\nfunction mapWidgetEvent(ev: WidgetEventPayload): TrackInput {\n\tconst eventType = ev.event_type ?? \"widget_click\";\n\n\t// For manual tracking methods (track, identify, step, conversion),\n\t// use \"widget_<type>\" as the event name. Auto-capture events already\n\t// have the \"widget_\" prefix.\n\tconst isAutoCapture = eventType.startsWith(\"widget_\");\n\tconst eventName: EventType = (\n\t\tisAutoCapture ? eventType : `widget_${eventType}`\n\t) as EventType;\n\n\t// Merge metadata + any extra properties from the event\n\tconst properties: Record<string, unknown> = {\n\t\t...(ev.metadata ?? {}),\n\t};\n\tif (ev.event_name) properties.event_name = ev.event_name;\n\n\treturn {\n\t\tevent: eventName,\n\t\tproperties,\n\t\tsessionId: ev.session_id,\n\t\ttraceId: ev.trace_id,\n\t\texternalUserId: ev.user_id,\n\t\teventId: ev.event_id,\n\t\ttimestamp: ev.timestamp,\n\t\tsource: ev.source ?? \"widget\",\n\t} as TrackInput;\n}\n\n/**\n * Creates a POST handler that receives tracking events from `useWaniwani`\n * and forwards them to the WaniWani backend.\n */\nexport function createTrackingRoute(options?: TrackingRouteOptions) {\n\tconst config: WaniWaniConfig = {\n\t\tapiKey: options?.apiKey,\n\t\tbaseUrl: options?.baseUrl,\n\t};\n\n\t// Lazy singleton — created on first request\n\tlet client: ReturnType<typeof waniwani> | undefined;\n\n\tfunction getClient() {\n\t\tif (!client) {\n\t\t\tclient = waniwani(config);\n\t\t}\n\t\treturn client;\n\t}\n\n\treturn async function handler(request: Request): Promise<Response> {\n\t\tlet body: BatchPayload;\n\t\ttry {\n\t\t\tbody = (await request.json()) as BatchPayload;\n\t\t} catch {\n\t\t\treturn new Response(JSON.stringify({ error: \"Invalid JSON\" }), {\n\t\t\t\tstatus: 400,\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t});\n\t\t}\n\n\t\tif (!Array.isArray(body.events) || body.events.length === 0) {\n\t\t\treturn new Response(\n\t\t\t\tJSON.stringify({ error: \"Missing or empty events array\" }),\n\t\t\t\t{\n\t\t\t\t\tstatus: 400,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\n\t\ttry {\n\t\t\tconst c = getClient();\n\t\t\tconst results: string[] = [];\n\n\t\t\tfor (const ev of body.events) {\n\t\t\t\tconst trackInput = mapWidgetEvent(ev);\n\t\t\t\tconst result = await c.track(trackInput);\n\t\t\t\tresults.push(result.eventId);\n\t\t\t}\n\n\t\t\tawait c.flush();\n\n\t\t\treturn new Response(\n\t\t\t\tJSON.stringify({ ok: true, accepted: results.length }),\n\t\t\t\t{\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\t},\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tconst message = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\treturn new Response(JSON.stringify({ error: message }), {\n\t\t\t\tstatus: 500,\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t});\n\t\t}\n\t};\n}\n","/**\n * Widget token minting — fetches short-lived JWTs from the WaniWani backend\n * so browser widgets can POST events directly.\n */\n\ninterface WidgetTokenConfig {\n\tbaseUrl: string;\n\tapiKey: string;\n}\n\ninterface WidgetTokenResult {\n\ttoken: string;\n\texpiresAt: string;\n}\n\ninterface CachedToken {\n\ttoken: string;\n\t/** Unix ms when the token expires */\n\texpiresAt: number;\n}\n\n/** Re-mint when < 2 minutes remain to avoid using nearly-expired tokens. */\nconst EXPIRY_BUFFER_MS = 2 * 60 * 1000;\n\nexport class WidgetTokenCache {\n\tprivate cached: CachedToken | null = null;\n\tprivate pending: Promise<string | null> | null = null;\n\tprivate readonly config: WidgetTokenConfig;\n\n\tconstructor(config: WidgetTokenConfig) {\n\t\tthis.config = config;\n\t}\n\n\t/**\n\t * Get a valid widget token. Returns a cached token if still fresh,\n\t * otherwise mints a new one. Returns `null` on failure.\n\t */\n\tasync getToken(sessionId?: string, traceId?: string): Promise<string | null> {\n\t\tif (this.cached && Date.now() < this.cached.expiresAt - EXPIRY_BUFFER_MS) {\n\t\t\treturn this.cached.token;\n\t\t}\n\n\t\t// Deduplicate concurrent requests\n\t\tif (this.pending) return this.pending;\n\n\t\tthis.pending = this.mint(sessionId, traceId).finally(() => {\n\t\t\tthis.pending = null;\n\t\t});\n\n\t\treturn this.pending;\n\t}\n\n\tprivate async mint(\n\t\tsessionId?: string,\n\t\ttraceId?: string,\n\t): Promise<string | null> {\n\t\tconst url = joinUrl(this.config.baseUrl, \"/api/mcp/widget-tokens\");\n\n\t\tconst body: Record<string, string> = {};\n\t\tif (sessionId) body.sessionId = sessionId;\n\t\tif (traceId) body.traceId = traceId;\n\n\t\ttry {\n\t\t\tconst response = await fetch(url, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\tAuthorization: `Bearer ${this.config.apiKey}`,\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify(body),\n\t\t\t});\n\n\t\t\tif (!response.ok) return null;\n\n\t\t\tconst json = (await response.json()) as Record<string, unknown>;\n\n\t\t\t// Support both flat { token, expiresAt } and nested { data: { token, expiresAt } }\n\t\t\tconst result = (\n\t\t\t\tjson.data && typeof json.data === \"object\" ? json.data : json\n\t\t\t) as WidgetTokenResult;\n\n\t\t\tconst expiresAtMs = new Date(result.expiresAt).getTime();\n\t\t\tif (!result.token || Number.isNaN(expiresAtMs)) return null;\n\n\t\t\tthis.cached = {\n\t\t\t\ttoken: result.token,\n\t\t\t\texpiresAt: expiresAtMs,\n\t\t\t};\n\n\t\t\treturn result.token;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n\nfunction joinUrl(baseUrl: string, path: string): string {\n\tconst base = baseUrl.endsWith(\"/\") ? baseUrl.slice(0, -1) : baseUrl;\n\treturn `${base}${path}`;\n}\n","import type { ToolCalledProperties, TrackInput } from \"../../tracking/index.js\";\nimport type { WaniWaniClient, WaniWaniConfig } from \"../../types.js\";\nimport { waniwani } from \"../../waniwani.js\";\nimport type { McpServer } from \"./tools/types\";\nimport { WidgetTokenCache } from \"./widget-token.js\";\n\ntype WaniwaniTracker = Pick<WaniWaniClient, \"flush\" | \"track\" | \"_config\">;\ntype UnknownRecord = Record<string, unknown>;\n\ntype WrappedServer = McpServer & {\n\t__waniwaniWrapped?: true;\n};\n\n/**\n * Options for withWaniwani().\n */\nexport type WithWaniwaniOptions = {\n\t/**\n\t * Optional pre-built WaniWani client.\n\t * When omitted, a new client is created from `config`.\n\t */\n\tclient?: WaniwaniTracker;\n\t/**\n\t * WaniWani client config used when `client` is omitted.\n\t */\n\tconfig?: WaniWaniConfig;\n\t/**\n\t * Optional explicit tool type. Defaults to `\"other\"`.\n\t */\n\ttoolType?:\n\t\t| ToolCalledProperties[\"type\"]\n\t\t| ((toolName: string) => ToolCalledProperties[\"type\"] | undefined);\n\t/**\n\t * Optional metadata merged into every tracked event.\n\t */\n\tmetadata?: UnknownRecord;\n\t/**\n\t * Flush tracking transport after each tool call.\n\t */\n\tflushAfterToolCall?: boolean;\n\t/**\n\t * Optional error callback for non-fatal tracking errors.\n\t */\n\tonError?: (error: Error) => void;\n\t/**\n\t * Inject widget tracking config into tool response `_meta.waniwani` so browser\n\t * widgets can send events directly to the WaniWani backend.\n\t *\n\t * Always injects `endpoint`. Injects `token` when an API key is configured\n\t * and token minting succeeds.\n\t *\n\t * @default true\n\t */\n\tinjectWidgetToken?: boolean;\n};\n\nconst DEFAULT_BASE_URL = \"https://app.waniwani.ai\";\n\n/**\n * Wrap an MCP server so tool handlers automatically emit `tool.called` events.\n *\n * The wrapper intercepts `server.registerTool(...)`, tracks each invocation,\n * then forwards execution to the original tool handler.\n *\n * When `injectWidgetToken` is enabled (default), tracking config is injected\n * into tool response `_meta.waniwani` so browser widgets can post events\n * directly to the WaniWani backend without a server-side proxy.\n */\nexport function withWaniwani(\n\tserver: McpServer,\n\toptions: WithWaniwaniOptions = {},\n): McpServer {\n\tconst wrappedServer = server as WrappedServer;\n\tif (wrappedServer.__waniwaniWrapped) {\n\t\treturn wrappedServer;\n\t}\n\n\twrappedServer.__waniwaniWrapped = true;\n\n\tconst tracker = options.client ?? waniwani(options.config);\n\tconst injectToken = options.injectWidgetToken !== false;\n\n\t// Lazy-init token cache — only created if we have an API key\n\tlet tokenCache: WidgetTokenCache | null = null;\n\n\tfunction getTokenCache(): WidgetTokenCache | null {\n\t\tif (tokenCache) return tokenCache;\n\t\tconst apiKey = tracker._config.apiKey;\n\t\tif (!apiKey) return null;\n\t\ttokenCache = new WidgetTokenCache({\n\t\t\tbaseUrl: tracker._config.baseUrl ?? DEFAULT_BASE_URL,\n\t\t\tapiKey,\n\t\t});\n\t\treturn tokenCache;\n\t}\n\n\tconst originalRegisterTool = server.registerTool.bind(server) as (\n\t\t...args: unknown[]\n\t) => unknown;\n\n\twrappedServer.registerTool = ((...args: unknown[]) => {\n\t\tconst [toolNameRaw, config, handlerRaw] = args;\n\t\tconst toolName =\n\t\t\ttypeof toolNameRaw === \"string\" && toolNameRaw.trim().length > 0\n\t\t\t\t? toolNameRaw\n\t\t\t\t: \"unknown\";\n\n\t\tif (typeof handlerRaw !== \"function\") {\n\t\t\treturn originalRegisterTool(...args);\n\t\t}\n\n\t\tconst handler = handlerRaw as (\n\t\t\tinput: unknown,\n\t\t\textra: unknown,\n\t\t) => Promise<unknown> | unknown;\n\n\t\tconst wrappedHandler = async (input: unknown, extra: unknown) => {\n\t\t\tconst startTime = performance.now();\n\t\t\ttry {\n\t\t\t\tconst result = await handler(input, extra);\n\t\t\t\tconst durationMs = Math.round(performance.now() - startTime);\n\n\t\t\t\tawait safeTrack(\n\t\t\t\t\ttracker,\n\t\t\t\t\tbuildTrackInput(toolName, extra, options, {\n\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\tstatus: \"ok\",\n\t\t\t\t\t}),\n\t\t\t\t\toptions.onError,\n\t\t\t\t);\n\n\t\t\t\tif (options.flushAfterToolCall) {\n\t\t\t\t\tawait safeFlush(tracker, options.onError);\n\t\t\t\t}\n\n\t\t\t\tif (injectToken) {\n\t\t\t\t\tawait injectWidgetConfig(\n\t\t\t\t\t\tresult,\n\t\t\t\t\t\tgetTokenCache(),\n\t\t\t\t\t\ttracker._config.baseUrl ?? DEFAULT_BASE_URL,\n\t\t\t\t\t\toptions.onError,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn result;\n\t\t\t} catch (error) {\n\t\t\t\tconst durationMs = Math.round(performance.now() - startTime);\n\n\t\t\t\tawait safeTrack(\n\t\t\t\t\ttracker,\n\t\t\t\t\tbuildTrackInput(toolName, extra, options, {\n\t\t\t\t\t\tdurationMs,\n\t\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\t\terrorMessage:\n\t\t\t\t\t\t\terror instanceof Error ? error.message : String(error),\n\t\t\t\t\t}),\n\t\t\t\t\toptions.onError,\n\t\t\t\t);\n\n\t\t\t\tif (options.flushAfterToolCall) {\n\t\t\t\t\tawait safeFlush(tracker, options.onError);\n\t\t\t\t}\n\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t};\n\n\t\treturn originalRegisterTool(toolNameRaw, config, wrappedHandler);\n\t}) as McpServer[\"registerTool\"];\n\n\treturn wrappedServer;\n}\n\nasync function injectWidgetConfig(\n\tresult: unknown,\n\tcache: WidgetTokenCache | null,\n\tbaseUrl: string,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\tif (!isRecord(result)) return;\n\n\tif (!isRecord(result._meta)) {\n\t\t(result as UnknownRecord)._meta = {};\n\t}\n\n\tconst meta = (result as UnknownRecord)._meta as UnknownRecord;\n\tconst waniwaniConfig: UnknownRecord = {\n\t\tendpoint: `${baseUrl.replace(/\\/$/, \"\")}/api/mcp/events/v2/batch`,\n\t};\n\n\tif (cache) {\n\t\ttry {\n\t\t\tconst token = await cache.getToken();\n\t\t\tif (token) {\n\t\t\t\twaniwaniConfig.token = token;\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tonError?.(toError(error));\n\t\t}\n\t}\n\n\tmeta.waniwani = waniwaniConfig;\n}\n\nfunction buildTrackInput(\n\ttoolName: string,\n\textra: unknown,\n\toptions: WithWaniwaniOptions,\n\ttiming?: { durationMs: number; status: string; errorMessage?: string },\n): TrackInput {\n\tconst toolType = resolveToolType(toolName, options.toolType);\n\tconst meta = extractMeta(extra);\n\n\treturn {\n\t\tevent: \"tool.called\",\n\t\tproperties: {\n\t\t\tname: toolName,\n\t\t\ttype: toolType,\n\t\t\t...(timing ?? {}),\n\t\t},\n\t\tmeta,\n\t\tmetadata: {\n\t\t\tsource: \"withWaniwani\",\n\t\t\t...(options.metadata ?? {}),\n\t\t},\n\t};\n}\n\nfunction resolveToolType(\n\ttoolName: string,\n\ttoolTypeOption: WithWaniwaniOptions[\"toolType\"],\n): ToolCalledProperties[\"type\"] {\n\tif (typeof toolTypeOption === \"function\") {\n\t\treturn toolTypeOption(toolName) ?? \"other\";\n\t}\n\treturn toolTypeOption ?? \"other\";\n}\n\nfunction extractMeta(extra: unknown): UnknownRecord | undefined {\n\tif (!isRecord(extra)) return undefined;\n\n\tconst meta = extra._meta;\n\tif (!isRecord(meta)) return undefined;\n\n\treturn meta;\n}\n\nfunction isRecord(value: unknown): value is UnknownRecord {\n\treturn Boolean(value) && typeof value === \"object\" && !Array.isArray(value);\n}\n\nasync function safeTrack(\n\ttracker: Pick<WaniWaniClient, \"track\">,\n\tinput: TrackInput,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\ttry {\n\t\tawait tracker.track(input);\n\t} catch (error) {\n\t\tonError?.(toError(error));\n\t}\n}\n\nasync function safeFlush(\n\ttracker: Pick<WaniWaniClient, \"flush\">,\n\tonError?: (error: Error) => void,\n): Promise<void> {\n\ttry {\n\t\tawait tracker.flush();\n\t} catch (error) {\n\t\tonError?.(toError(error));\n\t}\n}\n\nfunction toError(error: unknown): Error {\n\tif (error instanceof Error) {\n\t\treturn error;\n\t}\n\treturn new Error(String(error));\n}\n"],"mappings":"AAWO,SAASA,GAAiC,CAChD,OAAI,OAAO,OAAW,KAAe,WAAY,OACzC,SAED,UACR,CAKO,SAASC,IAAoB,CACnC,OAAOD,EAAe,IAAM,QAC7B,CAKO,SAASE,IAAqB,CACpC,OAAOF,EAAe,IAAM,UAC7B,CCrBO,IAAMG,EAAQ,YACRC,EAAM,UAMbC,EAAY,OAAO,IAAI,yBAAyB,EAChDC,EAAS,OAAO,IAAI,sBAAsB,EA4DzC,SAASC,EACfC,EAQkB,CAClB,GAAI,cAAeA,EAClB,MAAO,CACN,OAAQH,EACR,UAAWG,EAAO,UAClB,QAASA,EAAO,OACjB,EAED,GAAM,CAAE,SAAAC,EAAU,MAAAC,EAAO,QAAAC,EAAS,YAAAC,CAAY,EAAIJ,EAClD,MAAO,CACN,OAAQH,EACR,UAAW,CAAC,CAAE,SAAAI,EAAU,MAAAC,EAAO,QAAAC,EAAS,YAAAC,CAAY,CAAC,CACtD,CACD,CAQO,SAASC,EACfC,EACAN,EAKe,CACf,MAAO,CAAE,OAAQF,EAAQ,SAAAQ,EAAU,GAAGN,CAAO,CAC9C,CAEO,SAASO,EAAYC,EAA0C,CACrE,OACC,OAAOA,GAAU,UACjBA,IAAU,MACV,WAAYA,GACXA,EAA0B,SAAWX,CAExC,CAEO,SAASY,EAASD,EAAuC,CAC/D,OACC,OAAOA,GAAU,UACjBA,IAAU,MACV,WAAYA,GACXA,EAAuB,SAAWV,CAErC,CChIA,OAAS,KAAAY,MAAS,MCCX,IAAMC,EAAmB,sBACnBC,EAAgB,4BAIhBC,EAAY,MACxBC,EACAC,IACqB,CACrB,IAAMC,EAAiBF,EAAQ,SAAS,GAAG,EAAIA,EAAQ,MAAM,EAAG,EAAE,EAAIA,EAEtE,OAAO,MADQ,MAAM,MAAM,GAAGE,CAAc,GAAGD,CAAI,EAAE,GACjC,KAAK,CAC1B,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ACH,MAAM1B,EAAQ,MAAMc,CAAK,CAC1B,OAASS,EAAO,CACfG,IAAUK,EAAQR,CAAK,CAAC,CACzB,CACD,CAEA,eAAeF,GACdrB,EACA0B,EACgB,CAChB,GAAI,CACH,MAAM1B,EAAQ,MAAM,CACrB,OAASuB,EAAO,CACfG,IAAUK,EAAQR,CAAK,CAAC,CACzB,CACD,CAEA,SAASQ,EAAQR,EAAuB,CACvC,OAAIA,aAAiB,MACbA,EAED,IAAI,MAAM,OAAOA,CAAK,CAAC,CAC/B","names":["detectPlatform","isOpenAI","isMCPApps","START","END","INTERRUPT","WIDGET","interrupt","config","question","field","context","suggestions","showWidget","resource","isInterrupt","value","isWidget","z","MIME_TYPE_OPENAI","MIME_TYPE_MCP","fetchHtml","baseUrl","path","normalizedBase","buildOpenAIResourceMeta","config","buildMcpAppsResourceMeta","csp","buildToolMeta","deflateSync","inflateSync","encodeFlowToken","data","json","decodeFlowToken","token","compressed","describeZodField","schema","desc","def","vals","v","buildFlowProtocol","config","lines","fieldList","key","info","getInputMeta","args","decoded","decodeFlowToken","resolveNextNode","edge","state","isFilled","buildInterruptResult","questions","context","currentNode","conversational","q","unanswered","isSingle","q0","conversationalValue","executeFrom","startNodeName","startState","nodes","nodeConfigs","edges","meta","MAX_ITERATIONS","iterations","END","handler","result","isInterrupt","interruptResult","isWidget","widgetField","resource","nodeConversational","buildToolMeta","error","inputSchema","z","compileFlow","input","protocol","fullDescription","handleToolCall","inputMeta","startEdge","START","firstNode","step","updatedState","nextNode","server","extra","_meta","flowToken","encodeFlowToken","textPayload","content","responseMeta","StateGraph","config","name","configOrHandler","maybeHandler","START","END","handler","nodeConfig","from","to","condition","compileFlow","startEdge","edge","createFlow","config","StateGraph","createResource","config","id","title","description","baseUrl","htmlPath","widgetDomain","prefersBorder","autoHeight","widgetCSP","hostname","openaiUri","mcpUri","htmlPromise","getHtml","fetchHtml","uiDescription","register","server","html","MIME_TYPE_OPENAI","uri","buildOpenAIResourceMeta","MIME_TYPE_MCP","buildMcpAppsResourceMeta","createTool","config","handler","resource","description","inputSchema","annotations","id","title","toolMeta","buildToolMeta","server","args","extra","_meta","result","registerTools","tools","t","WaniWaniError","message","status","SDK_NAME","createKbClient","config","baseUrl","apiKey","requireApiKey","request","method","path","body","key","url","headers","init","response","text","WaniWaniError","files","query","options","DEFAULT_SOURCE","mapTrackEventToV2","input","options","now","generateId","createEventId","eventName","resolveEventName","meta","toRecord","metadata","correlation","resolveCorrelationIds","eventId","takeNonEmptyString","timestamp","normalizeTimestamp","source","rawLegacy","isLegacyTrackEvent","mappedMetadata","mapProperties","legacyProperties","mapLegacyProperties","explicitProperties","properties","requestId","pickFirstString","sessionId","traceId","externalUserId","correlationId","date","record","keys","key","value","DEFAULT_ENDPOINT_PATH","SDK_NAME","AUTH_FAILURE_STATUS","RETRYABLE_STATUS","createV2BatchTransport","options","BatchingV2Transport","joinUrl","DEFAULT_ENDPOINT_PATH","delayMs","resolve","event","dropCount","timeoutMs","flushPromise","timeoutSignal","batch","attempt","pendingBatch","result","events","response","error","getErrorMessage","data","parseJsonResponse","partial","rejected","byId","retryable","permanent","rejectedEvent","isRetryableRejectedEvent","rawDelay","status","rejectedCount","buffered","code","body","baseUrl","endpointPath","normalizedBase","normalizedPath","createTrackingClient","config","baseUrl","apiKey","tracking","requireApiKey","transport","createV2BatchTransport","client","event","mappedEvent","mapTrackEventToV2","options","attachShutdownHooks","defaultTimeoutMs","shutdown","waniwani","config","baseUrl","apiKey","trackingConfig","internalConfig","trackingClient","createTrackingClient","kbClient","createKbClient","mapWidgetEvent","ev","eventType","eventName","properties","createTrackingRoute","options","config","client","getClient","waniwani","request","body","c","results","trackInput","result","error","message","WidgetTokenCache","config","sessionId","traceId","url","joinUrl","body","response","json","result","expiresAtMs","baseUrl","path","DEFAULT_BASE_URL","withWaniwani","server","options","wrappedServer","tracker","waniwani","injectToken","tokenCache","getTokenCache","apiKey","WidgetTokenCache","originalRegisterTool","args","toolNameRaw","config","handlerRaw","toolName","handler","input","extra","startTime","result","durationMs","safeTrack","buildTrackInput","safeFlush","injectWidgetConfig","error","cache","baseUrl","onError","isRecord","meta","waniwaniConfig","token","toError","timing","toolType","resolveToolType","extractMeta","toolTypeOption","value"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waniwani/sdk",
3
- "version": "0.2.2-beta.1",
3
+ "version": "0.2.2-beta.2",
4
4
  "description": "WaniWani SDK - MCP event tracking, widget framework, and tools",
5
5
  "type": "module",
6
6
  "exports": {