mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 14:22:27 +00:00
ignore: diff stuff
This commit is contained in:
@@ -406,47 +406,4 @@ export namespace Session {
|
||||
await Project.setInitialized(Instance.project.id)
|
||||
},
|
||||
)
|
||||
|
||||
export const diff = fn(
|
||||
z.object({
|
||||
sessionID: Identifier.schema("session"),
|
||||
messageID: Identifier.schema("message").optional(),
|
||||
}),
|
||||
async (input) => {
|
||||
const all = await messages(input.sessionID)
|
||||
const index = !input.messageID ? 0 : all.findIndex((x) => x.info.id === input.messageID)
|
||||
if (index === -1) return []
|
||||
|
||||
let from: string | undefined
|
||||
let to: string | undefined
|
||||
|
||||
// scan assistant messages to find earliest from and latest to
|
||||
// snapshot
|
||||
for (let i = index + 1; i < all.length; i++) {
|
||||
const item = all[i]
|
||||
|
||||
// if messageID is provided, stop at the next user message
|
||||
if (input.messageID && item.info.role === "user") break
|
||||
|
||||
if (!from) {
|
||||
for (const part of item.parts) {
|
||||
if (part.type === "step-start" && part.snapshot) {
|
||||
from = part.snapshot
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const part of item.parts) {
|
||||
if (part.type === "step-finish" && part.snapshot) {
|
||||
to = part.snapshot
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (from && to) return Snapshot.diffFull(from, to)
|
||||
return []
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -398,11 +398,6 @@ export namespace SessionPrompt {
|
||||
}
|
||||
state().queued.delete(input.sessionID)
|
||||
SessionCompaction.prune(input)
|
||||
MessageSummary.summarize({
|
||||
sessionID: input.sessionID,
|
||||
messageID: result.info.parentID,
|
||||
providerID: model.providerID,
|
||||
})
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1297,6 +1292,11 @@ export namespace SessionPrompt {
|
||||
}
|
||||
snapshot = undefined
|
||||
}
|
||||
MessageSummary.summarize({
|
||||
sessionID: input.sessionID,
|
||||
messageID: assistantMsg.parentID,
|
||||
providerID: assistantMsg.modelID,
|
||||
})
|
||||
break
|
||||
|
||||
case "text-start":
|
||||
|
||||
@@ -5,6 +5,8 @@ import { Session } from "."
|
||||
import { generateText } from "ai"
|
||||
import { MessageV2 } from "./message-v2"
|
||||
import { Flag } from "@/flag/flag"
|
||||
import { Identifier } from "@/id/id"
|
||||
import { Snapshot } from "@/snapshot"
|
||||
|
||||
export namespace MessageSummary {
|
||||
export const summarize = fn(
|
||||
@@ -14,37 +16,90 @@ export namespace MessageSummary {
|
||||
providerID: z.string(),
|
||||
}),
|
||||
async (input) => {
|
||||
if (!Flag.OPENCODE_EXPERIMENTAL_TURN_SUMMARY) return
|
||||
const messages = await Session.messages(input.sessionID).then((msgs) =>
|
||||
msgs.filter(
|
||||
(m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
|
||||
),
|
||||
)
|
||||
const small = await Provider.getSmallModel(input.providerID)
|
||||
if (!small) return
|
||||
|
||||
const result = await generateText({
|
||||
model: small.language,
|
||||
maxOutputTokens: 100,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
const userMsg = messages.find((m) => m.info.id === input.messageID)!
|
||||
const diffs = await computeDiff({ messages })
|
||||
userMsg.info.summary = {
|
||||
diffs,
|
||||
text: "",
|
||||
}
|
||||
if (
|
||||
Flag.OPENCODE_EXPERIMENTAL_TURN_SUMMARY &&
|
||||
messages.every((m) => m.info.role !== "assistant" || m.info.time.completed)
|
||||
) {
|
||||
const small = await Provider.getSmallModel(input.providerID)
|
||||
if (!small) return
|
||||
const result = await generateText({
|
||||
model: small.language,
|
||||
maxOutputTokens: 100,
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: `
|
||||
Summarize the following conversation into 2 sentences MAX explaining what the assistant did and why. Do not explain the user's input.
|
||||
<conversation>
|
||||
${JSON.stringify(MessageV2.toModelMessage(messages))}
|
||||
</conversation>
|
||||
`,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const userMsg = messages.find((m) => m.info.id === input.messageID)!
|
||||
userMsg.info.summary = {
|
||||
text: result.text,
|
||||
diffs: [],
|
||||
},
|
||||
],
|
||||
})
|
||||
userMsg.info.summary = {
|
||||
text: result.text,
|
||||
diffs: [],
|
||||
}
|
||||
}
|
||||
await Session.updateMessage(userMsg.info)
|
||||
},
|
||||
)
|
||||
|
||||
export const diff = fn(
|
||||
z.object({
|
||||
sessionID: Identifier.schema("session"),
|
||||
messageID: Identifier.schema("message").optional(),
|
||||
}),
|
||||
async (input) => {
|
||||
let all = await Session.messages(input.sessionID)
|
||||
if (input.messageID)
|
||||
all = all.filter(
|
||||
(x) => x.info.id === input.messageID || (x.info.role === "assistant" && x.info.parentID === input.messageID),
|
||||
)
|
||||
|
||||
return computeDiff({
|
||||
messages: all,
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
async function computeDiff(input: { messages: MessageV2.WithParts[] }) {
|
||||
let from: string | undefined
|
||||
let to: string | undefined
|
||||
|
||||
// scan assistant messages to find earliest from and latest to
|
||||
// snapshot
|
||||
for (const item of input.messages) {
|
||||
if (!from) {
|
||||
for (const part of item.parts) {
|
||||
if (part.type === "step-start" && part.snapshot) {
|
||||
from = part.snapshot
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const part of item.parts) {
|
||||
if (part.type === "step-finish" && part.snapshot) {
|
||||
to = part.snapshot
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (from && to) return Snapshot.diffFull(from, to)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user