@waniwani/sdk 0.4.3 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chat/index.js +6 -6
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/next-js/index.d.ts +6 -1
- package/dist/chat/next-js/index.js +3 -3
- package/dist/chat/next-js/index.js.map +1 -1
- package/dist/chat/styles.css +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kb/index.d.ts +5 -0
- package/dist/mcp/index.d.ts +227 -242
- package/dist/mcp/index.js +4 -4
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react.js +5 -5
- package/dist/mcp/react.js.map +1 -1
- package/package.json +1 -1
package/dist/mcp/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
function
|
|
1
|
+
function V(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Ne(){return V()==="openai"}function Oe(){return V()==="mcp-apps"}var I="__start__",_="__end__",ie=Symbol.for("waniwani.flow.interrupt"),se=Symbol.for("waniwani.flow.widget");function ae(e,t){let n=t?.context,r=[];for(let[o,a]of Object.entries(e))if(typeof a=="object"&&a!==null&&"question"in a){let s=a;r.push({question:s.question,field:o,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:ie,questions:r,context:n}}function ce(e,t){return{__type:se,tool:e,...t}}function de(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ie}function ue(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===se}import{z as N}from"zod";var H="waniwani/client";function U(e){if(typeof e=="object"&&e!==null)return e[H]}function le(e,t){return{track(n){return e.track({...n,meta:{...t,...n.meta}})},identify(n,r){return e.identify(n,r,t)},kb:e.kb}}function W(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var Be=["waniwani/sessionId","openai/sessionId","sessionId","conversationId","anthropic/sessionId"],je=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],qe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],$e=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Le=["correlationId","openai/requestId"];function b(e){return e?W(e,Be):void 0}function pe(e){return e?W(e,je):void 0}function fe(e){return e?W(e,qe):void 0}function ge(e){return e?W(e,$e):void 0}function me(e){return e?W(e,Le):void 0}function z(e){return e!=null&&e!==""}async function P(e,t){return e.type==="direct"?e.to:e.condition(t)}function he(e,t,n,r){if(e.every(c=>z(r[c.field])))return null;let o=e.filter(c=>!z(r[c.field])),a=o.length===1,s=o[0];return{content:a&&s?{status:"interrupt",question:s.question,field:s.field,...s.suggestions?{suggestions:s.suggestions}:{},...s.context||t?{context:s.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...a&&s?{field:s.field}:{}}}}async function D(e,t,n,r,o,a,s){let i=e,c={...t},d=50,f=0;for(;f++<d;){if(i===_)return{content:{status:"complete"},flowTokenContent:{state:c}};let h=n.get(i);if(!h)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let l=await h({state:c,meta:a,interrupt:ae,showWidget:ce,waniwani:s});if(de(l)){for(let T of l.questions)T.validate&&o.set(`${i}:${T.field}`,T.validate);let y=he(l.questions,l.context,i,c);if(y)return y;for(let T of l.questions){let x=o.get(`${i}:${T.field}`);if(x)try{let C=c[T.field],R=await x(C);R&&typeof R=="object"&&(c={...c,...R})}catch(C){let R=C instanceof Error?C.message:String(C);delete c[T.field];let k=l.questions.map(E=>E.field===T.field?{...E,context:E.context?`ERROR: ${R}
|
|
2
2
|
|
|
3
|
-
${
|
|
4
|
-
`)}
|
|
5
|
-
${o}`,i=e.store??new _,s=new Map;async function c(l,w,f){if(l.action==="start"){let d=r.get(x);if(!d)return{content:{status:"error",error:"No start edge"}};let T={...l.stateUpdates??{}},u=await R(d,T);return W(u,T,n,r,s,f)}if(l.action==="continue"){let d=await se(l,i,w);if(!d)return{content:{status:"error",error:"Flow state not found for continue action. Pass back the flowToken from the previous response exactly as received."}};let T=d.state,u=d.step;if(!u)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let g={...T,...l.stateUpdates??{}};if(d.widgetId){let p=r.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let S=await R(p,g);return W(S,g,n,r,s,f)}return W(u,g,n,r,s,f)}return{content:{status:"error",error:`Unknown action: "${l.action}"`}}}return{id:t.id,title:t.title,description:a,async register(l){l.registerTool(t.id,{title:t.title,description:a,inputSchema:Ue,annotations:t.annotations},(async(w,f)=>{let T=f._meta??{},u=te(T),g=await c(w,u,T),p;if(g.flowTokenContent)if(u)await i.set(u,g.flowTokenContent),p=u;else{let k=re();await i.set(k,g.flowTokenContent),p=k}let S={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(S,null,2)}],_meta:T,...g.content.status==="error"?{isError:!0}:{}}}))}}}var C=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===x||t===E)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(t){return this.validate(),ae({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(x))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(x);if(t?.type==="direct"&&t.to!==E&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==x&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==E&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function ce(e){return new C(e)}function de(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function le(e,t){if(!e)return null;if(t){let n=await t.get(e);if(n)return n}return I(e)}async function ue(e,t){let n=t?.stateStore,r=[],o={registerTool:(...c)=>{r.push(c)}};await e.register(o);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.id}" did not register a handler`);let i;async function s(c){return i=c.flowToken,{...c,decodedState:await le(i,n)}}return{async start(c){let l=await a({action:"start",...c?{stateUpdates:c}:{}},{});return s(de(l))},async continueWith(c){if(!i)throw new Error("No flowToken \u2014 call start() first");let l=await a({action:"continue",flowToken:i,...c?{stateUpdates:c}:{}},{});return s(de(l))},async lastState(){return le(i,n)}}}var A="text/html+skybridge",D="text/html;profile=mcp-app",pe=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function fe(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function ge(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function L(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}}}}function me(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:c=!0}=e,l=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:p}=new URL(o);(p==="localhost"||p==="127.0.0.1")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${p}:*`,`wss://${p}:*`],resource_domains:[...l.resource_domains||[],`http://${p}:*`]})}catch{}let w=`ui://widgets/apps-sdk/${t}.html`,f=`ui://widgets/ext-apps/${t}.html`,d=null,T=()=>(d||(d=pe(o,a)),d),u=r;async function g(p){let S=await T();p.registerResource(`${t}-openai-widget`,w,{title:n,description:u,mimeType:A,_meta:{"openai/widgetDescription":u,"openai/widgetPrefersBorder":s}},async y=>({contents:[{uri:y.href,mimeType:A,text:S,_meta:fe({description:u,prefersBorder:s,widgetDomain:i,widgetCSP:l})}]})),p.registerResource(`${t}-mcp-widget`,f,{title:n,description:u,mimeType:D,_meta:{ui:{prefersBorder:s}}},async y=>({contents:[{uri:y.href,mimeType:D,text:S,_meta:ge({description:u,prefersBorder:s,widgetCSP:l})}]}))}return{id:t,title:n,description:r,openaiUri:w,mcpUri:f,autoHeight:c,register:g}}function Ne(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:i=!0}=e,s=e.id??n?.id,c=e.title??n?.title;if(!s)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let l=n?L({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:s,title:c,description:r,async register(w){w.registerTool(s,{title:c,description:r,inputSchema:o,annotations:a,...l&&{_meta:l}},(async(f,d)=>{let u=d._meta??{},g=await t(f,{extra:{_meta:u}});return n&&g.data?{content:[{type:"text",text:g.text}],structuredContent:g.data,_meta:{...l,...u,...i===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:g.text}],...g.data?{structuredContent:g.data}:{},...i===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Oe(e,t){await Promise.all(t.map(n=>n.register(e)))}var U=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Be="@waniwani/sdk";function he(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let c=r(),l=`${t.replace(/\/$/,"")}${i}`,w={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Be},f={method:a,headers:w};s!==void 0&&(w["Content-Type"]="application/json",f.body=JSON.stringify(s));let d=await fetch(l,f);if(!d.ok){let u=await d.text().catch(()=>"");throw new U(u||`KB API error: HTTP ${d.status}`,d.status)}return(await d.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var je="@waniwani/sdk";function O(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??we,o=Le(e),a=N(e.meta),i=N(e.metadata),s=Ke(e,a),c=v(e.eventId)??r(),l=He(e.timestamp,n),w=v(e.source)??t.source??je,f=K(e)?{...e}:void 0,d={...i};return Object.keys(a).length>0&&(d.meta=a),f&&(d.rawLegacy=f),{id:c,type:"mcp.event",name:o,source:w,timestamp:l,correlation:s,properties:qe(e,o),metadata:d,rawLegacy:f}}function we(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function qe(e,t){if(!K(e))return N(e.properties);let n=$e(e,t),r=N(e.properties);return{...n,...r}}function $e(e,t){switch(t){case"tool.called":{let n={};return v(e.toolName)&&(n.name=e.toolName),v(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),v(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return v(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),v(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Le(e){return K(e)?e.eventType:e.event}function Ke(e,t){let n=v(e.requestId)??P(t,["openai/requestId","requestId","mcp/requestId"]),r=v(e.sessionId)??P(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=v(e.traceId)??P(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=v(e.externalUserId)??P(t,["openai/userId","externalUserId","userId","actorId"]),i=v(e.correlationId)??P(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function He(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function P(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function N(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function K(e){return"eventType"in e}var Ve="/api/mcp/events/v2/batch";var ye="@waniwani/sdk",ze=new Set([401,403]),Ye=new Set([408,425,429,500,502,503,504]);function Te(e){return new H(e)}var H=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=Ze(t.baseUrl,t.endpointPath??Ve),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":ye},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ge(a)}}if(ze.has(n.status))return{kind:"auth",status:n.status};if(Ye.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Xe(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ye,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Je(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function Je(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function Xe(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function Ze(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ge(e){return e instanceof Error?e.message:String(e)}function ke(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?Te({baseUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,i={async identify(s,c){o();let l=O({event:"user.identified",externalUserId:s,properties:c});return a?.enqueue(l),{eventId:l.id}},async track(s){o();let c=O(s);return a?.enqueue(c),{eventId:c.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&Qe(i,r.shutdownTimeoutMs),i}function Qe(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function B(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=ke(o),i=he(o);return{...a,kb:i,_config:o}}function et(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function tt(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=B(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),c=[];for(let l of i.events){let w=et(l),f=await s.track(w);c.push(f.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let c=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var j=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=nt(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch{return null}}};function nt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Se="https://app.waniwani.ai";function rt(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??B(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let c=r._config.apiKey;return c?(a=new j({baseUrl:r._config.baseUrl??Se,apiKey:c}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[l,w,f]=c,d=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof f!="function")return s(...c);let T=f;return s(l,w,async(g,p)=>{let S=performance.now();try{let y=await T(g,p),k=Math.round(performance.now()-S);return await xe(r,ve(d,p,t,{durationMs:k,status:"ok"}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),o&&await ot(y,i(),r._config.baseUrl??Se,t.onError),st(y,p),y}catch(y){let k=Math.round(performance.now()-S);throw await xe(r,ve(d,p,t,{durationMs:k,status:"error",errorMessage:y instanceof Error?y.message:String(y)}),t.onError),t.flushAfterToolCall&&await Ee(r,t.onError),y}})}),n}async function ot(e,t,n,r){if(!F(e))return;F(e._meta)||(e._meta={});let o=e._meta,a={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(a.token=i)}catch(i){r?.(z(i))}o.waniwani=a}function ve(e,t,n,r){let o=it(e,n.toolType),a=Re(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function it(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Re(e){if(!F(e))return;let t=e._meta;if(F(t))return t}function F(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function xe(e,t,n){try{await e.track(t)}catch(r){n?.(z(r))}}async function Ee(e,t){try{await e.flush()}catch(n){t?.(z(n))}}function z(e){return e instanceof Error?e:new Error(String(e))}var V="waniwani/userLocation";function st(e,t){let n=Re(t);if(!n)return;let r=n[V];if(!r||!F(e))return;F(e._meta)||(e._meta={});let o=e._meta;o[V]||(o[V]=r)}export{E as END,x as START,C as StateGraph,ce as createFlow,ue as createFlowTestHarness,me as createResource,Ne as createTool,tt as createTrackingRoute,I as decodeFlowToken,q as detectPlatform,ie as encodeFlowToken,Fe as isMCPApps,Ce as isOpenAI,Oe as registerTools,rt as withWaniwani};
|
|
3
|
+
${E.context}`:`ERROR: ${R}`}:E),M=he(k,l.context,i,c);if(M)return M;break}}let u=r.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await P(u,c);continue}if(ue(l)){let y=l.field;if(y&&z(c[y])){let u=r.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await P(u,c);continue}return{content:{status:"widget",tool:l.tool.id,data:l.data,description:l.description,interactive:l.interactive!==!1},flowTokenContent:{step:i,state:c,field:y,widgetId:l.tool.id}}}c={...c,...l};let w=r.get(i);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await P(w,c)}catch(p){return{content:{status:"error",error:p instanceof Error?p.message:String(p)},flowTokenContent:{step:i,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Ke="@waniwani/sdk",Ve="https://app.waniwani.ai",A=class{baseUrl;apiKey;constructor(t){this.baseUrl=(t?.baseUrl??process.env.WANIWANI_BASE_URL??Ve).replace(/\/$/,""),this.apiKey=t?.apiKey??process.env.WANIWANI_API_KEY}async get(t){if(!this.apiKey)return null;try{return await this.request("/api/mcp/redis/get",{key:t})??null}catch{return null}}async set(t,n){if(this.apiKey)try{await this.request("/api/mcp/redis/set",{key:t,value:n})}catch{}}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch{}}async request(t,n){let r=`${this.baseUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":Ke},body:JSON.stringify(n)});if(!o.ok){let s=await o.text().catch(()=>"");throw new Error(s||`Flow state API error: HTTP ${o.status}`)}return(await o.json()).data}};function He(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 we(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=He(o);return a?`\`${r}\` (${a})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`,'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," 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."),t.join(`
|
|
4
|
+
`)}var ze={action:N.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:N.record(N.string(),N.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned.")};function ye(e){let{config:t,nodes:n,edges:r}=e,o=we(t),a=`${t.description}
|
|
5
|
+
${o}`,s=e.store??new A,i=new Map;async function c(d,f,h,p){if(d.action==="start"){let l=r.get(I);if(!l)return{content:{status:"error",error:"No start edge"}};let w={...d.stateUpdates??{}},y=await P(l,w);return D(y,w,n,r,i,h,p)}if(d.action==="continue"){if(!f)return{content:{status:"error",error:"No session ID available for continue action."}};let l=await s.get(f);if(!l)return{content:{status:"error",error:"Flow state not found. The flow may have expired."}};let w=l.state,y=l.step;if(!y)return{content:{status:"error",error:"Flow state is missing the current step. The flow may have expired."}};let u={...w,...d.stateUpdates??{}};if(l.widgetId){let T=r.get(y);if(!T)return{content:{status:"error",error:`No edge from step "${y}"`}};let x=await P(T,u);return D(x,u,n,r,i,h,p)}return D(y,u,n,r,i,h,p)}return{content:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:a,async register(d){d.registerTool(t.id,{title:t.title,description:a,inputSchema:ze,annotations:t.annotations},(async(f,h)=>{let p=h,l=p._meta??{},w=b(l),y=U(p),u=await c(f,w,l,y);return u.flowTokenContent&&w&&await s.set(w,u.flowTokenContent),{content:[{type:"text",text:JSON.stringify(u.content,null,2)}],_meta:l,...u.content.status==="error"?{isError:!0}:{}}}))}}}var F=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===_)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);return this.nodes.set(t,n),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(t){return this.validate(),ye({config:this.config,nodes:new Map(this.nodes),edges:new Map(this.edges),store:t?.store})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==_&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==_&&!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 Te(e){return new F(e)}function ke(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Se(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,a={registerTool:(...d)=>{r.push(d)}};await e.register(a);let s=r[0]?.[2];if(!s)throw new Error(`Flow "${e.id}" did not register a handler`);let i={_meta:{sessionId:o}};async function c(d){return{...d,decodedState:n?await n.get(o):null}}return{async start(d){let f=await s({action:"start",...d?{stateUpdates:d}:{}},i);return c(ke(f))},async continueWith(d){let f=await s({action:"continue",...d?{stateUpdates:d}:{}},i);return c(ke(f))},async lastState(){return n?n.get(o):null}}}var O="text/html+skybridge",B="text/html;profile=mcp-app",ve=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function xe(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 Y(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 Ee(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:s,prefersBorder:i=!0,autoHeight:c=!0}=e,d=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:u}=new URL(o);(u==="localhost"||u==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${u}:*`,`wss://${u}:*`],resource_domains:[...d.resource_domains||[],`http://${u}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,h=`ui://widgets/ext-apps/${t}.html`,p=null,l=()=>(p||(p=ve(o,a)),p),w=r;async function y(u){let T=await l();u.registerResource(`${t}-openai-widget`,f,{title:n,description:w,mimeType:O,_meta:{"openai/widgetDescription":w,"openai/widgetPrefersBorder":i}},async x=>({contents:[{uri:x.href,mimeType:O,text:T,_meta:xe({description:w,prefersBorder:i,widgetDomain:s,widgetCSP:d})}]})),u.registerResource(`${t}-mcp-widget`,h,{title:n,description:w,mimeType:B,_meta:{ui:{prefersBorder:i}}},async x=>({contents:[{uri:x.href,mimeType:B,text:T,_meta:Re({description:w,prefersBorder:i,widgetDomain:s,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:h,autoHeight:c,register:y}}function Ye(e,t){let{resource:n,description:r,inputSchema:o,annotations:a,autoInjectResultText:s=!0}=e,i=e.id??n?.id,c=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!c)throw new Error("createTool: `title` is required when no resource is provided");let d=n?Y({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:c,description:r,async register(f){f.registerTool(i,{title:c,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(h,p)=>{let l=p,w=l._meta??{},y=U(l),u=await t(h,{extra:{_meta:w},waniwani:y});return n&&u.data?{content:[{type:"text",text:u.text}],structuredContent:u.data,_meta:{...d,...w,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:u.text}],...u.data?{structuredContent:u.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Xe(e,t){await Promise.all(t.map(n=>n.register(e)))}var j=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Je="@waniwani/sdk";function Ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,s,i){let c=r(),d=`${t.replace(/\/$/,"")}${s}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Je},h={method:a,headers:f};i!==void 0&&(f["Content-Type"]="application/json",h.body=JSON.stringify(i));let p=await fetch(d,h);if(!p.ok){let w=await p.text().catch(()=>"");throw new j(w||`KB API error: HTTP ${p.status}`,p.status)}return(await p.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,s){return o("POST","/api/mcp/kb/search",{query:a,...s})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Ge="@waniwani/sdk";function $(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Ce,o=et(e),a=q(e.meta),s=q(e.metadata),i=tt(e,a),c=v(e.eventId)??r(),d=nt(e.timestamp,n),f=v(e.source)??t.source??Ge,h=X(e)?{...e}:void 0,p={...s};return Object.keys(a).length>0&&(p.meta=a),h&&(p.rawLegacy=h),{id:c,type:"mcp.event",name:o,source:f,timestamp:d,correlation:i,properties:Ze(e,o),metadata:p,rawLegacy:h}}function Ce(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ze(e,t){if(!X(e))return q(e.properties);let n=Qe(e,t),r=q(e.properties);return{...n,...r}}function Qe(e,t){switch(t){case"tool.called":{let n={};return v(e.toolName)&&(n.name=e.toolName),v(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),v(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return v(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),v(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function et(e){return X(e)?e.eventType:e.event}function tt(e,t){let n=v(e.requestId)??pe(t),r=v(e.sessionId)??b(t),o=v(e.traceId)??fe(t),a=v(e.externalUserId)??ge(t),s=v(e.correlationId)??me(t)??n,i={};return r&&(i.sessionId=r),o&&(i.traceId=o),n&&(i.requestId=n),s&&(i.correlationId=s),a&&(i.externalUserId=a),i}function nt(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 v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function X(e){return"eventType"in e}var rt="/api/mcp/events/v2/batch";var _e="@waniwani/sdk",ot=new Set([401,403]),it=new Set([408,425,429,500,502,503,504]);function be(e){return new J(e)}var J=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=ct(t.baseUrl,t.endpointPath??rt),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":_e},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:dt(a)}}if(ot.has(n.status))return{kind:"auth",status:n.status};if(it.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await at(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:_e,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(s=>[s.id,s])),o=[],a=[];for(let s of n){let i=r.get(s.eventId);if(i){if(st(s)){o.push(i);continue}a.push(i)}}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 st(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 at(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function ct(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function dt(e){return e instanceof Error?e.message:String(e)}function Me(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?be({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,s={async identify(i,c,d){o();let f=$({event:"user.identified",externalUserId:i,properties:c,meta:d});return a?.enqueue(f),{eventId:f.id}},async track(i){o();let c=$(i);return a?.enqueue(c),{eventId:c.id}},async flush(){o(),await a?.flush()},async shutdown(i){return o(),await a?.shutdown({timeoutMs:i?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&ut(s,r.shutdownTimeoutMs),s}function ut(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 Pe(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=Me(o),s=Ie(o);return{...a,kb:s,_config:o}}function lt(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 pt(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=Pe(t)),n}return async function(a){let s;try{s=await a.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 i=r(),c=[];for(let d of s.events){let f=lt(d),h=await i.track(f);c.push(h.eventId)}return await i.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(i){let c=i instanceof Error?i.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var L=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=ft(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 s=await a.json(),i=s.data&&typeof s.data=="object"?s.data:s,c=new Date(i.expiresAt).getTime();return!i.token||Number.isNaN(c)?null:(this.cached={token:i.token,expiresAt:c},i.token)}catch{return null}}};function ft(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Fe="waniwani/sessionId",G="waniwani/geoLocation",Z="waniwani/userLocation";function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function K(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function Q(e){if(!S(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>S(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function gt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ee(e,t,n,r,o){let a=gt(e,n.toolType),s=K(t);return{event:"tool.called",properties:{name:e,type:a,...r??{}},meta:s,metadata:{source:"withWaniwani",...n.metadata??{},...o&&{clientInfo:o}}}}async function te(e,t,n){try{await e.track(t)}catch(r){n?.(re(r))}}async function ne(e,t){try{await e.flush()}catch(n){t?.(re(n))}}async function We(e,t,n,r){if(!S(e))return;S(e._meta)||(e._meta={});let o=e._meta,a=S(o.waniwani)?o.waniwani:void 0,s={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let d=await t.getToken();d&&(s.token=d)}catch(d){r?.(re(d))}let i=b(o);i&&(s.sessionId||(s.sessionId=i));let c=Ue(o);c!==void 0&&(s.geoLocation||(s.geoLocation=c)),o.waniwani=s}function Ae(e,t){let n=K(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let r=e._meta,o=b(n);o&&!r[Fe]&&(r[Fe]=o);let a=Ue(n);a&&(r[G]||(r[G]=a),r[Z]||(r[Z]=a))}function Ue(e){if(!e)return;let t=e[G]??e[Z];if(S(t)||typeof t=="string")return t}function re(e){return e instanceof Error?e:new Error(String(e))}var De="https://app.waniwani.ai";function mt(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client,o=t.injectWidgetToken!==!1,a=null;function s(){if(a)return a;let c=r._config.apiKey;return c?(a=new L({baseUrl:r._config.baseUrl??De,apiKey:c}),a):null}let i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,f,h]=c,p=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof h!="function")return i(...c);let l=h;return i(d,f,async(y,u)=>{let T=K(u)??{},x=le(r,T);S(u)&&(u[H]=x);let C=performance.now(),R=e.server?.getClientVersion?.();try{let k=await l(y,u),M=Math.round(performance.now()-C),E=S(k)&&k.isError===!0;if(E){let oe=Q(k);console.error(`[waniwani] Tool "${p}" returned error${oe?`: ${oe}`:""}`)}return await te(r,ee(p,u,t,{durationMs:M,status:E?"error":"ok",...E&&{errorMessage:Q(k)??"Unknown tool error"}},R),t.onError),t.flushAfterToolCall&&await ne(r,t.onError),Ae(k,u),o&&await We(k,s(),r._config.baseUrl??De,t.onError),k}catch(k){let M=Math.round(performance.now()-C);throw await te(r,ee(p,u,t,{durationMs:M,status:"error",errorMessage:k instanceof Error?k.message:String(k)},R),t.onError),t.flushAfterToolCall&&await ne(r,t.onError),k}})}),n}export{_ as END,I as START,F as StateGraph,Te as createFlow,Se as createFlowTestHarness,Ee as createResource,Ye as createTool,pt as createTrackingRoute,V as detectPlatform,Oe as isMCPApps,Ne as isOpenAI,Xe as registerTools,mt as withWaniwani};
|
|
6
6
|
//# sourceMappingURL=index.js.map
|