@celhive/tool.js 0.0.6 → 0.0.8

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/lib/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './formatCitation';
2
2
  export * from './get-pdf-viewer-urll';
3
3
  export * from './markdown-patch';
4
4
  export * from './mock-stream';
5
+ export * from './stable-streaming';
5
6
  export * from './token-exceeded';
package/lib/index.js CHANGED
@@ -18,4 +18,5 @@ __exportStar(require("./formatCitation"), exports);
18
18
  __exportStar(require("./get-pdf-viewer-urll"), exports);
19
19
  __exportStar(require("./markdown-patch"), exports);
20
20
  __exportStar(require("./mock-stream"), exports);
21
+ __exportStar(require("./stable-streaming"), exports);
21
22
  __exportStar(require("./token-exceeded"), exports);
@@ -42,7 +42,33 @@ const markdownPatch = (md) => {
42
42
  // 写一个正则,如果 ```mermaid 前面只有一个换行符,就添加一个换行符
43
43
  const mendMermaid = mendBold.replace(/(\n)```mermaid/g, '$1\n```mermaid');
44
44
  // 在连续的 # 符号(1个以上)前面插入换行符,但不处理行首的 #
45
- const result = mendMermaid.replace(/([^\n#])(#{2,})/g, '$1\n$2');
45
+ const mendHashNewline = mendMermaid.replace(/([^\n#])(#{2,})/g, '$1\n$2');
46
+ // 再次处理新行中的标题格式,确保 # 和内容之间有空格
47
+ const lines2 = mendHashNewline.split('\n');
48
+ let inCodeBlock2 = false;
49
+ const result = lines2
50
+ .map((line) => {
51
+ // 检查是否是代码块的开始或结束
52
+ if (line.trim().startsWith('```')) {
53
+ inCodeBlock2 = !inCodeBlock2;
54
+ return line;
55
+ }
56
+ // 如果在代码块内,跳过处理
57
+ if (inCodeBlock2) {
58
+ return line;
59
+ }
60
+ const match = line.match(/^(\s*)(#+)(\s*)(.*)$/);
61
+ if (match) {
62
+ const [, leadingSpaces, hashes, spaces, content] = match;
63
+ if (hashes.length > 0 &&
64
+ spaces.length === 0 &&
65
+ !content.startsWith('#')) {
66
+ return `${leadingSpaces}${hashes} ${content}`;
67
+ }
68
+ }
69
+ return line;
70
+ })
71
+ .join('\n');
46
72
  return result;
47
73
  };
48
74
  exports.markdownPatch = markdownPatch;
@@ -0,0 +1,20 @@
1
+ interface stableStreamingProps {
2
+ /** Number of characters output per millisecond */
3
+ ratePerMs: number;
4
+ /** Callback invoked with each character */
5
+ callback: (char: string) => void;
6
+ }
7
+ /**
8
+ * StableStreaming class to stream characters at a stable rate.
9
+ * It ensures that characters are output at the specified rate per millisecond,
10
+ */
11
+ export declare class StableStreaming {
12
+ private options;
13
+ private queue;
14
+ private timer;
15
+ private pendingResolvers;
16
+ constructor(options: stableStreamingProps);
17
+ streaming(chars: string): Promise<void>;
18
+ private start;
19
+ }
20
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StableStreaming = void 0;
4
+ /**
5
+ * StableStreaming class to stream characters at a stable rate.
6
+ * It ensures that characters are output at the specified rate per millisecond,
7
+ */
8
+ class StableStreaming {
9
+ constructor(options) {
10
+ this.queue = [];
11
+ this.timer = null;
12
+ this.pendingResolvers = [];
13
+ if (!options || options.ratePerMs <= 0) {
14
+ throw new Error('ratePerMs must be greater than 0');
15
+ }
16
+ this.options = options;
17
+ }
18
+ streaming(chars) {
19
+ if (!chars)
20
+ return Promise.resolve();
21
+ this.queue.push(...chars.split(''));
22
+ return new Promise((resolve) => {
23
+ this.pendingResolvers.push(resolve);
24
+ if (!this.timer) {
25
+ this.start();
26
+ }
27
+ });
28
+ }
29
+ start() {
30
+ const charsPerTick = Math.max(1, Math.floor(this.options.ratePerMs));
31
+ this.timer = setInterval(() => {
32
+ let remaining = charsPerTick;
33
+ while (remaining > 0 && this.queue.length) {
34
+ const char = this.queue.shift();
35
+ if (char !== undefined) {
36
+ this.options.callback(char);
37
+ }
38
+ remaining -= 1;
39
+ }
40
+ if (!this.queue.length) {
41
+ if (this.timer) {
42
+ clearInterval(this.timer);
43
+ this.timer = null;
44
+ }
45
+ this.pendingResolvers.forEach((resolver) => resolver());
46
+ this.pendingResolvers = [];
47
+ }
48
+ }, 1);
49
+ }
50
+ }
51
+ exports.StableStreaming = StableStreaming;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celhive/tool.js",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"