-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔧 refactor: Simplify prompt-related files and functions
- Loading branch information
1 parent
aa92f93
commit e06502e
Showing
8 changed files
with
97 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
import { EnvVar } from '../../shared/types'; | ||
import logger from '../../shared/utils/logger.util'; | ||
import { FRAGMENT_PREFIX, ENV_PREFIX } from '../cli.constants'; | ||
import { readEnvVars } from './env.util'; | ||
import { handleError } from './error.util'; | ||
import { resolveCliInputs } from './input_processing.util'; | ||
import { processPromptContent } from '../../shared/utils/prompt_processing.util'; | ||
import { viewFragmentContent } from './fragment_operations.util'; | ||
|
||
export async function processCliPromptContent( | ||
messages: { role: string; content: string }[], | ||
inputs: Record<string, string> = {}, | ||
useStreaming: boolean = true | ||
): Promise<string> { | ||
export async function resolveValue(value: string, envVars: EnvVar[]): Promise<string> { | ||
if (value.startsWith(FRAGMENT_PREFIX)) { | ||
const [category, name] = value.split(FRAGMENT_PREFIX)[1].split('/'); | ||
const fragmentResult = await viewFragmentContent(category, name); | ||
|
||
if (fragmentResult.success && fragmentResult.data) { | ||
return fragmentResult.data; | ||
} else { | ||
logger.warn(`Failed to load fragment: ${category}/${name}`); | ||
return value; | ||
} | ||
} else if (value.startsWith(ENV_PREFIX)) { | ||
const envVarName = value.split(ENV_PREFIX)[1]; | ||
const actualEnvVar = envVars.find((v) => v.name === envVarName); | ||
|
||
if (actualEnvVar) { | ||
return await resolveValue(actualEnvVar.value, envVars); | ||
} else { | ||
logger.warn(`Env var not found: ${envVarName}`); | ||
return value; | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
export async function resolveInputs(inputs: Record<string, string>): Promise<Record<string, string>> { | ||
try { | ||
return processPromptContent(messages, inputs, useStreaming, resolveCliInputs, (event) => { | ||
if (event.type === 'content_block_delta' && event.delta) { | ||
if ('text' in event.delta) { | ||
process.stdout.write(event.delta.text); | ||
} else if ('partial_json' in event.delta) { | ||
process.stdout.write(event.delta.partial_json); | ||
} | ||
const envVarsResult = await readEnvVars(); | ||
const envVars = envVarsResult.success ? envVarsResult.data || [] : []; | ||
const resolvedInputs: Record<string, string> = {}; | ||
|
||
for (const [key, value] of Object.entries(inputs)) { | ||
if (value.startsWith(FRAGMENT_PREFIX) || value.startsWith(ENV_PREFIX)) { | ||
resolvedInputs[key] = await resolveValue(value, envVars); | ||
} else { | ||
resolvedInputs[key] = value; | ||
} | ||
}); | ||
} | ||
return resolvedInputs; | ||
} catch (error) { | ||
handleError(error, 'processing CLI prompt content'); | ||
handleError(error, 'resolving inputs'); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.