@waniwani/sdk 0.10.2 → 0.10.3-beta.1
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/next-js/index.d.ts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/mcp/index.d.ts +76 -4
- package/dist/mcp/index.js +5 -5
- package/dist/mcp/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/kb/index.d.ts +0 -84
|
@@ -57,7 +57,7 @@ interface KbClient {
|
|
|
57
57
|
sources(): Promise<KbSource[]>;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified";
|
|
60
|
+
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified" | "flow.node_reached";
|
|
61
61
|
interface ToolCalledProperties {
|
|
62
62
|
name?: string;
|
|
63
63
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
@@ -122,6 +122,12 @@ type TrackEvent = ({
|
|
|
122
122
|
properties?: PurchaseCompletedProperties;
|
|
123
123
|
} & BaseTrackEvent) | ({
|
|
124
124
|
event: "user.identified";
|
|
125
|
+
} & BaseTrackEvent) | ({
|
|
126
|
+
event: "flow.node_reached";
|
|
127
|
+
properties?: {
|
|
128
|
+
flowId?: string;
|
|
129
|
+
nodeId?: string;
|
|
130
|
+
};
|
|
125
131
|
} & BaseTrackEvent);
|
|
126
132
|
/**
|
|
127
133
|
* Legacy tracking shape supported for existing integrations.
|
package/dist/index.d.ts
CHANGED
|
@@ -59,7 +59,7 @@ interface KbClient {
|
|
|
59
59
|
sources(): Promise<KbSource[]>;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified";
|
|
62
|
+
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified" | "flow.node_reached";
|
|
63
63
|
interface ToolCalledProperties {
|
|
64
64
|
name?: string;
|
|
65
65
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
@@ -124,6 +124,12 @@ type TrackEvent = ({
|
|
|
124
124
|
properties?: PurchaseCompletedProperties;
|
|
125
125
|
} & BaseTrackEvent) | ({
|
|
126
126
|
event: "user.identified";
|
|
127
|
+
} & BaseTrackEvent) | ({
|
|
128
|
+
event: "flow.node_reached";
|
|
129
|
+
properties?: {
|
|
130
|
+
flowId?: string;
|
|
131
|
+
nodeId?: string;
|
|
132
|
+
};
|
|
127
133
|
} & BaseTrackEvent);
|
|
128
134
|
/**
|
|
129
135
|
* Legacy tracking shape supported for existing integrations.
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -370,7 +370,7 @@ interface KbClient {
|
|
|
370
370
|
sources(): Promise<KbSource[]>;
|
|
371
371
|
}
|
|
372
372
|
|
|
373
|
-
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified";
|
|
373
|
+
type EventType = "session.started" | "tool.called" | "quote.requested" | "quote.succeeded" | "quote.failed" | "link.clicked" | "purchase.completed" | "widget_render" | "widget_click" | "widget_link_click" | "widget_error" | "widget_scroll" | "widget_form_field" | "widget_form_submit" | "user.identified" | "flow.node_reached";
|
|
374
374
|
interface ToolCalledProperties {
|
|
375
375
|
name?: string;
|
|
376
376
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
@@ -435,6 +435,12 @@ type TrackEvent = ({
|
|
|
435
435
|
properties?: PurchaseCompletedProperties;
|
|
436
436
|
} & BaseTrackEvent) | ({
|
|
437
437
|
event: "user.identified";
|
|
438
|
+
} & BaseTrackEvent) | ({
|
|
439
|
+
event: "flow.node_reached";
|
|
440
|
+
properties?: {
|
|
441
|
+
flowId?: string;
|
|
442
|
+
nodeId?: string;
|
|
443
|
+
};
|
|
438
444
|
} & BaseTrackEvent);
|
|
439
445
|
/**
|
|
440
446
|
* Legacy tracking shape supported for existing integrations.
|
|
@@ -749,6 +755,29 @@ type NodeHandler<TState> = (ctx: NodeContext<TState>) => MaybePromise<DeepPartia
|
|
|
749
755
|
* Receives current state, returns the name of the next node.
|
|
750
756
|
*/
|
|
751
757
|
type ConditionFn<TState> = (state: Partial<TState>) => string | Promise<string>;
|
|
758
|
+
type NodeOptions = {
|
|
759
|
+
trackFunnel?: true | string;
|
|
760
|
+
};
|
|
761
|
+
type FlowGraphNode = {
|
|
762
|
+
id: string;
|
|
763
|
+
type: "widget" | "interrupt" | "action";
|
|
764
|
+
trackFunnel: false | true | string;
|
|
765
|
+
};
|
|
766
|
+
type FlowGraphEdge = {
|
|
767
|
+
from: string;
|
|
768
|
+
to: string;
|
|
769
|
+
type: "direct";
|
|
770
|
+
} | {
|
|
771
|
+
from: string;
|
|
772
|
+
to: string[];
|
|
773
|
+
type: "conditional";
|
|
774
|
+
};
|
|
775
|
+
type FlowGraph = {
|
|
776
|
+
flowId: string;
|
|
777
|
+
title: string;
|
|
778
|
+
nodes: FlowGraphNode[];
|
|
779
|
+
edges: FlowGraphEdge[];
|
|
780
|
+
};
|
|
752
781
|
type FlowConfig = {
|
|
753
782
|
/** Unique identifier for the flow (becomes the MCP tool name) */
|
|
754
783
|
id: string;
|
|
@@ -833,6 +862,7 @@ type RegisteredFlow = {
|
|
|
833
862
|
register: (server: McpServer) => Promise<void>;
|
|
834
863
|
/** Returns a Mermaid `flowchart TD` diagram of the flow graph. */
|
|
835
864
|
graph: () => string;
|
|
865
|
+
flowGraph: FlowGraph;
|
|
836
866
|
};
|
|
837
867
|
type FlowTokenContent = {
|
|
838
868
|
step?: string;
|
|
@@ -895,6 +925,7 @@ type FlowErrorContent = {
|
|
|
895
925
|
declare class StateGraph<TState extends Record<string, unknown>, TNodes extends string = never> {
|
|
896
926
|
private nodes;
|
|
897
927
|
private edges;
|
|
928
|
+
private nodeOptions;
|
|
898
929
|
private config;
|
|
899
930
|
constructor(config: FlowConfig);
|
|
900
931
|
/**
|
|
@@ -902,7 +933,7 @@ declare class StateGraph<TState extends Record<string, unknown>, TNodes extends
|
|
|
902
933
|
*
|
|
903
934
|
* The handler receives a context object with `state`, `meta`, `interrupt`, and `showWidget`.
|
|
904
935
|
*/
|
|
905
|
-
addNode<TName extends string>(name: TName, handler: NodeHandler<TState
|
|
936
|
+
addNode<TName extends string>(name: TName, handler: NodeHandler<TState>, options?: NodeOptions): StateGraph<TState, TNodes | TName>;
|
|
906
937
|
/**
|
|
907
938
|
* Add a direct edge between two nodes.
|
|
908
939
|
*
|
|
@@ -979,6 +1010,24 @@ declare function createFlow<const TSchema extends Record<string, z.ZodType>>(con
|
|
|
979
1010
|
state: TSchema;
|
|
980
1011
|
}): StateGraph<InferFlowState<TSchema>>;
|
|
981
1012
|
|
|
1013
|
+
/**
|
|
1014
|
+
* Mark a Zod schema as PII — its value will be replaced with `"REDACTED"` in
|
|
1015
|
+
* any `tool.called` event payload sent to the WaniWani API. The handler still
|
|
1016
|
+
* receives the original value; only the tracked copy is scrubbed.
|
|
1017
|
+
*
|
|
1018
|
+
* Apply at the end of the schema chain so the marker is attached to the final
|
|
1019
|
+
* schema:
|
|
1020
|
+
*
|
|
1021
|
+
* ```ts
|
|
1022
|
+
* ages: redacted(z.string().describe("Comma-separated ages")),
|
|
1023
|
+
* zipcode: redacted(z.string().describe("Spanish postal code")),
|
|
1024
|
+
* ```
|
|
1025
|
+
*
|
|
1026
|
+
* Uses Zod v4's `.meta()` registry, so the marker is preserved across schema
|
|
1027
|
+
* clones (`.optional()`, `.default()`, etc.).
|
|
1028
|
+
*/
|
|
1029
|
+
declare function redacted<T extends z.ZodType>(schema: T): T;
|
|
1030
|
+
|
|
982
1031
|
type WithDecodedState = {
|
|
983
1032
|
decodedState: FlowTokenContent | null;
|
|
984
1033
|
};
|
|
@@ -986,7 +1035,7 @@ type FlowTestResult = (FlowInterruptContent & WithDecodedState) | (FlowWidgetCon
|
|
|
986
1035
|
declare function createFlowTestHarness(flow: RegisteredFlow, options?: {
|
|
987
1036
|
stateStore?: FlowStore;
|
|
988
1037
|
}): Promise<{
|
|
989
|
-
start(intent: string, stateUpdates?: Record<string, unknown
|
|
1038
|
+
start(intent: string, stateUpdates?: Record<string, unknown>, context?: string): Promise<FlowTestResult>;
|
|
990
1039
|
continueWith(stateUpdates?: Record<string, unknown>): Promise<FlowTestResult>;
|
|
991
1040
|
lastState(): Promise<FlowTokenContent | null>;
|
|
992
1041
|
}>;
|
|
@@ -1162,6 +1211,29 @@ type WithWaniwaniOptions = {
|
|
|
1162
1211
|
* @default true
|
|
1163
1212
|
*/
|
|
1164
1213
|
injectWidgetToken?: boolean;
|
|
1214
|
+
/**
|
|
1215
|
+
* Strip `latitude`/`longitude` from known location `_meta` entries
|
|
1216
|
+
* (`openai/userLocation`, `waniwani/geoLocation`, `waniwani/userLocation`)
|
|
1217
|
+
* before events are sent to the WaniWani API. Applied to both the
|
|
1218
|
+
* request-level `_meta` and any `_meta` on the tool response.
|
|
1219
|
+
*
|
|
1220
|
+
* Enable this when precise geo coordinates should not reach the tracking
|
|
1221
|
+
* backend; city/region/country fields are preserved.
|
|
1222
|
+
*
|
|
1223
|
+
* @default false
|
|
1224
|
+
*/
|
|
1225
|
+
stripGeoCoordinates?: boolean;
|
|
1226
|
+
/**
|
|
1227
|
+
* Replace `input.stateUpdates[field]` with `"REDACTED"` for any field
|
|
1228
|
+
* marked via `redacted()` on a flow state schema. When `false` (default),
|
|
1229
|
+
* the declarative markers are ignored and raw values are tracked.
|
|
1230
|
+
*
|
|
1231
|
+
* Wire this to an env var when you want real values in development logs
|
|
1232
|
+
* but redacted values in production.
|
|
1233
|
+
*
|
|
1234
|
+
* @default false
|
|
1235
|
+
*/
|
|
1236
|
+
applyFieldRedactions?: boolean;
|
|
1165
1237
|
};
|
|
1166
1238
|
/**
|
|
1167
1239
|
* Wrap an MCP server so tool handlers automatically emit `tool.called` events.
|
|
@@ -1185,4 +1257,4 @@ type WithWaniwaniOptions = {
|
|
|
1185
1257
|
*/
|
|
1186
1258
|
declare function withWaniwani(server: McpServer, options?: WithWaniwaniOptions): McpServer;
|
|
1187
1259
|
|
|
1188
|
-
export { type ConditionFn, END, type FlowConfig, type FlowTestResult, type HostContext, type InferFlowState, type InterruptSignal, type KvStore, 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, WaniwaniKvStore, type WidgetCSP, type WidgetPlatform, type WidgetSignal, type WithWaniwaniOptions, createFlow, createFlowTestHarness, createResource, createTool, createTrackingRoute, detectPlatform, isMCPApps, isOpenAI, registerTools, withWaniwani };
|
|
1260
|
+
export { type ConditionFn, END, type FlowConfig, type FlowTestResult, type HostContext, type InferFlowState, type InterruptSignal, type KvStore, 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, WaniwaniKvStore, type WidgetCSP, type WidgetPlatform, type WidgetSignal, type WithWaniwaniOptions, createFlow, createFlowTestHarness, createResource, createTool, createTrackingRoute, detectPlatform, isMCPApps, isOpenAI, redacted, registerTools, withWaniwani };
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function
|
|
1
|
+
function ne(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function mt(){return ne()==="openai"}function ht(){return ne()==="mcp-apps"}var _="__start__",C="__end__",Te=Symbol.for("waniwani.flow.interrupt"),ke=Symbol.for("waniwani.flow.widget");function Se(e,t){let n=t?.context,r=[];for(let[o,i]of Object.entries(e))if(typeof i=="object"&&i!==null&&"question"in i){let s=i;r.push({question:s.question,field:o,suggestions:s.suggestions,context:s.context,validate:s.validate})}return{__type:Te,questions:r,context:n}}function ve(e,t){return{__type:ke,tool:typeof e=="string"?e:e.id,...t}}function Re(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===Te}function xe(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ke}import{z as M}from"zod";var re="waniwani/client";function B(e){if(typeof e=="object"&&e!==null)return e[re]}function Ee(e,t,n){return{track(r){return e.track({...r,meta:{...t,...r.meta}})},identify(r,o){return e.identify(r,o,t)},kb:e.kb,_config:n}}function D(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var yt=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Tt=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],kt=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],St=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],vt=["correlationId","openai/requestId"];function b(e){return e?D(e,yt):void 0}function Ie(e){return e?D(e,Tt):void 0}function Ce(e){return e?D(e,kt):void 0}function _e(e){return e?D(e,St):void 0}function be(e){return e?D(e,vt):void 0}var Rt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}];function O(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 Rt){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function Pe(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function K(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 xt(e,t,n){let r=t.split("."),o=r.pop();if(!o)return;let i=e;for(let s of r)(i[s]==null||typeof i[s]!="object"||Array.isArray(i[s]))&&(i[s]={}),i=i[s];i[o]=n}function Fe(e,t){let n=t.split("."),r=n.pop();if(!r)return;let o=e;for(let i of n){if(o==null||typeof o!="object")return;o=o[i]}o!=null&&typeof o=="object"&&delete o[r]}function oe(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?xt(t,n,r):t[n]=r;return t}function F(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]=F(n[r],o):n[r]=o;return n}function ie(e){return e!=null&&e!==""}async function W(e,t){return e.type==="direct"?e.to:e.condition(t)}function We(e,t,n,r){if(e.every(p=>ie(K(r,p.field))))return null;let o=e.filter(p=>!ie(K(r,p.field))),i=o.length===1,s=o[0];return{content:i&&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,...i&&s?{field:s.field}:{}}}}async function q(e,t,n,r,o,i,s,a,p){let c=e,d={...t},u=50,g=0;for(;g++<u;){if(c===C)return{content:{status:"complete"},flowTokenContent:{state:d}};let l=n.get(c);if(!l)return{content:{status:"error",error:`Unknown node: "${c}"`}};a?.get(c)?.trackFunnel&&s&&s.track({event:"flow.node_reached",properties:{flowId:p??"unknown",nodeId:c}}).catch(()=>{});try{let f=await l({state:d,meta:i,interrupt:Se,showWidget:ve,waniwani:s});if(Re(f)){for(let v of f.questions)v.validate&&o.set(`${c}:${v.field}`,v.validate);let S=We(f.questions,f.context,c,d);if(S)return S;for(let v of f.questions){let R=o.get(`${c}:${v.field}`);if(R)try{let E=K(d,v.field),P=await R(E);P&&typeof P=="object"&&(d=F(d,P))}catch(E){let P=E instanceof Error?E.message:String(E);Fe(d,v.field);let wt=f.questions.map(U=>U.field===v.field?{...U,context:U.context?`ERROR: ${P}
|
|
2
2
|
|
|
3
|
-
${
|
|
4
|
-
`)}var
|
|
5
|
-
${
|
|
6
|
-
`)}var M=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===E||t===I)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 Ue(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Ne({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>Ue(n,r)})}validate(){if(!this.edges.has(E))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(E);if(t?.type==="direct"&&t.to!==I&&!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!==E&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==I&&!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 De(e){return new M(e)}function Ke(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Oe(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...u)=>{r.push(u)}};await e.register(i);let a=r[0]?.[2];if(!a)throw new Error(`Flow "${e.name}" did not register a handler`);let s={_meta:{sessionId:o}};async function c(u){return{...u,decodedState:n?await n.get(o):null}}return{async start(u,f){let d=await a({action:"start",intent:u,...f?{stateUpdates:f}:{}},s);return c(Ke(d))},async continueWith(u){let f=await a({action:"continue",...u?{stateUpdates:u}:{}},s);return c(Ke(f))},async lastState(){return n?n.get(o):null}}}var $="text/html+skybridge",B="text/html;profile=mcp-app",je=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function $e(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Be(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 te(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 qe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:i,widgetDomain:a,prefersBorder:s=!0,autoHeight:c=!0}=e,u=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(u={...u,connect_domains:[...u.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...u.resource_domains||[],`http://${w}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,l=null,g=()=>(l||(l=je(o,i)),l),T=r;async function h(w){let p=await g();w.registerResource(`${t}-openai-widget`,f,{title:n,description:T,mimeType:$,_meta:{"openai/widgetDescription":T,"openai/widgetPrefersBorder":s}},async v=>({contents:[{uri:v.href,mimeType:$,text:p,_meta:$e({description:T,prefersBorder:s,widgetDomain:a,widgetCSP:u})}]})),w.registerResource(`${t}-mcp-widget`,d,{title:n,description:T,mimeType:B,_meta:{ui:{prefersBorder:s}}},async v=>({contents:[{uri:v.href,mimeType:B,text:p,_meta:Be({description:T,prefersBorder:s,widgetDomain:a,widgetCSP:u})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:d,autoHeight:c,register:h}}function yt(e,t){let{resource:n,description:r,inputSchema:o,annotations:i,autoInjectResultText:a=!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 u=n?te({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(f){f.registerTool(s,{title:c,description:r,inputSchema:o,annotations:i,...u&&{_meta:u}},(async(d,l)=>{let g=l,T=g._meta??{},h=O(g),w=await t(d,{extra:{_meta:T},waniwani:h});return n&&w.data?{content:[{type:"text",text:w.text}],structuredContent:w.data,_meta:{...u,...T,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:w.text}],...w.data?{structuredContent:w.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ht(e,t){await Promise.all(t.map(n=>n.register(e)))}var q=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var Tt="@waniwani/sdk";function He(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(i,a,s){let c=r(),u=`${t.replace(/\/$/,"")}${a}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":Tt},d={method:i,headers:f};s!==void 0&&(f["Content-Type"]="application/json",d.body=JSON.stringify(s));let l=await fetch(u,d);if(!l.ok){let T=await l.text().catch(()=>"");throw new q(T||`KB API error: HTTP ${l.status}`,l.status)}return(await l.json()).data}return{async ingest(i){return o("POST","/api/mcp/kb/ingest",{files:i})},async search(i,a){return o("POST","/api/mcp/kb/search",{query:i,...a})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var kt="@waniwani/sdk";function L(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Le,o=xt(e),i=H(e.meta),a=H(e.metadata),s=Rt(e,i),c=R(e.eventId)??r(),u=Et(e.timestamp,n),f=R(e.source)??b(i)??t.source??kt,d=ne(e)?{...e}:void 0,l={...a};return Object.keys(i).length>0&&(l.meta=i),d&&(l.rawLegacy=d),{id:c,type:"mcp.event",name:o,source:f,timestamp:u,correlation:s,properties:St(e,o),metadata:l,rawLegacy:d}}function Le(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function St(e,t){if(!ne(e))return H(e.properties);let n=vt(e,t),r=H(e.properties);return{...n,...r}}function vt(e,t){switch(t){case"tool.called":{let n={};return R(e.toolName)&&(n.name=e.toolName),R(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),R(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return R(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),R(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function xt(e){return ne(e)?e.eventType:e.event}function Rt(e,t){let n=R(e.requestId)??ve(t),r=R(e.sessionId)??C(t),o=R(e.traceId)??xe(t),i=R(e.externalUserId)??Re(t),a=R(e.correlationId)??Ee(t)??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),a&&(s.correlationId=a),i&&(s.externalUserId=i),s}function Et(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 H(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function R(e){if(typeof e=="string"&&e.trim().length!==0)return e}function ne(e){return"eventType"in e}var It="/api/mcp/events/v2/batch";var Ve="@waniwani/sdk",Ct=new Set([401,403]),bt=new Set([408,425,429,500,502,503,504]);function ze(e){return new re(e)}var re=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.apiUrl,t.endpointPath??It),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":Ve},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Mt(i)}}if(Ct.has(n.status))return{kind:"auth",status:n.status};if(bt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Pt(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:Ve,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],i=[];for(let a of n){let s=r.get(a.eventId);if(s){if(_t(a)){o.push(s);continue}i.push(s)}}return{retryable:o,permanent:i}}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 _t(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 Pt(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 Mt(e){return e instanceof Error?e.message:String(e)}function Ye(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 i=n?ze({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(s,c,u){o();let f=L({event:"user.identified",externalUserId:s,properties:c,meta:u});return i?.enqueue(f),{eventId:f.id}},async track(s){o();let c=L(s);return i?.enqueue(c),{eventId:c.id}},async flush(){o(),await i?.flush()},async shutdown(s){return o(),await i?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&Ft(a,r.shutdownTimeoutMs),a}function Ft(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 V(e){let t=e,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},i={apiUrl:n,apiKey:r,tracking:o},a=Ye(i),s=He(i);return{...a,kb:s,_config:i}}function At(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 Nt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=V(t)),n}return async function(i){let a;try{a=await i.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 s=r(),c=[];for(let u of a.events){let f=At(u),d=await s.track(f);c.push(d.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"}})}}}function z(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var oe=z("widget-token",!!process.env.WANIWANI_DEBUG),Ut=120*1e3,Y=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-Ut?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=Dt(this.config.apiUrl,"/api/mcp/widget-tokens");oe("minting token from",r);let o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let i=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o),signal:AbortSignal.timeout(5e3)});if(oe("mint response:",i.status),!i.ok)return null;let a=await i.json(),s=a.data&&typeof a.data=="object"?a.data:a,c=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(c)?null:(this.cached={token:s.token,expiresAt:c},s.token)}catch(i){return oe("mint failed:",i),null}}};function Dt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Ge="waniwani/sessionId",ie="waniwani/geoLocation",se="waniwani/userLocation";function k(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function K(e){if(!k(e))return;let t=e._meta;return k(t)?t:void 0}function ae(e){if(!k(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>k(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function Kt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function ce(e,t,n,r,o,i){let a=Kt(e,n.toolType),s=K(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(s),"-> source:",b(s)),{event:"tool.called",properties:{name:e,type:a,...r??{},...i?.input!==void 0&&{input:i.input},...i?.output!==void 0&&{output:i.output}},meta:s,source:b(s),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function de(e,t,n){try{await e.track(t)}catch(r){n?.(ue(r))}}async function pe(e,t){try{await e.flush()}catch(n){t?.(ue(n))}}async function Ze(e,t,n,r,o){if(!k(e))return;k(e._meta)||(e._meta={});let i=e._meta,a=k(i.waniwani)?i.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){o?.(ue(d))}let c=C(i);c&&(s.sessionId||(s.sessionId=c));let u=et(i);u!==void 0&&(s.geoLocation||(s.geoLocation=u));let f=b(K(r));f&&!s.source&&(s.source=f),i.waniwani=s}var Je=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function Xe(e,t){if(!t||!k(e))return;let n=!1;for(let o of Je)if(o in t){n=!0;break}if(!n)return;k(e._meta)||(e._meta={});let r=e._meta;for(let o of Je)o in t&&(o in r||(r[o]=t[o]))}function Qe(e,t){let n=K(t);if(!n||!k(e))return;k(e._meta)||(e._meta={});let r=e._meta,o=C(n);o&&!r[Ge]&&(r[Ge]=o);let i=et(n);i&&(r[ie]||(r[ie]=i),r[se]||(r[se]=i))}function et(e){if(!e)return;let t=e[ie]??e[se];if(k(t)||typeof t=="string")return t}function ue(e){return e instanceof Error?e:new Error(String(e))}function le(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function tt(e){if(console.log("[waniwani:debug] extractTransportSessionId extra keys:",Object.keys(e)),console.log("[waniwani:debug] extractTransportSessionId extra.sessionId:",typeof e.sessionId,e.sessionId),typeof e.sessionId=="string"&&e.sessionId)return console.log("[waniwani:debug] extractTransportSessionId found extra.sessionId:",e.sessionId),e.sessionId;if(le(e.requestInfo)){let t=e.requestInfo.headers;if(console.log("[waniwani:debug] extractTransportSessionId requestInfo.headers:",le(t)?Object.keys(t):t),le(t)){let n={};for(let o of Object.keys(t))n[o.toLowerCase()]=t[o];let r=n["mcp-session-id"];if(console.log("[waniwani:debug] extractTransportSessionId headers['mcp-session-id']:",r),typeof r=="string"&&r)return r}}else console.log("[waniwani:debug] extractTransportSessionId no requestInfo on extra")}var rt=Symbol.for("waniwani.wrappedHandler"),G=z("mcp",!!process.env.WANIWANI_DEBUG),ot="https://app.waniwani.ai";function nt(e,t,n,r){let{server:o,tracker:i,opts:a,tokenCache:s,injectToken:c}=n,u=async(f,d)=>{let l=K(d)??{},g=C(l);if(console.log("[waniwani:debug] bridge sessionId \u2014 existingSessionId from meta:",g,"| meta keys:",Object.keys(l)),!g&&k(d)){let p=tt(d);console.log("[waniwani:debug] bridge sessionId \u2014 transportSid:",p),p&&(l["waniwani/sessionId"]=p,d._meta=l)}let T=Se(i,l,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});k(d)&&(d[Z]=T);let h=performance.now(),w=o.server?.getClientVersion?.();try{let p=await t(f,d),v=Math.round(performance.now()-h);G(`tool "${e}" handler returned in ${v}ms, running post-processing...`);let S=k(p)&&p.isError===!0;if(S){let x=ae(p);console.error(`[waniwani] Tool "${e}" returned error${x?`: ${x}`:""}`)}return await de(i,ce(e,d,a,{durationMs:v,status:S?"error":"ok",...S&&{errorMessage:ae(p)??"Unknown tool error"}},w,{input:f,output:p}),a.onError),G(`tool "${e}" tracking done`),a.flushAfterToolCall&&await pe(i,a.onError),Qe(p,d),Xe(p,r),c&&(await Ze(p,s,i._config.apiUrl??ot,d,a.onError),G(`tool "${e}" widget config injected`)),G(`tool "${e}" post-processing complete, returning result`),p}catch(p){let v=Math.round(performance.now()-h);throw await de(i,ce(e,d,a,{durationMs:v,status:"error",errorMessage:p instanceof Error?p.message:String(p)},w,{input:f}),a.onError),a.flushAfterToolCall&&await pe(i,a.onError),p}};return u[rt]=!0,u}function Ot(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??V(),i=r.injectWidgetToken!==!1,a=o._config.apiKey?new Y({apiUrl:o._config.apiUrl??ot,apiKey:o._config.apiKey}):null,s={server:e,tracker:o,opts:r,tokenCache:a,injectToken:i},c=e.registerTool.bind(e);n.registerTool=((...f)=>{let[d,l,g]=f;if(typeof g!="function")return c(...f);let T=typeof d=="string"&&d.trim().length>0?d:"unknown",h=k(l)&&k(l._meta)?l._meta:void 0,w=nt(T,g,s,h);return c(d,l,w)});let u=e._registeredTools;if(k(u))for(let[f,d]of Object.entries(u)){if(!k(d))continue;let l=d.handler;if(typeof l!="function"||l[rt])continue;let g=k(d._meta)?d._meta:void 0;d.handler=nt(f,l,s,g)}return n}export{I as END,E as START,M as StateGraph,W as WaniwaniKvStore,De as createFlow,Oe as createFlowTestHarness,qe as createResource,yt as createTool,Nt as createTrackingRoute,J as detectPlatform,st as isMCPApps,it as isOpenAI,ht as registerTools,Ot as withWaniwani};
|
|
3
|
+
${U.context}`:`ERROR: ${P}`}:U),ye=We(wt,f.context,c,d);if(ye)return ye;break}}let w=r.get(c);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${c}"`}};c=await W(w,d);continue}if(xe(f)){let S=f.field;if(S&&ie(K(d,S))){let w=r.get(c);if(!w)return{content:{status:"error",error:`No outgoing edge from node "${c}"`}};c=await W(w,d);continue}return{content:{status:"widget",tool:f.tool,data:f.data,description:f.description,interactive:f.interactive!==!1},flowTokenContent:{step:c,state:d,field:S,widgetId:f.tool}}}d=F(d,f);let k=r.get(c);if(!k)return{content:{status:"error",error:`No outgoing edge from node "${c}"`}};c=await W(k,d)}catch(x){return{content:{status:"error",error:x instanceof Error?x.message:String(x)},flowTokenContent:{step:c,state:d}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}function se(e){return typeof e=="object"&&e!==null&&e.__ww_enc===1&&typeof e.ct=="string"&&typeof e.iv=="string"}var Ae=new Map;async function Me(e){let t=Ae.get(e);if(t)return t;let n=Buffer.from(e,"base64");if(n.length!==32)throw new Error("[WaniWani KV] WANIWANI_ENCRYPTION_KEY must be a base64-encoded 32-byte (256-bit) key.");let r=await globalThis.crypto.subtle.importKey("raw",n,{name:"AES-GCM"},!1,["encrypt","decrypt"]);return Ae.set(e,r),r}async function Ne(e,t){let n=await Me(t),r=globalThis.crypto.getRandomValues(new Uint8Array(12)),o=new TextEncoder().encode(JSON.stringify(e)),i=await globalThis.crypto.subtle.encrypt({name:"AES-GCM",iv:r},n,o);return{__ww_enc:1,ct:Buffer.from(i).toString("base64"),iv:Buffer.from(r).toString("base64")}}async function Ue(e,t){let n=await Me(t),r=Buffer.from(e.ct,"base64"),o=Buffer.from(e.iv,"base64"),i;try{i=await globalThis.crypto.subtle.decrypt({name:"AES-GCM",iv:o},n,r)}catch{throw new Error("[WaniWani KV] Decryption failed. The encryption key may be incorrect or the data may be corrupted.")}return JSON.parse(new TextDecoder().decode(i))}var Et="@waniwani/sdk",It="https://app.waniwani.ai",A=class{get baseUrl(){return(process.env.WANIWANI_API_URL??It).replace(/\/$/,"")}get apiKey(){return process.env.WANIWANI_API_KEY}get encryptionKey(){return process.env.WANIWANI_ENCRYPTION_KEY}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let n=await this.request("/api/mcp/redis/get",{key:t});if(n==null)return null;if(se(n)){if(!this.encryptionKey)throw new Error("[WaniWani KV] Encrypted data found but WANIWANI_ENCRYPTION_KEY is not set.");return Ue(n,this.encryptionKey)}return n}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");let r=this.encryptionKey?await Ne(n,this.encryptionKey):n;await this.request("/api/mcp/redis/set",{key:t,value:r})}async delete(t){if(this.apiKey)try{await this.request("/api/mcp/redis/delete",{key:t})}catch(n){console.error("[WaniWani KV] delete failed for key:",t,n)}}async request(t,n){let r=`${this.baseUrl}${t}`,o=await fetch(r,{method:"POST",headers:{Authorization:`Bearer ${this.apiKey}`,"Content-Type":"application/json","X-WaniWani-SDK":Et},body:JSON.stringify(n)});if(!o.ok){let s=await o.text().catch(()=>"");throw new Error(s||`KV store API error: HTTP ${o.status}`)}return(await o.json()).data}};var j=class{store=new A;get(t){return this.store.get(t)}set(t,n){return this.store.set(t,n)}delete(t){return this.store.delete(t)}};function Ct(e){let t=e.toString();return t.includes("showWidget")?"widget":t.includes("interrupt")?"interrupt":"action"}function _t(e,t){let n=e.toString(),r=[];for(let o of t){let i=`"${o}"`,s=`'${o}'`,a=`\`${o}\``;(n.includes(i)||n.includes(s)||n.includes(a))&&r.push(o)}return r}function De(e,t,n,r){let o=new Set([...t.keys(),C]),i=[];for(let[a,p]of t){let c=r.get(a);i.push({id:a,type:Ct(p),trackFunnel:c?.trackFunnel??!1})}let s=[];for(let[a,p]of n)if(p.type==="direct")s.push({from:a,to:p.to,type:"direct"});else{let c=_t(p.condition,o);s.push({from:a,to:c,type:"conditional"})}return{flowId:e.id,title:e.title,nodes:i,edges:s}}function Oe(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Ke(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin and include `intent`.'," `intent` must be a brief summary of the user's goal for this flow."," Do NOT invent missing intent."," Optionally include `context` \u2014 the situation or environment that led the user to start"," this flow (e.g. what page they are on, what they were doing, or what triggered the request)."," Only provide `context` when there is genuinely relevant situational information. Do NOT invent missing context."," If the user's message already contains answers to likely questions,"," extract them into `stateUpdates` as `{ field: value }` pairs."," 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 i=Pe(o);if(i){let s=o.description??"",a=Object.entries(i).map(([p,c])=>{let d=Oe(c);return d?`\`${r}.${p}\` (${d})`:`\`${r}.${p}\``}).join(", ");n.push(s?`\`${r}\` (${s}): ${a}`:`\`${r}\`: ${a}`)}else{let s=Oe(o);n.push(s?`\`${r}\` (${s})`:`\`${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
|
+
`)}function ae(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function je(e){let t=$e(e),n=ae(t.waniwani)?t.waniwani:{};return e.meta({...t,waniwani:{...n,redacted:!0}})}function bt(e){let n=$e(e).waniwani;return ae(n)&&n.redacted===!0}function $e(e){let t=e.meta;if(typeof t!="function")return{};let n=t.call(e);return ae(n)?n:{}}function Be(e){if(!e)return[];let t=[];for(let[n,r]of Object.entries(e))bt(r)&&t.push(n);return t}var H="waniwani/redactedStateUpdateFields";var Pt={action:M.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),intent:M.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow. Do not invent missing intent.`),context:M.string().optional().describe('Optional when action is "start". Describe the situation or environment that led the user to start this flow \u2014 e.g. what page they are on, what they were doing, or what triggered the request. Do not invent missing context.'),stateUpdates:M.record(M.string(),M.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 qe(e){let{config:t,nodes:n,edges:r}=e,o=De(t,n,r,e.nodeOptions),i=Ke(t),s=`${t.description}
|
|
5
|
+
${i}`,a=e.store??new j,p=new Map;async function c(l,y,x,f){if(l.action==="start"){let k=typeof l.intent=="string"?l.intent.trim():void 0;if(!k)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};if(l.intent=k,typeof l.context=="string"){let R=l.context.trim();l.context=R||void 0}let S=r.get(_);if(!S)return{content:{status:"error",error:"No start edge"}};let w=oe(l.stateUpdates??{}),v=await W(S,w);return q(v,w,n,r,p,x,f,e.nodeOptions,t.id)}if(l.action==="continue"){if(!y)return{content:{status:"error",error:"No session ID available for continue action."}};let k;try{k=await a.get(y)}catch(R){let E=R instanceof Error?R.message:String(R);return{content:{status:"error",error:`Failed to load flow state (session "${y}"): ${E}`}}}if(!k)return{content:{status:"error",error:`Flow state not found for session "${y}". The flow may have expired.`}};let S=k.state,w=k.step;if(!w)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let v=F(S,oe(l.stateUpdates??{}));if(k.widgetId){let R=r.get(w);if(!R)return{content:{status:"error",error:`No edge from step "${w}"`}};let E=await W(R,v);return q(E,v,n,r,p,x,f,e.nodeOptions,t.id)}return q(w,v,n,r,p,x,f,e.nodeOptions,t.id)}return{content:{status:"error",error:`Unknown action: "${l.action}"`}}}let d=Be(t.state),u={title:t.title,description:s,inputSchema:Pt,annotations:t.annotations,...d.length>0&&{_meta:{[H]:d}}},g=(async(l,y)=>{let x=y,f=x._meta??{},k=b(f),S=B(x),w=await c(l,k,f,S);if(k)if(w.flowTokenContent&&w.content.status!=="complete")try{await a.set(k,w.flowTokenContent)}catch(R){let E=R instanceof Error?R.message:String(R);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${k}"): ${E}`},null,2)}],_meta:f,isError:!0}}else w.content.status==="complete"&&await a.delete(k);return{content:[{type:"text",text:JSON.stringify(w.content,null,2)}],_meta:f,...w.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:u,handler:g,async register(l){let y={...u,_meta:{...u._meta,_flowGraph:o}};l.registerTool(t.id,y,g)},graph:e.graph,flowGraph:o}}function He(e,t){let n=["flowchart TD"];n.push(` ${_}((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 N=class{nodes=new Map;edges=new Map;nodeOptions=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===_||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),r&&this.nodeOptions.set(t,r),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 He(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return qe({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>He(n,r),nodeOptions:new Map(this.nodeOptions)})}validate(){if(!this.edges.has(_))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(_);if(t?.type==="direct"&&t.to!==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!==_&&!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 Le(e){return new N(e)}function Ve(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function ze(e,t){let n=t?.stateStore,r=[],o=`test-session-${Math.random().toString(36).slice(2,10)}`,i={registerTool:(...c)=>{r.push(c)}};await e.register(i);let s=r[0]?.[2];if(!s)throw new Error(`Flow "${e.name}" did not register a handler`);let a={_meta:{sessionId:o}};async function p(c){return{...c,decodedState:n?await n.get(o):null}}return{async start(c,d,u){let g=await s({action:"start",intent:c,...u?{context:u}:{},...d?{stateUpdates:d}:{}},a);return p(Ve(g))},async continueWith(c){let d=await s({action:"continue",...c?{stateUpdates:c}:{}},a);return p(Ve(d))},async lastState(){return n?n.get(o):null}}}var L="text/html+skybridge",V="text/html;profile=mcp-app",Ge=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Ye(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Ze(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 ce(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 Je(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:i,widgetDomain:s,prefersBorder:a=!0,autoHeight:p=!0}=e,c=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:f}=new URL(o);(f==="localhost"||f==="127.0.0.1")&&(c={...c,connect_domains:[...c.connect_domains||[],`ws://${f}:*`,`wss://${f}:*`],resource_domains:[...c.resource_domains||[],`http://${f}:*`]})}catch{}let d=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,g=null,l=()=>(g||(g=Ge(o,i)),g),y=r;async function x(f){let k=await l();f.registerResource(`${t}-openai-widget`,d,{title:n,description:y,mimeType:L,_meta:{"openai/widgetDescription":y,"openai/widgetPrefersBorder":a}},async S=>({contents:[{uri:S.href,mimeType:L,text:k,_meta:Ye({description:y,prefersBorder:a,widgetDomain:s,widgetCSP:c})}]})),f.registerResource(`${t}-mcp-widget`,u,{title:n,description:y,mimeType:V,_meta:{ui:{prefersBorder:a}}},async S=>({contents:[{uri:S.href,mimeType:V,text:k,_meta:Ze({description:y,prefersBorder:a,widgetDomain:s,widgetCSP:c})}]}))}return{id:t,title:n,description:r,openaiUri:d,mcpUri:u,autoHeight:p,register:x}}function Ft(e,t){let{resource:n,description:r,inputSchema:o,annotations:i,autoInjectResultText:s=!0}=e,a=e.id??n?.id,p=e.title??n?.title;if(!a)throw new Error("createTool: `id` is required when no resource is provided");if(!p)throw new Error("createTool: `title` is required when no resource is provided");let c=n?ce({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:a,title:p,description:r,async register(d){d.registerTool(a,{title:p,description:r,inputSchema:o,annotations:i,...c&&{_meta:c}},(async(u,g)=>{let l=g,y=l._meta??{},x=B(l),f=await t(u,{extra:{_meta:y},waniwani:x});return n&&f.data?{content:[{type:"text",text:f.text}],structuredContent:f.data,_meta:{...c,...y,...s===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:f.text}],...f.data?{structuredContent:f.data}:{},...s===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function Wt(e,t){await Promise.all(t.map(n=>n.register(e)))}var z=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var At="@waniwani/sdk";function Xe(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(i,s,a){let p=r(),c=`${t.replace(/\/$/,"")}${s}`,d={Authorization:`Bearer ${p}`,"X-WaniWani-SDK":At},u={method:i,headers:d};a!==void 0&&(d["Content-Type"]="application/json",u.body=JSON.stringify(a));let g=await fetch(c,u);if(!g.ok){let y=await g.text().catch(()=>"");throw new z(y||`KB API error: HTTP ${g.status}`,g.status)}return(await g.json()).data}return{async ingest(i){return o("POST","/api/mcp/kb/ingest",{files:i})},async search(i,s){return o("POST","/api/mcp/kb/search",{query:i,...s})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Mt="@waniwani/sdk";function Y(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??Qe,o=Dt(e),i=G(e.meta),s=G(e.metadata),a=Ot(e,i),p=I(e.eventId)??r(),c=Kt(e.timestamp,n),d=I(e.source)??O(i)??t.source??Mt,u=de(e)?{...e}:void 0,g={...s};return Object.keys(i).length>0&&(g.meta=i),u&&(g.rawLegacy=u),{id:p,type:"mcp.event",name:o,source:d,timestamp:c,correlation:a,properties:Nt(e,o),metadata:g,rawLegacy:u}}function Qe(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Nt(e,t){if(!de(e))return G(e.properties);let n=Ut(e,t),r=G(e.properties);return{...n,...r}}function Ut(e,t){switch(t){case"tool.called":{let n={};return I(e.toolName)&&(n.name=e.toolName),I(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),I(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return I(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),I(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function Dt(e){return de(e)?e.eventType:e.event}function Ot(e,t){let n=I(e.requestId)??Ie(t),r=I(e.sessionId)??b(t),o=I(e.traceId)??Ce(t),i=I(e.externalUserId)??_e(t),s=I(e.correlationId)??be(t)??n,a={};return r&&(a.sessionId=r),o&&(a.traceId=o),n&&(a.requestId=n),s&&(a.correlationId=s),i&&(a.externalUserId=i),a}function Kt(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 G(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function I(e){if(typeof e=="string"&&e.trim().length!==0)return e}function de(e){return"eventType"in e}var jt="/api/mcp/events/v2/batch";var et="@waniwani/sdk",$t=new Set([401,403]),Bt=new Set([408,425,429,500,502,503,504]);function tt(e){return new pe(e)}var pe=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=Lt(t.apiUrl,t.endpointPath??jt),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":et},body:JSON.stringify(this.makeBatchRequest(t))})}catch(i){return{kind:"retryable",reason:Vt(i)}}if($t.has(n.status))return{kind:"auth",status:n.status};if(Bt.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:et,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(s=>[s.id,s])),o=[],i=[];for(let s of n){let a=r.get(s.eventId);if(a){if(qt(s)){o.push(a);continue}i.push(a)}}return{retryable:o,permanent:i}}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 qt(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 Lt(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Vt(e){return e instanceof Error?e.message:String(e)}function nt(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 i=n?tt({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,s={async identify(a,p,c){o();let d=Y({event:"user.identified",externalUserId:a,properties:p,meta:c});return i?.enqueue(d),{eventId:d.id}},async track(a){o();let p=Y(a);return i?.enqueue(p),{eventId:p.id}},async flush(){o(),await i?.flush()},async shutdown(a){return o(),await i?.shutdown({timeoutMs:a?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return i&&zt(s,r.shutdownTimeoutMs),s}function zt(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 Z(e){let t=e,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},i={apiUrl:n,apiKey:r,tracking:o},s=nt(i),a=Xe(i);return{...s,kb:a,_config:i}}function Gt(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 Yt(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Z(t)),n}return async function(i){let s;try{s=await i.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(s.events)||s.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=r(),p=[];for(let c of s.events){let d=Gt(c),u=await a.track(d);p.push(u.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:p.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let p=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:p}),{status:500,headers:{"Content-Type":"application/json"}})}}}function J(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var ue=J("widget-token",!!process.env.WANIWANI_DEBUG),Zt=120*1e3,X=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-Zt?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=Jt(this.config.apiUrl,"/api/mcp/widget-tokens");ue("minting token from",r);let o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let i=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o),signal:AbortSignal.timeout(5e3)});if(ue("mint response:",i.status),!i.ok)return null;let s=await i.json(),a=s.data&&typeof s.data=="object"?s.data:s,p=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(p)?null:(this.cached={token:a.token,expiresAt:p},a.token)}catch(i){return ue("mint failed:",i),null}}};function Jt(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}async function Xt(e){let t=JSON.stringify({nodes:e.nodes,edges:e.edges});if(typeof globalThis.crypto?.subtle?.digest=="function"){let r=await crypto.subtle.digest("SHA-256",new TextEncoder().encode(t));return Array.from(new Uint8Array(r)).map(o=>o.toString(16).padStart(2,"0")).join("")}let n=0;for(let r=0;r<t.length;r++)n=(n<<5)-n+t.charCodeAt(r)|0;return`simple-${Math.abs(n).toString(36)}`}async function rt(e,t,n){if(e.length===0)return;let r=await Promise.all(e.map(async i=>({...i,configHash:await Xt(i)}))),o=JSON.stringify({flows:r});try{let i=`${t}/api/mcp/funnel/sync`;fetch(i,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`},body:o}).catch(()=>{})}catch{}}var ot="waniwani/sessionId",Q="waniwani/geoLocation",ee="waniwani/userLocation",Qt="openai/userLocation",en=[Qt,Q,ee],tn=["latitude","longitude"];function it(e){let t;for(let n of en){let r=e[n];if(!T(r))continue;let o;for(let i of tn)i in r&&(o||(o={...r}),delete o[i]);o&&(t||(t={...e}),t[n]=o)}return t??e}function T(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function $(e){if(!T(e))return;let t=e._meta;return T(t)?t:void 0}function le(e){if(!T(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>T(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function nn(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function fe(e,t,n,r,o,i){let s=nn(e,n.toolType),a=$(t),p=a&&n.stripGeoCoordinates?it(a):a,c=i?.input!==void 0&&n.redactInput?n.redactInput(i.input):i?.input,d=n.stripGeoCoordinates&&T(i?.output)&&T(i.output._meta)?{...i.output,_meta:it(i.output._meta)}:i?.output;return{event:"tool.called",properties:{name:e,type:s,...r??{},...c!==void 0&&{input:c},...d!==void 0&&{output:d}},meta:p,source:O(p),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ge(e,t,n){try{await e.track(t)}catch(r){n?.(me(r))}}async function we(e,t){try{await e.flush()}catch(n){t?.(me(n))}}async function at(e,t,n,r,o){if(!T(e))return;T(e._meta)||(e._meta={});let i=e._meta,s=T(i.waniwani)?i.waniwani:void 0,a={...s??{},endpoint:s?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(a.token=u)}catch(u){o?.(me(u))}let p=b(i);p&&(a.sessionId||(a.sessionId=p));let c=pt(i);c!==void 0&&(a.geoLocation||(a.geoLocation=c));let d=O($(r));d&&!a.source&&(a.source=d),i.waniwani=a}var st=["openai/outputTemplate","openai/widgetAccessible","openai/resultCanProduceWidget","openai/toolInvocation/invoking","openai/toolInvocation/invoked","ui/resourceUri","ui"];function ct(e,t){if(!t||!T(e))return;let n=!1;for(let o of st)if(o in t){n=!0;break}if(!n)return;T(e._meta)||(e._meta={});let r=e._meta;for(let o of st)o in t&&(o in r||(r[o]=t[o]))}function dt(e,t){let n=$(t);if(!n||!T(e))return;T(e._meta)||(e._meta={});let r=e._meta,o=b(n);o&&!r[ot]&&(r[ot]=o);let i=pt(n);i&&(r[Q]||(r[Q]=i),r[ee]||(r[ee]=i))}function pt(e){if(!e)return;let t=e[Q]??e[ee];if(T(t)||typeof t=="string")return t}function me(e){return e instanceof Error?e:new Error(String(e))}function ut(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function lt(e){if(typeof e.sessionId=="string"&&e.sessionId)return e.sessionId;if(ut(e.requestInfo)){let t=e.requestInfo.headers;if(ut(t)){let n={};for(let o of Object.keys(t))n[o.toLowerCase()]=t[o];let r=n["mcp-session-id"];if(typeof r=="string"&&r)return r}}}var gt=Symbol.for("waniwani.wrappedHandler"),te=J("mcp",!!process.env.WANIWANI_DEBUG),he="https://app.waniwani.ai",rn="REDACTED";function on(e){if(!e)return;let t=e[H];if(!Array.isArray(t)||t.length===0)return;let n=new Set(t.filter(r=>typeof r=="string"));if(n.size!==0)return r=>{if(!T(r))return r;let o=r.stateUpdates;if(!T(o))return r;let i=!1,s={...o};for(let a of n)a in s&&(s[a]=rn,i=!0);return i?{...r,stateUpdates:s}:r}}function ft(e,t,n,r){let{server:o,tracker:i,opts:s,tokenCache:a,injectToken:p}=n,c=s.applyFieldRedactions===!0?on(r):void 0,d=c?{...s,redactInput:c}:s,u=async(g,l)=>{let y=$(l)??{};if(!b(y)&&T(l)){let w=lt(l);w&&(y["waniwani/sessionId"]=w,l._meta=y)}let f=Ee(i,y,{apiUrl:i._config.apiUrl,apiKey:i._config.apiKey});T(l)&&(l[re]=f);let k=performance.now(),S=o.server?.getClientVersion?.();try{let w=await t(g,l),v=Math.round(performance.now()-k);te(`tool "${e}" handler returned in ${v}ms, running post-processing...`);let R=T(w)&&w.isError===!0;if(R){let E=le(w);console.error(`[waniwani] Tool "${e}" returned error${E?`: ${E}`:""}`)}return await ge(i,fe(e,l,d,{durationMs:v,status:R?"error":"ok",...R&&{errorMessage:le(w)??"Unknown tool error"}},S,{input:g,output:w}),s.onError),te(`tool "${e}" tracking done`),s.flushAfterToolCall&&await we(i,s.onError),dt(w,l),ct(w,r),p&&(await at(w,a,i._config.apiUrl??he,l,s.onError),te(`tool "${e}" widget config injected`)),te(`tool "${e}" post-processing complete, returning result`),w}catch(w){let v=Math.round(performance.now()-k);throw await ge(i,fe(e,l,d,{durationMs:v,status:"error",errorMessage:w instanceof Error?w.message:String(w)},S,{input:g}),s.onError),s.flushAfterToolCall&&await we(i,s.onError),w}};return u[gt]=!0,u}function sn(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Z(),i=r.injectWidgetToken!==!1,s=o._config.apiKey?new X({apiUrl:o._config.apiUrl??he,apiKey:o._config.apiKey}):null,a={server:e,tracker:o,opts:r,tokenCache:s,injectToken:i},p=e.registerTool.bind(e);n.registerTool=((...d)=>{let[u,g,l]=d;if(typeof l!="function")return p(...d);let y=typeof u=="string"&&u.trim().length>0?u:"unknown",x=T(g)&&T(g._meta)?g._meta:void 0,f=ft(y,l,a,x);return p(u,g,f)});let c=e._registeredTools;if(T(c))for(let[d,u]of Object.entries(c)){if(!T(u))continue;let g=u.handler;if(typeof g!="function"||g[gt])continue;let l=T(u._meta)?u._meta:void 0;u.handler=ft(d,g,a,l)}if(o._config.apiKey){let d=e._registeredTools,u=[];if(d&&typeof d=="object"){for(let g of Object.values(d))if(g&&typeof g=="object"){let l=g._meta,y=l&&typeof l=="object"?l._flowGraph:void 0;y?.nodes?.length&&u.push(y)}}u.length>0&&rt(u,o._config.apiUrl??he,o._config.apiKey)}return n}export{C as END,_ as START,N as StateGraph,A as WaniwaniKvStore,Le as createFlow,ze as createFlowTestHarness,Je as createResource,Ft as createTool,Yt as createTrackingRoute,ne as detectPlatform,ht as isMCPApps,mt as isOpenAI,je as redacted,Wt as registerTools,sn as withWaniwani};
|
|
7
7
|
//# sourceMappingURL=index.js.map
|