feat: migrate tool callers to SDK message finders on SQLite backend
This commit is contained in:
parent
553817c1a0
commit
291a3edc71
@ -1,20 +1,32 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
|
|
||||||
import { describe, test, expect, mock } from "bun:test"
|
import { describe, test, expect, mock } from "bun:test"
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { createBackgroundTask } from "./create-background-task"
|
import { createBackgroundTask } from "./create-background-task"
|
||||||
|
|
||||||
describe("createBackgroundTask", () => {
|
describe("createBackgroundTask", () => {
|
||||||
|
const launchMock = mock(() => Promise.resolve({
|
||||||
|
id: "test-task-id",
|
||||||
|
sessionID: null,
|
||||||
|
description: "Test task",
|
||||||
|
agent: "test-agent",
|
||||||
|
status: "pending",
|
||||||
|
}))
|
||||||
|
const getTaskMock = mock()
|
||||||
|
|
||||||
const mockManager = {
|
const mockManager = {
|
||||||
launch: mock(() => Promise.resolve({
|
launch: launchMock,
|
||||||
id: "test-task-id",
|
getTask: getTaskMock,
|
||||||
sessionID: null,
|
|
||||||
description: "Test task",
|
|
||||||
agent: "test-agent",
|
|
||||||
status: "pending",
|
|
||||||
})),
|
|
||||||
getTask: mock(),
|
|
||||||
} as unknown as BackgroundManager
|
} as unknown as BackgroundManager
|
||||||
|
|
||||||
const tool = createBackgroundTask(mockManager)
|
const mockClient = {
|
||||||
|
session: {
|
||||||
|
messages: mock(() => Promise.resolve({ data: [] })),
|
||||||
|
},
|
||||||
|
} as unknown as PluginInput["client"]
|
||||||
|
|
||||||
|
const tool = createBackgroundTask(mockManager, mockClient)
|
||||||
|
|
||||||
const testContext = {
|
const testContext = {
|
||||||
sessionID: "test-session",
|
sessionID: "test-session",
|
||||||
@ -31,14 +43,14 @@ describe("createBackgroundTask", () => {
|
|||||||
|
|
||||||
test("detects interrupted task as failure", async () => {
|
test("detects interrupted task as failure", async () => {
|
||||||
//#given
|
//#given
|
||||||
mockManager.launch.mockResolvedValueOnce({
|
launchMock.mockResolvedValueOnce({
|
||||||
id: "test-task-id",
|
id: "test-task-id",
|
||||||
sessionID: null,
|
sessionID: null,
|
||||||
description: "Test task",
|
description: "Test task",
|
||||||
agent: "test-agent",
|
agent: "test-agent",
|
||||||
status: "pending",
|
status: "pending",
|
||||||
})
|
})
|
||||||
mockManager.getTask.mockReturnValueOnce({
|
getTaskMock.mockReturnValueOnce({
|
||||||
id: "test-task-id",
|
id: "test-task-id",
|
||||||
sessionID: null,
|
sessionID: null,
|
||||||
description: "Test task",
|
description: "Test task",
|
||||||
|
|||||||
@ -1,13 +1,19 @@
|
|||||||
import { tool, type ToolDefinition } from "@opencode-ai/plugin"
|
import { tool, type PluginInput, type ToolDefinition } from "@opencode-ai/plugin"
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import type { BackgroundTaskArgs } from "./types"
|
import type { BackgroundTaskArgs } from "./types"
|
||||||
import { BACKGROUND_TASK_DESCRIPTION } from "./constants"
|
import { BACKGROUND_TASK_DESCRIPTION } from "./constants"
|
||||||
import { findFirstMessageWithAgent, findNearestMessageWithFields } from "../../features/hook-message-injector"
|
import {
|
||||||
|
findFirstMessageWithAgent,
|
||||||
|
findFirstMessageWithAgentFromSDK,
|
||||||
|
findNearestMessageWithFields,
|
||||||
|
findNearestMessageWithFieldsFromSDK,
|
||||||
|
} from "../../features/hook-message-injector"
|
||||||
import { getSessionAgent } from "../../features/claude-code-session-state"
|
import { getSessionAgent } from "../../features/claude-code-session-state"
|
||||||
import { storeToolMetadata } from "../../features/tool-metadata-store"
|
import { storeToolMetadata } from "../../features/tool-metadata-store"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { delay } from "./delay"
|
import { delay } from "./delay"
|
||||||
import { getMessageDir } from "./message-dir"
|
import { getMessageDir } from "./message-dir"
|
||||||
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
|
|
||||||
type ToolContextWithMetadata = {
|
type ToolContextWithMetadata = {
|
||||||
sessionID: string
|
sessionID: string
|
||||||
@ -18,7 +24,10 @@ type ToolContextWithMetadata = {
|
|||||||
callID?: string
|
callID?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createBackgroundTask(manager: BackgroundManager): ToolDefinition {
|
export function createBackgroundTask(
|
||||||
|
manager: BackgroundManager,
|
||||||
|
client: PluginInput["client"]
|
||||||
|
): ToolDefinition {
|
||||||
return tool({
|
return tool({
|
||||||
description: BACKGROUND_TASK_DESCRIPTION,
|
description: BACKGROUND_TASK_DESCRIPTION,
|
||||||
args: {
|
args: {
|
||||||
@ -35,8 +44,17 @@ export function createBackgroundTask(manager: BackgroundManager): ToolDefinition
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const messageDir = getMessageDir(ctx.sessionID)
|
const messageDir = getMessageDir(ctx.sessionID)
|
||||||
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
|
||||||
const firstMessageAgent = messageDir ? findFirstMessageWithAgent(messageDir) : null
|
const [prevMessage, firstMessageAgent] = isSqliteBackend()
|
||||||
|
? await Promise.all([
|
||||||
|
findNearestMessageWithFieldsFromSDK(client, ctx.sessionID),
|
||||||
|
findFirstMessageWithAgentFromSDK(client, ctx.sessionID),
|
||||||
|
])
|
||||||
|
: [
|
||||||
|
messageDir ? findNearestMessageWithFields(messageDir) : null,
|
||||||
|
messageDir ? findFirstMessageWithAgent(messageDir) : null,
|
||||||
|
]
|
||||||
|
|
||||||
const sessionAgent = getSessionAgent(ctx.sessionID)
|
const sessionAgent = getSessionAgent(ctx.sessionID)
|
||||||
const parentAgent = ctx.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
const parentAgent = ctx.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
||||||
|
|
||||||
|
|||||||
@ -1,17 +1,22 @@
|
|||||||
|
/// <reference types="bun-types" />
|
||||||
import { describe, test, expect, mock } from "bun:test"
|
import { describe, test, expect, mock } from "bun:test"
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { executeBackgroundAgent } from "./background-agent-executor"
|
import { executeBackgroundAgent } from "./background-agent-executor"
|
||||||
|
|
||||||
describe("executeBackgroundAgent", () => {
|
describe("executeBackgroundAgent", () => {
|
||||||
|
const launchMock = mock(() => Promise.resolve({
|
||||||
|
id: "test-task-id",
|
||||||
|
sessionID: null,
|
||||||
|
description: "Test task",
|
||||||
|
agent: "test-agent",
|
||||||
|
status: "pending",
|
||||||
|
}))
|
||||||
|
const getTaskMock = mock()
|
||||||
|
|
||||||
const mockManager = {
|
const mockManager = {
|
||||||
launch: mock(() => Promise.resolve({
|
launch: launchMock,
|
||||||
id: "test-task-id",
|
getTask: getTaskMock,
|
||||||
sessionID: null,
|
|
||||||
description: "Test task",
|
|
||||||
agent: "test-agent",
|
|
||||||
status: "pending",
|
|
||||||
})),
|
|
||||||
getTask: mock(),
|
|
||||||
} as unknown as BackgroundManager
|
} as unknown as BackgroundManager
|
||||||
|
|
||||||
const testContext = {
|
const testContext = {
|
||||||
@ -25,18 +30,25 @@ describe("executeBackgroundAgent", () => {
|
|||||||
description: "Test background task",
|
description: "Test background task",
|
||||||
prompt: "Test prompt",
|
prompt: "Test prompt",
|
||||||
subagent_type: "test-agent",
|
subagent_type: "test-agent",
|
||||||
|
run_in_background: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mockClient = {
|
||||||
|
session: {
|
||||||
|
messages: mock(() => Promise.resolve({ data: [] })),
|
||||||
|
},
|
||||||
|
} as unknown as PluginInput["client"]
|
||||||
|
|
||||||
test("detects interrupted task as failure", async () => {
|
test("detects interrupted task as failure", async () => {
|
||||||
//#given
|
//#given
|
||||||
mockManager.launch.mockResolvedValueOnce({
|
launchMock.mockResolvedValueOnce({
|
||||||
id: "test-task-id",
|
id: "test-task-id",
|
||||||
sessionID: null,
|
sessionID: null,
|
||||||
description: "Test task",
|
description: "Test task",
|
||||||
agent: "test-agent",
|
agent: "test-agent",
|
||||||
status: "pending",
|
status: "pending",
|
||||||
})
|
})
|
||||||
mockManager.getTask.mockReturnValueOnce({
|
getTaskMock.mockReturnValueOnce({
|
||||||
id: "test-task-id",
|
id: "test-task-id",
|
||||||
sessionID: null,
|
sessionID: null,
|
||||||
description: "Test task",
|
description: "Test task",
|
||||||
@ -45,7 +57,7 @@ describe("executeBackgroundAgent", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const result = await executeBackgroundAgent(testArgs, testContext, mockManager)
|
const result = await executeBackgroundAgent(testArgs, testContext, mockManager, mockClient)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(result).toContain("Task failed to start")
|
expect(result).toContain("Task failed to start")
|
||||||
|
|||||||
@ -1,14 +1,33 @@
|
|||||||
import type { ToolContextWithMetadata } from "./types"
|
import type { ToolContextWithMetadata } from "./types"
|
||||||
|
import type { OpencodeClient } from "./types"
|
||||||
import type { ParentContext } from "./executor-types"
|
import type { ParentContext } from "./executor-types"
|
||||||
import { findNearestMessageWithFields, findFirstMessageWithAgent } from "../../features/hook-message-injector"
|
import {
|
||||||
|
findFirstMessageWithAgent,
|
||||||
|
findFirstMessageWithAgentFromSDK,
|
||||||
|
findNearestMessageWithFields,
|
||||||
|
findNearestMessageWithFieldsFromSDK,
|
||||||
|
} from "../../features/hook-message-injector"
|
||||||
import { getSessionAgent } from "../../features/claude-code-session-state"
|
import { getSessionAgent } from "../../features/claude-code-session-state"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { getMessageDir } from "../../shared"
|
import { getMessageDir } from "../../shared/opencode-message-dir"
|
||||||
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
|
|
||||||
export function resolveParentContext(ctx: ToolContextWithMetadata): ParentContext {
|
export async function resolveParentContext(
|
||||||
|
ctx: ToolContextWithMetadata,
|
||||||
|
client: OpencodeClient
|
||||||
|
): Promise<ParentContext> {
|
||||||
const messageDir = getMessageDir(ctx.sessionID)
|
const messageDir = getMessageDir(ctx.sessionID)
|
||||||
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
|
||||||
const firstMessageAgent = messageDir ? findFirstMessageWithAgent(messageDir) : null
|
const [prevMessage, firstMessageAgent] = isSqliteBackend()
|
||||||
|
? await Promise.all([
|
||||||
|
findNearestMessageWithFieldsFromSDK(client, ctx.sessionID),
|
||||||
|
findFirstMessageWithAgentFromSDK(client, ctx.sessionID),
|
||||||
|
])
|
||||||
|
: [
|
||||||
|
messageDir ? findNearestMessageWithFields(messageDir) : null,
|
||||||
|
messageDir ? findFirstMessageWithAgent(messageDir) : null,
|
||||||
|
]
|
||||||
|
|
||||||
const sessionAgent = getSessionAgent(ctx.sessionID)
|
const sessionAgent = getSessionAgent(ctx.sessionID)
|
||||||
const parentAgent = ctx.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
const parentAgent = ctx.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
||||||
|
|
||||||
|
|||||||
@ -129,7 +129,7 @@ Prompts MUST be in English.`
|
|||||||
return skillError
|
return skillError
|
||||||
}
|
}
|
||||||
|
|
||||||
const parentContext = resolveParentContext(ctx)
|
const parentContext = await resolveParentContext(ctx, options.client)
|
||||||
|
|
||||||
if (args.session_id) {
|
if (args.session_id) {
|
||||||
if (runInBackground) {
|
if (runInBackground) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user