@waniwani/sdk 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mcp/index.d.ts +53 -62
- package/dist/mcp/index.js +3 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react.d.ts +8 -54
- package/dist/mcp/react.js +6 -6
- package/dist/mcp/react.js.map +1 -1
- package/package.json +1 -1
package/dist/mcp/index.d.ts
CHANGED
|
@@ -301,6 +301,52 @@ type RegisteredResource = {
|
|
|
301
301
|
register: (server: McpServer) => Promise<void>;
|
|
302
302
|
};
|
|
303
303
|
|
|
304
|
+
type ToolHandlerContext = {
|
|
305
|
+
/** Raw MCP request extra data (includes _meta for session extraction) */
|
|
306
|
+
extra?: {
|
|
307
|
+
_meta?: Record<string, unknown>;
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
type ToolConfig<TInput extends ZodRawShapeCompat> = {
|
|
311
|
+
/** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
|
|
312
|
+
resource?: RegisteredResource;
|
|
313
|
+
/** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
|
|
314
|
+
id?: string;
|
|
315
|
+
/** Display title. Defaults to resource.title when resource is present, required otherwise. */
|
|
316
|
+
title?: string;
|
|
317
|
+
/** Action-oriented description for the tool (tells the model WHEN to use it) */
|
|
318
|
+
description: string;
|
|
319
|
+
/** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
|
|
320
|
+
widgetDescription?: string;
|
|
321
|
+
/** Input schema using zod */
|
|
322
|
+
inputSchema: TInput;
|
|
323
|
+
/** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
|
|
324
|
+
invoking?: string;
|
|
325
|
+
/** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
|
|
326
|
+
invoked?: string;
|
|
327
|
+
/** Annotations describe the tool's potential impact. */
|
|
328
|
+
annotations?: {
|
|
329
|
+
readOnlyHint?: boolean;
|
|
330
|
+
idempotentHint?: boolean;
|
|
331
|
+
openWorldHint?: boolean;
|
|
332
|
+
destructiveHint?: boolean;
|
|
333
|
+
};
|
|
334
|
+
};
|
|
335
|
+
type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
|
|
336
|
+
/** Text content to return */
|
|
337
|
+
text: string;
|
|
338
|
+
/** Structured data to pass to the widget. Only meaningful when resource is present. */
|
|
339
|
+
data?: Record<string, unknown>;
|
|
340
|
+
}>;
|
|
341
|
+
type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
|
|
342
|
+
type RegisteredTool = {
|
|
343
|
+
id: string;
|
|
344
|
+
title: string;
|
|
345
|
+
description: string;
|
|
346
|
+
/** Register the tool on the server */
|
|
347
|
+
register: (server: McpServer) => Promise<void>;
|
|
348
|
+
};
|
|
349
|
+
|
|
304
350
|
declare const START: "__start__";
|
|
305
351
|
declare const END: "__end__";
|
|
306
352
|
declare const INTERRUPT: unique symbol;
|
|
@@ -329,9 +375,9 @@ type InterruptSignal = {
|
|
|
329
375
|
};
|
|
330
376
|
type WidgetSignal = {
|
|
331
377
|
readonly __type: typeof WIDGET;
|
|
332
|
-
/**
|
|
333
|
-
|
|
334
|
-
/** Data to pass to the
|
|
378
|
+
/** The display tool to delegate rendering to */
|
|
379
|
+
tool: RegisteredTool;
|
|
380
|
+
/** Data to pass to the display tool */
|
|
335
381
|
data: Record<string, unknown>;
|
|
336
382
|
/** Description of what the widget does (for the AI's context) */
|
|
337
383
|
description?: string;
|
|
@@ -370,15 +416,15 @@ declare function interrupt(config: {
|
|
|
370
416
|
context?: string;
|
|
371
417
|
}): InterruptSignal;
|
|
372
418
|
/**
|
|
373
|
-
* Create a widget signal — pauses the flow and
|
|
419
|
+
* Create a widget signal — pauses the flow and delegates rendering to a display tool.
|
|
374
420
|
*
|
|
375
|
-
* The
|
|
376
|
-
*
|
|
421
|
+
* The display tool is a regular `createTool()` with a resource attached.
|
|
422
|
+
* The flow engine will instruct the model to call the display tool separately.
|
|
377
423
|
*
|
|
378
424
|
* Pass `field` to enable auto-skip: if the field is already in state, the widget
|
|
379
425
|
* step will be skipped automatically.
|
|
380
426
|
*/
|
|
381
|
-
declare function showWidget(
|
|
427
|
+
declare function showWidget(tool: RegisteredTool, config: {
|
|
382
428
|
data: Record<string, unknown>;
|
|
383
429
|
description?: string;
|
|
384
430
|
field?: string;
|
|
@@ -437,15 +483,6 @@ type FlowConfig = {
|
|
|
437
483
|
* ```
|
|
438
484
|
*/
|
|
439
485
|
state: Record<string, z.ZodType>;
|
|
440
|
-
/**
|
|
441
|
-
* Widget resource for this flow. When set, the flow tool returns
|
|
442
|
-
* `structuredContent` on every response so the client renders a single
|
|
443
|
-
* widget that dynamically switches based on the current node.
|
|
444
|
-
*
|
|
445
|
-
* Widget nodes render via `FlowWidget`; interrupt/complete/error nodes
|
|
446
|
-
* render an empty 0-height widget.
|
|
447
|
-
*/
|
|
448
|
-
resource?: RegisteredResource;
|
|
449
486
|
/** Optional tool annotations */
|
|
450
487
|
annotations?: {
|
|
451
488
|
readOnlyHint?: boolean;
|
|
@@ -588,52 +625,6 @@ declare function createFlow<const TSchema extends Record<string, z.ZodType>>(con
|
|
|
588
625
|
*/
|
|
589
626
|
declare function createResource(config: ResourceConfig): RegisteredResource;
|
|
590
627
|
|
|
591
|
-
type ToolHandlerContext = {
|
|
592
|
-
/** Raw MCP request extra data (includes _meta for session extraction) */
|
|
593
|
-
extra?: {
|
|
594
|
-
_meta?: Record<string, unknown>;
|
|
595
|
-
};
|
|
596
|
-
};
|
|
597
|
-
type ToolConfig<TInput extends ZodRawShapeCompat> = {
|
|
598
|
-
/** The resource (HTML template) this tool renders. When present, tool returns structuredContent + widget _meta. */
|
|
599
|
-
resource?: RegisteredResource;
|
|
600
|
-
/** Tool identifier. Defaults to resource.id when resource is present, required otherwise. */
|
|
601
|
-
id?: string;
|
|
602
|
-
/** Display title. Defaults to resource.title when resource is present, required otherwise. */
|
|
603
|
-
title?: string;
|
|
604
|
-
/** Action-oriented description for the tool (tells the model WHEN to use it) */
|
|
605
|
-
description: string;
|
|
606
|
-
/** UI component description (describes WHAT the widget displays). Falls back to description. Only relevant when resource is present. */
|
|
607
|
-
widgetDescription?: string;
|
|
608
|
-
/** Input schema using zod */
|
|
609
|
-
inputSchema: TInput;
|
|
610
|
-
/** Optional loading message (defaults to "Loading..."). Only relevant when resource is present. */
|
|
611
|
-
invoking?: string;
|
|
612
|
-
/** Optional loaded message (defaults to "Loaded"). Only relevant when resource is present. */
|
|
613
|
-
invoked?: string;
|
|
614
|
-
/** Annotations describe the tool's potential impact. */
|
|
615
|
-
annotations?: {
|
|
616
|
-
readOnlyHint?: boolean;
|
|
617
|
-
idempotentHint?: boolean;
|
|
618
|
-
openWorldHint?: boolean;
|
|
619
|
-
destructiveHint?: boolean;
|
|
620
|
-
};
|
|
621
|
-
};
|
|
622
|
-
type ToolHandler<TInput extends ZodRawShapeCompat> = (input: ShapeOutput<TInput>, context: ToolHandlerContext) => Promise<{
|
|
623
|
-
/** Text content to return */
|
|
624
|
-
text: string;
|
|
625
|
-
/** Structured data to pass to the widget. Only meaningful when resource is present. */
|
|
626
|
-
data?: Record<string, unknown>;
|
|
627
|
-
}>;
|
|
628
|
-
type ToolToolCallback<TInput extends ZodRawShapeCompat> = ToolCallback<TInput>;
|
|
629
|
-
type RegisteredTool = {
|
|
630
|
-
id: string;
|
|
631
|
-
title: string;
|
|
632
|
-
description: string;
|
|
633
|
-
/** Register the tool on the server */
|
|
634
|
-
register: (server: McpServer) => Promise<void>;
|
|
635
|
-
};
|
|
636
|
-
|
|
637
628
|
/**
|
|
638
629
|
* Creates an MCP tool with minimal boilerplate.
|
|
639
630
|
*
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
2
|
-
`)}function ke(e){if(e.flowToken){let t=
|
|
3
|
-
${a}`,s=t.resource?R({openaiTemplateUri:t.resource.openaiUri,mcpTemplateUri:t.resource.mcpUri,invoking:"Loading...",invoked:"Loaded",autoHeight:t.resource.autoHeight}):void 0;async function d(p,g){let c=ke(p),f=c.state;if(p.action==="start"){let l=o.get(S);if(!l)return{content:{status:"error",error:"No start edge"}};let u={...f,...p.stateUpdates??{}},y=await C(l,u);return O(y,u,n,r,o,g)}if(p.action==="continue"){let l=c.step;if(!l)return{content:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let u={...f,...p.stateUpdates??{}};if(c.questions){let y=te(c.questions,c.interruptContext,l,u);if(y)return y}if(c.questions||c.widgetId){let y=o.get(l);if(!y)return{content:{status:"error",error:`No edge from step "${l}"`}};let w=await C(y,u);return O(w,u,n,r,o,g)}return O(l,u,n,r,o,g)}return{content:{status:"error",error:`Unknown action: "${p.action}"`}}}return{id:t.id,title:t.title,description:i,async register(p){p.registerTool(t.id,{title:t.title,description:i,inputSchema:Se,annotations:t.annotations,...s&&{_meta:s}},(async(g,c)=>{let l=c._meta??{},u=await d(g,l),y=u.flowTokenContent?Q(u.flowTokenContent):void 0,w={...u.content,...y?{flowToken:y}:{}},E=[{type:"text",text:JSON.stringify(w,null,2)}],T=t.resource?{__status:u.content.status,...u.structuredContent??{}}:void 0;return{content:E,...T&&{structuredContent:T},_meta:{...s??{},...l}}}))}}}var x=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===S||t===v)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);let o,a={};if(typeof n=="function")o=n;else if(r)o=r,a=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,a),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),ne({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(S))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(S);if(t?.type==="direct"&&t.to!==v&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==S&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==v&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function re(e){return new x(e)}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:a,widgetDomain:i,prefersBorder:s=!0,autoHeight:d=!0}=e,p=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(p={...p,connect_domains:[...p.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...p.resource_domains||[],`http://${w}:*`]})}catch{}let g=`ui://widgets/apps-sdk/${t}.html`,c=`ui://widgets/ext-apps/${t}.html`,f=null,l=()=>(f||(f=X(o,a)),f),u=r;async function y(w){let E=await l();w.registerResource(`${t}-openai-widget`,g,{title:n,description:u,mimeType:b,_meta:{"openai/widgetDescription":u,"openai/widgetPrefersBorder":s}},async T=>({contents:[{uri:T.href,mimeType:b,text:E,_meta:Z({description:u,prefersBorder:s,widgetDomain:i,widgetCSP:p})}]})),w.registerResource(`${t}-mcp-widget`,c,{title:n,description:u,mimeType:M,_meta:{ui:{prefersBorder:s}}},async T=>({contents:[{uri:T.href,mimeType:M,text:E,_meta:G({description:u,prefersBorder:s,widgetCSP:p})}]}))}return{id:t,title:n,description:r,openaiUri:g,mcpUri:c,autoHeight:d,register:y}}function ve(e,t){let{resource:n,description:r,inputSchema:o,annotations:a}=e,i=e.id??n?.id,s=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!s)throw new Error("createTool: `title` is required when no resource is provided");let d=n?R({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:s,description:r,async register(p){p.registerTool(i,{title:s,description:r,inputSchema:o,annotations:a,...d&&{_meta:d}},(async(g,c)=>{let l=c._meta??{},u=await t(g,{extra:{_meta:l}});return n&&u.data?{content:[{type:"text",text:u.text}],structuredContent:u.data,_meta:{...d,...l}}:{content:[{type:"text",text:u.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var P=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(a,i,s){let d=r(),p=`${t.replace(/\/$/,"")}${i}`,g={Authorization:`Bearer ${d}`,"X-WaniWani-SDK":xe},c={method:a,headers:g};s!==void 0&&(g["Content-Type"]="application/json",c.body=JSON.stringify(s));let f=await fetch(p,c);if(!f.ok){let u=await f.text().catch(()=>"");throw new P(u||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(a){return o("POST","/api/mcp/kb/ingest",{files:a})},async search(a,i){return o("POST","/api/mcp/kb/search",{query:a,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=_e(e),a=W(e.meta),i=W(e.metadata),s=be(e,a),d=k(e.eventId)??r(),p=Me(e.timestamp,n),g=k(e.source)??t.source??Re,c=$(e)?{...e}:void 0,f={...i};return Object.keys(a).length>0&&(f.meta=a),c&&(f.rawLegacy=c),{id:d,type:"mcp.event",name:o,source:g,timestamp:p,correlation:s,properties:Ie(e,o),metadata:f,rawLegacy:c}}function se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ie(e,t){if(!$(e))return W(e.properties);let n=Ce(e,t),r=W(e.properties);return{...n,...r}}function Ce(e,t){switch(t){case"tool.called":{let n={};return k(e.toolName)&&(n.name=e.toolName),k(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),k(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return k(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),k(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function _e(e){return $(e)?e.eventType:e.event}function be(e,t){let n=k(e.requestId)??_(t,["openai/requestId","requestId","mcp/requestId"]),r=k(e.sessionId)??_(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=k(e.traceId)??_(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),a=k(e.externalUserId)??_(t,["openai/userId","externalUserId","userId","actorId"]),i=k(e.correlationId)??_(t,["correlationId","openai/requestId"])??n,s={};return r&&(s.sessionId=r),o&&(s.traceId=o),n&&(s.requestId=n),i&&(s.correlationId=i),a&&(s.externalUserId=a),s}function Me(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function _(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function W(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function k(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Ae=new Set([408,425,429,500,502,503,504]);function ce(e){return new L(e)}var L=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=De(t.baseUrl,t.endpointPath??Pe),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(a){return{kind:"retryable",reason:Ue(a)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Ae.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ae,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],a=[];for(let i of n){let s=r.get(i.eventId);if(s){if(Fe(i)){o.push(s);continue}a.push(s)}}return{retryable:o,permanent:a}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function Fe(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function de(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let a=n?ce({baseUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,i={async track(s){o();let d=j(s);return a?.enqueue(d),{eventId:d.id}},async flush(){o(),await a?.flush()},async shutdown(s){return o(),await a?.shutdown({timeoutMs:s?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return a&&Be(i,r.shutdownTimeoutMs),i}function Be(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function A(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},a=de(o),i=ie(o);return{...a,kb:i,_config:o}}function Oe(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=A(t)),n}return async function(a){let i;try{i=await a.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let s=r(),d=[];for(let p of i.events){let g=Oe(p),c=await s.track(g);d.push(c.eventId)}return await s.flush(),new Response(JSON.stringify({ok:!0,accepted:d.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(s){let d=s instanceof Error?s.message:"Unknown error";return new Response(JSON.stringify({error:d}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=je(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let a=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!a.ok)return null;let i=await a.json(),s=i.data&&typeof i.data=="object"?i.data:i,d=new Date(s.expiresAt).getTime();return!s.token||Number.isNaN(d)?null:(this.cached={token:s.token,expiresAt:d},s.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??A(t.config),o=t.injectWidgetToken!==!1,a=null;function i(){if(a)return a;let d=r._config.apiKey;return d?(a=new F({baseUrl:r._config.baseUrl??ue,apiKey:d}),a):null}let s=e.registerTool.bind(e);return n.registerTool=((...d)=>{let[p,g,c]=d,f=typeof p=="string"&&p.trim().length>0?p:"unknown";if(typeof c!="function")return s(...d);let l=c;return s(p,g,async(y,w)=>{let E=performance.now();try{let T=await l(y,w),D=Math.round(performance.now()-E);return await le(r,pe(f,w,t,{durationMs:D,status:"ok"}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),o&&await Le(T,i(),r._config.baseUrl??ue,t.onError),T}catch(T){let D=Math.round(performance.now()-E);throw await le(r,pe(f,w,t,{durationMs:D,status:"error",errorMessage:T instanceof Error?T.message:String(T)}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),T}})}),n}async function Le(e,t,n,r){if(!N(e))return;N(e._meta)||(e._meta={});let o=e._meta,a={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(a.token=i)}catch(i){r?.(H(i))}o.waniwani=a}function pe(e,t,n,r){let o=He(e,n.toolType),a=Ve(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:a,metadata:{source:"withWaniwani",...n.metadata??{}}}}function He(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function Ve(e){if(!N(e))return;let t=e._meta;if(N(t))return t}function N(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function le(e,t,n){try{await e.track(t)}catch(r){n?.(H(r))}}async function fe(e,t){try{await e.flush()}catch(n){t?.(H(n))}}function H(e){return e instanceof Error?e:new Error(String(e))}export{v as END,S as START,x as StateGraph,re as createFlow,oe as createResource,ve as createTool,qe as createTrackingRoute,U as detectPlatform,z as interrupt,he as isMCPApps,ge as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
|
|
1
|
+
function D(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ge(){return D()==="openai"}function he(){return D()==="mcp-apps"}var k="__start__",S="__end__",U=Symbol.for("waniwani.flow.interrupt"),H=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:U,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:U,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:H,tool:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===U}function J(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===H}import{z as R}from"zod";import{deflateSync as me,inflateSync as we}from"zlib";function X(e){let t=JSON.stringify(e);return me(t).toString("base64")}function Z(e){try{let t=Buffer.from(e,"base64"),n=we(t).toString("utf-8");return JSON.parse(n)}catch{return null}}function ye(e){let t=e.description??"",n=e._zod?.def;if(n?.type==="enum"&&n.entries){let r=Object.keys(n.entries).map(o=>`"${o}"`).join(" | ");return t?`${r} \u2014 ${t}`:r}return t}function Te(e){let t=["","## FLOW EXECUTION PROTOCOL","","This tool implements a multi-step conversational flow. Follow this protocol exactly:","",'1. Call with `action: "start"` to begin. If the user\'s message already'," contains answers to likely questions, extract them into `stateUpdates`"," as `{ field: value }` pairs. The engine will auto-skip steps whose"," fields are already filled."," Only extract values the user explicitly stated \u2014 do NOT guess or invent values."];if(e.state){let n=Object.entries(e.state).map(([r,o])=>{let s=ye(o);return s?`\`${r}\` (${s})`:`\`${r}\``}).join(", ");t.push(` Known fields: ${n}.`)}return t.push("2. The response JSON `status` field tells you what to do next:",' - `"interrupt"`: Pause and ask the user. Two forms:'," a. Single question: `{ question, field, context? }` \u2014 ask `question`, store answer in `field`."," b. Multi-question: `{ questions: [{question, field}, ...], context? }` \u2014 ask ALL questions"," in one conversational message, collect all answers."," `context` (if present) is hidden AI instructions \u2014 use to shape your response, do NOT show verbatim."," Then call again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response (pass back exactly as received),'," `stateUpdates` = answers keyed by their `field` names, plus any other fields the user mentioned.",' - `"widget"`: The flow wants to show a UI widget. Call the tool named in the `tool`'," field, passing the `data` object as the tool's input."," Present the widget result to the user. When the user makes a choice or interacts"," with the widget, call THIS flow tool again with:",' `action: "continue"`, `flowToken` = the `flowToken` from the response,'," `stateUpdates` = `{ [field]: <user's selection> }` plus any other fields the user mentioned.",' - `"complete"`: The flow is done. Present the result to the user.',' - `"error"`: Something went wrong. Show the `error` message.',"","3. ALWAYS pass back the `flowToken` string exactly as received \u2014 it is an opaque token, do not modify it.","4. Do NOT invent state values. Only use `stateUpdates` for information the user explicitly provided.","5. Include only the fields the user actually answered in `stateUpdates` \u2014 do NOT guess missing ones."," If the user did not answer all pending questions, the engine will re-prompt for the remaining ones."," If the user mentioned values for other known fields, include those too \u2014"," they will be applied immediately and those steps will be auto-skipped."),t.join(`
|
|
2
|
+
`)}function ke(e){if(e.flowToken){let t=Z(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function I(e,t){return e.type==="direct"?e.to:e.condition(t)}function O(e){return e!=null&&e!==""}function G(e,t,n,r){if(e.every(c=>O(r[c.field])))return null;let o=e.filter(c=>!O(r[c.field])),s=o.length===1,i=o[0];return{content:s&&i?{status:"interrupt",question:i.question,field:i.field,...i.suggestions?{suggestions:i.suggestions}:{},...i.context||t?{context:i.context??t}:{}}:{status:"interrupt",questions:o,...t?{context:t}:{}},flowTokenContent:{step:n,state:r,...s&&i?{field:i.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function B(e,t,n,r,o,s){let i=e,a={...t},c=50,f=0;for(;f++<c;){if(i===S)return{content:{status:"complete"},flowTokenContent:{state:a}};let g=n.get(i);if(!g)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let d=await g(a,s);if(Y(d)){let l=G(d.questions,d.context,i,a);if(l)return l;let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}if(J(d)){let l=d.field??r.get(i)?.field;if(l&&O(a[l])){let p=o.get(i);if(!p)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(p,a);continue}return{content:{status:"widget",tool:d.tool.id,data:d.data,description:d.description},flowTokenContent:{step:i,state:a,field:l,widgetId:d.tool.id},structuredContent:d.data}}a={...a,...d};let u=o.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await I(u,a)}catch(d){return{content:{status:"error",error:d instanceof Error?d.message:String(d)},flowTokenContent:{step:i,state:a}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Se={action:R.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:R.record(R.string(),R.unknown()).optional().describe("State field values to set before processing the next node. Use this to pass the user's answer (keyed by the field name from the response) and any other values the user mentioned."),flowToken:R.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function Q(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,s=Te(t),i=`${t.description}
|
|
3
|
+
${s}`;async function a(c,f){let g=ke(c),d=g.state;if(c.action==="start"){let u=o.get(k);if(!u)return{content:{status:"error",error:"No start edge"}};let l={...d,...c.stateUpdates??{}},p=await I(u,l);return B(p,l,n,r,o,f)}if(c.action==="continue"){let u=g.step;if(!u)return{content:{status:"error",error:'Missing or invalid "flowToken" for continue action. Pass back the flowToken from the previous response exactly as received.'}};let l={...d,...c.stateUpdates??{}};if(g.questions){let p=G(g.questions,g.interruptContext,u,l);if(p)return p}if(g.questions||g.widgetId){let p=o.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let v=await I(p,l);return B(v,l,n,r,o,f)}return B(u,l,n,r,o,f)}return{content:{status:"error",error:`Unknown action: "${c.action}"`}}}return{id:t.id,title:t.title,description:i,async register(c){c.registerTool(t.id,{title:t.title,description:i,inputSchema:Se,annotations:t.annotations},(async(f,g)=>{let u=g._meta??{},l=await a(f,u),p=l.flowTokenContent?X(l.flowTokenContent):void 0,v={...l.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(v,null,2)}],...l.structuredContent&&{structuredContent:l.structuredContent},_meta:u}}))}}}var E=class{nodes=new Map;nodeConfigs=new Map;edges=new Map;config;constructor(t){this.config=t}addNode(t,n,r){if(t===k||t===S)throw new Error(`"${t}" is a reserved name and cannot be used as a node name`);if(this.nodes.has(t))throw new Error(`Node "${t}" already exists`);let o,s={};if(typeof n=="function")o=n;else if(r)o=r,s=n;else throw new Error(`Node "${t}" requires a handler function.`);return this.nodes.set(t,o),this.nodeConfigs.set(t,s),this}addEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge. Use addConditionalEdge for branching.`);return this.edges.set(t,{type:"direct",to:n}),this}addConditionalEdge(t,n){if(this.edges.has(t))throw new Error(`Node "${t}" already has an outgoing edge.`);return this.edges.set(t,{type:"conditional",condition:n}),this}compile(){return this.validate(),Q({config:this.config,nodes:new Map(this.nodes),nodeConfigs:new Map(this.nodeConfigs),edges:new Map(this.edges)})}validate(){if(!this.edges.has(k))throw new Error('Flow must have an entry point. Add an edge from START: .addEdge(START, "first_node")');let t=this.edges.get(k);if(t?.type==="direct"&&t.to!==S&&!this.nodes.has(t.to))throw new Error(`START edge references non-existent node: "${t.to}"`);for(let[n,r]of this.edges){if(n!==k&&!this.nodes.has(n))throw new Error(`Edge from non-existent node: "${n}"`);if(r.type==="direct"&&r.to!==S&&!this.nodes.has(r.to))throw new Error(`Edge from "${n}" references non-existent node: "${r.to}"`)}for(let[n]of this.nodes)if(!this.edges.has(n))throw new Error(`Node "${n}" has no outgoing edge. Add one with .addEdge("${n}", ...) or .addConditionalEdge("${n}", ...)`)}};function ee(e){return new E(e)}var b="text/html+skybridge",_="text/html;profile=mcp-app",te=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function ne(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function re(e){let t=e.widgetCSP?{connectDomains:e.widgetCSP.connect_domains,resourceDomains:e.widgetCSP.resource_domains,frameDomains:e.widgetCSP.frame_domains,redirectDomains:e.widgetCSP.redirect_domains}:void 0;return{ui:{...t&&{csp:t},...e.widgetDomain&&{domain:e.widgetDomain},...e.prefersBorder!==void 0&&{prefersBorder:e.prefersBorder}}}}function q(e){return{...e.openaiTemplateUri&&{"openai/outputTemplate":e.openaiTemplateUri},"openai/toolInvocation/invoking":e.invoking,"openai/toolInvocation/invoked":e.invoked,"openai/widgetAccessible":!0,"openai/resultCanProduceWidget":!0,...e.mcpTemplateUri&&{ui:{resourceUri:e.mcpTemplateUri,...e.autoHeight&&{autoHeight:!0}}}}}function oe(e){let{id:t,title:n,description:r,baseUrl:o,htmlPath:s,widgetDomain:i,prefersBorder:a=!0,autoHeight:c=!0}=e,f=e.widgetCSP??{connect_domains:[o],resource_domains:[o]};if(process.env.NODE_ENV==="development")try{let{hostname:w}=new URL(o);(w==="localhost"||w==="127.0.0.1")&&(f={...f,connect_domains:[...f.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...f.resource_domains||[],`http://${w}:*`]})}catch{}let g=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,u=null,l=()=>(u||(u=te(o,s)),u),p=r;async function v(w){let x=await l();w.registerResource(`${t}-openai-widget`,g,{title:n,description:p,mimeType:b,_meta:{"openai/widgetDescription":p,"openai/widgetPrefersBorder":a}},async T=>({contents:[{uri:T.href,mimeType:b,text:x,_meta:ne({description:p,prefersBorder:a,widgetDomain:i,widgetCSP:f})}]})),w.registerResource(`${t}-mcp-widget`,d,{title:n,description:p,mimeType:_,_meta:{ui:{prefersBorder:a}}},async T=>({contents:[{uri:T.href,mimeType:_,text:x,_meta:re({description:p,prefersBorder:a,widgetCSP:f})}]}))}return{id:t,title:n,description:r,openaiUri:g,mcpUri:d,autoHeight:c,register:v}}function ve(e,t){let{resource:n,description:r,inputSchema:o,annotations:s}=e,i=e.id??n?.id,a=e.title??n?.title;if(!i)throw new Error("createTool: `id` is required when no resource is provided");if(!a)throw new Error("createTool: `title` is required when no resource is provided");let c=n?q({openaiTemplateUri:n.openaiUri,mcpTemplateUri:n.mcpUri,invoking:e.invoking??"Loading...",invoked:e.invoked??"Loaded",autoHeight:n.autoHeight}):void 0;return{id:i,title:a,description:r,async register(f){f.registerTool(i,{title:a,description:r,inputSchema:o,annotations:s,...c&&{_meta:c}},(async(g,d)=>{let l=d._meta??{},p=await t(g,{extra:{_meta:l}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...c,...l}}:{content:[{type:"text",text:p.text}]}}))}}}async function Ee(e,t){await Promise.all(t.map(n=>n.register(e)))}var M=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};var xe="@waniwani/sdk";function ie(e){let{baseUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function o(s,i,a){let c=r(),f=`${t.replace(/\/$/,"")}${i}`,g={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":xe},d={method:s,headers:g};a!==void 0&&(g["Content-Type"]="application/json",d.body=JSON.stringify(a));let u=await fetch(f,d);if(!u.ok){let p=await u.text().catch(()=>"");throw new M(p||`KB API error: HTTP ${u.status}`,u.status)}return(await u.json()).data}return{async ingest(s){return o("POST","/api/mcp/kb/ingest",{files:s})},async search(s,i){return o("POST","/api/mcp/kb/search",{query:s,...i})},async sources(){return o("GET","/api/mcp/kb/sources")}}}var Re="@waniwani/sdk";function j(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??se,o=be(e),s=P(e.meta),i=P(e.metadata),a=_e(e,s),c=y(e.eventId)??r(),f=Me(e.timestamp,n),g=y(e.source)??t.source??Re,d=$(e)?{...e}:void 0,u={...i};return Object.keys(s).length>0&&(u.meta=s),d&&(u.rawLegacy=d),{id:c,type:"mcp.event",name:o,source:g,timestamp:f,correlation:a,properties:Ie(e,o),metadata:u,rawLegacy:d}}function se(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function Ie(e,t){if(!$(e))return P(e.properties);let n=Ce(e,t),r=P(e.properties);return{...n,...r}}function Ce(e,t){switch(t){case"tool.called":{let n={};return y(e.toolName)&&(n.name=e.toolName),y(e.toolType)&&(n.type=e.toolType),n}case"quote.succeeded":{let n={};return typeof e.quoteAmount=="number"&&(n.amount=e.quoteAmount),y(e.quoteCurrency)&&(n.currency=e.quoteCurrency),n}case"link.clicked":{let n={};return y(e.linkUrl)&&(n.url=e.linkUrl),n}case"purchase.completed":{let n={};return typeof e.purchaseAmount=="number"&&(n.amount=e.purchaseAmount),y(e.purchaseCurrency)&&(n.currency=e.purchaseCurrency),n}default:return{}}}function be(e){return $(e)?e.eventType:e.event}function _e(e,t){let n=y(e.requestId)??C(t,["openai/requestId","requestId","mcp/requestId"]),r=y(e.sessionId)??C(t,["openai/sessionId","sessionId","conversationId","anthropic/sessionId"]),o=y(e.traceId)??C(t,["openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"]),s=y(e.externalUserId)??C(t,["openai/userId","externalUserId","userId","actorId"]),i=y(e.correlationId)??C(t,["correlationId","openai/requestId"])??n,a={};return r&&(a.sessionId=r),o&&(a.traceId=o),n&&(a.requestId=n),i&&(a.correlationId=i),s&&(a.externalUserId=s),a}function Me(e,t){if(e instanceof Date)return e.toISOString();if(typeof e=="string"){let n=new Date(e);if(!Number.isNaN(n.getTime()))return n.toISOString()}return t().toISOString()}function C(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.trim().length>0)return r}}function P(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function y(e){if(typeof e=="string"&&e.trim().length!==0)return e}function $(e){return"eventType"in e}var Pe="/api/mcp/events/v2/batch";var ae="@waniwani/sdk",We=new Set([401,403]),Fe=new Set([408,425,429,500,502,503,504]);function ce(e){return new L(e)}var L=class{endpointUrl;flushIntervalMs;maxBatchSize;maxBufferSize;maxRetries;retryBaseDelayMs;retryMaxDelayMs;shutdownTimeoutMs;sdkVersion;fetchFn;logger;now;sleep;apiKey;buffer=[];flushTimer;flushScheduled=!1;flushScheduledTimer;flushInFlight;inFlightCount=0;isStopped=!1;isShuttingDown=!1;constructor(t){this.endpointUrl=De(t.baseUrl,t.endpointPath??Pe),this.flushIntervalMs=t.flushIntervalMs??1e3,this.maxBatchSize=t.maxBatchSize??20,this.maxBufferSize=t.maxBufferSize??1e3,this.maxRetries=t.maxRetries??3,this.retryBaseDelayMs=t.retryBaseDelayMs??200,this.retryMaxDelayMs=t.retryMaxDelayMs??2e3,this.shutdownTimeoutMs=t.shutdownTimeoutMs??2e3,this.fetchFn=t.fetchFn??fetch,this.logger=t.logger??console,this.now=t.now??(()=>new Date),this.sleep=t.sleep??(n=>new Promise(r=>setTimeout(r,n))),this.apiKey=t.apiKey,this.sdkVersion=t.sdkVersion,this.flushIntervalMs>0&&(this.flushTimer=setInterval(()=>{this.flush()},this.flushIntervalMs))}enqueue(t){if(this.isStopped||this.isShuttingDown){this.logger.warn("[WaniWani] Tracking transport is stopped, dropping event %s",t.id);return}if(this.buffer.length>=this.maxBufferSize){let n=this.buffer.length-this.maxBufferSize+1;this.buffer.splice(0,n),this.logger.warn("[WaniWani] Tracking buffer overflow, dropped %d oldest event(s)",n)}if(this.buffer.push(t),this.buffer.length>=this.maxBatchSize){this.flush();return}this.scheduleMicroFlush()}pendingEvents(){return this.buffer.length+this.inFlightCount}async flush(){return this.flushInFlight?this.flushInFlight:(this.flushInFlight=this.flushLoop().finally(()=>{this.flushInFlight=void 0}),this.flushInFlight)}async shutdown(t){this.isShuttingDown=!0,this.flushTimer&&(clearInterval(this.flushTimer),this.flushTimer=void 0),this.flushScheduledTimer&&(clearTimeout(this.flushScheduledTimer),this.flushScheduledTimer=void 0,this.flushScheduled=!1);let n=t?.timeoutMs??this.shutdownTimeoutMs,r=this.flush();if(!Number.isFinite(n)||n<=0)return await r,this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()};let o=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>o)])===o?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}scheduleMicroFlush(){this.flushScheduled||(this.flushScheduled=!0,this.flushScheduledTimer=setTimeout(()=>{this.flushScheduledTimer=void 0,this.flushScheduled=!1,this.flush()},0))}async flushLoop(){for(;this.buffer.length>0&&!this.isStopped;){let t=this.buffer.splice(0,this.maxBatchSize);await this.sendBatchWithRetry(t)}}async sendBatchWithRetry(t){let n=0,r=t;for(;r.length>0&&!this.isStopped;){this.inFlightCount=r.length;let o=await this.sendBatchOnce(r);switch(this.inFlightCount=0,o.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(o.status,r.length);return;case"permanent":this.logger.error("[WaniWani] Dropping %d event(s) after permanent failure: %s",r.length,o.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d event(s) after retry exhaustion: %s",r.length,o.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(o.permanent.length>0&&this.logger.error("[WaniWani] Dropping %d event(s) rejected as permanent",o.permanent.length),o.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[WaniWani] Dropping %d retryable event(s) after retry exhaustion",o.retryable.length);return}r=o.retryable,await this.sleep(this.backoffDelayMs(n)),n+=1;continue}}}async sendBatchOnce(t){let n;try{n=await this.fetchFn(this.endpointUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,"X-WaniWani-SDK":ae},body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Ue(s)}}if(We.has(n.status))return{kind:"auth",status:n.status};if(Fe.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await Ne(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let o=this.classifyRejectedEvents(t,r.rejected);return o.retryable.length===0&&o.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:o.retryable,permanent:o.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:ae,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(i=>[i.id,i])),o=[],s=[];for(let i of n){let a=r.get(i.eventId);if(a){if(Ae(i)){o.push(a);continue}s.push(a)}}return{retryable:o,permanent:s}}backoffDelayMs(t){let n=this.retryBaseDelayMs*2**t;return Math.min(n,this.retryMaxDelayMs)}stopTransportForAuthFailure(t,n){this.isStopped=!0;let r=this.buffer.length;this.buffer.splice(0,r),this.logger.error("[WaniWani] Auth failure (HTTP %d). Stopping tracking transport and dropping %d queued event(s)",t,n+r)}};function Ae(e){if(e.retryable===!0)return!0;let t=e.code.toLowerCase();return t.includes("timeout")||t.includes("temporary")||t.includes("unavailable")||t.includes("rate_limit")||t.includes("transient")||t.includes("server")}async function Ne(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function De(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Ue(e){return e instanceof Error?e.message:String(e)}function de(e){let{baseUrl:t,apiKey:n,tracking:r}=e;function o(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?ce({baseUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0,i={async track(a){o();let c=j(a);return s?.enqueue(c),{eventId:c.id}},async flush(){o(),await s?.flush()},async shutdown(a){return o(),await s?.shutdown({timeoutMs:a?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&Be(i,r.shutdownTimeoutMs),i}function Be(e,t){if(typeof process>"u"||typeof process.once!="function"||typeof process.on!="function")return;let n=()=>{e.shutdown({timeoutMs:t})};process.once("beforeExit",n),process.once("SIGINT",n),process.once("SIGTERM",n)}function W(e){let t=e?.baseUrl??"https://app.waniwani.ai",n=e?.apiKey??process.env.WANIWANI_API_KEY,r={endpointPath:e?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:e?.tracking?.flushIntervalMs??1e3,maxBatchSize:e?.tracking?.maxBatchSize??20,maxBufferSize:e?.tracking?.maxBufferSize??1e3,maxRetries:e?.tracking?.maxRetries??3,retryBaseDelayMs:e?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:e?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:e?.tracking?.shutdownTimeoutMs??2e3},o={baseUrl:t,apiKey:n,tracking:r},s=de(o),i=ie(o);return{...s,kb:i,_config:o}}function Oe(e){let t=e.event_type??"widget_click",r=t.startsWith("widget_")?t:`widget_${t}`,o={...e.metadata??{}};return e.event_name&&(o.event_name=e.event_name),{event:r,properties:o,sessionId:e.session_id,traceId:e.trace_id,externalUserId:e.user_id,eventId:e.event_id,timestamp:e.timestamp,source:e.source??"widget"}}function qe(e){let t={apiKey:e?.apiKey,baseUrl:e?.baseUrl},n;function r(){return n||(n=W(t)),n}return async function(s){let i;try{i=await s.json()}catch{return new Response(JSON.stringify({error:"Invalid JSON"}),{status:400,headers:{"Content-Type":"application/json"}})}if(!Array.isArray(i.events)||i.events.length===0)return new Response(JSON.stringify({error:"Missing or empty events array"}),{status:400,headers:{"Content-Type":"application/json"}});try{let a=r(),c=[];for(let f of i.events){let g=Oe(f),d=await a.track(g);c.push(d.eventId)}return await a.flush(),new Response(JSON.stringify({ok:!0,accepted:c.length}),{status:200,headers:{"Content-Type":"application/json"}})}catch(a){let c=a instanceof Error?a.message:"Unknown error";return new Response(JSON.stringify({error:c}),{status:500,headers:{"Content-Type":"application/json"}})}}}var F=class{cached=null;pending=null;config;constructor(t){this.config=t}async getToken(t,n){return this.cached&&Date.now()<this.cached.expiresAt-12e4?this.cached.token:this.pending?this.pending:(this.pending=this.mint(t,n).finally(()=>{this.pending=null}),this.pending)}async mint(t,n){let r=je(this.config.baseUrl,"/api/mcp/widget-tokens"),o={};t&&(o.sessionId=t),n&&(o.traceId=n);try{let s=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.config.apiKey}`},body:JSON.stringify(o)});if(!s.ok)return null;let i=await s.json(),a=i.data&&typeof i.data=="object"?i.data:i,c=new Date(a.expiresAt).getTime();return!a.token||Number.isNaN(c)?null:(this.cached={token:a.token,expiresAt:c},a.token)}catch{return null}}};function je(e,t){return`${e.endsWith("/")?e.slice(0,-1):e}${t}`}var ue="https://app.waniwani.ai";function $e(e,t={}){let n=e;if(n.__waniwaniWrapped)return n;n.__waniwaniWrapped=!0;let r=t.client??W(t.config),o=t.injectWidgetToken!==!1,s=null;function i(){if(s)return s;let c=r._config.apiKey;return c?(s=new F({baseUrl:r._config.baseUrl??ue,apiKey:c}),s):null}let a=e.registerTool.bind(e);return n.registerTool=((...c)=>{let[f,g,d]=c,u=typeof f=="string"&&f.trim().length>0?f:"unknown";if(typeof d!="function")return a(...c);let l=d;return a(f,g,async(v,w)=>{let x=performance.now();try{let T=await l(v,w),N=Math.round(performance.now()-x);return await le(r,pe(u,w,t,{durationMs:N,status:"ok"}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),o&&await Le(T,i(),r._config.baseUrl??ue,t.onError),T}catch(T){let N=Math.round(performance.now()-x);throw await le(r,pe(u,w,t,{durationMs:N,status:"error",errorMessage:T instanceof Error?T.message:String(T)}),t.onError),t.flushAfterToolCall&&await fe(r,t.onError),T}})}),n}async function Le(e,t,n,r){if(!A(e))return;A(e._meta)||(e._meta={});let o=e._meta,s={endpoint:`${n.replace(/\/$/,"")}/api/mcp/events/v2/batch`};if(t)try{let i=await t.getToken();i&&(s.token=i)}catch(i){r?.(V(i))}o.waniwani=s}function pe(e,t,n,r){let o=Ve(e,n.toolType),s=He(t);return{event:"tool.called",properties:{name:e,type:o,...r??{}},meta:s,metadata:{source:"withWaniwani",...n.metadata??{}}}}function Ve(e,t){return typeof t=="function"?t(e)??"other":t??"other"}function He(e){if(!A(e))return;let t=e._meta;if(A(t))return t}function A(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}async function le(e,t,n){try{await e.track(t)}catch(r){n?.(V(r))}}async function fe(e,t){try{await e.flush()}catch(n){t?.(V(n))}}function V(e){return e instanceof Error?e:new Error(String(e))}export{S as END,k as START,E as StateGraph,ee as createFlow,oe as createResource,ve as createTool,qe as createTrackingRoute,D as detectPlatform,z as interrupt,he as isMCPApps,ge as isOpenAI,Ee as registerTools,K as showWidget,$e as withWaniwani};
|
|
4
4
|
//# sourceMappingURL=index.js.map
|