@waniwani/sdk 0.2.2-beta.3 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -301,6 +301,52 @@ type RegisteredResource = {
301
301
  register: (server: McpServer) => Promise<void>;
302
302
  };
303
303
 
304
+ type ToolHandlerContext = {
305
+ /** Raw MCP request extra data (includes _meta for session extraction) */
306
+ extra?: {
307
+ _meta?: Record<string, unknown>;
308
+ };
309
+ };
310
+ type ToolConfig<TInput extends ZodRawShapeCompat> = {
311
+ /** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
312
+ resource?: RegisteredResource;
313
+ /** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
314
+ id?: string;
315
+ /** Display title. Defaults to resource.title when resource is present, required otherwise. */
316
+ title?: string;
317
+ /** Action-oriented description for the tool (tells the model WHEN to use it) */
318
+ description: string;
319
+ /** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
320
+ widgetDescription?: string;
321
+ /** Input schema using zod */
322
+ inputSchema: TInput;
323
+ /** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
324
+ invoking?: string;
325
+ /** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
326
+ invoked?: string;
327
+ /** Annotations describe the tool's potential impact. */
328
+ annotations?: {
329
+ readOnlyHint?: boolean;
330
+ idempotentHint?: boolean;
331
+ openWorldHint?: boolean;
332
+ destructiveHint?: boolean;
333
+ };
334
+ };
335
+ type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
336
+ /** Text content to return */
337
+ text: string;
338
+ /** Structured data to pass to the widget. Only meaningful when resource is present. */
339
+ data?: Record<string, unknown>;
340
+ }>;
341
+ type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
342
+ type RegisteredTool = {
343
+ id: string;
344
+ title: string;
345
+ description: string;
346
+ /** Register the tool on the server */
347
+ register: (server: McpServer) => Promise<void>;
348
+ };
349
+
304
350
  declare const START: "__start__";
305
351
  declare const END: "__end__";
306
352
  declare const INTERRUPT: unique symbol;
@@ -329,9 +375,9 @@ type InterruptSignal = {
329
375
  };
330
376
  type WidgetSignal = {
331
377
  readonly __type: typeof WIDGET;
332
- /** The resource to display */
333
- resource: RegisteredResource;
334
- /** Data to pass to the widget as structuredContent */
378
+ /** The display tool to delegate rendering to */
379
+ tool: RegisteredTool;
380
+ /** Data to pass to the display tool */
335
381
  data: Record<string, unknown>;
336
382
  /** Description of what the widget does (for the AI's context) */
337
383
  description?: string;
@@ -370,12 +416,15 @@ declare function interrupt(config: {
370
416
  context?: string;
371
417
  }): InterruptSignal;
372
418
  /**
373
- * Create a widget signal — pauses the flow and renders a widget UI.
419
+ * Create a widget signal — pauses the flow and delegates rendering to a display tool.
420
+ *
421
+ * The display tool is a regular `createTool()` with a resource attached.
422
+ * The flow engine will instruct the model to call the display tool separately.
374
423
  *
375
424
  * Pass `field` to enable auto-skip: if the field is already in state, the widget
376
425
  * step will be skipped automatically.
377
426
  */
378
- declare function showWidget(resource: RegisteredResource, config: {
427
+ declare function showWidget(tool: RegisteredTool, config: {
379
428
  data: Record<string, unknown>;
380
429
  description?: string;
381
430
  field?: string;
@@ -394,14 +443,6 @@ type NodeConfig<TState extends Record<string, unknown> = Record<string, unknown>
394
443
  * the node is auto-skipped. (Alternatively, pass `field` to `showWidget()`.)
395
444
  */
396
445
  field?: Extract<keyof TState, string>;
397
- /**
398
- * Mark this node as conversational — the AI will engage in back-and-forth
399
- * conversation before advancing to the next node.
400
- *
401
- * - `true` — generic conversational behavior
402
- * - `string` — specific guidance for the AI (e.g., "Help the user compare plans")
403
- */
404
- conversational?: boolean | string;
405
446
  };
406
447
  /**
407
448
  * Node handler — a single function type for all node kinds.
@@ -584,52 +625,6 @@ declare function createFlow<const TSchema extends Record<string, z.ZodType>>(con
584
625
  */
585
626
  declare function createResource(config: ResourceConfig): RegisteredResource;
586
627
 
587
- type ToolHandlerContext = {
588
- /** Raw MCP request extra data (includes _meta for session extraction) */
589
- extra?: {
590
- _meta?: Record<string, unknown>;
591
- };
592
- };
593
- type ToolConfig<TInput extends ZodRawShapeCompat> = {
594
- /** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
595
- resource?: RegisteredResource;
596
- /** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
597
- id?: string;
598
- /** Display title. Defaults to resource.title when resource is present, required otherwise. */
599
- title?: string;
600
- /** Action-oriented description for the tool (tells the model WHEN to use it) */
601
- description: string;
602
- /** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
603
- widgetDescription?: string;
604
- /** Input schema using zod */
605
- inputSchema: TInput;
606
- /** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
607
- invoking?: string;
608
- /** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
609
- invoked?: string;
610
- /** Annotations describe the tool's potential impact. */
611
- annotations?: {
612
- readOnlyHint?: boolean;
613
- idempotentHint?: boolean;
614
- openWorldHint?: boolean;
615
- destructiveHint?: boolean;
616
- };
617
- };
618
- type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
619
- /** Text content to return */
620
- text: string;
621
- /** Structured data to pass to the widget. Only meaningful when resource is present. */
622
- data?: Record<string, unknown>;
623
- }>;
624
- type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
625
- type RegisteredTool = {
626
- id: string;
627
- title: string;
628
- description: string;
629
- /** Register the tool on the server */
630
- register: (server: McpServer) => Promise<void>;
631
- };
632
-
633
628
  /**
634
629
  * Creates an MCP tool with minimal boilerplate.
635
630
  *
package/dist/mcp/index.js CHANGED
@@ -1,15 +1,4 @@
1
- function U(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function he(){return U()==="openai"}function me(){return U()==="mcp-apps"}var v="__start__",S="__end__",O=Symbol.for("waniwani.flow.interrupt"),H=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:O,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:O,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:H,resource:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===O}function X(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===H}import{z as _}from"zod";var R="text/html+skybridge",I="text/html;profile=mcp-app",J=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Z(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function G(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function M(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}}}}import{deflateSync as we,inflateSync as ye}from"zlib";function Q(e){let t=JSON.stringify(e);return we(t).toString("base64")}function ee(e){try{let t=Buffer.from(e,"base64"),n=ye(t).toString("utf-8");return JSON.parse(n)}catch{return null}}var te=`<!DOCTYPE html>
2
- <html><head><style>html,body{margin:0;padding:0;height:0;overflow:hidden}</style></head>
3
- <body><script>
4
- window.addEventListener('message',function(e){
5
- var d=e.data;
6
- if(!d||d.jsonrpc!=='2.0')return;
7
- if(d.method==='ui/initialize'){
8
- e.source.postMessage({jsonrpc:'2.0',id:d.id,result:{capabilities:{}}},'*');
9
- e.source.postMessage({jsonrpc:'2.0',method:'ui/notifications/size-changed',params:{height:0}},'*');
10
- }
11
- });
12
- </script></body></html>`;function Te(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function ke(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin. If the user\'s message already'," contains answers to likely questions, extract them into `stateUpdates`"," as `{ field: value }` pairs. The engine will auto-skip steps whose"," fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=Object.entries(e.state).map(([r,o])=>{let a=Te(o);return a?`\`${r}\` (${a})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: A widget UI is being shown. The user will interact with the widget.'," When the user makes a choice, call again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped.",'6. CONVERSATIONAL STEPS: When a response includes a `"conversational"` field:'," - Do NOT immediately call `continue`. Instead, engage in back-and-forth conversation."," - The user may ask follow-up questions, explore options, or request changes.",' - Only call `action: "continue"` when the user explicitly wants to move on',` (e.g., "looks good", "let's continue", "I'll go with X", or selects an option).`," - If `conversational` is a string, use it as guidance for what topics to discuss."," - While conversing, do NOT call the tool \u2014 just respond naturally to the user."),t.join(`
13
- `)}function ve(e){if(e.flowToken){let t=ee(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function C(e,t){return e.type==="direct"?e.to:e.condition(t)}function q(e){return e!=null&&e!==""}function ne(e,t,n,r,o){if(e.every(p=>q(r[p.field])))return null;let a=e.filter(p=>!q(r[p.field])),i=a.length===1,s=a[0],d=o?typeof o=="string"?o:!0:void 0;return{payload:i&&s?{status:"interrupt",question:s.question,field:s.field,...s.suggestions?{suggestions:s.suggestions}:{},...s.context||t?{context:s.context??t}:{},...d?{conversational:d}:{}}:{status:"interrupt",questions:a,...t?{context:t}:{},...d?{conversational:d}:{}},flowMeta:{step:n,state:r,...i&&s?{field:s.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function B(e,t,n,r,o,a){let i=e,s={...t},d=50,g=0;for(;g++<d;){if(i===S)return{payload:{status:"complete"},flowMeta:{state:s}};let p=n.get(i);if(!p)return{payload:{status:"error",error:`Unknown node: "${i}"`}};try{let c=await p(s,a);if(Y(c)){let f=ne(c.questions,c.context,i,s,r.get(i)?.conversational);if(f)return f;let l=o.get(i);if(!l)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(l,s);continue}if(X(c)){let f=c.field??r.get(i)?.field;if(f&&q(s[f])){let y=o.get(i);if(!y)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(y,s);continue}let l=c.resource,h=r.get(i)?.conversational;return{payload:{status:"widget",description:c.description,...h?{conversational:typeof h=="string"?h:!0}:{}},data:c.data,widgetMeta:M({openaiTemplateUri:l.openaiUri,mcpTemplateUri:l.mcpUri,invoking:"Loading...",invoked:"Loaded",autoHeight:l.autoHeight}),flowMeta:{step:i,state:s,field:f,widgetId:l.id}}}s={...s,...c};let u=o.get(i);if(!u)return{payload:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(u,s)}catch(c){return{payload:{status:"error",error:c instanceof Error?c.message:String(c)},flowMeta:{step:i,state:s}}}}return{payload:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Se={action:_.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:_.record(_.string(),_.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned."),flowToken:_.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function re(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,a=ke(t),i=`${t.description}
14
- ${a}`;async function s(d,g){let p=ve(d),c=p.state;if(d.action==="start"){let u=o.get(v);if(!u)return{payload:{status:"error",error:"No start edge"}};let f={...c,...d.stateUpdates??{}},l=await C(u,f);return B(l,f,n,r,o,g)}if(d.action==="continue"){let u=p.step;if(!u)return{payload:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let f={...c,...d.stateUpdates??{}};if(p.questions){let l=ne(p.questions,p.interruptContext,u,f,r.get(u)?.conversational);if(l)return l}if(p.questions||p.widgetId){let l=o.get(u);if(!l)return{payload:{status:"error",error:`No edge from step "${u}"`}};let h=await C(l,f);return B(h,f,n,r,o,g)}return B(u,f,n,r,o,g)}return{payload:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:i,async register(d){let g=`ui://widgets/apps-sdk/${t.id}_flow.html`,p=`ui://widgets/ext-apps/${t.id}_flow.html`;d.registerResource(`${t.id}-flow-openai-widget`,g,{mimeType:R,_meta:{"openai/widgetPrefersBorder":!1}},async c=>({contents:[{uri:c.href,mimeType:R,text:te}]})),d.registerResource(`${t.id}-flow-mcp-widget`,p,{mimeType:I,_meta:{ui:{prefersBorder:!1}}},async c=>({contents:[{uri:c.href,mimeType:I,text:te}]})),d.registerTool(t.id,{title:t.title,description:i,inputSchema:Se,annotations:t.annotations,_meta:M({openaiTemplateUri:g,mcpTemplateUri:p,invoking:"Loading...",invoked:"Loaded"})},(async(c,u)=>{let l=u._meta??{},h=await s(c,l),y=h.flowMeta?Q({step:h.flowMeta.step??"",state:h.flowMeta.state,field:h.flowMeta.field,widgetId:h.flowMeta.widgetId,questions:h.flowMeta.questions,interruptContext:h.flowMeta.interruptContext}):void 0,E={...h.payload,...y?{flowToken:y,flowId:t.id,...h.flowMeta?.widgetId?{widgetId:h.flowMeta.widgetId}:{}}:{}},T=[{type:"text",text:JSON.stringify(E,null,2)}],x={...h.widgetMeta??{},...l};return h.widgetMeta?{content:T,structuredContent:h.data,_meta:x}:{content:T,...Object.keys(x).length>0?{_meta:x}:{}}}))}}}var b=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===v||t===S)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);let o,a={};if(typeof n=="function")o=n;else if(r)o=r,a=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,a),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),re({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(v))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(v);if(t?.type==="direct"&&t.to!==S&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==v&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==S&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function oe(e){return new b(e)}function ie(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:d=!0}=e,g=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:y}=new URL(o);(y==="localhost"||y==="127.0.0.1")&&(g={...g,connect_domains:[...g.connect_domains||[],`ws://${y}:*`,`wss://${y}:*`],resource_domains:[...g.resource_domains||[],`http://${y}:*`]})}catch{}let p=`ui://widgets/apps-sdk/${t}.html`,c=`ui://widgets/ext-apps/${t}.html`,u=null,f=()=>(u||(u=J(o,a)),u),l=r;async function h(y){let E=await f();y.registerResource(`${t}-openai-widget`,p,{title:n,description:l,mimeType:R,_meta:{"openai/widgetDescription":l,"openai/widgetPrefersBorder":s}},async T=>({contents:[{uri:T.href,mimeType:R,text:E,_meta:Z({description:l,prefersBorder:s,widgetDomain:i,widgetCSP:g})}]})),y.registerResource(`${t}-mcp-widget`,c,{title:n,description:l,mimeType:I,_meta:{ui:{prefersBorder:s}}},async T=>({contents:[{uri:T.href,mimeType:I,text:E,_meta:G({description:l,prefersBorder:s,widgetCSP:g})}]}))}return{id:t,title:n,description:r,openaiUri:p,mcpUri:c,autoHeight:d,register:h}}function Ee(e,t){let{resource:n,description:r,inputSchema:o,annotations:a}=e,i=e.id??n?.id,s=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!s)throw new Error("createTool: `title` is required when no resource is provided");let d=n?M({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:s,description:r,async register(g){g.registerTool(i,{title:s,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(p,c)=>{let f=c._meta??{},l=await t(p,{extra:{_meta:f}});return n&&l.data?{content:[{type:"text",text:l.text}],structuredContent:l.data,_meta:{...d,...f}}:{content:[{type:"text",text:l.text}]}}))}}}async function xe(e,t){await Promise.all(t.map(n=>n.register(e)))}var W=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Re="@waniwani/sdk";function se(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let d=r(),g=`${t.replace(/\/$/,"")}${i}`,p={Authorization:`Bearer ${d}`,"X-WaniWani-SDK":Re},c={method:a,headers:p};s!==void 0&&(p["Content-Type"]="application/json",c.body=JSON.stringify(s));let u=await fetch(g,c);if(!u.ok){let l=await u.text().catch(()=>"");throw new W(l||`KB API error: HTTP ${u.status}`,u.status)}return(await u.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Ie="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??ae,o=_e(e),a=A(e.meta),i=A(e.metadata),s=Ce(e,a),d=k(e.eventId)??r(),g=Pe(e.timestamp,n),p=k(e.source)??t.source??Ie,c=L(e)?{...e}:void 0,u={...i};return Object.keys(a).length>0&&(u.meta=a),c&&(u.rawLegacy=c),{id:d,type:"mcp.event",name:o,source:p,timestamp:g,correlation:s,properties:Me(e,o),metadata:u,rawLegacy:c}}function ae(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Me(e,t){if(!L(e))return A(e.properties);let n=be(e,t),r=A(e.properties);return{...n,...r}}function be(e,t){switch(t){case"tool.called":{let n={};return k(e.toolName)&&(n.name=e.toolName),k(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),k(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return k(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),k(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function _e(e){return L(e)?e.eventType:e.event}function Ce(e,t){let n=k(e.requestId)??P(t,["openai/requestId","requestId","mcp/requestId"]),r=k(e.sessionId)??P(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=k(e.traceId)??P(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=k(e.externalUserId)??P(t,["openai/userId","externalUserId","userId","actorId"]),i=k(e.correlationId)??P(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function Pe(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function P(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function A(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function k(e){if(typeof e=="string"&&e.trim().length!==0)return e}function L(e){return"eventType"in e}var We="/api/mcp/events/v2/batch";var de="@waniwani/sdk",Ae=new Set([401,403]),Fe=new Set([408,425,429,500,502,503,504]);function ce(e){return new $(e)}var $=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=Ue(t.baseUrl,t.endpointPath??We),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":de},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Oe(a)}}if(Ae.has(n.status))return{kind:"auth",status:n.status};if(Fe.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await De(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:de,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Ne(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function Ne(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 De(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function Ue(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Oe(e){return e instanceof Error?e.message:String(e)}function ue(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?ce({baseUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,i={async track(s){o();let d=j(s);return a?.enqueue(d),{eventId:d.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&Be(i,r.shutdownTimeoutMs),i}function Be(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 F(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=ue(o),i=se(o);return{...a,kb:i,_config:o}}function qe(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function je(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=F(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),d=[];for(let g of i.events){let p=qe(g),c=await s.track(p);d.push(c.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:d.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let d=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:d}),{status:500,headers:{"Content-Type":"application/json"}})}}}var N=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=Le(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,d=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(d)?null:(this.cached={token:s.token,expiresAt:d},s.token)}catch{return null}}};function Le(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var le="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??F(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=r._config.apiKey;return d?(a=new N({baseUrl:r._config.baseUrl??le,apiKey:d}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[g,p,c]=d,u=typeof g=="string"&&g.trim().length>0?g:"unknown";if(typeof c!="function")return s(...d);let f=c;return s(g,p,async(h,y)=>{let E=performance.now();try{let T=await f(h,y),x=Math.round(performance.now()-E);return await ge(r,pe(u,y,t,{durationMs:x,status:"ok"}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),o&&await Ve(T,i(),r._config.baseUrl??le,t.onError),T}catch(T){let x=Math.round(performance.now()-E);throw await ge(r,pe(u,y,t,{durationMs:x,status:"error",errorMessage:T instanceof Error?T.message:String(T)}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),T}})}),n}async function Ve(e,t,n,r){if(!D(e))return;D(e._meta)||(e._meta={});let o=e._meta,a={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(a.token=i)}catch(i){r?.(V(i))}o.waniwani=a}function pe(e,t,n,r){let o=He(e,n.toolType),a=ze(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function He(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ze(e){if(!D(e))return;let t=e._meta;if(D(t))return t}function D(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function ge(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function fe(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{S as END,v as START,b as StateGraph,oe as createFlow,ie as createResource,Ee as createTool,je as createTrackingRoute,U as detectPlatform,z as interrupt,me as isMCPApps,he as isOpenAI,xe as registerTools,K as showWidget,$e as withWaniwani};
1
+ function D(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ge(){return D()==="openai"}function he(){return D()==="mcp-apps"}var k="__start__",S="__end__",U=Symbol.for("waniwani.flow.interrupt"),H=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:U,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:U,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:H,tool:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===U}function J(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===H}import{z as R}from"zod";import{deflateSync as me,inflateSync as we}from"zlib";function X(e){let t=JSON.stringify(e);return me(t).toString("base64")}function Z(e){try{let t=Buffer.from(e,"base64"),n=we(t).toString("utf-8");return JSON.parse(n)}catch{return null}}function ye(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Te(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin. If the user\'s message already'," contains answers to likely questions, extract them into `stateUpdates`"," as `{ field: value }` pairs. The engine will auto-skip steps whose"," fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=Object.entries(e.state).map(([r,o])=>{let s=ye(o);return s?`\`${r}\` (${s})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Present the widget result to the user. When the user makes a choice or interacts"," with the widget, call THIS flow tool again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped."),t.join(`
2
+ `)}function ke(e){if(e.flowToken){let t=Z(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function I(e,t){return e.type==="direct"?e.to:e.condition(t)}function O(e){return e!=null&&e!==""}function G(e,t,n,r){if(e.every(c=>O(r[c.field])))return null;let o=e.filter(c=>!O(r[c.field])),s=o.length===1,i=o[0];return{content:s&&i?{status:"interrupt",question:i.question,field:i.field,...i.suggestions?{suggestions:i.suggestions}:{},...i.context||t?{context:i.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&i?{field:i.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function B(e,t,n,r,o,s){let i=e,a={...t},c=50,l=0;for(;l++<c;){if(i===S)return{content:{status:"complete"},flowTokenContent:{state:a}};let f=n.get(i);if(!f)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await f(a,s);if(Y(d)){let g=G(d.questions,d.context,i,a);if(g)return g;let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}if(J(d)){let g=d.field??r.get(i)?.field;if(g&&O(a[g])){let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}return{content:{status:"widget",tool:d.tool.id,data:d.data,description:d.description},flowTokenContent:{step:i,state:a,field:g,widgetId:d.tool.id}}}a={...a,...d};let u=o.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(u,a)}catch(d){return{content:{status:"error",error:d instanceof Error?d.message:String(d)},flowTokenContent:{step:i,state:a}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Se={action:R.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:R.record(R.string(),R.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned."),flowToken:R.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function Q(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,s=Te(t),i=`${t.description}
3
+ ${s}`;async function a(c,l){let f=ke(c),d=f.state;if(c.action==="start"){let u=o.get(k);if(!u)return{content:{status:"error",error:"No start edge"}};let g={...d,...c.stateUpdates??{}},p=await I(u,g);return B(p,g,n,r,o,l)}if(c.action==="continue"){let u=f.step;if(!u)return{content:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let g={...d,...c.stateUpdates??{}};if(f.questions){let p=G(f.questions,f.interruptContext,u,g);if(p)return p}if(f.questions||f.widgetId){let p=o.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let v=await I(p,g);return B(v,g,n,r,o,l)}return B(u,g,n,r,o,l)}return{content:{status:"error",error:`Unknown action: "${c.action}"`}}}return{id:t.id,title:t.title,description:i,async register(c){c.registerTool(t.id,{title:t.title,description:i,inputSchema:Se,annotations:t.annotations},(async(l,f)=>{let u=f._meta??{},g=await a(l,u),p=g.flowTokenContent?X(g.flowTokenContent):void 0,v={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(v,null,2)}],...u}}))}}}var E=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===k||t===S)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);let o,s={};if(typeof n=="function")o=n;else if(r)o=r,s=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,s),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),Q({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(k))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(k);if(t?.type==="direct"&&t.to!==S&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==k&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==S&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function ee(e){return new E(e)}var b="text/html+skybridge",_="text/html;profile=mcp-app",te=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function ne(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function re(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 q(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}}}}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:i,prefersBorder:a=!0,autoHeight:c=!0}=e,l=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...l.resource_domains||[],`http://${w}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,u=null,g=()=>(u||(u=te(o,s)),u),p=r;async function v(w){let x=await g();w.registerResource(`${t}-openai-widget`,f,{title:n,description:p,mimeType:b,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":a}},async T=>({contents:[{uri:T.href,mimeType:b,text:x,_meta:ne({description:p,prefersBorder:a,widgetDomain:i,widgetCSP:l})}]})),w.registerResource(`${t}-mcp-widget`,d,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:a}}},async T=>({contents:[{uri:T.href,mimeType:_,text:x,_meta:re({description:p,prefersBorder:a,widgetCSP:l})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:d,autoHeight:c,register:v}}function ve(e,t){let{resource:n,description:r,inputSchema:o,annotations:s}=e,i=e.id??n?.id,a=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!a)throw new Error("createTool: `title` is required when no resource is provided");let c=n?q({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:a,description:r,async register(l){l.registerTool(i,{title:a,description:r,inputSchema:o,annotations:s,...c&&{_meta:c}},(async(f,d)=>{let g=d._meta??{},p=await t(f,{extra:{_meta:g}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...c,...g}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var M=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,i,a){let c=r(),l=`${t.replace(/\/$/,"")}${i}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":xe},d={method:s,headers:f};a!==void 0&&(f["Content-Type"]="application/json",d.body=JSON.stringify(a));let u=await fetch(l,d);if(!u.ok){let p=await u.text().catch(()=>"");throw new M(p||`KB API error: HTTP ${u.status}`,u.status)}return(await u.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,i){return o("POST","/api/mcp/kb/search",{query:s,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),s=P(e.meta),i=P(e.metadata),a=_e(e,s),c=y(e.eventId)??r(),l=Me(e.timestamp,n),f=y(e.source)??t.source??Re,d=$(e)?{...e}:void 0,u={...i};return Object.keys(s).length>0&&(u.meta=s),d&&(u.rawLegacy=d),{id:c,type:"mcp.event",name:o,source:f,timestamp:l,correlation:a,properties:Ie(e,o),metadata:u,rawLegacy:d}}function se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ie(e,t){if(!$(e))return P(e.properties);let n=Ce(e,t),r=P(e.properties);return{...n,...r}}function Ce(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function _e(e,t){let n=y(e.requestId)??C(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??C(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??C(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),s=y(e.externalUserId)??C(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??C(t,["correlationId","openai/requestId"])??n,a={};return r&&(a.sessionId=r),o&&(a.traceId=o),n&&(a.requestId=n),i&&(a.correlationId=i),s&&(a.externalUserId=s),a}function Me(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 C(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function P(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Fe=new Set([408,425,429,500,502,503,504]);function ce(e){return new L(e)}var L=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=De(t.baseUrl,t.endpointPath??Pe),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Ue(s)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Fe.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ae,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],s=[];for(let i of n){let a=r.get(i.eventId);if(a){if(Ae(i)){o.push(a);continue}s.push(a)}}return{retryable:o,permanent:s}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function Ae(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function de(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?ce({baseUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,i={async track(a){o();let c=j(a);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(a){return o(),await s?.shutdown({timeoutMs:a?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&Be(i,r.shutdownTimeoutMs),i}function Be(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 W(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},s=de(o),i=ie(o);return{...s,kb:i,_config:o}}function Oe(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=W(t)),n}return async function(s){let i;try{i=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=r(),c=[];for(let l of i.events){let f=Oe(l),d=await a.track(f);c.push(d.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let c=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=je(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let s=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!s.ok)return null;let i=await s.json(),a=i.data&&typeof i.data=="object"?i.data:i,c=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(c)?null:(this.cached={token:a.token,expiresAt:c},a.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??W(t.config),o=t.injectWidgetToken!==!1,s=null;function i(){if(s)return s;let c=r._config.apiKey;return c?(s=new F({baseUrl:r._config.baseUrl??ue,apiKey:c}),s):null}let a=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[l,f,d]=c,u=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof d!="function")return a(...c);let g=d;return a(l,f,async(v,w)=>{let x=performance.now();try{let T=await g(v,w),N=Math.round(performance.now()-x);return await le(r,pe(u,w,t,{durationMs:N,status:"ok"}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),o&&await Le(T,i(),r._config.baseUrl??ue,t.onError),T}catch(T){let N=Math.round(performance.now()-x);throw await le(r,pe(u,w,t,{durationMs:N,status:"error",errorMessage:T instanceof Error?T.message:String(T)}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),T}})}),n}async function Le(e,t,n,r){if(!A(e))return;A(e._meta)||(e._meta={});let o=e._meta,s={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(s.token=i)}catch(i){r?.(V(i))}o.waniwani=s}function pe(e,t,n,r){let o=Ve(e,n.toolType),s=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:s,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!A(e))return;let t=e._meta;if(A(t))return t}function A(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function le(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function fe(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{S as END,k as START,E as StateGraph,ee as createFlow,oe as createResource,ve as createTool,qe as createTrackingRoute,D as detectPlatform,z as interrupt,he as isMCPApps,ge as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
15
4
  //# sourceMappingURL=index.js.map