-
Notifications
You must be signed in to change notification settings - Fork 22
/
studio.ts
219 lines (185 loc) · 7.58 KB
/
studio.ts
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**********************************************************************
* Copyright (C) 2024 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { Uri, window, env } from '@podman-desktop/api';
import type {
ExtensionContext,
TelemetryLogger,
WebviewOptions,
WebviewPanel,
WebviewPanelOnDidChangeViewStateEvent,
} from '@podman-desktop/api';
import { RpcExtension } from '@shared/src/messages/MessageProxy';
import { StudioApiImpl } from './studio-api-impl';
import { ApplicationManager } from './managers/applicationManager';
import { GitManager } from './managers/gitManager';
import { TaskRegistry } from './registries/TaskRegistry';
import { PlayGroundManager } from './managers/playground';
import { CatalogManager } from './managers/catalogManager';
import { ModelsManager } from './managers/modelsManager';
import path from 'node:path';
import os from 'os';
import fs from 'node:fs';
import { ContainerRegistry } from './registries/ContainerRegistry';
import { PodmanConnection } from './managers/podmanConnection';
import { LocalRepositoryRegistry } from './registries/LocalRepositoryRegistry';
import { InferenceManager } from './managers/inference/inferenceManager';
import { PlaygroundV2Manager } from './managers/playgroundV2Manager';
import { SnippetManager } from './managers/SnippetManager';
// TODO: Need to be configured
export const AI_STUDIO_FOLDER = path.join('podman-desktop', 'ai-studio');
export class Studio {
readonly #extensionContext: ExtensionContext;
#panel: WebviewPanel | undefined;
rpcExtension: RpcExtension;
studioApi: StudioApiImpl;
playgroundManager: PlayGroundManager;
catalogManager: CatalogManager;
modelsManager: ModelsManager;
telemetry: TelemetryLogger;
#inferenceManager: InferenceManager;
constructor(readonly extensionContext: ExtensionContext) {
this.#extensionContext = extensionContext;
}
public async activate(): Promise<void> {
console.log('starting studio extension');
this.telemetry = env.createTelemetryLogger();
this.telemetry.logUsage('start');
const extensionUri = this.#extensionContext.extensionUri;
// register webview
this.#panel = window.createWebviewPanel('studio', 'AI Studio', this.getWebviewOptions(extensionUri));
this.#extensionContext.subscriptions.push(this.#panel);
// update html
const indexHtmlUri = Uri.joinPath(extensionUri, 'media', 'index.html');
const indexHtmlPath = indexHtmlUri.fsPath;
let indexHtml = await fs.promises.readFile(indexHtmlPath, 'utf8');
// replace links with webView Uri links
// in the content <script type="module" crossorigin src="./index-RKnfBG18.js"></script> replace src with webview.asWebviewUri
const scriptLink = indexHtml.match(/<script.*?src="(.*?)".*?>/g);
if (scriptLink) {
scriptLink.forEach(link => {
const src = link.match(/src="(.*?)"/);
if (src) {
const webviewSrc = this.#panel?.webview.asWebviewUri(Uri.joinPath(extensionUri, 'media', src[1]));
indexHtml = indexHtml.replace(src[1], webviewSrc.toString());
}
});
}
// and now replace for css file as well
const cssLink = indexHtml.match(/<link.*?href="(.*?)".*?>/g);
if (cssLink) {
cssLink.forEach(link => {
const href = link.match(/href="(.*?)"/);
if (href) {
const webviewHref = this.#panel?.webview.asWebviewUri(Uri.joinPath(extensionUri, 'media', href[1]));
indexHtml = indexHtml.replace(href[1], webviewHref.toString());
}
});
}
console.log('updated indexHtml to', indexHtml);
this.#panel.webview.html = indexHtml;
// Creating container registry
const containerRegistry = new ContainerRegistry();
this.#extensionContext.subscriptions.push(containerRegistry.init());
// Let's create the api that the front will be able to call
const appUserDirectory = path.join(os.homedir(), AI_STUDIO_FOLDER);
this.rpcExtension = new RpcExtension(this.#panel.webview);
const gitManager = new GitManager();
const podmanConnection = new PodmanConnection();
const taskRegistry = new TaskRegistry(this.#panel.webview);
this.playgroundManager = new PlayGroundManager(
this.#panel.webview,
containerRegistry,
podmanConnection,
this.telemetry,
);
// Create catalog manager, responsible for loading the catalog files and watching for changes
this.catalogManager = new CatalogManager(this.#panel.webview, appUserDirectory);
this.modelsManager = new ModelsManager(
appUserDirectory,
this.#panel.webview,
this.catalogManager,
this.telemetry,
taskRegistry,
);
const localRepositoryRegistry = new LocalRepositoryRegistry(this.#panel.webview);
const applicationManager = new ApplicationManager(
appUserDirectory,
gitManager,
taskRegistry,
this.#panel.webview,
podmanConnection,
this.catalogManager,
this.modelsManager,
this.telemetry,
localRepositoryRegistry,
);
this.#inferenceManager = new InferenceManager(
this.#panel.webview,
containerRegistry,
podmanConnection,
this.modelsManager,
this.telemetry,
);
this.#panel.onDidChangeViewState((e: WebviewPanelOnDidChangeViewStateEvent) => {
// Lazily init inference manager
if (!this.#inferenceManager.isInitialize()) {
this.#inferenceManager.init();
this.#extensionContext.subscriptions.push(this.#inferenceManager);
}
this.telemetry.logUsage(e.webviewPanel.visible ? 'opened' : 'closed');
});
const playgroundV2 = new PlaygroundV2Manager(this.#panel.webview, this.#inferenceManager);
const snippetManager = new SnippetManager(this.#panel.webview);
snippetManager.init();
// Creating StudioApiImpl
this.studioApi = new StudioApiImpl(
applicationManager,
this.playgroundManager,
this.catalogManager,
this.modelsManager,
this.telemetry,
localRepositoryRegistry,
taskRegistry,
this.#inferenceManager,
playgroundV2,
snippetManager,
);
this.catalogManager.init();
await this.modelsManager.loadLocalModels();
podmanConnection.init();
this.playgroundManager.adoptRunningPlaygrounds();
applicationManager.adoptRunningApplications();
// Register the instance
this.rpcExtension.registerInstance<StudioApiImpl>(StudioApiImpl, this.studioApi);
this.#extensionContext.subscriptions.push(this.catalogManager);
this.#extensionContext.subscriptions.push(this.modelsManager);
this.#extensionContext.subscriptions.push(podmanConnection);
}
public async deactivate(): Promise<void> {
console.log('stopping studio extension');
this.telemetry.logUsage('stop');
}
getWebviewOptions(extensionUri: Uri): WebviewOptions {
return {
// Enable javascript in the webview
// enableScripts: true,
// And restrict the webview to only loading content from our extension's `media` directory.
localResourceRoots: [Uri.joinPath(extensionUri, 'media')],
};
}
}