|
| 1 | +import { Module, NAME, Recipe, TYPE, UI } from "@commontools/builder"; |
| 2 | +import { getDoc, type DocLink, DocImpl, EntityId, idle, createRef, getRecipe, isDoc, isDocLink, run } from "@commontools/runner"; |
| 3 | +import { createStorage } from "./storage.js"; |
| 4 | + |
| 5 | +export type Charm = { |
| 6 | + [NAME]?: string; |
| 7 | + [UI]?: any; |
| 8 | + [TYPE]?: string; |
| 9 | + [key: string]: any; |
| 10 | +}; |
| 11 | + |
| 12 | +export const storage = createStorage((import.meta as any).env.VITE_STORAGE_TYPE ?? "memory"); |
| 13 | +export const charms = getDoc<DocLink[]>([], "charms"); |
| 14 | +(window as any).charms = charms; |
| 15 | + |
| 16 | +export async function addCharms(newCharms: DocImpl<any>[]) { |
| 17 | + await storage.syncCell(charms); |
| 18 | + |
| 19 | + await idle(); |
| 20 | + |
| 21 | + const currentCharmsIds = charms.get().map(({ cell }) => JSON.stringify(cell.entityId)); |
| 22 | + const charmsToAdd = newCharms.filter( |
| 23 | + (cell) => !currentCharmsIds.includes(JSON.stringify(cell.entityId)), |
| 24 | + ); |
| 25 | + |
| 26 | + if (charmsToAdd.length > 0) { |
| 27 | + charms.send([ |
| 28 | + ...charms.get(), |
| 29 | + ...charmsToAdd.map((cell) => ({ cell, path: [] }) satisfies DocLink), |
| 30 | + ]); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export function removeCharm(id: EntityId) { |
| 35 | + const newCharms = charms.get().filter(({ cell }) => cell.entityId !== id); |
| 36 | + if (newCharms.length !== charms.get().length) charms.send(newCharms); |
| 37 | +} |
| 38 | + |
| 39 | +export async function runPersistent( |
| 40 | + recipe: Recipe | Module, |
| 41 | + inputs?: any, |
| 42 | + cause?: any, |
| 43 | +): Promise<DocImpl<any>> { |
| 44 | + await idle(); |
| 45 | + |
| 46 | + // Fill in missing parameters from other charms. It's a simple match on |
| 47 | + // hashtags: For each top-level argument prop that has a hashtag in the |
| 48 | + // description, look for a charm that has a top-level output prop with the |
| 49 | + // same hashtag in the description, or has the hashtag in its own description. |
| 50 | + // If there is a match, assign the first one to the input property. |
| 51 | + |
| 52 | + // TODO: This should really be extracted into a full-fledged query builder. |
| 53 | + if ( |
| 54 | + !isDoc(inputs) && // Adding to a cell input is not supported yet |
| 55 | + !isDocLink(inputs) && // Neither for cell reference |
| 56 | + recipe.argumentSchema && |
| 57 | + (recipe.argumentSchema as any).type === "object" |
| 58 | + ) { |
| 59 | + const properties = (recipe.argumentSchema as any).properties; |
| 60 | + const inputProperties = |
| 61 | + typeof inputs === "object" && inputs !== null ? Object.keys(inputs) : []; |
| 62 | + for (const key in properties) { |
| 63 | + if (!(key in inputProperties) && properties[key].description?.includes("#")) { |
| 64 | + const hashtag = properties[key].description.match(/#(\w+)/)?.[1]; |
| 65 | + if (hashtag) { |
| 66 | + charms.get().forEach(({ cell }) => { |
| 67 | + const type = cell.sourceCell?.get()?.[TYPE]; |
| 68 | + const recipe = getRecipe(type); |
| 69 | + const charmProperties = (recipe?.resultSchema as any)?.properties as any; |
| 70 | + const matchingProperty = Object.keys(charmProperties ?? {}).find((property) => |
| 71 | + charmProperties[property].description?.includes(`#${hashtag}`), |
| 72 | + ); |
| 73 | + if (matchingProperty) { |
| 74 | + inputs = { |
| 75 | + ...inputs, |
| 76 | + [key]: { $alias: { cell, path: [matchingProperty] } }, |
| 77 | + }; |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return run(recipe, inputs, await storage.syncCell(createRef({ recipe, inputs }, cause))); |
| 86 | +} |
| 87 | + |
| 88 | +export async function syncCharm( |
| 89 | + entityId: string | EntityId | DocImpl<any>, |
| 90 | + waitForStorage: boolean = false, |
| 91 | +): Promise<DocImpl<Charm>> { |
| 92 | + return storage.syncCell(entityId, waitForStorage); |
| 93 | +} |
0 commit comments