mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(ui): dock repo and session tools in a desktop side panel
Add a docked desktop tool panel with an icon rail, driven by the shared panel URL param, and keep the mobile dialogs and drawers. Extract dialog bodies into reusable content components, rename the walkthrough dialog to a sheet, and make walkthrough generation asynchronous with client polling.
This commit is contained in:
1 parent
bdbcc60716
commit
485ab940aa
49 files changed
+2631
-1753
No files matched your search
@@ -23,8 +23,8 @@ export function createChangeWalkthroughRoutes(service: ChangeWalkthroughService)
|
||||
}
|
||||
|
||||
try {
|
||||
const { walkthrough, created } = await service.generate(c.req.param('sessionId'), parsed.data)
|
||||
return c.json({ walkthrough }, created ? 201 : 200)
|
||||
const state = await service.startGeneration(c.req.param('sessionId'), parsed.data)
|
||||
return c.json(state, state.generating ? 202 : 200)
|
||||
} catch (error) {
|
||||
return handleServiceError(c, error, 'Failed to generate change walkthrough', ServiceError)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
type ChangeWalkthrough,
|
||||
type ChangeWalkthroughState,
|
||||
type GenerateChangeWalkthroughRequest,
|
||||
type WalkthroughGenerationError,
|
||||
type WalkthroughHunk,
|
||||
type WalkthroughOmittedFile,
|
||||
type WalkthroughStop,
|
||||
@@ -212,8 +213,26 @@ export function parseWalkthroughResponse(text: string, hunks: WalkthroughHunk[])
|
||||
}
|
||||
}
|
||||
|
||||
interface InFlightGeneration {
|
||||
modelStarted: Promise<void>
|
||||
result: Promise<{ walkthrough: ChangeWalkthrough; created: boolean }>
|
||||
}
|
||||
|
||||
function toGenerationError(error: unknown): WalkthroughGenerationError {
|
||||
if (error instanceof ServiceError) {
|
||||
return {
|
||||
message: error.message,
|
||||
...(error.code ? { code: error.code } : {}),
|
||||
...(error.details !== undefined ? { details: error.details } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
return { message: getErrorMessage(error) || 'Failed to generate the change walkthrough' }
|
||||
}
|
||||
|
||||
export class ChangeWalkthroughService {
|
||||
private readonly inFlight = new Map<string, Promise<{ walkthrough: ChangeWalkthrough; created: boolean }>>()
|
||||
private readonly inFlight = new Map<string, InFlightGeneration>()
|
||||
private readonly failures = new Map<string, WalkthroughGenerationError>()
|
||||
private readonly deletedDuringGeneration = new Set<string>()
|
||||
private readonly timeoutMs: number
|
||||
|
||||
@@ -241,21 +260,19 @@ export class ChangeWalkthroughService {
|
||||
walkthrough,
|
||||
currentDiffHash,
|
||||
stale: walkthrough !== null && currentDiffHash !== null && walkthrough.diffHash !== currentDiffHash,
|
||||
generating: this.inFlight.has(sessionId),
|
||||
error: this.failures.get(sessionId) ?? null,
|
||||
}
|
||||
}
|
||||
|
||||
generate(sessionId: string, request: GenerateChangeWalkthroughRequest): Promise<{ walkthrough: ChangeWalkthrough; created: boolean }> {
|
||||
const existing = this.inFlight.get(sessionId)
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
return this.begin(sessionId, request).result
|
||||
}
|
||||
|
||||
const pending = this.runGenerate(sessionId, request).finally(() => {
|
||||
this.inFlight.delete(sessionId)
|
||||
this.deletedDuringGeneration.delete(sessionId)
|
||||
})
|
||||
this.inFlight.set(sessionId, pending)
|
||||
return pending
|
||||
async startGeneration(sessionId: string, request: GenerateChangeWalkthroughRequest): Promise<ChangeWalkthroughState> {
|
||||
const entry = this.begin(sessionId, request)
|
||||
await Promise.race([entry.modelStarted, entry.result])
|
||||
return this.getState(sessionId)
|
||||
}
|
||||
|
||||
/** Removes the stored walkthrough of a deleted session, including one still being generated. */
|
||||
@@ -268,12 +285,51 @@ export class ChangeWalkthroughService {
|
||||
if (this.inFlight.has(sessionID)) {
|
||||
this.deletedDuringGeneration.add(sessionID)
|
||||
}
|
||||
this.failures.delete(sessionID)
|
||||
deleteChangeWalkthrough(this.db, sessionID)
|
||||
}
|
||||
|
||||
private begin(sessionId: string, request: GenerateChangeWalkthroughRequest): InFlightGeneration {
|
||||
const existing = this.inFlight.get(sessionId)
|
||||
if (existing) {
|
||||
return existing
|
||||
}
|
||||
|
||||
this.failures.delete(sessionId)
|
||||
|
||||
let resolveModelStarted: () => void = () => {}
|
||||
const modelStarted = new Promise<void>((resolve) => {
|
||||
resolveModelStarted = resolve
|
||||
})
|
||||
let modelCalled = false
|
||||
const markModelStarted = () => {
|
||||
modelCalled = true
|
||||
resolveModelStarted()
|
||||
}
|
||||
|
||||
const result = this.runGenerate(sessionId, request, markModelStarted)
|
||||
.catch((error) => {
|
||||
if (modelCalled) {
|
||||
this.failures.set(sessionId, toGenerationError(error))
|
||||
}
|
||||
throw error
|
||||
})
|
||||
.finally(() => {
|
||||
this.inFlight.delete(sessionId)
|
||||
this.deletedDuringGeneration.delete(sessionId)
|
||||
})
|
||||
|
||||
result.catch(() => {})
|
||||
|
||||
const entry: InFlightGeneration = { modelStarted, result }
|
||||
this.inFlight.set(sessionId, entry)
|
||||
return entry
|
||||
}
|
||||
|
||||
private async runGenerate(
|
||||
sessionId: string,
|
||||
request: GenerateChangeWalkthroughRequest,
|
||||
onModelStart: () => void,
|
||||
): Promise<{ walkthrough: ChangeWalkthrough; created: boolean }> {
|
||||
const session = await this.readSession(sessionId)
|
||||
|
||||
@@ -314,6 +370,8 @@ export class ChangeWalkthroughService {
|
||||
|
||||
const prompt = buildWalkthroughPrompt({ title, hunks })
|
||||
|
||||
onModelStart()
|
||||
|
||||
let responseText: string
|
||||
try {
|
||||
responseText = await generateTextWithTimeout(this.openCodeClient, { prompt }, this.timeoutMs)
|
||||
|
||||
@@ -28,7 +28,7 @@ interface FakeSession {
|
||||
|
||||
function createFakeClient(sessions: Record<string, FakeSession>) {
|
||||
const generateCalls: string[] = []
|
||||
let reply = MODEL_REPLY
|
||||
let generateImpl: () => Promise<string> = async () => MODEL_REPLY
|
||||
|
||||
const client = {
|
||||
api: {
|
||||
@@ -62,7 +62,7 @@ function createFakeClient(sessions: Record<string, FakeSession>) {
|
||||
generate: {
|
||||
text: vi.fn(async (input: { prompt: string }) => {
|
||||
generateCalls.push(input.prompt)
|
||||
return { text: reply }
|
||||
return { text: await generateImpl() }
|
||||
}),
|
||||
},
|
||||
},
|
||||
@@ -72,8 +72,8 @@ function createFakeClient(sessions: Record<string, FakeSession>) {
|
||||
return {
|
||||
client,
|
||||
generateCalls,
|
||||
setReply: (next: string) => {
|
||||
reply = next
|
||||
setGenerateImpl: (impl: () => Promise<string>) => {
|
||||
generateImpl = impl
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -102,10 +102,18 @@ describe('change walkthrough routes', () => {
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const body = (await res.json()) as { walkthrough: unknown; currentDiffHash: string | null; stale: boolean }
|
||||
const body = (await res.json()) as {
|
||||
walkthrough: unknown
|
||||
currentDiffHash: string | null
|
||||
stale: boolean
|
||||
generating: boolean
|
||||
error: unknown
|
||||
}
|
||||
expect(body.walkthrough).toBeNull()
|
||||
expect(body.stale).toBe(false)
|
||||
expect(body.currentDiffHash).toEqual(expect.any(String))
|
||||
expect(body.generating).toBe(false)
|
||||
expect(body.error).toBeNull()
|
||||
})
|
||||
|
||||
it('GET returns 404 for a missing session', async () => {
|
||||
@@ -115,30 +123,59 @@ describe('change walkthrough routes', () => {
|
||||
await expect(res.json()).resolves.toMatchObject({ error: 'Session not found' })
|
||||
})
|
||||
|
||||
it('POST creates a walkthrough and GET reads it back', async () => {
|
||||
it('POST returns 202 while generating, then GET reads the walkthrough back', async () => {
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const postRes = await app.request(`/change-walkthroughs/${SESSION_ID}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({}),
|
||||
})
|
||||
|
||||
expect(postRes.status).toBe(201)
|
||||
const created = (await postRes.json()) as { walkthrough: { summary: string; sessionId: string } }
|
||||
expect(created.walkthrough.summary).toBe('A summary')
|
||||
expect(postRes.status).toBe(202)
|
||||
const body = (await postRes.json()) as { generating: boolean; walkthrough: unknown }
|
||||
expect(body.generating).toBe(true)
|
||||
expect(body.walkthrough).toBeNull()
|
||||
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { walkthrough: { summary: string } }
|
||||
expect(state.walkthrough.summary).toBe('A summary')
|
||||
resolveGenerate(MODEL_REPLY)
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { walkthrough: { summary: string } | null; generating: boolean }
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.walkthrough?.summary).toBe('A summary')
|
||||
})
|
||||
})
|
||||
|
||||
it('POST accepts an empty body', async () => {
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'POST' })
|
||||
|
||||
expect(res.status).toBe(201)
|
||||
expect(res.status).toBe(202)
|
||||
|
||||
resolveGenerate(MODEL_REPLY)
|
||||
await vi.waitFor(async () => {
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { walkthrough: unknown }
|
||||
expect(state.walkthrough).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
it('POST returns 200 without a second model call when changes are unchanged', async () => {
|
||||
it('POST returns 200 with the stored walkthrough without a second model call', async () => {
|
||||
await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'POST' })
|
||||
await vi.waitFor(async () => {
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { walkthrough: unknown }
|
||||
expect(state.walkthrough).not.toBeNull()
|
||||
})
|
||||
|
||||
const second = await app.request(`/change-walkthroughs/${SESSION_ID}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -146,19 +183,35 @@ describe('change walkthrough routes', () => {
|
||||
})
|
||||
|
||||
expect(second.status).toBe(200)
|
||||
const body = (await second.json()) as { walkthrough: { summary: string }; generating: boolean }
|
||||
expect(body.walkthrough.summary).toBe('A summary')
|
||||
expect(body.generating).toBe(false)
|
||||
expect(fake.generateCalls).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('POST regenerates when asked', async () => {
|
||||
await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'POST' })
|
||||
await vi.waitFor(async () => {
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { walkthrough: unknown }
|
||||
expect(state.walkthrough).not.toBeNull()
|
||||
})
|
||||
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ regenerate: true }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(201)
|
||||
expect(fake.generateCalls).toHaveLength(2)
|
||||
expect(res.status).toBe(202)
|
||||
|
||||
resolveGenerate(MODEL_REPLY)
|
||||
await vi.waitFor(() => expect(fake.generateCalls).toHaveLength(2))
|
||||
})
|
||||
|
||||
it('POST rejects an invalid body with 400', async () => {
|
||||
@@ -192,13 +245,23 @@ describe('change walkthrough routes', () => {
|
||||
await expect(res.json()).resolves.toMatchObject({ code: 'WALKTHROUGH_NO_TEXT_CHANGES' })
|
||||
})
|
||||
|
||||
it('POST returns 502 with a code when the model response is unparseable', async () => {
|
||||
fake.setReply('not json')
|
||||
it('POST records an unparseable model reply that GET surfaces', async () => {
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'POST' })
|
||||
expect(res.status).toBe(202)
|
||||
|
||||
expect(res.status).toBe(502)
|
||||
await expect(res.json()).resolves.toMatchObject({ code: 'WALKTHROUGH_UNPARSEABLE' })
|
||||
resolveGenerate('not json')
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { generating: boolean; error: { code?: string } | null }
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.error?.code).toBe('WALKTHROUGH_UNPARSEABLE')
|
||||
})
|
||||
})
|
||||
|
||||
it('POST returns 404 for a missing session', async () => {
|
||||
|
||||
@@ -383,6 +383,8 @@ describe('ChangeWalkthroughService', () => {
|
||||
expect(state.walkthrough).toBeNull()
|
||||
expect(state.stale).toBe(false)
|
||||
expect(state.currentDiffHash).toBe(computeChangesHash(threeHunks))
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.error).toBeNull()
|
||||
})
|
||||
|
||||
it('reports stale when the changes differ from the stored hash', async () => {
|
||||
@@ -526,6 +528,79 @@ describe('ChangeWalkthroughService', () => {
|
||||
await expect(service.generate('ses_missing', {})).rejects.toMatchObject({ status: 404 })
|
||||
})
|
||||
|
||||
describe('startGeneration', () => {
|
||||
const coveringReply = modelReply([{ title: 'A', explanation: 'x', hunkIds: ['f0h0', 'f0h1', 'f1h0'] }])
|
||||
|
||||
it('reports generating while the model call is pending, then the stored walkthrough', async () => {
|
||||
let resolveGenerate: (text: string) => void = () => {}
|
||||
fake.setGenerateImpl(() => new Promise<string>((resolve) => {
|
||||
resolveGenerate = resolve
|
||||
}))
|
||||
|
||||
const state = await service.startGeneration(SESSION_ID, {})
|
||||
expect(state.generating).toBe(true)
|
||||
expect(state.walkthrough).toBeNull()
|
||||
expect(state.error).toBeNull()
|
||||
|
||||
resolveGenerate(coveringReply)
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const settled = await service.getState(SESSION_ID)
|
||||
expect(settled.generating).toBe(false)
|
||||
expect(settled.walkthrough).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
it('rejects with 409 when the session has no changes', async () => {
|
||||
sessions[SESSION_ID]!.changes = []
|
||||
|
||||
await expect(service.startGeneration(SESSION_ID, {})).rejects.toMatchObject({
|
||||
status: 409,
|
||||
code: 'WALKTHROUGH_NO_CHANGES',
|
||||
})
|
||||
sessions[SESSION_ID]!.changes = threeHunks
|
||||
expect((await service.getState(SESSION_ID)).error).toBeNull()
|
||||
})
|
||||
|
||||
it('records an unparseable model failure on the state', async () => {
|
||||
fake.setGenerateImpl(async () => 'not json')
|
||||
|
||||
await service.startGeneration(SESSION_ID, {})
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const settled = await service.getState(SESSION_ID)
|
||||
expect(settled.generating).toBe(false)
|
||||
expect(settled.error).toMatchObject({ code: 'WALKTHROUGH_UNPARSEABLE' })
|
||||
})
|
||||
})
|
||||
|
||||
it('clears a previous error when a new generation starts', async () => {
|
||||
fake.setGenerateImpl(async () => 'not json')
|
||||
await service.startGeneration(SESSION_ID, {})
|
||||
await vi.waitFor(async () => {
|
||||
expect((await service.getState(SESSION_ID)).error).not.toBeNull()
|
||||
})
|
||||
|
||||
fake.setGenerateImpl(() => new Promise<string>(() => {}))
|
||||
const state = await service.startGeneration(SESSION_ID, { regenerate: true })
|
||||
|
||||
expect(state.generating).toBe(true)
|
||||
expect(state.error).toBeNull()
|
||||
})
|
||||
|
||||
it('clears the recorded error when the session is deleted', async () => {
|
||||
fake.setGenerateImpl(async () => 'not json')
|
||||
await service.startGeneration(SESSION_ID, {})
|
||||
await vi.waitFor(async () => {
|
||||
expect((await service.getState(SESSION_ID)).error).not.toBeNull()
|
||||
})
|
||||
|
||||
service.handleEvent(sessionEvent('session.deleted', SESSION_ID))
|
||||
|
||||
expect((await service.getState(SESSION_ID)).error).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('handleEvent', () => {
|
||||
const coveringReply = modelReply([{ title: 'A', explanation: 'x', hunkIds: ['f0h0', 'f0h1', 'f1h0'] }])
|
||||
|
||||
|
||||
Reference in new issue
Block a user