Generator

XMLNodeDescription

The XMLNodeDescription class is designed to generate and manage descriptions for XML tags. It provides functionality to create customizable prompts for XML elements, including their attributes and usage examples.

Constructor

constructor(desc: { 
    tag: string; 
    description: string; 
    selfClosing?: boolean; 
    attributes?: Record<string, string> 
})

Creates a new XMLNodeDescription instance.

  • Parameters:

    • desc: object: An object containing the tag details:

      • tag: string: The XML tag name.

      • description: string: A description of the XML tag.

      • selfClosing?: boolean: Optional. Indicates if the tag is self-closing. Default is false.

      • attributes?: Record<string, string>: Optional. A key-value pair of attribute names and their descriptions.

Methods

Getters

tag: string

Returns the XML tag name.

prompt: string

Returns the complete prompt, combining mainPrompt, attributePrompt, and examplePrompt.

mainPrompt: string

Returns the main prompt describing the tag.

attributePrompt: string

Returns the prompt describing the tag's attributes.

examplePrompt: string

Returns the prompt providing usage examples of the tag.

Public Methods

setExamples(examples: XMLNodeDescriptionExample[]): void

Sets the examples for the XML tag and regenerates the example prompt.

  • Parameters:

    • examples: XMLNodeDescriptionExample[]: An array of input-output examples.

setAttributes(attributes: Record<string, string>): void

Sets the attributes for the XML tag and regenerates the attribute prompt.

  • Parameters:

    • attributes: Record<string, string>: A key-value pair of attribute names and their descriptions.

overloadMainPrompt(prompt: string): void

Overrides the main prompt with a custom string.

  • Parameters:

    • prompt: string: The new main prompt.

overloadExamplePrompt(callback: ((example: XMLNodeDescriptionExample) => string) | null): void

Sets a custom callback for formatting example prompts and regenerates the example prompt.

  • Parameters:

    • callback: ((example: XMLNodeDescriptionExample) => string) | null: A function that takes an example and returns a formatted string, or null to reset to default formatting.

overloadAttributePrompt(callback: ((key: string, value: string) => string) | null): void

Sets a custom callback for formatting attribute prompts and regenerates the attribute prompt.

  • Parameters:

    • callback: ((key: string, value: string) => string) | null: A function that takes an attribute key and value and returns a formatted string, or null to reset to default formatting.

Usage Example

const imgDesc = new XMLNodeDescription({
  tag: 'img',
  description: 'An image element',
  selfClosing: true,
  attributes: { src: 'Image source URL', alt: 'Alternative text' }
});

console.log(imgDesc.mainPrompt);
// <img/>: An image element (This is a self-closing tag)

console.log(imgDesc.attributePrompt);
// Attributes:
//     src: Image source URL
//     alt: Alternative text

imgDesc.setExamples([
  { input: '', output: '<img src="example.jpg" alt="An example image"/>' }
]);

console.log(imgDesc.examplePrompt);
// Examples:
//     Input: ""
//     Output: "<img src="example.jpg" alt="An example image"/>"

console.log(imgDesc.prompt);
// <img/>: An image element (This is a self-closing tag)
// Attributes:
//     src: Image source URL
//     alt: Alternative text
// Examples:
//     Input: ""
//     Output: "<img src="example.jpg" alt="An example image"/>"

imgDesc.overloadAttributePrompt((key, value) => `  ${key.toUpperCase()}: ${value}\n`);
console.log(imgDesc.attributePrompt);
// Attributes:
//   SRC: Image source URL
//   ALT: Alternative text

Last updated