vscode.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type { WebviewApi } from "vscode-webview"
  2. import { MaybeTypedWebviewMessage as WebviewMessage } from "@roo/WebviewMessage" // kilocode_change - using MaybeTypedWebviewMessage
  3. /**
  4. * A utility wrapper around the acquireVsCodeApi() function, which enables
  5. * message passing and state management between the webview and extension
  6. * contexts.
  7. *
  8. * This utility also enables webview code to be run in a web browser-based
  9. * dev server by using native web browser features that mock the functionality
  10. * enabled by acquireVsCodeApi.
  11. */
  12. class VSCodeAPIWrapper {
  13. private readonly vsCodeApi: WebviewApi<unknown> | undefined
  14. constructor() {
  15. // Check if the acquireVsCodeApi function exists in the current development
  16. // context (i.e. VS Code development window or web browser)
  17. if (typeof acquireVsCodeApi === "function") {
  18. this.vsCodeApi = acquireVsCodeApi()
  19. }
  20. }
  21. /**
  22. * Post a message (i.e. send arbitrary data) to the owner of the webview.
  23. *
  24. * @remarks When running webview code inside a web browser, postMessage will instead
  25. * log the given message to the console.
  26. *
  27. * @param message Arbitrary data (must be JSON serializable) to send to the extension context.
  28. */
  29. public postMessage(message: WebviewMessage) {
  30. if (this.vsCodeApi) {
  31. this.vsCodeApi.postMessage(message)
  32. } else {
  33. console.log(message)
  34. }
  35. }
  36. /**
  37. * Get the persistent state stored for this webview.
  38. *
  39. * @remarks When running webview source code inside a web browser, getState will retrieve state
  40. * from local storage (https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
  41. *
  42. * @return The current state or `undefined` if no state has been set.
  43. */
  44. public getState(): unknown | undefined {
  45. if (this.vsCodeApi) {
  46. return this.vsCodeApi.getState()
  47. } else {
  48. const state = localStorage.getItem("vscodeState")
  49. return state ? JSON.parse(state) : undefined
  50. }
  51. }
  52. /**
  53. * Set the persistent state stored for this webview.
  54. *
  55. * @remarks When running webview source code inside a web browser, setState will set the given
  56. * state using local storage (https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage).
  57. *
  58. * @param newState New persisted state. This must be a JSON serializable object. Can be retrieved
  59. * using {@link getState}.
  60. *
  61. * @return The new state.
  62. */
  63. public setState<T extends unknown | undefined>(newState: T): T {
  64. if (this.vsCodeApi) {
  65. return this.vsCodeApi.setState(newState)
  66. } else {
  67. localStorage.setItem("vscodeState", JSON.stringify(newState))
  68. return newState
  69. }
  70. }
  71. }
  72. // Exports class singleton to prevent multiple invocations of acquireVsCodeApi.
  73. export const vscode = new VSCodeAPIWrapper()
  74. // kilocode_change start
  75. // Make vscode available globally - this allows the playwright tests
  76. // to post messages directly so we can setup provider credentials
  77. // without having to go through the Settings UI in every test.
  78. if (typeof window !== "undefined") {
  79. ;(window as unknown as { vscode: VSCodeAPIWrapper }).vscode = vscode
  80. }
  81. // kilocode_change end