move services to context
This commit is contained in:
parent
5ed9501dde
commit
5d45df5b0f
9 changed files with 133 additions and 147 deletions
|
|
@ -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
|
||||
}
|
||||
```
|
||||
|
|
@ -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(() => {
|
||||
|
|
|
|||
40
spec/permissions.md
Normal file
40
spec/permissions.md
Normal file
|
|
@ -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
|
||||
```
|
||||
0
spec/wire-format.md
Normal file
0
spec/wire-format.md
Normal file
25
src/__mocks__/services/ServiceContainerContext.ts
Normal file
25
src/__mocks__/services/ServiceContainerContext.ts
Normal file
|
|
@ -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);
|
||||
|
|
@ -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<T>(serviceName: string): ServiceResult<T> & {
|
||||
reload: () => Promise<void>;
|
||||
} {
|
||||
const container = useServiceContainer();
|
||||
const [result, setResult] = useState<ServiceResult<T>>(() =>
|
||||
serviceContainer.getSync<T>(serviceName)
|
||||
container.getSync<T>(serviceName)
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// Get initial state
|
||||
const initialResult = serviceContainer.getSync<T>(serviceName);
|
||||
const initialResult = container.getSync<T>(serviceName);
|
||||
setResult(initialResult);
|
||||
|
||||
// Auto-load if idle
|
||||
if (initialResult.state === 'idle') {
|
||||
serviceContainer.load<T>(serviceName).catch(() => {
|
||||
container.load<T>(serviceName).catch(() => {
|
||||
// Error is handled by the service container and will trigger the error event
|
||||
});
|
||||
}
|
||||
|
|
@ -58,22 +59,22 @@ export function useService<T>(serviceName: string): ServiceResult<T> & {
|
|||
};
|
||||
|
||||
// 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<void> => {
|
||||
await serviceContainer.reload(serviceName);
|
||||
await container.reload(serviceName);
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
@ -93,6 +94,7 @@ export function useServices<T extends Record<string, any>>(
|
|||
error: Error | null;
|
||||
allReady: boolean;
|
||||
} {
|
||||
const container = useServiceContainer();
|
||||
const [services, setServices] = useState<Partial<T>>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<Error | null>(null);
|
||||
|
|
@ -105,7 +107,7 @@ export function useServices<T extends Record<string, any>>(
|
|||
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<T extends Record<string, any>>(
|
|||
} 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<T extends Record<string, any>>(
|
|||
|
||||
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<T extends Record<string, any>>(
|
|||
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 };
|
||||
|
|
|
|||
23
src/services/ServiceContainerContext.tsx
Normal file
23
src/services/ServiceContainerContext.tsx
Normal file
|
|
@ -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<ServiceContainer>(defaultContainer);
|
||||
|
||||
export function ServiceContainerProvider({
|
||||
children,
|
||||
container = defaultContainer
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
container?: ServiceContainer;
|
||||
}) {
|
||||
return (
|
||||
<ServiceContainerContext.Provider value={container}>
|
||||
{children}
|
||||
</ServiceContainerContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useServiceContainer() {
|
||||
return useContext(ServiceContainerContext);
|
||||
}
|
||||
|
|
@ -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");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
})
|
||||
})
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue