@kaitranntt/ccs 4.4.0 → 5.0.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/README.md +14 -7
- package/VERSION +1 -1
- package/config/base-agy.settings.json +10 -0
- package/config/base-codex.settings.json +10 -0
- package/config/base-gemini.settings.json +10 -0
- package/dist/auth/auth-commands.d.ts +52 -0
- package/dist/auth/auth-commands.d.ts.map +1 -0
- package/dist/auth/auth-commands.js +479 -0
- package/dist/auth/auth-commands.js.map +1 -0
- package/dist/auth/profile-detector.d.ts +68 -0
- package/dist/auth/profile-detector.d.ts.map +1 -0
- package/dist/auth/profile-detector.js +209 -0
- package/dist/auth/profile-detector.js.map +1 -0
- package/dist/auth/profile-registry.d.ts +60 -0
- package/dist/auth/profile-registry.d.ts.map +1 -0
- package/dist/auth/profile-registry.js +188 -0
- package/dist/auth/profile-registry.js.map +1 -0
- package/dist/ccs.d.ts +10 -0
- package/dist/ccs.d.ts.map +1 -0
- package/dist/ccs.js +320 -0
- package/dist/ccs.js.map +1 -0
- package/dist/cliproxy/auth-handler.d.ts +93 -0
- package/dist/cliproxy/auth-handler.d.ts.map +1 -0
- package/dist/cliproxy/auth-handler.js +402 -0
- package/dist/cliproxy/auth-handler.js.map +1 -0
- package/dist/cliproxy/base-config-loader.d.ts +42 -0
- package/dist/cliproxy/base-config-loader.d.ts.map +1 -0
- package/dist/cliproxy/base-config-loader.js +123 -0
- package/dist/cliproxy/base-config-loader.js.map +1 -0
- package/dist/cliproxy/binary-manager.d.ts +104 -0
- package/dist/cliproxy/binary-manager.d.ts.map +1 -0
- package/dist/cliproxy/binary-manager.js +567 -0
- package/dist/cliproxy/binary-manager.js.map +1 -0
- package/dist/cliproxy/cliproxy-executor.d.ts +33 -0
- package/dist/cliproxy/cliproxy-executor.d.ts.map +1 -0
- package/dist/cliproxy/cliproxy-executor.js +297 -0
- package/dist/cliproxy/cliproxy-executor.js.map +1 -0
- package/dist/cliproxy/config-generator.d.ts +89 -0
- package/dist/cliproxy/config-generator.d.ts.map +1 -0
- package/dist/cliproxy/config-generator.js +263 -0
- package/dist/cliproxy/config-generator.js.map +1 -0
- package/dist/cliproxy/index.d.ts +13 -0
- package/dist/cliproxy/index.d.ts.map +1 -0
- package/dist/cliproxy/index.js +62 -0
- package/dist/cliproxy/index.js.map +1 -0
- package/dist/cliproxy/platform-detector.d.ts +48 -0
- package/dist/cliproxy/platform-detector.d.ts.map +1 -0
- package/dist/cliproxy/platform-detector.js +118 -0
- package/dist/cliproxy/platform-detector.js.map +1 -0
- package/dist/cliproxy/types.d.ts +169 -0
- package/dist/cliproxy/types.d.ts.map +1 -0
- package/dist/cliproxy/types.js +7 -0
- package/dist/cliproxy/types.js.map +1 -0
- package/dist/commands/doctor-command.d.ts +10 -0
- package/dist/commands/doctor-command.d.ts.map +1 -0
- package/dist/commands/doctor-command.js +44 -0
- package/dist/commands/doctor-command.js.map +1 -0
- package/dist/commands/help-command.d.ts +5 -0
- package/dist/commands/help-command.d.ts.map +1 -0
- package/dist/commands/help-command.js +104 -0
- package/dist/commands/help-command.js.map +1 -0
- package/dist/commands/install-command.d.ts +14 -0
- package/dist/commands/install-command.d.ts.map +1 -0
- package/dist/commands/install-command.js +39 -0
- package/dist/commands/install-command.js.map +1 -0
- package/dist/commands/shell-completion-command.d.ts +10 -0
- package/dist/commands/shell-completion-command.d.ts.map +1 -0
- package/dist/commands/shell-completion-command.js +85 -0
- package/dist/commands/shell-completion-command.js.map +1 -0
- package/dist/commands/sync-command.d.ts +10 -0
- package/dist/commands/sync-command.d.ts.map +1 -0
- package/dist/commands/sync-command.js +59 -0
- package/dist/commands/sync-command.js.map +1 -0
- package/dist/commands/update-command.d.ts +12 -0
- package/dist/commands/update-command.d.ts.map +1 -0
- package/dist/commands/update-command.js +295 -0
- package/dist/commands/update-command.js.map +1 -0
- package/dist/commands/version-command.d.ts +10 -0
- package/dist/commands/version-command.d.ts.map +1 -0
- package/dist/commands/version-command.js +100 -0
- package/dist/commands/version-command.js.map +1 -0
- package/dist/delegation/delegation-handler.d.ts +60 -0
- package/dist/delegation/delegation-handler.d.ts.map +1 -0
- package/dist/delegation/delegation-handler.js +174 -0
- package/dist/delegation/delegation-handler.js.map +1 -0
- package/dist/delegation/headless-executor.d.ts +114 -0
- package/dist/delegation/headless-executor.d.ts.map +1 -0
- package/dist/delegation/headless-executor.js +562 -0
- package/dist/delegation/headless-executor.js.map +1 -0
- package/dist/delegation/result-formatter.d.ts +108 -0
- package/dist/delegation/result-formatter.d.ts.map +1 -0
- package/dist/delegation/result-formatter.js +391 -0
- package/dist/delegation/result-formatter.js.map +1 -0
- package/dist/delegation/session-manager.d.ts +58 -0
- package/dist/delegation/session-manager.d.ts.map +1 -0
- package/dist/delegation/session-manager.js +153 -0
- package/dist/delegation/session-manager.js.map +1 -0
- package/dist/delegation/settings-parser.d.ts +31 -0
- package/dist/delegation/settings-parser.d.ts.map +1 -0
- package/dist/delegation/settings-parser.js +107 -0
- package/dist/delegation/settings-parser.js.map +1 -0
- package/dist/glmt/delta-accumulator.d.ts +210 -0
- package/dist/glmt/delta-accumulator.d.ts.map +1 -0
- package/dist/glmt/delta-accumulator.js +351 -0
- package/dist/glmt/delta-accumulator.js.map +1 -0
- package/dist/glmt/glmt-proxy.d.ts +72 -0
- package/dist/glmt/glmt-proxy.d.ts.map +1 -0
- package/dist/glmt/glmt-proxy.js +427 -0
- package/dist/glmt/glmt-proxy.js.map +1 -0
- package/dist/glmt/glmt-transformer.d.ts +265 -0
- package/dist/glmt/glmt-transformer.d.ts.map +1 -0
- package/dist/glmt/glmt-transformer.js +832 -0
- package/dist/glmt/glmt-transformer.js.map +1 -0
- package/dist/glmt/locale-enforcer.d.ts +38 -0
- package/dist/glmt/locale-enforcer.d.ts.map +1 -0
- package/dist/glmt/locale-enforcer.js +69 -0
- package/dist/glmt/locale-enforcer.js.map +1 -0
- package/dist/glmt/reasoning-enforcer.d.ts +52 -0
- package/dist/glmt/reasoning-enforcer.d.ts.map +1 -0
- package/dist/glmt/reasoning-enforcer.js +151 -0
- package/dist/glmt/reasoning-enforcer.js.map +1 -0
- package/dist/glmt/sse-parser.d.ts +47 -0
- package/dist/glmt/sse-parser.d.ts.map +1 -0
- package/dist/glmt/sse-parser.js +93 -0
- package/dist/glmt/sse-parser.js.map +1 -0
- package/dist/management/doctor.d.ts +104 -0
- package/dist/management/doctor.d.ts.map +1 -0
- package/dist/management/doctor.js +673 -0
- package/dist/management/doctor.js.map +1 -0
- package/dist/management/instance-manager.d.ts +57 -0
- package/dist/management/instance-manager.d.ts.map +1 -0
- package/dist/management/instance-manager.js +195 -0
- package/dist/management/instance-manager.js.map +1 -0
- package/dist/management/recovery-manager.d.ts +39 -0
- package/dist/management/recovery-manager.d.ts.map +1 -0
- package/dist/management/recovery-manager.js +141 -0
- package/dist/management/recovery-manager.js.map +1 -0
- package/dist/management/shared-manager.d.ts +47 -0
- package/dist/management/shared-manager.d.ts.map +1 -0
- package/dist/management/shared-manager.js +388 -0
- package/dist/management/shared-manager.js.map +1 -0
- package/dist/types/cli.d.ts +50 -0
- package/dist/types/cli.d.ts.map +1 -0
- package/dist/types/cli.js +16 -0
- package/dist/types/cli.js.map +1 -0
- package/dist/types/config.d.ts +51 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +26 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/delegation.d.ts +61 -0
- package/dist/types/delegation.d.ts.map +1 -0
- package/dist/types/delegation.js +6 -0
- package/dist/types/delegation.js.map +1 -0
- package/dist/types/glmt.d.ts +95 -0
- package/dist/types/glmt.d.ts.map +1 -0
- package/dist/types/glmt.js +7 -0
- package/dist/types/glmt.js.map +1 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +16 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/utils.d.ts +36 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/types/utils.js +22 -0
- package/dist/types/utils.js.map +1 -0
- package/dist/utils/claude-detector.d.ts +14 -0
- package/dist/utils/claude-detector.d.ts.map +1 -0
- package/dist/utils/claude-detector.js +112 -0
- package/dist/utils/claude-detector.js.map +1 -0
- package/dist/utils/claude-dir-installer.d.ts +46 -0
- package/dist/utils/claude-dir-installer.d.ts.map +1 -0
- package/dist/utils/claude-dir-installer.js +289 -0
- package/dist/utils/claude-dir-installer.js.map +1 -0
- package/dist/utils/claude-symlink-manager.d.ts +61 -0
- package/dist/utils/claude-symlink-manager.d.ts.map +1 -0
- package/dist/utils/claude-symlink-manager.js +291 -0
- package/dist/utils/claude-symlink-manager.js.map +1 -0
- package/dist/utils/config-manager.d.ts +32 -0
- package/dist/utils/config-manager.d.ts.map +1 -0
- package/dist/utils/config-manager.js +143 -0
- package/dist/utils/config-manager.js.map +1 -0
- package/dist/utils/delegation-validator.d.ts +39 -0
- package/dist/utils/delegation-validator.d.ts.map +1 -0
- package/dist/utils/delegation-validator.js +161 -0
- package/dist/utils/delegation-validator.js.map +1 -0
- package/dist/utils/error-codes.d.ts +36 -0
- package/dist/utils/error-codes.d.ts.map +1 -0
- package/dist/utils/error-codes.js +63 -0
- package/dist/utils/error-codes.js.map +1 -0
- package/dist/utils/error-manager.d.ts +59 -0
- package/dist/utils/error-manager.d.ts.map +1 -0
- package/dist/utils/error-manager.js +228 -0
- package/dist/utils/error-manager.js.map +1 -0
- package/dist/utils/helpers.d.ts +27 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +150 -0
- package/dist/utils/helpers.js.map +1 -0
- package/dist/utils/package-manager-detector.d.ts +14 -0
- package/dist/utils/package-manager-detector.d.ts.map +1 -0
- package/dist/utils/package-manager-detector.js +162 -0
- package/dist/utils/package-manager-detector.js.map +1 -0
- package/dist/utils/progress-indicator.d.ts +52 -0
- package/dist/utils/progress-indicator.d.ts.map +1 -0
- package/dist/utils/progress-indicator.js +102 -0
- package/dist/utils/progress-indicator.js.map +1 -0
- package/dist/utils/prompt.d.ts +29 -0
- package/dist/utils/prompt.d.ts.map +1 -0
- package/dist/utils/prompt.js +116 -0
- package/dist/utils/prompt.js.map +1 -0
- package/dist/utils/shell-completion.d.ts +52 -0
- package/dist/utils/shell-completion.d.ts.map +1 -0
- package/dist/utils/shell-completion.js +231 -0
- package/dist/utils/shell-completion.js.map +1 -0
- package/dist/utils/shell-executor.d.ts +15 -0
- package/dist/utils/shell-executor.d.ts.map +1 -0
- package/dist/utils/shell-executor.js +57 -0
- package/dist/utils/shell-executor.js.map +1 -0
- package/dist/utils/update-checker.d.ts +48 -0
- package/dist/utils/update-checker.d.ts.map +1 -0
- package/dist/utils/update-checker.js +241 -0
- package/dist/utils/update-checker.js.map +1 -0
- package/lib/ccs +21 -1907
- package/lib/ccs.ps1 +26 -1800
- package/lib/error-codes.ps1 +2 -1
- package/lib/prompt.ps1 +2 -2
- package/package.json +31 -11
- package/scripts/add-shebang.js +39 -0
- package/scripts/bump-version.sh +25 -37
- package/scripts/dev-install.sh +32 -11
- package/scripts/postinstall.js +29 -29
- package/bin/auth/auth-commands.js +0 -499
- package/bin/auth/profile-detector.js +0 -204
- package/bin/auth/profile-registry.js +0 -225
- package/bin/ccs.js +0 -1034
- package/bin/delegation/README.md +0 -191
- package/bin/delegation/delegation-handler.js +0 -212
- package/bin/delegation/headless-executor.js +0 -618
- package/bin/delegation/result-formatter.js +0 -485
- package/bin/delegation/session-manager.js +0 -157
- package/bin/delegation/settings-parser.js +0 -109
- package/bin/glmt/delta-accumulator.js +0 -276
- package/bin/glmt/glmt-proxy.js +0 -495
- package/bin/glmt/glmt-transformer.js +0 -999
- package/bin/glmt/locale-enforcer.js +0 -72
- package/bin/glmt/reasoning-enforcer.js +0 -173
- package/bin/glmt/sse-parser.js +0 -96
- package/bin/management/doctor.js +0 -721
- package/bin/management/instance-manager.js +0 -202
- package/bin/management/recovery-manager.js +0 -135
- package/bin/management/shared-manager.js +0 -402
- package/bin/utils/claude-detector.js +0 -73
- package/bin/utils/claude-dir-installer.js +0 -283
- package/bin/utils/claude-symlink-manager.js +0 -289
- package/bin/utils/config-manager.js +0 -103
- package/bin/utils/delegation-validator.js +0 -154
- package/bin/utils/error-codes.js +0 -59
- package/bin/utils/error-manager.js +0 -165
- package/bin/utils/helpers.js +0 -136
- package/bin/utils/progress-indicator.js +0 -111
- package/bin/utils/prompt.js +0 -134
- package/bin/utils/shell-completion.js +0 -256
- package/bin/utils/update-checker.js +0 -243
|
@@ -0,0 +1,832 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* GlmtTransformer - Convert between Anthropic and OpenAI formats with thinking and tool support
|
|
4
|
+
*
|
|
5
|
+
* Features:
|
|
6
|
+
* - Request: Anthropic → OpenAI (inject reasoning params, transform tools)
|
|
7
|
+
* - Response: OpenAI reasoning_content → Anthropic thinking blocks
|
|
8
|
+
* - Tool Support: Anthropic tools ↔ OpenAI function calling (bidirectional)
|
|
9
|
+
* - Streaming: Real-time tool calls with input_json deltas
|
|
10
|
+
* - Debug mode: Log raw data to ~/.ccs/logs/ (CCS_DEBUG=1)
|
|
11
|
+
* - Verbose mode: Console logging with timestamps
|
|
12
|
+
* - Validation: Self-test transformation results
|
|
13
|
+
*
|
|
14
|
+
* Control Tags (in user prompt):
|
|
15
|
+
* <Thinking:On|Off> - Enable/disable reasoning
|
|
16
|
+
* <Effort:Low|Medium|High> - Control reasoning depth
|
|
17
|
+
*/
|
|
18
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
19
|
+
if (k2 === undefined) k2 = k;
|
|
20
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
21
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
22
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
23
|
+
}
|
|
24
|
+
Object.defineProperty(o, k2, desc);
|
|
25
|
+
}) : (function(o, m, k, k2) {
|
|
26
|
+
if (k2 === undefined) k2 = k;
|
|
27
|
+
o[k2] = m[k];
|
|
28
|
+
}));
|
|
29
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
30
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
31
|
+
}) : function(o, v) {
|
|
32
|
+
o["default"] = v;
|
|
33
|
+
});
|
|
34
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
35
|
+
if (mod && mod.__esModule) return mod;
|
|
36
|
+
var result = {};
|
|
37
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
38
|
+
__setModuleDefault(result, mod);
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.GlmtTransformer = void 0;
|
|
43
|
+
const crypto = __importStar(require("crypto"));
|
|
44
|
+
const fs = __importStar(require("fs"));
|
|
45
|
+
const path = __importStar(require("path"));
|
|
46
|
+
const os = __importStar(require("os"));
|
|
47
|
+
const locale_enforcer_1 = require("./locale-enforcer");
|
|
48
|
+
const reasoning_enforcer_1 = require("./reasoning-enforcer");
|
|
49
|
+
class GlmtTransformer {
|
|
50
|
+
constructor(config = {}) {
|
|
51
|
+
this.defaultThinking = config.defaultThinking ?? true;
|
|
52
|
+
this.verbose = config.verbose || false;
|
|
53
|
+
// CCS_DEBUG controls all debug logging (file + console)
|
|
54
|
+
const debugEnabled = process.env.CCS_DEBUG === '1';
|
|
55
|
+
this.debugLog = config.debugLog ?? debugEnabled;
|
|
56
|
+
this.debugMode = config.debugMode ?? debugEnabled;
|
|
57
|
+
this.debugLogDir = config.debugLogDir || path.join(os.homedir(), '.ccs', 'logs');
|
|
58
|
+
this.modelMaxTokens = {
|
|
59
|
+
'GLM-4.6': 128000,
|
|
60
|
+
'GLM-4.5': 96000,
|
|
61
|
+
'GLM-4.5-air': 16000,
|
|
62
|
+
};
|
|
63
|
+
// Initialize locale enforcer (always enforce English)
|
|
64
|
+
this.localeEnforcer = new locale_enforcer_1.LocaleEnforcer();
|
|
65
|
+
// Initialize reasoning enforcer (enabled by default for all GLMT usage)
|
|
66
|
+
this.reasoningEnforcer = new reasoning_enforcer_1.ReasoningEnforcer({
|
|
67
|
+
enabled: config.explicitReasoning ?? true,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Transform Anthropic request to OpenAI format
|
|
72
|
+
*/
|
|
73
|
+
transformRequest(anthropicRequest) {
|
|
74
|
+
// Log original request
|
|
75
|
+
this.writeDebugLog('request-anthropic', anthropicRequest);
|
|
76
|
+
try {
|
|
77
|
+
// 1. Extract thinking control from messages (tags like <Thinking:On|Off>)
|
|
78
|
+
const thinkingConfig = this.extractThinkingControl(anthropicRequest.messages || []);
|
|
79
|
+
const hasControlTags = this.hasThinkingTags(anthropicRequest.messages || []);
|
|
80
|
+
// 2. Detect "think" keywords in user prompts (Anthropic-style)
|
|
81
|
+
const keywordConfig = this.detectThinkKeywords(anthropicRequest.messages || []);
|
|
82
|
+
if (keywordConfig && !anthropicRequest.thinking && !hasControlTags) {
|
|
83
|
+
thinkingConfig.thinking = keywordConfig.thinking;
|
|
84
|
+
thinkingConfig.effort = keywordConfig.effort;
|
|
85
|
+
this.log(`Detected think keyword: ${keywordConfig.keyword}, effort=${keywordConfig.effort}`);
|
|
86
|
+
}
|
|
87
|
+
// 3. Check anthropicRequest.thinking parameter (takes precedence)
|
|
88
|
+
if (anthropicRequest.thinking) {
|
|
89
|
+
if (anthropicRequest.thinking.type === 'enabled') {
|
|
90
|
+
thinkingConfig.thinking = true;
|
|
91
|
+
this.log('Claude CLI explicitly enabled thinking (overrides budget)');
|
|
92
|
+
}
|
|
93
|
+
else if (anthropicRequest.thinking.type === 'disabled') {
|
|
94
|
+
thinkingConfig.thinking = false;
|
|
95
|
+
this.log('Claude CLI explicitly disabled thinking (overrides budget)');
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.log(`Warning: Unknown thinking type: ${anthropicRequest.thinking.type}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
this.log(`Final thinking control: ${JSON.stringify(thinkingConfig)}`);
|
|
102
|
+
// 3. Map model
|
|
103
|
+
const glmModel = this.mapModel(anthropicRequest.model);
|
|
104
|
+
// 4. Inject locale instruction before sanitization
|
|
105
|
+
const messagesWithLocale = this.localeEnforcer.injectInstruction((anthropicRequest.messages || []));
|
|
106
|
+
// 4.5. Inject reasoning instruction (if enabled or thinking requested)
|
|
107
|
+
const messagesWithReasoning = this.reasoningEnforcer.injectInstruction(messagesWithLocale, thinkingConfig);
|
|
108
|
+
// 5. Convert to OpenAI format
|
|
109
|
+
const openaiRequest = {
|
|
110
|
+
model: glmModel,
|
|
111
|
+
messages: this.sanitizeMessages(messagesWithReasoning),
|
|
112
|
+
max_tokens: this.getMaxTokens(glmModel),
|
|
113
|
+
stream: anthropicRequest.stream ?? false,
|
|
114
|
+
};
|
|
115
|
+
// 5.5. Transform tools parameter if present
|
|
116
|
+
if (anthropicRequest.tools && anthropicRequest.tools.length > 0) {
|
|
117
|
+
openaiRequest.tools = this.transformTools(anthropicRequest.tools);
|
|
118
|
+
// Always use "auto" as Z.AI doesn't support other modes
|
|
119
|
+
openaiRequest.tool_choice = 'auto';
|
|
120
|
+
this.log(`Transformed ${anthropicRequest.tools.length} tools for OpenAI format`);
|
|
121
|
+
}
|
|
122
|
+
// 6. Preserve optional parameters
|
|
123
|
+
if (anthropicRequest.temperature !== undefined) {
|
|
124
|
+
openaiRequest.temperature = anthropicRequest.temperature;
|
|
125
|
+
}
|
|
126
|
+
if (anthropicRequest.top_p !== undefined) {
|
|
127
|
+
openaiRequest.top_p = anthropicRequest.top_p;
|
|
128
|
+
}
|
|
129
|
+
// 7. Handle streaming
|
|
130
|
+
if (anthropicRequest.stream !== undefined) {
|
|
131
|
+
openaiRequest.stream = anthropicRequest.stream;
|
|
132
|
+
}
|
|
133
|
+
// 8. Inject reasoning parameters
|
|
134
|
+
this.injectReasoningParams(openaiRequest, thinkingConfig);
|
|
135
|
+
// Log transformed request
|
|
136
|
+
this.writeDebugLog('request-openai', openaiRequest);
|
|
137
|
+
return { openaiRequest, thinkingConfig };
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
const err = error;
|
|
141
|
+
console.error('[glmt-transformer] Request transformation error:', err);
|
|
142
|
+
// Return original request with warning
|
|
143
|
+
return {
|
|
144
|
+
openaiRequest: anthropicRequest,
|
|
145
|
+
thinkingConfig: { thinking: false, effort: 'medium' },
|
|
146
|
+
error: err.message,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Transform OpenAI response to Anthropic format
|
|
152
|
+
*/
|
|
153
|
+
transformResponse(openaiResponse, _thinkingConfig = { thinking: false, effort: 'medium' }) {
|
|
154
|
+
// Log original response
|
|
155
|
+
this.writeDebugLog('response-openai', openaiResponse);
|
|
156
|
+
try {
|
|
157
|
+
const choice = openaiResponse.choices?.[0];
|
|
158
|
+
if (!choice) {
|
|
159
|
+
throw new Error('No choices in OpenAI response');
|
|
160
|
+
}
|
|
161
|
+
const message = choice.message;
|
|
162
|
+
const content = [];
|
|
163
|
+
// Add thinking block if reasoning_content exists
|
|
164
|
+
if (message.reasoning_content) {
|
|
165
|
+
const length = message.reasoning_content.length;
|
|
166
|
+
const lineCount = message.reasoning_content.split('\n').length;
|
|
167
|
+
const preview = message.reasoning_content.substring(0, 100).replace(/\n/g, ' ').trim();
|
|
168
|
+
this.log(`Detected reasoning_content:`);
|
|
169
|
+
this.log(` Length: ${length} characters`);
|
|
170
|
+
this.log(` Lines: ${lineCount}`);
|
|
171
|
+
this.log(` Preview: ${preview}...`);
|
|
172
|
+
content.push({
|
|
173
|
+
type: 'thinking',
|
|
174
|
+
thinking: message.reasoning_content,
|
|
175
|
+
signature: this.generateThinkingSignature(message.reasoning_content),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
this.log('No reasoning_content in OpenAI response');
|
|
180
|
+
this.log('Note: This is expected if thinking not requested or model cannot reason');
|
|
181
|
+
}
|
|
182
|
+
// Add text content
|
|
183
|
+
if (message.content) {
|
|
184
|
+
content.push({
|
|
185
|
+
type: 'text',
|
|
186
|
+
text: message.content,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
// Handle tool_calls if present
|
|
190
|
+
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
191
|
+
message.tool_calls.forEach((toolCall) => {
|
|
192
|
+
let parsedInput;
|
|
193
|
+
try {
|
|
194
|
+
parsedInput = JSON.parse(toolCall.function.arguments || '{}');
|
|
195
|
+
}
|
|
196
|
+
catch (parseError) {
|
|
197
|
+
const err = parseError;
|
|
198
|
+
this.log(`Warning: Invalid JSON in tool arguments: ${err.message}`);
|
|
199
|
+
parsedInput = { _error: 'Invalid JSON', _raw: toolCall.function.arguments };
|
|
200
|
+
}
|
|
201
|
+
content.push({
|
|
202
|
+
type: 'tool_use',
|
|
203
|
+
id: toolCall.id,
|
|
204
|
+
name: toolCall.function.name,
|
|
205
|
+
input: parsedInput,
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
const anthropicResponse = {
|
|
210
|
+
id: openaiResponse.id || 'msg_' + Date.now(),
|
|
211
|
+
type: 'message',
|
|
212
|
+
role: 'assistant',
|
|
213
|
+
content: content,
|
|
214
|
+
model: openaiResponse.model || 'glm-4.6',
|
|
215
|
+
stop_reason: this.mapStopReason(choice.finish_reason || 'stop'),
|
|
216
|
+
usage: {
|
|
217
|
+
input_tokens: openaiResponse.usage?.prompt_tokens || 0,
|
|
218
|
+
output_tokens: openaiResponse.usage?.completion_tokens || 0,
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
// Validate transformation in verbose mode
|
|
222
|
+
if (this.verbose) {
|
|
223
|
+
const validation = this.validateTransformation(anthropicResponse);
|
|
224
|
+
this.log(`Transformation validation: ${validation.passed}/${validation.total} checks passed`);
|
|
225
|
+
if (!validation.valid) {
|
|
226
|
+
this.log(`Failed checks: ${JSON.stringify(validation.checks, null, 2)}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// Log transformed response
|
|
230
|
+
this.writeDebugLog('response-anthropic', anthropicResponse);
|
|
231
|
+
return anthropicResponse;
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
const err = error;
|
|
235
|
+
console.error('[glmt-transformer] Response transformation error:', err);
|
|
236
|
+
// Return minimal valid response
|
|
237
|
+
return {
|
|
238
|
+
id: 'msg_error_' + Date.now(),
|
|
239
|
+
type: 'message',
|
|
240
|
+
role: 'assistant',
|
|
241
|
+
content: [
|
|
242
|
+
{
|
|
243
|
+
type: 'text',
|
|
244
|
+
text: '[Transformation Error] ' + err.message,
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
model: 'glm-4.6',
|
|
248
|
+
stop_reason: 'end_turn',
|
|
249
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Sanitize messages for OpenAI API compatibility
|
|
255
|
+
*/
|
|
256
|
+
sanitizeMessages(messages) {
|
|
257
|
+
const result = [];
|
|
258
|
+
for (const msg of messages) {
|
|
259
|
+
// If content is a string, add as-is
|
|
260
|
+
if (typeof msg.content === 'string') {
|
|
261
|
+
result.push(msg);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
// If content is an array, process blocks
|
|
265
|
+
if (Array.isArray(msg.content)) {
|
|
266
|
+
// Separate tool_result blocks from other content
|
|
267
|
+
const toolResults = msg.content.filter((block) => block.type === 'tool_result');
|
|
268
|
+
const textBlocks = msg.content.filter((block) => block.type === 'text');
|
|
269
|
+
// const toolUseBlocks = msg.content.filter(block => block.type === 'tool_use');
|
|
270
|
+
// CRITICAL: Tool messages must come BEFORE user text in OpenAI API
|
|
271
|
+
for (const toolResult of toolResults) {
|
|
272
|
+
result.push({
|
|
273
|
+
role: 'tool',
|
|
274
|
+
content: typeof toolResult.content === 'string'
|
|
275
|
+
? toolResult.content
|
|
276
|
+
: JSON.stringify(toolResult.content),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
// Add text content as user/assistant message AFTER tool messages
|
|
280
|
+
if (textBlocks.length > 0) {
|
|
281
|
+
const textContent = textBlocks.length === 1
|
|
282
|
+
? textBlocks[0].text || ''
|
|
283
|
+
: textBlocks.map((b) => b.text || '').join('\n');
|
|
284
|
+
result.push({
|
|
285
|
+
role: msg.role,
|
|
286
|
+
content: textContent,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
// If no content at all, add empty message
|
|
290
|
+
if (textBlocks.length === 0 && toolResults.length === 0) {
|
|
291
|
+
result.push({
|
|
292
|
+
role: msg.role,
|
|
293
|
+
content: '',
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
// Fallback: return message as-is
|
|
299
|
+
result.push(msg);
|
|
300
|
+
}
|
|
301
|
+
return result;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Transform Anthropic tools to OpenAI tools format
|
|
305
|
+
*/
|
|
306
|
+
transformTools(anthropicTools) {
|
|
307
|
+
return anthropicTools.map((tool) => ({
|
|
308
|
+
type: 'function',
|
|
309
|
+
function: {
|
|
310
|
+
name: tool.name,
|
|
311
|
+
description: tool.description,
|
|
312
|
+
parameters: tool.input_schema || {},
|
|
313
|
+
},
|
|
314
|
+
}));
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Check if messages contain thinking control tags
|
|
318
|
+
*/
|
|
319
|
+
hasThinkingTags(messages) {
|
|
320
|
+
for (const msg of messages) {
|
|
321
|
+
if (msg.role !== 'user')
|
|
322
|
+
continue;
|
|
323
|
+
const content = msg.content;
|
|
324
|
+
if (typeof content !== 'string')
|
|
325
|
+
continue;
|
|
326
|
+
// Check for control tags
|
|
327
|
+
if (/<Thinking:(On|Off)>/i.test(content) || /<Effort:(Low|Medium|High)>/i.test(content)) {
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Extract thinking control tags from user messages
|
|
335
|
+
*/
|
|
336
|
+
extractThinkingControl(messages) {
|
|
337
|
+
const config = {
|
|
338
|
+
thinking: this.defaultThinking,
|
|
339
|
+
effort: 'medium',
|
|
340
|
+
};
|
|
341
|
+
// Scan user messages for control tags
|
|
342
|
+
for (const msg of messages) {
|
|
343
|
+
if (msg.role !== 'user')
|
|
344
|
+
continue;
|
|
345
|
+
const content = msg.content;
|
|
346
|
+
if (typeof content !== 'string')
|
|
347
|
+
continue;
|
|
348
|
+
// Check for <Thinking:On|Off>
|
|
349
|
+
const thinkingMatch = content.match(/<Thinking:(On|Off)>/i);
|
|
350
|
+
if (thinkingMatch) {
|
|
351
|
+
config.thinking = thinkingMatch[1].toLowerCase() === 'on';
|
|
352
|
+
}
|
|
353
|
+
// Check for <Effort:Low|Medium|High>
|
|
354
|
+
const effortMatch = content.match(/<Effort:(Low|Medium|High)>/i);
|
|
355
|
+
if (effortMatch) {
|
|
356
|
+
config.effort = effortMatch[1].toLowerCase();
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return config;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Generate thinking signature for Claude Code UI
|
|
363
|
+
*/
|
|
364
|
+
generateThinkingSignature(thinking) {
|
|
365
|
+
// Generate signature hash
|
|
366
|
+
const hash = crypto.createHash('sha256').update(thinking).digest('hex').substring(0, 16);
|
|
367
|
+
return {
|
|
368
|
+
type: 'thinking_signature',
|
|
369
|
+
hash: hash,
|
|
370
|
+
length: thinking.length,
|
|
371
|
+
timestamp: Date.now(),
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Detect Anthropic-style "think" keywords in user prompts
|
|
376
|
+
*/
|
|
377
|
+
detectThinkKeywords(messages) {
|
|
378
|
+
if (!messages || messages.length === 0)
|
|
379
|
+
return null;
|
|
380
|
+
// Extract text from user messages
|
|
381
|
+
const text = messages
|
|
382
|
+
.filter((m) => m.role === 'user')
|
|
383
|
+
.map((m) => {
|
|
384
|
+
if (typeof m.content === 'string')
|
|
385
|
+
return m.content;
|
|
386
|
+
if (Array.isArray(m.content)) {
|
|
387
|
+
return m.content
|
|
388
|
+
.filter((block) => block.type === 'text')
|
|
389
|
+
.map((block) => block.text || '')
|
|
390
|
+
.join(' ');
|
|
391
|
+
}
|
|
392
|
+
return '';
|
|
393
|
+
})
|
|
394
|
+
.join(' ');
|
|
395
|
+
// Priority: ultrathink > think harder > think hard > think
|
|
396
|
+
if (/\bultrathink\b/i.test(text)) {
|
|
397
|
+
return { thinking: true, effort: 'max', keyword: 'ultrathink' };
|
|
398
|
+
}
|
|
399
|
+
if (/\bthink\s+harder\b/i.test(text)) {
|
|
400
|
+
return { thinking: true, effort: 'high', keyword: 'think harder' };
|
|
401
|
+
}
|
|
402
|
+
if (/\bthink\s+hard\b/i.test(text)) {
|
|
403
|
+
return { thinking: true, effort: 'medium', keyword: 'think hard' };
|
|
404
|
+
}
|
|
405
|
+
if (/\bthink\b/i.test(text)) {
|
|
406
|
+
return { thinking: true, effort: 'low', keyword: 'think' };
|
|
407
|
+
}
|
|
408
|
+
return null; // No keywords detected
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Inject reasoning parameters into OpenAI request
|
|
412
|
+
*/
|
|
413
|
+
injectReasoningParams(openaiRequest, thinkingConfig) {
|
|
414
|
+
// Always enable sampling for temperature/top_p to work
|
|
415
|
+
openaiRequest.do_sample = true;
|
|
416
|
+
// Add thinking-specific parameters if enabled
|
|
417
|
+
if (thinkingConfig.thinking) {
|
|
418
|
+
openaiRequest.reasoning = true;
|
|
419
|
+
openaiRequest.reasoning_effort = thinkingConfig.effort;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Map Anthropic model to GLM model
|
|
424
|
+
*/
|
|
425
|
+
mapModel(_anthropicModel) {
|
|
426
|
+
// Default to GLM-4.6 (latest and most capable)
|
|
427
|
+
return 'GLM-4.6';
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Get max tokens for model
|
|
431
|
+
*/
|
|
432
|
+
getMaxTokens(model) {
|
|
433
|
+
return this.modelMaxTokens[model] || 128000;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Map OpenAI stop reason to Anthropic stop reason
|
|
437
|
+
*/
|
|
438
|
+
mapStopReason(openaiReason) {
|
|
439
|
+
const mapping = {
|
|
440
|
+
stop: 'end_turn',
|
|
441
|
+
length: 'max_tokens',
|
|
442
|
+
tool_calls: 'tool_use',
|
|
443
|
+
content_filter: 'stop_sequence',
|
|
444
|
+
};
|
|
445
|
+
return mapping[openaiReason] || 'end_turn';
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Write debug log to file
|
|
449
|
+
*/
|
|
450
|
+
writeDebugLog(type, data) {
|
|
451
|
+
if (!this.debugLog)
|
|
452
|
+
return;
|
|
453
|
+
try {
|
|
454
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('.')[0];
|
|
455
|
+
const filename = `${timestamp}-${type}.json`;
|
|
456
|
+
const filepath = path.join(this.debugLogDir, filename);
|
|
457
|
+
// Ensure directory exists
|
|
458
|
+
fs.mkdirSync(this.debugLogDir, { recursive: true });
|
|
459
|
+
// Write file (pretty-printed)
|
|
460
|
+
fs.writeFileSync(filepath, JSON.stringify(data, null, 2) + '\n', 'utf8');
|
|
461
|
+
if (this.verbose) {
|
|
462
|
+
this.log(`Debug log written: ${filepath}`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
catch (error) {
|
|
466
|
+
const err = error;
|
|
467
|
+
console.error(`[glmt-transformer] Failed to write debug log: ${err.message}`);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Validate transformed Anthropic response
|
|
472
|
+
*/
|
|
473
|
+
validateTransformation(anthropicResponse) {
|
|
474
|
+
const checks = {
|
|
475
|
+
hasContent: Boolean(anthropicResponse.content && anthropicResponse.content.length > 0),
|
|
476
|
+
hasThinking: anthropicResponse.content?.some((block) => block.type === 'thinking') || false,
|
|
477
|
+
hasText: anthropicResponse.content?.some((block) => block.type === 'text') || false,
|
|
478
|
+
validStructure: anthropicResponse.type === 'message' && anthropicResponse.role === 'assistant',
|
|
479
|
+
hasUsage: Boolean(anthropicResponse.usage),
|
|
480
|
+
};
|
|
481
|
+
const passed = Object.values(checks).filter(Boolean).length;
|
|
482
|
+
const total = Object.keys(checks).length;
|
|
483
|
+
return { checks, passed, total, valid: passed === total };
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Transform OpenAI streaming delta to Anthropic events
|
|
487
|
+
*/
|
|
488
|
+
transformDelta(openaiEvent, accumulator) {
|
|
489
|
+
const events = [];
|
|
490
|
+
// Debug logging for streaming deltas
|
|
491
|
+
if (this.debugLog && openaiEvent.data) {
|
|
492
|
+
this.writeDebugLog('delta-openai', openaiEvent.data);
|
|
493
|
+
}
|
|
494
|
+
// Handle [DONE] marker
|
|
495
|
+
if (openaiEvent.event === 'done') {
|
|
496
|
+
if (!accumulator.isFinalized()) {
|
|
497
|
+
return this.finalizeDelta(accumulator);
|
|
498
|
+
}
|
|
499
|
+
return []; // Already finalized
|
|
500
|
+
}
|
|
501
|
+
// Usage update (appears in final chunk, may be before choice data)
|
|
502
|
+
if (openaiEvent.data?.usage) {
|
|
503
|
+
accumulator.updateUsage(openaiEvent.data.usage);
|
|
504
|
+
// If we have both usage AND finish_reason, finalize immediately
|
|
505
|
+
if (accumulator.getFinishReason()) {
|
|
506
|
+
events.push(...this.finalizeDelta(accumulator));
|
|
507
|
+
return events;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const choice = openaiEvent.data?.choices?.[0];
|
|
511
|
+
if (!choice)
|
|
512
|
+
return events;
|
|
513
|
+
const delta = choice.delta;
|
|
514
|
+
if (!delta)
|
|
515
|
+
return events;
|
|
516
|
+
// Message start
|
|
517
|
+
if (!accumulator.isMessageStarted()) {
|
|
518
|
+
if (openaiEvent.data?.model) {
|
|
519
|
+
accumulator.setModel(openaiEvent.data.model);
|
|
520
|
+
}
|
|
521
|
+
events.push(this.createMessageStartEvent(accumulator));
|
|
522
|
+
accumulator.setMessageStarted(true);
|
|
523
|
+
}
|
|
524
|
+
// Role
|
|
525
|
+
if (delta.role) {
|
|
526
|
+
accumulator.setRole(delta.role);
|
|
527
|
+
}
|
|
528
|
+
// Reasoning content delta
|
|
529
|
+
if (delta.reasoning_content) {
|
|
530
|
+
const currentBlock = accumulator.getCurrentBlock();
|
|
531
|
+
if (this.debugMode) {
|
|
532
|
+
console.error(`[GLMT-DEBUG] Reasoning delta: ${delta.reasoning_content.length} chars`);
|
|
533
|
+
console.error(`[GLMT-DEBUG] Current block: ${currentBlock?.type || 'none'}, index: ${currentBlock?.index ?? 'N/A'}`);
|
|
534
|
+
}
|
|
535
|
+
if (!currentBlock || currentBlock.type !== 'thinking') {
|
|
536
|
+
// Start thinking block
|
|
537
|
+
const block = accumulator.startBlock('thinking');
|
|
538
|
+
events.push(this.createContentBlockStartEvent(block));
|
|
539
|
+
if (this.debugMode) {
|
|
540
|
+
console.error(`[GLMT-DEBUG] Started new thinking block ${block.index}`);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
accumulator.addDelta(delta.reasoning_content);
|
|
544
|
+
const currentThinkingBlock = accumulator.getCurrentBlock();
|
|
545
|
+
if (currentThinkingBlock) {
|
|
546
|
+
events.push(this.createThinkingDeltaEvent(currentThinkingBlock, delta.reasoning_content));
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
// Text content delta
|
|
550
|
+
if (delta.content) {
|
|
551
|
+
const currentBlock = accumulator.getCurrentBlock();
|
|
552
|
+
// Close thinking block if transitioning from thinking to text
|
|
553
|
+
if (currentBlock && currentBlock.type === 'thinking' && !currentBlock.stopped) {
|
|
554
|
+
const signatureEvent = this.createSignatureDeltaEvent(currentBlock);
|
|
555
|
+
if (signatureEvent) {
|
|
556
|
+
events.push(signatureEvent);
|
|
557
|
+
}
|
|
558
|
+
events.push(this.createContentBlockStopEvent(currentBlock));
|
|
559
|
+
accumulator.stopCurrentBlock();
|
|
560
|
+
}
|
|
561
|
+
if (!accumulator.getCurrentBlock() || accumulator.getCurrentBlock()?.type !== 'text') {
|
|
562
|
+
// Start text block
|
|
563
|
+
const block = accumulator.startBlock('text');
|
|
564
|
+
events.push(this.createContentBlockStartEvent(block));
|
|
565
|
+
}
|
|
566
|
+
accumulator.addDelta(delta.content);
|
|
567
|
+
const currentTextBlock = accumulator.getCurrentBlock();
|
|
568
|
+
if (currentTextBlock) {
|
|
569
|
+
events.push(this.createTextDeltaEvent(currentTextBlock, delta.content));
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
// Check for planning loop
|
|
573
|
+
if (accumulator.checkForLoop()) {
|
|
574
|
+
this.log('WARNING: Planning loop detected - 3 consecutive thinking blocks with no tool calls');
|
|
575
|
+
this.log('Forcing early finalization to prevent unbounded planning');
|
|
576
|
+
// Close current block if any
|
|
577
|
+
const currentBlock = accumulator.getCurrentBlock();
|
|
578
|
+
if (currentBlock && !currentBlock.stopped) {
|
|
579
|
+
if (currentBlock.type === 'thinking') {
|
|
580
|
+
const signatureEvent = this.createSignatureDeltaEvent(currentBlock);
|
|
581
|
+
if (signatureEvent) {
|
|
582
|
+
events.push(signatureEvent);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
events.push(this.createContentBlockStopEvent(currentBlock));
|
|
586
|
+
accumulator.stopCurrentBlock();
|
|
587
|
+
}
|
|
588
|
+
// Force finalization
|
|
589
|
+
events.push(...this.finalizeDelta(accumulator));
|
|
590
|
+
return events;
|
|
591
|
+
}
|
|
592
|
+
// Tool calls deltas
|
|
593
|
+
if (delta.tool_calls && delta.tool_calls.length > 0) {
|
|
594
|
+
// Close current content block ONCE before processing any tool calls
|
|
595
|
+
const currentBlock = accumulator.getCurrentBlock();
|
|
596
|
+
if (currentBlock && !currentBlock.stopped) {
|
|
597
|
+
if (currentBlock.type === 'thinking') {
|
|
598
|
+
const signatureEvent = this.createSignatureDeltaEvent(currentBlock);
|
|
599
|
+
if (signatureEvent) {
|
|
600
|
+
events.push(signatureEvent);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
events.push(this.createContentBlockStopEvent(currentBlock));
|
|
604
|
+
accumulator.stopCurrentBlock();
|
|
605
|
+
}
|
|
606
|
+
// Process each tool call delta
|
|
607
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
608
|
+
// Track tool call state
|
|
609
|
+
const isNewToolCall = !accumulator.hasToolCall(toolCallDelta.index);
|
|
610
|
+
accumulator.addToolCallDelta(toolCallDelta);
|
|
611
|
+
// Emit tool use events (start + input_json deltas)
|
|
612
|
+
if (isNewToolCall) {
|
|
613
|
+
// Start new tool_use block in accumulator
|
|
614
|
+
const block = accumulator.startBlock('tool_use');
|
|
615
|
+
const toolCall = accumulator.getToolCall(toolCallDelta.index);
|
|
616
|
+
events.push({
|
|
617
|
+
event: 'content_block_start',
|
|
618
|
+
data: {
|
|
619
|
+
type: 'content_block_start',
|
|
620
|
+
index: block.index,
|
|
621
|
+
content_block: {
|
|
622
|
+
type: 'tool_use',
|
|
623
|
+
id: toolCall?.id || `tool_${toolCallDelta.index}`,
|
|
624
|
+
name: toolCall?.function?.name || '',
|
|
625
|
+
},
|
|
626
|
+
},
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
// Emit input_json delta if arguments present
|
|
630
|
+
if (toolCallDelta.function?.arguments) {
|
|
631
|
+
const currentToolBlock = accumulator.getCurrentBlock();
|
|
632
|
+
if (currentToolBlock && currentToolBlock.type === 'tool_use') {
|
|
633
|
+
events.push({
|
|
634
|
+
event: 'content_block_delta',
|
|
635
|
+
data: {
|
|
636
|
+
type: 'content_block_delta',
|
|
637
|
+
index: currentToolBlock.index,
|
|
638
|
+
delta: {
|
|
639
|
+
type: 'input_json_delta',
|
|
640
|
+
partial_json: toolCallDelta.function.arguments,
|
|
641
|
+
},
|
|
642
|
+
},
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
// Finish reason
|
|
649
|
+
if (choice.finish_reason) {
|
|
650
|
+
accumulator.setFinishReason(choice.finish_reason);
|
|
651
|
+
// If we have both finish_reason AND usage, finalize immediately
|
|
652
|
+
if (accumulator.hasUsageReceived()) {
|
|
653
|
+
events.push(...this.finalizeDelta(accumulator));
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
// Debug logging for generated events
|
|
657
|
+
if (this.debugLog && events.length > 0) {
|
|
658
|
+
this.writeDebugLog('delta-anthropic-events', {
|
|
659
|
+
events,
|
|
660
|
+
accumulator: accumulator.getSummary(),
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
return events;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Finalize streaming and generate closing events
|
|
667
|
+
*/
|
|
668
|
+
finalizeDelta(accumulator) {
|
|
669
|
+
if (accumulator.isFinalized()) {
|
|
670
|
+
return []; // Already finalized
|
|
671
|
+
}
|
|
672
|
+
const events = [];
|
|
673
|
+
// Close current content block if any
|
|
674
|
+
const currentBlock = accumulator.getCurrentBlock();
|
|
675
|
+
if (currentBlock && !currentBlock.stopped) {
|
|
676
|
+
if (currentBlock.type === 'thinking') {
|
|
677
|
+
const signatureEvent = this.createSignatureDeltaEvent(currentBlock);
|
|
678
|
+
if (signatureEvent) {
|
|
679
|
+
events.push(signatureEvent);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
events.push(this.createContentBlockStopEvent(currentBlock));
|
|
683
|
+
accumulator.stopCurrentBlock();
|
|
684
|
+
}
|
|
685
|
+
// Message delta (stop reason + usage)
|
|
686
|
+
events.push({
|
|
687
|
+
event: 'message_delta',
|
|
688
|
+
data: {
|
|
689
|
+
type: 'message_delta',
|
|
690
|
+
delta: {
|
|
691
|
+
stop_reason: this.mapStopReason(accumulator.getFinishReason() || 'stop'),
|
|
692
|
+
},
|
|
693
|
+
usage: {
|
|
694
|
+
input_tokens: accumulator.getInputTokens(),
|
|
695
|
+
output_tokens: accumulator.getOutputTokens(),
|
|
696
|
+
},
|
|
697
|
+
},
|
|
698
|
+
});
|
|
699
|
+
// Message stop
|
|
700
|
+
events.push({
|
|
701
|
+
event: 'message_stop',
|
|
702
|
+
data: {
|
|
703
|
+
type: 'message_stop',
|
|
704
|
+
},
|
|
705
|
+
});
|
|
706
|
+
accumulator.setFinalized(true);
|
|
707
|
+
return events;
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Create message_start event
|
|
711
|
+
*/
|
|
712
|
+
createMessageStartEvent(accumulator) {
|
|
713
|
+
return {
|
|
714
|
+
event: 'message_start',
|
|
715
|
+
data: {
|
|
716
|
+
type: 'message_start',
|
|
717
|
+
message: {
|
|
718
|
+
id: accumulator.getMessageId(),
|
|
719
|
+
type: 'message',
|
|
720
|
+
role: accumulator.getRole(),
|
|
721
|
+
content: [],
|
|
722
|
+
model: accumulator.getModel() || 'glm-4.6',
|
|
723
|
+
stop_reason: null,
|
|
724
|
+
usage: {
|
|
725
|
+
input_tokens: accumulator.getInputTokens(),
|
|
726
|
+
output_tokens: 0,
|
|
727
|
+
},
|
|
728
|
+
},
|
|
729
|
+
},
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Create content_block_start event
|
|
734
|
+
*/
|
|
735
|
+
createContentBlockStartEvent(block) {
|
|
736
|
+
return {
|
|
737
|
+
event: 'content_block_start',
|
|
738
|
+
data: {
|
|
739
|
+
type: 'content_block_start',
|
|
740
|
+
index: block.index,
|
|
741
|
+
content_block: {
|
|
742
|
+
type: block.type,
|
|
743
|
+
[block.type]: '',
|
|
744
|
+
},
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
/**
|
|
749
|
+
* Create thinking_delta event
|
|
750
|
+
*/
|
|
751
|
+
createThinkingDeltaEvent(block, delta) {
|
|
752
|
+
return {
|
|
753
|
+
event: 'content_block_delta',
|
|
754
|
+
data: {
|
|
755
|
+
type: 'content_block_delta',
|
|
756
|
+
index: block.index,
|
|
757
|
+
delta: {
|
|
758
|
+
type: 'thinking_delta',
|
|
759
|
+
thinking: delta,
|
|
760
|
+
},
|
|
761
|
+
},
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Create text_delta event
|
|
766
|
+
*/
|
|
767
|
+
createTextDeltaEvent(block, delta) {
|
|
768
|
+
return {
|
|
769
|
+
event: 'content_block_delta',
|
|
770
|
+
data: {
|
|
771
|
+
type: 'content_block_delta',
|
|
772
|
+
index: block.index,
|
|
773
|
+
delta: {
|
|
774
|
+
type: 'text_delta',
|
|
775
|
+
text: delta,
|
|
776
|
+
},
|
|
777
|
+
},
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Create thinking signature delta event
|
|
782
|
+
*/
|
|
783
|
+
createSignatureDeltaEvent(block) {
|
|
784
|
+
// FIX: Guard against empty content (signature timing race)
|
|
785
|
+
if (!block.content || block.content.length === 0) {
|
|
786
|
+
if (this.verbose) {
|
|
787
|
+
this.log(`WARNING: Skipping signature for empty thinking block ${block.index}`);
|
|
788
|
+
this.log(`This indicates a race condition - signature requested before content accumulated`);
|
|
789
|
+
}
|
|
790
|
+
return null;
|
|
791
|
+
}
|
|
792
|
+
const signature = this.generateThinkingSignature(block.content);
|
|
793
|
+
if (this.verbose) {
|
|
794
|
+
this.log(`Generating signature for block ${block.index}: ${block.content.length} chars`);
|
|
795
|
+
}
|
|
796
|
+
return {
|
|
797
|
+
event: 'content_block_delta',
|
|
798
|
+
data: {
|
|
799
|
+
type: 'content_block_delta',
|
|
800
|
+
index: block.index,
|
|
801
|
+
delta: {
|
|
802
|
+
type: 'thinking_signature_delta',
|
|
803
|
+
signature: signature,
|
|
804
|
+
},
|
|
805
|
+
},
|
|
806
|
+
};
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Create content_block_stop event
|
|
810
|
+
*/
|
|
811
|
+
createContentBlockStopEvent(block) {
|
|
812
|
+
return {
|
|
813
|
+
event: 'content_block_stop',
|
|
814
|
+
data: {
|
|
815
|
+
type: 'content_block_stop',
|
|
816
|
+
index: block.index,
|
|
817
|
+
},
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
/**
|
|
821
|
+
* Log message if verbose
|
|
822
|
+
*/
|
|
823
|
+
log(message) {
|
|
824
|
+
if (this.verbose) {
|
|
825
|
+
const timestamp = new Date().toTimeString().split(' ')[0]; // HH:MM:SS
|
|
826
|
+
console.error(`[glmt-transformer] [${timestamp}] ${message}`);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
exports.GlmtTransformer = GlmtTransformer;
|
|
831
|
+
exports.default = GlmtTransformer;
|
|
832
|
+
//# sourceMappingURL=glmt-transformer.js.map
|