Elffuss BraidHub

kikocis/continue public

4e7d29ea58e1 · 4.0 KiBRawPermalink
1import { Server } from "http";
2
3import express from "express";
4import type { ChatCompletionMessageParam } from "openai/resources.mjs";
5import { describe, expect, test } from "vitest";
6
7export interface MockRemoteServerConfig {
8 port?: number;
9 responses?: string[];
10}
11
12export class MockRemoteServer {
13 private server: Server | null = null;
14 private app: express.Application;
15 private messages: ChatCompletionMessageParam[] = [];
16 private responses: string[] = [];
17 private currentResponseIndex = 0;
18 private isProcessing = false;
19 private streamInterval: NodeJS.Timeout | null = null;
20
21 constructor(config: MockRemoteServerConfig = {}) {
22 this.app = express();
23 this.app.use(express.json());
24 this.responses = config.responses || ["Mock response"];
25 this.setupRoutes();
26 }
27
28 private setupRoutes() {
29 // GET /state - Return the current state
30 this.app.get("/state", (req, res) => {
31 res.json({
32 chatHistory: this.messages,
33 isProcessing: this.isProcessing,
34 messageQueueLength: 0,
35 });
36 });
37
38 // POST /message - Handle new messages
39 this.app.post("/message", (req, res) => {
40 const { message } = req.body;
41
42 if (!message && message !== "") {
43 return res.status(400).json({ error: "Message field is required" });
44 }
45
46 // Add user message
47 if (message) {
48 this.messages.push({
49 role: "user",
50 content: message,
51 });
52 }
53
54 // Start processing response
55 this.processResponse();
56
57 res.json({
58 queued: true,
59 position: 1,
60 willInterrupt: false,
61 });
62 });
63
64 // POST /exit - Shutdown server
65 this.app.post("/exit", (req, res) => {
66 res.json({
67 message: "Server shutting down",
68 success: true,
69 });
70
71 this.stop();
72 });
73 }
74
75 private processResponse() {
76 if (this.isProcessing) return;
77
78 this.isProcessing = true;
79
80 // Get the response to simulate
81 const response =
82 this.responses[this.currentResponseIndex % this.responses.length];
83 this.currentResponseIndex++;
84
85 // Simulate streaming response
86 let currentContent = "";
87 let charIndex = 0;
88
89 // Add initial streaming message
90 const streamingMessage: ChatCompletionMessageParam = {
91 role: "assistant",
92 content: "",
93 };
94 this.messages.push(streamingMessage);
95
96 // Simulate character-by-character streaming (but faster for tests)
97 this.streamInterval = setInterval(() => {
98 if (charIndex < response.length) {
99 currentContent += response[charIndex];
100 streamingMessage.content = currentContent;
101 charIndex++;
102 } else {
103 // Finish streaming
104 this.isProcessing = false;
105
106 if (this.streamInterval) {
107 clearInterval(this.streamInterval);
108 this.streamInterval = null;
109 }
110 }
111 }, 10); // Fast streaming for tests
112 }
113
114 public async start(port: number = 0): Promise<number> {
115 return new Promise((resolve, reject) => {
116 this.server = this.app.listen(port, (err?: Error) => {
117 if (err) {
118 reject(err);
119 } else {
120 const actualPort = (this.server!.address() as any)?.port || port;
121 resolve(actualPort);
122 }
123 });
124 });
125 }
126
127 public async stop(): Promise<void> {
128 return new Promise((resolve) => {
129 if (this.streamInterval) {
130 clearInterval(this.streamInterval);
131 this.streamInterval = null;
132 }
133
134 if (this.server) {
135 this.server.close(() => {
136 this.server = null;
137 resolve();
138 });
139 } else {
140 resolve();
141 }
142 });
143 }
144
145 public getMessages(): ChatCompletionMessageParam[] {
146 return [...this.messages];
147 }
148
149 public clearMessages(): void {
150 this.messages = [];
151 }
152
153 public setResponses(responses: string[]): void {
154 this.responses = responses;
155 this.currentResponseIndex = 0;
156 }
157}
158
159// Add a dummy test to satisfy Vitest requirement
160describe("MockRemoteServer", () => {
161 test("should be defined", () => {
162 expect(MockRemoteServer).toBeDefined();
163 });
164});