mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:10:42 +00:00
52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import type { Context } from "@mariozechner/pi-ai";
|
|
import { buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
|
|
|
|
export { buildCopilotIdeHeaders } from "../plugin-sdk/provider-auth.js";
|
|
|
|
function inferCopilotInitiator(messages: Context["messages"]): "agent" | "user" {
|
|
const last = messages[messages.length - 1];
|
|
if (!last) {
|
|
return "user";
|
|
}
|
|
if (last.role === "user" && containsCopilotContentType(last.content, "tool_result")) {
|
|
return "agent";
|
|
}
|
|
return last.role === "user" ? "user" : "agent";
|
|
}
|
|
|
|
function containsCopilotContentType(value: unknown, type: string): boolean {
|
|
if (Array.isArray(value)) {
|
|
return value.some((item) => containsCopilotContentType(item, type));
|
|
}
|
|
if (!value || typeof value !== "object") {
|
|
return false;
|
|
}
|
|
const entry = value as { type?: unknown; content?: unknown };
|
|
return entry.type === type || containsCopilotContentType(entry.content, type);
|
|
}
|
|
|
|
export function hasCopilotVisionInput(messages: Context["messages"]): boolean {
|
|
return messages.some((message) => {
|
|
if (message.role === "user" && Array.isArray(message.content)) {
|
|
return message.content.some((item) => containsCopilotContentType(item, "image"));
|
|
}
|
|
if (message.role === "toolResult" && Array.isArray(message.content)) {
|
|
return message.content.some((item) => containsCopilotContentType(item, "image"));
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
export function buildCopilotDynamicHeaders(params: {
|
|
messages: Context["messages"];
|
|
hasImages: boolean;
|
|
}): Record<string, string> {
|
|
return {
|
|
...buildCopilotIdeHeaders(),
|
|
"Copilot-Integration-Id": "vscode-chat",
|
|
"Openai-Organization": "github-copilot",
|
|
"x-initiator": inferCopilotInitiator(params.messages),
|
|
...(params.hasImages ? { "Copilot-Vision-Request": "true" } : {}),
|
|
};
|
|
}
|