1414import * as NodeOS from "node:os" ;
1515
1616import {
17+ ClaudeSettings ,
18+ CodexSettings ,
19+ type ServerSettings as ServerSettingsConfig ,
1720 USAGE_CONTRACT_VERSION ,
1821 type UsageProviderKind ,
1922 type UsageSource ,
@@ -85,6 +88,51 @@ const encodeRatesCache = Schema.encodeEffect(
8588const ScanCacheJson = Schema . fromJsonString ( Schema . Unknown as unknown as Schema . Codec < unknown > ) ;
8689const decodeScanCacheFile = Schema . decodeUnknownEffect ( ScanCacheJson ) ;
8790const encodeScanCacheFile = Schema . encodeEffect ( ScanCacheJson ) ;
91+ const decodeCodexSettings = Schema . decodeUnknownOption ( CodexSettings ) ;
92+ const decodeClaudeSettings = Schema . decodeUnknownOption ( ClaudeSettings ) ;
93+
94+ export const resolveTranscriptDirsForSettings = Effect . fn (
95+ "UsageService.resolveTranscriptDirsForSettings" ,
96+ ) ( function * ( settings : ServerSettingsConfig ) {
97+ const fileSystem = yield * FileSystem . FileSystem ;
98+ const path = yield * Path . Path ;
99+ const codexConfigs = [ settings . providers . codex ] ;
100+ const claudeConfigs = [ settings . providers . claudeAgent ] ;
101+
102+ for ( const instance of Object . values ( settings . providerInstances ) ) {
103+ if ( instance . enabled === false ) continue ;
104+ if ( instance . driver === "codex" ) {
105+ const config = decodeCodexSettings ( instance . config ?? { } ) ;
106+ if ( Option . isSome ( config ) && ( instance . enabled ?? config . value . enabled ) ) {
107+ codexConfigs . push ( config . value ) ;
108+ }
109+ } else if ( instance . driver === "claudeAgent" ) {
110+ const config = decodeClaudeSettings ( instance . config ?? { } ) ;
111+ if ( Option . isSome ( config ) && ( instance . enabled ?? config . value . enabled ) ) {
112+ claudeConfigs . push ( config . value ) ;
113+ }
114+ }
115+ }
116+
117+ const dirs : Array < { readonly provider : UsageProviderKind ; readonly dir : string } > = [ ] ;
118+ for ( const config of claudeConfigs ) {
119+ const homePath = yield * resolveClaudeHomePath ( config ) ;
120+ const nested = path . join ( homePath , ".claude" , "projects" ) ;
121+ const nestedExists = yield * fileSystem
122+ . exists ( nested )
123+ . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( false ) ) ) ;
124+ dirs . push ( {
125+ provider : "claude" ,
126+ dir : nestedExists ? nested : path . join ( homePath , "projects" ) ,
127+ } ) ;
128+ }
129+ for ( const config of codexConfigs ) {
130+ const layout = yield * resolveCodexHomeLayout ( config ) ;
131+ dirs . push ( { provider : "codex" , dir : path . join ( layout . sharedHomePath , "sessions" ) } ) ;
132+ }
133+
134+ return [ ...new Map ( dirs . map ( ( entry ) => [ `${ entry . provider } \0${ entry . dir } ` , entry ] ) ) . values ( ) ] ;
135+ } ) ;
88136
89137export class UsageService extends Context . Service <
90138 UsageService ,
@@ -184,19 +232,6 @@ export const make = Effect.gen(function* () {
184232 ) ;
185233 } ) ;
186234
187- /**
188- * Claude's config dir is the home itself when overridden, but a default
189- * install nests transcripts under `~/.claude/projects`. Probe both.
190- */
191- const resolveClaudeTranscriptDir = ( homePath : string ) =>
192- Effect . gen ( function * ( ) {
193- const nested = path . join ( homePath , ".claude" , "projects" ) ;
194- const nestedExists = yield * fileSystem
195- . exists ( nested )
196- . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( false ) ) ) ;
197- return nestedExists ? nested : path . join ( homePath , "projects" ) ;
198- } ) ;
199-
200235 /** Resolves the transcript directory for each provider. */
201236 const resolveTranscriptDirs = Effect . fn ( "UsageService.resolveTranscriptDirs" ) ( function * ( ) {
202237 // A settings failure must surface as an error: swallowing it here would
@@ -215,14 +250,9 @@ export const make = Effect.gen(function* () {
215250 ) ,
216251 ) ;
217252
218- const claudeHome = yield * resolveClaudeHomePath ( settings . providers . claudeAgent ) ;
219- const claudeDir = yield * resolveClaudeTranscriptDir ( claudeHome ) ;
220- const codexLayout = yield * resolveCodexHomeLayout ( settings . providers . codex ) ;
221-
222- return [
223- { provider : "claude" as const , dir : claudeDir } ,
224- { provider : "codex" as const , dir : path . join ( codexLayout . sharedHomePath , "sessions" ) } ,
225- ] ;
253+ return yield * resolveTranscriptDirsForSettings ( settings ) . pipe (
254+ Effect . provideService ( FileSystem . FileSystem , fileSystem ) ,
255+ ) ;
226256 } ) ;
227257
228258 /**
0 commit comments