File size: 10,446 Bytes
3d700dd | 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | import { describe, it, expect, beforeEach } from "vitest";
import { useModelStore } from "#/stores/model-store";
import {
getStoredConversationMetadata,
setStoredConversationMetadata,
} from "#/api/conversation-metadata-store";
import { OpenHandsEvent } from "#/types/agent-server/core";
import { seedModelSwitchesFromHistory } from "./record-model-switch-message";
const userMessage = (id: string): OpenHandsEvent =>
({
id,
timestamp: "2024-01-01T00:00:00Z",
source: "user",
llm_message: { role: "user", content: [{ type: "text", text: "hi" }] },
}) as unknown as OpenHandsEvent;
const switchObservation = (
id: string,
profileName: string,
isError = false,
timestamp = "2024-01-01T00:00:00Z",
): OpenHandsEvent =>
({
id,
timestamp,
source: "environment",
action_id: `action-${id}`,
observation: {
kind: "SwitchLLMObservation",
content: [],
is_error: isError,
profile_name: profileName,
reason: null,
active_model: null,
},
}) as unknown as OpenHandsEvent;
// An agent action event. `ThinkAction` is renderable (shown as a thinking
// block); `PlanningFileEditorAction` is hidden by `shouldRenderEvent`.
const agentAction = (id: string, kind: string): OpenHandsEvent =>
({
id,
timestamp: "2024-01-01T00:00:00Z",
source: "agent",
action: { kind, thought: "t" },
tool_name: "tool",
tool_call_id: `call-${id}`,
}) as unknown as OpenHandsEvent;
const entriesFor = (conversationId: string) =>
useModelStore.getState().entriesByConversation[conversationId] ?? [];
const activeProfileFor = (conversationId: string) =>
useModelStore.getState().activeProfileByConversation[conversationId];
const stampedProfileFor = (conversationId: string) =>
getStoredConversationMetadata(conversationId)?.active_profile;
describe("seedModelSwitchesFromHistory", () => {
beforeEach(() => {
useModelStore.getState().clearAll();
window.localStorage.clear();
});
it("seeds a successful switch anchored to the prior renderable event", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
const entries = entriesFor("c1");
expect(entries).toHaveLength(1);
expect(entries[0].switchedTo).toBe("fast");
expect(entries[0].anchorEventId).toBe("u1");
expect(entries[0].id).toBe("history-switch:o1");
});
it("is idempotent across re-seeds (e.g. reloads)", () => {
const events = [userMessage("u1"), switchObservation("o1", "fast")];
seedModelSwitchesFromHistory("c1", events);
seedModelSwitchesFromHistory("c1", events);
expect(entriesFor("c1")).toHaveLength(1);
});
it("ignores failed switches (they still render as error cards)", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("e1", "fast", true),
]);
expect(entriesFor("c1")).toHaveLength(0);
});
it("never anchors to a non-rendered event (must land on a rendered id)", () => {
// PlanningFileEditorAction is hidden by shouldRenderEvent, so the renderer
// never mounts it; anchoring there would orphan the message. The anchor
// must fall back to the prior rendered event (the user message).
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
agentAction("p1", "PlanningFileEditorAction"),
switchObservation("o1", "fast"),
]);
const entries = entriesFor("c1");
expect(entries).toHaveLength(1);
expect(entries[0].anchorEventId).toBe("u1");
});
it("anchors to a renderable ThinkAction that precedes the switch", () => {
// In uiEvents the ThinkObservation is dropped and the ThinkAction is kept
// (and rendered as a thinking block), so it is a valid anchor.
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
agentAction("t1", "ThinkAction"),
switchObservation("o1", "architect"),
]);
const entries = entriesFor("c1");
expect(entries).toHaveLength(1);
expect(entries[0].anchorEventId).toBe("t1");
});
it("anchors to null when no renderable event precedes the switch", () => {
seedModelSwitchesFromHistory("c1", [switchObservation("o1", "architect")]);
const entries = entriesFor("c1");
expect(entries).toHaveLength(1);
expect(entries[0].anchorEventId).toBeNull();
});
it("preserves order and anchors for multiple switches", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
userMessage("u2"),
switchObservation("o2", "architect"),
]);
const entries = entriesFor("c1");
expect(entries).toHaveLength(2);
expect(entries[0]).toMatchObject({
switchedTo: "fast",
anchorEventId: "u1",
});
expect(entries[1]).toMatchObject({
switchedTo: "architect",
anchorEventId: "u2",
});
});
it("re-stamps the active profile from a switch in the loaded history", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(activeProfileFor("c1")).toBe("fast");
expect(stampedProfileFor("c1")).toBe("fast");
});
it("stamps the latest switch when history holds several", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
userMessage("u2"),
switchObservation("o2", "architect"),
]);
expect(activeProfileFor("c1")).toBe("architect");
expect(stampedProfileFor("c1")).toBe("architect");
});
it("leaves the stamp untouched when history has no switch", () => {
setStoredConversationMetadata("c1", {
selected_repository: null,
selected_branch: null,
git_provider: null,
selected_workspace: null,
active_profile: "original",
plugins: null,
});
useModelStore.getState().setActiveProfile("c1", "original");
seedModelSwitchesFromHistory("c1", [userMessage("u1")]);
expect(activeProfileFor("c1")).toBe("original");
expect(stampedProfileFor("c1")).toBe("original");
});
it("does not stamp from a failed switch", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("e1", "fast", true),
]);
expect(activeProfileFor("c1")).toBeUndefined();
expect(stampedProfileFor("c1")).toBeUndefined();
});
it("keys the stamp per conversation", () => {
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(activeProfileFor("c2")).toBeUndefined();
expect(stampedProfileFor("c2")).toBeUndefined();
});
it("preserves the other stored metadata fields when re-stamping", () => {
setStoredConversationMetadata("c1", {
selected_repository: "org/repo",
selected_branch: "main",
git_provider: "github",
selected_workspace: "/ws",
active_profile: "stale",
plugins: [{ source: "s", ref: "r", repo_path: null }],
});
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(getStoredConversationMetadata("c1")).toMatchObject({
selected_repository: "org/repo",
selected_branch: "main",
git_provider: "github",
selected_workspace: "/ws",
active_profile: "fast",
plugins: [{ source: "s", ref: "r", repo_path: null }],
});
});
it("preserves workspace_mode across a stamp (#15520)", () => {
setStoredConversationMetadata("c1", {
selected_repository: null,
selected_branch: null,
git_provider: null,
selected_workspace: "/ws",
workspace_mode: "local_repo",
});
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(getStoredConversationMetadata("c1")?.workspace_mode).toBe(
"local_repo",
);
});
it("leaves a newer manual stamp alone (manual switch after a tool switch survives reload)", () => {
// t1: agent tool-switches to "fast" (observation in history). t2: user
// manually switches back to "architect" via /model — a REST call that
// leaves no observation. The reload seed must not roll the stamp back.
setStoredConversationMetadata("c1", {
selected_repository: null,
selected_branch: null,
git_provider: null,
active_profile: "architect",
stamped_at: "2024-06-01T00:00:00Z",
});
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(getStoredConversationMetadata("c1")?.active_profile).toBe(
"architect",
);
expect(getStoredConversationMetadata("c1")?.stamped_at).toBe(
"2024-06-01T00:00:00Z",
);
// The in-memory stamp takes priority over the persisted one in the pill,
// so it must not be set to the stale profile either.
expect(activeProfileFor("c1")).toBeUndefined();
});
it("repairs a legacy stamp written before stamped_at existed", () => {
setStoredConversationMetadata("c1", {
selected_repository: null,
selected_branch: null,
git_provider: null,
active_profile: "architect",
});
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(stampedProfileFor("c1")).toBe("fast");
expect(getStoredConversationMetadata("c1")?.stamped_at).toBe(
"2024-01-01T00:00:00Z",
);
});
it("re-stamps when the latest observation is newer than the stored stamp", () => {
setStoredConversationMetadata("c1", {
selected_repository: null,
selected_branch: null,
git_provider: null,
active_profile: "architect",
stamped_at: "2023-01-01T00:00:00Z",
});
seedModelSwitchesFromHistory("c1", [
userMessage("u1"),
switchObservation("o1", "fast"),
]);
expect(stampedProfileFor("c1")).toBe("fast");
expect(getStoredConversationMetadata("c1")?.stamped_at).toBe(
"2024-01-01T00:00:00Z",
);
});
it("stamps with the observation timestamp so re-seeding the same history is a no-op", () => {
const events = [userMessage("u1"), switchObservation("o1", "fast")];
seedModelSwitchesFromHistory("c1", events);
seedModelSwitchesFromHistory("c1", events);
expect(getStoredConversationMetadata("c1")?.stamped_at).toBe(
"2024-01-01T00:00:00Z",
);
});
});
|