fix(gateway): cap chat.history to 1000 messages
This commit is contained in:
@@ -458,7 +458,7 @@ export const CronRunLogEntrySchema = Type.Object(
|
||||
export const ChatHistoryParamsSchema = Type.Object(
|
||||
{
|
||||
sessionKey: NonEmptyString,
|
||||
limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 500 })),
|
||||
limit: Type.Optional(Type.Integer({ minimum: 1 })),
|
||||
},
|
||||
{ additionalProperties: false },
|
||||
);
|
||||
|
||||
@@ -1786,8 +1786,8 @@ describe("gateway server", () => {
|
||||
);
|
||||
expect(defaultRes.ok).toBe(true);
|
||||
const defaultMsgs = defaultRes.payload?.messages ?? [];
|
||||
expect(defaultMsgs.length).toBe(200);
|
||||
expect(firstContentText(defaultMsgs[0])).toBe("m100");
|
||||
expect(defaultMsgs.length).toBe(300);
|
||||
expect(firstContentText(defaultMsgs[0])).toBe("m0");
|
||||
|
||||
const limitedRes = await rpcReq<{ messages?: unknown[] }>(
|
||||
ws,
|
||||
@@ -1802,6 +1802,49 @@ describe("gateway server", () => {
|
||||
expect(limitedMsgs.length).toBe(5);
|
||||
expect(firstContentText(limitedMsgs[0])).toBe("m295");
|
||||
|
||||
const largeLines: string[] = [];
|
||||
for (let i = 0; i < 1500; i += 1) {
|
||||
largeLines.push(
|
||||
JSON.stringify({
|
||||
message: {
|
||||
role: "user",
|
||||
content: [{ type: "text", text: `b${i}` }],
|
||||
timestamp: Date.now() + i,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
await fs.writeFile(
|
||||
path.join(dir, "sess-main.jsonl"),
|
||||
largeLines.join("\n"),
|
||||
"utf-8",
|
||||
);
|
||||
|
||||
const cappedRes = await rpcReq<{ messages?: unknown[] }>(
|
||||
ws,
|
||||
"chat.history",
|
||||
{
|
||||
sessionKey: "main",
|
||||
},
|
||||
);
|
||||
expect(cappedRes.ok).toBe(true);
|
||||
const cappedMsgs = cappedRes.payload?.messages ?? [];
|
||||
expect(cappedMsgs.length).toBe(1000);
|
||||
expect(firstContentText(cappedMsgs[0])).toBe("b500");
|
||||
|
||||
const overLimitRes = await rpcReq<{ messages?: unknown[] }>(
|
||||
ws,
|
||||
"chat.history",
|
||||
{
|
||||
sessionKey: "main",
|
||||
limit: 5000,
|
||||
},
|
||||
);
|
||||
expect(overLimitRes.ok).toBe(true);
|
||||
const overLimitMsgs = overLimitRes.payload?.messages ?? [];
|
||||
expect(overLimitMsgs.length).toBe(1000);
|
||||
expect(firstContentText(overLimitMsgs[0])).toBe("b500");
|
||||
|
||||
ws.close();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
@@ -1823,7 +1823,9 @@ export async function startGatewayServer(
|
||||
sessionId && storePath
|
||||
? readSessionMessages(sessionId, storePath)
|
||||
: [];
|
||||
const max = typeof limit === "number" ? limit : 200;
|
||||
const hardMax = 1000;
|
||||
const requested = typeof limit === "number" ? limit : hardMax;
|
||||
const max = Math.min(hardMax, requested);
|
||||
const messages =
|
||||
rawMessages.length > max ? rawMessages.slice(-max) : rawMessages;
|
||||
const thinkingLevel =
|
||||
|
||||
Reference in New Issue
Block a user