@gitpagedocs/tools 1.1.66 → 1.1.68

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/README.md CHANGED
@@ -15,11 +15,12 @@ The **shared business-logic core** of the [Git Page Docs](../README.md) monorepo
15
15
  | `errors/` | `AppError` + typed subclasses |
16
16
  | `filesystem/` | `FileService` (root-bounded list/read/write/search) |
17
17
  | `documentation/` | `DocumentationService`, `patchManagedRegion`, `DocUpdater` |
18
+ | `i18n/` | Language bundles (`gitpagedocs/langs.json` + `langs/<lang>.json`): `loadLanguageBundles`, `applyLanguageBundles` (fold into legacy `site.langmenu` / `translations`), `splitLanguageBundles` (generator side); browser-safe |
18
19
  | `ports/` | Type-only contracts (logger, cache, crypto, security, config, ai) |
19
20
 
20
21
  ## Entry points (`exports`)
21
22
 
22
- `.` (full barrel) · `./ai` · `./errors` · `./ports` · `./crypto/web` · `./security/web` · `./cache/web` — the `web` subpaths are browser-safe (no `node:` imports) for the Next.js bundle.
23
+ `.` (full barrel) · `./ai` · `./errors` · `./i18n` · `./ports` · `./crypto/web` · `./security/web` · `./cache/web` — the `web` subpaths and `./i18n` are browser-safe (no `node:` imports) for the Next.js bundle.
23
24
 
24
25
  ## Usage
25
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitpagedocs/tools",
3
- "version": "1.1.66",
3
+ "version": "1.1.68",
4
4
  "type": "module",
5
5
  "description": "Shared business-logic core for Git Page Docs (consumed by frontend, cli and mcp). Ships TypeScript source; consume via tsx or a TS-aware bundler.",
6
6
  "main": "./src/index.ts",
@@ -9,6 +9,7 @@
9
9
  ".": "./src/index.ts",
10
10
  "./ai": "./src/ai/index.ts",
11
11
  "./errors": "./src/errors/index.ts",
12
+ "./i18n": "./src/i18n/index.ts",
12
13
  "./ports": "./src/ports/index.ts",
13
14
  "./crypto/web": "./src/crypto/web.ts",
14
15
  "./security/web": "./src/security/web.ts",
package/src/ai/catalog.ts CHANGED
@@ -25,6 +25,13 @@ export interface ProviderSpec {
25
25
  readonly auth: AuthStyle;
26
26
  readonly capabilities: ProviderCapabilities;
27
27
  readonly models: readonly ModelDescriptor[];
28
+ /**
29
+ * Environment variables that hold this provider's API key, in priority order.
30
+ * The single source for env-based credential detection; consumers read the
31
+ * first non-empty one instead of hardcoding names. Empty for keyless
32
+ * providers (Ollama).
33
+ */
34
+ readonly envVars: readonly string[];
28
35
  }
29
36
 
30
37
  const TEXT: ProviderCapabilities = { streaming: true, vision: false, audio: false };
@@ -38,6 +45,7 @@ function models(...ids: string[]): ModelDescriptor[] {
38
45
  export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
39
46
  openai: {
40
47
  id: "openai",
48
+ envVars: ["OPENAI_API_KEY"],
41
49
  label: "OpenAI",
42
50
  family: "openai-compatible",
43
51
  defaultModel: "gpt-4o-mini",
@@ -48,6 +56,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
48
56
  },
49
57
  anthropic: {
50
58
  id: "anthropic",
59
+ envVars: ["ANTHROPIC_API_KEY"],
51
60
  label: "Anthropic",
52
61
  family: "anthropic",
53
62
  // Latest Claude API model IDs.
@@ -59,6 +68,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
59
68
  },
60
69
  gemini: {
61
70
  id: "gemini",
71
+ envVars: ["GEMINI_API_KEY", "GOOGLE_API_KEY"],
62
72
  label: "Google Gemini",
63
73
  family: "gemini",
64
74
  defaultModel: "gemini-2.0-flash",
@@ -69,6 +79,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
69
79
  },
70
80
  openrouter: {
71
81
  id: "openrouter",
82
+ envVars: ["OPENROUTER_API_KEY"],
72
83
  label: "OpenRouter",
73
84
  family: "openai-compatible",
74
85
  defaultModel: "openai/gpt-4o-mini",
@@ -79,6 +90,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
79
90
  },
80
91
  ollama: {
81
92
  id: "ollama",
93
+ envVars: [],
82
94
  label: "Ollama (local)",
83
95
  family: "ollama",
84
96
  defaultModel: "llama3",
@@ -90,6 +102,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
90
102
  },
91
103
  "azure-openai": {
92
104
  id: "azure-openai",
105
+ envVars: ["AZURE_OPENAI_API_KEY"],
93
106
  label: "Azure OpenAI",
94
107
  family: "openai-compatible",
95
108
  defaultModel: "gpt-4o-mini",
@@ -101,6 +114,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
101
114
  },
102
115
  mistral: {
103
116
  id: "mistral",
117
+ envVars: ["MISTRAL_API_KEY"],
104
118
  label: "Mistral",
105
119
  family: "openai-compatible",
106
120
  defaultModel: "mistral-large-latest",
@@ -111,6 +125,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
111
125
  },
112
126
  deepseek: {
113
127
  id: "deepseek",
128
+ envVars: ["DEEPSEEK_API_KEY"],
114
129
  label: "DeepSeek",
115
130
  family: "openai-compatible",
116
131
  defaultModel: "deepseek-chat",
@@ -121,6 +136,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
121
136
  },
122
137
  cohere: {
123
138
  id: "cohere",
139
+ envVars: ["COHERE_API_KEY"],
124
140
  label: "Cohere",
125
141
  family: "cohere",
126
142
  defaultModel: "command-r-plus",
@@ -131,6 +147,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
131
147
  },
132
148
  groq: {
133
149
  id: "groq",
150
+ envVars: ["GROQ_API_KEY"],
134
151
  label: "Groq",
135
152
  family: "openai-compatible",
136
153
  defaultModel: "llama-3.3-70b-versatile",
@@ -141,6 +158,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
141
158
  },
142
159
  xai: {
143
160
  id: "xai",
161
+ envVars: ["XAI_API_KEY"],
144
162
  label: "xAI Grok",
145
163
  family: "openai-compatible",
146
164
  defaultModel: "grok-2-latest",
@@ -151,6 +169,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
151
169
  },
152
170
  together: {
153
171
  id: "together",
172
+ envVars: ["TOGETHER_API_KEY"],
154
173
  label: "Together AI",
155
174
  family: "openai-compatible",
156
175
  defaultModel: "meta-llama/Llama-3.3-70B-Instruct-Turbo",
@@ -161,6 +180,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
161
180
  },
162
181
  fireworks: {
163
182
  id: "fireworks",
183
+ envVars: ["FIREWORKS_API_KEY"],
164
184
  label: "Fireworks AI",
165
185
  family: "openai-compatible",
166
186
  defaultModel: "accounts/fireworks/models/llama-v3p3-70b-instruct",
@@ -171,6 +191,7 @@ export const PROVIDER_CATALOG: Readonly<Record<AiProviderId, ProviderSpec>> = {
171
191
  },
172
192
  perplexity: {
173
193
  id: "perplexity",
194
+ envVars: ["PERPLEXITY_API_KEY"],
174
195
  label: "Perplexity",
175
196
  family: "openai-compatible",
176
197
  defaultModel: "sonar",
@@ -0,0 +1,29 @@
1
+ import type { AiProviderId } from "../ports/ai";
2
+ import { PROVIDER_CATALOG } from "./catalog";
3
+
4
+ /** Environment lookup, injectable for tests; defaults to process.env. */
5
+ export type EnvLookup = Readonly<Record<string, string | undefined>>;
6
+
7
+ /**
8
+ * Resolves a provider's API key from the environment, reading the catalog's
9
+ * declared env vars in priority order (the single source — no hardcoded names
10
+ * at the call site). Returns undefined for keyless providers (Ollama) or when
11
+ * none is set.
12
+ */
13
+ export function resolveApiKeyFromEnv(
14
+ providerId: AiProviderId,
15
+ env: EnvLookup = process.env,
16
+ ): string | undefined {
17
+ for (const name of PROVIDER_CATALOG[providerId].envVars) {
18
+ const value = env[name]?.trim();
19
+ if (value) return value;
20
+ }
21
+ return undefined;
22
+ }
23
+
24
+ /** Provider ids that have at least one API key present in the environment. */
25
+ export function detectConfiguredProviders(env: EnvLookup = process.env): AiProviderId[] {
26
+ return (Object.keys(PROVIDER_CATALOG) as AiProviderId[]).filter((id) =>
27
+ PROVIDER_CATALOG[id].envVars.some((name) => Boolean(env[name]?.trim())),
28
+ );
29
+ }
package/src/ai/index.ts CHANGED
@@ -20,6 +20,11 @@ export { AnthropicProvider } from "./providers/anthropic-provider";
20
20
  export { GeminiProvider } from "./providers/gemini-provider";
21
21
  export { OllamaProvider } from "./providers/ollama-provider";
22
22
  export { CohereProvider } from "./providers/cohere-provider";
23
+ export {
24
+ resolveApiKeyFromEnv,
25
+ detectConfiguredProviders,
26
+ type EnvLookup,
27
+ } from "./env-credentials";
23
28
  export {
24
29
  legacyProviderToCatalogId,
25
30
  parseLegacyProviderAndModel,
@@ -0,0 +1,28 @@
1
+ export {
2
+ DEFAULT_LANGS_DIR,
3
+ DEFAULT_LANGS_MANIFEST_PATH,
4
+ LANGS_DIRNAME,
5
+ LANGS_MANIFEST_FILENAME,
6
+ applyLanguageBundles,
7
+ buildLanguageManifest,
8
+ getLanguageBundlePath,
9
+ isLanguageCode,
10
+ loadLanguageBundles,
11
+ parseLanguageBundle,
12
+ parseLanguageManifest,
13
+ splitLanguageBundles,
14
+ } from "./language-bundles";
15
+ export type {
16
+ InlineLangMenu,
17
+ InlineTranslationSection,
18
+ InlineTranslations,
19
+ JsonReader,
20
+ LanguageBundle,
21
+ LanguageBundleMap,
22
+ LanguageManifest,
23
+ LanguageStrings,
24
+ LoadLanguageBundlesOptions,
25
+ LocalizableConfig,
26
+ LocalizableSite,
27
+ TranslationSections,
28
+ } from "./language-bundles";
@@ -0,0 +1,204 @@
1
+ /**
2
+ * Language bundles — the UI strings of a gitpagedocs site, one JSON file per
3
+ * language, listed by a manifest:
4
+ *
5
+ * gitpagedocs/langs.json { "languages": ["en", "pt", "es"] }
6
+ * gitpagedocs/langs/<lang>.json { "langmenu": {...}, "translations": {...} }
7
+ *
8
+ * Historically these strings lived inline in `config.json` (`site.langmenu`
9
+ * keyed by language and `translations.<section>.<key>` keyed by language).
10
+ * `applyLanguageBundles` folds the bundles back into that legacy shape, so
11
+ * every consumer keeps reading `site.langmenu` / `translations` unchanged and
12
+ * old inline configs keep working. Bundles win over inline strings.
13
+ */
14
+
15
+ export const LANGS_MANIFEST_FILENAME = "langs.json";
16
+ export const LANGS_DIRNAME = "langs";
17
+ export const DEFAULT_LANGS_MANIFEST_PATH = `gitpagedocs/${LANGS_MANIFEST_FILENAME}`;
18
+ export const DEFAULT_LANGS_DIR = `gitpagedocs/${LANGS_DIRNAME}`;
19
+
20
+ /** Language codes become file names, so they are kept to a safe alphabet. */
21
+ const LANGUAGE_CODE_PATTERN = /^[A-Za-z0-9]{2,8}(?:[-_][A-Za-z0-9]{1,8})?$/;
22
+
23
+ export type LanguageStrings = Record<string, string>;
24
+ export type TranslationSections = Record<string, LanguageStrings>;
25
+
26
+ export interface LanguageManifest {
27
+ languages: string[];
28
+ }
29
+
30
+ export interface LanguageBundle {
31
+ langmenu?: LanguageStrings;
32
+ translations?: TranslationSections;
33
+ }
34
+
35
+ export type LanguageBundleMap = Record<string, LanguageBundle>;
36
+
37
+ /**
38
+ * Legacy inline shapes: `langmenu[lang][key]` and `translations[section][key][lang]`.
39
+ * Sections and keys may be absent, matching the optional fields of typed configs.
40
+ */
41
+ export type InlineLangMenu = Record<string, LanguageStrings>;
42
+ export type InlineTranslationSection = { [key: string]: LanguageStrings | undefined };
43
+ export type InlineTranslations = { [section: string]: InlineTranslationSection | undefined };
44
+
45
+ export interface LocalizableSite {
46
+ langmenu?: InlineLangMenu;
47
+ supportedLanguages?: string[];
48
+ }
49
+
50
+ export interface LocalizableConfig {
51
+ site?: LocalizableSite;
52
+ translations?: InlineTranslations;
53
+ }
54
+
55
+ /** Reads a repo-relative JSON file; resolves null when it is missing or invalid. */
56
+ export type JsonReader = (relativePath: string) => Promise<unknown>;
57
+
58
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
59
+ return typeof value === "object" && value !== null && !Array.isArray(value);
60
+ }
61
+
62
+ function isStringRecord(value: unknown): value is LanguageStrings {
63
+ return isPlainObject(value) && Object.values(value).every((entry) => typeof entry === "string");
64
+ }
65
+
66
+ export function isLanguageCode(value: unknown): value is string {
67
+ return typeof value === "string" && LANGUAGE_CODE_PATTERN.test(value);
68
+ }
69
+
70
+ export function getLanguageBundlePath(language: string, langsDir: string = DEFAULT_LANGS_DIR): string {
71
+ return `${langsDir}/${language}.json`;
72
+ }
73
+
74
+ export function parseLanguageManifest(raw: unknown): LanguageManifest | null {
75
+ if (!isPlainObject(raw) || !Array.isArray(raw.languages)) return null;
76
+ const languages = Array.from(new Set(raw.languages.filter(isLanguageCode)));
77
+ return languages.length > 0 ? { languages } : null;
78
+ }
79
+
80
+ function parseTranslationSections(raw: unknown): TranslationSections | undefined {
81
+ if (!isPlainObject(raw)) return undefined;
82
+ const sections: TranslationSections = {};
83
+ for (const [section, strings] of Object.entries(raw)) {
84
+ if (isStringRecord(strings)) sections[section] = strings;
85
+ }
86
+ return sections;
87
+ }
88
+
89
+ export function parseLanguageBundle(raw: unknown): LanguageBundle | null {
90
+ if (!isPlainObject(raw)) return null;
91
+ const bundle: LanguageBundle = {};
92
+ if (isStringRecord(raw.langmenu)) bundle.langmenu = raw.langmenu;
93
+ const translations = parseTranslationSections(raw.translations);
94
+ if (translations) bundle.translations = translations;
95
+ return bundle;
96
+ }
97
+
98
+ export interface LoadLanguageBundlesOptions {
99
+ manifestPath?: string;
100
+ langsDir?: string;
101
+ }
102
+
103
+ /**
104
+ * Loads the manifest and every bundle it lists. Resolves null when there is
105
+ * no manifest (legacy inline config); bundles that are missing or malformed
106
+ * are skipped so one broken file never takes the other languages down.
107
+ */
108
+ export async function loadLanguageBundles(
109
+ readJson: JsonReader,
110
+ options: LoadLanguageBundlesOptions = {},
111
+ ): Promise<LanguageBundleMap | null> {
112
+ const manifest = parseLanguageManifest(await readJson(options.manifestPath ?? DEFAULT_LANGS_MANIFEST_PATH));
113
+ if (!manifest) return null;
114
+
115
+ const entries = await Promise.all(
116
+ manifest.languages.map(async (language) => {
117
+ const bundle = parseLanguageBundle(await readJson(getLanguageBundlePath(language, options.langsDir)));
118
+ return [language, bundle] as const;
119
+ }),
120
+ );
121
+
122
+ const bundles: LanguageBundleMap = {};
123
+ for (const [language, bundle] of entries) {
124
+ if (bundle) bundles[language] = bundle;
125
+ }
126
+ return Object.keys(bundles).length > 0 ? bundles : null;
127
+ }
128
+
129
+ function mergeLangMenu(inline: InlineLangMenu | undefined, bundles: LanguageBundleMap): InlineLangMenu {
130
+ const merged: InlineLangMenu = { ...inline };
131
+ for (const [language, bundle] of Object.entries(bundles)) {
132
+ merged[language] = { ...inline?.[language], ...bundle.langmenu };
133
+ }
134
+ return merged;
135
+ }
136
+
137
+ function cloneTranslations(inline: InlineTranslations | undefined): Record<string, InlineTranslationSection> {
138
+ const cloned: Record<string, InlineTranslationSection> = {};
139
+ for (const [section, keys] of Object.entries(inline ?? {})) {
140
+ if (!keys) continue;
141
+ cloned[section] = {};
142
+ for (const [key, byLanguage] of Object.entries(keys)) {
143
+ if (byLanguage) cloned[section][key] = { ...byLanguage };
144
+ }
145
+ }
146
+ return cloned;
147
+ }
148
+
149
+ function mergeTranslations(inline: InlineTranslations | undefined, bundles: LanguageBundleMap): InlineTranslations {
150
+ const merged = cloneTranslations(inline);
151
+ for (const [language, bundle] of Object.entries(bundles)) {
152
+ for (const [section, strings] of Object.entries(bundle.translations ?? {})) {
153
+ const target = (merged[section] ??= {});
154
+ for (const [key, text] of Object.entries(strings)) {
155
+ target[key] = { ...target[key], [language]: text };
156
+ }
157
+ }
158
+ }
159
+ return merged;
160
+ }
161
+
162
+ /**
163
+ * Folds bundles into the legacy inline shape. The manifest order defines
164
+ * `site.supportedLanguages`; inline strings survive only for keys a bundle
165
+ * does not provide.
166
+ */
167
+ export function applyLanguageBundles<T extends LocalizableConfig>(config: T, bundles: LanguageBundleMap | null): T {
168
+ if (!bundles || Object.keys(bundles).length === 0) return config;
169
+ return {
170
+ ...config,
171
+ site: {
172
+ ...config.site,
173
+ langmenu: mergeLangMenu(config.site?.langmenu, bundles),
174
+ supportedLanguages: Object.keys(bundles),
175
+ },
176
+ translations: mergeTranslations(config.translations, bundles),
177
+ };
178
+ }
179
+
180
+ /** The generator-side inverse of `applyLanguageBundles`: one bundle per language. */
181
+ export function splitLanguageBundles(
182
+ languages: string[],
183
+ langmenu: InlineLangMenu,
184
+ translations: InlineTranslations,
185
+ ): LanguageBundleMap {
186
+ const bundles: LanguageBundleMap = {};
187
+ for (const language of languages) {
188
+ const sections: TranslationSections = {};
189
+ for (const [section, keys] of Object.entries(translations)) {
190
+ const strings: LanguageStrings = {};
191
+ for (const [key, byLanguage] of Object.entries(keys ?? {})) {
192
+ const text = byLanguage?.[language];
193
+ if (typeof text === "string") strings[key] = text;
194
+ }
195
+ if (Object.keys(strings).length > 0) sections[section] = strings;
196
+ }
197
+ bundles[language] = { langmenu: { ...langmenu[language] }, translations: sections };
198
+ }
199
+ return bundles;
200
+ }
201
+
202
+ export function buildLanguageManifest(languages: string[]): LanguageManifest {
203
+ return { languages: [...languages] };
204
+ }
package/src/index.ts CHANGED
@@ -16,6 +16,7 @@ export * from "./security";
16
16
  export * from "./config";
17
17
  export * from "./constants";
18
18
  export * from "./platform";
19
+ export * from "./i18n";
19
20
 
20
21
  // Phase 5 — shared AI system.
21
22
  export * from "./ai";