MCPcopy Create free account
hub / github.com/experdot/pointer / useChatTitles

Function useChatTitles

src/renderer/src/hooks/useChatTitles.ts:36–141  ·  view source on GitHub ↗
({
  pageId,
  messages,
  currentPath
}: UseChatTitlesOptions)

Source from the content-addressed store, hash-verified

34}
35
36export function useChatTitles({
37 pageId,
38 messages,
39 currentPath
40}: UseChatTitlesOptions): UseChatTitlesResult {
41 const [batchProgress, setBatchProgress] = useState<BatchProgress | null>(null)
42
43 const updateTitle = useCallback(
44 (messageId: string, title: string) => {
45 updateMessageTitle(pageId, messageId, title)
46 },
47 [pageId]
48 )
49
50 const deleteTitle = useCallback(
51 (messageId: string) => {
52 updateMessageTitle(pageId, messageId, '')
53 },
54 [pageId]
55 )
56
57 // 统一的 generateTitle 方法,通过 options 决定是否使用自定义配置
58 const generateTitle = useCallback(
59 async (messageId: string, options?: GenerateOptions) => {
60 const message = messages.find((m) => m.id === messageId)
61 if (!message) return
62
63 try {
64 const generation = getSwitchGeneration()
65 const result =
66 options?.llmId || options?.extraRequirements || options?.modelConfigId
67 ? await generateMessageTitleWithOptions({
68 content: message.content,
69 extraRequirements: options.extraRequirements,
70 llmId: options.llmId,
71 modelConfigId: options.modelConfigId
72 })
73 : await generateMessageTitle(message.content)
74
75 if (generation === getSwitchGeneration() && result.success && result.title) {
76 updateMessageTitle(pageId, messageId, result.title)
77 } else if (result.error) {
78 console.error('Failed to generate title:', result.error)
79 }
80 } catch (error) {
81 console.error('Failed to generate title:', error)
82 }
83 },
84 [pageId, messages]
85 )
86
87 // 统一的批量生成标题方法,消除了原来两个几乎相同的方法
88 const batchGenerateTitles = useCallback(
89 async (options?: GenerateOptions) => {
90 // 筛选出没有标题的消息
91 const messagesWithoutTitle = currentPath.filter((m) => !m.title)
92 if (messagesWithoutTitle.length === 0) return
93

Callers 1

useChatFunction · 0.90

Calls 4

updateMessageTitleFunction · 0.90
getSwitchGenerationFunction · 0.90
generateMessageTitleFunction · 0.90

Tested by

no test coverage detected