-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclawapi_helper.py
More file actions
142 lines (115 loc) · 3.88 KB
/
clawapi_helper.py
File metadata and controls
142 lines (115 loc) · 3.88 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
#!/usr/bin/env python3
"""
FreeClaw Helper - 对话式接口辅助函数
供 AI 助手在 QQ/飞书等环境中调用
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'lib'))
from config_manager import FreeClawConfigManager
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich import box
console = Console()
manager = FreeClawConfigManager()
def show_status():
"""显示配置状态"""
primary = manager.get_primary_model()
fallbacks = manager.get_fallbacks()
providers = manager.list_providers()
output = f"""**配置状态**
主模型:{primary}
Providers:{len(providers)} 个
Fallbacks:{len(fallbacks)} 个
Fallback 链:
"""
for i, fb in enumerate(fallbacks, 1):
output += f"{i}. {fb}\n"
return output
def show_providers():
"""显示所有 providers"""
providers = manager.list_providers()
output = "**API Providers**\n\n"
for p in providers:
protocol = p.get('protocol', 'openai-compatible')
output += f"• {p['name']} ({protocol}): {p['model_count']} 个模型, Key: {p['api_key']}\n"
return output
def show_channels():
"""显示所有 channels"""
channels = manager.list_channels()
output = "**通道配置**\n\n"
if channels:
for c in channels:
status = "✅ 已启用" if c['enabled'] else "❌ 已禁用"
output += f"• {c['name']} ({c['type']}): {status}\n"
else:
output += "(未配置通道)\n"
return output
def show_models(provider_name=None):
"""显示模型列表"""
models = manager.list_models(provider_name)
output = f"**模型列表**{' - ' + provider_name if provider_name else ''}\n\n"
for m in models:
output += f"• {m['full_id']}: {m['name']}\n"
return output
def add_provider_interactive(name, url, key, protocol='openai-compatible'):
"""添加 provider"""
try:
manager.add_provider(name, url, key, protocol=protocol)
return f"✅ Provider '{name}' 已添加 (协议: {protocol})"
except Exception as e:
return f"❌ 错误:{e}"
def set_primary_interactive(model_id):
"""设置主模型"""
try:
manager.set_primary_model(model_id)
return f"✅ 主模型已设置为 {model_id}"
except Exception as e:
return f"❌ 错误:{e}"
def add_channel_interactive(name, channel_type, token):
"""添加通道"""
try:
manager.add_channel(name, channel_type, {'token': token})
return f"✅ 通道 '{name}' 已添加"
except Exception as e:
return f"❌ 错误:{e}"
def toggle_channel_interactive(name):
"""切换通道状态"""
try:
new_state = manager.toggle_channel(name)
state_text = "启用" if new_state else "禁用"
return f"✅ 通道 '{name}' 已{state_text}"
except Exception as e:
return f"❌ 错误:{e}"
def show_protocols():
"""显示所有支持的协议"""
protocols = manager.list_protocols()
output = "**支持的协议**\n\n"
for p in protocols:
output += f"• {p['id']}: {p['name']}\n {p['description']}\n\n"
return output
def set_protocol_interactive(provider_name, protocol):
"""设置 provider 的协议"""
try:
manager.set_provider_protocol(provider_name, protocol)
return f"Provider '{provider_name}' 协议已设置为 {protocol}"
except Exception as e:
return f"错误:{e}"
__all__ = [
'show_status',
'show_providers',
'show_channels',
'show_models',
'show_protocols',
'add_provider_interactive',
'set_primary_interactive',
'add_channel_interactive',
'toggle_channel_interactive',
'set_protocol_interactive',
'manager'
]
if __name__ == "__main__":
print(show_status())
print("\n" + show_providers())
print("\n" + show_channels())