Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
bit-pm
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
燕伟桐
bit-pm
Commits
813f170c
Unverified
提交
813f170c
authored
4月 28, 2025
作者:
Will Chen
提交者:
GitHub
4月 28, 2025
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prefill messages in main process and ensure loading state is always shown (incl. new chat) (#35)
上级
9fb5439e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
54 行增加
和
58 行删除
+54
-58
useStreamChat.ts
src/hooks/useStreamChat.ts
+6
-32
chat_stream_handlers.ts
src/ipc/handlers/chat_stream_handlers.ts
+48
-26
没有找到文件。
src/hooks/useStreamChat.ts
浏览文件 @
813f170c
...
@@ -62,44 +62,18 @@ export function useStreamChat({
...
@@ -62,44 +62,18 @@ export function useStreamChat({
}
}
setError
(
null
);
setError
(
null
);
setMessages
((
currentMessages
:
Message
[])
=>
{
if
(
redo
)
{
let
remainingMessages
=
currentMessages
.
slice
();
if
(
currentMessages
[
currentMessages
.
length
-
1
].
role
===
"assistant"
)
{
remainingMessages
=
currentMessages
.
slice
(
0
,
-
1
);
}
return
[
...
remainingMessages
,
{
id
:
getRandomNumberId
(),
role
:
"assistant"
,
content
:
""
,
},
];
}
return
[
...
currentMessages
,
{
id
:
getRandomNumberId
(),
role
:
"user"
,
content
:
prompt
,
},
{
id
:
getRandomNumberId
(),
role
:
"assistant"
,
content
:
""
,
},
];
});
setIsStreaming
(
true
);
setIsStreaming
(
true
);
setStreamCount
((
streamCount
)
=>
streamCount
+
1
)
;
let
hasIncrementedStreamCount
=
false
;
try
{
try
{
IpcClient
.
getInstance
().
streamMessage
(
prompt
,
{
IpcClient
.
getInstance
().
streamMessage
(
prompt
,
{
chatId
,
chatId
,
redo
,
redo
,
onUpdate
:
(
updatedMessages
:
Message
[])
=>
{
onUpdate
:
(
updatedMessages
:
Message
[])
=>
{
if
(
!
hasIncrementedStreamCount
)
{
setStreamCount
((
streamCount
)
=>
streamCount
+
1
);
hasIncrementedStreamCount
=
true
;
}
setMessages
(
updatedMessages
);
setMessages
(
updatedMessages
);
},
},
onEnd
:
(
response
:
ChatResponseEnd
)
=>
{
onEnd
:
(
response
:
ChatResponseEnd
)
=>
{
...
...
src/ipc/handlers/chat_stream_handlers.ts
浏览文件 @
813f170c
...
@@ -97,6 +97,16 @@ export function registerChatStreamHandlers() {
...
@@ -97,6 +97,16 @@ export function registerChatStreamHandlers() {
})
})
.
returning
();
.
returning
();
// Add a placeholder assistant message immediately
const
[
placeholderAssistantMessage
]
=
await
db
.
insert
(
messages
)
.
values
({
chatId
:
req
.
chatId
,
role
:
"assistant"
,
content
:
""
,
// Start with empty content
})
.
returning
();
// Fetch updated chat data after possible deletions and additions
// Fetch updated chat data after possible deletions and additions
const
updatedChat
=
await
db
.
query
.
chats
.
findFirst
({
const
updatedChat
=
await
db
.
query
.
chats
.
findFirst
({
where
:
eq
(
chats
.
id
,
req
.
chatId
),
where
:
eq
(
chats
.
id
,
req
.
chatId
),
...
@@ -112,6 +122,12 @@ export function registerChatStreamHandlers() {
...
@@ -112,6 +122,12 @@ export function registerChatStreamHandlers() {
throw
new
Error
(
`Chat not found:
${
req
.
chatId
}
`
);
throw
new
Error
(
`Chat not found:
${
req
.
chatId
}
`
);
}
}
// Send the messages right away so that the loading state is shown for the message.
event
.
sender
.
send
(
"chat:response:chunk"
,
{
chatId
:
req
.
chatId
,
messages
:
updatedChat
.
messages
,
});
let
fullResponse
=
""
;
let
fullResponse
=
""
;
// Check if this is a test prompt
// Check if this is a test prompt
...
@@ -206,7 +222,7 @@ export function registerChatStreamHandlers() {
...
@@ -206,7 +222,7 @@ export function registerChatStreamHandlers() {
temperature
:
0
,
temperature
:
0
,
model
:
modelClient
,
model
:
modelClient
,
system
:
systemPrompt
,
system
:
systemPrompt
,
messages
:
chatMessages
,
messages
:
chatMessages
.
filter
((
m
)
=>
m
.
content
)
,
onError
:
(
error
)
=>
{
onError
:
(
error
)
=>
{
logger
.
error
(
"Error streaming text:"
,
error
);
logger
.
error
(
"Error streaming text:"
,
error
);
const
message
=
const
message
=
...
@@ -240,16 +256,20 @@ export function registerChatStreamHandlers() {
...
@@ -240,16 +256,20 @@ export function registerChatStreamHandlers() {
// Store the current partial response
// Store the current partial response
partialResponses
.
set
(
req
.
chatId
,
fullResponse
);
partialResponses
.
set
(
req
.
chatId
,
fullResponse
);
// Update the placeholder assistant message content in the messages array
const
currentMessages
=
[...
updatedChat
.
messages
];
if
(
currentMessages
.
length
>
0
&&
currentMessages
[
currentMessages
.
length
-
1
].
role
===
"assistant"
)
{
currentMessages
[
currentMessages
.
length
-
1
].
content
=
fullResponse
;
}
// Update the assistant message in the database
// Update the assistant message in the database
event
.
sender
.
send
(
"chat:response:chunk"
,
{
event
.
sender
.
send
(
"chat:response:chunk"
,
{
chatId
:
req
.
chatId
,
chatId
:
req
.
chatId
,
messages
:
[
messages
:
currentMessages
,
...
updatedChat
.
messages
,
{
role
:
"assistant"
,
content
:
fullResponse
,
},
],
});
});
// If the stream was aborted, exit early
// If the stream was aborted, exit early
...
@@ -266,14 +286,20 @@ export function registerChatStreamHandlers() {
...
@@ -266,14 +286,20 @@ export function registerChatStreamHandlers() {
// If we have a partial response, save it to the database
// If we have a partial response, save it to the database
if
(
partialResponse
)
{
if
(
partialResponse
)
{
try
{
try
{
// Insert a new assistant message with the partial content
// Update the placeholder assistant message with the partial content and cancellation note
await
db
.
insert
(
messages
).
values
({
await
db
chatId
,
.
update
(
messages
)
role
:
"assistant"
,
.
set
({
content
:
`
${
partialResponse
}
\n\n[Response cancelled by user]`
,
content
:
`
${
partialResponse
}
});
logger
.
log
(
`Saved partial response for chat
${
chatId
}
`
);
[Response cancelled by user]`
,
partialResponses
.
delete
(
chatId
);
})
.
where
(
eq
(
messages
.
id
,
placeholderAssistantMessage
.
id
));
logger
.
log
(
`Updated cancelled response for placeholder message
${
placeholderAssistantMessage
.
id
}
in chat
${
chatId
}
`
);
partialResponses
.
delete
(
req
.
chatId
);
}
catch
(
error
)
{
}
catch
(
error
)
{
logger
.
error
(
logger
.
error
(
`Error saving partial response for chat
${
chatId
}
:`
,
`Error saving partial response for chat
${
chatId
}
:`
,
...
@@ -301,21 +327,17 @@ export function registerChatStreamHandlers() {
...
@@ -301,21 +327,17 @@ export function registerChatStreamHandlers() {
}
}
const
chatSummary
=
chatTitle
?.[
1
];
const
chatSummary
=
chatTitle
?.[
1
];
// Create initial assistant message
// Update the placeholder assistant message with the full response
const
[
assistantMessage
]
=
await
db
await
db
.
insert
(
messages
)
.
update
(
messages
)
.
values
({
.
set
({
content
:
fullResponse
})
chatId
:
req
.
chatId
,
.
where
(
eq
(
messages
.
id
,
placeholderAssistantMessage
.
id
));
role
:
"assistant"
,
content
:
fullResponse
,
})
.
returning
();
if
(
readSettings
().
autoApproveChanges
)
{
if
(
readSettings
().
autoApproveChanges
)
{
const
status
=
await
processFullResponseActions
(
const
status
=
await
processFullResponseActions
(
fullResponse
,
fullResponse
,
req
.
chatId
,
req
.
chatId
,
{
chatSummary
,
messageId
:
assistantMessage
.
id
}
{
chatSummary
,
messageId
:
placeholderAssistantMessage
.
id
}
// Use placeholder ID
);
);
const
chat
=
await
db
.
query
.
chats
.
findFirst
({
const
chat
=
await
db
.
query
.
chats
.
findFirst
({
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论