@will1123/lx-ui-utils 1.0.1 → 1.0.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/README.md +747 -120
- package/dist/dist/index.esm.js +69039 -469
- package/dist/dist/index.esm.min.js +62900 -328
- package/dist/dist/index.esm.min.js.map +1 -1
- package/dist/dist/index.umd.js +69038 -468
- package/dist/dist/index.umd.min.js +346 -13
- package/dist/dist/index.umd.min.js.map +1 -1
- package/dist/es/helper.js +59 -0
- package/dist/es/helper.js.map +1 -0
- package/dist/es/index.js +13 -11
- package/dist/es/index.js.map +1 -1
- package/dist/es/markdown.js +248 -267
- package/dist/es/markdown.js.map +1 -1
- package/dist/es/sse.js +34 -142
- package/dist/es/sse.js.map +1 -1
- package/dist/es/thinking.js +19 -138
- package/dist/es/thinking.js.map +1 -1
- package/dist/lib/helper.js +59 -0
- package/dist/lib/helper.js.map +1 -0
- package/dist/lib/index.js +9 -7
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/markdown.js +249 -268
- package/dist/lib/markdown.js.map +1 -1
- package/dist/lib/sse.js +34 -142
- package/dist/lib/sse.js.map +1 -1
- package/dist/lib/thinking.js +19 -138
- package/dist/lib/thinking.js.map +1 -1
- package/dist/types/helper.d.ts +46 -0
- package/dist/types/helper.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/markdown.d.ts +69 -100
- package/dist/types/markdown.d.ts.map +1 -1
- package/dist/types/sse.d.ts +48 -48
- package/dist/types/sse.d.ts.map +1 -1
- package/dist/types/thinking.d.ts +25 -48
- package/dist/types/thinking.d.ts.map +1 -1
- package/package.json +19 -5
package/dist/lib/sse.js
CHANGED
|
@@ -36,151 +36,43 @@ var __async = (__this, __arguments, generator) => {
|
|
|
36
36
|
});
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}, timeout);
|
|
60
|
-
}
|
|
61
|
-
const response = yield fetch(url, {
|
|
62
|
-
method,
|
|
63
|
-
headers: __spreadValues({
|
|
64
|
-
"Content-Type": "application/json"
|
|
65
|
-
}, headers),
|
|
66
|
-
body: method === "POST" ? JSON.stringify(body) : void 0,
|
|
67
|
-
signal: this.controller.signal
|
|
39
|
+
const fetchEventSource = require("@microsoft/fetch-event-source");
|
|
40
|
+
function sse(config, handlers) {
|
|
41
|
+
return __async(this, null, function* () {
|
|
42
|
+
const { url, method = "POST", headers = {}, body, signal, token, openWhenHidden = true } = config;
|
|
43
|
+
const requestHeaders = __spreadValues({
|
|
44
|
+
"Content-Type": "application/json;charset=utf-8"
|
|
45
|
+
}, headers);
|
|
46
|
+
if (token) {
|
|
47
|
+
requestHeaders["Authorization"] = token;
|
|
48
|
+
}
|
|
49
|
+
yield fetchEventSource.fetchEventSource(url, {
|
|
50
|
+
method,
|
|
51
|
+
headers: requestHeaders,
|
|
52
|
+
body: JSON.stringify(body),
|
|
53
|
+
signal,
|
|
54
|
+
openWhenHidden,
|
|
55
|
+
onopen(response) {
|
|
56
|
+
return __async(this, null, function* () {
|
|
57
|
+
var _a2;
|
|
58
|
+
yield (_a2 = handlers.onOpen) == null ? void 0 : _a2.call(handlers, response);
|
|
68
59
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
(
|
|
81
|
-
|
|
82
|
-
const decoder = new TextDecoder();
|
|
83
|
-
let buffer = "";
|
|
84
|
-
if (!reader) {
|
|
85
|
-
throw new Error("Response body is null");
|
|
86
|
-
}
|
|
87
|
-
while (true) {
|
|
88
|
-
const { done, value } = yield reader.read();
|
|
89
|
-
if (done) {
|
|
90
|
-
(_c = handlers.onClose) == null ? void 0 : _c.call(handlers);
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
buffer += decoder.decode(value, { stream: true });
|
|
94
|
-
const lines = buffer.split("\n\n");
|
|
95
|
-
buffer = lines.pop() || "";
|
|
96
|
-
for (const line of lines) {
|
|
97
|
-
if (line.trim()) {
|
|
98
|
-
const message = this.parseMessage(line);
|
|
99
|
-
(_d = handlers.onMessage) == null ? void 0 : _d.call(handlers, message);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} catch (error) {
|
|
104
|
-
if (this.timeoutTimer) {
|
|
105
|
-
clearTimeout(this.timeoutTimer);
|
|
106
|
-
this.timeoutTimer = null;
|
|
107
|
-
}
|
|
108
|
-
if (this.controller && !this.controller.signal.aborted) {
|
|
109
|
-
(_e = handlers.onError) == null ? void 0 : _e.call(handlers, error);
|
|
110
|
-
}
|
|
60
|
+
},
|
|
61
|
+
onmessage(msg) {
|
|
62
|
+
var _a2;
|
|
63
|
+
(_a2 = handlers.onMessage) == null ? void 0 : _a2.call(handlers, msg);
|
|
64
|
+
},
|
|
65
|
+
onclose() {
|
|
66
|
+
var _a2;
|
|
67
|
+
(_a2 = handlers.onClose) == null ? void 0 : _a2.call(handlers);
|
|
68
|
+
},
|
|
69
|
+
onerror(err) {
|
|
70
|
+
var _a2;
|
|
71
|
+
(_a2 = handlers.onError) == null ? void 0 : _a2.call(handlers, err);
|
|
72
|
+
throw err;
|
|
111
73
|
}
|
|
112
74
|
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* 使用 EventSource 连接(仅支持 GET 请求)
|
|
116
|
-
*/
|
|
117
|
-
connect(url, handlers) {
|
|
118
|
-
this.eventSource = new EventSource(url);
|
|
119
|
-
this.eventSource.onopen = () => {
|
|
120
|
-
var _a;
|
|
121
|
-
(_a = handlers.onOpen) == null ? void 0 : _a.call(handlers);
|
|
122
|
-
};
|
|
123
|
-
this.eventSource.onmessage = (event) => {
|
|
124
|
-
var _a;
|
|
125
|
-
(_a = handlers.onMessage) == null ? void 0 : _a.call(handlers, {
|
|
126
|
-
data: event.data,
|
|
127
|
-
event: event.type
|
|
128
|
-
});
|
|
129
|
-
};
|
|
130
|
-
this.eventSource.onerror = (error) => {
|
|
131
|
-
var _a;
|
|
132
|
-
(_a = handlers.onError) == null ? void 0 : _a.call(handlers, new Error("EventSource error"));
|
|
133
|
-
this.close();
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* 解析 SSE 消息
|
|
138
|
-
*/
|
|
139
|
-
parseMessage(line) {
|
|
140
|
-
const message = {
|
|
141
|
-
data: ""
|
|
142
|
-
};
|
|
143
|
-
const lines = line.split("\n");
|
|
144
|
-
for (const l of lines) {
|
|
145
|
-
if (l.startsWith("data: ")) {
|
|
146
|
-
message.data = l.slice(6);
|
|
147
|
-
} else if (l.startsWith("event: ")) {
|
|
148
|
-
message.event = l.slice(7);
|
|
149
|
-
} else if (l.startsWith("id: ")) {
|
|
150
|
-
message.id = l.slice(4);
|
|
151
|
-
} else if (l.startsWith("retry: ")) {
|
|
152
|
-
message.retry = parseInt(l.slice(7));
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
return message;
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* 关闭连接
|
|
159
|
-
*/
|
|
160
|
-
close() {
|
|
161
|
-
if (this.controller) {
|
|
162
|
-
this.controller.abort();
|
|
163
|
-
this.controller = null;
|
|
164
|
-
}
|
|
165
|
-
if (this.eventSource) {
|
|
166
|
-
this.eventSource.close();
|
|
167
|
-
this.eventSource = null;
|
|
168
|
-
}
|
|
169
|
-
if (this.timeoutTimer) {
|
|
170
|
-
clearTimeout(this.timeoutTimer);
|
|
171
|
-
this.timeoutTimer = null;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
function sse(config, handlers) {
|
|
176
|
-
const connection = new SSEConnection();
|
|
177
|
-
if (config.method === "POST" || config.timeout) {
|
|
178
|
-
connection.request(config, handlers);
|
|
179
|
-
} else {
|
|
180
|
-
connection.connect(config.url, handlers);
|
|
181
|
-
}
|
|
182
|
-
return connection;
|
|
75
|
+
});
|
|
183
76
|
}
|
|
184
|
-
exports.SSEConnection = SSEConnection;
|
|
185
77
|
exports.sse = sse;
|
|
186
78
|
//# sourceMappingURL=sse.js.map
|
package/dist/lib/sse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse.js","sources":["../../src/sse.ts"],"sourcesContent":["/**\r\n * SSE
|
|
1
|
+
{"version":3,"file":"sse.js","sources":["../../src/sse.ts"],"sourcesContent":["import { fetchEventSource, type EventSourceMessage } from '@microsoft/fetch-event-source'\r\n\r\n// 重新导出 EventSourceMessage 类型\r\nexport type { EventSourceMessage }\r\n\r\n/**\r\n * SSE 请求配置\r\n */\r\nexport interface SSEConfig {\r\n /** 请求 URL */\r\n url: string\r\n /** 请求方法(默认 'POST') */\r\n method?: 'GET' | 'POST'\r\n /** 请求头 */\r\n headers?: Record<string, string>\r\n /** 请求体(对象类型) */\r\n body?: object\r\n /** AbortController 的 signal(用于取消请求) */\r\n signal?: AbortSignal\r\n /** 认证 Token */\r\n token?: string\r\n /** 页面隐藏时是否保持连接(默认 true) */\r\n openWhenHidden?: boolean\r\n}\r\n\r\n/**\r\n * SSE 事件处理器\r\n */\r\nexport interface SSEHandlers {\r\n /** 连接打开时触发(可验证响应) */\r\n onOpen?: (response: Response) => void | Promise<void>;\r\n /** 接收到消息时触发 */\r\n onMessage?: (message: EventSourceMessage) => void;\r\n /** 连接关闭时触发 */\r\n onClose?: () => void;\r\n /** 发生错误时触发 */\r\n onError?: (error: Error) => void;\r\n}\r\n\r\n/**\r\n * 发起 SSE 请求\r\n *\r\n * @example\r\n * ```ts\r\n * // 基础用法\r\n * await sse({\r\n * url: '/api/stream',\r\n * body: { prompt: 'hello' }\r\n * }, {\r\n * onMessage(msg) {\r\n * console.log(msg.data)\r\n * }\r\n * })\r\n *\r\n * // 使用 Token 认证\r\n * await sse({\r\n * url: '/api/stream',\r\n * token: 'Bearer your-token',\r\n * body: { prompt: 'hello' }\r\n * }, {\r\n * onOpen(response) {\r\n * console.log('连接已打开')\r\n * },\r\n * onMessage(msg) {\r\n * console.log(msg.data)\r\n * },\r\n * onError(err) {\r\n * console.error(err)\r\n * }\r\n * })\r\n * ```\r\n */\r\nexport async function sse(\r\n config: SSEConfig,\r\n handlers: SSEHandlers\r\n): Promise<void> {\r\n const { url, method = 'POST', headers = {}, body, signal, token, openWhenHidden = true } = config;\r\n\r\n // 构建请求头,自动添加 Authorization\r\n const requestHeaders: Record<string, string> = {\r\n 'Content-Type': 'application/json;charset=utf-8',\r\n ...headers\r\n };\r\n\r\n if (token) {\r\n requestHeaders['Authorization'] = token;\r\n }\r\n\r\n await fetchEventSource(url, {\r\n method,\r\n headers: requestHeaders,\r\n body: JSON.stringify(body),\r\n signal,\r\n openWhenHidden,\r\n\r\n async onopen(response) {\r\n await handlers.onOpen?.(response);\r\n },\r\n\r\n onmessage(msg) {\r\n handlers.onMessage?.(msg);\r\n },\r\n\r\n onclose() {\r\n handlers.onClose?.();\r\n },\r\n\r\n onerror(err) {\r\n handlers.onError?.(err);\r\n throw err;\r\n }\r\n });\r\n}\r\n"],"names":["fetchEventSource","_a"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,SAAsB,IACpB,QACA,UACe;AAAA;AACf,UAAM,EAAE,KAAK,SAAS,QAAQ,UAAU,CAAA,GAAI,MAAM,QAAQ,OAAO,iBAAiB,KAAA,IAAS;AAG3F,UAAM,iBAAyC;AAAA,MAC7C,gBAAgB;AAAA,OACb;AAGL,QAAI,OAAO;AACT,qBAAe,eAAe,IAAI;AAAA,IACpC;AAEA,UAAMA,iBAAAA,iBAAiB,KAAK;AAAA,MAC1B;AAAA,MACA,SAAS;AAAA,MACT,MAAM,KAAK,UAAU,IAAI;AAAA,MACzB;AAAA,MACA;AAAA,MAEM,OAAO,UAAU;AAAA;;AACrB,iBAAMC,MAAA,SAAS,WAAT,gBAAAA,IAAA,eAAkB;AAAA,QAC1B;AAAA;AAAA,MAEA,UAAU,KAAK;;AACb,SAAAA,MAAA,SAAS,cAAT,gBAAAA,IAAA,eAAqB;AAAA,MACvB;AAAA,MAEA,UAAU;;AACR,SAAAA,MAAA,SAAS,YAAT,gBAAAA,IAAA;AAAA,MACF;AAAA,MAEA,QAAQ,KAAK;;AACX,SAAAA,MAAA,SAAS,YAAT,gBAAAA,IAAA,eAAmB;AACnB,cAAM;AAAA,MACR;AAAA,IAAA,CACD;AAAA,EACH;AAAA;;"}
|
package/dist/lib/thinking.js
CHANGED
|
@@ -1,153 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
function extractThinking(text, config = {}) {
|
|
4
|
-
const { tagName = "think",
|
|
4
|
+
const { tagName = "think", mode = "full" } = config;
|
|
5
5
|
const openTag = `<${tagName}>`;
|
|
6
6
|
const closeTag = `</${tagName}>`;
|
|
7
7
|
const openIndex = text.indexOf(openTag);
|
|
8
|
-
const
|
|
9
|
-
if (
|
|
8
|
+
const closeTagIndex = text.indexOf(closeTag);
|
|
9
|
+
if (mode === "end-tag-only" && closeTagIndex !== -1) {
|
|
10
|
+
const thinking = text.slice(0, closeTagIndex).trim();
|
|
11
|
+
const content = text.slice(closeTagIndex + closeTag.length).trim();
|
|
10
12
|
return {
|
|
11
|
-
thinking
|
|
12
|
-
|
|
13
|
-
hasThinking: false
|
|
13
|
+
thinking,
|
|
14
|
+
content
|
|
14
15
|
};
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
thinking: includeTags ? `${openTag}${thinkingContent}${closeTag}` : thinkingContent,
|
|
24
|
-
rest: removeRest ? "" : restContent,
|
|
25
|
-
hasThinking: true
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
class ThinkingStreamExtractor {
|
|
29
|
-
constructor(config = {}) {
|
|
30
|
-
this.isComplete = false;
|
|
31
|
-
this.config = {
|
|
32
|
-
tagName: config.tagName || "think",
|
|
33
|
-
includeTags: config.includeTags || false,
|
|
34
|
-
removeRest: config.removeRest || false
|
|
35
|
-
};
|
|
36
|
-
this.state = {
|
|
37
|
-
inThinking: false,
|
|
38
|
-
tagBuffer: "",
|
|
39
|
-
contentBuffer: "",
|
|
40
|
-
beforeContent: "",
|
|
41
|
-
afterContent: ""
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* 追加文本并提取思考内容
|
|
46
|
-
*/
|
|
47
|
-
append(text) {
|
|
48
|
-
if (this.isComplete) {
|
|
49
|
-
return {
|
|
50
|
-
thinking: this.getThinking(),
|
|
51
|
-
rest: this.getRest(),
|
|
52
|
-
isComplete: true
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
const { tagName } = this.config;
|
|
56
|
-
const openTag = `<${tagName}>`;
|
|
57
|
-
const closeTag = `</${tagName}>`;
|
|
58
|
-
for (let i = 0; i < text.length; i++) {
|
|
59
|
-
const char = text[i];
|
|
60
|
-
if (!this.state.inThinking) {
|
|
61
|
-
this.state.tagBuffer += char;
|
|
62
|
-
if (this.state.tagBuffer.endsWith(openTag)) {
|
|
63
|
-
const tagStart = this.state.tagBuffer.length - openTag.length;
|
|
64
|
-
this.state.beforeContent += this.state.tagBuffer.slice(0, tagStart);
|
|
65
|
-
this.state.tagBuffer = "";
|
|
66
|
-
this.state.inThinking = true;
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
if (this.state.tagBuffer.length > openTag.length) {
|
|
70
|
-
this.state.beforeContent += this.state.tagBuffer.slice(0, 1);
|
|
71
|
-
this.state.tagBuffer = this.state.tagBuffer.slice(1);
|
|
72
|
-
}
|
|
73
|
-
} else {
|
|
74
|
-
this.state.tagBuffer += char;
|
|
75
|
-
if (this.state.tagBuffer.endsWith(closeTag)) {
|
|
76
|
-
const tagEnd = this.state.tagBuffer.length - closeTag.length;
|
|
77
|
-
this.state.contentBuffer += this.state.tagBuffer.slice(0, tagEnd);
|
|
78
|
-
this.state.tagBuffer = "";
|
|
79
|
-
this.state.inThinking = false;
|
|
80
|
-
this.isComplete = true;
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
if (this.state.tagBuffer.length > closeTag.length) {
|
|
84
|
-
const overflow = this.state.tagBuffer.slice(0, 1);
|
|
85
|
-
this.state.contentBuffer += overflow;
|
|
86
|
-
this.state.tagBuffer = this.state.tagBuffer.slice(1);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (!this.state.inThinking && this.state.tagBuffer) {
|
|
91
|
-
if (this.isComplete) {
|
|
92
|
-
this.state.afterContent += this.state.tagBuffer;
|
|
93
|
-
} else {
|
|
94
|
-
this.state.beforeContent += this.state.tagBuffer;
|
|
95
|
-
}
|
|
96
|
-
this.state.tagBuffer = "";
|
|
97
|
-
}
|
|
17
|
+
if (openIndex !== -1 && closeTagIndex !== -1) {
|
|
18
|
+
const thinkingStart = openIndex + openTag.length;
|
|
19
|
+
const thinkingContent = text.slice(thinkingStart, closeTagIndex).trim();
|
|
20
|
+
const beforeContent = text.slice(0, openIndex);
|
|
21
|
+
const afterContent = text.slice(closeTagIndex + closeTag.length);
|
|
22
|
+
const content = (beforeContent + afterContent).trim();
|
|
98
23
|
return {
|
|
99
|
-
thinking:
|
|
100
|
-
|
|
101
|
-
isComplete: this.isComplete
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* 获取当前提取的思考内容
|
|
106
|
-
*/
|
|
107
|
-
getThinking() {
|
|
108
|
-
const { tagName, includeTags } = this.config;
|
|
109
|
-
const openTag = `<${tagName}>`;
|
|
110
|
-
const closeTag = `</${tagName}>`;
|
|
111
|
-
const content = this.state.contentBuffer;
|
|
112
|
-
if (includeTags) {
|
|
113
|
-
return this.state.inThinking ? `${openTag}${content}` : `${openTag}${content}${closeTag}`;
|
|
114
|
-
}
|
|
115
|
-
return content;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* 获取剩余内容
|
|
119
|
-
*/
|
|
120
|
-
getRest() {
|
|
121
|
-
const { removeRest } = this.config;
|
|
122
|
-
if (removeRest) {
|
|
123
|
-
return "";
|
|
124
|
-
}
|
|
125
|
-
return this.state.beforeContent + this.state.afterContent;
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* 是否已完成提取
|
|
129
|
-
*/
|
|
130
|
-
complete() {
|
|
131
|
-
return this.isComplete;
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* 重置提取器
|
|
135
|
-
*/
|
|
136
|
-
reset() {
|
|
137
|
-
this.state = {
|
|
138
|
-
inThinking: false,
|
|
139
|
-
tagBuffer: "",
|
|
140
|
-
contentBuffer: "",
|
|
141
|
-
beforeContent: "",
|
|
142
|
-
afterContent: ""
|
|
24
|
+
thinking: thinkingContent,
|
|
25
|
+
content
|
|
143
26
|
};
|
|
144
|
-
this.isComplete = false;
|
|
145
27
|
}
|
|
28
|
+
return {
|
|
29
|
+
thinking: "",
|
|
30
|
+
content: text.trim()
|
|
31
|
+
};
|
|
146
32
|
}
|
|
147
|
-
function createThinkingExtractor(config) {
|
|
148
|
-
return new ThinkingStreamExtractor(config);
|
|
149
|
-
}
|
|
150
|
-
exports.ThinkingStreamExtractor = ThinkingStreamExtractor;
|
|
151
|
-
exports.createThinkingExtractor = createThinkingExtractor;
|
|
152
33
|
exports.extractThinking = extractThinking;
|
|
153
34
|
//# sourceMappingURL=thinking.js.map
|
package/dist/lib/thinking.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thinking.js","sources":["../../src/thinking.ts"],"sourcesContent":["/**\r\n *
|
|
1
|
+
{"version":3,"file":"thinking.js","sources":["../../src/thinking.ts"],"sourcesContent":["/**\r\n * 深度思考提取配置\r\n */\r\nexport interface ThinkingExtractConfig {\r\n /** 思考标签名称(默认为 'think') */\r\n tagName?: string\r\n /** 解析模式:'full'(完整标签,默认)| 'end-tag-only'(仅结束标签) */\r\n mode?: 'full' | 'end-tag-only'\r\n}\r\n\r\n/**\r\n * 深度思考提取结果\r\n */\r\nexport interface ThinkingResult {\r\n /** 提取的思考内容 */\r\n thinking: string\r\n /** 实际回答内容 */\r\n content: string\r\n}\r\n\r\n/**\r\n * 从大模型响应中提取深度思考\r\n *\r\n * @example\r\n * ```ts\r\n * // 默认模式:完整标签\r\n * const text1 = '这是回答内容'\r\n * const result1 = extractThinking(text1)\r\n * // { thinking: '这是思考内容', content: '这是回答内容' }\r\n *\r\n * // end-tag-only 模式:只有结束标签\r\n * const text2 = '这是前面的思考这是后面的正文'\r\n * const result2 = extractThinking(text2, { mode: 'end-tag-only' })\r\n * // { thinking: '这是前面的思考', content: '这是后面的正文' }\r\n *\r\n * // 没有标签时\r\n * const text3 = '全部是正文内容'\r\n * const result3 = extractThinking(text3)\r\n * // { thinking: '', content: '全部是正文内容' }\r\n * ```\r\n */\r\nexport function extractThinking(\r\n text: string,\r\n config: ThinkingExtractConfig = {}\r\n): ThinkingResult {\r\n const { tagName = 'think', mode = 'full' } = config\r\n\r\n const openTag = `<${tagName}>`\r\n const closeTag = `</${tagName}>`\r\n\r\n const openIndex = text.indexOf(openTag)\r\n const closeTagIndex = text.indexOf(closeTag)\r\n\r\n // end-tag-only 模式:只有结束标签\r\n if (mode === 'end-tag-only' && closeTagIndex !== -1) {\r\n const thinking = text.slice(0, closeTagIndex).trim()\r\n const content = text.slice(closeTagIndex + closeTag.length).trim()\r\n\r\n return {\r\n thinking: thinking,\r\n content: content\r\n }\r\n }\r\n\r\n // full 模式(默认):完整标签 内容\r\n if (openIndex !== -1 && closeTagIndex !== -1) {\r\n const thinkingStart = openIndex + openTag.length\r\n const thinkingContent = text.slice(thinkingStart, closeTagIndex).trim()\r\n\r\n const beforeContent = text.slice(0, openIndex)\r\n const afterContent = text.slice(closeTagIndex + closeTag.length)\r\n const content = (beforeContent + afterContent).trim()\r\n\r\n return {\r\n thinking: thinkingContent,\r\n content: content\r\n }\r\n }\r\n\r\n // 没有找到标签,全部是正文\r\n return {\r\n thinking: '',\r\n content: text.trim()\r\n }\r\n}\r\n"],"names":[],"mappings":";;AAyCO,SAAS,gBACd,MACA,SAAgC,IAChB;AAChB,QAAM,EAAE,UAAU,SAAS,OAAO,WAAW;AAE7C,QAAM,UAAU,IAAI,OAAO;AAC3B,QAAM,WAAW,KAAK,OAAO;AAE7B,QAAM,YAAY,KAAK,QAAQ,OAAO;AACtC,QAAM,gBAAgB,KAAK,QAAQ,QAAQ;AAG3C,MAAI,SAAS,kBAAkB,kBAAkB,IAAI;AACnD,UAAM,WAAW,KAAK,MAAM,GAAG,aAAa,EAAE,KAAA;AAC9C,UAAM,UAAU,KAAK,MAAM,gBAAgB,SAAS,MAAM,EAAE,KAAA;AAE5D,WAAO;AAAA,MACL;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAGA,MAAI,cAAc,MAAM,kBAAkB,IAAI;AAC5C,UAAM,gBAAgB,YAAY,QAAQ;AAC1C,UAAM,kBAAkB,KAAK,MAAM,eAAe,aAAa,EAAE,KAAA;AAEjE,UAAM,gBAAgB,KAAK,MAAM,GAAG,SAAS;AAC7C,UAAM,eAAe,KAAK,MAAM,gBAAgB,SAAS,MAAM;AAC/D,UAAM,WAAW,gBAAgB,cAAc,KAAA;AAE/C,WAAO;AAAA,MACL,UAAU;AAAA,MACV;AAAA,IAAA;AAAA,EAEJ;AAGA,SAAO;AAAA,IACL,UAAU;AAAA,IACV,SAAS,KAAK,KAAA;AAAA,EAAK;AAEvB;;"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 复制文本到剪贴板
|
|
3
|
+
*
|
|
4
|
+
* @param text - 要复制的文本
|
|
5
|
+
* @returns Promise<boolean> - 成功返回 true,失败返回 false
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { copyToClipboard } from '@will1123/lx-ui-utils'
|
|
10
|
+
*
|
|
11
|
+
* // 基础用法
|
|
12
|
+
* const success = await copyToClipboard('Hello World')
|
|
13
|
+
* if (success) {
|
|
14
|
+
* console.log('复制成功')
|
|
15
|
+
* } else {
|
|
16
|
+
* console.log('复制失败')
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* // 复制代码
|
|
20
|
+
* const code = 'console.log("Hello World")'
|
|
21
|
+
* await copyToClipboard(code)
|
|
22
|
+
*
|
|
23
|
+
* // 复制 HTML 内容
|
|
24
|
+
* const html = '<div>Hello</div>'
|
|
25
|
+
* await copyToClipboard(html)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function copyToClipboard(text: string): Promise<boolean>;
|
|
29
|
+
/**
|
|
30
|
+
* 判断值是否为 DOM 元素(HTMLElement 或 Document)
|
|
31
|
+
*
|
|
32
|
+
* @param val - 要判断的值
|
|
33
|
+
* @returns 是否为 DOM 元素
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* import { isDom } from '@will1123/lx-ui-utils'
|
|
38
|
+
*
|
|
39
|
+
* isDom(document.body) // true
|
|
40
|
+
* isDom(document) // true
|
|
41
|
+
* isDom({}) // false
|
|
42
|
+
* isDom('div') // false
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function isDom(val: unknown): val is HTMLElement | Document;
|
|
46
|
+
//# sourceMappingURL=helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../src/helper.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAiCpE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,WAAW,GAAG,QAAQ,CAEjE"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,cAAc,OAAO,CAAA;AACrB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,cAAc,OAAO,CAAA;AACrB,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA"}
|