forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-context.ts
More file actions
45 lines (39 loc) · 1.14 KB
/
Copy pathagent-context.ts
File metadata and controls
45 lines (39 loc) · 1.14 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
import {
effect,
inject,
Injector,
runInInjectionContext,
Signal,
} from "@angular/core";
import { Context } from "@ag-ui/client";
import { CopilotKit } from "./copilotkit";
export interface ConnectAgentContextConfig {
injector?: Injector;
}
/**
* Connects context to the agent.
*
* @param context - The context (or a signal of context) to connect to the agent.
* @param config - Optional configuration for connecting the context.
*/
export function connectAgentContext(
context: Context | Signal<Context>,
config?: ConnectAgentContextConfig,
) {
const injector = inject(Injector, { optional: true }) ?? config?.injector;
if (!injector) {
throw new Error(
"Injector not found. You must call connectAgentContext in an injector context or pass an injector in the config",
);
}
runInInjectionContext(injector, () => {
const copilotkit = inject(CopilotKit);
effect((teardown) => {
const contextValue = typeof context === "function" ? context() : context;
const id = copilotkit.core.addContext(contextValue);
teardown(() => {
copilotkit.core.removeContext(id);
});
});
});
}