@app-vitals/agent-admin 1.107.5 → 1.108.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/openapi.platform.json +121 -0
- package/package.json +2 -2
- package/src/account-repo-lookup-api.integration.test.ts +163 -0
- package/src/account-repo-lookup-api.ts +106 -0
- package/src/account-repo-lookup.ts +89 -0
- package/src/admin-boot.ts +7 -0
- package/src/admin-spec.ts +6 -0
- package/src/openapi-schemas.ts +22 -0
- package/src/platform-routes.ts +12 -0
- package/src/platform-routes.unit.test.ts +2 -0
package/openapi.platform.json
CHANGED
|
@@ -4733,6 +4733,67 @@
|
|
|
4733
4733
|
"required": [
|
|
4734
4734
|
"ids"
|
|
4735
4735
|
]
|
|
4736
|
+
},
|
|
4737
|
+
"AccountRepoLookupResponse": {
|
|
4738
|
+
"type": "object",
|
|
4739
|
+
"properties": {
|
|
4740
|
+
"repos": {
|
|
4741
|
+
"type": "array",
|
|
4742
|
+
"items": {
|
|
4743
|
+
"type": "object",
|
|
4744
|
+
"properties": {
|
|
4745
|
+
"repo": {
|
|
4746
|
+
"type": "string",
|
|
4747
|
+
"example": "acme/widgets"
|
|
4748
|
+
},
|
|
4749
|
+
"accountIds": {
|
|
4750
|
+
"type": "array",
|
|
4751
|
+
"items": {
|
|
4752
|
+
"type": "string"
|
|
4753
|
+
},
|
|
4754
|
+
"example": [
|
|
4755
|
+
"acct-a"
|
|
4756
|
+
]
|
|
4757
|
+
}
|
|
4758
|
+
},
|
|
4759
|
+
"required": [
|
|
4760
|
+
"repo",
|
|
4761
|
+
"accountIds"
|
|
4762
|
+
]
|
|
4763
|
+
}
|
|
4764
|
+
},
|
|
4765
|
+
"singleAccount": {
|
|
4766
|
+
"type": "boolean"
|
|
4767
|
+
},
|
|
4768
|
+
"singleAccountId": {
|
|
4769
|
+
"type": [
|
|
4770
|
+
"string",
|
|
4771
|
+
"null"
|
|
4772
|
+
],
|
|
4773
|
+
"description": "Set only when singleAccount is true"
|
|
4774
|
+
}
|
|
4775
|
+
},
|
|
4776
|
+
"required": [
|
|
4777
|
+
"repos",
|
|
4778
|
+
"singleAccount",
|
|
4779
|
+
"singleAccountId"
|
|
4780
|
+
]
|
|
4781
|
+
},
|
|
4782
|
+
"AccountRepoLookupBody": {
|
|
4783
|
+
"type": "object",
|
|
4784
|
+
"properties": {
|
|
4785
|
+
"repos": {
|
|
4786
|
+
"type": "array",
|
|
4787
|
+
"items": {
|
|
4788
|
+
"type": "string",
|
|
4789
|
+
"minLength": 1
|
|
4790
|
+
},
|
|
4791
|
+
"maxItems": 500
|
|
4792
|
+
}
|
|
4793
|
+
},
|
|
4794
|
+
"required": [
|
|
4795
|
+
"repos"
|
|
4796
|
+
]
|
|
4736
4797
|
}
|
|
4737
4798
|
},
|
|
4738
4799
|
"parameters": {}
|
|
@@ -9600,6 +9661,66 @@
|
|
|
9600
9661
|
}
|
|
9601
9662
|
}
|
|
9602
9663
|
}
|
|
9664
|
+
},
|
|
9665
|
+
"/account-lookup/by-repos": {
|
|
9666
|
+
"post": {
|
|
9667
|
+
"tags": [
|
|
9668
|
+
"accounts"
|
|
9669
|
+
],
|
|
9670
|
+
"summary": "Resolve repos to the accounts of the agents that list them",
|
|
9671
|
+
"description": "Admin-only. For each repo, the distinct accounts of the agents that list it, matched case-insensitively. An agent with no account counts as the default account. Also reports whether the deployment has exactly one account and its id.",
|
|
9672
|
+
"requestBody": {
|
|
9673
|
+
"content": {
|
|
9674
|
+
"application/json": {
|
|
9675
|
+
"schema": {
|
|
9676
|
+
"$ref": "#/components/schemas/AccountRepoLookupBody"
|
|
9677
|
+
}
|
|
9678
|
+
}
|
|
9679
|
+
}
|
|
9680
|
+
},
|
|
9681
|
+
"responses": {
|
|
9682
|
+
"200": {
|
|
9683
|
+
"description": "Accounts per repo and the single-account report",
|
|
9684
|
+
"content": {
|
|
9685
|
+
"application/json": {
|
|
9686
|
+
"schema": {
|
|
9687
|
+
"$ref": "#/components/schemas/AccountRepoLookupResponse"
|
|
9688
|
+
}
|
|
9689
|
+
}
|
|
9690
|
+
}
|
|
9691
|
+
},
|
|
9692
|
+
"400": {
|
|
9693
|
+
"description": "Bad request",
|
|
9694
|
+
"content": {
|
|
9695
|
+
"application/json": {
|
|
9696
|
+
"schema": {
|
|
9697
|
+
"$ref": "#/components/schemas/Error"
|
|
9698
|
+
}
|
|
9699
|
+
}
|
|
9700
|
+
}
|
|
9701
|
+
},
|
|
9702
|
+
"401": {
|
|
9703
|
+
"description": "Unauthorized",
|
|
9704
|
+
"content": {
|
|
9705
|
+
"application/json": {
|
|
9706
|
+
"schema": {
|
|
9707
|
+
"$ref": "#/components/schemas/Error"
|
|
9708
|
+
}
|
|
9709
|
+
}
|
|
9710
|
+
}
|
|
9711
|
+
},
|
|
9712
|
+
"403": {
|
|
9713
|
+
"description": "Forbidden (non-admin caller)",
|
|
9714
|
+
"content": {
|
|
9715
|
+
"application/json": {
|
|
9716
|
+
"schema": {
|
|
9717
|
+
"$ref": "#/components/schemas/Error"
|
|
9718
|
+
}
|
|
9719
|
+
}
|
|
9720
|
+
}
|
|
9721
|
+
}
|
|
9722
|
+
}
|
|
9723
|
+
}
|
|
9603
9724
|
}
|
|
9604
9725
|
},
|
|
9605
9726
|
"webhooks": {}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@app-vitals/agent-admin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.108.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/app-vitals/agent.git",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@anthropic-ai/sdk": "0.137.0",
|
|
47
|
-
"@app-vitals/agent-lib": "1.
|
|
47
|
+
"@app-vitals/agent-lib": "1.108.0",
|
|
48
48
|
"@hono/zod-openapi": "^1.0.0",
|
|
49
49
|
"@prisma/adapter-pg": "^7.10.0",
|
|
50
50
|
"@prisma/client": "^7.10.0",
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/account-repo-lookup-api.integration.test.ts
|
|
3
|
+
* POST /account-lookup/by-repos against real Postgres (MA-2.5).
|
|
4
|
+
*
|
|
5
|
+
* Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
9
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
10
|
+
import { createAccountRepoLookupApp } from "./account-repo-lookup-api.ts";
|
|
11
|
+
import { AccountRepoLookupService } from "./account-repo-lookup.ts";
|
|
12
|
+
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
13
|
+
|
|
14
|
+
const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
|
|
15
|
+
const describeOrSkip = TEST_DB ? describe : describe.skip;
|
|
16
|
+
|
|
17
|
+
describeOrSkip("/account-lookup/by-repos API (integration)", () => {
|
|
18
|
+
let prisma: PrismaClient;
|
|
19
|
+
let app: ReturnType<typeof createAccountRepoLookupApp>;
|
|
20
|
+
const headers = {
|
|
21
|
+
Authorization: "Bearer k",
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const lookup = async (repos: string[]) => {
|
|
26
|
+
const res = await app.request("/account-lookup/by-repos", {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers,
|
|
29
|
+
body: JSON.stringify({ repos }),
|
|
30
|
+
});
|
|
31
|
+
expect(res.status).toBe(200);
|
|
32
|
+
return (await res.json()) as {
|
|
33
|
+
repos: { repo: string; accountIds: string[] }[];
|
|
34
|
+
singleAccount: boolean;
|
|
35
|
+
singleAccountId: string | null;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const account = (name: string) =>
|
|
40
|
+
prisma.account.create({ data: { name } }).then((a) => a.id);
|
|
41
|
+
|
|
42
|
+
beforeEach(async () => {
|
|
43
|
+
prisma = createAdminPrismaClient(TEST_DB as string);
|
|
44
|
+
await prisma.agentMember.deleteMany();
|
|
45
|
+
await prisma.accountMember.deleteMany();
|
|
46
|
+
await prisma.agent.deleteMany();
|
|
47
|
+
await prisma.account.deleteMany();
|
|
48
|
+
app = createAccountRepoLookupApp({
|
|
49
|
+
lookup: new AccountRepoLookupService(prisma),
|
|
50
|
+
sessionSecret: "s".repeat(32),
|
|
51
|
+
adminApiKeys: new Map([
|
|
52
|
+
["k", { name: "t", scope: "*" }],
|
|
53
|
+
["scoped", { name: "s", scope: "agent-1" }],
|
|
54
|
+
]),
|
|
55
|
+
agentTokenService: { validate: async () => null } as never,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
afterEach(async () => {
|
|
60
|
+
await prisma.$disconnect();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("returns zero, one and several accounts per repo", async () => {
|
|
64
|
+
const a = await account("A");
|
|
65
|
+
const b = await account("B");
|
|
66
|
+
await prisma.agent.create({
|
|
67
|
+
data: { name: "x", accountId: a, repos: ["acme/widgets", "acme/one"] },
|
|
68
|
+
});
|
|
69
|
+
await prisma.agent.create({
|
|
70
|
+
data: { name: "y", accountId: b, repos: ["acme/widgets"] },
|
|
71
|
+
});
|
|
72
|
+
await prisma.agent.create({
|
|
73
|
+
data: { name: "z", accountId: a, repos: ["acme/widgets"] },
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const body = await lookup(["acme/widgets", "acme/one", "acme/none"]);
|
|
77
|
+
expect(body.repos).toEqual([
|
|
78
|
+
{ repo: "acme/widgets", accountIds: [a, b].sort() },
|
|
79
|
+
{ repo: "acme/one", accountIds: [a] },
|
|
80
|
+
{ repo: "acme/none", accountIds: [] },
|
|
81
|
+
]);
|
|
82
|
+
expect(body.singleAccount).toBe(false);
|
|
83
|
+
expect(body.singleAccountId).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("ignores deleted agents", async () => {
|
|
87
|
+
const a = await account("A");
|
|
88
|
+
const b = await account("B");
|
|
89
|
+
await prisma.agent.create({
|
|
90
|
+
data: { name: "x", accountId: a, repos: ["acme/widgets"] },
|
|
91
|
+
});
|
|
92
|
+
const gone = await prisma.agent.create({
|
|
93
|
+
data: { name: "y", accountId: b, repos: ["acme/widgets"] },
|
|
94
|
+
});
|
|
95
|
+
await prisma.agent.delete({ where: { id: gone.id } });
|
|
96
|
+
|
|
97
|
+
const body = await lookup(["acme/widgets"]);
|
|
98
|
+
expect(body.repos).toEqual([{ repo: "acme/widgets", accountIds: [a] }]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("matches case-insensitively in both directions", async () => {
|
|
102
|
+
const a = await account("A");
|
|
103
|
+
await prisma.agent.create({
|
|
104
|
+
data: { name: "x", accountId: a, repos: ["Acme/Widgets"] },
|
|
105
|
+
});
|
|
106
|
+
const body = await lookup(["acme/WIDGETS"]);
|
|
107
|
+
expect(body.repos).toEqual([{ repo: "acme/WIDGETS", accountIds: [a] }]);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("counts an agent with no account as the default (oldest) account", async () => {
|
|
111
|
+
const a = await account("A");
|
|
112
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
113
|
+
await account("B");
|
|
114
|
+
await prisma.agent.create({
|
|
115
|
+
data: { name: "x", repos: ["acme/widgets"] },
|
|
116
|
+
});
|
|
117
|
+
const body = await lookup(["acme/widgets"]);
|
|
118
|
+
expect(body.repos).toEqual([{ repo: "acme/widgets", accountIds: [a] }]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("reports the single-account case and resolves no-account agents to it", async () => {
|
|
122
|
+
const a = await account("A");
|
|
123
|
+
await prisma.agent.create({
|
|
124
|
+
data: { name: "x", repos: ["acme/widgets"] },
|
|
125
|
+
});
|
|
126
|
+
const body = await lookup(["acme/widgets"]);
|
|
127
|
+
expect(body.singleAccount).toBe(true);
|
|
128
|
+
expect(body.singleAccountId).toBe(a);
|
|
129
|
+
expect(body.repos).toEqual([{ repo: "acme/widgets", accountIds: [a] }]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("reports no single account when the deployment has no accounts", async () => {
|
|
133
|
+
expect(await lookup([])).toEqual({
|
|
134
|
+
repos: [],
|
|
135
|
+
singleAccount: false,
|
|
136
|
+
singleAccountId: null,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("refuses unauthenticated (401) and non-admin (403) callers, and validates the body", async () => {
|
|
141
|
+
const init = (h: Record<string, string>) => ({
|
|
142
|
+
method: "POST",
|
|
143
|
+
headers: h,
|
|
144
|
+
body: JSON.stringify({ repos: ["acme/widgets"] }),
|
|
145
|
+
});
|
|
146
|
+
const anon = await app.request(
|
|
147
|
+
"/account-lookup/by-repos",
|
|
148
|
+
init({ "Content-Type": "application/json" }),
|
|
149
|
+
);
|
|
150
|
+
expect(anon.status).toBe(401);
|
|
151
|
+
const scoped = await app.request(
|
|
152
|
+
"/account-lookup/by-repos",
|
|
153
|
+
init({ ...headers, Authorization: "Bearer scoped" }),
|
|
154
|
+
);
|
|
155
|
+
expect(scoped.status).toBe(403);
|
|
156
|
+
const bad = await app.request("/account-lookup/by-repos", {
|
|
157
|
+
method: "POST",
|
|
158
|
+
headers,
|
|
159
|
+
body: JSON.stringify({ repos: "nope" }),
|
|
160
|
+
});
|
|
161
|
+
expect(bad.status).toBe(400);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/account-repo-lookup-api.ts
|
|
3
|
+
* Admin-only account lookup by repo (MA-2.5):
|
|
4
|
+
* POST /account-lookup/by-repos {repos} ->
|
|
5
|
+
* {repos:[{repo,accountIds}], singleAccount, singleAccountId}
|
|
6
|
+
* Auth: admin cookie or scope "*" key (service credential); every other
|
|
7
|
+
* caller class gets 403, unauthenticated callers 401.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
|
11
|
+
import { HTTPException } from "hono/http-exception";
|
|
12
|
+
import type { AccountRepoLookupService } from "./account-repo-lookup.ts";
|
|
13
|
+
import type { AgentTokenService } from "./agent-tokens.ts";
|
|
14
|
+
import {
|
|
15
|
+
type AdminApiKey,
|
|
16
|
+
type AdminAuthEnv,
|
|
17
|
+
createAdminAuthMiddleware,
|
|
18
|
+
} from "./api-auth.ts";
|
|
19
|
+
import { ApiError, ForbiddenError } from "./errors.ts";
|
|
20
|
+
import {
|
|
21
|
+
AccountRepoLookupBodySchema,
|
|
22
|
+
AccountRepoLookupResponseSchema,
|
|
23
|
+
ErrorSchema,
|
|
24
|
+
} from "./openapi-schemas.ts";
|
|
25
|
+
|
|
26
|
+
interface AccountRepoLookupApiDeps {
|
|
27
|
+
lookup: Pick<AccountRepoLookupService, "lookupByRepos">;
|
|
28
|
+
sessionSecret: string;
|
|
29
|
+
adminApiKeys?: Map<string, AdminApiKey>;
|
|
30
|
+
agentTokenService: Pick<AgentTokenService, "validate">;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const jsonError = { content: { "application/json": { schema: ErrorSchema } } };
|
|
34
|
+
|
|
35
|
+
const lookupRoute = createRoute({
|
|
36
|
+
method: "post",
|
|
37
|
+
path: "/account-lookup/by-repos",
|
|
38
|
+
tags: ["accounts"],
|
|
39
|
+
summary: "Resolve repos to the accounts of the agents that list them",
|
|
40
|
+
description:
|
|
41
|
+
"Admin-only. For each repo, the distinct accounts of the agents that list it, matched case-insensitively. An agent with no account counts as the default account. Also reports whether the deployment has exactly one account and its id.",
|
|
42
|
+
request: {
|
|
43
|
+
body: {
|
|
44
|
+
content: { "application/json": { schema: AccountRepoLookupBodySchema } },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
responses: {
|
|
48
|
+
200: {
|
|
49
|
+
description: "Accounts per repo and the single-account report",
|
|
50
|
+
content: {
|
|
51
|
+
"application/json": { schema: AccountRepoLookupResponseSchema },
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
400: { description: "Bad request", ...jsonError },
|
|
55
|
+
401: { description: "Unauthorized", ...jsonError },
|
|
56
|
+
403: { description: "Forbidden (non-admin caller)", ...jsonError },
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export function createAccountRepoLookupApp(
|
|
61
|
+
deps: AccountRepoLookupApiDeps,
|
|
62
|
+
): OpenAPIHono<AdminAuthEnv> {
|
|
63
|
+
const app = new OpenAPIHono<AdminAuthEnv>({
|
|
64
|
+
defaultHook: (result, c) => {
|
|
65
|
+
if (!result.success) {
|
|
66
|
+
const message = result.error.issues
|
|
67
|
+
.map((i) => `${i.path.join(".")}: ${i.message}`)
|
|
68
|
+
.join(", ");
|
|
69
|
+
return c.json({ error: message }, 400);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
app.onError((err, c) => {
|
|
75
|
+
if (err instanceof ApiError && err.statusCode < 500) {
|
|
76
|
+
return c.json(
|
|
77
|
+
{ error: err.message },
|
|
78
|
+
err.statusCode as 400 | 401 | 403 | 404 | 409 | 422,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (err instanceof HTTPException && err.status < 500) {
|
|
82
|
+
return c.json({ error: err.message }, err.status as 400 | 415);
|
|
83
|
+
}
|
|
84
|
+
console.error("[account-repo-lookup-api] unhandled error:", err);
|
|
85
|
+
return c.json({ error: "Internal server error" }, 500);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
app.use(
|
|
89
|
+
"/account-lookup/*",
|
|
90
|
+
createAdminAuthMiddleware({
|
|
91
|
+
sessionSecret: deps.sessionSecret,
|
|
92
|
+
agentTokenService: deps.agentTokenService,
|
|
93
|
+
adminApiKeys: deps.adminApiKeys,
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
app.openapi(lookupRoute, async (c) => {
|
|
98
|
+
if (c.get("isAdmin") !== true) {
|
|
99
|
+
throw new ForbiddenError("Admin access required");
|
|
100
|
+
}
|
|
101
|
+
const { repos } = c.req.valid("json");
|
|
102
|
+
return c.json(await deps.lookup.lookupByRepos(repos), 200);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
return app;
|
|
106
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* admin/src/account-repo-lookup.ts
|
|
3
|
+
* AccountRepoLookupService: for each requested repo, the distinct accounts of
|
|
4
|
+
* the agents that list it (case-insensitive), plus whether the deployment has
|
|
5
|
+
* exactly one account.
|
|
6
|
+
*
|
|
7
|
+
* Agents are hard-deleted, so every existing Agent row is a live agent. An
|
|
8
|
+
* agent with no account counts as the default account: the only account when
|
|
9
|
+
* there is exactly one, otherwise the oldest account (createdAt, then id).
|
|
10
|
+
* With no accounts at all, an account-less agent contributes nothing.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { PrismaClient } from "../prisma/client/client.ts";
|
|
14
|
+
|
|
15
|
+
export interface RepoAccountIds {
|
|
16
|
+
repo: string;
|
|
17
|
+
accountIds: string[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AccountRepoLookupResult {
|
|
21
|
+
repos: RepoAccountIds[];
|
|
22
|
+
singleAccount: boolean;
|
|
23
|
+
singleAccountId: string | null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export class AccountRepoLookupService {
|
|
27
|
+
constructor(private prisma: PrismaClient) {}
|
|
28
|
+
|
|
29
|
+
/** Results follow first-seen request order; duplicate repos collapse. */
|
|
30
|
+
async lookupByRepos(repos: string[]): Promise<AccountRepoLookupResult> {
|
|
31
|
+
const accounts = await this.prisma.account.findMany({
|
|
32
|
+
select: { id: true },
|
|
33
|
+
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
|
|
34
|
+
});
|
|
35
|
+
const singleAccount = accounts.length === 1;
|
|
36
|
+
const singleAccountId = singleAccount ? (accounts[0]?.id ?? null) : null;
|
|
37
|
+
const defaultAccountId = accounts[0]?.id ?? null;
|
|
38
|
+
|
|
39
|
+
const unique: string[] = [];
|
|
40
|
+
const seen = new Set<string>();
|
|
41
|
+
for (const repo of repos) {
|
|
42
|
+
if (seen.has(repo)) continue;
|
|
43
|
+
seen.add(repo);
|
|
44
|
+
unique.push(repo);
|
|
45
|
+
}
|
|
46
|
+
if (unique.length === 0) {
|
|
47
|
+
return { repos: [], singleAccount, singleAccountId };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const wanted = new Set(unique.map((r) => r.toLowerCase()));
|
|
51
|
+
const agents = await this.prisma.agent.findMany({
|
|
52
|
+
where: { repos: { hasSome: await this.matchingCases(wanted) } },
|
|
53
|
+
select: { accountId: true, repos: true },
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const byRepo = new Map<string, Set<string>>();
|
|
57
|
+
for (const agent of agents) {
|
|
58
|
+
const accountId = agent.accountId ?? defaultAccountId;
|
|
59
|
+
if (accountId === null) continue;
|
|
60
|
+
for (const r of agent.repos) {
|
|
61
|
+
const key = r.toLowerCase();
|
|
62
|
+
if (!wanted.has(key)) continue;
|
|
63
|
+
const set = byRepo.get(key) ?? new Set<string>();
|
|
64
|
+
set.add(accountId);
|
|
65
|
+
byRepo.set(key, set);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
repos: unique.map((repo) => ({
|
|
71
|
+
repo,
|
|
72
|
+
accountIds: [...(byRepo.get(repo.toLowerCase()) ?? [])].sort(),
|
|
73
|
+
})),
|
|
74
|
+
singleAccount,
|
|
75
|
+
singleAccountId,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Prisma's `hasSome` is case-sensitive, so prefilter with the exact stored
|
|
81
|
+
* spellings found via a case-insensitive raw match.
|
|
82
|
+
*/
|
|
83
|
+
private async matchingCases(wanted: Set<string>): Promise<string[]> {
|
|
84
|
+
const rows = await this.prisma.$queryRaw<{ repo: string }[]>`
|
|
85
|
+
SELECT DISTINCT r AS repo FROM "Agent", unnest("repos") AS r
|
|
86
|
+
WHERE lower(r) = ANY(${[...wanted]}::text[])`;
|
|
87
|
+
return rows.map((row) => row.repo);
|
|
88
|
+
}
|
|
89
|
+
}
|
package/src/admin-boot.ts
CHANGED
|
@@ -127,6 +127,7 @@ import { HttpChatClient } from "./http-chat-client.ts";
|
|
|
127
127
|
import { HttpOktaAuthClient } from "./okta-auth-client.ts";
|
|
128
128
|
import { mountPlatformApis } from "./platform-routes.ts";
|
|
129
129
|
import { parsePodServiceAccountConfig } from "./pod-service-account-config.ts";
|
|
130
|
+
import { AccountRepoLookupService } from "./account-repo-lookup.ts";
|
|
130
131
|
import { PrincipalLookupService } from "./principal-lookup.ts";
|
|
131
132
|
import { createAdminPrismaClient } from "./prisma-client.ts";
|
|
132
133
|
import { isPushEnabled } from "./push-sender.ts";
|
|
@@ -620,6 +621,12 @@ export async function startAdmin(
|
|
|
620
621
|
adminApiKeys,
|
|
621
622
|
agentTokenService,
|
|
622
623
|
},
|
|
624
|
+
accountRepoLookup: {
|
|
625
|
+
lookup: new AccountRepoLookupService(prisma),
|
|
626
|
+
sessionSecret,
|
|
627
|
+
adminApiKeys,
|
|
628
|
+
agentTokenService,
|
|
629
|
+
},
|
|
623
630
|
templates: {
|
|
624
631
|
templateService,
|
|
625
632
|
callerScopeResolver,
|
package/src/admin-spec.ts
CHANGED
|
@@ -130,6 +130,12 @@ export function buildPlatformAdminSpec(): AdminOpenApiDocument {
|
|
|
130
130
|
adminApiKeys,
|
|
131
131
|
agentTokenService: { validate: async () => null },
|
|
132
132
|
},
|
|
133
|
+
accountRepoLookup: {
|
|
134
|
+
lookup: {} as never,
|
|
135
|
+
sessionSecret: SESSION_SECRET,
|
|
136
|
+
adminApiKeys,
|
|
137
|
+
agentTokenService: { validate: async () => null },
|
|
138
|
+
},
|
|
133
139
|
templates: {
|
|
134
140
|
templateService: {} as never,
|
|
135
141
|
callerScopeResolver: async () => ({ kind: "all" }),
|
package/src/openapi-schemas.ts
CHANGED
|
@@ -1630,3 +1630,25 @@ export const PrincipalByEmailQuerySchema = z.object({
|
|
|
1630
1630
|
.min(1)
|
|
1631
1631
|
.openapi({ param: { name: "email", in: "query" }, example: "o@acme.com" }),
|
|
1632
1632
|
});
|
|
1633
|
+
|
|
1634
|
+
// ─── Account lookup by repo (MA-2.5) ──────────────────────────────────────────
|
|
1635
|
+
|
|
1636
|
+
export const AccountRepoLookupBodySchema = z
|
|
1637
|
+
.object({ repos: z.array(z.string().min(1)).max(500) })
|
|
1638
|
+
.openapi("AccountRepoLookupBody");
|
|
1639
|
+
|
|
1640
|
+
export const AccountRepoLookupResponseSchema = z
|
|
1641
|
+
.object({
|
|
1642
|
+
repos: z.array(
|
|
1643
|
+
z.object({
|
|
1644
|
+
repo: z.string().openapi({ example: "acme/widgets" }),
|
|
1645
|
+
accountIds: z.array(z.string()).openapi({ example: ["acct-a"] }),
|
|
1646
|
+
}),
|
|
1647
|
+
),
|
|
1648
|
+
singleAccount: z.boolean(),
|
|
1649
|
+
singleAccountId: z
|
|
1650
|
+
.string()
|
|
1651
|
+
.nullable()
|
|
1652
|
+
.openapi({ description: "Set only when singleAccount is true" }),
|
|
1653
|
+
})
|
|
1654
|
+
.openapi("AccountRepoLookupResponse");
|
package/src/platform-routes.ts
CHANGED
|
@@ -20,10 +20,12 @@ import { type AdminDeps, createAdminApp } from "./agents-api.ts";
|
|
|
20
20
|
import { createAgentRuntimeApp } from "./api.ts";
|
|
21
21
|
import { createGithubEnterpriseApp } from "./github-enterprise-api.ts";
|
|
22
22
|
import { mountPlatformDoc } from "./platform-doc-route.ts";
|
|
23
|
+
import { createAccountRepoLookupApp } from "./account-repo-lookup-api.ts";
|
|
23
24
|
import { createPrincipalsApp } from "./principals-api.ts";
|
|
24
25
|
|
|
25
26
|
type AgentRuntimeDeps = Parameters<typeof createAgentRuntimeApp>[0];
|
|
26
27
|
type AccountsDeps = Parameters<typeof createAccountsApp>[0];
|
|
28
|
+
type AccountRepoLookupDeps = Parameters<typeof createAccountRepoLookupApp>[0];
|
|
27
29
|
type PrincipalsDeps = Parameters<typeof createPrincipalsApp>[0];
|
|
28
30
|
type TemplatesDeps = Parameters<typeof createAgentTemplatesApp>[0];
|
|
29
31
|
type GithubEnterpriseDeps = Parameters<typeof createGithubEnterpriseApp>[0];
|
|
@@ -38,6 +40,8 @@ export interface PlatformApiDeps {
|
|
|
38
40
|
};
|
|
39
41
|
accounts: AccountsDeps;
|
|
40
42
|
principals: PrincipalsDeps;
|
|
43
|
+
/** Optional so existing callers need not change (additive). */
|
|
44
|
+
accountRepoLookup?: AccountRepoLookupDeps;
|
|
41
45
|
templates: TemplatesDeps;
|
|
42
46
|
githubEnterprise: GithubEnterpriseDeps;
|
|
43
47
|
}
|
|
@@ -124,6 +128,14 @@ export function mountPlatformApis(
|
|
|
124
128
|
app: createPrincipalsApp(deps.principals),
|
|
125
129
|
inSpec: true,
|
|
126
130
|
});
|
|
131
|
+
if (deps.accountRepoLookup) {
|
|
132
|
+
mount({
|
|
133
|
+
name: "account-repo-lookup",
|
|
134
|
+
prefix: "/",
|
|
135
|
+
app: createAccountRepoLookupApp(deps.accountRepoLookup),
|
|
136
|
+
inSpec: true,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
127
139
|
|
|
128
140
|
return {
|
|
129
141
|
mounted,
|
|
@@ -24,6 +24,7 @@ function mountWithStubs() {
|
|
|
24
24
|
adminApi: { ...common, provisioner: undefined },
|
|
25
25
|
accounts: { accountService: {}, accountLifecycle: {}, ...common },
|
|
26
26
|
principals: { lookup: {}, ...common },
|
|
27
|
+
accountRepoLookup: { lookup: {}, ...common },
|
|
27
28
|
templates: {
|
|
28
29
|
templateService: {},
|
|
29
30
|
callerScopeResolver: async () => ({ kind: "all" }),
|
|
@@ -55,6 +56,7 @@ describe("mountPlatformApis", () => {
|
|
|
55
56
|
"github-enterprise",
|
|
56
57
|
"templates",
|
|
57
58
|
"principals",
|
|
59
|
+
"account-repo-lookup",
|
|
58
60
|
]);
|
|
59
61
|
});
|
|
60
62
|
|