forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilotkit-agent-context.ts
More file actions
134 lines (118 loc) · 3.08 KB
/
Copy pathcopilotkit-agent-context.ts
File metadata and controls
134 lines (118 loc) · 3.08 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
Directive,
Input,
OnInit,
OnChanges,
OnDestroy,
SimpleChanges,
Inject,
} from "@angular/core";
import { CopilotKit } from "../copilotkit";
import type { Context } from "@ag-ui/client";
/**
* Directive to manage agent context in CopilotKit.
* Automatically adds context on init, updates on changes, and removes on destroy.
*
* @example
* ```html
* <!-- With separate inputs -->
* <div copilotkitAgentContext
* [description]="'User preferences'"
* [value]="userSettings">
* </div>
*
* <!-- With context object -->
* <div [copilotkitAgentContext]="contextObject">
* </div>
*
* <!-- With dynamic values -->
* <div copilotkitAgentContext
* description="Form state"
* [value]="formData$ | async">
* </div>
* ```
*/
@Directive({
selector: "[copilotkitAgentContext]",
standalone: true,
})
export class CopilotKitAgentContext implements OnInit, OnChanges, OnDestroy {
private contextId?: string;
constructor(@Inject(CopilotKit) private readonly copilotkit: CopilotKit) {}
/**
* Context object containing both description and value.
* If provided, this takes precedence over individual inputs.
*/
@Input("copilotkitAgentContext") context?: Context;
/**
* Description of the context.
* Used when context object is not provided.
*/
@Input() description?: string;
/**
* Value of the context.
* Used when context object is not provided.
*/
@Input() value?: any;
ngOnInit(): void {
this.addContext();
}
ngOnChanges(changes: SimpleChanges): void {
// Check if any relevant input has changed
const hasContextChange = "context" in changes;
const hasDescriptionChange = "description" in changes;
const hasValueChange = "value" in changes;
if (hasContextChange || hasDescriptionChange || hasValueChange) {
// Skip the first change as ngOnInit handles initial setup
if (this.contextId) {
this.updateContext();
}
}
}
ngOnDestroy(): void {
this.removeContext();
}
/**
* Adds the context to CopilotKit
*/
private addContext(): void {
const contextToAdd = this.getContext();
if (contextToAdd) {
this.contextId = this.copilotkit.core.addContext(contextToAdd);
}
}
/**
* Updates the context by removing the old one and adding a new one
*/
private updateContext(): void {
this.removeContext();
this.addContext();
}
/**
* Removes the current context from CopilotKit
*/
private removeContext(): void {
if (this.contextId) {
this.copilotkit.core.removeContext(this.contextId);
this.contextId = undefined;
}
}
/**
* Gets the context object from inputs
*/
private getContext(): Context | null {
// If context object is provided, use it
if (this.context) {
return this.context;
}
// Otherwise, build from individual inputs
// Note: null is a valid value, but undefined means not set
if (this.description !== undefined && this.value !== undefined) {
return {
description: this.description,
value: this.value,
};
}
return null;
}
}