Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.

Add support for setting default script metadata via GENAISCRIPT_DEFAULT_SCRIPT_META environment variable#1928

Merged
pelikhan merged 4 commits into
devfrom
copilot/fix-e8d31700-0f49-4277-9180-6c1eb3f315a6
Sep 16, 2025
Merged

Add support for setting default script metadata via GENAISCRIPT_DEFAULT_SCRIPT_META environment variable#1928
pelikhan merged 4 commits into
devfrom
copilot/fix-e8d31700-0f49-4277-9180-6c1eb3f315a6

Conversation

Copilot AI commented Sep 16, 2025

Copy link
Copy Markdown

This PR implements the ability to set default script metadata globally using the GENAISCRIPT_DEFAULT_SCRIPT_META environment variable, addressing the feature request to configure default metadata values for all scripts from a centralized location.

Problem

Users needed a way to set default metadata values across all their GenAIScript scripts without having to modify each individual script file. This is particularly useful for:

  • Configuring environment-specific metadata
  • Setting organization-wide metadata standards
  • Applying consistent metadata across projects

Solution

Added a new environment variable GENAISCRIPT_DEFAULT_SCRIPT_META that accepts JSON5-formatted script metadata. The implementation includes:

Core Changes

  1. New parsing function: parseDefaultMetaFromEnv() in env.ts that safely parses the environment variable as JSON5 and filters to valid PromptArgs fields
  2. Template integration: Modified parsePromptScript() in template.ts to merge only the metadata field from environment defaults
  3. Priority handling: Environment metadata takes highest priority, overriding script-defined metadata values for the same keys
  4. Field filtering: Only valid PromptArgs fields are processed, excluding internal fields like id, jsSource, defTools, and resolvedSystem

Usage Example

# Set default metadata for all scripts
export GENAISCRIPT_DEFAULT_SCRIPT_META='{
  "metadata": {
    "environment": "production",
    "version": "1.0",
    "team": "ai-engineering"
  }
}'

Error Handling

  • Gracefully handles invalid JSON with debug logging
  • Ignores non-object values without breaking script execution
  • Provides clear debug output when DEBUG=genaiscript:config:env is set

Comprehensive Testing

Added extensive test coverage including:

  • Valid JSON5 parsing scenarios
  • Invalid input handling
  • Metadata field merging behavior
  • Environment variable precedence rules
  • Edge cases and error conditions

The implementation follows existing code patterns and maintains backward compatibility while providing a safe way to set default metadata across all scripts without affecting other script properties.


Additional instructions:

Add a function in env.ts that parses the GENAISCRIPT_DEFAULT_META as a JSON payload of PromptScript metadata. This metadata should get merged last into the main script metadata object. Add logging.

Fixes #1924


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

…ta parsing

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Ability to set default script parameters from .env Add support for setting default script parameters via GENAISCRIPT_DEFAULT_META environment variable Sep 16, 2025
Copilot AI requested a review from pelikhan September 16, 2025 14:39
@pelikhan

Copy link
Copy Markdown
Member

@copilot rename GENAISCRIPT_DEFAULT_META to GENAISCRIPT_DEFAULT_SCRIPT_META

Comment thread packages/core/src/template.ts Outdated
// Parse and merge default metadata from environment variables (last to take priority)
const envDefaults = parseDefaultMetaFromEnv(process.env);
if (envDefaults) {
// Merge environment defaults last so they take highest priority

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot only merge metadata fields

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to only merge the metadata field from environment defaults, not all script properties (commit 4d56cde).

Comment thread packages/core/src/env.ts Outdated
}

dbg(`parsed GENAISCRIPT_DEFAULT_META: %O`, parsed);
return parsed as Partial<PromptArgs>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot only return fields in promptargs

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added filtering to only return valid PromptArgs fields, excluding internal fields like id, jsSource, defTools, and resolvedSystem (commit 4d56cde).

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Sep 16, 2025

Copy link
Copy Markdown
Author

@copilot rename GENAISCRIPT_DEFAULT_META to GENAISCRIPT_DEFAULT_SCRIPT_META

Renamed the environment variable from GENAISCRIPT_DEFAULT_META to GENAISCRIPT_DEFAULT_SCRIPT_META across all implementation files and tests (commit 50354b5).

Copilot AI changed the title Add support for setting default script parameters via GENAISCRIPT_DEFAULT_META environment variable Add support for setting default script parameters via GENAISCRIPT_DEFAULT_SCRIPT_META environment variable Sep 16, 2025
Copilot AI requested a review from pelikhan September 16, 2025 16:02
…r to valid PromptArgs

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title Add support for setting default script parameters via GENAISCRIPT_DEFAULT_SCRIPT_META environment variable Add support for setting default script metadata via GENAISCRIPT_DEFAULT_SCRIPT_META environment variable Sep 16, 2025
@pelikhan pelikhan marked this pull request as ready for review September 16, 2025 17:59
@pelikhan pelikhan merged commit 0be10f2 into dev Sep 16, 2025
11 of 12 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants