@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
85 lines • 3.28 kB
JavaScript
export function isSecurityNoticeState(value) {
if (typeof value !== "object" ||
value === null ||
!("generation" in value) ||
typeof value.generation !== "number" ||
!Number.isSafeInteger(value.generation) ||
value.generation < 0 ||
!("notices" in value) ||
!Array.isArray(value.notices))
return false;
return value.notices.every((notice) => typeof notice === "object" &&
notice !== null &&
"category" in notice &&
(notice.category === "skill-link-blocked" || notice.category === "mcp-env-withheld") &&
"message" in notice &&
typeof notice.message === "string" &&
(!("path" in notice) || notice.path === undefined || typeof notice.path === "string"));
}
/** Session state, with an independent replay cursor owned by each subscription. */
export class SecurityNoticeSnapshot {
#notices = new Map();
#listeners = new Set();
#generation = 0;
get snapshot() {
return { generation: this.#generation, notices: [...this.#notices.values()] };
}
setResources(diagnostics) {
const before = JSON.stringify([...this.#notices]);
for (const key of this.#notices.keys())
if (key.startsWith("resource:"))
this.#notices.delete(key);
for (const diagnostic of diagnostics) {
if (diagnostic.category !== "skill-link-blocked")
continue;
this.#notices.set(`resource:${diagnostic.path ?? diagnostic.message}`, {
category: "skill-link-blocked",
message: diagnostic.message,
path: diagnostic.path,
});
}
if (before !== JSON.stringify([...this.#notices]))
this.#publish();
}
add(notice) {
const key = `runtime:${notice.category}:${notice.path ?? notice.message}`;
if (JSON.stringify(this.#notices.get(key)) === JSON.stringify(notice))
return;
this.#notices.set(key, notice);
this.#publish();
}
accept(value) {
if (typeof value !== "object" ||
value === null ||
!("message" in value) ||
typeof value.message !== "string" ||
!("category" in value) ||
(value.category !== "skill-link-blocked" && value.category !== "mcp-env-withheld"))
return;
if ("remove" in value && value.remove === true && "path" in value && typeof value.path === "string") {
if (this.#notices.delete(`runtime:${value.category}:${value.path}`))
this.#publish();
return;
}
this.add({
category: value.category,
message: value.message,
...("path" in value && typeof value.path === "string" ? { path: value.path } : {}),
});
}
subscribe(listener) {
this.#listeners.add(listener);
listener(this.snapshot);
return () => this.#listeners.delete(listener);
}
dispose() {
this.#listeners.clear();
}
#publish() {
this.#generation++;
const snapshot = this.snapshot;
for (const listener of this.#listeners)
listener(snapshot);
}
}
//# sourceMappingURL=security-notices.js.map