@unieai/rabi-util-code-language 0.0.0-stage → 0.2.1-rc.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 DeepSeek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,8 @@
1
- # Temporary Holding Version
1
+ # @unieai/rabi-util-code-language
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Single file-extension to syntax-highlighting language table shared by Client code surfaces and the Host read card
4
+
5
+ Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
6
+ not meant to be used on its own.
7
+
8
+ See LICENSE for the license and copyright of this package.
package/lib/index.js ADDED
@@ -0,0 +1,269 @@
1
+ //#region lib/types/index.js
2
+ /**
3
+ * Lowercase extension to canonical language id. A Map, not an object: a filename
4
+ * whose extension is an `Object.prototype` key (`foo.constructor`,
5
+ * `foo.__proto__`) must miss instead of resolving the inherited member, which
6
+ * would otherwise reach callers as a non-string language value.
7
+ */
8
+ const LANGUAGES = new Map(Object.entries({
9
+ typescript: [
10
+ "ts",
11
+ "tsx",
12
+ "mts",
13
+ "cts"
14
+ ],
15
+ javascript: [
16
+ "js",
17
+ "jsx",
18
+ "mjs",
19
+ "cjs"
20
+ ],
21
+ shellscript: [
22
+ "sh",
23
+ "bash",
24
+ "zsh"
25
+ ],
26
+ fish: ["fish"],
27
+ json: [
28
+ "json",
29
+ "jsonc",
30
+ "jsonl",
31
+ "ndjson",
32
+ "ipynb"
33
+ ],
34
+ csv: ["csv"],
35
+ python: [
36
+ "py",
37
+ "pyw",
38
+ "pyi"
39
+ ],
40
+ ruby: [
41
+ "rb",
42
+ "rake",
43
+ "gemspec"
44
+ ],
45
+ go: ["go"],
46
+ rust: ["rs"],
47
+ java: ["java"],
48
+ c: ["c", "h"],
49
+ cpp: [
50
+ "cc",
51
+ "cpp",
52
+ "cxx",
53
+ "hh",
54
+ "hpp",
55
+ "hxx"
56
+ ],
57
+ csharp: ["cs"],
58
+ kotlin: ["kt", "kts"],
59
+ swift: ["swift"],
60
+ php: ["php"],
61
+ yaml: ["yaml", "yml"],
62
+ toml: ["toml"],
63
+ ini: [
64
+ "ini",
65
+ "conf",
66
+ "cfg",
67
+ "properties"
68
+ ],
69
+ dotenv: ["env"],
70
+ log: ["log"],
71
+ diff: ["diff", "patch"],
72
+ http: ["http"],
73
+ markdown: ["md", "markdown"],
74
+ mdx: ["mdx"],
75
+ rst: ["rst"],
76
+ latex: [
77
+ "tex",
78
+ "sty",
79
+ "cls"
80
+ ],
81
+ bibtex: ["bib"],
82
+ asciidoc: ["adoc"],
83
+ html: [
84
+ "html",
85
+ "htm",
86
+ "xhtml"
87
+ ],
88
+ css: ["css"],
89
+ scss: ["scss"],
90
+ less: ["less"],
91
+ sql: ["sql"],
92
+ xml: [
93
+ "xml",
94
+ "xsd",
95
+ "xsl",
96
+ "xslt",
97
+ "plist",
98
+ "svg"
99
+ ],
100
+ lua: ["lua"],
101
+ bat: ["bat", "cmd"],
102
+ powershell: [
103
+ "ps1",
104
+ "psm1",
105
+ "psd1"
106
+ ],
107
+ r: ["r"],
108
+ julia: ["jl"],
109
+ dart: ["dart"],
110
+ scala: ["scala"],
111
+ clojure: [
112
+ "clj",
113
+ "cljs",
114
+ "edn"
115
+ ],
116
+ erlang: ["erl", "hrl"],
117
+ elixir: ["ex", "exs"],
118
+ haskell: ["hs"],
119
+ fsharp: [
120
+ "fs",
121
+ "fsi",
122
+ "fsx"
123
+ ],
124
+ vb: ["vb"],
125
+ perl: ["pl", "pm"],
126
+ verilog: ["v"],
127
+ "system-verilog": ["sv", "svh"],
128
+ graphql: ["graphql", "gql"],
129
+ proto: ["proto"],
130
+ hcl: [
131
+ "tf",
132
+ "tfvars",
133
+ "hcl"
134
+ ],
135
+ nix: ["nix"],
136
+ vue: ["vue"],
137
+ svelte: ["svelte"],
138
+ make: ["makefile", "mk"],
139
+ cmake: ["cmake"],
140
+ groovy: ["gradle", "groovy"]
141
+ }).flatMap(([language, extensions]) => extensions.map((extension) => [extension, language])));
142
+ /** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
143
+ const CODE_HIGHLIGHT_EXTENSIONS = [...LANGUAGES.keys()];
144
+ /**
145
+ * Persisted `lang` values a language-level short id cannot express, keyed by the
146
+ * suffix that produced them. Everything else falls back to
147
+ * {@link SHORT_BY_LANGUAGE}, so only a suffix whose own name is the better label
148
+ * appears here. Each value is the exact string recorded sessions already hold
149
+ * and must not change; the byte-level expectation for every suffix lives in the
150
+ * read consumer's test.
151
+ */
152
+ const READ_LANG_BY_EXTENSION = new Map([
153
+ ["tsx", "tsx"],
154
+ ["jsx", "jsx"],
155
+ ["tf", "tf"],
156
+ ["tfvars", "tfvars"],
157
+ ["gradle", "gradle"]
158
+ ]);
159
+ /**
160
+ * The short `lang` id the read card persists, keyed by canonical language id.
161
+ * Keys are exactly the languages in {@link LANGUAGE_EXTENSIONS}: where
162
+ * {@link READ_LANG_BY_EXTENSION} does not already fix a suffix's value, this
163
+ * table supplies the language's short name (`powershell` to `ps1`, `hcl` to
164
+ * `tf`, `system-verilog` to `sv`) rather than the grammar id. Keys are canonical
165
+ * ids, not untrusted extensions, so an object lookup cannot reach an
166
+ * `Object.prototype` member the way the extension tables can.
167
+ */
168
+ const SHORT_BY_LANGUAGE = {
169
+ typescript: "ts",
170
+ javascript: "js",
171
+ shellscript: "sh",
172
+ fish: "fish",
173
+ json: "json",
174
+ csv: "csv",
175
+ python: "py",
176
+ ruby: "rb",
177
+ go: "go",
178
+ rust: "rs",
179
+ java: "java",
180
+ c: "c",
181
+ cpp: "cpp",
182
+ csharp: "cs",
183
+ kotlin: "kotlin",
184
+ swift: "swift",
185
+ php: "php",
186
+ yaml: "yaml",
187
+ toml: "toml",
188
+ ini: "ini",
189
+ dotenv: "env",
190
+ log: "log",
191
+ diff: "diff",
192
+ http: "http",
193
+ markdown: "md",
194
+ mdx: "mdx",
195
+ rst: "rst",
196
+ latex: "tex",
197
+ bibtex: "bib",
198
+ asciidoc: "adoc",
199
+ html: "html",
200
+ css: "css",
201
+ scss: "scss",
202
+ less: "less",
203
+ sql: "sql",
204
+ xml: "xml",
205
+ lua: "lua",
206
+ bat: "bat",
207
+ powershell: "ps1",
208
+ r: "r",
209
+ julia: "jl",
210
+ dart: "dart",
211
+ scala: "scala",
212
+ clojure: "clj",
213
+ erlang: "erl",
214
+ elixir: "ex",
215
+ haskell: "hs",
216
+ fsharp: "fs",
217
+ vb: "vb",
218
+ perl: "pl",
219
+ verilog: "v",
220
+ "system-verilog": "sv",
221
+ graphql: "graphql",
222
+ proto: "proto",
223
+ hcl: "hcl",
224
+ nix: "nix",
225
+ vue: "vue",
226
+ svelte: "svelte",
227
+ make: "make",
228
+ cmake: "cmake",
229
+ groovy: "groovy"
230
+ };
231
+ function extensionForPath(path) {
232
+ const slash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
233
+ const base = path.slice(slash + 1);
234
+ const dot = base.lastIndexOf(".");
235
+ return dot < 0 ? void 0 : base.slice(dot + 1).toLowerCase();
236
+ }
237
+ /**
238
+ * Derive the syntax-highlighting language from a filename or path, case
239
+ * insensitively. The extension is the text after the last dot of the final path
240
+ * segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
241
+ * while an unlisted dotfile (`.gitignore`) and an extensionless name return
242
+ * `undefined`. Both path separators are recognized, so
243
+ * `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
244
+ * @param path - decoded filename or path.
245
+ * @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
246
+ */
247
+ function languageForPath(path) {
248
+ const extension = extensionForPath(path);
249
+ return extension === void 0 ? void 0 : LANGUAGES.get(extension);
250
+ }
251
+ /**
252
+ * Derive the Host read card's persisted `lang` hint from a read path's
253
+ * extension. The value is the language's short name; for some languages that name
254
+ * is also the grammar id (`kotlin`, `swift`, `java`, `yaml`, `json`), and
255
+ * {@link READ_LANG_BY_EXTENSION} overrides it where the suffix names itself
256
+ * better (`tsx`, `tf`, `gradle`). A suffix a recorded session already holds keeps
257
+ * its persisted value. An unrecognized suffix stays `undefined`, so the card
258
+ * renders as plain text.
259
+ * @param path - the model-facing path the read reported.
260
+ * @returns the persisted language hint, or `undefined` when the extension maps to none.
261
+ */
262
+ function readLangHintForPath(path) {
263
+ const extension = extensionForPath(path);
264
+ if (extension === void 0) return void 0;
265
+ const canonical = LANGUAGES.get(extension);
266
+ return canonical === void 0 ? void 0 : READ_LANG_BY_EXTENSION.get(extension) ?? SHORT_BY_LANGUAGE[canonical];
267
+ }
268
+ //#endregion
269
+ export { CODE_HIGHLIGHT_EXTENSIONS, languageForPath, readLangHintForPath };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The single file-extension to syntax-highlighting language table shared by every
3
+ * code surface: the Client's document Code preview and diff review, and the Host
4
+ * read tool's persisted `lang` hint. Language ids are the grammar ids the Client
5
+ * highlighter's alias table resolves, so a language returned here reaches
6
+ * `highlightLines`/`highlightToHtml` unchanged; a filename outside the table, or
7
+ * one naming a language the highlighter does not register, renders as plain text.
8
+ * The read tool persists a short `lang` id, so {@link readLangHintForPath}
9
+ * projects short ids over this table: a suffix keeps the value a recorded session
10
+ * already holds, and every other suffix persists the language's short name —
11
+ * which for `kotlin`, `swift`, `yaml`, `json`, and similar equals that
12
+ * language's grammar id.
13
+ * @module @unieai/rabi-util-code-language
14
+ */
15
+ /** Every recognized filename suffix, one entry each; a document-preview registry uses it to claim Code-rendered bodies. */
16
+ export declare const CODE_HIGHLIGHT_EXTENSIONS: readonly string[];
17
+ /**
18
+ * Derive the syntax-highlighting language from a filename or path, case
19
+ * insensitively. The extension is the text after the last dot of the final path
20
+ * segment; a leading dot is still the separator, so `.env` resolves to `dotenv`
21
+ * while an unlisted dotfile (`.gitignore`) and an extensionless name return
22
+ * `undefined`. Both path separators are recognized, so
23
+ * `C:\\dir\\main.PS1` resolves like `dir/main.ps1`.
24
+ * @param path - decoded filename or path.
25
+ * @returns the canonical language id, or `undefined` for an unrecognized or absent suffix.
26
+ */
27
+ export declare function languageForPath(path: string): string | undefined;
28
+ /**
29
+ * Derive the Host read card's persisted `lang` hint from a read path's
30
+ * extension. The value is the language's short name; for some languages that name
31
+ * is also the grammar id (`kotlin`, `swift`, `java`, `yaml`, `json`), and
32
+ * {@link READ_LANG_BY_EXTENSION} overrides it where the suffix names itself
33
+ * better (`tsx`, `tf`, `gradle`). A suffix a recorded session already holds keeps
34
+ * its persisted value. An unrecognized suffix stays `undefined`, so the card
35
+ * renders as plain text.
36
+ * @param path - the model-facing path the read reported.
37
+ * @returns the persisted language hint, or `undefined` when the extension maps to none.
38
+ */
39
+ export declare function readLangHintForPath(path: string): string | undefined;
40
+ //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@unieai/rabi-util-code-language",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "description": "Single file-extension to syntax-highlighting language table shared by Client code surfaces and the Host read card",
4
+ "version": "0.2.1-rc.3",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "type": "module",
9
+ "main": "lib/index.js",
10
+ "types": "lib/types/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./lib/types/index.d.ts",
14
+ "default": "./lib/index.js"
15
+ },
16
+ "./src/*": "./src/*",
17
+ "./package.json": "./package.json"
18
+ },
19
+ "files": [
20
+ "lib/index.js",
21
+ "lib/types/**/*.d.ts",
22
+ "README.md"
23
+ ],
24
+ "license": "MIT",
25
+ "peerDependencies": {
26
+ "@unieai/cordis": "4.0.5"
27
+ }
28
+ }