@inkeep/agents-run-api 0.39.4 → 0.40.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/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/thinking-preparation.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase1/tool.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-component.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/data-components.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/phase2/system-prompt.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact-retrieval-guidance.js +5 -0
- package/dist/_virtual/_raw_/home/runner/work/agents/agents/agents-run-api/templates/v1/shared/artifact.js +5 -0
- package/dist/a2a/client.d.ts +184 -0
- package/dist/a2a/client.js +510 -0
- package/dist/a2a/handlers.d.ts +7 -0
- package/dist/a2a/handlers.js +560 -0
- package/dist/a2a/transfer.d.ts +22 -0
- package/dist/a2a/transfer.js +46 -0
- package/dist/a2a/types.d.ts +79 -0
- package/dist/a2a/types.js +22 -0
- package/dist/agents/Agent.d.ts +266 -0
- package/dist/agents/Agent.js +1927 -0
- package/dist/agents/ModelFactory.d.ts +63 -0
- package/dist/agents/ModelFactory.js +194 -0
- package/dist/agents/SystemPromptBuilder.d.ts +21 -0
- package/dist/agents/SystemPromptBuilder.js +48 -0
- package/dist/agents/ToolSessionManager.d.ts +63 -0
- package/dist/agents/ToolSessionManager.js +146 -0
- package/dist/agents/generateTaskHandler.d.ts +49 -0
- package/dist/agents/generateTaskHandler.js +521 -0
- package/dist/agents/relationTools.d.ts +57 -0
- package/dist/agents/relationTools.js +262 -0
- package/dist/agents/types.d.ts +28 -0
- package/dist/agents/types.js +1 -0
- package/dist/agents/versions/v1/Phase1Config.d.ts +27 -0
- package/dist/agents/versions/v1/Phase1Config.js +424 -0
- package/dist/agents/versions/v1/Phase2Config.d.ts +31 -0
- package/dist/agents/versions/v1/Phase2Config.js +330 -0
- package/dist/constants/execution-limits/defaults.d.ts +51 -0
- package/dist/constants/execution-limits/defaults.js +52 -0
- package/dist/constants/execution-limits/index.d.ts +6 -0
- package/dist/constants/execution-limits/index.js +21 -0
- package/dist/create-app.d.ts +9 -0
- package/dist/create-app.js +195 -0
- package/dist/data/agent.d.ts +7 -0
- package/dist/data/agent.js +72 -0
- package/dist/data/agents.d.ts +34 -0
- package/dist/data/agents.js +139 -0
- package/dist/data/conversations.d.ts +128 -0
- package/dist/data/conversations.js +522 -0
- package/dist/data/db/dbClient.d.ts +6 -0
- package/dist/data/db/dbClient.js +17 -0
- package/dist/env.d.ts +57 -0
- package/dist/env.js +1 -2
- package/dist/handlers/executionHandler.d.ts +39 -0
- package/dist/handlers/executionHandler.js +456 -0
- package/dist/index.d.ts +8 -29
- package/dist/index.js +5 -11235
- package/dist/instrumentation.d.ts +1 -2
- package/dist/instrumentation.js +66 -3
- package/dist/{logger2.js → logger.d.ts} +1 -2
- package/dist/logger.js +1 -1
- package/dist/middleware/api-key-auth.d.ts +26 -0
- package/dist/middleware/api-key-auth.js +240 -0
- package/dist/middleware/index.d.ts +2 -0
- package/dist/middleware/index.js +3 -0
- package/dist/openapi.d.ts +4 -0
- package/dist/openapi.js +54 -0
- package/dist/routes/agents.d.ts +12 -0
- package/dist/routes/agents.js +147 -0
- package/dist/routes/chat.d.ts +13 -0
- package/dist/routes/chat.js +293 -0
- package/dist/routes/chatDataStream.d.ts +13 -0
- package/dist/routes/chatDataStream.js +352 -0
- package/dist/routes/mcp.d.ts +13 -0
- package/dist/routes/mcp.js +495 -0
- package/dist/services/AgentSession.d.ts +356 -0
- package/dist/services/AgentSession.js +1208 -0
- package/dist/services/ArtifactParser.d.ts +105 -0
- package/dist/services/ArtifactParser.js +338 -0
- package/dist/services/ArtifactService.d.ts +123 -0
- package/dist/services/ArtifactService.js +612 -0
- package/dist/services/BaseCompressor.d.ts +183 -0
- package/dist/services/BaseCompressor.js +504 -0
- package/dist/services/ConversationCompressor.d.ts +32 -0
- package/dist/services/ConversationCompressor.js +91 -0
- package/dist/services/IncrementalStreamParser.d.ts +98 -0
- package/dist/services/IncrementalStreamParser.js +327 -0
- package/dist/services/MidGenerationCompressor.d.ts +63 -0
- package/dist/services/MidGenerationCompressor.js +104 -0
- package/dist/services/PendingToolApprovalManager.d.ts +62 -0
- package/dist/services/PendingToolApprovalManager.js +133 -0
- package/dist/services/ResponseFormatter.d.ts +39 -0
- package/dist/services/ResponseFormatter.js +152 -0
- package/dist/tools/NativeSandboxExecutor.d.ts +38 -0
- package/dist/tools/NativeSandboxExecutor.js +432 -0
- package/dist/tools/SandboxExecutorFactory.d.ts +36 -0
- package/dist/tools/SandboxExecutorFactory.js +80 -0
- package/dist/tools/VercelSandboxExecutor.d.ts +71 -0
- package/dist/tools/VercelSandboxExecutor.js +340 -0
- package/dist/tools/distill-conversation-history-tool.d.ts +62 -0
- package/dist/tools/distill-conversation-history-tool.js +206 -0
- package/dist/tools/distill-conversation-tool.d.ts +41 -0
- package/dist/tools/distill-conversation-tool.js +141 -0
- package/dist/tools/sandbox-utils.d.ts +18 -0
- package/dist/tools/sandbox-utils.js +53 -0
- package/dist/types/chat.d.ts +27 -0
- package/dist/types/chat.js +1 -0
- package/dist/types/execution-context.d.ts +46 -0
- package/dist/types/execution-context.js +27 -0
- package/dist/types/xml.d.ts +5 -0
- package/dist/utils/SchemaProcessor.d.ts +52 -0
- package/dist/utils/SchemaProcessor.js +182 -0
- package/dist/utils/agent-operations.d.ts +62 -0
- package/dist/utils/agent-operations.js +53 -0
- package/dist/utils/artifact-component-schema.d.ts +42 -0
- package/dist/utils/artifact-component-schema.js +186 -0
- package/dist/utils/cleanup.d.ts +21 -0
- package/dist/utils/cleanup.js +59 -0
- package/dist/utils/data-component-schema.d.ts +2 -0
- package/dist/utils/data-component-schema.js +3 -0
- package/dist/utils/default-status-schemas.d.ts +20 -0
- package/dist/utils/default-status-schemas.js +24 -0
- package/dist/utils/json-postprocessor.d.ts +13 -0
- package/dist/{json-postprocessor.cjs → utils/json-postprocessor.js} +1 -2
- package/dist/utils/model-context-utils.d.ts +39 -0
- package/dist/utils/model-context-utils.js +181 -0
- package/dist/utils/model-resolver.d.ts +6 -0
- package/dist/utils/model-resolver.js +34 -0
- package/dist/utils/schema-validation.d.ts +44 -0
- package/dist/utils/schema-validation.js +97 -0
- package/dist/utils/stream-helpers.d.ts +197 -0
- package/dist/utils/stream-helpers.js +518 -0
- package/dist/utils/stream-registry.d.ts +22 -0
- package/dist/utils/stream-registry.js +34 -0
- package/dist/utils/token-estimator.d.ts +69 -0
- package/dist/utils/token-estimator.js +53 -0
- package/dist/utils/tracer.d.ts +7 -0
- package/dist/utils/tracer.js +7 -0
- package/package.json +5 -20
- package/dist/SandboxExecutorFactory.cjs +0 -895
- package/dist/SandboxExecutorFactory.js +0 -893
- package/dist/SandboxExecutorFactory.js.map +0 -1
- package/dist/chunk-VBDAOXYI.cjs +0 -927
- package/dist/chunk-VBDAOXYI.js +0 -832
- package/dist/chunk-VBDAOXYI.js.map +0 -1
- package/dist/chunk.cjs +0 -34
- package/dist/conversations.cjs +0 -7
- package/dist/conversations.js +0 -7
- package/dist/conversations2.cjs +0 -209
- package/dist/conversations2.js +0 -180
- package/dist/conversations2.js.map +0 -1
- package/dist/dbClient.cjs +0 -9676
- package/dist/dbClient.js +0 -9670
- package/dist/dbClient.js.map +0 -1
- package/dist/dbClient2.cjs +0 -5
- package/dist/dbClient2.js +0 -5
- package/dist/env.cjs +0 -59
- package/dist/env.js.map +0 -1
- package/dist/execution-limits.cjs +0 -260
- package/dist/execution-limits.js +0 -63
- package/dist/execution-limits.js.map +0 -1
- package/dist/index.cjs +0 -11260
- package/dist/index.d.cts +0 -36
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/instrumentation.cjs +0 -12
- package/dist/instrumentation.d.cts +0 -18
- package/dist/instrumentation.d.cts.map +0 -1
- package/dist/instrumentation.d.ts.map +0 -1
- package/dist/instrumentation2.cjs +0 -116
- package/dist/instrumentation2.js +0 -69
- package/dist/instrumentation2.js.map +0 -1
- package/dist/json-postprocessor.js +0 -20
- package/dist/json-postprocessor.js.map +0 -1
- package/dist/logger.cjs +0 -5
- package/dist/logger2.cjs +0 -1
- package/dist/nodefs.cjs +0 -29
- package/dist/nodefs.js +0 -27
- package/dist/nodefs.js.map +0 -1
- package/dist/opfs-ahp.cjs +0 -367
- package/dist/opfs-ahp.js +0 -368
- package/dist/opfs-ahp.js.map +0 -1
|
@@ -14,5 +14,4 @@ declare const defaultTextMapPropagator: CompositePropagator;
|
|
|
14
14
|
declare const defaultSDK: NodeSDK;
|
|
15
15
|
declare function flushBatchProcessor(): Promise<void>;
|
|
16
16
|
//#endregion
|
|
17
|
-
export { defaultBatchProcessor, defaultContextManager, defaultInstrumentations, defaultResource, defaultSDK, defaultSpanProcessors, defaultTextMapPropagator, flushBatchProcessor };
|
|
18
|
-
//# sourceMappingURL=instrumentation.d.ts.map
|
|
17
|
+
export { defaultBatchProcessor, defaultContextManager, defaultInstrumentations, defaultResource, defaultSDK, defaultSpanProcessors, defaultTextMapPropagator, flushBatchProcessor };
|
package/dist/instrumentation.js
CHANGED
|
@@ -1,5 +1,68 @@
|
|
|
1
|
-
import "./env.js";
|
|
2
|
-
import "./
|
|
3
|
-
import {
|
|
1
|
+
import { env } from "./env.js";
|
|
2
|
+
import { getLogger } from "./logger.js";
|
|
3
|
+
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
|
|
4
|
+
import { ALLOW_ALL_BAGGAGE_KEYS, BaggageSpanProcessor } from "@opentelemetry/baggage-span-processor";
|
|
5
|
+
import { AsyncLocalStorageContextManager } from "@opentelemetry/context-async-hooks";
|
|
6
|
+
import { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } from "@opentelemetry/core";
|
|
7
|
+
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
|
|
8
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
9
|
+
import { NodeSDK } from "@opentelemetry/sdk-node";
|
|
10
|
+
import { BatchSpanProcessor, NoopSpanProcessor } from "@opentelemetry/sdk-trace-base";
|
|
11
|
+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
4
12
|
|
|
13
|
+
//#region src/instrumentation.ts
|
|
14
|
+
const otlpExporter = new OTLPTraceExporter();
|
|
15
|
+
const logger = getLogger("instrumentation");
|
|
16
|
+
/**
|
|
17
|
+
* Creates a safe batch processor that falls back to no-op when SignOz is not configured
|
|
18
|
+
*/
|
|
19
|
+
function createSafeBatchProcessor() {
|
|
20
|
+
try {
|
|
21
|
+
return new BatchSpanProcessor(otlpExporter, {
|
|
22
|
+
scheduledDelayMillis: env.OTEL_BSP_SCHEDULE_DELAY,
|
|
23
|
+
maxExportBatchSize: env.OTEL_BSP_MAX_EXPORT_BATCH_SIZE
|
|
24
|
+
});
|
|
25
|
+
} catch (error) {
|
|
26
|
+
logger.warn({ error }, "Failed to create batch processor");
|
|
27
|
+
return new NoopSpanProcessor();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const defaultBatchProcessor = createSafeBatchProcessor();
|
|
31
|
+
const defaultResource = resourceFromAttributes({ [ATTR_SERVICE_NAME]: "inkeep-agents-run-api" });
|
|
32
|
+
const defaultInstrumentations = [getNodeAutoInstrumentations({
|
|
33
|
+
"@opentelemetry/instrumentation-http": {
|
|
34
|
+
enabled: true,
|
|
35
|
+
requestHook: (span, request) => {
|
|
36
|
+
const url = request?.url ?? request?.path;
|
|
37
|
+
if (!url) return;
|
|
38
|
+
const u = new URL(url, "http://localhost");
|
|
39
|
+
span.updateName(`${request?.method || "UNKNOWN"} ${u.pathname}`);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"@opentelemetry/instrumentation-undici": { requestHook: (span) => {
|
|
43
|
+
const method = span.attributes?.["http.request.method"];
|
|
44
|
+
const host = span.attributes?.["server.address"];
|
|
45
|
+
const path = span.attributes?.["url.path"];
|
|
46
|
+
if (method && path) span.updateName(host ? `${method} ${host}${path}` : `${method} ${path}`);
|
|
47
|
+
} }
|
|
48
|
+
})];
|
|
49
|
+
const defaultSpanProcessors = [new BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS), defaultBatchProcessor];
|
|
50
|
+
const defaultContextManager = new AsyncLocalStorageContextManager();
|
|
51
|
+
const defaultTextMapPropagator = new CompositePropagator({ propagators: [new W3CTraceContextPropagator(), new W3CBaggagePropagator()] });
|
|
52
|
+
const defaultSDK = new NodeSDK({
|
|
53
|
+
resource: defaultResource,
|
|
54
|
+
contextManager: defaultContextManager,
|
|
55
|
+
textMapPropagator: defaultTextMapPropagator,
|
|
56
|
+
spanProcessors: defaultSpanProcessors,
|
|
57
|
+
instrumentations: defaultInstrumentations
|
|
58
|
+
});
|
|
59
|
+
async function flushBatchProcessor() {
|
|
60
|
+
try {
|
|
61
|
+
await defaultBatchProcessor.forceFlush();
|
|
62
|
+
} catch (error) {
|
|
63
|
+
logger.warn({ error }, "Failed to flush batch processor");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
5
68
|
export { defaultBatchProcessor, defaultContextManager, defaultInstrumentations, defaultResource, defaultSDK, defaultSpanProcessors, defaultTextMapPropagator, flushBatchProcessor };
|
package/dist/logger.js
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ExecutionContext } from "@inkeep/agents-core";
|
|
2
|
+
import * as hono0 from "hono";
|
|
3
|
+
|
|
4
|
+
//#region src/middleware/api-key-auth.d.ts
|
|
5
|
+
declare const apiKeyAuth: () => hono0.MiddlewareHandler<{
|
|
6
|
+
Variables: {
|
|
7
|
+
executionContext: ExecutionContext;
|
|
8
|
+
};
|
|
9
|
+
}, string, {}, Response>;
|
|
10
|
+
declare const extractContextFromApiKey: (apiKey: string, baseUrl?: string) => Promise<ExecutionContext>;
|
|
11
|
+
/**
|
|
12
|
+
* Extract execution context from a team agent JWT token
|
|
13
|
+
* Team agent tokens are used for intra-tenant agent delegation
|
|
14
|
+
*/
|
|
15
|
+
declare const extractContextFromTeamAgentToken: (token: string, baseUrl?: string, expectedSubAgentId?: string) => Promise<ExecutionContext>;
|
|
16
|
+
/**
|
|
17
|
+
* Helper middleware for endpoints that optionally support API key authentication
|
|
18
|
+
* If no auth header is present, it continues without setting the executionContext
|
|
19
|
+
*/
|
|
20
|
+
declare const optionalAuth: () => hono0.MiddlewareHandler<{
|
|
21
|
+
Variables: {
|
|
22
|
+
executionContext?: ExecutionContext;
|
|
23
|
+
};
|
|
24
|
+
}, string, {}, Response>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { apiKeyAuth, extractContextFromApiKey, extractContextFromTeamAgentToken, optionalAuth };
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { env } from "../env.js";
|
|
2
|
+
import { getLogger } from "../logger.js";
|
|
3
|
+
import dbClient_default from "../data/db/dbClient.js";
|
|
4
|
+
import { createExecutionContext } from "../types/execution-context.js";
|
|
5
|
+
import { getAgentById, validateAndGetApiKey, validateTargetAgent, verifyServiceToken, verifyTempToken } from "@inkeep/agents-core";
|
|
6
|
+
import { HTTPException } from "hono/http-exception";
|
|
7
|
+
import { createMiddleware } from "hono/factory";
|
|
8
|
+
|
|
9
|
+
//#region src/middleware/api-key-auth.ts
|
|
10
|
+
const logger = getLogger("env-key-auth");
|
|
11
|
+
/**
|
|
12
|
+
* Attempts to authenticate using a JWT temporary token
|
|
13
|
+
* Returns execution context if successful, null if token is invalid or not a JWT
|
|
14
|
+
*/
|
|
15
|
+
async function tryAuthenticateWithTempJwt(apiKey, baseUrl, subAgentId) {
|
|
16
|
+
if (!apiKey.startsWith("eyJ") || !env.INKEEP_AGENTS_TEMP_JWT_PUBLIC_KEY) return null;
|
|
17
|
+
try {
|
|
18
|
+
const payload = await verifyTempToken(Buffer.from(env.INKEEP_AGENTS_TEMP_JWT_PUBLIC_KEY, "base64").toString("utf-8"), apiKey);
|
|
19
|
+
logger.info({}, "JWT temp token authenticated successfully");
|
|
20
|
+
return createExecutionContext({
|
|
21
|
+
apiKey,
|
|
22
|
+
tenantId: payload.tenantId,
|
|
23
|
+
projectId: payload.projectId,
|
|
24
|
+
agentId: payload.agentId,
|
|
25
|
+
apiKeyId: "temp-jwt",
|
|
26
|
+
baseUrl,
|
|
27
|
+
subAgentId,
|
|
28
|
+
metadata: { initiatedBy: payload.initiatedBy }
|
|
29
|
+
});
|
|
30
|
+
} catch (error) {
|
|
31
|
+
logger.debug({ error }, "JWT verification failed");
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const apiKeyAuth = () => createMiddleware(async (c, next) => {
|
|
36
|
+
if (c.req.method === "OPTIONS") {
|
|
37
|
+
await next();
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const authHeader = c.req.header("Authorization");
|
|
41
|
+
const tenantId = c.req.header("x-inkeep-tenant-id");
|
|
42
|
+
const projectId = c.req.header("x-inkeep-project-id");
|
|
43
|
+
const agentId = c.req.header("x-inkeep-agent-id");
|
|
44
|
+
const subAgentId = c.req.header("x-inkeep-sub-agent-id");
|
|
45
|
+
const proto = c.req.header("x-forwarded-proto")?.split(",")[0].trim();
|
|
46
|
+
const host = c.req.header("x-forwarded-host")?.split(",")[0].trim() ?? c.req.header("host");
|
|
47
|
+
const reqUrl = new URL(c.req.url);
|
|
48
|
+
const baseUrl = proto && host ? `${proto}://${host}` : host ? `${reqUrl.protocol}//${host}` : `${reqUrl.origin}`;
|
|
49
|
+
if (process.env.ENVIRONMENT === "development" || process.env.ENVIRONMENT === "test") {
|
|
50
|
+
logger.info({}, "development environment");
|
|
51
|
+
let executionContext;
|
|
52
|
+
if (authHeader?.startsWith("Bearer ")) {
|
|
53
|
+
const apiKey$1 = authHeader.substring(7);
|
|
54
|
+
const jwtContext$1 = await tryAuthenticateWithTempJwt(apiKey$1, baseUrl, subAgentId);
|
|
55
|
+
if (jwtContext$1) {
|
|
56
|
+
c.set("executionContext", jwtContext$1);
|
|
57
|
+
await next();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
executionContext = await extractContextFromApiKey(apiKey$1, baseUrl);
|
|
62
|
+
if (subAgentId) executionContext.subAgentId = subAgentId;
|
|
63
|
+
c.set("executionContext", executionContext);
|
|
64
|
+
} catch {
|
|
65
|
+
try {
|
|
66
|
+
executionContext = await extractContextFromTeamAgentToken(apiKey$1, baseUrl, subAgentId);
|
|
67
|
+
c.set("executionContext", executionContext);
|
|
68
|
+
} catch {
|
|
69
|
+
executionContext = createExecutionContext({
|
|
70
|
+
apiKey: "development",
|
|
71
|
+
tenantId: tenantId || "test-tenant",
|
|
72
|
+
projectId: projectId || "test-project",
|
|
73
|
+
agentId: agentId || "test-agent",
|
|
74
|
+
apiKeyId: "test-key",
|
|
75
|
+
baseUrl,
|
|
76
|
+
subAgentId
|
|
77
|
+
});
|
|
78
|
+
c.set("executionContext", executionContext);
|
|
79
|
+
logger.info({}, "Development/test environment - fallback to default context due to invalid API key");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
executionContext = createExecutionContext({
|
|
84
|
+
apiKey: "development",
|
|
85
|
+
tenantId: tenantId || "test-tenant",
|
|
86
|
+
projectId: projectId || "test-project",
|
|
87
|
+
agentId: agentId || "test-agent",
|
|
88
|
+
apiKeyId: "test-key",
|
|
89
|
+
baseUrl,
|
|
90
|
+
subAgentId
|
|
91
|
+
});
|
|
92
|
+
c.set("executionContext", executionContext);
|
|
93
|
+
logger.info({}, "Development/test environment - no API key provided, using default context");
|
|
94
|
+
}
|
|
95
|
+
await next();
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) throw new HTTPException(401, { message: "Missing or invalid authorization header. Expected: Bearer <api_key>" });
|
|
99
|
+
const apiKey = authHeader.substring(7);
|
|
100
|
+
const jwtContext = await tryAuthenticateWithTempJwt(apiKey, baseUrl, subAgentId);
|
|
101
|
+
if (jwtContext) {
|
|
102
|
+
c.set("executionContext", jwtContext);
|
|
103
|
+
await next();
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (env.INKEEP_AGENTS_RUN_API_BYPASS_SECRET) {
|
|
107
|
+
if (apiKey === env.INKEEP_AGENTS_RUN_API_BYPASS_SECRET) {
|
|
108
|
+
if (!tenantId || !projectId || !agentId) throw new HTTPException(401, { message: "Missing or invalid tenant, project, or agent ID" });
|
|
109
|
+
const executionContext = createExecutionContext({
|
|
110
|
+
apiKey,
|
|
111
|
+
tenantId,
|
|
112
|
+
projectId,
|
|
113
|
+
agentId,
|
|
114
|
+
apiKeyId: "bypass",
|
|
115
|
+
baseUrl,
|
|
116
|
+
subAgentId
|
|
117
|
+
});
|
|
118
|
+
c.set("executionContext", executionContext);
|
|
119
|
+
logger.info({}, "Bypass secret authenticated successfully");
|
|
120
|
+
await next();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (apiKey) {
|
|
124
|
+
try {
|
|
125
|
+
const executionContext = await extractContextFromApiKey(apiKey, baseUrl);
|
|
126
|
+
if (subAgentId) executionContext.subAgentId = subAgentId;
|
|
127
|
+
c.set("executionContext", executionContext);
|
|
128
|
+
logger.info({}, "API key authenticated successfully");
|
|
129
|
+
} catch {
|
|
130
|
+
const executionContext = await extractContextFromTeamAgentToken(apiKey, baseUrl, subAgentId);
|
|
131
|
+
c.set("executionContext", executionContext);
|
|
132
|
+
}
|
|
133
|
+
await next();
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
throw new HTTPException(401, { message: "Invalid Token" });
|
|
137
|
+
}
|
|
138
|
+
if (!apiKey || apiKey.length < 16) throw new HTTPException(401, { message: "Invalid API key format" });
|
|
139
|
+
try {
|
|
140
|
+
const executionContext = await extractContextFromApiKey(apiKey, baseUrl);
|
|
141
|
+
if (subAgentId) executionContext.subAgentId = subAgentId;
|
|
142
|
+
c.set("executionContext", executionContext);
|
|
143
|
+
logger.debug({
|
|
144
|
+
tenantId: executionContext.tenantId,
|
|
145
|
+
projectId: executionContext.projectId,
|
|
146
|
+
agentId: executionContext.agentId,
|
|
147
|
+
subAgentId: executionContext.subAgentId
|
|
148
|
+
}, "API key authenticated successfully");
|
|
149
|
+
await next();
|
|
150
|
+
} catch {
|
|
151
|
+
try {
|
|
152
|
+
const executionContext = await extractContextFromTeamAgentToken(apiKey, baseUrl, subAgentId);
|
|
153
|
+
c.set("executionContext", executionContext);
|
|
154
|
+
await next();
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (error instanceof HTTPException) throw error;
|
|
157
|
+
logger.error({ error }, "API key authentication error");
|
|
158
|
+
throw new HTTPException(500, { message: "Authentication failed" });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const extractContextFromApiKey = async (apiKey, baseUrl) => {
|
|
163
|
+
const apiKeyRecord = await validateAndGetApiKey(apiKey, dbClient_default);
|
|
164
|
+
if (!apiKeyRecord) throw new HTTPException(401, { message: "Invalid or expired API key" });
|
|
165
|
+
const agent = await getAgentById(dbClient_default)({ scopes: {
|
|
166
|
+
tenantId: apiKeyRecord.tenantId,
|
|
167
|
+
projectId: apiKeyRecord.projectId,
|
|
168
|
+
agentId: apiKeyRecord.agentId
|
|
169
|
+
} });
|
|
170
|
+
if (!agent) throw new HTTPException(401, { message: "Invalid or expired API key" });
|
|
171
|
+
logger.debug({
|
|
172
|
+
tenantId: apiKeyRecord.tenantId,
|
|
173
|
+
projectId: apiKeyRecord.projectId,
|
|
174
|
+
agentId: apiKeyRecord.agentId,
|
|
175
|
+
subAgentId: agent.defaultSubAgentId || void 0
|
|
176
|
+
}, "API key authenticated successfully");
|
|
177
|
+
return createExecutionContext({
|
|
178
|
+
apiKey,
|
|
179
|
+
tenantId: apiKeyRecord.tenantId,
|
|
180
|
+
projectId: apiKeyRecord.projectId,
|
|
181
|
+
agentId: apiKeyRecord.agentId,
|
|
182
|
+
apiKeyId: apiKeyRecord.id,
|
|
183
|
+
baseUrl,
|
|
184
|
+
subAgentId: agent.defaultSubAgentId || void 0
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Extract execution context from a team agent JWT token
|
|
189
|
+
* Team agent tokens are used for intra-tenant agent delegation
|
|
190
|
+
*/
|
|
191
|
+
const extractContextFromTeamAgentToken = async (token, baseUrl, expectedSubAgentId) => {
|
|
192
|
+
const result = await verifyServiceToken(token);
|
|
193
|
+
if (!result.valid || !result.payload) {
|
|
194
|
+
logger.warn({ error: result.error }, "Invalid team agent JWT token");
|
|
195
|
+
throw new HTTPException(401, { message: `Invalid team agent token: ${result.error || "Unknown error"}` });
|
|
196
|
+
}
|
|
197
|
+
const payload = result.payload;
|
|
198
|
+
if (expectedSubAgentId && !validateTargetAgent(payload, expectedSubAgentId)) {
|
|
199
|
+
logger.error({
|
|
200
|
+
tokenTargetAgentId: payload.aud,
|
|
201
|
+
expectedSubAgentId,
|
|
202
|
+
originAgentId: payload.sub
|
|
203
|
+
}, "Team agent token target mismatch");
|
|
204
|
+
throw new HTTPException(403, { message: "Token not valid for the requested agent" });
|
|
205
|
+
}
|
|
206
|
+
logger.info({
|
|
207
|
+
originAgentId: payload.sub,
|
|
208
|
+
targetAgentId: payload.aud,
|
|
209
|
+
tenantId: payload.tenantId,
|
|
210
|
+
projectId: payload.projectId
|
|
211
|
+
}, "Team agent JWT token authenticated successfully");
|
|
212
|
+
return createExecutionContext({
|
|
213
|
+
apiKey: "team-agent-jwt",
|
|
214
|
+
tenantId: payload.tenantId,
|
|
215
|
+
projectId: payload.projectId,
|
|
216
|
+
agentId: payload.aud,
|
|
217
|
+
apiKeyId: "team-agent-token",
|
|
218
|
+
baseUrl,
|
|
219
|
+
subAgentId: void 0,
|
|
220
|
+
metadata: {
|
|
221
|
+
teamDelegation: true,
|
|
222
|
+
originAgentId: payload.sub
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Helper middleware for endpoints that optionally support API key authentication
|
|
228
|
+
* If no auth header is present, it continues without setting the executionContext
|
|
229
|
+
*/
|
|
230
|
+
const optionalAuth = () => createMiddleware(async (c, next) => {
|
|
231
|
+
const authHeader = c.req.header("Authorization");
|
|
232
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
233
|
+
await next();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
return apiKeyAuth()(c, next);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
//#endregion
|
|
240
|
+
export { apiKeyAuth, extractContextFromApiKey, extractContextFromTeamAgentToken, optionalAuth };
|
package/dist/openapi.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { env } from "./env.js";
|
|
2
|
+
import { swaggerUI } from "@hono/swagger-ui";
|
|
3
|
+
|
|
4
|
+
//#region src/openapi.ts
|
|
5
|
+
function setupOpenAPIRoutes(app) {
|
|
6
|
+
app.get("/openapi.json", (c) => {
|
|
7
|
+
try {
|
|
8
|
+
const serverUrl = process.env.VERCEL_ENV === "production" && process.env.VERCEL_PROJECT_PRODUCTION_URL ? `https://${process.env.VERCEL_PROJECT_PRODUCTION_URL}` : process.env.VERCEL_ENV === "preview" && process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : env.INKEEP_AGENTS_RUN_API_URL;
|
|
9
|
+
const document = app.getOpenAPIDocument({
|
|
10
|
+
openapi: "3.0.0",
|
|
11
|
+
info: {
|
|
12
|
+
title: "Inkeep Agents Run API",
|
|
13
|
+
version: "1.0.0",
|
|
14
|
+
description: "Chat completions, MCP, and A2A run endpoints in the Inkeep Agent Framework."
|
|
15
|
+
},
|
|
16
|
+
servers: [{
|
|
17
|
+
url: serverUrl,
|
|
18
|
+
description: "API Server"
|
|
19
|
+
}]
|
|
20
|
+
});
|
|
21
|
+
document.components = {
|
|
22
|
+
...document.components,
|
|
23
|
+
securitySchemes: {
|
|
24
|
+
...document.components?.securitySchemes || {},
|
|
25
|
+
bearerAuth: {
|
|
26
|
+
type: "http",
|
|
27
|
+
scheme: "bearer",
|
|
28
|
+
bearerFormat: "API Key",
|
|
29
|
+
description: "API key authentication. All endpoints require a valid API key in the Authorization header as \"Bearer <api-key>\". API keys can be created in the management UI."
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
document.security = [{ bearerAuth: [] }];
|
|
34
|
+
return c.json(document);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.error("OpenAPI document generation failed:", error);
|
|
37
|
+
const errorDetails = error instanceof Error ? {
|
|
38
|
+
message: error.message,
|
|
39
|
+
stack: error.stack
|
|
40
|
+
} : JSON.stringify(error, null, 2);
|
|
41
|
+
return c.json({
|
|
42
|
+
error: "Failed to generate OpenAPI document",
|
|
43
|
+
details: errorDetails
|
|
44
|
+
}, 500);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
app.get("/docs", swaggerUI({
|
|
48
|
+
url: "/openapi.json",
|
|
49
|
+
title: "Inkeep Agents Run API Documentation"
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { setupOpenAPIRoutes };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
3
|
+
|
|
4
|
+
//#region src/routes/agents.d.ts
|
|
5
|
+
type AppVariables = {
|
|
6
|
+
credentialStores: CredentialStoreRegistry;
|
|
7
|
+
};
|
|
8
|
+
declare const app: OpenAPIHono<{
|
|
9
|
+
Variables: AppVariables;
|
|
10
|
+
}, {}, "/">;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { app as default };
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { getLogger } from "../logger.js";
|
|
2
|
+
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { a2aHandler } from "../a2a/handlers.js";
|
|
4
|
+
import { getRegisteredAgent } from "../data/agents.js";
|
|
5
|
+
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
6
|
+
import { HeadersScopeSchema, createApiError, getAgentWithDefaultSubAgent, getRequestExecutionContext } from "@inkeep/agents-core";
|
|
7
|
+
|
|
8
|
+
//#region src/routes/agents.ts
|
|
9
|
+
const app = new OpenAPIHono();
|
|
10
|
+
const logger = getLogger("agents");
|
|
11
|
+
app.openapi(createRoute({
|
|
12
|
+
method: "get",
|
|
13
|
+
path: "/.well-known/agent.json",
|
|
14
|
+
request: { headers: HeadersScopeSchema },
|
|
15
|
+
tags: ["a2a"],
|
|
16
|
+
security: [{ bearerAuth: [] }],
|
|
17
|
+
responses: {
|
|
18
|
+
200: {
|
|
19
|
+
description: "Agent Card for A2A discovery",
|
|
20
|
+
content: { "application/json": { schema: z.object({
|
|
21
|
+
name: z.string(),
|
|
22
|
+
description: z.string().optional(),
|
|
23
|
+
url: z.string(),
|
|
24
|
+
version: z.string(),
|
|
25
|
+
defaultInputModes: z.array(z.string()),
|
|
26
|
+
defaultOutputModes: z.array(z.string()),
|
|
27
|
+
skills: z.array(z.any())
|
|
28
|
+
}) } }
|
|
29
|
+
},
|
|
30
|
+
404: { description: "Agent not found" }
|
|
31
|
+
}
|
|
32
|
+
}), async (c) => {
|
|
33
|
+
const otelHeaders = {
|
|
34
|
+
traceparent: c.req.header("traceparent"),
|
|
35
|
+
tracestate: c.req.header("tracestate"),
|
|
36
|
+
baggage: c.req.header("baggage")
|
|
37
|
+
};
|
|
38
|
+
logger.info({
|
|
39
|
+
otelHeaders,
|
|
40
|
+
path: c.req.path,
|
|
41
|
+
method: c.req.method
|
|
42
|
+
}, "OpenTelemetry headers: well-known agent.json");
|
|
43
|
+
const executionContext = getRequestExecutionContext(c);
|
|
44
|
+
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
45
|
+
logger.info({ executionContext }, "executionContext");
|
|
46
|
+
logger.info({
|
|
47
|
+
message: "getRegisteredAgent (agent-level)",
|
|
48
|
+
tenantId,
|
|
49
|
+
projectId,
|
|
50
|
+
agentId,
|
|
51
|
+
subAgentId
|
|
52
|
+
}, "agent-level well-known agent.json");
|
|
53
|
+
const agent = await getRegisteredAgent({
|
|
54
|
+
executionContext,
|
|
55
|
+
credentialStoreRegistry: c.get("credentialStores"),
|
|
56
|
+
sandboxConfig: c.get("sandboxConfig")
|
|
57
|
+
});
|
|
58
|
+
logger.info({ agent }, "agent registered: well-known agent.json");
|
|
59
|
+
if (!agent) throw createApiError({
|
|
60
|
+
code: "not_found",
|
|
61
|
+
message: "Agent not found"
|
|
62
|
+
});
|
|
63
|
+
return c.json(agent.agentCard);
|
|
64
|
+
});
|
|
65
|
+
app.post("/a2a", async (c) => {
|
|
66
|
+
const otelHeaders = {
|
|
67
|
+
traceparent: c.req.header("traceparent"),
|
|
68
|
+
tracestate: c.req.header("tracestate"),
|
|
69
|
+
baggage: c.req.header("baggage")
|
|
70
|
+
};
|
|
71
|
+
logger.info({
|
|
72
|
+
otelHeaders,
|
|
73
|
+
path: c.req.path,
|
|
74
|
+
method: c.req.method
|
|
75
|
+
}, "OpenTelemetry headers: a2a");
|
|
76
|
+
const executionContext = getRequestExecutionContext(c);
|
|
77
|
+
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
78
|
+
if (subAgentId) {
|
|
79
|
+
logger.info({
|
|
80
|
+
message: "a2a (agent-level)",
|
|
81
|
+
tenantId,
|
|
82
|
+
projectId,
|
|
83
|
+
agentId,
|
|
84
|
+
subAgentId
|
|
85
|
+
}, "agent-level a2a endpoint");
|
|
86
|
+
const agent$1 = await getRegisteredAgent({
|
|
87
|
+
executionContext,
|
|
88
|
+
credentialStoreRegistry: c.get("credentialStores"),
|
|
89
|
+
sandboxConfig: c.get("sandboxConfig")
|
|
90
|
+
});
|
|
91
|
+
if (!agent$1) return c.json({
|
|
92
|
+
jsonrpc: "2.0",
|
|
93
|
+
error: {
|
|
94
|
+
code: -32004,
|
|
95
|
+
message: "Agent not found"
|
|
96
|
+
},
|
|
97
|
+
id: null
|
|
98
|
+
}, 404);
|
|
99
|
+
return a2aHandler(c, agent$1);
|
|
100
|
+
}
|
|
101
|
+
logger.info({
|
|
102
|
+
message: "a2a (agent-level)",
|
|
103
|
+
tenantId,
|
|
104
|
+
projectId,
|
|
105
|
+
agentId
|
|
106
|
+
}, "agent-level a2a endpoint");
|
|
107
|
+
const agent = await getAgentWithDefaultSubAgent(dbClient_default)({ scopes: {
|
|
108
|
+
tenantId,
|
|
109
|
+
projectId,
|
|
110
|
+
agentId
|
|
111
|
+
} });
|
|
112
|
+
if (!agent) return c.json({
|
|
113
|
+
jsonrpc: "2.0",
|
|
114
|
+
error: {
|
|
115
|
+
code: -32004,
|
|
116
|
+
message: "Agent not found"
|
|
117
|
+
},
|
|
118
|
+
id: null
|
|
119
|
+
}, 404);
|
|
120
|
+
if (!agent.defaultSubAgentId) return c.json({
|
|
121
|
+
jsonrpc: "2.0",
|
|
122
|
+
error: {
|
|
123
|
+
code: -32004,
|
|
124
|
+
message: "Agent does not have a default agent configured"
|
|
125
|
+
},
|
|
126
|
+
id: null
|
|
127
|
+
}, 400);
|
|
128
|
+
executionContext.subAgentId = agent.defaultSubAgentId;
|
|
129
|
+
const defaultSubAgent = await getRegisteredAgent({
|
|
130
|
+
executionContext,
|
|
131
|
+
credentialStoreRegistry: c.get("credentialStores"),
|
|
132
|
+
sandboxConfig: c.get("sandboxConfig")
|
|
133
|
+
});
|
|
134
|
+
if (!defaultSubAgent) return c.json({
|
|
135
|
+
jsonrpc: "2.0",
|
|
136
|
+
error: {
|
|
137
|
+
code: -32004,
|
|
138
|
+
message: "Agent not found"
|
|
139
|
+
},
|
|
140
|
+
id: null
|
|
141
|
+
}, 404);
|
|
142
|
+
return a2aHandler(c, defaultSubAgent);
|
|
143
|
+
});
|
|
144
|
+
var agents_default = app;
|
|
145
|
+
|
|
146
|
+
//#endregion
|
|
147
|
+
export { agents_default as default };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
3
|
+
|
|
4
|
+
//#region src/routes/chat.d.ts
|
|
5
|
+
type AppVariables = {
|
|
6
|
+
credentialStores: CredentialStoreRegistry;
|
|
7
|
+
requestBody?: any;
|
|
8
|
+
};
|
|
9
|
+
declare const app: OpenAPIHono<{
|
|
10
|
+
Variables: AppVariables;
|
|
11
|
+
}, {}, "/">;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { app as default };
|