-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathapp.ts
More file actions
70 lines (58 loc) · 2.11 KB
/
Copy pathapp.ts
File metadata and controls
70 lines (58 loc) · 2.11 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
import { Component, OnInit, signal } from '@angular/core';
import { connectToServer, log, type ServerInfo, type ToolCallInfo } from './implementation';
import { ThemeToggle } from './components/theme-toggle/theme-toggle';
import { CallToolPanel } from './components/call-tool-panel/call-tool-panel';
import {
ToolCallInfoPanel,
type ToolCallEntry,
} from './components/tool-call-info-panel/tool-call-info-panel';
function getQueryParams() {
const params = new URLSearchParams(window.location.search);
return {
server: params.get('server'),
tool: params.get('tool'),
call: params.get('call') === 'true',
hideThemeToggle: params.get('theme') === 'hide',
};
}
let nextToolCallId = 0;
@Component({
selector: 'app-root',
templateUrl: './app.html',
styleUrl: './app.scss',
imports: [ThemeToggle, CallToolPanel, ToolCallInfoPanel],
})
export class App implements OnInit {
servers = signal<ServerInfo[]>([]);
toolCalls = signal<ToolCallEntry[]>([]);
readonly queryParams = getQueryParams();
ngOnInit(): void {
connectToAllServers()
.then((s) => this.servers.set(s))
.catch((err) => log.error('Failed to connect to servers:', err));
}
addToolCall(info: ToolCallInfo): void {
this.toolCalls.update((calls) => [...calls, { ...info, id: nextToolCallId++ }]);
}
removeToolCall(id: number): void {
this.toolCalls.update((calls) => calls.filter((c) => c.id !== id));
}
}
async function connectToAllServers(): Promise<ServerInfo[]> {
const response = await fetch('/api/servers');
const serverUrls = (await response.json()) as string[];
const results = await Promise.allSettled(serverUrls.map((url) => connectToServer(new URL(url))));
const servers: ServerInfo[] = [];
for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.status === 'fulfilled') {
servers.push(result.value);
} else {
log.warn(`Failed to connect to ${serverUrls[i]}:`, result.reason);
}
}
if (servers.length === 0 && serverUrls.length > 0) {
throw new Error(`Failed to connect to any servers (${serverUrls.length} attempted)`);
}
return servers;
}