linshenkx/prompt-optimizer · error · ContextError

CONTEXT_ERROR_CODES.STORAGE_ERROR

CONTEXT_ERROR_CODES.STORAGE_ERROR

Error message

Failed to parse context store: ${details}

What it means

SmartVariableExtractor.extractVariable validates the proposed variable name against its naming rules (isValidVariableName) before doing anything, and throws with the offending name embedded. Variable names must satisfy the extractor's pattern (typically letters/digits/underscore, no spaces or braces) so the {{placeholder}} substitution stays parseable.

Source

Thrown at packages/core/src/services/context/repo.ts:176

          console.log('[ContextRepo] Migrated contexts to add mode field');
        }
      }

      // 确保currentId对应的上下文存在
      if (!doc.contexts[doc.currentId]) {
        // 修复:选择第一个可用的上下文
        const availableIds = Object.keys(doc.contexts);
        if (availableIds.length > 0) {
          doc.currentId = availableIds[0];
        } else {
          throw new ContextError(CONTEXT_ERROR_CODES.INVALID_STORE, 'No contexts available');
        }
      }

      return doc;
    } catch (error) {
      const details = error instanceof Error ? error.message : String(error)
      throw new ContextError(
        CONTEXT_ERROR_CODES.STORAGE_ERROR,
        `Failed to parse context store: ${details}`,
        { details },
      );
    }
  }

  /**
   * 更新存储文档
   */
  private async updateStoreDoc(
    updater: (doc: ContextStoreDoc) => ContextStoreDoc
  ): Promise<ContextStoreDoc> {
    let updatedDoc: ContextStoreDoc;

    await this.storage.updateData<ContextStoreDoc>(
      CONTEXT_STORE_KEY,
      (currentDoc: ContextStoreDoc | null) => {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Sanitize/validate the name in the UI before calling extractVariable (e.g. /^[A-Za-z_][A-Za-z0-9_]*$/)
  2. Trim and reject empty names; auto-slugify names containing spaces
  3. Show inline validation feedback in the variable-name input field

Example fix

// before
const result = extractor.extractVariable('my variable', content, selectedText, start, end)

// after
const safeName = userInput.trim().replace(/\s+/g, '_')
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(safeName)) {
  throw new Error(`Invalid variable name: ${userInput}`)
}
const result = extractor.extractVariable(safeName, content, selectedText, start, end)
Defensive patterns

Strategy: validation

Validate before calling

const VARIABLE_NAME_RE = /^[A-Za-z_][A-Za-z0-9_]*$//
if (!VARIABLE_NAME_RE.test(variableName.trim())) {
  // reject before calling extractVariable
  throw new Error('Please use letters, digits and underscores only')
}

Type guard

function isValidVariableName(name: string): boolean {
  return /^[A-Za-z_][A-Za-z0-9_]*$/.test(name)
}

Try / catch

try {
  const result = extractor.extractVariable(name, content, selectedText, start, end)
} catch (error) {
  if (error instanceof Error && error.message.startsWith('Invalid variable name:')) {
  showInlineError(error.message)
  return
  }
  throw error
}

Prevention

When it happens

Trigger: Calling extractVariable('my var', ...) or extractVariable('{{x}}', ...) or an empty name — any name failing the internal naming-rule check.

Common situations: Passing raw user input from a text field straight into extractVariable; names with spaces, CJK punctuation, hyphens, or braces; empty input from an unvalidated dialog.

Understand the failure class

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/d0cd65e8d3ac81a0. Report an issue: GitHub.