1
import { LobeChatPluginManifest, pluginManifestSchema } from '@lobehub/chat-plugin-sdk';
3
import { API_ENDPOINTS } from '@/services/_url';
4
import { OpenAIPluginManifest } from '@/types/openai/plugin';
6
const fetchJSON = async <T = any>(url: string, proxy = false): Promise<T> => {
10
res = await (proxy ? fetch(API_ENDPOINTS.proxy, { body: url, method: 'POST' }) : fetch(url));
12
throw new TypeError('fetchError');
16
throw new TypeError('fetchError');
20
const contentType = res.headers.get('Content-Type');
23
if (contentType === 'application/json') {
24
data = await res.json();
26
const { default: YAML } = await import('yaml');
28
const yaml = await res.text();
29
data = YAML.parse(yaml);
32
throw new TypeError('urlError');
38
export const convertOpenAIManifestToLobeManifest = (
39
data: OpenAIPluginManifest,
40
): LobeChatPluginManifest => {
41
const manifest: LobeChatPluginManifest = {
43
homepage: data.legal_info_url,
44
identifier: data.name_for_model,
46
avatar: data.logo_url,
47
description: data.description_for_human,
48
title: data.name_for_human,
50
openapi: data.api.url,
51
systemRole: data.description_for_model,
55
switch (data.auth.type) {
59
case 'service_http': {
63
default: data.auth.verification_tokens['openai'],
64
description: 'API Key',
79
export const getToolManifest = async (
81
useProxy: boolean = false,
82
): Promise<LobeChatPluginManifest> => {
85
throw new TypeError('noManifest');
90
let data = await fetchJSON<LobeChatPluginManifest>(url, useProxy);
95
if (data['description_for_model']) {
96
data = convertOpenAIManifestToLobeManifest(data as any);
100
const parser = pluginManifestSchema.safeParse(data);
102
if (!parser.success) {
103
throw new TypeError('manifestInvalid', { cause: parser.error });
107
if (parser.data.openapi) {
108
const openapiJson = await fetchJSON(parser.data.openapi, useProxy);
111
const { OpenAPIConvertor } = await import('@lobehub/chat-plugin-sdk/openapi');
113
const convertor = new OpenAPIConvertor(openapiJson);
114
const openAPIs = await convertor.convertOpenAPIToPluginSchema();
115
data.api = [...data.api, ...openAPIs];
117
data.settings = await convertor.convertAuthToSettingsSchema(data.settings);
119
throw new TypeError('openAPIInvalid', { cause: error });