@zieglar/pi-chat-exchange 0.2.2 → 0.3.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.
- package/README.md +11 -7
- package/dist/bin/mcp-stdio.js +124 -56
- package/dist/control/mcp/endpoint-discovery.js +60 -0
- package/dist/control/mcp/messaging-tools.js +12 -2
- package/dist/control/mcp/server.js +10 -2
- package/dist/hooks/config.js +106 -0
- package/dist/hooks/events.js +19 -0
- package/dist/hooks/runner.js +187 -0
- package/dist/host/bound-endpoint.js +19 -6
- package/dist/host/endpoint-host.js +92 -4
- package/dist/host/mcp-binding-config.js +41 -5
- package/dist/runtime/native-readiness.js +8 -5
- package/dist/storage/creation.js +16 -0
- package/dist/storage/ownership.js +45 -1
- package/dist/tunnel/background-authorization.js +343 -0
- package/dist/tunnel/background-manager.js +104 -8
- package/dist/tunnel/background-scope.js +69 -0
- package/dist/tunnel/codes.js +1 -1
- package/dist/tunnel/hooks-management-ui.js +225 -0
- package/dist/tunnel/hooks-management.js +431 -0
- package/dist/tunnel/live-adoption-observation.js +136 -0
- package/dist/tunnel/live-adoption-probe.js +240 -0
- package/dist/tunnel/live-connection-status.js +136 -0
- package/dist/tunnel/live-mcp-composition.js +143 -0
- package/dist/tunnel/live-process-observation.js +98 -0
- package/dist/tunnel/live-session-migration.js +169 -0
- package/dist/tunnel/live-session-policy.js +578 -0
- package/dist/tunnel/manager-panel.js +4 -2
- package/dist/tunnel/manager-ui.js +71 -15
- package/dist/tunnel/multi-session-admission.js +1 -1
- package/dist/tunnel/multi-session-policy.js +45 -13
- package/dist/tunnel/setup-ui.js +2 -4
- package/dist/tunnel/status-ui.js +34 -0
- package/package.json +4 -1
- package/pi-extension/allowlist.example.json +12 -1
- package/pi-extension/desktop-target.ts +157 -0
- package/pi-extension/index.ts +1128 -132
- package/pi-extension/live-session-profile.ts +49 -0
- package/pi-extension/live-session-runtime.ts +59 -0
- package/pi-extension/multi-session-commands.ts +114 -3
- package/pi-extension/multi-session-runtime.ts +63 -11
- package/pi-extension/native-inbound.ts +235 -0
- package/pi-extension/ownership-feedback.ts +100 -0
- package/pi-extension/session-onboarding.ts +154 -0
- package/pi-extension/session-recovery.ts +214 -0
- package/skills/ce-hook-script/SKILL.md +62 -0
- package/skills/ce-hook-script/examples/file-notification.py +79 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/** Native operator dialogs only; no model/MCP grant surface. */
|
|
2
|
+
import { defineMenu, runMenu, sanitizeTerminalText } from "@narumitw/pi-tui-kit";
|
|
3
|
+
import { parseHookRules } from "../hooks/config.js";
|
|
4
|
+
import { replaceHookRule } from "./hooks-management.js";
|
|
5
|
+
const safe = (value) => sanitizeTerminalText(value).replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
|
|
6
|
+
export async function runHooksManagementUi(ctx, options) {
|
|
7
|
+
if (options.signal?.aborted)
|
|
8
|
+
return { status: "cancelled", reason: "superseded" };
|
|
9
|
+
if (ctx.mode !== "tui" || !ctx.hasUI || typeof ctx.ui.custom !== "function")
|
|
10
|
+
return { status: "inspect-only" };
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
let cancellationReason = "superseded";
|
|
13
|
+
// Preserve the first cancellation source, including disposal after an abort.
|
|
14
|
+
const abort = (reason) => {
|
|
15
|
+
if (!controller.signal.aborted) {
|
|
16
|
+
cancellationReason = reason;
|
|
17
|
+
controller.abort();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const supersede = () => abort("superseded");
|
|
21
|
+
const cancelled = () => ({ status: "cancelled", reason: cancellationReason });
|
|
22
|
+
options.signal?.addEventListener("abort", supersede, { once: true });
|
|
23
|
+
const timer = setTimeout(() => abort("timeout"), 120_000);
|
|
24
|
+
timer.unref?.();
|
|
25
|
+
// Follow the existing native dialogs' disposal/cancellation convention.
|
|
26
|
+
const open = ctx.ui.custom.bind(ctx.ui);
|
|
27
|
+
const menuCtx = { ...ctx, ui: { ...ctx.ui, custom: (factory, customOptions) => open((tui, theme, keybindings, done) => {
|
|
28
|
+
let completed = false;
|
|
29
|
+
let disposed = false;
|
|
30
|
+
const complete = (result) => { if (!completed) {
|
|
31
|
+
completed = true;
|
|
32
|
+
done(result);
|
|
33
|
+
} };
|
|
34
|
+
const produced = factory(tui, theme, keybindings, complete);
|
|
35
|
+
if (typeof produced.then === "function")
|
|
36
|
+
throw new Error("Unsupported component");
|
|
37
|
+
const component = produced;
|
|
38
|
+
const handleInput = component.handleInput?.bind(component);
|
|
39
|
+
const dispose = component.dispose?.bind(component);
|
|
40
|
+
component.handleInput = data => {
|
|
41
|
+
const configured = keybindings;
|
|
42
|
+
if (options.keys.matches(data, options.keys.escape) || options.keys.matches(data, options.keys.ctrlC) || configured.matches?.(data, "tui.select.cancel"))
|
|
43
|
+
abort("user");
|
|
44
|
+
handleInput?.(data);
|
|
45
|
+
};
|
|
46
|
+
component.dispose = () => { if (!disposed) {
|
|
47
|
+
disposed = true;
|
|
48
|
+
dispose?.();
|
|
49
|
+
} if (!completed) {
|
|
50
|
+
supersede();
|
|
51
|
+
complete(undefined);
|
|
52
|
+
} };
|
|
53
|
+
return component;
|
|
54
|
+
}, customOptions) } };
|
|
55
|
+
let direction = "pi";
|
|
56
|
+
let rules = [];
|
|
57
|
+
let selectedRule;
|
|
58
|
+
let editing = false;
|
|
59
|
+
let error = "";
|
|
60
|
+
let candidate;
|
|
61
|
+
let confirmed = false;
|
|
62
|
+
let testConfirmed = false;
|
|
63
|
+
const to = (screen) => ({ kind: "to", screen });
|
|
64
|
+
const close = { kind: "close" };
|
|
65
|
+
try {
|
|
66
|
+
let selected;
|
|
67
|
+
try {
|
|
68
|
+
selected = options.management?.inspect();
|
|
69
|
+
}
|
|
70
|
+
catch { /* A committed upgrade may intentionally invalidate the old v1 selection. */ }
|
|
71
|
+
const v2 = selected?.snapshot.ref.kind === "ce-live-session-v2";
|
|
72
|
+
const cancel = { id: "cancel", label: "Cancel", action: "cancel" };
|
|
73
|
+
const screens = {
|
|
74
|
+
main: () => ({ kind: "actions", title: "Hooks · native configuration", hint: "close", lines: [
|
|
75
|
+
"Configured != adopted != eligible != executed.",
|
|
76
|
+
...(selected ? [`Pi → ChatGPT (this session): ${selected.piHooks.length} rules`,
|
|
77
|
+
`ChatGPT → Pi (sender binding): ${selected.mcpHooks.length} rules`, `Policy: ${selected.snapshot.ref.kind}`]
|
|
78
|
+
: options.management ? ["Selected policy unavailable; a committed upgrade may be pending Profile selection.", "Restricted reconciliation only. Supply the exact candidate path and operation from the prior pending report."]
|
|
79
|
+
: ["Live editing unavailable. Inspect the loaded rules for this Pi process."]),
|
|
80
|
+
"Saving requires explicit confirmation. Reload/start remains a separate operator action.",
|
|
81
|
+
...(selected && !v2 ? ["v1 is inspect-only until an explicit offline upgrade. Close existing Pi bindings through their normal lifecycle first."] : []),
|
|
82
|
+
...(error ? [error] : []),
|
|
83
|
+
], items: [
|
|
84
|
+
{ id: "close", label: "Close", close: true },
|
|
85
|
+
...(selected ? [{ id: "pi", label: "Pi → ChatGPT · current native Session", action: "pi" },
|
|
86
|
+
{ id: "mcp", label: "ChatGPT → Pi · MCP sender binding", action: "mcp" },
|
|
87
|
+
{ id: "test", label: "Safe test · fixed benign fixture", action: "openTest" },
|
|
88
|
+
...(!v2 ? [{ id: "upgrade", label: "Explicit offline v1 → v2 upgrade", action: "upgrade" }] : [])] : []),
|
|
89
|
+
...(options.loadedAudit ? [{ id: "loaded", label: "Loaded rules · this Pi process", action: "loaded" }] : []),
|
|
90
|
+
...(options.management ? [{ id: "recovery", label: "Reconcile / continue exact pending publication", action: "recovery" }] : []),
|
|
91
|
+
] }),
|
|
92
|
+
loaded: () => ({ kind: "detail", title: "Loaded Hook rules · this Pi process", hint: "close", lines: [
|
|
93
|
+
`Source: ${safe(options.loadedAudit?.source ?? "unavailable")}`,
|
|
94
|
+
`Configured=${options.loadedAudit?.snapshot.configured ?? 0} eligible=${options.loadedAudit?.snapshot.active ?? 0} executed=unverified`,
|
|
95
|
+
...(options.loadedAudit?.invalid ? ["Configuration invalid; Hooks refused."] : []),
|
|
96
|
+
...(options.loadedAudit?.snapshot.rules.map(rule => safe(`hook ${rule.id} | event=${rule.event} | recipient=${rule.recipient.adapter}/${rule.recipient.endpointId} | command=${rule.command} | args=${JSON.stringify(rule.args)} | timeout=${rule.timeoutMs}ms | ${rule.enabled ? "enabled" : "disabled"}`)) ?? []),
|
|
97
|
+
"Loaded snapshot only. Viewing does not save configuration or execute commands.",
|
|
98
|
+
] }),
|
|
99
|
+
rules: () => ({ kind: "actions", title: direction === "pi" ? "Pi → ChatGPT · current Session" : "ChatGPT → Pi · sender binding", hint: "close", lines: [
|
|
100
|
+
...(direction === "mcp" ? ["Incoming authority is the sender binding; receiving Pi outbound rules do not grant it."] : []),
|
|
101
|
+
...rules.map(rule => safe(JSON.stringify(rule))),
|
|
102
|
+
...(!v2 ? ["Editing refused until explicit offline upgrade."] : []), ...(error ? [error] : []),
|
|
103
|
+
], items: [cancel, ...rules.map(rule => ({ id: `rule:${rule.id}`, label: safe(`${rule.id} · ${rule.enabled ? "enabled" : "disabled"}`), action: "choose" })),
|
|
104
|
+
...(v2 ? [{ id: "add", label: "Add rule", action: "add" }, { id: "review", label: "Review full candidate", action: "review" }] : []),
|
|
105
|
+
] }),
|
|
106
|
+
rule: () => ({ kind: "actions", title: `Hook · ${safe(selectedRule.id)}`, hint: "close", lines: [safe(JSON.stringify(selectedRule))], items: [cancel,
|
|
107
|
+
...(v2 ? [{ id: "edit", label: "Edit rule", action: "edit" }, { id: "toggle", label: selectedRule.enabled ? "Disable rule" : "Enable rule", action: "toggle" }, { id: "remove", label: "Remove rule", action: "remove" }] : []),
|
|
108
|
+
] }),
|
|
109
|
+
input: () => ({ kind: "input", title: editing ? "Edit Hook · JSON rule" : "Add Hook · JSON rule", hint: "close", lines: [
|
|
110
|
+
"Enter one static JSON rule: id, event, recipient, absolute command, args array, timeoutMs, enabled.",
|
|
111
|
+
"No command runs here. Every argument is literal; no shell evaluation.", ...(error ? [error] : []),
|
|
112
|
+
], initialValue: JSON.stringify(editing ? selectedRule : { id: "notify", event: "ce.message.committed.v1", recipient: direction === "pi" ? selected.snapshot.manifest.chatgptEndpoint : { adapter: "pi", endpointId: "pi/REPLACE" }, command: "/absolute/path/to/executable", args: [], timeoutMs: 30000, enabled: false }), action: "input" }),
|
|
113
|
+
recovery: () => ({ kind: "input", title: "Reconcile pending publication · exact evidence", hint: "close", lines: [
|
|
114
|
+
"Enter JSON with exactly bindingPath and operationId from the prior pending report.",
|
|
115
|
+
"This reads evidence only. No scanning, new consent, upgrade replay or transport operation.", ...(error ? [error] : []),
|
|
116
|
+
], initialValue: '{"bindingPath":"/exact/private/messaging-HASH.yaml","operationId":"exact-operation"}', action: "recoveryInput" }),
|
|
117
|
+
review: () => ({ kind: "review", title: candidate?.kind === "continuation" ? "Continue published configuration · exact evidence" : candidate?.kind === "upgrade" ? "Explicit offline upgrade · exact candidate" : "Hooks · full candidate consent", hint: "close", content: [
|
|
118
|
+
"The entire candidate below is the scope of this confirmation.",
|
|
119
|
+
...(candidate?.kind === "continuation" ? ["The operation is already PUBLISHED. Confirmation selects these exact bytes under a verified STOPPED Profile lock; no new chain fact is published."] : []),
|
|
120
|
+
...(candidate?.kind === "upgrade" ? ["Requires verified STOPPED Profile lock. Retains SAME history, tombstones and Pi Hook facts. Initializes empty MCP consent.", "An upgrade marker cannot be rolled back; Profile selection may remain pending after a crash."] : []),
|
|
121
|
+
JSON.stringify(candidate, null, 2).split("\n").map(safe).join("\n"),
|
|
122
|
+
"Configuration only. No reload, transport operation or configured-command execution.",
|
|
123
|
+
].join("\n"), format: { kind: "text" }, confirm: { id: "confirm", label: "Approve and save this exact candidate", action: "confirm" } }),
|
|
124
|
+
safeTest: () => ({ kind: "actions", title: "Safe Hook test", hint: "close", lines: ["Run one fixed benign fixture with fixed argv and empty environment.", "Configured commands and personal scripts are never run. This does not test their behavior."], items: [cancel, { id: "run", label: "Run fixed benign fixture", action: "test" }] }),
|
|
125
|
+
};
|
|
126
|
+
const review = () => {
|
|
127
|
+
try {
|
|
128
|
+
candidate = options.management.preview(direction, rules);
|
|
129
|
+
error = "";
|
|
130
|
+
return to("review");
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
error = "Candidate refused; check rules, native Session and v2 policy selection.";
|
|
134
|
+
return to("rules");
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const menu = defineMenu({ start: "main", screens, actions: {
|
|
138
|
+
pi: () => { direction = "pi"; rules = selected.piHooks; return to("rules"); },
|
|
139
|
+
mcp: () => { direction = "mcp"; rules = selected.mcpHooks; return to("rules"); },
|
|
140
|
+
choose: ({ itemId }) => { selectedRule = rules.find(rule => `rule:${rule.id}` === itemId); return selectedRule ? to("rule") : to("rules"); },
|
|
141
|
+
add: () => { editing = false; return to("input"); }, edit: () => { editing = true; return to("input"); },
|
|
142
|
+
input: ({ value = "" }) => {
|
|
143
|
+
try {
|
|
144
|
+
const rule = parseHookRules([JSON.parse(value)])[0];
|
|
145
|
+
rules = replaceHookRule(rules, editing ? { kind: "edit", id: selectedRule.id, rule } : { kind: "add", rule });
|
|
146
|
+
return review();
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
error = "Invalid rule JSON; check the closed Hook schema.";
|
|
150
|
+
return to("input");
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
toggle: () => { rules = replaceHookRule(rules, { kind: selectedRule.enabled ? "disable" : "enable", id: selectedRule.id }); return review(); },
|
|
154
|
+
remove: () => { rules = replaceHookRule(rules, { kind: "remove", id: selectedRule.id }); return review(); },
|
|
155
|
+
review,
|
|
156
|
+
loaded: () => to("loaded"),
|
|
157
|
+
recovery: () => to("recovery"),
|
|
158
|
+
recoveryInput: ({ value = "" }) => {
|
|
159
|
+
try {
|
|
160
|
+
const input = JSON.parse(value);
|
|
161
|
+
if (!input || typeof input !== "object" || Array.isArray(input) || Reflect.ownKeys(input).length !== 2 || !Object.hasOwn(input, "bindingPath") || !Object.hasOwn(input, "operationId"))
|
|
162
|
+
throw new Error("input");
|
|
163
|
+
const fields = input;
|
|
164
|
+
if (typeof fields.bindingPath !== "string" || typeof fields.operationId !== "string")
|
|
165
|
+
throw new Error("input");
|
|
166
|
+
candidate = options.management.previewContinuation({ bindingPath: fields.bindingPath, operationId: fields.operationId });
|
|
167
|
+
error = "";
|
|
168
|
+
return to("review");
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
candidate = undefined;
|
|
172
|
+
error = "Pending publication refused: check exact evidence, current target scope and native Session.";
|
|
173
|
+
return to("recovery");
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
upgrade: () => { try {
|
|
177
|
+
candidate = options.management.previewUpgrade();
|
|
178
|
+
return to("review");
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
error = "Upgrade preview refused; reconcile the selected policy first.";
|
|
182
|
+
return to("main");
|
|
183
|
+
} },
|
|
184
|
+
confirm: () => { if (!controller.signal.aborted && candidate)
|
|
185
|
+
confirmed = true; return close; },
|
|
186
|
+
cancel: () => { abort("user"); return close; },
|
|
187
|
+
openTest: () => to("safeTest"), test: () => { if (!controller.signal.aborted)
|
|
188
|
+
testConfirmed = true; return close; },
|
|
189
|
+
} });
|
|
190
|
+
let menuFailed = false;
|
|
191
|
+
const result = await runMenu(menuCtx, menu, { getState: () => null, signal: controller.signal, onError: () => { menuFailed = true; supersede(); } });
|
|
192
|
+
if (menuFailed || result.kind === "error")
|
|
193
|
+
return { status: "refused", detail: "Hooks configuration publication refused; check configuration and pending publication evidence." };
|
|
194
|
+
if (controller.signal.aborted)
|
|
195
|
+
return cancelled();
|
|
196
|
+
// The native component has released the terminal. Never run a save or test
|
|
197
|
+
// from a preview renderer, navigation action or a disposed/aborted dialog.
|
|
198
|
+
if (confirmed && candidate) {
|
|
199
|
+
options.onProgress?.("Saving and publishing the Hook candidate");
|
|
200
|
+
return { status: "saved", ...await options.management.save(candidate, true, controller.signal) };
|
|
201
|
+
}
|
|
202
|
+
if (testConfirmed) {
|
|
203
|
+
options.onProgress?.("Running the fixed benign fixture");
|
|
204
|
+
try {
|
|
205
|
+
const test = await options.safeTest();
|
|
206
|
+
if (controller.signal.aborted)
|
|
207
|
+
return cancelled();
|
|
208
|
+
return { status: "tested", executed: test.executed, configuredCommandsExecuted: test.configuredCommandsExecuted };
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return { status: "test-failed", detail: "The fixed benign fixture test did not successfully complete; configured commands and personal scripts were not executed." };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return { status: "closed" };
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return { status: "refused", detail: "Hooks configuration publication refused; check configuration and pending publication evidence." };
|
|
218
|
+
}
|
|
219
|
+
finally {
|
|
220
|
+
clearTimeout(timer);
|
|
221
|
+
options.signal?.removeEventListener("abort", supersede);
|
|
222
|
+
supersede();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=hooks-management-ui.js.map
|