@dedenlabs/claude-code-router-cli 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +753 -655
- package/dist/cli.js +10039 -183
- package/dist/index.html +464 -0
- package/examples/README-External-Rules.md +32 -2
- package/examples/README-/350/260/203/350/257/225/346/227/245/345/277/227/350/204/232/346/234/254.md +386 -0
- package/examples/config-with-debug-logger.json +111 -0
- package/examples/configs/fixed-router-config.json +3 -4
- package/examples/configs/unified-router-example.json +165 -171
- package/examples/external-rules/debug-logger.js +254 -0
- package/examples/external-rules-example.js +6 -6
- package/examples/image/README/1765879508612.png +0 -0
- package/examples/image/README-/350/260/203/350/257/225/346/227/245/345/277/227/350/204/232/346/234/254/1765945871911.png +0 -0
- package/package.json +76 -64
- package/examples/external-rules/user-preference.js +0 -70
|
@@ -1,171 +1,165 @@
|
|
|
1
|
-
{
|
|
2
|
-
"LOG": true,
|
|
3
|
-
"HOST": "127.0.0.1",
|
|
4
|
-
"PORT":
|
|
5
|
-
"APIKEY": "1",
|
|
6
|
-
"API_TIMEOUT_MS": 600000,
|
|
7
|
-
"Providers": [
|
|
8
|
-
{
|
|
9
|
-
"name": "openrouter",
|
|
10
|
-
"api_base_url": "https://openrouter.ai/api/v1/chat/completions",
|
|
11
|
-
"api_key": "sk-or-v1-",
|
|
12
|
-
"models": [
|
|
13
|
-
"anthropic/claude-3.5-sonnet",
|
|
14
|
-
"anthropic/claude-3.7-sonnet:thinking",
|
|
15
|
-
"deepseek/deepseek-chat-v3-0324",
|
|
16
|
-
"@preset/kimi"
|
|
17
|
-
],
|
|
18
|
-
"transformer": {
|
|
19
|
-
"use": ["openrouter"],
|
|
20
|
-
"deepseek/deepseek-chat-v3-0324": {
|
|
21
|
-
"use": ["tooluse"]
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
"name": "deepseek",
|
|
27
|
-
"api_base_url": "https://api.deepseek.com/chat/completions",
|
|
28
|
-
"api_key": "sk-",
|
|
29
|
-
"models": [
|
|
30
|
-
|
|
31
|
-
"deepseek
|
|
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
|
-
"default": 1000,
|
|
167
|
-
"longContext": 60000
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
"NON_INTERACTIVE_MODE": false
|
|
171
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"LOG": true,
|
|
3
|
+
"HOST": "127.0.0.1",
|
|
4
|
+
"PORT": 3456,
|
|
5
|
+
"APIKEY": "1",
|
|
6
|
+
"API_TIMEOUT_MS": 600000,
|
|
7
|
+
"Providers": [
|
|
8
|
+
{
|
|
9
|
+
"name": "openrouter",
|
|
10
|
+
"api_base_url": "https://openrouter.ai/api/v1/chat/completions",
|
|
11
|
+
"api_key": "sk-or-v1-",
|
|
12
|
+
"models": [
|
|
13
|
+
"anthropic/claude-3.5-sonnet",
|
|
14
|
+
"anthropic/claude-3.7-sonnet:thinking",
|
|
15
|
+
"deepseek/deepseek-chat-v3-0324",
|
|
16
|
+
"@preset/kimi"
|
|
17
|
+
],
|
|
18
|
+
"transformer": {
|
|
19
|
+
"use": ["openrouter"],
|
|
20
|
+
"deepseek/deepseek-chat-v3-0324": {
|
|
21
|
+
"use": ["tooluse"]
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"name": "deepseek",
|
|
27
|
+
"api_base_url": "https://api.deepseek.com/chat/completions",
|
|
28
|
+
"api_key": "sk-",
|
|
29
|
+
"models": ["deepseek-chat", "deepseek-reasoner"],
|
|
30
|
+
"transformer": {
|
|
31
|
+
"use": ["deepseek"],
|
|
32
|
+
"deepseek-chat": {
|
|
33
|
+
"use": ["tooluse"]
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"name": "gemini",
|
|
39
|
+
"api_base_url": "https://generativelanguage.googleapis.com/v1beta/models/",
|
|
40
|
+
"api_key": "",
|
|
41
|
+
"models": ["gemini-2.5-flash", "gemini-2.5-pro"],
|
|
42
|
+
"transformer": {
|
|
43
|
+
"use": ["gemini"]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"Router": {
|
|
48
|
+
"engine": "unified",
|
|
49
|
+
"defaultRoute": "openrouter,anthropic/claude-3.5-sonnet",
|
|
50
|
+
"rules": [
|
|
51
|
+
{
|
|
52
|
+
"name": "longContext",
|
|
53
|
+
"priority": 100,
|
|
54
|
+
"enabled": true,
|
|
55
|
+
"condition": {
|
|
56
|
+
"type": "tokenThreshold",
|
|
57
|
+
"value": 60000,
|
|
58
|
+
"operator": "gt"
|
|
59
|
+
},
|
|
60
|
+
"action": {
|
|
61
|
+
"route": "gemini,gemini-2.5-pro",
|
|
62
|
+
"transformers": []
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"name": "subagent",
|
|
67
|
+
"priority": 90,
|
|
68
|
+
"enabled": true,
|
|
69
|
+
"condition": {
|
|
70
|
+
"type": "fieldExists",
|
|
71
|
+
"field": "system.1.text",
|
|
72
|
+
"operator": "exists"
|
|
73
|
+
},
|
|
74
|
+
"action": {
|
|
75
|
+
"route": "${subagent}",
|
|
76
|
+
"transformers": []
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"name": "background",
|
|
81
|
+
"priority": 80,
|
|
82
|
+
"enabled": true,
|
|
83
|
+
"condition": {
|
|
84
|
+
"type": "modelContains",
|
|
85
|
+
"value": "haiku",
|
|
86
|
+
"operator": "contains"
|
|
87
|
+
},
|
|
88
|
+
"action": {
|
|
89
|
+
"route": "openrouter,@preset/kimi",
|
|
90
|
+
"transformers": []
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
"name": "webSearch",
|
|
95
|
+
"priority": 70,
|
|
96
|
+
"enabled": true,
|
|
97
|
+
"condition": {
|
|
98
|
+
"type": "toolExists",
|
|
99
|
+
"value": "web_search",
|
|
100
|
+
"operator": "exists"
|
|
101
|
+
},
|
|
102
|
+
"action": {
|
|
103
|
+
"route": "gemini,gemini-2.5-flash",
|
|
104
|
+
"transformers": []
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"name": "thinking",
|
|
109
|
+
"priority": 60,
|
|
110
|
+
"enabled": true,
|
|
111
|
+
"condition": {
|
|
112
|
+
"type": "fieldExists",
|
|
113
|
+
"field": "thinking",
|
|
114
|
+
"operator": "exists"
|
|
115
|
+
},
|
|
116
|
+
"action": {
|
|
117
|
+
"route": "deepseek,deepseek-reasoner",
|
|
118
|
+
"transformers": []
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"name": "directMapping",
|
|
123
|
+
"priority": 50,
|
|
124
|
+
"enabled": true,
|
|
125
|
+
"condition": {
|
|
126
|
+
"type": "custom",
|
|
127
|
+
"customFunction": "directModelMapping"
|
|
128
|
+
},
|
|
129
|
+
"action": {
|
|
130
|
+
"route": "${mappedModel}",
|
|
131
|
+
"transformers": []
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"name": "userSpecified",
|
|
136
|
+
"priority": 40,
|
|
137
|
+
"enabled": true,
|
|
138
|
+
"condition": {
|
|
139
|
+
"type": "custom",
|
|
140
|
+
"customFunction": "modelContainsComma"
|
|
141
|
+
},
|
|
142
|
+
"action": {
|
|
143
|
+
"route": "${userModel}",
|
|
144
|
+
"transformers": []
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
"cache": {
|
|
149
|
+
"enabled": true,
|
|
150
|
+
"maxSize": 1000,
|
|
151
|
+
"ttl": 300000
|
|
152
|
+
},
|
|
153
|
+
"debug": {
|
|
154
|
+
"enabled": false,
|
|
155
|
+
"logLevel": "info",
|
|
156
|
+
"logToFile": true,
|
|
157
|
+
"logToConsole": true
|
|
158
|
+
},
|
|
159
|
+
"contextThreshold": {
|
|
160
|
+
"default": 1000,
|
|
161
|
+
"longContext": 60000
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
"NON_INTERACTIVE_MODE": false
|
|
165
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 调试日志外部路由脚本
|
|
3
|
+
*
|
|
4
|
+
* 专门用于打印模型请求数据的外部路由函数
|
|
5
|
+
* 优先级最高,但不拦截任何请求(始终返回false)
|
|
6
|
+
*
|
|
7
|
+
* 特性:
|
|
8
|
+
* - 变量式日志收集:每种日志类型单独存储
|
|
9
|
+
* - 选择性输出:用户可指定要输出的日志类型
|
|
10
|
+
* - 文件存储:增量写入到 ~/.claude-code-router/logs/
|
|
11
|
+
* - 时间戳:每个日志条目带时间标记
|
|
12
|
+
* - 折叠友好:支持编辑器折叠功能
|
|
13
|
+
*
|
|
14
|
+
* 使用方式:
|
|
15
|
+
* 1. 在配置中添加此外部规则
|
|
16
|
+
* 2. 设置最高优先级(999)
|
|
17
|
+
* 3. 该函数会收集所有请求数据但不拦截
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const fs = require('fs');
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const util = require('util');
|
|
23
|
+
const os = require('node:os');
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 打印详细的模型请求数据
|
|
29
|
+
* @param {RouteContext} context - 路由上下文,包含完整的请求信息
|
|
30
|
+
* @returns {boolean} - 始终返回false,不拦截路由
|
|
31
|
+
*/
|
|
32
|
+
function printModelRequestData(context) {
|
|
33
|
+
// ========== 收集日志 ==========
|
|
34
|
+
// 修改下面的数组来选择要输出的日志类型
|
|
35
|
+
// 可选值: 'basic', 'headers', 'messages', 'system', 'tools', 'body', 'usage', 'event'
|
|
36
|
+
const outputTypes = [
|
|
37
|
+
'body',
|
|
38
|
+
// 'basic',
|
|
39
|
+
// 'headers',
|
|
40
|
+
// 'messages',
|
|
41
|
+
// 'system',
|
|
42
|
+
// 'tools',
|
|
43
|
+
// 'usage',
|
|
44
|
+
// 'event'
|
|
45
|
+
];
|
|
46
|
+
// 是否写入到日志文件 `~/.claude-code-router/logs/*.log`
|
|
47
|
+
const logToFile = true;
|
|
48
|
+
// 是否输出到控制台
|
|
49
|
+
const logToConsole = false;
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
// ========== 直接收集日志 ==========
|
|
53
|
+
// 保留变量名,但将逻辑直接写在这里,方便修改和检查
|
|
54
|
+
|
|
55
|
+
// 基本信息
|
|
56
|
+
let basicInfo = '';
|
|
57
|
+
basicInfo += '📊 【基本信息】\n';
|
|
58
|
+
basicInfo += ` Token 数量: ${context.tokenCount}\n`;
|
|
59
|
+
basicInfo += ` 会话ID: ${context.sessionId || 'N/A'}\n`;
|
|
60
|
+
|
|
61
|
+
// 请求头信息
|
|
62
|
+
let headersMsg = '';
|
|
63
|
+
if (context.req?.headers) {
|
|
64
|
+
headersMsg += '📋 【请求头】\n';
|
|
65
|
+
const headers = context.req.headers;
|
|
66
|
+
Object.keys(headers).forEach(key => {
|
|
67
|
+
headersMsg += ` ${key}: ${headers[key]}\n`;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 消息内容
|
|
72
|
+
let messages = '';
|
|
73
|
+
if (context.messages && context.messages.length > 0) {
|
|
74
|
+
messages += `💬 【消息内容】(共 ${context.messages.length} 条)\n`;
|
|
75
|
+
|
|
76
|
+
context.messages.forEach((message, index) => {
|
|
77
|
+
messages += `\n [消息 ${index + 1}]\n`;
|
|
78
|
+
messages += ` Role: ${message.role}\n`;
|
|
79
|
+
|
|
80
|
+
// 处理内容
|
|
81
|
+
if (message.content) {
|
|
82
|
+
if (typeof message.content === 'string') {
|
|
83
|
+
const preview = message.content.substring(0, 200);
|
|
84
|
+
messages += ` Content: ${preview}${message.content.length > 200 ? '...' : ''}\n`;
|
|
85
|
+
} else if (Array.isArray(message.content)) {
|
|
86
|
+
messages += ` Content (多部分):\n`;
|
|
87
|
+
message.content.forEach((part, partIndex) => {
|
|
88
|
+
messages += ` [${partIndex}] Type: ${part.type}\n`;
|
|
89
|
+
if (part.type === 'text') {
|
|
90
|
+
const preview = part.text?.substring(0, 100) || '';
|
|
91
|
+
messages += ` Text: ${preview}${part.text?.length > 100 ? '...' : ''}\n`;
|
|
92
|
+
} else if (part.type === 'image_url') {
|
|
93
|
+
messages += ` Image URL: ${part.image_url?.url || 'N/A'}\n`;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 处理工具调用
|
|
100
|
+
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
101
|
+
messages += ` Tool Calls (${message.tool_calls.length} 个):\n`;
|
|
102
|
+
message.tool_calls.forEach((toolCall, toolIndex) => {
|
|
103
|
+
messages += ` [${toolIndex}] ${toolCall.name}(${toolCall.id})\n`;
|
|
104
|
+
messages += ` Args: ${JSON.stringify(toolCall.arguments, null, 2)}\n`;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 系统消息
|
|
111
|
+
let systemMessages = '';
|
|
112
|
+
if (context.system && context.system.length > 0) {
|
|
113
|
+
systemMessages += `⚙️ 【系统消息】(共 ${context.system.length} 条)\n`;
|
|
114
|
+
|
|
115
|
+
context.system.forEach((sysMsg, index) => {
|
|
116
|
+
systemMessages += `\n [系统消息 ${index + 1}]\n`;
|
|
117
|
+
if (sysMsg.content) {
|
|
118
|
+
const preview = sysMsg.content.substring(0, 300);
|
|
119
|
+
systemMessages += ` Content: ${preview}${sysMsg.content.length > 300 ? '...' : ''}\n`;
|
|
120
|
+
}
|
|
121
|
+
if (sysMsg.type) {
|
|
122
|
+
systemMessages += ` Type: ${sysMsg.type}\n`;
|
|
123
|
+
}
|
|
124
|
+
if (sysMsg.name) {
|
|
125
|
+
systemMessages += ` Name: ${sysMsg.name}\n`;
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 可用工具
|
|
131
|
+
let tools = '';
|
|
132
|
+
if (context.tools && context.tools.length > 0) {
|
|
133
|
+
tools += `🔧 【可用工具】(共 ${context.tools.length} 个)\n`;
|
|
134
|
+
|
|
135
|
+
context.tools.forEach((tool, index) => {
|
|
136
|
+
tools += `\n [${index + 1}] ${tool.name}\n`;
|
|
137
|
+
tools += ` Description: ${tool.description || 'N/A'}\n`;
|
|
138
|
+
if (tool.input_schema) {
|
|
139
|
+
tools += ` Input Schema: ${JSON.stringify(tool.input_schema, null, 2)}\n`;
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 请求体原始数据
|
|
145
|
+
let requestBody = '';
|
|
146
|
+
if (context.req) {
|
|
147
|
+
requestBody += '📦 【请求体原始数据】\n';
|
|
148
|
+
|
|
149
|
+
// 安全地打印请求信息,避免循环引用和修改原始数据
|
|
150
|
+
const safeReq = {
|
|
151
|
+
method: context.req.method,
|
|
152
|
+
url: context.req.url,
|
|
153
|
+
headers: context.req.headers,
|
|
154
|
+
body: context.req.body,
|
|
155
|
+
httpVersion: context.req.httpVersion,
|
|
156
|
+
socket: context.req.socket ? '[Socket Object]' : undefined
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// 创建 body 的深拷贝,避免修改原始数据
|
|
160
|
+
if (safeReq.body) {
|
|
161
|
+
safeReq.body = { ...safeReq.body };
|
|
162
|
+
// 清理可能过大的字段(仅在副本上操作)
|
|
163
|
+
if (safeReq.body.messages) {
|
|
164
|
+
safeReq.body.messages = `[包含 ${safeReq.body.messages.length} 条消息的数组]`;
|
|
165
|
+
}
|
|
166
|
+
if (safeReq.body.system) {
|
|
167
|
+
safeReq.body.system = `[包含 ${safeReq.body.system.length} 条系统消息的数组]`;
|
|
168
|
+
}
|
|
169
|
+
if (safeReq.body.tools) {
|
|
170
|
+
safeReq.body.tools = `[包含 ${safeReq.body.tools.length} 个工具定义的数组]`;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
requestBody += util.inspect(safeReq, { depth: null, colors: false, breakLength: Infinity }) + '\n';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 使用统计
|
|
178
|
+
let usageStats = '';
|
|
179
|
+
if (context.lastUsage) {
|
|
180
|
+
usageStats += '📈 【使用统计】\n';
|
|
181
|
+
usageStats += ` 输入 tokens: ${context.lastUsage.input_tokens || 0}\n`;
|
|
182
|
+
usageStats += ` 输出 tokens: ${context.lastUsage.output_tokens || 0}\n`;
|
|
183
|
+
usageStats += ` 总 tokens: ${context.lastUsage.total_tokens || 0}\n`;
|
|
184
|
+
if (context.lastUsage.cost) {
|
|
185
|
+
usageStats += ` 估算成本: $${context.lastUsage.cost}\n`;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 事件信息
|
|
190
|
+
let eventInfo = '';
|
|
191
|
+
if (context.event) {
|
|
192
|
+
eventInfo += '🎯 【事件信息】\n';
|
|
193
|
+
eventInfo += util.inspect(context.event, { depth: 2, colors: false }) + '\n';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// ========== 生成完整日志 ==========
|
|
197
|
+
let fullLog = '\n========================================\n';
|
|
198
|
+
fullLog += '🔍 【调试日志】模型请求数据\n';
|
|
199
|
+
fullLog += '========================================\n\n';
|
|
200
|
+
|
|
201
|
+
// 根据选择输出日志
|
|
202
|
+
if (outputTypes.includes('basic')) fullLog += basicInfo + '\n';
|
|
203
|
+
if (outputTypes.includes('headers') && headersMsg) fullLog += headersMsg + '\n';
|
|
204
|
+
if (outputTypes.includes('messages') && messages) fullLog += messages + '\n';
|
|
205
|
+
if (outputTypes.includes('system') && systemMessages) fullLog += systemMessages + '\n';
|
|
206
|
+
if (outputTypes.includes('tools') && tools) fullLog += tools + '\n';
|
|
207
|
+
if (outputTypes.includes('body') && requestBody) fullLog += requestBody + '\n';
|
|
208
|
+
if (outputTypes.includes('usage') && usageStats) fullLog += usageStats + '\n';
|
|
209
|
+
if (outputTypes.includes('event') && eventInfo) fullLog += eventInfo + '\n';
|
|
210
|
+
|
|
211
|
+
fullLog += '========================================\n';
|
|
212
|
+
fullLog += '✅ 【调试日志】打印完成 - 路由继续执行\n';
|
|
213
|
+
fullLog += '========================================\n\n';
|
|
214
|
+
|
|
215
|
+
// ========== 输出方式 ==========
|
|
216
|
+
// 1. 输出到控制台
|
|
217
|
+
if (logToConsole) console.log(fullLog);
|
|
218
|
+
|
|
219
|
+
// 2. 写入到日志文件(增量追加)
|
|
220
|
+
if (logToFile) writeLogToFile(fullLog);
|
|
221
|
+
|
|
222
|
+
// 始终返回 false,不拦截任何路由
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 日志文件路径
|
|
228
|
+
*/
|
|
229
|
+
const getLogFilePath = () => {
|
|
230
|
+
const logDir = path.join(os.homedir(), '.claude-code-router', 'logs');
|
|
231
|
+
if (!fs.existsSync(logDir)) {
|
|
232
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
233
|
+
}
|
|
234
|
+
const timestamp = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
|
|
235
|
+
return path.join(logDir, `debug-logger-${timestamp}.log`);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 写入日志到文件(增量追加)
|
|
240
|
+
* @param {string} content - 日志内容
|
|
241
|
+
*/
|
|
242
|
+
function writeLogToFile(content) {
|
|
243
|
+
try {
|
|
244
|
+
const logFile = getLogFilePath();
|
|
245
|
+
const timestamp = new Date().toISOString();
|
|
246
|
+
const logEntry = `[${timestamp}] ${content}\n`;
|
|
247
|
+
fs.appendFileSync(logFile, logEntry, 'utf8');
|
|
248
|
+
} catch (error) {
|
|
249
|
+
console.error('❌ 写入日志文件失败:', error.message);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 导出函数
|
|
254
|
+
module.exports = { printModelRequestData };
|
|
@@ -4,19 +4,19 @@
|
|
|
4
4
|
* 演示如何使用 externalFunction 条件类型来加载外部 JS 规则
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
// 示例1
|
|
8
|
-
const
|
|
9
|
-
name: '
|
|
7
|
+
// 示例1:只打印模型请求的数据不进行匹配
|
|
8
|
+
const printModelRequestDataRule = {
|
|
9
|
+
name: 'printModelRequestData',
|
|
10
10
|
priority: 100,
|
|
11
11
|
condition: {
|
|
12
12
|
type: 'externalFunction',
|
|
13
13
|
externalFunction: {
|
|
14
|
-
path: './external-rules/
|
|
14
|
+
path: './external-rules/debug-logger.js',
|
|
15
15
|
functionName: 'checkUserPreference'
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
action: {
|
|
19
|
-
route: '
|
|
19
|
+
route: ''
|
|
20
20
|
}
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -54,7 +54,7 @@ const complexRoutingRule = {
|
|
|
54
54
|
|
|
55
55
|
// 将这些规则添加到配置中
|
|
56
56
|
export const externalRules = [
|
|
57
|
-
|
|
57
|
+
printModelRequestDataRule,
|
|
58
58
|
timeBasedRule,
|
|
59
59
|
complexRoutingRule
|
|
60
60
|
];
|
|
Binary file
|