Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JD.SemanticKernel.Connectors.OpenAICodex

NuGet CI License: MIT

OpenAI Codex session connector for Semantic Kernel. Provides UseCodexChatCompletion() to wire local Codex CLI OAuth credentials (or an explicit API key) into any Semantic Kernel application without managing tokens manually.

Features

  • Multi-source credential resolution — Options → OPENAI_API_KEYCODEX_TOKEN~/.codex/auth.json → interactive device code login
  • Token exchange — Automatically exchanges Codex OAuth tokens for OpenAI API keys via the standard token exchange endpoint
  • Token refresh — Automatically refreshes expired tokens using stored refresh tokens
  • Device code login — Full interactive codex login equivalent for .NET applications
  • Semantic Kernel integration — One-call UseCodexChatCompletion() extension method
  • DI supportAddCodexAuthentication() for dependency injection
  • Model discoveryIModelDiscoveryProvider with known Codex models (o3, o4-mini, codex-mini, GPT-4.1, etc.)
  • Multi-TFM — Targets netstandard2.0, net8.0, and net10.0

Installation

dotnet add package JD.SemanticKernel.Connectors.OpenAICodex

Quick Start

Minimal (auto-discovers credentials)

using JD.SemanticKernel.Connectors.OpenAICodex;
using Microsoft.SemanticKernel;

var kernel = Kernel.CreateBuilder()
    .UseCodexChatCompletion()
    .Build();

With explicit API key

var kernel = Kernel.CreateBuilder()
    .UseCodexChatCompletion(apiKey: "sk-your-openai-api-key")
    .Build();

With specific model

var kernel = Kernel.CreateBuilder()
    .UseCodexChatCompletion(CodexModels.O3)
    .Build();

Using Dependency Injection

services.AddCodexAuthentication(o => o.ApiKey = "sk-...");

// Or from configuration:
services.AddCodexAuthentication(configuration);
// Reads from "CodexSession" section in appsettings.json

Direct HttpClient usage

using var client = CodexHttpClientFactory.Create();
// Use with any OpenAI-compatible API

Credential Resolution Order

The provider checks these sources in order and uses the first valid credential found:

Priority Source Description
1 CodexSessionOptions.ApiKey Explicit API key from options/config
2 CodexSessionOptions.AccessToken Explicit OAuth token from options/config
3 OPENAI_API_KEY env var Standard OpenAI environment variable
4 CODEX_TOKEN env var Codex-specific environment variable
5 ~/.codex/auth.json Codex CLI local credentials file
6 Device code login Interactive OAuth (when EnableInteractiveLogin = true)

Configuration

appsettings.json

{
  "CodexSession": {
    "ApiKey": null,
    "Issuer": "https://auth.openai.com",
    "ApiBaseUrl": "https://api.openai.com/v1",
    "AutoRefreshTokens": true,
    "EnableInteractiveLogin": false
  }
}

Available Options

Option Type Default Description
ApiKey string? null Explicit OpenAI API key
AccessToken string? null Explicit OAuth access token
CredentialsPath string? null Custom path to auth.json
Issuer string https://auth.openai.com OAuth issuer URL
ApiBaseUrl string https://api.openai.com/v1 OpenAI API base URL
ClientId string? null OAuth client ID for device code flow
EnableInteractiveLogin bool false Enable interactive device code login
AutoRefreshTokens bool true Auto-refresh expired tokens

Available Models

Constant Model ID Description
CodexModels.O3 o3 Most capable reasoning model
CodexModels.O4Mini o4-mini Fast, cost-effective reasoning
CodexModels.CodexMini codex-mini-latest Optimised for code generation
CodexModels.Gpt4Point1 gpt-4.1 High-capability general model
CodexModels.Gpt4Point1Mini gpt-4.1-mini Fast general model
CodexModels.Gpt4Point1Nano gpt-4.1-nano Fastest, most cost-effective

Related Packages

License

MIT

About

Semantic Kernel connector for OpenAI Codex CLI authentication — auto-discovers credentials, token exchange, device code OAuth

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages