Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/providers/base/Provider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

/**
* Abstract Provider Base Class.
Expand Down Expand Up @@ -41,7 +43,9 @@ export class Provider {
*/
async _loadProviderPricing(providerName) {
try {
const pricingPath = new URL(`../../../config/${providerName}-pricing.json`, import.meta.url).pathname;
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const configDir = path.resolve(currentDir, '../../../config');
const pricingPath = path.join(configDir, `${providerName}-pricing.json`);
const pricingContent = fs.readFileSync(pricingPath, 'utf-8');

this.providerPricing = JSON.parse(pricingContent);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/costTracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

/**
* Cost Tracking Utility.
Expand Down Expand Up @@ -56,7 +57,7 @@ function loadProviderPricingData(providerName) {
}

try {
const currentDir = path.dirname(new URL(import.meta.url).pathname);
const currentDir = path.dirname(fileURLToPath(import.meta.url));

// Look for {provider}-pricing.json in the config directory relative to this module.
const jsonPath = path.resolve(currentDir, `../../config/${providerName}-pricing.json`);
Expand Down
3 changes: 2 additions & 1 deletion src/utils/promptLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { encoding_for_model as encodingForModel } from 'tiktoken';
import { getApiTargetLanguage } from './languageMapping.js';
import { getPluralForms, extractPluralCount } from './poFileUtils.js';
Expand Down Expand Up @@ -49,7 +50,7 @@ function loadPromptTemplate(promptFilePath) {
*/
export function buildSystemPrompt(targetLang, sourceLang = 'English', promptFilePath = null) {
if (!promptFilePath) {
const currentDir = path.dirname(new URL(import.meta.url).pathname);
const currentDir = path.dirname(fileURLToPath(import.meta.url));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

LGTM: Improved file path resolution for cross-platform compatibility.

The change from new URL(import.meta.url).pathname to fileURLToPath(import.meta.url) is correct and addresses the file path resolution issues mentioned in the PR objectives. This is particularly important for Windows environments where URL pathname handling can be problematic.

However, ensure consistency across the codebase with similar patterns.


🏁 Script executed:

#!/bin/bash
# Description: Check for remaining usage of the old URL pattern that should be updated for consistency
# Expected: Find any remaining instances that might need similar updates

echo "Searching for remaining instances of 'new URL(import.meta.url).pathname' pattern:"
rg -n "new URL\(import\.meta\.url\)\.pathname" --type js

echo -e "\nSearching for any URL-based path resolution patterns:"
rg -n "import\.meta\.url.*pathname" --type js

Length of output: 604


Update remaining file path resolution for cross-platform consistency

The change to fileURLToPath(import.meta.url) correctly addresses Windows pathname issues and should be applied uniformly.

• In src/utils/costTracker.js (line 59), replace the old URL-based pattern:

- const currentDir = path.dirname(new URL(import.meta.url).pathname);
+ const currentDir = path.dirname(fileURLToPath(import.meta.url));

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/utils/costTracker.js at line 59, update the file path resolution to use
path.dirname(fileURLToPath(import.meta.url)) instead of the old URL-based
pattern to ensure cross-platform consistency, especially on Windows. Replace the
existing code with this new pattern to standardize path handling.


promptFilePath = path.resolve(currentDir, '../../config/prompt.md');
}
Expand Down
5 changes: 3 additions & 2 deletions tools/ab-prompt-test
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { OpenAI } from 'openai';
import chalk from 'chalk';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { getPromptTokenCount } from '../src/utils/promptLoader.js';
import { getLanguageName } from '../src/utils/languageMapping.js';
import { buildXmlPrompt, parseXmlResponse, buildDictionaryResponse } from '../src/utils/xmlTranslation.js';
Expand Down Expand Up @@ -156,7 +157,7 @@ let modelCosts;
let fallbackPricing;

try {
const currentDir = path.dirname(new URL(import.meta.url).pathname);
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const configPath = path.resolve(currentDir, '../config/openai-pricing.json');
const costData = JSON.parse(fs.readFileSync(configPath, 'utf8'));

Expand Down Expand Up @@ -280,7 +281,7 @@ async function testTranslation(openai, testStrings, promptConfig, targetLang) {
};

// Load dictionary and find matches (like potomatic does).
const currentDir = path.dirname(new URL(import.meta.url).pathname);
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const dictionaryDir = currentDir; // Look for dictionary files in tools directory.
const dictionary = loadDictionary(dictionaryDir, targetLang, mockLogger);
const dictionaryMatches = findDictionaryMatches(batch, dictionary);
Expand Down