Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
1800 lines (1633 loc) · 70.8 KB
/
Copy pathserver.py
File metadata and controls
1800 lines (1633 loc) · 70.8 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
# -*- coding: utf-8 -*-
"""omp 设置中心 / omp Settings Web(原 omp Settings Center,更早是模型角色切换器)。
- 模型角色:两级分组选择器,点击切换 modelRoles。
- 全部设置:读取 `omp config list --json` 全量目录(键/值/类型/描述),
标量项(布尔/数字/字符串/枚举)定向编辑 config.yml;数组与嵌套块只读展示。
- 用量:透传 `omp usage --json` 的额度/用量报告(各供应商窗口限额、
已用/剩余、重置时间、重置额度券),供「用量」页签做可视化。
安全设计:不用 `omp config set`(会重写整文件并剥离注释);按字节读写
~/.omp/agent/config.yml,只替换/插入目标行并沿用原行尾;每次写入前自动备份。
"""
import fnmatch
import json
import threading
import re
import shutil
import subprocess
import sys
import time
import webbrowser
import sqlite3
import urllib.request
import urllib.error
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
DB = Path.home() / ".omp" / "agent" / "agent.db"
HERE = Path(__file__).parent
CONFIG = Path.home() / ".omp" / "agent" / "config.yml"
PORT = 8788
EDITABLE_TYPES = {"string", "boolean", "number", "integer", "enum"}
# pythonw 下无控制台,omp.exe 子进程会各自弹出新窗口;Windows 上一律隐藏
NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0)
# ThreadingHTTPServer 下并发 POST 会同时改写 config.yml;统一串行化所有写入
_config_lock = threading.Lock()
ROLE_COMMENT = {
"default": "主会话模型",
"smol": "轻量快速任务",
"slow": "深度推理",
"plan": "规划模式",
"vision": "图像理解",
"task": "子代理",
"commit": "提交信息生成",
"tiny": "极小分类/标题",
"web": "网络搜索",
"advisor": "顾问评审",
}
KEY_LINE = re.compile(r'^([ \t]*)(.+?):(?:[ \t]([^\r\n]*))?(\r?\n)?$')
# 纯标量安全字符;其余一律双引号(JSON 转义是 YAML 双引号风格的子集)
PLAIN_SCALAR = re.compile(r'[A-Za-z0-9_@.,/\-]+(?::[A-Za-z0-9_@.,/\-]+)*')
def run(args):
p = subprocess.run(args, capture_output=True, text=True, shell=False,
encoding="utf-8", errors="replace", creationflags=NO_WINDOW)
return p.stdout
# ---------- config.yml 行级解析 ----------
def split_comment(v):
"""剥掉引号外的行尾注释,返回 (值, 注释)。"""
inq = None
for i, ch in enumerate(v):
if inq:
if ch == inq:
inq = None
elif ch in '"\'':
inq = ch
elif ch == '#' and (i == 0 or v[i - 1] in ' \t'):
return v[:i].rstrip(), v[i:]
return v.rstrip(), ''
def index_config(lines):
"""按缩进栈扫描,返回 {点路径: {line, kind: scalar|block}}。"""
idx, stack = {}, []
for i, line in enumerate(lines):
if not line.strip() or line.lstrip().startswith('#'):
continue
m = KEY_LINE.match(line)
if m is None:
continue
indent = len(m.group(1))
key = m.group(2).strip().strip('"\'')
val = (m.group(3) or '').strip()
while stack and stack[-1][0] >= indent:
stack.pop()
path = '.'.join(k for _, k in stack + [(indent, key)])
idx[path] = {'line': i, 'kind': 'scalar' if val else 'block'}
stack.append((indent, key))
return idx
def yaml_scalar(s):
if PLAIN_SCALAR.fullmatch(s) and \
not re.fullmatch(r'(?i:true|false|null|yes|no|on|off|~|-?\d+(\.\d+)?)', s):
return s
return json.dumps(s, ensure_ascii=False)
def coerce_value(typ, s):
s = s.strip()
if typ == 'boolean':
low = s.lower()
if low in ('true', '是', '1', 'yes'):
return 'true'
if low in ('false', '否', '0', 'no'):
return 'false'
raise ValueError('布尔项只接受 true/false')
if typ in ('number', 'integer'):
if re.fullmatch(r'-?\d+(\.\d+)?([eE][-+]?\d+)?', s) is None:
raise ValueError('该项要求数字')
return s
if s == '':
raise ValueError('空值请用 null 表示')
if s.lower() == 'null':
return 'null'
return yaml_scalar(s)
def write_backup():
backup = CONFIG.with_name(
f"config.yml.bak-settings-web-{time.strftime('%Y%m%d-%H%M%S')}")
shutil.copy2(CONFIG, backup)
return backup
def set_config_key(key, formatted):
with _config_lock:
return _set_config_key(key, formatted)
def _set_config_key(key, formatted):
raw = CONFIG.read_bytes().decode('utf-8')
eol = '\r\n' if '\r\n' in raw else '\n'
lines = raw.splitlines(keepends=True)
idx = index_config(lines)
if key in idx:
info = idx[key]
if info['kind'] != 'scalar':
return '该项在 config.yml 中是块值(数组/嵌套),请手工编辑'
m = KEY_LINE.match(lines[info['line']])
if m is None:
return '内部错误:行格式变化'
comment = (m.group(4) or '')
_, c = split_comment(m.group(3) or '')
lines[info['line']] = f"{m.group(1)}{m.group(2)}: {formatted}{c}{comment}"
if not lines[info['line']].endswith('\n'):
lines[info['line']] += eol
else:
parts = key.split('.')
target = None
for depth in range(len(parts) - 1, 0, -1):
p = '.'.join(parts[:depth])
if p in idx:
if idx[p]['kind'] != 'block':
return '父级是标量,无法插入子键'
target = (idx[p]['line'], depth)
break
new = []
base = target[1] if target else 0
remaining = parts[base:] if target else parts
for j, part in enumerate(remaining):
ind = ' ' * (base + j)
last = j == len(remaining) - 1
new.append(f"{ind}{part}:{f' {formatted}' if last else ''}{eol}")
if target:
lines[target[0] + 1:target[0] + 1] = new
else:
if lines and not lines[-1].endswith('\n'):
lines[-1] += eol
lines.extend(new)
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
# ---------- 读取 ----------
def parse_roles():
roles, in_block = {}, False
for line in CONFIG.read_bytes().decode('utf-8').splitlines():
if re.match(r'^modelRoles:\s*$', line):
in_block = True
continue
if in_block:
m = re.match(r'^ (\S+):\s*(\S+)\s*$', line)
if m:
roles[m.group(1)] = m.group(2)
elif line and not line.startswith(' '):
break
return roles
def list_models():
out = run(["omp", "models", "--json"])
try:
models = json.loads(out)["models"]
except (json.JSONDecodeError, KeyError):
return []
keep = []
for m in models:
if m.get("kind") != "chat":
continue
keep.append({
"selector": m["selector"],
"name": m.get("name") or m["id"],
"provider": m.get("provider"),
"contextWindow": m.get("contextWindow"),
"reasoning": bool(m.get("reasoning")),
})
keep.sort(key=lambda x: (x["provider"] or "", x["name"].lower()))
return keep
# ---------- 模型显示(enabledModels) ----------
ENABLED_HEAD = re.compile(r'^enabledModels:[ \t]*(\[\s*\])?[ \t]*(#.*)?(\r?\n)?$')
LIST_ITEM = re.compile(r'^[ \t]+-[ \t]+(.*?)[ \t]*(\r?\n)?$')
def enabled_block(lines):
"""定位顶层 enabledModels 段,返回 (首行, 尾后行, 条目) 或 None;含 path 作用域条目时条目为 None。"""
for i, line in enumerate(lines):
m = ENABLED_HEAD.match(line)
if not m:
continue
if m.group(1): # enabledModels: []
return i, i + 1, []
entries, j = [], i + 1
while j < len(lines):
ln = lines[j]
if not ln.strip():
break
if not ln[0].isspace():
break
item = LIST_ITEM.match(ln)
if item is None:
return i, None, None # 嵌套/作用域结构,不托管
v, _ = split_comment(item.group(1))
if v.startswith(('{', 'path', 'paths')) or v.endswith(':'):
return i, None, None
if len(v) >= 2 and v[0] == v[-1] and v[0] in '"\'':
v = v[1:-1]
entries.append(v)
j += 1
return i, j, entries
return None
def pattern_matches(pat, model):
p = pat.lower()
sel = model["selector"].lower()
if any(c in p for c in "*?["):
return fnmatch.fnmatchcase(sel, p) or fnmatch.fnmatchcase(sel.split('/', 1)[1], p)
return p == sel or p == sel.split('/', 1)[1]
def visibility_state():
lines = CONFIG.read_bytes().decode('utf-8').splitlines(keepends=True)
blk = enabled_block(lines)
models = list_models()
patterns = blk[2] if blk else []
managed = not (blk and blk[2] is None)
if not patterns: # 空名单 = 全部显示
visible = [m["selector"] for m in models]
else:
visible = [m["selector"] for m in models
if any(pattern_matches(p, m) for p in patterns)]
return {"models": models, "visible": visible, "patterns": patterns or [],
"managed": managed, "default": parse_roles().get("default")}
def set_visible(selected):
with _config_lock:
return _set_visible(selected)
def _set_visible(selected):
if not isinstance(selected, list) or not all(isinstance(s, str) for s in selected):
return "参数格式错误"
models = list_models()
by_sel = {m["selector"]: m for m in models}
chosen = {s for s in selected if s in by_sel}
if not chosen:
return "至少要保留一个可见模型(空名单在 omp 中等于全部显示)"
raw = CONFIG.read_bytes().decode('utf-8')
eol = '\r\n' if '\r\n' in raw else '\n'
lines = raw.splitlines(keepends=True)
blk = enabled_block(lines)
if blk and blk[2] is None:
return "config.yml 的 enabledModels 含 path 作用域等复杂结构,请手工编辑"
old = blk[2] if blk else []
# 当前匹配不到任何模型的条目(供应商离线等)原样保留,避免上线后被意外隐藏
dormant = [p for p in old if not any(pattern_matches(p, m) for m in models)]
providers = {}
for m in models:
providers.setdefault(m["provider"], []).append(m["selector"])
out = []
default = parse_roles().get("default")
if default and default in by_sel: # 启动模型取名单首项:把 default 角色模型放第一位且保持可见
chosen.add(default)
out.append(default)
for prov in sorted(providers):
sels = providers[prov]
if all(s in chosen for s in sels):
out.append(f"{prov}/*")
else:
out.extend(s for s in sorted(sels) if s in chosen and s != default)
out.extend(p for p in dormant if p not in out)
block = [f"enabledModels:{eol}"] + [f" - {json.dumps(p)}{eol}" for p in out]
if blk:
lines[blk[0]:blk[1]] = block
else:
if lines and not lines[-1].endswith('\n'):
lines[-1] += eol
lines.extend(block)
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
# ---------- 回退链(retry.fallbackChains 等) ----------
# 只托管 retry 顶层块/单行 flow map;fallbackChains 仅接受
# 块 map、inline [] 数组或显式 null,其余结构一律拒绝改写。
_MODEL_PATH = r'[A-Za-z0-9_.\-]+/(?:[A-Za-z0-9_.\-]+/)*[A-Za-z0-9_.*\-]+(?::[A-Za-z0-9_.*\-]+)?'
_THINKING = r'(?::(?:off|minimal|low|medium|high|xhigh|max|auto|inherit))?'
CHAIN_KEY = re.compile(r'(?:[A-Za-z0-9_.\-]+|' + _MODEL_PATH + r')' + _THINKING)
CHAIN_SELECTOR = re.compile(_MODEL_PATH + _THINKING)
FALLBACK_DEFAULTS = {"modelFallback": True, "fallbackOn": "any",
"fallbackRevertPolicy": "cooldown-expiry"}
FALLBACK_SCALARS = ("modelFallback", "fallbackOn", "fallbackRevertPolicy")
def _unquote(s):
s = s.strip()
if len(s) >= 2 and s[0] == s[-1] and s[0] in '"\'':
if s[0] == '"':
try:
return json.loads(s)
except json.JSONDecodeError:
return s[1:-1]
return s[1:-1]
return s
def _yaml_text(s):
s, _ = split_comment(s)
return _unquote(s)
def _split_flow(s):
"""按顶层逗号切 flow 内容,跳过引号与 []/{} 嵌套;不配对返回 None。"""
parts, depth, inq, cur = [], 0, None, ''
for ch in s:
if inq:
cur += ch
if ch == inq:
inq = None
continue
if ch in '"\'':
inq = ch
elif ch in '[{':
depth += 1
elif ch in ']}':
depth -= 1
elif ch == ',' and depth == 0:
parts.append(cur)
cur = ''
continue
cur += ch
if inq or depth != 0:
return None
parts.append(cur)
return [p for p in parts if p.strip()]
def _split_flow_pair(p):
"""在顶层找第一个冒号切 key:value;非合法 flow 对返回 None。"""
depth, inq = 0, None
for i, ch in enumerate(p):
if inq:
if ch == inq:
inq = None
elif ch in '"\'':
inq = ch
elif ch in '[{':
depth += 1
elif ch in ']}':
depth -= 1
elif ch == ':' and depth == 0:
return p[:i], p[i + 1:]
return None
def _parse_flow_seq(s):
"""解析 '[a, b]';返回 (items 或 None, ok)。ok=False 表示根本不是 '[' 开头。"""
s = s.strip()
if not s.startswith('['):
return None, False
if not s.endswith(']'):
return None, False
parts = _split_flow(s[1:-1])
if parts is None:
return None, True
return [_yaml_text(p) for p in parts], True
def _parse_flow_map(s):
"""解析 '{k: v, ...}' 单级 flow map;返回 (dict 或 None, ok)。"""
s = s.strip()
if not s.startswith('{') or not s.endswith('}'):
return None, False
parts = _split_flow(s[1:-1])
if parts is None:
return None, True
out = {}
for p in parts:
pair = _split_flow_pair(p)
if pair is None:
return None, True
k, v = pair
out[_unquote(k)] = v.strip()
return out, True
def _flow_chains(vclean):
"""把 inline 值(flow map 或 null/[] 空形式)解析成 {key: chain};
非 map、成员非序列、或结构非法返回 None。"""
if not vclean or vclean in ('null', '~', '{}', '[]'):
return {}
sub, ok = _parse_flow_map(vclean)
if not ok or sub is None:
return None
out = {}
for ck, cv in sub.items():
arr, ok = _parse_flow_seq(cv)
if not ok or arr is None:
return None
out[ck] = arr or []
return out
def _fc_children(lines, head_line, head_indent):
"""扫描块 map 形态的 fallbackChains 子键。
返回 (end, {key: {'chain': [...], 'seq': 是否块序列, 'ks', 'ke'}});
结构不支持返回 (head_line, None)。ke 为包块序列子项的排他结束行。"""
entries, j = {}, head_line + 1
while j < len(lines):
ln = lines[j]
if not ln.strip() or ln.lstrip().startswith('#'):
j += 1
continue
lead = len(ln) - len(ln.lstrip(' '))
if lead <= head_indent:
break
km = KEY_LINE.match(ln)
if km is None:
return head_line, None
kname = _unquote(km.group(2).strip())
kval, _ = split_comment(km.group(3) or '')
kval = kval.strip()
ks, ke = j, j + 1
if not kval:
# 空值 → 后跟更深缩进的 - 块序列;否则视同空链 []
chain, seq, j2 = [], False, j + 1
while j2 < len(lines):
ln2 = lines[j2]
if not ln2.strip():
j2 += 1
continue
if ln2.lstrip().startswith('#'):
j2 += 1
continue
if (len(ln2) - len(ln2.lstrip(' '))) <= lead:
break
im = LIST_ITEM.match(ln2)
if im is None:
return head_line, None # 键下嵌套 map/标量,不托管
seq = True
v = _yaml_text(im.group(1))
if v.startswith(('{', '[')):
return head_line, None
chain.append(v)
j2 += 1
# Leave comments between this chain and the next key outside its range.
ke = j2
while ke > ks + 1 and (not lines[ke - 1].strip() or
lines[ke - 1].lstrip().startswith('#')):
ke -= 1
entries[kname] = {'chain': chain, 'seq': seq, 'ks': ks, 'ke': ke}
j = ke
continue
arr, ok = _parse_flow_seq(kval)
if not ok or arr is None:
return head_line, None # 多行标量/嵌套/引用等,不托管
entries[kname] = {'chain': arr or [], 'seq': False, 'ks': ks, 'ke': ke}
j += 1
return j, entries
def _retry_region(idx, lines):
"""返回 (retry 行, retry 缩进, retry 块结束行);retry 非块返回 None。"""
info = idx.get('retry')
if info is None:
return None
m = KEY_LINE.match(lines[info['line']])
value, _ = split_comment(m.group(3) or '')
if value.strip():
return None
indent = len(lines[info['line']]) - len(lines[info['line']].lstrip(' '))
j = info['line'] + 1
while j < len(lines):
ln = lines[j]
if ln.strip() and not ln.lstrip().startswith('#') and \
(len(ln) - len(ln.lstrip(' '))) <= indent:
break
j += 1
return info['line'], indent, j
def _retry_flow_parse(m):
"""解析 retry 行上的 flow map 值,返回 (fmap, chains, scalars) 或 None。
fmap 为 {key: 原值文本};chains 是 fallbackChains 的 {key: chain}。"""
raw, _ = split_comment(m.group(3) or '')
fmap, ok = _parse_flow_map(raw)
if not ok or fmap is None:
return None
chains, scalars = {}, {}
for k, v in fmap.items():
if k == 'fallbackChains':
c = _flow_chains(v)
if c is None:
return None
chains = c
elif k in FALLBACK_SCALARS:
scalars[k] = _yaml_text(v)
return fmap, chains, scalars
def _scalar_bool(v):
return str(v).strip().lower() in ('true', 'yes', 'on', '1')
def _scalar_choice(v, options, default):
v = str(v).strip()
return v if v in options else default
def fallback_state():
"""读 retry.fallbackChains 与相关标量;不支持的 YAML 返回 {"error": ...}。"""
try:
raw = CONFIG.read_bytes().decode('utf-8')
except OSError as e:
return {"error": f"无法读取 {CONFIG}: {e}"}
lines = raw.splitlines(keepends=True)
idx = index_config(lines)
chains, scalars = {}, {}
info = idx.get('retry')
if info:
m = KEY_LINE.match(lines[info['line']])
vclean, _ = split_comment(m.group(3) or '') if m else ('', '')
vclean = vclean.strip()
if vclean:
# retry: {…} 单行 flow map / JSON inline map
r = _retry_flow_parse(m)
if r is None:
return {"error": "config.yml 的 retry 含不支持的 YAML 结构,"
"请手工编辑"}
_, chains, scalars = r
else:
reg = _retry_region(idx, lines)
if reg is None:
return {"error": "无法定位 retry 块"}
_, _, rend = reg
fc = idx.get('retry.fallbackChains')
if fc and fc['line'] < rend:
fm = KEY_LINE.match(lines[fc['line']])
fval, _ = split_comment(fm.group(3) or '') if fm else ('', '')
fval = fval.strip()
if not fval:
fci = len(lines[fc['line']]) - \
len(lines[fc['line']].lstrip(' '))
_, entries = _fc_children(lines, fc['line'], fci)
if entries is None:
return {"error": "config.yml 的 fallbackChains 含不支持的"
" YAML 结构,请手工编辑"}
chains = {k: e['chain'] for k, e in entries.items()}
else:
c = _flow_chains(fval)
if c is None:
return {"error": "config.yml 的 fallbackChains 含不支持"
"的 YAML 结构,请手工编辑"}
chains = c
for s in FALLBACK_SCALARS:
si = idx.get(f'retry.{s}')
if si and si['kind'] == 'scalar' and si['line'] < rend:
sm = KEY_LINE.match(lines[si['line']])
if sm:
scalars[s] = _yaml_text(sm.group(3) or '')
return {"chains": chains,
"modelFallback": _scalar_bool(
scalars.get("modelFallback", FALLBACK_DEFAULTS["modelFallback"])),
"fallbackOn": _scalar_choice(
scalars.get("fallbackOn"),
("any", "usage-limit", "except-usage-limit"),
FALLBACK_DEFAULTS["fallbackOn"]),
"fallbackRevertPolicy": _scalar_choice(
scalars.get("fallbackRevertPolicy"),
("cooldown-expiry", "never"),
FALLBACK_DEFAULTS["fallbackRevertPolicy"])}
def set_fallback_chain(key, chain):
with _config_lock:
return _set_fallback_chain(key, chain)
def _set_fallback_chain(key, chain):
"""写入/删除 retry.fallbackChains 下的一个键。
chain=None 删除该键(表示未配置),[] 显式禁用。只改这一个键。"""
key = (key or '').strip() if isinstance(key, str) else ''
if not CHAIN_KEY.fullmatch(key):
return "非法链键名(允许 provider/model-id、provider/* 或角色名)"
if chain is not None:
if not isinstance(chain, list) or \
not all(isinstance(s, str) for s in chain):
return "chain 必须是 selector 数组、null 或 []"
for s in chain:
if not CHAIN_SELECTOR.fullmatch(s.strip()):
return (f"非法 selector: {s!r}(须为 provider/model-id,"
f"可带 thinking 后缀)")
try:
raw = CONFIG.read_bytes().decode('utf-8')
except OSError as e:
return f"无法读取 {CONFIG}: {e}"
eol = '\r\n' if '\r\n' in raw else '\n'
lines = raw.splitlines(keepends=True)
body = None
if chain is not None:
items = ', '.join(yaml_scalar(s.strip()) for s in chain)
body = f"{key}: [{items}]{eol}" # 不带缩进的键行,插入时加 findent
idx = index_config(lines)
info = idx.get('retry')
if info is None:
if chain is None:
return None # 未配置:无需写入
if lines and not lines[-1].endswith('\n'):
lines[-1] += eol
lines.extend([f"retry:{eol}", f" fallbackChains:{eol}",
f" {body}"])
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
m = KEY_LINE.match(lines[info['line']])
rval, _ = split_comment(m.group(3) or '') if m else ('', '')
rval = rval.strip()
if rval:
# retry 是单行值:flow map 可整体改写为块;非 {} 的裸标量拒绝
if rval and not rval.startswith('{'):
return "config.yml 的 retry 含不支持的 YAML 结构,请手工编辑"
r = _retry_flow_parse(m)
if r is None:
return "config.yml 的 retry flow map 含不支持的结构,请手工编辑"
fmap, chains, _scalars = r
if chain is None:
if key not in chains:
return None
del chains[key]
else:
chains[key] = [s.strip() for s in chain]
indent0 = m.group(1)
sub = ' ' * (len(indent0) + 2)
_, comment = split_comment(m.group(3) or '')
note = ' ' + comment if comment else ''
new = [f"{indent0}retry:{note}{eol}"]
if 'fallbackChains' not in fmap and chains:
fmap['fallbackChains'] = '{}'
for k, v in fmap.items():
if k == 'fallbackChains':
if chains:
new.append(f"{sub}fallbackChains:{eol}")
new.extend(
f"{sub} {ck}: [{', '.join(yaml_scalar(s) for s in cv)}]"
f"{eol}" for ck, cv in chains.items())
else:
new.append(f"{sub}{k}: {v}{eol}")
lines[info['line']:info['line'] + 1] = new
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
reg = _retry_region(idx, lines)
if reg is None:
return "无法定位 retry 块"
rline, rindent, rend = reg
cindent = ' ' * (rindent + 2) # retry 直接子键缩进
findent = ' ' * (rindent + 4) # fallbackChains 子键缩进
fc = idx.get('retry.fallbackChains')
if fc and fc['line'] >= rend:
fc = None
fc_kind = None # None | 'block' | 'inline' | 'refuse'
fc_entries = None
fc_inline = None
if fc:
fm = KEY_LINE.match(lines[fc['line']])
fval, fcmt = split_comment(fm.group(3) or '') if fm else ('', '')
fval = fval.strip()
fcmt = (' ' + fcmt) if fcmt else ''
if not fval:
fci = len(lines[fc['line']]) - len(lines[fc['line']].lstrip(' '))
_, fc_entries = _fc_children(lines, fc['line'], fci)
fc_kind = 'refuse' if fc_entries is None else 'block'
else:
c = _flow_chains(fval)
fc_kind = 'refuse' if c is None else 'inline'
fc_inline = c
if fc_kind == 'refuse':
return ("config.yml 的 fallbackChains 含不支持的 YAML 结构"
"(嵌套/混排/非标量值),请手工编辑")
ent = (fc_entries or {}).get(key) if fc_kind == 'block' else None
if chain is None:
if fc_kind == 'block':
if ent is None:
return None
preserved = [line for line in lines[ent['ks']:ent['ke']]
if line.lstrip().startswith('#')]
lines[ent['ks']:ent['ke']] = preserved
# 删空了整个 map → 连 fallbackChains 头一起去掉
if not any(k != key for k in fc_entries):
del lines[fc['line']]
elif fc_kind == 'inline':
if key not in fc_inline:
return None
del fc_inline[key]
fm = KEY_LINE.match(lines[fc['line']])
if fc_inline:
inner = ', '.join(
f"{yaml_scalar(ck)}: "
f"[{', '.join(yaml_scalar(s) for s in cv)}]"
for ck, cv in fc_inline.items())
lines[fc['line']] = \
f"{fm.group(1)}fallbackChains: {{{inner}}}{fcmt}{eol}"
else:
del lines[fc['line']]
else:
return None # 没有该键,也没有 fallbackChains
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
if ent is not None:
old = KEY_LINE.match(lines[ent['ks']])
_, comment = split_comment(old.group(3) or '')
note = ' ' + comment if comment else ''
replacement = f"{old.group(1)}{key}: [{items}]{note}{eol}"
preserved = [line for line in lines[ent['ks'] + 1:ent['ke']]
if line.lstrip().startswith('#')]
lines[ent['ks']:ent['ke']] = [replacement, *preserved]
elif fc_kind == 'block' and fc is not None:
pos = fc['line'] + 1
for e in fc_entries.values():
pos = max(pos, e['ke'])
lines[pos:pos] = [f"{findent}{body}"]
elif fc_kind == 'inline' and fc is not None:
fc_inline[key] = [s.strip() for s in chain]
fm = KEY_LINE.match(lines[fc['line']])
inner = ', '.join(
f"{yaml_scalar(ck)}: [{', '.join(yaml_scalar(s) for s in cv)}]"
for ck, cv in fc_inline.items())
lines[fc['line']] = \
f"{fm.group(1)}fallbackChains: {{{inner}}}{fcmt}{eol}"
elif fc is not None:
# 不应到达(refuse 已提前返回)
return "config.yml 的 fallbackChains 结构不可写"
else:
lines[rline + 1:rline + 1] = [f"{cindent}fallbackChains:{eol}",
f"{findent}{body}"]
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
# ---------- Ctrl+P 轮换顺序(cycleOrder) ----------
CYCLE_DEFAULT = ["smol", "default", "slow"]
CYCLE_HEAD = re.compile(r'^cycleOrder:(?:[ \t]+([^\r\n]*?))?[ \t]*(\r?\n)?$')
CYCLE_ROLE = re.compile(r'[A-Za-z0-9_][A-Za-z0-9_.\-]*')
def _cycle_block(lines):
"""定位顶层 cycleOrder:返回 (首行, 尾后行, 条目, 首行注释, 列表内注释行);
不存在返回 None;结构不支持时条目为 None。"""
for i, line in enumerate(lines):
m = CYCLE_HEAD.match(line)
if not m:
continue
value, comment = split_comment(m.group(1) or '')
value = value.strip()
if value:
arr, ok = _parse_flow_seq(value)
if not ok or arr is None or any(':' in v for v in arr):
return i, i + 1, None, comment, []
return i, i + 1, arr, comment, []
entries, notes, end, j = [], [], i + 1, i + 1
while j < len(lines):
ln = lines[j]
if not ln.strip() or ln.lstrip().startswith('#'):
j += 1
continue
if not ln[0].isspace():
break
item = LIST_ITEM.match(ln)
raw, _ = split_comment(item.group(1)) if item else ('', '')
if item is None or raw.startswith(('{', '[')) or \
(':' in raw and raw[:1] not in '"\''):
return i, i + 1, None, comment, []
notes.extend(x for x in lines[end:j] if x.lstrip().startswith('#'))
entries.append(_unquote(raw))
j += 1
end = j
return i, end, entries, comment, notes
return None
def cycle_order_state():
"""读 cycleOrder;未配置时返回 omp 默认顺序。"""
try:
lines = CONFIG.read_bytes().decode('utf-8').splitlines(keepends=True)
except OSError as e:
return {"error": f"无法读取 {CONFIG}: {e}"}
blk = _cycle_block(lines)
if blk and blk[2] is None:
return {"error": "config.yml 的 cycleOrder 含不支持的 YAML 结构,请手工编辑"}
if not blk:
return {"order": list(CYCLE_DEFAULT), "custom": False,
"default": list(CYCLE_DEFAULT)}
head = CYCLE_HEAD.match(lines[blk[0]])
inline = bool(head and split_comment(head.group(1) or '')[0].strip())
custom = inline or bool(blk[2]) # 仅写 `cycleOrder:` 且无条目 = YAML null,按默认
order = list(blk[2] or []) if custom else list(CYCLE_DEFAULT)
return {"order": order, "custom": custom, "default": list(CYCLE_DEFAULT)}
def set_cycle_order(order):
with _config_lock:
return _set_cycle_order(order)
def _set_cycle_order(order):
"""order=None 删除 cycleOrder(恢复 omp 默认);否则写入有序角色名列表。"""
if order is not None:
if not isinstance(order, list) or not order:
return "轮换列表至少需要一个角色;恢复默认请使用恢复默认"
if not all(isinstance(r, str) and CYCLE_ROLE.fullmatch(r) for r in order):
return "角色名只能包含字母、数字、下划线、点和连字符,不能带 @"
if len(set(order)) != len(order):
return "轮换列表中的角色不能重复"
try:
raw = CONFIG.read_bytes().decode('utf-8')
except OSError as e:
return f"无法读取 {CONFIG}: {e}"
eol = '\r\n' if '\r\n' in raw else '\n'
lines = raw.splitlines(keepends=True)
blk = _cycle_block(lines)
if blk and blk[2] is None:
return "config.yml 的 cycleOrder 含不支持的 YAML 结构,请手工编辑"
if order is None:
if not blk:
return None
del lines[blk[0]:blk[1]]
else:
note = ' ' + blk[3] if blk and blk[3] else ''
block = [f"cycleOrder:{note}{eol}"] + \
[f" - {yaml_scalar(r)}{eol}" for r in order]
if blk:
lines[blk[0]:blk[1]] = block + blk[4]
else:
if lines and not lines[-1].endswith('\n'):
lines[-1] += eol
lines.extend(block)
write_backup()
CONFIG.write_bytes(''.join(lines).encode('utf-8'))
return None
def list_settings():
out = run(["omp", "config", "list", "--json"])
try:
catalog = json.loads(out)
except json.JSONDecodeError:
return {"error": "omp config list --json 解析失败", "items": []}
raw = CONFIG.read_bytes().decode('utf-8')
idx = index_config(raw.splitlines(keepends=True))
try:
zh = json.loads((HERE / "zh.json").read_text(encoding='utf-8'))
except (OSError, json.JSONDecodeError):
zh = {}
groups = zh.get("__groups", {})
items = []
for key, meta in catalog.items():
typ = meta.get('type', 'string')
info = zh.get(key, {})
items.append({
"key": key,
"value": meta.get('value'),
"type": typ,
"desc": meta.get('description', ''),
"name": info.get('name'),
"zh": info.get('desc'),
"desc_en": info.get('desc_en'),
"options": info.get('options'),
"group": groups.get(key.split('.')[0], key.split('.')[0]),
"custom": key in idx,
"editable": typ in EDITABLE_TYPES,
})
items.sort(key=lambda x: (x["key"].split('.')[0], x["key"]))
return {"items": items, "groups": groups, "groupmap": zh.get("__groupmap", {}),
"groups_en": zh.get("__groups_en", {})}
# ---------- 用量(omp usage --json) ----------
def usage_report():
"""跑 `omp usage --json` 并透传报告;失败返回 {"error": ...}。
报告中 metadata.email 属本机账号信息,本地面板原样展示;
但凭据/token 从不进入该 JSON(omp 自己也不输出)。"""
try:
p = subprocess.run(
["omp", "usage", "--json", "--no-extensions"],
capture_output=True, text=True, shell=False,
encoding="utf-8", errors="replace", timeout=120, creationflags=NO_WINDOW)
except subprocess.TimeoutExpired:
return {"error": "omp usage 超时(120s)"}
except OSError as e:
return {"error": f"无法执行 omp: {e}"}
if p.returncode != 0 and not p.stdout.strip():
err = (p.stderr or "").strip().splitlines()
return {"error": err[0] if err else f"omp usage 退出码 {p.returncode}"}
try:
return json.loads(p.stdout)
except json.JSONDecodeError:
return {"error": "omp usage 输出不是合法 JSON"}
def invalidate_usage(provider):
"""跑 `omp usage invalidate`(可选 --provider)。provider 只允许
'all' 或已认证账号列表里的 provider id,白名单校验防注入。"""
args = ["omp", "usage", "invalidate", "--no-extensions"]
if provider not in (None, "", "all"):
provider = provider.strip().lower()
known = {str(r.get("provider", "")).lower()
for r in (usage_report().get("reports") or [])}
if provider not in known:
return {"error": f"未知供应商: {provider}"}
args += ["--provider", provider]
try:
p = subprocess.run(args, capture_output=True, text=True, shell=False,
encoding="utf-8", errors="replace", timeout=60,
creationflags=NO_WINDOW)
except subprocess.TimeoutExpired:
return {"error": "omp usage invalidate 超时(60s)"}
except OSError as e:
return {"error": f"无法执行 omp: {e}"}
if p.returncode != 0:
err = (p.stderr or "").strip().splitlines()
return {"error": err[0] if err else f"omp usage invalidate 退出码 {p.returncode}"}
return {"ok": True, "message": (p.stdout or "").strip()}
def usage_with_resettable():
"""usage_report() 外加 resettable 标记:仅 anthropic/openai-codex
且 resetCredits.availableCount>0 时可重置(不透 token)。"""
r = usage_report()
if "error" in r:
return r
for rep in r.get("reports") or []:
rc = rep.get("resetCredits")
rep["resettable"] = rep.get("provider") in REDEEM_PROVIDERS \
and isinstance(rc, dict)
return r
# ---------- 多账号:账号列表 + auth.accountPolicies ----------
# 账号身份来自 `omp usage accounts --json`(无 token),额度来自 `omp usage --json`;
# 从不读取 agent.db 的凭据字段。策略只托管 auth.accountPolicies 的已知块结构:
# accountPolicies:
# - provider: <id>
# account: