Merge 762880fc9c into 09be5d45d5
This commit is contained in:
commit
0bde25f47c
@ -4,6 +4,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
|
|||||||
import { buildHistoryContextFromEntries, type HistoryEntry } from "../auto-reply/reply/history.js";
|
import { buildHistoryContextFromEntries, type HistoryEntry } from "../auto-reply/reply/history.js";
|
||||||
import { createDefaultDeps } from "../cli/deps.js";
|
import { createDefaultDeps } from "../cli/deps.js";
|
||||||
import { agentCommand } from "../commands/agent.js";
|
import { agentCommand } from "../commands/agent.js";
|
||||||
|
import type { ImageContent } from "../commands/agent/types.js";
|
||||||
import { emitAgentEvent, onAgentEvent } from "../infra/agent-events.js";
|
import { emitAgentEvent, onAgentEvent } from "../infra/agent-events.js";
|
||||||
import { defaultRuntime } from "../runtime.js";
|
import { defaultRuntime } from "../runtime.js";
|
||||||
import { authorizeGatewayConnect, type ResolvedGatewayAuth } from "./auth.js";
|
import { authorizeGatewayConnect, type ResolvedGatewayAuth } from "./auth.js";
|
||||||
@ -64,8 +65,30 @@ function extractTextContent(content: unknown): string {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractImages(content: unknown): ImageContent[] {
|
||||||
|
if (!Array.isArray(content)) return [];
|
||||||
|
const images: ImageContent[] = [];
|
||||||
|
for (const part of content) {
|
||||||
|
if (!part || typeof part !== "object") continue;
|
||||||
|
const p = part as { type?: unknown; image_url?: { url?: unknown } };
|
||||||
|
if (p.type === "image_url" && typeof p.image_url?.url === "string") {
|
||||||
|
const url = p.image_url.url;
|
||||||
|
const match = /^data:(image\/[^;]+);base64,(.*)$/.exec(url);
|
||||||
|
if (match) {
|
||||||
|
images.push({
|
||||||
|
type: "image",
|
||||||
|
mimeType: match[1]!,
|
||||||
|
data: match[2]!,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
function buildAgentPrompt(messagesUnknown: unknown): {
|
function buildAgentPrompt(messagesUnknown: unknown): {
|
||||||
message: string;
|
message: string;
|
||||||
|
images?: ImageContent[];
|
||||||
extraSystemPrompt?: string;
|
extraSystemPrompt?: string;
|
||||||
} {
|
} {
|
||||||
const messages = asMessages(messagesUnknown);
|
const messages = asMessages(messagesUnknown);
|
||||||
@ -73,14 +96,16 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
|||||||
const systemParts: string[] = [];
|
const systemParts: string[] = [];
|
||||||
const conversationEntries: Array<{ role: "user" | "assistant" | "tool"; entry: HistoryEntry }> =
|
const conversationEntries: Array<{ role: "user" | "assistant" | "tool"; entry: HistoryEntry }> =
|
||||||
[];
|
[];
|
||||||
|
let lastUserImages: ImageContent[] | undefined;
|
||||||
|
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (!msg || typeof msg !== "object") continue;
|
if (!msg || typeof msg !== "object") continue;
|
||||||
const role = typeof msg.role === "string" ? msg.role.trim() : "";
|
const role = typeof msg.role === "string" ? msg.role.trim() : "";
|
||||||
const content = extractTextContent(msg.content).trim();
|
const content = extractTextContent(msg.content).trim();
|
||||||
if (!role || !content) continue;
|
const images = extractImages(msg.content);
|
||||||
|
|
||||||
if (role === "system" || role === "developer") {
|
if (role === "system" || role === "developer") {
|
||||||
systemParts.push(content);
|
if (content) systemParts.push(content);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,6 +114,10 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (normalizedRole === "user") {
|
||||||
|
lastUserImages = images.length > 0 ? images : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
const name = typeof msg.name === "string" ? msg.name.trim() : "";
|
const name = typeof msg.name === "string" ? msg.name.trim() : "";
|
||||||
const sender =
|
const sender =
|
||||||
normalizedRole === "assistant"
|
normalizedRole === "assistant"
|
||||||
@ -99,10 +128,12 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
|||||||
? `Tool:${name}`
|
? `Tool:${name}`
|
||||||
: "Tool";
|
: "Tool";
|
||||||
|
|
||||||
conversationEntries.push({
|
if (content || images.length > 0) {
|
||||||
role: normalizedRole,
|
conversationEntries.push({
|
||||||
entry: { sender, body: content },
|
role: normalizedRole,
|
||||||
});
|
entry: { sender, body: content || (images.length > 0 ? "(image)" : "") },
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let message = "";
|
let message = "";
|
||||||
@ -134,6 +165,7 @@ function buildAgentPrompt(messagesUnknown: unknown): {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
message,
|
message,
|
||||||
|
images: lastUserImages,
|
||||||
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : undefined,
|
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -187,10 +219,10 @@ export async function handleOpenAiHttpRequest(
|
|||||||
const agentId = resolveAgentIdForRequest({ req, model });
|
const agentId = resolveAgentIdForRequest({ req, model });
|
||||||
const sessionKey = resolveOpenAiSessionKey({ req, agentId, user });
|
const sessionKey = resolveOpenAiSessionKey({ req, agentId, user });
|
||||||
const prompt = buildAgentPrompt(payload.messages);
|
const prompt = buildAgentPrompt(payload.messages);
|
||||||
if (!prompt.message) {
|
if (!prompt.message && (!prompt.images || prompt.images.length === 0)) {
|
||||||
sendJson(res, 400, {
|
sendJson(res, 400, {
|
||||||
error: {
|
error: {
|
||||||
message: "Missing user message in `messages`.",
|
message: "Missing user message or images in `messages`.",
|
||||||
type: "invalid_request_error",
|
type: "invalid_request_error",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -204,7 +236,8 @@ export async function handleOpenAiHttpRequest(
|
|||||||
try {
|
try {
|
||||||
const result = await agentCommand(
|
const result = await agentCommand(
|
||||||
{
|
{
|
||||||
message: prompt.message,
|
message: prompt.message || "(image)",
|
||||||
|
images: prompt.images,
|
||||||
extraSystemPrompt: prompt.extraSystemPrompt,
|
extraSystemPrompt: prompt.extraSystemPrompt,
|
||||||
sessionKey,
|
sessionKey,
|
||||||
runId,
|
runId,
|
||||||
@ -311,7 +344,8 @@ export async function handleOpenAiHttpRequest(
|
|||||||
try {
|
try {
|
||||||
const result = await agentCommand(
|
const result = await agentCommand(
|
||||||
{
|
{
|
||||||
message: prompt.message,
|
message: prompt.message || "(image)",
|
||||||
|
images: prompt.images,
|
||||||
extraSystemPrompt: prompt.extraSystemPrompt,
|
extraSystemPrompt: prompt.extraSystemPrompt,
|
||||||
sessionKey,
|
sessionKey,
|
||||||
runId,
|
runId,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user