@@ -10,7 +10,14 @@ import {
1010 type RequestPermissionRequest ,
1111 type RequestPermissionResponse ,
1212} from "@agentclientprotocol/sdk" ;
13- import { Agent , getLlmGatewayUrl , type OnLogCallback } from "@posthog/agent" ;
13+ import { Agent } from "@posthog/agent/agent" ;
14+ import {
15+ fetchGatewayModels ,
16+ formatGatewayModelName ,
17+ getProviderName ,
18+ } from "@posthog/agent/gateway-models" ;
19+ import { getLlmGatewayUrl } from "@posthog/agent/posthog-api" ;
20+ import type { OnLogCallback } from "@posthog/agent/types" ;
1421import { app } from "electron" ;
1522import { injectable } from "inversify" ;
1623import type { AcpMessage } from "../../../shared/types/session-events.js" ;
@@ -152,6 +159,12 @@ interface ManagedSession {
152159 needsRecreation : boolean ;
153160 promptPending : boolean ;
154161 pendingContext ?: string ;
162+ availableModels ?: Array < {
163+ modelId : string ;
164+ name : string ;
165+ description ?: string | null ;
166+ } > ;
167+ currentModelId ?: string ;
155168}
156169
157170function getClaudeCliPath ( ) : string {
@@ -383,25 +396,43 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
383396
384397 const mcpServers = this . buildMcpServers ( credentials ) ;
385398
399+ let availableModels :
400+ | Array < { modelId : string ; name : string ; description ?: string | null } >
401+ | undefined ;
402+ let currentModelId : string | undefined ;
403+
386404 if ( isReconnect ) {
387- await connection . extMethod ( "_posthog/session/resume" , {
388- sessionId : taskRunId ,
389- cwd : repoPath ,
390- mcpServers,
391- _meta : {
392- ...( logUrl && {
393- persistence : { taskId, runId : taskRunId , logUrl } ,
394- } ) ,
395- ...( sdkSessionId && { sdkSessionId } ) ,
396- ...( additionalDirectories ?. length && {
397- claudeCode : {
398- options : { additionalDirectories } ,
399- } ,
400- } ) ,
405+ const resumeResponse = await connection . extMethod (
406+ "_posthog/session/resume" ,
407+ {
408+ sessionId : taskRunId ,
409+ cwd : repoPath ,
410+ mcpServers,
411+ _meta : {
412+ ...( logUrl && {
413+ persistence : { taskId, runId : taskRunId , logUrl } ,
414+ } ) ,
415+ ...( sdkSessionId && { sdkSessionId } ) ,
416+ ...( additionalDirectories ?. length && {
417+ claudeCode : {
418+ options : { additionalDirectories } ,
419+ } ,
420+ } ) ,
421+ } ,
401422 } ,
402- } ) ;
423+ ) ;
424+ const resumeMeta = resumeResponse ?. _meta as
425+ | {
426+ models ?: {
427+ availableModels ?: typeof availableModels ;
428+ currentModelId ?: string ;
429+ } ;
430+ }
431+ | undefined ;
432+ availableModels = resumeMeta ?. models ?. availableModels ;
433+ currentModelId = resumeMeta ?. models ?. currentModelId ;
403434 } else {
404- await connection . newSession ( {
435+ const newSessionResponse = await connection . newSession ( {
405436 cwd : repoPath ,
406437 mcpServers,
407438 _meta : {
@@ -415,6 +446,8 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
415446 } ) ,
416447 } ,
417448 } ) ;
449+ availableModels = newSessionResponse . models ?. availableModels ;
450+ currentModelId = newSessionResponse . models ?. currentModelId ;
418451 }
419452
420453 const session : ManagedSession = {
@@ -430,6 +463,8 @@ export class AgentService extends TypedEventEmitter<AgentServiceEvents> {
430463 config,
431464 needsRecreation : false ,
432465 promptPending : false ,
466+ availableModels,
467+ currentModelId,
433468 } ;
434469
435470 this . sessions . set ( taskRunId , session ) ;
@@ -999,7 +1034,12 @@ For git operations while detached:
9991034 }
10001035
10011036 private toSessionResponse ( session : ManagedSession ) : SessionResponse {
1002- return { sessionId : session . taskRunId , channel : session . channel } ;
1037+ return {
1038+ sessionId : session . taskRunId ,
1039+ channel : session . channel ,
1040+ availableModels : session . availableModels ,
1041+ currentModelId : session . currentModelId ,
1042+ } ;
10031043 }
10041044
10051045 /**
@@ -1109,4 +1149,16 @@ For git operations while detached:
11091149 log . debug ( "Error in PR URL detection" , { taskRunId, error : err } ) ;
11101150 }
11111151 }
1152+
1153+ async getGatewayModels ( apiHost : string , _apiKey : string ) {
1154+ const gatewayUrl = getLlmGatewayUrl ( apiHost ) ;
1155+ const models = await fetchGatewayModels ( { gatewayUrl } ) ;
1156+
1157+ return models . map ( ( model ) => ( {
1158+ modelId : model . id ,
1159+ name : formatGatewayModelName ( model ) ,
1160+ description : `Context: ${ model . context_window . toLocaleString ( ) } tokens` ,
1161+ provider : getProviderName ( model . owned_by ) ,
1162+ } ) ) ;
1163+ }
11121164}
0 commit comments