agentk8 1.0.0

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 ADDED
@@ -0,0 +1,281 @@
1
+ #!/usr/bin/env bash
2
+ # AGENT-K Core Library
3
+ # Shared functions for logging, JSON helpers, date context, and path utilities
4
+
5
+ set -euo pipefail
6
+
7
+ # =============================================================================
8
+ # CONSTANTS
9
+ # =============================================================================
10
+
11
+ AGENTK_VERSION="1.0.0"
12
+ AGENTK_ROOT="${AGENTK_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}"
13
+ AGENTK_WORKSPACE="${AGENTK_ROOT}/workspace"
14
+ CLAUDE_KNOWLEDGE_CUTOFF="2024-04"
15
+
16
+ # =============================================================================
17
+ # COLORS & FORMATTING
18
+ # =============================================================================
19
+
20
+ # Check if terminal supports colors
21
+ if [[ -t 1 ]] && command -v tput &>/dev/null && [[ $(tput colors 2>/dev/null || echo 0) -ge 8 ]]; then
22
+ RED=$(tput setaf 1)
23
+ GREEN=$(tput setaf 2)
24
+ YELLOW=$(tput setaf 3)
25
+ BLUE=$(tput setaf 4)
26
+ MAGENTA=$(tput setaf 5)
27
+ CYAN=$(tput setaf 6)
28
+ WHITE=$(tput setaf 7)
29
+ BOLD=$(tput bold)
30
+ DIM=$(tput dim)
31
+ RESET=$(tput sgr0)
32
+ else
33
+ RED="" GREEN="" YELLOW="" BLUE="" MAGENTA="" CYAN="" WHITE=""
34
+ BOLD="" DIM="" RESET=""
35
+ fi
36
+
37
+ # =============================================================================
38
+ # LOGGING
39
+ # =============================================================================
40
+
41
+ # Log levels
42
+ LOG_LEVEL="${LOG_LEVEL:-info}"
43
+ declare -A LOG_LEVELS=([debug]=0 [info]=1 [warn]=2 [error]=3)
44
+
45
+ _log() {
46
+ local level="$1"
47
+ local message="$2"
48
+ local timestamp
49
+ timestamp=$(date '+%Y-%m-%d %H:%M:%S')
50
+
51
+ # Check if we should log this level
52
+ local current_level=${LOG_LEVELS[${LOG_LEVEL}]:-1}
53
+ local msg_level=${LOG_LEVELS[${level}]:-1}
54
+
55
+ if [[ $msg_level -ge $current_level ]]; then
56
+ case "$level" in
57
+ debug) echo "${DIM}[$timestamp] [DEBUG] $message${RESET}" ;;
58
+ info) echo "${CYAN}[$timestamp] [INFO]${RESET} $message" ;;
59
+ warn) echo "${YELLOW}[$timestamp] [WARN]${RESET} $message" >&2 ;;
60
+ error) echo "${RED}[$timestamp] [ERROR]${RESET} $message" >&2 ;;
61
+ esac
62
+ fi
63
+
64
+ # Also log to file if workspace exists
65
+ if [[ -d "$AGENTK_WORKSPACE/logs" ]]; then
66
+ echo "[$timestamp] [$level] $message" >> "$AGENTK_WORKSPACE/logs/agentk.log"
67
+ fi
68
+ }
69
+
70
+ log_debug() { _log debug "$1"; }
71
+ log_info() { _log info "$1"; }
72
+ log_warn() { _log warn "$1"; }
73
+ log_error() { _log error "$1"; }
74
+
75
+ # =============================================================================
76
+ # DATE & RECENCY AWARENESS
77
+ # =============================================================================
78
+
79
+ get_today() {
80
+ date '+%Y-%m-%d'
81
+ }
82
+
83
+ get_knowledge_age_months() {
84
+ local cutoff_year=${CLAUDE_KNOWLEDGE_CUTOFF%%-*}
85
+ local cutoff_month=${CLAUDE_KNOWLEDGE_CUTOFF##*-}
86
+ local current_year=$(date '+%Y')
87
+ local current_month=$(date '+%m')
88
+
89
+ # Remove leading zeros for arithmetic
90
+ cutoff_month=$((10#$cutoff_month))
91
+ current_month=$((10#$current_month))
92
+
93
+ echo $(( (current_year - cutoff_year) * 12 + current_month - cutoff_month ))
94
+ }
95
+
96
+ get_date_context() {
97
+ local today
98
+ today=$(get_today)
99
+ local age_months
100
+ age_months=$(get_knowledge_age_months)
101
+
102
+ cat <<EOF
103
+ ================================================================================
104
+ DATE AWARENESS
105
+ ================================================================================
106
+ TODAY'S DATE: $today
107
+ CLAUDE KNOWLEDGE CUTOFF: $CLAUDE_KNOWLEDGE_CUTOFF (~$age_months months old)
108
+
109
+ IMPORTANT: Any information from your training data may be outdated. When suggesting:
110
+ - Libraries/frameworks → verify current version exists
111
+ - Best practices → check if still recommended
112
+ - APIs → confirm endpoints haven't changed
113
+ - Papers/research → look for newer publications
114
+
115
+ USE SCOUT to verify recency when uncertain.
116
+ ================================================================================
117
+ EOF
118
+ }
119
+
120
+ # =============================================================================
121
+ # JSON HELPERS (requires jq)
122
+ # =============================================================================
123
+
124
+ check_jq() {
125
+ if ! command -v jq &>/dev/null; then
126
+ log_error "jq is required but not installed. Install with: brew install jq"
127
+ exit 1
128
+ fi
129
+ }
130
+
131
+ json_get() {
132
+ local file="$1"
133
+ local key="$2"
134
+ jq -r "$key" "$file" 2>/dev/null || echo ""
135
+ }
136
+
137
+ json_set() {
138
+ local file="$1"
139
+ local key="$2"
140
+ local value="$3"
141
+ local tmp
142
+ tmp=$(mktemp)
143
+
144
+ if [[ -f "$file" ]]; then
145
+ jq "$key = $value" "$file" > "$tmp" && mv "$tmp" "$file"
146
+ else
147
+ echo "{}" | jq "$key = $value" > "$file"
148
+ fi
149
+ }
150
+
151
+ json_create() {
152
+ local file="$1"
153
+ shift
154
+ local json="{}"
155
+
156
+ while [[ $# -gt 0 ]]; do
157
+ local key="$1"
158
+ local value="$2"
159
+ json=$(echo "$json" | jq "$key = $value")
160
+ shift 2
161
+ done
162
+
163
+ echo "$json" > "$file"
164
+ }
165
+
166
+ # =============================================================================
167
+ # PATH UTILITIES
168
+ # =============================================================================
169
+
170
+ ensure_dir() {
171
+ local dir="$1"
172
+ if [[ ! -d "$dir" ]]; then
173
+ mkdir -p "$dir"
174
+ log_debug "Created directory: $dir"
175
+ fi
176
+ }
177
+
178
+ ensure_workspace() {
179
+ ensure_dir "$AGENTK_WORKSPACE"
180
+ ensure_dir "$AGENTK_WORKSPACE/tasks"
181
+ ensure_dir "$AGENTK_WORKSPACE/results"
182
+ ensure_dir "$AGENTK_WORKSPACE/logs"
183
+ ensure_dir "$AGENTK_WORKSPACE/experiments"
184
+ }
185
+
186
+ get_task_file() {
187
+ local task_id="$1"
188
+ echo "$AGENTK_WORKSPACE/tasks/${task_id}.json"
189
+ }
190
+
191
+ get_result_file() {
192
+ local task_id="$1"
193
+ echo "$AGENTK_WORKSPACE/results/${task_id}.json"
194
+ }
195
+
196
+ get_agent_log() {
197
+ local agent="$1"
198
+ echo "$AGENTK_WORKSPACE/logs/${agent}.log"
199
+ }
200
+
201
+ # =============================================================================
202
+ # ID GENERATION
203
+ # =============================================================================
204
+
205
+ generate_id() {
206
+ local prefix="${1:-task}"
207
+ echo "${prefix}_$(date '+%Y%m%d_%H%M%S')_$$"
208
+ }
209
+
210
+ generate_session_id() {
211
+ generate_id "session"
212
+ }
213
+
214
+ generate_task_id() {
215
+ generate_id "task"
216
+ }
217
+
218
+ # =============================================================================
219
+ # DEPENDENCY CHECKS
220
+ # =============================================================================
221
+
222
+ check_dependencies() {
223
+ local missing=()
224
+
225
+ # Required
226
+ command -v jq &>/dev/null || missing+=("jq")
227
+ command -v claude &>/dev/null || missing+=("claude")
228
+
229
+ # Check bash version
230
+ if [[ "${BASH_VERSION%%.*}" -lt 4 ]]; then
231
+ log_error "Bash 4.0+ required. Current: $BASH_VERSION"
232
+ exit 1
233
+ fi
234
+
235
+ if [[ ${#missing[@]} -gt 0 ]]; then
236
+ log_error "Missing dependencies: ${missing[*]}"
237
+ log_info "Install with: brew install ${missing[*]}"
238
+ exit 1
239
+ fi
240
+
241
+ log_debug "All dependencies satisfied"
242
+ }
243
+
244
+ check_optional_dependencies() {
245
+ local optional=()
246
+
247
+ command -v tmux &>/dev/null || optional+=("tmux (for --visual mode)")
248
+
249
+ if [[ ${#optional[@]} -gt 0 ]]; then
250
+ log_debug "Optional dependencies not installed: ${optional[*]}"
251
+ fi
252
+ }
253
+
254
+ # =============================================================================
255
+ # CLEANUP
256
+ # =============================================================================
257
+
258
+ cleanup_old_tasks() {
259
+ local days="${1:-7}"
260
+ local task_dir="$AGENTK_WORKSPACE/tasks"
261
+ local result_dir="$AGENTK_WORKSPACE/results"
262
+
263
+ if [[ -d "$task_dir" ]]; then
264
+ find "$task_dir" -name "*.json" -mtime "+$days" -delete 2>/dev/null || true
265
+ fi
266
+
267
+ if [[ -d "$result_dir" ]]; then
268
+ find "$result_dir" -name "*.json" -mtime "+$days" -delete 2>/dev/null || true
269
+ fi
270
+
271
+ log_debug "Cleaned up tasks older than $days days"
272
+ }
273
+
274
+ # =============================================================================
275
+ # EXPORTS
276
+ # =============================================================================
277
+
278
+ export AGENTK_VERSION
279
+ export AGENTK_ROOT
280
+ export AGENTK_WORKSPACE
281
+ export CLAUDE_KNOWLEDGE_CUTOFF