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

Function validateRenderAndSave

packages/hosts/mcp/src/tool-server.ts:1709–1790  ·  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

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