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

Function artifactsSave

packages/core/sdk/src/executor.ts:3972–4024  ·  view source on GitHub ↗
(
      input: SaveArtifactInput,
    )

Source from the content-addressed store, hash-verified

3970 });
3971
3972 const artifactsSave = (
3973 input: SaveArtifactInput,
3974 ): Effect.Effect<Artifact, ArtifactNotFoundError | StorageFailure> =>
3975 Effect.gen(function* () {
3976 const now = new Date();
3977 const description = input.description ?? null;
3978 // An explicit id overwrites in place (v1 keeps no version history). It
3979 // must already resolve to a visible row: minting a caller-chosen id
3980 // would let a stale client resurrect a deleted artifact silently.
3981 if (input.id !== undefined) {
3982 const where = artifactById(input.id);
3983 const existing = yield* core.findFirst("artifact", { where });
3984 if (!existing) {
3985 return yield* new ArtifactNotFoundError({ id: ArtifactId.make(input.id) });
3986 }
3987 // `bindings` is written on every overwrite, including back to null:
3988 // it interprets `code`, so carrying the previous value forward under
3989 // new source would bind roles the new code never declares. `preview`
3990 // is written for exactly the same reason, and the case it prevents is
3991 // visible: an image preview captured from the OLD render would
3992 // otherwise keep advertising a version of the artifact that no longer
3993 // exists.
3994 const set = {
3995 title: input.title,
3996 description,
3997 code: input.code,
3998 bindings: input.bindings ?? null,
3999 preview: input.preview ?? null,
4000 updated_at: now,
4001 };
4002 yield* core.updateMany("artifact", { where, set });
4003 return rowToArtifact({ ...existing, ...set });
4004 }
4005 yield* requireUserSubject("user");
4006 const keys = yield* Effect.try({
4007 try: () => ownedKeys("user"),
4008 catch: (cause) => storageFailureFromUnknown("invalid owner", cause),
4009 });
4010 const created = yield* core.create("artifact", {
4011 tenant: keys.tenant,
4012 owner: keys.owner,
4013 subject: keys.subject,
4014 id: `art_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`,
4015 title: input.title,
4016 description,
4017 code: input.code,
4018 bindings: input.bindings ?? null,
4019 preview: input.preview ?? null,
4020 created_at: now,
4021 updated_at: now,
4022 });
4023 return rowToArtifact(created);
4024 });
4025
4026 /**
4027 * Replace an artifact's preview with a snapshot of a settled render.

Callers

nothing calls this directly

Calls 6

rowToArtifactFunction · 0.90
artifactByIdFunction · 0.85
requireUserSubjectFunction · 0.85
ownedKeysFunction · 0.85
toStringMethod · 0.80

Tested by

no test coverage detected