erosolar-cli 1.5.2 → 1.5.4
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/config/security-deployment.json +54 -0
- package/dist/bin/adapters/node/index.js +33 -0
- package/dist/bin/adapters/types.js +1 -0
- package/dist/bin/alpha-zero/agentWrapper.js +165 -0
- package/dist/bin/alpha-zero/codeEvaluator.js +272 -0
- package/dist/bin/alpha-zero/competitiveRunner.js +219 -0
- package/dist/bin/alpha-zero/index.js +98 -0
- package/dist/bin/alpha-zero/introspection.js +298 -0
- package/dist/bin/alpha-zero/metricsTracker.js +207 -0
- package/dist/bin/alpha-zero/security/core.js +269 -0
- package/dist/bin/alpha-zero/security/google.js +308 -0
- package/dist/bin/alpha-zero/security/googleLoader.js +40 -0
- package/dist/bin/alpha-zero/security/index.js +31 -0
- package/dist/bin/alpha-zero/security/simulation.js +274 -0
- package/dist/bin/alpha-zero/selfModification.js +231 -0
- package/dist/bin/alpha-zero/types.js +30 -0
- package/dist/bin/bin/erosolar-optimized.js +205 -0
- package/dist/bin/capabilities/agentSpawningCapability.js +116 -0
- package/dist/bin/capabilities/bashCapability.js +22 -0
- package/dist/bin/capabilities/cloudCapability.js +36 -0
- package/dist/bin/capabilities/codeAnalysisCapability.js +22 -0
- package/dist/bin/capabilities/codeQualityCapability.js +23 -0
- package/dist/bin/capabilities/dependencySecurityCapability.js +22 -0
- package/dist/bin/capabilities/devCapability.js +22 -0
- package/dist/bin/capabilities/editCapability.js +28 -0
- package/dist/bin/capabilities/emailCapability.js +20 -0
- package/dist/bin/capabilities/enhancedGitCapability.js +221 -0
- package/dist/bin/capabilities/filesystemCapability.js +22 -0
- package/dist/bin/capabilities/globCapability.js +28 -0
- package/dist/bin/capabilities/interactionCapability.js +20 -0
- package/dist/bin/capabilities/learnCapability.js +22 -0
- package/dist/bin/capabilities/mcpCapability.js +20 -0
- package/dist/bin/capabilities/notebookCapability.js +28 -0
- package/dist/bin/capabilities/planningCapability.js +27 -0
- package/dist/bin/capabilities/refactoringCapability.js +23 -0
- package/dist/bin/capabilities/repoChecksCapability.js +22 -0
- package/dist/bin/capabilities/searchCapability.js +22 -0
- package/dist/bin/capabilities/skillCapability.js +76 -0
- package/dist/bin/capabilities/taskManagementCapability.js +20 -0
- package/dist/bin/capabilities/testingCapability.js +23 -0
- package/dist/bin/capabilities/toolManifest.js +159 -0
- package/dist/bin/capabilities/toolRegistry.js +114 -0
- package/dist/bin/capabilities/webCapability.js +20 -0
- package/dist/bin/config.js +139 -0
- package/dist/bin/contracts/v1/agent.js +7 -0
- package/dist/bin/contracts/v1/agentProfileManifest.js +8 -0
- package/dist/bin/contracts/v1/agentRules.js +9 -0
- package/dist/bin/contracts/v1/toolAccess.js +8 -0
- package/dist/bin/core/agent.js +362 -0
- package/dist/bin/core/agentProfileManifest.js +187 -0
- package/dist/bin/core/agentProfiles.js +34 -0
- package/dist/bin/core/agentRulebook.js +135 -0
- package/dist/bin/core/agentSchemaLoader.js +233 -0
- package/dist/bin/core/contextManager.js +412 -0
- package/dist/bin/core/contextWindow.js +122 -0
- package/dist/bin/core/customCommands.js +80 -0
- package/dist/bin/core/errors/apiKeyErrors.js +114 -0
- package/dist/bin/core/errors/errorTypes.js +340 -0
- package/dist/bin/core/errors/safetyValidator.js +304 -0
- package/dist/bin/core/errors.js +32 -0
- package/dist/bin/core/modelDiscovery.js +755 -0
- package/dist/bin/core/preferences.js +224 -0
- package/dist/bin/core/schemaValidator.js +92 -0
- package/dist/bin/core/secretStore.js +199 -0
- package/dist/bin/core/sessionStore.js +187 -0
- package/dist/bin/core/toolRuntime.js +290 -0
- package/dist/bin/core/types.js +1 -0
- package/dist/bin/erosolar-optimized.d.ts +12 -0
- package/dist/bin/erosolar-optimized.d.ts.map +1 -0
- package/dist/bin/erosolar-optimized.js +239 -0
- package/dist/bin/erosolar-optimized.js.map +1 -0
- package/dist/bin/erosolar.js +14 -0
- package/dist/bin/erosolar.js.map +1 -1
- package/dist/bin/headless/headlessApp.js +172 -0
- package/dist/bin/mcp/config.js +202 -0
- package/dist/bin/mcp/stdioClient.js +172 -0
- package/dist/bin/mcp/toolBridge.js +104 -0
- package/dist/bin/mcp/types.js +1 -0
- package/dist/bin/plugins/index.js +113 -0
- package/dist/bin/plugins/providers/anthropic/index.js +25 -0
- package/dist/bin/plugins/providers/deepseek/index.js +24 -0
- package/dist/bin/plugins/providers/google/index.js +26 -0
- package/dist/bin/plugins/providers/index.js +19 -0
- package/dist/bin/plugins/providers/ollama/index.js +59 -0
- package/dist/bin/plugins/providers/openai/index.js +26 -0
- package/dist/bin/plugins/providers/xai/index.js +24 -0
- package/dist/bin/plugins/tools/agentSpawning/agentSpawningPlugin.js +8 -0
- package/dist/bin/plugins/tools/bash/localBashPlugin.js +13 -0
- package/dist/bin/plugins/tools/checks/localRepoChecksPlugin.js +13 -0
- package/dist/bin/plugins/tools/cloud/cloudPlugin.js +13 -0
- package/dist/bin/plugins/tools/codeAnalysis/codeAnalysisPlugin.js +13 -0
- package/dist/bin/plugins/tools/codeQuality/codeQualityPlugin.js +13 -0
- package/dist/bin/plugins/tools/dependency/dependencyPlugin.js +11 -0
- package/dist/bin/plugins/tools/development/devPlugin.js +13 -0
- package/dist/bin/plugins/tools/edit/editPlugin.js +14 -0
- package/dist/bin/plugins/tools/email/emailPlugin.js +11 -0
- package/dist/bin/plugins/tools/enhancedGit/enhancedGitPlugin.js +8 -0
- package/dist/bin/plugins/tools/filesystem/localFilesystemPlugin.js +13 -0
- package/dist/bin/plugins/tools/glob/globPlugin.js +14 -0
- package/dist/bin/plugins/tools/index.js +2 -0
- package/dist/bin/plugins/tools/interaction/interactionPlugin.js +11 -0
- package/dist/bin/plugins/tools/learn/learnPlugin.js +13 -0
- package/dist/bin/plugins/tools/mcp/mcpPlugin.js +8 -0
- package/dist/bin/plugins/tools/nodeDefaults.js +56 -0
- package/dist/bin/plugins/tools/notebook/notebookPlugin.js +14 -0
- package/dist/bin/plugins/tools/planning/planningPlugin.js +14 -0
- package/dist/bin/plugins/tools/refactoring/refactoringPlugin.js +11 -0
- package/dist/bin/plugins/tools/registry.js +57 -0
- package/dist/bin/plugins/tools/search/localSearchPlugin.js +13 -0
- package/dist/bin/plugins/tools/skills/skillPlugin.js +8 -0
- package/dist/bin/plugins/tools/taskManagement/taskManagementPlugin.js +11 -0
- package/dist/bin/plugins/tools/testing/testingPlugin.js +11 -0
- package/dist/bin/plugins/tools/web/webPlugin.js +11 -0
- package/dist/bin/providers/anthropicProvider.js +329 -0
- package/dist/bin/providers/googleProvider.js +203 -0
- package/dist/bin/providers/openaiChatCompletionsProvider.js +208 -0
- package/dist/bin/providers/openaiResponsesProvider.js +249 -0
- package/dist/bin/providers/providerFactory.js +24 -0
- package/dist/bin/runtime/agentController.js +321 -0
- package/dist/bin/runtime/agentHost.js +153 -0
- package/dist/bin/runtime/agentSession.js +195 -0
- package/dist/bin/runtime/node.js +10 -0
- package/dist/bin/runtime/universal.js +28 -0
- package/dist/bin/shell/bracketedPasteManager.js +350 -0
- package/dist/bin/shell/fileChangeTracker.js +65 -0
- package/dist/bin/shell/interactiveShell.js +2908 -0
- package/dist/bin/shell/liveStatus.js +78 -0
- package/dist/bin/shell/shellApp.js +290 -0
- package/dist/bin/shell/systemPrompt.js +60 -0
- package/dist/bin/shell/updateManager.js +108 -0
- package/dist/bin/skills/skillRepository.js +236 -0
- package/dist/bin/skills/types.js +1 -0
- package/dist/bin/subagents/taskRunner.js +269 -0
- package/dist/bin/tools/backgroundBashTools.js +211 -0
- package/dist/bin/tools/bashTools.js +159 -0
- package/dist/bin/tools/cloudTools.js +864 -0
- package/dist/bin/tools/codeAnalysisTools.js +641 -0
- package/dist/bin/tools/codeQualityTools.js +294 -0
- package/dist/bin/tools/dependencyTools.js +282 -0
- package/dist/bin/tools/devTools.js +238 -0
- package/dist/bin/tools/diffUtils.js +137 -0
- package/dist/bin/tools/editTools.js +134 -0
- package/dist/bin/tools/emailTools.js +448 -0
- package/dist/bin/tools/fileTools.js +282 -0
- package/dist/bin/tools/globTools.js +173 -0
- package/dist/bin/tools/grepTools.js +332 -0
- package/dist/bin/tools/interactionTools.js +170 -0
- package/dist/bin/tools/learnTools.js +1818 -0
- package/dist/bin/tools/notebookEditTools.js +196 -0
- package/dist/bin/tools/planningTools.js +46 -0
- package/dist/bin/tools/refactoringTools.js +293 -0
- package/dist/bin/tools/repoChecksTools.js +160 -0
- package/dist/bin/tools/searchTools.js +206 -0
- package/dist/bin/tools/skillTools.js +177 -0
- package/dist/bin/tools/taskManagementTools.js +156 -0
- package/dist/bin/tools/testingTools.js +232 -0
- package/dist/bin/tools/webTools.js +480 -0
- package/dist/bin/ui/ShellUIAdapter.js +459 -0
- package/dist/bin/ui/UnifiedUIController.js +183 -0
- package/dist/bin/ui/animation/AnimationScheduler.js +430 -0
- package/dist/bin/ui/codeHighlighter.js +854 -0
- package/dist/bin/ui/designSystem.js +121 -0
- package/dist/bin/ui/display.js +1222 -0
- package/dist/bin/ui/interrupts/InterruptManager.js +437 -0
- package/dist/bin/ui/layout.js +139 -0
- package/dist/bin/ui/orchestration/StatusOrchestrator.js +403 -0
- package/dist/bin/ui/outputMode.js +38 -0
- package/dist/bin/ui/persistentPrompt.js +183 -0
- package/dist/bin/ui/richText.js +338 -0
- package/dist/bin/ui/shortcutsHelp.js +87 -0
- package/dist/bin/ui/telemetry/UITelemetry.js +443 -0
- package/dist/bin/ui/textHighlighter.js +210 -0
- package/dist/bin/ui/theme.js +116 -0
- package/dist/bin/ui/toolDisplay.js +423 -0
- package/dist/bin/ui/toolDisplayAdapter.js +357 -0
- package/dist/bin/workspace.js +106 -0
- package/dist/bin/workspace.validator.js +213 -0
- package/dist/capabilities/cloudCapability.d.ts +13 -0
- package/dist/capabilities/cloudCapability.d.ts.map +1 -0
- package/dist/capabilities/cloudCapability.js +38 -0
- package/dist/capabilities/cloudCapability.js.map +1 -0
- package/dist/capabilities/index.d.ts +1 -0
- package/dist/capabilities/index.d.ts.map +1 -1
- package/dist/capabilities/index.js +1 -0
- package/dist/capabilities/index.js.map +1 -1
- package/dist/capabilities/offensiveSecurityCapability.d.ts +26 -0
- package/dist/capabilities/offensiveSecurityCapability.d.ts.map +1 -0
- package/dist/capabilities/offensiveSecurityCapability.js +58 -0
- package/dist/capabilities/offensiveSecurityCapability.js.map +1 -0
- package/dist/capabilities/realSecurityCapability.d.ts +26 -0
- package/dist/capabilities/realSecurityCapability.d.ts.map +1 -0
- package/dist/capabilities/realSecurityCapability.js +53 -0
- package/dist/capabilities/realSecurityCapability.js.map +1 -0
- package/dist/capabilities/securityCapability.d.ts +32 -0
- package/dist/capabilities/securityCapability.d.ts.map +1 -0
- package/dist/capabilities/securityCapability.js +57 -0
- package/dist/capabilities/securityCapability.js.map +1 -0
- package/dist/capabilities/ultimateSecurityCapability.d.ts +42 -0
- package/dist/capabilities/ultimateSecurityCapability.d.ts.map +1 -0
- package/dist/capabilities/ultimateSecurityCapability.js +96 -0
- package/dist/capabilities/ultimateSecurityCapability.js.map +1 -0
- package/dist/core/LazyLoader.d.ts +129 -0
- package/dist/core/LazyLoader.d.ts.map +1 -0
- package/dist/core/LazyLoader.js +240 -0
- package/dist/core/LazyLoader.js.map +1 -0
- package/dist/core/intelligenceTools.d.ts +19 -0
- package/dist/core/intelligenceTools.d.ts.map +1 -0
- package/dist/core/intelligenceTools.js +453 -0
- package/dist/core/intelligenceTools.js.map +1 -0
- package/dist/core/operationalTools.d.ts +19 -0
- package/dist/core/operationalTools.d.ts.map +1 -0
- package/dist/core/operationalTools.js +467 -0
- package/dist/core/operationalTools.js.map +1 -0
- package/dist/offensive/core/offensive-engine.d.ts +171 -0
- package/dist/offensive/core/offensive-engine.d.ts.map +1 -0
- package/dist/offensive/core/offensive-engine.js +345 -0
- package/dist/offensive/core/offensive-engine.js.map +1 -0
- package/dist/offensive/core/offensive-integration.d.ts +129 -0
- package/dist/offensive/core/offensive-integration.d.ts.map +1 -0
- package/dist/offensive/core/offensive-integration.js +364 -0
- package/dist/offensive/core/offensive-integration.js.map +1 -0
- package/dist/offensive/core/offensive-tools.d.ts +55 -0
- package/dist/offensive/core/offensive-tools.d.ts.map +1 -0
- package/dist/offensive/core/offensive-tools.js +438 -0
- package/dist/offensive/core/offensive-tools.js.map +1 -0
- package/dist/offensive/offensive-cli.d.ts +48 -0
- package/dist/offensive/offensive-cli.d.ts.map +1 -0
- package/dist/offensive/offensive-cli.js +233 -0
- package/dist/offensive/offensive-cli.js.map +1 -0
- package/dist/plugins/index.d.ts +1 -1
- package/dist/plugins/index.d.ts.map +1 -1
- package/dist/plugins/index.js +2 -0
- package/dist/plugins/index.js.map +1 -1
- package/dist/plugins/tools/cloud/cloudPlugin.d.ts +3 -0
- package/dist/plugins/tools/cloud/cloudPlugin.d.ts.map +1 -0
- package/dist/plugins/tools/cloud/cloudPlugin.js +14 -0
- package/dist/plugins/tools/cloud/cloudPlugin.js.map +1 -0
- package/dist/plugins/tools/nodeDefaults.d.ts.map +1 -1
- package/dist/plugins/tools/nodeDefaults.js +2 -0
- package/dist/plugins/tools/nodeDefaults.js.map +1 -1
- package/dist/security/advanced-persistence-research.d.ts +92 -0
- package/dist/security/advanced-persistence-research.d.ts.map +1 -0
- package/dist/security/advanced-persistence-research.js +195 -0
- package/dist/security/advanced-persistence-research.js.map +1 -0
- package/dist/security/apt-simulation-cli.d.ts +57 -0
- package/dist/security/apt-simulation-cli.d.ts.map +1 -0
- package/dist/security/apt-simulation-cli.js +278 -0
- package/dist/security/apt-simulation-cli.js.map +1 -0
- package/dist/security/apt-simulation-engine-complete.d.ts +97 -0
- package/dist/security/apt-simulation-engine-complete.d.ts.map +1 -0
- package/dist/security/apt-simulation-engine-complete.js +441 -0
- package/dist/security/apt-simulation-engine-complete.js.map +1 -0
- package/dist/security/apt-simulation-engine.d.ts +97 -0
- package/dist/security/apt-simulation-engine.d.ts.map +1 -0
- package/dist/security/apt-simulation-engine.js +441 -0
- package/dist/security/apt-simulation-engine.js.map +1 -0
- package/dist/security/assessment/vulnerabilityAssessment.d.ts +104 -0
- package/dist/security/assessment/vulnerabilityAssessment.d.ts.map +1 -0
- package/dist/security/assessment/vulnerabilityAssessment.js +315 -0
- package/dist/security/assessment/vulnerabilityAssessment.js.map +1 -0
- package/dist/security/authorization/securityAuthorization.d.ts +88 -0
- package/dist/security/authorization/securityAuthorization.d.ts.map +1 -0
- package/dist/security/authorization/securityAuthorization.js +172 -0
- package/dist/security/authorization/securityAuthorization.js.map +1 -0
- package/dist/security/authorization.d.ts +45 -0
- package/dist/security/authorization.d.ts.map +1 -0
- package/dist/security/authorization.js +128 -0
- package/dist/security/authorization.js.map +1 -0
- package/dist/security/comprehensive-security-research.d.ts +84 -0
- package/dist/security/comprehensive-security-research.d.ts.map +1 -0
- package/dist/security/comprehensive-security-research.js +211 -0
- package/dist/security/comprehensive-security-research.js.map +1 -0
- package/dist/security/offensive/exploitationEngine.d.ts +54 -0
- package/dist/security/offensive/exploitationEngine.d.ts.map +1 -0
- package/dist/security/offensive/exploitationEngine.js +263 -0
- package/dist/security/offensive/exploitationEngine.js.map +1 -0
- package/dist/security/persistence-analyzer.d.ts +56 -0
- package/dist/security/persistence-analyzer.d.ts.map +1 -0
- package/dist/security/persistence-analyzer.js +187 -0
- package/dist/security/persistence-analyzer.js.map +1 -0
- package/dist/security/persistence-cli.d.ts +36 -0
- package/dist/security/persistence-cli.d.ts.map +1 -0
- package/dist/security/persistence-cli.js +160 -0
- package/dist/security/persistence-cli.js.map +1 -0
- package/dist/security/persistence-research.d.ts +100 -0
- package/dist/security/persistence-research.d.ts.map +1 -0
- package/dist/security/persistence-research.js +372 -0
- package/dist/security/persistence-research.js.map +1 -0
- package/dist/security/real/networkExploitation.d.ts +92 -0
- package/dist/security/real/networkExploitation.d.ts.map +1 -0
- package/dist/security/real/networkExploitation.js +316 -0
- package/dist/security/real/networkExploitation.js.map +1 -0
- package/dist/security/real/persistenceImplementation.d.ts +62 -0
- package/dist/security/real/persistenceImplementation.d.ts.map +1 -0
- package/dist/security/real/persistenceImplementation.js +323 -0
- package/dist/security/real/persistenceImplementation.js.map +1 -0
- package/dist/security/real/vulnerabilityScanner.d.ts +73 -0
- package/dist/security/real/vulnerabilityScanner.d.ts.map +1 -0
- package/dist/security/real/vulnerabilityScanner.js +341 -0
- package/dist/security/real/vulnerabilityScanner.js.map +1 -0
- package/dist/security/research/persistenceResearch.d.ts +97 -0
- package/dist/security/research/persistenceResearch.d.ts.map +1 -0
- package/dist/security/research/persistenceResearch.js +282 -0
- package/dist/security/research/persistenceResearch.js.map +1 -0
- package/dist/security/security-testing-framework.d.ts +120 -0
- package/dist/security/security-testing-framework.d.ts.map +1 -0
- package/dist/security/security-testing-framework.js +372 -0
- package/dist/security/security-testing-framework.js.map +1 -0
- package/dist/security/simulation/attackSimulation.d.ts +93 -0
- package/dist/security/simulation/attackSimulation.d.ts.map +1 -0
- package/dist/security/simulation/attackSimulation.js +341 -0
- package/dist/security/simulation/attackSimulation.js.map +1 -0
- package/dist/shell/bracketedPasteManager.d.ts +76 -0
- package/dist/shell/bracketedPasteManager.d.ts.map +1 -1
- package/dist/shell/bracketedPasteManager.js +267 -9
- package/dist/shell/bracketedPasteManager.js.map +1 -1
- package/dist/shell/interactiveShell.d.ts +34 -1
- package/dist/shell/interactiveShell.d.ts.map +1 -1
- package/dist/shell/interactiveShell.js +304 -24
- package/dist/shell/interactiveShell.js.map +1 -1
- package/dist/shell/taskCompletionDetector.d.ts +101 -0
- package/dist/shell/taskCompletionDetector.d.ts.map +1 -0
- package/dist/shell/taskCompletionDetector.js +343 -0
- package/dist/shell/taskCompletionDetector.js.map +1 -0
- package/dist/tools/cloudTools.d.ts +57 -0
- package/dist/tools/cloudTools.d.ts.map +1 -0
- package/dist/tools/cloudTools.js +865 -0
- package/dist/tools/cloudTools.js.map +1 -0
- package/dist/tools/enhancedSecurityTools.d.ts +19 -0
- package/dist/tools/enhancedSecurityTools.d.ts.map +1 -0
- package/dist/tools/enhancedSecurityTools.js +215 -0
- package/dist/tools/enhancedSecurityTools.js.map +1 -0
- package/dist/tools/offensiveSecurityTools.d.ts +16 -0
- package/dist/tools/offensiveSecurityTools.d.ts.map +1 -0
- package/dist/tools/offensiveSecurityTools.js +285 -0
- package/dist/tools/offensiveSecurityTools.js.map +1 -0
- package/dist/tools/realSecurityTools.d.ts +18 -0
- package/dist/tools/realSecurityTools.d.ts.map +1 -0
- package/dist/tools/realSecurityTools.js +468 -0
- package/dist/tools/realSecurityTools.js.map +1 -0
- package/dist/tools/securityTools.d.ts +20 -0
- package/dist/tools/securityTools.d.ts.map +1 -0
- package/dist/tools/securityTools.js +449 -0
- package/dist/tools/securityTools.js.map +1 -0
- package/package.json +27 -12
- package/scripts/deploy-security-capabilities.js +178 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const registry = new Map();
|
|
2
|
+
export function registerToolPlugin(plugin) {
|
|
3
|
+
if (!plugin?.id?.trim()) {
|
|
4
|
+
throw new Error('Tool plugin id cannot be blank.');
|
|
5
|
+
}
|
|
6
|
+
registry.set(plugin.id, normalizePlugin(plugin));
|
|
7
|
+
}
|
|
8
|
+
export function unregisterToolPlugin(id) {
|
|
9
|
+
registry.delete(id);
|
|
10
|
+
}
|
|
11
|
+
export function listRegisteredToolPlugins() {
|
|
12
|
+
return Array.from(registry.values());
|
|
13
|
+
}
|
|
14
|
+
export async function instantiateToolPlugins(target, context, options = {}) {
|
|
15
|
+
const modules = [];
|
|
16
|
+
for (const plugin of registry.values()) {
|
|
17
|
+
if (!supportsTarget(plugin, target)) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (options.filter && !options.filter(plugin)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const result = await plugin.create(context);
|
|
24
|
+
if (!result) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(result)) {
|
|
28
|
+
for (const entry of result) {
|
|
29
|
+
if (entry) {
|
|
30
|
+
modules.push(entry);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
modules.push(result);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return modules;
|
|
39
|
+
}
|
|
40
|
+
function normalizePlugin(plugin) {
|
|
41
|
+
if (!plugin.targets?.length) {
|
|
42
|
+
return {
|
|
43
|
+
...plugin,
|
|
44
|
+
targets: ['universal'],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
...plugin,
|
|
49
|
+
targets: Array.from(new Set(plugin.targets)),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function supportsTarget(plugin, target) {
|
|
53
|
+
if (plugin.targets.includes('universal')) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return plugin.targets.includes(target);
|
|
57
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { SearchCapabilityModule } from '../../../capabilities/searchCapability.js';
|
|
2
|
+
export function createLocalSearchToolPlugin() {
|
|
3
|
+
return {
|
|
4
|
+
id: 'tool.search.local',
|
|
5
|
+
description: 'Local ripgrep-based search tooling.',
|
|
6
|
+
targets: ['node', 'cloud'],
|
|
7
|
+
create: (context) => {
|
|
8
|
+
return new SearchCapabilityModule({
|
|
9
|
+
workingDir: context.workingDir,
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TaskManagementCapabilityModule } from '../../../capabilities/taskManagementCapability.js';
|
|
2
|
+
export function createTaskManagementToolPlugin() {
|
|
3
|
+
return {
|
|
4
|
+
id: 'tool.task-management.local',
|
|
5
|
+
description: 'Task tracking and planning tools for organizing complex multi-step work.',
|
|
6
|
+
targets: ['node', 'cloud'],
|
|
7
|
+
create: (_context) => {
|
|
8
|
+
return new TaskManagementCapabilityModule({});
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { TestingCapabilityModule } from '../../../capabilities/testingCapability.js';
|
|
2
|
+
export function createTestingToolPlugin() {
|
|
3
|
+
return {
|
|
4
|
+
id: 'tool.testing.coverage',
|
|
5
|
+
description: 'Test generation scaffolding plus coverage execution/reporting utilities.',
|
|
6
|
+
targets: ['node'],
|
|
7
|
+
create: async (context) => new TestingCapabilityModule({
|
|
8
|
+
workingDir: context.workingDir,
|
|
9
|
+
}),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { WebCapabilityModule } from '../../../capabilities/webCapability.js';
|
|
2
|
+
export function createWebToolPlugin() {
|
|
3
|
+
return {
|
|
4
|
+
id: 'tool.web.local',
|
|
5
|
+
description: 'Web content fetching and search tools for accessing online information.',
|
|
6
|
+
targets: ['node', 'cloud'],
|
|
7
|
+
create: (_context) => {
|
|
8
|
+
return new WebCapabilityModule({});
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
2
|
+
import { APIError, RateLimitError } from '@anthropic-ai/sdk/error.js';
|
|
3
|
+
const DEFAULT_RATE_LIMIT_RETRIES = 4;
|
|
4
|
+
const DEFAULT_RATE_LIMIT_INITIAL_DELAY_MS = 1500;
|
|
5
|
+
const MIN_RATE_LIMIT_DELAY_MS = 750;
|
|
6
|
+
const MAX_RATE_LIMIT_DELAY_MS = 40000;
|
|
7
|
+
export class AnthropicMessagesProvider {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.id = 'anthropic';
|
|
10
|
+
this.client = new Anthropic({ apiKey: options.apiKey });
|
|
11
|
+
this.model = options.model;
|
|
12
|
+
this.maxTokens = options.maxTokens ?? 2048;
|
|
13
|
+
this.temperature = options.temperature ?? 0;
|
|
14
|
+
this.rateLimitMaxRetries = Math.max(0, options.rateLimitMaxRetries ?? DEFAULT_RATE_LIMIT_RETRIES);
|
|
15
|
+
this.rateLimitInitialDelayMs = Math.max(MIN_RATE_LIMIT_DELAY_MS, options.rateLimitInitialDelayMs ?? DEFAULT_RATE_LIMIT_INITIAL_DELAY_MS);
|
|
16
|
+
this.enablePromptCaching = options.enablePromptCaching ?? true;
|
|
17
|
+
}
|
|
18
|
+
async generate(messages, tools) {
|
|
19
|
+
const { system, chat } = convertConversation(messages, this.enablePromptCaching);
|
|
20
|
+
const response = await this.executeWithRateLimitRetries(() => this.client.messages.create({
|
|
21
|
+
model: this.model,
|
|
22
|
+
max_tokens: this.maxTokens,
|
|
23
|
+
temperature: this.temperature,
|
|
24
|
+
messages: chat,
|
|
25
|
+
...(system ? { system } : {}),
|
|
26
|
+
...(tools.length ? { tools: tools.map(mapTool) } : {}),
|
|
27
|
+
}));
|
|
28
|
+
const usage = mapUsage(response.usage);
|
|
29
|
+
const toolCalls = response.content
|
|
30
|
+
.filter((block) => block.type === 'tool_use')
|
|
31
|
+
.map((block) => ({
|
|
32
|
+
id: block.id,
|
|
33
|
+
name: block.name,
|
|
34
|
+
arguments: toRecord(block.input),
|
|
35
|
+
}));
|
|
36
|
+
const textContent = response.content
|
|
37
|
+
.filter((block) => block.type === 'text')
|
|
38
|
+
.map((block) => ('text' in block ? block.text : ''))
|
|
39
|
+
.join('\n')
|
|
40
|
+
.trim();
|
|
41
|
+
if (toolCalls.length > 0) {
|
|
42
|
+
return {
|
|
43
|
+
type: 'tool_calls',
|
|
44
|
+
toolCalls,
|
|
45
|
+
content: textContent,
|
|
46
|
+
usage,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
type: 'message',
|
|
51
|
+
content: textContent,
|
|
52
|
+
usage,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
async *generateStream(messages, tools) {
|
|
56
|
+
const { system, chat } = convertConversation(messages, this.enablePromptCaching);
|
|
57
|
+
const stream = this.client.messages.stream({
|
|
58
|
+
model: this.model,
|
|
59
|
+
max_tokens: this.maxTokens,
|
|
60
|
+
temperature: this.temperature,
|
|
61
|
+
messages: chat,
|
|
62
|
+
...(system ? { system } : {}),
|
|
63
|
+
...(tools.length ? { tools: tools.map(mapTool) } : {}),
|
|
64
|
+
});
|
|
65
|
+
let currentToolCall = null;
|
|
66
|
+
let currentToolCallInput = '';
|
|
67
|
+
let toolCallId = '';
|
|
68
|
+
for await (const event of stream) {
|
|
69
|
+
// Handle different event types
|
|
70
|
+
if (event.type === 'content_block_start') {
|
|
71
|
+
const block = event.content_block;
|
|
72
|
+
if (block.type === 'tool_use') {
|
|
73
|
+
toolCallId = block.id;
|
|
74
|
+
currentToolCall = {
|
|
75
|
+
id: block.id,
|
|
76
|
+
name: block.name,
|
|
77
|
+
arguments: {},
|
|
78
|
+
};
|
|
79
|
+
currentToolCallInput = '';
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else if (event.type === 'content_block_delta') {
|
|
83
|
+
const delta = event.delta;
|
|
84
|
+
if (delta.type === 'text_delta') {
|
|
85
|
+
yield {
|
|
86
|
+
type: 'content',
|
|
87
|
+
content: delta.text,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
else if (delta.type === 'input_json_delta' && currentToolCall) {
|
|
91
|
+
// accumulate tool input JSON fragments
|
|
92
|
+
if (typeof delta.partial_json === 'string' && delta.partial_json) {
|
|
93
|
+
currentToolCallInput += delta.partial_json;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (event.type === 'content_block_stop') {
|
|
98
|
+
if (currentToolCall && toolCallId) {
|
|
99
|
+
if (currentToolCallInput.trim()) {
|
|
100
|
+
try {
|
|
101
|
+
currentToolCall.arguments = toRecord(JSON.parse(currentToolCallInput));
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
currentToolCall.arguments = {};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
yield {
|
|
108
|
+
type: 'tool_call',
|
|
109
|
+
toolCall: currentToolCall,
|
|
110
|
+
};
|
|
111
|
+
currentToolCall = null;
|
|
112
|
+
currentToolCallInput = '';
|
|
113
|
+
toolCallId = '';
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
else if (event.type === 'message_stop') {
|
|
117
|
+
const finalMessage = await stream.finalMessage();
|
|
118
|
+
const usage = mapUsage(finalMessage.usage);
|
|
119
|
+
if (usage) {
|
|
120
|
+
yield {
|
|
121
|
+
type: 'usage',
|
|
122
|
+
usage,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
yield {
|
|
126
|
+
type: 'done',
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
getCapabilities() {
|
|
132
|
+
return {
|
|
133
|
+
streaming: true,
|
|
134
|
+
toolCalling: true,
|
|
135
|
+
vision: this.model.includes('sonnet') || this.model.includes('opus'),
|
|
136
|
+
functionCalling: true,
|
|
137
|
+
maxTokens: this.maxTokens,
|
|
138
|
+
supportedModalities: ['text', 'image'],
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
async executeWithRateLimitRetries(operation) {
|
|
142
|
+
let retries = 0;
|
|
143
|
+
let delayMs = this.rateLimitInitialDelayMs;
|
|
144
|
+
while (true) {
|
|
145
|
+
try {
|
|
146
|
+
return await operation();
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (!isRateLimitError(error)) {
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
if (retries >= this.rateLimitMaxRetries) {
|
|
153
|
+
throw buildRateLimitFailureError(error, retries);
|
|
154
|
+
}
|
|
155
|
+
const waitMs = determineRetryDelay(error.headers, delayMs);
|
|
156
|
+
await sleep(waitMs);
|
|
157
|
+
retries += 1;
|
|
158
|
+
delayMs = Math.min(delayMs * 2, MAX_RATE_LIMIT_DELAY_MS);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function convertConversation(messages, enablePromptCaching = false) {
|
|
164
|
+
const systemPrompts = [];
|
|
165
|
+
const chat = [];
|
|
166
|
+
for (const message of messages) {
|
|
167
|
+
switch (message.role) {
|
|
168
|
+
case 'system': {
|
|
169
|
+
systemPrompts.push(message.content);
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case 'user': {
|
|
173
|
+
chat.push({
|
|
174
|
+
role: 'user',
|
|
175
|
+
content: [{ type: 'text', text: message.content }],
|
|
176
|
+
});
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
case 'assistant': {
|
|
180
|
+
const contentBlocks = [];
|
|
181
|
+
if (message.content.trim().length > 0) {
|
|
182
|
+
contentBlocks.push({ type: 'text', text: message.content });
|
|
183
|
+
}
|
|
184
|
+
for (const call of message.toolCalls ?? []) {
|
|
185
|
+
contentBlocks.push({
|
|
186
|
+
type: 'tool_use',
|
|
187
|
+
id: call.id,
|
|
188
|
+
name: call.name,
|
|
189
|
+
input: call.arguments,
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
chat.push({
|
|
193
|
+
role: 'assistant',
|
|
194
|
+
content: contentBlocks.length ? contentBlocks : [{ type: 'text', text: '' }],
|
|
195
|
+
});
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case 'tool': {
|
|
199
|
+
const block = {
|
|
200
|
+
type: 'tool_result',
|
|
201
|
+
tool_use_id: message.toolCallId,
|
|
202
|
+
content: [{ type: 'text', text: message.content }],
|
|
203
|
+
};
|
|
204
|
+
chat.push({
|
|
205
|
+
role: 'user',
|
|
206
|
+
content: [block],
|
|
207
|
+
});
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
default:
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Add cache control breakpoints to optimize costs
|
|
215
|
+
// Cache the first few user messages (usually contain system context)
|
|
216
|
+
if (enablePromptCaching && chat.length > 2) {
|
|
217
|
+
const cacheBreakpoint = Math.min(2, chat.length - 1);
|
|
218
|
+
for (let i = 0; i < cacheBreakpoint; i++) {
|
|
219
|
+
const message = chat[i];
|
|
220
|
+
if (message && message.role === 'user' && Array.isArray(message.content)) {
|
|
221
|
+
const lastContent = message.content[message.content.length - 1];
|
|
222
|
+
if (lastContent && 'text' in lastContent) {
|
|
223
|
+
lastContent['cache_control'] = { type: 'ephemeral' };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
system: systemPrompts.length ? systemPrompts.join('\n\n') : null,
|
|
230
|
+
chat,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
function mapTool(definition) {
|
|
234
|
+
return {
|
|
235
|
+
name: definition.name,
|
|
236
|
+
description: definition.description,
|
|
237
|
+
input_schema: definition.parameters ?? {
|
|
238
|
+
type: 'object',
|
|
239
|
+
properties: {},
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function toRecord(value) {
|
|
244
|
+
if (isPlainRecord(value)) {
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
if (typeof value === 'string') {
|
|
248
|
+
const trimmed = value.trim();
|
|
249
|
+
if (!trimmed) {
|
|
250
|
+
return {};
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
const parsed = JSON.parse(trimmed);
|
|
254
|
+
return isPlainRecord(parsed) ? parsed : {};
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
return {};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return {};
|
|
261
|
+
}
|
|
262
|
+
function isPlainRecord(value) {
|
|
263
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
264
|
+
}
|
|
265
|
+
function mapUsage(usage) {
|
|
266
|
+
if (!usage) {
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
const total = typeof usage.input_tokens === 'number' && typeof usage.output_tokens === 'number'
|
|
270
|
+
? usage.input_tokens + usage.output_tokens
|
|
271
|
+
: undefined;
|
|
272
|
+
return {
|
|
273
|
+
inputTokens: usage.input_tokens,
|
|
274
|
+
outputTokens: usage.output_tokens,
|
|
275
|
+
totalTokens: total,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function isRateLimitError(error) {
|
|
279
|
+
if (error instanceof RateLimitError) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
if (error instanceof APIError && error.status === 429) {
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
return typeof error === 'object' && error !== null && 'status' in error && error.status === 429;
|
|
286
|
+
}
|
|
287
|
+
function determineRetryDelay(headers, fallbackMs) {
|
|
288
|
+
const retryAfter = parseRetryAfterHeader(headers);
|
|
289
|
+
if (retryAfter !== null) {
|
|
290
|
+
return clamp(retryAfter, MIN_RATE_LIMIT_DELAY_MS, MAX_RATE_LIMIT_DELAY_MS);
|
|
291
|
+
}
|
|
292
|
+
const jitter = fallbackMs * 0.25;
|
|
293
|
+
const randomized = fallbackMs + (Math.random() * (2 * jitter) - jitter);
|
|
294
|
+
return clamp(Math.round(randomized), MIN_RATE_LIMIT_DELAY_MS, MAX_RATE_LIMIT_DELAY_MS);
|
|
295
|
+
}
|
|
296
|
+
function parseRetryAfterHeader(headers) {
|
|
297
|
+
if (!headers || typeof headers.get !== 'function') {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const retryAfter = headers.get('retry-after');
|
|
301
|
+
if (!retryAfter) {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
const numeric = Number(retryAfter);
|
|
305
|
+
if (Number.isFinite(numeric) && numeric >= 0) {
|
|
306
|
+
return numeric * 1000;
|
|
307
|
+
}
|
|
308
|
+
const parsedDate = Date.parse(retryAfter);
|
|
309
|
+
if (Number.isFinite(parsedDate)) {
|
|
310
|
+
return Math.max(0, parsedDate - Date.now());
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
function clamp(value, min, max) {
|
|
315
|
+
return Math.max(min, Math.min(max, value));
|
|
316
|
+
}
|
|
317
|
+
async function sleep(durationMs) {
|
|
318
|
+
await new Promise((resolve) => setTimeout(resolve, durationMs));
|
|
319
|
+
}
|
|
320
|
+
function buildRateLimitFailureError(error, retries) {
|
|
321
|
+
const baseMessage = 'Anthropic rejected the request because the per-minute token rate limit was exceeded.';
|
|
322
|
+
const retryDetails = retries > 0 ? ` Waited and retried ${retries} time${retries === 1 ? '' : 's'} without success.` : '';
|
|
323
|
+
const advisory = ' Reduce the prompt size or wait for usage to reset before trying again. ' +
|
|
324
|
+
'See https://docs.claude.com/en/api/rate-limits for quota guidance.';
|
|
325
|
+
const original = error.message ? `\nOriginal message: ${error.message}` : '';
|
|
326
|
+
return new Error(`${baseMessage}${retryDetails}${advisory}${original}`, {
|
|
327
|
+
cause: error,
|
|
328
|
+
});
|
|
329
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { GoogleGenAI, } from '@google/genai';
|
|
2
|
+
export class GoogleGenAIProvider {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.client = new GoogleGenAI({
|
|
5
|
+
apiKey: options.apiKey,
|
|
6
|
+
});
|
|
7
|
+
this.id = options.providerId ?? 'google';
|
|
8
|
+
this.model = options.model;
|
|
9
|
+
this.temperature = options.temperature;
|
|
10
|
+
this.maxOutputTokens = options.maxOutputTokens;
|
|
11
|
+
}
|
|
12
|
+
async generate(messages, tools) {
|
|
13
|
+
const { contents, systemInstruction } = mapConversation(messages);
|
|
14
|
+
const config = {};
|
|
15
|
+
if (systemInstruction) {
|
|
16
|
+
config.systemInstruction = systemInstruction;
|
|
17
|
+
}
|
|
18
|
+
if (typeof this.temperature === 'number') {
|
|
19
|
+
config.temperature = this.temperature;
|
|
20
|
+
}
|
|
21
|
+
if (typeof this.maxOutputTokens === 'number') {
|
|
22
|
+
config.maxOutputTokens = this.maxOutputTokens;
|
|
23
|
+
}
|
|
24
|
+
const mappedTools = mapTools(tools);
|
|
25
|
+
if (mappedTools.length > 0) {
|
|
26
|
+
config.tools = mappedTools;
|
|
27
|
+
}
|
|
28
|
+
const response = await this.client.models.generateContent({
|
|
29
|
+
model: this.model,
|
|
30
|
+
contents: contents.length ? contents : createEmptyUserContent(),
|
|
31
|
+
config: Object.keys(config).length ? config : undefined,
|
|
32
|
+
});
|
|
33
|
+
const usage = mapUsage(response.usageMetadata);
|
|
34
|
+
const toolCalls = mapFunctionCalls(response.functionCalls ?? []);
|
|
35
|
+
const content = response.text?.trim() ?? '';
|
|
36
|
+
if (toolCalls.length > 0) {
|
|
37
|
+
return {
|
|
38
|
+
type: 'tool_calls',
|
|
39
|
+
toolCalls,
|
|
40
|
+
content,
|
|
41
|
+
usage,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
type: 'message',
|
|
46
|
+
content,
|
|
47
|
+
usage,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function mapConversation(messages) {
|
|
52
|
+
const contents = [];
|
|
53
|
+
const systemPrompts = [];
|
|
54
|
+
for (const message of messages) {
|
|
55
|
+
switch (message.role) {
|
|
56
|
+
case 'system': {
|
|
57
|
+
if (message.content.trim()) {
|
|
58
|
+
systemPrompts.push(message.content.trim());
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'user': {
|
|
63
|
+
contents.push({
|
|
64
|
+
role: 'user',
|
|
65
|
+
parts: [{ text: message.content }],
|
|
66
|
+
});
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case 'assistant': {
|
|
70
|
+
contents.push(mapAssistantMessage(message));
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
case 'tool': {
|
|
74
|
+
const content = mapToolMessage(message);
|
|
75
|
+
if (content) {
|
|
76
|
+
contents.push(content);
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
default:
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
contents,
|
|
86
|
+
systemInstruction: systemPrompts.length ? systemPrompts.join('\n\n') : undefined,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function mapAssistantMessage(message) {
|
|
90
|
+
const parts = [];
|
|
91
|
+
const text = message.content.trim();
|
|
92
|
+
if (text) {
|
|
93
|
+
parts.push({ text });
|
|
94
|
+
}
|
|
95
|
+
for (const call of message.toolCalls ?? []) {
|
|
96
|
+
parts.push({
|
|
97
|
+
functionCall: {
|
|
98
|
+
id: call.id || undefined,
|
|
99
|
+
name: call.name,
|
|
100
|
+
args: toRecord(call.arguments),
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
role: 'model',
|
|
106
|
+
parts: parts.length ? parts : [{ text: '' }],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function mapToolMessage(message) {
|
|
110
|
+
if (!message.toolCallId) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
role: 'user',
|
|
115
|
+
parts: [
|
|
116
|
+
{
|
|
117
|
+
functionResponse: {
|
|
118
|
+
id: message.toolCallId,
|
|
119
|
+
name: message.name,
|
|
120
|
+
response: parseToolResponse(message.content),
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function parseToolResponse(content) {
|
|
127
|
+
const trimmed = content.trim();
|
|
128
|
+
if (!trimmed) {
|
|
129
|
+
return { output: '' };
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
const parsed = JSON.parse(trimmed);
|
|
133
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
134
|
+
return parsed;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
}
|
|
139
|
+
return { output: content };
|
|
140
|
+
}
|
|
141
|
+
function mapFunctionCalls(calls) {
|
|
142
|
+
return calls
|
|
143
|
+
.filter((call) => Boolean(call.name))
|
|
144
|
+
.map((call) => ({
|
|
145
|
+
id: call.id || call.name || 'function_call',
|
|
146
|
+
name: call.name ?? 'function_call',
|
|
147
|
+
arguments: toRecord(call.args),
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
function createEmptyUserContent() {
|
|
151
|
+
return [
|
|
152
|
+
{
|
|
153
|
+
role: 'user',
|
|
154
|
+
parts: [{ text: '' }],
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
function mapTools(tools) {
|
|
159
|
+
if (!tools.length) {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
return [
|
|
163
|
+
{
|
|
164
|
+
functionDeclarations: tools.map((tool) => ({
|
|
165
|
+
name: tool.name,
|
|
166
|
+
description: tool.description,
|
|
167
|
+
parametersJsonSchema: tool.parameters ?? { type: 'object', properties: {} },
|
|
168
|
+
})),
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
function mapUsage(metadata) {
|
|
173
|
+
if (!metadata) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
inputTokens: metadata.promptTokenCount ?? undefined,
|
|
178
|
+
outputTokens: metadata.candidatesTokenCount ?? undefined,
|
|
179
|
+
totalTokens: metadata.totalTokenCount ?? undefined,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function toRecord(value) {
|
|
183
|
+
if (isPlainRecord(value)) {
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
if (typeof value === 'string') {
|
|
187
|
+
const trimmed = value.trim();
|
|
188
|
+
if (!trimmed) {
|
|
189
|
+
return {};
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
const parsed = JSON.parse(trimmed);
|
|
193
|
+
return isPlainRecord(parsed) ? parsed : {};
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
return {};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return {};
|
|
200
|
+
}
|
|
201
|
+
function isPlainRecord(value) {
|
|
202
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
203
|
+
}
|