{"meta":{"title":"Copilot CLI ACP 服务器","intro":"了解 GitHub Copilot 命令行界面 (CLI) 的代理客户端协议服务器。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/reference","title":"参考资料"},{"href":"/zh/copilot/reference/copilot-cli-reference","title":"\n              Copilot 命令行界面（CLI） 参考"},{"href":"/zh/copilot/reference/copilot-cli-reference/acp-server","title":"ACP 服务器"}],"documentType":"article"},"body":"# Copilot CLI ACP 服务器\n\n了解 GitHub Copilot 命令行界面 (CLI) 的代理客户端协议服务器。\n\n> [!NOTE]\n> GitHub Copilot 命令行界面 (CLI) 中的 ACP 支持处于 公共预览版 状态，可能会发生变化。\n\n## 概述\n\n代理客户端协议（ACP）是一种协议，用于标准化客户端（如代码编辑器和 IDE）和代理（如 Copilot 命令行界面（CLI）） 之间的通信。 有关此协议的更多详细信息，请参阅 [官方简介](https://agentclientprotocol.com/get-started/introduction)。\n\n## 用例\n\n*               **IDE 集成：** 将 Copilot 支持构建到任何编辑器或开发环境中。\n* **CI/CD 管道：** 在自动化工作流中协调代理编码任务。\n* **自定义前端：** 为特定开发人员工作流创建专用接口。\n* **多代理系统：** 使用标准协议与其他 AI 代理协调 Copilot 。\n\n## 启动 ACP 服务器\n\n              GitHub Copilot 命令行界面 (CLI) 可以使用 `--acp` 标志作为 ACP 服务器启动。 服务器支持两种模式， `stdio` 以及 `TCP`。\n\n### stdio 模式（建议用于 IDE 集成）\n\n默认情况下，在提供 `--acp` 标志时，`stdio` 模式将被推断出。 \n              `--stdio`还可以提供标志来消除歧义。\n\n```bash\ncopilot --acp --stdio\n```\n\n### TCP 模式\n\n              `--port`如果标志与`--acp`标志结合使用，则服务器以 TCP 模式启动。\n\n```bash\ncopilot --acp --port 3000\n```\n\n## 与 ACP 服务器集成\n\n库生态系统不断增加，以编程方式与 ACP 服务器交互。 假设 GitHub Copilot 命令行界面 (CLI) 正确安装和进行身份验证，以下示例演示了如何使用 [typescript](https://agentclientprotocol.com/libraries/typescript) 客户端发送单个提示并打印 AI 响应。\n\n```typescript\nimport * as acp from \"@agentclientprotocol/sdk\";\nimport { spawn } from \"node:child_process\";\nimport { Readable, Writable } from \"node:stream\";\n\nasync function main() {\n  const executable = process.env.COPILOT_CLI_PATH ?? \"copilot\";\n\n  // ACP uses standard input/output (stdin/stdout) for transport; we pipe these for the NDJSON stream.\n  const copilotProcess = spawn(executable, [\"--acp\", \"--stdio\"], {\n    stdio: [\"pipe\", \"pipe\", \"inherit\"],\n  });\n\n  if (!copilotProcess.stdin || !copilotProcess.stdout) {\n    throw new Error(\"Failed to start Copilot ACP process with piped stdio.\");\n  }\n\n  // Create ACP streams (NDJSON over stdio)\n  const output = Writable.toWeb(copilotProcess.stdin) as WritableStream<Uint8Array>;\n  const input = Readable.toWeb(copilotProcess.stdout) as ReadableStream<Uint8Array>;\n  const stream = acp.ndJsonStream(output, input);\n\n  const client: acp.Client = {\n    async requestPermission(params) {\n      // This example should not trigger tool calls; if it does, refuse.\n      return { outcome: { outcome: \"cancelled\" } };\n    },\n\n    async sessionUpdate(params) {\n      const update = params.update;\n\n      if (update.sessionUpdate === \"agent_message_chunk\" && update.content.type === \"text\") {\n        process.stdout.write(update.content.text);\n      }\n    },\n  };\n\n  const connection = new acp.ClientSideConnection((_agent) => client, stream);\n\n  await connection.initialize({\n    protocolVersion: acp.PROTOCOL_VERSION,\n    clientCapabilities: {},\n  });\n\n  const sessionResult = await connection.newSession({\n    cwd: process.cwd(),\n    mcpServers: [],\n  });\n\n  process.stdout.write(\"Session started!\\n\");\n  const promptText = \"Hello ACP Server!\";\n  process.stdout.write(`Sending prompt: '${promptText}'\\n`);\n\n  const promptResult = await connection.prompt({\n    sessionId: sessionResult.sessionId,\n    prompt: [{ type: \"text\", text: promptText }],\n  });\n\n  process.stdout.write(\"\\n\");\n\n  if (promptResult.stopReason !== \"end_turn\") {\n    process.stderr.write(`Prompt finished with stopReason=${promptResult.stopReason}\\n`);\n  }\n\n  // Best-effort cleanup\n  copilotProcess.stdin.end();\n  copilotProcess.kill(\"SIGTERM\");\n  await new Promise<void>((resolve) => {\n    copilotProcess.once(\"exit\", () => resolve());\n    setTimeout(() => resolve(), 2000);\n  });\n}\n\nmain().catch((error) => {\n  console.error(error);\n  process.exitCode = 1;\n});\n```\n\n## 延伸阅读\n\n* [官方 ACP 文档](https://agentclientprotocol.com/protocol/overview)"}