| 1 | import type { ProjectNode } from "./types"; |
| 2 | import type { SessionMutationResult, SessionSelector } from "../generated/desktopContract.generated"; |
| 3 | import { sessionTitleTarget } from "./sessionTitleOperation"; |
| 4 | |
| 5 | export interface SessionTitleBindings { |
| 6 | AIRenameSession(topicID: string): Promise<string>; |
| 7 | AIRenameSessionTarget(selector: SessionSelector): Promise<SessionMutationResult>; |
| 8 | RenameSessionTarget(selector: SessionSelector, title: string): Promise<SessionMutationResult>; |
| 9 | } |
| 10 | |
| 11 | export function mockAIRenameSession(topic?: ProjectNode | null): string { |
| 12 | if (!topic) return ""; |
| 13 | const title = topic.preview?.trim() || topic.label?.replace(/^●\s*/, "").trim() || ""; |
| 14 | if (title) topic.label = `${topic.label?.startsWith("● ") ? "● " : ""}${title}`; |
| 15 | return title; |
| 16 | } |
| 17 | |
| 18 | export function mockAIRenameTarget(nodes: ProjectNode[], target: string): string { |
| 19 | const find = (rows: ProjectNode[]): ProjectNode | undefined => { |
| 20 | for (const node of rows) { |
| 21 | if (sessionTitleTarget(node) === target || node.topicId === target) return node; |
| 22 | const child = find(node.children ?? []); |
| 23 | if (child) return child; |
| 24 | } |
| 25 | return undefined; |
| 26 | }; |
| 27 | return mockAIRenameSession(find(nodes)); |
| 28 | } |
| 29 | |
| 30 | export function mockSessionTitleTarget(nodes: ProjectNode[], selector: SessionSelector): ProjectNode | undefined { |
| 31 | const target = selector.ref?.sessionId |
| 32 | ? `session-id:${selector.ref.sessionId}` |
| 33 | : selector.sessionPath?.trim() || selector.topicId?.trim() || ""; |
| 34 | const find = (rows: ProjectNode[]): ProjectNode | undefined => { |
| 35 | for (const node of rows) { |
| 36 | if (sessionTitleTarget(node) === target || node.topicId === target) return node; |
| 37 | const child = find(node.children ?? []); |
| 38 | if (child) return child; |
| 39 | } |
| 40 | return undefined; |
| 41 | }; |
| 42 | return find(nodes); |
| 43 | } |
| 44 |