chore(repo): initialize planarchy workspace
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import OpenAI, { AzureOpenAI } from "openai";
|
||||
|
||||
type AiSettings = {
|
||||
aiProvider?: string | null;
|
||||
azureOpenAiEndpoint?: string | null;
|
||||
azureOpenAiDeployment?: string | null;
|
||||
azureOpenAiApiKey?: string | null;
|
||||
azureApiVersion?: string | null;
|
||||
aiMaxCompletionTokens?: number | null;
|
||||
aiTemperature?: number | null;
|
||||
};
|
||||
|
||||
/** Returns true if the settings have enough information to make an API call. */
|
||||
export function isAiConfigured(settings: AiSettings | null | undefined): boolean {
|
||||
if (!settings?.azureOpenAiApiKey || !settings.azureOpenAiDeployment) return false;
|
||||
if (settings.aiProvider === "azure" && !settings.azureOpenAiEndpoint) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Instantiates the right OpenAI client based on the stored provider setting. */
|
||||
export function createAiClient(settings: AiSettings): OpenAI {
|
||||
if (settings.aiProvider === "azure") {
|
||||
return new AzureOpenAI({
|
||||
endpoint: settings.azureOpenAiEndpoint!,
|
||||
apiKey: settings.azureOpenAiApiKey!,
|
||||
apiVersion: settings.azureApiVersion ?? "2025-01-01-preview",
|
||||
deployment: settings.azureOpenAiDeployment!,
|
||||
});
|
||||
}
|
||||
// Default: regular OpenAI (sk-... key)
|
||||
return new OpenAI({ apiKey: settings.azureOpenAiApiKey! });
|
||||
}
|
||||
|
||||
/** Turns raw API errors into actionable human-readable messages. */
|
||||
export function parseAiError(err: unknown): string {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
const lower = msg.toLowerCase();
|
||||
|
||||
if (lower.includes("401") || lower.includes("unauthorized") || lower.includes("invalid_api_key") || lower.includes("incorrect api key")) {
|
||||
return "Invalid API key — make sure you copied it correctly from your provider's dashboard.";
|
||||
}
|
||||
if (lower.includes("insufficient_quota") || lower.includes("exceeded your current quota") || lower.includes("billing")) {
|
||||
return "Account quota exceeded or billing issue — check your usage limits at platform.openai.com.";
|
||||
}
|
||||
if (lower.includes("403") || lower.includes("forbidden")) {
|
||||
return "Access denied — your key may not have permission to use this model/deployment.";
|
||||
}
|
||||
if (lower.includes("deploymentnotfound") || lower.includes("model_not_found") || (lower.includes("404") && lower.includes("deployment"))) {
|
||||
return "Deployment not found — check the deployment name matches exactly what's configured in Azure.";
|
||||
}
|
||||
if (lower.includes("404") || lower.includes("not found")) {
|
||||
return "Model not found — verify the model name (e.g. gpt-4o-mini) is correct and available on your account.";
|
||||
}
|
||||
if (lower.includes("429") || lower.includes("rate limit") || lower.includes("ratelimiterror")) {
|
||||
return "Rate limit exceeded — wait a moment and try again.";
|
||||
}
|
||||
if (lower.includes("econnrefused") || lower.includes("enotfound") || lower.includes("fetch failed") || lower.includes("failed to fetch")) {
|
||||
return "Cannot reach the API endpoint — check the endpoint URL and your network connection.";
|
||||
}
|
||||
if (lower.includes("context_length_exceeded") || lower.includes("maximum context")) {
|
||||
return "Request too large — the prompt exceeded the model's context limit.";
|
||||
}
|
||||
// Fall back to the raw message but strip noise
|
||||
return msg.replace(/^Error: /, "").slice(0, 300);
|
||||
}
|
||||
Reference in New Issue
Block a user