Ask AI

Ask an AI assistant about the code in your tab, explain a result, and review its suggested changes as a diff before you apply them.

The AI Chat panel answers questions about the code in your current tab. It works with Ollama on your own Mac, or with OpenAI, Anthropic or Google Gemini using your own API key. Pick one first: see AI providers and keys.

Open the panel

⌃⌘I, Tools → AI Chat…, or the activity bar opens AI Chat in the side bar. Its header shows the current provider and model; click it to open Settings, where the AI tab holds the provider and model. With no provider chosen, the panel asks you to pick one before you ask anything.

Ask a question

Ask about the tab you're in; the AI sees its code and recent output, and each tab keeps its own chat.

Each tab has its own conversation. Type in the message field and press ↩ or click Send (⇧↩ adds a new line). You don’t need to paste your code: JSLab sends it for you. An empty chat offers starters to fill the message field — Explain this tab, Why does line N fail? when the last run failed, and Add types for the name at the caret — which you can edit before sending. While a reply streams in, Stop cancels the request itself, not just the display, and the reply is marked as stopped part-way. New Chat starts over.

Replies render as Markdown, with highlighted code blocks. Links in a reply open in your browser.

What JSLab sends

Each message to the provider carries:

  • instructions for the assistant, built into JSLab, naming the tab’s language and runtime;
  • the current tab’s code, up to 100 KB, marked with its language, runtime and working-directory name;
  • the most recent run’s output, up to 20 KB, when Send Recent Output is on in Settings → AI. It’s on by default;
  • the conversation so far, trimmed from the oldest messages when it grows too large;
  • your message.

The code always travels ahead of the history, so trimming never drops it. Nothing else from your Mac is sent. With Ollama at its standard local address, none of it leaves your Mac.

Use code from a reply

Insert, replace or preview code from an AI reply as a diff, and apply proposed file changes on purpose.

Each finished code block in a reply has four buttons. They’re hidden while the block is still streaming, so half-written code can’t be inserted.

  • Copy copies the block.
  • Insert at Cursor types it at the caret.
  • Replace Editor replaces the whole tab with it, as one undoable edit.
  • Preview Changes shows the difference as an inline diff before anything changes.

Review changes as a diff

Preview Changes marks the lines that would be removed and shows the added lines in place, inside the editor. Your code isn’t touched until you accept something:

  • Apply accepts one change. The rest stay on screen for review.
  • Apply all accepts every change and closes the preview.
  • Discard closes the preview and leaves your code as it was.

Accepted changes land as minimal edits you can undo.

Proposed file changes

When the tab has a working directory, a reply can propose changes to files in it. A code block labelled with a path, such as a fence that starts with ts path=src/app.ts, proposes writing that file, and delete path=src/old.ts proposes removing one. Ask the assistant to label its code blocks this way when you want file changes.

JSLab lists the proposals under Proposed file changes, with each file marked New file, Changed or Deleted, and all of them ticked. Untick any you don’t want, then click Apply to disk, or Discard to drop them all. Paths have to stay inside the working directory: absolute paths and paths that climb out with .. are refused.

Without a working directory, the panel says so instead.

Explain Result

Right-click any output row and choose Explain Result to ask the AI why your code produced it.

// Why isn't this [1, 2, 10]? Right-click the result and choose Explain Result.
[1, 2, 10].sort();
0.1 + 0.2;

Right-click any output row and choose Explain Result. JSLab opens AI Chat and asks “Explain why line N produces this result:” with the row’s value, or “Explain why the code produces this output:” for a row that has no line. Your code and recent output go with it, as with any message. A very long value is cut to fit, and the prompt says so.

See The output panel.

The conversation is kept

Each tab’s conversation is saved as you go and comes back after you quit and relaunch JSLab. It’s kept to the most recent 200 turns and 1,000,000 characters. New Chat clears it. Closing a tab ends its conversation; Reopen Closed Tab brings both back. Each tab’s file is ai/conversations/<tab id>.json in JSLab’s data folder; see Troubleshooting.

When something goes wrong

The panel says what failed, adds the provider’s own detail when there is one, and offers Retry:

Message What to check
No AI provider is configured. Choose a provider in Settings → AI.
The provider rejected the credentials for this request. The API key is missing or wrong. See AI providers and keys.
Couldn’t reach the provider. For Ollama, that it’s running. Otherwise, your connection and the base URL.
The provider has no such model. The model name in Settings → AI.
The request is longer than this model accepts. Try a shorter file or a new chat.
The provider is rate limiting this request. Try again shortly. Wait a moment and try again.

Last updated