growth.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // 成长与反馈:负责人的表扬和批评记进档案;经验、等级、职级、绩效从这里算。规格:docs/specs/company.md §6.4。
  2. // 模型自己没有记性,"长久"靠两样:读数一直累加(经验只涨不落,绩效两边都会动),
  3. // 以及每一班的身份说明里都带着最近的表扬与待改进——被表扬的做法保持,被点出来的毛病改掉。
  4. import type { CompanyFeedback, CompanyFeedbackInput } from "@zcode/shared/company-protocol";
  5. import { feedbackLine, growthLines, rankOf } from "../domain/growth.js";
  6. import {
  7. createGrowthRecord,
  8. type CompanyGrowthRecord,
  9. type CompanyTaskRecord,
  10. } from "../domain/state.js";
  11. import type { CompanyDossierKeeper } from "./dossier.js";
  12. import type { CompanyRouting } from "./routing.js";
  13. import {
  14. conversationOf,
  15. employeeOf,
  16. messagesOf,
  17. progressionOf,
  18. type EmployeeRuntime,
  19. type LiveCompany,
  20. } from "./snapshot.js";
  21. import type { Trace } from "./trace.js";
  22. const MAX_FEEDBACK = 50;
  23. const MAX_NOTE_CHARS = 500;
  24. /** "关于哪件事"只留这么长的开头。 */
  25. const ABOUT_CHARS = 60;
  26. export interface CompanyGrowth {
  27. /** 给身份说明、换班整理、复盘用的成长小结。 */
  28. lines(live: LiveCompany, runtime: EmployeeRuntime): string[];
  29. /** 负责人在界面上点的表扬或批评;出错抛给界面。 */
  30. give(live: LiveCompany, input: CompanyFeedbackInput): Promise<void>;
  31. /** 负责人验收任务:通过记一条表扬,打回记一条待改进。 */
  32. review(live: LiveCompany, task: CompanyTaskRecord, accept: boolean, note?: string): Promise<void>;
  33. /** 经验可能涨了:等级变了就在他所在的群里公告。 */
  34. settle(live: LiveCompany, runtime: EmployeeRuntime): void;
  35. /** 装载时给每个人记下现在的等级(之后涨了才算升级)。返回有没有改动。 */
  36. baseline(live: LiveCompany): boolean;
  37. }
  38. /** 反馈针对的是哪句话、哪件任务。 */
  39. interface FeedbackTarget {
  40. about?: string;
  41. conversationId?: string;
  42. messageId?: string;
  43. taskId?: string;
  44. }
  45. export function createGrowth(deps: {
  46. now: () => number;
  47. trace: Trace;
  48. routing: CompanyRouting;
  49. dossier: CompanyDossierKeeper;
  50. }): CompanyGrowth {
  51. const { now, trace, routing, dossier } = deps;
  52. let sequence = 0;
  53. /** 这位员工的成长计数;还没有就建一份。 */
  54. function countsOf(live: LiveCompany, name: string): CompanyGrowthRecord {
  55. const existing = live.record.growth[name];
  56. if (existing) return existing;
  57. const created = createGrowthRecord();
  58. live.record.growth[name] = created;
  59. return created;
  60. }
  61. function lines(live: LiveCompany, runtime: EmployeeRuntime): string[] {
  62. const view = progressionOf(live, runtime.profile.name, runtime);
  63. return growthLines({
  64. level: view.level,
  65. rank: view.rank,
  66. expToNext: view.expToNext,
  67. performance: view.performance,
  68. feedback: live.record.feedback[runtime.profile.name] ?? [],
  69. });
  70. }
  71. function targetOf(live: LiveCompany, input: CompanyFeedbackInput): FeedbackTarget {
  72. const task = input.taskId
  73. ? live.record.tasks.find(
  74. (item) => item.id === input.taskId && item.employee === input.employee,
  75. )
  76. : undefined;
  77. const conversation =
  78. input.messageId && input.conversationId
  79. ? conversationOf(live, input.conversationId)
  80. : undefined;
  81. const message = conversation
  82. ? messagesOf(live, conversation.id).find((item) => item.id === input.messageId)
  83. : undefined;
  84. const about =
  85. task?.title ??
  86. (message ? message.text.replace(/\s+/gu, " ").slice(0, ABOUT_CHARS) : undefined);
  87. return {
  88. ...(about ? { about } : {}),
  89. ...(message && conversation
  90. ? { conversationId: conversation.id, messageId: message.id }
  91. : {}),
  92. ...(task ? { taskId: task.id } : {}),
  93. };
  94. }
  95. /** 记一条:档案里写一行(FEEDBACK.md),状态里留最近的一些给界面和身份说明用。 */
  96. async function record(
  97. live: LiveCompany,
  98. runtime: EmployeeRuntime,
  99. entry: Omit<CompanyFeedback, "id" | "at">,
  100. ): Promise<void> {
  101. sequence += 1;
  102. const feedback: CompanyFeedback = {
  103. id: `f-${String(now())}-${String(sequence)}`,
  104. at: now(),
  105. ...entry,
  106. };
  107. await dossier.appendFeedback(live, runtime, feedbackLine(feedback));
  108. const name = runtime.profile.name;
  109. live.record.feedback[name] = [feedback, ...(live.record.feedback[name] ?? [])].slice(
  110. 0,
  111. MAX_FEEDBACK,
  112. );
  113. }
  114. async function give(live: LiveCompany, input: CompanyFeedbackInput): Promise<void> {
  115. const runtime = employeeOf(live, input.employee);
  116. if (!runtime) throw new Error(`公司里没有叫「${input.employee}」的员工`);
  117. if (input.kind !== "praise" && input.kind !== "critique") {
  118. throw new Error("反馈只能是表扬(praise)或批评(critique)");
  119. }
  120. const note = typeof input.note === "string" ? input.note.trim() : "";
  121. if (note.length > MAX_NOTE_CHARS)
  122. throw new Error(`说明太长了,最多 ${String(MAX_NOTE_CHARS)} 个字`);
  123. // 批评不给话,员工不知道该改什么:这一条必须写。
  124. if (input.kind === "critique" && !note) {
  125. throw new Error("批评要写清哪里做得不对,他才知道改什么");
  126. }
  127. countsOf(live, input.employee)[input.kind] += 1;
  128. await record(live, runtime, {
  129. kind: input.kind,
  130. ...(note ? { note } : {}),
  131. ...targetOf(live, input),
  132. });
  133. await trace.persist(live, {
  134. files: trace.stateFile(live),
  135. message: `feedback ${input.employee} ${input.kind}`,
  136. });
  137. settle(live, runtime);
  138. }
  139. async function review(
  140. live: LiveCompany,
  141. task: CompanyTaskRecord,
  142. accept: boolean,
  143. note?: string,
  144. ): Promise<void> {
  145. const runtime = employeeOf(live, task.employee);
  146. if (!runtime) return;
  147. const counts = countsOf(live, task.employee);
  148. if (accept) counts.accepted += 1;
  149. else counts.rejected += 1;
  150. await record(live, runtime, {
  151. kind: accept ? "praise" : "critique",
  152. origin: "review",
  153. ...(note ? { note } : {}),
  154. about: task.title,
  155. taskId: task.id,
  156. });
  157. }
  158. function settle(live: LiveCompany, runtime: EmployeeRuntime): void {
  159. const name = runtime.profile.name;
  160. const counts = countsOf(live, name);
  161. const level = progressionOf(live, name, runtime).level;
  162. if (level <= counts.level) return;
  163. counts.level = level;
  164. const text = `🎉 ${name} 升到 Lv.${String(level)}(${rankOf(level)})`;
  165. const groups = live.record.conversations.filter(
  166. (conversation) => conversation.kind === "group" && conversation.members.includes(name),
  167. );
  168. for (const conversation of groups) routing.post(live, conversation.id, text);
  169. // 不在任何群里:在他和负责人的私聊里说一声。
  170. if (groups.length === 0) routing.postTo(live, name, text);
  171. trace.persistLater(live, {
  172. files: trace.stateFile(live),
  173. message: `employee ${name} 升到 Lv.${String(level)}`,
  174. });
  175. }
  176. function baseline(live: LiveCompany): boolean {
  177. let changed = false;
  178. for (const profile of live.company?.employees ?? []) {
  179. const runtime = employeeOf(live, profile.name);
  180. if (!runtime || live.record.growth[profile.name]) continue;
  181. live.record.growth[profile.name] = {
  182. ...createGrowthRecord(),
  183. level: progressionOf(live, profile.name, runtime).level,
  184. };
  185. changed = true;
  186. }
  187. return changed;
  188. }
  189. return { lines, give, review, settle, baseline };
  190. }