返回 oh-my-ppt
edit-system.ts
根目录 / src / main / agent-runtime / prompt / composers / edit-system.ts
1 import type { SessionDeckGenerationContext } from '../../agent/types'
2 import { progressText } from '@shared/progress'
3 import { INDEX_TRANSITION_TYPES } from '../../../../shared/index-transition'
4 import {
5 buildCanvasScenarioContentRules,
6 buildCanvasScenarioDeliveryGuard,
7 buildCanvasScenarioExpansionRules,
8 buildLayoutCollisionRules,
9 buildPageSemanticStructure,
10 buildCanvasConstraints,
11 CONTENT_LANGUAGE_RULES,
12 CONTENT_WRITING_RULES,
13 FRONTEND_CAPABILITIES,
14 SOURCE_DOCUMENT_FACT_RULE,
15 SOURCE_DOCUMENT_READ_STRATEGY,
16 SOURCE_GROUNDED_EXPANSION_RULES,
17 STABLE_HTML_FRAGMENT_PROTOCOL,
18 STYLE_FIDELITY_RULES,
19 buildOutlinePageList,
20 formatDesignContract,
21 resolveContextStylePrompt
22 } from './shared'
23 import { buildCanvasScenarioBrief, resolveCanvasScenario } from './canvas-scenario'
24 import { createPromptCatalog } from '../catalog'
25 import { formatSelectedElementRuntimeContext } from '../selected-element-context'
26
27 import containerTemplate from '../templates/edit-system/container.md?raw'
28 import deckTemplate from '../templates/edit-system/deck.md?raw'
29 import selectorTemplate from '../templates/edit-system/selector.md?raw'
30 import singlePageTemplate from '../templates/edit-system/single-page.md?raw'
31
32 type EditSystemTemplateVars = {
33 container: {
34 contentLanguageRules: string
35 indexTransitionTypes: string
36 analyzingEditRequestLabel: string
37 editCompletedLabel: string
38 statusLanguage: string
39 presetLabel: string
40 presetId: string
41 stylePrompt: string
42 designContractSection: string
43 sourceDocumentSection: string
44 topic: string
45 deckTitle: string
46 existingInfo: string
47 pageList: string
48 }
49 selector: {
50 contentLanguageRules: string
51 presetLabel: string
52 presetId: string
53 stylePrompt: string
54 designContractSection: string
55 canvasConstraints: string
56 layoutCollisionRules: string
57 pageSemanticStructure: string
58 frontendCapabilities: string
59 sourceDocumentSection: string
60 analyzingEditRequestLabel: string
61 editCompletedLabel: string
62 statusLanguage: string
63 topic: string
64 deckTitle: string
65 targetInfo: string
66 targetFileLine: string
67 selectorInfo: string
68 elementInfo: string
69 elementRuntimeContextInfo: string
70 existingInfo: string
71 pageList: string
72 }
73 singlePage: {
74 canvasEditIdentity: string
75 targetPageId: string
76 canvasScenarioBrief: string
77 canvasScenarioContentRules: string
78 contentLanguageRules: string
79 contentWritingRules: string
80 stableHtmlFragmentProtocol: string
81 canvasScenarioExpansionRules: string
82 canvasConstraints: string
83 layoutCollisionRules: string
84 canvasScenarioDeliveryGuard: string
85 pageSemanticStructure: string
86 frontendCapabilities: string
87 sourceDocumentSection: string
88 analyzingEditRequestLabel: string
89 editCompletedLabel: string
90 statusLanguage: string
91 topic: string
92 deckTitle: string
93 targetInfo: string
94 targetFileLine: string
95 existingInfo: string
96 pageList: string
97 presetLabel: string
98 presetId: string
99 stylePrompt: string
100 designContractSection: string
101 styleFidelityRules: string
102 }
103 deck: {
104 canvasEditIdentity: string
105 canvasScenarioBrief: string
106 canvasScenarioContentRules: string
107 contentLanguageRules: string
108 contentWritingRules: string
109 stableHtmlFragmentProtocol: string
110 canvasScenarioExpansionRules: string
111 canvasConstraints: string
112 layoutCollisionRules: string
113 canvasScenarioDeliveryGuard: string
114 pageSemanticStructure: string
115 frontendCapabilities: string
116 sourceDocumentSection: string
117 analyzingEditRequestLabel: string
118 editCompletedLabel: string
119 statusLanguage: string
120 topic: string
121 deckTitle: string
122 explicitTargetInfo: string
123 existingInfo: string
124 pageList: string
125 presetLabel: string
126 presetId: string
127 stylePrompt: string
128 designContractSection: string
129 styleFidelityRules: string
130 }
131 }
132
133 const editSystemPromptCatalog = createPromptCatalog<EditSystemTemplateVars>({
134 container: containerTemplate.trimEnd(),
135 selector: selectorTemplate.trimEnd(),
136 singlePage: singlePageTemplate.trimEnd(),
137 deck: deckTemplate.trimEnd()
138 })
139
140 const buildOptionalSection = (content: string): string => (content ? `\n\n${content}` : '')
141
142 const buildDesignContractSection = (context: SessionDeckGenerationContext, label: string): string =>
143 context.designContract ? buildOptionalSection(`${label}\n${formatDesignContract(context.designContract)}`) : ''
144
145 /** Selects the scoped edit prompt; the individual composers own their allowed tools. */
146 export function buildEditAgentSystemPrompt(
147 styleId: string | null | undefined,
148 context: SessionDeckGenerationContext
149 ): string {
150 const isContainerScopeEdit =
151 context.mode === 'edit' && context.editScope === 'presentation-container'
152 const isDeckScopeEdit = context.mode === 'edit' && context.editScope === 'deck'
153 const hasSelector = Boolean(context.selectedSelector?.trim())
154
155 if (isContainerScopeEdit) return buildContainerEditPrompt(styleId, context)
156 if (hasSelector) return buildSelectorEditPrompt(styleId, context)
157 if (isDeckScopeEdit) return buildDeckEditPrompt(styleId, context)
158 return buildSinglePageEditPrompt(styleId, context)
159 }
160
161 function buildSourceDocumentEditInstructions(
162 context: SessionDeckGenerationContext,
163 options?: { includeExpansion?: boolean }
164 ): string {
165 const sourceDocumentPaths = (context.sourceDocumentPaths || []).filter(Boolean)
166 if (sourceDocumentPaths.length === 0) return ''
167 const lines = [
168 '## Source documents (content evidence)',
169 'The session has user-imported reference documents. When the edit changes slide facts, examples, metrics, terminology, conclusions, or source-backed page content, use the source document as the authority.',
170 `- sourceDocumentPaths: ${sourceDocumentPaths.join(', ')}`,
171 SOURCE_DOCUMENT_READ_STRATEGY,
172 '- For pure visual/style-only edits, do not reread the source document unless the user asks for source-backed content changes.',
173 SOURCE_DOCUMENT_FACT_RULE
174 ]
175 if (options?.includeExpansion) lines.push(SOURCE_GROUNDED_EXPANSION_RULES)
176 return lines.join('\n')
177 }
178
179 function buildContainerEditPrompt(
180 styleId: string | null | undefined,
181 context: SessionDeckGenerationContext
182 ): string {
183 void styleId
184 const { presetLabel, presetId, stylePrompt } = resolveContextStylePrompt(context)
185 return editSystemPromptCatalog.render('container', {
186 contentLanguageRules: CONTENT_LANGUAGE_RULES,
187 indexTransitionTypes: INDEX_TRANSITION_TYPES.join(' / '),
188 analyzingEditRequestLabel: progressText(context.appLocale, 'understanding'),
189 editCompletedLabel: progressText(context.appLocale, 'completed'),
190 statusLanguage: context.appLocale === 'en' ? 'English' : 'Simplified Chinese',
191 presetLabel,
192 presetId,
193 stylePrompt,
194 designContractSection: buildDesignContractSection(context, '设计契约(本次演示的统一视觉参考):'),
195 sourceDocumentSection: buildOptionalSection(buildSourceDocumentEditInstructions(context)),
196 topic: context.topic,
197 deckTitle: context.deckTitle,
198 existingInfo: context.existingPageIds?.length
199 ? `Existing page IDs: ${context.existingPageIds.join(', ')}`
200 : '',
201 pageList: buildOutlinePageList(context)
202 })
203 }
204
205 function buildSelectorEditPrompt(
206 styleId: string | null | undefined,
207 context: SessionDeckGenerationContext
208 ): string {
209 void styleId
210 const { presetLabel, presetId, stylePrompt } = resolveContextStylePrompt(context)
211 const targetPagePath =
212 context.selectedPageId && context.pageFileMap[context.selectedPageId]
213 ? `/${context.selectedPageId}.html`
214 : undefined
215 return editSystemPromptCatalog.render('selector', {
216 contentLanguageRules: CONTENT_LANGUAGE_RULES,
217 presetLabel,
218 presetId,
219 stylePrompt,
220 designContractSection: buildDesignContractSection(
221 context,
222 '设计契约(本次演示的统一视觉参考,修改时保持协调即可):'
223 ),
224 canvasConstraints: buildCanvasConstraints(context.slideSize),
225 layoutCollisionRules: buildLayoutCollisionRules(context.slideSize),
226 pageSemanticStructure: buildPageSemanticStructure(context.slideSize),
227 frontendCapabilities: FRONTEND_CAPABILITIES,
228 sourceDocumentSection: buildOptionalSection(buildSourceDocumentEditInstructions(context)),
229 analyzingEditRequestLabel: progressText(context.appLocale, 'understanding'),
230 editCompletedLabel: progressText(context.appLocale, 'completed'),
231 statusLanguage: context.appLocale === 'en' ? 'English' : 'Simplified Chinese',
232 topic: context.topic,
233 deckTitle: context.deckTitle,
234 targetInfo: context.selectedPageId
235 ? `Target page: ${context.selectedPageId} (slide ${context.selectedPageNumber ?? '?'})`
236 : 'Target page: infer from the user message.',
237 targetFileLine: targetPagePath ? `Target file: ${targetPagePath}` : '',
238 selectorInfo: `Target element selector: ${context.selectedSelector}`,
239 elementInfo: context.elementTag
240 ? `Target element: <${context.elementTag}>${context.elementText ? `"${context.elementText}"` : ''}`
241 : '',
242 elementRuntimeContextInfo: formatSelectedElementRuntimeContext(
243 context.selectedElementContext
244 ),
245 existingInfo: context.existingPageIds?.length
246 ? `Existing page IDs: ${context.existingPageIds.join(', ')}`
247 : '',
248 pageList: buildOutlinePageList(context)
249 })
250 }
251
252 function buildSinglePageEditPrompt(
253 styleId: string | null | undefined,
254 context: SessionDeckGenerationContext
255 ): string {
256 void styleId
257 const { presetLabel, presetId, stylePrompt } = resolveContextStylePrompt(context)
258 const targetPageId = context.selectedPageId || context.allowedPageIds?.[0] || ''
259 const canvasScenario = resolveCanvasScenario(context.slideSize)
260 return editSystemPromptCatalog.render('singlePage', {
261 canvasEditIdentity: canvasScenario.editIdentity,
262 targetPageId,
263 canvasScenarioBrief: buildCanvasScenarioBrief(context.slideSize),
264 canvasScenarioContentRules: buildCanvasScenarioContentRules(context.slideSize),
265 contentLanguageRules: CONTENT_LANGUAGE_RULES,
266 contentWritingRules: CONTENT_WRITING_RULES,
267 stableHtmlFragmentProtocol: STABLE_HTML_FRAGMENT_PROTOCOL,
268 canvasScenarioExpansionRules: buildCanvasScenarioExpansionRules(context.slideSize),
269 canvasConstraints: buildCanvasConstraints(context.slideSize),
270 layoutCollisionRules: buildLayoutCollisionRules(context.slideSize),
271 canvasScenarioDeliveryGuard: buildCanvasScenarioDeliveryGuard(context.slideSize),
272 pageSemanticStructure: buildPageSemanticStructure(context.slideSize),
273 frontendCapabilities: FRONTEND_CAPABILITIES,
274 sourceDocumentSection: buildOptionalSection(
275 buildSourceDocumentEditInstructions(context, { includeExpansion: true })
276 ),
277 analyzingEditRequestLabel: progressText(context.appLocale, 'understanding'),
278 editCompletedLabel: progressText(context.appLocale, 'completed'),
279 statusLanguage: context.appLocale === 'en' ? 'English' : 'Simplified Chinese',
280 topic: context.topic,
281 deckTitle: context.deckTitle,
282 targetInfo: `Target page: ${targetPageId} (slide ${context.selectedPageNumber ?? '?'})`,
283 targetFileLine: targetPageId ? `Target file: /${targetPageId}.html` : '',
284 existingInfo: context.existingPageIds?.length
285 ? `Existing page IDs: ${context.existingPageIds.join(', ')}`
286 : '',
287 pageList: buildOutlinePageList(context),
288 presetLabel,
289 presetId,
290 stylePrompt,
291 designContractSection: buildDesignContractSection(
292 context,
293 '设计契约(本次演示的统一视觉参考,修改时保持协调即可):'
294 ),
295 styleFidelityRules: STYLE_FIDELITY_RULES
296 })
297 }
298
299 function buildDeckEditPrompt(
300 styleId: string | null | undefined,
301 context: SessionDeckGenerationContext
302 ): string {
303 void styleId
304 const { presetLabel, presetId, stylePrompt } = resolveContextStylePrompt(context)
305 const canvasScenario = resolveCanvasScenario(context.slideSize)
306 const explicitTargetInfo = context.selectPageIds?.length
307 ? `Selected page ids from UI (hard target): ${context.selectPageIds.join(', ')}`
308 : 'Target pages: all relevant /<pageId>.html files'
309 return editSystemPromptCatalog.render('deck', {
310 canvasEditIdentity: canvasScenario.editIdentity,
311 canvasScenarioBrief: buildCanvasScenarioBrief(context.slideSize),
312 canvasScenarioContentRules: buildCanvasScenarioContentRules(context.slideSize),
313 contentLanguageRules: CONTENT_LANGUAGE_RULES,
314 contentWritingRules: CONTENT_WRITING_RULES,
315 stableHtmlFragmentProtocol: STABLE_HTML_FRAGMENT_PROTOCOL,
316 canvasScenarioExpansionRules: buildCanvasScenarioExpansionRules(context.slideSize),
317 canvasConstraints: buildCanvasConstraints(context.slideSize),
318 layoutCollisionRules: buildLayoutCollisionRules(context.slideSize),
319 canvasScenarioDeliveryGuard: buildCanvasScenarioDeliveryGuard(context.slideSize),
320 pageSemanticStructure: buildPageSemanticStructure(context.slideSize),
321 frontendCapabilities: FRONTEND_CAPABILITIES,
322 sourceDocumentSection: buildOptionalSection(
323 buildSourceDocumentEditInstructions(context, { includeExpansion: true })
324 ),
325 analyzingEditRequestLabel: progressText(context.appLocale, 'understanding'),
326 editCompletedLabel: progressText(context.appLocale, 'completed'),
327 statusLanguage: context.appLocale === 'en' ? 'English' : 'Simplified Chinese',
328 topic: context.topic,
329 deckTitle: context.deckTitle,
330 explicitTargetInfo,
331 existingInfo: context.existingPageIds?.length
332 ? `Existing page IDs: ${context.existingPageIds.join(', ')}`
333 : '',
334 pageList: buildOutlinePageList(context),
335 presetLabel,
336 presetId,
337 stylePrompt,
338 designContractSection: buildDesignContractSection(
339 context,
340 '设计契约(本次演示的统一视觉参考,修改时保持协调即可):'
341 ),
342 styleFidelityRules: STYLE_FIDELITY_RULES
343 })
344 }
345
345 lines TYPESCRIPT