@waniwani/sdk 0.9.4 → 0.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,164 +1,153 @@
1
- # @waniwani
1
+ # @waniwani/sdk
2
2
 
3
- SDK for [app.waniwani.ai](https://app.waniwani.ai) with MCP tracking, widget helpers, and chat tooling.
3
+ [![npm](https://img.shields.io/npm/v/@waniwani/sdk.svg)](https://www.npmjs.com/package/@waniwani/sdk)
4
+ [![license](https://img.shields.io/npm/l/@waniwani/sdk.svg)](./LICENSE)
4
5
 
5
- ## Warning
6
+ > The official SDK for [WaniWani](https://waniwani.ai) — build, ship, and measure conversational MCP apps.
6
7
 
7
- This is pre-alpha software:
8
+ `@waniwani/sdk` is the developer-facing library that plugs into your MCP (Model Context Protocol) server and gives you **event tracking** and **multi-step conversational flows** out of the box.
8
9
 
9
- - APIs can change without notice
10
- - Behavior can change between releases
10
+ - **Zero runtime dependencies** sub-5KB bundle, safe for serverless and edge runtimes.
11
+ - **Works with any MCP runtime** — [Skybridge](https://github.com/alpic-ai/skybridge), [`@modelcontextprotocol/sdk`](https://www.npmjs.com/package/@modelcontextprotocol/sdk), [`@vercel/mcp-handler`](https://www.npmjs.com/package/@vercel/mcp-handler).
12
+ - **Fully typed** — Zod-powered state schemas, inferred node contexts, typed event properties.
13
+ - **Automatic tool tracking** — one line wraps your server and every tool call ships to your dashboard.
14
+ - **LangGraph-inspired flows** — compile a state graph into a single MCP tool that drives multi-turn conversations.
11
15
 
12
- ## Installation
16
+ > **Status:** pre-alpha. APIs and behaviour may change between releases — pin versions in production.
17
+
18
+ ## Install
13
19
 
14
20
  ```bash
15
- npm install @waniwani
21
+ npm install @waniwani/sdk
22
+ # or
23
+ pnpm add @waniwani/sdk
24
+ # or
25
+ bun add @waniwani/sdk
16
26
  ```
17
27
 
18
- ## Quick Start
28
+ Requires Node 18.17+ and an MCP server runtime.
19
29
 
20
- ```typescript
21
- import { waniwani } from "@waniwani";
30
+ ## Quick start
22
31
 
23
- const client = waniwani({
24
- apiKey: "your-api-key", // or WANIWANI_API_KEY
25
- });
32
+ ### 1. Get an API key
26
33
 
27
- const { eventId } = await client.track({
28
- event: "tool.called",
29
- properties: { name: "pricing", type: "pricing" },
30
- meta: extra._meta,
31
- });
34
+ Sign in to [app.waniwani.ai](https://app.waniwani.ai), create an MCP environment, and copy its API key. Expose it to your server as `WANIWANI_API_KEY`:
32
35
 
33
- await client.flush();
36
+ ```bash
37
+ # .env
38
+ WANIWANI_API_KEY=ww_live_...
34
39
  ```
35
40
 
36
- ## Events API V2
37
-
38
- New SDK versions send tracking data to **V2 only**:
41
+ ### 2. Wrap your MCP server
39
42
 
40
- - Endpoint: `POST /api/mcp/events/v2/batch`
41
- - Transport: buffered batching with immediate scheduling, interval flush, size-threshold flush
42
- - Resilience: retry/backoff on transient failures, permanent stop on auth failures
43
+ ```ts
44
+ import { waniwani } from "@waniwani/sdk";
45
+ import { withWaniwani } from "@waniwani/sdk/mcp";
46
+ import { McpServer } from "skybridge/server";
47
+ import "dotenv/config";
43
48
 
44
- Legacy `track()` input shapes remain supported and are mapped internally to canonical V2 events.
49
+ const server = new McpServer(
50
+ { name: "my-mcp-app", version: "0.0.1" },
51
+ { capabilities: {} },
52
+ );
45
53
 
46
- ## API
54
+ server.registerTool(/* ... your tools ... */);
47
55
 
48
- ### `waniwani(config?)`
56
+ // One line — every registered tool is now tracked automatically.
57
+ withWaniwani(server, { client: waniwani() });
49
58
 
50
- ```typescript
51
- const client = waniwani({
52
- apiKey: "...", // defaults to WANIWANI_API_KEY env var
53
- baseUrl: "...", // defaults to https://app.waniwani.ai
54
- tracking: {
55
- endpointPath: "/api/mcp/events/v2/batch",
56
- flushIntervalMs: 1000,
57
- maxBatchSize: 20,
58
- maxBufferSize: 1000,
59
- maxRetries: 3,
60
- retryBaseDelayMs: 200,
61
- retryMaxDelayMs: 2000,
62
- shutdownTimeoutMs: 2000,
63
- },
64
- });
59
+ server.run();
65
60
  ```
66
61
 
67
- ### `client.track(event)`
62
+ Every tool call produces a `tool.called` event with duration, status, input/output, and session correlation — all visible in your WaniWani dashboard within seconds.
63
+
64
+ ### 3. Track custom events
68
65
 
69
- Accepts modern and legacy shapes and returns `{ eventId: string }` immediately after enqueue.
66
+ ```ts
67
+ import { waniwani } from "@waniwani/sdk";
70
68
 
71
- Modern shape:
69
+ const wani = waniwani();
72
70
 
73
- ```typescript
74
- await client.track({
71
+ await wani.track({
75
72
  event: "quote.succeeded",
76
73
  properties: { amount: 99, currency: "USD" },
77
- meta: extra._meta,
78
- });
79
- ```
80
-
81
- Legacy-compatible shape:
82
-
83
- ```typescript
84
- await client.track({
85
- eventType: "tool.called",
86
- sessionId: "session-123",
87
- toolName: "pricing",
88
- toolType: "pricing",
89
- metadata: { source: "legacy" },
74
+ meta: extra._meta, // correlates the event with the current MCP session
90
75
  });
91
76
  ```
92
77
 
93
- ### `client.flush()`
78
+ ### 4. Build a flow
94
79
 
95
- Flushes buffered events.
80
+ Multi-turn conversations, compiled into a single MCP tool:
96
81
 
97
- ### `client.shutdown(options?)`
82
+ ```ts
83
+ import { createFlow, END, START } from "@waniwani/sdk/mcp";
84
+ import { z } from "zod";
98
85
 
99
- Flushes and stops transport. Returns:
100
-
101
- ```typescript
102
- { timedOut: boolean; pendingEvents: number }
86
+ export const onboardingFlow = createFlow({
87
+ id: "onboarding",
88
+ title: "User Onboarding",
89
+ description: "Use when a new user wants to get started.",
90
+ state: {
91
+ email: z.string().describe("Work email"),
92
+ useCase: z.string().describe("What they want to build"),
93
+ },
94
+ })
95
+ .addNode("ask_email", async ({ interrupt }) =>
96
+ interrupt({ email: { question: "What's your work email?" } }),
97
+ )
98
+ .addNode("ask_use_case", async ({ interrupt }) =>
99
+ interrupt({
100
+ useCase: {
101
+ question: "What do you want to build?",
102
+ suggestions: ["Analytics", "Support", "Lead capture"],
103
+ },
104
+ }),
105
+ )
106
+ .addEdge(START, "ask_email")
107
+ .addEdge("ask_email", "ask_use_case")
108
+ .addEdge("ask_use_case", END)
109
+ .compile();
110
+
111
+ await onboardingFlow.register(server);
103
112
  ```
104
113
 
105
- ## Event Types
106
-
107
- - `session.started`
108
- - `tool.called`
109
- - `quote.requested`
110
- - `quote.succeeded`
111
- - `quote.failed`
112
- - `link.clicked`
113
- - `purchase.completed`
114
-
115
- ## Declarative Event Tracking
116
-
117
- Track conversions and funnel steps without writing JavaScript — just add data attributes to your HTML elements.
118
-
119
- ### `data-ww-conversion`
114
+ The engine handles state persistence, resumption, branching, and validation. The model just calls one tool — everything else is managed server-side.
120
115
 
121
- Fires a conversion event on click. Format: `name key:value key:value ...`
116
+ ## Documentation
122
117
 
123
- ```html
124
- <button data-ww-conversion="purchase value:49.99 currency:EUR">Buy Now</button>
125
- <button data-ww-conversion="signup">Sign Up Free</button>
126
- ```
127
-
128
- | Token | Description |
129
- |-------|-------------|
130
- | First token | Conversion name (required) |
131
- | Any `key:value` | Included as event metadata |
132
-
133
- ### `data-ww-step`
118
+ Full product documentation lives at **[docs.waniwani.ai](https://docs.waniwani.ai)** (powered by Mintlify):
134
119
 
135
- Fires a funnel step event on click with an auto-incrementing sequence number. Format: `name key:value key:value ...`
120
+ - [Introduction](https://docs.waniwani.ai/introduction)
121
+ - [Quickstart](https://docs.waniwani.ai/quickstart)
122
+ - [Setup](https://docs.waniwani.ai/setup/installation)
123
+ - [Event Tracking](https://docs.waniwani.ai/tracking/overview)
124
+ - [Flows](https://docs.waniwani.ai/flows/overview)
136
125
 
137
- ```html
138
- <button data-ww-step="pricing">View Pricing</button>
139
- <button data-ww-step="select-plan plan:premium">Select Plan</button>
140
- <button data-ww-step="checkout">Checkout</button>
141
- ```
126
+ The same docs are also available in this repository under [`docs/`](./docs).
142
127
 
143
- Clicking these in order produces steps with `step_sequence` 1, 2, 3. Extra `key:value` pairs are included as event metadata.
128
+ ## What's inside the package
144
129
 
145
- Both attributes use `closest()` to walk up the DOM tree, so clicking a child element (e.g. an icon inside a button) works automatically.
130
+ | Entry point | What it gives you |
131
+ | ------------------------- | ------------------------------------------------------------------------------ |
132
+ | `@waniwani/sdk` | `waniwani()` client: event tracking, identify, flush, shutdown. |
133
+ | `@waniwani/sdk/mcp` | `withWaniwani`, `createFlow`, `createTool`, `createResource`, flow primitives. |
134
+ | `@waniwani/sdk/mcp/react` | React hooks for WaniWani-powered widgets. |
135
+ | `@waniwani/sdk/chat` | Chat UI components for embedding conversations. |
136
+ | `@waniwani/sdk/kb` | Knowledge base client. |
146
137
 
147
- ## Quality Gates
138
+ Most users only need `@waniwani/sdk` and `@waniwani/sdk/mcp`.
148
139
 
149
- Run from repo root:
140
+ ## Examples
150
141
 
151
- ```bash
152
- bun run typecheck && bun run lint && bun run build && bun run test
153
- ```
142
+ - **[Alpic x WaniWani demo](https://github.com/alpic-ai/apps-sdk-template)** — a Skybridge MCP server with a full `createFlow` booking journey.
154
143
 
155
- ## Verification and Contracts
144
+ ## Links
156
145
 
157
- - Manual playground flow: [`docs/playground-v2-manual-verification.md`](docs/playground-v2-manual-verification.md)
158
- - Events API V2 contract: [`docs/events-api-v2-contract.md`](docs/events-api-v2-contract.md)
159
- - Events table V2 proposal: [`docs/events-table-v2-schema.md`](docs/events-table-v2-schema.md)
160
- - Migration and release plan: [`docs/migration-v2-release-plan.md`](docs/migration-v2-release-plan.md)
146
+ - **Website** [waniwani.ai](https://waniwani.ai)
147
+ - **Dashboard** [app.waniwani.ai](https://app.waniwani.ai)
148
+ - **Docs** [docs.waniwani.ai](https://docs.waniwani.ai)
149
+ - **Issues** [github.com/WaniWani-AI/sdk/issues](https://github.com/WaniWani-AI/sdk/issues)
161
150
 
162
151
  ## License
163
152
 
164
- MIT
153
+ [MIT](./LICENSE) © WaniWani
@@ -1,9 +1,10 @@
1
- import { ContentBlock, CallToolResult } from '@modelcontextprotocol/sdk/types.js';
2
- import { McpServer, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
3
- export { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
4
- import { z } from 'zod';
1
+ import { ContentBlock, CallToolResult, ServerRequest, ServerNotification } from '@modelcontextprotocol/sdk/types.js';
5
2
  import { ZodRawShapeCompat, ShapeOutput } from '@modelcontextprotocol/sdk/server/zod-compat.js';
6
3
  export { ZodRawShapeCompat } from '@modelcontextprotocol/sdk/server/zod-compat.js';
4
+ import { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
5
+ import { z } from 'zod';
6
+ import { McpServer, ToolCallback } from '@modelcontextprotocol/sdk/server/mcp.js';
7
+ export { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
7
8
 
8
9
  /**
9
10
  * Widget platform types
@@ -800,6 +801,11 @@ type FlowConfig = {
800
801
  type InferFlowState<T extends Record<string, z.ZodType>> = {
801
802
  [K in keyof T]: z.infer<T[K]>;
802
803
  };
804
+ /**
805
+ * Flow tool handler — uses shared MCP types (`RequestHandlerExtra`, `CallToolResult`)
806
+ * so it's assignable to both MCP SDK's `ToolCallback` and Skybridge's `ToolHandler`.
807
+ */
808
+ type FlowToolHandler = (args: Record<string, unknown>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => CallToolResult | Promise<CallToolResult>;
803
809
  /**
804
810
  * A compiled flow — can be registered on an McpServer.
805
811
  *
@@ -813,7 +819,7 @@ type RegisteredFlow = {
813
819
  config: {
814
820
  title: string;
815
821
  description: string;
816
- inputSchema: Record<string, unknown>;
822
+ inputSchema: ZodRawShapeCompat;
817
823
  annotations?: {
818
824
  readOnlyHint?: boolean;
819
825
  idempotentHint?: boolean;
@@ -822,7 +828,7 @@ type RegisteredFlow = {
822
828
  };
823
829
  };
824
830
  /** Tool callback — pass to `server.registerTool(flow.name, flow.config, flow.handler)`. */
825
- handler: ToolCallback;
831
+ handler: FlowToolHandler;
826
832
  /** Register this flow on an MCP server. Shorthand for `server.registerTool(flow.name, flow.config, flow.handler)`. */
827
833
  register: (server: McpServer) => Promise<void>;
828
834
  /** Returns a Mermaid `flowchart TD` diagram of the flow graph. */
package/dist/mcp/index.js CHANGED
@@ -1,7 +1,7 @@
1
- function Z(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Ge(){return Z()==="openai"}function Je(){return Z()==="mcp-apps"}var I="__start__",b="__end__",ge=Symbol.for("waniwani.flow.interrupt"),me=Symbol.for("waniwani.flow.widget");function we(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:ge,questions:r,context:n}}function he(e,t){return{__type:me,tool:typeof e=="string"?e:e.id,...t}}function ye(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ge}function Te(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===me}import{z as K}from"zod";var Q="waniwani/client";function $(e){if(typeof e=="object"&&e!==null)return e[Q]}function ke(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 Xe=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],Ze=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],Qe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],et=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],tt=["correlationId","openai/requestId"];function P(e){return e?D(e,Xe):void 0}function Se(e){return e?D(e,Ze):void 0}function ve(e){return e?D(e,Qe):void 0}function xe(e){return e?D(e,et):void 0}function Re(e){return e?D(e,tt):void 0}var nt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function W(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 nt){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function Ee(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function U(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 rt(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 Ie(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 ee(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?rt(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 te(e){return e!=null&&e!==""}async function F(e,t){return e.type==="direct"?e.to:e.condition(t)}function Ce(e,t,n,r){if(e.every(c=>te(U(r,c.field))))return null;let o=e.filter(c=>!te(U(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 B(e,t,n,r,o,s,a){let i=e,c={...t},d=50,f=0;for(;f++<d;){if(i===b)return{content:{status:"complete"},flowTokenContent:{state:c}};let u=n.get(i);if(!u)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let g=await u({state:c,meta:s,interrupt:we,showWidget:he,waniwani:a});if(ye(g)){for(let y of g.questions)y.validate&&o.set(`${i}:${y.field}`,y.validate);let h=Ce(g.questions,g.context,i,c);if(h)return h;for(let y of g.questions){let v=o.get(`${i}:${y.field}`);if(v)try{let k=U(c,y.field),x=await v(k);x&&typeof x=="object"&&(c=M(c,x))}catch(k){let x=k instanceof Error?k.message:String(k);Ie(c,y.field);let S=g.questions.map(C=>C.field===y.field?{...C,context:C.context?`ERROR: ${x}
1
+ function X(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function Ge(){return X()==="openai"}function Je(){return X()==="mcp-apps"}var I="__start__",b="__end__",ge=Symbol.for("waniwani.flow.interrupt"),me=Symbol.for("waniwani.flow.widget");function we(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:ge,questions:r,context:n}}function he(e,t){return{__type:me,tool:typeof e=="string"?e:e.id,...t}}function ye(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===ge}function Te(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===me}import{z as K}from"zod";var Q="waniwani/client";function $(e){if(typeof e=="object"&&e!==null)return e[Q]}function ke(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 Ze=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","anthropic/sessionId"],Xe=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],Qe=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],et=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],tt=["correlationId","openai/requestId"];function P(e){return e?D(e,Ze):void 0}function Se(e){return e?D(e,Xe):void 0}function ve(e){return e?D(e,Qe):void 0}function xe(e){return e?D(e,et):void 0}function Re(e){return e?D(e,tt):void 0}var nt=[{key:"waniwani/sessionId",source:"chatbar"},{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"},{key:"anthropic/sessionId",source:"claude"}];function W(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 nt){let o=e[n];if(typeof o=="string"&&o.length>0)return r}}function Ee(e){let t=e._zod?.def;return t?.type==="object"&&t.shape?t.shape:null}function U(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 rt(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 Ie(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 ee(e){let t={};for(let[n,r]of Object.entries(e))n.includes(".")?rt(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 te(e){return e!=null&&e!==""}async function F(e,t){return e.type==="direct"?e.to:e.condition(t)}function Ce(e,t,n,r){if(e.every(c=>te(U(r,c.field))))return null;let o=e.filter(c=>!te(U(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 B(e,t,n,r,o,s,a){let i=e,c={...t},d=50,f=0;for(;f++<d;){if(i===b)return{content:{status:"complete"},flowTokenContent:{state:c}};let u=n.get(i);if(!u)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let g=await u({state:c,meta:s,interrupt:we,showWidget:he,waniwani:a});if(ye(g)){for(let y of g.questions)y.validate&&o.set(`${i}:${y.field}`,y.validate);let h=Ce(g.questions,g.context,i,c);if(h)return h;for(let y of g.questions){let v=o.get(`${i}:${y.field}`);if(v)try{let k=U(c,y.field),x=await v(k);x&&typeof x=="object"&&(c=M(c,x))}catch(k){let x=k instanceof Error?k.message:String(k);Ie(c,y.field);let S=g.questions.map(C=>C.field===y.field?{...C,context:C.context?`ERROR: ${x}
2
2
 
3
3
  ${C.context}`:`ERROR: ${x}`}:C),_=Ce(S,g.context,i,c);if(_)return _;break}}let p=r.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(p,c);continue}if(Te(g)){let h=g.field;if(h&&te(U(c,h))){let p=r.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(p,c);continue}return{content:{status:"widget",tool:g.tool,data:g.data,description:g.description,interactive:g.interactive!==!1},flowTokenContent:{step:i,state:c,field:h,widgetId:g.tool}}}c=M(c,g);let T=r.get(i);if(!T)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await F(T,c)}catch(l){return{content:{status:"error",error:l instanceof Error?l.message:String(l)},flowTokenContent:{step:i,state:c}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var ot="@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}async get(t){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");return await this.request("/api/mcp/redis/get",{key:t})??null}async set(t,n){if(!this.apiKey)throw new Error("[WaniWani KV] No API key configured. Set WANIWANI_API_KEY env var.");await this.request("/api/mcp/redis/set",{key:t,value:n})}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":ot},body:JSON.stringify(n)});if(!o.ok){let a=await o.text().catch(()=>"");throw new Error(a||`KV store API error: HTTP ${o.status}`)}return(await o.json()).data}};var O=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 be(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 _e(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,"," including relevant prior context that led to triggering it, if available."," 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 s=Ee(o);if(s){let a=o.description??"",i=Object.entries(s).map(([c,d])=>{let f=be(d);return f?`\`${r}.${c}\` (${f})`:`\`${r}.${c}\``}).join(", ");n.push(a?`\`${r}\` (${a}): ${i}`:`\`${r}\`: ${i}`)}else{let a=be(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
4
  `)}var st={action:K.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),intent:K.string().optional().describe(`Required when action is "start". Provide a brief summary of the user's goal for this flow, including relevant prior context that led to triggering it, if available. Do not invent missing context.`),stateUpdates:K.record(K.string(),K.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 Pe(e){let{config:t,nodes:n,edges:r}=e,o=_e(t),s=`${t.description}
5
5
  ${o}`,a=e.store??new O,i=new Map;async function c(u,l,g,T){if(u.action==="start"){let h=typeof u.intent=="string"?u.intent.trim():void 0;if(!h)return{content:{status:"error",error:`Missing required "intent" for action "start". Include a brief summary of the user's goal for this flow and any relevant prior context that led to triggering it, if available.`}};u.intent=h;let p=r.get(I);if(!p)return{content:{status:"error",error:"No start edge"}};let y=ee(u.stateUpdates??{}),v=await F(p,y);return B(v,y,n,r,i,g,T)}if(u.action==="continue"){if(!l)return{content:{status:"error",error:"No session ID available for continue action."}};let h;try{h=await a.get(l)}catch(k){let x=k instanceof Error?k.message:String(k);return{content:{status:"error",error:`Failed to load flow state (session "${l}"): ${x}`}}}if(!h)return{content:{status:"error",error:`Flow state not found for session "${l}". The flow may have expired.`}};let p=h.state,y=h.step;if(!y)return{content:{status:"error",error:'This flow has already completed. Use action "start" to begin a new flow.'}};let v=M(p,ee(u.stateUpdates??{}));if(h.widgetId){let k=r.get(y);if(!k)return{content:{status:"error",error:`No edge from step "${y}"`}};let x=await F(k,v);return B(x,v,n,r,i,g,T)}return B(y,v,n,r,i,g,T)}return{content:{status:"error",error:`Unknown action: "${u.action}"`}}}let d={title:t.title,description:s,inputSchema:st,annotations:t.annotations},f=(async(u,l)=>{let g=l,T=g._meta??{},h=P(T),p=$(g),y=await c(u,h,T,p);if(h)if(y.flowTokenContent&&y.content.status!=="complete")try{await a.set(h,y.flowTokenContent)}catch(k){let x=k instanceof Error?k.message:String(k);return{content:[{type:"text",text:JSON.stringify({status:"error",error:`Flow state failed to persist (session "${h}"): ${x}`},null,2)}],_meta:T,isError:!0}}else y.content.status==="complete"&&await a.delete(h);return{content:[{type:"text",text:JSON.stringify(y.content,null,2)}],_meta:T,...y.content.status==="error"?{isError:!0}:{}}});return{name:t.id,config:d,handler:f,async register(u){u.registerTool(t.id,d,f)},graph:e.graph}}function We(e,t){let n=["flowchart TD"];n.push(` ${I}((Start))`);for(let[r]of e)n.push(` ${r}[${r}]`);n.push(` ${b}((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;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===b)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 We(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Pe({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>We(n,r)})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!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 Me(e){return new N(e)}function Fe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ae(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.name}" 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,f){let u=await a({action:"start",intent:d,...f?{stateUpdates:f}:{}},i);return c(Fe(u))},async continueWith(d){let f=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(Fe(f))},async lastState(){return n?n.get(o):null}}}var q="text/html+skybridge",L="text/html;profile=mcp-app",Ne=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function De(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Ue(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 ne(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 Oe(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:p}=new URL(o);(p==="localhost"||p==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${p}:*`,`wss://${p}:*`],resource_domains:[...d.resource_domains||[],`http://${p}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,l=null,g=()=>(l||(l=Ne(o,s)),l),T=r;async function h(p){let y=await g();p.registerResource(`${t}-openai-widget`,f,{title:n,description:T,mimeType:q,_meta:{"openai/widgetDescription":T,"openai/widgetPrefersBorder":i}},async v=>({contents:[{uri:v.href,mimeType:q,text:y,_meta:De({description:T,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),p.registerResource(`${t}-mcp-widget`,u,{title:n,description:T,mimeType:L,_meta:{ui:{prefersBorder:i}}},async v=>({contents:[{uri:v.href,mimeType:L,text:y,_meta:Ue({description:T,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:u,autoHeight:c,register:h}}function at(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?ne({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:c,description:r,async register(f){f.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(u,l)=>{let g=l,T=g._meta??{},h=$(g),p=await t(u,{extra:{_meta:T},waniwani:h});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...d,...T,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:p.text}],...p.data?{structuredContent:p.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ct(e,t){await Promise.all(t.map(n=>n.register(e)))}var V=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var dt="@waniwani/sdk";function Ke(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}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":dt},u={method:s,headers:f};i!==void 0&&(f["Content-Type"]="application/json",u.body=JSON.stringify(i));let l=await fetch(d,u);if(!l.ok){let T=await l.text().catch(()=>"");throw new V(T||`KB API error: HTTP ${l.status}`,l.status)}return(await l.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 ut="@waniwani/sdk";function z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??je,o=ft(e),s=H(e.meta),a=H(e.metadata),i=gt(e,s),c=E(e.eventId)??r(),d=mt(e.timestamp,n),f=E(e.source)??W(s)??t.source??ut,u=re(e)?{...e}:void 0,l={...a};return Object.keys(s).length>0&&(l.meta=s),u&&(l.rawLegacy=u),{id:c,type:"mcp.event",name:o,source:f,timestamp:d,correlation:i,properties:pt(e,o),metadata:l,rawLegacy:u}}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 pt(e,t){if(!re(e))return H(e.properties);let n=lt(e,t),r=H(e.properties);return{...n,...r}}function lt(e,t){switch(t){case"tool.called":{let n={};return E(e.toolName)&&(n.name=e.toolName),E(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),E(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return E(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),E(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function ft(e){return re(e)?e.eventType:e.event}function gt(e,t){let n=E(e.requestId)??Se(t),r=E(e.sessionId)??P(t),o=E(e.traceId)??ve(t),s=E(e.externalUserId)??xe(t),a=E(e.correlationId)??Re(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 mt(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 E(e){if(typeof e=="string"&&e.trim().length!==0)return e}function re(e){return"eventType"in e}var wt="/api/mcp/events/v2/batch";var $e="@waniwani/sdk",ht=new Set([401,403]),yt=new Set([408,425,429,500,502,503,504]);function Be(e){return new oe(e)}var oe=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=St(t.apiUrl,t.endpointPath??wt),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":$e},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:vt(s)}}if(ht.has(n.status))return{kind:"auth",status:n.status};if(yt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await kt(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:$e,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(Tt(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 Tt(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 kt(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function St(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 qe(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?Be({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 f=z({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(f),{eventId:f.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&&xt(a,r.shutdownTimeoutMs),a}function xt(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 Y(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},s={apiUrl:n,apiKey:r,tracking:o},a=qe(s),i=Ke(s);return{...a,kb:i,_config:s}}function Rt(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 Et(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Y(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 f=Rt(d),u=await i.track(f);c.push(u.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"}})}}}function G(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var ie=G("widget-token",!!process.env.WANIWANI_DEBUG),It=120*1e3,J=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-It?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=Ct(this.config.apiUrl,"/api/mcp/widget-tokens");ie("minting token from",r);let 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),signal:AbortSignal.timeout(5e3)});if(ie("mint response:",s.status),!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(s){return ie("mint failed:",s),null}}};function Ct(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Le="waniwani/sessionId",se="waniwani/geoLocation",ae="waniwani/userLocation";function R(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function j(e){if(!R(e))return;let t=e._meta;return R(t)?t:void 0}function ce(e){if(!R(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>R(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function bt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function de(e,t,n,r,o,s){let a=bt(e,n.toolType),i=j(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",W(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:W(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ue(e,t,n){try{await e.track(t)}catch(r){n?.(le(r))}}async function pe(e,t){try{await e.flush()}catch(n){t?.(le(n))}}async function Ve(e,t,n,r,o){if(!R(e))return;R(e._meta)||(e._meta={});let s=e._meta,a=R(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(i.token=u)}catch(u){o?.(le(u))}let c=P(s);c&&(i.sessionId||(i.sessionId=c));let d=ze(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let f=W(j(r));f&&!i.source&&(i.source=f),s.waniwani=i}function He(e,t){let n=j(t);if(!n||!R(e))return;R(e._meta)||(e._meta={});let r=e._meta,o=P(n);o&&!r[Le]&&(r[Le]=o);let s=ze(n);s&&(r[se]||(r[se]=s),r[ae]||(r[ae]=s))}function ze(e){if(!e)return;let t=e[se]??e[ae];if(R(t)||typeof t=="string")return t}function le(e){return e instanceof Error?e:new Error(String(e))}var X=G("mcp",!!process.env.WANIWANI_DEBUG),Ye="https://app.waniwani.ai";function _t(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Y(),s=r.injectWidgetToken!==!1,a=o._config.apiKey?new J({apiUrl:o._config.apiUrl??Ye,apiKey:o._config.apiKey}):null,i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,f,u]=c,l=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof u!="function")return i(...c);let g=u;return i(d,f,async(h,p)=>{let y=j(p)??{},v=ke(o,y,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});R(p)&&(p[Q]=v);let k=performance.now(),x=e.server?.getClientVersion?.();try{let S=await g(h,p),_=Math.round(performance.now()-k);X(`tool "${l}" handler returned in ${_}ms, running post-processing...`);let C=R(S)&&S.isError===!0;if(C){let fe=ce(S);console.error(`[waniwani] Tool "${l}" returned error${fe?`: ${fe}`:""}`)}return await ue(o,de(l,p,r,{durationMs:_,status:C?"error":"ok",...C&&{errorMessage:ce(S)??"Unknown tool error"}},x,{input:h,output:S}),r.onError),X(`tool "${l}" tracking done`),r.flushAfterToolCall&&await pe(o,r.onError),He(S,p),s&&(await Ve(S,a,o._config.apiUrl??Ye,p,r.onError),X(`tool "${l}" widget config injected`)),X(`tool "${l}" post-processing complete, returning result`),S}catch(S){let _=Math.round(performance.now()-k);throw await ue(o,de(l,p,r,{durationMs:_,status:"error",errorMessage:S instanceof Error?S.message:String(S)},x,{input:h}),r.onError),r.flushAfterToolCall&&await pe(o,r.onError),S}})}),n}export{b as END,I as START,N as StateGraph,A as WaniwaniKvStore,Me as createFlow,Ae as createFlowTestHarness,Oe as createResource,at as createTool,Et as createTrackingRoute,Z as detectPlatform,Je as isMCPApps,Ge as isOpenAI,ct as registerTools,_t as withWaniwani};
6
+ `)}var N=class{nodes=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n){if(t===I||t===b)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 We(this.nodes,this.edges)}compile(t){this.validate();let n=new Map(this.nodes),r=new Map(this.edges);return Pe({config:this.config,nodes:n,edges:r,store:t?.store,graph:()=>We(n,r)})}validate(){if(!this.edges.has(I))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(I);if(t?.type==="direct"&&t.to!==b&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==I&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==b&&!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 Me(e){return new N(e)}function Fe(e){let t=e.content;return JSON.parse(t[0]?.text??"")}async function Ae(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.name}" 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,f){let u=await a({action:"start",intent:d,...f?{stateUpdates:f}:{}},i);return c(Fe(u))},async continueWith(d){let f=await a({action:"continue",...d?{stateUpdates:d}:{}},i);return c(Fe(f))},async lastState(){return n?n.get(o):null}}}var q="text/html+skybridge",L="text/html;profile=mcp-app",Ne=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function De(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function Ue(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 ne(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 Oe(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:p}=new URL(o);(p==="localhost"||p==="127.0.0.1")&&(d={...d,connect_domains:[...d.connect_domains||[],`ws://${p}:*`,`wss://${p}:*`],resource_domains:[...d.resource_domains||[],`http://${p}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,u=`ui://widgets/ext-apps/${t}.html`,l=null,g=()=>(l||(l=Ne(o,s)),l),T=r;async function h(p){let y=await g();p.registerResource(`${t}-openai-widget`,f,{title:n,description:T,mimeType:q,_meta:{"openai/widgetDescription":T,"openai/widgetPrefersBorder":i}},async v=>({contents:[{uri:v.href,mimeType:q,text:y,_meta:De({description:T,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]})),p.registerResource(`${t}-mcp-widget`,u,{title:n,description:T,mimeType:L,_meta:{ui:{prefersBorder:i}}},async v=>({contents:[{uri:v.href,mimeType:L,text:y,_meta:Ue({description:T,prefersBorder:i,widgetDomain:a,widgetCSP:d})}]}))}return{id:t,title:n,description:r,openaiUri:f,mcpUri:u,autoHeight:c,register:h}}function at(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?ne({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:c,description:r,async register(f){f.registerTool(i,{title:c,description:r,inputSchema:o,annotations:s,...d&&{_meta:d}},(async(u,l)=>{let g=l,T=g._meta??{},h=$(g),p=await t(u,{extra:{_meta:T},waniwani:h});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...d,...T,...a===!1?{"waniwani/autoInjectResultText":!1}:{}}}:{content:[{type:"text",text:p.text}],...p.data?{structuredContent:p.data}:{},...a===!1?{_meta:{"waniwani/autoInjectResultText":!1}}:{}}}))}}}async function ct(e,t){await Promise.all(t.map(n=>n.register(e)))}var H=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var dt="@waniwani/sdk";function Ke(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}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":dt},u={method:s,headers:f};i!==void 0&&(f["Content-Type"]="application/json",u.body=JSON.stringify(i));let l=await fetch(d,u);if(!l.ok){let T=await l.text().catch(()=>"");throw new H(T||`KB API error: HTTP ${l.status}`,l.status)}return(await l.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 ut="@waniwani/sdk";function z(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??je,o=ft(e),s=V(e.meta),a=V(e.metadata),i=gt(e,s),c=E(e.eventId)??r(),d=mt(e.timestamp,n),f=E(e.source)??W(s)??t.source??ut,u=re(e)?{...e}:void 0,l={...a};return Object.keys(s).length>0&&(l.meta=s),u&&(l.rawLegacy=u),{id:c,type:"mcp.event",name:o,source:f,timestamp:d,correlation:i,properties:pt(e,o),metadata:l,rawLegacy:u}}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 pt(e,t){if(!re(e))return V(e.properties);let n=lt(e,t),r=V(e.properties);return{...n,...r}}function lt(e,t){switch(t){case"tool.called":{let n={};return E(e.toolName)&&(n.name=e.toolName),E(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),E(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return E(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),E(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function ft(e){return re(e)?e.eventType:e.event}function gt(e,t){let n=E(e.requestId)??Se(t),r=E(e.sessionId)??P(t),o=E(e.traceId)??ve(t),s=E(e.externalUserId)??xe(t),a=E(e.correlationId)??Re(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 mt(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 E(e){if(typeof e=="string"&&e.trim().length!==0)return e}function re(e){return"eventType"in e}var wt="/api/mcp/events/v2/batch";var $e="@waniwani/sdk",ht=new Set([401,403]),yt=new Set([408,425,429,500,502,503,504]);function Be(e){return new oe(e)}var oe=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=St(t.apiUrl,t.endpointPath??wt),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":$e},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:vt(s)}}if(ht.has(n.status))return{kind:"auth",status:n.status};if(yt.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await kt(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:$e,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),o=[],s=[];for(let a of n){let i=r.get(a.eventId);if(i){if(Tt(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 Tt(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 kt(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function St(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 qe(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?Be({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 f=z({event:"user.identified",externalUserId:i,properties:c,meta:d});return s?.enqueue(f),{eventId:f.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&&xt(a,r.shutdownTimeoutMs),a}function xt(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 Y(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},s={apiUrl:n,apiKey:r,tracking:o},a=qe(s),i=Ke(s);return{...a,kb:i,_config:s}}function Rt(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 Et(e){let t={apiKey:e?.apiKey,apiUrl:e?.apiUrl},n;function r(){return n||(n=Y(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 f=Rt(d),u=await i.track(f);c.push(u.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"}})}}}function G(e,t){return t?(...n)=>console.log(`[waniwani:${e}]`,...n):()=>{}}var ie=G("widget-token",!!process.env.WANIWANI_DEBUG),It=120*1e3,J=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-It?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=Ct(this.config.apiUrl,"/api/mcp/widget-tokens");ie("minting token from",r);let 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),signal:AbortSignal.timeout(5e3)});if(ie("mint response:",s.status),!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(s){return ie("mint failed:",s),null}}};function Ct(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var Le="waniwani/sessionId",se="waniwani/geoLocation",ae="waniwani/userLocation";function R(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}function j(e){if(!R(e))return;let t=e._meta;return R(t)?t:void 0}function ce(e){if(!R(e))return;let t=e.content;return Array.isArray(t)?t.find(r=>R(r)&&r.type==="text"&&typeof r.text=="string")?.text:void 0}function bt(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function de(e,t,n,r,o,s){let a=bt(e,n.toolType),i=j(t);return console.log("[waniwani:debug] buildTrackInput meta:",JSON.stringify(i),"-> source:",W(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:W(i),metadata:{...n.metadata??{},...o&&{clientInfo:o}}}}async function ue(e,t,n){try{await e.track(t)}catch(r){n?.(le(r))}}async function pe(e,t){try{await e.flush()}catch(n){t?.(le(n))}}async function He(e,t,n,r,o){if(!R(e))return;R(e._meta)||(e._meta={});let s=e._meta,a=R(s.waniwani)?s.waniwani:void 0,i={...a??{},endpoint:a?.endpoint??`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let u=await t.getToken();u&&(i.token=u)}catch(u){o?.(le(u))}let c=P(s);c&&(i.sessionId||(i.sessionId=c));let d=ze(s);d!==void 0&&(i.geoLocation||(i.geoLocation=d));let f=W(j(r));f&&!i.source&&(i.source=f),s.waniwani=i}function Ve(e,t){let n=j(t);if(!n||!R(e))return;R(e._meta)||(e._meta={});let r=e._meta,o=P(n);o&&!r[Le]&&(r[Le]=o);let s=ze(n);s&&(r[se]||(r[se]=s),r[ae]||(r[ae]=s))}function ze(e){if(!e)return;let t=e[se]??e[ae];if(R(t)||typeof t=="string")return t}function le(e){return e instanceof Error?e:new Error(String(e))}var Z=G("mcp",!!process.env.WANIWANI_DEBUG),Ye="https://app.waniwani.ai";function _t(e,t){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t??{},o=r.client??Y(),s=r.injectWidgetToken!==!1,a=o._config.apiKey?new J({apiUrl:o._config.apiUrl??Ye,apiKey:o._config.apiKey}):null,i=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[d,f,u]=c,l=typeof d=="string"&&d.trim().length>0?d:"unknown";if(typeof u!="function")return i(...c);let g=u;return i(d,f,async(h,p)=>{let y=j(p)??{},v=ke(o,y,{apiUrl:o._config.apiUrl,apiKey:o._config.apiKey});R(p)&&(p[Q]=v);let k=performance.now(),x=e.server?.getClientVersion?.();try{let S=await g(h,p),_=Math.round(performance.now()-k);Z(`tool "${l}" handler returned in ${_}ms, running post-processing...`);let C=R(S)&&S.isError===!0;if(C){let fe=ce(S);console.error(`[waniwani] Tool "${l}" returned error${fe?`: ${fe}`:""}`)}return await ue(o,de(l,p,r,{durationMs:_,status:C?"error":"ok",...C&&{errorMessage:ce(S)??"Unknown tool error"}},x,{input:h,output:S}),r.onError),Z(`tool "${l}" tracking done`),r.flushAfterToolCall&&await pe(o,r.onError),Ve(S,p),s&&(await He(S,a,o._config.apiUrl??Ye,p,r.onError),Z(`tool "${l}" widget config injected`)),Z(`tool "${l}" post-processing complete, returning result`),S}catch(S){let _=Math.round(performance.now()-k);throw await ue(o,de(l,p,r,{durationMs:_,status:"error",errorMessage:S instanceof Error?S.message:String(S)},x,{input:h}),r.onError),r.flushAfterToolCall&&await pe(o,r.onError),S}})}),n}export{b as END,I as START,N as StateGraph,A as WaniwaniKvStore,Me as createFlow,Ae as createFlowTestHarness,Oe as createResource,at as createTool,Et as createTrackingRoute,X as detectPlatform,Je as isMCPApps,Ge as isOpenAI,ct as registerTools,_t as withWaniwani};
7
7
  //# sourceMappingURL=index.js.map