Open2

clienのコード読み

ナインナイン

読み方

  • 上位モジュールから下位に向けて読んでいく

ディレクトリ構成

ディレクトリ構造
./
├── src/                            # プロジェクトの主要なソースコード
│   ├── api/                        # API関連のモジュール
│   │   ├── providers/              # 各プロバイダーの実装
│   │   │   ├── anthropic.ts        # Anthropic APIハンドラー
│   │   │   ├── bedrock.ts          # AWS Bedrock APIハンドラー
│   │   │   ├── deepseek.ts         # DeepSeek APIハンドラー
│   │   │   ├── gemini.ts           # Google Gemini APIハンドラー
│   │   │   ├── lmstudio.ts         # LM Studioローカルモデルハンドラー
│   │   │   ├── ollama.ts           # Ollamaローカルモデルハンドラー
│   │   │   ├── openai-native.ts    # OpenAI API(ネイティブ)ハンドラー
│   │   │   ├── openai.ts           # OpenAI APIハンドラー
│   │   │   ├── openrouter.ts       # OpenRouter APIハンドラー
│   │   │   └── vertex.ts           # Vertex AI APIハンドラー
│   │   ├── transform/              # データ形式変換ユーティリティ
│   │   │   ├── gemini-format.ts    # AnthropicからGemini形式への変換
│   │   │   ├── o1-format.ts        # O1モデル用の形式変換
│   │   │   ├── openai-format.ts    # OpenAI用の形式変換
│   │   │   └── stream.ts           # ストリームデータ管理
│   │   └── index.ts                # APIハンドラーのエントリーポイント
│   ├── core/                       # プロジェクトの中核となるロジック
│   │   ├── assistant-message/      # メッセージ解析と処理
│   │   │   ├── diff.ts             # 差分解析ロジック
│   │   │   ├── index.ts            # メッセージモジュールエントリーポイント
│   │   │   └── parse-assistant-message.ts # メッセージ解析ロジック
│   │   ├── mentions/               # メンション解析機能
│   │   │   └── index.ts            # メンション処理
│   │   ├── prompts/                # プロンプトに関する処理
│   │   │   ├── responses.ts        # 応答フォーマット処理
│   │   │   └── system.ts           # システムプロンプト設定
│   │   └── sliding-window/         # スライディングウィンドウアルゴリズム
│   │       └── index.ts            # スライディングウィンドウ実装
│   ├── exports/                    # 外部にエクスポートする型や関数
│   │   ├── cline.d.ts              # TypeScript型定義
│   │   └── index.ts                # エクスポートモジュール
│   ├── integrations/               # 外部統合モジュール
│   │   ├── checkpoints/            # チェックポイント追跡
│   │   │   └── CheckpointTracker.ts # チェックポイントの追跡ロジック
│   │   ├── diagnostics/            # 診断モジュール
│   │   │   ├── DiagnosticsMonitor.ts # 診断モニタリング
│   │   │   └── index.ts            # 診断モジュールのエントリーポイント
│   │   ├── editor/                 # エディタ関連の機能
│   │   │   ├── DecorationController.ts # 装飾コントローラー
│   │   │   ├── detect-omission.ts  # 欠落検知
│   │   │   ├── DiffViewProvider.ts # 差分表示プロバイダー
│   │   │   └── detect-omission.ts  # 欠落の検出
│   │   ├── misc/                   # その他の機能
│   │   │   ├── export-markdown.ts  # Markdownエクスポート機能
│   │   │   ├── extract-text.ts     # テキスト抽出機能
│   │   │   ├── open-file.ts        # ファイルを開く機能
│   │   │   └── process-images.ts   # 画像処理機能
│   │   ├── notifications/          # 通知モジュール
│   │   │   └── index.ts            # 通知処理
│   │   ├── terminal/               # ターミナル管理
│   │   │   ├── TerminalManager.ts  # ターミナルの管理
│   │   │   ├── TerminalProcess.ts  # ターミナルプロセス
│   │   │   └── TerminalRegistry.ts # ターミナル登録
│   │   ├── theme/                  # テーマ設定
│   │   │   └── getTheme.ts         # テーマ取得
│   │   └── workspace/              # ワークスペース関連
│   │       ├── get-python-env.ts   # Python環境取得
│   │       └── WorkspaceTracker.ts # ワークスペース追跡
│   ├── services/                   # サービス層
│   │   ├── browser/                # ブラウザ関連
│   │   │   ├── BrowserSession.ts   # ブラウザセッション管理
│   │   │   └── UrlContentFetcher.ts # URLコンテンツ取得
│   │   ├── glob/                   # ファイル検索
│   │   │   └── list-files.ts       # ファイルリスト取得
│   │   ├── mcp/                    # MCPハブ
│   │   │   └── McpHub.ts           # MCPハブ管理
│   │   ├── ripgrep/                # Ripgrep関連
│   │   │   └── index.ts            # 検索ロジック
│   │   └── tree-sitter/            # Tree-sitter関連
│   │       ├── queries/            # 各言語のクエリ
│   │       │   ├── c-sharp.ts
│   │       │   ├── cpp.ts
│   │       │   └── typescript.ts
│   │       ├── index.ts            # Tree-sitterエントリーポイント
│   │       └── languageParser.ts   # 言語パーサ
│   ├── shared/                     # 共有ユーティリティ
│   │   ├── array.ts                # 配列ユーティリティ
│   │   ├── api.ts                  # APIヘルパー
│   │   ├── context-mentions.ts     # メンションコンテキスト
│   │   ├── ExtensionMessage.ts     # 拡張メッセージ
│   │   └── WebviewMessage.ts       # Webviewメッセージ
│   ├── test/                       # テストコード
│   │   └── extension.test.ts       # 拡張機能のテスト
│   ├── utils/                      # 汎用ユーティリティ
│   │   ├── cost.ts                 # コスト計算
│   │   ├── fs.ts                   # ファイルシステムユーティリティ
│   │   └── path.ts                 # パス操作ユーティリティ
│   └── extension.ts                # エクステンションのエントリーポイント
└── README.md                       # プロジェクトの説明
ナインナイン

まずはsrc/api/index.ts

src/api/index.ts の役割は、APIプロバイダーを抽象化して統一的に扱えるようにするエントリーポイントとして機能している。このファイルを通じて、プロジェクトは特定のプロバイダー(例: OpenAI, Anthropic, Google Gemini など)を動的に選択

import { Anthropic } from "@anthropic-ai/sdk"
import { ApiConfiguration, ModelInfo } from "../shared/api"
import { AnthropicHandler } from "./providers/anthropic"
import { AwsBedrockHandler } from "./providers/bedrock"
import { OpenRouterHandler } from "./providers/openrouter"
import { VertexHandler } from "./providers/vertex"
import { OpenAiHandler } from "./providers/openai"
import { OllamaHandler } from "./providers/ollama"
import { LmStudioHandler } from "./providers/lmstudio"
import { GeminiHandler } from "./providers/gemini"
import { OpenAiNativeHandler } from "./providers/openai-native"
import { ApiStream } from "./transform/stream"
import { DeepSeekHandler } from "./providers/deepseek"

export interface ApiHandler {
	createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream
	getModel(): { id: string; info: ModelInfo }
}

export function buildApiHandler(configuration: ApiConfiguration): ApiHandler {
	const { apiProvider, ...options } = configuration
	switch (apiProvider) {
		case "anthropic":
			return new AnthropicHandler(options)
		case "openrouter":
			return new OpenRouterHandler(options)
		case "bedrock":
			return new AwsBedrockHandler(options)
		case "vertex":
			return new VertexHandler(options)
		case "openai":
			return new OpenAiHandler(options)
		case "ollama":
			return new OllamaHandler(options)
		case "lmstudio":
			return new LmStudioHandler(options)
		case "gemini":
			return new GeminiHandler(options)
		case "openai-native":
			return new OpenAiNativeHandler(options)
		case "deepseek":
			return new DeepSeekHandler(options)
		default:
			return new AnthropicHandler(options)
	}
}