Skip to content

Commit 1212366

Browse files
authored
πŸ“¦ NEW: Check if BaseAI dev server is running (#25)
* πŸ‘Œ IMPROVE: Check if BaseAI dev server is running * πŸ“¦ NEW: Show warning if port in use but server not running * πŸ“¦ NEW: Lock file * πŸ‘Œ IMPROVE: Code * πŸ‘Œ IMPROVE: Remove dependency * πŸ‘Œ IMPROVE: Lock file * πŸ‘Œ IMPROVE: Code * πŸ‘Œ IMPROVE: Lingo * πŸ‘Œ IMPROVE: Code * πŸ› FIX: Lock file
1 parent 590bac3 commit 1212366

File tree

5 files changed

+871
-874
lines changed

5 files changed

+871
-874
lines changed

β€Žpackages/baseai/src/init/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ function displayOutro({ calledAsCommand }: { calledAsCommand: boolean }): void {
233233
}
234234
}
235235

236-
async function updateGitignore(gitignoreEntry: string): Promise<void> {
236+
async function updateGitignore({
237+
gitignoreEntry
238+
}: {
239+
gitignoreEntry: string;
240+
}): Promise<void> {
237241
const gitignorePath = path.join(process.cwd(), '.gitignore');
238242

239243
try {
@@ -355,8 +359,8 @@ export async function init({
355359
await createBaseAIDirectories();
356360
await createConfigFile();
357361
await updatePackageJsonScript();
358-
await updateGitignore('# baseai\n**/.baseai/\n');
359-
await updateGitignore('# env file\n.env\n');
362+
await updateGitignore({ gitignoreEntry: `# baseai\n**/.baseai/\n` });
363+
await updateGitignore({ gitignoreEntry: `# env file\n.env\n` });
360364
await createEnvBaseAIExample();
361365

362366
displayOutro({ calledAsCommand });

β€Žpackages/core/src/common/errors.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ export class APIConnectionError extends APIError {
108108

109109
constructor({message, cause}: {message?: string; cause?: Error}) {
110110
super(undefined, undefined, message || 'Connection error.', undefined);
111-
console.warn(
112-
`\nPlease check if BaseAI dev server is running. If not, run 'npx baseai dev' in the root directory.\n`,
113-
);
114111
if (cause) (this as Error).cause = cause;
115112
}
116113
}

β€Žpackages/core/src/pipes/pipes.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Request} from '../common/request';
44
import {getLLMApiKey} from '../utils/get-llm-api-key';
55
import {getApiUrl, isProd} from '../utils/is-prod';
66
import {toOldPipeFormat} from '../utils/to-old-pipe-format';
7+
import {isLocalServerRunning} from 'src/utils/local-server-running';
78

89
// Type Definitions
910
export type Role = 'user' | 'assistant' | 'system' | 'tool';
@@ -192,6 +193,10 @@ export class Pipe {
192193
let response = await this.createRequest<
193194
RunResponse | RunResponseStream
194195
>(endpoint, body);
196+
if (Object.entries(response).length === 0) {
197+
return {} as RunResponse | RunResponseStream;
198+
}
199+
195200
console.log('pipe.run.response');
196201
console.dir(response, {depth: null, colors: true});
197202

@@ -272,7 +277,14 @@ export class Pipe {
272277
llmApiKey: getLLMApiKey(this.pipe.model.provider),
273278
},
274279
};
275-
return this.request.post<T>(isProd() ? prodOptions : localOptions);
280+
281+
const isProdEnv = isProd();
282+
if (!isProdEnv) {
283+
const isServerRunning = await isLocalServerRunning();
284+
if (!isServerRunning) return {} as T;
285+
}
286+
287+
return this.request.post<T>(isProdEnv ? prodOptions : localOptions);
276288
}
277289
}
278290

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {getApiUrl} from './is-prod';
2+
3+
export async function isLocalServerRunning(): Promise<Boolean> {
4+
try {
5+
const endpoint = getApiUrl();
6+
7+
const response = await fetch(endpoint, {
8+
mode: 'no-cors',
9+
cache: 'no-cache', // Prevents caching of the request
10+
});
11+
12+
const portUseError = `\nPort 9000 is already in use. \nTerminate the process running on it. \nRun npx baseai@latest dev in an new terminal to start the dev server.\n`;
13+
14+
if (!response.ok) {
15+
console.error(portUseError);
16+
return false;
17+
}
18+
19+
const res = (await response.json()) as unknown as {
20+
success: boolean;
21+
};
22+
23+
if (!res.success) {
24+
console.error(portUseError);
25+
return false;
26+
}
27+
28+
return true;
29+
} catch (error) {
30+
// Port is not in use and BaseAI dev server is not running
31+
console.error(
32+
`\nBaseAI dev server is not running. \nPlease run npx baseai@latest dev in a new teriminal to start dev server.\n`,
33+
);
34+
return false;
35+
}
36+
}

0 commit comments

Comments
Β (0)