Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat(agent): add send_file tool for channel file sending
Jiayuan Zhanga29366d7cfdc
diff --git a/packages/core/src/agent/tools.ts b/packages/core/src/agent/tools.tsindex edc43dd2b6..551c824599 100644--- a/packages/core/src/agent/tools.ts+++ b/packages/core/src/agent/tools.ts@@ -11,6 +11,8 @@ import { createSessionsListTool } from "./tools/sessions-list.js";import { createMemorySearchTool } from "./tools/memory-search.js";import { createCronTool } from "./tools/cron/index.js";import { createDataTool } from "./tools/data/index.js";+import { createSendFileTool } from "./tools/send-file.js";+import type { SendFileCallback } from "./tools/send-file.js";import { filterTools } from "./tools/policy.js";import { isMulticaError, isRetryableError } from "@multica/utils";import type { ExecApprovalCallback } from "./tools/exec-approval-types.js";@@ -31,6 +33,8 @@ export interface CreateToolsOptions {provider?: string | undefined;/** Callback invoked when exec tool needs approval before running a command */onExecApprovalNeeded?: ExecApprovalCallback | undefined;+ /** Callback for sending files through messaging channels */+ onChannelSendFile?: SendFileCallback | undefined;}type ToolErrorPayload = {@@ -132,6 +136,12 @@ export function createAllTools(options: CreateToolsOptions | string): AgentTool<tools.push(memorySearchTool as AgentTool<any>);}+ // Add send_file tool if channel send callback is provided+ if (opts.onChannelSendFile) {+ const sendFileTool = createSendFileTool(opts.onChannelSendFile);+ tools.push(sendFileTool as AgentTool<any>);+ }+// Add sessions_spawn tool (will be filtered by policy for subagents)const sessionsSpawnTool = createSessionsSpawnTool({isSubagent: isSubagent ?? false,@@ -173,6 +183,7 @@ export function resolveTools(options: ResolveToolsOptions): AgentTool<any>[] {sessionId: options.sessionId,provider: options.provider,onExecApprovalNeeded: options.onExecApprovalNeeded,+ onChannelSendFile: options.onChannelSendFile,});// Apply policy filteringdiff --git a/packages/core/src/agent/tools/send-file.ts b/packages/core/src/agent/tools/send-file.tsnew file mode 100644index 0000000000..3a9afdfcd9--- /dev/null+++ b/packages/core/src/agent/tools/send-file.ts@@ -0,0 +1,120 @@+/**+ * send_file tool — sends a file to the active messaging channel.+ *+ * Available when the agent is connected to a channel (Telegram, etc.).+ * Auto-detects media type from file extension if not specified.+ */++import { Type } from "@sinclair/typebox";+import type { AgentTool } from "@mariozechner/pi-agent-core";+import { stat } from "node:fs/promises";+import { basename, extname } from "node:path";++const SendFileSchema = Type.Object({+ file_path: Type.String({+ description: "Absolute path to the file to send.",+ }),+ caption: Type.Optional(+ Type.String({+ description: "Optional caption text to accompany the file.",+ }),+ ),+ type: Type.Optional(+ Type.Union(+ [+ Type.Literal("auto"),+ Type.Literal("photo"),+ Type.Literal("document"),+ Type.Literal("video"),+ Type.Literal("audio"),+ Type.Literal("voice"),+ ],+ {+ description:+ 'Media type. "auto" (default) detects from file extension. Use "document" to force file attachment.',+ },+ ),+ ),+});++type SendFileArgs = {+ file_path: string;+ caption?: string;+ type?: "auto" | "photo" | "document" | "video" | "audio" | "voice";+};++type SendFileResult = {+ sent: boolean;+ file_path: string;+ detected_type: string;+ error?: string;+};++/** Callback provided by the Hub to route files through channels or gateway. */+export type SendFileCallback = (+ filePath: string,+ caption: string | undefined,+ type: string,+) => Promise<boolean>;++const PHOTO_EXTENSIONS = new Set([".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"]);+const VIDEO_EXTENSIONS = new Set([".mp4", ".webm", ".mov", ".avi", ".mkv"]);+const AUDIO_EXTENSIONS = new Set([".mp3", ".ogg", ".wav", ".m4a", ".flac", ".aac"]);++/** Detect outbound media type from file extension. */+function detectMediaType(filePath: string): string {+ const ext = extname(filePath).toLowerCase();+ if (PHOTO_EXTENSIONS.has(ext)) return "photo";+ if (VIDEO_EXTENSIONS.has(ext)) return "video";+ if (AUDIO_EXTENSIONS.has(ext)) return "audio";+ return "document";+}++export function createSendFileTool(+ onSendFile: SendFileCallback,+): AgentTool<typeof SendFileSchema, SendFileResult> {+ return {+ name: "send_file",+ label: "Send File",+ description:+ "Send a file (photo, document, video, audio) to the active messaging channel (e.g. Telegram). " ++ "The file must exist on the local filesystem. " ++ 'Type is auto-detected from extension unless overridden. Use type="document" to force file attachment.',+ parameters: SendFileSchema,+ execute: async (_toolCallId, args) => {+ const { file_path, caption, type } = args as SendFileArgs;++ // Validate file exists+ try {+ const fileStat = await stat(file_path);+ if (!fileStat.isFile()) {+ return {+ content: [{ type: "text", text: `Error: ${file_path} is not a file` }],+ details: { sent: false, file_path, detected_type: "unknown", error: "Not a file" },+ };+ }+ } catch {+ return {+ content: [{ type: "text", text: `Error: File not found: ${file_path}` }],+ details: { sent: false, file_path, detected_type: "unknown", error: "File not found" },+ };+ }++ const mediaType = type && type !== "auto" ? type : detectMediaType(file_path);++ const sent = await onSendFile(file_path, caption, mediaType);+ if (!sent) {+ return {+ content: [{ type: "text", text: "No active channel conversation to send the file to." }],+ details: { sent: false, file_path, detected_type: mediaType, error: "No active channel" },+ };+ }++ const filename = basename(file_path);+ return {+ content: [{ type: "text", text: `File sent: ${filename} (${mediaType})` }],+ details: { sent: true, file_path, detected_type: mediaType },+ };+ },+ };+}diff --git a/packages/core/src/agent/types.ts b/packages/core/src/agent/types.tsindex 14d7a67636..cc719f4d7d 100644--- a/packages/core/src/agent/types.ts+++ b/packages/core/src/agent/types.ts@@ -2,6 +2,8 @@ import type { ThinkingLevel } from "@mariozechner/pi-agent-core";import type { SkillsConfig } from "./skills/types.js";import type { ToolsConfig } from "./tools/policy.js";import type { ExecApprovalCallback, ExecApprovalConfig } from "./tools/exec-approval-types.js";+import type { SendFileCallback } from "./tools/send-file.js";+import type { ChannelInfo } from "./system-prompt/types.js";/** Controls how reasoning/thinking content blocks are handled */export type ReasoningMode = "off" | "on" | "stream";@@ -82,6 +84,12 @@ export type AgentOptions = {onExecApprovalNeeded?: ExecApprovalCallback | undefined;/** Exec approval configuration (security level, ask mode, allowlist) */execApproval?: ExecApprovalConfig | undefined;++ // === Channel Configuration ===+ /** Connected messaging channels (for system prompt awareness) */+ channels?: ChannelInfo[] | undefined;+ /** Callback for sending files through messaging channels (Telegram, etc.) */+ onChannelSendFile?: SendFileCallback | undefined;};export interface Message {