mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-20 08:04:44 +00:00
Merge pull request #261 from nicobailon/fix/mom-inline-image-attachments
fix: send Slack image attachments inline to LLM
This commit is contained in:
commit
14945df738
1 changed files with 44 additions and 7 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { Agent, type AgentEvent, ProviderTransport } from "@mariozechner/pi-agent-core";
|
import { Agent, type AgentEvent, type Attachment, ProviderTransport } from "@mariozechner/pi-agent-core";
|
||||||
import { getModel } from "@mariozechner/pi-ai";
|
import { getModel } from "@mariozechner/pi-ai";
|
||||||
import {
|
import {
|
||||||
AgentSession,
|
AgentSession,
|
||||||
|
|
@ -7,7 +7,7 @@ import {
|
||||||
messageTransformer,
|
messageTransformer,
|
||||||
type Skill,
|
type Skill,
|
||||||
} from "@mariozechner/pi-coding-agent";
|
} from "@mariozechner/pi-coding-agent";
|
||||||
import { existsSync, readFileSync } from "fs";
|
import { existsSync, readFileSync, statSync } from "fs";
|
||||||
import { mkdir, writeFile } from "fs/promises";
|
import { mkdir, writeFile } from "fs/promises";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
import { MomSessionManager, MomSettingsManager } from "./context.js";
|
import { MomSessionManager, MomSettingsManager } from "./context.js";
|
||||||
|
|
@ -64,6 +64,18 @@ function getAnthropicApiKey(): string {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const IMAGE_MIME_TYPES: Record<string, string> = {
|
||||||
|
jpg: "image/jpeg",
|
||||||
|
jpeg: "image/jpeg",
|
||||||
|
png: "image/png",
|
||||||
|
gif: "image/gif",
|
||||||
|
webp: "image/webp",
|
||||||
|
};
|
||||||
|
|
||||||
|
function getImageMimeType(filename: string): string | undefined {
|
||||||
|
return IMAGE_MIME_TYPES[filename.toLowerCase().split(".").pop() || ""];
|
||||||
|
}
|
||||||
|
|
||||||
function getMemory(channelDir: string): string {
|
function getMemory(channelDir: string): string {
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
|
|
||||||
|
|
@ -716,10 +728,34 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
|
||||||
const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;
|
const timestamp = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}${offsetSign}${offsetHours}:${offsetMins}`;
|
||||||
let userMessage = `[${timestamp}] [${ctx.message.userName || "unknown"}]: ${ctx.message.text}`;
|
let userMessage = `[${timestamp}] [${ctx.message.userName || "unknown"}]: ${ctx.message.text}`;
|
||||||
|
|
||||||
// Add attachment paths if any (convert to absolute paths in execution environment)
|
const imageAttachments: Attachment[] = [];
|
||||||
if (ctx.message.attachments && ctx.message.attachments.length > 0) {
|
const nonImagePaths: string[] = [];
|
||||||
const attachmentPaths = ctx.message.attachments.map((a) => `${workspacePath}/${a.local}`).join("\n");
|
|
||||||
userMessage += `\n\n<slack_attachments>\n${attachmentPaths}\n</slack_attachments>`;
|
for (const a of ctx.message.attachments || []) {
|
||||||
|
const fullPath = `${workspacePath}/${a.local}`;
|
||||||
|
const mimeType = getImageMimeType(a.local);
|
||||||
|
|
||||||
|
if (mimeType && existsSync(fullPath)) {
|
||||||
|
try {
|
||||||
|
const stats = statSync(fullPath);
|
||||||
|
imageAttachments.push({
|
||||||
|
id: a.local,
|
||||||
|
type: "image",
|
||||||
|
fileName: a.local.split("/").pop() || a.local,
|
||||||
|
mimeType,
|
||||||
|
size: stats.size,
|
||||||
|
content: readFileSync(fullPath).toString("base64"),
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
nonImagePaths.push(fullPath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nonImagePaths.push(fullPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nonImagePaths.length > 0) {
|
||||||
|
userMessage += `\n\n<slack_attachments>\n${nonImagePaths.join("\n")}\n</slack_attachments>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug: write context to last_prompt.jsonl
|
// Debug: write context to last_prompt.jsonl
|
||||||
|
|
@ -727,10 +763,11 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
|
||||||
systemPrompt,
|
systemPrompt,
|
||||||
messages: session.messages,
|
messages: session.messages,
|
||||||
newUserMessage: userMessage,
|
newUserMessage: userMessage,
|
||||||
|
imageAttachmentCount: imageAttachments.length,
|
||||||
};
|
};
|
||||||
await writeFile(join(channelDir, "last_prompt.jsonl"), JSON.stringify(debugContext, null, 2));
|
await writeFile(join(channelDir, "last_prompt.jsonl"), JSON.stringify(debugContext, null, 2));
|
||||||
|
|
||||||
await session.prompt(userMessage);
|
await session.prompt(userMessage, imageAttachments.length > 0 ? { attachments: imageAttachments } : undefined);
|
||||||
|
|
||||||
// Wait for queued messages
|
// Wait for queued messages
|
||||||
await queueChain;
|
await queueChain;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue