@@ -38,7 +38,7 @@ import DashboardView from "./pages/dashboard";
3838import SessionView from "./pages/session" ;
3939import ProtoWorkspacesView from "./pages/proto-workspaces" ;
4040import ProtoV1UxView from "./pages/proto-v1-ux" ;
41- import { createClient , unwrap , waitForHealthy } from "./lib/opencode" ;
41+ import { createClient , unwrap , waitForHealthy , type OpencodeAuth } from "./lib/opencode" ;
4242import {
4343 DEFAULT_MODEL ,
4444 HIDE_TITLEBAR_PREF_KEY ,
@@ -230,7 +230,7 @@ export default function App() {
230230 isTauriRuntime ( ) ? "sidecar" : "path"
231231 ) ;
232232
233- const [ engineRuntime , setEngineRuntime ] = createSignal < EngineRuntime > ( "direct " ) ;
233+ const [ engineRuntime , setEngineRuntime ] = createSignal < EngineRuntime > ( "openwrk " ) ;
234234
235235 const [ baseUrl , setBaseUrl ] = createSignal ( "http://127.0.0.1:4096" ) ;
236236 const [ clientDirectory , setClientDirectory ] = createSignal ( "" ) ;
@@ -670,30 +670,10 @@ export default function App() {
670670 return a . id . localeCompare ( b . id ) ;
671671 } ) ;
672672
673- const [ sidebarSessions , setSidebarSessions ] = createSignal < Session [ ] > ( [ ] ) ;
674- const refreshSidebarSessions = async ( ) => {
675- const c = client ( ) ;
676- if ( ! c ) {
677- setSidebarSessions ( [ ] ) ;
678- return ;
679- }
680- const list = unwrap ( await c . session . list ( ) ) ;
681- setSidebarSessions ( sortSessionsByActivity ( list ) ) ;
682- } ;
683-
684- createEffect ( ( ) => {
685- if ( ! client ( ) ) {
686- setSidebarSessions ( [ ] ) ;
687- return ;
688- }
689- refreshSidebarSessions ( ) . catch ( ( ) => undefined ) ;
690- } ) ;
691-
692673 const [ sessionsLoaded , setSessionsLoaded ] = createSignal ( false ) ;
693674 const loadSessionsWithReady = async ( scopeRoot ?: string ) => {
694675 await loadSessions ( scopeRoot ) ;
695676 setSessionsLoaded ( true ) ;
696- await refreshSidebarSessions ( ) . catch ( ( ) => undefined ) ;
697677 } ;
698678
699679 createEffect ( ( ) => {
@@ -927,9 +907,9 @@ export default function App() {
927907 if ( ! trimmed ) {
928908 throw new Error ( "Session name is required" ) ;
929909 }
930-
910+
931911 await renameSession ( sessionID , trimmed ) ;
932- await refreshSidebarSessions ( ) . catch ( ( ) => undefined ) ;
912+ await refreshSidebarWorkspaceSessions ( workspaceStore . activeWorkspaceId ( ) ) . catch ( ( ) => undefined ) ;
933913 }
934914
935915 async function deleteSessionById ( sessionID : string ) {
@@ -944,7 +924,7 @@ export default function App() {
944924 const params = root ? { sessionID : trimmed , directory : root } : { sessionID : trimmed } ;
945925 unwrap ( await c . session . delete ( params ) ) ;
946926 await loadSessions ( root || undefined ) . catch ( ( ) => undefined ) ;
947- await refreshSidebarSessions ( ) . catch ( ( ) => undefined ) ;
927+ await refreshSidebarWorkspaceSessions ( workspaceStore . activeWorkspaceId ( ) ) . catch ( ( ) => undefined ) ;
948928
949929 const nextStatus = { ...sessionStatusById ( ) } ;
950930 if ( nextStatus [ trimmed ] ) {
@@ -1444,33 +1424,195 @@ export default function App() {
14441424 engineRuntime,
14451425 } ) ;
14461426
1447- const sidebarWorkspaceGroups = createMemo < WorkspaceSessionGroup [ ] > ( ( ) => {
1448- const workspaces = workspaceStore . workspaces ( ) ;
1449- const sessionList = sidebarSessions ( ) ;
1450- const workspaceByRoot = new Map < string , string > ( ) ;
1451- const sessionsByWorkspaceId = new Map < string , Session [ ] > ( ) ;
1427+ type SidebarWorkspaceSessionsStatus = WorkspaceSessionGroup [ "status" ] ;
1428+ const [ sidebarSessionsByWorkspaceId , setSidebarSessionsByWorkspaceId ] = createSignal <
1429+ Record < string , Session [ ] >
1430+ > ( { } ) ;
1431+ const [ sidebarSessionStatusByWorkspaceId , setSidebarSessionStatusByWorkspaceId ] = createSignal <
1432+ Record < string , SidebarWorkspaceSessionsStatus >
1433+ > ( { } ) ;
1434+ const [ sidebarSessionErrorByWorkspaceId , setSidebarSessionErrorByWorkspaceId ] = createSignal <
1435+ Record < string , string | null >
1436+ > ( { } ) ;
14521437
1453- for ( const workspace of workspaces ) {
1454- const root = normalizeDirectoryPath (
1455- workspace . workspaceType === "remote" ? workspace . directory ?? "" : workspace . path
1456- ) ;
1457- if ( root ) {
1458- workspaceByRoot . set ( root , workspace . id ) ;
1438+ const pruneSidebarSessionState = ( workspaceIds : Set < string > ) => {
1439+ setSidebarSessionsByWorkspaceId ( ( prev ) => {
1440+ let changed = false ;
1441+ const next : Record < string , Session [ ] > = { } ;
1442+ for ( const [ id , list ] of Object . entries ( prev ) ) {
1443+ if ( ! workspaceIds . has ( id ) ) {
1444+ changed = true ;
1445+ continue ;
1446+ }
1447+ next [ id ] = list ;
1448+ }
1449+ return changed ? next : prev ;
1450+ } ) ;
1451+ setSidebarSessionStatusByWorkspaceId ( ( prev ) => {
1452+ let changed = false ;
1453+ const next : Record < string , SidebarWorkspaceSessionsStatus > = { } ;
1454+ for ( const [ id , status ] of Object . entries ( prev ) ) {
1455+ if ( ! workspaceIds . has ( id ) ) {
1456+ changed = true ;
1457+ continue ;
1458+ }
1459+ next [ id ] = status ;
1460+ }
1461+ return changed ? next : prev ;
1462+ } ) ;
1463+ setSidebarSessionErrorByWorkspaceId ( ( prev ) => {
1464+ let changed = false ;
1465+ const next : Record < string , string | null > = { } ;
1466+ for ( const [ id , error ] of Object . entries ( prev ) ) {
1467+ if ( ! workspaceIds . has ( id ) ) {
1468+ changed = true ;
1469+ continue ;
1470+ }
1471+ next [ id ] = error ;
1472+ }
1473+ return changed ? next : prev ;
1474+ } ) ;
1475+ } ;
1476+
1477+ const resolveSidebarClientConfig = ( workspaceId : string ) => {
1478+ const workspace = workspaceStore . workspaces ( ) . find ( ( entry ) => entry . id === workspaceId ) ?? null ;
1479+ if ( ! workspace ) return null ;
1480+
1481+ if ( workspace . workspaceType === "local" ) {
1482+ const info = workspaceStore . engine ( ) ;
1483+ const baseUrl = info ?. baseUrl ?. trim ( ) ?? "" ;
1484+ const directory = workspace . path ?. trim ( ) ?? "" ;
1485+ const username = info ?. opencodeUsername ?. trim ( ) ?? "" ;
1486+ const password = info ?. opencodePassword ?. trim ( ) ?? "" ;
1487+ const auth : OpencodeAuth | undefined = username && password ? { username, password } : undefined ;
1488+ return {
1489+ baseUrl,
1490+ directory,
1491+ auth,
1492+ } ;
1493+ }
1494+
1495+ const baseUrl = workspace . baseUrl ?. trim ( ) ?? "" ;
1496+ const directory = workspace . directory ?. trim ( ) ?? "" ;
1497+ if ( workspace . remoteType === "openwork" ) {
1498+ const token = workspace . openworkToken ?. trim ( ) || openworkServerSettings ( ) . token ?. trim ( ) || "" ;
1499+ const auth : OpencodeAuth | undefined = token ? { token, mode : "openwork" } : undefined ;
1500+ return {
1501+ baseUrl,
1502+ directory,
1503+ auth,
1504+ } ;
1505+ }
1506+ return {
1507+ baseUrl,
1508+ directory,
1509+ auth : undefined as OpencodeAuth | undefined ,
1510+ } ;
1511+ } ;
1512+
1513+ const sidebarRefreshSeqByWorkspaceId : Record < string , number > = { } ;
1514+ const refreshSidebarWorkspaceSessions = async ( workspaceId : string ) => {
1515+ const id = workspaceId . trim ( ) ;
1516+ if ( ! id ) return ;
1517+
1518+ const config = resolveSidebarClientConfig ( id ) ;
1519+ if ( ! config ) return ;
1520+
1521+ // For local workspaces, avoid thrashing UI with errors if the engine is offline.
1522+ if ( ! config . baseUrl ) {
1523+ setSidebarSessionStatusByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : "idle" } ) ) ;
1524+ setSidebarSessionErrorByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : null } ) ) ;
1525+ return ;
1526+ }
1527+
1528+ sidebarRefreshSeqByWorkspaceId [ id ] = ( sidebarRefreshSeqByWorkspaceId [ id ] ?? 0 ) + 1 ;
1529+ const seq = sidebarRefreshSeqByWorkspaceId [ id ] ;
1530+
1531+ setSidebarSessionStatusByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : "loading" } ) ) ;
1532+ setSidebarSessionErrorByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : null } ) ) ;
1533+
1534+ try {
1535+ let directory = config . directory ;
1536+ let c = createClient ( config . baseUrl , directory || undefined , config . auth ) ;
1537+
1538+ if ( ! directory ) {
1539+ try {
1540+ const pathInfo = unwrap ( await c . path . get ( ) ) ;
1541+ const discovered = normalizeDirectoryPath ( pathInfo . directory ?? "" ) ;
1542+ if ( discovered ) {
1543+ directory = discovered ;
1544+ c = createClient ( config . baseUrl , directory , config . auth ) ;
1545+ }
1546+ } catch {
1547+ // ignore
1548+ }
14591549 }
1460- sessionsByWorkspaceId . set ( workspace . id , [ ] ) ;
1550+
1551+ const list = unwrap ( await c . session . list ( ) ) ;
1552+ if ( sidebarRefreshSeqByWorkspaceId [ id ] !== seq ) return ;
1553+ setSidebarSessionsByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : sortSessionsByActivity ( list ) } ) ) ;
1554+ setSidebarSessionStatusByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : "ready" } ) ) ;
1555+ } catch ( error ) {
1556+ if ( sidebarRefreshSeqByWorkspaceId [ id ] !== seq ) return ;
1557+ const message = error instanceof Error ? error . message : safeStringify ( error ) ;
1558+ setSidebarSessionStatusByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : "error" } ) ) ;
1559+ setSidebarSessionErrorByWorkspaceId ( ( prev ) => ( { ...prev , [ id ] : message } ) ) ;
14611560 }
1561+ } ;
14621562
1463- for ( const session of sessionList ) {
1464- const root = normalizeDirectoryPath ( session . directory ?? "" ) ;
1465- if ( ! root ) continue ;
1466- const workspaceId = workspaceByRoot . get ( root ) ;
1467- if ( ! workspaceId ) continue ;
1468- const bucket = sessionsByWorkspaceId . get ( workspaceId ) ;
1469- if ( bucket ) bucket . push ( session ) ;
1563+ const refreshAllSidebarWorkspaceSessions = async ( prioritizeWorkspaceId ?: string | null ) => {
1564+ const list = workspaceStore . workspaces ( ) ;
1565+ if ( ! list . length ) return ;
1566+ const prioritize = ( prioritizeWorkspaceId ?? "" ) . trim ( ) ;
1567+ const ordered = prioritize
1568+ ? [ ...list . filter ( ( ws ) => ws . id === prioritize ) , ...list . filter ( ( ws ) => ws . id !== prioritize ) ]
1569+ : list ;
1570+ for ( const ws of ordered ) {
1571+ await refreshSidebarWorkspaceSessions ( ws . id ) ;
14701572 }
1573+ } ;
1574+
1575+ let lastSidebarRefreshKey = "" ;
1576+ createEffect ( ( ) => {
1577+ const engineInfo = workspaceStore . engine ( ) ;
1578+ const engineBaseUrl = engineInfo ?. baseUrl ?. trim ( ) ?? "" ;
1579+ const engineUser = engineInfo ?. opencodeUsername ?. trim ( ) ?? "" ;
1580+ const enginePass = engineInfo ?. opencodePassword ?. trim ( ) ?? "" ;
1581+ const tokenFallback = openworkServerSettings ( ) . token ?. trim ( ) ?? "" ;
1582+ const workspaceKey = workspaceStore
1583+ . workspaces ( )
1584+ . map ( ( ws ) => {
1585+ const root = ws . workspaceType === "local" ? ws . path ?. trim ( ) ?? "" : ws . directory ?. trim ( ) ?? "" ;
1586+ const base = ws . workspaceType === "local" ? "" : ws . baseUrl ?. trim ( ) ?? "" ;
1587+ const remoteType = ws . workspaceType === "remote" ? ( ws . remoteType ?? "" ) : "" ;
1588+ const token = ws . remoteType === "openwork" ? ( ws . openworkToken ?. trim ( ) ?? "" ) : "" ;
1589+ return [ ws . id , ws . workspaceType , remoteType , root , base , token ] . join ( "|" ) ;
1590+ } )
1591+ . join ( ";" ) ;
1592+
1593+ const key = [ engineBaseUrl , engineUser , enginePass , tokenFallback , workspaceKey ] . join ( "::" ) ;
1594+ if ( key === lastSidebarRefreshKey ) return ;
1595+ lastSidebarRefreshKey = key ;
14711596
1597+ pruneSidebarSessionState ( new Set ( workspaceStore . workspaces ( ) . map ( ( ws ) => ws . id ) ) ) ;
1598+ void refreshAllSidebarWorkspaceSessions ( workspaceStore . activeWorkspaceId ( ) ) . catch ( ( ) => undefined ) ;
1599+ } ) ;
1600+
1601+ createEffect ( ( ) => {
1602+ const id = workspaceStore . activeWorkspaceId ( ) . trim ( ) ;
1603+ if ( ! id ) return ;
1604+ const status = sidebarSessionStatusByWorkspaceId ( ) [ id ] ;
1605+ if ( status === "ready" || status === "loading" ) return ;
1606+ refreshSidebarWorkspaceSessions ( id ) . catch ( ( ) => undefined ) ;
1607+ } ) ;
1608+
1609+ const sidebarWorkspaceGroups = createMemo < WorkspaceSessionGroup [ ] > ( ( ) => {
1610+ const workspaces = workspaceStore . workspaces ( ) ;
1611+ const sessionsById = sidebarSessionsByWorkspaceId ( ) ;
1612+ const statusById = sidebarSessionStatusByWorkspaceId ( ) ;
1613+ const errorById = sidebarSessionErrorByWorkspaceId ( ) ;
14721614 return workspaces . map ( ( workspace ) => {
1473- const groupSessions = sortSessionsByActivity ( sessionsByWorkspaceId . get ( workspace . id ) ?? [ ] ) ;
1615+ const groupSessions = sessionsById [ workspace . id ] ?? [ ] ;
14741616 return {
14751617 workspace,
14761618 sessions : groupSessions . map ( ( session ) => ( {
@@ -1480,6 +1622,8 @@ export default function App() {
14801622 time : session . time ,
14811623 directory : session . directory ,
14821624 } ) ) ,
1625+ status : statusById [ workspace . id ] ?? "idle" ,
1626+ error : errorById [ workspace . id ] ?? null ,
14831627 } ;
14841628 } ) ;
14851629 } ) ;
@@ -1562,7 +1706,7 @@ export default function App() {
15621706 if ( cancelled ) return ;
15631707 const items = Array . isArray ( response . items ) ? response . items : [ ] ;
15641708 const match = items . find ( ( entry ) => normalizeDirectoryPath ( entry . path ) === root ) ;
1565- setOpenworkServerWorkspaceId ( response . activeId ?? match ?. id ?? null ) ;
1709+ setOpenworkServerWorkspaceId ( match ?. id ?? response . activeId ?? null ) ;
15661710 } catch {
15671711 if ( ! cancelled ) setOpenworkServerWorkspaceId ( null ) ;
15681712 }
0 commit comments