paseo-plugin-zcode-provider 0.3.0 → 0.4.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.
Files changed (78) hide show
  1. package/NOTICE.md +2 -2
  2. package/README.md +9 -9
  3. package/package.json +4 -4
  4. package/server/account.ts +10 -0
  5. package/server/build-info.ts +1 -1
  6. package/server/discovery/manifest.ts +2 -2
  7. package/server/mapping.ts +31 -0
  8. package/server/provider.ts +5 -18
  9. package/server/runtime/pins.ts +3 -3
  10. package/server/session.ts +7 -10
  11. package/server/unsupported-capabilities.ts +4 -1
  12. package/server/vendor/zcode/NOTICE.md +4 -1
  13. package/server/vendor/zcode/packages/rpc/src/channelClient.ts +1 -0
  14. package/server/vendor/zcode/packages/rpc/src/channelServer.ts +2 -0
  15. package/server/vendor/zcode/packages/rpc/src/channels.shared.ts +1 -0
  16. package/server/vendor/zcode/packages/rpc/src/protocol.ts +3 -2
  17. package/server/vendor/zcode/packages/shared/src/app-runtime-preferences.ts +18 -0
  18. package/server/vendor/zcode/packages/shared/src/attachment-source.ts +5 -0
  19. package/server/vendor/zcode/packages/shared/src/bots.ts +385 -2
  20. package/server/vendor/zcode/packages/shared/src/browser-use/commands.ts +3 -1
  21. package/server/vendor/zcode/packages/shared/src/channel-mention.ts +131 -0
  22. package/server/vendor/zcode/packages/shared/src/conversationSelection.ts +37 -0
  23. package/server/vendor/zcode/packages/shared/src/dynamic-workflow-feature.ts +137 -0
  24. package/server/vendor/zcode/packages/shared/src/env.ts +15 -11
  25. package/server/vendor/zcode/packages/shared/src/highspeed.ts +232 -0
  26. package/server/vendor/zcode/packages/shared/src/mcp-apps/appTools.ts +148 -0
  27. package/server/vendor/zcode/packages/shared/src/mcp-apps/contract.ts +262 -0
  28. package/server/vendor/zcode/packages/shared/src/mcp-apps/instance.ts +12 -0
  29. package/server/vendor/zcode/packages/shared/src/mcp-apps/resourceMime.ts +73 -0
  30. package/server/vendor/zcode/packages/shared/src/mcp-apps/sampling.ts +117 -0
  31. package/server/vendor/zcode/packages/shared/src/mcp-apps/schemas.ts +90 -0
  32. package/server/vendor/zcode/packages/shared/src/model-execution.ts +58 -1
  33. package/server/vendor/zcode/packages/shared/src/oauth.ts +3 -0
  34. package/server/vendor/zcode/packages/shared/src/off-peak-execution.ts +73 -0
  35. package/server/vendor/zcode/packages/shared/src/protocol.ts +78 -4
  36. package/server/vendor/zcode/packages/shared/src/remoteTarget.ts +65 -0
  37. package/server/vendor/zcode/packages/shared/src/request-security-edition/verification-policy.ts +46 -0
  38. package/server/vendor/zcode/packages/shared/src/server-remote.ts +93 -0
  39. package/server/vendor/zcode/packages/shared/src/source-server/compatibility.ts +115 -0
  40. package/server/vendor/zcode/packages/shared/src/source-server/contracts.ts +426 -0
  41. package/server/vendor/zcode/packages/shared/src/source-server/errorCodes.ts +74 -0
  42. package/server/vendor/zcode/packages/shared/src/sqlite-migrations/contract.ts +83 -0
  43. package/server/vendor/zcode/packages/shared/src/subagent-profile.ts +62 -0
  44. package/server/vendor/zcode/packages/shared/src/subagent-runtime-config.ts +51 -0
  45. package/server/vendor/zcode/packages/shared/src/subagents-types.ts +164 -0
  46. package/server/vendor/zcode/packages/shared/src/usage-stats.ts +14 -0
  47. package/server/vendor/zcode/packages/shared/src/validationAppSettings.ts +136 -3
  48. package/server/vendor/zcode/packages/shared/src/version.ts +13 -0
  49. package/server/vendor/zcode/packages/shared/src/zcode-agent-policy.ts +24 -1
  50. package/server/vendor/zcode/packages/shared/src/zcode-protocol/index.ts +697 -36
  51. package/server/vendor/zcode/packages/shared/src/zcode-protocol/trace.ts +11 -0
  52. package/server/vendor/zcode/packages/shared/src/zcode-protocol-legacy-types.ts +14 -2
  53. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/apply.ts +28 -9
  54. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/attachment-ref.ts +3 -0
  55. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/command.ts +73 -53
  56. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/create-session-config.ts +15 -0
  57. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/create-workflow-display.ts +47 -1
  58. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/delta.ts +58 -2
  59. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/input-intent.ts +31 -0
  60. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/rows.ts +28 -13
  61. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/sessions-index-workflow-activity.ts +37 -3
  62. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/sessions-index.ts +36 -0
  63. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/snapshot.ts +21 -0
  64. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/submission.ts +1 -1
  65. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/toolDisplay.ts +32 -4
  66. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/transport.ts +31 -6
  67. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/wire-assembler.ts +4 -1
  68. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/wire-fault.ts +4 -1
  69. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-artifacts.ts +62 -4
  70. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-command-rejections.ts +30 -0
  71. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-row-meta.ts +40 -3
  72. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-run-settings-command.ts +2 -2
  73. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-runs-delta.ts +6 -5
  74. package/server/vendor/zcode/packages/shared/src/zcode-protocol-v4/workflow-runs.ts +103 -26
  75. package/server/vendor/zcode/packages/shared/src/zcode-task-mode-schema.ts +13 -1
  76. package/server/vendor/zcode/packages/shared/src/zcode-task-types-core.ts +31 -2
  77. package/server/vendor/zcode/packages/shared/src/zcodeEndpoint.ts +188 -73
  78. package/server/vendor/zcode/provenance.json +189 -80
@@ -0,0 +1,426 @@
1
+ import { z } from "zod";
2
+ import type { RemoteTarget } from "../remoteTarget.js";
3
+ import { publicZCodeErrorCodeSchema } from "./errorCodes.js";
4
+ import { REMOTE_ASSET_INSTALL_MODES } from "../remoteAssetInstallMode.js";
5
+ import { isKnownRemoteResourcePackageId } from "../remoteResourcePackages.js";
6
+ import { wslUserSchema } from "../wslUserValidation.js";
7
+ import { serverRemoteWorkspaceInfoSchema } from "../server-remote.js";
8
+
9
+ const nonEmptyStringSchema = z.string().trim().min(1);
10
+ const credentialKeySchema = nonEmptyStringSchema;
11
+ const timestampSchema = z.number().int().nonnegative();
12
+
13
+ export const sourceServerTransportKindSchema = z.enum(["ssh", "wsl", "docker"]);
14
+ export type SourceServerTransportKind = z.infer<typeof sourceServerTransportKindSchema>;
15
+
16
+ export const sshProfileTargetSchema = z
17
+ .object({
18
+ kind: z.literal("ssh"),
19
+ host: nonEmptyStringSchema,
20
+ port: z.number().int().positive().max(65_535).optional(),
21
+ username: nonEmptyStringSchema,
22
+ sshConfigAlias: nonEmptyStringSchema.optional(),
23
+ privateKeyPath: z.string().optional(),
24
+ assetInstallMode: z.enum(REMOTE_ASSET_INSTALL_MODES).optional(),
25
+ resourcePackages: z
26
+ .object({
27
+ selectedPackageIds: z.array(z.string().refine(isKnownRemoteResourcePackageId)).optional(),
28
+ })
29
+ .strict()
30
+ .optional(),
31
+ passwordCredentialKey: credentialKeySchema.optional(),
32
+ privateKeyPassphraseCredentialKey: credentialKeySchema.optional(),
33
+ })
34
+ .strict();
35
+
36
+ export const wslProfileTargetSchema = z
37
+ .object({
38
+ kind: z.literal("wsl"),
39
+ distro: z.string().optional(),
40
+ user: wslUserSchema.optional(),
41
+ })
42
+ .strict();
43
+
44
+ export const dockerProfileTargetSchema = z
45
+ .object({
46
+ kind: z.literal("docker"),
47
+ container: nonEmptyStringSchema,
48
+ })
49
+ .strict();
50
+
51
+ export const sourceServerProfileTargetSchema = z.discriminatedUnion("kind", [
52
+ sshProfileTargetSchema,
53
+ wslProfileTargetSchema,
54
+ dockerProfileTargetSchema,
55
+ ]);
56
+
57
+ export type SSHProfileTarget = z.infer<typeof sshProfileTargetSchema>;
58
+ export type WSLProfileTarget = z.infer<typeof wslProfileTargetSchema>;
59
+ export type DockerProfileTarget = z.infer<typeof dockerProfileTargetSchema>;
60
+ export type SourceServerProfileTarget = z.infer<typeof sourceServerProfileTargetSchema>;
61
+
62
+ /** 升级授权只绑定执行身份,不携带凭据或可变 Profile 内容。 */
63
+ export const sourceServerUpdateTargetSchema = z.discriminatedUnion("kind", [
64
+ z
65
+ .object({
66
+ kind: z.literal("ssh"),
67
+ host: nonEmptyStringSchema.toLowerCase(),
68
+ port: z.number().int().positive().max(65_535).default(22),
69
+ username: nonEmptyStringSchema,
70
+ sshConfigAlias: nonEmptyStringSchema.optional(),
71
+ })
72
+ .strict(),
73
+ z
74
+ .object({
75
+ kind: z.literal("wsl"),
76
+ distro: nonEmptyStringSchema,
77
+ user: wslUserSchema.refine((value) => value.length > 0),
78
+ })
79
+ .strict(),
80
+ dockerProfileTargetSchema,
81
+ ]);
82
+ export type SourceServerUpdateTarget = z.infer<typeof sourceServerUpdateTargetSchema>;
83
+
84
+ export function getSourceServerUpdateTarget(
85
+ target: Exclude<RemoteTarget, { kind: "server" }>,
86
+ ): SourceServerUpdateTarget {
87
+ switch (target.kind) {
88
+ case "ssh":
89
+ return sourceServerUpdateTargetSchema.parse({
90
+ kind: target.kind,
91
+ host: target.host,
92
+ port: target.port ?? 22,
93
+ username: target.username,
94
+ ...(target.sshConfigAlias?.trim() ? { sshConfigAlias: target.sshConfigAlias.trim() } : {}),
95
+ });
96
+ case "wsl":
97
+ return sourceServerUpdateTargetSchema.parse({
98
+ kind: target.kind,
99
+ distro: target.distro,
100
+ user: target.user,
101
+ });
102
+ case "docker":
103
+ return sourceServerUpdateTargetSchema.parse({
104
+ kind: target.kind,
105
+ container: target.container,
106
+ });
107
+ }
108
+ }
109
+
110
+ export function isSameSourceServerUpdateTarget(
111
+ left: Exclude<RemoteTarget, { kind: "server" }>,
112
+ right: Exclude<RemoteTarget, { kind: "server" }>,
113
+ ): boolean {
114
+ if (
115
+ [left, right].some(
116
+ (target) => target.kind === "wsl" && (!target.distro?.trim() || !target.user?.trim()),
117
+ )
118
+ )
119
+ return false;
120
+ return (
121
+ JSON.stringify(getSourceServerUpdateTarget(left)) ===
122
+ JSON.stringify(getSourceServerUpdateTarget(right))
123
+ );
124
+ }
125
+
126
+ export const sourceServerUpdateRequestSchema = z
127
+ .object({
128
+ profileId: nonEmptyStringSchema,
129
+ force: z.boolean().optional(),
130
+ expectedTarget: sourceServerUpdateTargetSchema.optional(),
131
+ })
132
+ .strict();
133
+ export type SourceServerUpdateRequest = z.input<typeof sourceServerUpdateRequestSchema>;
134
+
135
+ const connectionProfileBaseShape = {
136
+ profileId: z.string().uuid(),
137
+ sourceServerId: z.string().uuid().optional(),
138
+ displayName: nonEmptyStringSchema.optional(),
139
+ lastConnectedAt: timestampSchema.optional(),
140
+ lastConnectError: z.string().optional(),
141
+ createdAt: timestampSchema,
142
+ lifecycleState: z.enum(["active", "retired"]).default("active"),
143
+ retiredReason: z.literal("source-reinstalled").optional(),
144
+ retiredAt: timestampSchema.optional(),
145
+ };
146
+
147
+ export const connectionProfileSchema = z.discriminatedUnion("transport", [
148
+ z
149
+ .object({
150
+ ...connectionProfileBaseShape,
151
+ transport: z.literal("ssh"),
152
+ target: sshProfileTargetSchema,
153
+ })
154
+ .strict(),
155
+ z
156
+ .object({
157
+ ...connectionProfileBaseShape,
158
+ transport: z.literal("wsl"),
159
+ target: wslProfileTargetSchema,
160
+ })
161
+ .strict(),
162
+ z
163
+ .object({
164
+ ...connectionProfileBaseShape,
165
+ transport: z.literal("docker"),
166
+ target: dockerProfileTargetSchema,
167
+ })
168
+ .strict(),
169
+ ]);
170
+
171
+ export type ConnectionProfile = z.infer<typeof connectionProfileSchema>;
172
+
173
+ export const sourceServerRecordSchema = z
174
+ .object({
175
+ sourceServerId: z.string().uuid(),
176
+ displayName: nonEmptyStringSchema,
177
+ osUser: nonEmptyStringSchema.optional(),
178
+ hostname: nonEmptyStringSchema.optional(),
179
+ lastSeenVersion: nonEmptyStringSchema.optional(),
180
+ lastOnlineAt: timestampSchema.optional(),
181
+ preferredProfileId: z.string().uuid().optional(),
182
+ installState: z.enum(["installed", "uninstalled"]).default("installed"),
183
+ declaredWorkspaces: z
184
+ .array(
185
+ z
186
+ .object({
187
+ workspacePath: nonEmptyStringSchema,
188
+ workspaceIdentity: nonEmptyStringSchema,
189
+ label: nonEmptyStringSchema.optional(),
190
+ })
191
+ .strict(),
192
+ )
193
+ .default([]),
194
+ })
195
+ .strict();
196
+
197
+ export type SourceServerRecord = z.infer<typeof sourceServerRecordSchema>;
198
+
199
+ export const sourceServerWorkspaceHistoryEntrySchema = z
200
+ .object({
201
+ workspacePath: nonEmptyStringSchema,
202
+ workspaceIdentity: nonEmptyStringSchema.optional(),
203
+ legacyWorkspaceIdentity: nonEmptyStringSchema.optional(),
204
+ profileId: z.string().uuid(),
205
+ sourceServerId: z.string().uuid().optional(),
206
+ lastOpenedAt: timestampSchema,
207
+ })
208
+ .strict();
209
+
210
+ export type SourceServerWorkspaceHistoryEntry = z.infer<
211
+ typeof sourceServerWorkspaceHistoryEntrySchema
212
+ >;
213
+
214
+ export const sourceServerMigrationConflictSchema = z
215
+ .object({
216
+ kind: z.enum(["task-identity", "config-file"]),
217
+ key: nonEmptyStringSchema,
218
+ taskId: nonEmptyStringSchema.optional(),
219
+ relativePath: nonEmptyStringSchema.optional(),
220
+ sourceKey: nonEmptyStringSchema,
221
+ canonicalKey: nonEmptyStringSchema,
222
+ reason: nonEmptyStringSchema,
223
+ })
224
+ .strict();
225
+ export type SourceServerMigrationConflict = z.infer<typeof sourceServerMigrationConflictSchema>;
226
+
227
+ export const sourceServerMigrationReportRowSchema = z
228
+ .object({
229
+ migrationId: nonEmptyStringSchema,
230
+ workspacePath: nonEmptyStringSchema,
231
+ canonicalKey: nonEmptyStringSchema,
232
+ variantKeys: z.array(nonEmptyStringSchema),
233
+ status: z.enum(["running", "completed", "failed", "abandoned", "abandoned-diverged"]),
234
+ phase: nonEmptyStringSchema.optional(),
235
+ errorCode: nonEmptyStringSchema.optional(),
236
+ taskBackupPath: nonEmptyStringSchema.optional(),
237
+ sessionBackupPath: nonEmptyStringSchema.optional(),
238
+ updatedAt: timestampSchema,
239
+ abandonedAt: timestampSchema.optional(),
240
+ abandonedTrigger: nonEmptyStringSchema.optional(),
241
+ conflicts: z.array(sourceServerMigrationConflictSchema),
242
+ })
243
+ .strict();
244
+ export type SourceServerMigrationReportRow = z.infer<typeof sourceServerMigrationReportRowSchema>;
245
+
246
+ export const sourceServerMigrationReportSchema = z
247
+ .object({ rows: z.array(sourceServerMigrationReportRowSchema) })
248
+ .strict();
249
+ export type SourceServerMigrationReport = z.infer<typeof sourceServerMigrationReportSchema>;
250
+
251
+ export const sourceServerMigrationPathActionSchema = z
252
+ .object({
253
+ workspacePath: nonEmptyStringSchema,
254
+ migrationId: nonEmptyStringSchema.optional(),
255
+ })
256
+ .strict();
257
+ export type SourceServerMigrationPathAction = z.infer<typeof sourceServerMigrationPathActionSchema>;
258
+
259
+ export const sourceServerResolveWorkspaceRequestSchema = z
260
+ .object({
261
+ workspacePath: nonEmptyStringSchema,
262
+ legacyRouteHint: nonEmptyStringSchema.optional(),
263
+ })
264
+ .strict();
265
+ export type SourceServerResolveWorkspaceRequest = z.infer<
266
+ typeof sourceServerResolveWorkspaceRequestSchema
267
+ >;
268
+
269
+ export const sourceServerResolveWorkspaceResultSchema = z
270
+ .object({
271
+ canonicalWorkspaceKey: nonEmptyStringSchema,
272
+ mergedKeys: z.array(nonEmptyStringSchema),
273
+ migrationId: nonEmptyStringSchema.optional(),
274
+ })
275
+ .strict();
276
+ export type SourceServerResolveWorkspaceResult = z.infer<
277
+ typeof sourceServerResolveWorkspaceResultSchema
278
+ >;
279
+
280
+ export const sourceServerConnectionStateSchema = z.enum([
281
+ "absent",
282
+ "bootstrapping",
283
+ "needs-install",
284
+ "needs-upgrade",
285
+ "updating",
286
+ "online",
287
+ "retrying",
288
+ "offline",
289
+ "identity-change-pending",
290
+ "failed",
291
+ ]);
292
+ export type SourceServerConnectionState = z.infer<typeof sourceServerConnectionStateSchema>;
293
+
294
+ /** 远端任务 owner 的活动投影;身份用于隔离,实际路径仅用于展示。 */
295
+ export const sourceServerRunningTaskSchema = z
296
+ .object({
297
+ sessionId: nonEmptyStringSchema,
298
+ workspacePath: nonEmptyStringSchema,
299
+ workspaceIdentity: nonEmptyStringSchema.optional(),
300
+ })
301
+ .strict();
302
+ export type SourceServerRunningTask = z.infer<typeof sourceServerRunningTaskSchema>;
303
+ export const sourceServerRunningTasksSchema = z.array(sourceServerRunningTaskSchema);
304
+
305
+ /** 只读安装预览;sizeBytes 是安装包实际大小,不代表解压后的磁盘占用。 */
306
+ export const sourceServerInstallPreviewSchema = z
307
+ .object({
308
+ version: nonEmptyStringSchema,
309
+ installPath: nonEmptyStringSchema.optional(),
310
+ sizeBytes: z.number().int().positive(),
311
+ })
312
+ .strict();
313
+ export type SourceServerInstallPreview = z.infer<typeof sourceServerInstallPreviewSchema>;
314
+
315
+ export const sourceServerConnectionStateSnapshotSchema = z
316
+ .object({
317
+ profileId: z.string().uuid(),
318
+ sourceServerId: z.string().uuid().optional(),
319
+ state: sourceServerConnectionStateSchema,
320
+ error: z.string().optional(),
321
+ updateAvailability: z.enum(["none", "available", "required"]).optional(),
322
+ currentVersion: nonEmptyStringSchema.optional(),
323
+ availableVersion: nonEmptyStringSchema.optional(),
324
+ minimumVersion: nonEmptyStringSchema.optional(),
325
+ upgradeTarget: z.enum(["controller", "server"]).optional(),
326
+ errorCode: publicZCodeErrorCodeSchema.optional(),
327
+ retryable: z.boolean().optional(),
328
+ installPreview: sourceServerInstallPreviewSchema.optional(),
329
+ runningTasks: sourceServerRunningTasksSchema.optional(),
330
+ pendingSourceServerId: z.string().uuid().optional(),
331
+ recoveryAttempt: z.number().int().positive().optional(),
332
+ recoveryLimit: z.number().int().positive().optional(),
333
+ recoveryBudgetExhausted: z.boolean().optional(),
334
+ updatedAt: timestampSchema,
335
+ })
336
+ .strict();
337
+ export type SourceServerConnectionStateSnapshot = z.infer<
338
+ typeof sourceServerConnectionStateSnapshotSchema
339
+ >;
340
+
341
+ export const sourceServerManagementSnapshotRequestSchema = z
342
+ .object({
343
+ refreshMigrationCounts: z.boolean().optional(),
344
+ })
345
+ .strict();
346
+ export type SourceServerManagementSnapshotRequest = z.infer<
347
+ typeof sourceServerManagementSnapshotRequestSchema
348
+ >;
349
+
350
+ export const sourceServerManagementSnapshotSchema = z
351
+ .object({
352
+ generation: z.number().int().nonnegative(),
353
+ profiles: z.array(connectionProfileSchema),
354
+ servers: z.array(sourceServerRecordSchema),
355
+ workspaceHistory: z.array(sourceServerWorkspaceHistoryEntrySchema),
356
+ states: z.array(sourceServerConnectionStateSnapshotSchema),
357
+ failedPathCount: z.number().int().nonnegative(),
358
+ abandonedPathCount: z.number().int().nonnegative(),
359
+ migrationCounts: z.array(
360
+ z.discriminatedUnion("status", [
361
+ z
362
+ .object({
363
+ sourceServerId: nonEmptyStringSchema,
364
+ status: z.literal("available"),
365
+ failedPathCount: z.number().int().nonnegative(),
366
+ abandonedPathCount: z.number().int().nonnegative(),
367
+ })
368
+ .strict(),
369
+ z
370
+ .object({
371
+ sourceServerId: nonEmptyStringSchema,
372
+ status: z.literal("requires-connection"),
373
+ })
374
+ .strict(),
375
+ z.object({ sourceServerId: nonEmptyStringSchema, status: z.literal("loading") }).strict(),
376
+ z
377
+ .object({ sourceServerId: nonEmptyStringSchema, status: z.literal("unavailable") })
378
+ .strict(),
379
+ ]),
380
+ ),
381
+ })
382
+ .strict();
383
+ export type SourceServerManagementSnapshot = z.infer<typeof sourceServerManagementSnapshotSchema>;
384
+
385
+ export const sourceServerManagementStateDeltaSchema = z
386
+ .object({
387
+ generation: z.number().int().positive(),
388
+ state: sourceServerConnectionStateSnapshotSchema,
389
+ })
390
+ .strict();
391
+ export type SourceServerManagementStateDelta = z.infer<
392
+ typeof sourceServerManagementStateDeltaSchema
393
+ >;
394
+
395
+ export const sourceServerManagementDirectoryDeltaSchema = z
396
+ .object({
397
+ generation: z.number().int().positive(),
398
+ })
399
+ .strict();
400
+ export type SourceServerManagementDirectoryDelta = z.infer<
401
+ typeof sourceServerManagementDirectoryDeltaSchema
402
+ >;
403
+
404
+ /** Server CLI 的可选逐行进度;终态仍由退出码和最后一行结果决定。 */
405
+ export const sourceServerInstallProgressSchema = z
406
+ .object({
407
+ type: z.literal("source-server-install-progress"),
408
+ phase: z.enum(["verify-archive", "extract", "verify", "restart"]),
409
+ })
410
+ .strict();
411
+ export type SourceServerInstallProgress = z.infer<typeof sourceServerInstallProgressSchema>;
412
+
413
+ export const sourceServerOperationLogSchema = z
414
+ .object({
415
+ profileId: z.string().uuid(),
416
+ operation: z.enum(["connect", "update"]),
417
+ phase: nonEmptyStringSchema,
418
+ level: z.enum(["info", "warn", "error"]),
419
+ message: z.string(),
420
+ progress: z.number().min(0).max(1).optional(),
421
+ timestamp: timestampSchema,
422
+ })
423
+ .strict();
424
+ export type SourceServerOperationLog = z.infer<typeof sourceServerOperationLogSchema>;
425
+
426
+ export type SourceServerDeclaredWorkspaceWire = z.infer<typeof serverRemoteWorkspaceInfoSchema>;
@@ -0,0 +1,74 @@
1
+ import { z } from "zod";
2
+
3
+ export const SOURCE_SERVER_INSTALL_REQUIRED_ERROR_CODE =
4
+ "source-server-install-required" as const;
5
+ export const SOURCE_SERVER_UPGRADE_REQUIRED_ERROR_CODE =
6
+ "source-server-upgrade-required" as const;
7
+ export const SOURCE_SERVER_UPDATE_RUNNING_TASKS_ERROR_CODE =
8
+ "source-server-update-running-tasks" as const;
9
+ export const WORKSPACE_TASK_IDENTITY_CONFLICT_ERROR_CODE =
10
+ "workspace-task-identity-conflict" as const;
11
+ export const WORKSPACE_CONFIG_FILE_CONFLICT_ERROR_CODE =
12
+ "workspace-config-file-conflict" as const;
13
+ export const WORKSPACE_MIGRATION_PLAN_DIVERGED_ERROR_CODE =
14
+ "workspace-migration-plan-diverged" as const;
15
+ export const WORKSPACE_SESSION_GENERATION_CHANGED_ERROR_CODE =
16
+ "workspace-session-generation-changed" as const;
17
+ export const REMOTE_BIND_CANCELED_ERROR_CODE = "remote-bind-canceled" as const;
18
+ export const REMOTE_WORKSPACE_BIND_SESSION_MISSING_ERROR_CODE =
19
+ "remote-workspace-bind-session-missing" as const;
20
+ export const REMOTE_WORKSPACE_DIRECTORY_UNAVAILABLE_ERROR_CODE =
21
+ "remote-workspace-directory-unavailable" as const;
22
+ export const REMOTE_RECOVERY_BUDGET_EXHAUSTED_ERROR_CODE =
23
+ "remote-recovery-budget-exhausted" as const;
24
+ export const SOURCE_SERVER_IDENTITY_CHANGE_PENDING_ERROR_CODE =
25
+ "source-server-identity-change-pending" as const;
26
+ export const SOURCE_SERVER_REMOVE_CALLBACK_MISSING_ERROR_CODE =
27
+ "source-server-remove-callback-missing" as const;
28
+ export const SOURCE_SERVER_REMOVE_CALLBACK_FAILED_ERROR_CODE =
29
+ "source-server-remove-callback-failed" as const;
30
+ export const SOURCE_SERVER_REMOVE_PERSIST_FAILED_ERROR_CODE =
31
+ "source-server-remove-persist-failed" as const;
32
+
33
+ export const PUBLIC_ZCODE_ERROR_CODES = [
34
+ "remote-authentication-failed",
35
+ "remote-host-unreachable",
36
+ "remote-connection-timeout",
37
+ "remote-host-not-found",
38
+ "remote-host-key-changed",
39
+ "remote-download-failed",
40
+ "remote-upload-failed",
41
+ "remote-preflight-failed",
42
+ "remote-install-failed",
43
+ "remote-space-insufficient",
44
+ "remote-permission-denied",
45
+ "remote-connection-canceled",
46
+ "remote-connection-unknown",
47
+ SOURCE_SERVER_INSTALL_REQUIRED_ERROR_CODE,
48
+ SOURCE_SERVER_UPGRADE_REQUIRED_ERROR_CODE,
49
+ SOURCE_SERVER_UPDATE_RUNNING_TASKS_ERROR_CODE,
50
+ WORKSPACE_TASK_IDENTITY_CONFLICT_ERROR_CODE,
51
+ WORKSPACE_CONFIG_FILE_CONFLICT_ERROR_CODE,
52
+ WORKSPACE_MIGRATION_PLAN_DIVERGED_ERROR_CODE,
53
+ WORKSPACE_SESSION_GENERATION_CHANGED_ERROR_CODE,
54
+ REMOTE_BIND_CANCELED_ERROR_CODE,
55
+ REMOTE_WORKSPACE_BIND_SESSION_MISSING_ERROR_CODE,
56
+ REMOTE_WORKSPACE_DIRECTORY_UNAVAILABLE_ERROR_CODE,
57
+ REMOTE_RECOVERY_BUDGET_EXHAUSTED_ERROR_CODE,
58
+ SOURCE_SERVER_IDENTITY_CHANGE_PENDING_ERROR_CODE,
59
+ SOURCE_SERVER_REMOVE_CALLBACK_MISSING_ERROR_CODE,
60
+ SOURCE_SERVER_REMOVE_CALLBACK_FAILED_ERROR_CODE,
61
+ SOURCE_SERVER_REMOVE_PERSIST_FAILED_ERROR_CODE,
62
+ ] as const;
63
+
64
+ export type PublicZCodeErrorCode = (typeof PUBLIC_ZCODE_ERROR_CODES)[number];
65
+
66
+ export const publicZCodeErrorCodeSchema = z.enum(PUBLIC_ZCODE_ERROR_CODES);
67
+
68
+ const publicZCodeErrorCodeSet = new Set<string>(PUBLIC_ZCODE_ERROR_CODES);
69
+
70
+ export function isPublicZCodeErrorCode(
71
+ value: unknown,
72
+ ): value is PublicZCodeErrorCode {
73
+ return typeof value === "string" && publicZCodeErrorCodeSet.has(value);
74
+ }
@@ -0,0 +1,83 @@
1
+ import { z } from "zod";
2
+ import type { DatabaseSync } from "node:sqlite";
3
+
4
+ /** 历史 SQL 是 checksum 输入,发布后不可修改;apply 只供带数据回填的迁移使用。 */
5
+ export interface SqliteMigration {
6
+ id: string;
7
+ /** 开发分支迁移后移时沿用旧记账;所有已存在别名必须具有相同 SQL checksum。 */
8
+ previousIds?: readonly string[];
9
+ appVersion: string;
10
+ sql: string;
11
+ touchedTables: readonly string[];
12
+ apply?: (db: DatabaseSync) => void;
13
+ }
14
+
15
+ export interface MigrationBackupTable {
16
+ name: string;
17
+ columns: string[];
18
+ rows: number;
19
+ hash: string;
20
+ }
21
+
22
+ export interface MigrationBackup {
23
+ version: 1;
24
+ database: string;
25
+ journalTable?: "schema_migration" | "tasks_schema_migration";
26
+ migrations: string[];
27
+ tables: MigrationBackupTable[];
28
+ skipped?: "insufficient-space";
29
+ }
30
+
31
+ export interface MigrationOptions {
32
+ database: string;
33
+ transactionOpen?: boolean;
34
+ onBeforeApply?: (migrationId: string) => void | Promise<void>;
35
+ onApplied?: (migrationId: string) => void;
36
+ onCommitting?: () => void | Promise<void>;
37
+ /** 保留已发布 Host 任务库的三列记账结构;默认使用 CLI/项目四列结构。 */
38
+ journalTable?: "schema_migration" | "tasks_schema_migration";
39
+ /** 按优先级排列的可写卷目录;首项通常是 ~/.zcode/backup。 */
40
+ backupDirectories: readonly string[];
41
+ /** 首选卷失败后探测;默认枚举平台已挂载卷,测试可注入受控临时目录。 */
42
+ fallbackBackupDirectories?: () => Promise<readonly string[]>;
43
+ /** 兼容现有 CLI schema_migration 列名,app 采用 applied_at。 */
44
+ appliedAtColumn?: "time_applied" | "applied_at";
45
+ now?: () => number;
46
+ onBackupSkipped?: (backup: MigrationBackup) => void;
47
+ }
48
+
49
+ export interface MigrationResult {
50
+ applied: string[];
51
+ backupPath?: string;
52
+ backupSkipped: boolean;
53
+ }
54
+
55
+ export class MigrationChecksumError extends Error {
56
+ constructor(readonly migrationId: string) {
57
+ super(`Migration checksum mismatch: ${migrationId}`);
58
+ this.name = "MigrationChecksumError";
59
+ }
60
+ }
61
+
62
+ export interface SqliteMigrationsContract {
63
+ /** 串行 admission、校验 checksum、表级备份、单事务迁移;失败回滚并抛错。 */
64
+ migrate(
65
+ db: DatabaseSync,
66
+ migrations: readonly SqliteMigration[],
67
+ options: MigrationOptions,
68
+ ): Promise<MigrationResult>;
69
+ /** 工程恢复:停用业务写者后调用;关闭外键防级联,逐表验证,失败回滚。 */
70
+ restore(db: DatabaseSync, backupPath: string): Promise<void>;
71
+ }
72
+
73
+ /** 建窗前的一次性 CLI 命令;不进入会话/消息协议。 */
74
+ export const STORAGE_MIGRATION_COMMAND = "storage-migrate";
75
+ export const storageMigrationReportSchema = z
76
+ .object({
77
+ version: z.literal(1),
78
+ kind: z.literal("storage-migrated"),
79
+ applied: z.array(z.string()),
80
+ backupPath: z.string().optional(),
81
+ backupSkipped: z.boolean(),
82
+ })
83
+ .strict();
@@ -0,0 +1,62 @@
1
+ import { z } from "zod";
2
+ import { modelSelectionSchema, type ModelSelection } from "./model-selection.js";
3
+ import type { AgentPermissionMode } from "./subagents-types.js";
4
+
5
+ export type AgentProfileSource = "built-in" | "project" | "user";
6
+ export type AgentMemoryScope = "user" | "project" | "local";
7
+
8
+ export interface AgentProfile {
9
+ background?: boolean;
10
+ color?: "red" | "blue" | "green" | "yellow" | "purple" | "orange" | "pink" | "cyan";
11
+ description: string;
12
+ disallowedTools?: readonly string[];
13
+ injectAgentsMd?: boolean;
14
+ maxTurns?: number;
15
+ mcpServers?: readonly string[];
16
+ memory?: AgentMemoryScope;
17
+ modelSelection?: ModelSelection;
18
+ name: string;
19
+ path?: string;
20
+ permissionMode?: AgentPermissionMode;
21
+ skills?: readonly string[];
22
+ source: AgentProfileSource;
23
+ systemPrompt: string;
24
+ tools?: readonly string[];
25
+ }
26
+
27
+ export interface AgentProfileParseDiagnostic {
28
+ code: string;
29
+ message: string;
30
+ path?: string;
31
+ }
32
+
33
+ export interface AgentProfileLoadResult {
34
+ diagnostics: AgentProfileParseDiagnostic[];
35
+ profiles: AgentProfile[];
36
+ }
37
+
38
+ /** 完整运行时 profile 的跨进程边界,不使用 Settings 摘要代替。 */
39
+ export const agentProfileSchema = z
40
+ .object({
41
+ name: z.string().min(1),
42
+ description: z.string(),
43
+ source: z.enum(["built-in", "project", "user"]),
44
+ systemPrompt: z.string(),
45
+ path: z.string().optional(),
46
+ modelSelection: modelSelectionSchema.optional(),
47
+ background: z.boolean().optional(),
48
+ injectAgentsMd: z.boolean().optional(),
49
+ color: z
50
+ .enum(["red", "blue", "green", "yellow", "purple", "orange", "pink", "cyan"])
51
+ .optional(),
52
+ permissionMode: z
53
+ .enum(["acceptEdits", "auto", "bypassPermissions", "default", "dontAsk", "plan"])
54
+ .optional(),
55
+ memory: z.enum(["user", "project", "local"]).optional(),
56
+ maxTurns: z.number().int().positive().optional(),
57
+ tools: z.array(z.string()).readonly().optional(),
58
+ disallowedTools: z.array(z.string()).readonly().optional(),
59
+ skills: z.array(z.string()).readonly().optional(),
60
+ mcpServers: z.array(z.string()).readonly().optional(),
61
+ })
62
+ .strict();
@@ -0,0 +1,51 @@
1
+ import { z } from "zod";
2
+ import { modelSelectionSchema } from "./model-selection.js";
3
+ import { agentProfileSchema, type AgentProfile } from "./subagent-profile.js";
4
+ import { createAgentStateId } from "./subagents-types.js";
5
+
6
+ export const subagentRuntimeStateSchema = z
7
+ .object({
8
+ disabledAgentIds: z.array(z.string()),
9
+ builtInModelSelectionOverrides: z
10
+ .object({
11
+ Explore: modelSelectionSchema.optional(),
12
+ "general-purpose": modelSelectionSchema.optional(),
13
+ })
14
+ .strict(),
15
+ pluginAgentModelSelectionOverrides: z.record(z.string(), modelSelectionSchema),
16
+ })
17
+ .strict();
18
+ export type SubagentRuntimeState = z.infer<typeof subagentRuntimeStateSchema>;
19
+
20
+ /** snapshot 已经解析;读取失败的回退与 RPC 断连错误不能混为一谈。 */
21
+ export const subagentRuntimeConfigSchema = z.discriminatedUnion("kind", [
22
+ z
23
+ .object({
24
+ kind: z.literal("ready"),
25
+ profiles: z.array(agentProfileSchema.refine((profile) => profile.source !== "built-in")),
26
+ builtInModelSelectionOverrides:
27
+ subagentRuntimeStateSchema.shape.builtInModelSelectionOverrides,
28
+ pluginAgentModelSelectionOverrides:
29
+ subagentRuntimeStateSchema.shape.pluginAgentModelSelectionOverrides,
30
+ })
31
+ .strict(),
32
+ z.object({ kind: z.literal("built-in-fallback") }).strict(),
33
+ ]);
34
+ export type SubagentRuntimeConfig = z.infer<typeof subagentRuntimeConfigSchema>;
35
+
36
+ /** 仅从解析缓存组合结果;启停变化不重新读取或解析 Markdown。 */
37
+ export function resolveSubagentRuntimeProfiles(
38
+ profiles: readonly AgentProfile[],
39
+ state: SubagentRuntimeState,
40
+ ) {
41
+ const disabled = new Set(state.disabledAgentIds);
42
+ return {
43
+ profiles: profiles.filter(
44
+ (profile) =>
45
+ profile.source !== "user" ||
46
+ !disabled.has(createAgentStateId({ name: profile.name, scope: "user", source: "user" })),
47
+ ),
48
+ builtInModelSelectionOverrides: state.builtInModelSelectionOverrides,
49
+ pluginAgentModelSelectionOverrides: state.pluginAgentModelSelectionOverrides,
50
+ };
51
+ }