Both servers expose OpenAI-compatible /v1/chat/completions and /v1/models, so we route them through pi-ai's `openai-completions` API the same way Fireworks does — but with a per-installation base URL the user enters during onboarding instead of a hardcoded one. The shared schemas grow a `local` credential payload carrying just the baseUrl; the credential resolver surfaces it on `ResolvedCredential.baseUrl`, the runtime bridge forwards it through `prepareRun`, and the worker plumbs it into `resolveModel` so the streamed Model<openai-completions> targets the user's server. The local-cost path short-circuits to zero in cost-writer since users bill their own hardware, and the live model list is fetched from the saved baseUrl by `fetchLocalConnectorModels` (Ollama and LM Studio both speak the standard OpenAI catalog shape). Onboarding ships a new LocalProviderCard and the Settings panel a LocalForm; both default to the catalog's per-connector localhost URL and accept any custom URL.
173 lines
6.4 KiB
TypeScript
173 lines
6.4 KiB
TypeScript
import { getEnvApiKey } from "@mariozechner/pi-ai";
|
|
import type { AppDatabase } from "./database";
|
|
import { getConnectorDefinition, isPiKnownProvider, parseEnvBindingText } from "./connectors";
|
|
import type { ConnectorId } from "@shared/types";
|
|
import type { CredentialStore } from "./credentials";
|
|
|
|
interface ConnectorHealthDependencies {
|
|
db: AppDatabase;
|
|
logger: {
|
|
info: (...args: unknown[]) => void;
|
|
error: (...args: unknown[]) => void;
|
|
};
|
|
publishDomainChanged: () => void;
|
|
credentialStore: CredentialStore;
|
|
}
|
|
|
|
type HealthState = {
|
|
status: "ready" | "auth_required" | "not_installed" | "error";
|
|
authState: "ok" | "required" | "error" | "unknown";
|
|
lastError: string | null;
|
|
};
|
|
|
|
export function createConnectorHealthService({
|
|
db,
|
|
logger,
|
|
publishDomainChanged,
|
|
credentialStore,
|
|
}: ConnectorHealthDependencies) {
|
|
function randomFallbackCompanyId() {
|
|
const snapshot = db.projection.snapshot.listSnapshot();
|
|
return snapshot.companies[0]?.id ?? "00000000-0000-0000-0000-000000000000";
|
|
}
|
|
|
|
function evaluateCredential(id: ConnectorId): HealthState {
|
|
const definition = getConnectorDefinition(id);
|
|
const stored = credentialStore.read(id);
|
|
|
|
switch (definition.auth.type) {
|
|
case "oauth": {
|
|
if (stored?.kind !== "oauth") {
|
|
return { status: "auth_required", authState: "required", lastError: "OAuth login required." };
|
|
}
|
|
const creds = stored.value;
|
|
const expires = typeof creds.expires === "number" ? creds.expires : null;
|
|
if (expires !== null && expires <= Date.now()) {
|
|
return { status: "auth_required", authState: "required", lastError: "OAuth token expired." };
|
|
}
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
case "api_key": {
|
|
if (stored?.kind === "api_key") {
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
if (isPiKnownProvider(definition.provider) && getEnvApiKey(definition.provider)) {
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
// Re-parse envBindingText on every probe: it is user-editable and must
|
|
// not be cached into credentialStore. An explicit binding for the
|
|
// connector's declared envKey (non-empty value) counts as ready.
|
|
const connector = db.core.connectors.findConnector(id);
|
|
const boundValue = connector
|
|
? parseEnvBindingText(connector.envBindingText)[definition.auth.envKey]
|
|
: undefined;
|
|
if (boundValue && boundValue.length > 0) {
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
return {
|
|
status: "auth_required",
|
|
authState: "required",
|
|
lastError: `API key required (${definition.auth.envKey}).`,
|
|
};
|
|
}
|
|
case "aws": {
|
|
if (stored?.kind !== "aws") {
|
|
return { status: "auth_required", authState: "required", lastError: "AWS credentials not configured." };
|
|
}
|
|
const cfg = stored.value;
|
|
if (cfg.authMode === "profile" && !cfg.awsProfile) {
|
|
return { status: "error", authState: "error", lastError: "AWS profile missing." };
|
|
}
|
|
if (cfg.authMode === "iam" && (!cfg.accessKeyId || !cfg.secretAccessKey)) {
|
|
return { status: "error", authState: "error", lastError: "AWS IAM keys missing." };
|
|
}
|
|
if (cfg.authMode === "bearer" && !cfg.bearerToken) {
|
|
return { status: "error", authState: "error", lastError: "AWS bearer token missing." };
|
|
}
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
case "azure": {
|
|
if (stored?.kind !== "azure") {
|
|
return { status: "auth_required", authState: "required", lastError: "Azure credentials not configured." };
|
|
}
|
|
const cfg = stored.value;
|
|
if (!cfg.apiKey) {
|
|
return { status: "error", authState: "error", lastError: "Azure OpenAI API key missing." };
|
|
}
|
|
if (!cfg.baseUrl && !cfg.resourceName) {
|
|
return { status: "error", authState: "error", lastError: "Azure requires baseUrl or resourceName." };
|
|
}
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
case "gcp_adc": {
|
|
if (stored?.kind !== "gcp_adc") {
|
|
return { status: "auth_required", authState: "required", lastError: "GCP ADC not configured." };
|
|
}
|
|
const cfg = stored.value;
|
|
if (!cfg.project || !cfg.location) {
|
|
return { status: "error", authState: "error", lastError: "GCP project/location missing." };
|
|
}
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
case "local": {
|
|
if (stored?.kind !== "local") {
|
|
return { status: "auth_required", authState: "required", lastError: "Local server URL not configured." };
|
|
}
|
|
if (!stored.value.baseUrl) {
|
|
return { status: "error", authState: "error", lastError: "Local server URL missing." };
|
|
}
|
|
return { status: "ready", authState: "ok", lastError: null };
|
|
}
|
|
}
|
|
}
|
|
|
|
async function checkConnector(id: ConnectorId) {
|
|
const companyId = db.core.settings.getCurrentCompanyId() ?? randomFallbackCompanyId();
|
|
|
|
db.core.connectors.updateConnectorHealth({
|
|
id,
|
|
status: "detected",
|
|
authState: db.core.connectors.getConnector(id).authState,
|
|
version: null,
|
|
lastError: null,
|
|
});
|
|
publishDomainChanged();
|
|
|
|
try {
|
|
const result = evaluateCredential(id);
|
|
db.core.connectors.updateConnectorHealth({
|
|
id,
|
|
status: result.status,
|
|
authState: result.authState,
|
|
version: null,
|
|
lastError: result.lastError,
|
|
});
|
|
db.core.activity.addActivity({
|
|
companyId,
|
|
actor: "system",
|
|
action: "connector.tested",
|
|
entityType: "connector",
|
|
entityId: id,
|
|
detail: result.status,
|
|
});
|
|
logger.info(`[connector] Health check for ${id}: ${result.status}`);
|
|
} catch (error) {
|
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
logger.error(`[connector] Health check failed for ${id}: ${errorMessage}`);
|
|
db.core.connectors.updateConnectorHealth({
|
|
id,
|
|
status: "error",
|
|
authState: "unknown",
|
|
version: null,
|
|
lastError: errorMessage,
|
|
});
|
|
}
|
|
publishDomainChanged();
|
|
}
|
|
|
|
return {
|
|
checkConnector,
|
|
randomFallbackCompanyId,
|
|
};
|
|
}
|