@@ -52,6 +52,7 @@ export interface PagesActions {
5252 createAndOpenCrosstabChat : ( title : string , folderId ?: string , lineage ?: PageLineage ) => string
5353 createAndOpenObjectChat : ( title : string , folderId ?: string , lineage ?: PageLineage ) => string
5454 createAndOpenSettingsPage : ( defaultActiveTab ?: string ) => string
55+ createChatWithInitialMessage : ( title : string , initialMessage : string , folderId ?: string , sourcePageId ?: string ) => string
5556
5657 // 复杂页面创建功能
5758 createChatFromCell : ( params : {
@@ -600,6 +601,78 @@ export const usePagesStore = create<PagesState & PagesActions>()(
600601 }
601602 } ,
602603
604+ createChatWithInitialMessage : ( title , initialMessage , folderId , sourcePageId ) => {
605+ try {
606+ const timestamp = Date . now ( )
607+
608+ // 如果没有指定order,获取同文件夹下的最小order值
609+ const siblingFolders = get ( ) . folders . filter ( f => f . parentId === folderId )
610+ const siblingPages = get ( ) . pages . filter ( p => p . folderId === folderId && p . type !== 'settings' )
611+
612+ // 获取所有同级项的最小order值
613+ const allOrders = [
614+ ...siblingFolders . map ( f => f . order || 0 ) ,
615+ ...siblingPages . map ( p => p . order || 0 )
616+ ]
617+
618+ const minOrder = allOrders . length > 0
619+ ? Math . min ( ...allOrders )
620+ : 1000
621+
622+ // 创建用户消息
623+ const userMessage = {
624+ id : uuidv4 ( ) ,
625+ role : 'user' as const ,
626+ content : initialMessage ,
627+ timestamp : timestamp
628+ }
629+
630+ const newPage : Page = {
631+ id : uuidv4 ( ) ,
632+ title,
633+ type : 'regular' ,
634+ createdAt : timestamp ,
635+ updatedAt : timestamp ,
636+ order : minOrder - 1000 , // 新页面添加到最前面
637+ folderId,
638+ messages : [ userMessage ] ,
639+ messageMap : { [ userMessage . id ] : userMessage } ,
640+ currentPath : [ userMessage . id ] ,
641+ rootMessageId : undefined ,
642+ ...( sourcePageId && {
643+ lineage : {
644+ source : 'other' as const ,
645+ sourcePageId,
646+ sourceContext : {
647+ customContext : {
648+ action : 'create_chat_with_text' ,
649+ originalText : initialMessage
650+ }
651+ } ,
652+ generatedPageIds : [ ] ,
653+ generatedAt : timestamp ,
654+ description : `从消息文本创建新对话`
655+ }
656+ } )
657+ }
658+
659+ set ( ( state ) => {
660+ state . pages . push ( newPage )
661+ } )
662+
663+ // 同时保存到 IndexedDB
664+ pagesStorage . savePage ( newPage )
665+
666+ const { setSelectedNode } = useUIStore . getState ( )
667+ setSelectedNode ( newPage . id , 'chat' )
668+
669+ return newPage . id
670+ } catch ( error ) {
671+ handleStoreError ( 'pagesStore' , 'createChatWithInitialMessage' , error )
672+ throw error
673+ }
674+ } ,
675+
603676 // 复杂页面创建功能
604677 createChatFromCell : ( params ) => {
605678 try {
0 commit comments