prompt.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import type { E2EWindow } from "./terminal"
  2. export type PromptProbeState = {
  3. popover: "at" | "slash" | null
  4. slash: {
  5. active: string | null
  6. ids: string[]
  7. }
  8. selected: string | null
  9. selects: number
  10. }
  11. export type PromptSendState = {
  12. started: number
  13. count: number
  14. sessionID?: string
  15. directory?: string
  16. }
  17. export const promptEnabled = () => {
  18. if (typeof window === "undefined") return false
  19. return (window as E2EWindow).__opencode_e2e?.prompt?.enabled === true
  20. }
  21. const root = () => {
  22. if (!promptEnabled()) return
  23. return (window as E2EWindow).__opencode_e2e?.prompt
  24. }
  25. export const promptProbe = {
  26. set(input: Omit<PromptProbeState, "selected" | "selects">) {
  27. const state = root()
  28. if (!state) return
  29. state.current = {
  30. popover: input.popover,
  31. slash: {
  32. active: input.slash.active,
  33. ids: [...input.slash.ids],
  34. },
  35. selected: state.current?.selected ?? null,
  36. selects: state.current?.selects ?? 0,
  37. }
  38. },
  39. select(id: string) {
  40. const state = root()
  41. if (!state) return
  42. const prev = state.current
  43. state.current = {
  44. popover: prev?.popover ?? null,
  45. slash: {
  46. active: prev?.slash.active ?? null,
  47. ids: [...(prev?.slash.ids ?? [])],
  48. },
  49. selected: id,
  50. selects: (prev?.selects ?? 0) + 1,
  51. }
  52. },
  53. clear() {
  54. const state = root()
  55. if (!state) return
  56. state.current = undefined
  57. },
  58. start() {
  59. const state = root()
  60. if (!state) return
  61. state.sent = {
  62. started: (state.sent?.started ?? 0) + 1,
  63. count: state.sent?.count ?? 0,
  64. sessionID: state.sent?.sessionID,
  65. directory: state.sent?.directory,
  66. }
  67. },
  68. submit(input: { sessionID: string; directory: string }) {
  69. const state = root()
  70. if (!state) return
  71. state.sent = {
  72. started: state.sent?.started ?? 0,
  73. count: (state.sent?.count ?? 0) + 1,
  74. sessionID: input.sessionID,
  75. directory: input.directory,
  76. }
  77. },
  78. }