@alauda/doom 2.4.0 → 2.5.0
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/cli/helpers.d.ts +12 -0
- package/lib/cli/helpers.js +58 -1
- package/lib/cli/load-config.js +1 -1
- package/lib/cli/translate.js +24 -38
- package/lib/global/Intelligence/AIAssistant/Chat/ChatRefDocs/index.d.ts +2 -2
- package/lib/global/Intelligence/AIAssistant/Chat/ThinkingProcess/index.js +2 -1
- package/lib/global/Intelligence/AIAssistant/Chat/index.js +1 -1
- package/lib/global/Intelligence/AIAssistant/index.js +21 -55
- package/lib/global/Intelligence/AIAssistant/types.d.ts +3 -8
- package/lib/global/Intelligence/index.js +2 -7
- package/lib/plugins/replace/parse-toc.js +1 -1
- package/lib/remark-lint/check-dead-links.js +6 -1
- package/lib/runtime/components/Markdown.js +0 -1
- package/lib/runtime/components/Masonry.js +0 -1
- package/lib/runtime/components/OpenAPIPath.d.ts +1 -1
- package/lib/runtime/components/OpenAPIPath.js +1 -1
- package/lib/runtime/components/_HeadingTitle.d.ts +1 -1
- package/lib/runtime/components/_HeadingTitle.js +1 -1
- package/lib/theme/AutoExpandable.js +0 -1
- package/package.json +24 -23
- package/styles/chat-ref-docs.module.scss +4 -0
- package/styles/chat.module.scss +85 -6
- package/lib/global/Intelligence/AIAssistant/utils.d.ts +0 -19
- package/lib/global/Intelligence/AIAssistant/utils.js +0 -131
package/lib/cli/helpers.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { Code, Root, RootContent } from 'mdast';
|
|
1
2
|
export declare const parseBoolean: (value?: string) => boolean;
|
|
2
3
|
export declare const parseBooleanOrString: (value?: string) => string | boolean;
|
|
3
4
|
export declare const isDoc: (filename: string) => boolean;
|
|
4
5
|
export declare const getMatchedDocFilePaths: (matched: string[]) => Promise<(string | string[])[]>;
|
|
6
|
+
export declare const stringifyMatter: (frontmatter: object, content: string) => string;
|
|
5
7
|
/**
|
|
6
8
|
* Support custom id like `#hello world {#custom-id}`
|
|
7
9
|
* Avoid https://mdxjs.com/docs/troubleshooting-mdx/#could-not-parse-expression-with-acorn-error
|
|
@@ -21,3 +23,13 @@ export declare const parseTerms: () => Promise<{
|
|
|
21
23
|
} | undefined;
|
|
22
24
|
name?: "company" | "product" | "productShort" | "alaudaCloudLink" | undefined;
|
|
23
25
|
}[]>;
|
|
26
|
+
export declare const translateCodeFile: (content: RootContent, { sourceBase, targetBase }: {
|
|
27
|
+
sourceBase: string;
|
|
28
|
+
targetBase: string;
|
|
29
|
+
}) => RootContent;
|
|
30
|
+
export interface CodeBlockPlaceholder {
|
|
31
|
+
node: Code;
|
|
32
|
+
placeholder: string;
|
|
33
|
+
}
|
|
34
|
+
export declare const replaceCodeBlocksWithPlaceholders: (content: Root | RootContent) => CodeBlockPlaceholder[];
|
|
35
|
+
export declare const restoreCodeBlockPlaceholders: (content: Root | RootContent, placeholders: readonly CodeBlockPlaceholder[]) => RootContent | Root;
|
package/lib/cli/helpers.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
import { glob } from 'tinyglobby';
|
|
4
|
+
import { visit } from 'unist-util-visit';
|
|
3
5
|
import { xfetch } from 'x-fetch';
|
|
4
|
-
import { parse } from 'yaml';
|
|
6
|
+
import { parse, stringify } from 'yaml';
|
|
5
7
|
import { FALSY_VALUES, TRUTHY_VALUES } from "../shared/index.js";
|
|
6
8
|
export const parseBoolean = (value) => value === undefined || !FALSY_VALUES.has(value);
|
|
7
9
|
export const parseBooleanOrString = (value) => value === undefined ||
|
|
@@ -21,6 +23,10 @@ export const getMatchedDocFilePaths = (matched) => Promise.all(matched.map(async
|
|
|
21
23
|
}
|
|
22
24
|
return [];
|
|
23
25
|
}));
|
|
26
|
+
export const stringifyMatter = (frontmatter, content) => '---\n' +
|
|
27
|
+
stringify(frontmatter) +
|
|
28
|
+
'---\n' +
|
|
29
|
+
(content.startsWith('\n') ? content : '\n' + content);
|
|
24
30
|
/**
|
|
25
31
|
* Support custom id like `#hello world {#custom-id}`
|
|
26
32
|
* Avoid https://mdxjs.com/docs/troubleshooting-mdx/#could-not-parse-expression-with-acorn-error
|
|
@@ -43,3 +49,54 @@ const parseTerms_ = async () => {
|
|
|
43
49
|
};
|
|
44
50
|
let parsedTermsCache;
|
|
45
51
|
export const parseTerms = () => (parsedTermsCache ??= parseTerms_());
|
|
52
|
+
const RELATIVE_FILE_META_REGEX = /(^|\s)(file)(\s*=\s*)(['"`]?)(\.\.?\/[^\s'"`]+)\4/g;
|
|
53
|
+
export const translateCodeFile = (content, { sourceBase, targetBase }) => {
|
|
54
|
+
visit(content, 'code', (code) => {
|
|
55
|
+
const nextMeta = code.meta?.replace(RELATIVE_FILE_META_REGEX, (_match, prefix, key, equals, quote, value) => `${prefix}${key}${equals}${quote}${path.relative(targetBase, path.resolve(sourceBase, value))}${quote}`);
|
|
56
|
+
if (nextMeta !== code.meta) {
|
|
57
|
+
code.meta = nextMeta;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return content;
|
|
61
|
+
};
|
|
62
|
+
const CODE_BLOCK_PLACEHOLDER_PREFIX = '__DOOM_TRANSLATE_CODE_BLOCK_';
|
|
63
|
+
const CODE_BLOCK_PLACEHOLDER_PATTERN = new RegExp(`^${CODE_BLOCK_PLACEHOLDER_PREFIX}(\\d+)__$`);
|
|
64
|
+
const getCodeBlockPlaceholderIndex = (value) => {
|
|
65
|
+
const match = CODE_BLOCK_PLACEHOLDER_PATTERN.exec(value);
|
|
66
|
+
if (!match) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
return +match[1];
|
|
70
|
+
};
|
|
71
|
+
export const replaceCodeBlocksWithPlaceholders = (content) => {
|
|
72
|
+
const placeholders = [];
|
|
73
|
+
visit(content, 'code', (code) => {
|
|
74
|
+
if (code.value.length <= 50) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const placeholder = `${CODE_BLOCK_PLACEHOLDER_PREFIX}${placeholders.length}__`;
|
|
78
|
+
placeholders.push({
|
|
79
|
+
node: { ...code },
|
|
80
|
+
placeholder,
|
|
81
|
+
});
|
|
82
|
+
code.value = placeholder;
|
|
83
|
+
delete code.lang;
|
|
84
|
+
delete code.meta;
|
|
85
|
+
});
|
|
86
|
+
return placeholders;
|
|
87
|
+
};
|
|
88
|
+
export const restoreCodeBlockPlaceholders = (content, placeholders) => {
|
|
89
|
+
visit(content, 'code', (code) => {
|
|
90
|
+
const value = code.value.trim();
|
|
91
|
+
const index = getCodeBlockPlaceholderIndex(value);
|
|
92
|
+
if (index == null) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const placeholder = placeholders.at(index);
|
|
96
|
+
if (!placeholder || placeholder.placeholder !== value) {
|
|
97
|
+
throw new Error(`Unmatched code block placeholder: ${value}`);
|
|
98
|
+
}
|
|
99
|
+
Object.assign(code, placeholder.node);
|
|
100
|
+
});
|
|
101
|
+
return content;
|
|
102
|
+
};
|
package/lib/cli/load-config.js
CHANGED
package/lib/cli/translate.js
CHANGED
|
@@ -5,9 +5,9 @@ import { setTimeout } from 'node:timers/promises';
|
|
|
5
5
|
import { isDeepStrictEqual } from 'node:util';
|
|
6
6
|
import { logger } from '@rspress/core';
|
|
7
7
|
import { removeLeadingSlash } from '@rspress/shared';
|
|
8
|
+
import matter from '@rspress/shared/gray-matter';
|
|
8
9
|
import { Command } from 'commander';
|
|
9
10
|
import ejs from 'ejs';
|
|
10
|
-
import matter from 'gray-matter';
|
|
11
11
|
import { OpenAI, RateLimitError } from 'openai';
|
|
12
12
|
import { pRateLimit } from 'p-ratelimit';
|
|
13
13
|
import { glob } from 'tinyglobby';
|
|
@@ -15,13 +15,15 @@ import { cyan, red } from 'yoctocolors';
|
|
|
15
15
|
import { mdProcessor, mdxProcessor, normalizeImgSrc, } from '../plugins/index.js';
|
|
16
16
|
import { Language, SUPPORTED_LANGUAGES, TITLE_TRANSLATION_MAP, } from '../shared/index.js';
|
|
17
17
|
import { pathExists } from '../utils/index.js';
|
|
18
|
-
import { escapeMarkdownHeadingIds, getMatchedDocFilePaths, parseBoolean, parseTerms, } from './helpers.js';
|
|
18
|
+
import { escapeMarkdownHeadingIds, getMatchedDocFilePaths, parseBoolean, parseTerms, replaceCodeBlocksWithPlaceholders, restoreCodeBlockPlaceholders, stringifyMatter, translateCodeFile, } from './helpers.js';
|
|
19
19
|
import { loadConfig } from './load-config.js';
|
|
20
20
|
export const TERMS_SUPPORTED_LANGUAGES = ['en', 'zh', 'ru'];
|
|
21
21
|
// Directories that should be copied instead of translated
|
|
22
22
|
const COPY_ONLY_DIRECTORIES = [
|
|
23
23
|
'apis/advanced_apis/**',
|
|
24
|
+
'apis/crds/**',
|
|
24
25
|
'apis/kubernetes_apis/**',
|
|
26
|
+
'apis/references/**',
|
|
25
27
|
];
|
|
26
28
|
const DEFAULT_SYSTEM_PROMPT = `
|
|
27
29
|
You are a professional technical documentation engineer, skilled in writing high-quality technical documentation in <%= targetLang %>. Please accurately translate the following text from <%= sourceLang %> to <%= targetLang %>, maintaining the style consistent with technical documentation in <%= sourceLang %>.
|
|
@@ -30,16 +32,16 @@ You are a professional technical documentation engineer, skilled in writing high
|
|
|
30
32
|
- Sentences should be fluent and conform to the expression habits of the <%= targetLang %> language.
|
|
31
33
|
- Input format is MDX; output format must also retain the original MDX format. Do not translate the names of jsx components such as <Overview />, and do not wrap output in unnecessary code blocks.
|
|
32
34
|
- **CRITICAL**: Do not translate or modify ANY link content in the document. This includes:
|
|
33
|
-
- URLs in markdown links: [text](URL) - keep URL exactly as is
|
|
35
|
+
- URLs in markdown links: [text](URL) - keep URL exactly as is but translate the text
|
|
34
36
|
- Reference-style links: [text][ref] and [ref]: URL - keep both ref and URL unchanged
|
|
35
37
|
- Inline URLs: https://example.com - keep completely unchanged
|
|
36
|
-
- Image links:  - keep src unchanged, but alt text
|
|
38
|
+
- Image links:  - keep src unchanged, but alt text should be translated
|
|
37
39
|
- Anchor links: [text](#anchor) - keep #anchor unchanged
|
|
40
|
+
- Custom anchors in headings: # Heading \\{#custom-anchor} - keep \\{#custom-anchor} unchanged and translate "Heading"
|
|
38
41
|
- Any href attributes in HTML tags - keep unchanged
|
|
39
42
|
- Do not translate professional technical terms and proper nouns, including but not limited to: Kubernetes, Docker, CLI, API, REST, GraphQL, JSON, YAML, Git, GitHub, GitLab, AWS, Azure, GCP, Linux, Windows, macOS, Node.js, React, Vue, Angular, TypeScript, JavaScript, Python, Java, Go, Rust, etc. Keep these terms in their original form.
|
|
40
43
|
- The title field and description field in frontmatter should be translated, other frontmatter fields should retain and do not translate.
|
|
41
44
|
- Content within MDX components needs to be translated, whereas MDX component names and parameter keys do not.
|
|
42
|
-
- Do not modify or translate any placeholders in the format of __ANCHOR_N__ (where N is a number). These placeholders must be kept exactly as they appear in the source text.
|
|
43
45
|
- Keep original escape characters like backslash, angle brackets, etc. unchanged during translation.
|
|
44
46
|
- Do not add any escape characters to special characters like [], (), {}, etc. unless they were explicitly present in the source text. For example:
|
|
45
47
|
- If source has "Architecture [Optional]", keep it as "Architecture [Optional]" (not "Architecture \\[Optional]")
|
|
@@ -106,29 +108,6 @@ const resolveTerms = async (sourceLang, targetLang, sourceContent) => {
|
|
|
106
108
|
logger.debug('Resolved terms:', terms);
|
|
107
109
|
return terms;
|
|
108
110
|
};
|
|
109
|
-
const ANCHOR_REGEX = /\\\\?\{#[\w-]+\}/g;
|
|
110
|
-
function replaceAnchorsWithPlaceholders(content) {
|
|
111
|
-
// Handle escaped underscores in anchor IDs (e.g., \{#independent\_doc\_site} -> \{#independent_doc_site})
|
|
112
|
-
// This is necessary because MDX processor automatically escapes underscores in heading IDs
|
|
113
|
-
// to ensure proper Markdown parsing, but we need the original unescaped form for anchor matching
|
|
114
|
-
const unescapedContent = content.replace(/\\_/g, '_');
|
|
115
|
-
const anchors = [];
|
|
116
|
-
const contentWithPlaceholders = unescapedContent.replace(ANCHOR_REGEX, (match) => {
|
|
117
|
-
anchors.push(match);
|
|
118
|
-
return `__ANCHOR_${anchors.length - 1}__`;
|
|
119
|
-
});
|
|
120
|
-
return { content: contentWithPlaceholders, anchors };
|
|
121
|
-
}
|
|
122
|
-
function restoreAnchors(content, anchors) {
|
|
123
|
-
return content.replace(/__ANCHOR_(\d+)__/g, (_, index) => {
|
|
124
|
-
const numIndex = parseInt(index, 10);
|
|
125
|
-
if (isNaN(numIndex) || numIndex < 0 || numIndex >= anchors.length) {
|
|
126
|
-
throw new Error(`Invalid anchor index: ${index}`);
|
|
127
|
-
}
|
|
128
|
-
const anchor = anchors[numIndex];
|
|
129
|
-
return anchor;
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
111
|
function extractFirstLevelHeading(content) {
|
|
133
112
|
const lines = content.split('\n');
|
|
134
113
|
for (const line of lines) {
|
|
@@ -168,7 +147,6 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
|
|
|
168
147
|
titleTranslationPrompt = `- The heading "${firstLevelHeading}" should be translated as "${titleTranslation}".`;
|
|
169
148
|
}
|
|
170
149
|
}
|
|
171
|
-
const { content: contentWithPlaceholders, anchors } = replaceAnchorsWithPlaceholders(sourceContent);
|
|
172
150
|
const finalSystemPrompt = await ejs.render(systemPrompt?.trim() || DEFAULT_SYSTEM_PROMPT, {
|
|
173
151
|
sourceLang,
|
|
174
152
|
targetLang,
|
|
@@ -186,7 +164,7 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
|
|
|
186
164
|
},
|
|
187
165
|
{
|
|
188
166
|
role: 'user',
|
|
189
|
-
content:
|
|
167
|
+
content: sourceContent,
|
|
190
168
|
},
|
|
191
169
|
],
|
|
192
170
|
model: openaiModel,
|
|
@@ -197,7 +175,7 @@ export const translate = async ({ source, sourceContent, target, systemPrompt, u
|
|
|
197
175
|
for await (const chunk of stream) {
|
|
198
176
|
content += chunk.choices[0]?.delta.content ?? '';
|
|
199
177
|
}
|
|
200
|
-
return
|
|
178
|
+
return content;
|
|
201
179
|
};
|
|
202
180
|
const limit = pRateLimit({
|
|
203
181
|
interval: 60_000, // 1min
|
|
@@ -314,7 +292,7 @@ export const translateCommand = new Command('translate')
|
|
|
314
292
|
const newFrontmatter = { ...sourceFrontmatter, sourceSHA };
|
|
315
293
|
delete newFrontmatter.i18n;
|
|
316
294
|
const { content } = matter(sourceContent);
|
|
317
|
-
targetContent =
|
|
295
|
+
targetContent = stringifyMatter(newFrontmatter, content);
|
|
318
296
|
const targetBase = path.dirname(targetFilePath);
|
|
319
297
|
await fs.mkdir(targetBase, { recursive: true });
|
|
320
298
|
await fs.writeFile(targetFilePath, targetContent);
|
|
@@ -325,17 +303,20 @@ export const translateCommand = new Command('translate')
|
|
|
325
303
|
const isMdx = sourceFilePath.endsWith('.mdx');
|
|
326
304
|
const processor = isMdx ? mdxProcessor : mdProcessor;
|
|
327
305
|
const ast = processor.parse(escapeMarkdownHeadingIds(sourceContent));
|
|
306
|
+
const sourceBase = path.dirname(sourceFilePath);
|
|
328
307
|
const targetBase = path.dirname(targetFilePath);
|
|
308
|
+
const normalizeOptions = { sourceBase, targetBase };
|
|
329
309
|
const normalizeImgSrcOptions = {
|
|
310
|
+
...normalizeOptions,
|
|
330
311
|
localPublicBase: path.resolve(docsDir, 'public'),
|
|
331
|
-
sourceBase: path.dirname(sourceFilePath),
|
|
332
|
-
targetBase,
|
|
333
312
|
translating: { source, target, copy },
|
|
334
313
|
};
|
|
335
|
-
const
|
|
314
|
+
const normalizedAst = {
|
|
336
315
|
...ast,
|
|
337
|
-
children: ast.children.map((it) => normalizeImgSrc(it, normalizeImgSrcOptions)),
|
|
338
|
-
}
|
|
316
|
+
children: ast.children.map((it) => translateCodeFile(normalizeImgSrc(it, normalizeImgSrcOptions), normalizeOptions)),
|
|
317
|
+
};
|
|
318
|
+
const codeBlockPlaceholders = replaceCodeBlocksWithPlaceholders(normalizedAst);
|
|
319
|
+
const normalizedSourceContent = processor.stringify(normalizedAst);
|
|
339
320
|
targetContent = await translate({
|
|
340
321
|
...config.translate,
|
|
341
322
|
source,
|
|
@@ -343,6 +324,11 @@ export const translateCommand = new Command('translate')
|
|
|
343
324
|
target,
|
|
344
325
|
additionalPrompts: sourceFrontmatter.i18n?.additionalPrompts,
|
|
345
326
|
});
|
|
327
|
+
if (codeBlockPlaceholders.length > 0) {
|
|
328
|
+
const targetAst = processor.parse(targetContent);
|
|
329
|
+
restoreCodeBlockPlaceholders(targetAst, codeBlockPlaceholders);
|
|
330
|
+
targetContent = processor.stringify(targetAst);
|
|
331
|
+
}
|
|
346
332
|
const newFrontmatter = { ...sourceFrontmatter, sourceSHA };
|
|
347
333
|
delete newFrontmatter.i18n;
|
|
348
334
|
const { data, content } = matter(targetContent);
|
|
@@ -363,7 +349,7 @@ export const translateCommand = new Command('translate')
|
|
|
363
349
|
if (typeof newFrontmatter.title !== 'string') {
|
|
364
350
|
delete newFrontmatter.title;
|
|
365
351
|
}
|
|
366
|
-
targetContent =
|
|
352
|
+
targetContent = stringifyMatter(newFrontmatter, content);
|
|
367
353
|
await fs.mkdir(targetBase, { recursive: true });
|
|
368
354
|
await fs.writeFile(targetFilePath, targetContent);
|
|
369
355
|
logger.info(`${cyan(sourceRelativePath)} translated to ${cyan(targetRelativePath)}`);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { SmartDocDisplayReferenceDoc } from '@alauda/doc-stream-sdk';
|
|
2
2
|
export interface ChatRefDocsProps {
|
|
3
|
-
refDocs:
|
|
3
|
+
refDocs: SmartDocDisplayReferenceDoc[];
|
|
4
4
|
}
|
|
5
5
|
export declare const ChatRefDocs: ({ refDocs }: ChatRefDocsProps) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { Markdown, useTranslation } from '@alauda/doom/runtime';
|
|
3
|
+
import classes from '@alauda/doom/styles/chat.module.scss';
|
|
3
4
|
export const ThinkingProcess = ({ children }) => {
|
|
4
5
|
const t = useTranslation();
|
|
5
|
-
return (_jsxs("div", { children: [_jsx("div", { children: t('thinking_process') }), _jsx(Markdown, { children: children })] }));
|
|
6
|
+
return (_jsxs("div", { children: [_jsx("div", { children: t('thinking_process') }), _jsx("div", { className: classes.markdown, children: _jsx(Markdown, { children: children }) })] }));
|
|
6
7
|
};
|
|
@@ -5,4 +5,4 @@ import { ThinkingProcess } from './ThinkingProcess/index.js';
|
|
|
5
5
|
import AssistantIcon from '@alauda/doom/assets/assistant.svg?react';
|
|
6
6
|
import { Markdown } from '@alauda/doom/runtime';
|
|
7
7
|
import classes from '@alauda/doom/styles/chat.module.scss';
|
|
8
|
-
export const Chat = ({ ref, messages }) => (_jsx("ul", { ref: ref, className: classes.container, children: messages.map(({ id, role, content,
|
|
8
|
+
export const Chat = ({ ref, messages }) => (_jsx("ul", { ref: ref, className: classes.container, children: messages.map(({ id, role, content, thoughtProcess, refDocs }) => (_jsxs("li", { className: clsx(classes.chat, classes[role]), children: [role === 'assistant' && _jsx(AssistantIcon, { className: classes.icon }), _jsxs("div", { className: classes.content, children: [thoughtProcess && (_jsx(ThinkingProcess, { children: thoughtProcess })), refDocs?.length ? _jsx(ChatRefDocs, { refDocs: refDocs }) : null, typeof content === 'string' ? (_jsx("div", { className: clsx(classes.markdown), children: _jsx(Markdown, { children: content }) })) : (content)] })] }, `${role}-${id}`))) }));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { consumeSmartDocDisplayStream } from '@alauda/doc-stream-sdk';
|
|
2
3
|
import { clsx } from 'clsx';
|
|
3
4
|
import { useEffect, useRef, useState } from 'react';
|
|
4
5
|
import { Tooltip } from 'react-tooltip';
|
|
@@ -9,7 +10,6 @@ import { Chat } from "./Chat/index.js";
|
|
|
9
10
|
import { Preamble } from "./Preamble/index.js";
|
|
10
11
|
import { ResizableUserInput } from "./ResizableUserInput/index.js";
|
|
11
12
|
import { Thinking } from "./Thinking.js";
|
|
12
|
-
import { consumeSSEEvents, getAnswerDelta, parseStreamContent, } from "./utils.js";
|
|
13
13
|
import CloseIcon from '@alauda/doom/assets/close.svg?react';
|
|
14
14
|
import LogoutIcon from '@alauda/doom/assets/logout.svg?react';
|
|
15
15
|
import NewChatIcon from '@alauda/doom/assets/new-chat.svg?react';
|
|
@@ -99,61 +99,27 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
99
99
|
session_id: sessionId,
|
|
100
100
|
},
|
|
101
101
|
});
|
|
102
|
-
|
|
103
|
-
let text = '';
|
|
104
|
-
let sseBuffer = '';
|
|
105
|
-
let sseEvent;
|
|
106
|
-
for await (const chunk_ of res.body) {
|
|
107
|
-
if (sessionId !== sessionIdRef.current) {
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
sseBuffer += textDecoder.decode(chunk_, { stream: true });
|
|
111
|
-
const result = consumeSSEEvents(sseBuffer, sseEvent);
|
|
112
|
-
const { events, remainder } = result;
|
|
113
|
-
sseBuffer = remainder;
|
|
114
|
-
sseEvent = result.event;
|
|
115
|
-
let updated = false;
|
|
116
|
-
for (const event of events) {
|
|
117
|
-
const delta = getAnswerDelta(event);
|
|
118
|
-
if (!delta) {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
text += delta;
|
|
122
|
-
updated = true;
|
|
123
|
-
}
|
|
124
|
-
if (!updated) {
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
const parsed = parseStreamContent(text);
|
|
128
|
-
flushMessages((messages) => [
|
|
129
|
-
...messages.slice(0, index),
|
|
130
|
-
{ ...messages[index], ...parsed },
|
|
131
|
-
...messages.slice(index + 1),
|
|
132
|
-
]);
|
|
133
|
-
}
|
|
134
|
-
if (sessionId !== sessionIdRef.current) {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
sseBuffer += textDecoder.decode();
|
|
138
|
-
const { events } = consumeSSEEvents(`${sseBuffer}\n`, sseEvent);
|
|
139
|
-
let updated = false;
|
|
140
|
-
for (const event of events) {
|
|
141
|
-
const delta = getAnswerDelta(event);
|
|
142
|
-
if (!delta) {
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
text += delta;
|
|
146
|
-
updated = true;
|
|
147
|
-
}
|
|
148
|
-
if (!updated) {
|
|
102
|
+
if (!res.body) {
|
|
149
103
|
return;
|
|
150
104
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
105
|
+
await consumeSmartDocDisplayStream(res.body, {
|
|
106
|
+
ignoreDocsBlocks: false,
|
|
107
|
+
onDisplayMessage(nextMessage) {
|
|
108
|
+
if (sessionId !== sessionIdRef.current) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
flushMessages((messages) => [
|
|
112
|
+
...messages.slice(0, index),
|
|
113
|
+
{
|
|
114
|
+
...messages[index],
|
|
115
|
+
content: nextMessage.content,
|
|
116
|
+
thoughtProcess: nextMessage.thoughtProcess || undefined,
|
|
117
|
+
refDocs: nextMessage.refDocs,
|
|
118
|
+
},
|
|
119
|
+
...messages.slice(index + 1),
|
|
120
|
+
]);
|
|
121
|
+
},
|
|
122
|
+
});
|
|
157
123
|
};
|
|
158
124
|
const [loading, setLoading] = useState(false);
|
|
159
125
|
const onSend = useMemoizedFn(async (content) => {
|
|
@@ -178,5 +144,5 @@ export const AIAssistant = ({ open, onOpenChange }) => {
|
|
|
178
144
|
setLoading(false);
|
|
179
145
|
}
|
|
180
146
|
});
|
|
181
|
-
return (_jsxs("div", { className: clsx(classes.container, '
|
|
147
|
+
return (_jsxs("div", { className: clsx(classes.container, 'rp-doc', open && classes.open), children: [_jsxs("div", { className: classes.header, children: [_jsxs("div", { className: classes.title, children: [t('ai_assistant'), loggedIn && (_jsxs(_Fragment, { children: [_jsxs("span", { className: classes.username, children: ["(", authInfo.detail.user.name, ")"] }), _jsx(LogoutIcon, { className: "ai-assistant-logout", onClick: onLogout }), _jsx(Tooltip, { anchorSelect: ".ai-assistant-logout", children: t('logout') })] }))] }), _jsxs("div", { className: classes.icons, children: [messages.length ? (_jsxs(_Fragment, { children: [_jsx(NewChatIcon, { className: "ai-assistant-new-chat", onClick: onNewChat }), _jsx(Tooltip, { anchorSelect: ".ai-assistant-new-chat", children: t('new_chat') })] })) : null, _jsx(CloseIcon, { className: "ai-assistant-close", onClick: onClose }), _jsx(Tooltip, { anchorSelect: ".ai-assistant-close", children: t('close') })] })] }), messages.length ? (_jsx(Chat, { ref: chatRef, messages: messages })) : (_jsx(Preamble, { loggedIn: loggedIn })), loggedIn && _jsx(ResizableUserInput, { loading: loading, onSend: onSend })] }));
|
|
182
148
|
};
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
+
import type { SmartDocDisplayReferenceDoc } from '@alauda/doc-stream-sdk';
|
|
1
2
|
import type { ReactNode } from 'react';
|
|
2
|
-
export interface RefDoc {
|
|
3
|
-
content: string;
|
|
4
|
-
cos_sim: number;
|
|
5
|
-
id: number;
|
|
6
|
-
path: string;
|
|
7
|
-
}
|
|
8
3
|
export interface ChatMessage {
|
|
9
4
|
id: number;
|
|
10
5
|
role: 'user' | 'assistant';
|
|
11
6
|
content: ReactNode;
|
|
12
|
-
refDocs?:
|
|
13
|
-
|
|
7
|
+
refDocs?: SmartDocDisplayReferenceDoc[];
|
|
8
|
+
thoughtProcess?: string | null;
|
|
14
9
|
}
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import { isProduction, NoSSR
|
|
3
|
-
import virtual from 'doom-@global-virtual';
|
|
2
|
+
import { isProduction, NoSSR } from '@rspress/core/runtime';
|
|
4
3
|
import { useState } from 'react';
|
|
5
4
|
import { Tooltip } from 'react-tooltip';
|
|
6
|
-
import { ACP_BASE } from "../../shared/index.js";
|
|
7
5
|
import { AIAssistant } from "./AIAssistant/index.js";
|
|
8
6
|
import assistantIcon from '@alauda/doom/assets/assistant.svg';
|
|
9
7
|
import { useMemoizedFn, useTranslation } from '@alauda/doom/runtime';
|
|
@@ -24,10 +22,7 @@ const Intelligence_ = () => {
|
|
|
24
22
|
return (_jsxs(_Fragment, { children: [_jsx(AIAssistant, { open: open, onOpenChange: toggleOpen }), open || (_jsx("button", { type: "button", className: "intelligence-entry", onClick: toggleOpen, children: _jsx("img", { alt: t('ai_assistant'), src: assistantIcon }) })), _jsx(Tooltip, { anchorSelect: ".intelligence-entry", place: "left", children: t('ai_assistant') })] }));
|
|
25
23
|
};
|
|
26
24
|
const Intelligence = () => {
|
|
27
|
-
|
|
28
|
-
if (lang !== 'en' ||
|
|
29
|
-
!ALLOWED_DOMAINS.has(location.hostname) ||
|
|
30
|
-
virtual.userBase !== ACP_BASE) {
|
|
25
|
+
if (!ALLOWED_DOMAINS.has(location.hostname)) {
|
|
31
26
|
return;
|
|
32
27
|
}
|
|
33
28
|
return _jsx(Intelligence_, {});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import Slugger from '@rspress/shared/github-slugger';
|
|
1
2
|
import { extractTextAndId } from '@rspress/shared/node-utils';
|
|
2
|
-
import Slugger from 'github-slugger';
|
|
3
3
|
import { visitChildren } from 'unist-util-visit-children';
|
|
4
4
|
export const parseToc = (tree, allDepths) => {
|
|
5
5
|
let title = '';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
import { PluginDriver, RouteService, remarkLink } from '@rspress/core';
|
|
2
|
+
import { PluginDriver, RouteService, remarkImage, remarkLink, } from '@rspress/core';
|
|
3
3
|
import { isProduction } from '@rspress/shared';
|
|
4
4
|
import { lintRule } from 'unified-lint-rule';
|
|
5
5
|
import { getConfig } from "./utils.js";
|
|
@@ -26,4 +26,9 @@ export const checkDeadLinks = lintRule('doom-lint:check-dead-links', async (tree
|
|
|
26
26
|
remarkLinkOptions: config.markdown?.link,
|
|
27
27
|
lint: true,
|
|
28
28
|
})(tree, vfile);
|
|
29
|
+
remarkImage({
|
|
30
|
+
docDirectory: config.root,
|
|
31
|
+
remarkImageOptions: config.markdown?.image,
|
|
32
|
+
lint: true,
|
|
33
|
+
})(tree, vfile);
|
|
29
34
|
});
|
|
@@ -2,7 +2,6 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
2
2
|
import { NoSSR } from '@rspress/core/runtime';
|
|
3
3
|
import { lazy, Suspense, useLayoutEffect, useRef, } from 'react';
|
|
4
4
|
const LazyMasonry = lazy(() => import('masonry-layout').then(({ default: MasonryLayout }) => {
|
|
5
|
-
// eslint-disable-next-line @eslint-react/component-hook-factories
|
|
6
5
|
const Masonry = ({ options, ...props }) => {
|
|
7
6
|
const ref = useRef(null);
|
|
8
7
|
useLayoutEffect(() => {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import { usePage } from '@rspress/core/runtime';
|
|
3
3
|
import { Badge } from '@rspress/core/theme-original';
|
|
4
|
+
import BananaSlug from '@rspress/shared/github-slugger';
|
|
4
5
|
import openapisMap from 'doom-@api-openapisMap';
|
|
5
6
|
import virtual from 'doom-@api-virtual';
|
|
6
|
-
import BananaSlug from 'github-slugger';
|
|
7
7
|
import { OpenAPIV3 } from 'openapi-types';
|
|
8
8
|
import { Fragment, useMemo } from 'react';
|
|
9
9
|
import { omitRoutePathRefs, resolveRef } from '../utils.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import BananaSlug from 'github-slugger';
|
|
2
|
+
import BananaSlug from '@rspress/shared/github-slugger';
|
|
3
3
|
import { Children, useMemo } from 'react';
|
|
4
4
|
import { X } from './_X.js';
|
|
5
5
|
// TODO: use context to simplify the usage of `slugger`
|
|
@@ -18,7 +18,6 @@ export const AutoExpandable = ({ threshold = 240, children, }) => {
|
|
|
18
18
|
let observer;
|
|
19
19
|
const calculate = () => {
|
|
20
20
|
if (containerEl.scrollHeight > threshold) {
|
|
21
|
-
// eslint-disable-next-line @eslint-react/set-state-in-effect
|
|
22
21
|
setExpandable(true);
|
|
23
22
|
}
|
|
24
23
|
observer?.disconnect();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alauda/doom",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Doctor Doom making docs.",
|
|
6
6
|
"repository": {
|
|
@@ -43,20 +43,21 @@
|
|
|
43
43
|
"rspress-plugins"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
+
"@alauda/doc-stream-sdk": "0.1.0",
|
|
46
47
|
"@alauda/doom-export": "^0.4.1",
|
|
47
48
|
"@cspell/eslint-plugin": "^10.0.0",
|
|
48
|
-
"@eslint-react/eslint-plugin": "^
|
|
49
|
+
"@eslint-react/eslint-plugin": "^5.7.6",
|
|
49
50
|
"@eslint/js": "^10.0.1",
|
|
50
|
-
"@inquirer/prompts": "^8.4.
|
|
51
|
+
"@inquirer/prompts": "^8.4.3",
|
|
51
52
|
"@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0",
|
|
52
|
-
"@rsbuild/plugin-react": "^
|
|
53
|
-
"@rsbuild/plugin-sass": "^1.5.
|
|
54
|
-
"@rsbuild/plugin-svgr": "^
|
|
53
|
+
"@rsbuild/plugin-react": "^2.0.0",
|
|
54
|
+
"@rsbuild/plugin-sass": "^1.5.2",
|
|
55
|
+
"@rsbuild/plugin-svgr": "^2.0.2",
|
|
55
56
|
"@rsbuild/plugin-yaml": "^1.0.4",
|
|
56
|
-
"@rspress/core": "2.0.
|
|
57
|
-
"@rspress/plugin-algolia": "2.0.
|
|
58
|
-
"@rspress/plugin-sitemap": "2.0.
|
|
59
|
-
"@rspress/shared": "2.0.
|
|
57
|
+
"@rspress/core": "2.0.11",
|
|
58
|
+
"@rspress/plugin-algolia": "2.0.11",
|
|
59
|
+
"@rspress/plugin-sitemap": "2.0.11",
|
|
60
|
+
"@rspress/shared": "2.0.11",
|
|
60
61
|
"@shikijs/transformers": "^4.0.2",
|
|
61
62
|
"@total-typescript/ts-reset": "^0.6.1",
|
|
62
63
|
"@types/react": "^19.2.14",
|
|
@@ -64,11 +65,11 @@
|
|
|
64
65
|
"chokidar": "^5.0.0",
|
|
65
66
|
"clsx": "^2.1.1",
|
|
66
67
|
"commander": "^14.0.3",
|
|
67
|
-
"ejs": "^5.0.
|
|
68
|
-
"es-toolkit": "^1.
|
|
69
|
-
"eslint": "^10.
|
|
68
|
+
"ejs": "^5.0.2",
|
|
69
|
+
"es-toolkit": "^1.46.1",
|
|
70
|
+
"eslint": "^10.3.0",
|
|
70
71
|
"eslint-plugin-mdx": "^3.7.0",
|
|
71
|
-
"globals": "^17.
|
|
72
|
+
"globals": "^17.6.0",
|
|
72
73
|
"hastscript": "^9.0.1",
|
|
73
74
|
"html-tag-names": "^2.1.0",
|
|
74
75
|
"jsencrypt": "^3.5.4",
|
|
@@ -78,14 +79,14 @@
|
|
|
78
79
|
"mdast-util-phrasing": "^4.1.0",
|
|
79
80
|
"mdast-util-to-markdown": "^2.1.2",
|
|
80
81
|
"mdast-util-to-string": "^4.0.0",
|
|
81
|
-
"mermaid": "^11.
|
|
82
|
-
"openai": "^6.
|
|
82
|
+
"mermaid": "^11.15.0",
|
|
83
|
+
"openai": "^6.37.0",
|
|
83
84
|
"openapi-types": "^12.1.3",
|
|
84
85
|
"p-ratelimit": "^1.0.1",
|
|
85
86
|
"picomatch": "^4.0.4",
|
|
86
87
|
"pluralize": "^8.0.0",
|
|
87
88
|
"react-markdown": "^10.1.0",
|
|
88
|
-
"react-tooltip": "^
|
|
89
|
+
"react-tooltip": "^6.0.2",
|
|
89
90
|
"rehype-raw": "^7.0.0",
|
|
90
91
|
"rehype-sanitize": "^6.0.0",
|
|
91
92
|
"remark-directive": "^4.0.0",
|
|
@@ -100,19 +101,19 @@
|
|
|
100
101
|
"remark-mdx": "^3.1.1",
|
|
101
102
|
"remark-message-control": "^8.0.0",
|
|
102
103
|
"remark-stringify": "^11.0.0",
|
|
103
|
-
"simple-git": "^3.
|
|
104
|
-
"string-width": "^8.2.
|
|
104
|
+
"simple-git": "^3.36.0",
|
|
105
|
+
"string-width": "^8.2.1",
|
|
105
106
|
"swagger2openapi": "^7.0.8",
|
|
106
107
|
"tinyglobby": "^0.2.16",
|
|
107
|
-
"type-fest": "^5.
|
|
108
|
-
"typescript": "^6.0.
|
|
109
|
-
"typescript-eslint": "^8.
|
|
108
|
+
"type-fest": "^5.6.0",
|
|
109
|
+
"typescript": "^6.0.3",
|
|
110
|
+
"typescript-eslint": "^8.59.3",
|
|
110
111
|
"unified": "^11.0.5",
|
|
111
112
|
"unified-lint-rule": "^3.0.1",
|
|
112
113
|
"unist-util-position": "^5.0.0",
|
|
113
114
|
"unist-util-visit-parents": "^6.0.2",
|
|
114
115
|
"x-fetch": "^0.2.6",
|
|
115
|
-
"yaml": "^2.
|
|
116
|
+
"yaml": "^2.9.0",
|
|
116
117
|
"yoctocolors": "^2.1.2"
|
|
117
118
|
}
|
|
118
119
|
}
|
package/styles/chat.module.scss
CHANGED
|
@@ -17,12 +17,6 @@
|
|
|
17
17
|
font-size: 14px;
|
|
18
18
|
width: calc(100% - 36px);
|
|
19
19
|
|
|
20
|
-
h2 {
|
|
21
|
-
margin-bottom: 0;
|
|
22
|
-
padding-top: 0;
|
|
23
|
-
border: 0;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
20
|
&,
|
|
27
21
|
p {
|
|
28
22
|
line-height: calc(20 / 14);
|
|
@@ -52,6 +46,91 @@
|
|
|
52
46
|
}
|
|
53
47
|
}
|
|
54
48
|
|
|
49
|
+
.markdown {
|
|
50
|
+
&:global(.rp-doc) {
|
|
51
|
+
> :first-child {
|
|
52
|
+
margin-top: 0;
|
|
53
|
+
padding-top: 0;
|
|
54
|
+
border-top: 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
> :last-child {
|
|
58
|
+
margin-bottom: 0;
|
|
59
|
+
padding-bottom: 0;
|
|
60
|
+
border-bottom: 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
p {
|
|
64
|
+
margin-top: 0.75rem;
|
|
65
|
+
margin-bottom: 0.75rem;
|
|
66
|
+
line-height: calc(20 / 14);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
h1,
|
|
70
|
+
h2,
|
|
71
|
+
h3,
|
|
72
|
+
h4,
|
|
73
|
+
h5,
|
|
74
|
+
h6 {
|
|
75
|
+
line-height: 1.4;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
h1 {
|
|
79
|
+
margin-bottom: 1rem;
|
|
80
|
+
font-size: 1.5rem;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
h2 {
|
|
84
|
+
margin-top: 1.25rem;
|
|
85
|
+
margin-bottom: 0.5rem;
|
|
86
|
+
padding-top: 0;
|
|
87
|
+
border: 0;
|
|
88
|
+
font-size: 1.25rem;
|
|
89
|
+
|
|
90
|
+
&:first-of-type:before {
|
|
91
|
+
display: none;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
h3 {
|
|
96
|
+
margin-top: 1rem;
|
|
97
|
+
margin-bottom: 0.5rem;
|
|
98
|
+
font-size: 1.125rem;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
h4,
|
|
102
|
+
h5,
|
|
103
|
+
h6 {
|
|
104
|
+
margin-top: 0.75rem;
|
|
105
|
+
margin-bottom: 0.375rem;
|
|
106
|
+
font-size: 1rem;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
ul,
|
|
110
|
+
ol {
|
|
111
|
+
margin-top: 0.75rem;
|
|
112
|
+
margin-bottom: 0.75rem;
|
|
113
|
+
padding-left: 1.125rem;
|
|
114
|
+
line-height: calc(20 / 14);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
ul li:not(:first-child),
|
|
118
|
+
ol li:not(:first-child) {
|
|
119
|
+
margin-top: 0.25rem;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
blockquote {
|
|
123
|
+
margin-top: 1rem;
|
|
124
|
+
margin-bottom: 1rem;
|
|
125
|
+
padding-left: 0.75rem;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
hr {
|
|
129
|
+
margin: 1.5rem 0;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
55
134
|
.assistant {
|
|
56
135
|
display: flex;
|
|
57
136
|
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { RefDoc } from './types.js';
|
|
2
|
-
export interface SSEEvent {
|
|
3
|
-
data: string;
|
|
4
|
-
event?: string;
|
|
5
|
-
}
|
|
6
|
-
export interface SSEConsumeResult {
|
|
7
|
-
event?: string;
|
|
8
|
-
events: SSEEvent[];
|
|
9
|
-
remainder: string;
|
|
10
|
-
}
|
|
11
|
-
export declare const unicodeToString: (unicodeStr: string) => string;
|
|
12
|
-
export declare const removePrefix: (prefix: string | null | undefined, text: string) => string;
|
|
13
|
-
export declare const consumeSSEEvents: (buffer: string, event?: string) => SSEConsumeResult;
|
|
14
|
-
export declare const getAnswerDelta: ({ event, data }: SSEEvent) => string;
|
|
15
|
-
export declare function parseStreamContent(text: string): {
|
|
16
|
-
refDocs: RefDoc[];
|
|
17
|
-
thinkingProcess: string | undefined;
|
|
18
|
-
content: string;
|
|
19
|
-
};
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
// based on https://gitlab-ce.alauda.cn/aml/smart-doc/-/blob/master/frontend/src/app/utils/unicode.ts
|
|
2
|
-
import { isObject } from 'es-toolkit/compat';
|
|
3
|
-
const IGNORED_SSE_EVENT_TYPES = /(?:^|[._-])(?:metadata|trace|updates?)(?:$|[._-])/i;
|
|
4
|
-
const STREAM_TEXT_KEYS = ['delta', 'text', 'content', 'answer', 'message'];
|
|
5
|
-
export const unicodeToString = (unicodeStr) => unicodeStr.replace(/\\u([0-9a-fA-F]{4})/g, (_match, p1) => String.fromCharCode(Number.parseInt(p1, 16)));
|
|
6
|
-
const isRecord = (value) => isObject(value) && !Array.isArray(value);
|
|
7
|
-
export const removePrefix = (prefix, text) => (prefix && text.startsWith(prefix) ? text.slice(prefix.length) : text);
|
|
8
|
-
const parseSSEFieldValue = (line, field) => {
|
|
9
|
-
const value = line.slice(field.length + 1);
|
|
10
|
-
return value.startsWith(' ') ? value.slice(1) : value;
|
|
11
|
-
};
|
|
12
|
-
const getStringValue = (value) => {
|
|
13
|
-
if (typeof value === 'string') {
|
|
14
|
-
return value;
|
|
15
|
-
}
|
|
16
|
-
if (Array.isArray(value)) {
|
|
17
|
-
const text = value.map((item) => getStringValue(item) || '').join('');
|
|
18
|
-
return text || undefined;
|
|
19
|
-
}
|
|
20
|
-
if (!isRecord(value)) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
for (const key of STREAM_TEXT_KEYS) {
|
|
24
|
-
const text = getStringValue(value[key]);
|
|
25
|
-
if (text) {
|
|
26
|
-
return text;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
const getChoicesDelta = (choices) => {
|
|
31
|
-
if (!Array.isArray(choices)) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
const text = choices.map((choice) => getStringValue(choice) || '').join('');
|
|
35
|
-
return text || undefined;
|
|
36
|
-
};
|
|
37
|
-
export const consumeSSEEvents = (buffer, event) => {
|
|
38
|
-
const events = [];
|
|
39
|
-
let remaining = buffer;
|
|
40
|
-
let currentEvent = event;
|
|
41
|
-
let lineBoundary = remaining.match(/\r?\n/);
|
|
42
|
-
while (lineBoundary?.index != null) {
|
|
43
|
-
const line = remaining.slice(0, lineBoundary.index);
|
|
44
|
-
remaining = remaining.slice(lineBoundary.index + lineBoundary[0].length);
|
|
45
|
-
if (!line) {
|
|
46
|
-
currentEvent = undefined;
|
|
47
|
-
}
|
|
48
|
-
else if (line.startsWith(':')) {
|
|
49
|
-
//
|
|
50
|
-
}
|
|
51
|
-
else if (line.startsWith('event:')) {
|
|
52
|
-
currentEvent = parseSSEFieldValue(line, 'event');
|
|
53
|
-
}
|
|
54
|
-
else if (line.startsWith('data:')) {
|
|
55
|
-
events.push({
|
|
56
|
-
event: currentEvent,
|
|
57
|
-
data: parseSSEFieldValue(line, 'data'),
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
lineBoundary = remaining.match(/\r?\n/);
|
|
61
|
-
}
|
|
62
|
-
return {
|
|
63
|
-
event: currentEvent,
|
|
64
|
-
events,
|
|
65
|
-
remainder: remaining,
|
|
66
|
-
};
|
|
67
|
-
};
|
|
68
|
-
export const getAnswerDelta = ({ event, data }) => {
|
|
69
|
-
if (!data || data === '[DONE]') {
|
|
70
|
-
return '';
|
|
71
|
-
}
|
|
72
|
-
if (event && IGNORED_SSE_EVENT_TYPES.test(event)) {
|
|
73
|
-
return '';
|
|
74
|
-
}
|
|
75
|
-
try {
|
|
76
|
-
const payload = JSON.parse(data);
|
|
77
|
-
if (typeof payload === 'string') {
|
|
78
|
-
return payload;
|
|
79
|
-
}
|
|
80
|
-
if (!isRecord(payload)) {
|
|
81
|
-
return '';
|
|
82
|
-
}
|
|
83
|
-
const type = typeof payload.type === 'string' ? payload.type : undefined;
|
|
84
|
-
if (type && IGNORED_SSE_EVENT_TYPES.test(type)) {
|
|
85
|
-
return '';
|
|
86
|
-
}
|
|
87
|
-
const choiceDelta = getChoicesDelta(payload.choices);
|
|
88
|
-
if (choiceDelta) {
|
|
89
|
-
return choiceDelta;
|
|
90
|
-
}
|
|
91
|
-
for (const key of STREAM_TEXT_KEYS) {
|
|
92
|
-
const text = getStringValue(payload[key]);
|
|
93
|
-
if (text) {
|
|
94
|
-
return text;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return '';
|
|
98
|
-
}
|
|
99
|
-
catch {
|
|
100
|
-
return data;
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
export function parseStreamContent(text) {
|
|
104
|
-
const docsMatches = [...text.matchAll(/<docs>([\s\S]*?)<\/docs>/g)];
|
|
105
|
-
const docsReferences = docsMatches.length
|
|
106
|
-
? unicodeToString(docsMatches.at(-1)[1])
|
|
107
|
-
: '[]';
|
|
108
|
-
let refDocs = [];
|
|
109
|
-
try {
|
|
110
|
-
refDocs = JSON.parse(docsReferences).filter((r) => isObject(r));
|
|
111
|
-
}
|
|
112
|
-
catch {
|
|
113
|
-
//
|
|
114
|
-
}
|
|
115
|
-
const thinkMatches = [...text.matchAll(/<think>([\s\S]*?)<\/think>/g)];
|
|
116
|
-
let thinkingProcess = thinkMatches.at(-1)?.[1];
|
|
117
|
-
if (!thinkingProcess) {
|
|
118
|
-
const openThinkMatch = text.match(/<think>([\s\S]*)$/);
|
|
119
|
-
thinkingProcess = openThinkMatch?.[1];
|
|
120
|
-
}
|
|
121
|
-
const content = text
|
|
122
|
-
.replace(/<docs>[\s\S]*?<\/docs>/g, '')
|
|
123
|
-
.replace(/<think>[\s\S]*?<\/think>/g, '')
|
|
124
|
-
.replace(/<docs>[\s\S]*$/g, '')
|
|
125
|
-
.replace(/<think>[\s\S]*$/g, '');
|
|
126
|
-
return {
|
|
127
|
-
refDocs,
|
|
128
|
-
thinkingProcess: thinkingProcess && unicodeToString(thinkingProcess),
|
|
129
|
-
content: unicodeToString(content),
|
|
130
|
-
};
|
|
131
|
-
}
|