适配图片

This commit is contained in:
Sirius
2026-08-12 16:23:52 +08:00
parent f65ac095bb
commit 995392e526
12 changed files with 821 additions and 116 deletions
+47 -1
View File
@@ -1,5 +1,12 @@
import Fastify, { type FastifyInstance } from "fastify";
import { ZodError } from "zod";
import {
AttachmentInputError,
materializeAttachments,
type AttachmentReference,
type MaterializedAttachments,
type PendingAttachment,
} from "./attachments.js";
import { startChatCompletionSse, type ChatCompletionSseWriter } from "./chat-stream.js";
import type { BridgeConfig } from "./config.js";
import {
@@ -15,6 +22,7 @@ import {
buildToolStructuredOutputRepairPrompt,
chatResponseTextFormat,
chatCompletionRequestSchema,
chatInputAttachments,
completionResponse,
ignoredChatCompatibilityParameters,
openAIError,
@@ -116,6 +124,10 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
object: "model",
created: 0,
owned_by: "command-code-local",
capabilities: {
input: config.models[id]!.supports_image_input ? ["text", "image"] : ["text"],
output: ["text"],
},
})),
}));
@@ -181,6 +193,28 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
));
}
let pendingAttachments: PendingAttachment[];
try {
pendingAttachments = chatInputAttachments(parsed);
if (pendingAttachments.length > 0 && !model.supports_image_input) {
throw new AttachmentInputError(
`Model '${parsed.model}' is not configured for image input`,
"unsupported_parameter",
pendingAttachments[0]!.param,
);
}
} catch (error) {
if (error instanceof AttachmentInputError) {
return reply.status(400).send(openAIError(
error.message,
error.code,
"invalid_request_error",
error.param,
));
}
throw error;
}
const toolSchemaErrors = validateToolSchemas(parsed);
if (toolSchemaErrors.length > 0) {
return reply.status(400).send(openAIError(
@@ -211,6 +245,7 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
const { controller: abortController } = lease;
let responseCompleted = false;
let acquired = false;
let materialized: MaterializedAttachments | undefined;
const cancelOnDisconnect = () => {
if (!responseCompleted) coordinator.cancel(abortController, "client disconnected");
@@ -233,6 +268,10 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
responseCompleted = true;
return reply;
}
materialized = await materializeAttachments(
config.resolvedWorkingDirectory,
pendingAttachments,
);
const toolMode = usesToolCalling(parsed);
const execution = await executeChatTurn({
config,
@@ -243,6 +282,7 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
deadline: Date.now() + config.timeout_seconds * 1000,
toolMode,
format: responseFormat,
attachmentReferences: materialized.references,
...(structuredValidation ? { validation: structuredValidation } : {}),
...(writer ? { writer } : {}),
});
@@ -351,6 +391,11 @@ export async function createServer(config: BridgeConfig): Promise<BridgeServer>
} finally {
request.raw.off("aborted", cancelOnDisconnect);
reply.raw.off("close", cancelOnDisconnect);
try {
await materialized?.cleanup();
} catch {
process.stderr.write("警告:无法清理本次请求的临时附件目录。\n");
}
if (!acquired) coordinator.cancel(abortController, "request ended before execution");
coordinator.finish(abortController);
}
@@ -379,6 +424,7 @@ interface ChatTurnOptions {
deadline: number;
toolMode: boolean;
format: ResponseTextFormat;
attachmentReferences: ReadonlyMap<string, AttachmentReference>;
validation?: StructuredValidation;
writer?: ChatCompletionSseWriter;
}
@@ -391,7 +437,7 @@ class ChatStructuredOutputError extends Error {
}
async function executeChatTurn(options: ChatTurnOptions): Promise<ChatExecution> {
const originalPrompt = buildCommandPrompt(options.request);
const originalPrompt = buildCommandPrompt(options.request, options.attachmentReferences);
const constrained = options.toolMode || options.validation !== undefined;
const first = await runCommandCode(
options.config,