@xopcai/xopc 0.0.265 → 0.0.267
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/dist/browser-ext/dist/assets/{auth-CknJ00ae.js → auth-D_IL6naL.js} +603 -203
- package/dist/browser-ext/dist/assets/sidepanel-CEPaVLjr.css +155 -0
- package/dist/browser-ext/dist/background.js +698 -169
- package/dist/browser-ext/dist/sidepanel.html +3 -3
- package/dist/browser-ext/dist/sidepanel.js +148 -391
- package/dist/browser-ext/manifest.json +3 -6
- package/dist/extensions/telegram/xopc.extension.json +1 -1
- package/dist/gateway/static/root/assets/{agent-avatar-dicebear-DRTTU2H3.js → agent-avatar-dicebear-ZrSAMchm.js} +1 -1
- package/dist/gateway/static/root/assets/{agent-defaults-settings-panel-BsXF7LVi.js → agent-defaults-settings-panel-DiNFN8qS.js} +1 -1
- package/dist/gateway/static/root/assets/{agents-DpjWfxYx.js → agents-Bu6Hf59K.js} +1 -1
- package/dist/gateway/static/root/assets/{app-management-settings-panel-33Mc81h2.js → app-management-settings-panel-D-L6yjS7.js} +1 -1
- package/dist/gateway/static/root/assets/{appearance-settings-C8JECSiJ.js → appearance-settings-BW5eb53j.js} +1 -1
- package/dist/gateway/static/root/assets/{apps-page-t64FcxIZ.js → apps-page-CiQIdz3N.js} +1 -1
- package/dist/gateway/static/root/assets/{archive-plugin-PzZ6SzfY.js → archive-plugin-DwXvu1p9.js} +1 -1
- package/dist/gateway/static/root/assets/{attachment-load-OhLL6K__.js → attachment-load-DqU2BYBp.js} +1 -1
- package/dist/gateway/static/root/assets/{automations-page-BpAYwuvx.js → automations-page-C7qoj9Sd.js} +1 -1
- package/dist/gateway/static/root/assets/{automations-workspace-B6aTT5IC.js → automations-workspace-CIARTLHO.js} +1 -1
- package/dist/gateway/static/root/assets/{binary-plugins-B5XpGsHg.js → binary-plugins-CfplDak2.js} +1 -1
- package/dist/gateway/static/root/assets/{block-editor-BgEhoTnN.js → block-editor-CzLs38ru.js} +1 -1
- package/dist/gateway/static/root/assets/{browser-automations-page-CBLunDRH.js → browser-automations-page-_SKi8s9b.js} +1 -1
- package/dist/gateway/static/root/assets/browser-settings-page-DTdx66sX.js +8 -0
- package/dist/gateway/static/root/assets/{capabilities-settings-panel-DftNnICK.js → capabilities-settings-panel-Ck9U0Olx.js} +1 -1
- package/dist/gateway/static/root/assets/{channels-settings-CpuWa5qu.js → channels-settings-I_vDpbnl.js} +1 -1
- package/dist/gateway/static/root/assets/{chat-terminal-dock-mnPsS_GJ.js → chat-terminal-dock-snvAu3hJ.js} +1 -1
- package/dist/gateway/static/root/assets/{connectors-page-D3nHGnNV.js → connectors-page-BmKTnzo1.js} +1 -1
- package/dist/gateway/static/root/assets/{cron-expression-NcGBvIfe.js → cron-expression-BhGZSbT6.js} +1 -1
- package/dist/gateway/static/root/assets/{date-picker-BrVcl7H5.js → date-picker-Cep5f9b-.js} +1 -1
- package/dist/gateway/static/root/assets/{dependency-picker--edd48Y9.js → dependency-picker-BzSEBQku.js} +1 -1
- package/dist/gateway/static/root/assets/{desktop-pet-vuyyb6I6.js → desktop-pet-BwX_6Gah.js} +1 -1
- package/dist/gateway/static/root/assets/{desktop-pet-settings-DWredJwO.js → desktop-pet-settings-WrbVsU2b.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-debug-page-CtBW4ebj.js → extension-debug-page-BdXSwtAZ.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-page-CsUrmtW7.js → extension-page-I95ukbRb.js} +1 -1
- package/dist/gateway/static/root/assets/{extension-settings-page-BUd31AuK.js → extension-settings-page-8zUisBay.js} +1 -1
- package/dist/gateway/static/root/assets/{gateway-settings-C6FQv7op.js → gateway-settings-DGj5b9Yv.js} +1 -1
- package/dist/gateway/static/root/assets/{heartbeat-settings-CDo1Gjp2.js → heartbeat-settings-BGQxU-Ug.js} +1 -1
- package/dist/gateway/static/root/assets/{home-page-D3_uvVJY.js → home-page-BZPpK0A3.js} +1 -1
- package/dist/gateway/static/root/assets/{index-Dpu7EKTm.js → index-D2OtD9Wa.js} +8 -8
- package/dist/gateway/static/root/assets/{keyboard-shortcuts-settings-CNVmrCrV.js → keyboard-shortcuts-settings-BUe_om99.js} +1 -1
- package/dist/gateway/static/root/assets/{local-app-workbench-page-B0IVDvs-.js → local-app-workbench-page-CbQnffS9.js} +1 -1
- package/dist/gateway/static/root/assets/{local-apps-page-DvkarvLt.js → local-apps-page-BZaLzfms.js} +1 -1
- package/dist/gateway/static/root/assets/{logs-page-BknY0vZf.js → logs-page-WWCHqW_x.js} +1 -1
- package/dist/gateway/static/root/assets/{management-settings-DW6hJINQ.js → management-settings-Duf8w-Bl.js} +1 -1
- package/dist/gateway/static/root/assets/{media-plugins-DYautOWY.js → media-plugins-2-QfzHmg.js} +1 -1
- package/dist/gateway/static/root/assets/{mobile-pairing-wizard-Dgu03cv-.js → mobile-pairing-wizard-DRaVIAt1.js} +1 -1
- package/dist/gateway/static/root/assets/{note-detail-panel-Cv9uH6Jq.js → note-detail-panel-BT-fWGxX.js} +3 -3
- package/dist/gateway/static/root/assets/{notes-page-DcIAiYJn.js → notes-page-C4705PzN.js} +1 -1
- package/dist/gateway/static/root/assets/{page-tabs-DF6Q5XvA.js → page-tabs-DaZ7QpNO.js} +1 -1
- package/dist/gateway/static/root/assets/{preview-open-alternatives-CQJH_jAN.js → preview-open-alternatives-CWEuqCmv.js} +1 -1
- package/dist/gateway/static/root/assets/{project-detail-page-D65JfNSw.js → project-detail-page-BZegJ6an.js} +1 -1
- package/dist/gateway/static/root/assets/{projects-page-BfWF7Sz-.js → projects-page-B7hUHeLI.js} +1 -1
- package/dist/gateway/static/root/assets/{remote-access-hub-DA1g4CUD.js → remote-access-hub-BE9hZpdh.js} +1 -1
- package/dist/gateway/static/root/assets/{runtime-tools-settings-panel-0-bFSBB8.js → runtime-tools-settings-panel-CslkSM-m.js} +1 -1
- package/dist/gateway/static/root/assets/{schema-form-csGbhTUr.js → schema-form-B2gvVrBq.js} +1 -1
- package/dist/gateway/static/root/assets/{sessions-page-DXPBNhrZ.js → sessions-page-DmAAPMo1.js} +1 -1
- package/dist/gateway/static/root/assets/settings-advanced-gate-CvBocGDA.js +1 -0
- package/dist/gateway/static/root/assets/{settings-form-section-YdrdY8B3.js → settings-form-section-CmlnuSO3.js} +1 -1
- package/dist/gateway/static/root/assets/{settings-page-Cv9waRn3.js → settings-page-DWWgyelT.js} +1 -1
- package/dist/gateway/static/root/assets/{setup-status-panel-DPf-_05Z.js → setup-status-panel-CJrN3m1o.js} +1 -1
- package/dist/gateway/static/root/assets/{share-preview-page-DBoEHTPn.js → share-preview-page-C12oyIKk.js} +1 -1
- package/dist/gateway/static/root/assets/{shares-settings-BQP5yNcO.js → shares-settings-D4BtSiLq.js} +1 -1
- package/dist/gateway/static/root/assets/{skill-reload-api-B5T8cgjq.js → skill-reload-api-CzKGn056.js} +1 -1
- package/dist/gateway/static/root/assets/{skills-page-D1G8h2Lt.js → skills-page-yhW7LLqk.js} +1 -1
- package/dist/gateway/static/root/assets/{skills-page.utils-BNZYTop7.js → skills-page.utils-FaTlq8t-.js} +1 -1
- package/dist/gateway/static/root/assets/{system-settings-panel-ByuppbHi.js → system-settings-panel-CMrkoJLN.js} +1 -1
- package/dist/gateway/static/root/assets/{task-detail-page-DNmkrF6y.js → task-detail-page-BUaO62cy.js} +1 -1
- package/dist/gateway/static/root/assets/{tunnel-api-CxWGpbwr.js → tunnel-api-C2tAZaJz.js} +1 -1
- package/dist/gateway/static/root/assets/{use-autosave-B0l4qubU.js → use-autosave-BsDRt4QI.js} +1 -1
- package/dist/gateway/static/root/assets/{user-model-page-DMsI6YI7.js → user-model-page-Dy8h1SSJ.js} +1 -1
- package/dist/gateway/static/root/assets/{voice-api-key-field-Bcz7boJg.js → voice-api-key-field-CS5heYMP.js} +1 -1
- package/dist/gateway/static/root/assets/{work-discovery-overlay-Dujq1j_u.js → work-discovery-overlay-BeSz5Th5.js} +1 -1
- package/dist/gateway/static/root/assets/{workflow-run-setup-panel-CxNYbLNB.js → workflow-run-setup-panel-BebW0CDK.js} +1 -1
- package/dist/gateway/static/root/assets/{workflows-page-Dk1jK2nv.js → workflows-page-CU8jFgPF.js} +1 -1
- package/dist/gateway/static/root/index.html +1 -1
- package/dist/package.js +1 -1
- package/dist/packages/browser-control-contract/src/index.js +30 -1
- package/dist/src/agent/agent-manager.js +4 -4
- package/dist/src/agent/background-review/run-background-review.js +1 -1
- package/dist/src/agent/embedded/model-runtime.js +1 -1
- package/dist/src/agent/embedded/run-for-session.js +1 -1
- package/dist/src/agent/external-tools/endpoint-provider.js +3 -1
- package/dist/src/agent/image/tool-model-config.js +1 -1
- package/dist/src/agent/mcp/oauth/mcp-oauth-store.js +1 -1
- package/dist/src/agent/service.js +2 -2
- package/dist/src/agent/session/session-inspector.js +1 -1
- package/dist/src/agent/skills/hub-lock.js +1 -1
- package/dist/src/agent/tasks/task-judge-service.js +3 -3
- package/dist/src/agent/tools/factory.js +1 -1
- package/dist/src/ai-assist/text-assist.js +1 -1
- package/dist/src/auth/credentials.js +2 -2
- package/dist/src/auth/oauth/xopc-cloud.js +1 -1
- package/dist/src/auth/profiles/store.js +1 -1
- package/dist/src/auth/sync-provider-auth.js +2 -2
- package/dist/src/browser/automations/runtime-service.d.ts +2 -0
- package/dist/src/browser/automations/runtime-service.js +1 -1
- package/dist/src/browser/drivers/create-driver.d.ts +2 -1
- package/dist/src/browser/drivers/create-driver.js +4 -6
- package/dist/src/browser/drivers/extension-driver.d.ts +7 -2
- package/dist/src/browser/enrollment.d.ts +8 -0
- package/dist/src/browser/enrollment.js +25 -0
- package/dist/src/browser/extension-identity.d.ts +2 -0
- package/dist/src/browser/extension-identity.js +5 -0
- package/dist/src/browser/native-messaging-host.d.ts +6 -3
- package/dist/src/browser/native-messaging-host.js +23 -5
- package/dist/src/browser/providers/browser-ext-install.d.ts +4 -1
- package/dist/src/browser/providers/browser-ext-install.js +47 -8
- package/dist/src/browser/providers/extension.js +1 -1
- package/dist/src/browser/providers/realtime-extension.d.ts +29 -0
- package/dist/src/browser/providers/realtime-extension.js +83 -0
- package/dist/src/cli/commands/doctor/checks/database-schema.js +1 -1
- package/dist/src/cli/commands/doctor/checks/provider-auth.js +1 -1
- package/dist/src/cli/commands/models.js +1 -1
- package/dist/src/config/index.js +4 -4
- package/dist/src/config/loader.js +2 -2
- package/dist/src/config/models-json.js +1 -1
- package/dist/src/endpoint-tools/policy.js +10 -3
- package/dist/src/extensions/lockfile.js +1 -1
- package/dist/src/gateway/agents-admin.js +1 -1
- package/dist/src/gateway/file-path-classifier.js +1 -1
- package/dist/src/gateway/hono/app.js +6 -1
- package/dist/src/gateway/hono/lib/config-payload.js +1 -1
- package/dist/src/gateway/hono/routes/browser.js +20 -30
- package/dist/src/gateway/hono/routes/models.js +1 -1
- package/dist/src/gateway/security/origin-check.js +5 -0
- package/dist/src/gateway/service/config-coordinator.d.ts +0 -2
- package/dist/src/gateway/service/config-coordinator.js +1 -3
- package/dist/src/gateway/service.d.ts +0 -11
- package/dist/src/gateway/service.js +5 -83
- package/dist/src/providers/auth-runtime/resolve-auth.js +1 -1
- package/dist/src/providers/index.js +2 -2
- package/dist/src/providers/model-registry.js +1 -1
- package/dist/src/providers/register-runtime-providers.js +1 -1
- package/dist/src/storage/sqlite/device-pairing-approval.js +24 -3
- package/dist/src/storage/sqlite/device-pairing-repository.d.ts +10 -1
- package/dist/src/storage/sqlite/device-pairing-repository.js +6 -5
- package/dist/src/storage/sqlite/migrations/157_browser_endpoint_principals.sql +50 -0
- package/dist/src/storage/sqlite/migrations/158_browser_native_enrollment.sql +4 -0
- package/dist/src/storage/sqlite/migrations/runner.d.ts +1 -1
- package/dist/src/storage/sqlite/migrations/runner.js +3 -3
- package/dist/src/storage/sqlite/migrations/runner.ts +1 -1
- package/dist/src/storage/sqlite/schema.js +1 -1
- package/dist/src/tui/tui-startup-resources.js +1 -1
- package/dist/src/tui/tui.js +1 -1
- package/dist/src/work-discovery/service.js +1 -1
- package/dist/src/workflows/service/workflow-run-service.js +1 -1
- package/package.json +3 -3
- package/dist/browser-ext/dist/assets/sidepanel-C69P3v9u.css +0 -113
- package/dist/gateway/static/root/assets/browser-settings-page-CSfpPCuD.js +0 -8
- package/dist/gateway/static/root/assets/settings-advanced-gate-DxeZm67O.js +0 -1
|
@@ -1,26 +1,592 @@
|
|
|
1
|
-
import { T as TAB_BINDING_PREFIX,
|
|
1
|
+
import { h as endpointToolContentSchema, E as ENDPOINT_PROTOCOL_VERSION, c as canonicalJson, T as TAB_BINDING_PREFIX, b as captureTabPage, P as PENDING_CONTEXT_KEY, r as readProfile, k as registerBrowserEndpoint, j as getAccessProfile, R as RealtimeClient, e as createBrowserEndpointHello, i as gatewayFetch } from './assets/auth-D_IL6naL.js';
|
|
2
2
|
|
|
3
3
|
const BROWSER_EXTENSION_PROTOCOL_VERSION = 4;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
input
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
const BROWSER_CONTROL_ENDPOINT_TOOL_NAME = "browser.control";
|
|
5
|
+
const BROWSER_CONTROL_ENDPOINT_INPUT_SCHEMA = {
|
|
6
|
+
type: "object",
|
|
7
|
+
additionalProperties: false,
|
|
8
|
+
required: ["input"],
|
|
9
|
+
properties: {
|
|
10
|
+
input: { type: "object" },
|
|
11
|
+
timeoutMs: { type: "integer", minimum: 1, maximum: 12e4 },
|
|
12
|
+
visualFallback: { type: "boolean" }
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
const BROWSER_CONTROL_ENDPOINT_OUTPUT_SCHEMA = {
|
|
16
|
+
type: "array",
|
|
17
|
+
minItems: 1,
|
|
18
|
+
maxItems: 1,
|
|
19
|
+
items: {
|
|
20
|
+
type: "object",
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
required: ["type", "value"],
|
|
23
|
+
properties: {
|
|
24
|
+
type: { const: "json" },
|
|
25
|
+
value: {}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const BROWSER_CONTROL_ENDPOINT_DESCRIPTOR = {
|
|
30
|
+
name: BROWSER_CONTROL_ENDPOINT_TOOL_NAME,
|
|
31
|
+
title: "Browser control transport",
|
|
32
|
+
description: "Internal authenticated transport for xopc browser-control actions.",
|
|
33
|
+
inputSchema: BROWSER_CONTROL_ENDPOINT_INPUT_SCHEMA,
|
|
34
|
+
outputSchema: BROWSER_CONTROL_ENDPOINT_OUTPUT_SCHEMA,
|
|
35
|
+
policyId: "browser.control",
|
|
36
|
+
sensitivity: "personal",
|
|
37
|
+
effect: "write",
|
|
38
|
+
confirmation: "never",
|
|
39
|
+
requiresForeground: false,
|
|
40
|
+
requiredPermissions: ["browser-control"],
|
|
41
|
+
timeoutMs: 12e4,
|
|
42
|
+
maxConcurrency: 4,
|
|
43
|
+
supportsCancellation: true,
|
|
44
|
+
idempotent: false,
|
|
45
|
+
resultKinds: ["json"]
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
50
|
+
* @module
|
|
51
|
+
*/
|
|
52
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
53
|
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
54
|
+
// node.js versions earlier than v19 don't declare it in global scope.
|
|
55
|
+
// For node.js, package.json#exports field mapping rewrites import
|
|
56
|
+
// from `crypto` to `cryptoNode`, which imports native module.
|
|
57
|
+
// Makes the utils un-importable in browsers without a bundler.
|
|
58
|
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
|
59
|
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
60
|
+
function isBytes(a) {
|
|
61
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
62
|
+
}
|
|
63
|
+
/** Asserts something is Uint8Array. */
|
|
64
|
+
function abytes(b, ...lengths) {
|
|
65
|
+
if (!isBytes(b))
|
|
66
|
+
throw new Error('Uint8Array expected');
|
|
67
|
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
|
68
|
+
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
|
69
|
+
}
|
|
70
|
+
/** Asserts a hash instance has not been destroyed / finished */
|
|
71
|
+
function aexists(instance, checkFinished = true) {
|
|
72
|
+
if (instance.destroyed)
|
|
73
|
+
throw new Error('Hash instance has been destroyed');
|
|
74
|
+
if (checkFinished && instance.finished)
|
|
75
|
+
throw new Error('Hash#digest() has already been called');
|
|
76
|
+
}
|
|
77
|
+
/** Asserts output is properly-sized byte array */
|
|
78
|
+
function aoutput(out, instance) {
|
|
79
|
+
abytes(out);
|
|
80
|
+
const min = instance.outputLen;
|
|
81
|
+
if (out.length < min) {
|
|
82
|
+
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
86
|
+
function clean(...arrays) {
|
|
87
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
88
|
+
arrays[i].fill(0);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** Create DataView of an array for easy byte-level manipulation. */
|
|
92
|
+
function createView(arr) {
|
|
93
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
94
|
+
}
|
|
95
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
96
|
+
function rotr(word, shift) {
|
|
97
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Converts string to bytes using UTF8 encoding.
|
|
101
|
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
|
102
|
+
*/
|
|
103
|
+
function utf8ToBytes(str) {
|
|
104
|
+
if (typeof str !== 'string')
|
|
105
|
+
throw new Error('string expected');
|
|
106
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
110
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
111
|
+
* Keep in mind for future mutable operations.
|
|
112
|
+
*/
|
|
113
|
+
function toBytes(data) {
|
|
114
|
+
if (typeof data === 'string')
|
|
115
|
+
data = utf8ToBytes(data);
|
|
116
|
+
abytes(data);
|
|
117
|
+
return data;
|
|
118
|
+
}
|
|
119
|
+
/** For runtime check if class implements interface */
|
|
120
|
+
class Hash {
|
|
121
|
+
}
|
|
122
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
123
|
+
function createHasher(hashCons) {
|
|
124
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
125
|
+
const tmp = hashCons();
|
|
126
|
+
hashC.outputLen = tmp.outputLen;
|
|
127
|
+
hashC.blockLen = tmp.blockLen;
|
|
128
|
+
hashC.create = () => hashCons();
|
|
129
|
+
return hashC;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Internal Merkle-Damgard hash utils.
|
|
134
|
+
* @module
|
|
135
|
+
*/
|
|
136
|
+
/** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
|
|
137
|
+
function setBigUint64(view, byteOffset, value, isLE) {
|
|
138
|
+
if (typeof view.setBigUint64 === 'function')
|
|
139
|
+
return view.setBigUint64(byteOffset, value, isLE);
|
|
140
|
+
const _32n = BigInt(32);
|
|
141
|
+
const _u32_max = BigInt(0xffffffff);
|
|
142
|
+
const wh = Number((value >> _32n) & _u32_max);
|
|
143
|
+
const wl = Number(value & _u32_max);
|
|
144
|
+
const h = isLE ? 4 : 0;
|
|
145
|
+
const l = isLE ? 0 : 4;
|
|
146
|
+
view.setUint32(byteOffset + h, wh, isLE);
|
|
147
|
+
view.setUint32(byteOffset + l, wl, isLE);
|
|
148
|
+
}
|
|
149
|
+
/** Choice: a ? b : c */
|
|
150
|
+
function Chi(a, b, c) {
|
|
151
|
+
return (a & b) ^ (~a & c);
|
|
152
|
+
}
|
|
153
|
+
/** Majority function, true if any two inputs is true. */
|
|
154
|
+
function Maj(a, b, c) {
|
|
155
|
+
return (a & b) ^ (a & c) ^ (b & c);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Merkle-Damgard hash construction base class.
|
|
159
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
160
|
+
*/
|
|
161
|
+
class HashMD extends Hash {
|
|
162
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
163
|
+
super();
|
|
164
|
+
this.finished = false;
|
|
165
|
+
this.length = 0;
|
|
166
|
+
this.pos = 0;
|
|
167
|
+
this.destroyed = false;
|
|
168
|
+
this.blockLen = blockLen;
|
|
169
|
+
this.outputLen = outputLen;
|
|
170
|
+
this.padOffset = padOffset;
|
|
171
|
+
this.isLE = isLE;
|
|
172
|
+
this.buffer = new Uint8Array(blockLen);
|
|
173
|
+
this.view = createView(this.buffer);
|
|
174
|
+
}
|
|
175
|
+
update(data) {
|
|
176
|
+
aexists(this);
|
|
177
|
+
data = toBytes(data);
|
|
178
|
+
abytes(data);
|
|
179
|
+
const { view, buffer, blockLen } = this;
|
|
180
|
+
const len = data.length;
|
|
181
|
+
for (let pos = 0; pos < len;) {
|
|
182
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
183
|
+
// Fast path: we have at least one block in input, cast it to view and process
|
|
184
|
+
if (take === blockLen) {
|
|
185
|
+
const dataView = createView(data);
|
|
186
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
187
|
+
this.process(dataView, pos);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
191
|
+
this.pos += take;
|
|
192
|
+
pos += take;
|
|
193
|
+
if (this.pos === blockLen) {
|
|
194
|
+
this.process(view, 0);
|
|
195
|
+
this.pos = 0;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
this.length += data.length;
|
|
199
|
+
this.roundClean();
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
digestInto(out) {
|
|
203
|
+
aexists(this);
|
|
204
|
+
aoutput(out, this);
|
|
205
|
+
this.finished = true;
|
|
206
|
+
// Padding
|
|
207
|
+
// We can avoid allocation of buffer for padding completely if it
|
|
208
|
+
// was previously not allocated here. But it won't change performance.
|
|
209
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
210
|
+
let { pos } = this;
|
|
211
|
+
// append the bit '1' to the message
|
|
212
|
+
buffer[pos++] = 0b10000000;
|
|
213
|
+
clean(this.buffer.subarray(pos));
|
|
214
|
+
// we have less than padOffset left in buffer, so we cannot put length in
|
|
215
|
+
// current block, need process it and pad again
|
|
216
|
+
if (this.padOffset > blockLen - pos) {
|
|
217
|
+
this.process(view, 0);
|
|
218
|
+
pos = 0;
|
|
219
|
+
}
|
|
220
|
+
// Pad until full block byte with zeros
|
|
221
|
+
for (let i = pos; i < blockLen; i++)
|
|
222
|
+
buffer[i] = 0;
|
|
223
|
+
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
|
224
|
+
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
|
225
|
+
// So we just write lowest 64 bits of that value.
|
|
226
|
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
227
|
+
this.process(view, 0);
|
|
228
|
+
const oview = createView(out);
|
|
229
|
+
const len = this.outputLen;
|
|
230
|
+
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
|
231
|
+
if (len % 4)
|
|
232
|
+
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
233
|
+
const outLen = len / 4;
|
|
234
|
+
const state = this.get();
|
|
235
|
+
if (outLen > state.length)
|
|
236
|
+
throw new Error('_sha2: outputLen bigger than state');
|
|
237
|
+
for (let i = 0; i < outLen; i++)
|
|
238
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
239
|
+
}
|
|
240
|
+
digest() {
|
|
241
|
+
const { buffer, outputLen } = this;
|
|
242
|
+
this.digestInto(buffer);
|
|
243
|
+
const res = buffer.slice(0, outputLen);
|
|
244
|
+
this.destroy();
|
|
245
|
+
return res;
|
|
246
|
+
}
|
|
247
|
+
_cloneInto(to) {
|
|
248
|
+
to || (to = new this.constructor());
|
|
249
|
+
to.set(...this.get());
|
|
250
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
251
|
+
to.destroyed = destroyed;
|
|
252
|
+
to.finished = finished;
|
|
253
|
+
to.length = length;
|
|
254
|
+
to.pos = pos;
|
|
255
|
+
if (length % blockLen)
|
|
256
|
+
to.buffer.set(buffer);
|
|
257
|
+
return to;
|
|
258
|
+
}
|
|
259
|
+
clone() {
|
|
260
|
+
return this._cloneInto();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
265
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
266
|
+
*/
|
|
267
|
+
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
|
268
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
269
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
270
|
+
]);
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
274
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
275
|
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
|
276
|
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
277
|
+
* @module
|
|
278
|
+
*/
|
|
279
|
+
/**
|
|
280
|
+
* Round constants:
|
|
281
|
+
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
|
282
|
+
*/
|
|
283
|
+
// prettier-ignore
|
|
284
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
285
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
286
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
287
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
288
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
289
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
290
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
291
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
292
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
293
|
+
]);
|
|
294
|
+
/** Reusable temporary buffer. "W" comes straight from spec. */
|
|
295
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
296
|
+
class SHA256 extends HashMD {
|
|
297
|
+
constructor(outputLen = 32) {
|
|
298
|
+
super(64, outputLen, 8, false);
|
|
299
|
+
// We cannot use array here since array allows indexing by variable
|
|
300
|
+
// which means optimizer/compiler cannot use registers.
|
|
301
|
+
this.A = SHA256_IV[0] | 0;
|
|
302
|
+
this.B = SHA256_IV[1] | 0;
|
|
303
|
+
this.C = SHA256_IV[2] | 0;
|
|
304
|
+
this.D = SHA256_IV[3] | 0;
|
|
305
|
+
this.E = SHA256_IV[4] | 0;
|
|
306
|
+
this.F = SHA256_IV[5] | 0;
|
|
307
|
+
this.G = SHA256_IV[6] | 0;
|
|
308
|
+
this.H = SHA256_IV[7] | 0;
|
|
309
|
+
}
|
|
310
|
+
get() {
|
|
311
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
312
|
+
return [A, B, C, D, E, F, G, H];
|
|
313
|
+
}
|
|
314
|
+
// prettier-ignore
|
|
315
|
+
set(A, B, C, D, E, F, G, H) {
|
|
316
|
+
this.A = A | 0;
|
|
317
|
+
this.B = B | 0;
|
|
318
|
+
this.C = C | 0;
|
|
319
|
+
this.D = D | 0;
|
|
320
|
+
this.E = E | 0;
|
|
321
|
+
this.F = F | 0;
|
|
322
|
+
this.G = G | 0;
|
|
323
|
+
this.H = H | 0;
|
|
324
|
+
}
|
|
325
|
+
process(view, offset) {
|
|
326
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
327
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
328
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
329
|
+
for (let i = 16; i < 64; i++) {
|
|
330
|
+
const W15 = SHA256_W[i - 15];
|
|
331
|
+
const W2 = SHA256_W[i - 2];
|
|
332
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
|
333
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
|
334
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
335
|
+
}
|
|
336
|
+
// Compression function main loop, 64 rounds
|
|
337
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
338
|
+
for (let i = 0; i < 64; i++) {
|
|
339
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
340
|
+
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
341
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
342
|
+
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
|
343
|
+
H = G;
|
|
344
|
+
G = F;
|
|
345
|
+
F = E;
|
|
346
|
+
E = (D + T1) | 0;
|
|
347
|
+
D = C;
|
|
348
|
+
C = B;
|
|
349
|
+
B = A;
|
|
350
|
+
A = (T1 + T2) | 0;
|
|
351
|
+
}
|
|
352
|
+
// Add the compressed chunk to the current hash value
|
|
353
|
+
A = (A + this.A) | 0;
|
|
354
|
+
B = (B + this.B) | 0;
|
|
355
|
+
C = (C + this.C) | 0;
|
|
356
|
+
D = (D + this.D) | 0;
|
|
357
|
+
E = (E + this.E) | 0;
|
|
358
|
+
F = (F + this.F) | 0;
|
|
359
|
+
G = (G + this.G) | 0;
|
|
360
|
+
H = (H + this.H) | 0;
|
|
361
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
362
|
+
}
|
|
363
|
+
roundClean() {
|
|
364
|
+
clean(SHA256_W);
|
|
365
|
+
}
|
|
366
|
+
destroy() {
|
|
367
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
368
|
+
clean(this.buffer);
|
|
369
|
+
}
|
|
14
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* SHA2-256 hash function from RFC 4634.
|
|
373
|
+
*
|
|
374
|
+
* It is the fastest JS hash, even faster than Blake3.
|
|
375
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
376
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
377
|
+
*/
|
|
378
|
+
const sha256$1 = /* @__PURE__ */ createHasher(() => new SHA256());
|
|
15
379
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
380
|
+
/**
|
|
381
|
+
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
|
382
|
+
*
|
|
383
|
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
|
384
|
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
385
|
+
*
|
|
386
|
+
* Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
|
387
|
+
* @module
|
|
388
|
+
* @deprecated
|
|
389
|
+
*/
|
|
390
|
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
|
391
|
+
const sha256 = sha256$1;
|
|
392
|
+
|
|
393
|
+
class EndpointToolClientError extends Error {
|
|
394
|
+
constructor(code, message) {
|
|
395
|
+
super(message);
|
|
396
|
+
this.code = code;
|
|
397
|
+
this.name = "EndpointToolClientError";
|
|
398
|
+
}
|
|
399
|
+
code;
|
|
400
|
+
}
|
|
401
|
+
function endpointToolRevision(descriptor) {
|
|
402
|
+
return [...sha256(new TextEncoder().encode(canonicalJson(descriptor)))].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
403
|
+
}
|
|
404
|
+
class EndpointToolRegistry {
|
|
405
|
+
tools = /* @__PURE__ */ new Map();
|
|
406
|
+
constructor(definitions) {
|
|
407
|
+
for (const definition of definitions) {
|
|
408
|
+
const name = definition.descriptor.name;
|
|
409
|
+
if (this.tools.has(name)) throw new Error(`Duplicate endpoint tool: ${name}`);
|
|
410
|
+
this.tools.set(name, {
|
|
411
|
+
definition,
|
|
412
|
+
revision: endpointToolRevision(definition.descriptor)
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
descriptors() {
|
|
417
|
+
return [...this.tools.values()].map(({ definition }) => definition.descriptor);
|
|
418
|
+
}
|
|
419
|
+
get(name) {
|
|
420
|
+
return this.tools.get(name);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
class EndpointToolHostController {
|
|
424
|
+
constructor(options) {
|
|
425
|
+
this.options = options;
|
|
426
|
+
this.now = options.now ?? Date.now;
|
|
427
|
+
}
|
|
428
|
+
options;
|
|
429
|
+
active = /* @__PURE__ */ new Map();
|
|
430
|
+
now;
|
|
431
|
+
sendMessage;
|
|
432
|
+
generation = 0;
|
|
433
|
+
connect(sendMessage) {
|
|
434
|
+
this.disconnect();
|
|
435
|
+
this.sendMessage = sendMessage;
|
|
436
|
+
}
|
|
437
|
+
disconnect() {
|
|
438
|
+
this.generation += 1;
|
|
439
|
+
this.sendMessage = void 0;
|
|
440
|
+
for (const invocation of this.active.values()) invocation.controller.abort();
|
|
441
|
+
this.active.clear();
|
|
442
|
+
}
|
|
443
|
+
publishAvailability() {
|
|
444
|
+
this.send("endpoint.availability_changed", {
|
|
445
|
+
availability: this.options.getAvailability()
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
async handleMessage(message) {
|
|
449
|
+
if (message.type === "tool.cancel") {
|
|
450
|
+
this.cancel(message.payload.invocationId);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
await this.invoke(message);
|
|
454
|
+
}
|
|
455
|
+
async invoke(message) {
|
|
456
|
+
const {
|
|
457
|
+
invocationId,
|
|
458
|
+
toolName,
|
|
459
|
+
arguments: args,
|
|
460
|
+
descriptorRevision,
|
|
461
|
+
confirmationRequired,
|
|
462
|
+
deadlineAt,
|
|
463
|
+
uploadGrant
|
|
464
|
+
} = message.payload;
|
|
465
|
+
if (this.active.has(invocationId)) {
|
|
466
|
+
this.sendError(invocationId, "PROTOCOL_ERROR", "Endpoint invocation is already running");
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
this.send("tool.received", { invocationId });
|
|
470
|
+
const registered = this.options.registry.get(toolName);
|
|
471
|
+
if (!registered) {
|
|
472
|
+
this.sendError(invocationId, "TOOL_NOT_FOUND", "Endpoint tool is not registered");
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (registered.revision !== descriptorRevision) {
|
|
476
|
+
this.sendError(invocationId, "TOOL_REVISION_MISMATCH", "Endpoint tool contract changed");
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const descriptor = registered.definition.descriptor;
|
|
480
|
+
const localConfirmationRequired = descriptor.confirmation === "always";
|
|
481
|
+
if (confirmationRequired !== localConfirmationRequired) {
|
|
482
|
+
this.sendError(invocationId, "PROTOCOL_ERROR", "Endpoint confirmation policy mismatch");
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const invocation = {
|
|
486
|
+
controller: new AbortController(),
|
|
487
|
+
generation: this.generation
|
|
488
|
+
};
|
|
489
|
+
this.active.set(invocationId, invocation);
|
|
490
|
+
try {
|
|
491
|
+
this.assertExecutable(invocationId, invocation, descriptor, deadlineAt);
|
|
492
|
+
const authorizationRequest = {
|
|
493
|
+
invocationId,
|
|
494
|
+
descriptor,
|
|
495
|
+
arguments: args,
|
|
496
|
+
deadlineAt,
|
|
497
|
+
signal: invocation.controller.signal
|
|
498
|
+
};
|
|
499
|
+
await this.options.authorize?.(authorizationRequest);
|
|
500
|
+
this.assertExecutable(invocationId, invocation, descriptor, deadlineAt);
|
|
501
|
+
if (localConfirmationRequired) {
|
|
502
|
+
this.send("tool.progress", { invocationId, message: "Waiting for approval" });
|
|
503
|
+
const allowed = await this.options.confirm(authorizationRequest);
|
|
504
|
+
this.assertExecutable(invocationId, invocation, descriptor, deadlineAt);
|
|
505
|
+
if (!allowed) throw new EndpointToolClientError("USER_DENIED", "User denied the endpoint tool call");
|
|
506
|
+
}
|
|
507
|
+
const result = await registered.definition.execute(args, {
|
|
508
|
+
invocationId,
|
|
509
|
+
signal: invocation.controller.signal,
|
|
510
|
+
reportProgress: (progress) => {
|
|
511
|
+
if (this.isCurrent(invocationId, invocation)) {
|
|
512
|
+
this.send("tool.progress", { invocationId, ...progress });
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
uploadFile: (file) => this.options.uploadFile(uploadGrant, file)
|
|
516
|
+
});
|
|
517
|
+
this.assertExecutable(invocationId, invocation, descriptor, deadlineAt);
|
|
518
|
+
const content = endpointToolContentSchema.array().min(1).max(20).parse(result.content);
|
|
519
|
+
this.send("tool.result", {
|
|
520
|
+
invocationId,
|
|
521
|
+
content,
|
|
522
|
+
...result.details ? { details: result.details } : {}
|
|
523
|
+
});
|
|
524
|
+
this.active.delete(invocationId);
|
|
525
|
+
if (result.afterSend) {
|
|
526
|
+
const generation = invocation.generation;
|
|
527
|
+
setTimeout(() => {
|
|
528
|
+
if (this.generation === generation && this.now() < deadlineAt && (!descriptor.requiresForeground || this.options.getAvailability() === "foreground")) {
|
|
529
|
+
result.afterSend?.();
|
|
530
|
+
}
|
|
531
|
+
}, this.options.afterSendDelayMs ?? 50);
|
|
532
|
+
}
|
|
533
|
+
} catch (error) {
|
|
534
|
+
if (!this.isCurrent(invocationId, invocation)) return;
|
|
535
|
+
this.active.delete(invocationId);
|
|
536
|
+
const normalized = this.normalizeError(error);
|
|
537
|
+
this.sendError(invocationId, normalized.code, normalized.message);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
cancel(invocationId) {
|
|
541
|
+
const invocation = this.active.get(invocationId);
|
|
542
|
+
if (!invocation) return;
|
|
543
|
+
this.active.delete(invocationId);
|
|
544
|
+
invocation.controller.abort();
|
|
545
|
+
this.send("tool.cancelled", { invocationId });
|
|
546
|
+
}
|
|
547
|
+
assertExecutable(invocationId, invocation, descriptor, deadlineAt) {
|
|
548
|
+
if (!this.isCurrent(invocationId, invocation) || invocation.controller.signal.aborted) {
|
|
549
|
+
throw new EndpointToolClientError("TOOL_CANCELLED", "Endpoint tool call was cancelled");
|
|
550
|
+
}
|
|
551
|
+
if (this.now() >= deadlineAt) {
|
|
552
|
+
throw new EndpointToolClientError("TOOL_TIMEOUT", "Endpoint tool deadline expired");
|
|
553
|
+
}
|
|
554
|
+
if (descriptor.requiresForeground && this.options.getAvailability() !== "foreground") {
|
|
555
|
+
throw new EndpointToolClientError("ENDPOINT_NOT_FOREGROUND", "Endpoint is not foreground");
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
isCurrent(invocationId, invocation) {
|
|
559
|
+
return this.active.get(invocationId) === invocation && invocation.generation === this.generation && this.sendMessage !== void 0;
|
|
560
|
+
}
|
|
561
|
+
normalizeError(error) {
|
|
562
|
+
if (error instanceof EndpointToolClientError) return error;
|
|
563
|
+
if (error instanceof TypeError) {
|
|
564
|
+
return new EndpointToolClientError("INVALID_ARGUMENTS", error.message);
|
|
565
|
+
}
|
|
566
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
567
|
+
return new EndpointToolClientError("USER_DENIED", error.message);
|
|
568
|
+
}
|
|
569
|
+
if (error instanceof Error && error.name === "NotAllowedError") {
|
|
570
|
+
return new EndpointToolClientError("PERMISSION_DENIED", error.message);
|
|
571
|
+
}
|
|
572
|
+
return new EndpointToolClientError(
|
|
573
|
+
"PROTOCOL_ERROR",
|
|
574
|
+
error instanceof Error ? error.message : String(error)
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
sendError(invocationId, code, message) {
|
|
578
|
+
this.send("tool.error", { invocationId, code, message });
|
|
579
|
+
}
|
|
580
|
+
send(type, payload) {
|
|
581
|
+
this.sendMessage?.({
|
|
582
|
+
protocolVersion: ENDPOINT_PROTOCOL_VERSION,
|
|
583
|
+
messageId: this.options.createMessageId(),
|
|
584
|
+
type,
|
|
585
|
+
sentAt: this.now(),
|
|
586
|
+
payload
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
}
|
|
24
590
|
|
|
25
591
|
const LOG_COLORS = {
|
|
26
592
|
debug: "color: #888",
|
|
@@ -320,16 +886,6 @@ async function resolveAttachedTarget(input) {
|
|
|
320
886
|
}
|
|
321
887
|
async function executeBrowserCommand(command) {
|
|
322
888
|
const startedAt = Date.now();
|
|
323
|
-
if (command.protocolVersion !== BROWSER_EXTENSION_PROTOCOL_VERSION) {
|
|
324
|
-
return {
|
|
325
|
-
id: command.id,
|
|
326
|
-
connectionId: command.connectionId,
|
|
327
|
-
result: fail(
|
|
328
|
-
"DRIVER_UNAVAILABLE",
|
|
329
|
-
`Browser protocol mismatch. Expected ${BROWSER_EXTENSION_PROTOCOL_VERSION}; reload the xopc extension.`
|
|
330
|
-
)
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
889
|
try {
|
|
334
890
|
const result = await execute(command.input, command.timeoutMs, command.visualFallback, startedAt);
|
|
335
891
|
return { id: command.id, connectionId: command.connectionId, result };
|
|
@@ -586,145 +1142,125 @@ function diff(previous, next) {
|
|
|
586
1142
|
}
|
|
587
1143
|
|
|
588
1144
|
const log = createLogger("Background");
|
|
589
|
-
|
|
590
|
-
let
|
|
591
|
-
let
|
|
592
|
-
let
|
|
593
|
-
let
|
|
594
|
-
let
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
connectionId: challenge.connectionId,
|
|
608
|
-
challenge: challenge.challenge,
|
|
609
|
-
issuedAt: challenge.issuedAt,
|
|
610
|
-
principalId: profile.deviceId,
|
|
611
|
-
extensionId: chrome.runtime.id,
|
|
612
|
-
extensionVersion
|
|
613
|
-
};
|
|
614
|
-
const message = {
|
|
615
|
-
type: "authenticate",
|
|
616
|
-
protocolVersion: BROWSER_EXTENSION_PROTOCOL_VERSION,
|
|
617
|
-
connectionId: challenge.connectionId,
|
|
618
|
-
principalId: profile.deviceId,
|
|
619
|
-
extensionId: chrome.runtime.id,
|
|
620
|
-
extensionVersion,
|
|
621
|
-
signature: await signBrowserBridgeChallenge(browserWireAuthenticationPayload(unsigned))
|
|
622
|
-
};
|
|
623
|
-
socket.send(JSON.stringify(message));
|
|
624
|
-
authenticatedConnectionId = challenge.connectionId;
|
|
625
|
-
sendStatus(profile.deviceId);
|
|
626
|
-
startKeepAlive();
|
|
627
|
-
}
|
|
628
|
-
async function connect() {
|
|
629
|
-
if (!await readProfile()) return;
|
|
630
|
-
if (ws && (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING)) return;
|
|
631
|
-
intentionalClose = false;
|
|
632
|
-
authenticatedConnectionId = null;
|
|
633
|
-
const socket = new WebSocket(XOPC_EXT_WS_URL);
|
|
634
|
-
ws = socket;
|
|
635
|
-
socket.onopen = () => {
|
|
636
|
-
if (ws !== socket) socket.close();
|
|
637
|
-
};
|
|
638
|
-
socket.onmessage = async (event) => {
|
|
639
|
-
if (ws !== socket) return;
|
|
640
|
-
try {
|
|
641
|
-
const value = JSON.parse(String(event.data));
|
|
642
|
-
if (value.type === "auth_challenge") {
|
|
643
|
-
await authenticate(socket, value);
|
|
644
|
-
reconnectDelay = WS_RECONNECT_BASE_DELAY;
|
|
645
|
-
log.info("Authenticated with xopc browser bridge");
|
|
646
|
-
return;
|
|
647
|
-
}
|
|
648
|
-
if (!authenticatedConnectionId || value.connectionId !== authenticatedConnectionId) {
|
|
649
|
-
throw new Error("Browser command arrived before authentication or for a stale connection");
|
|
650
|
-
}
|
|
651
|
-
const result = await executeBrowserCommand(value);
|
|
652
|
-
if (socket.readyState === WebSocket.OPEN) socket.send(JSON.stringify(result));
|
|
653
|
-
} catch (error) {
|
|
654
|
-
log.error("Failed to process browser bridge message", {
|
|
655
|
-
error: error instanceof Error ? error.message : String(error)
|
|
656
|
-
});
|
|
657
|
-
}
|
|
658
|
-
};
|
|
659
|
-
socket.onclose = (event) => {
|
|
660
|
-
if (ws !== socket) return;
|
|
661
|
-
stopKeepAlive();
|
|
662
|
-
ws = null;
|
|
663
|
-
authenticatedConnectionId = null;
|
|
664
|
-
log.warn("Disconnected from xopc browser bridge", {
|
|
665
|
-
code: event.code,
|
|
666
|
-
reason: event.reason,
|
|
667
|
-
wasClean: event.wasClean
|
|
1145
|
+
const BACKGROUND_CLIENT_ID_KEY = "xopc.browser.background-client-id";
|
|
1146
|
+
let realtime;
|
|
1147
|
+
let endpointClaim;
|
|
1148
|
+
let lastConnectionError;
|
|
1149
|
+
let connectTask;
|
|
1150
|
+
let connectionGeneration = 0;
|
|
1151
|
+
const browserToolRegistry = new EndpointToolRegistry([{
|
|
1152
|
+
descriptor: BROWSER_CONTROL_ENDPOINT_DESCRIPTOR,
|
|
1153
|
+
execute: async (args) => {
|
|
1154
|
+
const input = args.input;
|
|
1155
|
+
if (!input || typeof input !== "object") throw new TypeError("Browser control input is required");
|
|
1156
|
+
const result = await executeBrowserCommand({
|
|
1157
|
+
id: crypto.randomUUID(),
|
|
1158
|
+
protocolVersion: BROWSER_EXTENSION_PROTOCOL_VERSION,
|
|
1159
|
+
connectionId: "gateway-realtime",
|
|
1160
|
+
input,
|
|
1161
|
+
timeoutMs: typeof args.timeoutMs === "number" ? args.timeoutMs : 3e4,
|
|
1162
|
+
visualFallback: args.visualFallback !== false
|
|
668
1163
|
});
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
1164
|
+
return { content: [{ type: "json", value: result.result }] };
|
|
1165
|
+
}
|
|
1166
|
+
}]);
|
|
1167
|
+
const endpointHost = new EndpointToolHostController({
|
|
1168
|
+
registry: browserToolRegistry,
|
|
1169
|
+
getAvailability: () => "background",
|
|
1170
|
+
confirm: async () => false,
|
|
1171
|
+
uploadFile: async () => {
|
|
1172
|
+
throw new Error("Browser control does not upload endpoint files");
|
|
1173
|
+
},
|
|
1174
|
+
createMessageId: () => crypto.randomUUID()
|
|
1175
|
+
});
|
|
1176
|
+
async function backgroundClientId() {
|
|
1177
|
+
const stored = await chrome.storage.local.get(BACKGROUND_CLIENT_ID_KEY);
|
|
1178
|
+
if (typeof stored[BACKGROUND_CLIENT_ID_KEY] === "string") return stored[BACKGROUND_CLIENT_ID_KEY];
|
|
1179
|
+
const value = crypto.randomUUID();
|
|
1180
|
+
await chrome.storage.local.set({ [BACKGROUND_CLIENT_ID_KEY]: value });
|
|
1181
|
+
return value;
|
|
684
1182
|
}
|
|
685
|
-
function
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
1183
|
+
async function connect(generation) {
|
|
1184
|
+
if (realtime || !await readProfile()) return;
|
|
1185
|
+
await registerBrowserEndpoint();
|
|
1186
|
+
const profile = await getAccessProfile();
|
|
1187
|
+
const id = await backgroundClientId();
|
|
1188
|
+
const client = new RealtimeClient({
|
|
1189
|
+
clientId: id,
|
|
1190
|
+
clientKind: "browser_extension",
|
|
1191
|
+
getWebSocketUrl: () => {
|
|
1192
|
+
const url = new URL(profile.gatewayUrl);
|
|
1193
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
1194
|
+
url.pathname = "/api/realtime/v1/ws";
|
|
1195
|
+
url.search = "";
|
|
1196
|
+
url.hash = "";
|
|
1197
|
+
return url.toString();
|
|
1198
|
+
},
|
|
1199
|
+
issueTicket: async (signal) => {
|
|
1200
|
+
const response = await gatewayFetch("/api/realtime/tickets", {
|
|
1201
|
+
method: "POST",
|
|
1202
|
+
headers: { "Content-Type": "application/json" },
|
|
1203
|
+
body: JSON.stringify({ clientId: id, clientKind: "browser_extension" }),
|
|
1204
|
+
signal
|
|
1205
|
+
});
|
|
1206
|
+
const body = await response.json();
|
|
1207
|
+
if (!response.ok || !body.payload?.ticket) throw new Error(`Realtime ticket failed (${response.status})`);
|
|
1208
|
+
return body.payload.ticket;
|
|
1209
|
+
},
|
|
1210
|
+
createWebSocket: (url) => new WebSocket(url),
|
|
1211
|
+
onStateChange: (state, error) => {
|
|
1212
|
+
lastConnectionError = error;
|
|
1213
|
+
if (state === "connected") lastConnectionError = void 0;
|
|
1214
|
+
if (error) log.warn("Browser Realtime connection changed", { state, error });
|
|
691
1215
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
1216
|
+
});
|
|
1217
|
+
client.setEndpoint({
|
|
1218
|
+
createHello: () => createBrowserEndpointHello(browserToolRegistry.descriptors()),
|
|
1219
|
+
onReady: ({ endpointId, turnToken }) => {
|
|
1220
|
+
endpointClaim = { endpointId, token: turnToken };
|
|
1221
|
+
lastConnectionError = void 0;
|
|
1222
|
+
endpointHost.connect((message) => client.sendEndpointMessage(message));
|
|
1223
|
+
void chrome.runtime.sendMessage({ type: "browser/endpoint-ready", claim: endpointClaim }).catch(() => void 0);
|
|
1224
|
+
log.info("Browser control connected through Gateway Realtime", { endpointId });
|
|
1225
|
+
},
|
|
1226
|
+
onMessage: (message) => {
|
|
1227
|
+
void endpointHost.handleMessage(message);
|
|
1228
|
+
},
|
|
1229
|
+
onDisconnected: () => {
|
|
1230
|
+
endpointClaim = void 0;
|
|
1231
|
+
endpointHost.disconnect();
|
|
1232
|
+
}
|
|
1233
|
+
});
|
|
1234
|
+
if (generation !== connectionGeneration || realtime) return;
|
|
1235
|
+
realtime = client;
|
|
1236
|
+
client.connect();
|
|
711
1237
|
}
|
|
712
1238
|
function disconnect() {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
1239
|
+
connectionGeneration += 1;
|
|
1240
|
+
realtime?.disconnect();
|
|
1241
|
+
realtime = void 0;
|
|
1242
|
+
endpointClaim = void 0;
|
|
1243
|
+
endpointHost.disconnect();
|
|
1244
|
+
}
|
|
1245
|
+
function connectWithLogging() {
|
|
1246
|
+
if (connectTask) return;
|
|
1247
|
+
const generation = connectionGeneration;
|
|
1248
|
+
connectTask = connect(generation).catch((error) => {
|
|
1249
|
+
lastConnectionError = error instanceof Error ? error.message : String(error);
|
|
1250
|
+
log.warn("Could not connect browser Realtime transport", { error: lastConnectionError });
|
|
1251
|
+
}).finally(() => {
|
|
1252
|
+
connectTask = void 0;
|
|
1253
|
+
if (generation !== connectionGeneration && !realtime) connectWithLogging();
|
|
1254
|
+
});
|
|
720
1255
|
}
|
|
721
1256
|
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|
722
1257
|
if (message.type === "browser/get-status") {
|
|
723
|
-
sendResponse({ connected: Boolean(
|
|
1258
|
+
sendResponse({ connected: Boolean(endpointClaim), transport: "gateway-realtime", error: lastConnectionError });
|
|
1259
|
+
} else if (message.type === "browser/get-endpoint-claim") {
|
|
1260
|
+
sendResponse({ claim: endpointClaim, error: lastConnectionError });
|
|
724
1261
|
} else if (message.type === "browser/reconnect") {
|
|
725
1262
|
disconnect();
|
|
726
|
-
|
|
727
|
-
void connect();
|
|
1263
|
+
connectWithLogging();
|
|
728
1264
|
sendResponse({ ok: true });
|
|
729
1265
|
} else {
|
|
730
1266
|
sendResponse({ ok: false, error: `Unknown message: ${message.type}` });
|
|
@@ -752,21 +1288,14 @@ chrome.runtime.onInstalled.addListener(() => {
|
|
|
752
1288
|
contexts: ["selection"]
|
|
753
1289
|
});
|
|
754
1290
|
});
|
|
755
|
-
|
|
756
|
-
});
|
|
757
|
-
chrome.runtime.onStartup.addListener(() => {
|
|
758
|
-
void connect();
|
|
1291
|
+
connectWithLogging();
|
|
759
1292
|
});
|
|
1293
|
+
chrome.runtime.onStartup.addListener(connectWithLogging);
|
|
760
1294
|
chrome.storage.onChanged.addListener((changes, area) => {
|
|
761
1295
|
if (area === "local" && changes["xopc.browser.profile"]) {
|
|
762
1296
|
disconnect();
|
|
763
|
-
|
|
764
|
-
void connect();
|
|
1297
|
+
connectWithLogging();
|
|
765
1298
|
}
|
|
766
1299
|
});
|
|
767
|
-
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
768
|
-
if (alarm.name === WS_WATCHDOG_ALARM && !intentionalClose) void connect();
|
|
769
|
-
});
|
|
770
|
-
void chrome.alarms.create(WS_WATCHDOG_ALARM, { periodInMinutes: WS_WATCHDOG_PERIOD_MINUTES });
|
|
771
1300
|
void chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
|
|
772
|
-
|
|
1301
|
+
connectWithLogging();
|