@copilotkit/runtime 0.0.0-0.0.0-max-changeset-10101010101010-20260109191632
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/.eslintrc.js +7 -0
- package/CHANGELOG.md +2905 -0
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/__snapshots__/schema/schema.graphql +371 -0
- package/dist/index.d.ts +1495 -0
- package/dist/index.js +5644 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +5601 -0
- package/dist/index.mjs.map +1 -0
- package/dist/langgraph.d.ts +284 -0
- package/dist/langgraph.js +211 -0
- package/dist/langgraph.js.map +1 -0
- package/dist/langgraph.mjs +206 -0
- package/dist/langgraph.mjs.map +1 -0
- package/dist/v2/index.d.ts +2 -0
- package/dist/v2/index.js +22 -0
- package/dist/v2/index.js.map +1 -0
- package/dist/v2/index.mjs +5 -0
- package/dist/v2/index.mjs.map +1 -0
- package/jest.config.js +10 -0
- package/package.json +143 -0
- package/scripts/generate-gql-schema.ts +13 -0
- package/src/agents/langgraph/event-source.ts +329 -0
- package/src/agents/langgraph/events.ts +377 -0
- package/src/graphql/inputs/action.input.ts +16 -0
- package/src/graphql/inputs/agent-session.input.ts +13 -0
- package/src/graphql/inputs/agent-state.input.ts +13 -0
- package/src/graphql/inputs/cloud-guardrails.input.ts +16 -0
- package/src/graphql/inputs/cloud.input.ts +8 -0
- package/src/graphql/inputs/context-property.input.ts +10 -0
- package/src/graphql/inputs/copilot-context.input.ts +10 -0
- package/src/graphql/inputs/custom-property.input.ts +15 -0
- package/src/graphql/inputs/extensions.input.ts +21 -0
- package/src/graphql/inputs/forwarded-parameters.input.ts +22 -0
- package/src/graphql/inputs/frontend.input.ts +14 -0
- package/src/graphql/inputs/generate-copilot-response.input.ts +59 -0
- package/src/graphql/inputs/load-agent-state.input.ts +10 -0
- package/src/graphql/inputs/message.input.ts +110 -0
- package/src/graphql/inputs/meta-event.input.ts +18 -0
- package/src/graphql/message-conversion/agui-to-gql.test.ts +1263 -0
- package/src/graphql/message-conversion/agui-to-gql.ts +333 -0
- package/src/graphql/message-conversion/gql-to-agui.test.ts +1580 -0
- package/src/graphql/message-conversion/gql-to-agui.ts +278 -0
- package/src/graphql/message-conversion/index.ts +2 -0
- package/src/graphql/message-conversion/roundtrip-conversion.test.ts +526 -0
- package/src/graphql/resolvers/copilot.resolver.ts +708 -0
- package/src/graphql/resolvers/state.resolver.ts +27 -0
- package/src/graphql/types/agents-response.type.ts +19 -0
- package/src/graphql/types/base/index.ts +10 -0
- package/src/graphql/types/converted/index.ts +176 -0
- package/src/graphql/types/copilot-response.type.ts +138 -0
- package/src/graphql/types/enums.ts +38 -0
- package/src/graphql/types/extensions-response.type.ts +23 -0
- package/src/graphql/types/guardrails-result.type.ts +20 -0
- package/src/graphql/types/load-agent-state-response.type.ts +17 -0
- package/src/graphql/types/message-status.type.ts +42 -0
- package/src/graphql/types/meta-events.type.ts +71 -0
- package/src/graphql/types/response-status.type.ts +66 -0
- package/src/index.ts +4 -0
- package/src/langgraph.ts +1 -0
- package/src/lib/cloud/index.ts +4 -0
- package/src/lib/error-messages.ts +200 -0
- package/src/lib/index.ts +52 -0
- package/src/lib/integrations/index.ts +6 -0
- package/src/lib/integrations/nest/index.ts +14 -0
- package/src/lib/integrations/nextjs/app-router.ts +38 -0
- package/src/lib/integrations/nextjs/pages-router.ts +39 -0
- package/src/lib/integrations/node-express/index.ts +14 -0
- package/src/lib/integrations/node-http/index.ts +143 -0
- package/src/lib/integrations/node-http/request-handler.ts +111 -0
- package/src/lib/integrations/shared.ts +161 -0
- package/src/lib/logger.ts +28 -0
- package/src/lib/observability.ts +160 -0
- package/src/lib/runtime/__tests__/copilot-runtime-error.test.ts +169 -0
- package/src/lib/runtime/__tests__/mcp-tools-utils.test.ts +464 -0
- package/src/lib/runtime/agent-integrations/langgraph/agent.ts +209 -0
- package/src/lib/runtime/agent-integrations/langgraph/consts.ts +34 -0
- package/src/lib/runtime/agent-integrations/langgraph/index.ts +2 -0
- package/src/lib/runtime/copilot-runtime.ts +710 -0
- package/src/lib/runtime/mcp-tools-utils.ts +254 -0
- package/src/lib/runtime/retry-utils.ts +96 -0
- package/src/lib/runtime/telemetry-agent-runner.ts +139 -0
- package/src/lib/runtime/types.ts +49 -0
- package/src/lib/runtime/utils.ts +87 -0
- package/src/lib/streaming.ts +202 -0
- package/src/lib/telemetry-client.ts +64 -0
- package/src/service-adapters/anthropic/anthropic-adapter.ts +452 -0
- package/src/service-adapters/anthropic/utils.ts +152 -0
- package/src/service-adapters/bedrock/bedrock-adapter.ts +73 -0
- package/src/service-adapters/conversion.ts +67 -0
- package/src/service-adapters/empty/empty-adapter.ts +38 -0
- package/src/service-adapters/events.ts +294 -0
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +84 -0
- package/src/service-adapters/google/google-genai-adapter.test.ts +104 -0
- package/src/service-adapters/google/google-genai-adapter.ts +88 -0
- package/src/service-adapters/groq/groq-adapter.ts +203 -0
- package/src/service-adapters/index.ts +18 -0
- package/src/service-adapters/langchain/langchain-adapter.ts +111 -0
- package/src/service-adapters/langchain/langserve.ts +88 -0
- package/src/service-adapters/langchain/types.ts +14 -0
- package/src/service-adapters/langchain/utils.ts +313 -0
- package/src/service-adapters/openai/openai-adapter.ts +283 -0
- package/src/service-adapters/openai/openai-assistant-adapter.ts +344 -0
- package/src/service-adapters/openai/utils.ts +199 -0
- package/src/service-adapters/service-adapter.ts +41 -0
- package/src/service-adapters/shared/error-utils.ts +61 -0
- package/src/service-adapters/shared/index.ts +1 -0
- package/src/service-adapters/unify/unify-adapter.ts +151 -0
- package/src/utils/failed-response-status-reasons.ts +70 -0
- package/src/utils/index.ts +1 -0
- package/src/v2/index.ts +3 -0
- package/tests/global.d.ts +13 -0
- package/tests/service-adapters/anthropic/allowlist-approach.test.ts +226 -0
- package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +389 -0
- package/tests/service-adapters/openai/allowlist-approach.test.ts +238 -0
- package/tests/service-adapters/openai/openai-adapter.test.ts +301 -0
- package/tests/setup.jest.ts +21 -0
- package/tests/tsconfig.json +10 -0
- package/tsconfig.json +13 -0
- package/tsup.config.ts +20 -0
- package/typedoc.json +4 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { ReplaySubject } from "rxjs";
|
|
2
|
+
import {
|
|
3
|
+
CopilotKitLowLevelError,
|
|
4
|
+
CopilotKitError,
|
|
5
|
+
CopilotKitErrorCode,
|
|
6
|
+
ensureStructuredError,
|
|
7
|
+
} from "@copilotkit/shared";
|
|
8
|
+
import { errorConfig, getFallbackMessage } from "./error-messages";
|
|
9
|
+
|
|
10
|
+
export async function writeJsonLineResponseToEventStream<T>(
|
|
11
|
+
response: ReadableStream<Uint8Array>,
|
|
12
|
+
eventStream$: ReplaySubject<T>,
|
|
13
|
+
) {
|
|
14
|
+
const reader = response.getReader();
|
|
15
|
+
const decoder = new TextDecoder();
|
|
16
|
+
let buffer = [];
|
|
17
|
+
|
|
18
|
+
function flushBuffer() {
|
|
19
|
+
const currentBuffer = buffer.join("");
|
|
20
|
+
if (currentBuffer.trim().length === 0) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const parts = currentBuffer.split("\n");
|
|
24
|
+
if (parts.length === 0) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
29
|
+
|
|
30
|
+
// truncate buffer
|
|
31
|
+
buffer = [];
|
|
32
|
+
|
|
33
|
+
if (!lastPartIsComplete) {
|
|
34
|
+
// put back the last part
|
|
35
|
+
buffer.push(parts.pop());
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
parts
|
|
39
|
+
.map((part) => part.trim())
|
|
40
|
+
.filter((part) => part != "")
|
|
41
|
+
.forEach((part) => {
|
|
42
|
+
eventStream$.next(JSON.parse(part));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
while (true) {
|
|
48
|
+
const { done, value } = await reader.read();
|
|
49
|
+
|
|
50
|
+
if (!done) {
|
|
51
|
+
buffer.push(decoder.decode(value, { stream: true }));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
flushBuffer();
|
|
55
|
+
|
|
56
|
+
if (done) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
} catch (error) {
|
|
61
|
+
// Preserve already structured CopilotKit errors, only convert unstructured errors
|
|
62
|
+
const structuredError = ensureStructuredError(error, convertStreamingErrorToStructured);
|
|
63
|
+
eventStream$.error(structuredError);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
eventStream$.complete();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function convertStreamingErrorToStructured(error: any): CopilotKitError {
|
|
70
|
+
// Determine a more helpful error message based on context
|
|
71
|
+
let helpfulMessage = generateHelpfulErrorMessage(error);
|
|
72
|
+
|
|
73
|
+
// For network-related errors, use CopilotKitLowLevelError to preserve the original error
|
|
74
|
+
if (
|
|
75
|
+
error?.message?.includes("fetch failed") ||
|
|
76
|
+
error?.message?.includes("ECONNREFUSED") ||
|
|
77
|
+
error?.message?.includes("ENOTFOUND") ||
|
|
78
|
+
error?.message?.includes("ETIMEDOUT") ||
|
|
79
|
+
error?.message?.includes("terminated") ||
|
|
80
|
+
error?.cause?.code === "UND_ERR_SOCKET" ||
|
|
81
|
+
error?.message?.includes("other side closed") ||
|
|
82
|
+
error?.code === "UND_ERR_SOCKET"
|
|
83
|
+
) {
|
|
84
|
+
console.log("error", error);
|
|
85
|
+
return new CopilotKitLowLevelError({
|
|
86
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
87
|
+
url: "streaming connection",
|
|
88
|
+
message: helpfulMessage,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// For all other errors, preserve the raw error in a basic CopilotKitError
|
|
93
|
+
return new CopilotKitError({
|
|
94
|
+
message: helpfulMessage,
|
|
95
|
+
code: CopilotKitErrorCode.UNKNOWN,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Generates a helpful error message based on error patterns and context
|
|
101
|
+
*/
|
|
102
|
+
export function generateHelpfulErrorMessage(error: any, context: string = "connection"): string {
|
|
103
|
+
const baseMessage = error?.message || String(error);
|
|
104
|
+
|
|
105
|
+
// Check for preserved error information from Python agent
|
|
106
|
+
const originalErrorType = error?.originalErrorType || error?.extensions?.originalErrorType;
|
|
107
|
+
const statusCode = error?.statusCode || error?.extensions?.statusCode;
|
|
108
|
+
const responseData = error?.responseData || error?.extensions?.responseData;
|
|
109
|
+
|
|
110
|
+
// First, try to match by original error type if available (more specific)
|
|
111
|
+
if (originalErrorType) {
|
|
112
|
+
const typeConfig = errorConfig.errorPatterns[originalErrorType];
|
|
113
|
+
if (typeConfig) {
|
|
114
|
+
return typeConfig.message.replace("{context}", context);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Check for specific error patterns from configuration
|
|
119
|
+
for (const [pattern, config] of Object.entries(errorConfig.errorPatterns)) {
|
|
120
|
+
const shouldMatch =
|
|
121
|
+
baseMessage?.includes(pattern) ||
|
|
122
|
+
error?.cause?.code === pattern ||
|
|
123
|
+
error?.code === pattern ||
|
|
124
|
+
statusCode === parseInt(pattern) ||
|
|
125
|
+
(pattern === "other_side_closed" && baseMessage?.includes("other side closed")) ||
|
|
126
|
+
(pattern === "fetch_failed" && baseMessage?.includes("fetch failed")) ||
|
|
127
|
+
(responseData && JSON.stringify(responseData).includes(pattern));
|
|
128
|
+
|
|
129
|
+
if (shouldMatch) {
|
|
130
|
+
// Replace {context} placeholder with actual context
|
|
131
|
+
return config.message.replace("{context}", context);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Try to match by category for fallback messages
|
|
136
|
+
if (isNetworkError(error)) {
|
|
137
|
+
return getFallbackMessage("network");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (isConnectionError(error)) {
|
|
141
|
+
return getFallbackMessage("connection");
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (isAuthenticationError(error)) {
|
|
145
|
+
return getFallbackMessage("authentication");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Default fallback
|
|
149
|
+
return getFallbackMessage("default");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Determines if an error is network-related
|
|
154
|
+
*/
|
|
155
|
+
function isNetworkError(error: any): boolean {
|
|
156
|
+
const networkPatterns = ["ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT", "fetch_failed"];
|
|
157
|
+
return networkPatterns.some(
|
|
158
|
+
(pattern) =>
|
|
159
|
+
error?.message?.includes(pattern) ||
|
|
160
|
+
error?.cause?.code === pattern ||
|
|
161
|
+
error?.code === pattern,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Determines if an error is connection-related
|
|
167
|
+
*/
|
|
168
|
+
function isConnectionError(error: any): boolean {
|
|
169
|
+
const connectionPatterns = ["terminated", "UND_ERR_SOCKET", "other side closed"];
|
|
170
|
+
return connectionPatterns.some(
|
|
171
|
+
(pattern) =>
|
|
172
|
+
error?.message?.includes(pattern) ||
|
|
173
|
+
error?.cause?.code === pattern ||
|
|
174
|
+
error?.code === pattern,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Determines if an error is authentication-related
|
|
180
|
+
*/
|
|
181
|
+
function isAuthenticationError(error: any): boolean {
|
|
182
|
+
const authPatterns = [
|
|
183
|
+
"401",
|
|
184
|
+
"api key",
|
|
185
|
+
"unauthorized",
|
|
186
|
+
"authentication",
|
|
187
|
+
"AuthenticationError",
|
|
188
|
+
"PermissionDeniedError",
|
|
189
|
+
];
|
|
190
|
+
const baseMessage = error?.message || String(error);
|
|
191
|
+
const originalErrorType = error?.originalErrorType || error?.extensions?.originalErrorType;
|
|
192
|
+
const statusCode = error?.statusCode || error?.extensions?.statusCode;
|
|
193
|
+
|
|
194
|
+
return authPatterns.some(
|
|
195
|
+
(pattern) =>
|
|
196
|
+
baseMessage?.toLowerCase().includes(pattern.toLowerCase()) ||
|
|
197
|
+
originalErrorType === pattern ||
|
|
198
|
+
statusCode === 401 ||
|
|
199
|
+
error?.status === 401 ||
|
|
200
|
+
error?.statusCode === 401,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { TelemetryClient } from "@copilotkit/shared";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { CopilotRuntime, resolveEndpointType } from "./runtime/copilot-runtime";
|
|
4
|
+
import { RuntimeInstanceCreatedInfo } from "@copilotkit/shared/src/telemetry/events";
|
|
5
|
+
import { CreateCopilotRuntimeServerOptions } from "./integrations/shared";
|
|
6
|
+
import { EndpointType, LangGraphPlatformEndpoint } from "./runtime/types";
|
|
7
|
+
const packageJson = require("../../package.json");
|
|
8
|
+
|
|
9
|
+
const telemetryClient = new TelemetryClient({
|
|
10
|
+
packageName: packageJson.name,
|
|
11
|
+
packageVersion: packageJson.version,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export function getRuntimeInstanceTelemetryInfo(
|
|
15
|
+
options: CreateCopilotRuntimeServerOptions,
|
|
16
|
+
): RuntimeInstanceCreatedInfo {
|
|
17
|
+
const runtime = options.runtime;
|
|
18
|
+
const remoteEndpoints = runtime.params?.remoteEndpoints ?? [];
|
|
19
|
+
const endpointsInfo = remoteEndpoints.reduce(
|
|
20
|
+
(acc, endpoint) => {
|
|
21
|
+
let info = { ...acc };
|
|
22
|
+
|
|
23
|
+
const endpointType = resolveEndpointType(endpoint);
|
|
24
|
+
if (!info.endpointTypes.includes(endpointType)) {
|
|
25
|
+
info = {
|
|
26
|
+
...info,
|
|
27
|
+
endpointTypes: [...info.endpointTypes, endpointType],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (endpointType === EndpointType.LangGraphPlatform) {
|
|
32
|
+
// When type is resolved, recreating a const with casting of type
|
|
33
|
+
const ep = endpoint as LangGraphPlatformEndpoint;
|
|
34
|
+
info = {
|
|
35
|
+
...info,
|
|
36
|
+
agentsAmount: ep.agents.length,
|
|
37
|
+
hashedKey: ep.langsmithApiKey
|
|
38
|
+
? createHash("sha256").update(ep.langsmithApiKey).digest("hex")
|
|
39
|
+
: null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return info;
|
|
44
|
+
},
|
|
45
|
+
{ endpointTypes: [], agentsAmount: null, hashedKey: null },
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Get public API key from options.cloud.publicApiKey
|
|
49
|
+
const publicApiKey = options.cloud?.publicApiKey;
|
|
50
|
+
const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
actionsAmount: runtime.params?.actions?.length ?? 0,
|
|
54
|
+
endpointsAmount: remoteEndpoints.length,
|
|
55
|
+
endpointTypes: endpointsInfo.endpointTypes,
|
|
56
|
+
agentsAmount: Object.keys(runtime.instance.agents).length,
|
|
57
|
+
hashedLgcKey: endpointsInfo.hashedKey,
|
|
58
|
+
"cloud.api_key_provided": apiKeyProvided,
|
|
59
|
+
...(apiKeyProvided ? { "cloud.public_api_key": publicApiKey } : {}),
|
|
60
|
+
...(options.cloud?.baseUrl ? { "cloud.base_url": options.cloud.baseUrl } : {}),
|
|
61
|
+
} as RuntimeInstanceCreatedInfo;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default telemetryClient;
|