@noorm/marie-cli 0.1.18 → 0.1.25
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 +7 -15
- package/SENTINEL.md +4 -7
- package/dist/cli-new/components/App.js +16 -63
- package/dist/cli-new/components/App.js.map +1 -1
- package/dist/cli-new/components/ApprovalDialog.js +2 -1
- package/dist/cli-new/components/ApprovalDialog.js.map +1 -1
- package/dist/cli-new/components/Banner.js +4 -3
- package/dist/cli-new/components/Banner.js.map +1 -1
- package/dist/cli-new/components/ChatArea.js +6 -7
- package/dist/cli-new/components/ChatArea.js.map +1 -1
- package/dist/cli-new/components/Header.js +13 -7
- package/dist/cli-new/components/Header.js.map +1 -1
- package/dist/cli-new/components/InputArea.js +73 -12
- package/dist/cli-new/components/InputArea.js.map +1 -1
- package/dist/cli-new/components/MessageBubble.js +26 -18
- package/dist/cli-new/components/MessageBubble.js.map +1 -1
- package/dist/cli-new/components/SessionSwitcher.js +4 -7
- package/dist/cli-new/components/SessionSwitcher.js.map +1 -1
- package/dist/cli-new/components/SetupWizard.js +80 -257
- package/dist/cli-new/components/SetupWizard.js.map +1 -1
- package/dist/cli-new/components/ToolCallDisplay.js +20 -5
- package/dist/cli-new/components/ToolCallDisplay.js.map +1 -1
- package/dist/cli-new/components/WizardSteps.js +22 -0
- package/dist/cli-new/components/WizardSteps.js.map +1 -0
- package/dist/cli-new/constants/SetupConstants.js +42 -0
- package/dist/cli-new/constants/SetupConstants.js.map +1 -0
- package/dist/cli-new/hooks/useGit.js +19 -62
- package/dist/cli-new/hooks/useGit.js.map +1 -1
- package/dist/cli-new/hooks/useMarie.js +26 -18
- package/dist/cli-new/hooks/useMarie.js.map +1 -1
- package/dist/cli-new/hooks/useSessions.js +1 -1
- package/dist/cli-new/hooks/useSessions.js.map +1 -1
- package/dist/cli-new/hooks/useSetupWizard.js +88 -0
- package/dist/cli-new/hooks/useSetupWizard.js.map +1 -0
- package/dist/cli-new/hooks/useUpdateCheck.js +4 -3
- package/dist/cli-new/hooks/useUpdateCheck.js.map +1 -1
- package/dist/cli-new/index.js +2 -4
- package/dist/cli-new/index.js.map +1 -1
- package/dist/cli-new/services/CommandService.js +104 -0
- package/dist/cli-new/services/CommandService.js.map +1 -0
- package/dist/cli-new/services/GitService.js +91 -0
- package/dist/cli-new/services/GitService.js.map +1 -0
- package/dist/cli-new/services/MarieService.js +77 -0
- package/dist/cli-new/services/MarieService.js.map +1 -0
- package/dist/cli-new/services/auth-server.js +128 -0
- package/dist/cli-new/services/auth-server.js.map +1 -0
- package/dist/cli-new/utils/version.js +24 -0
- package/dist/cli-new/utils/version.js.map +1 -0
- package/dist/monolith/adapters/CliMarieAdapter.js +12 -11
- package/dist/monolith/adapters/CliMarieAdapter.js.map +1 -1
- package/dist/monolith/cli/CliFileSystemPort.js +17 -3
- package/dist/monolith/cli/CliFileSystemPort.js.map +1 -1
- package/dist/monolith/cli/MarieToolDefinitionsCLI.js +39 -31
- package/dist/monolith/cli/MarieToolDefinitionsCLI.js.map +1 -1
- package/dist/monolith/cli/index.js +5 -20
- package/dist/monolith/cli/index.js.map +1 -1
- package/dist/monolith/cli/services/JoyAutomationServiceCLI.js +15 -62
- package/dist/monolith/cli/services/JoyAutomationServiceCLI.js.map +1 -1
- package/dist/monolith/cli/storage.js +142 -72
- package/dist/monolith/cli/storage.js.map +1 -1
- package/dist/monolith/domain/joy/RitualService.js +44 -46
- package/dist/monolith/domain/joy/RitualService.js.map +1 -1
- package/dist/monolith/domain/marie/MarieCortex.js +148 -0
- package/dist/monolith/domain/marie/MarieCortex.js.map +1 -0
- package/dist/monolith/domain/marie/PersonalityRenderer.js +97 -0
- package/dist/monolith/domain/marie/PersonalityRenderer.js.map +1 -0
- package/dist/monolith/infrastructure/Configuration.js +68 -0
- package/dist/monolith/infrastructure/Configuration.js.map +1 -0
- package/dist/monolith/infrastructure/CoreInfrastructure.js +204 -0
- package/dist/monolith/infrastructure/CoreInfrastructure.js.map +1 -0
- package/dist/monolith/infrastructure/ai/agents/MarieAscendant.js +3 -3
- package/dist/monolith/infrastructure/ai/agents/MarieAscendant.js.map +1 -1
- package/dist/monolith/infrastructure/ai/context/ContextArchiveService.js +6 -27
- package/dist/monolith/infrastructure/ai/context/ContextArchiveService.js.map +1 -1
- package/dist/monolith/infrastructure/ai/context/ContextManager.js +142 -131
- package/dist/monolith/infrastructure/ai/context/ContextManager.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieEngine.js +115 -1077
- package/dist/monolith/infrastructure/ai/core/MarieEngine.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieEventDispatcher.js +1 -37
- package/dist/monolith/infrastructure/ai/core/MarieEventDispatcher.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieLockManager.js +6 -1
- package/dist/monolith/infrastructure/ai/core/MarieLockManager.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieProgressTracker.js +172 -221
- package/dist/monolith/infrastructure/ai/core/MarieProgressTracker.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieSanitizer.js +292 -141
- package/dist/monolith/infrastructure/ai/core/MarieSanitizer.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieToolProcessor.js +331 -614
- package/dist/monolith/infrastructure/ai/core/MarieToolProcessor.js.map +1 -1
- package/dist/monolith/infrastructure/ai/core/MarieVitality.js +238 -0
- package/dist/monolith/infrastructure/ai/core/MarieVitality.js.map +1 -0
- package/dist/monolith/infrastructure/ai/core/SessionLogService.js +9 -2
- package/dist/monolith/infrastructure/ai/core/SessionLogService.js.map +1 -1
- package/dist/monolith/infrastructure/ai/providers/AIProvider.js +402 -1
- package/dist/monolith/infrastructure/ai/providers/AIProvider.js.map +1 -1
- package/dist/monolith/infrastructure/ai/providers/DreamBeesProvider.js +114 -0
- package/dist/monolith/infrastructure/ai/providers/DreamBeesProvider.js.map +1 -0
- package/dist/monolith/infrastructure/ai/providers/OpenRouterProvider.js +426 -392
- package/dist/monolith/infrastructure/ai/providers/OpenRouterProvider.js.map +1 -1
- package/dist/monolith/infrastructure/ai/providers/OpenRouterStreamParser.js +235 -241
- package/dist/monolith/infrastructure/ai/providers/OpenRouterStreamParser.js.map +1 -1
- package/dist/monolith/infrastructure/ai/workerAi.js +185 -0
- package/dist/monolith/infrastructure/ai/workerAi.js.map +1 -0
- package/dist/monolith/infrastructure/config/ConfigService.js +216 -503
- package/dist/monolith/infrastructure/config/ConfigService.js.map +1 -1
- package/dist/monolith/infrastructure/joy/CognitiveRituals.js +4 -165
- package/dist/monolith/infrastructure/joy/CognitiveRituals.js.map +1 -1
- package/dist/monolith/infrastructure/joy/JoyTools.js +14 -47
- package/dist/monolith/infrastructure/joy/JoyTools.js.map +1 -1
- package/dist/monolith/infrastructure/persistence/MarieMindAutonomics.js +4 -0
- package/dist/monolith/infrastructure/persistence/MarieMindAutonomics.js.map +1 -0
- package/dist/monolith/infrastructure/persistence/MarieMindEngine.js +11 -0
- package/dist/monolith/infrastructure/persistence/MarieMindEngine.js.map +1 -0
- package/dist/monolith/infrastructure/persistence/NoormmeAutonomics.js +123 -106
- package/dist/monolith/infrastructure/persistence/NoormmeAutonomics.js.map +1 -1
- package/dist/monolith/infrastructure/persistence/NoormmeEngine.js +508 -63
- package/dist/monolith/infrastructure/persistence/NoormmeEngine.js.map +1 -1
- package/dist/monolith/infrastructure/persistence/NoormmeSchema.js +68 -39
- package/dist/monolith/infrastructure/persistence/NoormmeSchema.js.map +1 -1
- package/dist/monolith/infrastructure/persistence/NoormmeSeeder.js +80 -67
- package/dist/monolith/infrastructure/persistence/NoormmeSeeder.js.map +1 -1
- package/dist/monolith/infrastructure/persistence/NoormmeTools.js +122 -75
- package/dist/monolith/infrastructure/persistence/NoormmeTools.js.map +1 -1
- package/dist/monolith/infrastructure/services/MarieMemoryStore.js +133 -134
- package/dist/monolith/infrastructure/services/MarieMemoryStore.js.map +1 -1
- package/dist/monolith/infrastructure/tools/MarieToolDefinitions.js +6 -30
- package/dist/monolith/infrastructure/tools/MarieToolDefinitions.js.map +1 -1
- package/dist/monolith/infrastructure/tools/PureStreamParser.js +68 -80
- package/dist/monolith/infrastructure/tools/PureStreamParser.js.map +1 -1
- package/dist/monolith/infrastructure/tools/SharedToolDefinitions.js +12 -11
- package/dist/monolith/infrastructure/tools/SharedToolDefinitions.js.map +1 -1
- package/dist/monolith/infrastructure/tools/SovereignTools.js +326 -0
- package/dist/monolith/infrastructure/tools/SovereignTools.js.map +1 -0
- package/dist/monolith/infrastructure/tools/ToolRegistry.js +45 -26
- package/dist/monolith/infrastructure/tools/ToolRegistry.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/AnalysisTools.js +39 -153
- package/dist/monolith/infrastructure/tools/definitions/AnalysisTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/AutomationTools.js +31 -46
- package/dist/monolith/infrastructure/tools/definitions/AutomationTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/ContextTools.js +41 -13
- package/dist/monolith/infrastructure/tools/definitions/ContextTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/CoreTools.js +10 -14
- package/dist/monolith/infrastructure/tools/definitions/CoreTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/DiagnosticTools.js +39 -70
- package/dist/monolith/infrastructure/tools/definitions/DiagnosticTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/NavigationTools.js +30 -181
- package/dist/monolith/infrastructure/tools/definitions/NavigationTools.js.map +1 -1
- package/dist/monolith/infrastructure/tools/definitions/PlanningTools.js +12 -9
- package/dist/monolith/infrastructure/tools/definitions/PlanningTools.js.map +1 -1
- package/dist/monolith/plumbing/Plumbing.js +238 -0
- package/dist/monolith/plumbing/Plumbing.js.map +1 -0
- package/dist/monolith/plumbing/PlumbingAnalysis.js +109 -0
- package/dist/monolith/plumbing/PlumbingAnalysis.js.map +1 -0
- package/dist/monolith/plumbing/PlumbingSystem.js +169 -0
- package/dist/monolith/plumbing/PlumbingSystem.js.map +1 -0
- package/dist/monolith/plumbing/analysis/ComplexityService.js +30 -34
- package/dist/monolith/plumbing/analysis/ComplexityService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/DependencyService.js +55 -44
- package/dist/monolith/plumbing/analysis/DependencyService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/DiscoveryService.js +40 -42
- package/dist/monolith/plumbing/analysis/DiscoveryService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/JoyMapService.js +52 -56
- package/dist/monolith/plumbing/analysis/JoyMapService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/LintService.js +118 -118
- package/dist/monolith/plumbing/analysis/LintService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/MarieSentinelService.js +278 -268
- package/dist/monolith/plumbing/analysis/MarieSentinelService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/QualityGuardrailService.js +116 -114
- package/dist/monolith/plumbing/analysis/QualityGuardrailService.js.map +1 -1
- package/dist/monolith/plumbing/analysis/SurgicalMender.js +57 -59
- package/dist/monolith/plumbing/analysis/SurgicalMender.js.map +1 -1
- package/dist/monolith/plumbing/analysis/TestService.js +89 -89
- package/dist/monolith/plumbing/analysis/TestService.js.map +1 -1
- package/dist/monolith/plumbing/filesystem/FileService.js +123 -195
- package/dist/monolith/plumbing/filesystem/FileService.js.map +1 -1
- package/dist/monolith/plumbing/filesystem/PathResolver.js +7 -8
- package/dist/monolith/plumbing/filesystem/PathResolver.js.map +1 -1
- package/dist/monolith/plumbing/git/GitService.js +4 -4
- package/dist/monolith/plumbing/git/GitService.js.map +1 -1
- package/dist/monolith/plumbing/lsp/SymbolService.js +5 -34
- package/dist/monolith/plumbing/lsp/SymbolService.js.map +1 -1
- package/dist/monolith/plumbing/terminal/ProcessRegistry.js +20 -22
- package/dist/monolith/plumbing/terminal/ProcessRegistry.js.map +1 -1
- package/dist/monolith/plumbing/terminal/TerminalService.js +127 -141
- package/dist/monolith/plumbing/terminal/TerminalService.js.map +1 -1
- package/dist/monolith/plumbing/utils/EnvironmentUtils.js +3 -23
- package/dist/monolith/plumbing/utils/EnvironmentUtils.js.map +1 -1
- package/dist/monolith/plumbing/utils/JsonUtils.js +252 -311
- package/dist/monolith/plumbing/utils/JsonUtils.js.map +1 -1
- package/dist/monolith/plumbing/utils/PlumbingCore.js +549 -0
- package/dist/monolith/plumbing/utils/PlumbingCore.js.map +1 -0
- package/dist/monolith/plumbing/utils/PrefixTree.js +61 -114
- package/dist/monolith/plumbing/utils/PrefixTree.js.map +1 -1
- package/dist/monolith/plumbing/utils/StreamTagDetector.js +89 -127
- package/dist/monolith/plumbing/utils/StreamTagDetector.js.map +1 -1
- package/dist/monolith/plumbing/utils/StringUtils.js +87 -89
- package/dist/monolith/plumbing/utils/StringUtils.js.map +1 -1
- package/dist/monolith/runtime/MarieRuntime.js +76 -499
- package/dist/monolith/runtime/MarieRuntime.js.map +1 -1
- package/dist/monolith/runtime/RuntimeAdapterBase.js +1 -1
- package/dist/monolith/runtime/RuntimeAdapterBase.js.map +1 -1
- package/dist/monolith/runtime/providerFactory.js +1 -7
- package/dist/monolith/runtime/providerFactory.js.map +1 -1
- package/dist/monolith/services/HealthService.js +29 -32
- package/dist/monolith/services/HealthService.js.map +1 -1
- package/dist/monolith/services/JoyAutomationService.js +58 -95
- package/dist/monolith/services/JoyAutomationService.js.map +1 -1
- package/dist/monolith/services/MarieAutomationService.js +59 -0
- package/dist/monolith/services/MarieAutomationService.js.map +1 -0
- package/dist/monolith/services/MarieGhostService.js +46 -173
- package/dist/monolith/services/MarieGhostService.js.map +1 -1
- package/dist/monolith/services/MarieServices.js +102 -0
- package/dist/monolith/services/MarieServices.js.map +1 -0
- package/dist/monolith/services/MarieTypes.js +2 -0
- package/dist/monolith/services/MarieTypes.js.map +1 -0
- package/dist/monolith/services/UpdateService.js +47 -49
- package/dist/monolith/services/UpdateService.js.map +1 -1
- package/dist/prompts.js +11 -5
- package/dist/prompts.js.map +1 -1
- package/dist/test_prefix_tree.js +9 -9
- package/dist/test_prefix_tree.js.map +1 -1
- package/package.json +18 -89
- package/run_test.js +5 -0
- package/.marie_visual_verify_1771225696548/progress_bar_check.txt +0 -1
- package/dist/extension.cjs +0 -1155
- package/dist/extension.js +0 -474
- package/dist/extension.js.map +0 -1
- package/dist/monolith/adapters/VscodeMarieAdapter.js +0 -81
- package/dist/monolith/adapters/VscodeMarieAdapter.js.map +0 -1
- package/dist/monolith/infrastructure/ai/core/GhostPort.js +0 -2
- package/dist/monolith/infrastructure/ai/core/GhostPort.js.map +0 -1
- package/dist/monolith/infrastructure/ai/core/VscodeFileSystemPort.js +0 -33
- package/dist/monolith/infrastructure/ai/core/VscodeFileSystemPort.js.map +0 -1
- package/dist/monolith/infrastructure/ai/providers/AnthropicProvider.js +0 -154
- package/dist/monolith/infrastructure/ai/providers/AnthropicProvider.js.map +0 -1
- package/dist/monolith/infrastructure/ai/providers/CerebrasProvider.js +0 -214
- package/dist/monolith/infrastructure/ai/providers/CerebrasProvider.js.map +0 -1
- package/dist/monolith/plumbing/ui/DecorationService.js +0 -54
- package/dist/monolith/plumbing/ui/DecorationService.js.map +0 -1
- package/dist/monolith/services/JoyLogService.js +0 -48
- package/dist/monolith/services/JoyLogService.js.map +0 -1
- package/dist/monolith/services/JoyService.js +0 -209
- package/dist/monolith/services/JoyService.js.map +0 -1
- package/dist/monolith/services/MarieSCMProvider.js +0 -41
- package/dist/monolith/services/MarieSCMProvider.js.map +0 -1
- package/dist/webview-ui/main.css +0 -1
- package/dist/webview-ui/main.js +0 -108
- package/lint_output.txt +0 -705
- package/lint_output_v2.txt +0 -711
- package/test-mind-p6.sqlite +0 -0
- package/test-mind-p6.sqlite-shm +0 -0
- package/test-mind-p6.sqlite-wal +0 -0
|
@@ -1,426 +1,460 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getErrorMessage } from "../../../plumbing/utils/
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { getTokensPerChar } from "../../config/ConfigService.js";
|
|
2
|
+
import { getErrorMessage, safeParseJson, extractToolCall, createStreamTagDetector, safeStringify, safeGetValues } from "../../../plumbing/utils/PlumbingCore.js";
|
|
3
|
+
const BASE_URL = "https://openrouter.ai/api/v1";
|
|
4
|
+
/**
|
|
5
|
+
* Translates Anthropic model names to OpenRouter format.
|
|
6
|
+
* OpenRouter expects format like "anthropic/claude-3.5-sonnet"
|
|
7
|
+
*/
|
|
8
|
+
function translateModelName(model) {
|
|
9
|
+
const normalized = (model || "").trim();
|
|
10
|
+
if (!normalized) {
|
|
11
|
+
return "anthropic/claude-3.5-sonnet";
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
throw new Error(errorMsg);
|
|
13
|
+
// If already in OpenRouter format, return as-is
|
|
14
|
+
if (normalized.includes("/")) {
|
|
15
|
+
return normalized;
|
|
16
|
+
}
|
|
17
|
+
// Map Anthropic model names to OpenRouter format
|
|
18
|
+
const modelMap = {
|
|
19
|
+
"claude-3-5-sonnet-20241022": "anthropic/claude-3.5-sonnet",
|
|
20
|
+
"claude-3-5-sonnet": "anthropic/claude-3.5-sonnet",
|
|
21
|
+
"claude-3-5-haiku-20241022": "anthropic/claude-3.5-haiku",
|
|
22
|
+
"claude-3-5-haiku": "anthropic/claude-3.5-haiku",
|
|
23
|
+
"claude-3-opus-20240229": "anthropic/claude-3-opus",
|
|
24
|
+
"claude-3-opus": "anthropic/claude-3-opus",
|
|
25
|
+
"claude-3-sonnet-20240229": "anthropic/claude-3-sonnet",
|
|
26
|
+
"claude-3-sonnet": "anthropic/claude-3-sonnet",
|
|
27
|
+
"claude-3-haiku-20240307": "anthropic/claude-3-haiku",
|
|
28
|
+
"claude-3-haiku": "anthropic/claude-3-haiku",
|
|
29
|
+
};
|
|
30
|
+
if (modelMap[normalized]) {
|
|
31
|
+
return modelMap[normalized];
|
|
32
|
+
}
|
|
33
|
+
// Try to convert claude models that aren't in the map.
|
|
34
|
+
if (normalized.startsWith("claude-")) {
|
|
35
|
+
const withoutDateSuffix = normalized.replace(/-20\d{6}$/, "");
|
|
36
|
+
const claudeVersioned = withoutDateSuffix.match(/^claude-(\d+)-(\d+)(-.+)$/);
|
|
37
|
+
if (claudeVersioned) {
|
|
38
|
+
const major = claudeVersioned[1];
|
|
39
|
+
const minor = claudeVersioned[2];
|
|
40
|
+
const variant = claudeVersioned[3];
|
|
41
|
+
return `anthropic/claude-${major}.${minor}${variant}`;
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
43
|
+
return `anthropic/${withoutDateSuffix}`;
|
|
44
|
+
}
|
|
45
|
+
// Return as-is if we can't translate
|
|
46
|
+
return normalized;
|
|
47
|
+
}
|
|
48
|
+
function getOpenAiMessages(params) {
|
|
49
|
+
const messages = [];
|
|
50
|
+
if (params.system)
|
|
51
|
+
messages.push({ role: "system", content: params.system });
|
|
52
|
+
for (const msg of params.messages) {
|
|
53
|
+
if (typeof msg.content === "string") {
|
|
54
|
+
messages.push({ role: msg.role, content: msg.content });
|
|
55
|
+
}
|
|
56
|
+
else if (Array.isArray(msg.content)) {
|
|
57
|
+
let text = "";
|
|
58
|
+
const tools = [];
|
|
59
|
+
for (const block of msg.content) {
|
|
60
|
+
if (block.type === "text")
|
|
61
|
+
text += block.text;
|
|
62
|
+
else if (block.type === "tool_use")
|
|
63
|
+
tools.push({
|
|
64
|
+
id: block.id,
|
|
65
|
+
type: "function",
|
|
66
|
+
function: {
|
|
67
|
+
name: block.name,
|
|
68
|
+
arguments: safeStringify(block.input),
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
else if (block.type === "tool_result") {
|
|
72
|
+
messages.push({
|
|
73
|
+
role: "tool",
|
|
74
|
+
tool_call_id: block.tool_use_id,
|
|
75
|
+
content: typeof block.content === "string"
|
|
76
|
+
? block.content
|
|
77
|
+
: safeStringify(block.content),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
59
80
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
81
|
+
if (msg.role === "assistant" && (text || tools.length > 0)) {
|
|
82
|
+
const m = { role: "assistant", content: text || null };
|
|
83
|
+
if (tools.length > 0)
|
|
84
|
+
m.tool_calls = tools;
|
|
85
|
+
messages.push(m);
|
|
86
|
+
}
|
|
87
|
+
else if (msg.role === "user" && text) {
|
|
88
|
+
messages.push({ role: "user", content: text });
|
|
66
89
|
}
|
|
67
90
|
}
|
|
68
|
-
return {
|
|
69
|
-
role: "assistant",
|
|
70
|
-
content: choice.content || "",
|
|
71
|
-
};
|
|
72
91
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
92
|
+
return messages;
|
|
93
|
+
}
|
|
94
|
+
function getOpenAiTools(params) {
|
|
95
|
+
if (!params.tools)
|
|
96
|
+
return undefined;
|
|
97
|
+
return params.tools.map((tool) => ({
|
|
98
|
+
type: "function",
|
|
99
|
+
function: {
|
|
100
|
+
name: tool.name,
|
|
101
|
+
description: tool.description,
|
|
102
|
+
parameters: tool.input_schema,
|
|
103
|
+
},
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
function stripToolWrapperTags(text) {
|
|
107
|
+
if (!text)
|
|
108
|
+
return "";
|
|
109
|
+
return text
|
|
110
|
+
.replace(/<\|tool_calls_section_begin\|>/g, "")
|
|
111
|
+
.replace(/<\|tool_calls_section_end\|>/g, "")
|
|
112
|
+
.replace(/<\|tool_call_begin\|>/g, "")
|
|
113
|
+
.replace(/<\|tool_call_end\|>/g, "")
|
|
114
|
+
.replace(/<\|tool_call_argument_begin\|>/g, "")
|
|
115
|
+
.replace(/<\|tool_call_arguments_begin\|>/g, "")
|
|
116
|
+
.replace(/<tool_call>/g, "")
|
|
117
|
+
.replace(/<\/tool_call>/g, "")
|
|
118
|
+
.replace(/<tool>/g, "")
|
|
119
|
+
.replace(/<\/tool>/g, "")
|
|
120
|
+
.replace(/<function\b[^>]*>/g, "")
|
|
121
|
+
.replace(/<\/function>/g, "")
|
|
122
|
+
.replace(/<function_calls>/g, "")
|
|
123
|
+
.replace(/<\/function_calls>/g, "")
|
|
124
|
+
.trim();
|
|
125
|
+
}
|
|
126
|
+
function createStreamParser() {
|
|
127
|
+
const tagDetector = createStreamTagDetector();
|
|
128
|
+
let llamaToolMode = false;
|
|
129
|
+
let thinkingMode = false;
|
|
130
|
+
let llamaBufferParts = [];
|
|
131
|
+
const toolCalls = {};
|
|
132
|
+
let indexCount = 0;
|
|
133
|
+
function finalizeCurrentBuffer(events) {
|
|
134
|
+
if (llamaBufferParts.length === 0)
|
|
135
|
+
return;
|
|
136
|
+
const bufferContent = llamaBufferParts.join("");
|
|
137
|
+
const extracted = extractToolCall(bufferContent);
|
|
138
|
+
if (extracted) {
|
|
139
|
+
const id = extracted.id || `call_${Date.now()}_${indexCount}`;
|
|
140
|
+
events.push({ type: "tool_call_delta", index: indexCount, id, name: extracted.name, argumentsDelta: safeStringify(extracted.input) });
|
|
141
|
+
toolCalls[indexCount] = { id, name: extracted.name, arguments: safeStringify(extracted.input) };
|
|
142
|
+
indexCount++;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const cleaned = stripToolWrapperTags(bufferContent);
|
|
146
|
+
if (cleaned)
|
|
147
|
+
events.push({ type: "content_delta", text: cleaned });
|
|
148
|
+
}
|
|
149
|
+
llamaBufferParts = [];
|
|
150
|
+
llamaToolMode = false;
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
process(chunk, onUpdate, contentParts, reasoningParts) {
|
|
154
|
+
let currentChunk = chunk;
|
|
155
|
+
while (true) {
|
|
156
|
+
const detection = tagDetector.process(currentChunk);
|
|
157
|
+
currentChunk = "";
|
|
158
|
+
if (detection.text) {
|
|
159
|
+
if (llamaToolMode)
|
|
160
|
+
llamaBufferParts.push(detection.text);
|
|
161
|
+
else if (thinkingMode) {
|
|
162
|
+
reasoningParts.push(detection.text);
|
|
163
|
+
onUpdate({ type: "reasoning_delta", text: detection.text });
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
contentParts.push(detection.text);
|
|
167
|
+
onUpdate({ type: "content_delta", text: detection.text });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (detection.type === "tag") {
|
|
171
|
+
const tag = detection.tag;
|
|
172
|
+
if (tag === "<thought>") {
|
|
173
|
+
thinkingMode = true;
|
|
174
|
+
onUpdate({ type: "stage_change", stage: "thinking", label: "Thinking..." });
|
|
175
|
+
}
|
|
176
|
+
else if (tag === "</thought>") {
|
|
177
|
+
thinkingMode = false;
|
|
178
|
+
onUpdate({ type: "stage_change", stage: "responding", label: "Generating response..." });
|
|
179
|
+
}
|
|
180
|
+
else if (tag.startsWith("[Tool Use:") || tag.startsWith("[Tool Result:")) {
|
|
181
|
+
if (llamaToolMode)
|
|
182
|
+
finalizeCurrentBuffer([]); // Logic merger simplification
|
|
183
|
+
if (tag.startsWith("[Tool Use:")) {
|
|
184
|
+
llamaToolMode = true;
|
|
185
|
+
llamaBufferParts.push(tag);
|
|
186
|
+
onUpdate({ type: "stage_change", stage: "calling_tool", label: "Using tool..." });
|
|
187
|
+
}
|
|
188
|
+
else
|
|
189
|
+
onUpdate({ type: "content_delta", text: tag });
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
const isEnd = ["</tool>", "<|tool_call_end|>", "</invoke>", "</function>", "</tool_code>", "</tool_call>", "</call>"].includes(tag) || tag === "<|tool_calls_section_end|>";
|
|
193
|
+
const isStart = ["<tool>", "<|tool_call_begin|>", "<function_calls>", "<tool_code>", "<tool_call>"].includes(tag) || tag === "<|tool_calls_section_begin|>" || tag.startsWith("<function") || tag === "<invoke";
|
|
194
|
+
if (isEnd) {
|
|
195
|
+
llamaBufferParts.push(tag);
|
|
196
|
+
const bufferContent = llamaBufferParts.join("");
|
|
197
|
+
const extracted = extractToolCall(bufferContent);
|
|
198
|
+
if (extracted) {
|
|
199
|
+
const id = extracted.id || `call_${Date.now()}_${indexCount}`;
|
|
200
|
+
onUpdate({ type: "tool_call_delta", index: indexCount, id, name: extracted.name, argumentsDelta: safeStringify(extracted.input) });
|
|
201
|
+
toolCalls[indexCount] = { id, name: extracted.name, arguments: safeStringify(extracted.input) };
|
|
202
|
+
indexCount++;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
const cleaned = stripToolWrapperTags(bufferContent);
|
|
206
|
+
if (cleaned)
|
|
207
|
+
onUpdate({ type: "content_delta", text: cleaned });
|
|
208
|
+
}
|
|
209
|
+
llamaToolMode = false;
|
|
210
|
+
llamaBufferParts = [];
|
|
211
|
+
}
|
|
212
|
+
else if (isStart) {
|
|
213
|
+
llamaToolMode = true;
|
|
214
|
+
onUpdate({ type: "stage_change", stage: "calling_tool", label: "Using tool..." });
|
|
215
|
+
llamaBufferParts.push(tag);
|
|
216
|
+
}
|
|
217
|
+
else if (llamaToolMode)
|
|
218
|
+
llamaBufferParts.push(tag);
|
|
219
|
+
else
|
|
220
|
+
onUpdate({ type: "content_delta", text: tag });
|
|
221
|
+
}
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
finalize(fullContent, onUpdate) {
|
|
228
|
+
const bufferContent = llamaBufferParts.join("");
|
|
229
|
+
let extracted = extractToolCall(bufferContent) || extractToolCall(fullContent);
|
|
230
|
+
if (extracted) {
|
|
231
|
+
const id = extracted.id || `call_${Date.now()}_${indexCount}_final`;
|
|
232
|
+
const collectedValues = safeGetValues(toolCalls, 50);
|
|
233
|
+
const already = collectedValues.some(tc => tc.name === extracted.name && (tc.arguments === safeStringify(extracted.input)));
|
|
234
|
+
if (!already) {
|
|
235
|
+
onUpdate({ type: "tool_call_delta", index: indexCount, id, name: extracted.name, argumentsDelta: safeStringify(extracted.input) });
|
|
236
|
+
toolCalls[indexCount] = { id, name: extracted.name, arguments: safeStringify(extracted.input) };
|
|
237
|
+
indexCount++;
|
|
238
|
+
}
|
|
239
|
+
llamaBufferParts = [];
|
|
240
|
+
}
|
|
241
|
+
else if (llamaToolMode) {
|
|
242
|
+
const cleaned = stripToolWrapperTags(llamaBufferParts.join(""));
|
|
243
|
+
if (cleaned)
|
|
244
|
+
onUpdate({ type: "content_delta", text: cleaned });
|
|
245
|
+
}
|
|
246
|
+
llamaToolMode = false;
|
|
247
|
+
llamaBufferParts = [];
|
|
248
|
+
},
|
|
249
|
+
getToolCalls: () => toolCalls
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
export function createOpenRouterProvider(apiKey) {
|
|
253
|
+
return {
|
|
254
|
+
async createMessage(params) {
|
|
255
|
+
const translatedModel = translateModelName(params.model);
|
|
256
|
+
const response = await fetch(`${BASE_URL}/chat/completions`, {
|
|
90
257
|
method: "POST",
|
|
91
258
|
headers: {
|
|
92
|
-
Authorization: `Bearer ${
|
|
93
|
-
"HTTP-Referer": "https://github.com/
|
|
94
|
-
"X-Title": "
|
|
259
|
+
Authorization: `Bearer ${apiKey}`,
|
|
260
|
+
"HTTP-Referer": "https://github.com/CardSorting/Marie-Coder-New-the-vatican",
|
|
261
|
+
"X-Title": "DreamBees CLI",
|
|
95
262
|
"Content-Type": "application/json",
|
|
96
|
-
Accept: "
|
|
97
|
-
"Cache-Control": "no-cache",
|
|
98
|
-
Connection: "keep-alive",
|
|
263
|
+
Accept: "application/json",
|
|
99
264
|
},
|
|
100
|
-
body:
|
|
265
|
+
body: safeStringify({
|
|
101
266
|
model: translatedModel,
|
|
102
|
-
messages:
|
|
103
|
-
tools:
|
|
267
|
+
messages: getOpenAiMessages(params),
|
|
268
|
+
tools: getOpenAiTools(params),
|
|
104
269
|
max_tokens: params.max_tokens || 1024,
|
|
105
|
-
stream: true,
|
|
106
|
-
include_reasoning: true,
|
|
107
270
|
}),
|
|
108
|
-
signal: controller.signal,
|
|
109
271
|
});
|
|
110
272
|
if (!response.ok) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const decoder = new TextDecoder();
|
|
117
|
-
const contentParts = [];
|
|
118
|
-
const reasoningParts = [];
|
|
119
|
-
const bufferParts = [];
|
|
120
|
-
const toolCalls = {};
|
|
121
|
-
const startTime = Date.now();
|
|
122
|
-
onUpdate({ type: "run_started", timestamp: startTime });
|
|
123
|
-
onUpdate({
|
|
124
|
-
type: "stage_change",
|
|
125
|
-
stage: "thinking",
|
|
126
|
-
label: "Processing request...",
|
|
127
|
-
});
|
|
128
|
-
const streamParser = new OpenRouterStreamParser();
|
|
129
|
-
try {
|
|
130
|
-
while (true) {
|
|
131
|
-
const { done, value } = await reader.read();
|
|
132
|
-
if (done)
|
|
133
|
-
break;
|
|
134
|
-
const chunk = decoder.decode(value, { stream: true });
|
|
135
|
-
bufferParts.push(chunk);
|
|
136
|
-
const buffer = bufferParts.join("");
|
|
137
|
-
const lines = buffer.split("\n");
|
|
138
|
-
const remainder = lines.pop() || "";
|
|
139
|
-
bufferParts.length = 0;
|
|
140
|
-
if (remainder)
|
|
141
|
-
bufferParts.push(remainder);
|
|
142
|
-
for (const line of lines) {
|
|
143
|
-
const trimmedLine = line.trim();
|
|
144
|
-
if (!trimmedLine || trimmedLine.startsWith(":"))
|
|
145
|
-
continue;
|
|
146
|
-
if (trimmedLine.startsWith("data: ")) {
|
|
147
|
-
const dataStr = trimmedLine.slice(6);
|
|
148
|
-
if (dataStr === "[DONE]")
|
|
149
|
-
continue;
|
|
150
|
-
try {
|
|
151
|
-
const data = JSON.parse(dataStr);
|
|
152
|
-
const choice = data.choices?.[0];
|
|
153
|
-
if (!choice)
|
|
154
|
-
continue;
|
|
155
|
-
const delta = choice.delta;
|
|
156
|
-
if (delta?.content) {
|
|
157
|
-
const events = streamParser.processContent(delta.content);
|
|
158
|
-
for (const event of events) {
|
|
159
|
-
if (event.type === "content_delta")
|
|
160
|
-
contentParts.push(event.text);
|
|
161
|
-
if (event.type === "reasoning_delta")
|
|
162
|
-
reasoningParts.push(event.text);
|
|
163
|
-
onUpdate(event);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (delta?.tool_calls) {
|
|
167
|
-
onUpdate({
|
|
168
|
-
type: "stage_change",
|
|
169
|
-
stage: "calling_tool",
|
|
170
|
-
label: "Using tool...",
|
|
171
|
-
});
|
|
172
|
-
for (const tc of delta.tool_calls) {
|
|
173
|
-
const idx = tc.index;
|
|
174
|
-
if (!toolCalls[idx]) {
|
|
175
|
-
toolCalls[idx] = {
|
|
176
|
-
id: tc.id || "",
|
|
177
|
-
name: tc.function?.name || "",
|
|
178
|
-
argumentsParts: [],
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
if (tc.id)
|
|
183
|
-
toolCalls[idx].id = tc.id;
|
|
184
|
-
if (tc.function?.name)
|
|
185
|
-
toolCalls[idx].name += tc.function.name;
|
|
186
|
-
}
|
|
187
|
-
if (tc.function?.arguments) {
|
|
188
|
-
toolCalls[idx].argumentsParts.push(tc.function.arguments);
|
|
189
|
-
onUpdate({
|
|
190
|
-
type: "tool_call_delta",
|
|
191
|
-
index: idx,
|
|
192
|
-
id: toolCalls[idx].id,
|
|
193
|
-
name: toolCalls[idx].name,
|
|
194
|
-
argumentsDelta: tc.function.arguments,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (data.usage) {
|
|
200
|
-
onUpdate({
|
|
201
|
-
type: "usage",
|
|
202
|
-
usage: {
|
|
203
|
-
inputTokens: data.usage.prompt_tokens,
|
|
204
|
-
outputTokens: data.usage.completion_tokens,
|
|
205
|
-
totalTokens: data.usage.total_tokens,
|
|
206
|
-
reasoningTokens: data.usage.reasoning_tokens,
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
catch (e) {
|
|
212
|
-
// Ignore parsing errors for individual chunks
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
273
|
+
let errorMsg = `OpenRouter API error: ${response.status} ${response.statusText} (model: ${translatedModel})`;
|
|
274
|
+
try {
|
|
275
|
+
const errorJson = (await response.json());
|
|
276
|
+
if (errorJson.error?.message)
|
|
277
|
+
errorMsg = `OpenRouter API error: ${errorJson.error.message} (model: ${translatedModel})`;
|
|
216
278
|
}
|
|
279
|
+
catch (e) {
|
|
280
|
+
const text = await response.text().catch(() => "");
|
|
281
|
+
if (text)
|
|
282
|
+
errorMsg += ` - ${text}`;
|
|
283
|
+
}
|
|
284
|
+
throw new Error(errorMsg);
|
|
217
285
|
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const fullReasoning = reasoningParts.join("");
|
|
224
|
-
const finalEvents = streamParser.finalize(fullContent);
|
|
225
|
-
for (const event of finalEvents)
|
|
226
|
-
onUpdate(event);
|
|
227
|
-
const endTime = Date.now();
|
|
228
|
-
onUpdate({
|
|
229
|
-
type: "run_completed",
|
|
230
|
-
timestamp: endTime,
|
|
231
|
-
durationMs: endTime - startTime,
|
|
232
|
-
});
|
|
233
|
-
const llamaCalls = streamParser.getCollectedToolCalls();
|
|
234
|
-
for (const idx in llamaCalls) {
|
|
235
|
-
const call = llamaCalls[idx];
|
|
236
|
-
toolCalls[parseInt(idx) + 1000] = {
|
|
237
|
-
id: call.id,
|
|
238
|
-
name: call.name,
|
|
239
|
-
argumentsParts: [call.arguments], // Wrap string as array for consistency
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
const finalToolUses = [];
|
|
243
|
-
for (const idx in toolCalls) {
|
|
244
|
-
const tc = toolCalls[idx];
|
|
286
|
+
const data = (await response.json());
|
|
287
|
+
if (!data.choices || data.choices.length === 0)
|
|
288
|
+
throw new Error("OpenRouter API returned no choices.");
|
|
289
|
+
const choice = data.choices[0].message;
|
|
290
|
+
if (choice.tool_calls && choice.tool_calls.length > 0) {
|
|
245
291
|
try {
|
|
246
|
-
const
|
|
247
|
-
finalToolUses.push({
|
|
292
|
+
const toolUses = choice.tool_calls.map((tc) => ({
|
|
248
293
|
id: tc.id,
|
|
249
|
-
name: tc.name,
|
|
250
|
-
input:
|
|
251
|
-
});
|
|
294
|
+
name: tc.function.name,
|
|
295
|
+
input: safeParseJson(tc.function.arguments).result,
|
|
296
|
+
}));
|
|
297
|
+
return {
|
|
298
|
+
role: "assistant",
|
|
299
|
+
content: choice.content || "",
|
|
300
|
+
tool_uses: toolUses,
|
|
301
|
+
};
|
|
252
302
|
}
|
|
253
|
-
catch (
|
|
254
|
-
|
|
303
|
+
catch (error) {
|
|
304
|
+
console.error("Failed to parse tool arguments from OpenRouter:", getErrorMessage(error));
|
|
305
|
+
return {
|
|
306
|
+
role: "assistant",
|
|
307
|
+
content: choice.content || `(Message error: Malformed tool arguments)`,
|
|
308
|
+
};
|
|
255
309
|
}
|
|
256
310
|
}
|
|
257
|
-
const blocks = [];
|
|
258
|
-
if (fullReasoning)
|
|
259
|
-
blocks.push({ type: "thought", text: fullReasoning });
|
|
260
|
-
if (fullContent)
|
|
261
|
-
blocks.push({ type: "text", text: fullContent });
|
|
262
311
|
return {
|
|
263
312
|
role: "assistant",
|
|
264
|
-
content:
|
|
265
|
-
tool_uses: finalToolUses.length > 0 ? finalToolUses : undefined,
|
|
313
|
+
content: choice.content || "",
|
|
266
314
|
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
315
|
+
},
|
|
316
|
+
async createMessageStream(params, onUpdate, signal) {
|
|
317
|
+
const controller = new AbortController();
|
|
318
|
+
let isTimeout = false;
|
|
319
|
+
const timeoutId = setTimeout(() => {
|
|
320
|
+
isTimeout = true;
|
|
321
|
+
controller.abort();
|
|
322
|
+
}, 120000);
|
|
323
|
+
let isUserAbort = false;
|
|
324
|
+
const onAbort = () => {
|
|
325
|
+
isUserAbort = true;
|
|
326
|
+
controller.abort();
|
|
327
|
+
};
|
|
328
|
+
if (signal)
|
|
329
|
+
signal.addEventListener("abort", onAbort);
|
|
330
|
+
try {
|
|
331
|
+
const translatedModel = translateModelName(params.model);
|
|
332
|
+
const response = await fetch(`${BASE_URL}/chat/completions`, {
|
|
333
|
+
method: "POST",
|
|
334
|
+
headers: {
|
|
335
|
+
Authorization: `Bearer ${apiKey}`,
|
|
336
|
+
"HTTP-Referer": "https://github.com/CardSorting/Marie-Coder-New-the-vatican",
|
|
337
|
+
"X-Title": "DreamBees CLI",
|
|
338
|
+
"Content-Type": "application/json",
|
|
339
|
+
Accept: "text/event-stream",
|
|
340
|
+
"Cache-Control": "no-cache",
|
|
341
|
+
Connection: "keep-alive",
|
|
342
|
+
},
|
|
343
|
+
body: safeStringify({
|
|
344
|
+
model: translatedModel,
|
|
345
|
+
messages: getOpenAiMessages(params),
|
|
346
|
+
tools: getOpenAiTools(params),
|
|
347
|
+
max_tokens: params.max_tokens || 1024,
|
|
348
|
+
stream: true,
|
|
349
|
+
include_reasoning: true,
|
|
350
|
+
}),
|
|
351
|
+
signal: controller.signal,
|
|
352
|
+
});
|
|
353
|
+
if (!response.ok) {
|
|
354
|
+
throw new Error(`OpenRouter API error: ${response.status} (model: ${translatedModel})`);
|
|
273
355
|
}
|
|
274
|
-
|
|
275
|
-
throw new Error("OpenRouter
|
|
356
|
+
if (!response.body)
|
|
357
|
+
throw new Error("OpenRouter API returned no body.");
|
|
358
|
+
const reader = response.body.getReader();
|
|
359
|
+
const decoder = new TextDecoder();
|
|
360
|
+
const contentParts = [];
|
|
361
|
+
const reasoningParts = [];
|
|
362
|
+
const bufferParts = [];
|
|
363
|
+
const toolCalls = {};
|
|
364
|
+
const startTime = Date.now();
|
|
365
|
+
onUpdate({ type: "run_started", timestamp: startTime });
|
|
366
|
+
onUpdate({
|
|
367
|
+
type: "stage_change",
|
|
368
|
+
stage: "thinking",
|
|
369
|
+
label: "Processing request...",
|
|
370
|
+
});
|
|
371
|
+
const streamParser = createStreamParser();
|
|
372
|
+
try {
|
|
373
|
+
while (true) {
|
|
374
|
+
const { done, value } = await reader.read();
|
|
375
|
+
if (done)
|
|
376
|
+
break;
|
|
377
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
378
|
+
streamParser.process(chunk, onUpdate, contentParts, reasoningParts);
|
|
379
|
+
}
|
|
276
380
|
}
|
|
277
|
-
|
|
278
|
-
|
|
381
|
+
finally {
|
|
382
|
+
reader.releaseLock();
|
|
279
383
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Translates Anthropic model names to OpenRouter format.
|
|
291
|
-
* OpenRouter expects format like "anthropic/claude-3.5-sonnet"
|
|
292
|
-
*/
|
|
293
|
-
translateModelName(model) {
|
|
294
|
-
const normalized = (model || "").trim();
|
|
295
|
-
if (!normalized) {
|
|
296
|
-
return "anthropic/claude-3.5-sonnet";
|
|
297
|
-
}
|
|
298
|
-
// If already in OpenRouter format, return as-is
|
|
299
|
-
if (normalized.includes("/")) {
|
|
300
|
-
return normalized;
|
|
301
|
-
}
|
|
302
|
-
// Map Anthropic model names to OpenRouter format
|
|
303
|
-
const modelMap = {
|
|
304
|
-
"claude-3-5-sonnet-20241022": "anthropic/claude-3.5-sonnet",
|
|
305
|
-
"claude-3-5-sonnet": "anthropic/claude-3.5-sonnet",
|
|
306
|
-
"claude-3-5-haiku-20241022": "anthropic/claude-3.5-haiku",
|
|
307
|
-
"claude-3-5-haiku": "anthropic/claude-3.5-haiku",
|
|
308
|
-
"claude-3-opus-20240229": "anthropic/claude-3-opus",
|
|
309
|
-
"claude-3-opus": "anthropic/claude-3-opus",
|
|
310
|
-
"claude-3-sonnet-20240229": "anthropic/claude-3-sonnet",
|
|
311
|
-
"claude-3-sonnet": "anthropic/claude-3-sonnet",
|
|
312
|
-
"claude-3-haiku-20240307": "anthropic/claude-3-haiku",
|
|
313
|
-
"claude-3-haiku": "anthropic/claude-3-haiku",
|
|
314
|
-
};
|
|
315
|
-
if (modelMap[normalized]) {
|
|
316
|
-
return modelMap[normalized];
|
|
317
|
-
}
|
|
318
|
-
// Try to convert claude models that aren't in the map.
|
|
319
|
-
// Examples:
|
|
320
|
-
// - claude-3-5-sonnet-20241022 => anthropic/claude-3.5-sonnet
|
|
321
|
-
// - claude-3-7-sonnet => anthropic/claude-3.7-sonnet
|
|
322
|
-
if (normalized.startsWith("claude-")) {
|
|
323
|
-
const withoutDateSuffix = normalized.replace(/-20\d{6}$/, "");
|
|
324
|
-
const claudeVersioned = withoutDateSuffix.match(/^claude-(\d+)-(\d+)(-.+)$/);
|
|
325
|
-
if (claudeVersioned) {
|
|
326
|
-
const major = claudeVersioned[1];
|
|
327
|
-
const minor = claudeVersioned[2];
|
|
328
|
-
const variant = claudeVersioned[3];
|
|
329
|
-
return `anthropic/claude-${major}.${minor}${variant}`;
|
|
330
|
-
}
|
|
331
|
-
return `anthropic/${withoutDateSuffix}`;
|
|
332
|
-
}
|
|
333
|
-
// Return as-is if we can't translate
|
|
334
|
-
return normalized;
|
|
335
|
-
}
|
|
336
|
-
getOpenAiMessages(params) {
|
|
337
|
-
const messages = [];
|
|
338
|
-
if (params.system)
|
|
339
|
-
messages.push({ role: "system", content: params.system });
|
|
340
|
-
for (const msg of params.messages) {
|
|
341
|
-
if (typeof msg.content === "string") {
|
|
342
|
-
messages.push({ role: msg.role, content: msg.content });
|
|
343
|
-
}
|
|
344
|
-
else if (Array.isArray(msg.content)) {
|
|
345
|
-
let text = "";
|
|
346
|
-
const tools = [];
|
|
347
|
-
for (const block of msg.content) {
|
|
348
|
-
if (block.type === "text")
|
|
349
|
-
text += block.text;
|
|
350
|
-
else if (block.type === "tool_use")
|
|
351
|
-
tools.push({
|
|
352
|
-
id: block.id,
|
|
353
|
-
type: "function",
|
|
354
|
-
function: {
|
|
355
|
-
name: block.name,
|
|
356
|
-
arguments: JSON.stringify(block.input),
|
|
357
|
-
},
|
|
358
|
-
});
|
|
359
|
-
else if (block.type === "tool_result") {
|
|
360
|
-
messages.push({
|
|
361
|
-
role: "tool",
|
|
362
|
-
tool_call_id: block.tool_use_id,
|
|
363
|
-
content: typeof block.content === "string"
|
|
364
|
-
? block.content
|
|
365
|
-
: JSON.stringify(block.content),
|
|
366
|
-
});
|
|
384
|
+
const fullContent = contentParts.join("");
|
|
385
|
+
streamParser.finalize(fullContent, onUpdate);
|
|
386
|
+
const endTime = Date.now();
|
|
387
|
+
onUpdate({ type: "run_completed", timestamp: endTime, durationMs: endTime - startTime });
|
|
388
|
+
const finalToolUses = [];
|
|
389
|
+
for (const tc of Object.values(streamParser.getToolCalls())) {
|
|
390
|
+
try {
|
|
391
|
+
finalToolUses.push({ id: tc.id, name: tc.name, input: safeParseJson(tc.arguments).result });
|
|
367
392
|
}
|
|
393
|
+
catch { }
|
|
368
394
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
395
|
+
const blocks = [];
|
|
396
|
+
if (reasoningParts.length > 0)
|
|
397
|
+
blocks.push({ type: "thought", text: reasoningParts.join("") });
|
|
398
|
+
if (contentParts.length > 0)
|
|
399
|
+
blocks.push({ type: "text", text: contentParts.join("") });
|
|
400
|
+
return {
|
|
401
|
+
role: "assistant",
|
|
402
|
+
content: blocks.length > 0 ? blocks : fullContent,
|
|
403
|
+
tool_uses: finalToolUses.length > 0 ? finalToolUses : undefined,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
catch (error) {
|
|
407
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
408
|
+
// Distinguish between user manual stop and actual timeout
|
|
409
|
+
if (isUserAbort) {
|
|
410
|
+
throw new Error("Request stopped by user");
|
|
411
|
+
}
|
|
412
|
+
else if (isTimeout) {
|
|
413
|
+
throw new Error("OpenRouter timeout");
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
throw new Error("Request aborted");
|
|
417
|
+
}
|
|
377
418
|
}
|
|
419
|
+
throw error;
|
|
378
420
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (!response.ok) {
|
|
418
|
-
throw new Error(`OpenRouter Embedding error: ${response.status}`);
|
|
419
|
-
}
|
|
420
|
-
const data = (await response.json());
|
|
421
|
-
if (!data.data || data.data.length === 0)
|
|
422
|
-
throw new Error("OpenRouter Embedding returned no data.");
|
|
423
|
-
return data.data[0].embedding;
|
|
424
|
-
}
|
|
421
|
+
finally {
|
|
422
|
+
clearTimeout(timeoutId);
|
|
423
|
+
if (signal)
|
|
424
|
+
signal.removeEventListener("abort", onAbort);
|
|
425
|
+
}
|
|
426
|
+
},
|
|
427
|
+
estimateTokens(text) {
|
|
428
|
+
return Math.ceil(text.length * getTokensPerChar());
|
|
429
|
+
},
|
|
430
|
+
async listModels() {
|
|
431
|
+
return [
|
|
432
|
+
{ id: "anthropic/claude-3.5-sonnet", name: "Claude 3.5 Sonnet" },
|
|
433
|
+
{ id: "google/gemini-2.0-flash-001", name: "Gemini 2.0 Flash" },
|
|
434
|
+
];
|
|
435
|
+
},
|
|
436
|
+
async createEmbedding(text) {
|
|
437
|
+
const response = await fetch(`${BASE_URL}/embeddings`, {
|
|
438
|
+
method: "POST",
|
|
439
|
+
headers: {
|
|
440
|
+
Authorization: `Bearer ${apiKey}`,
|
|
441
|
+
"HTTP-Referer": "https://github.com/CardSorting/Marie-Coder-New-the-vatican",
|
|
442
|
+
"X-Title": "DreamBees CLI",
|
|
443
|
+
"Content-Type": "application/json",
|
|
444
|
+
},
|
|
445
|
+
body: safeStringify({
|
|
446
|
+
model: "openai/text-embedding-3-small",
|
|
447
|
+
input: text,
|
|
448
|
+
}),
|
|
449
|
+
});
|
|
450
|
+
if (!response.ok) {
|
|
451
|
+
throw new Error(`OpenRouter Embedding error: ${response.status}`);
|
|
452
|
+
}
|
|
453
|
+
const data = (await response.json());
|
|
454
|
+
if (!data.data || data.data.length === 0)
|
|
455
|
+
throw new Error("OpenRouter Embedding returned no data.");
|
|
456
|
+
return data.data[0].embedding;
|
|
457
|
+
},
|
|
458
|
+
};
|
|
425
459
|
}
|
|
426
460
|
//# sourceMappingURL=OpenRouterProvider.js.map
|