|
|
@@ -248,7 +248,7 @@ export namespace Session {
|
|
|
if (
|
|
|
model.info.limit.context &&
|
|
|
tokens >
|
|
|
- (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9
|
|
|
+ (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9
|
|
|
) {
|
|
|
await summarize({
|
|
|
sessionID: input.sessionID,
|
|
|
@@ -295,7 +295,7 @@ export namespace Session {
|
|
|
draft.title = result.text
|
|
|
})
|
|
|
})
|
|
|
- .catch(() => {})
|
|
|
+ .catch(() => { })
|
|
|
}
|
|
|
const msg: Message.Info = {
|
|
|
role: "user",
|
|
|
@@ -572,7 +572,7 @@ export namespace Session {
|
|
|
case "tool-call": {
|
|
|
const [match] = next.parts.flatMap((p) =>
|
|
|
p.type === "tool-invocation" &&
|
|
|
- p.toolInvocation.toolCallId === value.toolCallId
|
|
|
+ p.toolInvocation.toolCallId === value.toolCallId
|
|
|
? [p]
|
|
|
: [],
|
|
|
)
|
|
|
@@ -736,7 +736,9 @@ export namespace Session {
|
|
|
},
|
|
|
}
|
|
|
await updateMessage(next)
|
|
|
- const result = await generateText({
|
|
|
+
|
|
|
+ let text: Message.TextPart | undefined
|
|
|
+ const result = streamText({
|
|
|
abortSignal: abort.signal,
|
|
|
model: model.language,
|
|
|
messages: [
|
|
|
@@ -757,16 +759,46 @@ export namespace Session {
|
|
|
],
|
|
|
},
|
|
|
],
|
|
|
+ onStepFinish: async (step) => {
|
|
|
+ const assistant = next.metadata!.assistant!
|
|
|
+ const usage = getUsage(model.info, step.usage, step.providerMetadata)
|
|
|
+ assistant.cost += usage.cost
|
|
|
+ assistant.tokens = usage.tokens
|
|
|
+ await updateMessage(next)
|
|
|
+ if (text) {
|
|
|
+ Bus.publish(Message.Event.PartUpdated, {
|
|
|
+ part: text,
|
|
|
+ messageID: next.id,
|
|
|
+ sessionID: next.metadata.sessionID,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ text = undefined
|
|
|
+ },
|
|
|
+ async onFinish(input) {
|
|
|
+ const assistant = next.metadata!.assistant!
|
|
|
+ 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)
|
|
|
+ },
|
|
|
})
|
|
|
- next.parts.push({
|
|
|
- type: "text",
|
|
|
- text: result.text,
|
|
|
- })
|
|
|
- const assistant = next.metadata!.assistant!
|
|
|
- const usage = getUsage(model.info, result.usage, result.providerMetadata)
|
|
|
- assistant.cost = usage.cost
|
|
|
- assistant.tokens = usage.tokens
|
|
|
- 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
|
|
|
+
|
|
|
+ await updateMessage(next)
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
function lock(sessionID: string) {
|