TUI: honor explicit session key in global scope

This commit is contained in:
Vignesh Natarajan
2026-02-14 16:40:37 -08:00
parent b08146fad6
commit 56b38d2fbe
2 changed files with 64 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { resolveFinalAssistantText } from "./tui.js";
import { resolveFinalAssistantText, resolveTuiSessionKey } from "./tui.js";
describe("resolveFinalAssistantText", () => {
it("falls back to streamed text when final text is empty", () => {
@@ -15,3 +15,35 @@ describe("resolveFinalAssistantText", () => {
).toBe("All done");
});
});
describe("resolveTuiSessionKey", () => {
it("uses global only as the default when scope is global", () => {
expect(
resolveTuiSessionKey({
raw: "",
sessionScope: "global",
currentAgentId: "main",
sessionMainKey: "agent:main:main",
}),
).toBe("global");
expect(
resolveTuiSessionKey({
raw: "test123",
sessionScope: "global",
currentAgentId: "main",
sessionMainKey: "agent:main:main",
}),
).toBe("agent:main:test123");
});
it("keeps explicit agent-prefixed keys unchanged", () => {
expect(
resolveTuiSessionKey({
raw: "agent:ops:incident",
sessionScope: "global",
currentAgentId: "main",
sessionMainKey: "agent:main:main",
}),
).toBe("agent:ops:incident");
});
});

View File

@@ -77,6 +77,31 @@ export function createEditorSubmitHandler(params: {
};
}
export function resolveTuiSessionKey(params: {
raw?: string;
sessionScope: SessionScope;
currentAgentId: string;
sessionMainKey: string;
}) {
const trimmed = (params.raw ?? "").trim();
if (!trimmed) {
if (params.sessionScope === "global") {
return "global";
}
return buildAgentMainSessionKey({
agentId: params.currentAgentId,
mainKey: params.sessionMainKey,
});
}
if (trimmed === "global" || trimmed === "unknown") {
return trimmed;
}
if (trimmed.startsWith("agent:")) {
return trimmed;
}
return `agent:${params.currentAgentId}:${trimmed}`;
}
export async function runTui(opts: TuiOptions) {
const config = loadConfig();
const initialSessionInput = (opts.session ?? "").trim();
@@ -298,23 +323,12 @@ export async function runTui(opts: TuiOptions) {
};
const resolveSessionKey = (raw?: string) => {
const trimmed = (raw ?? "").trim();
if (sessionScope === "global") {
return "global";
}
if (!trimmed) {
return buildAgentMainSessionKey({
agentId: currentAgentId,
mainKey: sessionMainKey,
});
}
if (trimmed === "global" || trimmed === "unknown") {
return trimmed;
}
if (trimmed.startsWith("agent:")) {
return trimmed;
}
return `agent:${currentAgentId}:${trimmed}`;
return resolveTuiSessionKey({
raw,
sessionScope,
currentAgentId,
sessionMainKey,
});
};
currentSessionKey = resolveSessionKey(initialSessionInput);