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

Function validateRenderAndSave

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

1681 * would have refused.
1682 */
1683 const validateRenderAndSave = (input: {
1684 readonly code: string;
1685 readonly title: string;
1686 readonly description?: string | undefined;
1687 readonly connections?: Readonly<Record<string, string>> | undefined;
1688 readonly existing: Artifact | null;
1689 }): Effect.Effect<McpToolResult, unknown> =>
1690 Effect.gen(function* () {
1691 const rejection = validateArtifactCode(input.code);
1692 if (rejection) return renderRejectedResult(rejection);
1693
1694 // Static checks first, then the real one: render it. See
1695 // `smokeRenderRejection` for what the model is told.
1696 //
1697 // FAIL OPEN. The renderer is injected, runs on three different hosts,
1698 // and is the newest thing in this path — if IT breaks (a missing
1699 // module, an environment gap on some host), the right outcome is a
1700 // saved artifact and a logged warning, never a refused create of code
1701 // that is perfectly good. Only a definite `failed` blocks a save.
1702 const smoke = config.smokeRenderArtifact;
1703 // The render that validates the artifact is also the render that
1704 // previews it: the same pass produces the loading-state markup the
1705 // gallery draws, so a preview costs nothing beyond sanitizing it.
1706 let preview: string | null = null;
1707 if (smoke) {
1708 const smokeResult: ArtifactSmokeRenderResult = yield* Effect.tryPromise(() =>
1709 smoke(input.code),
1710 ).pipe(
1711 Effect.catchCause((cause) =>
1712 Effect.as(Effect.logWarning("create-artifact smoke render was unavailable", cause), {
1713 status: "ok",
1714 } satisfies ArtifactSmokeRenderResult),
1715 ),
1716 );
1717 const renderRejection = smokeRenderRejection(smokeResult);
1718 if (renderRejection) {
1719 yield* Effect.annotateCurrentSpan({ "mcp.artifact.smoke_render": "failed" });
1720 return renderRejectedResult(renderRejection);
1721 }
1722 // Fail open, exactly as the verdict does: a preview that cannot be
1723 // produced or cannot be sanitized is a card that falls back to its
1724 // schematic, never a create that is refused.
1725 preview =
1726 smokeResult.status === "ok" && smokeResult.markup !== undefined
1727 ? sanitizeArtifactPreviewMarkup(smokeResult.markup)
1728 : null;
1729 }
1730
1731 const saveInput = {
1732 code: input.code,
1733 title: input.title,
1734 preview,
1735 ...(input.description === undefined ? {} : { description: input.description }),
1736 ...(input.existing === null ? {} : { existingId: input.existing.id }),
1737 };
1738
1739 const roles = extractArtifactRoles(input.code);
1740 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