Skip to content

Commit

Permalink
feat: add credentials api service to admin ui (#1074)
Browse files Browse the repository at this point in the history
- adds api routes + credential api service
- apis and services are namespaced to allow for reused code between agents, workflows, and threads
  • Loading branch information
ryanhopperlowe authored Dec 30, 2024
1 parent 9674669 commit a39f77d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ui/admin/app/lib/model/credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const CredentialNamespace = {
Threads: "threads",
Agents: "agents",
Workflows: "workflows",
} as const;
export type CredentialNamespace =
(typeof CredentialNamespace)[keyof typeof CredentialNamespace];

export type Credential = {
contextID: string;
name: string;
envVars: string[];
expiresAt?: string; // date
};
2 changes: 2 additions & 0 deletions ui/admin/app/lib/model/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export type EntityMeta<
metadata?: TMetadata;
type?: string;
};

export type EntityList<T> = { items: Nullish<T[]> };
12 changes: 12 additions & 0 deletions ui/admin/app/lib/routers/apiRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import queryString from "query-string";
import { mutate } from "swr";

import { CredentialNamespace } from "~/lib/model/credentials";
import {
KnowledgeFileNamespace,
KnowledgeSourceNamespace,
Expand Down Expand Up @@ -167,6 +168,17 @@ export const ApiRoutes = {
getEnv: (entityId: string) => buildUrl(`/agents/${entityId}/env`),
updateEnv: (entityId: string) => buildUrl(`/agents/${entityId}/env`),
},
credentials: {
getCredentialsForEntity: (
namespace: CredentialNamespace,
entityId: string
) => buildUrl(`/${namespace}/${entityId}/credentials`),
deleteCredential: (
namespace: CredentialNamespace,
entityId: string,
credentialId: string
) => buildUrl(`/${namespace}/${entityId}/credentials/${credentialId}`),
},
threads: {
base: () => buildUrl("/threads"),
getById: (threadId: string) => buildUrl(`/threads/${threadId}`),
Expand Down
48 changes: 48 additions & 0 deletions ui/admin/app/lib/service/api/credentialApiService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Credential, CredentialNamespace } from "~/lib/model/credentials";
import { EntityList } from "~/lib/model/primitives";
import { ApiRoutes } from "~/lib/routers/apiRoutes";
import { request } from "~/lib/service/api/primitives";

async function getCredentials(
namespace: CredentialNamespace,
entityId: string
) {
const { data } = await request<EntityList<Credential>>({
url: ApiRoutes.credentials.getCredentialsForEntity(namespace, entityId)
.url,
});

return data.items ?? [];
}
getCredentials.key = (
namespace: CredentialNamespace,
entityId?: Nullish<string>
) => {
if (!entityId) return null;

return {
url: ApiRoutes.credentials.getCredentialsForEntity(namespace, entityId)
.path,
entityId,
namespace,
};
};

async function deleteCredential(
namespace: CredentialNamespace,
entityId: string,
credentialId: string
) {
await request({
url: ApiRoutes.credentials.deleteCredential(
namespace,
entityId,
credentialId
).url,
});
}

export const CredentialApiService = {
getCredentials,
deleteCredential,
};

0 comments on commit a39f77d

Please sign in to comment.