alineo 0.2.0 → 0.4.0
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/dist/index.d.mts +388 -6
- package/dist/index.mjs +723 -24
- package/dist/pi-bridge.js +249 -16
- package/dist/pi-permission-gate.js +337 -0
- package/package.json +4 -3
package/dist/pi-bridge.js
CHANGED
|
@@ -4,9 +4,12 @@ var createInterface = require("readline").createInterface;
|
|
|
4
4
|
var http = require("http");
|
|
5
5
|
var fs = require("fs");
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
7
|
+
// Paths/port are fixed in the sandbox; the env overrides exist only so the bridge can be
|
|
8
|
+
// driven against a stub `pi` in unit tests (test/pi-bridge.test.ts).
|
|
9
|
+
var PORT = Number(process.env.ALINEO_BRIDGE_PORT) || 3001;
|
|
10
|
+
var ENV_FILE = process.env.ALINEO_ENV_FILE || "/etc/alineo-env";
|
|
11
|
+
var PI_CONFIG_FILE = process.env.ALINEO_PI_CONFIG || "/etc/alineo-pi.json";
|
|
12
|
+
var PI_BIN = process.env.ALINEO_PI_BIN || "pi";
|
|
10
13
|
|
|
11
14
|
// Re-read /etc/alineo-env into process.env on each Pi (re)start so setEnv() changes take effect.
|
|
12
15
|
function loadEnv() {
|
|
@@ -19,7 +22,12 @@ function loadEnv() {
|
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
// Build the pi CLI args from /etc/alineo-pi.json (model/provider config, written by the host).
|
|
22
|
-
// Supports: provider, model, resume (--continue to resume the most recent session)
|
|
25
|
+
// Supports: provider, model, resume (--continue to resume the most recent session),
|
|
26
|
+
// permissions (loads the alineo permission-gate extension by explicit path).
|
|
27
|
+
// `POLICY_ACTIVE` gates the permission-request routing below and is set here, before Pi
|
|
28
|
+
// is ever spawned, so there is no window where Pi could emit a gate dialog first.
|
|
29
|
+
var POLICY_ACTIVE = false;
|
|
30
|
+
var PERMISSION_GATE_PATH = process.env.ALINEO_PERMISSION_GATE_PATH || "/alineo-permission-gate.js";
|
|
23
31
|
function buildPiArgs() {
|
|
24
32
|
var args = ["--mode", "rpc", "--approve"];
|
|
25
33
|
try {
|
|
@@ -28,8 +36,12 @@ function buildPiArgs() {
|
|
|
28
36
|
if (cfg.provider) args.push("--provider", cfg.provider);
|
|
29
37
|
if (cfg.model) args.push("--model", cfg.model);
|
|
30
38
|
if (cfg.resume) args.push("--continue");
|
|
39
|
+
if (cfg.permissions && fs.existsSync(PERMISSION_GATE_PATH)) {
|
|
40
|
+
POLICY_ACTIVE = true;
|
|
41
|
+
args.push("-e", PERMISSION_GATE_PATH);
|
|
42
|
+
}
|
|
31
43
|
}
|
|
32
|
-
} catch
|
|
44
|
+
} catch {}
|
|
33
45
|
return args;
|
|
34
46
|
}
|
|
35
47
|
|
|
@@ -53,7 +65,7 @@ function startHeartbeat(res) {
|
|
|
53
65
|
return setInterval(function () {
|
|
54
66
|
try {
|
|
55
67
|
res.write(": ping\n\n");
|
|
56
|
-
} catch
|
|
68
|
+
} catch {}
|
|
57
69
|
}, 3000);
|
|
58
70
|
}
|
|
59
71
|
function stopHeartbeat(iv) {
|
|
@@ -70,8 +82,100 @@ var state = {
|
|
|
70
82
|
queue: [], // pending items: { message, streamingBehavior?, res, text, t0 }
|
|
71
83
|
gen: 0, // incremented on every (re)start; guards against stale async callbacks
|
|
72
84
|
pendingCmds: {}, // id → { res, timer, bash? } — commands waiting for Pi's response ack
|
|
85
|
+
pendingPermissions: {}, // pi extension_ui_request id → { tool, target, title, timer }
|
|
86
|
+
permissionChannel: null, // { res } — long-lived SSE sink for permission_request when no prompt is active
|
|
73
87
|
};
|
|
74
88
|
|
|
89
|
+
// Permission-gate dialogs whose title starts with this marker are held + routed to the host
|
|
90
|
+
// instead of auto-cancelled. Must match adapters/pi-permission-gate.js's MARKER.
|
|
91
|
+
var PERM_MARKER = "ALINEO_PERM ";
|
|
92
|
+
var PERMISSION_TIMEOUT_MS = 300000; // 5 min, matches Cline's desktop auto-deny
|
|
93
|
+
|
|
94
|
+
// Push a permission event to every live SSE sink: the active prompt stream (so a caller
|
|
95
|
+
// iterating prompt() sees it inline) AND the dedicated /permission-stream channel (so an
|
|
96
|
+
// attached operator sees it even between / across prompts). Both may be present.
|
|
97
|
+
function emitPermission(obj) {
|
|
98
|
+
var line = "data: " + JSON.stringify(obj) + "\n\n";
|
|
99
|
+
var sinks = [state.active, state.permissionChannel];
|
|
100
|
+
for (var i = 0; i < sinks.length; i++) {
|
|
101
|
+
if (sinks[i] && sinks[i].res) {
|
|
102
|
+
try {
|
|
103
|
+
sinks[i].res.write(line);
|
|
104
|
+
} catch {}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Answer a held Pi extension_ui_request. `value` is a JSON string the gate parses;
|
|
110
|
+
// omit it (pass null) to send { cancelled: true } instead.
|
|
111
|
+
function answerPermission(id, value) {
|
|
112
|
+
if (value === null) rpc({ type: "extension_ui_response", id: id, cancelled: true });
|
|
113
|
+
else rpc({ type: "extension_ui_response", id: id, value: value });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Auto-resolve every OTHER pending permission for the same tool — opencode's "one decision
|
|
117
|
+
// clears the batch" for an "always" (allow) or "reject" (deny) verdict.
|
|
118
|
+
function resolveMatchingPending(exceptId, tool, verdict) {
|
|
119
|
+
Object.keys(state.pendingPermissions).forEach(function (pid) {
|
|
120
|
+
if (pid === exceptId) return;
|
|
121
|
+
var p = state.pendingPermissions[pid];
|
|
122
|
+
if (p.tool !== tool) return;
|
|
123
|
+
clearTimeout(p.timer);
|
|
124
|
+
delete state.pendingPermissions[pid];
|
|
125
|
+
answerPermission(
|
|
126
|
+
pid,
|
|
127
|
+
verdict === "allow"
|
|
128
|
+
? JSON.stringify({ verdict: "allow", scope: "once" })
|
|
129
|
+
: JSON.stringify({ verdict: "reject" }),
|
|
130
|
+
);
|
|
131
|
+
emitPermission({
|
|
132
|
+
type: "permission_resolved",
|
|
133
|
+
requestId: pid,
|
|
134
|
+
decision: verdict === "allow" ? { kind: "once" } : { kind: "reject" },
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Time a held permission request out: answer the gate as cancelled + tell the host.
|
|
140
|
+
function expirePermission(pid) {
|
|
141
|
+
if (!state.pendingPermissions[pid]) return;
|
|
142
|
+
delete state.pendingPermissions[pid];
|
|
143
|
+
answerPermission(pid, null);
|
|
144
|
+
emitPermission({ type: "permission_resolved", requestId: pid, decision: { kind: "reject" } });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// While a caller is attached to /permission-stream a human is present, so the auto-deny
|
|
148
|
+
// timeout is suspended; it's re-armed (fresh full window) when they disconnect.
|
|
149
|
+
function suspendPermissionTimers() {
|
|
150
|
+
Object.keys(state.pendingPermissions).forEach(function (pid) {
|
|
151
|
+
var p = state.pendingPermissions[pid];
|
|
152
|
+
if (p.timer) {
|
|
153
|
+
clearTimeout(p.timer);
|
|
154
|
+
p.timer = null;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
function resumePermissionTimers() {
|
|
159
|
+
Object.keys(state.pendingPermissions).forEach(function (pid) {
|
|
160
|
+
var p = state.pendingPermissions[pid];
|
|
161
|
+
if (!p.timer) {
|
|
162
|
+
p.timer = setTimeout(function () {
|
|
163
|
+
expirePermission(pid);
|
|
164
|
+
}, PERMISSION_TIMEOUT_MS);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Fail every held permission request (pi restart, abort, exit).
|
|
170
|
+
function cancelPendingPermissions() {
|
|
171
|
+
Object.keys(state.pendingPermissions).forEach(function (pid) {
|
|
172
|
+
var p = state.pendingPermissions[pid];
|
|
173
|
+
clearTimeout(p.timer);
|
|
174
|
+
answerPermission(pid, null);
|
|
175
|
+
});
|
|
176
|
+
state.pendingPermissions = {};
|
|
177
|
+
}
|
|
178
|
+
|
|
75
179
|
// Fail all in-flight pendingCmds cleanly, handling bash (SSE) vs regular (JSON) responses.
|
|
76
180
|
function cleanupPendingCmds(reason) {
|
|
77
181
|
Object.keys(state.pendingCmds).forEach(function (id) {
|
|
@@ -82,7 +186,7 @@ function cleanupPendingCmds(reason) {
|
|
|
82
186
|
try {
|
|
83
187
|
p.res.write("data: " + JSON.stringify({ error: reason }) + "\n\n");
|
|
84
188
|
p.res.end();
|
|
85
|
-
} catch
|
|
189
|
+
} catch {}
|
|
86
190
|
} else {
|
|
87
191
|
respond(p.res, 500, { ok: false, error: reason });
|
|
88
192
|
}
|
|
@@ -99,16 +203,17 @@ function startPi() {
|
|
|
99
203
|
state.active = null;
|
|
100
204
|
}
|
|
101
205
|
cleanupPendingCmds("pi restarted");
|
|
206
|
+
cancelPendingPermissions();
|
|
102
207
|
|
|
103
208
|
if (state.rl) {
|
|
104
209
|
try {
|
|
105
210
|
state.rl.close();
|
|
106
|
-
} catch
|
|
211
|
+
} catch {}
|
|
107
212
|
}
|
|
108
213
|
if (state.proc) {
|
|
109
214
|
try {
|
|
110
215
|
state.proc.kill("SIGTERM");
|
|
111
|
-
} catch
|
|
216
|
+
} catch {}
|
|
112
217
|
}
|
|
113
218
|
state.proc = null;
|
|
114
219
|
state.rl = null;
|
|
@@ -120,7 +225,7 @@ function startPi() {
|
|
|
120
225
|
var gen = ++state.gen;
|
|
121
226
|
log("pi start gen=" + gen + ": " + args.join(" "));
|
|
122
227
|
|
|
123
|
-
var proc = spawn(
|
|
228
|
+
var proc = spawn(PI_BIN, args, {
|
|
124
229
|
stdio: ["pipe", "pipe", "pipe"],
|
|
125
230
|
env: Object.assign({}, process.env),
|
|
126
231
|
});
|
|
@@ -141,6 +246,7 @@ function startPi() {
|
|
|
141
246
|
state.proc = null;
|
|
142
247
|
state.ready = false;
|
|
143
248
|
cleanupPendingCmds("pi exited");
|
|
249
|
+
cancelPendingPermissions();
|
|
144
250
|
if (state.active) {
|
|
145
251
|
stopHeartbeat(state.active.heartbeat);
|
|
146
252
|
state.active.res.write("data: " + JSON.stringify({ error: "pi exited" }) + "\n\n");
|
|
@@ -180,7 +286,7 @@ function handleLine(line) {
|
|
|
180
286
|
var ev;
|
|
181
287
|
try {
|
|
182
288
|
ev = JSON.parse(line);
|
|
183
|
-
} catch
|
|
289
|
+
} catch {
|
|
184
290
|
return;
|
|
185
291
|
}
|
|
186
292
|
|
|
@@ -222,11 +328,11 @@ function handleLine(line) {
|
|
|
222
328
|
if (output)
|
|
223
329
|
try {
|
|
224
330
|
pending.res.write("data: " + JSON.stringify({ type: "text", text: output }) + "\n\n");
|
|
225
|
-
} catch
|
|
331
|
+
} catch {}
|
|
226
332
|
try {
|
|
227
333
|
pending.res.write("data: [DONE]\n\n");
|
|
228
334
|
pending.res.end();
|
|
229
|
-
} catch
|
|
335
|
+
} catch {}
|
|
230
336
|
} else if (ev.success) {
|
|
231
337
|
respond(pending.res, 200, { ok: true, data: ev.data || null });
|
|
232
338
|
} else {
|
|
@@ -285,6 +391,43 @@ function handleLine(line) {
|
|
|
285
391
|
// We auto-cancel them immediately so Pi never stalls — even when there is no active prompt.
|
|
286
392
|
// Fire-and-forget methods need no response; we just forward them.
|
|
287
393
|
if (ev.type === "extension_ui_request") {
|
|
394
|
+
// alineo permission-gate dialog: title is `ALINEO_PERM {json}`. Hold it open, route it
|
|
395
|
+
// to the host as a permission_request, and wait for POST /permission-response.
|
|
396
|
+
if (
|
|
397
|
+
POLICY_ACTIVE &&
|
|
398
|
+
ev.method === "select" &&
|
|
399
|
+
ev.id &&
|
|
400
|
+
typeof ev.title === "string" &&
|
|
401
|
+
ev.title.indexOf(PERM_MARKER) === 0
|
|
402
|
+
) {
|
|
403
|
+
var meta = {};
|
|
404
|
+
try {
|
|
405
|
+
meta = JSON.parse(ev.title.slice(PERM_MARKER.length));
|
|
406
|
+
} catch {}
|
|
407
|
+
var entry = {
|
|
408
|
+
tool: meta.tool || "unknown",
|
|
409
|
+
target: meta.target || "",
|
|
410
|
+
title: meta.title || "Approve tool call",
|
|
411
|
+
since: Date.now(),
|
|
412
|
+
timer: null,
|
|
413
|
+
};
|
|
414
|
+
// No auto-deny clock while a human is watching the permission stream.
|
|
415
|
+
if (!state.permissionChannel) {
|
|
416
|
+
entry.timer = setTimeout(function () {
|
|
417
|
+
expirePermission(ev.id);
|
|
418
|
+
}, PERMISSION_TIMEOUT_MS);
|
|
419
|
+
}
|
|
420
|
+
state.pendingPermissions[ev.id] = entry;
|
|
421
|
+
emitPermission({
|
|
422
|
+
type: "permission_request",
|
|
423
|
+
requestId: ev.id,
|
|
424
|
+
tool: entry.tool,
|
|
425
|
+
target: entry.target,
|
|
426
|
+
title: entry.title,
|
|
427
|
+
});
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
|
|
288
431
|
var DIALOG_METHODS = ["select", "confirm", "input", "editor"];
|
|
289
432
|
var isDialog = DIALOG_METHODS.indexOf(ev.method) !== -1;
|
|
290
433
|
var uiParams = {};
|
|
@@ -515,7 +658,7 @@ function respond(res, status, body) {
|
|
|
515
658
|
try {
|
|
516
659
|
res.writeHead(status, { "Content-Type": "application/json" });
|
|
517
660
|
res.end(JSON.stringify(body));
|
|
518
|
-
} catch
|
|
661
|
+
} catch {}
|
|
519
662
|
}
|
|
520
663
|
|
|
521
664
|
// --- HTTP server ---
|
|
@@ -534,6 +677,63 @@ http
|
|
|
534
677
|
res.end(logBuf.join("\n"));
|
|
535
678
|
return;
|
|
536
679
|
}
|
|
680
|
+
if (req.url === "/permission-stream") {
|
|
681
|
+
// Long-lived SSE sink so permission_request events surface even when no prompt()
|
|
682
|
+
// stream is open. One at a time — a new subscriber replaces the old.
|
|
683
|
+
if (state.permissionChannel) {
|
|
684
|
+
try {
|
|
685
|
+
state.permissionChannel.res.end();
|
|
686
|
+
} catch {}
|
|
687
|
+
}
|
|
688
|
+
res.writeHead(200, {
|
|
689
|
+
"Content-Type": "text/event-stream",
|
|
690
|
+
"Cache-Control": "no-cache",
|
|
691
|
+
Connection: "keep-alive",
|
|
692
|
+
});
|
|
693
|
+
var pHeartbeat = startHeartbeat(res);
|
|
694
|
+
state.permissionChannel = { res: res };
|
|
695
|
+
// A reconnecting operator needs to see what's still outstanding, and the auto-deny
|
|
696
|
+
// clock pauses while they're attached.
|
|
697
|
+
suspendPermissionTimers();
|
|
698
|
+
Object.keys(state.pendingPermissions).forEach(function (pid) {
|
|
699
|
+
var p = state.pendingPermissions[pid];
|
|
700
|
+
try {
|
|
701
|
+
res.write(
|
|
702
|
+
"data: " +
|
|
703
|
+
JSON.stringify({
|
|
704
|
+
type: "permission_request",
|
|
705
|
+
requestId: pid,
|
|
706
|
+
tool: p.tool,
|
|
707
|
+
target: p.target,
|
|
708
|
+
title: p.title,
|
|
709
|
+
}) +
|
|
710
|
+
"\n\n",
|
|
711
|
+
);
|
|
712
|
+
} catch {}
|
|
713
|
+
});
|
|
714
|
+
req.on("close", function () {
|
|
715
|
+
stopHeartbeat(pHeartbeat);
|
|
716
|
+
if (state.permissionChannel && state.permissionChannel.res === res) {
|
|
717
|
+
state.permissionChannel = null;
|
|
718
|
+
resumePermissionTimers();
|
|
719
|
+
}
|
|
720
|
+
});
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if (req.url === "/pending-permissions") {
|
|
724
|
+
var pending = Object.keys(state.pendingPermissions).map(function (pid) {
|
|
725
|
+
var p = state.pendingPermissions[pid];
|
|
726
|
+
return {
|
|
727
|
+
requestId: pid,
|
|
728
|
+
tool: p.tool,
|
|
729
|
+
target: p.target,
|
|
730
|
+
title: p.title,
|
|
731
|
+
since: p.since || 0,
|
|
732
|
+
};
|
|
733
|
+
});
|
|
734
|
+
respond(res, 200, { ok: true, data: { pending: pending } });
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
537
737
|
if (req.url === "/messages") {
|
|
538
738
|
rpcWithAck({ id: "gm" + Date.now(), type: "get_messages" }, res);
|
|
539
739
|
return;
|
|
@@ -565,7 +765,7 @@ http
|
|
|
565
765
|
var data = {};
|
|
566
766
|
try {
|
|
567
767
|
if (body) data = JSON.parse(body);
|
|
568
|
-
} catch
|
|
768
|
+
} catch {
|
|
569
769
|
res.writeHead(400);
|
|
570
770
|
res.end("bad json");
|
|
571
771
|
return;
|
|
@@ -597,7 +797,7 @@ http
|
|
|
597
797
|
try {
|
|
598
798
|
res.write("data: " + JSON.stringify({ error: "bash timeout" }) + "\n\n");
|
|
599
799
|
res.end();
|
|
600
|
-
} catch
|
|
800
|
+
} catch {}
|
|
601
801
|
}
|
|
602
802
|
}, 30000);
|
|
603
803
|
res.writeHead(200, {
|
|
@@ -617,6 +817,9 @@ http
|
|
|
617
817
|
|
|
618
818
|
case "/abort":
|
|
619
819
|
// End the active SSE stream immediately, drain the queue, then wait for Pi's ack.
|
|
820
|
+
// A held permission request would leave Pi's tool_call hook awaiting forever —
|
|
821
|
+
// reject each so the abort can actually unwind.
|
|
822
|
+
cancelPendingPermissions();
|
|
620
823
|
state.queue.length = 0;
|
|
621
824
|
if (state.active) {
|
|
622
825
|
stopHeartbeat(state.active.heartbeat);
|
|
@@ -631,6 +834,36 @@ http
|
|
|
631
834
|
rpcWithAck({ id: "s" + Date.now(), type: "steer", message: data.message || "" }, res);
|
|
632
835
|
return;
|
|
633
836
|
|
|
837
|
+
case "/permission-response": {
|
|
838
|
+
var pp = state.pendingPermissions[data.requestId];
|
|
839
|
+
if (!pp) {
|
|
840
|
+
respond(res, 404, { ok: false, error: "no such pending permission" });
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
clearTimeout(pp.timer);
|
|
844
|
+
delete state.pendingPermissions[data.requestId];
|
|
845
|
+
var decision = data.decision || {};
|
|
846
|
+
if (decision.kind === "once") {
|
|
847
|
+
answerPermission(data.requestId, JSON.stringify({ verdict: "allow", scope: "once" }));
|
|
848
|
+
} else if (decision.kind === "always") {
|
|
849
|
+
answerPermission(data.requestId, JSON.stringify({ verdict: "allow", scope: "always" }));
|
|
850
|
+
resolveMatchingPending(data.requestId, pp.tool, "allow");
|
|
851
|
+
} else {
|
|
852
|
+
answerPermission(
|
|
853
|
+
data.requestId,
|
|
854
|
+
JSON.stringify({ verdict: "reject", feedback: decision.feedback || "" }),
|
|
855
|
+
);
|
|
856
|
+
resolveMatchingPending(data.requestId, pp.tool, "reject");
|
|
857
|
+
}
|
|
858
|
+
emitPermission({
|
|
859
|
+
type: "permission_resolved",
|
|
860
|
+
requestId: data.requestId,
|
|
861
|
+
decision: decision,
|
|
862
|
+
});
|
|
863
|
+
respond(res, 200, { ok: true });
|
|
864
|
+
return;
|
|
865
|
+
}
|
|
866
|
+
|
|
634
867
|
case "/follow-up":
|
|
635
868
|
rpcWithAck(
|
|
636
869
|
{ id: "fu" + Date.now(), type: "follow_up", message: data.message || "" },
|