mirror of
https://github.com/pacnpal/Roo-Code.git
synced 2025-12-22 13:21:07 -05:00
Refactor out file helpers into fs.ts
This commit is contained in:
24
src/utils/cost.ts
Normal file
24
src/utils/cost.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { ModelInfo } from "../shared/api"
|
||||
|
||||
export function calculateApiCost(
|
||||
modelInfo: ModelInfo,
|
||||
inputTokens: number,
|
||||
outputTokens: number,
|
||||
cacheCreationInputTokens?: number,
|
||||
cacheReadInputTokens?: number
|
||||
): number {
|
||||
const modelCacheWritesPrice = modelInfo.cacheWritesPrice
|
||||
let cacheWritesCost = 0
|
||||
if (cacheCreationInputTokens && modelCacheWritesPrice) {
|
||||
cacheWritesCost = (modelCacheWritesPrice / 1_000_000) * cacheCreationInputTokens
|
||||
}
|
||||
const modelCacheReadsPrice = modelInfo.cacheReadsPrice
|
||||
let cacheReadsCost = 0
|
||||
if (cacheReadInputTokens && modelCacheReadsPrice) {
|
||||
cacheReadsCost = (modelCacheReadsPrice / 1_000_000) * cacheReadInputTokens
|
||||
}
|
||||
const baseInputCost = (modelInfo.inputPrice / 1_000_000) * inputTokens
|
||||
const outputCost = (modelInfo.outputPrice / 1_000_000) * outputTokens
|
||||
const totalCost = cacheWritesCost + cacheReadsCost + baseInputCost + outputCost
|
||||
return totalCost
|
||||
}
|
||||
47
src/utils/fs.ts
Normal file
47
src/utils/fs.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import fs from "fs/promises"
|
||||
import * as path from "path"
|
||||
|
||||
/**
|
||||
* Asynchronously creates all non-existing subdirectories for a given file path
|
||||
* and collects them in an array for later deletion.
|
||||
*
|
||||
* @param filePath - The full path to a file.
|
||||
* @returns A promise that resolves to an array of newly created directories.
|
||||
*/
|
||||
export async function createDirectoriesForFile(filePath: string): Promise<string[]> {
|
||||
const newDirectories: string[] = []
|
||||
const normalizedFilePath = path.normalize(filePath) // Normalize path for cross-platform compatibility
|
||||
const directoryPath = path.dirname(normalizedFilePath)
|
||||
|
||||
let currentPath = directoryPath
|
||||
const dirsToCreate: string[] = []
|
||||
|
||||
// Traverse up the directory tree and collect missing directories
|
||||
while (!(await fileExistsAtPath(currentPath))) {
|
||||
dirsToCreate.push(currentPath)
|
||||
currentPath = path.dirname(currentPath)
|
||||
}
|
||||
|
||||
// Create directories from the topmost missing one down to the target directory
|
||||
for (let i = dirsToCreate.length - 1; i >= 0; i--) {
|
||||
await fs.mkdir(dirsToCreate[i])
|
||||
newDirectories.push(dirsToCreate[i])
|
||||
}
|
||||
|
||||
return newDirectories
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to check if a path exists.
|
||||
*
|
||||
* @param path - The path to check.
|
||||
* @returns A promise that resolves to true if the path exists, false otherwise.
|
||||
*/
|
||||
export async function fileExistsAtPath(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await fs.access(filePath)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user