@waniwani/sdk 0.4.9-beta.8 → 0.5.0
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.d.ts +6 -2
- package/dist/chat/index.js +6 -6
- package/dist/chat/index.js.map +1 -1
- package/dist/chat/next-js/index.d.ts +1 -1
- package/dist/chat/next-js/index.js +3 -3
- package/dist/chat/next-js/index.js.map +1 -1
- package/dist/chat/server/index.d.ts +1 -1
- package/dist/chat/styles.css +1 -1
- package/dist/evals/index.d.ts +47 -6
- package/dist/evals/index.js +2 -2
- package/dist/evals/index.js.map +1 -1
- package/dist/index.d.ts +72 -5
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/kb/index.d.ts +1 -1
- package/dist/kb/index.js +1 -1
- package/dist/kb/index.js.map +1 -1
- package/dist/mcp/index.d.ts +11 -10
- package/dist/mcp/index.js +5 -5
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react.d.ts +2 -2
- package/dist/mcp/react.js +1 -1
- package/dist/mcp/react.js.map +1 -1
- package/package.json +1 -1
package/dist/kb/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/error.ts","../../src/kb/client.ts"],"sourcesContent":["// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// KB Client — thin HTTP wrapper for knowledge base API\n\nimport { WaniWaniError } from \"../error.js\";\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tKbClient,\n\tKbIngestFile,\n\tKbIngestResult,\n\tKbSearchOptions,\n\tKbSource,\n\tSearchResult,\n} from \"./types.js\";\n\nconst SDK_NAME = \"@waniwani/sdk\";\n\nexport function createKbClient(config: InternalConfig): KbClient {\n\tconst {
|
|
1
|
+
{"version":3,"sources":["../../src/error.ts","../../src/kb/client.ts"],"sourcesContent":["// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// KB Client — thin HTTP wrapper for knowledge base API\n\nimport { WaniWaniError } from \"../error.js\";\nimport type { InternalConfig } from \"../types.js\";\nimport type {\n\tKbClient,\n\tKbIngestFile,\n\tKbIngestResult,\n\tKbSearchOptions,\n\tKbSource,\n\tSearchResult,\n} from \"./types.js\";\n\nconst SDK_NAME = \"@waniwani/sdk\";\n\nexport function createKbClient(config: InternalConfig): KbClient {\n\tconst { apiUrl, apiKey } = config;\n\n\tfunction requireApiKey(): string {\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(\"WANIWANI_API_KEY is not set\");\n\t\t}\n\t\treturn apiKey;\n\t}\n\n\tasync function request<T>(\n\t\tmethod: \"GET\" | \"POST\",\n\t\tpath: string,\n\t\tbody?: unknown,\n\t): Promise<T> {\n\t\tconst key = requireApiKey();\n\t\tconst url = `${apiUrl.replace(/\\/$/, \"\")}${path}`;\n\n\t\tconst headers: Record<string, string> = {\n\t\t\tAuthorization: `Bearer ${key}`,\n\t\t\t\"X-WaniWani-SDK\": SDK_NAME,\n\t\t};\n\n\t\tconst init: RequestInit = { method, headers };\n\n\t\tif (body !== undefined) {\n\t\t\theaders[\"Content-Type\"] = \"application/json\";\n\t\t\tinit.body = JSON.stringify(body);\n\t\t}\n\n\t\tconst response = await fetch(url, init);\n\n\t\tif (!response.ok) {\n\t\t\tconst text = await response.text().catch(() => \"\");\n\t\t\tthrow new WaniWaniError(\n\t\t\t\ttext || `KB API error: HTTP ${response.status}`,\n\t\t\t\tresponse.status,\n\t\t\t);\n\t\t}\n\n\t\tconst json = (await response.json()) as { data: T };\n\t\treturn json.data;\n\t}\n\n\treturn {\n\t\tasync ingest(files: KbIngestFile[]): Promise<KbIngestResult> {\n\t\t\treturn request<KbIngestResult>(\"POST\", \"/api/mcp/kb/ingest\", {\n\t\t\t\tfiles,\n\t\t\t});\n\t\t},\n\n\t\tasync search(\n\t\t\tquery: string,\n\t\t\toptions?: KbSearchOptions,\n\t\t): Promise<SearchResult[]> {\n\t\t\treturn request<SearchResult[]>(\"POST\", \"/api/mcp/kb/search\", {\n\t\t\t\tquery,\n\t\t\t\t...options,\n\t\t\t});\n\t\t},\n\n\t\tasync sources(): Promise<KbSource[]> {\n\t\t\treturn request<KbSource[]>(\"GET\", \"/api/mcp/kb/sources\");\n\t\t},\n\t};\n}\n"],"mappings":"AAEO,IAAMA,EAAN,cAA4B,KAAM,CACxC,YACCC,EACOC,EACN,CACD,MAAMD,CAAO,EAFN,YAAAC,EAGP,KAAK,KAAO,eACb,CACD,ECGA,IAAMC,EAAW,gBAEV,SAASC,EAAeC,EAAkC,CAChE,GAAM,CAAE,OAAAC,EAAQ,OAAAC,CAAO,EAAIF,EAE3B,SAASG,GAAwB,CAChC,GAAI,CAACD,EACJ,MAAM,IAAI,MAAM,6BAA6B,EAE9C,OAAOA,CACR,CAEA,eAAeE,EACdC,EACAC,EACAC,EACa,CACb,IAAMC,EAAML,EAAc,EACpBM,EAAM,GAAGR,EAAO,QAAQ,MAAO,EAAE,CAAC,GAAGK,CAAI,GAEzCI,EAAkC,CACvC,cAAe,UAAUF,CAAG,GAC5B,iBAAkBV,CACnB,EAEMa,EAAoB,CAAE,OAAAN,EAAQ,QAAAK,CAAQ,EAExCH,IAAS,SACZG,EAAQ,cAAc,EAAI,mBAC1BC,EAAK,KAAO,KAAK,UAAUJ,CAAI,GAGhC,IAAMK,EAAW,MAAM,MAAMH,EAAKE,CAAI,EAEtC,GAAI,CAACC,EAAS,GAAI,CACjB,IAAMC,EAAO,MAAMD,EAAS,KAAK,EAAE,MAAM,IAAM,EAAE,EACjD,MAAM,IAAIE,EACTD,GAAQ,sBAAsBD,EAAS,MAAM,GAC7CA,EAAS,MACV,CACD,CAGA,OADc,MAAMA,EAAS,KAAK,GACtB,IACb,CAEA,MAAO,CACN,MAAM,OAAOG,EAAgD,CAC5D,OAAOX,EAAwB,OAAQ,qBAAsB,CAC5D,MAAAW,CACD,CAAC,CACF,EAEA,MAAM,OACLC,EACAC,EAC0B,CAC1B,OAAOb,EAAwB,OAAQ,qBAAsB,CAC5D,MAAAY,EACA,GAAGC,CACJ,CAAC,CACF,EAEA,MAAM,SAA+B,CACpC,OAAOb,EAAoB,MAAO,qBAAqB,CACxD,CACD,CACD","names":["WaniWaniError","message","status","SDK_NAME","createKbClient","config","apiUrl","apiKey","requireApiKey","request","method","path","body","key","url","headers","init","response","text","WaniWaniError","files","query","options"]}
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -288,8 +288,8 @@ type ResourceConfig = {
|
|
|
288
288
|
/** UI description (describes WHAT the resource displays) */
|
|
289
289
|
description?: string;
|
|
290
290
|
/** Base URL for fetching the HTML */
|
|
291
|
-
|
|
292
|
-
/** Path to the HTML file (relative to
|
|
291
|
+
apiUrl: string;
|
|
292
|
+
/** Path to the HTML file (relative to apiUrl) */
|
|
293
293
|
htmlPath: string;
|
|
294
294
|
/** Domain for OpenAI security context */
|
|
295
295
|
widgetDomain: string;
|
|
@@ -977,7 +977,7 @@ declare function createFlowTestHarness(flow: RegisteredFlow, options?: {
|
|
|
977
977
|
* const pricingUI = createResource({
|
|
978
978
|
* id: "pricing_table",
|
|
979
979
|
* title: "Pricing Table",
|
|
980
|
-
*
|
|
980
|
+
* apiUrl: "https://my-app.com",
|
|
981
981
|
* htmlPath: "/widgets/pricing",
|
|
982
982
|
* widgetDomain: "my-app.com",
|
|
983
983
|
* });
|
|
@@ -1035,7 +1035,7 @@ declare function registerTools(server: McpServer, tools: RegisteredTool[]): Prom
|
|
|
1035
1035
|
*
|
|
1036
1036
|
* const handler = createTrackingRoute({
|
|
1037
1037
|
* apiKey: process.env.WANIWANI_API_KEY,
|
|
1038
|
-
*
|
|
1038
|
+
* apiUrl: process.env.WANIWANI_BASE_URL,
|
|
1039
1039
|
* });
|
|
1040
1040
|
*
|
|
1041
1041
|
* export { handler as POST };
|
|
@@ -1045,7 +1045,7 @@ interface TrackingRouteOptions {
|
|
|
1045
1045
|
/** API key for the WaniWani backend. Defaults to WANIWANI_API_KEY env var. */
|
|
1046
1046
|
apiKey?: string;
|
|
1047
1047
|
/** Base URL for the WaniWani backend. Defaults to https://app.waniwani.ai. */
|
|
1048
|
-
|
|
1048
|
+
apiUrl?: string;
|
|
1049
1049
|
}
|
|
1050
1050
|
/**
|
|
1051
1051
|
* Creates a POST handler that receives tracking events from `useWaniwani`
|
|
@@ -1069,7 +1069,7 @@ interface WaniWaniClient extends TrackingClient {
|
|
|
1069
1069
|
readonly kb: KbClient;
|
|
1070
1070
|
}
|
|
1071
1071
|
interface InternalConfig {
|
|
1072
|
-
|
|
1072
|
+
apiUrl: string;
|
|
1073
1073
|
apiKey: string | undefined;
|
|
1074
1074
|
tracking: Required<TrackingConfig>;
|
|
1075
1075
|
}
|
|
@@ -1082,10 +1082,11 @@ type UnknownRecord = Record<string, unknown>;
|
|
|
1082
1082
|
*/
|
|
1083
1083
|
type WithWaniwaniOptions = {
|
|
1084
1084
|
/**
|
|
1085
|
-
* The WaniWani client instance.
|
|
1086
|
-
*
|
|
1085
|
+
* The WaniWani client instance. When omitted, a client is created
|
|
1086
|
+
* automatically using the global config registered by `defineConfig()`,
|
|
1087
|
+
* falling back to env vars.
|
|
1087
1088
|
*/
|
|
1088
|
-
client
|
|
1089
|
+
client?: WaniwaniTracker;
|
|
1089
1090
|
/**
|
|
1090
1091
|
* Optional explicit tool type. Defaults to `"other"`.
|
|
1091
1092
|
*/
|
|
@@ -1123,6 +1124,6 @@ type WithWaniwaniOptions = {
|
|
|
1123
1124
|
* into tool response `_meta.waniwani` so browser widgets can post events
|
|
1124
1125
|
* directly to the WaniWani backend without a server-side proxy.
|
|
1125
1126
|
*/
|
|
1126
|
-
declare function withWaniwani(server: McpServer, options
|
|
1127
|
+
declare function withWaniwani(server: McpServer, options?: WithWaniwaniOptions): McpServer;
|
|
1127
1128
|
|
|
1128
1129
|
export { type ConditionFn, END, type FlowConfig, type FlowTestResult, type HostContext, type InferFlowState, type InterruptSignal, type NodeContext, type NodeHandler, type RegisteredFlow, type RegisteredResource, type RegisteredTool, type ResourceConfig, START, type ScopedWaniWaniClient, StateGraph, type ToolCallResult, type ToolConfig, type ToolHandler, type ToolHandlerContext, type ToolResult, type ToolToolCallback, type TrackingRouteOptions, type TypedInterrupt, type TypedShowWidget, type UnifiedWidgetClient, type WidgetCSP, type WidgetPlatform, type WidgetSignal, type WithWaniwaniOptions, createFlow, createFlowTestHarness, createResource, createTool, createTrackingRoute, detectPlatform, isMCPApps, isOpenAI, registerTools, withWaniwani };
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function
|
|
1
|
+
function J(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function He(){return J()==="openai"}function Ye(){return J()==="mcp-apps"}var R="__start__",C="__end__",pe=Symbol.for("waniwani.flow.interrupt"),le=Symbol.for("waniwani.flow.widget");function fe(e,t){let n=t?.context,r=[];for(let[o,s]of Object.entries(e))if(typeof s=="object"&&s!==null&&"question"in s){let a=s;r.push({question:a.question,field:o,suggestions:a.suggestions,context:a.context,validate:a.validate})}return{__type:pe,questions:r,context:n}}function ge(e,t){return{__type:le,tool:e,...t}}function me(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===pe}function we(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===le}import{z as $}from"zod";var X="waniwani/client";function O(e){if(typeof e=="object"&&e!==null)return e[X]}function he(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 A(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var Ge=["waniwani/sessionId","openai/sessionId","sessionId","conversationId","anthropic/sessionId"],Je=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],Xe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],Ze=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],Qe=["correlationId","openai/requestId"];function _(e){return e?A(e,Ge):void 0}function ye(e){return e?A(e,Je):void 0}function Te(e){return e?A(e,Xe):void 0}function ke(e){return e?A(e,Ze):void 0}function Se(e){return e?A(e,Qe):void 0}var et=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function P(e){if(!e)return;let t=e["waniwani/source"];if(typeof t=="string"&&t.length>0)return t;for(let{key:n,source:r}of et){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function ve(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function N(e,t){let n=t.split("."),r=e;for(let o of n){if(r==null||typeof r!="object")return;r=r[o]}return r}function tt(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let s=e;for(let a of r)(s[a]==null||typeof s[a]!="object"||Array.isArray(s[a]))&&(s[a]={}),s=s[a];s[o]=n}function xe(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let s of n){if(o==null||typeof o!="object")return;o=o[s]}o!=null&&typeof o=="object"&&delete o[r]}function Z(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?tt(t,n,r):t[n]=r;return t}function M(e,t){let n={...e};for(let[r,o]of Object.entries(t))o!==null&&typeof o=="object"&&!Array.isArray(o)&&n[r]!==null&&typeof n[r]=="object"&&!Array.isArray(n[r])?n[r]=M(n[r],o):n[r]=o;return n}function Q(e){return e!=null&&e!==""}async function W(e,t){return e.type==="direct"?e.to:e.condition(t)}function Re(e,t,n,r){if(e.every(c=>Q(N(r,c.field))))return null;let o=e.filter(c=>!Q(N(r,c.field))),s=o.length===1,a=o[0];return{content:s&&a?{status:"interrupt",question:a.question,field:a.field,...a.suggestions?{suggestions:a.suggestions}:{},...a.context||t?{context:a.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&a?{field:a.field}:{}}}}async function K(e,t,n,r,o,s,a){let i=e,c={...t},d=50,p=0;for(;p++<d;){if(i===C)return{content:{status:"complete"},flowTokenContent:{state:c}};let g=n.get(i);if(!g)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let u=await g({state:c,meta:s,interrupt:fe,showWidget:ge,waniwani:a});if(me(u)){for(let y of u.questions)y.validate&&o.set(`${i}:${y.field}`,y.validate);let T=Re(u.questions,u.context,i,c);if(T)return T;for(let y of u.questions){let x=o.get(`${i}:${y.field}`);if(x)try{let b=N(c,y.field),E=await x(b);E&&typeof E=="object"&&(c=M(c,E))}catch(b){let E=b instanceof Error?b.message:String(b);xe(c,y.field);let j=u.questions.map(I=>I.field===y.field?{...I,context:I.context?`ERROR: ${E}
|
|
2
2
|
|
|
3
|
-
${I.context}`:`ERROR: ${E}`}:I),
|
|
4
|
-
`)}var
|
|
5
|
-
${o}`,a=e.store??new
|
|
6
|
-
`)}var A=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===R||t===C)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}graph(){return Ie(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Ee({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>Ie(n,r)})}validate(){if(!this.edges.has(R))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(R);if(t?.type==="direct"&&t.to!==C&&!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!==R&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==C&&!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 A(e)}function be(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function _e(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,s={registerTool:(...d)=>{r.push(d)}};await e.register(s);let a=r[0]?.[2];if(!a)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 l=await a({action:"start",...d?{stateUpdates:d}:{}},i);return c(be(l))},async continueWith(d){let l=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(be(l))},async lastState(){return n?n.get(o):null}}}var B="text/html+skybridge",q="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 Me(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Fe(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 Z(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 We(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:a,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 l=`ui://widgets/apps-sdk/${t}.html`,g=`ui://widgets/ext-apps/${t}.html`,f=null,p=()=>(f||(f=Pe(o,s)),f),w=r;async function y(u){let T=await p();u.registerResource(`${t}-openai-widget`,l,{title:n,description:w,mimeType:B,_meta:{"openai/widgetDescription":w,"openai/widgetPrefersBorder":i}},async x=>({contents:[{uri:x.href,mimeType:B,text:T,_meta:Me({description:w,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),u.registerResource(`${t}-mcp-widget`,g,{title:n,description:w,mimeType:q,_meta:{ui:{prefersBorder:i}}},async x=>({contents:[{uri:x.href,mimeType:q,text:T,_meta:Fe({description:w,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:l,mcpUri:g,autoHeight:c,register:y}}function rt(e,t){let{resource:n,description:r,inputSchema:o,annotations:s,autoInjectResultText:a=!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?Z({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(l){l.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(g,f)=>{let p=f,w=p._meta??{},y=j(p),u=await t(g,{extra:{_meta:w},waniwani:y});return n&&u.data?{content:[{type:"text",text:u.text}],structuredContent:u.data,_meta:{...d,...w,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:u.text}],...u.data?{structuredContent:u.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ot(e,t){await Promise.all(t.map(n=>n.register(e)))}var L=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var it="@waniwani/sdk";function Ae(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,a,i){let c=r(),d=`${t.replace(/\/$/,"")}${a}`,l={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":it},g={method:s,headers:l};i!==void 0&&(l["Content-Type"]="application/json",g.body=JSON.stringify(i));let f=await fetch(d,g);if(!f.ok){let w=await f.text().catch(()=>"");throw new L(w||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){return o("POST","/api/mcp/kb/search",{query:s,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var st="@waniwani/sdk";function z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Ne,o=dt(e),s=V(e.meta),a=V(e.metadata),i=ut(e,s),c=v(e.eventId)??r(),d=pt(e.timestamp,n),l=v(e.source)??M(s)??t.source??st,g=Q(e)?{...e}:void 0,f={...a};return Object.keys(s).length>0&&(f.meta=s),g&&(f.rawLegacy=g),{id:c,type:"mcp.event",name:o,source:l,timestamp:d,correlation:i,properties:at(e,o),metadata:f,rawLegacy:g}}function Ne(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function at(e,t){if(!Q(e))return V(e.properties);let n=ct(e,t),r=V(e.properties);return{...n,...r}}function ct(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 dt(e){return Q(e)?e.eventType:e.event}function ut(e,t){let n=v(e.requestId)??he(t),r=v(e.sessionId)??_(t),o=v(e.traceId)??we(t),s=v(e.externalUserId)??ye(t),a=v(e.correlationId)??Te(t)??n,i={};return r&&(i.sessionId=r),o&&(i.traceId=o),n&&(i.requestId=n),a&&(i.correlationId=a),s&&(i.externalUserId=s),i}function pt(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 V(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function Q(e){return"eventType"in e}var lt="/api/mcp/events/v2/batch";var De="@waniwani/sdk",ft=new Set([401,403]),gt=new Set([408,425,429,500,502,503,504]);function Ue(e){return new ee(e)}var ee=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=wt(t.baseUrl,t.endpointPath??lt),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(s){return{kind:"retryable",reason:yt(s)}}if(ft.has(n.status))return{kind:"auth",status:n.status};if(gt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await ht(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(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(mt(a)){o.push(i);continue}s.push(i)}}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 mt(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 ht(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function wt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function yt(e){return e instanceof Error?e.message:String(e)}function Oe(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?Ue({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,a={async identify(i,c,d){o();let l=z({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(l),{eventId:l.id}},async track(i){o();let c=z(i);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(i){return o(),await s?.shutdown({timeoutMs:i?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&Tt(a,r.shutdownTimeoutMs),a}function Tt(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 je(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=Oe(o),a=Ae(o);return{...s,kb:a,_config:o}}function kt(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 St(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=je(t)),n}return async function(s){let a;try{a=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.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 a.events){let l=kt(d),g=await i.track(l);c.push(g.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 H=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=vt(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 a=await s.json(),i=a.data&&typeof a.data=="object"?a.data:a,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 vt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Ke="waniwani/sessionId",te="waniwani/geoLocation",ne="waniwani/userLocation";function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function O(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function re(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 xt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function oe(e,t,n,r,o,s){let a=xt(e,n.toolType),i=O(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",M(i)),{event:"tool.called",properties:{name:e,type:a,...r??{},...s?.input!==void 0&&{input:s.input},...s?.output!==void 0&&{output:s.output}},meta:i,source:M(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ie(e,t,n){try{await e.track(t)}catch(r){n?.(ae(r))}}async function se(e,t){try{await e.flush()}catch(n){t?.(ae(n))}}async function $e(e,t,n,r,o){if(!S(e))return;S(e._meta)||(e._meta={});let s=e._meta,a=S(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let g=await t.getToken();g&&(i.token=g)}catch(g){o?.(ae(g))}let c=_(s);c&&(i.sessionId||(i.sessionId=c));let d=qe(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let l=M(O(r));l&&!i.source&&(i.source=l),s.waniwani=i}function Be(e,t){let n=O(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let r=e._meta,o=_(n);o&&!r[Ke]&&(r[Ke]=o);let s=qe(n);s&&(r[te]||(r[te]=s),r[ne]||(r[ne]=s))}function qe(e){if(!e)return;let t=e[te]??e[ne];if(S(t)||typeof t=="string")return t}function ae(e){return e instanceof Error?e:new Error(String(e))}var Le="https://app.waniwani.ai";function Rt(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client,o=t.injectWidgetToken!==!1,s=null;function a(){if(s)return s;let c=r._config.apiKey;return c?(s=new H({baseUrl:r._config.baseUrl??Le,apiKey:c}),s):null}let i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,l,g]=c,f=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof g!="function")return i(...c);let p=g;return i(d,l,async(y,u)=>{let T=O(u)??{},x=me(r,T);S(u)&&(u[J]=x);let b=performance.now(),E=e.server?.getClientVersion?.();try{let k=await p(y,u),P=Math.round(performance.now()-b),I=S(k)&&k.isError===!0;if(I){let ce=re(k);console.error(`[waniwani] Tool "${f}" returned error${ce?`: ${ce}`:""}`)}return await ie(r,oe(f,u,t,{durationMs:P,status:I?"error":"ok",...I&&{errorMessage:re(k)??"Unknown tool error"}},E,{input:y,output:k}),t.onError),t.flushAfterToolCall&&await se(r,t.onError),Be(k,u),o&&await $e(k,a(),r._config.baseUrl??Le,u,t.onError),k}catch(k){let P=Math.round(performance.now()-b);throw await ie(r,oe(f,u,t,{durationMs:P,status:"error",errorMessage:k instanceof Error?k.message:String(k)},E,{input:y}),t.onError),t.flushAfterToolCall&&await se(r,t.onError),k}})}),n}export{C as END,R as START,A as StateGraph,Ce as createFlow,_e as createFlowTestHarness,We as createResource,rt as createTool,St as createTrackingRoute,Y as detectPlatform,ze as isMCPApps,Ve as isOpenAI,ot as registerTools,Rt as withWaniwani};
|
|
3
|
+
${I.context}`:`ERROR: ${E}`}:I),k=Re(j,u.context,i,c);if(k)return k;break}}let l=r.get(i);if(!l)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await W(l,c);continue}if(we(u)){let T=u.field;if(T&&Q(N(c,T))){let l=r.get(i);if(!l)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await W(l,c);continue}return{content:{status:"widget",tool:u.tool.id,data:u.data,description:u.description,interactive:u.interactive!==!1},flowTokenContent:{step:i,state:c,field:T,widgetId:u.tool.id}}}c=M(c,u);let h=r.get(i);if(!h)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await W(h,c)}catch(f){return{content:{status:"error",error:f instanceof Error?f.message:String(f)},flowTokenContent:{step:i,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var nt="@waniwani/sdk",rt="https://app.waniwani.ai",D=class{apiUrl;apiKey;constructor(t){this.apiUrl=(t?.apiUrl??process.env.WANIWANI_BASE_URL??rt).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.apiUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":nt},body:JSON.stringify(n)});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(a||`Flow state API error: HTTP ${o.status}`)}return(await o.json()).data}};function Ee(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 Ie(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=[];for(let[r,o]of Object.entries(e.state)){let s=ve(o);if(s){let a=o.description??"",i=Object.entries(s).map(([c,d])=>{let p=Ee(d);return p?`\`${r}.${c}\` (${p})`:`\`${r}.${c}\``}).join(", ");n.push(a?`\`${r}\` (${a}): ${i}`:`\`${r}\`: ${i}`)}else{let a=Ee(o);n.push(a?`\`${r}\` (${a})`:`\`${r}\``)}}t.push(` Known fields: ${n.join(", ")}.`)}return t.push(" For grouped fields (shown as `group.subfield`), use dot-notation keys in `stateUpdates`:",' e.g. `{ "driver.name": "John", "driver.license": "ABC123" }`.',"2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, 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 ot={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.")};function Ce(e){let{config:t,nodes:n,edges:r}=e,o=Ie(t),s=`${t.description}
|
|
5
|
+
${o}`,a=e.store??new D,i=new Map;async function c(d,p,g,f){if(d.action==="start"){let u=r.get(R);if(!u)return{content:{status:"error",error:"No start edge"}};let h=Z(d.stateUpdates??{}),T=await W(u,h);return K(T,h,n,r,i,g,f)}if(d.action==="continue"){if(!p)return{content:{status:"error",error:"No session ID available for continue action."}};let u=await a.get(p);if(!u)return{content:{status:"error",error:"Flow state not found. The flow may have expired."}};let h=u.state,T=u.step;if(!T)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let l=M(h,Z(d.stateUpdates??{}));if(u.widgetId){let y=r.get(T);if(!y)return{content:{status:"error",error:`No edge from step "${T}"`}};let x=await W(y,l);return K(x,l,n,r,i,g,f)}return K(T,l,n,r,i,g,f)}return{content:{status:"error",error:`Unknown action: "${d.action}"`}}}return{id:t.id,title:t.title,description:s,graph:e.graph,async register(d){d.registerTool(t.id,{title:t.title,description:s,inputSchema:ot,annotations:t.annotations},(async(p,g)=>{let f=g,u=f._meta??{},h=_(u),T=O(f),l=await c(p,h,u,T);return h&&(l.flowTokenContent&&l.content.status!=="complete"?await a.set(h,l.flowTokenContent):l.content.status==="complete"&&await a.delete(h)),{content:[{type:"text",text:JSON.stringify(l.content,null,2)}],_meta:u,...l.content.status==="error"?{isError:!0}:{}}}))}}}function be(e,t){let n=["flowchart TD"];n.push(` ${R}((Start))`);for(let[r]of e)n.push(` ${r}[${r}]`);n.push(` ${C}((End))`);for(let[r,o]of t)o.type==="direct"?n.push(` ${r} --> ${o.to}`):n.push(` ${r} -.-> ${r}_branch([?])`);return n.join(`
|
|
6
|
+
`)}var F=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===R||t===C)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}graph(){return be(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Ce({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>be(n,r)})}validate(){if(!this.edges.has(R))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(R);if(t?.type==="direct"&&t.to!==C&&!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!==R&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==C&&!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 _e(e){return new F(e)}function Pe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Me(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,s={registerTool:(...d)=>{r.push(d)}};await e.register(s);let a=r[0]?.[2];if(!a)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 p=await a({action:"start",...d?{stateUpdates:d}:{}},i);return c(Pe(p))},async continueWith(d){let p=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(Pe(p))},async lastState(){return n?n.get(o):null}}}var B="text/html+skybridge",q="text/html;profile=mcp-app",We=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 Ae(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function ee(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}},...e.mcpTemplateUri&&{"ui/resourceUri":e.mcpTemplateUri}}}function Ne(e){let{id:t,title:n,description:r,apiUrl:o,htmlPath:s,widgetDomain:a,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:l}=new URL(o);(l==="localhost"||l==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${l}:*`,`wss://${l}:*`],resource_domains:[...d.resource_domains||[],`http://${l}:*`]})}catch{}let p=`ui://widgets/apps-sdk/${t}.html`,g=`ui://widgets/ext-apps/${t}.html`,f=null,u=()=>(f||(f=We(o,s)),f),h=r;async function T(l){let y=await u();l.registerResource(`${t}-openai-widget`,p,{title:n,description:h,mimeType:B,_meta:{"openai/widgetDescription":h,"openai/widgetPrefersBorder":i}},async x=>({contents:[{uri:x.href,mimeType:B,text:y,_meta:Fe({description:h,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),l.registerResource(`${t}-mcp-widget`,g,{title:n,description:h,mimeType:q,_meta:{ui:{prefersBorder:i}}},async x=>({contents:[{uri:x.href,mimeType:q,text:y,_meta:Ae({description:h,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:p,mcpUri:g,autoHeight:c,register:T}}function it(e,t){let{resource:n,description:r,inputSchema:o,annotations:s,autoInjectResultText:a=!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?ee({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(p){p.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(g,f)=>{let u=f,h=u._meta??{},T=O(u),l=await t(g,{extra:{_meta:h},waniwani:T});return n&&l.data?{content:[{type:"text",text:l.text}],structuredContent:l.data,_meta:{...d,...h,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:l.text}],...l.data?{structuredContent:l.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function st(e,t){await Promise.all(t.map(n=>n.register(e)))}var L=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var at="@waniwani/sdk";function De(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,a,i){let c=r(),d=`${t.replace(/\/$/,"")}${a}`,p={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":at},g={method:s,headers:p};i!==void 0&&(p["Content-Type"]="application/json",g.body=JSON.stringify(i));let f=await fetch(d,g);if(!f.ok){let h=await f.text().catch(()=>"");throw new L(h||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){return o("POST","/api/mcp/kb/search",{query:s,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var ct;function Ue(){return ct}var dt="@waniwani/sdk";function z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??je,o=lt(e),s=V(e.meta),a=V(e.metadata),i=ft(e,s),c=v(e.eventId)??r(),d=gt(e.timestamp,n),p=v(e.source)??P(s)??t.source??dt,g=te(e)?{...e}:void 0,f={...a};return Object.keys(s).length>0&&(f.meta=s),g&&(f.rawLegacy=g),{id:c,type:"mcp.event",name:o,source:p,timestamp:d,correlation:i,properties:ut(e,o),metadata:f,rawLegacy:g}}function je(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function ut(e,t){if(!te(e))return V(e.properties);let n=pt(e,t),r=V(e.properties);return{...n,...r}}function pt(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 lt(e){return te(e)?e.eventType:e.event}function ft(e,t){let n=v(e.requestId)??ye(t),r=v(e.sessionId)??_(t),o=v(e.traceId)??Te(t),s=v(e.externalUserId)??ke(t),a=v(e.correlationId)??Se(t)??n,i={};return r&&(i.sessionId=r),o&&(i.traceId=o),n&&(i.requestId=n),a&&(i.correlationId=a),s&&(i.externalUserId=s),i}function gt(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 V(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function v(e){if(typeof e=="string"&&e.trim().length!==0)return e}function te(e){return"eventType"in e}var mt="/api/mcp/events/v2/batch";var Oe="@waniwani/sdk",wt=new Set([401,403]),ht=new Set([408,425,429,500,502,503,504]);function Ke(e){return new ne(e)}var ne=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=kt(t.apiUrl,t.endpointPath??mt),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":Oe},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:St(s)}}if(wt.has(n.status))return{kind:"auth",status:n.status};if(ht.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Tt(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:Oe,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(yt(a)){o.push(i);continue}s.push(i)}}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 yt(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 Tt(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function kt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function St(e){return e instanceof Error?e.message:String(e)}function $e(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?Ke({apiUrl: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,a={async identify(i,c,d){o();let p=z({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(p),{eventId:p.id}},async track(i){o();let c=z(i);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(i){return o(),await s?.shutdown({timeoutMs:i?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&vt(a,r.shutdownTimeoutMs),a}function vt(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 H(e){let t=e??Ue(),n=t?.apiUrl??"https://app.waniwani.ai",r=t?.apiKey??process.env.WANIWANI_API_KEY,o={endpointPath:t?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:t?.tracking?.flushIntervalMs??1e3,maxBatchSize:t?.tracking?.maxBatchSize??20,maxBufferSize:t?.tracking?.maxBufferSize??1e3,maxRetries:t?.tracking?.maxRetries??3,retryBaseDelayMs:t?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:t?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:t?.tracking?.shutdownTimeoutMs??2e3},s={apiUrl:n,apiKey:r,tracking:o},a=$e(s),i=De(s);return{...a,kb:i,_config:s}}function xt(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 Rt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=H(t)),n}return async function(s){let a;try{a=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(a.events)||a.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 a.events){let p=xt(d),g=await i.track(p);c.push(g.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 Y=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=Et(this.config.apiUrl,"/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 a=await s.json(),i=a.data&&typeof a.data=="object"?a.data:a,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 Et(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Be="waniwani/sessionId",re="waniwani/geoLocation",oe="waniwani/userLocation";function S(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function U(e){if(!S(e))return;let t=e._meta;return S(t)?t:void 0}function ie(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 It(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function se(e,t,n,r,o,s){let a=It(e,n.toolType),i=U(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",P(i)),{event:"tool.called",properties:{name:e,type:a,...r??{},...s?.input!==void 0&&{input:s.input},...s?.output!==void 0&&{output:s.output}},meta:i,source:P(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ae(e,t,n){try{await e.track(t)}catch(r){n?.(de(r))}}async function ce(e,t){try{await e.flush()}catch(n){t?.(de(n))}}async function qe(e,t,n,r,o){if(!S(e))return;S(e._meta)||(e._meta={});let s=e._meta,a=S(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let g=await t.getToken();g&&(i.token=g)}catch(g){o?.(de(g))}let c=_(s);c&&(i.sessionId||(i.sessionId=c));let d=Ve(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let p=P(U(r));p&&!i.source&&(i.source=p),s.waniwani=i}function Le(e,t){let n=U(t);if(!n||!S(e))return;S(e._meta)||(e._meta={});let r=e._meta,o=_(n);o&&!r[Be]&&(r[Be]=o);let s=Ve(n);s&&(r[re]||(r[re]=s),r[oe]||(r[oe]=s))}function Ve(e){if(!e)return;let t=e[re]??e[oe];if(S(t)||typeof t=="string")return t}function de(e){return e instanceof Error?e:new Error(String(e))}var ze="https://app.waniwani.ai";function Ct(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??H(),s=r.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=o._config.apiKey;return d?(a=new Y({apiUrl:o._config.apiUrl??ze,apiKey:d}),a):null}let c=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[p,g,f]=d,u=typeof p=="string"&&p.trim().length>0?p:"unknown";if(typeof f!="function")return c(...d);let h=f;return c(p,g,async(l,y)=>{let x=U(y)??{},b=he(o,x);S(y)&&(y[X]=b);let E=performance.now(),j=e.server?.getClientVersion?.();try{let k=await h(l,y),I=Math.round(performance.now()-E),G=S(k)&&k.isError===!0;if(G){let ue=ie(k);console.error(`[waniwani] Tool "${u}" returned error${ue?`: ${ue}`:""}`)}return await ae(o,se(u,y,r,{durationMs:I,status:G?"error":"ok",...G&&{errorMessage:ie(k)??"Unknown tool error"}},j,{input:l,output:k}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),Le(k,y),s&&await qe(k,i(),o._config.apiUrl??ze,y,r.onError),k}catch(k){let I=Math.round(performance.now()-E);throw await ae(o,se(u,y,r,{durationMs:I,status:"error",errorMessage:k instanceof Error?k.message:String(k)},j,{input:l}),r.onError),r.flushAfterToolCall&&await ce(o,r.onError),k}})}),n}export{C as END,R as START,F as StateGraph,_e as createFlow,Me as createFlowTestHarness,Ne as createResource,it as createTool,Rt as createTrackingRoute,J as detectPlatform,Ye as isMCPApps,He as isOpenAI,st as registerTools,Ct as withWaniwani};
|
|
7
7
|
//# sourceMappingURL=index.js.map
|