Skip to content

Commit 7441c3b

Browse files
committed
Minimum-viable spell casting implementation
Finds a spell based on search and runs it using compatible data
1 parent 81eebac commit 7441c3b

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

typescript/packages/lookslike-high-level/src/components/window-manager.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616

1717
import {
1818
addRecipe,
19-
effect,
2019
DocImpl,
20+
effect,
2121
EntityId,
2222
getEntityId,
2323
getRecipe,
@@ -31,6 +31,58 @@ import * as Schema from "../schema.js";
3131
import { buildRecipe } from "../localBuild.js";
3232
import * as iframeSpellAi from "./iframe-spell-ai.js";
3333

34+
async function castSpell(value: string, openCharm: (charmId: string) => void) {
35+
const searchUrl =
36+
typeof window !== "undefined"
37+
? window.location.protocol + "//" + window.location.host + "/api/ai/spell/search"
38+
: "//api/ai/spell/search";
39+
40+
// Search for suggested spells based on input
41+
const response = await fetch(searchUrl, {
42+
method: "POST",
43+
headers: {
44+
"Content-Type": "application/json",
45+
accepts: "application/json",
46+
},
47+
body: JSON.stringify({
48+
query: value,
49+
tags: [],
50+
options: {
51+
limit: 10,
52+
includeCompatibility: true,
53+
},
54+
}),
55+
});
56+
57+
if (response.ok) {
58+
const searchResponse: {
59+
spells: {
60+
key: string;
61+
name: string;
62+
description: string;
63+
compatibleBlobs: { data: unknown; key: string; snippet: string }[];
64+
}[];
65+
blobs: string[];
66+
} = await response.json();
67+
console.log("Search response:", searchResponse);
68+
69+
let recipeKey = searchResponse.spells?.[0]?.key;
70+
let blob = searchResponse.spells?.[0]?.compatibleBlobs?.[0];
71+
72+
if (recipeKey && blob) {
73+
const recipeId = recipeKey.replace("spell-", "");
74+
await syncRecipe(recipeId);
75+
76+
const recipe = getRecipe(recipeId);
77+
if (!recipe) return;
78+
79+
const charm: DocImpl<Charm> = await runPersistent(recipe, blob.data);
80+
addCharms([charm]);
81+
openCharm(JSON.stringify(charm.entityId));
82+
}
83+
}
84+
}
85+
3486
@customElement("common-window-manager")
3587
export class CommonWindowManager extends LitElement {
3688
static override styles = [
@@ -166,7 +218,11 @@ export class CommonWindowManager extends LitElement {
166218
const shiftKey = event.detail.shiftKey;
167219
console.log("Unibox submitted:", value, shiftKey);
168220

169-
iframeSpellAi.iterate(this.focusedCharm, value, shiftKey);
221+
if (this.focusedCharm) {
222+
iframeSpellAi.iterate(this.focusedCharm, value, shiftKey);
223+
} else {
224+
castSpell(value, this.openCharm.bind(this));
225+
}
170226
}
171227

172228
input: string = "";

0 commit comments

Comments
 (0)