Skip to content

Commit dd5cc92

Browse files
authored
Use protobuf definition to generate language independent types (#9)
* Protobuf definition and TS type generation - initial # Conflicts: # package-lock.json # packages/langium-ai-tools/package.json * Align langium and lai versions
1 parent b130cb9 commit dd5cc92

File tree

12 files changed

+968
-741
lines changed

12 files changed

+968
-741
lines changed

package-lock.json

Lines changed: 768 additions & 681 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/examples/example-dsl-evaluator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"chromadb": "^1.9.2",
2222
"chromadb-default-embed": "^2.13.2",
2323
"dotenv": "^16.4.5",
24-
"langium-ai-tools": "^0.0.2",
24+
"langium-ai-tools": "^4.1.2",
2525
"ollama": "^0.5.9",
2626
"openai": "^4.67.3"
2727
},

packages/examples/example-dsl-evaluator/src/eval-langium.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ function calculateTriangleAreas(data: MergedEvaluatorResultType): number[] {
144144
data.warnings,
145145
1.0 - (data.similarity ?? 0), // Semantic Diff
146146
(data.errors + data.warnings + data.infos + data.hints + data.unassigned) / 5.0, // Total Diagnostics
147-
data.response_length ?? 0,
147+
data.responseLength ?? 0,
148148
data.edit_distance,
149-
data._runtime ?? 0
149+
data.runtime ?? 0
150150
];
151151

152152
const n = values.length;

packages/langium-ai-mcp/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"watch": "tsc -w",
2323
"start": "node .",
2424
"cstart": "node ./dist/mcp-client.js",
25-
"prepare": "npm run build",
2625
"test": "vitest run"
2726
},
2827
"author": {
@@ -39,7 +38,8 @@
3938
"license": "MIT",
4039
"dependencies": {
4140
"@modelcontextprotocol/sdk": "^1.17.4",
42-
"langium-ai-tools": "0.0.2"
41+
"langium-ai-tools": "~4.1.2",
42+
"langium": "~4.1.0"
4343
},
4444
"volta": {
4545
"node": "20.10.0",

packages/langium-ai-tools/package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
}
3030
},
3131
"scripts": {
32-
"build": "tsc",
33-
"watch": "tsc -w",
32+
"build": "npm run generate && tsc && cp src/interface.proto dist/",
33+
"generate": "mkdir -p src/gen && protoc --ts_out=force_disable_services,force_optimize_code_size,eslint_disable:./src/gen --proto_path=src src/interface.proto",
34+
"watch": "npm run generate && tsc -w",
3435
"test": "vitest run",
35-
"clean": "rimraf ./dist"
36+
"clean": "rimraf ./dist ./src/gen"
3637
},
3738
"author": {
3839
"name": "TypeFox",
@@ -47,11 +48,14 @@
4748
"license": "MIT",
4849
"dependencies": {
4950
"langium": "~4.1.0",
50-
"levenshtein-edit-distance": "^3.0.1"
51+
"levenshtein-edit-distance": "^3.0.1",
52+
"@protobuf-ts/runtime": "^2.11.1"
53+
},
54+
"devDependencies": {
55+
"@protobuf-ts/plugin": "^2.11.1"
5156
},
5257
"volta": {
5358
"node": "20.10.0",
5459
"npm": "10.2.3"
55-
},
56-
"devDependencies": {}
57-
}
60+
}
61+
}

packages/langium-ai-tools/src/analyzer/document-analyzer.ts

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import { LangiumServices } from "langium/lsp";
1212
import { EvaluationContext } from "../evaluator/document-evaluator.js";
1313
import { EvaluatorResult } from "../evaluator/evaluator.js";
1414
import { LangiumEvaluator, LangiumEvaluatorResultData } from "../evaluator/langium-evaluator.js";
15+
import { EvaluatorResultMsg, SyntaxStatistic } from "../gen/interface.js";
16+
17+
1518

1619
/**
1720
* Extends LangiumEvaluator and adds analysis capabilities.
@@ -40,14 +43,40 @@ export class LangiumDocumentAnalyzer<T extends LangiumServices> extends LangiumE
4043
this.analysisOptions = { ...DEFAULT_OPTIONS, ...analysisOptions };
4144
}
4245

43-
evaluateDocument(doc: LangiumDocument, ctx: EvaluationContext): EvaluatorResult<LangiumEvaluatorResultData> {
46+
/**
47+
* Evaluates a Langium document.
48+
* Here we return protocol compatible object EvaluatorResultMsg.
49+
*
50+
* @param doc Langium document to evaluate
51+
* @param ctx Evaluation context
52+
* @returns Evaluation result with syntax statistics in metadata
53+
*/
54+
evaluateDocument(doc: LangiumDocument, ctx: EvaluationContext): EvaluatorResult<LangiumEvaluatorResultData> & EvaluatorResultMsg {
4455
const validationResult = super.evaluateDocument(doc, ctx);
45-
if (this.analysisOptions.analysisMode !== AnalysisMode.NO_STATISTIC && validationResult.data.failures === 0) {
56+
if (this.analysisOptions.analysisMode !== AnalysisMode.NO_STATISTIC && validationResult.data && validationResult.data.failures === 0) {
4657
// Add syntax usage statistics only if build was successful
4758
const statistics = this.collectSyntaxUsageStatistics(doc, this.services.Grammar);
48-
validationResult.metadata[LangiumDocumentAnalyzer.METADATA_KEY] = statistics;
59+
validationResult.metadata[LangiumDocumentAnalyzer.METADATA_KEY] = {
60+
value: {
61+
oneofKind: 'syntaxStatisticValue',
62+
syntaxStatisticValue: statistics
63+
}
64+
};
4965
}
50-
return validationResult;
66+
// make sure we fulfill the EvaluatorResultMsg interface
67+
return {
68+
...validationResult,
69+
data: {
70+
...validationResult.data,
71+
diagnostics: validationResult.data.diagnostics.map(diagnostic => {
72+
const code = typeof diagnostic.code === 'number' ? String(diagnostic.code) : diagnostic.code;
73+
return {
74+
...diagnostic,
75+
code
76+
};
77+
})
78+
}
79+
};
5180
}
5281

5382
collectSyntaxUsageStatistics(doc: LangiumDocument, grammar: Grammar): SyntaxStatistic {
@@ -173,7 +202,11 @@ export class LangiumDocumentAnalyzer<T extends LangiumServices> extends LangiumE
173202
extractStatisticsFromResult(result: Partial<EvaluatorResult> | undefined): SyntaxStatistic | undefined {
174203
const metadata = result?.metadata;
175204
if (metadata && metadata[LangiumDocumentAnalyzer.METADATA_KEY]) {
176-
return metadata[LangiumDocumentAnalyzer.METADATA_KEY] as SyntaxStatistic;
205+
const value = metadata[LangiumDocumentAnalyzer.METADATA_KEY].value;
206+
if (value.oneofKind === 'syntaxStatisticValue') {
207+
return value.syntaxStatisticValue;
208+
}
209+
return undefined;
177210
}
178211
return undefined;
179212
}
@@ -240,42 +273,3 @@ const DEFAULT_OPTIONS: AnalysisOptions = {
240273
computeDiversity: true
241274
};
242275

243-
/**
244-
* Type representing syntax usage statistics.
245-
*/
246-
export type SyntaxStatistic = {
247-
/** Map of rule names to their usage counts */
248-
ruleUsage: Record<string, number>;
249-
250-
/** Percentage of used rules compared to all available rules */
251-
coverage: number;
252-
253-
/** Diversity metrics for rule usage patterns */
254-
diversity: {
255-
256-
/**
257-
* Shannon entropy - information diversity measure.
258-
* **Range:** 0 to log₂(n) where n = number of rules.
259-
* - **Low (0-1):** dominated by few rules
260-
* - **Medium (1-3):** moderate diversity
261-
* - **High (>3):** high diversity
262-
*/
263-
entropy: number;
264-
265-
/**
266-
* Gini coefficient - inequality measure. Range: 0 to 1.
267-
* - **Low (0-0.3):** equal distribution
268-
* - **Medium (0.3-0.7):** moderate inequality
269-
* - **High (0.7-1):** high inequality
270-
*/
271-
giniCoefficient: number;
272-
273-
/**
274-
* Simpson's diversity index - probability that two randomly selected items are different. **Range:** 0 to 1.
275-
* - **Low (0-0.3):** low diversity
276-
* - **Medium (0.3-0.7):** moderate diversity
277-
* - **High (0.7-1):** high diversity
278-
*/
279-
simpsonIndex: number;
280-
};
281-
}

packages/langium-ai-tools/src/evaluator/eval-matrix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class EvalMatrix {
107107
result.name = `${runner.name} - ${testCase.name} - ${evaluator.name}`;
108108
}
109109
// add runtime there too, so we have access to it
110-
result.data!._runtime = (runnerEndTime.getTime() - runnerStartTime.getTime()) / 1000.0; // in seconds
110+
result.data!.runtime = (runnerEndTime.getTime() - runnerStartTime.getTime()) / 1000.0; // in seconds
111111

112112
result.metadata = {
113113
runner: runner.name,

packages/langium-ai-tools/src/evaluator/evaluator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { readFileSync, existsSync, readdirSync } from 'fs';
1212
import * as path from 'path';
1313

1414
export type EvaluatorResultData = Record<string, unknown> & {
15-
_runtime?: number;
15+
runtime?: number;
1616
};
1717

1818
/**

packages/langium-ai-tools/src/evaluator/langium-evaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface LangiumEvaluatorResultData extends FailureAwarenessData {
4747
/**
4848
* Length of the response in chars
4949
*/
50-
response_length: number;
50+
responseLength: number;
5151

5252
/**
5353
* Raw diagnostic data, same which is used to compute the other values above
@@ -109,7 +109,7 @@ export class LangiumEvaluator<T extends LangiumServices> extends AbstractDocumen
109109
infos: 0,
110110
hints: 0,
111111
unassigned: 0,
112-
response_length: 0,
112+
responseLength: 0,
113113
diagnostics: []
114114
};
115115
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Generated protobuf files
2+
*.ts

0 commit comments

Comments
 (0)