oxios 1.10.1

Oxios Agent OS — Agent Operating System powered by oxi-sdk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
import { useQuery } from '@tanstack/react-query'
import { Link, useRouterState } from '@tanstack/react-router'
import {
  Activity,
  Bell,
  BookOpen,
  Bot,
  Brain,
  Calendar,
  CalendarDays,
  CheckSquare,
  FilePlus,
  FolderKanban,
  FolderOpen,
  FolderPlus,
  GitBranch,
  LayoutDashboard,
  Mail,
  MessageSquare,
  Network,
  PanelLeft,
  PanelLeftClose,
  Settings,
  Theater,
  Timer,
  Trash2,
  Users,
  Wallet,
  Zap,
} from 'lucide-react'
import React, { useCallback, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { FileTree } from '@/components/knowledge/file-tree'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import {
  useDeleteFile,
  useJournalToday,
  useKnowledgeTree,
  useWriteFile,
} from '@/hooks/use-knowledge'
import { api } from '@/lib/api-client'
import { cn } from '@/lib/utils'
import { useKnowledgeStore } from '@/stores/knowledge'
import { deriveSidebarMode, useSidebarStore } from '@/stores/sidebar'
import { ChatSessionNav } from './chat-session-nav'
import { ModeTabs } from './mode-tabs'

// ── Types ──────────────────────────────────────────────────────

interface NavItem {
  labelKey: string
  href: string
  icon: React.ReactNode
  show?: boolean
  badge?: number
}

// ── Sidebar design primitives ─────────────────────────────────
//
// Shared tokens for all three sidebar modes (Console, Knowledge, Chat).
// Every item, section header, and separator must use these constants
// so the three modes feel visually identical.
//

/** Primary navigation item (icon + label). */
export const itemBase =
  'flex items-center gap-3 rounded-lg px-2.5 py-2 text-sm w-full text-left select-none transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-sidebar'

/** Dense list item (session rows, file rows). */
export const itemDense =
  'flex items-center gap-2 rounded-lg px-2.5 py-1.5 text-xs w-full text-left select-none transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring'

export const itemActive = 'bg-sidebar-accent text-sidebar-accent-foreground font-medium'
export const itemInactive =
  'text-sidebar-foreground/70 hover:bg-sidebar-accent/50 hover:text-sidebar-foreground'
export const itemCollapsedBase =
  'flex items-center justify-center rounded-lg p-2 select-none transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring'

/** Section header label. */
export const sectionHeader =
  'px-2 mb-1 text-xs font-medium text-muted-foreground uppercase tracking-wider select-none'

/** Vertical spacing between sections. */
export const sectionGap = 'mb-3'

/** Horizontal separator between sections. */
export const sectionSeparator = 'border-t border-sidebar-border my-2'

// ── Console mode nav groups ────────────────────────────────────

const consoleNavGroups: { labelKey: string; items: NavItem[] }[] = [
  {
    labelKey: 'common.main',
    items: [
      { labelKey: 'common.dashboard', href: '/', icon: <LayoutDashboard className="h-4 w-4" /> },
    ],
  },
  {
    labelKey: 'common.agents',
    items: [
      { labelKey: 'common.agents', href: '/agents', icon: <Bot className="h-4 w-4" /> },
      {
        labelKey: 'common.agentGroups',
        href: '/agent-groups',
        icon: <Users className="h-4 w-4" />,
      },
      { labelKey: 'common.personas', href: '/personas', icon: <Theater className="h-4 w-4" /> },
      { labelKey: 'common.skills', href: '/skills', icon: <Zap className="h-4 w-4" /> },
    ],
  },
  {
    labelKey: 'common.projects',
    items: [
      {
        labelKey: 'common.projects',
        href: '/projects',
        icon: <FolderKanban className="h-4 w-4" />,
      },
      {
        labelKey: 'common.mounts',
        href: '/mounts',
        icon: <FolderPlus className="h-4 w-4" />,
      },
    ],
  },
  {
    labelKey: 'common.storage',
    items: [
      { labelKey: 'common.memory', href: '/memory', icon: <Brain className="h-4 w-4" /> },
      {
        labelKey: 'common.workspace',
        href: '/workspace',
        icon: <FolderOpen className="h-4 w-4" />,
      },
    ],
  },
  {
    labelKey: 'common.operations',
    items: [
      { labelKey: 'common.scheduler', href: '/scheduler', icon: <Calendar className="h-4 w-4" /> },
      {
        labelKey: 'common.calendar',
        href: '/calendar',
        icon: <CalendarDays className="h-4 w-4" />,
      },
      { labelKey: 'common.cronJobs', href: '/cron-jobs', icon: <Timer className="h-4 w-4" /> },
      { labelKey: 'common.budget', href: '/budget', icon: <Wallet className="h-4 w-4" /> },
    ],
  },
  {
    labelKey: 'common.infrastructure',
    items: [
      { labelKey: 'common.mcpServers', href: '/mcp', icon: <Zap className="h-4 w-4" /> },
      { labelKey: 'common.email', href: '/email', icon: <Mail className="h-4 w-4" /> },
      { labelKey: 'common.git', href: '/git', icon: <GitBranch className="h-4 w-4" /> },
      { labelKey: 'common.a2aMonitor', href: '/a2a', icon: <Network className="h-4 w-4" /> },
    ],
  },
  {
    labelKey: 'common.system',
    items: [
      { labelKey: 'common.resources', href: '/resources', icon: <Activity className="h-4 w-4" /> },
      { labelKey: 'common.security', href: '/security', icon: <Bell className="h-4 w-4" /> },
      { labelKey: 'common.events', href: '/events', icon: <Bell className="h-4 w-4" /> },
    ],
  },
]

// ── Sidebar component ──────────────────────────────────────────

export function Sidebar() {
  const { collapsed, toggle, mode, setMode, mobileOpen } = useSidebarStore()
  const router = useRouterState()
  const currentPath = router.location.pathname

  // Sync mode from route
  useEffect(() => {
    const derivedMode = deriveSidebarMode(currentPath)
    setMode(derivedMode)
  }, [currentPath, setMode])

  return (
    <aside
      className={cn(
        'flex h-full w-72 max-w-[85vw] flex-col overflow-hidden border-r bg-sidebar text-sidebar-foreground transition-[width] duration-300 ease-[var(--animate-in-easing)]',
        // Desktop collapses to icon rail; mobile drawer stays full width
        collapsed ? 'lg:w-16 lg:max-w-none' : 'lg:w-60 lg:max-w-none',
      )}
    >
      {/* Header — brand + collapse toggle */}
      <div
        className={cn(
          'flex h-14 items-center px-3',
          collapsed && !mobileOpen ? 'justify-center' : 'justify-between',
        )}
      >
        {!(collapsed && !mobileOpen) && (
          <div className="flex items-center gap-2">
            <Zap className="h-5 w-5 text-primary" />
            <span className="font-bold text-lg">Oxios</span>
          </div>
        )}
        {/* Desktop collapse toggle */}
        <button
          type="button"
          onClick={toggle}
          className="hidden lg:block rounded-md p-1.5 hover:bg-sidebar-accent focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring"
          aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
        >
          {collapsed ? <PanelLeft className="h-4 w-4" /> : <PanelLeftClose className="h-4 w-4" />}
        </button>
      </div>

      {/* Mobile only: mode tabs inside sidebar overlay */}
      {mobileOpen && (
        <div className="lg:hidden px-2 py-1.5">
          <ModeTabs variant="sidebar" />
        </div>
      )}

      <Separator />

      {/* Nav content — mode-specific */}
      <nav className="flex-1 overflow-y-auto p-2">
        {mode === 'console' && <ConsoleNav />}
        {mode === 'knowledge' && <KnowledgeNav />}
        {mode === 'chat' && <ChatSessionNav />}
      </nav>
    </aside>
  )
}

// ── Console Nav ────────────────────────────────────────────────

function useApprovalsCount() {
  const { data } = useQuery({
    queryKey: ['approvals-pending-count'],
    queryFn: async () => {
      const res = await api.get<{ id: string; status: string }[]>('/api/approvals')
      const items = Array.isArray(res) ? res : []
      return items.filter((a) => a.status === 'pending').length
    },
    refetchInterval: 10_000,
  })
  return data ?? 0
}

function ConsoleNav() {
  const { t } = useTranslation()
  const router = useRouterState()
  const currentPath = router.location.pathname
  const { collapsed } = useSidebarStore()
  const pendingCount = useApprovalsCount()

  const mainItems: NavItem[] = [
    consoleNavGroups[0]!.items[0]!, // Dashboard
    {
      labelKey: 'common.approvals',
      href: '/approvals',
      icon: <CheckSquare className="h-4 w-4" />,
      badge: pendingCount,
    },
  ]

  return (
    <>
      <div className={sectionGap}>
        {!collapsed && <p className={sectionHeader}>{t('common.main')}</p>}
        {mainItems.map((item) => (
          <NavItemLink
            key={item.href}
            item={item}
            currentPath={currentPath}
            collapsed={collapsed}
          />
        ))}
      </div>
      {consoleNavGroups.slice(1).map((group) => (
        <div key={group.labelKey} className={sectionGap}>
          {!collapsed && <p className={sectionHeader}>{t(group.labelKey)}</p>}
          {group.items.map((item) => (
            <NavItemLink
              key={item.href}
              item={item}
              currentPath={currentPath}
              collapsed={collapsed}
            />
          ))}
        </div>
      ))}
    </>
  )
}

// ── Knowledge Nav ──────────────────────────────────────────────

function KnowledgeNav() {
  const { t } = useTranslation()
  const { collapsed } = useSidebarStore()
  const router = useRouterState()
  const currentPath = router.location.pathname
  const { mode, currentFilePath, openFile, openChat } = useKnowledgeStore()
  const { data: entries, isLoading, refetch } = useKnowledgeTree()
  const writeFile = useWriteFile()
  const deleteFile = useDeleteFile()
  const journalToday = useJournalToday()

  const handleNewFile = useCallback(async () => {
    const name = 'New file.md'
    await writeFile.mutateAsync({ path: name, content: `# New file\n\n` })
    openFile(name)
    refetch()
  }, [writeFile, openFile, refetch])

  const handleNewFolder = useCallback(async () => {
    const name = prompt('Enter folder name:', 'New Folder')
    if (!name?.trim()) return
    await writeFile.mutateAsync({ path: `${name.trim()}/.keep`, content: '' })
    refetch()
  }, [writeFile, refetch])

  const handleDelete = useCallback(async () => {
    if (!currentFilePath) return
    if (confirm(`Delete ${currentFilePath}?`)) {
      await deleteFile.mutateAsync(currentFilePath)
    }
  }, [deleteFile, currentFilePath])

  const handleOpenJournal = useCallback(() => {
    if (journalToday.data?.path) {
      openFile(journalToday.data.path)
    }
  }, [journalToday.data, openFile])

  // Collapsed: show minimal icons
  if (collapsed) {
    return (
      <div className="flex flex-col items-center gap-1 py-1">
        <Tooltip>
          <TooltipTrigger asChild>
            <button
              type="button"
              onClick={() => openChat()}
              className={cn(itemCollapsedBase, mode === 'chat' && itemActive)}
            >
              <MessageSquare className="h-4 w-4" />
            </button>
          </TooltipTrigger>
          <TooltipContent side="right">{t('knowledge.chatTitle', 'Quick Notes')}</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger asChild>
            <button
              type="button"
              onClick={handleOpenJournal}
              className={cn(itemCollapsedBase, itemInactive)}
            >
              <BookOpen className="h-4 w-4" />
            </button>
          </TooltipTrigger>
          <TooltipContent side="right">{t('knowledge.toJournal')}</TooltipContent>
        </Tooltip>
        <Tooltip>
          <TooltipTrigger asChild>
            <button
              type="button"
              onClick={handleNewFile}
              className={cn(itemCollapsedBase, itemInactive)}
            >
              <FilePlus className="h-4 w-4" />
            </button>
          </TooltipTrigger>
          <TooltipContent side="right">{t('knowledge.newFile')}</TooltipContent>
        </Tooltip>
      </div>
    )
  }

  return (
    <>
      {/* Quick Notes */}
      <div className={sectionGap}>
        <button
          type="button"
          onClick={() => openChat()}
          className={cn(itemBase, mode === 'chat' ? itemActive : itemInactive)}
        >
          <MessageSquare className="h-4 w-4" />
          <span>{t('knowledge.chatTitle', 'Quick Notes')}</span>
        </button>
      </div>

      {/* Journal */}
      <div className={sectionGap}>
        <button
          type="button"
          onClick={handleOpenJournal}
          disabled={journalToday.isLoading}
          className={cn(itemBase, itemInactive, 'disabled:opacity-50')}
        >
          <BookOpen className="h-4 w-4" />
          <span>{t('knowledge.toJournal')}</span>
        </button>
      </div>

      {/* Sub-routes */}
      <div className={sectionGap}>
        <NavItemLink
          item={{
            href: '/knowledge/graph',
            icon: <Network className="h-4 w-4" />,
            labelKey: 'knowledge.linkGraphTitle',
          }}
          currentPath={currentPath}
          collapsed={collapsed}
        />
        <NavItemLink
          item={{
            href: '/knowledge/habits',
            icon: <Activity className="h-4 w-4" />,
            labelKey: 'knowledge.habitsTitle',
          }}
          currentPath={currentPath}
          collapsed={collapsed}
        />
        <NavItemLink
          item={{
            href: '/knowledge/settings',
            icon: <Settings className="h-4 w-4" />,
            labelKey: 'knowledge.knowledgeSettings',
          }}
          currentPath={currentPath}
          collapsed={collapsed}
        />
      </div>

      <div className={sectionSeparator} />

      {/* File tree */}
      <div className={sectionGap}>
        <p className={sectionHeader}>{t('knowledge.files', 'Files')}</p>
      </div>
      <div className="flex-1 overflow-y-auto">
        {isLoading ? (
          <div className="px-4 py-2 text-xs text-sidebar-foreground/50">
            {t('knowledge.loading')}
          </div>
        ) : entries ? (
          <FileTree entries={entries} onFileSelect={openFile} currentPath={currentFilePath} />
        ) : null}
      </div>

      {/* Action bar */}
      <div className="flex items-center gap-0.5 pt-2 border-t border-sidebar-border mt-2 px-1">
        <Button
          variant="ghost"
          size="icon"
          className="h-7 w-7 hover:bg-sidebar-accent/50"
          onClick={handleNewFile}
          title={t('knowledge.newFileShortcut')}
        >
          <FilePlus className="h-4 w-4" />
        </Button>
        <Button
          variant="ghost"
          size="icon"
          className="h-7 w-7 hover:bg-sidebar-accent/50"
          onClick={handleNewFolder}
          title={t('knowledge.newFolderShortcut')}
        >
          <FolderPlus className="h-4 w-4" />
        </Button>
        {currentFilePath && (
          <Button
            variant="ghost"
            size="icon"
            className="h-7 w-7 text-destructive hover:bg-sidebar-accent/50"
            onClick={handleDelete}
            title={t('knowledge.deleteCurrentFile')}
          >
            <Trash2 className="h-4 w-4" />
          </Button>
        )}
        <div className="flex-1" />
        <span className="text-2xs text-sidebar-foreground/50 font-mono rounded border bg-sidebar/50 px-1.5 py-0.5">
          ⌘K
        </span>
      </div>
    </>
  )
}

// ── NavItemLink (shared) ───────────────────────────────────────

function NavItemLink({
  item,
  currentPath,
  collapsed,
}: {
  item: NavItem
  currentPath: string
  collapsed: boolean
}) {
  const { t } = useTranslation()
  const isActive =
    currentPath === item.href || (item.href !== '/' && currentPath.startsWith(item.href))
  const showBadge = item.badge != null && item.badge > 0

  const link = (
    <Link
      to={item.href}
      className={cn(itemBase, isActive ? itemActive : itemInactive, collapsed && 'justify-center')}
    >
      {item.icon}
      {!collapsed && <span>{t(item.labelKey)}</span>}
      {!collapsed && showBadge && (
        <span className="ml-auto flex h-4 min-w-4 items-center justify-center rounded-full bg-warning px-1 text-2xs font-bold text-white animate-scale-in">
          {item.badge}
        </span>
      )}
    </Link>
  )

  return collapsed ? (
    <Tooltip key={item.href}>
      <TooltipTrigger asChild>{link}</TooltipTrigger>
      <TooltipContent side="right">
        {`${t(item.labelKey)}${item.badge ? ` (${item.badge})` : ''}`}
      </TooltipContent>
    </Tooltip>
  ) : (
    <React.Fragment key={item.href}>{link}</React.Fragment>
  )
}