forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.ts
More file actions
112 lines (100 loc) · 3.15 KB
/
Copy pathshared.ts
File metadata and controls
112 lines (100 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { YogaInitialContext } from "graphql-yoga";
import { buildSchemaSync } from "type-graphql";
import { CopilotResolver } from "../../graphql/resolvers/copilot.resolver";
import { CopilotRuntime } from "../runtime/copilot-runtime";
import { CopilotServiceAdapter } from "../../service-adapters";
import { CopilotCloudOptions } from "../cloud";
import { LogLevel, createLogger } from "../../lib/logger";
import telemetry from "../telemetry-client";
import { StateResolver } from "../../graphql/resolvers/state.resolver";
/**
* CORS configuration for CopilotKit endpoints.
*/
export interface CopilotEndpointCorsConfig {
/**
* Allowed origin(s). Can be a string, array of strings, or a function that returns the origin.
*/
origin:
| string
| string[]
| ((origin: string, c: any) => string | undefined | null);
/**
* Whether to include credentials (cookies, authorization headers) in CORS requests.
* When true, origin cannot be "*" - must be an explicit origin.
*/
credentials?: boolean;
}
const logger = createLogger();
type AnyPrimitive = string | boolean | number | null;
export type CopilotRequestContextProperties = Record<
string,
AnyPrimitive | Record<string, AnyPrimitive>
>;
export type GraphQLContext = YogaInitialContext & {
_copilotkit: CreateCopilotRuntimeServerOptions;
properties: CopilotRequestContextProperties;
logger: typeof logger;
};
export interface CreateCopilotRuntimeServerOptions {
runtime: CopilotRuntime<any>;
serviceAdapter?: CopilotServiceAdapter;
endpoint: string;
baseUrl?: string;
cloud?: CopilotCloudOptions;
properties?: CopilotRequestContextProperties;
logLevel?: LogLevel;
/**
* Optional CORS configuration. When not provided, defaults to allowing all origins without credentials.
* To support HTTP-only cookies, provide cors config with credentials: true and explicit origin.
*/
cors?: CopilotEndpointCorsConfig;
}
export function buildSchema(
options: {
emitSchemaFile?: string;
} = {},
) {
logger.debug("Building GraphQL schema...");
const schema = buildSchemaSync({
resolvers: [CopilotResolver, StateResolver],
emitSchemaFile: options.emitSchemaFile,
});
logger.debug("GraphQL schema built successfully");
return schema;
}
export type CommonConfig = {
logging: typeof logger;
};
export function getCommonConfig(
options: CreateCopilotRuntimeServerOptions,
): CommonConfig {
const logLevel =
(process.env.LOG_LEVEL as LogLevel) ||
(options.logLevel as LogLevel) ||
"error";
const logger = createLogger({
level: logLevel,
component: "getCommonConfig",
});
if (options.cloud) {
telemetry.setCloudConfiguration({
publicApiKey: options.cloud.publicApiKey,
baseUrl: options.cloud.baseUrl,
});
}
if (options.properties?._copilotkit) {
telemetry.setGlobalProperties({
_copilotkit: {
...(options.properties._copilotkit as Record<string, any>),
},
});
}
telemetry.setGlobalProperties({
runtime: {
serviceAdapter: options.serviceAdapter?.constructor?.name ?? "none",
},
});
return {
logging: createLogger({ component: "CopilotKit Runtime", level: logLevel }),
};
}