1
0
Fork 0

fix(ai): bound the structured path too, and stop the dialog overpromising

The timeout from the previous commit only covered the plain-text fetch, so
the default drafting path was still unbounded: `streamObject` for
json_schema had no abort signal, which is what `generateDialogueDraft` and
the probe's structured half use. A provider that answers the plain probe
and then stalls could still hold the Generate modal and the Test button
open, while the docs claimed every request was bounded. Both
`streamObject` calls (the shared structured path and Compose's fallback)
now carry the same ceiling.

Dialog copy told the author the branch continues into the successor even
after they cleared the return box, which is when it does not. It now says
that step becomes unreachable until wired by hand. When the node cannot
take a return at all the checkbox used to vanish without explanation; the
reason is now stated where the toggle would be.

A choice node marked as the start could receive the return hop, drawing an
edge into the start node that `connectHop` and the drop handler both
refuse. It no longer returns, and its open end stays free like the other
cases.

Also: the two misplaced doc comments now sit above the declarations they
describe, and the whole-conversation caveat moved into the shared preview
component so the global settings tab carries it too.
This commit is contained in:
Jason Fraley 2026-11-24 15:00:30 -05:00
parent 7adfcde72d
commit 5ccb25e643
8 changed files with 65 additions and 11 deletions

View file

@ -112,7 +112,7 @@ export const NpcDialogues: CollectionConfig = {
{
label: "AI Drafting",
description:
"How the drafting tools should sound for this conversation. The preview shows the assembled instruction for a whole-conversation draft; a generated branch swaps only the structure section.",
"How the drafting tools should sound for this conversation. The preview shows the exact instruction the model receives.",
fields: [
{
name: "voiceOverride",

View file

@ -399,13 +399,22 @@ export function GenerateDraftDialog({
{effectivePlacement === "attach" && attachTarget
? `The branch is wired in from ${attachTarget.label} and lands beside it, unsaved.${
attachTarget.returnsTo === "successor"
? " This replaces that node's current next step, and the branch continues into it at the end."
? returnToCaller
? " This replaces that node's current next step, and the branch continues into it at the end."
: " This replaces that node's current next step, and nothing continues into it, so it becomes unreachable until you wire it by hand."
: ""
}`
: "The branch lands beside the conversation and you drag the line to wire it."}
</p>
)}
{effectivePlacement === "attach" && attachTarget && !attachTarget.returnsTo && (
<p className="text-[11px] leading-snug text-white/45">
This node cannot take a return hop, so the branch&apos;s open end stays free for you
to wire. A condition would retake the same branch, a start node accepts nothing
pointing at it, and a node with nothing after it has nowhere to go.
</p>
)}
{effectivePlacement === "attach" && attachTarget?.returnsTo && (
<label className="flex cursor-pointer items-start gap-2 text-[11px] leading-snug text-white/70">
<input

View file

@ -61,7 +61,8 @@ function PreviewFrame({
<p className="mt-2 text-[11px] text-white/40">
VOICE and HOUSE RULES follow the fields above. STRUCTURE, FIELDS, STATE and OUTPUT
are fixed in code because the draft validator enforces them.
are fixed in code because the draft validator enforces them. This is the prompt for a
whole-conversation draft; a generated branch swaps only the STRUCTURE section.
{voice ? "" : ` No custom voice is set, so the built-in voice is used: ${DEFAULT_VOICE.length} characters.`}
</p>
</div>

View file

@ -49,8 +49,8 @@ export const MODEL_LABEL =
//
// Only sent when the active connection allows it: a strict provider rejects
// unrecognised body fields with a 400.
export /** Ceiling for one completion, including a slow local model. */
const REQUEST_TIMEOUT_MS = 600_000;
/** Ceiling for one completion, including a slow local model. */
export const REQUEST_TIMEOUT_MS = 600_000;
const REASONING_DISABLED = { reasoning_effort: "none" } as const;
@ -322,6 +322,10 @@ export async function generateStructured<T>(
const result = streamObject({
maxOutputTokens: options.maxTokens ?? 5000,
// Same ceiling as the plain path. Without it a provider that answers the
// plain probe and then stalls leaves Generate busy forever, because the
// modal waits on `result.object` for as long as the stream dribbles.
abortSignal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
model: providerFor(connection)(connection?.modelId || options.model || resolveModel()),
onError: (error) => {
console.error("AI generateStructured: ", error);

View file

@ -163,7 +163,12 @@ export function returnTargetOf(
source: DialogueNode,
sourceHandle?: string | null,
): { target: string; kind: "caller" | "successor" } | null {
if (source.type === "choice") return { target: source.id, kind: "caller" };
// A hop into the start node is refused everywhere else (`connectHop`, the
// canvas drop handler), so a start menu is the one caller that cannot take a
// return. Its open end stays free instead of breaking that rule silently.
if (source.type === "choice") {
return source.data.isStart ? null : { target: source.id, kind: "caller" };
}
if (source.type === "condition") return null;
const successor = hopFromHandle(source, sourceHandle)?.target;
return successor ? { target: successor, kind: "successor" } : null;
@ -367,7 +372,12 @@ export function describeAttachTarget(node: DialogueNode): {
};
}
if (node.type === "choice") {
return { label, output: "a new choice button", canAttach: true, returnsTo: "caller" };
return {
label,
output: "a new choice button",
canAttach: true,
returnsTo: returnTargetOf(node, null)?.kind ?? null,
};
}
if (node.type === "condition") {
// A branch cannot rejoin a condition: re-entering re-evaluates the same flags

View file

@ -21,6 +21,10 @@ export function normalizeDashes(text: string): string {
.replace(/\u2013/g, "-");
}
/** Length ceilings for admin-entered prompt text. Shared by every writer. */
export const MAX_VOICE_CHARS = 4000;
export const MAX_EXTRA_RULES_CHARS = 2000;
/**
* Clean an admin-entered prompt fragment: normalise dashes, trim, collapse an
* empty result to null (meaning "use the built-in"), and cap the length.
@ -28,10 +32,6 @@ export function normalizeDashes(text: string): string {
* A cap rather than a rejection: an over-long paste should still save something
* usable instead of failing the whole settings form with no explanation.
*/
/** Length ceilings for admin-entered prompt text. Shared by every writer. */
export const MAX_VOICE_CHARS = 4000;
export const MAX_EXTRA_RULES_CHARS = 2000;
export function cleanPromptText(value: unknown, maxLength: number): string | null {
if (typeof value !== "string") return null;
const normalised = normalizeDashes(value.trim());

View file

@ -130,6 +130,7 @@ import {
generatePlainText,
MODEL_ID,
MODEL_LABEL,
REQUEST_TIMEOUT_MS,
reasoningOptionsFor,
} from "@/lib/ai/generate";
import { resolveConnection } from "@/lib/ai/connections";
@ -782,6 +783,9 @@ export default buildConfig({
// structured path rather than guessing at a wrapper.
const streamResult = streamObject({
maxOutputTokens: options.maxTokens || 5000,
// Same ceiling as every other model call: Compose runs inside an
// admin request, and a stalled provider would hold it open.
abortSignal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
model: connectionProvider(connection)(model),
onError: (error) => {
console.error("Unsloth generateObject: ", error);

View file

@ -181,6 +181,32 @@ describe("insertGeneratedFragment", () => {
expect(result.nodes.some((n) => n.id === "end")).toBe(true);
});
it("does not point a branch at a start node, which the canvas refuses everywhere else", () => {
// A choice can be the start node. Returning to it would draw an edge into the
// start, the one thing `connectHop` and the drop handler both reject.
const base = existing().map((n) =>
n.id === "menu" ? { ...n, data: { ...n.data, isStart: true } } : n,
);
const described = describeAttachTarget(node("m2", "choice", { isStart: true }));
expect(described.canAttach).toBe(true);
expect(described.returnsTo).toBeNull();
const result = insertGeneratedFragment(base, fragment(), {
attachment: { sourceId: "menu", sourceHandle: null },
returnToCaller: true,
newId: ids("a1", "a2"),
});
expect(result.returnedFrom).toBeNull();
expect(result.returnKind).toBeNull();
// Only the inserted nodes matter: the existing start legitimately leads to it.
const insertedPointsAtStart = result.nodes
.filter((n) => n.id === "a1" || n.id === "a2")
.some((n) => n.data.next?.target === "menu");
expect(insertedPointsAtStart).toBe(false);
});
it("refuses to rejoin a condition, because re-entering retakes the same branch", () => {
const base = [
...existing(),