@@ -15,7 +15,6 @@ import * as jsonDiffPatch from 'jsondiffpatch'
1515import DiffMatchPatch from 'diff-match-patch'
1616import { constants , utils } from '@opentiny/tiny-engine-utils'
1717import { useHistory , getMetaApi , useMessage } from '@opentiny/tiny-engine-meta-register'
18- import useAIChat from '../../../container/src/composables/useAIChat'
1918import type { canvasApi as CanvasApi } from '../../../container/src/container'
2019import type { Node , RootNode } from '../../../types'
2120import type {
@@ -85,6 +84,43 @@ const rootSchema = ref([
8584 }
8685] )
8786
87+ // 初始化单个节点的AI状态
88+ const initializeNodeAIStatus = ( node : object , initialStatus : Partial < NodeAIStatus > = { } ) => {
89+ if ( ! pageState . nodesStatus [ node . id ] ) {
90+ pageState . nodesStatus [ node . id ] = { }
91+ }
92+
93+ pageState . nodesStatus [ node . id ] . aiStatus = {
94+ state : 'hidden' ,
95+ originalNodeData : deepClone ( node ) ,
96+ aiModifiedNodeData : undefined ,
97+ aiContext : null ,
98+ lastAIAction : '' ,
99+ aiHistory : [ ] ,
100+ ...initialStatus
101+ }
102+ }
103+
104+ // 初始化所有现有节点的AI状态
105+ const initializeAllNodesAIStatus = ( ) => {
106+ // 递归遍历 pageSchema 的 children 来初始化所有节点的AI状态
107+ const traverseNodes = ( nodes : any [ ] ) => {
108+ if ( ! nodes ) return
109+ nodes . forEach ( ( node ) => {
110+ if ( node . id && ! pageState . nodesStatus [ node . id ] ?. aiStatus ) {
111+ initializeNodeAIStatus ( node )
112+ }
113+ if ( Array . isArray ( node . children ) && node . children . length ) {
114+ traverseNodes ( node . children )
115+ }
116+ } )
117+ }
118+
119+ if ( pageState . pageSchema ?. children ) {
120+ traverseNodes ( pageState . pageSchema . children )
121+ }
122+ }
123+
88124const handleTinyGridColumnsSlots = ( node : Node ) => {
89125 const columns = Array . isArray ( node . props ?. columns ) ? node . props . columns : [ ]
90126 for ( const columnItem of columns ) {
@@ -205,7 +241,7 @@ const resetCanvasState = async (state: Partial<PageState> = {}) => {
205241 generateNodesMap ( pageState . pageSchema . children , pageState . pageSchema )
206242
207243 // 初始化所有节点的AI状态
208- useAIChat ( ) . initializeAllNodesAIStatus ( )
244+ initializeAllNodesAIStatus ( )
209245 }
210246
211247 const diffPatch = jsonDiffPatchInstance . diff ( previousSchema , pageState . pageSchema )
@@ -239,7 +275,6 @@ const updatePageSchema = (newPageSchema: any) => {
239275 generateNodesMap ( newPageSchema . children , newPageSchema )
240276
241277 // 为新增的节点初始化AI状态(已存在的不覆盖)
242- const { initializeNodeAIStatus } = useAIChat ( )
243278 nodesMap . value . forEach ( ( { node } ) => {
244279 if ( node . id && ! pageState . nodesStatus [ node . id ] ?. aiStatus ) {
245280 initializeNodeAIStatus ( node )
@@ -428,7 +463,7 @@ const operationTypeMap = {
428463
429464 // 初始化新节点的AI状态
430465 if ( newNodeData . id ) {
431- useAIChat ( ) . initializeNodeAIStatus ( newNodeData )
466+ initializeNodeAIStatus ( newNodeData )
432467 }
433468
434469 // 6. 如果新节点有子节点,递归构建 nodeMap
@@ -437,7 +472,6 @@ const operationTypeMap = {
437472 generateNodesMap ( newNodeData . children , newNode )
438473
439474 // 递归初始化所有子节点的AI状态
440- const { initializeNodeAIStatus : initAIStatus } = useAIChat ( )
441475 const initChildrenAIStatus = ( children : Node [ ] ) => {
442476 children . forEach ( ( child ) => {
443477 if ( child . id ) {
0 commit comments