@@ -62,6 +62,7 @@ const ObjectBrowser: React.FC<ObjectBrowserProps> = ({ chatId }) => {
6262 onDelete = { ( ) => handleNodeDelete ( nodeId ) }
6363 onClearChildren = { ( ) => handleClearChildren ( nodeId ) }
6464 onSaveEdit = { ( id , newValue ) => handleSaveEdit ( id , newValue ) }
65+ onCreateChat = { ( id , name ) => handleCreateChat ( id , name ) }
6566 />
6667 ) ,
6768 key : nodeId ,
@@ -200,6 +201,230 @@ const ObjectBrowser: React.FC<ObjectBrowserProps> = ({ chatId }) => {
200201 [ dispatch , chat . id , nodes , modal ]
201202 )
202203
204+ // 获取节点上下文信息
205+ const getNodeContext = useCallback ( ( currentNode : ObjectNodeType ) => {
206+ if ( ! currentNode ) return ''
207+
208+ // 获取节点完整信息
209+ const getNodeInformation = ( node : ObjectNodeType ) : string => {
210+ let information = `# 节点 - [${ node . name } ]\n${ node . description || '' } `
211+
212+ // 添加属性信息
213+ if ( node . properties && Object . keys ( node . properties ) . length > 0 ) {
214+ const properties = Object . entries ( node . properties ) . map ( ( [ key , value ] ) => ( {
215+ Name : key ,
216+ Value : value
217+ } ) )
218+ information += `\n## 属性列表\n${ JSON . stringify ( properties ) } `
219+ }
220+
221+ // 添加子节点信息
222+ if ( node . children && node . children . length > 0 ) {
223+ const children = node . children
224+ . map ( ( childId ) => {
225+ const childNode = nodes [ childId ]
226+ return childNode
227+ ? childNode . description
228+ ? {
229+ Name : childNode . name ,
230+ Description : childNode . description
231+ }
232+ : {
233+ Name : childNode . name
234+ }
235+ : null
236+ } )
237+ . filter ( Boolean )
238+
239+ if ( children . length > 0 ) {
240+ information += `\n## 子节点列表\n${ JSON . stringify ( children ) } `
241+ }
242+ }
243+
244+ // 添加引用信息
245+ if ( node . references && node . references . length > 0 ) {
246+ const references = node . references . map ( ( ref ) => {
247+ const refNode = nodes [ ref . id ]
248+ return {
249+ Name : ref . name ,
250+ Description : ref . description || '' ,
251+ Type : ref . type ,
252+ Strength : ref . strength ,
253+ NodeExists : ! ! refNode ,
254+ NodeDescription : refNode ?. description || ''
255+ }
256+ } )
257+ information += `\n## 引用关系\n${ JSON . stringify ( references ) } `
258+ }
259+
260+ return information
261+ }
262+
263+ // 从当前节点向上追踪到根节点,构建完整链路
264+ const buildAncestorChain = ( node : ObjectNodeType ) : ObjectNodeType [ ] => {
265+ const chain : ObjectNodeType [ ] = [ ]
266+ let current = node
267+
268+ // 向上追踪到根节点
269+ while ( current ) {
270+ chain . unshift ( current )
271+ if ( current . parentId && nodes [ current . parentId ] ) {
272+ current = nodes [ current . parentId ]
273+ } else {
274+ break
275+ }
276+ }
277+
278+ return chain
279+ }
280+
281+ // 获取同级节点
282+ const getSiblings = ( node : ObjectNodeType ) : ObjectNodeType [ ] => {
283+ if ( ! node . parentId ) return [ ]
284+ const parent = nodes [ node . parentId ]
285+ if ( ! parent || ! parent . children ) return [ ]
286+ return parent . children
287+ . filter ( ( id ) => id !== node . id )
288+ . map ( ( id ) => nodes [ id ] )
289+ . filter ( Boolean )
290+ }
291+
292+ // 获取当前节点的引用信息
293+ const getCurrentReferences = ( node : ObjectNodeType ) => {
294+ if ( ! node . references ) return [ ]
295+ return node . references . map ( ( ref ) => ( {
296+ ...ref ,
297+ referencedNode : nodes [ ref . id ] || null
298+ } ) )
299+ }
300+
301+ // 获取引用当前节点的其他节点(反向引用)
302+ const getIncomingReferences = ( node : ObjectNodeType ) => {
303+ const incomingRefs : Array < {
304+ fromNode : ObjectNodeType
305+ reference : any
306+ } > = [ ]
307+
308+ Object . values ( nodes ) . forEach ( ( otherNode ) => {
309+ if ( otherNode . id !== node . id && otherNode . references ) {
310+ otherNode . references . forEach ( ( ref ) => {
311+ if ( ref . id === node . id ) {
312+ incomingRefs . push ( {
313+ fromNode : otherNode ,
314+ reference : ref
315+ } )
316+ }
317+ } )
318+ }
319+ } )
320+
321+ return incomingRefs
322+ }
323+
324+ const ancestorChain = buildAncestorChain ( currentNode )
325+ const siblings = getSiblings ( currentNode )
326+ const currentReferences = getCurrentReferences ( currentNode )
327+ const incomingReferences = getIncomingReferences ( currentNode )
328+
329+ // 构建完整的上下文信息
330+ let contextInfo = '# 完整上下文信息\n\n'
331+
332+ // 添加层级结构信息
333+ contextInfo += '## 节点层级结构\n'
334+ contextInfo += '从根节点到当前节点的完整路径:\n'
335+ ancestorChain . forEach ( ( ancestor , index ) => {
336+ const indent = ' ' . repeat ( index )
337+ contextInfo += `${ indent } - ${ ancestor . name } `
338+ if ( ancestor . description ) {
339+ contextInfo += ` (${ ancestor . description } )`
340+ }
341+ contextInfo += '\n'
342+ } )
343+
344+ // 添加每个层级节点的详细信息
345+ contextInfo += '\n## 路径节点详细信息\n'
346+ ancestorChain . forEach ( ( ancestor , index ) => {
347+ contextInfo += `\n### 第${ index + 1 } 层节点\n`
348+ contextInfo += getNodeInformation ( ancestor )
349+ contextInfo += '\n'
350+ } )
351+
352+ // 添加同级节点信息
353+ if ( siblings . length > 0 ) {
354+ contextInfo += '\n## 同级节点信息\n'
355+ siblings . forEach ( ( sibling ) => {
356+ contextInfo += `\n### 同级节点 - ${ sibling . name } \n`
357+ contextInfo += getNodeInformation ( sibling )
358+ contextInfo += '\n'
359+ } )
360+ } else {
361+ contextInfo += '\n## 同级节点信息\n无同级节点\n'
362+ }
363+
364+ // 添加引用关系信息
365+ if ( currentReferences . length > 0 ) {
366+ contextInfo += '\n## 当前节点的引用关系\n'
367+ currentReferences . forEach ( ( ref ) => {
368+ contextInfo += `\n### 引用节点 - ${ ref . name } \n`
369+ contextInfo += `- **引用类型**: ${ ref . type } \n`
370+ contextInfo += `- **引用强度**: ${ ref . strength } \n`
371+ if ( ref . description ) {
372+ contextInfo += `- **引用描述**: ${ ref . description } \n`
373+ }
374+ if ( ref . referencedNode ) {
375+ contextInfo += `- **节点存在**: 是\n`
376+ contextInfo += `- **节点描述**: ${ ref . referencedNode . description || '无' } \n`
377+ } else {
378+ contextInfo += `- **节点存在**: 否(可能已被删除)\n`
379+ }
380+ contextInfo += '\n'
381+ } )
382+ } else {
383+ contextInfo += '\n## 当前节点的引用关系\n当前节点无引用其他节点\n'
384+ }
385+
386+ // 添加反向引用信息
387+ if ( incomingReferences . length > 0 ) {
388+ contextInfo += '\n## 被其他节点引用的情况\n'
389+ incomingReferences . forEach ( ( incomingRef ) => {
390+ contextInfo += `\n### 来自节点 - ${ incomingRef . fromNode . name } \n`
391+ contextInfo += `- **引用类型**: ${ incomingRef . reference . type } \n`
392+ contextInfo += `- **引用强度**: ${ incomingRef . reference . strength } \n`
393+ if ( incomingRef . reference . description ) {
394+ contextInfo += `- **引用描述**: ${ incomingRef . reference . description } \n`
395+ }
396+ contextInfo += `- **来源节点描述**: ${ incomingRef . fromNode . description || '无' } \n`
397+ contextInfo += '\n'
398+ } )
399+ } else {
400+ contextInfo += '\n## 被其他节点引用的情况\n当前节点未被其他节点引用\n'
401+ }
402+
403+ return contextInfo
404+ } , [ nodes ] )
405+
406+ // 处理创建对话
407+ const handleCreateChat = useCallback (
408+ ( nodeId : string , nodeName : string ) => {
409+ const node = nodes [ nodeId ]
410+ if ( ! node ) return
411+
412+ const nodeContext = getNodeContext ( node )
413+
414+ dispatch ( {
415+ type : 'CREATE_CHAT_FROM_OBJECT_NODE' ,
416+ payload : {
417+ folderId : chat . folderId ,
418+ nodeId,
419+ nodeName,
420+ nodeContext,
421+ sourcePageId : chatId
422+ }
423+ } )
424+ } ,
425+ [ dispatch , chat . folderId , chatId , nodes , getNodeContext ]
426+ )
427+
203428 // 搜索处理
204429 const handleSearch = useCallback ( ( e : React . ChangeEvent < HTMLInputElement > ) => {
205430 const query = e . target . value
0 commit comments