@omercnet/paseo-omp 5.4.0-next.417.1 → 5.4.0-next.418.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,140 @@
1
+ import { type PluginSurfaceProps, useRpc } from "@getpaseo/plugin/client";
2
+ import { TextInput } from "@getpaseo/plugin/client/ui";
3
+ import { useMutation } from "@tanstack/react-query";
4
+ import { useState } from "react";
5
+ import { Text, View } from "react-native";
6
+ import {
7
+ MAX_RULE_TEST_CHARS,
8
+ type OmpRuleTestSource,
9
+ OmpRuleTestSourceSchema,
10
+ testOmpRules,
11
+ } from "../shared/omp-rules";
12
+ import type { OmpStore } from "../shared/omp-store";
13
+ import { describe } from "./omp-accounts-section";
14
+ import type { OmpConfigStyles } from "./omp-config-styles";
15
+ import { Choice } from "./omp-tool-config-section";
16
+
17
+ type Theme = PluginSurfaceProps["theme"];
18
+
19
+ const SOURCES = OmpRuleTestSourceSchema.options;
20
+
21
+ export function OmpRuleTest({
22
+ theme,
23
+ styles,
24
+ cwd,
25
+ store,
26
+ }: {
27
+ theme: Theme;
28
+ styles: OmpConfigStyles;
29
+ cwd?: string;
30
+ store?: OmpStore;
31
+ }) {
32
+ const test = useRpc(testOmpRules);
33
+ const [snippet, setSnippet] = useState("");
34
+ const [path, setPath] = useState("");
35
+ const [source, setSource] = useState<OmpRuleTestSource | null>(null);
36
+ const run = useMutation({
37
+ mutationFn: () =>
38
+ test({
39
+ store,
40
+ ...(cwd ? { cwd } : {}),
41
+ snippet,
42
+ ...(path.trim() ? { path: path.trim() } : {}),
43
+ ...(source ? { source } : {}),
44
+ }),
45
+ });
46
+ const result = run.data;
47
+ return (
48
+ <View style={styles.card}>
49
+ <View style={styles.cardHeader}>
50
+ <Text style={styles.cardTitle}>Test a snippet</Text>
51
+ </View>
52
+ <View style={styles.setting}>
53
+ <Text style={styles.settingDescription}>
54
+ Paste text to see which rules would fire on it, in this {cwd ? "workspace" : "user"}{" "}
55
+ scope. Nothing is sent to a model.
56
+ </Text>
57
+ <TextInput
58
+ accessibilityLabel="Snippet to test"
59
+ multiline
60
+ maxLength={MAX_RULE_TEST_CHARS}
61
+ placeholder="Text the agent might write"
62
+ placeholderTextColor={theme.colors.foregroundMuted}
63
+ value={snippet}
64
+ onChangeText={setSnippet}
65
+ style={[styles.scalarInput, { minHeight: 120, maxWidth: undefined }]}
66
+ />
67
+ <TextInput
68
+ accessibilityLabel="File path of the snippet"
69
+ autoCapitalize="none"
70
+ autoCorrect={false}
71
+ placeholder="File path, optional (src/app.ts)"
72
+ placeholderTextColor={theme.colors.foregroundMuted}
73
+ value={path}
74
+ onChangeText={setPath}
75
+ style={[styles.scalarInput, { maxWidth: undefined }]}
76
+ />
77
+ <View style={styles.editorActions}>
78
+ <Choice styles={styles} label="Auto" selected={!source} onPress={() => setSource(null)} />
79
+ {SOURCES.map((option) => (
80
+ <Choice
81
+ key={option}
82
+ styles={styles}
83
+ label={option}
84
+ selected={source === option}
85
+ onPress={() => setSource(option)}
86
+ />
87
+ ))}
88
+ </View>
89
+ <View style={styles.editorActions}>
90
+ <Choice
91
+ styles={styles}
92
+ label={run.isPending ? "Testing..." : "Test"}
93
+ selected
94
+ disabled={snippet.trim() === "" || run.isPending}
95
+ onPress={() => run.mutate()}
96
+ />
97
+ </View>
98
+ {run.error ? (
99
+ <Text accessibilityRole="alert" style={styles.error}>
100
+ {describe(run.error)}
101
+ </Text>
102
+ ) : null}
103
+ {result?.status === "error" ? (
104
+ <Text accessibilityRole="alert" style={styles.error}>
105
+ {result.message}
106
+ </Text>
107
+ ) : null}
108
+ {result?.status === "ready" ? (
109
+ <>
110
+ <Text style={styles.muted}>
111
+ {result.triggered.length === 0
112
+ ? `No rule fired, out of ${result.evaluated} checked`
113
+ : `${result.triggered.length} of ${result.evaluated} rules fired`}
114
+ </Text>
115
+ {result.triggered.map((hit) => (
116
+ <View key={hit.name} style={styles.setting}>
117
+ <Text selectable style={styles.settingValue}>
118
+ {hit.name}
119
+ </Text>
120
+ {hit.regex.length > 0 ? (
121
+ <Text selectable style={styles.settingDescription}>
122
+ Text matched: {hit.regex.join(", ")}
123
+ </Text>
124
+ ) : null}
125
+ {hit.ast.length > 0 ? (
126
+ <Text selectable style={styles.settingDescription}>
127
+ Code matched: {hit.ast.join(", ")}
128
+ </Text>
129
+ ) : null}
130
+ <Text selectable style={styles.settingPath}>
131
+ {hit.path}
132
+ </Text>
133
+ </View>
134
+ ))}
135
+ </>
136
+ ) : null}
137
+ </View>
138
+ </View>
139
+ );
140
+ }
@@ -12,6 +12,7 @@ import {
12
12
  import type { OmpStore } from "../shared/omp-store";
13
13
  import { describe } from "./omp-accounts-section";
14
14
  import type { OmpConfigStyles } from "./omp-config-styles";
15
+ import { OmpRuleTest } from "./omp-rule-test";
15
16
  import { ompStoreKey } from "./omp-store-state";
16
17
 
17
18
  type Theme = PluginSurfaceProps["theme"];
@@ -212,6 +213,7 @@ export function OmpRulesSection({
212
213
  </Text>
213
214
  <Text style={styles.muted}>Rule settings (ttsr.*) are in the Configuration tab.</Text>
214
215
  </View>
216
+ <OmpRuleTest theme={theme} styles={styles} cwd={cwd} store={store} />
215
217
  {rules.isLoading ? <Text style={styles.muted}>Loading rules...</Text> : null}
216
218
  {rules.error ? (
217
219
  <Text accessibilityRole="alert" style={styles.error}>
@@ -28,7 +28,7 @@ const KINDS: readonly { kind: OmpToolConfigKind; label: string; example: string
28
28
  },
29
29
  ];
30
30
 
31
- function Choice({
31
+ export function Choice({
32
32
  styles,
33
33
  label,
34
34
  selected,
package/index.server.ts CHANGED
@@ -25,6 +25,7 @@ import {
25
25
  } from "./server/omp-plugins";
26
26
  import { readHostAppendPrompt, readOmpPromptFiles } from "./server/omp-prompt-files";
27
27
  import { resolveSetOmpRuleEnabled } from "./server/omp-rule-switch";
28
+ import { resolveTestOmpRules } from "./server/omp-rule-test";
28
29
  import { resolveListOmpRules } from "./server/omp-rules";
29
30
  import { resolveListOmpSecurity } from "./server/omp-security";
30
31
  import { resolveListOmpSettings, resolveUpdateOmpSettings } from "./server/omp-settings";
@@ -98,7 +99,7 @@ import {
98
99
  mutateOmpPluginConfig,
99
100
  } from "./shared/omp-plugins";
100
101
  import { getOmpPromptFiles } from "./shared/omp-prompt-files";
101
- import { listOmpRules, setOmpRuleEnabled } from "./shared/omp-rules";
102
+ import { listOmpRules, setOmpRuleEnabled, testOmpRules } from "./shared/omp-rules";
102
103
  import { listOmpSecurity } from "./shared/omp-security";
103
104
  import { listOmpSettings, updateOmpSettings } from "./shared/omp-settings";
104
105
  import { deleteOmpManagedSkill, listOmpSkills, setOmpSkillEnabled } from "./shared/omp-skills";
@@ -227,6 +228,7 @@ export default function contribute(server: PluginServerContext) {
227
228
  server.handle(mutateOmpMarketplace, scoped(resolveMutateOmpMarketplace));
228
229
  server.handle(listOmpRules, scoped(resolveListOmpRules));
229
230
  server.handle(setOmpRuleEnabled, scoped(resolveSetOmpRuleEnabled));
231
+ server.handle(testOmpRules, scoped(resolveTestOmpRules));
230
232
  server.handle(listOmpExtensions, scoped(resolveListOmpExtensions));
231
233
  server.handle(setOmpExtensionEnabled, scoped(resolveSetOmpExtensionEnabled));
232
234
  server.handle(listOmpSecurity, scoped(resolveListOmpSecurity));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omercnet/paseo-omp",
3
- "version": "5.4.0-next.417.1",
3
+ "version": "5.4.0-next.418.1",
4
4
  "type": "module",
5
5
  "description": "Paseo integration for OMP, including its direct provider and workspace tooling.",
6
6
  "license": "MIT",
@@ -0,0 +1,82 @@
1
+ import type { RpcInput } from "@getpaseo/plugin";
2
+ import { z } from "zod";
3
+ import type { OmpRuleTestHit, testOmpRules } from "../shared/omp-rules";
4
+ import {
5
+ inOmpScope,
6
+ type OmpRulesDependencies,
7
+ OmpRulesError,
8
+ ompCommandDependencies,
9
+ runJson,
10
+ } from "./omp-rules";
11
+ import { OmpPublicError } from "./provider/security";
12
+
13
+ const MAX_HITS = 100;
14
+ const MAX_TEXT_CHARS = 2_000;
15
+ const MAX_PATTERNS = 20;
16
+
17
+ const Patterns = z
18
+ .array(z.string())
19
+ .default([])
20
+ .transform((list) => list.slice(0, MAX_PATTERNS).map((text) => text.slice(0, MAX_TEXT_CHARS)));
21
+
22
+ /** `omp ttsr test --json`: the rules that fired are `triggered`; `notTriggered` is not needed. */
23
+ const TestOutputSchema = z.object({
24
+ evaluated: z.number().int().nonnegative(),
25
+ triggered: z.array(z.unknown()),
26
+ });
27
+
28
+ const TriggeredSchema = z.object({
29
+ name: z.string().min(1).max(256),
30
+ path: z.string().max(4_096),
31
+ sourceProvider: z.string().max(128),
32
+ matched: z.object({ regex: Patterns, ast: Patterns }),
33
+ });
34
+
35
+ export async function testOmpRulesWithDependencies(
36
+ input: RpcInput<typeof testOmpRules>,
37
+ dependencies: OmpRulesDependencies,
38
+ ): Promise<
39
+ | { status: "ready"; evaluated: number; triggered: OmpRuleTestHit[] }
40
+ | { status: "error"; message: string }
41
+ > {
42
+ try {
43
+ const executable = await dependencies.resolveExecutable();
44
+ if (!executable) throw new OmpRulesError("The omp command was not found on this host");
45
+ // The snippet goes over stdin and the optional values are `--flag=value`, so neither can be
46
+ // read as another option. No shell is involved.
47
+ const args = [
48
+ "ttsr",
49
+ "test",
50
+ "--json",
51
+ "--file",
52
+ "-",
53
+ ...(input.source ? [`--source=${input.source}`] : []),
54
+ ...(input.path ? [`--path=${input.path}`] : []),
55
+ ];
56
+ const raw = await inOmpScope(input.cwd, dependencies, (runIn) =>
57
+ runJson(dependencies, executable, args, runIn, input.snippet),
58
+ );
59
+ const output = TestOutputSchema.safeParse(raw);
60
+ if (!output.success) throw new OmpRulesError("omp ttsr test gave output the plugin can't read");
61
+ const triggered = output.data.triggered
62
+ .flatMap((entry): OmpRuleTestHit[] => {
63
+ const parsed = TriggeredSchema.safeParse(entry);
64
+ if (!parsed.success) return [];
65
+ const { name, path, sourceProvider, matched } = parsed.data;
66
+ return [{ name, provider: sourceProvider, path, regex: matched.regex, ast: matched.ast }];
67
+ })
68
+ .slice(0, MAX_HITS);
69
+ // Fired rules that all fail to parse mean the output changed shape; "none fired" would be false.
70
+ if (output.data.triggered.length > 0 && triggered.length === 0) {
71
+ throw new OmpRulesError("omp ttsr test gave output the plugin can't read");
72
+ }
73
+ return { status: "ready", evaluated: output.data.evaluated, triggered };
74
+ } catch (error) {
75
+ if (error instanceof OmpPublicError) return { status: "error", message: error.message };
76
+ throw error;
77
+ }
78
+ }
79
+
80
+ export function resolveTestOmpRules(input: RpcInput<typeof testOmpRules>) {
81
+ return testOmpRulesWithDependencies(input, ompCommandDependencies);
82
+ }
@@ -29,7 +29,12 @@ export class OmpRulesError extends OmpPublicError {}
29
29
 
30
30
  export interface OmpRulesDependencies {
31
31
  resolveExecutable(): Promise<string | null>;
32
- run(executable: string, args: readonly string[], cwd: string): Promise<BoundedRun>;
32
+ run(
33
+ executable: string,
34
+ args: readonly string[],
35
+ cwd: string,
36
+ input?: string,
37
+ ): Promise<BoundedRun>;
33
38
  makeScratchDir(prefix: string): Promise<string>;
34
39
  removeDir(path: string): Promise<void>;
35
40
  }
@@ -111,9 +116,10 @@ export async function runJson(
111
116
  executable: string,
112
117
  args: readonly string[],
113
118
  cwd: string,
119
+ input?: string,
114
120
  ): Promise<unknown> {
115
121
  const label = `omp ${args.join(" ")}`;
116
- const run = await dependencies.run(executable, args, cwd);
122
+ const run = await dependencies.run(executable, args, cwd, input);
117
123
  if (run.outcome === "timeout") throw new OmpRulesError(`${label} timed out`);
118
124
  if (run.outcome !== "exited" || run.exitCode !== 0 || run.truncated) {
119
125
  throw new OmpRulesError(`${label} failed`);
@@ -424,7 +430,7 @@ export async function listOmpRulesWithDependencies(
424
430
 
425
431
  export const ompCommandDependencies: OmpRulesDependencies = {
426
432
  resolveExecutable: resolveOmpExecutable,
427
- run: (executable, args, cwd) =>
433
+ run: (executable, args, cwd, input) =>
428
434
  runBounded(
429
435
  defaultSpawn,
430
436
  executable,
@@ -434,6 +440,8 @@ export const ompCommandDependencies: OmpRulesDependencies = {
434
440
  KILL_GRACE_MS,
435
441
  MAX_OUTPUT_BYTES,
436
442
  cwd,
443
+ 0,
444
+ input,
437
445
  ),
438
446
  makeScratchDir: (prefix) => mkdtemp(prefix),
439
447
  removeDir: (path) => rm(path, { recursive: true, force: true }),
@@ -1,2 +1,2 @@
1
1
  /** Release Please keeps this bundle-safe constant aligned with package.json. */
2
- export const PASEO_OMP_PACKAGE_VERSION = "5.4.0-next.417.1"; // x-release-please-version
2
+ export const PASEO_OMP_PACKAGE_VERSION = "5.4.0-next.418.1"; // x-release-please-version
@@ -118,6 +118,8 @@ export type SpawnFn = (
118
118
  args: readonly string[],
119
119
  env: NodeJS.ProcessEnv,
120
120
  cwd?: string,
121
+ /** Written to the child's stdin, which is closed afterwards. Without it stdin closes at once. */
122
+ input?: string,
121
123
  ) => ProbeChildProcess;
122
124
 
123
125
  export type SignalProcess = (pid: number, signal: NodeJS.Signals | 0) => void;
@@ -234,13 +236,18 @@ export function defaultSpawn(
234
236
  args: readonly string[],
235
237
  env: NodeJS.ProcessEnv,
236
238
  cwd?: string,
239
+ input?: string,
237
240
  ): ProbeChildProcess {
238
241
  const child = spawn(command, args, {
239
242
  cwd,
240
- stdio: ["ignore", "pipe", "pipe"],
243
+ stdio: ["pipe", "pipe", "pipe"],
241
244
  env,
242
245
  detached: process.platform !== "win32",
243
246
  });
247
+ // A child that exits without reading closes the pipe; that is not an error here. Without
248
+ // input, stdin closes at once, so a child that reads it sees end of input.
249
+ child.stdin.on("error", () => {});
250
+ child.stdin.end(input);
244
251
  return {
245
252
  pid: child.pid,
246
253
  stdout: child.stdout,
@@ -378,11 +385,12 @@ export function runBounded(
378
385
  maxBytes: number,
379
386
  cwd?: string,
380
387
  stderrLimit = 0,
388
+ input?: string,
381
389
  ): Promise<BoundedRun> {
382
390
  const { promise, resolve } = Promise.withResolvers<BoundedRun>();
383
391
  let child: ProbeChildProcess;
384
392
  try {
385
- child = spawnFn(command, args, env, cwd);
393
+ child = spawnFn(command, args, env, cwd, input);
386
394
  } catch (error) {
387
395
  resolve({
388
396
  outcome: "spawn-error",
@@ -64,3 +64,48 @@ export const setOmpRuleEnabled = defineRpc({
64
64
  z.object({ status: z.literal("error"), message: z.string() }),
65
65
  ]),
66
66
  });
67
+
68
+ export const MAX_RULE_TEST_CHARS = 20_000;
69
+
70
+ export const OmpRuleTestSourceSchema = z.enum(["text", "thinking", "tool"]);
71
+ export type OmpRuleTestSource = z.infer<typeof OmpRuleTestSourceSchema>;
72
+
73
+ export const OmpRuleTestHitSchema = z.object({
74
+ name: z.string(),
75
+ provider: z.string(),
76
+ path: z.string(),
77
+ /** The rule's own patterns that matched the snippet. */
78
+ regex: z.array(z.string()),
79
+ ast: z.array(z.string()),
80
+ });
81
+ export type OmpRuleTestHit = z.infer<typeof OmpRuleTestHitSchema>;
82
+
83
+ /** Runs `omp ttsr test`: which rules would fire on a snippet, in this workspace or the user scope. */
84
+ export const testOmpRules = defineRpc({
85
+ name: "paseo-omp.test-rules",
86
+ input: z
87
+ .object({
88
+ store: OmpStoreSchema.optional(),
89
+ cwd: OmpWorkspaceCwdSchema.optional(),
90
+ snippet: z.string().min(1).max(MAX_RULE_TEST_CHARS),
91
+ /** The file the snippet pretends to come from, for scope and glob matching. */
92
+ path: z
93
+ .string()
94
+ .trim()
95
+ .min(1)
96
+ .max(1_024)
97
+ .refine((path) => !path.includes("\0"), "The path cannot hold a null character")
98
+ .optional(),
99
+ source: OmpRuleTestSourceSchema.optional(),
100
+ })
101
+ .strict(),
102
+ output: z.discriminatedUnion("status", [
103
+ z.object({
104
+ status: z.literal("ready"),
105
+ /** How many rules OMP checked. */
106
+ evaluated: z.number().int().nonnegative(),
107
+ triggered: z.array(OmpRuleTestHitSchema),
108
+ }),
109
+ z.object({ status: z.literal("error"), message: z.string() }),
110
+ ]),
111
+ });