@mastra/core
Version:
175 lines • 8.09 kB
TypeScript
/**
* WorkspaceSkills - Skills implementation.
*
* Provides discovery and search operations for skills stored
* in skills paths. All operations are async.
*/
import type { IndexDocument, SearchResult } from '../search/index.js';
import type { SkillSource as SkillSourceInterface } from './skill-source.js';
import type { Skill, SkillMetadata, SkillSearchResult, SkillSearchOptions, WorkspaceSkills, SkillsResolver, SkillsContext } from './types.js';
/**
* Minimal search engine interface - only the methods we actually use.
* This allows both the real SearchEngine and test mocks to be used.
*/
interface SkillSearchEngine {
index(doc: IndexDocument): Promise<void>;
remove?(id: string): Promise<void>;
search(query: string, options?: {
topK?: number;
minScore?: number;
mode?: 'bm25' | 'vector' | 'hybrid';
}): Promise<SearchResult[]>;
clear(): void;
}
interface RemovableSkillSearchEngine extends SkillSearchEngine {
remove(id: string): Promise<void>;
}
interface SharedSearchState {
documentIds: Set<string>;
}
/** @internal Prefix of every search document ID owned by a request-scoped skills view. */
export declare const SKILL_SCOPE_DOCUMENT_PREFIX = "skill-scope:";
/**
* Configuration for WorkspaceSkillsImpl
*/
export interface WorkspaceSkillsImplConfig {
/**
* Source for loading skills.
*/
source: SkillSourceInterface;
/**
* Paths to scan for skills.
* Can be a static array or a function that returns paths based on context.
*/
skills: SkillsResolver;
/** Search engine for skill search (optional) */
searchEngine?: SkillSearchEngine;
/** Validate skills on load (default: true) */
validateOnLoad?: boolean;
/** Assert that the owning workspace can still serve skill operations. */
assertAvailable?: () => void;
/**
* Check SKILL.md file mtime in addition to directory mtime for staleness detection.
* Enables detection of in-place file edits (e.g., fixing validation errors).
* Increases stat calls - recommended for local development only.
* Default: false
*/
checkSkillFileMtime?: boolean;
/** @internal Namespace used to isolate a dynamic resolver's search documents. */
searchNamespace?: string;
/** @internal Search document registry shared by request-scoped views. */
sharedSearchState?: SharedSearchState;
/**
* @internal Called before this view re-indexes after `releaseSearchIndex()`,
* so the owner can re-admit it into its bounded cache first.
*/
onIndexReadmit?: () => Promise<void>;
/**
* @internal Called when this view's last in-flight index-dependent operation
* finishes, so the owner can evict it if the cache is over its bound.
*/
onIndexIdle?: () => Promise<void>;
}
/**
* Implementation of WorkspaceSkills interface.
*/
export declare class WorkspaceSkillsImpl implements WorkspaceSkills {
#private;
static readonly GLOB_RESOLVE_INTERVAL = 5000;
static readonly STALENESS_CHECK_COOLDOWN = 30000;
constructor(config: WorkspaceSkillsImplConfig);
/**
* @internal True while this view (or a path-scoped child) is inside an
* index-dependent operation. Owners must not release a busy view's index.
*/
get isIndexBusy(): boolean;
/**
* @internal Drop every search document this view (and its path-scoped
* children) indexed. The catalog stays intact; the next search-dependent
* operation re-admits the view via `onIndexReadmit` and re-indexes.
*/
releaseSearchIndex(): Promise<void>;
getScoped(context?: SkillsContext): Promise<WorkspaceSkills>;
list(): Promise<SkillMetadata[]>;
get(name: string): Promise<Skill | null>;
has(name: string): Promise<boolean>;
/**
* Register an alternate location string that resolves to the skill at `skillPath`.
* Called by SkillsProcessor when a `formatLocation` override remaps the advertised
* location, so the `skill` and `skill_read` tools can resolve the remapped
* location back to the underlying skill.
*
* Both the exact location and its `/SKILL.md`-stripped form are registered so
* lookups succeed whether or not the model preserves the suffix.
*/
registerLocationAlias(location: string, skillPath: string): void;
refresh(): Promise<void>;
maybeRefresh(context?: SkillsContext): Promise<void>;
addSkill(skillPath: string): Promise<void>;
removeSkill(skillName: string): Promise<void>;
search(query: string, options?: SkillSearchOptions): Promise<SkillSearchResult[]>;
getReference(skillName: string, referencePath: string): Promise<string | null>;
getScript(skillName: string, scriptPath: string): Promise<string | null>;
getAsset(skillName: string, assetPath: string): Promise<Buffer | null>;
listReferences(skillName: string): Promise<string[]>;
listScripts(skillName: string): Promise<string[]>;
listAssets(skillName: string): Promise<string[]>;
}
/**
* Resolves the skill source per request (e.g. from a dynamic workspace
* filesystem resolver).
*/
export type SkillSourceResolver = (context: SkillsContext) => Promise<SkillSourceInterface> | SkillSourceInterface;
export interface ResolvedSourceWorkspaceSkillsConfig extends Omit<WorkspaceSkillsImplConfig, 'source' | 'searchEngine' | 'searchNamespace' | 'sharedSearchState'> {
/** Resolves the skill source for a request. */
source: SkillSourceResolver;
/** Search engine whose documents can be removed when a resolved source is evicted. */
searchEngine?: RemovableSkillSearchEngine;
/**
* Maximum number of resolved sources to keep discovery caches and search
* documents for. Least-recently-used sources beyond this are evicted and
* their search documents removed. Defaults to 16.
*/
maxCachedSources?: number;
}
/**
* WorkspaceSkills backed by a per-request skill source.
*
* `getScoped()` resolves the source for the request and returns a
* `WorkspaceSkillsImpl` bound to it. Views are cached per resolved source
* instance and per request context. Sources are keyed by object identity only
* (never by a user-supplied `id`) so two tenants can never share a view by
* accident. Each source gets its own search namespace so same-named skills
* from different sources never bleed into each other's search results.
*
* The per-source cache is a bounded LRU: when a source is evicted, every
* document it indexed into the shared search engine is removed, so a resolver
* that returns a new filesystem on every request cannot grow the index
* without bound. An evicted view that is still in use re-admits itself (and
* re-indexes) on its next search, so callers never observe missing results.
* Resolvers that return a stable instance per tenant keep that tenant's
* discovery cache warm across requests.
*
* Calls made directly on this instance (without `getScoped`) resolve the
* source with an empty context, mirroring how workspace tools resolve the
* filesystem when no request context is available.
*/
export declare class ResolvedSourceWorkspaceSkills implements WorkspaceSkills {
#private;
constructor(config: ResolvedSourceWorkspaceSkillsConfig);
getScoped(context?: SkillsContext): Promise<WorkspaceSkills>;
list(): Promise<SkillMetadata[]>;
get(name: string): Promise<Skill | null>;
has(name: string): Promise<boolean>;
refresh(): Promise<void>;
maybeRefresh(context?: SkillsContext): Promise<void>;
search(query: string, options?: SkillSearchOptions): Promise<SkillSearchResult[]>;
getReference(skillName: string, referencePath: string): Promise<string | null>;
getScript(skillName: string, scriptPath: string): Promise<string | null>;
getAsset(skillName: string, assetPath: string): Promise<Buffer | null>;
listReferences(skillName: string): Promise<string[]>;
listScripts(skillName: string): Promise<string[]>;
listAssets(skillName: string): Promise<string[]>;
}
export {};
//# sourceMappingURL=workspace-skills.d.ts.map