codex

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 10 Imported by: 0

README

codex-go

概要

codex-go は OpenAI が提供する CLI 型のコーディングエージェント codex を Go から簡単に利用するためのラッパーライブラリです。

本ライブラリは codex 0.2.0 ~ 0.46.0 での動作を確認しています。

インストール

go get github.com/thamaji/codex-go

使用例

簡単な使用例です。環境変数 OPENAI_APIKEY に API キーを入れて実行します。

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/thamaji/codex-go"
)

func main() {
    apiKey := os.Getenv("OPENAI_APIKEY")

    client := codex.New(codex.WithLogger(os.Stderr, "info"))

    ctx := context.Background()
    if err := client.Login(ctx, apiKey); err != nil {
        log.Fatal(err)
    }

    // シンプルな呼び出し例
    text, err := client.Invoke(
        ctx,
        "hello",
        codex.WithCwd("."),
        codex.WithSandbox("read-only"),
        codex.WithApprovalPolicy("never"),
    )
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(text)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codex

type Codex struct {
	// contains filtered or unexported fields
}

Codex は codex コマンドをラップするクライアント構造体です。 内部で実行コマンドのパスやログ設定、認証用のロックを保持します。

func New

func New(options ...CodexOption) *Codex

New は Codex のインスタンスを作成します。 WithExecutablePath で codex コマンドの実行パスを指定します。 WithLogger で codex コマンドのログ出力を設定します。

func (*Codex) Invoke

func (codex *Codex) Invoke(ctx context.Context, prompt string, options ...InvokeOption) (string, error)

Invoke は Codex を実行して結果を返します。 指定可能なオプションの詳細は以下を参照してください。 https://github.com/openai/codex/blob/main/docs/advanced.md#codex-mcp-server-quickstart

func (*Codex) IsLoggedIn added in v0.0.5

func (codex *Codex) IsLoggedIn(ctx context.Context) (bool, error)

IsLoggedIn は Codex が認証済みであるかを返します。

func (*Codex) Login

func (codex *Codex) Login(ctx context.Context, apiKey string) error

Login は OpenAI の API Key を使用して Codex の認証を行います。

func (*Codex) SetExecutablePath

func (codex *Codex) SetExecutablePath(path string)

SetExecutablePath は Codex インスタンスの実行ファイルパスを設定します。 テストやカスタムビルドを使う場合に利用します。

func (*Codex) SetLogger

func (codex *Codex) SetLogger(w io.Writer, level string)

SetLogger は Codex インスタンスのログ出力先とログレベルを設定します。 `WithLogger` と同様の振る舞いを持ち、インスタンス生成後に設定を変更できます。

func (*Codex) Version added in v0.0.4

func (codex *Codex) Version() (string, error)

Version は Codex のバージョン情報を返します。

type CodexOption

type CodexOption func(*Codex)

CodexOption は Codex の設定を変更するためのオプション関数です。 これを使って `New` 呼び出し時に実装固有の設定を注入できます。

func WithExecutablePath

func WithExecutablePath(path string) CodexOption

WithExecutablePath は Codex が使用する実行ファイルのパスを設定する オプションを返します。デフォルトは `codex` です。

func WithLogger

func WithLogger(w io.Writer, level string) CodexOption

WithLogger は Codex のログ出力先とログレベルを設定するオプションを返します。 `w` にログが書き出され、`level` でログの詳細度を制御します。

type InvokeOption

type InvokeOption func(*invokeOptions) error

InvokeOption は Invoke 呼び出しに渡すオプション関数の型です。 各オプションは内部の `invokeOptions` を変更し、エラーを返すことができます。

func WithApprovalPolicy

func WithApprovalPolicy(approvalPolicy string) InvokeOption

WithApprovalPolicy は承認ポリシー("untrusted", "on-failure", "never") を設定するオプションを返します。無効な値が与えられるとエラーになります。

func WithBaseInstructions

func WithBaseInstructions(baseInstructions string) InvokeOption

WithBaseInstructions はツールに渡すベース命令(base instructions)を設定する オプションを返します。Codex 全体の振舞いを指示します。 この指定が無い場合、Codex はモデル毎に適切な base instructions を自動的に指定します。

func WithCompactPrompt added in v0.0.5

func WithCompactPrompt(compactPrompt string) InvokeOption

WithCompactPrompt は会話履歴が長くなった際に、履歴を要約するためのプロンプトを指定します。 このオプションは Codex バージョン 0.52.0 以降でのみ有効です。

func WithConfig

func WithConfig(config map[string]any) InvokeOption

WithConfig はツール呼び出し時に渡す追加の設定マップを指定するオプションを返します。

func WithCwd

func WithCwd(cwd string) InvokeOption

WithCwd はツール実行時のカレントディレクトリを指定するオプションを返します。

func WithDeveloperInstructions added in v0.0.5

func WithDeveloperInstructions(developerInstructions string) InvokeOption

WithDeveloperInstructions はツールに渡す developer ロールの命令(developer instructions)を設定する オプションを返します。セッション全体での振舞いを指示します。 このオプションは Codex バージョン 0.53.0 以降でのみ有効です。

func WithIncludePlanTool

func WithIncludePlanTool(includePlanTool bool) InvokeOption

WithIncludePlanTool は実行時にプランツールを含めるかどうかを設定するオプションを返します。 Deprecated: Codex バージョン 0.48.0 以降はプランツールは常に有効です。このオプションは廃止されました。

func WithModel

func WithModel(model string) InvokeOption

WithModel は使用するモデル名を指定するオプションを返します。

func WithProfile

func WithProfile(profile string) InvokeOption

WithProfile は実行時に使用するプロファイル名を設定するオプションを返します。

func WithSandbox

func WithSandbox(sandbox string) InvokeOption

WithSandbox はサンドボックス設定("read-only", "workspace-write", "danger-full-access") を指定するオプションを返します。無効な値が与えられるとエラーになります。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL