solrouter 0.4.1 → 0.4.3
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/package.json +1 -1
- package/src/auth.js +2 -2
- package/src/index.js +2 -2
- package/src/route.js +26 -2
- package/src/server.js +27 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solrouter",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Local privacy router for coding tools: pseudonymises every request before it leaves your machine, and sends work a frontier model would refuse to an uncensored open-weight model you control.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/src/auth.js
CHANGED
|
@@ -27,7 +27,7 @@ function openBrowser(url) {
|
|
|
27
27
|
|
|
28
28
|
/** Loopback browser login. Resolves once the callback delivers a token.
|
|
29
29
|
* `open` is injectable for testing (defaults to launching the real browser). */
|
|
30
|
-
export function browserLogin({ timeoutMs = 180000, open = openBrowser } = {}) {
|
|
30
|
+
export function browserLogin({ timeoutMs = 180000, open = openBrowser, save = saveAuth } = {}) {
|
|
31
31
|
return new Promise((resolve, reject) => {
|
|
32
32
|
const state = randomBytes(16).toString("hex");
|
|
33
33
|
const server = createServer((req, res) => {
|
|
@@ -45,7 +45,7 @@ export function browserLogin({ timeoutMs = 180000, open = openBrowser } = {}) {
|
|
|
45
45
|
res.end("<h2>SolRouter proxy connected.</h2><p>You can close this tab and return to the terminal.</p>");
|
|
46
46
|
server.close();
|
|
47
47
|
if (!token.access_token) return reject(new Error("no token in callback"));
|
|
48
|
-
|
|
48
|
+
save(token);
|
|
49
49
|
resolve(token);
|
|
50
50
|
});
|
|
51
51
|
server.listen(0, "127.0.0.1", () => {
|
package/src/index.js
CHANGED
|
@@ -120,11 +120,11 @@ export async function handleStream(body, ctx = {}, cfg) {
|
|
|
120
120
|
kind, mode, maskedPrompt: extractText(masked, kind) };
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
/** Call the private leg for a (masked) prompt. Returns { reply, ok }. */
|
|
123
|
+
/** Call the private leg for a (masked) prompt. Returns { reply, ok, status }. */
|
|
124
124
|
export async function privateReply(cfg, maskedPrompt) {
|
|
125
125
|
const { callSolrouterAgent } = await import("./providers.js");
|
|
126
126
|
const r = await callSolrouterAgent(cfg.private, maskedPrompt);
|
|
127
|
-
return { reply: r.reply || "", ok: r.ok };
|
|
127
|
+
return { reply: r.reply || "", ok: r.ok, status: r.status };
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
export { createMasker } from "./mask.js";
|
package/src/route.js
CHANGED
|
@@ -42,11 +42,35 @@ export function likelyRefused(text) {
|
|
|
42
42
|
return false;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
// Softer decline signals — phrases refusals use even when they don't start with
|
|
46
|
+
// "I can't". Used only for SHORT, complete responses (a long substantive answer
|
|
47
|
+
// that happens to contain one of these is not a refusal).
|
|
48
|
+
const DECLINE_HINTS = [
|
|
49
|
+
/\bI (?:can(?:'|no)?t|won'?t|cannot|will not)\b/i,
|
|
50
|
+
/\bI'?m not (?:going to|able to|willing to|comfortable|going)\b/i,
|
|
51
|
+
/\bI (?:won'?t|don'?t|do not) (?:feel comfortable|think I should|be able)\b/i,
|
|
52
|
+
/\bI'?d (?:rather not|prefer not)\b/i,
|
|
53
|
+
/\b(?:not something I (?:can|will)|that'?s not something)\b/i,
|
|
54
|
+
/\bI'?m (?:sorry|afraid)\b/i,
|
|
55
|
+
/\b(?:against|violates?)\b.{0,30}\b(?:policy|policies|guidelines)\b/i,
|
|
56
|
+
/\bhappy to (?:help|pick|assist)\b.{0,40}\binstead\b/i,
|
|
57
|
+
/\b(?:instead|rather)\b.{0,30}\b(?:I'?m here|happy to|let me)\b/i,
|
|
58
|
+
/\bI (?:must|have to|need to) (?:decline|refuse|pass)\b/i,
|
|
59
|
+
/\bnot appropriate\b/i,
|
|
60
|
+
];
|
|
61
|
+
|
|
45
62
|
/** Did a model response come back as a refusal? Used for the reactive retry. */
|
|
46
63
|
export function isRefusal(text) {
|
|
47
64
|
const s = String(text ?? "").trim();
|
|
48
|
-
if (!s
|
|
49
|
-
|
|
65
|
+
if (!s) return false;
|
|
66
|
+
// Strong signatures fire at any length up to ~1500 (refusals are short).
|
|
67
|
+
if (s.length <= 1500) for (const re of REFUSAL_SIGNATURES) if (re.test(s)) return true;
|
|
68
|
+
// Short + soft decline hints → also a refusal. Short because a real coding answer
|
|
69
|
+
// is long / full of code; a brief prose reply that declines is a refusal.
|
|
70
|
+
if (s.length <= 600) {
|
|
71
|
+
const hasCode = /```|\bfunction\b|\bconst \b|\bimport \b|=>|\bdef \b|;\n/.test(s);
|
|
72
|
+
if (!hasCode) for (const re of DECLINE_HINTS) if (re.test(s)) return true;
|
|
73
|
+
}
|
|
50
74
|
return false;
|
|
51
75
|
}
|
|
52
76
|
|
package/src/server.js
CHANGED
|
@@ -7,11 +7,21 @@
|
|
|
7
7
|
// localhost only — nothing here should be reachable off the machine.
|
|
8
8
|
|
|
9
9
|
import { createServer } from "node:http";
|
|
10
|
+
import { appendFileSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
10
12
|
import { handle, handleStream, createMasker, privateReply } from "./index.js";
|
|
11
13
|
import { forward } from "./providers.js";
|
|
12
14
|
import { dialect, rewriteText } from "./wire.js";
|
|
13
15
|
import { isRefusal } from "./route.js";
|
|
14
16
|
import { writeReceipt } from "./receipt.js";
|
|
17
|
+
import { HOME_DIR } from "./config.js";
|
|
18
|
+
|
|
19
|
+
// Ground-truth log of every streaming routing decision, so a real Claude Code run
|
|
20
|
+
// can be diagnosed from actual traffic instead of guesses. Never contains raw PII
|
|
21
|
+
// beyond the frontier response prefix (already sent to the provider).
|
|
22
|
+
function logDecision(o) {
|
|
23
|
+
try { appendFileSync(join(HOME_DIR, "debug.log"), JSON.stringify({ ts: new Date().toISOString(), ...o }) + "\n"); } catch {}
|
|
24
|
+
}
|
|
15
25
|
|
|
16
26
|
/** Pull assistant text out of an Anthropic SSE chunk (text_delta events). */
|
|
17
27
|
function extractSseText(chunk) {
|
|
@@ -117,9 +127,14 @@ export function startServer(cfg) {
|
|
|
117
127
|
}
|
|
118
128
|
|
|
119
129
|
let leg = s.leg, refused = false, model = s.model;
|
|
130
|
+
logDecision({ event: "stream-decision", reqModel: body.model, mode: s.mode,
|
|
131
|
+
committed, refusalLikely, isRefusalFinal: isRefusal(acc), respLen: acc.length,
|
|
132
|
+
respPrefix: acc.slice(0, 400) });
|
|
120
133
|
if (!committed && s.mode === "auto" && refusalLikely) {
|
|
121
134
|
// Streamed refusal → reroute to the uncensored private leg.
|
|
122
135
|
const pr = await privateReply(cfg, s.maskedPrompt);
|
|
136
|
+
logDecision({ event: "reroute", privateOk: pr.ok, privateStatus: pr.status,
|
|
137
|
+
privateModel: cfg.private.model, replyLen: (pr.reply || "").length });
|
|
123
138
|
if (pr.ok && pr.reply) {
|
|
124
139
|
leg = "private"; refused = true; model = cfg.private.model;
|
|
125
140
|
const restored = restorer ? s.masker.unmask(pr.reply) : pr.reply;
|
|
@@ -129,7 +144,18 @@ export function startServer(cfg) {
|
|
|
129
144
|
leg, reason: "reactive-refusal", model, entities: s.masker.entities(), refused: true, sentToProvider: s.sent });
|
|
130
145
|
return;
|
|
131
146
|
}
|
|
132
|
-
//
|
|
147
|
+
// Reroute was needed but the uncensored leg failed — say so LOUDLY instead
|
|
148
|
+
// of silently relaying Claude's refusal (this is almost always "not logged
|
|
149
|
+
// in" / an invalid SolRouter key).
|
|
150
|
+
res.write(synthAnthropicSse(
|
|
151
|
+
`⚠️ [solrouter] Claude refused this, and the uncensored leg couldn't be reached (${pr.status || "error"}). ` +
|
|
152
|
+
`Run \`solrouter login --token sk_solrouter_…\` with a valid key from solrouter.com/sdk, then retry.`,
|
|
153
|
+
cfg.private.model));
|
|
154
|
+
res.end();
|
|
155
|
+
if (cfg.receipt?.enabled) writeReceipt(cfg.receipt.path, {
|
|
156
|
+
leg: "private", reason: "reroute-failed", model: cfg.private.model,
|
|
157
|
+
entities: s.masker.entities(), refused: true, sentToProvider: s.sent });
|
|
158
|
+
return;
|
|
133
159
|
}
|
|
134
160
|
if (!committed) emit(held); // short non-refusal: flush the buffer
|
|
135
161
|
if (restorer) res.write(restorer.flush());
|