mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 10:21:24 +00:00
refactor: de-duplicate channel runtime and payload helpers
This commit is contained in:
@@ -1,23 +1,30 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { MsgContext } from "../auto-reply/templating.js";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import { createMediaAttachmentCache, normalizeMediaAttachments } from "./runner.js";
|
||||
|
||||
type AudioFixtureParams = {
|
||||
ctx: MsgContext;
|
||||
type MediaFixtureParams = {
|
||||
ctx: { MediaPath: string; MediaType: string };
|
||||
media: ReturnType<typeof normalizeMediaAttachments>;
|
||||
cache: ReturnType<typeof createMediaAttachmentCache>;
|
||||
};
|
||||
|
||||
export async function withAudioFixture(
|
||||
filePrefix: string,
|
||||
run: (params: AudioFixtureParams) => Promise<void>,
|
||||
export async function withMediaFixture(
|
||||
params: {
|
||||
filePrefix: string;
|
||||
extension: string;
|
||||
mediaType: string;
|
||||
fileContents: Buffer;
|
||||
},
|
||||
run: (params: MediaFixtureParams) => Promise<void>,
|
||||
) {
|
||||
const tmpPath = path.join(os.tmpdir(), filePrefix + "-" + Date.now().toString() + ".wav");
|
||||
await fs.writeFile(tmpPath, Buffer.from("RIFF"));
|
||||
const ctx: MsgContext = { MediaPath: tmpPath, MediaType: "audio/wav" };
|
||||
const tmpPath = path.join(
|
||||
os.tmpdir(),
|
||||
`${params.filePrefix}-${Date.now().toString()}.${params.extension}`,
|
||||
);
|
||||
await fs.writeFile(tmpPath, params.fileContents);
|
||||
const ctx = { MediaPath: tmpPath, MediaType: params.mediaType };
|
||||
const media = normalizeMediaAttachments(ctx);
|
||||
const cache = createMediaAttachmentCache(media, {
|
||||
localPathRoots: [path.dirname(tmpPath)],
|
||||
@@ -32,3 +39,18 @@ export async function withAudioFixture(
|
||||
await fs.unlink(tmpPath).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
export async function withAudioFixture(
|
||||
filePrefix: string,
|
||||
run: (params: MediaFixtureParams) => Promise<void>,
|
||||
) {
|
||||
await withMediaFixture(
|
||||
{
|
||||
filePrefix,
|
||||
extension: "wav",
|
||||
mediaType: "audio/wav",
|
||||
fileContents: Buffer.from("RIFF"),
|
||||
},
|
||||
run,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user