Files
upstream-twenty/packages/twenty-server/scripts/ai-catalog-sync/index.ts
upstream-mirror e7da97194a
Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

212 lines
6.4 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import * as prettier from 'prettier';
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';
import { assertPayloadIsUsable } from './utils/assert-payload-is-usable.util';
import { buildCatalog } from './utils/build-catalog.util';
import { carryOverCommittedFields } from './utils/carry-over-committed-fields.util';
import { enrichCatalog } from './utils/enrich-catalog.util';
import { fetchArtificialAnalysisBenchmarks } from './utils/fetch-artificial-analysis-benchmarks.util';
import { projectCatalog } from './utils/project-catalog.util';
import { readCommittedBenchmarks } from './utils/read-committed-benchmarks.util';
import { buildCoverageReport } from './utils/build-coverage-report.util';
import { renderCoverageReport } from './utils/render-coverage-report.util';
import { type BenchmarkIndex } from './types/benchmark-index.type';
import { type CatalogSpec } from './types/catalog-spec.type';
import { type GeneratedCatalog } from './types/generated-catalog.type';
const AI_MODELS_DIR = path.resolve(
__dirname,
'..',
'..',
'src',
'engine',
'metadata-modules',
'ai',
'ai-models',
);
const MODELS_PATH = path.join(AI_MODELS_DIR, 'ai-models.json');
const SELF_HOST_SPEC_PATH = path.join(AI_MODELS_DIR, 'ai-self-host-spec.json');
const CATALOG_PATH = path.join(AI_MODELS_DIR, 'ai-providers.json');
const BENCHMARKS_PATH = path.join(AI_MODELS_DIR, 'ai-model-benchmarks.json');
// oxlint-disable no-console
const log = console.log;
const warn = console.warn;
// oxlint-enable no-console
const readArgument = (flag: string): string | undefined => {
const index = process.argv.indexOf(flag);
return index === -1 ? undefined : process.argv[index + 1];
};
const readCommittedModels = (filePath: string): GeneratedCatalog =>
fs.existsSync(filePath)
? (JSON.parse(fs.readFileSync(filePath, 'utf-8')) as GeneratedCatalog)
: {};
// 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),
});
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
const data: ModelsDevData = await response.json();
assertPayloadIsUsable({ data, vendors });
return data;
};
// A leaderboard outage must never break the model catalog, so a failed fetch
// degrades to an empty index and the catalog is written without benchmarks.
const fetchBenchmarks = async (): Promise<BenchmarkIndex> => {
const apiKey = process.env.ARTIFICIAL_ANALYSIS_API_KEY;
if (!isDefined(apiKey) || apiKey.length === 0) {
warn('Skipping benchmarks: ARTIFICIAL_ANALYSIS_API_KEY is not set');
return new Map();
}
try {
const index = await fetchArtificialAnalysisBenchmarks(apiKey);
log(`Fetched ${index.size} model aliases from Artificial Analysis`);
return index;
} catch (error) {
warn(
`Skipping benchmarks: ${error instanceof Error ? error.message : String(error)}`,
);
return new Map();
}
};
const writeJson = async (filePath: string, value: unknown): Promise<void> => {
const prettierConfig = await prettier.resolveConfig(filePath);
const formatted = await prettier.format(
JSON.stringify(value, null, 2) + '\n',
{ ...prettierConfig, filepath: filePath },
);
fs.writeFileSync(filePath, formatted, 'utf-8');
log(`Wrote ${filePath}`);
};
const main = async (): Promise<void> => {
const dryRun = process.argv.includes('--dry-run');
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({ vendors });
log(`Fetched ${Object.keys(modelsDevData).length} providers from models.dev`);
const fetched = await fetchBenchmarks();
// A failed fetch must not delete measurements we already published: the
// catalog PR is automerged, so an empty index would silently strip every
// benchmark until the next healthy run.
const benchmarkIndex =
fetched.size > 0 ? fetched : readCommittedBenchmarks(BENCHMARKS_PATH);
if (fetched.size === 0 && benchmarkIndex.size > 0) {
warn(
`Reusing ${benchmarkIndex.size} committed benchmark aliases from a previous run`,
);
}
const catalog = buildCatalog({ data: modelsDevData, vendors });
carryOverCommittedFields({
catalog,
committedCatalog: readCommittedModels(MODELS_PATH),
});
const overlay = enrichCatalog({
catalog,
modelsDevData,
benchmarkIndex,
measuredAt,
});
const renderedReport = renderCoverageReport(buildCoverageReport(catalog));
log(`\n${renderedReport}\n`);
if (isDefined(reportPath)) {
fs.writeFileSync(reportPath, `${renderedReport}\n`, 'utf-8');
}
if (dryRun) {
log('[DRY RUN] Skipping writes');
return;
}
await writeJson(MODELS_PATH, catalog);
// Self-host runs the same projection cloud does, from a spec that names the
// five direct routes and lets each serve its whole vendor.
await writeJson(
CATALOG_PATH,
projectCatalog({ canonicalCatalog: catalog, spec: selfHostSpec }),
);
await writeJson(BENCHMARKS_PATH, {
source: 'artificialanalysis.ai',
measuredAt,
models: overlay,
});
};
main().catch((error) => {
// oxlint-disable-next-line no-console
console.error('AI catalog sync failed:', error);
process.exit(1);
});