@useknest/widget-core 0.0.1-alpha.1 → 0.0.1-alpha.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/config.d.ts +3 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/constants.d.ts +10 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +98 -0
- package/dist/streaming.d.ts +19 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/types.d.ts +7 -2
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +33 -47
- package/README.md +0 -26
- package/dist/internal.d.ts +0 -4
- package/dist/widget.js +0 -4677
- package/dist/widget.umd.js +0 -85
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE5C,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAoB/F"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const API_BASE_URL = "http://localhost:5173/api/v1";
|
|
2
|
+
export declare const CHAT_API_URL = "http://localhost:5173/api/v1/chat-messages";
|
|
3
|
+
export declare const CONFIG_API_URL = "http://localhost:5173/api/v1/widget-config";
|
|
4
|
+
export declare const FALLBACK_ERROR_MESSAGE = "Sorry, I didn't get a response.";
|
|
5
|
+
export declare const DEFAULT_ERROR_MESSAGE = "Unknown error occurred";
|
|
6
|
+
export declare const DEFAULT_AVATAR_URL = "/default-avatar.svg";
|
|
7
|
+
export declare const DEFAULT_BRAND_COLOR = "#0d7a7f";
|
|
8
|
+
export declare const DEFAULT_WELCOME_MESSAGE = "Hi! I'm your AI assistant. I'm trained to answer questions about your documentation. How can I help you today?";
|
|
9
|
+
export declare const DEFAULT_EXAMPLE_QUESTIONS: string[];
|
|
10
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,iCAAiC,CAAC;AAC3D,eAAO,MAAM,YAAY,+CAAkC,CAAC;AAC5D,eAAO,MAAM,cAAc,+CAAkC,CAAC;AAE9D,eAAO,MAAM,sBAAsB,oCAAoC,CAAC;AACxE,eAAO,MAAM,qBAAqB,2BAA2B,CAAC;AAC9D,eAAO,MAAM,kBAAkB,wBAAwB,CAAC;AACxD,eAAO,MAAM,mBAAmB,YAAY,CAAC;AAC7C,eAAO,MAAM,uBAAuB,mHAC6E,CAAC;AAClH,eAAO,MAAM,yBAAyB,UAIrC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
export
|
|
5
|
-
export type {
|
|
1
|
+
export type { MessageRole, Source, Message, ChatMessageRequest, ChatMessageResponse, ChatApiError, WidgetConfig, SSEEvent } from './types';
|
|
2
|
+
export { API_BASE_URL, CHAT_API_URL, CONFIG_API_URL, FALLBACK_ERROR_MESSAGE, DEFAULT_ERROR_MESSAGE, DEFAULT_AVATAR_URL, DEFAULT_BRAND_COLOR, DEFAULT_WELCOME_MESSAGE, DEFAULT_EXAMPLE_QUESTIONS } from './constants';
|
|
3
|
+
export { fetchWidgetConfig } from './config';
|
|
4
|
+
export { streamChatMessage } from './streaming';
|
|
5
|
+
export type { StreamCallbacks, StreamMessageOptions } from './streaming';
|
|
6
|
+
export { formatContent } from './utils';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EACX,WAAW,EACX,MAAM,EACN,OAAO,EACP,kBAAkB,EAClB,mBAAmB,EACnB,YAAY,EACZ,YAAY,EACZ,QAAQ,EACR,MAAM,SAAS,CAAC;AAGjB,OAAO,EACN,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,sBAAsB,EACtB,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,uBAAuB,EACvB,yBAAyB,EACzB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAG7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,YAAY,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGzE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { marked as E } from "marked";
|
|
2
|
+
const g = "http://localhost:5173/api/v1", y = `${g}/chat-messages`, m = `${g}/widget-config`, I = "Sorry, I didn't get a response.", u = "Unknown error occurred", R = "/default-avatar.svg", S = "#0d7a7f", L = "Hi! I'm your AI assistant. I'm trained to answer questions about your documentation. How can I help you today?", C = [
|
|
3
|
+
"How do I get started?",
|
|
4
|
+
"What features are available?",
|
|
5
|
+
"How do I integrate this?"
|
|
6
|
+
];
|
|
7
|
+
async function T(n) {
|
|
8
|
+
try {
|
|
9
|
+
const t = await fetch(m, {
|
|
10
|
+
method: "GET",
|
|
11
|
+
headers: {
|
|
12
|
+
"X-Api-Key": n
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
if (t.ok)
|
|
16
|
+
return await t.json();
|
|
17
|
+
{
|
|
18
|
+
const r = await t.text();
|
|
19
|
+
return console.warn("Failed to fetch widget config:", t.status, r), null;
|
|
20
|
+
}
|
|
21
|
+
} catch (t) {
|
|
22
|
+
return console.error("Error fetching widget config:", t), null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function b(n) {
|
|
26
|
+
const { publishableApiKey: t, content: r, threadId: l, callbacks: o } = n;
|
|
27
|
+
try {
|
|
28
|
+
const a = { content: r };
|
|
29
|
+
l && (a.threadId = l);
|
|
30
|
+
const s = await fetch(y, {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
"X-Api-Key": t
|
|
35
|
+
},
|
|
36
|
+
body: JSON.stringify(a)
|
|
37
|
+
});
|
|
38
|
+
if (!s.ok) {
|
|
39
|
+
const c = await s.json();
|
|
40
|
+
throw new Error(c.error || "Failed to send message");
|
|
41
|
+
}
|
|
42
|
+
const f = s.body?.getReader();
|
|
43
|
+
if (!f)
|
|
44
|
+
throw new Error("Response body is not readable");
|
|
45
|
+
const A = new TextDecoder();
|
|
46
|
+
let i = "", p = !1;
|
|
47
|
+
for (; ; ) {
|
|
48
|
+
const { done: c, value: w } = await f.read();
|
|
49
|
+
if (c) break;
|
|
50
|
+
i += A.decode(w, { stream: !0 });
|
|
51
|
+
const h = i.split(`
|
|
52
|
+
`);
|
|
53
|
+
i = h.pop() || "";
|
|
54
|
+
for (const d of h)
|
|
55
|
+
if (d.startsWith("data: "))
|
|
56
|
+
try {
|
|
57
|
+
const e = JSON.parse(d.slice(6));
|
|
58
|
+
if (e.type === "init")
|
|
59
|
+
e.threadId && o.onInit && o.onInit(e.threadId);
|
|
60
|
+
else if (e.type === "content")
|
|
61
|
+
e.content && (p = !0, o.onContent(e.content));
|
|
62
|
+
else if (e.type === "complete")
|
|
63
|
+
o.onComplete && o.onComplete(e.sources || []);
|
|
64
|
+
else if (e.type === "error")
|
|
65
|
+
throw new Error(e.error || u);
|
|
66
|
+
} catch (e) {
|
|
67
|
+
console.error("Error parsing SSE data:", e, d);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
p || o.onError(I);
|
|
71
|
+
} catch (a) {
|
|
72
|
+
const s = a instanceof Error ? a.message : u;
|
|
73
|
+
o.onError(s);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
E.setOptions({
|
|
77
|
+
breaks: !0,
|
|
78
|
+
gfm: !0
|
|
79
|
+
});
|
|
80
|
+
function O(n) {
|
|
81
|
+
return n.split(/\n\s*\n/).map((r) => r.trim()).filter((r) => r).map((r) => ({
|
|
82
|
+
html: E.parseInline(r)
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
g as API_BASE_URL,
|
|
87
|
+
y as CHAT_API_URL,
|
|
88
|
+
m as CONFIG_API_URL,
|
|
89
|
+
R as DEFAULT_AVATAR_URL,
|
|
90
|
+
S as DEFAULT_BRAND_COLOR,
|
|
91
|
+
u as DEFAULT_ERROR_MESSAGE,
|
|
92
|
+
C as DEFAULT_EXAMPLE_QUESTIONS,
|
|
93
|
+
L as DEFAULT_WELCOME_MESSAGE,
|
|
94
|
+
I as FALLBACK_ERROR_MESSAGE,
|
|
95
|
+
T as fetchWidgetConfig,
|
|
96
|
+
O as formatContent,
|
|
97
|
+
b as streamChatMessage
|
|
98
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Source } from './types';
|
|
2
|
+
export interface StreamCallbacks {
|
|
3
|
+
onInit?: (threadId: string) => void;
|
|
4
|
+
onContent: (content: string) => void;
|
|
5
|
+
onComplete?: (sources: Source[]) => void;
|
|
6
|
+
onError: (error: string) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface StreamMessageOptions {
|
|
9
|
+
publishableApiKey: string;
|
|
10
|
+
content: string;
|
|
11
|
+
threadId?: string;
|
|
12
|
+
callbacks: StreamCallbacks;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Sends a chat message and streams the response via SSE.
|
|
16
|
+
* This function handles all streaming logic in a framework-agnostic way.
|
|
17
|
+
*/
|
|
18
|
+
export declare function streamChatMessage(options: StreamMessageOptions): Promise<void>;
|
|
19
|
+
//# sourceMappingURL=streaming.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming.d.ts","sourceRoot":"","sources":["../src/streaming.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAsB,MAAM,EAAY,MAAM,SAAS,CAAC;AAEpE,MAAM,WAAW,eAAe;IAC/B,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IACpC,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IACrC,UAAU,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IACzC,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC;AAED,MAAM,WAAW,oBAAoB;IACpC,iBAAiB,EAAE,MAAM,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,eAAe,CAAC;CAC3B;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC,CA4EpF"}
|
package/dist/types.d.ts
CHANGED
|
@@ -43,6 +43,11 @@ export interface WidgetConfig {
|
|
|
43
43
|
exampleQuestions?: string[];
|
|
44
44
|
brandColor?: string;
|
|
45
45
|
}
|
|
46
|
-
export interface
|
|
47
|
-
|
|
46
|
+
export interface SSEEvent {
|
|
47
|
+
type: 'init' | 'content' | 'complete' | 'error';
|
|
48
|
+
threadId?: string;
|
|
49
|
+
content?: string;
|
|
50
|
+
sources?: Source[];
|
|
51
|
+
error?: string;
|
|
48
52
|
}
|
|
53
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,WAAW,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AC;CACpB;AAED,MAAM,WAAW,QAAQ;IACxB,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,UAAU,GAAG,OAAO,CAAC;IAChD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface FormattedParagraph {
|
|
2
|
+
html: string;
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Formats message content by splitting into paragraphs and rendering markdown.
|
|
6
|
+
* Returns an array of paragraphs with rendered HTML.
|
|
7
|
+
*/
|
|
8
|
+
export declare function formatContent(content: string): FormattedParagraph[];
|
|
9
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAQA,MAAM,WAAW,kBAAkB;IAClC,IAAI,EAAE,MAAM,CAAC;CACb;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAQnE"}
|
package/package.json
CHANGED
|
@@ -1,48 +1,34 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@vitest/browser": "^3.2.4",
|
|
36
|
-
"jsdom": "^25.0.1",
|
|
37
|
-
"svelte": "^5.39.5",
|
|
38
|
-
"typescript": "^5.9.2",
|
|
39
|
-
"vite": "^7.1.7",
|
|
40
|
-
"vite-plugin-dts": "^4.3.0",
|
|
41
|
-
"vitest": "^3.2.4",
|
|
42
|
-
"vitest-browser-svelte": "^1.1.0"
|
|
43
|
-
},
|
|
44
|
-
"dependencies": {
|
|
45
|
-
"dompurify": "^3.2.3",
|
|
46
|
-
"marked": "^16.4.1"
|
|
47
|
-
}
|
|
48
|
-
}
|
|
2
|
+
"name": "@useknest/widget-core",
|
|
3
|
+
"version": "0.0.1-alpha.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Core business logic for Knest chat widget",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"typescript": "^5.9.2",
|
|
20
|
+
"vite": "^7.1.7",
|
|
21
|
+
"vite-plugin-dts": "^4.4.1",
|
|
22
|
+
"vitest": "^3.0.0"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@sentry/browser": "^9.33.0",
|
|
26
|
+
"marked": "^16.4.1"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "vite build",
|
|
30
|
+
"dev": "vite build --watch",
|
|
31
|
+
"test": "vitest",
|
|
32
|
+
"test:ci": "vitest run"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/README.md
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
# @useknest/widget-core
|
|
2
|
-
|
|
3
|
-
Core Web Component for Knest chat widget.
|
|
4
|
-
|
|
5
|
-
## Usage
|
|
6
|
-
|
|
7
|
-
### Script tag (Vanilla JS)
|
|
8
|
-
|
|
9
|
-
```html
|
|
10
|
-
<script src="https://unpkg.com/@useknest/widget-core"></script>
|
|
11
|
-
<knest-chat publishable-api-key="your-key"></knest-chat>
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
### Framework Wrappers
|
|
15
|
-
|
|
16
|
-
For better framework integration, use the framework-specific wrappers:
|
|
17
|
-
|
|
18
|
-
- React: `@useknest/widget-react`
|
|
19
|
-
- Vue: `@useknest/widget-vue`
|
|
20
|
-
- Svelte: `@useknest/widget-svelte`
|
|
21
|
-
|
|
22
|
-
### Build
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
pnpm build
|
|
26
|
-
```
|
package/dist/internal.d.ts
DELETED