Skip to content

Commit

Permalink
ts error
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Aug 30, 2024
1 parent 271f58d commit 9326ff9
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 28 deletions.
6 changes: 4 additions & 2 deletions app/client/platforms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}
}}
/>
Expand Down
4 changes: 2 additions & 2 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -95,7 +95,7 @@ export function PreCode(props: { children: any }) {
}, 600);

const enableArtifacts = useMemo(
() => plugins?.includes(Plugin.Artifacts),
() => plugins?.includes(ArtifactsPlugin.Artifacts),
[plugins],
);

Expand Down
8 changes: 5 additions & 3 deletions app/components/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export enum FileName {
Prompts = "prompts.json",
}

export enum Plugin {
export enum ArtifactsPlugin {
Artifacts = "artifacts",
}

Expand Down
6 changes: 3 additions & 3 deletions app/store/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -17,7 +17,7 @@ export type Mask = {
modelConfig: ModelConfig;
lang: Lang;
builtin: boolean;
plugin?: Plugin[];
plugin?: string[];
};

export const DEFAULT_MASK_STATE = {
Expand All @@ -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(
Expand Down
24 changes: 16 additions & 8 deletions app/store/plugin.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -25,16 +25,17 @@ export type FunctionToolItem = {

type FunctionToolServiceItem = {
api: OpenAPIClientAxios;
length: number;
tools: FunctionToolItem[];
funcs: Function[];
funcs: Record<string, Function>;
};

export const FunctionToolService = {
tools: {} as Record<string, FunctionToolServiceItem>,
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 {
Expand All @@ -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",
Expand All @@ -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);
}
Expand All @@ -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];
},
};
Expand Down Expand Up @@ -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), {}),
];
Expand Down
2 changes: 1 addition & 1 deletion app/utils/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9326ff9

Please sign in to comment.