Skip to content

Commit cfbaccf

Browse files
authored
fix(app): list AI Gateway providers in Settings under live provider sync (#6719)
With liveProviderSync, the engine plugin owns the Gateway catalog and the server sync stops fetching /v1/inference-providers, so it reports no Gateway rows and no member_auth_required skips. Settings > AI Providers only reads those, so it listed no Gateway providers and no "Sign in to X" rows, and a sign-in started there polled until it timed out because the live sync pass returns handled_live. In live mode the provider-auth store now reads the member's usable Gateway providers from Den during each sync. Settings builds its organization rows and sign-in rows from that list, and a sign-in counts as connected once Den stops asking for it. The flag-off path is unchanged. Signed-off-by: OmarMcAdam <gh@mcadam.io>
1 parent a4a45c5 commit cfbaccf

7 files changed

Lines changed: 403 additions & 38 deletions

File tree

‎apps/app/src/app/lib/den.ts‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
AUTOMATION_SCHEDULE_KINDS,
1010
AUTOMATION_SCHEDULE_KINDS_HEADER,
1111
} from "@openwork/types/automations";
12-
import type { GatewayProviderSummary } from "@openwork/types/den/gateway";
12+
import type { GatewayAuthorizationRequest, GatewayProviderSummary } from "@openwork/types/den/gateway";
13+
import { parseGatewayUsableModel } from "./gateway-usable-model";
1314
import type { DesktopStartupReport } from "@openwork/types/den/diagnostics";
1415
import {
1516
parseModelRemovalInventory,
@@ -341,6 +342,12 @@ export type DenMcpToken = {
341342

342343
export type DenOrgGatewayProvider = Pick<GatewayProviderSummary, "id" | "providerId" | "name" | "source">;
343344

345+
/** A Gateway provider as this member can use it: ready models plus sets still awaiting their sign-in. */
346+
export type DenOrgUsableGatewayProvider = Pick<
347+
GatewayProviderSummary,
348+
"id" | "providerId" | "name" | "source" | "credentialStatus" | "updatedAt" | "models" | "authorizationRequests"
349+
>;
350+
344351
export type DenOrgLlmProviderModel = {
345352
id: string;
346353
name: string;
@@ -2155,6 +2162,70 @@ function getDenOrgGatewayProviders(payload: unknown): DenOrgGatewayProvider[] {
21552162
});
21562163
}
21572164

2165+
function parseDenOrgUsableGatewayProvider(value: unknown): DenOrgUsableGatewayProvider | null {
2166+
if (
2167+
!isRecord(value) ||
2168+
typeof value.id !== "string" || !/^ipr_/i.test(value.id) ||
2169+
typeof value.providerId !== "string" || !value.providerId.trim() ||
2170+
typeof value.name !== "string" || !value.name.trim() ||
2171+
value.source !== "openwork_gateway" ||
2172+
(value.credentialStatus !== "ready" && value.credentialStatus !== "member_auth_required" && value.credentialStatus !== "org_credential_missing")
2173+
) {
2174+
return null;
2175+
}
2176+
const models = Array.isArray(value.models)
2177+
? value.models.flatMap((model) => {
2178+
const parsed = parseGatewayUsableModel(model);
2179+
return parsed ? [parsed] : [];
2180+
})
2181+
: [];
2182+
const authorizationRequests: GatewayAuthorizationRequest[] = Array.isArray(value.authorizationRequests)
2183+
? value.authorizationRequests.flatMap((request) => {
2184+
if (
2185+
!isRecord(request) ||
2186+
typeof request.credentialSetId !== "string" || !/^gcs_[0-7][0-9a-hjkmnp-tv-z]{25}$/.test(request.credentialSetId) ||
2187+
typeof request.name !== "string" || !request.name.trim() ||
2188+
typeof request.authUrl !== "string" || !request.authUrl.trim()
2189+
) {
2190+
return [];
2191+
}
2192+
const credentialSetId = request.credentialSetId;
2193+
return [{
2194+
credentialSetId,
2195+
name: request.name,
2196+
authUrl: request.authUrl,
2197+
...(Array.isArray(request.models)
2198+
? { models: request.models.flatMap((model) => {
2199+
const parsed = parseGatewayUsableModel(model, credentialSetId);
2200+
return parsed ? [parsed] : [];
2201+
}) }
2202+
: {}),
2203+
}];
2204+
})
2205+
: [];
2206+
return {
2207+
id: value.id,
2208+
providerId: value.providerId,
2209+
name: value.name,
2210+
source: value.source,
2211+
credentialStatus: value.credentialStatus,
2212+
updatedAt: typeof value.updatedAt === "string" ? value.updatedAt : "",
2213+
models,
2214+
authorizationRequests,
2215+
};
2216+
}
2217+
2218+
/** Lenient for display: one malformed row is dropped rather than hiding every provider. */
2219+
function getDenOrgUsableGatewayProviders(payload: unknown): DenOrgUsableGatewayProvider[] {
2220+
if (!isRecord(payload) || !Array.isArray(payload.inferenceProviders)) {
2221+
throw new DenApiError(500, "invalid_gateway_providers_payload", "AI Gateway provider response was invalid.");
2222+
}
2223+
return payload.inferenceProviders.flatMap((provider: unknown) => {
2224+
const parsed = parseDenOrgUsableGatewayProvider(provider);
2225+
return parsed ? [parsed] : [];
2226+
});
2227+
}
2228+
21582229
const CONNECTION_READINESS_FAILURES = ["sign_in_required", "sign_in_rejected", "setup_required", "provider_setup_required", "unavailable", "timed_out", "settings_changed", "tools_unreadable"] as const;
21592230
export type DenConnectionReadinessFailure = (typeof CONNECTION_READINESS_FAILURES)[number];
21602231

@@ -3402,6 +3473,22 @@ export function createDenClient(options: {
34023473
}
34033474
},
34043475

3476+
/** The member's usable Gateway providers, with models and pending sign-ins. [] on Den servers without Gateway. */
3477+
async listOrgUsableGatewayProviders(orgId: string): Promise<DenOrgUsableGatewayProvider[]> {
3478+
try {
3479+
const payload = await requestJson<unknown>(baseUrls, "/v1/inference-providers?scope=usable", {
3480+
method: "GET",
3481+
token,
3482+
organizationId: orgId,
3483+
cache: "no-store",
3484+
});
3485+
return getDenOrgUsableGatewayProviders(payload);
3486+
} catch (error) {
3487+
if (error instanceof DenApiError && [404, 405, 501].includes(error.status)) return [];
3488+
throw error;
3489+
}
3490+
},
3491+
34053492
async listAutomations(
34063493
orgId: string,
34073494
options: { cursor?: string; limit?: number } = {},
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { GatewayUsableModel } from "@openwork/types/den/gateway";
2+
3+
const GATEWAY_MODEL_ID = /^gwm_[0-7][0-9a-hjkmnp-tv-z]{25}_[0-7][0-9a-hjkmnp-tv-z]{25}_[0-7][0-9a-hjkmnp-tv-z]{25}$/;
4+
5+
/**
6+
* One Gateway model combination, or null when any identity field is missing or
7+
* its `gwm_` id does not encode its own model group and credential set. Pass
8+
* `credentialSetId` to also require the model to belong to that set.
9+
*/
10+
export function parseGatewayUsableModel(value: unknown, credentialSetId?: unknown): GatewayUsableModel | null {
11+
if (!value || typeof value !== "object"
12+
|| !("id" in value) || typeof value.id !== "string" || !GATEWAY_MODEL_ID.test(value.id)
13+
|| !("name" in value) || typeof value.name !== "string"
14+
|| !("config" in value) || !value.config || typeof value.config !== "object" || Array.isArray(value.config)
15+
|| !("id" in value.config) || value.config.id !== value.id
16+
|| !("upstreamModelId" in value) || typeof value.upstreamModelId !== "string"
17+
|| !("modelGroupId" in value) || typeof value.modelGroupId !== "string"
18+
|| !("modelGroupName" in value) || typeof value.modelGroupName !== "string"
19+
|| !("credentialSetId" in value) || typeof value.credentialSetId !== "string"
20+
|| (credentialSetId !== undefined && value.credentialSetId !== credentialSetId)
21+
|| value.id.split("_")[2] !== value.credentialSetId.slice(4)
22+
|| value.id.split("_")[1] !== value.modelGroupId.slice(4)
23+
|| !("credentialSetName" in value) || typeof value.credentialSetName !== "string") return null;
24+
return {
25+
id: value.id, name: value.name, config: { ...value.config, id: value.id },
26+
upstreamModelId: value.upstreamModelId, modelGroupId: value.modelGroupId, modelGroupName: value.modelGroupName,
27+
credentialSetId: value.credentialSetId, credentialSetName: value.credentialSetName,
28+
};
29+
}

‎apps/app/src/app/lib/openwork-server.ts‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from "./agent-context-diagnostics-transport";
1717
import { desktopFetch, desktopFetchViaMain, desktopFetchAgentContextDiagnostics } from "./desktop";
1818
import { isOpenworkGatewayRuntime } from "./gateway-runtime";
19+
import { parseGatewayUsableModel } from "./gateway-usable-model";
1920
import { isDesktopRuntime } from "./runtime-env";
2021
import type { ExecResult, OpencodeConfigFile, WorkspaceInfo, WorkspaceList } from "./desktop";
2122
import type { DenResourceSnapshot } from "./den-types";
@@ -306,27 +307,6 @@ function parseCloudImportedProvider(value: unknown): CloudImportedProvider | nul
306307
};
307308
}
308309

309-
function parsePendingGatewayModel(value: unknown, credentialSetId: unknown): GatewayUsableModel | null {
310-
if (!value || typeof value !== "object"
311-
|| !("id" in value) || typeof value.id !== "string"
312-
|| !/^gwm_[0-7][0-9a-hjkmnp-tv-z]{25}_[0-7][0-9a-hjkmnp-tv-z]{25}_[0-7][0-9a-hjkmnp-tv-z]{25}$/.test(value.id)
313-
|| !("name" in value) || typeof value.name !== "string"
314-
|| !("config" in value) || !value.config || typeof value.config !== "object" || Array.isArray(value.config)
315-
|| !("id" in value.config) || value.config.id !== value.id
316-
|| !("upstreamModelId" in value) || typeof value.upstreamModelId !== "string"
317-
|| !("modelGroupId" in value) || typeof value.modelGroupId !== "string"
318-
|| !("modelGroupName" in value) || typeof value.modelGroupName !== "string"
319-
|| !("credentialSetId" in value) || typeof value.credentialSetId !== "string" || value.credentialSetId !== credentialSetId
320-
|| value.id.split("_")[2] !== value.credentialSetId.slice(4)
321-
|| value.id.split("_")[1] !== value.modelGroupId.slice(4)
322-
|| !("credentialSetName" in value) || typeof value.credentialSetName !== "string") return null;
323-
return {
324-
id: value.id, name: value.name, config: { ...value.config, id: value.id },
325-
upstreamModelId: value.upstreamModelId, modelGroupId: value.modelGroupId, modelGroupName: value.modelGroupName,
326-
credentialSetId: value.credentialSetId, credentialSetName: value.credentialSetName,
327-
};
328-
}
329-
330310
function parseCloudProviderSyncStatus(value: unknown): OpenworkCloudProviderSyncStatus {
331311
if (!value || typeof value !== "object" || !("hasSession" in value) || typeof value.hasSession !== "boolean" || !("providers" in value) || !Array.isArray(value.providers)) {
332312
throw new Error("Invalid cloud provider sync status response.");
@@ -367,7 +347,7 @@ function parseCloudProviderSyncStatus(value: unknown): OpenworkCloudProviderSync
367347
...("authUrl" in raw && typeof raw.authUrl === "string" ? { authUrl: raw.authUrl } : {}),
368348
...(raw.reason === "member_auth_required" && "credentialSetId" in raw && "models" in raw && Array.isArray(raw.models)
369349
? { models: raw.models.flatMap((value: unknown) => {
370-
const model = parsePendingGatewayModel(value, raw.credentialSetId);
350+
const model = parseGatewayUsableModel(value, raw.credentialSetId);
371351
return model ? [model] : [];
372352
}) } : {}),
373353
});

‎apps/app/src/react-app/domains/connections/provider-auth/cloud-provider-config.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,63 @@ export const resolveGatewayConnectProviders = (
150150
authUrl: provider.authUrl ?? null,
151151
}));
152152

153+
/** The fields of a Den Gateway provider the live-sync helpers below read. */
154+
export type LiveGatewayProvider = {
155+
id: string;
156+
providerId: string;
157+
name: string;
158+
credentialStatus: "ready" | "member_auth_required" | "org_credential_missing";
159+
updatedAt: string;
160+
models: GatewayUsableModel[];
161+
authorizationRequests: { credentialSetId: string; name: string; authUrl: string; models?: GatewayUsableModel[] }[];
162+
};
163+
164+
/**
165+
* Sign-in rows for live provider sync, built from Den's Gateway list the same
166+
* way the server sync derives its `member_auth_required` skips.
167+
*/
168+
export const resolveLiveGatewayConnectProviders = (providers: readonly LiveGatewayProvider[]): GatewayConnectProvider[] =>
169+
providers.flatMap((provider) => {
170+
const requests = provider.authorizationRequests.map((request) => ({
171+
cloudProviderId: provider.id,
172+
credentialSetId: request.credentialSetId,
173+
...(request.models === undefined ? {} : { models: request.models }),
174+
providerId: provider.id,
175+
name: `${provider.name} / ${request.name}`,
176+
authUrl: null,
177+
}));
178+
if (requests.length > 0 || provider.models.length > 0 || provider.credentialStatus !== GATEWAY_MEMBER_AUTH_REQUIRED_REASON) return requests;
179+
return [{ cloudProviderId: provider.id, providerId: provider.id, name: provider.name, authUrl: null }];
180+
});
181+
182+
/**
183+
* Ready Gateway providers in the shape Settings lists synced providers, for
184+
* live provider sync where the engine (not the server sync) delivers them.
185+
*/
186+
export const resolveLiveGatewayImportedProviders = (providers: readonly LiveGatewayProvider[]): Record<string, CloudImportedProvider> =>
187+
Object.fromEntries(providers.filter((provider) => provider.models.length > 0).map((provider) => [provider.id, {
188+
cloudProviderId: provider.id,
189+
providerId: provider.id,
190+
sourceProviderId: provider.providerId,
191+
name: provider.name,
192+
source: OPENWORK_GATEWAY_PROVIDER_SOURCE,
193+
updatedAt: provider.updatedAt || null,
194+
modelIds: provider.models.map((model) => model.id).sort(),
195+
importedAt: null,
196+
} satisfies CloudImportedProvider]));
197+
198+
/** Whether Den no longer asks this member to sign in to the row's provider or credential set. */
199+
export function isLiveGatewaySetConnected(provider: GatewayConnectProvider, providers: readonly LiveGatewayProvider[]) {
200+
const live = providers.find((entry) => entry.id === provider.cloudProviderId);
201+
if (!live) return false;
202+
if (provider.credentialSetId) {
203+
const suffix = provider.credentialSetId.slice(4);
204+
return !live.authorizationRequests.some((request) => request.credentialSetId === provider.credentialSetId)
205+
&& live.models.some((model) => model.id.split("_")[2] === suffix);
206+
}
207+
return live.models.length > 0;
208+
}
209+
153210
export function pendingGatewayModelOptions(providers: readonly GatewayConnectProvider[]): ModelOption[] {
154211
return providers.flatMap((provider) => (provider.models ?? []).flatMap((model) => {
155212
if (!provider.credentialSetId || model.credentialSetId !== provider.credentialSetId) return [];

0 commit comments

Comments
 (0)