1- import Agent from "../../agent/index.js"
2- import Global from "../../global/index.js"
3- import * as Type from "../../type/index.js"
4- import * as acpt from "./session-types.js"
5- import { getCurrentSession , loadSession , Session } from "../../session/index.js"
1+ import Session from "../../session/index.js"
2+ import { getCurrentSession , loadSession } from "../../session/index.js"
63import bus from "../../bus/index.js"
74import id from "../../id/index.js"
85import project from "../../project/index.js"
@@ -601,21 +598,116 @@ export async function createRepl(agent: Agent): Promise<void> {
601598 }
602599}
603600
604- // SESSION MANAGEMENT METHODS FOR ACP
601+ // SESSION MANAGEMENT TYPES FOR ACP
605602// Issue #8931: Expose TUI Session Management Commands via ACP
606603
604+ export interface SessionInfo {
605+ id : string
606+ title : string
607+ createdAt : string
608+ updatedAt : string
609+ projectPath ?: string
610+ messageCount : number
611+ }
612+
613+ export interface SessionListParams {
614+ limit ?: number
615+ projectPath ?: string
616+ }
617+
618+ export interface SessionListResult {
619+ sessions : SessionInfo [ ]
620+ }
621+
622+ export interface SessionSwitchParams {
623+ sessionId : string
624+ }
625+
626+ export interface SessionCreateParams {
627+ title ?: string
628+ projectPath ?: string
629+ }
630+
631+ export interface SessionForkParams {
632+ sessionId : string
633+ messageId : string
634+ title ?: string
635+ }
636+
637+ export interface SessionRenameParams {
638+ sessionId : string
639+ title : string
640+ }
641+
642+ export interface SessionDeleteParams {
643+ sessionId : string
644+ }
645+
646+ export interface SessionInfoParams {
647+ sessionId : string
648+ }
649+
650+ export interface SessionUndoParams {
651+ sessionId ?: string
652+ }
653+
654+ export interface SessionRedoParams {
655+ sessionId ?: string
656+ }
657+
658+ export interface SessionCompactParams {
659+ sessionId ?: string
660+ }
661+
662+ export interface SessionExportParams {
663+ sessionId ?: string
664+ format ?: "text" | "json" | "markdown"
665+ }
666+
667+ export interface SessionExportResult {
668+ content : string
669+ filename : string
670+ }
671+
672+ export interface SessionJumpParams {
673+ sessionId : string
674+ messageId : string
675+ }
676+
677+ export interface SessionDuplicateParams {
678+ sessionId : string
679+ title ?: string
680+ }
681+
682+ // Session storage location reference
683+ export const SESSION_STORAGE_PATH = "~/.local/share/opencode/storage/session/"
684+
685+ // Helper to format session for API response
686+ function formatSessionInfo ( session : any ) : SessionInfo {
687+ return {
688+ id : session . id ,
689+ title : session . title ?? `Session ${ session . id ?. slice ( 0 , 8 ) ?? "unknown" } ` ,
690+ createdAt : session . createdAt ?? new Date ( ) . toISOString ( ) ,
691+ updatedAt : session . updatedAt ?? new Date ( ) . toISOString ( ) ,
692+ projectPath : session . projectPath ,
693+ messageCount : session . messages ?. length ?? 0 ,
694+ }
695+ }
696+
697+ // SESSION MANAGEMENT HANDLERS
698+
607699/**
608700 * List all available sessions
609701 */
610- async function listSessions (
611- params : acpt . SessionListParams ,
612- ) : Promise < acpt . SessionListResult > {
702+ export async function listSessions (
703+ params : SessionListParams = { } ,
704+ ) : Promise < SessionListResult > {
613705 const sessionDir = path . join (
614706 os . home ( ) ,
615707 ".local/share/opencode/storage/session" ,
616708 )
617709
618- const sessions : acpt . SessionInfo [ ] = [ ]
710+ const sessions : SessionInfo [ ] = [ ]
619711
620712 if ( fs . existsSync ( sessionDir ) ) {
621713 const entries = fs . readdirSync ( sessionDir )
@@ -641,16 +733,7 @@ async function listSessions(
641733 continue
642734 }
643735
644- sessions . push ( {
645- id : sessionData . id ?? entry ,
646- title :
647- sessionData . title ??
648- `Session ${ entry . slice ( 0 , 8 ) } ` ,
649- createdAt : sessionData . createdAt ?? new Date ( ) . toISOString ( ) ,
650- updatedAt : sessionData . updatedAt ?? new Date ( ) . toISOString ( ) ,
651- projectPath : sessionData . projectPath ,
652- messageCount : sessionData . messages ?. length ?? 0 ,
653- } )
736+ sessions . push ( formatSessionInfo ( sessionData ) )
654737 }
655738 } catch {
656739 // Skip invalid sessions
@@ -671,8 +754,8 @@ async function listSessions(
671754/**
672755 * Switch to an existing session
673756 */
674- async function switchSession (
675- params : acpt . SessionSwitchParams ,
757+ export async function switchSession (
758+ params : SessionSwitchParams ,
676759) : Promise < { success : boolean ; sessionId : string } > {
677760 const { sessionId } = params
678761
@@ -704,36 +787,29 @@ async function switchSession(
704787/**
705788 * Create a new session
706789 */
707- async function createSession (
708- params : acpt . SessionCreateParams ,
709- ) : Promise < acpt . SessionInfo > {
790+ export async function createSession (
791+ params : SessionCreateParams = { } ,
792+ ) : Promise < SessionInfo > {
710793 const { title, projectPath } = params
711794
712795 const session = await loadSession ( {
713796 createdAt : new Date ( ) ,
714797 updatedAt : new Date ( ) ,
715798 title : title ?? "New Session" ,
716799 id : id . generate ( ) ,
717- agent : await agent . clone ( ) ,
800+ agent : undefined ,
718801 workingDirectory : projectPath ?? process . cwd ( ) ,
719802 } )
720803
721- return {
722- id : session . id ,
723- title : session . title ?? `Session ${ session . id . slice ( 0 , 8 ) } ` ,
724- createdAt : session . createdAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
725- updatedAt : session . updatedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
726- projectPath : session . projectPath ,
727- messageCount : session . messages ?. length ?? 0 ,
728- }
804+ return formatSessionInfo ( session )
729805}
730806
731807/**
732808 * Fork a session from a specific message
733809 */
734- async function forkSession (
735- params : acpt . SessionForkParams ,
736- ) : Promise < acpt . SessionInfo > {
810+ export async function forkSession (
811+ params : SessionForkParams ,
812+ ) : Promise < SessionInfo > {
737813 const { sessionId, messageId, title } = params
738814
739815 // Load the source session
@@ -772,7 +848,7 @@ async function forkSession(
772848 title ??
773849 `${ sessionData . title ?? "Fork" } (${ new Date ( ) . toLocaleDateString ( ) } )` ,
774850 id : id . generate ( ) ,
775- agent : await agent . clone ( ) ,
851+ agent : undefined ,
776852 workingDirectory : sessionData . projectPath ?? process . cwd ( ) ,
777853 messages : messages . map ( ( m : any ) => ( {
778854 ...m ,
@@ -782,21 +858,14 @@ async function forkSession(
782858 } ) ) ,
783859 } )
784860
785- return {
786- id : newSession . id ,
787- title : newSession . title ?? `Session ${ newSession . id . slice ( 0 , 8 ) } ` ,
788- createdAt : newSession . createdAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
789- updatedAt : newSession . updatedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
790- projectPath : newSession . projectPath ,
791- messageCount : newSession . messages ?. length ?? 0 ,
792- }
861+ return formatSessionInfo ( newSession )
793862}
794863
795864/**
796865 * Rename a session
797866 */
798- async function renameSession (
799- params : acpt . SessionRenameParams ,
867+ export async function renameSession (
868+ params : SessionRenameParams ,
800869) : Promise < { success : boolean ; sessionId : string } > {
801870 const { sessionId, title } = params
802871
@@ -833,8 +902,8 @@ async function renameSession(
833902/**
834903 * Delete a session
835904 */
836- async function deleteSession (
837- params : acpt . SessionDeleteParams ,
905+ export async function deleteSession (
906+ params : SessionDeleteParams ,
838907) : Promise < { success : boolean ; sessionId : string } > {
839908 const { sessionId } = params
840909
@@ -861,9 +930,9 @@ async function deleteSession(
861930/**
862931 * Get information about a specific session
863932 */
864- async function getSessionInfo (
865- params : acpt . SessionInfoParams ,
866- ) : Promise < acpt . SessionInfo > {
933+ export async function getSessionInfo (
934+ params : SessionInfoParams ,
935+ ) : Promise < SessionInfo > {
867936 const { sessionId } = params
868937
869938 if ( ! sessionId ) {
@@ -884,21 +953,14 @@ async function getSessionInfo(
884953 const content = fs . readFileSync ( sessionPath , "utf-8" )
885954 const sessionData = JSON . parse ( content )
886955
887- return {
888- id : sessionData . id ?? sessionId ,
889- title : sessionData . title ?? `Session ${ sessionId . slice ( 0 , 8 ) } ` ,
890- createdAt : sessionData . createdAt ?? new Date ( ) . toISOString ( ) ,
891- updatedAt : sessionData . updatedAt ?? new Date ( ) . toISOString ( ) ,
892- projectPath : sessionData . projectPath ,
893- messageCount : sessionData . messages ?. length ?? 0 ,
894- }
956+ return formatSessionInfo ( sessionData )
895957}
896958
897959/**
898960 * Undo the last message in a session
899961 */
900- async function undoMessage (
901- params : acpt . SessionUndoParams ,
962+ export async function undoMessage (
963+ params : SessionUndoParams = { } ,
902964) : Promise < { success : boolean ; sessionId : string } > {
903965 const { sessionId } = params
904966
@@ -945,8 +1007,8 @@ async function undoMessage(
9451007/**
9461008 * Redo a message in a session
9471009 */
948- async function redoMessage (
949- params : acpt . SessionRedoParams ,
1010+ export async function redoMessage (
1011+ params : SessionRedoParams = { } ,
9501012) : Promise < { success : boolean ; sessionId : string } > {
9511013 const { sessionId } = params
9521014
@@ -966,8 +1028,8 @@ async function redoMessage(
9661028/**
9671029 * Compact a session
9681030 */
969- async function compactSession (
970- params : acpt . SessionCompactParams ,
1031+ export async function compactSession (
1032+ params : SessionCompactParams = { } ,
9711033) : Promise < { success : boolean ; sessionId : string } > {
9721034 const { sessionId } = params
9731035
@@ -1004,9 +1066,9 @@ async function compactSession(
10041066/**
10051067 * Export a session
10061068 */
1007- async function exportSession (
1008- params : acpt . SessionExportParams ,
1009- ) : Promise < acpt . SessionExportResult > {
1069+ export async function exportSession (
1070+ params : SessionExportParams = { } ,
1071+ ) : Promise < SessionExportResult > {
10101072 const { sessionId, format = "markdown" } = params
10111073
10121074 const currentSession = getCurrentSession ( )
@@ -1073,8 +1135,8 @@ async function exportSession(
10731135/**
10741136 * Jump to a specific message in a session
10751137 */
1076- async function jumpToMessage (
1077- params : acpt . SessionJumpParams ,
1138+ export async function jumpToMessage (
1139+ params : SessionJumpParams ,
10781140) : Promise < { success : boolean ; sessionId : string } > {
10791141 const { sessionId, messageId } = params
10801142
@@ -1119,9 +1181,9 @@ async function jumpToMessage(
11191181/**
11201182 * Duplicate a session
11211183 */
1122- async function duplicateSession (
1123- params : acpt . SessionDuplicateParams ,
1124- ) : Promise < acpt . SessionInfo > {
1184+ export async function duplicateSession (
1185+ params : SessionDuplicateParams ,
1186+ ) : Promise < SessionInfo > {
11251187 const { sessionId, title } = params
11261188
11271189 if ( ! sessionId ) {
@@ -1150,35 +1212,10 @@ async function duplicateSession(
11501212 title ??
11511213 `${ sessionData . title ?? "Copy" } (${ new Date ( ) . toLocaleDateString ( ) } )` ,
11521214 id : id . generate ( ) ,
1153- agent : await agent . clone ( ) ,
1215+ agent : undefined ,
11541216 workingDirectory : sessionData . projectPath ?? process . cwd ( ) ,
11551217 messages : sessionData . messages ?? [ ] ,
11561218 } )
11571219
1158- return {
1159- id : newSession . id ,
1160- title : newSession . title ?? `Session ${ newSession . id . slice ( 0 , 8 ) } ` ,
1161- createdAt : newSession . createdAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
1162- updatedAt : newSession . updatedAt ?. toISOString ( ) ?? new Date ( ) . toISOString ( ) ,
1163- projectPath : newSession . projectPath ,
1164- messageCount : newSession . messages ?. length ?? 0 ,
1165- }
1166- }
1167-
1168- export default {
1169- createAcpServer,
1170- createRepl,
1171- listSessions,
1172- switchSession,
1173- createSession,
1174- forkSession,
1175- renameSession,
1176- deleteSession,
1177- getSessionInfo,
1178- undoMessage,
1179- redoMessage,
1180- compactSession,
1181- exportSession,
1182- jumpToMessage,
1183- duplicateSession,
1220+ return formatSessionInfo ( newSession )
11841221}
0 commit comments