@mastra/factory 0.29.0-alpha.5 → 0.29.0-alpha.7
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/README.md +55 -31
- package/dist/boards/semantics.d.ts.map +1 -1
- package/dist/boards/semantics.js +7 -1
- package/dist/boards/semantics.js.map +1 -1
- package/dist/factory.d.ts +10 -0
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +98 -41
- package/dist/factory.js.map +1 -1
- package/dist/integrations/github/issue-intake.d.ts +15 -0
- package/dist/integrations/github/issue-intake.d.ts.map +1 -0
- package/dist/integrations/github/issue-intake.js +50 -0
- package/dist/integrations/github/issue-intake.js.map +1 -0
- package/dist/integrations/github/issue-reconciler.d.ts.map +1 -1
- package/dist/integrations/github/issue-reconciler.js +10 -1
- package/dist/integrations/github/issue-reconciler.js.map +1 -1
- package/dist/integrations/github/rules.d.ts +2 -0
- package/dist/integrations/github/rules.d.ts.map +1 -1
- package/dist/integrations/github/rules.js +3 -0
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/platform/connect/routes.d.ts +43 -1
- package/dist/integrations/platform/connect/routes.d.ts.map +1 -1
- package/dist/integrations/platform/connect/routes.js +82 -7
- package/dist/integrations/platform/connect/routes.js.map +1 -1
- package/dist/integrations/platform/connect/session-tools.d.ts +12 -0
- package/dist/integrations/platform/connect/session-tools.d.ts.map +1 -1
- package/dist/integrations/platform/connect/session-tools.js +28 -10
- package/dist/integrations/platform/connect/session-tools.js.map +1 -1
- package/dist/integrations/platform/vanta/integration.d.ts.map +1 -1
- package/dist/integrations/platform/vanta/integration.js +3 -3
- package/dist/integrations/platform/vanta/integration.js.map +1 -1
- package/dist/integrations/vanta/sync.d.ts +9 -0
- package/dist/integrations/vanta/sync.d.ts.map +1 -1
- package/dist/integrations/vanta/sync.js +10 -1
- package/dist/integrations/vanta/sync.js.map +1 -1
- package/dist/routes/board-snapshot.d.ts +5 -5
- package/dist/routes/board-snapshot.d.ts.map +1 -1
- package/dist/routes/board-snapshot.js +10 -14
- package/dist/routes/board-snapshot.js.map +1 -1
- package/dist/routes/config.d.ts +11 -2
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +242 -75
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/contracts.d.ts +43 -14
- package/dist/routes/contracts.d.ts.map +1 -1
- package/dist/routes/contracts.js +27 -17
- package/dist/routes/contracts.js.map +1 -1
- package/dist/routes/custom-endpoint-url.d.ts +8 -0
- package/dist/routes/custom-endpoint-url.d.ts.map +1 -0
- package/dist/routes/custom-endpoint-url.js +45 -0
- package/dist/routes/custom-endpoint-url.js.map +1 -0
- package/dist/routes/custom-provider-source.d.ts +24 -8
- package/dist/routes/custom-provider-source.d.ts.map +1 -1
- package/dist/routes/custom-provider-source.js +93 -43
- package/dist/routes/custom-provider-source.js.map +1 -1
- package/dist/routes/intake.d.ts +6 -0
- package/dist/routes/intake.d.ts.map +1 -1
- package/dist/routes/intake.js +20 -0
- package/dist/routes/intake.js.map +1 -1
- package/dist/routes/project-sessions.d.ts +15 -3
- package/dist/routes/project-sessions.d.ts.map +1 -1
- package/dist/routes/project-sessions.js +56 -14
- package/dist/routes/project-sessions.js.map +1 -1
- package/dist/routes/projects.d.ts +6 -1
- package/dist/routes/projects.d.ts.map +1 -1
- package/dist/routes/projects.js +19 -3
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/provider-credentials.d.ts +10 -1
- package/dist/routes/provider-credentials.d.ts.map +1 -1
- package/dist/routes/provider-credentials.js +13 -16
- package/dist/routes/provider-credentials.js.map +1 -1
- package/dist/routes/session-rows.d.ts +1 -1
- package/dist/routes/session-rows.d.ts.map +1 -1
- package/dist/routes/session-rows.js +38 -42
- package/dist/routes/session-rows.js.map +1 -1
- package/dist/routes/source-control-sessions.d.ts.map +1 -1
- package/dist/routes/source-control-sessions.js +4 -4
- package/dist/routes/source-control-sessions.js.map +1 -1
- package/dist/routes/surface.d.ts +3 -0
- package/dist/routes/surface.d.ts.map +1 -1
- package/dist/routes/surface.js +20 -7
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/tenant-credentials.d.ts.map +1 -1
- package/dist/routes/tenant-credentials.js +4 -4
- package/dist/routes/tenant-credentials.js.map +1 -1
- package/dist/routes/work-items.d.ts +9 -3
- package/dist/routes/work-items.d.ts.map +1 -1
- package/dist/routes/work-items.js +39 -16
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.d.ts +5 -0
- package/dist/rules/binding-context.d.ts.map +1 -1
- package/dist/rules/binding-context.js +51 -5
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +2 -1
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/start-coordinator.d.ts.map +1 -1
- package/dist/rules/start-coordinator.js +2 -7
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/sandbox/session-retirement.d.ts +6 -0
- package/dist/sandbox/session-retirement.d.ts.map +1 -1
- package/dist/sandbox/session-retirement.js +17 -0
- package/dist/sandbox/session-retirement.js.map +1 -1
- package/dist/session/factory-session.d.ts.map +1 -1
- package/dist/session/factory-session.js +2 -1
- package/dist/session/factory-session.js.map +1 -1
- package/dist/session/live-sessions.d.ts +6 -1
- package/dist/session/live-sessions.d.ts.map +1 -1
- package/dist/session/live-sessions.js +4 -0
- package/dist/session/live-sessions.js.map +1 -1
- package/dist/session/run-audit.d.ts +1 -1
- package/dist/session/run-audit.d.ts.map +1 -1
- package/dist/session/run-audit.js.map +1 -1
- package/dist/session/session-row-status.d.ts +3 -3
- package/dist/session/session-row-status.js +4 -4
- package/dist/session/session-row-status.js.map +1 -1
- package/dist/session/session-row-view.d.ts +3 -4
- package/dist/session/session-row-view.d.ts.map +1 -1
- package/dist/session/session-row-view.js +14 -21
- package/dist/session/session-row-view.js.map +1 -1
- package/dist/session/session-rows.d.ts +22 -8
- package/dist/session/session-rows.d.ts.map +1 -1
- package/dist/session/session-rows.js +158 -83
- package/dist/session/session-rows.js.map +1 -1
- package/dist/session/session-unloader.d.ts +101 -0
- package/dist/session/session-unloader.d.ts.map +1 -0
- package/dist/session/session-unloader.js +278 -0
- package/dist/session/session-unloader.js.map +1 -0
- package/dist/session/work-item-repository.d.ts +5 -1
- package/dist/session/work-item-repository.d.ts.map +1 -1
- package/dist/session/work-item-repository.js +39 -15
- package/dist/session/work-item-repository.js.map +1 -1
- package/dist/session-list-changes.d.ts +9 -0
- package/dist/session-list-changes.d.ts.map +1 -0
- package/dist/session-list-changes.js +54 -0
- package/dist/session-list-changes.js.map +1 -0
- package/dist/storage/announced-lists.d.ts +14 -0
- package/dist/storage/announced-lists.d.ts.map +1 -0
- package/dist/storage/announced-lists.js +78 -0
- package/dist/storage/announced-lists.js.map +1 -0
- package/dist/storage/domains/boards/base.d.ts +1 -1
- package/dist/storage/domains/boards/base.d.ts.map +1 -1
- package/dist/storage/domains/boards/base.js +27 -9
- package/dist/storage/domains/boards/base.js.map +1 -1
- package/dist/storage/domains/custom-providers/base.d.ts +62 -19
- package/dist/storage/domains/custom-providers/base.d.ts.map +1 -1
- package/dist/storage/domains/custom-providers/base.js +131 -100
- package/dist/storage/domains/custom-providers/base.js.map +1 -1
- package/dist/storage/domains/intake/base.d.ts +25 -7
- package/dist/storage/domains/intake/base.d.ts.map +1 -1
- package/dist/storage/domains/intake/base.js +83 -19
- package/dist/storage/domains/intake/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +8 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +110 -64
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +8 -5
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts +9 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +43 -11
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/tools/read-tools.d.ts +9 -2
- package/dist/tools/read-tools.d.ts.map +1 -1
- package/dist/tools/read-tools.js +57 -12
- package/dist/tools/read-tools.js.map +1 -1
- package/dist/tools/session-tools.d.ts +0 -13
- package/dist/tools/session-tools.d.ts.map +1 -1
- package/dist/tools/session-tools.js +127 -336
- package/dist/tools/session-tools.js.map +1 -1
- package/dist/tools/work-item-tools.d.ts.map +1 -1
- package/dist/tools/work-item-tools.js +91 -23
- package/dist/tools/work-item-tools.js.map +1 -1
- package/dist/work-items/card-reference.d.ts +21 -0
- package/dist/work-items/card-reference.d.ts.map +1 -0
- package/dist/work-items/card-reference.js +58 -0
- package/dist/work-items/card-reference.js.map +1 -0
- package/package.json +7 -7
- package/dist/storage/probed-list.d.ts +0 -11
- package/dist/storage/probed-list.d.ts.map +0 -1
- package/dist/storage/probed-list.js +0 -52
- package/dist/storage/probed-list.js.map +0 -1
package/dist/routes/config.js
CHANGED
|
@@ -2,8 +2,10 @@ import { resolveFactoryModelPolicy, routedPersonalDefaults } from "../session/cr
|
|
|
2
2
|
import "../session/memory-settings-hydration.js";
|
|
3
3
|
import { effectiveModelScope, factoryProjectSettingsUserId } from "../storage/domains/projects/base.js";
|
|
4
4
|
import { THINKING_LEVELS } from "./contracts.js";
|
|
5
|
+
import { customEndpointUrlProblem } from "./custom-endpoint-url.js";
|
|
6
|
+
import { customProviderOwner } from "./custom-provider-source.js";
|
|
5
7
|
import { MASTRA_SIGN_IN_REQUIRED, MastraGatewayError, createGatewayKey, getGatewayCredits } from "./mastra-gateway.js";
|
|
6
|
-
import { MASTRA_GATEWAY_CATALOG_PROVIDER, WEB_OAUTH_PROVIDERS, credentialLabel, getAuthProviderId, listTenantCredentialsForRequest, resolveCredentialContext, tenantOrgId } from "./provider-credentials.js";
|
|
8
|
+
import { MASTRA_GATEWAY_CATALOG_PROVIDER, WEB_OAUTH_PROVIDERS, credentialLabel, getAuthProviderId, listTenantCredentialsForRequest, resolveCredentialContext, runScopeFor, storedScopeFor, tenantOrgId } from "./provider-credentials.js";
|
|
7
9
|
import { Route, loose } from "./route.js";
|
|
8
10
|
import { readRunningPackages } from "./running-packages.js";
|
|
9
11
|
import { resolveTenantProviderDefaultModel } from "./tenant-credentials.js";
|
|
@@ -12,14 +14,37 @@ import { registerApiRoute } from "@mastra/core/server";
|
|
|
12
14
|
import { MIN_OBSERVATION_THRESHOLD } from "@mastra/code-sdk/constants";
|
|
13
15
|
import { z } from "zod";
|
|
14
16
|
import { MASTRA_GATEWAY_PROVIDER, getCustomProviderId } from "@mastra/code-sdk/onboarding/settings";
|
|
15
|
-
import { MASTRA_GATEWAY_PREFIX } from "@mastra/code-sdk/providers/model-ids";
|
|
16
|
-
import { MastraCodeGateway } from "@mastra/code-sdk/agents/mastracode-gateway";
|
|
17
|
+
import { MASTRA_GATEWAY_PREFIX, normalizeLegacyProviderId } from "@mastra/code-sdk/providers/model-ids";
|
|
18
|
+
import { MASTRACODE_GATEWAY_ID, MastraCodeGateway } from "@mastra/code-sdk/agents/mastracode-gateway";
|
|
17
19
|
import { resolveAutoOMModelId } from "@mastra/code-sdk/onboarding/packs";
|
|
18
20
|
import { AMAZON_BEDROCK_GATEWAY_ID } from "@mastra/code-sdk/providers/amazon-bedrock-gateway";
|
|
19
21
|
import { GOOGLE_VERTEX_GATEWAY_ID } from "@mastra/code-sdk/providers/google-vertex-gateway";
|
|
20
|
-
import { MastraGateway, getModelReasoningOptions } from "@mastra/core/llm";
|
|
22
|
+
import { MastraGateway, PROVIDER_REGISTRY, getModelReasoningOptions } from "@mastra/core/llm";
|
|
21
23
|
// src/routes/config.ts
|
|
22
24
|
/**
|
|
25
|
+
* Whether a custom provider may not take `providerId`, because it already
|
|
26
|
+
* means something to model resolution or to the provider pickers: a provider
|
|
27
|
+
* Factory ships with (even if its catalog failed to load or this server does
|
|
28
|
+
* not enable it), the id its
|
|
29
|
+
* credentials are saved under, an older id rewritten before lookup, or a
|
|
30
|
+
* gateway's own namespace. A custom provider there would be shadowed,
|
|
31
|
+
* rerouted, or shown to others as that provider.
|
|
32
|
+
*/
|
|
33
|
+
async function isReservedProviderId(controller, providerId) {
|
|
34
|
+
if (normalizeLegacyProviderId(providerId) !== providerId) return true;
|
|
35
|
+
const catalog = await controller.listAvailableModels();
|
|
36
|
+
return [
|
|
37
|
+
MASTRA_GATEWAY_CATALOG_PROVIDER,
|
|
38
|
+
MASTRA_GATEWAY_PROVIDER,
|
|
39
|
+
MASTRACODE_GATEWAY_ID,
|
|
40
|
+
AMAZON_BEDROCK_GATEWAY_ID,
|
|
41
|
+
GOOGLE_VERTEX_GATEWAY_ID,
|
|
42
|
+
...Object.keys(WEB_OAUTH_PROVIDERS),
|
|
43
|
+
...Object.keys(PROVIDER_REGISTRY),
|
|
44
|
+
...catalog.map((model) => model.provider)
|
|
45
|
+
].some((id) => id === providerId || getAuthProviderId(id) === providerId);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
23
48
|
* The controller's model catalog, plus the Mastra Gateway's models for tenant
|
|
24
49
|
* callers. The boot-time catalog lists `mastra/...` models only when the server
|
|
25
50
|
* itself has a gateway key; tenants bring their own (connected in one click).
|
|
@@ -102,27 +127,44 @@ async function listProviders({ controller, authStorage, tenantCredentials, deplo
|
|
|
102
127
|
});
|
|
103
128
|
}
|
|
104
129
|
if (listCustomProviders) try {
|
|
105
|
-
for (const record of await listCustomProviders()) {
|
|
106
|
-
|
|
107
|
-
|
|
130
|
+
for (const { record, scope } of await listCustomProviders()) {
|
|
131
|
+
const listed = seen.get(record.providerId);
|
|
132
|
+
if (listed && !listed.custom) continue;
|
|
133
|
+
seen.delete(`${MASTRACODE_GATEWAY_ID}/${record.providerId}`);
|
|
134
|
+
const info = listed ?? {
|
|
108
135
|
provider: record.providerId,
|
|
109
|
-
source:
|
|
136
|
+
source: "none",
|
|
110
137
|
custom: true
|
|
111
|
-
}
|
|
138
|
+
};
|
|
139
|
+
if (!tenantCredentials) info.source = "stored";
|
|
140
|
+
else if (scope === "user") {
|
|
141
|
+
info.userCredential = "api_key";
|
|
142
|
+
info.source = "stored-user";
|
|
143
|
+
} else {
|
|
144
|
+
info.orgCredential = "api_key";
|
|
145
|
+
info.orgKey = true;
|
|
146
|
+
if (info.source === "none") info.source = "stored-org";
|
|
147
|
+
}
|
|
148
|
+
seen.set(record.providerId, info);
|
|
112
149
|
}
|
|
113
150
|
} catch {}
|
|
114
151
|
return Array.from(seen.values()).sort((a, b) => a.provider.localeCompare(b.provider));
|
|
115
152
|
}
|
|
116
153
|
/** Redact a stored custom-provider row for the client (key presence only). */
|
|
117
|
-
function toCustomProviderInfo(record) {
|
|
154
|
+
function toCustomProviderInfo({ record, scope }) {
|
|
118
155
|
return {
|
|
119
156
|
id: record.providerId,
|
|
120
157
|
name: record.name,
|
|
121
158
|
url: record.url,
|
|
122
159
|
hasApiKey: Boolean(record.apiKey),
|
|
123
|
-
models: record.models
|
|
160
|
+
models: record.models,
|
|
161
|
+
scope,
|
|
162
|
+
urlProblem: customEndpointUrlProblem(record.url)
|
|
124
163
|
};
|
|
125
164
|
}
|
|
165
|
+
function isCallable(record) {
|
|
166
|
+
return !customEndpointUrlProblem(record.url);
|
|
167
|
+
}
|
|
126
168
|
/**
|
|
127
169
|
* Resolve the custom-providers context for a request, or a ready-to-return
|
|
128
170
|
* error response. Same posture as memory settings: tenant rows in deployed
|
|
@@ -131,17 +173,21 @@ function toCustomProviderInfo(record) {
|
|
|
131
173
|
async function resolveCustomProvidersContext({ c, auth, customProviders }) {
|
|
132
174
|
await auth.ensureUser(c);
|
|
133
175
|
const tenant = auth.tenant(c);
|
|
134
|
-
|
|
176
|
+
const listAt = (scope) => customProviderOwner(tenant && {
|
|
177
|
+
...tenant,
|
|
178
|
+
credentialScope: runScopeFor(scope)
|
|
179
|
+
}, auth.enabled());
|
|
180
|
+
const user = listAt("user");
|
|
181
|
+
if (!user) return { response: c.json({ error: "unauthorized" }, 401) };
|
|
135
182
|
if (customProviders) try {
|
|
136
183
|
await customProviders.ensureReady();
|
|
137
|
-
return
|
|
184
|
+
return {
|
|
138
185
|
storage: customProviders,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
userId: "local"
|
|
186
|
+
userId: tenant?.userId ?? "local",
|
|
187
|
+
lists: auth.enabled() ? {
|
|
188
|
+
user,
|
|
189
|
+
org: listAt("org")
|
|
190
|
+
} : { user }
|
|
145
191
|
};
|
|
146
192
|
} catch {}
|
|
147
193
|
return { response: c.json({
|
|
@@ -149,10 +195,38 @@ async function resolveCustomProvidersContext({ c, auth, customProviders }) {
|
|
|
149
195
|
message: "Custom provider storage is unavailable — the app database is not configured or failed to start."
|
|
150
196
|
}, 503) };
|
|
151
197
|
}
|
|
152
|
-
/**
|
|
198
|
+
/** Every custom provider the caller can reach in Settings, with the scope each is saved at. */
|
|
199
|
+
async function listScopedCustomProviders(ctx) {
|
|
200
|
+
const [user, org] = await Promise.all([ctx.storage.list(ctx.lists.user), ctx.lists.org ? ctx.storage.list(ctx.lists.org) : []]);
|
|
201
|
+
return [...user.map((record) => ({
|
|
202
|
+
record,
|
|
203
|
+
scope: "user"
|
|
204
|
+
})), ...org.map((record) => ({
|
|
205
|
+
record,
|
|
206
|
+
scope: "org"
|
|
207
|
+
}))];
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The custom providers model resolution uses for `tenant`, so a catalog never
|
|
211
|
+
* offers a model its session cannot run. Without a tenant the route applies no
|
|
212
|
+
* credential scope, and every list the caller can reach counts — the same rule
|
|
213
|
+
* those routes apply to saved keys.
|
|
214
|
+
*/
|
|
215
|
+
async function listCustomProvidersInEffect({ c, auth, customProviders, tenant }) {
|
|
216
|
+
const ctx = await resolveCustomProvidersContext({
|
|
217
|
+
c,
|
|
218
|
+
auth,
|
|
219
|
+
customProviders
|
|
220
|
+
});
|
|
221
|
+
if ("response" in ctx) return [];
|
|
222
|
+
if (!tenant) return (await listScopedCustomProviders(ctx)).map(({ record }) => record).filter(isCallable);
|
|
223
|
+
const owner = customProviderOwner(tenant, auth.enabled());
|
|
224
|
+
return owner ? (await ctx.storage.list(owner)).filter(isCallable) : [];
|
|
225
|
+
}
|
|
226
|
+
/** Validate + coerce a request body into a custom-provider save. */
|
|
153
227
|
function parseCustomProviderBody(body) {
|
|
154
228
|
if (!body || typeof body !== "object") return { error: "Invalid JSON body" };
|
|
155
|
-
const b = body;
|
|
229
|
+
const b = { ...body };
|
|
156
230
|
const name = typeof b.name === "string" ? b.name.trim() : "";
|
|
157
231
|
if (!name) return { error: "Missing required field: name" };
|
|
158
232
|
const url = typeof b.url === "string" ? b.url.trim() : "";
|
|
@@ -167,9 +241,29 @@ function parseCustomProviderBody(body) {
|
|
|
167
241
|
name,
|
|
168
242
|
url,
|
|
169
243
|
apiKey: typeof b.apiKey === "string" && b.apiKey.trim() ? b.apiKey.trim() : void 0,
|
|
170
|
-
models: Array.isArray(b.models) ? b.models.filter((m) => typeof m === "string" && m.trim().length > 0).map((m) => m.trim()) : []
|
|
244
|
+
models: Array.isArray(b.models) ? b.models.filter((m) => typeof m === "string" && m.trim().length > 0).map((m) => m.trim()) : [],
|
|
245
|
+
previousId: typeof b.previousId === "string" && b.previousId ? b.previousId : void 0,
|
|
246
|
+
scope: b.scope === "org" ? "org" : "user"
|
|
171
247
|
};
|
|
172
248
|
}
|
|
249
|
+
/** What each storage refusal tells the client. */
|
|
250
|
+
const CUSTOM_PROVIDER_REFUSALS = {
|
|
251
|
+
name_taken: {
|
|
252
|
+
status: 409,
|
|
253
|
+
error: "provider_name_taken",
|
|
254
|
+
message: "A provider with this name already exists here. Choose another name."
|
|
255
|
+
},
|
|
256
|
+
not_found: {
|
|
257
|
+
status: 404,
|
|
258
|
+
error: "custom_provider_not_found",
|
|
259
|
+
message: "This provider no longer exists."
|
|
260
|
+
},
|
|
261
|
+
api_key_required: {
|
|
262
|
+
status: 400,
|
|
263
|
+
error: "api_key_required",
|
|
264
|
+
message: "Enter the API key again: the saved key is only kept while the URL stays on the same host."
|
|
265
|
+
}
|
|
266
|
+
};
|
|
173
267
|
/**
|
|
174
268
|
* Providers whose credentials can only come from the server process. Amazon
|
|
175
269
|
* Bedrock uses the AWS credential chain, while Google Vertex uses Application
|
|
@@ -272,18 +366,20 @@ async function buildModelCatalog({ controller, authStorage, tenantCredentials, d
|
|
|
272
366
|
}] : []);
|
|
273
367
|
if (listCustomProviders) try {
|
|
274
368
|
const known = new Set(catalog.map((m) => m.id));
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
369
|
+
for (const record of await listCustomProviders()) {
|
|
370
|
+
const credentialHolder = record.userId === null ? "organization" : "personal";
|
|
371
|
+
for (const model of record.models) {
|
|
372
|
+
const id = `${record.providerId}/${model}`;
|
|
373
|
+
if (known.has(id)) continue;
|
|
374
|
+
known.add(id);
|
|
375
|
+
catalog.push({
|
|
376
|
+
id,
|
|
377
|
+
provider: record.providerId,
|
|
378
|
+
modelName: model,
|
|
379
|
+
hasApiKey: true,
|
|
380
|
+
...tenantCredentials ? { credentialHolder } : {}
|
|
381
|
+
});
|
|
382
|
+
}
|
|
287
383
|
}
|
|
288
384
|
} catch {}
|
|
289
385
|
return catalog.sort((a, b) => a.provider === b.provider ? a.id.localeCompare(b.id) : a.provider.localeCompare(b.provider));
|
|
@@ -293,8 +389,8 @@ async function buildModelCatalog({ controller, authStorage, tenantCredentials, d
|
|
|
293
389
|
* rows) for Factory-scoped sessions, the session owner's for user-scoped ones.
|
|
294
390
|
*/
|
|
295
391
|
async function listSessionScopedCredentials({ credentials, orgId, userId, modelScope }) {
|
|
296
|
-
const
|
|
297
|
-
return (await credentials.listCredentials(orgId, userId)).filter((record) => record.scope ===
|
|
392
|
+
const storedScope = storedScopeFor(modelScope);
|
|
393
|
+
return (await credentials.listCredentials(orgId, userId)).filter((record) => record.scope === storedScope);
|
|
298
394
|
}
|
|
299
395
|
/** Resolve the default-model context for a request, or a ready-to-return error response. */
|
|
300
396
|
async function resolveModelDefaultsContext({ c, auth, modelDefaults }) {
|
|
@@ -422,6 +518,29 @@ var ConfigRoutes = class extends Route {
|
|
|
422
518
|
});
|
|
423
519
|
const gatewayFailure = (c, error) => error instanceof MastraGatewayError ? c.json({ error: error.message }, error.status) : c.json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
424
520
|
const onCustomProvidersChanged = options.onCustomProvidersChanged ?? (() => {});
|
|
521
|
+
const customProvidersChanged = (orgId) => {
|
|
522
|
+
onCustomProvidersChanged({ orgId });
|
|
523
|
+
controller.invalidateAvailableModelsCache?.();
|
|
524
|
+
};
|
|
525
|
+
/**
|
|
526
|
+
* The list a custom-provider write addresses. Changing the organization's
|
|
527
|
+
* list takes an admin, as org-wide keys do. No-auth mode has one list,
|
|
528
|
+
* whatever scope the request names.
|
|
529
|
+
*/
|
|
530
|
+
const resolveCustomProviderWrite = async (c, ctx, scope) => {
|
|
531
|
+
if (scope !== "org" || !ctx.lists.org) return {
|
|
532
|
+
owner: ctx.lists.user,
|
|
533
|
+
scope: "user"
|
|
534
|
+
};
|
|
535
|
+
if (!await auth.isOrganizationAdmin(c, ctx.lists.org.orgId)) return { response: c.json({
|
|
536
|
+
error: "organization_admin_required",
|
|
537
|
+
message: "Only organization admins can change org-wide providers."
|
|
538
|
+
}, 403) };
|
|
539
|
+
return {
|
|
540
|
+
owner: ctx.lists.org,
|
|
541
|
+
scope: "org"
|
|
542
|
+
};
|
|
543
|
+
};
|
|
425
544
|
const resolveSessionModelContext = async (c, address, includeDefaultModel = true) => {
|
|
426
545
|
const tenant = auth.tenant(c);
|
|
427
546
|
if (!tenant && auth.enabled()) return { response: c.json({ error: "unauthorized" }, 401) };
|
|
@@ -460,11 +579,17 @@ var ConfigRoutes = class extends Route {
|
|
|
460
579
|
defaultModelId = ownerDefaults?.modelId ?? null;
|
|
461
580
|
personalThinkingLevel = ownerDefaults?.thinkingLevel ?? null;
|
|
462
581
|
}
|
|
582
|
+
const credentialTenant = {
|
|
583
|
+
orgId,
|
|
584
|
+
userId: policy.tenant.userId,
|
|
585
|
+
credentialScope: modelScope
|
|
586
|
+
};
|
|
463
587
|
return {
|
|
464
588
|
tenantCredentials,
|
|
465
589
|
modelScope,
|
|
466
590
|
defaultModelId,
|
|
467
|
-
personalThinkingLevel
|
|
591
|
+
personalThinkingLevel,
|
|
592
|
+
credentialTenant
|
|
468
593
|
};
|
|
469
594
|
};
|
|
470
595
|
const resolveOMResponseContext = async (c, { sessionId, threadId, resourceId, factoryProjectId }) => {
|
|
@@ -474,6 +599,10 @@ var ConfigRoutes = class extends Route {
|
|
|
474
599
|
credentials: options.modelCredentials
|
|
475
600
|
});
|
|
476
601
|
const tenant = auth.tenant(c);
|
|
602
|
+
let customProviderTenant = tenant && {
|
|
603
|
+
...tenant,
|
|
604
|
+
credentialScope: "user"
|
|
605
|
+
};
|
|
477
606
|
if (sessionId !== void 0 && threadId !== void 0) {
|
|
478
607
|
const address = z.object({
|
|
479
608
|
sessionId: z.string().trim().min(1),
|
|
@@ -491,6 +620,7 @@ var ConfigRoutes = class extends Route {
|
|
|
491
620
|
const sessionContext = await resolveSessionModelContext(c, address.data, false);
|
|
492
621
|
if (sessionContext && "response" in sessionContext) return { response: sessionContext.response };
|
|
493
622
|
tenantCredentials = sessionContext?.tenantCredentials;
|
|
623
|
+
customProviderTenant = sessionContext?.credentialTenant;
|
|
494
624
|
} else if (threadId !== void 0) return { response: c.json({ error: "invalid_session_context" }, 400) };
|
|
495
625
|
else if (tenantCredentials && factoryProjectId && tenant) {
|
|
496
626
|
if (!options.factoryProjects) return { response: c.json({ error: "factory_unavailable" }, 503) };
|
|
@@ -500,8 +630,12 @@ var ConfigRoutes = class extends Route {
|
|
|
500
630
|
id: factoryProjectId
|
|
501
631
|
});
|
|
502
632
|
if (!project) return { response: c.json({ error: "factory_project_not_found" }, 404) };
|
|
503
|
-
const
|
|
504
|
-
tenantCredentials = tenantCredentials.filter((record) => record.scope ===
|
|
633
|
+
const credentialScope = effectiveModelScope(project.modelScope, "run");
|
|
634
|
+
tenantCredentials = tenantCredentials.filter((record) => record.scope === storedScopeFor(credentialScope));
|
|
635
|
+
customProviderTenant = {
|
|
636
|
+
...tenant,
|
|
637
|
+
credentialScope
|
|
638
|
+
};
|
|
505
639
|
} else if (tenantCredentials) tenantCredentials = tenantCredentials.filter((record) => record.scope === "user");
|
|
506
640
|
const access = await buildProviderAccess({
|
|
507
641
|
controller,
|
|
@@ -512,12 +646,13 @@ var ConfigRoutes = class extends Route {
|
|
|
512
646
|
const availableProviders = new Set(Object.entries(access).flatMap(([providerId, configured]) => configured ? [providerId] : []));
|
|
513
647
|
if (access["mastra"] || MastraCodeGateway.getMastraGatewayApiKey()) availableProviders.add(MASTRA_GATEWAY_PROVIDER);
|
|
514
648
|
if (options.customProviders) {
|
|
515
|
-
const
|
|
649
|
+
const customProviders = await listCustomProvidersInEffect({
|
|
516
650
|
c: loose(c),
|
|
517
651
|
auth,
|
|
518
|
-
customProviders: options.customProviders
|
|
652
|
+
customProviders: options.customProviders,
|
|
653
|
+
tenant: customProviderTenant
|
|
519
654
|
});
|
|
520
|
-
|
|
655
|
+
for (const provider of customProviders) availableProviders.add(provider.providerId);
|
|
521
656
|
}
|
|
522
657
|
return { availableProviders };
|
|
523
658
|
};
|
|
@@ -569,7 +704,8 @@ var ConfigRoutes = class extends Route {
|
|
|
569
704
|
auth,
|
|
570
705
|
customProviders
|
|
571
706
|
});
|
|
572
|
-
|
|
707
|
+
if ("response" in ctx) return [];
|
|
708
|
+
return (await listScopedCustomProviders(ctx)).filter(({ record }) => isCallable(record));
|
|
573
709
|
} : void 0;
|
|
574
710
|
return c.json({
|
|
575
711
|
providers: await listProviders({
|
|
@@ -761,8 +897,7 @@ var ConfigRoutes = class extends Route {
|
|
|
761
897
|
});
|
|
762
898
|
if ("response" in ctx) return ctx.response;
|
|
763
899
|
try {
|
|
764
|
-
|
|
765
|
-
return c.json({ providers: records.map(toCustomProviderInfo) });
|
|
900
|
+
return c.json({ providers: (await listScopedCustomProviders(ctx)).map(toCustomProviderInfo) });
|
|
766
901
|
} catch (error) {
|
|
767
902
|
return c.json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
768
903
|
}
|
|
@@ -786,24 +921,48 @@ var ConfigRoutes = class extends Route {
|
|
|
786
921
|
}
|
|
787
922
|
const parsed = parseCustomProviderBody(body);
|
|
788
923
|
if ("error" in parsed) return c.json({ error: parsed.error }, 400);
|
|
789
|
-
const
|
|
924
|
+
const urlProblem = customEndpointUrlProblem(parsed.url);
|
|
925
|
+
if (urlProblem) return c.json({
|
|
926
|
+
error: "custom_endpoint_url_refused",
|
|
927
|
+
message: urlProblem
|
|
928
|
+
}, 400);
|
|
929
|
+
const target = await resolveCustomProviderWrite(loose(c), ctx, parsed.scope);
|
|
930
|
+
if ("response" in target) return target.response;
|
|
931
|
+
const input = {
|
|
932
|
+
providerId: getCustomProviderId(parsed.name),
|
|
933
|
+
name: parsed.name,
|
|
934
|
+
url: parsed.url,
|
|
935
|
+
apiKey: parsed.apiKey,
|
|
936
|
+
models: parsed.models
|
|
937
|
+
};
|
|
790
938
|
try {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
939
|
+
if (auth.enabled() && await isReservedProviderId(controller, input.providerId)) return c.json({
|
|
940
|
+
error: "provider_name_taken",
|
|
941
|
+
message: `"${parsed.name}" is reserved for a built-in provider. Choose another name.`
|
|
942
|
+
}, 409);
|
|
943
|
+
const write = parsed.previousId ? await ctx.storage.update({
|
|
944
|
+
owner: target.owner,
|
|
945
|
+
providerId: parsed.previousId,
|
|
946
|
+
input
|
|
947
|
+
}) : await ctx.storage.create({
|
|
948
|
+
owner: target.owner,
|
|
949
|
+
createdBy: ctx.userId,
|
|
950
|
+
input
|
|
802
951
|
});
|
|
803
|
-
|
|
952
|
+
if ("refused" in write) {
|
|
953
|
+
const { status, error, message } = CUSTOM_PROVIDER_REFUSALS[write.refused];
|
|
954
|
+
return c.json({
|
|
955
|
+
error,
|
|
956
|
+
message
|
|
957
|
+
}, status);
|
|
958
|
+
}
|
|
959
|
+
customProvidersChanged(target.owner.orgId);
|
|
804
960
|
return c.json({
|
|
805
961
|
ok: true,
|
|
806
|
-
provider: toCustomProviderInfo(
|
|
962
|
+
provider: toCustomProviderInfo({
|
|
963
|
+
record: write.saved,
|
|
964
|
+
scope: target.scope
|
|
965
|
+
})
|
|
807
966
|
});
|
|
808
967
|
} catch (error) {
|
|
809
968
|
return c.json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
@@ -821,12 +980,20 @@ var ConfigRoutes = class extends Route {
|
|
|
821
980
|
});
|
|
822
981
|
if ("response" in ctx) return ctx.response;
|
|
823
982
|
const id = c.req.param("id");
|
|
983
|
+
const target = await resolveCustomProviderWrite(loose(c), ctx, c.req.query("scope"));
|
|
984
|
+
if ("response" in target) return target.response;
|
|
824
985
|
try {
|
|
825
|
-
await ctx.storage.delete({
|
|
826
|
-
|
|
986
|
+
if (!await ctx.storage.delete({
|
|
987
|
+
owner: target.owner,
|
|
827
988
|
providerId: id
|
|
828
|
-
})
|
|
829
|
-
|
|
989
|
+
})) {
|
|
990
|
+
const { status, error, message } = CUSTOM_PROVIDER_REFUSALS.not_found;
|
|
991
|
+
return c.json({
|
|
992
|
+
error,
|
|
993
|
+
message
|
|
994
|
+
}, status);
|
|
995
|
+
}
|
|
996
|
+
customProvidersChanged(target.owner.orgId);
|
|
830
997
|
return c.json({ ok: true });
|
|
831
998
|
} catch (error) {
|
|
832
999
|
return c.json({ error: error instanceof Error ? error.message : String(error) }, 500);
|
|
@@ -848,6 +1015,7 @@ var ConfigRoutes = class extends Route {
|
|
|
848
1015
|
let tenantCredentials = allTenantCredentials;
|
|
849
1016
|
let sessionPolicy;
|
|
850
1017
|
let credentialTenant;
|
|
1018
|
+
let sessionTenant;
|
|
851
1019
|
const resourceId = c.req.query("resourceId");
|
|
852
1020
|
const threadId = c.req.query("threadId");
|
|
853
1021
|
const sessionId = c.req.query("sessionId");
|
|
@@ -869,6 +1037,7 @@ var ConfigRoutes = class extends Route {
|
|
|
869
1037
|
if (context && "response" in context) return context.response;
|
|
870
1038
|
if (context) {
|
|
871
1039
|
tenantCredentials = context.tenantCredentials;
|
|
1040
|
+
sessionTenant = context.credentialTenant;
|
|
872
1041
|
sessionPolicy = {
|
|
873
1042
|
modelScope: context.modelScope,
|
|
874
1043
|
defaultModelId: context.defaultModelId,
|
|
@@ -882,7 +1051,7 @@ var ConfigRoutes = class extends Route {
|
|
|
882
1051
|
if (tenant) credentialTenant = {
|
|
883
1052
|
orgId: tenantOrgId(tenant),
|
|
884
1053
|
userId: tenant.userId,
|
|
885
|
-
credentialScope: scope.data
|
|
1054
|
+
credentialScope: runScopeFor(scope.data)
|
|
886
1055
|
};
|
|
887
1056
|
} else if (allTenantCredentials && factoryProjectId && tenant) {
|
|
888
1057
|
if (!options.factoryProjects) return c.json({ error: "factory_unavailable" }, 503);
|
|
@@ -892,12 +1061,12 @@ var ConfigRoutes = class extends Route {
|
|
|
892
1061
|
id: factoryProjectId
|
|
893
1062
|
});
|
|
894
1063
|
if (!project) return c.json({ error: "factory_project_not_found" }, 404);
|
|
895
|
-
const
|
|
896
|
-
tenantCredentials = allTenantCredentials.filter((record) => record.scope ===
|
|
1064
|
+
const storedScope = project.modelScope === "user" ? "user" : "org";
|
|
1065
|
+
tenantCredentials = allTenantCredentials.filter((record) => record.scope === storedScope);
|
|
897
1066
|
credentialTenant = {
|
|
898
1067
|
orgId: tenantOrgId(tenant),
|
|
899
1068
|
userId: tenant.userId,
|
|
900
|
-
credentialScope:
|
|
1069
|
+
credentialScope: runScopeFor(storedScope)
|
|
901
1070
|
};
|
|
902
1071
|
}
|
|
903
1072
|
const customProviders = options.customProviders;
|
|
@@ -906,14 +1075,12 @@ var ConfigRoutes = class extends Route {
|
|
|
906
1075
|
authStorage,
|
|
907
1076
|
tenantCredentials,
|
|
908
1077
|
deploymentProviders,
|
|
909
|
-
listCustomProviders: customProviders ?
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
return "response" in ctx ? [] : ctx.storage.list({ orgId: ctx.orgId });
|
|
916
|
-
} : void 0
|
|
1078
|
+
listCustomProviders: customProviders ? () => listCustomProvidersInEffect({
|
|
1079
|
+
c: loose(c),
|
|
1080
|
+
auth,
|
|
1081
|
+
customProviders,
|
|
1082
|
+
tenant: sessionTenant ?? credentialTenant
|
|
1083
|
+
}) : void 0
|
|
917
1084
|
});
|
|
918
1085
|
if (!credentialTenant) return c.json({
|
|
919
1086
|
...sessionPolicy,
|