agentk8 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/core.sh CHANGED
@@ -8,7 +8,7 @@ set -euo pipefail
8
8
  # CONSTANTS
9
9
  # =============================================================================
10
10
 
11
- AGENTK_VERSION="1.0.4"
11
+ AGENTK_VERSION="1.0.5"
12
12
  AGENTK_ROOT="${AGENTK_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
13
13
  AGENTK_WORKSPACE="${AGENTK_ROOT}/workspace"
14
14
  CLAUDE_KNOWLEDGE_CUTOFF="2024-04"
package/lib/spawn.sh CHANGED
@@ -235,15 +235,86 @@ spawn_agent_interactive() {
235
235
 
236
236
  log_info "Starting interactive session with agent: $agent"
237
237
 
238
- # Run claude interactively
238
+ # Run claude with JSON output to capture tokens
239
+ local output_file
240
+ output_file=$(mktemp)
241
+
239
242
  if [[ -n "$initial_prompt" ]]; then
240
- claude --system-prompt "$prompt_file" "$initial_prompt"
243
+ # Run with --print to get non-interactive output with token info
244
+ if claude --print --output-format json --system-prompt "$prompt_file" "$initial_prompt" > "$output_file" 2>&1; then
245
+ # Parse and display response
246
+ _parse_and_display_response "$output_file"
247
+ else
248
+ # Fallback: show raw output on error
249
+ cat "$output_file"
250
+ fi
241
251
  else
252
+ # Interactive mode - can't easily capture tokens, run normally
242
253
  claude --system-prompt "$prompt_file"
243
254
  fi
244
255
 
245
256
  # Cleanup
246
- rm -f "$prompt_file"
257
+ rm -f "$prompt_file" "$output_file"
258
+ }
259
+
260
+ # Parse JSON response and extract tokens
261
+ _parse_and_display_response() {
262
+ local output_file="$1"
263
+
264
+ # Check if output is valid JSON
265
+ if jq -e . "$output_file" >/dev/null 2>&1; then
266
+ # Extract the response text - handle different JSON structures
267
+ local response
268
+ response=$(jq -r '
269
+ if .result then .result
270
+ elif .content then
271
+ if type == "array" then .[].text
272
+ else .content
273
+ end
274
+ elif .text then .text
275
+ else .
276
+ end
277
+ ' "$output_file" 2>/dev/null)
278
+
279
+ # Extract token counts - check various possible locations
280
+ local input_tokens output_tokens total_tokens
281
+ input_tokens=$(jq -r '
282
+ .usage.input_tokens //
283
+ .inputTokens //
284
+ .stats.input_tokens //
285
+ 0
286
+ ' "$output_file" 2>/dev/null)
287
+ output_tokens=$(jq -r '
288
+ .usage.output_tokens //
289
+ .outputTokens //
290
+ .stats.output_tokens //
291
+ 0
292
+ ' "$output_file" 2>/dev/null)
293
+
294
+ # Handle null values
295
+ [[ "$input_tokens" == "null" || -z "$input_tokens" ]] && input_tokens=0
296
+ [[ "$output_tokens" == "null" || -z "$output_tokens" ]] && output_tokens=0
297
+
298
+ total_tokens=$((input_tokens + output_tokens))
299
+
300
+ # Update session token count via file
301
+ if [[ $total_tokens -gt 0 ]] && [[ -n "${_TOKEN_FILE:-}" ]]; then
302
+ local current
303
+ current=$(cat "$_TOKEN_FILE" 2>/dev/null || echo "0")
304
+ echo "$((current + total_tokens))" > "$_TOKEN_FILE"
305
+ fi
306
+
307
+ # Display the response
308
+ echo "$response"
309
+
310
+ # Show token info in dim text
311
+ if [[ $total_tokens -gt 0 ]]; then
312
+ printf "\n${DIM}↑ %d tokens (in: %d, out: %d)${RESET}\n" "$total_tokens" "$input_tokens" "$output_tokens"
313
+ fi
314
+ else
315
+ # Not JSON, display as-is (probably an error or plain text)
316
+ cat "$output_file"
317
+ fi
247
318
  }
248
319
 
249
320
  # =============================================================================
package/lib/ui.sh CHANGED
@@ -518,14 +518,36 @@ print_info() {
518
518
  # SESSION STATS & TOKEN TRACKING
519
519
  # =============================================================================
520
520
 
521
+ # Token tracking file (persists across subshells)
522
+ _TOKEN_FILE=""
523
+
521
524
  init_session_stats() {
522
525
  _SESSION_START_TIME=$(date +%s)
523
526
  _SESSION_TOKENS=0
527
+ # Create token tracking file (export for subshells)
528
+ export _TOKEN_FILE="${AGENTK_WORKSPACE:-.}/.session_tokens"
529
+ echo "0" > "$_TOKEN_FILE"
524
530
  }
525
531
 
526
532
  add_tokens() {
527
533
  local tokens="${1:-0}"
528
- _SESSION_TOKENS=$((_SESSION_TOKENS + tokens))
534
+ if [[ -n "$_TOKEN_FILE" ]] && [[ -f "$_TOKEN_FILE" ]]; then
535
+ local current
536
+ current=$(cat "$_TOKEN_FILE" 2>/dev/null || echo "0")
537
+ local new_total=$((current + tokens))
538
+ echo "$new_total" > "$_TOKEN_FILE"
539
+ _SESSION_TOKENS=$new_total
540
+ else
541
+ _SESSION_TOKENS=$((_SESSION_TOKENS + tokens))
542
+ fi
543
+ }
544
+
545
+ get_session_tokens() {
546
+ if [[ -n "$_TOKEN_FILE" ]] && [[ -f "$_TOKEN_FILE" ]]; then
547
+ cat "$_TOKEN_FILE" 2>/dev/null || echo "0"
548
+ else
549
+ echo "${_SESSION_TOKENS:-0}"
550
+ fi
529
551
  }
530
552
 
531
553
  format_tokens() {
@@ -553,12 +575,17 @@ print_session_stats() {
553
575
  fi
554
576
  fi
555
577
 
578
+ local total_tokens
579
+ total_tokens=$(get_session_tokens)
556
580
  local tokens_display
557
- tokens_display=$(format_tokens "${_SESSION_TOKENS:-0}")
581
+ tokens_display=$(format_tokens "$total_tokens")
558
582
 
559
583
  echo
560
584
  print_divider "─"
561
585
  printf "${DIM}Session: %s │ Tokens: ↑ %s${RESET}\n" "$elapsed" "$tokens_display"
586
+
587
+ # Cleanup token file
588
+ [[ -n "$_TOKEN_FILE" ]] && rm -f "$_TOKEN_FILE" 2>/dev/null
562
589
  }
563
590
 
564
591
  print_status_line() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentk8",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Multi-Agent Claude Code Terminal Suite - Orchestrate multiple Claude agents for software development and ML research",
5
5
  "keywords": [
6
6
  "claude",