mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-29 21:33:54 +00:00
- Rename packages/opencode → packages/tfcode (directory only) - Rename bin/opencode → bin/tfcode (CLI binary) - Rename .opencode → .tfcode (config directory) - Update package.json name and bin field - Update config directory path references (.tfcode) - Keep internal code references as 'opencode' for easy upstream sync - Keep @opencode-ai/* workspace package names This minimal branding approach allows clean merges from upstream opencode repository while providing tfcode branding for users.
117 lines
2.8 KiB
TypeScript
117 lines
2.8 KiB
TypeScript
import { RequestError, type McpServer } from "@agentclientprotocol/sdk"
|
|
import type { ACPSessionState } from "./types"
|
|
import { Log } from "@/util/log"
|
|
import type { OpencodeClient } from "@opencode-ai/sdk/v2"
|
|
|
|
const log = Log.create({ service: "acp-session-manager" })
|
|
|
|
export class ACPSessionManager {
|
|
private sessions = new Map<string, ACPSessionState>()
|
|
private sdk: OpencodeClient
|
|
|
|
constructor(sdk: OpencodeClient) {
|
|
this.sdk = sdk
|
|
}
|
|
|
|
tryGet(sessionId: string): ACPSessionState | undefined {
|
|
return this.sessions.get(sessionId)
|
|
}
|
|
|
|
async create(cwd: string, mcpServers: McpServer[], model?: ACPSessionState["model"]): Promise<ACPSessionState> {
|
|
const session = await this.sdk.session
|
|
.create(
|
|
{
|
|
directory: cwd,
|
|
},
|
|
{ throwOnError: true },
|
|
)
|
|
.then((x) => x.data!)
|
|
|
|
const sessionId = session.id
|
|
const resolvedModel = model
|
|
|
|
const state: ACPSessionState = {
|
|
id: sessionId,
|
|
cwd,
|
|
mcpServers,
|
|
createdAt: new Date(),
|
|
model: resolvedModel,
|
|
}
|
|
log.info("creating_session", { state })
|
|
|
|
this.sessions.set(sessionId, state)
|
|
return state
|
|
}
|
|
|
|
async load(
|
|
sessionId: string,
|
|
cwd: string,
|
|
mcpServers: McpServer[],
|
|
model?: ACPSessionState["model"],
|
|
): Promise<ACPSessionState> {
|
|
const session = await this.sdk.session
|
|
.get(
|
|
{
|
|
sessionID: sessionId,
|
|
directory: cwd,
|
|
},
|
|
{ throwOnError: true },
|
|
)
|
|
.then((x) => x.data!)
|
|
|
|
const resolvedModel = model
|
|
|
|
const state: ACPSessionState = {
|
|
id: sessionId,
|
|
cwd,
|
|
mcpServers,
|
|
createdAt: new Date(session.time.created),
|
|
model: resolvedModel,
|
|
}
|
|
log.info("loading_session", { state })
|
|
|
|
this.sessions.set(sessionId, state)
|
|
return state
|
|
}
|
|
|
|
get(sessionId: string): ACPSessionState {
|
|
const session = this.sessions.get(sessionId)
|
|
if (!session) {
|
|
log.error("session not found", { sessionId })
|
|
throw RequestError.invalidParams(JSON.stringify({ error: `Session not found: ${sessionId}` }))
|
|
}
|
|
return session
|
|
}
|
|
|
|
getModel(sessionId: string) {
|
|
const session = this.get(sessionId)
|
|
return session.model
|
|
}
|
|
|
|
setModel(sessionId: string, model: ACPSessionState["model"]) {
|
|
const session = this.get(sessionId)
|
|
session.model = model
|
|
this.sessions.set(sessionId, session)
|
|
return session
|
|
}
|
|
|
|
getVariant(sessionId: string) {
|
|
const session = this.get(sessionId)
|
|
return session.variant
|
|
}
|
|
|
|
setVariant(sessionId: string, variant?: string) {
|
|
const session = this.get(sessionId)
|
|
session.variant = variant
|
|
this.sessions.set(sessionId, session)
|
|
return session
|
|
}
|
|
|
|
setMode(sessionId: string, modeId: string) {
|
|
const session = this.get(sessionId)
|
|
session.modeId = modeId
|
|
this.sessions.set(sessionId, session)
|
|
return session
|
|
}
|
|
}
|