@nuasite/cms 0.19.1 → 0.20.2
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/dist/editor.js +12615 -12689
- package/package.json +3 -3
- package/src/build-processor.ts +4 -4
- package/src/dev-middleware.ts +185 -189
- package/src/editor/api.ts +0 -251
- package/src/editor/components/fields.tsx +6 -6
- package/src/editor/components/markdown-editor-overlay.tsx +46 -70
- package/src/editor/components/markdown-inline-editor.tsx +34 -165
- package/src/editor/components/mdx-block-view.tsx +351 -47
- package/src/editor/components/mdx-component-picker.tsx +35 -11
- package/src/editor/components/media-library.tsx +1 -15
- package/src/editor/components/modal-shell.tsx +1 -1
- package/src/editor/components/toolbar.tsx +0 -75
- package/src/editor/constants.ts +0 -4
- package/src/editor/editor.ts +2 -192
- package/src/editor/hooks/index.ts +0 -3
- package/src/editor/hooks/useBlockEditorHandlers.ts +1 -8
- package/src/editor/hooks/useTooltipState.ts +1 -2
- package/src/editor/index.tsx +2 -18
- package/src/editor/milkdown-mdx-plugin.tsx +116 -19
- package/src/editor/milkdown-utils.ts +174 -0
- package/src/editor/post-message.ts +0 -6
- package/src/editor/signals.ts +0 -183
- package/src/editor/styles.css +0 -108
- package/src/editor/types.ts +0 -76
- package/src/html-processor.ts +9 -7
- package/src/source-finder/cache.ts +47 -0
- package/src/source-finder/collection-finder.ts +181 -0
- package/src/source-finder/index.ts +5 -2
- package/src/source-finder/search-index.ts +79 -0
- package/src/source-finder/snippet-utils.ts +36 -61
- package/src/types.ts +0 -4
- package/src/utils.ts +10 -0
- package/src/vite-plugin.ts +24 -4
- package/src/editor/ai.ts +0 -185
- package/src/editor/components/ai-chat.tsx +0 -631
- package/src/editor/components/ai-tooltip.tsx +0 -180
- package/src/editor/components/mdx-props-editor.tsx +0 -94
- package/src/editor/hooks/useAIHandlers.ts +0 -345
package/src/editor/ai.ts
DELETED
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import { type CmsAiAction, type CmsAiChatRequest, type CmsAiStreamCallbacks, streamAiChat } from './api'
|
|
2
|
-
import { logDebug } from './dom'
|
|
3
|
-
import type { CmsConfig, ComponentProp } from './types'
|
|
4
|
-
|
|
5
|
-
// Re-export for consumers
|
|
6
|
-
export type { CmsAiAction }
|
|
7
|
-
|
|
8
|
-
export interface AIRequest {
|
|
9
|
-
prompt: string
|
|
10
|
-
elementId: string
|
|
11
|
-
currentContent: string
|
|
12
|
-
context?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface AIBlockPropsRequest {
|
|
16
|
-
prompt: string
|
|
17
|
-
componentName: string
|
|
18
|
-
props: ComponentProp[]
|
|
19
|
-
currentValues: Record<string, unknown>
|
|
20
|
-
context?: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface AIStreamCallbacks {
|
|
24
|
-
onStart?: () => void
|
|
25
|
-
onToken?: (token: string, fullText: string) => void
|
|
26
|
-
onComplete?: (finalText: string) => void
|
|
27
|
-
onError?: (error: Error) => void
|
|
28
|
-
/** Called when AI provides status updates (thinking, coding, building, deploying) */
|
|
29
|
-
onStatus?: (status: string, message?: string) => void
|
|
30
|
-
/** Called when AI requests an action (refresh page, show preview, apply edit) */
|
|
31
|
-
onAction?: (action: CmsAiAction) => void
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface AIConfig {
|
|
35
|
-
endpoint: string
|
|
36
|
-
headers?: Record<string, string>
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function getAIConfig(config: CmsConfig): AIConfig {
|
|
40
|
-
return {
|
|
41
|
-
endpoint: config.apiBase,
|
|
42
|
-
headers: {
|
|
43
|
-
'Content-Type': 'application/json',
|
|
44
|
-
},
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export class AIService {
|
|
49
|
-
private config: CmsConfig
|
|
50
|
-
private aiConfig: AIConfig
|
|
51
|
-
private abortController: AbortController | null = null
|
|
52
|
-
|
|
53
|
-
constructor(config: CmsConfig) {
|
|
54
|
-
this.config = config
|
|
55
|
-
this.aiConfig = getAIConfig(config)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Stream AI chat request using rich SSE events
|
|
60
|
-
* Supports status updates, actions (refresh, preview), and streaming text
|
|
61
|
-
*/
|
|
62
|
-
async streamRequest(request: AIRequest, callbacks: AIStreamCallbacks): Promise<void> {
|
|
63
|
-
this.abortController = new AbortController()
|
|
64
|
-
let fullText = ''
|
|
65
|
-
|
|
66
|
-
callbacks.onStart?.()
|
|
67
|
-
|
|
68
|
-
const chatRequest: CmsAiChatRequest = {
|
|
69
|
-
prompt: request.prompt,
|
|
70
|
-
elementId: request.elementId,
|
|
71
|
-
currentContent: request.currentContent,
|
|
72
|
-
context: request.context,
|
|
73
|
-
pageUrl: window.location.href,
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const streamCallbacks: CmsAiStreamCallbacks = {
|
|
77
|
-
onToken: (token, accumulatedText) => {
|
|
78
|
-
fullText = accumulatedText
|
|
79
|
-
callbacks.onToken?.(token, accumulatedText)
|
|
80
|
-
},
|
|
81
|
-
onStatus: (status, message) => {
|
|
82
|
-
logDebug(this.config.debug, 'AI status:', status, message)
|
|
83
|
-
callbacks.onStatus?.(status, message)
|
|
84
|
-
},
|
|
85
|
-
onAction: action => {
|
|
86
|
-
logDebug(this.config.debug, 'AI action:', action)
|
|
87
|
-
callbacks.onAction?.(action)
|
|
88
|
-
},
|
|
89
|
-
onError: (error, code) => {
|
|
90
|
-
logDebug(this.config.debug, 'AI error:', error, code)
|
|
91
|
-
callbacks.onError?.(new Error(error))
|
|
92
|
-
},
|
|
93
|
-
onDone: summary => {
|
|
94
|
-
logDebug(this.config.debug, 'AI done:', summary)
|
|
95
|
-
callbacks.onComplete?.(fullText)
|
|
96
|
-
},
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
await streamAiChat(
|
|
101
|
-
this.aiConfig.endpoint,
|
|
102
|
-
chatRequest,
|
|
103
|
-
streamCallbacks,
|
|
104
|
-
this.abortController.signal,
|
|
105
|
-
)
|
|
106
|
-
} catch (error) {
|
|
107
|
-
callbacks.onError?.(error instanceof Error ? error : new Error(String(error)))
|
|
108
|
-
} finally {
|
|
109
|
-
this.abortController = null
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
abort(): void {
|
|
114
|
-
if (this.abortController) {
|
|
115
|
-
this.abortController.abort()
|
|
116
|
-
this.abortController = null
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
isStreaming(): boolean {
|
|
121
|
-
return this.abortController !== null
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Generate prop values for a component using AI
|
|
126
|
-
*/
|
|
127
|
-
async generateBlockProps(request: AIBlockPropsRequest): Promise<Record<string, unknown>> {
|
|
128
|
-
try {
|
|
129
|
-
const response = await fetch(`${this.aiConfig.endpoint}/ai/generate-props`, {
|
|
130
|
-
method: 'POST',
|
|
131
|
-
credentials: 'include',
|
|
132
|
-
headers: this.aiConfig.headers,
|
|
133
|
-
body: JSON.stringify({
|
|
134
|
-
prompt: request.prompt,
|
|
135
|
-
componentName: request.componentName,
|
|
136
|
-
props: request.props,
|
|
137
|
-
currentValues: request.currentValues,
|
|
138
|
-
context: request.context,
|
|
139
|
-
pageUrl: window.location.href,
|
|
140
|
-
}),
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
if (!response.ok) {
|
|
144
|
-
throw new Error(`AI request failed: ${response.status} ${response.statusText}`)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
const result = await response.json()
|
|
148
|
-
return result.props || {}
|
|
149
|
-
} catch (error) {
|
|
150
|
-
logDebug(this.config.debug, 'AI generateBlockProps error:', error)
|
|
151
|
-
throw error
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Suggest the best component to use based on user intent
|
|
157
|
-
*/
|
|
158
|
-
async suggestComponent(
|
|
159
|
-
prompt: string,
|
|
160
|
-
availableComponents: Array<{ name: string; description?: string; props: ComponentProp[] }>,
|
|
161
|
-
): Promise<{ componentName: string; suggestedProps: Record<string, unknown> } | null> {
|
|
162
|
-
try {
|
|
163
|
-
const response = await fetch(`${this.aiConfig.endpoint}/ai/suggest-component`, {
|
|
164
|
-
method: 'POST',
|
|
165
|
-
credentials: 'include',
|
|
166
|
-
headers: this.aiConfig.headers,
|
|
167
|
-
body: JSON.stringify({
|
|
168
|
-
prompt,
|
|
169
|
-
availableComponents,
|
|
170
|
-
pageUrl: window.location.href,
|
|
171
|
-
}),
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
if (!response.ok) {
|
|
175
|
-
throw new Error(`AI request failed: ${response.status} ${response.statusText}`)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const result = await response.json()
|
|
179
|
-
return result.suggestion || null
|
|
180
|
-
} catch (error) {
|
|
181
|
-
logDebug(this.config.debug, 'AI suggestComponent error:', error)
|
|
182
|
-
throw error
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
}
|