Dialogue drafting hung on "Drafting..." forever against OpenRouter, with the provider having received and completed the request. Two independent faults, both found by running the app's own code against the live endpoint. The hang: streamObject is lazy. Awaiting result.object alone does not pump the stream in this SDK version, so the promise waited for a parse that never started and the call sat until its ten minute ceiling. One identical request, measured twice: object-only never returned in 60s, draining textStream first returned a valid draft in 6.9s. generateStructured now drains the stream before reading the object. The endpoint: provider(modelId) resolves to OpenAI's Responses API, which only OpenAI implements. OpenRouter answered 200 and never completed. Every OpenAI-compatible service the app targets serves /chat/completions, which the plain path already used, so chatModelFor reaches the model through .chat(). The instrumented request now goes to /chat/completions and Compose's structured branch uses the same call. Defences, since a provider can still answer and then go quiet: an idle watchdog aborts a body that delivers no bytes for 60s on both paths, transport retries are off so one stall is one wait, a draft is capped at four minutes, and the dialog has a UI watchdog plus a catch that shows the failure instead of spinning. Against the live endpoint the real function now returns a five node draft in 6.2s. 246 tests across 24 files pass, including a regression test that fails if the stream drain is ever removed.
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
// @vitest-environment node
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
/**
|
|
* Regression: `streamObject`'s stream is lazy, so `result.object` never resolves
|
|
* unless something drains the stream first. Awaiting only the object promise is
|
|
* what made every NPC draft hang on "Drafting..." against OpenRouter, with the
|
|
* provider having delivered a complete answer.
|
|
*
|
|
* The stub below models that laziness: `object` resolves only after the text
|
|
* stream has been read.
|
|
*/
|
|
vi.mock("ai", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("ai")>();
|
|
return {
|
|
...actual,
|
|
streamObject: () => {
|
|
let drained = false;
|
|
return {
|
|
textStream: (async function* () {
|
|
drained = true;
|
|
yield '{"nodes":[{"id":"n1"}]}';
|
|
})(),
|
|
object: new Promise((resolve) => {
|
|
const timer = setInterval(() => {
|
|
if (!drained) return;
|
|
clearInterval(timer);
|
|
resolve({ nodes: [{ id: "n1" }] });
|
|
}, 5);
|
|
}),
|
|
finishReason: Promise.resolve("stop"),
|
|
};
|
|
},
|
|
};
|
|
});
|
|
|
|
const { generateStructured } = await import("@/lib/ai/generate");
|
|
|
|
describe("structured generation drives the stream", () => {
|
|
it("resolves the object instead of waiting on a lazy stream", async () => {
|
|
const result = await generateStructured<{ nodes: { id: string }[] }>({
|
|
prompt: "Draft something.",
|
|
system: "Stub.",
|
|
schema: { type: "object", properties: {}, additionalProperties: false },
|
|
connection: {
|
|
label: "stub",
|
|
baseUrl: "https://example.test/v1",
|
|
modelId: "stub-model",
|
|
apiKey: "unused",
|
|
structuredMode: "json_schema",
|
|
sendReasoningOff: false,
|
|
},
|
|
});
|
|
|
|
expect(result.object.nodes).toHaveLength(1);
|
|
expect(result.truncated).toBe(false);
|
|
});
|
|
});
|