-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2readme2.sh
More file actions
executable file
·2076 lines (2042 loc) · 86.9 KB
/
script2readme2.sh
File metadata and controls
executable file
·2076 lines (2042 loc) · 86.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{\rtf1\ansi\ansicpg1252\cocoartf2822
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 HelveticaNeue;\f1\fnil\fcharset0 HelveticaNeue-Bold;\f2\fnil\fcharset0 .SFNS-Regular_wdth_opsz110000_GRAD_wght1F40000;
\f3\fnil\fcharset0 .SFNS-Regular_wdth_opsz168000_GRAD_wght1F40000;\f4\fnil\fcharset0 .AppleSystemUIFontMonospaced-Regular;}
{\colortbl;\red255\green255\blue255;\red16\green16\blue16;\red255\green255\blue255;}
{\*\expandedcolortbl;;\cssrgb\c7843\c7843\c7451;\cssrgb\c100000\c100000\c100000;}
{\*\listtable{\list\listtemplateid1\listhybrid{\listlevel\levelnfc23\levelnfcn23\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{none\}}{\leveltext\leveltemplateid1\'00;}{\levelnumbers;}\fi-360\li720\lin720 }{\listlevel\levelnfc23\levelnfcn23\leveljc0\leveljcn0\levelfollow0\levelstartat1\levelspace360\levelindent0{\*\levelmarker \{none\}}{\leveltext\leveltemplateid2\'00;}{\levelnumbers;}\fi-360\li1440\lin1440 }{\listname ;}\listid1}}
{\*\listoverridetable{\listoverride\listid1\listoverridecount0\ls1}}
\margl1440\margr1440\vieww17800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\partightenfactor0
\f0\fs24 \cf2 \cb3 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 \
\
\pard\pardeftab720\partightenfactor0
\f1\b \cf2 IT
\f2\b0\fs21 \AppleTypeServices\AppleTypeServicesF65539 \
Ian Trimble\
\pard\pardeftab720\qc\partightenfactor0
\f0\fs18 \AppleTypeServices \cf2 Pro plan\
\pard\pardeftab720\qc\partightenfactor0
\f2\fs21 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
{\field{\*\fldinst{HYPERLINK "https://claude.ai/project/019681f3-f1a8-75a0-9c4f-6a625e735306"}}{\fldrslt
\f0 \AppleTypeServices \cf2 Script2Readme\'a0/\
}}\pard\pardeftab720\qc\partightenfactor0
\f0 \AppleTypeServices \cf2 Untitled\
\
\pard\pardeftab720\qc\partightenfactor0
\f2 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
{\field{\*\fldinst{HYPERLINK "https://claude.ai/new"}}{\fldrslt
\fs24 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
}}\pard\pardeftab720\qc\partightenfactor0
\f3\fs45 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
\cf2 What can I help you with today?
\f0\fs24 \AppleTypeServices \
\pard\pardeftab720\partightenfactor0
\fs32 \cf2 \
\pard\pardeftab720\partightenfactor0
\fs24 \cf2 \
Analyze this script for errors and potential problems. Suggest fixes\
\pard\pardeftab720\qc\partightenfactor0
\fs32 \cf2 \
\
\
\
\pard\pardeftab720\partightenfactor0
\fs18 \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\fs28 \cf2 \
3.7 Sonnet\
\pard\pardeftab720\qc\partightenfactor0
\fs21 \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\f2\fs32 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
\f0\fs24 \AppleTypeServices \cf2 \
\pard\pardeftab720\partightenfactor0
\cf2 script2readme.sh
\fs20 1,916 lines
\fs32 \
\pard\pardeftab720\partightenfactor0
\f2\fs22 \AppleTypeServices\AppleTypeServicesF65539 \cf2 SH\
\pard\pardeftab720\qc\partightenfactor0
\f0\fs32 \AppleTypeServices \cf2 \
\pard\pardeftab720\partightenfactor0
\fs24 \cf2 \
\
\pard\pardeftab720\qc\partightenfactor0
\f2\fs21 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\
\pard\pardeftab720\qc\partightenfactor0
\f0 \AppleTypeServices \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\fs28 \cf2 \
\pard\pardeftab720\partightenfactor0
\f2\fs27 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\f0\fs32 \AppleTypeServices \cf2 \
\pard\pardeftab720\partightenfactor0
\fs28 \cf2 \
\
\pard\pardeftab720\partightenfactor0
\fs21 \cf2 \
\pard\pardeftab720\sa120\partightenfactor0
\f2 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
{\field{\*\fldinst{HYPERLINK "https://claude.ai/project/019681f3-f1a8-75a0-9c4f-6a625e735306"}}{\fldrslt
\f0\fs24 \AppleTypeServices \cf2 \
\
}}\pard\pardeftab720\partightenfactor0
\f0\fs24 \AppleTypeServices \cf2 \
\pard\tx940\tx1440\pardeftab720\li1440\fi-1440\partightenfactor0
\ls1\ilvl1
\f2\fs22 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \kerning1\expnd0\expndtw0 \outl0\strokewidth0 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 \uc0\u8232
\f0\fs32 \AppleTypeServices \uc0\u8232 \u8232 \u8232
\fs24 \uc0\u8232 \u8232
\f2\fs22 \AppleTypeServices\AppleTypeServicesF65539 \uc0\u8232
\f0\fs32 \AppleTypeServices \uc0\u8232 \u8232 \u8232
\fs24 \uc0\u8232 \u8232
\f2\fs22 \AppleTypeServices\AppleTypeServicesF65539 \uc0\u8232
\f0\fs32 \AppleTypeServices \uc0\u8232 \u8232 \u8232
\fs24 \uc0\u8232 \
\pard\pardeftab720\sa120\partightenfactor0
\f2\fs21 \AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\f0\fs24 \AppleTypeServices \cf2 \
\pard\pardeftab720\qc\partightenfactor0
\fs18 \cf2 \
\pard\pardeftab720\partightenfactor0
\fs24 \cf2 \
\pard\pardeftab720\partightenfactor0
\AppleTypeServices\AppleTypeServicesF65539 \cf2 \
\pard\pardeftab720\partightenfactor0
\f2\fs27 \AppleTypeServices\AppleTypeServicesF65539 \cf2 script2readme.sh
\f0\fs32 \AppleTypeServices \
\pard\pardeftab720\qc\partightenfactor0
\cf2 \
\pard\pardeftab720\partightenfactor0
\fs18 \cf2 76.67 KB\'a0\'951,916 lines\
\'95\
Formatting may be inconsistent from source\
\pard\pardeftab720\partightenfactor0
\f4 \cf2 #!/bin/zsh\
#\
# script2readme.sh - Generate README documentation from scripts using Ollama models\
# Author: Ian Trimble\
# Created: April 28, 2025\
# Version: 1.5.0\
#\
\
# Enable debug mode only when explicitly requested\
if [[ "$1" == "--debug" ]]; then\
set -x\
shift\
fi\
\
# =================== CONFIGURATION ===================\
# App information\
APP_NAME="Script to README Generator"\
APP_VERSION="1.5.0"\
APP_AUTHOR="Ian Trimble"\
\
# Directory structure\
BENCHMARK_DIR="$\{HOME\}/ollama_benchmarks"\
PROJECT_DIR="$(pwd)"\
SESSION_ID=$(date +%Y%m%d_%H%M%S)_$(openssl rand -hex 4)\
BENCHMARK_LOG="$\{BENCHMARK_DIR\}/benchmark_log.csv"\
METRICS_LOG="$\{BENCHMARK_DIR\}/metrics_$\{SESSION_ID\}.json"\
CHANGELOG="$\{PROJECT_DIR\}/CHANGELOG.md"\
README="$(pwd)/README.md"\
OLLAMA_API="http://localhost:11434/api/chat"\
\
# Colors and formatting (from enhanced_script.sh)\
RED='\\033[0;31m'\
GREEN='\\033[0;32m'\
YELLOW='\\033[0;33m'\
BLUE='\\033[0;34m'\
MAGENTA='\\033[0;35m'\
CYAN='\\033[0;36m'\
WHITE='\\033[1;37m'\
GRAY='\\033[0;37m'\
BOLD='\\033[1m'\
RESET='\\033[0m'\
\
# Check if terminal supports colors\
if [ ! -t 1 ]; then\
# Reset all color variables to empty strings if not in a terminal\
RED='' GREEN='' YELLOW='' BLUE='' MAGENTA='' CYAN='' WHITE='' GRAY=''\
BOLD='' RESET=''\
fi\
\
# Did you know tips\
declare -a TIPS\
TIPS=(\
"You can select different models for different scripts to compare documentation quality."\
"Larger models (13B+) generally produce more detailed documentation but take longer."\
"Your benchmarks are saved to $\{BENCHMARK_DIR\} for performance analysis."\
"The script automatically detects base64-encoded files and decodes them."\
"Script metrics like function count and line count help estimate processing time."\
"Good documentation can reduce project onboarding time by up to 60%."\
"The script supports multiple script types: shell, python, ruby, javascript, and more."\
"Historical performance data improves time estimates with each run."\
"The generated README includes detailed metadata about your script."\
"Future versions will support batch processing and interactive editing."\
)\
\
# Default model will be set dynamically based on available models\
DEFAULT_MODEL=""\
\
# Model complexity factors will be assigned dynamically\
declare -A MODEL_COMPLEXITY\
\
# Model complexity estimation based on parameter size\
# These are base factors - actual values will be calculated dynamically\
declare -A MODEL_SIZE_COMPLEXITY\
MODEL_SIZE_COMPLEXITY["1-3B"]=1.0 # Small models (1-3B parameters)\
MODEL_SIZE_COMPLEXITY["4-7B"]=2.5 # Medium models (4-7B parameters)\
MODEL_SIZE_COMPLEXITY["8-13B"]=4.0 # Large models (8-13B parameters)\
MODEL_SIZE_COMPLEXITY["14B+"]=6.0 # Extra large models (14B+ parameters)\
\
# Default for unknown models\
MODEL_COMPLEXITY["default"]=2.5\
\
# Create benchmark directory if it doesn't exist\
mkdir -p "$\{BENCHMARK_DIR\}"\
\
# Initialize benchmark file if it doesn't exist\
if [ ! -f "$\{BENCHMARK_LOG\}" ]; then\
echo "timestamp,session_id,script_name,script_size_bytes,script_lines,script_chars,model,operation,duration,tokens,cpu_usage,memory_usage" > "$\{BENCHMARK_LOG\}"\
fi\
\
# Create project directory if not exists (should be present, but just in case)\
mkdir -p "$\{PROJECT_DIR\}"\
\
# Initialize changelog if it doesn't exist\
if [ ! -f "$\{CHANGELOG\}" ]; then\
\{\
echo "# Script to README Generator Changelog"\
echo ""\
echo "## Version 1.4.3 - $(date '+%Y-%m-%d')"\
echo "- Enhanced prompt engineering for more comprehensive READMEs"\
echo "- Added colorized output for better terminal experience"\
echo "- Improved base64 file detection and decoding"\
echo "- Added \\"Did you know\\" tips system for user guidance"\
echo "- Enhanced README output with detailed script metadata table"\
echo "- Added model performance information section"\
echo "- Improved JSON request handling for special characters"\
echo "- Fixed API error handling and response parsing"\
\} > "$\{CHANGELOG\}"\
echo -e "$\{GREEN\}Created new changelog at $\{CHANGELOG\}$\{RESET\}"\
else\
# Check if version entry exists, add if not\
if ! grep -q "## Version $\{APP_VERSION\}" "$\{CHANGELOG\}"; then\
sed -i '' "1a\\\\\
\\\\\
## Version $\{APP_VERSION\} - $(date '+%Y-%m-%d')\\\\\
- Significantly enhanced system prompt for more comprehensive and better structured READMEs\\\\\
- Added specific section requirements with mandatory templates for consistent documentation\\\\\
- Implemented model-specific prompt optimizations for deepseek-coder and codellama models\\\\\
- Improved time estimation algorithm with model-specific timing adjustments\\\\\
- Extended minimum processing times to ensure high-quality output from all models\\\\\
- Enhanced markdown formatting instructions with specific guidelines for code blocks and lists\\\\\
- Added word count requirements for more thorough documentation\\\\\
- Improved error handling during API requests with better feedback\
" "$\{CHANGELOG\}"\
echo -e "$\{GREEN\}Updated changelog at $\{CHANGELOG\}$\{RESET\}"\
fi\
fi\
\
# Initialize metrics JSON log\
echo "\{\\"session_id\\": \\"$\{SESSION_ID\}\\", \\"timestamp\\": \\"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\\", \\"app_version\\": \\"$\{APP_VERSION\}\\", \\"metrics\\": []\}" > "$\{METRICS_LOG\}"\
\
# =================== HELPER FUNCTIONS ===================\
# Function to display progress\
display_progress() \{\
local progress=$1\
local duration=$2\
local width=50\
local filled=$((width * progress / 100))\
local empty=$((width - filled))\
\
# Create the progress bar\
printf "\\r["\
printf "%$\{filled\}s" '' | tr ' ' '='\
printf ">"\
printf "%$\{empty\}s" '' | tr ' ' ' '\
printf "] %3d%% (%s)" $progress "$duration"\
\}\
\
# Function to estimate completion time\
estimate_completion_time() \{\
local script_size=$1\
local model=$2\
local line_count=$3\
\
# Get model complexity factor\
local complexity=$\{MODEL_COMPLEXITY[$model]\}\
if [ -z "$complexity" ]; then\
complexity=$\{MODEL_COMPLEXITY["default"]\}\
fi\
\
# Base time in seconds per KB for a standard model\
local base_time=2\
\
# Check if we have historical data for this model\
local historical_data=""\
local historical_factor=1.0\
if [ -f "$\{BENCHMARK_DIR\}/model_performance.csv" ]; then\
# Format: model,avg_time_per_kb,samples\
historical_data=$(grep "^$\{model\}," "$\{BENCHMARK_DIR\}/model_performance.csv" 2>/dev/null)\
if [ -n "$historical_data" ]; then\
local avg_time=$(echo "$historical_data" | cut -d, -f2)\
\
# Validate avg_time is a valid number\
if [[ "$avg_time" =~ ^[0-9]+(\\.[0-9]+)?$ ]]; then\
# Calculate historical factor but ensure division by zero is avoided\
if (( $(echo "$base_time > 0" | bc -l 2>/dev/null) )) && (( $(echo "$complexity > 0" | bc -l 2>/dev/null) )); then\
historical_factor=$(echo "scale=2; $avg_time / ($base_time * $complexity)" | bc 2>/dev/null)\
\
# Validate result is a reasonable number\
if [ -z "$historical_factor" ] || [[ ! "$historical_factor" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || \
(( $(echo "$historical_factor <= 0" | bc -l 2>/dev/null) )) || \
(( $(echo "$historical_factor > 100" | bc -l 2>/dev/null) )); then\
echo "Warning: Invalid historical factor calculated ($historical_factor). Using default."\
historical_factor=1.0\
else\
echo "Using historical performance data for $model (factor: $historical_factor)" >> "$\{BENCHMARK_DIR\}/estimation_log.txt"\
fi\
else\
echo "Warning: Invalid base_time or complexity for historical factor calculation. Using default."\
historical_factor=1.0\
fi\
else\
echo "Warning: Invalid average time in historical data ($avg_time). Using default."\
historical_factor=1.0\
fi\
else\
echo "No historical data found for $model. Using default factor." >> "$\{BENCHMARK_DIR\}/estimation_log.txt"\
fi\
else\
echo "No model performance database found. Using default factor." >> "$\{BENCHMARK_DIR\}/estimation_log.txt"\
fi\
\
# Adjust based on script size and line count\
# Larger scripts may take disproportionately longer\
# Scripts with more lines may be more complex\
local size_factor\
local line_factor\
\
# Calculate size factor (with error handling)\
size_factor=$(printf "%.2f" $(echo "scale=2; ($script_size / 1024) ^ 0.6" | bc 2>/dev/null))\
\
# Make sure size_factor is a valid number and at least 1.0\
if [ -z "$size_factor" ] || ! [[ "$size_factor" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || (( $(echo "$size_factor < 1.0" | bc -l 2>/dev/null) )); then\
echo "Resetting invalid size factor ($size_factor) to 1.0"\
size_factor=1.0\
fi\
\
# Calculate line complexity factor (with error handling)\
if [ $line_count -gt 0 ]; then\
# More lines per KB indicates more complex code\
local lines_per_kb=$(echo "scale=2; $line_count / ($script_size / 1024)" | bc 2>/dev/null)\
if [ -n "$lines_per_kb" ] && (( $(echo "$lines_per_kb > 0" | bc 2>/dev/null) )); then\
# Normalize to a factor between 0.8 and 1.5\
line_factor=$(echo "scale=2; 0.8 + (($lines_per_kb / 40) * 0.7)" | bc 2>/dev/null)\
# Cap at reasonable bounds\
if (( $(echo "$line_factor > 1.5" | bc 2>/dev/null) )); then\
line_factor=1.5\
elif (( $(echo "$line_factor < 0.8" | bc 2>/dev/null) )); then\
line_factor=0.8\
fi\
else\
line_factor=1.0\
fi\
else\
line_factor=1.0\
fi\
\
# Calculate estimated seconds with error handling\
local estimate\
\
# Make sure all factors are valid for calculation\
if [ -z "$base_time" ] || [ -z "$size_factor" ] || [ -z "$complexity" ] || [ -z "$line_factor" ] || [ -z "$historical_factor" ]; then\
echo "Warning: Missing factors for time calculation. Using default estimate."\
estimate=30\
else\
# Log factors for debugging\
echo "Calculation factors: base_time=$base_time, size_factor=$size_factor, complexity=$complexity, line_factor=$line_factor, historical_factor=$historical_factor"\
\
# Force factors to reasonable values if needed\
[ -z "$base_time" ] && base_time=2\
[ "$size_factor" = "0" ] || [ -z "$size_factor" ] && size_factor=1.0\
[ "$complexity" = "0" ] || [ -z "$complexity" ] && complexity=2.5\
[ "$line_factor" = "0" ] || [ -z "$line_factor" ] && line_factor=1.0\
[ "$historical_factor" = "0" ] || [ -z "$historical_factor" ] && historical_factor=1.0\
\
# Calculate the estimate with non-zero factors\
estimate=$(printf "%.0f" $(echo "scale=0; $base_time * $size_factor * $complexity * $line_factor * $historical_factor" | bc 2>/dev/null))\
\
# Validate the estimate is a number and at least 10\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]]; then\
echo "Warning: Estimate calculation failed. Using default estimate."\
estimate=30\
elif [ "$estimate" -eq 0 ]; then\
echo "Warning: Estimate calculated as 0. Using default estimate."\
estimate=30\
fi\
fi\
\
# Log estimation factors for analysis\
echo "$(date '+%Y-%m-%d %H:%M:%S') - Model: $model, Size: $\{script_size\}B, Lines: $line_count, Complexity: $complexity, SizeFactor: $size_factor, LineFactor: $line_factor, HistoricalFactor: $historical_factor, Estimate: $\{estimate\}s" >> "$\{BENCHMARK_DIR\}/estimation_log.txt"\
\
# Ensure minimum reasonable time\
if [ $estimate -lt 10 ] || [ -z "$estimate" ]; then\
estimate=10\
fi\
\
# Apply model-specific time adjustments based on benchmarks and known patterns\
# Set minimum times for specific models based on previous benchmark data\
if [[ "$model" == "deepseek-coder:latest" ]]; then\
# From benchmarks: deepseek-coder:latest can take a while for large files\
local min_time=180\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]] || [ "$estimate" -lt $min_time ]; then\
echo "Adjusting estimate for $\{model\} based on historical data (from $\{estimate\}s to $\{min_time\}s)"\
echo "Note: deepseek-coder:latest needs extra time for enhanced documentation quality"\
estimate=$min_time\
fi\
elif [[ "$model" == "qwen2.5-coder:7b" ]]; then\
# From benchmarks: qwen2.5-coder:7b consistently takes ~55s for large files\
local min_time=80\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]] || [ "$estimate" -lt $min_time ]; then\
echo "Adjusting estimate for $\{model\} based on historical data (from $\{estimate\}s to $\{min_time\}s)"\
estimate=$min_time\
fi\
elif [[ "$model" == *"codellama"* ]]; then\
# Codellama models need extra time for enhanced documentation quality\
local min_time=150\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]] || [ "$estimate" -lt $min_time ]; then\
echo "Adjusting estimate for $\{model\} based on enhanced documentation requirements (from $\{estimate\}s to $\{min_time\}s)"\
echo "Note: Codellama models need extra time to generate high-quality documentation"\
estimate=$min_time\
fi\
elif [[ "$model" == *"llama:70b"* || "$model" == *"deepseek-coder"* || "$model" == *"mixtral:8x7b"* \
|| "$model" == *"grok"* || "$model" == *"phi"* ]]; then\
# For other large models, use generic large model minimum - be generous!\
local min_time=120\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]] || [ "$estimate" -lt $min_time ]; then\
if ! [[ "$estimate" =~ ^[0-9]+$ ]]; then\
echo "Warning: Invalid estimate. Using default for $\{model\}."\
estimate=$min_time\
else\
echo "Adjusting estimate upwards for large model $\{model\} (from $\{estimate\}s to $\{min_time\}s)"\
estimate=$min_time\
fi\
fi\
else\
# For any other model, ensure a minimum of 90 seconds to account for enhanced documentation\
local min_time=90\
if [ -z "$estimate" ] || ! [[ "$estimate" =~ ^[0-9]+$ ]] || [ "$estimate" -lt $min_time ]; then\
echo "Ensuring minimum estimate of $\{min_time\}s for $\{model\} with enhanced documentation requirements"\
estimate=$min_time\
fi\
fi\
\
# Scale estimate based on file size for large files (>10KB)\
if [ "$script_size" -gt 10240 ] && [ "$estimate" -gt 0 ]; then\
local size_scale=$(echo "scale=2; ($script_size / 10240) ^ 0.6" | bc 2>/dev/null)\
if [[ "$size_scale" =~ ^[0-9]+(\\.[0-9]+)?$ ]] && (( $(echo "$size_scale > 1.0" | bc -l 2>/dev/null) )); then\
local original_estimate=$estimate\
estimate=$(printf "%.0f" $(echo "scale=0; $estimate * $size_scale" | bc 2>/dev/null))\
echo "Scaling estimate by $\{size_scale\}x for large file of $\{script_size\} bytes ($\{original_estimate\}s \uc0\u8594 $\{estimate\}s)"\
fi\
fi\
\
echo $estimate\
\}\
\
# Function to format time in human-readable format\
format_time() \{\
local seconds=$1\
\
# Ensure seconds is an integer\
if [ -z "$seconds" ] || ! [[ "$seconds" =~ ^[0-9]+$ ]]; then\
echo "Warning: Invalid time value to format: '$seconds'. Using default." >&2\
seconds=0\
fi\
\
local minutes=$((seconds / 60))\
local remaining_seconds=$((seconds % 60))\
\
if [ $minutes -gt 0 ]; then\
echo "$\{minutes\}m $\{remaining_seconds\}s"\
else\
echo "$\{seconds\}s"\
fi\
\}\
\
# Function to show a random tip\
show_tip() \{\
local tip_index=$((RANDOM % $\{#TIPS[@]\}))\
local tip="$\{TIPS[$tip_index]\}"\
echo -e "\\n$\{YELLOW\}\uc0\u55357 \u56481 $\{BOLD\}Did you know?$\{RESET\} $\{tip\}$\{RESET\}\\n"\
\}\
\
# Function to display usage information\
show_usage() \{\
echo -e "$\{CYAN\}$\{BOLD\}$\{APP_NAME\}$\{RESET\} $\{WHITE\}(v$\{APP_VERSION\})$\{RESET\}"\
echo -e "$\{GRAY\}Generates README documentation from script files using Ollama models$\{RESET\}"\
echo ""\
echo -e "$\{YELLOW\}$\{BOLD\}Usage:$\{RESET\} ./script2readme.sh $\{GREEN\}[OPTIONS]$\{RESET\} $\{MAGENTA\}<input_file>$\{RESET\} $\{BLUE\}[model]$\{RESET\}"\
echo ""\
echo -e "$\{YELLOW\}$\{BOLD\}Options:$\{RESET\}"\
echo -e " $\{GREEN\}--debug$\{RESET\} Enable debug mode"\
echo -e " $\{GREEN\}--help$\{RESET\} Show this help message"\
echo -e " $\{GREEN\}--list-models$\{RESET\} List available Ollama models"\
echo -e " $\{GREEN\}--version$\{RESET\} Show version information"\
echo -e " $\{GREEN\}--no-estimate$\{RESET\} Skip time estimation"\
echo ""\
echo -e "$\{YELLOW\}$\{BOLD\}Arguments:$\{RESET\}"\
echo -e " $\{MAGENTA\}<input_file>$\{RESET\} Path to script file to document"\
echo -e " $\{BLUE\}[model]$\{RESET\} Optional Ollama model name (default: $\{WHITE\}$\{DEFAULT_MODEL\}$\{RESET\})"\
echo ""\
echo -e "$\{YELLOW\}$\{BOLD\}Examples:$\{RESET\}"\
echo -e " $\{GRAY\}./script2readme.sh my_script.sh$\{RESET\}"\
echo -e " $\{GRAY\}./script2readme.sh my_script.sh codellama:7b$\{RESET\}"\
echo ""\
echo -e "$\{YELLOW\}$\{BOLD\}Output:$\{RESET\}"\
echo -e " - Creates a new README file (format: README_script_model.md)"\
echo -e " - Logs performance metrics and benchmarks to $\{BENCHMARK_DIR\}"\
echo ""\
show_tip\
exit 0\
\}\
\
# Function to display version information\
show_version() \{\
echo -e "$\{CYAN\}$\{BOLD\}$\{APP_NAME\}$\{RESET\} $\{WHITE\}v$\{APP_VERSION\}$\{RESET\}"\
echo -e "$\{GRAY\}Author: $\{WHITE\}$\{APP_AUTHOR\}$\{RESET\}"\
echo -e "$\{GRAY\}Created: April 28, 2025$\{RESET\}"\
echo -e "$\{GRAY\}License: MIT$\{RESET\}"\
show_tip\
exit 0\
\}\
\
# Function to get system information\
get_system_info() \{\
local cpu_info=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo "Unknown")\
local memory_info=$(sysctl -n hw.memsize 2>/dev/null | awk '\{print int($1/1024/1024/1024) " GB"\}' || echo "Unknown")\
local os_info=$(sw_vers -productVersion 2>/dev/null || echo "Unknown")\
local ollama_version=$(ollama --version 2>/dev/null || echo "Unknown")\
\
# Add system info to metrics log\
jq --arg cpu "$\{cpu_info\}" \\\
--arg mem "$\{memory_info\}" \\\
--arg os "$\{os_info\}" \\\
--arg ollama "$\{ollama_version\}" \\\
'.system_info = \{"cpu": $cpu, "memory": $mem, "os": $os, "ollama_version": $ollama\}' "$\{METRICS_LOG\}" > "$\{METRICS_LOG\}.tmp" 2>/dev/null\
\
# Check if jq succeeded\
if [ $? -eq 0 ]; then\
mv "$\{METRICS_LOG\}.tmp" "$\{METRICS_LOG\}"\
else\
echo "Warning: Failed to update metrics log with system information"\
fi\
\
echo -e "$\{BLUE\}System Info:$\{RESET\} CPU: $\{WHITE\}$\{cpu_info\}$\{RESET\}, Memory: $\{WHITE\}$\{memory_info\}$\{RESET\}, OS: $\{WHITE\}$\{os_info\}$\{RESET\}, Ollama: $\{WHITE\}$\{ollama_version\}$\{RESET\}"\
\}\
\
# Function to get current resource usage\
get_resource_usage() \{\
local cpu_usage=$(ps -o %cpu= -p $$ | awk '\{print $1\}')\
local memory_usage=$(ps -o rss= -p $$ | awk '\{print int($1/1024) " MB"\}')\
\
echo "$\{cpu_usage\},$\{memory_usage\}"\
\}\
\
# Function to log benchmark data\
log_benchmark() \{\
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")\
local script_name=$1\
local script_size=$2\
local script_lines=$3\
local script_chars=$4\
local model=$5\
local operation=$6\
local duration=$7\
local token_count=$8\
\
# Get resource usage\
local resource_usage=$(get_resource_usage)\
local cpu_usage=$(echo $\{resource_usage\} | cut -d, -f1)\
local memory_usage=$(echo $\{resource_usage\} | cut -d, -f2)\
\
# Log to CSV for detailed data\
echo "$\{timestamp\},$\{SESSION_ID\},$\{script_name\},$\{script_size\},$\{script_lines\},$\{script_chars\},$\{model\},$\{operation\},$\{duration\},$\{token_count\},$\{cpu_usage\},$\{memory_usage\}" >> "$\{BENCHMARK_LOG\}"\
\
# Add metric to JSON log\
jq --arg timestamp "$\{timestamp\}" \\\
--arg script "$\{script_name\}" \\\
--arg size "$\{script_size\}" \\\
--arg lines "$\{script_lines\}" \\\
--arg chars "$\{script_chars\}" \\\
--arg model "$\{model\}" \\\
--arg op "$\{operation\}" \\\
--arg dur "$\{duration\}" \\\
--arg tokens "$\{token_count\}" \\\
--arg cpu "$\{cpu_usage\}" \\\
--arg mem "$\{memory_usage\}" \\\
'.metrics += [\{"timestamp": $timestamp, "script": $script, "size_bytes": $size, "line_count": $lines, "char_count": $chars, "model": $model, "operation": $op, "duration": $dur, "token_count": $tokens, "cpu_usage": $cpu, "memory_usage": $mem\}]' "$\{METRICS_LOG\}" > "$\{METRICS_LOG\}.tmp" 2>/dev/null\
\
# Check if jq succeeded\
if [ $? -eq 0 ]; then\
mv "$\{METRICS_LOG\}.tmp" "$\{METRICS_LOG\}"\
else\
echo "Warning: Failed to update metrics log with benchmark data"\
fi\
\
# Return for chaining\
echo "$\{operation\}:$\{duration\}"\
\}\
\
# Function to generate a benchmark summary\
generate_benchmark_summary() \{\
local model=$1\
local script_name=$2\
local total_time=$3\
local api_time=$4\
local parse_time=$5\
local script_size=$6\
local script_lines=$7\
local script_chars=$8\
local token_count=$9\
local estimated_time=$\{10\}\
\
echo -e "$\{BLUE\}\uc0\u9556 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9559 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{CYAN\}$\{BOLD\}README GENERATION COMPLETE $\{RESET\}$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9568 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9571 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56516 Script:$\{RESET\} $\{YELLOW\}$\{script_name\}$\{RESET\}$(printf "%$((40-$\{#script_name\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55358 \u56598 Model:$\{RESET\} $\{MAGENTA\}$\{model\}$\{RESET\}$(printf "%$((41-$\{#model\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u9201 \u65039 Total time:$\{RESET\} $\{GREEN\}$\{total_time\}s$\{RESET\}$(printf "%$((37-$\{#total_time\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
\
if [ -n "$estimated_time" ]; then\
local accuracy\
accuracy=$(printf "%.1f" $(echo "scale=1; $estimated_time / $total_time * 100" | bc 2>/dev/null)) || accuracy="N/A"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56622 Est. vs Actual:$\{RESET\} $\{estimated_time\}s vs $\{total_time\}s ($\{accuracy\}%)$(printf "%$((21-$\{#estimated_time\}-$\{#total_time\}-$\{#accuracy\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
fi\
\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56580 API request time:$\{RESET\} $\{CYAN\}$\{api_time\}s$\{RESET\}$(printf "%$((31-$\{#api_time\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56589 Response parse time:$\{RESET\} $\{CYAN\}$\{parse_time\}s$\{RESET\}$(printf "%$((29-$\{#parse_time\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56541 Response size:$\{RESET\} ~$\{token_count\} words$(printf "%$((33-$\{#token_count\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{WHITE\}\u55357 \u56514 Script metrics:$\{RESET\}$(printf "%$((35))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
\
local size_kb\
size_kb=$(printf "%.2f" $(echo "scale=2; $\{script_size\}/1024" | bc 2>/dev/null)) || size_kb="N/A"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}- Size: $\{script_size\} bytes ($\{size_kb\} KB)$\{RESET\}$(printf "%$((38-$\{#script_size\}-$\{#size_kb\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}- Lines: $\{script_lines\}$\{RESET\}$(printf "%$((43-$\{#script_lines\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}- Characters: $\{script_chars\}$\{RESET\}$(printf "%$((37-$\{#script_chars\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9568 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9571 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}\u55357 \u56523 Session ID: $\{SESSION_ID\}$\{RESET\}$(printf "%$((38-$\{#SESSION_ID\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}\u55357 \u56522 Benchmark logs: $\{BENCHMARK_DIR\}$\{RESET\}$(printf "%$((35-$\{#BENCHMARK_DIR\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}\u55357 \u56522 Metrics log: $\{METRICS_LOG\}$\{RESET\}$(printf "%$((38-$\{#METRICS_LOG\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}\u55357 \u56536 README file: $\{README\}$\{RESET\}$(printf "%$((38-$\{#README\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9553 $\{GRAY\}\u55357 \u56541 Changelog: $\{CHANGELOG\}$\{RESET\}$(printf "%$((39-$\{#CHANGELOG\}))s" "")$\{BLUE\}\u9553 $\{RESET\}"\
echo -e "$\{BLUE\}\uc0\u9562 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9552 \u9565 $\{RESET\}"\
\}\
\
# Function to update the model performance database\
update_model_performance() \{\
local model=$1\
local file_size=$2\
local duration=$3\
\
# Calculate time per KB\
local time_per_kb=0\
\
# Prevent division by zero or invalid calculations\
if [ -z "$file_size" ] || [ "$file_size" -le 0 ]; then\
echo "Warning: Invalid file size ($file_size) for performance calculation" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
return\
fi\
\
if [ -z "$duration" ] || [[ ! "$duration" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || (( $(echo "$duration <= 0" | bc -l 2>/dev/null) )); then\
echo "Warning: Invalid duration ($duration) for performance calculation" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
return\
fi\
\
# Calculate KB value first\
local size_kb=$(echo "scale=4; $file_size / 1024" | bc 2>/dev/null)\
\
# Validate KB conversion\
if [ -z "$size_kb" ] || [[ ! "$size_kb" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || (( $(echo "$size_kb <= 0" | bc -l 2>/dev/null) )); then\
echo "Warning: Invalid KB conversion ($size_kb) for performance calculation" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
return\
fi\
\
# Calculate time per KB\
time_per_kb=$(echo "scale=4; $duration / $size_kb" | bc 2>/dev/null)\
\
# Skip if the calculation failed or resulted in unreasonable values\
if [ -z "$time_per_kb" ] || [[ ! "$time_per_kb" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || \
(( $(echo "$time_per_kb <= 0" | bc -l 2>/dev/null) )) || \
(( $(echo "$time_per_kb > 1000" | bc -l 2>/dev/null) )); then\
echo "Warning: Unable to calculate reasonable performance metrics for $model (got $time_per_kb)" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
return\
fi\
\
echo "Calculated performance for $model: $time_per_kb secs/KB (file: $\{file_size\}B, $\{size_kb\}KB, duration: $\{duration\}s)" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
\
local perf_file="$\{BENCHMARK_DIR\}/model_performance.csv"\
\
# Create file if it doesn't exist\
if [ ! -f "$perf_file" ]; then\
echo "model,avg_time_per_kb,samples,last_updated" > "$perf_file"\
fi\
\
# Check if model exists in performance file\
if grep -q "^$\{model\}," "$perf_file"; then\
# Read existing entry\
local current_data=$(grep "^$\{model\}," "$perf_file")\
\
# Extract current values with validation\
local current_avg=$(echo "$current_data" | cut -d, -f2)\
if [ -z "$current_avg" ] || ! [[ "$current_avg" =~ ^[0-9]+(\\.[0-9]+)?$ ]]; then\
echo "Warning: Invalid average time in database for $model. Resetting to current value." >> "$\{BENCHMARK_DIR\}/model_log.txt"\
current_avg=$time_per_kb\
fi\
\
local current_samples=$(echo "$current_data" | cut -d, -f3)\
if [ -z "$current_samples" ] || ! [[ "$current_samples" =~ ^[0-9]+$ ]]; then\
echo "Warning: Invalid sample count in database for $model. Resetting to 1." >> "$\{BENCHMARK_DIR\}/model_log.txt"\
current_samples=1\
fi\
\
# Calculate new average (weighted by number of samples)\
local new_samples=$((current_samples + 1))\
\
# Ensure we have valid values for calculation\
if [ "$current_samples" -gt 0 ] && [ -n "$current_avg" ] && [ -n "$time_per_kb" ]; then\
local new_avg=$(echo "scale=4; (($current_avg * $current_samples) + $time_per_kb) / $new_samples" | bc 2>/dev/null)\
\
# Validate the calculated average\
if [ -z "$new_avg" ] || ! [[ "$new_avg" =~ ^[0-9]+(\\.[0-9]+)?$ ]] || \
(( $(echo "$new_avg <= 0" | bc -l 2>/dev/null) )) || \
(( $(echo "$new_avg > 1000" | bc -l 2>/dev/null) )); then\
echo "Warning: Invalid new average calculated for $model. Using current value." >> "$\{BENCHMARK_DIR\}/model_log.txt"\
new_avg=$time_per_kb\
fi\
\
# Update the entry\
echo "Updating model performance: $model, old: $current_avg ($current_samples samples), new: $new_avg ($new_samples samples)" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
sed -i '' "s/^$\{model\},$\{current_avg\},$\{current_samples\},.*/$\{model\},$\{new_avg\},$\{new_samples\},$(date '+%Y-%m-%d %H:%M:%S')/" "$perf_file"\
else\
# If we have invalid data, just replace with new data\
echo "Warning: Invalid data for weighted average calculation. Using current value for $model." >> "$\{BENCHMARK_DIR\}/model_log.txt"\
sed -i '' "s/^$\{model\},.*/$\{model\},$\{time_per_kb\},1,$(date '+%Y-%m-%d %H:%M:%S')/" "$perf_file"\
fi\
else\
# Add new entry\
echo "Adding new model performance entry for $model: $time_per_kb secs/KB" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
echo "$\{model\},$\{time_per_kb\},1,$(date '+%Y-%m-%d %H:%M:%S')" >> "$perf_file"\
fi\
\
# Log the update\
echo "$(date '+%Y-%m-%d %H:%M:%S') - Updated performance metrics for $model: $time_per_kb secs/KB (file size: $\{file_size\}B, duration: $\{duration\}s)" >> "$\{BENCHMARK_DIR\}/model_log.txt"\
\}\
\
# Function to update the changelog\
update_changelog() \{\
local script_name=$1\
local model=$2\
local duration=$3\
\
# Get the current date and time\
local today=$(date '+%Y-%m-%d')\
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')\
\
# Check if there's already an entry for today\
if grep -q "### $\{today\}" "$\{CHANGELOG\}"; then\
# Append to today's entry with timestamp\
sed -i '' "/### $\{today\}/a\\\\\
- Generated README for $\{script_name\} with $\{model\} ($\{duration\}s) at $\{timestamp\}\
" "$\{CHANGELOG\}"\
else\
# Create a new entry for today with timestamp\
sed -i '' "/^## Version/a\\\\\
\\\\\
### $\{today\}\\\\\
- Generated README for $\{script_name\} with $\{model\} ($\{duration\}s) at $\{timestamp\}\
" "$\{CHANGELOG\}"\
fi\
\
# Log to console\
echo -e "$\{GREEN\}Updated changelog at $\{CHANGELOG\} with entry for $\{script_name\}$\{RESET\}"\
\}\
\
# Function to check if running in a terminal\
is_terminal() \{\
[ -t 0 ]\
\}\
\
# Function to count script features\
count_script_features() \{\
local content="$1"\
\
# Count functions (very basic, may need refinement)\
local function_count=$(echo "$\{content\}" | grep -c "function " || echo "0")\
\
# Count commands (very rough estimate)\
local command_count=$(echo "$\{content\}" | grep -v "^#" | grep -v "^$" | grep -c ";" || echo "0")\
\
# Count conditionals\
local if_count=$(echo "$\{content\}" | grep -c "if " || echo "0")\
local case_count=$(echo "$\{content\}" | grep -c "case " || echo "0")\
\
# Count loops\
local for_count=$(echo "$\{content\}" | grep -c "for " || echo "0")\
local while_count=$(echo "$\{content\}" | grep -c "while " || echo "0")\
\
# Add features to metrics log (with error handling)\
jq --arg functions "$\{function_count\}" \\\
--arg commands "$\{command_count\}" \\\
--arg ifs "$\{if_count\}" \\\
--arg cases "$\{case_count\}" \\\
--arg fors "$\{for_count\}" \\\
--arg whiles "$\{while_count\}" \\\
'.script_features = \{"function_count": $functions, "command_count": $commands, "if_count": $ifs, "case_count": $cases, "for_count": $fors, "while_count": $whiles\}' "$\{METRICS_LOG\}" > "$\{METRICS_LOG\}.tmp" 2>/dev/null\
\
# Check if jq succeeded\
if [ $? -eq 0 ]; then\
mv "$\{METRICS_LOG\}.tmp" "$\{METRICS_LOG\}"\
else\
echo "Warning: Failed to update metrics log with script features"\
fi\
\}\
\
# Function to check for dependencies\
check_dependencies() \{\
local missing_deps=0\
\
# Start timer\
local start_time=$(date +%s.%N)\
\
# Check for jq\
if ! command -v jq &> /dev/null; then\
echo "Error: jq is required. Please install jq (e.g., 'brew install jq')."\
missing_deps=1\
fi\
\
# Check for bc\
if ! command -v bc &> /dev/null; then\
echo "Error: bc is required. Please install bc."\
missing_deps=1\
fi\
\
# Check for ollama\
if ! command -v ollama &> /dev/null; then\
echo "Error: ollama is required. Please install Ollama."\
missing_deps=1\
else\
# Check if Ollama server is running\
if ! curl -s -m 2 "http://localhost:11434/api/tags" &> /dev/null; then\
echo "Error: Ollama server is not running. Please start it with 'ollama serve'."\
missing_deps=1\
fi\
fi\
\
# End timer\
local end_time=$(date +%s.%N)\
local duration=$(printf "%.2f" $(echo "$\{end_time\} - $\{start_time\}" | bc))\
\
log_benchmark "system" "0" "0" "0" "system" "dependency_check" "$\{duration\}" "0"\
\
return $\{missing_deps\}\
\}\
\
# Function to get available models and determine their complexity\
get_models() \{\
local start_time=$(date +%s.%N)\
\
echo "Fetching available models..."\
local ollama_output=$(ollama list 2>/dev/null)\
\
if [ $? -ne 0 ]; then\
echo "Error: Failed to run 'ollama list'. Ensure Ollama is running."\
exit 1\
fi\
\
# Parse models\
models=($(echo "$ollama_output" | tail -n +2 | awk '\{print $1\}'))\
model_ids=($(echo "$ollama_output" | tail -n +2 | awk '\{print $2\}'))\
model_sizes=($(echo "$ollama_output" | tail -n +2 | awk '\{print $3, $4\}'))\
\
if [ $\{#models[@]\} -eq 0 ]; then\
echo "No models found. Please download a model using 'ollama pull <model>'."\
exit 1\
fi\
\
# Estimate complexity based on model info\
for ((i=1; i<=$\{#models[@]\}; i++)); do\
local model_name="$\{models[$i]\}"\
local model_size="$\{model_sizes[$i]\}"\
local size_number=0\
local complexity=0\
\
# Try to extract parameter size from model name\
# Common formats: model-7b, model:7b, model/7b, model-7B, etc.\
if [[ "$model_name" =~ ([0-9]+)[bB] ]]; then\
size_number=$\{BASH_REMATCH[1]\}\
fi\
\
# If not found in name, check the model size info from ollama list\
if [ "$size_number" -eq 0 ] && [[ "$model_size" =~ ([0-9]+(\\.[0-9]+)?)B ]]; then\
size_number=$\{BASH_REMATCH[1]\}\
fi\
\
# If still not found, try parameter_size from model info\
if [ "$size_number" -eq 0 ]; then\
# Get model info to extract parameter size\
local model_info=$(ollama show $model_name 2>/dev/null)\
if [[ "$model_info" =~ 'parameter_size":' ]]; then\
if [[ "$model_info" =~ 'parameter_size":"([0-9\\.]+)B' ]]; then\
size_number=$\{BASH_REMATCH[1]\}\
fi\
fi\
fi\
\
# Assign complexity based on size if found\
if (( $(echo "$size_number > 0" | bc -l 2>/dev/null) )); then\
# Determine size category\
if (( $(echo "$size_number < 4" | bc -l 2>/dev/null) )); then\
complexity=$\{MODEL_SIZE_COMPLEXITY["1-3B"]\}\
elif (( $(echo "$size_number < 8" | bc -l 2>/dev/null) )); then\
complexity=$\{MODEL_SIZE_COMPLEXITY["4-7B"]\}\
elif (( $(echo "$size_number < 14" | bc -l 2>/dev/null) )); then\
complexity=$\{MODEL_SIZE_COMPLEXITY["8-13B"]\}\
else\
complexity=$\{MODEL_SIZE_COMPLEXITY["14B+"]\}\
fi\
else\
# If size can't be determined, use default and adjust based on name heuristics\
complexity=$\{MODEL_COMPLEXITY["default"]\}\
\
# Adjust based on common model family naming conventions\
if [[ "$model_name" =~ (tiny|small|lite|1b|2b|3b) ]]; then\
complexity=$\{MODEL_SIZE_COMPLEXITY["1-3B"]\}\
elif [[ "$model_name" =~ (base|7b|6b|5b|4b) ]]; then\
complexity=$\{MODEL_SIZE_COMPLEXITY["4-7B"]\}\
elif [[ "$model_name" =~ (large|13b|12b|11b|10b|9b|8b) ]]; then\
complexity=$\{MODEL_SIZE_COMPLEXITY["8-13B"]\}\
elif [[ "$model_name" =~ (xl|xxl|huge|70b|35b|34b|33b|32b|31b|30b|20b|19b|18b|17b|16b|15b|14b) ]]; then\
complexity=$\{MODEL_SIZE_COMPLEXITY["14B+"]\}\
fi\
fi\
\
# Store complexity\
MODEL_COMPLEXITY["$model_name"]=$complexity\
\
# Log model details to log file\
echo "Model: $model_name, Size: $model_size, Complexity Factor: $complexity" >> "$\{BENCHMARK_DIR\}/models_log.txt"\
done\
\
# Set default model to first available model (unless already set)\
if [ -z "$DEFAULT_MODEL" ]; then\
DEFAULT_MODEL="$\{models[1]\}"\
fi\
\
# Add models to metrics log\
local models_json="["\
for ((i=1; i<=$\{#models[@]\}; i++)); do\
models_json+="\\"$\{models[$i]\}\\""\
if [ $i -lt $\{#models[@]\} ]; then\
models_json+=","\
fi\
done\
models_json+="]"\
\
jq --argjson models "$\{models_json\}" '.available_models = $models' "$\{METRICS_LOG\}" > "$\{METRICS_LOG\}.tmp" 2>/dev/null\
\
# Check if jq succeeded\
if [ $? -eq 0 ]; then\
mv "$\{METRICS_LOG\}.tmp" "$\{METRICS_LOG\}"\
else\
echo "Warning: Failed to update metrics log with available models"\