mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-05-16 04:31:38 +00:00
update , add mcp server stdio and sse
This commit is contained in:
@@ -2,93 +2,95 @@ import { Notice } from "obsidian";
|
||||
|
||||
import EventListener from "../../../event-listener";
|
||||
import { DocumentChanges } from "../../../render-plugin/document-changes-listener";
|
||||
import { onEnt } from "../../../utils/web-search";
|
||||
import Context from "../context-detection";
|
||||
|
||||
import State from "./state";
|
||||
|
||||
class PredictingState extends State {
|
||||
private predictionPromise: Promise<void> | null = null;
|
||||
private isStillNeeded = true;
|
||||
private readonly prefix: string;
|
||||
private readonly suffix: string;
|
||||
private predictionPromise: Promise<void> | null = null;
|
||||
private isStillNeeded = true;
|
||||
private readonly prefix: string;
|
||||
private readonly suffix: string;
|
||||
|
||||
constructor(context: EventListener, prefix: string, suffix: string) {
|
||||
super(context);
|
||||
this.prefix = prefix;
|
||||
this.suffix = suffix;
|
||||
}
|
||||
constructor(context: EventListener, prefix: string, suffix: string) {
|
||||
super(context);
|
||||
this.prefix = prefix;
|
||||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
static createAndStartPredicting(
|
||||
context: EventListener,
|
||||
prefix: string,
|
||||
suffix: string
|
||||
): PredictingState {
|
||||
const predictingState = new PredictingState(context, prefix, suffix);
|
||||
predictingState.startPredicting();
|
||||
context.setContext(Context.getContext(prefix, suffix));
|
||||
return predictingState;
|
||||
}
|
||||
static createAndStartPredicting(
|
||||
context: EventListener,
|
||||
prefix: string,
|
||||
suffix: string
|
||||
): PredictingState {
|
||||
const predictingState = new PredictingState(context, prefix, suffix);
|
||||
predictingState.startPredicting();
|
||||
context.setContext(Context.getContext(prefix, suffix));
|
||||
return predictingState;
|
||||
}
|
||||
|
||||
handleCancelKeyPressed(): boolean {
|
||||
this.cancelPrediction();
|
||||
return true;
|
||||
}
|
||||
handleCancelKeyPressed(): boolean {
|
||||
this.cancelPrediction();
|
||||
return true;
|
||||
}
|
||||
|
||||
async handleDocumentChange(
|
||||
documentChanges: DocumentChanges
|
||||
): Promise<void> {
|
||||
if (
|
||||
documentChanges.hasCursorMoved() ||
|
||||
documentChanges.hasUserTyped() ||
|
||||
documentChanges.hasUserDeleted() ||
|
||||
documentChanges.isTextAdded()
|
||||
) {
|
||||
this.cancelPrediction();
|
||||
}
|
||||
}
|
||||
async handleDocumentChange(
|
||||
documentChanges: DocumentChanges
|
||||
): Promise<void> {
|
||||
if (
|
||||
documentChanges.hasCursorMoved() ||
|
||||
documentChanges.hasUserTyped() ||
|
||||
documentChanges.hasUserDeleted() ||
|
||||
documentChanges.isTextAdded()
|
||||
) {
|
||||
this.cancelPrediction();
|
||||
}
|
||||
}
|
||||
|
||||
private cancelPrediction(): void {
|
||||
this.isStillNeeded = false;
|
||||
this.context.transitionToIdleState();
|
||||
}
|
||||
private cancelPrediction(): void {
|
||||
this.isStillNeeded = false;
|
||||
this.context.transitionToIdleState();
|
||||
}
|
||||
|
||||
startPredicting(): void {
|
||||
this.predictionPromise = this.predict();
|
||||
}
|
||||
startPredicting(): void {
|
||||
this.predictionPromise = this.predict();
|
||||
}
|
||||
|
||||
private async predict(): Promise<void> {
|
||||
private async predict(): Promise<void> {
|
||||
onEnt(`predict`)
|
||||
|
||||
const result =
|
||||
await this.context.autocomplete?.fetchPredictions(
|
||||
this.prefix,
|
||||
this.suffix
|
||||
);
|
||||
const result =
|
||||
await this.context.autocomplete?.fetchPredictions(
|
||||
this.prefix,
|
||||
this.suffix
|
||||
);
|
||||
|
||||
if (!this.isStillNeeded) {
|
||||
return;
|
||||
}
|
||||
if (!this.isStillNeeded) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.isErr()) {
|
||||
new Notice(
|
||||
`Copilot: Something went wrong cannot make a prediction. Full error is available in the dev console. Please check your settings. `
|
||||
);
|
||||
console.error(result.error);
|
||||
this.context.transitionToIdleState();
|
||||
}
|
||||
if (result.isErr()) {
|
||||
new Notice(
|
||||
`Copilot: Something went wrong cannot make a prediction. Full error is available in the dev console. Please check your settings. `
|
||||
);
|
||||
console.error(result.error);
|
||||
this.context.transitionToIdleState();
|
||||
}
|
||||
|
||||
const prediction = result.unwrapOr("");
|
||||
const prediction = result.unwrapOr("");
|
||||
|
||||
if (prediction === "") {
|
||||
this.context.transitionToIdleState();
|
||||
return;
|
||||
}
|
||||
this.context.transitionToSuggestingState(prediction, this.prefix, this.suffix);
|
||||
}
|
||||
if (prediction === "") {
|
||||
this.context.transitionToIdleState();
|
||||
return;
|
||||
}
|
||||
this.context.transitionToSuggestingState(prediction, this.prefix, this.suffix);
|
||||
}
|
||||
|
||||
|
||||
getStatusBarText(): string {
|
||||
return `Predicting for ${this.context.context}`;
|
||||
}
|
||||
getStatusBarText(): string {
|
||||
return `Predicting for ${this.context.context}`;
|
||||
}
|
||||
}
|
||||
|
||||
export default PredictingState;
|
||||
|
||||
Reference in New Issue
Block a user