tachibot-mcp 2.0.2
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/.env.example +260 -0
- package/CHANGELOG.md +54 -0
- package/CODE_OF_CONDUCT.md +56 -0
- package/CONTRIBUTING.md +54 -0
- package/Dockerfile +36 -0
- package/LICENSE +644 -0
- package/README.md +201 -0
- package/SECURITY.md +95 -0
- package/dist/personality/komaai-expressions.js +12 -0
- package/dist/profiles/balanced.json +33 -0
- package/dist/profiles/code_focus.json +33 -0
- package/dist/profiles/full.json +33 -0
- package/dist/profiles/minimal.json +33 -0
- package/dist/profiles/research_power.json +33 -0
- package/dist/scripts/build-profiles.js +46 -0
- package/dist/src/application/services/focus/FocusModeRegistry.js +46 -0
- package/dist/src/application/services/focus/FocusTool.service.js +109 -0
- package/dist/src/application/services/focus/ModeRegistry.js +46 -0
- package/dist/src/application/services/focus/modes/focus-deep.mode.js +27 -0
- package/dist/src/application/services/focus/modes/status.mode.js +50 -0
- package/dist/src/application/services/focus/modes/tachibot-status.mode.js +50 -0
- package/dist/src/collaborative-orchestrator.js +391 -0
- package/dist/src/config/model-constants.js +188 -0
- package/dist/src/config/model-defaults.js +57 -0
- package/dist/src/config/model-preferences.js +382 -0
- package/dist/src/config/timeout-config.js +130 -0
- package/dist/src/config.js +173 -0
- package/dist/src/domain/interfaces/IFocusMode.js +5 -0
- package/dist/src/domain/interfaces/IProvider.js +6 -0
- package/dist/src/domain/interfaces/ITool.js +5 -0
- package/dist/src/focus-deep.js +245 -0
- package/dist/src/infrastructure/ascii/art/robots.ascii.js +16 -0
- package/dist/src/mcp-client.js +90 -0
- package/dist/src/memory/index.js +17 -0
- package/dist/src/memory/memory-config.js +135 -0
- package/dist/src/memory/memory-interface.js +174 -0
- package/dist/src/memory/memory-manager.js +383 -0
- package/dist/src/memory/providers/devlog-provider.js +385 -0
- package/dist/src/memory/providers/hybrid-provider.js +399 -0
- package/dist/src/memory/providers/local-provider.js +388 -0
- package/dist/src/memory/providers/mem0-provider.js +337 -0
- package/dist/src/modes/architect.js +477 -0
- package/dist/src/modes/auditor.js +362 -0
- package/dist/src/modes/challenger.js +841 -0
- package/dist/src/modes/code-reviewer.js +382 -0
- package/dist/src/modes/commit-guardian.js +424 -0
- package/dist/src/modes/documentation-writer.js +572 -0
- package/dist/src/modes/scout.js +587 -0
- package/dist/src/modes/shared/helpers/challenger-helpers.js +454 -0
- package/dist/src/modes/shared/helpers/index.js +17 -0
- package/dist/src/modes/shared/helpers/scout-helpers.js +270 -0
- package/dist/src/modes/shared/helpers/verifier-helpers.js +332 -0
- package/dist/src/modes/test-architect.js +767 -0
- package/dist/src/modes/verifier.js +378 -0
- package/dist/src/monitoring/performance-monitor.js +435 -0
- package/dist/src/optimization/batch-executor.js +121 -0
- package/dist/src/optimization/context-pruner.js +196 -0
- package/dist/src/optimization/cost-monitor.js +338 -0
- package/dist/src/optimization/index.js +65 -0
- package/dist/src/optimization/model-router.js +264 -0
- package/dist/src/optimization/result-cache.js +114 -0
- package/dist/src/optimization/token-optimizer.js +257 -0
- package/dist/src/optimization/token-tracker.js +118 -0
- package/dist/src/orchestrator-instructions.js +128 -0
- package/dist/src/orchestrator-lite.js +139 -0
- package/dist/src/orchestrator.js +191 -0
- package/dist/src/orchestrators/collaborative/interfaces/IToolExecutionEngine.js +1 -0
- package/dist/src/orchestrators/collaborative/interfaces/IToolExecutionStrategy.js +5 -0
- package/dist/src/orchestrators/collaborative/interfaces/IVisualizationRenderer.js +1 -0
- package/dist/src/orchestrators/collaborative/registries/ModelProviderRegistry.js +95 -0
- package/dist/src/orchestrators/collaborative/registries/ToolAdapterRegistry.js +64 -0
- package/dist/src/orchestrators/collaborative/services/tool-execution/ToolExecutionService.js +502 -0
- package/dist/src/orchestrators/collaborative/services/visualization/VisualizationService.js +206 -0
- package/dist/src/orchestrators/collaborative/types/session-types.js +5 -0
- package/dist/src/profiles/balanced.js +37 -0
- package/dist/src/profiles/code_focus.js +37 -0
- package/dist/src/profiles/debug_intensive.js +59 -0
- package/dist/src/profiles/full.js +37 -0
- package/dist/src/profiles/minimal.js +37 -0
- package/dist/src/profiles/research_code.js +59 -0
- package/dist/src/profiles/research_power.js +37 -0
- package/dist/src/profiles/types.js +5 -0
- package/dist/src/profiles/workflow_builder.js +53 -0
- package/dist/src/prompt-engineer-lite.js +78 -0
- package/dist/src/prompt-engineer.js +399 -0
- package/dist/src/reasoning-chain.js +508 -0
- package/dist/src/sequential-thinking.js +291 -0
- package/dist/src/server-diagnostic.js +74 -0
- package/dist/src/server-raw.js +158 -0
- package/dist/src/server-simple.js +58 -0
- package/dist/src/server.js +514 -0
- package/dist/src/session/session-logger.js +617 -0
- package/dist/src/session/session-manager.js +571 -0
- package/dist/src/session/session-tools.js +400 -0
- package/dist/src/tools/advanced-modes.js +200 -0
- package/dist/src/tools/claude-integration.js +356 -0
- package/dist/src/tools/consolidated/ai-router.js +174 -0
- package/dist/src/tools/consolidated/ai-tool.js +48 -0
- package/dist/src/tools/consolidated/brainstorm-tool.js +87 -0
- package/dist/src/tools/consolidated/environment-detector.js +80 -0
- package/dist/src/tools/consolidated/index.js +50 -0
- package/dist/src/tools/consolidated/search-tool.js +110 -0
- package/dist/src/tools/consolidated/workflow-tool.js +238 -0
- package/dist/src/tools/gemini-tools.js +329 -0
- package/dist/src/tools/grok-enhanced.js +376 -0
- package/dist/src/tools/grok-tools.js +299 -0
- package/dist/src/tools/lmstudio-tools.js +223 -0
- package/dist/src/tools/openai-tools.js +498 -0
- package/dist/src/tools/openrouter-tools.js +317 -0
- package/dist/src/tools/optimized-wrapper.js +204 -0
- package/dist/src/tools/perplexity-tools.js +294 -0
- package/dist/src/tools/pingpong-tool.js +343 -0
- package/dist/src/tools/qwen-wrapper.js +74 -0
- package/dist/src/tools/tool-router.js +444 -0
- package/dist/src/tools/unified-ai-provider.js +260 -0
- package/dist/src/tools/workflow-runner.js +425 -0
- package/dist/src/tools/workflow-validator-tool.js +107 -0
- package/dist/src/types.js +23 -0
- package/dist/src/utils/input-validator.js +130 -0
- package/dist/src/utils/model-router.js +91 -0
- package/dist/src/utils/progress-stream.js +255 -0
- package/dist/src/utils/provider-router.js +88 -0
- package/dist/src/utils/smart-api-client.js +146 -0
- package/dist/src/utils/table-builder.js +218 -0
- package/dist/src/utils/timestamp-formatter.js +134 -0
- package/dist/src/utils/tool-compressor.js +257 -0
- package/dist/src/utils/tool-config.js +201 -0
- package/dist/src/validators/dependency-graph-validator.js +147 -0
- package/dist/src/validators/interpolation-validator.js +222 -0
- package/dist/src/validators/output-usage-validator.js +151 -0
- package/dist/src/validators/syntax-validator.js +102 -0
- package/dist/src/validators/tool-registry-validator.js +123 -0
- package/dist/src/validators/tool-types.js +97 -0
- package/dist/src/validators/types.js +8 -0
- package/dist/src/validators/workflow-validator.js +134 -0
- package/dist/src/visualizer-lite.js +42 -0
- package/dist/src/visualizer.js +179 -0
- package/dist/src/workflows/circuit-breaker.js +199 -0
- package/dist/src/workflows/custom-workflows.js +451 -0
- package/dist/src/workflows/engine/AutoSynthesizer.js +97 -0
- package/dist/src/workflows/engine/StepParameterResolver.js +74 -0
- package/dist/src/workflows/engine/VariableInterpolator.js +123 -0
- package/dist/src/workflows/engine/WorkflowDiscovery.js +125 -0
- package/dist/src/workflows/engine/WorkflowExecutionEngine.js +485 -0
- package/dist/src/workflows/engine/WorkflowExecutor.js +113 -0
- package/dist/src/workflows/engine/WorkflowFileManager.js +244 -0
- package/dist/src/workflows/engine/WorkflowHelpers.js +114 -0
- package/dist/src/workflows/engine/WorkflowOutputFormatter.js +83 -0
- package/dist/src/workflows/engine/events/WorkflowEventBus.js +132 -0
- package/dist/src/workflows/engine/events/interfaces/IEventBus.js +5 -0
- package/dist/src/workflows/engine/handlers/ErrorRecoveryHandler.js +162 -0
- package/dist/src/workflows/engine/handlers/PromptEnhancementHandler.js +115 -0
- package/dist/src/workflows/engine/handlers/SessionPersistenceHandler.js +167 -0
- package/dist/src/workflows/engine/handlers/StepExecutionHandler.js +231 -0
- package/dist/src/workflows/engine/handlers/ToolInvocationHandler.js +46 -0
- package/dist/src/workflows/engine/interfaces/IAutoSynthesizer.js +5 -0
- package/dist/src/workflows/engine/interfaces/IStepParameterResolver.js +5 -0
- package/dist/src/workflows/engine/interfaces/IVariableInterpolator.js +5 -0
- package/dist/src/workflows/engine/interfaces/IWorkflowDiscovery.js +4 -0
- package/dist/src/workflows/engine/interfaces/IWorkflowFileManager.js +5 -0
- package/dist/src/workflows/engine/interfaces/IWorkflowOutputFormatter.js +5 -0
- package/dist/src/workflows/engine/state/WorkflowStateMachine.js +194 -0
- package/dist/src/workflows/engine/state/interfaces/IStateMachine.js +17 -0
- package/dist/src/workflows/fallback-strategies.js +373 -0
- package/dist/src/workflows/message-queue.js +455 -0
- package/dist/src/workflows/model-router.js +189 -0
- package/dist/src/workflows/orchestrator-examples.js +174 -0
- package/dist/src/workflows/orchestrator-integration.js +200 -0
- package/dist/src/workflows/self-healing.js +524 -0
- package/dist/src/workflows/tool-mapper.js +407 -0
- package/dist/src/workflows/tool-orchestrator.js +796 -0
- package/dist/src/workflows/workflow-engine.js +573 -0
- package/dist/src/workflows/workflow-parser.js +283 -0
- package/dist/src/workflows/workflow-types.js +95 -0
- package/dist/src/workflows.js +568 -0
- package/dist/test-workflow-file-output.js +93 -0
- package/docs/API_KEYS.md +570 -0
- package/docs/CLAUDE_CODE_SETUP.md +181 -0
- package/docs/CLAUDE_DESKTOP_MANUAL.md +127 -0
- package/docs/CONFIGURATION.md +745 -0
- package/docs/FOCUS_MODES.md +240 -0
- package/docs/INSTALLATION_BOTH.md +145 -0
- package/docs/TERMS.md +352 -0
- package/docs/TOOLS_REFERENCE.md +1622 -0
- package/docs/TOOL_PARAMETERS.md +496 -0
- package/docs/TOOL_PROFILES.md +236 -0
- package/docs/WORKFLOWS.md +987 -0
- package/docs/WORKFLOW_OUTPUT.md +198 -0
- package/docs/WORKFLOW_PROGRESS_TRACKING.md +305 -0
- package/docs/workflows/design-brainstorm.md +335 -0
- package/package.json +97 -0
- package/profiles/balanced.json +37 -0
- package/profiles/code_focus.json +37 -0
- package/profiles/debug_intensive.json +34 -0
- package/profiles/full.json +37 -0
- package/profiles/minimal.json +37 -0
- package/profiles/research_power.json +37 -0
- package/profiles/workflow_builder.json +37 -0
- package/smithery.yaml +66 -0
- package/start.sh +8 -0
- package/tools.config.json +81 -0
- package/tsconfig.json +18 -0
- package/workflows/accessibility-code-audit.yaml +92 -0
- package/workflows/code-architecture-review.yaml +202 -0
- package/workflows/code-review.yaml +142 -0
- package/workflows/core/iterative-problem-solver.yaml +283 -0
- package/workflows/creative-brainstorm-yaml.yaml +215 -0
- package/workflows/pingpong.yaml +141 -0
- package/workflows/system/README.md +412 -0
- package/workflows/system/challenger.yaml +175 -0
- package/workflows/system/scout.yaml +164 -0
- package/workflows/system/verifier.yaml +133 -0
- package/workflows/ultra-creative-brainstorm.yaml +318 -0
- package/workflows/ux-research-flow.yaml +92 -0
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenRouter Tools Implementation
|
|
3
|
+
* Provides access to Qwen3, QwQ, and other models via OpenRouter
|
|
4
|
+
*/
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
// NOTE: dotenv is loaded in server.ts before any imports
|
|
7
|
+
// No need to reload here - just read from process.env
|
|
8
|
+
// OpenRouter API configuration
|
|
9
|
+
const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY;
|
|
10
|
+
const OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions";
|
|
11
|
+
// Available OpenRouter models (verified names)
|
|
12
|
+
export var OpenRouterModel;
|
|
13
|
+
(function (OpenRouterModel) {
|
|
14
|
+
// Qwen models - Premium models with credits
|
|
15
|
+
OpenRouterModel["QWEN3_CODER_PLUS"] = "qwen/qwen3-coder-plus";
|
|
16
|
+
OpenRouterModel["QWEN3_CODER"] = "qwen/qwen3-coder";
|
|
17
|
+
OpenRouterModel["QWEN3_30B"] = "qwen/qwen3-30b-a3b-instruct-2507";
|
|
18
|
+
OpenRouterModel["QWEN3_235B_THINKING"] = "qwen/qwen3-235b-a22b-thinking-2507";
|
|
19
|
+
OpenRouterModel["QWQ_32B"] = "qwen/qwq-32b";
|
|
20
|
+
// Moonshot AI models
|
|
21
|
+
OpenRouterModel["KIMI_K2_THINKING"] = "moonshotai/kimi-k2-thinking";
|
|
22
|
+
})(OpenRouterModel || (OpenRouterModel = {}));
|
|
23
|
+
/**
|
|
24
|
+
* Call OpenRouter API
|
|
25
|
+
*/
|
|
26
|
+
export async function callOpenRouter(messages, model = OpenRouterModel.QWEN3_CODER, temperature = 0.7, maxTokens = 20480 // 20k for comprehensive code analysis
|
|
27
|
+
) {
|
|
28
|
+
if (!OPENROUTER_API_KEY) {
|
|
29
|
+
return `[OpenRouter API key not configured. Add OPENROUTER_API_KEY to .env file]`;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
// Kimi K2 Thinking requires special reasoning parameters
|
|
33
|
+
const isKimiThinking = model === OpenRouterModel.KIMI_K2_THINKING;
|
|
34
|
+
const requestBody = {
|
|
35
|
+
model,
|
|
36
|
+
messages,
|
|
37
|
+
temperature,
|
|
38
|
+
max_tokens: maxTokens,
|
|
39
|
+
stream: false
|
|
40
|
+
};
|
|
41
|
+
// Kimi K2 Thinking has built-in reasoning - no special params needed
|
|
42
|
+
// OpenRouter auto-enables reasoning for this model
|
|
43
|
+
const response = await fetch(OPENROUTER_API_URL, {
|
|
44
|
+
method: "POST",
|
|
45
|
+
headers: {
|
|
46
|
+
"Authorization": `Bearer ${OPENROUTER_API_KEY}`,
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
"HTTP-Referer": "https://tachibot-mcp.local",
|
|
49
|
+
"X-Title": "Tachibot MCP Server"
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(requestBody)
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const error = await response.text();
|
|
55
|
+
throw new Error(`OpenRouter API error: ${response.statusText} - ${error}`);
|
|
56
|
+
}
|
|
57
|
+
const data = await response.json();
|
|
58
|
+
return data.choices?.[0]?.message?.content || "No response from OpenRouter";
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
return `[OpenRouter error: ${error instanceof Error ? error.message : String(error)}]`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Qwen Coder Tool
|
|
66
|
+
* Advanced code generation with 480B MoE model
|
|
67
|
+
*/
|
|
68
|
+
export const qwenCoderTool = {
|
|
69
|
+
name: "qwen_coder",
|
|
70
|
+
description: "Code generation",
|
|
71
|
+
parameters: z.object({
|
|
72
|
+
task: z.enum(["generate", "review", "optimize", "debug", "refactor", "explain"]),
|
|
73
|
+
code: z.string().optional(),
|
|
74
|
+
requirements: z.string().optional().default(""), // Changed: Make optional with default
|
|
75
|
+
language: z.string().optional(),
|
|
76
|
+
useFree: z.boolean().optional().default(false)
|
|
77
|
+
}),
|
|
78
|
+
execute: async (args, { log }) => {
|
|
79
|
+
const taskPrompts = {
|
|
80
|
+
generate: "Generate new code according to requirements",
|
|
81
|
+
review: "Review code for quality, bugs, and improvements",
|
|
82
|
+
optimize: "Optimize code for performance and efficiency",
|
|
83
|
+
debug: "Debug and fix issues in the code",
|
|
84
|
+
refactor: "Refactor code for better structure and maintainability",
|
|
85
|
+
explain: "Explain how the code works in detail"
|
|
86
|
+
};
|
|
87
|
+
const systemPrompt = `You are Qwen3-Coder, an advanced code generation model.
|
|
88
|
+
Task: ${taskPrompts[args.task]}
|
|
89
|
+
${args.language ? `Language: ${args.language}` : ''}
|
|
90
|
+
Focus on: clean code, best practices, performance, and maintainability.`;
|
|
91
|
+
const requirementsText = args.requirements || "Analyze and provide insights";
|
|
92
|
+
const userPrompt = args.code
|
|
93
|
+
? `Code:\n\`\`\`${args.language || ''}\n${args.code}\n\`\`\`\n\nRequirements: ${requirementsText}`
|
|
94
|
+
: requirementsText;
|
|
95
|
+
const messages = [
|
|
96
|
+
{ role: "system", content: systemPrompt },
|
|
97
|
+
{ role: "user", content: userPrompt }
|
|
98
|
+
];
|
|
99
|
+
const model = args.useFree === true ? OpenRouterModel.QWEN3_30B : OpenRouterModel.QWEN3_CODER_PLUS;
|
|
100
|
+
return await callOpenRouter(messages, model, 0.2, 8000);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* QwQ Reasoning Tool
|
|
105
|
+
* Deep reasoning with QwQ-32B model
|
|
106
|
+
*/
|
|
107
|
+
export const qwqReasoningTool = {
|
|
108
|
+
name: "qwq_reason",
|
|
109
|
+
description: "Deep reasoning",
|
|
110
|
+
parameters: z.object({
|
|
111
|
+
problem: z.string(),
|
|
112
|
+
context: z.string().optional(),
|
|
113
|
+
approach: z.enum(["step-by-step", "mathematical", "logical", "creative"])
|
|
114
|
+
.optional()
|
|
115
|
+
.default("step-by-step"),
|
|
116
|
+
useFree: z.boolean().optional().default(true)
|
|
117
|
+
}),
|
|
118
|
+
execute: async (args, { log }) => {
|
|
119
|
+
const approachPrompts = {
|
|
120
|
+
"step-by-step": "Break down the problem and solve it step by step",
|
|
121
|
+
mathematical: "Apply mathematical reasoning and proofs",
|
|
122
|
+
logical: "Use formal logic and deductive reasoning",
|
|
123
|
+
creative: "Think creatively and explore unconventional solutions"
|
|
124
|
+
};
|
|
125
|
+
const messages = [
|
|
126
|
+
{
|
|
127
|
+
role: "system",
|
|
128
|
+
content: `You are QwQ, specialized in deep reasoning and problem-solving.
|
|
129
|
+
${approachPrompts[args.approach || 'step-by-step']}.
|
|
130
|
+
Show your thinking process clearly.
|
|
131
|
+
${args.context ? `Context: ${args.context}` : ''}`
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
role: "user",
|
|
135
|
+
content: args.problem
|
|
136
|
+
}
|
|
137
|
+
];
|
|
138
|
+
const model = args.useFree !== false ? OpenRouterModel.QWQ_32B : OpenRouterModel.QWQ_32B;
|
|
139
|
+
return await callOpenRouter(messages, model, 0.3, 6000);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Qwen General Tool
|
|
144
|
+
* General-purpose assistance with Qwen3-32B
|
|
145
|
+
*/
|
|
146
|
+
export const qwenGeneralTool = {
|
|
147
|
+
name: "qwen_general",
|
|
148
|
+
description: "General-purpose assistance with Qwen3-32B",
|
|
149
|
+
parameters: z.object({
|
|
150
|
+
query: z.string(),
|
|
151
|
+
mode: z.enum(["chat", "analysis", "creative", "technical"])
|
|
152
|
+
.optional()
|
|
153
|
+
.default("chat"),
|
|
154
|
+
useFree: z.boolean().optional().default(true)
|
|
155
|
+
}),
|
|
156
|
+
execute: async (args, { log }) => {
|
|
157
|
+
const modePrompts = {
|
|
158
|
+
chat: "Provide helpful, conversational responses",
|
|
159
|
+
analysis: "Provide detailed analysis and insights",
|
|
160
|
+
creative: "Be creative and innovative in your response",
|
|
161
|
+
technical: "Focus on technical accuracy and detail"
|
|
162
|
+
};
|
|
163
|
+
const messages = [
|
|
164
|
+
{
|
|
165
|
+
role: "system",
|
|
166
|
+
content: `You are Qwen3, a helpful AI assistant.
|
|
167
|
+
${modePrompts[args.mode || 'chat']}.`
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
role: "user",
|
|
171
|
+
content: args.query
|
|
172
|
+
}
|
|
173
|
+
];
|
|
174
|
+
const model = args.useFree === true ? OpenRouterModel.QWEN3_30B : OpenRouterModel.QWEN3_CODER;
|
|
175
|
+
return await callOpenRouter(messages, model, 0.7, 3000);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Multi-Model Tool
|
|
180
|
+
* Access various models via OpenRouter
|
|
181
|
+
*/
|
|
182
|
+
export const openRouterMultiModelTool = {
|
|
183
|
+
name: "openrouter_multi",
|
|
184
|
+
description: "Access Qwen and Kimi models through OpenRouter",
|
|
185
|
+
parameters: z.object({
|
|
186
|
+
query: z.string(),
|
|
187
|
+
model: z.enum([
|
|
188
|
+
"qwen-coder", "qwen-coder-plus",
|
|
189
|
+
"qwq-32b", "kimi-k2-thinking"
|
|
190
|
+
]),
|
|
191
|
+
temperature: z.number().optional().default(0.7)
|
|
192
|
+
}),
|
|
193
|
+
execute: async (args, { log }) => {
|
|
194
|
+
const modelMap = {
|
|
195
|
+
"qwen-coder": OpenRouterModel.QWEN3_CODER,
|
|
196
|
+
"qwen-coder-plus": OpenRouterModel.QWEN3_CODER_PLUS,
|
|
197
|
+
"qwq-32b": OpenRouterModel.QWQ_32B,
|
|
198
|
+
"kimi-k2-thinking": OpenRouterModel.KIMI_K2_THINKING
|
|
199
|
+
};
|
|
200
|
+
const messages = [
|
|
201
|
+
{
|
|
202
|
+
role: "system",
|
|
203
|
+
content: "You are a helpful AI assistant. Provide clear, accurate responses."
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
role: "user",
|
|
207
|
+
content: args.query
|
|
208
|
+
}
|
|
209
|
+
];
|
|
210
|
+
const selectedModel = modelMap[args.model];
|
|
211
|
+
if (!selectedModel) {
|
|
212
|
+
return `[Model ${args.model} not available]`;
|
|
213
|
+
}
|
|
214
|
+
return await callOpenRouter(messages, selectedModel, args.temperature || 0.7, 4000);
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Code Competition Tool
|
|
219
|
+
* Competitive programming and algorithm challenges
|
|
220
|
+
*/
|
|
221
|
+
export const qwenCompetitiveTool = {
|
|
222
|
+
name: "qwen_competitive",
|
|
223
|
+
description: "Competitive programming",
|
|
224
|
+
parameters: z.object({
|
|
225
|
+
problem: z.string(),
|
|
226
|
+
constraints: z.string().optional(),
|
|
227
|
+
language: z.enum(["python", "cpp", "java", "javascript", "rust"])
|
|
228
|
+
.optional()
|
|
229
|
+
.default("python"),
|
|
230
|
+
optimize: z.boolean().optional().default(true)
|
|
231
|
+
}),
|
|
232
|
+
execute: async (args, { log }) => {
|
|
233
|
+
const messages = [
|
|
234
|
+
{
|
|
235
|
+
role: "system",
|
|
236
|
+
content: `You are an expert competitive programmer.
|
|
237
|
+
Solve the problem efficiently with clean, optimized code.
|
|
238
|
+
Language: ${args.language}
|
|
239
|
+
${args.optimize ? 'Optimize for both time and space complexity.' : ''}
|
|
240
|
+
${args.constraints ? `Constraints: ${args.constraints}` : ''}
|
|
241
|
+
Provide:
|
|
242
|
+
1. Approach explanation
|
|
243
|
+
2. Complete working code
|
|
244
|
+
3. Time and space complexity analysis`
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
role: "user",
|
|
248
|
+
content: args.problem
|
|
249
|
+
}
|
|
250
|
+
];
|
|
251
|
+
return await callOpenRouter(messages, OpenRouterModel.QWEN3_CODER, 0.1, 6000);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* Kimi K2 Thinking Tool
|
|
256
|
+
* Advanced agentic reasoning with 1T MoE model (32B active)
|
|
257
|
+
* Excels at long-horizon reasoning, multi-step analysis, and complex problem-solving
|
|
258
|
+
*/
|
|
259
|
+
export const kimiThinkingTool = {
|
|
260
|
+
name: "kimi_thinking",
|
|
261
|
+
description: "Advanced agentic reasoning",
|
|
262
|
+
parameters: z.object({
|
|
263
|
+
problem: z.string(),
|
|
264
|
+
context: z.string().optional(),
|
|
265
|
+
approach: z.enum(["step-by-step", "analytical", "creative", "systematic"])
|
|
266
|
+
.optional()
|
|
267
|
+
.default("step-by-step"),
|
|
268
|
+
maxSteps: z.number().optional().default(10) // For multi-step reasoning
|
|
269
|
+
}),
|
|
270
|
+
execute: async (args, { log }) => {
|
|
271
|
+
const approachPrompts = {
|
|
272
|
+
"step-by-step": "Break down the problem into clear steps and solve systematically",
|
|
273
|
+
analytical: "Analyze the problem deeply, considering multiple perspectives and implications",
|
|
274
|
+
creative: "Think creatively and explore unconventional solutions and approaches",
|
|
275
|
+
systematic: "Apply systematic reasoning with clear logic chains and verification"
|
|
276
|
+
};
|
|
277
|
+
const messages = [
|
|
278
|
+
{
|
|
279
|
+
role: "system",
|
|
280
|
+
content: `You are Kimi K2 Thinking, a state-of-the-art agentic reasoning model.
|
|
281
|
+
${approachPrompts[args.approach || 'step-by-step']}.
|
|
282
|
+
Show your complete thinking process with clear reasoning chains.
|
|
283
|
+
Use up to ${args.maxSteps} reasoning steps if needed for complex problems.
|
|
284
|
+
${args.context ? `Context: ${args.context}` : ''}
|
|
285
|
+
Focus on: thorough analysis, logical reasoning, and actionable insights.`
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
role: "user",
|
|
289
|
+
content: args.problem
|
|
290
|
+
}
|
|
291
|
+
];
|
|
292
|
+
// Higher temperature (0.7) for creative reasoning, more tokens (16k) for detailed thought chains
|
|
293
|
+
return await callOpenRouter(messages, OpenRouterModel.KIMI_K2_THINKING, 0.7, 16000);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
/**
|
|
297
|
+
* Check if OpenRouter is available
|
|
298
|
+
*/
|
|
299
|
+
export function isOpenRouterAvailable() {
|
|
300
|
+
return !!OPENROUTER_API_KEY;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Get all OpenRouter tools
|
|
304
|
+
*/
|
|
305
|
+
export function getAllOpenRouterTools() {
|
|
306
|
+
if (!isOpenRouterAvailable()) {
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
return [
|
|
310
|
+
qwenCoderTool,
|
|
311
|
+
qwqReasoningTool,
|
|
312
|
+
qwenGeneralTool,
|
|
313
|
+
openRouterMultiModelTool,
|
|
314
|
+
qwenCompetitiveTool,
|
|
315
|
+
kimiThinkingTool
|
|
316
|
+
];
|
|
317
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optimized Tools Wrapper - Integrates Phase 1 optimizations
|
|
3
|
+
* Wraps existing tool calls with model routing, token optimization, and cost monitoring
|
|
4
|
+
*/
|
|
5
|
+
import { modelRouter, tokenOptimizer, costMonitor } from '../optimization/index.js';
|
|
6
|
+
/**
|
|
7
|
+
* Main optimization wrapper for tool calls
|
|
8
|
+
*/
|
|
9
|
+
export class OptimizedToolWrapper {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.defaultMaxTokens = 1000;
|
|
12
|
+
this.confirmationThreshold = 0.10; // $0.10
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Execute tool call with optimizations
|
|
16
|
+
*/
|
|
17
|
+
async executeOptimized(toolCall, executor) {
|
|
18
|
+
// Step 1: Build context and select model
|
|
19
|
+
const context = modelRouter.buildContext(toolCall.query, {
|
|
20
|
+
maxTokens: toolCall.maxTokens,
|
|
21
|
+
temperature: toolCall.temperature,
|
|
22
|
+
});
|
|
23
|
+
const selection = this.selectModelWithOverride(context, toolCall.model);
|
|
24
|
+
// Step 2: Check cost limits
|
|
25
|
+
const costCheck = await costMonitor.checkRequest(selection.primary, toolCall.maxTokens || this.defaultMaxTokens);
|
|
26
|
+
if (!costCheck.allowed) {
|
|
27
|
+
throw new Error(`Cost limit exceeded: ${costCheck.warning}`);
|
|
28
|
+
}
|
|
29
|
+
if (costCheck.requiresConfirmation && costCheck.estimatedCost > this.confirmationThreshold) {
|
|
30
|
+
console.warn(`⚠️ High cost request: $${costCheck.estimatedCost.toFixed(2)} for model ${selection.primary}`);
|
|
31
|
+
// In production, this would trigger a user confirmation
|
|
32
|
+
}
|
|
33
|
+
// Step 3: Optimize the request
|
|
34
|
+
const optimizedRequest = await tokenOptimizer.optimize({
|
|
35
|
+
prompt: toolCall.query,
|
|
36
|
+
model: selection.primary,
|
|
37
|
+
maxTokens: toolCall.maxTokens,
|
|
38
|
+
temperature: toolCall.temperature,
|
|
39
|
+
metadata: toolCall.metadata,
|
|
40
|
+
canBatch: true,
|
|
41
|
+
});
|
|
42
|
+
// Step 4: If cached, return immediately
|
|
43
|
+
if (optimizedRequest.fromCache) {
|
|
44
|
+
return {
|
|
45
|
+
result: optimizedRequest.prompt, // In real implementation, this would be the cached response
|
|
46
|
+
model: selection.primary,
|
|
47
|
+
cost: 0,
|
|
48
|
+
cached: true,
|
|
49
|
+
optimized: true,
|
|
50
|
+
tokensSaved: optimizedRequest.originalLength ?
|
|
51
|
+
optimizedRequest.originalLength - optimizedRequest.prompt.length : 0,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
// Step 5: Execute with selected model
|
|
55
|
+
let result;
|
|
56
|
+
let actualModel = selection.primary;
|
|
57
|
+
let inputTokens = 0;
|
|
58
|
+
let outputTokens = 0;
|
|
59
|
+
try {
|
|
60
|
+
// Execute with primary model
|
|
61
|
+
result = await executor(selection.primary, optimizedRequest.prompt, {
|
|
62
|
+
maxTokens: toolCall.maxTokens || this.defaultMaxTokens,
|
|
63
|
+
temperature: toolCall.temperature || 0.7,
|
|
64
|
+
...toolCall.metadata,
|
|
65
|
+
});
|
|
66
|
+
// Estimate tokens (in production, get actual counts from API response)
|
|
67
|
+
inputTokens = Math.ceil(optimizedRequest.prompt.length / 4);
|
|
68
|
+
outputTokens = Math.ceil((typeof result === 'string' ? result.length : JSON.stringify(result).length) / 4);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// Try fallback model
|
|
72
|
+
console.warn(`Primary model ${selection.primary} failed, trying fallback ${selection.fallback}`);
|
|
73
|
+
actualModel = selection.fallback;
|
|
74
|
+
result = await executor(selection.fallback, optimizedRequest.prompt, {
|
|
75
|
+
maxTokens: toolCall.maxTokens || this.defaultMaxTokens,
|
|
76
|
+
temperature: toolCall.temperature || 0.7,
|
|
77
|
+
...toolCall.metadata,
|
|
78
|
+
});
|
|
79
|
+
inputTokens = Math.ceil(optimizedRequest.prompt.length / 4);
|
|
80
|
+
outputTokens = Math.ceil((typeof result === 'string' ? result.length : JSON.stringify(result).length) / 4);
|
|
81
|
+
}
|
|
82
|
+
// Step 6: Track usage
|
|
83
|
+
const usage = await costMonitor.trackUsage(actualModel, inputTokens, outputTokens, `tool-${toolCall.tool}-${Date.now()}`);
|
|
84
|
+
// Step 7: Return optimized result
|
|
85
|
+
return {
|
|
86
|
+
result,
|
|
87
|
+
model: actualModel,
|
|
88
|
+
cost: usage.cost,
|
|
89
|
+
cached: false,
|
|
90
|
+
optimized: optimizedRequest.compressed || false,
|
|
91
|
+
tokensSaved: optimizedRequest.originalLength ?
|
|
92
|
+
optimizedRequest.originalLength - (optimizedRequest.compressedLength || optimizedRequest.prompt.length) : 0,
|
|
93
|
+
alert: usage.alert?.message,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Select model with optional override
|
|
98
|
+
*/
|
|
99
|
+
selectModelWithOverride(context, override) {
|
|
100
|
+
if (override) {
|
|
101
|
+
// User specified a model, use it but still calculate costs
|
|
102
|
+
const cost = modelRouter.estimateCost(override, context.maxTokens || this.defaultMaxTokens);
|
|
103
|
+
return {
|
|
104
|
+
primary: override,
|
|
105
|
+
fallback: modelRouter.selectModel(context).primary,
|
|
106
|
+
estimatedCost: cost,
|
|
107
|
+
estimatedLatency: 2000,
|
|
108
|
+
requiresConfirmation: cost > this.confirmationThreshold,
|
|
109
|
+
reasoning: 'User-specified model override',
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return modelRouter.selectModel(context);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get optimization statistics
|
|
116
|
+
*/
|
|
117
|
+
getStats() {
|
|
118
|
+
const tokenMetrics = tokenOptimizer.getMetrics();
|
|
119
|
+
const costReport = costMonitor.getUsageReport();
|
|
120
|
+
return {
|
|
121
|
+
optimization: {
|
|
122
|
+
cacheHitRate: `${(tokenMetrics.cacheHitRate * 100).toFixed(1)}%`,
|
|
123
|
+
compressionRatio: `${(tokenMetrics.compressionRatio * 100).toFixed(1)}%`,
|
|
124
|
+
tokensSaved: tokenMetrics.totalSaved,
|
|
125
|
+
batchesProcessed: tokenMetrics.batchesProcessed,
|
|
126
|
+
},
|
|
127
|
+
costs: {
|
|
128
|
+
hourly: `$${costReport.hourly.used.toFixed(2)} / $${costReport.hourly.limit.toFixed(2)}`,
|
|
129
|
+
daily: `$${costReport.daily.used.toFixed(2)} / $${costReport.daily.limit.toFixed(2)}`,
|
|
130
|
+
remaining: {
|
|
131
|
+
hourly: `$${costReport.hourly.remaining.toFixed(2)}`,
|
|
132
|
+
daily: `$${costReport.daily.remaining.toFixed(2)}`,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
topModels: costReport.topModels.map(m => `${m.model}: $${m.cost.toFixed(3)} (${m.usage} calls)`),
|
|
136
|
+
alerts: costReport.alerts.map(a => a.message),
|
|
137
|
+
recommendations: [
|
|
138
|
+
...tokenMetrics.recommendations,
|
|
139
|
+
...this.generateRecommendations(tokenMetrics, costReport),
|
|
140
|
+
],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Generate optimization recommendations
|
|
145
|
+
*/
|
|
146
|
+
generateRecommendations(tokenMetrics, costReport) {
|
|
147
|
+
const recs = [];
|
|
148
|
+
// Check if using expensive models too much
|
|
149
|
+
const expensiveModels = ['gpt-5'];
|
|
150
|
+
const topModel = costReport.topModels[0];
|
|
151
|
+
if (topModel && expensiveModels.includes(topModel.model)) {
|
|
152
|
+
recs.push(`💰 Consider using cheaper models - ${topModel.model} is your top cost driver`);
|
|
153
|
+
}
|
|
154
|
+
// Check cache effectiveness
|
|
155
|
+
if (tokenMetrics.cacheHitRate > 0.5) {
|
|
156
|
+
recs.push('✅ Cache is working effectively');
|
|
157
|
+
}
|
|
158
|
+
// Check compression
|
|
159
|
+
if (tokenMetrics.compressionRatio > 0.3) {
|
|
160
|
+
recs.push(`📦 Good compression ratio: ${(tokenMetrics.compressionRatio * 100).toFixed(0)}% reduction`);
|
|
161
|
+
}
|
|
162
|
+
// Cost savings estimate
|
|
163
|
+
const potentialSavings = costReport.daily.used * 0.7; // 70% reduction target
|
|
164
|
+
if (potentialSavings > 1) {
|
|
165
|
+
recs.push(`💎 Optimizations could save ~$${potentialSavings.toFixed(2)}/day`);
|
|
166
|
+
}
|
|
167
|
+
return recs;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Reset all metrics
|
|
171
|
+
*/
|
|
172
|
+
resetMetrics() {
|
|
173
|
+
tokenOptimizer.resetMetrics();
|
|
174
|
+
costMonitor.reset();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Export singleton instance
|
|
178
|
+
export const optimizedWrapper = new OptimizedToolWrapper();
|
|
179
|
+
/**
|
|
180
|
+
* Helper function to wrap any tool with optimizations
|
|
181
|
+
*/
|
|
182
|
+
export function wrapWithOptimization(toolName, originalExecutor) {
|
|
183
|
+
return async (query, options) => {
|
|
184
|
+
const result = await optimizedWrapper.executeOptimized({
|
|
185
|
+
tool: toolName,
|
|
186
|
+
query,
|
|
187
|
+
model: options?.model,
|
|
188
|
+
maxTokens: options?.maxTokens,
|
|
189
|
+
temperature: options?.temperature,
|
|
190
|
+
metadata: options,
|
|
191
|
+
}, originalExecutor);
|
|
192
|
+
// Log optimization info
|
|
193
|
+
if (result.cached) {
|
|
194
|
+
console.error(`✅ Cached response used for ${toolName}`);
|
|
195
|
+
}
|
|
196
|
+
if (result.tokensSaved && result.tokensSaved > 0) {
|
|
197
|
+
console.error(`📦 Saved ${result.tokensSaved} tokens through optimization`);
|
|
198
|
+
}
|
|
199
|
+
if (result.alert) {
|
|
200
|
+
console.warn(`⚠️ Cost alert: ${result.alert}`);
|
|
201
|
+
}
|
|
202
|
+
return result.result;
|
|
203
|
+
};
|
|
204
|
+
}
|