Initial commit: Velum Raycast extension

Pseudonymize-and-AI workflow for handling PII-sensitive text via the
Velum API and Raycast AI. Commands cover end-to-end email summary and
reply, briefing/action-items/structured-data extraction, manual
pseudonymize/depseudonymize on selection or clipboard, and session
management. Includes Raycast 2.0 Beta workarounds for selection capture
and rich-text clipboard.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
muena
2026-05-20 06:46:42 +02:00
commit 1843479884
31 changed files with 7855 additions and 0 deletions

133
src/auth.ts Normal file
View File

@@ -0,0 +1,133 @@
import { LocalStorage } from "@raycast/api";
import { getPreferences } from "./preferences";
const TOKEN_STORAGE_KEY = "velum.auth.token.v1";
const TOKEN_EXPIRY_SKEW_MS = 30_000;
type StoredToken = {
accessToken: string;
tokenType: string;
expiresAt: number;
authKey: string;
};
type TokenResponse = {
access_token: string;
token_type?: string;
expires_in?: number;
};
function currentAuthKey(): string {
const preferences = getPreferences();
return [
preferences.authentikTokenUrl,
preferences.clientId,
preferences.serviceAccountUsername,
].join("|");
}
async function getStoredToken(): Promise<StoredToken | undefined> {
const raw = await LocalStorage.getItem<string>(TOKEN_STORAGE_KEY);
if (!raw) {
return undefined;
}
try {
const token = JSON.parse(raw) as StoredToken;
if (token.authKey !== currentAuthKey()) {
return undefined;
}
if (
!token.accessToken ||
Date.now() + TOKEN_EXPIRY_SKEW_MS >= token.expiresAt
) {
return undefined;
}
return token;
} catch {
await clearAccessToken();
return undefined;
}
}
export async function clearAccessToken(): Promise<void> {
await LocalStorage.removeItem(TOKEN_STORAGE_KEY);
}
export async function getAccessToken(): Promise<string> {
const stored = await getStoredToken();
if (stored) {
return stored.accessToken;
}
return refreshAccessToken();
}
export async function refreshAccessToken(): Promise<string> {
const preferences = getPreferences();
const body = new URLSearchParams({
grant_type: "client_credentials",
client_id: preferences.clientId,
username: preferences.serviceAccountUsername,
password: preferences.serviceAccountPassword,
scope: preferences.scope,
});
const response = await fetch(preferences.authentikTokenUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body,
});
if (!response.ok) {
const details = await response.text().catch(() => response.statusText);
throw new Error(
`Authentik token request failed: ${response.status} ${response.statusText}: ${details}`,
);
}
const token = (await response.json()) as TokenResponse;
if (!token.access_token) {
throw new Error("Authentik token response did not include access_token.");
}
const stored: StoredToken = {
accessToken: token.access_token,
tokenType: token.token_type || "Bearer",
expiresAt: Date.now() + Math.max(30, token.expires_in ?? 300) * 1000,
authKey: currentAuthKey(),
};
await LocalStorage.setItem(TOKEN_STORAGE_KEY, JSON.stringify(stored));
return stored.accessToken;
}
export async function fetchWithAuth(
input: string,
init: RequestInit = {},
retry = true,
): Promise<Response> {
const token = await getAccessToken();
const headers = new Headers(init.headers);
headers.set("Authorization", `Bearer ${token}`);
const response = await fetch(input, {
...init,
headers,
});
if ((response.status === 401 || response.status === 403) && retry) {
await clearAccessToken();
const freshToken = await refreshAccessToken();
const retryHeaders = new Headers(init.headers);
retryHeaders.set("Authorization", `Bearer ${freshToken}`);
return fetch(input, {
...init,
headers: retryHeaders,
});
}
return response;
}