Skip to content

Commit

Permalink
Merge pull request #69 from justyns/template-options
Browse files Browse the repository at this point in the history
Add more insertAt options
  • Loading branch information
justyns authored Sep 11, 2024
2 parents 59adc08 + 1672c11 commit a9e8043
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 42 deletions.
14 changes: 13 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ This page is a brief overview of each version.
- **removeDuplicateStart** - Remove the first line from the response if it matches the line before the response started.
- **convertToBulletList** - Convert response to a markdown list.
- **convertToTaskList** - Convert response to a markdown list of tasks.
- Add new insertAt options for [[Templated Prompts]]:
- **replace-selection**: Replaces the currently selected text with the generated content. If no text is selected, it behaves like the 'cursor' option.
- **replace-paragraph**: Replaces the entire paragraph (or item) where the cursor is located with the generated content.
- **start-of-line**: Inserts at the start of the current line.
- **end-of-line**: Inserts at the end of the current line.
- **start-of-item**: Inserts at the start of the current item (list item or task).
- **end-of-item**: Inserts at the end of the current item.
- **new-line-above**: Inserts on a new line above the current line.
- **new-line-below**: Inserts on a new line below the current line.
- **replace-line**: Replaces the current line with the generated content.
- **replace-smart**: Intelligently replaces content based on context (selected text, current item, or current paragraph).
- AICore Library: Add `aiSplitTodo` slash command and [[^Library/AICore/AIPrompt/AI Split Task]] templated prompt to split a task into smaller subtasks.
- AICore Library: Add template prompts for rewriting text, mostly as a demo for the `replace-smart` insertAt option.

---
## 0.3.2
Expand Down Expand Up @@ -115,4 +127,4 @@ This page is a brief overview of each version.
- Save dall-e generated image locally
- Add script to update readme automatically
- Save and display the revised prompt from dall-e-3
---
---
24 changes: 24 additions & 0 deletions docs/Library/AICore/AIPrompt/AI Rewrite - Grammar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
tags:
- template
- aiPrompt
- meta

description: "Rewrite current paragraph or selected text to fix and improve grammar."
aiprompt:
description: "Rewrite current paragraph or selected text to fix and improve grammar."
chat: true
enrichMessages: true
insertAt: replace-smart
---

**user**: [enrich:false] I’ll provide the note contents, and instructions.
**assistant**: What is the note title?
**user**: [enrich:true] {{@page.name}}
**assistant**: What are the note contents? I will only use this as context.
**user**: [enrich:true]
{{@currentPageText}}
**assistant**: What text should be rewritten?
**user**: [enrich:true] {{@smartReplaceText}}
**assistant**: What are the instructions?
**user**: [enrich:false] Rewrite the provided text. Keep the meaning and facts the same. Return ONLY the rewritten text, no other information or preamble. Correct and improve the grammar. Keep as much of the original text as possible. Preserve existing formatting such as indenting and bullets.
24 changes: 24 additions & 0 deletions docs/Library/AICore/AIPrompt/AI Rewrite - Professional.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
tags:
- template
- aiPrompt
- meta

description: "Rewrite current paragraph or selected text to be more professional"
aiprompt:
description: "Rewrite current paragraph or selected text to be more professional"
chat: true
enrichMessages: true
insertAt: replace-smart
---

**user**: [enrich:false] I’ll provide the note contents, and instructions.
**assistant**: What is the note title?
**user**: [enrich:true] {{@page.name}}
**assistant**: What are the note contents? I will only use this as context.
**user**: [enrich:true]
{{@currentPageText}}
**assistant**: What text should be rewritten?
**user**: [enrich:true] {{@smartReplaceText}}
**assistant**: What are the instructions?
**user**: [enrich:false] Rewrite the provided text. Keep the meaning and facts the same. Remove extra or redundant words. Stay concise. Maintain a professional and neutral tone. Return ONLY the rewritten text, no other information or preamble. Preserve existing formatting such as indenting and bullets.
32 changes: 32 additions & 0 deletions docs/Templated Prompts.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,41 @@ As of version 0.4.0, the following global metadata is available for use inside o
* **`currentPageText`**: Entire text of the current page.
* **`parentItemBounds`**: Start and end positions of the parent item.
* **`parentItemText`**: Full text of the parent item. A parent item may contain child items.
* **`selectedText`**: Text the user has currently selected.
* **`currentParagraph`**: Text of the current paragraph where the cursor is located.
* **`smartReplaceType`**: Indicates the type of content being replaced when using the 'replace-smart' option. Can be 'selected-text', 'current-item', or 'current-paragraph'.
* **`smartReplaceText`**: The text that will be replaced when using the 'replace-smart' option.


All of these can be accessed by prefixing the variable name with `@`, like `@lineEndPos` or `@currentLineNumber`.

## Insert At Options

The `insertAt` option in the `aiprompt` frontmatter determines where the generated content will be inserted. The valid options are:

* **`cursor`**: Inserts at the current cursor position
* **`page-start`**: Inserts at the beginning of the page
* **`page-end`**: Inserts at the end of the page
* **`start-of-line`**: Inserts at the start of the current line
* **`end-of-line`**: Inserts at the end of the current line
* **`start-of-item`**: Inserts at the start of the current item (list item or task)
* **`end-of-item`**: Inserts at the end of the current item
* **`new-line-above`**: Inserts on a new line above the current line
* **`new-line-below`**: Inserts on a new line below the current line
* **`replace-line`**: Replaces the current line with the generated content
* **`replace-paragraph`**: Replaces the entire paragraph (or item) where the cursor is located with the generated content
* **`replace-selection`**: Replaces the currently selected text with the generated content. If no text is selected, it behaves like the 'cursor' option
* **`replace-smart`**: Intelligently replaces content based on context:
- If text is selected, it replaces the selection.
- If no text is selected but the cursor is within a list item or task, it replaces the entire item.
- If neither of the above applies, it replaces the current paragraph.

### Replacing content

If the objective is to replace all or a portion of the note's content, the `replace-smart` option is the best choice. It intelligently selects the most appropriate text to replace based on the cursor's context. If more control is needed, any of the other options can be used.

**Note** that the replace options will remove existing content before inserting the new content. Make sure there is a backup of any important content before using these options.

## Chat-style prompts

As of version 0.3.0, `aiprompt.chat` can be set to true in the template frontmatter to treat the template similar to a page using [[Commands/AI: Chat on current page]].
Expand Down
33 changes: 29 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
# SilverBullet AI Plug

_SilverBullet AI_ is a plug that integrates various LLMs (Large Language Models) into [_SilverBullet_](https://silverbullet.md/), the markdown-based note taking tool, allowing users to perform various AI-related tasks directly within their notes. It requires SilverBullet to work, and also access to either a self-hosted or SaaS LLM such as Ollama, OpenAI (ChatGPT), Google Gemini, etc.
_SilverBullet AI_ is a plug that integrates various LLMs (Large Language Models) into [_SilverBullet_](https://silverbullet.md/), the markdown-based note-taking tool. This integration allows users to perform a wide range of AI-related tasks directly within their notes.

silverbullet-ai is very new and is still in early development. It may not work as expected. Please report any issues you encounter, or feature ideas. You can do so through [Github Issues](https://github.com/justyns/silverbullet-ai/issues).
## What are LLMs?

If you are new here, start with [[Quick Start]], and then [[AI Core Library]]!
LLMs, or Large Language Models, are advanced AI systems trained on vast amounts of text data. They can understand and generate human-like text, making them powerful tools for various language-related tasks.

> **warning** **Warning**: Please backup your notes before using this plug. It inserts and replaces text at certain points. It is recommended to use the git plug to keep your notes versioned automatically in a git repo.
## Requirements

- SilverBullet installation
- Access to either a self-hosted or SaaS LLM such as:
- Ollama
- OpenAI (ChatGPT)
- Google Gemini
- Others (see [[Providers]])

## Development Status

SilverBullet AI is currently in early development. It may not work as expected. We encourage users to:

- Report any issues encountered
- Share feature ideas

You can do so through our [Github Issues](https://github.com/justyns/silverbullet-ai/issues) page, or the SilverBullet [community discourse](https://community.silverbullet.md/).

## Getting Started

If you're new here, we recommend starting with:

1. [[Quick Start]]
2. [[AI Core Library]]

> **Warning**: Please back up your notes before using this plug. It inserts and replaces text at certain points. We recommend using the git plug to keep your notes versioned automatically in a git repository.
## Features

Expand Down
49 changes: 49 additions & 0 deletions src/editorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,52 @@ export function getLineAfter(text: string, pos: number): string {
const lineNumber = getLineNumberAtPos(text, pos);
return getLine(text, lineNumber + 1);
}

/**
* Get the paragraph containing the cursor position. A paragraph is defined as a block of text
* that starts with an empty line or a line with only whitespace, and ends with an empty line or a line with only whitespace.
* @param text The text of the page.
* @param pos The cursor position.
* @returns The start and end positions of the paragraph and the paragraph text.
*/
export function getParagraph(text: string, pos: number): {
from: number;
to: number;
text: string;
} {
const lines = text.split("\n");
let currentPos = 0;
let start = 0;
let end = text.length;

for (let i = 0; i < lines.length; i++) {
const lineLength = lines[i].length + 1;

if (currentPos <= pos && pos < currentPos + lineLength) {
// Look backwards for the start of the paragraph
console.log("Looking backwards for the start of the paragraph");
for (let j = i; j >= 0; j--) {
if (j === 0 || lines[j - 1].trim() === "") {
start = j === 0 ? 0 : lines.slice(0, j).join("\n").length + 1;
break;
}
}
// Look forwards for the end of the paragraph
console.log("Looking forwards for the end of the paragraph");
for (let k = i; k < lines.length; k++) {
if (k === lines.length - 1 || lines[k + 1].trim() === "") {
end = lines.slice(0, k + 1).join("\n").length;
break;
}
}
break;
}
currentPos += lineLength;
}
console.log("Found paragraph", text.slice(start, end));
return {
from: start,
to: end,
text: text.slice(start, end),
};
}
Loading

0 comments on commit a9e8043

Please sign in to comment.