Native Extension API Reference
Native Extension API Reference
Section titled “Native Extension API Reference”Summary
Section titled “Summary”The native API exposes the Rust extension directly to PHP. Complex inputs and outputs are JSON strings so the PHP surface can track the Copilot SDK without introducing many PHP value objects.
Use this API when you need raw status calls, model listing, manual session control, streaming event polling, or arbitrary SDK JSON-RPC calls.
Namespace
Section titled “Namespace”CopilotSymbols
Section titled “Symbols”| Symbol | Type | Description |
|---|---|---|
copilot_sdk_version() | function | Returns the extension crate version. |
Copilot\Client | class | Starts and controls the Copilot CLI server connection. |
Copilot\Session | class | Represents a Copilot conversation session. |
Synopsis
Section titled “Synopsis”use Copilot\Client;
$client = new Client(json_encode([ 'cwd' => getcwd(), 'githubToken' => getenv('GITHUB_COPILOT_TOKEN'), 'useLoggedInUser' => false, 'copilotHome' => __DIR__ . '/../var/copilot',], JSON_THROW_ON_ERROR));
$session = $client->createSession(json_encode([ 'model' => 'gpt-5', 'streaming' => true, 'permissionPolicy' => 'deny_all',], JSON_THROW_ON_ERROR));
$event = json_decode( $session->sendAndWaitJson('Explain this project in one paragraph.'), true, 512, JSON_THROW_ON_ERROR);
$session->disconnect();$client->stop();Global Function
Section titled “Global Function”copilot_sdk_version()
Section titled “copilot_sdk_version()”function copilot_sdk_version(): stringReturns the extension crate version.
Return Value
Section titled “Return Value”Returns a semantic version string.
Example
Section titled “Example”echo copilot_sdk_version(), PHP_EOL;Copilot\Client
Section titled “Copilot\Client”Description
Section titled “Description”Starts the Copilot CLI server and opens a client connection.
__construct()
Section titled “__construct()”public function __construct(?string $optionsJson = null)Creates a Copilot client from optional JSON-encoded client options.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$optionsJson | ?string | JSON object with client options. |
Throws
Section titled “Throws”| Exception | Condition |
|---|---|
Throwable | JSON is invalid, options are invalid, or the Copilot CLI server cannot start. |
Example
Section titled “Example”$client = new Copilot\Client(json_encode([ 'githubToken' => getenv('GITHUB_COPILOT_TOKEN'), 'useLoggedInUser' => false, 'copilotHome' => __DIR__ . '/../var/copilot', 'cwd' => getcwd(),], JSON_THROW_ON_ERROR));ping()
Section titled “ping()”public function ping(?string $message = null): stringPings the Copilot server.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$message | ?string | Optional ping message. |
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$response = json_decode($client->ping('hello'), true, 512, JSON_THROW_ON_ERROR);modelsJson()
Section titled “modelsJson()”public function modelsJson(): stringReturns available model metadata.
Return Value
Section titled “Return Value”Returns a JSON string containing model metadata.
Example
Section titled “Example”$models = json_decode($client->modelsJson(), true, 512, JSON_THROW_ON_ERROR);statusJson()
Section titled “statusJson()”public function statusJson(): stringReturns general Copilot server status.
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$status = json_decode($client->statusJson(), true, 512, JSON_THROW_ON_ERROR);authStatusJson()
Section titled “authStatusJson()”public function authStatusJson(): stringReturns authentication status.
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$auth = json_decode($client->authStatusJson(), true, 512, JSON_THROW_ON_ERROR);callJson()
Section titled “callJson()”public function callJson(string $method, ?string $paramsJson = null): stringCalls an arbitrary Copilot SDK JSON-RPC method.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$method | string | SDK JSON-RPC method name. |
$paramsJson | ?string | JSON-encoded params object or array. |
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$result = json_decode( $client->callJson('status', json_encode([], JSON_THROW_ON_ERROR)), true, 512, JSON_THROW_ON_ERROR);createSession()
Section titled “createSession()”public function createSession(?string $configJson = null): Copilot\SessionCreates a new Copilot session.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$configJson | ?string | JSON-encoded session configuration. |
Return Value
Section titled “Return Value”Returns a Copilot\Session instance.
Example
Section titled “Example”$session = $client->createSession(json_encode([ 'model' => 'gpt-5', 'permissionPolicy' => 'deny_all',], JSON_THROW_ON_ERROR));resumeSession()
Section titled “resumeSession()”public function resumeSession(string $sessionId, ?string $configJson = null): Copilot\SessionResumes an existing session id.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$sessionId | string | Session id to resume. |
$configJson | ?string | JSON-encoded session configuration. |
Return Value
Section titled “Return Value”Returns a Copilot\Session instance.
Example
Section titled “Example”$session = $client->resumeSession($sessionId, json_encode([ 'permissionPolicy' => 'deny_all',], JSON_THROW_ON_ERROR));stop()
Section titled “stop()”public function stop(): voidStops the Copilot client/server. Calling it more than once is safe.
Example
Section titled “Example”$client->stop();Copilot\Session
Section titled “Copilot\Session”Description
Section titled “Description”Represents an active or resumed Copilot conversation session.
public function id(): stringReturns the session id.
Return Value
Section titled “Return Value”Returns the session id string.
Example
Section titled “Example”echo $session->id(), PHP_EOL;workspacePath()
Section titled “workspacePath()”public function workspacePath(): ?stringReturns the session workspace path when one is known.
Return Value
Section titled “Return Value”Returns the workspace path or null.
Example
Section titled “Example”$workspacePath = $session->workspacePath();remoteUrl()
Section titled “remoteUrl()”public function remoteUrl(): ?stringReturns the remote URL when one is known.
Return Value
Section titled “Return Value”Returns the remote URL or null.
Example
Section titled “Example”$remoteUrl = $session->remoteUrl();capabilitiesJson()
Section titled “capabilitiesJson()”public function capabilitiesJson(): stringReturns session capabilities.
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$capabilities = json_decode($session->capabilitiesJson(), true, 512, JSON_THROW_ON_ERROR);send()
Section titled “send()”public function send(string $prompt, ?string $optionsJson = null): stringSends or enqueues a prompt.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$prompt | string | Prompt text. |
$optionsJson | ?string | JSON-encoded message options. |
Return Value
Section titled “Return Value”Returns the message id.
Example
Section titled “Example”$messageId = $session->send('List the top-level PHP files.', json_encode([ 'mode' => 'enqueue',], JSON_THROW_ON_ERROR));sendAndWaitJson()
Section titled “sendAndWaitJson()”public function sendAndWaitJson(string $prompt, ?string $optionsJson = null): stringSends a prompt, waits for the next response event, and returns it.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$prompt | string | Prompt text. |
$optionsJson | ?string | JSON-encoded message options. |
Return Value
Section titled “Return Value”Returns a JSON string containing the next response event.
Example
Section titled “Example”$event = json_decode($session->sendAndWaitJson('Explain README.md.', json_encode([ 'timeoutSeconds' => 90,], JSON_THROW_ON_ERROR)), true, 512, JSON_THROW_ON_ERROR);messagesJson()
Section titled “messagesJson()”public function messagesJson(): stringReturns session messages.
Return Value
Section titled “Return Value”Returns a JSON string.
Example
Section titled “Example”$messages = json_decode($session->messagesJson(), true, 512, JSON_THROW_ON_ERROR);nextEventJson()
Section titled “nextEventJson()”public function nextEventJson(?int $timeoutMs = null): ?stringReturns the next streaming event, or null when no event arrives before the timeout.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$timeoutMs | ?int | Wait timeout in milliseconds. |
Return Value
Section titled “Return Value”Returns a JSON event string or null.
Example
Section titled “Example”while (($eventJson = $session->nextEventJson(250)) !== null) { $event = json_decode($eventJson, true, 512, JSON_THROW_ON_ERROR); var_dump($event['type'] ?? null);}abort()
Section titled “abort()”public function abort(): voidAborts the active Copilot operation for the session.
Example
Section titled “Example”$session->abort();setModel()
Section titled “setModel()”public function setModel(string $model, ?string $optionsJson = null): voidChanges the session model.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
$model | string | Model name. |
$optionsJson | ?string | JSON-encoded set-model options. |
Example
Section titled “Example”$session->setModel('gpt-5', json_encode([ 'reasoningEffort' => 'medium',], JSON_THROW_ON_ERROR));disconnect()
Section titled “disconnect()”public function disconnect(): voidDisconnects the session. Calling it more than once is safe.
Example
Section titled “Example”$session->disconnect();See Also
Section titled “See Also”📦 Source: soderlind/ext-php-copilot · Edit on GitHub