feat: migrate call-omo-agent tool callers to SDK message finders
This commit is contained in:
parent
2bf8b15f24
commit
553817c1a0
@ -1,21 +1,38 @@
|
|||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import { findFirstMessageWithAgent, findNearestMessageWithFields } from "../../features/hook-message-injector"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
|
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"
|
import { log } from "../../shared"
|
||||||
import type { CallOmoAgentArgs } from "./types"
|
import type { CallOmoAgentArgs } from "./types"
|
||||||
import type { ToolContextWithMetadata } from "./tool-context-with-metadata"
|
import type { ToolContextWithMetadata } from "./tool-context-with-metadata"
|
||||||
import { getMessageDir } from "./message-storage-directory"
|
import { getMessageDir } from "./message-storage-directory"
|
||||||
import { getSessionTools } from "../../shared/session-tools-store"
|
import { getSessionTools } from "../../shared/session-tools-store"
|
||||||
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
|
|
||||||
export async function executeBackgroundAgent(
|
export async function executeBackgroundAgent(
|
||||||
args: CallOmoAgentArgs,
|
args: CallOmoAgentArgs,
|
||||||
toolContext: ToolContextWithMetadata,
|
toolContext: ToolContextWithMetadata,
|
||||||
manager: BackgroundManager,
|
manager: BackgroundManager,
|
||||||
|
client: PluginInput["client"],
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
try {
|
try {
|
||||||
const messageDir = getMessageDir(toolContext.sessionID)
|
const messageDir = getMessageDir(toolContext.sessionID)
|
||||||
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
|
||||||
const firstMessageAgent = messageDir ? findFirstMessageWithAgent(messageDir) : null
|
const [prevMessage, firstMessageAgent] = isSqliteBackend()
|
||||||
|
? await Promise.all([
|
||||||
|
findNearestMessageWithFieldsFromSDK(client, toolContext.sessionID),
|
||||||
|
findFirstMessageWithAgentFromSDK(client, toolContext.sessionID),
|
||||||
|
])
|
||||||
|
: [
|
||||||
|
messageDir ? findNearestMessageWithFields(messageDir) : null,
|
||||||
|
messageDir ? findFirstMessageWithAgent(messageDir) : null,
|
||||||
|
]
|
||||||
|
|
||||||
const sessionAgent = getSessionAgent(toolContext.sessionID)
|
const sessionAgent = getSessionAgent(toolContext.sessionID)
|
||||||
const parentAgent =
|
const parentAgent =
|
||||||
toolContext.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
toolContext.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 { executeBackground } from "./background-executor"
|
import { executeBackground } from "./background-executor"
|
||||||
|
|
||||||
describe("executeBackground", () => {
|
describe("executeBackground", () => {
|
||||||
|
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("executeBackground", () => {
|
|||||||
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("executeBackground", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const result = await executeBackground(testArgs, testContext, mockManager)
|
const result = await executeBackground(testArgs, testContext, mockManager, mockClient)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(result).toContain("Task failed to start")
|
expect(result).toContain("Task failed to start")
|
||||||
|
|||||||
@ -1,11 +1,18 @@
|
|||||||
import type { CallOmoAgentArgs } from "./types"
|
import type { CallOmoAgentArgs } from "./types"
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import { log } from "../../shared"
|
import { log } from "../../shared"
|
||||||
import { consumeNewMessages } from "../../shared/session-cursor"
|
import { consumeNewMessages } from "../../shared/session-cursor"
|
||||||
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 { getMessageDir } from "./message-dir"
|
import { getMessageDir } from "./message-dir"
|
||||||
import { getSessionTools } from "../../shared/session-tools-store"
|
import { getSessionTools } from "../../shared/session-tools-store"
|
||||||
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
|
|
||||||
export async function executeBackground(
|
export async function executeBackground(
|
||||||
args: CallOmoAgentArgs,
|
args: CallOmoAgentArgs,
|
||||||
@ -16,12 +23,22 @@ export async function executeBackground(
|
|||||||
abort: AbortSignal
|
abort: AbortSignal
|
||||||
metadata?: (input: { title?: string; metadata?: Record<string, unknown> }) => void
|
metadata?: (input: { title?: string; metadata?: Record<string, unknown> }) => void
|
||||||
},
|
},
|
||||||
manager: BackgroundManager
|
manager: BackgroundManager,
|
||||||
|
client: PluginInput["client"]
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
try {
|
try {
|
||||||
const messageDir = getMessageDir(toolContext.sessionID)
|
const messageDir = getMessageDir(toolContext.sessionID)
|
||||||
const prevMessage = messageDir ? findNearestMessageWithFields(messageDir) : null
|
|
||||||
const firstMessageAgent = messageDir ? findFirstMessageWithAgent(messageDir) : null
|
const [prevMessage, firstMessageAgent] = isSqliteBackend()
|
||||||
|
? await Promise.all([
|
||||||
|
findNearestMessageWithFieldsFromSDK(client, toolContext.sessionID),
|
||||||
|
findFirstMessageWithAgentFromSDK(client, toolContext.sessionID),
|
||||||
|
])
|
||||||
|
: [
|
||||||
|
messageDir ? findNearestMessageWithFields(messageDir) : null,
|
||||||
|
messageDir ? findFirstMessageWithAgent(messageDir) : null,
|
||||||
|
]
|
||||||
|
|
||||||
const sessionAgent = getSessionAgent(toolContext.sessionID)
|
const sessionAgent = getSessionAgent(toolContext.sessionID)
|
||||||
const parentAgent = toolContext.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
const parentAgent = toolContext.agent ?? sessionAgent ?? firstMessageAgent ?? prevMessage?.agent
|
||||||
|
|
||||||
|
|||||||
@ -48,7 +48,7 @@ export function createCallOmoAgent(
|
|||||||
if (args.session_id) {
|
if (args.session_id) {
|
||||||
return `Error: session_id is not supported in background mode. Use run_in_background=false to continue an existing session.`
|
return `Error: session_id is not supported in background mode. Use run_in_background=false to continue an existing session.`
|
||||||
}
|
}
|
||||||
return await executeBackground(args, toolCtx, backgroundManager)
|
return await executeBackground(args, toolCtx, backgroundManager, ctx.client)
|
||||||
}
|
}
|
||||||
|
|
||||||
return await executeSync(args, toolCtx, ctx)
|
return await executeSync(args, toolCtx, ctx)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user