@huyooo/ai-chat-shared 0.2.14 → 0.2.16

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/src/types.ts DELETED
@@ -1,77 +0,0 @@
1
- /**
2
- * AI Chat 共享类型定义
3
- */
4
-
5
- // ============ 内容块类型 ============
6
-
7
- /** 内容块类型 */
8
- export type ContentBlockType = 'text' | 'code'
9
-
10
- /** 内容块基础接口 */
11
- export interface ContentBlockBase {
12
- id: string
13
- type: ContentBlockType
14
- }
15
-
16
- /** 文本块 */
17
- export interface TextBlock extends ContentBlockBase {
18
- type: 'text'
19
- content: string
20
- }
21
-
22
- /** 代码块 */
23
- export interface CodeBlock extends ContentBlockBase {
24
- type: 'code'
25
- content: string
26
- language?: string
27
- filename?: string
28
- }
29
-
30
- /** 内容块联合类型 */
31
- export type ContentBlock = TextBlock | CodeBlock
32
-
33
-
34
- // ============ 内置工具结果数据类型 ============
35
-
36
- /** 天气数据 */
37
- export interface WeatherData {
38
- city: string
39
- temperature: number
40
- condition: string
41
- humidity?: number
42
- wind?: string
43
- icon?: string
44
- forecast?: WeatherForecastItem[]
45
- }
46
-
47
- /** 天气预报项 */
48
- export interface WeatherForecastItem {
49
- date: string
50
- high: number
51
- low: number
52
- condition: string
53
- icon?: string
54
- }
55
-
56
- /** 搜索结果项 */
57
- export interface SearchResultItem {
58
- title: string
59
- url: string
60
- snippet?: string
61
- }
62
-
63
- /** 代码执行结果 */
64
- export interface CodeExecutionResult {
65
- output: string
66
- exitCode: number
67
- error?: string
68
- }
69
-
70
- /** 文件操作结果 */
71
- export interface FileOperationResult {
72
- success: boolean
73
- path: string
74
- operation: 'read' | 'write' | 'delete' | 'create' | 'move'
75
- content?: string
76
- error?: string
77
- }
package/src/visual.ts DELETED
@@ -1,68 +0,0 @@
1
- import { renderMarkdown } from './markdown'
2
-
3
- function normalizeLanguage(lang?: string): string {
4
- return (lang || '').trim().toLowerCase()
5
- }
6
-
7
- export function isMermaidLanguage(lang?: string): boolean {
8
- return normalizeLanguage(lang) === 'mermaid'
9
- }
10
-
11
- export function isLatexLanguage(lang?: string): boolean {
12
- const l = normalizeLanguage(lang)
13
- return l === 'latex' || l === 'katex' || l === 'tex'
14
- }
15
-
16
- export function isLatexDocument(code: string): boolean {
17
- const t = (code || '').trim()
18
- return /\\documentclass\b|\\usepackage\b|\\begin\{document\}|\\end\{document\}/.test(t)
19
- }
20
-
21
- export function canVisualizeLatex(code: string): boolean {
22
- const t = (code || '').trim()
23
- if (!t) return false
24
- if (isLatexDocument(t)) return false
25
- return true
26
- }
27
-
28
- export function hasLatexDelimiters(code: string): boolean {
29
- const t = (code || '').trim()
30
- if (!t) return false
31
- return (
32
- /\$\$[\s\S]*\$\$/.test(t) ||
33
- /\\\[[\s\S]*\\\]/.test(t) ||
34
- /\\\([\s\S]*\\\)/.test(t) ||
35
- /(?<!\$)\$(?!\$)[\s\S]*?\$(?!\$)/.test(t)
36
- )
37
- }
38
-
39
- export function shouldShowVisualToggle(lang?: string, code?: string): boolean {
40
- if (isMermaidLanguage(lang)) return true
41
- if (isLatexLanguage(lang)) return canVisualizeLatex(code || '')
42
- return false
43
- }
44
-
45
- /**
46
- * 将 fenced 的 latex/katex/tex 代码块渲染成 HTML(复用 shared 的 renderMarkdown + latex 预处理)。
47
- * - 如果是完整 LaTeX 文档:返回提示 + code block(避免 KaTeX 报错)
48
- * - 如果已经带分隔符:直接渲染(避免二次包裹)
49
- * - 否则:自动包一层 $$...$$ 作为 display 公式渲染
50
- */
51
- export function renderFencedLatexToHtml(code: string): string {
52
- const t = (code || '').trim()
53
- if (!t) return ''
54
-
55
- if (!canVisualizeLatex(t)) {
56
- return renderMarkdown(
57
- `> 不支持将完整 LaTeX 文档作为公式渲染,请切换到代码视图查看。\n\n\`\`\`latex\n${t}\n\`\`\``
58
- )
59
- }
60
-
61
- if (hasLatexDelimiters(t)) {
62
- return renderMarkdown(t)
63
- }
64
-
65
- return renderMarkdown(`$$\n${t}\n$$`)
66
- }
67
-
68
-