feat(ollama): add tool calling support

Add Ollama tool integration for web search, weather, datetime, filesystem operations, and shell evaluation. Implement multi-round tool call handling, streaming updates, thinking cleanup, and safer path validation for file tools.

Also add related environment configuration, command execution helper, MarkdownV2 cancel handling fixes, and remove unused TryAgain callback command.
This commit is contained in:
2026-05-03 15:16:14 +03:00
parent 86b26813e2
commit 2fc60806ff
7 changed files with 2046 additions and 129 deletions
+67
View File
@@ -47,6 +47,7 @@ import {SendOptions} from "../model/send-options";
import {EditOptions} from "../model/edit-options";
import {StoredUser} from "../model/stored-user";
import {performFFmpeg} from "./ffmpeg";
import {exec} from "node:child_process";
export const ignore = () => {
};
@@ -2171,4 +2172,70 @@ export async function processInlineQuery(query: InlineQuery): Promise<void> {
export async function processCallbackQuery(query: CallbackQuery): Promise<void> {
console.log("CallbackQuery", query);
await findAndExecuteCallbackCommand(callbackCommands, query);
}
export async function runCommand(cmd: string):
Promise<{
stdout: string | null | undefined;
stderr: string | null | undefined
}> {
if (cmd.length > 500) {
throw new Error("Command is too long");
}
const forbiddenPatterns = [
/\bsudo\b/,
/\bsu\b/,
/\brm\b/,
/\brmdir\b/,
/\bchmod\b/,
/\bchown\b/,
/\bdd\b/,
/\bmkfs\b/,
/\bmount\b/,
/\bumount\b/,
/\breboot\b/,
/\bshutdown\b/,
/\bkill\b/,
/\bcurl\b/,
/\bwget\b/,
/\bssh\b/,
/\bscp\b/,
/\brsync\b/,
/\bnc\b/,
/\bnmap\b/,
/\.\./,
/\/etc\/?/,
/\/home\/?/,
/\/root\/?/,
/~\//,
/\.ssh/,
/\.env/,
];
for (const pattern of forbiddenPatterns) {
if (pattern.test(cmd)) {
throw new Error(`Forbidden shell command pattern: ${pattern}`);
}
}
try {
const {stdout, stderr} = exec(cmd);
if (stdout) {
console.log("COMMAND: ", cmd, "\n", 'Output:', stdout);
}
if (stderr) {
console.error("COMMAND: ", cmd, "\n", 'Error:', stderr);
}
return {stdout: (await stdout?.toArray())?.join(""), stderr: (await stderr?.toArray())?.join("")}
} catch (error: any) {
console.error('Error code:', error.code);
console.error('Stderr:', error.stderr);
return {stdout: null, stderr: error.stderr};
}
}