refactor: data flow inversion, IPC contract fixes, and frontend adaptation

Data flow inversion:
- Add getHistoryForAgent() in conversations.ts — Agent now reads
  history directly from SQLite instead of receiving it from frontend
- Remove historyMessages parameter from agent:runStream IPC chain,
  add maxHistoryRounds for configurable context window
- Frontend (useAIChat.ts) pre-creates conversation before Agent call,
  removes history message collection logic

IPC fixes:
- Restore llm:chat and llm:chatStream handlers using pi-ai's
  completeSimple/streamSimple (P0-1: SQLLab compatibility)
- Send agent:complete with aborted flag in all abort paths to prevent
  hanging Promises in renderer (P0-2)
- Fix createConversation parameter order to match preload/data layer
  contract: (sessionId, title?) instead of (title, sessionId?) (P1)

Preload/frontend:
- Update preload API signatures and type declarations
- Use shared types for TokenUsage and AgentRuntimeStatus
This commit is contained in:
digua
2026-02-26 21:06:27 +08:00
parent 1b059a52cc
commit 07fffb0112
6 changed files with 219 additions and 209 deletions

View File

@@ -1,5 +1,6 @@
import { ElectronAPI } from '@electron-toolkit/preload'
import type { AnalysisSession, MessageType, ImportProgress, ExportProgress } from '../../src/types/base'
import type { TokenUsage, AgentRuntimeStatus } from '../shared/types'
import type {
MemberActivity,
MemberNameHistory,
@@ -582,29 +583,7 @@ interface RAGConfig {
topK?: number
}
// Token 使用量类型
interface TokenUsage {
promptTokens: number
completionTokens: number
totalTokens: number
}
interface AgentRuntimeStatus {
phase: 'preparing' | 'thinking' | 'tool_running' | 'responding' | 'completed' | 'aborted' | 'error'
round: number
toolsUsed: number
currentTool?: string
contextTokens: number
contextWindow: number
contextUsage: number
totalUsage: TokenUsage
nodeCount?: number
tagCount?: number
segmentSize?: number
checkoutCount?: number
activeAnchorNodeId?: string | null
updatedAt: number
}
// TokenUsage & AgentRuntimeStatus — imported from electron/shared/types.ts
// Agent 相关类型
interface AgentStreamChunk {
@@ -659,10 +638,10 @@ interface AgentApi {
userMessage: string,
context: ToolContext,
onChunk?: (chunk: AgentStreamChunk) => void,
historyMessages?: Array<{ role: 'user' | 'assistant'; content: string }>,
chatType?: 'group' | 'private',
promptConfig?: PromptConfig,
locale?: string
locale?: string,
maxHistoryRounds?: number
) => { requestId: string; promise: Promise<{ success: boolean; result?: AgentResult; error?: string }> }
abort: (requestId: string) => Promise<{ success: boolean; error?: string }>
}