Line data Source code
1 : import { api } from '@/scripts/api'
2 : import type { ComfyNodeDefImpl } from '@/core/graph/nodeDef/ComfyNodeDefImpl'
3 : import { NodeSourceType, getNodeSource } from '@/types/nodeSource'
4 : import { extractCustomNodeName } from '@/platform/customNodes/manager/utils/packUtils'
5 :
6 2355 : class NodeHelpService {
7 : async fetchNodeHelp(node: ComfyNodeDefImpl, locale: string): Promise<string> {
8 18 : const nodeSource = getNodeSource(node.python_module)
9 :
10 18 : if (nodeSource.type === NodeSourceType.Blueprint) {
11 0 : return node.description || ''
12 : }
13 :
14 18 : if (nodeSource.type === NodeSourceType.CustomNodes) {
15 1 : return this.fetchCustomNodeHelp(node, locale)
16 : } else {
17 17 : return this.fetchCoreNodeHelp(node, locale)
18 : }
19 : }
20 :
21 : private async fetchCustomNodeHelp(
22 : node: ComfyNodeDefImpl,
23 : locale: string
24 : ): Promise<string> {
25 1 : const customNodeName = extractCustomNodeName(node.python_module)
26 1 : let lastError: string | undefined
27 1 : if (!customNodeName) {
28 0 : throw new Error('Invalid custom node module')
29 : }
30 :
31 : // Try locale-specific path first
32 1 : const localePath = `/extensions/${customNodeName}/docs/${node.name}/${locale}.md`
33 1 : const localeDoc = await this.tryFetchMarkdown(localePath)
34 1 : if (localeDoc.text) return localeDoc.text
35 1 : lastError = localeDoc.errorText
36 :
37 : // Fall back to non-locale path
38 1 : const fallbackPath = `/extensions/${customNodeName}/docs/${node.name}.md`
39 1 : const fallbackDoc = await this.tryFetchMarkdown(fallbackPath)
40 1 : if (fallbackDoc.text) return fallbackDoc.text
41 1 : lastError = fallbackDoc.errorText ?? lastError
42 :
43 1 : throw new Error(lastError ?? 'Help not found')
44 : }
45 :
46 : private async fetchCoreNodeHelp(
47 : node: ComfyNodeDefImpl,
48 : locale: string
49 : ): Promise<string> {
50 17 : const mdUrl = `/docs/${node.name}/${locale}.md`
51 17 : const doc = await this.tryFetchMarkdown(mdUrl)
52 15 : if (!doc.text) {
53 1 : throw new Error(doc.errorText ?? 'Help not found')
54 : }
55 :
56 14 : return doc.text
57 : }
58 :
59 : /**
60 : * Fetch a markdown file and return its text, guarding against HTML/SPA fallbacks.
61 : * Returns null when not OK or when the content type indicates HTML.
62 : */
63 : private async tryFetchMarkdown(
64 : path: string
65 : ): Promise<{ text: string | null; errorText?: string }> {
66 19 : const res = await fetch(api.fileURL(path))
67 :
68 17 : if (!res.ok) {
69 3 : return { text: null, errorText: res.statusText }
70 : }
71 :
72 19 : const contentType = res.headers.get('content-type') ?? ''
73 19 : const text = await res.text()
74 :
75 : const isHtmlContentType = contentType.includes('text/html')
76 :
77 14 : if (isHtmlContentType) return { text: null, errorText: res.statusText }
78 :
79 14 : return { text }
80 : }
81 : }
82 :
83 : // Export singleton instance
84 2355 : export const nodeHelpService = new NodeHelpService()
|