Skip to content

Commit 7ab70e2

Browse files
committed
Optimize: lazy load ai and auth-provider packages to improve initialization speed
- Make ai package import dynamic to avoid loading 2.8MB package during initialization - Make auth-provider import dynamic to avoid loading 812KB package during initialization - Both packages now load only when janitor runs (on session idle events) - Combined with gpt-tokenizer lazy loading, plugin initialization is now ~58.6MB lighter - Improves plugin startup time and reduces memory footprint
1 parent d92712e commit 7ab70e2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

lib/janitor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { generateObject } from "ai"
21
import { z } from "zod"
32
import type { Logger } from "./logger"
43
import type { StateManager } from "./state"
@@ -328,6 +327,9 @@ export class Janitor {
328327
prunedIds: allPrunedSoFar.slice(0, 5) // Show first 5
329328
})
330329

330+
// Lazy import - only load the 2.8MB ai package when actually needed
331+
const { generateObject } = await import('ai')
332+
331333
// Analyze which tool calls are obsolete
332334
const result = await generateObject({
333335
model: modelSelection.model,

lib/model-selector.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* This module handles intelligent model selection for the DCP plugin's analysis tasks.
55
* It attempts to use the same model as the current session, with fallbacks to other
66
* available models when needed.
7+
*
8+
* NOTE: OpencodeAI is lazily imported to avoid loading the 812KB package during
9+
* plugin initialization. The package is only loaded when model selection is needed.
710
*/
811

912
import type { LanguageModel } from 'ai';
10-
import { OpencodeAI } from '@tarquinen/opencode-auth-provider';
1113
import type { Logger } from './logger';
1214

1315
export interface ModelInfo {
@@ -82,6 +84,9 @@ export async function selectModel(
8284
configModel?: string
8385
): Promise<ModelSelectionResult> {
8486
logger?.info('model-selector', 'Model selection started', { currentModel, configModel });
87+
88+
// Lazy import - only load the 812KB auth provider package when actually needed
89+
const { OpencodeAI } = await import('@tarquinen/opencode-auth-provider');
8590
const opencodeAI = new OpencodeAI();
8691

8792
let failedModelInfo: ModelInfo | undefined;

0 commit comments

Comments
 (0)