Skip to content

Commit

Permalink
refactor: add libro package
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshinesmilelk committed Feb 5, 2025
1 parent d9b7f3d commit 7f571f2
Show file tree
Hide file tree
Showing 35 changed files with 260 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {
InlineCompletionProvider,
CancellationToken,
EditorCancellationToken,
IIntelligentCompletionsResult,
ICompletionContext,
} from '@difizen/libro-code-editor';
import { singleton } from '@difizen/mana-app';

import { CompletionRequest } from './inline-completion-request.js';
import { AiCompletionRequest } from './inline-completion-request.js';
import { raceCancellation, sleep } from './utils.js';

// 缓存最近一次的补全结果
Expand All @@ -21,11 +21,11 @@ const inlineCompletionCache: {
};

class ReqStack {
queue: CompletionRequest[];
queue: AiCompletionRequest[];
constructor() {
this.queue = [];
}
addReq(reqRequest: CompletionRequest) {
addReq(reqRequest: AiCompletionRequest) {
this.queue.push(reqRequest);
}
runReq() {
Expand Down Expand Up @@ -66,12 +66,12 @@ export class AICompletionProvider implements InlineCompletionProvider {

async provideInlineCompletionItems(
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
) {
this.cancelRequest();

// 放入队列
const requestImp = new CompletionRequest(context, token);
const requestImp = new AiCompletionRequest(context, token);
this.reqStack.addReq(requestImp);

await raceCancellation(sleep(this.inlineComletionsDebounceTime), token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import type {
IAICompletionOption,
ICompletionContext,
IIntelligentCompletionsResult,
CancellationToken,
EditorCancellationToken,
} from '@difizen/libro-code-editor';
import { transient } from '@difizen/mana-app';
import { v4 } from 'uuid';

import { generateInstructionsPrompt } from './Prompt/instruction.js';

@transient()
export class CompletionRequest {
export class AiCompletionRequest {
isCancelFlag: boolean;
id: string;

constructor(
public context: ICompletionContext,
public token: CancellationToken,
public token: EditorCancellationToken,
) {
this.isCancelFlag = false;
this.id = v4();
Expand All @@ -25,7 +25,7 @@ export class CompletionRequest {
// 拼接上下文信息
protected constructRequestContext(
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
): IAICompletionOption {
// const prompt = lineBasedPromptProcessor.processPrefix(context.prefix);
// const suffix = lineBasedPromptProcessor.processSuffix(context.suffix);
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-ai-native/src/ai-inline-completions/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ManaModule } from '@difizen/mana-app';

import { AICompletionContribution } from './inline-completion-contribution.js';
import { AICompletionProvider } from './inline-completion-provider.js';
import { CompletionRequest } from './inline-completion-request.js';
import { AiCompletionRequest } from './inline-completion-request.js';

export const LibroAICompletionModule = ManaModule.create()
.register(CompletionRequest, AICompletionProvider, AICompletionContribution)
.register(AiCompletionRequest, AICompletionProvider, AICompletionContribution)
.dependOn(CodeEditorModule);
8 changes: 4 additions & 4 deletions packages/libro-ai-native/src/ai-inline-completions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CancellationToken } from '@difizen/libro-code-editor';
import type { EditorCancellationToken } from '@difizen/libro-code-editor';

import { lineBasedCompletionModelConfigs } from './contant.js';

Expand Down Expand Up @@ -39,16 +39,16 @@ export function sleep(time: number) {

export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
): Promise<T | undefined>;
export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
defaultValue: T,
): Promise<T>;
export function raceCancellation<T>(
promise: Promise<T>,
token: CancellationToken,
token: EditorCancellationToken,
defaultValue?: T,
): Promise<T | undefined> {
return Promise.race([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface InlineCompletionProvider {
provideInlineCompletionItems: ProvideInlineCompletionsFunction;
}

export interface CancellationToken {
export interface EditorCancellationToken {
/**
* A flag signalling is cancellation has been requested.
*/
Expand Down Expand Up @@ -169,5 +169,5 @@ export interface InlineCompletionImplement {

export type ProvideInlineCompletionsFunction = (
context: ICompletionContext,
token: CancellationToken,
token: EditorCancellationToken,
) => Promise<IIntelligentCompletionsResult | undefined>;
2 changes: 1 addition & 1 deletion packages/libro-codemirror/src/auto-complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### Sources

@CompletionContext
@CodemirrorCompletionContext

@CompletionResult

Expand Down
14 changes: 7 additions & 7 deletions packages/libro-codemirror/src/auto-complete/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface Completion {
}

/// An instance of this is passed to completion source functions.
export class CompletionContext {
export class CodemirrorCompletionContext {
/// @internal
abortListeners: (() => void)[] | null = [];

Expand Down Expand Up @@ -105,7 +105,7 @@ export class CompletionContext {

/// Allows you to register abort handlers, which will be called when
/// the query is
/// [aborted](#autocomplete.CompletionContext.aborted).
/// [aborted](#autocomplete.CodemirrorCompletionContext.aborted).
addEventListener(type: 'abort', listener: () => void) {
if (type === 'abort' && this.abortListeners) {
this.abortListeners.push(listener);
Expand Down Expand Up @@ -144,7 +144,7 @@ export function completeFromList(
const [validFor, match] = options.every((o) => /^\w+$/.test(o.label))
? [/\w*$/, /\w+$/]
: prefixMatch(options);
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
const token = context.matchBefore(match);
return token || context.explicit
? { from: token ? token.from : context.pos, options, validFor }
Expand All @@ -158,7 +158,7 @@ export function ifIn(
nodes: readonly string[],
source: CompletionSource,
): CompletionSource {
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
for (
let pos: SyntaxNode | null = syntaxTree(context.state).resolveInner(
context.pos,
Expand All @@ -181,7 +181,7 @@ export function ifNotIn(
nodes: readonly string[],
source: CompletionSource,
): CompletionSource {
return (context: CompletionContext) => {
return (context: CodemirrorCompletionContext) => {
for (
let pos: SyntaxNode | null = syntaxTree(context.state).resolveInner(
context.pos,
Expand All @@ -203,7 +203,7 @@ export function ifNotIn(
/// synchronously or as a promise. Returning null indicates no
/// completions are available.
export type CompletionSource = (
context: CompletionContext,
context: CodemirrorCompletionContext,
) => CompletionResult | null | Promise<CompletionResult | null>;

/// Interface for objects returned by completion sources.
Expand Down Expand Up @@ -250,7 +250,7 @@ export interface CompletionResult {
current: CompletionResult,
from: number,
to: number,
context: CompletionContext,
context: CodemirrorCompletionContext,
) => CompletionResult | null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/auto-complete/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
cur,
asSource,
ensureAnchor,
CompletionContext,
CodemirrorCompletionContext,
} from './completion.js';
import type { CompletionConfig } from './config.js';
import { completionConfig } from './config.js';
Expand Down Expand Up @@ -389,7 +389,7 @@ export class ActiveResult extends ActiveSource {
this.result,
from,
to,
new CompletionContext(tr.state, pos, explicitPos >= 0),
new CodemirrorCompletionContext(tr.state, pos, explicitPos >= 0),
))
) {
return new ActiveResult(
Expand Down
10 changes: 7 additions & 3 deletions packages/libro-codemirror/src/auto-complete/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import { ViewPlugin, logException, getTooltip } from '@codemirror/view';

import type { CompletionResult } from './completion.js';
import { cur, CompletionContext, applyCompletion } from './completion.js';
import { cur, CodemirrorCompletionContext, applyCompletion } from './completion.js';
import { completionConfig } from './config.js';
import {
completionState,
Expand Down Expand Up @@ -113,7 +113,7 @@ class RunningQuery {

constructor(
readonly active: ActiveSource,
readonly context: CompletionContext,
readonly context: CodemirrorCompletionContext,
) {}
}

Expand Down Expand Up @@ -216,7 +216,11 @@ export const completionPlugin = ViewPlugin.fromClass(
startQuery(active: ActiveSource) {
const { state } = this.view,
pos = cur(state);
const context = new CompletionContext(state, pos, active.explicitPos === pos);
const context = new CodemirrorCompletionContext(
state,
pos,
active.explicitPos === pos,
);
const pending = new RunningQuery(active, context);
this.running.push(pending);
Promise.resolve(active.source(context))
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { FoldIcon, UnFoldIcon } from './libro-icon.js';
import { lspPythonCompletion } from './lsp/completion.js';
import { formatKeymap } from './lsp/format.js';
import { lspLint, lspTooltip } from './lsp/index.js';
import { ensure } from './mode.js';
import { codemirrorEnsure } from './mode.js';
import { getTheme, defaultTheme } from './theme.js';
import { tabTooltip, tooltipKeymap } from './tooltip.js';

Expand Down Expand Up @@ -632,7 +632,7 @@ export class EditorConfiguration {
const themeOverload = this.updateThemeOverload(config);
extensions.push(this._themeOverloader.of(themeOverload), insertExt, keymapExt);

ensure(config?.mimetype ?? 'text/x-python')
codemirrorEnsure(config?.mimetype ?? 'text/x-python')
.then((spec) => {
if (spec) {
extensions.push(this.get('language')?.of(spec.support!));
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { v4 } from 'uuid';
import type { CodeMirrorConfig } from './config.js';
import { EditorConfiguration } from './config.js';
import { stateFactory } from './factory.js';
import { ensure } from './mode.js';
import { codemirrorEnsure } from './mode.js';
import { monitorPlugin } from './monitor.js';

/**
Expand Down Expand Up @@ -922,7 +922,7 @@ export class CodeMirrorEditor implements IEditor {
protected _onMimeTypeChanged(): void {
const mime = this._model.mimeType;
// TODO: should we provide a hook for when the mode is done being set?
void ensure(mime).then((spec) => {
void codemirrorEnsure(mime).then((spec) => {
if (spec) {
this._editorConfig.reconfigureExtension(
this._editor,
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-codemirror/src/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ export function findBest(mode: string | ISpec, fallback = true): ISpec | null {
/**
* Ensure a codemirror mode is available by name or Codemirror spec.
*
* @param mode - The mode to ensure. If it is a string, uses [findBest]
* @param mode - The mode to codemirrorEnsure. If it is a string, uses [findBest]
* to get the appropriate spec.
*
* @returns A promise that resolves when the mode is available.
*/
export async function ensure(mode: string | ISpec): Promise<ISpec | null> {
export async function codemirrorEnsure(mode: string | ISpec): Promise<ISpec | null> {
const spec = findBest(mode);
if (spec) {
spec.support = await spec.load!();
Expand Down
2 changes: 1 addition & 1 deletion packages/libro-jupyter/src/components/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const WarningIcon: React.FC = () => (
</svg>
);

export const Location: React.FC = () => (
export const LocationIcon: React.FC = () => (
<svg
width="17px"
height="20px"
Expand Down
15 changes: 0 additions & 15 deletions packages/libro-jupyter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
export * from '@difizen/libro-code-cell';
export * from '@difizen/libro-code-editor';
export * from '@difizen/libro-common';
export * from '@difizen/libro-core';
export * from '@difizen/libro-cofine-editor';
export * from '@difizen/libro-kernel';
export * from '@difizen/libro-l10n';
export * from '@difizen/libro-lsp';
export * from '@difizen/libro-markdown-cell';
export * from '@difizen/libro-output';
export * from '@difizen/libro-raw-cell';
export * from '@difizen/libro-codemirror';
export * from '@difizen/libro-rendermime';
export * from '@difizen/libro-search';
export * from '@difizen/libro-search-code-cell';
export * from './add-between-cell/index.js';
export * from './cell/index.js';
export * from './command/index.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-jupyter/src/toolbar/toolbar-contribution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ToolbarRegistry } from '@difizen/mana-app';
import { singleton, inject, ToolbarContribution } from '@difizen/mana-app';
import { l10n } from '@difizen/mana-l10n';

import { Location } from '../components/icons.js';
import { LocationIcon } from '../components/icons.js';
import { KernelStatusAndSelectorProvider } from '../libro-jupyter-protocol.js';

import { RunSelector } from './run-selector.js';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class LibroJupyterToolbarContribution implements ToolbarContribution {

registry.registerItem({
id: NotebookCommands['SelectLastRunCell'].id,
icon: Location,
icon: LocationIcon,
command: NotebookCommands['SelectLastRunCell'].id,
group: ['group2'],
order: 'b',
Expand Down
8 changes: 4 additions & 4 deletions packages/libro-kernel/src/server/connection-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A wrapped error for a fetch response.
*/
export class ResponseError extends Error {
export class JupyterResponseError extends Error {
/**
* The response associated with the error.
*/
Expand Down Expand Up @@ -44,15 +44,15 @@ export class NetworkError extends TypeError {
*
* @param response The response object.
*
* @returns A promise that resolves with a `ResponseError` object.
* @returns A promise that resolves with a `JupyterResponseError` object.
*/
export const createResponseError = async (response: Response) => {
try {
const data = await response.json();
if (data.message) {
return new ResponseError(response, data.message);
return new JupyterResponseError(response, data.message);
}
return new ResponseError(response);
return new JupyterJupyterResponseError(response);
} catch (e) {
console.warn(e);
return new ResponseError(response);
Expand Down
1 change: 0 additions & 1 deletion packages/libro-lab/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from '@difizen/libro-jupyter';
export * from './module.js';
export * from './lab-app.js';
export * from './github-link/index.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/libro-lsp/src/adapters/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Disposable, Event, View } from '@difizen/mana-app';
import type {} from '@difizen/mana-app';
import mergeWith from 'lodash.mergewith';

import type { ClientCapabilities, LanguageIdentifier } from '../lsp.js';
import type { LspClientCapabilities, LanguageIdentifier } from '../lsp.js';
import type { IVirtualPosition } from '../positioning.js';
import type {
Document,
Expand Down Expand Up @@ -544,7 +544,7 @@ export abstract class WidgetLSPAdapter<T extends NotebookView> implements Dispos
protected async _connect(virtualDocument: VirtualDocument) {
const language = virtualDocument.language;

let capabilities: ClientCapabilities = {
let capabilities: LspClientCapabilities = {
textDocument: {
synchronization: {
dynamicRegistration: true,
Expand Down
Loading

0 comments on commit 7f571f2

Please sign in to comment.