Skip to content

Commit

Permalink
Add feature to ignore files by name and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
taichimaeda committed Apr 18, 2024
1 parent 1d50a28 commit 420ac0a
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"chart.js": "^4.4.2",
"js-tiktoken": "^1.0.10",
"lucide-react": "^0.363.0",
"minimatch": "^9.0.4",
"openai": "^4.30.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class BaseAPIClient implements APIClient {
async fetchCompletions(language: string, prefix: string, suffix: string) {
const { settings } = this.plugin;

const instance = this.getInstance(settings.chat.provider);
const instance = this.getInstance(settings.completions.provider);
if (instance === undefined) {
return;
}
Expand Down
10 changes: 6 additions & 4 deletions src/api/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ export const OPENAI_MODELS = [
'gpt-3.5-turbo-16k-0613',
] as const;

// TODO
export const OPENROUTER_MODELS = ['gpt-4'];
// TODO:
// This is a placeholder.
export const OPENROUTER_MODELS = ['gpt-4'] as const;

// TODO
export const OLLAMA_MODELS = ['gpt-4'];
// TODO:
// This is a placeholder.
export const OLLAMA_MODELS = ['gpt-4'] as const;

export const MODELS = {
openai: OPENAI_MODELS,
Expand Down
20 changes: 14 additions & 6 deletions src/api/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,41 @@ const OPENAI_MODEL_OUTPUT_COSTS: Record<OpenAIModel, number> = {
'gpt-3.5-turbo-16k-0613': 1.5,
};

// TODO
// TODO:
// This is a placeholder.
const OPENROUTER_INPUT_COSTS: Record<OpenRouterModel, number> = {
'gpt-4': 30,
};

// TODO
// TODO:
// This is a placeholder.
const OPENROUTER_OUTPUT_COSTS: Record<OpenRouterModel, number> = {
'gpt-4': 60,
};

// TODO
// TODO:
// This is a placeholder.
const OLLAMA_INPUT_COSTS: Record<OllamaModel, number> = {
'gpt-4': 0,
};

// TODO
// TODO:
// This is a placeholder.
const OLLAMA_OUTPUT_COSTS: Record<OllamaModel, number> = {
'gpt-4': 0,
};

const INPUT_COSTS: Record<Provider, Record<Model, number>> = {
// TODO:
// Replace `Record<string, number>` to an appropriate type.
const INPUT_COSTS: Record<Provider, Record<string, number>> = {
openai: OPENAI_MODEL_INPUT_COSTS,
openrouter: OPENROUTER_INPUT_COSTS,
ollama: OLLAMA_INPUT_COSTS,
};

const OUTPUT_COSTS: Record<Provider, Record<Model, number>> = {
// TODO:
// Replace `Record<string, number>` to an appropriate type.
const OUTPUT_COSTS: Record<Provider, Record<string, number>> = {
openai: OPENAI_MODEL_OUTPUT_COSTS,
openrouter: OPENROUTER_OUTPUT_COSTS,
ollama: OLLAMA_OUTPUT_COSTS,
Expand Down
23 changes: 20 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Extension } from '@codemirror/state';
import { addIcon, Notice, Plugin, setIcon } from 'obsidian';
import { minimatch } from 'minimatch';
import { addIcon, MarkdownView, Notice, Plugin, setIcon } from 'obsidian';
import { MemoryCacheProxy } from './api/cache';
import { APIClient, BaseAPIClient } from './api/client';
import { UsageMonitorProxy, UsageTracker } from './api/usage';
Expand Down Expand Up @@ -191,9 +192,25 @@ export default class Markpilot extends Plugin {

getEditorExtension() {
return inlineCompletionsExtension(async (...args) => {
if (this.settings.completions.enabled) {
return this.client.fetchCompletions(...args);
// TODO:
// Extract this logic to somewhere appropriate.
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
const file = view?.file;
const content = view?.editor.getValue();
const isIgnoredFile = this.settings.completions.ignoredFiles.some(
(pattern) => file?.path && minimatch(file?.path, pattern),
);
const hasIgnoredTags = this.settings.completions.ignoredTags.some((tag) =>
content?.includes(tag),
);
if (
isIgnoredFile ||
hasIgnoredTags ||
!this.settings.completions.enabled
) {
return;
}
return this.client.fetchCompletions(...args);
}, this);
}

Expand Down
Loading

0 comments on commit 420ac0a

Please sign in to comment.