@omercnet/paseo-omp 5.4.0 → 5.5.0-next.427.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.
Files changed (80) hide show
  1. package/OVERVIEW.md +7 -1
  2. package/README.md +2 -1
  3. package/client/ast-edit.tsx +53 -0
  4. package/client/composer-pill-settings.tsx +0 -5
  5. package/client/config-sidebar.tsx +15 -0
  6. package/client/generated-image.tsx +79 -0
  7. package/client/lsp-diagnostics.tsx +69 -0
  8. package/client/omp-config-surface.tsx +438 -108
  9. package/client/omp-config-views.ts +32 -5
  10. package/client/omp-extensions-section.tsx +145 -0
  11. package/client/omp-local-models-section.tsx +215 -0
  12. package/client/omp-plugin-manager.tsx +198 -3
  13. package/client/omp-routing-editor.tsx +123 -2
  14. package/client/omp-rule-test.tsx +140 -0
  15. package/client/omp-rules-section.tsx +353 -0
  16. package/client/omp-security-section.tsx +99 -0
  17. package/client/omp-security-state.ts +14 -0
  18. package/client/omp-skills-section.tsx +195 -0
  19. package/client/omp-tool-config-section.tsx +204 -0
  20. package/client/omp-tool-policy-state.ts +101 -0
  21. package/client/omp-tool-policy.tsx +435 -0
  22. package/client/omp-update-row.tsx +87 -0
  23. package/client/provider-diagnostics-state.ts +38 -1
  24. package/client/provider-image.tsx +1 -1
  25. package/client/rule-notice.tsx +33 -0
  26. package/client/sidebar-compat.ts +0 -109
  27. package/client/web.ts +34 -0
  28. package/index.client.tsx +79 -46
  29. package/index.server.ts +77 -3
  30. package/npm-shrinkwrap.json +2 -2
  31. package/package.json +4 -4
  32. package/server/generated-images.ts +57 -0
  33. package/server/omp-agents.ts +170 -0
  34. package/server/omp-extensions.ts +235 -0
  35. package/server/omp-list-setting.ts +98 -0
  36. package/server/omp-local-models.ts +202 -0
  37. package/server/omp-models-file.ts +178 -0
  38. package/server/omp-plugins.ts +182 -0
  39. package/server/omp-prompt-files.ts +41 -0
  40. package/server/omp-rule-firings.ts +211 -0
  41. package/server/omp-rule-switch.ts +38 -0
  42. package/server/omp-rule-test.ts +82 -0
  43. package/server/omp-rules.ts +452 -0
  44. package/server/omp-security.ts +169 -0
  45. package/server/omp-settings.ts +92 -16
  46. package/server/omp-skills.ts +179 -0
  47. package/server/omp-tool-config.ts +144 -0
  48. package/server/omp-update.ts +203 -0
  49. package/server/package-version.ts +1 -1
  50. package/server/provider/connection.ts +1 -1
  51. package/server/provider/image-cache.ts +1 -1
  52. package/server/provider/omp-rpc-environment.ts +1 -1
  53. package/server/provider/session-descriptors.ts +3 -0
  54. package/server/provider-diagnostics.ts +26 -4
  55. package/server/regular-file.ts +30 -0
  56. package/server/rule-notices.ts +112 -0
  57. package/server/turn-rows.ts +59 -0
  58. package/server/turn-screenshots.ts +140 -0
  59. package/shared/ast-edit.ts +124 -0
  60. package/shared/composer-pill-settings.ts +0 -2
  61. package/shared/generated-image.ts +68 -0
  62. package/shared/lsp-diagnostics.ts +141 -0
  63. package/shared/omp-agents.ts +26 -0
  64. package/shared/omp-extensions.ts +45 -0
  65. package/shared/omp-local-models.ts +44 -0
  66. package/shared/omp-models-file.ts +30 -0
  67. package/shared/omp-plugins.ts +90 -0
  68. package/shared/omp-prompt-files.ts +24 -0
  69. package/shared/omp-rules.ts +134 -0
  70. package/shared/omp-security.ts +50 -0
  71. package/shared/omp-settings.ts +211 -2
  72. package/shared/omp-skills.ts +80 -0
  73. package/shared/omp-tool-config.ts +283 -0
  74. package/shared/omp-update.ts +27 -0
  75. package/shared/provider-image.ts +10 -3
  76. package/shared/rule-notices.ts +20 -0
  77. package/client/hub-sidebar.tsx +0 -99
  78. package/client/sessions-popover.tsx +0 -78
  79. package/server/sessions.ts +0 -58
  80. package/shared/sessions.ts +0 -24
@@ -0,0 +1,211 @@
1
+ import { constants } from "node:fs";
2
+ import { type FileHandle, open, stat } from "node:fs/promises";
3
+ import { createInterface } from "node:readline";
4
+ import type { RpcInput } from "@getpaseo/plugin";
5
+ import { z } from "zod";
6
+ import type { listOmpRuleFirings, OmpRuleFiring } from "../shared/omp-rules";
7
+ import { currentOmpEnvironment } from "./paths";
8
+ import { listOmpSessionDescriptors } from "./provider/session-descriptors";
9
+
10
+ export interface FiringLimits {
11
+ /** Newest sessions to count. */
12
+ sessions: number;
13
+ /** Text read from one session file; the rest of a longer file is left uncounted. */
14
+ fileBytes: number;
15
+ /** Text read across all sessions of one request; sessions already counted cost nothing. */
16
+ totalBytes: number;
17
+ ms: number;
18
+ }
19
+
20
+ /** `sessions` plus the one extra session asked for must stay within the session listing's cap of 500. */
21
+ export const DEFAULT_FIRING_LIMITS: FiringLimits = {
22
+ sessions: 400,
23
+ fileBytes: 8 * 1024 * 1024,
24
+ totalBytes: 64 * 1024 * 1024,
25
+ ms: 8_000,
26
+ };
27
+ const MAX_RULES = 500;
28
+ const MAX_CACHED_FILES = 2_000;
29
+ const MAX_NAME_CHARS = 256;
30
+
31
+ /** One `ttsr_injection` line of an OMP session file: the rules injected after a match. */
32
+ const InjectionSchema = z.object({
33
+ type: z.literal("ttsr_injection"),
34
+ timestamp: z.string().optional(),
35
+ injectedRules: z.array(z.string()),
36
+ });
37
+
38
+ interface Tally {
39
+ count: number;
40
+ /** Epoch ms of the latest firing, or 0 when no entry had a readable time. */
41
+ last: number;
42
+ }
43
+
44
+ interface FileFirings {
45
+ mtimeMs: number;
46
+ size: number;
47
+ /** Bytes the counts cover: a larger bound than this one means the file must be read again. */
48
+ readBytes: number;
49
+ truncated: boolean;
50
+ rules: Map<string, Tally>;
51
+ }
52
+
53
+ /** Counts per session file, valid while its modification time and size are unchanged. */
54
+ const cache = new Map<string, FileFirings>();
55
+
56
+ function remember(path: string, firings: FileFirings): void {
57
+ cache.delete(path);
58
+ cache.set(path, firings);
59
+ if (cache.size > MAX_CACHED_FILES) {
60
+ const oldest = cache.keys().next().value;
61
+ if (oldest !== undefined) cache.delete(oldest);
62
+ }
63
+ }
64
+
65
+ function addTo(rules: Map<string, Tally>, name: string, at: number, count = 1): void {
66
+ const tally = rules.get(name) ?? { count: 0, last: 0 };
67
+ tally.count += count;
68
+ tally.last = Math.max(tally.last, at);
69
+ rules.set(name, tally);
70
+ }
71
+
72
+ const LINES_PER_TIME_CHECK = 1_000;
73
+
74
+ /** The counts of one file, or null when `expired` ended the read or the file could not be read. */
75
+ async function countFile(
76
+ path: string,
77
+ readBytes: number,
78
+ expired: () => boolean,
79
+ ): Promise<Map<string, Tally> | null> {
80
+ const rules = new Map<string, Tally>();
81
+ let lineCount = 0;
82
+ let handle: FileHandle;
83
+ try {
84
+ handle = await open(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
85
+ } catch {
86
+ return null;
87
+ }
88
+ try {
89
+ const lines = createInterface({
90
+ input: handle.createReadStream({ encoding: "utf8", end: readBytes - 1 }),
91
+ crlfDelay: Number.POSITIVE_INFINITY,
92
+ });
93
+ for await (const line of lines) {
94
+ lineCount += 1;
95
+ if (lineCount % LINES_PER_TIME_CHECK === 0 && expired()) return null;
96
+ if (!line.includes('"ttsr_injection"')) continue;
97
+ let parsed: unknown;
98
+ try {
99
+ parsed = JSON.parse(line);
100
+ } catch {
101
+ continue;
102
+ }
103
+ const entry = InjectionSchema.safeParse(parsed);
104
+ if (!entry.success) continue;
105
+ const at = Date.parse(entry.data.timestamp ?? "");
106
+ for (const name of entry.data.injectedRules) {
107
+ if (name && name.length <= MAX_NAME_CHARS) addTo(rules, name, Number.isNaN(at) ? 0 : at);
108
+ }
109
+ }
110
+ } catch {
111
+ return null;
112
+ } finally {
113
+ await handle.close().catch(() => undefined);
114
+ }
115
+ return expired() ? null : rules;
116
+ }
117
+
118
+ /**
119
+ * How often each rule fired, from the `ttsr_injection` entries of OMP's session files: the
120
+ * sessions of `cwd` when given, else every session in the store. Newest sessions first, bounded
121
+ * by `limits`; files already counted are reused while their mtime and size are unchanged.
122
+ */
123
+ export async function readRuleFirings(
124
+ input: { cwd?: string; sessionDir?: string },
125
+ environment: NodeJS.ProcessEnv = currentOmpEnvironment(),
126
+ limits: FiringLimits = DEFAULT_FIRING_LIMITS,
127
+ now: () => number = Date.now,
128
+ ): Promise<{ firings: OmpRuleFiring[]; sessions: number; partial: boolean }> {
129
+ const started = now();
130
+ const expired = () => now() - started > limits.ms;
131
+ let partial = false;
132
+ const signal = AbortSignal.timeout(limits.ms);
133
+ let found: Awaited<ReturnType<typeof listOmpSessionDescriptors>>;
134
+ try {
135
+ found = await listOmpSessionDescriptors(
136
+ {
137
+ // One more than wanted, to learn whether older sessions exist.
138
+ limit: limits.sessions + 1,
139
+ signal,
140
+ onTruncated: () => {
141
+ partial = true;
142
+ },
143
+ ...(input.cwd ? { cwd: input.cwd } : {}),
144
+ ...(input.sessionDir ? { sessionDir: input.sessionDir } : {}),
145
+ },
146
+ environment,
147
+ );
148
+ } catch (error) {
149
+ if (signal.aborted) return { firings: [], sessions: 0, partial: true };
150
+ throw error;
151
+ }
152
+ if (found.length > limits.sessions) partial = true;
153
+ const sessions = found.slice(0, limits.sessions);
154
+ const totals = new Map<string, Tally>();
155
+ let counted = 0;
156
+ let bytes = 0;
157
+ for (const session of sessions) {
158
+ const file = session.transcriptFile;
159
+ if (!file) continue;
160
+ if (expired()) {
161
+ partial = true;
162
+ break;
163
+ }
164
+ const info = await stat(file).catch(() => null);
165
+ if (!info?.isFile()) continue;
166
+ const readBytes = Math.min(info.size, limits.fileBytes);
167
+ let firings = cache.get(file);
168
+ if (
169
+ firings?.mtimeMs !== info.mtimeMs ||
170
+ firings.size !== info.size ||
171
+ firings.readBytes !== readBytes
172
+ ) {
173
+ if (bytes + readBytes > limits.totalBytes) {
174
+ partial = true;
175
+ break;
176
+ }
177
+ bytes += readBytes;
178
+ const rules = await countFile(file, readBytes, expired);
179
+ if (!rules) {
180
+ // Not cached: a file that could not be read completely is read again next time.
181
+ partial = true;
182
+ continue;
183
+ }
184
+ firings = {
185
+ mtimeMs: info.mtimeMs,
186
+ size: info.size,
187
+ readBytes,
188
+ truncated: info.size > limits.fileBytes,
189
+ rules,
190
+ };
191
+ remember(file, firings);
192
+ }
193
+ if (firings.truncated) partial = true;
194
+ counted += 1;
195
+ for (const [name, tally] of firings.rules) addTo(totals, name, tally.last, tally.count);
196
+ }
197
+ if (totals.size > MAX_RULES) partial = true;
198
+ const firings = [...totals]
199
+ .map(([name, tally]) => ({
200
+ name,
201
+ count: tally.count,
202
+ lastFiredAt: tally.last > 0 ? new Date(tally.last).toISOString() : null,
203
+ }))
204
+ .sort((left, right) => right.count - left.count || left.name.localeCompare(right.name))
205
+ .slice(0, MAX_RULES);
206
+ return { firings, sessions: counted, partial };
207
+ }
208
+
209
+ export function resolveListOmpRuleFirings(input: RpcInput<typeof listOmpRuleFirings>) {
210
+ return readRuleFirings(input);
211
+ }
@@ -0,0 +1,38 @@
1
+ import type { RpcInput } from "@getpaseo/plugin";
2
+ import type { setOmpRuleEnabled } from "../shared/omp-rules";
3
+ import {
4
+ type OmpListDependencies,
5
+ ompListDependencies,
6
+ requireOmpExecutable,
7
+ writeListEntry,
8
+ } from "./omp-list-setting";
9
+ import { inOmpScope } from "./omp-rules";
10
+ import { runSettingsMutation } from "./omp-settings";
11
+ import { OmpPublicError } from "./provider/security";
12
+
13
+ /** Off adds the rule to `ttsr.disabledRules`; on removes it. A workspace saves the whole project list. */
14
+ export async function setOmpRuleEnabledWithDependencies(
15
+ input: RpcInput<typeof setOmpRuleEnabled>,
16
+ dependencies: OmpListDependencies,
17
+ ): Promise<{ status: "ok" } | { status: "error"; message: string }> {
18
+ try {
19
+ const executable = await requireOmpExecutable(dependencies);
20
+ const change = {
21
+ cwd: input.cwd,
22
+ list: "ttsr.disabledRules" as const,
23
+ entry: input.name,
24
+ enabled: input.enabled,
25
+ };
26
+ await inOmpScope(input.cwd, dependencies, (runIn) =>
27
+ runSettingsMutation(() => writeListEntry(change, dependencies, executable, runIn)),
28
+ );
29
+ return { status: "ok" };
30
+ } catch (error) {
31
+ if (error instanceof OmpPublicError) return { status: "error", message: error.message };
32
+ throw error;
33
+ }
34
+ }
35
+
36
+ export function resolveSetOmpRuleEnabled(input: RpcInput<typeof setOmpRuleEnabled>) {
37
+ return setOmpRuleEnabledWithDependencies(input, ompListDependencies);
38
+ }
@@ -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
+ }