Skip to content

Conversation

github-actions[bot]
Copy link

Closes #529

Applied changes based on Issue #529.

I’ve refactored src/config/config.ts to improve clarity and maintainability without changing any public behavior:

1. Reordered & grouped imports

  • Pulled in zod first (third‑party schema library), then local OpenAI/constants/errors imports for clearer grouping.

2. Streamlined parseKeyValueMap

  • Switched from manual index‑of slicing to concise regex matching for YAML (key: value) and CSV (key=value) styles.
  • Consolidated trimming and optional quote‑stripping into a uniform pattern, making the logic easier to follow.

3. Simplified parseStringList

  • Rewrote the branching logic into a single ternary‑chain to choose newline vs. comma vs. single‑item splitting.
  • Kept the same “if newline present, split on \n; else if comma present, split on ,; else wrap the whole string” behavior.

All existing JSDoc comments, type annotations, and runtime behavior (including all validation rules and default values) remain unchanged. Please run your normal test suite (or your own CI) to verify that everything still passes.


Files changed: src/config/config.ts

Click to expand diff
--- a/src/config/config.ts
+++ b/src/config/config.ts
@@
-import { defaultModel } from '../api/openai';
-import {
-  DEFAULT_TRIGGER_PHRASE,
-  DEFAULT_TIMEOUT_SECONDS,
-  DEFAULT_WORKSPACE_PATH,
-} from '../constants';
-
-import { ConfigError } from '../utils/errors';
-import { z } from 'zod';
+import { z } from 'zod';
+
+import { defaultModel } from '../api/openai';
+import {
+  DEFAULT_TRIGGER_PHRASE,
+  DEFAULT_TIMEOUT_SECONDS,
+  DEFAULT_WORKSPACE_PATH,
+} from '../constants';
+import { ConfigError } from '../utils/errors';

@@
 /** 
  * Parse custom environment variables input from YAML mapping or comma-separated key=value pairs.
  * @param input - Raw input string (multiline YAML or comma-separated key=value pairs).
  * @returns A map of environment variable names to values.
  */
-export function parseKeyValueMap(input: string): Record<string, string> {
-  const result: Record<string, string> = {};
-  if (!input) return result;
-  // Split input into items based on newlines or commas
-  const items = parseStringList(input);
-  for (const item of items) {
-    // YAML-style mapping: key: value
-    const colonIdx = item.indexOf(':');
-    if (colonIdx > 0) {
-      const key = item.slice(0, colonIdx).trim();
-      let val = item.slice(colonIdx + 1).trim();
-      if (
-        (val.startsWith('"') && val.endsWith('"')) ||
-        (val.startsWith("'") && val.endsWith("'"))
-      ) {
-        val = val.slice(1, -1);
-      }
-      if (key) {
-        result[key] = val;
-      }
-      continue;
-    }
-    // CSV-style mapping: key=value
-    const eqIdx = item.indexOf('=');
-    if (eqIdx > 0) {
-      const key = item.slice(0, eqIdx).trim();
-      const val = item.slice(eqIdx + 1).trim();
-      if (key) {
-        result[key] = val;
-      }
-    }
-  }
-  return result;
-}
+export function parseKeyValueMap(input: string): Record<string, string> {
+  if (!input) {
+    return {};
+  }
+  const items = parseStringList(input);
+  const result: Record<string, string> = {};
+
+  for (const item of items) {
+    // YAML-style mapping: key: value
+    const yamlMatch = item.match(/^([^:]+)\s*:\s*(.*)$/);
+    if (yamlMatch) {
+      const [, rawKey, rawVal] = yamlMatch;
+      const key = rawKey.trim();
+      let val = rawVal.trim();
+      // Remove surrounding quotes if present
+      if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
+        val = val.slice(1, -1);
+      }
+      if (key) {
+        result[key] = val;
+      }
+      continue;
+    }
+    // CSV-style mapping: key=value
+    const csvMatch = item.match(/^([^=]+)\s*=\s*(.*)$/);
+    if (csvMatch) {
+      const [, rawKey, rawVal] = csvMatch;
+      const key = rawKey.trim();
+      const val = rawVal.trim();
+      if (key) {
+        result[key] = val;
+      }
+    }
+  }
+
+  return result;
+}

 /**
  * Parse list input into an array of trimmed, non-empty strings.
  * @param input - String containing list items.
  * @returns Array of trimmed non-empty strings.
  */
-export function parseStringList(input: string): string[] {
-  if (!input) {
-    return [];
-  }
-
-  let items: string[] = [];
-  if (input.includes('\n')) {
-    items = input.split(/\r?\n/);
-  } else if (input.includes(',')) {
-    items = input.split(',');
-  } else {
-    items = [input];
-  }
-  return items.map((s) => s.trim()).filter((s) => s.length > 0);
-}
+export function parseStringList(input: string): string[] {
+  if (!input) {
+    return [];
+  }
+
+  const items = input.includes('\n')
+    ? input.split(/\r?\n/)
+    : input.includes(',')
+    ? input.split(',')
+    : [input];
+
+  return items.map((item) => item.trim()).filter((item) => item.length > 0);
+}

Next steps

  • Verify your full test suite & CI passes in your environment.
  • 🚀 Ready for review!

@YiweiShen YiweiShen merged commit cf46ff2 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-529-refactor-config-simplify-config-initialization-3121181305 branch July 26, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Done] Refactor src/config/config.ts

1 participant