| 1 | import log from 'electron-log/main.js' |
| 2 | import type { ModelRuntimeConfig } from '../model/usage' |
| 3 | import type { DeepAgentStreamResult } from './types' |
| 4 | |
| 5 | export interface AgentSessionEntry { |
| 6 | agent: DeepAgentStreamResult | null |
| 7 | /** Per-page agents for concurrent generation (keyed by pageId). */ |
| 8 | pageAgents: Map<string, DeepAgentStreamResult> |
| 9 | projectDir: string |
| 10 | provider: string |
| 11 | model: string |
| 12 | baseUrl?: string |
| 13 | temperature?: number |
| 14 | modelRuntime?: ModelRuntimeConfig |
| 15 | } |
| 16 | |
| 17 | export interface AgentSessionConfig { |
| 18 | sessionId: string |
| 19 | provider: string |
| 20 | model: string |
| 21 | baseUrl?: string |
| 22 | temperature?: number |
| 23 | projectDir: string |
| 24 | modelRuntime?: ModelRuntimeConfig |
| 25 | } |
| 26 | |
| 27 | export class AgentManager { |
| 28 | private agents = new Map<string, AgentSessionEntry>() |
| 29 | |
| 30 | getSession(sessionId: string): AgentSessionEntry | undefined { |
| 31 | return this.agents.get(sessionId) |
| 32 | } |
| 33 | |
| 34 | setAgent(sessionId: string, agent: DeepAgentStreamResult): void { |
| 35 | const entry = this.agents.get(sessionId) |
| 36 | if (!entry) return |
| 37 | entry.agent = agent |
| 38 | } |
| 39 | |
| 40 | clearCachedAgent(sessionId: string): void { |
| 41 | const entry = this.agents.get(sessionId) |
| 42 | if (!entry) return |
| 43 | entry.agent = null |
| 44 | } |
| 45 | |
| 46 | /** Clear all cached agent instances without changing the job cancellation state. */ |
| 47 | clearCachedAgents(sessionId: string): void { |
| 48 | const entry = this.agents.get(sessionId) |
| 49 | if (!entry) return |
| 50 | entry.agent = null |
| 51 | entry.pageAgents.clear() |
| 52 | } |
| 53 | |
| 54 | /** Store a per-page agent for concurrent generation. Does not overwrite the main agent. */ |
| 55 | setPageAgent(sessionId: string, pageId: string, agent: DeepAgentStreamResult): void { |
| 56 | const entry = this.agents.get(sessionId) |
| 57 | if (!entry) return |
| 58 | entry.pageAgents.set(pageId, agent) |
| 59 | } |
| 60 | |
| 61 | removePageAgent(sessionId: string, pageId: string): void { |
| 62 | const entry = this.agents.get(sessionId) |
| 63 | if (!entry) return |
| 64 | entry.pageAgents.delete(pageId) |
| 65 | } |
| 66 | |
| 67 | ensureSession(config: AgentSessionConfig): AgentSessionEntry { |
| 68 | const existing = this.agents.get(config.sessionId) |
| 69 | if (existing) { |
| 70 | existing.provider = config.provider |
| 71 | existing.model = config.model |
| 72 | existing.baseUrl = config.baseUrl |
| 73 | existing.temperature = config.temperature |
| 74 | existing.projectDir = config.projectDir |
| 75 | existing.modelRuntime = config.modelRuntime |
| 76 | log.info('[agent] ensureSession hit existing', { |
| 77 | sessionId: config.sessionId, |
| 78 | provider: existing.provider, |
| 79 | model: existing.model, |
| 80 | baseUrl: existing.baseUrl || '', |
| 81 | temperature: existing.temperature ?? null, |
| 82 | projectDir: existing.projectDir |
| 83 | }) |
| 84 | return existing |
| 85 | } |
| 86 | |
| 87 | const model = config.model.trim() |
| 88 | if (!model) throw new Error('恢复会话失败:model 不能为空。') |
| 89 | const entry: AgentSessionEntry = { |
| 90 | agent: null, |
| 91 | pageAgents: new Map<string, DeepAgentStreamResult>(), |
| 92 | projectDir: config.projectDir, |
| 93 | provider: config.provider, |
| 94 | model, |
| 95 | baseUrl: config.baseUrl, |
| 96 | temperature: config.temperature, |
| 97 | modelRuntime: config.modelRuntime |
| 98 | } |
| 99 | |
| 100 | log.info('[agent] ensureSession create entry', { |
| 101 | sessionId: config.sessionId, |
| 102 | provider: entry.provider, |
| 103 | model, |
| 104 | baseUrl: entry.baseUrl || '', |
| 105 | temperature: entry.temperature ?? null, |
| 106 | projectDir: entry.projectDir |
| 107 | }) |
| 108 | |
| 109 | this.agents.set(config.sessionId, entry) |
| 110 | return entry |
| 111 | } |
| 112 | |
| 113 | removeSession(sessionId: string): void { |
| 114 | const entry = this.agents.get(sessionId) |
| 115 | if (entry) { |
| 116 | entry.agent = null |
| 117 | entry.pageAgents.clear() |
| 118 | } |
| 119 | this.agents.delete(sessionId) |
| 120 | log.info('[agent] removeSession', { sessionId }) |
| 121 | } |
| 122 | } |
| 123 |