Skip to content
Merged
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
166 changes: 142 additions & 24 deletions packages/dsh-desktop-client-ui/client.js

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions src/main/enterprise/chat-view.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'node:path'
import { randomUUID } from 'node:crypto'
import { app, session, shell, WebContentsView, type BrowserWindow, type Session } from 'electron'
import { parseEnterpriseNavigation, parseEnterpriseSettingsSection, parseChatPage, parseChatId, parseChatPresentation, type EnterpriseSettingsSection, type EnterpriseChatFrame, type EnterpriseChatHistory, type EnterpriseChatLandingGeometry, type EnterpriseChatPresentation, type EnterpriseChatViewState } from '../../shared/enterprise-chat-view'
import { parseEnterpriseNavigation, parseEnterpriseSettingsSection, parseChatPage, parseChatId, parseChatName, parseChatPresentation, parseRunningConversations, type EnterpriseSettingsSection, type EnterpriseChatFrame, type EnterpriseChatHistory, type EnterpriseChatLandingGeometry, type EnterpriseChatPresentation, type EnterpriseChatViewState } from '../../shared/enterprise-chat-view'
import type { EnterpriseDesktopRuntime } from './enterprise-desktop'
import { MODULE_ROUTE_PATTERN, MODULE_THEME_CSS, moduleThemeScript } from './chat-module-theme'
import { chatDialogViewportScript, guardChatDialogViewportScript } from './chat-dialog-viewport'
Expand Down Expand Up @@ -95,6 +95,8 @@ export class EnterpriseChatView {
private origin?: string
private routes = new Map<string, string>()
private deletedConversations = new Set<string>()
/** Reported by the current enterprise document; a new document starts empty and reports again. */
private running: string[] = []
private view?: WebContentsView
private isolated?: Session
private owner?: BrowserWindow
Expand Down Expand Up @@ -151,7 +153,8 @@ export class EnterpriseChatView {
conversationId: url?.pathname.match(/\/workspace\/(?:c|chat|linsight)\/([a-zA-Z0-9_-]+)/u)?.[1],
connected: account.connected,
projectScope: account.connected ? account.connectionKey : undefined,
draftProjectId: projectId && /^[a-zA-Z0-9_-]{1,128}$/u.test(projectId) ? projectId : undefined
draftProjectId: projectId && /^[a-zA-Z0-9_-]{1,128}$/u.test(projectId) ? projectId : undefined,
...(this.running.length ? { running: this.running } : {})
}
}

Expand Down Expand Up @@ -439,6 +442,32 @@ export class EnterpriseChatView {
return { epoch }
}

/** Same history grant as deletion. The server also stamps the conversation's update time. */
async rename(input: unknown, nameInput: unknown, expectedEpoch: unknown): Promise<{ epoch: number }> {
const id = parseChatId(input)
const name = parseChatName(nameInput)
const epoch = this.generation
if (expectedEpoch !== epoch || !this.enterprise.snapshot().connected || !this.routes.has(id)) {
throw new Error('Select a conversation from current Chat history.')
}
if (this.state.phase !== 'ready') await this.connect()
if (epoch !== this.generation || !this.isolated || !this.origin) throw new Error('Chat identity changed.')
const response = await this.isolated.fetch(new URL('/api/v1/chat/conversation/rename', this.origin).href, {
method: 'POST', redirect: 'error', headers: { 'content-type': 'application/json' },
body: JSON.stringify({ conversationId: id, name }), signal: AbortSignal.timeout(15_000)
})
if (!response.ok) throw new Error(`Chat rename failed (${response.status}).`)
const result: unknown = await response.json()
if (epoch !== this.generation) throw new Error('Chat identity changed.')
if (!result || typeof result !== 'object' || !('status_code' in result) || result.status_code !== 200) {
throw new Error('Chat rename failed.')
}
this.note('[enterprise-chat] conversation renamed')
// The open document loads a conversation's name once; tell it to read the new one.
if (this.view && !this.view.webContents.isDestroyed()) this.view.webContents.send('enterprise-chat:conversation-renamed', { conversationId: id })
return { epoch }
}

async open(input: unknown, projectInput?: unknown): Promise<void> {
const id = parseChatId(input)
const projectId = projectInput === undefined ? undefined : projectUuid(projectInput)
Expand Down Expand Up @@ -754,6 +783,15 @@ export class EnterpriseChatView {
catch { this.note('[enterprise-chat] invalid_navigation_report') }
return
}
if (channel === 'enterprise-chat:running') {
try {
const running = parseRunningConversations(report.ids)
if (running.join() === this.running.join()) return
this.running = running
this.notify()
} catch { this.note('[enterprise-chat] invalid_running_report') }
return
}
if (channel !== 'enterprise-chat:landing' || !/^\/workspace\/c\/[a-zA-Z0-9_-]+$/u.test(url.pathname) || typeof report.landing !== 'boolean') return
this.landing = url.pathname === '/workspace/c/new' && report.landing
this.landingPath = url.pathname
Expand All @@ -768,6 +806,8 @@ export class EnterpriseChatView {
this.landingPath = undefined
this.welcomeCases = undefined
this.layout()
// Streams belong to the outgoing document; the next one reports its own.
if (this.running.length) { this.running = []; this.notify() }
}
if (isMainFrame && !isInPlace && generation === this.generation && this.view === view && this.settingsSection) {
this.settingsReady = false
Expand Down Expand Up @@ -1052,6 +1092,7 @@ export class EnterpriseChatView {
this.origin = undefined
this.routes.clear()
this.deletedConversations.clear()
this.running = []
const view = this.view
const isolated = this.isolated
this.view = undefined
Expand Down
7 changes: 6 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ function registerHarnessHandlers(): void {
}
})

for (const channel of ['enterprise-chat:present', 'enterprise-chat:capture', 'enterprise-chat:landing-geometry', 'enterprise-chat:landing-text-motion', 'enterprise-chat:state', 'enterprise-chat:retry', 'enterprise-chat:usage', 'enterprise-chat:history', 'enterprise-chat:open', 'enterprise-chat:remove', 'enterprise-chat:open-page', 'enterprise-chat:focus-settings', 'enterprise-chat:projects-state', 'enterprise-chat:project-action', 'enterprise-chat:project-conversations']) ipcMain.removeHandler(channel)
for (const channel of ['enterprise-chat:present', 'enterprise-chat:capture', 'enterprise-chat:landing-geometry', 'enterprise-chat:landing-text-motion', 'enterprise-chat:state', 'enterprise-chat:retry', 'enterprise-chat:usage', 'enterprise-chat:history', 'enterprise-chat:open', 'enterprise-chat:remove', 'enterprise-chat:rename', 'enterprise-chat:open-page', 'enterprise-chat:focus-settings', 'enterprise-chat:projects-state', 'enterprise-chat:project-action', 'enterprise-chat:project-conversations']) ipcMain.removeHandler(channel)
ipcMain.handle('enterprise-chat:projects-state', (event) => {
assertTrustedMainWindowEvent(event)
return enterpriseChat?.projectsState()
Expand Down Expand Up @@ -1803,6 +1803,11 @@ function registerHarnessHandlers(): void {
if (!enterpriseChat) throw new Error('Enterprise Chat unavailable.')
return enterpriseChat.remove(id, epoch)
})
ipcMain.handle('enterprise-chat:rename', (event, id: unknown, name: unknown, epoch: unknown) => {
assertTrustedMainWindowEvent(event)
if (!enterpriseChat) throw new Error('Enterprise Chat unavailable.')
return enterpriseChat.rename(id, name, epoch)
})
ipcMain.removeHandler('harness:restart')
ipcMain.handle('harness:restart', async (event) => {
if (!mainWindow || mainWindow.isDestroyed() || event.sender !== mainWindow.webContents) {
Expand Down
13 changes: 13 additions & 0 deletions src/preload/enterprise-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ contextBridge.exposeInMainWorld('dshChatPresentation', Object.freeze({
ipcRenderer.on('enterprise-chat:projects-changed', receive)
return () => ipcRenderer.removeListener('enterprise-chat:projects-changed', receive)
},
onConversationRenamed(listener: unknown): () => void {
if (typeof listener !== 'function') throw new TypeError('Rename listener must be a function.')
const receive = (_event: Electron.IpcRendererEvent, detail: unknown): void => {
const id = detail && typeof detail === 'object' ? (detail as { conversationId?: unknown }).conversationId : undefined
if (typeof id === 'string') listener(id)
}
ipcRenderer.on('enterprise-chat:conversation-renamed', receive)
return () => ipcRenderer.removeListener('enterprise-chat:conversation-renamed', receive)
},
/** Conversations whose replies or tasks are still running in this document. */
reportRunning(ids: unknown): void {
ipcRenderer.send('enterprise-chat:running', { ids, path: window.location.pathname })
},
reportNavigation(navigation: unknown): void {
ipcRenderer.send('enterprise-chat:navigation', { navigation, path: window.location.pathname })
},
Expand Down
1 change: 1 addition & 0 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ contextBridge.exposeInMainWorld('dshEnterpriseChat', Object.freeze({
openPage: (id: string): Promise<void> => ipcRenderer.invoke('enterprise-chat:open-page', id),
open: (id: string, projectId?: string): Promise<void> => ipcRenderer.invoke('enterprise-chat:open', id, projectId),
remove: (id: string, epoch: number): Promise<{ epoch: number }> => ipcRenderer.invoke('enterprise-chat:remove', id, epoch),
rename: (id: string, name: string, epoch: number): Promise<{ epoch: number }> => ipcRenderer.invoke('enterprise-chat:rename', id, name, epoch),
subscribe: (listener: (state: EnterpriseChatViewState) => void): (() => void) => {
const receive = (_event: Electron.IpcRendererEvent, state: EnterpriseChatViewState): void => listener(state)
ipcRenderer.on('enterprise-chat:changed', receive)
Expand Down
16 changes: 16 additions & 0 deletions src/shared/enterprise-chat-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export interface EnterpriseChatViewState {
settingsSection?: EnterpriseSettingsSection
settingsReady?: boolean
settingsFailure?: 'load' | 'unsupported'
/** Cloud conversations the enterprise document reports as generating; absent until it reports any. */
running?: string[]
}

export function parseChatPresentation(value: unknown): EnterpriseChatPresentation {
Expand Down Expand Up @@ -138,6 +140,20 @@ export function parseChatId(value: unknown): string {
if (typeof value !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/u.test(value)) throw new Error('Invalid Chat identity.')
return value
}
/** BiSheng stores conversation names in message_session.name, a VARCHAR(255) column. */
export const CHAT_NAME_MAX_LENGTH = 255
export function parseChatName(value: unknown): string {
if (typeof value !== 'string') throw new Error('Invalid Chat name.')
const name = value.trim()
if (!name || [...name].length > CHAT_NAME_MAX_LENGTH || /\p{Cc}/u.test(name)) throw new Error('Invalid Chat name.')
return name
}
const MAX_RUNNING_CONVERSATIONS = 100
/** Activity is presentation data from the enterprise document, never an authorization signal. */
export function parseRunningConversations(value: unknown): string[] {
if (!Array.isArray(value) || value.length > MAX_RUNNING_CONVERSATIONS) throw new Error('Invalid Chat activity.')
return [...new Set(value.map(parseChatId))].sort()
}

export interface EnterpriseNavigationEntry {
id: string
Expand Down
Loading
Loading