Files
goodbuddy/src/agent-daemon/agent-acp-connection.ts
T
mesalogo 56ebd40bd8 feat: unify execution and add workspace file imports
Remove Ask/Execute modes and general product tool approvals so requests use the selected runtime, enabled capabilities, and execution account permissions. Migrate retired application metadata while preserving user content.

Retire the Results page while retaining conversation images and stored artifacts. Add local and remote workspace file imports, timestamps and sorting, improve image discovery and saving, and refine workspace navigation and supervision responsiveness. Include the accompanying web-search changes, tests, probes, and product documentation.

Remote imports require an updated Agent. Existing artifacts are not automatically copied into project directories. Validation evidence and outstanding failures remain documented in feature progress files; no full test suite or real-model calls were rerun for this commit.

Release note: Run requests without switching work modes, import files into a selected local or remote workspace folder, sort workspace files by date, and keep using conversation images after removal of the Results page.
2026-10-04 20:02:59 +08:00

109 lines
4.0 KiB
TypeScript

import {
ClientSideConnection,
ndJsonStream,
type Client,
type InitializeRequest,
type InitializeResponse
} from '@agentclientprotocol/sdk'
import type { RuntimeAcpProcessOwner } from './runtime-acp-backend'
/** One decoder and handshake per native process, with Session-scoped clients. */
export class AgentAcpConnection {
readonly connection: ClientSideConnection
readonly exited: Promise<never>
private initialization?: Promise<InitializeResponse>
private readonly sessions = new Map<string, Client>()
private readonly unsubscribeOutput: () => void
private readonly unsubscribeExit: () => void
private closed = false
private closeInput?: () => void
private bridgeOrigin?: string
constructor(
process: RuntimeAcpProcessOwner,
fallback?: () => Client,
createConnection?: (client: () => Client, stream: ReturnType<typeof ndJsonStream>) => ClientSideConnection
) {
let controller: ReadableStreamDefaultController<Uint8Array> | undefined
let rejectExit!: (error: Error) => void
this.exited = new Promise<never>((_resolve, reject) => { rejectExit = reject })
void this.exited.catch(() => undefined)
const input = new ReadableStream<Uint8Array>({
start(value) { controller = value }
})
this.closeInput = () => {
controller?.close()
controller = undefined
}
this.unsubscribeOutput = process.subscribeOutput((output) => {
if (!this.closed && output.stream === 'stdout' && output.data.byteLength > 0) {
controller?.enqueue(output.data.slice())
}
}) ?? (() => undefined)
this.unsubscribeExit = process.subscribeExit?.(() => {
if (this.closed) return
const error = new Error('ACP Runtime process exited')
controller?.error(error)
controller = undefined
rejectExit(error)
}) ?? (() => undefined)
const output = new WritableStream<Uint8Array>({
write: async (data) => { await process.writeStdin(data) }
})
const client = (): Client => ({
sessionUpdate: async (notification) => {
await (this.sessions.get(notification.sessionId) ?? fallback?.())
?.sessionUpdate(notification)
},
requestPermission: async (request) =>
await (this.sessions.get(request.sessionId) ?? fallback?.())
?.requestPermission(request) ?? { outcome: { outcome: 'cancelled' } },
extMethod: async () => ({}),
extNotification: async (method, params) => {
if (
method === 'goodbuddy/modelBridgeReady' &&
typeof params.origin === 'string' &&
/^http:\/\/127\.0\.0\.1:\d+\/[A-Za-z0-9_-]{43}$/u.test(params.origin)
) this.bridgeOrigin = params.origin
}
})
const stream = ndJsonStream(output, input)
this.connection = createConnection?.(client, stream) ?? new ClientSideConnection(client, stream)
}
initialize(request: InitializeRequest): Promise<InitializeResponse> {
return this.initialization ??= this.connection.initialize(request)
}
register(sessionId: string, client: Client): () => void {
if (this.sessions.has(sessionId)) throw new Error('ACP Session already has an owner')
this.sessions.set(sessionId, client)
return () => {
if (this.sessions.get(sessionId) === client) this.sessions.delete(sessionId)
}
}
async setModelRoute(
sessionId: string,
operationId: string,
socketPath?: string,
imageToolName?: string
): Promise<void> {
if (!this.bridgeOrigin) throw new Error('Shared model bridge is not ready')
const response = await fetch(`${this.bridgeOrigin}/session`, {
method: 'POST', headers: { 'content-type': 'application/json' },
body: JSON.stringify({ sessionId, operationId, ...(socketPath ? { socketPath, imageToolName } : { release: true }) }),
signal: AbortSignal.timeout(10_000)
})
if (!response.ok) throw new Error(`Shared model bridge route failed (${response.status})`)
}
dispose(): void {
if (this.closed) return
this.closed = true
this.sessions.clear()
this.unsubscribeOutput()
this.unsubscribeExit()
this.closeInput?.()
}
}