Просмотр исходного кода

Merge remote-tracking branch 'connectai/main' into feature/using-tauri-fetch

lloydzhou 1 год назад
Родитель
Сommit
7df308d655
3 измененных файлов с 22 добавлено и 9 удалено
  1. 5 4
      app/api/google.ts
  2. 5 5
      app/store/plugin.ts
  3. 12 0
      app/utils.ts

+ 5 - 4
app/api/google.ts

@@ -23,7 +23,8 @@ export async function handle(
     });
     });
   }
   }
 
 
-  const bearToken = req.headers.get("Authorization") ?? "";
+  const bearToken =
+    req.headers.get("x-goog-api-key") || req.headers.get("Authorization") || "";
   const token = bearToken.trim().replaceAll("Bearer ", "").trim();
   const token = bearToken.trim().replaceAll("Bearer ", "").trim();
 
 
   const apiKey = token ? token : serverConfig.googleApiKey;
   const apiKey = token ? token : serverConfig.googleApiKey;
@@ -92,7 +93,7 @@ async function request(req: NextRequest, apiKey: string) {
     10 * 60 * 1000,
     10 * 60 * 1000,
   );
   );
   const fetchUrl = `${baseUrl}${path}${
   const fetchUrl = `${baseUrl}${path}${
-    req?.nextUrl?.searchParams?.get("alt") === "sse" ? "&alt=sse" : ""
+    req?.nextUrl?.searchParams?.get("alt") === "sse" ? "?alt=sse" : ""
   }`;
   }`;
 
 
   console.log("[Fetch Url] ", fetchUrl);
   console.log("[Fetch Url] ", fetchUrl);
@@ -100,8 +101,8 @@ async function request(req: NextRequest, apiKey: string) {
     headers: {
     headers: {
       "Content-Type": "application/json",
       "Content-Type": "application/json",
       "Cache-Control": "no-store",
       "Cache-Control": "no-store",
-      "x-google-api-key":
-        req.headers.get("x-google-api-key") ||
+      "x-goog-api-key":
+        req.headers.get("x-goog-api-key") ||
         (req.headers.get("Authorization") ?? "").replace("Bearer ", ""),
         (req.headers.get("Authorization") ?? "").replace("Bearer ", ""),
     },
     },
     method: req.method,
     method: req.method,

+ 5 - 5
app/store/plugin.ts

@@ -4,7 +4,7 @@ import { nanoid } from "nanoid";
 import { createPersistStore } from "../utils/store";
 import { createPersistStore } from "../utils/store";
 import { getClientConfig } from "../config/client";
 import { getClientConfig } from "../config/client";
 import yaml from "js-yaml";
 import yaml from "js-yaml";
-import { adapter } from "../utils";
+import { adapter, getOperationId } from "../utils";
 import { useAccessStore } from "./access";
 import { useAccessStore } from "./access";
 
 
 const isApp = getClientConfig()?.isApp;
 const isApp = getClientConfig()?.isApp;
@@ -116,7 +116,7 @@ export const FunctionToolService = {
         return {
         return {
           type: "function",
           type: "function",
           function: {
           function: {
-            name: o.operationId,
+            name: getOperationId(o),
             description: o.description || o.summary,
             description: o.description || o.summary,
             parameters: parameters,
             parameters: parameters,
           },
           },
@@ -124,7 +124,7 @@ export const FunctionToolService = {
       }),
       }),
       funcs: operations.reduce((s, o) => {
       funcs: operations.reduce((s, o) => {
         // @ts-ignore
         // @ts-ignore
-        s[o.operationId] = function (args) {
+        s[getOperationId(o)] = function (args) {
           const parameters: Record<string, any> = {};
           const parameters: Record<string, any> = {};
           if (o.parameters instanceof Array) {
           if (o.parameters instanceof Array) {
             o.parameters.forEach((p) => {
             o.parameters.forEach((p) => {
@@ -139,8 +139,8 @@ export const FunctionToolService = {
           } else if (authLocation == "body") {
           } else if (authLocation == "body") {
             args[headerName] = tokenValue;
             args[headerName] = tokenValue;
           }
           }
-          // @ts-ignore
-          return api.client[o.operationId](
+          // @ts-ignore if o.operationId is null, then using o.path and o.method
+          return api.client.paths[o.path][o.method](
             parameters,
             parameters,
             args,
             args,
             api.axiosConfigDefaults,
             api.axiosConfigDefaults,

+ 12 - 0
app/utils.ts

@@ -361,3 +361,15 @@ export function safeLocalStorage(): {
     },
     },
   };
   };
 }
 }
+
+export function getOperationId(operation: {
+  operationId?: string;
+  method: string;
+  path: string;
+}) {
+  // pattern '^[a-zA-Z0-9_-]+$'
+  return (
+    operation?.operationId ||
+    `${operation.method.toUpperCase()}${operation.path.replaceAll("/", "_")}`
+  );
+}