tryharness 0.7.53 → 0.7.57
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 +23 -19
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +484 -90
- package/lib/activity.mjs +10 -10
- package/lib/adapters.mjs +173 -222
- package/lib/agentBinaries.mjs +71 -0
- package/lib/agentRuntimeConfig.mjs +1 -1
- package/lib/agentRuntimes.mjs +34 -18
- package/lib/agentSignIn.mjs +456 -0
- package/lib/answers.mjs +5 -5
- package/lib/archetypes.d.ts +64 -0
- package/lib/archetypes.js +989 -0
- package/lib/asks.mjs +4 -4
- package/lib/audit.mjs +666 -0
- package/lib/backupSetup.mjs +143 -0
- package/lib/boardAccess.mjs +24 -11
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardFileCommands.mjs +6 -6
- package/lib/boardFiles.mjs +13 -12
- package/lib/boardMirror.mjs +124 -25
- package/lib/boardProtocol.json +88 -2
- package/lib/boardWaits.mjs +101 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +6 -6
- package/lib/cards.mjs +138 -28
- package/lib/cloudAgents.mjs +216 -0
- package/lib/cloudAi.mjs +1312 -0
- package/lib/cloudAssistants.mjs +242 -0
- package/lib/cloudBoot.mjs +119 -0
- package/lib/cloudSave.mjs +197 -0
- package/lib/cloudSsh.mjs +178 -0
- package/lib/commands.mjs +7 -2
- package/lib/context.mjs +19 -5
- package/lib/contractGate.mjs +399 -0
- package/lib/control-client.mjs +9 -1
- package/lib/controlSocket.mjs +86 -0
- package/lib/coordinator.mjs +252 -144
- package/lib/daemon.mjs +1351 -2767
- package/lib/desk.mjs +240 -0
- package/lib/desktop.mjs +178 -74
- package/lib/directLink.mjs +16 -5
- package/lib/environment.mjs +2 -1
- package/lib/environments.mjs +247 -9
- package/lib/firewall.mjs +9 -9
- package/lib/folders.mjs +20 -16
- package/lib/frames.mjs +2 -3
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +25 -7
- package/lib/goals.mjs +7 -7
- package/lib/guestRequests.mjs +94 -0
- package/lib/handoffs.mjs +8 -7
- package/lib/handover.mjs +4 -4
- package/lib/harnessModel.mjs +6 -6
- package/lib/harnessOffers.mjs +266 -0
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +26 -8
- package/lib/jobAnswers.mjs +190 -0
- package/lib/landed.mjs +887 -0
- package/lib/lane.mjs +42 -11
- package/lib/lessons.mjs +16 -16
- package/lib/library.mjs +3 -3
- package/lib/lineup.mjs +3 -0
- package/lib/linkRelay.mjs +574 -0
- package/lib/live.mjs +284 -13
- package/lib/localAgentAsk.mjs +74 -0
- package/lib/localStack.mjs +536 -0
- package/lib/localmodels.mjs +6 -4
- package/lib/loopBackstop.mjs +4 -4
- package/lib/machineIdentity.mjs +9 -1
- package/lib/machineRoutes.mjs +181 -2
- package/lib/machineRuns.mjs +76 -1
- package/lib/machineSeal.mjs +39 -12
- package/lib/mcp-stdio.mjs +163 -27
- package/lib/mcpRelay.mjs +1 -1
- package/lib/members.json +29 -2
- package/lib/members.mjs +1 -1
- package/lib/models.mjs +85 -31
- package/lib/museSession.mjs +21 -13
- package/lib/nativeHost.mjs +36 -8
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +50 -5
- package/lib/notify.mjs +7 -5
- package/lib/nudge.mjs +50 -19
- package/lib/ocr.mjs +9 -9
- package/lib/onboard.mjs +26 -20
- package/lib/oneLine.mjs +7 -7
- package/lib/ownMachines.mjs +1223 -140
- package/lib/pairing.mjs +60 -16
- package/lib/paths.mjs +19 -1
- package/lib/peerProcess.mjs +124 -0
- package/lib/personModel.mjs +89 -0
- package/lib/personNotices.mjs +151 -0
- package/lib/phones.mjs +538 -31
- package/lib/placeCommands.mjs +4 -4
- package/lib/places.mjs +400 -12
- package/lib/profile.mjs +67 -14
- package/lib/projectGit.mjs +158 -14
- package/lib/projectPieces.mjs +902 -0
- package/lib/projectSettings.mjs +7 -4
- package/lib/projects.mjs +384 -62
- package/lib/provenance.mjs +5 -1
- package/lib/proxyLinks.mjs +267 -0
- package/lib/reactions.mjs +7 -5
- package/lib/readiness.mjs +145 -23
- package/lib/record.mjs +128 -0
- package/lib/redact.mjs +4 -4
- package/lib/relayLimits.mjs +62 -0
- package/lib/release.mjs +170 -0
- package/lib/releaseKeys.json +6 -0
- package/lib/repoLinks.mjs +4 -4
- package/lib/roomChannels.mjs +24 -0
- package/lib/roomPage.mjs +1 -1
- package/lib/runAuthority.mjs +101 -0
- package/lib/runBrief.mjs +19 -8
- package/lib/runWorker.mjs +31 -7
- package/lib/runner.mjs +223 -29
- package/lib/runtime-install.mjs +143 -9
- package/lib/schedules.mjs +2 -2
- package/lib/screens.mjs +1 -1
- package/lib/sealed.mjs +11 -2
- package/lib/sealedThreads.mjs +671 -0
- package/lib/secretPatterns.json +1 -1
- package/lib/secrets.mjs +4 -4
- package/lib/seen.mjs +6 -6
- package/lib/serverWords.mjs +175 -0
- package/lib/service.mjs +22 -12
- package/lib/sessionControl.mjs +378 -90
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +57 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/speech.mjs +5 -5
- package/lib/store.mjs +420 -36
- package/lib/stuck.mjs +1 -1
- package/lib/swarm.mjs +304 -37
- package/lib/swarmDevices.mjs +319 -0
- package/lib/swarmMemory.mjs +27 -25
- package/lib/t3.mjs +24 -15
- package/lib/taskCommands.mjs +80 -20
- package/lib/taskRooms.mjs +125 -0
- package/lib/taskRuns.mjs +45 -3
- package/lib/team.json +255 -148
- package/lib/team.mjs +554 -190
- package/lib/threadActions.mjs +315 -0
- package/lib/threadAuthority.mjs +101 -0
- package/lib/threadBriefs.mjs +178 -0
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +209 -50
- package/lib/threadView.mjs +20 -7
- package/lib/usageTotals.mjs +2 -3
- package/lib/vault.mjs +101 -10
- package/lib/vaultCloud.mjs +2 -2
- package/lib/vaultCommands.mjs +15 -6
- package/lib/vaultTools.js +2 -2
- package/lib/verdicts.mjs +328 -0
- package/lib/wakes.mjs +1 -1
- package/lib/watches.mjs +2 -2
- package/lib/windowAsks.mjs +13 -5
- package/lib/worktrees.mjs +157 -25
- package/npm-shrinkwrap.json +1093 -0
- package/package.json +12 -11
- package/release.json +220 -0
- package/lib/modelTiers.json +0 -1183
- package/lib/modelTiers.mjs +0 -133
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { randomBytes, timingSafeEqual } from "node:crypto";
|
|
4
|
+
import { readJson, writeJsonAtomic } from "./files.mjs";
|
|
5
|
+
import { paths } from "./paths.mjs";
|
|
6
|
+
|
|
7
|
+
// The cloud computer's local model address (docs/cloud-swarm.md, inference boundary). The agents the person put on
|
|
8
|
+
// their own proxy link or their own API key call it, as they would call any model server:
|
|
9
|
+
//
|
|
10
|
+
// - /link/<agent>/v1/...: a proxy link. Each call goes sealed to the computer that holds the link (proxyLinks.mjs)
|
|
11
|
+
// over the line the two computers share (ownMachines.mjs linkCall), its answer coming back in parts as it streams.
|
|
12
|
+
// Only this computer's own processes reach the address, and only with this computer's link token, which the
|
|
13
|
+
// agents' settings carry.
|
|
14
|
+
// - /key/<agent>/<provider>/...: the person's own key. The agent sends its key as it always does; it is passed on to
|
|
15
|
+
// that provider only, and only if it is the key the person gave this computer for that agent.
|
|
16
|
+
//
|
|
17
|
+
// Owner, October 26: when the person's link or key does not answer, the request goes to Harness's providers. A call
|
|
18
|
+
// that fails before its answer starts (the computer holding the link is away, the link or key errors, the key is out
|
|
19
|
+
// of credit, the model refuses) is sent again to Harness's AI gateway on Auto, with this computer's AI key, charged
|
|
20
|
+
// like any use of Harness's AI. An answer that broke off after it began is not sent again. The next call tries the
|
|
21
|
+
// person's link or key first, so it is back on theirs the moment it answers. Each switch is kept here, so the run it
|
|
22
|
+
// happened in says so (taskRuns.mjs) and the person's window shows it. On the privacy setting nothing goes to Harness:
|
|
23
|
+
// the call fails and says why.
|
|
24
|
+
|
|
25
|
+
export const linkRelayFile = () => path.join(paths.home, "link-relay.json");
|
|
26
|
+
|
|
27
|
+
/** Where each provider of a person's key answers, under its API's version path. */
|
|
28
|
+
export const KEY_PROVIDERS = {
|
|
29
|
+
anthropic: "https://api.anthropic.com/v1",
|
|
30
|
+
openai: "https://api.openai.com/v1",
|
|
31
|
+
openrouter: "https://openrouter.ai/api/v1",
|
|
32
|
+
xai: "https://api.x.ai/v1",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// What each agent speaks to its model (cloudAi.mjs points it at this address): the one call the link is tested with.
|
|
36
|
+
const TEST_CALLS = {
|
|
37
|
+
"claude-code": (model) => ({
|
|
38
|
+
path: "/messages",
|
|
39
|
+
headers: { "anthropic-version": "2023-06-01" },
|
|
40
|
+
body: {
|
|
41
|
+
model,
|
|
42
|
+
max_tokens: 16,
|
|
43
|
+
messages: [{ role: "user", content: "Say OK." }],
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
46
|
+
codex: (model) => ({
|
|
47
|
+
path: "/responses",
|
|
48
|
+
headers: {},
|
|
49
|
+
body: { model, input: "Say OK.", max_output_tokens: 16 },
|
|
50
|
+
}),
|
|
51
|
+
};
|
|
52
|
+
const chatTest = (model) => ({
|
|
53
|
+
path: "/chat/completions",
|
|
54
|
+
headers: {},
|
|
55
|
+
body: {
|
|
56
|
+
model,
|
|
57
|
+
max_tokens: 16,
|
|
58
|
+
messages: [{ role: "user", content: "Say OK." }],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// The model calls Harness's gateway answers, by the path after the API's version (src/server/cloudAi/http.ts).
|
|
63
|
+
const GATEWAY_CALLS = new Set([
|
|
64
|
+
"/messages",
|
|
65
|
+
"/messages/count_tokens",
|
|
66
|
+
"/chat/completions",
|
|
67
|
+
"/responses",
|
|
68
|
+
]);
|
|
69
|
+
// The request headers a model call needs. The agent's own key and this computer's link token stay out of them.
|
|
70
|
+
const PASSED_HEADERS = [
|
|
71
|
+
"content-type",
|
|
72
|
+
"accept",
|
|
73
|
+
"anthropic-version",
|
|
74
|
+
"anthropic-beta",
|
|
75
|
+
"openai-beta",
|
|
76
|
+
];
|
|
77
|
+
// What the person's own key travels in, to its own provider only.
|
|
78
|
+
const KEY_HEADERS = ["authorization", "x-api-key"];
|
|
79
|
+
|
|
80
|
+
// One error both wires read: Anthropic's clients take error.type and error.message, OpenAI's error.message.
|
|
81
|
+
const errorBody = (message) =>
|
|
82
|
+
JSON.stringify({ type: "error", error: { type: "api_error", message } });
|
|
83
|
+
|
|
84
|
+
const sameSecret = (given, held) => {
|
|
85
|
+
const a = Buffer.from(String(given || ""));
|
|
86
|
+
const b = Buffer.from(String(held || ""));
|
|
87
|
+
return a.length > 0 && a.length === b.length && timingSafeEqual(a, b);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const tokenOf = (headers) =>
|
|
91
|
+
headers["x-api-key"] ||
|
|
92
|
+
String(headers.authorization || "").replace(/^Bearer\s+/i, "");
|
|
93
|
+
|
|
94
|
+
const pick = (headers, names) =>
|
|
95
|
+
Object.fromEntries(
|
|
96
|
+
names
|
|
97
|
+
.filter((name) => headers[name] != null)
|
|
98
|
+
.map((name) => [name, String(headers[name])])
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// What an error body says, in a few words: never more than the provider's own message.
|
|
102
|
+
const saidIn = (text) => {
|
|
103
|
+
try {
|
|
104
|
+
const body = JSON.parse(text);
|
|
105
|
+
return String(body.error?.message ?? body.message ?? "").slice(0, 300);
|
|
106
|
+
} catch {
|
|
107
|
+
return String(text).slice(0, 200);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// The path after the API's version: an agent names it with the version (Claude Code's base URL has none), and some
|
|
112
|
+
// name the version twice once their own base URL carries it.
|
|
113
|
+
const wirePath = (rest) => {
|
|
114
|
+
let wire = rest || "/";
|
|
115
|
+
while (/^\/v1(\/|$)/.test(wire)) wire = wire.slice(3) || "/";
|
|
116
|
+
return wire;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export function createLinkRelay({
|
|
120
|
+
file = linkRelayFile(),
|
|
121
|
+
log = () => {},
|
|
122
|
+
// How an agent's calls run here (cloudAi.mjs routeOf): { kind: "link", link: { holder, holderLabel, link } }, or
|
|
123
|
+
// { kind: "standin", reason } when the person's link or key is not set up here, or null when the agent is not on
|
|
124
|
+
// its own link or key.
|
|
125
|
+
routeOf = () => null,
|
|
126
|
+
// The key the person gave this computer for an agent: { provider, key } or null.
|
|
127
|
+
keyOf = () => null,
|
|
128
|
+
// One sealed call to the computer holding a link: its opened answer, or { unreachable, error } when it did not
|
|
129
|
+
// arrive ("timed_out": asked again; anything else: the call fails).
|
|
130
|
+
linkCall = async () => ({
|
|
131
|
+
unreachable: "offline",
|
|
132
|
+
error: "no line to your other computers",
|
|
133
|
+
}),
|
|
134
|
+
// Harness's AI for this computer: { baseUrl, key } (cloudAi.mjs harnessAi).
|
|
135
|
+
harnessAi = async () => {
|
|
136
|
+
throw new Error("Harness's AI is not set up on this computer");
|
|
137
|
+
},
|
|
138
|
+
// The privacy setting: nothing goes to Harness.
|
|
139
|
+
privacy = () => false,
|
|
140
|
+
// Where each provider of a person's key answers.
|
|
141
|
+
providers = KEY_PROVIDERS,
|
|
142
|
+
fetchImpl = fetch,
|
|
143
|
+
}) {
|
|
144
|
+
const saved = readJson(file, {});
|
|
145
|
+
const token =
|
|
146
|
+
typeof saved.token === "string" && saved.token.length >= 32
|
|
147
|
+
? saved.token
|
|
148
|
+
: `hlink_${randomBytes(24).toString("base64url")}`;
|
|
149
|
+
let port = Number.isInteger(saved.port) ? saved.port : 0;
|
|
150
|
+
let server = null;
|
|
151
|
+
|
|
152
|
+
// Per agent: when a call last went to Harness's AI instead, why, and when one last answered on the person's own.
|
|
153
|
+
const switches = new Map();
|
|
154
|
+
const switched = (agent, kind, reason) => {
|
|
155
|
+
const was = switches.get(agent) ?? {};
|
|
156
|
+
switches.set(agent, { ...was, kind, reason, at: Date.now() });
|
|
157
|
+
log(`${agent}: on Harness's AI for a call: ${reason}`);
|
|
158
|
+
};
|
|
159
|
+
const answeredOwn = (agent) =>
|
|
160
|
+
switches.set(agent, {
|
|
161
|
+
...(switches.get(agent) ?? {}),
|
|
162
|
+
ownAt: Date.now(),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const where = (link) =>
|
|
166
|
+
link.holderLabel ? `your computer ${link.holderLabel}` : "your computer";
|
|
167
|
+
|
|
168
|
+
// ---- one call through a link: its answer, or why it did not start ----
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Starts one call through a link and reads until its answer begins. { ok: true, status, contentType, first, pump }
|
|
172
|
+
* for an answer that began (2xx): `pump(data, stopped)` hands on the rest and resolves an error if it broke off.
|
|
173
|
+
* { ok: false, reason } for a call that failed before its answer began.
|
|
174
|
+
*/
|
|
175
|
+
const startLink = async (link, request) => {
|
|
176
|
+
const started = await linkCall(link.holder, {
|
|
177
|
+
op: "start",
|
|
178
|
+
link: link.link,
|
|
179
|
+
...request,
|
|
180
|
+
});
|
|
181
|
+
if (started.unreachable)
|
|
182
|
+
return {
|
|
183
|
+
ok: false,
|
|
184
|
+
reason: `${where(link)} could not be reached (${started.error || started.unreachable})`,
|
|
185
|
+
};
|
|
186
|
+
if (!started.ok)
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
reason: started.error || "your proxy link refused the call",
|
|
190
|
+
};
|
|
191
|
+
const close = () =>
|
|
192
|
+
void linkCall(link.holder, { op: "close", call: started.call });
|
|
193
|
+
let offset = 0;
|
|
194
|
+
const read = async () => {
|
|
195
|
+
for (;;) {
|
|
196
|
+
const part = await linkCall(link.holder, {
|
|
197
|
+
op: "read",
|
|
198
|
+
call: started.call,
|
|
199
|
+
offset,
|
|
200
|
+
});
|
|
201
|
+
// The line's own deadline passed with nothing new: the same part is asked for again.
|
|
202
|
+
if (part.unreachable === "timed_out") continue;
|
|
203
|
+
if (part.unreachable)
|
|
204
|
+
return {
|
|
205
|
+
error: `${where(link)} stopped answering (${part.error || part.unreachable})`,
|
|
206
|
+
};
|
|
207
|
+
if (!part.ok)
|
|
208
|
+
return { error: part.error || "your proxy link broke off" };
|
|
209
|
+
const bytes = Buffer.from(String(part.data || ""), "base64");
|
|
210
|
+
offset = part.offset ?? offset + bytes.length;
|
|
211
|
+
return { part, bytes };
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
let first = await read();
|
|
215
|
+
const collected = [];
|
|
216
|
+
while (!first.error && !first.part.status && !first.part.done) {
|
|
217
|
+
collected.push(first.bytes);
|
|
218
|
+
first = await read();
|
|
219
|
+
}
|
|
220
|
+
if (first.error) {
|
|
221
|
+
close();
|
|
222
|
+
return { ok: false, reason: first.error };
|
|
223
|
+
}
|
|
224
|
+
const { part } = first;
|
|
225
|
+
collected.push(first.bytes);
|
|
226
|
+
if (!part.status || part.status < 200 || part.status >= 300) {
|
|
227
|
+
// A refusal is short: read to its end for its words.
|
|
228
|
+
let rest = part;
|
|
229
|
+
while (!rest.done) {
|
|
230
|
+
const next = await read();
|
|
231
|
+
if (next.error) break;
|
|
232
|
+
collected.push(next.bytes);
|
|
233
|
+
rest = next.part;
|
|
234
|
+
}
|
|
235
|
+
close();
|
|
236
|
+
const said = saidIn(Buffer.concat(collected).toString("utf8"));
|
|
237
|
+
return {
|
|
238
|
+
ok: false,
|
|
239
|
+
reason: part.status
|
|
240
|
+
? `your model answered ${part.status}${said ? `: ${said}` : ""}`
|
|
241
|
+
: (part.error ?? "your model did not answer"),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const pump = async (data, stopped) => {
|
|
245
|
+
try {
|
|
246
|
+
let current = part;
|
|
247
|
+
while (!current.done) {
|
|
248
|
+
if (stopped()) return null;
|
|
249
|
+
const next = await read();
|
|
250
|
+
if (next.error) return next.error;
|
|
251
|
+
if (next.bytes.length) data(next.bytes);
|
|
252
|
+
current = next.part;
|
|
253
|
+
}
|
|
254
|
+
return current.error
|
|
255
|
+
? `your proxy link broke off: ${current.error}`
|
|
256
|
+
: null;
|
|
257
|
+
} finally {
|
|
258
|
+
close();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
return {
|
|
262
|
+
ok: true,
|
|
263
|
+
status: part.status,
|
|
264
|
+
contentType: part.contentType,
|
|
265
|
+
first: Buffer.concat(collected),
|
|
266
|
+
pump,
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// ---- one call with the person's own key, to its own provider ----
|
|
271
|
+
|
|
272
|
+
const fromFetch = (response) => ({
|
|
273
|
+
status: response.status,
|
|
274
|
+
contentType: response.headers.get("content-type") ?? "",
|
|
275
|
+
first: Buffer.alloc(0),
|
|
276
|
+
pump: async (data, stopped) => {
|
|
277
|
+
try {
|
|
278
|
+
for await (const bytes of response.body ?? []) {
|
|
279
|
+
if (stopped()) return null;
|
|
280
|
+
data(Buffer.from(bytes));
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
} catch (error) {
|
|
284
|
+
return `the answer broke off (${error.message})`;
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
const startKey = async (provider, request, headers) => {
|
|
290
|
+
const origin = providers[provider];
|
|
291
|
+
if (!origin)
|
|
292
|
+
return { ok: false, reason: `${provider} is not a key provider here` };
|
|
293
|
+
let response;
|
|
294
|
+
try {
|
|
295
|
+
response = await fetchImpl(`${origin}${request.wire}`, {
|
|
296
|
+
method: request.method,
|
|
297
|
+
headers: {
|
|
298
|
+
...pick(headers, PASSED_HEADERS),
|
|
299
|
+
...pick(headers, KEY_HEADERS),
|
|
300
|
+
},
|
|
301
|
+
...(request.body != null && request.method === "POST"
|
|
302
|
+
? { body: request.body }
|
|
303
|
+
: {}),
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
return {
|
|
307
|
+
ok: false,
|
|
308
|
+
reason: `your key's provider did not answer (${error.message})`,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
if (response.status < 200 || response.status >= 300) {
|
|
312
|
+
const said = saidIn(await response.text().catch(() => ""));
|
|
313
|
+
return {
|
|
314
|
+
ok: false,
|
|
315
|
+
reason: `your key's provider answered ${response.status}${said ? `: ${said}` : ""}`,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
return { ok: true, ...fromFetch(response) };
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// ---- the same call on Harness's AI, on Auto ----
|
|
322
|
+
|
|
323
|
+
const startHarness = async (agent, kind, request, headers) => {
|
|
324
|
+
const ai = await harnessAi();
|
|
325
|
+
let body = request.body;
|
|
326
|
+
if (body != null) {
|
|
327
|
+
try {
|
|
328
|
+
body = JSON.stringify({ ...JSON.parse(body), model: "harness" });
|
|
329
|
+
} catch {}
|
|
330
|
+
}
|
|
331
|
+
const response = await fetchImpl(
|
|
332
|
+
`${String(ai.baseUrl).replace(/\/$/, "")}/${agent}/v1${request.wire}`,
|
|
333
|
+
{
|
|
334
|
+
method: request.method,
|
|
335
|
+
headers: {
|
|
336
|
+
...pick(headers, PASSED_HEADERS),
|
|
337
|
+
authorization: `Bearer ${ai.key}`,
|
|
338
|
+
// The gateway answers an agent the person put on their own link or key only for this (cloudAi/gateway.ts).
|
|
339
|
+
"x-harness-fallback": kind,
|
|
340
|
+
},
|
|
341
|
+
...(body != null && request.method === "POST" ? { body } : {}),
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
return fromFetch(response);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
// ---- the local address ----
|
|
348
|
+
|
|
349
|
+
const parse = (pathname) => {
|
|
350
|
+
const link = /^\/link\/([a-z0-9-]+)\/v1(\/[A-Za-z0-9/_.:-]*)$/.exec(
|
|
351
|
+
pathname
|
|
352
|
+
);
|
|
353
|
+
if (link) return { route: "link", agent: link[1], wire: wirePath(link[2]) };
|
|
354
|
+
const key =
|
|
355
|
+
/^\/key\/([a-z0-9-]+)\/([a-z0-9-]+)(\/[A-Za-z0-9/_.:-]*)?$/.exec(
|
|
356
|
+
pathname
|
|
357
|
+
);
|
|
358
|
+
if (key)
|
|
359
|
+
return {
|
|
360
|
+
route: "key",
|
|
361
|
+
agent: key[1],
|
|
362
|
+
provider: key[2],
|
|
363
|
+
wire: wirePath(key[3]),
|
|
364
|
+
};
|
|
365
|
+
return null;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const handle = async (request, response) => {
|
|
369
|
+
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
370
|
+
const fail = (status, message) => {
|
|
371
|
+
if (response.headersSent) return response.destroy();
|
|
372
|
+
response.writeHead(status, { "content-type": "application/json" });
|
|
373
|
+
response.end(errorBody(message));
|
|
374
|
+
};
|
|
375
|
+
const parsed = parse(url.pathname);
|
|
376
|
+
if (!parsed) return fail(404, "not a model address on this computer");
|
|
377
|
+
const { agent } = parsed;
|
|
378
|
+
const route = routeOf(agent);
|
|
379
|
+
if (parsed.route === "link") {
|
|
380
|
+
if (!sameSecret(tokenOf(request.headers), token))
|
|
381
|
+
return fail(401, "This computer's link token is not valid.");
|
|
382
|
+
if (!route)
|
|
383
|
+
return fail(
|
|
384
|
+
403,
|
|
385
|
+
`${agent} is not on your own proxy link or key on this computer.`
|
|
386
|
+
);
|
|
387
|
+
} else {
|
|
388
|
+
const held = keyOf(agent);
|
|
389
|
+
if (
|
|
390
|
+
!held ||
|
|
391
|
+
held.provider !== parsed.provider ||
|
|
392
|
+
!sameSecret(tokenOf(request.headers), held.key)
|
|
393
|
+
)
|
|
394
|
+
return fail(
|
|
395
|
+
401,
|
|
396
|
+
"That is not the key you gave this computer for this agent."
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
const kind =
|
|
400
|
+
parsed.route === "key"
|
|
401
|
+
? "key"
|
|
402
|
+
: route.kind === "link"
|
|
403
|
+
? "link"
|
|
404
|
+
: "standin";
|
|
405
|
+
const chunks = [];
|
|
406
|
+
for await (const chunk of request) chunks.push(chunk);
|
|
407
|
+
const call = {
|
|
408
|
+
method: request.method,
|
|
409
|
+
path: parsed.wire,
|
|
410
|
+
wire: parsed.wire,
|
|
411
|
+
headers: request.headers,
|
|
412
|
+
body: chunks.length ? Buffer.concat(chunks).toString("utf8") : null,
|
|
413
|
+
};
|
|
414
|
+
let gone = false;
|
|
415
|
+
response.on("close", () => {
|
|
416
|
+
if (!response.writableFinished) gone = true;
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
const own =
|
|
420
|
+
kind === "key"
|
|
421
|
+
? await startKey(parsed.provider, call, request.headers)
|
|
422
|
+
: kind === "link"
|
|
423
|
+
? await startLink(route.link, call)
|
|
424
|
+
: { ok: false, reason: route.reason };
|
|
425
|
+
let answer = own;
|
|
426
|
+
if (own.ok) answeredOwn(agent);
|
|
427
|
+
else {
|
|
428
|
+
const yours = kind === "key" ? "your API key" : "your proxy link";
|
|
429
|
+
if (privacy())
|
|
430
|
+
return fail(
|
|
431
|
+
502,
|
|
432
|
+
`${yours[0].toUpperCase()}${yours.slice(1)} did not answer: ${own.reason}. Your privacy setting keeps this from going to Harness's AI.`
|
|
433
|
+
);
|
|
434
|
+
if (!GATEWAY_CALLS.has(call.wire) || request.method !== "POST")
|
|
435
|
+
return fail(502, `${yours} did not answer: ${own.reason}.`);
|
|
436
|
+
try {
|
|
437
|
+
answer = await startHarness(
|
|
438
|
+
agent,
|
|
439
|
+
kind === "key" ? "key" : "link",
|
|
440
|
+
call,
|
|
441
|
+
request.headers
|
|
442
|
+
);
|
|
443
|
+
} catch (error) {
|
|
444
|
+
return fail(
|
|
445
|
+
502,
|
|
446
|
+
`${yours} did not answer (${own.reason}), and Harness's AI could not take it: ${error.message}.`
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
switched(agent, kind === "key" ? "key" : "link", own.reason);
|
|
450
|
+
}
|
|
451
|
+
response.writeHead(answer.status, {
|
|
452
|
+
"content-type": answer.contentType || "application/octet-stream",
|
|
453
|
+
});
|
|
454
|
+
if (answer.first?.length) response.write(answer.first);
|
|
455
|
+
const brokeOff = await answer.pump(
|
|
456
|
+
(bytes) => response.write(bytes),
|
|
457
|
+
() => gone
|
|
458
|
+
);
|
|
459
|
+
if (brokeOff) {
|
|
460
|
+
log(`${agent}: an answer broke off: ${brokeOff}`);
|
|
461
|
+
response.destroy();
|
|
462
|
+
} else response.end();
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
const listen = (at) =>
|
|
466
|
+
new Promise((resolve, reject) => {
|
|
467
|
+
const made = http.createServer((request, response) => {
|
|
468
|
+
handle(request, response).catch((error) => {
|
|
469
|
+
log(`model address: ${error.message}`);
|
|
470
|
+
if (!response.headersSent) {
|
|
471
|
+
response.writeHead(502, { "content-type": "application/json" });
|
|
472
|
+
response.end(errorBody(`The call failed here: ${error.message}`));
|
|
473
|
+
} else response.destroy();
|
|
474
|
+
});
|
|
475
|
+
});
|
|
476
|
+
made.once("error", reject);
|
|
477
|
+
made.listen(at, "127.0.0.1", () => resolve(made));
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
/** Listens on the address the agents' settings name, the same one after a restart where it is still free. */
|
|
481
|
+
const start = async () => {
|
|
482
|
+
if (server) return address();
|
|
483
|
+
server = await listen(port).catch((error) =>
|
|
484
|
+
error.code === "EADDRINUSE" && port ? listen(0) : Promise.reject(error)
|
|
485
|
+
);
|
|
486
|
+
port = server.address().port;
|
|
487
|
+
writeJsonAtomic(file, { port, token });
|
|
488
|
+
return address();
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
const address = () => ({
|
|
492
|
+
baseUrl: `http://127.0.0.1:${port}/link`,
|
|
493
|
+
keyBaseUrl: `http://127.0.0.1:${port}/key`,
|
|
494
|
+
token,
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* The test before a link is kept: one real call through it on the agent's own wire, which must answer 2xx (never
|
|
499
|
+
* Harness's AI in its place). With no model named, the link's own model list names the first one.
|
|
500
|
+
* { ok, model } or { ok: false, error }.
|
|
501
|
+
*/
|
|
502
|
+
const test = async (agent, link, model = null) => {
|
|
503
|
+
const call = async (request) => {
|
|
504
|
+
const started = await startLink(link, request);
|
|
505
|
+
if (!started.ok) return { failure: started.reason };
|
|
506
|
+
const parts = [started.first];
|
|
507
|
+
const broke = await started.pump(
|
|
508
|
+
(bytes) => parts.push(bytes),
|
|
509
|
+
() => false
|
|
510
|
+
);
|
|
511
|
+
return {
|
|
512
|
+
failure: broke,
|
|
513
|
+
text: Buffer.concat(parts).toString("utf8"),
|
|
514
|
+
};
|
|
515
|
+
};
|
|
516
|
+
let picked = model ? String(model).trim() : "";
|
|
517
|
+
if (!picked) {
|
|
518
|
+
const listed = await call({ method: "GET", path: "/models" });
|
|
519
|
+
if (listed.failure)
|
|
520
|
+
return {
|
|
521
|
+
ok: false,
|
|
522
|
+
error: `Your proxy link did not answer: ${listed.failure}.`,
|
|
523
|
+
};
|
|
524
|
+
try {
|
|
525
|
+
picked = String(JSON.parse(listed.text).data?.[0]?.id ?? "");
|
|
526
|
+
} catch {}
|
|
527
|
+
if (!picked)
|
|
528
|
+
return {
|
|
529
|
+
ok: false,
|
|
530
|
+
error: "Your proxy link lists no model. Name the model it serves.",
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
const shaped = (TEST_CALLS[agent] ?? chatTest)(picked);
|
|
534
|
+
const tried = await call({
|
|
535
|
+
method: "POST",
|
|
536
|
+
path: shaped.path,
|
|
537
|
+
headers: { "content-type": "application/json", ...shaped.headers },
|
|
538
|
+
body: JSON.stringify(shaped.body),
|
|
539
|
+
});
|
|
540
|
+
if (tried.failure)
|
|
541
|
+
return {
|
|
542
|
+
ok: false,
|
|
543
|
+
error: `Your proxy link did not answer for ${picked}: ${tried.failure}.`,
|
|
544
|
+
};
|
|
545
|
+
return { ok: true, model: picked };
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
/** Whether an agent's calls are on Harness's AI just now, and why: its last call went there and none answered on its own since. */
|
|
549
|
+
const switchOf = (agent) => {
|
|
550
|
+
const state = switches.get(agent);
|
|
551
|
+
return state?.at && state.at > (state.ownAt ?? 0)
|
|
552
|
+
? {
|
|
553
|
+
kind: state.kind,
|
|
554
|
+
reason: state.reason,
|
|
555
|
+
at: new Date(state.at).toISOString(),
|
|
556
|
+
}
|
|
557
|
+
: null;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
/** Why an agent's calls went to Harness's AI since a moment (a run's start), or null when none did. */
|
|
561
|
+
const switchedSince = (agent, since) => {
|
|
562
|
+
const state = switches.get(agent);
|
|
563
|
+
return state?.at && state.at >= since
|
|
564
|
+
? { kind: state.kind, reason: state.reason }
|
|
565
|
+
: null;
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
const close = () => {
|
|
569
|
+
server?.close();
|
|
570
|
+
server = null;
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
return { start, address, test, switchOf, switchedSince, close };
|
|
574
|
+
}
|