Skip to content

Commit 0c74dcf

Browse files
committed
Refine the type definitions
1 parent 40d20c9 commit 0c74dcf

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

src/renderer/composables/chatCompletions.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,34 @@ import type {
1818

1919
import { Tool } from '@modelcontextprotocol/sdk/types'
2020

21-
import { REASONING_EFFORT, ENABLE_THINKING } from '@/renderer/types'
21+
import { ReasoningEffort, REASONING_EFFORT, ENABLE_THINKING } from '@/renderer/types'
2222

23-
interface ChatRequestBody {
23+
type ChatRequestBody = {
2424
model?: string
2525
stream?: boolean
2626
temperature?: number
2727
messages?: ChatCompletionMessage[]
2828
top_p?: number
2929
tools?: Tool[]
30+
reasoning_effort?: ReasoningEffort
31+
enable_thinking?: boolean
32+
chat_template_kwargs?: Record<string, any>
33+
[key: string]: unknown
34+
}
35+
36+
type SimpleStreamChoice = {
37+
index: number
38+
delta: AssistantMessage
39+
message?: AssistantMessage
40+
finish_reason: string | null
41+
}
42+
43+
type SimpleStreamResponse = {
44+
choices: SimpleStreamChoice[]
45+
}
46+
47+
type SimpleCfResponse = {
48+
response: AssistantMessage
3049
}
3150

3251
export type ChatProcessResult = 'aborted' | 'error' | 'done'
@@ -370,17 +389,20 @@ const read = async (
370389
return read(reader, sessionId, target, buffer, stream)
371390
}
372391

373-
const parseJson = (content, target: AssistantMessage) => {
392+
const parseJson = (content: string, target: AssistantMessage) => {
374393
try {
375-
const parsed = JSON.parse(content)
394+
const parsed = JSON.parse(content as string)
376395
parseChoices(parsed, target)
377396
} catch (e) {
378397
console.log(e, content)
379398
parseChoice(content, target)
380399
}
381400
}
382401

383-
const parseChoices = (parsed, target) => {
402+
const parseChoices = (
403+
parsed: SimpleStreamResponse | SimpleCfResponse,
404+
target: AssistantMessage
405+
) => {
384406
if ('choices' in parsed) {
385407
return parsed.choices.map((choice) => {
386408
const content = choice.delta || choice.message
@@ -393,7 +415,7 @@ const parseChoices = (parsed, target) => {
393415
}
394416
}
395417

396-
const parseChoice = (choice: AssistantMessage, target: AssistantMessage) => {
418+
const parseChoice = (choice: AssistantMessage | string, target: AssistantMessage) => {
397419
if (choice) {
398420
if (target.role === 'assistant') {
399421
if (typeof choice === 'string') {
@@ -408,7 +430,7 @@ const parseChoice = (choice: AssistantMessage, target: AssistantMessage) => {
408430
target.reasoning_content += choice.reasoning_content
409431
}
410432
}
411-
parseTool(choice.tool_calls, target)
433+
parseTool((choice as AssistantMessage).tool_calls, target)
412434
}
413435
}
414436
}
@@ -453,18 +475,19 @@ const parseTool = (tools: ToolCall[] | undefined, target: AssistantMessage) => {
453475

454476
// Merge each property from source function
455477
Object.keys(sourceFunc).forEach((key) => {
456-
const value = sourceFunc[key]
478+
const typedKey = key as 'name' | 'arguments'
479+
const value = sourceFunc[typedKey]
457480

458481
// Skip null values (don't overwrite existing values with null)
459482
if (value === null) return
460483

461484
// Merge strategy:
462485
// - If target has existing non-empty value: concatenate
463486
// - Otherwise: overwrite
464-
if (targetFunc[key] && targetFunc[key] !== '{}') {
465-
targetFunc[key] += value
487+
if (targetFunc[typedKey] && targetFunc[typedKey] !== '{}') {
488+
targetFunc[typedKey] += value
466489
} else {
467-
targetFunc[key] = value
490+
targetFunc[typedKey] = value
468491
}
469492
})
470493
}

src/renderer/types/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export const CHATBOT_DEFAULTS = {
3535
mcp: true
3636
}
3737

38-
export const REASONING_EFFORT = ['minimal', 'low', 'medium', 'high']
38+
export const REASONING_EFFORT = ['minimal', 'low', 'medium', 'high'] as const
39+
40+
export type ReasoningEffort = (typeof REASONING_EFFORT)[number]
3941

4042
export const ENABLE_THINKING = ['true', 'false']

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"module": "esnext",
77
"moduleResolution": "bundler",
88
"jsx": "preserve",
9-
"noImplicitAny": false,
9+
"noImplicitAny": true,
1010
"noUnusedLocals": true,
1111
"noUnusedParameters": true,
1212
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)