Spaces:
Paused
Paused
hfpi: follow the Space's default model and model list on every boot, keeping the user's own choices
Browse files- sandbox/hfpi.mjs +32 -8
sandbox/hfpi.mjs
CHANGED
|
@@ -39,6 +39,8 @@ const AGENT_SAVE_SECONDS = Number(process.env.HFPI_AGENT_SAVE_SECONDS ?? 20);
|
|
| 39 |
const PROJECT_SAVE_SECONDS = Number(process.env.HFPI_PROJECT_SAVE_SECONDS ?? 120);
|
| 40 |
const NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
| 41 |
const MANAGED_MARK = "<!-- managed by hfpi: delete this line to keep your own edits -->";
|
|
|
|
|
|
|
| 42 |
|
| 43 |
// Regenerable directories are not saved: they make archives 20x larger and slower.
|
| 44 |
// Python virtualenvs are found by their pyvenv.cfg, Rust/pytest caches by CACHEDIR.TAG.
|
|
@@ -408,6 +410,32 @@ function restoreProjects() {
|
|
| 408 |
|
| 409 |
// ---------------------------------------------------------------- pi configuration
|
| 410 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
function configurePi() {
|
| 412 |
fs.mkdirSync(AGENT_DIR, { recursive: true });
|
| 413 |
|
|
@@ -427,17 +455,13 @@ function configurePi() {
|
|
| 427 |
writeJson(modelsPath, models);
|
| 428 |
|
| 429 |
const settingsPath = path.join(AGENT_DIR, "settings.json");
|
|
|
|
| 430 |
const settings = readJson(settingsPath, {}) || {};
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
settings.defaultModel = process.env.HFPI_DEFAULT_MODEL || "zai-org/GLM-5.3";
|
| 434 |
-
settings.defaultThinkingLevel = settings.defaultThinkingLevel || "medium";
|
| 435 |
-
}
|
| 436 |
-
if (!settings.enabledModels && process.env.HFPI_ENABLED_MODELS) {
|
| 437 |
-
settings.enabledModels = process.env.HFPI_ENABLED_MODELS.split(",").map((s) => s.trim()).filter(Boolean);
|
| 438 |
-
}
|
| 439 |
settings.enableInstallTelemetry = false;
|
| 440 |
writeJson(settingsPath, settings);
|
|
|
|
| 441 |
|
| 442 |
// Global context about this environment, rewritten each boot unless the user
|
| 443 |
// removed the managed marker to keep their own version.
|
|
|
|
| 39 |
const PROJECT_SAVE_SECONDS = Number(process.env.HFPI_PROJECT_SAVE_SECONDS ?? 120);
|
| 40 |
const NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
| 41 |
const MANAGED_MARK = "<!-- managed by hfpi: delete this line to keep your own edits -->";
|
| 42 |
+
const DEFAULT_MODEL = "deepseek-ai/DeepSeek-V4.1-Flash";
|
| 43 |
+
const LEGACY_DEFAULT_MODEL = "zai-org/GLM-5.3"; // the default before hfpi-models.json recorded it
|
| 44 |
|
| 45 |
// Regenerable directories are not saved: they make archives 20x larger and slower.
|
| 46 |
// Python virtualenvs are found by their pyvenv.cfg, Rust/pytest caches by CACHEDIR.TAG.
|
|
|
|
| 410 |
|
| 411 |
// ---------------------------------------------------------------- pi configuration
|
| 412 |
|
| 413 |
+
// The Space's default model and model list follow the Space on every boot, without
|
| 414 |
+
// undoing what the user chose in Pi Web: `applied` is what hfpi set last time (none
|
| 415 |
+
// in workspaces from before this record, when the default was LEGACY_DEFAULT_MODEL).
|
| 416 |
+
// Returns the new record. Mutates `settings`.
|
| 417 |
+
function mergeModelSettings(settings, applied, defaultModel, spaceModels) {
|
| 418 |
+
const before = applied || { defaultModel: settings.defaultProvider ? LEGACY_DEFAULT_MODEL : null, enabledModels: [] };
|
| 419 |
+
if (!settings.defaultProvider) {
|
| 420 |
+
settings.defaultThinkingLevel = settings.defaultThinkingLevel || "medium";
|
| 421 |
+
}
|
| 422 |
+
if (!settings.defaultProvider || (settings.defaultProvider === "huggingface" && settings.defaultModel === before.defaultModel)) {
|
| 423 |
+
settings.defaultProvider = "huggingface";
|
| 424 |
+
settings.defaultModel = defaultModel;
|
| 425 |
+
}
|
| 426 |
+
if (spaceModels.length) {
|
| 427 |
+
if (!settings.enabledModels) {
|
| 428 |
+
settings.enabledModels = [...spaceModels];
|
| 429 |
+
} else {
|
| 430 |
+
// Drop what the Space dropped, add what it added; the user's own changes stay.
|
| 431 |
+
const kept = settings.enabledModels.filter((m) => spaceModels.includes(m) || !before.enabledModels.includes(m));
|
| 432 |
+
const added = spaceModels.filter((m) => !kept.includes(m) && !before.enabledModels.includes(m));
|
| 433 |
+
settings.enabledModels = [...kept, ...added];
|
| 434 |
+
}
|
| 435 |
+
}
|
| 436 |
+
return { defaultModel, enabledModels: spaceModels };
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
function configurePi() {
|
| 440 |
fs.mkdirSync(AGENT_DIR, { recursive: true });
|
| 441 |
|
|
|
|
| 455 |
writeJson(modelsPath, models);
|
| 456 |
|
| 457 |
const settingsPath = path.join(AGENT_DIR, "settings.json");
|
| 458 |
+
const managedPath = path.join(AGENT_DIR, "hfpi-models.json");
|
| 459 |
const settings = readJson(settingsPath, {}) || {};
|
| 460 |
+
const spaceModels = (process.env.HFPI_ENABLED_MODELS || "").split(",").map((s) => s.trim()).filter(Boolean);
|
| 461 |
+
const applied = mergeModelSettings(settings, readJson(managedPath, null), process.env.HFPI_DEFAULT_MODEL || DEFAULT_MODEL, spaceModels);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
settings.enableInstallTelemetry = false;
|
| 463 |
writeJson(settingsPath, settings);
|
| 464 |
+
writeJson(managedPath, applied);
|
| 465 |
|
| 466 |
// Global context about this environment, rewritten each boot unless the user
|
| 467 |
// removed the managed marker to keep their own version.
|