├── .gitignore
├── statusline.sh
├── statusline.ps1
├── LICENSE
├── zai.js
└── zai-debug.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | *.log
3 | logs/
4 | *.log.*
5 |
6 | # OS Files
7 | .DS_Store
8 | Thumbs.db
9 | desktop.ini
10 |
11 | # IDE
12 | .vscode/
13 | .idea/
14 | *.swp
15 | *.swo
16 | *~
17 |
18 | # Node modules
19 | node_modules/
20 |
21 | # Personal config files
22 | config.json
23 | settings.json
24 |
25 | # API Keys
26 | .env
27 | .env.local
28 | *.key
29 |
30 | # Temporary files
31 | tmp/
32 | temp/
33 | *.tmp
34 |
--------------------------------------------------------------------------------
/statusline.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | # StatusLine for Claude Code CLI
4 | # Author: Bedolla
5 | # Date: 2025-10-25
6 | # Version: 1.0
7 | # Requirements: bash 4.0+ or zsh 5.0+, Git installed
8 | # Compatibility: macOS, Linux
9 | #
10 | # Description:
11 | # Displays useful information in 2 lines:
12 | # - Line 1: Path | Branch + Git Status | Model
13 | # - Line 2: Cost USD | Session Duration | Lines +/- (Net: N)
14 |
15 | # ============================================================================
16 | # CONFIGURATION
17 | # ============================================================================
18 |
19 | SHOW_DETAILED_GIT=true # Show detailed git indicators (staged, modified, etc.)
20 | SHOW_API_DURATION=false # Show API call duration in addition to total session duration
21 | MAX_PATH_LENGTH=50 # Maximum characters for path before truncation
22 |
23 | # ============================================================================
24 | # FUNCTIONS: GitStatus
25 | # ============================================================================
26 |
27 | # Function: get_git_status
28 | # Description: Extracts Git repository status including branch name, file counts,
29 | # and commit synchronization state with remote.
30 | # Parameters:
31 | # $1 - workspace_dir: Directory path to analyze
32 | # Returns:
33 | # Pipe-separated string: branch|staged|modified|untracked|deleted|conflict|ahead|behind|clean
34 | # Example: "main|2|1|0|0|0|1|0|false"
35 | get_git_status() {
36 | local workspace_dir="$1"
37 |
38 | # Initialize counters and status variables
39 | local branch=""
40 | local staged_files=0 # Files in staging area (git add)
41 | local modified_files=0 # Modified but not staged
42 | local untracked_files=0 # New files not tracked by git
43 | local deleted_files=0 # Deleted files
44 | local conflict_files=0 # Files with merge conflicts
45 | local commits_ahead=0 # Commits ahead of remote
46 | local commits_behind=0 # Commits behind remote
47 | local is_clean=true # True if no pending changes
48 |
49 | # Save current directory to restore later
50 | local original_dir=$(pwd)
51 |
52 | # Change to workspace directory if it exists
53 | if [[ -n "$workspace_dir" && -d "$workspace_dir" ]]; then
54 | cd "$workspace_dir" 2>/dev/null || return
55 | fi
56 |
57 | # Execute git status with machine-readable format
58 | # --porcelain: Stable format for scripts
59 | # --branch: Include branch info in output
60 | local lines
61 | lines=$(git status --porcelain --branch 2>/dev/null)
62 |
63 | # Return to original directory
64 | cd "$original_dir"
65 |
66 | # If no output, not a git repository
67 | if [[ -z "$lines" ]]; then
68 | echo "No Git||||0|0|0|0|0|0|0|false"
69 | return
70 | fi
71 |
72 | # Extract branch information (line starting with ##)
73 | local branch_line=$(echo "$lines" | grep "^##" | head -n 1)
74 |
75 | if [[ -n "$branch_line" ]]; then
76 | # Extract branch name from line like: "## main...origin/main [ahead 1]"
77 | branch=$(echo "$branch_line" | sed -E 's/^## ([^.]+).*/\1/' | sed 's/^## //')
78 |
79 | # Extract commits ahead of remote (if any)
80 | # Uses BASH_REMATCH for bash, falls back to match for zsh
81 | if [[ "$branch_line" =~ ahead\ ([0-9]+) ]]; then
82 | commits_ahead=${BASH_REMATCH[1]:-${match[1]}}
83 | fi
84 |
85 | # Extract commits behind remote (if any)
86 | if [[ "$branch_line" =~ behind\ ([0-9]+) ]]; then
87 | commits_behind=${BASH_REMATCH[1]:-${match[1]}}
88 | fi
89 | fi
90 |
91 | # Process file status lines (exclude branch line)
92 | local file_lines=$(echo "$lines" | grep -v "^##")
93 |
94 | # Parse each file status line
95 | # Format: XY filename (X=staging, Y=working tree)
96 | while IFS= read -r line; do
97 | [[ -z "$line" ]] && continue
98 | [[ ${#line} -lt 2 ]] && continue
99 |
100 | # Extract status codes (first two characters)
101 | local staging_code="${line:0:1}" # Staging area status
102 | local working_code="${line:1:1}" # Working tree status
103 |
104 | # Process staging area status (first column)
105 | # M=Modified, A=Added, R=Renamed, C=Copied, D=Deleted, U=Unmerged, ?=Untracked
106 | case "$staging_code" in
107 | M|A|R|C)
108 | ((staged_files++)) # Ready for commit
109 | ;;
110 | D)
111 | ((staged_files++)) # Deleted and staged
112 | ((deleted_files++))
113 | ;;
114 | U)
115 | ((conflict_files++)) # Merge conflict
116 | ;;
117 | \?)
118 | if [[ "$working_code" == "?" ]]; then
119 | ((untracked_files++)) # New file not tracked
120 | fi
121 | ;;
122 | esac
123 |
124 | # Process working tree status (second column)
125 | case "$working_code" in
126 | M)
127 | ((modified_files++)) # Modified but not staged
128 | ;;
129 | D)
130 | ((deleted_files++)) # Deleted but not staged
131 | ;;
132 | U)
133 | ((conflict_files++)) # Merge conflict
134 | ;;
135 | esac
136 | done <<< "$file_lines"
137 |
138 | # Determine if repository is clean (no pending changes)
139 | if [[ $staged_files -eq 0 && $modified_files -eq 0 && \
140 | $untracked_files -eq 0 && $deleted_files -eq 0 && \
141 | $conflict_files -eq 0 && $commits_ahead -eq 0 && \
142 | $commits_behind -eq 0 ]]; then
143 | is_clean=true
144 | else
145 | is_clean=false
146 | fi
147 |
148 | # Return pipe-separated values
149 | echo "$branch|$staged_files|$modified_files|$untracked_files|$deleted_files|$conflict_files|$commits_ahead|$commits_behind|$is_clean"
150 | }
151 |
152 | # Function: build_git_indicators
153 | # Description: Builds a compact string with Git status indicators using emojis.
154 | # Parameters:
155 | # $1 - branch: Branch name
156 | # $2 - staged: Number of staged files
157 | # $3 - modified: Number of modified files
158 | # $4 - untracked: Number of untracked files
159 | # $5 - deleted: Number of deleted files
160 | # $6 - conflict: Number of files with conflicts
161 | # $7 - ahead: Commits ahead of remote
162 | # $8 - behind: Commits behind remote
163 | # $9 - clean: Boolean indicating if repository is clean
164 | # Returns:
165 | # String like "main ✅2 ✏️1 ⬆️1" or "main ✓" if clean
166 | build_git_indicators() {
167 | local branch="$1"
168 | local staged="$2"
169 | local modified="$3"
170 | local untracked="$4"
171 | local deleted="$5"
172 | local conflict="$6"
173 | local ahead="$7"
174 | local behind="$8"
175 | local clean="$9"
176 |
177 | local indicators=""
178 |
179 | # Build indicator string with emoji + count for each status
180 | [[ $staged -gt 0 ]] && indicators+="✅$staged " # ✅ Staged files
181 | [[ $modified -gt 0 ]] && indicators+="✏️ $modified " # ✏️ Modified files
182 | [[ $untracked -gt 0 ]] && indicators+="📄$untracked " # 📄 Untracked files
183 | [[ $deleted -gt 0 ]] && indicators+="🗑️ $deleted " # 🗑️ Deleted files
184 | [[ $conflict -gt 0 ]] && indicators+="⚠️ $conflict " # ⚠️ Conflicts
185 | [[ $ahead -gt 0 ]] && indicators+="⬆️ $ahead " # ⬆️ Commits ahead
186 | [[ $behind -gt 0 ]] && indicators+="⬇️ $behind " # ⬇️ Commits behind
187 |
188 | # Return formatted status
189 | if [[ -z "$indicators" && "$clean" == "true" ]]; then
190 | echo "$branch ✓" # Clean repository
191 | elif [[ -n "$indicators" ]]; then
192 | echo "$branch ${indicators% }" # Branch with indicators
193 | else
194 | echo "$branch" # Just branch name
195 | fi
196 | }
197 |
198 | # ============================================================================
199 | # FUNCTIONS: Formatting
200 | # ============================================================================
201 |
202 | # Function: format_duration
203 | # Description: Converts milliseconds to human-readable duration.
204 | # Parameters:
205 | # $1 - milliseconds: Duration in milliseconds
206 | # Returns:
207 | # String like "2h 15m 30s", "5m 23s", or "45s"
208 | format_duration() {
209 | local milliseconds=$1
210 | local total_seconds=$((milliseconds / 1000))
211 |
212 | # Calculate hours, minutes, seconds
213 | local hours=$((total_seconds / 3600))
214 | local minutes=$(((total_seconds % 3600) / 60))
215 | local seconds=$((total_seconds % 60))
216 |
217 | local result=""
218 |
219 | # Build result string (only include non-zero components)
220 | [[ $hours -gt 0 ]] && result+="${hours}h "
221 | [[ $minutes -gt 0 ]] && result+="${minutes}m "
222 | [[ $seconds -gt 0 || -z "$result" ]] && result+="${seconds}s" # Always show seconds if nothing else
223 |
224 | # Remove trailing space
225 | echo "${result% }"
226 | }
227 |
228 | # Function: format_path
229 | # Description: Formats and truncates file paths for display.
230 | # Replaces home directory with ~, truncates long paths with ...
231 | # Parameters:
232 | # $1 - full_path: Complete file path
233 | # $2 - max_length: Maximum allowed length
234 | # Returns:
235 | # Formatted path like "~/Projects/MyApp" or ".../src/components"
236 | format_path() {
237 | local full_path="$1"
238 | local max_length=$2
239 |
240 | # Return "unknown" for empty paths
241 | [[ -z "$full_path" ]] && echo "unknown" && return
242 |
243 | local path="$full_path"
244 |
245 | # Replace home directory with ~ for brevity
246 | local home_dir="${HOME}"
247 | if [[ "$path" == "$home_dir"* ]]; then
248 | path="~${path#$home_dir}"
249 | fi
250 |
251 | # If path is short enough, return as-is
252 | if [[ ${#path} -le $max_length ]]; then
253 | echo "$path"
254 | return
255 | fi
256 |
257 | # Split path into parts
258 | IFS='/' read -ra parts <<< "$path"
259 |
260 | # Build truncated path from right to left (keep most recent directories)
261 | local final_parts=()
262 | local current_length=3 # Account for "..."
263 |
264 | # Iterate backwards through path components
265 | for ((i=${#parts[@]}; i>0; i--)); do
266 | local part="${parts[$i]}"
267 | local length_with_part=$((current_length + ${#part} + 1)) # +1 for separator
268 |
269 | # Add part if it fits within max length
270 | if [[ $length_with_part -le $max_length ]]; then
271 | final_parts=("$part" "${final_parts[@]}")
272 | current_length=$length_with_part
273 | else
274 | break # Stop when we exceed max length
275 | fi
276 | done
277 |
278 | # Build final truncated path with "..." prefix
279 | local result=".../"
280 | for part in "${final_parts[@]}"; do
281 | result+="$part/"
282 | done
283 |
284 | # Remove trailing slash
285 | echo "${result%/}"
286 | }
287 |
288 | # Function: get_model_name
289 | # Description: Formats model names for consistent display.
290 | # Applies title case and corrects known model names.
291 | # Parameters:
292 | # $1 - name: Raw model name from API
293 | # Returns:
294 | # Formatted name like "GLM 4.6", "Claude", "GPT", "DeepSeek R1"
295 | get_model_name() {
296 | local name="$1"
297 |
298 | # Return "unknown" for empty names
299 | [[ -z "$name" ]] && echo "unknown" && return
300 |
301 | # Apply title case (capitalize first letter of each word)
302 | name=$(echo "$name" | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2))}1')
303 |
304 | # Apply specific corrections for known model names (ordered from most specific to most generic)
305 | name="${name//Glm-4.6-V/GLM 4.6 V}"
306 | name="${name//Glm-4.6/GLM 4.6}"
307 | name="${name//Glm-4.5-Air/GLM 4.5 Air}"
308 | name="${name//Glm-4.5-V/GLM 4.5 V}"
309 | name="${name//Glm-4.5/GLM 4.5}"
310 | name="${name//Glm-4/GLM 4}"
311 |
312 | echo "$name"
313 | }
314 |
315 | # ============================================================================
316 | # MAIN FUNCTION
317 | # ============================================================================
318 |
319 | # Function: main
320 | # Description: Entry point. Reads JSON from stdin, parses it, and generates
321 | # a 2-line status display with directory, git, model, cost, and lines info.
322 | # Input: JSON from stdin (Claude Code CLI format)
323 | # Output: 2 lines of formatted status information
324 | main() {
325 | # Read JSON input from stdin
326 | local json_input
327 | json_input=$(cat)
328 |
329 | # Exit if no input provided
330 | if [[ -z "$json_input" ]]; then
331 | echo "No input data"
332 | return
333 | fi
334 |
335 | # Extract values from JSON using jq
336 | local full_directory=$(echo "$json_input" | jq -r '.workspace.current_dir // "unknown"')
337 | local model_name=$(echo "$json_input" | jq -r '.model.display_name // "unknown"')
338 | local cost_usd=$(echo "$json_input" | jq -r '.cost.total_cost_usd // 0')
339 | local duration_ms=$(echo "$json_input" | jq -r '.cost.total_duration_ms // 0')
340 | local api_duration_ms=$(echo "$json_input" | jq -r '.cost.total_api_duration_ms // 0')
341 | local lines_added=$(echo "$json_input" | jq -r '.cost.total_lines_added // 0')
342 | local lines_removed=$(echo "$json_input" | jq -r '.cost.total_lines_removed // 0')
343 |
344 | # Format directory path with truncation
345 | local directory_path=$(format_path "$full_directory" $MAX_PATH_LENGTH)
346 |
347 | # Get Git status and parse results
348 | local git_status=$(get_git_status "$full_directory")
349 | IFS='|' read -r branch staged modified untracked deleted conflict ahead behind clean <<< "$git_status"
350 |
351 | # Build Git info string (detailed or simple)
352 | local git_info
353 | if [[ "$SHOW_DETAILED_GIT" == true ]]; then
354 | git_info=$(build_git_indicators "$branch" "$staged" "$modified" "$untracked" "$deleted" "$conflict" "$ahead" "$behind" "$clean")
355 | else
356 | git_info="$branch" # Just branch name
357 | fi
358 |
359 | # Format model name
360 | local model=$(get_model_name "$model_name")
361 |
362 | # Format cost with 2 decimal places
363 | local formatted_cost=$(printf "\$%.2f USD" "$cost_usd")
364 |
365 | # Format session duration
366 | local session_duration=$(format_duration "$duration_ms")
367 |
368 | # Calculate net lines changed
369 | local net=$((lines_added - lines_removed))
370 | local lines_info="+${lines_added}/-${lines_removed} (Net: ${net})"
371 |
372 | # Choose Git emoji based on repository status
373 | local git_emoji
374 | if [[ "$branch" == "No Git" ]]; then
375 | git_emoji="📦" # Package emoji for non-git directories
376 | else
377 | git_emoji="🍃" # Leaf emoji for git repositories
378 | fi
379 |
380 | # LINE 1: Directory | Git Status | Model
381 | local line1="🗂️ ${directory_path} | ${git_emoji} ${git_info} | 🤖 ${model}"
382 |
383 | # LINE 2: Cost | Duration | Lines
384 | local line2="💵 ${formatted_cost} | ⏱️ ${session_duration}"
385 |
386 | # Optionally append API duration
387 | if [[ "$SHOW_API_DURATION" == true ]]; then
388 | local api_duration_sec=$(echo "scale=1; $api_duration_ms / 1000" | bc)
389 | line2+=" (API: ${api_duration_sec}s)"
390 | fi
391 |
392 | # Append lines info
393 | line2+=" | ✏️ ${lines_info}"
394 |
395 | # Output both lines
396 | echo "$line1"
397 | echo "$line2"
398 | }
399 |
400 | # Execute main function
401 | main
402 |
--------------------------------------------------------------------------------
/statusline.ps1:
--------------------------------------------------------------------------------
1 | #Requires -Version 7.0
2 |
3 | <#
4 | .SYNOPSIS
5 | StatusLine for Claude Code CLI.
6 |
7 | .DESCRIPTION
8 | Displays useful information in 2 lines:
9 | - Line 1: Path | Branch + Git Status | Model
10 | - Line 2: Cost USD | Session Duration | Lines +/- (Net: N)
11 |
12 | .NOTES
13 | Author: Bedolla
14 | Date: 2025-10-25
15 | Version: 1.0
16 | Requirements: PowerShell 7.0+, Git installed
17 | Compatibility: Windows, macOS, Linux
18 | #>
19 |
20 | # ============================================================================
21 | # CONFIGURATION
22 | # ============================================================================
23 |
24 | [bool]$SHOW_DETAILED_GIT = $true # Show detailed git indicators (staged, modified, etc.)
25 | [bool]$SHOW_API_DURATION = $false # Show API call duration in addition to total session duration
26 | [int]$MAX_PATH_LENGTH = 50 # Maximum characters for path before truncation
27 |
28 | # ============================================================================
29 | # CLASS: GitStatus
30 | # ============================================================================
31 |
32 | class GitStatus {
33 | [string]$Branch
34 | [int]$StagedFiles
35 | [int]$ModifiedFiles
36 | [int]$UntrackedFiles
37 | [int]$DeletedFiles
38 | [int]$ConflictFiles
39 | [int]$CommitsAhead
40 | [int]$CommitsBehind
41 | [bool]$IsClean
42 |
43 | GitStatus() {
44 | # Initialize all counters and status variables
45 | $this.Branch = ""
46 | $this.StagedFiles = 0 # Files in staging area (git add)
47 | $this.ModifiedFiles = 0 # Modified but not staged
48 | $this.UntrackedFiles = 0 # New files not tracked by git
49 | $this.DeletedFiles = 0 # Deleted files
50 | $this.ConflictFiles = 0 # Files with merge conflicts
51 | $this.CommitsAhead = 0 # Commits ahead of remote
52 | $this.CommitsBehind = 0 # Commits behind remote
53 | $this.IsClean = $false # True if no pending changes
54 | }
55 |
56 | # Method: Get Git status using git status --porcelain
57 | [void] GetStatus([string]$workspaceDir) {
58 | try {
59 | # Save current directory to restore later
60 | [string]$originalDir = Get-Location
61 |
62 | # Change to workspace directory if it exists
63 | if (-not [string]::IsNullOrWhiteSpace($workspaceDir) -and (Test-Path $workspaceDir)) {
64 | Set-Location $workspaceDir
65 | }
66 |
67 | # Execute git status with machine-readable format
68 | # --porcelain: Stable format for scripts
69 | # --branch: Include branch info in output
70 | [string[]]$lines = & git status --porcelain --branch 2>$null
71 |
72 | # Return to original directory
73 | Set-Location $originalDir
74 |
75 | # If no output, not a git repository
76 | if ($null -eq $lines -or $lines.Count -eq 0) {
77 | $this.Branch = "No Git"
78 | return
79 | }
80 |
81 | # Extract branch information (first line starting with ##)
82 | [string]$branchLine = $lines | Where-Object { $_.StartsWith("##") } | Select-Object -First 1
83 | if ($branchLine) {
84 | # Extract branch name from line like: "## main...origin/main [ahead 1]"
85 | if ($branchLine -match "^## ([^\\.]+)") {
86 | $this.Branch = $matches[1]
87 | }
88 | elseif ($branchLine -match "^## (.+)") {
89 | $this.Branch = $matches[1]
90 | }
91 |
92 | # Extract commits ahead/behind remote (if any)
93 | if ($branchLine -match "ahead (\\d+)") {
94 | $this.CommitsAhead = [int]$matches[1]
95 | }
96 | if ($branchLine -match "behind (\\d+)") {
97 | $this.CommitsBehind = [int]$matches[1]
98 | }
99 | }
100 |
101 | # Process file status lines (exclude branch line)
102 | [string[]]$fileLines = $lines | Where-Object { -not $_.StartsWith("##") }
103 |
104 | # Parse each file status line
105 | # Format: XY filename (X=staging, Y=working tree)
106 | foreach ($line in $fileLines) {
107 | if ($line.Length -lt 2) { continue }
108 |
109 | # Extract status codes (first two characters)
110 | [char]$stagingCode = $line[0] # Staging area status
111 | [char]$workingTreeCode = $line[1] # Working tree status
112 |
113 | # Process staging area status (first column)
114 | # M=Modified, A=Added, R=Renamed, C=Copied, D=Deleted, U=Unmerged, ?=Untracked
115 | if ($stagingCode -eq 'M' -or $stagingCode -eq 'A' -or $stagingCode -eq 'R' -or $stagingCode -eq 'C') {
116 | $this.StagedFiles++ # Ready for commit
117 | }
118 | if ($stagingCode -eq 'D') {
119 | $this.StagedFiles++ # Deleted and staged
120 | $this.DeletedFiles++
121 | }
122 |
123 | # Process working tree status (second column)
124 | if ($workingTreeCode -eq 'M') {
125 | $this.ModifiedFiles++ # Modified but not staged
126 | }
127 | if ($workingTreeCode -eq 'D') {
128 | $this.DeletedFiles++ # Deleted but not staged
129 | }
130 |
131 | # Untracked files - count individual files within folders
132 | if ($stagingCode -eq '?' -and $workingTreeCode -eq '?') {
133 | # If it's a folder (ends with /), count individual files inside
134 | if ($line.TrimEnd().EndsWith('/')) {
135 | [string]$folder = $line.Substring(3).TrimEnd('/')
136 | [string[]]$filesInFolder = & git ls-files --others --exclude-standard -- "$folder/" 2>$null
137 | if ($null -ne $filesInFolder -and $filesInFolder.Count -gt 0) {
138 | $this.UntrackedFiles += $filesInFolder.Count
139 | }
140 | }
141 | else {
142 | # It's an individual file
143 | $this.UntrackedFiles++ # New file not tracked
144 | }
145 | }
146 |
147 | # Detect merge conflicts (various patterns)
148 | if ($stagingCode -eq 'U' -or $workingTreeCode -eq 'U') {
149 | $this.ConflictFiles++ # Unmerged file
150 | }
151 | if ($stagingCode -eq 'A' -and $workingTreeCode -eq 'A') {
152 | $this.ConflictFiles++ # Both added
153 | }
154 | if ($stagingCode -eq 'D' -and $workingTreeCode -eq 'D') {
155 | $this.ConflictFiles++ # Both deleted
156 | }
157 | }
158 |
159 | # Determine if repository is clean (no pending changes)
160 | $this.IsClean = (
161 | $this.StagedFiles -eq 0 -and
162 | $this.ModifiedFiles -eq 0 -and
163 | $this.UntrackedFiles -eq 0 -and
164 | $this.DeletedFiles -eq 0 -and
165 | $this.ConflictFiles -eq 0 -and
166 | $this.CommitsAhead -eq 0 -and
167 | $this.CommitsBehind -eq 0
168 | )
169 | }
170 | catch {
171 | $this.Branch = "error"
172 | }
173 | }
174 |
175 | # Method: Build compact indicators string
176 | # Returns string with emoji indicators for each status type
177 | [string] BuildIndicators() {
178 | [System.Collections.Generic.List[string]]$indicators = @()
179 |
180 | if ($this.StagedFiles -gt 0) {
181 | # ✅ Staged files (ready for commit)
182 | $indicators.Add("$([char]::ConvertFromUtf32(0x2705)) $($this.StagedFiles)") # ✅
183 | }
184 | if ($this.ModifiedFiles -gt 0) {
185 | # ✏️ Modified files (unstaged)
186 | $indicators.Add("$([char]::ConvertFromUtf32(0x270F))$([char]::ConvertFromUtf32(0xFE0F)) $($this.ModifiedFiles)") # ✏️
187 | }
188 | if ($this.UntrackedFiles -gt 0) {
189 | # 📄 New files without tracking
190 | $indicators.Add("$([char]::ConvertFromUtf32(0x1F4C4)) $($this.UntrackedFiles)") # 📄
191 | }
192 | if ($this.DeletedFiles -gt 0) {
193 | # 🗑️ Deleted files
194 | $indicators.Add("$([char]::ConvertFromUtf32(0x1F5D1))$([char]::ConvertFromUtf32(0xFE0F)) $($this.DeletedFiles)") # 🗑️
195 | }
196 | if ($this.ConflictFiles -gt 0) {
197 | # ⚠️ Files with conflicts
198 | $indicators.Add("$([char]::ConvertFromUtf32(0x26A0))$([char]::ConvertFromUtf32(0xFE0F)) $($this.ConflictFiles)") # ⚠️
199 | }
200 | if ($this.CommitsAhead -gt 0) {
201 | # ⬆️ Commits ahead of remote
202 | $indicators.Add("$([char]::ConvertFromUtf32(0x2B06))$([char]::ConvertFromUtf32(0xFE0F)) $($this.CommitsAhead)") # ⬆️
203 | }
204 | if ($this.CommitsBehind -gt 0) {
205 | # ⬇️ Commits behind remote
206 | $indicators.Add("$([char]::ConvertFromUtf32(0x2B07))$([char]::ConvertFromUtf32(0xFE0F)) $($this.CommitsBehind)") # ⬇️
207 | }
208 |
209 | # Return formatted status
210 | if ($indicators.Count -eq 0 -and $this.IsClean) {
211 | # ✓ Clean repository
212 | return "$([char]::ConvertFromUtf32(0x2713))" # ✓
213 | }
214 |
215 | # Join all indicators with spaces
216 | return [string]::Join(" ", $indicators)
217 | }
218 |
219 | # Method: Get complete Git string (branch + indicators)
220 | # Returns branch name with status indicators
221 | [string] GetFullStatus() {
222 | [string]$indicators = $this.BuildIndicators()
223 |
224 | if ([string]::IsNullOrWhiteSpace($indicators)) {
225 | return $this.Branch # Just branch name
226 | }
227 |
228 | return "$($this.Branch) $indicators" # Branch with indicators
229 | }
230 | }
231 |
232 | # ============================================================================
233 | # CLASS: DurationFormatter
234 | # ============================================================================
235 |
236 | class DurationFormatter {
237 | # Static method: Format milliseconds to readable format (5m 23s, 2h 15m, etc.)
238 | static [string] FormatDuration([double]$milliseconds) {
239 | [int]$totalSeconds = [Math]::Floor($milliseconds / 1000)
240 |
241 | # Calculate hours, minutes, seconds
242 | [int]$hours = [Math]::Floor($totalSeconds / 3600)
243 | [int]$minutes = [Math]::Floor(($totalSeconds % 3600) / 60)
244 | [int]$seconds = $totalSeconds % 60
245 |
246 | [System.Collections.Generic.List[string]]$parts = @()
247 |
248 | # Build result string (only include non-zero components)
249 | if ($hours -gt 0) {
250 | $parts.Add("$($hours)h")
251 | }
252 | if ($minutes -gt 0) {
253 | $parts.Add("$($minutes)m")
254 | }
255 | if ($seconds -gt 0 -or $parts.Count -eq 0) {
256 | $parts.Add("$($seconds)s") # Always show seconds if nothing else
257 | }
258 |
259 | return [string]::Join(" ", $parts)
260 | }
261 | }
262 |
263 | # ============================================================================
264 | # CLASS: PathFormatter
265 | # ============================================================================
266 |
267 | class PathFormatter {
268 | # Static method: Format full path with truncation logic
269 | # Replaces home directory with ~, truncates long paths with ...
270 | static [string] FormatPath([string]$fullPath, [int]$maxLength) {
271 | # Return "unknown" for empty paths
272 | if ([string]::IsNullOrWhiteSpace($fullPath)) {
273 | return "unknown"
274 | }
275 |
276 | # Normalize path separators for current OS
277 | [string]$separator = [System.IO.Path]::DirectorySeparatorChar
278 | [string]$path = $fullPath.Replace('/', $separator).Replace('\\', $separator)
279 |
280 | # Replace home directory with ~ for brevity
281 | [string]$userProfile = if ($env:USERPROFILE) { $env:USERPROFILE } else { $env:HOME }
282 | if ($path.StartsWith($userProfile, [StringComparison]::OrdinalIgnoreCase)) {
283 | $path = "~" + $path.Substring($userProfile.Length)
284 | }
285 |
286 | # If path is short enough, return as-is
287 | if ($path.Length -le $maxLength) {
288 | return $path
289 | }
290 |
291 | # Split path into components
292 | [string[]]$parts = $path.Split([System.IO.Path]::DirectorySeparatorChar)
293 |
294 | # Build truncated path from right to left (keep most recent directories)
295 | [System.Collections.Generic.List[string]]$finalParts = @()
296 | [int]$currentLength = 3 # Account for "..."
297 |
298 | # Iterate backwards through path components
299 | for ([int]$i = $parts.Length - 1; $i -ge 0; $i--) {
300 | [string]$part = $parts[$i]
301 | [int]$lengthWithPart = $currentLength + $part.Length + 1 # +1 for separator
302 |
303 | # Add part if it fits within max length
304 | if ($lengthWithPart -le $maxLength) {
305 | $finalParts.Insert(0, $part)
306 | $currentLength = $lengthWithPart
307 | }
308 | else {
309 | break # Stop when we exceed max length
310 | }
311 | }
312 |
313 | # Build final truncated path with "..." prefix
314 | return "..." + [System.IO.Path]::DirectorySeparatorChar + [string]::Join([System.IO.Path]::DirectorySeparatorChar, $finalParts)
315 | }
316 | }
317 |
318 | # ============================================================================
319 | # CLASS: StatusLineRenderer
320 | # ============================================================================
321 |
322 | class StatusLineRenderer {
323 | [PSCustomObject]$InputData
324 | [bool]$ShowDetailedGit
325 | [bool]$ShowApiDuration
326 | [int]$MaxPathLength
327 |
328 | StatusLineRenderer([PSCustomObject]$data, [bool]$showGit, [bool]$showApi, [int]$maxLength) {
329 | $this.InputData = $data
330 | $this.ShowDetailedGit = $showGit
331 | $this.ShowApiDuration = $showApi
332 | $this.MaxPathLength = $maxLength
333 | }
334 |
335 | # Method: Get formatted directory path
336 | [string] GetDirectoryPath() {
337 | [string]$fullDirectory = $this.InputData.workspace.current_dir
338 |
339 | if ([string]::IsNullOrWhiteSpace($fullDirectory)) {
340 | return "unknown"
341 | }
342 |
343 | return [PathFormatter]::FormatPath($fullDirectory, $this.MaxPathLength)
344 | }
345 |
346 | # Method: Get Git information
347 | [string] GetGitInfo() {
348 | [string]$workspaceDir = $this.InputData.workspace.current_dir
349 |
350 | [GitStatus]$gitStatus = [GitStatus]::new()
351 | $gitStatus.GetStatus($workspaceDir)
352 |
353 | if ($this.ShowDetailedGit) {
354 | return $gitStatus.GetFullStatus()
355 | }
356 | else {
357 | return $gitStatus.Branch
358 | }
359 | }
360 |
361 | # Method: Get current model name
362 | # Formats model names for consistent display with title case and corrections
363 | [string] GetModelName() {
364 | [string]$modelName = $this.InputData.model.display_name
365 |
366 | # Return "unknown" for empty names
367 | if ([string]::IsNullOrWhiteSpace($modelName)) {
368 | return "unknown"
369 | }
370 |
371 | # Apply title case (capitalize first letter of each word)
372 | # Example: "glm-4.6" → "Glm-4.6", "claude-3.5-sonnet" → "Claude-3.5-Sonnet"
373 | $modelName = (Get-Culture).TextInfo.ToTitleCase($modelName.ToLower())
374 |
375 | # Apply specific corrections for known model names (ordered from most specific to most generic)
376 | $modelName = $modelName.Replace("Glm-4.6-V", "GLM 4.6 V")
377 | $modelName = $modelName.Replace("Glm-4.6", "GLM 4.6")
378 | $modelName = $modelName.Replace("Glm-4.5-Air", "GLM 4.5 Air")
379 | $modelName = $modelName.Replace("Glm-4.5-V", "GLM 4.5 V")
380 | $modelName = $modelName.Replace("Glm-4.5", "GLM 4.5")
381 | $modelName = $modelName.Replace("Glm-4", "GLM 4")
382 |
383 | return $modelName
384 | }
385 |
386 | # Method: Get total cost in USD
387 | # Formats cost with 2 decimal places
388 | [string] GetCostUsd() {
389 | [double]$costUsd = 0.0
390 |
391 | if ($null -ne $this.InputData.cost.total_cost_usd) {
392 | $costUsd = [double]$this.InputData.cost.total_cost_usd
393 | }
394 |
395 | return "`$$($costUsd.ToString('F2')) USD"
396 | }
397 |
398 | # Method: Get formatted session duration
399 | [string] GetSessionDuration() {
400 | [double]$durationMs = 0.0
401 |
402 | if ($null -ne $this.InputData.cost.total_duration_ms) {
403 | $durationMs = [double]$this.InputData.cost.total_duration_ms
404 | }
405 |
406 | return [DurationFormatter]::FormatDuration($durationMs)
407 | }
408 |
409 | # Method: Get formatted API duration (optional)
410 | [string] GetApiDuration() {
411 | [double]$apiDurationMs = 0.0
412 |
413 | if ($null -ne $this.InputData.cost.total_api_duration_ms) {
414 | $apiDurationMs = [double]$this.InputData.cost.total_api_duration_ms
415 | }
416 |
417 | [double]$apiDurationSeconds = $apiDurationMs / 1000.0
418 | return "$($apiDurationSeconds.ToString('F1'))s"
419 | }
420 |
421 | # Method: Get code lines information
422 | # Returns formatted string with added/removed lines and net change
423 | [string] GetLinesInfo() {
424 | [int]$linesAdded = 0
425 | [int]$linesRemoved = 0
426 |
427 | if ($null -ne $this.InputData.cost.total_lines_added) {
428 | $linesAdded = [int]$this.InputData.cost.total_lines_added
429 | }
430 | if ($null -ne $this.InputData.cost.total_lines_removed) {
431 | $linesRemoved = [int]$this.InputData.cost.total_lines_removed
432 | }
433 |
434 | # Calculate net lines changed
435 | [int]$net = $linesAdded - $linesRemoved
436 |
437 | return "+$linesAdded/-$linesRemoved (Net: $net)"
438 | }
439 |
440 | # Method: Render complete StatusLine (2 lines)
441 | # Returns formatted 2-line status display
442 | [string] Render() {
443 | # Emojis with UTF-32 codes
444 | [string]$emojiFolder = [char]::ConvertFromUtf32(0x1F5C2) # 🗂️
445 | [string]$emojiLeaf = [char]::ConvertFromUtf32(0x1F343) # 🍃
446 | [string]$emojiPackage = [char]::ConvertFromUtf32(0x1F4E6) # 📦
447 | [string]$emojiRobot = [char]::ConvertFromUtf32(0x1F916) # 🤖
448 | [string]$emojiMoney = [char]::ConvertFromUtf32(0x1F4B5) # 💵
449 | [string]$emojiClock = [char]::ConvertFromUtf32(0x23F1) # ⏱️
450 | [string]$emojiPencil = [char]::ConvertFromUtf32(0x270F) # ✏️
451 |
452 | # Gather all components for display
453 | [string]$directoryPath = $this.GetDirectoryPath()
454 | [string]$gitInfo = $this.GetGitInfo()
455 | [string]$modelName = $this.GetModelName()
456 | [string]$costUsd = $this.GetCostUsd()
457 | [string]$sessionDuration = $this.GetSessionDuration()
458 | [string]$linesInfo = $this.GetLinesInfo()
459 |
460 | # LINE 1: Directory | Git | Model
461 | # Choose Git emoji based on repository status
462 | [string]$gitEmoji = if ($gitInfo -eq "No Git") { $emojiPackage } else { $emojiLeaf } # 📦 or 🍃
463 |
464 | # Build line 1 with extra space after folder emoji for visual separation
465 | [string]$line1 = "$emojiFolder $directoryPath | $gitEmoji $gitInfo | $emojiRobot $modelName"
466 |
467 | # LINE 2: Cost | Duration | Lines
468 | # Build line 2 with extra space after clock and pencil emojis
469 | [string]$line2 = "$emojiMoney $costUsd | $emojiClock $sessionDuration"
470 |
471 | # Optionally append API duration
472 | if ($this.ShowApiDuration) {
473 | [string]$apiDuration = $this.GetApiDuration()
474 | $line2 += " (API: $apiDuration)"
475 | }
476 |
477 | # Append lines info
478 | $line2 += " | $emojiPencil $linesInfo"
479 |
480 | # Return 2 lines separated by newline
481 | return "$line1`n$line2"
482 | }
483 | }
484 |
485 | # ============================================================================
486 | # MAIN FUNCTION
487 | # ============================================================================
488 |
489 | # Main function: Entry point for the script
490 | # Reads JSON from stdin, parses it, and generates 2-line status display
491 | function Main {
492 | try {
493 | # Read JSON input from stdin (provided by Claude Code CLI)
494 | [string]$jsonInput = [Console]::In.ReadToEnd()
495 |
496 | # Exit if no input provided
497 | if ([string]::IsNullOrWhiteSpace($jsonInput)) {
498 | Write-Output "No input data"
499 | return
500 | }
501 |
502 | # Parse JSON into PowerShell object
503 | [PSCustomObject]$data = $jsonInput | ConvertFrom-Json
504 |
505 | # Create renderer with global configuration
506 | [StatusLineRenderer]$renderer = [StatusLineRenderer]::new(
507 | $data,
508 | $SHOW_DETAILED_GIT,
509 | $SHOW_API_DURATION,
510 | $MAX_PATH_LENGTH
511 | )
512 |
513 | # Render and display 2-line output
514 | [string]$output = $renderer.Render()
515 | Write-Output $output
516 | }
517 | catch {
518 | # In case of error, show simple message
519 | Write-Output "Error in StatusLine: $($_.Exception.Message)"
520 | }
521 | }
522 |
523 | # Execute main function
524 | Main
525 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (C) 2025 Bedolla
2 |
3 | GNU AFFERO GENERAL PUBLIC LICENSE
4 | Version 3, 19 November 2007
5 |
6 | Copyright (C) 2007 Free Software Foundation, Inc.
7 | Everyone is permitted to copy and distribute verbatim copies
8 | of this license document, but changing it is not allowed.
9 |
10 | Preamble
11 |
12 | The GNU Affero General Public License is a free, copyleft license for
13 | software and other kinds of works, specifically designed to ensure
14 | cooperation with the community in the case of network server software.
15 |
16 | The licenses for most software and other practical works are designed
17 | to take away your freedom to share and change the works. By contrast,
18 | our General Public Licenses are intended to guarantee your freedom to
19 | share and change all versions of a program—to make sure it remains free
20 | software for all its users.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | Developers that use our General Public Licenses protect your rights
30 | with two steps: (1) assert copyright on the software, and (2) offer
31 | you this License which gives you legal permission to copy, distribute
32 | and/or modify the software.
33 |
34 | A secondary benefit of defending all users' freedom is that
35 | improvements made in alternate versions of the program, if they
36 | receive widespread use, become available for other developers to
37 | incorporate. Many developers of free software are heartened and
38 | encouraged by the resulting cooperation. However, in the case of
39 | software used on network servers, this result may fail to come about.
40 | The GNU General Public License permits making a modified version and
41 | letting the public access it on a server without ever releasing its
42 | source code to the public.
43 |
44 | The GNU Affero General Public License is designed specifically to
45 | ensure that, in such cases, the modified source code becomes available
46 | to the community. It requires the operator of a network server to
47 | provide the source code of the modified version running there to the
48 | users of that server. Therefore, public use of a modified version, on a
49 | publicly accessible server, gives the public access to the source code
50 | of the modified version.
51 |
52 | An older license, called the Affero General Public License and
53 | published by Affero, was designed to accomplish similar goals. This is
54 | a different license, not a version of the Affero GPL, but Affero has
55 | released a new version of the Affero GPL which permits relicensing
56 | under this license.
57 |
58 | The precise terms and conditions for copying, distribution and
59 | modification follow.
60 |
61 | TERMS AND CONDITIONS
62 |
63 | 0. Definitions.
64 |
65 | “This License” refers to version 3 of the GNU Affero General Public License.
66 |
67 | “Copyright” also means copyright-like laws that apply to other kinds of
68 | works, such as semiconductor masks.
69 |
70 | “The Program” refers to any copyrightable work licensed under this
71 | License. Each licensee is addressed as “you”. “Licensees” and
72 | “recipients” may be individuals or organizations.
73 |
74 | To “modify” a work means to copy from or adapt all or part of the work
75 | in a fashion requiring copyright permission, other than the making of an
76 | exact copy. The resulting work is called a “modified version” of the
77 | earlier work or a work “based on” the earlier work.
78 |
79 | A “covered work” means either the unmodified Program or a work based
80 | on the Program.
81 |
82 | To “propagate” a work means to do anything with it that, without
83 | permission, would make you directly or secondarily liable for
84 | infringement under applicable copyright law, except executing it on a
85 | computer or modifying a private copy. Propagation includes copying,
86 | distribution (with or without modification), making available to the
87 | public, and in some countries other activities as well.
88 |
89 | To “convey” a work means any kind of propagation that enables other
90 | parties to make or receive copies. Mere interaction with a user through
91 | a computer network, with no transfer of a copy, is not conveying.
92 |
93 | An interactive user interface displays “Appropriate Legal Notices” to
94 | the extent that it includes a convenient and prominently visible feature
95 | that (1) displays an appropriate copyright notice, and (2) tells the
96 | user that there is no warranty for the work (except to the extent that
97 | warranties are provided), that licensees may convey the work under this
98 | License, and how to view a copy of this License. If the interface
99 | presents a list of user commands or options, such as a menu, a
100 | prominent item in the list meets this criterion.
101 |
102 | 1. Source Code.
103 |
104 | The “source code” for a work means the preferred form of the work for
105 | making modifications to it. “Object code” means any non-source form of a
106 | work.
107 |
108 | A “Standard Interface” means an interface that either is an official
109 | standard defined by a recognized standards body, or, in the case of
110 | interfaces specified for a particular programming language, one that is
111 | widely used among developers working in that language.
112 |
113 | The “System Libraries” of an executable work include anything, other
114 | than the work as a whole, that (a) is included in the normal form of
115 | packaging a Major Component, but which is not part of that Major
116 | Component, and (b) serves only to enable use of the work with that
117 | Major Component, or to implement a Standard Interface for which an
118 | implementation is available to the public in source code form. A “Major
119 | Component”, in this context, means a major essential component (kernel,
120 | window system, and so on) of the specific operating system (if any) on
121 | which the executable work runs, or a compiler used to produce the work,
122 | or an object code interpreter used to run it.
123 |
124 | The “Corresponding Source” for a work in object code form means all the
125 | source code needed to generate, install, and (for an executable work) run
126 | the object code and to modify the work, including scripts to control
127 | those activities. However, it does not include the work's System
128 | Libraries, or general-purpose tools or generally available free programs
129 | which are used unmodified in performing those activities but which are
130 | not part of the work. For example, Corresponding Source includes
131 | interface definition files associated with source files for the work,
132 | and the source code for shared libraries and dynamically linked
133 | subprograms that the work is specifically designed to require, such as by
134 | intimate data communication or control flow between those subprograms
135 | and other parts of the work.
136 |
137 | The Corresponding Source need not include anything that users can
138 | regenerate automatically from other parts of the Corresponding Source.
139 |
140 | The Corresponding Source for a work in source code form is that same
141 | work.
142 |
143 | 2. Basic Permissions.
144 |
145 | All rights granted under this License are granted for the term of
146 | copyright on the Program, and are irrevocable provided the stated
147 | conditions are met. This License explicitly affirms your unlimited
148 | permission to run the unmodified Program. The output from running a
149 | covered work is covered by this License only if the output, given its
150 | content, constitutes a covered work. This License acknowledges your
151 | rights of fair use or other equivalent, as provided by copyright law.
152 |
153 | You may make, run and propagate covered works that you do not convey,
154 | without conditions so long as your license otherwise remains in force.
155 | You may convey covered works to others for the sole purpose of having
156 | them make modifications exclusively for you, or provide you with
157 | facilities for running those works, provided that you comply with the
158 | terms of this License in conveying all material for which you do not
159 | control copyright. Those thus making or running the covered works for
160 | you must do so exclusively on your behalf, under your direction and
161 | control, on terms that prohibit them from making any copies of your
162 | copyrighted material outside their relationship with you.
163 |
164 | Conveying under any other circumstances is permitted solely under the
165 | conditions stated below. Sublicensing is not allowed; section 10 makes
166 | it unnecessary.
167 |
168 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
169 |
170 | No covered work shall be deemed part of an effective technological
171 | measure under any applicable law fulfilling obligations under article 11
172 | of the WIPO copyright treaty adopted on 20 December 1996, or similar
173 | laws prohibiting or restricting circumvention of such measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to the
178 | covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice; keep
188 | intact all notices stating that this License and any non-permissive terms
189 | added in accord with section 7 apply to the code; keep intact all notices
190 | of the absence of any warranty; and give all recipients a copy of this
191 | License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey, and
194 | you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the terms
200 | of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | “keep intact all notices”.
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program, in
226 | or on a volume of a storage or distribution medium, is called an
227 | “aggregate” if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work in
230 | an aggregate does not cause this License to apply to the other parts of
231 | the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms of
236 | sections 4 and 5, provided that you also convey the machine-readable
237 | Corresponding Source under the terms of this License, in one of these
238 | ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be included
283 | in conveying the object code work.
284 |
285 | A “User Product” is either (1) a “consumer product”, which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for
288 | incorporation into a dwelling. In determining whether a product is a
289 | consumer product, doubtful cases shall be resolved in favor of coverage.
290 | For a particular product received by a particular user, “normally used”
291 | refers to a typical or common use of that class of product, regardless of
292 | the status of the particular user or of the way in which the particular
293 | user actually uses, or expects or is expected to use, the product. A
294 | product is a consumer product regardless of whether the product has
295 | substantial commercial, industrial or non-consumer uses, unless such uses
296 | represent the only significant mode of use of the product.
297 |
298 | “Installation Information” for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied by
312 | the Installation Information. But this requirement does not apply if
313 | neither you nor any third party retains the ability to install modified
314 | object code on the User Product (for example, the work has been
315 | installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | “Additional permissions” are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by this
340 | License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option remove
343 | any additional permissions from that copy, or from any part of it.
344 | (Additional permissions may be written to require their own removal in
345 | certain cases when you modify the work.) You may place additional
346 | permissions on material, added by you to a covered work, for which you
347 | have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered “further
377 | restrictions” within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further restriction,
380 | you may remove that term. If a license document contains a further
381 | restriction but permits relicensing or conveying under this License, you
382 | may add to a covered work material governed by the terms of that license
383 | document, provided that the further restriction does not survive such
384 | relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you must
387 | place, in the relevant source files, a statement of the additional terms
388 | that apply to those files, or a notice indicating where to find the
389 | applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions; the above
393 | requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your license
404 | from a particular copyright holder is reinstated (a) provisionally,
405 | unless and until the copyright holder explicitly and finally terminates
406 | your license, and (b) permanently, if the copyright holder fails to
407 | notify you of the violation by some reasonable means prior to 60 days
408 | after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is reinstated
411 | permanently if the copyright holder notifies you of the violation by
412 | some reasonable means, this is the first time you have received notice
413 | of violation of this License (for any work) from that copyright holder,
414 | and you cure the violation prior to 30 days after your receipt of the
415 | notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or run a
426 | copy of the Program. Ancillary propagation of a covered work occurring
427 | solely as a consequence of using peer-to-peer transmission to receive a
428 | copy likewise does not require acceptance. However, nothing other than
429 | this License grants you permission to propagate or modify any covered
430 | work. These actions infringe copyright if you do not accept this
431 | License. Therefore, by modifying or propagating a covered work, you
432 | indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An “entity transaction” is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that transaction
445 | who receives a copy of the work also receives whatever licenses to the
446 | work the party's predecessor in interest had or could give under the
447 | previous paragraph, plus a right to possession of the Corresponding
448 | Source of the work from the predecessor in interest, if the predecessor
449 | has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may not
453 | impose a license fee, royalty, or other charge for exercise of rights
454 | granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that any
456 | patent claim is infringed by making, using, selling, offering for sale,
457 | or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A “contributor” is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The work
463 | thus licensed is called the contributor's “contributor version”.
464 |
465 | A contributor's “essential patent claims” are all patent claims owned or
466 | controlled by the contributor, whether already acquired or hereafter
467 | acquired, that would be infringed by some manner, permitted by this
468 | License, of making, using, or selling its contributor version, but do
469 | not include claims that would be infringed only as a consequence of
470 | further modification of the contributor version. For purposes of this
471 | definition, “control” includes the right to grant patent sublicenses in a
472 | manner consistent with the requirements of this License.
473 |
474 | Each contributor grants you a non-exclusive, worldwide, royalty-free
475 | patent license under the contributor's essential patent claims, to make,
476 | use, sell, offer for sale, import and otherwise run, modify and
477 | propagate the contents of its contributor version.
478 |
479 | In the following three paragraphs, a “patent license” is any express
480 | agreement or commitment, however denominated, not to enforce a patent
481 | (such as an express permission to practice a patent or covenant not to
482 | sue for patent infringement). To “grant” such a patent license to a
483 | party means to make such an agreement or commitment not to enforce a
484 | patent against the party.
485 |
486 | If you convey a covered work, knowingly relying on a patent license,
487 | and the Corresponding Source of the work is not available for anyone to
488 | copy, free of charge and under the terms of this License, through a
489 | publicly available network server or other readily accessible means,
490 | then you must either (1) cause the Corresponding Source to be so
491 | available, or (2) arrange to deprive yourself of the benefit of the
492 | patent license for this particular work, or (3) arrange, in a manner
493 | consistent with the requirements of this License, to extend the patent
494 | license to downstream recipients. “Knowingly relying” means you have
495 | actual knowledge that, but for the patent license, your conveying the
496 | covered work in a country, or your recipient's use of the covered work
497 | in a country, would infringe one or more identifiable patents in that
498 | country that you have reason to believe are valid.
499 |
500 | If, pursuant to or in connection with a single transaction or
501 | arrangement, you convey, or propagate by procuring conveyance of, a
502 | covered work, and grant a patent license to some of the parties
503 | receiving the covered work authorizing them to use, propagate, modify or
504 | convey a specific copy of the covered work, then the patent license you
505 | grant is automatically extended to all recipients of the covered work
506 | and works based on it.
507 |
508 | A patent license is “discriminatory” if it does not include within the
509 | scope of its coverage, prohibits the exercise of, or is conditioned on
510 | the non-exercise of one or more of the rights that are specifically
511 | granted under this License. You may not convey a covered work if you are
512 | a party to an arrangement with a third party that is in the business of
513 | distributing software, under which you make payment to the third party
514 | based on the extent of your activity of conveying the work, and under
515 | which the third party grants, to any of the parties who would receive
516 | the covered work from you, a discriminatory patent license (a) in
517 | connection with copies of the covered work conveyed by you (or copies
518 | made from those copies), or (b) primarily for and in connection with
519 | specific products or compilations that contain the covered work, unless
520 | you entered into that arrangement, or that patent license was granted,
521 | prior to 28 March 2007.
522 |
523 | Nothing in this License shall be construed as excluding or limiting any
524 | implied license or other defenses to infringement that may otherwise be
525 | available to you under applicable patent law.
526 |
527 | 12. No Surrender of Others' Freedom.
528 |
529 | If conditions are imposed on you (whether by court order, agreement or
530 | otherwise) that contradict the conditions of this License, they do not
531 | excuse you from the conditions of this License. If you cannot convey a
532 | covered work so as to satisfy simultaneously your obligations under this
533 | License and any other pertinent obligations, then as a consequence you
534 | may not convey it at all. For example, if you agree to terms that obligate
535 | you to collect a royalty for further conveying from those to whom you
536 | convey the Program, the only way you could satisfy both those terms and
537 | this License would be to refrain entirely from conveying the Program.
538 |
539 | 13. Remote Network Interaction; Use with the GNU General Public License.
540 |
541 | Notwithstanding any other provision of this License, if you modify the
542 | Program, your modified version must prominently offer all users
543 | interacting with it remotely through a computer network (if your version
544 | supports such interaction) an opportunity to receive the Corresponding
545 | Source of your version by providing access to the Corresponding Source
546 | from a network server at no charge, through some standard or customary
547 | means of facilitating copying of software. This Corresponding Source
548 | shall include the Corresponding Source for any work covered by version 3
549 | of the GNU General Public License that is incorporated pursuant to the
550 | following paragraph.
551 |
552 | Notwithstanding any other provision of this License, you have permission
553 | to link or combine any covered work with a work licensed under version 3
554 | of the GNU General Public License into a single combined work, and to
555 | convey the resulting work. The terms of this License will continue to
556 | apply to the part which is the covered work, but the work with which it
557 | is combined will remain governed by version 3 of the GNU General Public
558 | License.
559 |
560 | 14. Revised Versions of this License.
561 |
562 | The Free Software Foundation may publish revised and/or new versions of
563 | the GNU Affero General Public License from time to time. Such new
564 | versions will be similar in spirit to the present version, but may
565 | differ in detail to address new problems or concerns.
566 |
567 | Each version is given a distinguishing version number. If the Program
568 | specifies that a certain numbered version of the GNU Affero General
569 | Public License “or any later version” applies to it, you have the
570 | option of following the terms and conditions either of that numbered
571 | version or of any later version published by the Free Software
572 | Foundation. If the Program does not specify a version number of the GNU
573 | Affero General Public License, you may choose any version ever published
574 | by the Free Software Foundation.
575 |
576 | If the Program specifies that a proxy can decide which future versions
577 | of the GNU Affero General Public License can be used, that proxy's
578 | public statement of acceptance of a version permanently authorizes you
579 | to choose that version for the Program.
580 |
581 | Later license versions may give you additional or different permissions.
582 | However, no additional obligations are imposed on any author or
583 | copyright holder as a result of your choosing to follow a later version.
584 |
585 | 15. Disclaimer of Warranty.
586 |
587 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
588 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
589 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY
590 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
591 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
592 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
593 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
594 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
595 |
596 | 16. Limitation of Liability.
597 |
598 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
599 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
600 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
601 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
602 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
603 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
604 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
605 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
606 | SUCH DAMAGES.
607 |
608 | 17. Interpretation of Sections 15 and 16.
609 |
610 | If the disclaimer of warranty and limitation of liability provided above
611 | cannot be given local legal effect according to their terms, reviewing
612 | courts shall apply local law that most closely approximates an absolute
613 | waiver of all civil liability in connection with the Program, unless a
614 | warranty or assumption of liability accompanies a copy of the Program in
615 | return for a fee.
616 |
617 | END OF TERMS AND CONDITIONS
618 |
619 | How to Apply These Terms to Your New Programs
620 |
621 | If you develop a new program, and you want it to be of the greatest
622 | possible use to the public, the best way to achieve this is to make it
623 | free software which everyone can redistribute and change under these terms.
624 |
625 | To do so, attach the following notices to the program. It is safest to
626 | attach them to the start of each source file to most effectively state
627 | the exclusion of warranty; and each file should have at least the
628 | “copyright” line and a pointer to where the full notice is found.
629 |
630 |
631 | Copyright (C)
632 |
633 | This program is free software: you can redistribute it and/or modify
634 | it under the terms of the GNU Affero General Public License as
635 | published by the Free Software Foundation, either version 3 of the
636 | License, or (at your option) any later version.
637 |
638 | This program is distributed in the hope that it will be useful,
639 | but WITHOUT ANY WARRANTY; without even the implied warranty of
640 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
641 | GNU Affero General Public License for more details.
642 |
643 | You should have received a copy of the GNU Affero General Public License
644 | along with this program. If not, see .
645 |
646 | Also add information on how to contact you by electronic and paper mail.
647 |
648 | If your software can interact with users remotely through a computer
649 | network, you should also make sure that it provides a way for users to
650 | get its source. For example, if your program is a web application, its
651 | interface could display a “Source” link that leads users to an archive
652 | of the code. There are many ways you could offer source, and different
653 | solutions will be better for different programs; see section 13 for the
654 | specific requirements.
655 |
656 | You should also get your employer (if you work as a programmer) or
657 | school, if any, to sign a “copyright disclaimer” for the program, if
658 | necessary. For more information on this, and how to apply and follow the
659 | GNU AGPL, see .
--------------------------------------------------------------------------------
/zai.js:
--------------------------------------------------------------------------------
1 | // ============================================================================
2 | // Z.AI TRANSFORMER FOR CLAUDE CODE ROUTER (PRODUCTION)
3 | // ============================================================================
4 | //
5 | // PURPOSE: Claude Code Router Transformer for Z.ai's OpenAI-Compatible Endpoint
6 | // Solves Claude Code limitations and enables advanced features.
7 | //
8 | // FLOW: Claude Code → This Transformer → Z.AI OpenAI-Compatible Endpoint
9 | //
10 | // KEY FEATURES:
11 | //
12 | // 1. MAX OUTPUT TOKENS FIX (Primary Solution)
13 | // - Problem: Claude Code limits max_tokens to 32K/64K
14 | // - Solution: Transformer overrides to real model limits
15 | // • GLM 4.6: 128K (131,072 tokens)
16 | // • GLM 4.5: 96K (98,304 tokens)
17 | // • GLM 4.5-air: 96K (98,304 tokens)
18 | // • GLM 4.5v: 16K (16,384 tokens)
19 | //
20 | // 2. SAMPLING CONTROL (Guaranteed)
21 | // - Sets do_sample=true to ensure temperature and top_p always work
22 | // - Applies model-specific temperature and top_p values
23 | //
24 | // 3. REASONING CONTROL (Transformer-Managed)
25 | // - With default config (reasoning=true), transformer always controls reasoning
26 | // - Claude Code's native toggle (Tab key / alwaysThinkingEnabled) does NOT work
27 | // - To enable Claude Code control: Set all models to reasoning=false
28 | // - Translation: Transforms Claude Code reasoning → Z.AI thinking format
29 | //
30 | // 4. KEYWORD-BASED PROMPT ENHANCEMENT (Auto-Detection)
31 | // - Detects analytical keywords: analyze, calculate, count, explain, etc.
32 | // - Automatically adds reasoning instructions to user prompt
33 | // - REQUIRES: reasoning=true AND keywordDetection=true (both must be true)
34 | // - If either is false, keywords are ignored
35 | //
36 | // 5. ULTRATHINK MODE (User-Triggered)
37 | // - User types "ultrathink" anywhere in their message
38 | // - Enables enhanced reasoning with prompt optimization
39 | // - WORKS INDEPENDENTLY: Does NOT require reasoning or keywordDetection
40 | // - NOT AFFECTED by global overrides (works independently of settings)
41 | // - Highest precedence, always enabled when detected
42 | //
43 | // 6. GLOBAL CONFIGURATION OVERRIDES (Optional)
44 | // - Override settings across ALL models via options
45 | // - overrideMaxTokens: Override max_tokens globally
46 | // - overrideTemperature: Override temperature globally
47 | // - overrideTopP: Override top_p globally
48 | // - overrideReasoning: Override reasoning on/off globally
49 | // - overrideKeywordDetection: Override keyword detection globally
50 | // - customKeywords: Add or replace keyword list
51 | // - overrideKeywords: Use ONLY custom keywords (true) or add to defaults (false)
52 | //
53 | // 7. CUSTOM USER TAGS
54 | // - Tags: ,
55 | // - Direct control over reasoning without modifying configuration
56 | // - IMPORTANT HIERARCHY: has HIGHER priority than
57 | // • alone → reasoning disabled
58 | // • + → reasoning enabled (Effort overrides)
59 | // • alone → reasoning enabled
60 | //
61 | // 8. FORCE PERMANENT THINKING (Level 0 - Maximum Priority)
62 | // - Option: forcePermanentThinking (in transformer options)
63 | // - Forces reasoning=true + effort=high on EVERY user message
64 | // - Overrides ALL other settings (Ultrathink, User Tags, Global Overrides, Model Config, Default)
65 | // - User Tags like , , are completely ignored
66 | // - Nuclear option: Use only when you want thinking 100% of the time with no way to disable it
67 | //
68 | // HIERARCHY: Force Permanent Thinking (0) > Ultrathink (1) > Custom Tags (2) > Global Override (3) > Model Config (4) > Claude Code (5)
69 | // NOTE: With default config (reasoning=true for all models), Level 4 applies model defaults.
70 | // Level 5 (Claude Code's native toggle) only works when:
71 | // - No user conditions (Levels 0-3) are active AND
72 | // - Model has reasoning=false in configuration
73 | // KEYWORDS: Requires reasoning=true + keywordDetection=true + keywords detected
74 | //
75 | // PRODUCTION: No debug logging, optimal performance
76 | //
77 | // CCR TYPE DEFINITIONS:
78 | // Based on: https://github.com/musistudio/llms/blob/main/src/types/llm.ts
79 | // https://github.com/musistudio/llms/blob/main/src/types/transformer.ts
80 | //
81 | // REFERENCES:
82 | // - CCR Transformer: https://github.com/musistudio/claude-code-router
83 | // - Z.AI Thinking: https://docs.z.ai/guides/overview/concept-param#thinking
84 | // ============================================================================
85 |
86 | /**
87 | * Cache control settings for messages and content blocks
88 | * @typedef {Object} CacheControl
89 | * @property {string} type - Cache control type (e.g., "ephemeral")
90 | */
91 |
92 | /**
93 | * Image URL container
94 | * @typedef {Object} ImageUrl
95 | * @property {string} url - The actual image URL (can be data URL or http/https)
96 | */
97 |
98 | /**
99 | * Function call details
100 | * @typedef {Object} FunctionCallDetails
101 | * @property {string} name - Name of the function to call
102 | * @property {string} arguments - JSON string of function arguments
103 | */
104 |
105 | /**
106 | * Thinking/reasoning content block from model
107 | * @typedef {Object} ThinkingBlock
108 | * @property {string} content - The thinking/reasoning text
109 | * @property {string} [signature] - Optional signature for thinking verification
110 | */
111 |
112 | /**
113 | * Function parameters JSON Schema
114 | * @typedef {Object} FunctionParameters
115 | * @property {"object"} type - Always "object" for parameters root
116 | * @property {Object.} properties - Parameter definitions
117 | * @property {string[]} [required] - List of required parameter names
118 | * @property {boolean} [additionalProperties] - Allow additional properties
119 | * @property {string} [$schema] - JSON Schema version
120 | */
121 |
122 | /**
123 | * Function definition
124 | * @typedef {Object} FunctionDefinition
125 | * @property {string} name - Function name (must be unique)
126 | * @property {string} description - Description of what the function does
127 | * @property {FunctionParameters} parameters - JSON Schema for function parameters
128 | */
129 |
130 | /**
131 | * Reasoning configuration
132 | * @typedef {Object} ReasoningConfig
133 | * @property {ThinkLevel} [effort] - Reasoning effort level (OpenAI-style)
134 | * @property {number} [max_tokens] - Maximum tokens for reasoning (Anthropic-style)
135 | * @property {boolean} [enabled] - Whether reasoning is enabled
136 | */
137 |
138 | /**
139 | * Transformer configuration item (object form)
140 | * @typedef {Object} TransformerConfigItem
141 | * @property {string} name - Transformer name
142 | * @property {Object} [options] - Transformer options
143 | */
144 |
145 | /**
146 | * Transformer configuration
147 | * @typedef {Object} TransformerConfig
148 | * @property {string|string[]|TransformerConfigItem[]} use - Transformer name(s) or configuration(s)
149 | */
150 |
151 | /**
152 | * Global overrides configuration
153 | * @typedef {Object} GlobalOverrides
154 | * @property {number|null} maxTokens - Override max_tokens for all models (takes precedence over model config)
155 | * @property {number|null} temperature - Override temperature for all models
156 | * @property {number|null} topP - Override top_p for all models
157 | * @property {boolean|null} reasoning - Override reasoning on/off for all models
158 | * @property {boolean|null} keywordDetection - Override automatic prompt enhancement on/off for all models
159 | */
160 |
161 | /**
162 | * Text content block in a message
163 | * @typedef {Object} TextContent
164 | * @property {"text"} type - Content type identifier
165 | * @property {string} text - The actual text content
166 | * @property {CacheControl} [cache_control] - Optional cache control settings
167 | */
168 |
169 | /**
170 | * Image content block in a message
171 | * @typedef {Object} ImageContent
172 | * @property {"image_url"} type - Content type identifier for images
173 | * @property {ImageUrl} image_url - Image URL container
174 | * @property {string} media_type - MIME type of the image (e.g., "image/png", "image/jpeg")
175 | */
176 |
177 | /**
178 | * Union type for message content blocks
179 | * @typedef {TextContent | ImageContent} MessageContent
180 | */
181 |
182 | /**
183 | * Tool/function call representation
184 | * @typedef {Object} ToolCall
185 | * @property {string} id - Unique identifier for this tool call
186 | * @property {"function"} type - Always "function" for function calls
187 | * @property {FunctionCallDetails} function - Function call details
188 | */
189 |
190 | /**
191 | * Unified message format compatible with multiple LLM providers
192 | * @typedef {Object} UnifiedMessage
193 | * @property {"user"|"assistant"|"system"|"tool"} role - Message role in conversation
194 | * @property {string|null|MessageContent[]} content - Message content (string, null, or structured blocks)
195 | * @property {ToolCall[]} [tool_calls] - Tool/function calls made by assistant (OpenAI format - reserved for future compatibility)
196 | * @property {string} [tool_call_id] - ID of tool call this message is responding to for role="tool" (OpenAI format - reserved for future compatibility)
197 | * @property {CacheControl} [cache_control] - Cache control settings for this message
198 | * @property {ThinkingBlock} [thinking] - Reasoning/thinking content from model
199 | */
200 |
201 | /**
202 | * Tool/function definition for LLM
203 | * @typedef {Object} UnifiedTool
204 | * @property {"function"} type - Always "function" for function tools
205 | * @property {FunctionDefinition} function - Function definition
206 | */
207 |
208 | /**
209 | * Reasoning effort level (OpenAI o1-style)
210 | * @typedef {"low"|"medium"|"high"} ThinkLevel
211 | */
212 |
213 | /**
214 | * @typedef {Object} UnifiedChatRequest
215 | * @property {UnifiedMessage[]} messages - Array of conversation messages
216 | * @property {string} model - LLM model name
217 | * @property {number} [max_tokens] - Maximum tokens in response
218 | * @property {number} [temperature] - Temperature for generation (0.0 - 2.0)
219 | * @property {number} [top_p] - Top-P nucleus sampling (0.0 - 1.0)
220 | * @property {boolean} [stream] - Whether response should be streamed
221 | * @property {UnifiedTool[]} [tools] - Available tools for the model
222 | * @property {"auto"|"none"|"required"|string|UnifiedTool} [tool_choice] - Tool selection strategy
223 | * @property {ReasoningConfig} [reasoning] - Reasoning configuration
224 | * @property {ThinkingConfiguration} [thinking] - Thinking configuration (provider-specific)
225 | */
226 |
227 | /**
228 | * @typedef {Object} LLMProvider
229 | * @property {string} name - Provider name
230 | * @property {string} baseUrl - API base URL
231 | * @property {string} apiKey - API key
232 | * @property {string[]} models - Available models
233 | * @property {TransformerConfig} [transformer] - Transformer configuration
234 | */
235 |
236 | /**
237 | * @typedef {Object} TransformerContext
238 | * @property {*} [key] - Additional context for transformer
239 | */
240 |
241 | /**
242 | * Standard Fetch API Response (also available in Node.js 18+)
243 | * @typedef {Object} Response
244 | * @property {boolean} ok - Indicates if response was successful (status 200-299)
245 | * @property {number} status - HTTP status code
246 | * @property {string} statusText - HTTP status message
247 | * @property {Headers} headers - Response headers
248 | * @property {boolean} redirected - Indicates if response is result of redirect
249 | * @property {string} type - Response type (basic, cors, etc.)
250 | * @property {string} url - Response URL
251 | * @property {function(): Promise} arrayBuffer - Read body as ArrayBuffer
252 | * @property {function(): Promise} blob - Read body as Blob
253 | * @property {function(): Promise} formData - Read body as FormData
254 | * @property {function(): Promise} json - Read body as JSON
255 | * @property {function(): Promise} text - Read body as text
256 | * @property {ReadableStream} [body] - Body stream
257 | * @property {boolean} bodyUsed - Indicates if body has been read
258 | */
259 |
260 | /**
261 | * Model-specific configuration
262 | * @typedef {Object} ModelConfig
263 | * @property {number} maxTokens - Maximum output tokens
264 | * @property {number|null} contextWindow - Maximum input tokens (context)
265 | * @property {number|null} temperature - Randomness control (0.0-2.0)
266 | * @property {number|null} topP - Nucleus sampling (0.0-1.0)
267 | * @property {boolean} reasoning - Whether model supports native reasoning (model decides when to use it)
268 | * @property {boolean} keywordDetection - Enable automatic prompt enhancement when analytical keywords are detected
269 | * @property {string} provider - Model provider (Z.AI only)
270 | */
271 |
272 | /**
273 | * Request body to be modified by reasoning formatter
274 | * @typedef {Object} RequestBody
275 | * @property {*} [key] - Dynamic properties for the request body
276 | */
277 |
278 | /**
279 | * Function that applies provider-specific reasoning format
280 | * @typedef {function(RequestBody, string): void} ReasoningFormatter
281 | * @param {RequestBody} body - Request body to modify
282 | * @param {string} modelName - Model name
283 | */
284 |
285 | /**
286 | * Dictionary of model configurations indexed by model name
287 | * @typedef {Record} ModelConfigurationMap
288 | */
289 |
290 | /**
291 | * Dictionary of reasoning formatters indexed by provider
292 | * @typedef {Record} ReasoningFormatterMap
293 | */
294 |
295 | /**
296 | * Thinking/reasoning configuration for provider
297 | * @typedef {Object} ThinkingConfiguration
298 | * @property {string} type - Thinking type (e.g., "enabled")
299 | * @property {*} [key] - Additional provider-specific properties
300 | */
301 |
302 | /**
303 | * Delta content in streaming response
304 | * @typedef {Object} StreamDelta
305 | * @property {string} [role] - Message role
306 | * @property {string} [content] - Content chunk
307 | * @property {string} [reasoning_content] - Reasoning/thinking content chunk
308 | * @property {string} [finish_reason] - Reason for completion
309 | */
310 |
311 | /**
312 | * Choice in streaming response
313 | * @typedef {Object} StreamChoice
314 | * @property {StreamDelta} delta - Delta content
315 | * @property {number} index - Choice index
316 | */
317 |
318 | /**
319 | * Modified request body to send to provider
320 | * @typedef {Object} ModifiedRequestBody
321 | * @property {string} model - Model name
322 | * @property {number} max_tokens - Maximum tokens
323 | * @property {number} [temperature] - Temperature setting
324 | * @property {number} [top_p] - Top-P setting
325 | * @property {boolean} [do_sample] - Sampling control
326 | * @property {UnifiedMessage[]} messages - Messages array
327 | * @property {ThinkingConfiguration} [thinking] - Thinking configuration
328 | * @property {StreamChoice[]} [choices] - Choices in response (for streaming)
329 | * @property {*} [key] - Additional dynamic properties
330 | */
331 |
332 | /**
333 | * CCR Transformer interface (based on @musistudio/llms)
334 | *
335 | * @typedef {Object} CCRTransformer
336 | * @property {string} name - Unique transformer name (REQUIRED)
337 | * @property {function(UnifiedChatRequest, LLMProvider, TransformerContext): Promise