Skip to content

Commit eaae356

Browse files
jjgaoclaude
andcommitted
Stop naming a connector with no credential, and clear a stale connector status
A connector stored as "on" that had no token (OpenEvidence was on from before it needed one) showed as off in the Connectors panel but was still named in every chat request, so each message came back "no API token: add yours". After the reader added the token, that last-message error stayed on screen, including across closing and reopening the sidebar. requestEntries now names only connectors that are on AND may be (the same rule the panel uses), and any change to a connector's switch or token clears its remembered status. Verified: tsc, eslint and the 18 connector-state tests pass. In Chrome, from a leftover "on" with no token: a message names no connectors and shows no error; after pasting a token, closing and reopening, and switching on, the request names the connector with its token, the server reports it reached with 1 tool, and the approval card appears. Refs #128 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012XdQQNTxj4XzvBoLxynDRu
1 parent e05957b commit eaae356

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

‎app/_agent/ChatSidebar.tsx‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ function ChatPanel({ info }: { info: ChatInfo }) {
9595
infoRef.current = info;
9696
const statusSink = useRef<(s: ConnectorStatus[]) => void>(() => {});
9797
statusSink.current = (list) => setConnStatus(Object.fromEntries(list.map((s) => [s.id, s])));
98-
const saveConn = (next: ConnectorState) => { setConn(next); setConnSaveFailed(!saveState(local(), next)); };
98+
// a connector's last-turn status describes the settings it had then: any change to it clears the status
99+
const saveConn = (next: ConnectorState, changed: string) => {
100+
setConn(next);
101+
setConnSaveFailed(!saveState(local(), next));
102+
setConnStatus((s) => Object.fromEntries(Object.entries(s).filter(([id]) => id !== changed)));
103+
};
99104

100105
const transport = useMemo(() => new DefaultChatTransport({
101106
api: "/api/chat",
@@ -227,9 +232,9 @@ function ChatPanel({ info }: { info: ChatInfo }) {
227232
onClear={() => { setHistory([]); setHistoryNote({ dropped: 0, failed: !saveHistory(local(), []) }); }} />}
228233
{showConnectors && <ConnectorsPanel connectors={info.connectors} error={info.connectorsError} state={conn} status={connStatus} note={connNote} saveFailed={connSaveFailed}
229234
onDismissNote={() => setConnNote(null)}
230-
onSwitch={(id, on) => saveConn(withOn(conn, id, on))}
231-
onDisconnect={(id) => saveConn(withToken(conn, id, null))}
232-
onSaveToken={(id, token) => saveConn(withToken(conn, id, { token }))}
235+
onSwitch={(id, on) => saveConn(withOn(conn, id, on), id)}
236+
onDisconnect={(id) => saveConn(withToken(conn, id, null), id)}
237+
onSaveToken={(id, token) => saveConn(withToken(conn, id, { token }), id)}
233238
onConnect={(id) => { location.assign(connectUrl(id, location.pathname + location.search)); }} />}
234239
<Conversation className={showHistory || showConnectors ? "hidden" : undefined}>
235240
<ConversationContent>

‎lib/chat-connector-state.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export function withToken(s: ConnectorState, id: string, t: ConnectorToken | nul
5050
if (t) tokens[id] = t; else delete tokens[id];
5151
return { on: t ? s.on : s.on.filter((x) => x !== id), tokens };
5252
}
53-
/** What a chat request names: each connector that is on, with its token when it has a good one. */
53+
/**
54+
* What a chat request names: each connector that is on AND may be (an OAuth or token one has a good
55+
* credential), with its token. One stored as on that has lost its credential is left out, as the panel
56+
* shows it off, so the model is not told about a connector that cannot answer.
57+
*/
5458
export function requestEntries(s: ConnectorState, known: Pick<Connector, "id" | "auth">[], now: number): { id: string; token?: string }[] {
55-
return known.filter((c) => s.on.includes(c.id)).map((c) => ({ id: c.id, ...(signedIn(s, c.id, now) ? { token: s.tokens[c.id].token } : {}) }));
59+
return known.filter((c) => s.on.includes(c.id) && canSwitchOn(s, c, now).ok).map((c) => ({ id: c.id, ...(signedIn(s, c.id, now) ? { token: s.tokens[c.id].token } : {}) }));
5660
}

‎test/chat-connectors.test.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,20 @@ describe("the browser's connector state", () => {
112112
assert.deepEqual(canSwitchOn(withToken(empty, "cbioportal", { token: "t", expiresAt: 5 }), cb, 10), { ok: false, why: "the sign-in has expired: connect again" });
113113
assert.equal(signedIn(withToken(empty, "cbioportal", { token: "t" }), "cbioportal", 1e15), true, "no expiry given, none assumed");
114114
});
115-
test("signing out switches the connector off, and a request names only what is on, with a good token", () => {
116-
const base = withToken(withOn(withOn({ on: [], tokens: {} }, "cbioportal", true), "openevidence", true), "cbioportal", { token: "tok", expiresAt: 100 });
117-
assert.deepEqual(requestEntries(base, DEFAULT_CONNECTORS, 10), [{ id: "openevidence" }, { id: "cbioportal", token: "tok" }]);
118-
assert.deepEqual(requestEntries(base, DEFAULT_CONNECTORS, 200), [{ id: "openevidence" }, { id: "cbioportal" }], "an expired token is not sent");
119-
const out = withToken(base, "cbioportal", null);
120-
assert.deepEqual(out.on, ["openevidence"]);
115+
test("signing out switches the connector off", () => {
116+
const st = withToken(withOn(withOn({ on: [], tokens: {} }, "cbioportal", true), "openevidence", true), "cbioportal", { token: "tok", expiresAt: 100 });
117+
assert.deepEqual(withToken(st, "cbioportal", null).on, ["openevidence"]);
118+
});
119+
test("a request names what is on and may be, each with its token", () => {
120+
const st = withToken(withToken(withOn(withOn({ on: [], tokens: {} }, "cbioportal", true), "openevidence", true), "openevidence", { token: "k" }), "cbioportal", { token: "tok", expiresAt: 100 });
121+
assert.deepEqual(requestEntries(st, DEFAULT_CONNECTORS, 10), [{ id: "openevidence", token: "k" }, { id: "cbioportal", token: "tok" }]);
122+
assert.deepEqual(requestEntries(st, DEFAULT_CONNECTORS, 200), [{ id: "openevidence", token: "k" }], "a sign-in that has run out is not named");
121123
assert.deepEqual(requestEntries(withOn({ on: [], tokens: {} }, "x", true), DEFAULT_CONNECTORS, 1), [], "an id the deployment does not have is not named");
122124
});
125+
test("one stored as on that has no credential is not named, as the panel shows it off (it was: 'no API token' on every message)", () => {
126+
const st = withOn({ on: [], tokens: {} }, "openevidence", true);
127+
assert.deepEqual(st.on, ["openevidence"]);
128+
assert.deepEqual(requestEntries(st, DEFAULT_CONNECTORS, 10), []);
129+
assert.deepEqual(requestEntries(withToken(st, "openevidence", { token: "k" }), DEFAULT_CONNECTORS, 10), [{ id: "openevidence", token: "k" }], "and is named once it has one");
130+
});
123131
});

0 commit comments

Comments
 (0)