Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-node@v7
with:
node-version: 24
cache: npm
# deno fmt until the switch to oxfmt
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- run: deno task ci
- run: npm ci
- run: npm run ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.env.*
.helix

node_modules
7 changes: 7 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"rules": {
"no-control-regex": "off",
"typescript/no-explicit-any": "error"
}
}
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# llm-cli

A simple but full-featured CLI powered by Deno for chatting with LLMs.
A simple but full-featured CLI for chatting with LLMs, written in TypeScript.

Thanks to [Justin Bennett](https://github.com/zephraph) for the
[script](https://github.com/zephraph/deno.run/blob/0972d0cd3d8f050cb11c3a6d1c1c201608d100a9/chat-gpt.ts)
Expand All @@ -21,7 +21,7 @@ And it's easy to add more.
<img width="711" alt="image" src="https://github.com/user-attachments/assets/0acab93c-4062-46db-9779-8e48f9ccc1e9" />

Equations render as images in Ghostty and Kitty, with raw TeX elsewhere. Try
`deno task math-demo` for examples.
`npm run math-demo` for examples.

### Continue chat with replies

Expand Down Expand Up @@ -85,16 +85,16 @@ environment.

### Dependencies

- [Deno](https://docs.deno.com/runtime/manual) (essential)
- [Node](https://nodejs.org/) 24 or later (essential)
- [`gh`](https://cli.github.com/) (GitHub CLI, optional)
- Only needed if you want to upload chats as GitHub gists
- You'll need to be logged in
([`gh auth login`](https://cli.github.com/manual/gh_auth_login))

### Installation

Clone the repo. Then you just need a way to run `main.ts`. I use this function in my
`.zprofile`:
Clone the repo and run `npm install` in it. Then you just need a way to run `main.ts`. I use
this function in my `.zprofile`:

```bash
function ai() {
Expand All @@ -103,11 +103,8 @@ function ai() {
}
```

The `"$@"` passes all command-line arguments to `main.ts`.

You'll need to `chmod +x` the script in order to be able to execute it directly like above.
Otherwise, you could run it with `deno run path/to/main.ts`, but that is less convenient
because you'll have to set all the permissions I've set in the shebang line.
The `"$@"` passes all command-line arguments to `main.ts`, which runs with `node` through
its shebang line. You could also run it with `node path/to/main.ts`.

## Contributions

Expand Down
6 changes: 3 additions & 3 deletions adapters/claude.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Anthropic from "@anthropic-ai/sdk"
import * as R from "remeda"
import { match } from "ts-pattern"

import { postprocessSchemaContent, prepareSchema } from "../schema.ts"
import { getCost } from "../models.ts"
import { parseDataUrl } from "../utils.ts"
import { parseDataUrl } from "../image-input.ts"
import { uniqueBy } from "../utils.ts"
import type { ThinkLevel } from "../types.ts"
import type { ChatInput } from "./types.ts"
import { validateClaudeImageSize, validateRequestSize } from "./request_size.ts"
Expand Down Expand Up @@ -34,7 +34,7 @@ function renderClaudeContentBlock(msg: Anthropic.Beta.Messages.BetaContentBlock)
const citations = (msg.citations || [])
.filter((c) => c.type === "web_search_result_location")
if (citations.length === 0) return msg.text
const unique = R.uniqueBy(citations, (c) => c.url)
const unique = uniqueBy(citations, (c) => c.url)
const sources = unique.map((c) => `[${c.title || c.url}](${c.url})`).join(" · ")
return msg.text + " (" + sources + ")"
} else if (msg.type === "server_tool_use" && msg.name === "web_search") {
Expand Down
21 changes: 11 additions & 10 deletions adapters/claude_test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { assertEquals } from "@std/assert"
import assert from "node:assert/strict"
import { test } from "node:test"

import { claudeThinkParams } from "./claude.ts"

Deno.test("Claude uses adaptive thinking with effort levels", () => {
test("Claude uses adaptive thinking with effort levels", () => {
const params = claudeThinkParams("high")
assertEquals(params.thinking?.type, "adaptive")
assertEquals(params.output_config?.effort, "xhigh")
assertEquals(params.effort, "xhigh")
assert.deepEqual(params.thinking?.type, "adaptive")
assert.deepEqual(params.output_config?.effort, "xhigh")
assert.deepEqual(params.effort, "xhigh")
})

Deno.test("Claude leaves effort at the API default unless asked", () => {
test("Claude leaves effort at the API default unless asked", () => {
const params = claudeThinkParams(undefined)
assertEquals(params.thinking.type, "adaptive")
assertEquals(params.output_config, undefined)
assertEquals(params.max_tokens, 20_000)
assertEquals(claudeThinkParams("off").max_tokens, 8_000)
assert.deepEqual(params.thinking.type, "adaptive")
assert.deepEqual(params.output_config, undefined)
assert.deepEqual(params.max_tokens, 20_000)
assert.deepEqual(claudeThinkParams("off").max_tokens, 8_000)
})
9 changes: 5 additions & 4 deletions adapters/gemini.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { GoogleGenAI, type Part, ThinkingLevel } from "@google/genai"
import { encodeBase64 } from "@std/encoding/base64"
import { Buffer } from "node:buffer"
import process from "node:process"

import { postprocessSchemaContent, prepareSchema } from "../schema.ts"
import { getCost } from "../models.ts"
import { parseDataUrl } from "../utils.ts"
import { parseDataUrl } from "../image-input.ts"
import type { ThinkLevel } from "../types.ts"
import type { ChatInput } from "./types.ts"
import { validateRequestSize } from "./request_size.ts"
Expand All @@ -18,7 +19,7 @@ async function imageToInlinePart(image_url: string): Promise<Part> {
if (!res.ok) throw new Error(`Failed to fetch image: ${res.status}`)
const buf = new Uint8Array(await res.arrayBuffer())
const mimeType = res.headers.get("content-type")?.split(";")[0] || "image/png"
return { inlineData: { mimeType, data: encodeBase64(buf) } }
return { inlineData: { mimeType, data: Buffer.from(buf).toString("base64") } }
}

export function geminiThinkParams(key: string, think: ThinkLevel | undefined) {
Expand All @@ -42,7 +43,7 @@ export function geminiThinkParams(key: string, think: ThinkLevel | undefined) {
export async function geminiCreateMessage(
{ chat, model, config, signal, outputSchema }: ChatInput,
) {
const apiKey = Deno.env.get("GEMINI_API_KEY")
const apiKey = process.env.GEMINI_API_KEY
if (!apiKey) throw Error("GEMINI_API_KEY missing")

const { thinkingLevel, effort } = geminiThinkParams(model.key, config.think)
Expand Down
20 changes: 9 additions & 11 deletions adapters/gemini_test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { assertEquals } from "@std/assert"
import assert from "node:assert/strict"
import { test } from "node:test"

import { ThinkingLevel } from "@google/genai"

import { geminiThinkParams } from "./gemini.ts"

Deno.test("Gemini default sends MEDIUM, or MINIMAL on flash-lite", () => {
assertEquals(geminiThinkParams("gemini-4-argon", undefined), {
test("Gemini default sends MEDIUM, or MINIMAL on flash-lite", () => {
assert.deepEqual(geminiThinkParams("gemini-4-argon", undefined), {
thinkingLevel: ThinkingLevel.MEDIUM,
effort: "medium",
})
assertEquals(geminiThinkParams("gemini-3.5-flash-lite", undefined), {
assert.deepEqual(geminiThinkParams("gemini-3.5-flash-lite", undefined), {
thinkingLevel: ThinkingLevel.MINIMAL,
effort: "minimal",
})
})

Deno.test("--quick only uses MINIMAL on flash-lite", () => {
assertEquals(geminiThinkParams("gemini-3.7-flash", "off").effort, "low")
assertEquals(geminiThinkParams("gemini-3.1-pro-preview", "off").effort, "low")
assertEquals(
geminiThinkParams("gemini-3.5-flash-lite", "off").effort,
"minimal",
)
test("--quick only uses MINIMAL on flash-lite", () => {
assert.deepEqual(geminiThinkParams("gemini-3.7-flash", "off").effort, "low")
assert.deepEqual(geminiThinkParams("gemini-3.1-pro-preview", "off").effort, "low")
assert.deepEqual(geminiThinkParams("gemini-3.5-flash-lite", "off").effort, "minimal")
})
Loading
Loading