Skip to content

feat: CoachAnalyzer — post-hoc skill grading (extracted from lead-engine)#4

Merged
chatman-media merged 1 commit into
mainfrom
feat/coach-analyzer
May 21, 2026
Merged

feat: CoachAnalyzer — post-hoc skill grading (extracted from lead-engine)#4
chatman-media merged 1 commit into
mainfrom
feat/coach-analyzer

Conversation

@chatman-media

@chatman-media chatman-media commented May 21, 2026

Copy link
Copy Markdown
Owner

Summary

Добавляет CoachAnalyzer — post-hoc анализ closed-lead conversation'ов: для каждой пары (user-question, bot-reply) дёргает gradeSkills и пишет результаты в skill-outcomes repo.

Извлечён из chatman-media/lead-engine (SaaS-платформа, использующая sales как доменный модуль). Возвращён upstream чтобы npm-consumers пакета тоже могли использовать.

Decoupling

Lead-engine'овская версия импортировала DAL-типы напрямую из @chatman-media/conversation-engine (LeadRow, MessageRow, MessagesRepo, SkillOutcomesRepo). Для package-agnostic использования заменено на in-file interfaces:

```ts
export interface CoachAnalyzerLead { id: number; tenantId: number; }
export interface CoachAnalyzerMessage { id: number; role: string; text: string; }
export interface CoachAnalyzerMessages { recent(conversationId, limit): Promise<...> }
export interface CoachAnalyzerSkillOutcomes { record(opts): Promise }
```

Caller передаёт свои репы — sales остаётся schema-агностичен.

Tests

8 unit tests pass:

  • `extractUserAssistantPairs` — 5 cases (alternating, human-role, two users in a row, system skip, empty text)
  • `CoachAnalyzer.analyzeLead` — 3 cases (happy path + write outcomes, idempotency, no-pairs)

```
$ bun test src/tests/coach-analyzer.test.ts
8 pass / 0 fail
```

`bun typecheck` — clean.

Bump: 0.1.1 → 0.2.0

Minor — additions only, no breaking changes.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added conversation analysis capabilities to automatically evaluate skill development from completed coaching interactions and record outcomes.
  • Tests

    • Added comprehensive unit tests for the analysis functionality.
  • Chores

    • Version updated to 0.2.0.

Review Change Stack

Добавляет `CoachAnalyzer` class — post-hoc анализ closed-lead conversation'ов.
Для каждой пары (user-question, bot-reply) дёргает `gradeSkills`
(@chatman-media/rag) и пишет результаты в skill-outcomes repo. Используется:
  - admin-UI: win-rate per skill для inspect'а
  - coach-proposals: данные для proposeStyleEdits
  - shadow-evaluations: pair-wise сравнения styles

Извлечён из чужого монорепо (chatman-media/lead-engine — SaaS-платформа).
Чтобы класс мог работать в любой DAL-shape (lead-engine использует Drizzle
multi-tenant repos; standalone tg-chatbot — что-то другое), все упоминания
конкретных типов БД заменены на in-file interfaces:

  - CoachAnalyzerLead     — { id, tenantId }
  - CoachAnalyzerMessage  — { id, role, text }
  - CoachAnalyzerMessages — recent(conversationId, limit)
  - CoachAnalyzerSkillOutcomes — record(...): Promise<boolean>

ChatClient + gradeSkills остаются из @chatman-media/rag (existing peer dep).

Также exported `extractUserAssistantPairs` для standalone использования —
свернуть flat-массив messages в alternating user→assistant pairs.

8 unit tests покрывают:
  - extractUserAssistantPairs (alternating / human-role / два user подряд
    / system skip / пустой text)
  - CoachAnalyzer (анализ пар + write outcomes, idempotency, no-pairs case)

Bump: 0.1.1 → 0.2.0 (minor — только additions, никаких breaking changes).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3cfa90ea-cabf-4f88-8bc1-9403c8dfb6ea

📥 Commits

Reviewing files that changed from the base of the PR and between 2294854 and 6d2e4d5.

📒 Files selected for processing (4)
  • package.json
  • src/__tests__/coach-analyzer.test.ts
  • src/coach-analyzer.ts
  • src/index.ts

📝 Walkthrough

Walkthrough

This PR introduces a new CoachAnalyzer module that performs post-hoc analysis of completed conversations. It extracts ordered user-assistant message pairs, grades each pair for coaching skills using an LLM, and records skill outcomes with idempotent semantics. The feature is fully tested and exported from the package entrypoint, with the version bumped to 0.2.0.

Changes

Coach Analyzer Feature

Layer / File(s) Summary
Type contracts and interfaces
src/coach-analyzer.ts (lines 1–97)
Defines CoachAnalyzerOpts with dependency injection for chat resolution and grading model, minimal CoachAnalyzerMessage / CoachAnalyzerLead / CoachAnalyzerMessages repository contracts for fetching conversation history, CoachAnalyzerSkillOutcomes contract for idempotent outcome recording, and AnalyzeLeadOpts / AnalysisResult shapes for input and output of analysis.
Message pair extraction
src/coach-analyzer.ts (lines 155–177), src/__tests__/coach-analyzer.test.ts (lines 1–63)
extractUserAssistantPairs scans messages and yields valid (user, assistant/human) pairs with non-empty text, skipping system messages and handling consecutive user messages. Tests validate normal alternation, human role handling, skip-first logic, and filtering rules.
CoachAnalyzer orchestration and analysis
src/coach-analyzer.ts (lines 99–152), src/__tests__/coach-analyzer.test.ts (lines 65–215)
CoachAnalyzer.analyzeLead loads recent messages, extracts pairs, calls gradeSkills for skill slugs, and records one skill_outcomes row per (lead, skill, source) with de-duplication. Tests include fakes for chat and repository, assertions on LLM call counts, outcome counters, idempotency verification, and edge case handling for conversations with no valid pairs.
Public API exposure
src/index.ts (lines 10–25)
Re-exports CoachAnalyzer class, extractUserAssistantPairs function, and all type contracts (CoachAnalyzerOpts, CoachAnalyzerMessage, CoachAnalyzerLead, CoachAnalyzerMessages, CoachAnalyzerSkillOutcomes, AnalyzeLeadOpts, AnalysisResult) from the package entrypoint.
Package version
package.json (line 3)
Version bumped from 0.1.1 to 0.2.0.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A coach analyzer hops in today,
Extracting pairs from chats, come what may,
With skills graded true and outcomes tracked well,
Idempotent and tested—a perfect spell! ✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/coach-analyzer

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatman-media chatman-media merged commit 7070d02 into main May 21, 2026
1 of 3 checks passed
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.

1 participant