@@ -741,6 +741,54 @@ function M.fork_session(message_id)
741741 end )
742742end
743743
744+ --- @param current_session ? Session
745+ --- @param new_title ? string
746+ function M .rename_session (current_session , new_title )
747+ local promise = require (' opencode.promise' ).new ()
748+ current_session = current_session or vim .deepcopy (state .active_session ) --[[ @as Session]]
749+ if not current_session then
750+ vim .notify (' No active session to rename' , vim .log .levels .WARN )
751+ promise :resolve (nil )
752+ return promise
753+ end
754+ local function rename_session_with_title (title )
755+ state .api_client
756+ :update_session (current_session .id , { title = title })
757+ :catch (function (err )
758+ vim .schedule (function ()
759+ vim .notify (' Failed to rename session: ' .. vim .inspect (err ), vim .log .levels .ERROR )
760+ end )
761+ end )
762+ :and_then (function ()
763+ current_session .title = title
764+ if state .active_session and state .active_session .id == current_session .id then
765+ local session_obj = session .get_by_id (current_session .id )
766+ if session_obj then
767+ session_obj .title = title
768+ state .active_session = vim .deepcopy (session_obj )
769+ end
770+ end
771+ promise :resolve (current_session )
772+ end )
773+ end
774+
775+ if new_title and new_title ~= ' ' then
776+ rename_session_with_title (new_title )
777+ return promise
778+ end
779+
780+ vim .schedule (function ()
781+ vim .ui .input ({ prompt = ' New session name: ' , default = current_session .title or ' ' }, function (input )
782+ if input and input ~= ' ' then
783+ rename_session_with_title (input )
784+ else
785+ promise :resolve (nil )
786+ end
787+ end )
788+ end )
789+ return promise
790+ end
791+
744792-- Returns the ID of the next user message after the current undo point
745793-- This is a port of the opencode tui logic
746794-- https://github.com/sst/opencode/blob/dev/packages/tui/internal/components/chat/messages.go#L1199
@@ -913,8 +961,8 @@ M.commands = {
913961 },
914962
915963 session = {
916- desc = ' Manage sessions (new/select/child/compact/share/unshare)' ,
917- completions = { ' new' , ' select' , ' child' , ' compact' , ' share' , ' unshare' , ' agents_init' },
964+ desc = ' Manage sessions (new/select/child/compact/share/unshare/rename )' ,
965+ completions = { ' new' , ' select' , ' child' , ' compact' , ' share' , ' unshare' , ' agents_init' , ' rename ' },
918966 fn = function (args )
919967 local subcmd = args [1 ]
920968 if subcmd == ' new' then
@@ -942,6 +990,9 @@ M.commands = {
942990 M .unshare ()
943991 elseif subcmd == ' agents_init' then
944992 M .initialize ()
993+ elseif subcmd == ' rename' then
994+ local title = table.concat (vim .list_slice (args , 2 ), ' ' )
995+ M .rename_session (state .active_session , title )
945996 else
946997 local valid_subcmds = table.concat (M .commands .session .completions , ' , ' )
947998 vim .notify (' Invalid session subcommand. Use: ' .. valid_subcmds , vim .log .levels .ERROR )
0 commit comments