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) => {
|
async (c) => {
|
||||||
const params = c.req.valid("param")
|
const params = c.req.valid("param")
|
||||||
const message = await Session.getMessage({
|
const message = await MessageV2.get({
|
||||||
sessionID: params.id,
|
sessionID: params.id,
|
||||||
messageID: params.messageID,
|
messageID: params.messageID,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ export namespace SessionCompaction {
|
|||||||
draft.time.compacting = undefined
|
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 model = await Provider.getModel(input.providerID, input.modelID)
|
||||||
const system = [
|
const system = [
|
||||||
...SystemPrompt.summarize(model.providerID),
|
...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 {
|
return {
|
||||||
info: msg,
|
info: msg,
|
||||||
parts,
|
parts,
|
||||||
@@ -287,7 +287,7 @@ export namespace SessionCompaction {
|
|||||||
})
|
})
|
||||||
if (result.shouldRetry) {
|
if (result.shouldRetry) {
|
||||||
for (let retry = 1; retry < maxRetries; retry++) {
|
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) {
|
if (lastRetryPart) {
|
||||||
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
||||||
@@ -336,7 +336,7 @@ export namespace SessionCompaction {
|
|||||||
if (
|
if (
|
||||||
!msg.error ||
|
!msg.error ||
|
||||||
(MessageV2.AbortedError.isInstance(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
|
msg.summary = true
|
||||||
Bus.publish(Event.Compacted, {
|
Bus.publish(Event.Compacted, {
|
||||||
|
|||||||
@@ -273,17 +273,6 @@ export namespace Session {
|
|||||||
return diffs ?? []
|
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(
|
export const messages = fn(
|
||||||
z.object({
|
z.object({
|
||||||
sessionID: Identifier.schema("session"),
|
sessionID: Identifier.schema("session"),
|
||||||
@@ -291,7 +280,7 @@ export namespace Session {
|
|||||||
}),
|
}),
|
||||||
async (input) => {
|
async (input) => {
|
||||||
const result = [] as MessageV2.WithParts[]
|
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
|
if (input.limit && result.length >= input.limit) break
|
||||||
result.push(msg)
|
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() {
|
export async function* list() {
|
||||||
const project = Instance.project
|
const project = Instance.project
|
||||||
for (const item of await Storage.list(["session", project.id])) {
|
for (const item of await Storage.list(["session", project.id])) {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import {
|
|||||||
import { Identifier } from "../id/id"
|
import { Identifier } from "../id/id"
|
||||||
import { LSP } from "../lsp"
|
import { LSP } from "../lsp"
|
||||||
import { Snapshot } from "@/snapshot"
|
import { Snapshot } from "@/snapshot"
|
||||||
|
import { fn } from "@/util/fn"
|
||||||
|
import { Storage } from "@/storage/storage"
|
||||||
|
|
||||||
export namespace MessageV2 {
|
export namespace MessageV2 {
|
||||||
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
export const OutputLengthError = NamedError.create("MessageOutputLengthError", z.object({}))
|
||||||
@@ -655,6 +657,39 @@ export namespace MessageV2 {
|
|||||||
return convertToModelMessages(result)
|
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>) {
|
export async function filterCompacted(stream: AsyncIterable<MessageV2.WithParts>) {
|
||||||
const result = [] as MessageV2.WithParts[]
|
const result = [] as MessageV2.WithParts[]
|
||||||
for await (const msg of stream) {
|
for await (const msg of stream) {
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ export namespace SessionPrompt {
|
|||||||
})
|
})
|
||||||
if (result.shouldRetry) {
|
if (result.shouldRetry) {
|
||||||
for (let retry = 1; retry < maxRetries; retry++) {
|
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) {
|
if (lastRetryPart) {
|
||||||
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
|
||||||
@@ -434,7 +434,7 @@ export namespace SessionPrompt {
|
|||||||
providerID: string
|
providerID: string
|
||||||
signal: AbortSignal
|
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")
|
const lastAssistant = msgs.findLast((msg) => msg.info.role === "assistant")
|
||||||
if (
|
if (
|
||||||
lastAssistant?.info.role === "assistant" &&
|
lastAssistant?.info.role === "assistant" &&
|
||||||
@@ -1106,7 +1106,7 @@ export namespace SessionPrompt {
|
|||||||
})
|
})
|
||||||
toolcalls[value.toolCallId] = part as MessageV2.ToolPart
|
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)
|
const lastThree = parts.slice(-DOOM_LOOP_THRESHOLD)
|
||||||
if (
|
if (
|
||||||
lastThree.length === DOOM_LOOP_THRESHOLD &&
|
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) {
|
for (const part of p) {
|
||||||
if (
|
if (
|
||||||
part.type === "tool" &&
|
part.type === "tool" &&
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export const TaskTool = Tool.define("task", async () => {
|
|||||||
parentID: ctx.sessionID,
|
parentID: ctx.sessionID,
|
||||||
title: params.description + ` (@${agent.name} subagent)`,
|
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")
|
if (msg.info.role !== "assistant") throw new Error("Not an assistant message")
|
||||||
|
|
||||||
ctx.metadata({
|
ctx.metadata({
|
||||||
|
|||||||
Reference in New Issue
Block a user