Voraussetzungen
- Der Benutzer muss authentifiziert werden (GitHub Token oder angemeldeter Benutzer)
- Das Arbeitsverzeichnis der Sitzung muss ein GitHub Repository sein.
Aktivieren von Remotesitzungen
Immer aktiv (auf Clientebene)
Legen Sie remote: true beim Erstellen des Clients fest. Jede Sitzung in einem GitHub Repository erhält automatisch eine Remote-URL.
Typescript
import { CopilotClient } from "@github/copilot-sdk";
const client = new CopilotClient({ remote: true });
const session = await client.createSession({
workingDirectory: "/path/to/github-repo",
onPermissionRequest: async () => ({ allowed: true }),
});
session.on("session.info", (event) => {
if (event.data.infoType === "remote") {
console.log("Remote URL:", event.data.url);
}
});
Python
from copilot import CopilotClient
client = CopilotClient(enable_remote_sessions=True)
session = await client.create_session(
working_directory="/path/to/github-repo",
on_permission_request=lambda req: {"allowed": True},
)
def on_event(event):
if event.type == "session.info" and event.data.info_type == "remote":
print(f"Remote URL: {event.data.url}")
session.on(on_event)
Go
client, _ := copilot.NewClient(&copilot.ClientOptions{Remote: true})
session, _ := client.CreateSession(ctx, &copilot.SessionConfig{
WorkingDirectory: "/path/to/github-repo",
OnPermissionRequest: func(req copilot.PermissionRequest, inv copilot.PermissionInvocation) (rpc.PermissionDecision, error) {
return &rpc.PermissionDecisionApproveOnce{}, nil
},
})
session.On(func(event copilot.SessionEvent) {
if event.Type == "session.info" {
// Check infoType and extract URL
}
})
C#
var client = new CopilotClient(new CopilotClientOptions { Remote = true });
var session = await client.CreateSessionAsync(new SessionConfig
{
WorkingDirectory = "/path/to/github-repo",
OnPermissionRequest = (req, inv) =>
Task.FromResult(PermissionDecision.ApproveOnce()),
});
session.On((SessionEvent e) =>
{
if (e is SessionInfoEvent info && info.Data.InfoType == "remote")
{
Console.WriteLine($"Remote URL: {info.Data.Url}");
}
});
Rust
use github_copilot_sdk::{Client, ClientOptions, SessionConfig};
use github_copilot_sdk::handler::PermissionResult;
let client = Client::start(
ClientOptions::new().with_enable_remote_sessions(true)
).await?;
let session = client.create_session(
SessionConfig::new("/path/to/github-repo")
.with_permission_handler(|_req, _inv| async {
Ok(PermissionResult::approve_once())
}),
).await?;
let mut events = session.subscribe();
while let Ok(event) = events.recv().await {
if event.event_type == "session.info" {
// Check info_type and extract URL
}
}
Cloud-Sitzungen
Legen Sie die Option zum cloud Erstellen einer Sitzung fest, um eine Remotesitzung in der Cloud anstelle einer lokalen Sitzung zu erstellen. Sie können Repositorymetadaten einschließen, um die Cloudsitzung einem GitHub Repository zuzuordnen.
Typescript
const session = await client.createSession({
onPermissionRequest: async () => ({ allowed: true }),
cloud: {
repository: { owner: "github", name: "copilot-sdk", branch: "main" },
},
});
Python
from copilot import CloudSessionOptions, CloudSessionRepository
session = await client.create_session(
on_permission_request=PermissionHandler.approve_all,
cloud=CloudSessionOptions(
repository=CloudSessionRepository(
owner="github",
name="copilot-sdk",
branch="main",
)
),
)
Go
session, err := client.CreateSession(ctx, &copilot.SessionConfig{
Cloud: &copilot.CloudSessionOptions{
Repository: &copilot.CloudSessionRepository{
Owner: "github",
Name: "copilot-sdk",
Branch: "main",
},
},
})
C#
var session = await client.CreateSessionAsync(new SessionConfig
{
Cloud = new CloudSessionOptions
{
Repository = new CloudSessionRepository
{
Owner = "github",
Name = "copilot-sdk",
Branch = "main"
}
}
});
Rust
use github_copilot_sdk::{CloudSessionOptions, CloudSessionRepository, SessionConfig};
let session = client.create_session(
SessionConfig::default().with_cloud(
CloudSessionOptions::with_repository(
CloudSessionRepository::new("github", "copilot-sdk").with_branch("main"),
),
),
).await?;
Bei Bedarf (Umschalter pro Sitzung)
Verwenden Sie session.rpc.remote.enable(), um den Remotezugriff während der Sitzung zu starten, und session.rpc.remote.disable(), um ihn zu beenden. Dies entspricht den CLI-Befehlen /remote on und /remote off.
Typescript
const result = await session.rpc.remote.enable();
console.log("Remote URL:", result.url);
// Later: stop sharing
await session.rpc.remote.disable();
Python
result = await session.rpc.remote.enable()
print(f"Remote URL: {result.url}")
# Later: stop sharing
await session.rpc.remote.disable()
Go
result, err := session.RPC.Remote.Enable(ctx)
if result.URL != nil {
fmt.Println("Remote URL:", *result.URL)
}
// Later: stop sharing
err = session.RPC.Remote.Disable(ctx)
C#
var result = await session.Rpc.Remote.EnableAsync();
Console.WriteLine($"Remote URL: {result.Url}");
// Later: stop sharing
await session.Rpc.Remote.DisableAsync();
Rust
let result = session.rpc().remote().enable().await?;
if let Some(url) = &result.url {
println!("Remote URL: {url}");
}
// Later: stop sharing
session.rpc().remote().disable().await?;
QR-Codegenerierung
Die Remote-URL kann als QR-Code für einfachen mobilen Zugriff gerendert werden. Das SDK stellt die URL bereit: Verwenden Sie Ihre bevorzugte QR-Codebibliothek:
Hinweise
- Die
remoteClient-Option gilt nur dann, wenn das SDK den CLI-Prozess startet. Es wird ignoriert, wenn übercliUrleine Verbindung zu einem externen Server hergestellt wird. - Die Sitzungsoption
cloudgilt nur für neue Sitzungen, die mitsession.createerstellt werden; sie wird nicht verwendet, wenn eine vorhandene Sitzung wiederaufgenommen wird. - Wenn das Arbeitsverzeichnis kein GitHub-Repository ist, wird die Remote-Konfiguration stillschweigend übersprungen (Always-on-Modus) oder es wird ein Fehler zurückgegeben (On-demand-Modus).
- Remotesitzungen erfordern eine Authentifizierung. Stellen Sie sicher, dass
gitHubTokenoderuseLoggedInUserkonfiguriert ist.