Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/launcher/src/main/agent-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path"
import fs from "fs"
import os from "os"
import https from "https"
import crypto from "crypto"
import { spawnSync } from "child_process"
import { withPathEnv } from "./env"
import { EventEmitter } from "events"
Expand Down Expand Up @@ -2131,6 +2132,39 @@ export class AgentManager extends EventEmitter {
return removed
}

createChatSession(workspaceId: string): ChatSessionMeta {
const ws = this._resolveChatWorkspace(workspaceId)
if (!ws) throw new Error("Workspace not found")

const dir = path.join(LAUNCHER_SESSIONS_DIR, ws.id)
ensureDir(dir)

let channelName = ""
let file = ""
do {
channelName = `chat-${Date.now()}-${crypto.randomUUID().slice(0, 8)}`
file = path.join(dir, `${channelName}.json`)
} while (fs.existsSync(file))

const now = new Date().toISOString()
const meta: ChatSessionMeta = {
id: `${ws.id}:${channelName}`,
workspaceId: ws.id,
workspaceSlug: ws.slug,
workspaceName: ws.name,
channelName,
title: ws.name || ws.slug || channelName,
lastMessageAt: null,
lastMessagePreview: null,
messageCount: 0,
participants: [],
createdAt: now,
}

fs.writeFileSync(file, JSON.stringify(meta, null, 2), "utf-8")
return meta
}

private _touchChatSession(
ws: WorkspaceConfig,
channelName: string,
Expand Down
3 changes: 3 additions & 0 deletions packages/launcher/src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,9 @@ function setupIPC(): void {
ipcMain.handle("session:list", (_e, workspaceId) =>
requireManager().listChatSessions(workspaceId),
)
ipcMain.handle("session:create", (_e, workspaceId) =>
requireManager().createChatSession(workspaceId),
)
ipcMain.handle("session:load", (_e, workspaceId, channelName) =>
requireManager().loadChatSession(workspaceId, channelName),
)
Expand Down
1 change: 1 addition & 0 deletions packages/launcher/src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ contextBridge.exposeInMainWorld('api', {

// ── Sessions ──
sessionList: (workspaceId?: string) => ipcRenderer.invoke('session:list', workspaceId),
sessionCreate: (workspaceId: string) => ipcRenderer.invoke('session:create', workspaceId),
sessionLoad: (workspaceId: string, channelName: string) =>
ipcRenderer.invoke('session:load', workspaceId, channelName),
sessionDelete: (workspaceId: string, channelName: string) =>
Expand Down
13 changes: 9 additions & 4 deletions packages/launcher/src/renderer/pages/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import SessionList from '../../components/chat/SessionList'
import { ConfirmDialog } from '../../components/ui/ConfirmDialog'
import type { ToastType } from '../../hooks/useToast'

const DEFAULT_CHANNEL = 'main'

interface ChatPageProps {
showToast: (msg: string, type?: ToastType) => void
}
Expand Down Expand Up @@ -277,9 +275,16 @@ export default function ChatPage({ showToast }: ChatPageProps): React.JSX.Elemen
void activate(workspaceId, channelName)
}

const handleNewChat = (): void => {
const handleNewChat = async (): Promise<void> => {
if (!selectedWorkspaceId) return
void activate(selectedWorkspaceId, DEFAULT_CHANNEL)
try {
const session = await window.api.sessionCreate(selectedWorkspaceId)
setSelectedWorkspaceId(session.workspaceId)
await refreshSessions()
await activate(session.workspaceId, session.channelName)
} catch (e: unknown) {
showToast(`New chat failed: ${(e as Error).message}`, 'error')
}
}

const requestDeleteSession = (workspaceId: string, channelName: string): void => {
Expand Down
1 change: 1 addition & 0 deletions packages/launcher/src/renderer/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ declare global {

// ── Sessions ──
sessionList(workspaceId?: string): Promise<ChatSessionMeta[]>
sessionCreate(workspaceId: string): Promise<ChatSessionMeta>
sessionLoad(workspaceId: string, channelName: string): Promise<ChatSessionMeta | null>
sessionDelete(workspaceId: string, channelName: string): Promise<boolean>
sessionClear(workspaceId?: string): Promise<number>
Expand Down
Loading