huaweicloud-devkit 2.0.1-next.3 → 2.0.1-next.4

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.
Files changed (68) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -1
  3. package/plugins/huaweicloud-core/.claude-plugin/plugin.json +1 -1
  4. package/plugins/huaweicloud-core/.codex-plugin/plugin.json +1 -1
  5. package/plugins/huaweicloud-core/.cursor-plugin/plugin.json +1 -1
  6. package/plugins/huaweicloud-core/.hermes-plugin/plugin.json +1 -1
  7. package/plugins/huaweicloud-core/.mcp.json +3 -0
  8. package/plugins/huaweicloud-core/.workbuddy-plugin/plugin.json +1 -1
  9. package/plugins/huaweicloud-core/dist/approval-roster.js +2 -0
  10. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkit-gate.js +7 -0
  11. package/plugins/huaweicloud-core/dist/lib/hdkit/hdkitservice-api.js +30 -1
  12. package/plugins/huaweicloud-core/dist/lib/hdkit/sandbox-pool.js +21 -0
  13. package/plugins/huaweicloud-core/dist/mcp-server.js +5019 -3879
  14. package/plugins/huaweicloud-core/dist/mcp-server.js.map +4 -4
  15. package/plugins/huaweicloud-core/dist/packs/registry.js +1 -0
  16. package/plugins/huaweicloud-core/dist/packs/sandbox/approval.js +13 -2
  17. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-account.js +198 -10
  18. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-credentials.js +11 -0
  19. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-session.js +22 -0
  20. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-tunnel.js +46 -0
  21. package/plugins/huaweicloud-core/dist/packs/sandbox/handlers-upload.js +11 -0
  22. package/plugins/huaweicloud-core/dist/packs/sandbox/schemas.js +20 -0
  23. package/plugins/huaweicloud-core/dist/packs/sandbox/tools.js +26 -18
  24. package/plugins/huaweicloud-core/dist/sandbox/deploy-check.js +97 -0
  25. package/plugins/huaweicloud-core/dist/sandbox/deploy-nginx.js +30 -14
  26. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-backend.js +37 -0
  27. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-connections.js +84 -0
  28. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-client.js +229 -0
  29. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-protocol.js +151 -0
  30. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-envd-transport.js +121 -0
  31. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-service-api.js +80 -0
  32. package/plugins/huaweicloud-core/dist/sandbox/devbox/devbox-upload.js +106 -0
  33. package/plugins/huaweicloud-core/dist/sandbox/hwlink-connection.js +12 -0
  34. package/plugins/huaweicloud-core/dist/sandbox/upload-project.js +5 -2
  35. package/plugins/huaweicloud-core/dist/tool-definitions.js +4 -0
  36. package/plugins/huaweicloud-core/dist/tools.js +1 -0
  37. package/plugins/huaweicloud-core/openclaw.plugin.json +1 -1
  38. package/plugins/huaweicloud-core/safety/policy.json +2 -1
  39. package/plugins/huaweicloud-core/skills/huawei-sandbox/SKILL.md +67 -16
  40. package/plugins/huaweicloud-core/skills/huawei-sandbox/references/devbox-backend.md +78 -0
  41. package/plugins/huaweicloud-core/skills/huawei-sandbox/references/resource-pool.md +14 -0
  42. package/plugins/huaweicloud-core/skills/huaweicloud-safety/references/approvals.md +2 -1
  43. package/plugins/huaweicloud-core/src/approval-roster.ts +2 -0
  44. package/plugins/huaweicloud-core/src/lib/hdkit/hdkit-gate.ts +7 -0
  45. package/plugins/huaweicloud-core/src/lib/hdkit/hdkitservice-api.ts +30 -1
  46. package/plugins/huaweicloud-core/src/lib/hdkit/sandbox-pool.ts +22 -0
  47. package/plugins/huaweicloud-core/src/packs/registry.ts +1 -0
  48. package/plugins/huaweicloud-core/src/packs/sandbox/approval.ts +15 -2
  49. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-account.ts +206 -11
  50. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-credentials.ts +12 -0
  51. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-session.ts +23 -0
  52. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-tunnel.ts +49 -0
  53. package/plugins/huaweicloud-core/src/packs/sandbox/handlers-upload.ts +11 -0
  54. package/plugins/huaweicloud-core/src/packs/sandbox/schemas.ts +23 -0
  55. package/plugins/huaweicloud-core/src/packs/sandbox/tools.ts +27 -17
  56. package/plugins/huaweicloud-core/src/sandbox/deploy-check.ts +105 -0
  57. package/plugins/huaweicloud-core/src/sandbox/deploy-nginx.ts +36 -18
  58. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-backend.ts +44 -0
  59. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-connections.ts +115 -0
  60. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-client.ts +281 -0
  61. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-protocol.ts +180 -0
  62. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-envd-transport.ts +142 -0
  63. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-service-api.ts +102 -0
  64. package/plugins/huaweicloud-core/src/sandbox/devbox/devbox-upload.ts +167 -0
  65. package/plugins/huaweicloud-core/src/sandbox/hwlink-connection.ts +12 -0
  66. package/plugins/huaweicloud-core/src/sandbox/upload-project.ts +5 -2
  67. package/plugins/huaweicloud-core/src/tool-definitions.ts +4 -0
  68. package/plugins/huaweicloud-core/src/tools.ts +3 -0
@@ -0,0 +1,102 @@
1
+ // DevBox control plane over hdkitservice (`/sandbox/devbox/*`). Every call
2
+ // carries the user's AK/SK (+ optional X-HW-Security-Token) through the one
3
+ // shared hdkitRequest seam — there is no sandbox key. EnvD tokens returned by
4
+ // connect/refresh stay in the in-process connection cache only.
5
+
6
+ import { asRecord } from '../../lib/as-record.ts';
7
+ import { hdkitRequest } from '../../lib/hdkit/hdkitservice-api.ts';
8
+ import {
9
+ requireDevboxConnection,
10
+ devboxTokenNeedsRefresh,
11
+ setDevboxConnectionFromResponse,
12
+ type DevboxConnectionRecord,
13
+ } from './devbox-connections.ts';
14
+
15
+ export const DEVBOX_CONNECT_TIMEOUT_DEFAULT = 1800;
16
+ const DEVBOX_CONNECT_TIMEOUT_MIN = 10;
17
+ const DEVBOX_CONNECT_TIMEOUT_MAX = 86400;
18
+
19
+ export function devboxConnectTimeoutSeconds(): number {
20
+ const raw = process.env.HW_SANDBOX_CONNECT_TIMEOUT_SECONDS;
21
+ if (raw === undefined || raw === '') return DEVBOX_CONNECT_TIMEOUT_DEFAULT;
22
+ const value = Number(raw);
23
+ if (!Number.isSafeInteger(value) || value < DEVBOX_CONNECT_TIMEOUT_MIN || value > DEVBOX_CONNECT_TIMEOUT_MAX) {
24
+ throw new Error(
25
+ `HW_SANDBOX_CONNECT_TIMEOUT_SECONDS must be an integer between ${DEVBOX_CONNECT_TIMEOUT_MIN} and ` +
26
+ `${DEVBOX_CONNECT_TIMEOUT_MAX} (got "${raw}").`,
27
+ );
28
+ }
29
+ return value;
30
+ }
31
+
32
+ export interface DevboxConnectOptions {
33
+ timeoutSeconds?: number;
34
+ source?: string;
35
+ envVars?: Record<string, string>;
36
+ poolType?: string;
37
+ }
38
+
39
+ export async function devboxConnect(options: DevboxConnectOptions = {}): Promise<Record<string, unknown>> {
40
+ const body: Record<string, unknown> = {
41
+ timeout: options.timeoutSeconds ?? devboxConnectTimeoutSeconds(),
42
+ source: options.source || 'CLI',
43
+ };
44
+ if (options.envVars && Object.keys(options.envVars).length > 0) body.envVars = options.envVars;
45
+ if (options.poolType) body.pool_type = options.poolType;
46
+ return asRecord(await hdkitRequest('POST', 'sandbox/devbox/connect', body, 300000));
47
+ }
48
+
49
+ export async function devboxRefresh(sandboxId: string, timeoutSeconds?: number): Promise<Record<string, unknown>> {
50
+ const body: Record<string, unknown> = {
51
+ timeout: timeoutSeconds ?? devboxConnectTimeoutSeconds(),
52
+ };
53
+ return asRecord(await hdkitRequest('POST', `sandbox/devbox/${encodeURIComponent(sandboxId)}/connect`, body, 120000));
54
+ }
55
+
56
+ export async function devboxShutdown(sandboxId: string): Promise<Record<string, unknown>> {
57
+ return asRecord(await hdkitRequest('POST', `sandbox/devbox/${encodeURIComponent(sandboxId)}/shutdown`, {}, 120000));
58
+ }
59
+
60
+ export async function devboxKill(sandboxId: string): Promise<Record<string, unknown>> {
61
+ return asRecord(await hdkitRequest('DELETE', `sandbox/devbox/${encodeURIComponent(sandboxId)}`, undefined, 120000));
62
+ }
63
+
64
+ export async function devboxStatus(sandboxId: string): Promise<Record<string, unknown>> {
65
+ return asRecord(await hdkitRequest('GET', `sandbox/devbox/${encodeURIComponent(sandboxId)}`, undefined, 30000));
66
+ }
67
+
68
+ /** True for EnvD 401/403 — triggers the one automatic refresh+retry. */
69
+ export function isDevboxAuthError(error: unknown): boolean {
70
+ if (!error || typeof error !== 'object') return false;
71
+ const rec = error as { auth?: unknown; status?: unknown };
72
+ return rec.auth === true || rec.status === 401 || rec.status === 403;
73
+ }
74
+
75
+ /** Calls hdkitservice refresh with the current AK/SK and updates the cache. */
76
+ export async function refreshDevboxConnection(connection: DevboxConnectionRecord): Promise<DevboxConnectionRecord> {
77
+ const result = await devboxRefresh(connection.sandboxId);
78
+ return setDevboxConnectionFromResponse(connection.workspaceId, result);
79
+ }
80
+
81
+ /**
82
+ * Data-plane wrapper: refresh the EnvD token five minutes before expiry, and
83
+ * on a 401/403 refresh once and retry the same operation. A second auth
84
+ * failure is returned to the caller (control-plane re-authentication is the
85
+ * agent's next action).
86
+ */
87
+ export async function withDevboxConnection<T>(
88
+ workspaceId: string,
89
+ operation: (connection: DevboxConnectionRecord) => Promise<T>,
90
+ ): Promise<T> {
91
+ let connection = requireDevboxConnection(workspaceId);
92
+ if (devboxTokenNeedsRefresh(connection)) {
93
+ connection = await refreshDevboxConnection(connection);
94
+ }
95
+ try {
96
+ return await operation(connection);
97
+ } catch (error) {
98
+ if (!isDevboxAuthError(error)) throw error;
99
+ const refreshed = await refreshDevboxConnection(requireDevboxConnection(workspaceId));
100
+ return await operation(refreshed);
101
+ }
102
+ }
@@ -0,0 +1,167 @@
1
+ // DevBox upload implementations: EnvD /files binary writes, md5 verify, and
2
+ // tar extraction for projects. No session, no HTTP tunnel, no file server.
3
+
4
+ import { createHash } from 'node:crypto';
5
+ import { readFileSync, rmSync, statSync } from 'node:fs';
6
+ import { dirname as posixDirname, posix } from 'node:path';
7
+
8
+ import { classifyLocalUploadPath } from '../upload-file.ts';
9
+ import { computeMd5, createTarGz } from '../upload-project.ts';
10
+ import { execDevboxOneShot, writeDevboxFile } from './devbox-envd-client.ts';
11
+ import { withDevboxConnection } from './devbox-service-api.ts';
12
+
13
+ export interface DevboxUploadFileResult {
14
+ ok: boolean;
15
+ localPath: string;
16
+ remotePath: string;
17
+ bytes: number;
18
+ chunks: number;
19
+ md5: string;
20
+ md5Verified: boolean;
21
+ }
22
+
23
+ export interface DevboxUploadProjectResult {
24
+ ok: boolean;
25
+ localDir: string;
26
+ remotePath: string;
27
+ bytes: number;
28
+ md5: string;
29
+ md5Verified: boolean;
30
+ extracted: boolean;
31
+ }
32
+
33
+ function shellQuote(value: string): string {
34
+ return `'${value.replaceAll("'", `'\\''`)}'`;
35
+ }
36
+
37
+ function remoteParent(remotePath: string): string {
38
+ const parent = posixDirname(remotePath);
39
+ return parent && parent !== '.' ? parent : '/';
40
+ }
41
+
42
+ async function verifiedRemoteMd5(
43
+ workspaceId: string,
44
+ remotePath: string,
45
+ expectedMd5: string,
46
+ timeoutMs: number,
47
+ ): Promise<boolean> {
48
+ return withDevboxConnection(workspaceId, async (connection) => {
49
+ const result = await execDevboxOneShot(
50
+ connection,
51
+ `md5sum ${shellQuote(remotePath)} | awk '{print $1}'`,
52
+ Math.min(timeoutMs, 60000),
53
+ );
54
+ return result.stdout.trim() === expectedMd5;
55
+ });
56
+ }
57
+
58
+ export async function uploadFileDevbox(
59
+ workspaceId: string,
60
+ localPath: string,
61
+ remotePath: string,
62
+ timeoutMs = 120000,
63
+ ): Promise<DevboxUploadFileResult> {
64
+ const localError = classifyLocalUploadPath(localPath, 'file');
65
+ if (localError) throw localError;
66
+ if (!remotePath.startsWith('/')) throw new Error('sandbox upload: remote_path must be an absolute path.');
67
+
68
+ const data = readFileSync(localPath);
69
+ const md5 = createHash('md5').update(data).digest('hex');
70
+
71
+ await withDevboxConnection(workspaceId, async (connection) => {
72
+ await execDevboxOneShot(connection, `mkdir -p ${shellQuote(remoteParent(remotePath))}`, Math.min(timeoutMs, 30000));
73
+ await writeDevboxFile(connection, remotePath, data, timeoutMs);
74
+ });
75
+
76
+ const md5Verified = await verifiedRemoteMd5(workspaceId, remotePath, md5, timeoutMs);
77
+ if (!md5Verified) {
78
+ throw new Error(`sandbox upload: md5 mismatch after writing ${remotePath}.`);
79
+ }
80
+ return {
81
+ ok: true,
82
+ localPath,
83
+ remotePath,
84
+ bytes: data.length,
85
+ chunks: 1,
86
+ md5,
87
+ md5Verified,
88
+ };
89
+ }
90
+
91
+ export async function uploadProjectDevbox(
92
+ workspaceId: string,
93
+ localDir: string,
94
+ remoteDir: string | undefined,
95
+ timeoutMs = 300000,
96
+ options: { exclude?: string[]; extract?: boolean; verify?: boolean } = {},
97
+ ): Promise<DevboxUploadProjectResult> {
98
+ const localError = classifyLocalUploadPath(localDir, 'directory');
99
+ if (localError) throw localError;
100
+
101
+ const projectName = posix.basename(localDir);
102
+ const targetParentDir = remoteDir || '/workspace';
103
+ const archiveRemotePath = `${targetParentDir}/${projectName}.tar.gz`;
104
+ const archivePath = await createTarGz(localDir, options.exclude);
105
+
106
+ try {
107
+ const archiveSize = statSync(archivePath).size;
108
+ const expectedMd5 = await computeMd5(archivePath);
109
+ const archiveBytes = readFileSync(archivePath);
110
+
111
+ await withDevboxConnection(workspaceId, async (connection) => {
112
+ await execDevboxOneShot(connection, `mkdir -p ${shellQuote(targetParentDir)}`, Math.min(timeoutMs, 30000));
113
+ await writeDevboxFile(connection, archiveRemotePath, archiveBytes, timeoutMs);
114
+ });
115
+
116
+ const md5Verified = await verifiedRemoteMd5(workspaceId, archiveRemotePath, expectedMd5, timeoutMs);
117
+ if (options.verify !== false && !md5Verified) {
118
+ throw new Error(`sandbox upload project: md5 mismatch after writing ${archiveRemotePath}.`);
119
+ }
120
+
121
+ if (options.extract !== false) {
122
+ await withDevboxConnection(workspaceId, async (connection) => {
123
+ const extract = await execDevboxOneShot(
124
+ connection,
125
+ `mkdir -p ${shellQuote(targetParentDir)} && tar -xzf ${shellQuote(archiveRemotePath)} -C ` +
126
+ `${shellQuote(targetParentDir)} && rm -f ${shellQuote(archiveRemotePath)}`,
127
+ timeoutMs,
128
+ );
129
+ if (extract.exitCode !== 0) {
130
+ throw new Error(
131
+ `sandbox upload project: tar extraction failed (exit ${extract.exitCode}): ${extract.stderr.trim()}`,
132
+ );
133
+ }
134
+ // Best-effort traverse-permission hardening for later nginx serving.
135
+ await execDevboxOneShot(
136
+ connection,
137
+ [
138
+ `REAL_PATH=$(readlink -f ${shellQuote(`${targetParentDir}/${projectName}`)} 2>/dev/null || echo ${shellQuote(`${targetParentDir}/${projectName}`)})`,
139
+ `if [ ! -d "$REAL_PATH" ] && [ -d ${shellQuote(targetParentDir)} ]; then`,
140
+ ` REAL_PATH=${shellQuote(`${targetParentDir}/${projectName}`)}`,
141
+ `fi`,
142
+ `chmod -R o+rX "$REAL_PATH" 2>/dev/null || true`,
143
+ `find "$REAL_PATH" -type d -exec chmod o+x {} \\; 2>/dev/null || true`,
144
+ `find "$REAL_PATH" -type f -path "*/node_modules/.bin/*" -exec chmod +x {} \\; 2>/dev/null || true`,
145
+ ].join('\n'),
146
+ Math.min(timeoutMs, 15000),
147
+ ).catch(() => undefined);
148
+ });
149
+ }
150
+
151
+ return {
152
+ ok: true,
153
+ localDir,
154
+ remotePath: options.extract !== false ? `${targetParentDir}/${projectName}` : archiveRemotePath,
155
+ bytes: archiveSize,
156
+ md5: expectedMd5,
157
+ md5Verified,
158
+ extracted: options.extract !== false,
159
+ };
160
+ } finally {
161
+ try {
162
+ rmSync(posixDirname(archivePath), { recursive: true, force: true });
163
+ } catch {
164
+ // best-effort local temp cleanup
165
+ }
166
+ }
167
+ }
@@ -11,6 +11,9 @@
11
11
  import { createConnection, getCredentials } from '../lib/hdkit/hwlink-api.ts';
12
12
  import { getWebSocketImpl } from '../proxy/proxy-agent.ts';
13
13
  import { trackSandboxConnect, trackSandboxDisconnect } from '../telemetry/telemetry.ts';
14
+ import { backendForWorkspace } from './devbox/devbox-backend.ts';
15
+ import { execDevboxOneShot } from './devbox/devbox-envd-client.ts';
16
+ import { withDevboxConnection } from './devbox/devbox-service-api.ts';
14
17
  import {
15
18
  loadWsExec,
16
19
  type HwlinkExecResult,
@@ -141,6 +144,9 @@ export async function execOneShot(
141
144
  username: string,
142
145
  timeoutMs: number,
143
146
  ): Promise<HwlinkExecResult> {
147
+ if (backendForWorkspace(workspaceId) === 'devbox') {
148
+ return withDevboxConnection(workspaceId, (connection) => execDevboxOneShot(connection, command, timeoutMs));
149
+ }
144
150
  const { wsUrl, source, WebSocketImpl, wsExec } = await openHwlinkConnection(workspaceId);
145
151
  return await wsExec.executeHwlinkCommand({
146
152
  url: wsUrl,
@@ -158,6 +164,12 @@ export async function execWithSession(
158
164
  username: string,
159
165
  timeoutMs?: number,
160
166
  ): Promise<HwlinkExecResult> {
167
+ // Devbox v1 has no persistent PTY: the tool face degrades to one-shot and
168
+ // marks the response (degraded/statePersisted:false). Commands must be
169
+ // self-contained; no shell state survives between calls.
170
+ if (backendForWorkspace(workspaceId) === 'devbox') {
171
+ return execOneShot(workspaceId, command, username, timeoutMs ?? 60000);
172
+ }
161
173
  const session = await getSession(workspaceId, username, timeoutMs);
162
174
  return await session.exec(command, { timeoutMs });
163
175
  }
@@ -43,7 +43,10 @@ interface UploadProjectResult {
43
43
  extracted: boolean;
44
44
  }
45
45
 
46
- async function createTarGz(localDir: string, exclude: string[] = []): Promise<string> {
46
+ // Exported for the devbox upload path (src/sandbox/devbox/devbox-upload.ts):
47
+ // the archive recipe (git archive vs tar + exclude expansion) is shared so
48
+ // the two backends produce identical payloads.
49
+ export async function createTarGz(localDir: string, exclude: string[] = []): Promise<string> {
47
50
  const archiveName = `${basename(localDir)}.tar.gz`;
48
51
  const archiveDir = join(tmpdir(), `sandbox-upload-${Date.now()}`);
49
52
  mkdirSync(archiveDir, { recursive: true });
@@ -80,7 +83,7 @@ async function createTarGz(localDir: string, exclude: string[] = []): Promise<st
80
83
  return archivePath;
81
84
  }
82
85
 
83
- async function computeMd5(filePath: string): Promise<string> {
86
+ export async function computeMd5(filePath: string): Promise<string> {
84
87
  return new Promise((resolve, reject) => {
85
88
  const hash = createHash('md5');
86
89
  createReadStream(filePath)
@@ -210,6 +210,10 @@ export const TOOL_DEFINITIONS_LITERALS = [
210
210
  name: 'huaweicloud_sandbox_shutdown',
211
211
  description: packDescription('huaweicloud_sandbox_shutdown'),
212
212
  },
213
+ {
214
+ name: 'huaweicloud_sandbox_kill',
215
+ description: packDescription('huaweicloud_sandbox_kill'),
216
+ },
213
217
  {
214
218
  name: 'huaweicloud_voucher_status',
215
219
  description: packDescription('huaweicloud_voucher_status'),
@@ -146,6 +146,8 @@ export type ToolArgs = {
146
146
  workspace_id?: string;
147
147
  username?: string;
148
148
  timeout_ms?: number;
149
+ /** Devbox sandbox TTL in seconds (connect/refresh). */
150
+ timeout_seconds?: number;
149
151
  // The async-exec claim protocol (huaweicloud_sandbox_exec_one_shot mode:
150
152
  // "async" + huaweicloud_sandbox_exec_poll).
151
153
  task_id?: string;
@@ -246,6 +248,7 @@ const NUMERIC_ARG_KEYS = [
246
248
  'node_port',
247
249
  'public_port',
248
250
  'expiry_hours',
251
+ 'timeout_seconds',
249
252
  ] as const;
250
253
 
251
254
  function normalizeNumericArgs(args: ToolArgs): ToolArgs {