Spaces:
Paused

File size: 2,911 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { request } from "../request";
import type { ChannelConfig, SingleChannelConfig } from "../types";

/**
 * Localized text: either a plain string, or a mapping from locale code
 * (e.g. "zh-CN", "en-US", "zh", "en") to the display string.
 * Plain strings are shown as-is; dict values are resolved against the
 * current UI language with graceful fallback (see resolveLocalized).
 */
export type LocalizedText = string | Record<string, string>;

export interface ChannelConfigField {
  name: string;
  label: LocalizedText;
  type: "text" | "password" | "number" | "switch" | "select";
  required?: boolean;
  placeholder?: LocalizedText;
  help?: LocalizedText;
  default?: unknown;
  options?: string[];
}

export interface ChannelSchema {
  label: string;
  description: string;
  plugin_id: string;
  config_fields: ChannelConfigField[];
  icon?: string;
  doc_url?: LocalizedText;
}

export interface ChannelConflictAgent {
  agent_id: string;
  agent_name: string;
}

export interface ChannelConflictResponse {
  conflict: boolean;
  agents: ChannelConflictAgent[];
}

export const channelApi = {
  listChannelTypes: () => request<string[]>("/config/channels/types"),

  listChannels: () => request<ChannelConfig>("/config/channels"),

  listChannelSchemas: () =>
    request<Record<string, ChannelSchema>>("/config/channels/schemas"),

  updateChannels: (body: ChannelConfig) =>
    request<ChannelConfig>("/config/channels", {
      method: "PUT",
      body: JSON.stringify(body),
    }),

  getChannelConfig: (channelName: string) =>
    request<SingleChannelConfig>(
      `/config/channels/${encodeURIComponent(channelName)}`,
    ),

  updateChannelConfig: (channelName: string, body: SingleChannelConfig) =>
    request<SingleChannelConfig>(
      `/config/channels/${encodeURIComponent(channelName)}`,
      {
        method: "PUT",
        body: JSON.stringify(body),
      },
    ),

  checkChannelConflict: (channelName: string, body: SingleChannelConfig) =>
    request<ChannelConflictResponse>(
      `/config/channels/${encodeURIComponent(channelName)}/conflict-check`,
      {
        method: "POST",
        body: JSON.stringify(body),
      },
    ),

  getChannelQrcode: (channel: string, params?: Record<string, string>) => {
    const qs = params ? "?" + new URLSearchParams(params).toString() : "";
    return request<{ qrcode_img: string; poll_token: string }>(
      `/config/channels/${encodeURIComponent(channel)}/qrcode${qs}`,
    );
  },

  getChannelQrcodeStatus: (
    channel: string,
    token: string,
    params?: Record<string, string>,
  ) => {
    const extra = params ? "&" + new URLSearchParams(params).toString() : "";
    return request<{
      status: string;
      credentials: Record<string, string>;
    }>(
      `/config/channels/${encodeURIComponent(
        channel,
      )}/qrcode/status?token=${encodeURIComponent(token)}${extra}`,
    );
  },
};