@tomflow/chatloop-tools 0.9.16 → 0.9.17
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/index.mjs +4 -0
- package/lib/gateway.mjs +15 -3
- package/lib/mcp-tool-router.mjs +50 -3
- package/node_modules/@tomflow/chatloop-context/README.md +240 -0
- package/node_modules/@tomflow/chatloop-context/bin/cli.mjs +48 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/approval.mjs +151 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/context.mjs +376 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/document.mjs +266 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/evidence.mjs +134 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/presentation.mjs +82 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/project.mjs +349 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/sdk.mjs +72 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/start.mjs +73 -0
- package/node_modules/@tomflow/chatloop-context/capabilities/task.mjs +507 -0
- package/node_modules/@tomflow/chatloop-context/capabilities.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-context/docs/interfaces.md +182 -0
- package/node_modules/@tomflow/chatloop-context/docs/operator-and-migration.md +277 -0
- package/node_modules/@tomflow/chatloop-context/index.mjs +233 -0
- package/node_modules/@tomflow/chatloop-context/lib/api-log.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/approval-policy.mjs +24 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-catalog.generated.json +1989 -0
- package/node_modules/@tomflow/chatloop-context/lib/capability-router.mjs +67 -0
- package/node_modules/@tomflow/chatloop-context/lib/cli-commands.mjs +280 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-cli.mjs +298 -0
- package/node_modules/@tomflow/chatloop-context/lib/context-migration.mjs +280 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/context.mjs +115 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/evidence.mjs +51 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/health.mjs +55 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/migration.mjs +262 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/project.mjs +246 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/review.mjs +147 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/run.mjs +343 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/store.mjs +391 -0
- package/node_modules/@tomflow/chatloop-context/lib/core/task.mjs +166 -0
- package/node_modules/@tomflow/chatloop-context/lib/line-review.mjs +13 -0
- package/node_modules/@tomflow/chatloop-context/lib/methods.mjs +111 -0
- package/node_modules/@tomflow/chatloop-context/lib/operations.mjs +107 -0
- package/node_modules/@tomflow/chatloop-context/lib/orphan-operator.mjs +117 -0
- package/node_modules/@tomflow/chatloop-context/lib/process-inventory.mjs +20 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-branch.mjs +312 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-diagnostic.mjs +153 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-guards.mjs +19 -0
- package/node_modules/@tomflow/chatloop-context/lib/project-store.mjs +115 -0
- package/node_modules/@tomflow/chatloop-context/lib/trusted-run.mjs +86 -0
- package/node_modules/@tomflow/chatloop-context/lib/workspace-operator.mjs +169 -0
- package/node_modules/@tomflow/chatloop-context/package.json +40 -0
- package/node_modules/@tomflow/chatloop-context/scripts/build-capabilities.mjs +114 -0
- package/node_modules/@tomflow/chatloop-extension/README.md +94 -0
- package/node_modules/@tomflow/chatloop-extension/actions.generated.mjs +11 -0
- package/node_modules/@tomflow/chatloop-extension/bin/cli.mjs +141 -0
- package/node_modules/@tomflow/chatloop-extension/bin/native-host.mjs +109 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_dom.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_screenshot.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/chat_text.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/clean.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/continue.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/handoff.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/reload.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/start.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/capabilities/status.mjs +1 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/clean.js +120 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/common.js +180 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/continue.js +322 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/dom.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/handoff.js +378 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/index.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/model-intent.js +54 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/read-mode.js +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/reload.js +20 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/screenshot.js +69 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/start.js +287 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/status.js +23 -0
- package/node_modules/@tomflow/chatloop-extension/extension/actions/text.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/background.js +16 -0
- package/node_modules/@tomflow/chatloop-extension/extension/clean.html +5 -0
- package/node_modules/@tomflow/chatloop-extension/extension/content.js +40 -0
- package/node_modules/@tomflow/chatloop-extension/extension/continue.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/dom.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/handoff.html +9 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/action-catalog.generated.js +2 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-aspect.generated.js +275 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/api-observation-queue.generated.js +76 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/core.js +107 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/index.js +36 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-approval.js +227 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-inspect.js +144 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-poll.js +171 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer-recovery.js +109 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/observer.js +154 -0
- package/node_modules/@tomflow/chatloop-extension/extension/loop/resources.js +254 -0
- package/node_modules/@tomflow/chatloop-extension/extension/manifest.json +42 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.html +25 -0
- package/node_modules/@tomflow/chatloop-extension/extension/popup.js +93 -0
- package/node_modules/@tomflow/chatloop-extension/extension/reload.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/screenshot.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/start.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/status.html +6 -0
- package/node_modules/@tomflow/chatloop-extension/extension/text.html +17 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-result.js +136 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/action-take.js +220 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/admission.js +122 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-prepare.js +155 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-recovery.js +185 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/adopt-result.js +120 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/bootstrap.js +172 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/manual.js +165 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/readiness.js +244 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/router.js +181 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/scheduler.js +147 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-failure.js +197 -0
- package/node_modules/@tomflow/chatloop-extension/extension/worker/turn-finished.js +200 -0
- package/node_modules/@tomflow/chatloop-extension/index.mjs +71 -0
- package/node_modules/@tomflow/chatloop-extension/lib/action-routing.mjs +18 -0
- package/node_modules/@tomflow/chatloop-extension/lib/api-observation-queue.mjs +74 -0
- package/node_modules/@tomflow/chatloop-extension/lib/bridge.mjs +208 -0
- package/node_modules/@tomflow/chatloop-extension/lib/browser-launch.mjs +40 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-binding.mjs +45 -0
- package/node_modules/@tomflow/chatloop-extension/lib/context-ipc.mjs +111 -0
- package/node_modules/@tomflow/chatloop-extension/lib/owner-evidence.mjs +67 -0
- package/node_modules/@tomflow/chatloop-extension/lib/session-file.mjs +120 -0
- package/node_modules/@tomflow/chatloop-extension/package.json +38 -0
- package/node_modules/@tomflow/chatloop-extension/scripts/build-actions.mjs +133 -0
- package/node_modules/@tomflow/chatloop-trace/README.md +105 -0
- package/node_modules/@tomflow/chatloop-trace/events/context.api.mjs +23 -0
- package/node_modules/@tomflow/chatloop-trace/events/typed.api.call.mjs +22 -0
- package/node_modules/@tomflow/chatloop-trace/events.generated.mjs +4 -0
- package/node_modules/@tomflow/chatloop-trace/index.mjs +59 -0
- package/node_modules/@tomflow/chatloop-trace/lib/api-aspect.mjs +273 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace-reader.mjs +265 -0
- package/node_modules/@tomflow/chatloop-trace/lib/trace.mjs +205 -0
- package/node_modules/@tomflow/chatloop-trace/package.json +30 -0
- package/node_modules/@tomflow/chatloop-trace/scripts/build-events.mjs +68 -0
- package/node_modules/ps-list/index.d.ts +76 -0
- package/node_modules/ps-list/index.js +366 -0
- package/node_modules/ps-list/license +9 -0
- package/node_modules/ps-list/package.json +44 -0
- package/node_modules/ps-list/readme.md +56 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe +0 -0
- package/node_modules/ps-list/vendor/fastlist-0.3.0-x86.exe +0 -0
- package/package.json +23 -8
- package/providers/form.mjs +68 -8
- package/scripts/build-form-app.mjs +27 -0
- package/ui/form/review-app.js +128 -0
- package/ui/form/review.html +859 -0
- package/ui/form/review.template.html +45 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import {promisify} from 'node:util';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
import childProcess from 'node:child_process';
|
|
7
|
+
|
|
8
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
|
|
10
|
+
const DEFAULT_MAX_BUFFER = 64_000_000; // 64 MB
|
|
11
|
+
const MAXIMUM_PATH_COMBINATION_ATTEMPTS = 6; // Limit path parsing attempts for performance
|
|
12
|
+
|
|
13
|
+
const execFile = promisify(childProcess.execFile);
|
|
14
|
+
|
|
15
|
+
// Process list field names (used in ps command output)
|
|
16
|
+
const PROCESS_FIELDS = {
|
|
17
|
+
CPU_PERCENT: '%cpu',
|
|
18
|
+
MEMORY_PERCENT: '%mem',
|
|
19
|
+
PROCESS_ID: 'pid',
|
|
20
|
+
PARENT_PROCESS_ID: 'ppid',
|
|
21
|
+
USER_ID: 'uid',
|
|
22
|
+
START_TIME: 'lstart',
|
|
23
|
+
COMMAND_NAME: 'comm',
|
|
24
|
+
ARGUMENTS: 'args',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Helper to build ps command flags consistently
|
|
28
|
+
const buildProcessCommandFlags = includeAllUsersProcesses => (includeAllUsersProcesses === false ? '' : 'a') + 'wwxo';
|
|
29
|
+
|
|
30
|
+
// Safe Date parsing with validation
|
|
31
|
+
const makeStartTime = startTimeString => {
|
|
32
|
+
if (!startTimeString) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const parsedDate = new Date(startTimeString);
|
|
37
|
+
return Number.isNaN(parsedDate.getTime()) ? undefined : parsedDate;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Extract executable path from command line using filesystem validation only
|
|
41
|
+
const extractExecutablePath = commandLine => {
|
|
42
|
+
if (!commandLine) {
|
|
43
|
+
return '';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Skip non-path commands (no filesystem validation possible)
|
|
47
|
+
if (!commandLine.startsWith('/') && !commandLine.startsWith('"')) {
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Handle quoted paths first: "/path with spaces/executable"
|
|
52
|
+
if (commandLine.startsWith('"')) {
|
|
53
|
+
const quotedPathMatch = commandLine.match(/^"([^"]+)"/);
|
|
54
|
+
if (quotedPathMatch && fs.existsSync(quotedPathMatch[1])) {
|
|
55
|
+
return quotedPathMatch[1];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// For unquoted absolute paths, try different strategies
|
|
62
|
+
const commandParts = commandLine.split(' ');
|
|
63
|
+
|
|
64
|
+
// Strategy 1: Simple first token (most common case)
|
|
65
|
+
const firstCommandToken = commandParts[0];
|
|
66
|
+
if (fs.existsSync(firstCommandToken)) {
|
|
67
|
+
return firstCommandToken;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Strategy 2: Progressive combination for paths with spaces
|
|
71
|
+
// Limit to reasonable number of attempts for performance
|
|
72
|
+
const maximumCombinationAttempts = Math.min(commandParts.length, MAXIMUM_PATH_COMBINATION_ATTEMPTS);
|
|
73
|
+
for (let tokenCount = 2; tokenCount <= maximumCombinationAttempts; tokenCount++) {
|
|
74
|
+
const candidateExecutablePath = commandParts.slice(0, tokenCount).join(' ');
|
|
75
|
+
if (fs.existsSync(candidateExecutablePath)) {
|
|
76
|
+
return candidateExecutablePath;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return '';
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Resolve executable path - simple approach
|
|
84
|
+
const resolveExecutablePath = (operatingSystemPlatform, processId, commandLine) => {
|
|
85
|
+
// On Linux, try /proc/{pid}/exe first for accurate path
|
|
86
|
+
if (operatingSystemPlatform === 'linux' && processId) {
|
|
87
|
+
try {
|
|
88
|
+
const symbolicLink = fs.readlinkSync(`/proc/${processId}/exe`);
|
|
89
|
+
return symbolicLink.replace(/\s+\(deleted\)$/, '');
|
|
90
|
+
} catch {
|
|
91
|
+
// Continue to other methods
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Extract path from command line
|
|
96
|
+
return extractExecutablePath(commandLine);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Parse and validate numeric field with fallback
|
|
100
|
+
const parseNumericField = (fieldValue, parserFunction = Number.parseInt, defaultValue = 0) => {
|
|
101
|
+
if (!fieldValue) {
|
|
102
|
+
return defaultValue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const parsedValue = parserFunction(fieldValue, 10);
|
|
106
|
+
return Number.isNaN(parsedValue) ? defaultValue : parsedValue;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Parse integer or return undefined - used for uid to avoid conflating unknown with root (0)
|
|
110
|
+
const parseIntegerOrUndefined = fieldValue => {
|
|
111
|
+
if (fieldValue === undefined || fieldValue === '') {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const parsedValue = Number.parseInt(fieldValue, 10);
|
|
116
|
+
return Number.isNaN(parsedValue) ? undefined : parsedValue;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Unified field parser
|
|
120
|
+
const parseProcessFields = ({processId, parentProcessId, userId, cpuUsage, memoryUsage, commandName, startTimeString, command}) => {
|
|
121
|
+
// Parse numeric fields with proper defaults
|
|
122
|
+
const parsedProcessId = parseNumericField(processId);
|
|
123
|
+
const parsedParentProcessId = parseNumericField(parentProcessId);
|
|
124
|
+
const parsedUserId = parseIntegerOrUndefined(userId);
|
|
125
|
+
const parsedCpuUsagePercentage = parseNumericField(cpuUsage, Number.parseFloat);
|
|
126
|
+
const parsedMemoryUsagePercentage = parseNumericField(memoryUsage, Number.parseFloat);
|
|
127
|
+
|
|
128
|
+
// Resolve executable path from command line
|
|
129
|
+
const resolvedExecutablePath = resolveExecutablePath(process.platform, parsedProcessId, command);
|
|
130
|
+
|
|
131
|
+
// Derive process name: prefer basename of path, fallback to command name
|
|
132
|
+
const derivedProcessName = resolvedExecutablePath ? path.basename(resolvedExecutablePath) : (commandName || '');
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
pid: parsedProcessId,
|
|
136
|
+
ppid: parsedParentProcessId,
|
|
137
|
+
uid: parsedUserId, // Undefined when uid can't be parsed (avoid conflating with root)
|
|
138
|
+
cpu: parsedCpuUsagePercentage,
|
|
139
|
+
memory: parsedMemoryUsagePercentage,
|
|
140
|
+
name: derivedProcessName,
|
|
141
|
+
path: resolvedExecutablePath,
|
|
142
|
+
startTime: makeStartTime(startTimeString),
|
|
143
|
+
cmd: command || '',
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const windows = async () => {
|
|
148
|
+
// Source: https://github.com/MarkTiedemann/fastlist
|
|
149
|
+
let binary;
|
|
150
|
+
switch (process.arch) {
|
|
151
|
+
case 'x64': {
|
|
152
|
+
binary = 'fastlist-0.3.0-x64.exe';
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
case 'ia32': {
|
|
157
|
+
binary = 'fastlist-0.3.0-x86.exe';
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
case 'arm64': {
|
|
162
|
+
throw new Error('Windows ARM64 is not supported yet.');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
default: {
|
|
166
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const binaryPath = path.join(__dirname, 'vendor', binary);
|
|
171
|
+
const {stdout} = await execFile(binaryPath, {
|
|
172
|
+
maxBuffer: DEFAULT_MAX_BUFFER,
|
|
173
|
+
windowsHide: true,
|
|
174
|
+
encoding: 'utf8',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
return stdout
|
|
178
|
+
.trim()
|
|
179
|
+
.split(/\r?\n/)
|
|
180
|
+
.map(line => line.split('\t'))
|
|
181
|
+
.map(([processId, parentProcessId, processName]) => ({
|
|
182
|
+
pid: Number.parseInt(processId, 10),
|
|
183
|
+
ppid: Number.parseInt(parentProcessId, 10),
|
|
184
|
+
name: processName,
|
|
185
|
+
}));
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Fallback implementation with multiple ps calls
|
|
189
|
+
const nonWindowsFallbackMultipleCalls = async (options = {}) => {
|
|
190
|
+
const processDataByProcessId = {};
|
|
191
|
+
const processCommandFlags = buildProcessCommandFlags(options.all);
|
|
192
|
+
|
|
193
|
+
const fields = [
|
|
194
|
+
PROCESS_FIELDS.COMMAND_NAME,
|
|
195
|
+
PROCESS_FIELDS.ARGUMENTS,
|
|
196
|
+
PROCESS_FIELDS.PARENT_PROCESS_ID,
|
|
197
|
+
PROCESS_FIELDS.USER_ID,
|
|
198
|
+
PROCESS_FIELDS.CPU_PERCENT,
|
|
199
|
+
PROCESS_FIELDS.MEMORY_PERCENT,
|
|
200
|
+
PROCESS_FIELDS.START_TIME,
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
await Promise.all(fields.map(async fieldName => {
|
|
204
|
+
// Use headerless output with = syntax
|
|
205
|
+
const {stdout} = await execFile('ps', [processCommandFlags, `${PROCESS_FIELDS.PROCESS_ID}=,${fieldName}=`], {
|
|
206
|
+
maxBuffer: DEFAULT_MAX_BUFFER,
|
|
207
|
+
encoding: 'utf8',
|
|
208
|
+
env: {
|
|
209
|
+
...process.env,
|
|
210
|
+
LC_ALL: 'C',
|
|
211
|
+
LANG: 'C',
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
for (const line of stdout.trim().split('\n')) {
|
|
216
|
+
const trimmedLine = line.trim();
|
|
217
|
+
const spaceIndex = trimmedLine.indexOf(' ');
|
|
218
|
+
if (spaceIndex === -1) {
|
|
219
|
+
// No space found - treat entire line as processId with empty field value
|
|
220
|
+
const processId = trimmedLine;
|
|
221
|
+
const fieldValue = '';
|
|
222
|
+
processDataByProcessId[processId] ??= {};
|
|
223
|
+
processDataByProcessId[processId][fieldName] = fieldValue;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const processId = trimmedLine.slice(0, spaceIndex);
|
|
228
|
+
const fieldValue = trimmedLine.slice(spaceIndex + 1).trim();
|
|
229
|
+
|
|
230
|
+
processDataByProcessId[processId] ??= {};
|
|
231
|
+
processDataByProcessId[processId][fieldName] = fieldValue;
|
|
232
|
+
}
|
|
233
|
+
}));
|
|
234
|
+
|
|
235
|
+
// Filter out incomplete entries and convert to process objects
|
|
236
|
+
return Object.entries(processDataByProcessId)
|
|
237
|
+
.filter(([, data]) => data[PROCESS_FIELDS.COMMAND_NAME] && data[PROCESS_FIELDS.PARENT_PROCESS_ID] !== undefined)
|
|
238
|
+
.map(([processId, data]) => parseProcessFields({
|
|
239
|
+
processId,
|
|
240
|
+
parentProcessId: data[PROCESS_FIELDS.PARENT_PROCESS_ID],
|
|
241
|
+
userId: data[PROCESS_FIELDS.USER_ID],
|
|
242
|
+
cpuUsage: data[PROCESS_FIELDS.CPU_PERCENT],
|
|
243
|
+
memoryUsage: data[PROCESS_FIELDS.MEMORY_PERCENT],
|
|
244
|
+
commandName: data[PROCESS_FIELDS.COMMAND_NAME],
|
|
245
|
+
startTimeString: data[PROCESS_FIELDS.START_TIME],
|
|
246
|
+
command: data[PROCESS_FIELDS.ARGUMENTS] ?? '',
|
|
247
|
+
}));
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// Main implementation with headerless output and simpler parsing
|
|
251
|
+
const nonWindowsCall = async (options = {}) => {
|
|
252
|
+
const processCommandFlags = buildProcessCommandFlags(options.all);
|
|
253
|
+
|
|
254
|
+
// Common execFile options to avoid duplication
|
|
255
|
+
const executeFileOptions = {
|
|
256
|
+
maxBuffer: DEFAULT_MAX_BUFFER,
|
|
257
|
+
encoding: 'utf8',
|
|
258
|
+
env: {
|
|
259
|
+
...process.env,
|
|
260
|
+
LC_ALL: 'C',
|
|
261
|
+
LANG: 'C',
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// Build field lists for ps commands
|
|
266
|
+
const processFieldCommaSeparatedList = [
|
|
267
|
+
PROCESS_FIELDS.PROCESS_ID,
|
|
268
|
+
PROCESS_FIELDS.PARENT_PROCESS_ID,
|
|
269
|
+
PROCESS_FIELDS.USER_ID,
|
|
270
|
+
PROCESS_FIELDS.CPU_PERCENT,
|
|
271
|
+
PROCESS_FIELDS.MEMORY_PERCENT,
|
|
272
|
+
PROCESS_FIELDS.START_TIME,
|
|
273
|
+
PROCESS_FIELDS.COMMAND_NAME,
|
|
274
|
+
].map(fieldName => `${fieldName}=`).join(',');
|
|
275
|
+
|
|
276
|
+
const commandFieldCommaSeparatedList = [
|
|
277
|
+
PROCESS_FIELDS.PROCESS_ID,
|
|
278
|
+
PROCESS_FIELDS.ARGUMENTS,
|
|
279
|
+
].map(fieldName => `${fieldName}=`).join(',');
|
|
280
|
+
|
|
281
|
+
// Use headerless output with = syntax for consistent parsing
|
|
282
|
+
const processListingPromises = [
|
|
283
|
+
execFile('ps', [processCommandFlags, processFieldCommaSeparatedList], executeFileOptions),
|
|
284
|
+
execFile('ps', [processCommandFlags, commandFieldCommaSeparatedList], executeFileOptions),
|
|
285
|
+
];
|
|
286
|
+
|
|
287
|
+
const [processOutput, commandOutput] = await Promise.all(processListingPromises);
|
|
288
|
+
const processLines = processOutput.stdout.trim().split('\n');
|
|
289
|
+
const commandLines = commandOutput.stdout.trim().split('\n');
|
|
290
|
+
|
|
291
|
+
// Build command lookup map
|
|
292
|
+
const commandLinesByProcessId = {};
|
|
293
|
+
for (const line of commandLines) {
|
|
294
|
+
const trimmedLine = line.trim();
|
|
295
|
+
const spaceIndex = trimmedLine.indexOf(' ');
|
|
296
|
+
if (spaceIndex === -1) {
|
|
297
|
+
// No space found - treat entire line as processId with empty command
|
|
298
|
+
const processId = trimmedLine;
|
|
299
|
+
commandLinesByProcessId[processId] = '';
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const processId = trimmedLine.slice(0, spaceIndex);
|
|
304
|
+
const command = trimmedLine.slice(spaceIndex + 1).trim();
|
|
305
|
+
commandLinesByProcessId[processId] = command;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Parse process lines - format: pid ppid uid cpu mem lstart comm
|
|
309
|
+
const processes = [];
|
|
310
|
+
for (const line of processLines) {
|
|
311
|
+
const trimmedLine = line.trim();
|
|
312
|
+
if (!trimmedLine) {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Parse fixed-width fields more carefully
|
|
317
|
+
// Format: "pid ppid uid cpu mem Day Mon DD HH:MM:SS YYYY comm"
|
|
318
|
+
// First 5 fields should be numeric values (pid, ppid, uid are integers; cpu, memory are floats)
|
|
319
|
+
// Regex breakdown: ^(\d+) = pid, \s+(\d+) = ppid, \s+(\d+) = uid, \s+([\d.]+) = cpu%, \s+([\d.]+) = mem%, \s+(.+) = rest
|
|
320
|
+
const processLineRegexMatch = trimmedLine.match(/^(\d+)\s+(\d+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)\s+(.+)/);
|
|
321
|
+
if (!processLineRegexMatch) {
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const [, processId, parentProcessId, userId, cpuUsage, memoryUsage, dateAndCommandPortion] = processLineRegexMatch;
|
|
326
|
+
|
|
327
|
+
// Parse lstart from dateAndCommandPortion - it's always: "Day Mon DD HH:MM:SS YYYY"
|
|
328
|
+
// This regex handles both single and double digit days
|
|
329
|
+
// Regex breakdown: ((?:\w{3}\s+){2} = "Day Mon ", \d{1,2} = day, \s+(?:\d{2}:){2}\d{2} = "HH:MM:SS", \s+\d{4}) = " YYYY"
|
|
330
|
+
const startTimeRegexMatch = dateAndCommandPortion.match(/^((?:\w{3}\s+){2}\d{1,2}\s+(?:\d{2}:){2}\d{2}\s+\d{4})\s+(.*)$/);
|
|
331
|
+
|
|
332
|
+
let startTimeString = '';
|
|
333
|
+
let processCommandName = dateAndCommandPortion; // Fallback if start time parsing fails
|
|
334
|
+
|
|
335
|
+
if (startTimeRegexMatch) {
|
|
336
|
+
startTimeString = startTimeRegexMatch[1];
|
|
337
|
+
processCommandName = startTimeRegexMatch[2] || '';
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
processes.push(parseProcessFields({
|
|
341
|
+
processId,
|
|
342
|
+
parentProcessId,
|
|
343
|
+
userId,
|
|
344
|
+
cpuUsage,
|
|
345
|
+
memoryUsage,
|
|
346
|
+
commandName: processCommandName,
|
|
347
|
+
startTimeString,
|
|
348
|
+
command: commandLinesByProcessId[processId] ?? '',
|
|
349
|
+
}));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return processes;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const nonWindows = async (options = {}) => {
|
|
356
|
+
try {
|
|
357
|
+
return await nonWindowsCall(options);
|
|
358
|
+
} catch {
|
|
359
|
+
// Fallback to multiple calls if main approach fails
|
|
360
|
+
return nonWindowsFallbackMultipleCalls(options);
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const psList = process.platform === 'win32' ? windows : nonWindows;
|
|
365
|
+
|
|
366
|
+
export default psList;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ps-list",
|
|
3
|
+
"version": "9.0.0",
|
|
4
|
+
"description": "Get running processes",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "sindresorhus/ps-list",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Sindre Sorhus",
|
|
10
|
+
"email": "sindresorhus@gmail.com",
|
|
11
|
+
"url": "https://sindresorhus.com"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"types": "./index.d.ts",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=20"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"test": "xo && ava && tsd"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.js",
|
|
27
|
+
"index.d.ts",
|
|
28
|
+
"vendor/*.exe"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"ps",
|
|
32
|
+
"proc",
|
|
33
|
+
"process",
|
|
34
|
+
"processes",
|
|
35
|
+
"list",
|
|
36
|
+
"running",
|
|
37
|
+
"tasklist"
|
|
38
|
+
],
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"ava": "^6.4.1",
|
|
41
|
+
"tsd": "^0.33.0",
|
|
42
|
+
"xo": "^0.59.0"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# ps-list
|
|
2
|
+
|
|
3
|
+
> Get running processes
|
|
4
|
+
|
|
5
|
+
Works on macOS, Linux, and Windows. Windows ARM64 is not supported yet.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install ps-list
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import psList from 'ps-list';
|
|
17
|
+
|
|
18
|
+
console.log(await psList());
|
|
19
|
+
//=> [{pid: 3213, name: 'node', cmd: 'node test.js', ppid: 1, uid: 501, cpu: 0.1, memory: 1.5, path: '/usr/local/bin/node', startTime: 2025-01-15T10:30:00.000Z}, …]
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
### psList(options?)
|
|
25
|
+
|
|
26
|
+
Returns a `Promise<ProcessDescriptor[]>` with the running processes.
|
|
27
|
+
|
|
28
|
+
On macOS and Linux:
|
|
29
|
+
- The `name` property is truncated to 15 characters by the system
|
|
30
|
+
- The `cmd` property contains the full command line with arguments
|
|
31
|
+
- The `cpu` property is the CPU usage percentage (0-100)
|
|
32
|
+
- The `memory` property is the memory usage percentage (0-100)
|
|
33
|
+
- The `path` property is a best-effort attempt to get the full executable path:
|
|
34
|
+
- On Linux: reads from `/proc/{pid}/exe` when available
|
|
35
|
+
- On macOS: extracted from command line when possible
|
|
36
|
+
- Falls back to `comm` (which may be truncated)
|
|
37
|
+
- The `startTime` property contains the process start time as a Date object
|
|
38
|
+
|
|
39
|
+
The `cmd`, `cpu`, `memory`, `uid`, `path`, and `startTime` properties are not available on Windows.
|
|
40
|
+
|
|
41
|
+
#### options
|
|
42
|
+
|
|
43
|
+
Type: `object`
|
|
44
|
+
|
|
45
|
+
##### all
|
|
46
|
+
|
|
47
|
+
Type: `boolean`\
|
|
48
|
+
Default: `true`
|
|
49
|
+
|
|
50
|
+
Include other users' processes as well as your own.
|
|
51
|
+
|
|
52
|
+
On Windows this has no effect and will always be the user's own processes.
|
|
53
|
+
|
|
54
|
+
## Related
|
|
55
|
+
|
|
56
|
+
- [fastlist](https://github.com/MarkTiedemann/fastlist) - The binary used in this module to list the running processes on Windows
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.9.
|
|
6
|
+
"version": "0.9.17",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"description": "Chat Loop local tools: MCP gateway, isolated bundled tunnel-client and unified CLI",
|
|
9
9
|
"bin": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"index.mjs",
|
|
14
14
|
"providers.generated.mjs",
|
|
15
15
|
"providers/",
|
|
16
|
+
"ui/",
|
|
16
17
|
"lib/",
|
|
17
18
|
"bin/",
|
|
18
19
|
"scripts/",
|
|
@@ -24,22 +25,36 @@
|
|
|
24
25
|
"test": "node --test ./test/*.test.mjs",
|
|
25
26
|
"build:providers": "node ./scripts/build-providers.mjs",
|
|
26
27
|
"build:providers:check": "node ./scripts/build-providers.mjs --check",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
28
|
+
"build:form": "node ./scripts/build-form-app.mjs",
|
|
29
|
+
"build:form:check": "node ./scripts/build-form-app.mjs --check",
|
|
30
|
+
"pretest": "node ./scripts/build-providers.mjs --check && node ./scripts/build-form-app.mjs --check",
|
|
31
|
+
"prepack": "node ./scripts/build-providers.mjs --check && node ./scripts/build-form-app.mjs --check"
|
|
29
32
|
},
|
|
30
33
|
"dependencies": {
|
|
31
|
-
"@wonderwhy-er/desktop-commander": "0.2.44",
|
|
32
34
|
"@tomflow/chatloop-context": "0.6.11",
|
|
33
|
-
"@tomflow/chatloop-trace": "0.4.4"
|
|
35
|
+
"@tomflow/chatloop-trace": "0.4.4",
|
|
36
|
+
"@wonderwhy-er/desktop-commander": "0.2.44",
|
|
37
|
+
"ps-list": "9.0.0",
|
|
38
|
+
"@tomflow/chatloop-extension": "0.4.11"
|
|
34
39
|
},
|
|
35
40
|
"engines": {
|
|
36
41
|
"node": ">=24"
|
|
37
42
|
},
|
|
38
43
|
"optionalDependencies": {
|
|
39
|
-
"@tomflow/chatloop-tunnel-darwin-
|
|
40
|
-
"@tomflow/chatloop-tunnel-darwin-
|
|
44
|
+
"@tomflow/chatloop-tunnel-darwin-arm64": "0.0.24",
|
|
45
|
+
"@tomflow/chatloop-tunnel-darwin-x64": "0.0.24"
|
|
41
46
|
},
|
|
42
47
|
"exports": {
|
|
43
48
|
".": "./index.mjs"
|
|
44
|
-
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@modelcontextprotocol/ext-apps": "1.7.5",
|
|
52
|
+
"esbuild": "0.28.2"
|
|
53
|
+
},
|
|
54
|
+
"bundleDependencies": [
|
|
55
|
+
"@tomflow/chatloop-context",
|
|
56
|
+
"@tomflow/chatloop-trace",
|
|
57
|
+
"ps-list",
|
|
58
|
+
"@tomflow/chatloop-extension"
|
|
59
|
+
]
|
|
45
60
|
}
|
package/providers/form.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// Fixed ChatGPT card adapter, not a second approval authority.
|
|
2
2
|
// Form IDs identify interactions; Context Issue IDs own R/P decisions.
|
|
3
3
|
import {randomUUID} from 'node:crypto';
|
|
4
|
+
import {readFileSync} from 'node:fs';
|
|
5
|
+
const RESOURCE_URI='ui://chat-loop/form/review-v1';
|
|
6
|
+
const RESOURCE_MIME='text/html;profile=mcp-app';
|
|
4
7
|
const ID=/^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
|
|
5
8
|
const FORM_ID=/^[A-Za-z0-9_-]{8,80}$/;
|
|
6
9
|
const UUID=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
@@ -8,7 +11,8 @@ const ISSUE=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
8
11
|
const HASH=/^[a-f0-9]{64}$/;
|
|
9
12
|
const fail=code=>Object.assign(Error(code),{code});
|
|
10
13
|
const err=code=>({isError:true,content:[{type:'text',text:code}]});
|
|
11
|
-
const ok=value=>({content:[{type:'text',text:JSON.stringify(value)}]
|
|
14
|
+
const ok=value=>({content:[{type:'text',text:JSON.stringify(value)}],
|
|
15
|
+
structuredContent:value});
|
|
12
16
|
const filled=(s,max)=>typeof s==='string'&&s.trim().length>0&&s.length<=max;
|
|
13
17
|
const shape=(v,fields)=>v&&typeof v==='object'&&!Array.isArray(v)
|
|
14
18
|
&&Object.keys(v).every(k=>fields.includes(k));
|
|
@@ -42,7 +46,9 @@ const tools=[
|
|
|
42
46
|
required:['mode','card'],
|
|
43
47
|
properties:{...common,contentHash:{type:'string',pattern:HASH.source},
|
|
44
48
|
card:cardSchema}},
|
|
45
|
-
annotations:{readOnlyHint:false}
|
|
49
|
+
annotations:{readOnlyHint:false},
|
|
50
|
+
_meta:{ui:{resourceUri:RESOURCE_URI,visibility:['model','app']},
|
|
51
|
+
'openai/outputTemplate':RESOURCE_URI,'openai/widgetAccessible':true}},
|
|
46
52
|
{name:'chat_loop_form__submit',description:
|
|
47
53
|
'Record a caller-asserted ChatGPT user-turn A/B choice for an existing formId; GENERAL/TEST_ONLY is SOFT observation only. MANAGED delegates to Context Issue authority with exact issueId, revisions and original writeRequestId. No click authentication implied.',
|
|
48
54
|
inputSchema:{type:'object',additionalProperties:false,
|
|
@@ -52,7 +58,9 @@ const tools=[
|
|
|
52
58
|
comment:{type:'string',maxLength:4000},
|
|
53
59
|
userTurn:{type:'string',minLength:8,maxLength:256},
|
|
54
60
|
presentedAssistantRef:{type:'string',minLength:8,maxLength:256}}},
|
|
55
|
-
annotations:{readOnlyHint:false}
|
|
61
|
+
annotations:{readOnlyHint:false},
|
|
62
|
+
_meta:{ui:{visibility:['model','app']},
|
|
63
|
+
'openai/widgetAccessible':true}}
|
|
56
64
|
];
|
|
57
65
|
function check(a,fields){
|
|
58
66
|
if(!shape(a,fields)||!['GENERAL','TEST_ONLY','MANAGED'].includes(a.mode)
|
|
@@ -127,7 +135,9 @@ async function prepare(a,context,form){
|
|
|
127
135
|
const formId=a.formId||randomUUID();
|
|
128
136
|
if(a.mode!=='MANAGED'){
|
|
129
137
|
return {status:a.mode==='TEST_ONLY'?'TEST_ONLY_PREVIEW':'FORM_PREPARED_SOFT',
|
|
130
|
-
formId,kind:a.kind||'CUSTOM',
|
|
138
|
+
formId,mode:a.mode,kind:a.kind||'CUSTOM',
|
|
139
|
+
...(a.projectId?{projectId:a.projectId}:{}),
|
|
140
|
+
...(a.chatRef?{chatRef:a.chatRef}:{}),
|
|
131
141
|
...(a.documentId?{documentId:a.documentId,version:a.version}:{}),
|
|
132
142
|
view:view(a,formId,null,a.kind!=='CUSTOM'&&a.version>1
|
|
133
143
|
?a.documentId[0]+(a.version-1):null),
|
|
@@ -156,13 +166,14 @@ async function prepare(a,context,form){
|
|
|
156
166
|
// a caller-supplied formId is a separate non-authoritative UI correlation ID.
|
|
157
167
|
const managedFormId=a.formId||issue.issueId;
|
|
158
168
|
return {status:'WAITING_APPROVAL',formId:managedFormId,issueId:issue.issueId,
|
|
169
|
+
mode:a.mode,projectId:a.projectId,taskId:a.taskId,
|
|
159
170
|
documentId:a.documentId,version:a.version,kind:a.kind,
|
|
160
171
|
taskRevision:issue.taskRevision,approvalRevision:issue.approvalRevision,
|
|
161
172
|
writeRequestId:a.writeRequestId,trace:trace(form),
|
|
162
173
|
view:view(a,managedFormId,issue.issueId,prior),approvalAuthority:'CONTEXT',
|
|
163
174
|
businessSideEffects:true};
|
|
164
175
|
}
|
|
165
|
-
|
|
176
|
+
function validateSubmit(a){
|
|
166
177
|
check(a,SUBMIT);
|
|
167
178
|
if(!FORM_ID.test(a.formId||'')||!['A','B'].includes(a.choice)
|
|
168
179
|
||!filled(a.userTurn,256)||!filled(a.presentedAssistantRef,256)
|
|
@@ -171,6 +182,11 @@ async function submit(a,context,form){
|
|
|
171
182
|
if(a.choice==='B'&&!filled(a.comment,4000)
|
|
172
183
|
||a.choice==='A'&&a.comment!==undefined&&a.comment!=='')
|
|
173
184
|
throw fail('FORM_COMMENT_INVALID');
|
|
185
|
+
if(a.mode==='MANAGED'&&!ISSUE.test(a.issueId||''))
|
|
186
|
+
throw fail('FORM_ISSUE_REF_REQUIRED');
|
|
187
|
+
}
|
|
188
|
+
async function submit(a,context,form){
|
|
189
|
+
validateSubmit(a);
|
|
174
190
|
if(a.mode!=='MANAGED'){
|
|
175
191
|
return {status:'FORM_SUBMISSION_OBSERVED_SOFT',formId:a.formId,
|
|
176
192
|
choice:a.choice,trace:trace(form),approvalAuthority:'NONE',
|
|
@@ -199,13 +215,57 @@ async function submit(a,context,form){
|
|
|
199
215
|
export const provider=Object.freeze({
|
|
200
216
|
id:'form',order:5,names:tools.map(x=>x.name),
|
|
201
217
|
create({context,form}={}){
|
|
218
|
+
// Bounded, process-local UI correlation only. Context is still the
|
|
219
|
+
// authoritative MANAGED Issue store; a cache hit is NOT user proof.
|
|
220
|
+
const forms=new Map(),TTL=30*60*1000;
|
|
221
|
+
const lookup=id=>{
|
|
222
|
+
const record=forms.get(id);
|
|
223
|
+
if(!record||Date.now()-record.at>TTL){forms.delete(id);return null}
|
|
224
|
+
return record;
|
|
225
|
+
};
|
|
202
226
|
return Object.freeze({
|
|
203
227
|
tools:()=>tools,
|
|
228
|
+
resources:()=>[{uri:RESOURCE_URI,name:'Chat Loop Review',
|
|
229
|
+
mimeType:RESOURCE_MIME,description:'A/B review with one locked submit'}],
|
|
230
|
+
async readResource(uri){
|
|
231
|
+
if(uri!==RESOURCE_URI)throw fail('MCP_RESOURCE_NOT_FOUND');
|
|
232
|
+
return {contents:[{uri,mimeType:RESOURCE_MIME,
|
|
233
|
+
text:readFileSync(new URL('../ui/form/review.html',import.meta.url),'utf8')}]};
|
|
234
|
+
},
|
|
204
235
|
async call(name,args){
|
|
205
236
|
try{
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
237
|
+
const isPrepare=name===tools[0].name;
|
|
238
|
+
const isSubmit=name===tools[1].name;
|
|
239
|
+
if(isSubmit){
|
|
240
|
+
validateSubmit(args);
|
|
241
|
+
const prior=lookup(args?.formId);
|
|
242
|
+
if(!prior)throw fail('FORM_NOT_PREPARED');
|
|
243
|
+
if(prior.submitted)throw fail('FORM_ALREADY_SUBMITTED');
|
|
244
|
+
for(const key of ['mode','projectId','chatRef','taskId','kind',
|
|
245
|
+
'documentId','version','issueId']){
|
|
246
|
+
if(args.mode==='GENERAL'&&key==='kind'&&args.kind===undefined)
|
|
247
|
+
continue; // Backward-compatible optional generic kind.
|
|
248
|
+
if(prior.args[key]!==args?.[key]&&(
|
|
249
|
+
prior.args[key]!==undefined||args?.[key]!==undefined))
|
|
250
|
+
throw fail('FORM_SCOPE_MISMATCH');
|
|
251
|
+
}
|
|
252
|
+
// Mark before async Context call. UNKNOWN is never retried with
|
|
253
|
+
// a different request ID; the real Context receipt remains owner.
|
|
254
|
+
prior.submitted=true;
|
|
255
|
+
}
|
|
256
|
+
const result=isPrepare?await prepare(args,context,form)
|
|
257
|
+
:isSubmit?await submit(args,context,form):null;
|
|
258
|
+
if(isSubmit&&result&&args.mode!=='MANAGED')
|
|
259
|
+
result.preparedFormIdVerified=true;
|
|
260
|
+
if(isPrepare&&result){
|
|
261
|
+
for(const [id,record] of forms){
|
|
262
|
+
if(Date.now()-record.at>TTL)forms.delete(id);
|
|
263
|
+
}
|
|
264
|
+
if(forms.size>=256)forms.delete(forms.keys().next().value);
|
|
265
|
+
forms.set(result.formId,{at:Date.now(),submitted:false,
|
|
266
|
+
args:{...args,kind:result.kind,formId:result.formId,
|
|
267
|
+
issueId:result.issueId}});
|
|
268
|
+
}
|
|
209
269
|
if(result&&args?.projectId&&typeof form?.observe==='function'){
|
|
210
270
|
const parent=trace(form);
|
|
211
271
|
if(parent)setImmediate(()=>{
|