Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

Eliminated code duplication across OpenAI-compatible job runners (openai, openrouter) and API key retrieval patterns in all providers.

Changes

Created src/job/utils.ts with shared utilities:

  • createStreamingGenerator() - SSE streaming response handler
  • transformToolsToFunctions() - ChatTool to OpenAI function format conversion
  • transformUsageData() - OpenAI usage response normalization
  • getApiKey() - Environment variable fallback pattern

Refactored job runners:

  • openai.ts: 105 → 79 lines (-26)
  • openrouter.ts: 79 → 53 lines (-26)
  • voyage.ts, fal.ts: Now use shared getApiKey()

Example

Before:

// Duplicated in openai.ts and openrouter.ts
const apiKey = options?.apiKey || process.env.OPENAI_API_KEY;
const tools = input.tools?.map((tool: ChatTool) => ({
  type: "function",
  function: { name: tool.name, description: tool.description, parameters: tool.input }
}));

After:

const apiKey = getApiKey(options, "OPENAI_API_KEY");
const tools = transformToolsToFunctions(input.tools);

Net: -15 lines, ~77 lines of duplication removed.

Original prompt

Find and refactor duplicated code


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 16, 2025 03:15
Co-authored-by: wangzuo <1039026+wangzuo@users.noreply.github.com>
Co-authored-by: wangzuo <1039026+wangzuo@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor duplicated code for better maintainability Refactor: Extract duplicated code in job runners Nov 16, 2025
Copilot AI requested a review from wangzuo November 16, 2025 03:20
@wangzuo wangzuo marked this pull request as ready for review November 25, 2025 16:00
@wangzuo wangzuo merged commit 9987500 into main Nov 25, 2025
2 checks passed
@wangzuo wangzuo deleted the copilot/refactor-duplicated-code branch November 25, 2025 16:00
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.

2 participants