Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion crates/factory-server/examples/e2e_fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use factory_core::{
RenovateAccess, RenovateCi, RetryLimits, Session,
};
use factory_runner::ForgeKind;
use factory_server::Store;
use factory_server::{OpenRouterModel, Store};

const ADMIN: &str = "[email protected]";
const COLLABORATOR: &str = "[email protected]";
Expand Down Expand Up @@ -313,6 +313,22 @@ fn seed_local(store: &Store, url: &str, models: Option<&Vec<String>>) -> Result<
Ok(())
}

fn seed_openrouter_models(store: &Store, models: Option<&Vec<String>>) -> Result<()> {
let models: Vec<OpenRouterModel> = models
.map_or(&[][..], Vec::as_slice)
.iter()
.map(|model| {
let (id, name) = model.split_once('|').unwrap_or((model, model));
OpenRouterModel {
id: id.to_owned(),
name: name.to_owned(),
}
})
.collect();
store.replace_openrouter_models(&models, 0)?;
Ok(())
}

fn seed(store: &Store) -> Result<()> {
store.create_project(
SHARED_PROJECT,
Expand Down Expand Up @@ -452,6 +468,7 @@ fn main() -> Result<()> {
let store = Store::open(&db)?;
match command.as_str() {
"seed" => seed(&store)?,
"openrouter-models" => seed_openrouter_models(&store, flags.get("model"))?,
"local-models" => seed_local(&store, one("url")?, flags.get("model"))?,
"project" => {
let name = one("name")?;
Expand Down
15 changes: 14 additions & 1 deletion crates/factory-server/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,9 @@ pub struct AgentChoice {
/// Whether a stage on this agent needs a model, so the panel offers no
/// "CLI default" chip.
pub model_required: bool,
/// For an agent whose model field is a search, the stored models as id and
/// name. The field then draws no chip per model.
pub model_search: Option<Vec<(String, String)>>,
}

/// One server's block inside a project's form: whether the project runs it, the
Expand Down Expand Up @@ -3643,6 +3646,7 @@ fn agent_data(agents: &[AgentChoice]) -> (String, String) {
"models": choice.models,
"hint": choice.model_hint,
"required": choice.model_required,
"search": choice.model_search,
})
});
(efforts, models)
Expand Down Expand Up @@ -3796,6 +3800,7 @@ fn agent_fields(
let choice = project.agents.iter().find(|choice| choice.id == agent);
let suggestions: &[String] = choice.map_or(&[], |choice| &choice.models);
let model_required = choice.is_some_and(|choice| choice.model_required);
let searchable = choice.is_some_and(|choice| choice.model_search.is_some());
let model_hint = choice.map_or_else(
|| {
format!(
Expand Down Expand Up @@ -3831,11 +3836,19 @@ fn agent_fields(
button.chip type="button" data-model=(suggestion) data-model-for=(key) { (suggestion) }
}
}
input id={ (project.name) "-" (key) } type="text" name=(key) list={ "model-suggestions-" (key) }
input id={ (project.name) "-" (key) } type="text" name=(key)
list=[(!searchable).then(|| format!("model-suggestions-{key}"))]
role=[searchable.then_some("combobox")]
aria-autocomplete=[searchable.then_some("list")]
aria-controls=[searchable.then(|| format!("model-results-{key}"))]
aria-expanded=[searchable.then_some("false")]
autocomplete=[searchable.then_some("off")]
placeholder=(if model_required { "Model ID" } else { "CLI default" }) value=(model) data-model-input=(key);
datalist id={ "model-suggestions-" (key) } {
@for suggestion in suggestions { option value=(suggestion) {} }
}
ul.model-results id={ "model-results-" (key) } role="listbox" aria-label="Matches"
data-model-results=(key) hidden {}
p.hint data-model-hint=(key) { (model_hint) }
}
div.field {
Expand Down
13 changes: 11 additions & 2 deletions crates/factory-server/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,12 @@ fn agent_choices(state: &AppState) -> Result<Vec<pages::AgentChoice>, StoreError
let local_url = local.url;
let models_url = local.models_url;
let local_models = state.store.local_models()?;
let openrouter_models: Vec<(String, String)> = state
.store
.openrouter_models()?
.into_iter()
.map(|model| (model.id, model.name))
.collect();
Ok(state
.registry
.entries()
Expand All @@ -2528,8 +2534,10 @@ fn agent_choices(state: &AppState) -> Result<Vec<pages::AgentChoice>, StoreError
.map(|&model| model.to_owned())
.collect()
};
let model_hint = if openrouter {
"Type an OpenRouter model ID. An OpenRouter stage needs a model.".to_owned()
let model_hint = if openrouter && openrouter_models.is_empty() {
"No OpenRouter model list is stored. Type a model id.".to_owned()
} else if openrouter {
format!("Search {} OpenRouter models.", openrouter_models.len())
} else if local {
match (&local_url, models.is_empty()) {
(None, _) => {
Expand Down Expand Up @@ -2569,6 +2577,7 @@ fn agent_choices(state: &AppState) -> Result<Vec<pages::AgentChoice>, StoreError
models,
model_hint,
model_required: local || openrouter,
model_search: openrouter.then(|| openrouter_models.clone()),
}
})
.collect())
Expand Down
111 changes: 107 additions & 4 deletions crates/factory-server/static/settings-pipeline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// The pipeline on a project's settings page. Selecting a stage, a gate or
// merge shows its panel without a reload and updates the `?stage=` value in
// the address and the form's action, so a save comes back to the same panel.
// The model buttons fill the stage's model field, "Use the default" sets a
// The model buttons fill the stage's model field, and an agent with a model
// search lists the stored models that match what is typed. "Use the default" sets a
// gate back to the factory default, and the nodes follow the fields as they
// change. Without this script each node is a link that reloads the page with
// its panel open, and the buttons stay hidden.
Expand Down Expand Up @@ -71,14 +72,80 @@
setEffortLabel(stage, selected);
}

var MAX_RESULTS = 20;

function showResults(stage) {
var results = form.querySelector('[data-model-results="' + stage + '"]');
var input = form.querySelector('[data-model-input="' + stage + '"]');
if (!results || !input) return;
var agent = form.querySelector('input[name="' + stage + '_agent"]:checked');
var spec = agent && AGENT_MODELS[agent.value];
var query = input.value.trim().toLowerCase();
results.textContent = "";
results.removeAttribute("data-active");
input.removeAttribute("aria-activedescendant");
var matches = [];
if (spec && spec.search && query !== "") {
for (var i = 0; i < spec.search.length && matches.length < MAX_RESULTS; i++) {
var model = spec.search[i];
if (
model[0].toLowerCase().indexOf(query) >= 0 ||
model[1].toLowerCase().indexOf(query) >= 0
) {
matches.push(model);
}
}
}
matches.forEach(function (model, index) {
var item = document.createElement("li");
item.id = "model-result-" + stage + "-" + index;
item.setAttribute("role", "option");
item.dataset.modelResult = model[0];
item.textContent = model[1] && model[1] !== model[0] ? model[0] + " (" + model[1] + ")" : model[0];
results.appendChild(item);
});
setExpanded(stage, !!(spec && spec.search), matches.length > 0);
}

function setExpanded(stage, searchable, open) {
var results = form.querySelector('[data-model-results="' + stage + '"]');
var input = form.querySelector('[data-model-input="' + stage + '"]');
results.hidden = !open;
input.removeAttribute("aria-activedescendant");
if (searchable) {
input.setAttribute("role", "combobox");
input.setAttribute("aria-autocomplete", "list");
input.setAttribute("aria-controls", results.id);
input.setAttribute("aria-expanded", open ? "true" : "false");
input.removeAttribute("list");
input.setAttribute("autocomplete", "off");
} else {
input.removeAttribute("role");
input.removeAttribute("aria-autocomplete");
input.removeAttribute("aria-controls");
input.removeAttribute("aria-expanded");
input.removeAttribute("autocomplete");
input.setAttribute("list", "model-suggestions-" + stage);
}
}

function pickResult(stage, id) {
var input = form.querySelector('[data-model-input="' + stage + '"]');
input.value = id;
input.dispatchEvent(new Event("input", { bubbles: true }));
var results = form.querySelector('[data-model-results="' + stage + '"]');
results.textContent = "";
setExpanded(stage, true, false);
}

function redrawModels(stage) {
var agent = form.querySelector('input[name="' + stage + '_agent"]:checked');
var spec = agent && AGENT_MODELS[agent.value];
var chips = form.querySelector('[data-model-chips="' + stage + '"]');
var input = form.querySelector('[data-model-input="' + stage + '"]');
if (!spec || !chips || !input) return;
var previous = AGENT_MODELS[chips.dataset.agent];
var datalist = form.querySelector("#" + input.getAttribute("list"));
var datalist = form.querySelector("#model-suggestions-" + stage);
var hint = form.querySelector('[data-model-hint="' + stage + '"]');
chips.dataset.agent = agent.value;
chips.textContent = "";
Expand All @@ -103,15 +170,23 @@
hint.textContent = spec.hint;
}
input.placeholder = spec.required ? "Model ID" : "CLI default";
showResults(stage);
var value = input.value.trim();
setModelLabel(stage);
if (value !== "" && previous && previous.models.indexOf(value) >= 0) {
var offered = previous ? previous.models.concat((previous.search || []).map(function (m) { return m[0]; })) : [];
if (value !== "" && offered.indexOf(value) >= 0) {
input.value = "";
input.dispatchEvent(new Event("input", { bubbles: true }));
}
}

form.addEventListener("click", function (event) {
var result = event.target.closest("[data-model-result]");
if (result) {
var results = result.closest("[data-model-results]");
pickResult(results.dataset.modelResults, result.dataset.modelResult);
return;
}
var chip = event.target.closest("[data-model-for]");
if (!chip) return;
var input = form.querySelector('[data-model-input="' + chip.dataset.modelFor + '"]');
Expand All @@ -120,7 +195,35 @@
});

form.addEventListener("input", function (event) {
if (event.target.dataset.modelInput) setModelLabel(event.target.dataset.modelInput);
var stage = event.target.dataset.modelInput;
if (!stage) return;
setModelLabel(stage);
showResults(stage);
});

form.addEventListener("keydown", function (event) {
var stage = event.target.dataset && event.target.dataset.modelInput;
var results = stage && form.querySelector('[data-model-results="' + stage + '"]');
if (!results || results.hidden) return;
var items = results.children;
var active = Number(results.dataset.active === undefined ? -1 : results.dataset.active);
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
active = event.key === "ArrowDown" ? Math.min(active + 1, items.length - 1) : Math.max(active - 1, 0);
results.dataset.active = String(active);
Array.prototype.forEach.call(items, function (item, i) {
if (i === active) item.setAttribute("aria-selected", "true");
else item.removeAttribute("aria-selected");
});
event.target.setAttribute("aria-activedescendant", items[active].id);
items[active].scrollIntoView({ block: "nearest" });
event.preventDefault();
} else if (event.key === "Enter" && active >= 0) {
event.preventDefault();
pickResult(stage, items[active].dataset.modelResult);
} else if (event.key === "Escape") {
results.removeAttribute("data-active");
setExpanded(stage, true, false);
}
});

form.addEventListener("change", function (event) {
Expand Down
4 changes: 4 additions & 0 deletions crates/factory-server/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,10 @@ form.card { padding: var(--space-24); margin-bottom: var(--space-24); }
.pipeline-js .model-chips { display: flex; }
.chip { border: var(--border-width) solid var(--border); border-radius: var(--radius-pill); background: var(--surface); color: var(--text); padding: 3px var(--space-10); font-size: var(--font-size-sm); }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.model-results { list-style: none; margin: var(--space-6) 0 0; padding: 0; border: var(--border-width) solid var(--border); border-radius: var(--radius-md); background: var(--surface); max-height: 16rem; overflow-y: auto; }
.model-results[hidden] { display: none; }
.model-results li { padding: var(--space-6) var(--space-10); font-size: var(--font-size-sm); cursor: pointer; }
.model-results li:hover, .model-results li[aria-selected="true"] { color: var(--accent); text-decoration: underline; }
.seg { display: inline-flex; border: var(--border-width) solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.seg label { display: block; position: relative; margin: 0; font-weight: normal; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
Expand Down
58 changes: 58 additions & 0 deletions crates/factory-server/tests/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8031,6 +8031,64 @@ async fn a_local_stage_offers_the_stored_models_and_names_the_server() {
assert!(page.body.contains("&quot;local&quot;"));
}

#[tokio::test]
async fn an_openrouter_stage_is_a_search_over_the_stored_models_with_no_chips() {
let dashboard = dashboard();
dashboard
.store
.replace_openrouter_models(
&[
factory_server::OpenRouterModel {
id: "openai/gpt-5".to_owned(),
name: "OpenAI: GPT-5".to_owned(),
},
factory_server::OpenRouterModel {
id: "x-ai/grok-4".to_owned(),
name: "xAI: Grok 4".to_owned(),
},
],
1,
)
.unwrap();
dashboard
.store
.save_project_agents(
"maestro",
&agents("openrouter", "claude", "claude", "claude", "claude"),
)
.unwrap();
let page = dashboard.get("/settings/projects/maestro").await;
let start = page.body.find("data-panel=\"clarify\"").unwrap();
let rest = &page.body[start..];
let panel = &rest[..rest.find("data-model-hint").unwrap() + 200];
assert!(!panel.contains("data-model=\""), "no chip, no CLI default");
assert!(!panel.contains("<option"));
assert!(panel.contains("data-model-results=\"clarify\""));
assert!(panel.contains("Search 2 OpenRouter models."));
assert!(
page.body
.contains("&quot;search&quot;:[[&quot;openai/gpt-5&quot;")
);
assert!(page.body.contains("Model required"));
}

#[tokio::test]
async fn an_openrouter_stage_without_a_stored_list_says_to_type_an_id() {
let dashboard = dashboard();
dashboard
.store
.save_project_agents(
"maestro",
&agents("openrouter", "claude", "claude", "claude", "claude"),
)
.unwrap();
let page = dashboard.get("/settings/projects/maestro").await;
assert!(
page.body
.contains("No OpenRouter model list is stored. Type a model id.")
);
}

#[tokio::test]
async fn a_local_stage_without_a_stored_list_offers_no_chips() {
let dashboard = dashboard();
Expand Down
Loading