forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia-understanding.auto.e2e.test.ts
More file actions
167 lines (136 loc) 路 5.17 KB
/
Copy pathmedia-understanding.auto.e2e.test.ts
File metadata and controls
167 lines (136 loc) 路 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { MsgContext } from "../src/auto-reply/templating.js";
import type { OpenClawConfig } from "../src/config/config.js";
const makeTempDir = async (prefix: string) => await fs.mkdtemp(path.join(os.tmpdir(), prefix));
const writeExecutable = async (dir: string, name: string, content: string) => {
const filePath = path.join(dir, name);
await fs.writeFile(filePath, content, { mode: 0o755 });
return filePath;
};
const makeTempMedia = async (ext: string) => {
const dir = await makeTempDir("openclaw-media-e2e-");
const filePath = path.join(dir, `sample${ext}`);
await fs.writeFile(filePath, "audio");
return { dir, filePath };
};
const loadApply = async () => {
vi.resetModules();
return await import("../src/media-understanding/apply.js");
};
const envSnapshot = () => ({
PATH: process.env.PATH,
SHERPA_ONNX_MODEL_DIR: process.env.SHERPA_ONNX_MODEL_DIR,
WHISPER_CPP_MODEL: process.env.WHISPER_CPP_MODEL,
});
const restoreEnv = (snapshot: ReturnType<typeof envSnapshot>) => {
process.env.PATH = snapshot.PATH;
process.env.SHERPA_ONNX_MODEL_DIR = snapshot.SHERPA_ONNX_MODEL_DIR;
process.env.WHISPER_CPP_MODEL = snapshot.WHISPER_CPP_MODEL;
};
describe("media understanding auto-detect (e2e)", () => {
let tempPaths: string[] = [];
afterEach(async () => {
for (const p of tempPaths) {
await fs.rm(p, { recursive: true, force: true }).catch(() => {});
}
tempPaths = [];
});
it("uses sherpa-onnx-offline when available", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("openclaw-bin-sherpa-");
const modelDir = await makeTempDir("openclaw-sherpa-model-");
tempPaths.push(binDir, modelDir);
await fs.writeFile(path.join(modelDir, "tokens.txt"), "a");
await fs.writeFile(path.join(modelDir, "encoder.onnx"), "a");
await fs.writeFile(path.join(modelDir, "decoder.onnx"), "a");
await fs.writeFile(path.join(modelDir, "joiner.onnx"), "a");
await writeExecutable(
binDir,
"sherpa-onnx-offline",
`#!/usr/bin/env bash\necho "{\\"text\\":\\"sherpa ok\\"}"\n`,
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
process.env.SHERPA_ONNX_MODEL_DIR = modelDir;
const { filePath } = await makeTempMedia(".wav");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:audio>",
MediaPath: filePath,
MediaType: "audio/wav",
};
const cfg: OpenClawConfig = { tools: { media: { audio: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Transcript).toBe("sherpa ok");
} finally {
restoreEnv(snapshot);
}
});
it("uses whisper-cli when sherpa is missing", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("openclaw-bin-whispercpp-");
const modelDir = await makeTempDir("openclaw-whispercpp-model-");
tempPaths.push(binDir, modelDir);
const modelPath = path.join(modelDir, "tiny.bin");
await fs.writeFile(modelPath, "model");
await writeExecutable(
binDir,
"whisper-cli",
"#!/usr/bin/env bash\n" +
'out=""\n' +
'prev=""\n' +
'for arg in "$@"; do\n' +
' if [ "$prev" = "-of" ]; then out="$arg"; break; fi\n' +
' prev="$arg"\n' +
"done\n" +
'if [ -n "$out" ]; then echo \'whisper cpp ok\' > "${out}.txt"; fi\n',
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
process.env.WHISPER_CPP_MODEL = modelPath;
const { filePath } = await makeTempMedia(".wav");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:audio>",
MediaPath: filePath,
MediaType: "audio/wav",
};
const cfg: OpenClawConfig = { tools: { media: { audio: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Transcript).toBe("whisper cpp ok");
} finally {
restoreEnv(snapshot);
}
});
it("uses gemini CLI for images when available", async () => {
const snapshot = envSnapshot();
try {
const binDir = await makeTempDir("openclaw-bin-gemini-");
tempPaths.push(binDir);
await writeExecutable(
binDir,
"gemini",
`#!/usr/bin/env bash\necho '{\\"response\\":\\"gemini ok\\"' + "}'\n`,
);
process.env.PATH = `${binDir}:/usr/bin:/bin`;
const { filePath } = await makeTempMedia(".png");
tempPaths.push(path.dirname(filePath));
const { applyMediaUnderstanding } = await loadApply();
const ctx: MsgContext = {
Body: "<media:image>",
MediaPath: filePath,
MediaType: "image/png",
};
const cfg: OpenClawConfig = { tools: { media: { image: {} } } };
await applyMediaUnderstanding({ ctx, cfg });
expect(ctx.Body).toContain("gemini ok");
} finally {
restoreEnv(snapshot);
}
});
});