@copilotkitnext/angular 0.0.1
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/.turbo/turbo-build.log +39 -0
- package/.turbo/turbo-check-types.log +0 -0
- package/.turbo/turbo-test.log +71 -0
- package/LICENSE +10 -0
- package/README-agent-context.md +310 -0
- package/dist/LICENSE +10 -0
- package/dist/components/chat/copilot-chat-assistant-message-buttons.component.d.ts +75 -0
- package/dist/components/chat/copilot-chat-assistant-message-renderer.component.d.ts +31 -0
- package/dist/components/chat/copilot-chat-assistant-message-toolbar.component.d.ts +8 -0
- package/dist/components/chat/copilot-chat-assistant-message.component.d.ts +131 -0
- package/dist/components/chat/copilot-chat-assistant-message.types.d.ts +31 -0
- package/dist/components/chat/copilot-chat-audio-recorder.component.d.ts +40 -0
- package/dist/components/chat/copilot-chat-buttons.component.d.ts +66 -0
- package/dist/components/chat/copilot-chat-input-defaults.d.ts +37 -0
- package/dist/components/chat/copilot-chat-input.component.d.ts +133 -0
- package/dist/components/chat/copilot-chat-input.types.d.ts +129 -0
- package/dist/components/chat/copilot-chat-message-view-cursor.component.d.ts +15 -0
- package/dist/components/chat/copilot-chat-message-view.component.d.ts +293 -0
- package/dist/components/chat/copilot-chat-message-view.types.d.ts +24 -0
- package/dist/components/chat/copilot-chat-textarea.component.d.ts +45 -0
- package/dist/components/chat/copilot-chat-tool-calls-view.component.d.ts +35 -0
- package/dist/components/chat/copilot-chat-toolbar.component.d.ts +8 -0
- package/dist/components/chat/copilot-chat-tools-menu.component.d.ts +20 -0
- package/dist/components/chat/copilot-chat-user-message-branch-navigation.component.d.ts +23 -0
- package/dist/components/chat/copilot-chat-user-message-buttons.component.d.ts +39 -0
- package/dist/components/chat/copilot-chat-user-message-renderer.component.d.ts +9 -0
- package/dist/components/chat/copilot-chat-user-message-toolbar.component.d.ts +8 -0
- package/dist/components/chat/copilot-chat-user-message.component.d.ts +55 -0
- package/dist/components/chat/copilot-chat-user-message.types.d.ts +33 -0
- package/dist/components/chat/copilot-chat-view-disclaimer.component.d.ts +15 -0
- package/dist/components/chat/copilot-chat-view-feather.component.d.ts +15 -0
- package/dist/components/chat/copilot-chat-view-handlers.service.d.ts +11 -0
- package/dist/components/chat/copilot-chat-view-input-container.component.d.ts +23 -0
- package/dist/components/chat/copilot-chat-view-scroll-to-bottom-button.component.d.ts +17 -0
- package/dist/components/chat/copilot-chat-view-scroll-view.component.d.ts +84 -0
- package/dist/components/chat/copilot-chat-view.component.d.ts +205 -0
- package/dist/components/chat/copilot-chat-view.types.d.ts +42 -0
- package/dist/components/chat/copilot-chat.component.d.ts +37 -0
- package/dist/components/copilotkit-tool-render.component.d.ts +25 -0
- package/dist/core/chat-configuration/chat-configuration.providers.d.ts +54 -0
- package/dist/core/chat-configuration/chat-configuration.service.d.ts +75 -0
- package/dist/core/chat-configuration/chat-configuration.types.d.ts +27 -0
- package/dist/core/copilotkit.providers.d.ts +13 -0
- package/dist/core/copilotkit.service.d.ts +119 -0
- package/dist/core/copilotkit.types.d.ts +83 -0
- package/dist/directives/copilotkit-agent-context.directive.d.ts +68 -0
- package/dist/directives/copilotkit-agent.directive.d.ts +106 -0
- package/dist/directives/copilotkit-chat-config.directive.d.ts +84 -0
- package/dist/directives/copilotkit-config.directive.d.ts +44 -0
- package/dist/directives/copilotkit-frontend-tool.directive.d.ts +25 -0
- package/dist/directives/copilotkit-human-in-the-loop.directive.d.ts +124 -0
- package/dist/directives/stick-to-bottom.directive.d.ts +62 -0
- package/dist/fesm2022/copilotkitnext-angular.mjs +9314 -0
- package/dist/fesm2022/copilotkitnext-angular.mjs.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/lib/directives/tooltip.directive.d.ts +33 -0
- package/dist/lib/slots/copilot-slot.component.d.ts +34 -0
- package/dist/lib/slots/slot.types.d.ts +55 -0
- package/dist/lib/slots/slot.utils.d.ts +108 -0
- package/dist/lib/utils.d.ts +6 -0
- package/dist/services/resize-observer.service.d.ts +44 -0
- package/dist/services/scroll-position.service.d.ts +50 -0
- package/dist/styles.css +1963 -0
- package/dist/types/frontend-tool.d.ts +37 -0
- package/dist/types/human-in-the-loop.d.ts +44 -0
- package/dist/utils/agent-context.utils.d.ts +75 -0
- package/dist/utils/agent.utils.d.ts +99 -0
- package/dist/utils/chat-config.utils.d.ts +166 -0
- package/dist/utils/copilotkit.utils.d.ts +16 -0
- package/dist/utils/frontend-tool.utils.d.ts +119 -0
- package/dist/utils/human-in-the-loop.utils.d.ts +92 -0
- package/eslint.config.mjs +20 -0
- package/ng-package.json +19 -0
- package/package.json +96 -0
- package/slots.md +331 -0
- package/src/components/chat/__tests__/copilot-chat-assistant-message.component.spec.ts +282 -0
- package/src/components/chat/__tests__/copilot-chat-input.component.spec.ts +419 -0
- package/src/components/chat/__tests__/copilot-chat-message-view.component.spec.ts +372 -0
- package/src/components/chat/__tests__/copilot-chat-user-message.component.spec.ts +249 -0
- package/src/components/chat/copilot-chat-assistant-message-buttons.component.ts +292 -0
- package/src/components/chat/copilot-chat-assistant-message-renderer.component.ts +472 -0
- package/src/components/chat/copilot-chat-assistant-message-toolbar.component.ts +29 -0
- package/src/components/chat/copilot-chat-assistant-message.component.ts +463 -0
- package/src/components/chat/copilot-chat-assistant-message.types.ts +50 -0
- package/src/components/chat/copilot-chat-audio-recorder.component.ts +241 -0
- package/src/components/chat/copilot-chat-buttons.component.ts +308 -0
- package/src/components/chat/copilot-chat-buttons.component.ts.bak +471 -0
- package/src/components/chat/copilot-chat-input-defaults.ts +47 -0
- package/src/components/chat/copilot-chat-input.component.ts +512 -0
- package/src/components/chat/copilot-chat-input.types.ts +148 -0
- package/src/components/chat/copilot-chat-message-view-cursor.component.ts +51 -0
- package/src/components/chat/copilot-chat-message-view.component.ts +233 -0
- package/src/components/chat/copilot-chat-message-view.types.ts +39 -0
- package/src/components/chat/copilot-chat-textarea.component.ts +220 -0
- package/src/components/chat/copilot-chat-tool-calls-view.component.ts +261 -0
- package/src/components/chat/copilot-chat-toolbar.component.ts +35 -0
- package/src/components/chat/copilot-chat-tools-menu.component.ts +185 -0
- package/src/components/chat/copilot-chat-user-message-branch-navigation.component.ts +121 -0
- package/src/components/chat/copilot-chat-user-message-buttons.component.ts +170 -0
- package/src/components/chat/copilot-chat-user-message-renderer.component.ts +37 -0
- package/src/components/chat/copilot-chat-user-message-toolbar.component.ts +37 -0
- package/src/components/chat/copilot-chat-user-message.component.ts +247 -0
- package/src/components/chat/copilot-chat-user-message.types.ts +42 -0
- package/src/components/chat/copilot-chat-view-disclaimer.component.ts +51 -0
- package/src/components/chat/copilot-chat-view-feather.component.ts +47 -0
- package/src/components/chat/copilot-chat-view-handlers.service.ts +14 -0
- package/src/components/chat/copilot-chat-view-input-container.component.ts +87 -0
- package/src/components/chat/copilot-chat-view-scroll-to-bottom-button.component.ts +79 -0
- package/src/components/chat/copilot-chat-view-scroll-view.component.ts +322 -0
- package/src/components/chat/copilot-chat-view.component.ts +420 -0
- package/src/components/chat/copilot-chat-view.types.ts +52 -0
- package/src/components/chat/copilot-chat.component.ts +232 -0
- package/src/components/copilotkit-tool-render.component.ts +169 -0
- package/src/core/__tests__/copilotkit.service.spec.ts +1051 -0
- package/src/core/__tests__/copilotkit.service.wildcard.spec.ts +316 -0
- package/src/core/chat-configuration/__tests__/chat-configuration.service.spec.ts +287 -0
- package/src/core/chat-configuration/chat-configuration.providers.ts +71 -0
- package/src/core/chat-configuration/chat-configuration.service.ts +162 -0
- package/src/core/chat-configuration/chat-configuration.types.ts +57 -0
- package/src/core/copilotkit.providers.ts +59 -0
- package/src/core/copilotkit.service.ts +542 -0
- package/src/core/copilotkit.types.ts +132 -0
- package/src/directives/__tests__/copilotkit-agent-context.directive.spec.ts +384 -0
- package/src/directives/__tests__/copilotkit-agent.directive.spec.ts +253 -0
- package/src/directives/__tests__/copilotkit-chat-config.directive.spec.ts +385 -0
- package/src/directives/__tests__/copilotkit-config.directive.spec.ts +69 -0
- package/src/directives/__tests__/copilotkit-frontend-tool-simple.directive.spec.ts +60 -0
- package/src/directives/__tests__/copilotkit-frontend-tool.directive.spec.ts +108 -0
- package/src/directives/__tests__/copilotkit-human-in-the-loop.directive.spec.ts +452 -0
- package/src/directives/copilotkit-agent-context.directive.ts +138 -0
- package/src/directives/copilotkit-agent.directive.ts +225 -0
- package/src/directives/copilotkit-chat-config.directive.ts +241 -0
- package/src/directives/copilotkit-config.directive.ts +81 -0
- package/src/directives/copilotkit-frontend-tool.directive.ts +145 -0
- package/src/directives/copilotkit-human-in-the-loop.directive.ts +281 -0
- package/src/directives/stick-to-bottom.directive.ts +204 -0
- package/src/index.ts +105 -0
- package/src/lib/directives/tooltip.directive.ts +292 -0
- package/src/lib/slots/__tests__/slot.utils.spec.ts +377 -0
- package/src/lib/slots/copilot-slot.component.ts +135 -0
- package/src/lib/slots/index.ts +3 -0
- package/src/lib/slots/slot.types.ts +64 -0
- package/src/lib/slots/slot.utils.ts +289 -0
- package/src/lib/utils.ts +10 -0
- package/src/public-api.ts +1 -0
- package/src/services/resize-observer.service.ts +181 -0
- package/src/services/scroll-position.service.ts +169 -0
- package/src/styles/globals.css +266 -0
- package/src/styles/index.css +3 -0
- package/src/test-setup.ts +15 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/testing.utils.ts +248 -0
- package/src/types/frontend-tool.ts +44 -0
- package/src/types/human-in-the-loop.ts +52 -0
- package/src/utils/__tests__/agent.utils.spec.ts +234 -0
- package/src/utils/__tests__/chat-config.utils.spec.ts +306 -0
- package/src/utils/__tests__/frontend-tool-inject.spec.ts +350 -0
- package/src/utils/__tests__/frontend-tool-integration.spec.ts +199 -0
- package/src/utils/__tests__/frontend-tool.utils.spec.ts +272 -0
- package/src/utils/__tests__/human-in-the-loop.utils.spec.ts +365 -0
- package/src/utils/agent-context.utils.ts +133 -0
- package/src/utils/agent.utils.ts +239 -0
- package/src/utils/chat-config.utils.ts +221 -0
- package/src/utils/copilotkit.utils.ts +20 -0
- package/src/utils/frontend-tool.utils.ts +266 -0
- package/src/utils/human-in-the-loop.utils.ts +359 -0
- package/tsconfig.json +33 -0
- package/tsconfig.spec.json +12 -0
- package/vitest.config.mts +34 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { TestBed } from "@angular/core/testing";
|
|
2
|
+
import { Component, OnInit } from "@angular/core";
|
|
3
|
+
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
registerFrontendTool,
|
|
6
|
+
createDynamicFrontendTool,
|
|
7
|
+
} from "../frontend-tool.utils";
|
|
8
|
+
import { CopilotKitService } from "../../core/copilotkit.service";
|
|
9
|
+
import { provideCopilotKit } from "../../core/copilotkit.providers";
|
|
10
|
+
import { z } from "zod";
|
|
11
|
+
import { signal } from "@angular/core";
|
|
12
|
+
|
|
13
|
+
// Mock CopilotKitCore
|
|
14
|
+
vi.mock("@copilotkitnext/core", () => ({
|
|
15
|
+
CopilotKitCore: vi.fn().mockImplementation(() => ({
|
|
16
|
+
addTool: vi.fn(),
|
|
17
|
+
removeTool: vi.fn(),
|
|
18
|
+
setRuntimeUrl: vi.fn(),
|
|
19
|
+
setHeaders: vi.fn(),
|
|
20
|
+
setProperties: vi.fn(),
|
|
21
|
+
setAgents: vi.fn(),
|
|
22
|
+
subscribe: vi.fn(() => () => {}),
|
|
23
|
+
})),
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
// Mock component for testing
|
|
27
|
+
@Component({
|
|
28
|
+
template: `<div>Mock Render</div>`,
|
|
29
|
+
standalone: true,
|
|
30
|
+
})
|
|
31
|
+
class MockRenderComponent {}
|
|
32
|
+
|
|
33
|
+
describe("Frontend Tool Inject Functions", () => {
|
|
34
|
+
let service: CopilotKitService;
|
|
35
|
+
let addToolSpy: any;
|
|
36
|
+
let removeToolSpy: any;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
TestBed.configureTestingModule({
|
|
40
|
+
providers: [provideCopilotKit({})],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
service = TestBed.inject(CopilotKitService);
|
|
44
|
+
addToolSpy = vi.spyOn(service.copilotkit, "addTool");
|
|
45
|
+
removeToolSpy = vi.spyOn(service.copilotkit, "removeTool");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
afterEach(() => {
|
|
49
|
+
vi.clearAllMocks();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("registerFrontendTool", () => {
|
|
53
|
+
it("should register tool within component context", () => {
|
|
54
|
+
@Component({
|
|
55
|
+
template: "",
|
|
56
|
+
standalone: true,
|
|
57
|
+
providers: [provideCopilotKit({})],
|
|
58
|
+
})
|
|
59
|
+
class TestComponent implements OnInit {
|
|
60
|
+
// Move the registration to constructor or field initializer
|
|
61
|
+
// where injection context is available
|
|
62
|
+
toolName = registerFrontendTool({
|
|
63
|
+
name: "testTool",
|
|
64
|
+
description: "Test tool",
|
|
65
|
+
parameters: z.object({ value: z.string() }),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
ngOnInit() {
|
|
69
|
+
expect(this.toolName).toBe("testTool");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
74
|
+
fixture.detectChanges();
|
|
75
|
+
|
|
76
|
+
// Tool should be added
|
|
77
|
+
expect(addToolSpy).toHaveBeenCalledWith(
|
|
78
|
+
expect.objectContaining({
|
|
79
|
+
name: "testTool",
|
|
80
|
+
description: "Test tool",
|
|
81
|
+
})
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should register render when provided", () => {
|
|
86
|
+
@Component({
|
|
87
|
+
template: "",
|
|
88
|
+
standalone: true,
|
|
89
|
+
providers: [provideCopilotKit({})],
|
|
90
|
+
})
|
|
91
|
+
class TestComponent {
|
|
92
|
+
// Call in field initializer where injection context is available
|
|
93
|
+
toolName = registerFrontendTool({
|
|
94
|
+
name: "renderTool",
|
|
95
|
+
render: MockRenderComponent,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
100
|
+
fixture.detectChanges();
|
|
101
|
+
|
|
102
|
+
const renders = service.currentRenderToolCalls();
|
|
103
|
+
const renderTool = renders.find((r) => r.name === "renderTool");
|
|
104
|
+
expect(renderTool).toBeDefined();
|
|
105
|
+
expect(renderTool?.render).toBe(MockRenderComponent);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should cleanup on component destroy", () => {
|
|
109
|
+
@Component({
|
|
110
|
+
template: "",
|
|
111
|
+
standalone: true,
|
|
112
|
+
providers: [provideCopilotKit({})],
|
|
113
|
+
})
|
|
114
|
+
class TestComponent {
|
|
115
|
+
toolName = registerFrontendTool({
|
|
116
|
+
name: "cleanupTool",
|
|
117
|
+
description: "Tool with auto cleanup",
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
122
|
+
fixture.detectChanges();
|
|
123
|
+
|
|
124
|
+
expect(addToolSpy).toHaveBeenCalled();
|
|
125
|
+
|
|
126
|
+
// Destroy the component
|
|
127
|
+
fixture.destroy();
|
|
128
|
+
|
|
129
|
+
// Tool should be removed
|
|
130
|
+
expect(removeToolSpy).toHaveBeenCalledWith("cleanupTool");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("should handle complex tool configuration", () => {
|
|
134
|
+
const schema = z.object({
|
|
135
|
+
query: z.string(),
|
|
136
|
+
limit: z.number().optional(),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const handler = vi.fn(async (args: any) => {
|
|
140
|
+
return { results: [] };
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
@Component({
|
|
144
|
+
template: "",
|
|
145
|
+
standalone: true,
|
|
146
|
+
providers: [provideCopilotKit({})],
|
|
147
|
+
})
|
|
148
|
+
class TestComponent {
|
|
149
|
+
toolName = registerFrontendTool({
|
|
150
|
+
name: "searchTool",
|
|
151
|
+
description: "Search tool",
|
|
152
|
+
parameters: schema,
|
|
153
|
+
handler: handler,
|
|
154
|
+
render: MockRenderComponent,
|
|
155
|
+
followUp: true,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
160
|
+
fixture.detectChanges();
|
|
161
|
+
|
|
162
|
+
expect(addToolSpy).toHaveBeenCalledWith(
|
|
163
|
+
expect.objectContaining({
|
|
164
|
+
name: "searchTool",
|
|
165
|
+
description: "Search tool",
|
|
166
|
+
followUp: true,
|
|
167
|
+
})
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("createDynamicFrontendTool", () => {
|
|
173
|
+
it("should create tool with initial configuration", () => {
|
|
174
|
+
@Component({
|
|
175
|
+
template: "",
|
|
176
|
+
standalone: true,
|
|
177
|
+
providers: [provideCopilotKit({})],
|
|
178
|
+
})
|
|
179
|
+
class TestComponent implements OnInit {
|
|
180
|
+
// Create in field initializer
|
|
181
|
+
tool = createDynamicFrontendTool(
|
|
182
|
+
"dynamicTool",
|
|
183
|
+
"Dynamic tool",
|
|
184
|
+
z.object({ value: z.string() }),
|
|
185
|
+
() => vi.fn(async () => "result")
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
ngOnInit() {
|
|
189
|
+
expect(this.tool).toBeDefined();
|
|
190
|
+
expect(this.tool.update).toBeDefined();
|
|
191
|
+
expect(this.tool.destroy).toBeDefined();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
196
|
+
fixture.detectChanges();
|
|
197
|
+
|
|
198
|
+
expect(addToolSpy).toHaveBeenCalledWith(
|
|
199
|
+
expect.objectContaining({
|
|
200
|
+
name: "dynamicTool",
|
|
201
|
+
description: "Dynamic tool",
|
|
202
|
+
})
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("should handle dynamic updates", () => {
|
|
207
|
+
@Component({
|
|
208
|
+
template: "",
|
|
209
|
+
standalone: true,
|
|
210
|
+
providers: [provideCopilotKit({})],
|
|
211
|
+
})
|
|
212
|
+
class TestComponent implements OnInit {
|
|
213
|
+
handlerSignal = signal(vi.fn(async () => "initial"));
|
|
214
|
+
|
|
215
|
+
tool = createDynamicFrontendTool(
|
|
216
|
+
"updateableTool",
|
|
217
|
+
"Updateable tool",
|
|
218
|
+
z.object({ value: z.string() }),
|
|
219
|
+
() => this.handlerSignal()
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
ngOnInit() {
|
|
223
|
+
// Initial registration
|
|
224
|
+
expect(addToolSpy).toHaveBeenCalledTimes(1);
|
|
225
|
+
|
|
226
|
+
// Update handler
|
|
227
|
+
this.handlerSignal.set(vi.fn(async () => "updated"));
|
|
228
|
+
this.tool.update();
|
|
229
|
+
|
|
230
|
+
// Should remove old and add new
|
|
231
|
+
expect(removeToolSpy).toHaveBeenCalledWith("updateableTool");
|
|
232
|
+
expect(addToolSpy).toHaveBeenCalledTimes(2);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
237
|
+
fixture.detectChanges();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("should support dynamic description", () => {
|
|
241
|
+
@Component({
|
|
242
|
+
template: "",
|
|
243
|
+
standalone: true,
|
|
244
|
+
providers: [provideCopilotKit({})],
|
|
245
|
+
})
|
|
246
|
+
class TestComponent implements OnInit {
|
|
247
|
+
descSignal = signal("Initial description");
|
|
248
|
+
|
|
249
|
+
tool = createDynamicFrontendTool(
|
|
250
|
+
"descTool",
|
|
251
|
+
() => this.descSignal(),
|
|
252
|
+
z.object({}),
|
|
253
|
+
() => vi.fn(async () => null)
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
ngOnInit() {
|
|
257
|
+
this.descSignal.set("Updated description");
|
|
258
|
+
this.tool.update();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
263
|
+
fixture.detectChanges();
|
|
264
|
+
|
|
265
|
+
expect(addToolSpy).toHaveBeenLastCalledWith(
|
|
266
|
+
expect.objectContaining({
|
|
267
|
+
description: "Updated description",
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should cleanup on manual destroy", () => {
|
|
273
|
+
@Component({
|
|
274
|
+
template: "",
|
|
275
|
+
standalone: true,
|
|
276
|
+
providers: [provideCopilotKit({})],
|
|
277
|
+
})
|
|
278
|
+
class TestComponent implements OnInit {
|
|
279
|
+
tool = createDynamicFrontendTool(
|
|
280
|
+
"manualCleanupTool",
|
|
281
|
+
"Manual cleanup",
|
|
282
|
+
z.object({}),
|
|
283
|
+
() => vi.fn(async () => null)
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
ngOnInit() {
|
|
287
|
+
// Manual destroy
|
|
288
|
+
this.tool.destroy();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
293
|
+
fixture.detectChanges();
|
|
294
|
+
|
|
295
|
+
expect(removeToolSpy).toHaveBeenCalledWith("manualCleanupTool");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("should auto-cleanup on component destroy", () => {
|
|
299
|
+
@Component({
|
|
300
|
+
template: "",
|
|
301
|
+
standalone: true,
|
|
302
|
+
providers: [provideCopilotKit({})],
|
|
303
|
+
})
|
|
304
|
+
class TestComponent {
|
|
305
|
+
tool = createDynamicFrontendTool(
|
|
306
|
+
"autoCleanupTool",
|
|
307
|
+
"Auto cleanup",
|
|
308
|
+
z.object({}),
|
|
309
|
+
() => vi.fn(async () => null)
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
314
|
+
fixture.detectChanges();
|
|
315
|
+
|
|
316
|
+
expect(addToolSpy).toHaveBeenCalled();
|
|
317
|
+
|
|
318
|
+
fixture.destroy();
|
|
319
|
+
|
|
320
|
+
expect(removeToolSpy).toHaveBeenCalledWith("autoCleanupTool");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("should support dynamic render", () => {
|
|
324
|
+
@Component({
|
|
325
|
+
template: "",
|
|
326
|
+
standalone: true,
|
|
327
|
+
providers: [provideCopilotKit({})],
|
|
328
|
+
})
|
|
329
|
+
class TestComponent {
|
|
330
|
+
renderSignal = signal<any>(MockRenderComponent);
|
|
331
|
+
|
|
332
|
+
tool = createDynamicFrontendTool(
|
|
333
|
+
"renderDynamicTool",
|
|
334
|
+
"Dynamic render",
|
|
335
|
+
z.object({}),
|
|
336
|
+
() => vi.fn(async () => null),
|
|
337
|
+
() => this.renderSignal()
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const fixture = TestBed.createComponent(TestComponent);
|
|
342
|
+
fixture.detectChanges();
|
|
343
|
+
|
|
344
|
+
const renders = service.currentRenderToolCalls();
|
|
345
|
+
const renderTool = renders.find((r) => r.name === "renderDynamicTool");
|
|
346
|
+
expect(renderTool).toBeDefined();
|
|
347
|
+
expect(renderTool?.render).toBe(MockRenderComponent);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
});
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { TestBed } from "@angular/core/testing";
|
|
2
|
+
import { describe, it, expect, beforeEach, vi } from "vitest";
|
|
3
|
+
import { CopilotKitService } from "../../core/copilotkit.service";
|
|
4
|
+
import { provideCopilotKit } from "../../core/copilotkit.providers";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
|
|
7
|
+
// Mock CopilotKitCore
|
|
8
|
+
vi.mock("@copilotkitnext/core", () => ({
|
|
9
|
+
CopilotKitCore: vi.fn().mockImplementation(() => ({
|
|
10
|
+
addTool: vi.fn(),
|
|
11
|
+
removeTool: vi.fn(),
|
|
12
|
+
setRuntimeUrl: vi.fn(),
|
|
13
|
+
setHeaders: vi.fn(),
|
|
14
|
+
setProperties: vi.fn(),
|
|
15
|
+
setAgents: vi.fn(),
|
|
16
|
+
subscribe: vi.fn(() => () => {}),
|
|
17
|
+
})),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
describe("Frontend Tool Integration", () => {
|
|
21
|
+
let service: CopilotKitService;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
TestBed.configureTestingModule({
|
|
25
|
+
providers: [provideCopilotKit({})],
|
|
26
|
+
});
|
|
27
|
+
service = TestBed.inject(CopilotKitService);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("Service Tool Render Methods", () => {
|
|
31
|
+
it("should register tool render", () => {
|
|
32
|
+
const render = {
|
|
33
|
+
name: "testTool",
|
|
34
|
+
args: z.object({ value: z.string() }),
|
|
35
|
+
render: {} as any, // Mock component
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
service.registerToolRender("testTool", render);
|
|
39
|
+
|
|
40
|
+
const retrieved = service.getToolRender("testTool");
|
|
41
|
+
expect(retrieved).toBe(render);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should unregister tool render", () => {
|
|
45
|
+
const render = {
|
|
46
|
+
name: "removeTool",
|
|
47
|
+
args: z.object({}),
|
|
48
|
+
render: {} as any,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
service.registerToolRender("removeTool", render);
|
|
52
|
+
expect(service.getToolRender("removeTool")).toBeDefined();
|
|
53
|
+
|
|
54
|
+
service.unregisterToolRender("removeTool");
|
|
55
|
+
expect(service.getToolRender("removeTool")).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should warn when overwriting render", () => {
|
|
59
|
+
const consoleSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
60
|
+
|
|
61
|
+
service.registerToolRender("dupeTool", {
|
|
62
|
+
name: "dupeTool",
|
|
63
|
+
args: z.object({}),
|
|
64
|
+
render: {} as any,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
service.registerToolRender("dupeTool", {
|
|
68
|
+
name: "dupeTool",
|
|
69
|
+
args: z.object({}),
|
|
70
|
+
render: {} as any,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
74
|
+
expect.stringContaining("being overwritten")
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
consoleSpy.mockRestore();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should handle multiple tool renders", () => {
|
|
81
|
+
const tools = ["tool1", "tool2", "tool3"];
|
|
82
|
+
|
|
83
|
+
tools.forEach((name) => {
|
|
84
|
+
service.registerToolRender(name, {
|
|
85
|
+
name: name,
|
|
86
|
+
args: z.object({}),
|
|
87
|
+
render: {} as any,
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
tools.forEach((name) => {
|
|
92
|
+
expect(service.getToolRender(name)).toBeDefined();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Remove middle one
|
|
96
|
+
service.unregisterToolRender("tool2");
|
|
97
|
+
|
|
98
|
+
expect(service.getToolRender("tool1")).toBeDefined();
|
|
99
|
+
expect(service.getToolRender("tool2")).toBeUndefined();
|
|
100
|
+
expect(service.getToolRender("tool3")).toBeDefined();
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("Tool Registration Flow", () => {
|
|
105
|
+
it("should add tool to copilotkit instance", () => {
|
|
106
|
+
const addToolSpy = vi.spyOn(service.copilotkit, "addTool");
|
|
107
|
+
|
|
108
|
+
const tool = {
|
|
109
|
+
name: "flowTool",
|
|
110
|
+
description: "Test flow",
|
|
111
|
+
parameters: z.object({ input: z.string() }),
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
service.copilotkit.addTool(tool);
|
|
115
|
+
|
|
116
|
+
expect(addToolSpy).toHaveBeenCalledWith(tool);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("should remove tool from copilotkit instance", () => {
|
|
120
|
+
const removeToolSpy = vi.spyOn(service.copilotkit, "removeTool");
|
|
121
|
+
|
|
122
|
+
service.copilotkit.removeTool("testTool");
|
|
123
|
+
|
|
124
|
+
expect(removeToolSpy).toHaveBeenCalledWith("testTool");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should handle tool with all properties", () => {
|
|
128
|
+
const addToolSpy = vi.spyOn(service.copilotkit, "addTool");
|
|
129
|
+
|
|
130
|
+
const tool = {
|
|
131
|
+
name: "fullTool",
|
|
132
|
+
description: "Full featured tool",
|
|
133
|
+
parameters: z.object({
|
|
134
|
+
query: z.string(),
|
|
135
|
+
filters: z.object({
|
|
136
|
+
category: z.enum(["a", "b", "c"]),
|
|
137
|
+
}),
|
|
138
|
+
}),
|
|
139
|
+
handler: vi.fn(async () => ({ results: [] })),
|
|
140
|
+
followUp: true,
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
service.copilotkit.addTool(tool);
|
|
144
|
+
|
|
145
|
+
expect(addToolSpy).toHaveBeenCalledWith(
|
|
146
|
+
expect.objectContaining({
|
|
147
|
+
name: "fullTool",
|
|
148
|
+
description: "Full featured tool",
|
|
149
|
+
followUp: true,
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe("Render Tool Calls State", () => {
|
|
156
|
+
it("should update current render tool calls", () => {
|
|
157
|
+
const initial = service.currentRenderToolCalls();
|
|
158
|
+
expect(initial).toEqual([]);
|
|
159
|
+
|
|
160
|
+
const renders = [
|
|
161
|
+
{ name: "tool1", args: z.object({}), render: {} as any },
|
|
162
|
+
{ name: "tool2", args: z.object({}), render: {} as any },
|
|
163
|
+
];
|
|
164
|
+
|
|
165
|
+
service.setCurrentRenderToolCalls(renders);
|
|
166
|
+
|
|
167
|
+
expect(service.currentRenderToolCalls()).toEqual(renders);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should merge renders when registering", () => {
|
|
171
|
+
service.setCurrentRenderToolCalls([
|
|
172
|
+
{ name: "existing", args: z.object({}), render: {} as any },
|
|
173
|
+
]);
|
|
174
|
+
|
|
175
|
+
service.registerToolRender("newTool", {
|
|
176
|
+
name: "newTool",
|
|
177
|
+
args: z.object({}),
|
|
178
|
+
render: {} as any,
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const renders = service.currentRenderToolCalls();
|
|
182
|
+
expect(renders.find((r) => r.name === "existing")).toBeDefined();
|
|
183
|
+
expect(renders.find((r) => r.name === "newTool")).toBeDefined();
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should preserve other renders when unregistering", () => {
|
|
187
|
+
service.setCurrentRenderToolCalls([
|
|
188
|
+
{ name: "keep1", args: z.object({}), render: {} as any },
|
|
189
|
+
{ name: "remove", args: z.object({}), render: {} as any },
|
|
190
|
+
{ name: "keep2", args: z.object({}), render: {} as any },
|
|
191
|
+
]);
|
|
192
|
+
|
|
193
|
+
service.unregisterToolRender("remove");
|
|
194
|
+
|
|
195
|
+
const renders = service.currentRenderToolCalls();
|
|
196
|
+
expect(renders.map((r) => r.name)).toEqual(["keep1", "keep2"]);
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|