Skip to content

The open alternative to Salesforce, designed for AI.

refactor(ai): derive the SDK package from the packages we bundle (#25845)

Félix Malfait7b327b289acd
diff --git a/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminNewAiProvider.tsx b/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminNewAiProvider.tsx
index 8703341b9ef..134671a9a4e 100644
--- a/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminNewAiProvider.tsx
+++ b/packages/twenty-front/src/pages/settings/admin-panel/SettingsAdminNewAiProvider.tsx
@@ -1,3 +1,5 @@
+import { isNonEmptyString } from '@sniptt/guards';
+
import { useMemo, useState } from 'react';
import { useMutation, useQuery } from '@apollo/client/react';
@@ -183,11 +185,28 @@ export const SettingsAdminNewAiProvider = () => {
}
config.region = values.region.trim();
- if (values.accessKeyId.trim()) {
- config.accessKeyId = values.accessKeyId.trim();
+ const accessKeyId = values.accessKeyId.trim();
+ const secretAccessKey = values.secretAccessKey.trim();
+
+ // Half a key pair is a slip, not a mode: role auth ignores both fields,
+ // so accepting it would run under an identity nobody chose.
+ if (isNonEmptyString(accessKeyId) !== isNonEmptyString(secretAccessKey)) {
+ form.setError(
+ isNonEmptyString(accessKeyId) ? 'secretAccessKey' : 'accessKeyId',
+ {
+ type: 'manual',
+ message: t`Enter both keys, or neither to use the instance IAM role`,
+ },
+ );
+
+ return;
}
- if (values.secretAccessKey.trim()) {
- config.secretAccessKey = values.secretAccessKey.trim();
+
+ if (isNonEmptyString(accessKeyId)) {
+ config.accessKeyId = accessKeyId;
+ config.secretAccessKey = secretAccessKey;
+ } else {
+ config.authType = 'role';
}
}
@@ -435,12 +454,16 @@ export const SettingsAdminNewAiProvider = () => {
<Controller
name="accessKeyId"
control={form.control}
- render={({ field: { onChange, value } }) => (
+ render={({
+ field: { onChange, value },
+ fieldState: { error },
+ }) => (
<TextInput
value={value}
onChange={onChange}
placeholder={t`AKIA...`}
fullWidth
+ error={error?.message}
/>
)}
/>
@@ -454,12 +477,16 @@ export const SettingsAdminNewAiProvider = () => {
<Controller
name="secretAccessKey"
control={form.control}
- render={({ field: { onChange, value } }) => (
+ render={({
+ field: { onChange, value },
+ fieldState: { error },
+ }) => (
<TextInput
value={value}
onChange={onChange}
fullWidth
type="password"
+ error={error?.message}
/>
)}
/>
diff --git a/packages/twenty-server/scripts/ai-catalog-sync/__tests__/build-catalog.spec.ts b/packages/twenty-server/scripts/ai-catalog-sync/__tests__/build-catalog.spec.ts
index 8896847e5e3..4087fe594e0 100644
--- a/packages/twenty-server/scripts/ai-catalog-sync/__tests__/build-catalog.spec.ts
+++ b/packages/twenty-server/scripts/ai-catalog-sync/__tests__/build-catalog.spec.ts
@@ -11,6 +11,8 @@ const languageModel = {
limit: { context: 128000, output: 8192 },
};
+const VENDORS = ['openai', 'anthropic', 'google', 'xai', 'mistral'];
+
const payload = (): ModelsDevData => ({
openai: { id: 'openai', models: { 'gpt-x': languageModel } },
anthropic: { id: 'anthropic', models: { 'claude-x': languageModel } },
@@ -20,14 +22,16 @@ const payload = (): ModelsDevData => ({
});
describe('assertPayloadIsUsable', () => {
- it('accepts a payload carrying models for every native provider', () => {
- expect(() => assertPayloadIsUsable(payload())).not.toThrow();
+ it('accepts a payload carrying models for every vendor we ship', () => {
+ expect(() =>
+ assertPayloadIsUsable({ data: payload(), vendors: VENDORS }),
+ ).not.toThrow();
});
it('refuses an empty payload rather than generating an empty catalog', () => {
- expect(() => assertPayloadIsUsable({} as ModelsDevData)).toThrow(
- /refusing to overwrite the catalog/,
- );
+ expect(() =>
+ assertPayloadIsUsable({ data: {} as ModelsDevData, vendors: VENDORS }),
+ ).toThrow(/refusing to overwrite the catalog/);
});
it('names the providers that came back empty', () => {
@@ -35,7 +39,9 @@ describe('assertPayloadIsUsable', () => {
data.anthropic.models = {};
- expect(() => assertPayloadIsUsable(data)).toThrow(/anthropic/);
+ expect(() => assertPayloadIsUsable({ data, vendors: VENDORS })).toThrow(
+ /anthropic/,
+ );
});
});
@@ -49,7 +55,7 @@ describe('buildCatalog', () => {
context_over_200k: { input: 4 },
};
- const model = buildCatalog(data).openai.models[0];
+ const model = buildCatalog({ data, vendors: VENDORS }).openai.models[0];
// A missing long-context rate is unknown, not free.
expect(model.longContextCost?.outputCostPerMillionTokens).toBe(6);
diff --git a/packages/twenty-server/scripts/ai-catalog-sync/index.ts b/packages/twenty-server/scripts/ai-catalog-sync/index.ts
index fd8628764c2..81f8c187b48 100644
--- a/packages/twenty-server/scripts/ai-catalog-sync/index.ts
+++ b/packages/twenty-server/scripts/ai-catalog-sync/index.ts
@@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as prettier from 'prettier';
-import { isDefined } from 'twenty-shared/utils';
+import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
import { MODELS_DEV_API_URL } from 'src/engine/metadata-modules/ai/ai-models/constants/models-dev.const';
import { type ModelsDevData } from 'src/engine/metadata-modules/ai/ai-models/types/models-dev-data.type';
@@ -52,7 +52,33 @@ const readCommittedModels = (filePath: string): GeneratedCatalog =>
? (JSON.parse(fs.readFileSync(filePath, 'utf-8')) as GeneratedCatalog)
: {};
-const fetchModelsDev = async (): Promise<ModelsDevData> => {
+// The vendors the shipped catalog carries are the ones the self-host spec
+// serves; nothing else needs to be fetched, checked or written.
+const readVendors = (spec: CatalogSpec): string[] => {
+ const vendors = [
+ ...new Set(
+ spec.providers.flatMap((provider) =>
+ Array.isArray(provider.models) ? [] : [provider.models.vendor],
+ ),
+ ),
+ ];
+
+ // An empty list would fetch nothing, assert nothing and write an empty
+ // catalog over the real one, and the sync PR automerges.
+ if (!isNonEmptyArray(vendors)) {
+ throw new Error(
+ `${SELF_HOST_SPEC_PATH} names no vendor to carry: every route lists models explicitly`,
+ );
+ }
+
+ return vendors;
+};
+
+const fetchModelsDev = async ({
+ vendors,
+}: {
+ vendors: string[];
+}): Promise<ModelsDevData> => {
const response = await fetch(MODELS_DEV_API_URL, {
signal: AbortSignal.timeout(120000),
});
@@ -63,7 +89,7 @@ const fetchModelsDev = async (): Promise<ModelsDevData> => {
const data: ModelsDevData = await response.json();
- assertPayloadIsUsable(data);
+ assertPayloadIsUsable({ data, vendors });
return data;
};
@@ -111,9 +137,14 @@ const main = async (): Promise<void> => {
const reportPath = readArgument('--report');
const measuredAt = new Date().toISOString().slice(0, 10);
+ const selfHostSpec = JSON.parse(
+ fs.readFileSync(SELF_HOST_SPEC_PATH, 'utf-8'),
+ ) as CatalogSpec;
+ const vendors = readVendors(selfHostSpec);
+
log('Fetching models.dev API...');
- const modelsDevData = await fetchModelsDev();
+ const modelsDevData = await fetchModelsDev({ vendors });
log(`Fetched ${Object.keys(modelsDevData).length} providers from models.dev`);
@@ -131,7 +162,7 @@ const main = async (): Promise<void> => {
);
}
- const catalog = buildCatalog(modelsDevData);
+ const catalog = buildCatalog({ data: modelsDevData, vendors });
carryOverCommittedFields({
catalog,
@@ -164,12 +195,7 @@ const main = async (): Promise<void> => {
// five direct routes and lets each serve its whole vendor.
await writeJson(
CATALOG_PATH,
- projectCatalog({
- canonicalCatalog: catalog,
- spec: JSON.parse(
- fs.readFileSync(SELF_HOST_SPEC_PATH, 'utf-8'),
- ) as CatalogSpec,
- }),
+ projectCatalog({ canonicalCatalog: catalog, spec: selfHostSpec }),
);
await writeJson(BENCHMARKS_PATH, {
source: 'artificialanalysis.ai',
diff --git a/packages/twenty-server/scripts/ai-catalog-sync/utils/assert-payload-is-usable.util.ts b/packages/twenty-server/scripts/ai-catalog-sync/utils/assert-payload-is-usable.util.ts
index af30cde8182..7225fa18d8f 100644
--- a/packages/twenty-server/scripts/ai-catalog-sync/utils/assert-payload-is-usable.util.ts
+++ b/packages/twenty-server/scripts/ai-catalog-sync/utils/assert-payload-is-usable.util.ts
@@ -1,13 +1,16 @@
-import { NATIVE_AI_SDK_PROVIDER_IDS } from 'twenty-shared/ai';
-
import { type ModelsDevData } from 'src/engine/metadata-modules/ai/ai-models/types/models-dev-data.type';
// A 200 carrying an empty or reshaped payload would otherwise generate an
// empty catalog, and the sync PR automerges over the real one.
-export const assertPayloadIsUsable = (data: ModelsDevData): void => {
- const missing = NATIVE_AI_SDK_PROVIDER_IDS.filter(
- (providerName) =>
- Object.keys(data[providerName]?.models ?? {}).length === 0,
+export const assertPayloadIsUsable = ({
+ data,
+ vendors,
+}: {
+ data: ModelsDevData;
+ vendors: string[];
+}): void => {
+ const missing = vendors.filter(
+ (vendor) => Object.keys(data[vendor]?.models ?? {}).length === 0,
);
if (missing.length > 0) {
diff --git a/packages/twenty-server/scripts/ai-catalog-sync/utils/build-catalog.util.ts b/packages/twenty-server/scripts/ai-catalog-sync/utils/build-catalog.util.ts
index a8dc74bfc46..80da59ff9ea 100644
--- a/packages/twenty-server/scripts/ai-catalog-sync/utils/build-catalog.util.ts
+++ b/packages/twenty-server/scripts/ai-catalog-sync/utils/build-catalog.util.ts
@@ -1,4 +1,3 @@
-import { NATIVE_AI_SDK_PROVIDER_IDS } from 'twenty-shared/ai';
import { isDefined } from 'twenty-shared/utils';
import { type ModelsDevData } from 'src/engine/metadata-modules/ai/ai-models/types/models-dev-data.type';
@@ -123,10 +122,18 @@ const buildModel = ({
return model;
};
-export const buildCatalog = (data: ModelsDevData): GeneratedCatalog => {
+// Which vendors we carry is a deployment decision, so it is read from the
+// spec that states it rather than inferred from how the SDK names a package.
+export const buildCatalog = ({
+ data,
+ vendors,
+}: {
+ data: ModelsDevData;
+ vendors: string[];
+}): GeneratedCatalog => {
const catalog: GeneratedCatalog = {};
- for (const providerName of NATIVE_AI_SDK_PROVIDER_IDS) {
+ for (const providerName of vendors) {
const providerData = data[providerName];
if (!isDefined(providerData)) {
diff --git a/packages/twenty-shared/src/ai/constants/native-ai-sdk-provider-ids.const.ts b/packages/twenty-shared/src/ai/constants/native-ai-sdk-provider-ids.const.ts
deleted file mode 100644
index 3d00baa3446..00000000000
--- a/packages/twenty-shared/src/ai/constants/native-ai-sdk-provider-ids.const.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-// Provider IDs where the SDK package follows the @ai-sdk/{providerId} pattern.
-// Other providers default to @ai-sdk/openai-compatible.
-export const NATIVE_AI_SDK_PROVIDER_IDS = [
- 'openai',
- 'anthropic',
- 'google',
- 'mistral',
- 'xai',
-] as const;
-
-export type NativeAiSdkProviderId = (typeof NATIVE_AI_SDK_PROVIDER_IDS)[number];
diff --git a/packages/twenty-shared/src/ai/index.ts b/packages/twenty-shared/src/ai/index.ts
index bb6e0ee9522..1ab0f634a96 100644
--- a/packages/twenty-shared/src/ai/index.ts
+++ b/packages/twenty-shared/src/ai/index.ts
@@ -26,8 +26,6 @@ export type { DatabaseCrudOperation } from './constants/database-crud-operation.
export { DATABASE_CRUD_OPERATIONS } from './constants/database-crud-operation.const';
export { DEFAULT_AI_AGENT_MODEL_TIER } from './constants/default-ai-agent-model-tier.const';
export { DEFAULT_AI_CHAT_MODEL_TIER } from './constants/default-ai-chat-model-tier.const';
-export type { NativeAiSdkProviderId } from './constants/native-ai-sdk-provider-ids.const';
-export { NATIVE_AI_SDK_PROVIDER_IDS } from './constants/native-ai-sdk-provider-ids.const';
export { ToolCategory } from './constants/tool-category.const';
export type {
AgentResponseFormatType,
diff --git a/packages/twenty-shared/src/ai/utils/infer-ai-sdk-package.util.ts b/packages/twenty-shared/src/ai/utils/infer-ai-sdk-package.util.ts
index d68a03d1238..9ff979a4e18 100644
--- a/packages/twenty-shared/src/ai/utils/infer-ai-sdk-package.util.ts
+++ b/packages/twenty-shared/src/ai/utils/infer-ai-sdk-package.util.ts
@@ -1,7 +1,12 @@
-import { type AiSdkPackage } from '../constants/ai-sdk-packages.const';
-import { NATIVE_AI_SDK_PROVIDER_IDS } from '../constants/native-ai-sdk-provider-ids.const';
+import {
+ AI_SDK_PACKAGES,
+ type AiSdkPackage,
+} from '../constants/ai-sdk-packages.const';
+// models.dev names a vendor the way the AI SDK names its package for most of
+// the ones we bundle, so a vendor we have a package for is served by it and
+// everything else goes through the compatible shim.
export const inferAiSdkPackage = (providerId: string): AiSdkPackage =>
- (NATIVE_AI_SDK_PROVIDER_IDS as readonly string[]).includes(providerId)
+ (AI_SDK_PACKAGES as readonly string[]).includes(`@ai-sdk/${providerId}`)
? (`@ai-sdk/${providerId}` as AiSdkPackage)
: '@ai-sdk/openai-compatible';