UNPKG

gaunt-sloth-assistant

Version:

[![Tests and Lint](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml) [![Integration Tests](https://github.co

76 lines 3.18 kB
import { displayInfo, displayWarning } from '#src/consoleUtils.js'; import { debugLog, debugLogError } from '#src/debugUtils.js'; import { env } from '#src/systemUtils.js'; import { writeFileIfNotExistsWithMessages } from '#src/utils.js'; import { isAIMessage } from '@langchain/core/messages'; /** * Function to process JSON config and create Anthropic LLM instance */ // noinspection JSUnusedGlobalSymbols export async function processJsonConfig(llmConfig) { const anthropic = await import('@langchain/anthropic'); // Use config value if available, otherwise use the environment variable const anthropicApiKey = llmConfig.apiKey || env.ANTHROPIC_API_KEY; return new anthropic.ChatAnthropic({ ...llmConfig, apiKey: anthropicApiKey, model: llmConfig.model || 'claude-sonnet-4-20250514', }); } const jsonContent = `{ "llm": { "type": "anthropic", "model": "claude-sonnet-4-20250514" } }`; // noinspection JSUnusedGlobalSymbols export function init(configFileName) { // Determine which content to use based on file extension if (!configFileName.endsWith('.json')) { throw new Error('Only JSON config is supported.'); } writeFileIfNotExistsWithMessages(configFileName, jsonContent); displayWarning(`You need to update your ${configFileName} to add your Anthropic API key, ` + 'or define ANTHROPIC_API_KEY environment variable.'); } // noinspection JSUnusedGlobalSymbols export function postProcessJsonConfig(config) { // eslint-disable-next-line if (config.hooks?.postModelHook === 'skip') { return { ...config, hooks: { ...config.hooks, postModelHook: undefined }, }; } displayInfo('Applying Anthropic post-processing to config.'); return { ...config, hooks: { ...config.hooks, postModelHook: config.hooks?.postModelHook || postModelHook }, }; } /** * There's something off with calling server tools with ReAct agent, * the tool is not added in react_agent_executor because it is not Runnable, * but the tool_node explodes because LLM reports calling non-existing tool. * This method removes tool calls from messages, leaving the resulting content. * This method seems unnecessary with OpenAI, but is needed for Anthropic, * OpenAI does not need a name on the tool and does not seem to return server_tool_use. */ export function postModelHook(state) { try { const lastMessage = state.messages[state.messages.length - 1]; if (isAIMessage(lastMessage) && lastMessage.tool_calls && Array.isArray(lastMessage.content)) { const serverToolsCalled = lastMessage.content .filter((content) => content.type == 'server_tool_use' && content.name) .map((content) => content.name); debugLog('found server tool calls ' + serverToolsCalled.join(',')); lastMessage.tool_calls = lastMessage.tool_calls.filter((tc) => !serverToolsCalled.includes(tc.name)); } return state; } catch (e) { debugLogError('removeServerToolCalls error', e); return state; } } //# sourceMappingURL=anthropic.js.map