Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 5972bf9

Browse files
committed
feat: set conversation title
1 parent d543060 commit 5972bf9

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/apis/chatgpt.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ export async function setConversationProperty(
3333
await request(token, 'PATCH', `/conversation/${conversationId}`, propertyObject)
3434
}
3535

36+
export async function setTitle(accessToken: string, conversationId: string) {
37+
const title = 'ἐντελέχεια.άι'
38+
if (conversationId) {
39+
const ret = await request(accessToken, 'PATCH', `/conversation/${conversationId}`, {
40+
title: title,
41+
})
42+
const data = JSON.parse(ret.responseText)
43+
console.debug('set_title', data)
44+
if (data.sucssess === true) {
45+
return title
46+
} else {
47+
return null
48+
}
49+
}
50+
}
51+
3652
export async function sendModerations(
3753
token: string,
3854
question: string,

src/configs/userConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ type UserConfigType = {
147147
tokenSavedOn: number
148148
messageId: string | null
149149
conversationId: string | null
150+
conversationTitle: string | null
150151
}
151152

152153
export const defaultConfig: UserConfigType = {
@@ -166,6 +167,7 @@ export const defaultConfig: UserConfigType = {
166167
tokenSavedOn: 0,
167168
messageId: uuidv4(),
168169
conversationId: null,
170+
conversationTitle: null,
169171
}
170172

171173
export async function getUserConfig(): Promise<UserConfigType> {

src/service/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
generateAnswersWithChatgptWebApi,
88
generateAnswersWithGptCompletionApi,
99
sendMessageFeedback,
10+
setTitle,
1011
} from '../apis'
1112
import {
1213
chatgptApiModelKeys,
1314
chatgptWebModelKeys,
1415
getUserConfig,
1516
gptApiModelKeys,
1617
isUsingApiKey,
18+
updateUserConfig,
1719
} from '../configs'
1820
import { cache, getAccessToken, initSession, KEY_ACCESS_TOKEN } from '../utils'
1921

@@ -47,6 +49,16 @@ Browser.runtime.onConnect.addListener((port) => {
4749
}
4850
console.debug('session', session)
4951
await generateAnswersWithChatgptWebApi(port, session.question, session, accessToken)
52+
53+
// set conversation title
54+
if (session.conversationId && session.conversationTitle == null) {
55+
console.debug('set title')
56+
const title = await setTitle(accessToken, session.conversationId)
57+
if (title) {
58+
session.conversationTitle = title
59+
await updateUserConfig({ conversationTitle: title })
60+
}
61+
}
5062
} else if (gptApiModelKeys.includes(config.modelName)) {
5163
await generateAnswersWithGptCompletionApi(
5264
port,
@@ -118,6 +130,19 @@ Browser.runtime.onMessage.addListener(async (message) => {
118130
} else if (message.type === 'SET_SESSION') {
119131
console.debug('setting session', message.session)
120132
sessionDataMap.set(sessionId, message.session)
133+
} else if (message.type === 'SET_TITLE') {
134+
// set conversation title
135+
const accessToken = await getAccessToken()
136+
const config = await getUserConfig()
137+
const session = sessionDataMap.get(sessionId)
138+
139+
if (session.conversationId && session.conversationId !== config.conversationId) {
140+
const title = await setTitle(accessToken, session.conversationId)
141+
if (title) {
142+
session.conversationTitle = title
143+
await updateUserConfig({ conversationTitle: title })
144+
}
145+
}
121146
}
122147
})
123148

src/utils/initSession.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Session {
99
conversationId: string | null
1010
messageId: string | null
1111
parentMessageId: string | null
12+
conversationTitle: string | null
1213
conversationRecords: ConversationRecord[] | null
1314
useApiKey: boolean | null
1415
}
@@ -18,6 +19,7 @@ export function initSession({
1819
conversationId = null,
1920
messageId = null,
2021
parentMessageId = null,
22+
conversationTitle = null,
2123
conversationRecords = [],
2224
useApiKey = null,
2325
}: Partial<Session> = {}): Session {
@@ -26,6 +28,7 @@ export function initSession({
2628
conversationId,
2729
messageId,
2830
parentMessageId,
31+
conversationTitle,
2932
conversationRecords,
3033
useApiKey,
3134
}

0 commit comments

Comments
 (0)