@waniwani/sdk 0.19.6 → 0.19.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chat/express-js/index.d.ts +15 -1
- package/dist/chat/index.d.ts +15 -1
- package/dist/chat/next-js/index.d.ts +15 -1
- package/dist/index.d.ts +16 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/legacy/chat/express-js/index.d.ts +15 -1
- package/dist/legacy/chat/next-js/index.d.ts +15 -1
- package/dist/legacy/index.d.ts +15 -1
- package/dist/mcp/index.d.ts +17 -3
- package/dist/mcp/index.js +5 -5
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/react/skybridge.d.ts +15 -1
- package/dist/mcp/react.d.ts +15 -1
- package/package.json +1 -1
|
@@ -68,8 +68,12 @@ interface KbClient {
|
|
|
68
68
|
* first-class names at runtime (the widget transport passes these through
|
|
69
69
|
* verbatim instead of namespacing them as custom widget events).
|
|
70
70
|
*/
|
|
71
|
-
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted"];
|
|
71
|
+
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted", "session.error"];
|
|
72
72
|
type EventType = (typeof EVENT_TYPES)[number];
|
|
73
|
+
declare const SESSION_ERROR_CODES: readonly ["agent_failed", "tool_failed", "upstream_failed"];
|
|
74
|
+
type SessionErrorCodeType = (typeof SESSION_ERROR_CODES)[number];
|
|
75
|
+
declare const ERROR_CAUSES: readonly ["timeout", "rate_limited", "upstream_4xx", "upstream_5xx", "network", "invalid_output", "flow_dead_end", "flow_unknown_node", "flow_loop", "unknown"];
|
|
76
|
+
type ErrorCauseType = (typeof ERROR_CAUSES)[number];
|
|
73
77
|
/**
|
|
74
78
|
* Properties for `page.viewed` — emitted once when the chat widget initializes
|
|
75
79
|
* on a host page (the top of the funnel). Attributed to the anonymous
|
|
@@ -96,6 +100,7 @@ interface ToolCalledProperties {
|
|
|
96
100
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
97
101
|
/** Retrieval traces for kb.search() calls made inside this tool handler. */
|
|
98
102
|
kbSearch?: KbSearchTrace[];
|
|
103
|
+
cause?: ErrorCauseType;
|
|
99
104
|
}
|
|
100
105
|
interface LinkClickedProperties {
|
|
101
106
|
url?: string;
|
|
@@ -142,6 +147,12 @@ interface ConvertedProperties {
|
|
|
142
147
|
/** When the conversion actually happened — for backdated off-platform sales. */
|
|
143
148
|
occurredAt?: string;
|
|
144
149
|
}
|
|
150
|
+
interface SessionErrorProperties {
|
|
151
|
+
code: SessionErrorCodeType;
|
|
152
|
+
cause: ErrorCauseType;
|
|
153
|
+
tool?: string;
|
|
154
|
+
node?: string;
|
|
155
|
+
}
|
|
145
156
|
interface TrackingContext {
|
|
146
157
|
/**
|
|
147
158
|
* MCP request metadata passed through to the API.
|
|
@@ -208,6 +219,9 @@ type TrackEvent = ({
|
|
|
208
219
|
} & BaseTrackEvent) | ({
|
|
209
220
|
event: "converted";
|
|
210
221
|
properties?: ConvertedProperties;
|
|
222
|
+
} & BaseTrackEvent) | ({
|
|
223
|
+
event: "session.error";
|
|
224
|
+
properties?: SessionErrorProperties;
|
|
211
225
|
} & BaseTrackEvent);
|
|
212
226
|
/**
|
|
213
227
|
* Legacy tracking shape supported for existing integrations.
|
package/dist/chat/index.d.ts
CHANGED
|
@@ -22,8 +22,12 @@ interface KbSearchTrace {
|
|
|
22
22
|
* first-class names at runtime (the widget transport passes these through
|
|
23
23
|
* verbatim instead of namespacing them as custom widget events).
|
|
24
24
|
*/
|
|
25
|
-
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted"];
|
|
25
|
+
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted", "session.error"];
|
|
26
26
|
type EventType = (typeof EVENT_TYPES)[number];
|
|
27
|
+
declare const SESSION_ERROR_CODES: readonly ["agent_failed", "tool_failed", "upstream_failed"];
|
|
28
|
+
type SessionErrorCodeType = (typeof SESSION_ERROR_CODES)[number];
|
|
29
|
+
declare const ERROR_CAUSES: readonly ["timeout", "rate_limited", "upstream_4xx", "upstream_5xx", "network", "invalid_output", "flow_dead_end", "flow_unknown_node", "flow_loop", "unknown"];
|
|
30
|
+
type ErrorCauseType = (typeof ERROR_CAUSES)[number];
|
|
27
31
|
/**
|
|
28
32
|
* Properties for `page.viewed` — emitted once when the chat widget initializes
|
|
29
33
|
* on a host page (the top of the funnel). Attributed to the anonymous
|
|
@@ -50,6 +54,7 @@ interface ToolCalledProperties {
|
|
|
50
54
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
51
55
|
/** Retrieval traces for kb.search() calls made inside this tool handler. */
|
|
52
56
|
kbSearch?: KbSearchTrace[];
|
|
57
|
+
cause?: ErrorCauseType;
|
|
53
58
|
}
|
|
54
59
|
interface LinkClickedProperties {
|
|
55
60
|
url?: string;
|
|
@@ -96,6 +101,12 @@ interface ConvertedProperties {
|
|
|
96
101
|
/** When the conversion actually happened — for backdated off-platform sales. */
|
|
97
102
|
occurredAt?: string;
|
|
98
103
|
}
|
|
104
|
+
interface SessionErrorProperties {
|
|
105
|
+
code: SessionErrorCodeType;
|
|
106
|
+
cause: ErrorCauseType;
|
|
107
|
+
tool?: string;
|
|
108
|
+
node?: string;
|
|
109
|
+
}
|
|
99
110
|
interface TrackingContext {
|
|
100
111
|
/**
|
|
101
112
|
* MCP request metadata passed through to the API.
|
|
@@ -162,6 +173,9 @@ type TrackEvent = ({
|
|
|
162
173
|
} & BaseTrackEvent) | ({
|
|
163
174
|
event: "converted";
|
|
164
175
|
properties?: ConvertedProperties;
|
|
176
|
+
} & BaseTrackEvent) | ({
|
|
177
|
+
event: "session.error";
|
|
178
|
+
properties?: SessionErrorProperties;
|
|
165
179
|
} & BaseTrackEvent);
|
|
166
180
|
/**
|
|
167
181
|
* Legacy tracking shape supported for existing integrations.
|
|
@@ -68,8 +68,12 @@ interface KbClient {
|
|
|
68
68
|
* first-class names at runtime (the widget transport passes these through
|
|
69
69
|
* verbatim instead of namespacing them as custom widget events).
|
|
70
70
|
*/
|
|
71
|
-
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted"];
|
|
71
|
+
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted", "session.error"];
|
|
72
72
|
type EventType = (typeof EVENT_TYPES)[number];
|
|
73
|
+
declare const SESSION_ERROR_CODES: readonly ["agent_failed", "tool_failed", "upstream_failed"];
|
|
74
|
+
type SessionErrorCodeType = (typeof SESSION_ERROR_CODES)[number];
|
|
75
|
+
declare const ERROR_CAUSES: readonly ["timeout", "rate_limited", "upstream_4xx", "upstream_5xx", "network", "invalid_output", "flow_dead_end", "flow_unknown_node", "flow_loop", "unknown"];
|
|
76
|
+
type ErrorCauseType = (typeof ERROR_CAUSES)[number];
|
|
73
77
|
/**
|
|
74
78
|
* Properties for `page.viewed` — emitted once when the chat widget initializes
|
|
75
79
|
* on a host page (the top of the funnel). Attributed to the anonymous
|
|
@@ -96,6 +100,7 @@ interface ToolCalledProperties {
|
|
|
96
100
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
97
101
|
/** Retrieval traces for kb.search() calls made inside this tool handler. */
|
|
98
102
|
kbSearch?: KbSearchTrace[];
|
|
103
|
+
cause?: ErrorCauseType;
|
|
99
104
|
}
|
|
100
105
|
interface LinkClickedProperties {
|
|
101
106
|
url?: string;
|
|
@@ -142,6 +147,12 @@ interface ConvertedProperties {
|
|
|
142
147
|
/** When the conversion actually happened — for backdated off-platform sales. */
|
|
143
148
|
occurredAt?: string;
|
|
144
149
|
}
|
|
150
|
+
interface SessionErrorProperties {
|
|
151
|
+
code: SessionErrorCodeType;
|
|
152
|
+
cause: ErrorCauseType;
|
|
153
|
+
tool?: string;
|
|
154
|
+
node?: string;
|
|
155
|
+
}
|
|
145
156
|
interface TrackingContext {
|
|
146
157
|
/**
|
|
147
158
|
* MCP request metadata passed through to the API.
|
|
@@ -208,6 +219,9 @@ type TrackEvent = ({
|
|
|
208
219
|
} & BaseTrackEvent) | ({
|
|
209
220
|
event: "converted";
|
|
210
221
|
properties?: ConvertedProperties;
|
|
222
|
+
} & BaseTrackEvent) | ({
|
|
223
|
+
event: "session.error";
|
|
224
|
+
properties?: SessionErrorProperties;
|
|
211
225
|
} & BaseTrackEvent);
|
|
212
226
|
/**
|
|
213
227
|
* Legacy tracking shape supported for existing integrations.
|
package/dist/index.d.ts
CHANGED
|
@@ -115,8 +115,12 @@ declare function defineConfig(config: WaniWaniProjectConfig): WaniWaniProjectCon
|
|
|
115
115
|
* first-class names at runtime (the widget transport passes these through
|
|
116
116
|
* verbatim instead of namespacing them as custom widget events).
|
|
117
117
|
*/
|
|
118
|
-
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted"];
|
|
118
|
+
declare const EVENT_TYPES: readonly ["session.started", "page.viewed", "tool.called", "link.clicked", "widget_render", "user.identified", "price_shown", "prices_compared", "option_selected", "lead_qualified", "converted", "session.error"];
|
|
119
119
|
type EventType = (typeof EVENT_TYPES)[number];
|
|
120
|
+
declare const SESSION_ERROR_CODES: readonly ["agent_failed", "tool_failed", "upstream_failed"];
|
|
121
|
+
type SessionErrorCodeType = (typeof SESSION_ERROR_CODES)[number];
|
|
122
|
+
declare const ERROR_CAUSES: readonly ["timeout", "rate_limited", "upstream_4xx", "upstream_5xx", "network", "invalid_output", "flow_dead_end", "flow_unknown_node", "flow_loop", "unknown"];
|
|
123
|
+
type ErrorCauseType = (typeof ERROR_CAUSES)[number];
|
|
120
124
|
/**
|
|
121
125
|
* Properties for `page.viewed` — emitted once when the chat widget initializes
|
|
122
126
|
* on a host page (the top of the funnel). Attributed to the anonymous
|
|
@@ -143,6 +147,7 @@ interface ToolCalledProperties {
|
|
|
143
147
|
type?: "pricing" | "product_info" | "availability" | "support" | "other";
|
|
144
148
|
/** Retrieval traces for kb.search() calls made inside this tool handler. */
|
|
145
149
|
kbSearch?: KbSearchTrace[];
|
|
150
|
+
cause?: ErrorCauseType;
|
|
146
151
|
}
|
|
147
152
|
interface LinkClickedProperties {
|
|
148
153
|
url?: string;
|
|
@@ -189,6 +194,12 @@ interface ConvertedProperties {
|
|
|
189
194
|
/** When the conversion actually happened — for backdated off-platform sales. */
|
|
190
195
|
occurredAt?: string;
|
|
191
196
|
}
|
|
197
|
+
interface SessionErrorProperties {
|
|
198
|
+
code: SessionErrorCodeType;
|
|
199
|
+
cause: ErrorCauseType;
|
|
200
|
+
tool?: string;
|
|
201
|
+
node?: string;
|
|
202
|
+
}
|
|
192
203
|
interface TrackingContext {
|
|
193
204
|
/**
|
|
194
205
|
* MCP request metadata passed through to the API.
|
|
@@ -255,6 +266,9 @@ type TrackEvent = ({
|
|
|
255
266
|
} & BaseTrackEvent) | ({
|
|
256
267
|
event: "converted";
|
|
257
268
|
properties?: ConvertedProperties;
|
|
269
|
+
} & BaseTrackEvent) | ({
|
|
270
|
+
event: "session.error";
|
|
271
|
+
properties?: SessionErrorProperties;
|
|
258
272
|
} & BaseTrackEvent);
|
|
259
273
|
/**
|
|
260
274
|
* Legacy tracking shape supported for existing integrations.
|
|
@@ -547,4 +561,4 @@ interface V2BatchResponse {
|
|
|
547
561
|
*/
|
|
548
562
|
declare function waniwani(config?: WaniWaniConfig | WaniWaniProjectConfig): WaniWaniClient;
|
|
549
563
|
|
|
550
|
-
export { type ComparedPriceOption, type ConvertedProperties, EVENT_TYPES, type EventType, type FrontendClientOptions, type FrontendIdentity, type FrontendTrackingClient, type KbClient, type KbIngestFile, type KbIngestResult, type KbSearchOptions, type KbSource, type LeadQualifiedProperties, type LegacyTrackEvent, type LinkClickedProperties, type OptionSelectedProperties, type PriceShownProperties, type PricesComparedProperties, type RevenueConvertedInput, type RevenueLeadQualifiedInput, type RevenueOptionSelectedInput, type RevenuePriceShownInput, type RevenuePricesComparedInput, type RevenueTrackingApi, type ToolCalledProperties, type TrackEvent, type TrackFn, type TrackInput, type TrackingConfig, type TrackingShutdownOptions, type TrackingShutdownResult, type V2BatchRejectedEvent, type V2BatchRequest, type V2BatchResponse, type V2CorrelationIds, type V2EnvelopeType, type V2EventEnvelope, type WaniWaniClient, type WaniWaniConfig, WaniWaniError, type WaniWaniProjectConfig, createFrontendClient, defineConfig, waniwani };
|
|
564
|
+
export { type ComparedPriceOption, type ConvertedProperties, ERROR_CAUSES, EVENT_TYPES, type ErrorCauseType, type EventType, type FrontendClientOptions, type FrontendIdentity, type FrontendTrackingClient, type KbClient, type KbIngestFile, type KbIngestResult, type KbSearchOptions, type KbSource, type LeadQualifiedProperties, type LegacyTrackEvent, type LinkClickedProperties, type OptionSelectedProperties, type PriceShownProperties, type PricesComparedProperties, type RevenueConvertedInput, type RevenueLeadQualifiedInput, type RevenueOptionSelectedInput, type RevenuePriceShownInput, type RevenuePricesComparedInput, type RevenueTrackingApi, SESSION_ERROR_CODES, type SessionErrorCodeType, type SessionErrorProperties, type ToolCalledProperties, type TrackEvent, type TrackFn, type TrackInput, type TrackingConfig, type TrackingShutdownOptions, type TrackingShutdownResult, type V2BatchRejectedEvent, type V2BatchRequest, type V2BatchResponse, type V2CorrelationIds, type V2EnvelopeType, type V2EventEnvelope, type WaniWaniClient, type WaniWaniConfig, WaniWaniError, type WaniWaniProjectConfig, createFrontendClient, defineConfig, waniwani };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var y=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};import{existsSync as Y,readFileSync as z}from"fs";import{resolve as $}from"path";var H="waniwani.json",m;function b(){if(m!==void 0)return m;try{let e=$(process.cwd(),H);if(!Y(e))return m=null,null;let t=z(e,"utf-8");return m=JSON.parse(t),m}catch{return m=null,null}}var C="__waniwani_config__";function G(e){return globalThis[C]=e,e}function P(){return globalThis[C]}var _=["session.started","page.viewed","tool.called","link.clicked","widget_render","user.identified","price_shown","prices_compared","option_selected","lead_qualified","converted"];function v(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var X=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Q=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],J=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],Z=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],ee=["waniwani/visitorId","visitorId"],te=["correlationId","openai/requestId"];function M(e){return e?v(e,X):void 0}function A(e){return e?v(e,Q):void 0}function B(e){return e?v(e,J):void 0}function F(e){return e?v(e,Z):void 0}function O(e){return e?v(e,ee):void 0}function D(e){return e?v(e,te):void 0}var ne=[{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}],re=[{needle:"claude",source:"claude"},{needle:"chatgpt",source:"chatgpt"},{needle:"openai",source:"chatgpt"},{needle:"gemini",source:"gemini"}];function U(e,t){if(e){let r=e["waniwani/source"];if(typeof r=="string"&&r.length>0)return r;for(let{key:i,source:s}of ne){let a=e[i];if(typeof a=="string"&&a.length>0)return s}}let n=t?.name;if(typeof n=="string"&&n.length>0){let r=n.toLowerCase();for(let{needle:i,source:s}of re)if(r.includes(i))return s}}var ie="@waniwani/sdk";function T(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??L,i=ae(e),s=I(e.meta),a=I(e.metadata),o=ce(e,s),c=h(e.eventId)??r(),g=ue(e.timestamp,n),d=h(e.source)??U(s)??t.source??ie,u=x(e)?{...e}:void 0,f={...a};return Object.keys(s).length>0&&(f.meta=s),u&&(f.rawLegacy=u),{id:c,type:"mcp.event",name:i,source:d,timestamp:g,correlation:o,properties:se(e,i),metadata:f,rawLegacy:u}}function L(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function se(e,t){if(!x(e))return I(e.properties);let n=oe(e,t),r=I(e.properties);return{...n,...r}}function oe(e,t){switch(t){case"tool.called":{let n={};return h(e.toolName)&&(n.name=e.toolName),h(e.toolType)&&(n.type=e.toolType),n}case"link.clicked":{let n={};return h(e.linkUrl)&&(n.url=e.linkUrl),n}default:return{}}}function ae(e){return x(e)?e.eventType:e.event}function ce(e,t){let n=h(e.requestId)??A(t),r=h(e.sessionId)??M(t),i=h(e.traceId)??B(t),s=h(e.externalUserId)??F(t),a=h(e.visitorId)??O(t),o=h(e.correlationId)??D(t)??n,c={};return r&&(c.sessionId=r),i&&(c.traceId=i),n&&(c.requestId=n),o&&(c.correlationId=o),s&&(c.externalUserId=s),a&&(c.visitorId=a),c}function ue(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 I(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function h(e){if(typeof e=="string"&&e.trim().length!==0)return e}function x(e){return"eventType"in e}function k(e){return{priceShown:({amount:t,currency:n,itemId:r,label:i,...s})=>e({event:"price_shown",properties:{amount:t,currency:n,itemId:r,label:i},...s}),pricesCompared:({options:t,...n})=>e({event:"prices_compared",properties:{options:t},...n}),optionSelected:({id:t,amount:n,currency:r,...i})=>e({event:"option_selected",properties:{id:t,amount:n,currency:r},...i}),leadQualified:t=>{let{externalId:n,email:r,name:i,...s}=t??{};return e({event:"lead_qualified",properties:{externalId:n,email:r,name:i},...s})},converted:({amount:t,currency:n,occurredAt:r,...i})=>e({event:"converted",properties:{amount:t,currency:n,occurredAt:r},...i})}}var de="/api/mcp/events/v2/batch";var W="@waniwani/sdk";var pe=new Set([401,403]),le=new Set([408,425,429,500,502,503,504]);function w(e){return new R(e)}var R=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=ge(t.apiUrl,t.endpointPath??de),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)),this.registerBrowserTeardown()}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 i=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>i)])===i?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}registerBrowserTeardown(){if(typeof document>"u"||typeof window>"u")return;let t=()=>{document.visibilityState==="hidden"&&this.keepaliveFlush()};document.addEventListener("visibilitychange",t),window.addEventListener("pagehide",()=>this.keepaliveFlush())}keepaliveFlush(){if(this.isStopped||this.buffer.length===0)return;let t=this.buffer.splice(0,this.buffer.length);this.sendKeepaliveChunk(t)}sendKeepaliveChunk(t){let n=JSON.stringify(this.makeBatchRequest(t));if(n.length>6e4&&t.length>1){let r=Math.ceil(t.length/2);this.sendKeepaliveChunk(t.slice(0,r)),this.sendKeepaliveChunk(t.slice(r));return}try{this.fetchFn(this.endpointUrl,{method:"POST",headers:this.requestHeaders(),body:n,keepalive:!0}).catch(()=>{})}catch{}}requestHeaders(){let t={"Content-Type":"application/json","X-WaniWani-SDK":W};return this.apiKey&&(t.Authorization=`Bearer ${this.apiKey}`),t}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 i=await this.sendBatchOnce(r);switch(this.inFlightCount=0,i.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(i.status,r.length);return;case"permanent":this.logger.error("[Waniwani] Dropping %d event(s) after permanent failure: %s",r.length,i.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[Waniwani] Dropping %d event(s) after retry exhaustion: %s",r.length,i.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(i.permanent.length>0&&this.logger.error("[Waniwani] Dropping %d event(s) rejected as permanent",i.permanent.length),i.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[Waniwani] Dropping %d retryable event(s) after retry exhaustion",i.retryable.length);return}r=i.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:this.requestHeaders(),body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:me(s)}}if(pe.has(n.status))return{kind:"auth",status:n.status};if(le.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await he(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let i=this.classifyRejectedEvents(t,r.rejected);return i.retryable.length===0&&i.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:i.retryable,permanent:i.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:W,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),i=[],s=[];for(let a of n){let o=r.get(a.eventId);if(o){if(fe(a)){i.push(o);continue}s.push(o)}}return{retryable:i,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 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 he(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function ge(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function me(e){return e instanceof Error?e.message:String(e)}function ve(e){let{apiUrl:t,endpointPath:n}=Te(e.endpoint),r=w({apiUrl:t,apiKey:e.token,endpointPath:n,flushIntervalMs:e.flushIntervalMs}),i,s=o=>typeof o=="function"?o():o,a=async o=>{let c=e.identity?.()??{},g=s(e.channelId),d=g===void 0?o.properties:{channelId:g,...o.properties},u=T({sessionId:c.sessionId,visitorId:c.visitorId,externalUserId:c.externalUserId??i,traceId:c.traceId,...o,properties:d,metadata:{...e.metadata,...o.metadata}},{source:s(e.source)});return r.enqueue(u),{eventId:u.id}};return{track:Object.assign(a,k(a)),identify(o,c){return i=o,a({event:"user.identified",externalUserId:o,properties:c})},flush:()=>r.flush(),shutdown:o=>r.shutdown(o)}}function Te(e){let t=new URL(e);return{apiUrl:t.origin,endpointPath:`${t.pathname}${t.search}`}}import{AsyncLocalStorage as ye}from"async_hooks";var K=globalThis,E=K.__waniwaniKbRetrievalStore;E||(E=new ye,K.__waniwaniKbRetrievalStore=E);var Ie=E;function V(e){try{Ie.getStore()?.searches.push(e)}catch{}}var ke="@waniwani/sdk";function j(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function i(s,a,o){let c=r(),g=`${t.replace(/\/$/,"")}${a}`,d={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":ke},u={method:s,headers:d};o!==void 0&&(d["Content-Type"]="application/json",u.body=JSON.stringify(o));let f=await fetch(g,u);if(!f.ok){let q=await f.text().catch(()=>"");throw new y(q||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(s){return i("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){let o=await i("POST","/api/mcp/kb/search",{query:s,...a});return V({query:s,resultCount:o.length,results:o.map(c=>({source:c.source,heading:c.heading,score:c.score}))}),o},async sources(){return i("GET","/api/mcp/kb/sources")}}}function N(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function i(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?w({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0;function a(d){i();let u=T(d);return!u.correlation.sessionId&&!u.correlation.externalUserId&&!u.correlation.visitorId&&console.warn(`[waniwani] event "${u.name}" has no sessionId, externalUserId, or visitorId; the ingest API requires one and will reject it.`),s?.enqueue(u),{eventId:u.id}}let o=async d=>a(d),c=Object.assign(o,k(o)),g={async identify(d,u,f){i();let S=T({event:"user.identified",externalUserId:d,properties:u,meta:f});return s?.enqueue(S),{eventId:S.id}},track:c,async flush(){i(),await s?.flush()},async shutdown(d){return i(),await s?.shutdown({timeoutMs:d?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&we(g,r.shutdownTimeoutMs),g}function we(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 Ee(e){let n=e??b()??P(),r=n?.apiUrl??process.env.WANIWANI_API_URL??"https://app.waniwani.ai",i=n?.apiKey??process.env.WANIWANI_API_KEY,s={endpointPath:n?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:n?.tracking?.flushIntervalMs??1e3,maxBatchSize:n?.tracking?.maxBatchSize??20,maxBufferSize:n?.tracking?.maxBufferSize??1e3,maxRetries:n?.tracking?.maxRetries??3,retryBaseDelayMs:n?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:n?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:n?.tracking?.shutdownTimeoutMs??2e3},a={apiUrl:r,apiKey:i,tracking:s},o=N(a),c=j(a);return{...o,kb:c,_config:a}}export{_ as EVENT_TYPES,y as WaniWaniError,ve as createFrontendClient,G as defineConfig,Ee as waniwani};
|
|
1
|
+
var y=class extends Error{constructor(n,r){super(n);this.status=r;this.name="WaniWaniError"}};import{existsSync as $,readFileSync as H}from"fs";import{resolve as G}from"path";var X="waniwani.json",m;function _(){if(m!==void 0)return m;try{let e=G(process.cwd(),X);if(!$(e))return m=null,null;let t=H(e,"utf-8");return m=JSON.parse(t),m}catch{return m=null,null}}var C="__waniwani_config__";function Q(e){return globalThis[C]=e,e}function b(){return globalThis[C]}var P=["session.started","page.viewed","tool.called","link.clicked","widget_render","user.identified","price_shown","prices_compared","option_selected","lead_qualified","converted","session.error"],M=["agent_failed","tool_failed","upstream_failed"],O=["timeout","rate_limited","upstream_4xx","upstream_5xx","network","invalid_output","flow_dead_end","flow_unknown_node","flow_loop","unknown"];function v(e,t){for(let n of t){let r=e[n];if(typeof r=="string"&&r.length>0)return r}}var J=["waniwani/sessionId","openai/sessionId","openai/session","sessionId","conversationId","mcp-session-id"],Z=["waniwani/requestId","openai/requestId","requestId","mcp/requestId"],ee=["waniwani/traceId","openai/traceId","traceId","mcp/traceId","openai/requestId","requestId"],te=["waniwani/userId","openai/userId","externalUserId","userId","actorId"],ne=["waniwani/visitorId","visitorId"],re=["correlationId","openai/requestId"];function A(e){return e?v(e,J):void 0}function B(e){return e?v(e,Z):void 0}function F(e){return e?v(e,ee):void 0}function D(e){return e?v(e,te):void 0}function U(e){return e?v(e,ne):void 0}function L(e){return e?v(e,re):void 0}var ie=[{key:"openai/sessionId",source:"chatgpt"},{key:"openai/session",source:"chatgpt"}],se=[{needle:"claude",source:"claude"},{needle:"chatgpt",source:"chatgpt"},{needle:"openai",source:"chatgpt"},{needle:"gemini",source:"gemini"}];function W(e,t){if(e){let r=e["waniwani/source"];if(typeof r=="string"&&r.length>0)return r;for(let{key:i,source:s}of ie){let a=e[i];if(typeof a=="string"&&a.length>0)return s}}let n=t?.name;if(typeof n=="string"&&n.length>0){let r=n.toLowerCase();for(let{needle:i,source:s}of se)if(r.includes(i))return s}}var oe="@waniwani/sdk";function T(e,t={}){let n=t.now??(()=>new Date),r=t.generateId??K,i=ue(e),s=I(e.meta),a=I(e.metadata),o=de(e,s),c=h(e.eventId)??r(),g=pe(e.timestamp,n),d=h(e.source)??W(s)??t.source??oe,u=x(e)?{...e}:void 0,f={...a};return Object.keys(s).length>0&&(f.meta=s),u&&(f.rawLegacy=u),{id:c,type:"mcp.event",name:i,source:d,timestamp:g,correlation:o,properties:ae(e,i),metadata:f,rawLegacy:u}}function K(){return typeof crypto<"u"&&typeof crypto.randomUUID=="function"?`evt_${crypto.randomUUID()}`:`evt_${Math.random().toString(36).slice(2,10)}_${Date.now().toString(36)}`}function ae(e,t){if(!x(e))return I(e.properties);let n=ce(e,t),r=I(e.properties);return{...n,...r}}function ce(e,t){switch(t){case"tool.called":{let n={};return h(e.toolName)&&(n.name=e.toolName),h(e.toolType)&&(n.type=e.toolType),n}case"link.clicked":{let n={};return h(e.linkUrl)&&(n.url=e.linkUrl),n}default:return{}}}function ue(e){return x(e)?e.eventType:e.event}function de(e,t){let n=h(e.requestId)??B(t),r=h(e.sessionId)??A(t),i=h(e.traceId)??F(t),s=h(e.externalUserId)??D(t),a=h(e.visitorId)??U(t),o=h(e.correlationId)??L(t)??n,c={};return r&&(c.sessionId=r),i&&(c.traceId=i),n&&(c.requestId=n),o&&(c.correlationId=o),s&&(c.externalUserId=s),a&&(c.visitorId=a),c}function pe(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 I(e){return!e||typeof e!="object"||Array.isArray(e)?{}:e}function h(e){if(typeof e=="string"&&e.trim().length!==0)return e}function x(e){return"eventType"in e}function E(e){return{priceShown:({amount:t,currency:n,itemId:r,label:i,...s})=>e({event:"price_shown",properties:{amount:t,currency:n,itemId:r,label:i},...s}),pricesCompared:({options:t,...n})=>e({event:"prices_compared",properties:{options:t},...n}),optionSelected:({id:t,amount:n,currency:r,...i})=>e({event:"option_selected",properties:{id:t,amount:n,currency:r},...i}),leadQualified:t=>{let{externalId:n,email:r,name:i,...s}=t??{};return e({event:"lead_qualified",properties:{externalId:n,email:r,name:i},...s})},converted:({amount:t,currency:n,occurredAt:r,...i})=>e({event:"converted",properties:{amount:t,currency:n,occurredAt:r},...i})}}var le="/api/mcp/events/v2/batch";var V="@waniwani/sdk";var fe=new Set([401,403]),he=new Set([408,425,429,500,502,503,504]);function S(e){return new R(e)}var R=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=ve(t.apiUrl,t.endpointPath??le),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)),this.registerBrowserTeardown()}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 i=Symbol("shutdown-timeout");return await Promise.race([r.then(()=>"flushed"),this.sleep(n).then(()=>i)])===i?(this.isStopped=!0,{timedOut:!0,pendingEvents:this.pendingEvents()}):(this.isStopped=!0,{timedOut:!1,pendingEvents:this.pendingEvents()})}registerBrowserTeardown(){if(typeof document>"u"||typeof window>"u")return;let t=()=>{document.visibilityState==="hidden"&&this.keepaliveFlush()};document.addEventListener("visibilitychange",t),window.addEventListener("pagehide",()=>this.keepaliveFlush())}keepaliveFlush(){if(this.isStopped||this.buffer.length===0)return;let t=this.buffer.splice(0,this.buffer.length);this.sendKeepaliveChunk(t)}sendKeepaliveChunk(t){let n=JSON.stringify(this.makeBatchRequest(t));if(n.length>6e4&&t.length>1){let r=Math.ceil(t.length/2);this.sendKeepaliveChunk(t.slice(0,r)),this.sendKeepaliveChunk(t.slice(r));return}try{this.fetchFn(this.endpointUrl,{method:"POST",headers:this.requestHeaders(),body:n,keepalive:!0}).catch(()=>{})}catch{}}requestHeaders(){let t={"Content-Type":"application/json","X-WaniWani-SDK":V};return this.apiKey&&(t.Authorization=`Bearer ${this.apiKey}`),t}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 i=await this.sendBatchOnce(r);switch(this.inFlightCount=0,i.kind){case"success":return;case"auth":this.stopTransportForAuthFailure(i.status,r.length);return;case"permanent":this.logger.error("[Waniwani] Dropping %d event(s) after permanent failure: %s",r.length,i.reason);return;case"retryable":if(n>=this.maxRetries){this.logger.error("[Waniwani] Dropping %d event(s) after retry exhaustion: %s",r.length,i.reason);return}await this.sleep(this.backoffDelayMs(n)),n+=1;continue;case"partial":if(i.permanent.length>0&&this.logger.error("[Waniwani] Dropping %d event(s) rejected as permanent",i.permanent.length),i.retryable.length===0)return;if(n>=this.maxRetries){this.logger.error("[Waniwani] Dropping %d retryable event(s) after retry exhaustion",i.retryable.length);return}r=i.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:this.requestHeaders(),body:JSON.stringify(this.makeBatchRequest(t))})}catch(s){return{kind:"retryable",reason:Te(s)}}if(fe.has(n.status))return{kind:"auth",status:n.status};if(he.has(n.status))return{kind:"retryable",reason:`HTTP ${n.status}`};if(!n.ok)return{kind:"permanent",reason:`HTTP ${n.status}`};let r=await me(n);if(!r?.rejected||r.rejected.length===0)return{kind:"success"};let i=this.classifyRejectedEvents(t,r.rejected);return i.retryable.length===0&&i.permanent.length===0?{kind:"success"}:{kind:"partial",retryable:i.retryable,permanent:i.permanent}}makeBatchRequest(t){return{sentAt:this.now().toISOString(),source:{sdk:V,version:this.sdkVersion??"0.0.0"},events:t}}classifyRejectedEvents(t,n){let r=new Map(t.map(a=>[a.id,a])),i=[],s=[];for(let a of n){let o=r.get(a.eventId);if(o){if(ge(a)){i.push(o);continue}s.push(o)}}return{retryable:i,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 ge(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 me(e){let t=await e.text();if(t)try{return JSON.parse(t)}catch{return}}function ve(e,t){let n=e.endsWith("/")?e:`${e}/`,r=t.startsWith("/")?t.slice(1):t;return`${n}${r}`}function Te(e){return e instanceof Error?e.message:String(e)}function ye(e){let{apiUrl:t,endpointPath:n}=Ie(e.endpoint),r=S({apiUrl:t,apiKey:e.token,endpointPath:n,flushIntervalMs:e.flushIntervalMs}),i,s=o=>typeof o=="function"?o():o,a=async o=>{let c=e.identity?.()??{},g=s(e.channelId),d=g===void 0?o.properties:{channelId:g,...o.properties},u=T({sessionId:c.sessionId,visitorId:c.visitorId,externalUserId:c.externalUserId??i,traceId:c.traceId,...o,properties:d,metadata:{...e.metadata,...o.metadata}},{source:s(e.source)});return r.enqueue(u),{eventId:u.id}};return{track:Object.assign(a,E(a)),identify(o,c){return i=o,a({event:"user.identified",externalUserId:o,properties:c})},flush:()=>r.flush(),shutdown:o=>r.shutdown(o)}}function Ie(e){let t=new URL(e);return{apiUrl:t.origin,endpointPath:`${t.pathname}${t.search}`}}import{AsyncLocalStorage as Ee}from"async_hooks";var j=globalThis,k=j.__waniwaniKbRetrievalStore;k||(k=new Ee,j.__waniwaniKbRetrievalStore=k);var Se=k;function N(e){try{Se.getStore()?.searches.push(e)}catch{}}var ke="@waniwani/sdk";function q(e){let{apiUrl:t,apiKey:n}=e;function r(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}async function i(s,a,o){let c=r(),g=`${t.replace(/\/$/,"")}${a}`,d={Authorization:`Bearer ${c}`,"X-WaniWani-SDK":ke},u={method:s,headers:d};o!==void 0&&(d["Content-Type"]="application/json",u.body=JSON.stringify(o));let f=await fetch(g,u);if(!f.ok){let z=await f.text().catch(()=>"");throw new y(z||`KB API error: HTTP ${f.status}`,f.status)}return(await f.json()).data}return{async ingest(s){return i("POST","/api/mcp/kb/ingest",{files:s})},async search(s,a){let o=await i("POST","/api/mcp/kb/search",{query:s,...a});return N({query:s,resultCount:o.length,results:o.map(c=>({source:c.source,heading:c.heading,score:c.score}))}),o},async sources(){return i("GET","/api/mcp/kb/sources")}}}function Y(e){let{apiUrl:t,apiKey:n,tracking:r}=e;function i(){if(!n)throw new Error("WANIWANI_API_KEY is not set");return n}let s=n?S({apiUrl:t,apiKey:n,endpointPath:r.endpointPath,flushIntervalMs:r.flushIntervalMs,maxBatchSize:r.maxBatchSize,maxBufferSize:r.maxBufferSize,maxRetries:r.maxRetries,retryBaseDelayMs:r.retryBaseDelayMs,retryMaxDelayMs:r.retryMaxDelayMs,shutdownTimeoutMs:r.shutdownTimeoutMs}):void 0;function a(d){i();let u=T(d);return!u.correlation.sessionId&&!u.correlation.externalUserId&&!u.correlation.visitorId&&console.warn(`[waniwani] event "${u.name}" has no sessionId, externalUserId, or visitorId; the ingest API requires one and will reject it.`),s?.enqueue(u),{eventId:u.id}}let o=async d=>a(d),c=Object.assign(o,E(o)),g={async identify(d,u,f){i();let w=T({event:"user.identified",externalUserId:d,properties:u,meta:f});return s?.enqueue(w),{eventId:w.id}},track:c,async flush(){i(),await s?.flush()},async shutdown(d){return i(),await s?.shutdown({timeoutMs:d?.timeoutMs??r.shutdownTimeoutMs})??{timedOut:!1,pendingEvents:0}}};return s&&we(g,r.shutdownTimeoutMs),g}function we(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 xe(e){let n=e??_()??b(),r=n?.apiUrl??process.env.WANIWANI_API_URL??"https://app.waniwani.ai",i=n?.apiKey??process.env.WANIWANI_API_KEY,s={endpointPath:n?.tracking?.endpointPath??"/api/mcp/events/v2/batch",flushIntervalMs:n?.tracking?.flushIntervalMs??1e3,maxBatchSize:n?.tracking?.maxBatchSize??20,maxBufferSize:n?.tracking?.maxBufferSize??1e3,maxRetries:n?.tracking?.maxRetries??3,retryBaseDelayMs:n?.tracking?.retryBaseDelayMs??200,retryMaxDelayMs:n?.tracking?.retryMaxDelayMs??2e3,shutdownTimeoutMs:n?.tracking?.shutdownTimeoutMs??2e3},a={apiUrl:r,apiKey:i,tracking:s},o=Y(a),c=q(a);return{...o,kb:c,_config:a}}export{O as ERROR_CAUSES,P as EVENT_TYPES,M as SESSION_ERROR_CODES,y as WaniWaniError,ye as createFrontendClient,Q as defineConfig,xe as waniwani};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|