const fs = require('node:fs') const path = require('node:path') const assert = require('node:assert/strict') const root = path.resolve(__dirname, '../..') const model = '404e005b-83ae-5e2e-a3b3-999f9d5cd47e' const tenant = '10059' let token async function api(route, method = 'GET', body) { const response = await fetch(`http://127.0.0.1:18080/api/v1${route}`, { method, headers: { 'Content-Type': 'application/json', ...(token ? { Authorization: `Bearer ${token}`, 'X-Tenant-ID': tenant } : {}) }, body: body === undefined ? undefined : JSON.stringify(body), signal: AbortSignal.timeout(30000) }) const result = await response.json() assert.ok(response.ok && result.success !== false, `${method} ${route}: HTTP ${response.status}`) return result } async function main() { const credentials = JSON.parse(fs.readFileSync(path.join(root, 'acceptance/super-admin-login.json'), 'utf8')) const login = await api('/auth/login', 'POST', { username: credentials.username, password: credentials.password }) token = (login.data || login).token const before = (await api('/knowledge-bases')).data const targets = before.filter(kb => !kb.summary_model_id && kb.embedding_model_id) if (!process.argv.includes('--apply')) { console.log(JSON.stringify({ missing_models: targets.map(kb => kb.name) })); return } fs.writeFileSync(path.join(root, 'acceptance', `kb-model-config-before-${Date.now()}.json`), JSON.stringify(targets, null, 2), { mode: 0o600, flag: 'wx' }) for (const kb of targets) { const c = kb.chunking_config || {}, e = kb.extract_config || {}, q = kb.question_generation_config || {} const request = { llmModelId: model, embeddingModelId: kb.embedding_model_id, vlm_config: kb.vlm_config, asr_config: kb.asr_config, multimodal: { enabled: !!kb.vlm_config?.enabled }, storageBackendId: kb.storage_backend_id, storageProvider: kb.storage_config?.provider || 'local', documentSplitting: { chunkSize: c.chunk_size || 512, chunkOverlap: c.chunk_size ? c.chunk_overlap : 80, separators: c.separators?.length ? c.separators : ['\n\n', '\n', '。', '!', '?', ';', ';'], parserEngineRules: c.parser_engine_rules, enableParentChild: !!c.enable_parent_child, parentChunkSize: c.parent_chunk_size || 4096, childChunkSize: c.child_chunk_size || 384, strategy: c.strategy, tokenLimit: c.token_limit, languages: c.languages, tableMetadataInstructions: c.table_metadata_instructions, }, nodeExtract: { ...e, customInstructions: e.custom_instructions }, questionGeneration: { enabled: !!q.enabled, questionCount: q.question_count, customInstructions: q.custom_instructions }, } await api(`/initialization/config/${kb.id}`, 'PUT', request) const after = (await api(`/knowledge-bases/${kb.id}`)).data assert.equal(after.summary_model_id, model) assert.equal(after.embedding_model_id, kb.embedding_model_id) assert.deepEqual(after.indexing_strategy, kb.indexing_strategy) assert.deepEqual(after.wiki_config, kb.wiki_config) assert.equal(after.storage_backend_id, kb.storage_backend_id) } const after = (await api('/knowledge-bases')).data console.log(JSON.stringify({ updated: targets.map(kb => kb.name), initialized: after.filter(kb => kb.summary_model_id && kb.embedding_model_id).length, total: after.length, existing_indexes_preserved: true })) } main().catch(error => { console.error(error.message); process.exitCode = 1 })