@hileeon/mcc 0.1.7 → 0.1.9
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 +226 -127
- package/dist/accounts/store.d.ts +1 -0
- package/dist/accounts/store.d.ts.map +1 -1
- package/dist/accounts/store.js.map +1 -1
- package/dist/commands/launch.d.ts +9 -0
- package/dist/commands/launch.d.ts.map +1 -0
- package/dist/commands/launch.js +158 -0
- package/dist/commands/launch.js.map +1 -0
- package/dist/commands/mcp.d.ts +9 -0
- package/dist/commands/mcp.d.ts.map +1 -0
- package/dist/commands/mcp.js +112 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/profile.d.ts +8 -0
- package/dist/commands/profile.d.ts.map +1 -0
- package/dist/commands/profile.js +125 -0
- package/dist/commands/profile.js.map +1 -0
- package/dist/core/model-router.d.ts.map +1 -1
- package/dist/core/model-router.js +5 -2
- package/dist/core/model-router.js.map +1 -1
- package/dist/{dashboard-server.d.ts → dashboard/server.d.ts} +1 -1
- package/dist/dashboard/server.d.ts.map +1 -0
- package/dist/{dashboard-server.js → dashboard/server.js} +169 -51
- package/dist/dashboard/server.js.map +1 -0
- package/dist/mcc.d.ts +4 -2
- package/dist/mcc.d.ts.map +1 -1
- package/dist/mcc.js +121 -408
- package/dist/mcc.js.map +1 -1
- package/dist/mcp/mcp-config.d.ts +17 -1
- package/dist/mcp/mcp-config.d.ts.map +1 -1
- package/dist/mcp/mcp-config.js +50 -17
- package/dist/mcp/mcp-config.js.map +1 -1
- package/dist/proxy/proxy-daemon.d.ts.map +1 -1
- package/dist/proxy/proxy-daemon.js +17 -2
- package/dist/proxy/proxy-daemon.js.map +1 -1
- package/dist/proxy/proxy-entry.js +5 -3
- package/dist/proxy/proxy-entry.js.map +1 -1
- package/dist/proxy/proxy-server.d.ts.map +1 -1
- package/dist/proxy/proxy-server.js +32 -6
- package/dist/proxy/proxy-server.js.map +1 -1
- package/dist/shared/config.d.ts +15 -0
- package/dist/shared/config.d.ts.map +1 -0
- package/dist/shared/config.js +79 -0
- package/dist/shared/config.js.map +1 -0
- package/dist/shared/logger.d.ts +23 -18
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/logger.js +17 -178
- package/dist/shared/logger.js.map +1 -1
- package/dist/shared/provider-preset-catalog.d.ts +6 -2
- package/dist/shared/provider-preset-catalog.d.ts.map +1 -1
- package/dist/shared/provider-preset-catalog.js +47 -26
- package/dist/shared/provider-preset-catalog.js.map +1 -1
- package/dist/ui/assets/index-ClqmrjNk.js +40 -0
- package/dist/ui/assets/index-CwMwQ-Z4.css +1 -0
- package/dist/ui/index.html +21 -13
- package/dist/update.d.ts +31 -0
- package/dist/update.d.ts.map +1 -0
- package/dist/update.js +196 -0
- package/dist/update.js.map +1 -0
- package/lib/mcp/mcc-image-analysis-server.cjs +454 -454
- package/lib/mcp/mcc-websearch-server.cjs +339 -339
- package/lib/mcp-hooks/image-analysis-runtime.cjs +510 -510
- package/lib/mcp-hooks/image-analyzer-transformer.cjs +526 -526
- package/lib/mcp-hooks/websearch-transformer.cjs +1597 -1421
- package/lib/proxy/config/config-loader-facade.js +24 -24
- package/lib/proxy/glmt/delta-accumulator.js +362 -362
- package/lib/proxy/glmt/glmt-transformer.js +203 -203
- package/lib/proxy/glmt/index.js +40 -40
- package/lib/proxy/glmt/locale-enforcer.js +68 -68
- package/lib/proxy/glmt/pipeline/content-transformer.js +161 -161
- package/lib/proxy/glmt/pipeline/index.js +19 -19
- package/lib/proxy/glmt/pipeline/request-transformer.js +115 -115
- package/lib/proxy/glmt/pipeline/response-builder.js +204 -204
- package/lib/proxy/glmt/pipeline/stream-parser.js +233 -233
- package/lib/proxy/glmt/pipeline/tool-call-handler.js +77 -77
- package/lib/proxy/glmt/pipeline/types.js +5 -5
- package/lib/proxy/glmt/reasoning-enforcer.js +150 -150
- package/lib/proxy/glmt/sse-parser.js +101 -101
- package/lib/proxy/services/logging.js +13 -13
- package/lib/proxy/transformers/request-transformer.js +471 -451
- package/lib/proxy/transformers/sse-stream-transformer.js +198 -198
- package/lib/shared/logger.cjs +156 -138
- package/package.json +58 -41
- package/dist/dashboard-server.d.ts.map +0 -1
- package/dist/dashboard-server.js.map +0 -1
- package/dist/ui/assets/index-B16lhKZ6.js +0 -40
- package/dist/ui/assets/index-jEfiB6-h.css +0 -1
|
@@ -1,151 +1,151 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* ReasoningEnforcer - Inject explicit reasoning instructions into prompts
|
|
4
|
-
*
|
|
5
|
-
* Purpose: Force GLM models to use structured reasoning output format (<reasoning_content>)
|
|
6
|
-
* This complements API parameters (reasoning: true) with explicit prompt instructions.
|
|
7
|
-
*
|
|
8
|
-
* Strategy:
|
|
9
|
-
* 1. If system prompt exists: Prepend reasoning instruction
|
|
10
|
-
* 2. If no system prompt: Prepend to first user message
|
|
11
|
-
* 3. Select prompt template based on effort level (low/medium/high/max)
|
|
12
|
-
* 4. Preserve message structure (string vs array content)
|
|
13
|
-
*/
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.ReasoningEnforcer = void 0;
|
|
16
|
-
class ReasoningEnforcer {
|
|
17
|
-
constructor(options = {}) {
|
|
18
|
-
this.enabled = options.enabled ?? false; // Opt-in by default
|
|
19
|
-
this.prompts = options.prompts || this.getDefaultPrompts();
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Inject reasoning instruction into messages
|
|
23
|
-
* @param messages - Messages array to modify
|
|
24
|
-
* @param thinkingConfig - { thinking: boolean, effort: string }
|
|
25
|
-
* @returns Modified messages array
|
|
26
|
-
*/
|
|
27
|
-
injectInstruction(messages, thinkingConfig = {}) {
|
|
28
|
-
// Only inject if enabled or thinking explicitly requested
|
|
29
|
-
if (!this.enabled && !thinkingConfig.thinking) {
|
|
30
|
-
return messages;
|
|
31
|
-
}
|
|
32
|
-
// Clone messages to avoid mutation
|
|
33
|
-
const modifiedMessages = JSON.parse(JSON.stringify(messages));
|
|
34
|
-
// Select prompt based on effort level
|
|
35
|
-
const effort = (thinkingConfig.effort?.toLowerCase() || 'medium');
|
|
36
|
-
const prompt = this.selectPrompt(effort);
|
|
37
|
-
// Strategy 1: Inject into system prompt (preferred)
|
|
38
|
-
const systemIndex = modifiedMessages.findIndex((m) => m.role === 'system');
|
|
39
|
-
if (systemIndex >= 0) {
|
|
40
|
-
const systemMsg = modifiedMessages[systemIndex];
|
|
41
|
-
if (typeof systemMsg.content === 'string') {
|
|
42
|
-
systemMsg.content = `${prompt}\n\n${systemMsg.content}`;
|
|
43
|
-
}
|
|
44
|
-
else if (Array.isArray(systemMsg.content)) {
|
|
45
|
-
systemMsg.content.unshift({
|
|
46
|
-
type: 'text',
|
|
47
|
-
text: prompt,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
return modifiedMessages;
|
|
51
|
-
}
|
|
52
|
-
// Strategy 2: Prepend to first user message
|
|
53
|
-
const userIndex = modifiedMessages.findIndex((m) => m.role === 'user');
|
|
54
|
-
if (userIndex >= 0) {
|
|
55
|
-
const userMsg = modifiedMessages[userIndex];
|
|
56
|
-
if (typeof userMsg.content === 'string') {
|
|
57
|
-
userMsg.content = `${prompt}\n\n${userMsg.content}`;
|
|
58
|
-
}
|
|
59
|
-
else if (Array.isArray(userMsg.content)) {
|
|
60
|
-
userMsg.content.unshift({
|
|
61
|
-
type: 'text',
|
|
62
|
-
text: prompt,
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
return modifiedMessages;
|
|
66
|
-
}
|
|
67
|
-
// No system or user messages found (edge case)
|
|
68
|
-
return modifiedMessages;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Select prompt template based on effort level
|
|
72
|
-
*/
|
|
73
|
-
selectPrompt(effort) {
|
|
74
|
-
return this.prompts[effort] || this.prompts.medium;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Get default prompt templates
|
|
78
|
-
*/
|
|
79
|
-
getDefaultPrompts() {
|
|
80
|
-
return {
|
|
81
|
-
low: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
82
|
-
|
|
83
|
-
CRITICAL: Before answering, write 2-3 sentences of reasoning in <reasoning_content> tags.
|
|
84
|
-
|
|
85
|
-
OUTPUT FORMAT:
|
|
86
|
-
<reasoning_content>
|
|
87
|
-
(Brief analysis: what is the problem? what's the approach?)
|
|
88
|
-
</reasoning_content>
|
|
89
|
-
|
|
90
|
-
(Write your final answer here)`,
|
|
91
|
-
medium: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
92
|
-
|
|
93
|
-
CRITICAL REQUIREMENTS:
|
|
94
|
-
1. Always think step-by-step before answering
|
|
95
|
-
2. Write your reasoning process explicitly in <reasoning_content> tags
|
|
96
|
-
3. Never skip your chain of thought, even for simple problems
|
|
97
|
-
|
|
98
|
-
OUTPUT FORMAT:
|
|
99
|
-
<reasoning_content>
|
|
100
|
-
(Write your detailed thinking here: analyze the problem, explore approaches,
|
|
101
|
-
evaluate trade-offs, and arrive at a conclusion)
|
|
102
|
-
</reasoning_content>
|
|
103
|
-
|
|
104
|
-
(Write your final answer here based on your reasoning above)`,
|
|
105
|
-
high: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
106
|
-
|
|
107
|
-
CRITICAL REQUIREMENTS:
|
|
108
|
-
1. Think deeply and systematically before answering
|
|
109
|
-
2. Write comprehensive reasoning in <reasoning_content> tags
|
|
110
|
-
3. Explore multiple approaches and evaluate trade-offs
|
|
111
|
-
4. Show all steps in your problem-solving process
|
|
112
|
-
|
|
113
|
-
OUTPUT FORMAT:
|
|
114
|
-
<reasoning_content>
|
|
115
|
-
(Write exhaustive analysis here:
|
|
116
|
-
- Problem decomposition
|
|
117
|
-
- Multiple approach exploration
|
|
118
|
-
- Trade-off analysis for each approach
|
|
119
|
-
- Edge case consideration
|
|
120
|
-
- Final conclusion with justification)
|
|
121
|
-
</reasoning_content>
|
|
122
|
-
|
|
123
|
-
(Write your final answer here based on your systematic reasoning above)`,
|
|
124
|
-
max: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
125
|
-
|
|
126
|
-
CRITICAL REQUIREMENTS:
|
|
127
|
-
1. Think exhaustively from first principles
|
|
128
|
-
2. Write extremely detailed reasoning in <reasoning_content> tags
|
|
129
|
-
3. Analyze ALL possible angles, approaches, and edge cases
|
|
130
|
-
4. Challenge your own assumptions and explore alternatives
|
|
131
|
-
5. Provide rigorous justification for every claim
|
|
132
|
-
|
|
133
|
-
OUTPUT FORMAT:
|
|
134
|
-
<reasoning_content>
|
|
135
|
-
(Write comprehensive analysis here:
|
|
136
|
-
- First principles breakdown
|
|
137
|
-
- Exhaustive approach enumeration
|
|
138
|
-
- Comparative analysis of all approaches
|
|
139
|
-
- Edge case and failure mode analysis
|
|
140
|
-
- Assumption validation
|
|
141
|
-
- Counter-argument consideration
|
|
142
|
-
- Final conclusion with rigorous justification)
|
|
143
|
-
</reasoning_content>
|
|
144
|
-
|
|
145
|
-
(Write your final answer here based on your exhaustive reasoning above)`,
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
exports.ReasoningEnforcer = ReasoningEnforcer;
|
|
150
|
-
exports.default = ReasoningEnforcer;
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ReasoningEnforcer - Inject explicit reasoning instructions into prompts
|
|
4
|
+
*
|
|
5
|
+
* Purpose: Force GLM models to use structured reasoning output format (<reasoning_content>)
|
|
6
|
+
* This complements API parameters (reasoning: true) with explicit prompt instructions.
|
|
7
|
+
*
|
|
8
|
+
* Strategy:
|
|
9
|
+
* 1. If system prompt exists: Prepend reasoning instruction
|
|
10
|
+
* 2. If no system prompt: Prepend to first user message
|
|
11
|
+
* 3. Select prompt template based on effort level (low/medium/high/max)
|
|
12
|
+
* 4. Preserve message structure (string vs array content)
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ReasoningEnforcer = void 0;
|
|
16
|
+
class ReasoningEnforcer {
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this.enabled = options.enabled ?? false; // Opt-in by default
|
|
19
|
+
this.prompts = options.prompts || this.getDefaultPrompts();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Inject reasoning instruction into messages
|
|
23
|
+
* @param messages - Messages array to modify
|
|
24
|
+
* @param thinkingConfig - { thinking: boolean, effort: string }
|
|
25
|
+
* @returns Modified messages array
|
|
26
|
+
*/
|
|
27
|
+
injectInstruction(messages, thinkingConfig = {}) {
|
|
28
|
+
// Only inject if enabled or thinking explicitly requested
|
|
29
|
+
if (!this.enabled && !thinkingConfig.thinking) {
|
|
30
|
+
return messages;
|
|
31
|
+
}
|
|
32
|
+
// Clone messages to avoid mutation
|
|
33
|
+
const modifiedMessages = JSON.parse(JSON.stringify(messages));
|
|
34
|
+
// Select prompt based on effort level
|
|
35
|
+
const effort = (thinkingConfig.effort?.toLowerCase() || 'medium');
|
|
36
|
+
const prompt = this.selectPrompt(effort);
|
|
37
|
+
// Strategy 1: Inject into system prompt (preferred)
|
|
38
|
+
const systemIndex = modifiedMessages.findIndex((m) => m.role === 'system');
|
|
39
|
+
if (systemIndex >= 0) {
|
|
40
|
+
const systemMsg = modifiedMessages[systemIndex];
|
|
41
|
+
if (typeof systemMsg.content === 'string') {
|
|
42
|
+
systemMsg.content = `${prompt}\n\n${systemMsg.content}`;
|
|
43
|
+
}
|
|
44
|
+
else if (Array.isArray(systemMsg.content)) {
|
|
45
|
+
systemMsg.content.unshift({
|
|
46
|
+
type: 'text',
|
|
47
|
+
text: prompt,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return modifiedMessages;
|
|
51
|
+
}
|
|
52
|
+
// Strategy 2: Prepend to first user message
|
|
53
|
+
const userIndex = modifiedMessages.findIndex((m) => m.role === 'user');
|
|
54
|
+
if (userIndex >= 0) {
|
|
55
|
+
const userMsg = modifiedMessages[userIndex];
|
|
56
|
+
if (typeof userMsg.content === 'string') {
|
|
57
|
+
userMsg.content = `${prompt}\n\n${userMsg.content}`;
|
|
58
|
+
}
|
|
59
|
+
else if (Array.isArray(userMsg.content)) {
|
|
60
|
+
userMsg.content.unshift({
|
|
61
|
+
type: 'text',
|
|
62
|
+
text: prompt,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return modifiedMessages;
|
|
66
|
+
}
|
|
67
|
+
// No system or user messages found (edge case)
|
|
68
|
+
return modifiedMessages;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Select prompt template based on effort level
|
|
72
|
+
*/
|
|
73
|
+
selectPrompt(effort) {
|
|
74
|
+
return this.prompts[effort] || this.prompts.medium;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get default prompt templates
|
|
78
|
+
*/
|
|
79
|
+
getDefaultPrompts() {
|
|
80
|
+
return {
|
|
81
|
+
low: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
82
|
+
|
|
83
|
+
CRITICAL: Before answering, write 2-3 sentences of reasoning in <reasoning_content> tags.
|
|
84
|
+
|
|
85
|
+
OUTPUT FORMAT:
|
|
86
|
+
<reasoning_content>
|
|
87
|
+
(Brief analysis: what is the problem? what's the approach?)
|
|
88
|
+
</reasoning_content>
|
|
89
|
+
|
|
90
|
+
(Write your final answer here)`,
|
|
91
|
+
medium: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
92
|
+
|
|
93
|
+
CRITICAL REQUIREMENTS:
|
|
94
|
+
1. Always think step-by-step before answering
|
|
95
|
+
2. Write your reasoning process explicitly in <reasoning_content> tags
|
|
96
|
+
3. Never skip your chain of thought, even for simple problems
|
|
97
|
+
|
|
98
|
+
OUTPUT FORMAT:
|
|
99
|
+
<reasoning_content>
|
|
100
|
+
(Write your detailed thinking here: analyze the problem, explore approaches,
|
|
101
|
+
evaluate trade-offs, and arrive at a conclusion)
|
|
102
|
+
</reasoning_content>
|
|
103
|
+
|
|
104
|
+
(Write your final answer here based on your reasoning above)`,
|
|
105
|
+
high: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
106
|
+
|
|
107
|
+
CRITICAL REQUIREMENTS:
|
|
108
|
+
1. Think deeply and systematically before answering
|
|
109
|
+
2. Write comprehensive reasoning in <reasoning_content> tags
|
|
110
|
+
3. Explore multiple approaches and evaluate trade-offs
|
|
111
|
+
4. Show all steps in your problem-solving process
|
|
112
|
+
|
|
113
|
+
OUTPUT FORMAT:
|
|
114
|
+
<reasoning_content>
|
|
115
|
+
(Write exhaustive analysis here:
|
|
116
|
+
- Problem decomposition
|
|
117
|
+
- Multiple approach exploration
|
|
118
|
+
- Trade-off analysis for each approach
|
|
119
|
+
- Edge case consideration
|
|
120
|
+
- Final conclusion with justification)
|
|
121
|
+
</reasoning_content>
|
|
122
|
+
|
|
123
|
+
(Write your final answer here based on your systematic reasoning above)`,
|
|
124
|
+
max: `You are an expert reasoning model using GLM-4.6 architecture.
|
|
125
|
+
|
|
126
|
+
CRITICAL REQUIREMENTS:
|
|
127
|
+
1. Think exhaustively from first principles
|
|
128
|
+
2. Write extremely detailed reasoning in <reasoning_content> tags
|
|
129
|
+
3. Analyze ALL possible angles, approaches, and edge cases
|
|
130
|
+
4. Challenge your own assumptions and explore alternatives
|
|
131
|
+
5. Provide rigorous justification for every claim
|
|
132
|
+
|
|
133
|
+
OUTPUT FORMAT:
|
|
134
|
+
<reasoning_content>
|
|
135
|
+
(Write comprehensive analysis here:
|
|
136
|
+
- First principles breakdown
|
|
137
|
+
- Exhaustive approach enumeration
|
|
138
|
+
- Comparative analysis of all approaches
|
|
139
|
+
- Edge case and failure mode analysis
|
|
140
|
+
- Assumption validation
|
|
141
|
+
- Counter-argument consideration
|
|
142
|
+
- Final conclusion with rigorous justification)
|
|
143
|
+
</reasoning_content>
|
|
144
|
+
|
|
145
|
+
(Write your final answer here based on your exhaustive reasoning above)`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.ReasoningEnforcer = ReasoningEnforcer;
|
|
150
|
+
exports.default = ReasoningEnforcer;
|
|
151
151
|
//# sourceMappingURL=reasoning-enforcer.js.map
|
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
/**
|
|
4
|
-
* SSEParser - Parse Server-Sent Events (SSE) stream
|
|
5
|
-
*
|
|
6
|
-
* Handles:
|
|
7
|
-
* - Incomplete events across chunks
|
|
8
|
-
* - Multiple events in single chunk
|
|
9
|
-
* - Malformed data (skip gracefully)
|
|
10
|
-
* - [DONE] marker
|
|
11
|
-
*
|
|
12
|
-
* Usage:
|
|
13
|
-
* const parser = new SSEParser();
|
|
14
|
-
* stream.on('data', chunk => {
|
|
15
|
-
* const events = parser.parse(chunk);
|
|
16
|
-
* events.forEach(event => { ... });
|
|
17
|
-
* });
|
|
18
|
-
*/
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.SSEParser = void 0;
|
|
21
|
-
class SSEParser {
|
|
22
|
-
constructor(options = {}) {
|
|
23
|
-
this.buffer = '';
|
|
24
|
-
this.eventCount = 0;
|
|
25
|
-
this.maxBufferSize = options.maxBufferSize || 1024 * 1024; // 1MB default
|
|
26
|
-
this.throwOnMalformedJson = options.throwOnMalformedJson === true;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Parse chunk and extract SSE events
|
|
30
|
-
* @param chunk - Data chunk from stream
|
|
31
|
-
* @returns Array of parsed events
|
|
32
|
-
*/
|
|
33
|
-
parse(chunk) {
|
|
34
|
-
this.buffer += chunk.toString().replace(/\r\n?/g, '\n');
|
|
35
|
-
// C-01 Fix: Prevent unbounded buffer growth (DoS protection)
|
|
36
|
-
if (this.buffer.length > this.maxBufferSize) {
|
|
37
|
-
throw new Error(`SSE buffer exceeded ${this.maxBufferSize} bytes (DoS protection)`);
|
|
38
|
-
}
|
|
39
|
-
const events = [];
|
|
40
|
-
const segments = this.buffer.split('\n\n');
|
|
41
|
-
this.buffer = segments.pop() || '';
|
|
42
|
-
for (const segment of segments) {
|
|
43
|
-
const lines = segment.split('\n');
|
|
44
|
-
const currentEvent = { event: 'message', data: '' };
|
|
45
|
-
const dataLines = [];
|
|
46
|
-
for (const rawLine of lines) {
|
|
47
|
-
const line = rawLine.trimEnd();
|
|
48
|
-
if (!line || line.startsWith(':')) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
if (line.startsWith('event: ')) {
|
|
52
|
-
currentEvent.event = line.substring(7).trim();
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
if (line.startsWith('data:')) {
|
|
56
|
-
dataLines.push(line.substring(5).trimStart());
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
if (line.startsWith('id: ')) {
|
|
60
|
-
currentEvent.id = line.substring(4).trim();
|
|
61
|
-
continue;
|
|
62
|
-
}
|
|
63
|
-
if (line.startsWith('retry: ')) {
|
|
64
|
-
currentEvent.retry = parseInt(line.substring(7), 10);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const data = dataLines.join('\n');
|
|
68
|
-
if (!data) {
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
if (data === '[DONE]') {
|
|
72
|
-
this.eventCount++;
|
|
73
|
-
events.push({ event: 'done', data: null, index: this.eventCount });
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
try {
|
|
77
|
-
currentEvent.data = JSON.parse(data);
|
|
78
|
-
this.eventCount++;
|
|
79
|
-
currentEvent.index = this.eventCount;
|
|
80
|
-
events.push({ ...currentEvent });
|
|
81
|
-
}
|
|
82
|
-
catch (e) {
|
|
83
|
-
if (typeof console !== 'undefined' && console.error) {
|
|
84
|
-
console.error('[SSEParser] Malformed JSON event:', e.message, 'Data:', data.substring(0, 100));
|
|
85
|
-
}
|
|
86
|
-
if (this.throwOnMalformedJson) {
|
|
87
|
-
throw new Error(`Malformed SSE JSON event: ${e.message}`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return events;
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Reset parser state (for reuse)
|
|
95
|
-
*/
|
|
96
|
-
reset() {
|
|
97
|
-
this.buffer = '';
|
|
98
|
-
this.eventCount = 0;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.SSEParser = SSEParser;
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
/**
|
|
4
|
+
* SSEParser - Parse Server-Sent Events (SSE) stream
|
|
5
|
+
*
|
|
6
|
+
* Handles:
|
|
7
|
+
* - Incomplete events across chunks
|
|
8
|
+
* - Multiple events in single chunk
|
|
9
|
+
* - Malformed data (skip gracefully)
|
|
10
|
+
* - [DONE] marker
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* const parser = new SSEParser();
|
|
14
|
+
* stream.on('data', chunk => {
|
|
15
|
+
* const events = parser.parse(chunk);
|
|
16
|
+
* events.forEach(event => { ... });
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.SSEParser = void 0;
|
|
21
|
+
class SSEParser {
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.buffer = '';
|
|
24
|
+
this.eventCount = 0;
|
|
25
|
+
this.maxBufferSize = options.maxBufferSize || 1024 * 1024; // 1MB default
|
|
26
|
+
this.throwOnMalformedJson = options.throwOnMalformedJson === true;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parse chunk and extract SSE events
|
|
30
|
+
* @param chunk - Data chunk from stream
|
|
31
|
+
* @returns Array of parsed events
|
|
32
|
+
*/
|
|
33
|
+
parse(chunk) {
|
|
34
|
+
this.buffer += chunk.toString().replace(/\r\n?/g, '\n');
|
|
35
|
+
// C-01 Fix: Prevent unbounded buffer growth (DoS protection)
|
|
36
|
+
if (this.buffer.length > this.maxBufferSize) {
|
|
37
|
+
throw new Error(`SSE buffer exceeded ${this.maxBufferSize} bytes (DoS protection)`);
|
|
38
|
+
}
|
|
39
|
+
const events = [];
|
|
40
|
+
const segments = this.buffer.split('\n\n');
|
|
41
|
+
this.buffer = segments.pop() || '';
|
|
42
|
+
for (const segment of segments) {
|
|
43
|
+
const lines = segment.split('\n');
|
|
44
|
+
const currentEvent = { event: 'message', data: '' };
|
|
45
|
+
const dataLines = [];
|
|
46
|
+
for (const rawLine of lines) {
|
|
47
|
+
const line = rawLine.trimEnd();
|
|
48
|
+
if (!line || line.startsWith(':')) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (line.startsWith('event: ')) {
|
|
52
|
+
currentEvent.event = line.substring(7).trim();
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (line.startsWith('data:')) {
|
|
56
|
+
dataLines.push(line.substring(5).trimStart());
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (line.startsWith('id: ')) {
|
|
60
|
+
currentEvent.id = line.substring(4).trim();
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (line.startsWith('retry: ')) {
|
|
64
|
+
currentEvent.retry = parseInt(line.substring(7), 10);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const data = dataLines.join('\n');
|
|
68
|
+
if (!data) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (data === '[DONE]') {
|
|
72
|
+
this.eventCount++;
|
|
73
|
+
events.push({ event: 'done', data: null, index: this.eventCount });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
currentEvent.data = JSON.parse(data);
|
|
78
|
+
this.eventCount++;
|
|
79
|
+
currentEvent.index = this.eventCount;
|
|
80
|
+
events.push({ ...currentEvent });
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
if (typeof console !== 'undefined' && console.error) {
|
|
84
|
+
console.error('[SSEParser] Malformed JSON event:', e.message, 'Data:', data.substring(0, 100));
|
|
85
|
+
}
|
|
86
|
+
if (this.throwOnMalformedJson) {
|
|
87
|
+
throw new Error(`Malformed SSE JSON event: ${e.message}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return events;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reset parser state (for reuse)
|
|
95
|
+
*/
|
|
96
|
+
reset() {
|
|
97
|
+
this.buffer = '';
|
|
98
|
+
this.eventCount = 0;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.SSEParser = SSEParser;
|
|
102
102
|
//# sourceMappingURL=sse-parser.js.map
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Stub for CCS services/logging - provides no-op logger
|
|
3
|
-
*/
|
|
4
|
-
function createLogger(_name) {
|
|
5
|
-
return {
|
|
6
|
-
info: () => {},
|
|
7
|
-
warn: () => {},
|
|
8
|
-
error: () => {},
|
|
9
|
-
debug: () => {},
|
|
10
|
-
stage: () => {},
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
module.exports = { createLogger };
|
|
1
|
+
/**
|
|
2
|
+
* Stub for CCS services/logging - provides no-op logger
|
|
3
|
+
*/
|
|
4
|
+
function createLogger(_name) {
|
|
5
|
+
return {
|
|
6
|
+
info: () => {},
|
|
7
|
+
warn: () => {},
|
|
8
|
+
error: () => {},
|
|
9
|
+
debug: () => {},
|
|
10
|
+
stage: () => {},
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
module.exports = { createLogger };
|