mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 10:14:22 +01:00
fix: update references after moving message functions to MessageV2 namespace
This commit is contained in:
@@ -827,7 +827,7 @@ export namespace Server {
|
||||
),
|
||||
async (c) => {
|
||||
const params = c.req.valid("param")
|
||||
const message = await Session.getMessage({
|
||||
const message = await MessageV2.get({
|
||||
sessionID: params.id,
|
||||
messageID: params.messageID,
|
||||
})
|
||||
|
||||
@@ -111,7 +111,7 @@ export namespace SessionCompaction {
|
||||
draft.time.compacting = undefined
|
||||
})
|
||||
})
|
||||
const toSummarize = await MessageV2.filterCompacted(Session.messageStream(input.sessionID))
|
||||
const toSummarize = await MessageV2.filterCompacted(MessageV2.stream(input.sessionID))
|
||||
const model = await Provider.getModel(input.providerID, input.modelID)
|
||||
const system = [
|
||||
...SystemPrompt.summarize(model.providerID),
|
||||
@@ -270,7 +270,7 @@ export namespace SessionCompaction {
|
||||
}
|
||||
}
|
||||
|
||||
const parts = await Session.getParts(msg.id)
|
||||
const parts = await MessageV2.parts(msg.id)
|
||||
return {
|
||||
info: msg,
|
||||
parts,
|
||||
@@ -287,7 +287,7 @@ export namespace SessionCompaction {
|
||||
})
|
||||
if (result.shouldRetry) {
|
||||
for (let retry = 1; retry < maxRetries; retry++) {
|
||||
const lastRetryPart = result.parts.findLast((p) => p.type === "retry")
|
||||
const lastRetryPart = result.parts.findLast((p): p is MessageV2.RetryPart => p.type === "retry")
|
||||
|
||||
if (lastRetryPart) {
|
||||
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
||||
@@ -336,7 +336,7 @@ export namespace SessionCompaction {
|
||||
if (
|
||||
!msg.error ||
|
||||
(MessageV2.AbortedError.isInstance(msg.error) &&
|
||||
result.parts.some((part) => part.type === "text" && part.text.length > 0))
|
||||
result.parts.some((part): part is MessageV2.TextPart => part.type === "text" && part.text.length > 0))
|
||||
) {
|
||||
msg.summary = true
|
||||
Bus.publish(Event.Compacted, {
|
||||
|
||||
@@ -273,17 +273,6 @@ export namespace Session {
|
||||
return diffs ?? []
|
||||
})
|
||||
|
||||
export const messageStream = fn(Identifier.schema("session"), async function* (sessionID) {
|
||||
const list = await Array.fromAsync(await Storage.list(["message", sessionID]))
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
const read = await Storage.read<MessageV2.Info>(list[i])
|
||||
yield {
|
||||
info: read,
|
||||
parts: await getParts(read.id),
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
export const messages = fn(
|
||||
z.object({
|
||||
sessionID: Identifier.schema("session"),
|
||||
@@ -291,7 +280,7 @@ export namespace Session {
|
||||
}),
|
||||
async (input) => {
|
||||
const result = [] as MessageV2.WithParts[]
|
||||
for await (const msg of messageStream(input.sessionID)) {
|
||||
for await (const msg of MessageV2.stream(input.sessionID)) {
|
||||
if (input.limit && result.length >= input.limit) break
|
||||
result.push(msg)
|
||||
}
|
||||
@@ -300,29 +289,6 @@ export namespace Session {
|
||||
},
|
||||
)
|
||||
|
||||
export const getMessage = fn(
|
||||
z.object({
|
||||
sessionID: Identifier.schema("session"),
|
||||
messageID: Identifier.schema("message"),
|
||||
}),
|
||||
async (input) => {
|
||||
return {
|
||||
info: await Storage.read<MessageV2.Info>(["message", input.sessionID, input.messageID]),
|
||||
parts: await getParts(input.messageID),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
export const getParts = fn(Identifier.schema("message"), async (messageID) => {
|
||||
const result = [] as MessageV2.Part[]
|
||||
for (const item of await Storage.list(["part", messageID])) {
|
||||
const read = await Storage.read<MessageV2.Part>(item)
|
||||
result.push(read)
|
||||
}
|
||||
result.sort((a, b) => (a.id > b.id ? 1 : -1))
|
||||
return result
|
||||
})
|
||||
|
||||
export async function* list() {
|
||||
const project = Instance.project
|
||||
for (const item of await Storage.list(["session", project.id])) {
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
import { Identifier } from "../id/id"
|
||||
import { LSP } from "../lsp"
|
||||
import { Snapshot } from "@/snapshot"
|
||||
import { fn } from "@/util/fn"
|
||||
import { Storage } from "@/storage/storage"
|
||||
|
||||
export namespace MessageV2 {
|
||||
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
||||
@@ -655,6 +657,39 @@ export namespace MessageV2 {
|
||||
return convertToModelMessages(result)
|
||||
}
|
||||
|
||||
export const stream = fn(Identifier.schema("session"), async function* (sessionID) {
|
||||
const list = await Array.fromAsync(await Storage.list(["message", sessionID]))
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
yield await get({
|
||||
sessionID,
|
||||
messageID: list[i][2],
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export const parts = fn(Identifier.schema("message"), async (messageID) => {
|
||||
const result = [] as MessageV2.Part[]
|
||||
for (const item of await Storage.list(["part", messageID])) {
|
||||
const read = await Storage.read<MessageV2.Part>(item)
|
||||
result.push(read)
|
||||
}
|
||||
result.sort((a, b) => (a.id > b.id ? 1 : -1))
|
||||
return result
|
||||
})
|
||||
|
||||
export const get = fn(
|
||||
z.object({
|
||||
sessionID: Identifier.schema("session"),
|
||||
messageID: Identifier.schema("message"),
|
||||
}),
|
||||
async (input) => {
|
||||
return {
|
||||
info: await Storage.read<MessageV2.Info>(["message", input.sessionID, input.messageID]),
|
||||
parts: await parts(input.messageID),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
export async function filterCompacted(stream: AsyncIterable<MessageV2.WithParts>) {
|
||||
const result = [] as MessageV2.WithParts[]
|
||||
for await (const msg of stream) {
|
||||
|
||||
@@ -362,7 +362,7 @@ export namespace SessionPrompt {
|
||||
})
|
||||
if (result.shouldRetry) {
|
||||
for (let retry = 1; retry < maxRetries; retry++) {
|
||||
const lastRetryPart = result.parts.findLast((p) => p.type === "retry")
|
||||
const lastRetryPart = result.parts.findLast((p): p is MessageV2.RetryPart => p.type === "retry")
|
||||
|
||||
if (lastRetryPart) {
|
||||
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
||||
@@ -434,7 +434,7 @@ export namespace SessionPrompt {
|
||||
providerID: string
|
||||
signal: AbortSignal
|
||||
}) {
|
||||
let msgs = await MessageV2.filterCompacted(Session.messageStream(input.sessionID))
|
||||
let msgs = await MessageV2.filterCompacted(MessageV2.stream(input.sessionID))
|
||||
const lastAssistant = msgs.findLast((msg) => msg.info.role === "assistant")
|
||||
if (
|
||||
lastAssistant?.info.role === "assistant" &&
|
||||
@@ -1106,7 +1106,7 @@ export namespace SessionPrompt {
|
||||
})
|
||||
toolcalls[value.toolCallId] = part as MessageV2.ToolPart
|
||||
|
||||
const parts = await Session.getParts(assistantMsg.id)
|
||||
const parts = await MessageV2.parts(assistantMsg.id)
|
||||
const lastThree = parts.slice(-DOOM_LOOP_THRESHOLD)
|
||||
if (
|
||||
lastThree.length === DOOM_LOOP_THRESHOLD &&
|
||||
@@ -1319,7 +1319,7 @@ export namespace SessionPrompt {
|
||||
})
|
||||
}
|
||||
}
|
||||
const p = await Session.getParts(assistantMsg.id)
|
||||
const p = await MessageV2.parts(assistantMsg.id)
|
||||
for (const part of p) {
|
||||
if (
|
||||
part.type === "tool" &&
|
||||
|
||||
@@ -35,7 +35,7 @@ export const TaskTool = Tool.define("task", async () => {
|
||||
parentID: ctx.sessionID,
|
||||
title: params.description + ` (@${agent.name} subagent)`,
|
||||
})
|
||||
const msg = await Session.getMessage({ sessionID: ctx.sessionID, messageID: ctx.messageID })
|
||||
const msg = await MessageV2.get({ sessionID: ctx.sessionID, messageID: ctx.messageID })
|
||||
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
||||
|
||||
ctx.metadata({
|
||||
|
||||
Reference in New Issue
Block a user