@hyperfixi/patterns-reference 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Semantic Bridge
3
+ *
4
+ * Connects the patterns-reference database to the semantic registry,
5
+ * enabling runtime pattern matching using database-backed patterns.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { initializeSemanticIntegration } from '@hyperfixi/patterns-reference';
10
+ *
11
+ * // Initialize integration with semantic parser
12
+ * await initializeSemanticIntegration();
13
+ *
14
+ * // Now the semantic parser can access patterns from the database
15
+ * import { parse } from '@lokascript/semantic';
16
+ * const result = parse('toggle .active', 'en');
17
+ * ```
18
+ *
19
+ * @module @hyperfixi/patterns-reference/semantic-bridge
20
+ */
21
+
22
+ import {
23
+ createPatternsProvider,
24
+ type DatabasePatternsProvider,
25
+ } from './registry/patterns-provider';
26
+ import type { ConnectionOptions } from './types';
27
+
28
+ // =============================================================================
29
+ // Types
30
+ // =============================================================================
31
+
32
+ /**
33
+ * Options for semantic integration initialization.
34
+ */
35
+ export interface SemanticIntegrationOptions extends ConnectionOptions {
36
+ /** Whether to register as the default provider */
37
+ registerAsDefault?: boolean;
38
+ /** Custom provider instance to use */
39
+ provider?: DatabasePatternsProvider;
40
+ }
41
+
42
+ /**
43
+ * Result of initialization.
44
+ */
45
+ export interface IntegrationResult {
46
+ success: boolean;
47
+ provider: DatabasePatternsProvider;
48
+ registeredWith: 'semantic' | 'standalone';
49
+ error?: string;
50
+ }
51
+
52
+ // =============================================================================
53
+ // Bridge State
54
+ // =============================================================================
55
+
56
+ let bridgeInitialized = false;
57
+ let bridgeProvider: DatabasePatternsProvider | null = null;
58
+
59
+ // =============================================================================
60
+ // Integration Functions
61
+ // =============================================================================
62
+
63
+ /**
64
+ * Initialize semantic integration.
65
+ *
66
+ * This registers the patterns-reference database as an external pattern source
67
+ * with the @lokascript/semantic registry, enabling runtime pattern matching.
68
+ *
69
+ * @param options - Integration options
70
+ * @returns Integration result
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const result = await initializeSemanticIntegration();
75
+ * if (result.success) {
76
+ * console.log('Patterns available in semantic parser');
77
+ * }
78
+ * ```
79
+ */
80
+ export async function initializeSemanticIntegration(
81
+ options?: SemanticIntegrationOptions
82
+ ): Promise<IntegrationResult> {
83
+ // Create or use provided provider
84
+ const provider = options?.provider || createPatternsProvider(options);
85
+ bridgeProvider = provider;
86
+
87
+ try {
88
+ // Try to register with @lokascript/semantic
89
+ // Use 'any' to avoid type errors with dynamic imports
90
+ const semantic = (await import('@lokascript/semantic')) as any;
91
+
92
+ if (typeof semantic.registerPatternsSource === 'function') {
93
+ semantic.registerPatternsSource(provider);
94
+ bridgeInitialized = true;
95
+
96
+ return {
97
+ success: true,
98
+ provider,
99
+ registeredWith: 'semantic',
100
+ };
101
+ } else {
102
+ // Semantic package doesn't have registerPatternsSource yet
103
+ console.warn(
104
+ '[SemanticBridge] @lokascript/semantic does not export registerPatternsSource. ' +
105
+ 'Running in standalone mode.'
106
+ );
107
+
108
+ return {
109
+ success: true,
110
+ provider,
111
+ registeredWith: 'standalone',
112
+ };
113
+ }
114
+ } catch (error) {
115
+ // @lokascript/semantic not available
116
+ console.warn(
117
+ '[SemanticBridge] @lokascript/semantic not available:',
118
+ error instanceof Error ? error.message : String(error)
119
+ );
120
+
121
+ return {
122
+ success: true,
123
+ provider,
124
+ registeredWith: 'standalone',
125
+ error: '@lokascript/semantic not available',
126
+ };
127
+ }
128
+ }
129
+
130
+ /**
131
+ * Check if semantic integration is initialized.
132
+ */
133
+ export function isSemanticIntegrationInitialized(): boolean {
134
+ return bridgeInitialized;
135
+ }
136
+
137
+ /**
138
+ * Get the bridge provider instance.
139
+ */
140
+ export function getBridgeProvider(): DatabasePatternsProvider | null {
141
+ return bridgeProvider;
142
+ }
143
+
144
+ /**
145
+ * Uninitialize semantic integration.
146
+ * Useful for testing or cleanup.
147
+ */
148
+ export async function uninitializeSemanticIntegration(): Promise<void> {
149
+ if (!bridgeProvider) {
150
+ return;
151
+ }
152
+
153
+ try {
154
+ const semantic = (await import('@lokascript/semantic')) as any;
155
+
156
+ if (typeof semantic.unregisterPatternsSource === 'function') {
157
+ semantic.unregisterPatternsSource(bridgeProvider.id);
158
+ }
159
+ } catch {
160
+ // Ignore errors during cleanup
161
+ }
162
+
163
+ bridgeProvider.clearCache();
164
+ bridgeProvider = null;
165
+ bridgeInitialized = false;
166
+ }
167
+
168
+ // =============================================================================
169
+ // Convenience Functions
170
+ // =============================================================================
171
+
172
+ /**
173
+ * Query patterns from the database for a specific language.
174
+ * Works regardless of whether semantic integration is initialized.
175
+ *
176
+ * @param language - Language code
177
+ * @returns Array of pattern entries
178
+ */
179
+ export async function queryPatterns(language: string) {
180
+ const provider = bridgeProvider || createPatternsProvider();
181
+ return provider.getPatternsForLanguage(language);
182
+ }
183
+
184
+ /**
185
+ * Query patterns from the database for a specific command.
186
+ *
187
+ * @param command - Command name (e.g., 'toggle', 'add')
188
+ * @param language - Optional language filter
189
+ * @returns Array of pattern entries
190
+ */
191
+ export async function queryPatternsForCommand(command: string, language?: string) {
192
+ const provider = bridgeProvider || createPatternsProvider();
193
+ return provider.getPatternsForCommand(command, language);
194
+ }
195
+
196
+ /**
197
+ * Get supported languages from the database.
198
+ *
199
+ * @returns Array of language codes
200
+ */
201
+ export async function getSupportedLanguages(): Promise<string[]> {
202
+ const provider = bridgeProvider || createPatternsProvider();
203
+ return provider.getSupportedLanguages();
204
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Sync Module
3
+ *
4
+ * Re-exports sync operations that were migrated from packages/semantic/scripts/.
5
+ * These are typically run via CLI scripts rather than imported directly.
6
+ */
7
+
8
+ // Note: The actual sync implementations remain in packages/semantic/scripts/
9
+ // for now, as they are Bun-specific and need direct database access.
10
+ // This module provides type-safe wrappers that can be used programmatically.
11
+
12
+ import type {
13
+ SyncOptions,
14
+ SyncResult,
15
+ ValidationOptions,
16
+ ValidationResult,
17
+ DiscoveryResult,
18
+ } from '../types';
19
+
20
+ export type { SyncOptions, SyncResult, ValidationOptions, ValidationResult, DiscoveryResult };
21
+
22
+ /**
23
+ * Sync translations from code_examples to pattern_translations.
24
+ *
25
+ * This is a placeholder - the actual implementation is in
26
+ * packages/semantic/scripts/sync-lsp-translations.ts
27
+ */
28
+ export async function syncTranslations(_options: SyncOptions = {}): Promise<SyncResult> {
29
+ throw new Error(
30
+ 'syncTranslations should be run via: bun run packages/semantic/scripts/sync-lsp-translations.ts'
31
+ );
32
+ }
33
+
34
+ /**
35
+ * Validate all translations in the database.
36
+ *
37
+ * This is a placeholder - the actual implementation is in
38
+ * packages/semantic/scripts/validate-translations.ts
39
+ */
40
+ export async function validateAllTranslations(
41
+ _options: ValidationOptions = {}
42
+ ): Promise<ValidationResult> {
43
+ throw new Error(
44
+ 'validateAllTranslations should be run via: bun run packages/semantic/scripts/validate-translations.ts'
45
+ );
46
+ }
47
+
48
+ /**
49
+ * Discover and analyze patterns in the database.
50
+ *
51
+ * This is a placeholder - the actual implementation is in
52
+ * packages/semantic/scripts/pattern-discovery.ts
53
+ */
54
+ export async function discoverPatterns(_verbose: boolean = false): Promise<DiscoveryResult> {
55
+ throw new Error(
56
+ 'discoverPatterns should be run via: bun run packages/semantic/scripts/pattern-discovery.ts'
57
+ );
58
+ }
59
+
60
+ /**
61
+ * Seed LLM examples from verified translations.
62
+ *
63
+ * This is a placeholder - the actual implementation is in
64
+ * packages/semantic/scripts/seed-llm-examples.ts
65
+ */
66
+ export async function seedLLMExamples(_dryRun: boolean = false): Promise<{ count: number }> {
67
+ throw new Error(
68
+ 'seedLLMExamples should be run via: bun run packages/semantic/scripts/seed-llm-examples.ts'
69
+ );
70
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Type declarations for better-sqlite3.
3
+ * This is a minimal declaration to support typecheck before npm install.
4
+ * The full types from @types/better-sqlite3 will be used after install.
5
+ */
6
+
7
+ declare module 'better-sqlite3' {
8
+ namespace BetterSqlite3 {
9
+ interface Database {
10
+ prepare<T = unknown>(sql: string): Statement<T>;
11
+ exec(sql: string): this;
12
+ pragma(pragma: string, options?: { simple?: boolean }): unknown;
13
+ close(): void;
14
+ }
15
+
16
+ interface Statement<T = unknown> {
17
+ run(...params: unknown[]): RunResult;
18
+ get(...params: unknown[]): T | undefined;
19
+ all(...params: unknown[]): T[];
20
+ }
21
+
22
+ interface RunResult {
23
+ changes: number;
24
+ lastInsertRowid: number | bigint;
25
+ }
26
+
27
+ interface Options {
28
+ readonly?: boolean;
29
+ fileMustExist?: boolean;
30
+ timeout?: number;
31
+ verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;
32
+ }
33
+ }
34
+
35
+ class Database implements BetterSqlite3.Database {
36
+ constructor(filename: string, options?: BetterSqlite3.Options);
37
+ prepare<T = unknown>(sql: string): BetterSqlite3.Statement<T>;
38
+ exec(sql: string): this;
39
+ pragma(pragma: string, options?: { simple?: boolean }): unknown;
40
+ close(): void;
41
+ }
42
+
43
+ export = Database;
44
+ }