File size: 2,947 Bytes
c1f12bf
d079437
c1f12bf
 
 
 
 
 
 
 
 
d079437
c1f12bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d079437
 
 
 
 
 
 
 
 
 
 
 
c1f12bf
d079437
 
 
 
 
c1f12bf
 
 
 
d079437
 
 
 
 
 
 
299bd34
d079437
 
 
24c6b45
083ce88
 
 
 
 
d079437
 
 
 
 
 
083ce88
299bd34
88a7433
d079437
 
 
 
 
 
 
 
299bd34
d079437
 
 
 
 
 
 
c1f12bf
 
d079437
c1f12bf
 
d079437
 
 
c1f12bf
d079437
c1f12bf
d079437
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use client'

import { create } from 'zustand'
import {
  ClapperPlugin,
  ClapperPluginApi,
  ClapperPluginMeta,
  PluginsStore,
  PublicServices,
} from '@aitube/clapper-services'
import { useTimeline } from '@aitube/timeline'

import { getDefaultPluginsState } from './getDefaultPluginsState'
import { useScriptEditor } from '../editors/script-editor/useScriptEditor'
import { useMonitor } from '../monitor/useMonitor'
import { useTasks } from '@/components/tasks/useTasks'
import { useRenderer } from '../renderer'
import { useBroadcast } from '../broadcast/useBroadcast'
import { useResolver } from '../resolver/useResolver'
import { useAssistant } from '../assistant/useAssistant'
import { useAudio } from '../audio/useAudio'
import { useUI } from '../ui'
import { fetchAndRun } from './fetchAndRun'
import {
  useEditors,
  useEntityEditor,
  useProjectEditor,
  useSegmentEditor,
} from '../editors'
import { useSimulator } from '../simulator/useSimulator'
import { useIO } from '../io/useIO'
import { useMic } from '../mic/useMic'

export const usePlugins = create<PluginsStore>((set, get) => ({
  ...getDefaultPluginsState(),

  refreshAvailablePlugins: async () => {
    // TODO fetch the plugins from somewhere,
    // eg github, a .json file,
    // clapper.app/plugins.json etc

    set({
      availablePlugins: [
        //
      ],
    })
  },

  install: async (id: string) => {
    const { availablePlugins } = get()
    const plugin = availablePlugins.find((p) => p.id === id)
    if (!plugin) {
      throw new Error(`couldn't find plugin "${id}"`)
    }

    await fetchAndRun(plugin.assetUrl)
  },
  uninstall: async (id: string) => {
    // TODO
  },

  pluginApiGetServices: async (id: string): Promise<PublicServices> => {
    return {
      audio: useAudio,
      assistant: useAssistant,
      mic: useMic,
      segmentEditor: useSegmentEditor,
      entityEditor: useEntityEditor,
      projectEditor: useProjectEditor,
      scriptEditor: useScriptEditor,
      editors: useEditors,
      monitor: useMonitor,
      tasks: useTasks,
      timeline: useTimeline,
      renderer: useRenderer,
      resolver: useResolver,
      broadcast: useBroadcast,
      simulator: useSimulator,
      ui: useUI,
      io: useIO,
    }
  },
  pluginApiGetSettings: async (id: string) => {
    return []
  },

  connect: async (plugin: ClapperPlugin) => {
    const api: ClapperPluginApi = {
      getServices: async (): Promise<PublicServices> => {
        return get().pluginApiGetServices(plugin.meta.id)
      },
      getSettings: async () => {
        return get().pluginApiGetSettings(plugin.meta.id)
      },
    }
    return api
  },
}))

if (typeof window !== 'undefined') {
  ;(window as any).usePlugins = usePlugins

  // plugins will have to execute:
  // usePlugins.getState().connect(<...>)
  ;(window as any).installPlugin = async (plugin: ClapperPlugin) => {
    return usePlugins.getState().connect(plugin)
  }
}