@openclaw/copilot 2026.10.10-beta.5 → 2026.11.5-beta.1
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 +18 -0
- package/dist/.setup/{attempt-Bu_STDE0.mjs → attempt-BdJgfQV3.mjs} +469 -666
- package/dist/.setup/{harness-Je_2Cj-D.mjs → harness-Djqx8viF.mjs} +190 -282
- package/dist/.setup/{isolated-completion-CrN9i842.mjs → isolated-completion-Dp_YaEKF.mjs} +21 -42
- package/dist/.setup/runtime-CAM77uEc.mjs +152 -0
- package/dist/.setup/runtime-artifact-D42RfCpG.mjs +55 -0
- package/dist/.setup/sdk-loader-BXVlNPFC.mjs +144 -0
- package/dist/.setup/session-restrictions-BrC08l2U.mjs +223 -0
- package/dist/.source-module-urls/f7f35489a89dbc0fc6d0e80a0da5720af5de227b511e490a2415ffe55b1bc471.json +1 -1
- package/dist/doctor-contract-api.js +1 -2
- package/dist/harness.js +1 -1
- package/dist/index.js +2 -3
- package/package.json +5 -5
- package/dist/.setup/runtime-C0R_TFVt.mjs +0 -219
- package/dist/.setup/usage-bridge--rLPdCvV.mjs +0 -76
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { a as createCopilotByokProxy, c as tokenFingerprint, n as resolveCopilotProvider, r as createCopilotAbortError } from "./harness-
|
|
2
|
-
import {
|
|
3
|
-
import { asNonArrayRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
1
|
+
import { a as createCopilotByokProxy, c as tokenFingerprint, n as resolveCopilotProvider, r as createCopilotAbortError } from "./harness-Djqx8viF.mjs";
|
|
2
|
+
import { n as buildAssistantMessage, t as createCopilotIsolatedSessionRestrictions } from "./session-restrictions-BrC08l2U.mjs";
|
|
4
3
|
import { resolve } from "node:path";
|
|
5
4
|
import { raceWithTimeout } from "openclaw/plugin-sdk/time-runtime";
|
|
6
|
-
|
|
5
|
+
// extensions/copilot/src/isolated-completion.ts
|
|
7
6
|
function startBestEffortCleanup(cleanup) {
|
|
8
7
|
try {
|
|
9
8
|
cleanup().catch(() => void 0);
|
|
@@ -20,7 +19,7 @@ function createTimeoutError(timeoutMs) {
|
|
|
20
19
|
async function awaitWithinCompletionBoundary(params) {
|
|
21
20
|
const signal = params.boundary.abortSignal;
|
|
22
21
|
if (signal?.aborted) throw createCopilotAbortError(signal.reason);
|
|
23
|
-
const remainingMs = params.boundary.deadlineMs -
|
|
22
|
+
const remainingMs = params.boundary.deadlineMs - performance.now();
|
|
24
23
|
if (remainingMs <= 0) throw createTimeoutError(params.boundary.timeoutMs);
|
|
25
24
|
let boundaryError;
|
|
26
25
|
const rejectBoundary = (error) => {
|
|
@@ -62,7 +61,7 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
62
61
|
const boundary = {
|
|
63
62
|
abortSignal: params.abortSignal,
|
|
64
63
|
assertCurrent: params.assertCurrent,
|
|
65
|
-
deadlineMs:
|
|
64
|
+
deadlineMs: performance.now() + params.timeoutMs,
|
|
66
65
|
timeoutMs: params.timeoutMs
|
|
67
66
|
};
|
|
68
67
|
if (params.authorization.owner !== "host") throw new Error("[copilot] isolated completion requires host-prepared authorization");
|
|
@@ -92,7 +91,7 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
92
91
|
});
|
|
93
92
|
const byokProxy = await awaitWithinCompletionBoundary({
|
|
94
93
|
boundary,
|
|
95
|
-
start:
|
|
94
|
+
start: () => createCopilotByokProxy(resolvedProvider),
|
|
96
95
|
cleanupLate: async (proxy) => await proxy?.close()
|
|
97
96
|
});
|
|
98
97
|
const sessionProvider = byokProxy?.provider ?? resolvedProvider;
|
|
@@ -105,7 +104,7 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
105
104
|
try {
|
|
106
105
|
const acquiredHandle = await awaitWithinCompletionBoundary({
|
|
107
106
|
boundary,
|
|
108
|
-
start:
|
|
107
|
+
start: () => pool.acquire({
|
|
109
108
|
agentId: params.agentId,
|
|
110
109
|
authMode: githubAuth ? "gitHubToken" : "byok",
|
|
111
110
|
authProfileId,
|
|
@@ -118,7 +117,7 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
118
117
|
useLoggedInUser: false,
|
|
119
118
|
...githubAuth ? { gitHubToken: apiKey } : {}
|
|
120
119
|
}),
|
|
121
|
-
cleanupLate:
|
|
120
|
+
cleanupLate: (lateHandle) => pool.release(lateHandle)
|
|
122
121
|
});
|
|
123
122
|
handle = acquiredHandle;
|
|
124
123
|
const sessionConfig = {
|
|
@@ -135,17 +134,17 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
135
134
|
};
|
|
136
135
|
const createdSession = await awaitWithinCompletionBoundary({
|
|
137
136
|
boundary,
|
|
138
|
-
start:
|
|
137
|
+
start: () => acquiredHandle.client.createSession(sessionConfig),
|
|
139
138
|
cleanupLate: async (lateSession) => {
|
|
140
|
-
startBestEffortCleanup(
|
|
141
|
-
startBestEffortCleanup(
|
|
139
|
+
startBestEffortCleanup(() => lateSession.abort());
|
|
140
|
+
startBestEffortCleanup(() => lateSession.disconnect());
|
|
142
141
|
}
|
|
143
142
|
});
|
|
144
143
|
session = createdSession;
|
|
145
144
|
const requestHeaders = sessionProvider.provider?.headers;
|
|
146
145
|
const event = await awaitWithinCompletionBoundary({
|
|
147
146
|
boundary,
|
|
148
|
-
start:
|
|
147
|
+
start: (remainingMs) => createdSession.sendAndWait({
|
|
149
148
|
prompt: params.prompt,
|
|
150
149
|
...requestHeaders ? { requestHeaders } : {}
|
|
151
150
|
}, remainingMs),
|
|
@@ -154,42 +153,22 @@ async function runCopilotIsolatedCompletion(params, getPool) {
|
|
|
154
153
|
}
|
|
155
154
|
});
|
|
156
155
|
if (event?.type !== "assistant.message" || event.agentId !== void 0) throw new Error("[copilot] isolated completion did not return a root assistant message");
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
type: "text",
|
|
164
|
-
text: event.data.content
|
|
165
|
-
});
|
|
166
|
-
for (const toolRequest of event.data.toolRequests ?? []) content.push({
|
|
167
|
-
type: "toolCall",
|
|
168
|
-
id: toolRequest.toolCallId,
|
|
169
|
-
name: toolRequest.name,
|
|
170
|
-
arguments: { ...asNonArrayRecord(toolRequest.arguments) }
|
|
171
|
-
});
|
|
172
|
-
return { assistant: {
|
|
173
|
-
role: "assistant",
|
|
174
|
-
content,
|
|
175
|
-
api: model.api,
|
|
176
|
-
provider: model.provider,
|
|
177
|
-
model: event.data.model ?? model.id,
|
|
178
|
-
stopReason: event.data.toolRequests?.length ? "toolUse" : "stop",
|
|
179
|
-
timestamp: Date.now(),
|
|
180
|
-
usage: buildCopilotAssistantUsage({ fallbackOutputTokens: event.data.outputTokens })
|
|
181
|
-
} };
|
|
156
|
+
return { assistant: buildAssistantMessage({
|
|
157
|
+
assistantTexts: [],
|
|
158
|
+
event,
|
|
159
|
+
modelRef: model,
|
|
160
|
+
now: Date.now
|
|
161
|
+
}) };
|
|
182
162
|
} finally {
|
|
183
163
|
if (session) {
|
|
184
164
|
const sessionToClose = session;
|
|
185
|
-
startBestEffortCleanup(
|
|
165
|
+
startBestEffortCleanup(() => sessionToClose.disconnect());
|
|
186
166
|
}
|
|
187
|
-
if (byokProxy) startBestEffortCleanup(
|
|
167
|
+
if (byokProxy) startBestEffortCleanup(() => byokProxy.close());
|
|
188
168
|
if (handle) {
|
|
189
169
|
const handleToRelease = handle;
|
|
190
|
-
startBestEffortCleanup(
|
|
170
|
+
startBestEffortCleanup(() => pool.release(handleToRelease));
|
|
191
171
|
}
|
|
192
172
|
}
|
|
193
173
|
}
|
|
194
|
-
//#endregion
|
|
195
174
|
export { runCopilotIsolatedCompletion };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { n as loadCopilotSdk } from "./sdk-loader-BXVlNPFC.mjs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { toStringifiedError } from "openclaw/plugin-sdk/error-runtime";
|
|
4
|
+
// extensions/copilot/src/runtime.ts
|
|
5
|
+
const DEFAULT_IDLE_TTL_MS = 3e5;
|
|
6
|
+
const POOL_DISPOSED_MESSAGE = "[copilot-pool] pool disposed";
|
|
7
|
+
function createCopilotClientPool(options = {}) {
|
|
8
|
+
const sdkFactory = options.sdkFactory ?? (async (clientOptions) => {
|
|
9
|
+
return new (await (loadCopilotSdk())).CopilotClient(clientOptions);
|
|
10
|
+
});
|
|
11
|
+
const idleTtlMs = options.idleTtlMs ?? DEFAULT_IDLE_TTL_MS;
|
|
12
|
+
const entries = /* @__PURE__ */ new Map();
|
|
13
|
+
const releasedHandles = /* @__PURE__ */ new WeakSet();
|
|
14
|
+
let disposePromise;
|
|
15
|
+
const createDisposedError = () => /* @__PURE__ */ new Error(POOL_DISPOSED_MESSAGE);
|
|
16
|
+
const maybeDeleteEntry = (entry) => {
|
|
17
|
+
if (entries.get(entry.cacheKey) === entry) entries.delete(entry.cacheKey);
|
|
18
|
+
};
|
|
19
|
+
const stopEntry = (entry) => {
|
|
20
|
+
if (entry.stopping) return entry.stopping;
|
|
21
|
+
clearTimeout(entry.idleTimer);
|
|
22
|
+
entry.stopping = (async () => {
|
|
23
|
+
try {
|
|
24
|
+
return await (await entry.client).stop();
|
|
25
|
+
} catch (error) {
|
|
26
|
+
return [toStringifiedError(error)];
|
|
27
|
+
} finally {
|
|
28
|
+
maybeDeleteEntry(entry);
|
|
29
|
+
}
|
|
30
|
+
})();
|
|
31
|
+
return entry.stopping;
|
|
32
|
+
};
|
|
33
|
+
const createEntry = (key, cacheKey, clientOptions, runtimeArtifact) => {
|
|
34
|
+
const entry = {
|
|
35
|
+
key,
|
|
36
|
+
cacheKey,
|
|
37
|
+
refCount: 0,
|
|
38
|
+
client: Promise.resolve().then(async () => {
|
|
39
|
+
try {
|
|
40
|
+
const client = await sdkFactory(clientOptions);
|
|
41
|
+
if (runtimeArtifact) try {
|
|
42
|
+
await client.start();
|
|
43
|
+
const { validateCopilotRuntimeArtifact } = await import("./runtime-artifact-D42RfCpG.mjs");
|
|
44
|
+
if (!await validateCopilotRuntimeArtifact(runtimeArtifact)) throw new Error("[copilot] runtime changed while starting; retry setup verification");
|
|
45
|
+
} catch (error) {
|
|
46
|
+
await client.stop().catch(() => void 0);
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
return client;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
maybeDeleteEntry(entry);
|
|
52
|
+
throw toStringifiedError(error);
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
entries.set(cacheKey, entry);
|
|
57
|
+
return entry;
|
|
58
|
+
};
|
|
59
|
+
const acquire = async (inputKey, optionsForCreate, runtimeArtifactRequest) => {
|
|
60
|
+
if (runtimeArtifactRequest && optionsForCreate.connection) throw new Error("[copilot] runtime verification requires the packaged stdio runtime");
|
|
61
|
+
const capture = runtimeArtifactRequest ? await (await import("./runtime-artifact-D42RfCpG.mjs")).captureCopilotRuntimeArtifact(optionsForCreate.env) : void 0;
|
|
62
|
+
const expected = runtimeArtifactRequest?.expected;
|
|
63
|
+
if (expected && (capture?.binding.id !== expected.id || capture.binding.fingerprint !== expected.fingerprint)) throw new Error("[copilot] verified runtime changed; rerun setup verification");
|
|
64
|
+
const runtimeArtifact = capture?.binding;
|
|
65
|
+
const clientMode = optionsForCreate.mode;
|
|
66
|
+
const key = {
|
|
67
|
+
agentId: inputKey.agentId,
|
|
68
|
+
copilotHome: normalizeCopilotHome(optionsForCreate.copilotHome),
|
|
69
|
+
authMode: inputKey.authMode,
|
|
70
|
+
authProfileId: inputKey.authProfileId,
|
|
71
|
+
authProfileVersion: inputKey.authProfileVersion,
|
|
72
|
+
...clientMode === "empty" ? { clientMode } : {},
|
|
73
|
+
...runtimeArtifact ? { runtimeArtifactFingerprint: runtimeArtifact.fingerprint } : {}
|
|
74
|
+
};
|
|
75
|
+
const cacheKey = JSON.stringify(key);
|
|
76
|
+
const clientOptions = normalizeClientCreateOptions(optionsForCreate, key.copilotHome);
|
|
77
|
+
if (capture) clientOptions.connection = capture.connection;
|
|
78
|
+
const toHandle = (client) => ({
|
|
79
|
+
key,
|
|
80
|
+
client,
|
|
81
|
+
...runtimeArtifact ? { runtimeArtifact } : {}
|
|
82
|
+
});
|
|
83
|
+
while (true) {
|
|
84
|
+
if (disposePromise) throw createDisposedError();
|
|
85
|
+
const entry = entries.get(cacheKey) ?? createEntry(key, cacheKey, clientOptions, runtimeArtifact);
|
|
86
|
+
if (entry.stopping) {
|
|
87
|
+
await entry.stopping;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
clearTimeout(entry.idleTimer);
|
|
91
|
+
entry.refCount += 1;
|
|
92
|
+
const client = await entry.client;
|
|
93
|
+
if (disposePromise) {
|
|
94
|
+
await stopEntry(entry);
|
|
95
|
+
throw createDisposedError();
|
|
96
|
+
}
|
|
97
|
+
return toHandle(client);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const release = async (handle) => {
|
|
101
|
+
if (releasedHandles.has(handle)) return;
|
|
102
|
+
releasedHandles.add(handle);
|
|
103
|
+
const entry = entries.get(JSON.stringify(handle.key));
|
|
104
|
+
if (!entry || entry.stopping || await entry.client !== handle.client) return;
|
|
105
|
+
entry.refCount -= 1;
|
|
106
|
+
if (entry.refCount > 0) return;
|
|
107
|
+
if (disposePromise) {
|
|
108
|
+
await stopEntry(entry);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
entry.idleTimer = setTimeout(() => {
|
|
112
|
+
stopEntry(entry);
|
|
113
|
+
}, idleTtlMs);
|
|
114
|
+
};
|
|
115
|
+
const dispose = async () => {
|
|
116
|
+
if (disposePromise) {
|
|
117
|
+
await disposePromise;
|
|
118
|
+
return [];
|
|
119
|
+
}
|
|
120
|
+
const snapshot = [...entries.values()];
|
|
121
|
+
for (const entry of snapshot) clearTimeout(entry.idleTimer);
|
|
122
|
+
disposePromise = (async () => {
|
|
123
|
+
const errors = [];
|
|
124
|
+
for (const entry of snapshot) {
|
|
125
|
+
const stopErrors = await stopEntry(entry);
|
|
126
|
+
errors.push(...stopErrors);
|
|
127
|
+
}
|
|
128
|
+
entries.clear();
|
|
129
|
+
return errors;
|
|
130
|
+
})();
|
|
131
|
+
return await disposePromise;
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
acquire,
|
|
135
|
+
release,
|
|
136
|
+
dispose,
|
|
137
|
+
size: () => entries.size
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function normalizeClientCreateOptions(options, normalizedCopilotHome) {
|
|
141
|
+
const { copilotHome: _copilotHome, ...clientOptions } = options;
|
|
142
|
+
return {
|
|
143
|
+
...clientOptions,
|
|
144
|
+
baseDirectory: normalizedCopilotHome
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function normalizeCopilotHome(copilotHome) {
|
|
148
|
+
let normalizedHome = resolve(copilotHome);
|
|
149
|
+
if (process.platform === "win32") normalizedHome = normalizedHome.toLowerCase();
|
|
150
|
+
return normalizedHome;
|
|
151
|
+
}
|
|
152
|
+
export { createCopilotClientPool };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { r as loadCopilotSdkWithIdentity, t as fingerprintCopilotPackage } from "./sdk-loader-BXVlNPFC.mjs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import fsp from "node:fs/promises";
|
|
7
|
+
import { existsSync } from "node:fs";
|
|
8
|
+
// extensions/copilot/src/runtime-artifact.ts
|
|
9
|
+
function runtimePlatform() {
|
|
10
|
+
if (process.arch !== "x64" && process.arch !== "arm64") throw new Error(`Copilot runtime artifacts do not support architecture ${process.arch}.`);
|
|
11
|
+
if (process.platform === "linux") {
|
|
12
|
+
const report = process.report?.getReport();
|
|
13
|
+
return `${(isRecord(report) && isRecord(report.header) ? report.header : void 0)?.glibcVersionRuntime ? "linux" : "linuxmusl"}-${process.arch}`;
|
|
14
|
+
}
|
|
15
|
+
if (process.platform === "darwin" || process.platform === "win32") return `${process.platform}-${process.arch}`;
|
|
16
|
+
throw new Error(`Copilot runtime artifacts do not support platform ${process.platform}.`);
|
|
17
|
+
}
|
|
18
|
+
async function resolveRuntimePackage(entryPath, packageName) {
|
|
19
|
+
const packageRoot = (createRequire(entryPath).resolve.paths(packageName) ?? []).map((lookupPath) => path.join(lookupPath, packageName)).find((candidate) => existsSync(path.join(candidate, "package.json")));
|
|
20
|
+
if (!packageRoot) throw new Error(`Copilot runtime package ${packageName} is not installed.`);
|
|
21
|
+
return await fsp.realpath(packageRoot);
|
|
22
|
+
}
|
|
23
|
+
async function captureCopilotRuntimeArtifact(env = process.env) {
|
|
24
|
+
const defaultConnection = process.env.COPILOT_SDK_DEFAULT_CONNECTION;
|
|
25
|
+
if (defaultConnection && defaultConnection.toLowerCase() !== "stdio") throw new Error("Copilot runtime artifact verification requires the stdio transport.");
|
|
26
|
+
const { sdk, identity } = await loadCopilotSdkWithIdentity();
|
|
27
|
+
if (await fingerprintCopilotPackage(identity.packageRoot) !== identity.fingerprint) throw new Error("Copilot SDK changed after loading. Restart OpenClaw before verifying inference.");
|
|
28
|
+
const platform = runtimePlatform();
|
|
29
|
+
const packageName = `@github/copilot-sdk-${platform}`;
|
|
30
|
+
const packageRoot = await resolveRuntimePackage(identity.entryPath, packageName);
|
|
31
|
+
const executable = await fsp.realpath(path.join(packageRoot, "prebuilds", platform, process.platform === "win32" ? "copilot-runtime.exe" : "copilot-runtime"));
|
|
32
|
+
if (env.COPILOT_CLI_PATH && await fsp.realpath(env.COPILOT_CLI_PATH) !== executable) throw new Error("Copilot runtime artifact verification does not support a custom COPILOT_CLI_PATH.");
|
|
33
|
+
const runtimeFingerprint = await fingerprintCopilotPackage(packageRoot);
|
|
34
|
+
const id = `copilot-sdk:${platform}:${path.relative(identity.packageRoot, identity.entryPath).split(path.sep).join("/")}`;
|
|
35
|
+
return {
|
|
36
|
+
binding: {
|
|
37
|
+
id,
|
|
38
|
+
fingerprint: createHash("sha256").update(JSON.stringify([
|
|
39
|
+
id,
|
|
40
|
+
identity.fingerprint,
|
|
41
|
+
runtimeFingerprint
|
|
42
|
+
])).digest("hex")
|
|
43
|
+
},
|
|
44
|
+
connection: sdk.RuntimeConnection.forStdio({ path: executable })
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async function validateCopilotRuntimeArtifact(binding) {
|
|
48
|
+
try {
|
|
49
|
+
const current = await captureCopilotRuntimeArtifact();
|
|
50
|
+
return current.binding.id === binding.id && current.binding.fingerprint === binding.fingerprint;
|
|
51
|
+
} catch {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export { captureCopilotRuntimeArtifact, validateCopilotRuntimeArtifact };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { materializeErrorStack } from "openclaw/plugin-sdk/error-runtime";
|
|
5
|
+
import fsp from "node:fs/promises";
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
8
|
+
import { sha256File } from "openclaw/plugin-sdk/file-access-runtime";
|
|
9
|
+
import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";
|
|
10
|
+
// extensions/copilot/src/sdk-loader.ts
|
|
11
|
+
const COPILOT_SDK_SPEC = `@github/copilot-sdk@${{ "@github/copilot-sdk": "1.0.23" }["@github/copilot-sdk"]}`;
|
|
12
|
+
let cached;
|
|
13
|
+
const installedImports = /* @__PURE__ */ new Map();
|
|
14
|
+
function unchanged(before, after) {
|
|
15
|
+
return before.dev === after.dev && before.ino === after.ino && before.mode === after.mode && before.size === after.size && before.mtimeNs === after.mtimeNs && before.ctimeNs === after.ctimeNs;
|
|
16
|
+
}
|
|
17
|
+
/** Installed package bytes, excluding separately resolved dependency installations. */
|
|
18
|
+
async function fingerprintCopilotPackage(packageRoot) {
|
|
19
|
+
const hash = createHash("sha256");
|
|
20
|
+
async function visit(directory) {
|
|
21
|
+
const entries = await fsp.readdir(directory, { withFileTypes: true });
|
|
22
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (entry.name === "node_modules") continue;
|
|
25
|
+
const filePath = path.join(directory, entry.name);
|
|
26
|
+
if (entry.isDirectory()) await visit(filePath);
|
|
27
|
+
else if (entry.isFile() || entry.isSymbolicLink()) {
|
|
28
|
+
const target = await fsp.realpath(filePath);
|
|
29
|
+
const before = await fsp.stat(target, { bigint: true });
|
|
30
|
+
const contents = await sha256File(target, { maxBytes: Number(before.size) });
|
|
31
|
+
if (await fsp.realpath(filePath) !== target || !unchanged(before, await fsp.stat(target, { bigint: true })) || BigInt(contents.bytes) !== before.size) throw new Error("Copilot runtime package changed while fingerprinting.");
|
|
32
|
+
const relativePath = path.relative(packageRoot, filePath).split(path.sep).join("/");
|
|
33
|
+
hash.update(`${relativePath}\0${before.mode}\0${contents.digest}\0`);
|
|
34
|
+
} else throw new Error(`Copilot runtime package contains an unsupported file: ${filePath}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
await visit(packageRoot);
|
|
38
|
+
return hash.digest("hex");
|
|
39
|
+
}
|
|
40
|
+
async function importInstalledSdk(selectedEntry) {
|
|
41
|
+
const entryPath = await fsp.realpath(selectedEntry);
|
|
42
|
+
const previous = installedImports.get(entryPath);
|
|
43
|
+
if (previous) return previous;
|
|
44
|
+
let packageRoot = path.dirname(entryPath);
|
|
45
|
+
while (true) {
|
|
46
|
+
const manifestPath = path.join(packageRoot, "package.json");
|
|
47
|
+
if (existsSync(manifestPath) && JSON.parse(await fsp.readFile(manifestPath, "utf8")).name === "@github/copilot-sdk") break;
|
|
48
|
+
const parent = path.dirname(packageRoot);
|
|
49
|
+
if (parent === packageRoot) throw new Error("Could not locate the installed Copilot SDK package.");
|
|
50
|
+
packageRoot = parent;
|
|
51
|
+
}
|
|
52
|
+
const fingerprint = await fingerprintCopilotPackage(packageRoot);
|
|
53
|
+
const loaded = (async () => {
|
|
54
|
+
const sdk = await import(pathToFileURL(entryPath).href).catch((error) => {
|
|
55
|
+
installedImports.delete(entryPath);
|
|
56
|
+
throw error;
|
|
57
|
+
});
|
|
58
|
+
try {
|
|
59
|
+
if (await fingerprintCopilotPackage(packageRoot) !== fingerprint) throw new Error("Copilot SDK changed while loading. Restart OpenClaw before verifying inference.");
|
|
60
|
+
} catch (error) {
|
|
61
|
+
materializeErrorStack(error);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
sdk,
|
|
66
|
+
identity: {
|
|
67
|
+
entryPath,
|
|
68
|
+
packageRoot,
|
|
69
|
+
fingerprint
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
})();
|
|
73
|
+
installedImports.set(entryPath, loaded);
|
|
74
|
+
return loaded;
|
|
75
|
+
}
|
|
76
|
+
async function loadCopilotSdk(options = {}) {
|
|
77
|
+
return (await loadSdk(options.primaryImport)).sdk;
|
|
78
|
+
}
|
|
79
|
+
async function loadCopilotSdkWithIdentity() {
|
|
80
|
+
const loaded = await loadSdk();
|
|
81
|
+
if (!loaded.identity) throw new Error("The loaded Copilot SDK has no installed implementation identity.");
|
|
82
|
+
return {
|
|
83
|
+
sdk: loaded.sdk,
|
|
84
|
+
identity: loaded.identity
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
async function loadSdk(primaryImport) {
|
|
88
|
+
if (!cached) cached = doLoad(primaryImport).catch((err) => {
|
|
89
|
+
cached = void 0;
|
|
90
|
+
throw err;
|
|
91
|
+
});
|
|
92
|
+
return cached;
|
|
93
|
+
}
|
|
94
|
+
async function doLoad(primaryImport) {
|
|
95
|
+
let primaryErr;
|
|
96
|
+
try {
|
|
97
|
+
return primaryImport ? { sdk: await primaryImport() } : await importInstalledSdk(fileURLToPath(import.meta.resolve("@github/copilot-sdk")));
|
|
98
|
+
} catch (err) {
|
|
99
|
+
primaryErr = err;
|
|
100
|
+
}
|
|
101
|
+
const fallbackDir = path.join(resolveStateDir(), "npm-runtime", "copilot");
|
|
102
|
+
const fallbackPath = path.join(fallbackDir, "node_modules", "@github", "copilot-sdk");
|
|
103
|
+
if (!existsSync(fallbackPath)) throw createMissingSdkError(primaryErr, void 0, fallbackPath);
|
|
104
|
+
try {
|
|
105
|
+
return await importInstalledSdk(createRequire(path.join(fallbackDir, "package.json")).resolve("@github/copilot-sdk"));
|
|
106
|
+
} catch (fallbackErr) {
|
|
107
|
+
throw createMissingSdkError(primaryErr, fallbackErr, fallbackPath);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function createMissingSdkError(primaryErr, fallbackErr, fallbackPath) {
|
|
111
|
+
const lines = [
|
|
112
|
+
"[copilot] @github/copilot-sdk is not installed.",
|
|
113
|
+
"",
|
|
114
|
+
"The external @openclaw/copilot plugin depends on @github/copilot-sdk",
|
|
115
|
+
"including its platform-specific Copilot runtime package.",
|
|
116
|
+
"Reinstall the plugin once with:",
|
|
117
|
+
"",
|
|
118
|
+
" openclaw plugins install @openclaw/copilot",
|
|
119
|
+
"",
|
|
120
|
+
"For source checkouts or offline repair, install the SDK directly:",
|
|
121
|
+
"",
|
|
122
|
+
` npm install ${COPILOT_SDK_SPEC}`,
|
|
123
|
+
"",
|
|
124
|
+
`The legacy fallback location is still probed at\n ${fallbackPath}`,
|
|
125
|
+
"",
|
|
126
|
+
"Primary resolution error:",
|
|
127
|
+
` ${summarizeError(primaryErr)}`
|
|
128
|
+
];
|
|
129
|
+
if (fallbackErr !== void 0) lines.push("", "Fallback resolution error:", ` ${summarizeError(fallbackErr)}`);
|
|
130
|
+
const err = new Error(lines.join("\n"));
|
|
131
|
+
err.code = "COPILOT_SDK_MISSING";
|
|
132
|
+
return err;
|
|
133
|
+
}
|
|
134
|
+
function summarizeError(value) {
|
|
135
|
+
if (value === void 0 || value === null) return "(none)";
|
|
136
|
+
if (value instanceof Error) return value.message || String(value);
|
|
137
|
+
if (typeof value === "string") return value;
|
|
138
|
+
try {
|
|
139
|
+
return JSON.stringify(value);
|
|
140
|
+
} catch {
|
|
141
|
+
return Object.prototype.toString.call(value);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
export { loadCopilotSdk as n, loadCopilotSdkWithIdentity as r, fingerprintCopilotPackage as t };
|