@saber2pr/ai-assistant 0.0.4

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.
Files changed (58) hide show
  1. package/ReadMe.md +57 -0
  2. package/lib/app.d.ts +1 -0
  3. package/lib/app.js +27 -0
  4. package/lib/chat.d.ts +3 -0
  5. package/lib/chat.js +222 -0
  6. package/lib/chat.style.d.ts +12 -0
  7. package/lib/chat.style.js +20 -0
  8. package/lib/components/Codeblock/index.d.ts +9 -0
  9. package/lib/components/Codeblock/index.js +55 -0
  10. package/lib/components/Codeblock/index.style.d.ts +1 -0
  11. package/lib/components/Codeblock/index.style.js +13 -0
  12. package/lib/components/MarkdownText/index.d.ts +3 -0
  13. package/lib/components/MarkdownText/index.js +71 -0
  14. package/lib/components/MyRuntimeProvider/index.d.ts +5 -0
  15. package/lib/components/MyRuntimeProvider/index.js +22 -0
  16. package/lib/components/MyRuntimeProvider/myModelAdapterStream.d.ts +4 -0
  17. package/lib/components/MyRuntimeProvider/myModelAdapterStream.js +180 -0
  18. package/lib/components/Thread/index.d.ts +2 -0
  19. package/lib/components/Thread/index.js +286 -0
  20. package/lib/components/Thread/index.style.d.ts +15 -0
  21. package/lib/components/Thread/index.style.js +18 -0
  22. package/lib/components/ThreadList/index.d.ts +4 -0
  23. package/lib/components/ThreadList/index.js +131 -0
  24. package/lib/constants/index.d.ts +1 -0
  25. package/lib/constants/index.js +4 -0
  26. package/lib/context.d.ts +3 -0
  27. package/lib/context.js +8 -0
  28. package/lib/hooks/useAsync.d.ts +14 -0
  29. package/lib/hooks/useAsync.js +105 -0
  30. package/lib/hooks/useCopy.d.ts +5 -0
  31. package/lib/hooks/useCopy.js +48 -0
  32. package/lib/hooks/useI18n.d.ts +5 -0
  33. package/lib/hooks/useI18n.js +16 -0
  34. package/lib/i18n/i18n.d.ts +14 -0
  35. package/lib/i18n/i18n.js +31 -0
  36. package/lib/i18n/index.d.ts +6 -0
  37. package/lib/i18n/index.js +7 -0
  38. package/lib/i18n/locale.d.ts +4 -0
  39. package/lib/i18n/locale.js +7 -0
  40. package/lib/i18n/locales.d.ts +36 -0
  41. package/lib/i18n/locales.js +37 -0
  42. package/lib/index.d.ts +1 -0
  43. package/lib/index.js +17 -0
  44. package/lib/llm/context.d.ts +3 -0
  45. package/lib/llm/context.js +10 -0
  46. package/lib/llm/engine.d.ts +6 -0
  47. package/lib/llm/engine.js +53 -0
  48. package/lib/styles.d.ts +6 -0
  49. package/lib/styles.js +18 -0
  50. package/lib/types/assistant.d.ts +53 -0
  51. package/lib/types/assistant.js +2 -0
  52. package/lib/utils/event.d.ts +8 -0
  53. package/lib/utils/event.js +28 -0
  54. package/lib/utils/parseStreamData.d.ts +5 -0
  55. package/lib/utils/parseStreamData.js +33 -0
  56. package/lib/utils/streamRequest.d.ts +4 -0
  57. package/lib/utils/streamRequest.js +150 -0
  58. package/package.json +75 -0
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ import { AIAssistantConfig } from './types/assistant';
3
+ export declare const AIConfigContext: React.Context<AIAssistantConfig>;
package/lib/context.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AIConfigContext = void 0;
7
+ var react_1 = __importDefault(require("react"));
8
+ exports.AIConfigContext = react_1.default.createContext({});
@@ -0,0 +1,14 @@
1
+ export type UseAsyncOps<T> = {
2
+ manual: boolean;
3
+ onSuccess?(result?: T): any;
4
+ onError?(err?: Error): any;
5
+ };
6
+ export type UseAsyncResult<A extends any[], T> = {
7
+ data: T;
8
+ loading: boolean;
9
+ setData(data: T): void;
10
+ run: (...args: A) => Promise<T>;
11
+ };
12
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>): UseAsyncResult<A, T>;
13
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>, deps: any[]): UseAsyncResult<A, T>;
14
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>, deps: any[], ops: UseAsyncOps<T>): UseAsyncResult<A, T>;
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.useAsync = useAsync;
49
+ var react_1 = require("react");
50
+ function useAsync(run, deps, ops) {
51
+ var _this = this;
52
+ if (deps === void 0) { deps = []; }
53
+ var _a = (0, react_1.useState)(), data = _a[0], setData = _a[1];
54
+ var _b = (0, react_1.useState)(), loading = _b[0], setLoading = _b[1];
55
+ var main = function () {
56
+ var args = [];
57
+ for (var _i = 0; _i < arguments.length; _i++) {
58
+ args[_i] = arguments[_i];
59
+ }
60
+ return __awaiter(_this, void 0, void 0, function () {
61
+ var result, error_1;
62
+ return __generator(this, function (_a) {
63
+ switch (_a.label) {
64
+ case 0:
65
+ _a.trys.push([0, 4, 7, 8]);
66
+ setLoading(true);
67
+ return [4 /*yield*/, run.apply(void 0, args)];
68
+ case 1:
69
+ result = _a.sent();
70
+ if (!(ops === null || ops === void 0 ? void 0 : ops.onSuccess)) return [3 /*break*/, 3];
71
+ return [4 /*yield*/, (ops === null || ops === void 0 ? void 0 : ops.onSuccess(result))];
72
+ case 2:
73
+ _a.sent();
74
+ _a.label = 3;
75
+ case 3:
76
+ setData(result);
77
+ return [2 /*return*/, result];
78
+ case 4:
79
+ error_1 = _a.sent();
80
+ if (!(ops === null || ops === void 0 ? void 0 : ops.onError)) return [3 /*break*/, 6];
81
+ return [4 /*yield*/, (ops === null || ops === void 0 ? void 0 : ops.onError(error_1))];
82
+ case 5:
83
+ _a.sent();
84
+ _a.label = 6;
85
+ case 6: return [3 /*break*/, 8];
86
+ case 7:
87
+ setLoading(false);
88
+ return [7 /*endfinally*/];
89
+ case 8: return [2 /*return*/];
90
+ }
91
+ });
92
+ });
93
+ };
94
+ (0, react_1.useEffect)(function () {
95
+ if (!(ops === null || ops === void 0 ? void 0 : ops.manual)) {
96
+ main();
97
+ }
98
+ }, __spreadArray([ops === null || ops === void 0 ? void 0 : ops.manual], deps, true));
99
+ return {
100
+ data: data,
101
+ loading: loading,
102
+ run: main,
103
+ setData: setData,
104
+ };
105
+ }
@@ -0,0 +1,5 @@
1
+ import ClipboardJS from 'clipboard';
2
+ export declare const useCopy: <Button extends HTMLElement = any, Target extends HTMLElement = any>(id: string, init?: (cp: ClipboardJS) => void | VoidFunction, deps?: any[]) => {
3
+ button: import("react").MutableRefObject<Button>;
4
+ target: import("react").MutableRefObject<Target>;
5
+ };
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.useCopy = void 0;
16
+ var clipboard_1 = __importDefault(require("clipboard"));
17
+ var react_1 = require("react");
18
+ var useCopy = function (id, init, deps) {
19
+ if (init === void 0) { init = function (cp) {
20
+ cp.on('success', function () { });
21
+ }; }
22
+ if (deps === void 0) { deps = []; }
23
+ var ref = (0, react_1.useRef)();
24
+ var targetRef = (0, react_1.useRef)();
25
+ (0, react_1.useEffect)(function () {
26
+ var targetId = "Clipboard-Target-".concat(id || Date.now());
27
+ if (ref.current) {
28
+ ref.current.setAttribute('data-clipboard-target', "#".concat(targetId));
29
+ }
30
+ else {
31
+ return;
32
+ }
33
+ if (targetRef.current) {
34
+ targetRef.current.id = targetId;
35
+ }
36
+ else {
37
+ ref.current.id = targetId;
38
+ }
39
+ var cp = new clipboard_1.default(ref.current);
40
+ init && init(cp);
41
+ return function () { return cp.destroy(); };
42
+ }, __spreadArray(__spreadArray([], deps, true), [id], false));
43
+ return {
44
+ button: ref,
45
+ target: targetRef,
46
+ };
47
+ };
48
+ exports.useCopy = useCopy;
@@ -0,0 +1,5 @@
1
+ import { i18nKeys } from '../i18n/locales';
2
+ export declare const useI18n: () => {
3
+ t: (key: i18nKeys) => string;
4
+ locale: "zh-CN" | "en-US";
5
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useI18n = void 0;
4
+ var react_1 = require("react");
5
+ var context_1 = require("../context");
6
+ var locales_1 = require("../i18n/locales");
7
+ var useI18n = function () {
8
+ var config = (0, react_1.useContext)(context_1.AIConfigContext);
9
+ var locale = config.locale || 'zh-CN';
10
+ var strings = locales_1.locales[locale] || locales_1.locales['zh-CN'];
11
+ var t = function (key) {
12
+ return strings[key] || key;
13
+ };
14
+ return { t: t, locale: locale };
15
+ };
16
+ exports.useI18n = useI18n;
@@ -0,0 +1,14 @@
1
+ export interface ITextMap {
2
+ [key: string]: string;
3
+ }
4
+ export type ILocalesTextMap = {
5
+ [locale: string]: ITextMap;
6
+ };
7
+ export declare class I18n<T extends ILocalesTextMap, M = T[keyof T]> {
8
+ private localesTextMap;
9
+ private currentTextMap;
10
+ locale: keyof T;
11
+ constructor(localesTextMap: T);
12
+ setLocal(locale: keyof T): void;
13
+ format(contentKey: keyof M, args?: Record<string, string>): string | keyof M;
14
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.I18n = void 0;
4
+ var template = function (tpl, values) {
5
+ return tpl.replace(/{\w+}/g, function (slot) { return values[slot.replace(/{|}/g, '')]; });
6
+ };
7
+ var I18n = /** @class */ (function () {
8
+ function I18n(localesTextMap) {
9
+ this.localesTextMap = localesTextMap;
10
+ this.currentTextMap = {};
11
+ }
12
+ I18n.prototype.setLocal = function (locale) {
13
+ this.locale = locale;
14
+ this.currentTextMap =
15
+ this.localesTextMap[locale] ||
16
+ this.localesTextMap[Object.keys(this.localesTextMap)[0]];
17
+ };
18
+ I18n.prototype.format = function (contentKey, args) {
19
+ var i18nformatString = this.currentTextMap[contentKey];
20
+ if (!i18nformatString) {
21
+ var currentTextMap = this.localesTextMap['en'] || {};
22
+ i18nformatString = currentTextMap[contentKey];
23
+ }
24
+ if (!i18nformatString) {
25
+ return contentKey;
26
+ }
27
+ return args ? template(i18nformatString, args) : i18nformatString;
28
+ };
29
+ return I18n;
30
+ }());
31
+ exports.I18n = I18n;
@@ -0,0 +1,6 @@
1
+ import { I18n } from './i18n';
2
+ declare const i18n: I18n<{
3
+ zh: {};
4
+ en: {};
5
+ }, {} | {}>;
6
+ export { i18n };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.i18n = void 0;
4
+ var i18n_1 = require("./i18n");
5
+ var locale_1 = require("./locale");
6
+ var i18n = new i18n_1.I18n(locale_1.localeMap);
7
+ exports.i18n = i18n;
@@ -0,0 +1,4 @@
1
+ export declare const localeMap: {
2
+ zh: {};
3
+ en: {};
4
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.localeMap = void 0;
4
+ exports.localeMap = {
5
+ zh: {},
6
+ en: {}
7
+ };
@@ -0,0 +1,36 @@
1
+ export declare const locales: {
2
+ 'zh-CN': {
3
+ assistantTitle: string;
4
+ newChat: string;
5
+ tabChat: string;
6
+ tabHistory: string;
7
+ loadingModel: string;
8
+ loadingTip: string;
9
+ noHistory: string;
10
+ defaultWelcome: string;
11
+ defaultPlaceholder: string;
12
+ defaultEmpty: string;
13
+ cancel: string;
14
+ send: string;
15
+ thinking: string;
16
+ newChatFallback: string;
17
+ };
18
+ 'en-US': {
19
+ assistantTitle: string;
20
+ newChat: string;
21
+ tabChat: string;
22
+ tabHistory: string;
23
+ loadingModel: string;
24
+ loadingTip: string;
25
+ noHistory: string;
26
+ defaultWelcome: string;
27
+ defaultPlaceholder: string;
28
+ defaultEmpty: string;
29
+ cancel: string;
30
+ send: string;
31
+ thinking: string;
32
+ newChatFallback: string;
33
+ };
34
+ };
35
+ export type LocaleType = keyof typeof locales;
36
+ export type i18nKeys = keyof typeof locales['zh-CN'];
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.locales = void 0;
4
+ exports.locales = {
5
+ 'zh-CN': {
6
+ assistantTitle: 'AI 助手',
7
+ newChat: '新对话',
8
+ tabChat: '对话',
9
+ tabHistory: '历史列表',
10
+ loadingModel: '正在初始化模型...',
11
+ loadingTip: '首次加载模型可能需要较长时间,请耐心等待',
12
+ noHistory: '暂无历史对话',
13
+ defaultWelcome: '有什么可以帮忙的?',
14
+ defaultPlaceholder: '给 GPT 发送消息',
15
+ defaultEmpty: '我是 AI,可以回答你的问题,请在下方输入框输入你的需求~',
16
+ cancel: '取消',
17
+ send: '发送',
18
+ thinking: '思考中',
19
+ newChatFallback: '新对话',
20
+ },
21
+ 'en-US': {
22
+ assistantTitle: 'AI Assistant',
23
+ newChat: 'New Chat',
24
+ tabChat: 'Chat',
25
+ tabHistory: 'History',
26
+ loadingModel: 'Initializing model...',
27
+ loadingTip: 'First load may take a while, please wait patiently',
28
+ noHistory: 'No history',
29
+ defaultWelcome: 'How can I help you?',
30
+ defaultPlaceholder: 'Send a message to GPT',
31
+ defaultEmpty: 'I am an AI, I can answer your questions. Please enter your needs in the input box below~',
32
+ cancel: 'Cancel',
33
+ send: 'Send',
34
+ thinking: 'Thinking...',
35
+ newChatFallback: 'New Chat',
36
+ }
37
+ };
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './chat';
package/lib/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./chat"), exports);
@@ -0,0 +1,3 @@
1
+ import { MLCEngine } from '@mlc-ai/web-llm';
2
+ export declare const LLMContext: import("react").Context<MLCEngine>;
3
+ export declare const useLLm: () => MLCEngine;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useLLm = exports.LLMContext = void 0;
4
+ var react_1 = require("react");
5
+ exports.LLMContext = (0, react_1.createContext)(null);
6
+ var useLLm = function () {
7
+ var context = (0, react_1.useContext)(exports.LLMContext);
8
+ return context;
9
+ };
10
+ exports.useLLm = useLLm;
@@ -0,0 +1,6 @@
1
+ import { InitProgressCallback } from "@mlc-ai/web-llm";
2
+ export interface GetLLmengineProps {
3
+ selectedModel?: string;
4
+ initProgressCallback?: InitProgressCallback;
5
+ }
6
+ export declare const getLLMengine: ({ selectedModel, initProgressCallback }: GetLLmengineProps) => Promise<import("@mlc-ai/web-llm").MLCEngine>;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.getLLMengine = void 0;
40
+ var web_llm_1 = require("@mlc-ai/web-llm");
41
+ var getLLMengine = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
42
+ var engine;
43
+ var _c = _b.selectedModel, selectedModel = _c === void 0 ? 'Llama-3.1-8B-Instruct-q4f32_1-MLC' : _c, initProgressCallback = _b.initProgressCallback;
44
+ return __generator(this, function (_d) {
45
+ switch (_d.label) {
46
+ case 0: return [4 /*yield*/, (0, web_llm_1.CreateMLCEngine)(selectedModel, { initProgressCallback: initProgressCallback })];
47
+ case 1:
48
+ engine = _d.sent();
49
+ return [2 /*return*/, engine];
50
+ }
51
+ });
52
+ }); };
53
+ exports.getLLMengine = getLLMengine;
@@ -0,0 +1,6 @@
1
+ export declare const Root: import("styled-components").StyledComponent<"div", any, {}, never>;
2
+ export declare const Layout: import("styled-components").StyledComponent<"div", any, {}, never>;
3
+ export declare const Aside: import("styled-components").StyledComponent<"div", any, {}, never>;
4
+ export declare const Content: import("styled-components").StyledComponent<"div", any, {}, never>;
5
+ export declare const MainContent: import("styled-components").StyledComponent<"div", any, {}, never>;
6
+ export declare const FloatButton: import("styled-components").StyledComponent<"div", any, {}, never>;
package/lib/styles.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
+ return cooked;
5
+ };
6
+ var __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.FloatButton = exports.MainContent = exports.Content = exports.Aside = exports.Layout = exports.Root = void 0;
11
+ var styled_components_1 = __importDefault(require("styled-components"));
12
+ exports.Root = styled_components_1.default.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n padding: 48px;\n"], ["\n padding: 48px;\n"])));
13
+ exports.Layout = styled_components_1.default.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n margin: 48px;\n border-radius: 4px;\n border: 1px solid #e5e5e5;\n display: flex;\n height: 600px;\n background-color: #fff;\n"], ["\n margin: 48px;\n border-radius: 4px;\n border: 1px solid #e5e5e5;\n display: flex;\n height: 600px;\n background-color: #fff;\n"])));
14
+ exports.Aside = styled_components_1.default.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n display: none;\n"], ["\n display: none;\n"])));
15
+ exports.Content = styled_components_1.default.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n padding: 12px 0px;\n flex-grow: 1;\n"], ["\n padding: 12px 0px;\n flex-grow: 1;\n"])));
16
+ exports.MainContent = styled_components_1.default.div(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n display: flex;\n justify-content: center;\n align-items: center;\n height: 100%;\n font-size: 18px;\n color: #999;\n"], ["\n display: flex;\n justify-content: center;\n align-items: center;\n height: 100%;\n font-size: 18px;\n color: #999;\n"])));
17
+ exports.FloatButton = styled_components_1.default.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n position: fixed;\n width: 56px;\n height: 56px;\n border-radius: 28px;\n background: linear-gradient(135deg, #1890ff 0%, #0050b3 100%);\n box-shadow: 0 4px 16px rgba(24, 144, 255, 0.35);\n display: flex;\n justify-content: center;\n align-items: center;\n cursor: grab;\n z-index: 1000;\n color: #fff;\n font-size: 24px;\n transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);\n border: 1px solid rgba(255, 255, 255, 0.1);\n backdrop-filter: blur(4px);\n\n &:active {\n cursor: grabbing;\n }\n\n &:hover {\n transform: scale(1.1) rotate(5deg);\n background: linear-gradient(135deg, #40a9ff 0%, #096dd9 100%);\n box-shadow: 0 8px 24px rgba(24, 144, 255, 0.45);\n }\n\n &::after {\n content: '';\n position: absolute;\n top: -2px;\n left: -2px;\n right: -2px;\n bottom: -2px;\n border-radius: 30px;\n background: linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0) 100%);\n opacity: 0.5;\n pointer-events: none;\n }\n\n .anticon {\n filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));\n }\n"], ["\n position: fixed;\n width: 56px;\n height: 56px;\n border-radius: 28px;\n background: linear-gradient(135deg, #1890ff 0%, #0050b3 100%);\n box-shadow: 0 4px 16px rgba(24, 144, 255, 0.35);\n display: flex;\n justify-content: center;\n align-items: center;\n cursor: grab;\n z-index: 1000;\n color: #fff;\n font-size: 24px;\n transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);\n border: 1px solid rgba(255, 255, 255, 0.1);\n backdrop-filter: blur(4px);\n\n &:active {\n cursor: grabbing;\n }\n\n &:hover {\n transform: scale(1.1) rotate(5deg);\n background: linear-gradient(135deg, #40a9ff 0%, #096dd9 100%);\n box-shadow: 0 8px 24px rgba(24, 144, 255, 0.45);\n }\n\n &::after {\n content: '';\n position: absolute;\n top: -2px;\n left: -2px;\n right: -2px;\n bottom: -2px;\n border-radius: 30px;\n background: linear-gradient(135deg, rgba(255, 255, 255, 0.4) 0%, rgba(255, 255, 255, 0) 100%);\n opacity: 0.5;\n pointer-events: none;\n }\n\n .anticon {\n filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));\n }\n"])));
18
+ var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6;
@@ -0,0 +1,53 @@
1
+ import { LocaleType } from '../i18n/locales';
2
+ import { MLCEngine } from '@mlc-ai/web-llm';
3
+ export interface ChatMessage {
4
+ role: 'system' | 'user' | 'assistant';
5
+ content: string;
6
+ }
7
+ export interface AIAssistantConfig {
8
+ /**
9
+ * The welcome message displayed at the top of the chat.
10
+ */
11
+ welcomeMessage?: string;
12
+ /**
13
+ * A list of suggested questions to show when the chat is empty.
14
+ */
15
+ suggestions?: string[];
16
+ /**
17
+ * The placeholder text for the message input box.
18
+ */
19
+ placeholder?: string;
20
+ /**
21
+ * The message displayed when the thread list is empty.
22
+ */
23
+ emptyMessage?: string;
24
+ /**
25
+ * The language locale for the UI.
26
+ * @default 'zh-CN'
27
+ */
28
+ locale?: LocaleType;
29
+ /**
30
+ * The ID of the container element where the assistant will be mounted.
31
+ * @default 'ai-assistant-root'
32
+ */
33
+ containerId?: string;
34
+ /**
35
+ * The initial coordinates of the floating button.
36
+ * @default { x: window.innerWidth - 100, y: window.innerHeight - 100 }
37
+ */
38
+ initialPosition?: {
39
+ x: number;
40
+ y: number;
41
+ };
42
+ /**
43
+ * A hook to intercept and modify conversation messages before they are sent to the AI.
44
+ * Useful for injecting system prompts, background knowledge, or filtering history.
45
+ *
46
+ * @example
47
+ * onBeforeChat: (messages) => {
48
+ * const systemPrompt = { role: 'system', content: 'You are a translation assistant' };
49
+ * return [systemPrompt, ...messages];
50
+ * }
51
+ */
52
+ onBeforeChat?: (messages: ChatMessage[], llm: MLCEngine) => ChatMessage[] | Promise<ChatMessage[]>;
53
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export declare class Dispatcher {
2
+ private constructor();
3
+ static instance: Dispatcher;
4
+ private events;
5
+ addEventListener(type: string, listener: Function): void;
6
+ removeEventListener(type: string, listener: Function): void;
7
+ dispatch(type: string, message: any): void;
8
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Dispatcher = void 0;
4
+ var Dispatcher = /** @class */ (function () {
5
+ function Dispatcher() {
6
+ this.events = {};
7
+ }
8
+ Dispatcher.prototype.addEventListener = function (type, listener) {
9
+ var listeners = this.events[type] || [];
10
+ listeners.push(listener);
11
+ this.events[type] = listeners;
12
+ };
13
+ Dispatcher.prototype.removeEventListener = function (type, listener) {
14
+ var listeners = this.events[type] || [];
15
+ var index = listeners.indexOf(listener);
16
+ listeners.splice(index, 1);
17
+ };
18
+ Dispatcher.prototype.dispatch = function (type, message) {
19
+ var listeners = this.events[type] || [];
20
+ for (var _i = 0, listeners_1 = listeners; _i < listeners_1.length; _i++) {
21
+ var listener = listeners_1[_i];
22
+ listener(message);
23
+ }
24
+ };
25
+ Dispatcher.instance = new Dispatcher();
26
+ return Dispatcher;
27
+ }());
28
+ exports.Dispatcher = Dispatcher;
@@ -0,0 +1,5 @@
1
+ export declare function parseStreamData(content: string): {
2
+ event: string;
3
+ id: string;
4
+ data: any;
5
+ }[];
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseStreamData = parseStreamData;
4
+ function parseStreamData(content) {
5
+ // 按行分割数据
6
+ var chunks = content.split('\n\n').filter(Boolean);
7
+ var result = [];
8
+ // 遍历行以解析事件类型、ID和数据
9
+ for (var _i = 0, chunks_1 = chunks; _i < chunks_1.length; _i++) {
10
+ var chunk = chunks_1[_i];
11
+ var event_1 = void 0;
12
+ var id = void 0;
13
+ var data = void 0;
14
+ var lines = chunk.split('\n');
15
+ for (var _a = 0, lines_1 = lines; _a < lines_1.length; _a++) {
16
+ var line = lines_1[_a];
17
+ if (line.startsWith('event:')) {
18
+ event_1 = line.replace('event:', '').trim();
19
+ }
20
+ if (line.startsWith('id:')) {
21
+ id = line.replace('id:', '').trim();
22
+ }
23
+ if (line.startsWith('data:')) {
24
+ try {
25
+ data = JSON.parse(line.replace('data:', '').trim());
26
+ }
27
+ catch (error) { }
28
+ }
29
+ }
30
+ result.push({ event: event_1, id: id, data: data });
31
+ }
32
+ return result;
33
+ }
@@ -0,0 +1,4 @@
1
+ import { ChatModelRunResult } from '@assistant-ui/react';
2
+ export declare function streamRequest(url: string, options: RequestInit & {
3
+ onChange?(type: 'complete' | 'running' | 'incomplete', data: string): void;
4
+ }): AsyncGenerator<ChatModelRunResult>;