rspace-online/lib/data-types.ts

63 lines
1.7 KiB
TypeScript

/**
* Data type system for shape-to-shape data piping.
*
* Shapes declare typed input/output ports. Arrows connect ports,
* flowing data from source outputs to target inputs with type checking.
*/
/** Supported data types for ports. */
export type DataType =
| "string"
| "number"
| "boolean"
| "image-url"
| "video-url"
| "text"
| "json"
| "trigger"
| "any";
/** Describes a single input or output port on a shape. */
export interface PortDescriptor {
name: string;
type: DataType;
direction: "input" | "output";
}
/** Type compatibility matrix: can `source` flow into `target`? */
export function isCompatible(source: DataType, target: DataType): boolean {
if (source === target) return true;
if (target === "any" || source === "any") return true;
// text and string are interchangeable
if ((source === "text" && target === "string") || (source === "string" && target === "text")) return true;
// URLs are strings
if ((source === "image-url" || source === "video-url") && (target === "string" || target === "text")) return true;
// strings can be image/video URLs (user knows best)
if ((source === "string" || source === "text") && (target === "image-url" || target === "video-url")) return true;
return false;
}
/** Color tint per data type for arrow visualization. */
export function dataTypeColor(type: DataType): string {
switch (type) {
case "text":
case "string":
return "#3b82f6"; // blue
case "number":
return "#f59e0b"; // amber
case "boolean":
return "#8b5cf6"; // purple
case "image-url":
return "#10b981"; // emerald
case "video-url":
return "#ef4444"; // red
case "json":
return "#6366f1"; // indigo
case "trigger":
return "#f97316"; // orange
case "any":
default:
return "#6b7280"; // gray
}
}