-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
87 lines (74 loc) · 2.56 KB
/
client.ts
File metadata and controls
87 lines (74 loc) · 2.56 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
import {Client4} from '@mattermost/client';
import type {AIBotInfo, AIToolInfo, Flow} from 'types';
const BASE_URL = '/plugins/com.mattermost.channel-automation/api/v1';
const client = new Client4();
async function doFetch<T>(url: string, options: {method?: string; body?: string} = {}): Promise<T> {
const fetchOptions = client.getOptions({
method: options.method || 'get',
headers: options.body ? {'Content-Type': 'application/json'} : {},
body: options.body,
});
const resp = await fetch(url, fetchOptions);
if (!resp.ok) {
let message = resp.statusText;
try {
const text = await resp.text();
try {
const body = JSON.parse(text);
if (body && body.error) {
message = body.error;
}
} catch {
// Not JSON — use the plain text response body directly
if (text.trim()) {
message = text.trim();
}
}
} catch {
// ignore read errors
}
throw new Error(message);
}
if (resp.status === 204) {
return undefined as unknown as T;
}
return resp.json() as Promise<T>;
}
export async function getFlows(): Promise<Flow[]> {
const flows = await doFetch<Flow[] | null>(`${BASE_URL}/flows`);
return flows ?? [];
}
export async function getFlow(id: string): Promise<Flow> {
return doFetch<Flow>(`${BASE_URL}/flows/${id}`);
}
export async function createFlow(data: Partial<Flow>): Promise<Flow> {
return doFetch<Flow>(`${BASE_URL}/flows`, {
method: 'POST',
body: JSON.stringify(data),
});
}
export async function updateFlow(id: string, data: Partial<Flow>): Promise<Flow> {
return doFetch<Flow>(`${BASE_URL}/flows/${id}`, {
method: 'PUT',
body: JSON.stringify(data),
});
}
export async function deleteFlow(id: string): Promise<void> {
await doFetch<void>(`${BASE_URL}/flows/${id}`, {
method: 'DELETE',
});
}
export async function getAIBots(): Promise<AIBotInfo[]> {
const resp = await doFetch<{bots: AIBotInfo[]}>('/plugins/mattermost-ai/ai_bots');
return resp.bots ?? [];
}
export async function getAgentTools(agentId: string): Promise<AIToolInfo[]> {
const tools = await doFetch<AIToolInfo[] | null>(`${BASE_URL}/agents/${agentId}/tools`);
return tools ?? [];
}
export interface ClientConfig {
enable_ui: boolean;
}
export async function getConfig(): Promise<ClientConfig> {
return doFetch<ClientConfig>(`${BASE_URL}/config`);
}