@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.
- package/ReadMe.md +57 -0
- package/lib/app.d.ts +1 -0
- package/lib/app.js +27 -0
- package/lib/chat.d.ts +3 -0
- package/lib/chat.js +222 -0
- package/lib/chat.style.d.ts +12 -0
- package/lib/chat.style.js +20 -0
- package/lib/components/Codeblock/index.d.ts +9 -0
- package/lib/components/Codeblock/index.js +55 -0
- package/lib/components/Codeblock/index.style.d.ts +1 -0
- package/lib/components/Codeblock/index.style.js +13 -0
- package/lib/components/MarkdownText/index.d.ts +3 -0
- package/lib/components/MarkdownText/index.js +71 -0
- package/lib/components/MyRuntimeProvider/index.d.ts +5 -0
- package/lib/components/MyRuntimeProvider/index.js +22 -0
- package/lib/components/MyRuntimeProvider/myModelAdapterStream.d.ts +4 -0
- package/lib/components/MyRuntimeProvider/myModelAdapterStream.js +180 -0
- package/lib/components/Thread/index.d.ts +2 -0
- package/lib/components/Thread/index.js +286 -0
- package/lib/components/Thread/index.style.d.ts +15 -0
- package/lib/components/Thread/index.style.js +18 -0
- package/lib/components/ThreadList/index.d.ts +4 -0
- package/lib/components/ThreadList/index.js +131 -0
- package/lib/constants/index.d.ts +1 -0
- package/lib/constants/index.js +4 -0
- package/lib/context.d.ts +3 -0
- package/lib/context.js +8 -0
- package/lib/hooks/useAsync.d.ts +14 -0
- package/lib/hooks/useAsync.js +105 -0
- package/lib/hooks/useCopy.d.ts +5 -0
- package/lib/hooks/useCopy.js +48 -0
- package/lib/hooks/useI18n.d.ts +5 -0
- package/lib/hooks/useI18n.js +16 -0
- package/lib/i18n/i18n.d.ts +14 -0
- package/lib/i18n/i18n.js +31 -0
- package/lib/i18n/index.d.ts +6 -0
- package/lib/i18n/index.js +7 -0
- package/lib/i18n/locale.d.ts +4 -0
- package/lib/i18n/locale.js +7 -0
- package/lib/i18n/locales.d.ts +36 -0
- package/lib/i18n/locales.js +37 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +17 -0
- package/lib/llm/context.d.ts +3 -0
- package/lib/llm/context.js +10 -0
- package/lib/llm/engine.d.ts +6 -0
- package/lib/llm/engine.js +53 -0
- package/lib/styles.d.ts +6 -0
- package/lib/styles.js +18 -0
- package/lib/types/assistant.d.ts +53 -0
- package/lib/types/assistant.js +2 -0
- package/lib/utils/event.d.ts +8 -0
- package/lib/utils/event.js +28 -0
- package/lib/utils/parseStreamData.d.ts +5 -0
- package/lib/utils/parseStreamData.js +33 -0
- package/lib/utils/streamRequest.d.ts +4 -0
- package/lib/utils/streamRequest.js +150 -0
- package/package.json +75 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
3
|
+
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);
|
|
4
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
5
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
6
|
+
function step(op) {
|
|
7
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
8
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
9
|
+
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;
|
|
10
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
11
|
+
switch (op[0]) {
|
|
12
|
+
case 0: case 1: t = op; break;
|
|
13
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
14
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
15
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
16
|
+
default:
|
|
17
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
18
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
19
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
20
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
21
|
+
if (t[2]) _.ops.pop();
|
|
22
|
+
_.trys.pop(); continue;
|
|
23
|
+
}
|
|
24
|
+
op = body.call(thisArg, _);
|
|
25
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
26
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
|
30
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
|
31
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
32
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
|
33
|
+
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
|
34
|
+
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
|
|
35
|
+
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
|
|
36
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
|
37
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
|
38
|
+
function fulfill(value) { resume("next", value); }
|
|
39
|
+
function reject(value) { resume("throw", value); }
|
|
40
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
|
41
|
+
};
|
|
42
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.streamRequest = streamRequest;
|
|
44
|
+
var parseStreamData_1 = require("./parseStreamData");
|
|
45
|
+
function streamRequest(url, options) {
|
|
46
|
+
return __asyncGenerator(this, arguments, function streamRequest_1() {
|
|
47
|
+
var result, reader, currentContent, reasonContent, chunks, _a, done, value, text, result_2, _i, result_1, parserRes, _b, content, reasoning_content, error_1;
|
|
48
|
+
return __generator(this, function (_c) {
|
|
49
|
+
switch (_c.label) {
|
|
50
|
+
case 0: return [4 /*yield*/, __await(fetch(url, options))];
|
|
51
|
+
case 1:
|
|
52
|
+
result = _c.sent();
|
|
53
|
+
reader = result.body.getReader();
|
|
54
|
+
currentContent = '';
|
|
55
|
+
reasonContent = '';
|
|
56
|
+
chunks = '';
|
|
57
|
+
_c.label = 2;
|
|
58
|
+
case 2:
|
|
59
|
+
if (!true) return [3 /*break*/, 18];
|
|
60
|
+
_c.label = 3;
|
|
61
|
+
case 3:
|
|
62
|
+
_c.trys.push([3, 13, , 17]);
|
|
63
|
+
return [4 /*yield*/, __await(reader.read()
|
|
64
|
+
// 流读取完成
|
|
65
|
+
)];
|
|
66
|
+
case 4:
|
|
67
|
+
_a = _c.sent(), done = _a.done, value = _a.value;
|
|
68
|
+
if (!done) return [3 /*break*/, 7];
|
|
69
|
+
options.onChange && options.onChange('complete', currentContent);
|
|
70
|
+
return [4 /*yield*/, __await({
|
|
71
|
+
status: {
|
|
72
|
+
type: 'complete',
|
|
73
|
+
reason: 'stop',
|
|
74
|
+
},
|
|
75
|
+
content: [
|
|
76
|
+
{
|
|
77
|
+
text: currentContent,
|
|
78
|
+
type: 'text',
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
})];
|
|
82
|
+
case 5: return [4 /*yield*/, _c.sent()];
|
|
83
|
+
case 6:
|
|
84
|
+
_c.sent();
|
|
85
|
+
return [3 /*break*/, 18];
|
|
86
|
+
case 7:
|
|
87
|
+
text = new TextDecoder().decode(value, { stream: true });
|
|
88
|
+
chunks += text;
|
|
89
|
+
result_2 = (0, parseStreamData_1.parseStreamData)(chunks);
|
|
90
|
+
_i = 0, result_1 = result_2;
|
|
91
|
+
_c.label = 8;
|
|
92
|
+
case 8:
|
|
93
|
+
if (!(_i < result_1.length)) return [3 /*break*/, 12];
|
|
94
|
+
parserRes = result_1[_i];
|
|
95
|
+
if (!(parserRes.event === 'answer')) return [3 /*break*/, 11];
|
|
96
|
+
_b = parserRes.data.delta, content = _b.content, reasoning_content = _b.reasoning_content;
|
|
97
|
+
currentContent += content || '';
|
|
98
|
+
reasonContent += reasoning_content || '';
|
|
99
|
+
options.onChange && options.onChange('running', currentContent);
|
|
100
|
+
return [4 /*yield*/, __await({
|
|
101
|
+
status: {
|
|
102
|
+
type: 'running',
|
|
103
|
+
},
|
|
104
|
+
content: [
|
|
105
|
+
{
|
|
106
|
+
text: currentContent,
|
|
107
|
+
type: 'text',
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
})];
|
|
111
|
+
case 9: return [4 /*yield*/, _c.sent()];
|
|
112
|
+
case 10:
|
|
113
|
+
_c.sent();
|
|
114
|
+
_c.label = 11;
|
|
115
|
+
case 11:
|
|
116
|
+
_i++;
|
|
117
|
+
return [3 /*break*/, 8];
|
|
118
|
+
case 12:
|
|
119
|
+
chunks = '';
|
|
120
|
+
return [3 /*break*/, 17];
|
|
121
|
+
case 13:
|
|
122
|
+
error_1 = _c.sent();
|
|
123
|
+
// 点击了取消
|
|
124
|
+
console.error('Stream reading error:', error_1);
|
|
125
|
+
if (!((error_1 === null || error_1 === void 0 ? void 0 : error_1.name) === 'AbortError')) return [3 /*break*/, 16];
|
|
126
|
+
options.signal && options.signal.throwIfAborted();
|
|
127
|
+
options.onChange && options.onChange('incomplete', reasonContent);
|
|
128
|
+
return [4 /*yield*/, __await({
|
|
129
|
+
status: {
|
|
130
|
+
type: 'incomplete',
|
|
131
|
+
reason: 'error',
|
|
132
|
+
},
|
|
133
|
+
content: [
|
|
134
|
+
{
|
|
135
|
+
text: reasonContent,
|
|
136
|
+
type: 'reasoning',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
})];
|
|
140
|
+
case 14: return [4 /*yield*/, _c.sent()];
|
|
141
|
+
case 15:
|
|
142
|
+
_c.sent();
|
|
143
|
+
return [3 /*break*/, 18];
|
|
144
|
+
case 16: return [3 /*break*/, 17];
|
|
145
|
+
case 17: return [3 /*break*/, 2];
|
|
146
|
+
case 18: return [2 /*return*/];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@saber2pr/ai-assistant",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "AI Assistant",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"start": "cross-env NODE_ENV=development webpack -w",
|
|
13
|
+
"dev": "cross-env NODE_ENV=development webpack-dev-server",
|
|
14
|
+
"lint": "prettier --write ./src",
|
|
15
|
+
"build": "rimraf ./build && cross-env NODE_ENV=production webpack",
|
|
16
|
+
"prepublishOnly": "yarn tsc",
|
|
17
|
+
"prepare": "husky install"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"AI Assistant"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@assistant-ui/react": "^0.8.6",
|
|
25
|
+
"@assistant-ui/react-ai-sdk": "^0.8.0",
|
|
26
|
+
"@assistant-ui/react-markdown": "^0.8.0",
|
|
27
|
+
"@mlc-ai/web-llm": "^0.2.80",
|
|
28
|
+
"antd": "~4",
|
|
29
|
+
"clipboard": "^2.0.11",
|
|
30
|
+
"normalize.css": "^8.0.1",
|
|
31
|
+
"react": "^18.1.0",
|
|
32
|
+
"react-dom": "^18.1.0",
|
|
33
|
+
"react-hook-speech-to-text": "^0.8.0",
|
|
34
|
+
"react-syntax-highlighter": "^15.6.1",
|
|
35
|
+
"remark-gfm": "^4.0.1",
|
|
36
|
+
"styled-components": "^5.3.5"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@babel/core": "^7.17.5",
|
|
40
|
+
"dotenv": "^16.4.7",
|
|
41
|
+
"@babel/preset-env": "^7.18.2",
|
|
42
|
+
"@babel/preset-react": "^7.17.12",
|
|
43
|
+
"@babel/preset-typescript": "^7.17.12",
|
|
44
|
+
"@types/react": "^18.0.9",
|
|
45
|
+
"@types/react-dom": "^18.0.5",
|
|
46
|
+
"@types/styled-components": "^5.1.25",
|
|
47
|
+
"@types/webpack": "^5.28.0",
|
|
48
|
+
"@types/webpack-dev-server": "^4.7.2",
|
|
49
|
+
"babel-loader": "^8.2.3",
|
|
50
|
+
"babel-plugin-import": "^1.13.3",
|
|
51
|
+
"babel-plugin-module-resolver": "^4.1.0",
|
|
52
|
+
"cross-env": "^6.0.0",
|
|
53
|
+
"css-loader": "^2.1.1",
|
|
54
|
+
"html-webpack-plugin": "^5.5.0",
|
|
55
|
+
"husky": "^7.0.4",
|
|
56
|
+
"less": "^3.9.0",
|
|
57
|
+
"less-loader": "^5.0.0",
|
|
58
|
+
"lint-staged": "^12.3.4",
|
|
59
|
+
"mini-css-extract-plugin": "^1.6.1",
|
|
60
|
+
"prettier": "^2.5.1",
|
|
61
|
+
"rimraf": "^3.0.2",
|
|
62
|
+
"style-loader": "^0.23.1",
|
|
63
|
+
"typescript": "^5.9.3",
|
|
64
|
+
"url-loader": "^2.0.0",
|
|
65
|
+
"webpack": "^5.69.1",
|
|
66
|
+
"webpack-cli": "^4.9.2",
|
|
67
|
+
"webpack-dev-server": "^4.7.4"
|
|
68
|
+
},
|
|
69
|
+
"lint-staged": {
|
|
70
|
+
"*.{js,jsx,ts,tsx}": [
|
|
71
|
+
"yarn lint",
|
|
72
|
+
"git add ."
|
|
73
|
+
]
|
|
74
|
+
}
|
|
75
|
+
}
|