Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/memos-local-plugin/core/llm/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export async function httpPostJson<TResp>(opts: HttpPostOpts<unknown>): Promise<
attempt,
transient,
durationMs: ms,
body: truncateLogBody(text),
});
if (transient && attempt <= opts.maxRetries) {
opts.onRetry?.(attempt);
Expand Down Expand Up @@ -137,6 +138,7 @@ export async function httpPostStream(opts: {
provider: LlmProviderName;
log: LlmProviderLogger;
}): Promise<Response> {
const start = Date.now();
const signal = mergeSignals(opts.signal, AbortSignal.timeout(opts.timeoutMs));
const resp = await fetch(opts.url, {
method: "POST",
Expand All @@ -150,6 +152,12 @@ export async function httpPostStream(opts: {
});
if (!resp.ok) {
const text = await safeText(resp);
opts.log.warn("http.non_ok", {
status: resp.status,
transient: resp.status >= 500 || resp.status === 429,
durationMs: Date.now() - start,
body: truncateLogBody(text),
});
throw new MemosError(
errCodeForStatus(resp.status),
`HTTP ${resp.status} from ${opts.provider} (stream)`,
Expand Down Expand Up @@ -217,6 +225,10 @@ async function safeText(resp: Response): Promise<string | undefined> {
}
}

function truncateLogBody(text: string | undefined): string | undefined {
return text?.slice(0, 512);
}

function isTransientError(err: unknown): boolean {
if (!(err instanceof Error)) return false;
const msg = err.message ?? "";
Expand Down
48 changes: 48 additions & 0 deletions apps/memos-local-plugin/tests/unit/llm/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ describe("llm/fetcher", () => {
}
});

it("logs a truncated response body for non-ok JSON responses", async () => {
const warn = vi.fn();
const body = "x".repeat(600);
mockFetch([new Response(body, { status: 400 })]);

await expect(
httpPostJson({
url: "https://x",
body: {},
timeoutMs: 5_000,
maxRetries: 0,
provider: "openai_compatible",
log: { ...nullLog(), warn },
}),
).rejects.toBeInstanceOf(MemosError);

expect(warn).toHaveBeenCalledWith(
"http.non_ok",
expect.objectContaining({
status: 400,
body: "x".repeat(512),
}),
);
});

it("timeout → LLM_TIMEOUT", async () => {
const timeout = new DOMException("The operation was aborted due to timeout", "TimeoutError");
mockFetch([timeout]);
Expand Down Expand Up @@ -172,6 +197,29 @@ describe("llm/fetcher", () => {
}
});

it("logs response body for non-ok streaming responses", async () => {
const warn = vi.fn();
mockFetch([new Response("stream rejected", { status: 400 })]);

await expect(
httpPostStream({
url: "https://x",
body: {},
timeoutMs: 5_000,
provider: "openai_compatible",
log: { ...nullLog(), warn },
}),
).rejects.toBeInstanceOf(MemosError);

expect(warn).toHaveBeenCalledWith(
"http.non_ok",
expect.objectContaining({
status: 400,
body: "stream rejected",
}),
);
});

it("decodeSse splits events at blank lines and drops [DONE] sentinel handling to caller", async () => {
const chunks = [
"data: {\"a\":1}\n\n",
Expand Down
Loading