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) { if (shouldStream) {
const [tools, funcs] = usePluginStore const [tools, funcs] = usePluginStore
.getState() .getState()
.getAsTools(useChatStore.getState().currentSession().mask?.plugin); .getAsTools(
useChatStore.getState().currentSession().mask?.plugin as string[],
);
console.log("getAsTools", tools, funcs); console.log("getAsTools", tools, funcs);
stream( stream(
chatPath, chatPath,
requestPayload, requestPayload,
getHeaders(), getHeaders(),
tools, tools as any,
funcs, funcs,
controller, controller,
// parseSSE // parseSSE

View File

@ -97,7 +97,7 @@ import {
REQUEST_TIMEOUT_MS, REQUEST_TIMEOUT_MS,
UNFINISHED_INPUT, UNFINISHED_INPUT,
ServiceProvider, ServiceProvider,
Plugin, ArtifactsPlugin,
} from "../constant"; } from "../constant";
import { Avatar } from "./emoji"; import { Avatar } from "./emoji";
import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask"; import { ContextPrompts, MaskAvatar, MaskConfig } from "./mask";
@ -738,22 +738,23 @@ export function ChatActions(props: {
items={[ items={[
{ {
title: Locale.Plugin.Artifacts, title: Locale.Plugin.Artifacts,
value: Plugin.Artifacts, value: ArtifactsPlugin.Artifacts as string,
}, },
].concat( ].concat(
pluginStore.getAll().map((item) => ({ pluginStore.getAll().map((item) => ({
title: `${item.title}@${item.version}`, // @ts-ignore
value: item.id, title: `${item?.title}@${item?.version}`,
// @ts-ignore
value: item?.id,
})), })),
)} )}
onClose={() => setShowPluginSelector(false)} onClose={() => setShowPluginSelector(false)}
onSelection={(s) => { onSelection={(s) => {
const plugin = s[0];
chatStore.updateCurrentSession((session) => { chatStore.updateCurrentSession((session) => {
session.mask.plugin = s; session.mask.plugin = s as string[];
}); });
if (s.includes(Plugin.Artifacts)) { if (s.includes(ArtifactsPlugin.Artifacts)) {
showToast(Plugin.Artifacts); showToast(ArtifactsPlugin.Artifacts);
} }
}} }}
/> />

View File

@ -19,7 +19,7 @@ import {
HTMLPreview, HTMLPreview,
HTMLPreviewHander, HTMLPreviewHander,
} from "./artifacts"; } from "./artifacts";
import { Plugin } from "../constant"; import { ArtifactsPlugin } from "../constant";
import { useChatStore } from "../store"; import { useChatStore } from "../store";
import { IconButton } from "./button"; import { IconButton } from "./button";
@ -95,7 +95,7 @@ export function PreCode(props: { children: any }) {
}, 600); }, 600);
const enableArtifacts = useMemo( const enableArtifacts = useMemo(
() => plugins?.includes(Plugin.Artifacts), () => plugins?.includes(ArtifactsPlugin.Artifacts),
[plugins], [plugins],
); );

View File

@ -46,8 +46,8 @@ export function PluginPage() {
const onSearch = (text: string) => { const onSearch = (text: string) => {
setSearchText(text); setSearchText(text);
if (text.length > 0) { if (text.length > 0) {
const result = allPlugins.filter((m) => const result = allPlugins.filter(
m.title.toLowerCase().includes(text.toLowerCase()), (m) => m?.title.toLowerCase().includes(text.toLowerCase()),
); );
setSearchPlugins(result); setSearchPlugins(result);
} else { } else {
@ -63,7 +63,9 @@ export function PluginPage() {
const onChangePlugin = useDebouncedCallback((editingPlugin, e) => { const onChangePlugin = useDebouncedCallback((editingPlugin, e) => {
const content = e.target.innerText; const content = e.target.innerText;
try { try {
const api = new OpenAPIClientAxios({ definition: yaml.load(content) }); const api = new OpenAPIClientAxios({
definition: yaml.load(content) as any,
});
api api
.init() .init()
.then(() => { .then(() => {

View File

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

View File

@ -2,7 +2,7 @@ import { BUILTIN_MASKS } from "../masks";
import { getLang, Lang } from "../locales"; import { getLang, Lang } from "../locales";
import { DEFAULT_TOPIC, ChatMessage } from "./chat"; import { DEFAULT_TOPIC, ChatMessage } from "./chat";
import { ModelConfig, useAppConfig } from "./config"; import { ModelConfig, useAppConfig } from "./config";
import { StoreKey, Plugin } from "../constant"; import { StoreKey, ArtifactsPlugin } from "../constant";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { createPersistStore } from "../utils/store"; import { createPersistStore } from "../utils/store";
@ -17,7 +17,7 @@ export type Mask = {
modelConfig: ModelConfig; modelConfig: ModelConfig;
lang: Lang; lang: Lang;
builtin: boolean; builtin: boolean;
plugin?: Plugin[]; plugin?: string[];
}; };
export const DEFAULT_MASK_STATE = { export const DEFAULT_MASK_STATE = {
@ -38,7 +38,7 @@ export const createEmptyMask = () =>
lang: getLang(), lang: getLang(),
builtin: false, builtin: false,
createdAt: Date.now(), createdAt: Date.now(),
plugin: [Plugin.Artifacts], plugin: [ArtifactsPlugin.Artifacts as string],
}) as Mask; }) as Mask;
export const useMaskStore = createPersistStore( export const useMaskStore = createPersistStore(

View File

@ -1,6 +1,6 @@
import OpenAPIClientAxios from "openapi-client-axios"; import OpenAPIClientAxios from "openapi-client-axios";
import { getLang, Lang } from "../locales"; import { getLang, Lang } from "../locales";
import { StoreKey, Plugin } from "../constant"; import { StoreKey } from "../constant";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { createPersistStore } from "../utils/store"; import { createPersistStore } from "../utils/store";
import yaml from "js-yaml"; import yaml from "js-yaml";
@ -25,8 +25,9 @@ export type FunctionToolItem = {
type FunctionToolServiceItem = { type FunctionToolServiceItem = {
api: OpenAPIClientAxios; api: OpenAPIClientAxios;
length: number;
tools: FunctionToolItem[]; tools: FunctionToolItem[];
funcs: Function[]; funcs: Record<string, Function>;
}; };
export const FunctionToolService = { export const FunctionToolService = {
@ -34,7 +35,7 @@ export const FunctionToolService = {
add(plugin: Plugin, replace = false) { add(plugin: Plugin, replace = false) {
if (!replace && this.tools[plugin.id]) return this.tools[plugin.id]; if (!replace && this.tools[plugin.id]) return this.tools[plugin.id];
const api = new OpenAPIClientAxios({ const api = new OpenAPIClientAxios({
definition: yaml.load(plugin.content), definition: yaml.load(plugin.content) as any,
}); });
console.log("add", plugin, api); console.log("add", plugin, api);
try { try {
@ -45,6 +46,7 @@ export const FunctionToolService = {
api, api,
length: operations.length, length: operations.length,
tools: operations.map((o) => { tools: operations.map((o) => {
// @ts-ignore
const parameters = o?.requestBody?.content["application/json"] const parameters = o?.requestBody?.content["application/json"]
?.schema || { ?.schema || {
type: "object", type: "object",
@ -55,14 +57,18 @@ export const FunctionToolService = {
} }
if (o.parameters instanceof Array) { if (o.parameters instanceof Array) {
o.parameters.forEach((p) => { 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.in}__${p.name}`
const name = p.name; // @ts-ignore
console.log("p", p, p.schema); const name = p?.name;
parameters["properties"][name] = { parameters["properties"][name] = {
// @ts-ignore
type: p.schema.type, type: p.schema.type,
// @ts-ignore
description: p.description, description: p.description,
}; };
// @ts-ignore
if (p.required) { if (p.required) {
parameters["required"].push(name); parameters["required"].push(name);
} }
@ -76,15 +82,16 @@ export const FunctionToolService = {
description: o.description, description: o.description,
parameters: parameters, parameters: parameters,
}, },
}; } as FunctionToolItem;
}), }),
funcs: operations.reduce((s, o) => { funcs: operations.reduce((s, o) => {
// @ts-ignore
s[o.operationId] = api.client[o.operationId]; s[o.operationId] = api.client[o.operationId];
return s; return s;
}, {}), }, {}),
}); });
}, },
get(id) { get(id: string) {
return this.tools[id]; return this.tools[id];
}, },
}; };
@ -146,6 +153,7 @@ export const usePluginStore = createPersistStore(
.filter((i) => i) .filter((i) => i)
.map((p) => FunctionToolService.add(p)); .map((p) => FunctionToolService.add(p));
return [ return [
// @ts-ignore
selected.reduce((s, i) => s.concat(i.tools), []), selected.reduce((s, i) => s.concat(i.tools), []),
selected.reduce((s, i) => Object.assign(s, i.funcs), {}), selected.reduce((s, i) => Object.assign(s, i.funcs), {}),
]; ];

View File

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

View File

@ -57,6 +57,7 @@
"@types/react-dom": "^18.2.7", "@types/react-dom": "^18.2.7",
"@types/react-katex": "^3.0.0", "@types/react-katex": "^3.0.0",
"@types/spark-md5": "^3.0.4", "@types/spark-md5": "^3.0.4",
"@types/js-yaml": "4.0.9",
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"eslint": "^8.49.0", "eslint": "^8.49.0",

View File

@ -1684,6 +1684,11 @@
"@types/react" "*" "@types/react" "*"
hoist-non-react-statics "^3.3.0" 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": "@types/json-schema@*", "@types/json-schema@^7.0.8":
version "7.0.12" version "7.0.12"
resolved "https://registry.npmmirror.com/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb" resolved "https://registry.npmmirror.com/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"