| import { getApiKeyById, createApiKey } from "@/lib/localDb";
|
| import { getConsistentMachineId } from "@/shared/utils/machineId";
|
|
|
| export async function resolveApiKey(
|
| apiKeyId?: string | null,
|
| apiKey?: string | null
|
| ): Promise<string> {
|
| if (apiKeyId) {
|
| try {
|
| const keyRecord = await getApiKeyById(apiKeyId);
|
| if (keyRecord?.key) return keyRecord.key as string;
|
| } catch {
|
|
|
| }
|
| }
|
| return apiKey || "sk_omniroute";
|
| }
|
|
|
| |
| |
| |
| |
|
|
| export async function getOrCreateApiKey(apiKeyId?: string | null): Promise<string> {
|
| if (apiKeyId) {
|
| try {
|
| const keyRecord = await getApiKeyById(apiKeyId);
|
| if (keyRecord?.key) return keyRecord.key as string;
|
| } catch {
|
|
|
| }
|
| }
|
|
|
|
|
| let machineId = "unknown";
|
| try {
|
| machineId = await getConsistentMachineId();
|
| const keyRecord = await createApiKey("CLI Auto-Key", machineId);
|
| return keyRecord.key as string;
|
| } catch {
|
|
|
| return `sk-${machineId}-fallback-${Date.now()}`;
|
| }
|
| }
|
|
|