Merge pull request #1953 from maximharizanov/fix/copilot-initiator-attribution
fix(copilot): mark internal hook injections as agent-initiated
This commit is contained in:
commit
6ec6642e13
@ -13,6 +13,7 @@ import {
|
|||||||
normalizeSDKResponse,
|
normalizeSDKResponse,
|
||||||
promptWithModelSuggestionRetry,
|
promptWithModelSuggestionRetry,
|
||||||
resolveInheritedPromptTools,
|
resolveInheritedPromptTools,
|
||||||
|
createInternalAgentTextPart,
|
||||||
} from "../../shared"
|
} from "../../shared"
|
||||||
import { setSessionTools } from "../../shared/session-tools-store"
|
import { setSessionTools } from "../../shared/session-tools-store"
|
||||||
import { ConcurrencyManager } from "./concurrency"
|
import { ConcurrencyManager } from "./concurrency"
|
||||||
@ -1311,7 +1312,7 @@ Use \`background_output(task_id="${task.id}")\` to retrieve this result when rea
|
|||||||
...(agent !== undefined ? { agent } : {}),
|
...(agent !== undefined ? { agent } : {}),
|
||||||
...(model !== undefined ? { model } : {}),
|
...(model !== undefined ? { model } : {}),
|
||||||
...(tools ? { tools } : {}),
|
...(tools ? { tools } : {}),
|
||||||
parts: [{ type: "text", text: notification }],
|
parts: [createInternalAgentTextPart(notification)],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
log("[background-agent] Sent notification to parent session:", {
|
log("[background-agent] Sent notification to parent session:", {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import type { BackgroundTask } from "./types"
|
import type { BackgroundTask } from "./types"
|
||||||
import type { ResultHandlerContext } from "./result-handler-context"
|
import type { ResultHandlerContext } from "./result-handler-context"
|
||||||
import { TASK_CLEANUP_DELAY_MS } from "./constants"
|
import { TASK_CLEANUP_DELAY_MS } from "./constants"
|
||||||
import { log } from "../../shared"
|
import { createInternalAgentTextPart, log } from "../../shared"
|
||||||
import { getTaskToastManager } from "../task-toast-manager"
|
import { getTaskToastManager } from "../task-toast-manager"
|
||||||
import { formatDuration } from "./duration-formatter"
|
import { formatDuration } from "./duration-formatter"
|
||||||
import { buildBackgroundTaskNotificationText } from "./background-task-notification-template"
|
import { buildBackgroundTaskNotificationText } from "./background-task-notification-template"
|
||||||
@ -72,7 +72,7 @@ export async function notifyParentSession(
|
|||||||
...(agent !== undefined ? { agent } : {}),
|
...(agent !== undefined ? { agent } : {}),
|
||||||
...(model !== undefined ? { model } : {}),
|
...(model !== undefined ? { model } : {}),
|
||||||
...(tools ? { tools } : {}),
|
...(tools ? { tools } : {}),
|
||||||
parts: [{ type: "text", text: notification }],
|
parts: [createInternalAgentTextPart(notification)],
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { MESSAGE_STORAGE, PART_STORAGE } from "./constants"
|
|||||||
import type { MessageMeta, OriginalMessageContext, TextPart, ToolPermission } from "./types"
|
import type { MessageMeta, OriginalMessageContext, TextPart, ToolPermission } from "./types"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
import { isSqliteBackend } from "../../shared/opencode-storage-detection"
|
||||||
import { normalizeSDKResponse } from "../../shared"
|
import { createInternalAgentTextPart, normalizeSDKResponse } from "../../shared"
|
||||||
|
|
||||||
export interface StoredMessage {
|
export interface StoredMessage {
|
||||||
agent?: string
|
agent?: string
|
||||||
@ -331,7 +331,7 @@ export function injectHookMessage(
|
|||||||
const textPart: TextPart = {
|
const textPart: TextPart = {
|
||||||
id: partID,
|
id: partID,
|
||||||
type: "text",
|
type: "text",
|
||||||
text: hookContent,
|
text: createInternalAgentTextPart(hookContent).text,
|
||||||
synthetic: true,
|
synthetic: true,
|
||||||
time: {
|
time: {
|
||||||
start: now,
|
start: now,
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { resolveInheritedPromptTools } from "../../shared"
|
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||||
import { HOOK_NAME } from "./hook-name"
|
import { HOOK_NAME } from "./hook-name"
|
||||||
import { BOULDER_CONTINUATION_PROMPT } from "./system-reminder-templates"
|
import { BOULDER_CONTINUATION_PROMPT } from "./system-reminder-templates"
|
||||||
import { resolveRecentPromptContextForSession } from "./recent-model-resolver"
|
import { resolveRecentPromptContextForSession } from "./recent-model-resolver"
|
||||||
@ -53,7 +53,7 @@ export async function injectBoulderContinuation(input: {
|
|||||||
agent: agent ?? "atlas",
|
agent: agent ?? "atlas",
|
||||||
...(promptContext.model !== undefined ? { model: promptContext.model } : {}),
|
...(promptContext.model !== undefined ? { model: promptContext.model } : {}),
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
parts: [{ type: "text", text: prompt }],
|
parts: [createInternalAgentTextPart(prompt)],
|
||||||
},
|
},
|
||||||
query: { directory: ctx.directory },
|
query: { directory: ctx.directory },
|
||||||
})
|
})
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { loadClaudeHooksConfig } from "../config"
|
|||||||
import { loadPluginExtendedConfig } from "../config-loader"
|
import { loadPluginExtendedConfig } from "../config-loader"
|
||||||
import { executeStopHooks, type StopContext } from "../stop"
|
import { executeStopHooks, type StopContext } from "../stop"
|
||||||
import type { PluginConfig } from "../types"
|
import type { PluginConfig } from "../types"
|
||||||
import { isHookDisabled, log } from "../../../shared"
|
import { createInternalAgentTextPart, isHookDisabled, log } from "../../../shared"
|
||||||
import {
|
import {
|
||||||
clearSessionHookState,
|
clearSessionHookState,
|
||||||
sessionErrorState,
|
sessionErrorState,
|
||||||
@ -94,7 +94,7 @@ export function createSessionEventHandler(ctx: PluginInput, config: PluginConfig
|
|||||||
.prompt({
|
.prompt({
|
||||||
path: { id: sessionID },
|
path: { id: sessionID },
|
||||||
body: {
|
body: {
|
||||||
parts: [{ type: "text", text: stopResult.injectPrompt }],
|
parts: [createInternalAgentTextPart(stopResult.injectPrompt)],
|
||||||
},
|
},
|
||||||
query: { directory: ctx.directory },
|
query: { directory: ctx.directory },
|
||||||
})
|
})
|
||||||
|
|||||||
@ -3,7 +3,11 @@ import { log } from "../../shared/logger"
|
|||||||
import { findNearestMessageWithFields } from "../../features/hook-message-injector"
|
import { findNearestMessageWithFields } from "../../features/hook-message-injector"
|
||||||
import { getMessageDir } from "./message-storage-directory"
|
import { getMessageDir } from "./message-storage-directory"
|
||||||
import { withTimeout } from "./with-timeout"
|
import { withTimeout } from "./with-timeout"
|
||||||
import { normalizeSDKResponse, resolveInheritedPromptTools } from "../../shared"
|
import {
|
||||||
|
createInternalAgentTextPart,
|
||||||
|
normalizeSDKResponse,
|
||||||
|
resolveInheritedPromptTools,
|
||||||
|
} from "../../shared"
|
||||||
|
|
||||||
type MessageInfo = {
|
type MessageInfo = {
|
||||||
agent?: string
|
agent?: string
|
||||||
@ -64,7 +68,7 @@ export async function injectContinuationPrompt(
|
|||||||
...(agent !== undefined ? { agent } : {}),
|
...(agent !== undefined ? { agent } : {}),
|
||||||
...(model !== undefined ? { model } : {}),
|
...(model !== undefined ? { model } : {}),
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
parts: [{ type: "text", text: options.prompt }],
|
parts: [createInternalAgentTextPart(options.prompt)],
|
||||||
},
|
},
|
||||||
query: { directory: options.directory },
|
query: { directory: options.directory },
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
declare const require: (name: string) => any
|
declare const require: (name: string) => any
|
||||||
const { describe, expect, test } = require("bun:test")
|
const { describe, expect, test } = require("bun:test")
|
||||||
import { extractResumeConfig, resumeSession } from "./resume"
|
import { extractResumeConfig, resumeSession } from "./resume"
|
||||||
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||||
import type { MessageData } from "./types"
|
import type { MessageData } from "./types"
|
||||||
|
|
||||||
describe("session-recovery resume", () => {
|
describe("session-recovery resume", () => {
|
||||||
@ -44,5 +45,8 @@ describe("session-recovery resume", () => {
|
|||||||
// then
|
// then
|
||||||
expect(ok).toBe(true)
|
expect(ok).toBe(true)
|
||||||
expect(promptBody?.tools).toEqual({ question: false, bash: true })
|
expect(promptBody?.tools).toEqual({ question: false, bash: true })
|
||||||
|
expect(Array.isArray(promptBody?.parts)).toBe(true)
|
||||||
|
const firstPart = (promptBody?.parts as Array<{ text?: string }>)?.[0]
|
||||||
|
expect(firstPart?.text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import type { createOpencodeClient } from "@opencode-ai/sdk"
|
import type { createOpencodeClient } from "@opencode-ai/sdk"
|
||||||
import type { MessageData, ResumeConfig } from "./types"
|
import type { MessageData, ResumeConfig } from "./types"
|
||||||
import { resolveInheritedPromptTools } from "../../shared"
|
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||||
|
|
||||||
const RECOVERY_RESUME_TEXT = "[session recovered - continuing previous task]"
|
const RECOVERY_RESUME_TEXT = "[session recovered - continuing previous task]"
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ export async function resumeSession(client: Client, config: ResumeConfig): Promi
|
|||||||
await client.session.promptAsync({
|
await client.session.promptAsync({
|
||||||
path: { id: config.sessionID },
|
path: { id: config.sessionID },
|
||||||
body: {
|
body: {
|
||||||
parts: [{ type: "text", text: RECOVERY_RESUME_TEXT }],
|
parts: [createInternalAgentTextPart(RECOVERY_RESUME_TEXT)],
|
||||||
agent: config.agent,
|
agent: config.agent,
|
||||||
model: config.model,
|
model: config.model,
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
|
|||||||
@ -2,18 +2,26 @@ declare const require: (name: string) => any
|
|||||||
const { describe, expect, test } = require("bun:test")
|
const { describe, expect, test } = require("bun:test")
|
||||||
|
|
||||||
import { injectContinuation } from "./continuation-injection"
|
import { injectContinuation } from "./continuation-injection"
|
||||||
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||||
|
|
||||||
describe("injectContinuation", () => {
|
describe("injectContinuation", () => {
|
||||||
test("inherits tools from resolved message info when reinjecting", async () => {
|
test("inherits tools from resolved message info when reinjecting", async () => {
|
||||||
// given
|
// given
|
||||||
let capturedTools: Record<string, boolean> | undefined
|
let capturedTools: Record<string, boolean> | undefined
|
||||||
|
let capturedText: string | undefined
|
||||||
const ctx = {
|
const ctx = {
|
||||||
directory: "/tmp/test",
|
directory: "/tmp/test",
|
||||||
client: {
|
client: {
|
||||||
session: {
|
session: {
|
||||||
todo: async () => ({ data: [{ id: "1", content: "todo", status: "pending", priority: "high" }] }),
|
todo: async () => ({ data: [{ id: "1", content: "todo", status: "pending", priority: "high" }] }),
|
||||||
promptAsync: async (input: { body: { tools?: Record<string, boolean> } }) => {
|
promptAsync: async (input: {
|
||||||
|
body: {
|
||||||
|
tools?: Record<string, boolean>
|
||||||
|
parts?: Array<{ type: string; text: string }>
|
||||||
|
}
|
||||||
|
}) => {
|
||||||
capturedTools = input.body.tools
|
capturedTools = input.body.tools
|
||||||
|
capturedText = input.body.parts?.[0]?.text
|
||||||
return {}
|
return {}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -37,5 +45,6 @@ describe("injectContinuation", () => {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
expect(capturedTools).toEqual({ question: false, bash: true })
|
expect(capturedTools).toEqual({ question: false, bash: true })
|
||||||
|
expect(capturedText).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
|
|
||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import { normalizeSDKResponse, resolveInheritedPromptTools } from "../../shared"
|
import {
|
||||||
|
createInternalAgentTextPart,
|
||||||
|
normalizeSDKResponse,
|
||||||
|
resolveInheritedPromptTools,
|
||||||
|
} from "../../shared"
|
||||||
import {
|
import {
|
||||||
findNearestMessageWithFields,
|
findNearestMessageWithFields,
|
||||||
findNearestMessageWithFieldsFromSDK,
|
findNearestMessageWithFieldsFromSDK,
|
||||||
@ -151,7 +155,7 @@ ${todoList}`
|
|||||||
agent: agentName,
|
agent: agentName,
|
||||||
...(model !== undefined ? { model } : {}),
|
...(model !== undefined ? { model } : {}),
|
||||||
...(inheritedTools ? { tools: inheritedTools } : {}),
|
...(inheritedTools ? { tools: inheritedTools } : {}),
|
||||||
parts: [{ type: "text", text: prompt }],
|
parts: [createInternalAgentTextPart(prompt)],
|
||||||
},
|
},
|
||||||
query: { directory: ctx.directory },
|
query: { directory: ctx.directory },
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { afterEach, describe, expect, test } from "bun:test"
|
||||||
import { _resetForTesting, setMainSession } from "../../features/claude-code-session-state"
|
import { _resetForTesting, setMainSession } from "../../features/claude-code-session-state"
|
||||||
import type { BackgroundTask } from "../../features/background-agent"
|
import type { BackgroundTask } from "../../features/background-agent"
|
||||||
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../../shared/internal-initiator-marker"
|
||||||
import { createUnstableAgentBabysitterHook } from "./index"
|
import { createUnstableAgentBabysitterHook } from "./index"
|
||||||
|
|
||||||
const projectDir = process.cwd()
|
const projectDir = process.cwd()
|
||||||
@ -93,6 +94,7 @@ describe("unstable-agent-babysitter hook", () => {
|
|||||||
expect(text).toContain("background_output")
|
expect(text).toContain("background_output")
|
||||||
expect(text).toContain("background_cancel")
|
expect(text).toContain("background_cancel")
|
||||||
expect(text).toContain("deep thought")
|
expect(text).toContain("deep thought")
|
||||||
|
expect(text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("fires reminder for hung minimax task", async () => {
|
test("fires reminder for hung minimax task", async () => {
|
||||||
@ -128,6 +130,7 @@ describe("unstable-agent-babysitter hook", () => {
|
|||||||
expect(text).toContain("background_output")
|
expect(text).toContain("background_output")
|
||||||
expect(text).toContain("background_cancel")
|
expect(text).toContain("background_cancel")
|
||||||
expect(text).toContain("minimax thought")
|
expect(text).toContain("minimax thought")
|
||||||
|
expect(text).toContain(OMO_INTERNAL_INITIATOR_MARKER)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("does not remind stable model tasks", async () => {
|
test("does not remind stable model tasks", async () => {
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import type { BackgroundManager } from "../../features/background-agent"
|
import type { BackgroundManager } from "../../features/background-agent"
|
||||||
import { getMainSessionID, getSessionAgent } from "../../features/claude-code-session-state"
|
import { getMainSessionID, getSessionAgent } from "../../features/claude-code-session-state"
|
||||||
import { log } from "../../shared/logger"
|
import { log } from "../../shared/logger"
|
||||||
import { resolveInheritedPromptTools } from "../../shared"
|
import { createInternalAgentTextPart, resolveInheritedPromptTools } from "../../shared"
|
||||||
import {
|
import {
|
||||||
buildReminder,
|
buildReminder,
|
||||||
extractMessages,
|
extractMessages,
|
||||||
@ -158,7 +158,7 @@ export function createUnstableAgentBabysitterHook(ctx: BabysitterContext, option
|
|||||||
...(agent ? { agent } : {}),
|
...(agent ? { agent } : {}),
|
||||||
...(model ? { model } : {}),
|
...(model ? { model } : {}),
|
||||||
...(tools ? { tools } : {}),
|
...(tools ? { tools } : {}),
|
||||||
parts: [{ type: "text", text: reminder }],
|
parts: [createInternalAgentTextPart(reminder)],
|
||||||
},
|
},
|
||||||
query: { directory: ctx.directory },
|
query: { directory: ctx.directory },
|
||||||
})
|
})
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import type { PluginContext, PluginInterface, ToolsRecord } from "./plugin/types
|
|||||||
import type { OhMyOpenCodeConfig } from "./config"
|
import type { OhMyOpenCodeConfig } from "./config"
|
||||||
|
|
||||||
import { createChatParamsHandler } from "./plugin/chat-params"
|
import { createChatParamsHandler } from "./plugin/chat-params"
|
||||||
|
import { createChatHeadersHandler } from "./plugin/chat-headers"
|
||||||
import { createChatMessageHandler } from "./plugin/chat-message"
|
import { createChatMessageHandler } from "./plugin/chat-message"
|
||||||
import { createMessagesTransformHandler } from "./plugin/messages-transform"
|
import { createMessagesTransformHandler } from "./plugin/messages-transform"
|
||||||
import { createEventHandler } from "./plugin/event"
|
import { createEventHandler } from "./plugin/event"
|
||||||
@ -30,11 +31,13 @@ export function createPluginInterface(args: {
|
|||||||
return {
|
return {
|
||||||
tool: tools,
|
tool: tools,
|
||||||
|
|
||||||
"chat.params": async (input, output) => {
|
"chat.params": async (input: unknown, output: unknown) => {
|
||||||
const handler = createChatParamsHandler({ anthropicEffort: hooks.anthropicEffort })
|
const handler = createChatParamsHandler({ anthropicEffort: hooks.anthropicEffort })
|
||||||
await handler(input, output)
|
await handler(input, output)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"chat.headers": createChatHeadersHandler({ ctx }),
|
||||||
|
|
||||||
"chat.message": createChatMessageHandler({
|
"chat.message": createChatMessageHandler({
|
||||||
ctx,
|
ctx,
|
||||||
pluginConfig,
|
pluginConfig,
|
||||||
|
|||||||
109
src/plugin/chat-headers.test.ts
Normal file
109
src/plugin/chat-headers.test.ts
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
|
||||||
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
|
||||||
|
import { createChatHeadersHandler } from "./chat-headers"
|
||||||
|
|
||||||
|
describe("createChatHeadersHandler", () => {
|
||||||
|
test("sets x-initiator=agent for Copilot internal marker messages", async () => {
|
||||||
|
const handler = createChatHeadersHandler({
|
||||||
|
ctx: {
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
message: async () => ({
|
||||||
|
data: {
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never,
|
||||||
|
})
|
||||||
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
|
await handler(
|
||||||
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
|
provider: { id: "github-copilot" },
|
||||||
|
message: {
|
||||||
|
id: "msg_1",
|
||||||
|
role: "user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(output.headers["x-initiator"]).toBe("agent")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("does not override non-copilot providers", async () => {
|
||||||
|
const handler = createChatHeadersHandler({
|
||||||
|
ctx: {
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
message: async () => ({
|
||||||
|
data: {
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
type: "text",
|
||||||
|
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never,
|
||||||
|
})
|
||||||
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
|
await handler(
|
||||||
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
|
provider: { id: "openai" },
|
||||||
|
message: {
|
||||||
|
id: "msg_1",
|
||||||
|
role: "user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(output.headers["x-initiator"]).toBeUndefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("does not override regular user messages", async () => {
|
||||||
|
const handler = createChatHeadersHandler({
|
||||||
|
ctx: {
|
||||||
|
client: {
|
||||||
|
session: {
|
||||||
|
message: async () => ({
|
||||||
|
data: {
|
||||||
|
parts: [{ type: "text", text: "normal user message" }],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as never,
|
||||||
|
})
|
||||||
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
|
await handler(
|
||||||
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
|
provider: { id: "github-copilot" },
|
||||||
|
message: {
|
||||||
|
id: "msg_1",
|
||||||
|
role: "user",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
output,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(output.headers["x-initiator"]).toBeUndefined()
|
||||||
|
})
|
||||||
|
})
|
||||||
104
src/plugin/chat-headers.ts
Normal file
104
src/plugin/chat-headers.ts
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
|
||||||
|
import type { PluginContext } from "./types"
|
||||||
|
|
||||||
|
type ChatHeadersInput = {
|
||||||
|
sessionID: string
|
||||||
|
provider: { id: string }
|
||||||
|
message: {
|
||||||
|
id?: string
|
||||||
|
role?: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChatHeadersOutput = {
|
||||||
|
headers: Record<string, string>
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
|
return typeof value === "object" && value !== null
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildChatHeadersInput(raw: unknown): ChatHeadersInput | null {
|
||||||
|
if (!isRecord(raw)) return null
|
||||||
|
|
||||||
|
const sessionID = raw.sessionID
|
||||||
|
const provider = raw.provider
|
||||||
|
const message = raw.message
|
||||||
|
|
||||||
|
if (typeof sessionID !== "string") return null
|
||||||
|
if (!isRecord(provider) || typeof provider.id !== "string") return null
|
||||||
|
if (!isRecord(message)) return null
|
||||||
|
|
||||||
|
return {
|
||||||
|
sessionID,
|
||||||
|
provider: { id: provider.id },
|
||||||
|
message: {
|
||||||
|
id: typeof message.id === "string" ? message.id : undefined,
|
||||||
|
role: typeof message.role === "string" ? message.role : undefined,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isChatHeadersOutput(raw: unknown): raw is ChatHeadersOutput {
|
||||||
|
if (!isRecord(raw)) return false
|
||||||
|
if (!isRecord(raw.headers)) {
|
||||||
|
raw.headers = {}
|
||||||
|
}
|
||||||
|
return isRecord(raw.headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
function isCopilotProvider(providerID: string): boolean {
|
||||||
|
return providerID === "github-copilot" || providerID === "github-copilot-enterprise"
|
||||||
|
}
|
||||||
|
|
||||||
|
async function hasInternalMarker(
|
||||||
|
client: PluginContext["client"],
|
||||||
|
sessionID: string,
|
||||||
|
messageID: string,
|
||||||
|
): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const response = await client.session.message({
|
||||||
|
path: { id: sessionID, messageID },
|
||||||
|
})
|
||||||
|
|
||||||
|
const data = response.data
|
||||||
|
if (!isRecord(data) || !Array.isArray(data.parts)) return false
|
||||||
|
|
||||||
|
return data.parts.some((part) => {
|
||||||
|
if (!isRecord(part) || part.type !== "text" || typeof part.text !== "string") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return part.text.includes(OMO_INTERNAL_INITIATOR_MARKER)
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function isOmoInternalMessage(input: ChatHeadersInput, client: PluginContext["client"]): Promise<boolean> {
|
||||||
|
if (input.message.role !== "user") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!input.message.id) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasInternalMarker(client, input.sessionID, input.message.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createChatHeadersHandler(args: { ctx: PluginContext }): (input: unknown, output: unknown) => Promise<void> {
|
||||||
|
const { ctx } = args
|
||||||
|
|
||||||
|
return async (input, output): Promise<void> => {
|
||||||
|
const normalizedInput = buildChatHeadersInput(input)
|
||||||
|
if (!normalizedInput) return
|
||||||
|
if (!isChatHeadersOutput(output)) return
|
||||||
|
|
||||||
|
if (!isCopilotProvider(normalizedInput.provider.id)) return
|
||||||
|
if (!(await isOmoInternalMessage(normalizedInput, ctx.client))) return
|
||||||
|
|
||||||
|
output.headers["x-initiator"] = "agent"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,7 +2,17 @@ import type { Plugin, ToolDefinition } from "@opencode-ai/plugin"
|
|||||||
|
|
||||||
export type PluginContext = Parameters<Plugin>[0]
|
export type PluginContext = Parameters<Plugin>[0]
|
||||||
export type PluginInstance = Awaited<ReturnType<Plugin>>
|
export type PluginInstance = Awaited<ReturnType<Plugin>>
|
||||||
export type PluginInterface = Omit<PluginInstance, "experimental.session.compacting">
|
|
||||||
|
type ChatHeadersHook = PluginInstance extends { "chat.headers"?: infer T }
|
||||||
|
? T
|
||||||
|
: (input: unknown, output: unknown) => Promise<void>
|
||||||
|
|
||||||
|
export type PluginInterface = Omit<
|
||||||
|
PluginInstance,
|
||||||
|
"experimental.session.compacting" | "chat.headers"
|
||||||
|
> & {
|
||||||
|
"chat.headers"?: ChatHeadersHook
|
||||||
|
}
|
||||||
|
|
||||||
export type ToolsRecord = Record<string, ToolDefinition>
|
export type ToolsRecord = Record<string, ToolDefinition>
|
||||||
|
|
||||||
|
|||||||
@ -57,3 +57,4 @@ export * from "./opencode-message-dir"
|
|||||||
export * from "./normalize-sdk-response"
|
export * from "./normalize-sdk-response"
|
||||||
export * from "./session-directory-resolver"
|
export * from "./session-directory-resolver"
|
||||||
export * from "./prompt-tools"
|
export * from "./prompt-tools"
|
||||||
|
export * from "./internal-initiator-marker"
|
||||||
|
|||||||
11
src/shared/internal-initiator-marker.ts
Normal file
11
src/shared/internal-initiator-marker.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export const OMO_INTERNAL_INITIATOR_MARKER = "<!-- OMO_INTERNAL_INITIATOR -->"
|
||||||
|
|
||||||
|
export function createInternalAgentTextPart(text: string): {
|
||||||
|
type: "text"
|
||||||
|
text: string
|
||||||
|
} {
|
||||||
|
return {
|
||||||
|
type: "text",
|
||||||
|
text: `${text}\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,6 +16,10 @@ type ToolContextWithCallID = ToolContext & {
|
|||||||
call_id?: string
|
call_id?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ToolContextWithMetadata = ToolContextWithCallID & {
|
||||||
|
metadata?: (value: unknown) => void
|
||||||
|
}
|
||||||
|
|
||||||
function resolveToolCallID(ctx: ToolContextWithCallID): string | undefined {
|
function resolveToolCallID(ctx: ToolContextWithCallID): string | undefined {
|
||||||
if (typeof ctx.callID === "string" && ctx.callID.trim() !== "") return ctx.callID
|
if (typeof ctx.callID === "string" && ctx.callID.trim() !== "") return ctx.callID
|
||||||
if (typeof ctx.callId === "string" && ctx.callId.trim() !== "") return ctx.callId
|
if (typeof ctx.callId === "string" && ctx.callId.trim() !== "") return ctx.callId
|
||||||
@ -145,6 +149,7 @@ CONTENT FORMAT:
|
|||||||
},
|
},
|
||||||
execute: async (args: HashlineEditArgs, context: ToolContext) => {
|
execute: async (args: HashlineEditArgs, context: ToolContext) => {
|
||||||
try {
|
try {
|
||||||
|
const metadataContext = context as ToolContextWithMetadata
|
||||||
const filePath = args.filePath
|
const filePath = args.filePath
|
||||||
const { edits } = args
|
const { edits } = args
|
||||||
|
|
||||||
@ -188,9 +193,11 @@ CONTENT FORMAT:
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
context.metadata(meta)
|
if (typeof metadataContext.metadata === "function") {
|
||||||
|
metadataContext.metadata(meta)
|
||||||
|
}
|
||||||
|
|
||||||
const callID = resolveToolCallID(context)
|
const callID = resolveToolCallID(metadataContext)
|
||||||
if (callID) {
|
if (callID) {
|
||||||
storeToolMetadata(context.sessionID, callID, meta)
|
storeToolMetadata(context.sessionID, callID, meta)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user