(input: {
readonly code: string;
readonly title: string;
readonly description?: string | undefined;
readonly connections?: Readonly<Record<string, string>> | undefined;
readonly existing: Artifact | null;
})
| 2349 | * would have refused. |
| 2350 | */ |
| 2351 | const validateRenderAndSave = (input: { |
| 2352 | readonly code: string; |
| 2353 | readonly title: string; |
| 2354 | readonly description?: string | undefined; |
| 2355 | readonly connections?: Readonly<Record<string, string>> | undefined; |
| 2356 | readonly existing: Artifact | null; |
| 2357 | }): Effect.Effect<McpToolResult, unknown> => |
| 2358 | Effect.gen(function* () { |
| 2359 | const rejection = validateArtifactCode(input.code); |
| 2360 | if (rejection) return renderRejectedResult(rejection); |
| 2361 | |
| 2362 | // Static checks first, then the real one: render it. See |
| 2363 | // `smokeRenderRejection` for what the model is told. |
| 2364 | // |
| 2365 | // FAIL OPEN. The renderer is injected, runs on three different hosts, |
| 2366 | // and is the newest thing in this path — if IT breaks (a missing |
| 2367 | // module, an environment gap on some host), the right outcome is a |
| 2368 | // saved artifact and a logged warning, never a refused create of code |
| 2369 | // that is perfectly good. Only a definite `failed` blocks a save. |
| 2370 | const smoke = config.smokeRenderArtifact; |
| 2371 | // The render that validates the artifact is also the render that |
| 2372 | // previews it: the same pass produces the loading-state markup the |
| 2373 | // gallery draws, so a preview costs nothing beyond sanitizing it. |
| 2374 | let preview: string | null = null; |
| 2375 | if (smoke) { |
| 2376 | const smokeResult: ArtifactSmokeRenderResult = yield* Effect.tryPromise(() => |
| 2377 | smoke(input.code), |
| 2378 | ).pipe( |
| 2379 | Effect.catchCause((cause) => |
| 2380 | Effect.as( |
| 2381 | Effect.logWarning("create-artifact smoke render was unavailable", { |
| 2382 | causeKind: Cause.isCause(cause) ? "Cause" : "Error", |
| 2383 | }), |
| 2384 | { |
| 2385 | status: "ok", |
| 2386 | } satisfies ArtifactSmokeRenderResult, |
| 2387 | ), |
| 2388 | ), |
| 2389 | ); |
| 2390 | const renderRejection = smokeRenderRejection(smokeResult); |
| 2391 | if (renderRejection) { |
| 2392 | yield* Effect.annotateCurrentSpan({ "mcp.artifact.smoke_render": "failed" }); |
| 2393 | return renderRejectedResult(renderRejection); |
| 2394 | } |
| 2395 | // Fail open, exactly as the verdict does: a preview that cannot be |
| 2396 | // produced or cannot be sanitized is a card that falls back to its |
| 2397 | // schematic, never a create that is refused. |
| 2398 | preview = |
| 2399 | smokeResult.status === "ok" && smokeResult.markup !== undefined |
| 2400 | ? sanitizeArtifactPreviewMarkup(smokeResult.markup) |
| 2401 | : null; |
| 2402 | } |
| 2403 | |
| 2404 | const saveInput = { |
| 2405 | code: input.code, |
| 2406 | title: input.title, |
| 2407 | preview, |
| 2408 | ...(input.description === undefined ? {} : { description: input.description }), |
no test coverage detected