@inkeep/agents-run-api 0.0.0-dev-20260113172432 → 0.0.0-dev-20260115183047
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 +2 -1
- package/dist/a2a/client.d.ts +3 -1
- package/dist/a2a/client.js +23 -9
- package/dist/a2a/handlers.js +11 -5
- package/dist/a2a/transfer.d.ts +6 -1
- package/dist/a2a/transfer.js +10 -6
- package/dist/agents/Agent.d.ts +8 -4
- package/dist/agents/Agent.js +226 -92
- package/dist/agents/ToolSessionManager.js +2 -2
- package/dist/agents/generateTaskHandler.d.ts +4 -9
- package/dist/agents/generateTaskHandler.js +107 -246
- package/dist/agents/relationTools.d.ts +19 -8
- package/dist/agents/relationTools.js +105 -14
- package/dist/agents/types.d.ts +4 -2
- package/dist/agents/versions/v1/Phase1Config.d.ts +4 -3
- package/dist/agents/versions/v1/Phase1Config.js +33 -20
- package/dist/constants/execution-limits/index.js +1 -1
- package/dist/context/ContextFetcher.d.ts +68 -0
- package/dist/context/ContextFetcher.js +276 -0
- package/dist/context/ContextResolver.d.ts +56 -0
- package/dist/context/ContextResolver.js +273 -0
- package/dist/context/context.d.ts +19 -0
- package/dist/context/context.js +108 -0
- package/dist/context/contextCache.d.ts +56 -0
- package/dist/context/contextCache.js +175 -0
- package/dist/context/index.d.ts +6 -0
- package/dist/context/index.js +7 -0
- package/dist/context/validation.d.ts +39 -0
- package/dist/context/validation.js +255 -0
- package/dist/create-app.d.ts +2 -2
- package/dist/create-app.js +7 -2
- package/dist/data/agent.d.ts +2 -2
- package/dist/data/agent.js +17 -22
- package/dist/data/agents.d.ts +2 -2
- package/dist/data/agents.js +34 -42
- package/dist/data/conversations.d.ts +2 -1
- package/dist/data/conversations.js +7 -8
- package/dist/data/db/dbClient.d.ts +2 -2
- package/dist/data/db/dbClient.js +2 -10
- package/dist/env.d.ts +6 -2
- package/dist/env.js +3 -1
- package/dist/handlers/executionHandler.d.ts +3 -2
- package/dist/handlers/executionHandler.js +46 -22
- package/dist/index.d.ts +3 -3
- package/dist/middleware/api-key-auth.d.ts +4 -10
- package/dist/middleware/api-key-auth.js +164 -163
- package/dist/middleware/index.d.ts +3 -2
- package/dist/middleware/index.js +3 -2
- package/dist/middleware/projectConfig.d.ts +19 -0
- package/dist/middleware/projectConfig.js +80 -0
- package/dist/routes/agents.d.ts +2 -1
- package/dist/routes/agents.js +7 -13
- package/dist/routes/chat.d.ts +2 -1
- package/dist/routes/chat.js +29 -52
- package/dist/routes/chatDataStream.d.ts +2 -1
- package/dist/routes/chatDataStream.js +28 -30
- package/dist/routes/mcp.d.ts +2 -1
- package/dist/routes/mcp.js +25 -37
- package/dist/services/AgentSession.d.ts +5 -7
- package/dist/services/AgentSession.js +25 -34
- package/dist/services/ArtifactParser.d.ts +2 -2
- package/dist/services/ArtifactParser.js +2 -2
- package/dist/services/ArtifactService.d.ts +2 -3
- package/dist/services/ArtifactService.js +23 -20
- package/dist/services/BaseCompressor.js +2 -2
- package/dist/services/IncrementalStreamParser.d.ts +2 -2
- package/dist/services/IncrementalStreamParser.js +2 -2
- package/dist/services/ResponseFormatter.d.ts +2 -2
- package/dist/services/ResponseFormatter.js +2 -2
- package/dist/services/evaluationHttpClient.d.ts +21 -0
- package/dist/services/evaluationHttpClient.js +48 -0
- package/dist/services/evaluationRunConfigMatcher.d.ts +6 -0
- package/dist/services/evaluationRunConfigMatcher.js +7 -0
- package/dist/tools/NativeSandboxExecutor.js +1 -1
- package/dist/tools/distill-conversation-history-tool.js +2 -2
- package/dist/tools/distill-conversation-tool.js +2 -2
- package/dist/types/execution-context.d.ts +8 -14
- package/dist/types/execution-context.js +3 -2
- package/dist/utils/model-resolver.d.ts +2 -2
- package/dist/utils/model-resolver.js +3 -13
- package/dist/utils/project.d.ts +207 -0
- package/dist/utils/project.js +315 -0
- package/dist/utils/token-estimator.d.ts +6 -52
- package/dist/utils/token-estimator.js +2 -38
- package/package.json +2 -2
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { env } from "../env.js";
|
|
2
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
3
|
+
import { InternalServices, ManageApiError, ManagementApiClient } from "@inkeep/agents-core";
|
|
4
|
+
import { createMiddleware } from "hono/factory";
|
|
5
|
+
|
|
6
|
+
//#region src/middleware/projectConfig.ts
|
|
7
|
+
const logger = getLogger$1("projectConfigMiddleware");
|
|
8
|
+
/**
|
|
9
|
+
* Middleware that fetches the full project definition from the Management API
|
|
10
|
+
* and adds it to the Hono context for use in route handlers.
|
|
11
|
+
*
|
|
12
|
+
* This middleware should be applied after authentication middleware since it
|
|
13
|
+
* requires the execution context to be set.
|
|
14
|
+
*/
|
|
15
|
+
const projectConfigMiddleware = createMiddleware(async (c, next) => {
|
|
16
|
+
const executionContext = c.get("executionContext");
|
|
17
|
+
const { tenantId, projectId, ref } = executionContext;
|
|
18
|
+
logger.debug({
|
|
19
|
+
tenantId,
|
|
20
|
+
projectId,
|
|
21
|
+
ref
|
|
22
|
+
}, "Fetching project config from Management API");
|
|
23
|
+
try {
|
|
24
|
+
const client = new ManagementApiClient({
|
|
25
|
+
apiUrl: env.INKEEP_AGENTS_MANAGE_API_URL,
|
|
26
|
+
tenantId,
|
|
27
|
+
projectId,
|
|
28
|
+
auth: {
|
|
29
|
+
mode: "internalService",
|
|
30
|
+
internalServiceName: InternalServices.INKEEP_AGENTS_RUN_API
|
|
31
|
+
},
|
|
32
|
+
ref
|
|
33
|
+
});
|
|
34
|
+
const resolvedRef = await client.getResolvedRef();
|
|
35
|
+
if (!resolvedRef) throw new Error("Resolved ref not found");
|
|
36
|
+
if (resolvedRef.type !== "branch") throw new Error(`Runtime operations require a branch ref. Got ${resolvedRef.type} '${resolvedRef.name}'.`);
|
|
37
|
+
const projectConfig = await client.getFullProject();
|
|
38
|
+
c.set("executionContext", {
|
|
39
|
+
...executionContext,
|
|
40
|
+
project: projectConfig,
|
|
41
|
+
resolvedRef
|
|
42
|
+
});
|
|
43
|
+
logger.debug({
|
|
44
|
+
tenantId,
|
|
45
|
+
projectId,
|
|
46
|
+
agentCount: Object.keys(projectConfig.agents || {}).length,
|
|
47
|
+
toolCount: Object.keys(projectConfig.tools || {}).length
|
|
48
|
+
}, "Project config fetched successfully");
|
|
49
|
+
await next();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof ManageApiError) {
|
|
52
|
+
logger.error({
|
|
53
|
+
tenantId,
|
|
54
|
+
projectId,
|
|
55
|
+
statusCode: error.statusCode,
|
|
56
|
+
message: error.message
|
|
57
|
+
}, "Failed to fetch project config from Management API");
|
|
58
|
+
if (error.isNotFound) return c.json({
|
|
59
|
+
error: "Project not found",
|
|
60
|
+
message: `Project ${projectId} not found for tenant ${tenantId}`
|
|
61
|
+
}, 404);
|
|
62
|
+
if (error.isUnauthorized || error.isForbidden) return c.json({
|
|
63
|
+
error: "Access denied",
|
|
64
|
+
message: "Unable to access project configuration"
|
|
65
|
+
}, 403);
|
|
66
|
+
}
|
|
67
|
+
logger.error({
|
|
68
|
+
tenantId,
|
|
69
|
+
projectId,
|
|
70
|
+
error: error instanceof Error ? error.message : String(error)
|
|
71
|
+
}, "Unexpected error fetching project config");
|
|
72
|
+
return c.json({
|
|
73
|
+
error: "Internal server error",
|
|
74
|
+
message: "Failed to load project configuration"
|
|
75
|
+
}, 500);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { projectConfigMiddleware };
|
package/dist/routes/agents.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
2
|
+
import { CredentialStoreRegistry, ResolvedRef } from "@inkeep/agents-core";
|
|
3
3
|
|
|
4
4
|
//#region src/routes/agents.d.ts
|
|
5
5
|
type AppVariables = {
|
|
6
6
|
credentialStores: CredentialStoreRegistry;
|
|
7
|
+
ref: ResolvedRef;
|
|
7
8
|
};
|
|
8
9
|
declare const app: OpenAPIHono<{
|
|
9
10
|
Variables: AppVariables;
|
package/dist/routes/agents.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { getLogger } from "../logger.js";
|
|
2
|
-
import dbClient_default from "../data/db/dbClient.js";
|
|
1
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
3
2
|
import { a2aHandler } from "../a2a/handlers.js";
|
|
4
3
|
import { getRegisteredAgent } from "../data/agents.js";
|
|
5
4
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
6
|
-
import { HeadersScopeSchema, createApiError
|
|
5
|
+
import { HeadersScopeSchema, createApiError } from "@inkeep/agents-core";
|
|
7
6
|
|
|
8
7
|
//#region src/routes/agents.ts
|
|
9
8
|
const app = new OpenAPIHono();
|
|
10
|
-
const logger = getLogger("agents");
|
|
9
|
+
const logger = getLogger$1("agents");
|
|
11
10
|
app.openapi(createRoute({
|
|
12
11
|
method: "get",
|
|
13
12
|
path: "/.well-known/agent.json",
|
|
@@ -40,9 +39,8 @@ app.openapi(createRoute({
|
|
|
40
39
|
path: c.req.path,
|
|
41
40
|
method: c.req.method
|
|
42
41
|
}, "OpenTelemetry headers: well-known agent.json");
|
|
43
|
-
const executionContext =
|
|
42
|
+
const executionContext = c.get("executionContext");
|
|
44
43
|
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
45
|
-
logger.info({ executionContext }, "executionContext");
|
|
46
44
|
logger.info({
|
|
47
45
|
message: "getRegisteredAgent (agent-level)",
|
|
48
46
|
tenantId,
|
|
@@ -73,8 +71,8 @@ app.post("/a2a", async (c) => {
|
|
|
73
71
|
path: c.req.path,
|
|
74
72
|
method: c.req.method
|
|
75
73
|
}, "OpenTelemetry headers: a2a");
|
|
76
|
-
const executionContext =
|
|
77
|
-
const { tenantId, projectId, agentId, subAgentId } = executionContext;
|
|
74
|
+
const executionContext = c.get("executionContext");
|
|
75
|
+
const { tenantId, projectId, agentId, subAgentId, project } = executionContext;
|
|
78
76
|
if (subAgentId) {
|
|
79
77
|
logger.info({
|
|
80
78
|
message: "a2a (agent-level)",
|
|
@@ -104,11 +102,7 @@ app.post("/a2a", async (c) => {
|
|
|
104
102
|
projectId,
|
|
105
103
|
agentId
|
|
106
104
|
}, "agent-level a2a endpoint");
|
|
107
|
-
const agent =
|
|
108
|
-
tenantId,
|
|
109
|
-
projectId,
|
|
110
|
-
agentId
|
|
111
|
-
} });
|
|
105
|
+
const agent = project.agents[agentId];
|
|
112
106
|
if (!agent) return c.json({
|
|
113
107
|
jsonrpc: "2.0",
|
|
114
108
|
error: {
|
package/dist/routes/chat.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
2
|
+
import { CredentialStoreRegistry, FullExecutionContext } from "@inkeep/agents-core";
|
|
3
3
|
|
|
4
4
|
//#region src/routes/chat.d.ts
|
|
5
5
|
type AppVariables = {
|
|
6
6
|
credentialStores: CredentialStoreRegistry;
|
|
7
|
+
executionContext: FullExecutionContext;
|
|
7
8
|
requestBody?: any;
|
|
8
9
|
};
|
|
9
10
|
declare const app: OpenAPIHono<{
|
package/dist/routes/chat.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { getLogger } from "../logger.js";
|
|
1
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
2
2
|
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { contextValidationMiddleware } from "../context/validation.js";
|
|
4
|
+
import { handleContextResolution } from "../context/context.js";
|
|
5
|
+
import "../context/index.js";
|
|
3
6
|
import { errorOp } from "../utils/agent-operations.js";
|
|
4
7
|
import { createSSEStreamHelper } from "../utils/stream-helpers.js";
|
|
5
8
|
import { ExecutionHandler } from "../handlers/executionHandler.js";
|
|
6
9
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
7
|
-
import {
|
|
10
|
+
import { createApiError, createMessage, createOrGetConversation, generateId, getActiveAgentForConversation, getConversationId, setActiveAgentForConversation } from "@inkeep/agents-core";
|
|
8
11
|
import { context, propagation, trace } from "@opentelemetry/api";
|
|
9
12
|
import { streamSSE } from "hono/streaming";
|
|
10
13
|
|
|
11
14
|
//#region src/routes/chat.ts
|
|
12
15
|
const app = new OpenAPIHono();
|
|
13
|
-
const logger = getLogger("completionsHandler");
|
|
16
|
+
const logger = getLogger$1("completionsHandler");
|
|
14
17
|
const chatCompletionsRoute = createRoute({
|
|
15
18
|
method: "post",
|
|
16
19
|
path: "/completions",
|
|
@@ -82,9 +85,9 @@ const chatCompletionsRoute = createRoute({
|
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
});
|
|
85
|
-
app.use("/completions", contextValidationMiddleware
|
|
88
|
+
app.use("/completions", contextValidationMiddleware);
|
|
86
89
|
app.openapi(chatCompletionsRoute, async (c) => {
|
|
87
|
-
getLogger("chat").info({
|
|
90
|
+
getLogger$1("chat").info({
|
|
88
91
|
path: c.req.path,
|
|
89
92
|
method: c.req.method,
|
|
90
93
|
params: c.req.param()
|
|
@@ -100,9 +103,9 @@ app.openapi(chatCompletionsRoute, async (c) => {
|
|
|
100
103
|
method: c.req.method
|
|
101
104
|
}, "OpenTelemetry headers: chat");
|
|
102
105
|
try {
|
|
103
|
-
const executionContext =
|
|
104
|
-
const { tenantId, projectId, agentId } = executionContext;
|
|
105
|
-
getLogger("chat").debug({
|
|
106
|
+
const executionContext = c.get("executionContext");
|
|
107
|
+
const { tenantId, projectId, agentId, ref } = executionContext;
|
|
108
|
+
getLogger$1("chat").debug({
|
|
106
109
|
tenantId,
|
|
107
110
|
agentId
|
|
108
111
|
}, "Extracted chat parameters from API key context");
|
|
@@ -126,36 +129,16 @@ app.openapi(chatCompletionsRoute, async (c) => {
|
|
|
126
129
|
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
127
130
|
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
128
131
|
return await context.with(ctxWithBaggage, async () => {
|
|
129
|
-
const fullAgent =
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
132
|
+
const fullAgent = executionContext.project.agents[agentId];
|
|
133
|
+
if (!fullAgent) throw createApiError({
|
|
134
|
+
code: "not_found",
|
|
135
|
+
message: "Agent not found"
|
|
136
|
+
});
|
|
137
|
+
const agent = fullAgent;
|
|
135
138
|
let defaultSubAgentId;
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
name: fullAgent.name,
|
|
140
|
-
tenantId,
|
|
141
|
-
projectId,
|
|
142
|
-
defaultSubAgentId: fullAgent.defaultSubAgentId
|
|
143
|
-
};
|
|
144
|
-
const agentKeys = Object.keys(fullAgent.subAgents || {});
|
|
145
|
-
const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : "";
|
|
146
|
-
defaultSubAgentId = fullAgent.defaultSubAgentId || firstAgentId;
|
|
147
|
-
} else {
|
|
148
|
-
agent = await getAgentWithDefaultSubAgent(dbClient_default)({ scopes: {
|
|
149
|
-
tenantId,
|
|
150
|
-
projectId,
|
|
151
|
-
agentId
|
|
152
|
-
} });
|
|
153
|
-
if (!agent) throw createApiError({
|
|
154
|
-
code: "not_found",
|
|
155
|
-
message: "Agent not found"
|
|
156
|
-
});
|
|
157
|
-
defaultSubAgentId = agent.defaultSubAgentId || "";
|
|
158
|
-
}
|
|
139
|
+
const agentKeys = Object.keys(fullAgent.subAgents || {});
|
|
140
|
+
const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : "";
|
|
141
|
+
defaultSubAgentId = fullAgent.defaultSubAgentId || firstAgentId;
|
|
159
142
|
if (!defaultSubAgentId) throw createApiError({
|
|
160
143
|
code: "not_found",
|
|
161
144
|
message: "No default agent found in agent"
|
|
@@ -164,7 +147,9 @@ app.openapi(chatCompletionsRoute, async (c) => {
|
|
|
164
147
|
tenantId,
|
|
165
148
|
projectId,
|
|
166
149
|
id: conversationId,
|
|
167
|
-
|
|
150
|
+
agentId,
|
|
151
|
+
activeSubAgentId: defaultSubAgentId,
|
|
152
|
+
ref: executionContext.resolvedRef
|
|
168
153
|
});
|
|
169
154
|
const activeAgent = await getActiveAgentForConversation(dbClient_default)({
|
|
170
155
|
scopes: {
|
|
@@ -173,34 +158,26 @@ app.openapi(chatCompletionsRoute, async (c) => {
|
|
|
173
158
|
},
|
|
174
159
|
conversationId
|
|
175
160
|
});
|
|
176
|
-
if (!activeAgent) setActiveAgentForConversation(dbClient_default)({
|
|
161
|
+
if (!activeAgent) await setActiveAgentForConversation(dbClient_default)({
|
|
177
162
|
scopes: {
|
|
178
163
|
tenantId,
|
|
179
164
|
projectId
|
|
180
165
|
},
|
|
181
166
|
conversationId,
|
|
182
|
-
|
|
167
|
+
agentId,
|
|
168
|
+
subAgentId: defaultSubAgentId,
|
|
169
|
+
ref: executionContext.resolvedRef
|
|
183
170
|
});
|
|
184
171
|
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
185
|
-
if (!
|
|
186
|
-
scopes: {
|
|
187
|
-
tenantId,
|
|
188
|
-
projectId,
|
|
189
|
-
agentId
|
|
190
|
-
},
|
|
191
|
-
subAgentId
|
|
192
|
-
})) throw createApiError({
|
|
172
|
+
if (!executionContext.project.agents[agentId]?.subAgents[subAgentId]) throw createApiError({
|
|
193
173
|
code: "not_found",
|
|
194
174
|
message: "Agent not found"
|
|
195
175
|
});
|
|
196
176
|
const validatedContext = c.get("validatedContext") || body.headers || {};
|
|
197
177
|
await handleContextResolution({
|
|
198
|
-
|
|
199
|
-
projectId,
|
|
200
|
-
agentId,
|
|
178
|
+
executionContext,
|
|
201
179
|
conversationId,
|
|
202
180
|
headers: validatedContext,
|
|
203
|
-
dbClient: dbClient_default,
|
|
204
181
|
credentialStores: c.get("credentialStores")
|
|
205
182
|
});
|
|
206
183
|
logger.info({
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
2
|
+
import { CredentialStoreRegistry, FullExecutionContext } from "@inkeep/agents-core";
|
|
3
3
|
|
|
4
4
|
//#region src/routes/chatDataStream.d.ts
|
|
5
5
|
type AppVariables = {
|
|
6
6
|
credentialStores: CredentialStoreRegistry;
|
|
7
7
|
requestBody?: any;
|
|
8
|
+
executionContext: FullExecutionContext;
|
|
8
9
|
};
|
|
9
10
|
declare const app: OpenAPIHono<{
|
|
10
11
|
Variables: AppVariables;
|
|
@@ -1,18 +1,21 @@
|
|
|
1
|
-
import { getLogger } from "../logger.js";
|
|
1
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
2
2
|
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { contextValidationMiddleware } from "../context/validation.js";
|
|
4
|
+
import { handleContextResolution } from "../context/context.js";
|
|
5
|
+
import "../context/index.js";
|
|
3
6
|
import { pendingToolApprovalManager } from "../services/PendingToolApprovalManager.js";
|
|
4
7
|
import { errorOp } from "../utils/agent-operations.js";
|
|
5
8
|
import { createBufferingStreamHelper, createVercelStreamHelper } from "../utils/stream-helpers.js";
|
|
6
9
|
import { ExecutionHandler } from "../handlers/executionHandler.js";
|
|
7
10
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
8
|
-
import { commonGetErrorResponses,
|
|
11
|
+
import { commonGetErrorResponses, createApiError, createMessage, generateId, getActiveAgentForConversation, getConversation, getConversationId, loggerFactory, setActiveAgentForConversation } from "@inkeep/agents-core";
|
|
9
12
|
import { context, propagation, trace } from "@opentelemetry/api";
|
|
10
13
|
import { stream } from "hono/streaming";
|
|
11
14
|
import { JsonToSseTransformStream, createUIMessageStream } from "ai";
|
|
12
15
|
|
|
13
16
|
//#region src/routes/chatDataStream.ts
|
|
14
17
|
const app = new OpenAPIHono();
|
|
15
|
-
const logger = getLogger("chatDataStream");
|
|
18
|
+
const logger = getLogger$1("chatDataStream");
|
|
16
19
|
const chatDataStreamRoute = createRoute({
|
|
17
20
|
method: "post",
|
|
18
21
|
path: "/chat",
|
|
@@ -60,11 +63,11 @@ const chatDataStreamRoute = createRoute({
|
|
|
60
63
|
...commonGetErrorResponses
|
|
61
64
|
}
|
|
62
65
|
});
|
|
63
|
-
app.use("/chat", contextValidationMiddleware
|
|
66
|
+
app.use("/chat", contextValidationMiddleware);
|
|
64
67
|
app.openapi(chatDataStreamRoute, async (c) => {
|
|
65
68
|
try {
|
|
66
|
-
const executionContext =
|
|
67
|
-
const { tenantId, projectId, agentId } = executionContext;
|
|
69
|
+
const executionContext = c.get("executionContext");
|
|
70
|
+
const { tenantId, projectId, agentId, resolvedRef } = executionContext;
|
|
68
71
|
loggerFactory.getLogger("chatDataStream").debug({
|
|
69
72
|
tenantId,
|
|
70
73
|
projectId,
|
|
@@ -95,11 +98,7 @@ app.openapi(chatDataStreamRoute, async (c) => {
|
|
|
95
98
|
currentBag = currentBag.setEntry("conversation.id", { value: conversationId });
|
|
96
99
|
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
97
100
|
return await context.with(ctxWithBaggage, async () => {
|
|
98
|
-
const agent =
|
|
99
|
-
tenantId,
|
|
100
|
-
projectId,
|
|
101
|
-
agentId
|
|
102
|
-
} });
|
|
101
|
+
const agent = executionContext.project.agents[agentId];
|
|
103
102
|
if (!agent) throw createApiError({
|
|
104
103
|
code: "not_found",
|
|
105
104
|
message: "Agent not found"
|
|
@@ -117,33 +116,29 @@ app.openapi(chatDataStreamRoute, async (c) => {
|
|
|
117
116
|
},
|
|
118
117
|
conversationId
|
|
119
118
|
});
|
|
120
|
-
if (!activeAgent) setActiveAgentForConversation(dbClient_default)({
|
|
119
|
+
if (!activeAgent) await setActiveAgentForConversation(dbClient_default)({
|
|
121
120
|
scopes: {
|
|
122
121
|
tenantId,
|
|
123
122
|
projectId
|
|
124
123
|
},
|
|
125
124
|
conversationId,
|
|
126
|
-
subAgentId: defaultSubAgentId
|
|
125
|
+
subAgentId: defaultSubAgentId,
|
|
126
|
+
ref: executionContext.resolvedRef,
|
|
127
|
+
agentId
|
|
127
128
|
});
|
|
128
129
|
const subAgentId = activeAgent?.activeSubAgentId || defaultSubAgentId;
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
code: "not_found",
|
|
138
|
-
message: "Agent not found"
|
|
139
|
-
});
|
|
130
|
+
logger.info({ subAgentId }, "subAgentId");
|
|
131
|
+
if (!executionContext.project.agents[agentId]?.subAgents[subAgentId]) {
|
|
132
|
+
logger.error({ subAgentId }, "subAgentId not found");
|
|
133
|
+
throw createApiError({
|
|
134
|
+
code: "not_found",
|
|
135
|
+
message: "Agent not found"
|
|
136
|
+
});
|
|
137
|
+
}
|
|
140
138
|
await handleContextResolution({
|
|
141
|
-
|
|
142
|
-
projectId,
|
|
143
|
-
agentId,
|
|
139
|
+
executionContext,
|
|
144
140
|
conversationId,
|
|
145
141
|
headers: c.get("validatedContext") || body.headers || {},
|
|
146
|
-
dbClient: dbClient_default,
|
|
147
142
|
credentialStores: c.get("credentialStores")
|
|
148
143
|
});
|
|
149
144
|
const lastUserMessage = body.messages.filter((m) => m.role === "user").slice(-1)[0];
|
|
@@ -216,7 +211,9 @@ app.openapi(chatDataStreamRoute, async (c) => {
|
|
|
216
211
|
const streamHelper = createVercelStreamHelper(writer);
|
|
217
212
|
try {
|
|
218
213
|
const emitOperations = c.req.header("x-emit-operations") === "true";
|
|
219
|
-
|
|
214
|
+
const executionHandler = new ExecutionHandler();
|
|
215
|
+
const datasetRunId = c.req.header("x-inkeep-dataset-run-id");
|
|
216
|
+
if (!(await executionHandler.execute({
|
|
220
217
|
executionContext,
|
|
221
218
|
conversationId,
|
|
222
219
|
userMessage: userText,
|
|
@@ -224,6 +221,7 @@ app.openapi(chatDataStreamRoute, async (c) => {
|
|
|
224
221
|
requestId: `chatds-${Date.now()}`,
|
|
225
222
|
sseHelper: streamHelper,
|
|
226
223
|
emitOperations,
|
|
224
|
+
datasetRunId: datasetRunId || void 0,
|
|
227
225
|
forwardedHeaders
|
|
228
226
|
})).success) await streamHelper.writeOperation(errorOp("Unable to process request", "system"));
|
|
229
227
|
} catch (err) {
|
|
@@ -294,7 +292,7 @@ const toolApprovalRoute = createRoute({
|
|
|
294
292
|
app.openapi(toolApprovalRoute, async (c) => {
|
|
295
293
|
return trace.getTracer("tool-approval-handler").startActiveSpan("tool_approval_request", async (span) => {
|
|
296
294
|
try {
|
|
297
|
-
const { tenantId, projectId } =
|
|
295
|
+
const { tenantId, projectId } = c.get("executionContext");
|
|
298
296
|
const { conversationId, toolCallId, approved, reason } = await c.req.json();
|
|
299
297
|
logger.info({
|
|
300
298
|
conversationId,
|
package/dist/routes/mcp.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
-
import { CredentialStoreRegistry } from "@inkeep/agents-core";
|
|
2
|
+
import { CredentialStoreRegistry, FullExecutionContext } from "@inkeep/agents-core";
|
|
3
3
|
|
|
4
4
|
//#region src/routes/mcp.d.ts
|
|
5
5
|
type AppVariables = {
|
|
6
6
|
credentialStores: CredentialStoreRegistry;
|
|
7
7
|
requestBody?: any;
|
|
8
|
+
executionContext: FullExecutionContext;
|
|
8
9
|
};
|
|
9
10
|
declare const app: OpenAPIHono<{
|
|
10
11
|
Variables: AppVariables;
|
package/dist/routes/mcp.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import { getLogger } from "../logger.js";
|
|
1
|
+
import { getLogger as getLogger$1 } from "../logger.js";
|
|
2
2
|
import dbClient_default from "../data/db/dbClient.js";
|
|
3
|
+
import { contextValidationMiddleware } from "../context/validation.js";
|
|
4
|
+
import { handleContextResolution } from "../context/context.js";
|
|
5
|
+
import "../context/index.js";
|
|
3
6
|
import { createMCPStreamHelper } from "../utils/stream-helpers.js";
|
|
4
7
|
import { ExecutionHandler } from "../handlers/executionHandler.js";
|
|
5
8
|
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
6
|
-
import { HeadersScopeSchema,
|
|
9
|
+
import { HeadersScopeSchema, createMessage, createOrGetConversation, generateId, getConversation, getConversationId, updateConversation } from "@inkeep/agents-core";
|
|
7
10
|
import { context, propagation, trace } from "@opentelemetry/api";
|
|
8
11
|
import { McpServer } from "@alcyone-labs/modelcontextprotocol-sdk/server/mcp.js";
|
|
9
12
|
import { StreamableHTTPServerTransport } from "@alcyone-labs/modelcontextprotocol-sdk/server/streamableHttp.js";
|
|
10
13
|
import { toFetchResponse, toReqRes } from "fetch-to-node";
|
|
11
14
|
|
|
12
15
|
//#region src/routes/mcp.ts
|
|
13
|
-
const logger = getLogger("mcp");
|
|
16
|
+
const logger = getLogger$1("mcp");
|
|
14
17
|
/**
|
|
15
18
|
* Singleton mock response object for spoof initialization
|
|
16
19
|
*/
|
|
@@ -83,7 +86,7 @@ const spoofTransportInitialization = async (transport, req, sessionId, mcpProtoc
|
|
|
83
86
|
}, "Spoof initialization failed, continuing anyway");
|
|
84
87
|
}
|
|
85
88
|
};
|
|
86
|
-
const validateSession = async (req, res, body, tenantId, projectId, agentId) => {
|
|
89
|
+
const validateSession = async (req, res, body, tenantId, projectId, agentId, ref) => {
|
|
87
90
|
const sessionId = req.headers["mcp-session-id"];
|
|
88
91
|
logger.info({ sessionId }, "Received MCP session ID");
|
|
89
92
|
if (!sessionId) {
|
|
@@ -154,7 +157,7 @@ const setupTracing = (conversationId, tenantId, agentId) => {
|
|
|
154
157
|
/**
|
|
155
158
|
* Processes and stores the user message
|
|
156
159
|
*/
|
|
157
|
-
const processUserMessage = async (tenantId, projectId, conversationId, query) => {
|
|
160
|
+
const processUserMessage = async (tenantId, projectId, conversationId, query, ref) => {
|
|
158
161
|
const messageSpan = trace.getActiveSpan();
|
|
159
162
|
if (messageSpan) messageSpan.setAttributes({
|
|
160
163
|
"message.content": query,
|
|
@@ -202,13 +205,9 @@ const executeAgentQuery = async (executionContext, conversationId, query, defaul
|
|
|
202
205
|
* Creates and configures an MCP server for the given context
|
|
203
206
|
*/
|
|
204
207
|
const getServer = async (headers$1, executionContext, conversationId, credentialStores) => {
|
|
205
|
-
const { tenantId, projectId, agentId } = executionContext;
|
|
208
|
+
const { tenantId, projectId, agentId, resolvedRef } = executionContext;
|
|
206
209
|
setupTracing(conversationId, tenantId, agentId);
|
|
207
|
-
const agent =
|
|
208
|
-
tenantId,
|
|
209
|
-
projectId,
|
|
210
|
-
agentId
|
|
211
|
-
} });
|
|
210
|
+
const agent = executionContext.project.agents[agentId];
|
|
212
211
|
if (!agent) throw new Error("Agent not found");
|
|
213
212
|
const server = new McpServer({
|
|
214
213
|
name: "inkeep-chat-api-server",
|
|
@@ -224,14 +223,7 @@ const getServer = async (headers$1, executionContext, conversationId, credential
|
|
|
224
223
|
isError: true
|
|
225
224
|
};
|
|
226
225
|
const defaultSubAgentId = agent.defaultSubAgentId;
|
|
227
|
-
if (!
|
|
228
|
-
scopes: {
|
|
229
|
-
tenantId,
|
|
230
|
-
projectId,
|
|
231
|
-
agentId
|
|
232
|
-
},
|
|
233
|
-
subAgentId: defaultSubAgentId
|
|
234
|
-
})) return {
|
|
226
|
+
if (!executionContext.project.agents[agentId]?.subAgents[defaultSubAgentId]) return {
|
|
235
227
|
content: [{
|
|
236
228
|
type: "text",
|
|
237
229
|
text: `Agent not found`
|
|
@@ -239,12 +231,9 @@ const getServer = async (headers$1, executionContext, conversationId, credential
|
|
|
239
231
|
isError: true
|
|
240
232
|
};
|
|
241
233
|
const resolvedContext = await handleContextResolution({
|
|
242
|
-
|
|
243
|
-
projectId,
|
|
244
|
-
agentId,
|
|
234
|
+
executionContext,
|
|
245
235
|
conversationId,
|
|
246
236
|
headers: headers$1,
|
|
247
|
-
dbClient: dbClient_default,
|
|
248
237
|
credentialStores
|
|
249
238
|
});
|
|
250
239
|
logger.info({
|
|
@@ -256,7 +245,7 @@ const getServer = async (headers$1, executionContext, conversationId, credential
|
|
|
256
245
|
hasHeaders: !!headers$1,
|
|
257
246
|
hasValidatedContext: !!resolvedContext
|
|
258
247
|
}, "parameters");
|
|
259
|
-
await processUserMessage(tenantId, projectId, conversationId, query);
|
|
248
|
+
await processUserMessage(tenantId, projectId, conversationId, query, resolvedRef);
|
|
260
249
|
return executeAgentQuery(executionContext, conversationId, query, defaultSubAgentId);
|
|
261
250
|
} catch (error) {
|
|
262
251
|
return {
|
|
@@ -272,7 +261,7 @@ const getServer = async (headers$1, executionContext, conversationId, credential
|
|
|
272
261
|
};
|
|
273
262
|
const app = new OpenAPIHono();
|
|
274
263
|
app.use("/", async (c, next) => {
|
|
275
|
-
if (c.req.method === "POST") return contextValidationMiddleware(
|
|
264
|
+
if (c.req.method === "POST") return contextValidationMiddleware(c, next);
|
|
276
265
|
return next();
|
|
277
266
|
});
|
|
278
267
|
/**
|
|
@@ -280,9 +269,9 @@ app.use("/", async (c, next) => {
|
|
|
280
269
|
*/
|
|
281
270
|
const validateRequestParameters = (c) => {
|
|
282
271
|
try {
|
|
283
|
-
const executionContext =
|
|
272
|
+
const executionContext = c.get("executionContext");
|
|
284
273
|
const { tenantId, projectId, agentId } = executionContext;
|
|
285
|
-
getLogger("mcp").debug({
|
|
274
|
+
getLogger$1("mcp").debug({
|
|
286
275
|
tenantId,
|
|
287
276
|
projectId,
|
|
288
277
|
agentId
|
|
@@ -292,7 +281,7 @@ const validateRequestParameters = (c) => {
|
|
|
292
281
|
executionContext
|
|
293
282
|
};
|
|
294
283
|
} catch (error) {
|
|
295
|
-
getLogger("chat").warn({ error: error instanceof Error ? error.message : "Unknown error" }, "Failed to get execution context");
|
|
284
|
+
getLogger$1("chat").warn({ error: error instanceof Error ? error.message : "Unknown error" }, "Failed to get execution context");
|
|
296
285
|
return {
|
|
297
286
|
valid: false,
|
|
298
287
|
response: c.json({
|
|
@@ -310,7 +299,7 @@ const validateRequestParameters = (c) => {
|
|
|
310
299
|
* Creates a new MCP session and handles initialization
|
|
311
300
|
*/
|
|
312
301
|
const handleInitializationRequest = async (body, executionContext, validatedContext, req, res, c, credentialStores) => {
|
|
313
|
-
const { tenantId, projectId, agentId } = executionContext;
|
|
302
|
+
const { tenantId, projectId, agentId, resolvedRef, project } = executionContext;
|
|
314
303
|
logger.info({ body }, "Received initialization request");
|
|
315
304
|
const sessionId = getConversationId();
|
|
316
305
|
const activeSpan = trace.getActiveSpan();
|
|
@@ -325,11 +314,7 @@ const handleInitializationRequest = async (body, executionContext, validatedCont
|
|
|
325
314
|
currentBag = currentBag.setEntry("conversation.id", { value: sessionId });
|
|
326
315
|
const ctxWithBaggage = propagation.setBaggage(context.active(), currentBag);
|
|
327
316
|
return await context.with(ctxWithBaggage, async () => {
|
|
328
|
-
const agent =
|
|
329
|
-
tenantId,
|
|
330
|
-
projectId,
|
|
331
|
-
agentId
|
|
332
|
-
} });
|
|
317
|
+
const agent = project.agents[agentId];
|
|
333
318
|
if (!agent) return c.json({
|
|
334
319
|
jsonrpc: "2.0",
|
|
335
320
|
error: {
|
|
@@ -346,11 +331,14 @@ const handleInitializationRequest = async (body, executionContext, validatedCont
|
|
|
346
331
|
},
|
|
347
332
|
id: body.id || null
|
|
348
333
|
}, { status: 400 });
|
|
334
|
+
const activeSubAgentId = agent.defaultSubAgentId;
|
|
349
335
|
const conversation = await createOrGetConversation(dbClient_default)({
|
|
350
336
|
id: sessionId,
|
|
351
337
|
tenantId,
|
|
352
338
|
projectId,
|
|
353
|
-
activeSubAgentId
|
|
339
|
+
activeSubAgentId,
|
|
340
|
+
agentId,
|
|
341
|
+
ref: resolvedRef,
|
|
354
342
|
metadata: { sessionData: {
|
|
355
343
|
agentId,
|
|
356
344
|
sessionType: "mcp",
|
|
@@ -380,8 +368,8 @@ const handleInitializationRequest = async (body, executionContext, validatedCont
|
|
|
380
368
|
* Handles requests for existing MCP sessions
|
|
381
369
|
*/
|
|
382
370
|
const handleExistingSessionRequest = async (body, executionContext, validatedContext, req, res, credentialStores) => {
|
|
383
|
-
const { tenantId, projectId, agentId } = executionContext;
|
|
384
|
-
const conversation = await validateSession(req, res, body, tenantId, projectId, agentId);
|
|
371
|
+
const { tenantId, projectId, agentId, resolvedRef } = executionContext;
|
|
372
|
+
const conversation = await validateSession(req, res, body, tenantId, projectId, agentId, resolvedRef);
|
|
385
373
|
if (!conversation) return toFetchResponse(res);
|
|
386
374
|
const sessionId = conversation.id;
|
|
387
375
|
await updateConversation(dbClient_default)({
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DelegationReturnedData, DelegationSentData, ModelSettings, StatusUpdateSettings, TransferData } from "@inkeep/agents-core";
|
|
1
|
+
import { DelegationReturnedData, DelegationSentData, FullExecutionContext, ModelSettings, StatusUpdateSettings, TransferData } from "@inkeep/agents-core";
|
|
2
2
|
|
|
3
3
|
//#region src/services/AgentSession.d.ts
|
|
4
4
|
type AgentSessionEventType = 'agent_generate' | 'agent_reasoning' | 'transfer' | 'delegation_sent' | 'delegation_returned' | 'artifact_saved' | 'tool_call' | 'tool_result' | 'compression' | 'error';
|
|
@@ -143,9 +143,7 @@ interface ErrorEventData {
|
|
|
143
143
|
declare class AgentSession {
|
|
144
144
|
readonly sessionId: string;
|
|
145
145
|
readonly messageId: string;
|
|
146
|
-
readonly
|
|
147
|
-
readonly tenantId?: string | undefined;
|
|
148
|
-
readonly projectId?: string | undefined;
|
|
146
|
+
readonly executionContext: FullExecutionContext;
|
|
149
147
|
readonly contextId?: string | undefined;
|
|
150
148
|
private events;
|
|
151
149
|
private statusUpdateState?;
|
|
@@ -163,7 +161,7 @@ declare class AgentSession {
|
|
|
163
161
|
private artifactService?;
|
|
164
162
|
private artifactParser?;
|
|
165
163
|
private isEmitOperations;
|
|
166
|
-
constructor(sessionId: string, messageId: string,
|
|
164
|
+
constructor(sessionId: string, messageId: string, executionContext: FullExecutionContext, contextId?: string | undefined);
|
|
167
165
|
/**
|
|
168
166
|
* Enable emit operations to send data operations
|
|
169
167
|
*/
|
|
@@ -211,7 +209,7 @@ declare class AgentSession {
|
|
|
211
209
|
getSummary(): {
|
|
212
210
|
sessionId: string;
|
|
213
211
|
messageId: string;
|
|
214
|
-
agentId: string
|
|
212
|
+
agentId: string;
|
|
215
213
|
totalEvents: number;
|
|
216
214
|
eventCounts: Record<AgentSessionEventType, number>;
|
|
217
215
|
agentCounts: Record<string, number>;
|
|
@@ -296,7 +294,7 @@ declare class AgentSessionManager {
|
|
|
296
294
|
/**
|
|
297
295
|
* Create a new session for a message
|
|
298
296
|
*/
|
|
299
|
-
createSession(messageId: string,
|
|
297
|
+
createSession(messageId: string, executionContext: FullExecutionContext, contextId?: string): string;
|
|
300
298
|
/**
|
|
301
299
|
* Initialize status updates for a session
|
|
302
300
|
*/
|