mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
feat(codex): support explicit local model catalog configuration (#2266)
* fix(codex): reuse local model catalogs by default * fix(codex): verify catalog provenance and native schema before reuse * fix(codex): keep refresh credentials out of discovery homes * refactor(codex): reduce catalog support to explicit configuration * fix(codex): invalidate selected catalogs and resolve shim names
This commit is contained in:
@@ -146,6 +146,30 @@ describe('listCodexModels cwd', () => {
|
||||
expect(constructorOptions).toHaveLength(4);
|
||||
});
|
||||
|
||||
it('invalidates the environment-selected catalog and honors its precedence over config.toml', async () => {
|
||||
const home = await mkdtemp(join(tmpdir(), 'hapi-codex-env-models-'));
|
||||
temporaryHomes.push(home);
|
||||
vi.stubEnv('CODEX_HOME', home);
|
||||
const configured = join(home, 'configured.json');
|
||||
const selected = join(home, 'selected.json');
|
||||
await writeFile(join(home, 'config.toml'), `model_catalog_json = ${JSON.stringify(configured)}\n`);
|
||||
await writeFile(configured, 'configured');
|
||||
await writeFile(selected, 'first');
|
||||
vi.stubEnv('HAPI_CODEX_MODEL_CATALOG_JSON', selected);
|
||||
listModelsMock.mockResolvedValue({ data: [{ id: 'first' }] });
|
||||
await listCodexModels();
|
||||
await writeFile(configured, 'unused-change');
|
||||
await listCodexModels();
|
||||
expect(constructorOptions).toHaveLength(1);
|
||||
await writeFile(selected, 'second');
|
||||
listModelsMock.mockResolvedValue({ data: [{ id: 'second' }] });
|
||||
expect((await listCodexModels())[0]?.id).toBe('second');
|
||||
expect(constructorOptions).toHaveLength(2);
|
||||
vi.stubEnv('HAPI_CODEX_MODEL_CATALOG_JSON', '');
|
||||
await listCodexModels();
|
||||
expect(constructorOptions).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('resolves a relative catalog path from the Codex config directory', async () => {
|
||||
const home = await mkdtemp(join(tmpdir(), 'hapi-codex-models-'));
|
||||
temporaryHomes.push(home);
|
||||
|
||||
@@ -4,6 +4,7 @@ import { readFile } from 'node:fs/promises';
|
||||
import { join, resolve } from 'node:path';
|
||||
import type { CodexModelsResponse, CodexModelSummary } from '@hapi/protocol/apiTypes';
|
||||
import { CodexAppServerClient } from '@/codex/codexAppServerClient';
|
||||
import { resolveModelCatalogArgs } from '@/codex/utils/modelCatalog';
|
||||
import { resolveCodexHome } from '@/codex/utils/codexHome';
|
||||
import { getErrorMessage } from './rpcResponses';
|
||||
|
||||
@@ -114,7 +115,10 @@ async function catalogFingerprint(): Promise<string> {
|
||||
const rootConfig = config?.toString('utf8').split(/^\s*\[/m, 1)[0] ?? '';
|
||||
const rawPath = rootConfig.match(/^\s*model_catalog_json\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*')/m)?.[1];
|
||||
let catalogPath: string | undefined;
|
||||
if (rawPath) {
|
||||
const override = resolveModelCatalogArgs();
|
||||
if (override.length) {
|
||||
catalogPath = JSON.parse(override[1].slice('model_catalog_json='.length)) as string;
|
||||
} else if (rawPath) {
|
||||
catalogPath = rawPath.startsWith('"') ? JSON.parse(rawPath) as string : rawPath.slice(1, -1);
|
||||
}
|
||||
const catalog = catalogPath ? await readOptionalFile(resolve(home, catalogPath)) : null;
|
||||
|
||||
Reference in New Issue
Block a user