Skip to content

Commit c68f6bb

Browse files
committed
Refactor createCompletion to use SessionEntry object
Updated createCompletion and related calls to accept a SessionEntry object instead of separate conversation and sessionId parameters. This change improves type safety and allows for easier management of session-related data, including tools. Also updated type definitions and usages to reflect this refactor.
1 parent 3e9a87f commit c68f6bb

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

src/renderer/components/common/SamplingCard.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ const tryCompletions = () => {
4646
// const { messages, ...restParams } = samplingParams.value
4747
// restParams.target = samplingResults.value
4848
samplingId.value = historyStore.getDate()
49-
createCompletion(samplingResults.value, samplingId.value, samplingParams.value)
49+
createCompletion(
50+
{
51+
id: samplingId.value,
52+
messages: samplingResults.value
53+
},
54+
samplingParams.value
55+
)
5056
}
5157
}
5258
}

src/renderer/composables/chatCompletions.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ import type { ChatbotConfig } from '@/types/llm'
99

1010
import { SamplingRequestParams, SamplingMessage } from '@/types/ipc'
1111

12+
import type { SessionEntry, McpToolMessage } from '@/renderer/types/session'
13+
14+
import { unionBy } from 'lodash'
15+
1216
import type {
1317
AssistantMessage,
1418
ToolCall,
1519
ChatCompletionRequestMessage,
16-
ChatCompletionMessage,
17-
ChatConversationMessage
20+
ChatCompletionMessage
1821
} from '@/renderer/types/message'
1922

20-
import { Tool } from '@modelcontextprotocol/sdk/types'
21-
2223
import { ReasoningEffort, REASONING_EFFORT, ENABLE_THINKING } from '@/renderer/types'
2324

2425
type ChatRequestBody = {
@@ -27,7 +28,7 @@ type ChatRequestBody = {
2728
temperature?: number
2829
messages?: ChatCompletionMessage[]
2930
top_p?: number
30-
tools?: Tool[]
31+
tools?: McpToolMessage[]
3132
reasoning_effort?: ReasoningEffort
3233
enable_thinking?: boolean
3334
chat_template_kwargs?: Record<string, any>
@@ -129,8 +130,7 @@ const promptMessage = (
129130
}
130131

131132
export const createCompletion = async (
132-
targetConversation: ChatConversationMessage[],
133-
sessionId: string,
133+
session: SessionEntry,
134134
sampling?: SamplingRequestParams
135135
): Promise<ChatProcessResult> => {
136136
const snackbarStore = useSnackbarStore()
@@ -185,7 +185,7 @@ export const createCompletion = async (
185185
}
186186
}
187187

188-
const target = targetConversation
188+
const target = session.messages
189189

190190
if (!sampling) {
191191
const conversation = target.reduce(
@@ -225,6 +225,7 @@ export const createCompletion = async (
225225
const tools = await agentStore.getTools()
226226
if (tools && tools.length > 0) {
227227
body.tools = tools
228+
session.tools = unionBy(session.tools, tools, 'function.name')
228229
}
229230
}
230231
} else {
@@ -249,8 +250,8 @@ export const createCompletion = async (
249250

250251
const abortController = new AbortController()
251252

252-
console.log('Chat session started: ', sessionId)
253-
messageStore.generating[sessionId] = abortController
253+
console.log('Chat session started: ', session.id)
254+
messageStore.generating[session.id] = abortController
254255

255256
const completion = await fetch(
256257
chatbotConfig.url + (chatbotConfig.path ? chatbotConfig.path : ''),
@@ -305,11 +306,11 @@ export const createCompletion = async (
305306
const buffer = ''
306307

307308
// Read the stream
308-
await read(reader, sessionId, lastItem, buffer, chatbotConfig.stream)
309+
await read(reader, session.id, lastItem, buffer, chatbotConfig.stream)
309310
} catch (error: any) {
310311
snackbarStore.showErrorMessage(error?.message)
311312
} finally {
312-
const result = messageStore.delete(sessionId) ? 'done' : 'aborted'
313+
const result = messageStore.delete(session.id) ? 'done' : 'aborted'
313314
return result
314315
}
315316
}

src/renderer/store/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export const useMessageStore = defineStore('messageStore', {
137137
// Verify the old conversation still exist
138138
if (oldConversation) {
139139
this.generating[sessionId] = 'prepare'
140-
createCompletion(oldConversation.messages, sessionId).then((reason: ChatProcessResult) => {
140+
createCompletion(oldConversation).then((reason: ChatProcessResult) => {
141141
if (reason === 'done') {
142142
this.postToolCall(sessionId)
143143
}

src/renderer/types/session.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import type { ChatConversationMessage } from '@/renderer/types/message'
22

3+
import { Tool as McpToolMessage } from '@modelcontextprotocol/sdk/types'
4+
35
export type SessionId = string
46

7+
export type { McpToolMessage }
8+
59
export type SessionEntry = {
610
id: SessionId
711
messages: ChatConversationMessage[]
12+
tools?: McpToolMessage[]
813
}

0 commit comments

Comments
 (0)