mirror of
https://github.com/sst/opencode.git
synced 2025-07-07 16:14:59 +00:00
Handle summarize interruption in the backend
This commit is contained in:
parent
c85f23e9fa
commit
7ddc548918
2 changed files with 48 additions and 22 deletions
|
@ -947,26 +947,60 @@ export namespace Session {
|
|||
const usage = getUsage(model.info, input.usage, input.providerMetadata)
|
||||
assistant.cost += usage.cost
|
||||
assistant.tokens = usage.tokens
|
||||
next.metadata!.time.completed = Date.now()
|
||||
await updateMessage(next)
|
||||
},
|
||||
})
|
||||
|
||||
for await (const value of result.fullStream) {
|
||||
switch (value.type) {
|
||||
case "text-delta":
|
||||
if (!text) {
|
||||
text = {
|
||||
type: "text",
|
||||
text: value.textDelta,
|
||||
}
|
||||
next.parts.push(text)
|
||||
} else text.text += value.textDelta
|
||||
try {
|
||||
for await (const value of result.fullStream) {
|
||||
switch (value.type) {
|
||||
case "text-delta":
|
||||
if (!text) {
|
||||
text = {
|
||||
type: "text",
|
||||
text: value.textDelta,
|
||||
}
|
||||
next.parts.push(text)
|
||||
} else text.text += value.textDelta
|
||||
|
||||
await updateMessage(next)
|
||||
break
|
||||
await updateMessage(next)
|
||||
break
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
log.error("summarize stream error", {
|
||||
error: e,
|
||||
})
|
||||
switch (true) {
|
||||
case Message.OutputLengthError.isInstance(e):
|
||||
next.metadata.error = e
|
||||
break
|
||||
case LoadAPIKeyError.isInstance(e):
|
||||
next.metadata.error = new Provider.AuthError(
|
||||
{
|
||||
providerID: input.providerID,
|
||||
message: e.message,
|
||||
},
|
||||
{ cause: e },
|
||||
).toObject()
|
||||
break
|
||||
case e instanceof Error:
|
||||
next.metadata.error = new NamedError.Unknown(
|
||||
{ message: e.toString() },
|
||||
{ cause: e },
|
||||
).toObject()
|
||||
break
|
||||
default:
|
||||
next.metadata.error = new NamedError.Unknown(
|
||||
{ message: JSON.stringify(e) },
|
||||
{ cause: e },
|
||||
)
|
||||
}
|
||||
Bus.publish(Event.Error, {
|
||||
error: next.metadata.error,
|
||||
})
|
||||
}
|
||||
next.metadata!.time.completed = Date.now()
|
||||
await updateMessage(next)
|
||||
}
|
||||
|
||||
function lock(sessionID: string) {
|
||||
|
|
|
@ -389,14 +389,6 @@ func (a *App) Cancel(ctx context.Context, sessionID string) error {
|
|||
if a.compactCancel != nil {
|
||||
a.compactCancel()
|
||||
a.compactCancel = nil
|
||||
|
||||
// Mark any incomplete message as completed when canceling compact
|
||||
if len(a.Messages) > 0 {
|
||||
lastMessage := &a.Messages[len(a.Messages)-1]
|
||||
if lastMessage.Metadata.Time.Completed == 0 {
|
||||
lastMessage.Metadata.Time.Completed = float64(time.Now().Unix())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err := a.Client.Session.Abort(ctx, sessionID)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue