Skip to content

Commit

Permalink
🐛 fix(env): Fix config file cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultyou committed Oct 20, 2024
1 parent 3e93421 commit 666e5f3
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update_views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
FORCE_REGENERATE: ${{ env.FORCE_REGENERATE }}
CLI_ENV: github_actions
CLI_ENV: ci
run: npm run generate-metadata

- name: Update views
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ archive/
# Ignore local database
*.sqlite

# Ignore macOS files
.DS_Store

# Ignore local temporary files
z_diff_output.txt
z_dir2prompt_output.txt
2 changes: 1 addition & 1 deletion src/app/utils/prompt_operations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';

import { Metadata } from '../../shared/types';
import { readFileContent, readDirectory, isDirectory } from '../../shared/utils/file_operations';
import { isDirectory, readDirectory, readFileContent } from '../../shared/utils/file_operations';
import logger from '../../shared/utils/logger';
import { processPromptContent } from '../../shared/utils/prompt_operations';
import { appConfig } from '../config/app.config';
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/menu.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Command } from 'commander';

import { BaseCommand } from './base.command';
import { getConfig } from '../../shared/config';
import { hasPrompts, hasFragments } from '../utils/content.util';
import { hasFragments, hasPrompts } from '../utils/content.util';

type MenuAction = 'sync' | 'prompts' | 'fragments' | 'settings' | 'env' | 'back';

Expand Down Expand Up @@ -32,7 +32,7 @@ class MenuCommand extends BaseCommand {
{ name: 'Settings', value: 'settings' }
);

console.clear();
// console.clear();

const action = await this.showMenu<MenuAction>(
`${chalk.reset(chalk.italic(chalk.cyan('Want to manage AI prompts with ease ?')))}
Expand Down
16 changes: 10 additions & 6 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/usr/bin/env node
process.env.PROMPT_LIBRARY_ENV = 'cli';

import { input } from '@inquirer/prompts';
import { Command } from 'commander';
import dotenv from 'dotenv';

import { getConfigValue, setConfig } from '../shared/config';
import configCommand from './commands/config.command';
import envCommand from './commands/env.command';
import executeCommand from './commands/execute.command';
Expand All @@ -15,18 +14,23 @@ import promptsCommand from './commands/prompts.command';
import settingsCommand from './commands/settings.command';
import syncCommand from './commands/sync.command';
import { initDatabase } from './utils/database.util';
import { getConfig, setConfig } from '../shared/config';

process.env.CLI_ENV = 'cli';

dotenv.config();

async function ensureApiKey(): Promise<void> {
const config = getConfig();
let apiKey = getConfigValue('ANTHROPIC_API_KEY');

if (!config.ANTHROPIC_API_KEY) {
if (!apiKey) {
console.log('ANTHROPIC_API_KEY is not set.');
const apiKey = await input({ message: 'Please enter your Anthropic API key:' });
apiKey = await input({ message: 'Please enter your Anthropic API key:' });
setConfig('ANTHROPIC_API_KEY', apiKey);
}

if (!getConfigValue('ANTHROPIC_API_KEY')) {
throw new Error('Failed to set ANTHROPIC_API_KEY');
}
}

async function main(): Promise<void> {
Expand Down
2 changes: 0 additions & 2 deletions src/cli/utils/conversation.util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Updated ConversationManager class

import { processCliPromptContent, resolveCliInputs } from './prompt.cli.util';
import { getPromptFiles } from './prompt.util';
import { ApiResult } from '../../shared/types';
Expand Down
4 changes: 2 additions & 2 deletions src/cli/utils/database.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import yaml from 'js-yaml';
import NodeCache from 'node-cache';
import sqlite3, { RunResult } from 'sqlite3';

import { handleError, AppError } from './error.util';
import { AppError, handleError } from './error.util';
import { createPrompt } from './prompt.util';
import { commonConfig } from '../../shared/config/common.config';
import { ApiResult, CategoryItem, Metadata, Prompt, Variable } from '../../shared/types';
import { readDirectory, fileExists, readFileContent } from '../../shared/utils/file_operations';
import { fileExists, readDirectory, readFileContent } from '../../shared/utils/file_operations';
import logger from '../../shared/utils/logger';
import { cliConfig } from '../config/cli.config';

Expand Down
8 changes: 4 additions & 4 deletions src/cli/utils/prompt.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chalk from 'chalk';

import { runAsync, getAsync, allAsync } from './database.util';
import { allAsync, getAsync, runAsync } from './database.util';
import { readEnvVars } from './env.util';
import { getPromptMetadata } from './metadata.util';
import { Metadata, Prompt, ApiResult, Variable } from '../../shared/types';
import { ApiResult, Metadata, Prompt, Variable } from '../../shared/types';
import { processPromptContent } from '../../shared/utils/prompt_operations';
import { formatTitleCase, formatSnakeCase } from '../../shared/utils/string_formatter';
import { formatSnakeCase, formatTitleCase } from '../../shared/utils/string_formatter';

export async function createPrompt(metadata: Metadata, content: string): Promise<ApiResult<void>> {
try {
Expand Down Expand Up @@ -126,7 +126,7 @@ export async function getPromptFiles(
}

export async function viewPromptDetails(details: Prompt & { variables: Variable[] }, isExecute = false): Promise<void> {
console.clear();
// console.clear();
console.log(chalk.cyan('Prompt:'), details.title);
console.log(`\n${details.description || ''}`);
console.log(chalk.cyan('\nCategory:'), formatTitleCase(details.primary_category));
Expand Down
4 changes: 3 additions & 1 deletion src/shared/config/config.constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as os from 'os';
import * as path from 'path';

export const isCliEnvironment = process.env.PROMPT_LIBRARY_ENV === 'cli';
import { commonConfig } from './common.config';

export const isCliEnvironment = commonConfig.CLI_ENV === 'cli';

export const CONFIG_DIR = isCliEnvironment
? path.join(os.homedir(), '.prompt-library-cli')
Expand Down
54 changes: 44 additions & 10 deletions src/shared/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as fs from 'fs';

import { commonConfig, CommonConfig } from './common.config';
import { CommonConfig, commonConfig } from './common.config';
import { CONFIG_DIR, CONFIG_FILE, isCliEnvironment } from './config.constants';
import { AppConfig, appConfig } from '../../app/config/app.config';
import { CliConfig, cliConfig } from '../../cli/config/cli.config';

export type Config = CommonConfig & (CliConfig | AppConfig);

let loadedConfig: Config | null = null;

function loadConfig(): Config {
const environmentConfig = isCliEnvironment ? cliConfig : appConfig;
let config: Config = { ...commonConfig, ...environmentConfig };
Expand All @@ -16,29 +18,61 @@ function loadConfig(): Config {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}

if (!fs.existsSync(CONFIG_FILE)) {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
} else {
if (fs.existsSync(CONFIG_FILE)) {
const fileConfig = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf-8'));
config = { ...config, ...fileConfig };
}
}
return config;
}

const loadedConfig: Config = loadConfig();
export function setConfig<K extends keyof Config>(key: K, value: Config[K]): void {
if (!isCliEnvironment) {
throw new Error('setConfig is only available in CLI environment');
}

if (!loadedConfig) {
loadedConfig = loadConfig();
}

loadedConfig[key] = value;

// Update process.env to reflect the change
if (typeof value === 'string') {
process.env[key.toString()] = value;
}

// Write to file
fs.writeFileSync(CONFIG_FILE, JSON.stringify(loadedConfig, null, 2));

// Clear the cache by reassigning loadedConfig to null
loadedConfig = null as unknown as Config;
}

export function getConfig(): Readonly<Config> {
if (loadedConfig === null) {
loadedConfig = loadConfig();
}
return loadedConfig;
}

export function setConfig<K extends keyof Config>(key: K, value: Config[K]): void {
export function getConfigValue<K extends keyof Config>(key: K): Config[K] {
if (loadedConfig === null) {
loadedConfig = loadConfig();
}

if (isCliEnvironment) {
loadedConfig[key] = value;
fs.writeFileSync(CONFIG_FILE, JSON.stringify(loadedConfig, null, 2));
return loadedConfig[key];
} else {
throw new Error('setConfig is only available in CLI environment');
const envValue = process.env[key.toString()];
return (envValue !== undefined ? envValue : loadedConfig[key]) as Config[K];
}
}

export const config: Readonly<Config> = loadedConfig;
type ConfigValue = Config[keyof Config];

export const config: Readonly<Config> = new Proxy({} as Config, {
get(_, prop: string): ConfigValue {
return getConfigValue(prop as keyof Config);
}
});
4 changes: 2 additions & 2 deletions src/shared/utils/anthropic_client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Anthropic } from '@anthropic-ai/sdk';
import { Message, MessageStreamEvent } from '@anthropic-ai/sdk/resources';

import { config, getConfigValue } from '../config';
import logger from './logger';
import { config } from '../config';
import { commonConfig } from '../config/common.config';

export function initializeAnthropicClient(): Anthropic {
const apiKey = commonConfig.ANTHROPIC_API_KEY;
const apiKey = getConfigValue('ANTHROPIC_API_KEY');

if (!apiKey) {
logger.error('ANTHROPIC_API_KEY is not set in the environment.');
Expand Down

0 comments on commit 666e5f3

Please sign in to comment.