Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,57 @@ Switch from npm-based distribution to standalone binary distribution with curl i

---

### Consolidate Chat Handlers with Adapter Pattern

**Context:** `src/chat/handler.ts` (container) and `src/chat/host-handler.ts` (host) share ~95% identical code:
- `StreamMessage` interface duplicated (L18-40 vs L11-33)
- `processBuffer()` method identical
- `handleStreamMessage()` method ~95% identical
- `interrupt()` method identical
- Default model `'sonnet'` hardcoded in both

Same pattern exists for OpenCode handlers. When we add more agents, this will get worse.

**Task:** Refactor to adapter pattern:

1. Create `src/chat/base-chat-session.ts`:
- Move `StreamMessage` interface here
- Create abstract `BaseChatSession` class with:
- `protected buffer: string`
- `protected sessionId?: string`
- `protected model: string` (default from constant)
- `protected processBuffer(): void` - shared implementation
- `protected handleStreamMessage(msg: StreamMessage): void` - shared implementation
- `async interrupt(): Promise<void>` - shared implementation
- `abstract getSpawnCommand(): string[]` - what differs between container/host
- `abstract getSpawnOptions(): object` - cwd, env differences

2. Create `src/chat/adapters/`:
- `container-adapter.ts` - implements spawn for `docker exec` into container
- `host-adapter.ts` - implements spawn for direct `claude` execution

3. Refactor `ChatSession` and `HostChatSession` to extend `BaseChatSession` and use adapters

4. Do the same for OpenCode handlers (`opencode-handler.ts`, `host-opencode-handler.ts`)

5. Move default model `'sonnet'` to `src/shared/constants.ts` as `DEFAULT_CLAUDE_MODEL`

**Files to create:**
- `src/chat/base-chat-session.ts`
- `src/chat/adapters/container-adapter.ts`
- `src/chat/adapters/host-adapter.ts`

**Files to modify:**
- `src/chat/handler.ts` - extend base, use adapter
- `src/chat/host-handler.ts` - extend base, use adapter
- `src/chat/opencode-handler.ts` - same pattern
- `src/chat/host-opencode-handler.ts` - same pattern
- `src/shared/constants.ts` - add DEFAULT_CLAUDE_MODEL

**Verify:** Chat still works for both container and host workspaces. Run existing chat tests.

---

## Considerations

> Add items here to discuss with project owner before promoting to tasks.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "rm -rf ./dist && bun run build:ts && bun run build:worker && bun run build:web && bun link",
"build:ts": "tsc && chmod +x dist/index.js",
"build:worker": "bun build src/index.ts --compile --outfile dist/perry-worker --target=bun",
"build:web": "cp src/shared/client-types.ts web/src/lib/types.ts && cd web && bun run build && cp -r dist ../dist/agent/web",
"build:web": "cd web && bun run build && cp -r dist ../dist/agent/web",
"test": "vitest run",
"test:web": "playwright test",
"test:tui": "vitest run --config vitest.tui.config.js",
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'lucide-react'
import { cn } from '@/lib/utils'
import { api, type WorkspaceInfo } from '@/lib/api'
import { HOST_WORKSPACE_NAME } from '@/lib/types'
import { HOST_WORKSPACE_NAME } from '@shared/client-types'
import { Button } from '@/components/ui/button'
import { ThemeSwitcher } from '@/components/ThemeSwitcher'

Expand Down
2 changes: 1 addition & 1 deletion web/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
SSHKeyInfo,
RecentSession,
ModelInfo,
} from './types'
} from '@shared/client-types'

export type {
WorkspaceInfo,
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/WorkspaceDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
X,
} from 'lucide-react'
import { api, type SessionInfo, type AgentType } from '@/lib/api'
import { HOST_WORKSPACE_NAME } from '@/lib/types'
import { HOST_WORKSPACE_NAME } from '@shared/client-types'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/WorkspaceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { useNavigate } from 'react-router-dom'
import { Plus, RefreshCw, Boxes, ChevronRight, Sparkles, Monitor, AlertTriangle, Shield } from 'lucide-react'
import { api, type WorkspaceInfo, type CreateWorkspaceRequest, type HostInfo } from '@/lib/api'
import { HOST_WORKSPACE_NAME } from '@/lib/types'
import { HOST_WORKSPACE_NAME } from '@shared/client-types'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Input } from '@/components/ui/input'
Expand Down
5 changes: 3 additions & 2 deletions web/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
/* Path aliases */
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
"@/*": ["./src/*"],
"@shared/*": ["../src/shared/*"]
}
},
"include": ["src"]
"include": ["src", "../src/shared"]
}
1 change: 1 addition & 0 deletions web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'@shared': path.resolve(__dirname, '../src/shared'),
},
},
server: {
Expand Down