@harness-gw/sdk 0.16.1 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/dist/chunks/index.js +42 -12
- package/dist/client/acp-workspace-client-composer.d.ts +1 -1
- package/dist/client/acp-workspace-client-sessions.d.ts +1 -1
- package/dist/client/acp-workspace-client.d.ts +10 -4
- package/dist/client/connection.d.ts +9 -2
- package/dist/client/hgw-client.d.ts +9 -3
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +183 -48
- package/dist/client/log.d.ts +2 -2
- package/dist/client/tolerant.d.ts +15 -0
- package/dist/client/types.d.ts +44 -4
- package/dist/client/workspace.d.ts +13 -0
- package/dist/protocol/acp.d.ts +182 -2
- package/dist/protocol/mcp-apps.d.ts +26 -10
- package/dist/protocol/push.d.ts +83 -9
- package/dist/protocol/sdk.js +12 -0
- package/package.json +6 -6
package/dist/client/index.js
CHANGED
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
HGW_REPLAY_BEFORE2,
|
|
31
31
|
HgwHistoryPageResponseMetaSchema2,
|
|
32
32
|
HgwHistoryPageTagSchema2,
|
|
33
|
+
HgwSessionSetupResponseMetaSchema2,
|
|
33
34
|
HgwSessionResumeResponseMetaSchema2,
|
|
34
35
|
HgwSteerResponseSchema2,
|
|
35
36
|
HgwAgentsListResponseSchema2,
|
|
@@ -54,7 +55,7 @@ import {
|
|
|
54
55
|
methods as methods2,
|
|
55
56
|
RequestError,
|
|
56
57
|
SessionUpdate
|
|
57
|
-
} from "@agentclientprotocol/sdk/experimental/v2";
|
|
58
|
+
} from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
58
59
|
import {
|
|
59
60
|
createWebSocketStream
|
|
60
61
|
} from "@agentclientprotocol/sdk/experimental/ws-client";
|
|
@@ -288,6 +289,17 @@ class AcpConnectionClosedError extends Error {
|
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
291
|
|
|
292
|
+
class HgwVersionMismatchError extends Error {
|
|
293
|
+
gatewayVersion;
|
|
294
|
+
clientVersion;
|
|
295
|
+
constructor(gatewayVersion, clientVersion) {
|
|
296
|
+
super(`The gateway speaks hgw extension version ${String(gatewayVersion)}; this client speaks version ${clientVersion}`);
|
|
297
|
+
this.gatewayVersion = gatewayVersion;
|
|
298
|
+
this.clientVersion = clientVersion;
|
|
299
|
+
this.name = "HgwVersionMismatchError";
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
291
303
|
// client/limits.ts
|
|
292
304
|
var HANDSHAKE_DEADLINE_MS = 1e4;
|
|
293
305
|
var REQUEST_DEADLINE_MS = {
|
|
@@ -305,7 +317,7 @@ var CAPACITY_BACKOFF = { minMs: 30000, maxMs: 60000 };
|
|
|
305
317
|
// client/log.ts
|
|
306
318
|
import {
|
|
307
319
|
methods
|
|
308
|
-
} from "@agentclientprotocol/sdk/experimental/v2";
|
|
320
|
+
} from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
309
321
|
import pino from "pino/browser.js";
|
|
310
322
|
var DEBUG_PARAM = "debug";
|
|
311
323
|
var DEBUG_VALUE = "acp";
|
|
@@ -359,10 +371,7 @@ function frameFields(direction, message) {
|
|
|
359
371
|
};
|
|
360
372
|
}
|
|
361
373
|
function loggedStream(stream, logger) {
|
|
362
|
-
const log = (direction, frame) =>
|
|
363
|
-
for (const message of [frame].flat())
|
|
364
|
-
logger.debug(frameFields(direction, message), "acp.frame");
|
|
365
|
-
};
|
|
374
|
+
const log = (direction, frame) => logger.debug(frameFields(direction, frame), "acp.frame");
|
|
366
375
|
const writer = stream.writable.getWriter();
|
|
367
376
|
return {
|
|
368
377
|
readable: stream.readable.pipeThrough(new TransformStream({
|
|
@@ -397,6 +406,58 @@ function callEach(listeners, logger, event, ...args) {
|
|
|
397
406
|
}
|
|
398
407
|
}
|
|
399
408
|
|
|
409
|
+
// client/tolerant.ts
|
|
410
|
+
var MAX_PASSES = 8;
|
|
411
|
+
function unknownKeys(issues, prefix = []) {
|
|
412
|
+
const found = [];
|
|
413
|
+
for (const issue of issues) {
|
|
414
|
+
const path = [...prefix, ...issue.path];
|
|
415
|
+
if (issue.code === "unrecognized_keys")
|
|
416
|
+
found.push(...issue.keys.map((key) => [...path, key]));
|
|
417
|
+
else if (issue.code === "invalid_union") {
|
|
418
|
+
const branch = issue.errors.map((branchIssues) => unknownKeys(branchIssues, path)).find((keys) => keys !== undefined && keys.length > 0);
|
|
419
|
+
if (branch === undefined)
|
|
420
|
+
return;
|
|
421
|
+
found.push(...branch);
|
|
422
|
+
} else
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
return found;
|
|
426
|
+
}
|
|
427
|
+
function withoutKey(value, path) {
|
|
428
|
+
const [head, ...rest] = path;
|
|
429
|
+
if (head === undefined || typeof value !== "object" || value === null)
|
|
430
|
+
return value;
|
|
431
|
+
const copy = Array.isArray(value) ? [...value] : { ...value };
|
|
432
|
+
if (rest.length === 0)
|
|
433
|
+
delete copy[head];
|
|
434
|
+
else
|
|
435
|
+
copy[head] = withoutKey(copy[head], rest);
|
|
436
|
+
return copy;
|
|
437
|
+
}
|
|
438
|
+
function tolerant(schema) {
|
|
439
|
+
const safeParse = (value) => {
|
|
440
|
+
let result = schema.safeParse(value);
|
|
441
|
+
for (let pass = 0;!result.success && pass < MAX_PASSES; pass += 1) {
|
|
442
|
+
const unknown = unknownKeys(result.error.issues);
|
|
443
|
+
if (!unknown?.length)
|
|
444
|
+
break;
|
|
445
|
+
value = unknown.reduce(withoutKey, value);
|
|
446
|
+
result = schema.safeParse(value);
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
};
|
|
450
|
+
return {
|
|
451
|
+
safeParse,
|
|
452
|
+
parse(value) {
|
|
453
|
+
const result = safeParse(value);
|
|
454
|
+
if (result.success)
|
|
455
|
+
return result.data;
|
|
456
|
+
throw result.error;
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
400
461
|
// client/connection.ts
|
|
401
462
|
var TRY_AGAIN_LATER = 1013;
|
|
402
463
|
var POLICY_VIOLATION = 1008;
|
|
@@ -457,6 +518,25 @@ function hgwMetaOf(meta) {
|
|
|
457
518
|
const parsed = HgwEnvelopeSchema.safeParse(meta);
|
|
458
519
|
return parsed.success ? parsed.data[HGW_META_KEY2] : undefined;
|
|
459
520
|
}
|
|
521
|
+
function setupOfUpdate(update, meta) {
|
|
522
|
+
if (SessionUpdate.isConfigOptionUpdate(update))
|
|
523
|
+
return { configOptions: update.configOptions };
|
|
524
|
+
if (!SessionUpdate.isAvailableCommandsUpdate(update))
|
|
525
|
+
return;
|
|
526
|
+
const capabilities = tolerant(HgwAvailableCommandsMetaSchema2).safeParse(meta).data?.capabilities;
|
|
527
|
+
return {
|
|
528
|
+
availableCommands: update.availableCommands,
|
|
529
|
+
...capabilities ? { capabilities } : {}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function setupOfAnswer(answer) {
|
|
533
|
+
const capabilities = tolerant(HgwSessionSetupResponseMetaSchema2).safeParse(hgwMetaOf(answer._meta)).data?.capabilities;
|
|
534
|
+
return {
|
|
535
|
+
configOptions: answer.configOptions ?? [],
|
|
536
|
+
availableCommands: answer.availableCommands ?? [],
|
|
537
|
+
...capabilities ? { capabilities } : {}
|
|
538
|
+
};
|
|
539
|
+
}
|
|
460
540
|
function acpSocketUrl(path, base) {
|
|
461
541
|
const url = new URL(path, base);
|
|
462
542
|
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
@@ -673,7 +753,7 @@ function readEach(entries, schema, skipped) {
|
|
|
673
753
|
const kept = [];
|
|
674
754
|
let unreadable;
|
|
675
755
|
for (const entry of entries) {
|
|
676
|
-
const read = schema.safeParse(entry);
|
|
756
|
+
const read = tolerant(schema).safeParse(entry);
|
|
677
757
|
if (read.success)
|
|
678
758
|
kept.push(read.data);
|
|
679
759
|
else {
|
|
@@ -716,6 +796,7 @@ function createAcpConnection(options) {
|
|
|
716
796
|
let agentCapabilities;
|
|
717
797
|
let status = "connecting";
|
|
718
798
|
let outage;
|
|
799
|
+
let versionMismatch;
|
|
719
800
|
let started = false;
|
|
720
801
|
let inspecting;
|
|
721
802
|
let ended = false;
|
|
@@ -847,6 +928,9 @@ function createAcpConnection(options) {
|
|
|
847
928
|
deliverChild(open, params.sessionId, params.update, meta);
|
|
848
929
|
return;
|
|
849
930
|
}
|
|
931
|
+
const reported = setupOfUpdate(params.update, meta);
|
|
932
|
+
if (reported)
|
|
933
|
+
report(open, reported);
|
|
850
934
|
for (const { listener } of open.listeners)
|
|
851
935
|
callEach(listener.update ? [listener.update] : [], logger, "acp.listener.failed", params.update, meta);
|
|
852
936
|
}).onRequest(methods2.client.session.requestPermission, ({ params, signal }) => permissionRequest(params, signal)).onRequest(methods2.client.elicitation.create, ({ params, signal }) => elicitationRequest(params, signal));
|
|
@@ -942,7 +1026,8 @@ function createAcpConnection(options) {
|
|
|
942
1026
|
const announced = hgwMetaOf(response._meta);
|
|
943
1027
|
agentCapabilities = response.capabilities ?? undefined;
|
|
944
1028
|
if (announced?.version !== HGW_EXTENSION_VERSION2) {
|
|
945
|
-
const error = new
|
|
1029
|
+
const error = new HgwVersionMismatchError(typeof announced?.version === "number" ? announced.version : undefined, HGW_EXTENSION_VERSION2);
|
|
1030
|
+
versionMismatch = error;
|
|
946
1031
|
failInitialized?.(error);
|
|
947
1032
|
failInitialized = undefined;
|
|
948
1033
|
settleInitialized = undefined;
|
|
@@ -1002,6 +1087,7 @@ function createAcpConnection(options) {
|
|
|
1002
1087
|
owners.set(sessionId, agentId);
|
|
1003
1088
|
if (meta.history)
|
|
1004
1089
|
open.history = meta.history;
|
|
1090
|
+
hand(open, report(open, setupOfAnswer(response)));
|
|
1005
1091
|
if (fromStart) {
|
|
1006
1092
|
open.replayed = true;
|
|
1007
1093
|
replayed = true;
|
|
@@ -1016,7 +1102,7 @@ function createAcpConnection(options) {
|
|
|
1016
1102
|
}
|
|
1017
1103
|
}
|
|
1018
1104
|
async function listAgents() {
|
|
1019
|
-
const response = AgentsListEntriesSchema.parse(await request("short", (agent, options) => agent.request(HGW_METHODS2.agents.list, undefined, options), "read"));
|
|
1105
|
+
const response = tolerant(AgentsListEntriesSchema).parse(await request("short", (agent, options) => agent.request(HGW_METHODS2.agents.list, undefined, options), "read"));
|
|
1020
1106
|
return {
|
|
1021
1107
|
...response,
|
|
1022
1108
|
agents: readEach(response.agents, HgwAgentsListResponseSchema2.shape.agents.element, (entry) => logger.warn({ id: loggedId(entry, "summary", "id") }, "agents.entry_skipped"))
|
|
@@ -1026,6 +1112,16 @@ function createAcpConnection(options) {
|
|
|
1026
1112
|
const settle = listener.replay?.();
|
|
1027
1113
|
return settle ? [settle] : [];
|
|
1028
1114
|
}
|
|
1115
|
+
function report(open, reported) {
|
|
1116
|
+
open.setup = { ...open.setup, ...reported };
|
|
1117
|
+
return reported;
|
|
1118
|
+
}
|
|
1119
|
+
function hand(open, setup, listeners = open.listeners) {
|
|
1120
|
+
if (Object.keys(setup).length === 0)
|
|
1121
|
+
return;
|
|
1122
|
+
for (const { listener } of listeners)
|
|
1123
|
+
callEach(listener.setup ? [listener.setup] : [], logger, "acp.listener.failed", setup);
|
|
1124
|
+
}
|
|
1029
1125
|
function notOpen() {
|
|
1030
1126
|
return new Error("The Session is not open on this ACP connection");
|
|
1031
1127
|
}
|
|
@@ -1089,6 +1185,7 @@ function createAcpConnection(options) {
|
|
|
1089
1185
|
known.listeners.add(subscription);
|
|
1090
1186
|
if (known.replaying && !known.replaying.delivered)
|
|
1091
1187
|
known.replaying.settles.push(...settleOf(subscription));
|
|
1188
|
+
hand(known, known.setup, [subscription]);
|
|
1092
1189
|
return () => leave(sessionId, known, subscription);
|
|
1093
1190
|
}
|
|
1094
1191
|
const open = openSession(sessionId, [subscription], false);
|
|
@@ -1099,7 +1196,8 @@ function createAcpConnection(options) {
|
|
|
1099
1196
|
listeners: new Set(listeners),
|
|
1100
1197
|
state: "joining",
|
|
1101
1198
|
replayed: created,
|
|
1102
|
-
replayOwed: false
|
|
1199
|
+
replayOwed: false,
|
|
1200
|
+
setup: {}
|
|
1103
1201
|
};
|
|
1104
1202
|
sessions.set(sessionId, open);
|
|
1105
1203
|
const initial = !joinable ? "detached" : created ? "joined" : "joining";
|
|
@@ -1120,7 +1218,18 @@ function createAcpConnection(options) {
|
|
|
1120
1218
|
return;
|
|
1121
1219
|
open.grace = clock.setTimeout(() => part(sessionId, open), PART_GRACE_MS);
|
|
1122
1220
|
}
|
|
1123
|
-
function
|
|
1221
|
+
function openCreated(sessionId, answer) {
|
|
1222
|
+
let open = sessions.get(sessionId);
|
|
1223
|
+
if (!open) {
|
|
1224
|
+
const created = openSession(sessionId, [], true);
|
|
1225
|
+
created.grace = clock.setTimeout(() => part(sessionId, created), PART_GRACE_MS);
|
|
1226
|
+
open = created;
|
|
1227
|
+
}
|
|
1228
|
+
hand(open, report(open, setupOfAnswer(answer)));
|
|
1229
|
+
}
|
|
1230
|
+
function forget(sessionId, open) {
|
|
1231
|
+
clock.clearTimeout(open.grace);
|
|
1232
|
+
open.grace = undefined;
|
|
1124
1233
|
sessions.delete(sessionId);
|
|
1125
1234
|
owners.delete(sessionId);
|
|
1126
1235
|
for (const [child, parent] of children)
|
|
@@ -1129,6 +1238,9 @@ function createAcpConnection(options) {
|
|
|
1129
1238
|
open.owner?.dispose();
|
|
1130
1239
|
open.waiters?.reject(notOpen());
|
|
1131
1240
|
open.waiters = undefined;
|
|
1241
|
+
}
|
|
1242
|
+
function part(sessionId, open) {
|
|
1243
|
+
forget(sessionId, open);
|
|
1132
1244
|
if (open.state === "gone")
|
|
1133
1245
|
return;
|
|
1134
1246
|
request("short", (agent, options) => agent.request(HGW_METHODS2.session.part, { sessionId }, options)).catch((err) => {
|
|
@@ -1151,6 +1263,8 @@ function createAcpConnection(options) {
|
|
|
1151
1263
|
}, options);
|
|
1152
1264
|
}, { sessionId });
|
|
1153
1265
|
const { history } = HgwHistoryPageResponseMetaSchema2.parse(hgwMetaOf(response._meta));
|
|
1266
|
+
if (sessions.get(sessionId) === open)
|
|
1267
|
+
hand(open, report(open, setupOfAnswer(response)));
|
|
1154
1268
|
return { updates, history };
|
|
1155
1269
|
} finally {
|
|
1156
1270
|
if (open.page === updates)
|
|
@@ -1303,25 +1417,22 @@ function createAcpConnection(options) {
|
|
|
1303
1417
|
return outage;
|
|
1304
1418
|
},
|
|
1305
1419
|
subscribeOutage: (listener) => subscribeTo(outageListeners, listener),
|
|
1420
|
+
get versionMismatch() {
|
|
1421
|
+
return versionMismatch;
|
|
1422
|
+
},
|
|
1306
1423
|
login,
|
|
1307
1424
|
async newSession(meta) {
|
|
1308
|
-
const
|
|
1309
|
-
owners.set(sessionId, meta.agentId);
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
open.grace = clock.setTimeout(() => part(sessionId, open), PART_GRACE_MS);
|
|
1313
|
-
}
|
|
1314
|
-
return { sessionId };
|
|
1425
|
+
const created = await request("short", (agent, options) => agent.request(methods2.agent.session.new, { cwd: AGENT_CWD, _meta: { [HGW_META_KEY2]: meta } }, options));
|
|
1426
|
+
owners.set(created.sessionId, meta.agentId);
|
|
1427
|
+
openCreated(created.sessionId, created);
|
|
1428
|
+
return { sessionId: created.sessionId };
|
|
1315
1429
|
},
|
|
1316
1430
|
async forkSession(sessionId) {
|
|
1317
1431
|
const forked = await request("short", (agent, options) => agent.request(methods2.agent.session.fork, { sessionId, cwd: AGENT_CWD }, options));
|
|
1318
1432
|
const agentId = owners.get(sessionId);
|
|
1319
1433
|
if (agentId !== undefined)
|
|
1320
1434
|
owners.set(forked.sessionId, agentId);
|
|
1321
|
-
|
|
1322
|
-
const open = openSession(forked.sessionId, [], true);
|
|
1323
|
-
open.grace = clock.setTimeout(() => part(forked.sessionId, open), PART_GRACE_MS);
|
|
1324
|
-
}
|
|
1435
|
+
openCreated(forked.sessionId, forked);
|
|
1325
1436
|
return { sessionId: forked.sessionId };
|
|
1326
1437
|
},
|
|
1327
1438
|
async listSessions(meta, cursor) {
|
|
@@ -1366,8 +1477,17 @@ function createAcpConnection(options) {
|
|
|
1366
1477
|
},
|
|
1367
1478
|
async setConfigOption(sessionId, configId, value) {
|
|
1368
1479
|
const response = await request("medium", (agent, options) => agent.request(methods2.agent.session.setConfigOption, { sessionId, configId, type: "id", value }, options), { sessionId });
|
|
1480
|
+
const open = sessions.get(sessionId);
|
|
1481
|
+
if (open)
|
|
1482
|
+
report(open, { configOptions: response.configOptions });
|
|
1369
1483
|
return response.configOptions;
|
|
1370
1484
|
},
|
|
1485
|
+
async closeSession(sessionId) {
|
|
1486
|
+
const open = sessions.get(sessionId);
|
|
1487
|
+
if (open)
|
|
1488
|
+
forget(sessionId, open);
|
|
1489
|
+
await request("short", (agent, options) => agent.request(methods2.agent.session.close, { sessionId }, options));
|
|
1490
|
+
},
|
|
1371
1491
|
async deleteSession(sessionId) {
|
|
1372
1492
|
await request("short", (agent, options) => agent.request(methods2.agent.session.delete, { sessionId }, options));
|
|
1373
1493
|
},
|
|
@@ -1376,7 +1496,7 @@ function createAcpConnection(options) {
|
|
|
1376
1496
|
},
|
|
1377
1497
|
async steer(steer) {
|
|
1378
1498
|
assertFrameFits(steer);
|
|
1379
|
-
return HgwSteerResponseSchema2.parse(await request("medium", (agent, options) => agent.request(HGW_METHODS2.session.steer, steer, options), { sessionId: steer.sessionId }));
|
|
1499
|
+
return tolerant(HgwSteerResponseSchema2).parse(await request("medium", (agent, options) => agent.request(HGW_METHODS2.session.steer, steer, options), { sessionId: steer.sessionId }));
|
|
1380
1500
|
},
|
|
1381
1501
|
focus(sessionId, presence) {
|
|
1382
1502
|
const report = {
|
|
@@ -1394,7 +1514,7 @@ function createAcpConnection(options) {
|
|
|
1394
1514
|
const response = await request("short", (agent, options) => agent.request(HGW_METHODS2.agents.update, update, options)).catch((error) => {
|
|
1395
1515
|
throw agentUpdateError(error);
|
|
1396
1516
|
});
|
|
1397
|
-
return HgwAgentUpdateResponseSchema2.parse(response);
|
|
1517
|
+
return tolerant(HgwAgentUpdateResponseSchema2).parse(response);
|
|
1398
1518
|
},
|
|
1399
1519
|
subscribeNotification: (method, listener) => subscribeKeyed(notificationListeners, method, listener),
|
|
1400
1520
|
subscribePendingRequests: (listener) => subscribeTo(pendingListeners, listener),
|
|
@@ -1405,7 +1525,7 @@ function createAcpConnection(options) {
|
|
|
1405
1525
|
import {
|
|
1406
1526
|
SessionConfigOption,
|
|
1407
1527
|
SessionUpdate as SessionUpdate2
|
|
1408
|
-
} from "@agentclientprotocol/sdk/experimental/v2";
|
|
1528
|
+
} from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
1409
1529
|
var MODEL_CATEGORY = "model";
|
|
1410
1530
|
var EFFORT_CATEGORY = "thought_level";
|
|
1411
1531
|
function selectOf(option, category) {
|
|
@@ -1454,7 +1574,7 @@ function projectModels(options) {
|
|
|
1454
1574
|
function projectContext(update, meta) {
|
|
1455
1575
|
if (update.size <= 0)
|
|
1456
1576
|
return;
|
|
1457
|
-
const aos = HgwUsageMetaSchema2.safeParse(meta);
|
|
1577
|
+
const aos = tolerant(HgwUsageMetaSchema2).safeParse(meta);
|
|
1458
1578
|
return {
|
|
1459
1579
|
usedTokens: update.used,
|
|
1460
1580
|
maxTokens: update.size,
|
|
@@ -1533,28 +1653,38 @@ function createAcpComposerStore(connection) {
|
|
|
1533
1653
|
for (const listener of registry.get(sessionId) ?? [])
|
|
1534
1654
|
listener();
|
|
1535
1655
|
}
|
|
1656
|
+
function entryOf(sessionId) {
|
|
1657
|
+
const known = sessions.get(sessionId) ?? { projection: {}, writes: 0 };
|
|
1658
|
+
sessions.set(sessionId, known);
|
|
1659
|
+
return known;
|
|
1660
|
+
}
|
|
1536
1661
|
function project(known, sessionId, next) {
|
|
1537
1662
|
known.projection = next;
|
|
1538
|
-
notify(listeners, sessionId);
|
|
1539
1663
|
const current = currentOf(next, known.current);
|
|
1540
1664
|
if (current === known.current)
|
|
1541
1665
|
return;
|
|
1542
1666
|
known.current = current;
|
|
1543
1667
|
notify(modelListeners, sessionId);
|
|
1544
1668
|
}
|
|
1669
|
+
function settle(known, sessionId, { configOptions, availableCommands, capabilities }) {
|
|
1670
|
+
if (configOptions)
|
|
1671
|
+
project(known, sessionId, projectModels(configOptions));
|
|
1672
|
+
if (availableCommands)
|
|
1673
|
+
known.commands = availableCommands.map(({ name, description }) => ({
|
|
1674
|
+
name,
|
|
1675
|
+
description
|
|
1676
|
+
}));
|
|
1677
|
+
if (capabilities)
|
|
1678
|
+
known.reported = capabilities;
|
|
1679
|
+
if (known.reported)
|
|
1680
|
+
known.capabilities = capabilitiesOf(known.reported, known.commands);
|
|
1681
|
+
notify(listeners, sessionId);
|
|
1682
|
+
}
|
|
1545
1683
|
function accept(sessionId, update, meta) {
|
|
1546
|
-
const known =
|
|
1547
|
-
|
|
1548
|
-
if (
|
|
1549
|
-
|
|
1550
|
-
} else if (SessionUpdate2.isAvailableCommandsUpdate(update)) {
|
|
1551
|
-
known.commands = update.availableCommands.map(({ name, description }) => ({ name, description }));
|
|
1552
|
-
const aos = HgwAvailableCommandsMetaSchema2.safeParse(meta);
|
|
1553
|
-
if (aos.success)
|
|
1554
|
-
known.reported = aos.data.capabilities;
|
|
1555
|
-
if (known.reported)
|
|
1556
|
-
known.capabilities = capabilitiesOf(known.reported, known.commands);
|
|
1557
|
-
notify(listeners, sessionId);
|
|
1684
|
+
const known = entryOf(sessionId);
|
|
1685
|
+
const setup = setupOfUpdate(update, meta);
|
|
1686
|
+
if (setup) {
|
|
1687
|
+
settle(known, sessionId, setup);
|
|
1558
1688
|
} else if (SessionUpdate2.isUsageUpdate(update)) {
|
|
1559
1689
|
const context = projectContext(update, meta);
|
|
1560
1690
|
if (!context)
|
|
@@ -1573,6 +1703,7 @@ function createAcpComposerStore(connection) {
|
|
|
1573
1703
|
function subscribe(sessionId) {
|
|
1574
1704
|
const leave = connection.subscribe(sessionId, {
|
|
1575
1705
|
update: (update, meta) => accept(sessionId, update, meta),
|
|
1706
|
+
setup: (setup) => settle(entryOf(sessionId), sessionId, setup),
|
|
1576
1707
|
replay: () => {
|
|
1577
1708
|
const replayed = sessions.get(sessionId);
|
|
1578
1709
|
if (replayed)
|
|
@@ -1606,8 +1737,10 @@ function createAcpComposerStore(connection) {
|
|
|
1606
1737
|
const generation = ++known.writes;
|
|
1607
1738
|
const configId = (category === MODEL_CATEGORY ? known.projection.modelConfigId : known.projection.effortConfigId) ?? category;
|
|
1608
1739
|
const next = projectModels(await connection.setConfigOption(sessionId, configId, valueId));
|
|
1609
|
-
if (!stale(sessionId, known, generation))
|
|
1740
|
+
if (!stale(sessionId, known, generation)) {
|
|
1610
1741
|
project(known, sessionId, next);
|
|
1742
|
+
notify(listeners, sessionId);
|
|
1743
|
+
}
|
|
1611
1744
|
if (!next.models)
|
|
1612
1745
|
throw new Error("The Session reports no models");
|
|
1613
1746
|
return next.models;
|
|
@@ -1629,12 +1762,12 @@ function createAcpComposerStore(connection) {
|
|
|
1629
1762
|
import {
|
|
1630
1763
|
SessionUpdate as SessionUpdate3,
|
|
1631
1764
|
StateUpdate
|
|
1632
|
-
} from "@agentclientprotocol/sdk/experimental/v2";
|
|
1765
|
+
} from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
1633
1766
|
|
|
1634
1767
|
// client/hgw-notification.ts
|
|
1635
1768
|
function subscribeHgwNotification(connection, method, schema, handler) {
|
|
1636
1769
|
return connection.subscribeNotification(method, (params) => {
|
|
1637
|
-
const parsed = schema.safeParse(params);
|
|
1770
|
+
const parsed = tolerant(schema).safeParse(params);
|
|
1638
1771
|
if (parsed.success)
|
|
1639
1772
|
handler(parsed.data);
|
|
1640
1773
|
});
|
|
@@ -1786,7 +1919,7 @@ function createAcpSessionStore({
|
|
|
1786
1919
|
}
|
|
1787
1920
|
if (!SessionUpdate3.isPlanUpdate(update))
|
|
1788
1921
|
return;
|
|
1789
|
-
const plan = HgwPlanMetaSchema2.safeParse(meta);
|
|
1922
|
+
const plan = tolerant(HgwPlanMetaSchema2).safeParse(meta);
|
|
1790
1923
|
if (plan.success)
|
|
1791
1924
|
setTodos(sessionId, plan.data.todos);
|
|
1792
1925
|
}
|
|
@@ -2186,6 +2319,7 @@ function createAcpWorkspaceClient({
|
|
|
2186
2319
|
adopt(created.sessionId, agentId);
|
|
2187
2320
|
return { sessionId: created.sessionId };
|
|
2188
2321
|
},
|
|
2322
|
+
closeSession: (sessionId) => connection.closeSession(sessionId),
|
|
2189
2323
|
subscribeSession,
|
|
2190
2324
|
async markSessionRead(sessionId) {
|
|
2191
2325
|
store.setUnread(sessionId, false);
|
|
@@ -2509,7 +2643,7 @@ class McpAppFileReadError extends HgwClientError {
|
|
|
2509
2643
|
}
|
|
2510
2644
|
}
|
|
2511
2645
|
async function normalizedError(response) {
|
|
2512
|
-
const parsed = ErrorResponseSchema2.safeParse(await response.json().catch(() => {
|
|
2646
|
+
const parsed = tolerant(ErrorResponseSchema2).safeParse(await response.json().catch(() => {
|
|
2513
2647
|
return;
|
|
2514
2648
|
}));
|
|
2515
2649
|
return parsed.success ? parsed.data.error : undefined;
|
|
@@ -2565,7 +2699,7 @@ class HgwRemoteClient {
|
|
|
2565
2699
|
} catch {
|
|
2566
2700
|
throw new HgwClientError("gateway-failure", "Invalid gateway response");
|
|
2567
2701
|
}
|
|
2568
|
-
const parsed = schema.safeParse(payload);
|
|
2702
|
+
const parsed = tolerant(schema).safeParse(payload);
|
|
2569
2703
|
if (!parsed.success)
|
|
2570
2704
|
throw new HgwClientError("gateway-failure", "Invalid gateway response");
|
|
2571
2705
|
return parsed.data;
|
|
@@ -2579,9 +2713,9 @@ class HgwRemoteClient {
|
|
|
2579
2713
|
async putPushSubscription(registration) {
|
|
2580
2714
|
await this.#write("/push/subscriptions", "PUT", registration);
|
|
2581
2715
|
}
|
|
2582
|
-
async deletePushSubscription(
|
|
2583
|
-
const request = PushUnregistrationSchema2.safeParse(
|
|
2584
|
-
if (!request.success || !endpoint.trim())
|
|
2716
|
+
async deletePushSubscription(device) {
|
|
2717
|
+
const request = PushUnregistrationSchema2.safeParse(device);
|
|
2718
|
+
if (!request.success || "endpoint" in request.data && !request.data.endpoint.trim())
|
|
2585
2719
|
throw new HgwClientError("gateway-failure", "Invalid push subscription");
|
|
2586
2720
|
await this.#write("/push/subscriptions", "DELETE", request.data);
|
|
2587
2721
|
}
|
|
@@ -2756,6 +2890,7 @@ export {
|
|
|
2756
2890
|
AgentUpdateError,
|
|
2757
2891
|
HgwClientError,
|
|
2758
2892
|
HgwRemoteClient,
|
|
2893
|
+
HgwVersionMismatchError,
|
|
2759
2894
|
McpAppFileReadError,
|
|
2760
2895
|
PART_GRACE_MS,
|
|
2761
2896
|
SILENT_LOGGER,
|
package/dist/client/log.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Stream } from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
2
2
|
import type { LogFields, Logger } from "../lifecycle/index.js";
|
|
3
3
|
type FlagStorage = {
|
|
4
4
|
getItem(key: string): string | null;
|
|
@@ -27,7 +27,7 @@ export declare function frameFields(direction: "in" | "out", message: unknown):
|
|
|
27
27
|
frame: Json;
|
|
28
28
|
};
|
|
29
29
|
/** `stream` with one `acp.frame` debug line per message, each way. */
|
|
30
|
-
export declare function loggedStream(stream:
|
|
30
|
+
export declare function loggedStream(stream: Stream, logger: Logger): Stream;
|
|
31
31
|
/**
|
|
32
32
|
* The id at `path` in a row not yet checked, bounded for a warning. A warning
|
|
33
33
|
* about a rejected row names it by this alone, never by its content.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
/** What a parse boundary needs of a schema. */
|
|
3
|
+
type Schema<T> = Pick<z.ZodType<T>, "safeParse">;
|
|
4
|
+
/**
|
|
5
|
+
* `schema` as a client reads what the gateway sends: a key it does not know is
|
|
6
|
+
* dropped wherever a strict object sits, so a later gateway may add one, and
|
|
7
|
+
* every key it knows is still validated. A value that fails for any other
|
|
8
|
+
* reason, a known key of the wrong type, an unknown enum value or union
|
|
9
|
+
* variant, still fails. What a client sends stays strict.
|
|
10
|
+
*/
|
|
11
|
+
export declare function tolerant<T>(schema: Schema<T>): {
|
|
12
|
+
safeParse: (value: unknown) => z.ZodSafeParseResult<T>;
|
|
13
|
+
parse(value: unknown): T;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
package/dist/client/types.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { AgentCapabilities, ContentBlock, CreateElicitationRequest, CreateElicitationResponse, RequestPermissionRequest, RequestPermissionResponse, SessionConfigOption, SessionInfo, SessionUpdate } from "@agentclientprotocol/sdk/experimental/v2";
|
|
1
|
+
import type { AgentCapabilities, AvailableCommand, ContentBlock, CreateElicitationRequest, CreateElicitationResponse, RequestPermissionRequest, RequestPermissionResponse, SessionConfigOption, SessionInfo, SessionUpdate } from "@agentclientprotocol/sdk/experimental/v2/unstable";
|
|
2
2
|
import type { z } from "zod";
|
|
3
3
|
import type { AgentCatalogResponseSchema } from "../protocol/index.js";
|
|
4
4
|
import type { HgwAgentUpdateRequest, HgwAgentUpdateResponseSchema, HgwHistoryCursor, HgwInitializeMetaSchema, HgwPromptMetaSchema, HgwSessionListMetaSchema, HgwSessionNewMetaSchema, HgwSessionSearchMeta, HgwSessionUpdateRequestSchema, HgwSteerRequestSchema, HgwSteerResponseSchema } from "../protocol/acp.js";
|
|
5
|
+
import type { HgwWorkspaceCapabilities } from "./hgw-client.js";
|
|
6
|
+
import type { HgwVersionMismatchError } from "./workspace.js";
|
|
5
7
|
/**
|
|
6
8
|
* The browser-side seam over one ACP v2 WebSocket connection to the gateway.
|
|
7
9
|
* `connection.ts` implements it; the runtime, thread list, interactions, and
|
|
@@ -46,6 +48,19 @@ export type AcpHistoryPage = {
|
|
|
46
48
|
* whether the replay completed.
|
|
47
49
|
*/
|
|
48
50
|
export type AcpSessionReplayListener = () => ((replayed: boolean) => void) | void;
|
|
51
|
+
/**
|
|
52
|
+
* What a Session reports of its setup: its config options, its slash commands,
|
|
53
|
+
* and the capabilities that travel with the commands. A `session/new`,
|
|
54
|
+
* `session/resume`, or `session/fork` answer carries both lists, one it omits
|
|
55
|
+
* read as none, and the capabilities when it names them; a
|
|
56
|
+
* `config_option_update` or `available_commands_update` carries one list. Each
|
|
57
|
+
* list is the Session's whole list, and a field left out here is unchanged.
|
|
58
|
+
*/
|
|
59
|
+
export type AcpSessionSetup = {
|
|
60
|
+
readonly configOptions?: readonly SessionConfigOption[];
|
|
61
|
+
readonly availableCommands?: readonly AvailableCommand[];
|
|
62
|
+
readonly capabilities?: HgwWorkspaceCapabilities;
|
|
63
|
+
};
|
|
49
64
|
/**
|
|
50
65
|
* Where one opened Session stands: `joining` until the gateway has joined it,
|
|
51
66
|
* `joined` while it follows the Session, `unavailable` while a failed join
|
|
@@ -71,6 +86,14 @@ export type AcpSessionListener = {
|
|
|
71
86
|
* may return a callback, called once that replay has settled either way.
|
|
72
87
|
*/
|
|
73
88
|
replay?: AcpSessionReplayListener;
|
|
89
|
+
/**
|
|
90
|
+
* The Session's setup: on subscribe, what it has reported so far by answer
|
|
91
|
+
* or update, then what each later setup answer reports, such as a rejoin's
|
|
92
|
+
* `session/resume`. Live updates of those lists reach `update` alone. An
|
|
93
|
+
* answer and an update alike replace the lists they name, in the order they
|
|
94
|
+
* arrive, as ACP has a client apply them.
|
|
95
|
+
*/
|
|
96
|
+
setup?: (setup: AcpSessionSetup) => void;
|
|
74
97
|
state?: (state: AcpSessionState) => void;
|
|
75
98
|
};
|
|
76
99
|
/**
|
|
@@ -103,14 +126,23 @@ export interface AcpConnection {
|
|
|
103
126
|
*/
|
|
104
127
|
readonly outage: AcpConnectionOutage | undefined;
|
|
105
128
|
subscribeOutage(listener: (outage: AcpConnectionOutage | undefined) => void): () => void;
|
|
129
|
+
/**
|
|
130
|
+
* Set, before `status` turns `closed`, once a handshake finds the gateway
|
|
131
|
+
* speaking another hgw extension version: the first one, which fails
|
|
132
|
+
* `initialized` with the same error, or a reconnect's, after `initialized`
|
|
133
|
+
* has resolved. No other close sets it.
|
|
134
|
+
*/
|
|
135
|
+
readonly versionMismatch: HgwVersionMismatchError | undefined;
|
|
106
136
|
/**
|
|
107
137
|
* Redeems a guest invitation. The connection keeps the token and replays the
|
|
108
138
|
* login before it rejoins its Sessions on a recovered transport.
|
|
109
139
|
*/
|
|
110
140
|
login(token: string): Promise<void>;
|
|
111
141
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
142
|
+
* Creates a Session, opened in its grace: it parts unless a listener
|
|
143
|
+
* subscribes within it, and one that does is handed the setup the answer
|
|
144
|
+
* reported. Its row follows as an update. The browser holds the shared
|
|
145
|
+
* address, so it names the Agent.
|
|
114
146
|
*/
|
|
115
147
|
newSession(meta: z.infer<typeof HgwSessionNewMetaSchema> & {
|
|
116
148
|
agentId: string;
|
|
@@ -120,7 +152,7 @@ export interface AcpConnection {
|
|
|
120
152
|
/**
|
|
121
153
|
* Forks a Session into a new one holding its conversation so far, where
|
|
122
154
|
* `capabilities.session.fork` says the gateway forks; the gateway joins
|
|
123
|
-
* this connection to it, as `newSession` does.
|
|
155
|
+
* this connection to it, and it opens in its grace, as `newSession` does.
|
|
124
156
|
*/
|
|
125
157
|
forkSession(sessionId: string): Promise<{
|
|
126
158
|
sessionId: string;
|
|
@@ -173,6 +205,14 @@ export interface AcpConnection {
|
|
|
173
205
|
}>;
|
|
174
206
|
cancel(sessionId: string): void;
|
|
175
207
|
setConfigOption(sessionId: string, configId: string, value: string): Promise<SessionConfigOption[]>;
|
|
208
|
+
/**
|
|
209
|
+
* Ends the Session with ACP's `session/close`, which stops its work, and
|
|
210
|
+
* forgets it here at once, with no `_hgw/session/part`: its listeners hear
|
|
211
|
+
* nothing more of it, and nothing rejoins it. A Session the gateway no
|
|
212
|
+
* longer has (`-32002`) is closed already, so that answer resolves too.
|
|
213
|
+
*/
|
|
214
|
+
closeSession(sessionId: string): Promise<void>;
|
|
215
|
+
/** Resolves, as `closeSession` does, for a Session already gone (`-32002`). */
|
|
176
216
|
deleteSession(sessionId: string): Promise<void>;
|
|
177
217
|
updateSession(request: z.infer<typeof HgwSessionUpdateRequestSchema>): Promise<void>;
|
|
178
218
|
steer(request: z.infer<typeof HgwSteerRequestSchema>): Promise<z.infer<typeof HgwSteerResponseSchema>>;
|
|
@@ -26,6 +26,19 @@ export declare class AgentUpdateError extends Error {
|
|
|
26
26
|
export declare class AcpConnectionClosedError extends Error {
|
|
27
27
|
constructor(message?: string, options?: ErrorOptions);
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* The gateway speaks another hgw extension version than this client, so the
|
|
31
|
+
* connection ended for good: no retry changes the gateway's version. Which
|
|
32
|
+
* side is older says which one to update.
|
|
33
|
+
*/
|
|
34
|
+
export declare class HgwVersionMismatchError extends Error {
|
|
35
|
+
/** What the gateway's `initialize` announced; undefined for no number. */
|
|
36
|
+
readonly gatewayVersion: number | undefined;
|
|
37
|
+
readonly clientVersion: number;
|
|
38
|
+
constructor(
|
|
39
|
+
/** What the gateway's `initialize` announced; undefined for no number. */
|
|
40
|
+
gatewayVersion: number | undefined, clientVersion: number);
|
|
41
|
+
}
|
|
29
42
|
export type SessionStatus = "idle" | "running" | "waiting-for-input" | "failed" | "unknown";
|
|
30
43
|
export type ActivityBase = {
|
|
31
44
|
id: string;
|