@hyperfixi/patterns-reference 3.1.1 → 3.2.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.
- package/CHANGELOG.md +145 -0
- package/README.md +47 -36
- package/data/engine-verification.json +40 -38
- package/data/patterns.db +0 -0
- package/data/patterns.db.stamp +1 -0
- package/dist/api/index.d.mts +2 -2
- package/dist/api/index.d.ts +2 -2
- package/dist/api/index.js +198 -98
- package/dist/api/index.mjs +197 -100
- package/dist/{index-6GkHj5yJ.d.mts → index-CoDUfq2P.d.mts} +39 -3
- package/dist/{index-6GkHj5yJ.d.ts → index-CoDUfq2P.d.ts} +39 -3
- package/dist/index.d.mts +87 -13
- package/dist/index.d.ts +87 -13
- package/dist/index.js +261 -144
- package/dist/index.mjs +258 -146
- package/dist/{llm-B5nGz8V1.d.mts → llm-CCCSw-yp.d.ts} +21 -10
- package/dist/{llm-rEdJQScF.d.ts → llm-PxnriEZ_.d.mts} +21 -10
- package/dist/sync/index.d.mts +1 -1
- package/dist/sync/index.d.ts +1 -1
- package/dist/sync/index.js +4 -1
- package/dist/sync/index.mjs +4 -1
- package/package.json +16 -16
- package/src/adapters/llm-adapter.ts +64 -42
- package/src/api/engine-filter.ts +37 -0
- package/src/api/llm.ts +54 -64
- package/src/api/patterns.ts +92 -30
- package/src/api/roles.ts +6 -4
- package/src/api/translations.ts +34 -27
- package/src/database/connection.ts +16 -4
- package/src/html-snippets.ts +39 -10
- package/src/index.ts +12 -2
- package/src/registry/patterns-provider.ts +3 -1
- package/src/sync/db-stamp.ts +7 -4
- package/src/sync/markup-attributes.ts +15 -0
- package/src/sync/verify-parses.ts +42 -0
- package/src/types/better-sqlite3.d.ts +2 -0
- package/src/types/index.ts +44 -2
- package/src/sync/span-mask.ts +0 -166
- package/src/sync/translation-checks.ts +0 -282
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as SearchOptions, C as ConnectionOptions, a as Pattern, l as PatternStats, p as Translation, W as WordOrder, s as VerificationResult, f as LLMExample, j as ExampleOptions } from './index-CoDUfq2P.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Pattern Queries API
|
|
@@ -19,7 +19,9 @@ declare function getPatternsByCategory(category: string, options?: ConnectionOpt
|
|
|
19
19
|
*/
|
|
20
20
|
declare function getPatternsByCommand(command: string, options?: ConnectionOptions): Promise<Pattern[]>;
|
|
21
21
|
/**
|
|
22
|
-
* Search patterns by text query
|
|
22
|
+
* Search patterns by text query: title, code or description — and, when a
|
|
23
|
+
* `language` is named, that language's translation, so a query in the language
|
|
24
|
+
* finds a pattern by its own code there.
|
|
23
25
|
*/
|
|
24
26
|
declare function searchPatterns(query: string, searchOptions?: SearchOptions, connOptions?: ConnectionOptions): Promise<Pattern[]>;
|
|
25
27
|
/**
|
|
@@ -67,8 +69,10 @@ declare function getVerifiedTranslations(language: string, limit?: number, optio
|
|
|
67
69
|
*/
|
|
68
70
|
declare function getHighConfidenceTranslations(minConfidence?: number, limit?: number, options?: ConnectionOptions): Promise<Translation[]>;
|
|
69
71
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
+
* Re-measure whether a translation parses, record the result, and return it.
|
|
73
|
+
* Uses the same definition sync-translations writes `verified_parses` with
|
|
74
|
+
* (src/sync/verify-parses.ts): every hyperscript body of the row parses in its
|
|
75
|
+
* language. Note: This requires @lokascript/semantic to be available.
|
|
72
76
|
*/
|
|
73
77
|
declare function verifyTranslation(translation: Translation, options?: ConnectionOptions): Promise<VerificationResult>;
|
|
74
78
|
/**
|
|
@@ -94,27 +98,33 @@ declare function getWordOrder(language: string): WordOrder;
|
|
|
94
98
|
* Get LLM examples relevant to a prompt.
|
|
95
99
|
* Uses text similarity to find relevant patterns.
|
|
96
100
|
*/
|
|
97
|
-
declare function getLLMExamples(prompt: string, language?: string, limit?: number, options?:
|
|
101
|
+
declare function getLLMExamples(prompt: string, language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
98
102
|
/**
|
|
99
103
|
* Get examples by command type.
|
|
100
104
|
*/
|
|
101
|
-
declare function getExamplesByCommand(command: string, language?: string, limit?: number, options?:
|
|
105
|
+
declare function getExamplesByCommand(command: string, language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
102
106
|
/**
|
|
103
107
|
* Get high-quality examples (for few-shot prompts).
|
|
104
108
|
*/
|
|
105
|
-
declare function getHighQualityExamples(language?: string, minQuality?: number, limit?: number, options?:
|
|
109
|
+
declare function getHighQualityExamples(language?: string, minQuality?: number, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
106
110
|
/**
|
|
107
111
|
* Get most used examples (popular).
|
|
112
|
+
*
|
|
113
|
+
* @deprecated Only an explicit trackExampleUsage() call counts a use, and nothing
|
|
114
|
+
* makes one, so `usage_count` is 0 on every shipped example and this ranks by
|
|
115
|
+
* quality alone: getHighQualityExamples with no floor. Reads used to count
|
|
116
|
+
* themselves through the read-only handle they open; the write failed and the
|
|
117
|
+
* error was swallowed.
|
|
108
118
|
*/
|
|
109
|
-
declare function getMostUsedExamples(language?: string, limit?: number, options?:
|
|
119
|
+
declare function getMostUsedExamples(language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
110
120
|
/**
|
|
111
121
|
* Build few-shot context for LLM prompting.
|
|
112
122
|
*/
|
|
113
|
-
declare function buildFewShotContext(prompt: string, language?: string, numExamples?: number, options?:
|
|
123
|
+
declare function buildFewShotContext(prompt: string, language?: string, numExamples?: number, options?: ExampleOptions): Promise<string>;
|
|
114
124
|
/**
|
|
115
125
|
* Add a new LLM example.
|
|
116
126
|
*/
|
|
117
|
-
declare function addLLMExample(example: Omit<LLMExample, 'id' | 'usageCount' | 'createdAt'>, options?: ConnectionOptions): Promise<number>;
|
|
127
|
+
declare function addLLMExample(example: Omit<LLMExample, 'id' | 'usageCount' | 'createdAt' | 'engine'>, options?: ConnectionOptions): Promise<number>;
|
|
118
128
|
/**
|
|
119
129
|
* Update example quality score.
|
|
120
130
|
*/
|
|
@@ -126,6 +136,7 @@ declare function getLLMStats(options?: ConnectionOptions): Promise<{
|
|
|
126
136
|
total: number;
|
|
127
137
|
byLanguage: Record<string, number>;
|
|
128
138
|
avgQuality: number;
|
|
139
|
+
/** @deprecated 0 unless something calls trackExampleUsage() (see getMostUsedExamples). */
|
|
129
140
|
totalUsage: number;
|
|
130
141
|
}>;
|
|
131
142
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { m as SearchOptions, C as ConnectionOptions, a as Pattern, l as PatternStats, p as Translation, W as WordOrder, s as VerificationResult, f as LLMExample, j as ExampleOptions } from './index-CoDUfq2P.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Pattern Queries API
|
|
@@ -19,7 +19,9 @@ declare function getPatternsByCategory(category: string, options?: ConnectionOpt
|
|
|
19
19
|
*/
|
|
20
20
|
declare function getPatternsByCommand(command: string, options?: ConnectionOptions): Promise<Pattern[]>;
|
|
21
21
|
/**
|
|
22
|
-
* Search patterns by text query
|
|
22
|
+
* Search patterns by text query: title, code or description — and, when a
|
|
23
|
+
* `language` is named, that language's translation, so a query in the language
|
|
24
|
+
* finds a pattern by its own code there.
|
|
23
25
|
*/
|
|
24
26
|
declare function searchPatterns(query: string, searchOptions?: SearchOptions, connOptions?: ConnectionOptions): Promise<Pattern[]>;
|
|
25
27
|
/**
|
|
@@ -67,8 +69,10 @@ declare function getVerifiedTranslations(language: string, limit?: number, optio
|
|
|
67
69
|
*/
|
|
68
70
|
declare function getHighConfidenceTranslations(minConfidence?: number, limit?: number, options?: ConnectionOptions): Promise<Translation[]>;
|
|
69
71
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
+
* Re-measure whether a translation parses, record the result, and return it.
|
|
73
|
+
* Uses the same definition sync-translations writes `verified_parses` with
|
|
74
|
+
* (src/sync/verify-parses.ts): every hyperscript body of the row parses in its
|
|
75
|
+
* language. Note: This requires @lokascript/semantic to be available.
|
|
72
76
|
*/
|
|
73
77
|
declare function verifyTranslation(translation: Translation, options?: ConnectionOptions): Promise<VerificationResult>;
|
|
74
78
|
/**
|
|
@@ -94,27 +98,33 @@ declare function getWordOrder(language: string): WordOrder;
|
|
|
94
98
|
* Get LLM examples relevant to a prompt.
|
|
95
99
|
* Uses text similarity to find relevant patterns.
|
|
96
100
|
*/
|
|
97
|
-
declare function getLLMExamples(prompt: string, language?: string, limit?: number, options?:
|
|
101
|
+
declare function getLLMExamples(prompt: string, language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
98
102
|
/**
|
|
99
103
|
* Get examples by command type.
|
|
100
104
|
*/
|
|
101
|
-
declare function getExamplesByCommand(command: string, language?: string, limit?: number, options?:
|
|
105
|
+
declare function getExamplesByCommand(command: string, language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
102
106
|
/**
|
|
103
107
|
* Get high-quality examples (for few-shot prompts).
|
|
104
108
|
*/
|
|
105
|
-
declare function getHighQualityExamples(language?: string, minQuality?: number, limit?: number, options?:
|
|
109
|
+
declare function getHighQualityExamples(language?: string, minQuality?: number, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
106
110
|
/**
|
|
107
111
|
* Get most used examples (popular).
|
|
112
|
+
*
|
|
113
|
+
* @deprecated Only an explicit trackExampleUsage() call counts a use, and nothing
|
|
114
|
+
* makes one, so `usage_count` is 0 on every shipped example and this ranks by
|
|
115
|
+
* quality alone: getHighQualityExamples with no floor. Reads used to count
|
|
116
|
+
* themselves through the read-only handle they open; the write failed and the
|
|
117
|
+
* error was swallowed.
|
|
108
118
|
*/
|
|
109
|
-
declare function getMostUsedExamples(language?: string, limit?: number, options?:
|
|
119
|
+
declare function getMostUsedExamples(language?: string, limit?: number, options?: ExampleOptions): Promise<LLMExample[]>;
|
|
110
120
|
/**
|
|
111
121
|
* Build few-shot context for LLM prompting.
|
|
112
122
|
*/
|
|
113
|
-
declare function buildFewShotContext(prompt: string, language?: string, numExamples?: number, options?:
|
|
123
|
+
declare function buildFewShotContext(prompt: string, language?: string, numExamples?: number, options?: ExampleOptions): Promise<string>;
|
|
114
124
|
/**
|
|
115
125
|
* Add a new LLM example.
|
|
116
126
|
*/
|
|
117
|
-
declare function addLLMExample(example: Omit<LLMExample, 'id' | 'usageCount' | 'createdAt'>, options?: ConnectionOptions): Promise<number>;
|
|
127
|
+
declare function addLLMExample(example: Omit<LLMExample, 'id' | 'usageCount' | 'createdAt' | 'engine'>, options?: ConnectionOptions): Promise<number>;
|
|
118
128
|
/**
|
|
119
129
|
* Update example quality score.
|
|
120
130
|
*/
|
|
@@ -126,6 +136,7 @@ declare function getLLMStats(options?: ConnectionOptions): Promise<{
|
|
|
126
136
|
total: number;
|
|
127
137
|
byLanguage: Record<string, number>;
|
|
128
138
|
avgQuality: number;
|
|
139
|
+
/** @deprecated 0 unless something calls trackExampleUsage() (see getMostUsedExamples). */
|
|
129
140
|
totalUsage: number;
|
|
130
141
|
}>;
|
|
131
142
|
|
package/dist/sync/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { D as DiscoveryResult,
|
|
1
|
+
import { D as DiscoveryResult, n as SyncOptions, o as SyncResult, V as ValidationOptions, r as ValidationResult } from '../index-CoDUfq2P.mjs';
|
|
2
2
|
import '@lokascript/semantic';
|
|
3
3
|
|
|
4
4
|
/**
|
package/dist/sync/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { D as DiscoveryResult,
|
|
1
|
+
import { D as DiscoveryResult, n as SyncOptions, o as SyncResult, V as ValidationOptions, r as ValidationResult } from '../index-CoDUfq2P.js';
|
|
2
2
|
import '@lokascript/semantic';
|
|
3
3
|
|
|
4
4
|
/**
|
package/dist/sync/index.js
CHANGED
|
@@ -30,7 +30,10 @@ function dbInputFiles(dbPath) {
|
|
|
30
30
|
for (const f of [
|
|
31
31
|
path.join(pr, "scripts", "init-db.ts"),
|
|
32
32
|
path.join(pr, "scripts", "sync-translations.ts"),
|
|
33
|
-
|
|
33
|
+
// Decides which markup `_` bodies are translated (and which stay English).
|
|
34
|
+
path.join(pr, "src", "sync", "markup-attributes.ts"),
|
|
35
|
+
// Writes every row's `verified_parses`.
|
|
36
|
+
path.join(pr, "src", "sync", "verify-parses.ts"),
|
|
34
37
|
// Committed engine-verification results are seeded into the engine
|
|
35
38
|
// column by init-db.ts, so they are DB input too.
|
|
36
39
|
path.join(pr, "data", "engine-verification.json")
|
package/dist/sync/index.mjs
CHANGED
|
@@ -28,7 +28,10 @@ function dbInputFiles(dbPath) {
|
|
|
28
28
|
for (const f of [
|
|
29
29
|
join(pr, "scripts", "init-db.ts"),
|
|
30
30
|
join(pr, "scripts", "sync-translations.ts"),
|
|
31
|
-
|
|
31
|
+
// Decides which markup `_` bodies are translated (and which stay English).
|
|
32
|
+
join(pr, "src", "sync", "markup-attributes.ts"),
|
|
33
|
+
// Writes every row's `verified_parses`.
|
|
34
|
+
join(pr, "src", "sync", "verify-parses.ts"),
|
|
32
35
|
// Committed engine-verification results are seeded into the engine
|
|
33
36
|
// column by init-db.ts, so they are DB input too.
|
|
34
37
|
join(pr, "data", "engine-verification.json")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyperfixi/patterns-reference",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Queryable patterns database for hyperscript with multilingual translations and LLM support",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "tsup",
|
|
27
27
|
"dev": "tsup --watch",
|
|
28
|
+
"pretest": "../../scripts/ensure-fresh.sh ../semantic",
|
|
28
29
|
"test": "vitest",
|
|
29
30
|
"test:run": "vitest run",
|
|
30
31
|
"typecheck": "tsc --noEmit",
|
|
@@ -32,15 +33,13 @@
|
|
|
32
33
|
"db:init:force": "tsx scripts/init-db.ts --force",
|
|
33
34
|
"sync:translations": "tsx scripts/sync-translations.ts",
|
|
34
35
|
"seed:llm": "tsx scripts/seed-llm-examples.ts",
|
|
35
|
-
"populate": "npm run db:init:force && npm run sync:translations && npm run seed:llm
|
|
36
|
-
"db:fix-translations": "tsx scripts/run-sql.ts scripts/fix-translations.sql",
|
|
36
|
+
"populate": "npm run db:init:force && npm run sync:translations && npm run seed:llm",
|
|
37
37
|
"populate:dry-run": "npm run db:init:force && tsx scripts/sync-translations.ts --dry-run && tsx scripts/seed-llm-examples.ts --dry-run",
|
|
38
|
-
"validate": "tsx scripts/validate-all.ts",
|
|
39
|
-
"validate:fix": "tsx scripts/validate-all.ts --fix",
|
|
40
38
|
"verify": "tsx scripts/verify-translations.ts",
|
|
41
39
|
"verify:verbose": "tsx scripts/verify-translations.ts --verbose",
|
|
42
40
|
"test:check": "VITEST_QUIET=1 bash ../../scripts/vitest-run.sh --reporter=dot",
|
|
43
|
-
"verify:engines": "tsx scripts/verify-engines.ts --update-db"
|
|
41
|
+
"verify:engines": "tsx scripts/verify-engines.ts --update-db",
|
|
42
|
+
"verify:engines:check": "tsx scripts/verify-engines.ts --check"
|
|
44
43
|
},
|
|
45
44
|
"keywords": [
|
|
46
45
|
"hyperscript",
|
|
@@ -66,21 +65,22 @@
|
|
|
66
65
|
},
|
|
67
66
|
"homepage": "https://github.com/codetalcott/hyperfixi/tree/main/packages/patterns-reference#readme",
|
|
68
67
|
"dependencies": {
|
|
69
|
-
"@lokascript/semantic": "^3.
|
|
70
|
-
"better-sqlite3": "^13.0.
|
|
68
|
+
"@lokascript/semantic": "^3.2.0",
|
|
69
|
+
"better-sqlite3": "^13.0.3"
|
|
71
70
|
},
|
|
72
71
|
"devDependencies": {
|
|
73
|
-
"@hyperfixi/
|
|
74
|
-
"@hyperfixi/
|
|
75
|
-
"@hyperfixi/
|
|
76
|
-
"@
|
|
77
|
-
"@types/
|
|
72
|
+
"@hyperfixi/components": "^3.2.0",
|
|
73
|
+
"@hyperfixi/core": "^3.2.0",
|
|
74
|
+
"@hyperfixi/reactivity": "^3.2.0",
|
|
75
|
+
"@hyperfixi/realtime": "^3.2.0",
|
|
76
|
+
"@types/better-sqlite3": "^9.6.0",
|
|
77
|
+
"@types/node": "^26.6.3",
|
|
78
78
|
"hyperscript.org": "0.9.93",
|
|
79
|
-
"jsdom": "^30.
|
|
79
|
+
"jsdom": "^30.1.1",
|
|
80
80
|
"tsup": "^8.0.0",
|
|
81
|
-
"tsx": "^4.23.
|
|
81
|
+
"tsx": "^4.23.15",
|
|
82
82
|
"typescript": "^5.0.0",
|
|
83
|
-
"vitest": "^
|
|
83
|
+
"vitest": "^5.0.2"
|
|
84
84
|
},
|
|
85
85
|
"files": [
|
|
86
86
|
"dist",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { getDatabase, closeDatabase } from '../database/connection';
|
|
12
|
-
import type { LLMExample, ConnectionOptions } from '../types';
|
|
12
|
+
import type { LLMExample, ConnectionOptions, EngineCompat } from '../types';
|
|
13
|
+
import { exampleCondition } from '../api/engine-filter';
|
|
13
14
|
import {
|
|
14
15
|
getLLMExamples,
|
|
15
16
|
getExamplesByCommand,
|
|
@@ -32,8 +33,17 @@ export interface LLMExampleRecord {
|
|
|
32
33
|
completion: string;
|
|
33
34
|
language: string;
|
|
34
35
|
qualityScore: number;
|
|
36
|
+
/** The engine(s) verified to run the example's pattern (never null here). */
|
|
37
|
+
engine: EngineCompat | null;
|
|
35
38
|
}
|
|
36
39
|
|
|
40
|
+
/** Every sync example query reads through this join; see api/engine-filter.ts. */
|
|
41
|
+
const RECORDS_FROM = `
|
|
42
|
+
SELECT le.id, le.prompt, le.completion, le.language, le.quality_score as qualityScore,
|
|
43
|
+
ce.engine AS engine
|
|
44
|
+
FROM llm_examples le
|
|
45
|
+
JOIN code_examples ce ON ce.id = le.code_example_id`;
|
|
46
|
+
|
|
37
47
|
/**
|
|
38
48
|
* Database row type for internal use
|
|
39
49
|
*/
|
|
@@ -73,16 +83,20 @@ export function isDatabaseAvailable(): boolean {
|
|
|
73
83
|
* @param prompt - The user's request/prompt
|
|
74
84
|
* @param language - Target language code (default: 'en')
|
|
75
85
|
* @param limit - Maximum number of examples to return (default: 5)
|
|
86
|
+
* @param engine - Only examples whose pattern runs on this engine. Examples no
|
|
87
|
+
* engine runs are never returned, with or without it.
|
|
76
88
|
*/
|
|
77
89
|
export function findRelevantExamples(
|
|
78
90
|
prompt: string,
|
|
79
91
|
language: string = 'en',
|
|
80
|
-
limit: number = 5
|
|
92
|
+
limit: number = 5,
|
|
93
|
+
engine?: EngineCompat
|
|
81
94
|
): LLMExampleRecord[] {
|
|
82
95
|
if (!syncDatabaseAvailable) return [];
|
|
83
96
|
|
|
84
97
|
try {
|
|
85
98
|
const db = getDatabase({ readonly: true });
|
|
99
|
+
const runs = exampleCondition('ce.engine', engine);
|
|
86
100
|
|
|
87
101
|
// Extract keywords from prompt
|
|
88
102
|
const keywords = extractKeywords(prompt);
|
|
@@ -91,43 +105,33 @@ export function findRelevantExamples(
|
|
|
91
105
|
// Return top-quality examples as fallback
|
|
92
106
|
const rows = db
|
|
93
107
|
.prepare(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
WHERE language = ?
|
|
98
|
-
ORDER BY quality_score DESC, usage_count DESC
|
|
108
|
+
`${RECORDS_FROM}
|
|
109
|
+
WHERE le.language = ? AND ${runs.sql}
|
|
110
|
+
ORDER BY le.quality_score DESC, le.usage_count DESC
|
|
99
111
|
LIMIT ?
|
|
100
112
|
`
|
|
101
113
|
)
|
|
102
|
-
.all(language, limit) as LLMExampleRecord[];
|
|
114
|
+
.all(language, ...runs.params, limit) as LLMExampleRecord[];
|
|
103
115
|
|
|
104
|
-
trackUsageSync(
|
|
105
|
-
db,
|
|
106
|
-
rows.map(r => r.id)
|
|
107
|
-
);
|
|
108
116
|
return rows;
|
|
109
117
|
}
|
|
110
118
|
|
|
111
119
|
// Build LIKE clauses for keyword matching
|
|
112
|
-
const likeClauses = keywords
|
|
120
|
+
const likeClauses = keywords
|
|
121
|
+
.map(() => '(le.prompt LIKE ? OR le.completion LIKE ?)')
|
|
122
|
+
.join(' OR ');
|
|
113
123
|
const params = keywords.flatMap(k => [`%${k}%`, `%${k}%`]);
|
|
114
124
|
|
|
115
125
|
const rows = db
|
|
116
126
|
.prepare(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
WHERE language = ? AND (${likeClauses})
|
|
121
|
-
ORDER BY quality_score DESC
|
|
127
|
+
`${RECORDS_FROM}
|
|
128
|
+
WHERE le.language = ? AND ${runs.sql} AND (${likeClauses})
|
|
129
|
+
ORDER BY le.quality_score DESC
|
|
122
130
|
LIMIT ?
|
|
123
131
|
`
|
|
124
132
|
)
|
|
125
|
-
.all(language, ...params, limit) as LLMExampleRecord[];
|
|
133
|
+
.all(language, ...runs.params, ...params, limit) as LLMExampleRecord[];
|
|
126
134
|
|
|
127
|
-
trackUsageSync(
|
|
128
|
-
db,
|
|
129
|
-
rows.map(r => r.id)
|
|
130
|
-
);
|
|
131
135
|
return rows;
|
|
132
136
|
} catch (error) {
|
|
133
137
|
console.warn(
|
|
@@ -145,24 +149,24 @@ export function findRelevantExamples(
|
|
|
145
149
|
export function findExamplesByCommand(
|
|
146
150
|
command: string,
|
|
147
151
|
language: string = 'en',
|
|
148
|
-
limit: number = 5
|
|
152
|
+
limit: number = 5,
|
|
153
|
+
engine?: EngineCompat
|
|
149
154
|
): LLMExampleRecord[] {
|
|
150
155
|
if (!syncDatabaseAvailable) return [];
|
|
151
156
|
|
|
152
157
|
try {
|
|
153
158
|
const db = getDatabase({ readonly: true });
|
|
159
|
+
const runs = exampleCondition('ce.engine', engine);
|
|
154
160
|
|
|
155
161
|
const rows = db
|
|
156
162
|
.prepare(
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
WHERE language = ? AND completion LIKE ?
|
|
161
|
-
ORDER BY quality_score DESC
|
|
163
|
+
`${RECORDS_FROM}
|
|
164
|
+
WHERE le.language = ? AND ${runs.sql} AND le.completion LIKE ?
|
|
165
|
+
ORDER BY le.quality_score DESC
|
|
162
166
|
LIMIT ?
|
|
163
167
|
`
|
|
164
168
|
)
|
|
165
|
-
.all(language, `%${command}%`, limit) as LLMExampleRecord[];
|
|
169
|
+
.all(language, ...runs.params, `%${command}%`, limit) as LLMExampleRecord[];
|
|
166
170
|
|
|
167
171
|
return rows;
|
|
168
172
|
} catch (error) {
|
|
@@ -180,9 +184,10 @@ export function findExamplesByCommand(
|
|
|
180
184
|
export function buildFewShotContextSync(
|
|
181
185
|
prompt: string,
|
|
182
186
|
language: string = 'en',
|
|
183
|
-
numExamples: number = 3
|
|
187
|
+
numExamples: number = 3,
|
|
188
|
+
engine?: EngineCompat
|
|
184
189
|
): string {
|
|
185
|
-
const examples = findRelevantExamples(prompt, language, numExamples);
|
|
190
|
+
const examples = findRelevantExamples(prompt, language, numExamples, engine);
|
|
186
191
|
|
|
187
192
|
if (examples.length === 0) {
|
|
188
193
|
return '';
|
|
@@ -202,6 +207,10 @@ export function buildFewShotContextSync(
|
|
|
202
207
|
|
|
203
208
|
/**
|
|
204
209
|
* Track example usage (sync interface).
|
|
210
|
+
*
|
|
211
|
+
* @deprecated The counts go into the installed package's own database file,
|
|
212
|
+
* which every install and every `populate` replaces, and only
|
|
213
|
+
* getMostUsedExamples (deprecated with it) reads them. Nothing calls this.
|
|
205
214
|
*/
|
|
206
215
|
export function trackExampleUsage(ids: number[]): void {
|
|
207
216
|
if (!syncDatabaseAvailable || ids.length === 0) return;
|
|
@@ -376,16 +385,29 @@ export function createLLMAdapter(options?: ConnectionOptions) {
|
|
|
376
385
|
isDatabaseAvailable,
|
|
377
386
|
|
|
378
387
|
// Async methods (preferred for new code)
|
|
379
|
-
getLLMExamples: (prompt: string, language?: string, limit?: number) =>
|
|
380
|
-
getLLMExamples(prompt, language, limit, options),
|
|
381
|
-
getExamplesByCommand: (
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
388
|
+
getLLMExamples: (prompt: string, language?: string, limit?: number, engine?: EngineCompat) =>
|
|
389
|
+
getLLMExamples(prompt, language, limit, { ...options, engine }),
|
|
390
|
+
getExamplesByCommand: (
|
|
391
|
+
command: string,
|
|
392
|
+
language?: string,
|
|
393
|
+
limit?: number,
|
|
394
|
+
engine?: EngineCompat
|
|
395
|
+
) => getExamplesByCommand(command, language, limit, { ...options, engine }),
|
|
396
|
+
getHighQualityExamples: (
|
|
397
|
+
language?: string,
|
|
398
|
+
minQuality?: number,
|
|
399
|
+
limit?: number,
|
|
400
|
+
engine?: EngineCompat
|
|
401
|
+
) => getHighQualityExamples(language, minQuality, limit, { ...options, engine }),
|
|
402
|
+
/** @deprecated See getMostUsedExamples. */
|
|
403
|
+
getMostUsedExamples: (language?: string, limit?: number, engine?: EngineCompat) =>
|
|
404
|
+
getMostUsedExamples(language, limit, { ...options, engine }),
|
|
405
|
+
buildFewShotContext: (
|
|
406
|
+
prompt: string,
|
|
407
|
+
language?: string,
|
|
408
|
+
numExamples?: number,
|
|
409
|
+
engine?: EngineCompat
|
|
410
|
+
) => buildFewShotContext(prompt, language, numExamples, { ...options, engine }),
|
|
389
411
|
getLLMStats: () => getLLMStats(options),
|
|
390
412
|
|
|
391
413
|
// Cleanup
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selecting patterns by the engine that runs them.
|
|
3
|
+
*
|
|
4
|
+
* `code_examples.engine` is mechanically verified (scripts/verify-engines.ts;
|
|
5
|
+
* CI's `verify:engines:check`): 'both', 'lokascript' (hyperfixi only),
|
|
6
|
+
* 'hyperscript' (upstream _hyperscript only) or NULL (verified on neither).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { EngineCompat } from '../types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* SQL for "the pattern runs on `engine`" over an engine column. A pattern
|
|
13
|
+
* verified on both engines runs on either, so 'hyperscript' and 'lokascript'
|
|
14
|
+
* include 'both'. `null` selects the patterns no engine runs.
|
|
15
|
+
*/
|
|
16
|
+
export function runsOn(
|
|
17
|
+
column: string,
|
|
18
|
+
engine: EngineCompat | null
|
|
19
|
+
): { sql: string; params: string[] } {
|
|
20
|
+
if (engine === null) return { sql: `${column} IS NULL`, params: [] };
|
|
21
|
+
if (engine === 'both') return { sql: `${column} = 'both'`, params: [] };
|
|
22
|
+
return { sql: `${column} IN ('both', ?)`, params: [engine] };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The condition for serving LLM examples. An example is code handed to a
|
|
27
|
+
* generator as correct, so one whose pattern NO engine runs is never served;
|
|
28
|
+
* `engine` narrows further to what a particular runtime accepts.
|
|
29
|
+
*/
|
|
30
|
+
export function exampleCondition(
|
|
31
|
+
column: string,
|
|
32
|
+
engine: EngineCompat | undefined
|
|
33
|
+
): { sql: string; params: string[] } {
|
|
34
|
+
return engine === undefined
|
|
35
|
+
? { sql: `${column} IS NOT NULL`, params: [] }
|
|
36
|
+
: runsOn(column, engine);
|
|
37
|
+
}
|