refactor(transformer): 根据消息角色设置内容类型并优化代码格式
根据消息角色动态设置内容类型,将assistant消息标记为output_text而非input_text 同时优化代码格式,包括多行导入和长行拆分,提高可读性
This commit is contained in:
@@ -1,6 +1,16 @@
|
|||||||
import { ChatCompletionRequest, ResponsesRequest, InputItem, Message, Content } from "../types.js";
|
import {
|
||||||
|
ChatCompletionRequest,
|
||||||
|
ResponsesRequest,
|
||||||
|
InputItem,
|
||||||
|
Message,
|
||||||
|
Content,
|
||||||
|
} from "../types.js";
|
||||||
import { getNormalizedModel } from "./model-map.js";
|
import { getNormalizedModel } from "./model-map.js";
|
||||||
import { getReasoningConfig, getTextVerbosity, getIncludeFields } from "./reasoning.js";
|
import {
|
||||||
|
getReasoningConfig,
|
||||||
|
getTextVerbosity,
|
||||||
|
getIncludeFields,
|
||||||
|
} from "./reasoning.js";
|
||||||
import { getCodexInstructions } from "../prompts/index.js";
|
import { getCodexInstructions } from "../prompts/index.js";
|
||||||
import { logDebug, logWarn } from "../logger.js";
|
import { logDebug, logWarn } from "../logger.js";
|
||||||
|
|
||||||
@@ -8,12 +18,14 @@ export function messagesToInput(messages: Message[]): InputItem[] {
|
|||||||
return messages.map((msg) => {
|
return messages.map((msg) => {
|
||||||
const content: Content[] = [];
|
const content: Content[] = [];
|
||||||
|
|
||||||
|
const contentType = msg.role === "assistant" ? "output_text" : "input_text";
|
||||||
|
|
||||||
if (typeof msg.content === "string") {
|
if (typeof msg.content === "string") {
|
||||||
content.push({ type: "input_text", text: msg.content });
|
content.push({ type: contentType, text: msg.content });
|
||||||
} else if (Array.isArray(msg.content)) {
|
} else if (Array.isArray(msg.content)) {
|
||||||
for (const item of msg.content) {
|
for (const item of msg.content) {
|
||||||
if (item.type === "text") {
|
if (item.type === "text") {
|
||||||
content.push({ type: "input_text", text: item.text || "" });
|
content.push({ type: contentType, text: item.text || "" });
|
||||||
} else if (item.type === "image_url") {
|
} else if (item.type === "image_url") {
|
||||||
content.push({ type: "input_image", image_url: item.image_url });
|
content.push({ type: "input_image", image_url: item.image_url });
|
||||||
} else {
|
} else {
|
||||||
@@ -30,7 +42,9 @@ export function messagesToInput(messages: Message[]): InputItem[] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function filterInput(input: InputItem[] | undefined): InputItem[] | undefined {
|
export function filterInput(
|
||||||
|
input: InputItem[] | undefined,
|
||||||
|
): InputItem[] | undefined {
|
||||||
if (!Array.isArray(input)) {
|
if (!Array.isArray(input)) {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
@@ -58,7 +72,10 @@ export async function transformChatCompletionRequest(
|
|||||||
): Promise<ResponsesRequest> {
|
): Promise<ResponsesRequest> {
|
||||||
const normalizedModel = getNormalizedModel(request.model) || request.model;
|
const normalizedModel = getNormalizedModel(request.model) || request.model;
|
||||||
|
|
||||||
logDebug(null, `Transforming chat completion request: model=${request.model} -> ${normalizedModel}`);
|
logDebug(
|
||||||
|
null,
|
||||||
|
`Transforming chat completion request: model=${request.model} -> ${normalizedModel}`,
|
||||||
|
);
|
||||||
|
|
||||||
const codexInstructions = await getCodexInstructions(normalizedModel);
|
const codexInstructions = await getCodexInstructions(normalizedModel);
|
||||||
const reasoningConfig = getReasoningConfig(normalizedModel);
|
const reasoningConfig = getReasoningConfig(normalizedModel);
|
||||||
@@ -101,7 +118,10 @@ export async function transformResponsesRequest(
|
|||||||
): Promise<ResponsesRequest> {
|
): Promise<ResponsesRequest> {
|
||||||
const normalizedModel = getNormalizedModel(request.model) || request.model;
|
const normalizedModel = getNormalizedModel(request.model) || request.model;
|
||||||
|
|
||||||
logDebug(null, `Transforming responses request: model=${request.model} -> ${normalizedModel}`);
|
logDebug(
|
||||||
|
null,
|
||||||
|
`Transforming responses request: model=${request.model} -> ${normalizedModel}`,
|
||||||
|
);
|
||||||
|
|
||||||
const codexInstructions = await getCodexInstructions(normalizedModel);
|
const codexInstructions = await getCodexInstructions(normalizedModel);
|
||||||
const reasoningConfig = getReasoningConfig(
|
const reasoningConfig = getReasoningConfig(
|
||||||
@@ -116,7 +136,10 @@ export async function transformResponsesRequest(
|
|||||||
...request,
|
...request,
|
||||||
model: normalizedModel,
|
model: normalizedModel,
|
||||||
input: filterInput(
|
input: filterInput(
|
||||||
request.input || (request.messages ? messagesToInput(request.messages as Message[]) : undefined),
|
request.input ||
|
||||||
|
(request.messages
|
||||||
|
? messagesToInput(request.messages as Message[])
|
||||||
|
: undefined),
|
||||||
),
|
),
|
||||||
stream: request.stream !== undefined ? request.stream : true,
|
stream: request.stream !== undefined ? request.stream : true,
|
||||||
store: request.store !== undefined ? request.store : false,
|
store: request.store !== undefined ? request.store : false,
|
||||||
|
|||||||
Reference in New Issue
Block a user