/** * Main API for creating multilingual DSLs */ import type { SemanticNode, LanguageTokenizer, LanguagePattern } from '../core/types'; import type { CommandSchema } from '../schema'; import type { PatternGenLanguageProfile } from '../generation/profile'; import type { DomainRenderFn } from '../generation/renderer'; import { type LanguageProfile as GrammarProfile } from '../grammar'; import { type Dictionary, type ProfileProvider, type ValueExtractor } from '../interfaces'; /** * Language configuration for a DSL. */ export interface LanguageConfig { /** ISO 639-1 language code */ readonly code: string; /** English name of the language */ readonly name: string; /** Native name of the language */ readonly nativeName: string; /** Language tokenizer */ readonly tokenizer: LanguageTokenizer; /** Language profile for pattern generation */ readonly patternProfile: PatternGenLanguageProfile; /** * Language profile for grammar transformation. * * Optional everywhere: `translate()` primarily works parse→render through * the schema-driven renderer, which needs no grammar profile. Configure this * only to enable the whole-string GrammarTransformer FALLBACK for input the * pattern matcher cannot parse (or actions render() cannot cover). */ readonly grammarProfile?: GrammarProfile; } /** * Code generator function that transforms semantic AST to target code. */ export interface CodeGenerator { generate(node: SemanticNode): string; } /** * Per-language vocabulary for a command added via {@link DomainExtension}. * * This is the same shape a language profile uses, narrowed to the parts a * single new command needs. */ export interface ExtensionVocabulary { /** The command's verb in this language. */ readonly keyword: PatternGenLanguageProfile['keywords'][string]; /** * Markers for roles this language does not already cover in its profile. * Usually unnecessary: role markers are shared across a domain's commands. */ readonly roleMarkers?: PatternGenLanguageProfile['roleMarkers']; } /** * A command added to a DSL from outside the package that defines it. * * Supplying a schema plus one vocabulary entry per language is enough to parse, * render and translate the command in every one of them — word order, role * markers and keyword placement all follow from the schema and the language * profiles. `render` and `generate` are only needed when the command's surface * form or output cannot be derived. * * @example * ```typescript * const research: DomainExtension = { * schema: defineCommand({ * action: 'research', * roles: [ * defineRole({ role: 'patient', required: true, expectedTypes: ['expression'] }), * defineRole({ * role: 'source', required: true, expectedTypes: ['expression'], * markerOverride: { en: 'from', ja: 'から', ar: 'من' }, * }), * ], * }), * vocabulary: { * en: { keyword: { primary: 'research' } }, * ja: { keyword: { primary: '調査' } }, * ar: { keyword: { primary: 'ابحث' } }, * }, * }; * * const llm = createLLMDSL({ extensions: [research] }); * llm.parse('research "climate" from #wiki', 'en'); * llm.render(node, 'ja'); // → '"climate" #wiki から 調査' * ``` */ export interface DomainExtension { /** Schema for the new command. Its action must not collide with an existing one. */ readonly schema: CommandSchema; /** * Vocabulary per language code. Codes must be configured on the DSL; an * unknown code is a configuration error, not a silent no-op. * * A subset is allowed — a command can be added in six of a DSL's eleven * languages. The languages left out simply do not parse it, so vocabulary * can be filled in over time rather than all at once. */ readonly vocabulary: Readonly>; /** Custom rendering for this action; takes precedence over the schema-driven path. */ readonly render?: DomainRenderFn; /** Custom code generation for this action; takes precedence over the DSL's generator. */ readonly generate?: (node: SemanticNode) => string; } /** * DSL configuration with dependency injection support. */ export interface DSLConfig { /** DSL name (for debugging/documentation) */ readonly name?: string; /** Command schemas defining the DSL grammar */ readonly schemas: readonly CommandSchema[]; /** Language configurations */ readonly languages: readonly LanguageConfig[]; /** Dictionary for keyword translation (default: built from language configs) */ readonly dictionary?: Dictionary; /** Profile provider for grammar transformation (default: built from language configs) */ readonly profileProvider?: ProfileProvider; /** Value extractors for tokenization (default: generic extractors) */ readonly valueExtractors?: ValueExtractor[]; /** Code generator for compilation (default: none) */ readonly codeGenerator?: CodeGenerator; /** * The domain's natural-language renderer, consulted by {@link MultilingualDSL.render}. * Actions it returns `null` for fall through to the schema-driven renderer. */ readonly renderer?: DomainRenderFn; /** * Commands added from outside this package. Their schemas and vocabulary are * merged into the DSL before pattern generation, so they parse, render and * compile like built-in commands. */ readonly extensions?: readonly DomainExtension[]; /** Auto-generate patterns from schemas (default: true) */ readonly generatePatterns?: boolean; /** Custom patterns to supplement generated ones */ readonly customPatterns?: LanguagePattern[]; /** * What reads and writes the DSL. `'core'`, the only engine (and the * default): the core semantic's hyperscript parser runs — patterns from * `createSchemaPatternGenerator` (positions descending, as a domain's schemas * read), matched by `MatchSkeleton` with the stock reader and confidence * model, and rendered through those patterns where the domain's own renderer * declines. The option stays so a config written for framework 4.7–4.8 * (`engine: 'core'`) builds unchanged; `'classic'`, framework's first * generator and matcher, was removed in 5.0 (domains rethink Phase 6) and * throws. */ readonly engine?: 'core'; } /** * Validation result. */ export interface ValidationResult { readonly valid: boolean; readonly node?: SemanticNode; readonly errors?: string[]; /** Set when the input was refused for a known reason: `'unread-input'` (`UnreadInputError`). */ readonly errorCode?: string; } /** * Compilation result. */ export interface CompileResult { readonly ok: boolean; readonly code?: string; readonly errors?: string[]; /** Set when the input was refused for a known reason: `'unread-input'` (`UnreadInputError`). */ readonly errorCode?: string; readonly node?: SemanticNode; readonly metadata?: { readonly parser: string; readonly confidence: number; }; } /** * Thrown by `parse` (and so `compile`, `validate`, `translate`) when the input reads as a command * only by leaving some of it unread: `select name from users banana` is not `select name from users`. * `reading` is what the longest such match read, `unread` the text after it. `hint`, when there is * one, says how the input might read: where a bare word read as a value is followed by another, the * two may be one value written without quotes (todo `add buy milk to groceries`). */ export declare class UnreadInputError extends Error { readonly input: string; readonly language: string; readonly reading: SemanticNode; readonly unread: string; readonly hint?: string | undefined; readonly code = "unread-input"; constructor(input: string, language: string, reading: SemanticNode, unread: string, hint?: string | undefined); } /** Options for `translate`. */ export interface TranslateOptions { /** * `'allow'` returns a translation that does not read back as the source instead of throwing * `TranslationReadBackError`, as semantic's `translate` does with the same option. */ readonly lossy?: 'allow' | 'refuse'; } /** * Thrown by `translate` when its output, read in the target language, is not the source command: * it does not read at all, or reads as another command, or with a role lost or changed. `partial` is * what `translate` would have returned; `reading` what it reads as (null when it does not read). */ export declare class TranslationReadBackError extends Error { readonly partial: string; readonly from: string; readonly to: string; readonly source: SemanticNode; readonly reading: SemanticNode | null; readonly why: string; readonly code = "translation-does-not-read-back"; constructor(partial: string, from: string, to: string, source: SemanticNode, reading: SemanticNode | null, why: string); } /** * Main multilingual DSL interface. */ export interface MultilingualDSL { parse(input: string, language: string): SemanticNode; parseWithConfidence(input: string, language: string): { node: SemanticNode; confidence: number; }; validate(input: string, language: string): ValidationResult; compile(input: string, language: string): CompileResult; /** * Parse in one language, render in another. The output must read back as the source command * (the same action and role values, or the same compiled code); when it does not, * `TranslationReadBackError`, unless `{ lossy: 'allow' }`. */ translate(input: string, fromLanguage: string, toLanguage: string, options?: TranslateOptions): string; /** * Render a parsed node back to natural language. * * Returns `null` when the action cannot be rendered — no custom renderer, no * domain renderer, and no schema. Optional so that third-party implementers * of this interface are not broken by its addition; `createMultilingualDSL` * always provides it. */ render?(node: SemanticNode, language: string): string | null; getSupportedLanguages(): string[]; } export declare function createMultilingualDSL(config: DSLConfig): MultilingualDSL; //# sourceMappingURL=create-dsl.d.ts.map