This commit is contained in:
lloydzhou 2024-08-30 23:39:08 +08:00
parent 271f58d9cf
commit 9326ff9d08
10 changed files with 47 additions and 28 deletions

View File

@ -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

View File

@ -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);
}
}}
/>

View File

@ -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],
);

View File

@ -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(() => {

View File

@ -73,7 +73,7 @@ export enum FileName {
Prompts = "prompts.json",
}
export enum Plugin {
export enum ArtifactsPlugin {
Artifacts = "artifacts",
}

View File

@ -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(

View File

@ -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<string, Function>;
};
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), {}),
];

View File

@ -158,7 +158,7 @@ export function stream(
requestPayload: any,
headers: any,
tools: any[],
funcs: any,
funcs: Record<string, Function>,
controller: AbortController,
parseSSE: (text: string, runTools: any[]) => string | undefined,
processToolMessage: (

View File

@ -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",

View File

@ -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"