char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Stores split refactor (#27240)521a64cd0

char/aliceai-t5-moe
1.8 KiB75 linesraw
1/**
2 * permissionsStore - Allowed tool permissions
3 *
4 * Owns the set of tools the user has permanently allowed, persisted to
5 * localStorage. The agentic loop's permission gates consult it to run a
6 * tool without prompting.
7 */
8
9import { browser } from '$app/environment';
10import { ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY } from '$lib/constants';
11import { SvelteSet } from 'svelte/reactivity';
12
13class PermissionsStore {
14	private _tools = $state(new SvelteSet<string>());
15
16	get tools(): ReadonlySet<string> {
17		return this._tools;
18	}
19
20	allowTool(key: string): void {
21		this._tools.add(key);
22		this.persist();
23	}
24
25	allowTools(keys: string[]): void {
26		for (const key of keys) this._tools.add(key);
27		this.persist();
28	}
29
30	hasTool(key: string): boolean {
31		return this._tools.has(key);
32	}
33
34	/**
35	 * Load persisted permissions. Called by initStores() after migrations
36	 * have run.
37	 */
38	initialize(): void {
39		// browser-only init: skip on SSR to avoid localStorage side effects
40		if (!browser) return;
41
42		try {
43			const stored = localStorage.getItem(ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY);
44
45			if (stored) {
46				for (const name of JSON.parse(stored) as string[]) {
47					if (typeof name === 'string') this._tools.add(name);
48				}
49			}
50		} catch (err) {
51			console.error(
52				`Failed to load permissions from localStorage ("${ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY}"):`,
53				err
54			);
55		}
56	}
57
58	revokeTool(key: string): void {
59		this._tools.delete(key);
60		this.persist();
61	}
62
63	private persist(): void {
64		try {
65			localStorage.setItem(ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY, JSON.stringify([...this._tools]));
66		} catch (err) {
67			console.error(
68				`Failed to persist to localStorage ("${ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY}"):`,
69				err
70			);
71		}
72	}
73}
74
75export const permissionsStore = new PermissionsStore();