|
|
@@ -3,6 +3,11 @@ import {
|
|
|
allowRequestVerificationHeaders,
|
|
|
requestVerificationInteractionAvailable,
|
|
|
} from "@zcode/shared";
|
|
|
+import { z } from "zod";
|
|
|
+import {
|
|
|
+ assertV4CommandAckOk,
|
|
|
+ createHostCommandEnvelope,
|
|
|
+} from "./zcodeV4HostCommand.js";
|
|
|
import { zcodeMcpUiSamplingResultSchema } from "@zcode/shared";
|
|
|
import { MCP_APPS_SAMPLING_TRANSPORT_TIMEOUT_MS } from "@zcode/shared/mcp-apps";
|
|
|
import {
|
|
|
@@ -98,6 +103,8 @@ import {
|
|
|
zcodeAutomationListParamsSchema,
|
|
|
zcodeAutomationUpdateParamsSchema,
|
|
|
zcodeOffPeakCreateParamsSchema,
|
|
|
+ zcodeTaskDispatchParamsSchema,
|
|
|
+ zcodeTaskDispatchResultSchema,
|
|
|
zcodeOffPeakListParamsSchema,
|
|
|
OFF_PEAK_PROVIDER_IDS,
|
|
|
zcodeComputerUseOperationEventSchema,
|
|
|
@@ -2971,6 +2978,85 @@ export function createZCodeAgentService(
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
+ // 项目级任务分发(docs/specs/workspace-task-dispatch.md):批量 v4 createSession。
|
|
|
+ // firstInput 使"创建即首发"天然满足全并发执行——每个会话的 agent turn 在创建
|
|
|
+ // 完成后立即启动,创建循环本身只是快速的命令下发。单项失败隔离(spec §4)。
|
|
|
+ if (request.method === zcodeProtocolMethods.taskDispatch) {
|
|
|
+ const parsed = zcodeTaskDispatchParamsSchema.safeParse(request.params);
|
|
|
+ if (!parsed.success) {
|
|
|
+ void client.respondError(request.id, {
|
|
|
+ code: -32602,
|
|
|
+ message: "Invalid task dispatch params",
|
|
|
+ data: parsed.error.flatten(),
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ void (async () => {
|
|
|
+ try {
|
|
|
+ const results: z.infer<typeof zcodeTaskDispatchResultSchema>["results"] = [];
|
|
|
+ for (const item of parsed.data.tasks) {
|
|
|
+ try {
|
|
|
+ // 与 sendConversationCommandV4 同一条受控通道:getClient(含账号配置
|
|
|
+ // 同步在 createSession 命令路径内完成)→ buildConversationCommandEnvelope
|
|
|
+ // → V4 command。这里直调工厂内函数而非 service 方法(handler 在方法
|
|
|
+ // 对象装配前注册,方法名不在作用域)。
|
|
|
+ const commandClient = await getClient(workspace);
|
|
|
+ const envelope = await buildConversationCommandEnvelope({
|
|
|
+ workspacePath: workspace.workspacePath,
|
|
|
+ ...(workspace.workspaceIdentity
|
|
|
+ ? { workspaceIdentity: workspace.workspaceIdentity }
|
|
|
+ : {}),
|
|
|
+ envelope: createHostCommandEnvelope({
|
|
|
+ type: "createSession",
|
|
|
+ sessionId: null,
|
|
|
+ payload: {
|
|
|
+ workspaceId:
|
|
|
+ workspace.workspaceIdentity?.trim() || workspace.workspacePath,
|
|
|
+ // 统一权限确认的授权落点:整批共用 session 级权限范围。
|
|
|
+ permissionScope: "session",
|
|
|
+ ...(parsed.data.execute
|
|
|
+ ? { firstInput: { text: item.prompt } }
|
|
|
+ : {}),
|
|
|
+ },
|
|
|
+ }),
|
|
|
+ });
|
|
|
+ const ack = assertV4CommandAckOk(
|
|
|
+ "createSession",
|
|
|
+ await commandClient.request(V4_METHODS.command, envelope, commandAckSchema),
|
|
|
+ `dispatch ${item.id}`,
|
|
|
+ );
|
|
|
+ const sessionId =
|
|
|
+ ack.result?.type === "createSession" ? ack.result.sessionId : undefined;
|
|
|
+ results.push({
|
|
|
+ id: item.id,
|
|
|
+ title: item.title,
|
|
|
+ status: parsed.data.execute ? "started" : "created",
|
|
|
+ ...(sessionId ? { sessionId } : {}),
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ // 失败隔离:单项失败带一行摘要继续其余(不带堆栈,对齐协议 schema 上限)。
|
|
|
+ results.push({
|
|
|
+ id: item.id,
|
|
|
+ title: item.title,
|
|
|
+ status: "failed",
|
|
|
+ failure: (error instanceof Error ? error.message : String(error)).slice(
|
|
|
+ 0,
|
|
|
+ 400,
|
|
|
+ ),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ await client.respond(request.id, { results });
|
|
|
+ } catch (error) {
|
|
|
+ await client.respondError(request.id, {
|
|
|
+ code: -32603,
|
|
|
+ message: error instanceof Error ? error.message : String(error),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ })();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (request.method === zcodeProtocolMethods.automationList) {
|
|
|
const parsed = zcodeAutomationListParamsSchema.safeParse(request.params ?? {});
|
|
|
if (!parsed.success) {
|