Skip to content

Commit

Permalink
feat: upgrade agent
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector committed Mar 19, 2024
1 parent acf7485 commit a319b50
Show file tree
Hide file tree
Showing 24 changed files with 197 additions and 881 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
"configurations": [
{
"name": "SecretNote: Server",
"type": "python",
"type": "debugpy",
"request": "launch",
// python -m secretnote.server
"module": "secretnote.server",
"args": ["--no-browser"],
"justMyCode": false,
"cwd": "${workspaceFolder}/pyprojects/secretnote"
},
{
"name": "Pytest",
"type": "python",
"type": "debugpy",
"request": "launch",
"module": "pytest",
"args": ["-s", "-v", "--no-cov", "-Werror", "pyprojects"],
Expand Down
8 changes: 7 additions & 1 deletion packages/secretnote/.umirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export default defineConfig({
]
: false,
proxy: {
'/api': {
'/secretnoteagent/': {
target: 'http://localhost:8888/',
changeOrigin: true,
secure: false,
ws: true,
},
'/api/': {
target: 'http://localhost:8888/',
changeOrigin: true,
secure: false,
Expand Down
3 changes: 1 addition & 2 deletions packages/secretnote/src/modules/editor/cell/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
CellService,
JupyterCodeCellView,
KernelError,
ILSPDocumentConnectionManager,
CodeEditorManager,
} from '@difizen/libro-jupyter';
import {
Expand Down Expand Up @@ -86,7 +85,7 @@ export class SecretNoteCodeCellView extends JupyterCodeCellView {
}

getUsableConnections() {
const libroModel = this.parent.model as SecretNoteModel;
const libroModel = this.parent.model as unknown as SecretNoteModel;

if (!libroModel) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { NotebookModel, NotebookOption } from '@difizen/libro-jupyter';
import { ContentContribution } from '@difizen/libro-jupyter';
import { URI, singleton } from '@difizen/mana-app';

import { getLocalBaseUrl } from '@/utils';

import type { SecretNoteModel } from '../model';

@singleton({ contrib: ContentContribution })
Expand All @@ -14,7 +16,10 @@ export class SecretNoteContentContribution implements ContentContribution {
const fireUri = new URI(options.resource);
const filePath = fireUri.path.toString();

const currentFileContents = await secretNoteModel.contentsManager.get(filePath);
const currentFileContents = await secretNoteModel.contentsManager.get(filePath, {
baseUrl: getLocalBaseUrl(),
content: true,
});
if (currentFileContents) {
currentFileContents.content.nbformat_minor = 5;
secretNoteModel.currentFileContents = currentFileContents;
Expand Down
14 changes: 11 additions & 3 deletions packages/secretnote/src/modules/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { debounce } from 'lodash-es';
import { SecretNoteKernelManager } from '@/modules/kernel';
import { SecretNoteServerManager } from '@/modules/server';
import type { IServer } from '@/modules/server';
import { getLocalBaseUrl } from '@/utils';

@transient()
export class SecretNoteModel extends LibroModel {
Expand Down Expand Up @@ -66,7 +67,7 @@ export class SecretNoteModel extends LibroModel {
this.commandRegistry = commandRegistry;
this.serverManager.onServerAdded(this.onServerAdded.bind(this));
this.serverManager.onServerDeleted(this.onServerDeleted.bind(this));
this.onSourceChanged(this.autoSave.bind(this));
this.onChanged(this.autoSave.bind(this));
}

async startKernelConnection() {
Expand Down Expand Up @@ -100,6 +101,7 @@ export class SecretNoteModel extends LibroModel {
type: this.currentFileContents.type,
content: notebookContent,
format: this.currentFileContents.format,
baseUrl: getLocalBaseUrl(),
});

if (!res) {
Expand Down Expand Up @@ -204,13 +206,17 @@ export class SecretNoteModel extends LibroModel {

async createCheckpoint() {
if (this.currentFileContents) {
await this.contentsManager.createCheckpoint(this.currentFileContents.path);
await this.contentsManager.createCheckpoint(this.currentFileContents.path, {
baseUrl: getLocalBaseUrl(),
});
}
}

async listCheckpoints() {
if (this.currentFileContents) {
await this.contentsManager.listCheckpoints(this.currentFileContents.path);
await this.contentsManager.listCheckpoints(this.currentFileContents.path, {
baseUrl: getLocalBaseUrl(),
});
}
}

Expand All @@ -219,6 +225,7 @@ export class SecretNoteModel extends LibroModel {
await this.contentsManager.restoreCheckpoint(
this.currentFileContents.path,
checkpointID,
{ baseUrl: getLocalBaseUrl() },
);
}
}
Expand All @@ -228,6 +235,7 @@ export class SecretNoteModel extends LibroModel {
await this.contentsManager.deleteCheckpoint(
this.currentFileContents.path,
checkpointID,
{ baseUrl: getLocalBaseUrl() },
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions packages/secretnote/src/modules/file/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContentsManager } from '@difizen/libro-jupyter';
import { inject, prop, singleton } from '@difizen/mana-app';
import type { DataNode } from 'antd/es/tree';

import { downloadFileByUrl as download } from '@/utils';
import { downloadFileByUrl as download, getRemoteBaseUrl } from '@/utils';

import { SecretNoteServerManager } from '../server';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class FileService {

try {
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: this.serverManager.getServerUrl(server).baseUrl,
baseUrl: getRemoteBaseUrl(server.id),
content: true,
});

Expand Down Expand Up @@ -79,7 +79,7 @@ export class FileService {
return false;
}
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: this.serverManager.getServerUrl(server).baseUrl,
baseUrl: getRemoteBaseUrl(server.id),
content: true,
});

Expand All @@ -90,7 +90,7 @@ export class FileService {
const serverId = nodeData.key as string;
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
const baseUrl = this.serverManager.getServerUrl(server).baseUrl;
const baseUrl = getRemoteBaseUrl(server.id);
const path = `${BASE_PATH}/${name}`;
await this.contentsManager.save(path, {
content,
Expand All @@ -108,7 +108,7 @@ export class FileService {
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
const data = await this.contentsManager.getDownloadUrl(path, {
baseUrl: this.serverManager.getServerUrl(server).baseUrl,
baseUrl: getRemoteBaseUrl(server.id),
});
download(data, nodeData.title as string);
}
Expand All @@ -122,7 +122,7 @@ export class FileService {
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
await this.contentsManager.delete(path, {
baseUrl: this.serverManager.getServerUrl(server).baseUrl,
baseUrl: getRemoteBaseUrl(server.id),
});
await this.getFileTree();
}
Expand All @@ -133,7 +133,7 @@ export class FileService {
const server = await this.serverManager.getServerDetail(serverId);
if (server) {
const data = await this.contentsManager.get(decodedPath, {
baseUrl: this.serverManager.getServerUrl(server).baseUrl,
baseUrl: getRemoteBaseUrl(server.id),
content: true,
});
return data;
Expand Down
24 changes: 17 additions & 7 deletions packages/secretnote/src/modules/kernel/kernel-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { inject, singleton, StorageService } from '@difizen/mana-app';

import { SecretNoteServerManager } from '@/modules/server';
import type { IServer } from '@/modules/server';
import { getRemoteBaseUrl, getRemoteWsUrl } from '@/utils';

interface StoredSessionInfo {
sessionId: string;
Expand Down Expand Up @@ -72,7 +73,10 @@ export class SecretNoteKernelManager {
if (isAlive) {
connection = await this.connectToKernel({
...options,
serverSettings: this.serverManager.getServerUrl(s),
serverSettings: {
baseUrl: getRemoteBaseUrl(s.id),
wsUrl: getRemoteWsUrl(s.id),
},
});
} else {
await this.removeStoredSession(fileInfo, hit);
Expand Down Expand Up @@ -168,7 +172,10 @@ export class SecretNoteKernelManager {
path: fileInfo.path,
type: fileInfo.type,
} as ISessionOptions,
this.serverManager.getServerUrl(server),
{
baseUrl: getRemoteBaseUrl(server.id),
wsUrl: getRemoteWsUrl(server.id),
},
);

if (!newSession || !newSession.kernel) {
Expand All @@ -190,7 +197,10 @@ export class SecretNoteKernelManager {

const kernelConnection = await this.connectToKernel({
...options,
serverSettings: this.serverManager.getServerUrl(server),
serverSettings: {
baseUrl: getRemoteBaseUrl(server.id),
wsUrl: getRemoteWsUrl(server.id),
},
});

return kernelConnection;
Expand All @@ -203,10 +213,10 @@ export class SecretNoteKernelManager {

protected async isKernelAlive(id: string, server: IServer): Promise<boolean> {
try {
const data = await this.kernelRestAPI.getKernelModel(
id,
this.serverManager.getServerUrl(server),
);
const data = await this.kernelRestAPI.getKernelModel(id, {
baseUrl: getRemoteBaseUrl(server.id),
wsUrl: getRemoteWsUrl(server.id),
});
return !!data;
} catch {
return false;
Expand Down
11 changes: 3 additions & 8 deletions packages/secretnote/src/modules/metrics/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IKernelConnection, KernelMessage } from '@difizen/libro-jupyter';
import { kernelStatus, ServerConnection } from '@difizen/libro-jupyter';
import { kernelStatus } from '@difizen/libro-jupyter';
import { getOrigin, inject, prop, singleton } from '@difizen/mana-app';
import { Poll } from '@lumino/polling';

Expand Down Expand Up @@ -43,7 +43,6 @@ export class MetricsService {

protected readonly serverManager: SecretNoteServerManager;
protected readonly kernelManager: SecretNoteKernelManager;
protected readonly serverConnection: ServerConnection;
protected readonly notebookFileService: NotebookFileService;

@prop()
Expand All @@ -55,12 +54,10 @@ export class MetricsService {
constructor(
@inject(SecretNoteServerManager) serverManager: SecretNoteServerManager,
@inject(SecretNoteKernelManager) kernelManager: SecretNoteKernelManager,
@inject(ServerConnection) serverConnection: ServerConnection,
@inject(NotebookFileService) notebookFileService: NotebookFileService,
) {
this.serverManager = serverManager;
this.kernelManager = kernelManager;
this.serverConnection = serverConnection;
this.notebookFileService = notebookFileService;
this.notebookFileService.onNotebookFileChanged(() => {
this.refresh();
Expand Down Expand Up @@ -175,8 +172,7 @@ export class MetricsService {
try {
const url = '/api/metrics/v1';
const init = { method: 'GET' };
const address = this.serverManager.getServerUrl(server).baseUrl;
const data = await request(url, init, address);
const data = await request(url, init, server.id);

return {
cpu: data.cpu_percent,
Expand Down Expand Up @@ -206,8 +202,7 @@ export class MetricsService {
try {
const url = '/api/metrics/v1/kernel_usage/get_usage/' + id;
const init = { method: 'GET' };
const address = this.serverManager.getServerUrl(server).baseUrl;
const data = await request(url, init, address);
const data = await request(url, init, server.id);
const { cpu, memory, pid, cpuText, memoryText } = this.parseKernelStatus(data);
const { color, text_zh } = kernelStatus[status];

Expand Down
28 changes: 21 additions & 7 deletions packages/secretnote/src/modules/notebook/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IContentsModel, LibroView } from '@difizen/libro-jupyter';
import { ContentsManager } from '@difizen/libro-jupyter';
import { Emitter, inject, prop, singleton } from '@difizen/mana-app';

import { downloadFileByUrl } from '@/utils';
import { downloadFileByUrl, getLocalBaseUrl } from '@/utils';

const BASE_PATH = '/';
const FILE_EXT = '.ipynb';
Expand Down Expand Up @@ -45,7 +45,10 @@ export class NotebookFileService {
}

async getFileList() {
const list = await this.contentsManager.get(BASE_PATH);
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: getLocalBaseUrl(),
content: true,
});
const notebookFileList = list.content.filter((file: any) =>
file.name.endsWith(FILE_EXT),
);
Expand All @@ -66,7 +69,9 @@ export class NotebookFileService {
if (isExisted) {
throw new Error('The notebook is already existed.');
}
const newFile = await this.contentsManager.rename(path, newPath);
const newFile = await this.contentsManager.rename(path, newPath, {
baseUrl: getLocalBaseUrl(),
});
await this.getFileList();
if (this.currentNotebookFile?.path === path) {
this.currentNotebookFile = newFile;
Expand All @@ -82,33 +87,41 @@ export class NotebookFileService {
const file = await this.contentsManager.newUntitled({
path: BASE_PATH,
type: 'notebook',
baseUrl: getLocalBaseUrl(),
});
await this.getFileList();
this.openFile(file);
this.renameNotebookFile = this.createRenameNoteBookFile(file);
}

async deleteFile(file: IContentsModel) {
await this.contentsManager.delete(file.path);
await this.contentsManager.delete(file.path, { baseUrl: getLocalBaseUrl() });
await this.getFileList();
if (this.currentNotebookFile?.path === file.path) {
this.currentNotebookFile = null;
}
}

async exportFile(file: IContentsModel) {
const data = await this.contentsManager.getDownloadUrl(file.path);
const data = await this.contentsManager.getDownloadUrl(file.path, {
baseUrl: getLocalBaseUrl(),
});
downloadFileByUrl(data, file.name);
}

async copyFile(file: IContentsModel) {
const newFile = await this.contentsManager.copy(file.path, BASE_PATH);
const newFile = await this.contentsManager.copy(file.path, BASE_PATH, {
baseUrl: getLocalBaseUrl(),
});
await this.getFileList();
this.openFile(newFile);
}

async isFileExisted(path: string) {
const list = await this.contentsManager.get(BASE_PATH);
const list = await this.contentsManager.get(BASE_PATH, {
baseUrl: getLocalBaseUrl(),
content: true,
});
return list.content.some((file: any) => file.path === path);
}

Expand All @@ -118,6 +131,7 @@ export class NotebookFileService {
type: 'notebook',
content: JSON.parse(content),
format: 'json',
baseUrl: getLocalBaseUrl(),
});
await this.getFileList();
}
Expand Down
Loading

0 comments on commit a319b50

Please sign in to comment.