retrage.github.io

Creating Custom Gemini Commands in Project IDX

Authors: @retrage @benevolent0505

Translated by Gemini

Project IDX is an experimental development environment provided by Google that can be accessed via a web browser. Built on top of VS Code, one of its key features is the integration of Gemini for development assistance.

In this article, we will introduce a method we’ve discovered to create custom Gemini commands in Project IDX, which is not documented in the official documentation.


Gemini Integration in Project IDX

In Project IDX, Gemini can be used to generate code, provide explanations, and more, similar to GitHub Copilot. While this functionality is integrated into Project IDX, it’s actually implemented as an extension plugin. Like regular VS Code extensions, Gemini plugin logs can be found at the following path:

/home/user/.codeoss-cloudworkstations/data/logs/20240914T120048/exthost1/output_logging_20240914T120116/2-Gemini.log

This log shows that prompts for Gemini are loaded from the following paths:

/opt/code-oss/extensions/monospace-aida/ai/prompts
/home/user/<project-name>/.idx/ai/prompts

Importantly, the second path, .idx/ai/prompts within the workspace, is also searched. This suggests that by placing appropriate prompt files in this location, we might be able to add custom Gemini commands.

Prompt Format

To understand the file format, let’s examine the first path mentioned above. Currently, files with the .njk extension are placed there. .njk is a template language called NunJucks.

chat-add-comments.njk
chat-clear.njk
chat-create.njk
chat-edit.njk
chat-facts.njk
chat-facts-selfaware.njk
chat-idx.njk
chat-name-thread.njk
chat-preamble.njk
chat-suggest-next-prompt.njk
devtools-debug.njk
error-help.njk
explain-selection.njk
sidekick-add-comments.njk
sidekick-create.njk
sidekick-edit.njk
sidekick-error-fix.njk

As an example of a prompt file, chat-add-comments.njk is shown below.

---
name: 'chat-add-comments'
description: 'Add comments to code'
command: 'addComments'
availability: ['chat']
action: 'chat'
stopSequences: ['Explanation of changes:']
requiredContext:
  - selection
---

Please add code comments to the following block of code.

{% if prompt -%}
Follow these additional instructions: "{{ prompt }}"
{% endif %}

```
{{ selectedCode }}
```

Based on this, we can infer that the upper part is metadata for the command, providing information such as:

  • Name
  • Description
  • Where it can be used
  • Strings to stop Gemini’s generation
  • Required context The part below is a NunJucks template representing the prompt given to the actual Gemini.

Based on this, let’s try adding a custom Gemini command.

Adding a Custom Gemini Command

Let’s try adding a command to “translate selected code into an arbitrary programming language.” Create a file named chat-translate-comments.njk in the .idx/ai/prompts directory within the workspace.

---
name: 'chat-translate-comments'
description: 'Translate comments'
command: 'myTranlateComments'
availability: ['chat']
action: 'chat'
stopSequences: ['Explanation of changes:']
requiredContext: 
  - selection
---

Please translate the comments in English below to the following language: "{{ prompt }}"

```
{{ selectedCode }}
```

After saving this, close the Project IDX tab in the browser once. This is because the commands are loaded at startup.

Executing the Custom Gemini Command

Reopen Project IDX, open the target workspace, and try executing the Gemini command we just added. Here, let’s try translating some English comments into Japanese. Select the code, open the Gemini command palette with Cmd+I, and type /myTranslateComments Japanese.

Project IDX Custom Gemini Command

As you can see, the Gemini-suggested changes appear, indicating that the custom Gemini command we added has been executed and the comments have been translated into the specified natural language as per the prompt.

Summary

In this article, we introduced a method for adding Gemini commands in Project IDX that is not documented. While the output is not always stable and may not always produce the expected results, the ability to customize AI-assisted coding is attractive. It is likely that this feature will eventually be documented, allowing users to freely create Gemini commands.