@use-lattice/litmus 0.121.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/dist/src/accounts-Bt1oJb1Z.cjs +219 -0
- package/dist/src/accounts-DjOU8Rm3.js +178 -0
- package/dist/src/agentic-utils-D03IiXQc.js +153 -0
- package/dist/src/agentic-utils-Dh7xaMQM.cjs +180 -0
- package/dist/src/agents-C6BIMlZa.js +231 -0
- package/dist/src/agents-DvIpNX1L.cjs +666 -0
- package/dist/src/agents-ZP0RP9vV.cjs +231 -0
- package/dist/src/agents-maJXdjbR.js +665 -0
- package/dist/src/aimlapi-BTbQjG2E.cjs +30 -0
- package/dist/src/aimlapi-CwMxqfXP.js +30 -0
- package/dist/src/audio-BBUdvsde.cjs +97 -0
- package/dist/src/audio-D5DPZ7I-.js +97 -0
- package/dist/src/base-BEysXrkq.cjs +222 -0
- package/dist/src/base-C451JQfq.js +193 -0
- package/dist/src/blobs-BY8MDmpo.js +230 -0
- package/dist/src/blobs-BgcNn97m.cjs +256 -0
- package/dist/src/cache-BBE_lsTA.cjs +4 -0
- package/dist/src/cache-BkrqU5Ba.js +237 -0
- package/dist/src/cache-DsCxFlsZ.cjs +297 -0
- package/dist/src/chat-CPJWDP6a.cjs +289 -0
- package/dist/src/chat-CXX3xzkk.cjs +811 -0
- package/dist/src/chat-CcDgZFJ4.js +787 -0
- package/dist/src/chat-Dz5ZeGO2.js +289 -0
- package/dist/src/chatkit-Dw0mKkML.cjs +1158 -0
- package/dist/src/chatkit-swAIVuea.js +1157 -0
- package/dist/src/chunk-DEq-mXcV.js +15 -0
- package/dist/src/claude-agent-sdk-BXZJtOg6.js +379 -0
- package/dist/src/claude-agent-sdk-CkfyjDoG.cjs +383 -0
- package/dist/src/cloudflare-ai-BzpJcqUH.js +161 -0
- package/dist/src/cloudflare-ai-Cmy_R1y2.cjs +161 -0
- package/dist/src/cloudflare-gateway-B9tVQKok.cjs +272 -0
- package/dist/src/cloudflare-gateway-DrD3ew3H.js +272 -0
- package/dist/src/codex-sdk-Dezj9Nwm.js +1056 -0
- package/dist/src/codex-sdk-Dl9D4k5B.cjs +1060 -0
- package/dist/src/cometapi-C-9YvCHC.js +54 -0
- package/dist/src/cometapi-DHgDKoO2.cjs +54 -0
- package/dist/src/completion-B8Ctyxpr.js +120 -0
- package/dist/src/completion-Cxrt08sj.cjs +131 -0
- package/dist/src/createHash-BwgE13yv.cjs +27 -0
- package/dist/src/createHash-DmPQkvBh.js +15 -0
- package/dist/src/docker-BiqcTwLv.js +80 -0
- package/dist/src/docker-C7tEJnP-.cjs +80 -0
- package/dist/src/esm-C62Zofr1.cjs +409 -0
- package/dist/src/esm-DMVc93eh.js +379 -0
- package/dist/src/evalResult-C3NJPQOo.cjs +301 -0
- package/dist/src/evalResult-C7JJAPBb.js +295 -0
- package/dist/src/evalResult-DoVTZZWI.cjs +2 -0
- package/dist/src/extractor-DnMD3fwt.cjs +391 -0
- package/dist/src/extractor-DtlL28vL.js +374 -0
- package/dist/src/fetch-BTxakTSg.cjs +1133 -0
- package/dist/src/fetch-DQckpUFz.js +928 -0
- package/dist/src/fileExtensions-DnqA1y9x.js +85 -0
- package/dist/src/fileExtensions-bYh77CN8.cjs +114 -0
- package/dist/src/genaiTracer-CyZrmaK0.cjs +268 -0
- package/dist/src/genaiTracer-D3fD9dNV.js +256 -0
- package/dist/src/graders-BNscxFrU.js +13644 -0
- package/dist/src/graders-D2oE9Msq.js +2 -0
- package/dist/src/graders-c0Ez_w-9.cjs +2 -0
- package/dist/src/graders-d0F2M3e9.cjs +14056 -0
- package/dist/src/image-0ZhE0VlR.cjs +280 -0
- package/dist/src/image-CWE1pdNv.js +257 -0
- package/dist/src/image-D9ZK6hwL.js +163 -0
- package/dist/src/image-DKZgZITg.cjs +163 -0
- package/dist/src/index.cjs +11366 -0
- package/dist/src/index.d.cts +19640 -0
- package/dist/src/index.d.ts +19641 -0
- package/dist/src/index.js +11306 -0
- package/dist/src/invariant-Ddh24eXh.js +25 -0
- package/dist/src/invariant-kfQ8Bu82.cjs +30 -0
- package/dist/src/knowledgeBase-BgPyGFUd.cjs +122 -0
- package/dist/src/knowledgeBase-DyHilYaP.js +122 -0
- package/dist/src/litellm-CyMeneHS.js +135 -0
- package/dist/src/litellm-DWDF73yF.cjs +135 -0
- package/dist/src/logger-C40ZGil9.js +717 -0
- package/dist/src/logger-DyfK9PBt.cjs +917 -0
- package/dist/src/luma-ray-BAU9X_ep.cjs +315 -0
- package/dist/src/luma-ray-nwVseBbv.js +313 -0
- package/dist/src/messages-B5ADWTTv.js +245 -0
- package/dist/src/messages-BCnZfqrS.cjs +257 -0
- package/dist/src/meteor-DLZZ3osF.cjs +134 -0
- package/dist/src/meteor-DUiCJRC-.js +134 -0
- package/dist/src/modelslab-00cveB8L.cjs +163 -0
- package/dist/src/modelslab-D9sCU_L7.js +163 -0
- package/dist/src/nova-reel-CTapvqYH.js +276 -0
- package/dist/src/nova-reel-DlWuuroF.cjs +278 -0
- package/dist/src/nova-sonic-5UPWfeMv.cjs +363 -0
- package/dist/src/nova-sonic-BhSwQNym.js +363 -0
- package/dist/src/openai-BWrJK9d8.cjs +52 -0
- package/dist/src/openai-DumO8WQn.js +47 -0
- package/dist/src/openclaw-B8brrjC_.cjs +577 -0
- package/dist/src/openclaw-Bkayww9q.js +571 -0
- package/dist/src/opencode-sdk-7xjoDNiM.cjs +562 -0
- package/dist/src/opencode-sdk-SGwAPxht.js +558 -0
- package/dist/src/otlpReceiver-CoAHfAN9.cjs +15 -0
- package/dist/src/otlpReceiver-oO3EQwI9.js +14 -0
- package/dist/src/providerRegistry-4yjhaEM8.js +45 -0
- package/dist/src/providerRegistry-DhV4rJIc.cjs +50 -0
- package/dist/src/providers-B5RJVG-7.cjs +33609 -0
- package/dist/src/providers-BdmZCLzV.js +33262 -0
- package/dist/src/providers-CxtRxn8e.js +2 -0
- package/dist/src/providers-DnQLNbx1.cjs +3 -0
- package/dist/src/pythonUtils-BD0druiM.cjs +275 -0
- package/dist/src/pythonUtils-IBhn5YGR.js +249 -0
- package/dist/src/quiverai-BDOwZBsM.cjs +213 -0
- package/dist/src/quiverai-D3JTF5lD.js +213 -0
- package/dist/src/responses-B2LCDCXZ.js +667 -0
- package/dist/src/responses-BvNm4Xv9.cjs +685 -0
- package/dist/src/rubyUtils-B0NwnfpY.cjs +245 -0
- package/dist/src/rubyUtils-BroxzZ7c.cjs +2 -0
- package/dist/src/rubyUtils-hqVw5UvJ.js +222 -0
- package/dist/src/sagemaker-Cno2V-Sx.js +689 -0
- package/dist/src/sagemaker-fV_KUgs5.cjs +691 -0
- package/dist/src/server-BOuAXb06.cjs +238 -0
- package/dist/src/server-CtI-EWzm.cjs +2 -0
- package/dist/src/server-Cy3DZymt.js +189 -0
- package/dist/src/slack-CP8xBePa.js +135 -0
- package/dist/src/slack-DSQ1yXVb.cjs +135 -0
- package/dist/src/store-BwDDaBjb.cjs +246 -0
- package/dist/src/store-DcbLC593.cjs +2 -0
- package/dist/src/store-IGpqMIkv.js +240 -0
- package/dist/src/tables-3Q2cL7So.cjs +373 -0
- package/dist/src/tables-Bi2fjr4W.js +288 -0
- package/dist/src/telemetry-Bg2WqF79.js +161 -0
- package/dist/src/telemetry-D0x6u5kX.cjs +166 -0
- package/dist/src/telemetry-DXNimrI0.cjs +2 -0
- package/dist/src/text-B_UCRPp2.js +22 -0
- package/dist/src/text-CW1cyrwj.cjs +33 -0
- package/dist/src/tokenUsageUtils-NYT-WKS6.js +138 -0
- package/dist/src/tokenUsageUtils-bVa1ga6f.cjs +173 -0
- package/dist/src/transcription-Cl_W16Pr.js +122 -0
- package/dist/src/transcription-yt1EecY8.cjs +124 -0
- package/dist/src/transform-BCtGrl_W.cjs +228 -0
- package/dist/src/transform-Bv6gG2MJ.cjs +1688 -0
- package/dist/src/transform-CY1wbpRy.js +1507 -0
- package/dist/src/transform-DU8rUL9P.cjs +2 -0
- package/dist/src/transform-yWaShiKr.js +216 -0
- package/dist/src/transformersAvailability-BGkzavwb.js +35 -0
- package/dist/src/transformersAvailability-DKoRtQLy.cjs +35 -0
- package/dist/src/types-5aqHpBwE.cjs +3769 -0
- package/dist/src/types-Bn6D9c4U.js +3300 -0
- package/dist/src/util-BkKlTkI2.js +293 -0
- package/dist/src/util-CTh0bfOm.cjs +1119 -0
- package/dist/src/util-D17oBwo7.cjs +328 -0
- package/dist/src/util-DsS_-v4p.js +613 -0
- package/dist/src/util-DuntT1Ga.js +951 -0
- package/dist/src/util-aWjdCYMI.cjs +667 -0
- package/dist/src/utils-CisQwpjA.js +94 -0
- package/dist/src/utils-yWamDvmz.cjs +123 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/drizzle/0000_lush_hellion.sql +36 -0
- package/drizzle/0001_wide_calypso.sql +3 -0
- package/drizzle/0002_tidy_juggernaut.sql +1 -0
- package/drizzle/0003_lively_naoko.sql +8 -0
- package/drizzle/0004_minor_peter_quill.sql +19 -0
- package/drizzle/0005_silky_millenium_guard.sql +2 -0
- package/drizzle/0006_harsh_caretaker.sql +42 -0
- package/drizzle/0007_cloudy_wong.sql +1 -0
- package/drizzle/0008_broad_boomer.sql +2 -0
- package/drizzle/0009_strong_marten_broadcloak.sql +19 -0
- package/drizzle/0010_needy_bishop.sql +11 -0
- package/drizzle/0011_moaning_millenium_guard.sql +1 -0
- package/drizzle/0012_late_marten_broadcloak.sql +2 -0
- package/drizzle/0013_previous_dormammu.sql +9 -0
- package/drizzle/0014_lazy_captain_universe.sql +2 -0
- package/drizzle/0015_zippy_wallop.sql +29 -0
- package/drizzle/0016_jazzy_zemo.sql +2 -0
- package/drizzle/0017_reflective_praxagora.sql +4 -0
- package/drizzle/0018_fat_vanisher.sql +22 -0
- package/drizzle/0019_new_clint_barton.sql +8 -0
- package/drizzle/0020_skinny_maverick.sql +1 -0
- package/drizzle/0021_mysterious_madelyne_pryor.sql +13 -0
- package/drizzle/0022_sleepy_ultimo.sql +25 -0
- package/drizzle/0023_wooden_mandrill.sql +2 -0
- package/drizzle/AGENTS.md +68 -0
- package/drizzle/CLAUDE.md +1 -0
- package/drizzle/meta/0000_snapshot.json +221 -0
- package/drizzle/meta/0001_snapshot.json +214 -0
- package/drizzle/meta/0002_snapshot.json +221 -0
- package/drizzle/meta/0005_snapshot.json +369 -0
- package/drizzle/meta/0006_snapshot.json +638 -0
- package/drizzle/meta/0007_snapshot.json +640 -0
- package/drizzle/meta/0008_snapshot.json +649 -0
- package/drizzle/meta/0009_snapshot.json +554 -0
- package/drizzle/meta/0010_snapshot.json +619 -0
- package/drizzle/meta/0011_snapshot.json +627 -0
- package/drizzle/meta/0012_snapshot.json +639 -0
- package/drizzle/meta/0013_snapshot.json +717 -0
- package/drizzle/meta/0014_snapshot.json +717 -0
- package/drizzle/meta/0015_snapshot.json +897 -0
- package/drizzle/meta/0016_snapshot.json +1031 -0
- package/drizzle/meta/0018_snapshot.json +1210 -0
- package/drizzle/meta/0019_snapshot.json +1165 -0
- package/drizzle/meta/0020_snapshot.json +1232 -0
- package/drizzle/meta/0021_snapshot.json +1311 -0
- package/drizzle/meta/0022_snapshot.json +1481 -0
- package/drizzle/meta/0023_snapshot.json +1496 -0
- package/drizzle/meta/_journal.json +174 -0
- package/package.json +240 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
const require_logger = require("./logger-DyfK9PBt.cjs");
|
|
2
|
+
const require_esm = require("./esm-C62Zofr1.cjs");
|
|
3
|
+
let fs = require("fs");
|
|
4
|
+
fs = require_logger.__toESM(fs);
|
|
5
|
+
let path = require("path");
|
|
6
|
+
path = require_logger.__toESM(path);
|
|
7
|
+
let os = require("os");
|
|
8
|
+
os = require_logger.__toESM(os);
|
|
9
|
+
let child_process = require("child_process");
|
|
10
|
+
let util = require("util");
|
|
11
|
+
let python_shell = require("python-shell");
|
|
12
|
+
//#region src/python/pythonUtils.ts
|
|
13
|
+
const execFileAsync = (0, util.promisify)(child_process.execFile);
|
|
14
|
+
/**
|
|
15
|
+
* Gets an integer value from an environment variable.
|
|
16
|
+
* @param key - The environment variable name
|
|
17
|
+
* @returns The parsed integer value, or undefined if not set or not a valid integer
|
|
18
|
+
*/
|
|
19
|
+
function getEnvInt(key) {
|
|
20
|
+
const value = process.env[key];
|
|
21
|
+
if (value === void 0) return;
|
|
22
|
+
const parsed = parseInt(value, 10);
|
|
23
|
+
return isNaN(parsed) ? void 0 : parsed;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Resolves the Python executable path from explicit config and environment.
|
|
27
|
+
* This centralizes the fallback logic: configPath > PROMPTFOO_PYTHON env var.
|
|
28
|
+
*
|
|
29
|
+
* Note: Does NOT apply the final 'python' default - that's handled by
|
|
30
|
+
* validatePythonPath. This preserves the distinction between "explicitly
|
|
31
|
+
* configured" (should fail if invalid) and "using system default" (should
|
|
32
|
+
* try fallback detection).
|
|
33
|
+
*
|
|
34
|
+
* @param configPath - Explicitly configured Python path from provider config
|
|
35
|
+
* @returns The configured path, or undefined if neither config nor env var is set
|
|
36
|
+
*/
|
|
37
|
+
function getConfiguredPythonPath(configPath) {
|
|
38
|
+
if (configPath) return configPath;
|
|
39
|
+
return require_logger.getEnvString("PROMPTFOO_PYTHON") || void 0;
|
|
40
|
+
}
|
|
41
|
+
const state = {
|
|
42
|
+
cachedPythonPath: null,
|
|
43
|
+
validationPromise: null
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Try to find Python using Windows 'where' command, filtering out Microsoft Store stubs.
|
|
47
|
+
*/
|
|
48
|
+
async function tryWindowsWhere() {
|
|
49
|
+
try {
|
|
50
|
+
const output = (await execFileAsync("where", ["python"])).stdout.trim();
|
|
51
|
+
if (!output) {
|
|
52
|
+
require_logger.logger.debug("Windows 'where python' returned empty output");
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const paths = output.split("\n").filter((path$2) => path$2.trim());
|
|
56
|
+
for (const pythonPath of paths) {
|
|
57
|
+
const trimmedPath = pythonPath.trim();
|
|
58
|
+
if (trimmedPath.includes("WindowsApps") || !trimmedPath.endsWith(".exe")) continue;
|
|
59
|
+
const validated = await tryPath(trimmedPath);
|
|
60
|
+
if (validated) return validated;
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
64
|
+
require_logger.logger.debug(`Windows 'where python' failed: ${errorMsg}`);
|
|
65
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES")) require_logger.logger.warn(`Permission denied when searching for Python: ${errorMsg}`);
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Try Python commands to get sys.executable path.
|
|
71
|
+
*/
|
|
72
|
+
async function tryPythonCommands(commands) {
|
|
73
|
+
for (const cmd of commands) try {
|
|
74
|
+
const executablePath = (await execFileAsync(cmd, ["-c", "import sys; print(sys.executable)"])).stdout.trim();
|
|
75
|
+
if (executablePath && executablePath !== "None") {
|
|
76
|
+
if (process.platform === "win32" && !executablePath.toLowerCase().endsWith(".exe")) {
|
|
77
|
+
if (executablePath.includes("\\") || /^[A-Za-z]:/.test(executablePath)) return executablePath + ".exe";
|
|
78
|
+
}
|
|
79
|
+
return executablePath;
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
83
|
+
require_logger.logger.debug(`Python command "${cmd}" failed: ${errorMsg}`);
|
|
84
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) require_logger.logger.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Try direct command validation as final fallback.
|
|
90
|
+
*/
|
|
91
|
+
async function tryDirectCommands(commands) {
|
|
92
|
+
for (const cmd of commands) try {
|
|
93
|
+
const validated = await tryPath(cmd);
|
|
94
|
+
if (validated) return validated;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
97
|
+
require_logger.logger.debug(`Direct command "${cmd}" failed: ${errorMsg}`);
|
|
98
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) require_logger.logger.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Attempts to get the Python executable path using platform-appropriate strategies.
|
|
104
|
+
* @returns The Python executable path if successful, or null if failed.
|
|
105
|
+
*/
|
|
106
|
+
async function getSysExecutable() {
|
|
107
|
+
if (process.platform === "win32") {
|
|
108
|
+
const whereResult = await tryWindowsWhere();
|
|
109
|
+
if (whereResult) return whereResult;
|
|
110
|
+
const sysResult = await tryPythonCommands(["py", "py -3"]);
|
|
111
|
+
if (sysResult) return sysResult;
|
|
112
|
+
return await tryDirectCommands(["python"]);
|
|
113
|
+
} else return await tryPythonCommands(["python3", "python"]);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Attempts to validate a Python executable path.
|
|
117
|
+
* @param path - The path to the Python executable to test.
|
|
118
|
+
* @returns The validated path if successful, or null if invalid.
|
|
119
|
+
*/
|
|
120
|
+
async function tryPath(path$3) {
|
|
121
|
+
let timeoutId;
|
|
122
|
+
try {
|
|
123
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
124
|
+
timeoutId = setTimeout(() => reject(/* @__PURE__ */ new Error("Command timed out")), 2500);
|
|
125
|
+
});
|
|
126
|
+
const result = await Promise.race([execFileAsync(path$3, ["--version"]), timeoutPromise]);
|
|
127
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
128
|
+
if (result.stdout.trim().startsWith("Python")) return path$3;
|
|
129
|
+
return null;
|
|
130
|
+
} catch {
|
|
131
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Validates and caches the Python executable path.
|
|
137
|
+
*
|
|
138
|
+
* @param pythonPath - Path to the Python executable.
|
|
139
|
+
* @param isExplicit - If true, only tries the provided path.
|
|
140
|
+
* @returns Validated Python executable path.
|
|
141
|
+
* @throws {Error} If no valid Python executable is found.
|
|
142
|
+
*/
|
|
143
|
+
async function validatePythonPath(pythonPath, isExplicit) {
|
|
144
|
+
if (state.cachedPythonPath) return state.cachedPythonPath;
|
|
145
|
+
if (!state.validationPromise) state.validationPromise = (async () => {
|
|
146
|
+
try {
|
|
147
|
+
const primaryPath = await tryPath(pythonPath);
|
|
148
|
+
if (primaryPath) {
|
|
149
|
+
state.cachedPythonPath = primaryPath;
|
|
150
|
+
state.validationPromise = null;
|
|
151
|
+
return primaryPath;
|
|
152
|
+
}
|
|
153
|
+
if (isExplicit) {
|
|
154
|
+
const error = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}" Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
|
|
155
|
+
state.validationPromise = null;
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
const detectedPath = await getSysExecutable();
|
|
159
|
+
if (detectedPath) {
|
|
160
|
+
state.cachedPythonPath = detectedPath;
|
|
161
|
+
state.validationPromise = null;
|
|
162
|
+
return detectedPath;
|
|
163
|
+
}
|
|
164
|
+
const error = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}", sys.executable detection, and fallback commands. Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
|
|
165
|
+
state.validationPromise = null;
|
|
166
|
+
throw error;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
state.validationPromise = null;
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
})();
|
|
172
|
+
return state.validationPromise;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Runs a Python script with the specified method and arguments.
|
|
176
|
+
*
|
|
177
|
+
* @param scriptPath - The path to the Python script to run.
|
|
178
|
+
* @param method - The name of the method to call in the Python script.
|
|
179
|
+
* @param args - An array of arguments to pass to the Python script.
|
|
180
|
+
* @param options - Optional settings for running the Python script.
|
|
181
|
+
* @param options.pythonExecutable - Optional path to the Python executable.
|
|
182
|
+
* @returns A promise that resolves to the output of the Python script.
|
|
183
|
+
* @throws An error if there's an issue running the Python script or parsing its output.
|
|
184
|
+
*/
|
|
185
|
+
async function runPython(scriptPath, method, args, options = {}) {
|
|
186
|
+
const absPath = path.default.resolve(scriptPath);
|
|
187
|
+
const tempJsonPath = path.default.join(os.default.tmpdir(), `promptfoo-python-input-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
|
|
188
|
+
const outputPath = path.default.join(os.default.tmpdir(), `promptfoo-python-output-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
|
|
189
|
+
const customPath = getConfiguredPythonPath(options.pythonExecutable);
|
|
190
|
+
let pythonPath = customPath || "python";
|
|
191
|
+
pythonPath = await validatePythonPath(pythonPath, typeof customPath === "string");
|
|
192
|
+
const pythonOptions = {
|
|
193
|
+
args: [
|
|
194
|
+
absPath,
|
|
195
|
+
method,
|
|
196
|
+
tempJsonPath,
|
|
197
|
+
outputPath
|
|
198
|
+
],
|
|
199
|
+
env: process.env,
|
|
200
|
+
mode: "binary",
|
|
201
|
+
pythonPath,
|
|
202
|
+
scriptPath: require_esm.getWrapperDir("python"),
|
|
203
|
+
...require_logger.getEnvBool("PROMPTFOO_PYTHON_DEBUG_ENABLED") && { stdio: "inherit" }
|
|
204
|
+
};
|
|
205
|
+
try {
|
|
206
|
+
fs.default.writeFileSync(tempJsonPath, require_logger.safeJsonStringify(args), "utf-8");
|
|
207
|
+
require_logger.logger.debug(`Running Python wrapper with args: ${require_logger.safeJsonStringify(args)}`);
|
|
208
|
+
await new Promise((resolve, reject) => {
|
|
209
|
+
try {
|
|
210
|
+
const pyshell = new python_shell.PythonShell("wrapper.py", pythonOptions);
|
|
211
|
+
pyshell.stdout?.on("data", (chunk) => {
|
|
212
|
+
require_logger.logger.debug(chunk.toString("utf-8").trim());
|
|
213
|
+
});
|
|
214
|
+
pyshell.stderr?.on("data", (chunk) => {
|
|
215
|
+
require_logger.logger.error(chunk.toString("utf-8").trim());
|
|
216
|
+
});
|
|
217
|
+
pyshell.end((err) => {
|
|
218
|
+
if (err) reject(err);
|
|
219
|
+
else resolve();
|
|
220
|
+
});
|
|
221
|
+
} catch (error) {
|
|
222
|
+
reject(error);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
const output = fs.default.readFileSync(outputPath, "utf-8");
|
|
226
|
+
require_logger.logger.debug(`Python script ${absPath} returned: ${output}`);
|
|
227
|
+
let result;
|
|
228
|
+
try {
|
|
229
|
+
result = JSON.parse(output);
|
|
230
|
+
require_logger.logger.debug(`Python script ${absPath} parsed output type: ${typeof result}, structure: ${result ? JSON.stringify(Object.keys(result)) : "undefined"}`);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
throw new Error(`Invalid JSON: ${error.message} when parsing result: ${output}\nStack Trace: ${error.stack}`);
|
|
233
|
+
}
|
|
234
|
+
if (result?.type !== "final_result") throw new Error("The Python script `call_api` function must return a dict with an `output`");
|
|
235
|
+
return result.data;
|
|
236
|
+
} catch (error) {
|
|
237
|
+
require_logger.logger.error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
|
|
238
|
+
throw new Error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
|
|
239
|
+
} finally {
|
|
240
|
+
[tempJsonPath, outputPath].forEach((file) => {
|
|
241
|
+
try {
|
|
242
|
+
fs.default.unlinkSync(file);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
require_logger.logger.error(`Error removing ${file}: ${error}`);
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
//#endregion
|
|
250
|
+
Object.defineProperty(exports, "getConfiguredPythonPath", {
|
|
251
|
+
enumerable: true,
|
|
252
|
+
get: function() {
|
|
253
|
+
return getConfiguredPythonPath;
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
Object.defineProperty(exports, "getEnvInt", {
|
|
257
|
+
enumerable: true,
|
|
258
|
+
get: function() {
|
|
259
|
+
return getEnvInt;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
Object.defineProperty(exports, "runPython", {
|
|
263
|
+
enumerable: true,
|
|
264
|
+
get: function() {
|
|
265
|
+
return runPython;
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
Object.defineProperty(exports, "validatePythonPath", {
|
|
269
|
+
enumerable: true,
|
|
270
|
+
get: function() {
|
|
271
|
+
return validatePythonPath;
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
//# sourceMappingURL=pythonUtils-BD0druiM.cjs.map
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { h as safeJsonStringify, r as logger, v as getEnvBool, x as getEnvString } from "./logger-C40ZGil9.js";
|
|
2
|
+
import { n as getWrapperDir } from "./esm-DMVc93eh.js";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import os from "os";
|
|
6
|
+
import { execFile } from "child_process";
|
|
7
|
+
import { promisify } from "util";
|
|
8
|
+
import { PythonShell } from "python-shell";
|
|
9
|
+
//#region src/python/pythonUtils.ts
|
|
10
|
+
const execFileAsync = promisify(execFile);
|
|
11
|
+
/**
|
|
12
|
+
* Gets an integer value from an environment variable.
|
|
13
|
+
* @param key - The environment variable name
|
|
14
|
+
* @returns The parsed integer value, or undefined if not set or not a valid integer
|
|
15
|
+
*/
|
|
16
|
+
function getEnvInt(key) {
|
|
17
|
+
const value = process.env[key];
|
|
18
|
+
if (value === void 0) return;
|
|
19
|
+
const parsed = parseInt(value, 10);
|
|
20
|
+
return isNaN(parsed) ? void 0 : parsed;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the Python executable path from explicit config and environment.
|
|
24
|
+
* This centralizes the fallback logic: configPath > PROMPTFOO_PYTHON env var.
|
|
25
|
+
*
|
|
26
|
+
* Note: Does NOT apply the final 'python' default - that's handled by
|
|
27
|
+
* validatePythonPath. This preserves the distinction between "explicitly
|
|
28
|
+
* configured" (should fail if invalid) and "using system default" (should
|
|
29
|
+
* try fallback detection).
|
|
30
|
+
*
|
|
31
|
+
* @param configPath - Explicitly configured Python path from provider config
|
|
32
|
+
* @returns The configured path, or undefined if neither config nor env var is set
|
|
33
|
+
*/
|
|
34
|
+
function getConfiguredPythonPath(configPath) {
|
|
35
|
+
if (configPath) return configPath;
|
|
36
|
+
return getEnvString("PROMPTFOO_PYTHON") || void 0;
|
|
37
|
+
}
|
|
38
|
+
const state = {
|
|
39
|
+
cachedPythonPath: null,
|
|
40
|
+
validationPromise: null
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Try to find Python using Windows 'where' command, filtering out Microsoft Store stubs.
|
|
44
|
+
*/
|
|
45
|
+
async function tryWindowsWhere() {
|
|
46
|
+
try {
|
|
47
|
+
const output = (await execFileAsync("where", ["python"])).stdout.trim();
|
|
48
|
+
if (!output) {
|
|
49
|
+
logger.debug("Windows 'where python' returned empty output");
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const paths = output.split("\n").filter((path) => path.trim());
|
|
53
|
+
for (const pythonPath of paths) {
|
|
54
|
+
const trimmedPath = pythonPath.trim();
|
|
55
|
+
if (trimmedPath.includes("WindowsApps") || !trimmedPath.endsWith(".exe")) continue;
|
|
56
|
+
const validated = await tryPath(trimmedPath);
|
|
57
|
+
if (validated) return validated;
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
61
|
+
logger.debug(`Windows 'where python' failed: ${errorMsg}`);
|
|
62
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES")) logger.warn(`Permission denied when searching for Python: ${errorMsg}`);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Try Python commands to get sys.executable path.
|
|
68
|
+
*/
|
|
69
|
+
async function tryPythonCommands(commands) {
|
|
70
|
+
for (const cmd of commands) try {
|
|
71
|
+
const executablePath = (await execFileAsync(cmd, ["-c", "import sys; print(sys.executable)"])).stdout.trim();
|
|
72
|
+
if (executablePath && executablePath !== "None") {
|
|
73
|
+
if (process.platform === "win32" && !executablePath.toLowerCase().endsWith(".exe")) {
|
|
74
|
+
if (executablePath.includes("\\") || /^[A-Za-z]:/.test(executablePath)) return executablePath + ".exe";
|
|
75
|
+
}
|
|
76
|
+
return executablePath;
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
80
|
+
logger.debug(`Python command "${cmd}" failed: ${errorMsg}`);
|
|
81
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) logger.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Try direct command validation as final fallback.
|
|
87
|
+
*/
|
|
88
|
+
async function tryDirectCommands(commands) {
|
|
89
|
+
for (const cmd of commands) try {
|
|
90
|
+
const validated = await tryPath(cmd);
|
|
91
|
+
if (validated) return validated;
|
|
92
|
+
} catch (error) {
|
|
93
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
94
|
+
logger.debug(`Direct command "${cmd}" failed: ${errorMsg}`);
|
|
95
|
+
if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) logger.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Attempts to get the Python executable path using platform-appropriate strategies.
|
|
101
|
+
* @returns The Python executable path if successful, or null if failed.
|
|
102
|
+
*/
|
|
103
|
+
async function getSysExecutable() {
|
|
104
|
+
if (process.platform === "win32") {
|
|
105
|
+
const whereResult = await tryWindowsWhere();
|
|
106
|
+
if (whereResult) return whereResult;
|
|
107
|
+
const sysResult = await tryPythonCommands(["py", "py -3"]);
|
|
108
|
+
if (sysResult) return sysResult;
|
|
109
|
+
return await tryDirectCommands(["python"]);
|
|
110
|
+
} else return await tryPythonCommands(["python3", "python"]);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Attempts to validate a Python executable path.
|
|
114
|
+
* @param path - The path to the Python executable to test.
|
|
115
|
+
* @returns The validated path if successful, or null if invalid.
|
|
116
|
+
*/
|
|
117
|
+
async function tryPath(path) {
|
|
118
|
+
let timeoutId;
|
|
119
|
+
try {
|
|
120
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
121
|
+
timeoutId = setTimeout(() => reject(/* @__PURE__ */ new Error("Command timed out")), 2500);
|
|
122
|
+
});
|
|
123
|
+
const result = await Promise.race([execFileAsync(path, ["--version"]), timeoutPromise]);
|
|
124
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
125
|
+
if (result.stdout.trim().startsWith("Python")) return path;
|
|
126
|
+
return null;
|
|
127
|
+
} catch {
|
|
128
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Validates and caches the Python executable path.
|
|
134
|
+
*
|
|
135
|
+
* @param pythonPath - Path to the Python executable.
|
|
136
|
+
* @param isExplicit - If true, only tries the provided path.
|
|
137
|
+
* @returns Validated Python executable path.
|
|
138
|
+
* @throws {Error} If no valid Python executable is found.
|
|
139
|
+
*/
|
|
140
|
+
async function validatePythonPath(pythonPath, isExplicit) {
|
|
141
|
+
if (state.cachedPythonPath) return state.cachedPythonPath;
|
|
142
|
+
if (!state.validationPromise) state.validationPromise = (async () => {
|
|
143
|
+
try {
|
|
144
|
+
const primaryPath = await tryPath(pythonPath);
|
|
145
|
+
if (primaryPath) {
|
|
146
|
+
state.cachedPythonPath = primaryPath;
|
|
147
|
+
state.validationPromise = null;
|
|
148
|
+
return primaryPath;
|
|
149
|
+
}
|
|
150
|
+
if (isExplicit) {
|
|
151
|
+
const error = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}" Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
|
|
152
|
+
state.validationPromise = null;
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
const detectedPath = await getSysExecutable();
|
|
156
|
+
if (detectedPath) {
|
|
157
|
+
state.cachedPythonPath = detectedPath;
|
|
158
|
+
state.validationPromise = null;
|
|
159
|
+
return detectedPath;
|
|
160
|
+
}
|
|
161
|
+
const error = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}", sys.executable detection, and fallback commands. Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
|
|
162
|
+
state.validationPromise = null;
|
|
163
|
+
throw error;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
state.validationPromise = null;
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
return state.validationPromise;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Runs a Python script with the specified method and arguments.
|
|
173
|
+
*
|
|
174
|
+
* @param scriptPath - The path to the Python script to run.
|
|
175
|
+
* @param method - The name of the method to call in the Python script.
|
|
176
|
+
* @param args - An array of arguments to pass to the Python script.
|
|
177
|
+
* @param options - Optional settings for running the Python script.
|
|
178
|
+
* @param options.pythonExecutable - Optional path to the Python executable.
|
|
179
|
+
* @returns A promise that resolves to the output of the Python script.
|
|
180
|
+
* @throws An error if there's an issue running the Python script or parsing its output.
|
|
181
|
+
*/
|
|
182
|
+
async function runPython(scriptPath, method, args, options = {}) {
|
|
183
|
+
const absPath = path.resolve(scriptPath);
|
|
184
|
+
const tempJsonPath = path.join(os.tmpdir(), `promptfoo-python-input-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
|
|
185
|
+
const outputPath = path.join(os.tmpdir(), `promptfoo-python-output-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
|
|
186
|
+
const customPath = getConfiguredPythonPath(options.pythonExecutable);
|
|
187
|
+
let pythonPath = customPath || "python";
|
|
188
|
+
pythonPath = await validatePythonPath(pythonPath, typeof customPath === "string");
|
|
189
|
+
const pythonOptions = {
|
|
190
|
+
args: [
|
|
191
|
+
absPath,
|
|
192
|
+
method,
|
|
193
|
+
tempJsonPath,
|
|
194
|
+
outputPath
|
|
195
|
+
],
|
|
196
|
+
env: process.env,
|
|
197
|
+
mode: "binary",
|
|
198
|
+
pythonPath,
|
|
199
|
+
scriptPath: getWrapperDir("python"),
|
|
200
|
+
...getEnvBool("PROMPTFOO_PYTHON_DEBUG_ENABLED") && { stdio: "inherit" }
|
|
201
|
+
};
|
|
202
|
+
try {
|
|
203
|
+
fs.writeFileSync(tempJsonPath, safeJsonStringify(args), "utf-8");
|
|
204
|
+
logger.debug(`Running Python wrapper with args: ${safeJsonStringify(args)}`);
|
|
205
|
+
await new Promise((resolve, reject) => {
|
|
206
|
+
try {
|
|
207
|
+
const pyshell = new PythonShell("wrapper.py", pythonOptions);
|
|
208
|
+
pyshell.stdout?.on("data", (chunk) => {
|
|
209
|
+
logger.debug(chunk.toString("utf-8").trim());
|
|
210
|
+
});
|
|
211
|
+
pyshell.stderr?.on("data", (chunk) => {
|
|
212
|
+
logger.error(chunk.toString("utf-8").trim());
|
|
213
|
+
});
|
|
214
|
+
pyshell.end((err) => {
|
|
215
|
+
if (err) reject(err);
|
|
216
|
+
else resolve();
|
|
217
|
+
});
|
|
218
|
+
} catch (error) {
|
|
219
|
+
reject(error);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
const output = fs.readFileSync(outputPath, "utf-8");
|
|
223
|
+
logger.debug(`Python script ${absPath} returned: ${output}`);
|
|
224
|
+
let result;
|
|
225
|
+
try {
|
|
226
|
+
result = JSON.parse(output);
|
|
227
|
+
logger.debug(`Python script ${absPath} parsed output type: ${typeof result}, structure: ${result ? JSON.stringify(Object.keys(result)) : "undefined"}`);
|
|
228
|
+
} catch (error) {
|
|
229
|
+
throw new Error(`Invalid JSON: ${error.message} when parsing result: ${output}\nStack Trace: ${error.stack}`);
|
|
230
|
+
}
|
|
231
|
+
if (result?.type !== "final_result") throw new Error("The Python script `call_api` function must return a dict with an `output`");
|
|
232
|
+
return result.data;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
logger.error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
|
|
235
|
+
throw new Error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
|
|
236
|
+
} finally {
|
|
237
|
+
[tempJsonPath, outputPath].forEach((file) => {
|
|
238
|
+
try {
|
|
239
|
+
fs.unlinkSync(file);
|
|
240
|
+
} catch (error) {
|
|
241
|
+
logger.error(`Error removing ${file}: ${error}`);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
export { validatePythonPath as i, getEnvInt as n, runPython as r, getConfiguredPythonPath as t };
|
|
248
|
+
|
|
249
|
+
//# sourceMappingURL=pythonUtils-IBhn5YGR.js.map
|