Skip to content

Commit 28d8af4

Browse files
committed
add parent id to assistant messages
1 parent 10ff6e9 commit 28d8af4

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

packages/opencode/src/session/compaction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export namespace SessionCompaction {
109109
const msg = (await Session.updateMessage({
110110
id: Identifier.ascending("message"),
111111
role: "assistant",
112+
parentID: toSummarize.findLast((m) => m.info.role === "user")?.info.id!,
112113
sessionID: input.sessionID,
113114
system,
114115
mode: "build",

packages/opencode/src/session/message-v2.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export namespace MessageV2 {
278278
])
279279
.optional(),
280280
system: z.string().array(),
281+
parentID: z.string(),
281282
modelID: z.string(),
282283
providerID: z.string(),
283284
mode: z.string(),
@@ -346,6 +347,7 @@ export namespace MessageV2 {
346347
if (v1.role === "assistant") {
347348
const info: Assistant = {
348349
id: v1.id,
350+
parentID: "",
349351
sessionID: v1.metadata.sessionID,
350352
role: "assistant",
351353
time: {

packages/opencode/src/session/prompt.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export namespace SessionPrompt {
235235
modelID: model.info.id,
236236
})
237237
step++
238-
await processor.next()
238+
await processor.next(msgs.findLast((m) => m.info.role === "user")?.info.id!)
239239
await using _ = defer(async () => {
240240
await processor.end()
241241
})
@@ -900,9 +900,10 @@ export namespace SessionPrompt {
900900
let snapshot: string | undefined
901901
let blocked = false
902902

903-
async function createMessage() {
903+
async function createMessage(parentID: string) {
904904
const msg: MessageV2.Info = {
905905
id: Identifier.ascending("message"),
906+
parentID,
906907
role: "assistant",
907908
system: input.system,
908909
mode: input.agent,
@@ -938,11 +939,11 @@ export namespace SessionPrompt {
938939
assistantMsg = undefined
939940
}
940941
},
941-
async next() {
942+
async next(parentID: string) {
942943
if (assistantMsg) {
943944
throw new Error("end previous assistant message first")
944945
}
945-
assistantMsg = await createMessage()
946+
assistantMsg = await createMessage(parentID)
946947
return assistantMsg
947948
},
948949
get message() {
@@ -1424,6 +1425,7 @@ export namespace SessionPrompt {
14241425
const msg: MessageV2.Assistant = {
14251426
id: Identifier.ascending("message"),
14261427
sessionID: input.sessionID,
1428+
parentID: userMsg.id,
14271429
system: [],
14281430
mode: input.agent,
14291431
cost: 0,
@@ -1696,6 +1698,7 @@ export namespace SessionPrompt {
16961698
const assistantMsg: MessageV2.Assistant = {
16971699
id: Identifier.ascending("message"),
16981700
sessionID: input.sessionID,
1701+
parentID: userMsg.id,
16991702
system: [],
17001703
mode: agentName,
17011704
cost: 0,

0 commit comments

Comments
 (0)