extension.wit

  1package zed:extension;
  2
  3world extension {
  4    import context-server;
  5    import dap;
  6    import github;
  7    import http-client;
  8    import platform;
  9    import process;
 10    import nodejs;
 11    import llm-provider;
 12
 13    use common.{env-vars, range};
 14    use context-server.{context-server-configuration};
 15    use dap.{attach-request, build-task-template, debug-config, debug-adapter-binary, debug-task-definition, debug-request, debug-scenario, launch-request, resolved-task, start-debugging-request-arguments-request};
 16    use lsp.{completion, symbol};
 17    use process.{command};
 18    use slash-command.{slash-command, slash-command-argument-completion, slash-command-output};
 19    use llm-provider.{
 20        provider-info, model-info, completion-request,
 21        credential-type, cache-configuration, completion-event, token-usage
 22    };
 23
 24    /// Initializes the extension.
 25    export init-extension: func();
 26
 27    /// The type of a downloaded file.
 28    enum downloaded-file-type {
 29        /// A gzipped file (`.gz`).
 30        gzip,
 31        /// A gzipped tar archive (`.tar.gz`).
 32        gzip-tar,
 33        /// A ZIP file (`.zip`).
 34        zip,
 35        /// An uncompressed file.
 36        uncompressed,
 37    }
 38
 39    /// The installation status for a language server.
 40    variant language-server-installation-status {
 41        /// The language server has no installation status.
 42        none,
 43        /// The language server is being downloaded.
 44        downloading,
 45        /// The language server is checking for updates.
 46        checking-for-update,
 47        /// The language server installation failed for specified reason.
 48        failed(string),
 49    }
 50
 51    record settings-location {
 52        worktree-id: u64,
 53        path: string,
 54    }
 55
 56    import get-settings: func(path: option<settings-location>, category: string, key: option<string>) -> result<string, string>;
 57
 58    /// Downloads a file from the given URL and saves it to the given path within the extension's
 59    /// working directory.
 60    ///
 61    /// The file will be extracted according to the given file type.
 62    import download-file: func(url: string, file-path: string, file-type: downloaded-file-type) -> result<_, string>;
 63
 64    /// Makes the file at the given path executable.
 65    import make-file-executable: func(filepath: string) -> result<_, string>;
 66
 67    /// Updates the installation status for the given language server.
 68    import set-language-server-installation-status: func(language-server-name: string, status: language-server-installation-status);
 69
 70    /// A Zed worktree.
 71    resource worktree {
 72        /// Returns the ID of the worktree.
 73        id: func() -> u64;
 74        /// Returns the root path of the worktree.
 75        root-path: func() -> string;
 76        /// Returns the textual contents of the specified file in the worktree.
 77        read-text-file: func(path: string) -> result<string, string>;
 78        /// Returns the path to the given binary name, if one is present on the `$PATH`.
 79        which: func(binary-name: string) -> option<string>;
 80        /// Returns the current shell environment.
 81        shell-env: func() -> env-vars;
 82    }
 83
 84    /// A Zed project.
 85    resource project {
 86        /// Returns the IDs of all of the worktrees in this project.
 87        worktree-ids: func() -> list<u64>;
 88    }
 89
 90    /// A key-value store.
 91    resource key-value-store {
 92        /// Inserts an entry under the specified key.
 93        insert: func(key: string, value: string) -> result<_, string>;
 94    }
 95
 96    /// Returns the command used to start up the language server.
 97    export language-server-command: func(language-server-id: string, worktree: borrow<worktree>) -> result<command, string>;
 98
 99    /// Returns the initialization options to pass to the language server on startup.
100    ///
101    /// The initialization options are represented as a JSON string.
102    export language-server-initialization-options: func(language-server-id: string, worktree: borrow<worktree>) -> result<option<string>, string>;
103
104    /// Returns the workspace configuration options to pass to the language server.
105    export language-server-workspace-configuration: func(language-server-id: string, worktree: borrow<worktree>) -> result<option<string>, string>;
106
107    /// Returns the initialization options to pass to the other language server.
108    export language-server-additional-initialization-options: func(language-server-id: string, target-language-server-id: string, worktree: borrow<worktree>) -> result<option<string>, string>;
109
110    /// Returns the workspace configuration options to pass to the other language server.
111    export language-server-additional-workspace-configuration: func(language-server-id: string, target-language-server-id: string, worktree: borrow<worktree>) -> result<option<string>, string>;
112
113    /// A label containing some code.
114    record code-label {
115        /// The source code to parse with Tree-sitter.
116        code: string,
117        /// The spans to display in the label.
118        spans: list<code-label-span>,
119        /// The range of the displayed label to include when filtering.
120        filter-range: range,
121    }
122
123    /// A span within a code label.
124    variant code-label-span {
125        /// A range into the parsed code.
126        code-range(range),
127        /// A span containing a code literal.
128        literal(code-label-span-literal),
129    }
130
131    /// A span containing a code literal.
132    record code-label-span-literal {
133        /// The literal text.
134        text: string,
135        /// The name of the highlight to use for this literal.
136        highlight-name: option<string>,
137    }
138
139    export labels-for-completions: func(language-server-id: string, completions: list<completion>) -> result<list<option<code-label>>, string>;
140    export labels-for-symbols: func(language-server-id: string, symbols: list<symbol>) -> result<list<option<code-label>>, string>;
141
142
143    /// Returns the completions that should be shown when completing the provided slash command with the given query.
144    export complete-slash-command-argument: func(command: slash-command, args: list<string>) -> result<list<slash-command-argument-completion>, string>;
145
146    /// Returns the output from running the provided slash command.
147    export run-slash-command: func(command: slash-command, args: list<string>, worktree: option<borrow<worktree>>) -> result<slash-command-output, string>;
148
149    /// Returns the command used to start up a context server.
150    export context-server-command: func(context-server-id: string, project: borrow<project>) -> result<command, string>;
151
152    /// Returns the configuration for a context server.
153    export context-server-configuration: func(context-server-id: string, project: borrow<project>) -> result<option<context-server-configuration>, string>;
154
155    /// Returns a list of packages as suggestions to be included in the `/docs`
156    /// search results.
157    ///
158    /// This can be used to provide completions for known packages (e.g., from the
159    /// local project or a registry) before a package has been indexed.
160    export suggest-docs-packages: func(provider-name: string) -> result<list<string>, string>;
161
162    /// Indexes the docs for the specified package.
163    export index-docs: func(provider-name: string, package-name: string, database: borrow<key-value-store>) -> result<_, string>;
164
165    /// Returns a configured debug adapter binary for a given debug task.
166    export get-dap-binary: func(adapter-name: string, config: debug-task-definition, user-installed-path: option<string>, worktree: borrow<worktree>) -> result<debug-adapter-binary, string>;
167    /// Returns the kind of a debug scenario (launch or attach).
168    export dap-request-kind: func(adapter-name: string, config: string) -> result<start-debugging-request-arguments-request, string>;
169    export dap-config-to-scenario: func(config: debug-config) -> result<debug-scenario, string>;
170    export dap-locator-create-scenario: func(locator-name: string, build-config-template: build-task-template, resolved-label: string, debug-adapter-name: string) -> option<debug-scenario>;
171    export run-dap-locator: func(locator-name: string, config: resolved-task) -> result<debug-request, string>;
172
173    // =========================================================================
174    // Language Model Provider Extension API
175    // =========================================================================
176
177    /// Returns information about language model providers offered by this extension.
178    export llm-providers: func() -> list<provider-info>;
179
180    /// Returns the models available for a provider.
181    export llm-provider-models: func(provider-id: string) -> result<list<model-info>, string>;
182
183    /// Returns markdown content to display in the provider's settings UI.
184    /// This can include setup instructions, links to documentation, etc.
185    export llm-provider-settings-markdown: func(provider-id: string) -> option<string>;
186
187    /// Check if the provider is authenticated.
188    export llm-provider-is-authenticated: func(provider-id: string) -> bool;
189
190    /// Attempt to authenticate the provider.
191    export llm-provider-authenticate: func(provider-id: string) -> result<_, string>;
192
193    /// Reset credentials for the provider.
194    export llm-provider-reset-credentials: func(provider-id: string) -> result<_, string>;
195
196    /// Count tokens for a request.
197    export llm-count-tokens: func(
198        provider-id: string,
199        model-id: string,
200        request: completion-request
201    ) -> result<u64, string>;
202
203    /// Start streaming a completion from the model.
204    /// Returns a stream ID that can be used with llm-stream-next and llm-stream-close.
205    export llm-stream-completion-start: func(
206        provider-id: string,
207        model-id: string,
208        request: completion-request
209    ) -> result<string, string>;
210
211    /// Get the next event from a completion stream.
212    /// Returns None when the stream is complete.
213    export llm-stream-completion-next: func(
214        stream-id: string
215    ) -> result<option<completion-event>, string>;
216
217    /// Close a completion stream and release its resources.
218    export llm-stream-completion-close: func(
219        stream-id: string
220    );
221
222    /// Get cache configuration for a model (if prompt caching is supported).
223    export llm-cache-configuration: func(
224        provider-id: string,
225        model-id: string
226    ) -> option<cache-configuration>;
227
228    // =========================================================================
229    // Language Model Provider Imports (callable by extensions)
230    // =========================================================================
231
232    /// Request a credential from the user.
233    /// Returns true if the credential was provided, false if the user cancelled.
234    import llm-request-credential: func(
235        provider-id: string,
236        credential-type: credential-type,
237        label: string,
238        placeholder: string
239    ) -> result<bool, string>;
240
241    /// Get a stored credential for this provider.
242    import llm-get-credential: func(provider-id: string) -> option<string>;
243
244    /// Store a credential for this provider.
245    import llm-store-credential: func(provider-id: string, value: string) -> result<_, string>;
246
247    /// Delete a stored credential for this provider.
248    import llm-delete-credential: func(provider-id: string) -> result<_, string>;
249
250    /// Read an environment variable.
251    import llm-get-env-var: func(name: string) -> option<string>;
252}