Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(widgets): read a node's children and name a missing type

David Neyra43f2a7ebfed3
diff --git a/packages/phone-protocol/src/widget-input.ts b/packages/phone-protocol/src/widget-input.ts
new file mode 100644
index 0000000000..995f28b93a
--- /dev/null
+++ b/packages/phone-protocol/src/widget-input.ts
@@ -0,0 +1,20 @@
+// How a model's widget input is read: the children a node names, and what is wrong with a node's type.
+export function childNodes(raw: Record<string, unknown>): unknown[] {
+ const children = raw.children;
+ if (children === undefined || children === null) return [];
+ if (Array.isArray(children)) return children;
+ if (typeof children !== "object") return [children];
+ const keys = Object.keys(children);
+ if (keys.length !== 1) return [children];
+ const only = children[keys[0]!];
+ if (Array.isArray(only)) return only;
+ if (typeof only === "object" && only !== null) return [only];
+ return [children];
+}
+
+/** What is wrong with a node's type, in one sentence that names what to write. */
+export function nodeTypeProblem(raw: Record<string, unknown>, known: string): string {
+ return raw.type === undefined
+ ? `has no type; use one of ${known}`
+ : `unknown type ${JSON.stringify(raw.type)}; use one of ${known}`;
+}