Skip to content

Commit 4c1980b

Browse files
committed
Refactor env code
1 parent 16d4316 commit 4c1980b

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"type": "module",
88
"main": "index.js",
99
"scripts": {
10-
"start": "tsc && node dist/index.js",
11-
"build": "tsc",
12-
"dev": "tsc && concurrently \"tsc --watch\" \"nodemon dist/index.js\""
10+
"start": "npx tsc && node dist/index.js",
11+
"build": "npx tsc",
12+
"watch": "npx tsc --watch"
1313
},
1414
"dependencies": {
1515
"@azure/ai-projects": "^1.0.0-beta.2",
@@ -21,7 +21,6 @@
2121
"concurrently": "^9.1.2",
2222
"nodemon": "^3.1.9",
2323
"ts-node": "^10.9.2",
24-
"tsc": "^2.0.4",
2524
"typescript": "^5.8.2"
2625
}
2726
}

src/env.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { config } from 'dotenv';
2+
config();
3+
4+
export const aiFoundryConnectionString = process.env.AI_FOUNDRY_PROJECT_CONNECTION_STRING || '';
5+
export const aiSearchConnectionString = process.env.AI_SEARCH_CONNECTION_STRING || '';
6+
export const model = process.env.AI_MODEL || '';
7+
8+
/**
9+
* Validates that all required environment variables are set
10+
* @throws Error if any required environment variable is missing
11+
*/
12+
export function validateEnvironment(): void {
13+
if (!aiFoundryConnectionString) {
14+
throw new Error('Please set the AI_FOUNDRY_PROJECT_CONNECTION_STRING environment variable.');
15+
}
16+
17+
if (!aiSearchConnectionString) {
18+
throw new Error('Please set the AI_SEARCH_CONNECTION_STRING environment variable.');
19+
}
20+
21+
if (!model) {
22+
throw new Error('Please set the AI_MODEL environment variable.');
23+
}
24+
}
25+
26+
// Validate environment variables when the module is imported
27+
validateEnvironment();

src/index.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import fs from 'fs';
2+
import readline from 'readline';
23
import { AIProjectsClient, DoneEvent, ErrorEvent, isOutputOfType, MessageStreamEvent, RunStreamEvent, ToolUtility } from '@azure/ai-projects';
34
import type { AgentOutput, AgentThreadOutput, MessageDeltaChunk, MessageDeltaTextContent, MessageImageFileContentOutput, MessageTextContentOutput, OpenAIPageableListOfThreadMessageOutput, ThreadRunOutput } from '@azure/ai-projects';
45
import { DefaultAzureCredential } from '@azure/identity';
5-
import readline from 'readline';
6-
import { config } from 'dotenv';
6+
import { aiFoundryConnectionString, aiSearchConnectionString, model } from './env.js';
77
import { PromptConfig } from './types';
8-
config();
9-
10-
// Environment variables with proper validation
11-
const aiFoundryConnectionString = process.env.AI_FOUNDRY_PROJECT_CONNECTION_STRING || '';
12-
const aiSearchConnectionString = process.env.AI_SEARCH_CONNECTION_STRING || '';
13-
const model = process.env.AI_MODEL || '';
14-
15-
if (!aiFoundryConnectionString) {
16-
throw new Error('Please set the AI_FOUNDRY_PROJECT_CONNECTION_STRING environment variable.');
17-
}
18-
19-
if (!aiSearchConnectionString) {
20-
throw new Error('Please set the AI_SEARCH_CONNECTION_STRING environment variable.');
21-
}
22-
23-
if (!model) {
24-
throw new Error('Please set the AI_MODEL environment variable.');
25-
}
268

279
// Define prompt configurations
2810
const promptConfig: Record<string, PromptConfig> = {
@@ -94,7 +76,7 @@ async function main() {
9476
}
9577
} catch (err) {
9678
console.error('The application encountered an error:', err);
97-
process.exit(1);
79+
// process.exit(1);
9880
}
9981
}
10082

0 commit comments

Comments
 (0)