Serve write tools whose later reads confirm the effect, and keep main's state-file ownership and part-wise reply measurement. Squashed rebase of #5600 onto the rebased #5352 tip.
51 lines
2.0 KiB
TypeScript
51 lines
2.0 KiB
TypeScript
import type {
|
|
StateWritePlan,
|
|
WriteBody,
|
|
} from "../../domain/analysisView/environmentStateWriteRules.js";
|
|
|
|
/** Members named in a description; the plan in the state Evidence holds every one. */
|
|
const DESCRIBED_MEMBERS = 12;
|
|
|
|
const members = (names: readonly string[]): string =>
|
|
names.length <= DESCRIBED_MEMBERS
|
|
? names.join(", ")
|
|
: `${names.slice(0, DESCRIBED_MEMBERS).join(", ")}, … (${String(names.length)} members; all in the state Evidence)`;
|
|
|
|
/** The body members a served write accepts, as a description clause. */
|
|
const bodyClause = (
|
|
body: WriteBody,
|
|
effect: StateWritePlan["effect"],
|
|
): string => {
|
|
const parts: string[] = [];
|
|
if (body.accepted.length > 0)
|
|
parts.push(`body members ${members(body.accepted)}`);
|
|
if (body.fixed.length > 0)
|
|
parts.push(
|
|
`${members(body.fixed.map(({ member }) => member))} only with the value the capture sent`,
|
|
);
|
|
if (body.required.length > 0)
|
|
parts.push(`required: ${members(body.required)}`);
|
|
else if (effect === "update") parts.push("send at least one");
|
|
return parts.length === 0 ? "" : ` (${parts.join("; ")})`;
|
|
};
|
|
|
|
/** What a served write tool does to the state, for its description; undefined for a tool the state does not serve. */
|
|
export const describeWrite = (
|
|
plan: StateWritePlan | undefined,
|
|
): string | undefined => {
|
|
if (plan?.served !== true) return undefined;
|
|
const body = plan.body === null ? "" : bodyClause(plan.body, plan.effect);
|
|
switch (plan.effect) {
|
|
case "create":
|
|
return `creates an entity of ${plan.target ?? ""} in the served environment state${body}, modelled on the recorded calls`;
|
|
case "update":
|
|
return `changes the entity the path names in the served environment state${body}, modelled on the recorded calls`;
|
|
case "delete":
|
|
return "removes the entity the path names from the served environment state, modelled on the recorded calls";
|
|
case "document":
|
|
return `changes a document of the served environment state${body}, modelled on the recorded calls`;
|
|
case "none":
|
|
return undefined;
|
|
}
|
|
};
|