diff --git a/docs/remote-server-api.md b/docs/remote-server-api.md deleted file mode 100644 index 1b4282256..000000000 --- a/docs/remote-server-api.md +++ /dev/null @@ -1,111 +0,0 @@ -# Remote Server API Documentation - -## Message Format - -The remote server should send messages in the same format as the local Continue CLI to ensure consistent display and functionality. - -### Message Structure - -Each message in the `chatHistory` array should follow this structure: - -```typescript -interface DisplayMessage { - role: "user" | "assistant" | "system"; - content: string; - messageType?: "chat" | "tool-start" | "tool-result" | "tool-error" | "system"; - toolName?: string; - toolResult?: string; - isStreaming?: boolean; -} -``` - -### Tool Messages - -When the assistant uses tools, the server should send specific message types: - -#### Tool Start Message -```json -{ - "role": "system", - "content": "○ Read(src/file.ts)", - "messageType": "tool-start", - "toolName": "read_file" -} -``` - -#### Tool Result Message -```json -{ - "role": "system", - "content": "● Read(src/file.ts)", - "messageType": "tool-result", - "toolName": "read_file", - "toolResult": "File contents here..." -} -``` - -#### Tool Error Message -```json -{ - "role": "system", - "content": "✗ Tool error: File not found", - "messageType": "tool-error" -} -``` - -### Tool Name Mapping - -Tool display names should map to tool names as follows: - -| Display Name | Tool Name | -|-------------|-----------| -| Read | read_file | -| Write | write_file | -| Bash | run_terminal_command | -| List | list_files | -| Search | search_code | -| Diff | view_diff | -| Fetch | fetch | -| Exit | exit | - -### Benefits - -By sending messages in this format, the remote server enables: - -1. **Consistent UI** - Tool results are displayed with nice summaries using the `ToolResultSummary` component -2. **Better UX** - Long outputs are collapsed, diffs are syntax highlighted, etc. -3. **Proper State Tracking** - The UI can track which tools are running, completed, or errored -4. **No Client-Side Transformation** - The client doesn't need hacky regex parsing to detect tool calls - -### Example Response - -Here's a complete example of a `/state` response with tool calls: - -```json -{ - "chatHistory": [ - { - "role": "user", - "content": "Read the README file" - }, - { - "role": "system", - "content": "○ Read(README.md)", - "messageType": "tool-start", - "toolName": "read_file" - }, - { - "role": "system", - "content": "● Read(README.md)", - "messageType": "tool-result", - "toolName": "read_file", - "toolResult": "# My Project\n\nThis is a sample README file.\n\n## Installation\n\nRun `npm install`" - }, - { - "role": "assistant", - "content": "I've read the README file. It contains installation instructions that recommend running `npm install`." - } - ], - "isProcessing": false -} -``` \ No newline at end of file diff --git a/jest.setup.ts b/jest.setup.ts index b5b9dde1e..e1f4b4492 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -13,6 +13,7 @@ jest.mock("./src/hooks/useService.js"); jest.mock("./src/auth/workos.js"); jest.mock("./src/commands/commands.js"); jest.mock("./src/services/index.js"); +jest.mock("./src/services/ServiceContainerContext.js"); // Set up global afterEach hook to clear all timers afterEach(() => { diff --git a/spec/permissions.md b/spec/permissions.md new file mode 100644 index 000000000..b4340aa5e --- /dev/null +++ b/spec/permissions.md @@ -0,0 +1,40 @@ +# Permissions + +```bash +cn --allow Read --ask Write --exclude Bash + +cn --no-tools --allow Read + +cn --only Read + +cn --mcp anthropic/filesystem --no-tools --allow anthropic/filesystem/read_file +``` + +```yaml +mcpServers: + - name: GitHub MCP + type: streamable-http + url: https://api.github.com/mcp + +permissions: + - tool: Read + permission: allow + - tool: Write + permission: ask + - tool: Bash + permission: exclude + +permissions: + - Read(*) + - ~Write(**/*.ts) + - !Bash(*) + +allow: + - Read(*) + +ask: + - Write(**/*.py) + +exclude: + - Write +``` diff --git a/spec/wire-format.md b/spec/wire-format.md new file mode 100644 index 000000000..e69de29bb diff --git a/src/__mocks__/services/ServiceContainerContext.ts b/src/__mocks__/services/ServiceContainerContext.ts new file mode 100644 index 000000000..b5c0dfdf9 --- /dev/null +++ b/src/__mocks__/services/ServiceContainerContext.ts @@ -0,0 +1,25 @@ +import React from 'react'; +import { jest } from "@jest/globals"; +import { ServiceContainer } from '../../services/ServiceContainer.js'; + +// Create a mock service container +const mockServiceContainer = { + getSync: jest.fn((serviceName: string) => ({ + state: "ready", + value: {}, + error: null, + })), + isReady: jest.fn(() => true), + on: jest.fn(), + off: jest.fn(), + load: jest.fn(() => Promise.resolve()), + reload: jest.fn(() => Promise.resolve()), + emit: jest.fn(), + setMaxListeners: jest.fn(), +} as unknown as ServiceContainer; + +export const ServiceContainerProvider = ({ children }: { children: React.ReactNode }) => { + return React.createElement(React.Fragment, null, children); +}; + +export const useServiceContainer = jest.fn(() => mockServiceContainer); \ No newline at end of file diff --git a/src/hooks/useService.ts b/src/hooks/useService.ts index a66be5153..ccbe34093 100644 --- a/src/hooks/useService.ts +++ b/src/hooks/useService.ts @@ -1,5 +1,5 @@ import { useEffect, useState } from 'react'; -import { serviceContainer } from '../services/ServiceContainer.js'; +import { useServiceContainer } from '../services/ServiceContainerContext.js'; import { ServiceResult } from '../services/types.js'; /** @@ -9,18 +9,19 @@ import { ServiceResult } from '../services/types.js'; export function useService(serviceName: string): ServiceResult & { reload: () => Promise; } { + const container = useServiceContainer(); const [result, setResult] = useState>(() => - serviceContainer.getSync(serviceName) + container.getSync(serviceName) ); useEffect(() => { // Get initial state - const initialResult = serviceContainer.getSync(serviceName); + const initialResult = container.getSync(serviceName); setResult(initialResult); // Auto-load if idle if (initialResult.state === 'idle') { - serviceContainer.load(serviceName).catch(() => { + container.load(serviceName).catch(() => { // Error is handled by the service container and will trigger the error event }); } @@ -58,22 +59,22 @@ export function useService(serviceName: string): ServiceResult & { }; // Subscribe to events - serviceContainer.on(`${serviceName}:loading`, onLoading); - serviceContainer.on(`${serviceName}:ready`, onReady); - serviceContainer.on(`${serviceName}:error`, onError); - serviceContainer.on(`${serviceName}:changed`, onChanged); + container.on(`${serviceName}:loading`, onLoading); + container.on(`${serviceName}:ready`, onReady); + container.on(`${serviceName}:error`, onError); + container.on(`${serviceName}:changed`, onChanged); // Cleanup return () => { - serviceContainer.off(`${serviceName}:loading`, onLoading); - serviceContainer.off(`${serviceName}:ready`, onReady); - serviceContainer.off(`${serviceName}:error`, onError); - serviceContainer.off(`${serviceName}:changed`, onChanged); + container.off(`${serviceName}:loading`, onLoading); + container.off(`${serviceName}:ready`, onReady); + container.off(`${serviceName}:error`, onError); + container.off(`${serviceName}:changed`, onChanged); }; - }, [serviceName]); + }, [serviceName, container]); const reload = async (): Promise => { - await serviceContainer.reload(serviceName); + await container.reload(serviceName); }; return { @@ -93,6 +94,7 @@ export function useServices>( error: Error | null; allReady: boolean; } { + const container = useServiceContainer(); const [services, setServices] = useState>({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); @@ -105,7 +107,7 @@ export function useServices>( let allReady = true; for (const serviceName of serviceNames) { - const result = serviceContainer.getSync(serviceName as string); + const result = container.getSync(serviceName as string); if (result.state === 'loading') { hasLoading = true; @@ -116,7 +118,7 @@ export function useServices>( } else if (result.state === 'idle') { allReady = false; // Auto-load idle services - serviceContainer.load(serviceName as string).catch(() => {}); + container.load(serviceName as string).catch(() => {}); } else if (result.state === 'ready' && result.value !== null) { newServices[serviceName] = result.value as T[keyof T]; } else { @@ -140,16 +142,16 @@ export function useServices>( const onAnyChange = () => updateState(); - serviceContainer.on(`${name}:loading`, onAnyChange); - serviceContainer.on(`${name}:ready`, onAnyChange); - serviceContainer.on(`${name}:error`, onAnyChange); - serviceContainer.on(`${name}:changed`, onAnyChange); + container.on(`${name}:loading`, onAnyChange); + container.on(`${name}:ready`, onAnyChange); + container.on(`${name}:error`, onAnyChange); + container.on(`${name}:changed`, onAnyChange); listeners.push(() => { - serviceContainer.off(`${name}:loading`, onAnyChange); - serviceContainer.off(`${name}:ready`, onAnyChange); - serviceContainer.off(`${name}:error`, onAnyChange); - serviceContainer.off(`${name}:changed`, onAnyChange); + container.off(`${name}:loading`, onAnyChange); + container.off(`${name}:ready`, onAnyChange); + container.off(`${name}:error`, onAnyChange); + container.off(`${name}:changed`, onAnyChange); }); } @@ -157,10 +159,10 @@ export function useServices>( return () => { listeners.forEach(cleanup => cleanup()); }; - }, [serviceNames.join(',')]); + }, [serviceNames.join(','), container]); const allReady = serviceNames.every(name => - serviceContainer.isReady(name as string) + container.isReady(name as string) ); return { services, loading, error, allReady }; diff --git a/src/services/ServiceContainerContext.tsx b/src/services/ServiceContainerContext.tsx new file mode 100644 index 000000000..9f08e3119 --- /dev/null +++ b/src/services/ServiceContainerContext.tsx @@ -0,0 +1,23 @@ +import React, { createContext, useContext } from 'react'; +import { ServiceContainer } from './ServiceContainer.js'; +import { serviceContainer as defaultContainer } from './index.js'; + +const ServiceContainerContext = createContext(defaultContainer); + +export function ServiceContainerProvider({ + children, + container = defaultContainer +}: { + children: React.ReactNode; + container?: ServiceContainer; +}) { + return ( + + {children} + + ); +} + +export function useServiceContainer() { + return useContext(ServiceContainerContext); +} \ No newline at end of file diff --git a/src/ui/__tests__/TUIChat.basic.test.tsx b/src/ui/__tests__/TUIChat.basic.test.tsx index 3121a6e2b..a869ecf95 100644 --- a/src/ui/__tests__/TUIChat.basic.test.tsx +++ b/src/ui/__tests__/TUIChat.basic.test.tsx @@ -28,7 +28,7 @@ describe("TUIChat - Basic UI Tests", () => { } }); - it("shows loading state when services are loading", () => { + it("shows UI even when services are loading", () => { // Override for this test context.mockUseServices.mockReturnValue({ services: {}, @@ -42,7 +42,8 @@ describe("TUIChat - Basic UI Tests", () => { expect(frame).toBeDefined(); if (frame) { - expect(frame).toMatch(/loading|service/i); + // Should show the chat UI even while loading + expect(frame).toContain("Ask anything"); } }); diff --git a/src/ui/index.ts b/src/ui/index.ts index 7d0587c0f..c63b70db7 100644 --- a/src/ui/index.ts +++ b/src/ui/index.ts @@ -1,6 +1,7 @@ import { render } from "ink"; import React from "react"; import { initializeServices } from "../services/index.js"; +import { ServiceContainerProvider } from "../services/ServiceContainerContext.js"; import TUIChat from "./TUIChat.js"; export { default as MarkdownRenderer } from "./MarkdownRenderer.js"; @@ -22,11 +23,13 @@ export async function startTUIChat( // Start the TUI immediately - it will handle loading states const { unmount } = render( - React.createElement(TUIChat, { - configPath, - initialPrompt, - resume, - additionalRules, + React.createElement(ServiceContainerProvider, { + children: React.createElement(TUIChat, { + configPath, + initialPrompt, + resume, + additionalRules, + }) }) ); @@ -45,9 +48,11 @@ export async function startRemoteTUIChat( ) { // Start the TUI in remote mode - no services needed const { unmount } = render( - React.createElement(TUIChat, { - remoteUrl, - initialPrompt, + React.createElement(ServiceContainerProvider, { + children: React.createElement(TUIChat, { + remoteUrl, + initialPrompt, + }) }) );