@copilotkit/runtime 1.55.2-next.1 → 1.55.3-canary.1776215089

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 (107) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/agent/converters/tanstack.cjs.map +1 -1
  3. package/dist/agent/converters/tanstack.d.cts +6 -19
  4. package/dist/agent/converters/tanstack.d.cts.map +1 -1
  5. package/dist/agent/converters/tanstack.d.mts +6 -19
  6. package/dist/agent/converters/tanstack.d.mts.map +1 -1
  7. package/dist/agent/converters/tanstack.mjs.map +1 -1
  8. package/dist/agent/index.cjs +14 -0
  9. package/dist/agent/index.cjs.map +1 -1
  10. package/dist/agent/index.d.cts +12 -1
  11. package/dist/agent/index.d.cts.map +1 -1
  12. package/dist/agent/index.d.mts +12 -1
  13. package/dist/agent/index.d.mts.map +1 -1
  14. package/dist/agent/index.mjs +14 -0
  15. package/dist/agent/index.mjs.map +1 -1
  16. package/dist/index.d.cts +2 -1
  17. package/dist/index.d.mts +2 -1
  18. package/dist/lib/index.d.cts +1 -0
  19. package/dist/lib/index.d.cts.map +1 -1
  20. package/dist/lib/index.d.mts +1 -0
  21. package/dist/lib/index.d.mts.map +1 -1
  22. package/dist/lib/runtime/copilot-runtime.cjs.map +1 -1
  23. package/dist/lib/runtime/copilot-runtime.d.cts +3 -3
  24. package/dist/lib/runtime/copilot-runtime.d.cts.map +1 -1
  25. package/dist/lib/runtime/copilot-runtime.d.mts +3 -3
  26. package/dist/lib/runtime/copilot-runtime.d.mts.map +1 -1
  27. package/dist/lib/runtime/copilot-runtime.mjs.map +1 -1
  28. package/dist/package.cjs +1 -1
  29. package/dist/package.mjs +1 -1
  30. package/dist/v2/index.cjs +1 -0
  31. package/dist/v2/index.d.cts +3 -3
  32. package/dist/v2/index.d.mts +3 -3
  33. package/dist/v2/index.mjs +2 -2
  34. package/dist/v2/runtime/core/fetch-handler.cjs +43 -3
  35. package/dist/v2/runtime/core/fetch-handler.cjs.map +1 -1
  36. package/dist/v2/runtime/core/fetch-handler.d.cts.map +1 -1
  37. package/dist/v2/runtime/core/fetch-handler.d.mts.map +1 -1
  38. package/dist/v2/runtime/core/fetch-handler.mjs +43 -3
  39. package/dist/v2/runtime/core/fetch-handler.mjs.map +1 -1
  40. package/dist/v2/runtime/core/fetch-router.cjs +26 -0
  41. package/dist/v2/runtime/core/fetch-router.cjs.map +1 -1
  42. package/dist/v2/runtime/core/fetch-router.mjs +26 -0
  43. package/dist/v2/runtime/core/fetch-router.mjs.map +1 -1
  44. package/dist/v2/runtime/core/hooks.cjs.map +1 -1
  45. package/dist/v2/runtime/core/hooks.d.cts +13 -0
  46. package/dist/v2/runtime/core/hooks.d.cts.map +1 -1
  47. package/dist/v2/runtime/core/hooks.d.mts +13 -0
  48. package/dist/v2/runtime/core/hooks.d.mts.map +1 -1
  49. package/dist/v2/runtime/core/hooks.mjs.map +1 -1
  50. package/dist/v2/runtime/core/runtime.cjs +13 -0
  51. package/dist/v2/runtime/core/runtime.cjs.map +1 -1
  52. package/dist/v2/runtime/core/runtime.d.cts +43 -3
  53. package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
  54. package/dist/v2/runtime/core/runtime.d.mts +43 -3
  55. package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
  56. package/dist/v2/runtime/core/runtime.mjs +13 -1
  57. package/dist/v2/runtime/core/runtime.mjs.map +1 -1
  58. package/dist/v2/runtime/handlers/get-runtime-info.cjs +18 -10
  59. package/dist/v2/runtime/handlers/get-runtime-info.cjs.map +1 -1
  60. package/dist/v2/runtime/handlers/get-runtime-info.mjs +19 -11
  61. package/dist/v2/runtime/handlers/get-runtime-info.mjs.map +1 -1
  62. package/dist/v2/runtime/handlers/handle-connect.cjs +1 -1
  63. package/dist/v2/runtime/handlers/handle-connect.cjs.map +1 -1
  64. package/dist/v2/runtime/handlers/handle-connect.mjs +1 -1
  65. package/dist/v2/runtime/handlers/handle-connect.mjs.map +1 -1
  66. package/dist/v2/runtime/handlers/handle-run.cjs +1 -1
  67. package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
  68. package/dist/v2/runtime/handlers/handle-run.mjs +1 -1
  69. package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
  70. package/dist/v2/runtime/handlers/handle-stop.cjs +2 -1
  71. package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
  72. package/dist/v2/runtime/handlers/handle-stop.mjs +2 -1
  73. package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
  74. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs +1 -1
  75. package/dist/v2/runtime/handlers/intelligence/thread-names.cjs.map +1 -1
  76. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs +1 -1
  77. package/dist/v2/runtime/handlers/intelligence/thread-names.mjs.map +1 -1
  78. package/dist/v2/runtime/handlers/intelligence/threads.cjs +179 -0
  79. package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -0
  80. package/dist/v2/runtime/handlers/intelligence/threads.mjs +173 -0
  81. package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -0
  82. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +3 -2
  83. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  84. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +3 -2
  85. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  86. package/dist/v2/runtime/index.d.cts +1 -1
  87. package/dist/v2/runtime/index.d.mts +1 -1
  88. package/package.json +2 -2
  89. package/src/agent/__tests__/capabilities.test.ts +81 -0
  90. package/src/agent/converters/tanstack.ts +15 -7
  91. package/src/agent/index.ts +33 -0
  92. package/src/lib/runtime/copilot-runtime.ts +6 -1
  93. package/src/v2/runtime/__tests__/agents-factory.test.ts +136 -0
  94. package/src/v2/runtime/__tests__/fetch-router.test.ts +76 -0
  95. package/src/v2/runtime/__tests__/get-runtime-info.test.ts +134 -1
  96. package/src/v2/runtime/core/fetch-handler.ts +55 -4
  97. package/src/v2/runtime/core/fetch-router.ts +48 -0
  98. package/src/v2/runtime/core/hooks.ts +6 -1
  99. package/src/v2/runtime/core/runtime.ts +63 -2
  100. package/src/v2/runtime/handlers/get-runtime-info.ts +33 -8
  101. package/src/v2/runtime/handlers/handle-connect.ts +1 -1
  102. package/src/v2/runtime/handlers/handle-run.ts +1 -1
  103. package/src/v2/runtime/handlers/handle-stop.ts +2 -1
  104. package/src/v2/runtime/handlers/handle-threads.ts +1 -0
  105. package/src/v2/runtime/handlers/intelligence/thread-names.ts +1 -1
  106. package/src/v2/runtime/handlers/intelligence/threads.ts +28 -0
  107. package/src/v2/runtime/handlers/shared/agent-utils.ts +3 -2
@@ -56,9 +56,70 @@ interface CopilotRuntimeMiddlewares {
56
56
  openGenerativeUI?: OpenGenerativeUIConfig;
57
57
  }
58
58
 
59
+ /**
60
+ * Context passed to agent factory functions for per-request agent resolution.
61
+ */
62
+ export interface AgentFactoryContext {
63
+ /** The incoming HTTP request. */
64
+ request: Request;
65
+ }
66
+
67
+ /**
68
+ * A function that dynamically creates agents on a per-request basis.
69
+ * Useful for multi-tenant scenarios or request-scoped agent configuration.
70
+ */
71
+ export type AgentsFactory = (
72
+ ctx: AgentFactoryContext,
73
+ ) => MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
74
+
75
+ /**
76
+ * Agents can be provided as:
77
+ * - A static record of agents
78
+ * - A Promise that resolves to a record of agents
79
+ * - A factory function that receives request context and returns agents (or a Promise of agents)
80
+ */
81
+ export type AgentsConfig =
82
+ | MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>
83
+ | AgentsFactory;
84
+
85
+ /**
86
+ * Resolve an AgentsConfig value to a concrete record of agents.
87
+ * If the config is a factory function, it is called with the given request context.
88
+ * Otherwise it is awaited directly (static record or Promise).
89
+ */
90
+ export async function resolveAgents(
91
+ agents: AgentsConfig,
92
+ request?: Request,
93
+ ): Promise<Record<string, AbstractAgent>> {
94
+ if (typeof agents === "function") {
95
+ if (!request) {
96
+ throw new Error(
97
+ "Agent factory function requires a request context, but none was provided.",
98
+ );
99
+ }
100
+ return agents({ request });
101
+ }
102
+ return agents;
103
+ }
104
+
59
105
  interface BaseCopilotRuntimeOptions extends CopilotRuntimeMiddlewares {
60
- /** Map of available agents (loaded lazily is fine). */
61
- agents: MaybePromise<NonEmptyRecord<Record<string, AbstractAgent>>>;
106
+ /**
107
+ * Map of available agents, or a factory function for per-request agent resolution.
108
+ *
109
+ * Static record:
110
+ * ```ts
111
+ * agents: { support: new SupportAgent(), technical: new TechnicalAgent() }
112
+ * ```
113
+ *
114
+ * Factory function (called per-request):
115
+ * ```ts
116
+ * agents: ({ request }) => {
117
+ * const tenantId = request.headers.get("x-tenant-id");
118
+ * return { default: createAgentForTenant(tenantId) };
119
+ * }
120
+ * ```
121
+ */
122
+ agents: AgentsConfig;
62
123
  /** Optional transcription service for audio processing. */
63
124
  transcriptionService?: TranscriptionService;
64
125
  /** Optional *before* middleware – callback function or webhook URL. */
@@ -1,4 +1,9 @@
1
- import { CopilotRuntimeLike, isIntelligenceRuntime } from "../core/runtime";
1
+ import type { AgentCapabilities } from "@ag-ui/core";
2
+ import {
3
+ CopilotRuntimeLike,
4
+ isIntelligenceRuntime,
5
+ resolveAgents,
6
+ } from "../core/runtime";
2
7
  import {
3
8
  AgentDescription,
4
9
  RuntimeInfo,
@@ -26,22 +31,42 @@ interface HandleGetRuntimeInfoParameters {
26
31
 
27
32
  export async function handleGetRuntimeInfo({
28
33
  runtime,
34
+ request,
29
35
  }: HandleGetRuntimeInfoParameters) {
30
36
  try {
31
- const agents = await runtime.agents;
37
+ const agents = await resolveAgents(runtime.agents, request);
38
+
39
+ const agentEntries = await Promise.all(
40
+ Object.entries(agents).map(async ([name, agent]) => {
41
+ let capabilities: AgentCapabilities | undefined;
42
+ try {
43
+ capabilities = agent.getCapabilities
44
+ ? await agent.getCapabilities()
45
+ : undefined;
46
+ } catch (error) {
47
+ // Per-agent isolation: a single agent failing to report capabilities
48
+ // must not take down the entire /info endpoint.
49
+ console.warn(
50
+ `Failed to fetch capabilities for agent "${name}":`,
51
+ error instanceof Error ? error.message : error,
52
+ );
53
+ capabilities = undefined;
54
+ }
32
55
 
33
- const agentsDict = Object.entries(agents).reduce(
34
- (acc, [name, agent]) => {
35
- acc[name] = {
56
+ const description: AgentDescription = {
36
57
  name,
37
58
  description: agent.description,
38
59
  className: agent.constructor.name,
60
+ ...(capabilities ? { capabilities } : {}),
39
61
  };
40
- return acc;
41
- },
42
- {} as Record<string, AgentDescription>,
62
+
63
+ return [name, description] as const;
64
+ }),
43
65
  );
44
66
 
67
+ const agentsDict: Record<string, AgentDescription> =
68
+ Object.fromEntries(agentEntries);
69
+
45
70
  const runtimeInfo: RuntimeInfo = {
46
71
  version: VERSION,
47
72
  agents: agentsDict,
@@ -29,7 +29,7 @@ export async function handleConnectAgent({
29
29
  });
30
30
 
31
31
  try {
32
- const agent = await cloneAgentForRequest(runtime, agentId);
32
+ const agent = await cloneAgentForRequest(runtime, agentId, request);
33
33
  if (agent instanceof Response) {
34
34
  return agent;
35
35
  }
@@ -30,7 +30,7 @@ export async function handleRunAgent({
30
30
  });
31
31
 
32
32
  try {
33
- const agent = await cloneAgentForRequest(runtime, agentId);
33
+ const agent = await cloneAgentForRequest(runtime, agentId, request);
34
34
  if (agent instanceof Response) {
35
35
  return agent;
36
36
  }
@@ -1,4 +1,5 @@
1
1
  import type { CopilotRuntimeLike } from "../core/runtime";
2
+ import { resolveAgents } from "../core/runtime";
2
3
  import { EventType } from "@ag-ui/client";
3
4
 
4
5
  interface StopAgentParameters {
@@ -15,7 +16,7 @@ export async function handleStopAgent({
15
16
  threadId,
16
17
  }: StopAgentParameters) {
17
18
  try {
18
- const agents = await runtime.agents;
19
+ const agents = await resolveAgents(runtime.agents, request);
19
20
 
20
21
  if (!agents[agentId]) {
21
22
  return new Response(
@@ -1,6 +1,7 @@
1
1
  export {
2
2
  handleArchiveThread,
3
3
  handleDeleteThread,
4
+ handleGetThreadMessages,
4
5
  handleListThreads,
5
6
  handleSubscribeToThreads,
6
7
  handleUpdateThread,
@@ -99,7 +99,7 @@ async function runTitleGenerationAttempt(params: {
99
99
  prompt: string;
100
100
  }): Promise<string | null> {
101
101
  const { runtime, request, agentId, threadId, prompt } = params;
102
- const agent = await cloneAgentForRequest(runtime, agentId);
102
+ const agent = await cloneAgentForRequest(runtime, agentId, request);
103
103
  if (isHandlerResponse(agent)) {
104
104
  logger.warn(
105
105
  { agentId, threadId },
@@ -231,3 +231,31 @@ export async function handleDeleteThread({
231
231
  return errorResponse("Failed to delete thread", 500);
232
232
  }
233
233
  }
234
+
235
+ export async function handleGetThreadMessages({
236
+ runtime,
237
+ request,
238
+ threadId,
239
+ }: ThreadMutationParams): Promise<Response> {
240
+ const intelligenceRuntime = requireIntelligenceRuntime(runtime);
241
+ if (isHandlerResponse(intelligenceRuntime)) {
242
+ return intelligenceRuntime;
243
+ }
244
+
245
+ try {
246
+ const user = await resolveIntelligenceUser({
247
+ runtime: intelligenceRuntime,
248
+ request,
249
+ });
250
+ if (isHandlerResponse(user)) return user;
251
+
252
+ const data = await intelligenceRuntime.intelligence.getThreadMessages({
253
+ threadId,
254
+ });
255
+
256
+ return Response.json(data);
257
+ } catch (error) {
258
+ logger.error({ err: error, threadId }, "Error getting thread messages");
259
+ return errorResponse("Failed to get thread messages", 500);
260
+ }
261
+ }
@@ -5,7 +5,7 @@ import {
5
5
  } from "@ag-ui/client";
6
6
  import { A2UIMiddleware } from "@ag-ui/a2ui-middleware";
7
7
  import { MCPAppsMiddleware } from "@ag-ui/mcp-apps-middleware";
8
- import { CopilotRuntimeLike } from "../../core/runtime";
8
+ import { CopilotRuntimeLike, resolveAgents } from "../../core/runtime";
9
9
  import { OpenGenerativeUIMiddleware } from "../../open-generative-ui-middleware";
10
10
  import { extractForwardableHeaders } from "../header-utils";
11
11
  import { logger } from "@copilotkit/shared";
@@ -28,8 +28,9 @@ export interface ConnectRequestBody extends RunAgentInput {
28
28
  export async function cloneAgentForRequest(
29
29
  runtime: CopilotRuntimeLike,
30
30
  agentId: string,
31
+ request?: Request,
31
32
  ): Promise<AbstractAgent | Response> {
32
- const agents = await runtime.agents;
33
+ const agents = await resolveAgents(runtime.agents, request);
33
34
 
34
35
  if (!agents[agentId]) {
35
36
  return new Response(