mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:38:27 +00:00
fix(gateway): tighten openai-http edge handling
This commit is contained in:
@@ -41,6 +41,51 @@ function writeSse(res: ServerResponse, data: unknown) {
|
||||
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
||||
}
|
||||
|
||||
function buildAgentCommandInput(params: {
|
||||
prompt: { message: string; extraSystemPrompt?: string };
|
||||
sessionKey: string;
|
||||
runId: string;
|
||||
}) {
|
||||
return {
|
||||
message: params.prompt.message,
|
||||
extraSystemPrompt: params.prompt.extraSystemPrompt,
|
||||
sessionKey: params.sessionKey,
|
||||
runId: params.runId,
|
||||
deliver: false as const,
|
||||
messageChannel: "webchat" as const,
|
||||
bestEffortDeliver: false as const,
|
||||
};
|
||||
}
|
||||
|
||||
function writeAssistantRoleChunk(res: ServerResponse, params: { runId: string; model: string }) {
|
||||
writeSse(res, {
|
||||
id: params.runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: params.model,
|
||||
choices: [{ index: 0, delta: { role: "assistant" } }],
|
||||
});
|
||||
}
|
||||
|
||||
function writeAssistantContentChunk(
|
||||
res: ServerResponse,
|
||||
params: { runId: string; model: string; content: string; finishReason: "stop" | null },
|
||||
) {
|
||||
writeSse(res, {
|
||||
id: params.runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model: params.model,
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: { content: params.content },
|
||||
finish_reason: params.finishReason,
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
function asMessages(val: unknown): OpenAiChatMessage[] {
|
||||
return Array.isArray(val) ? (val as OpenAiChatMessage[]) : [];
|
||||
}
|
||||
@@ -194,22 +239,15 @@ export async function handleOpenAiHttpRequest(
|
||||
|
||||
const runId = `chatcmpl_${randomUUID()}`;
|
||||
const deps = createDefaultDeps();
|
||||
const commandInput = buildAgentCommandInput({
|
||||
prompt,
|
||||
sessionKey,
|
||||
runId,
|
||||
});
|
||||
|
||||
if (!stream) {
|
||||
try {
|
||||
const result = await agentCommand(
|
||||
{
|
||||
message: prompt.message,
|
||||
extraSystemPrompt: prompt.extraSystemPrompt,
|
||||
sessionKey,
|
||||
runId,
|
||||
deliver: false,
|
||||
messageChannel: "webchat",
|
||||
bestEffortDeliver: false,
|
||||
},
|
||||
defaultRuntime,
|
||||
deps,
|
||||
);
|
||||
const result = await agentCommand(commandInput, defaultRuntime, deps);
|
||||
|
||||
const content = resolveAgentResponseText(result);
|
||||
|
||||
@@ -258,28 +296,15 @@ export async function handleOpenAiHttpRequest(
|
||||
|
||||
if (!wroteRole) {
|
||||
wroteRole = true;
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model,
|
||||
choices: [{ index: 0, delta: { role: "assistant" } }],
|
||||
});
|
||||
writeAssistantRoleChunk(res, { runId, model });
|
||||
}
|
||||
|
||||
sawAssistantDelta = true;
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
writeAssistantContentChunk(res, {
|
||||
runId,
|
||||
model,
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: { content },
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
content,
|
||||
finishReason: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -302,19 +327,7 @@ export async function handleOpenAiHttpRequest(
|
||||
|
||||
void (async () => {
|
||||
try {
|
||||
const result = await agentCommand(
|
||||
{
|
||||
message: prompt.message,
|
||||
extraSystemPrompt: prompt.extraSystemPrompt,
|
||||
sessionKey,
|
||||
runId,
|
||||
deliver: false,
|
||||
messageChannel: "webchat",
|
||||
bestEffortDeliver: false,
|
||||
},
|
||||
defaultRuntime,
|
||||
deps,
|
||||
);
|
||||
const result = await agentCommand(commandInput, defaultRuntime, deps);
|
||||
|
||||
if (closed) {
|
||||
return;
|
||||
@@ -323,30 +336,17 @@ export async function handleOpenAiHttpRequest(
|
||||
if (!sawAssistantDelta) {
|
||||
if (!wroteRole) {
|
||||
wroteRole = true;
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
model,
|
||||
choices: [{ index: 0, delta: { role: "assistant" } }],
|
||||
});
|
||||
writeAssistantRoleChunk(res, { runId, model });
|
||||
}
|
||||
|
||||
const content = resolveAgentResponseText(result);
|
||||
|
||||
sawAssistantDelta = true;
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
writeAssistantContentChunk(res, {
|
||||
runId,
|
||||
model,
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: { content },
|
||||
finish_reason: null,
|
||||
},
|
||||
],
|
||||
content,
|
||||
finishReason: null,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -354,18 +354,11 @@ export async function handleOpenAiHttpRequest(
|
||||
if (closed) {
|
||||
return;
|
||||
}
|
||||
writeSse(res, {
|
||||
id: runId,
|
||||
object: "chat.completion.chunk",
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
writeAssistantContentChunk(res, {
|
||||
runId,
|
||||
model,
|
||||
choices: [
|
||||
{
|
||||
index: 0,
|
||||
delta: { content: "Error: internal error" },
|
||||
finish_reason: "stop",
|
||||
},
|
||||
],
|
||||
content: "Error: internal error",
|
||||
finishReason: "stop",
|
||||
});
|
||||
emitAgentEvent({
|
||||
runId,
|
||||
|
||||
Reference in New Issue
Block a user