write-language 0.1.296 → 0.1.297

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/src/index.ts CHANGED
@@ -1,42 +1,42 @@
1
- /**
2
- * @fileoverview Language Generation Module
3
- *
4
- * Centralized exports for AI language model response generation using Vercel AI SDK.
5
- * Provides prompt templates, model registries, provider factories, and response
6
- * generation functions for 10+ LLM providers.
7
- *
8
- * @module language-generation
9
- * @author ai-research-agent contributors
10
- */
11
-
12
- export {
13
- writeLanguageResponse,
14
- convertMarkdownToHTMLEscaped,
15
- } from "./generate-response";
16
-
17
- export type {
18
- LLMProviderName,
19
- GenerateLanguageOptions,
20
- GenerateLanguageResult,
21
- LanguageAttachment,
22
- } from "./generate-response";
23
-
24
- export type {
25
- AgentPrompt,
26
- AgentTool,
27
- } from "./generation-types";
28
-
29
- export { AGENT_PROMPTS, extractJSONFromLanguageReply } from "./prompt-templates";
30
- export {
31
- LANGUAGE_MODELS,
32
- LANGUAGE_PROVIDERS,
33
- getModelsByProvider,
34
- getAllModels,
35
- getModelsByCapability,
36
- getTextOnlyModels,
37
- getMultimodalModels,
38
- } from "./language-model-registry";
39
- export type { ModelCapability, ModelInfo } from "./language-model-registry";
40
- export { createLLMProvider } from "./provider-factory";
41
- export { convertMarkdownToHTMLEscaped as markdownToHTML } from "./utils/markdown-to-html";
42
- export { highlightCode, Prism } from "./utils/prism";
1
+ /**
2
+ * @fileoverview Language Generation Module
3
+ *
4
+ * Centralized exports for AI language model response generation using Vercel AI SDK.
5
+ * Provides prompt templates, model registries, provider factories, and response
6
+ * generation functions for 10+ LLM providers.
7
+ *
8
+ * @module language-generation
9
+ * @author ai-research-agent contributors
10
+ */
11
+
12
+ export {
13
+ writeLanguageResponse,
14
+ convertMarkdownToHTMLEscaped,
15
+ } from "./generate-response";
16
+
17
+ export type {
18
+ LLMProviderName,
19
+ GenerateLanguageOptions,
20
+ GenerateLanguageResult,
21
+ LanguageAttachment,
22
+ } from "./generate-response";
23
+
24
+ export type {
25
+ AgentPrompt,
26
+ AgentTool,
27
+ } from "./generation-types";
28
+
29
+ export { AGENT_PROMPTS, extractJSONFromLanguageReply } from "./prompt-templates";
30
+ export {
31
+ LANGUAGE_MODELS,
32
+ LANGUAGE_PROVIDERS,
33
+ getModelsByProvider,
34
+ getAllModels,
35
+ getModelsByCapability,
36
+ getTextOnlyModels,
37
+ getMultimodalModels,
38
+ } from "./language-model-registry";
39
+ export type { ModelCapability, ModelInfo } from "./language-model-registry";
40
+ export { createLLMProvider } from "./provider-factory";
41
+ export { convertMarkdownToHTMLEscaped as markdownToHTML } from "./utils/markdown-to-html";
42
+ export { highlightCode, hljs } from "./utils/highlight";
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @fileoverview Syntax highlighting for fenced code blocks, on highlight.js'
3
+ * core build with an explicit set of languages.
4
+ *
5
+ * Every language below is an ordinary ES module import, registered
6
+ * synchronously when this module loads, so there is no global to publish and
7
+ * no load order to protect. Only the core and the listed grammars are bundled,
8
+ * not highlight.js' full set of ~190 languages, which keeps the Worker bundles
9
+ * that pull this package in small.
10
+ */
11
+ import hljs from "highlight.js/lib/core";
12
+ import bash from "highlight.js/lib/languages/bash";
13
+ import c from "highlight.js/lib/languages/c";
14
+ import cpp from "highlight.js/lib/languages/cpp";
15
+ import css from "highlight.js/lib/languages/css";
16
+ import go from "highlight.js/lib/languages/go";
17
+ import java from "highlight.js/lib/languages/java";
18
+ import javascript from "highlight.js/lib/languages/javascript";
19
+ import json from "highlight.js/lib/languages/json";
20
+ import markdown from "highlight.js/lib/languages/markdown";
21
+ import python from "highlight.js/lib/languages/python";
22
+ import rust from "highlight.js/lib/languages/rust";
23
+ import sql from "highlight.js/lib/languages/sql";
24
+ import typescript from "highlight.js/lib/languages/typescript";
25
+ import xml from "highlight.js/lib/languages/xml";
26
+ import yaml from "highlight.js/lib/languages/yaml";
27
+
28
+ hljs.registerLanguage("bash", bash);
29
+ hljs.registerLanguage("c", c);
30
+ hljs.registerLanguage("cpp", cpp);
31
+ hljs.registerLanguage("css", css);
32
+ hljs.registerLanguage("go", go);
33
+ hljs.registerLanguage("java", java);
34
+ hljs.registerLanguage("javascript", javascript);
35
+ hljs.registerLanguage("json", json);
36
+ hljs.registerLanguage("markdown", markdown);
37
+ hljs.registerLanguage("python", python);
38
+ hljs.registerLanguage("rust", rust);
39
+ hljs.registerLanguage("sql", sql);
40
+ hljs.registerLanguage("typescript", typescript);
41
+ hljs.registerLanguage("xml", xml);
42
+ hljs.registerLanguage("yaml", yaml);
43
+ // highlight.js' JavaScript and TypeScript grammars already understand JSX.
44
+ hljs.registerAliases(["jsx"], { languageName: "javascript" });
45
+ hljs.registerAliases(["tsx"], { languageName: "typescript" });
46
+ hljs.registerAliases(["markup"], { languageName: "xml" });
47
+
48
+ /**
49
+ * Highlights `code` as `lang`, returning HTML made of `hljs-*` spans, or
50
+ * `null` when `lang` is not a registered language or alias — callers render
51
+ * that block escaped and unhighlighted.
52
+ */
53
+ export function highlightCode(code: string, lang: string): string | null {
54
+ if (!lang || !hljs.getLanguage(lang)) return null;
55
+ return hljs.highlight(code, { language: lang, ignoreIllegals: true }).value;
56
+ }
57
+
58
+ export { hljs };
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * @module research/agents/markdown-to-html
3
- * @description Converts Markdown to HTML with Prism.js syntax highlighting.
3
+ * @description Converts Markdown to HTML with highlight.js syntax highlighting.
4
4
  */
5
5
  import { marked } from "marked";
6
- import { highlightCode } from "./prism";
6
+ import { highlightCode } from "./highlight";
7
7
  import { encode, decode } from "html-entities";
8
8
 
9
9
  // Inline onclick: self-contained per button, works in dangerouslySetInnerHTML contexts
@@ -40,7 +40,7 @@ marked.use({
40
40
  });
41
41
 
42
42
  /**
43
- * Convert markdown text to HTML with Prism.js syntax highlighting.
43
+ * Convert markdown text to HTML with highlight.js syntax highlighting.
44
44
  * Unescapes HTML entities like `&` → `&`.
45
45
  */
46
46
  export async function convertMarkdownToHTMLEscaped(
@@ -1,12 +0,0 @@
1
- import { default as Prism } from 'prismjs';
2
- /**
3
- * Registers the grammars above, once per runtime. Cheap to call repeatedly —
4
- * callers are meant to invoke it wherever they are about to highlight, so that
5
- * the work does not hinge on a module-level statement surviving tree-shaking.
6
- *
7
- * Never rejects: a grammar that fails to load just leaves its language out of
8
- * `Prism.languages`, which every caller already treats as "render this block
9
- * unhighlighted".
10
- */
11
- export declare function loadPrismGrammars(): Promise<void>;
12
- export default Prism;
@@ -1,3 +0,0 @@
1
- import { default as Prism } from './prism-global';
2
- export declare function highlightCode(code: string, lang: string): string | null;
3
- export { Prism };
@@ -1,81 +0,0 @@
1
- /**
2
- * @fileoverview Owns the Prism instance: publishes it on `globalThis`, then
3
- * registers the language grammars that prismjs' own entry point leaves out —
4
- * without ever depending on module evaluation order.
5
- *
6
- * The `prismjs/components/*` files are plain browser scripts, not modules:
7
- * `prism-markup.js` literally opens with `Prism.languages.markup = {...}`,
8
- * resolving `Prism` as a free variable off the global object. Someone has to
9
- * put it there first. prismjs' own entry point does that for `window`
10
- * (browser) and `global` (Node); on Cloudflare Workers / edge runtimes it sees
11
- * neither, which is what the assignment below is for.
12
- *
13
- * Publishing it is only half the job, though: it has to happen *before* the
14
- * grammar scripts run, and a static `import "prismjs/components/..."` cannot
15
- * promise that once a bundler is in the loop. Those files declare no
16
- * dependency on Prism — reading a global is invisible to the module graph — so
17
- * nothing pins them after whoever publishes it, and a bundler is free to hoist
18
- * them, split them into another chunk, or drop the publishing statement as a
19
- * dead `globalThis` write. When that happens the chunk dies on load with
20
- * `ReferenceError: Prism is not defined`, which takes down the whole route
21
- * that lazily imported it rather than just the syntax highlighting.
22
- *
23
- * So the grammars are loaded with `import()` from inside `loadPrismGrammars()`
24
- * instead. The global is published by a statement earlier in that same
25
- * function body, which makes the ordering a runtime fact rather than a promise
26
- * the bundler has to keep.
27
- */
28
- import Prism from "prismjs";
29
-
30
- /**
31
- * The grammars to register, in order — several extend an earlier one, so this
32
- * list is loaded sequentially rather than in parallel (`tsx` needs `jsx` and
33
- * `typescript`; `cpp` needs `c`). Markup, CSS, C-like and JavaScript are
34
- * absent because prismjs' entry point already bundles them.
35
- */
36
- const GRAMMARS: ReadonlyArray<() => Promise<unknown>> = [
37
- () => import("prismjs/components/prism-typescript.js"),
38
- () => import("prismjs/components/prism-jsx.js"),
39
- () => import("prismjs/components/prism-tsx.js"),
40
- () => import("prismjs/components/prism-python.js"),
41
- () => import("prismjs/components/prism-bash.js"),
42
- () => import("prismjs/components/prism-json.js"),
43
- () => import("prismjs/components/prism-yaml.js"),
44
- () => import("prismjs/components/prism-markdown.js"),
45
- () => import("prismjs/components/prism-sql.js"),
46
- () => import("prismjs/components/prism-rust.js"),
47
- () => import("prismjs/components/prism-go.js"),
48
- () => import("prismjs/components/prism-java.js"),
49
- () => import("prismjs/components/prism-c.js"),
50
- () => import("prismjs/components/prism-cpp.js"),
51
- ];
52
-
53
- /** Puts Prism where the grammar scripts look for it. Idempotent. */
54
- function publishPrismGlobal(): void {
55
- (globalThis as typeof globalThis & { Prism?: typeof Prism }).Prism ??= Prism;
56
- }
57
-
58
- publishPrismGlobal();
59
-
60
- let loading: Promise<void> | undefined;
61
-
62
- /**
63
- * Registers the grammars above, once per runtime. Cheap to call repeatedly —
64
- * callers are meant to invoke it wherever they are about to highlight, so that
65
- * the work does not hinge on a module-level statement surviving tree-shaking.
66
- *
67
- * Never rejects: a grammar that fails to load just leaves its language out of
68
- * `Prism.languages`, which every caller already treats as "render this block
69
- * unhighlighted".
70
- */
71
- export function loadPrismGrammars(): Promise<void> {
72
- loading ??= (async () => {
73
- publishPrismGlobal();
74
- for (const load of GRAMMARS) {
75
- await load();
76
- }
77
- })().catch(() => {});
78
- return loading;
79
- }
80
-
81
- export default Prism;
@@ -1,20 +0,0 @@
1
- /**
2
- * @fileoverview Exposes a small `highlightCode` helper used by
3
- * markdown-to-html.ts for code-block syntax highlighting. The Prism instance
4
- * and its language grammars are owned by `prism-global.ts`.
5
- */
6
- import Prism, { loadPrismGrammars } from "./prism-global";
7
-
8
- // Start the grammars loading now, and again from `highlightCode` below — the
9
- // second call is what guarantees they are requested at all, since a bundler is
10
- // free to drop this one as a side effect of a module it thinks is pure.
11
- void loadPrismGrammars();
12
-
13
- export function highlightCode(code: string, lang: string): string | null {
14
- void loadPrismGrammars();
15
- const grammar = Prism.languages[lang];
16
- if (!grammar) return null;
17
- return Prism.highlight(code, grammar, lang);
18
- }
19
-
20
- export { Prism };