@hileeon/mcc 0.1.0
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/.claude/CLAUDE.md +204 -0
- package/.claude/agents/.gitkeep +0 -0
- package/.claude/settings.json +9 -0
- package/.claude/skills/.gitkeep +0 -0
- package/README.md +127 -0
- package/dist/accounts/instance-manager.d.ts +11 -0
- package/dist/accounts/instance-manager.d.ts.map +1 -0
- package/dist/accounts/instance-manager.js +89 -0
- package/dist/accounts/instance-manager.js.map +1 -0
- package/dist/accounts/shared-manager.d.ts +25 -0
- package/dist/accounts/shared-manager.d.ts.map +1 -0
- package/dist/accounts/shared-manager.js +186 -0
- package/dist/accounts/shared-manager.js.map +1 -0
- package/dist/accounts/store.d.ts +30 -0
- package/dist/accounts/store.d.ts.map +1 -0
- package/dist/accounts/store.js +128 -0
- package/dist/accounts/store.js.map +1 -0
- package/dist/core/model-router.d.ts +30 -0
- package/dist/core/model-router.d.ts.map +1 -0
- package/dist/core/model-router.js +64 -0
- package/dist/core/model-router.js.map +1 -0
- package/dist/dashboard-server.d.ts +5 -0
- package/dist/dashboard-server.d.ts.map +1 -0
- package/dist/dashboard-server.js +387 -0
- package/dist/dashboard-server.js.map +1 -0
- package/dist/mcc.d.ts +8 -0
- package/dist/mcc.d.ts.map +1 -0
- package/dist/mcc.js +474 -0
- package/dist/mcc.js.map +1 -0
- package/dist/mcp/external-registry.d.ts +24 -0
- package/dist/mcp/external-registry.d.ts.map +1 -0
- package/dist/mcp/external-registry.js +99 -0
- package/dist/mcp/external-registry.js.map +1 -0
- package/dist/mcp/installer.d.ts +31 -0
- package/dist/mcp/installer.d.ts.map +1 -0
- package/dist/mcp/installer.js +273 -0
- package/dist/mcp/installer.js.map +1 -0
- package/dist/mcp/mcp-config.d.ts +86 -0
- package/dist/mcp/mcp-config.d.ts.map +1 -0
- package/dist/mcp/mcp-config.js +178 -0
- package/dist/mcp/mcp-config.js.map +1 -0
- package/dist/mcp/registry.d.ts +23 -0
- package/dist/mcp/registry.d.ts.map +1 -0
- package/dist/mcp/registry.js +100 -0
- package/dist/mcp/registry.js.map +1 -0
- package/dist/proxy/proxy-daemon.d.ts +27 -0
- package/dist/proxy/proxy-daemon.d.ts.map +1 -0
- package/dist/proxy/proxy-daemon.js +192 -0
- package/dist/proxy/proxy-daemon.js.map +1 -0
- package/dist/proxy/proxy-entry.d.ts +11 -0
- package/dist/proxy/proxy-entry.d.ts.map +1 -0
- package/dist/proxy/proxy-entry.js +74 -0
- package/dist/proxy/proxy-entry.js.map +1 -0
- package/dist/proxy/proxy-paths.d.ts +27 -0
- package/dist/proxy/proxy-paths.d.ts.map +1 -0
- package/dist/proxy/proxy-paths.js +125 -0
- package/dist/proxy/proxy-paths.js.map +1 -0
- package/dist/proxy/proxy-server.d.ts +20 -0
- package/dist/proxy/proxy-server.d.ts.map +1 -0
- package/dist/proxy/proxy-server.js +280 -0
- package/dist/proxy/proxy-server.js.map +1 -0
- package/dist/proxy/upstream-url.d.ts +7 -0
- package/dist/proxy/upstream-url.d.ts.map +1 -0
- package/dist/proxy/upstream-url.js +38 -0
- package/dist/proxy/upstream-url.js.map +1 -0
- package/dist/shared/logger.d.ts +23 -0
- package/dist/shared/logger.d.ts.map +1 -0
- package/dist/shared/logger.js +184 -0
- package/dist/shared/logger.js.map +1 -0
- package/dist/shared/provider-preset-catalog.d.ts +41 -0
- package/dist/shared/provider-preset-catalog.d.ts.map +1 -0
- package/dist/shared/provider-preset-catalog.js +299 -0
- package/dist/shared/provider-preset-catalog.js.map +1 -0
- package/docs/decisions.md +33 -0
- package/docs/lessons.md +8 -0
- package/docs/product.md +37 -0
- package/lib/mcp/mcc-image-analysis-server.cjs +454 -0
- package/lib/mcp/mcc-websearch-server.cjs +339 -0
- package/lib/mcp-hooks/image-analysis-runtime.cjs +510 -0
- package/lib/mcp-hooks/image-analyzer-transformer.cjs +526 -0
- package/lib/mcp-hooks/websearch-transformer.cjs +1421 -0
- package/lib/proxy/config/config-loader-facade.js +24 -0
- package/lib/proxy/glmt/delta-accumulator.js +363 -0
- package/lib/proxy/glmt/glmt-transformer.js +204 -0
- package/lib/proxy/glmt/index.js +41 -0
- package/lib/proxy/glmt/locale-enforcer.js +69 -0
- package/lib/proxy/glmt/pipeline/content-transformer.js +162 -0
- package/lib/proxy/glmt/pipeline/index.js +20 -0
- package/lib/proxy/glmt/pipeline/request-transformer.js +116 -0
- package/lib/proxy/glmt/pipeline/response-builder.js +205 -0
- package/lib/proxy/glmt/pipeline/stream-parser.js +234 -0
- package/lib/proxy/glmt/pipeline/tool-call-handler.js +78 -0
- package/lib/proxy/glmt/pipeline/types.js +6 -0
- package/lib/proxy/glmt/reasoning-enforcer.js +151 -0
- package/lib/proxy/glmt/sse-parser.js +102 -0
- package/lib/proxy/services/logging.js +13 -0
- package/lib/proxy/transformers/request-transformer.js +452 -0
- package/lib/proxy/transformers/sse-stream-transformer.js +199 -0
- package/lib/shared/logger.cjs +138 -0
- package/package.json +35 -0
- package/src/accounts/instance-manager.ts +58 -0
- package/src/accounts/shared-manager.ts +154 -0
- package/src/accounts/store.ts +111 -0
- package/src/core/model-router.ts +82 -0
- package/src/dashboard-server.ts +407 -0
- package/src/mcc.ts +474 -0
- package/src/mcp/external-registry.ts +73 -0
- package/src/mcp/installer.ts +258 -0
- package/src/mcp/mcp-config.ts +168 -0
- package/src/mcp/registry.ts +89 -0
- package/src/proxy/proxy-daemon.ts +184 -0
- package/src/proxy/proxy-entry.ts +63 -0
- package/src/proxy/proxy-paths.ts +97 -0
- package/src/proxy/proxy-server.ts +278 -0
- package/src/proxy/upstream-url.ts +38 -0
- package/src/shared/logger.ts +140 -0
- package/src/shared/provider-preset-catalog.ts +340 -0
- package/tsconfig.json +33 -0
- package/ui/.prettierrc +9 -0
- package/ui/index.html +12 -0
- package/ui/package.json +33 -0
- package/ui/postcss.config.js +6 -0
- package/ui/src/App.tsx +753 -0
- package/ui/src/components/ui/button.tsx +48 -0
- package/ui/src/components/ui/card.tsx +50 -0
- package/ui/src/components/ui/input.tsx +21 -0
- package/ui/src/components/ui/label.tsx +20 -0
- package/ui/src/components/ui/select.tsx +80 -0
- package/ui/src/components/ui/switch.tsx +26 -0
- package/ui/src/components/ui/tabs.tsx +52 -0
- package/ui/src/index.css +33 -0
- package/ui/src/lib/api.ts +185 -0
- package/ui/src/lib/utils.ts +6 -0
- package/ui/src/main.tsx +10 -0
- package/ui/src/vite-env.d.ts +1 -0
- package/ui/tailwind.config.js +49 -0
- package/ui/tsconfig.json +25 -0
- package/ui/vite.config.ts +20 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stub for CCS config/config-loader-facade
|
|
3
|
+
* Provides getCcsDir pointing to ~/.mcc instead of ~/.ccs
|
|
4
|
+
*/
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const os = require('os');
|
|
7
|
+
|
|
8
|
+
function getCcsDir() {
|
|
9
|
+
return process.env.MCC_HOME || path.join(os.homedir(), '.mcc');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function loadConfigSafe() {
|
|
13
|
+
return { profiles: {} };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function loadSettings(_settingsPath) {
|
|
17
|
+
return { env: {} };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function loadOrCreateUnifiedConfig() {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = { getCcsDir, loadConfigSafe, loadSettings, loadOrCreateUnifiedConfig };
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* DeltaAccumulator - Maintain state across streaming deltas
|
|
5
|
+
*
|
|
6
|
+
* Tracks:
|
|
7
|
+
* - Message metadata (id, model, role)
|
|
8
|
+
* - Content blocks (thinking, text)
|
|
9
|
+
* - Current block index
|
|
10
|
+
* - Accumulated content
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* const acc = new DeltaAccumulator(thinkingConfig);
|
|
14
|
+
* const events = transformer.transformDelta(openaiEvent, acc);
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.DeltaAccumulator = void 0;
|
|
18
|
+
class DeltaAccumulator {
|
|
19
|
+
constructor(_thinkingConfig = {}, options = {}) {
|
|
20
|
+
// ========== Finish Reason ==========
|
|
21
|
+
this.finishReason = null;
|
|
22
|
+
this.usageReceived = false;
|
|
23
|
+
this.messageId = 'msg_' + Date.now() + '_' + Math.random().toString(36).substring(7);
|
|
24
|
+
this.model = null;
|
|
25
|
+
this.role = 'assistant';
|
|
26
|
+
// Content blocks
|
|
27
|
+
this.contentBlocks = [];
|
|
28
|
+
this.currentBlockIndex = -1;
|
|
29
|
+
// Tool calls tracking
|
|
30
|
+
this.toolCalls = [];
|
|
31
|
+
this.toolCallsIndex = {};
|
|
32
|
+
// Buffers
|
|
33
|
+
this.thinkingBuffer = '';
|
|
34
|
+
this.textBuffer = '';
|
|
35
|
+
// C-02 Fix: Limits to prevent unbounded accumulation
|
|
36
|
+
this.maxBlocks = options.maxBlocks || 100;
|
|
37
|
+
this.maxBufferSize = options.maxBufferSize || 10 * 1024 * 1024; // 10MB
|
|
38
|
+
// Loop detection configuration
|
|
39
|
+
this.loopDetectionThreshold = options.loopDetectionThreshold || 3;
|
|
40
|
+
this.loopDetected = false;
|
|
41
|
+
// State flags
|
|
42
|
+
this.messageStarted = false;
|
|
43
|
+
this.finalized = false;
|
|
44
|
+
// Statistics
|
|
45
|
+
this.inputTokens = 0;
|
|
46
|
+
this.outputTokens = 0;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get current content block
|
|
50
|
+
* @returns Current block or null
|
|
51
|
+
*/
|
|
52
|
+
getCurrentBlock() {
|
|
53
|
+
if (this.currentBlockIndex >= 0 && this.currentBlockIndex < this.contentBlocks.length) {
|
|
54
|
+
return this.contentBlocks[this.currentBlockIndex];
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Start new content block
|
|
60
|
+
* @param type - Block type ('thinking', 'text', or 'tool_use')
|
|
61
|
+
* @returns New block
|
|
62
|
+
*/
|
|
63
|
+
startBlock(type) {
|
|
64
|
+
// C-02 Fix: Enforce max blocks limit
|
|
65
|
+
if (this.contentBlocks.length >= this.maxBlocks) {
|
|
66
|
+
throw new Error(`Maximum ${this.maxBlocks} content blocks exceeded (DoS protection)`);
|
|
67
|
+
}
|
|
68
|
+
this.currentBlockIndex++;
|
|
69
|
+
const block = {
|
|
70
|
+
index: this.currentBlockIndex,
|
|
71
|
+
type: type,
|
|
72
|
+
content: '',
|
|
73
|
+
started: true,
|
|
74
|
+
stopped: false,
|
|
75
|
+
};
|
|
76
|
+
this.contentBlocks.push(block);
|
|
77
|
+
// Reset buffer for new block (tool_use doesn't use buffers)
|
|
78
|
+
if (type === 'thinking') {
|
|
79
|
+
this.thinkingBuffer = '';
|
|
80
|
+
}
|
|
81
|
+
else if (type === 'text') {
|
|
82
|
+
this.textBuffer = '';
|
|
83
|
+
}
|
|
84
|
+
return block;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Add delta to current block
|
|
88
|
+
* @param delta - Content delta
|
|
89
|
+
*/
|
|
90
|
+
addDelta(delta) {
|
|
91
|
+
const block = this.getCurrentBlock();
|
|
92
|
+
if (!block) {
|
|
93
|
+
// FIX: Guard against null block (should never happen, but defensive)
|
|
94
|
+
console.error('[DeltaAccumulator] ERROR: addDelta called with no current block');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (block.type === 'thinking') {
|
|
98
|
+
// C-02 Fix: Enforce buffer size limit
|
|
99
|
+
if (this.thinkingBuffer.length + delta.length > this.maxBufferSize) {
|
|
100
|
+
throw new Error(`Thinking buffer exceeded ${this.maxBufferSize} bytes (DoS protection)`);
|
|
101
|
+
}
|
|
102
|
+
this.thinkingBuffer += delta;
|
|
103
|
+
block.content = this.thinkingBuffer;
|
|
104
|
+
// FIX: Verify assignment succeeded (paranoid check for race conditions)
|
|
105
|
+
if (block.content.length !== this.thinkingBuffer.length) {
|
|
106
|
+
console.error('[DeltaAccumulator] ERROR: Block content assignment failed');
|
|
107
|
+
console.error(`Expected: ${this.thinkingBuffer.length}, Got: ${block.content.length}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else if (block.type === 'text') {
|
|
111
|
+
// C-02 Fix: Enforce buffer size limit
|
|
112
|
+
if (this.textBuffer.length + delta.length > this.maxBufferSize) {
|
|
113
|
+
throw new Error(`Text buffer exceeded ${this.maxBufferSize} bytes (DoS protection)`);
|
|
114
|
+
}
|
|
115
|
+
this.textBuffer += delta;
|
|
116
|
+
block.content = this.textBuffer;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Mark current block as stopped
|
|
121
|
+
*/
|
|
122
|
+
stopCurrentBlock() {
|
|
123
|
+
const block = this.getCurrentBlock();
|
|
124
|
+
if (block) {
|
|
125
|
+
block.stopped = true;
|
|
126
|
+
// FIX: Log block closure for debugging (helps diagnose timing issues)
|
|
127
|
+
if (block.type === 'thinking' && process.env.CCS_DEBUG === '1') {
|
|
128
|
+
console.error(`[DeltaAccumulator] Stopped thinking block ${block.index}: ${block.content?.length || 0} chars`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Update usage statistics
|
|
134
|
+
* @param usage - Usage object from OpenAI
|
|
135
|
+
*/
|
|
136
|
+
updateUsage(usage) {
|
|
137
|
+
if (usage) {
|
|
138
|
+
this.inputTokens = usage.prompt_tokens || usage.input_tokens || 0;
|
|
139
|
+
this.outputTokens = usage.completion_tokens || usage.output_tokens || 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Add or update tool call delta
|
|
144
|
+
* @param toolCallDelta - Tool call delta from OpenAI
|
|
145
|
+
*/
|
|
146
|
+
addToolCallDelta(toolCallDelta) {
|
|
147
|
+
const index = toolCallDelta.index;
|
|
148
|
+
if (!this.toolCallsIndex[index]) {
|
|
149
|
+
const toolCall = {
|
|
150
|
+
index: index,
|
|
151
|
+
id: '',
|
|
152
|
+
type: 'function',
|
|
153
|
+
function: {
|
|
154
|
+
name: '',
|
|
155
|
+
arguments: '',
|
|
156
|
+
},
|
|
157
|
+
blockIndex: -1,
|
|
158
|
+
};
|
|
159
|
+
this.toolCalls.push(toolCall);
|
|
160
|
+
this.toolCallsIndex[index] = toolCall;
|
|
161
|
+
}
|
|
162
|
+
const toolCall = this.toolCallsIndex[index];
|
|
163
|
+
if (toolCallDelta.id) {
|
|
164
|
+
toolCall.id = toolCallDelta.id;
|
|
165
|
+
}
|
|
166
|
+
if (toolCallDelta.type) {
|
|
167
|
+
toolCall.type = toolCallDelta.type;
|
|
168
|
+
}
|
|
169
|
+
if (toolCallDelta.function?.name) {
|
|
170
|
+
toolCall.function.name += toolCallDelta.function.name;
|
|
171
|
+
}
|
|
172
|
+
if (toolCallDelta.function?.arguments) {
|
|
173
|
+
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
setToolCallBlockIndex(toolCallIndex, blockIndex) {
|
|
177
|
+
const toolCall = this.toolCallsIndex[toolCallIndex];
|
|
178
|
+
if (toolCall) {
|
|
179
|
+
toolCall.blockIndex = blockIndex;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
getToolCallBlockIndex(toolCallIndex) {
|
|
183
|
+
const toolCall = this.toolCallsIndex[toolCallIndex];
|
|
184
|
+
if (!toolCall || toolCall.blockIndex < 0) {
|
|
185
|
+
throw new Error(`Tool call ${toolCallIndex} does not have an assigned content block`);
|
|
186
|
+
}
|
|
187
|
+
return toolCall.blockIndex;
|
|
188
|
+
}
|
|
189
|
+
getUnstoppedBlocks() {
|
|
190
|
+
return this.contentBlocks.filter((b) => !b.stopped);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get all tool calls
|
|
194
|
+
* @returns Tool calls array
|
|
195
|
+
*/
|
|
196
|
+
getToolCalls() {
|
|
197
|
+
return this.toolCalls;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Check for planning loop pattern
|
|
201
|
+
* Loop = N consecutive thinking blocks with no tool calls
|
|
202
|
+
* @returns True if loop detected
|
|
203
|
+
*/
|
|
204
|
+
checkForLoop() {
|
|
205
|
+
// Already detected loop
|
|
206
|
+
if (this.loopDetected) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
// Need minimum blocks to detect pattern
|
|
210
|
+
if (this.contentBlocks.length < this.loopDetectionThreshold) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
// Get last N blocks
|
|
214
|
+
const recentBlocks = this.contentBlocks.slice(-this.loopDetectionThreshold);
|
|
215
|
+
// Check if all recent blocks are thinking blocks
|
|
216
|
+
const allThinking = recentBlocks.every((b) => b.type === 'thinking');
|
|
217
|
+
// Check if no tool calls have been made at all
|
|
218
|
+
const noToolCalls = this.toolCalls.length === 0;
|
|
219
|
+
// Loop detected if: all recent blocks are thinking AND no tool calls yet
|
|
220
|
+
if (allThinking && noToolCalls) {
|
|
221
|
+
this.loopDetected = true;
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Reset loop detection state (for testing)
|
|
228
|
+
*/
|
|
229
|
+
resetLoopDetection() {
|
|
230
|
+
this.loopDetected = false;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get summary of accumulated state
|
|
234
|
+
* @returns Summary
|
|
235
|
+
*/
|
|
236
|
+
getSummary() {
|
|
237
|
+
return {
|
|
238
|
+
messageId: this.messageId,
|
|
239
|
+
model: this.model,
|
|
240
|
+
role: this.role,
|
|
241
|
+
blockCount: this.contentBlocks.length,
|
|
242
|
+
currentIndex: this.currentBlockIndex,
|
|
243
|
+
toolCallCount: this.toolCalls.length,
|
|
244
|
+
messageStarted: this.messageStarted,
|
|
245
|
+
finalized: this.finalized,
|
|
246
|
+
loopDetected: this.loopDetected,
|
|
247
|
+
usage: {
|
|
248
|
+
input_tokens: this.inputTokens,
|
|
249
|
+
output_tokens: this.outputTokens,
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
// ========== State Getters ==========
|
|
254
|
+
/**
|
|
255
|
+
* Check if message has been finalized
|
|
256
|
+
*/
|
|
257
|
+
isFinalized() {
|
|
258
|
+
return this.finalized;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Check if message has started
|
|
262
|
+
*/
|
|
263
|
+
isMessageStarted() {
|
|
264
|
+
return this.messageStarted;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get message ID
|
|
268
|
+
*/
|
|
269
|
+
getMessageId() {
|
|
270
|
+
return this.messageId;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Get model name
|
|
274
|
+
*/
|
|
275
|
+
getModel() {
|
|
276
|
+
return this.model;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Get role
|
|
280
|
+
*/
|
|
281
|
+
getRole() {
|
|
282
|
+
return this.role;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get input tokens
|
|
286
|
+
*/
|
|
287
|
+
getInputTokens() {
|
|
288
|
+
return this.inputTokens;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Get output tokens
|
|
292
|
+
*/
|
|
293
|
+
getOutputTokens() {
|
|
294
|
+
return this.outputTokens;
|
|
295
|
+
}
|
|
296
|
+
// ========== State Setters ==========
|
|
297
|
+
/**
|
|
298
|
+
* Set model name
|
|
299
|
+
*/
|
|
300
|
+
setModel(model) {
|
|
301
|
+
this.model = model;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Set message started flag
|
|
305
|
+
*/
|
|
306
|
+
setMessageStarted(started) {
|
|
307
|
+
this.messageStarted = started;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Set role
|
|
311
|
+
*/
|
|
312
|
+
setRole(role) {
|
|
313
|
+
this.role = role;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Set finalized flag
|
|
317
|
+
*/
|
|
318
|
+
setFinalized(finalized) {
|
|
319
|
+
this.finalized = finalized;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Set finish reason
|
|
323
|
+
*/
|
|
324
|
+
setFinishReason(reason) {
|
|
325
|
+
this.finishReason = reason;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Get finish reason
|
|
329
|
+
*/
|
|
330
|
+
getFinishReason() {
|
|
331
|
+
return this.finishReason;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Check if usage stats have been received
|
|
335
|
+
*/
|
|
336
|
+
hasUsageReceived() {
|
|
337
|
+
return this.usageReceived;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Mark usage as received
|
|
341
|
+
*/
|
|
342
|
+
setUsageReceived(received) {
|
|
343
|
+
this.usageReceived = received;
|
|
344
|
+
}
|
|
345
|
+
// ========== Tool Call Helpers ==========
|
|
346
|
+
/**
|
|
347
|
+
* Check if there are any tool calls, or check if a specific index exists
|
|
348
|
+
*/
|
|
349
|
+
hasToolCall(index) {
|
|
350
|
+
if (index === undefined) {
|
|
351
|
+
return this.toolCalls.length > 0;
|
|
352
|
+
}
|
|
353
|
+
return this.toolCallsIndex[index] !== undefined;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Get tool call by index
|
|
357
|
+
*/
|
|
358
|
+
getToolCall(index) {
|
|
359
|
+
return this.toolCallsIndex[index];
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
exports.DeltaAccumulator = DeltaAccumulator;
|
|
363
|
+
//# sourceMappingURL=delta-accumulator.js.map
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GlmtTransformer - Orchestrator for Anthropic ↔ OpenAI format transformation
|
|
4
|
+
*
|
|
5
|
+
* Pipeline Architecture:
|
|
6
|
+
* - RequestTransformer: Anthropic → OpenAI request conversion
|
|
7
|
+
* - StreamParser: Delta processing for streaming responses
|
|
8
|
+
* - ResponseBuilder: SSE event generation
|
|
9
|
+
* - ToolCallHandler: Tool call processing
|
|
10
|
+
*/
|
|
11
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
14
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
15
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
16
|
+
}
|
|
17
|
+
Object.defineProperty(o, k2, desc);
|
|
18
|
+
}) : (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
o[k2] = m[k];
|
|
21
|
+
}));
|
|
22
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
23
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
24
|
+
}) : function(o, v) {
|
|
25
|
+
o["default"] = v;
|
|
26
|
+
});
|
|
27
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.GlmtTransformer = void 0;
|
|
36
|
+
const fs = __importStar(require("fs"));
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
const logging_1 = require("../services/logging");
|
|
39
|
+
const pipeline_1 = require("./pipeline");
|
|
40
|
+
const config_loader_facade_1 = require("../config/config-loader-facade");
|
|
41
|
+
class GlmtTransformer {
|
|
42
|
+
constructor(config = {}) {
|
|
43
|
+
this.logger = (0, logging_1.createLogger)('glmt:transformer');
|
|
44
|
+
this.verbose = config.verbose || false;
|
|
45
|
+
const debugEnabled = process.env.CCS_DEBUG === '1';
|
|
46
|
+
this.debugLog = config.debugLog ?? debugEnabled;
|
|
47
|
+
this.debugLogDir = config.debugLogDir || path.join((0, config_loader_facade_1.getCcsDir)(), 'logs');
|
|
48
|
+
// Initialize pipeline components
|
|
49
|
+
this.requestTransformer = new pipeline_1.RequestTransformer({
|
|
50
|
+
defaultThinking: config.defaultThinking ?? true,
|
|
51
|
+
verbose: this.verbose,
|
|
52
|
+
explicitReasoning: config.explicitReasoning ?? true,
|
|
53
|
+
log: (msg) => this.log(msg),
|
|
54
|
+
});
|
|
55
|
+
this.responseBuilder = new pipeline_1.ResponseBuilder(this.verbose);
|
|
56
|
+
this.toolCallHandler = new pipeline_1.ToolCallHandler();
|
|
57
|
+
this.contentTransformer = new pipeline_1.ContentTransformer(config.defaultThinking ?? true);
|
|
58
|
+
this.streamParser = new pipeline_1.StreamParser({
|
|
59
|
+
verbose: this.verbose,
|
|
60
|
+
debugMode: config.debugMode ?? debugEnabled,
|
|
61
|
+
debugLog: this.debugLog,
|
|
62
|
+
writeDebugLog: (type, data) => this.writeDebugLog(type, data),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/** Transform Anthropic request to OpenAI format */
|
|
66
|
+
transformRequest(anthropicRequest) {
|
|
67
|
+
this.writeDebugLog('request-anthropic', anthropicRequest);
|
|
68
|
+
const result = this.requestTransformer.transform(anthropicRequest);
|
|
69
|
+
this.writeDebugLog('request-openai', result.openaiRequest);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
/** Transform OpenAI response to Anthropic format */
|
|
73
|
+
transformResponse(openaiResponse, _thinkingConfig = { thinking: false, effort: 'medium' }) {
|
|
74
|
+
this.writeDebugLog('response-openai', openaiResponse);
|
|
75
|
+
try {
|
|
76
|
+
const choice = openaiResponse.choices?.[0];
|
|
77
|
+
if (!choice)
|
|
78
|
+
throw new Error('No choices in OpenAI response');
|
|
79
|
+
const message = choice.message;
|
|
80
|
+
const content = [];
|
|
81
|
+
if (message.reasoning_content) {
|
|
82
|
+
this.log(`Detected reasoning_content: ${message.reasoning_content.length} chars`);
|
|
83
|
+
content.push({
|
|
84
|
+
type: 'thinking',
|
|
85
|
+
thinking: message.reasoning_content,
|
|
86
|
+
signature: this.responseBuilder.generateThinkingSignature(message.reasoning_content),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (message.content) {
|
|
90
|
+
content.push({ type: 'text', text: message.content });
|
|
91
|
+
}
|
|
92
|
+
if (message.tool_calls?.length) {
|
|
93
|
+
content.push(...this.toolCallHandler.processToolCalls(message.tool_calls));
|
|
94
|
+
}
|
|
95
|
+
const anthropicResponse = {
|
|
96
|
+
id: openaiResponse.id || 'msg_' + Date.now(),
|
|
97
|
+
type: 'message',
|
|
98
|
+
role: 'assistant',
|
|
99
|
+
content,
|
|
100
|
+
model: openaiResponse.model || 'glm-5',
|
|
101
|
+
stop_reason: this.responseBuilder.mapStopReason(choice.finish_reason || 'stop'),
|
|
102
|
+
usage: {
|
|
103
|
+
input_tokens: openaiResponse.usage?.prompt_tokens || 0,
|
|
104
|
+
output_tokens: openaiResponse.usage?.completion_tokens || 0,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
this.writeDebugLog('response-anthropic', anthropicResponse);
|
|
108
|
+
return anthropicResponse;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
const err = error;
|
|
112
|
+
this.logger.stage('cleanup', 'response.transform_failed', 'GLMT response transformation failed', undefined, { level: 'error', error: { name: err.name, message: err.message } });
|
|
113
|
+
console.error('[glmt-transformer] Response transformation error:', err);
|
|
114
|
+
return {
|
|
115
|
+
id: 'msg_error_' + Date.now(),
|
|
116
|
+
type: 'message',
|
|
117
|
+
role: 'assistant',
|
|
118
|
+
content: [{ type: 'text', text: '[Transformation Error] ' + err.message }],
|
|
119
|
+
model: 'glm-5',
|
|
120
|
+
stop_reason: 'end_turn',
|
|
121
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Transform streaming delta (delegates to StreamParser) */
|
|
126
|
+
transformDelta(openaiEvent, accumulator) {
|
|
127
|
+
return this.streamParser.transformDelta(openaiEvent, accumulator);
|
|
128
|
+
}
|
|
129
|
+
/** Finalize streaming (delegates to StreamParser) */
|
|
130
|
+
finalizeDelta(accumulator) {
|
|
131
|
+
return this.streamParser.finalizeDelta(accumulator);
|
|
132
|
+
}
|
|
133
|
+
redactSensitiveData(data) {
|
|
134
|
+
if (data === null || data === undefined)
|
|
135
|
+
return data;
|
|
136
|
+
if (typeof data !== 'object')
|
|
137
|
+
return data;
|
|
138
|
+
if (Array.isArray(data))
|
|
139
|
+
return data.map((item) => this.redactSensitiveData(item));
|
|
140
|
+
const SENSITIVE_KEYS = /^(authorization|auth[_-]?token|api[_-]?key|apikey|token|secret|password|credential|x-api-key|anthropic-api-key|cookie)$/i;
|
|
141
|
+
const result = {};
|
|
142
|
+
for (const [key, value] of Object.entries(data)) {
|
|
143
|
+
if (SENSITIVE_KEYS.test(key)) {
|
|
144
|
+
result[key] = '[REDACTED]';
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
result[key] = this.redactSensitiveData(value);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
writeDebugLog(type, data) {
|
|
153
|
+
if (!this.debugLog)
|
|
154
|
+
return;
|
|
155
|
+
try {
|
|
156
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('.')[0];
|
|
157
|
+
const filepath = path.join(this.debugLogDir, `${timestamp}-${type}.json`);
|
|
158
|
+
fs.mkdirSync(this.debugLogDir, { recursive: true });
|
|
159
|
+
const redacted = this.redactSensitiveData(data);
|
|
160
|
+
fs.writeFileSync(filepath, JSON.stringify(redacted, null, 2) + '\n', 'utf8');
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
this.logger.warn('debug-log.write_failed', 'GLMT debug log write failed', {
|
|
164
|
+
message: error.message,
|
|
165
|
+
});
|
|
166
|
+
console.error(`[glmt-transformer] Debug log error: ${error.message}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
log(message) {
|
|
170
|
+
if (this.verbose) {
|
|
171
|
+
this.logger.debug('transformer.verbose', message);
|
|
172
|
+
console.error(`[glmt-transformer] [${new Date().toTimeString().split(' ')[0]}] ${message}`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// ========== Backwards-compatible public methods ==========
|
|
176
|
+
/** Generate thinking signature (delegates to ResponseBuilder) */
|
|
177
|
+
generateThinkingSignature(thinking) {
|
|
178
|
+
return this.responseBuilder.generateThinkingSignature(thinking);
|
|
179
|
+
}
|
|
180
|
+
/** Map stop reason (delegates to ResponseBuilder) */
|
|
181
|
+
mapStopReason(openaiReason) {
|
|
182
|
+
return this.responseBuilder.mapStopReason(openaiReason);
|
|
183
|
+
}
|
|
184
|
+
/** Detect think keywords (delegates to ContentTransformer) */
|
|
185
|
+
detectThinkKeywords(messages) {
|
|
186
|
+
return this.contentTransformer.detectThinkKeywords(messages);
|
|
187
|
+
}
|
|
188
|
+
/** Validate transformation result */
|
|
189
|
+
validateTransformation(anthropicResponse) {
|
|
190
|
+
const checks = {
|
|
191
|
+
hasContent: Boolean(anthropicResponse.content && anthropicResponse.content.length > 0),
|
|
192
|
+
hasThinking: anthropicResponse.content?.some((block) => block.type === 'thinking') || false,
|
|
193
|
+
hasText: anthropicResponse.content?.some((block) => block.type === 'text') || false,
|
|
194
|
+
validStructure: anthropicResponse.type === 'message' && anthropicResponse.role === 'assistant',
|
|
195
|
+
hasUsage: Boolean(anthropicResponse.usage),
|
|
196
|
+
};
|
|
197
|
+
const passed = Object.values(checks).filter(Boolean).length;
|
|
198
|
+
const total = Object.keys(checks).length;
|
|
199
|
+
return { checks, passed, total, valid: passed === total };
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.GlmtTransformer = GlmtTransformer;
|
|
203
|
+
exports.default = GlmtTransformer;
|
|
204
|
+
//# sourceMappingURL=glmt-transformer.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GLMT (GLM Thinking) Module Barrel Export
|
|
4
|
+
*
|
|
5
|
+
* Provides OpenAI-to-Anthropic protocol translation for GLM models with
|
|
6
|
+
* extended thinking support.
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
20
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.ReasoningEnforcer = exports.LocaleEnforcer = exports.DeltaAccumulator = exports.SSEParser = exports.GlmtTransformer = exports.GlmtProxy = void 0;
|
|
24
|
+
// Core proxy and transformer
|
|
25
|
+
var glmt_proxy_1 = require("./glmt-proxy");
|
|
26
|
+
Object.defineProperty(exports, "GlmtProxy", { enumerable: true, get: function () { return glmt_proxy_1.GlmtProxy; } });
|
|
27
|
+
var glmt_transformer_1 = require("./glmt-transformer");
|
|
28
|
+
Object.defineProperty(exports, "GlmtTransformer", { enumerable: true, get: function () { return glmt_transformer_1.GlmtTransformer; } });
|
|
29
|
+
// Streaming utilities
|
|
30
|
+
var sse_parser_1 = require("./sse-parser");
|
|
31
|
+
Object.defineProperty(exports, "SSEParser", { enumerable: true, get: function () { return sse_parser_1.SSEParser; } });
|
|
32
|
+
var delta_accumulator_1 = require("./delta-accumulator");
|
|
33
|
+
Object.defineProperty(exports, "DeltaAccumulator", { enumerable: true, get: function () { return delta_accumulator_1.DeltaAccumulator; } });
|
|
34
|
+
// Content enforcers
|
|
35
|
+
var locale_enforcer_1 = require("./locale-enforcer");
|
|
36
|
+
Object.defineProperty(exports, "LocaleEnforcer", { enumerable: true, get: function () { return locale_enforcer_1.LocaleEnforcer; } });
|
|
37
|
+
var reasoning_enforcer_1 = require("./reasoning-enforcer");
|
|
38
|
+
Object.defineProperty(exports, "ReasoningEnforcer", { enumerable: true, get: function () { return reasoning_enforcer_1.ReasoningEnforcer; } });
|
|
39
|
+
// Pipeline components and types
|
|
40
|
+
__exportStar(require("./pipeline"), exports);
|
|
41
|
+
//# sourceMappingURL=index.js.map
|