Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a925a6d1be |
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { assert, describe, expect, it } from "@effect/vitest";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
@@ -67,98 +67,6 @@ describe("ProcessDiagnostics", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("parses Windows CIM JSON process rows", () =>
|
||||
Effect.sync(() => {
|
||||
const rows = ProcessDiagnostics.parseWindowsProcessRows(`[
|
||||
{
|
||||
"ProcessId": 10,
|
||||
"ParentProcessId": 1,
|
||||
"Name": "node.exe",
|
||||
"CommandLine": "node server.js",
|
||||
"Status": "Running",
|
||||
"WorkingSetSize": 4096,
|
||||
"PercentProcessorTime": 12.5
|
||||
},
|
||||
{
|
||||
"ProcessId": "invalid",
|
||||
"ParentProcessId": 10,
|
||||
"Name": "ignored.exe"
|
||||
},
|
||||
{
|
||||
"ProcessId": 11,
|
||||
"ParentProcessId": 10,
|
||||
"Name": "agent.exe",
|
||||
"CommandLine": null,
|
||||
"Status": null,
|
||||
"WorkingSetSize": -128,
|
||||
"PercentProcessorTime": -1
|
||||
}
|
||||
]`);
|
||||
|
||||
assert.deepStrictEqual(rows, [
|
||||
{
|
||||
pid: 10,
|
||||
ppid: 1,
|
||||
pgid: null,
|
||||
status: "Running",
|
||||
cpuPercent: 12.5,
|
||||
rssBytes: 4096,
|
||||
elapsed: "",
|
||||
command: "node server.js",
|
||||
},
|
||||
{
|
||||
pid: 11,
|
||||
ppid: 10,
|
||||
pgid: null,
|
||||
status: "Live",
|
||||
cpuPercent: 0,
|
||||
rssBytes: 0,
|
||||
elapsed: "",
|
||||
command: "agent.exe",
|
||||
},
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("parses single Windows CIM JSON process objects", () =>
|
||||
Effect.sync(() => {
|
||||
const rows = ProcessDiagnostics.parseWindowsProcessRows(`{
|
||||
"ProcessId": 42,
|
||||
"ParentProcessId": 10,
|
||||
"Name": "agent.exe",
|
||||
"CommandLine": "",
|
||||
"WorkingSetSize": 2048,
|
||||
"PercentProcessorTime": 5
|
||||
}`);
|
||||
|
||||
assert.deepStrictEqual(rows, [
|
||||
{
|
||||
pid: 42,
|
||||
ppid: 10,
|
||||
pgid: null,
|
||||
status: "Live",
|
||||
cpuPercent: 5,
|
||||
rssBytes: 2048,
|
||||
elapsed: "",
|
||||
command: "agent.exe",
|
||||
},
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("ignores malformed Windows CIM JSON output", () =>
|
||||
Effect.sync(() => {
|
||||
assert.deepStrictEqual(ProcessDiagnostics.parseWindowsProcessRows("{not json"), []);
|
||||
assert.deepStrictEqual(
|
||||
ProcessDiagnostics.parseWindowsProcessRows(`{
|
||||
"ProcessId": 42,
|
||||
"Name": "missing-parent.exe"
|
||||
}`),
|
||||
[],
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("aggregates only descendants of the server process", () =>
|
||||
Effect.sync(() => {
|
||||
const diagnostics = ProcessDiagnostics.aggregateProcessDiagnostics({
|
||||
|
||||
@@ -28,7 +28,7 @@ export interface ProcessRow {
|
||||
readonly command: string;
|
||||
}
|
||||
|
||||
const PROCESS_QUERY_TIMEOUT = Duration.seconds(1);
|
||||
const PROCESS_QUERY_TIMEOUT_MS = 1_000;
|
||||
const POSIX_PROCESS_QUERY_COMMAND = "pid=,ppid=,pgid=,stat=,pcpu=,rss=,etime=,command=";
|
||||
const PROCESS_QUERY_MAX_OUTPUT_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
@@ -121,19 +121,6 @@ const ProcessDiagnosticsError = Schema.Union([
|
||||
type ProcessDiagnosticsError = typeof ProcessDiagnosticsError.Type;
|
||||
const isProcessDiagnosticsError = Schema.is(ProcessDiagnosticsError);
|
||||
|
||||
const WindowsCimProcessRecord = Schema.Struct({
|
||||
ProcessId: Schema.Number,
|
||||
ParentProcessId: Schema.Number,
|
||||
Name: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
CommandLine: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
Status: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
WorkingSetSize: Schema.optional(Schema.Number),
|
||||
PercentProcessorTime: Schema.optional(Schema.Number),
|
||||
});
|
||||
type WindowsCimProcessRecord = typeof WindowsCimProcessRecord.Type;
|
||||
const decodeWindowsProcessJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Json));
|
||||
const decodeWindowsProcessRecord = Schema.decodeUnknownOption(WindowsCimProcessRecord);
|
||||
|
||||
function parsePositiveInt(value: string): number | null {
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
return Number.isInteger(parsed) && parsed > 0 ? parsed : null;
|
||||
@@ -214,54 +201,51 @@ export function parsePosixProcessRows(output: string): ReadonlyArray<ProcessRow>
|
||||
return rows;
|
||||
}
|
||||
|
||||
function nonEmptyStringOption(value: string | null | undefined): Option.Option<string> {
|
||||
return typeof value === "string" && value.trim().length > 0 ? Option.some(value) : Option.none();
|
||||
function normalizeWindowsProcessRow(value: unknown): ProcessRow | null {
|
||||
if (typeof value !== "object" || value === null) return null;
|
||||
const record = value as Record<string, unknown>;
|
||||
const pid = typeof record.ProcessId === "number" ? record.ProcessId : null;
|
||||
const ppid = typeof record.ParentProcessId === "number" ? record.ParentProcessId : null;
|
||||
const commandLine =
|
||||
typeof record.CommandLine === "string" && record.CommandLine.trim().length > 0
|
||||
? record.CommandLine
|
||||
: typeof record.Name === "string"
|
||||
? record.Name
|
||||
: null;
|
||||
const workingSet =
|
||||
typeof record.WorkingSetSize === "number" && Number.isFinite(record.WorkingSetSize)
|
||||
? Math.max(0, Math.round(record.WorkingSetSize))
|
||||
: 0;
|
||||
const cpuPercent =
|
||||
typeof record.PercentProcessorTime === "number" && Number.isFinite(record.PercentProcessorTime)
|
||||
? Math.max(0, record.PercentProcessorTime)
|
||||
: 0;
|
||||
|
||||
if (!pid || pid <= 0 || ppid === null || ppid < 0 || !commandLine) return null;
|
||||
return {
|
||||
pid,
|
||||
ppid,
|
||||
pgid: null,
|
||||
status: typeof record.Status === "string" && record.Status.length > 0 ? record.Status : "Live",
|
||||
cpuPercent,
|
||||
rssBytes: workingSet,
|
||||
elapsed: "",
|
||||
command: commandLine,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeWindowsProcessRow(value: unknown): Option.Option<ProcessRow> {
|
||||
return Option.flatMap(decodeWindowsProcessRecord(value), (record: WindowsCimProcessRecord) => {
|
||||
const pid = record.ProcessId;
|
||||
const ppid = record.ParentProcessId;
|
||||
if (!Number.isInteger(pid) || pid <= 0 || !Number.isInteger(ppid) || ppid < 0) {
|
||||
return Option.none();
|
||||
}
|
||||
|
||||
const commandLine = nonEmptyStringOption(record.CommandLine).pipe(
|
||||
Option.orElse(() => nonEmptyStringOption(record.Name)),
|
||||
);
|
||||
return Option.map(commandLine, (command) => ({
|
||||
pid,
|
||||
ppid,
|
||||
pgid: null,
|
||||
status: Option.getOrElse(nonEmptyStringOption(record.Status), () => "Live"),
|
||||
cpuPercent:
|
||||
typeof record.PercentProcessorTime === "number" &&
|
||||
Number.isFinite(record.PercentProcessorTime)
|
||||
? Math.max(0, record.PercentProcessorTime)
|
||||
: 0,
|
||||
rssBytes:
|
||||
typeof record.WorkingSetSize === "number" && Number.isFinite(record.WorkingSetSize)
|
||||
? Math.max(0, Math.round(record.WorkingSetSize))
|
||||
: 0,
|
||||
elapsed: "",
|
||||
command,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
export function parseWindowsProcessRows(output: string): ReadonlyArray<ProcessRow> {
|
||||
function parseWindowsProcessRows(output: string): ReadonlyArray<ProcessRow> {
|
||||
if (output.trim().length === 0) return [];
|
||||
const parsed = decodeWindowsProcessJson(output);
|
||||
if (Option.isNone(parsed)) {
|
||||
try {
|
||||
const parsed = JSON.parse(output) as unknown;
|
||||
const records = Array.isArray(parsed) ? parsed : [parsed];
|
||||
return records.flatMap((record) => {
|
||||
const row = normalizeWindowsProcessRow(record);
|
||||
return row ? [row] : [];
|
||||
});
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
const records = Array.isArray(parsed.value) ? parsed.value : [parsed.value];
|
||||
return records.flatMap((record) =>
|
||||
Option.match(normalizeWindowsProcessRow(record), {
|
||||
onNone: () => [],
|
||||
onSome: (row) => [row],
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function buildDescendantEntries(
|
||||
@@ -397,7 +381,7 @@ const runProcess = Effect.fn("runProcess")(function* (input: {
|
||||
} satisfies ProcessOutput;
|
||||
}).pipe(
|
||||
Effect.scoped,
|
||||
Effect.timeoutOption(PROCESS_QUERY_TIMEOUT),
|
||||
Effect.timeoutOption(Duration.millis(PROCESS_QUERY_TIMEOUT_MS)),
|
||||
Effect.flatMap((result) =>
|
||||
Option.match(result, {
|
||||
onNone: () =>
|
||||
@@ -406,7 +390,7 @@ const runProcess = Effect.fn("runProcess")(function* (input: {
|
||||
command: input.command,
|
||||
argCount: input.args.length,
|
||||
cwd,
|
||||
timeoutMillis: Duration.toMillis(PROCESS_QUERY_TIMEOUT),
|
||||
timeoutMillis: PROCESS_QUERY_TIMEOUT_MS,
|
||||
}),
|
||||
),
|
||||
onSome: Effect.succeed,
|
||||
|
||||
@@ -47,6 +47,7 @@ import {
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
|
||||
import {
|
||||
@@ -886,28 +887,19 @@ interface ComposerPromptEditorProps {
|
||||
cursorAdjacentToMention: boolean,
|
||||
terminalContextIds: string[],
|
||||
) => void;
|
||||
onCommandKeyDown?: (
|
||||
key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
|
||||
event: KeyboardEvent,
|
||||
) => boolean;
|
||||
onCommandKeyDown?: ComposerCommandKeyDownHandler;
|
||||
onPaste: React.ClipboardEventHandler<HTMLElement>;
|
||||
editorRef: React.RefObject<ComposerPromptEditorHandle | null>;
|
||||
}
|
||||
|
||||
function ComposerCommandKeyPlugin(props: {
|
||||
onCommandKeyDown?: (
|
||||
key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
|
||||
event: KeyboardEvent,
|
||||
) => boolean;
|
||||
}) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
type ComposerCommandKey = "ArrowDown" | "ArrowUp" | "Enter" | "Tab";
|
||||
type ComposerCommandKeyDownHandler = (key: ComposerCommandKey, event: KeyboardEvent) => boolean;
|
||||
|
||||
useEffect(() => {
|
||||
const handleCommand = (
|
||||
key: "ArrowDown" | "ArrowUp" | "Enter" | "Tab",
|
||||
event: KeyboardEvent | null,
|
||||
): boolean => {
|
||||
if (!props.onCommandKeyDown || !event) {
|
||||
function useComposerCommandKeyCommands(onCommandKeyDown?: ComposerCommandKeyDownHandler) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const handleCommandKeyDown = useEffectEvent(
|
||||
(key: ComposerCommandKey, event: KeyboardEvent | null): boolean => {
|
||||
if (!onCommandKeyDown || !event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -916,32 +908,34 @@ function ComposerCommandKeyPlugin(props: {
|
||||
return true;
|
||||
}
|
||||
|
||||
const handled = props.onCommandKeyDown(key, event);
|
||||
const handled = onCommandKeyDown(key, event);
|
||||
if (handled) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
return handled;
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const unregisterArrowDown = editor.registerCommand(
|
||||
KEY_ARROW_DOWN_COMMAND,
|
||||
(event) => handleCommand("ArrowDown", event),
|
||||
(event) => handleCommandKeyDown("ArrowDown", event),
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
);
|
||||
const unregisterArrowUp = editor.registerCommand(
|
||||
KEY_ARROW_UP_COMMAND,
|
||||
(event) => handleCommand("ArrowUp", event),
|
||||
(event) => handleCommandKeyDown("ArrowUp", event),
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
);
|
||||
const unregisterEnter = editor.registerCommand(
|
||||
KEY_ENTER_COMMAND,
|
||||
(event) => handleCommand("Enter", event),
|
||||
(event) => handleCommandKeyDown("Enter", event),
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
);
|
||||
const unregisterTab = editor.registerCommand(
|
||||
KEY_TAB_COMMAND,
|
||||
(event) => handleCommand("Tab", event),
|
||||
(event) => handleCommandKeyDown("Tab", event),
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
);
|
||||
|
||||
@@ -951,9 +945,7 @@ function ComposerCommandKeyPlugin(props: {
|
||||
unregisterEnter();
|
||||
unregisterTab();
|
||||
};
|
||||
}, [editor, props]);
|
||||
|
||||
return null;
|
||||
}, [editor]);
|
||||
}
|
||||
|
||||
function ComposerInlineTokenArrowPlugin() {
|
||||
@@ -1123,7 +1115,8 @@ function ComposerSurroundSelectionPlugin(props: {
|
||||
}) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const terminalContextsRef = useRef(props.terminalContexts);
|
||||
const skillMetadataRef = useRef(skillMetadataByName(props.skills));
|
||||
const [initialSkillMetadata] = useState(() => skillMetadataByName(props.skills));
|
||||
const skillMetadataRef = useRef(initialSkillMetadata);
|
||||
const pendingSurroundSelectionRef = useRef<{
|
||||
value: string;
|
||||
expandedStart: number;
|
||||
@@ -1404,7 +1397,8 @@ function ComposerPromptEditorInner({
|
||||
const terminalContextsSignatureRef = useRef(terminalContextsSignature);
|
||||
const skillsSignature = skillSignature(skills);
|
||||
const skillsSignatureRef = useRef(skillsSignature);
|
||||
const skillMetadataRef = useRef(skillMetadataByName(skills));
|
||||
const [initialSkillMetadata] = useState(() => skillMetadataByName(skills));
|
||||
const skillMetadataRef = useRef(initialSkillMetadata);
|
||||
const snapshotRef = useRef({
|
||||
value,
|
||||
cursor: initialCursor,
|
||||
@@ -1429,6 +1423,8 @@ function ComposerPromptEditorInner({
|
||||
editor.setEditable(!disabled);
|
||||
}, [disabled, editor]);
|
||||
|
||||
useComposerCommandKeyCommands(onCommandKeyDown);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const normalizedCursor = clampCollapsedComposerCursor(value, cursor);
|
||||
const previousSnapshot = snapshotRef.current;
|
||||
@@ -1629,7 +1625,6 @@ function ComposerPromptEditorInner({
|
||||
ErrorBoundary={LexicalErrorBoundary}
|
||||
/>
|
||||
<OnChangePlugin onChange={handleEditorChange} />
|
||||
<ComposerCommandKeyPlugin {...(onCommandKeyDown ? { onCommandKeyDown } : {})} />
|
||||
<ComposerSurroundSelectionPlugin terminalContexts={terminalContexts} skills={skills} />
|
||||
<ComposerInlineTokenArrowPlugin />
|
||||
<ComposerInlineTokenSelectionNormalizePlugin />
|
||||
@@ -1656,7 +1651,8 @@ export function ComposerPromptEditor({
|
||||
}: ComposerPromptEditorProps) {
|
||||
const initialValueRef = useRef(value);
|
||||
const initialTerminalContextsRef = useRef(terminalContexts);
|
||||
const initialSkillMetadataRef = useRef(skillMetadataByName(skills));
|
||||
const [initialSkillMetadata] = useState(() => skillMetadataByName(skills));
|
||||
const initialSkillMetadataRef = useRef(initialSkillMetadata);
|
||||
const initialConfig = useMemo<InitialConfigType>(
|
||||
() => ({
|
||||
namespace: "t3tools-composer-editor",
|
||||
|
||||
Reference in new issue
Block a user