Skip to content

APIリファレンス

Trae IDE APIリファレンスへようこそ。この包括的なガイドでは、Trae IDEを拡張およびカスタマイズするために利用可能なすべてのAPI、SDK、統合方法について説明します。

概要

Trae IDEは、以下を支援する複数のAPIと拡張ポイントを提供します:

  • カスタム拡張機能とプラグインの作成
  • 外部ツールとサービスとの統合
  • 開発ワークフローの自動化
  • IDEの動作のカスタマイズ
  • AI駆動の開発ツールの構築

APIカテゴリ

コアAPI

  • エディターAPI - コードエディターの制御、ドキュメント管理、テキスト操作の処理
  • ワークスペースAPI - プロジェクト、ファイル、ワークスペース設定の管理
  • UI API - カスタムパネル、ダイアログ、インターフェース要素の作成
  • コマンドAPI - カスタムコマンドとキーボードショートカットの登録

AI・インテリジェンス

開発ツール

拡張機能・プラグイン

クイックスタート

最初の拡張機能の作成

typescript
import { TraeAPI } from '@trae/api';

export function activate(context: TraeAPI.ExtensionContext) {
  // コマンドを登録
  const disposable = TraeAPI.commands.registerCommand('myExtension.helloWorld', () => {
    TraeAPI.window.showInformationMessage('Hello from Trae!');
  });

  context.subscriptions.push(disposable);
}

export function deactivate() {
  // 拡張機能が無効化されたときのクリーンアップ
}

基本的なAPI使用法

typescript
// アクティブなエディターを取得
const editor = TraeAPI.window.activeTextEditor;
if (editor) {
  const document = editor.document;
  const selection = editor.selection;
  
  // 選択されたテキストを取得
  const selectedText = document.getText(selection);
  console.log('Selected text:', selectedText);
}

// 通知を表示
TraeAPI.window.showInformationMessage('操作が完了しました!');

// コマンドを実行
TraeAPI.commands.executeCommand('workbench.action.files.save');

認証

一部のAPIは外部サービスの認証が必要です:

typescript
// AI API認証
const aiClient = new TraeAPI.AI.Client({
  apiKey: process.env.TRAE_AI_API_KEY,
  endpoint: 'https://api.trae.ai'
});

// GitHub統合
const githubAuth = await TraeAPI.auth.getSession('github');
if (githubAuth) {
  const octokit = new Octokit({
    auth: githubAuth.accessToken
  });
}

エラーハンドリング

すべてのAPIは一貫したエラーハンドリングパターンを使用します:

typescript
try {
  const result = await TraeAPI.workspace.openFolder('/path/to/project');
  console.log('Folder opened:', result);
} catch (error) {
  if (error instanceof TraeAPI.FileSystemError) {
    TraeAPI.window.showErrorMessage(`フォルダーを開けませんでした: ${error.message}`);
  } else {
    console.error('予期しないエラー:', error);
  }
}

イベントシステム

Trae IDEはイベント駆動アーキテクチャを使用します:

typescript
// ファイル変更をリッスン
TraeAPI.workspace.onDidChangeTextDocument((event) => {
  console.log('Document changed:', event.document.fileName);
});

// エディター選択変更をリッスン
TraeAPI.window.onDidChangeTextEditorSelection((event) => {
  console.log('Selection changed in:', event.textEditor.document.fileName);
});

// AIチャットイベントをリッスン
TraeAPI.ai.onDidReceiveMessage((message) => {
  console.log('AI message:', message.content);
});

設定

拡張機能マニフェスト

すべての拡張機能にはpackage.jsonマニフェストが必要です:

json
{
  "name": "my-trae-extension",
  "displayName": "My Trae Extension",
  "description": "Trae IDE用のサンプル拡張機能",
  "version": "1.0.0",
  "engines": {
    "trae": "^1.0.0"
  },
  "categories": [
    "Other"
  ],
  "activationEvents": [
    "onCommand:myExtension.helloWorld"
  ],
  "main": "./out/extension.js",
  "contributes": {
    "commands": [
      {
        "command": "myExtension.helloWorld",
        "title": "Hello World"
      }
    ]
  },
  "scripts": {
    "compile": "tsc -p ./",
    "watch": "tsc -watch -p ./"
  },
  "devDependencies": {
    "@trae/api": "^1.0.0",
    "typescript": "^4.9.0"
  }
}

TypeScript設定

json
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES2020",
    "outDir": "out",
    "lib": ["ES2020"],
    "sourceMap": true,
    "rootDir": "src",
    "strict": true
  },
  "exclude": ["node_modules", ".trae-test"]
}

テスト

Traeは拡張機能用のテストユーティリティを提供します:

typescript
import * as assert from 'assert';
import { TraeAPI } from '@trae/api';

suite('拡張機能テストスイート', () => {
  test('サンプルテスト', async () => {
    // 拡張機能の機能をテスト
    const result = await TraeAPI.commands.executeCommand('myExtension.helloWorld');
    assert.strictEqual(result, true);
  });
});

公開

拡張機能をTrae Marketplaceに公開:

bash
# Trae CLIをインストール
npm install -g @trae/cli

# 拡張機能をパッケージ化
trae package

# マーケットプレイスに公開
trae publish

ベストプラクティス

パフォーマンス

  • 重い操作には遅延読み込みを使用
  • リソースを適切に破棄
  • メインスレッドのブロックを避ける
  • 高コストな計算をキャッシュ

ユーザーエクスペリエンス

  • 明確なエラーメッセージを提供
  • 長時間の操作には進捗を表示
  • Traeのデザインガイドラインに従う
  • キーボードショートカットをサポート

セキュリティ

  • すべてのユーザー入力を検証
  • 安全な通信プロトコルを使用
  • 機密データを平文で保存しない
  • 最小権限の原則に従う

移行ガイド

VS Code拡張機能から

Trae IDEは多くのVS Code拡張機能と互換性があるように設計されています:

typescript
// VS Code API
import * as vscode from 'vscode';

// Trae API(類似構造)
import { TraeAPI } from '@trae/api';

// ほとんどのVS Codeパターンが直接動作
const editor = TraeAPI.window.activeTextEditor; // vscode.window.activeTextEditorと同じ

破壊的変更

API変更と移行手順の詳細については、移行ガイドを参照してください。

サポート

サンプル拡張機能のコレクションを探索:


このドキュメントはTrae IDE APIバージョン1.0用です。古いバージョンについては、バージョンアーカイブを参照してください。

究極の AI 駆動 IDE 学習ガイド