MCPcopy
hub / github.com/foambubble/foam / FoamEmbeddings

Class FoamEmbeddings

packages/foam-vscode/src/ai/model/embeddings.ts:36–382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

34 * Manages embeddings for all resources in the workspace
35 */
36export class FoamEmbeddings implements IDisposable {
37 /**
38 * Maps resource URIs to their embeddings
39 */
40 private embeddings: Map<string, Embedding> = new Map();
41
42 private onDidUpdateEmitter = new Emitter<void>();
43 onDidUpdate = this.onDidUpdateEmitter.event;
44
45 /**
46 * List of disposables to destroy with the embeddings
47 */
48 private disposables: IDisposable[] = [];
49
50 constructor(
51 private readonly workspace: FoamWorkspace,
52 private readonly provider: EmbeddingProvider,
53 private readonly cache?: EmbeddingCache
54 ) {}
55
56 /**
57 * Get the embedding for a resource
58 * @param uri The URI of the resource
59 * @returns The embedding vector, or null if not found
60 */
61 public getEmbedding(uri: URI): number[] | null {
62 const embedding = this.embeddings.get(uri.path);
63 return embedding ? embedding.vector : null;
64 }
65
66 /**
67 * Check if embeddings are available
68 * @returns true if at least one embedding exists
69 */
70 public hasEmbeddings(): boolean {
71 return this.embeddings.size > 0;
72 }
73
74 /**
75 * Get the number of embeddings
76 * @returns The count of embeddings
77 */
78 public size(): number {
79 return this.embeddings.size;
80 }
81
82 /**
83 * Find similar resources to a given resource
84 * @param uri The URI of the target resource
85 * @param topK The number of similar resources to return
86 * @returns Array of similar resources sorted by similarity (highest first)
87 */
88 public getSimilar(uri: URI, topK: number = 10): SimilarResource[] {
89 const targetEmbedding = this.getEmbedding(uri);
90 if (!targetEmbedding) {
91 return [];
92 }
93

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected