Files
hack/tools/scripts/recreate_alldeep_channels.py
T

94 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Delete the broken channels 539-561 and recreate with correct type/base_url.
Keys are sourced from the vault (NewAPI does not return raw channel keys)."""
import json, sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from newapi_client import (
get_channel, delete_channel, add_channel, list_channels, test_channel,
)
ROOT = Path(__file__).resolve().parent
H = ROOT / "llm-key-hunter"
VAULT = H / "usable_keys"
# prefix -> (vault_dir, type, base_url, models)
SPEC = {
"ZhipuCoding": ("zhipu", 8, "https://open.bigmodel.cn/api/coding/paas/v4/chat/completions",
"glm-4.5-flash,glm-4.6,glm-4.7,glm-4.7-flash,glm-5.2,glm-5"),
"ZhipuPaaS": ("zhipu", 8, "https://open.bigmodel.cn/api/paas/v4/chat/completions",
"glm-4-flash,glm-4.5-flash,glm-4-air,glm-4.6"),
"Volcano": ("volcanoark", 1, "https://ark.cn-beijing.volces.com/api/v3",
"doubao-seed-1-6-flash-250615,doubao-seed-1-6-250615,"
"doubao-1-5-lite-32k-250115,doubao-1-5-pro-32k-250115"),
"LongCat": ("longcat", 1, "https://api.longcat.chat/openai", "LongCat-2.0"),
}
def load_keys(vdir):
p = VAULT / vdir / "keys.json"
return [e["key"] for e in json.loads(p.read_text()) if e.get("key")]
def main():
# 1) enumerate broken channels (539-561)
targets = []
for cid in range(539, 562):
ch = get_channel(cid)
if not ch or not ch.get("name"):
continue
name = ch["name"]
prefix = name.split("_")[0]
if prefix in SPEC:
targets.append((cid, name, prefix))
# 2) delete them all first
for cid, name, prefix in targets:
ok, msg = delete_channel(cid)
print(f" deleted {cid} {name}: {ok}")
time.sleep(0.15)
time.sleep(1)
# 3) recreate, pulling keys from vault.
# Channels were named PREFIX_<last6>. Match vault keys by last6.
live = 0
for cid, name, prefix in targets:
vdir, ctype, base, models = SPEC[prefix]
suffix = name.rsplit("_", 1)[-1]
keys = load_keys(vdir)
key = next((k for k in keys if k[-6:] == suffix), None)
if not key:
print(f" {name}: no vault key matching suffix {suffix}")
continue
channel = {
"name": name, "type": ctype, "key": key,
"base_url": base, "models": models,
"groups": ["default"], "model_mapping": "",
"priority": 0, "weight": 0, "auto_ban": 1,
}
ok, msg = add_channel(channel)
if not ok:
print(f" {name}: ADD FAIL {msg}")
continue
new_id = None
for ch in list_channels():
if ch.get("name") == name:
new_id = ch["id"]; break
if new_id is None:
for nid in range(562, 650):
d = get_channel(nid)
if d and d.get("name") == name:
new_id = nid; break
time.sleep(0.4)
t_ok, t_msg, _ = test_channel(new_id) if new_id else (False, "no id", 0)
mark = "✅" if t_ok else "⚠"
print(f" {mark} {name:24} id={new_id} {t_msg[:70]}")
if t_ok:
live += 1
time.sleep(0.3)
print(f"\n=== {live}/{len(targets)} live ===")
if __name__ == "__main__":
main()