refactor: remove assistant auto-launch and simplify redirect flow (#282)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-06-20 12:28:32 -04:00
1 parent 634dcd9af9
commit 5b85abd627
6 files changed
+91 -465

No files matched your search

@@ -1,197 +0,0 @@
import { renderHook, act } from '@testing-library/react'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { useAssistantSessionLauncher } from './useAssistantSessionLauncher'
import { OpenCodeClient } from '@/api/opencode'
const mocks = vi.hoisted(() => ({
listSessions: vi.fn(),
listSessionsPage: vi.fn(),
createSession: vi.fn(),
sendPromptAsync: vi.fn(),
}))
vi.mock('@/api/opencode', () => ({
OpenCodeClient: vi.fn(() => ({
listSessions: mocks.listSessions,
listSessionsPage: mocks.listSessionsPage,
createSession: mocks.createSession,
sendPromptAsync: mocks.sendPromptAsync,
})),
}))
beforeEach(() => {
mocks.sendPromptAsync.mockResolvedValue(undefined)
})
describe('useAssistantSessionLauncher', () => {
beforeEach(() => {
vi.clearAllMocks()
localStorage.clear()
})
it('opens the latest root session in the assistant directory', async () => {
mocks.listSessionsPage.mockResolvedValue({
items: [
{ id: 'older', directory: '/assistant', time: { updated: 10 } },
{ id: 'newest-child', parentID: 'newest', directory: '/assistant', time: { updated: 40 } },
{ id: 'different-directory', directory: '/other', time: { updated: 50 } },
{ id: 'newest', directory: '/assistant', time: { updated: 30 } },
],
})
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
directory: '/assistant',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(OpenCodeClient).toHaveBeenCalledWith('http://localhost:5551', '/assistant')
expect(mocks.listSessionsPage).toHaveBeenCalledWith({ limit: 25, order: 'desc' })
expect(mocks.listSessions).not.toHaveBeenCalled()
expect(onNavigate).toHaveBeenCalledWith('newest')
expect(mocks.createSession).not.toHaveBeenCalled()
expect(mocks.sendPromptAsync).not.toHaveBeenCalled()
})
it('paginates assistant sessions instead of making an unbounded session list request', async () => {
mocks.listSessionsPage
.mockResolvedValueOnce({
items: [
{ id: 'newest-child', parentID: 'newest', directory: '/assistant', time: { updated: 40 } },
],
nextCursor: 'next-page',
})
.mockResolvedValueOnce({
items: [
{ id: 'newest', directory: '/assistant', time: { updated: 30 } },
],
})
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
directory: '/assistant',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(mocks.listSessionsPage).toHaveBeenNthCalledWith(1, { limit: 25, order: 'desc' })
expect(mocks.listSessionsPage).toHaveBeenNthCalledWith(2, { cursor: 'next-page' })
expect(mocks.listSessions).not.toHaveBeenCalled()
expect(onNavigate).toHaveBeenCalledWith('newest')
})
it('creates a session when the assistant directory has no root sessions', async () => {
mocks.listSessionsPage.mockResolvedValue({
items: [
{ id: 'other', directory: '/other', time: { updated: 50 } },
],
})
mocks.createSession.mockResolvedValue({ id: 'created' })
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
directory: '/assistant',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(mocks.createSession).toHaveBeenCalledWith({ title: 'Assistant' })
expect(mocks.sendPromptAsync).toHaveBeenCalledWith('created', {
parts: [
expect.objectContaining({
type: 'text',
text: expect.stringContaining('Welcome to OpenCode Manager!'),
}),
],
})
expect(onNavigate).toHaveBeenCalledWith('created')
const promptCall = mocks.sendPromptAsync.mock.calls[0]
const promptText = promptCall[1].parts[0].text as string
expect(promptText).toContain('.opencode/agents/assistant.md')
expect(promptText).toContain('AGENTS.md')
expect(promptText).toContain('.opencode/skills/')
expect(promptText).toContain('directory')
expect(promptText).toContain('durable preferences')
expect(promptText).toContain('self-editing rules')
expect(promptText).not.toContain('v file')
expect(promptText).not.toMatch(/AGENTS\.md contains workspace-level instructions, durable preferences, and self-editing rules/)
})
it('navigates after creating a session without waiting for the welcome prompt to complete', async () => {
mocks.listSessionsPage.mockResolvedValue({ items: [] })
let resolvePrompt: () => void
const promptPromise = new Promise<void>((resolve) => {
resolvePrompt = resolve
})
mocks.createSession.mockResolvedValue({ id: 'created' })
mocks.sendPromptAsync.mockImplementation(() => promptPromise)
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
directory: '/assistant',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(onNavigate).toHaveBeenCalledWith('created')
expect(mocks.sendPromptAsync).toHaveBeenCalledWith('created', {
parts: [
expect.objectContaining({
type: 'text',
text: expect.stringContaining('Welcome to OpenCode Manager!'),
}),
],
})
resolvePrompt!()
})
it('navigates even when welcome prompt fails', async () => {
mocks.listSessionsPage.mockResolvedValue({ items: [] })
mocks.createSession.mockResolvedValue({ id: 'created' })
mocks.sendPromptAsync.mockRejectedValueOnce(new Error('provider unavailable'))
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
directory: '/assistant',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(onNavigate).toHaveBeenCalledWith('created')
expect(mocks.sendPromptAsync).toHaveBeenCalled()
})
it('rejects when the assistant directory is unavailable', async () => {
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
opcodeUrl: 'http://localhost:5551',
onNavigate,
}))
await act(async () => {
await expect(result.current.openAssistant()).rejects.toThrow('Assistant workspace directory is unavailable')
})
expect(OpenCodeClient).not.toHaveBeenCalled()
expect(mocks.listSessionsPage).not.toHaveBeenCalled()
expect(mocks.createSession).not.toHaveBeenCalled()
expect(onNavigate).not.toHaveBeenCalled()
})
})
@@ -1,93 +0,0 @@
import { useCallback } from 'react'
import { OpenCodeClient } from '@/api/opencode'
import type { components } from '@/api/opencode-types'
interface UseAssistantSessionLauncherOptions {
opcodeUrl: string
directory?: string
onNavigate: (sessionId: string) => void
}
type OpenCodeSession = components['schemas']['Session']
const ASSISTANT_SESSION_LOOKUP_PAGE_SIZE = 25
function isAssistantRootSession(session: OpenCodeSession, assistantDirectory: string): boolean {
return !session.parentID && session.directory === assistantDirectory
}
function findNewestRootAssistantSession(sessions: OpenCodeSession[], assistantDirectory: string): OpenCodeSession | undefined {
return sessions
.filter((session) => isAssistantRootSession(session, assistantDirectory))
.sort((a, b) => b.time.updated - a.time.updated)[0]
}
async function getLatestAssistantSession(
client: OpenCodeClient,
assistantDirectory: string,
): Promise<OpenCodeSession | undefined> {
let page = await client.listSessionsPage({ limit: ASSISTANT_SESSION_LOOKUP_PAGE_SIZE, order: 'desc' })
let latestRootSession = findNewestRootAssistantSession(page.items, assistantDirectory)
while (!latestRootSession && page.nextCursor) {
page = await client.listSessionsPage({ cursor: page.nextCursor })
latestRootSession = findNewestRootAssistantSession(page.items, assistantDirectory)
}
return latestRootSession
}
const ASSISTANT_WELCOME_PROMPT = `Welcome to OpenCode Manager! I'm your assistant and I'm here to help you work with your code.
To get started, let's set up your assistant:
**1. Name your assistant**
What would you like to call me? This name will help personalize our interactions.
**2. Review AGENTS.md**
AGENTS.md explains the assistant workspace directory and points to the files OpenCode Manager manages.
**3. Review the assistant agent**
.opencode/agents/assistant.md contains the default Assistant Mode agent instructions, durable preferences, self-editing rules, and skill guidance.
**4. Use workspace skills**
.opencode/skills/ contains managed workspace skills for repos, schedules, notifications, and settings.
Take your time exploring and customizing these settings. Let me know when you're ready to start coding, or if you have any questions about getting set up!`
async function sendAssistantWelcomePrompt(client: OpenCodeClient, sessionId: string): Promise<void> {
await client.sendPromptAsync(sessionId, {
parts: [
{
type: 'text',
text: ASSISTANT_WELCOME_PROMPT,
},
],
}).catch(() => undefined)
}
export function useAssistantSessionLauncher({
opcodeUrl,
directory,
onNavigate,
}: UseAssistantSessionLauncherOptions) {
const openAssistant = useCallback(async () => {
if (!directory) {
throw new Error('Assistant workspace directory is unavailable')
}
const client = new OpenCodeClient(opcodeUrl, directory)
const newest = await getLatestAssistantSession(client, directory)
if (newest) {
onNavigate(newest.id)
} else {
const session = await client.createSession({ title: 'Assistant' })
onNavigate(session.id)
void sendAssistantWelcomePrompt(client, session.id)
}
}, [opcodeUrl, directory, onNavigate])
return { openAssistant }
}