FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cecli/cecli/utils.py at main · cecli-dev/cecli · GitHub
cecli-dev
cecli
Repository navigation
Code
Issues
11
(11)
Pull requests
8
(8)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cecli
/
cecli
/
utils.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
634 lines (504 loc) · 18.2 KB
Breadcrumbs
cecli
/
cecli
/
utils.py
Copy path
File metadata and controls
634 lines (504 loc) · 18.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
glob
import
json
import
os
import
platform
import
shlex
import
shutil
import
ssl
import
subprocess
import
sys
import
tempfile
from
pathlib
import
Path
from
cecli
.
dump
import
dump
# noqa: F401
IMAGE_EXTENSIONS
=
{
".png"
,
".jpg"
,
".jpeg"
,
".gif"
,
".bmp"
,
".tiff"
,
".webp"
,
".pdf"
}
def
expand_glob_patterns
(
patterns
):
"""Expand glob patterns in a list of file paths."""
expanded_files
=
[]
for
pattern
in
patterns
:
# Check if the pattern contains glob characters
if
any
(
c
in
pattern
for
c
in
"*?[]"
):
# Use glob to expand the pattern
matches
=
glob
.
glob
(
pattern
,
recursive
=
True
)
if
matches
:
expanded_files
.
extend
(
matches
)
else
:
# If no matches, keep the original pattern
expanded_files
.
append
(
pattern
)
else
:
# Not a glob pattern, keep as is
expanded_files
.
append
(
pattern
)
return
expanded_files
def
_execute_fzf
(
input_data
,
multi
=
False
):
"""
Runs fzf as a subprocess, feeding it input_data.
Returns the selected items.
"""
if
not
shutil
.
which
(
"fzf"
):
return
[]
# fzf not available
fzf_command
=
[
"fzf"
,
"--read0"
]
if
multi
:
fzf_command
.
append
(
"--multi"
)
# Recommended flags for a good experience
fzf_command
.
extend
([
"--height"
,
"80%"
,
"--reverse"
])
process
=
subprocess
.
Popen
(
fzf_command
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
text
=
False
,
# Use binary mode for null character handling
)
# fzf expects a null-separated list of strings for multi-line items
# Join with null character instead of newline
input_bytes
=
"
\0
"
.
join
(
input_data
).
encode
(
"utf-8"
)
stdout
,
_
=
process
.
communicate
(
input_bytes
)
if
process
.
returncode
==
0
:
# fzf returns selected items null-separated when using --read0
output
=
stdout
.
decode
(
"utf-8"
).
rstrip
(
"
\0
\n
"
)
return
output
.
split
(
"
\0
"
)
if
output
else
[]
else
:
# User cancelled (e.g., pressed Esc)
return
[]
def
run_fzf
(
input_data
,
multi
=
False
,
coder
=
None
):
"""
Runs fzf as a subprocess, feeding it input_data.
Returns the selected items.
"""
if
not
shutil
.
which
(
"fzf"
):
return
[]
# fzf not available
tui
=
None
if
coder
is
not
None
and
coder
.
tui
:
tui
=
coder
.
tui
()
result
=
[]
if
tui
:
result
=
tui
.
run_obstructive
(
_execute_fzf
,
input_data
,
multi
=
multi
)
else
:
result
=
_execute_fzf
(
input_data
,
multi
=
multi
)
return
result
class
IgnorantTemporaryDirectory
:
def
__init__
(
self
):
if
sys
.
version_info
>=
(
3
,
10
):
self
.
temp_dir
=
tempfile
.
TemporaryDirectory
(
ignore_cleanup_errors
=
True
)
else
:
self
.
temp_dir
=
tempfile
.
TemporaryDirectory
()
def
__enter__
(
self
):
return
self
.
temp_dir
.
__enter__
()
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
self
.
cleanup
()
def
cleanup
(
self
):
try
:
self
.
temp_dir
.
cleanup
()
except
(
OSError
,
PermissionError
,
RecursionError
):
pass
# Ignore errors (Windows and potential recursion)
def
__getattr__
(
self
,
item
):
return
getattr
(
self
.
temp_dir
,
item
)
class
ChdirTemporaryDirectory
(
IgnorantTemporaryDirectory
):
def
__init__
(
self
):
try
:
self
.
cwd
=
os
.
getcwd
()
except
FileNotFoundError
:
self
.
cwd
=
None
super
().
__init__
()
def
__enter__
(
self
):
res
=
super
().
__enter__
()
os
.
chdir
(
Path
(
self
.
temp_dir
.
name
).
resolve
())
return
str
(
Path
(
res
).
resolve
())
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
if
self
.
cwd
:
try
:
os
.
chdir
(
self
.
cwd
)
except
FileNotFoundError
:
pass
super
().
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
class
GitTemporaryDirectory
(
ChdirTemporaryDirectory
):
def
__enter__
(
self
):
dname
=
super
().
__enter__
()
self
.
repo
=
make_repo
(
dname
)
return
dname
def
__exit__
(
self
,
exc_type
,
exc_val
,
exc_tb
):
del
self
.
repo
super
().
__exit__
(
exc_type
,
exc_val
,
exc_tb
)
def
make_repo
(
path
=
None
):
import
git
if
not
path
:
path
=
"."
repo
=
git
.
Repo
.
init
(
path
)
repo
.
config_writer
().
set_value
(
"user"
,
"name"
,
"Test User"
).
release
()
repo
.
config_writer
().
set_value
(
"user"
,
"email"
,
"testuser@example.com"
).
release
()
return
repo
def
is_image_file
(
file_name
):
"""
Check if the given file name has an image file extension.
:param file_name: The name of the file to check.
:return: True if the file is an image, False otherwise.
"""
file_name
=
str
(
file_name
)
# Convert file_name to string
return
any
(
file_name
.
endswith
(
ext
)
for
ext
in
IMAGE_EXTENSIONS
)
def
safe_abs_path
(
res
):
"Gives an abs path, which safely returns a full (not 8.3) windows path"
res
=
Path
(
res
).
resolve
()
return
str
(
res
)
def
safe_relpath
(
path
,
start
=
None
):
"""Return ``os.path.relpath(path, start)``, falling back to *path* on cross-drive.
On Windows, ``os.path.relpath`` raises ``ValueError`` when *path* and *start*
(or the implicit CWD) are on different drives. Keep the absolute path in that
case instead of crashing.
"""
try
:
return
os
.
path
.
relpath
(
path
,
start
)
except
ValueError
:
return
str
(
path
)
def
format_content
(
role
,
content
):
formatted_lines
=
[]
for
line
in
content
.
splitlines
():
formatted_lines
.
append
(
f"
{
role
}
{
line
}
"
)
return
"
\n
"
.
join
(
formatted_lines
)
def
format_messages
(
messages
,
title
=
None
):
output
=
[]
if
title
:
output
.
append
(
f"
{
title
.
upper
()
}
{
'*'
*
50
}
"
)
for
msg
in
messages
:
output
.
append
(
"-------"
)
role
=
msg
[
"role"
].
upper
()
content
=
msg
.
get
(
"content"
)
if
isinstance
(
content
,
list
):
# Handle list content (e.g., image messages)
for
item
in
content
:
if
isinstance
(
item
,
dict
):
for
key
,
value
in
item
.
items
():
if
isinstance
(
value
,
dict
)
and
"url"
in
value
:
output
.
append
(
f"
{
role
}
{
key
.
capitalize
()
}
URL:
{
value
[
'url'
]
}
"
)
else
:
output
.
append
(
f"
{
role
}
{
key
}
:
{
value
}
"
)
else
:
output
.
append
(
f"
{
role
}
{
item
}
"
)
elif
isinstance
(
content
,
str
):
# Handle string content
# For large content, especially with many files, use a truncated display approach
if
len
(
content
)
>
5000
:
# Count the number of code blocks (approximation)
fence_count
=
content
.
count
(
"```"
)
//
2
if
fence_count
>
5
:
# Show truncated content with file count for large files to improve performance
first_line
=
content
.
split
(
"
\n
"
,
1
)[
0
]
output
.
append
(
f"
{
role
}
{
first_line
}
[content with ~
{
fence_count
}
files truncated]"
)
else
:
output
.
append
(
format_content
(
role
,
content
))
else
:
output
.
append
(
format_content
(
role
,
content
))
function_call
=
msg
.
get
(
"function_call"
)
if
function_call
:
output
.
append
(
f"
{
role
}
Function Call:
{
function_call
}
"
)
return
"
\n
"
.
join
(
output
)
def
show_messages
(
messages
,
title
=
None
,
functions
=
None
):
formatted_output
=
format_messages
(
messages
,
title
)
print
(
formatted_output
)
if
functions
:
dump
(
functions
)
def
split_chat_history_markdown
(
text
,
include_tool
=
False
):
messages
=
[]
user
=
[]
assistant
=
[]
tool
=
[]
lines
=
text
.
splitlines
(
keepends
=
True
)
def
append_msg
(
role
,
lines
):
lines
=
""
.
join
(
lines
)
if
lines
.
strip
():
messages
.
append
(
dict
(
role
=
role
,
content
=
lines
))
for
line
in
lines
:
if
line
.
startswith
(
"# "
):
continue
if
line
.
startswith
(
"> "
):
append_msg
(
"assistant"
,
assistant
)
assistant
=
[]
append_msg
(
"user"
,
user
)
user
=
[]
tool
.
append
(
line
[
2
:])
continue
# if line.startswith("#### /"):
# continue
if
line
.
startswith
(
"#### "
):
append_msg
(
"assistant"
,
assistant
)
assistant
=
[]
append_msg
(
"tool"
,
tool
)
tool
=
[]
content
=
line
[
5
:]
user
.
append
(
content
)
continue
append_msg
(
"user"
,
user
)
user
=
[]
append_msg
(
"tool"
,
tool
)
tool
=
[]
assistant
.
append
(
line
)
append_msg
(
"assistant"
,
assistant
)
append_msg
(
"user"
,
user
)
if
not
include_tool
:
messages
=
[
m
for
m
in
messages
if
m
[
"role"
]
!=
"tool"
]
return
messages
def
get_pip_install
(
args
):
cmd
=
[
sys
.
executable
,
"-m"
,
"pip"
,
"install"
,
"--upgrade"
,
"--upgrade-strategy"
,
"only-if-needed"
,
]
cmd
+=
args
return
cmd
def
get_self_upgrade_command
():
"""Return the command used to upgrade cecli for the current install method."""
# pipx sets these environment variables inside each tool's venv
if
os
.
environ
.
get
(
"PIPX_VENV_DIR"
)
or
os
.
environ
.
get
(
"PIPX_PACKAGE_DIR"
):
return
[
"pipx"
,
"upgrade"
,
"cecli-dev"
]
if
_is_uv_tool_env
():
return
[
"uv"
,
"tool"
,
"upgrade"
,
"cecli-dev"
]
return
get_pip_install
([
"cecli-dev"
])
def
_is_pip_install_command
(
cmd
):
"""Return True if cmd is a `python -m pip install ...` invocation."""
try
:
i
=
cmd
.
index
(
"-m"
)
except
ValueError
:
return
False
return
i
+
2
<
len
(
cmd
)
and
cmd
[
i
+
1
]
==
"pip"
and
cmd
[
i
+
2
]
==
"install"
def
_is_uv_tool_env
():
"""Return True if running inside a `uv tool install` environment."""
cfg
=
Path
(
sys
.
prefix
)
/
"pyvenv.cfg"
try
:
if
not
(
cfg
.
exists
()
and
"uv = "
in
cfg
.
read_text
(
errors
=
"replace"
)):
return
False
except
OSError
:
return
False
prefix
=
Path
(
sys
.
prefix
)
tool_dir
=
os
.
environ
.
get
(
"UV_TOOL_DIR"
)
if
tool_dir
:
try
:
if
prefix
.
is_relative_to
(
tool_dir
):
return
True
except
(
ValueError
,
OSError
):
pass
# Default uv tool installs live under ".../uv/tools/<name>".
# A plain `uv venv` project venv does not have the uv/tools path parts.
return
"uv"
in
prefix
.
parts
and
"tools"
in
prefix
.
parts
def
run_install
(
cmd
):
print
()
print
(
"Installing:"
,
printable_shell_command
(
cmd
))
if
_is_pip_install_command
(
cmd
):
# First ensure pip is available
ensurepip_cmd
=
[
sys
.
executable
,
"-m"
,
"ensurepip"
,
"--upgrade"
]
try
:
subprocess
.
run
(
ensurepip_cmd
,
capture_output
=
True
,
check
=
False
)
except
Exception
:
pass
# Continue even if ensurepip fails
try
:
from
cecli
.
waiting
import
Spinner
output
=
[]
process
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
,
text
=
True
,
bufsize
=
1
,
universal_newlines
=
True
,
encoding
=
getattr
(
sys
.
stdout
,
"encoding"
,
None
)
or
"utf-8"
,
errors
=
"replace"
,
)
spinner
=
Spinner
(
"Installing..."
)
while
True
:
char
=
process
.
stdout
.
read
(
1
)
if
not
char
:
break
output
.
append
(
char
)
spinner
.
step
()
spinner
.
end
()
return_code
=
process
.
wait
()
output
=
""
.
join
(
output
)
if
return_code
==
0
:
print
(
"Installation complete."
)
print
()
return
True
,
output
except
subprocess
.
CalledProcessError
as
e
:
print
(
f"
\n
Error running pip install:
{
e
}
"
)
print
(
"
\n
Installation failed.
\n
"
)
return
False
,
output
def
find_common_root
(
abs_fnames
):
try
:
if
len
(
abs_fnames
)
==
1
:
return
safe_abs_path
(
os
.
path
.
dirname
(
list
(
abs_fnames
)[
0
]))
elif
abs_fnames
:
return
safe_abs_path
(
os
.
path
.
commonpath
(
list
(
abs_fnames
)))
except
(
OSError
,
ValueError
):
# ValueError: cross-drive commonpath on Windows (e.g. C: vs E:).
pass
# Restore the original safe fallback: callers assign this straight to
# Coder.root, and Path(False) (or the implicit None for empty input)
# would TypeError on the next Path(root) call. "" resolves as the CWD,
# matching the pre-existing behavior.
return
""
def
format_tokens
(
count
):
if
count
<
1000
:
return
f"
{
count
}
"
elif
count
<
10000
:
return
f"
{
count
/
1000
:.1f
}
k"
else
:
return
f"
{
round
(
count
/
1000
)
}
k"
def
touch_file
(
fname
):
fname
=
Path
(
fname
)
try
:
fname
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
fname
.
touch
()
return
True
except
OSError
:
return
False
async
def
check_pip_install_extra
(
io
,
module
,
prompt
,
pip_install_cmd
=
None
,
self_update
=
False
,
cmd
=
None
):
if
module
:
for
_attempt
in
range
(
2
):
try
:
__import__
(
module
)
return
True
except
ssl
.
SSLError
:
# OpenSSL CONF module lazy-init flake (WSL2 + OpenSSL 3.5 + Py3.14):
# the very first SSL context creation can fail with
# ``[CONF: MODULE_INITIALIZATION_ERROR]`` / "unknown error (0x0)",
# and a second attempt succeeds. Mirrors llms.runtime.make_client.
if
_attempt
==
0
:
continue
raise
except
(
ImportError
,
ModuleNotFoundError
,
RuntimeError
):
break
if
cmd
is
None
:
cmd
=
get_pip_install
(
pip_install_cmd
or
[])
if
prompt
:
io
.
tool_warning
(
prompt
)
if
self_update
and
platform
.
system
()
==
"Windows"
:
io
.
tool_output
(
"Run this command to update:"
)
print
()
print
(
printable_shell_command
(
cmd
))
# plain print so it doesn't line-wrap
return
run_prompt
=
"Run pip install?"
if
cmd
and
cmd
[
0
]
==
"uv"
:
run_prompt
=
"Run uv tool upgrade?"
elif
cmd
and
cmd
[
0
]
==
"pipx"
:
run_prompt
=
"Run pipx upgrade?"
if
not
await
io
.
confirm_ask
(
run_prompt
,
default
=
"y"
,
subject
=
printable_shell_command
(
cmd
)):
return
success
,
output
=
run_install
(
cmd
)
if
success
:
if
not
module
:
return
True
try
:
__import__
(
module
)
return
True
except
(
ImportError
,
ModuleNotFoundError
,
RuntimeError
)
as
err
:
io
.
tool_error
(
str
(
err
))
pass
io
.
tool_error
(
output
)
print
()
print
(
"Install failed, try running this command manually:"
)
print
(
printable_shell_command
(
cmd
))
def
printable_shell_command
(
cmd_list
):
"""
Convert a list of command arguments to a properly shell-escaped string.
Args:
cmd_list (list): List of command arguments.
Returns:
str: Shell-escaped command string.
"""
return
shlex
.
join
(
cmd_list
)
def
split_concatenated_json
(
s
:
str
)
->
list
[
str
]:
"""
Splits a string containing one or more concatenated JSON objects
and returns them as a list of raw strings.
"""
res
=
[]
decoder
=
json
.
JSONDecoder
()
idx
=
0
s_len
=
len
(
s
)
while
idx
<
s_len
:
# 1. Use Regex-free "find" to jump to the next potential JSON start
# This replaces your manual 'while s[i].isspace()' loop
brace_idx
=
s
.
find
(
"{"
,
idx
)
bracket_idx
=
s
.
find
(
"["
,
idx
)
# Determine the earliest starting point
if
brace_idx
==
-
1
and
bracket_idx
==
-
1
:
# No more JSON documents found, but check for trailing text
remainder
=
s
[
idx
:].
strip
()
if
remainder
:
res
.
append
(
s
[
idx
:])
break
# Set idx to the first '{' or '[' found
start_index
=
(
min
(
brace_idx
,
bracket_idx
)
if
(
brace_idx
!=
-
1
and
bracket_idx
!=
-
1
)
else
max
(
brace_idx
,
bracket_idx
)
)
try
:
# 2. Let the C-optimized parser find the end of the object
_
,
end_idx
=
decoder
.
raw_decode
(
s
,
start_index
)
# 3. Slice the original string and add to results
res
.
append
(
s
[
start_index
:
end_idx
])
# Move our pointer to the end of the last document
idx
=
end_idx
except
json
.
JSONDecodeError
:
# If it looks like JSON but fails (e.g. malformed),
# we skip this character and try to find the next valid start
idx
=
start_index
+
1
return
res
def
parse_concatenated_json
(
s
:
str
)
->
list
:
objs
=
[]
decoder
=
json
.
JSONDecoder
()
idx
=
0
s_len
=
len
(
s
)
while
idx
<
s_len
:
# Jump to the next potential start of a JSON object or array
# This skips whitespace, commas, or "noise" between documents instantly
brace_idx
=
s
.
find
(
"{"
,
idx
)
bracket_idx
=
s
.
find
(
"["
,
idx
)
# Determine which one comes first
if
brace_idx
==
-
1
and
bracket_idx
==
-
1
:
break
elif
brace_idx
==
-
1
:
idx
=
bracket_idx
elif
bracket_idx
==
-
1
:
idx
=
brace_idx
else
:
idx
=
min
(
brace_idx
,
bracket_idx
)
try
:
# raw_decode attempts to parse starting exactly at idx
obj
,
end_idx
=
decoder
.
raw_decode
(
s
,
idx
)
objs
.
append
(
obj
)
idx
=
end_idx
except
json
.
JSONDecodeError
:
# If it's a false start (like a { inside a non-JSON string),
# skip it and keep looking
idx
+=
1
return
objs
def
copy_tool_call
(
tool_call
):
"""
Copies a tool call whether it's a Pydantic model, SimpleNamespace, or dict.
"""
from
types
import
SimpleNamespace
if
hasattr
(
tool_call
,
"model_copy"
):
return
tool_call
.
model_copy
(
deep
=
True
)
if
isinstance
(
tool_call
,
SimpleNamespace
):
import
copy
return
copy
.
deepcopy
(
tool_call
)
if
isinstance
(
tool_call
,
dict
):
import
copy
return
copy
.
deepcopy
(
tool_call
)
return
tool_call
def
tool_call_to_dict
(
tool_call
):
"""
Converts any tool-call representation to a dict.
"""
if
hasattr
(
tool_call
,
"model_dump"
):
return
tool_call
.
model_dump
()
if
hasattr
(
tool_call
,
"__dict__"
):
res
=
dict
(
tool_call
.
__dict__
)
if
"function"
in
res
and
hasattr
(
res
[
"function"
],
"__dict__"
):
res
[
"function"
]
=
dict
(
res
[
"function"
].
__dict__
)
return
res
if
isinstance
(
tool_call
,
dict
):
return
tool_call
return
{}
Back
|
FazBrowse Home
|
New Git URL