| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- // 成长与反馈:负责人的表扬和批评记进档案;经验、等级、职级、绩效从这里算。规格:docs/specs/company.md §6.4。
- // 模型自己没有记性,"长久"靠两样:读数一直累加(经验只涨不落,绩效两边都会动),
- // 以及每一班的身份说明里都带着最近的表扬与待改进——被表扬的做法保持,被点出来的毛病改掉。
- import type { CompanyFeedback, CompanyFeedbackInput } from "@zcode/shared/company-protocol";
- import { feedbackLine, growthLines, rankOf } from "../domain/growth.js";
- import {
- createGrowthRecord,
- type CompanyGrowthRecord,
- type CompanyTaskRecord,
- } from "../domain/state.js";
- import type { CompanyDossierKeeper } from "./dossier.js";
- import type { CompanyRouting } from "./routing.js";
- import {
- conversationOf,
- employeeOf,
- messagesOf,
- progressionOf,
- type EmployeeRuntime,
- type LiveCompany,
- } from "./snapshot.js";
- import type { Trace } from "./trace.js";
- const MAX_FEEDBACK = 50;
- const MAX_NOTE_CHARS = 500;
- /** "关于哪件事"只留这么长的开头。 */
- const ABOUT_CHARS = 60;
- export interface CompanyGrowth {
- /** 给身份说明、换班整理、复盘用的成长小结。 */
- lines(live: LiveCompany, runtime: EmployeeRuntime): string[];
- /** 负责人在界面上点的表扬或批评;出错抛给界面。 */
- give(live: LiveCompany, input: CompanyFeedbackInput): Promise<void>;
- /** 负责人验收任务:通过记一条表扬,打回记一条待改进。 */
- review(live: LiveCompany, task: CompanyTaskRecord, accept: boolean, note?: string): Promise<void>;
- /** 经验可能涨了:等级变了就在他所在的群里公告。 */
- settle(live: LiveCompany, runtime: EmployeeRuntime): void;
- /** 装载时给每个人记下现在的等级(之后涨了才算升级)。返回有没有改动。 */
- baseline(live: LiveCompany): boolean;
- }
- /** 反馈针对的是哪句话、哪件任务。 */
- interface FeedbackTarget {
- about?: string;
- conversationId?: string;
- messageId?: string;
- taskId?: string;
- }
- export function createGrowth(deps: {
- now: () => number;
- trace: Trace;
- routing: CompanyRouting;
- dossier: CompanyDossierKeeper;
- }): CompanyGrowth {
- const { now, trace, routing, dossier } = deps;
- let sequence = 0;
- /** 这位员工的成长计数;还没有就建一份。 */
- function countsOf(live: LiveCompany, name: string): CompanyGrowthRecord {
- const existing = live.record.growth[name];
- if (existing) return existing;
- const created = createGrowthRecord();
- live.record.growth[name] = created;
- return created;
- }
- function lines(live: LiveCompany, runtime: EmployeeRuntime): string[] {
- const view = progressionOf(live, runtime.profile.name, runtime);
- return growthLines({
- level: view.level,
- rank: view.rank,
- expToNext: view.expToNext,
- performance: view.performance,
- feedback: live.record.feedback[runtime.profile.name] ?? [],
- });
- }
- function targetOf(live: LiveCompany, input: CompanyFeedbackInput): FeedbackTarget {
- const task = input.taskId
- ? live.record.tasks.find(
- (item) => item.id === input.taskId && item.employee === input.employee,
- )
- : undefined;
- const conversation =
- input.messageId && input.conversationId
- ? conversationOf(live, input.conversationId)
- : undefined;
- const message = conversation
- ? messagesOf(live, conversation.id).find((item) => item.id === input.messageId)
- : undefined;
- const about =
- task?.title ??
- (message ? message.text.replace(/\s+/gu, " ").slice(0, ABOUT_CHARS) : undefined);
- return {
- ...(about ? { about } : {}),
- ...(message && conversation
- ? { conversationId: conversation.id, messageId: message.id }
- : {}),
- ...(task ? { taskId: task.id } : {}),
- };
- }
- /** 记一条:档案里写一行(FEEDBACK.md),状态里留最近的一些给界面和身份说明用。 */
- async function record(
- live: LiveCompany,
- runtime: EmployeeRuntime,
- entry: Omit<CompanyFeedback, "id" | "at">,
- ): Promise<void> {
- sequence += 1;
- const feedback: CompanyFeedback = {
- id: `f-${String(now())}-${String(sequence)}`,
- at: now(),
- ...entry,
- };
- await dossier.appendFeedback(live, runtime, feedbackLine(feedback));
- const name = runtime.profile.name;
- live.record.feedback[name] = [feedback, ...(live.record.feedback[name] ?? [])].slice(
- 0,
- MAX_FEEDBACK,
- );
- }
- async function give(live: LiveCompany, input: CompanyFeedbackInput): Promise<void> {
- const runtime = employeeOf(live, input.employee);
- if (!runtime) throw new Error(`公司里没有叫「${input.employee}」的员工`);
- if (input.kind !== "praise" && input.kind !== "critique") {
- throw new Error("反馈只能是表扬(praise)或批评(critique)");
- }
- const note = typeof input.note === "string" ? input.note.trim() : "";
- if (note.length > MAX_NOTE_CHARS)
- throw new Error(`说明太长了,最多 ${String(MAX_NOTE_CHARS)} 个字`);
- // 批评不给话,员工不知道该改什么:这一条必须写。
- if (input.kind === "critique" && !note) {
- throw new Error("批评要写清哪里做得不对,他才知道改什么");
- }
- countsOf(live, input.employee)[input.kind] += 1;
- await record(live, runtime, {
- kind: input.kind,
- ...(note ? { note } : {}),
- ...targetOf(live, input),
- });
- await trace.persist(live, {
- files: trace.stateFile(live),
- message: `feedback ${input.employee} ${input.kind}`,
- });
- settle(live, runtime);
- }
- async function review(
- live: LiveCompany,
- task: CompanyTaskRecord,
- accept: boolean,
- note?: string,
- ): Promise<void> {
- const runtime = employeeOf(live, task.employee);
- if (!runtime) return;
- const counts = countsOf(live, task.employee);
- if (accept) counts.accepted += 1;
- else counts.rejected += 1;
- await record(live, runtime, {
- kind: accept ? "praise" : "critique",
- origin: "review",
- ...(note ? { note } : {}),
- about: task.title,
- taskId: task.id,
- });
- }
- function settle(live: LiveCompany, runtime: EmployeeRuntime): void {
- const name = runtime.profile.name;
- const counts = countsOf(live, name);
- const level = progressionOf(live, name, runtime).level;
- if (level <= counts.level) return;
- counts.level = level;
- const text = `🎉 ${name} 升到 Lv.${String(level)}(${rankOf(level)})`;
- const groups = live.record.conversations.filter(
- (conversation) => conversation.kind === "group" && conversation.members.includes(name),
- );
- for (const conversation of groups) routing.post(live, conversation.id, text);
- // 不在任何群里:在他和负责人的私聊里说一声。
- if (groups.length === 0) routing.postTo(live, name, text);
- trace.persistLater(live, {
- files: trace.stateFile(live),
- message: `employee ${name} 升到 Lv.${String(level)}`,
- });
- }
- function baseline(live: LiveCompany): boolean {
- let changed = false;
- for (const profile of live.company?.employees ?? []) {
- const runtime = employeeOf(live, profile.name);
- if (!runtime || live.record.growth[profile.name]) continue;
- live.record.growth[profile.name] = {
- ...createGrowthRecord(),
- level: progressionOf(live, profile.name, runtime).level,
- };
- changed = true;
- }
- return changed;
- }
- return { lines, give, review, settle, baseline };
- }
|