mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-07 09:54:56 +01:00
track finish reason
This commit is contained in:
@@ -162,6 +162,7 @@ export namespace MessageV2 {
|
|||||||
|
|
||||||
export const StepFinishPart = PartBase.extend({
|
export const StepFinishPart = PartBase.extend({
|
||||||
type: z.literal("step-finish"),
|
type: z.literal("step-finish"),
|
||||||
|
reason: z.string(),
|
||||||
snapshot: z.string().optional(),
|
snapshot: z.string().optional(),
|
||||||
cost: z.number(),
|
cost: z.number(),
|
||||||
tokens: z.object({
|
tokens: z.object({
|
||||||
|
|||||||
@@ -1270,6 +1270,7 @@ export namespace SessionPrompt {
|
|||||||
assistantMsg.tokens = usage.tokens
|
assistantMsg.tokens = usage.tokens
|
||||||
await Session.updatePart({
|
await Session.updatePart({
|
||||||
id: Identifier.ascending("part"),
|
id: Identifier.ascending("part"),
|
||||||
|
reason: value.finishReason,
|
||||||
snapshot: await Snapshot.track(),
|
snapshot: await Snapshot.track(),
|
||||||
messageID: assistantMsg.id,
|
messageID: assistantMsg.id,
|
||||||
sessionID: assistantMsg.sessionID,
|
sessionID: assistantMsg.sessionID,
|
||||||
|
|||||||
@@ -4,14 +4,16 @@ import z from "zod"
|
|||||||
import { Session } from "."
|
import { Session } from "."
|
||||||
import { generateText, type ModelMessage } from "ai"
|
import { generateText, type ModelMessage } from "ai"
|
||||||
import { MessageV2 } from "./message-v2"
|
import { MessageV2 } from "./message-v2"
|
||||||
import { Flag } from "@/flag/flag"
|
|
||||||
import { Identifier } from "@/id/id"
|
import { Identifier } from "@/id/id"
|
||||||
import { Snapshot } from "@/snapshot"
|
import { Snapshot } from "@/snapshot"
|
||||||
|
|
||||||
import { ProviderTransform } from "@/provider/transform"
|
import { ProviderTransform } from "@/provider/transform"
|
||||||
import { SystemPrompt } from "./system"
|
import { SystemPrompt } from "./system"
|
||||||
|
import { Log } from "@/util/log"
|
||||||
|
|
||||||
export namespace SessionSummary {
|
export namespace SessionSummary {
|
||||||
|
const log = Log.create({ service: "session.summary" })
|
||||||
|
|
||||||
export const summarize = fn(
|
export const summarize = fn(
|
||||||
z.object({
|
z.object({
|
||||||
sessionID: z.string(),
|
sessionID: z.string(),
|
||||||
@@ -53,7 +55,7 @@ export namespace SessionSummary {
|
|||||||
const small = await Provider.getSmallModel(assistantMsg.providerID)
|
const small = await Provider.getSmallModel(assistantMsg.providerID)
|
||||||
if (!small) return
|
if (!small) return
|
||||||
|
|
||||||
const textPart = msgWithParts.parts.find((p) => p.type === "text" && p.synthetic === false) as MessageV2.TextPart
|
const textPart = msgWithParts.parts.find((p) => p.type === "text" && !p.synthetic) as MessageV2.TextPart
|
||||||
if (textPart && !userMsg.summary?.title) {
|
if (textPart && !userMsg.summary?.title) {
|
||||||
const result = await generateText({
|
const result = await generateText({
|
||||||
maxOutputTokens: small.info.reasoning ? 1500 : 20,
|
maxOutputTokens: small.info.reasoning ? 1500 : 20,
|
||||||
@@ -72,19 +74,25 @@ export namespace SessionSummary {
|
|||||||
],
|
],
|
||||||
model: small.language,
|
model: small.language,
|
||||||
})
|
})
|
||||||
|
log.info("title", { title: result.text })
|
||||||
userMsg.summary.title = result.text
|
userMsg.summary.title = result.text
|
||||||
await Session.updateMessage(userMsg)
|
await Session.updateMessage(userMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (messages.every((m) => m.info.role !== "assistant" || m.info.time.completed)) {
|
if (
|
||||||
|
messages.some(
|
||||||
|
(m) =>
|
||||||
|
m.info.role === "assistant" && m.parts.some((p) => p.type === "step-finish" && p.reason !== "tool-calls"),
|
||||||
|
)
|
||||||
|
) {
|
||||||
const result = await generateText({
|
const result = await generateText({
|
||||||
model: small.language,
|
model: small.language,
|
||||||
maxOutputTokens: 100,
|
maxOutputTokens: 50,
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
role: "user",
|
role: "user",
|
||||||
content: `
|
content: `
|
||||||
Summarize the following conversation into 2 sentences MAX explaining what the assistant did and why. Do not explain the user's input.
|
Summarize the following conversation into 2 sentences MAX explaining what the assistant did and why. Do not explain the user's input. Do not speak in the third person about the assistant.
|
||||||
<conversation>
|
<conversation>
|
||||||
${JSON.stringify(MessageV2.toModelMessage(messages))}
|
${JSON.stringify(MessageV2.toModelMessage(messages))}
|
||||||
</conversation>
|
</conversation>
|
||||||
@@ -93,6 +101,7 @@ export namespace SessionSummary {
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
userMsg.summary.body = result.text
|
userMsg.summary.body = result.text
|
||||||
|
log.info("body", { body: result.text })
|
||||||
await Session.updateMessage(userMsg)
|
await Session.updateMessage(userMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user