@waniwani/sdk 0.12.5 → 0.12.6

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.
@@ -350,6 +350,60 @@ interface FlowStore {
350
350
  delete(key: string): Promise<void>;
351
351
  }
352
352
 
353
+ declare const flowOutputSchema: {
354
+ status: z.ZodEnum<{
355
+ error: "error";
356
+ interrupt: "interrupt";
357
+ widget: "widget";
358
+ complete: "complete";
359
+ }>;
360
+ question: z.ZodOptional<z.ZodString>;
361
+ field: z.ZodOptional<z.ZodString>;
362
+ fieldSchema: z.ZodOptional<z.ZodObject<{
363
+ type: z.ZodEnum<{
364
+ string: "string";
365
+ number: "number";
366
+ boolean: "boolean";
367
+ object: "object";
368
+ enum: "enum";
369
+ array: "array";
370
+ unknown: "unknown";
371
+ }>;
372
+ values: z.ZodOptional<z.ZodArray<z.ZodString>>;
373
+ description: z.ZodOptional<z.ZodString>;
374
+ optional: z.ZodOptional<z.ZodBoolean>;
375
+ }, z.core.$strip>>;
376
+ suggestions: z.ZodOptional<z.ZodArray<z.ZodString>>;
377
+ questions: z.ZodOptional<z.ZodArray<z.ZodObject<{
378
+ question: z.ZodString;
379
+ field: z.ZodString;
380
+ suggestions: z.ZodOptional<z.ZodArray<z.ZodString>>;
381
+ context: z.ZodOptional<z.ZodString>;
382
+ fieldSchema: z.ZodOptional<z.ZodObject<{
383
+ type: z.ZodEnum<{
384
+ string: "string";
385
+ number: "number";
386
+ boolean: "boolean";
387
+ object: "object";
388
+ enum: "enum";
389
+ array: "array";
390
+ unknown: "unknown";
391
+ }>;
392
+ values: z.ZodOptional<z.ZodArray<z.ZodString>>;
393
+ description: z.ZodOptional<z.ZodString>;
394
+ optional: z.ZodOptional<z.ZodBoolean>;
395
+ }, z.core.$strip>>;
396
+ }, z.core.$strip>>>;
397
+ context: z.ZodOptional<z.ZodString>;
398
+ tool: z.ZodOptional<z.ZodString>;
399
+ data: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
400
+ description: z.ZodOptional<z.ZodString>;
401
+ interactive: z.ZodOptional<z.ZodBoolean>;
402
+ sessionId: z.ZodOptional<z.ZodString>;
403
+ error: z.ZodOptional<z.ZodString>;
404
+ };
405
+ type FlowOutputSchema = typeof flowOutputSchema;
406
+
353
407
  declare const START: "__start__";
354
408
  declare const END: "__end__";
355
409
  declare const INTERRUPT: unique symbol;
@@ -637,6 +691,12 @@ type RegisteredFlow = {
637
691
  title: string;
638
692
  description: string;
639
693
  inputSchema: ZodRawShapeCompat;
694
+ /**
695
+ * JSON Schema for the structured payload returned in every flow tool
696
+ * call. Baked in so MCP clients render the flow protocol as a typed
697
+ * contract instead of an opaque JSON string.
698
+ */
699
+ outputSchema: FlowOutputSchema;
640
700
  annotations?: {
641
701
  readOnlyHint?: boolean;
642
702
  idempotentHint?: boolean;
package/dist/mcp/index.js CHANGED
@@ -1,7 +1,7 @@
1
- var C="__start__",_="__end__",xe=Symbol.for("waniwani.flow.interrupt"),Ee=Symbol.for("waniwani.flow.widget");function Ie(e,t){let n=t?.context,o=[];for(let[r,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let s=i;o.push({question:s.question,field:r,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:xe,questions:o,context:n}}function be(e,t){let n=typeof e=="object"&&e!==null&&"tool"in e,o=n?e.tool:e,r=n?e:t??{};if(r.description!==void 0){let l=typeof o=="string"?o:o.id;throw new Error(`showWidget("${l}", { description }) is no longer supported. The engine now emits a standardized instruction telling the AI to call the widget tool. Return any per-widget description from the "${l}" tool's own response instead.`)}let{data:i,interactive:s,field:a}=r;return{__type:Ee,tool:typeof o=="string"?o:o.id,...i!==void 0?{data:i}:{},...s!==void 0?{interactive:s}:{},...a!==void 0?{field:a}:{}}}function Ce(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===xe}function _e(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===Ee}import{z as W}from"zod";var de="waniwani/client";function G(e){if(typeof e=="object"&&e!==null)return e[de]}function Fe(e,t,n){return{track(o){return e.track({...o,meta:{...t,...o.meta}})},identify(o,r){return e.identify(o,r,t)},kb:e.kb,_config:n}}function L(e,t){for(let n of t){let o=e[n];if(typeof o=="string"&&o.length>0)return o}}var xt=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Et=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],It=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],bt=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Ct=["correlationId","openai/requestId"];var U="waniwani/flow";function F(e){return e?L(e,xt):void 0}function Pe(e){return e?L(e,Et):void 0}function We(e){return e?L(e,It):void 0}function Ne(e){return e?L(e,bt):void 0}function Ae(e){return e?L(e,Ct):void 0}var _t=[{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}];function $(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:o}of _t){let r=e[n];if(typeof r=="string"&&r.length>0)return o}}function Me(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function B(e,t){let n=t.split("."),o=e;for(let r of n){if(o==null||typeof o!="object")return;o=o[r]}return o}function Ft(e,t,n){let o=t.split("."),r=o.pop();if(!r)return;let i=e;for(let s of o)(i[s]==null||typeof i[s]!="object"||Array.isArray(i[s]))&&(i[s]={}),i=i[s];i[r]=n}function Ue(e,t){let n=t.split("."),o=n.pop();if(!o)return;let r=e;for(let i of n){if(r==null||typeof r!="object")return;r=r[i]}r!=null&&typeof r=="object"&&delete r[o]}function Y(e){let t={};for(let[n,o]of Object.entries(e))n.includes(".")?Ft(t,n,o):o!==null&&typeof o=="object"&&!Array.isArray(o)?t=P(t,{[n]:o}):t[n]=o;return t}function P(e,t){let n={...e};for(let[o,r]of Object.entries(t))r!==null&&typeof r=="object"&&!Array.isArray(r)&&n[o]!==null&&typeof n[o]=="object"&&!Array.isArray(n[o])?n[o]=P(n[o],r):n[o]=r;return n}function Oe(e){return e._zod?.def}function Pt(e){let t=e,n=!1;for(let o=0;o<8;o++){let r=Oe(t);if(!r?.innerType)break;if(r.type==="optional"||r.type==="nullable"||r.type==="default"){n=!0,t=r.innerType;continue}break}return{inner:t,optional:n}}function De(e){let{inner:t,optional:n}=Pt(e),o=t.description??e.description??void 0,r=Oe(t),i=r?.type,s={type:"unknown",...o?{description:o}:{},...n?{optional:!0}:{}};return i==="enum"&&r?.entries?{...s,type:"enum",values:Object.keys(r.entries)}:i==="string"||i==="number"||i==="boolean"||i==="object"||i==="array"?{...s,type:i}:s}function ce(e,t){if(!e||!t)return;if(t.includes(".")){let o=t.indexOf("."),r=t.slice(0,o),i=t.slice(o+1),s=e[r];if(!s)return;let l=Me(s)?.[i];return l?De(l):void 0}let n=e[t];if(n)return De(n)}function le(e){return e!=null&&e!==""}async function A(e,t){return e.type==="direct"?e.to:e.condition(t)}function Ke(e,t,n,o,r){if(e.every(d=>le(B(o,d.field))))return null;let i=e.filter(d=>!le(B(o,d.field))).map(d=>{let c=ce(r,d.field);return c?{...d,fieldSchema:c}:d}),s=i.length===1,a=i[0];return{content:s&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.fieldSchema?{fieldSchema:a.fieldSchema}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:i,...t?{context:t}:{}},flowTokenContent:{step:n,state:o,...s&&a?{field:a.field}:{}}}}async function q(e,t,n,o,r,i,s,a,l){let d=e,c={...t},u=[],p=50,v=0;for(;v++<p;){if(d===_)return{content:{status:"complete"},flowTokenContent:{state:c},nodesVisited:u};let f=n.get(d);if(!f)return{content:{status:"error",error:`Unknown node: "${d}"`},nodesVisited:u};a?.get(d)?.hideFromFunnel||u.push(d);try{let g=await f({state:c,meta:i,interrupt:Ie,showWidget:be,waniwani:s});if(Ce(g)){for(let T of g.questions)T.validate&&r.set(`${d}:${T.field}`,T.validate);let h=Ke(g.questions,g.context,d,c,l);if(h)return{...h,nodesVisited:u};for(let T of g.questions){let R=r.get(`${d}:${T.field}`);if(R)try{let x=B(c,T.field),I=await R(x);I&&typeof I=="object"&&(c=P(c,I))}catch(x){let I=x instanceof Error?x.message:String(x);Ue(c,T.field);let N=g.questions.map(M=>M.field===T.field?{...M,context:M.context?`ERROR: ${I}
1
+ var _="__start__",F="__end__",Ie=Symbol.for("waniwani.flow.interrupt"),be=Symbol.for("waniwani.flow.widget");function Ce(e,t){let n=t?.context,o=[];for(let[r,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let s=i;o.push({question:s.question,field:r,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:Ie,questions:o,context:n}}function _e(e,t){let n=typeof e=="object"&&e!==null&&"tool"in e,o=n?e.tool:e,r=n?e:t??{};if(r.description!==void 0){let l=typeof o=="string"?o:o.id;throw new Error(`showWidget("${l}", { description }) is no longer supported. The engine now emits a standardized instruction telling the AI to call the widget tool. Return any per-widget description from the "${l}" tool's own response instead.`)}let{data:i,interactive:s,field:a}=r;return{__type:be,tool:typeof o=="string"?o:o.id,...i!==void 0?{data:i}:{},...s!==void 0?{interactive:s}:{},...a!==void 0?{field:a}:{}}}function Fe(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===Ie}function Pe(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===be}import{z as N}from"zod";var ce="waniwani/client";function Y(e){if(typeof e=="object"&&e!==null)return e[ce]}function We(e,t,n){return{track(o){return e.track({...o,meta:{...t,...o.meta}})},identify(o,r){return e.identify(o,r,t)},kb:e.kb,_config:n}}function $(e,t){for(let n of t){let o=e[n];if(typeof o=="string"&&o.length>0)return o}}var bt=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Ct=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],_t=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],Ft=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Pt=["correlationId","openai/requestId"];var D="waniwani/flow";function P(e){return e?$(e,bt):void 0}function Ne(e){return e?$(e,Ct):void 0}function Ae(e){return e?$(e,_t):void 0}function Me(e){return e?$(e,Ft):void 0}function Ue(e){return e?$(e,Pt):void 0}var Wt=[{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}];function q(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:o}of Wt){let r=e[n];if(typeof r=="string"&&r.length>0)return o}}function De(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function B(e,t){let n=t.split("."),o=e;for(let r of n){if(o==null||typeof o!="object")return;o=o[r]}return o}function Nt(e,t,n){let o=t.split("."),r=o.pop();if(!r)return;let i=e;for(let s of o)(i[s]==null||typeof i[s]!="object"||Array.isArray(i[s]))&&(i[s]={}),i=i[s];i[r]=n}function Oe(e,t){let n=t.split("."),o=n.pop();if(!o)return;let r=e;for(let i of n){if(r==null||typeof r!="object")return;r=r[i]}r!=null&&typeof r=="object"&&delete r[o]}function Z(e){let t={};for(let[n,o]of Object.entries(e))n.includes(".")?Nt(t,n,o):o!==null&&typeof o=="object"&&!Array.isArray(o)?t=W(t,{[n]:o}):t[n]=o;return t}function W(e,t){let n={...e};for(let[o,r]of Object.entries(t))r!==null&&typeof r=="object"&&!Array.isArray(r)&&n[o]!==null&&typeof n[o]=="object"&&!Array.isArray(n[o])?n[o]=W(n[o],r):n[o]=r;return n}function Ke(e){return e._zod?.def}function At(e){let t=e,n=!1;for(let o=0;o<8;o++){let r=Ke(t);if(!r?.innerType)break;if(r.type==="optional"||r.type==="nullable"||r.type==="default"){n=!0,t=r.innerType;continue}break}return{inner:t,optional:n}}function je(e){let{inner:t,optional:n}=At(e),o=t.description??e.description??void 0,r=Ke(t),i=r?.type,s={type:"unknown",...o?{description:o}:{},...n?{optional:!0}:{}};return i==="enum"&&r?.entries?{...s,type:"enum",values:Object.keys(r.entries)}:i==="string"||i==="number"||i==="boolean"||i==="object"||i==="array"?{...s,type:i}:s}function le(e,t){if(!e||!t)return;if(t.includes(".")){let o=t.indexOf("."),r=t.slice(0,o),i=t.slice(o+1),s=e[r];if(!s)return;let l=De(s)?.[i];return l?je(l):void 0}let n=e[t];if(n)return je(n)}function ue(e){return e!=null&&e!==""}async function M(e,t){return e.type==="direct"?e.to:e.condition(t)}function Le(e,t,n,o,r){if(e.every(d=>ue(B(o,d.field))))return null;let i=e.filter(d=>!ue(B(o,d.field))).map(d=>{let c=le(r,d.field);return c?{...d,fieldSchema:c}:d}),s=i.length===1,a=i[0];return{content:s&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.fieldSchema?{fieldSchema:a.fieldSchema}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:i,...t?{context:t}:{}},flowTokenContent:{step:n,state:o,...s&&a?{field:a.field}:{}}}}async function H(e,t,n,o,r,i,s,a,l){let d=e,c={...t},u=[],p=50,x=0;for(;x++<p;){if(d===F)return{content:{status:"complete"},flowTokenContent:{state:c},nodesVisited:u};let f=n.get(d);if(!f)return{content:{status:"error",error:`Unknown node: "${d}"`},nodesVisited:u};a?.get(d)?.hideFromFunnel||u.push(d);try{let g=await f({state:c,meta:i,interrupt:Ce,showWidget:_e,waniwani:s});if(Fe(g)){for(let T of g.questions)T.validate&&r.set(`${d}:${T.field}`,T.validate);let y=Le(g.questions,g.context,d,c,l);if(y)return{...y,nodesVisited:u};for(let T of g.questions){let R=r.get(`${d}:${T.field}`);if(R)try{let E=B(c,T.field),b=await R(E);b&&typeof b=="object"&&(c=W(c,b))}catch(E){let b=E instanceof Error?E.message:String(E);Oe(c,T.field);let A=g.questions.map(U=>U.field===T.field?{...U,context:U.context?`ERROR: ${b}
2
2
 
3
- ${M.context}`:`ERROR: ${I}`}:M),z=Ke(N,g.context,d,c,l);if(z)return{...z,nodesVisited:u};break}}let w=o.get(d);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await A(w,c);continue}if(_e(g)){let h=g.field;if(h&&le(B(c,h))){let T=o.get(d);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await A(T,c);continue}let w=h?ce(l,h):void 0;return{content:{status:"widget",tool:g.tool,...g.data!==void 0?{data:g.data}:{},description:`IMPORTANT: You MUST now call the ${g.tool} tool to display the widget. Do NOT skip this step`,interactive:g.interactive!==!1,...h?{field:h}:{},...w?{fieldSchema:w}:{}},flowTokenContent:{step:d,state:c,field:h,widgetId:g.tool},nodesVisited:u}}c=P(c,g);let E=o.get(d);if(!E)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await A(E,c)}catch(k){return{content:{status:"error",error:k instanceof Error?k.message:String(k)},flowTokenContent:{step:d,state:c},nodesVisited:u}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"},nodesVisited:u}}function ue(e){return typeof e=="object"&&e!==null&&e.__ww_enc===1&&typeof e.ct=="string"&&typeof e.iv=="string"}var je=new Map;async function Le(e){let t=je.get(e);if(t)return t;let n=Buffer.from(e,"base64");if(n.length!==32)throw new Error("[WaniWani KV] WANIWANI_ENCRYPTION_KEY must be a base64-encoded 32-byte (256-bit) key.");let o=await globalThis.crypto.subtle.importKey("raw",n,{name:"AES-GCM"},!1,["encrypt","decrypt"]);return je.set(e,o),o}async function $e(e,t){let n=await Le(t),o=globalThis.crypto.getRandomValues(new Uint8Array(12)),r=new TextEncoder().encode(JSON.stringify(e)),i=await globalThis.crypto.subtle.encrypt({name:"AES-GCM",iv:o},n,r);return{__ww_enc:1,ct:Buffer.from(i).toString("base64"),iv:Buffer.from(o).toString("base64")}}async function Be(e,t){let n=await Le(t),o=Buffer.from(e.ct,"base64"),r=Buffer.from(e.iv,"base64"),i;try{i=await globalThis.crypto.subtle.decrypt({name:"AES-GCM",iv:r},n,o)}catch{throw new Error("[WaniWani KV] Decryption failed. The encryption key may be incorrect or the data may be corrupted.")}return JSON.parse(new TextDecoder().decode(i))}var Wt={debug:0,warn:1,error:2,none:3};function Nt(){let e=process.env.WANIWANI_LOG_LEVEL;return e&&e in Wt?e:process.env.WANIWANI_DEBUG?"debug":"none"}function D(e,t){return t??Nt()==="debug"?(...o)=>console.log(`[waniwani:${e}]`,...o):()=>{}}var At="@waniwani/sdk",Mt="https://app.waniwani.ai",Ut=D("kv"),O=class{get baseUrl(){return(process.env.WANIWANI_API_URL??Mt).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}get encryptionKey(){return process.env.WANIWANI_ENCRYPTION_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let n=await this.request("/api/mcp/redis/get",{key:t});if(n==null)return null;if(ue(n)){if(!this.encryptionKey)throw new Error("[WaniWani KV] Encrypted data found but WANIWANI_ENCRYPTION_KEY is not set.");return Be(n,this.encryptionKey)}return n}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let o=this.encryptionKey;Ut(`set "${t}" \u2014 encryption ${o?"enabled":"disabled (no WANIWANI_ENCRYPTION_KEY)"}`);let r=o?await $e(n,o):n;await this.request("/api/mcp/redis/set",{key:t,value:r})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let o=`${this.baseUrl}${t}`,r=await fetch(o,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":At},body:JSON.stringify(n)});if(!r.ok){let s=await r.text().catch(()=>"");throw new Error(s||`KV store API error: HTTP ${r.status}`)}return(await r.json()).data}};var Z=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,n){this.map.set(t,n)}async delete(t){this.map.delete(t)}};var H=class{store=new O;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Dt(e){let t=e.toString();return t.includes("showWidget")?"widget":t.includes("interrupt")?"interrupt":"action"}function Ot(e,t){let n=e.toString(),o=[];for(let r of t){let i=`"${r}"`,s=`'${r}'`,a=`\`${r}\``;(n.includes(i)||n.includes(s)||n.includes(a))&&o.push(r)}return o}function qe(e,t,n,o){let r=new Set([...t.keys(),_]),i=[];for(let[a,l]of t){let d=o.get(a);i.push({id:a,type:Dt(l),label:d?.label??a,...d?.hideFromFunnel?{hideFromFunnel:!0}:{}})}let s=[];for(let[a,l]of n)if(l.type==="direct")s.push({from:a,to:l.to,type:"direct"});else{let d=Ot(l.condition,r);s.push({from:a,to:d,type:"conditional"})}return{flowId:e.id,title:e.title,nodes:i,edges:s}}function He(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow."," Do NOT invent missing intent."," Optionally include `context` \u2014 the situation or environment that led the user to start"," this flow (e.g. what page they are on, what they were doing, or what triggered the request)."," Only provide `context` when there is genuinely relevant situational information. Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs (see the `stateUpdates` schema"," for the list of writable fields). 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."];return e.omitIntentPII&&t.push(" Do NOT include PII in `intent` or `context` \u2014 no names, emails, phones, addresses, IDs, ages, or birthdates.",' Summarize the goal abstractly (e.g. "user wants a quote", not "Jane Doe wants a quote").'),t.push(" For grouped fields (z.object state), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, fieldSchema?, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field, fieldSchema?}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `fieldSchema` (when present) describes the expected value: `{ type, values?, description?, optional? }`.",' Use it to validate before sending \u2014 match enum `values` exactly, coerce strings to numbers where `type: "number"`.'," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Check the `interactive` field in the response:"," \u2022 `interactive: true` \u2014 The widget requires user interaction. After calling the display tool,"," STOP and WAIT for the user to interact with the widget. Do NOT call this flow tool again"," until the user has responded. When they do, call with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned."," \u2022 `interactive: false` \u2014 The widget is display-only. Call the display tool, then immediately",' call THIS flow tool again with `action: "continue"`. Do NOT wait for user interaction.',' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","4. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped.","5. CORRECTION: If the user wants to CHANGE a previously-answered field",' (e.g. "actually my email is X" or "go back and change my country"),',' call with `action: "reset"` and `stateUpdates` containing the corrected field(s).'," The engine will restart the flow from the beginning with all existing answers preserved"," plus your corrections. Steps with filled answers will be auto-skipped."," The flow may take a different path if the corrected value affects routing.",' Do NOT use "reset" for the CURRENT question \u2014 use "continue" for that.',"6. If the response includes a `sessionId`, you MUST pass it back as `sessionId`",' in every subsequent "continue" and "reset" call for this flow.'),t.join(`
4
- `)}function pe(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function Ve(e){let t=ze(e),n=pe(t.waniwani)?t.waniwani:{};return e.meta({...t,waniwani:{...n,redacted:!0}})}function Kt(e){let n=ze(e).waniwani;return pe(n)&&n.redacted===!0}function ze(e){let t=e.meta;if(typeof t!="function")return{};let n=t.call(e);return pe(n)?n:{}}function Ge(e){if(!e)return[];let t=[];for(let[n,o]of Object.entries(e))Kt(o)&&t.push(n);return t}var J="waniwani/redactedStateUpdateFields";function jt(e){let t=e.omitIntentPII?" Do not include PII (names, emails, phones, addresses, IDs, ages, birthdates) \u2014 summarize abstractly.":"",o=e.state&&Object.keys(e.state).length>0?W.object(e.state).partial().passthrough():W.record(W.string(),W.unknown());return{action:W.enum(["start","continue","reset"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget), "reset" to restart from the beginning with a correction to a previously-collected field'),intent:W.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow. Do not invent missing intent.${t}`),context:W.string().optional().describe(`Optional when action is "start". Describe the situation or environment that led the user to start this flow \u2014 e.g. what page they are on, what they were doing, or what triggered the request. Do not invent missing context.${t}`),stateUpdates:o.optional().describe(`State field values to set before processing the next node. Pass the user's answer (keyed by the field name from the response) and any other values the user mentioned. For nested state fields, use dot-paths like "driver.name".`),sessionId:W.string().optional().describe('Session identifier. If the response includes a `sessionId`, pass it back on every subsequent "continue" and "reset" call for this flow.')}}function Lt(e){if(process.env.WANIWANI_API_KEY)return new H;throw new Error(`[waniwani] createFlow "${e}": no flow store configured. Pass { store } to .compile() \u2014 use MemoryKvStore from "@waniwani/sdk/mcp" for local development, or plug in a Redis/Upstash/Cloudflare KV adapter for production. Alternatively, set WANIWANI_API_KEY to use hosted flow state on app.waniwani.ai.`)}function Ye(e){let{config:t,nodes:n,edges:o}=e,r=jt(t),i=qe(t,n,o,e.nodeOptions),s=He(t),a=`${t.description}
5
- ${s}`,l=e.store??Lt(t.id),d=new Map;async function c(f,k,g,E){if(f.action==="start"){let h=typeof f.intent=="string"?f.intent.trim():void 0;if(!h)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};if(f.intent=h,typeof f.context=="string"){let x=f.context.trim();f.context=x||void 0}let w=o.get(C);if(!w)return{content:{status:"error",error:"No start edge"}};let T=Y(f.stateUpdates??{}),R=await A(w,T);return q(R,T,n,o,d,g,E,e.nodeOptions,t.state)}if(f.action==="continue"){if(!k)return{content:{status:"error",error:"No session ID available for continue action."}};let h;try{h=await l.get(k)}catch(x){let I=x instanceof Error?x.message:String(x);return{content:{status:"error",error:`Failed to load flow state (session "${k}"): ${I}`}}}if(!h)return{content:{status:"error",error:`Flow state not found for session "${k}". The flow may have expired.`}};let w=h.state,T=h.step;if(!T)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let R=P(w,Y(f.stateUpdates??{}));if(h.widgetId){let x=o.get(T);if(!x)return{content:{status:"error",error:`No edge from step "${T}"`}};let I=await A(x,R);return q(I,R,n,o,d,g,E,e.nodeOptions,t.state)}return q(T,R,n,o,d,g,E,e.nodeOptions,t.state)}if(f.action==="reset"){if(!k)return{content:{status:"error",error:"No session ID available for reset action."}};let h;try{h=await l.get(k)}catch(I){let N=I instanceof Error?I.message:String(I);return{content:{status:"error",error:`Failed to load flow state (session "${k}"): ${N}`}}}if(!h)return{content:{status:"error",error:`Flow state not found for session "${k}". The flow may have completed or expired. Use action "start" to begin a new flow.`}};if(!h.step)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};if(!f.stateUpdates||Object.keys(f.stateUpdates).length===0)return{content:{status:"error",error:'Missing "stateUpdates" for action "reset". Include the corrected field(s).'}};let w=o.get(C);if(!w)return{content:{status:"error",error:"No start edge"}};let T=h.state,R=P(T,Y(f.stateUpdates)),x=await A(w,R);return q(x,R,n,o,d,g,E,e.nodeOptions,t.state)}return{content:{status:"error",error:`Unknown action: "${f.action}"`}}}let u=Ge(t.state),p={title:t.title,description:a,inputSchema:r,annotations:t.annotations,...u.length>0&&{_meta:{[J]:u}}},v=(async(f,k)=>{let g=k,E=g._meta??{},h=F(E),w=h??f.sessionId;!w&&f.action==="start"&&(w=crypto.randomUUID(),E["waniwani/sessionId"]=w);let T=G(g),R=await c(f,w,E,T);if(w&&R.flowTokenContent)try{await l.set(w,R.flowTokenContent)}catch(N){let z=N instanceof Error?N.message:String(N);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${w}"): ${z}`},null,2)}],_meta:E,isError:!0}}let x=!h&&w?{...R.content,sessionId:w}:R.content,I=[{type:"text",text:JSON.stringify(x,null,2)}];return R.nodesVisited?.length&&(E[U]={flowId:t.id,nodesVisited:R.nodesVisited}),{content:I,_meta:E,...R.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:p,handler:v,async register(f){let k={...p,_meta:{...p._meta,_flowGraph:i}};f.registerTool(t.id,k,v)},graph:e.graph,flowGraph:i}}function Ze(e,t){let n=["flowchart TD"];n.push(` ${C}((Start))`);for(let[o]of e)n.push(` ${o}[${o}]`);n.push(` ${_}((End))`);for(let[o,r]of t)r.type==="direct"?n.push(` ${o} --> ${r.to}`):n.push(` ${o} -.-> ${o}_branch([?])`);return n.join(`
6
- `)}var K=class{nodes=new Map;edges=new Map;nodeOptions=new Map;config;constructor(t){this.config=t}addNode(t,n,o){let r=typeof t=="string"?t:t.id,i=typeof t=="string"?n:t.run,s=typeof t=="string"?o?.label:t.label,a=typeof t=="string"?o?.hideFromFunnel:t.hideFromFunnel;if(r===C||r===_)throw new Error(`"${r}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(r))throw new Error(`Node "${r}" already exists`);return this.nodes.set(r,i),(s!==void 0||a!==void 0)&&this.nodeOptions.set(r,{label:s??r,hideFromFunnel:a}),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return Ze(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),o=new Map(this.edges);return Ye({config:this.config,nodes:n,edges:o,store:t?.store,graph:()=>Ze(n,o),nodeOptions:new Map(this.nodeOptions)})}validate(){if(!this.edges.has(C))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(C);if(t?.type==="direct"&&t.to!==_&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,o]of this.edges){if(n!==C&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(o.type==="direct"&&o.to!==_&&!this.nodes.has(o.to))throw new Error(`Edge from "${n}" references non-existent node: "${o.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function Je(e){return new K(e)}function fe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Xe(e,t){let n=t?.stateStore,o=[],r=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...d)=>{o.push(d)}};await e.register(i);let s=o[0]?.[2];if(!s)throw new Error(`Flow "${e.name}" did not register a handler`);let a={_meta:{sessionId:r}};async function l(d){return{...d,decodedState:n?await n.get(r):null}}return{async start(d,c,u){let p=await s({action:"start",intent:d,...u?{context:u}:{},...c?{stateUpdates:c}:{}},a);return l(fe(p))},async continueWith(d){let c=await s({action:"continue",...d?{stateUpdates:d}:{}},a);return l(fe(c))},async resetWith(d){let c=await s({action:"reset",stateUpdates:d},a);return l(fe(c))},async lastState(){return n?n.get(r):null}}}var X=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};var $t="@waniwani/sdk";function Qe(e){let{apiUrl:t,apiKey:n}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function r(i,s,a){let l=o(),d=`${t.replace(/\/$/,"")}${s}`,c={Authorization:`Bearer ${l}`,"X-WaniWani-SDK":$t},u={method:i,headers:c};a!==void 0&&(c["Content-Type"]="application/json",u.body=JSON.stringify(a));let p=await fetch(d,u);if(!p.ok){let f=await p.text().catch(()=>"");throw new X(f||`KB API error: HTTP ${p.status}`,p.status)}return(await p.json()).data}return{async ingest(i){return r("POST","/api/mcp/kb/ingest",{files:i})},async search(i,s){return r("POST","/api/mcp/kb/search",{query:i,...s})},async sources(){return r("GET","/api/mcp/kb/sources")}}}import{existsSync as Bt,readFileSync as qt}from"fs";import{resolve as Ht}from"path";var Vt="waniwani.json",j;function et(){if(j!==void 0)return j;try{let e=Ht(process.cwd(),Vt);if(!Bt(e))return j=null,null;let t=qt(e,"utf-8");return j=JSON.parse(t),j}catch{return j=null,null}}var zt="__waniwani_config__";function tt(){return globalThis[zt]}var Gt="@waniwani/sdk";function ee(e,t={}){let n=t.now??(()=>new Date),o=t.generateId??nt,r=Jt(e),i=Q(e.meta),s=Q(e.metadata),a=Xt(e,i),l=b(e.eventId)??o(),d=Qt(e.timestamp,n),c=b(e.source)??$(i)??t.source??Gt,u=ge(e)?{...e}:void 0,p={...s};return Object.keys(i).length>0&&(p.meta=i),u&&(p.rawLegacy=u),{id:l,type:"mcp.event",name:r,source:c,timestamp:d,correlation:a,properties:Yt(e,r),metadata:p,rawLegacy:u}}function nt(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Yt(e,t){if(!ge(e))return Q(e.properties);let n=Zt(e,t),o=Q(e.properties);return{...n,...o}}function Zt(e,t){switch(t){case"tool.called":{let n={};return b(e.toolName)&&(n.name=e.toolName),b(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),b(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return b(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),b(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Jt(e){return ge(e)?e.eventType:e.event}function Xt(e,t){let n=b(e.requestId)??Pe(t),o=b(e.sessionId)??F(t),r=b(e.traceId)??We(t),i=b(e.externalUserId)??Ne(t),s=b(e.correlationId)??Ae(t)??n,a={};return o&&(a.sessionId=o),r&&(a.traceId=r),n&&(a.requestId=n),s&&(a.correlationId=s),i&&(a.externalUserId=i),a}function Qt(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 Q(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function b(e){if(typeof e=="string"&&e.trim().length!==0)return e}function ge(e){return"eventType"in e}var en="/api/mcp/events/v2/batch";var ot="@waniwani/sdk",tn=new Set([401,403]),nn=new Set([408,425,429,500,502,503,504]);function rt(e){return new we(e)}var we=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=sn(t.apiUrl,t.endpointPath??en),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(o=>setTimeout(o,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,o=this.flush();if(!Number.isFinite(n)||n<=0)return await o,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let r=Symbol("shutdown-timeout");return await Promise.race([o.then(()=>"flushed"),this.sleep(n).then(()=>r)])===r?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,o=t;for(;o.length>0&&!this.isStopped;){this.inFlightCount=o.length;let r=await this.sendBatchOnce(o);switch(this.inFlightCount=0,r.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(r.status,o.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",o.length,r.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",o.length,r.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(r.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",r.permanent.length),r.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",r.retryable.length);return}o=r.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":ot},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:an(i)}}if(tn.has(n.status))return{kind:"auth",status:n.status};if(nn.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let o=await rn(n);if(!o?.rejected||o.rejected.length===0)return{kind:"success"};let r=this.classifyRejectedEvents(t,o.rejected);return r.retryable.length===0&&r.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:r.retryable,permanent:r.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ot,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let o=new Map(t.map(s=>[s.id,s])),r=[],i=[];for(let s of n){let a=o.get(s.eventId);if(a){if(on(s)){r.push(a);continue}i.push(a)}}return{retryable:r,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let o=this.buffer.length;this.buffer.splice(0,o),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+o)}};function on(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 rn(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function sn(e,t){let n=e.endsWith("/")?e:`${e}/`,o=t.startsWith("/")?t.slice(1):t;return`${n}${o}`}function an(e){return e instanceof Error?e.message:String(e)}function it(e){let{apiUrl:t,apiKey:n,tracking:o}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?rt({apiUrl:t,apiKey:n,endpointPath:o.endpointPath,flushIntervalMs:o.flushIntervalMs,maxBatchSize:o.maxBatchSize,maxBufferSize:o.maxBufferSize,maxRetries:o.maxRetries,retryBaseDelayMs:o.retryBaseDelayMs,retryMaxDelayMs:o.retryMaxDelayMs,shutdownTimeoutMs:o.shutdownTimeoutMs}):void 0,s={async identify(a,l,d){r();let c=ee({event:"user.identified",externalUserId:a,properties:l,meta:d});return i?.enqueue(c),{eventId:c.id}},async track(a){r();let l=ee(a);return i?.enqueue(l),{eventId:l.id}},async flush(){r(),await i?.flush()},async shutdown(a){return r(),await i?.shutdown({timeoutMs:a?.timeoutMs??o.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&dn(s,o.shutdownTimeoutMs),s}function dn(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 te(e){let n=e??et()??tt(),o=n?.apiUrl??"https://app.waniwani.ai",r=n?.apiKey??process.env.WANIWANI_API_KEY,i={endpointPath:n?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:n?.tracking?.flushIntervalMs??1e3,maxBatchSize:n?.tracking?.maxBatchSize??20,maxBufferSize:n?.tracking?.maxBufferSize??1e3,maxRetries:n?.tracking?.maxRetries??3,retryBaseDelayMs:n?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:n?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:n?.tracking?.shutdownTimeoutMs??2e3},s={apiUrl:o,apiKey:r,tracking:i},a=it(s),l=Qe(s);return{...a,kb:l,_config:s}}function cn(e){let t=e.event_type??"widget_click",o=t.startsWith("widget_")?t:`widget_${t}`,r={...e.metadata??{}};return e.event_name&&(r.event_name=e.event_name),{event:o,properties:r,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function ln(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function o(){return n||(n=te(t)),n}return async function(i){let s;try{s=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(s.events)||s.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=o(),l=[];for(let d of s.events){let c=cn(d),u=await a.track(c);l.push(u.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:l.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let l=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:l}),{status:500,headers:{"Content-Type":"application/json"}})}}}var me=D("widget-token"),un=120*1e3,ne=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-un?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let o=pn(this.config.apiUrl,"/api/mcp/widget-tokens");me("minting token from",o);let r={};t&&(r.sessionId=t),n&&(r.traceId=n);try{let i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(r),signal:AbortSignal.timeout(5e3)});if(me("mint response:",i.status),!i.ok)return null;let s=await i.json(),a=s.data&&typeof s.data=="object"?s.data:s,l=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(l)?null:(this.cached={token:a.token,expiresAt:l},a.token)}catch(i){return me("mint failed:",i),null}}};function pn(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}async function st(e){if(typeof globalThis.crypto?.subtle?.digest=="function"){let n=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(e));return Array.from(new Uint8Array(n)).map(o=>o.toString(16).padStart(2,"0")).join("")}let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n)|0;return`simple-${Math.abs(t).toString(36)}`}function fn(e){return st(JSON.stringify({nodes:e.nodes,edges:e.edges}))}async function at(e){if(e.length===0)return null;let t=[...e].sort((r,i)=>r.flowId.localeCompare(i.flowId)),n=await st(JSON.stringify(t.map(r=>({flowId:r.flowId,nodes:r.nodes,edges:r.edges})))),o=await Promise.all(e.map(async r=>({flowId:r.flowId,title:r.title,configHash:await fn(r),nodes:r.nodes,edges:r.edges})));return{compositeHash:n,flows:o}}var dt="waniwani/sessionId",oe="waniwani/geoLocation",re="waniwani/userLocation",gn="openai/userLocation",wn=[gn,oe,re];function ct(e,t){if(t.length===0)return e;let n;for(let o of wn){let r=e[o];if(!S(r))continue;let i;for(let s of t)s in r&&(i||(i={...r}),delete i[s]);i&&(n||(n={...e}),n[o]=i)}return n??e}function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function V(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function ye(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(o=>S(o)&&o.type==="text"&&typeof o.text=="string")?.text:void 0}function mn(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function he(e,t,n,o,r,i){let s=mn(e,n.toolType),a=n.stripLocationFields,l=a&&a.length>0,d=V(t),c=d&&l?ct(d,a):d,u=i?.input!==void 0&&n.redactInput?n.redactInput(i.input):i?.input,p=l&&S(i?.output)&&S(i.output._meta)?{...i.output,_meta:ct(i.output._meta,a)}:i?.output,f=(S(i?.output)&&S(i.output._meta)?i.output._meta:void 0)?.[U],k=f&&c?{...c,[U]:f}:f?{[U]:f}:c;return{event:"tool.called",properties:{name:e,type:s,...o??{},...u!==void 0&&{input:u},...p!==void 0&&{output:p}},meta:k,source:$(c),metadata:{...n.metadata??{},...r&&{clientInfo:r},...n.funnelSync&&{funnelSync:n.funnelSync}}}}async function Te(e,t,n){try{await e.track(t)}catch(o){n?.(ke(o))}}async function Se(e,t){try{await e.flush()}catch(n){t?.(ke(n))}}async function ut(e,t,n,o,r){if(!S(e))return;S(e._meta)||(e._meta={});let i=e._meta,s=S(i.waniwani)?i.waniwani:void 0,a={...s??{},endpoint:s?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(a.token=u)}catch(u){r?.(ke(u))}let l=F(i);l&&(a.sessionId||(a.sessionId=l));let d=gt(i);d!==void 0&&(a.geoLocation||(a.geoLocation=d));let c=$(V(o));c&&!a.source&&(a.source=c),i.waniwani=a}var lt=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function pt(e,t){if(!t||!S(e))return;let n=!1;for(let r of lt)if(r in t){n=!0;break}if(!n)return;S(e._meta)||(e._meta={});let o=e._meta;for(let r of lt)r in t&&(r in o||(o[r]=t[r]))}function ft(e,t){let n=V(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let o=e._meta,r=F(n);r&&!o[dt]&&(o[dt]=r);let i=gt(n);i&&(o[oe]||(o[oe]=i),o[re]||(o[re]=i))}function gt(e){if(!e)return;let t=e[oe]??e[re];if(S(t)||typeof t=="string")return t}function ke(e){return e instanceof Error?e:new Error(String(e))}function wt(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function mt(e){if(typeof e.sessionId=="string"&&e.sessionId)return e.sessionId;if(wt(e.requestInfo)){let t=e.requestInfo.headers;if(wt(t)){let n={};for(let i of Object.keys(t))n[i.toLowerCase()]=t[i];let o=n["mcp-session-id"];if(typeof o=="string"&&o)return o;let r=n["x-waniwani-session-id"];if(typeof r=="string"&&r)return r}}}var ht=Symbol.for("waniwani.wrappedHandler"),ie=D("mcp"),Tt="https://app.waniwani.ai",yn="REDACTED";function hn(e){if(!e)return;let t=e[J];if(!Array.isArray(t)||t.length===0)return;let n=new Set(t.filter(o=>typeof o=="string"));if(n.size!==0)return o=>{if(!S(o))return o;let r=o.stateUpdates;if(!S(r))return o;let i=!1,s={...r};for(let a of n)a in s&&(s[a]=yn,i=!0);return i?{...o,stateUpdates:s}:o}}function yt(e,t,n,o){let{server:r,tracker:i,opts:s,tokenCache:a,injectToken:l}=n,d=s.applyFieldRedactions===!0?hn(o):void 0,c=async(u,p)=>{let v=d?{...s,redactInput:d,funnelSync:n.funnelSync}:{...s,funnelSync:n.funnelSync},f=V(p)??{};if(!F(f)&&S(p)){let w=mt(p);w&&(f["waniwani/sessionId"]=w,p._meta=f)}let g=Fe(i,f,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});S(p)&&(p[de]=g);let E=performance.now(),h=r.server?.getClientVersion?.();try{let w=await t(u,p),T=Math.round(performance.now()-E);ie(`tool "${e}" handler returned in ${T}ms, running post-processing...`);let R=S(w)&&w.isError===!0;if(R){let x=ye(w);console.error(`[waniwani] Tool "${e}" returned error${x?`: ${x}`:""}`)}return await Te(i,he(e,p,v,{durationMs:T,status:R?"error":"ok",...R&&{errorMessage:ye(w)??"Unknown tool error"}},h,{input:u,output:w}),s.onError),ie(`tool "${e}" tracking done`),s.flushAfterToolCall&&await Se(i,s.onError),ft(w,p),pt(w,o),l&&(await ut(w,a,i._config.apiUrl??Tt,p,s.onError),ie(`tool "${e}" widget config injected`)),ie(`tool "${e}" post-processing complete, returning result`),w}catch(w){let T=Math.round(performance.now()-E);throw await Te(i,he(e,p,v,{durationMs:T,status:"error",errorMessage:w instanceof Error?w.message:String(w)},h,{input:u}),s.onError),s.flushAfterToolCall&&await Se(i,s.onError),w}};return c[ht]=!0,c}async function Tn(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let o=t??{},r=o.client??te(),i=o.injectWidgetToken!==!1,s=r._config.apiKey?new ne({apiUrl:r._config.apiUrl??Tt,apiKey:r._config.apiKey}):null,a={server:e,tracker:r,opts:o,tokenCache:s,injectToken:i,funnelSync:null},l=e.registerTool.bind(e);n.registerTool=((...c)=>{let[u,p,v]=c;if(typeof v!="function")return l(...c);let f=typeof u=="string"&&u.trim().length>0?u:"unknown",k=S(p)&&S(p._meta)?p._meta:void 0,g=yt(f,v,a,k);return l(u,p,g)});let d=e._registeredTools;if(S(d))for(let[c,u]of Object.entries(d)){if(!S(u))continue;let p=u.handler;if(typeof p!="function"||p[ht])continue;let v=S(u._meta)?u._meta:void 0;u.handler=yt(c,p,a,v)}if(r._config.apiKey){let c=e._registeredTools,u=[];if(c&&typeof c=="object"){for(let p of Object.values(c))if(p&&typeof p=="object"){let v=p._meta,f=v&&typeof v=="object"?v._flowGraph:void 0;f?.nodes?.length&&u.push(f)}}u.length>0&&(a.funnelSync=await at(u))}return n}function ve(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Sn(){return ve()==="openai"}function kn(){return ve()==="mcp-apps"}var se="text/html+skybridge",ae="text/html;profile=mcp-app",St=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function kt(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function vt(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function Re(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Rt(e){let{id:t,title:n,description:o,baseUrl:r,htmlPath:i,widgetDomain:s,prefersBorder:a=!0,autoHeight:l=!0}=e,d=e.widgetCSP??{connect_domains:[r],resource_domains:[r]};if(process.env.NODE_ENV==="development")try{let{hostname:g}=new URL(r);(g==="localhost"||g==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${g}:*`,`wss://${g}:*`],resource_domains:[...d.resource_domains||[],`http://${g}:*`]})}catch{}let c=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,p=null,v=()=>(p||(p=St(r,i)),p),f=o;async function k(g){let E=await v();g.registerResource(`${t}-openai-widget`,c,{title:n,description:f,mimeType:se,_meta:{"openai/widgetDescription":f,"openai/widgetPrefersBorder":a}},async h=>({contents:[{uri:h.href,mimeType:se,text:E,_meta:kt({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]})),g.registerResource(`${t}-mcp-widget`,u,{title:n,description:f,mimeType:ae,_meta:{ui:{prefersBorder:a}}},async h=>({contents:[{uri:h.href,mimeType:ae,text:E,_meta:vt({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]}))}return{id:t,title:n,description:o,openaiUri:c,mcpUri:u,autoHeight:l,register:k}}function vn(e,t){let{resource:n,description:o,inputSchema:r,annotations:i,autoInjectResultText:s=!0}=e,a=e.id??n?.id,l=e.title??n?.title;if(!a)throw new Error("createTool: `id` is required when no resource is provided");if(!l)throw new Error("createTool: `title` is required when no resource is provided");let d=n?Re({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:a,title:l,description:o,async register(c){c.registerTool(a,{title:l,description:o,inputSchema:r,annotations:i,...d&&{_meta:d}},(async(u,p)=>{let v=p,f=v._meta??{},k=G(v),g=await t(u,{extra:{_meta:f},waniwani:k});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...d,...f,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Rn(e,t){await Promise.all(t.map(n=>n.register(e)))}export{_ as END,Z as MemoryKvStore,C as START,K as StateGraph,O as WaniwaniKvStore,Je as createFlow,Xe as createFlowTestHarness,Rt as createResource,vn as createTool,ln as createTrackingRoute,ve as detectPlatform,kn as isMCPApps,Sn as isOpenAI,Ve as redacted,Rn as registerTools,Tn as withWaniwani};
3
+ ${U.context}`:`ERROR: ${b}`}:U),G=Le(A,g.context,d,c,l);if(G)return{...G,nodesVisited:u};break}}let w=o.get(d);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await M(w,c);continue}if(Pe(g)){let y=g.field;if(y&&ue(B(c,y))){let T=o.get(d);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await M(T,c);continue}let w=y?le(l,y):void 0;return{content:{status:"widget",tool:g.tool,...g.data!==void 0?{data:g.data}:{},description:`IMPORTANT: You MUST now call the ${g.tool} tool to display the widget. Do NOT skip this step`,interactive:g.interactive!==!1,...y?{field:y}:{},...w?{fieldSchema:w}:{}},flowTokenContent:{step:d,state:c,field:y,widgetId:g.tool},nodesVisited:u}}c=W(c,g);let I=o.get(d);if(!I)return{content:{status:"error",error:`No outgoing edge from node "${d}"`},nodesVisited:u};d=await M(I,c)}catch(v){return{content:{status:"error",error:v instanceof Error?v.message:String(v)},flowTokenContent:{step:d,state:c},nodesVisited:u}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"},nodesVisited:u}}function pe(e){return typeof e=="object"&&e!==null&&e.__ww_enc===1&&typeof e.ct=="string"&&typeof e.iv=="string"}var $e=new Map;async function qe(e){let t=$e.get(e);if(t)return t;let n=Buffer.from(e,"base64");if(n.length!==32)throw new Error("[WaniWani KV] WANIWANI_ENCRYPTION_KEY must be a base64-encoded 32-byte (256-bit) key.");let o=await globalThis.crypto.subtle.importKey("raw",n,{name:"AES-GCM"},!1,["encrypt","decrypt"]);return $e.set(e,o),o}async function Be(e,t){let n=await qe(t),o=globalThis.crypto.getRandomValues(new Uint8Array(12)),r=new TextEncoder().encode(JSON.stringify(e)),i=await globalThis.crypto.subtle.encrypt({name:"AES-GCM",iv:o},n,r);return{__ww_enc:1,ct:Buffer.from(i).toString("base64"),iv:Buffer.from(o).toString("base64")}}async function He(e,t){let n=await qe(t),o=Buffer.from(e.ct,"base64"),r=Buffer.from(e.iv,"base64"),i;try{i=await globalThis.crypto.subtle.decrypt({name:"AES-GCM",iv:r},n,o)}catch{throw new Error("[WaniWani KV] Decryption failed. The encryption key may be incorrect or the data may be corrupted.")}return JSON.parse(new TextDecoder().decode(i))}var Mt={debug:0,warn:1,error:2,none:3};function Ut(){let e=process.env.WANIWANI_LOG_LEVEL;return e&&e in Mt?e:process.env.WANIWANI_DEBUG?"debug":"none"}function O(e,t){return t??Ut()==="debug"?(...o)=>console.log(`[waniwani:${e}]`,...o):()=>{}}var Dt="@waniwani/sdk",Ot="https://app.waniwani.ai",jt=O("kv"),j=class{get baseUrl(){return(process.env.WANIWANI_API_URL??Ot).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}get encryptionKey(){return process.env.WANIWANI_ENCRYPTION_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let n=await this.request("/api/mcp/redis/get",{key:t});if(n==null)return null;if(pe(n)){if(!this.encryptionKey)throw new Error("[WaniWani KV] Encrypted data found but WANIWANI_ENCRYPTION_KEY is not set.");return He(n,this.encryptionKey)}return n}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let o=this.encryptionKey;jt(`set "${t}" \u2014 encryption ${o?"enabled":"disabled (no WANIWANI_ENCRYPTION_KEY)"}`);let r=o?await Be(n,o):n;await this.request("/api/mcp/redis/set",{key:t,value:r})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let o=`${this.baseUrl}${t}`,r=await fetch(o,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":Dt},body:JSON.stringify(n)});if(!r.ok){let s=await r.text().catch(()=>"");throw new Error(s||`KV store API error: HTTP ${r.status}`)}return(await r.json()).data}};var J=class{map=new Map;async get(t){return this.map.get(t)??null}async set(t,n){this.map.set(t,n)}async delete(t){this.map.delete(t)}};var V=class{store=new j;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Kt(e){let t=e.toString();return t.includes("showWidget")?"widget":t.includes("interrupt")?"interrupt":"action"}function Lt(e,t){let n=e.toString(),o=[];for(let r of t){let i=`"${r}"`,s=`'${r}'`,a=`\`${r}\``;(n.includes(i)||n.includes(s)||n.includes(a))&&o.push(r)}return o}function Ve(e,t,n,o){let r=new Set([...t.keys(),F]),i=[];for(let[a,l]of t){let d=o.get(a);i.push({id:a,type:Kt(l),label:d?.label??a,...d?.hideFromFunnel?{hideFromFunnel:!0}:{}})}let s=[];for(let[a,l]of n)if(l.type==="direct")s.push({from:a,to:l.to,type:"direct"});else{let d=Lt(l.condition,r);s.push({from:a,to:d,type:"conditional"})}return{flowId:e.id,title:e.title,nodes:i,edges:s}}import{z as k}from"zod";var ze=k.object({type:k.enum(["enum","string","number","boolean","object","array","unknown"]),values:k.array(k.string()).optional(),description:k.string().optional(),optional:k.boolean().optional()}).describe("JIT schema fragment for a state field \u2014 type, allowed values, and description."),$t=k.object({question:k.string(),field:k.string(),suggestions:k.array(k.string()).optional(),context:k.string().optional(),fieldSchema:ze.optional()}).describe("One question within a multi-question interrupt."),fe={status:k.enum(["interrupt","widget","complete","error"]).describe("Current flow status and the next action the assistant should take."),question:k.string().optional().describe("Single question to ask the user when status is interrupt."),field:k.string().optional().describe("State field to fill with the user's answer on the next continue call."),fieldSchema:ze.optional().describe("JIT schema fragment for the single-question shorthand."),suggestions:k.array(k.string()).optional().describe("Suggested answers for the single-question shorthand."),questions:k.array($t).optional().describe("Multiple questions to ask the user when status is interrupt."),context:k.string().optional().describe("Private instruction context for the assistant."),tool:k.string().optional().describe("Widget tool to call when status is widget."),data:k.record(k.string(),k.unknown()).optional().describe("Input data to pass to the widget tool."),description:k.string().optional().describe("Instruction for rendering the requested widget."),interactive:k.boolean().optional().describe("Whether the widget requires user interaction before continuing."),sessionId:k.string().optional().describe("Session identifier to pass on future continue and reset calls."),error:k.string().optional().describe("Error message when status is error.")};function Ge(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow."," Do NOT invent missing intent."," Optionally include `context` \u2014 the situation or environment that led the user to start"," this flow (e.g. what page they are on, what they were doing, or what triggered the request)."," Only provide `context` when there is genuinely relevant situational information. Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs (see the `stateUpdates` schema"," for the list of writable fields). 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."];return e.omitIntentPII&&t.push(" Do NOT include PII in `intent` or `context` \u2014 no names, emails, phones, addresses, IDs, ages, or birthdates.",' Summarize the goal abstractly (e.g. "user wants a quote", not "Jane Doe wants a quote").'),t.push(" For grouped fields (z.object state), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, fieldSchema?, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field, fieldSchema?}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `fieldSchema` (when present) describes the expected value: `{ type, values?, description?, optional? }`.",' Use it to validate before sending \u2014 match enum `values` exactly, coerce strings to numbers where `type: "number"`.'," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Check the `interactive` field in the response:"," \u2022 `interactive: true` \u2014 The widget requires user interaction. After calling the display tool,"," STOP and WAIT for the user to interact with the widget. Do NOT call this flow tool again"," until the user has responded. When they do, call with:",' `action: "continue"`,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned."," \u2022 `interactive: false` \u2014 The widget is display-only. Call the display tool, then immediately",' call THIS flow tool again with `action: "continue"`. Do NOT wait for user interaction.',' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","4. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped.","5. CORRECTION: If the user wants to CHANGE a previously-answered field",' (e.g. "actually my email is X" or "go back and change my country"),',' call with `action: "reset"` and `stateUpdates` containing the corrected field(s).'," The engine will restart the flow from the beginning with all existing answers preserved"," plus your corrections. Steps with filled answers will be auto-skipped."," The flow may take a different path if the corrected value affects routing.",' Do NOT use "reset" for the CURRENT question \u2014 use "continue" for that.',"6. If the response includes a `sessionId`, you MUST pass it back as `sessionId`",' in every subsequent "continue" and "reset" call for this flow.'),t.join(`
4
+ `)}function ge(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function Ye(e){let t=Ze(e),n=ge(t.waniwani)?t.waniwani:{};return e.meta({...t,waniwani:{...n,redacted:!0}})}function qt(e){let n=Ze(e).waniwani;return ge(n)&&n.redacted===!0}function Ze(e){let t=e.meta;if(typeof t!="function")return{};let n=t.call(e);return ge(n)?n:{}}function Je(e){if(!e)return[];let t=[];for(let[n,o]of Object.entries(e))qt(o)&&t.push(n);return t}var X="waniwani/redactedStateUpdateFields";function Bt(e){let t=e.omitIntentPII?" Do not include PII (names, emails, phones, addresses, IDs, ages, birthdates) \u2014 summarize abstractly.":"",o=e.state&&Object.keys(e.state).length>0?N.object(e.state).partial().passthrough():N.record(N.string(),N.unknown());return{action:N.enum(["start","continue","reset"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget), "reset" to restart from the beginning with a correction to a previously-collected field'),intent:N.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow. Do not invent missing intent.${t}`),context:N.string().optional().describe(`Optional when action is "start". Describe the situation or environment that led the user to start this flow \u2014 e.g. what page they are on, what they were doing, or what triggered the request. Do not invent missing context.${t}`),stateUpdates:o.optional().describe(`State field values to set before processing the next node. Pass the user's answer (keyed by the field name from the response) and any other values the user mentioned. For nested state fields, use dot-paths like "driver.name".`),sessionId:N.string().optional().describe('Session identifier. If the response includes a `sessionId`, pass it back on every subsequent "continue" and "reset" call for this flow.')}}function Ht(e){if(process.env.WANIWANI_API_KEY)return new V;throw new Error(`[waniwani] createFlow "${e}": no flow store configured. Pass { store } to .compile() \u2014 use MemoryKvStore from "@waniwani/sdk/mcp" for local development, or plug in a Redis/Upstash/Cloudflare KV adapter for production. Alternatively, set WANIWANI_API_KEY to use hosted flow state on app.waniwani.ai.`)}function Xe(e){let{config:t,nodes:n,edges:o}=e,r=Bt(t),i=Ve(t,n,o,e.nodeOptions),s=Ge(t),a=`${t.description}
5
+ ${s}`,l=e.store??Ht(t.id),d=new Map;async function c(f,v,g,I){if(f.action==="start"){let y=typeof f.intent=="string"?f.intent.trim():void 0;if(!y)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};if(f.intent=y,typeof f.context=="string"){let E=f.context.trim();f.context=E||void 0}let w=o.get(_);if(!w)return{content:{status:"error",error:"No start edge"}};let T=Z(f.stateUpdates??{}),R=await M(w,T);return H(R,T,n,o,d,g,I,e.nodeOptions,t.state)}if(f.action==="continue"){if(!v)return{content:{status:"error",error:"No session ID available for continue action."}};let y;try{y=await l.get(v)}catch(E){let b=E instanceof Error?E.message:String(E);return{content:{status:"error",error:`Failed to load flow state (session "${v}"): ${b}`}}}if(!y)return{content:{status:"error",error:`Flow state not found for session "${v}". The flow may have expired.`}};let w=y.state,T=y.step;if(!T)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let R=W(w,Z(f.stateUpdates??{}));if(y.widgetId){let E=o.get(T);if(!E)return{content:{status:"error",error:`No edge from step "${T}"`}};let b=await M(E,R);return H(b,R,n,o,d,g,I,e.nodeOptions,t.state)}return H(T,R,n,o,d,g,I,e.nodeOptions,t.state)}if(f.action==="reset"){if(!v)return{content:{status:"error",error:"No session ID available for reset action."}};let y;try{y=await l.get(v)}catch(b){let A=b instanceof Error?b.message:String(b);return{content:{status:"error",error:`Failed to load flow state (session "${v}"): ${A}`}}}if(!y)return{content:{status:"error",error:`Flow state not found for session "${v}". The flow may have completed or expired. Use action "start" to begin a new flow.`}};if(!y.step)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};if(!f.stateUpdates||Object.keys(f.stateUpdates).length===0)return{content:{status:"error",error:'Missing "stateUpdates" for action "reset". Include the corrected field(s).'}};let w=o.get(_);if(!w)return{content:{status:"error",error:"No start edge"}};let T=y.state,R=W(T,Z(f.stateUpdates)),E=await M(w,R);return H(E,R,n,o,d,g,I,e.nodeOptions,t.state)}return{content:{status:"error",error:`Unknown action: "${f.action}"`}}}let u=Je(t.state),p={title:t.title,description:a,inputSchema:r,outputSchema:fe,annotations:t.annotations,...u.length>0&&{_meta:{[X]:u}}},x=(async(f,v)=>{let g=v,I=g._meta??{},y=P(I),w=y??f.sessionId;!w&&f.action==="start"&&(w=crypto.randomUUID(),I["waniwani/sessionId"]=w);let T=Y(g),R=await c(f,w,I,T);if(w&&R.flowTokenContent)try{await l.set(w,R.flowTokenContent)}catch(A){let G=A instanceof Error?A.message:String(A);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${w}"): ${G}`},null,2)}],_meta:I,isError:!0}}let E=!y&&w?{...R.content,sessionId:w}:R.content,b=[{type:"text",text:JSON.stringify(E,null,2)}];return R.nodesVisited?.length&&(I[D]={flowId:t.id,nodesVisited:R.nodesVisited}),{content:b,structuredContent:E,_meta:I,...R.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:p,handler:x,async register(f){let v={...p,_meta:{...p._meta,_flowGraph:i}};f.registerTool(t.id,v,x)},graph:e.graph,flowGraph:i}}function Qe(e,t){let n=["flowchart TD"];n.push(` ${_}((Start))`);for(let[o]of e)n.push(` ${o}[${o}]`);n.push(` ${F}((End))`);for(let[o,r]of t)r.type==="direct"?n.push(` ${o} --> ${r.to}`):n.push(` ${o} -.-> ${o}_branch([?])`);return n.join(`
6
+ `)}var K=class{nodes=new Map;edges=new Map;nodeOptions=new Map;config;constructor(t){this.config=t}addNode(t,n,o){let r=typeof t=="string"?t:t.id,i=typeof t=="string"?n:t.run,s=typeof t=="string"?o?.label:t.label,a=typeof t=="string"?o?.hideFromFunnel:t.hideFromFunnel;if(r===_||r===F)throw new Error(`"${r}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(r))throw new Error(`Node "${r}" already exists`);return this.nodes.set(r,i),(s!==void 0||a!==void 0)&&this.nodeOptions.set(r,{label:s??r,hideFromFunnel:a}),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}graph(){return Qe(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),o=new Map(this.edges);return Xe({config:this.config,nodes:n,edges:o,store:t?.store,graph:()=>Qe(n,o),nodeOptions:new Map(this.nodeOptions)})}validate(){if(!this.edges.has(_))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(_);if(t?.type==="direct"&&t.to!==F&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,o]of this.edges){if(n!==_&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(o.type==="direct"&&o.to!==F&&!this.nodes.has(o.to))throw new Error(`Edge from "${n}" references non-existent node: "${o.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function et(e){return new K(e)}function we(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function tt(e,t){let n=t?.stateStore,o=[],r=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...d)=>{o.push(d)}};await e.register(i);let s=o[0]?.[2];if(!s)throw new Error(`Flow "${e.name}" did not register a handler`);let a={_meta:{sessionId:r}};async function l(d){return{...d,decodedState:n?await n.get(r):null}}return{async start(d,c,u){let p=await s({action:"start",intent:d,...u?{context:u}:{},...c?{stateUpdates:c}:{}},a);return l(we(p))},async continueWith(d){let c=await s({action:"continue",...d?{stateUpdates:d}:{}},a);return l(we(c))},async resetWith(d){let c=await s({action:"reset",stateUpdates:d},a);return l(we(c))},async lastState(){return n?n.get(r):null}}}var Q=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};var Vt="@waniwani/sdk";function nt(e){let{apiUrl:t,apiKey:n}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function r(i,s,a){let l=o(),d=`${t.replace(/\/$/,"")}${s}`,c={Authorization:`Bearer ${l}`,"X-WaniWani-SDK":Vt},u={method:i,headers:c};a!==void 0&&(c["Content-Type"]="application/json",u.body=JSON.stringify(a));let p=await fetch(d,u);if(!p.ok){let f=await p.text().catch(()=>"");throw new Q(f||`KB API error: HTTP ${p.status}`,p.status)}return(await p.json()).data}return{async ingest(i){return r("POST","/api/mcp/kb/ingest",{files:i})},async search(i,s){return r("POST","/api/mcp/kb/search",{query:i,...s})},async sources(){return r("GET","/api/mcp/kb/sources")}}}import{existsSync as zt,readFileSync as Gt}from"fs";import{resolve as Yt}from"path";var Zt="waniwani.json",L;function ot(){if(L!==void 0)return L;try{let e=Yt(process.cwd(),Zt);if(!zt(e))return L=null,null;let t=Gt(e,"utf-8");return L=JSON.parse(t),L}catch{return L=null,null}}var Jt="__waniwani_config__";function rt(){return globalThis[Jt]}var Xt="@waniwani/sdk";function te(e,t={}){let n=t.now??(()=>new Date),o=t.generateId??it,r=tn(e),i=ee(e.meta),s=ee(e.metadata),a=nn(e,i),l=C(e.eventId)??o(),d=on(e.timestamp,n),c=C(e.source)??q(i)??t.source??Xt,u=me(e)?{...e}:void 0,p={...s};return Object.keys(i).length>0&&(p.meta=i),u&&(p.rawLegacy=u),{id:l,type:"mcp.event",name:r,source:c,timestamp:d,correlation:a,properties:Qt(e,r),metadata:p,rawLegacy:u}}function it(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Qt(e,t){if(!me(e))return ee(e.properties);let n=en(e,t),o=ee(e.properties);return{...n,...o}}function en(e,t){switch(t){case"tool.called":{let n={};return C(e.toolName)&&(n.name=e.toolName),C(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),C(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return C(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),C(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function tn(e){return me(e)?e.eventType:e.event}function nn(e,t){let n=C(e.requestId)??Ne(t),o=C(e.sessionId)??P(t),r=C(e.traceId)??Ae(t),i=C(e.externalUserId)??Me(t),s=C(e.correlationId)??Ue(t)??n,a={};return o&&(a.sessionId=o),r&&(a.traceId=r),n&&(a.requestId=n),s&&(a.correlationId=s),i&&(a.externalUserId=i),a}function on(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 ee(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function C(e){if(typeof e=="string"&&e.trim().length!==0)return e}function me(e){return"eventType"in e}var rn="/api/mcp/events/v2/batch";var st="@waniwani/sdk",sn=new Set([401,403]),an=new Set([408,425,429,500,502,503,504]);function at(e){return new he(e)}var he=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=ln(t.apiUrl,t.endpointPath??rn),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(o=>setTimeout(o,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,o=this.flush();if(!Number.isFinite(n)||n<=0)return await o,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let r=Symbol("shutdown-timeout");return await Promise.race([o.then(()=>"flushed"),this.sleep(n).then(()=>r)])===r?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,o=t;for(;o.length>0&&!this.isStopped;){this.inFlightCount=o.length;let r=await this.sendBatchOnce(o);switch(this.inFlightCount=0,r.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(r.status,o.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",o.length,r.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",o.length,r.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(r.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",r.permanent.length),r.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",r.retryable.length);return}o=r.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":st},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:un(i)}}if(sn.has(n.status))return{kind:"auth",status:n.status};if(an.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let o=await cn(n);if(!o?.rejected||o.rejected.length===0)return{kind:"success"};let r=this.classifyRejectedEvents(t,o.rejected);return r.retryable.length===0&&r.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:r.retryable,permanent:r.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:st,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let o=new Map(t.map(s=>[s.id,s])),r=[],i=[];for(let s of n){let a=o.get(s.eventId);if(a){if(dn(s)){r.push(a);continue}i.push(a)}}return{retryable:r,permanent:i}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let o=this.buffer.length;this.buffer.splice(0,o),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+o)}};function dn(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 cn(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function ln(e,t){let n=e.endsWith("/")?e:`${e}/`,o=t.startsWith("/")?t.slice(1):t;return`${n}${o}`}function un(e){return e instanceof Error?e.message:String(e)}function dt(e){let{apiUrl:t,apiKey:n,tracking:o}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let i=n?at({apiUrl:t,apiKey:n,endpointPath:o.endpointPath,flushIntervalMs:o.flushIntervalMs,maxBatchSize:o.maxBatchSize,maxBufferSize:o.maxBufferSize,maxRetries:o.maxRetries,retryBaseDelayMs:o.retryBaseDelayMs,retryMaxDelayMs:o.retryMaxDelayMs,shutdownTimeoutMs:o.shutdownTimeoutMs}):void 0,s={async identify(a,l,d){r();let c=te({event:"user.identified",externalUserId:a,properties:l,meta:d});return i?.enqueue(c),{eventId:c.id}},async track(a){r();let l=te(a);return i?.enqueue(l),{eventId:l.id}},async flush(){r(),await i?.flush()},async shutdown(a){return r(),await i?.shutdown({timeoutMs:a?.timeoutMs??o.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&pn(s,o.shutdownTimeoutMs),s}function pn(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 ne(e){let n=e??ot()??rt(),o=n?.apiUrl??"https://app.waniwani.ai",r=n?.apiKey??process.env.WANIWANI_API_KEY,i={endpointPath:n?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:n?.tracking?.flushIntervalMs??1e3,maxBatchSize:n?.tracking?.maxBatchSize??20,maxBufferSize:n?.tracking?.maxBufferSize??1e3,maxRetries:n?.tracking?.maxRetries??3,retryBaseDelayMs:n?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:n?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:n?.tracking?.shutdownTimeoutMs??2e3},s={apiUrl:o,apiKey:r,tracking:i},a=dt(s),l=nt(s);return{...a,kb:l,_config:s}}function fn(e){let t=e.event_type??"widget_click",o=t.startsWith("widget_")?t:`widget_${t}`,r={...e.metadata??{}};return e.event_name&&(r.event_name=e.event_name),{event:o,properties:r,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function gn(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function o(){return n||(n=ne(t)),n}return async function(i){let s;try{s=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(s.events)||s.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=o(),l=[];for(let d of s.events){let c=fn(d),u=await a.track(c);l.push(u.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:l.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let l=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:l}),{status:500,headers:{"Content-Type":"application/json"}})}}}var ye=O("widget-token"),wn=120*1e3,oe=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-wn?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let o=mn(this.config.apiUrl,"/api/mcp/widget-tokens");ye("minting token from",o);let r={};t&&(r.sessionId=t),n&&(r.traceId=n);try{let i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(r),signal:AbortSignal.timeout(5e3)});if(ye("mint response:",i.status),!i.ok)return null;let s=await i.json(),a=s.data&&typeof s.data=="object"?s.data:s,l=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(l)?null:(this.cached={token:a.token,expiresAt:l},a.token)}catch(i){return ye("mint failed:",i),null}}};function mn(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}async function ct(e){if(typeof globalThis.crypto?.subtle?.digest=="function"){let n=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(e));return Array.from(new Uint8Array(n)).map(o=>o.toString(16).padStart(2,"0")).join("")}let t=0;for(let n=0;n<e.length;n++)t=(t<<5)-t+e.charCodeAt(n)|0;return`simple-${Math.abs(t).toString(36)}`}function hn(e){return ct(JSON.stringify({nodes:e.nodes,edges:e.edges}))}async function lt(e){if(e.length===0)return null;let t=[...e].sort((r,i)=>r.flowId.localeCompare(i.flowId)),n=await ct(JSON.stringify(t.map(r=>({flowId:r.flowId,nodes:r.nodes,edges:r.edges})))),o=await Promise.all(e.map(async r=>({flowId:r.flowId,title:r.title,configHash:await hn(r),nodes:r.nodes,edges:r.edges})));return{compositeHash:n,flows:o}}var ut="waniwani/sessionId",re="waniwani/geoLocation",ie="waniwani/userLocation",yn="openai/userLocation",Tn=[yn,re,ie];function pt(e,t){if(t.length===0)return e;let n;for(let o of Tn){let r=e[o];if(!S(r))continue;let i;for(let s of t)s in r&&(i||(i={...r}),delete i[s]);i&&(n||(n={...e}),n[o]=i)}return n??e}function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function z(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function Te(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(o=>S(o)&&o.type==="text"&&typeof o.text=="string")?.text:void 0}function Sn(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Se(e,t,n,o,r,i){let s=Sn(e,n.toolType),a=n.stripLocationFields,l=a&&a.length>0,d=z(t),c=d&&l?pt(d,a):d,u=i?.input!==void 0&&n.redactInput?n.redactInput(i.input):i?.input,p=l&&S(i?.output)&&S(i.output._meta)?{...i.output,_meta:pt(i.output._meta,a)}:i?.output,f=(S(i?.output)&&S(i.output._meta)?i.output._meta:void 0)?.[D],v=f&&c?{...c,[D]:f}:f?{[D]:f}:c;return{event:"tool.called",properties:{name:e,type:s,...o??{},...u!==void 0&&{input:u},...p!==void 0&&{output:p}},meta:v,source:q(c),metadata:{...n.metadata??{},...r&&{clientInfo:r},...n.funnelSync&&{funnelSync:n.funnelSync}}}}async function ke(e,t,n){try{await e.track(t)}catch(o){n?.(xe(o))}}async function ve(e,t){try{await e.flush()}catch(n){t?.(xe(n))}}async function gt(e,t,n,o,r){if(!S(e))return;S(e._meta)||(e._meta={});let i=e._meta,s=S(i.waniwani)?i.waniwani:void 0,a={...s??{},endpoint:s?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(a.token=u)}catch(u){r?.(xe(u))}let l=P(i);l&&(a.sessionId||(a.sessionId=l));let d=ht(i);d!==void 0&&(a.geoLocation||(a.geoLocation=d));let c=q(z(o));c&&!a.source&&(a.source=c),i.waniwani=a}var ft=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function wt(e,t){if(!t||!S(e))return;let n=!1;for(let r of ft)if(r in t){n=!0;break}if(!n)return;S(e._meta)||(e._meta={});let o=e._meta;for(let r of ft)r in t&&(r in o||(o[r]=t[r]))}function mt(e,t){let n=z(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let o=e._meta,r=P(n);r&&!o[ut]&&(o[ut]=r);let i=ht(n);i&&(o[re]||(o[re]=i),o[ie]||(o[ie]=i))}function ht(e){if(!e)return;let t=e[re]??e[ie];if(S(t)||typeof t=="string")return t}function xe(e){return e instanceof Error?e:new Error(String(e))}function yt(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Tt(e){if(typeof e.sessionId=="string"&&e.sessionId)return e.sessionId;if(yt(e.requestInfo)){let t=e.requestInfo.headers;if(yt(t)){let n={};for(let i of Object.keys(t))n[i.toLowerCase()]=t[i];let o=n["mcp-session-id"];if(typeof o=="string"&&o)return o;let r=n["x-waniwani-session-id"];if(typeof r=="string"&&r)return r}}}var kt=Symbol.for("waniwani.wrappedHandler"),se=O("mcp"),vt="https://app.waniwani.ai",kn="REDACTED";function vn(e){if(!e)return;let t=e[X];if(!Array.isArray(t)||t.length===0)return;let n=new Set(t.filter(o=>typeof o=="string"));if(n.size!==0)return o=>{if(!S(o))return o;let r=o.stateUpdates;if(!S(r))return o;let i=!1,s={...r};for(let a of n)a in s&&(s[a]=kn,i=!0);return i?{...o,stateUpdates:s}:o}}function St(e,t,n,o){let{server:r,tracker:i,opts:s,tokenCache:a,injectToken:l}=n,d=s.applyFieldRedactions===!0?vn(o):void 0,c=async(u,p)=>{let x=d?{...s,redactInput:d,funnelSync:n.funnelSync}:{...s,funnelSync:n.funnelSync},f=z(p)??{};if(!P(f)&&S(p)){let w=Tt(p);w&&(f["waniwani/sessionId"]=w,p._meta=f)}let g=We(i,f,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});S(p)&&(p[ce]=g);let I=performance.now(),y=r.server?.getClientVersion?.();try{let w=await t(u,p),T=Math.round(performance.now()-I);se(`tool "${e}" handler returned in ${T}ms, running post-processing...`);let R=S(w)&&w.isError===!0;if(R){let E=Te(w);console.error(`[waniwani] Tool "${e}" returned error${E?`: ${E}`:""}`)}return await ke(i,Se(e,p,x,{durationMs:T,status:R?"error":"ok",...R&&{errorMessage:Te(w)??"Unknown tool error"}},y,{input:u,output:w}),s.onError),se(`tool "${e}" tracking done`),s.flushAfterToolCall&&await ve(i,s.onError),mt(w,p),wt(w,o),l&&(await gt(w,a,i._config.apiUrl??vt,p,s.onError),se(`tool "${e}" widget config injected`)),se(`tool "${e}" post-processing complete, returning result`),w}catch(w){let T=Math.round(performance.now()-I);throw await ke(i,Se(e,p,x,{durationMs:T,status:"error",errorMessage:w instanceof Error?w.message:String(w)},y,{input:u}),s.onError),s.flushAfterToolCall&&await ve(i,s.onError),w}};return c[kt]=!0,c}async function xn(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let o=t??{},r=o.client??ne(),i=o.injectWidgetToken!==!1,s=r._config.apiKey?new oe({apiUrl:r._config.apiUrl??vt,apiKey:r._config.apiKey}):null,a={server:e,tracker:r,opts:o,tokenCache:s,injectToken:i,funnelSync:null},l=e.registerTool.bind(e);n.registerTool=((...c)=>{let[u,p,x]=c;if(typeof x!="function")return l(...c);let f=typeof u=="string"&&u.trim().length>0?u:"unknown",v=S(p)&&S(p._meta)?p._meta:void 0,g=St(f,x,a,v);return l(u,p,g)});let d=e._registeredTools;if(S(d))for(let[c,u]of Object.entries(d)){if(!S(u))continue;let p=u.handler;if(typeof p!="function"||p[kt])continue;let x=S(u._meta)?u._meta:void 0;u.handler=St(c,p,a,x)}if(r._config.apiKey){let c=e._registeredTools,u=[];if(c&&typeof c=="object"){for(let p of Object.values(c))if(p&&typeof p=="object"){let x=p._meta,f=x&&typeof x=="object"?x._flowGraph:void 0;f?.nodes?.length&&u.push(f)}}u.length>0&&(a.funnelSync=await lt(u))}return n}function Re(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Rn(){return Re()==="openai"}function En(){return Re()==="mcp-apps"}var ae="text/html+skybridge",de="text/html;profile=mcp-app",xt=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Rt(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Et(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 Ee(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function It(e){let{id:t,title:n,description:o,baseUrl:r,htmlPath:i,widgetDomain:s,prefersBorder:a=!0,autoHeight:l=!0}=e,d=e.widgetCSP??{connect_domains:[r],resource_domains:[r]};if(process.env.NODE_ENV==="development")try{let{hostname:g}=new URL(r);(g==="localhost"||g==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${g}:*`,`wss://${g}:*`],resource_domains:[...d.resource_domains||[],`http://${g}:*`]})}catch{}let c=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,p=null,x=()=>(p||(p=xt(r,i)),p),f=o;async function v(g){let I=await x();g.registerResource(`${t}-openai-widget`,c,{title:n,description:f,mimeType:ae,_meta:{"openai/widgetDescription":f,"openai/widgetPrefersBorder":a}},async y=>({contents:[{uri:y.href,mimeType:ae,text:I,_meta:Rt({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]})),g.registerResource(`${t}-mcp-widget`,u,{title:n,description:f,mimeType:de,_meta:{ui:{prefersBorder:a}}},async y=>({contents:[{uri:y.href,mimeType:de,text:I,_meta:Et({description:f,prefersBorder:a,widgetDomain:s,widgetCSP:d})}]}))}return{id:t,title:n,description:o,openaiUri:c,mcpUri:u,autoHeight:l,register:v}}function In(e,t){let{resource:n,description:o,inputSchema:r,annotations:i,autoInjectResultText:s=!0}=e,a=e.id??n?.id,l=e.title??n?.title;if(!a)throw new Error("createTool: `id` is required when no resource is provided");if(!l)throw new Error("createTool: `title` is required when no resource is provided");let d=n?Ee({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:a,title:l,description:o,async register(c){c.registerTool(a,{title:l,description:o,inputSchema:r,annotations:i,...d&&{_meta:d}},(async(u,p)=>{let x=p,f=x._meta??{},v=Y(x),g=await t(u,{extra:{_meta:f},waniwani:v});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...d,...f,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function bn(e,t){await Promise.all(t.map(n=>n.register(e)))}export{F as END,J as MemoryKvStore,_ as START,K as StateGraph,j as WaniwaniKvStore,et as createFlow,tt as createFlowTestHarness,It as createResource,In as createTool,gn as createTrackingRoute,Re as detectPlatform,En as isMCPApps,Rn as isOpenAI,Ye as redacted,bn as registerTools,xn as withWaniwani};
7
7
  //# sourceMappingURL=index.js.map