Skip to content

feat(ai): add prompts.getAll to fetch all prompts at a label - #4903

Merged
jurajmajerik merged 4 commits into
mainfrom
prompts-get-all-by-label
Sep 10, 2026
Merged

jurajmajerik merged 4 commits into
mainfrom
prompts-get-all-by-label

Conversation

@jurajmajerik

Copy link
Copy Markdown
Contributor

Problem

Changes

  • prompts.getAll({ label: 'production' }) fetches every prompt at that label in one call, following pagination, and returns an object of name to result.
  • Each fetched prompt is stored in the existing cache, so later prompts.get(name, { label }) calls are cache hits. One call per cache cycle instead of one per prompt.
  • Old servers ignore the label param on the list endpoint and return latest versions. Each row is checked against its all_labels field; if nothing resolves the label, the call throws a clear error instead of caching wrong versions. A single row whose label moved mid-request is skipped with a warning.
  • Pagination links are only followed on the configured host (the personal API key travels in the Authorization header), and the page safety cap throws instead of returning a truncated result. Both mirror review findings on the Python PR.

Release info Sub-libraries affected

Libraries affected

  • @posthog/ai

Checklist

  • Tests for new code
  • Accounted for the impact of any changes across different platforms
  • Accounted for backwards compatibility of any changes (no breaking changes!)
  • Took care not to unnecessarily increase the bundle size

If releasing new changes

  • Changeset added in .changeset/ (written by hand, same format as pnpm changeset)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

@jurajmajerik jurajmajerik self-assigned this Sep 10, 2026
@jurajmajerik
jurajmajerik marked this pull request as ready for review September 10, 2026 14:11
@jurajmajerik
jurajmajerik requested a review from a team as a code owner September 10, 2026 14:11
@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
packages/ai/src/prompts.ts:301
**Partial matches hide incompatibility**

On an older server that ignores the label filter, some latest prompt versions can match the requested label while others do not. Because this guard throws only when no rows match, `getAll()` returns an incomplete set as a successful result. Callers cannot distinguish the missing prompts from prompts that do not carry the label, and the promised one-request cache warm-up is incomplete.

### Issue 2
packages/ai/src/prompts.ts:276-280
**Malformed rows are silently skipped**

This condition handles malformed rows the same way as valid rows whose label moved. If a response contains valid rows alongside one with a missing or incorrectly typed prompt field, `getAll()` caches and returns only the valid subset instead of rejecting the invalid response. That hides server response errors and leaves callers with an incomplete result, unlike the existing single-prompt fetch path, which throws when response validation fails.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "feat(ai): add prompts.getAll to fetch al..." | Re-trigger Greptile

Comment thread packages/ai/src/prompts.ts Outdated
}
}

if (rows.length > 0 && Object.keys(results).length === 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Partial matches hide incompatibility

On an older server that ignores the label filter, some latest prompt versions can match the requested label while others do not. Because this guard throws only when no rows match, getAll() returns an incomplete set as a successful result. Callers cannot distinguish the missing prompts from prompts that do not carry the label, and the promised one-request cache warm-up is incomplete.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/prompts.ts
Line: 301

Comment:
**Partial matches hide incompatibility**

On an older server that ignores the label filter, some latest prompt versions can match the requested label while others do not. Because this guard throws only when no rows match, `getAll()` returns an incomplete set as a successful result. Callers cannot distinguish the missing prompts from prompts that do not carry the label, and the promised one-request cache warm-up is incomplete.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed in 25a7bd5

Comment thread packages/ai/src/prompts.ts Outdated
Comment on lines +276 to +280
if (!isPromptApiResponse(row) || !rowResolvesLabel(row, label)) {
const name = typeof row === 'object' && row !== null ? (row as Record<string, unknown>).name : undefined
skipped.push(typeof name === 'string' ? name : '?')
continue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Malformed rows are silently skipped

This condition handles malformed rows the same way as valid rows whose label moved. If a response contains valid rows alongside one with a missing or incorrectly typed prompt field, getAll() caches and returns only the valid subset instead of rejecting the invalid response. That hides server response errors and leaves callers with an incomplete result, unlike the existing single-prompt fetch path, which throws when response validation fails.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/ai/src/prompts.ts
Line: 276-280

Comment:
**Malformed rows are silently skipped**

This condition handles malformed rows the same way as valid rows whose label moved. If a response contains valid rows alongside one with a missing or incorrectly typed prompt field, `getAll()` caches and returns only the valid subset instead of rejecting the invalid response. That hides server response errors and leaves callers with an incomplete result, unlike the existing single-prompt fetch path, which throws when response validation fails.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed in 3d8197f

@marandaneto marandaneto left a comment

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.

Advisory review of the verified PR snapshot.

label,
config,
fetchedAt: now,
})

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.

blocking: Preserve __proto__ as a prompt entry — The API permits the prompt name __proto__, but assigning it into the plain results object changes its prototype instead of creating an own property. This prompt disappears from enumeration and JSON serialization; when it is the only returned prompt, getAll() incorrectly throws the unsupported-server error. Use a dictionary that safely handles arbitrary prompt names. Reproduction: reproduced — pnpm exec vitest run tests/prompts.test.ts tests/prompts-get-all-review.test.ts --coverage.enabled=false in a disposable validation worktree passed all 65 existing tests and failed both added regression cases (missing entry and erroneous exception); both cases passed with a temporary null-prototype result object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@marandaneto marandaneto left a comment

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.

approving to unblock, resolve comments before

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