feat: expand inbound media notes
This commit is contained in:
@@ -80,6 +80,7 @@ import {
|
||||
import { SILENT_REPLY_TOKEN } from "./tokens.js";
|
||||
import { isAudio, transcribeInboundAudio } from "./transcription.js";
|
||||
import type { GetReplyOptions, ReplyPayload } from "./types.js";
|
||||
import { buildInboundMediaNote } from "./media-note.js";
|
||||
|
||||
export {
|
||||
extractElevatedDirective,
|
||||
@@ -713,9 +714,7 @@ export async function getReplyFromConfig(
|
||||
.filter(Boolean)
|
||||
.join("\n\n")
|
||||
: [threadStarterNote, prefixedBodyBase].filter(Boolean).join("\n\n");
|
||||
const mediaNote = ctx.MediaPath?.length
|
||||
? `[media attached: ${ctx.MediaPath}${ctx.MediaType ? ` (${ctx.MediaType})` : ""}${ctx.MediaUrl ? ` | ${ctx.MediaUrl}` : ""}]`
|
||||
: undefined;
|
||||
const mediaNote = buildInboundMediaNote(ctx);
|
||||
const mediaReplyHint = mediaNote
|
||||
? "To send an image back, add a line like: MEDIA:https://example.com/image.jpg (no spaces). Keep caption in the text body."
|
||||
: undefined;
|
||||
@@ -857,8 +856,13 @@ async function stageSandboxMedia(params: {
|
||||
workspaceDir: string;
|
||||
}) {
|
||||
const { ctx, sessionCtx, cfg, sessionKey, workspaceDir } = params;
|
||||
const rawPath = ctx.MediaPath?.trim();
|
||||
if (!rawPath || !sessionKey) return;
|
||||
const hasPathsArray = Array.isArray(ctx.MediaPaths) && ctx.MediaPaths.length > 0;
|
||||
const rawPaths = hasPathsArray
|
||||
? ctx.MediaPaths
|
||||
: ctx.MediaPath?.trim()
|
||||
? [ctx.MediaPath.trim()]
|
||||
: [];
|
||||
if (rawPaths.length === 0 || !sessionKey) return;
|
||||
|
||||
const sandbox = await ensureSandboxWorkspaceForSession({
|
||||
config: cfg,
|
||||
@@ -867,45 +871,84 @@ async function stageSandboxMedia(params: {
|
||||
});
|
||||
if (!sandbox) return;
|
||||
|
||||
let source = rawPath;
|
||||
if (source.startsWith("file://")) {
|
||||
try {
|
||||
source = fileURLToPath(source);
|
||||
} catch {
|
||||
return;
|
||||
const resolveAbsolutePath = (value: string): string | null => {
|
||||
let resolved = value.trim();
|
||||
if (!resolved) return null;
|
||||
if (resolved.startsWith("file://")) {
|
||||
try {
|
||||
resolved = fileURLToPath(resolved);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!path.isAbsolute(source)) return;
|
||||
|
||||
const originalMediaPath = ctx.MediaPath;
|
||||
const originalMediaUrl = ctx.MediaUrl;
|
||||
if (!path.isAbsolute(resolved)) return null;
|
||||
return resolved;
|
||||
};
|
||||
|
||||
try {
|
||||
const fileName = path.basename(source);
|
||||
if (!fileName) return;
|
||||
const destDir = path.join(sandbox.workspaceDir, "media", "inbound");
|
||||
await fs.mkdir(destDir, { recursive: true });
|
||||
const dest = path.join(destDir, fileName);
|
||||
await fs.copyFile(source, dest);
|
||||
|
||||
const relative = path.posix.join("media", "inbound", fileName);
|
||||
ctx.MediaPath = relative;
|
||||
sessionCtx.MediaPath = relative;
|
||||
const usedNames = new Set<string>();
|
||||
const staged = new Map<string, string>(); // absolute source -> relative sandbox path
|
||||
|
||||
if (originalMediaUrl) {
|
||||
let normalizedUrl = originalMediaUrl;
|
||||
if (normalizedUrl.startsWith("file://")) {
|
||||
try {
|
||||
normalizedUrl = fileURLToPath(normalizedUrl);
|
||||
} catch {
|
||||
normalizedUrl = originalMediaUrl;
|
||||
}
|
||||
for (const raw of rawPaths) {
|
||||
const source = resolveAbsolutePath(raw);
|
||||
if (!source) continue;
|
||||
if (staged.has(source)) continue;
|
||||
|
||||
const baseName = path.basename(source);
|
||||
if (!baseName) continue;
|
||||
const parsed = path.parse(baseName);
|
||||
let fileName = baseName;
|
||||
let suffix = 1;
|
||||
while (usedNames.has(fileName)) {
|
||||
fileName = `${parsed.name}-${suffix}${parsed.ext}`;
|
||||
suffix += 1;
|
||||
}
|
||||
if (normalizedUrl === originalMediaPath || normalizedUrl === source) {
|
||||
ctx.MediaUrl = relative;
|
||||
sessionCtx.MediaUrl = relative;
|
||||
usedNames.add(fileName);
|
||||
|
||||
const dest = path.join(destDir, fileName);
|
||||
await fs.copyFile(source, dest);
|
||||
const relative = path.posix.join("media", "inbound", fileName);
|
||||
staged.set(source, relative);
|
||||
}
|
||||
|
||||
const rewriteIfStaged = (value: string | undefined): string | undefined => {
|
||||
const raw = value?.trim();
|
||||
if (!raw) return value;
|
||||
const abs = resolveAbsolutePath(raw);
|
||||
if (!abs) return value;
|
||||
const mapped = staged.get(abs);
|
||||
return mapped ?? value;
|
||||
};
|
||||
|
||||
const nextMediaPaths = hasPathsArray
|
||||
? rawPaths.map((p) => rewriteIfStaged(p) ?? p)
|
||||
: undefined;
|
||||
if (nextMediaPaths) {
|
||||
ctx.MediaPaths = nextMediaPaths;
|
||||
sessionCtx.MediaPaths = nextMediaPaths;
|
||||
ctx.MediaPath = nextMediaPaths[0];
|
||||
sessionCtx.MediaPath = nextMediaPaths[0];
|
||||
} else {
|
||||
const rewritten = rewriteIfStaged(ctx.MediaPath);
|
||||
if (rewritten && rewritten !== ctx.MediaPath) {
|
||||
ctx.MediaPath = rewritten;
|
||||
sessionCtx.MediaPath = rewritten;
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(ctx.MediaUrls) && ctx.MediaUrls.length > 0) {
|
||||
const nextUrls = ctx.MediaUrls.map((u) => rewriteIfStaged(u) ?? u);
|
||||
ctx.MediaUrls = nextUrls;
|
||||
sessionCtx.MediaUrls = nextUrls;
|
||||
}
|
||||
const rewrittenUrl = rewriteIfStaged(ctx.MediaUrl);
|
||||
if (rewrittenUrl && rewrittenUrl !== ctx.MediaUrl) {
|
||||
ctx.MediaUrl = rewrittenUrl;
|
||||
sessionCtx.MediaUrl = rewrittenUrl;
|
||||
}
|
||||
} catch (err) {
|
||||
logVerbose(`Failed to stage inbound media for sandbox: ${String(err)}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user