There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
323 lines
9.4 KiB
Rust
323 lines
9.4 KiB
Rust
mod model;
|
|
mod rate_limiter;
|
|
mod registry;
|
|
mod request;
|
|
mod role;
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
pub mod fake_provider;
|
|
|
|
use anyhow::Result;
|
|
use futures::FutureExt;
|
|
use futures::{future::BoxFuture, stream::BoxStream, StreamExt, TryStreamExt as _};
|
|
use gpui::{AnyElement, AnyView, App, AsyncAppContext, SharedString, Task, Window};
|
|
pub use model::*;
|
|
use proto::Plan;
|
|
pub use rate_limiter::*;
|
|
pub use registry::*;
|
|
pub use request::*;
|
|
pub use role::*;
|
|
use schemars::JsonSchema;
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
use std::fmt;
|
|
use std::{future::Future, sync::Arc};
|
|
use ui::IconName;
|
|
|
|
pub const ZED_CLOUD_PROVIDER_ID: &str = "zed.dev";
|
|
|
|
pub fn init(cx: &mut App) {
|
|
registry::init(cx);
|
|
}
|
|
|
|
/// The availability of a [`LanguageModel`].
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
pub enum LanguageModelAvailability {
|
|
/// The language model is available to the general public.
|
|
Public,
|
|
/// The language model is available to users on the indicated plan.
|
|
RequiresPlan(Plan),
|
|
}
|
|
|
|
/// Configuration for caching language model messages.
|
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
|
|
pub struct LanguageModelCacheConfiguration {
|
|
pub max_cache_anchors: usize,
|
|
pub should_speculate: bool,
|
|
pub min_total_token: usize,
|
|
}
|
|
|
|
/// A completion event from a language model.
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
|
pub enum LanguageModelCompletionEvent {
|
|
Stop(StopReason),
|
|
Text(String),
|
|
ToolUse(LanguageModelToolUse),
|
|
StartMessage { message_id: String },
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum StopReason {
|
|
EndTurn,
|
|
MaxTokens,
|
|
ToolUse,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
|
|
pub struct LanguageModelToolUseId(Arc<str>);
|
|
|
|
impl fmt::Display for LanguageModelToolUseId {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
impl<T> From<T> for LanguageModelToolUseId
|
|
where
|
|
T: Into<Arc<str>>,
|
|
{
|
|
fn from(value: T) -> Self {
|
|
Self(value.into())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
|
|
pub struct LanguageModelToolUse {
|
|
pub id: LanguageModelToolUseId,
|
|
pub name: String,
|
|
pub input: serde_json::Value,
|
|
}
|
|
|
|
pub struct LanguageModelTextStream {
|
|
pub message_id: Option<String>,
|
|
pub stream: BoxStream<'static, Result<String>>,
|
|
}
|
|
|
|
impl Default for LanguageModelTextStream {
|
|
fn default() -> Self {
|
|
Self {
|
|
message_id: None,
|
|
stream: Box::pin(futures::stream::empty()),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait LanguageModel: Send + Sync {
|
|
fn id(&self) -> LanguageModelId;
|
|
fn name(&self) -> LanguageModelName;
|
|
/// If None, falls back to [LanguageModelProvider::icon]
|
|
fn icon(&self) -> Option<IconName> {
|
|
None
|
|
}
|
|
fn provider_id(&self) -> LanguageModelProviderId;
|
|
fn provider_name(&self) -> LanguageModelProviderName;
|
|
fn telemetry_id(&self) -> String;
|
|
|
|
fn api_key(&self, _cx: &App) -> Option<String> {
|
|
None
|
|
}
|
|
|
|
/// Returns the availability of this language model.
|
|
fn availability(&self) -> LanguageModelAvailability {
|
|
LanguageModelAvailability::Public
|
|
}
|
|
|
|
fn max_token_count(&self) -> usize;
|
|
fn max_output_tokens(&self) -> Option<u32> {
|
|
None
|
|
}
|
|
|
|
fn count_tokens(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
cx: &App,
|
|
) -> BoxFuture<'static, Result<usize>>;
|
|
|
|
fn stream_completion(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
cx: &AsyncAppContext,
|
|
) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>>;
|
|
|
|
fn stream_completion_text(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
cx: &AsyncAppContext,
|
|
) -> BoxFuture<'static, Result<LanguageModelTextStream>> {
|
|
let events = self.stream_completion(request, cx);
|
|
|
|
async move {
|
|
let mut events = events.await?.fuse();
|
|
let mut message_id = None;
|
|
let mut first_item_text = None;
|
|
|
|
if let Some(first_event) = events.next().await {
|
|
match first_event {
|
|
Ok(LanguageModelCompletionEvent::StartMessage { message_id: id }) => {
|
|
message_id = Some(id.clone());
|
|
}
|
|
Ok(LanguageModelCompletionEvent::Text(text)) => {
|
|
first_item_text = Some(text);
|
|
}
|
|
_ => (),
|
|
}
|
|
}
|
|
|
|
let stream = futures::stream::iter(first_item_text.map(Ok))
|
|
.chain(events.filter_map(|result| async move {
|
|
match result {
|
|
Ok(LanguageModelCompletionEvent::StartMessage { .. }) => None,
|
|
Ok(LanguageModelCompletionEvent::Text(text)) => Some(Ok(text)),
|
|
Ok(LanguageModelCompletionEvent::Stop(_)) => None,
|
|
Ok(LanguageModelCompletionEvent::ToolUse(_)) => None,
|
|
Err(err) => Some(Err(err)),
|
|
}
|
|
}))
|
|
.boxed();
|
|
|
|
Ok(LanguageModelTextStream { message_id, stream })
|
|
}
|
|
.boxed()
|
|
}
|
|
|
|
fn use_any_tool(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
name: String,
|
|
description: String,
|
|
schema: serde_json::Value,
|
|
cx: &AsyncAppContext,
|
|
) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>>;
|
|
|
|
fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
|
|
None
|
|
}
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
fn as_fake(&self) -> &fake_provider::FakeLanguageModel {
|
|
unimplemented!()
|
|
}
|
|
}
|
|
|
|
impl dyn LanguageModel {
|
|
pub fn use_tool<T: LanguageModelTool>(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
cx: &AsyncAppContext,
|
|
) -> impl 'static + Future<Output = Result<T>> {
|
|
let schema = schemars::schema_for!(T);
|
|
let schema_json = serde_json::to_value(&schema).unwrap();
|
|
let stream = self.use_any_tool(request, T::name(), T::description(), schema_json, cx);
|
|
async move {
|
|
let stream = stream.await?;
|
|
let response = stream.try_collect::<String>().await?;
|
|
Ok(serde_json::from_str(&response)?)
|
|
}
|
|
}
|
|
|
|
pub fn use_tool_stream<T: LanguageModelTool>(
|
|
&self,
|
|
request: LanguageModelRequest,
|
|
cx: &AsyncAppContext,
|
|
) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
|
|
let schema = schemars::schema_for!(T);
|
|
let schema_json = serde_json::to_value(&schema).unwrap();
|
|
self.use_any_tool(request, T::name(), T::description(), schema_json, cx)
|
|
}
|
|
}
|
|
|
|
pub trait LanguageModelTool: 'static + DeserializeOwned + JsonSchema {
|
|
fn name() -> String;
|
|
fn description() -> String;
|
|
}
|
|
|
|
pub trait LanguageModelProvider: 'static {
|
|
fn id(&self) -> LanguageModelProviderId;
|
|
fn name(&self) -> LanguageModelProviderName;
|
|
fn icon(&self) -> IconName {
|
|
IconName::ZedAssistant
|
|
}
|
|
fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>>;
|
|
fn load_model(&self, _model: Arc<dyn LanguageModel>, _cx: &App) {}
|
|
fn is_authenticated(&self, cx: &App) -> bool;
|
|
fn authenticate(&self, cx: &mut App) -> Task<Result<()>>;
|
|
fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView;
|
|
fn must_accept_terms(&self, _cx: &App) -> bool {
|
|
false
|
|
}
|
|
fn render_accept_terms(
|
|
&self,
|
|
_view: LanguageModelProviderTosView,
|
|
_cx: &mut App,
|
|
) -> Option<AnyElement> {
|
|
None
|
|
}
|
|
fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>>;
|
|
}
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
pub enum LanguageModelProviderTosView {
|
|
ThreadEmptyState,
|
|
PromptEditorPopup,
|
|
Configuration,
|
|
}
|
|
|
|
pub trait LanguageModelProviderState: 'static {
|
|
type ObservableEntity;
|
|
|
|
fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>>;
|
|
|
|
fn subscribe<T: 'static>(
|
|
&self,
|
|
cx: &mut gpui::Context<T>,
|
|
callback: impl Fn(&mut T, &mut gpui::Context<T>) + 'static,
|
|
) -> Option<gpui::Subscription> {
|
|
let entity = self.observable_entity()?;
|
|
Some(cx.observe(&entity, move |this, _, cx| {
|
|
callback(this, cx);
|
|
}))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
|
pub struct LanguageModelId(pub SharedString);
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
|
pub struct LanguageModelName(pub SharedString);
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
|
pub struct LanguageModelProviderId(pub SharedString);
|
|
|
|
#[derive(Clone, Eq, PartialEq, Hash, Debug, Ord, PartialOrd)]
|
|
pub struct LanguageModelProviderName(pub SharedString);
|
|
|
|
impl fmt::Display for LanguageModelProviderId {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", self.0)
|
|
}
|
|
}
|
|
|
|
impl From<String> for LanguageModelId {
|
|
fn from(value: String) -> Self {
|
|
Self(SharedString::from(value))
|
|
}
|
|
}
|
|
|
|
impl From<String> for LanguageModelName {
|
|
fn from(value: String) -> Self {
|
|
Self(SharedString::from(value))
|
|
}
|
|
}
|
|
|
|
impl From<String> for LanguageModelProviderId {
|
|
fn from(value: String) -> Self {
|
|
Self(SharedString::from(value))
|
|
}
|
|
}
|
|
|
|
impl From<String> for LanguageModelProviderName {
|
|
fn from(value: String) -> Self {
|
|
Self(SharedString::from(value))
|
|
}
|
|
}
|