refactor: fix tool call state handling and clean up imports (#21709)
This commit is contained in:
@@ -121,7 +121,6 @@ export const SessionRoutes = lazy(() =>
|
|||||||
),
|
),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const sessionID = c.req.valid("param").sessionID
|
const sessionID = c.req.valid("param").sessionID
|
||||||
log.info("SEARCH", { url: c.req.url })
|
|
||||||
const session = await Session.get(sessionID)
|
const session = await Session.get(sessionID)
|
||||||
return c.json(session)
|
return c.json(session)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { Installation } from "../installation"
|
|||||||
import { Database, NotFoundError, eq, and, gte, isNull, desc, like, inArray, lt } from "../storage/db"
|
import { Database, NotFoundError, eq, and, gte, isNull, desc, like, inArray, lt } from "../storage/db"
|
||||||
import { SyncEvent } from "../sync"
|
import { SyncEvent } from "../sync"
|
||||||
import type { SQL } from "../storage/db"
|
import type { SQL } from "../storage/db"
|
||||||
import { SessionTable } from "./session.sql"
|
import { PartTable, SessionTable } from "./session.sql"
|
||||||
import { ProjectTable } from "../project/project.sql"
|
import { ProjectTable } from "../project/project.sql"
|
||||||
import { Storage } from "@/storage/storage"
|
import { Storage } from "@/storage/storage"
|
||||||
import { Log } from "../util/log"
|
import { Log } from "../util/log"
|
||||||
@@ -345,6 +345,11 @@ export namespace Session {
|
|||||||
messageID: MessageID
|
messageID: MessageID
|
||||||
partID: PartID
|
partID: PartID
|
||||||
}) => Effect.Effect<PartID>
|
}) => Effect.Effect<PartID>
|
||||||
|
readonly getPart: (input: {
|
||||||
|
sessionID: SessionID
|
||||||
|
messageID: MessageID
|
||||||
|
partID: PartID
|
||||||
|
}) => Effect.Effect<MessageV2.Part | undefined>
|
||||||
readonly updatePart: <T extends MessageV2.Part>(part: T) => Effect.Effect<T>
|
readonly updatePart: <T extends MessageV2.Part>(part: T) => Effect.Effect<T>
|
||||||
readonly updatePartDelta: (input: {
|
readonly updatePartDelta: (input: {
|
||||||
sessionID: SessionID
|
sessionID: SessionID
|
||||||
@@ -492,6 +497,29 @@ export namespace Session {
|
|||||||
return part
|
return part
|
||||||
}).pipe(Effect.withSpan("Session.updatePart"))
|
}).pipe(Effect.withSpan("Session.updatePart"))
|
||||||
|
|
||||||
|
const getPart: Interface["getPart"] = Effect.fn("Session.getPart")(function* (input) {
|
||||||
|
const row = Database.use((db) =>
|
||||||
|
db
|
||||||
|
.select()
|
||||||
|
.from(PartTable)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(PartTable.session_id, input.sessionID),
|
||||||
|
eq(PartTable.message_id, input.messageID),
|
||||||
|
eq(PartTable.id, input.partID),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.get(),
|
||||||
|
)
|
||||||
|
if (!row) return
|
||||||
|
return {
|
||||||
|
...row.data,
|
||||||
|
id: row.id,
|
||||||
|
sessionID: row.session_id,
|
||||||
|
messageID: row.message_id,
|
||||||
|
} as MessageV2.Part
|
||||||
|
})
|
||||||
|
|
||||||
const create = Effect.fn("Session.create")(function* (input?: {
|
const create = Effect.fn("Session.create")(function* (input?: {
|
||||||
parentID?: SessionID
|
parentID?: SessionID
|
||||||
title?: string
|
title?: string
|
||||||
@@ -675,6 +703,7 @@ export namespace Session {
|
|||||||
removeMessage,
|
removeMessage,
|
||||||
removePart,
|
removePart,
|
||||||
updatePart,
|
updatePart,
|
||||||
|
getPart,
|
||||||
updatePartDelta,
|
updatePartDelta,
|
||||||
initialize,
|
initialize,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -176,12 +176,22 @@ export namespace SessionProcessor {
|
|||||||
if (ctx.assistantMessage.summary) {
|
if (ctx.assistantMessage.summary) {
|
||||||
throw new Error(`Tool call not allowed while generating summary: ${value.toolName}`)
|
throw new Error(`Tool call not allowed while generating summary: ${value.toolName}`)
|
||||||
}
|
}
|
||||||
const match = ctx.toolcalls[value.toolCallId]
|
const pointer = ctx.toolcalls[value.toolCallId]
|
||||||
if (!match) return
|
const match = yield* session.getPart({
|
||||||
|
partID: pointer.id,
|
||||||
|
messageID: pointer.messageID,
|
||||||
|
sessionID: pointer.sessionID,
|
||||||
|
})
|
||||||
|
if (!match || match.type !== "tool") return
|
||||||
ctx.toolcalls[value.toolCallId] = yield* session.updatePart({
|
ctx.toolcalls[value.toolCallId] = yield* session.updatePart({
|
||||||
...match,
|
...match,
|
||||||
tool: value.toolName,
|
tool: value.toolName,
|
||||||
state: { status: "running", input: value.input, time: { start: Date.now() } },
|
state: {
|
||||||
|
...match.state,
|
||||||
|
status: "running",
|
||||||
|
input: value.input,
|
||||||
|
time: { start: Date.now() },
|
||||||
|
},
|
||||||
metadata: match.metadata?.providerExecuted
|
metadata: match.metadata?.providerExecuted
|
||||||
? { ...value.providerMetadata, providerExecuted: true }
|
? { ...value.providerMetadata, providerExecuted: true }
|
||||||
: value.providerMetadata,
|
: value.providerMetadata,
|
||||||
@@ -237,6 +247,7 @@ export namespace SessionProcessor {
|
|||||||
case "tool-error": {
|
case "tool-error": {
|
||||||
const match = ctx.toolcalls[value.toolCallId]
|
const match = ctx.toolcalls[value.toolCallId]
|
||||||
if (!match || match.state.status !== "running") return
|
if (!match || match.state.status !== "running") return
|
||||||
|
|
||||||
yield* session.updatePart({
|
yield* session.updatePart({
|
||||||
...match,
|
...match,
|
||||||
state: {
|
state: {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { SessionPrompt } from "../session/prompt"
|
|||||||
import { Config } from "../config/config"
|
import { Config } from "../config/config"
|
||||||
import { Permission } from "@/permission"
|
import { Permission } from "@/permission"
|
||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
|
import { Log } from "@/util/log"
|
||||||
|
|
||||||
const id = "task"
|
const id = "task"
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import { createOpencodeServer } from "./server.js"
|
|||||||
import type { ServerOptions } from "./server.js"
|
import type { ServerOptions } from "./server.js"
|
||||||
|
|
||||||
export * as data from "./data.js"
|
export * as data from "./data.js"
|
||||||
import * as data from "./data.js"
|
|
||||||
|
|
||||||
export async function createOpencode(options?: ServerOptions) {
|
export async function createOpencode(options?: ServerOptions) {
|
||||||
const server = await createOpencodeServer({
|
const server = await createOpencodeServer({
|
||||||
|
|||||||
Reference in New Issue
Block a user