presentation.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Tool render-intent vocabulary: the provider-neutral types a tool declares via
  3. * `ToolDefinition.presentCall`/`ToolDefinition.presentResult` to say how one of its calls
  4. * renders in a UI (an editor's tool-call card, a CLI log line).
  5. * @module @deepseek-ai/dsh-tools/src/presentation
  6. */
  7. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  8. /**
  9. * Category of a tool call, used by a UI to pick an icon or treatment. The
  10. * provider-neutral vocabulary lets tools describe themselves without depending
  11. * on a particular client; `other` is the default.
  12. */
  13. export type ToolCallKind = 'read' | 'edit' | 'delete' | 'move' | 'search' | 'execute' | 'fetch' | 'other'
  14. /**
  15. * A file location a tool reads or modifies, so a capable UI can "follow along" —
  16. * highlight or jump to the file (and line) as the tool runs. `path` is what the
  17. * tool operated on (the model-facing path); `line` is an optional 1-based line
  18. * to focus (e.g. a read's offset).
  19. */
  20. export interface FileLocation {
  21. path: string
  22. line?: number
  23. }
  24. /**
  25. * A single-file change a tool is about to make, for a UI that renders inline
  26. * diffs. `oldText` is `null` for a new-file create (nothing to diff against);
  27. * an overwrite also uses `null`, because a call-time presenter has no access to
  28. * the file's prior content.
  29. */
  30. export interface FileDiff {
  31. path: string
  32. /** Prior content, or `null` for a new file / an overwrite (no prior content available at call time). */
  33. oldText: string | null
  34. /** Content after the change. */
  35. newText: string
  36. }
  37. /**
  38. * Provider-neutral pending-call presentation. Tools declare one tagged intent;
  39. * UI bridges map it without special-casing tool names.
  40. */
  41. export type ToolCallView = GenericCallView | TerminalCallView | DiffCallView
  42. /**
  43. * The default card: a titled tool-call row with an optional category icon, a
  44. * salient raw input, extra content blocks, and follow-along file locations. Any
  45. * tool whose call is not a terminal or a diff uses this.
  46. */
  47. export interface GenericCallView {
  48. card: 'generic'
  49. /**
  50. * Human-readable, always-visible label describing what THIS call does. Keep it
  51. * short — a UI shows it as a card header / log line.
  52. */
  53. title: string
  54. /** Category for icon/treatment; defaults to `other` when omitted. */
  55. kind?: ToolCallKind
  56. /**
  57. * The salient input to surface in a detail/expanded view (e.g. a background
  58. * task id). Omit to show nothing; a string renders as-is, an object as pretty
  59. * JSON. NOT the full raw args object unless that is genuinely what a reader wants.
  60. */
  61. rawInput?: unknown
  62. /**
  63. * UI-facing content blocks to show on the pending call alongside the title.
  64. * Omit to show none. A UI maps these to its own content blocks.
  65. */
  66. content?: ContentBlock[]
  67. /** Files this call reads/modifies, for editor follow-along. Omit for a call that touches no file. */
  68. locations?: FileLocation[]
  69. }
  70. /**
  71. * A call that IS a shell command running in a working directory: a capable UI
  72. * renders it as a terminal card (cwd-headed, with the command as the title and
  73. * live/afterward output from the {@link TerminalResultView}); an incapable UI
  74. * falls back to a generic card whose body is the fenced command output. Set by a
  75. * tool whose call is a foreground command (e.g. `bash`).
  76. */
  77. export interface TerminalCallView {
  78. card: 'terminal'
  79. /** The command, shown as the terminal card's title / header line. */
  80. title: string
  81. /**
  82. * A human-readable one-line summary of what the command does, rendered ABOVE
  83. * the terminal card (the card itself has no description slot). Omit for none.
  84. */
  85. description?: string
  86. /**
  87. * Working directory the command runs in, shown as the terminal header. An
  88. * ABSOLUTE path is used as-is; a RELATIVE path is resolved by the UI bridge
  89. * against the session workspace (the pure presenter can't see the session cwd).
  90. * Omit entirely to let the bridge use the session workspace.
  91. */
  92. cwd?: string
  93. }
  94. /**
  95. * A call that creates or modifies files, rendered as an inline diff card by a
  96. * capable UI. Set by a tool whose call writes/edits a file (e.g. `write`,
  97. * `edit`). The diffs are derived from the call ARGUMENTS (a create's `oldText` is
  98. * `null`); the tool emits a separate {@link DiffResultView} after `execute` — the
  99. * applied change (an edit/overwrite hunk with context, or a whole-file diff for a
  100. * create).
  101. */
  102. export interface DiffCallView {
  103. card: 'diff'
  104. /** Card header (e.g. `Write foo.txt`). */
  105. title: string
  106. /** One entry per file the call changes. */
  107. diffs: FileDiff[]
  108. /** Files this call modifies, for editor follow-along (usually the diffs' paths). */
  109. locations?: FileLocation[]
  110. }
  111. /**
  112. * How a tool wants the COMPLETED call shown — the *result* state, after `execute`
  113. * returns. A `card`-tagged union mirroring {@link ToolCallView}: a UI switches on
  114. * `card`. Lets the tool reformat its result for a UI distinctly from the
  115. * model-facing text it returned from `execute`. Returned by
  116. * `ToolDefinition.presentResult`; omitting the method keeps the pending
  117. * title and renders the raw result content.
  118. */
  119. export type ToolResultView = GenericResultView | TerminalResultView | DiffResultView
  120. /**
  121. * The default completed card: an optional replacement title and reformatted
  122. * content. Omit a field to keep the pending title / render the raw result content.
  123. */
  124. export interface GenericResultView {
  125. card: 'generic'
  126. /** Replacement title for the completed call. Omit to keep the pending-state title. */
  127. title?: string
  128. /**
  129. * UI-facing result content (harness {@link ContentBlock}s), reformatted from
  130. * the model-facing result. Omit to let the UI render the raw result content.
  131. */
  132. content?: ContentBlock[]
  133. }
  134. /**
  135. * The completed state of a {@link TerminalCallView}: the captured output and exit
  136. * status. A capable UI renders `output` in the terminal card and shows an
  137. * exit-status pill; an incapable UI gets a fenced ```console fallback the BRIDGE
  138. * derives from `output` (the tool does not double-encode it).
  139. */
  140. export interface TerminalResultView {
  141. card: 'terminal'
  142. /** Replacement title for the completed call. Omit to keep the pending-state title. */
  143. title?: string
  144. /** Captured command output (stdout+stderr as the tool chooses to combine them). */
  145. output?: string
  146. /**
  147. * Process exit code, when the run ended by exiting (not a signal). Lets a
  148. * capable UI show an exit-status pill. Omit when killed by a signal or unknown.
  149. */
  150. exitCode?: number
  151. /** Signal name that killed the process (e.g. `SIGTERM`). Mutually exclusive with `exitCode`. */
  152. signal?: string
  153. }
  154. /**
  155. * A completed file mutation rendered as an inline diff card, the result-time
  156. * analogue of {@link DiffCallView}. Because a completed UI update replaces the
  157. * pending card content, mutation tools return this even when it repeats the
  158. * call-time diff; otherwise raw result text would replace the diff.
  159. */
  160. export interface DiffResultView {
  161. card: 'diff'
  162. /** Replacement title for the completed call. Omit to keep the pending-state title. */
  163. title?: string
  164. /** The change to show, in file order — applied contextual hunks, or a whole-file diff when there is no before-image. */
  165. diffs: FileDiff[]
  166. }