Spaces:
reon314
/
Runtime error

File size: 3,620 Bytes
3b6afc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require('dotenv').config();
const { z } = require('zod');
const fs = require('fs');
const yaml = require('js-yaml');
const path = require('path');
const { DynamicStructuredTool } = require('langchain/tools');
const { createOpenAPIChain } = require('langchain/chains');
const SUFFIX = 'Prioritize using responses for subsequent requests to better fulfill the query.';

const AuthBearer = z
  .object({
    type: z.string().includes('service_http'),
    authorization_type: z.string().includes('bearer'),
    verification_tokens: z.object({
      openai: z.string(),
    }),
  })
  .catch(() => false);

const AuthDefinition = z
  .object({
    type: z.string(),
    authorization_type: z.string(),
    verification_tokens: z.object({
      openai: z.string(),
    }),
  })
  .catch(() => false);

async function readSpecFile(filePath) {
  try {
    const fileContents = await fs.promises.readFile(filePath, 'utf8');
    if (path.extname(filePath) === '.json') {
      return JSON.parse(fileContents);
    }
    return yaml.load(fileContents);
  } catch (e) {
    console.error(e);
    return false;
  }
}

async function getSpec(url) {
  const RegularUrl = z
    .string()
    .url()
    .catch(() => false);

  if (RegularUrl.parse(url) && path.extname(url) === '.json') {
    const response = await fetch(url);
    return await response.json();
  }

  const ValidSpecPath = z
    .string()
    .url()
    .catch(async () => {
      const spec = path.join(__dirname, '..', '.well-known', 'openapi', url);
      if (!fs.existsSync(spec)) {
        return false;
      }

      return await readSpecFile(spec);
    });

  return ValidSpecPath.parse(url);
}

async function createOpenAPIPlugin({ data, llm, user, message, verbose = false }) {
  let spec;
  try {
    spec = await getSpec(data.api.url, verbose);
  } catch (error) {
    verbose && console.debug('getSpec error', error);
    return null;
  }

  if (!spec) {
    verbose && console.debug('No spec found');
    return null;
  }

  const headers = {};
  const { auth, description_for_model } = data;
  if (auth && AuthDefinition.parse(auth)) {
    verbose && console.debug('auth detected', auth);
    const { openai } = auth.verification_tokens;
    if (AuthBearer.parse(auth)) {
      headers.authorization = `Bearer ${openai}`;
      verbose && console.debug('added auth bearer', headers);
    }
  }

  return new DynamicStructuredTool({
    name: data.name_for_model,
    description: `${data.description_for_human} ${SUFFIX}`,
    schema: z.object({
      query: z
        .string()
        .describe(
          'For the query, be specific in a conversational manner. It will be interpreted by a human.',
        ),
    }),
    func: async () => {
      const chainOptions = {
        llm,
        verbose,
      };

      if (data.headers && data.headers['librechat_user_id']) {
        verbose && console.debug('id detected', headers);
        headers[data.headers['librechat_user_id']] = user;
      }

      if (Object.keys(headers).length > 0) {
        verbose && console.debug('headers detected', headers);
        chainOptions.headers = headers;
      }

      if (data.params) {
        verbose && console.debug('params detected', data.params);
        chainOptions.params = data.params;
      }

      const chain = await createOpenAPIChain(spec, chainOptions);
      const result = await chain.run(
        `${message}\n\n||>Instructions: ${description_for_model}\n${SUFFIX}`,
      );
      console.log('api chain run result', result);
      return result;
    },
  });
}

module.exports = {
  getSpec,
  readSpecFile,
  createOpenAPIPlugin,
};