@opengeni/worker-bundle 0.23.4 → 0.26.0-canary.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/activities/agent-turn/admission.d.ts +17 -2
- package/dist/activities/agent-turn/agent-build.d.ts +3 -10
- package/dist/activities/agent-turn/claim.d.ts +8 -0
- package/dist/activities/agent-turn/codex.d.ts +2 -2
- package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
- package/dist/activities/agent-turn/errors.d.ts +1 -1
- package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
- package/dist/activities/agent-turn/list-models.d.ts +16 -0
- package/dist/activities/agent-turn/model-usage.d.ts +15 -1
- package/dist/activities/agent-turn/quiescence.d.ts +11 -6
- package/dist/activities/agent-turn/run.d.ts +9 -0
- package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
- package/dist/activities/agent-turn/session-title.d.ts +21 -0
- package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +4 -13
- package/dist/activities/agent-turn/tool-policy.d.ts +40 -5
- package/dist/activities/agent-turn/turn-context.d.ts +13 -2
- package/dist/activities/agent-turn.d.ts +1 -1
- package/dist/activities/automations.d.ts +1 -0
- package/dist/activities/capabilities.d.ts +3 -1
- package/dist/activities/codemode-dispatcher.d.ts +10 -1
- package/dist/activities/codex-capacity.d.ts +32 -3
- package/dist/activities/codex-fleet-shadow.d.ts +1 -1
- package/dist/activities/codex-image-generation.d.ts +1 -1
- package/dist/activities/codex-rotation.d.ts +8 -67
- package/dist/activities/context-compaction.d.ts +2 -0
- package/dist/activities/goals.d.ts +12 -1
- package/dist/activities/google-drive-publication.d.ts +2 -13
- package/dist/activities/image-generation-operation.d.ts +13 -4
- package/dist/activities/image-generation-references.d.ts +2 -1
- package/dist/activities/scheduled-tasks.d.ts +1 -0
- package/dist/activities/session-state.d.ts +4 -2
- package/dist/activities/types.d.ts +19 -1
- package/dist/{activities-control-52W5IDWM.js → activities-control-CTEMLTHG.js} +339 -195
- package/dist/activities-control-CTEMLTHG.js.map +1 -0
- package/dist/activities-control.d.ts +4 -0
- package/dist/{activities-turn-A2G2KLSY.js → activities-turn-S5IU347A.js} +1846 -1136
- package/dist/activities-turn-S5IU347A.js.map +1 -0
- package/dist/activities.d.ts +4 -0
- package/dist/{chunk-GFNXVZVQ.js → chunk-D4ZEFOYH.js} +107 -7
- package/dist/chunk-D4ZEFOYH.js.map +1 -0
- package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
- package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
- package/dist/{chunk-IPVMCAW5.js → chunk-TAUE3GYT.js} +225 -154
- package/dist/chunk-TAUE3GYT.js.map +1 -0
- package/dist/github-rest-mcp.d.ts +0 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +43 -7
- package/dist/index.js.map +1 -1
- package/dist/observability-metrics.d.ts +35 -8
- package/dist/sandbox-reaper-timeout.d.ts +2 -2
- package/dist/sandbox-resume.d.ts +6 -1
- package/dist/workflow-bundle.js +96 -10
- package/dist/workflows/activities.d.ts +6 -1
- package/dist/workflows/automations.d.ts +5 -0
- package/dist/workflows/session.d.ts +3 -0
- package/package.json +17 -17
- package/src/activities/agent-run-admission.ts +7 -1
- package/src/activities/agent-turn/admission.ts +47 -6
- package/src/activities/agent-turn/agent-build.ts +20 -26
- package/src/activities/agent-turn/claim.ts +59 -6
- package/src/activities/agent-turn/codex-capacity.ts +311 -234
- package/src/activities/agent-turn/codex.ts +2 -2
- package/src/activities/agent-turn/compaction-prep.ts +8 -1
- package/src/activities/agent-turn/credential-leases.ts +36 -0
- package/src/activities/agent-turn/errors.ts +29 -1
- package/src/activities/agent-turn/failure-settlement.ts +451 -323
- package/src/activities/agent-turn/finalization.ts +31 -17
- package/src/activities/agent-turn/list-models.ts +101 -0
- package/src/activities/agent-turn/model-usage.ts +120 -45
- package/src/activities/agent-turn/quiescence.ts +32 -17
- package/src/activities/agent-turn/run.ts +98 -9
- package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
- package/src/activities/agent-turn/session-title.ts +55 -2
- package/src/activities/agent-turn/stream-attempt.ts +281 -136
- package/src/activities/agent-turn/tool-environment.ts +132 -39
- package/src/activities/agent-turn/tool-policy.ts +71 -12
- package/src/activities/agent-turn/turn-context.ts +21 -2
- package/src/activities/agent-turn.ts +5 -1
- package/src/activities/automations.ts +122 -67
- package/src/activities/capabilities.ts +129 -2
- package/src/activities/codemode-dispatcher.ts +235 -59
- package/src/activities/codex-capacity.ts +89 -10
- package/src/activities/codex-fleet-shadow.ts +8 -1
- package/src/activities/codex-image-generation.ts +19 -2
- package/src/activities/codex-rotation.ts +45 -142
- package/src/activities/context-compaction.ts +3 -0
- package/src/activities/goals.ts +116 -39
- package/src/activities/google-drive-publication.ts +4 -81
- package/src/activities/image-generation-operation.ts +28 -3
- package/src/activities/image-generation-references.ts +9 -9
- package/src/activities/mcp-credentials.ts +53 -0
- package/src/activities/rig-verification.ts +5 -3
- package/src/activities/sandbox-lease.ts +29 -12
- package/src/activities/scheduled-tasks.ts +217 -110
- package/src/activities/session-state.ts +17 -0
- package/src/activities/site-auth-maintenance.ts +12 -12
- package/src/activities/types.ts +22 -8
- package/src/activities.ts +2 -0
- package/src/activity-services.ts +4 -1
- package/src/github-rest-mcp.ts +2 -83
- package/src/index.ts +34 -4
- package/src/observability-metrics.ts +158 -14
- package/src/sandbox-reaper-timeout.ts +10 -4
- package/src/sandbox-resume.ts +58 -8
- package/src/sandbox-routing.ts +71 -16
- package/src/workflows/activities.ts +16 -0
- package/src/workflows/automations.ts +21 -2
- package/src/workflows/session.ts +85 -7
- package/dist/activities/api-integrations.d.ts +0 -21
- package/dist/activities-control-52W5IDWM.js.map +0 -1
- package/dist/activities-turn-A2G2KLSY.js.map +0 -1
- package/dist/chunk-GFNXVZVQ.js.map +0 -1
- package/dist/chunk-IPVMCAW5.js.map +0 -1
- package/src/activities/api-integrations.ts +0 -141
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
resolveTurnExecutionPolicyV1,
|
|
3
|
+
WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
|
|
4
|
+
WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
|
|
5
|
+
} from "@opengeni/config";
|
|
2
6
|
import {
|
|
3
7
|
assertWorkspaceModelPolicyAllows,
|
|
4
8
|
canonicalConfiguredModel,
|
|
5
9
|
createAndStartSessionWithOutcome,
|
|
6
10
|
recordWorkspaceUsage,
|
|
11
|
+
resolveCatalogSettings,
|
|
12
|
+
resolveWorkspaceCatalogSettings,
|
|
7
13
|
} from "@opengeni/core";
|
|
8
14
|
import {
|
|
9
15
|
AutomationAuthorityRevokedError,
|
|
@@ -40,10 +46,23 @@ export function createAutomationActivities(
|
|
|
40
46
|
const assertModelPolicy = options.assertModelPolicy ?? assertWorkspaceModelPolicyAllows;
|
|
41
47
|
const recordUsage = options.recordUsage ?? recordWorkspaceUsage;
|
|
42
48
|
return {
|
|
49
|
+
settleAutomationRunFailure: async (input: DispatchAutomationRunInput): Promise<void> => {
|
|
50
|
+
const service = await services();
|
|
51
|
+
await settle(service.db, {
|
|
52
|
+
workspaceId: input.workspaceId,
|
|
53
|
+
runId: input.runId,
|
|
54
|
+
status: "failed",
|
|
55
|
+
errorCode: "dispatch_failed",
|
|
56
|
+
});
|
|
57
|
+
},
|
|
43
58
|
dispatchAutomationRun: async (
|
|
44
59
|
input: DispatchAutomationRunInput,
|
|
45
60
|
): Promise<DispatchAutomationRunResult> => {
|
|
46
61
|
const service = await services();
|
|
62
|
+
const catalogSourceSettings = service.catalogSourceSettings ?? service.settings;
|
|
63
|
+
const deploymentCatalogSettings = (
|
|
64
|
+
await resolveCatalogSettings(service.db, catalogSourceSettings)
|
|
65
|
+
).settings;
|
|
47
66
|
const run = await claim(service.db, input);
|
|
48
67
|
if (!run) return { action: "not_found" };
|
|
49
68
|
if (run.status === "dispatched") {
|
|
@@ -51,71 +70,102 @@ export function createAutomationActivities(
|
|
|
51
70
|
return { action: "already_dispatched", sessionId: run.sessionId };
|
|
52
71
|
}
|
|
53
72
|
if (run.status === "skipped") {
|
|
54
|
-
return {
|
|
73
|
+
return {
|
|
74
|
+
action: "skipped",
|
|
75
|
+
reason: run.errorCode ?? "authority_revoked",
|
|
76
|
+
};
|
|
55
77
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
accepted.accountId !== input.accountId ||
|
|
60
|
-
accepted.workspaceId !== input.workspaceId ||
|
|
61
|
-
accepted.sourceId !== run.sourceId ||
|
|
62
|
-
accepted.triggerId !== run.triggerId ||
|
|
63
|
-
accepted.triggerRevision !== run.triggerRevision ||
|
|
64
|
-
accepted.eventId !== run.eventId
|
|
65
|
-
) {
|
|
66
|
-
await settle(service.db, {
|
|
67
|
-
workspaceId: input.workspaceId,
|
|
68
|
-
runId: run.id,
|
|
69
|
-
status: "skipped",
|
|
70
|
-
errorCode: "accepted_execution_mismatch",
|
|
71
|
-
});
|
|
72
|
-
return { action: "skipped", reason: "accepted_execution_mismatch" };
|
|
78
|
+
if (run.status === "failed") {
|
|
79
|
+
return { action: "failed", reason: run.errorCode ?? "dispatch_failed" };
|
|
73
80
|
}
|
|
74
81
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
82
|
+
try {
|
|
83
|
+
const accepted = run.acceptedExecution;
|
|
84
|
+
if (
|
|
85
|
+
accepted.accountId !== input.accountId ||
|
|
86
|
+
accepted.workspaceId !== input.workspaceId ||
|
|
87
|
+
accepted.sourceId !== run.sourceId ||
|
|
88
|
+
accepted.triggerId !== run.triggerId ||
|
|
89
|
+
accepted.triggerRevision !== run.triggerRevision ||
|
|
90
|
+
accepted.eventId !== run.eventId
|
|
91
|
+
) {
|
|
92
|
+
await settle(service.db, {
|
|
93
|
+
workspaceId: input.workspaceId,
|
|
94
|
+
runId: run.id,
|
|
95
|
+
status: "skipped",
|
|
96
|
+
errorCode: "accepted_execution_mismatch",
|
|
97
|
+
});
|
|
98
|
+
return { action: "skipped", reason: "accepted_execution_mismatch" };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const template = accepted.sessionTemplate;
|
|
102
|
+
const requestedModel = template.model ?? deploymentCatalogSettings.openaiModel;
|
|
103
|
+
const catalogSettings =
|
|
104
|
+
requestedModel.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
|
|
105
|
+
requestedModel.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX)
|
|
106
|
+
? (
|
|
107
|
+
await resolveWorkspaceCatalogSettings(service.db, catalogSourceSettings, {
|
|
108
|
+
accountId: input.accountId,
|
|
109
|
+
workspaceId: input.workspaceId,
|
|
110
|
+
retainedProductModelId: requestedModel,
|
|
111
|
+
})
|
|
112
|
+
).settings
|
|
113
|
+
: deploymentCatalogSettings;
|
|
114
|
+
const catalogService = { ...service, settings: catalogSettings };
|
|
115
|
+
let model: string;
|
|
116
|
+
try {
|
|
117
|
+
const configuredModel = canonicalConfiguredModel(catalogSettings, requestedModel);
|
|
118
|
+
if (!configuredModel) throw new Error("automation has no configured model");
|
|
119
|
+
await assertModelPolicy(service.db, catalogSettings, input.workspaceId, configuredModel);
|
|
120
|
+
model = configuredModel;
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (!isUnprocessableEntity(error)) throw error;
|
|
123
|
+
await settle(service.db, {
|
|
124
|
+
workspaceId: input.workspaceId,
|
|
125
|
+
runId: run.id,
|
|
126
|
+
status: "failed",
|
|
127
|
+
errorCode: "dispatch_failed",
|
|
128
|
+
});
|
|
129
|
+
return { action: "failed", reason: "dispatch_failed" };
|
|
130
|
+
}
|
|
131
|
+
const denial = await admit(catalogService, {
|
|
132
|
+
accountId: input.accountId,
|
|
90
133
|
workspaceId: input.workspaceId,
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
errorCode: denial,
|
|
134
|
+
model,
|
|
135
|
+
requestedAgentRuns: 1,
|
|
94
136
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
137
|
+
if (denial) {
|
|
138
|
+
await settle(service.db, {
|
|
139
|
+
workspaceId: input.workspaceId,
|
|
140
|
+
runId: run.id,
|
|
141
|
+
status: "failed",
|
|
142
|
+
errorCode: denial,
|
|
143
|
+
});
|
|
144
|
+
return { action: "failed", reason: denial };
|
|
145
|
+
}
|
|
146
|
+
const reasoningEffort = template.reasoningEffort ?? catalogSettings.openaiReasoningEffort;
|
|
147
|
+
const sandboxBackend = template.sandboxBackend ?? catalogSettings.sandboxBackend;
|
|
148
|
+
if (sandboxBackend === "selfhosted") {
|
|
149
|
+
await settle(service.db, {
|
|
150
|
+
workspaceId: input.workspaceId,
|
|
151
|
+
runId: run.id,
|
|
152
|
+
status: "skipped",
|
|
153
|
+
errorCode: "interactive_compute_not_allowed",
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
action: "skipped",
|
|
157
|
+
reason: "interactive_compute_not_allowed",
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
const turnExecutionPolicy = resolveTurnExecutionPolicyV1(catalogSettings, {
|
|
161
|
+
modelId: model,
|
|
162
|
+
requestedModelId: template.model,
|
|
163
|
+
modelSource: template.model ? "explicit" : "deployment",
|
|
164
|
+
reasoningEffort,
|
|
165
|
+
reasoningSource: template.reasoningEffort ? "explicit" : "deployment",
|
|
166
|
+
latencyMode: "standard",
|
|
167
|
+
latencyModeSource: "deployment",
|
|
105
168
|
});
|
|
106
|
-
return { action: "skipped", reason: "interactive_compute_not_allowed" };
|
|
107
|
-
}
|
|
108
|
-
const turnExecutionPolicy = resolveTurnExecutionPolicyV1(service.settings, {
|
|
109
|
-
modelId: model,
|
|
110
|
-
requestedModelId: template.model,
|
|
111
|
-
modelSource: template.model ? "explicit" : "deployment",
|
|
112
|
-
reasoningEffort,
|
|
113
|
-
reasoningSource: template.reasoningEffort ? "explicit" : "deployment",
|
|
114
|
-
latencyMode: "standard",
|
|
115
|
-
latencyModeSource: "deployment",
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
try {
|
|
119
169
|
const created = await createSession({
|
|
120
170
|
db: service.db,
|
|
121
171
|
bus: service.bus,
|
|
@@ -163,11 +213,14 @@ export function createAutomationActivities(
|
|
|
163
213
|
policyRole: template.policyRole,
|
|
164
214
|
firstPartyMcpPermissions: template.firstPartyMcpPermissions,
|
|
165
215
|
firstPartyMcpTools: template.firstPartyMcpTools,
|
|
216
|
+
retainWorkspaceCustomModel:
|
|
217
|
+
model.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) ||
|
|
218
|
+
model.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX),
|
|
166
219
|
createIdempotencyKey: `automation-run:${run.id}`,
|
|
167
220
|
subjectId: accepted.serviceSubjectId,
|
|
168
221
|
});
|
|
169
222
|
await recordUsage(
|
|
170
|
-
{ db: service.db, settings:
|
|
223
|
+
{ db: service.db, settings: catalogSettings },
|
|
171
224
|
{
|
|
172
225
|
accountId: input.accountId,
|
|
173
226
|
workspaceId: input.workspaceId,
|
|
@@ -201,14 +254,16 @@ export function createAutomationActivities(
|
|
|
201
254
|
});
|
|
202
255
|
return { action: "skipped", reason: "authority_revoked" };
|
|
203
256
|
}
|
|
204
|
-
await settle(service.db, {
|
|
205
|
-
workspaceId: input.workspaceId,
|
|
206
|
-
runId: run.id,
|
|
207
|
-
status: "failed",
|
|
208
|
-
errorCode: "dispatch_failed",
|
|
209
|
-
});
|
|
210
257
|
throw error;
|
|
211
258
|
}
|
|
212
259
|
},
|
|
213
260
|
};
|
|
214
261
|
}
|
|
262
|
+
|
|
263
|
+
function isUnprocessableEntity(error: unknown): boolean {
|
|
264
|
+
return (
|
|
265
|
+
error instanceof Error &&
|
|
266
|
+
"status" in error &&
|
|
267
|
+
(error as Error & { status?: unknown }).status === 422
|
|
268
|
+
);
|
|
269
|
+
}
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
import {
|
|
2
2
|
environmentsEncryptionKeyBytes,
|
|
3
3
|
type Settings,
|
|
4
|
+
WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
|
|
5
|
+
WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
|
|
4
6
|
withCodexCatalogProvider,
|
|
5
7
|
withWorkspaceGatewayCatalogProvider,
|
|
6
8
|
withWorkspaceGatewayCredential,
|
|
9
|
+
withWorkspaceOpenRouterCatalogProvider,
|
|
10
|
+
withWorkspaceOpenRouterCredential,
|
|
11
|
+
withOrganizationGatewayCatalogProvider,
|
|
12
|
+
withOrganizationGatewayCredential,
|
|
13
|
+
withOrganizationOpenRouterCatalogProvider,
|
|
14
|
+
withOrganizationOpenRouterCredential,
|
|
15
|
+
ORGANIZATION_GATEWAY_MODEL_ID_PREFIX,
|
|
16
|
+
ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX,
|
|
7
17
|
withXaiSubscriptionCatalogProvider,
|
|
8
18
|
} from "@opengeni/config";
|
|
9
19
|
import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
|
|
10
20
|
import {
|
|
21
|
+
getWorkspaceGatewayCustomModelForExecution,
|
|
22
|
+
getWorkspaceOpenRouterCustomModelForExecution,
|
|
11
23
|
listSessionMcpServerMetadata,
|
|
12
24
|
listSessionMcpServersForRun,
|
|
25
|
+
listWorkspaceGatewayCustomModels,
|
|
26
|
+
listWorkspaceOpenRouterCustomModels,
|
|
13
27
|
workspaceCodexSubscriptionActive,
|
|
14
28
|
loadWorkspaceVercelAiGatewayApiKey,
|
|
29
|
+
loadWorkspaceOpenRouterApiKey,
|
|
30
|
+
listOrganizationModelProviderCustomModelsForWorkspace,
|
|
31
|
+
getOrganizationModelProviderCustomModelForExecution,
|
|
32
|
+
loadOrganizationModelProviderApiKey,
|
|
15
33
|
type Database,
|
|
16
34
|
type SessionMcpServerForRun,
|
|
17
35
|
} from "@opengeni/db";
|
|
@@ -126,10 +144,119 @@ export function withXaiSubscriptionProvider(settings: Settings): Settings {
|
|
|
126
144
|
|
|
127
145
|
export async function settingsWithWorkspaceGatewayCredential(
|
|
128
146
|
db: Database,
|
|
147
|
+
accountId: string,
|
|
129
148
|
workspaceId: string,
|
|
130
149
|
settings: Settings,
|
|
150
|
+
retainedProductModelId?: string | null,
|
|
131
151
|
): Promise<Settings> {
|
|
132
|
-
const
|
|
152
|
+
const activeCustomModels = await listWorkspaceGatewayCustomModels(db, { accountId, workspaceId });
|
|
153
|
+
const retainedUpstreamModelId = retainedProductModelId?.startsWith(
|
|
154
|
+
WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
|
|
155
|
+
)
|
|
156
|
+
? retainedProductModelId.slice(WORKSPACE_GATEWAY_MODEL_ID_PREFIX.length)
|
|
157
|
+
: null;
|
|
158
|
+
const retainedCustomModel = retainedUpstreamModelId
|
|
159
|
+
? await getWorkspaceGatewayCustomModelForExecution(db, {
|
|
160
|
+
accountId,
|
|
161
|
+
workspaceId,
|
|
162
|
+
upstreamModelId: retainedUpstreamModelId,
|
|
163
|
+
})
|
|
164
|
+
: null;
|
|
165
|
+
const customModels =
|
|
166
|
+
retainedCustomModel &&
|
|
167
|
+
!activeCustomModels.some(
|
|
168
|
+
(model) => model.upstreamModelId === retainedCustomModel.upstreamModelId,
|
|
169
|
+
)
|
|
170
|
+
? [...activeCustomModels, retainedCustomModel]
|
|
171
|
+
: activeCustomModels;
|
|
172
|
+
const catalogSettings = withWorkspaceGatewayCatalogProvider(settings, customModels);
|
|
133
173
|
const apiKey = await loadWorkspaceVercelAiGatewayApiKey(db, settings, workspaceId);
|
|
134
|
-
return apiKey
|
|
174
|
+
return apiKey
|
|
175
|
+
? withWorkspaceGatewayCredential(catalogSettings, apiKey, customModels)
|
|
176
|
+
: catalogSettings;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export async function settingsWithWorkspaceOpenRouterCredential(
|
|
180
|
+
db: Database,
|
|
181
|
+
accountId: string,
|
|
182
|
+
workspaceId: string,
|
|
183
|
+
settings: Settings,
|
|
184
|
+
retainedProductModelId?: string | null,
|
|
185
|
+
): Promise<Settings> {
|
|
186
|
+
const activeCustomModels = await listWorkspaceOpenRouterCustomModels(db, {
|
|
187
|
+
accountId,
|
|
188
|
+
workspaceId,
|
|
189
|
+
});
|
|
190
|
+
const retainedUpstreamModelId = retainedProductModelId?.startsWith(
|
|
191
|
+
WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
|
|
192
|
+
)
|
|
193
|
+
? retainedProductModelId.slice(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX.length)
|
|
194
|
+
: null;
|
|
195
|
+
const retainedCustomModel = retainedUpstreamModelId
|
|
196
|
+
? await getWorkspaceOpenRouterCustomModelForExecution(db, {
|
|
197
|
+
accountId,
|
|
198
|
+
workspaceId,
|
|
199
|
+
upstreamModelId: retainedUpstreamModelId,
|
|
200
|
+
})
|
|
201
|
+
: null;
|
|
202
|
+
const customModels =
|
|
203
|
+
retainedCustomModel &&
|
|
204
|
+
!activeCustomModels.some(
|
|
205
|
+
(model) => model.upstreamModelId === retainedCustomModel.upstreamModelId,
|
|
206
|
+
)
|
|
207
|
+
? [...activeCustomModels, retainedCustomModel]
|
|
208
|
+
: activeCustomModels;
|
|
209
|
+
const catalogSettings = withWorkspaceOpenRouterCatalogProvider(settings, customModels);
|
|
210
|
+
const apiKey = await loadWorkspaceOpenRouterApiKey(db, settings, workspaceId);
|
|
211
|
+
return apiKey
|
|
212
|
+
? withWorkspaceOpenRouterCredential(catalogSettings, apiKey, customModels)
|
|
213
|
+
: catalogSettings;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export async function settingsWithOrganizationProviderCredentials(
|
|
217
|
+
db: Database,
|
|
218
|
+
accountId: string,
|
|
219
|
+
workspaceId: string,
|
|
220
|
+
settings: Settings,
|
|
221
|
+
retainedProductModelId?: string | null,
|
|
222
|
+
): Promise<Settings> {
|
|
223
|
+
const buildModels = async (providerKind: "vercel_gateway" | "openrouter", prefix: string) => {
|
|
224
|
+
const active = await listOrganizationModelProviderCustomModelsForWorkspace(db, {
|
|
225
|
+
accountId,
|
|
226
|
+
workspaceId,
|
|
227
|
+
providerKind,
|
|
228
|
+
});
|
|
229
|
+
const upstreamModelId = retainedProductModelId?.startsWith(prefix)
|
|
230
|
+
? retainedProductModelId.slice(prefix.length)
|
|
231
|
+
: null;
|
|
232
|
+
const retained = upstreamModelId
|
|
233
|
+
? await getOrganizationModelProviderCustomModelForExecution(db, {
|
|
234
|
+
accountId,
|
|
235
|
+
workspaceId,
|
|
236
|
+
providerKind,
|
|
237
|
+
upstreamModelId,
|
|
238
|
+
})
|
|
239
|
+
: null;
|
|
240
|
+
return retained && !active.some((model) => model.id === retained.id)
|
|
241
|
+
? [...active, retained]
|
|
242
|
+
: active;
|
|
243
|
+
};
|
|
244
|
+
const gatewayModels = await buildModels("vercel_gateway", ORGANIZATION_GATEWAY_MODEL_ID_PREFIX);
|
|
245
|
+
const openRouterModels = await buildModels("openrouter", ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX);
|
|
246
|
+
const gatewayKey = await loadOrganizationModelProviderApiKey(db, settings, {
|
|
247
|
+
accountId,
|
|
248
|
+
workspaceId,
|
|
249
|
+
providerKind: "vercel_gateway",
|
|
250
|
+
});
|
|
251
|
+
const gatewaySettings = gatewayKey
|
|
252
|
+
? withOrganizationGatewayCredential(settings, gatewayKey, gatewayModels)
|
|
253
|
+
: withOrganizationGatewayCatalogProvider(settings, gatewayModels);
|
|
254
|
+
const openRouterKey = await loadOrganizationModelProviderApiKey(db, settings, {
|
|
255
|
+
accountId,
|
|
256
|
+
workspaceId,
|
|
257
|
+
providerKind: "openrouter",
|
|
258
|
+
});
|
|
259
|
+
return openRouterKey
|
|
260
|
+
? withOrganizationOpenRouterCredential(gatewaySettings, openRouterKey, openRouterModels)
|
|
261
|
+
: withOrganizationOpenRouterCatalogProvider(gatewaySettings, openRouterModels);
|
|
135
262
|
}
|