tryharness 0.7.45 → 0.7.54
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 +239 -14
- package/assets/dsh-plugin.mjs +72 -0
- package/assets/opencode-plugin.js +4 -3
- package/cli.mjs +497 -196
- package/lib/actorRoster.mjs +28 -5
- package/lib/adapters.mjs +165 -222
- package/lib/agentRuntimeConfig.mjs +36 -0
- package/lib/agentRuntimes.mjs +570 -0
- package/lib/agentSignIn.mjs +226 -0
- package/lib/atlas.mjs +7 -2
- package/lib/audit.mjs +643 -0
- package/lib/backupSetup.mjs +141 -0
- package/lib/boardAccess.mjs +8 -1
- package/lib/boardBackup.mjs +283 -0
- package/lib/boardCommands.mjs +5 -10
- package/lib/boardFileCommands.mjs +2 -1
- package/lib/boardMirror.mjs +523 -0
- package/lib/boardProtocol.json +263 -6
- package/lib/boardTools.mjs +2 -6
- package/lib/boardWaits.mjs +102 -0
- package/lib/book.mjs +659 -0
- package/lib/bridge.mjs +26 -4
- package/lib/browserWake.mjs +60 -0
- package/lib/cards.mjs +159 -16
- package/lib/cloudBoot.mjs +124 -0
- package/lib/commands.mjs +25 -5
- package/lib/connectionTools.d.ts +13 -0
- package/lib/connectionTools.js +45 -0
- package/lib/connectorAuth.mjs +309 -0
- package/lib/connectorCatalog.json +41 -0
- package/lib/connectorCommands.mjs +121 -0
- package/lib/connectorGoogle.mjs +513 -0
- package/lib/connectors.mjs +805 -0
- package/lib/context.mjs +18 -4
- package/lib/contractGate.mjs +377 -0
- package/lib/coordinator.mjs +152 -39
- package/lib/coreModels.mjs +78 -0
- package/lib/daemon.mjs +973 -54
- package/lib/desk.mjs +239 -0
- package/lib/desktop.mjs +113 -8
- package/lib/digest.mjs +32 -0
- package/lib/directLink.mjs +283 -0
- package/lib/environments.mjs +825 -0
- package/lib/folders.mjs +4 -4
- package/lib/gitHost.mjs +174 -0
- package/lib/goalCommands.mjs +58 -14
- package/lib/hookDialects.mjs +298 -0
- package/lib/inference.mjs +263 -152
- package/lib/landed.mjs +887 -0
- package/lib/lessons.mjs +6 -0
- package/lib/lineup.mjs +324 -0
- package/lib/live.mjs +284 -13
- package/lib/localmodels.mjs +10 -0
- package/lib/machineIdentity.mjs +3 -0
- package/lib/machineRoutes.mjs +40 -2
- package/lib/machineRuns.mjs +30 -3
- package/lib/machineSeal.mjs +69 -10
- package/lib/mcp-stdio.mjs +189 -120
- package/lib/mcpRelay.mjs +119 -0
- package/lib/members.json +26 -0
- package/lib/members.mjs +33 -0
- package/lib/modelMemory.mjs +100 -0
- package/lib/models.mjs +48 -15
- package/lib/museSession.mjs +122 -0
- package/lib/nativeHost.mjs +203 -0
- package/lib/neovim.mjs +126 -0
- package/lib/network.mjs +7 -2
- package/lib/nudge.mjs +57 -18
- package/lib/onboard.mjs +430 -1
- package/lib/oneclawLocal.mjs +150 -0
- package/lib/ownMachines.mjs +828 -39
- package/lib/pairing.mjs +182 -0
- package/lib/paths.mjs +8 -0
- package/lib/personModel.mjs +89 -0
- package/lib/phones.mjs +119 -3
- package/lib/placeCommands.mjs +27 -0
- package/lib/places.mjs +337 -9
- package/lib/plans.mjs +151 -27
- package/lib/profile.mjs +152 -0
- package/lib/projects.mjs +624 -137
- package/lib/readiness.mjs +400 -0
- package/lib/record.mjs +115 -0
- package/lib/repoLinks.mjs +354 -0
- package/lib/roomChannels.mjs +641 -0
- package/lib/runBrief.mjs +32 -2
- package/lib/runRequests.mjs +157 -0
- package/lib/runWorker.mjs +37 -9
- package/lib/runner.mjs +74 -11
- package/lib/secretPatterns.json +55 -0
- package/lib/secrets.mjs +10 -27
- package/lib/seen.mjs +102 -0
- package/lib/service.mjs +11 -6
- package/lib/sessionControl.mjs +386 -96
- package/lib/sessionIndex.mjs +5 -3
- package/lib/specialties.json +74 -0
- package/lib/specialties.mjs +477 -0
- package/lib/specialtyKeys.d.ts +3 -0
- package/lib/specialtyKeys.js +58 -0
- package/lib/specialtyNotice.mjs +147 -0
- package/lib/specialtySnapshot.mjs +138 -0
- package/lib/store.mjs +829 -29
- package/lib/subagents.mjs +8 -4
- package/lib/swarm.mjs +483 -37
- package/lib/swarmMemory.mjs +947 -0
- package/lib/t3.mjs +56 -17
- package/lib/taskCommands.mjs +91 -13
- package/lib/taskRuns.mjs +68 -11
- package/lib/team.json +253 -144
- package/lib/team.mjs +538 -147
- package/lib/threadLine.mjs +116 -0
- package/lib/threadRuns.mjs +78 -12
- package/lib/threadView.mjs +66 -16
- package/lib/tokenizer.mjs +405 -0
- package/lib/ui.mjs +16 -2
- package/lib/usageTotals.mjs +148 -0
- package/lib/vault.mjs +1144 -0
- package/lib/vaultCloud.mjs +117 -0
- package/lib/vaultCommands.mjs +201 -0
- package/lib/vaultTools.d.ts +12 -0
- package/lib/vaultTools.js +42 -0
- package/lib/windowAsks.mjs +42 -0
- package/lib/workFocus.mjs +59 -0
- package/lib/workLinks.mjs +90 -0
- package/lib/worktrees.mjs +51 -13
- package/package.json +10 -5
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { authorizeConnector } from "./connectorAuth.mjs";
|
|
4
|
+
import {
|
|
5
|
+
GOOGLE_OAUTH,
|
|
6
|
+
createGoogleConnector,
|
|
7
|
+
googleOperation,
|
|
8
|
+
googleProvider,
|
|
9
|
+
} from "./connectorGoogle.mjs";
|
|
10
|
+
import {
|
|
11
|
+
createCipheriv,
|
|
12
|
+
createDecipheriv,
|
|
13
|
+
createHash,
|
|
14
|
+
randomBytes,
|
|
15
|
+
randomUUID,
|
|
16
|
+
} from "node:crypto";
|
|
17
|
+
|
|
18
|
+
const STATE = "connectors:v1";
|
|
19
|
+
const terminal = new Set(["returned", "failed", "unknown", "cancelled"]);
|
|
20
|
+
const json = (value) => JSON.parse(JSON.stringify(value));
|
|
21
|
+
const canonical = (value) => {
|
|
22
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
23
|
+
if (value && typeof value === "object")
|
|
24
|
+
return `{${Object.keys(value)
|
|
25
|
+
.sort()
|
|
26
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
27
|
+
.join(",")}}`;
|
|
28
|
+
return JSON.stringify(value);
|
|
29
|
+
};
|
|
30
|
+
const digest = (value) =>
|
|
31
|
+
createHash("sha256").update(canonical(value)).digest("hex");
|
|
32
|
+
const object = (value) =>
|
|
33
|
+
value && typeof value === "object" && !Array.isArray(value);
|
|
34
|
+
const refuse = (error) => ({ ok: false, error });
|
|
35
|
+
|
|
36
|
+
function endpoint(value) {
|
|
37
|
+
const url = new URL(value);
|
|
38
|
+
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
|
|
39
|
+
if (
|
|
40
|
+
url.username ||
|
|
41
|
+
url.password ||
|
|
42
|
+
url.hash ||
|
|
43
|
+
(url.protocol !== "https:" && !(loopback && url.protocol === "http:"))
|
|
44
|
+
)
|
|
45
|
+
throw new Error(
|
|
46
|
+
"a connection needs HTTPS, or an HTTP server on this computer, with no URL credentials"
|
|
47
|
+
);
|
|
48
|
+
return url.href;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Requests carry cancellation, never an invented endpoint deadline. A returned
|
|
52
|
+
// JSON-RPC message completes an SSE operation even if the server keeps streaming.
|
|
53
|
+
async function message(response, id) {
|
|
54
|
+
if (!response.headers.get("content-type")?.includes("text/event-stream")) {
|
|
55
|
+
const value = await response.json();
|
|
56
|
+
if (value.id !== id)
|
|
57
|
+
throw new Error("the provider returned a different request");
|
|
58
|
+
return value;
|
|
59
|
+
}
|
|
60
|
+
const reader = response.body.getReader();
|
|
61
|
+
const decoder = new TextDecoder();
|
|
62
|
+
let buffer = "";
|
|
63
|
+
try {
|
|
64
|
+
while (true) {
|
|
65
|
+
const chunk = await reader.read();
|
|
66
|
+
buffer += decoder.decode(chunk.value, { stream: !chunk.done });
|
|
67
|
+
buffer = buffer.replace(/\r\n/g, "\n");
|
|
68
|
+
let boundary;
|
|
69
|
+
while (
|
|
70
|
+
(boundary = buffer.indexOf("\n\n")) >= 0 ||
|
|
71
|
+
(chunk.done && buffer)
|
|
72
|
+
) {
|
|
73
|
+
if (boundary < 0) boundary = buffer.length;
|
|
74
|
+
const event = buffer.slice(0, boundary);
|
|
75
|
+
buffer = buffer.slice(boundary + 2);
|
|
76
|
+
const data = event
|
|
77
|
+
.split("\n")
|
|
78
|
+
.filter((line) => line.startsWith("data:"))
|
|
79
|
+
.map((line) => line.slice(5).trimStart())
|
|
80
|
+
.join("\n");
|
|
81
|
+
if (!data) continue;
|
|
82
|
+
const value = JSON.parse(data);
|
|
83
|
+
if (value.id === id) return value;
|
|
84
|
+
}
|
|
85
|
+
if (chunk.done)
|
|
86
|
+
throw new Error("the provider stream ended without an answer");
|
|
87
|
+
}
|
|
88
|
+
} finally {
|
|
89
|
+
await reader.cancel().catch(() => {});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function transport(url, bearer, signal) {
|
|
94
|
+
let session;
|
|
95
|
+
let version;
|
|
96
|
+
let next = 0;
|
|
97
|
+
async function rpc(method, params, notification = false) {
|
|
98
|
+
const id = ++next;
|
|
99
|
+
const response = await fetch(url, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
redirect: "error",
|
|
102
|
+
signal,
|
|
103
|
+
headers: {
|
|
104
|
+
"content-type": "application/json",
|
|
105
|
+
accept: "application/json, text/event-stream",
|
|
106
|
+
...(bearer ? { authorization: `Bearer ${bearer}` } : {}),
|
|
107
|
+
...(session ? { "mcp-session-id": session } : {}),
|
|
108
|
+
...(version ? { "mcp-protocol-version": version } : {}),
|
|
109
|
+
},
|
|
110
|
+
body: JSON.stringify({
|
|
111
|
+
jsonrpc: "2.0",
|
|
112
|
+
...(notification ? {} : { id }),
|
|
113
|
+
method,
|
|
114
|
+
...(params ? { params } : {}),
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
await response.body?.cancel();
|
|
119
|
+
throw Object.assign(new Error(`provider HTTP ${response.status}`), {
|
|
120
|
+
answered: response.status >= 400 && response.status < 500,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
session = response.headers.get("mcp-session-id") || session;
|
|
124
|
+
if (notification) {
|
|
125
|
+
await response.body?.cancel();
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
const value = await message(response, id);
|
|
129
|
+
if (value.error)
|
|
130
|
+
throw Object.assign(new Error("the provider refused the operation"), {
|
|
131
|
+
answered: true,
|
|
132
|
+
});
|
|
133
|
+
return value.result;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
async initialize() {
|
|
137
|
+
const initialized = await rpc("initialize", {
|
|
138
|
+
protocolVersion: "2025-06-18",
|
|
139
|
+
capabilities: {},
|
|
140
|
+
clientInfo: { name: "harness-relay", version: "1" },
|
|
141
|
+
});
|
|
142
|
+
if (!["2025-06-18", "2025-03-26"].includes(initialized?.protocolVersion))
|
|
143
|
+
throw new Error("the provider selected an unsupported MCP protocol");
|
|
144
|
+
version = initialized.protocolVersion;
|
|
145
|
+
await rpc("notifications/initialized", null, true);
|
|
146
|
+
},
|
|
147
|
+
rpc,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Local connection ownership, explicit grants and durable effects behind one seam.
|
|
152
|
+
* Server hints never grant authority; an unknown effect is never replayed. */
|
|
153
|
+
// `onWaiting(operation, connection)`: an operation now waits for the person's approval, so they can be told (daemon.mjs).
|
|
154
|
+
export function createConnectors({
|
|
155
|
+
store,
|
|
156
|
+
directory,
|
|
157
|
+
log,
|
|
158
|
+
onWaiting = () => {},
|
|
159
|
+
}) {
|
|
160
|
+
const state = JSON.parse(
|
|
161
|
+
store.meta(STATE) || '{"connections":{},"operations":{}}'
|
|
162
|
+
);
|
|
163
|
+
const active = new Map();
|
|
164
|
+
const opening = new Map();
|
|
165
|
+
let closing = false;
|
|
166
|
+
const keyFile = path.join(directory, "connector-key");
|
|
167
|
+
const save = () => {
|
|
168
|
+
if (!closing) store.meta(STATE, JSON.stringify(state));
|
|
169
|
+
};
|
|
170
|
+
function key() {
|
|
171
|
+
if (!fs.existsSync(keyFile))
|
|
172
|
+
fs.writeFileSync(keyFile, randomBytes(32), { flag: "wx", mode: 0o600 });
|
|
173
|
+
return fs.readFileSync(keyFile);
|
|
174
|
+
}
|
|
175
|
+
function seal(id, credentials) {
|
|
176
|
+
const nonce = randomBytes(12);
|
|
177
|
+
const cipher = createCipheriv("aes-256-gcm", key(), nonce);
|
|
178
|
+
cipher.setAAD(Buffer.from(id));
|
|
179
|
+
const bytes = Buffer.concat([
|
|
180
|
+
cipher.update(JSON.stringify(credentials)),
|
|
181
|
+
cipher.final(),
|
|
182
|
+
]);
|
|
183
|
+
return Buffer.concat([nonce, cipher.getAuthTag(), bytes]).toString(
|
|
184
|
+
"base64"
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
function credentials(connection) {
|
|
188
|
+
const bytes = Buffer.from(connection.secret, "base64");
|
|
189
|
+
const cipher = createDecipheriv(
|
|
190
|
+
"aes-256-gcm",
|
|
191
|
+
key(),
|
|
192
|
+
bytes.subarray(0, 12)
|
|
193
|
+
);
|
|
194
|
+
cipher.setAAD(Buffer.from(connection.id));
|
|
195
|
+
cipher.setAuthTag(bytes.subarray(12, 28));
|
|
196
|
+
return JSON.parse(
|
|
197
|
+
Buffer.concat([
|
|
198
|
+
cipher.update(bytes.subarray(28)),
|
|
199
|
+
cipher.final(),
|
|
200
|
+
]).toString()
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
function publicConnection(connection, agent, hosted = false) {
|
|
204
|
+
const { secret: _secret, grants, ...visible } = connection;
|
|
205
|
+
const isOwner = agent === "user" && !hosted;
|
|
206
|
+
const allowed = grants.filter(
|
|
207
|
+
(grant) => grant.agent === agent && (!hosted || grant.hosted)
|
|
208
|
+
);
|
|
209
|
+
if (!isOwner) {
|
|
210
|
+
delete visible.authorizeUrl;
|
|
211
|
+
delete visible.url;
|
|
212
|
+
delete visible.resources;
|
|
213
|
+
visible.tools = visible.tools.filter((tool) =>
|
|
214
|
+
allowed.some(
|
|
215
|
+
(grant) =>
|
|
216
|
+
grant.tool === tool.name && grant.fingerprint === digest(tool)
|
|
217
|
+
)
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
...visible,
|
|
222
|
+
grants: isOwner ? grants : allowed,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const owner = (request, agent) => agent === "user" && !request.hostedCaller;
|
|
226
|
+
const eligible = (connection, agent, hosted) =>
|
|
227
|
+
connection.grants.some(
|
|
228
|
+
(grant) => grant.agent === agent && (!hosted || grant.hosted)
|
|
229
|
+
);
|
|
230
|
+
const definition = (connection, tool) =>
|
|
231
|
+
connection.tools.find((candidate) => candidate.name === tool);
|
|
232
|
+
function grantFor(connection, request, agent) {
|
|
233
|
+
if (connection.status !== "ready") return null;
|
|
234
|
+
const tool = definition(connection, request.tool);
|
|
235
|
+
if (!tool) return null;
|
|
236
|
+
return connection.grants.find(
|
|
237
|
+
(grant) =>
|
|
238
|
+
grant.agent === agent &&
|
|
239
|
+
grant.tool === request.tool &&
|
|
240
|
+
grant.fingerprint === digest(tool) &&
|
|
241
|
+
(!request.hostedCaller || grant.hosted) &&
|
|
242
|
+
(!grant.task || grant.task === request.task) &&
|
|
243
|
+
Object.entries(grant.arguments || {}).every(
|
|
244
|
+
([name, value]) =>
|
|
245
|
+
canonical(request.arguments?.[name]) === canonical(value)
|
|
246
|
+
)
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
function current(operation) {
|
|
250
|
+
const connection = state.connections[operation.connection];
|
|
251
|
+
if (!connection || connection.revision !== operation.revision) return false;
|
|
252
|
+
if (operation.task) {
|
|
253
|
+
const task = store.getTask(operation.task);
|
|
254
|
+
if (
|
|
255
|
+
!task ||
|
|
256
|
+
task.version !== operation.taskVersion ||
|
|
257
|
+
!["open", "claimed", "running", "needs_input"].includes(task.status)
|
|
258
|
+
)
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
return Boolean(grantFor(connection, operation, operation.agent));
|
|
262
|
+
}
|
|
263
|
+
function operationView(operation, agent, hosted) {
|
|
264
|
+
const connection = state.connections[operation.connection];
|
|
265
|
+
const visible = json(operation);
|
|
266
|
+
if (
|
|
267
|
+
(agent !== "user" || hosted) &&
|
|
268
|
+
(!connection ||
|
|
269
|
+
connection.revision !== operation.revision ||
|
|
270
|
+
!grantFor(connection, { ...operation, hostedCaller: hosted }, agent))
|
|
271
|
+
) {
|
|
272
|
+
delete visible.result;
|
|
273
|
+
delete visible.arguments;
|
|
274
|
+
delete visible.preview;
|
|
275
|
+
}
|
|
276
|
+
return visible;
|
|
277
|
+
}
|
|
278
|
+
async function openClient(connection, signal) {
|
|
279
|
+
if (signal.aborted || closing) throw new Error("connection cancelled");
|
|
280
|
+
const auth = credentials(connection);
|
|
281
|
+
if (
|
|
282
|
+
auth.refreshToken &&
|
|
283
|
+
(!auth.accessToken ||
|
|
284
|
+
(auth.expiresAt && Date.parse(auth.expiresAt) <= Date.now()))
|
|
285
|
+
) {
|
|
286
|
+
const response = await fetch(endpoint(auth.tokenEndpoint), {
|
|
287
|
+
method: "POST",
|
|
288
|
+
redirect: "error",
|
|
289
|
+
signal,
|
|
290
|
+
headers: {
|
|
291
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
292
|
+
...(auth.clientSecret && auth.authMethod === "client_secret_basic"
|
|
293
|
+
? {
|
|
294
|
+
authorization: `Basic ${Buffer.from(`${encodeURIComponent(auth.clientId)}:${encodeURIComponent(auth.clientSecret)}`).toString("base64")}`,
|
|
295
|
+
}
|
|
296
|
+
: {}),
|
|
297
|
+
},
|
|
298
|
+
body: new URLSearchParams({
|
|
299
|
+
grant_type: "refresh_token",
|
|
300
|
+
refresh_token: auth.refreshToken,
|
|
301
|
+
client_id: auth.clientId,
|
|
302
|
+
...(auth.resource ? { resource: auth.resource } : {}),
|
|
303
|
+
...(auth.clientSecret && auth.authMethod === "client_secret_post"
|
|
304
|
+
? { client_secret: auth.clientSecret }
|
|
305
|
+
: {}),
|
|
306
|
+
}),
|
|
307
|
+
});
|
|
308
|
+
if (!response.ok) {
|
|
309
|
+
await response.body?.cancel();
|
|
310
|
+
throw new Error("provider sign-in needs to be renewed");
|
|
311
|
+
}
|
|
312
|
+
const tokens = await response.json();
|
|
313
|
+
if (typeof tokens.access_token !== "string")
|
|
314
|
+
throw new Error("the provider returned no access token");
|
|
315
|
+
auth.accessToken = tokens.access_token;
|
|
316
|
+
auth.refreshToken = tokens.refresh_token || auth.refreshToken;
|
|
317
|
+
auth.expiresAt =
|
|
318
|
+
typeof tokens.expires_in === "number"
|
|
319
|
+
? new Date(Date.now() + tokens.expires_in * 1000).toISOString()
|
|
320
|
+
: null;
|
|
321
|
+
if (signal.aborted || connection.status === "revoked")
|
|
322
|
+
throw new Error("connection revoked");
|
|
323
|
+
connection.secret = seal(connection.id, auth);
|
|
324
|
+
save();
|
|
325
|
+
}
|
|
326
|
+
if (auth.expiresAt && Date.parse(auth.expiresAt) <= Date.now())
|
|
327
|
+
throw new Error("sign-in expired");
|
|
328
|
+
const remote = connection.provider
|
|
329
|
+
? createGoogleConnector({
|
|
330
|
+
provider: connection.provider,
|
|
331
|
+
url: connection.url,
|
|
332
|
+
bearer: auth.bearer || auth.accessToken,
|
|
333
|
+
scope: auth.grantedScope,
|
|
334
|
+
signal,
|
|
335
|
+
})
|
|
336
|
+
: transport(connection.url, auth.bearer || auth.accessToken, signal);
|
|
337
|
+
await remote.initialize();
|
|
338
|
+
return remote;
|
|
339
|
+
}
|
|
340
|
+
function client(connection, signal) {
|
|
341
|
+
// Refresh tokens can rotate. Serialize sign-in/handshake for one account so
|
|
342
|
+
// two agents never race the same refresh token; their tool calls stay independent.
|
|
343
|
+
const previous = opening.get(connection.id) || Promise.resolve();
|
|
344
|
+
const next = previous
|
|
345
|
+
.catch(() => {})
|
|
346
|
+
.then(() => openClient(connection, signal));
|
|
347
|
+
opening.set(connection.id, next);
|
|
348
|
+
return next.finally(() => {
|
|
349
|
+
if (opening.get(connection.id) === next) opening.delete(connection.id);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
function launch(id, work) {
|
|
353
|
+
const controller = new AbortController();
|
|
354
|
+
active.set(id, controller);
|
|
355
|
+
void Promise.resolve()
|
|
356
|
+
.then(() => work(controller.signal))
|
|
357
|
+
.catch(() => {
|
|
358
|
+
log("a connector background operation stopped");
|
|
359
|
+
})
|
|
360
|
+
.finally(() => {
|
|
361
|
+
if (active.get(id) === controller) active.delete(id);
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
function invalidate(id) {
|
|
365
|
+
active.get(`discovery:${id}`)?.abort();
|
|
366
|
+
for (const operation of Object.values(state.operations)) {
|
|
367
|
+
if (operation.connection !== id || terminal.has(operation.status))
|
|
368
|
+
continue;
|
|
369
|
+
active.get(`operation:${operation.id}`)?.abort();
|
|
370
|
+
operation.status = operation.dispatchedAt ? "unknown" : "cancelled";
|
|
371
|
+
operation.finishedAt = new Date().toISOString();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
function discover(connection) {
|
|
375
|
+
const revision = connection.revision;
|
|
376
|
+
launch(`discovery:${connection.id}`, async (signal) => {
|
|
377
|
+
try {
|
|
378
|
+
const remote = await client(connection, signal);
|
|
379
|
+
const tools = [];
|
|
380
|
+
const seen = new Set();
|
|
381
|
+
let cursor;
|
|
382
|
+
do {
|
|
383
|
+
const listed = await remote.rpc(
|
|
384
|
+
"tools/list",
|
|
385
|
+
cursor ? { cursor } : {}
|
|
386
|
+
);
|
|
387
|
+
if (!Array.isArray(listed?.tools))
|
|
388
|
+
throw new Error("the provider returned no tool catalog");
|
|
389
|
+
tools.push(...listed.tools);
|
|
390
|
+
cursor = listed.nextCursor;
|
|
391
|
+
if (cursor && seen.has(cursor))
|
|
392
|
+
throw new Error("the provider repeated a catalog page");
|
|
393
|
+
seen.add(cursor);
|
|
394
|
+
} while (cursor);
|
|
395
|
+
if (connection.revision !== revision || signal.aborted) return;
|
|
396
|
+
const definitions = tools.filter(
|
|
397
|
+
(tool) => typeof tool.name === "string" && object(tool.inputSchema)
|
|
398
|
+
);
|
|
399
|
+
const identity = remote.identity
|
|
400
|
+
? await remote.identity()
|
|
401
|
+
: connection.identity;
|
|
402
|
+
const resources = remote.resources
|
|
403
|
+
? await remote.resources()
|
|
404
|
+
: undefined;
|
|
405
|
+
if (connection.revision !== revision || signal.aborted) return;
|
|
406
|
+
connection.tools = definitions;
|
|
407
|
+
if (identity) connection.identity = identity;
|
|
408
|
+
if (resources) connection.resources = resources;
|
|
409
|
+
connection.status = "ready";
|
|
410
|
+
connection.error = null;
|
|
411
|
+
} catch {
|
|
412
|
+
if (connection.revision !== revision || signal.aborted) return;
|
|
413
|
+
connection.status = "unavailable";
|
|
414
|
+
connection.error =
|
|
415
|
+
"Could not discover tools. Check the connection and sign-in.";
|
|
416
|
+
}
|
|
417
|
+
save();
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
function execute(operation) {
|
|
421
|
+
launch(`operation:${operation.id}`, async (signal) => {
|
|
422
|
+
if (!current(operation)) {
|
|
423
|
+
operation.status = "cancelled";
|
|
424
|
+
save();
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
const connection = state.connections[operation.connection];
|
|
428
|
+
let dispatched = false;
|
|
429
|
+
try {
|
|
430
|
+
operation.status = "running";
|
|
431
|
+
save();
|
|
432
|
+
const remote = await client(connection, signal);
|
|
433
|
+
if (!current(operation) || signal.aborted)
|
|
434
|
+
throw new Error("authority changed");
|
|
435
|
+
operation.dispatchedAt = new Date().toISOString();
|
|
436
|
+
save();
|
|
437
|
+
dispatched = true;
|
|
438
|
+
const result = await remote.rpc("tools/call", {
|
|
439
|
+
name: operation.tool,
|
|
440
|
+
arguments: operation.arguments,
|
|
441
|
+
});
|
|
442
|
+
if (!current(operation) || signal.aborted)
|
|
443
|
+
throw new Error("authority changed while the provider was working");
|
|
444
|
+
if (!object(result) || !Array.isArray(result.content))
|
|
445
|
+
throw new Error("unreadable provider result");
|
|
446
|
+
const auth = credentials(connection);
|
|
447
|
+
const secrets = [
|
|
448
|
+
auth.bearer,
|
|
449
|
+
auth.accessToken,
|
|
450
|
+
auth.refreshToken,
|
|
451
|
+
auth.clientSecret,
|
|
452
|
+
].filter((value) => typeof value === "string" && value.length);
|
|
453
|
+
const safe = (value) => {
|
|
454
|
+
if (typeof value === "string")
|
|
455
|
+
return secrets.reduce(
|
|
456
|
+
(text, secret) =>
|
|
457
|
+
text.replaceAll(secret, "[credential withheld]"),
|
|
458
|
+
value
|
|
459
|
+
);
|
|
460
|
+
if (Array.isArray(value)) return value.map(safe);
|
|
461
|
+
if (object(value))
|
|
462
|
+
return Object.fromEntries(
|
|
463
|
+
Object.entries(value).map(([name, entry]) => [
|
|
464
|
+
safe(name),
|
|
465
|
+
safe(entry),
|
|
466
|
+
])
|
|
467
|
+
);
|
|
468
|
+
return value;
|
|
469
|
+
};
|
|
470
|
+
operation.result = safe(result);
|
|
471
|
+
operation.status = result?.isError ? "failed" : "returned";
|
|
472
|
+
} catch (error) {
|
|
473
|
+
operation.status =
|
|
474
|
+
dispatched && !error.answered
|
|
475
|
+
? "unknown"
|
|
476
|
+
: signal.aborted
|
|
477
|
+
? "cancelled"
|
|
478
|
+
: "failed";
|
|
479
|
+
operation.error =
|
|
480
|
+
operation.status === "unknown"
|
|
481
|
+
? "The provider may have acted. Reconcile the destination before retrying."
|
|
482
|
+
: "The operation did not complete. Check access and sign-in.";
|
|
483
|
+
}
|
|
484
|
+
operation.finishedAt = new Date().toISOString();
|
|
485
|
+
save();
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
for (const operation of Object.values(state.operations)) {
|
|
489
|
+
if (operation.status === "running")
|
|
490
|
+
operation.status = operation.dispatchedAt ? "unknown" : "cancelled";
|
|
491
|
+
if (operation.status === "queued") operation.status = "cancelled";
|
|
492
|
+
}
|
|
493
|
+
for (const connection of Object.values(state.connections)) {
|
|
494
|
+
if (connection.status === "revoked") {
|
|
495
|
+
delete connection.secret;
|
|
496
|
+
delete connection.authorizeUrl;
|
|
497
|
+
connection.grants = [];
|
|
498
|
+
}
|
|
499
|
+
if (["authorizing", "sign_in", "discovering"].includes(connection.status)) {
|
|
500
|
+
connection.status = "unavailable";
|
|
501
|
+
connection.error =
|
|
502
|
+
"Connection setup was interrupted. Refresh discovery or sign in again.";
|
|
503
|
+
delete connection.authorizeUrl;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
save();
|
|
507
|
+
return {
|
|
508
|
+
command(request, agent) {
|
|
509
|
+
const action = request.command.slice("board.connector.".length);
|
|
510
|
+
const id = String(request.id || "");
|
|
511
|
+
const connection = state.connections[id];
|
|
512
|
+
const isOwner = owner(request, agent);
|
|
513
|
+
if (
|
|
514
|
+
["add", "reconnect", "grant", "revoke", "decide", "refresh"].includes(
|
|
515
|
+
action
|
|
516
|
+
) &&
|
|
517
|
+
!isOwner
|
|
518
|
+
)
|
|
519
|
+
return refuse(
|
|
520
|
+
"only the person on this computer manages connections and approvals"
|
|
521
|
+
);
|
|
522
|
+
if (action === "operations")
|
|
523
|
+
return isOwner
|
|
524
|
+
? {
|
|
525
|
+
ok: true,
|
|
526
|
+
operations: Object.values(state.operations).map((operation) =>
|
|
527
|
+
operationView(operation, agent, false)
|
|
528
|
+
),
|
|
529
|
+
}
|
|
530
|
+
: refuse("only the person reviews connection operations");
|
|
531
|
+
if (action === "list")
|
|
532
|
+
return {
|
|
533
|
+
ok: true,
|
|
534
|
+
connections: Object.values(state.connections)
|
|
535
|
+
.filter(
|
|
536
|
+
(entry) => isOwner || eligible(entry, agent, request.hostedCaller)
|
|
537
|
+
)
|
|
538
|
+
.map((entry) =>
|
|
539
|
+
publicConnection(entry, agent, request.hostedCaller)
|
|
540
|
+
),
|
|
541
|
+
};
|
|
542
|
+
if (action === "add" || action === "reconnect") {
|
|
543
|
+
if (action === "reconnect" && !connection)
|
|
544
|
+
return refuse("connection not found");
|
|
545
|
+
if (
|
|
546
|
+
action === "reconnect" &&
|
|
547
|
+
request.revision !== undefined &&
|
|
548
|
+
request.revision !== connection.revision
|
|
549
|
+
)
|
|
550
|
+
return refuse("the connection changed; refresh before trying again");
|
|
551
|
+
const label = action === "reconnect" ? connection.label : request.label;
|
|
552
|
+
if (!label || !object(request.credentials || {}))
|
|
553
|
+
return refuse("a connection needs a name and credentials object");
|
|
554
|
+
let url;
|
|
555
|
+
try {
|
|
556
|
+
url = endpoint(action === "reconnect" ? connection.url : request.url);
|
|
557
|
+
} catch (error) {
|
|
558
|
+
return refuse(error.message);
|
|
559
|
+
}
|
|
560
|
+
if (action === "reconnect") invalidate(id);
|
|
561
|
+
// A new sign-in may select a different account. Old permissions must not
|
|
562
|
+
// follow it; the person grants access again after discovery.
|
|
563
|
+
const saved = {
|
|
564
|
+
id: action === "reconnect" ? id : randomUUID(),
|
|
565
|
+
label: String(label),
|
|
566
|
+
url,
|
|
567
|
+
provider: googleProvider(
|
|
568
|
+
url,
|
|
569
|
+
action === "reconnect" ? connection.provider : request.provider
|
|
570
|
+
),
|
|
571
|
+
account: String(request.account || connection?.account || ""),
|
|
572
|
+
revision: action === "reconnect" ? connection.revision + 1 : 1,
|
|
573
|
+
createdAt: connection?.createdAt || new Date().toISOString(),
|
|
574
|
+
grants: [],
|
|
575
|
+
tools: [],
|
|
576
|
+
status: "discovering",
|
|
577
|
+
};
|
|
578
|
+
saved.secret = seal(saved.id, request.credentials || {});
|
|
579
|
+
state.connections[saved.id] = saved;
|
|
580
|
+
save();
|
|
581
|
+
if (request.signIn === true) {
|
|
582
|
+
saved.status = "authorizing";
|
|
583
|
+
launch(`discovery:${saved.id}`, async (signal) => {
|
|
584
|
+
try {
|
|
585
|
+
const signIn = await authorizeConnector({
|
|
586
|
+
...request.oauth,
|
|
587
|
+
url,
|
|
588
|
+
signal,
|
|
589
|
+
...(saved.provider && new URL(url).protocol === "https:"
|
|
590
|
+
? {
|
|
591
|
+
metadata: GOOGLE_OAUTH,
|
|
592
|
+
resource: false,
|
|
593
|
+
offline: true,
|
|
594
|
+
clientId:
|
|
595
|
+
request.oauth?.clientId ||
|
|
596
|
+
process.env.HARNESS_GOOGLE_CLIENT_ID,
|
|
597
|
+
clientSecret:
|
|
598
|
+
request.oauth?.clientSecret ||
|
|
599
|
+
process.env.HARNESS_GOOGLE_CLIENT_SECRET,
|
|
600
|
+
}
|
|
601
|
+
: {}),
|
|
602
|
+
});
|
|
603
|
+
saved.authorizeUrl = signIn.authorizeUrl;
|
|
604
|
+
saved.status = "sign_in";
|
|
605
|
+
save();
|
|
606
|
+
const auth = await signIn.completion;
|
|
607
|
+
if (signal.aborted || saved.status === "revoked") return;
|
|
608
|
+
saved.secret = seal(saved.id, auth);
|
|
609
|
+
saved.scope = auth.grantedScope;
|
|
610
|
+
if (auth.identity) saved.identity = auth.identity;
|
|
611
|
+
delete saved.authorizeUrl;
|
|
612
|
+
saved.status = "discovering";
|
|
613
|
+
save();
|
|
614
|
+
discover(saved);
|
|
615
|
+
} catch {
|
|
616
|
+
if (signal.aborted) return;
|
|
617
|
+
delete saved.authorizeUrl;
|
|
618
|
+
saved.status = "unavailable";
|
|
619
|
+
saved.error =
|
|
620
|
+
"Sign-in did not complete. This provider may require its own registered OAuth app.";
|
|
621
|
+
save();
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
} else discover(saved);
|
|
625
|
+
return { ok: true, connection: publicConnection(saved, agent) };
|
|
626
|
+
}
|
|
627
|
+
if (["result", "decide"].includes(action)) {
|
|
628
|
+
const operation = state.operations[id];
|
|
629
|
+
if (!operation || (!isOwner && operation.agent !== agent))
|
|
630
|
+
return refuse("operation not found");
|
|
631
|
+
if (action === "decide") {
|
|
632
|
+
if (operation.status !== "needs_approval" || !current(operation))
|
|
633
|
+
return refuse("the operation or its authority has changed");
|
|
634
|
+
operation.status = request.approve === true ? "queued" : "cancelled";
|
|
635
|
+
save();
|
|
636
|
+
if (request.approve === true) execute(operation);
|
|
637
|
+
}
|
|
638
|
+
return {
|
|
639
|
+
ok: true,
|
|
640
|
+
operation: operationView(operation, agent, request.hostedCaller),
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
if (
|
|
644
|
+
!connection ||
|
|
645
|
+
(!isOwner && !eligible(connection, agent, request.hostedCaller))
|
|
646
|
+
)
|
|
647
|
+
return refuse("connection not found or not granted to this agent");
|
|
648
|
+
if (action === "tools")
|
|
649
|
+
return {
|
|
650
|
+
ok: true,
|
|
651
|
+
connection: publicConnection(connection, agent, request.hostedCaller),
|
|
652
|
+
tools: connection.tools.filter(
|
|
653
|
+
(tool) =>
|
|
654
|
+
isOwner ||
|
|
655
|
+
connection.grants.some(
|
|
656
|
+
(grant) =>
|
|
657
|
+
grant.agent === agent &&
|
|
658
|
+
grant.tool === tool.name &&
|
|
659
|
+
grant.fingerprint === digest(tool) &&
|
|
660
|
+
(!request.hostedCaller || grant.hosted)
|
|
661
|
+
)
|
|
662
|
+
),
|
|
663
|
+
};
|
|
664
|
+
if (action === "grant") {
|
|
665
|
+
if (connection.status !== "ready")
|
|
666
|
+
return refuse(
|
|
667
|
+
"sign in and discover operations before granting access"
|
|
668
|
+
);
|
|
669
|
+
if (
|
|
670
|
+
request.revision !== undefined &&
|
|
671
|
+
request.revision !== connection.revision
|
|
672
|
+
)
|
|
673
|
+
return refuse(
|
|
674
|
+
"agent access changed in another window; refresh before saving"
|
|
675
|
+
);
|
|
676
|
+
if (
|
|
677
|
+
!Array.isArray(request.grants) ||
|
|
678
|
+
request.grants.some(
|
|
679
|
+
(grant) =>
|
|
680
|
+
!object(grant) ||
|
|
681
|
+
!grant.agent ||
|
|
682
|
+
!definition(connection, grant.tool) ||
|
|
683
|
+
!["read", "write", "send", "delete"].includes(grant.effect) ||
|
|
684
|
+
(connection.provider &&
|
|
685
|
+
definition(connection, grant.tool).effect !== grant.effect) ||
|
|
686
|
+
(grant.arguments && !object(grant.arguments))
|
|
687
|
+
)
|
|
688
|
+
)
|
|
689
|
+
return refuse(
|
|
690
|
+
"each grant needs an agent, a discovered tool and an explicit effect"
|
|
691
|
+
);
|
|
692
|
+
connection.grants = request.grants.map((grant) => ({
|
|
693
|
+
agent: String(grant.agent),
|
|
694
|
+
tool: String(grant.tool),
|
|
695
|
+
effect: grant.effect,
|
|
696
|
+
hosted: grant.hosted === true,
|
|
697
|
+
standing: grant.standing === true,
|
|
698
|
+
arguments: grant.arguments || {},
|
|
699
|
+
task: grant.task || null,
|
|
700
|
+
fingerprint: digest(definition(connection, grant.tool)),
|
|
701
|
+
}));
|
|
702
|
+
connection.revision++;
|
|
703
|
+
invalidate(id);
|
|
704
|
+
save();
|
|
705
|
+
return { ok: true, connection: publicConnection(connection, agent) };
|
|
706
|
+
}
|
|
707
|
+
if (action === "revoke" || action === "refresh") {
|
|
708
|
+
if (action === "refresh" && connection.status === "revoked")
|
|
709
|
+
return refuse(
|
|
710
|
+
"this account is disconnected; sign in again to reconnect"
|
|
711
|
+
);
|
|
712
|
+
connection.revision++;
|
|
713
|
+
connection.status = action === "revoke" ? "revoked" : "discovering";
|
|
714
|
+
if (action === "revoke") {
|
|
715
|
+
delete connection.secret;
|
|
716
|
+
delete connection.authorizeUrl;
|
|
717
|
+
delete connection.scope;
|
|
718
|
+
connection.grants = [];
|
|
719
|
+
connection.error = null;
|
|
720
|
+
}
|
|
721
|
+
invalidate(id);
|
|
722
|
+
save();
|
|
723
|
+
if (action === "refresh") discover(connection);
|
|
724
|
+
return { ok: true, connection: publicConnection(connection, agent) };
|
|
725
|
+
}
|
|
726
|
+
if (action === "call") {
|
|
727
|
+
if (!object(request.arguments || {}))
|
|
728
|
+
return refuse("tool arguments must be an object");
|
|
729
|
+
if (connection.provider) {
|
|
730
|
+
try {
|
|
731
|
+
googleOperation(
|
|
732
|
+
connection.provider,
|
|
733
|
+
request.tool,
|
|
734
|
+
request.arguments || {}
|
|
735
|
+
);
|
|
736
|
+
} catch (error) {
|
|
737
|
+
return refuse(error.message);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
const grant = grantFor(connection, request, agent);
|
|
741
|
+
if (!grant)
|
|
742
|
+
return refuse(
|
|
743
|
+
"the operation, resource or disclosure is outside this agent's grant"
|
|
744
|
+
);
|
|
745
|
+
const operationId = String(request.requestId || randomUUID());
|
|
746
|
+
const operation = {
|
|
747
|
+
id: operationId,
|
|
748
|
+
connection: id,
|
|
749
|
+
agent,
|
|
750
|
+
hostedCaller: request.hostedCaller === true,
|
|
751
|
+
tool: request.tool,
|
|
752
|
+
arguments: json(request.arguments || {}),
|
|
753
|
+
task: request.task || null,
|
|
754
|
+
taskVersion: request.task
|
|
755
|
+
? store.getTask(request.task)?.version
|
|
756
|
+
: null,
|
|
757
|
+
revision: connection.revision,
|
|
758
|
+
effect: grant.effect,
|
|
759
|
+
};
|
|
760
|
+
if (connection.provider) {
|
|
761
|
+
const planned = googleOperation(
|
|
762
|
+
connection.provider,
|
|
763
|
+
request.tool,
|
|
764
|
+
operation.arguments
|
|
765
|
+
);
|
|
766
|
+
if (planned.preview) operation.preview = planned.preview;
|
|
767
|
+
}
|
|
768
|
+
const fingerprint = digest(operation);
|
|
769
|
+
const prior = state.operations[operationId];
|
|
770
|
+
if (prior)
|
|
771
|
+
return prior.fingerprint === fingerprint
|
|
772
|
+
? {
|
|
773
|
+
ok: true,
|
|
774
|
+
operation: operationView(prior, agent, request.hostedCaller),
|
|
775
|
+
}
|
|
776
|
+
: refuse(
|
|
777
|
+
"request id already belongs to different content or authority"
|
|
778
|
+
);
|
|
779
|
+
if (!current(operation))
|
|
780
|
+
return refuse("task or connection authority is no longer current");
|
|
781
|
+
Object.assign(operation, {
|
|
782
|
+
fingerprint,
|
|
783
|
+
createdAt: new Date().toISOString(),
|
|
784
|
+
status:
|
|
785
|
+
grant.effect === "read" || grant.standing
|
|
786
|
+
? "queued"
|
|
787
|
+
: "needs_approval",
|
|
788
|
+
});
|
|
789
|
+
state.operations[operationId] = operation;
|
|
790
|
+
save();
|
|
791
|
+
if (operation.status === "queued") execute(operation);
|
|
792
|
+
else onWaiting(operation, connection);
|
|
793
|
+
return {
|
|
794
|
+
ok: true,
|
|
795
|
+
operation: operationView(operation, agent, request.hostedCaller),
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
return refuse("unknown connection operation");
|
|
799
|
+
},
|
|
800
|
+
close() {
|
|
801
|
+
for (const controller of active.values()) controller.abort();
|
|
802
|
+
closing = true;
|
|
803
|
+
},
|
|
804
|
+
};
|
|
805
|
+
}
|