更新若干功能
This commit is contained in:
16
ai.go
16
ai.go
@@ -160,6 +160,9 @@ func (a *App) SendAIMessage(conversationID, projectID int64, scenario, content s
|
||||
if e != nil {
|
||||
return AIConversation{}, e
|
||||
}
|
||||
if e := a.checkAIPolicy(); e != nil {
|
||||
return AIConversation{}, e
|
||||
}
|
||||
|
||||
var conv AIConversation
|
||||
if conversationID == 0 {
|
||||
@@ -224,7 +227,11 @@ func (a *App) runAIStream(ctx context.Context, provider ai.Provider, convID int6
|
||||
return
|
||||
}
|
||||
var sb strings.Builder
|
||||
var usage *ai.Usage
|
||||
for chunk := range stream {
|
||||
if chunk.Usage != nil {
|
||||
usage = chunk.Usage
|
||||
}
|
||||
if chunk.Err != nil {
|
||||
// 已有部分内容则保留落库,方便用户继续。
|
||||
if sb.Len() > 0 {
|
||||
@@ -236,6 +243,9 @@ func (a *App) runAIStream(ctx context.Context, provider ai.Provider, convID int6
|
||||
a.emit("ai:stream", aiStreamEvent{ConversationID: convID, Error: chunk.Err.Error(), Done: true})
|
||||
return
|
||||
}
|
||||
if chunk.Content == "" {
|
||||
continue
|
||||
}
|
||||
sb.WriteString(chunk.Content)
|
||||
a.emit("ai:stream", aiStreamEvent{ConversationID: convID, Delta: chunk.Content})
|
||||
}
|
||||
@@ -248,6 +258,9 @@ func (a *App) runAIStream(ctx context.Context, provider ai.Provider, convID int6
|
||||
a.emit("ai:stream", aiStreamEvent{ConversationID: convID, Error: e.Error(), Done: true})
|
||||
return
|
||||
}
|
||||
if usage != nil {
|
||||
go a.reportAIUsage(provider.Name(), usage.PromptTokens, usage.CompletionTokens, usage.Estimated)
|
||||
}
|
||||
a.store.TouchAIConversation(convID, provider.Name())
|
||||
a.emit("ai:stream", aiStreamEvent{ConversationID: convID, Done: true, MessageID: m.ID})
|
||||
}
|
||||
@@ -478,6 +491,9 @@ func (a *App) generateAISummaries(projectID int64, kinds ...string) {
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
if e := a.checkAIPolicy(); e != nil {
|
||||
return
|
||||
}
|
||||
st, _ := a.store.Settings()
|
||||
aiSummarySem <- struct{}{}
|
||||
defer func() { <-aiSummarySem }()
|
||||
|
||||
Reference in New Issue
Block a user