@inkeep/agents-run-api 0.1.2 → 0.1.6

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.
Files changed (163) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +1 -1
  3. package/SUPPLEMENTAL_TERMS.md +40 -0
  4. package/dist/chunk-P6IQZWFC.js +239 -0
  5. package/dist/conversations-EUPRCMQZ.js +1 -0
  6. package/dist/index.cjs +8992 -0
  7. package/dist/index.d.cts +15 -0
  8. package/dist/index.d.ts +13 -21
  9. package/dist/index.js +8644 -27
  10. package/package.json +14 -11
  11. package/templates/v1/artifact.xml +7 -0
  12. package/templates/v1/data-component.xml +9 -0
  13. package/templates/v1/system-prompt.xml +52 -0
  14. package/templates/v1/thinking-preparation.xml +34 -0
  15. package/templates/v1/tool.xml +12 -0
  16. package/dist/AgentExecutionServer.d.ts +0 -28
  17. package/dist/AgentExecutionServer.d.ts.map +0 -1
  18. package/dist/AgentExecutionServer.js +0 -41
  19. package/dist/__tests__/setup.d.ts +0 -4
  20. package/dist/__tests__/setup.d.ts.map +0 -1
  21. package/dist/__tests__/setup.js +0 -50
  22. package/dist/__tests__/utils/testProject.d.ts +0 -18
  23. package/dist/__tests__/utils/testProject.d.ts.map +0 -1
  24. package/dist/__tests__/utils/testProject.js +0 -26
  25. package/dist/__tests__/utils/testRequest.d.ts +0 -8
  26. package/dist/__tests__/utils/testRequest.d.ts.map +0 -1
  27. package/dist/__tests__/utils/testRequest.js +0 -32
  28. package/dist/__tests__/utils/testTenant.d.ts +0 -64
  29. package/dist/__tests__/utils/testTenant.d.ts.map +0 -1
  30. package/dist/__tests__/utils/testTenant.js +0 -71
  31. package/dist/a2a/client.d.ts +0 -182
  32. package/dist/a2a/client.d.ts.map +0 -1
  33. package/dist/a2a/client.js +0 -645
  34. package/dist/a2a/handlers.d.ts +0 -4
  35. package/dist/a2a/handlers.d.ts.map +0 -1
  36. package/dist/a2a/handlers.js +0 -657
  37. package/dist/a2a/transfer.d.ts +0 -18
  38. package/dist/a2a/transfer.d.ts.map +0 -1
  39. package/dist/a2a/transfer.js +0 -22
  40. package/dist/a2a/types.d.ts +0 -63
  41. package/dist/a2a/types.d.ts.map +0 -1
  42. package/dist/a2a/types.js +0 -1
  43. package/dist/agents/Agent.d.ts +0 -154
  44. package/dist/agents/Agent.d.ts.map +0 -1
  45. package/dist/agents/Agent.js +0 -1107
  46. package/dist/agents/ModelFactory.d.ts +0 -62
  47. package/dist/agents/ModelFactory.d.ts.map +0 -1
  48. package/dist/agents/ModelFactory.js +0 -208
  49. package/dist/agents/SystemPromptBuilder.d.ts +0 -14
  50. package/dist/agents/SystemPromptBuilder.d.ts.map +0 -1
  51. package/dist/agents/SystemPromptBuilder.js +0 -62
  52. package/dist/agents/ToolSessionManager.d.ts +0 -61
  53. package/dist/agents/ToolSessionManager.d.ts.map +0 -1
  54. package/dist/agents/ToolSessionManager.js +0 -143
  55. package/dist/agents/artifactTools.d.ts +0 -30
  56. package/dist/agents/artifactTools.d.ts.map +0 -1
  57. package/dist/agents/artifactTools.js +0 -463
  58. package/dist/agents/generateTaskHandler.d.ts +0 -41
  59. package/dist/agents/generateTaskHandler.d.ts.map +0 -1
  60. package/dist/agents/generateTaskHandler.js +0 -350
  61. package/dist/agents/relationTools.d.ts +0 -35
  62. package/dist/agents/relationTools.d.ts.map +0 -1
  63. package/dist/agents/relationTools.js +0 -244
  64. package/dist/agents/types.d.ts +0 -23
  65. package/dist/agents/types.d.ts.map +0 -1
  66. package/dist/agents/types.js +0 -1
  67. package/dist/agents/versions/V1Config.d.ts +0 -21
  68. package/dist/agents/versions/V1Config.d.ts.map +0 -1
  69. package/dist/agents/versions/V1Config.js +0 -285
  70. package/dist/app.d.ts +0 -12
  71. package/dist/app.d.ts.map +0 -1
  72. package/dist/app.js +0 -202
  73. package/dist/data/agentGraph.d.ts +0 -4
  74. package/dist/data/agentGraph.d.ts.map +0 -1
  75. package/dist/data/agentGraph.js +0 -73
  76. package/dist/data/agents.d.ts +0 -4
  77. package/dist/data/agents.d.ts.map +0 -1
  78. package/dist/data/agents.js +0 -73
  79. package/dist/data/conversations.d.ts +0 -59
  80. package/dist/data/conversations.d.ts.map +0 -1
  81. package/dist/data/conversations.js +0 -216
  82. package/dist/data/db/clean.d.ts +0 -6
  83. package/dist/data/db/clean.d.ts.map +0 -1
  84. package/dist/data/db/clean.js +0 -77
  85. package/dist/data/db/dbClient.d.ts +0 -3
  86. package/dist/data/db/dbClient.d.ts.map +0 -1
  87. package/dist/data/db/dbClient.js +0 -13
  88. package/dist/env.d.ts +0 -45
  89. package/dist/env.d.ts.map +0 -1
  90. package/dist/env.js +0 -64
  91. package/dist/handlers/executionHandler.d.ts +0 -36
  92. package/dist/handlers/executionHandler.d.ts.map +0 -1
  93. package/dist/handlers/executionHandler.js +0 -399
  94. package/dist/index.d.ts.map +0 -1
  95. package/dist/instrumentation.d.ts +0 -13
  96. package/dist/instrumentation.d.ts.map +0 -1
  97. package/dist/instrumentation.js +0 -66
  98. package/dist/logger.d.ts +0 -4
  99. package/dist/logger.d.ts.map +0 -1
  100. package/dist/logger.js +0 -32
  101. package/dist/middleware/api-key-auth.d.ts +0 -22
  102. package/dist/middleware/api-key-auth.d.ts.map +0 -1
  103. package/dist/middleware/api-key-auth.js +0 -139
  104. package/dist/middleware/index.d.ts +0 -2
  105. package/dist/middleware/index.d.ts.map +0 -1
  106. package/dist/middleware/index.js +0 -1
  107. package/dist/openapi.d.ts +0 -2
  108. package/dist/openapi.d.ts.map +0 -1
  109. package/dist/openapi.js +0 -36
  110. package/dist/routes/agents.d.ts +0 -10
  111. package/dist/routes/agents.d.ts.map +0 -1
  112. package/dist/routes/agents.js +0 -158
  113. package/dist/routes/chat.d.ts +0 -4
  114. package/dist/routes/chat.d.ts.map +0 -1
  115. package/dist/routes/chat.js +0 -308
  116. package/dist/routes/chatDataStream.d.ts +0 -4
  117. package/dist/routes/chatDataStream.d.ts.map +0 -1
  118. package/dist/routes/chatDataStream.js +0 -179
  119. package/dist/routes/mcp.d.ts +0 -4
  120. package/dist/routes/mcp.d.ts.map +0 -1
  121. package/dist/routes/mcp.js +0 -500
  122. package/dist/server.d.ts +0 -5
  123. package/dist/server.d.ts.map +0 -1
  124. package/dist/server.js +0 -61
  125. package/dist/tracer.d.ts +0 -24
  126. package/dist/tracer.d.ts.map +0 -1
  127. package/dist/tracer.js +0 -106
  128. package/dist/types/chat.d.ts +0 -25
  129. package/dist/types/chat.d.ts.map +0 -1
  130. package/dist/types/chat.js +0 -1
  131. package/dist/types/execution-context.d.ts +0 -14
  132. package/dist/types/execution-context.d.ts.map +0 -1
  133. package/dist/types/execution-context.js +0 -14
  134. package/dist/utils/agent-operations.d.ts +0 -92
  135. package/dist/utils/agent-operations.d.ts.map +0 -1
  136. package/dist/utils/agent-operations.js +0 -78
  137. package/dist/utils/artifact-component-schema.d.ts +0 -29
  138. package/dist/utils/artifact-component-schema.d.ts.map +0 -1
  139. package/dist/utils/artifact-component-schema.js +0 -119
  140. package/dist/utils/artifact-parser.d.ts +0 -71
  141. package/dist/utils/artifact-parser.d.ts.map +0 -1
  142. package/dist/utils/artifact-parser.js +0 -251
  143. package/dist/utils/cleanup.d.ts +0 -19
  144. package/dist/utils/cleanup.d.ts.map +0 -1
  145. package/dist/utils/cleanup.js +0 -66
  146. package/dist/utils/data-component-schema.d.ts +0 -6
  147. package/dist/utils/data-component-schema.d.ts.map +0 -1
  148. package/dist/utils/data-component-schema.js +0 -43
  149. package/dist/utils/graph-session.d.ts +0 -200
  150. package/dist/utils/graph-session.d.ts.map +0 -1
  151. package/dist/utils/graph-session.js +0 -1012
  152. package/dist/utils/incremental-stream-parser.d.ts +0 -57
  153. package/dist/utils/incremental-stream-parser.d.ts.map +0 -1
  154. package/dist/utils/incremental-stream-parser.js +0 -287
  155. package/dist/utils/response-formatter.d.ts +0 -27
  156. package/dist/utils/response-formatter.d.ts.map +0 -1
  157. package/dist/utils/response-formatter.js +0 -160
  158. package/dist/utils/stream-helpers.d.ts +0 -174
  159. package/dist/utils/stream-helpers.d.ts.map +0 -1
  160. package/dist/utils/stream-helpers.js +0 -466
  161. package/dist/utils/stream-registry.d.ts +0 -18
  162. package/dist/utils/stream-registry.d.ts.map +0 -1
  163. package/dist/utils/stream-registry.js +0 -33
@@ -1,308 +0,0 @@
1
- import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
2
- // import { Hono } from 'hono';
3
- import { trace } from '@opentelemetry/api';
4
- import { streamSSE } from 'hono/streaming';
5
- import { nanoid } from 'nanoid';
6
- import { z } from 'zod';
7
- import { handleContextResolution } from '@inkeep/agents-core';
8
- import { createMessage, createOrGetConversation, getAgentById, getAgentGraphWithDefaultAgent, getActiveAgentForConversation, setActiveAgentForConversation, getFullGraph, getRequestExecutionContext, } from '@inkeep/agents-core';
9
- import { ExecutionHandler } from '../handlers/executionHandler.js';
10
- import { getLogger } from '../logger.js';
11
- import { contextValidationMiddleware } from '@inkeep/agents-core';
12
- import { createSSEStreamHelper } from '../utils/stream-helpers.js';
13
- import dbClient from '../data/db/dbClient.js';
14
- const app = new OpenAPIHono();
15
- const logger = getLogger('completionsHandler');
16
- // Define the OpenAPI route schema
17
- const chatCompletionsRoute = createRoute({
18
- method: 'post',
19
- path: '/completions',
20
- tags: ['chat'],
21
- summary: 'Create chat completion',
22
- description: 'Creates a new chat completion with streaming SSE response using the configured agent graph',
23
- security: [{ bearerAuth: [] }],
24
- request: {
25
- body: {
26
- content: {
27
- 'application/json': {
28
- schema: z.object({
29
- model: z.string().describe('The model to use for the completion'),
30
- messages: z
31
- .array(z.object({
32
- role: z
33
- .enum(['system', 'user', 'assistant', 'function', 'tool'])
34
- .describe('The role of the message'),
35
- content: z
36
- .union([
37
- z.string(),
38
- z.array(z.strictObject({
39
- type: z.string(),
40
- text: z.string().optional(),
41
- })),
42
- ])
43
- .describe('The message content'),
44
- name: z.string().optional().describe('The name of the message sender'),
45
- }))
46
- .describe('The conversation messages'),
47
- temperature: z.number().optional().describe('Controls randomness (0-1)'),
48
- top_p: z.number().optional().describe('Controls nucleus sampling'),
49
- n: z.number().optional().describe('Number of completions to generate'),
50
- stream: z.boolean().optional().describe('Whether to stream the response'),
51
- max_tokens: z.number().optional().describe('Maximum tokens to generate'),
52
- presence_penalty: z.number().optional().describe('Presence penalty (-2 to 2)'),
53
- frequency_penalty: z.number().optional().describe('Frequency penalty (-2 to 2)'),
54
- logit_bias: z.record(z.string(), z.number()).optional().describe('Token logit bias'),
55
- user: z.string().optional().describe('User identifier'),
56
- conversationId: z.string().optional().describe('Conversation ID for multi-turn chat'),
57
- tools: z.array(z.string()).optional().describe('Available tools'),
58
- runConfig: z.record(z.string(), z.unknown()).optional().describe('Run configuration'),
59
- requestContext: z
60
- .record(z.string(), z.unknown())
61
- .optional()
62
- .describe('Context data for template processing (validated against context config schema)'),
63
- }),
64
- },
65
- },
66
- },
67
- },
68
- responses: {
69
- 200: {
70
- description: 'Streaming chat completion response in Server-Sent Events format',
71
- headers: z.object({
72
- 'Content-Type': z.string().default('text/event-stream'),
73
- 'Cache-Control': z.string().default('no-cache'),
74
- Connection: z.string().default('keep-alive'),
75
- }),
76
- content: {
77
- 'text/event-stream': {
78
- schema: z.string().describe('Server-Sent Events stream with chat completion chunks'),
79
- },
80
- },
81
- },
82
- 400: {
83
- description: 'Invalid request context or parameters',
84
- content: {
85
- 'application/json': {
86
- schema: z.object({
87
- error: z.string(),
88
- details: z
89
- .array(z.object({
90
- field: z.string(),
91
- message: z.string(),
92
- value: z.unknown().optional(),
93
- }))
94
- .optional(),
95
- }),
96
- },
97
- },
98
- },
99
- 404: {
100
- description: 'Agent graph or agent not found',
101
- content: {
102
- 'application/json': {
103
- schema: z.object({
104
- error: z.string(),
105
- }),
106
- },
107
- },
108
- },
109
- 500: {
110
- description: 'Internal server error',
111
- content: {
112
- 'application/json': {
113
- schema: z.object({
114
- error: z.string(),
115
- message: z.string(),
116
- }),
117
- },
118
- },
119
- },
120
- },
121
- });
122
- // Apply context validation middleware
123
- app.use('/completions', contextValidationMiddleware(dbClient));
124
- app.openapi(chatCompletionsRoute, async (c) => {
125
- getLogger('chat').info({
126
- path: c.req.path,
127
- method: c.req.method,
128
- params: c.req.param(),
129
- }, 'Chat route accessed');
130
- const otelHeaders = {
131
- traceparent: c.req.header('traceparent'),
132
- tracestate: c.req.header('tracestate'),
133
- baggage: c.req.header('baggage'),
134
- };
135
- logger.info({
136
- otelHeaders,
137
- path: c.req.path,
138
- method: c.req.method,
139
- }, 'OpenTelemetry headers: chat');
140
- try {
141
- // Get execution context from API key authentication
142
- const executionContext = getRequestExecutionContext(c);
143
- const { tenantId, projectId, graphId } = executionContext;
144
- getLogger('chat').debug({
145
- tenantId,
146
- graphId,
147
- }, 'Extracted chat parameters from API key context');
148
- // Get conversationId from request body or generate new one
149
- const body = c.req.valid('json');
150
- const conversationId = body.conversationId || nanoid();
151
- // Get the graph from the full graph system first, fall back to legacy system
152
- const fullGraph = await getFullGraph(dbClient)({
153
- scopes: { tenantId, projectId },
154
- graphId,
155
- });
156
- let agentGraph;
157
- let defaultAgentId;
158
- if (fullGraph) {
159
- // Use full graph system
160
- agentGraph = {
161
- id: fullGraph.id,
162
- name: fullGraph.name,
163
- tenantId,
164
- projectId,
165
- defaultAgentId: fullGraph.defaultAgentId,
166
- };
167
- const agentKeys = Object.keys(fullGraph.agents || {});
168
- const firstAgentId = agentKeys.length > 0 ? agentKeys[0] : '';
169
- defaultAgentId = fullGraph.defaultAgentId || firstAgentId; // Use first agent if no defaultAgentId
170
- }
171
- else {
172
- // Fall back to legacy system
173
- agentGraph = await getAgentGraphWithDefaultAgent(dbClient)({
174
- scopes: { tenantId, projectId },
175
- graphId,
176
- });
177
- if (!agentGraph) {
178
- return c.json({ error: 'Agent graph not found' }, 404);
179
- }
180
- defaultAgentId = agentGraph.defaultAgentId || '';
181
- }
182
- if (!defaultAgentId) {
183
- return c.json({ error: 'No default agent found in graph' }, 404);
184
- }
185
- // Get or create conversation with the default agent
186
- await createOrGetConversation(dbClient)({
187
- tenantId,
188
- projectId,
189
- id: conversationId,
190
- activeAgentId: defaultAgentId,
191
- });
192
- const activeAgent = await getActiveAgentForConversation(dbClient)({
193
- scopes: { tenantId, projectId },
194
- conversationId,
195
- });
196
- if (!activeAgent) {
197
- // Use the default agent from the graph instead of headAgentId
198
- setActiveAgentForConversation(dbClient)({
199
- scopes: { tenantId, projectId },
200
- conversationId,
201
- agentId: defaultAgentId,
202
- });
203
- }
204
- const agentId = activeAgent?.activeAgentId || defaultAgentId;
205
- const agentInfo = await getAgentById(dbClient)({
206
- scopes: { tenantId, projectId },
207
- agentId: agentId,
208
- });
209
- if (!agentInfo) {
210
- return c.json({ error: 'Agent not found' }, 404);
211
- }
212
- // Get validated context from middleware (falls back to body.context if no validation)
213
- const validatedContext = c.get('validatedContext') || body.requestContext || {};
214
- // Context resolution with intelligent conversation state detection
215
- await handleContextResolution(tenantId, projectId, conversationId, graphId, validatedContext, dbClient);
216
- logger.info({
217
- tenantId,
218
- graphId,
219
- conversationId,
220
- defaultAgentId,
221
- activeAgentId: activeAgent?.activeAgentId || 'none',
222
- hasContextConfig: !!agentGraph.contextConfigId,
223
- hasRequestContext: !!body.requestContext,
224
- hasValidatedContext: !!validatedContext,
225
- validatedContextKeys: Object.keys(validatedContext),
226
- }, 'parameters');
227
- const requestId = `chatcmpl-${Date.now()}`;
228
- const timestamp = Math.floor(Date.now() / 1000);
229
- // Extract user message for context
230
- const lastUserMessage = body.messages
231
- .filter((msg) => msg.role === 'user')
232
- .slice(-1)[0];
233
- const userMessage = lastUserMessage ? getMessageText(lastUserMessage.content) : '';
234
- const messageSpan = trace.getActiveSpan();
235
- if (messageSpan) {
236
- messageSpan.setAttributes({
237
- 'message.content': userMessage,
238
- 'message.timestamp': Date.now(),
239
- });
240
- }
241
- // Store the user message in the database
242
- await createMessage(dbClient)({
243
- id: nanoid(),
244
- tenantId,
245
- projectId,
246
- conversationId,
247
- role: 'user',
248
- content: {
249
- text: userMessage,
250
- },
251
- visibility: 'user-facing',
252
- messageType: 'chat',
253
- });
254
- if (messageSpan) {
255
- messageSpan.addEvent('user.message.stored', {
256
- 'message.id': conversationId,
257
- 'database.operation': 'insert',
258
- });
259
- }
260
- // Use Hono's streamSSE helper for proper SSE formatting
261
- return streamSSE(c, async (stream) => {
262
- // Create SSE stream helper
263
- const sseHelper = createSSEStreamHelper(stream, requestId, timestamp);
264
- // Start with the role
265
- await sseHelper.writeRole();
266
- logger.info({ agentId }, 'Starting execution');
267
- // Use the execution handler
268
- const executionHandler = new ExecutionHandler();
269
- const result = await executionHandler.execute({
270
- executionContext,
271
- conversationId,
272
- userMessage,
273
- initialAgentId: agentId,
274
- requestId,
275
- sseHelper,
276
- });
277
- logger.info({ result }, `Execution completed: ${result.success ? 'success' : 'failed'} after ${result.iterations} iterations`);
278
- if (!result.success) {
279
- // If execution failed and no error was already streamed, send a default error
280
- await sseHelper.writeError('Sorry, I was unable to process your request at this time. Please try again.');
281
- }
282
- // Complete the stream
283
- await sseHelper.complete();
284
- });
285
- }
286
- catch (error) {
287
- console.error('❌ Error in chat completions endpoint:', {
288
- error: error instanceof Error ? error.message : error,
289
- stack: error instanceof Error ? error.stack : undefined,
290
- });
291
- return c.json({
292
- error: 'Failed to process chat completion',
293
- message: error instanceof Error ? error.message : 'Unknown error',
294
- }, 500);
295
- }
296
- });
297
- // Helper function to extract text from content
298
- const getMessageText = (content) => {
299
- if (typeof content === 'string') {
300
- return content;
301
- }
302
- // For content arrays, extract text from all text items
303
- return content
304
- .filter((item) => item.type === 'text' && item.text)
305
- .map((item) => item.text)
306
- .join(' ');
307
- };
308
- export default app;
@@ -1,4 +0,0 @@
1
- import { OpenAPIHono } from '@hono/zod-openapi';
2
- declare const app: OpenAPIHono<import("hono").Env, {}, "/">;
3
- export default app;
4
- //# sourceMappingURL=chatDataStream.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"chatDataStream.d.ts","sourceRoot":"","sources":["../../src/routes/chatDataStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,WAAW,EAAK,MAAM,mBAAmB,CAAC;AAsBhE,QAAA,MAAM,GAAG,0CAAoB,CAAC;AAqM9B,eAAe,GAAG,CAAC"}
@@ -1,179 +0,0 @@
1
- import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
2
- import { trace } from '@opentelemetry/api';
3
- import { createUIMessageStream, JsonToSseTransformStream } from 'ai';
4
- import { stream } from 'hono/streaming';
5
- import { nanoid } from 'nanoid';
6
- import { handleContextResolution } from '@inkeep/agents-core';
7
- import { createMessage, getAgentById, getActiveAgentForConversation, setActiveAgentForConversation, getAgentGraphWithDefaultAgent, contextValidationMiddleware, commonGetErrorResponses, getRequestExecutionContext, } from '@inkeep/agents-core';
8
- import { ExecutionHandler } from '../handlers/executionHandler.js';
9
- import { getLogger } from '../logger.js';
10
- import { createVercelStreamHelper } from '../utils/stream-helpers.js';
11
- import dbClient from '../data/db/dbClient.js';
12
- const app = new OpenAPIHono();
13
- const logger = getLogger('chatDataStream');
14
- const chatDataStreamRoute = createRoute({
15
- method: 'post',
16
- path: '/chat',
17
- tags: ['chat'],
18
- summary: 'Chat (Vercel Streaming Protocol)',
19
- description: 'Chat completion endpoint streaming with Vercel data stream protocol.',
20
- security: [{ bearerAuth: [] }],
21
- request: {
22
- body: {
23
- content: {
24
- 'application/json': {
25
- schema: z.object({
26
- model: z.string().optional(),
27
- messages: z.array(z.object({
28
- role: z.enum(['system', 'user', 'assistant', 'function', 'tool']),
29
- content: z.any(),
30
- parts: z
31
- .array(z.object({
32
- type: z.union([
33
- z.enum(['text', 'image', 'audio', 'video', 'file']),
34
- z.string().regex(/^data-/, 'Type must start with "data-"'),
35
- ]),
36
- text: z.string().optional(),
37
- }))
38
- .optional(),
39
- })),
40
- id: z.string().optional(),
41
- conversationId: z.string().optional(),
42
- requestContext: z
43
- .record(z.string(), z.unknown())
44
- .optional()
45
- .describe('Context data for template processing'),
46
- }),
47
- },
48
- },
49
- },
50
- },
51
- responses: {
52
- 200: {
53
- description: 'Streamed chat completion',
54
- headers: z.object({
55
- 'Content-Type': z.string().default('text/plain; charset=utf-8'),
56
- 'x-vercel-ai-data-stream': z.string().default('v1'),
57
- }),
58
- },
59
- ...commonGetErrorResponses,
60
- },
61
- });
62
- // Apply context validation middleware
63
- app.use('/chat', contextValidationMiddleware(dbClient));
64
- app.openapi(chatDataStreamRoute, async (c) => {
65
- try {
66
- // Get execution context from API key authentication
67
- const executionContext = getRequestExecutionContext(c);
68
- const { tenantId, projectId, graphId } = executionContext;
69
- const body = await c.req.valid('json');
70
- const conversationId = body.conversationId || nanoid();
71
- // Add conversation ID to parent span
72
- const activeSpan = trace.getActiveSpan();
73
- if (activeSpan) {
74
- activeSpan.setAttributes({
75
- 'conversation.id': conversationId,
76
- 'tenant.id': tenantId,
77
- 'graph.id': graphId,
78
- 'project.id': projectId,
79
- });
80
- }
81
- const agentGraph = await getAgentGraphWithDefaultAgent(dbClient)({
82
- scopes: { tenantId, projectId },
83
- graphId,
84
- });
85
- if (!agentGraph) {
86
- return c.json({ error: 'Agent graph not found' }, 404);
87
- }
88
- const defaultAgentId = agentGraph.defaultAgentId;
89
- const graphName = agentGraph.name;
90
- const activeAgent = await getActiveAgentForConversation(dbClient)({
91
- scopes: { tenantId, projectId },
92
- conversationId,
93
- });
94
- if (!activeAgent) {
95
- setActiveAgentForConversation(dbClient)({
96
- scopes: { tenantId, projectId },
97
- conversationId,
98
- agentId: defaultAgentId,
99
- });
100
- }
101
- const agentId = activeAgent?.activeAgentId || defaultAgentId;
102
- const agentInfo = await getAgentById(dbClient)({
103
- scopes: { tenantId, projectId },
104
- agentId: agentId,
105
- });
106
- if (!agentInfo) {
107
- return c.json({ error: 'Agent not found' }, 404);
108
- }
109
- // Get validated context from middleware (falls back to body.context if no validation)
110
- const validatedContext = c.get('validatedContext') || body.requestContext || {};
111
- // Context resolution with intelligent conversation state detection
112
- await handleContextResolution(tenantId, projectId, conversationId, graphId, validatedContext, dbClient);
113
- // Store last user message
114
- const lastUserMessage = body.messages.filter((m) => m.role === 'user').slice(-1)[0];
115
- const userText = typeof lastUserMessage?.content === 'string'
116
- ? lastUserMessage.content
117
- : lastUserMessage?.parts?.map((p) => p.text).join('') || '';
118
- logger.info({ userText, lastUserMessage }, 'userText');
119
- const messageSpan = trace.getActiveSpan();
120
- if (messageSpan) {
121
- messageSpan.setAttributes({
122
- 'message.timestamp': new Date().toISOString(),
123
- 'message.content': userText,
124
- 'graph.name': graphName,
125
- });
126
- }
127
- await createMessage(dbClient)({
128
- id: nanoid(),
129
- tenantId,
130
- projectId,
131
- conversationId,
132
- role: 'user',
133
- content: { text: userText },
134
- visibility: 'user-facing',
135
- messageType: 'chat',
136
- });
137
- if (messageSpan) {
138
- messageSpan.addEvent('user.message.stored', {
139
- 'message.id': conversationId,
140
- 'database.operation': 'insert',
141
- });
142
- }
143
- // Create UI Message Stream using AI SDK V5
144
- const dataStream = createUIMessageStream({
145
- execute: async ({ writer }) => {
146
- const streamHelper = createVercelStreamHelper(writer);
147
- try {
148
- const executionHandler = new ExecutionHandler();
149
- const result = await executionHandler.execute({
150
- executionContext,
151
- conversationId,
152
- userMessage: userText,
153
- initialAgentId: agentId,
154
- requestId: `chatds-${Date.now()}`,
155
- sseHelper: streamHelper,
156
- });
157
- if (!result.success) {
158
- await streamHelper.writeError('Unable to process request');
159
- }
160
- }
161
- catch (err) {
162
- logger.error({ err }, 'Streaming error');
163
- await streamHelper.writeError('Internal server error');
164
- }
165
- },
166
- });
167
- c.header('content-type', 'text/event-stream');
168
- c.header('cache-control', 'no-cache');
169
- c.header('connection', 'keep-alive');
170
- c.header('x-vercel-ai-data-stream', 'v2');
171
- c.header('x-accel-buffering', 'no'); // disable nginx buffering
172
- return stream(c, (stream) => stream.pipe(dataStream.pipeThrough(new JsonToSseTransformStream()).pipeThrough(new TextEncoderStream())));
173
- }
174
- catch (error) {
175
- logger.error({ error }, 'chatDataStream error');
176
- return c.json({ error: 'Failed to process chat completion' }, 500);
177
- }
178
- });
179
- export default app;
@@ -1,4 +0,0 @@
1
- import { OpenAPIHono } from '@hono/zod-openapi';
2
- declare const app: OpenAPIHono<import("hono").Env, {}, "/">;
3
- export default app;
4
- //# sourceMappingURL=mcp.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/routes/mcp.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAmY7D,QAAA,MAAM,GAAG,0CAAoB,CAAC;AAwT9B,eAAe,GAAG,CAAC"}