참고
Copilot SDK가 현재 공개 미리 보기에 있습니다. 기능 및 가용성은 변경될 수 있습니다.
GitHub Copilot SDK 를 사용하면 선호하는 프로그래밍 언어로 구동되는 GitHub Copilot 애플리케이션을 빌드할 수 있습니다. 이 가이드에서는 SDK를 사용하여 `npm`설치하고, 첫 번째 메시지를 보내고, 스트리밍 응답을 추가합니다.
다른 언어에 대한 자세한 내용과 단계는 리포지토리에서 github/copilot-sdk을 설치합니다.
필수 조건
시작하기 전에 Node.js 18 이상이 설치되어 있는지 확인합니다.
Authentication
[AUTOTITLE](/copilot/how-tos/copilot-cli/install-copilot-cli)의 지침에 따라 GitHub Copilot CLI을(를) 설치하고 인증하세요. 이렇게 하면 SDK가 귀하의 GitHub 계정에 액세스하고 Copilot을(를) 사용할 수 있습니다.
-
설치 및 작동 중 Copilot CLI 인지 확인합니다.
Bash copilot --version
copilot --version
설치
-
새 디렉터리를 만들고 프로젝트를 초기화하십시오.
Bash mkdir copilot-demo && cd copilot-demo npm init -y --init-type module
mkdir copilot-demo && cd copilot-demo npm init -y --init-type module -
SDK 및 TypeScript 실행기를 설치합니다.
Bash npm install @github/copilot-sdk tsx
npm install @github/copilot-sdk tsx
첫 번째 메시지 보내기
-
새 파일을
index.ts만들고 다음 코드를 추가합니다. 그러면 단일 프롬프트가 Copilot 전송되고 응답이 출력됩니다.TypeScript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-4.1" }); const response = await session.sendAndWait({ prompt: "What is 2 + 2?" }); console.log(response?.data.content); await client.stop(); process.exit(0);import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-4.1" }); const response = await session.sendAndWait({ prompt: "What is 2 + 2?" }); console.log(response?.data.content); await client.stop(); process.exit(0); -
코드를 실행합니다.
Bash npx tsx index.ts
npx tsx index.ts
이 예제에서:
-
** Copilot CLI에 연결을 관리하는 새 클라이언트를 `CopilotClient()`** 만듭니다. -
** `createSession()` ** 는 지정된 모델을 사용하여 새 대화 세션을 시작합니다. -
** `sendAndWait()` ** 는 프롬프트를 보내고 반환하기 전에 전체 응답을 기다립니다.
스트리밍 응답 추가
전체 응답을 기다리는 대신 생성될 때 스트리밍할 수 있습니다. 이는 출력을 실시간으로 표시하려는 긴 응답 또는 대화형 애플리케이션에 유용합니다.
-
정답 청크가 도착할 때 이를 수신하고 출력하도록 다음 코드로
index.ts를 업데이트하세요.TypeScript import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-4.1", streaming: true, }); // Listen for response chunks session.on("assistant.message_delta", (event) => { process.stdout.write(event.data.deltaContent); }); session.on("session.idle", () => { console.log(); // New line when done }); await session.sendAndWait({ prompt: "Tell me a short joke" }); await client.stop(); process.exit(0);import { CopilotClient } from "@github/copilot-sdk"; const client = new CopilotClient(); const session = await client.createSession({ model: "gpt-4.1", streaming: true, }); // Listen for response chunks session.on("assistant.message_delta", (event) => { process.stdout.write(event.data.deltaContent); }); session.on("session.idle", () => { console.log(); // New line when done }); await session.sendAndWait({ prompt: "Tell me a short joke" }); await client.stop(); process.exit(0); -
코드를 실행합니다.
Bash npx tsx index.ts
npx tsx index.ts
스트리밍을 사용하도록 설정하면 응답이 생성될 때 증분 방식으로 표시됩니다. 이벤트를 구독하여 각 청크를 실시간으로 처리할 수 있습니다.
-
** `assistant.message_delta` **는 응답이 생성될 때마다 각 청크마다 실행됩니다. -
** `session.idle` **는 응답이 완료되고, 세션이 다음 메시지를 받을 준비가 되었을 때 발생합니다.
이벤트 구독 방법
SDK는 이벤트를 구독하는 다음 메서드를 제공합니다.
-
**on(handler)**: 모든 이벤트를 구독합니다. 구독 취소 함수를 반환합니다. -
**on(eventType, handler)**: 특정 이벤트 유형을 구독합니다. 구독 취소 함수를 반환합니다.
다음 코드를 index.ts 추가하여 이벤트를 구독하고 더 이상 필요하지 않은 경우 구독을 취소합니다.
// Subscribe to all events
const unsubscribeAll = session.on((event) => {
console.log("Event:", event.type);
});
// Subscribe to specific event type
const unsubscribeIdle = session.on("session.idle", (event) => {
console.log("Session is idle");
});
// Later, to unsubscribe:
unsubscribeAll();
unsubscribeIdle();
// Subscribe to all events
const unsubscribeAll = session.on((event) => {
console.log("Event:", event.type);
});
// Subscribe to specific event type
const unsubscribeIdle = session.on("session.idle", (event) => {
console.log("Session is idle");
});
// Later, to unsubscribe:
unsubscribeAll();
unsubscribeIdle();
다음 단계:
Copilot SDK 사용을 계속 진행하려면 github/copilot-sdk 리포지토리의 Build Your First Copilot-Powered App를 참조하세요.