@waniwani/sdk 0.9.3 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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
@@ -320,11 +320,6 @@ interface NextJsHandlerOptions {
320
320
  * Logs request details, response codes, resolved URLs, and caught errors.
321
321
  */
322
322
  debug?: boolean;
323
- /**
324
- * Additional origins allowed for cross-origin requests.
325
- * The WaniWani platform URL is always included by default.
326
- */
327
- allowedOrigins?: string[];
328
323
  }
329
324
  interface NextJsHandlerResult {
330
325
  /** GET handler: routes sub-paths (e.g. /resource for MCP widget content) */
@@ -1,10 +1,10 @@
1
- function A(t,e){return e?(...n)=>console.log(`[waniwani:${t}]`,...n):()=>{}}function I(t){if(t===!0)return{};if(!(t===!1||t===void 0))return t}function W(t){let e=new Set(t.map(n=>n.replace(/\/$/,"").toLowerCase()));return function(r,i){let s=i?.headers.get("origin"),g=s?.toLowerCase();return g!=null&&(e.has(g)||g.endsWith(".mcp.waniwani.run"))&&(r.headers.set("Access-Control-Allow-Origin",s),r.headers.set("Access-Control-Allow-Methods","GET, POST, OPTIONS"),r.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),r.headers.set("Access-Control-Expose-Headers","X-Session-Id"),r.headers.set("Vary","Origin")),r}}function N(t){return function(n,r,i){return t(new Response(JSON.stringify(n),{headers:{"Content-Type":"application/json"},status:r}),i)}}var U=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};function L(t){let e=t.headers,n=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,r=n?V(n):void 0,i=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,s=e.get("x-vercel-ip-country-region")??void 0,g=e.get("x-vercel-ip-latitude")??e.get("cf-iplatitude")??void 0,c=e.get("x-vercel-ip-longitude")??e.get("cf-iplongitude")??void 0,f=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,d=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:r,country:i,countryRegion:s,latitude:g,longitude:c,timezone:f,ip:d}}function V(t){try{return decodeURIComponent(t)}catch{return t}}function E(t){if(!t)return!1;let e=Array.isArray(t.content)&&t.content.length>0,n=typeof t.structuredContent=="object"&&t.structuredContent!==null&&Object.keys(t.structuredContent).length>0;return e||n}function q(t){if(!E(t))return"";let e=["## Widget Model Context","This hidden context was supplied by an MCP App via `ui/update-model-context`.","Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly."];if(t.content?.length){let n=t.content.map(r=>r.type==="text"&&typeof r.text=="string"?r.text.trim():JSON.stringify(r,null,2)).filter(Boolean).join(`
1
+ function H(t,e){return e?(...n)=>console.log(`[waniwani:${t}]`,...n):()=>{}}function O(t){if(t===!0)return{};if(!(t===!1||t===void 0))return t}function W(){return function(e){return e.headers.set("Access-Control-Allow-Origin","*"),e.headers.set("Access-Control-Allow-Methods","GET, POST, OPTIONS"),e.headers.set("Access-Control-Allow-Headers","Content-Type, Authorization, X-Session-Id, X-Client-User-Agent"),e.headers.set("Access-Control-Expose-Headers","X-Session-Id"),e}}function N(t){return function(n,o){return t(new Response(JSON.stringify(n),{headers:{"Content-Type":"application/json"},status:o}))}}var v=class extends Error{constructor(n,o){super(n);this.status=o;this.name="WaniWaniError"}};function q(t){let e=t.headers,n=e.get("x-vercel-ip-city")??e.get("cf-ipcity")??void 0,o=n?z(n):void 0,i=e.get("x-vercel-ip-country")??e.get("cf-ipcountry")??void 0,r=e.get("x-vercel-ip-country-region")??void 0,x=e.get("x-vercel-ip-latitude")??e.get("cf-iplatitude")??void 0,a=e.get("x-vercel-ip-longitude")??e.get("cf-iplongitude")??void 0,l=e.get("x-vercel-ip-timezone")??e.get("cf-iptimezone")??void 0,m=e.get("x-real-ip")??e.get("x-forwarded-for")?.split(",")[0]?.trim()??e.get("cf-connecting-ip")??void 0;return{city:o,country:i,countryRegion:r,latitude:x,longitude:a,timezone:l,ip:m}}function z(t){try{return decodeURIComponent(t)}catch{return t}}function E(t){if(!t)return!1;let e=Array.isArray(t.content)&&t.content.length>0,n=typeof t.structuredContent=="object"&&t.structuredContent!==null&&Object.keys(t.structuredContent).length>0;return e||n}function B(t){if(!E(t))return"";let e=["## Widget Model Context","This hidden context was supplied by an MCP App via `ui/update-model-context`.","Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly."];if(t.content?.length){let n=t.content.map(o=>o.type==="text"&&typeof o.text=="string"?o.text.trim():JSON.stringify(o,null,2)).filter(Boolean).join(`
2
2
 
3
3
  `);n&&e.push(`Content blocks:
4
4
  ${n}`)}return t.structuredContent&&Object.keys(t.structuredContent).length>0&&e.push(`Structured content JSON:
5
5
  ${JSON.stringify(t.structuredContent,null,2)}`),e.join(`
6
6
 
7
- `)}function B(t,e){if(!E(e))return t;let n=q(e);return n?[t,n].filter(Boolean).join(`
7
+ `)}function L(t,e){if(!E(e))return t;let n=B(e);return n?[t,n].filter(Boolean).join(`
8
8
 
9
- `):t}function J(t){let{apiKey:e,apiUrl:n,source:r,systemPrompt:i,maxSteps:s,beforeRequest:g,mcpServerUrl:c,resolveConfig:f,debug:d,webSearch:b}=t,o=A("chat",d);return async function(a){o("\u2192 POST",a.url);try{let p=await a.json(),S=p.messages??[],y=p.sessionId,x=p.modelContext,R=i,k=p.visitorContext??null,H=L(a),j={geo:H,client:k};if(o("body parsed \u2014 messages:",S.length,"sessionId:",y??"(none)","geo:",JSON.stringify(H)),g){o("running beforeRequest hook");try{let u=await g({messages:S,sessionId:y,modelContext:x,request:a,visitor:j});u&&(u.messages&&(S=u.messages),u.systemPrompt!==void 0&&(R=u.systemPrompt),u.sessionId!==void 0&&(y=u.sessionId),u.modelContext!==void 0&&(x=u.modelContext)),o("beforeRequest hook done \u2014 messages:",S.length,"sessionId:",y??"(none)")}catch(u){console.error("[waniwani:chat] beforeRequest hook error:",u);let O=u instanceof U?u.status:400,F=u instanceof Error?u.message:"Request rejected";return o("\u2190 returning",O,"from hook error"),Response.json({error:F},{status:O})}}let l=c??(await f()).mcpServerUrl;o("mcpServerUrl:",l),R=B(R,x);let h=`${n}/api/mcp/chat`;o("forwarding to",h);let M=a.headers.get("user-agent"),m=await fetch(h,{method:"POST",headers:{"Content-Type":"application/json","X-WaniWani-Stream-Protocol":"2",...e?{Authorization:`Bearer ${e}`}:{},...M?{"X-Client-User-Agent":M}:{}},body:JSON.stringify({messages:S,mcpServerUrl:l,sessionId:y,source:r,systemPrompt:R,maxSteps:s,visitor:j,webSearch:b}),signal:a.signal});if(o("upstream response status:",m.status),!m.ok){let u=await m.text().catch(()=>"");return o("\u2190 returning",m.status,"upstream error:",u),new Response(u,{status:m.status,headers:{"Content-Type":m.headers.get("Content-Type")??"application/json"}})}let T=new Headers({"Content-Type":m.headers.get("Content-Type")??"text/event-stream"}),w=m.headers.get("x-session-id");return w&&T.set("x-session-id",w),o("\u2190 streaming response",m.status,"body null?",m.body===null),new Response(m.body,{status:m.status,headers:T})}catch(p){console.error("[waniwani:chat] handler error:",p);let S=p instanceof Error?p.message:"Unknown error occurred",y=p instanceof U?p.status:500;return o("\u2190 returning",y,"from caught error"),Response.json({error:S},{status:y})}}}function G(t){let{mcpServerUrl:e,resolveConfig:n,debug:r}=t,i=A("resource",r);return async function(g){i("\u2192 GET",g.toString());try{let c=g.searchParams.get("uri");if(i("uri:",c??"(missing)"),!c)return i("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let f=e??(await n()).mcpServerUrl;i("mcpServerUrl:",f);let d,b;try{[{createMCPClient:d},{StreamableHTTPClientTransport:b}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),i("MCP deps loaded")}catch(C){return console.error("[waniwani:resource] MCP deps import failed:",C),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}i("creating MCP client for",f);let o=await d({transport:new b(new URL(f))});try{i("reading resource:",c);let C=await o.readResource({uri:c});i("resource contents count:",C.contents.length);let a=C.contents[0];if(!a)return i("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let p;return"text"in a&&typeof a.text=="string"?p=a.text:"blob"in a&&typeof a.blob=="string"&&(p=atob(a.blob)),p?(i("\u2190 200 HTML length:",p.length),new Response(p,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(i("\u2190 404 resource has no content, keys:",Object.keys(a)),Response.json({error:"Resource has no content"},{status:404}))}finally{await o.close(),i("MCP client closed")}}catch(c){console.error("[waniwani:resource] handler error:",c);let f=c instanceof Error?c.message:"Unknown error occurred",d=c instanceof U?c.status:500;return i("\u2190 returning",d,"from caught error"),Response.json({error:f},{status:d})}}}function $(t){let{mcpServerUrl:e,resolveConfig:n,debug:r,source:i}=t,s=A("tool",r);return async function(c){s("\u2192 POST",c.url);try{let f=await c.json(),{name:d,arguments:b}=f,o=c.headers.get("x-session-id")?.trim();if(!d||typeof d!="string")return s("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});s("tool:",d,"args:",JSON.stringify(b),"sessionId:",o||"(none)");let C=e??(await n()).mcpServerUrl;s("mcpServerUrl:",C);let a,p;try{[{Client:a},{StreamableHTTPClientTransport:p}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),s("MCP deps loaded")}catch(x){return console.error("[waniwani:tool] MCP deps import failed:",x),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}s("creating MCP client for",C);let S=new p(new URL(C)),y=new a({name:"waniwani-tool-caller",version:"1.0.0"});await y.connect(S);try{s("calling tool:",d);let x={};o&&(x["waniwani/sessionId"]=o),i&&(x["waniwani/source"]=i);let R=await y.callTool({name:d,arguments:b??{},...Object.keys(x).length>0?{_meta:x}:{}});return s("tool result received"),Response.json({content:R.content,structuredContent:R.structuredContent,_meta:R._meta,isError:R.isError})}finally{await y.close(),s("MCP client closed")}}catch(f){console.error("[waniwani:tool] handler error:",f);let d=f instanceof Error?f.message:"Unknown error occurred",b=f instanceof U?f.status:500;return s("\u2190 returning",b,"from caught error"),Response.json({error:d},{status:b})}}}var z=300*1e3;function D(t,e){let n=null,r=null;return async function(){if(n&&Date.now()<n.expiresAt)return n.config;if(r)return r;r=(async()=>{if(!e)throw new U("WANIWANI_API_KEY is required for createChatHandler",401);let s=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!s.ok){let c=await s.text().catch(()=>"");throw new U(`Failed to resolve MCP environment config: ${s.status} ${c}`,s.status)}let g=await s.json();return n={config:g,expiresAt:Date.now()+z},g})();try{return await r}finally{r=null}}}var K="https://app.waniwani.ai";function _(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:n=K,source:r,systemPrompt:i,maxSteps:s=5,beforeRequest:g,mcpServerUrl:c,allowedOrigins:f,debug:d=!1,webSearch:b}=t,o=A("router",d),C=W([n,...f??[]]),a=N(C),p=D(n,e),S=J({apiKey:e,apiUrl:n,source:r,systemPrompt:i,maxSteps:s,beforeRequest:g,mcpServerUrl:c,resolveConfig:p,debug:d,webSearch:I(b)}),y=G({mcpServerUrl:c,resolveConfig:p,debug:d}),x=$({mcpServerUrl:c,resolveConfig:p,debug:d,source:r}),R=process.env.WANIWANI_EVAL==="1";async function k(l){o("\u2192 GET",l.url);try{let h=new URL(l.url),m=h.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(o("pathname:",h.pathname,"subRoute:",m),R&&m==="scenarios"){o("dispatching to scenarios handler (proxy to app API)");try{let w=await(await fetch(`${n}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return a(w.data??w,200,l)}catch{return a([],200,l)}}if(m==="resource"){o("dispatching to resource handler");let T=await y(h);return o("\u2190 resource handler returned",T.status),C(T,l)}return m==="config"?(o("dispatching to config handler"),a({debug:d,eval:R},200,l)):(o("\u2190 404 no matching sub-route for",m),a({error:"Not found"},404,l))}catch(h){console.error("[waniwani:router] GET handler error:",h);let M=h instanceof Error?h.message:"Unknown error occurred";return o("\u2190 500 from caught error"),a({error:M},500,l)}}async function H(l){o("\u2192 POST",l.url);try{let h=new URL(l.url),m=h.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(o("pathname:",h.pathname,"subRoute:",m),R&&m==="scenarios"){o("dispatching to save-scenario handler (proxy to app API)");try{let w=await l.json(),u=await fetch(`${n}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(w)}),O=await u.json();return u.ok?a({ok:!0,scenario:O.data},200,l):a({error:O.message??"Failed to save scenario"},u.status,l)}catch(w){let u=w instanceof Error?w.message:"Failed to save scenario";return a({error:u},400,l)}}if(m==="tool"){o("dispatching to tool handler");let w=await x(l);return o("\u2190 tool handler returned",w.status),C(w,l)}o("dispatching to chat handler");let T=await S(l);return C(T,l)}catch(h){console.error("[waniwani:router] POST handler error:",h);let M=h instanceof Error?h.message:"Unknown error occurred";return o("\u2190 500 from caught error"),a({error:M},500,l)}}function j(l){return C(new Response(null,{status:204}),l)}return{handleChat:S,handleResource:y,handleTool:x,routeGet:k,routePost:H,handleOptions:j}}function Le(t,e){let{apiKey:n,apiUrl:r}=t._config,i=e?.debug??process.env.WANIWANI_DEBUG==="1",s=_({...e?.chat,apiKey:n,apiUrl:r,source:e?.source,allowedOrigins:e?.allowedOrigins,debug:i});return{POST:s.routePost,GET:s.routeGet,OPTIONS:g=>s.handleOptions(g)}}export{Le as toNextJsHandler};
9
+ `):t}function J(t){let{apiKey:e,apiUrl:n,source:o,systemPrompt:i,maxSteps:r,beforeRequest:x,mcpServerUrl:a,resolveConfig:l,debug:m,webSearch:s}=t,c=H("chat",m);return async function(u){c("\u2192 POST",u.url);try{let p=await u.json(),w=p.messages??[],C=p.sessionId,y=p.modelContext,U=i,k=p.visitorContext??null,j=q(u),M={geo:j,client:k};if(c("body parsed \u2014 messages:",w.length,"sessionId:",C??"(none)","geo:",JSON.stringify(j)),x){c("running beforeRequest hook");try{let d=await x({messages:w,sessionId:C,modelContext:y,request:u,visitor:M});d&&(d.messages&&(w=d.messages),d.systemPrompt!==void 0&&(U=d.systemPrompt),d.sessionId!==void 0&&(C=d.sessionId),d.modelContext!==void 0&&(y=d.modelContext)),c("beforeRequest hook done \u2014 messages:",w.length,"sessionId:",C??"(none)")}catch(d){console.error("[waniwani:chat] beforeRequest hook error:",d);let I=d instanceof v?d.status:400,F=d instanceof Error?d.message:"Request rejected";return c("\u2190 returning",I,"from hook error"),Response.json({error:F},{status:I})}}let h=a??(await l()).mcpServerUrl;c("mcpServerUrl:",h),U=L(U,y);let T=`${n}/api/mcp/chat`;c("forwarding to",T);let b=u.headers.get("user-agent"),g=await fetch(T,{method:"POST",headers:{"Content-Type":"application/json","X-WaniWani-Stream-Protocol":"2",...e?{Authorization:`Bearer ${e}`}:{},...b?{"X-Client-User-Agent":b}:{}},body:JSON.stringify({messages:w,mcpServerUrl:h,sessionId:C,source:o,systemPrompt:U,maxSteps:r,visitor:M,webSearch:s}),signal:u.signal});if(c("upstream response status:",g.status),!g.ok){let d=await g.text().catch(()=>"");return c("\u2190 returning",g.status,"upstream error:",d),new Response(d,{status:g.status,headers:{"Content-Type":g.headers.get("Content-Type")??"application/json"}})}let R=new Headers({"Content-Type":g.headers.get("Content-Type")??"text/event-stream"}),A=g.headers.get("x-session-id");return A&&R.set("x-session-id",A),c("\u2190 streaming response",g.status,"body null?",g.body===null),new Response(g.body,{status:g.status,headers:R})}catch(p){console.error("[waniwani:chat] handler error:",p);let w=p instanceof Error?p.message:"Unknown error occurred",C=p instanceof v?p.status:500;return c("\u2190 returning",C,"from caught error"),Response.json({error:w},{status:C})}}}function G(t){let{mcpServerUrl:e,resolveConfig:n,debug:o}=t,i=H("resource",o);return async function(x){i("\u2192 GET",x.toString());try{let a=x.searchParams.get("uri");if(i("uri:",a??"(missing)"),!a)return i("\u2190 400 missing uri"),Response.json({error:"Missing uri query parameter"},{status:400});let l=e??(await n()).mcpServerUrl;i("mcpServerUrl:",l);let m,s;try{[{createMCPClient:m},{StreamableHTTPClientTransport:s}]=await Promise.all([import("@ai-sdk/mcp"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),i("MCP deps loaded")}catch(f){return console.error("[waniwani:resource] MCP deps import failed:",f),Response.json({error:"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving."},{status:501})}i("creating MCP client for",l);let c=await m({transport:new s(new URL(l))});try{i("reading resource:",a);let f=await c.readResource({uri:a});i("resource contents count:",f.contents.length);let u=f.contents[0];if(!u)return i("\u2190 404 resource not found"),Response.json({error:"Resource not found"},{status:404});let p;return"text"in u&&typeof u.text=="string"?p=u.text:"blob"in u&&typeof u.blob=="string"&&(p=atob(u.blob)),p?(i("\u2190 200 HTML length:",p.length),new Response(p,{headers:{"Content-Type":"text/html","Cache-Control":"private, max-age=300"}})):(i("\u2190 404 resource has no content, keys:",Object.keys(u)),Response.json({error:"Resource has no content"},{status:404}))}finally{await c.close(),i("MCP client closed")}}catch(a){console.error("[waniwani:resource] handler error:",a);let l=a instanceof Error?a.message:"Unknown error occurred",m=a instanceof v?a.status:500;return i("\u2190 returning",m,"from caught error"),Response.json({error:l},{status:m})}}}function D(t){let{mcpServerUrl:e,resolveConfig:n,debug:o,source:i}=t,r=H("tool",o);return async function(a){r("\u2192 POST",a.url);try{let l=await a.json(),{name:m,arguments:s}=l,c=a.headers.get("x-session-id")?.trim();if(!m||typeof m!="string")return r("\u2190 400 missing tool name"),Response.json({error:"Missing tool name"},{status:400});r("tool:",m,"args:",JSON.stringify(s),"sessionId:",c||"(none)");let f=e??(await n()).mcpServerUrl;r("mcpServerUrl:",f);let u,p;try{[{Client:u},{StreamableHTTPClientTransport:p}]=await Promise.all([import("@modelcontextprotocol/sdk/client/index.js"),import("@modelcontextprotocol/sdk/client/streamableHttp.js")]),r("MCP deps loaded")}catch(y){return console.error("[waniwani:tool] MCP deps import failed:",y),Response.json({error:"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls."},{status:501})}r("creating MCP client for",f);let w=new p(new URL(f)),C=new u({name:"waniwani-tool-caller",version:"1.0.0"});await C.connect(w);try{r("calling tool:",m);let y={};c&&(y["waniwani/sessionId"]=c),i&&(y["waniwani/source"]=i);let U=await C.callTool({name:m,arguments:s??{},...Object.keys(y).length>0?{_meta:y}:{}});return r("tool result received"),Response.json({content:U.content,structuredContent:U.structuredContent,_meta:U._meta,isError:U.isError})}finally{await C.close(),r("MCP client closed")}}catch(l){console.error("[waniwani:tool] handler error:",l);let m=l instanceof Error?l.message:"Unknown error occurred",s=l instanceof v?l.status:500;return r("\u2190 returning",s,"from caught error"),Response.json({error:m},{status:s})}}}var V=300*1e3;function $(t,e){let n=null,o=null;return async function(){if(n&&Date.now()<n.expiresAt)return n.config;if(o)return o;o=(async()=>{if(!e)throw new v("WANIWANI_API_KEY is required for createChatHandler",401);let r=await fetch(`${t}/api/mcp/environments/config`,{method:"GET",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"}});if(!r.ok){let a=await r.text().catch(()=>"");throw new v(`Failed to resolve MCP environment config: ${r.status} ${a}`,r.status)}let x=await r.json();return n={config:x,expiresAt:Date.now()+V},x})();try{return await o}finally{o=null}}}var K="https://app.waniwani.ai";function _(t={}){let{apiKey:e=process.env.WANIWANI_API_KEY,apiUrl:n=K,source:o,systemPrompt:i,maxSteps:r=5,beforeRequest:x,mcpServerUrl:a,debug:l=!1,webSearch:m}=t,s=H("router",l),c=W(),f=N(c),u=$(n,e),p=J({apiKey:e,apiUrl:n,source:o,systemPrompt:i,maxSteps:r,beforeRequest:x,mcpServerUrl:a,resolveConfig:u,debug:l,webSearch:O(m)}),w=G({mcpServerUrl:a,resolveConfig:u,debug:l}),C=D({mcpServerUrl:a,resolveConfig:u,debug:l,source:o}),y=process.env.WANIWANI_EVAL==="1";async function U(M){s("\u2192 GET",M.url);try{let h=new URL(M.url),b=h.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(s("pathname:",h.pathname,"subRoute:",b),y&&b==="scenarios"){s("dispatching to scenarios handler (proxy to app API)");try{let R=await(await fetch(`${n}/api/mcp/scenarios`,{headers:{...e?{Authorization:`Bearer ${e}`}:{}}})).json();return f(R.data??R,200)}catch{return f([],200)}}if(b==="resource"){s("dispatching to resource handler");let g=await w(h);return s("\u2190 resource handler returned",g.status),c(g)}return b==="config"?(s("dispatching to config handler"),f({debug:l,eval:y},200)):(s("\u2190 404 no matching sub-route for",b),f({error:"Not found"},404))}catch(h){console.error("[waniwani:router] GET handler error:",h);let T=h instanceof Error?h.message:"Unknown error occurred";return s("\u2190 500 from caught error"),f({error:T},500)}}async function k(M){s("\u2192 POST",M.url);try{let h=new URL(M.url),b=h.pathname.replace(/\/$/,"").split("/").filter(Boolean).at(-1);if(s("pathname:",h.pathname,"subRoute:",b),y&&b==="scenarios"){s("dispatching to save-scenario handler (proxy to app API)");try{let R=await M.json(),A=await fetch(`${n}/api/mcp/scenarios`,{method:"POST",headers:{"Content-Type":"application/json",...e?{Authorization:`Bearer ${e}`}:{}},body:JSON.stringify(R)}),d=await A.json();return A.ok?f({ok:!0,scenario:d.data},200):f({error:d.message??"Failed to save scenario"},A.status)}catch(R){let A=R instanceof Error?R.message:"Failed to save scenario";return f({error:A},400)}}if(b==="tool"){s("dispatching to tool handler");let R=await C(M);return s("\u2190 tool handler returned",R.status),c(R)}s("dispatching to chat handler");let g=await p(M);return c(g)}catch(h){console.error("[waniwani:router] POST handler error:",h);let T=h instanceof Error?h.message:"Unknown error occurred";return s("\u2190 500 from caught error"),f({error:T},500)}}function j(){return c(new Response(null,{status:204}))}return{handleChat:p,handleResource:w,handleTool:C,routeGet:U,routePost:k,handleOptions:j}}function qe(t,e){let{apiKey:n,apiUrl:o}=t._config,i=e?.debug??process.env.WANIWANI_DEBUG==="1",r=_({...e?.chat,apiKey:n,apiUrl:o,source:e?.source,debug:i});return{POST:r.routePost,GET:r.routeGet,OPTIONS:()=>r.handleOptions()}}export{qe as toNextJsHandler};
10
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/utils/logger.ts","../../../src/chat/server/@types.ts","../../../src/chat/server/@utils.ts","../../../src/error.ts","../../../src/chat/server/geo.ts","../../../src/shared/model-context.ts","../../../src/chat/server/model-context.ts","../../../src/chat/server/handle-chat.ts","../../../src/chat/server/handle-resource.ts","../../../src/chat/server/handle-tool.ts","../../../src/chat/server/mcp-config-resolver.ts","../../../src/chat/server/api-handler.ts","../../../src/chat/server/next-js/index.ts"],"sourcesContent":["/**\n * Creates a namespaced logger that writes to console.log when enabled,\n * or is a no-op when disabled.\n *\n * @example\n * const log = createLogger(\"chat\", debug);\n * log(\"→ POST\", request.url); // [waniwani:chat] → POST ...\n */\nexport function createLogger(\n\tnamespace: string,\n\tenabled: boolean,\n): (...args: unknown[]) => void {\n\treturn enabled\n\t\t? (...args: unknown[]) => console.log(`[waniwani:${namespace}]`, ...args)\n\t\t: () => {};\n}\n","// WaniWani SDK - Chat Server Types\n\nimport type { UIMessage } from \"ai\";\nimport type { ModelContextUpdate } from \"../../shared/model-context\";\nimport type { GeoLocation } from \"./geo\";\n\n// ============================================================================\n// Visitor Context\n// ============================================================================\n\n/** Client-side visitor context sent in the request body */\nexport interface ClientVisitorContext {\n\ttimezone: string;\n\tlanguage: string;\n\tlanguages: string[];\n\tdeviceType: \"mobile\" | \"tablet\" | \"desktop\";\n\treferrer: string;\n\tvisitorId: string;\n}\n\n/** Combined visitor context: server geo + client context */\nexport interface VisitorMeta {\n\tgeo: GeoLocation;\n\tclient: ClientVisitorContext | null;\n}\n\n// ============================================================================\n// Before Request Hook\n// ============================================================================\n\nexport interface BeforeRequestContext {\n\t/** The conversation messages from the client */\n\tmessages: UIMessage[];\n\t/** Session identifier for conversation continuity */\n\tsessionId?: string;\n\t/** Hidden widget-provided model context for the next assistant turn */\n\tmodelContext?: ModelContextUpdate;\n\t/** The original HTTP Request object */\n\trequest: Request;\n\t/** Server-extracted geo location + client-provided visitor context */\n\tvisitor: VisitorMeta;\n}\n\nexport type BeforeRequestResult = {\n\t/** Override messages (e.g., filtered, augmented) */\n\tmessages?: UIMessage[];\n\t/** Override the system prompt for this request */\n\tsystemPrompt?: string;\n\t/** Override sessionId */\n\tsessionId?: string;\n\t/** Override hidden widget-provided model context */\n\tmodelContext?: ModelContextUpdate;\n};\n\n// ============================================================================\n// Web Search\n// ============================================================================\n\nexport interface WebSearchConfig {\n\t/** Restrict web search results to these domains */\n\tincludeDomains?: string[];\n\t/** Exclude these domains from web search results */\n\texcludeDomains?: string[];\n}\n\n// ============================================================================\n// API Handler Options\n// ============================================================================\n\nexport interface ApiHandlerOptions {\n\t/**\n\t * Identifies this chatbar instance in analytics.\n\t * Forwarded as `waniwani/source` in MCP request metadata.\n\t */\n\tsource?: string;\n\n\t/**\n\t * Your WaniWani API key.\n\t * Defaults to process.env.WANIWANI_API_KEY.\n\t */\n\tapiKey?: string;\n\n\t/**\n\t * The base URL of the WaniWani API.\n\t * Defaults to https://app.waniwani.ai.\n\t */\n\tapiUrl?: string;\n\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable verbose debug logging for all handler steps.\n\t * Logs request details, response codes, resolved URLs, and caught errors.\n\t */\n\tdebug?: boolean;\n\n\t/**\n\t * Additional origins allowed for cross-origin requests.\n\t * The WaniWani platform URL (apiUrl) is always included.\n\t */\n\tallowedOrigins?: string[];\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Result\n// ============================================================================\n\nexport interface ApiHandler {\n\t/** Proxies chat messages to the WaniWani API */\n\thandleChat: (request: Request) => Promise<Response>;\n\t/** Serves MCP resource content (HTML widgets) */\n\thandleResource: (url: URL) => Promise<Response>;\n\t/** Calls an MCP server tool and returns JSON */\n\thandleTool: (request: Request) => Promise<Response>;\n\t/** Routes GET sub-paths (e.g. /resource) */\n\trouteGet: (request: Request) => Promise<Response>;\n\t/** Routes POST sub-paths (e.g. /tool), defaults to chat */\n\troutePost: (request: Request) => Promise<Response>;\n\t/** Handles CORS preflight requests */\n\thandleOptions: (request?: Request) => Response;\n}\n\n// ============================================================================\n// Internal Dependencies (shared across sub-handlers)\n// ============================================================================\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\ntype ConfigResolver = () => Promise<McpEnvironmentConfig>;\n\nexport interface ApiHandlerDeps {\n\tapiKey: string | undefined;\n\tapiUrl: string;\n\tsource: string | undefined;\n\tsystemPrompt: string | undefined;\n\tmaxSteps: number;\n\tbeforeRequest: ApiHandlerOptions[\"beforeRequest\"];\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\twebSearch?: WebSearchConfig;\n}\n\n/** Normalize `true` to `{}` so the upstream API always receives an object or undefined */\nexport function resolveWebSearchConfig(\n\tvalue: boolean | WebSearchConfig | undefined,\n): WebSearchConfig | undefined {\n\tif (value === true) {\n\t\treturn {};\n\t}\n\tif (value === false || value === undefined) {\n\t\treturn undefined;\n\t}\n\treturn value;\n}\n\nexport interface ResourceHandlerDeps {\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\tsource?: string;\n}\n","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response, request?: Request) => Response;\n\nexport function createCors(allowedOrigins: string[]): CorsFunction {\n\tconst originSet = new Set(\n\t\tallowedOrigins.map((o) => o.replace(/\\/$/, \"\").toLowerCase()),\n\t);\n\n\treturn function applyCors(response: Response, request?: Request): Response {\n\t\tconst requestOrigin = request?.headers.get(\"origin\");\n\t\tconst origin = requestOrigin?.toLowerCase();\n\t\t// Auto-allow all *.mcp.waniwani.run subdomains (deployed chat widgets)\n\t\tconst isAllowed =\n\t\t\torigin != null &&\n\t\t\t(originSet.has(origin) || origin.endsWith(\".mcp.waniwani.run\"));\n\n\t\tif (!isAllowed) {\n\t\t\treturn response;\n\t\t}\n\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Origin\",\n\t\t\trequestOrigin as string,\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Allow-Methods\", \"GET, POST, OPTIONS\");\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Headers\",\n\t\t\t\"Content-Type, Authorization, X-Session-Id, X-Client-User-Agent\",\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Expose-Headers\", \"X-Session-Id\");\n\t\tresponse.headers.set(\"Vary\", \"Origin\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(\n\t\tdata: object,\n\t\tstatus: number,\n\t\trequest?: Request,\n\t): Response {\n\t\treturn cors(\n\t\t\tnew Response(JSON.stringify(data), {\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tstatus,\n\t\t\t}),\n\t\t\trequest,\n\t\t);\n\t};\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// Geo — Extract location metadata from platform request headers\n\n/**\n * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).\n * All fields are optional — in local dev, no headers are present.\n */\nexport interface GeoLocation {\n\tcity?: string;\n\tcountry?: string;\n\tcountryRegion?: string;\n\tlatitude?: string;\n\tlongitude?: string;\n\ttimezone?: string;\n\tip?: string;\n}\n\n/**\n * Extracts geolocation from server-side request headers.\n *\n * Supports Vercel (`x-vercel-ip-*`), Cloudflare (`cf-ip*`, `cf-connecting-ip`),\n * and generic IP headers (`x-real-ip`, `x-forwarded-for`).\n *\n * Returns a `GeoLocation` with all fields optional (empty object in local dev).\n */\nexport function extractGeoFromHeaders(request: Request): GeoLocation {\n\tconst h = request.headers;\n\n\t// Vercel URL-encodes city names (e.g. \"S%C3%A3o%20Paulo\")\n\tconst rawCity = h.get(\"x-vercel-ip-city\") ?? h.get(\"cf-ipcity\") ?? undefined;\n\tconst city = rawCity ? safeDecodeURI(rawCity) : undefined;\n\n\tconst country =\n\t\th.get(\"x-vercel-ip-country\") ?? h.get(\"cf-ipcountry\") ?? undefined;\n\tconst countryRegion = h.get(\"x-vercel-ip-country-region\") ?? undefined;\n\tconst latitude =\n\t\th.get(\"x-vercel-ip-latitude\") ?? h.get(\"cf-iplatitude\") ?? undefined;\n\tconst longitude =\n\t\th.get(\"x-vercel-ip-longitude\") ?? h.get(\"cf-iplongitude\") ?? undefined;\n\tconst timezone =\n\t\th.get(\"x-vercel-ip-timezone\") ?? h.get(\"cf-iptimezone\") ?? undefined;\n\tconst ip =\n\t\th.get(\"x-real-ip\") ??\n\t\th.get(\"x-forwarded-for\")?.split(\",\")[0]?.trim() ??\n\t\th.get(\"cf-connecting-ip\") ??\n\t\tundefined;\n\n\treturn { city, country, countryRegion, latitude, longitude, timezone, ip };\n}\n\nfunction safeDecodeURI(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n","import type { ContentBlock } from \"@modelcontextprotocol/sdk/types.js\";\n\nexport type ModelContextContentBlock = ContentBlock;\n\nexport type ModelContextUpdate = {\n\tcontent?: ModelContextContentBlock[];\n\tstructuredContent?: Record<string, unknown>;\n};\n\nexport function hasModelContext(\n\tvalue: ModelContextUpdate | null | undefined,\n): value is ModelContextUpdate {\n\tif (!value) {\n\t\treturn false;\n\t}\n\tconst hasContent = Array.isArray(value.content) && value.content.length > 0;\n\tconst hasStructuredContent =\n\t\ttypeof value.structuredContent === \"object\" &&\n\t\tvalue.structuredContent !== null &&\n\t\tObject.keys(value.structuredContent).length > 0;\n\treturn hasContent || hasStructuredContent;\n}\n\nexport function mergeModelContext(\n\tcurrent: ModelContextUpdate | null | undefined,\n\tnext: ModelContextUpdate | null | undefined,\n): ModelContextUpdate | null {\n\tif (!hasModelContext(current) && !hasModelContext(next)) {\n\t\treturn null;\n\t}\n\tif (!hasModelContext(current)) {\n\t\treturn {\n\t\t\t...(next?.content ? { content: [...next.content] } : {}),\n\t\t\t...(next?.structuredContent\n\t\t\t\t? { structuredContent: { ...next.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (!hasModelContext(next)) {\n\t\treturn {\n\t\t\t...(current.content ? { content: [...current.content] } : {}),\n\t\t\t...(current.structuredContent\n\t\t\t\t? { structuredContent: { ...current.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\n\treturn {\n\t\t...(current.content || next.content\n\t\t\t? { content: [...(current.content ?? []), ...(next.content ?? [])] }\n\t\t\t: {}),\n\t\t...(current.structuredContent || next.structuredContent\n\t\t\t? {\n\t\t\t\t\tstructuredContent: {\n\t\t\t\t\t\t...(current.structuredContent ?? {}),\n\t\t\t\t\t\t...(next.structuredContent ?? {}),\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\nexport function formatModelContextForPrompt(\n\tvalue: ModelContextUpdate | null | undefined,\n): string {\n\tif (!hasModelContext(value)) {\n\t\treturn \"\";\n\t}\n\n\tconst sections: string[] = [\n\t\t\"## Widget Model Context\",\n\t\t\"This hidden context was supplied by an MCP App via `ui/update-model-context`.\",\n\t\t\"Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly.\",\n\t];\n\n\tif (value.content?.length) {\n\t\tconst renderedBlocks = value.content\n\t\t\t.map((block) => {\n\t\t\t\tif (block.type === \"text\" && typeof block.text === \"string\") {\n\t\t\t\t\treturn block.text.trim();\n\t\t\t\t}\n\t\t\t\treturn JSON.stringify(block, null, 2);\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\\n\");\n\t\tif (renderedBlocks) {\n\t\t\tsections.push(`Content blocks:\\n${renderedBlocks}`);\n\t\t}\n\t}\n\n\tif (\n\t\tvalue.structuredContent &&\n\t\tObject.keys(value.structuredContent).length > 0\n\t) {\n\t\tsections.push(\n\t\t\t`Structured content JSON:\\n${JSON.stringify(value.structuredContent, null, 2)}`,\n\t\t);\n\t}\n\n\treturn sections.join(\"\\n\\n\");\n}\n","import {\n\tformatModelContextForPrompt,\n\thasModelContext,\n\ttype ModelContextUpdate,\n} from \"../../shared/model-context\";\n\nexport function applyModelContextToSystemPrompt(\n\tsystemPrompt: string | undefined,\n\tmodelContext: ModelContextUpdate | undefined,\n): string | undefined {\n\tif (!hasModelContext(modelContext)) {\n\t\treturn systemPrompt;\n\t}\n\n\tconst widgetContext = formatModelContextForPrompt(modelContext);\n\tif (!widgetContext) {\n\t\treturn systemPrompt;\n\t}\n\n\treturn [systemPrompt, widgetContext].filter(Boolean).join(\"\\n\\n\");\n}\n","// Handle Chat - Proxies chat requests to the WaniWani API\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type {\n\tApiHandlerDeps,\n\tClientVisitorContext,\n\tVisitorMeta,\n} from \"./@types\";\nimport { extractGeoFromHeaders } from \"./geo\";\nimport { applyModelContextToSystemPrompt } from \"./model-context\";\n\nexport function createChatRequestHandler(deps: ApiHandlerDeps) {\n\tconst {\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch,\n\t} = deps;\n\n\tconst log = createLogger(\"chat\", debug);\n\n\treturn async function handleChat(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\t// 1. Parse request body\n\t\t\tconst body = await request.json();\n\t\t\tlet messages = body.messages ?? [];\n\t\t\tlet sessionId: string | undefined = body.sessionId;\n\t\t\tlet modelContext = body.modelContext;\n\t\t\tlet effectiveSystemPrompt = systemPrompt;\n\n\t\t\t// Extract visitor context (client-side + server-side geo)\n\t\t\tconst clientVisitorContext: ClientVisitorContext | null =\n\t\t\t\tbody.visitorContext ?? null;\n\t\t\tconst geo = extractGeoFromHeaders(request);\n\t\t\tconst visitor: VisitorMeta = { geo, client: clientVisitorContext };\n\n\t\t\tlog(\n\t\t\t\t\"body parsed — messages:\",\n\t\t\t\tmessages.length,\n\t\t\t\t\"sessionId:\",\n\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\"geo:\",\n\t\t\t\tJSON.stringify(geo),\n\t\t\t);\n\n\t\t\t// 2. Run beforeRequest hook\n\t\t\tif (beforeRequest) {\n\t\t\t\tlog(\"running beforeRequest hook\");\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await beforeRequest({\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsessionId,\n\t\t\t\t\t\tmodelContext,\n\t\t\t\t\t\trequest,\n\t\t\t\t\t\tvisitor,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result) {\n\t\t\t\t\t\tif (result.messages) {\n\t\t\t\t\t\t\tmessages = result.messages;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.systemPrompt !== undefined) {\n\t\t\t\t\t\t\teffectiveSystemPrompt = result.systemPrompt;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.sessionId !== undefined) {\n\t\t\t\t\t\t\tsessionId = result.sessionId;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.modelContext !== undefined) {\n\t\t\t\t\t\t\tmodelContext = result.modelContext;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tlog(\n\t\t\t\t\t\t\"beforeRequest hook done — messages:\",\n\t\t\t\t\t\tmessages.length,\n\t\t\t\t\t\t\"sessionId:\",\n\t\t\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\t);\n\t\t\t\t} catch (hookError) {\n\t\t\t\t\tconsole.error(\"[waniwani:chat] beforeRequest hook error:\", hookError);\n\t\t\t\t\tconst status =\n\t\t\t\t\t\thookError instanceof WaniWaniError ? hookError.status : 400;\n\t\t\t\t\tconst message =\n\t\t\t\t\t\thookError instanceof Error ? hookError.message : \"Request rejected\";\n\t\t\t\t\tlog(\"← returning\", status, \"from hook error\");\n\t\t\t\t\treturn Response.json({ error: message }, { status });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 3. Resolve MCP server URL\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\t\t\teffectiveSystemPrompt = applyModelContextToSystemPrompt(\n\t\t\t\teffectiveSystemPrompt,\n\t\t\t\tmodelContext,\n\t\t\t);\n\n\t\t\t// 4. Forward to WaniWani API\n\t\t\tconst upstreamUrl = `${apiUrl}/api/mcp/chat`;\n\t\t\tlog(\"forwarding to\", upstreamUrl);\n\t\t\tconst clientUserAgent = request.headers.get(\"user-agent\");\n\n\t\t\tconst response = await fetch(upstreamUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"X-WaniWani-Stream-Protocol\": \"2\",\n\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t...(clientUserAgent\n\t\t\t\t\t\t? { \"X-Client-User-Agent\": clientUserAgent }\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmessages,\n\t\t\t\t\tmcpServerUrl,\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsource,\n\t\t\t\t\tsystemPrompt: effectiveSystemPrompt,\n\t\t\t\t\tmaxSteps,\n\t\t\t\t\tvisitor,\n\t\t\t\t\twebSearch,\n\t\t\t\t}),\n\t\t\t\tsignal: request.signal,\n\t\t\t});\n\n\t\t\tlog(\"upstream response status:\", response.status);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorBody = await response.text().catch(() => \"\");\n\t\t\t\tlog(\"← returning\", response.status, \"upstream error:\", errorBody);\n\t\t\t\treturn new Response(errorBody, {\n\t\t\t\t\tstatus: response.status,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\":\n\t\t\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 5. Stream the response back\n\t\t\tconst headers = new Headers({\n\t\t\t\t\"Content-Type\":\n\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"text/event-stream\",\n\t\t\t});\n\t\t\tconst upstreamSessionId = response.headers.get(\"x-session-id\");\n\t\t\tif (upstreamSessionId) {\n\t\t\t\theaders.set(\"x-session-id\", upstreamSessionId);\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"← streaming response\",\n\t\t\t\tresponse.status,\n\t\t\t\t\"body null?\",\n\t\t\t\tresponse.body === null,\n\t\t\t);\n\t\t\treturn new Response(response.body, {\n\t\t\t\tstatus: response.status,\n\t\t\t\theaders,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:chat] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Resource - Serves MCP resource content (HTML widgets)\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createResourceHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"resource\", debug);\n\n\treturn async function handleResource(url: URL): Promise<Response> {\n\t\tlog(\"→ GET\", url.toString());\n\t\ttry {\n\t\t\tconst uri = url.searchParams.get(\"uri\");\n\t\t\tlog(\"uri:\", uri ?? \"(missing)\");\n\n\t\t\tif (!uri) {\n\t\t\t\tlog(\"← 400 missing uri\");\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{ error: \"Missing uri query parameter\" },\n\t\t\t\t\t{ status: 400 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:resource] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"reading resource:\", uri);\n\t\t\t\tconst result = await mcp.readResource({ uri });\n\t\t\t\tlog(\"resource contents count:\", result.contents.length);\n\n\t\t\t\tconst content = result.contents[0];\n\t\t\t\tif (!content) {\n\t\t\t\t\tlog(\"← 404 resource not found\");\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource not found\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlet html: string | undefined;\n\t\t\t\tif (\"text\" in content && typeof content.text === \"string\") {\n\t\t\t\t\thtml = content.text;\n\t\t\t\t} else if (\"blob\" in content && typeof content.blob === \"string\") {\n\t\t\t\t\thtml = atob(content.blob);\n\t\t\t\t}\n\n\t\t\t\tif (!html) {\n\t\t\t\t\tlog(\"← 404 resource has no content, keys:\", Object.keys(content));\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource has no content\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlog(\"← 200 HTML length:\", html.length);\n\t\t\t\treturn new Response(html, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=300\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:resource] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tool - Calls MCP server tools directly and returns JSON results\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createToolHandler(deps: ResourceHandlerDeps) {\n\tconst {\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t} = deps;\n\n\tconst log = createLogger(\"tool\", debug);\n\n\treturn async function handleTool(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst body = await request.json();\n\t\t\tconst { name, arguments: args } = body as {\n\t\t\t\tname: string;\n\t\t\t\targuments: Record<string, unknown>;\n\t\t\t};\n\t\t\tconst requestSessionId = request.headers.get(\"x-session-id\")?.trim();\n\n\t\t\tif (!name || typeof name !== \"string\") {\n\t\t\t\tlog(\"← 400 missing tool name\");\n\t\t\t\treturn Response.json({ error: \"Missing tool name\" }, { status: 400 });\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"tool:\",\n\t\t\t\tname,\n\t\t\t\t\"args:\",\n\t\t\t\tJSON.stringify(args),\n\t\t\t\t\"sessionId:\",\n\t\t\t\trequestSessionId || \"(none)\",\n\t\t\t);\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet Client: typeof import(\"@modelcontextprotocol/sdk/client/index.js\")[\"Client\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ Client }, { StreamableHTTPClientTransport }] = await Promise.all([\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/index.js\"),\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\"[waniwani:tool] MCP deps import failed:\", importError);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst transport = new StreamableHTTPClientTransport(\n\t\t\t\tnew URL(mcpServerUrl),\n\t\t\t);\n\t\t\tconst client = new Client({\n\t\t\t\tname: \"waniwani-tool-caller\",\n\t\t\t\tversion: \"1.0.0\",\n\t\t\t});\n\t\t\tawait client.connect(transport);\n\n\t\t\ttry {\n\t\t\t\tlog(\"calling tool:\", name);\n\t\t\t\tconst _meta: Record<string, unknown> = {};\n\t\t\t\tif (requestSessionId) {\n\t\t\t\t\t_meta[\"waniwani/sessionId\"] = requestSessionId;\n\t\t\t\t}\n\t\t\t\tif (source) {\n\t\t\t\t\t_meta[\"waniwani/source\"] = source;\n\t\t\t\t}\n\t\t\t\tconst result = await client.callTool({\n\t\t\t\t\tname,\n\t\t\t\t\targuments: args ?? {},\n\t\t\t\t\t...(Object.keys(_meta).length > 0 ? { _meta } : {}),\n\t\t\t\t} as {\n\t\t\t\t\tname: string;\n\t\t\t\t\targuments: Record<string, unknown>;\n\t\t\t\t\t_meta?: Record<string, unknown>;\n\t\t\t\t});\n\t\t\t\tlog(\"tool result received\");\n\n\t\t\t\treturn Response.json({\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tstructuredContent: result.structuredContent,\n\t\t\t\t\t_meta: result._meta,\n\t\t\t\t\tisError: result.isError,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait client.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tool] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// MCP Config Resolver - Lazy-loads and caches MCP environment config\n\nimport { WaniWaniError } from \"../../error\";\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\nconst TTL_MS = 5 * 60 * 1000; // 5 minutes\n\nexport function createMcpConfigResolver(\n\tapiUrl: string,\n\tapiKey: string | undefined,\n) {\n\tlet cached: { config: McpEnvironmentConfig; expiresAt: number } | null = null;\n\tlet inflight: Promise<McpEnvironmentConfig> | null = null;\n\n\treturn async function resolve(): Promise<McpEnvironmentConfig> {\n\t\tif (cached && Date.now() < cached.expiresAt) {\n\t\t\treturn cached.config;\n\t\t}\n\n\t\t// Deduplicate concurrent requests (cold start scenario)\n\t\tif (inflight) {\n\t\t\treturn inflight;\n\t\t}\n\n\t\tinflight = (async () => {\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t\"WANIWANI_API_KEY is required for createChatHandler\",\n\t\t\t\t\t401,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst response = await fetch(`${apiUrl}/api/mcp/environments/config`, {\n\t\t\t\tmethod: \"GET\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t`Failed to resolve MCP environment config: ${response.status} ${body}`,\n\t\t\t\t\tresponse.status,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data = (await response.json()) as McpEnvironmentConfig;\n\t\t\tcached = { config: data, expiresAt: Date.now() + TTL_MS };\n\t\t\treturn data;\n\t\t})();\n\n\t\ttry {\n\t\t\treturn await inflight;\n\t\t} finally {\n\t\t\tinflight = null;\n\t\t}\n\t};\n}\n","// API Handler - Composes chat and resource handlers into a unified API handler\n\nimport { createLogger } from \"../../utils/logger.js\";\nimport {\n\ttype ApiHandler,\n\ttype ApiHandlerOptions,\n\tresolveWebSearchConfig,\n} from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\nconst DEFAULT_API_URL = \"https://app.waniwani.ai\";\n\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = DEFAULT_API_URL,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tallowedOrigins: extraOrigins,\n\t\tdebug = false,\n\t\twebSearch,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors([apiUrl, ...(extraOrigins ?? [])]);\n\tconst json = createJsonResponse(cors);\n\n\tconst resolveConfig = createMcpConfigResolver(apiUrl, apiKey);\n\n\tconst handleChat = createChatRequestHandler({\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch: resolveWebSearchConfig(webSearch),\n\t});\n\n\tconst handleResource = createResourceHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleTool = createToolHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t});\n\n\tconst evalEnabled = process.env.WANIWANI_EVAL === \"1\";\n\n\tasync function routeGet(request: Request): Promise<Response> {\n\t\tlog(\"→ GET\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to scenarios handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\treturn json(data.data ?? data, 200, request);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200, request);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"resource\") {\n\t\t\t\tlog(\"dispatching to resource handler\");\n\t\t\t\tconst response = await handleResource(url);\n\t\t\t\tlog(\"← resource handler returned\", response.status);\n\t\t\t\treturn cors(response, request);\n\t\t\t}\n\n\t\t\tif (subRoute === \"config\") {\n\t\t\t\tlog(\"dispatching to config handler\");\n\t\t\t\treturn json({ debug, eval: evalEnabled }, 200, request);\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404, request);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] GET handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500, request);\n\t\t}\n\t}\n\n\tasync function routePost(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to save-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to save scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t\trequest,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200, request);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to save scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400, request);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"tool\") {\n\t\t\t\tlog(\"dispatching to tool handler\");\n\t\t\t\tconst response = await handleTool(request);\n\t\t\t\tlog(\"← tool handler returned\", response.status);\n\t\t\t\treturn cors(response, request);\n\t\t\t}\n\n\t\t\t// Default: treat as chat request\n\t\t\tlog(\"dispatching to chat handler\");\n\t\t\tconst chatResponse = await handleChat(request);\n\t\t\treturn cors(chatResponse, request);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] POST handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500, request);\n\t\t}\n\t}\n\n\tfunction handleOptions(request?: Request): Response {\n\t\treturn cors(new Response(null, { status: 204 }), request);\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\n\t\thandleOptions,\n\t};\n}\n","// WaniWani SDK - Next.js Adapter\n\nimport type { WaniWaniClient } from \"../../../types.js\";\nimport { createApiHandler } from \"../api-handler.js\";\nimport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\nexport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\n/**\n * Create Next.js route handlers from a WaniWani client.\n *\n * Returns `{ GET, POST }` for use with catch-all routes.\n * Mount at `app/api/waniwani/[[...path]]/route.ts`:\n *\n * - `POST /api/waniwani` → chat (proxied to WaniWani API)\n * - `GET /api/waniwani/resource?uri=…` → MCP resource content\n *\n * @example\n * ```typescript\n * // app/api/waniwani/[[...path]]/route.ts\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani();\n *\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: {\n * systemPrompt: \"You are a helpful assistant.\",\n * mcpServerUrl: process.env.MCP_SERVER_URL!,\n * },\n * });\n * ```\n */\nexport function toNextJsHandler(\n\tclient: WaniWaniClient,\n\toptions: NextJsHandlerOptions,\n): NextJsHandlerResult {\n\tconst { apiKey, apiUrl } = client._config;\n\n\tconst debugEnabled = options?.debug ?? process.env.WANIWANI_DEBUG === \"1\";\n\n\tconst handler = createApiHandler({\n\t\t...options?.chat,\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource: options?.source,\n\t\tallowedOrigins: options?.allowedOrigins,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tPOST: handler.routePost,\n\t\tGET: handler.routeGet,\n\t\tOPTIONS: (request: Request) => handler.handleOptions(request),\n\t};\n}\n"],"mappings":"AAQO,SAASA,EACfC,EACAC,EAC+B,CAC/B,OAAOA,EACJ,IAAIC,IAAoB,QAAQ,IAAI,aAAaF,CAAS,IAAK,GAAGE,CAAI,EACtE,IAAM,CAAC,CACX,CCqKO,SAASC,EACfC,EAC8B,CAC9B,GAAIA,IAAU,GACb,MAAO,CAAC,EAET,GAAI,EAAAA,IAAU,IAASA,IAAU,QAGjC,OAAOA,CACR,CC1LO,SAASC,EAAWC,EAAwC,CAClE,IAAMC,EAAY,IAAI,IACrBD,EAAe,IAAKE,GAAMA,EAAE,QAAQ,MAAO,EAAE,EAAE,YAAY,CAAC,CAC7D,EAEA,OAAO,SAAmBC,EAAoBC,EAA6B,CAC1E,IAAMC,EAAgBD,GAAS,QAAQ,IAAI,QAAQ,EAC7CE,EAASD,GAAe,YAAY,EAM1C,OAHCC,GAAU,OACTL,EAAU,IAAIK,CAAM,GAAKA,EAAO,SAAS,mBAAmB,KAM9DH,EAAS,QAAQ,IAChB,8BACAE,CACD,EACAF,EAAS,QAAQ,IAAI,+BAAgC,oBAAoB,EACzEA,EAAS,QAAQ,IAChB,+BACA,gEACD,EACAA,EAAS,QAAQ,IAAI,gCAAiC,cAAc,EACpEA,EAAS,QAAQ,IAAI,OAAQ,QAAQ,GAC9BA,CACR,CACD,CAEO,SAASI,EAAmBC,EAAoB,CACtD,OAAO,SACNC,EACAC,EACAN,EACW,CAC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vB,EAAK,CAAE,MAAOwB,CAAI,EAAG,IAAKX,CAAO,CACzC,CACD,CAEA,GAAIE,IAAa,OAAQ,CACxBnB,EAAI,6BAA6B,EACjC,IAAMqB,EAAW,MAAMR,EAAWI,CAAO,EACzC,OAAAjB,EAAI,+BAA2BqB,EAAS,MAAM,EACvCnB,EAAKmB,EAAUJ,CAAO,CAC9B,CAGAjB,EAAI,6BAA6B,EACjC,IAAM6B,EAAe,MAAMrB,EAAWS,CAAO,EAC7C,OAAOf,EAAK2B,EAAcZ,CAAO,CAClC,OAASK,EAAO,CACf,QAAQ,MAAM,wCAAyCA,CAAK,EAC5D,IAAMC,EACLD,aAAiB,MAAQA,EAAM,QAAU,yBAC1C,OAAAtB,EAAI,8BAAyB,EACtBI,EAAK,CAAE,MAAOmB,CAAQ,EAAG,IAAKN,CAAO,CAC7C,CACD,CAEA,SAASa,EAAcb,EAA6B,CACnD,OAAOf,EAAK,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,EAAGe,CAAO,CACzD,CAEA,MAAO,CACN,WAAAT,EACA,eAAAG,EACA,WAAAE,EACA,SAAAG,EACA,UAAAQ,EACA,cAAAM,CACD,CACD,CCvJO,SAASC,GACfC,EACAC,EACsB,CACtB,GAAM,CAAE,OAAAC,EAAQ,OAAAC,CAAO,EAAIH,EAAO,QAE5BI,EAAeH,GAAS,OAAS,QAAQ,IAAI,iBAAmB,IAEhEI,EAAUC,EAAiB,CAChC,GAAGL,GAAS,KACZ,OAAAC,EACA,OAAAC,EACA,OAAQF,GAAS,OACjB,eAAgBA,GAAS,eACzB,MAAOG,CACR,CAAC,EAED,MAAO,CACN,KAAMC,EAAQ,UACd,IAAKA,EAAQ,SACb,QAAUE,GAAqBF,EAAQ,cAAcE,CAAO,CAC7D,CACD","names":["createLogger","namespace","enabled","args","resolveWebSearchConfig","value","createCors","allowedOrigins","originSet","o","response","request","requestOrigin","origin","createJsonResponse","cors","data","status","WaniWaniError","message","status","extractGeoFromHeaders","request","h","rawCity","city","safeDecodeURI","country","countryRegion","latitude","longitude","timezone","ip","value","hasModelContext","value","hasContent","hasStructuredContent","formatModelContextForPrompt","value","hasModelContext","sections","renderedBlocks","block","applyModelContextToSystemPrompt","systemPrompt","modelContext","hasModelContext","widgetContext","formatModelContextForPrompt","createChatRequestHandler","deps","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrlOverride","resolveConfig","debug","webSearch","log","createLogger","request","body","messages","sessionId","modelContext","effectiveSystemPrompt","clientVisitorContext","geo","extractGeoFromHeaders","visitor","result","hookError","status","WaniWaniError","message","mcpServerUrl","applyModelContextToSystemPrompt","upstreamUrl","clientUserAgent","response","errorBody","headers","upstreamSessionId","error","createResourceHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","url","uri","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","content","html","error","message","status","WaniWaniError","createToolHandler","deps","mcpServerUrlOverride","resolveConfig","debug","source","log","createLogger","request","body","name","args","requestSessionId","mcpServerUrl","Client","StreamableHTTPClientTransport","importError","transport","client","_meta","result","error","message","status","WaniWaniError","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","extraOrigins","debug","webSearch","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","resolveWebSearchConfig","handleResource","createResourceHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","handleOptions","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler","request"]}
1
+ {"version":3,"sources":["../../../src/utils/logger.ts","../../../src/chat/server/@types.ts","../../../src/chat/server/@utils.ts","../../../src/error.ts","../../../src/chat/server/geo.ts","../../../src/shared/model-context.ts","../../../src/chat/server/model-context.ts","../../../src/chat/server/handle-chat.ts","../../../src/chat/server/handle-resource.ts","../../../src/chat/server/handle-tool.ts","../../../src/chat/server/mcp-config-resolver.ts","../../../src/chat/server/api-handler.ts","../../../src/chat/server/next-js/index.ts"],"sourcesContent":["/**\n * Creates a namespaced logger that writes to console.log when enabled,\n * or is a no-op when disabled.\n *\n * @example\n * const log = createLogger(\"chat\", debug);\n * log(\"→ POST\", request.url); // [waniwani:chat] → POST ...\n */\nexport function createLogger(\n\tnamespace: string,\n\tenabled: boolean,\n): (...args: unknown[]) => void {\n\treturn enabled\n\t\t? (...args: unknown[]) => console.log(`[waniwani:${namespace}]`, ...args)\n\t\t: () => {};\n}\n","// WaniWani SDK - Chat Server Types\n\nimport type { UIMessage } from \"ai\";\nimport type { ModelContextUpdate } from \"../../shared/model-context\";\nimport type { GeoLocation } from \"./geo\";\n\n// ============================================================================\n// Visitor Context\n// ============================================================================\n\n/** Client-side visitor context sent in the request body */\nexport interface ClientVisitorContext {\n\ttimezone: string;\n\tlanguage: string;\n\tlanguages: string[];\n\tdeviceType: \"mobile\" | \"tablet\" | \"desktop\";\n\treferrer: string;\n\tvisitorId: string;\n}\n\n/** Combined visitor context: server geo + client context */\nexport interface VisitorMeta {\n\tgeo: GeoLocation;\n\tclient: ClientVisitorContext | null;\n}\n\n// ============================================================================\n// Before Request Hook\n// ============================================================================\n\nexport interface BeforeRequestContext {\n\t/** The conversation messages from the client */\n\tmessages: UIMessage[];\n\t/** Session identifier for conversation continuity */\n\tsessionId?: string;\n\t/** Hidden widget-provided model context for the next assistant turn */\n\tmodelContext?: ModelContextUpdate;\n\t/** The original HTTP Request object */\n\trequest: Request;\n\t/** Server-extracted geo location + client-provided visitor context */\n\tvisitor: VisitorMeta;\n}\n\nexport type BeforeRequestResult = {\n\t/** Override messages (e.g., filtered, augmented) */\n\tmessages?: UIMessage[];\n\t/** Override the system prompt for this request */\n\tsystemPrompt?: string;\n\t/** Override sessionId */\n\tsessionId?: string;\n\t/** Override hidden widget-provided model context */\n\tmodelContext?: ModelContextUpdate;\n};\n\n// ============================================================================\n// Web Search\n// ============================================================================\n\nexport interface WebSearchConfig {\n\t/** Restrict web search results to these domains */\n\tincludeDomains?: string[];\n\t/** Exclude these domains from web search results */\n\texcludeDomains?: string[];\n}\n\n// ============================================================================\n// API Handler Options\n// ============================================================================\n\nexport interface ApiHandlerOptions {\n\t/**\n\t * Identifies this chatbar instance in analytics.\n\t * Forwarded as `waniwani/source` in MCP request metadata.\n\t */\n\tsource?: string;\n\n\t/**\n\t * Your WaniWani API key.\n\t * Defaults to process.env.WANIWANI_API_KEY.\n\t */\n\tapiKey?: string;\n\n\t/**\n\t * The base URL of the WaniWani API.\n\t * Defaults to https://app.waniwani.ai.\n\t */\n\tapiUrl?: string;\n\n\t/**\n\t * System prompt for the assistant.\n\t * Can be overridden per-request via `beforeRequest`.\n\t */\n\tsystemPrompt?: string;\n\n\t/**\n\t * Maximum number of tool call steps. Defaults to 5.\n\t */\n\tmaxSteps?: number;\n\n\t/**\n\t * Hook called before each request is forwarded to the WaniWani API.\n\t * - Return void to use defaults.\n\t * - Return an object to override messages, systemPrompt, or sessionId.\n\t * - Throw to reject the request (the error message is returned as JSON).\n\t */\n\tbeforeRequest?: (\n\t\tcontext: BeforeRequestContext,\n\t) =>\n\t\t| Promise<BeforeRequestResult | undefined>\n\t\t| BeforeRequestResult\n\t\t| undefined;\n\n\t/**\n\t * Override the MCP server URL directly, bypassing config resolution.\n\t * Useful for development/testing when pointing to a local MCP server.\n\t */\n\tmcpServerUrl?: string;\n\n\t/**\n\t * Enable verbose debug logging for all handler steps.\n\t * Logs request details, response codes, resolved URLs, and caught errors.\n\t */\n\tdebug?: boolean;\n\n\t/**\n\t * Enable web search as an additional tool alongside MCP tools.\n\t * Pass `true` to enable with defaults, or a config object to restrict domains.\n\t */\n\twebSearch?: boolean | WebSearchConfig;\n}\n\n// ============================================================================\n// API Handler Result\n// ============================================================================\n\nexport interface ApiHandler {\n\t/** Proxies chat messages to the WaniWani API */\n\thandleChat: (request: Request) => Promise<Response>;\n\t/** Serves MCP resource content (HTML widgets) */\n\thandleResource: (url: URL) => Promise<Response>;\n\t/** Calls an MCP server tool and returns JSON */\n\thandleTool: (request: Request) => Promise<Response>;\n\t/** Routes GET sub-paths (e.g. /resource) */\n\trouteGet: (request: Request) => Promise<Response>;\n\t/** Routes POST sub-paths (e.g. /tool), defaults to chat */\n\troutePost: (request: Request) => Promise<Response>;\n\t/** Handles CORS preflight requests */\n\thandleOptions: () => Response;\n}\n\n// ============================================================================\n// Internal Dependencies (shared across sub-handlers)\n// ============================================================================\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\ntype ConfigResolver = () => Promise<McpEnvironmentConfig>;\n\nexport interface ApiHandlerDeps {\n\tapiKey: string | undefined;\n\tapiUrl: string;\n\tsource: string | undefined;\n\tsystemPrompt: string | undefined;\n\tmaxSteps: number;\n\tbeforeRequest: ApiHandlerOptions[\"beforeRequest\"];\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\twebSearch?: WebSearchConfig;\n}\n\n/** Normalize `true` to `{}` so the upstream API always receives an object or undefined */\nexport function resolveWebSearchConfig(\n\tvalue: boolean | WebSearchConfig | undefined,\n): WebSearchConfig | undefined {\n\tif (value === true) {\n\t\treturn {};\n\t}\n\tif (value === false || value === undefined) {\n\t\treturn undefined;\n\t}\n\treturn value;\n}\n\nexport interface ResourceHandlerDeps {\n\tmcpServerUrl: string | undefined;\n\tresolveConfig: ConfigResolver;\n\tdebug: boolean;\n\tsource?: string;\n}\n","// Shared helpers for chat server handlers\n\nexport type CorsFunction = (response: Response) => Response;\n\nexport function createCors(): CorsFunction {\n\treturn function applyCors(response: Response): Response {\n\t\tresponse.headers.set(\"Access-Control-Allow-Origin\", \"*\");\n\t\tresponse.headers.set(\"Access-Control-Allow-Methods\", \"GET, POST, OPTIONS\");\n\t\tresponse.headers.set(\n\t\t\t\"Access-Control-Allow-Headers\",\n\t\t\t\"Content-Type, Authorization, X-Session-Id, X-Client-User-Agent\",\n\t\t);\n\t\tresponse.headers.set(\"Access-Control-Expose-Headers\", \"X-Session-Id\");\n\t\treturn response;\n\t};\n}\n\nexport function createJsonResponse(cors: CorsFunction) {\n\treturn function json(data: object, status: number): Response {\n\t\treturn cors(\n\t\t\tnew Response(JSON.stringify(data), {\n\t\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\t\tstatus,\n\t\t\t}),\n\t\t);\n\t};\n}\n","// WaniWani SDK - Errors\n\nexport class WaniWaniError extends Error {\n\tconstructor(\n\t\tmessage: string,\n\t\tpublic status: number,\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"WaniWaniError\";\n\t}\n}\n","// Geo — Extract location metadata from platform request headers\n\n/**\n * Server-side geolocation extracted from platform headers (Vercel, Cloudflare).\n * All fields are optional — in local dev, no headers are present.\n */\nexport interface GeoLocation {\n\tcity?: string;\n\tcountry?: string;\n\tcountryRegion?: string;\n\tlatitude?: string;\n\tlongitude?: string;\n\ttimezone?: string;\n\tip?: string;\n}\n\n/**\n * Extracts geolocation from server-side request headers.\n *\n * Supports Vercel (`x-vercel-ip-*`), Cloudflare (`cf-ip*`, `cf-connecting-ip`),\n * and generic IP headers (`x-real-ip`, `x-forwarded-for`).\n *\n * Returns a `GeoLocation` with all fields optional (empty object in local dev).\n */\nexport function extractGeoFromHeaders(request: Request): GeoLocation {\n\tconst h = request.headers;\n\n\t// Vercel URL-encodes city names (e.g. \"S%C3%A3o%20Paulo\")\n\tconst rawCity = h.get(\"x-vercel-ip-city\") ?? h.get(\"cf-ipcity\") ?? undefined;\n\tconst city = rawCity ? safeDecodeURI(rawCity) : undefined;\n\n\tconst country =\n\t\th.get(\"x-vercel-ip-country\") ?? h.get(\"cf-ipcountry\") ?? undefined;\n\tconst countryRegion = h.get(\"x-vercel-ip-country-region\") ?? undefined;\n\tconst latitude =\n\t\th.get(\"x-vercel-ip-latitude\") ?? h.get(\"cf-iplatitude\") ?? undefined;\n\tconst longitude =\n\t\th.get(\"x-vercel-ip-longitude\") ?? h.get(\"cf-iplongitude\") ?? undefined;\n\tconst timezone =\n\t\th.get(\"x-vercel-ip-timezone\") ?? h.get(\"cf-iptimezone\") ?? undefined;\n\tconst ip =\n\t\th.get(\"x-real-ip\") ??\n\t\th.get(\"x-forwarded-for\")?.split(\",\")[0]?.trim() ??\n\t\th.get(\"cf-connecting-ip\") ??\n\t\tundefined;\n\n\treturn { city, country, countryRegion, latitude, longitude, timezone, ip };\n}\n\nfunction safeDecodeURI(value: string): string {\n\ttry {\n\t\treturn decodeURIComponent(value);\n\t} catch {\n\t\treturn value;\n\t}\n}\n","import type { ContentBlock } from \"@modelcontextprotocol/sdk/types.js\";\n\nexport type ModelContextContentBlock = ContentBlock;\n\nexport type ModelContextUpdate = {\n\tcontent?: ModelContextContentBlock[];\n\tstructuredContent?: Record<string, unknown>;\n};\n\nexport function hasModelContext(\n\tvalue: ModelContextUpdate | null | undefined,\n): value is ModelContextUpdate {\n\tif (!value) {\n\t\treturn false;\n\t}\n\tconst hasContent = Array.isArray(value.content) && value.content.length > 0;\n\tconst hasStructuredContent =\n\t\ttypeof value.structuredContent === \"object\" &&\n\t\tvalue.structuredContent !== null &&\n\t\tObject.keys(value.structuredContent).length > 0;\n\treturn hasContent || hasStructuredContent;\n}\n\nexport function mergeModelContext(\n\tcurrent: ModelContextUpdate | null | undefined,\n\tnext: ModelContextUpdate | null | undefined,\n): ModelContextUpdate | null {\n\tif (!hasModelContext(current) && !hasModelContext(next)) {\n\t\treturn null;\n\t}\n\tif (!hasModelContext(current)) {\n\t\treturn {\n\t\t\t...(next?.content ? { content: [...next.content] } : {}),\n\t\t\t...(next?.structuredContent\n\t\t\t\t? { structuredContent: { ...next.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\tif (!hasModelContext(next)) {\n\t\treturn {\n\t\t\t...(current.content ? { content: [...current.content] } : {}),\n\t\t\t...(current.structuredContent\n\t\t\t\t? { structuredContent: { ...current.structuredContent } }\n\t\t\t\t: {}),\n\t\t};\n\t}\n\n\treturn {\n\t\t...(current.content || next.content\n\t\t\t? { content: [...(current.content ?? []), ...(next.content ?? [])] }\n\t\t\t: {}),\n\t\t...(current.structuredContent || next.structuredContent\n\t\t\t? {\n\t\t\t\t\tstructuredContent: {\n\t\t\t\t\t\t...(current.structuredContent ?? {}),\n\t\t\t\t\t\t...(next.structuredContent ?? {}),\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t: {}),\n\t};\n}\n\nexport function formatModelContextForPrompt(\n\tvalue: ModelContextUpdate | null | undefined,\n): string {\n\tif (!hasModelContext(value)) {\n\t\treturn \"\";\n\t}\n\n\tconst sections: string[] = [\n\t\t\"## Widget Model Context\",\n\t\t\"This hidden context was supplied by an MCP App via `ui/update-model-context`.\",\n\t\t\"Use it for the next assistant turn only. If it includes flow continuation or tool-call instructions, follow them exactly.\",\n\t];\n\n\tif (value.content?.length) {\n\t\tconst renderedBlocks = value.content\n\t\t\t.map((block) => {\n\t\t\t\tif (block.type === \"text\" && typeof block.text === \"string\") {\n\t\t\t\t\treturn block.text.trim();\n\t\t\t\t}\n\t\t\t\treturn JSON.stringify(block, null, 2);\n\t\t\t})\n\t\t\t.filter(Boolean)\n\t\t\t.join(\"\\n\\n\");\n\t\tif (renderedBlocks) {\n\t\t\tsections.push(`Content blocks:\\n${renderedBlocks}`);\n\t\t}\n\t}\n\n\tif (\n\t\tvalue.structuredContent &&\n\t\tObject.keys(value.structuredContent).length > 0\n\t) {\n\t\tsections.push(\n\t\t\t`Structured content JSON:\\n${JSON.stringify(value.structuredContent, null, 2)}`,\n\t\t);\n\t}\n\n\treturn sections.join(\"\\n\\n\");\n}\n","import {\n\tformatModelContextForPrompt,\n\thasModelContext,\n\ttype ModelContextUpdate,\n} from \"../../shared/model-context\";\n\nexport function applyModelContextToSystemPrompt(\n\tsystemPrompt: string | undefined,\n\tmodelContext: ModelContextUpdate | undefined,\n): string | undefined {\n\tif (!hasModelContext(modelContext)) {\n\t\treturn systemPrompt;\n\t}\n\n\tconst widgetContext = formatModelContextForPrompt(modelContext);\n\tif (!widgetContext) {\n\t\treturn systemPrompt;\n\t}\n\n\treturn [systemPrompt, widgetContext].filter(Boolean).join(\"\\n\\n\");\n}\n","// Handle Chat - Proxies chat requests to the WaniWani API\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type {\n\tApiHandlerDeps,\n\tClientVisitorContext,\n\tVisitorMeta,\n} from \"./@types\";\nimport { extractGeoFromHeaders } from \"./geo\";\nimport { applyModelContextToSystemPrompt } from \"./model-context\";\n\nexport function createChatRequestHandler(deps: ApiHandlerDeps) {\n\tconst {\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch,\n\t} = deps;\n\n\tconst log = createLogger(\"chat\", debug);\n\n\treturn async function handleChat(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\t// 1. Parse request body\n\t\t\tconst body = await request.json();\n\t\t\tlet messages = body.messages ?? [];\n\t\t\tlet sessionId: string | undefined = body.sessionId;\n\t\t\tlet modelContext = body.modelContext;\n\t\t\tlet effectiveSystemPrompt = systemPrompt;\n\n\t\t\t// Extract visitor context (client-side + server-side geo)\n\t\t\tconst clientVisitorContext: ClientVisitorContext | null =\n\t\t\t\tbody.visitorContext ?? null;\n\t\t\tconst geo = extractGeoFromHeaders(request);\n\t\t\tconst visitor: VisitorMeta = { geo, client: clientVisitorContext };\n\n\t\t\tlog(\n\t\t\t\t\"body parsed — messages:\",\n\t\t\t\tmessages.length,\n\t\t\t\t\"sessionId:\",\n\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\"geo:\",\n\t\t\t\tJSON.stringify(geo),\n\t\t\t);\n\n\t\t\t// 2. Run beforeRequest hook\n\t\t\tif (beforeRequest) {\n\t\t\t\tlog(\"running beforeRequest hook\");\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await beforeRequest({\n\t\t\t\t\t\tmessages,\n\t\t\t\t\t\tsessionId,\n\t\t\t\t\t\tmodelContext,\n\t\t\t\t\t\trequest,\n\t\t\t\t\t\tvisitor,\n\t\t\t\t\t});\n\n\t\t\t\t\tif (result) {\n\t\t\t\t\t\tif (result.messages) {\n\t\t\t\t\t\t\tmessages = result.messages;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.systemPrompt !== undefined) {\n\t\t\t\t\t\t\teffectiveSystemPrompt = result.systemPrompt;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.sessionId !== undefined) {\n\t\t\t\t\t\t\tsessionId = result.sessionId;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (result.modelContext !== undefined) {\n\t\t\t\t\t\t\tmodelContext = result.modelContext;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tlog(\n\t\t\t\t\t\t\"beforeRequest hook done — messages:\",\n\t\t\t\t\t\tmessages.length,\n\t\t\t\t\t\t\"sessionId:\",\n\t\t\t\t\t\tsessionId ?? \"(none)\",\n\t\t\t\t\t);\n\t\t\t\t} catch (hookError) {\n\t\t\t\t\tconsole.error(\"[waniwani:chat] beforeRequest hook error:\", hookError);\n\t\t\t\t\tconst status =\n\t\t\t\t\t\thookError instanceof WaniWaniError ? hookError.status : 400;\n\t\t\t\t\tconst message =\n\t\t\t\t\t\thookError instanceof Error ? hookError.message : \"Request rejected\";\n\t\t\t\t\tlog(\"← returning\", status, \"from hook error\");\n\t\t\t\t\treturn Response.json({ error: message }, { status });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// 3. Resolve MCP server URL\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\t\t\teffectiveSystemPrompt = applyModelContextToSystemPrompt(\n\t\t\t\teffectiveSystemPrompt,\n\t\t\t\tmodelContext,\n\t\t\t);\n\n\t\t\t// 4. Forward to WaniWani API\n\t\t\tconst upstreamUrl = `${apiUrl}/api/mcp/chat`;\n\t\t\tlog(\"forwarding to\", upstreamUrl);\n\t\t\tconst clientUserAgent = request.headers.get(\"user-agent\");\n\n\t\t\tconst response = await fetch(upstreamUrl, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\"X-WaniWani-Stream-Protocol\": \"2\",\n\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t...(clientUserAgent\n\t\t\t\t\t\t? { \"X-Client-User-Agent\": clientUserAgent }\n\t\t\t\t\t\t: {}),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmessages,\n\t\t\t\t\tmcpServerUrl,\n\t\t\t\t\tsessionId,\n\t\t\t\t\tsource,\n\t\t\t\t\tsystemPrompt: effectiveSystemPrompt,\n\t\t\t\t\tmaxSteps,\n\t\t\t\t\tvisitor,\n\t\t\t\t\twebSearch,\n\t\t\t\t}),\n\t\t\t\tsignal: request.signal,\n\t\t\t});\n\n\t\t\tlog(\"upstream response status:\", response.status);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst errorBody = await response.text().catch(() => \"\");\n\t\t\t\tlog(\"← returning\", response.status, \"upstream error:\", errorBody);\n\t\t\t\treturn new Response(errorBody, {\n\t\t\t\t\tstatus: response.status,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\":\n\t\t\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"application/json\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\n\t\t\t// 5. Stream the response back\n\t\t\tconst headers = new Headers({\n\t\t\t\t\"Content-Type\":\n\t\t\t\t\tresponse.headers.get(\"Content-Type\") ?? \"text/event-stream\",\n\t\t\t});\n\t\t\tconst upstreamSessionId = response.headers.get(\"x-session-id\");\n\t\t\tif (upstreamSessionId) {\n\t\t\t\theaders.set(\"x-session-id\", upstreamSessionId);\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"← streaming response\",\n\t\t\t\tresponse.status,\n\t\t\t\t\"body null?\",\n\t\t\t\tresponse.body === null,\n\t\t\t);\n\t\t\treturn new Response(response.body, {\n\t\t\t\tstatus: response.status,\n\t\t\t\theaders,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:chat] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Resource - Serves MCP resource content (HTML widgets)\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createResourceHandler(deps: ResourceHandlerDeps) {\n\tconst { mcpServerUrl: mcpServerUrlOverride, resolveConfig, debug } = deps;\n\n\tconst log = createLogger(\"resource\", debug);\n\n\treturn async function handleResource(url: URL): Promise<Response> {\n\t\tlog(\"→ GET\", url.toString());\n\t\ttry {\n\t\t\tconst uri = url.searchParams.get(\"uri\");\n\t\t\tlog(\"uri:\", uri ?? \"(missing)\");\n\n\t\t\tif (!uri) {\n\t\t\t\tlog(\"← 400 missing uri\");\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{ error: \"Missing uri query parameter\" },\n\t\t\t\t\t{ status: 400 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet createMCPClient: typeof import(\"@ai-sdk/mcp\")[\"createMCPClient\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ createMCPClient }, { StreamableHTTPClientTransport }] =\n\t\t\t\t\tawait Promise.all([\n\t\t\t\t\t\timport(\"@ai-sdk/mcp\"),\n\t\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\n\t\t\t\t\t\"[waniwani:resource] MCP deps import failed:\",\n\t\t\t\t\timportError,\n\t\t\t\t);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP resource handler requires @ai-sdk/mcp and @modelcontextprotocol/sdk. Install them to enable resource serving.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst mcp = await createMCPClient({\n\t\t\t\ttransport: new StreamableHTTPClientTransport(new URL(mcpServerUrl)),\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\tlog(\"reading resource:\", uri);\n\t\t\t\tconst result = await mcp.readResource({ uri });\n\t\t\t\tlog(\"resource contents count:\", result.contents.length);\n\n\t\t\t\tconst content = result.contents[0];\n\t\t\t\tif (!content) {\n\t\t\t\t\tlog(\"← 404 resource not found\");\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource not found\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlet html: string | undefined;\n\t\t\t\tif (\"text\" in content && typeof content.text === \"string\") {\n\t\t\t\t\thtml = content.text;\n\t\t\t\t} else if (\"blob\" in content && typeof content.blob === \"string\") {\n\t\t\t\t\thtml = atob(content.blob);\n\t\t\t\t}\n\n\t\t\t\tif (!html) {\n\t\t\t\t\tlog(\"← 404 resource has no content, keys:\", Object.keys(content));\n\t\t\t\t\treturn Response.json(\n\t\t\t\t\t\t{ error: \"Resource has no content\" },\n\t\t\t\t\t\t{ status: 404 },\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tlog(\"← 200 HTML length:\", html.length);\n\t\t\t\treturn new Response(html, {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": \"text/html\",\n\t\t\t\t\t\t\"Cache-Control\": \"private, max-age=300\",\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait mcp.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:resource] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// Handle Tool - Calls MCP server tools directly and returns JSON results\n\nimport { WaniWaniError } from \"../../error\";\nimport { createLogger } from \"../../utils/logger.js\";\nimport type { ResourceHandlerDeps } from \"./@types\";\n\nexport function createToolHandler(deps: ResourceHandlerDeps) {\n\tconst {\n\t\tmcpServerUrl: mcpServerUrlOverride,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t} = deps;\n\n\tconst log = createLogger(\"tool\", debug);\n\n\treturn async function handleTool(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst body = await request.json();\n\t\t\tconst { name, arguments: args } = body as {\n\t\t\t\tname: string;\n\t\t\t\targuments: Record<string, unknown>;\n\t\t\t};\n\t\t\tconst requestSessionId = request.headers.get(\"x-session-id\")?.trim();\n\n\t\t\tif (!name || typeof name !== \"string\") {\n\t\t\t\tlog(\"← 400 missing tool name\");\n\t\t\t\treturn Response.json({ error: \"Missing tool name\" }, { status: 400 });\n\t\t\t}\n\n\t\t\tlog(\n\t\t\t\t\"tool:\",\n\t\t\t\tname,\n\t\t\t\t\"args:\",\n\t\t\t\tJSON.stringify(args),\n\t\t\t\t\"sessionId:\",\n\t\t\t\trequestSessionId || \"(none)\",\n\t\t\t);\n\n\t\t\tconst mcpServerUrl =\n\t\t\t\tmcpServerUrlOverride ?? (await resolveConfig()).mcpServerUrl;\n\t\t\tlog(\"mcpServerUrl:\", mcpServerUrl);\n\n\t\t\t// Dynamic imports — these are optional peer dependencies\n\t\t\tlet Client: typeof import(\"@modelcontextprotocol/sdk/client/index.js\")[\"Client\"];\n\t\t\tlet StreamableHTTPClientTransport: typeof import(\"@modelcontextprotocol/sdk/client/streamableHttp.js\")[\"StreamableHTTPClientTransport\"];\n\n\t\t\ttry {\n\t\t\t\t[{ Client }, { StreamableHTTPClientTransport }] = await Promise.all([\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/index.js\"),\n\t\t\t\t\timport(\"@modelcontextprotocol/sdk/client/streamableHttp.js\"),\n\t\t\t\t]);\n\t\t\t\tlog(\"MCP deps loaded\");\n\t\t\t} catch (importError) {\n\t\t\t\tconsole.error(\"[waniwani:tool] MCP deps import failed:\", importError);\n\t\t\t\treturn Response.json(\n\t\t\t\t\t{\n\t\t\t\t\t\terror:\n\t\t\t\t\t\t\t\"MCP tool handler requires @modelcontextprotocol/sdk. Install it to enable tool calls.\",\n\t\t\t\t\t},\n\t\t\t\t\t{ status: 501 },\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tlog(\"creating MCP client for\", mcpServerUrl);\n\t\t\tconst transport = new StreamableHTTPClientTransport(\n\t\t\t\tnew URL(mcpServerUrl),\n\t\t\t);\n\t\t\tconst client = new Client({\n\t\t\t\tname: \"waniwani-tool-caller\",\n\t\t\t\tversion: \"1.0.0\",\n\t\t\t});\n\t\t\tawait client.connect(transport);\n\n\t\t\ttry {\n\t\t\t\tlog(\"calling tool:\", name);\n\t\t\t\tconst _meta: Record<string, unknown> = {};\n\t\t\t\tif (requestSessionId) {\n\t\t\t\t\t_meta[\"waniwani/sessionId\"] = requestSessionId;\n\t\t\t\t}\n\t\t\t\tif (source) {\n\t\t\t\t\t_meta[\"waniwani/source\"] = source;\n\t\t\t\t}\n\t\t\t\tconst result = await client.callTool({\n\t\t\t\t\tname,\n\t\t\t\t\targuments: args ?? {},\n\t\t\t\t\t...(Object.keys(_meta).length > 0 ? { _meta } : {}),\n\t\t\t\t} as {\n\t\t\t\t\tname: string;\n\t\t\t\t\targuments: Record<string, unknown>;\n\t\t\t\t\t_meta?: Record<string, unknown>;\n\t\t\t\t});\n\t\t\t\tlog(\"tool result received\");\n\n\t\t\t\treturn Response.json({\n\t\t\t\t\tcontent: result.content,\n\t\t\t\t\tstructuredContent: result.structuredContent,\n\t\t\t\t\t_meta: result._meta,\n\t\t\t\t\tisError: result.isError,\n\t\t\t\t});\n\t\t\t} finally {\n\t\t\t\tawait client.close();\n\t\t\t\tlog(\"MCP client closed\");\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:tool] handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tconst status = error instanceof WaniWaniError ? error.status : 500;\n\t\t\tlog(\"← returning\", status, \"from caught error\");\n\t\t\treturn Response.json({ error: message }, { status });\n\t\t}\n\t};\n}\n","// MCP Config Resolver - Lazy-loads and caches MCP environment config\n\nimport { WaniWaniError } from \"../../error\";\n\ninterface McpEnvironmentConfig {\n\tmcpServerUrl: string;\n}\n\nconst TTL_MS = 5 * 60 * 1000; // 5 minutes\n\nexport function createMcpConfigResolver(\n\tapiUrl: string,\n\tapiKey: string | undefined,\n) {\n\tlet cached: { config: McpEnvironmentConfig; expiresAt: number } | null = null;\n\tlet inflight: Promise<McpEnvironmentConfig> | null = null;\n\n\treturn async function resolve(): Promise<McpEnvironmentConfig> {\n\t\tif (cached && Date.now() < cached.expiresAt) {\n\t\t\treturn cached.config;\n\t\t}\n\n\t\t// Deduplicate concurrent requests (cold start scenario)\n\t\tif (inflight) {\n\t\t\treturn inflight;\n\t\t}\n\n\t\tinflight = (async () => {\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t\"WANIWANI_API_KEY is required for createChatHandler\",\n\t\t\t\t\t401,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst response = await fetch(`${apiUrl}/api/mcp/environments/config`, {\n\t\t\t\tmethod: \"GET\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${apiKey}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text().catch(() => \"\");\n\t\t\t\tthrow new WaniWaniError(\n\t\t\t\t\t`Failed to resolve MCP environment config: ${response.status} ${body}`,\n\t\t\t\t\tresponse.status,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tconst data = (await response.json()) as McpEnvironmentConfig;\n\t\t\tcached = { config: data, expiresAt: Date.now() + TTL_MS };\n\t\t\treturn data;\n\t\t})();\n\n\t\ttry {\n\t\t\treturn await inflight;\n\t\t} finally {\n\t\t\tinflight = null;\n\t\t}\n\t};\n}\n","// API Handler - Composes chat and resource handlers into a unified API handler\n\nimport { createLogger } from \"../../utils/logger.js\";\nimport {\n\ttype ApiHandler,\n\ttype ApiHandlerOptions,\n\tresolveWebSearchConfig,\n} from \"./@types\";\nimport { createCors, createJsonResponse } from \"./@utils\";\nimport { createChatRequestHandler } from \"./handle-chat\";\nimport { createResourceHandler } from \"./handle-resource\";\nimport { createToolHandler } from \"./handle-tool\";\nimport { createMcpConfigResolver } from \"./mcp-config-resolver\";\n\nconst DEFAULT_API_URL = \"https://app.waniwani.ai\";\n\nexport function createApiHandler(options: ApiHandlerOptions = {}): ApiHandler {\n\tconst {\n\t\tapiKey = process.env.WANIWANI_API_KEY,\n\t\tapiUrl = DEFAULT_API_URL,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps = 5,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tdebug = false,\n\t\twebSearch,\n\t} = options;\n\n\tconst log = createLogger(\"router\", debug);\n\tconst cors = createCors();\n\tconst json = createJsonResponse(cors);\n\n\tconst resolveConfig = createMcpConfigResolver(apiUrl, apiKey);\n\n\tconst handleChat = createChatRequestHandler({\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource,\n\t\tsystemPrompt,\n\t\tmaxSteps,\n\t\tbeforeRequest,\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\twebSearch: resolveWebSearchConfig(webSearch),\n\t});\n\n\tconst handleResource = createResourceHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t});\n\n\tconst handleTool = createToolHandler({\n\t\tmcpServerUrl,\n\t\tresolveConfig,\n\t\tdebug,\n\t\tsource,\n\t});\n\n\tconst evalEnabled = process.env.WANIWANI_EVAL === \"1\";\n\n\tasync function routeGet(request: Request): Promise<Response> {\n\t\tlog(\"→ GET\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to scenarios handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\treturn json(data.data ?? data, 200);\n\t\t\t\t} catch {\n\t\t\t\t\treturn json([], 200);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"resource\") {\n\t\t\t\tlog(\"dispatching to resource handler\");\n\t\t\t\tconst response = await handleResource(url);\n\t\t\t\tlog(\"← resource handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\tif (subRoute === \"config\") {\n\t\t\t\tlog(\"dispatching to config handler\");\n\t\t\t\treturn json({ debug, eval: evalEnabled }, 200);\n\t\t\t}\n\n\t\t\tlog(\"← 404 no matching sub-route for\", subRoute);\n\t\t\treturn json({ error: \"Not found\" }, 404);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] GET handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tasync function routePost(request: Request): Promise<Response> {\n\t\tlog(\"→ POST\", request.url);\n\t\ttry {\n\t\t\tconst url = new URL(request.url);\n\t\t\tconst segments = url.pathname\n\t\t\t\t.replace(/\\/$/, \"\")\n\t\t\t\t.split(\"/\")\n\t\t\t\t.filter(Boolean);\n\t\t\tconst subRoute = segments.at(-1);\n\t\t\tlog(\"pathname:\", url.pathname, \"subRoute:\", subRoute);\n\n\t\t\tif (evalEnabled && subRoute === \"scenarios\") {\n\t\t\t\tlog(\"dispatching to save-scenario handler (proxy to app API)\");\n\t\t\t\ttry {\n\t\t\t\t\tconst body = await request.json();\n\t\t\t\t\tconst res = await fetch(`${apiUrl}/api/mcp/scenarios`, {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t\t\t\t...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify(body),\n\t\t\t\t\t});\n\t\t\t\t\tconst data = await res.json();\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\treturn json(\n\t\t\t\t\t\t\t{ error: data.message ?? \"Failed to save scenario\" },\n\t\t\t\t\t\t\tres.status,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn json({ ok: true, scenario: data.data }, 200);\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg =\n\t\t\t\t\t\te instanceof Error ? e.message : \"Failed to save scenario\";\n\t\t\t\t\treturn json({ error: msg }, 400);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (subRoute === \"tool\") {\n\t\t\t\tlog(\"dispatching to tool handler\");\n\t\t\t\tconst response = await handleTool(request);\n\t\t\t\tlog(\"← tool handler returned\", response.status);\n\t\t\t\treturn cors(response);\n\t\t\t}\n\n\t\t\t// Default: treat as chat request\n\t\t\tlog(\"dispatching to chat handler\");\n\t\t\tconst chatResponse = await handleChat(request);\n\t\t\treturn cors(chatResponse);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[waniwani:router] POST handler error:\", error);\n\t\t\tconst message =\n\t\t\t\terror instanceof Error ? error.message : \"Unknown error occurred\";\n\t\t\tlog(\"← 500 from caught error\");\n\t\t\treturn json({ error: message }, 500);\n\t\t}\n\t}\n\n\tfunction handleOptions(): Response {\n\t\treturn cors(new Response(null, { status: 204 }));\n\t}\n\n\treturn {\n\t\thandleChat,\n\t\thandleResource,\n\t\thandleTool,\n\t\trouteGet,\n\t\troutePost,\n\t\thandleOptions,\n\t};\n}\n","// WaniWani SDK - Next.js Adapter\n\nimport type { WaniWaniClient } from \"../../../types.js\";\nimport { createApiHandler } from \"../api-handler.js\";\nimport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\nexport type { NextJsHandlerOptions, NextJsHandlerResult } from \"./@types.js\";\n\n/**\n * Create Next.js route handlers from a WaniWani client.\n *\n * Returns `{ GET, POST }` for use with catch-all routes.\n * Mount at `app/api/waniwani/[[...path]]/route.ts`:\n *\n * - `POST /api/waniwani` → chat (proxied to WaniWani API)\n * - `GET /api/waniwani/resource?uri=…` → MCP resource content\n *\n * @example\n * ```typescript\n * // app/api/waniwani/[[...path]]/route.ts\n * import { waniwani } from \"@waniwani/sdk\";\n * import { toNextJsHandler } from \"@waniwani/sdk/next-js\";\n *\n * const wani = waniwani();\n *\n * export const { GET, POST } = toNextJsHandler(wani, {\n * chat: {\n * systemPrompt: \"You are a helpful assistant.\",\n * mcpServerUrl: process.env.MCP_SERVER_URL!,\n * },\n * });\n * ```\n */\nexport function toNextJsHandler(\n\tclient: WaniWaniClient,\n\toptions: NextJsHandlerOptions,\n): NextJsHandlerResult {\n\tconst { apiKey, apiUrl } = client._config;\n\n\tconst debugEnabled = options?.debug ?? process.env.WANIWANI_DEBUG === \"1\";\n\n\tconst handler = createApiHandler({\n\t\t...options?.chat,\n\t\tapiKey,\n\t\tapiUrl,\n\t\tsource: options?.source,\n\t\tdebug: debugEnabled,\n\t});\n\n\treturn {\n\t\tPOST: handler.routePost,\n\t\tGET: handler.routeGet,\n\t\tOPTIONS: () => handler.handleOptions(),\n\t};\n}\n"],"mappings":"AAQO,SAASA,EACfC,EACAC,EAC+B,CAC/B,OAAOA,EACJ,IAAIC,IAAoB,QAAQ,IAAI,aAAaF,CAAS,IAAK,GAAGE,CAAI,EACtE,IAAM,CAAC,CACX,CC+JO,SAASC,EACfC,EAC8B,CAC9B,GAAIA,IAAU,GACb,MAAO,CAAC,EAET,GAAI,EAAAA,IAAU,IAASA,IAAU,QAGjC,OAAOA,CACR,CCpLO,SAASC,GAA2B,CAC1C,OAAO,SAAmBC,EAA8B,CACvD,OAAAA,EAAS,QAAQ,IAAI,8BAA+B,GAAG,EACvDA,EAAS,QAAQ,IAAI,+BA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IAAMC,EACLD,aAAa,MAAQA,EAAE,QAAU,0BAClC,OAAOvB,EAAK,CAAE,MAAOwB,CAAI,EAAG,GAAG,CAChC,CACD,CAEA,GAAIT,IAAa,OAAQ,CACxBnB,EAAI,6BAA6B,EACjC,IAAMqB,EAAW,MAAMR,EAAWI,CAAO,EACzC,OAAAjB,EAAI,+BAA2BqB,EAAS,MAAM,EACvCnB,EAAKmB,CAAQ,CACrB,CAGArB,EAAI,6BAA6B,EACjC,IAAM6B,EAAe,MAAMrB,EAAWS,CAAO,EAC7C,OAAOf,EAAK2B,CAAY,CACzB,OAASP,EAAO,CACf,QAAQ,MAAM,wCAAyCA,CAAK,EAC5D,IAAMC,EACLD,aAAiB,MAAQA,EAAM,QAAU,yBAC1C,OAAAtB,EAAI,8BAAyB,EACtBI,EAAK,CAAE,MAAOmB,CAAQ,EAAG,GAAG,CACpC,CACD,CAEA,SAASO,GAA0B,CAClC,OAAO5B,EAAK,IAAI,SAAS,KAAM,CAAE,OAAQ,GAAI,CAAC,CAAC,CAChD,CAEA,MAAO,CACN,WAAAM,EACA,eAAAG,EACA,WAAAE,EACA,SAAAG,EACA,UAAAQ,EACA,cAAAM,CACD,CACD,CCrJO,SAASC,GACfC,EACAC,EACsB,CACtB,GAAM,CAAE,OAAAC,EAAQ,OAAAC,CAAO,EAAIH,EAAO,QAE5BI,EAAeH,GAAS,OAAS,QAAQ,IAAI,iBAAmB,IAEhEI,EAAUC,EAAiB,CAChC,GAAGL,GAAS,KACZ,OAAAC,EACA,OAAAC,EACA,OAAQF,GAAS,OACjB,MAAOG,CACR,CAAC,EAED,MAAO,CACN,KAAMC,EAAQ,UACd,IAAKA,EAAQ,SACb,QAAS,IAAMA,EAAQ,cAAc,CACtC,CACD","names":["createLogger","namespace","enabled","args","resolveWebSearchConfig","value","createCors","response","createJsonResponse","cors","data","status","WaniWaniError","message","status","extractGeoFromHeaders","request","h","rawCity","city","safeDecodeURI","country","countryRegion","latitude","longitude","timezone","ip","value","hasModelContext","value","hasContent","hasStructuredContent","formatModelContextForPrompt","value","hasModelContext","sections","renderedBlocks","block","applyModelContextToSystemPrompt","systemPrompt","modelContext","hasModelContext","widgetContext","formatModelContextForPrompt","createChatRequestHandler","deps","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrlOverride","resolveConfig","debug","webSearch","log","createLogger","request","body","messages","sessionId","modelContext","effectiveSystemPrompt","clientVisitorContext","geo","extractGeoFromHeaders","visitor","result","hookError","status","WaniWaniError","message","mcpServerUrl","applyModelContextToSystemPrompt","upstreamUrl","clientUserAgent","response","errorBody","headers","upstreamSessionId","error","createResourceHandler","deps","mcpServerUrlOverride","resolveConfig","debug","log","createLogger","url","uri","mcpServerUrl","createMCPClient","StreamableHTTPClientTransport","importError","mcp","result","content","html","error","message","status","WaniWaniError","createToolHandler","deps","mcpServerUrlOverride","resolveConfig","debug","source","log","createLogger","request","body","name","args","requestSessionId","mcpServerUrl","Client","StreamableHTTPClientTransport","importError","transport","client","_meta","result","error","message","status","WaniWaniError","TTL_MS","createMcpConfigResolver","apiUrl","apiKey","cached","inflight","WaniWaniError","response","body","data","DEFAULT_API_URL","createApiHandler","options","apiKey","apiUrl","source","systemPrompt","maxSteps","beforeRequest","mcpServerUrl","debug","webSearch","log","createLogger","cors","createCors","json","createJsonResponse","resolveConfig","createMcpConfigResolver","handleChat","createChatRequestHandler","resolveWebSearchConfig","handleResource","createResourceHandler","handleTool","createToolHandler","evalEnabled","routeGet","request","url","subRoute","data","response","error","message","routePost","body","res","e","msg","chatResponse","handleOptions","toNextJsHandler","client","options","apiKey","apiUrl","debugEnabled","handler","createApiHandler"]}
@@ -119,11 +119,6 @@ interface ApiHandlerOptions {
119
119
  * Logs request details, response codes, resolved URLs, and caught errors.
120
120
  */
121
121
  debug?: boolean;
122
- /**
123
- * Additional origins allowed for cross-origin requests.
124
- * The WaniWani platform URL (apiUrl) is always included.
125
- */
126
- allowedOrigins?: string[];
127
122
  /**
128
123
  * Enable web search as an additional tool alongside MCP tools.
129
124
  * Pass `true` to enable with defaults, or a config object to restrict domains.
@@ -142,7 +137,7 @@ interface ApiHandler {
142
137
  /** Routes POST sub-paths (e.g. /tool), defaults to chat */
143
138
  routePost: (request: Request) => Promise<Response>;
144
139
  /** Handles CORS preflight requests */
145
- handleOptions: (request?: Request) => Response;
140
+ handleOptions: () => Response;
146
141
  }
147
142
 
148
143
  export { type ApiHandler, type ApiHandlerOptions, type BeforeRequestContext, type BeforeRequestResult, type ClientVisitorContext, type GeoLocation, type VisitorMeta, WaniWaniError, type WebSearchConfig, extractGeoFromHeaders };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waniwani/sdk",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "description": "WaniWani SDK - MCP event tracking, widget framework, and tools",
5
5
  "type": "module",
6
6
  "exports": {