Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions typescript/packages/common-charm/src/charm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ export class CharmManager {
}

// Return Cell with argument content according to the schema of the charm.
getArgument<T = any>(charm: Cell<Charm | T>): T {
getArgument<T = any>(charm: Cell<Charm | T>): Cell<T> | undefined {
const source = charm.getSourceCell(processSchema);
const recipeId = source?.get()?.[TYPE];
const recipe = getRecipe(recipeId);
const argumentSchema = recipe?.argumentSchema;
return source?.key("argument").asSchema(argumentSchema!) as T;
return source?.key("argument").asSchema(argumentSchema!) as
| Cell<T>
| undefined;
}

// note: removing a charm doesn't clean up the charm's cells
Expand Down
33 changes: 21 additions & 12 deletions typescript/packages/jumble/src/components/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,21 @@ export interface SelectOption {
export const castSpellAsCharm = async (
charmManager: CharmManager,
recipeKey: string,
blobId: string,
argument: Cell<any>,
) => {
if (recipeKey && blobId) {
if (recipeKey && argument) {
console.log("Syncing...");
const recipeId = recipeKey.replace("spell-", "");
await charmManager.syncRecipeBlobby(recipeId);

const recipe = getRecipe(recipeId);
if (!recipe) return;

console.log("Syncing blob...");
const cell = await charmManager.getCellById({ "/": blobId }, ["argument"]);
console.log("Casting...");
const charm: Cell<Charm> = await charmManager.runPersistent(recipe, cell);
const charm: Cell<Charm> = await charmManager.runPersistent(
recipe,
argument,
);
charmManager.add([charm]);
return charm.entityId;
}
Expand Down Expand Up @@ -495,17 +496,17 @@ async function handleUseDataInSpell(deps: CommandContext) {
}

const charm = await deps.charmManager.get(deps.focusedCharmId);
const sourceCell = charm?.getSourceCell();

const sourceId = getEntityId(sourceCell)?.["/"];
if (!sourceId) {
throw new Error("No source ID found");
if (!charm) throw new Error("No current charm found");
const argument = deps.charmManager.getArgument(charm);
if (!argument) {
throw new Error("No sourceCell/argument found for current charm");
}

deps.setLoading(true);
const newCharm = await castSpellAsCharm(
deps.charmManager,
selectedSpell.id,
sourceId,
argument,
);

if (!newCharm) {
Expand Down Expand Up @@ -578,10 +579,18 @@ async function handleUseSpellOnOtherData(deps: CommandContext) {
}

deps.setLoading(true);

console.log("Syncing blob...");
// TODO(ben,seefeld): We might want spellcaster to return docId/path
// pairs and use those directly instead of hardcoding `argument` here.
const argument = await deps.charmManager.getCellById({
"/": selectedCell.id,
}, ["argument"]);

const newCharm = await castSpellAsCharm(
deps.charmManager,
spellId,
selectedCell.id,
argument,
);
if (!newCharm) throw new Error("Failed to cast spell");

Expand Down