1
0

index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * Session feedback event plus the human-facing `/feedback` producer. Recording
  3. * appends one authoritative log-only event and does not start model work. The
  4. * append is eager but unflushed, so acknowledgement reports that the entry is
  5. * logged, not that it reached disk.
  6. * @module @deepseek-ai/dsh-command-feedback
  7. */
  8. import type { Context } from 'cordis'
  9. import type { CommandInvocation, CommandResult } from '@deepseek-ai/dsh-commands'
  10. import type { Session } from '@deepseek-ai/dsh-session'
  11. export const name = 'command-feedback'
  12. export const inject = ['commands']
  13. const USAGE = 'Usage: /feedback <text>'
  14. declare module '@deepseek-ai/dsh-session' {
  15. interface SessionEventMap {
  16. /**
  17. * One recorded human remark about this session. Log-only and independent
  18. * of its trigger; it never enters the model surface or derived history.
  19. */
  20. 'feedback/record': { text: string }
  21. }
  22. }
  23. /**
  24. * Record feedback independently of any UI trigger.
  25. * @param session - session the feedback describes.
  26. * @param text - human-authored feedback; surrounding whitespace is discarded.
  27. * @throws {TypeError} when the normalized text is empty.
  28. */
  29. export function recordFeedback(session: Session, text: string): void {
  30. const normalized = text.trim()
  31. if (normalized.length === 0) throw new TypeError('feedback text must not be empty')
  32. session.append('feedback/record', { text: normalized })
  33. }
  34. /**
  35. * Validate, record, and acknowledge one feedback entry. Returning an error
  36. * leaves no `feedback/record` event.
  37. * @param invocation - receiving agent, raw command input, and UI cancellation.
  38. * @returns an acknowledgement, or a usage error when no feedback text was supplied.
  39. */
  40. function executeFeedbackCommand(invocation: CommandInvocation): CommandResult {
  41. if (invocation.rawInput.trim().length === 0) {
  42. return { kind: 'error', text: `Feedback text is required. ${USAGE}` }
  43. }
  44. recordFeedback(invocation.agent.session, invocation.rawInput)
  45. return { kind: 'success', text: 'Feedback recorded.' }
  46. }
  47. /** Register the global `/feedback` command for every composed command adapter. */
  48. export function apply(ctx: Context): void {
  49. ctx.commands.register({
  50. name: 'feedback',
  51. description: 'record feedback about this session',
  52. input: { hint: '<text>' },
  53. recordInput: false,
  54. handler: executeFeedbackCommand,
  55. })
  56. }