chatboxai/chatbox · error · Error

Failed to create knowledge base

Error message

Failed to create knowledge base

What it means

Thrown by getModel() when `settings.provider` is falsy. Identical precondition to the one in getProviderSettings but at the top-level model factory — getModel() will not even attempt a registry lookup without a provider. The message is the same ('Model provider must not be empty.') so callers cannot tell which function threw from the message alone; the stack trace points to index.ts:156.

Source

Thrown at src/main/knowledge-base/ipc-handlers.ts:125

        }

        const db = getDatabase()
        const documentParserJson = documentParser ? JSON.stringify(documentParser) : null
        const rs = await db.execute({
          sql: 'INSERT INTO knowledge_base (name, embedding_model, rerank_model, vision_model, document_parser, provider_mode) VALUES (?, ?, ?, ?, ?, ?)',
          args: [
            name.trim(),
            embeddingModel,
            rerankModel || null,
            visionModel || null,
            documentParserJson,
            providerMode || null,
          ],
        })
        const id = rs.lastInsertRowid

        if (!id) {
          throw new Error('Failed to create knowledge base')
        }

        log.info(`[IPC] Knowledge base created successfully: id=${id}, name=${name}`)
        return { id, name: name.trim() }
      } catch (error: unknown) {
        log.error(`ipcMain: kb:create failed for name=${name}`, error)
        sentry.withScope((scope) => {
          scope.setTag('component', 'knowledge-base-ipc')
          scope.setTag('operation', 'kb_create')
          scope.setExtra('name', name)
          scope.setExtra('embeddingModel', embeddingModel)
          scope.setExtra('rerankModel', rerankModel)
          scope.setExtra('visionModel', visionModel)
          scope.setExtra('documentParser', documentParser?.type)
          sentry.captureException(error)
        })
        throw error
      }

View on GitHub (pinned to 81571269ad)

Solutions

  1. Before calling getModel(), assert `settings.provider` is non-empty and fall back to the global default provider.
  2. Centralise session construction so `provider` is always set from a single source of truth.
  3. Add a runtime guard at the UI/controller boundary that prevents dispatching chat/paint actions until a provider is chosen.
  4. Run a settings-migration/repair on load to backfill `provider` from `globalSettings.defaultModelProvider`.

Example fix

// before
const model = getModel(session, globals, config, deps)  // throws if session.provider empty

// after
if (!session.provider) {
  throw new Error('Cannot start chat: no provider selected')  // surfaced clearly to UI
}
const model = getModel(session, globals, config, deps)
Defensive patterns

Strategy: validation

Validate before calling

function ensureProvider(session: SessionSettings, globals: Settings): string {
  if (!session.provider) session.provider = (globals as any).defaultModelProvider || getBuiltinProviderIds()[0]
  return session.provider
}

Type guard

function hasProvider(s: { provider?: string }): s is { provider: string } {
  return typeof s.provider === 'string' && s.provider.length > 0
}

Try / catch

if (!hasProvider(settings)) throw new UserFacingError('Cannot start: no provider selected')
const model = getModel(settings, globals, config, deps)

Prevention

When it happens

Trigger: getModel() invoked with a SessionSettings object whose `provider` is empty (e.g. before the user picks a model); a code path that builds a fresh settings object and forgets to copy `provider`; deserialised session after a migration that dropped the field; programmatic callers passing `{ modelId: '...' }` without `provider`.

Common situations: Background tasks (e.g. quick-action, agent loop, translation) call getModel() with a partial settings snapshot that lost `provider`; new chat created before defaults applied; settings loaded from a corrupt file; refactor changed the SessionSettings shape and a caller was not updated.

Related errors


AI-assisted analysis of chatboxai/chatbox@81571269ad (2026-08-12). Data as JSON: /api/errors/bc563e62f778ab68. Report an issue: GitHub.