MCPcopy Create free account
hub / github.com/diillson/chatcli / maybeEnableLocalHub

Method maybeEnableLocalHub

cli/hub_local.go:237–270  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
237func (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}

Callers 1

startHubSyncMethod · 0.95

Calls 14

DefaultDBPathFunction · 0.92
OpenSQLiteStoreFunction · 0.92
resolveHubEnabledFunction · 0.85
hubEnabledSourceFunction · 0.85
resolveHubTTLFunction · 0.85
resolveHubPrincipalFunction · 0.85
newHubSyncFunction · 0.85
newLocalHubClientFunction · 0.85
WarnMethod · 0.80
ErrorMethod · 0.65
CloseMethod · 0.65
PurgeIdleMethod · 0.65

Tested by

no test coverage detected