MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / validateRenderAndSave

Function validateRenderAndSave

packages/hosts/mcp/src/tool-server.ts:1720–1801  ·  view source on GitHub ↗
(input: {
      readonly code: string;
      readonly title: string;
      readonly description?: string | undefined;
      readonly connections?: Readonly<Record<string, string>> | undefined;
      readonly existing: Artifact | null;
    })

Source from the content-addressed store, hash-verified

1718 * would have refused.
1719 */
1720 const validateRenderAndSave = (input: {
1721 readonly code: string;
1722 readonly title: string;
1723 readonly description?: string | undefined;
1724 readonly connections?: Readonly<Record<string, string>> | undefined;
1725 readonly existing: Artifact | null;
1726 }): Effect.Effect<McpToolResult, unknown> =>
1727 Effect.gen(function* () {
1728 const rejection = validateArtifactCode(input.code);
1729 if (rejection) return renderRejectedResult(rejection);
1730
1731 // Static checks first, then the real one: render it. See
1732 // `smokeRenderRejection` for what the model is told.
1733 //
1734 // FAIL OPEN. The renderer is injected, runs on three different hosts,
1735 // and is the newest thing in this path — if IT breaks (a missing
1736 // module, an environment gap on some host), the right outcome is a
1737 // saved artifact and a logged warning, never a refused create of code
1738 // that is perfectly good. Only a definite `failed` blocks a save.
1739 const smoke = config.smokeRenderArtifact;
1740 // The render that validates the artifact is also the render that
1741 // previews it: the same pass produces the loading-state markup the
1742 // gallery draws, so a preview costs nothing beyond sanitizing it.
1743 let preview: string | null = null;
1744 if (smoke) {
1745 const smokeResult: ArtifactSmokeRenderResult = yield* Effect.tryPromise(() =>
1746 smoke(input.code),
1747 ).pipe(
1748 Effect.catchCause((cause) =>
1749 Effect.as(Effect.logWarning("create-artifact smoke render was unavailable", cause), {
1750 status: "ok",
1751 } satisfies ArtifactSmokeRenderResult),
1752 ),
1753 );
1754 const renderRejection = smokeRenderRejection(smokeResult);
1755 if (renderRejection) {
1756 yield* Effect.annotateCurrentSpan({ "mcp.artifact.smoke_render": "failed" });
1757 return renderRejectedResult(renderRejection);
1758 }
1759 // Fail open, exactly as the verdict does: a preview that cannot be
1760 // produced or cannot be sanitized is a card that falls back to its
1761 // schematic, never a create that is refused.
1762 preview =
1763 smokeResult.status === "ok" && smokeResult.markup !== undefined
1764 ? sanitizeArtifactPreviewMarkup(smokeResult.markup)
1765 : null;
1766 }
1767
1768 const saveInput = {
1769 code: input.code,
1770 title: input.title,
1771 preview,
1772 ...(input.description === undefined ? {} : { description: input.description }),
1773 ...(input.existing === null ? {} : { existingId: input.existing.id }),
1774 };
1775
1776 const roles = extractArtifactRoles(input.code);
1777 if (roles.length === 0 && input.connections === undefined) {

Callers 2

createArtifactFunction · 0.85
editArtifactFunction · 0.85

Calls 8

validateArtifactCodeFunction · 0.90
smokeRenderRejectionFunction · 0.90
extractArtifactRolesFunction · 0.90
resolveArtifactBindingsFunction · 0.90
renderRejectedResultFunction · 0.85
saveAndDeliverArtifactFunction · 0.85
listMethod · 0.65

Tested by

no test coverage detected