Skip to content

Commit

Permalink
feat(plugin): add leightweight context mode
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminshafii committed Jan 1, 2025
1 parent 8967c90 commit 24c50eb
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 359 deletions.
40 changes: 37 additions & 3 deletions packages/plugin/views/assistant/ai-chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const ChatComponent: React.FC<ChatComponentProps> = ({
currentFile,
screenpipe,
textSelections,
isLightweightMode,
} = useContextItems();

const uniqueReferences = getUniqueReferences();
Expand All @@ -72,9 +73,42 @@ export const ChatComponent: React.FC<ChatComponentProps> = ({
});

const contextString = React.useMemo(() => {
const contextString = JSON.stringify(contextItems);
return contextString;
}, [contextItems]);
if (isLightweightMode) {
// In lightweight mode, only include metadata
const lightweightContext = {
files: Object.fromEntries(
Object.entries(files).map(([id, file]) => [
id,
{ ...file, content: '' }
])
),
folders: Object.fromEntries(
Object.entries(folders).map(([id, folder]) => [
id,
{ ...folder, files: folder.files.map(f => ({ ...f, content: '' })) }
])
),
tags: Object.fromEntries(
Object.entries(tags).map(([id, tag]) => [
id,
{ ...tag, files: tag.files.map(f => ({ ...f, content: '' })) }
])
),
searchResults: Object.fromEntries(
Object.entries(searchResults).map(([id, search]) => [
id,
{ ...search, results: search.results.map(r => ({ ...r, content: '' })) }
])
),
// Keep these as is
currentFile: currentFile ? { ...currentFile, content: '' } : null,
screenpipe,
textSelections,
};
return JSON.stringify(lightweightContext);
}
return JSON.stringify(contextItems);
}, [contextItems, isLightweightMode]);
logger.debug("contextString", contextString);

const chatBody = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ interface Example {
}

const examples: Example[] = [
// {
// prompt: "Move all my untitled notes to an 'Inbox' folder",
// description: "File organization",
// icon: "📁"
// },
// {
// prompt: "Rename my daily notes to include topics discussed",
// description: "Smart renaming",
// icon: "✏️"
// },
{
prompt: "Move all my untitled notes to an 'Inbox' folder",
description: "File organization",
icon: "📁"
},
{
prompt: "Rename my daily notes to include topics discussed",
description: "Smart renaming",
icon: "✏️"
},
{
prompt: "Add a summary section to my meeting notes",
prompt: "Add a summary section at the bottom of my current note",
description: "Content editing",
icon: "➕"
},
Expand All @@ -27,16 +27,16 @@ const examples: Example[] = [
description: "Smart search",
icon: "🔍"
},
{
prompt: "Tag my book notes with relevant categories",
description: "Auto-tagging",
icon: "🏷️"
},
{
prompt: "Analyze my vault structure and suggest improvements",
description: "Vault analysis",
icon: "📊"
},
// {
// prompt: "Tag my book notes with relevant categories",
// description: "Auto-tagging",
// icon: "🏷️"
// },
// {
// prompt: "Analyze my vault structure and suggest improvements",
// description: "Vault analysis",
// icon: "📊"
// },
{
prompt: "Get a summary of my day from Screenpipe",
description: "Daily summary",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { init, get_encoding } from "tiktoken/init";
import wasmBinary from "tiktoken/tiktoken_bg.wasm";
import { useDebouncedCallback } from 'use-debounce';
import { logger } from "../../../services/logger";
import { useContextItems } from "./use-context-items";

interface TokenStats {
contextSize: number;
Expand All @@ -19,10 +20,10 @@ export function ContextLimitIndicator({
const [stats, setStats] = React.useState<TokenStats>({ contextSize: 0, percentUsed: 0 });
const [error, setError] = React.useState<string>();
const [tiktokenInitialized, setTiktokenInitialized] = React.useState(false);
const { isLightweightMode, toggleLightweightMode } = useContextItems();

// Initialize encoder once on mount
React.useEffect(() => {

async function setup() {
try {
if (!tiktokenInitialized) {
Expand All @@ -39,7 +40,6 @@ export function ContextLimitIndicator({

// Debounced token calculation
const calculateTokens = useDebouncedCallback((text: string) => {
console.log("calculateTokens", { text, tiktokenInitialized });
if (!text || !tiktokenInitialized) return;
const encoder = get_encoding("cl100k_base");

Expand Down Expand Up @@ -67,14 +67,39 @@ export function ContextLimitIndicator({
}

const isOverLimit = stats.contextSize > maxContextSize;
const shouldWarn = stats.percentUsed > 80;

return (
<div className={`mt-2 p-2 rounded text-xs flex gap-1 items-center justify-between
${isOverLimit ? "border border-[--text-error] text-[--text-error]" : "bg-[--background-modifier-border] text-[--text-muted]"}`}>
<span>
{isOverLimit ? "Context size exceeds maximum" : "Context used"}
</span>
<span className="font-mono">{stats.percentUsed.toFixed(0)}%</span>
<div className="mt-2 space-y-2">
<div className={`p-2 rounded text-xs flex gap-1 items-center justify-between
${isOverLimit
? "border border-[--text-error] text-[--text-error]"
: shouldWarn
? "border border-[--text-warning] text-[--text-warning]"
: "bg-[--background-modifier-border] text-[--text-muted]"}`}>
<span>
{isOverLimit
? "Context size exceeds maximum"
: shouldWarn
? "Context size nearing limit"
: "Context used"}
</span>
<span className="font-mono">{stats.percentUsed.toFixed(0)}%</span>
</div>

<button
onClick={toggleLightweightMode}
title="Enable when processing multiple files to remove content from context. Useful for batch operations like moving, renaming, or tagging files."
className={`w-full p-2 rounded text-xs flex items-center justify-between group relative
${isLightweightMode
? "bg-[--interactive-accent] text-[--text-on-accent]"
: "bg-[--background-modifier-border] text-[--text-muted] hover:bg-[--background-modifier-border-hover]"}`}
>
<span>Disable Context</span>
<div className="absolute bottom-full left-0 w-full p-2 bg-[--background-secondary] rounded shadow-lg text-[--text-normal] opacity-0 group-hover:opacity-100 transition-opacity mb-2 text-left pointer-events-none">
Use this when processing multiple files to reduce context size. Removes file content from context while preserving metadata for batch operations.
</div>
</button>
</div>
);
}
Loading

0 comments on commit 24c50eb

Please sign in to comment.