plugin.ts 3.2 KB

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