From 9326ff9d0873e2fe684dbd5df0eccdc3e1ae3403 Mon Sep 17 00:00:00 2001 From: lloydzhou Date: Fri, 30 Aug 2024 23:39:08 +0800 Subject: [PATCH] ts error --- app/client/platforms/openai.ts | 6 ++++-- app/components/chat.tsx | 17 +++++++++-------- app/components/markdown.tsx | 4 ++-- app/components/plugin.tsx | 8 +++++--- app/constant.ts | 2 +- app/store/mask.ts | 6 +++--- app/store/plugin.ts | 24 ++++++++++++++++-------- app/utils/chat.ts | 2 +- package.json | 1 + yarn.lock | 5 +++++ 10 files changed, 47 insertions(+), 28 deletions(-) diff --git a/app/client/platforms/openai.ts b/app/client/platforms/openai.ts index f0c577c7516..4c5831fe3e9 100644 --- a/app/client/platforms/openai.ts +++ b/app/client/platforms/openai.ts @@ -243,13 +243,15 @@ export class ChatGPTApi implements LLMApi { if (shouldStream) { const [tools, funcs] = usePluginStore .getState() - .getAsTools(useChatStore.getState().currentSession().mask?.plugin); + .getAsTools( + useChatStore.getState().currentSession().mask?.plugin as string[], + ); console.log("getAsTools", tools, funcs); stream( chatPath, requestPayload, getHeaders(), - tools, + tools as any, funcs, controller, // parseSSE diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 0e2aa3e2fe6..7bac62bc4c3 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -97,7 +97,7 @@ import { REQUEST_TIMEOUT_MS, UNFINISHED_INPUT, ServiceProvider, - Plugin, + ArtifactsPlugin, } from "../constant"; import { Avatar } from "./emoji"; import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask"; @@ -738,22 +738,23 @@ export function ChatActions(props: { items={[ { title: Locale.Plugin.Artifacts, - value: Plugin.Artifacts, + value: ArtifactsPlugin.Artifacts as string, }, ].concat( pluginStore.getAll().map((item) => ({ - title: `${item.title}@${item.version}`, - value: item.id, + // @ts-ignore + title: `${item?.title}@${item?.version}`, + // @ts-ignore + value: item?.id, })), )} onClose={() => setShowPluginSelector(false)} onSelection={(s) => { - const plugin = s[0]; chatStore.updateCurrentSession((session) => { - session.mask.plugin = s; + session.mask.plugin = s as string[]; }); - if (s.includes(Plugin.Artifacts)) { - showToast(Plugin.Artifacts); + if (s.includes(ArtifactsPlugin.Artifacts)) { + showToast(ArtifactsPlugin.Artifacts); } }} /> diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 500af71752f..58579ab47a4 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -19,7 +19,7 @@ import { HTMLPreview, HTMLPreviewHander, } from "./artifacts"; -import { Plugin } from "../constant"; +import { ArtifactsPlugin } from "../constant"; import { useChatStore } from "../store"; import { IconButton } from "./button"; @@ -95,7 +95,7 @@ export function PreCode(props: { children: any }) { }, 600); const enableArtifacts = useMemo( - () => plugins?.includes(Plugin.Artifacts), + () => plugins?.includes(ArtifactsPlugin.Artifacts), [plugins], ); diff --git a/app/components/plugin.tsx b/app/components/plugin.tsx index 247cba257cd..35cda90894b 100644 --- a/app/components/plugin.tsx +++ b/app/components/plugin.tsx @@ -46,8 +46,8 @@ export function PluginPage() { const onSearch = (text: string) => { setSearchText(text); if (text.length > 0) { - const result = allPlugins.filter((m) => - m.title.toLowerCase().includes(text.toLowerCase()), + const result = allPlugins.filter( + (m) => m?.title.toLowerCase().includes(text.toLowerCase()), ); setSearchPlugins(result); } else { @@ -63,7 +63,9 @@ export function PluginPage() { const onChangePlugin = useDebouncedCallback((editingPlugin, e) => { const content = e.target.innerText; try { - const api = new OpenAPIClientAxios({ definition: yaml.load(content) }); + const api = new OpenAPIClientAxios({ + definition: yaml.load(content) as any, + }); api .init() .then(() => { diff --git a/app/constant.ts b/app/constant.ts index 0fa14dca573..f6026d671c8 100644 --- a/app/constant.ts +++ b/app/constant.ts @@ -73,7 +73,7 @@ export enum FileName { Prompts = "prompts.json", } -export enum Plugin { +export enum ArtifactsPlugin { Artifacts = "artifacts", } diff --git a/app/store/mask.ts b/app/store/mask.ts index a790f89f833..05f511b0fbb 100644 --- a/app/store/mask.ts +++ b/app/store/mask.ts @@ -2,7 +2,7 @@ import { BUILTIN_MASKS } from "../masks"; import { getLang, Lang } from "../locales"; import { DEFAULT_TOPIC, ChatMessage } from "./chat"; import { ModelConfig, useAppConfig } from "./config"; -import { StoreKey, Plugin } from "../constant"; +import { StoreKey, ArtifactsPlugin } from "../constant"; import { nanoid } from "nanoid"; import { createPersistStore } from "../utils/store"; @@ -17,7 +17,7 @@ export type Mask = { modelConfig: ModelConfig; lang: Lang; builtin: boolean; - plugin?: Plugin[]; + plugin?: string[]; }; export const DEFAULT_MASK_STATE = { @@ -38,7 +38,7 @@ export const createEmptyMask = () => lang: getLang(), builtin: false, createdAt: Date.now(), - plugin: [Plugin.Artifacts], + plugin: [ArtifactsPlugin.Artifacts as string], }) as Mask; export const useMaskStore = createPersistStore( diff --git a/app/store/plugin.ts b/app/store/plugin.ts index b25b162a34b..031a2aaf55b 100644 --- a/app/store/plugin.ts +++ b/app/store/plugin.ts @@ -1,6 +1,6 @@ import OpenAPIClientAxios from "openapi-client-axios"; import { getLang, Lang } from "../locales"; -import { StoreKey, Plugin } from "../constant"; +import { StoreKey } from "../constant"; import { nanoid } from "nanoid"; import { createPersistStore } from "../utils/store"; import yaml from "js-yaml"; @@ -25,8 +25,9 @@ export type FunctionToolItem = { type FunctionToolServiceItem = { api: OpenAPIClientAxios; + length: number; tools: FunctionToolItem[]; - funcs: Function[]; + funcs: Record; }; export const FunctionToolService = { @@ -34,7 +35,7 @@ export const FunctionToolService = { add(plugin: Plugin, replace = false) { if (!replace && this.tools[plugin.id]) return this.tools[plugin.id]; const api = new OpenAPIClientAxios({ - definition: yaml.load(plugin.content), + definition: yaml.load(plugin.content) as any, }); console.log("add", plugin, api); try { @@ -45,6 +46,7 @@ export const FunctionToolService = { api, length: operations.length, tools: operations.map((o) => { + // @ts-ignore const parameters = o?.requestBody?.content["application/json"] ?.schema || { type: "object", @@ -55,14 +57,18 @@ export const FunctionToolService = { } if (o.parameters instanceof Array) { o.parameters.forEach((p) => { - if (p.in == "query" || p.in == "path") { + // @ts-ignore + if (p?.in == "query" || p?.in == "path") { // const name = `${p.in}__${p.name}` - const name = p.name; - console.log("p", p, p.schema); + // @ts-ignore + const name = p?.name; parameters["properties"][name] = { + // @ts-ignore type: p.schema.type, + // @ts-ignore description: p.description, }; + // @ts-ignore if (p.required) { parameters["required"].push(name); } @@ -76,15 +82,16 @@ export const FunctionToolService = { description: o.description, parameters: parameters, }, - }; + } as FunctionToolItem; }), funcs: operations.reduce((s, o) => { + // @ts-ignore s[o.operationId] = api.client[o.operationId]; return s; }, {}), }); }, - get(id) { + get(id: string) { return this.tools[id]; }, }; @@ -146,6 +153,7 @@ export const usePluginStore = createPersistStore( .filter((i) => i) .map((p) => FunctionToolService.add(p)); return [ + // @ts-ignore selected.reduce((s, i) => s.concat(i.tools), []), selected.reduce((s, i) => Object.assign(s, i.funcs), {}), ]; diff --git a/app/utils/chat.ts b/app/utils/chat.ts index aa268a9fe41..454a24771ca 100644 --- a/app/utils/chat.ts +++ b/app/utils/chat.ts @@ -158,7 +158,7 @@ export function stream( requestPayload: any, headers: any, tools: any[], - funcs: any, + funcs: Record, controller: AbortController, parseSSE: (text: string, runTools: any[]) => string | undefined, processToolMessage: ( diff --git a/package.json b/package.json index 6907bbf455f..82f23a4a0f8 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "@types/react-dom": "^18.2.7", "@types/react-katex": "^3.0.0", "@types/spark-md5": "^3.0.4", + "@types/js-yaml": "4.0.9", "concurrently": "^8.2.2", "cross-env": "^7.0.3", "eslint": "^8.49.0", diff --git a/yarn.lock b/yarn.lock index 6d8c07cc1d3..f138eb95738 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1684,6 +1684,11 @@ "@types/react" "*" hoist-non-react-statics "^3.3.0" +"@types/js-yaml@4.0.9": + version "4.0.9" + resolved "https://registry.npmjs.org/@types/js-yaml/-/js-yaml-4.0.9.tgz#cd82382c4f902fed9691a2ed79ec68c5898af4c2" + integrity sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg== + "@types/json-schema@*", "@types/json-schema@^7.0.8": version "7.0.12" resolved "https://registry.npmmirror.com/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"