@@ -13,6 +13,7 @@ import { writeDesktop3pConfig, type Desktop3pConfigMode, parseDesktop3pModeArgs
1313import { filterCatalogVisibleModels , desktopVisibleNativeSlugs } from "../codex/catalog" ;
1414import { buildClaudeDesktopState , fetchAllModels } from "../server/management-api" ;
1515import { findLiveProxy } from "../server/proxy-liveness" ;
16+ import { runtimeRequest } from "./runtime-api" ;
1617
1718function isFamily ( value : string | undefined ) : value is DesktopFamily {
1819 return ! ! value && ( DESKTOP_FAMILIES as readonly string [ ] ) . includes ( value ) ;
@@ -28,20 +29,51 @@ function printDesktopHelp(): void {
2829 ocx claude desktop import <path> [--apply]` ) ;
2930}
3031
31- async function applyProfile ( profile : DesktopProfile , mode : Desktop3pConfigMode ) : Promise < { ok : boolean ; path : string ; reason ?: string } > {
32+ export interface ApplyProfileDeps {
33+ findLiveProxyImpl ?: typeof findLiveProxy ;
34+ postApplyImpl ?: (
35+ mode : Desktop3pConfigMode ,
36+ profile : DesktopProfile ,
37+ ) => Promise < { ok ?: boolean ; path ?: string ; error ?: string } > ;
38+ }
39+
40+ export async function applyProfile (
41+ profile : DesktopProfile ,
42+ mode : Desktop3pConfigMode ,
43+ deps : ApplyProfileDeps = { } ,
44+ ) : Promise < { ok : boolean ; path : string ; reason ?: string } > {
3245 const config = loadConfig ( ) ;
3346 const state = await buildClaudeDesktopState ( config , profile ) ;
3447 config . claudeCode = { ...( config . claudeCode ?? { } ) , desktopProfile : state . profile } ;
3548 saveConfigPreservingClaudeCode ( config ) ;
36- const live = await findLiveProxy ( ) ;
49+ const live = await ( deps . findLiveProxyImpl ?? findLiveProxy ) ( ) ;
50+ if ( live ) {
51+ // #859: the Desktop alias reverse-map is process-local. Applying through the
52+ // serving process installs the map there; a local-only write leaves the
53+ // daemon unable to decode aliases, and the provider rejects them (400).
54+ const post = deps . postApplyImpl ?? ( async ( m : Desktop3pConfigMode , p : DesktopProfile ) =>
55+ runtimeRequest < { ok ?: boolean ; path ?: string ; error ?: string } > (
56+ "/api/claude-desktop/apply" ,
57+ // The daemon's config may be older than what we just saved, so the
58+ // profile travels with the request instead of being re-read there.
59+ { method : "POST" , body : JSON . stringify ( { mode : m , profile : p } ) } ,
60+ ) ) ;
61+ try {
62+ const applied = await post ( mode , state . profile ) ;
63+ if ( applied . ok === false ) return { ok : false , path : applied . path ?? "" , reason : applied . error ?? "daemon apply failed" } ;
64+ return { ok : true , path : applied . path ?? "" } ;
65+ } catch ( error ) {
66+ return { ok : false , path : "" , reason : error instanceof Error ? error . message : String ( error ) } ;
67+ }
68+ }
3769 const allModels = await fetchAllModels ( config ) ;
3870 const routed = filterCatalogVisibleModels ( allModels , config ) . map ( model => ( {
3971 provider : model . provider ,
4072 id : model . id ,
4173 contextWindow : model . contextWindow ,
4274 } ) ) ;
4375 const result = writeDesktop3pConfig (
44- live ?. port ?? config . port ?? 10100 ,
76+ config . port ?? 10100 ,
4577 [ ...desktopVisibleNativeSlugs ( config ) ] ,
4678 routed ,
4779 config . apiKeys ?. [ 0 ] ?. key ,
@@ -51,7 +83,7 @@ async function applyProfile(profile: DesktopProfile, mode: Desktop3pConfigMode):
5183 return { ok : result . written , path : result . path , reason : result . reason } ;
5284}
5385
54- export async function handleClaudeDesktopCommand ( argv : string [ ] ) : Promise < number > {
86+ export async function handleClaudeDesktopCommand ( argv : string [ ] , deps : ApplyProfileDeps = { } ) : Promise < number > {
5587 const command = argv [ 0 ] ;
5688 if ( command === "help" || command === "--help" || command === "-h" ) {
5789 printDesktopHelp ( ) ;
@@ -72,8 +104,12 @@ export async function handleClaudeDesktopCommand(argv: string[]): Promise<number
72104 try {
73105 const config = loadConfig ( ) ;
74106 const state = await buildClaudeDesktopState ( config ) ;
75- const result = await applyProfile ( state . profile , parsedMode . mode ) ;
76- if ( ! result . ok ) { console . error ( `설정 적용 실패: ${ result . reason ?? "unknown error" } ` ) ; return 1 ; }
107+ const result = await applyProfile ( state . profile , parsedMode . mode , deps ) ;
108+ if ( ! result . ok ) {
109+ console . error ( `설정 적용 실패: ${ result . reason ?? "unknown error" } ` ) ;
110+ console . error ( "프로필은 저장되었지만 Claude Desktop 설정 파일에는 반영되지 않았습니다. 프록시 상태를 확인한 뒤 다시 적용해 주세요." ) ;
111+ return 1 ;
112+ }
77113 console . log ( `Claude Desktop 설정을 적용했습니다: ${ result . path } ` ) ;
78114 console . log ( "Claude Desktop을 완전히 종료한 뒤 다시 열어 주세요." ) ;
79115 return 0 ;
@@ -137,7 +173,7 @@ export async function handleClaudeDesktopCommand(argv: string[]): Promise<number
137173 config . claudeCode = { ...( config . claudeCode ?? { } ) , desktopProfile : reconciled } ;
138174 saveConfigPreservingClaudeCode ( config ) ;
139175 if ( flags . includes ( "--apply" ) ) {
140- const result = await applyProfile ( reconciled , "static" ) ;
176+ const result = await applyProfile ( reconciled , "static" , deps ) ;
141177 if ( ! result . ok ) { console . error ( `프로필은 저장했지만 Desktop 적용에 실패했습니다: ${ result . reason ?? "unknown error" } ` ) ; return 1 ; }
142178 }
143179 console . log ( "Claude Desktop 프로필을 가져왔습니다." ) ;
0 commit comments