@openai/codex-security 0.1.21 → 0.1.23
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/README.md +529 -0
- package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
- package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
- package/_bundled_plugin/references/config-preflight.md +2 -30
- package/_bundled_plugin/references/core-scan.md +1 -1
- package/_bundled_plugin/references/final-report.md +1 -1
- package/_bundled_plugin/references/finding-detail-fields.md +5 -10
- package/_bundled_plugin/references/sarif-adapter.md +1 -1
- package/_bundled_plugin/scripts/config_preflight.py +2 -8
- package/_bundled_plugin/scripts/finalize_scan_contract.py +114 -24
- package/_bundled_plugin/scripts/finding_preview.py +22 -4
- package/_bundled_plugin/scripts/generate_in_scope_files.py +27 -1
- package/_bundled_plugin/scripts/windows_scan_local_files.py +101 -7
- package/_bundled_plugin/scripts/workbench_cli.py +21 -1
- package/_bundled_plugin/scripts/workbench_dashboard.py +115 -0
- package/_bundled_plugin/scripts/workbench_db.py +29 -2
- package/_bundled_plugin/scripts/workbench_finding_index.py +42 -24
- package/_bundled_plugin/scripts/workbench_finding_workflows.py +190 -0
- package/_bundled_plugin/scripts/workbench_findings.py +216 -0
- package/_bundled_plugin/scripts/workbench_saved_results.py +30 -5
- package/_bundled_plugin/scripts/workbench_schema.py +263 -58
- package/_bundled_plugin/scripts/workbench_target.py +22 -3
- package/_bundled_plugin/skills/track-findings/SKILL.md +2 -0
- package/dist/api.d.ts +2 -0
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +115 -27
- package/dist/api.js.map +1 -1
- package/dist/cli.d.ts +4 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +124 -58
- package/dist/cli.js.map +1 -1
- package/dist/cloud-publish.js +5 -1
- package/dist/cloud-publish.js.map +1 -1
- package/dist/contract.d.ts +1 -0
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +9 -0
- package/dist/contract.js.map +1 -1
- package/dist/custom-publish.d.ts +22 -0
- package/dist/custom-publish.d.ts.map +1 -0
- package/dist/custom-publish.js +47 -0
- package/dist/custom-publish.js.map +1 -0
- package/dist/custom-validation-prompt.js +1 -1
- package/dist/custom-validation-prompt.js.map +1 -1
- package/dist/deduplication/checkpointed-review.d.ts +16 -0
- package/dist/deduplication/checkpointed-review.d.ts.map +1 -0
- package/dist/deduplication/checkpointed-review.js +76 -0
- package/dist/deduplication/checkpointed-review.js.map +1 -0
- package/dist/deduplication/codex-review.d.ts +21 -0
- package/dist/deduplication/codex-review.d.ts.map +1 -0
- package/dist/deduplication/codex-review.js +252 -0
- package/dist/deduplication/codex-review.js.map +1 -0
- package/dist/deduplication/deduplication-prompts.d.ts +8 -0
- package/dist/deduplication/deduplication-prompts.d.ts.map +1 -0
- package/dist/deduplication/deduplication-prompts.js +35 -0
- package/dist/deduplication/deduplication-prompts.js.map +1 -0
- package/dist/deduplication/deduplication-reviewer.d.ts +46 -0
- package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -0
- package/dist/deduplication/deduplication-reviewer.js +105 -0
- package/dist/deduplication/deduplication-reviewer.js.map +1 -0
- package/dist/deduplication/deduplication.d.ts +6 -0
- package/dist/deduplication/deduplication.d.ts.map +1 -0
- package/dist/deduplication/deduplication.js +85 -0
- package/dist/deduplication/deduplication.js.map +1 -0
- package/dist/deduplication/scan.d.ts +16 -0
- package/dist/deduplication/scan.d.ts.map +1 -0
- package/dist/deduplication/scan.js +101 -0
- package/dist/deduplication/scan.js.map +1 -0
- package/dist/finding-dedupe-groups.d.ts +7 -0
- package/dist/finding-dedupe-groups.d.ts.map +1 -0
- package/dist/finding-dedupe-groups.js +2 -0
- package/dist/finding-dedupe-groups.js.map +1 -0
- package/dist/finding-retrieval.d.ts +13 -0
- package/dist/finding-retrieval.d.ts.map +1 -0
- package/dist/finding-retrieval.js +2 -0
- package/dist/finding-retrieval.js.map +1 -0
- package/dist/finding-workflow.d.ts +53 -0
- package/dist/finding-workflow.d.ts.map +1 -0
- package/dist/finding-workflow.js +117 -0
- package/dist/finding-workflow.js.map +1 -0
- package/dist/findings-client.d.ts +15 -0
- package/dist/findings-client.d.ts.map +1 -0
- package/dist/findings-client.js +60 -0
- package/dist/findings-client.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/publication.js +28 -40
- package/dist/publication.js.map +1 -1
- package/dist/runtime.d.ts +5 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +113 -12
- package/dist/runtime.js.map +1 -1
- package/dist/saved-scan.d.ts +15 -0
- package/dist/saved-scan.d.ts.map +1 -0
- package/dist/saved-scan.js +68 -0
- package/dist/saved-scan.js.map +1 -0
- package/dist/scan-comparison.d.ts +3 -2
- package/dist/scan-comparison.d.ts.map +1 -1
- package/dist/scan-comparison.js +1 -1
- package/dist/scan-comparison.js.map +1 -1
- package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +276 -0
- package/dist/server/dashboard/app.css +2 -0
- package/dist/server/dashboard/app.js +66 -0
- package/dist/server/dashboard/index.html +18 -0
- package/dist/server/dashboard-types.d.ts +45 -0
- package/dist/server/dashboard-types.d.ts.map +1 -0
- package/dist/server/dashboard-types.js +2 -0
- package/dist/server/dashboard-types.js.map +1 -0
- package/dist/server/dashboard.d.ts +6 -0
- package/dist/server/dashboard.d.ts.map +1 -0
- package/dist/server/dashboard.js +45 -0
- package/dist/server/dashboard.js.map +1 -0
- package/dist/server/embeddings.d.ts +16 -0
- package/dist/server/embeddings.d.ts.map +1 -0
- package/dist/server/embeddings.js +114 -0
- package/dist/server/embeddings.js.map +1 -0
- package/dist/server/errors.d.ts +5 -0
- package/dist/server/errors.d.ts.map +1 -0
- package/dist/server/errors.js +8 -0
- package/dist/server/errors.js.map +1 -0
- package/dist/server/findings-service.d.ts +20 -0
- package/dist/server/findings-service.d.ts.map +1 -0
- package/dist/server/findings-service.js +31 -0
- package/dist/server/findings-service.js.map +1 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +32 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/routes.d.ts +6 -0
- package/dist/server/routes.d.ts.map +1 -0
- package/dist/server/routes.js +86 -0
- package/dist/server/routes.js.map +1 -0
- package/dist/server/server.d.ts +10 -0
- package/dist/server/server.d.ts.map +1 -0
- package/dist/server/server.js +17 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/sqlite-store.d.ts +22 -0
- package/dist/server/sqlite-store.d.ts.map +1 -0
- package/dist/server/sqlite-store.js +82 -0
- package/dist/server/sqlite-store.js.map +1 -0
- package/dist/server/storage.d.ts +32 -0
- package/dist/server/storage.d.ts.map +1 -0
- package/dist/server/storage.js +2 -0
- package/dist/server/storage.js.map +1 -0
- package/dist/server/validation.d.ts +17 -0
- package/dist/server/validation.d.ts.map +1 -0
- package/dist/server/validation.js +53 -0
- package/dist/server/validation.js.map +1 -0
- package/dist/trusted-executable.d.ts.map +1 -1
- package/dist/trusted-executable.js +2 -1
- package/dist/trusted-executable.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +17 -5
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en" data-theme="light">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<meta name="color-scheme" content="light dark" />
|
|
7
|
+
<title>Findings · Codex Security</title>
|
|
8
|
+
<link rel="stylesheet" href="./app.css" />
|
|
9
|
+
<script type="module" src="./app.js"></script>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<div id="root"></div>
|
|
13
|
+
<noscript
|
|
14
|
+
>This read-only dashboard requires JavaScript to refresh findings and
|
|
15
|
+
group data.</noscript
|
|
16
|
+
>
|
|
17
|
+
</body>
|
|
18
|
+
</html>
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Finding } from "../models.js";
|
|
2
|
+
import type { FindingDedupeGroup } from "../finding-dedupe-groups.js";
|
|
3
|
+
export type DashboardView = "findings" | "groups";
|
|
4
|
+
export interface DashboardQuery {
|
|
5
|
+
view: DashboardView;
|
|
6
|
+
limit: number;
|
|
7
|
+
offset: number;
|
|
8
|
+
query: string;
|
|
9
|
+
repository: string;
|
|
10
|
+
sort: "activity" | "newest";
|
|
11
|
+
id?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Stored finding or duplicate group list projection. */
|
|
14
|
+
export interface DashboardItem {
|
|
15
|
+
id: string;
|
|
16
|
+
title: string;
|
|
17
|
+
repositoryIds: string[];
|
|
18
|
+
createdAt: string;
|
|
19
|
+
updatedAt: string;
|
|
20
|
+
memberCount?: number;
|
|
21
|
+
severity?: Finding["severity"]["level"];
|
|
22
|
+
}
|
|
23
|
+
export interface DashboardDetail {
|
|
24
|
+
item: DashboardItem;
|
|
25
|
+
finding?: Finding;
|
|
26
|
+
group?: FindingDedupeGroup;
|
|
27
|
+
groups?: FindingDedupeGroup[];
|
|
28
|
+
}
|
|
29
|
+
export interface DashboardSnapshot {
|
|
30
|
+
overview: {
|
|
31
|
+
findings: number;
|
|
32
|
+
groups: number;
|
|
33
|
+
};
|
|
34
|
+
repositories: {
|
|
35
|
+
id: string;
|
|
36
|
+
label: string;
|
|
37
|
+
}[];
|
|
38
|
+
items: DashboardItem[];
|
|
39
|
+
total: number;
|
|
40
|
+
limit: number;
|
|
41
|
+
offset: number;
|
|
42
|
+
nextOffset: number | null;
|
|
43
|
+
detail: DashboardDetail | null;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=dashboard-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dashboard-types.d.ts","source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AAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,MAAM,EAAE,eAAe,GAAG,IAAI,CAAC;CAChC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dashboard-types.js","sourceRoot":"","sources":["../../src/server/dashboard-types.ts"],"names":[],"mappings":"","sourcesContent":["import type { Finding } from \"../models.js\";\nimport type { FindingDedupeGroup } from \"../finding-dedupe-groups.js\";\n\nexport type DashboardView = \"findings\" | \"groups\";\n\nexport interface DashboardQuery {\n view: DashboardView;\n limit: number;\n offset: number;\n query: string;\n repository: string;\n sort: \"activity\" | \"newest\";\n id?: string;\n}\n\n/** Stored finding or duplicate group list projection. */\nexport interface DashboardItem {\n id: string;\n title: string;\n repositoryIds: string[];\n createdAt: string;\n updatedAt: string;\n memberCount?: number;\n severity?: Finding[\"severity\"][\"level\"];\n}\n\nexport interface DashboardDetail {\n item: DashboardItem;\n finding?: Finding;\n group?: FindingDedupeGroup;\n groups?: FindingDedupeGroup[];\n}\n\nexport interface DashboardSnapshot {\n overview: {\n findings: number;\n groups: number;\n };\n repositories: { id: string; label: string }[];\n items: DashboardItem[];\n total: number;\n limit: number;\n offset: number;\n nextOffset: number | null;\n detail: DashboardDetail | null;\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ServerResponse } from "node:http";
|
|
2
|
+
import type { DashboardQuery } from "./dashboard-types.js";
|
|
3
|
+
/** Serve only bundled UI assets, never a path supplied by a request. */
|
|
4
|
+
export declare function serveDashboard(path: string, response: ServerResponse): Promise<boolean>;
|
|
5
|
+
export declare function dashboardQuery(parameters: URLSearchParams): DashboardQuery;
|
|
6
|
+
//# sourceMappingURL=dashboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dashboard.d.ts","sourceRoot":"","sources":["../../src/server/dashboard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,KAAK,EAAE,cAAc,EAAiB,MAAM,sBAAsB,CAAC;AAU1E,wEAAwE;AACxE,wBAAsB,cAAc,CAClC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,cAAc,GACvB,OAAO,CAAC,OAAO,CAAC,CAoBlB;AAED,wBAAgB,cAAc,CAAC,UAAU,EAAE,eAAe,GAAG,cAAc,CAkB1E"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { FindingsError } from "./errors.js";
|
|
3
|
+
import { pagination } from "./validation.js";
|
|
4
|
+
const assets = new Map([
|
|
5
|
+
["/dashboard/", ["index.html", "text/html; charset=utf-8"]],
|
|
6
|
+
["/dashboard/app.js", ["app.js", "text/javascript; charset=utf-8"]],
|
|
7
|
+
["/dashboard/app.css", ["app.css", "text/css; charset=utf-8"]],
|
|
8
|
+
]);
|
|
9
|
+
/** Serve only bundled UI assets, never a path supplied by a request. */
|
|
10
|
+
export async function serveDashboard(path, response) {
|
|
11
|
+
if (path === "/dashboard") {
|
|
12
|
+
response.writeHead(308, { Location: "dashboard/" });
|
|
13
|
+
response.end();
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
const asset = assets.get(path);
|
|
17
|
+
if (!asset)
|
|
18
|
+
return false;
|
|
19
|
+
const body = await readFile(new URL(`./dashboard/${asset[0]}`, import.meta.url));
|
|
20
|
+
response.writeHead(200, {
|
|
21
|
+
"Content-Type": asset[1],
|
|
22
|
+
"Cache-Control": "no-cache",
|
|
23
|
+
"X-Content-Type-Options": "nosniff",
|
|
24
|
+
"Content-Security-Policy": "default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; base-uri 'none'; frame-ancestors 'none'",
|
|
25
|
+
});
|
|
26
|
+
response.end(body);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
export function dashboardQuery(parameters) {
|
|
30
|
+
const view = parameters.get("view") ?? "findings";
|
|
31
|
+
const sort = parameters.get("sort") ?? "activity";
|
|
32
|
+
if (!["findings", "groups"].includes(view))
|
|
33
|
+
throw new FindingsError("invalid_request", "Unknown dashboard view.");
|
|
34
|
+
if (sort !== "activity" && sort !== "newest")
|
|
35
|
+
throw new FindingsError("invalid_request", "sort must be activity or newest.");
|
|
36
|
+
return {
|
|
37
|
+
view: view,
|
|
38
|
+
...pagination(parameters),
|
|
39
|
+
query: parameters.get("query") ?? "",
|
|
40
|
+
repository: parameters.get("repository") ?? "",
|
|
41
|
+
sort,
|
|
42
|
+
...(parameters.has("id") ? { id: parameters.get("id") } : {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=dashboard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dashboard.js","sourceRoot":"","sources":["../../src/server/dashboard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC;IACrB,CAAC,aAAa,EAAE,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC;IAC3D,CAAC,mBAAmB,EAAE,CAAC,QAAQ,EAAE,gCAAgC,CAAC,CAAC;IACnE,CAAC,oBAAoB,EAAE,CAAC,SAAS,EAAE,yBAAyB,CAAC,CAAC;CAC/D,CAAC,CAAC;AAEH,wEAAwE;AACxE,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAY,EACZ,QAAwB;IAExB,IAAI,IAAI,KAAK,YAAY,EAAE,CAAC;QAC1B,QAAQ,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,YAAY,E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AAE,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,EAAE;QAC9C,IAAI;QACJ,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,UAAU,CAAC,GAAG,CAAC,IAAI,CAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC/D,CAAC;AACJ,CAAC","sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport type { ServerResponse } from \"node:http\";\nimport type { DashboardQuery, DashboardView } from \"./dashboard-types.js\";\nimport { FindingsError } from \"./errors.js\";\nimport { pagination } from \"./validation.js\";\n\nconst assets = new Map([\n [\"/dashboard/\", [\"index.html\", \"text/html; charset=utf-8\"]],\n [\"/dashboard/app.js\", [\"app.js\", \"text/javascript; charset=utf-8\"]],\n [\"/dashboard/app.css\", [\"app.css\", \"text/css; charset=utf-8\"]],\n]);\n\n/** Serve only bundled UI assets, never a path supplied by a request. */\nexport async function serveDashboard(\n path: string,\n response: ServerResponse,\n): Promise<boolean> {\n if (path === \"/dashboard\") {\n response.writeHead(308, { Location: \"dashboard/\" });\n response.end();\n return true;\n }\n const asset = assets.get(path);\n if (!asset) return false;\n const body = await readFile(\n new URL(`./dashboard/${asset[0]}`, import.meta.url),\n );\n response.writeHead(200, {\n \"Content-Type\": asset[1]!,\n \"Cache-Control\": \"no-cache\",\n \"X-Content-Type-Options\": \"nosniff\",\n \"Content-Security-Policy\":\n \"default-src 'none'; script-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; font-src 'self'; base-uri 'none'; frame-ancestors 'none'\",\n });\n response.end(body);\n return true;\n}\n\nexport function dashboardQuery(parameters: URLSearchParams): DashboardQuery {\n const view = parameters.get(\"view\") ?? \"findings\";\n const sort = parameters.get(\"sort\") ?? \"activity\";\n if (![\"findings\", \"groups\"].includes(view))\n throw new FindingsError(\"invalid_request\", \"Unknown dashboard view.\");\n if (sort !== \"activity\" && sort !== \"newest\")\n throw new FindingsError(\n \"invalid_request\",\n \"sort must be activity or newest.\",\n );\n return {\n view: view as DashboardView,\n ...pagination(parameters),\n query: parameters.get(\"query\") ?? \"\",\n repository: parameters.get(\"repository\") ?? \"\",\n sort,\n ...(parameters.has(\"id\") ? { id: parameters.get(\"id\")! } : {}),\n };\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Finding } from "../models.js";
|
|
2
|
+
import type { FindingEmbedding } from "./storage.js";
|
|
3
|
+
export declare const EMBEDDING_MODEL = "text-embedding-3-large";
|
|
4
|
+
export declare const EMBEDDING_DIMENSIONS = 1536;
|
|
5
|
+
export interface FindingEmbedder {
|
|
6
|
+
embed(findings: readonly Finding[]): Promise<FindingEmbedding[]>;
|
|
7
|
+
}
|
|
8
|
+
export declare class OpenAiFindingEmbedder implements FindingEmbedder {
|
|
9
|
+
private readonly apiKey;
|
|
10
|
+
private readonly request;
|
|
11
|
+
private readonly encoding;
|
|
12
|
+
constructor(apiKey: string | undefined, request?: (url: string, init: RequestInit) => Promise<Response>);
|
|
13
|
+
embed(findings: readonly Finding[]): Promise<FindingEmbedding[]>;
|
|
14
|
+
private embedChunks;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=embeddings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embeddings.d.ts","sourceRoot":"","sources":["../../src/server/embeddings.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAE5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAErD,eAAO,MAAM,eAAe,2BAA2B,CAAC;AACxD,eAAO,MAAM,oBAAoB,OAAO,CAAC;AAKzC,MAAM,WAAW,eAAe;IAC9B,KAAK,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC,CAAC;CAClE;AAOD,qBAAa,qBAAsB,YAAW,eAAe;IAIzD,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJ1B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA4B;gBAGlC,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,OAAO,GAAE,CACxB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,WAAW,KACd,OAAO,CAAC,QAAQ,CAAS;IAG1B,KAAK,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,OAAO,CAAC,gBAAgB,EAAE,CAAC;YA2CxD,WAAW;CAyE1B"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { Tiktoken } from "js-tiktoken/lite";
|
|
2
|
+
import cl100kBase from "js-tiktoken/ranks/cl100k_base";
|
|
3
|
+
import { FindingsError } from "./errors.js";
|
|
4
|
+
export const EMBEDDING_MODEL = "text-embedding-3-large";
|
|
5
|
+
export const EMBEDDING_DIMENSIONS = 1536;
|
|
6
|
+
const MAX_INPUT_TOKENS = 8192;
|
|
7
|
+
const MAX_REQUEST_TOKENS = 300_000;
|
|
8
|
+
const MAX_REQUEST_INPUTS = 2048;
|
|
9
|
+
export class OpenAiFindingEmbedder {
|
|
10
|
+
apiKey;
|
|
11
|
+
request;
|
|
12
|
+
encoding = new Tiktoken(cl100kBase);
|
|
13
|
+
constructor(apiKey, request = fetch) {
|
|
14
|
+
this.apiKey = apiKey;
|
|
15
|
+
this.request = request;
|
|
16
|
+
}
|
|
17
|
+
async embed(findings) {
|
|
18
|
+
if (findings.length === 0)
|
|
19
|
+
return [];
|
|
20
|
+
if (!this.apiKey) {
|
|
21
|
+
throw new FindingsError("embedding_unavailable", "Set OPENAI_API_KEY or CODEX_API_KEY to generate embeddings.");
|
|
22
|
+
}
|
|
23
|
+
const vectors = findings.map(() => Array(EMBEDDING_DIMENSIONS).fill(0));
|
|
24
|
+
let chunks = [];
|
|
25
|
+
let requestTokens = 0;
|
|
26
|
+
for (const [findingIndex, finding] of findings.entries()) {
|
|
27
|
+
const tokens = this.encoding.encode(JSON.stringify(finding), [], []);
|
|
28
|
+
for (let start = 0; start < tokens.length; start += MAX_INPUT_TOKENS) {
|
|
29
|
+
const chunk = tokens.slice(start, start + MAX_INPUT_TOKENS);
|
|
30
|
+
if (chunks.length === MAX_REQUEST_INPUTS ||
|
|
31
|
+
requestTokens + chunk.length > MAX_REQUEST_TOKENS) {
|
|
32
|
+
await this.embedChunks(chunks, vectors);
|
|
33
|
+
chunks = [];
|
|
34
|
+
requestTokens = 0;
|
|
35
|
+
}
|
|
36
|
+
chunks.push({ findingIndex, tokens: chunk });
|
|
37
|
+
requestTokens += chunk.length;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (chunks.length > 0)
|
|
41
|
+
await this.embedChunks(chunks, vectors);
|
|
42
|
+
return vectors.map((vector) => {
|
|
43
|
+
const norm = Math.hypot(...vector);
|
|
44
|
+
if (norm === 0 || !Number.isFinite(norm))
|
|
45
|
+
throw invalidEmbeddingResponse();
|
|
46
|
+
return {
|
|
47
|
+
model: EMBEDDING_MODEL,
|
|
48
|
+
vector: vector.map((value) => value / norm),
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async embedChunks(chunks, vectors) {
|
|
53
|
+
let response;
|
|
54
|
+
try {
|
|
55
|
+
response = await this.request("https://api.openai.com/v1/embeddings", {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: {
|
|
58
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
},
|
|
61
|
+
body: JSON.stringify({
|
|
62
|
+
model: EMBEDDING_MODEL,
|
|
63
|
+
dimensions: EMBEDDING_DIMENSIONS,
|
|
64
|
+
encoding_format: "float",
|
|
65
|
+
input: chunks.map(({ tokens }) => tokens),
|
|
66
|
+
}),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new FindingsError("embedding_failed", "Could not reach the embedding provider.");
|
|
71
|
+
}
|
|
72
|
+
if (!response.ok) {
|
|
73
|
+
throw new FindingsError("embedding_failed", `Embedding provider returned HTTP ${response.status}.`);
|
|
74
|
+
}
|
|
75
|
+
let payload;
|
|
76
|
+
try {
|
|
77
|
+
payload = (await response.json());
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw invalidEmbeddingResponse();
|
|
81
|
+
}
|
|
82
|
+
if (payload?.model !== EMBEDDING_MODEL ||
|
|
83
|
+
!Array.isArray(payload.data) ||
|
|
84
|
+
payload.data.length !== chunks.length) {
|
|
85
|
+
throw invalidEmbeddingResponse();
|
|
86
|
+
}
|
|
87
|
+
const seen = new Set();
|
|
88
|
+
for (const item of payload.data) {
|
|
89
|
+
const index = item?.index;
|
|
90
|
+
const embedding = item?.embedding;
|
|
91
|
+
if (typeof index !== "number" ||
|
|
92
|
+
!Number.isInteger(index) ||
|
|
93
|
+
index < 0 ||
|
|
94
|
+
index >= chunks.length ||
|
|
95
|
+
seen.has(index) ||
|
|
96
|
+
!Array.isArray(embedding) ||
|
|
97
|
+
embedding.length !== EMBEDDING_DIMENSIONS ||
|
|
98
|
+
!embedding.every((value) => typeof value === "number" && Number.isFinite(value))) {
|
|
99
|
+
throw invalidEmbeddingResponse();
|
|
100
|
+
}
|
|
101
|
+
seen.add(index);
|
|
102
|
+
const chunk = chunks[index];
|
|
103
|
+
const vector = vectors[chunk.findingIndex];
|
|
104
|
+
for (let dimension = 0; dimension < vector.length; dimension++) {
|
|
105
|
+
vector[dimension] =
|
|
106
|
+
vector[dimension] + embedding[dimension] * chunk.tokens.length;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function invalidEmbeddingResponse() {
|
|
112
|
+
return new FindingsError("embedding_failed", "Embedding provider returned invalid vectors.");
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=embeddings.js.map
|
|
@@ -0,0 +1 @@
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iktoken/ranks/cl100k_base\";\nimport type { Finding } from \"../models.js\";\nimport { FindingsError } from \"./errors.js\";\nimport type { FindingEmbedding } from \"./storage.js\";\n\nexport const EMBEDDING_MODEL = \"text-embedding-3-large\";\nexport const EMBEDDING_DIMENSIONS = 1536;\nconst MAX_INPUT_TOKENS = 8192;\nconst MAX_REQUEST_TOKENS = 300_000;\nconst MAX_REQUEST_INPUTS = 2048;\n\nexport interface FindingEmbedder {\n embed(findings: readonly Finding[]): Promise<FindingEmbedding[]>;\n}\n\ninterface Chunk {\n findingIndex: number;\n tokens: number[];\n}\n\nexport class OpenAiFindingEmbedder implements FindingEmbedder {\n private readonly encoding = new Tiktoken(cl100kBase);\n\n constructor(\n private readonly apiKey: string | undefined,\n private readonly request: (\n url: string,\n init: RequestInit,\n ) => Promise<Response> = fetch,\n ) {}\n\n async embed(findings: readonly Finding[]): Promise<FindingEmbedding[]> {\n if (findings.length === 0) return [];\n if (!this.apiKey) {\n throw new FindingsError(\n \"embedding_unavailable\",\n \"Set OPENAI_API_KEY or CODEX_API_KEY to generate embeddings.\",\n );\n }\n\n const vectors = findings.map(() =>\n Array<number>(EMBEDDING_DIMENSIONS).fill(0),\n );\n let chunks: Chunk[] = [];\n let requestTokens = 0;\n for (const [findingIndex, finding] of findings.entries()) {\n const tokens = this.encoding.encode(JSON.stringify(finding), [], []);\n for (let start = 0; start < tokens.length; start += MAX_INPUT_TOKENS) {\n const chunk = tokens.slice(start, start + MAX_INPUT_TOKENS);\n if (\n chunks.length === MAX_REQUEST_INPUTS ||\n requestTokens + chunk.length > MAX_REQUEST_TOKENS\n ) {\n await this.embedChunks(chunks, vectors);\n chunks = [];\n requestTokens = 0;\n }\n chunks.push({ findingIndex, tokens: chunk });\n requestTokens += chunk.length;\n }\n }\n if (chunks.length > 0) await this.embedChunks(chunks, vectors);\n\n return vectors.map((vector) => {\n const norm = Math.hypot(...vector);\n if (norm === 0 || !Number.isFinite(norm))\n throw invalidEmbeddingResponse();\n return {\n model: EMBEDDING_MODEL,\n vector: vector.map((value) => value / norm),\n };\n });\n }\n\n private async embedChunks(\n chunks: Chunk[],\n vectors: number[][],\n ): Promise<void> {\n let response: Response;\n try {\n response = await this.request(\"https://api.openai.com/v1/embeddings\", {\n method: \"POST\",\n headers: {\n Authorization: `Bearer ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n model: EMBEDDING_MODEL,\n dimensions: EMBEDDING_DIMENSIONS,\n encoding_format: \"float\",\n input: chunks.map(({ tokens }) => tokens),\n }),\n });\n } catch {\n throw new FindingsError(\n \"embedding_failed\",\n \"Could not reach the embedding provider.\",\n );\n }\n if (!response.ok) {\n throw new FindingsError(\n \"embedding_failed\",\n `Embedding provider returned HTTP ${response.status}.`,\n );\n }\n\n let payload: { model?: unknown; data?: unknown } | null;\n try {\n payload = (await response.json()) as typeof payload;\n } catch {\n throw invalidEmbeddingResponse();\n }\n if (\n payload?.model !== EMBEDDING_MODEL ||\n !Array.isArray(payload.data) ||\n payload.data.length !== chunks.length\n ) {\n throw invalidEmbeddingResponse();\n }\n const seen = new Set<number>();\n for (const item of payload.data) {\n const index: unknown = item?.index;\n const embedding: unknown = item?.embedding;\n if (\n typeof index !== \"number\" ||\n !Number.isInteger(index) ||\n index < 0 ||\n index >= chunks.length ||\n seen.has(index) ||\n !Array.isArray(embedding) ||\n embedding.length !== EMBEDDING_DIMENSIONS ||\n !embedding.every(\n (value: unknown) =>\n typeof value === \"number\" && Number.isFinite(value),\n )\n ) {\n throw invalidEmbeddingResponse();\n }\n seen.add(index);\n const chunk = chunks[index]!;\n const vector = vectors[chunk.findingIndex]!;\n for (let dimension = 0; dimension < vector.length; dimension++) {\n vector[dimension] =\n vector[dimension]! + embedding[dimension]! * chunk.tokens.length;\n }\n }\n }\n}\n\nfunction invalidEmbeddingResponse(): FindingsError {\n return new FindingsError(\n \"embedding_failed\",\n \"Embedding provider returned invalid vectors.\",\n );\n}\n"]}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare class FindingsError extends Error {
|
|
2
|
+
readonly code: "invalid_request" | "finding_conflict" | "embedding_unavailable" | "embedding_failed" | "finding_not_indexed";
|
|
3
|
+
constructor(code: "invalid_request" | "finding_conflict" | "embedding_unavailable" | "embedding_failed" | "finding_not_indexed", message: string);
|
|
4
|
+
}
|
|
5
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,aAAc,SAAQ,KAAK;IAEpC,QAAQ,CAAC,IAAI,EACT,iBAAiB,GACjB,kBAAkB,GAClB,uBAAuB,GACvB,kBAAkB,GAClB,qBAAqB;gBALhB,IAAI,EACT,iBAAiB,GACjB,kBAAkB,GAClB,uBAAuB,GACvB,kBAAkB,GAClB,qBAAqB,EACzB,OAAO,EAAE,MAAM;CAIlB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/server/errors.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,aAAc,SAAQ,KAAK;IAE3B;IADX,YACW,IAKgB,EACzB,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QARN,SAAI,GAAJ,IAAI,CAKY;IAI3B,CAAC;CACF","sourcesContent":["export class FindingsError extends Error {\n constructor(\n readonly code:\n | \"invalid_request\"\n | \"finding_conflict\"\n | \"embedding_unavailable\"\n | \"embedding_failed\"\n | \"finding_not_indexed\",\n message: string,\n ) {\n super(message);\n }\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Finding } from "../models.js";
|
|
2
|
+
import type { DashboardQuery } from "./dashboard-types.js";
|
|
3
|
+
import type { FindingSearchScope } from "../finding-retrieval.js";
|
|
4
|
+
import type { FindingEmbedder } from "./embeddings.js";
|
|
5
|
+
import type { FindingsPage, FindingsStore } from "./storage.js";
|
|
6
|
+
export declare class FindingsService {
|
|
7
|
+
private readonly store;
|
|
8
|
+
private readonly embeddings;
|
|
9
|
+
constructor(store: FindingsStore, embeddings: FindingEmbedder);
|
|
10
|
+
insert(findings: readonly Finding[], repositoryId?: string): Promise<string[]>;
|
|
11
|
+
potentialDuplicates(findingId: string, scope: FindingSearchScope): Promise<import("../finding-retrieval.js").FindingNeighborhood>;
|
|
12
|
+
storeDedupeGroups(groups: readonly string[][]): Promise<import("../finding-dedupe-groups.js").FindingDedupeGroup[]>;
|
|
13
|
+
listDedupeGroups(findingId: string): Promise<import("../finding-dedupe-groups.js").FindingDedupeGroup[]>;
|
|
14
|
+
list(page: {
|
|
15
|
+
limit: number;
|
|
16
|
+
offset: number;
|
|
17
|
+
}): Promise<FindingsPage>;
|
|
18
|
+
dashboard(query: DashboardQuery): Promise<import("./dashboard-types.js").DashboardSnapshot>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=findings-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findings-service.d.ts","sourceRoot":"","sources":["../../src/server/findings-service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEhE,qBAAa,eAAe;IAExB,OAAO,CAAC,QAAQ,CAAC,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,UAAU;gBADV,KAAK,EAAE,aAAa,EACpB,UAAU,EAAE,eAAe;IAGxC,MAAM,CACV,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,YAAY,CAAC,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,EAAE,CAAC;IAWd,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,kBAAkB;IAIhE,iBAAiB,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,EAAE;IAI7C,gBAAgB,CAAC,SAAS,EAAE,MAAM;IAIlC,IAAI,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,YAAY,CAAC;IAIpE,SAAS,CAAC,KAAK,EAAE,cAAc;CAGtC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class FindingsService {
|
|
2
|
+
store;
|
|
3
|
+
embeddings;
|
|
4
|
+
constructor(store, embeddings) {
|
|
5
|
+
this.store = store;
|
|
6
|
+
this.embeddings = embeddings;
|
|
7
|
+
}
|
|
8
|
+
async insert(findings, repositoryId) {
|
|
9
|
+
const embeddings = await this.embeddings.embed(findings);
|
|
10
|
+
return await this.store.insert(findings.map((finding, index) => ({
|
|
11
|
+
finding,
|
|
12
|
+
embedding: embeddings[index],
|
|
13
|
+
})), repositoryId);
|
|
14
|
+
}
|
|
15
|
+
async potentialDuplicates(findingId, scope) {
|
|
16
|
+
return await this.store.findPotentialDuplicates(findingId, scope);
|
|
17
|
+
}
|
|
18
|
+
async storeDedupeGroups(groups) {
|
|
19
|
+
return await this.store.storeDedupeGroups(groups);
|
|
20
|
+
}
|
|
21
|
+
async listDedupeGroups(findingId) {
|
|
22
|
+
return await this.store.listDedupeGroups(findingId);
|
|
23
|
+
}
|
|
24
|
+
async list(page) {
|
|
25
|
+
return await this.store.list(page);
|
|
26
|
+
}
|
|
27
|
+
async dashboard(query) {
|
|
28
|
+
return await this.store.dashboard(query);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=findings-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"findings-service.js","sourceRoot":"","sources":["../../src/server/findings-service.ts"],"names":[],"mappings":"AAMA,MAAM,OAAO,eAAe;IAEP;IACA;IAFnB,YACmB,KAAoB,EACpB,UAA2B;QAD3B,UAAK,GAAL,KAAK,CAAe;QACpB,eAAU,GAAV,UAAU,CAAiB;IAC3C,CAAC;IAEJ,KAAK,CAAC,MAAM,CACV,QAA4B,EAC5B,YAAqB;QAErB,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACzD,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,CAC5B,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YAChC,OAAO;YACP,SAAS,EAAE,UAAU,CAAC,KAAK,CAAE;SAC9B,CAAC,CAAC,EACH,YAAY,CACb,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,mBAAmB,CAAC,SAAiB,EAAE,KAAyB;QACpE,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,uBAAuB,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;IACpE,CAAC;IAED,KAAK,CAAC,iBAAiB,CAAC,MAA2B;QACjD,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IACpD,CAAC;IAED,KAAK,CAAC,gBAAgB,CAAC,SAAiB;QACtC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,IAAuC;QAChD,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,KAAqB;QACnC,OAAO,MAAM,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;CACF","sourcesContent":["import type { Finding } from \"../models.js\";\nimport type { DashboardQuery } from \"./dashboard-types.js\";\nimport type { FindingSearchScope } from \"../finding-retrieval.js\";\nimport type { FindingEmbedder } from \"./embeddings.js\";\nimport type { FindingsPage, FindingsStore } from \"./storage.js\";\n\nexport class FindingsService {\n constructor(\n private readonly store: FindingsStore,\n private readonly embeddings: FindingEmbedder,\n ) {}\n\n async insert(\n findings: readonly Finding[],\n repositoryId?: string,\n ): Promise<string[]> {\n const embeddings = await this.embeddings.embed(findings);\n return await this.store.insert(\n findings.map((finding, index) => ({\n finding,\n embedding: embeddings[index]!,\n })),\n repositoryId,\n );\n }\n\n async potentialDuplicates(findingId: string, scope: FindingSearchScope) {\n return await this.store.findPotentialDuplicates(findingId, scope);\n }\n\n async storeDedupeGroups(groups: readonly string[][]) {\n return await this.store.storeDedupeGroups(groups);\n }\n\n async listDedupeGroups(findingId: string) {\n return await this.store.listDedupeGroups(findingId);\n }\n\n async list(page: { limit: number; offset: number }): Promise<FindingsPage> {\n return await this.store.list(page);\n }\n\n async dashboard(query: DashboardQuery) {\n return await this.store.dashboard(query);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { startFindingsServer } from "./server.js";
|
|
2
|
+
import { SqliteFindingsStore } from "./sqlite-store.js";
|
|
3
|
+
import { OpenAiFindingEmbedder } from "./embeddings.js";
|
|
4
|
+
async function main() {
|
|
5
|
+
const host = process.env["HOST"] ?? "127.0.0.1";
|
|
6
|
+
const port = Number(process.env["PORT"] ?? 3000);
|
|
7
|
+
const server = await startFindingsServer({
|
|
8
|
+
store: new SqliteFindingsStore(),
|
|
9
|
+
embeddings: new OpenAiFindingEmbedder(process.env["OPENAI_API_KEY"] ?? process.env["CODEX_API_KEY"]),
|
|
10
|
+
host,
|
|
11
|
+
port,
|
|
12
|
+
});
|
|
13
|
+
const address = server.address();
|
|
14
|
+
if (address !== null && typeof address !== "string") {
|
|
15
|
+
console.log(`Findings service listening on ${address.address}:${address.port}`);
|
|
16
|
+
}
|
|
17
|
+
const shutdown = () => {
|
|
18
|
+
server.close((error) => {
|
|
19
|
+
if (error !== undefined) {
|
|
20
|
+
console.error(error);
|
|
21
|
+
process.exitCode = 1;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
process.once("SIGINT", shutdown);
|
|
26
|
+
process.once("SIGTERM", shutdown);
|
|
27
|
+
}
|
|
28
|
+
main().catch((error) => {
|
|
29
|
+
console.error(error);
|
|
30
|
+
process.exitCode = 1;
|
|
31
|
+
});
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAExD,KAAK,UAAU,IAAI;IACjB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC;IAChD,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC;IACjD,MAAM,MAAM,GAAG,MAAM,mBAAmB,CAAC;QACvC,KAAK,EAAE,IAAI,mBAAmB,EAAE;QAChC,UAAU,EAAE,IAAI,qBAAqB,CACnC,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAC9D;QACD,IAAI;QACJ,IAAI;KACL,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,CAAC;IACjC,IAAI,OAAO,KAAK,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QACpD,OAAO,CAAC,GAAG,CACT,iCAAiC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,IAAI,EAAE,CACnE,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACrB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACrB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;YACvB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AACpC,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC9B,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACrB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC","sourcesContent":["import { startFindingsServer } from \"./server.js\";\nimport { SqliteFindingsStore } from \"./sqlite-store.js\";\nimport { OpenAiFindingEmbedder } from \"./embeddings.js\";\n\nasync function main(): Promise<void> {\n const host = process.env[\"HOST\"] ?? \"127.0.0.1\";\n const port = Number(process.env[\"PORT\"] ?? 3000);\n const server = await startFindingsServer({\n store: new SqliteFindingsStore(),\n embeddings: new OpenAiFindingEmbedder(\n process.env[\"OPENAI_API_KEY\"] ?? process.env[\"CODEX_API_KEY\"],\n ),\n host,\n port,\n });\n const address = server.address();\n if (address !== null && typeof address !== \"string\") {\n console.log(\n `Findings service listening on ${address.address}:${address.port}`,\n );\n }\n\n const shutdown = () => {\n server.close((error) => {\n if (error !== undefined) {\n console.error(error);\n process.exitCode = 1;\n }\n });\n };\n process.once(\"SIGINT\", shutdown);\n process.once(\"SIGTERM\", shutdown);\n}\n\nmain().catch((error: unknown) => {\n console.error(error);\n process.exitCode = 1;\n});\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { ValidateFunction } from "ajv";
|
|
3
|
+
import type { FindingsService } from "./findings-service.js";
|
|
4
|
+
import { type FindingsRequest } from "./validation.js";
|
|
5
|
+
export declare function handleFindingsRequest(request: IncomingMessage, response: ServerResponse, service: FindingsService, validate: ValidateFunction<FindingsRequest>): Promise<void>;
|
|
6
|
+
//# sourceMappingURL=routes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/server/routes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACjE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,KAAK,CAAC;AAG5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAIL,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAC;AAEzB,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,cAAc,EACxB,OAAO,EAAE,eAAe,EACxB,QAAQ,EAAE,gBAAgB,CAAC,eAAe,CAAC,GAC1C,OAAO,CAAC,IAAI,CAAC,CA2Ff"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { FindingsError } from "./errors.js";
|
|
2
|
+
import { dashboardQuery, serveDashboard } from "./dashboard.js";
|
|
3
|
+
import { findingSearchScope, pagination, validateDedupeGroups, } from "./validation.js";
|
|
4
|
+
export async function handleFindingsRequest(request, response, service, validate) {
|
|
5
|
+
try {
|
|
6
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
7
|
+
const route = `${request.method} ${url.pathname}`;
|
|
8
|
+
if (request.method === "GET" &&
|
|
9
|
+
(await serveDashboard(url.pathname, response)))
|
|
10
|
+
return;
|
|
11
|
+
if (route === "GET /v1/dashboard") {
|
|
12
|
+
response.setHeader("Cache-Control", "no-store");
|
|
13
|
+
json(response, 200, await service.dashboard(dashboardQuery(url.searchParams)));
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (route === "GET /v1/findings") {
|
|
17
|
+
console.log(route);
|
|
18
|
+
json(response, 200, await service.list(pagination(url.searchParams)));
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const candidates = /^\/v1\/finding\/([^/]+)\/potential-duplicates$/.exec(url.pathname);
|
|
22
|
+
if (request.method === "GET" && candidates) {
|
|
23
|
+
console.log("GET /v1/finding/:id/potential-duplicates");
|
|
24
|
+
json(response, 200, await service.potentialDuplicates(candidates[1], findingSearchScope(url.searchParams)));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const dedupeGroups = /^\/v1\/finding\/([^/]+)\/dedupe-groups$/.exec(url.pathname);
|
|
28
|
+
if (request.method === "GET" && dedupeGroups) {
|
|
29
|
+
console.log("GET /v1/finding/:id/dedupe-groups");
|
|
30
|
+
json(response, 200, await service.listDedupeGroups(dedupeGroups[1]));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (route === "POST /v1/dedupe-groups") {
|
|
34
|
+
console.log(route);
|
|
35
|
+
const input = await readJson(request);
|
|
36
|
+
if (!validateDedupeGroups(input)) {
|
|
37
|
+
throw new FindingsError("invalid_request", "Expected {groups: [[findingId, ...], ...]} with at least two distinct finding IDs per group.");
|
|
38
|
+
}
|
|
39
|
+
json(response, 201, await service.storeDedupeGroups(input.groups));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (route === "POST /v1/bulk/findings") {
|
|
43
|
+
console.log(route);
|
|
44
|
+
const input = await readJson(request);
|
|
45
|
+
if (!validate(input)) {
|
|
46
|
+
throw new FindingsError("invalid_request", "Expected {findings: [...]} with an optional nonempty repositoryId, using the existing Finding schema.");
|
|
47
|
+
}
|
|
48
|
+
json(response, 201, await service.insert(input.findings, input.repositoryId));
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
request.resume();
|
|
52
|
+
json(response, 404, { error: "not_found" });
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
if (error instanceof FindingsError) {
|
|
56
|
+
const status = {
|
|
57
|
+
invalid_request: 400,
|
|
58
|
+
finding_conflict: 409,
|
|
59
|
+
embedding_unavailable: 503,
|
|
60
|
+
embedding_failed: 502,
|
|
61
|
+
finding_not_indexed: 404,
|
|
62
|
+
}[error.code];
|
|
63
|
+
json(response, status, { error: error.code, message: error.message });
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
console.error(error);
|
|
67
|
+
json(response, 500, { error: "internal_error" });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function readJson(request) {
|
|
72
|
+
const chunks = [];
|
|
73
|
+
for await (const chunk of request)
|
|
74
|
+
chunks.push(Buffer.from(chunk));
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
throw new FindingsError("invalid_request", "Request body must be valid JSON.");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function json(response, status, body) {
|
|
83
|
+
response.writeHead(status, { "Content-Type": "application/json" });
|
|
84
|
+
response.end(JSON.stringify(body));
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=routes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../../src/server/routes.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhE,OAAO,EACL,kBAAkB,E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CAAC;IACJ,CAAC;AACH,CAAC;AAED,SAAS,IAAI,CAAC,QAAwB,EAAE,MAAc,EAAE,IAAa;IACnE,QAAQ,CAAC,SAAS,CAAC,MAAM,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC,CAAC;IACnE,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport type { ValidateFunction } from \"ajv\";\nimport { FindingsError } from \"./errors.js\";\nimport { dashboardQuery, serveDashboard } from \"./dashboard.js\";\nimport type { FindingsService } from \"./findings-service.js\";\nimport {\n findingSearchScope,\n pagination,\n validateDedupeGroups,\n type FindingsRequest,\n} from \"./validation.js\";\n\nexport async function handleFindingsRequest(\n request: IncomingMessage,\n response: ServerResponse,\n service: FindingsService,\n validate: ValidateFunction<FindingsRequest>,\n): Promise<void> {\n try {\n const url = new URL(request.url ?? \"/\", \"http://localhost\");\n const route = `${request.method} ${url.pathname}`;\n if (\n request.method === \"GET\" &&\n (await serveDashboard(url.pathname, response))\n )\n return;\n if (route === \"GET /v1/dashboard\") {\n response.setHeader(\"Cache-Control\", \"no-store\");\n json(\n response,\n 200,\n await service.dashboard(dashboardQuery(url.searchParams)),\n );\n return;\n }\n if (route === \"GET /v1/findings\") {\n console.log(route);\n json(response, 200, await service.list(pagination(url.searchParams)));\n return;\n }\n const candidates = /^\\/v1\\/finding\\/([^/]+)\\/potential-duplicates$/.exec(\n url.pathname,\n );\n if (request.method === \"GET\" && candidates) {\n console.log(\"GET /v1/finding/:id/potential-duplicates\");\n json(\n response,\n 200,\n await service.potentialDuplicates(\n candidates[1]!,\n findingSearchScope(url.searchParams),\n ),\n );\n return;\n }\n const dedupeGroups = /^\\/v1\\/finding\\/([^/]+)\\/dedupe-groups$/.exec(\n url.pathname,\n );\n if (request.method === \"GET\" && dedupeGroups) {\n console.log(\"GET /v1/finding/:id/dedupe-groups\");\n json(response, 200, await service.listDedupeGroups(dedupeGroups[1]!));\n return;\n }\n if (route === \"POST /v1/dedupe-groups\") {\n console.log(route);\n const input = await readJson(request);\n if (!validateDedupeGroups(input)) {\n throw new FindingsError(\n \"invalid_request\",\n \"Expected {groups: [[findingId, ...], ...]} with at least two distinct finding IDs per group.\",\n );\n }\n json(response, 201, await service.storeDedupeGroups(input.groups));\n return;\n }\n if (route === \"POST /v1/bulk/findings\") {\n console.log(route);\n const input = await readJson(request);\n if (!validate(input)) {\n throw new FindingsError(\n \"invalid_request\",\n \"Expected {findings: [...]} with an optional nonempty repositoryId, using the existing Finding schema.\",\n );\n }\n json(\n response,\n 201,\n await service.insert(input.findings, input.repositoryId),\n );\n return;\n }\n request.resume();\n json(response, 404, { error: \"not_found\" });\n } catch (error) {\n if (error instanceof FindingsError) {\n const status = {\n invalid_request: 400,\n finding_conflict: 409,\n embedding_unavailable: 503,\n embedding_failed: 502,\n finding_not_indexed: 404,\n }[error.code];\n json(response, status, { error: error.code, message: error.message });\n } else {\n console.error(error);\n json(response, 500, { error: \"internal_error\" });\n }\n }\n}\n\nasync function readJson(request: IncomingMessage): Promise<unknown> {\n const chunks: Buffer[] = [];\n for await (const chunk of request) chunks.push(Buffer.from(chunk));\n try {\n return JSON.parse(Buffer.concat(chunks).toString(\"utf8\"));\n } catch {\n throw new FindingsError(\n \"invalid_request\",\n \"Request body must be valid JSON.\",\n );\n }\n}\n\nfunction json(response: ServerResponse, status: number, body: unknown): void {\n response.writeHead(status, { \"Content-Type\": \"application/json\" });\n response.end(JSON.stringify(body));\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Server } from "node:http";
|
|
2
|
+
import type { FindingEmbedder } from "./embeddings.js";
|
|
3
|
+
import type { FindingsStore } from "./storage.js";
|
|
4
|
+
export declare function startFindingsServer(options: {
|
|
5
|
+
store: FindingsStore;
|
|
6
|
+
embeddings: FindingEmbedder;
|
|
7
|
+
host: string;
|
|
8
|
+
port: number;
|
|
9
|
+
}): Promise<Server>;
|
|
10
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAGlD,wBAAsB,mBAAmB,CAAC,OAAO,EAAE;IACjD,KAAK,EAAE,aAAa,CAAC;IACrB,UAAU,EAAE,eAAe,CAAC;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,GAAG,OAAO,CAAC,MAAM,CAAC,CAUlB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { once } from "node:events";
|
|
2
|
+
import { createServer } from "node:http";
|
|
3
|
+
import { FindingsService } from "./findings-service.js";
|
|
4
|
+
import { handleFindingsRequest } from "./routes.js";
|
|
5
|
+
import { findingsRequestValidator } from "./validation.js";
|
|
6
|
+
export async function startFindingsServer(options) {
|
|
7
|
+
await options.store.initialize();
|
|
8
|
+
const validate = await findingsRequestValidator();
|
|
9
|
+
const service = new FindingsService(options.store, options.embeddings);
|
|
10
|
+
const server = createServer((request, response) => {
|
|
11
|
+
void handleFindingsRequest(request, response, service, validate);
|
|
12
|
+
});
|
|
13
|
+
server.listen(options.port, options.host);
|
|
14
|
+
await once(server, "listening");
|
|
15
|
+
return server;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACnC,OAAO,EAAE,YAAY,EAAe,MAAM,WAAW,CAAC;AAEtD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAE3D,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,OAKzC;IACC,MAAM,OAAO,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;IACjC,MAAM,QAAQ,GAAG,MAAM,wBAAwB,EAAE,CAAC;IAClD,MAAM,OAAO,GAAG,IAAI,eAAe,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;IACvE,MAAM,MAAM,GAAG,YAAY,CAAC,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;QAChD,KAAK,qBAAqB,CAAC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,IAAI,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAChC,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { once } from \"node:events\";\nimport { createServer, type Server } from \"node:http\";\nimport type { FindingEmbedder } from \"./embeddings.js\";\nimport { FindingsService } from \"./findings-service.js\";\nimport { handleFindingsRequest } from \"./routes.js\";\nimport type { FindingsStore } from \"./storage.js\";\nimport { findingsRequestValidator } from \"./validation.js\";\n\nexport async function startFindingsServer(options: {\n store: FindingsStore;\n embeddings: FindingEmbedder;\n host: string;\n port: number;\n}): Promise<Server> {\n await options.store.initialize();\n const validate = await findingsRequestValidator();\n const service = new FindingsService(options.store, options.embeddings);\n const server = createServer((request, response) => {\n void handleFindingsRequest(request, response, service, validate);\n });\n server.listen(options.port, options.host);\n await once(server, \"listening\");\n return server;\n}\n"]}
|