@@ -52,25 +52,44 @@ function Wezterm.health()
5252return true
5353end
5454
55- --- Get the `wezterm` pane ID where `opencode` is running.
55+ --- Retrieve the `wezterm` pane ID associated with the running `opencode` instance.
56+ --- This establishes a direct link between the spawned `opencode` pane and its ID.
57+ --- If the `opencode` pane is closed and a new one is created manually, it cannot
58+ --- still be tracked by this ID.
5659--- @return string | nil pane_id
5760function Wezterm :get_pane_id ()
5861local ok = self .health ()
5962if ok ~= true then
6063error (ok )
6164end
6265
63- local base_cmd = self .cmd :match (" ^%S+" )or self .cmd
64- local result = vim .fn .system (
65- string.format (" wezterm cli list --format json 2>&1 | jq -r '.[] | select(.title ==\" %s\" ) | .pane_id'" ,base_cmd )
66- )
67- if result and result ~= " " and not result :match (" error" )then
68- self .pane_id = result :match (" ^%d+" )
69- else
66+ if self .pane_id == nil then
67+ return nil
68+ end
69+
70+ local result = vim .fn .system (" wezterm cli list --format json 2>&1" )
71+
72+ if result == nil or result == " " or result :match (" error" )then
73+ self .pane_id = nil
74+ return nil
75+ end
76+
77+ local success ,panes = pcall (vim .json .decode ,result )
78+ if not success or type (panes )~= " table" then
7079self .pane_id = nil
80+ return nil
81+ end
82+
83+ -- Search for the pane in the list
84+ for _ ,pane in ipairs (panes )do
85+ if tostring (pane .pane_id )== tostring (self .pane_id )then
86+ return self .pane_id
87+ end
7188end
7289
73- return self .pane_id
90+ -- Pane was not found in the list
91+ self .pane_id = nil
92+ return nil
7493end
7594
7695--- Create or kill the `opencode` pane.