createPromptGenerator: unknown generator names produce cryptic 'creator is not a function' TypeError

Open Beginner friendly
#44 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
tooling

Research direction

Start in src/prompt-generators.ts and compare createPromptGenerator with the createAgent validation pattern from issue #5. Replace the unchecked creator lookup with validation using promptGeneratorTypes, then verify that an unknown name reports the name and known generators while valid creators still run.

Written by the indexing model from the issue text.

Description

bug S3

Observed behavior

When createPromptGenerator(...) is called with a tuple whose first element is not a registered generator name, the function throws a cryptic TypeError: creator is not a function instead of a helpful error that names the unknown generator and lists the known ones.

In src/prompt-generators.ts:

const creator = promptGeneratorCreators[type] as PromptGeneratorCreator;
return creator(...args);

The as PromptGeneratorCreator cast hides the fact that promptGeneratorCreators[type] can be undefined, and the subsequent call then throws inside the runtime with no useful context for the caller.

Expected behavior

createPromptGenerator should validate that type is a known prompt generator name and throw a descriptive error that lists the known names. This is the same pattern used in createAgent after issue #5 was fixed:

const creator: AgentCreator | undefined = agentCreators[type];
if (creator == null) {
  throw new Error(
    `Unknown agent '${type}'. Known agents: ${agentTypes.join('\n')}.`,
  );
}

The fix for createPromptGenerator should be identical in spirit, surfacing promptGeneratorTypes (which is already exported from the same file) in the error message.

Minimal reproduction

import { createPromptGenerator } from 'loop-the-loop/prompt-generators';

await createPromptGenerator([
  // @ts-expect-error - intentionally invalid name
  'not-a-real-generator',
  { foo: 'bar' },
]);
// TypeError: creator is not a function

A real-world trigger is a typo in a CLI JSON config file (for example "per_file" instead of "per-file"). The JSON schema would reject it, but a programmatic caller or anyone bypassing the schema gets a confusing crash.

Suggested fix

Mirror the createAgent pattern in src/prompt-generators.ts:

const creator: PromptGeneratorCreator | undefined =
  promptGeneratorCreators[type];
if (creator == null) {
  throw new Error(
    `Unknown prompt generator '${type}'. Known prompt generators: ${promptGeneratorTypes.join('\n')}.`,
  );
}
return creator(...args);

Related: #5 (the same fix was applied to createAgent).

Dominant language
TypeScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from joewalker/loop-the-loop

All issues in joewalker/loop-the-loop

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.