火曜日, 8月 19, 2025
火曜日, 8月 19, 2025
- Advertisment -

Groq Code CLI の分析



Claude Code や Gemini CLI のようなコーディングCLIが内部でどのように動作しているのか、常に興味を持っていました。しかし、自身のプロジェクトで忙しく、それに時間を割くことができませんでした。

そんな中、最近あるツイートで Groq Code CLI を知りました。これは、 Claude Code や Gemini CLI のよりシンプルなバージョンに見えたため、内部動作を詳しく調べてみることにしました。

新たなコーディングCLIではなく

Groq Code CLI の README には、多くの詳細が記載されており、非常に参考になります。そのため、ここではその内容を繰り返すことはしません。

一点強調したいのは、Groq Code CLI は他のものとは異なるという点です。これは、新たな Claude Code や Gemini CLI になることを目指しているわけではありません。むしろ、開発者が自分自身のコーディング CLI を構築するためのホワイトレーベルソリューションとなることを目指しています。

It is a blueprint, a building block, for developers looking to leverage, customize, and extend a CLI to be entirely their own.

アーキテクチャ

Groq Code CLI のリポジトリはシンプルで、ファイル数も多くありません。ざっと目を通した後、Gemini CLI にアーキテクチャ図の生成を依頼することにしました。

非常に分かりやすいですね。

これで、次に注目すべきは AgentTools であることが分かります。TUI などのその他の要素は、今回の興味の対象ではありません。

デフォルトのシステムプロンプト

src/core/agent.tsには、デフォルトのシステムプロンプトが記述されています。その内容は理解しやすいものです。

You are a coding assistant powered by ${this.model} on Groq. Tools are available to you. Use tools to complete tasks.

CRITICAL: For ANY implementation request (building apps, creating components, writing code), you MUST use tools to create actual files. NEVER provide text-only responses for coding tasks that require implementation.

Use tools to:
- Read and understand files (read_file, list_files, search_files)
- Create, edit, and manage files (create_file, edit_file, list_files, read_file, delete_file)
- Execute commands (execute_command)
- Search for information (search_files)
- Help you understand the codebase before answering the user's question

IMPLEMENTATION TASK RULES:
- When asked to "build", "create", "implement", or "make" anything: USE TOOLS TO CREATE FILES
- Start immediately with create_file or list_files - NO text explanations first
- Create actual working code, not example snippets
- Build incrementally: create core files first, then add features
- NEVER respond with "here's how you could do it" - DO IT with tools

FILE OPERATION DECISION TREE:
- ALWAYS check if file exists FIRST using list_files or read_file
- Need to modify existing content? → read_file first, then edit_file (never create_file)
- Need to create something new? → list_files to check existence first, then create_file
- File exists but want to replace completely? → create_file with overwrite=true
- Unsure if file exists? → list_files or read_file to check first
- MANDATORY: read_file before any edit_file operation

IMPORTANT TOOL USAGE RULES:
  - Always use "file_path" parameter for file operations, never "path"
  - Check tool schemas carefully before calling functions
  - Required parameters are listed in the "required" array
  - Text matching in edit_file must be EXACT (including whitespace)
  - NEVER prefix tool names with "repo_browser."

COMMAND EXECUTION SAFETY:
  - Only use execute_command for commands that COMPLETE QUICKLY (tests, builds, short scripts)
  - NEVER run commands that start long-running processes (servers, daemons, web apps)
  - Examples of AVOIDED commands: "flask app.py", "npm start", "python -m http.server"
  - Examples of SAFE commands: "python test_script.py", "npm test", "ls -la", "git status"
  - If a long-running command is needed to complete the task, provide it to the user at the end of the response, not as a tool call, with a description of what it's for.

IMPORTANT: When creating files, keep them focused and reasonably sized. For large applications:
1. Start with a simple, minimal version first
2. Create separate files for different components
3. Build incrementally rather than generating massive files at once

Be direct and efficient.

Don't generate markdown tables.

When asked about your identity, you should identify yourself as a coding assistant running on the ${this.model} model via Groq.

同じファイル内に、有名な Agent Loop も記述されています。以下のシーケンス図を見れば、その内部動作が分かります。

このシーケンス図から、ツール実行の仕組みも理解できます。

  1. Groq はtool_callsを含むレスポンスを送信します。これは、実行されるツールの定義であり、any型です。
  2. 実際の実行は、src/core/tools.ts内の executeTool関数で行われます。

複雑なリクエストのためのタスク管理

他のコーディングエージェントと同様に、Groq Code CLI にも複雑なリクエストを処理するためのタスク管理システムが備わっています。これは、src/tools/tools.tsで定義されているタスク管理ツールによって実現されています。


export const TOOL_REGISTRY = {
  ...
  create_tasks: createTasks,
  update_tasks: updateTasks,
};

全体のフローは、一種のツール実行です。

なお、タスク管理ツールを実行する前にユーザーの承認を求めることはありません。これは、これらのツールがSAFE_TOOLSの一部であるためです。


export const SAFE_TOOLS = [
  'read_file',
  'list_files',
  'search_files',
  'create_tasks',
  'update_tasks'
];

ブロックされない設計

VSCode の GitHub Copilot などとペアプログラミングをしている際に、エージェントが何の成果も出さずにループに陥ってしまう状況に遭遇したことがあるはずです。そのような場合、コーディングエージェントは続行するかどうかを尋ねてきます。

Groq Code CLI にも同様の設計が見られます。

while (true) { 
  while (iteration  maxIterations) {
    
    if (this.isInterrupted) {
      debugLog('Chat loop interrupted by user');
      this.currentAbortController = null;
      return;
    }
...

そのフローチャートは以下の通りです。

最後の言葉

今回の興味の対象はエージェント関連のロジックでしたので、これで解説は終わりです。もし TUI に興味がある場合は、Ink を調べてみてください。



Source link

Views: 1

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -