fix(copilot): keep notifications visible and detect marker via message lookup
This commit is contained in:
parent
64e8e164aa
commit
a85f7efb1d
@ -36,7 +36,7 @@ export function createPluginInterface(args: {
|
|||||||
await handler(input, output)
|
await handler(input, output)
|
||||||
},
|
},
|
||||||
|
|
||||||
"chat.headers": createChatHeadersHandler(),
|
"chat.headers": createChatHeadersHandler({ ctx }),
|
||||||
|
|
||||||
"chat.message": createChatMessageHandler({
|
"chat.message": createChatMessageHandler({
|
||||||
ctx,
|
ctx,
|
||||||
|
|||||||
@ -4,22 +4,34 @@ import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
|
|||||||
import { createChatHeadersHandler } from "./chat-headers"
|
import { createChatHeadersHandler } from "./chat-headers"
|
||||||
|
|
||||||
describe("createChatHeadersHandler", () => {
|
describe("createChatHeadersHandler", () => {
|
||||||
test("sets x-initiator=agent for Copilot internal synthetic marker messages", async () => {
|
test("sets x-initiator=agent for Copilot internal marker messages", async () => {
|
||||||
const handler = createChatHeadersHandler()
|
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: {} }
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
await handler(
|
await handler(
|
||||||
{
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
provider: { id: "github-copilot" },
|
provider: { id: "github-copilot" },
|
||||||
message: {
|
message: {
|
||||||
info: { role: "user" },
|
id: "msg_1",
|
||||||
parts: [
|
role: "user",
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
|
||||||
synthetic: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
output,
|
output,
|
||||||
@ -29,21 +41,33 @@ describe("createChatHeadersHandler", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("does not override non-copilot providers", async () => {
|
test("does not override non-copilot providers", async () => {
|
||||||
const handler = createChatHeadersHandler()
|
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: {} }
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
await handler(
|
await handler(
|
||||||
{
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
provider: { id: "openai" },
|
provider: { id: "openai" },
|
||||||
message: {
|
message: {
|
||||||
info: { role: "user" },
|
id: "msg_1",
|
||||||
parts: [
|
role: "user",
|
||||||
{
|
|
||||||
type: "text",
|
|
||||||
text: `notification\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
|
||||||
synthetic: true,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
output,
|
output,
|
||||||
@ -53,15 +77,28 @@ describe("createChatHeadersHandler", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("does not override regular user messages", async () => {
|
test("does not override regular user messages", async () => {
|
||||||
const handler = createChatHeadersHandler()
|
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: {} }
|
const output: { headers: Record<string, string> } = { headers: {} }
|
||||||
|
|
||||||
await handler(
|
await handler(
|
||||||
{
|
{
|
||||||
|
sessionID: "ses_1",
|
||||||
provider: { id: "github-copilot" },
|
provider: { id: "github-copilot" },
|
||||||
message: {
|
message: {
|
||||||
info: { role: "user" },
|
id: "msg_1",
|
||||||
parts: [{ type: "text", text: "normal user message" }],
|
role: "user",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
output,
|
output,
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
|
import { OMO_INTERNAL_INITIATOR_MARKER } from "../shared"
|
||||||
|
import type { PluginContext } from "./types"
|
||||||
|
|
||||||
type ChatHeadersInput = {
|
type ChatHeadersInput = {
|
||||||
|
sessionID: string
|
||||||
provider: { id: string }
|
provider: { id: string }
|
||||||
message: {
|
message: {
|
||||||
info?: { role?: string }
|
id?: string
|
||||||
parts?: Array<{ type?: string; text?: string; synthetic?: boolean }>
|
role?: string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,28 +21,20 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|||||||
function buildChatHeadersInput(raw: unknown): ChatHeadersInput | null {
|
function buildChatHeadersInput(raw: unknown): ChatHeadersInput | null {
|
||||||
if (!isRecord(raw)) return null
|
if (!isRecord(raw)) return null
|
||||||
|
|
||||||
|
const sessionID = raw.sessionID
|
||||||
const provider = raw.provider
|
const provider = raw.provider
|
||||||
const message = raw.message
|
const message = raw.message
|
||||||
|
|
||||||
|
if (typeof sessionID !== "string") return null
|
||||||
if (!isRecord(provider) || typeof provider.id !== "string") return null
|
if (!isRecord(provider) || typeof provider.id !== "string") return null
|
||||||
if (!isRecord(message)) return null
|
if (!isRecord(message)) return null
|
||||||
|
|
||||||
const info = isRecord(message.info) ? message.info : undefined
|
|
||||||
const rawParts = Array.isArray(message.parts) ? message.parts : undefined
|
|
||||||
|
|
||||||
const parts = rawParts
|
|
||||||
?.filter(isRecord)
|
|
||||||
.map((part) => ({
|
|
||||||
type: typeof part.type === "string" ? part.type : undefined,
|
|
||||||
text: typeof part.text === "string" ? part.text : undefined,
|
|
||||||
synthetic: part.synthetic === true,
|
|
||||||
}))
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
sessionID,
|
||||||
provider: { id: provider.id },
|
provider: { id: provider.id },
|
||||||
message: {
|
message: {
|
||||||
info: info ? { role: typeof info.role === "string" ? info.role : undefined } : undefined,
|
id: typeof message.id === "string" ? message.id : undefined,
|
||||||
parts,
|
role: typeof message.role === "string" ? message.role : undefined,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -57,28 +51,53 @@ function isCopilotProvider(providerID: string): boolean {
|
|||||||
return providerID === "github-copilot" || providerID === "github-copilot-enterprise"
|
return providerID === "github-copilot" || providerID === "github-copilot-enterprise"
|
||||||
}
|
}
|
||||||
|
|
||||||
function isOmoInternalMessage(input: ChatHeadersInput): boolean {
|
async function hasInternalMarker(
|
||||||
if (input.message.info?.role !== "user") {
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return input.message.parts?.some((part) => {
|
if (!input.message.id) {
|
||||||
if (part.type !== "text" || !part.text || part.synthetic !== true) {
|
return false
|
||||||
return false
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return part.text.includes(OMO_INTERNAL_INITIATOR_MARKER)
|
return hasInternalMarker(client, input.sessionID, input.message.id)
|
||||||
}) ?? false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createChatHeadersHandler(): (input: unknown, output: unknown) => Promise<void> {
|
export function createChatHeadersHandler(args: { ctx: PluginContext }): (input: unknown, output: unknown) => Promise<void> {
|
||||||
|
const { ctx } = args
|
||||||
|
|
||||||
return async (input, output): Promise<void> => {
|
return async (input, output): Promise<void> => {
|
||||||
const normalizedInput = buildChatHeadersInput(input)
|
const normalizedInput = buildChatHeadersInput(input)
|
||||||
if (!normalizedInput) return
|
if (!normalizedInput) return
|
||||||
if (!isChatHeadersOutput(output)) return
|
if (!isChatHeadersOutput(output)) return
|
||||||
|
|
||||||
if (!isCopilotProvider(normalizedInput.provider.id)) return
|
if (!isCopilotProvider(normalizedInput.provider.id)) return
|
||||||
if (!isOmoInternalMessage(normalizedInput)) return
|
if (!(await isOmoInternalMessage(normalizedInput, ctx.client))) return
|
||||||
|
|
||||||
output.headers["x-initiator"] = "agent"
|
output.headers["x-initiator"] = "agent"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,11 +3,9 @@ export const OMO_INTERNAL_INITIATOR_MARKER = "<!-- OMO_INTERNAL_INITIATOR -->"
|
|||||||
export function createInternalAgentTextPart(text: string): {
|
export function createInternalAgentTextPart(text: string): {
|
||||||
type: "text"
|
type: "text"
|
||||||
text: string
|
text: string
|
||||||
synthetic: true
|
|
||||||
} {
|
} {
|
||||||
return {
|
return {
|
||||||
type: "text",
|
type: "text",
|
||||||
text: `${text}\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
text: `${text}\n${OMO_INTERNAL_INITIATOR_MARKER}`,
|
||||||
synthetic: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user