|
@@ -8,7 +8,7 @@ import { Flag } from "@/flag/flag"
|
|
|
import { Identifier } from "@/id/id"
|
|
import { Identifier } from "@/id/id"
|
|
|
import { Snapshot } from "@/snapshot"
|
|
import { Snapshot } from "@/snapshot"
|
|
|
|
|
|
|
|
-export namespace MessageSummary {
|
|
|
|
|
|
|
+export namespace SessionSummary {
|
|
|
export const summarize = fn(
|
|
export const summarize = fn(
|
|
|
z.object({
|
|
z.object({
|
|
|
sessionID: z.string(),
|
|
sessionID: z.string(),
|
|
@@ -16,46 +16,62 @@ export namespace MessageSummary {
|
|
|
providerID: z.string(),
|
|
providerID: z.string(),
|
|
|
}),
|
|
}),
|
|
|
async (input) => {
|
|
async (input) => {
|
|
|
- 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 userMsg = messages.find((m) => m.info.id === input.messageID)!
|
|
|
|
|
- const diffs = await computeDiff({ messages })
|
|
|
|
|
- userMsg.info.summary = {
|
|
|
|
|
|
|
+ const all = await Session.messages(input.sessionID)
|
|
|
|
|
+ await Promise.all([
|
|
|
|
|
+ summarizeSession({ sessionID: input.sessionID, messages: all }),
|
|
|
|
|
+ summarizeMessage({ messageID: input.messageID, messages: all }),
|
|
|
|
|
+ ])
|
|
|
|
|
+ },
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ async function summarizeSession(input: { sessionID: string; messages: MessageV2.WithParts[] }) {
|
|
|
|
|
+ const diffs = await computeDiff({ messages: input.messages })
|
|
|
|
|
+ await Session.update(input.sessionID, (draft) => {
|
|
|
|
|
+ draft.summary = {
|
|
|
diffs,
|
|
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: `
|
|
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async function summarizeMessage(input: { messageID: string; messages: MessageV2.WithParts[] }) {
|
|
|
|
|
+ const messages = input.messages.filter(
|
|
|
|
|
+ (m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
|
|
|
|
|
+ )
|
|
|
|
|
+ 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 assistantMsg = messages.find((m) => m.info.role === "assistant")!.info as MessageV2.Assistant
|
|
|
|
|
+ const small = await Provider.getSmallModel(assistantMsg.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.
|
|
Summarize the following conversation into 2 sentences MAX explaining what the assistant did and why. Do not explain the user's input.
|
|
|
<conversation>
|
|
<conversation>
|
|
|
${JSON.stringify(MessageV2.toModelMessage(messages))}
|
|
${JSON.stringify(MessageV2.toModelMessage(messages))}
|
|
|
</conversation>
|
|
</conversation>
|
|
|
`,
|
|
`,
|
|
|
- },
|
|
|
|
|
- ],
|
|
|
|
|
- })
|
|
|
|
|
- userMsg.info.summary = {
|
|
|
|
|
- text: result.text,
|
|
|
|
|
- diffs: [],
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ },
|
|
|
|
|
+ ],
|
|
|
|
|
+ })
|
|
|
|
|
+ userMsg.info.summary = {
|
|
|
|
|
+ text: result.text,
|
|
|
|
|
+ diffs: [],
|
|
|
}
|
|
}
|
|
|
- await Session.updateMessage(userMsg.info)
|
|
|
|
|
- },
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ await Session.updateMessage(userMsg.info)
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
export const diff = fn(
|
|
export const diff = fn(
|
|
|
z.object({
|
|
z.object({
|