tryharness 0.7.83 → 0.7.86
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 +21 -19
- package/assets/dsh-plugin.mjs +75 -4
- package/assets/herdr-mod/harness/main.js +367 -0
- package/assets/herdr-mod/harness/mod.json +6 -0
- package/assets/opencode-plugin.js +87 -22
- package/assets/pi-extension.ts +81 -2
- package/cli.mjs +544 -208
- package/lib/adapters.mjs +62 -27
- package/lib/agentBinaries.mjs +6 -8
- package/lib/agentClaims.mjs +168 -0
- package/lib/agentReadTools.json +415 -0
- package/lib/agentSignIn.mjs +17 -18
- package/lib/agentWiring.mjs +114 -0
- package/lib/archetypes.js +10 -11
- package/lib/attention.mjs +100 -0
- package/lib/audit.mjs +1 -1
- package/lib/backupSetup.mjs +6 -28
- package/lib/bankrApprovals.mjs +86 -0
- package/lib/boardAccess.mjs +40 -66
- package/lib/boardBackup.mjs +86 -85
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +29 -36
- package/lib/boardFiles.mjs +83 -15
- package/lib/boardMirror.mjs +78 -11
- package/lib/boardProtocol.json +191 -123
- package/lib/boardThreads.mjs +19 -37
- package/lib/boardTools.mjs +22 -15
- package/lib/boardWatches.mjs +43 -15
- package/lib/bridge.mjs +7 -3
- package/lib/browserAgentTools.mjs +59 -0
- package/lib/browserWake.mjs +0 -6
- package/lib/cards.mjs +52 -0
- package/lib/circling.mjs +189 -0
- package/lib/cleanup.mjs +234 -0
- package/lib/cloudBoot.mjs +21 -4
- package/lib/cloudSave.mjs +46 -5
- package/lib/compaction.mjs +8 -1
- package/lib/completion.mjs +358 -0
- package/lib/computerDevices.mjs +853 -0
- package/lib/connectionStatus.mjs +34 -0
- package/lib/connectors.mjs +139 -1
- package/lib/coordinator.mjs +108 -14
- package/lib/coreModels.mjs +36 -12
- package/lib/daemon.mjs +2426 -338
- package/lib/daemonLock.mjs +103 -0
- package/lib/deviceDoor.mjs +308 -0
- package/lib/deviceIdentity.mjs +114 -0
- package/lib/deviceList.mjs +172 -0
- package/lib/deviceSeal.mjs +74 -0
- package/lib/dispatcher.mjs +497 -26
- package/lib/dispatches.mjs +3 -3
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +67 -41
- package/lib/events.mjs +318 -0
- package/lib/facts.mjs +170 -0
- package/lib/fileSource.mjs +540 -0
- package/lib/files.mjs +47 -2
- package/lib/gitHost.mjs +39 -1
- package/lib/goalCommands.mjs +12 -80
- package/lib/handover.mjs +21 -16
- package/lib/harnessOffers.mjs +140 -15
- package/lib/herdr.mjs +100 -15
- package/lib/herdrAppCall.mjs +19 -0
- package/lib/herdrCore.mjs +59 -0
- package/lib/herdrRuns.mjs +1126 -0
- package/lib/hookDialects.mjs +235 -50
- package/lib/identityLink.mjs +81 -0
- package/lib/inference.mjs +31 -2
- package/lib/jamAccess.mjs +82 -0
- package/lib/{projectBoard.mjs → jamBoard.mjs} +5 -4
- package/lib/{projectGit.mjs → jamGit.mjs} +53 -15
- package/lib/jamKey.mjs +274 -0
- package/lib/{projectPieces.mjs → jamPieces.mjs} +35 -29
- package/lib/{projectSettings.mjs → jamSettings.mjs} +12 -22
- package/lib/jamStore.mjs +973 -0
- package/lib/jamUpgrade.mjs +112 -0
- package/lib/jams.mjs +3079 -0
- package/lib/kanban.mjs +13 -7
- package/lib/landed.mjs +5 -1
- package/lib/library.mjs +41 -22
- package/lib/limits.mjs +91 -0
- package/lib/lineDiff.mjs +97 -0
- package/lib/lineup.mjs +18 -13
- package/lib/link.mjs +584 -0
- package/lib/localStack.mjs +75 -16
- package/lib/localmodels.mjs +58 -78
- package/lib/loopBackstop.mjs +2 -1
- package/lib/machine.mjs +544 -0
- package/lib/machineLink.mjs +446 -0
- package/lib/machineRoutes.mjs +68 -19
- package/lib/machineRuns.mjs +85 -4
- package/lib/machineSeal.mjs +6 -31
- package/lib/manager.mjs +134 -12
- package/lib/mcp-stdio.mjs +492 -215
- package/lib/mcpRelay.mjs +34 -35
- package/lib/members.json +1 -1
- package/lib/modelRoute.mjs +162 -0
- package/lib/nativeHost.mjs +81 -90
- package/lib/nativeSenses.mjs +258 -0
- package/lib/nerves.mjs +523 -0
- package/lib/network.mjs +117 -278
- package/lib/networkPlaces.mjs +578 -0
- package/lib/nextStep.mjs +95 -0
- package/lib/notify.mjs +2 -2
- package/lib/nudge.mjs +116 -10
- package/lib/originals.mjs +216 -0
- package/lib/outsideEvents.mjs +383 -0
- package/lib/outsideRuns.mjs +230 -0
- package/lib/ownMachines.mjs +674 -256
- package/lib/pain.mjs +215 -0
- package/lib/pairing.mjs +91 -11
- package/lib/paths.mjs +42 -3
- package/lib/peerProcess.mjs +21 -7
- package/lib/peopleLink.mjs +268 -0
- package/lib/personNotices.mjs +3 -2
- package/lib/phones.mjs +252 -542
- package/lib/placeCards.mjs +282 -0
- package/lib/placeChecks.mjs +95 -0
- package/lib/placeCommands.mjs +8 -17
- package/lib/placeRequests.mjs +152 -0
- package/lib/placeStore.mjs +295 -0
- package/lib/placeTool.d.ts +12 -0
- package/lib/placeTool.js +40 -0
- package/lib/places.mjs +63 -14
- package/lib/plans.mjs +53 -8
- package/lib/presence.mjs +270 -0
- package/lib/privateNetwork.d.ts +2 -0
- package/lib/privateNetwork.js +93 -0
- package/lib/proposals.mjs +300 -0
- package/lib/publicFetch.d.ts +32 -0
- package/lib/publicFetch.js +248 -0
- package/lib/reach.mjs +128 -0
- package/lib/rebuildable.mjs +72 -0
- package/lib/recall.mjs +216 -0
- package/lib/record.mjs +13 -0
- package/lib/reflexes.mjs +392 -0
- package/lib/relayLimits.mjs +31 -2
- package/lib/releaseNotice.mjs +198 -0
- package/lib/removeHarness.mjs +15 -4
- package/lib/repoLinks.mjs +204 -35
- package/lib/runAuthority.mjs +102 -39
- package/lib/runBrief.mjs +93 -40
- package/lib/runRequests.mjs +52 -14
- package/lib/runUsage.mjs +31 -0
- package/lib/runWorker.mjs +74 -25
- package/lib/runner.mjs +289 -45
- package/lib/runtime-install.mjs +37 -8
- package/lib/schedules.mjs +171 -47
- package/lib/screenWatches.mjs +221 -0
- package/lib/sealed.mjs +12 -30
- package/lib/sealedThreads.mjs +198 -392
- package/lib/secretStore.mjs +178 -0
- package/lib/secrets.mjs +11 -0
- package/lib/seen.mjs +3 -9
- package/lib/selfUpdate.mjs +51 -15
- package/lib/senseCall.mjs +1514 -0
- package/lib/senseHook.mjs +114 -0
- package/lib/senseTools.mjs +1298 -0
- package/lib/sessionBriefs.mjs +87 -0
- package/lib/sessionControl.mjs +63 -13
- package/lib/sessionFiles.mjs +85 -0
- package/lib/setupChoices.mjs +12 -3
- package/lib/setupMode.mjs +70 -25
- package/lib/sharedKeys.mjs +166 -0
- package/lib/sleep.mjs +396 -0
- package/lib/sources.mjs +306 -0
- package/lib/speech.mjs +166 -411
- package/lib/spoken.mjs +210 -0
- package/lib/steerHook.mjs +3 -1
- package/lib/steerQueue.mjs +40 -11
- package/lib/store.mjs +323 -130
- package/lib/stuck.mjs +85 -7
- package/lib/swarm.mjs +157 -49
- package/lib/swarmDevices.mjs +25 -85
- package/lib/swarmMemory.mjs +93 -9
- package/lib/t3.mjs +73 -3
- package/lib/taskCommands.mjs +51 -60
- package/lib/taskRooms.mjs +30 -4
- package/lib/taskRules.json +29 -0
- package/lib/taskRules.mjs +21 -0
- package/lib/taskRuns.mjs +74 -11
- package/lib/team.mjs +2 -1
- package/lib/threadActions.mjs +37 -9
- package/lib/threadAuthority.mjs +4 -4
- package/lib/threadBriefs.mjs +173 -43
- package/lib/threadNotes.mjs +320 -0
- package/lib/threadRecord.mjs +491 -0
- package/lib/threadRuns.mjs +216 -108
- package/lib/threadView.mjs +27 -30
- package/lib/threadWatches.mjs +1962 -0
- package/lib/timeSense.mjs +839 -0
- package/lib/tripwires.mjs +454 -0
- package/lib/ui.mjs +1 -1
- package/lib/vault.mjs +560 -45
- package/lib/vaultCommands.mjs +5 -3
- package/lib/vaultPeers.mjs +165 -0
- package/lib/vaultTools.d.ts +4 -1
- package/lib/vaultTools.js +22 -1
- package/lib/verdicts.mjs +12 -26
- package/lib/versions.mjs +117 -0
- package/lib/waitTool.mjs +278 -0
- package/lib/wakePicture.mjs +268 -0
- package/lib/wakeQueue.mjs +108 -0
- package/lib/watchResponses.mjs +533 -0
- package/lib/watches.mjs +290 -145
- package/lib/windowAsks.mjs +80 -17
- package/lib/worktrees.mjs +159 -9
- package/npm-shrinkwrap.json +138 -631
- package/package.json +7 -6
- package/release.json +211 -129
- package/lib/callSessions.mjs +0 -51
- package/lib/directLink.mjs +0 -349
- package/lib/peopleBoard.mjs +0 -131
- package/lib/projects.mjs +0 -2917
- package/lib/roomChannels.mjs +0 -667
package/lib/mcpRelay.mjs
CHANGED
|
@@ -4,9 +4,11 @@ import { Readable } from "node:stream";
|
|
|
4
4
|
import { pipeline } from "node:stream/promises";
|
|
5
5
|
import { paths } from "./paths.mjs";
|
|
6
6
|
|
|
7
|
-
// How long an agent's
|
|
8
|
-
//
|
|
7
|
+
// How long an agent's MCP server waits on this daemon for a read of the person's browser or a download: the deadline
|
|
8
|
+
// every agent call had when it went to Harness's /api/mcp (an earlier mcp-stdio.mjs), kept as it was, not measured
|
|
9
|
+
// again.
|
|
9
10
|
export const MCP_CALL_DEADLINE_MS = 90_000;
|
|
11
|
+
|
|
10
12
|
export const RELAY_NOT_RUNNING =
|
|
11
13
|
"Harness relay is not running on this computer: run npx tryharness";
|
|
12
14
|
// The only downloads this computer's key goes to: Harness's generated media and chat files.
|
|
@@ -25,42 +27,16 @@ export function generatedPath(name) {
|
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
+
* What an agent's MCP server asks of this daemon beyond the board: its reads of the person's browsers, which this
|
|
31
|
+
* computer asks the browser for itself (`deviceDoor`, deviceDoor.mjs), and Harness's generated media saved to disk.
|
|
32
|
+
* Nothing an agent here calls goes to Harness's /api/mcp.
|
|
30
33
|
*/
|
|
31
|
-
export function createMcpRelay({ config,
|
|
34
|
+
export function createMcpRelay({ config, log, deviceDoor = null }) {
|
|
32
35
|
const origin = String(config.origin).replace(/\/$/, "");
|
|
33
36
|
const authorization = "Bearer " + config.token;
|
|
34
37
|
|
|
35
|
-
// Straight to /api/mcp, from the daemon: while Harness has not said it carries calls on the bridge (an older
|
|
36
|
-
// Harness, or the moment between dialing and its server_features), and for a call whose browser session is held by
|
|
37
|
-
// another of Harness's servers, which only an HTTP request can be replayed to.
|
|
38
|
-
const postToHarness = async (rpc) => {
|
|
39
|
-
const response = await fetch(origin + "/api/mcp", {
|
|
40
|
-
method: "POST",
|
|
41
|
-
// This computer tells its agents who else is in the swarm itself (mcp-stdio.mjs), so Harness does not too.
|
|
42
|
-
headers: {
|
|
43
|
-
"content-type": "application/json",
|
|
44
|
-
authorization,
|
|
45
|
-
"x-harness-swarm": "local",
|
|
46
|
-
},
|
|
47
|
-
body: JSON.stringify(rpc),
|
|
48
|
-
signal: AbortSignal.timeout(MCP_CALL_DEADLINE_MS),
|
|
49
|
-
});
|
|
50
|
-
return { ok: true, status: response.status, body: await response.text() };
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/** One JSON-RPC request to Harness's MCP server; the answer is its status and body as /api/mcp sends them. */
|
|
54
|
-
async function call(rpc) {
|
|
55
|
-
if (!bridge.has("mcp_call")) return postToHarness(rpc);
|
|
56
|
-
const answer = await bridge.ask("mcp_call", { rpc }, MCP_CALL_DEADLINE_MS);
|
|
57
|
-
if (!answer.ok) return answer;
|
|
58
|
-
if (answer.replay) return postToHarness(rpc);
|
|
59
|
-
return { ok: true, status: answer.status, body: answer.body };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
38
|
/** A generated file list_generated_media named, streamed to disk; the answer is what the agent is told. */
|
|
63
|
-
async function download(link) {
|
|
39
|
+
async function download(link, agent) {
|
|
64
40
|
let url;
|
|
65
41
|
try {
|
|
66
42
|
url = new URL(String(link || ""));
|
|
@@ -76,6 +52,9 @@ export function createMcpRelay({ config, bridge, log }) {
|
|
|
76
52
|
ok: false,
|
|
77
53
|
error: "download must be a URL list_generated_media gave",
|
|
78
54
|
};
|
|
55
|
+
// A document made in one of the person's browsers is asked of that browser by this computer, never Harness.
|
|
56
|
+
if (url.pathname === "/api/mcp/file" && deviceDoor)
|
|
57
|
+
return saveFromBrowser(url, agent);
|
|
79
58
|
try {
|
|
80
59
|
const response = await fetch(url, {
|
|
81
60
|
headers: { authorization },
|
|
@@ -104,9 +83,29 @@ export function createMcpRelay({ config, bridge, log }) {
|
|
|
104
83
|
}
|
|
105
84
|
}
|
|
106
85
|
|
|
86
|
+
async function saveFromBrowser(url, agent) {
|
|
87
|
+
const file = await deviceDoor.fileFor({
|
|
88
|
+
id: url.searchParams.get("id") ?? "",
|
|
89
|
+
device: url.searchParams.get("device"),
|
|
90
|
+
agent,
|
|
91
|
+
});
|
|
92
|
+
if (file.error) return { ok: false, error: file.error };
|
|
93
|
+
const saved = generatedPath(file.filename);
|
|
94
|
+
fs.writeFileSync(saved, Buffer.from(file.data, "base64"));
|
|
95
|
+
return {
|
|
96
|
+
ok: true,
|
|
97
|
+
path: saved,
|
|
98
|
+
mime: file.mediaType,
|
|
99
|
+
bytes: fs.statSync(saved).size,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
107
103
|
const COMMANDS = {
|
|
108
|
-
"mcp.call": (request) =>
|
|
109
|
-
|
|
104
|
+
"mcp.call": async (request) =>
|
|
105
|
+
deviceDoor
|
|
106
|
+
? deviceDoor.forAgent(request.rpc, request.agent)
|
|
107
|
+
: { ok: false, error: "this computer reads no browser" },
|
|
108
|
+
"mcp.download": (request) => download(request.url, request.agent),
|
|
110
109
|
};
|
|
111
110
|
return {
|
|
112
111
|
handles: (command) => Object.hasOwn(COMMANDS, command),
|
package/lib/members.json
CHANGED
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"where": "on Harness's server, through Bankr", "limits": "Cannot see files on this computer. Reading balances or a transaction needs no task: an agent on this computer reads them with the wallet tool in seconds." },
|
|
63
63
|
{ "id": "quotient", "name": "Quotient", "aliases": [], "road": "server", "home": "cloud", "runs": "server", "target": true, "tagged": true, "wake": "run",
|
|
64
64
|
"fixedRole": { "specialty": "prediction-markets", "role": "analyst" },
|
|
65
|
-
"about": "prediction
|
|
65
|
+
"about": "prediction market and asset research: searches the markets it tracks, with their odds and rules, and researches assets, portfolios, X and forecasts",
|
|
66
66
|
"where": "on Harness's server", "limits": "Takes no positions from a task. Cannot see files on this computer." },
|
|
67
67
|
{ "id": "muse", "name": "Muse", "aliases": [], "road": "hosted", "home": "cloud", "runs": "self", "target": true, "wake": "listen",
|
|
68
68
|
"about": "writing and creative drafts",
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { harnessHome } from "./paths.mjs";
|
|
5
|
+
import { notesAtStep, sessionsAtWork } from "./senseCall.mjs";
|
|
6
|
+
|
|
7
|
+
// The route at the model (docs/senses-plan.md S4.9, open item 8): for a model Harness serves on this computer, an
|
|
8
|
+
// OpenAI-style address an agent can be pointed at. Each request goes through to the model as it is, streamed or not;
|
|
9
|
+
// what Harness noticed for the agent's session (steerQueue.mjs SENSE, the notes its hooks give after a tool call)
|
|
10
|
+
// rides at the end of the request as one message from Harness, added after what the agent sent and never written into
|
|
11
|
+
// earlier turns, so the provider's cache of the conversation keeps working (spend and context, rule 7). It is the route
|
|
12
|
+
// for an agent with no hook of its own; one whose hooks already give the notes takes them there first, and a note is
|
|
13
|
+
// given once whichever way takes it. Nothing is put between an agent and its model unless the agent is pointed here.
|
|
14
|
+
//
|
|
15
|
+
// The session: the header `x-harness-session` or the request's `user`; else, under `/agent/<agent>/v1/…`, that agent's
|
|
16
|
+
// one session at work on this computer. With none, the request goes through untouched.
|
|
17
|
+
|
|
18
|
+
const AT_WORK_MS = 15 * 60_000;
|
|
19
|
+
const LARGEST_BODY = 20_000_000;
|
|
20
|
+
|
|
21
|
+
/** Where the route listens, for setup and an agent's settings: `{ url }`, or null while it is not running. */
|
|
22
|
+
export const routeFile = (home = harnessHome) =>
|
|
23
|
+
path.join(home, "senses", "model-route.json");
|
|
24
|
+
|
|
25
|
+
const readBody = (request) =>
|
|
26
|
+
new Promise((resolve, reject) => {
|
|
27
|
+
const parts = [];
|
|
28
|
+
let size = 0;
|
|
29
|
+
request.on("data", (chunk) => {
|
|
30
|
+
size += chunk.length;
|
|
31
|
+
if (size > LARGEST_BODY) {
|
|
32
|
+
reject(new Error("the request is too large"));
|
|
33
|
+
request.destroy();
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
parts.push(chunk);
|
|
37
|
+
});
|
|
38
|
+
request.on("end", () => resolve(Buffer.concat(parts).toString("utf8")));
|
|
39
|
+
request.on("error", reject);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* `serving(model)` resolves `{ base, headers }`, where a model Harness serves answers and what it asks of a request
|
|
44
|
+
* (its key; inference.mjs serving); `models()` the
|
|
45
|
+
* ids it serves. `start()` listens on this computer only and writes where; `stop()`.
|
|
46
|
+
*/
|
|
47
|
+
export function createModelRoute({
|
|
48
|
+
serving,
|
|
49
|
+
models = () => [],
|
|
50
|
+
home = harnessHome,
|
|
51
|
+
port = 0,
|
|
52
|
+
log = () => {},
|
|
53
|
+
}) {
|
|
54
|
+
let server = null;
|
|
55
|
+
|
|
56
|
+
const sessionFor = (request, body, agentInPath) => {
|
|
57
|
+
const named = request.headers["x-harness-session"] ?? body?.user;
|
|
58
|
+
if (typeof named === "string" && named) return named;
|
|
59
|
+
if (!agentInPath) return null;
|
|
60
|
+
const working = sessionsAtWork(AT_WORK_MS, home).filter(
|
|
61
|
+
(each) => each.agent === agentInPath
|
|
62
|
+
);
|
|
63
|
+
return working.length === 1 ? working[0].session : null;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const answer = (response, status, value) => {
|
|
67
|
+
response.writeHead(status, { "content-type": "application/json" });
|
|
68
|
+
response.end(JSON.stringify(value));
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const handle = async (request, response) => {
|
|
72
|
+
const url = new URL(request.url, "http://127.0.0.1");
|
|
73
|
+
const inPath = /^\/agent\/([\w.-]+)(\/v1\/.*)$/.exec(url.pathname);
|
|
74
|
+
const route = inPath ? inPath[2] : url.pathname;
|
|
75
|
+
if (request.method === "GET" && route === "/v1/models")
|
|
76
|
+
return answer(response, 200, {
|
|
77
|
+
object: "list",
|
|
78
|
+
data: models().map((id) => ({
|
|
79
|
+
id,
|
|
80
|
+
object: "model",
|
|
81
|
+
owned_by: "harness",
|
|
82
|
+
})),
|
|
83
|
+
});
|
|
84
|
+
if (request.method !== "POST" || route !== "/v1/chat/completions")
|
|
85
|
+
return answer(response, 404, { error: { message: "not here" } });
|
|
86
|
+
let body;
|
|
87
|
+
try {
|
|
88
|
+
body = JSON.parse(await readBody(request));
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return answer(response, 400, { error: { message: error.message } });
|
|
91
|
+
}
|
|
92
|
+
const model = String(body.model ?? "");
|
|
93
|
+
if (!models().includes(model))
|
|
94
|
+
return answer(response, 404, {
|
|
95
|
+
error: {
|
|
96
|
+
message: `Harness does not serve ${model || "that model"} here`,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
const session = sessionFor(request, body, inPath?.[1] ?? null);
|
|
100
|
+
const notes = session ? notesAtStep(session, null, home) : "";
|
|
101
|
+
const messages = Array.isArray(body.messages) ? body.messages : [];
|
|
102
|
+
const sent = notes
|
|
103
|
+
? { ...body, messages: [...messages, { role: "user", content: notes }] }
|
|
104
|
+
: body;
|
|
105
|
+
let base;
|
|
106
|
+
let headers = {};
|
|
107
|
+
try {
|
|
108
|
+
({ base, headers = {} } = await serving(model));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return answer(response, 503, { error: { message: error.message } });
|
|
111
|
+
}
|
|
112
|
+
const upstream = await fetch(`${base}/v1/chat/completions`, {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: { "content-type": "application/json", ...headers },
|
|
115
|
+
body: JSON.stringify(sent),
|
|
116
|
+
}).catch((error) => ({ error }));
|
|
117
|
+
if (upstream.error)
|
|
118
|
+
return answer(response, 502, {
|
|
119
|
+
error: { message: upstream.error.message },
|
|
120
|
+
});
|
|
121
|
+
response.writeHead(upstream.status, {
|
|
122
|
+
"content-type":
|
|
123
|
+
upstream.headers.get("content-type") ?? "application/json",
|
|
124
|
+
});
|
|
125
|
+
// Streamed or whole, as the model sends it.
|
|
126
|
+
for await (const chunk of upstream.body) response.write(chunk);
|
|
127
|
+
response.end();
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
start() {
|
|
132
|
+
if (server) return Promise.resolve(server.address());
|
|
133
|
+
server = http.createServer((request, response) => {
|
|
134
|
+
handle(request, response).catch((error) => {
|
|
135
|
+
log(`model route: ${error.message}`);
|
|
136
|
+
if (!response.headersSent)
|
|
137
|
+
answer(response, 500, { error: { message: error.message } });
|
|
138
|
+
else response.end();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
return new Promise((resolve) => {
|
|
142
|
+
server.listen(port, "127.0.0.1", () => {
|
|
143
|
+
const { port: bound } = server.address();
|
|
144
|
+
const url = `http://127.0.0.1:${bound}/v1`;
|
|
145
|
+
fs.mkdirSync(path.dirname(routeFile(home)), {
|
|
146
|
+
recursive: true,
|
|
147
|
+
mode: 0o700,
|
|
148
|
+
});
|
|
149
|
+
fs.writeFileSync(routeFile(home), JSON.stringify({ url }), {
|
|
150
|
+
mode: 0o600,
|
|
151
|
+
});
|
|
152
|
+
resolve({ url });
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
},
|
|
156
|
+
stop() {
|
|
157
|
+
server?.close();
|
|
158
|
+
server = null;
|
|
159
|
+
fs.rmSync(routeFile(home), { force: true });
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
}
|
package/lib/nativeHost.mjs
CHANGED
|
@@ -4,9 +4,9 @@ import path from "node:path";
|
|
|
4
4
|
import { harnessNode, paths } from "./paths.mjs";
|
|
5
5
|
|
|
6
6
|
// The Harness extension finds the daemon on its own computer through Chrome's native messaging: Chrome starts this
|
|
7
|
-
// host for the extension,
|
|
8
|
-
//
|
|
9
|
-
//
|
|
7
|
+
// host for the extension, which listens through it for what this computer hands the Harness page (it opens a hidden
|
|
8
|
+
// page to take it) and tells it which page the person is on. The page itself reaches the computer over its line from
|
|
9
|
+
// the person's signed device list, never through this host.
|
|
10
10
|
//
|
|
11
11
|
// Only the extension named in the manifest's allowed_origins can start the host (Chrome enforces it). Its ID is fixed
|
|
12
12
|
// by the public key in packages/browser-extension/manifest.json, the Chrome Web Store item's.
|
|
@@ -17,38 +17,85 @@ export const EXTENSION_IDS = ["ekdjlnkjchjplkadglnaolilhpnokgok"];
|
|
|
17
17
|
const MAX_MESSAGE_BYTES = 64 * 1024;
|
|
18
18
|
|
|
19
19
|
// Where each Chromium browser reads a person's own host manifests: its user data folder's NativeMessagingHosts.
|
|
20
|
+
// The well-known browsers by name, and any other Chromium browser found on this computer (Arc, Opera and the rest):
|
|
21
|
+
// a user data folder holds a "Local State" file and, for a browser that runs extensions, a profile with an Extensions
|
|
22
|
+
// folder. Electron apps keep a "Local State" too but run no extensions, so they get nothing.
|
|
20
23
|
// A browser that is not installed (its folder absent) gets none.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
const KNOWN_BROWSERS = {
|
|
25
|
+
darwin: [
|
|
26
|
+
"Google/Chrome",
|
|
27
|
+
"Google/Chrome Beta",
|
|
28
|
+
"Google/Chrome Canary",
|
|
29
|
+
"Chromium",
|
|
30
|
+
"BraveSoftware/Brave-Browser",
|
|
31
|
+
"Microsoft Edge",
|
|
32
|
+
"Vivaldi",
|
|
33
|
+
],
|
|
34
|
+
linux: [
|
|
35
|
+
"google-chrome",
|
|
36
|
+
"google-chrome-beta",
|
|
37
|
+
"chromium",
|
|
38
|
+
"BraveSoftware/Brave-Browser",
|
|
39
|
+
"microsoft-edge",
|
|
40
|
+
"vivaldi",
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const listDir = (folder) => {
|
|
45
|
+
try {
|
|
46
|
+
return fs.readdirSync(folder, { withFileTypes: true });
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
33
49
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** A Chromium user data folder whose browser runs extensions. */
|
|
53
|
+
const isExtensionBrowser = (folder) =>
|
|
54
|
+
fs.existsSync(path.join(folder, "Local State")) &&
|
|
55
|
+
listDir(folder).some(
|
|
56
|
+
(entry) =>
|
|
57
|
+
entry.isDirectory() &&
|
|
58
|
+
fs.existsSync(path.join(folder, entry.name, "Extensions"))
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
/** Every extension-running Chromium user data folder up to two levels under `root` ("Arc/User Data", "Opera"). */
|
|
62
|
+
function discoverBrowsers(root) {
|
|
63
|
+
const found = [];
|
|
64
|
+
for (const entry of listDir(root)) {
|
|
65
|
+
if (!entry.isDirectory()) continue;
|
|
66
|
+
const folder = path.join(root, entry.name);
|
|
67
|
+
if (isExtensionBrowser(folder)) {
|
|
68
|
+
found.push(folder);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
for (const inner of listDir(folder))
|
|
72
|
+
if (
|
|
73
|
+
inner.isDirectory() &&
|
|
74
|
+
isExtensionBrowser(path.join(folder, inner.name))
|
|
75
|
+
)
|
|
76
|
+
found.push(path.join(folder, inner.name));
|
|
44
77
|
}
|
|
45
|
-
return
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function browserFolders(platform, home) {
|
|
82
|
+
const root =
|
|
83
|
+
platform === "darwin"
|
|
84
|
+
? path.join(home, "Library", "Application Support")
|
|
85
|
+
: platform === "linux"
|
|
86
|
+
? process.env.XDG_CONFIG_HOME || path.join(home, ".config")
|
|
87
|
+
: null;
|
|
88
|
+
if (!root) return [];
|
|
89
|
+
const known = (KNOWN_BROWSERS[platform] ?? []).map((folder) =>
|
|
90
|
+
path.join(root, folder)
|
|
91
|
+
);
|
|
92
|
+
return [...new Set([...known, ...discoverBrowsers(root)])];
|
|
46
93
|
}
|
|
47
94
|
|
|
48
95
|
const manifestFor = (program) => ({
|
|
49
96
|
name: HOST_NAME,
|
|
50
97
|
description:
|
|
51
|
-
"Harness:
|
|
98
|
+
"Harness: lets the Harness extension hear the daemon on this computer",
|
|
52
99
|
path: program,
|
|
53
100
|
type: "stdio",
|
|
54
101
|
allowed_origins: EXTENSION_IDS.map((id) => `chrome-extension://${id}/`),
|
|
@@ -57,7 +104,7 @@ const manifestFor = (program) => ({
|
|
|
57
104
|
/**
|
|
58
105
|
* Installs the host for every Chromium browser on this computer: a launcher in Harness's folder that runs this
|
|
59
106
|
* runtime's CLI, and a manifest beside each browser's profiles. Windows registers hosts in the registry instead, which
|
|
60
|
-
* this does not do yet; there the
|
|
107
|
+
* this does not do yet; there the extension does not wake a Harness page for the computer.
|
|
61
108
|
*/
|
|
62
109
|
export function installNativeHost({
|
|
63
110
|
runtimeRoot,
|
|
@@ -109,8 +156,8 @@ const shellQuote = (value) => `'${String(value).replaceAll("'", `'\\''`)}'`;
|
|
|
109
156
|
|
|
110
157
|
/**
|
|
111
158
|
* The host itself, run by Chrome: reads length-prefixed JSON messages on `input` and answers each on `output`.
|
|
112
|
-
* `daemon(request)` is the daemon's control socket.
|
|
113
|
-
*
|
|
159
|
+
* `daemon(request)` is the daemon's control socket. Messages: `listen`, after which what the daemon hands the Harness
|
|
160
|
+
* page is passed on as `for-harness`, and `active-page`, the page the person is on.
|
|
114
161
|
*/
|
|
115
162
|
export async function serveNativeHost({
|
|
116
163
|
input,
|
|
@@ -149,65 +196,7 @@ export async function serveNativeHost({
|
|
|
149
196
|
);
|
|
150
197
|
return { ok: true };
|
|
151
198
|
}
|
|
152
|
-
|
|
153
|
-
if (message?.type === "this-computer") {
|
|
154
|
-
const found = await daemon({ command: "phone.identity" });
|
|
155
|
-
return found?.ok
|
|
156
|
-
? {
|
|
157
|
-
ok: true,
|
|
158
|
-
computer: {
|
|
159
|
-
publicKey: found.publicKey,
|
|
160
|
-
hostname: found.hostname,
|
|
161
|
-
label: found.label ?? null,
|
|
162
|
-
role: found.role ?? null,
|
|
163
|
-
joined: found.joined !== false,
|
|
164
|
-
connected: found.connected !== false,
|
|
165
|
-
},
|
|
166
|
-
}
|
|
167
|
-
: { ok: false, error: found?.error || "the daemon did not say" };
|
|
168
|
-
}
|
|
169
|
-
// The page asking is served by Harness, so its word alone makes no offer: the daemon asks the person on this
|
|
170
|
-
// computer, and the host waits for their answer (as long as the ask lasts, the offer's ten minutes).
|
|
171
|
-
if (message?.type === "computer-offer") {
|
|
172
|
-
let asked = null;
|
|
173
|
-
do
|
|
174
|
-
asked = await daemon(
|
|
175
|
-
{
|
|
176
|
-
command: "phone.link.ask",
|
|
177
|
-
id: asked?.id ?? null,
|
|
178
|
-
label: String(message.label ?? "").slice(0, 60),
|
|
179
|
-
waitMs: 25_000,
|
|
180
|
-
},
|
|
181
|
-
30_000
|
|
182
|
-
);
|
|
183
|
-
while (asked?.ok && asked.state === "waiting" && !closed);
|
|
184
|
-
if (asked?.ok && asked.state === "allowed" && asked.url)
|
|
185
|
-
return { ok: true, fragment: new URL(asked.url).hash };
|
|
186
|
-
return {
|
|
187
|
-
ok: false,
|
|
188
|
-
code: asked?.state ?? "failed",
|
|
189
|
-
error:
|
|
190
|
-
asked?.state === "refused" || asked?.state === "expired"
|
|
191
|
-
? "the person did not allow this browser on this computer"
|
|
192
|
-
: asked?.error || "no offer was made",
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
// The extension itself asking to hold this browser's link (its deskKey.js), never a page's ask passed on: the
|
|
196
|
-
// background sends this on its own, and the key it pairs stays in the extension.
|
|
197
|
-
if (message?.type === "extension-link") {
|
|
198
|
-
const made = await daemon({ command: "phone.link.extension" });
|
|
199
|
-
return made?.ok && made.fragment
|
|
200
|
-
? { ok: true, fragment: made.fragment }
|
|
201
|
-
: { ok: false, error: made?.error || "no offer was made" };
|
|
202
|
-
}
|
|
203
|
-
return { ok: false, error: "unknown message" };
|
|
204
|
-
} catch (error) {
|
|
205
|
-
// The daemon not running is the common case: setup was never finished here, or it stopped.
|
|
206
|
-
return {
|
|
207
|
-
ok: false,
|
|
208
|
-
error: `the Harness daemon on this computer is not running (${error.message})`,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
199
|
+
return { ok: false, error: "unknown message" };
|
|
211
200
|
};
|
|
212
201
|
let buffered = Buffer.alloc(0);
|
|
213
202
|
for await (const chunk of input) {
|
|
@@ -229,7 +218,9 @@ export async function serveNativeHost({
|
|
|
229
218
|
}
|
|
230
219
|
closed = true;
|
|
231
220
|
// The browser went: no page is the one the person is on any more.
|
|
232
|
-
await daemon({ command: "browser.page.set", page: null }).catch(
|
|
221
|
+
await daemon({ command: "browser.page.set", page: null, gone: true }).catch(
|
|
222
|
+
() => null
|
|
223
|
+
);
|
|
233
224
|
}
|
|
234
225
|
|
|
235
226
|
export function writeMessage(output, message) {
|