|
| 1 | +import { ITool } from "./tool"; |
| 2 | + |
| 3 | +/** |
| 4 | + * A select option definition for twist configuration. |
| 5 | + * Renders as a dropdown in the Flutter UI. |
| 6 | + */ |
| 7 | +export type SelectDef = { |
| 8 | + type: "select"; |
| 9 | + label: string; |
| 10 | + description?: string; |
| 11 | + choices: ReadonlyArray<{ value: string; label: string }>; |
| 12 | + default: string; |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * A text input option definition for twist configuration. |
| 17 | + * Renders as a text field in the Flutter UI. |
| 18 | + */ |
| 19 | +export type TextDef = { |
| 20 | + type: "text"; |
| 21 | + label: string; |
| 22 | + description?: string; |
| 23 | + default: string; |
| 24 | + placeholder?: string; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * A number input option definition for twist configuration. |
| 29 | + * Renders as a number field in the Flutter UI. |
| 30 | + */ |
| 31 | +export type NumberDef = { |
| 32 | + type: "number"; |
| 33 | + label: string; |
| 34 | + description?: string; |
| 35 | + default: number; |
| 36 | + min?: number; |
| 37 | + max?: number; |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * A boolean toggle option definition for twist configuration. |
| 42 | + * Renders as a switch in the Flutter UI. |
| 43 | + */ |
| 44 | +export type BooleanDef = { |
| 45 | + type: "boolean"; |
| 46 | + label: string; |
| 47 | + description?: string; |
| 48 | + default: boolean; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Union of all option definition types. |
| 53 | + */ |
| 54 | +export type OptionDef = SelectDef | TextDef | NumberDef | BooleanDef; |
| 55 | + |
| 56 | +/** |
| 57 | + * Schema defining all configurable options for a twist. |
| 58 | + * Each key maps to an option definition that describes its type, label, and default value. |
| 59 | + */ |
| 60 | +export type OptionsSchema = Record<string, OptionDef>; |
| 61 | + |
| 62 | +/** |
| 63 | + * Infers the resolved value types from an options schema. |
| 64 | + * Boolean options resolve to `boolean`, number options to `number`, |
| 65 | + * and select/text options to `string`. |
| 66 | + */ |
| 67 | +export type ResolvedOptions<T extends OptionsSchema> = { |
| 68 | + [K in keyof T]: T[K] extends BooleanDef |
| 69 | + ? boolean |
| 70 | + : T[K] extends NumberDef |
| 71 | + ? number |
| 72 | + : string; |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | + * Built-in marker class for twist configuration options. |
| 77 | + * |
| 78 | + * Declare options in your twist's `build()` method to expose configurable |
| 79 | + * settings to users. The schema is introspected at deploy time and stored |
| 80 | + * alongside permissions. At runtime, user values are merged with defaults. |
| 81 | + * |
| 82 | + * @example |
| 83 | + * ```typescript |
| 84 | + * import { Options, type OptionsSchema } from "@plotday/twister/options"; |
| 85 | + * |
| 86 | + * export default class MyTwist extends Twist<MyTwist> { |
| 87 | + * build(build: ToolBuilder) { |
| 88 | + * return { |
| 89 | + * options: build(Options, { |
| 90 | + * model: { |
| 91 | + * type: 'select', |
| 92 | + * label: 'AI Model', |
| 93 | + * choices: [ |
| 94 | + * { value: 'fast', label: 'Fast' }, |
| 95 | + * { value: 'smart', label: 'Smart' }, |
| 96 | + * ], |
| 97 | + * default: 'fast', |
| 98 | + * }, |
| 99 | + * }), |
| 100 | + * // ... other tools |
| 101 | + * }; |
| 102 | + * } |
| 103 | + * |
| 104 | + * async respond(note: Note) { |
| 105 | + * const model = this.tools.options.model; // typed as string |
| 106 | + * } |
| 107 | + * } |
| 108 | + * ``` |
| 109 | + */ |
| 110 | +export abstract class Options extends ITool { |
| 111 | + static readonly toolId = "Options"; |
| 112 | +} |
0 commit comments