|
| 1 | +import OpenAPIClientAxios from "openapi-client-axios"; |
| 2 | +import { getLang, Lang } from "../locales"; |
| 3 | +import { StoreKey, Plugin } from "../constant"; |
| 4 | +import { nanoid } from "nanoid"; |
| 5 | +import { createPersistStore } from "../utils/store"; |
| 6 | +import yaml from "js-yaml"; |
| 7 | + |
| 8 | +export type Plugin = { |
| 9 | + id: string; |
| 10 | + createdAt: number; |
| 11 | + title: string; |
| 12 | + version: string; |
| 13 | + context: string; |
| 14 | + builtin: boolean; |
| 15 | +}; |
| 16 | + |
| 17 | +export const createEmptyPlugin = () => |
| 18 | + ({ |
| 19 | + id: nanoid(), |
| 20 | + title: "", |
| 21 | + version: "", |
| 22 | + context: "", |
| 23 | + builtin: false, |
| 24 | + createdAt: Date.now(), |
| 25 | + }) as Plugin; |
| 26 | + |
| 27 | +export const DEFAULT_PLUGIN_STATE = { |
| 28 | + plugins: {} as Record<string, Plugin>, |
| 29 | +}; |
| 30 | + |
| 31 | +export const usePluginStore = createPersistStore( |
| 32 | + { ...DEFAULT_PLUGIN_STATE }, |
| 33 | + |
| 34 | + (set, get) => ({ |
| 35 | + create(plugin?: Partial<Plugin>) { |
| 36 | + const plugins = get().plugins; |
| 37 | + const id = nanoid(); |
| 38 | + plugins[id] = { |
| 39 | + ...createEmptyPlugin(), |
| 40 | + ...plugin, |
| 41 | + id, |
| 42 | + builtin: false, |
| 43 | + }; |
| 44 | + |
| 45 | + set(() => ({ plugins })); |
| 46 | + get().markUpdate(); |
| 47 | + |
| 48 | + return plugins[id]; |
| 49 | + }, |
| 50 | + updatePlugin(id: string, updater: (plugin: Plugin) => void) { |
| 51 | + const plugins = get().plugins; |
| 52 | + const plugin = plugins[id]; |
| 53 | + if (!plugin) return; |
| 54 | + const updatePlugin = { ...plugin }; |
| 55 | + updater(updatePlugin); |
| 56 | + plugins[id] = updatePlugin; |
| 57 | + set(() => ({ plugins })); |
| 58 | + get().markUpdate(); |
| 59 | + }, |
| 60 | + delete(id: string) { |
| 61 | + const plugins = get().plugins; |
| 62 | + delete plugins[id]; |
| 63 | + set(() => ({ plugins })); |
| 64 | + get().markUpdate(); |
| 65 | + }, |
| 66 | + |
| 67 | + getAsTools(ids: string[]) { |
| 68 | + const plugins = get().plugins; |
| 69 | + const selected = ids |
| 70 | + .map((id) => plugins[id]) |
| 71 | + .filter((i) => i) |
| 72 | + .map((i) => [ |
| 73 | + i, |
| 74 | + new OpenAPIClientAxios({ definition: yaml.load(i.content) }), |
| 75 | + ]) |
| 76 | + .map(([item, api]) => { |
| 77 | + api.initSync(); |
| 78 | + const operations = api.getOperations().map((o) => { |
| 79 | + const parameters = o.parameters; |
| 80 | + return [ |
| 81 | + { |
| 82 | + type: "function", |
| 83 | + function: { |
| 84 | + name: o.operationId, |
| 85 | + description: o.description, |
| 86 | + parameters: o.parameters, |
| 87 | + }, |
| 88 | + }, |
| 89 | + api.client[o.operationId], |
| 90 | + ]; |
| 91 | + // return [{ |
| 92 | + // }, function(arg) { |
| 93 | + // const args = [] |
| 94 | + // for (const p in parameters) { |
| 95 | + // if (p.type === "object") { |
| 96 | + // const a = {} |
| 97 | + // for (const n of p.) |
| 98 | + // } |
| 99 | + // } |
| 100 | + // }] |
| 101 | + }); |
| 102 | + return [item, api, operations]; |
| 103 | + }); |
| 104 | + console.log("selected", selected); |
| 105 | + const result = selected.reduce((s, i) => s.concat(i[2]), []); |
| 106 | + return [ |
| 107 | + result.map(([t, _]) => t), |
| 108 | + result.reduce((s, i) => { |
| 109 | + s[i[0].function.name] = i[1]; |
| 110 | + return s; |
| 111 | + }, {}), |
| 112 | + ]; |
| 113 | + }, |
| 114 | + get(id?: string) { |
| 115 | + return get().plugins[id ?? 1145141919810]; |
| 116 | + }, |
| 117 | + getAll() { |
| 118 | + return Object.values(get().plugins).sort( |
| 119 | + (a, b) => b.createdAt - a.createdAt, |
| 120 | + ); |
| 121 | + }, |
| 122 | + }), |
| 123 | + { |
| 124 | + name: StoreKey.Plugin, |
| 125 | + version: 1, |
| 126 | + }, |
| 127 | +); |
0 commit comments