mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
refactor: move assistant workspace install before OpenCode server start (#263)
* refactor: move assistant workspace install before OpenCode server start * refactor: skip assistant-mode status fetch on open, use repo fullPath and cached session
This commit is contained in:
1 parent
0d14e754e1
commit
0d384d6cf0
9 files changed
+151
-169
No files matched your search
@@ -2,18 +2,12 @@ import { renderHook, act } from '@testing-library/react'
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { useAssistantSessionLauncher } from './useAssistantSessionLauncher'
|
||||
import { OpenCodeClient } from '@/api/opencode'
|
||||
import { initializeAssistantMode } from '@/api/repos'
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
listSessions: vi.fn(),
|
||||
listSessionsPage: vi.fn(),
|
||||
createSession: vi.fn(),
|
||||
sendPromptAsync: vi.fn(),
|
||||
initializeAssistantMode: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/repos', () => ({
|
||||
initializeAssistantMode: mocks.initializeAssistantMode,
|
||||
}))
|
||||
|
||||
vi.mock('@/api/opencode', () => ({
|
||||
@@ -33,7 +27,6 @@ describe('useAssistantSessionLauncher', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
localStorage.clear()
|
||||
mocks.initializeAssistantMode.mockResolvedValue({ directory: '/assistant' })
|
||||
})
|
||||
|
||||
it('opens the latest root session in the assistant directory', async () => {
|
||||
@@ -49,6 +42,7 @@ describe('useAssistantSessionLauncher', () => {
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -56,7 +50,6 @@ describe('useAssistantSessionLauncher', () => {
|
||||
await result.current.openAssistant()
|
||||
})
|
||||
|
||||
expect(initializeAssistantMode).toHaveBeenCalledWith(123)
|
||||
expect(OpenCodeClient).toHaveBeenCalledWith('http://localhost:5551', '/assistant')
|
||||
expect(mocks.listSessionsPage).toHaveBeenCalledWith({ limit: 25, order: 'desc' })
|
||||
expect(mocks.listSessions).not.toHaveBeenCalled()
|
||||
@@ -83,6 +76,7 @@ describe('useAssistantSessionLauncher', () => {
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -96,26 +90,13 @@ describe('useAssistantSessionLauncher', () => {
|
||||
expect(onNavigate).toHaveBeenCalledWith('newest')
|
||||
})
|
||||
|
||||
it('notifies an existing assistant session when some generated updates were preserved', async () => {
|
||||
mocks.initializeAssistantMode.mockResolvedValue({
|
||||
directory: '/assistant',
|
||||
warnings: [
|
||||
{
|
||||
code: 'assistant-agents-md-preserved',
|
||||
path: '/assistant/AGENTS.md',
|
||||
message: 'Some Assistant Mode instruction updates were not applied because AGENTS.md appears to contain customized legacy assistant instructions. To regenerate the default workspace explanation, manually delete AGENTS.md and initialize Assistant Mode again.',
|
||||
},
|
||||
],
|
||||
})
|
||||
mocks.listSessionsPage.mockResolvedValue({
|
||||
items: [
|
||||
{ id: 'existing', directory: '/assistant', time: { updated: 10 } },
|
||||
],
|
||||
})
|
||||
it('navigates directly to the cached assistant session without querying OpenCode', async () => {
|
||||
localStorage.setItem('ocm:assistant:last-session:123:/assistant', 'cached')
|
||||
const onNavigate = vi.fn()
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -123,18 +104,11 @@ describe('useAssistantSessionLauncher', () => {
|
||||
await result.current.openAssistant()
|
||||
})
|
||||
|
||||
expect(onNavigate).toHaveBeenCalledWith('existing')
|
||||
expect(mocks.listSessionsPage).toHaveBeenCalledWith({ limit: 25, order: 'desc' })
|
||||
expect(mocks.sendPromptAsync).toHaveBeenCalledWith('existing', {
|
||||
parts: [
|
||||
expect.objectContaining({
|
||||
type: 'text',
|
||||
text: expect.stringContaining('some generated instruction changes were not applied'),
|
||||
}),
|
||||
],
|
||||
})
|
||||
const promptText = mocks.sendPromptAsync.mock.calls[0][1].parts[0].text as string
|
||||
expect(promptText).toContain('manually delete AGENTS.md')
|
||||
expect(onNavigate).toHaveBeenCalledWith('cached')
|
||||
expect(OpenCodeClient).not.toHaveBeenCalled()
|
||||
expect(mocks.listSessionsPage).not.toHaveBeenCalled()
|
||||
expect(mocks.createSession).not.toHaveBeenCalled()
|
||||
expect(mocks.sendPromptAsync).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('creates a session when the assistant directory has no root sessions', async () => {
|
||||
@@ -148,6 +122,7 @@ describe('useAssistantSessionLauncher', () => {
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -191,6 +166,7 @@ describe('useAssistantSessionLauncher', () => {
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -218,6 +194,7 @@ describe('useAssistantSessionLauncher', () => {
|
||||
const { result } = renderHook(() => useAssistantSessionLauncher({
|
||||
repoId: 123,
|
||||
opcodeUrl: 'http://localhost:5551',
|
||||
directory: '/assistant',
|
||||
onNavigate,
|
||||
}))
|
||||
|
||||
@@ -228,4 +205,22 @@ describe('useAssistantSessionLauncher', () => {
|
||||
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({
|
||||
repoId: 123,
|
||||
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,12 +1,11 @@
|
||||
import { useCallback } from 'react'
|
||||
import { initializeAssistantMode } from '@/api/repos'
|
||||
import { OpenCodeClient } from '@/api/opencode'
|
||||
import type { AssistantModeStatus } from '@opencode-manager/shared/types'
|
||||
import type { components } from '@/api/opencode-types'
|
||||
|
||||
interface UseAssistantSessionLauncherOptions {
|
||||
repoId: number
|
||||
opcodeUrl: string
|
||||
directory?: string
|
||||
onNavigate: (sessionId: string) => void
|
||||
}
|
||||
|
||||
@@ -28,6 +27,14 @@ function setCachedAssistantSessionId(repoId: number, directory: string, sessionI
|
||||
}
|
||||
}
|
||||
|
||||
function getCachedAssistantSessionId(repoId: number, directory: string): string | undefined {
|
||||
try {
|
||||
return localStorage.getItem(getLastAssistantSessionKey(repoId, directory)) ?? undefined
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
function isAssistantRootSession(session: OpenCodeSession, assistantDirectory: string): boolean {
|
||||
return !session.parentID && session.directory === assistantDirectory
|
||||
}
|
||||
@@ -71,43 +78,12 @@ AGENTS.md explains the assistant workspace directory and points to the files Ope
|
||||
|
||||
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!`
|
||||
|
||||
function buildAssistantModeWarningsPrompt(assistant: AssistantModeStatus): string | undefined {
|
||||
if (!assistant.warnings?.length) return undefined
|
||||
|
||||
return [
|
||||
'Assistant Mode was updated, but some generated instruction changes were not applied.',
|
||||
'',
|
||||
...assistant.warnings.map((warning) => `- ${warning.message}`),
|
||||
].join('\n')
|
||||
}
|
||||
|
||||
function buildAssistantWelcomePrompt(assistant: AssistantModeStatus): string {
|
||||
const warningsPrompt = buildAssistantModeWarningsPrompt(assistant)
|
||||
return warningsPrompt
|
||||
? `${ASSISTANT_WELCOME_PROMPT}\n\n${warningsPrompt}`
|
||||
: ASSISTANT_WELCOME_PROMPT
|
||||
}
|
||||
|
||||
async function sendAssistantWelcomePrompt(client: OpenCodeClient, sessionId: string, assistant: AssistantModeStatus): Promise<void> {
|
||||
async function sendAssistantWelcomePrompt(client: OpenCodeClient, sessionId: string): Promise<void> {
|
||||
await client.sendPromptAsync(sessionId, {
|
||||
parts: [
|
||||
{
|
||||
type: 'text',
|
||||
text: buildAssistantWelcomePrompt(assistant),
|
||||
},
|
||||
],
|
||||
}).catch(() => undefined)
|
||||
}
|
||||
|
||||
async function sendAssistantModeWarningsPrompt(client: OpenCodeClient, sessionId: string, assistant: AssistantModeStatus): Promise<void> {
|
||||
const warningsPrompt = buildAssistantModeWarningsPrompt(assistant)
|
||||
if (!warningsPrompt) return
|
||||
|
||||
await client.sendPromptAsync(sessionId, {
|
||||
parts: [
|
||||
{
|
||||
type: 'text',
|
||||
text: warningsPrompt,
|
||||
text: ASSISTANT_WELCOME_PROMPT,
|
||||
},
|
||||
],
|
||||
}).catch(() => undefined)
|
||||
@@ -116,26 +92,34 @@ async function sendAssistantModeWarningsPrompt(client: OpenCodeClient, sessionId
|
||||
export function useAssistantSessionLauncher({
|
||||
repoId,
|
||||
opcodeUrl,
|
||||
directory,
|
||||
onNavigate,
|
||||
}: UseAssistantSessionLauncherOptions) {
|
||||
const openAssistant = useCallback(async () => {
|
||||
const assistant = await initializeAssistantMode(repoId)
|
||||
const client = new OpenCodeClient(opcodeUrl, assistant.directory)
|
||||
const assistantDirectory = assistant.directory
|
||||
if (!directory) {
|
||||
throw new Error('Assistant workspace directory is unavailable')
|
||||
}
|
||||
|
||||
const newest = await getLatestAssistantSession(client, assistantDirectory)
|
||||
const cachedSessionId = getCachedAssistantSessionId(repoId, directory)
|
||||
if (cachedSessionId) {
|
||||
onNavigate(cachedSessionId)
|
||||
return
|
||||
}
|
||||
|
||||
const client = new OpenCodeClient(opcodeUrl, directory)
|
||||
|
||||
const newest = await getLatestAssistantSession(client, directory)
|
||||
|
||||
if (newest) {
|
||||
setCachedAssistantSessionId(repoId, assistantDirectory, newest.id)
|
||||
setCachedAssistantSessionId(repoId, directory, newest.id)
|
||||
onNavigate(newest.id)
|
||||
void sendAssistantModeWarningsPrompt(client, newest.id, assistant)
|
||||
} else {
|
||||
const session = await client.createSession({ title: 'Assistant' })
|
||||
setCachedAssistantSessionId(repoId, assistantDirectory, session.id)
|
||||
setCachedAssistantSessionId(repoId, directory, session.id)
|
||||
onNavigate(session.id)
|
||||
void sendAssistantWelcomePrompt(client, session.id, assistant)
|
||||
void sendAssistantWelcomePrompt(client, session.id)
|
||||
}
|
||||
}, [repoId, opcodeUrl, onNavigate])
|
||||
}, [repoId, opcodeUrl, directory, onNavigate])
|
||||
|
||||
return { openAssistant }
|
||||
}
|
||||
Reference in new issue
Block a user