File size: 1,473 Bytes
68d7816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { IEventService } from '#/app/event/event';

import { titleFromPromptMetadataText } from '#/agent/prompt/promptMetadataText';

import type { ISessionMetadata, SessionTitleKind } from './sessionMetadata';
import { SessionMetaUpdated } from './sessionMetaEvents';

export function isUntitled(title: string | undefined): boolean {
  return title === undefined || title.trim().length === 0 || title === 'New Session';
}

export interface PromptMetadataUpdateTarget {
  readonly metadata: ISessionMetadata;
  readonly eventService: IEventService;
  readonly sessionId: string;
}

export async function applyPromptMetadataUpdate(
  target: PromptMetadataUpdateTarget,
  text: string | undefined,
): Promise<void> {
  if (text === undefined) return;
  const current = await target.metadata.read();
  const patch: { lastPrompt: string; title?: string; titleKind?: SessionTitleKind } = {
    lastPrompt: text,
  };
  if (current.titleKind !== 'custom' && isUntitled(current.title)) {
    patch.title = titleFromPromptMetadataText(text);
    patch.titleKind = 'replaceable';
  }
  await target.metadata.update(patch);
  target.eventService.publish(
    new SessionMetaUpdated({
      payload: {
        agentId: 'main',
        sessionId: target.sessionId,
        title: patch.title,
        patch: {
          title: patch.title,
          isCustomTitle: patch.titleKind === undefined ? undefined : false,
          lastPrompt: text,
        },
      },
    }),
  );
}