11import * as fs from "fs" ;
22import * as path from "path" ;
33import * as os from "os" ;
4+ import { writeFile } from "fs/promises" ;
45import {
56 setChannelCwd as setChannelCwdInConfig ,
67} from "./ode" ;
@@ -16,6 +17,7 @@ const homedir = typeof os.homedir === "function" ? os.homedir : () => "";
1617const ODE_CONFIG_DIR = join ( homedir ( ) , ".config" , "ode" ) ;
1718const SETTINGS_FILE = join ( ODE_CONFIG_DIR , "settings.json" ) ;
1819const AGENTS_DIR = join ( ODE_CONFIG_DIR , "agents" ) ;
20+ const SETTINGS_SAVE_DEBOUNCE_MS = 1000 ;
1921
2022export interface ChannelSettings {
2123 threadSessions : Record < string , string > ; // threadId -> sessionId
@@ -34,6 +36,9 @@ export interface Settings {
3436}
3537
3638let cachedSettings : Settings | null = null ;
39+ let pendingSettingsWriteTimer : ReturnType < typeof setTimeout > | null = null ;
40+ let pendingSettingsSnapshot : Settings | null = null ;
41+ let settingsWriteChain : Promise < void > = Promise . resolve ( ) ;
3742
3843function ensureDataDir ( ) : void {
3944 if ( ! existsSync ( ODE_CONFIG_DIR ) ) {
@@ -95,7 +100,26 @@ export function saveSettings(settings: Settings): void {
95100 ...settings ,
96101 channels : normalizedChannels ,
97102 } ;
98- writeFileSync ( SETTINGS_FILE , JSON . stringify ( cachedSettings , null , 2 ) ) ;
103+
104+ pendingSettingsSnapshot = structuredClone ( cachedSettings ) ;
105+ if ( pendingSettingsWriteTimer ) {
106+ clearTimeout ( pendingSettingsWriteTimer ) ;
107+ pendingSettingsWriteTimer = null ;
108+ }
109+
110+ pendingSettingsWriteTimer = setTimeout ( ( ) => {
111+ pendingSettingsWriteTimer = null ;
112+ const snapshot = pendingSettingsSnapshot ;
113+ if ( ! snapshot ) return ;
114+ pendingSettingsSnapshot = null ;
115+ const payload = JSON . stringify ( snapshot , null , 2 ) ;
116+ settingsWriteChain = settingsWriteChain
117+ . catch ( ( ) => undefined )
118+ . then ( async ( ) => {
119+ await writeFile ( SETTINGS_FILE , payload , "utf-8" ) ;
120+ } )
121+ . catch ( ( ) => undefined ) ;
122+ } , SETTINGS_SAVE_DEBOUNCE_MS ) ;
99123}
100124
101125export function getPendingRestartMessages ( ) : PendingRestartMessage [ ] {
0 commit comments