@inkeep/agents-api 0.42.0 → 0.43.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/dist/.well-known/workflow/v1/manifest.debug.json +15 -15
- package/dist/.well-known/workflow/v1/step.cjs +45672 -44470
- package/dist/_virtual/rolldown_runtime.js +7 -0
- package/dist/createApp.js +40 -12
- package/dist/domains/evals/api/.well-known/workflow/v1/flow.d.ts +4 -0
- package/dist/domains/evals/api/.well-known/workflow/v1/flow.js +12 -0
- package/dist/domains/evals/api/.well-known/workflow/v1/step.d.ts +4 -0
- package/dist/domains/evals/api/.well-known/workflow/v1/step.js +12 -0
- package/dist/domains/evals/routes/datasetTriggers.d.ts +2 -2
- package/dist/domains/evals/routes/index.d.ts +2 -2
- package/dist/domains/evals/scripts/build-workflow.js +2 -2
- package/dist/domains/evals/workflow/routes.d.ts +2 -2
- package/dist/domains/evals/workflow/world.js +3 -2
- package/dist/domains/github/config.d.ts +14 -0
- package/dist/domains/github/config.js +47 -0
- package/dist/domains/github/index.d.ts +12 -0
- package/dist/domains/github/index.js +18 -0
- package/dist/domains/github/installation.d.ts +34 -0
- package/dist/domains/github/installation.js +172 -0
- package/dist/domains/github/jwks.d.ts +20 -0
- package/dist/domains/github/jwks.js +85 -0
- package/dist/domains/github/oidcToken.d.ts +22 -0
- package/dist/domains/github/oidcToken.js +140 -0
- package/dist/domains/github/routes/tokenExchange.d.ts +7 -0
- package/dist/domains/github/routes/tokenExchange.js +130 -0
- package/dist/domains/manage/index.js +0 -2
- package/dist/domains/manage/routes/agent.js +7 -4
- package/dist/domains/manage/routes/agentFull.js +9 -6
- package/dist/domains/manage/routes/apiKeys.js +1 -2
- package/dist/domains/manage/routes/artifactComponents.js +5 -5
- package/dist/domains/manage/routes/cliAuth.js +3 -3
- package/dist/domains/manage/routes/contextConfigs.js +5 -5
- package/dist/domains/manage/routes/conversations.d.ts +2 -2
- package/dist/domains/manage/routes/credentialStores.js +2 -2
- package/dist/domains/manage/routes/credentials.js +6 -7
- package/dist/domains/manage/routes/dataComponents.js +6 -7
- package/dist/domains/manage/routes/externalAgents.js +1 -2
- package/dist/domains/manage/routes/index.d.ts +2 -2
- package/dist/domains/manage/routes/index.js +4 -0
- package/dist/domains/manage/routes/invitations.js +1 -1
- package/dist/domains/manage/routes/mcp.d.ts +2 -2
- package/dist/domains/manage/routes/playgroundToken.js +1 -2
- package/dist/domains/manage/routes/projectFull.js +33 -11
- package/dist/domains/manage/routes/projectMembers.js +16 -35
- package/dist/domains/manage/routes/projectPermissions.js +17 -10
- package/dist/domains/manage/routes/projects.js +4 -5
- package/dist/domains/manage/routes/signoz.d.ts +2 -2
- package/dist/domains/manage/routes/signoz.js +6 -3
- package/dist/domains/manage/routes/subAgentArtifactComponents.js +5 -5
- package/dist/domains/manage/routes/subAgentDataComponents.js +5 -5
- package/dist/domains/manage/routes/subAgentExternalAgentRelations.js +5 -5
- package/dist/domains/manage/routes/subAgentFunctionTools.js +5 -5
- package/dist/domains/manage/routes/subAgentRelations.js +6 -6
- package/dist/domains/manage/routes/subAgentTeamAgentRelations.js +6 -6
- package/dist/domains/manage/routes/subAgentToolRelations.js +6 -6
- package/dist/domains/manage/routes/subAgents.js +5 -5
- package/dist/domains/manage/routes/tools.js +24 -3
- package/dist/domains/manage/routes/triggers.js +49 -24
- package/dist/domains/manage/routes/userOrganizations.js +4 -4
- package/dist/domains/manage/routes/{agentToolRelations.d.ts → userProjectMemberships.d.ts} +1 -1
- package/dist/domains/manage/routes/userProjectMemberships.js +45 -0
- package/dist/domains/mcp/routes/mcp.d.ts +7 -0
- package/dist/domains/mcp/routes/mcp.js +45 -0
- package/dist/domains/run/agents/Agent.d.ts +1 -0
- package/dist/domains/run/agents/Agent.js +207 -44
- package/dist/domains/run/agents/relationTools.d.ts +2 -2
- package/dist/domains/run/context/ContextFetcher.js +8 -7
- package/dist/domains/run/context/ContextResolver.js +1 -1
- package/dist/domains/run/handlers/executionHandler.js +143 -79
- package/dist/domains/run/routes/agents.js +1 -1
- package/dist/domains/run/routes/chat.js +47 -1
- package/dist/domains/run/routes/chatDataStream.js +107 -14
- package/dist/domains/run/routes/webhooks.js +40 -348
- package/dist/domains/run/services/AgentSession.d.ts +3 -0
- package/dist/domains/run/services/AgentSession.js +9 -0
- package/dist/domains/run/services/BaseCompressor.js +1 -1
- package/dist/domains/run/services/ToolApprovalUiBus.d.ts +28 -0
- package/dist/domains/run/services/ToolApprovalUiBus.js +44 -0
- package/dist/domains/run/services/TriggerService.d.ts +31 -0
- package/dist/domains/run/services/TriggerService.js +543 -0
- package/dist/domains/run/tools/NativeSandboxExecutor.d.ts +3 -2
- package/dist/domains/run/tools/NativeSandboxExecutor.js +76 -48
- package/dist/domains/run/tools/SandboxExecutorFactory.d.ts +11 -1
- package/dist/domains/run/tools/SandboxExecutorFactory.js +27 -3
- package/dist/domains/run/tools/VercelSandboxExecutor.d.ts +3 -11
- package/dist/domains/run/tools/VercelSandboxExecutor.js +137 -127
- package/dist/domains/run/utils/stream-helpers.d.ts +134 -0
- package/dist/domains/run/utils/stream-helpers.js +182 -0
- package/dist/domains/run/utils/token-estimator.d.ts +2 -2
- package/dist/factory.d.ts +24 -24
- package/dist/index.d.ts +24 -24
- package/dist/initialization.js +9 -2
- package/dist/middleware/cors.js +1 -1
- package/dist/middleware/manageAuth.d.ts +2 -2
- package/dist/middleware/projectAccess.d.ts +4 -11
- package/dist/middleware/projectAccess.js +1 -17
- package/dist/middleware/projectConfig.d.ts +3 -3
- package/dist/middleware/requirePermission.d.ts +2 -2
- package/dist/middleware/runAuth.d.ts +4 -4
- package/dist/middleware/sessionAuth.d.ts +3 -3
- package/dist/middleware/tenantAccess.d.ts +2 -2
- package/dist/middleware/tenantAccess.js +4 -4
- package/dist/middleware/tracing.d.ts +3 -3
- package/dist/openapi.d.ts +35 -1
- package/dist/openapi.js +39 -95
- package/dist/routes/healthChecks.d.ts +10 -0
- package/dist/routes/healthChecks.js +75 -0
- package/dist/types/app.d.ts +2 -0
- package/dist/utils/healthChecks.d.ts +8 -0
- package/dist/utils/healthChecks.js +38 -0
- package/dist/utils/signozHelpers.d.ts +2 -2
- package/dist/utils/signozHelpers.js +15 -3
- package/package.json +7 -9
- package/dist/domains/evals/services/startEvaluation.d.ts +0 -19
- package/dist/domains/evals/services/startEvaluation.js +0 -18
- package/dist/domains/index.d.ts +0 -4
- package/dist/domains/index.js +0 -5
- package/dist/domains/manage/routes/agentToolRelations.js +0 -289
- package/dist/domains/run/agents/ModelFactory.d.ts +0 -63
- package/dist/domains/run/agents/ModelFactory.js +0 -194
- package/dist/domains/run/data/agent.d.ts +0 -7
- package/dist/domains/run/data/agent.js +0 -67
- package/dist/domains/run/services/evaluationRunConfigMatcher.d.ts +0 -4
- package/dist/domains/run/services/evaluationRunConfigMatcher.js +0 -7
- package/dist/domains/run/utils/cleanup.d.ts +0 -21
- package/dist/domains/run/utils/cleanup.js +0 -59
- package/dist/utils/tempApiKeys.d.ts +0 -17
- package/dist/utils/tempApiKeys.js +0 -26
- package/dist/utils/workflowApiHelpers.d.ts +0 -1
- package/dist/utils/workflowApiHelpers.js +0 -1
package/dist/createApp.js
CHANGED
|
@@ -2,8 +2,10 @@ import { getLogger } from "./logger.js";
|
|
|
2
2
|
import { env } from "./env.js";
|
|
3
3
|
import { evalRoutes } from "./domains/evals/index.js";
|
|
4
4
|
import { workflowRoutes } from "./domains/evals/workflow/routes.js";
|
|
5
|
+
import { githubRoutes } from "./domains/github/index.js";
|
|
5
6
|
import { sessionAuth, sessionContext } from "./middleware/sessionAuth.js";
|
|
6
7
|
import { manageRoutes } from "./domains/manage/index.js";
|
|
8
|
+
import mcp_default from "./domains/mcp/routes/mcp.js";
|
|
7
9
|
import { flushBatchProcessor } from "./instrumentation.js";
|
|
8
10
|
import { runRoutes } from "./domains/run/index.js";
|
|
9
11
|
import { authCorsConfig, defaultCorsConfig, playgroundCorsConfig, runCorsConfig, signozCorsConfig } from "./middleware/cors.js";
|
|
@@ -18,7 +20,8 @@ import { evalApiKeyAuth } from "./middleware/evalsAuth.js";
|
|
|
18
20
|
import { projectConfigMiddleware, projectConfigMiddlewareExcept } from "./middleware/projectConfig.js";
|
|
19
21
|
import { executionBaggageMiddleware } from "./middleware/tracing.js";
|
|
20
22
|
import { setupOpenAPIRoutes } from "./openapi.js";
|
|
21
|
-
import {
|
|
23
|
+
import { healthChecksHandler } from "./routes/healthChecks.js";
|
|
24
|
+
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
22
25
|
import { Hono } from "hono";
|
|
23
26
|
import { cors } from "hono/cors";
|
|
24
27
|
import { requestId } from "hono/request-id";
|
|
@@ -33,6 +36,11 @@ const isWebhookRoute = (path) => {
|
|
|
33
36
|
function createAgentsHono(config) {
|
|
34
37
|
const { serverConfig, credentialStores, auth, sandboxConfig } = config;
|
|
35
38
|
const app = new OpenAPIHono();
|
|
39
|
+
const CapabilitiesResponseSchema = z.object({ sandbox: z.object({
|
|
40
|
+
configured: z.boolean().describe("Whether a sandbox provider is configured. Required for Function Tools execution."),
|
|
41
|
+
provider: z.enum(["native", "vercel"]).optional().describe("The configured sandbox provider, if enabled."),
|
|
42
|
+
runtime: z.enum(["node22", "typescript"]).optional().describe("The configured sandbox runtime, if enabled.")
|
|
43
|
+
}).describe("Sandbox execution capabilities (used by Function Tools).") }).describe("Optional server capabilities and configuration.").openapi("CapabilitiesResponseSchema");
|
|
36
44
|
app.use("*", requestId());
|
|
37
45
|
if (auth) {
|
|
38
46
|
app.use("/api/auth/*", cors(authCorsConfig));
|
|
@@ -48,6 +56,7 @@ function createAgentsHono(config) {
|
|
|
48
56
|
if (c.req.path.startsWith("/run/")) return next();
|
|
49
57
|
if (c.req.path.includes("/playground/token")) return next();
|
|
50
58
|
if (c.req.path.includes("/signoz/")) return next();
|
|
59
|
+
if (c.req.path.includes("/api/github/")) return next();
|
|
51
60
|
return cors(defaultCorsConfig)(c, next);
|
|
52
61
|
});
|
|
53
62
|
app.use("*", async (c, next) => {
|
|
@@ -82,20 +91,11 @@ function createAgentsHono(config) {
|
|
|
82
91
|
}));
|
|
83
92
|
app.onError(errorHandler);
|
|
84
93
|
app.use("*", sessionContext());
|
|
85
|
-
app.
|
|
86
|
-
method: "get",
|
|
87
|
-
path: "/health",
|
|
88
|
-
operationId: "health",
|
|
89
|
-
summary: "Health check",
|
|
90
|
-
description: "Check if the management service is healthy",
|
|
91
|
-
responses: { 204: { description: "Service is healthy" } }
|
|
92
|
-
}), (c) => {
|
|
93
|
-
return c.body(null, 204);
|
|
94
|
-
});
|
|
94
|
+
app.route("/", healthChecksHandler);
|
|
95
95
|
app.openapi(createRoute({
|
|
96
96
|
method: "get",
|
|
97
97
|
path: "/api/workflow/process",
|
|
98
|
-
tags: ["
|
|
98
|
+
tags: ["Workflows"],
|
|
99
99
|
summary: "Process workflow jobs",
|
|
100
100
|
description: "Keeps the workflow worker active to process queued jobs (called by cron)",
|
|
101
101
|
responses: { 200: { description: "Processing complete" } }
|
|
@@ -114,6 +114,32 @@ function createAgentsHono(config) {
|
|
|
114
114
|
if (c.req.header("Authorization")?.startsWith("Bearer ")) return manageApiKeyAuth()(c, next);
|
|
115
115
|
return sessionAuth()(c, next);
|
|
116
116
|
});
|
|
117
|
+
app.use("/manage/capabilities", async (c, next) => {
|
|
118
|
+
if (!auth || env.DISABLE_AUTH || isTestEnvironment()) {
|
|
119
|
+
await next();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (c.req.header("Authorization")?.startsWith("Bearer ")) return manageApiKeyAuth()(c, next);
|
|
123
|
+
return sessionAuth()(c, next);
|
|
124
|
+
});
|
|
125
|
+
app.openapi(createRoute({
|
|
126
|
+
method: "get",
|
|
127
|
+
path: "/manage/capabilities",
|
|
128
|
+
operationId: "capabilities",
|
|
129
|
+
summary: "Get server capabilities",
|
|
130
|
+
description: "Get information about optional server-side capabilities and configuration.",
|
|
131
|
+
responses: { 200: {
|
|
132
|
+
description: "Server capabilities",
|
|
133
|
+
content: { "application/json": { schema: CapabilitiesResponseSchema } }
|
|
134
|
+
} }
|
|
135
|
+
}), (c) => {
|
|
136
|
+
if (!sandboxConfig) return c.json({ sandbox: { configured: false } });
|
|
137
|
+
return c.json({ sandbox: {
|
|
138
|
+
configured: true,
|
|
139
|
+
provider: sandboxConfig.provider,
|
|
140
|
+
runtime: sandboxConfig.runtime
|
|
141
|
+
} });
|
|
142
|
+
});
|
|
117
143
|
if (env.DISABLE_AUTH || isTestEnvironment()) app.use("/manage/tenants/:tenantId/*", async (c, next) => {
|
|
118
144
|
const tenantId = c.req.param("tenantId");
|
|
119
145
|
if (tenantId) {
|
|
@@ -156,6 +182,8 @@ function createAgentsHono(config) {
|
|
|
156
182
|
return fetch(forwardedRequest);
|
|
157
183
|
});
|
|
158
184
|
app.route("/evals", evalRoutes);
|
|
185
|
+
app.route("/api/github", githubRoutes);
|
|
186
|
+
app.route("/mcp", mcp_default);
|
|
159
187
|
setupOpenAPIRoutes(app);
|
|
160
188
|
app.use("/run/*", async (_c, next) => {
|
|
161
189
|
await next();
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
//#region src/domains/evals/api/.well-known/workflow/v1/flow.ts
|
|
4
|
+
console.log("[WF CONSUMER] flow.ts loaded", (/* @__PURE__ */ new Date()).toISOString());
|
|
5
|
+
const flowModule = createRequire(import.meta.url)("../../../../dist/.well-known/workflow/v1/flow.cjs");
|
|
6
|
+
async function handler(req) {
|
|
7
|
+
console.log("[WF CONSUMER HIT] flow.ts", (/* @__PURE__ */ new Date()).toISOString(), req.method, req.url);
|
|
8
|
+
return flowModule.default(req);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { handler as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
//#region src/domains/evals/api/.well-known/workflow/v1/step.ts
|
|
4
|
+
console.log("[WF CONSUMER] step.ts loaded", (/* @__PURE__ */ new Date()).toISOString());
|
|
5
|
+
const stepModule = createRequire(import.meta.url)("../../../../dist/.well-known/workflow/v1/step.cjs");
|
|
6
|
+
async function handler(req) {
|
|
7
|
+
console.log("[WF CONSUMER HIT] step.ts", (/* @__PURE__ */ new Date()).toISOString(), req.method, req.url);
|
|
8
|
+
return stepModule.default(req);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { handler as default };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import * as
|
|
2
|
+
import * as hono12 from "hono";
|
|
3
3
|
|
|
4
4
|
//#region src/domains/evals/routes/datasetTriggers.d.ts
|
|
5
|
-
declare const app: OpenAPIHono<
|
|
5
|
+
declare const app: OpenAPIHono<hono12.Env, {}, "/">;
|
|
6
6
|
//#endregion
|
|
7
7
|
export { app as default };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import * as
|
|
2
|
+
import * as hono13 from "hono";
|
|
3
3
|
|
|
4
4
|
//#region src/domains/evals/routes/index.d.ts
|
|
5
|
-
declare const app: OpenAPIHono<
|
|
5
|
+
declare const app: OpenAPIHono<hono13.Env, {}, "/">;
|
|
6
6
|
//#endregion
|
|
7
7
|
export { app as default };
|
|
@@ -5,7 +5,7 @@ import { StandaloneBuilder } from "@workflow/builders";
|
|
|
5
5
|
* Custom workflow build script that supports externalizing native modules.
|
|
6
6
|
*
|
|
7
7
|
* The default `workflow build` CLI doesn't expose the externalPackages option,
|
|
8
|
-
* so we use the builder directly to exclude native modules like
|
|
8
|
+
* so we use the builder directly to exclude native modules like @napi-rs/keyring.
|
|
9
9
|
*/
|
|
10
10
|
const config = {
|
|
11
11
|
dirs: ["./src/domains/evals/workflow"],
|
|
@@ -14,7 +14,7 @@ const config = {
|
|
|
14
14
|
stepsBundlePath: "./.well-known/workflow/v1/step.cjs",
|
|
15
15
|
workflowsBundlePath: "./.well-known/workflow/v1/flow.cjs",
|
|
16
16
|
webhookBundlePath: "./.well-known/workflow/v1/webhook.mjs",
|
|
17
|
-
externalPackages: ["
|
|
17
|
+
externalPackages: ["@napi-rs/keyring"]
|
|
18
18
|
};
|
|
19
19
|
async function build() {
|
|
20
20
|
console.log("Building workflow bundles...");
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
|
-
import * as
|
|
2
|
+
import * as hono_types9 from "hono/types";
|
|
3
3
|
|
|
4
4
|
//#region src/domains/evals/workflow/routes.d.ts
|
|
5
|
-
declare const workflowRoutes: Hono<
|
|
5
|
+
declare const workflowRoutes: Hono<hono_types9.BlankEnv, hono_types9.BlankSchema, "/">;
|
|
6
6
|
//#endregion
|
|
7
7
|
export { workflowRoutes };
|
|
@@ -11,8 +11,9 @@ import { createVercelWorld } from "@workflow/world-vercel";
|
|
|
11
11
|
* Vercel's NFT can't trace dynamic imports in bundled code.
|
|
12
12
|
*
|
|
13
13
|
* Set WORKFLOW_TARGET_WORLD to: 'local' | 'vercel' | '@workflow/world-postgres'
|
|
14
|
+
* Defaults to 'local' for development if not set.
|
|
14
15
|
*/
|
|
15
|
-
const targetWorld = env.WORKFLOW_TARGET_WORLD;
|
|
16
|
+
const targetWorld = env.WORKFLOW_TARGET_WORLD || "local";
|
|
16
17
|
let world;
|
|
17
18
|
if (targetWorld === "vercel") {
|
|
18
19
|
const token = process.env.WORKFLOW_VERCEL_AUTH_TOKEN;
|
|
@@ -30,7 +31,7 @@ if (targetWorld === "vercel") {
|
|
|
30
31
|
jobPrefix: env.WORKFLOW_POSTGRES_JOB_PREFIX,
|
|
31
32
|
queueConcurrency: Number(env.WORKFLOW_POSTGRES_WORKER_CONCURRENCY) || 10
|
|
32
33
|
});
|
|
33
|
-
else
|
|
34
|
+
else world = createLocalWorld();
|
|
34
35
|
|
|
35
36
|
//#endregion
|
|
36
37
|
export { world };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from "@hono/zod-openapi";
|
|
2
|
+
|
|
3
|
+
//#region src/domains/github/config.d.ts
|
|
4
|
+
declare const GitHubAppConfigSchema: z.ZodObject<{
|
|
5
|
+
appId: z.ZodString;
|
|
6
|
+
privateKey: z.ZodString;
|
|
7
|
+
}, z.core.$strip>;
|
|
8
|
+
type GitHubAppConfig = z.infer<typeof GitHubAppConfigSchema>;
|
|
9
|
+
declare function getGitHubAppConfig(): GitHubAppConfig;
|
|
10
|
+
declare function isGitHubAppConfigured(): boolean;
|
|
11
|
+
declare function validateGitHubAppConfigOnStartup(): void;
|
|
12
|
+
declare function clearConfigCache(): void;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { GitHubAppConfig, clearConfigCache, getGitHubAppConfig, isGitHubAppConfigured, validateGitHubAppConfigOnStartup };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { getLogger } from "../../logger.js";
|
|
2
|
+
import { z } from "@hono/zod-openapi";
|
|
3
|
+
|
|
4
|
+
//#region src/domains/github/config.ts
|
|
5
|
+
const logger = getLogger("github-config");
|
|
6
|
+
const GitHubAppConfigSchema = z.object({
|
|
7
|
+
appId: z.string().min(1, "GITHUB_APP_ID is required"),
|
|
8
|
+
privateKey: z.string().min(1, "GITHUB_APP_PRIVATE_KEY is required")
|
|
9
|
+
});
|
|
10
|
+
let cachedConfig = null;
|
|
11
|
+
function getGitHubAppConfig() {
|
|
12
|
+
if (cachedConfig) return cachedConfig;
|
|
13
|
+
const appId = process.env.GITHUB_APP_ID;
|
|
14
|
+
const privateKey = process.env.GITHUB_APP_PRIVATE_KEY?.replace(/\\n/g, "\n");
|
|
15
|
+
const result = GitHubAppConfigSchema.safeParse({
|
|
16
|
+
appId,
|
|
17
|
+
privateKey
|
|
18
|
+
});
|
|
19
|
+
if (!result.success) {
|
|
20
|
+
const errorMessage = `GitHub App credentials are not configured. ${result.error.issues.map((issue) => issue.message).join(". ")}. Please set GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY environment variables.`;
|
|
21
|
+
logger.error({}, errorMessage);
|
|
22
|
+
throw new Error(errorMessage);
|
|
23
|
+
}
|
|
24
|
+
cachedConfig = result.data;
|
|
25
|
+
logger.info({}, "GitHub App credentials loaded successfully");
|
|
26
|
+
return cachedConfig;
|
|
27
|
+
}
|
|
28
|
+
function isGitHubAppConfigured() {
|
|
29
|
+
return Boolean(process.env.GITHUB_APP_ID && process.env.GITHUB_APP_PRIVATE_KEY);
|
|
30
|
+
}
|
|
31
|
+
function validateGitHubAppConfigOnStartup() {
|
|
32
|
+
if (!isGitHubAppConfigured()) {
|
|
33
|
+
logger.warn({}, "GitHub App credentials not configured. Token exchange endpoint will return 500 errors. Set GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY to enable the feature.");
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
getGitHubAppConfig();
|
|
38
|
+
} catch (error) {
|
|
39
|
+
logger.error({ error }, "GitHub App credentials are invalid. Token exchange endpoint will return 500 errors.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function clearConfigCache() {
|
|
43
|
+
cachedConfig = null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//#endregion
|
|
47
|
+
export { clearConfigCache, getGitHubAppConfig, isGitHubAppConfigured, validateGitHubAppConfigOnStartup };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GitHubAppConfig, getGitHubAppConfig, isGitHubAppConfigured } from "./config.js";
|
|
2
|
+
import { GenerateInstallationAccessTokenResult, GenerateTokenError, GenerateTokenResult, InstallationAccessToken, InstallationInfo, LookupInstallationError, LookupInstallationForRepoResult, LookupInstallationResult, generateInstallationAccessToken, lookupInstallationForRepo } from "./installation.js";
|
|
3
|
+
import { GetJwkResult, JwksError, JwksResult, clearJwksCache, getJwkForToken, getJwksCacheStatus } from "./jwks.js";
|
|
4
|
+
import { GitHubOidcClaims, ValidateOidcTokenResult, ValidateTokenError, ValidateTokenResult, validateOidcToken } from "./oidcToken.js";
|
|
5
|
+
import { Hono } from "hono";
|
|
6
|
+
import * as hono_types5 from "hono/types";
|
|
7
|
+
|
|
8
|
+
//#region src/domains/github/index.d.ts
|
|
9
|
+
declare function createGithubRoutes(): Hono<hono_types5.BlankEnv, hono_types5.BlankSchema, "/">;
|
|
10
|
+
declare const githubRoutes: Hono<hono_types5.BlankEnv, hono_types5.BlankSchema, "/">;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { type GenerateInstallationAccessTokenResult, type GenerateTokenError, type GenerateTokenResult, type GetJwkResult, type GitHubAppConfig, type GitHubOidcClaims, type InstallationAccessToken, type InstallationInfo, type JwksError, type JwksResult, type LookupInstallationError, type LookupInstallationForRepoResult, type LookupInstallationResult, type ValidateOidcTokenResult, type ValidateTokenError, type ValidateTokenResult, clearJwksCache, createGithubRoutes, generateInstallationAccessToken, getGitHubAppConfig, getJwkForToken, getJwksCacheStatus, githubRoutes, isGitHubAppConfigured, lookupInstallationForRepo, validateOidcToken };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getGitHubAppConfig, isGitHubAppConfigured, validateGitHubAppConfigOnStartup } from "./config.js";
|
|
2
|
+
import { generateInstallationAccessToken, lookupInstallationForRepo } from "./installation.js";
|
|
3
|
+
import { clearJwksCache, getJwkForToken, getJwksCacheStatus } from "./jwks.js";
|
|
4
|
+
import { validateOidcToken } from "./oidcToken.js";
|
|
5
|
+
import tokenExchange_default from "./routes/tokenExchange.js";
|
|
6
|
+
import { Hono } from "hono";
|
|
7
|
+
|
|
8
|
+
//#region src/domains/github/index.ts
|
|
9
|
+
function createGithubRoutes() {
|
|
10
|
+
validateGitHubAppConfigOnStartup();
|
|
11
|
+
const app = new Hono();
|
|
12
|
+
app.route("/token-exchange", tokenExchange_default);
|
|
13
|
+
return app;
|
|
14
|
+
}
|
|
15
|
+
const githubRoutes = createGithubRoutes();
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { clearJwksCache, createGithubRoutes, generateInstallationAccessToken, getGitHubAppConfig, getJwkForToken, getJwksCacheStatus, githubRoutes, isGitHubAppConfigured, lookupInstallationForRepo, validateOidcToken };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region src/domains/github/installation.d.ts
|
|
2
|
+
interface InstallationInfo {
|
|
3
|
+
installationId: number;
|
|
4
|
+
accountLogin: string;
|
|
5
|
+
accountType: 'User' | 'Organization';
|
|
6
|
+
}
|
|
7
|
+
interface LookupInstallationResult {
|
|
8
|
+
success: true;
|
|
9
|
+
installation: InstallationInfo;
|
|
10
|
+
}
|
|
11
|
+
interface LookupInstallationError {
|
|
12
|
+
success: false;
|
|
13
|
+
errorType: 'not_installed' | 'api_error' | 'jwt_error';
|
|
14
|
+
message: string;
|
|
15
|
+
}
|
|
16
|
+
type LookupInstallationForRepoResult = LookupInstallationResult | LookupInstallationError;
|
|
17
|
+
interface InstallationAccessToken {
|
|
18
|
+
token: string;
|
|
19
|
+
expiresAt: string;
|
|
20
|
+
}
|
|
21
|
+
interface GenerateTokenResult {
|
|
22
|
+
success: true;
|
|
23
|
+
accessToken: InstallationAccessToken;
|
|
24
|
+
}
|
|
25
|
+
interface GenerateTokenError {
|
|
26
|
+
success: false;
|
|
27
|
+
errorType: 'api_error' | 'jwt_error';
|
|
28
|
+
message: string;
|
|
29
|
+
}
|
|
30
|
+
type GenerateInstallationAccessTokenResult = GenerateTokenResult | GenerateTokenError;
|
|
31
|
+
declare function lookupInstallationForRepo(repositoryOwner: string, repositoryName: string): Promise<LookupInstallationForRepoResult>;
|
|
32
|
+
declare function generateInstallationAccessToken(installationId: number): Promise<GenerateInstallationAccessTokenResult>;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { GenerateInstallationAccessTokenResult, GenerateTokenError, GenerateTokenResult, InstallationAccessToken, InstallationInfo, LookupInstallationError, LookupInstallationForRepoResult, LookupInstallationResult, generateInstallationAccessToken, lookupInstallationForRepo };
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { getLogger } from "../../logger.js";
|
|
2
|
+
import { getGitHubAppConfig } from "./config.js";
|
|
3
|
+
import { createPrivateKey } from "node:crypto";
|
|
4
|
+
import { SignJWT } from "jose";
|
|
5
|
+
|
|
6
|
+
//#region src/domains/github/installation.ts
|
|
7
|
+
const logger = getLogger("github-installation");
|
|
8
|
+
const GITHUB_API_BASE = "https://api.github.com";
|
|
9
|
+
async function createAppJwt() {
|
|
10
|
+
const config = getGitHubAppConfig();
|
|
11
|
+
const privateKey = createPrivateKey({
|
|
12
|
+
key: config.privateKey,
|
|
13
|
+
format: "pem"
|
|
14
|
+
});
|
|
15
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
16
|
+
return await new SignJWT({}).setProtectedHeader({ alg: "RS256" }).setIssuedAt(now - 60).setExpirationTime(now + 600).setIssuer(config.appId).sign(privateKey);
|
|
17
|
+
}
|
|
18
|
+
async function lookupInstallationForRepo(repositoryOwner, repositoryName) {
|
|
19
|
+
let appJwt;
|
|
20
|
+
try {
|
|
21
|
+
appJwt = await createAppJwt();
|
|
22
|
+
} catch (error) {
|
|
23
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
24
|
+
logger.error({ error: message }, "Failed to create GitHub App JWT");
|
|
25
|
+
return {
|
|
26
|
+
success: false,
|
|
27
|
+
errorType: "jwt_error",
|
|
28
|
+
message: `Failed to create GitHub App authentication: ${message}`
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const url = `${GITHUB_API_BASE}/repos/${repositoryOwner}/${repositoryName}/installation`;
|
|
32
|
+
try {
|
|
33
|
+
const response = await fetch(url, {
|
|
34
|
+
method: "GET",
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: `Bearer ${appJwt}`,
|
|
37
|
+
Accept: "application/vnd.github+json",
|
|
38
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
39
|
+
"User-Agent": "inkeep-agents-api"
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
if (response.status === 404) return {
|
|
43
|
+
success: false,
|
|
44
|
+
errorType: "not_installed",
|
|
45
|
+
message: `GitHub App is not installed on repository ${repositoryOwner}/${repositoryName}. Please install the Inkeep Agents GitHub App on the repository to enable token exchange.`
|
|
46
|
+
};
|
|
47
|
+
if (!response.ok) {
|
|
48
|
+
const errorText = await response.text();
|
|
49
|
+
logger.error({
|
|
50
|
+
status: response.status,
|
|
51
|
+
error: errorText,
|
|
52
|
+
repositoryOwner,
|
|
53
|
+
repositoryName
|
|
54
|
+
}, "GitHub API error looking up installation");
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
errorType: "api_error",
|
|
58
|
+
message: `GitHub API error (${response.status}): Failed to look up installation for repository`
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
const installationId = data.id;
|
|
63
|
+
const accountLogin = data.account?.login;
|
|
64
|
+
const accountType = data.account?.type;
|
|
65
|
+
if (typeof installationId !== "number" || typeof accountLogin !== "string") {
|
|
66
|
+
logger.error({ data }, "Unexpected response format from GitHub API");
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
errorType: "api_error",
|
|
70
|
+
message: "Unexpected response format from GitHub API"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
logger.info({
|
|
74
|
+
installationId,
|
|
75
|
+
accountLogin,
|
|
76
|
+
accountType,
|
|
77
|
+
repositoryOwner,
|
|
78
|
+
repositoryName
|
|
79
|
+
}, "Found GitHub App installation for repository");
|
|
80
|
+
return {
|
|
81
|
+
success: true,
|
|
82
|
+
installation: {
|
|
83
|
+
installationId,
|
|
84
|
+
accountLogin,
|
|
85
|
+
accountType: accountType === "Organization" ? "Organization" : "User"
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
90
|
+
logger.error({
|
|
91
|
+
error: message,
|
|
92
|
+
repositoryOwner,
|
|
93
|
+
repositoryName
|
|
94
|
+
}, "Error calling GitHub API to look up installation");
|
|
95
|
+
return {
|
|
96
|
+
success: false,
|
|
97
|
+
errorType: "api_error",
|
|
98
|
+
message: `Failed to connect to GitHub API: ${message}`
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function generateInstallationAccessToken(installationId) {
|
|
103
|
+
let appJwt;
|
|
104
|
+
try {
|
|
105
|
+
appJwt = await createAppJwt();
|
|
106
|
+
} catch (error) {
|
|
107
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
108
|
+
logger.error({ error: message }, "Failed to create GitHub App JWT for token generation");
|
|
109
|
+
return {
|
|
110
|
+
success: false,
|
|
111
|
+
errorType: "jwt_error",
|
|
112
|
+
message: `Failed to create GitHub App authentication: ${message}`
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const url = `${GITHUB_API_BASE}/app/installations/${installationId}/access_tokens`;
|
|
116
|
+
try {
|
|
117
|
+
const response = await fetch(url, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: {
|
|
120
|
+
Authorization: `Bearer ${appJwt}`,
|
|
121
|
+
Accept: "application/vnd.github+json",
|
|
122
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
123
|
+
"User-Agent": "inkeep-agents-api"
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
const errorText = await response.text();
|
|
128
|
+
logger.error({
|
|
129
|
+
status: response.status,
|
|
130
|
+
error: errorText,
|
|
131
|
+
installationId
|
|
132
|
+
}, "GitHub API error generating installation access token");
|
|
133
|
+
return {
|
|
134
|
+
success: false,
|
|
135
|
+
errorType: "api_error",
|
|
136
|
+
message: `GitHub API error (${response.status}): Failed to generate installation access token`
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const data = await response.json();
|
|
140
|
+
const token = data.token;
|
|
141
|
+
const expiresAt = data.expires_at;
|
|
142
|
+
if (typeof token !== "string" || typeof expiresAt !== "string") {
|
|
143
|
+
logger.error({ data }, "Unexpected response format from GitHub API for token generation");
|
|
144
|
+
return {
|
|
145
|
+
success: false,
|
|
146
|
+
errorType: "api_error",
|
|
147
|
+
message: "Unexpected response format from GitHub API"
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
success: true,
|
|
152
|
+
accessToken: {
|
|
153
|
+
token,
|
|
154
|
+
expiresAt
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
159
|
+
logger.error({
|
|
160
|
+
error: message,
|
|
161
|
+
installationId
|
|
162
|
+
}, "Error calling GitHub API to generate installation access token");
|
|
163
|
+
return {
|
|
164
|
+
success: false,
|
|
165
|
+
errorType: "api_error",
|
|
166
|
+
message: `Failed to connect to GitHub API: ${message}`
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
export { generateInstallationAccessToken, lookupInstallationForRepo };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { CryptoKey, JWSHeaderParameters } from "jose";
|
|
2
|
+
|
|
3
|
+
//#region src/domains/github/jwks.d.ts
|
|
4
|
+
interface JwksResult {
|
|
5
|
+
success: true;
|
|
6
|
+
key: CryptoKey;
|
|
7
|
+
}
|
|
8
|
+
interface JwksError {
|
|
9
|
+
success: false;
|
|
10
|
+
error: string;
|
|
11
|
+
}
|
|
12
|
+
type GetJwkResult = JwksResult | JwksError;
|
|
13
|
+
declare function getJwkForToken(header: JWSHeaderParameters): Promise<GetJwkResult>;
|
|
14
|
+
declare function clearJwksCache(): void;
|
|
15
|
+
declare function getJwksCacheStatus(): {
|
|
16
|
+
cached: boolean;
|
|
17
|
+
expiresIn?: number;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { GetJwkResult, JwksError, JwksResult, clearJwksCache, getJwkForToken, getJwksCacheStatus };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { getLogger } from "../../logger.js";
|
|
2
|
+
import { createRemoteJWKSet } from "jose";
|
|
3
|
+
|
|
4
|
+
//#region src/domains/github/jwks.ts
|
|
5
|
+
const logger = getLogger("github-jwks");
|
|
6
|
+
const GITHUB_OIDC_JWKS_URL = "https://token.actions.githubusercontent.com/.well-known/jwks";
|
|
7
|
+
const CACHE_TTL_MS = 3600 * 1e3;
|
|
8
|
+
let jwksCache = null;
|
|
9
|
+
function createJwksWithLogging() {
|
|
10
|
+
logger.info({}, "Creating new JWKS fetch function for GitHub OIDC");
|
|
11
|
+
return createRemoteJWKSet(new URL(GITHUB_OIDC_JWKS_URL), { cacheMaxAge: CACHE_TTL_MS });
|
|
12
|
+
}
|
|
13
|
+
function isCacheExpired() {
|
|
14
|
+
if (!jwksCache) return true;
|
|
15
|
+
return Date.now() - jwksCache.fetchedAt > CACHE_TTL_MS;
|
|
16
|
+
}
|
|
17
|
+
function getOrCreateJwksFunction() {
|
|
18
|
+
if (!jwksCache || isCacheExpired()) jwksCache = {
|
|
19
|
+
jwks: createJwksWithLogging(),
|
|
20
|
+
fetchedAt: Date.now()
|
|
21
|
+
};
|
|
22
|
+
return jwksCache.jwks;
|
|
23
|
+
}
|
|
24
|
+
async function getJwkForToken(header) {
|
|
25
|
+
const kid = header.kid;
|
|
26
|
+
if (!kid) return {
|
|
27
|
+
success: false,
|
|
28
|
+
error: "Token is missing key ID (kid) in header"
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
const key = await getOrCreateJwksFunction()(header);
|
|
32
|
+
logger.debug({ kid }, "Successfully retrieved JWK for token");
|
|
33
|
+
return {
|
|
34
|
+
success: true,
|
|
35
|
+
key
|
|
36
|
+
};
|
|
37
|
+
} catch (error) {
|
|
38
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
39
|
+
if (errorMessage.includes("no applicable key found")) {
|
|
40
|
+
logger.warn({ kid }, "Key ID not found in JWKS, refreshing cache");
|
|
41
|
+
jwksCache = null;
|
|
42
|
+
try {
|
|
43
|
+
const key = await getOrCreateJwksFunction()(header);
|
|
44
|
+
logger.info({ kid }, "Successfully retrieved JWK after cache refresh");
|
|
45
|
+
return {
|
|
46
|
+
success: true,
|
|
47
|
+
key
|
|
48
|
+
};
|
|
49
|
+
} catch (retryError) {
|
|
50
|
+
const retryErrorMessage = retryError instanceof Error ? retryError.message : "Unknown error";
|
|
51
|
+
logger.error({
|
|
52
|
+
kid,
|
|
53
|
+
error: retryErrorMessage
|
|
54
|
+
}, "Failed to retrieve JWK after cache refresh");
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
error: `Key ID '${kid}' not found in GitHub OIDC JWKS`
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
logger.error({
|
|
62
|
+
kid,
|
|
63
|
+
error: errorMessage
|
|
64
|
+
}, "Failed to fetch JWKS from GitHub");
|
|
65
|
+
return {
|
|
66
|
+
success: false,
|
|
67
|
+
error: `Failed to fetch GitHub OIDC JWKS: ${errorMessage}`
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function clearJwksCache() {
|
|
72
|
+
jwksCache = null;
|
|
73
|
+
logger.debug({}, "JWKS cache cleared");
|
|
74
|
+
}
|
|
75
|
+
function getJwksCacheStatus() {
|
|
76
|
+
if (!jwksCache) return { cached: false };
|
|
77
|
+
const expiresIn = CACHE_TTL_MS - (Date.now() - jwksCache.fetchedAt);
|
|
78
|
+
return {
|
|
79
|
+
cached: true,
|
|
80
|
+
expiresIn: Math.max(0, expiresIn)
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
export { clearJwksCache, getJwkForToken, getJwksCacheStatus };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/domains/github/oidcToken.d.ts
|
|
2
|
+
interface GitHubOidcClaims {
|
|
3
|
+
repository: string;
|
|
4
|
+
repository_owner: string;
|
|
5
|
+
repository_id: string;
|
|
6
|
+
workflow: string;
|
|
7
|
+
actor: string;
|
|
8
|
+
ref: string;
|
|
9
|
+
}
|
|
10
|
+
interface ValidateTokenResult {
|
|
11
|
+
success: true;
|
|
12
|
+
claims: GitHubOidcClaims;
|
|
13
|
+
}
|
|
14
|
+
interface ValidateTokenError {
|
|
15
|
+
success: false;
|
|
16
|
+
errorType: 'invalid_signature' | 'expired' | 'wrong_issuer' | 'wrong_audience' | 'malformed' | 'jwks_error';
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
type ValidateOidcTokenResult = ValidateTokenResult | ValidateTokenError;
|
|
20
|
+
declare function validateOidcToken(token: string): Promise<ValidateOidcTokenResult>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { GitHubOidcClaims, ValidateOidcTokenResult, ValidateTokenError, ValidateTokenResult, validateOidcToken };
|