@waniwani/sdk 0.2.2 → 0.2.3
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 +62 -53
- package/dist/mcp/index.js +3 -3
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react.d.ts +49 -8
- 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,52 +301,6 @@ 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
|
-
|
|
350
304
|
declare const START: "__start__";
|
|
351
305
|
declare const END: "__end__";
|
|
352
306
|
declare const INTERRUPT: unique symbol;
|
|
@@ -375,9 +329,9 @@ type InterruptSignal = {
|
|
|
375
329
|
};
|
|
376
330
|
type WidgetSignal = {
|
|
377
331
|
readonly __type: typeof WIDGET;
|
|
378
|
-
/**
|
|
379
|
-
|
|
380
|
-
/** Data to pass to the
|
|
332
|
+
/** Identifier for the widget component to render on the client */
|
|
333
|
+
widgetId: string;
|
|
334
|
+
/** Data to pass to the widget component */
|
|
381
335
|
data: Record<string, unknown>;
|
|
382
336
|
/** Description of what the widget does (for the AI's context) */
|
|
383
337
|
description?: string;
|
|
@@ -416,15 +370,15 @@ declare function interrupt(config: {
|
|
|
416
370
|
context?: string;
|
|
417
371
|
}): InterruptSignal;
|
|
418
372
|
/**
|
|
419
|
-
* Create a widget signal — pauses the flow and
|
|
373
|
+
* Create a widget signal — pauses the flow and renders a widget inline.
|
|
420
374
|
*
|
|
421
|
-
* The
|
|
422
|
-
*
|
|
375
|
+
* The `widgetId` identifies which component to render on the client side
|
|
376
|
+
* via the `FlowWidget` factory component.
|
|
423
377
|
*
|
|
424
378
|
* Pass `field` to enable auto-skip: if the field is already in state, the widget
|
|
425
379
|
* step will be skipped automatically.
|
|
426
380
|
*/
|
|
427
|
-
declare function showWidget(
|
|
381
|
+
declare function showWidget(widgetId: string, config: {
|
|
428
382
|
data: Record<string, unknown>;
|
|
429
383
|
description?: string;
|
|
430
384
|
field?: string;
|
|
@@ -483,6 +437,15 @@ type FlowConfig = {
|
|
|
483
437
|
* ```
|
|
484
438
|
*/
|
|
485
439
|
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;
|
|
486
449
|
/** Optional tool annotations */
|
|
487
450
|
annotations?: {
|
|
488
451
|
readOnlyHint?: boolean;
|
|
@@ -625,6 +588,52 @@ declare function createFlow<const TSchema extends Record<string, z.ZodType>>(con
|
|
|
625
588
|
*/
|
|
626
589
|
declare function createResource(config: ResourceConfig): RegisteredResource;
|
|
627
590
|
|
|
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
|
+
|
|
628
637
|
/**
|
|
629
638
|
* Creates an MCP tool with minimal boilerplate.
|
|
630
639
|
*
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
2
|
-
`)}function ke(e){if(e.flowToken){let t=
|
|
3
|
-
${s}`;async function a(c,l){let f=ke(c),d=f.state;if(c.action==="start"){let u=o.get(k);if(!u)return{content:{status:"error",error:"No start edge"}};let g={...d,...c.stateUpdates??{}},p=await I(u,g);return B(p,g,n,r,o,l)}if(c.action==="continue"){let u=f.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 g={...d,...c.stateUpdates??{}};if(f.questions){let p=G(f.questions,f.interruptContext,u,g);if(p)return p}if(f.questions||f.widgetId){let p=o.get(u);if(!p)return{content:{status:"error",error:`No edge from step "${u}"`}};let v=await I(p,g);return B(v,g,n,r,o,l)}return B(u,g,n,r,o,l)}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(l,f)=>{let u=f._meta??{},g=await a(l,u),p=g.flowTokenContent?X(g.flowTokenContent):void 0,v={...g.content,...p?{flowToken:p}:{}};return{content:[{type:"text",text:JSON.stringify(v,null,2)}],...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,l=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")&&(l={...l,connect_domains:[...l.connect_domains||[],`ws://${w}:*`,`wss://${w}:*`],resource_domains:[...l.resource_domains||[],`http://${w}:*`]})}catch{}let f=`ui://widgets/apps-sdk/${t}.html`,d=`ui://widgets/ext-apps/${t}.html`,u=null,g=()=>(u||(u=te(o,s)),u),p=r;async function v(w){let x=await g();w.registerResource(`${t}-openai-widget`,f,{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:l})}]})),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:l})}]}))}return{id:t,title:n,description:r,openaiUri:f,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(l){l.registerTool(i,{title:a,description:r,inputSchema:o,annotations:s,...c&&{_meta:c}},(async(f,d)=>{let g=d._meta??{},p=await t(f,{extra:{_meta:g}});return n&&p.data?{content:[{type:"text",text:p.text}],structuredContent:p.data,_meta:{...c,...g}}:{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(),l=`${t.replace(/\/$/,"")}${i}`,f={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":xe},d={method:s,headers:f};a!==void 0&&(f["Content-Type"]="application/json",d.body=JSON.stringify(a));let u=await fetch(l,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(),l=Me(e.timestamp,n),f=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:f,timestamp:l,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 l of i.events){let f=Oe(l),d=await a.track(f);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[l,f,d]=c,u=typeof l=="string"&&l.trim().length>0?l:"unknown";if(typeof d!="function")return a(...c);let g=d;return a(l,f,async(v,w)=>{let x=performance.now();try{let T=await g(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};
|
|
1
|
+
function U(){return typeof window<"u"&&"openai"in window?"openai":"mcp-apps"}function ge(){return U()==="openai"}function he(){return U()==="mcp-apps"}var S="__start__",v="__end__",B=Symbol.for("waniwani.flow.interrupt"),V=Symbol.for("waniwani.flow.widget");function z(e){if("questions"in e)return{__type:B,questions:e.questions,context:e.context};let{question:t,field:n,context:r,suggestions:o}=e;return{__type:B,questions:[{question:t,field:n,context:r,suggestions:o}]}}function K(e,t){return{__type:V,widgetId:e,...t}}function Y(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===B}function J(e){return typeof e=="object"&&e!==null&&"__type"in e&&e.__type===V}import{z as I}from"zod";var b="text/html+skybridge",M="text/html;profile=mcp-app",X=async(e,t)=>{let n=e.endsWith("/")?e.slice(0,-1):e;return await(await fetch(`${n}${t}`)).text()};function Z(e){return{"openai/widgetDescription":e.description,"openai/widgetPrefersBorder":e.prefersBorder,"openai/widgetDomain":e.widgetDomain,...e.widgetCSP&&{"openai/widgetCSP":e.widgetCSP}}}function G(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 R(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}}}}}import{deflateSync as me,inflateSync as we}from"zlib";function Q(e){let t=JSON.stringify(e);return me(t).toString("base64")}function ee(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 a=ye(o);return a?`\`${r}\` (${a})`:`\`${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"`: A UI widget is being displayed inline to the user.'," Present the `description` to the user and wait for their interaction."," When the user responds with their selection, call THIS 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=ee(e.flowToken);if(t)return t}return{step:void 0,state:{}}}async function C(e,t){return e.type==="direct"?e.to:e.condition(t)}function q(e){return e!=null&&e!==""}function te(e,t,n,r){if(e.every(d=>q(r[d.field])))return null;let o=e.filter(d=>!q(r[d.field])),a=o.length===1,i=o[0];return{content:a&&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,...a&&i?{field:i.field}:{},questions:e,...t?{interruptContext:t}:{}}}}async function O(e,t,n,r,o,a){let i=e,s={...t},d=50,p=0;for(;p++<d;){if(i===v)return{content:{status:"complete"},flowTokenContent:{state:s}};let g=n.get(i);if(!g)return{content:{status:"error",error:`Unknown node: "${i}"`}};try{let c=await g(s,a);if(Y(c)){let l=te(c.questions,c.context,i,s);if(l)return l;let u=o.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(u,s);continue}if(J(c)){let l=c.field??r.get(i)?.field;if(l&&q(s[l])){let u=o.get(i);if(!u)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(u,s);continue}return{content:{status:"widget",description:c.description},flowTokenContent:{step:i,state:s,field:l,widgetId:c.widgetId},structuredContent:{__status:"widget",__widgetId:c.widgetId,...c.data}}}s={...s,...c};let f=o.get(i);if(!f)return{content:{status:"error",error:`No outgoing edge from node "${i}"`}};i=await C(f,s)}catch(c){return{content:{status:"error",error:c instanceof Error?c.message:String(c)},flowTokenContent:{step:i,state:s}}}}return{content:{status:"error",error:"Flow exceeded maximum iterations (possible infinite loop)"}}}var Se={action:I.enum(["start","continue"]).describe('"start" to begin the flow, "continue" to resume after a pause (interrupt or widget)'),stateUpdates:I.record(I.string(),I.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:I.string().optional().describe("Opaque flow token from the previous response. Pass back exactly as received.")};function ne(e){let{config:t,nodes:n,nodeConfigs:r,edges:o}=e,a=Te(t),i=`${t.description}
|
|
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};
|
|
4
4
|
//# sourceMappingURL=index.js.map
|