fix(models): watch auth-profiles.json so externally added creds becom… · openclaw/openclaw@06a6d2b
sjf
·
2026-05-22
·
via Recent Commits to openclaw:main
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +import path from "node:path"; |
| 2 | +import chokidar from "chokidar"; |
| 3 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 4 | +import { listAgentIds, resolveAgentDir } from "./agent-scope-config.js"; |
| 5 | + |
| 6 | +// Watches every configured agent's auth-profiles.json and fires onChange when |
| 7 | +// any of them is written. Covers the stale-FALSE case where a user adds |
| 8 | +// credentials via an external tool (`codex login`, hand-edited file, etc.) — |
| 9 | +// without the watcher the gateway's prepared auth map stays inert until the |
| 10 | +// next reload or restart. |
| 11 | + |
| 12 | +export type AuthProfilesWatcherHandle = { |
| 13 | +stop: () => Promise<void>; |
| 14 | +}; |
| 15 | + |
| 16 | +export function watchAuthProfilesForChanges(params: { |
| 17 | +cfg: OpenClawConfig; |
| 18 | +onChange: () => void; |
| 19 | +}): AuthProfilesWatcherHandle { |
| 20 | +const watchPaths = listAgentIds(params.cfg).map((agentId) => |
| 21 | +path.join(resolveAgentDir(params.cfg, agentId), "auth-profiles.json"), |
| 22 | +); |
| 23 | +const watcher = chokidar.watch(watchPaths, { |
| 24 | +ignoreInitial: true, |
| 25 | +awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 }, |
| 26 | +usePolling: Boolean(process.env.VITEST), |
| 27 | +}); |
| 28 | +watcher.on("all", () => { |
| 29 | +try { |
| 30 | +params.onChange(); |
| 31 | +} catch { |
| 32 | +// onChange errors must not crash the watcher. |
| 33 | +} |
| 34 | +}); |
| 35 | +return { |
| 36 | +stop: async () => { |
| 37 | +await watcher.close(); |
| 38 | +}, |
| 39 | +}; |
| 40 | +} |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。