maybeEnableLocalHub wires a standalone CLI to the on-disk hub unless the session is already connected to a remote hub. The hub is on by default; the `enabled` setting (or CHATCLI_HUB_ENABLED=false) opts out. Returns a closer for the opened database, or nil when local mode is off.
(ctx context.Context)
| 235 | // `enabled` setting (or CHATCLI_HUB_ENABLED=false) opts out. Returns a closer |
| 236 | // for the opened database, or nil when local mode is off. |
| 237 | func (cli *ChatCLI) maybeEnableLocalHub(ctx context.Context) func() { |
| 238 | if cli.hubSync != nil || cli.isRemote { |
| 239 | return nil // a /connect session already owns hub sync |
| 240 | } |
| 241 | dbPath, err := hub.DefaultDBPath() |
| 242 | if err != nil { |
| 243 | cli.logger.Warn("local hub: cannot resolve db path; disabling", zap.Error(err)) |
| 244 | return nil |
| 245 | } |
| 246 | store, err := hub.OpenSQLiteStore(ctx, dbPath, cli.logger) |
| 247 | if err != nil { |
| 248 | cli.logger.Warn("local hub: cannot open db; disabling", zap.Error(err)) |
| 249 | return nil |
| 250 | } |
| 251 | // The enabled setting lives in the db (so /config hub set enabled off reaches |
| 252 | // the gateway too); it can only be read once the store is open. Disabling is |
| 253 | // a legitimate choice but never a silent one — it severs cross-channel |
| 254 | // continuity with the gateway. |
| 255 | if !resolveHubEnabled(ctx, store) { |
| 256 | cli.logger.Warn("local hub: conversation hub DISABLED via setting/env; this session will not share context with the gateway", |
| 257 | zap.String("source", hubEnabledSource(ctx, store))) |
| 258 | _ = store.Close() |
| 259 | return nil |
| 260 | } |
| 261 | if n, err := store.PurgeIdle(ctx, resolveHubTTL(ctx, store)); err != nil { |
| 262 | cli.logger.Warn("local hub: purge idle failed", zap.Error(err)) |
| 263 | } else if n > 0 { |
| 264 | cli.logger.Info("local hub: purged idle conversations", zap.Int("count", n)) |
| 265 | } |
| 266 | principal := resolveHubPrincipal(ctx, store) |
| 267 | cli.hubSync = newHubSync(newLocalHubClient(store, principal), cli.logger) |
| 268 | cli.logger.Info("local hub mode enabled", zap.String("principal", principal), zap.String("db", dbPath)) |
| 269 | return func() { _ = store.Close() } |
| 270 | } |
no test coverage detected