@koinzhang/paseo-plugin-commands 0.1.0
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.
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/client/agent-state.ts +79 -0
- package/client/commands.ts +209 -0
- package/client/menu-list.tsx +194 -0
- package/client/picker.tsx +318 -0
- package/client/settings-screen.tsx +81 -0
- package/client/settings-store.ts +22 -0
- package/index.client.tsx +35 -0
- package/index.server.ts +12 -0
- package/package.json +42 -0
- package/paseo-plugin.json +7 -0
- package/server/control.ts +53 -0
- package/server/daemon.ts +85 -0
- package/shared/commands.ts +376 -0
- package/shared/parse.ts +23 -0
- package/shared/popover-geometry.ts +66 -0
- package/shared/resend.ts +26 -0
- package/shared/resolve-option.ts +80 -0
- package/shared/rpc.ts +42 -0
- package/shared/settings.ts +13 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PluginButton,
|
|
3
|
+
PluginButtonIconProps,
|
|
4
|
+
PluginButtonRegistration,
|
|
5
|
+
PluginClientContext,
|
|
6
|
+
} from "@getpaseo/plugin/client";
|
|
7
|
+
import { Icon, Modal as HostModal, useToast } from "@getpaseo/plugin/client/react-native";
|
|
8
|
+
import type { PluginTheme } from "@getpaseo/plugin";
|
|
9
|
+
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type RefObject } from "react";
|
|
10
|
+
import {
|
|
11
|
+
Animated,
|
|
12
|
+
Modal,
|
|
13
|
+
Platform,
|
|
14
|
+
Pressable,
|
|
15
|
+
ScrollView,
|
|
16
|
+
StatusBar,
|
|
17
|
+
View,
|
|
18
|
+
useWindowDimensions,
|
|
19
|
+
} from "react-native";
|
|
20
|
+
import type { Plan } from "../shared/commands.ts";
|
|
21
|
+
import {
|
|
22
|
+
pillRectFromIcon,
|
|
23
|
+
placePopover,
|
|
24
|
+
visibleHeight,
|
|
25
|
+
type Rect,
|
|
26
|
+
type Size,
|
|
27
|
+
} from "../shared/popover-geometry.ts";
|
|
28
|
+
import type { AgentControl } from "../shared/rpc.ts";
|
|
29
|
+
import { MenuList, type PickerLeaf } from "./menu-list.tsx";
|
|
30
|
+
|
|
31
|
+
type PickerPlan = Extract<Plan, { kind: "picker" }>;
|
|
32
|
+
/** Applies a control and resolves to a message to show, if any. */
|
|
33
|
+
export type RunControl = (control: AgentControl, note?: string) => Promise<string | null>;
|
|
34
|
+
|
|
35
|
+
export interface PickerTarget {
|
|
36
|
+
workspaceId: string;
|
|
37
|
+
agentId: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface PickerState {
|
|
41
|
+
plan: PickerPlan;
|
|
42
|
+
run: RunControl;
|
|
43
|
+
open: boolean;
|
|
44
|
+
close(): void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Paseo's composer pill menu: above the pill, start-aligned, 12px away. */
|
|
48
|
+
const POPOVER = { offset: 12, minWidth: 280, maxWidth: 420, maxHeight: 440 } as const;
|
|
49
|
+
const ENTER_MS = 150;
|
|
50
|
+
|
|
51
|
+
const states = new Map<string, PickerState>();
|
|
52
|
+
const listeners = new Set<() => void>();
|
|
53
|
+
|
|
54
|
+
function setState(agentId: string, state: PickerState | null) {
|
|
55
|
+
if (state) states.set(agentId, state);
|
|
56
|
+
else states.delete(agentId);
|
|
57
|
+
for (const listener of listeners) listener();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function subscribe(listener: () => void) {
|
|
61
|
+
listeners.add(listener);
|
|
62
|
+
return () => {
|
|
63
|
+
listeners.delete(listener);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function usePickerState(agentId: string): PickerState | undefined {
|
|
68
|
+
return useSyncExternalStore(
|
|
69
|
+
subscribe,
|
|
70
|
+
() => states.get(agentId),
|
|
71
|
+
() => states.get(agentId),
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isDark(color: string): boolean {
|
|
76
|
+
const hex = /^#([0-9a-f]{6})$/i.exec(color)?.[1];
|
|
77
|
+
if (!hex) return true;
|
|
78
|
+
const [r, g, b] = [0, 2, 4].map((index) => parseInt(hex.slice(index, index + 2), 16) / 255);
|
|
79
|
+
return 0.2126 * (r ?? 0) + 0.7152 * (g ?? 0) + 0.0722 * (b ?? 0) < 0.5;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Paseo menu surface: surface1, outlined, 8px radius, the theme's medium shadow. */
|
|
83
|
+
function surfaceStyle(theme: PluginTheme) {
|
|
84
|
+
const dark = isDark(theme.colors.surface0);
|
|
85
|
+
return {
|
|
86
|
+
position: "absolute" as const,
|
|
87
|
+
minWidth: POPOVER.minWidth,
|
|
88
|
+
maxWidth: POPOVER.maxWidth,
|
|
89
|
+
backgroundColor: theme.colors.surface1,
|
|
90
|
+
borderWidth: 1,
|
|
91
|
+
borderColor: theme.colors.border,
|
|
92
|
+
borderRadius: 8,
|
|
93
|
+
overflow: "hidden" as const,
|
|
94
|
+
shadowColor: dark ? "rgba(0, 0, 0, 0.20)" : "rgba(0, 0, 0, 0.04)",
|
|
95
|
+
shadowOffset: { width: 0, height: 4 },
|
|
96
|
+
shadowRadius: dark ? 8 : 16,
|
|
97
|
+
elevation: dark ? 8 : 4,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Selecting closes the picker first and reports the outcome as a toast, like Paseo's menus. */
|
|
102
|
+
function useChoose(state: PickerState) {
|
|
103
|
+
const toast = useToast();
|
|
104
|
+
return (entry: PickerLeaf) => {
|
|
105
|
+
if (!entry.control) return;
|
|
106
|
+
const { control, note } = entry;
|
|
107
|
+
state.close();
|
|
108
|
+
state.run(control, note).then(
|
|
109
|
+
(message) => {
|
|
110
|
+
if (message) toast.show(message, { variant: "warning" });
|
|
111
|
+
},
|
|
112
|
+
(error: unknown) => toast.error(error instanceof Error ? error.message : String(error)),
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function PickerPopover({
|
|
118
|
+
anchor,
|
|
119
|
+
state,
|
|
120
|
+
theme,
|
|
121
|
+
}: {
|
|
122
|
+
anchor: RefObject<View | null>;
|
|
123
|
+
state: PickerState;
|
|
124
|
+
theme: PluginTheme;
|
|
125
|
+
}) {
|
|
126
|
+
const choose = useChoose(state);
|
|
127
|
+
const viewport = useWindowDimensions();
|
|
128
|
+
const [trigger, setTrigger] = useState<Rect | null>(null);
|
|
129
|
+
const [content, setContent] = useState<Size | null>(null);
|
|
130
|
+
const progress = useRef(new Animated.Value(0)).current;
|
|
131
|
+
const surface = useMemo(() => surfaceStyle(theme), [theme]);
|
|
132
|
+
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (!state.open) {
|
|
135
|
+
setTrigger(null);
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
// Android Modal content starts under a translucent status bar; measureInWindow does not.
|
|
139
|
+
const statusBar = Platform.OS === "android" ? (StatusBar.currentHeight ?? 0) : 0;
|
|
140
|
+
let cancelled = false;
|
|
141
|
+
anchor.current?.measureInWindow((x, y, width, height) => {
|
|
142
|
+
if (!cancelled) setTrigger(pillRectFromIcon({ x, y: y + statusBar, width, height }));
|
|
143
|
+
});
|
|
144
|
+
return () => {
|
|
145
|
+
cancelled = true;
|
|
146
|
+
};
|
|
147
|
+
}, [anchor, state.open, viewport.width, viewport.height]);
|
|
148
|
+
|
|
149
|
+
const visible = content
|
|
150
|
+
? { width: content.width, height: visibleHeight(content.height, POPOVER.maxHeight, viewport.height) }
|
|
151
|
+
: null;
|
|
152
|
+
const position =
|
|
153
|
+
trigger && visible
|
|
154
|
+
? placePopover({ trigger, content: visible, window: viewport, side: "top", offset: POPOVER.offset })
|
|
155
|
+
: null;
|
|
156
|
+
const placed = position !== null;
|
|
157
|
+
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (!placed) {
|
|
160
|
+
progress.setValue(0);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
Animated.timing(progress, {
|
|
164
|
+
toValue: 1,
|
|
165
|
+
duration: ENTER_MS,
|
|
166
|
+
useNativeDriver: Platform.OS !== "web",
|
|
167
|
+
}).start();
|
|
168
|
+
}, [placed, progress]);
|
|
169
|
+
|
|
170
|
+
return (
|
|
171
|
+
<Modal
|
|
172
|
+
transparent
|
|
173
|
+
visible={state.open}
|
|
174
|
+
animationType="none"
|
|
175
|
+
statusBarTranslucent={Platform.OS === "android"}
|
|
176
|
+
onRequestClose={state.close}
|
|
177
|
+
>
|
|
178
|
+
<Pressable
|
|
179
|
+
accessibilityRole="button"
|
|
180
|
+
accessibilityLabel="Close menu"
|
|
181
|
+
style={{ position: "absolute", top: 0, right: 0, bottom: 0, left: 0 }}
|
|
182
|
+
onPress={state.close}
|
|
183
|
+
/>
|
|
184
|
+
<Animated.View
|
|
185
|
+
accessibilityRole="menu"
|
|
186
|
+
style={[
|
|
187
|
+
surface,
|
|
188
|
+
{
|
|
189
|
+
left: position?.x ?? -9999,
|
|
190
|
+
top: position?.y ?? -9999,
|
|
191
|
+
opacity: progress,
|
|
192
|
+
transform: [
|
|
193
|
+
{ scale: progress.interpolate({ inputRange: [0, 1], outputRange: [0.97, 1] }) },
|
|
194
|
+
],
|
|
195
|
+
transformOrigin: position?.placement === "bottom" ? "left top" : "left bottom",
|
|
196
|
+
},
|
|
197
|
+
]}
|
|
198
|
+
>
|
|
199
|
+
<ScrollView
|
|
200
|
+
bounces={false}
|
|
201
|
+
showsVerticalScrollIndicator
|
|
202
|
+
style={visible ? { height: visible.height } : null}
|
|
203
|
+
contentContainerStyle={{ flexGrow: 1 }}
|
|
204
|
+
>
|
|
205
|
+
<View
|
|
206
|
+
collapsable={false}
|
|
207
|
+
onLayout={(event) => {
|
|
208
|
+
const { width, height } = event.nativeEvent.layout;
|
|
209
|
+
setContent((current) =>
|
|
210
|
+
current && current.width === width && current.height === height
|
|
211
|
+
? current
|
|
212
|
+
: { width, height },
|
|
213
|
+
);
|
|
214
|
+
}}
|
|
215
|
+
>
|
|
216
|
+
<MenuList
|
|
217
|
+
items={state.plan.items}
|
|
218
|
+
theme={theme}
|
|
219
|
+
compact={false}
|
|
220
|
+
autoFocus={placed}
|
|
221
|
+
onChoose={choose}
|
|
222
|
+
onClose={state.close}
|
|
223
|
+
/>
|
|
224
|
+
</View>
|
|
225
|
+
</ScrollView>
|
|
226
|
+
</Animated.View>
|
|
227
|
+
</Modal>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function PickerSheet({ state, theme }: { state: PickerState; theme: PluginTheme }) {
|
|
232
|
+
const choose = useChoose(state);
|
|
233
|
+
return (
|
|
234
|
+
<HostModal
|
|
235
|
+
title={state.plan.title}
|
|
236
|
+
open={state.open}
|
|
237
|
+
onOpenChange={(open) => {
|
|
238
|
+
if (!open) state.close();
|
|
239
|
+
}}
|
|
240
|
+
>
|
|
241
|
+
<HostModal.Content contentContainerStyle={{ paddingHorizontal: 0, paddingTop: 0, gap: 0 }}>
|
|
242
|
+
<MenuList
|
|
243
|
+
items={state.plan.items}
|
|
244
|
+
theme={theme}
|
|
245
|
+
compact
|
|
246
|
+
autoFocus={false}
|
|
247
|
+
onChoose={choose}
|
|
248
|
+
onClose={state.close}
|
|
249
|
+
/>
|
|
250
|
+
</HostModal.Content>
|
|
251
|
+
</HostModal>
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The pill's icon hosts the picker. Slash commands cannot open host UI, but a mounted plugin
|
|
257
|
+
* component can, so the picker opens as soon as the pill mounts: anchored above the pill on wide
|
|
258
|
+
* layouts, as a sheet on compact ones.
|
|
259
|
+
*/
|
|
260
|
+
function PickerIcon(props: PluginButtonIconProps) {
|
|
261
|
+
const { size, color, theme, layout } = props;
|
|
262
|
+
const state = usePickerState(props.context === "agent" ? props.agentId : "");
|
|
263
|
+
const anchor = useRef<View>(null);
|
|
264
|
+
return (
|
|
265
|
+
<>
|
|
266
|
+
<View ref={anchor} collapsable={false}>
|
|
267
|
+
<Icon name={state?.plan.icon ?? "ListChecks"} size={size} color={color} />
|
|
268
|
+
</View>
|
|
269
|
+
{state && layout.compact ? <PickerSheet state={state} theme={theme} /> : null}
|
|
270
|
+
{state && !layout.compact ? (
|
|
271
|
+
<PickerPopover anchor={anchor} state={state} theme={theme} />
|
|
272
|
+
) : null}
|
|
273
|
+
</>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* A picker is a temporary composer pill whose icon opens the menu right away. Closing or
|
|
279
|
+
* choosing removes the pill; pressing the pill reopens it; the next picker replaces it.
|
|
280
|
+
*/
|
|
281
|
+
export function createPickerHost(client: PluginClientContext) {
|
|
282
|
+
const pills = new Map<string, PluginButtonRegistration>();
|
|
283
|
+
|
|
284
|
+
function remove(agentId: string) {
|
|
285
|
+
pills.get(agentId)?.remove();
|
|
286
|
+
pills.delete(agentId);
|
|
287
|
+
setState(agentId, null);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function show(target: PickerTarget, plan: PickerPlan, run: RunControl) {
|
|
291
|
+
const { agentId } = target;
|
|
292
|
+
setState(agentId, { plan, run, open: true, close: () => remove(agentId) });
|
|
293
|
+
const button: PluginButton = {
|
|
294
|
+
title: plan.title,
|
|
295
|
+
icon: PickerIcon,
|
|
296
|
+
label: plan.title,
|
|
297
|
+
behavior: {
|
|
298
|
+
kind: "action",
|
|
299
|
+
onPress() {
|
|
300
|
+
const current = states.get(agentId);
|
|
301
|
+
if (current) setState(agentId, { ...current, open: true });
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
const existing = pills.get(agentId);
|
|
306
|
+
if (existing) existing.update(button);
|
|
307
|
+
else pills.set(agentId, client.addComposerPill({ id: "picker", ...target, button }));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
show,
|
|
312
|
+
dispose() {
|
|
313
|
+
for (const agentId of [...pills.keys()]) remove(agentId);
|
|
314
|
+
},
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export type PickerHost = ReturnType<typeof createPickerHost>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import { useSettings, type PluginSurfaceProps, type SettingsState } from "@getpaseo/plugin/client";
|
|
4
|
+
import {
|
|
5
|
+
SettingsAction,
|
|
6
|
+
SettingsCard,
|
|
7
|
+
SettingsSection,
|
|
8
|
+
SettingsSwitch,
|
|
9
|
+
} from "@getpaseo/plugin/client/ui";
|
|
10
|
+
import { COMMANDS_SETTINGS } from "../shared/settings.ts";
|
|
11
|
+
import { COMMAND_SECTIONS, COMMANDS, type CommandSection } from "./commands.ts";
|
|
12
|
+
import { setDisabledCommands } from "./settings-store.ts";
|
|
13
|
+
|
|
14
|
+
type ReadySettings = Extract<SettingsState<typeof COMMANDS_SETTINGS.schema>, { status: "ready" }>;
|
|
15
|
+
|
|
16
|
+
const SECTION_TITLES: Record<CommandSection, string> = {
|
|
17
|
+
runtime: "Runtime",
|
|
18
|
+
session: "Session",
|
|
19
|
+
prompt: "Prompt",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function CommandSwitches({ settings }: { settings: ReadySettings }) {
|
|
23
|
+
const disabled = settings.values.disabled;
|
|
24
|
+
const toggle = (name: string, enabled: boolean) => {
|
|
25
|
+
const next = enabled ? disabled.filter((entry) => entry !== name) : [...disabled, name];
|
|
26
|
+
void settings.save({ ...settings.values, disabled: next }, settings.revision);
|
|
27
|
+
};
|
|
28
|
+
const groups = COMMAND_SECTIONS.map((section) =>
|
|
29
|
+
COMMANDS.filter((command) => command.section === section),
|
|
30
|
+
).filter((group) => group.length > 0);
|
|
31
|
+
const lastName = groups.at(-1)?.at(-1)?.name;
|
|
32
|
+
return (
|
|
33
|
+
<>
|
|
34
|
+
{groups.map((group) => (
|
|
35
|
+
<SettingsSection key={group[0].section} title={SECTION_TITLES[group[0].section]}>
|
|
36
|
+
<SettingsCard>
|
|
37
|
+
{group.map((command) => (
|
|
38
|
+
<SettingsSwitch
|
|
39
|
+
key={command.name}
|
|
40
|
+
label={`/${command.name}${command.argumentHint ? ` ${command.argumentHint}` : ""}`}
|
|
41
|
+
hint={`${command.description}. ${command.details}`}
|
|
42
|
+
value={!disabled.includes(command.name)}
|
|
43
|
+
disabled={settings.saving}
|
|
44
|
+
onValueChange={(enabled) => toggle(command.name, enabled)}
|
|
45
|
+
error={command.name === lastName ? settings.saveError : null}
|
|
46
|
+
/>
|
|
47
|
+
))}
|
|
48
|
+
</SettingsCard>
|
|
49
|
+
</SettingsSection>
|
|
50
|
+
))}
|
|
51
|
+
</>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function CommandsSettingsScreen({ theme }: PluginSurfaceProps) {
|
|
56
|
+
const settings = useSettings(COMMANDS_SETTINGS);
|
|
57
|
+
const textStyle = useMemo(() => ({ color: theme.colors.foreground }), [theme]);
|
|
58
|
+
const values = settings.status === "ready" ? settings.values : null;
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (values) setDisabledCommands(values.disabled);
|
|
62
|
+
}, [values]);
|
|
63
|
+
|
|
64
|
+
if (settings.status === "loading") return <Text style={textStyle}>Loading settings…</Text>;
|
|
65
|
+
if (settings.status !== "ready") {
|
|
66
|
+
return (
|
|
67
|
+
<SettingsSection title="Slash commands">
|
|
68
|
+
<Text style={textStyle}>{settings.error}</Text>
|
|
69
|
+
<SettingsAction label="Try again" actionLabel="Reload" onPress={settings.reload} />
|
|
70
|
+
{settings.status === "invalid" ? (
|
|
71
|
+
<SettingsAction
|
|
72
|
+
label="Restore default settings"
|
|
73
|
+
actionLabel="Reset"
|
|
74
|
+
onPress={settings.reset}
|
|
75
|
+
/>
|
|
76
|
+
) : null}
|
|
77
|
+
</SettingsSection>
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return <CommandSwitches settings={settings} />;
|
|
81
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
type Listener = (disabled: ReadonlySet<string>) => void;
|
|
2
|
+
|
|
3
|
+
let disabled: ReadonlySet<string> = new Set();
|
|
4
|
+
const listeners = new Set<Listener>();
|
|
5
|
+
|
|
6
|
+
export function getDisabledCommands(): ReadonlySet<string> {
|
|
7
|
+
return disabled;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function setDisabledCommands(names: readonly string[]): void {
|
|
11
|
+
const next = new Set(names);
|
|
12
|
+
if (next.size === disabled.size && [...next].every((name) => disabled.has(name))) return;
|
|
13
|
+
disabled = next;
|
|
14
|
+
for (const listener of listeners) listener(disabled);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function subscribeDisabledCommands(listener: Listener): () => void {
|
|
18
|
+
listeners.add(listener);
|
|
19
|
+
return () => {
|
|
20
|
+
listeners.delete(listener);
|
|
21
|
+
};
|
|
22
|
+
}
|
package/index.client.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { settingsRpc } from "@getpaseo/plugin";
|
|
2
|
+
import type { PluginClientContext } from "@getpaseo/plugin/client";
|
|
3
|
+
import { registerCommands } from "./client/commands.ts";
|
|
4
|
+
import { createPickerHost } from "./client/picker.tsx";
|
|
5
|
+
import { CommandsSettingsScreen } from "./client/settings-screen.tsx";
|
|
6
|
+
import { setDisabledCommands } from "./client/settings-store.ts";
|
|
7
|
+
import { COMMANDS_SETTINGS } from "./shared/settings.ts";
|
|
8
|
+
|
|
9
|
+
export default function contribute(client: PluginClientContext) {
|
|
10
|
+
const pickers = createPickerHost(client);
|
|
11
|
+
const unregister = registerCommands(client, pickers);
|
|
12
|
+
const removeSettings = client.addSettingsScreen({
|
|
13
|
+
id: "commands",
|
|
14
|
+
title: "Settings",
|
|
15
|
+
icon: "Settings",
|
|
16
|
+
Component: CommandsSettingsScreen,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
let disposed = false;
|
|
20
|
+
client
|
|
21
|
+
.rpc(settingsRpc(COMMANDS_SETTINGS.id).read, {})
|
|
22
|
+
.then((result) => {
|
|
23
|
+
if (disposed || result.status !== "ready") return;
|
|
24
|
+
const parsed = COMMANDS_SETTINGS.schema.safeParse(result.values);
|
|
25
|
+
if (parsed.success) setDisabledCommands(parsed.data.disabled);
|
|
26
|
+
})
|
|
27
|
+
.catch(() => {});
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
disposed = true;
|
|
31
|
+
removeSettings();
|
|
32
|
+
unregister();
|
|
33
|
+
pickers.dispose();
|
|
34
|
+
};
|
|
35
|
+
}
|
package/index.server.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PluginServerContext } from "@getpaseo/plugin/server";
|
|
2
|
+
import { runAgentControl } from "./server/control.ts";
|
|
3
|
+
import { createDaemonConnection } from "./server/daemon.ts";
|
|
4
|
+
import { agentControl } from "./shared/rpc.ts";
|
|
5
|
+
import { COMMANDS_SETTINGS } from "./shared/settings.ts";
|
|
6
|
+
|
|
7
|
+
export default function contribute(server: PluginServerContext) {
|
|
8
|
+
const daemon = createDaemonConnection();
|
|
9
|
+
server.registerSettings(COMMANDS_SETTINGS);
|
|
10
|
+
server.handle(agentControl, async (input) => runAgentControl(await daemon.get(), input));
|
|
11
|
+
return () => daemon.close();
|
|
12
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@koinzhang/paseo-plugin-commands",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Essential client-side slash commands for controlling the current Paseo agent.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/koinzhang/paseo-plugins.git",
|
|
10
|
+
"directory": "commands"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"paseo-plugin.json",
|
|
14
|
+
"CHANGELOG.md",
|
|
15
|
+
"index.client.tsx",
|
|
16
|
+
"index.server.ts",
|
|
17
|
+
"client/",
|
|
18
|
+
"server/",
|
|
19
|
+
"shared/",
|
|
20
|
+
"!**/*.test.ts",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "node --experimental-strip-types --test shared/*.test.ts server/*.test.ts"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@getpaseo/client": "0.9.2",
|
|
30
|
+
"ws": "^8.18.3"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@getpaseo/plugin": "0.9.2",
|
|
34
|
+
"@types/node": "^24.13.5",
|
|
35
|
+
"@types/react": "~19.2.0",
|
|
36
|
+
"@types/ws": "^8.18.1",
|
|
37
|
+
"react": "19.1.0",
|
|
38
|
+
"react-native": "0.81.5",
|
|
39
|
+
"typescript": "^5.9.3",
|
|
40
|
+
"zod": "^4.6.5"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
|
|
2
|
+
import type { AgentControl } from "../shared/rpc.ts";
|
|
3
|
+
|
|
4
|
+
type Notice = { type: "info" | "warning" | "error"; message: string } | null;
|
|
5
|
+
|
|
6
|
+
export async function runAgentControl(
|
|
7
|
+
client: DaemonClient,
|
|
8
|
+
input: AgentControl,
|
|
9
|
+
): Promise<{ notice: Notice }> {
|
|
10
|
+
const { agentId } = input;
|
|
11
|
+
switch (input.op) {
|
|
12
|
+
case "model":
|
|
13
|
+
await client.setAgentModel(agentId, input.modelId);
|
|
14
|
+
return { notice: null };
|
|
15
|
+
case "thinking":
|
|
16
|
+
return { notice: await client.setAgentThinkingOption(agentId, input.thinkingOptionId) };
|
|
17
|
+
case "mode":
|
|
18
|
+
return { notice: await client.setAgentMode(agentId, input.modeId) };
|
|
19
|
+
case "feature":
|
|
20
|
+
await client.setAgentFeature(agentId, input.featureId, input.value);
|
|
21
|
+
return { notice: null };
|
|
22
|
+
case "config":
|
|
23
|
+
return { notice: await applyConfig(client, agentId, input.config) };
|
|
24
|
+
case "rename":
|
|
25
|
+
await client.updateAgent(agentId, { name: input.title });
|
|
26
|
+
return { notice: null };
|
|
27
|
+
case "cancel":
|
|
28
|
+
await client.cancelAgent(agentId);
|
|
29
|
+
return { notice: null };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function applyConfig(
|
|
34
|
+
client: DaemonClient,
|
|
35
|
+
agentId: string,
|
|
36
|
+
config: Extract<AgentControl, { op: "config" }>["config"],
|
|
37
|
+
): Promise<Notice> {
|
|
38
|
+
if (client.getLastServerInfoMessage()?.features?.agentConfigApply === true) {
|
|
39
|
+
return client.applyAgentConfig(agentId, config);
|
|
40
|
+
}
|
|
41
|
+
// Older daemons: apply the same bundle one field at a time, model first so
|
|
42
|
+
// thinking options are validated against the new model.
|
|
43
|
+
let notice: Notice = null;
|
|
44
|
+
if (config.modelId) await client.setAgentModel(agentId, config.modelId);
|
|
45
|
+
if (config.modeId) notice = (await client.setAgentMode(agentId, config.modeId)) ?? notice;
|
|
46
|
+
if (config.thinkingOptionId) {
|
|
47
|
+
notice = (await client.setAgentThinkingOption(agentId, config.thinkingOptionId)) ?? notice;
|
|
48
|
+
}
|
|
49
|
+
for (const [featureId, value] of Object.entries(config.featureValues ?? {})) {
|
|
50
|
+
await client.setAgentFeature(agentId, featureId, value);
|
|
51
|
+
}
|
|
52
|
+
return notice;
|
|
53
|
+
}
|
package/server/daemon.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { DaemonClient, type WebSocketLike } from "@getpaseo/client/internal/daemon-client";
|
|
5
|
+
import WebSocket from "ws";
|
|
6
|
+
|
|
7
|
+
interface DaemonTarget {
|
|
8
|
+
url: string;
|
|
9
|
+
socketPath?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function paseoHome(): string {
|
|
13
|
+
const raw = process.env.PASEO_HOME ?? "~/.paseo";
|
|
14
|
+
return path.resolve(raw.startsWith("~") ? path.join(homedir(), raw.slice(1)) : raw);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Mirrors the listen forms the daemon accepts: host:port, /path, unix:///path. */
|
|
18
|
+
export function toDaemonTarget(listen: string): DaemonTarget {
|
|
19
|
+
const trimmed = listen.trim();
|
|
20
|
+
const socketPath = trimmed.startsWith("unix://")
|
|
21
|
+
? trimmed.slice("unix://".length)
|
|
22
|
+
: trimmed.startsWith("/")
|
|
23
|
+
? trimmed
|
|
24
|
+
: null;
|
|
25
|
+
if (socketPath) return { url: `ws+unix://${socketPath}:/ws`, socketPath };
|
|
26
|
+
const endpoint = /^\d+$/.test(trimmed) ? `127.0.0.1:${trimmed}` : trimmed;
|
|
27
|
+
return { url: `ws://${endpoint}/ws` };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function resolveListen(): Promise<string> {
|
|
31
|
+
if (process.env.PASEO_LISTEN) return process.env.PASEO_LISTEN;
|
|
32
|
+
try {
|
|
33
|
+
const pid = JSON.parse(await readFile(path.join(paseoHome(), "paseo.pid"), "utf8")) as {
|
|
34
|
+
listen?: unknown;
|
|
35
|
+
};
|
|
36
|
+
if (typeof pid.listen === "string" && pid.listen) return pid.listen;
|
|
37
|
+
} catch {
|
|
38
|
+
// Fall through to the daemon default.
|
|
39
|
+
}
|
|
40
|
+
return `127.0.0.1:${process.env.PORT ?? "6767"}`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function connect(): Promise<DaemonClient> {
|
|
44
|
+
const target = toDaemonTarget(await resolveListen());
|
|
45
|
+
const client = new DaemonClient({
|
|
46
|
+
url: target.url,
|
|
47
|
+
clientId: `commands-plugin-${process.pid}`,
|
|
48
|
+
clientType: "cli",
|
|
49
|
+
password: process.env.PASEO_PASSWORD || undefined,
|
|
50
|
+
connectTimeoutMs: 5_000,
|
|
51
|
+
reconnect: { enabled: true },
|
|
52
|
+
webSocketFactory: (url, options) =>
|
|
53
|
+
new WebSocket(url, options?.protocols, {
|
|
54
|
+
headers: options?.headers,
|
|
55
|
+
...(target.socketPath ? { socketPath: target.socketPath } : {}),
|
|
56
|
+
}) as unknown as WebSocketLike,
|
|
57
|
+
});
|
|
58
|
+
try {
|
|
59
|
+
await client.connect();
|
|
60
|
+
return client;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
await client.close().catch(() => {});
|
|
63
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
64
|
+
throw new Error(`Cannot reach the Paseo daemon at ${target.url}: ${message}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** One lazily opened connection per plugin process, reopened after a failure. */
|
|
69
|
+
export function createDaemonConnection() {
|
|
70
|
+
let pending: Promise<DaemonClient> | null = null;
|
|
71
|
+
return {
|
|
72
|
+
get(): Promise<DaemonClient> {
|
|
73
|
+
pending ??= connect().catch((error: unknown) => {
|
|
74
|
+
pending = null;
|
|
75
|
+
throw error;
|
|
76
|
+
});
|
|
77
|
+
return pending;
|
|
78
|
+
},
|
|
79
|
+
async close(): Promise<void> {
|
|
80
|
+
const current = pending;
|
|
81
|
+
pending = null;
|
|
82
|
+
if (current) await (await current.catch(() => null))?.close();
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|