Skip to content

Commit bd53b65

Browse files
authored
refactor: fix tool call state handling and clean up imports (#21709)
1 parent 46da801 commit bd53b65

5 files changed

Lines changed: 45 additions & 6 deletions

File tree

packages/opencode/src/server/routes/session.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ export const SessionRoutes = lazy(() =>
121121
),
122122
async (c) => {
123123
const sessionID = c.req.valid("param").sessionID
124-
log.info("SEARCH", { url: c.req.url })
125124
const session = await Session.get(sessionID)
126125
return c.json(session)
127126
},

packages/opencode/src/session/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Installation } from "../installation"
1212
import { Database, NotFoundError, eq, and, gte, isNull, desc, like, inArray, lt } from "../storage/db"
1313
import { SyncEvent } from "../sync"
1414
import type { SQL } from "../storage/db"
15-
import { SessionTable } from "./session.sql"
15+
import { PartTable, SessionTable } from "./session.sql"
1616
import { ProjectTable } from "../project/project.sql"
1717
import { Storage } from "@/storage/storage"
1818
import { Log } from "../util/log"
@@ -345,6 +345,11 @@ export namespace Session {
345345
messageID: MessageID
346346
partID: PartID
347347
}) => Effect.Effect<PartID>
348+
readonly getPart: (input: {
349+
sessionID: SessionID
350+
messageID: MessageID
351+
partID: PartID
352+
}) => Effect.Effect<MessageV2.Part | undefined>
348353
readonly updatePart: <T extends MessageV2.Part>(part: T) => Effect.Effect<T>
349354
readonly updatePartDelta: (input: {
350355
sessionID: SessionID
@@ -492,6 +497,29 @@ export namespace Session {
492497
return part
493498
}).pipe(Effect.withSpan("Session.updatePart"))
494499

500+
const getPart: Interface["getPart"] = Effect.fn("Session.getPart")(function* (input) {
501+
const row = Database.use((db) =>
502+
db
503+
.select()
504+
.from(PartTable)
505+
.where(
506+
and(
507+
eq(PartTable.session_id, input.sessionID),
508+
eq(PartTable.message_id, input.messageID),
509+
eq(PartTable.id, input.partID),
510+
),
511+
)
512+
.get(),
513+
)
514+
if (!row) return
515+
return {
516+
...row.data,
517+
id: row.id,
518+
sessionID: row.session_id,
519+
messageID: row.message_id,
520+
} as MessageV2.Part
521+
})
522+
495523
const create = Effect.fn("Session.create")(function* (input?: {
496524
parentID?: SessionID
497525
title?: string
@@ -675,6 +703,7 @@ export namespace Session {
675703
removeMessage,
676704
removePart,
677705
updatePart,
706+
getPart,
678707
updatePartDelta,
679708
initialize,
680709
})

packages/opencode/src/session/processor.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,22 @@ export namespace SessionProcessor {
176176
if (ctx.assistantMessage.summary) {
177177
throw new Error(`Tool call not allowed while generating summary: ${value.toolName}`)
178178
}
179-
const match = ctx.toolcalls[value.toolCallId]
180-
if (!match) return
179+
const pointer = ctx.toolcalls[value.toolCallId]
180+
const match = yield* session.getPart({
181+
partID: pointer.id,
182+
messageID: pointer.messageID,
183+
sessionID: pointer.sessionID,
184+
})
185+
if (!match || match.type !== "tool") return
181186
ctx.toolcalls[value.toolCallId] = yield* session.updatePart({
182187
...match,
183188
tool: value.toolName,
184-
state: { status: "running", input: value.input, time: { start: Date.now() } },
189+
state: {
190+
...match.state,
191+
status: "running",
192+
input: value.input,
193+
time: { start: Date.now() },
194+
},
185195
metadata: match.metadata?.providerExecuted
186196
? { ...value.providerMetadata, providerExecuted: true }
187197
: value.providerMetadata,
@@ -237,6 +247,7 @@ export namespace SessionProcessor {
237247
case "tool-error": {
238248
const match = ctx.toolcalls[value.toolCallId]
239249
if (!match || match.state.status !== "running") return
250+
240251
yield* session.updatePart({
241252
...match,
242253
state: {

packages/opencode/src/tool/task.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SessionPrompt } from "../session/prompt"
99
import { Config } from "../config/config"
1010
import { Permission } from "@/permission"
1111
import { Effect } from "effect"
12+
import { Log } from "@/util/log"
1213

1314
const id = "task"
1415

packages/sdk/js/src/v2/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { createOpencodeServer } from "./server.js"
66
import type { ServerOptions } from "./server.js"
77

88
export * as data from "./data.js"
9-
import * as data from "./data.js"
109

1110
export async function createOpencode(options?: ServerOptions) {
1211
const server = await createOpencodeServer({

0 commit comments

Comments
 (0)