UNPKG

@tanstack/ai

Version:

Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.

61 lines (60 loc) • 1.93 kB
//#region src/byok/memory-credentials.ts /** * A {@link CredentialStore} in memory, for development and tests. The * credentials are lost when the process stops. */ function memoryCredentialStore() { const values = /* @__PURE__ */ new Map(); let lastRevision = 0; const owner = (scope) => JSON.stringify([scope.tenantId ?? null, scope.userId ?? null]); const write = (scope, id, credential) => { const key = owner(scope); let bucket = values.get(key); if (!bucket) { bucket = /* @__PURE__ */ new Map(); values.set(key, bucket); } const revision = String(++lastRevision); bucket.set(id, { credential: structuredClone(credential), revision }); return revision; }; return { get: ({ scope, id }) => { const stored = values.get(owner(scope))?.get(id); return Promise.resolve(stored ? structuredClone(stored.credential) : null); }, getVersioned: ({ scope, id }) => { const stored = values.get(owner(scope))?.get(id); return Promise.resolve(stored ? structuredClone(stored) : null); }, set: ({ scope, id, credential }) => { write(scope, id, credential); return Promise.resolve(); }, setIf: ({ scope, id, credential, expectedRevision }) => { if ((values.get(owner(scope))?.get(id)?.revision ?? null) !== expectedRevision) return Promise.resolve({ ok: false, reason: "conflict" }); return Promise.resolve({ ok: true, revision: write(scope, id, credential) }); }, delete: ({ scope, id }) => { values.get(owner(scope))?.delete(id); return Promise.resolve(); }, list: (scope) => Promise.resolve([...values.get(owner(scope))?.entries() ?? []].map(([id, { credential }]) => ({ id, type: credential.type, ...credential.type !== "api_key" && credential.expiresAt !== void 0 ? { expiresAt: credential.expiresAt } : {} }))) }; } //#endregion export { memoryCredentialStore }; //# sourceMappingURL=memory-credentials.js.map