@gpambrozio/paseo-herald 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,171 @@
1
+ /**
2
+ * A settings row that opens a searchable, scrolling list — for the choices
3
+ * that do not fit the host's `SettingsSelect`, whose popover does not scroll.
4
+ * A Mac lists well over a hundred `say` voices in fifty languages; a browser
5
+ * lists dozens; a daemon lists every model of every provider.
6
+ *
7
+ * Built only from host pieces: `Modal` and `Modal.Content` with scrolling
8
+ * off so the host `FlatList` is the one that scrolls, and the host `TextInput`
9
+ * so the keyboard and the sheet get along on a phone.
10
+ */
11
+ import type { PluginTheme } from "@getpaseo/plugin";
12
+ import { FlatList, Icon, Modal, TextInput } from "@getpaseo/plugin/client/react-native";
13
+ import { SettingsRow } from "@getpaseo/plugin/client/ui";
14
+ import { useMemo, useState } from "react";
15
+ import { Pressable, Text, View } from "react-native";
16
+
17
+ export interface PickerOption {
18
+ label: string;
19
+ value: string;
20
+ /** A second, muted line: the language of a voice, the provider of a model. */
21
+ detail?: string;
22
+ }
23
+
24
+ interface OptionPickerProps {
25
+ label: string;
26
+ hint?: string;
27
+ /** The modal's title. */
28
+ title: string;
29
+ value: string;
30
+ options: readonly PickerOption[];
31
+ disabled?: boolean;
32
+ searchPlaceholder?: string;
33
+ onSelect(value: string): void;
34
+ theme: PluginTheme;
35
+ compact: boolean;
36
+ }
37
+
38
+ function withAlpha(color: string, alpha: string): string {
39
+ return /^#[0-9a-fA-F]{6}$/.test(color) ? `${color}${alpha}` : color;
40
+ }
41
+
42
+ export function OptionPicker(props: OptionPickerProps) {
43
+ const { theme, compact, options, value, onSelect } = props;
44
+ const [open, setOpen] = useState(false);
45
+ const [query, setQuery] = useState("");
46
+
47
+ const current = options.find((option) => option.value === value) ?? null;
48
+ const filtered = useMemo(() => {
49
+ const needle = query.trim().toLowerCase();
50
+ if (needle === "") return options;
51
+ return options.filter((option) =>
52
+ `${option.label} ${option.detail ?? ""} ${option.value}`.toLowerCase().includes(needle),
53
+ );
54
+ }, [options, query]);
55
+
56
+ const styles = useMemo(() => {
57
+ const { colors } = theme;
58
+ const separator = withAlpha(colors.foregroundMuted, "33");
59
+ return {
60
+ trigger: {
61
+ flexDirection: "row" as const,
62
+ alignItems: "center" as const,
63
+ gap: 6,
64
+ maxWidth: compact ? 180 : 280,
65
+ paddingHorizontal: 10,
66
+ paddingVertical: 6,
67
+ borderRadius: 8,
68
+ borderWidth: 1,
69
+ borderColor: separator,
70
+ backgroundColor: colors.surface1,
71
+ },
72
+ triggerText: { color: colors.foreground, fontSize: 13, flexShrink: 1 },
73
+ triggerDisabled: { opacity: 0.5 },
74
+ body: { flex: 1 },
75
+ content: { flex: 1, padding: compact ? 12 : 16, gap: 10 },
76
+ search: {
77
+ color: colors.foreground,
78
+ fontSize: 15,
79
+ paddingHorizontal: 12,
80
+ paddingVertical: 10,
81
+ borderRadius: 8,
82
+ borderWidth: 1,
83
+ borderColor: separator,
84
+ backgroundColor: colors.surface1,
85
+ },
86
+ count: { color: colors.foregroundMuted, fontSize: 12 },
87
+ list: { flex: 1 },
88
+ row: {
89
+ flexDirection: "row" as const,
90
+ alignItems: "center" as const,
91
+ gap: 10,
92
+ paddingVertical: 10,
93
+ paddingHorizontal: 8,
94
+ borderBottomWidth: 1,
95
+ borderBottomColor: separator,
96
+ },
97
+ rowSelected: { backgroundColor: withAlpha(colors.accent, "1a"), borderRadius: 8 },
98
+ rowText: { flex: 1, gap: 2 },
99
+ rowLabel: { color: colors.foreground, fontSize: 15 },
100
+ rowDetail: { color: colors.foregroundMuted, fontSize: 12 },
101
+ empty: { color: colors.foregroundMuted, fontSize: 14, padding: 16, textAlign: "center" as const },
102
+ };
103
+ }, [theme, compact]);
104
+
105
+ const muted = theme.colors.foregroundMuted;
106
+
107
+ return (
108
+ <>
109
+ <SettingsRow label={props.label} {...(props.hint === undefined ? {} : { hint: props.hint })}>
110
+ <Pressable
111
+ accessibilityRole="button"
112
+ accessibilityLabel={`${props.label}: ${current?.label ?? (value === "" ? "not set" : value)}. Change`}
113
+ disabled={props.disabled === true}
114
+ onPress={() => {
115
+ setQuery("");
116
+ setOpen(true);
117
+ }}
118
+ style={[styles.trigger, props.disabled === true ? styles.triggerDisabled : null]}
119
+ >
120
+ <Text style={styles.triggerText} numberOfLines={1}>
121
+ {current?.label ?? (value === "" ? "Choose…" : value)}
122
+ </Text>
123
+ <Icon name="ChevronDown" size={14} color={muted} />
124
+ </Pressable>
125
+ </SettingsRow>
126
+ <Modal title={props.title} open={open} onOpenChange={setOpen}>
127
+ <Modal.Content scrollable={false} style={styles.body} contentContainerStyle={styles.content}>
128
+ <TextInput
129
+ value={query}
130
+ onChangeText={setQuery}
131
+ placeholder={props.searchPlaceholder ?? "Search"}
132
+ placeholderTextColor={muted}
133
+ autoCorrect={false}
134
+ autoCapitalize="none"
135
+ autoFocus={!compact}
136
+ style={styles.search}
137
+ />
138
+ <Text style={styles.count}>
139
+ {filtered.length === options.length
140
+ ? `${options.length} choices`
141
+ : `${filtered.length} of ${options.length}`}
142
+ </Text>
143
+ <FlatList
144
+ data={filtered}
145
+ keyExtractor={(option) => option.value}
146
+ style={styles.list}
147
+ keyboardShouldPersistTaps="handled"
148
+ ListEmptyComponent={<Text style={styles.empty}>Nothing matches.</Text>}
149
+ renderItem={({ item }) => (
150
+ <Pressable
151
+ accessibilityRole="button"
152
+ accessibilityLabel={item.detail === undefined ? item.label : `${item.label}, ${item.detail}`}
153
+ onPress={() => {
154
+ onSelect(item.value);
155
+ setOpen(false);
156
+ }}
157
+ style={[styles.row, item.value === value ? styles.rowSelected : null]}
158
+ >
159
+ <View style={styles.rowText}>
160
+ <Text style={styles.rowLabel}>{item.label}</Text>
161
+ {item.detail === undefined ? null : <Text style={styles.rowDetail}>{item.detail}</Text>}
162
+ </View>
163
+ {item.value === value ? <Icon name="Check" size={16} color={theme.colors.accent} /> : null}
164
+ </Pressable>
165
+ )}
166
+ />
167
+ </Modal.Content>
168
+ </Modal>
169
+ </>
170
+ );
171
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * A settings row that opens the summary prompt for editing. The host's
3
+ * `SettingsInput` is a single line, so — as with `client/option-picker.tsx` —
4
+ * the editor is a host `Modal` with scrolling off, holding a multiline host
5
+ * `TextInput` that takes the whole body.
6
+ *
7
+ * The draft is local until *Save*, so *Cancel* really cancels, and the daemon
8
+ * is not written on every keystroke. Saving an empty prompt is saving the
9
+ * default one; there is no such thing as no prompt.
10
+ *
11
+ * Async function expressions only; see `client/herald.tsx`.
12
+ */
13
+ import type { PluginTheme } from "@getpaseo/plugin";
14
+ import { Modal, TextInput } from "@getpaseo/plugin/client/react-native";
15
+ import { SettingsRow } from "@getpaseo/plugin/client/ui";
16
+ import { useMemo, useState } from "react";
17
+ import { Pressable, ScrollView, Text, View } from "react-native";
18
+
19
+ import { DEFAULT_SUMMARY_PROMPT, PROMPT_PLACEHOLDERS } from "../shared/herald";
20
+
21
+ interface PromptEditorProps {
22
+ label: string;
23
+ hint?: string;
24
+ /** The template as saved. Empty means the default one is in use. */
25
+ value: string;
26
+ disabled?: boolean;
27
+ onSave(next: string): void;
28
+ theme: PluginTheme;
29
+ compact: boolean;
30
+ }
31
+
32
+ function withAlpha(color: string, alpha: string): string {
33
+ return /^#[0-9a-fA-F]{6}$/.test(color) ? `${color}${alpha}` : color;
34
+ }
35
+
36
+ export function isDefaultPrompt(value: string): boolean {
37
+ return value.trim() === "" || value.trim() === DEFAULT_SUMMARY_PROMPT.trim();
38
+ }
39
+
40
+ /**
41
+ * What the model is asked to answer with is what `parseSummaryText` reads, so
42
+ * a prompt that stopped asking for it is worth a word — but not a refusal: the
43
+ * parser falls back to the reply as prose, which is a working, if less
44
+ * predictable, summary.
45
+ */
46
+ function promptWarning(draft: string): string | null {
47
+ if (draft.trim() === "") return null;
48
+ if (draft.includes("speech")) return null;
49
+ return 'This prompt no longer asks for a JSON object with a "speech" key. Herald will speak whatever the model replies, trimmed to the first 45 words.';
50
+ }
51
+
52
+ export function PromptEditor(props: PromptEditorProps) {
53
+ const { theme, compact, value, onSave } = props;
54
+ const [open, setOpen] = useState(false);
55
+ const [draft, setDraft] = useState(value);
56
+
57
+ const styles = useMemo(() => {
58
+ const { colors } = theme;
59
+ const separator = withAlpha(colors.foregroundMuted, "33");
60
+ return {
61
+ trigger: {
62
+ flexDirection: "row" as const,
63
+ alignItems: "center" as const,
64
+ paddingHorizontal: 10,
65
+ paddingVertical: 6,
66
+ borderRadius: 8,
67
+ borderWidth: 1,
68
+ borderColor: separator,
69
+ backgroundColor: colors.surface1,
70
+ },
71
+ triggerText: { color: colors.foreground, fontSize: 13 },
72
+ triggerDisabled: { opacity: 0.5 },
73
+ body: { flex: 1 },
74
+ content: { flex: 1, padding: compact ? 12 : 16, gap: 10 },
75
+ editor: {
76
+ flex: 1,
77
+ minHeight: 180,
78
+ color: colors.foreground,
79
+ fontSize: 13,
80
+ lineHeight: 19,
81
+ fontFamily: "monospace" as const,
82
+ textAlignVertical: "top" as const,
83
+ padding: 12,
84
+ borderRadius: 8,
85
+ borderWidth: 1,
86
+ borderColor: separator,
87
+ backgroundColor: colors.surface1,
88
+ },
89
+ legend: { maxHeight: compact ? 120 : 160 },
90
+ legendTitle: { color: colors.foreground, fontSize: 13, fontWeight: "600" as const },
91
+ legendNote: { color: colors.foregroundMuted, fontSize: 12, lineHeight: 17 },
92
+ legendRow: { flexDirection: "row" as const, gap: 8, paddingVertical: 3 },
93
+ legendName: { color: colors.accent, fontSize: 12, fontFamily: "monospace" as const, width: 108 },
94
+ legendText: { color: colors.foregroundMuted, fontSize: 12, flex: 1, lineHeight: 17 },
95
+ warning: { color: colors.statusWarning, fontSize: 12, lineHeight: 17 },
96
+ buttons: { flexDirection: "row" as const, alignItems: "center" as const, gap: 8 },
97
+ spacer: { flex: 1 },
98
+ button: {
99
+ paddingHorizontal: 14,
100
+ paddingVertical: 8,
101
+ borderRadius: 8,
102
+ borderWidth: 1,
103
+ borderColor: separator,
104
+ backgroundColor: colors.surface1,
105
+ },
106
+ buttonText: { color: colors.foreground, fontSize: 13 },
107
+ primary: { backgroundColor: colors.accent, borderColor: colors.accent },
108
+ primaryText: { color: colors.accentForeground, fontSize: 13, fontWeight: "600" as const },
109
+ };
110
+ }, [theme, compact]);
111
+
112
+ const warning = promptWarning(draft);
113
+
114
+ return (
115
+ <>
116
+ <SettingsRow label={props.label} {...(props.hint === undefined ? {} : { hint: props.hint })}>
117
+ <Pressable
118
+ accessibilityRole="button"
119
+ accessibilityLabel={`${props.label}: ${isDefaultPrompt(value) ? "the default" : "customised"}. Edit`}
120
+ disabled={props.disabled === true}
121
+ onPress={() => {
122
+ setDraft(value.trim() === "" ? DEFAULT_SUMMARY_PROMPT : value);
123
+ setOpen(true);
124
+ }}
125
+ style={[styles.trigger, props.disabled === true ? styles.triggerDisabled : null]}
126
+ >
127
+ <Text style={styles.triggerText}>{isDefaultPrompt(value) ? "Default — edit…" : "Customised — edit…"}</Text>
128
+ </Pressable>
129
+ </SettingsRow>
130
+ <Modal title={props.label} open={open} onOpenChange={setOpen}>
131
+ <Modal.Content scrollable={false} style={styles.body} contentContainerStyle={styles.content}>
132
+ <TextInput
133
+ value={draft}
134
+ onChangeText={setDraft}
135
+ multiline
136
+ autoCorrect={false}
137
+ autoCapitalize="none"
138
+ spellCheck={false}
139
+ placeholder={DEFAULT_SUMMARY_PROMPT}
140
+ placeholderTextColor={theme.colors.foregroundMuted}
141
+ style={styles.editor}
142
+ />
143
+ <ScrollView style={styles.legend}>
144
+ <Text style={styles.legendTitle}>Placeholders</Text>
145
+ <Text style={styles.legendNote}>
146
+ Herald fills each of these in before sending the prompt. A line whose placeholder has
147
+ nothing to fill it for that event is left out whole, so a label and its placeholder
148
+ belong on one line. Anything else in double braces is sent as you typed it.
149
+ </Text>
150
+ {PROMPT_PLACEHOLDERS.map((placeholder) => (
151
+ <View key={placeholder.name} style={styles.legendRow}>
152
+ <Text style={styles.legendName}>{`{{${placeholder.name}}}`}</Text>
153
+ <Text style={styles.legendText}>{placeholder.description}</Text>
154
+ </View>
155
+ ))}
156
+ </ScrollView>
157
+ {warning === null ? null : <Text style={styles.warning}>{warning}</Text>}
158
+ <View style={styles.buttons}>
159
+ <Pressable
160
+ accessibilityRole="button"
161
+ onPress={() => setDraft(DEFAULT_SUMMARY_PROMPT)}
162
+ style={styles.button}
163
+ >
164
+ <Text style={styles.buttonText}>Restore the default</Text>
165
+ </Pressable>
166
+ <View style={styles.spacer} />
167
+ <Pressable accessibilityRole="button" onPress={() => setOpen(false)} style={styles.button}>
168
+ <Text style={styles.buttonText}>Cancel</Text>
169
+ </Pressable>
170
+ <Pressable
171
+ accessibilityRole="button"
172
+ onPress={() => {
173
+ onSave(isDefaultPrompt(draft) ? "" : draft);
174
+ setOpen(false);
175
+ }}
176
+ style={[styles.button, styles.primary]}
177
+ >
178
+ <Text style={styles.primaryText}>Save</Text>
179
+ </Pressable>
180
+ </View>
181
+ </Modal.Content>
182
+ </Modal>
183
+ </>
184
+ );
185
+ }