Unified · Split
| @@ -344,7 +344,7 @@ impl AssistantPanel { · 343 unmodified lines | |||
| 344 | let assist_id = inline_assistant.read(cx).id; | 344 | let assist_id = inline_assistant.read(cx).id; |
| 345 | match event { | 345 | match event { |
| 346 | InlineAssistantEvent::Confirmed { prompt } => { | 346 | InlineAssistantEvent::Confirmed { prompt } => { |
| 347 | self.generate(assist_id, prompt, cx); | 347 | self.confirm_inline_assist(assist_id, prompt, cx); |
| 348 | } | 348 | } |
| 349 | InlineAssistantEvent::Canceled => { | 349 | InlineAssistantEvent::Canceled => { |
| 350 | self.complete_inline_assist(assist_id, true, cx); | 350 | self.complete_inline_assist(assist_id, true, cx); |
| @@ -398,7 +398,12 @@ impl AssistantPanel { · 47 unmodified lines | |||
| 398 | } | 398 | } |
| 399 | } | 399 | } |
| 400 | 400 | ||
| 401 | fn generate(&mut self, inline_assist_id: usize, user_prompt: &str, cx: &mut ViewContext<Self>) { | 401 | fn confirm_inline_assist( |
| 402 | &mut self, | ||
| 403 | inline_assist_id: usize, | ||
| 404 | user_prompt: &str, | ||
| 405 | cx: &mut ViewContext<Self>, | ||
| 406 | ) { | ||
| 402 | let api_key = if let Some(api_key) = self.api_key.borrow().clone() { | 407 | let api_key = if let Some(api_key) = self.api_key.borrow().clone() { |
| 403 | api_key | 408 | api_key |
| 404 | } else { | 409 | } else { |
| @@ -473,26 +478,51 @@ impl AssistantPanel { · 68 unmodified lines | |||
| 473 | .language_at(range.start) | 478 | .language_at(range.start) |
| 474 | .map(|language| language.name()); | 479 | .map(|language| language.name()); |
| 475 | let language_name = language_name.as_deref().unwrap_or(""); | 480 | let language_name = language_name.as_deref().unwrap_or(""); |
| 481 | let model = settings::get::<AssistantSettings>(cx) | ||
| 482 | .default_open_ai_model | ||
| 483 | .clone(); | ||
| 476 | 484 | ||
| 477 | let mut prompt = String::new(); | 485 | let mut prompt = String::new(); |
| 478 | writeln!(prompt, "You're an expert {language_name} engineer.").unwrap(); | 486 | writeln!(prompt, "You're an expert {language_name} engineer.").unwrap(); |
| 479 | writeln!( | ||
| 480 | prompt, | ||
| 481 | "You're currently working inside an editor on this code:" | ||
| 482 | ) | ||
| 483 | .unwrap(); | ||
| 484 | match pending_assist.kind { | 487 | match pending_assist.kind { |
| 485 | InlineAssistKind::Refactor => { | 488 | InlineAssistKind::Refactor => { |
| 489 | writeln!( | ||
| 490 | prompt, | ||
| 491 | "You're currently working inside an editor on this code:" | ||
| 492 | ) | ||
| 493 | .unwrap(); | ||
| 494 | writeln!(prompt, "```{language_name}").unwrap(); | ||
| 495 | for chunk in snapshot.text_for_range(Anchor::min()..Anchor::max()) { | ||
| 496 | write!(prompt, "{chunk}").unwrap(); | ||
| 497 | } | ||
| 498 | writeln!(prompt, "```").unwrap(); | ||
| 499 | |||
| 500 | writeln!( | ||
| 501 | prompt, | ||
| 502 | "In particular, the user has selected the following code:" | ||
| 503 | ) | ||
| 504 | .unwrap(); | ||
| 486 | writeln!(prompt, "```{language_name}").unwrap(); | 505 | writeln!(prompt, "```{language_name}").unwrap(); |
| 487 | writeln!(prompt, "{normalized_selected_text}").unwrap(); | 506 | writeln!(prompt, "{normalized_selected_text}").unwrap(); |
| 488 | writeln!(prompt, "```").unwrap(); | 507 | writeln!(prompt, "```").unwrap(); |
| 508 | writeln!(prompt).unwrap(); | ||
| 489 | writeln!( | 509 | writeln!( |
| 490 | prompt, | 510 | prompt, |
| 491 | "Modify the code given the user prompt: {user_prompt}" | 511 | "Modify the selected code given the user prompt: {user_prompt}" |
| 512 | ) | ||
| 513 | .unwrap(); | ||
| 514 | writeln!( | ||
| 515 | prompt, | ||
| 516 | "You MUST reply only with the edited selected code, not the entire file." | ||
| 492 | ) | 517 | ) |
| 493 | .unwrap(); | 518 | .unwrap(); |
| 494 | } | 519 | } |
| 495 | InlineAssistKind::Insert => { | 520 | InlineAssistKind::Insert => { |
| 521 | writeln!( | ||
| 522 | prompt, | ||
| 523 | "You're currently working inside an editor on this code:" | ||
| 524 | ) | ||
| 525 | .unwrap(); | ||
| 496 | writeln!(prompt, "```{language_name}").unwrap(); | 526 | writeln!(prompt, "```{language_name}").unwrap(); |
| 497 | for chunk in snapshot.text_for_range(Anchor::min()..range.start) { | 527 | for chunk in snapshot.text_for_range(Anchor::min()..range.start) { |
| 498 | write!(prompt, "{chunk}").unwrap(); | 528 | write!(prompt, "{chunk}").unwrap(); |
| @@ -517,11 +547,11 @@ impl AssistantPanel { · 18 unmodified lines | |||
| 517 | } | 547 | } |
| 518 | } | 548 | } |
| 519 | writeln!(prompt, "Your answer MUST always be valid {language_name}.").unwrap(); | 549 | writeln!(prompt, "Your answer MUST always be valid {language_name}.").unwrap(); |
| 520 | writeln!(prompt, "DO NOT wrap your response in Markdown blocks.").unwrap(); | 550 | writeln!(prompt, "Always wrap your response in a Markdown codeblock.").unwrap(); |
| 521 | writeln!(prompt, "Never make remarks, always output code.").unwrap(); | 551 | writeln!(prompt, "Never make remarks, always output code.").unwrap(); |
| 522 | 552 | ||
| 523 | let request = OpenAIRequest { | 553 | let request = OpenAIRequest { |
| 524 | model: "gpt-4".into(), | 554 | model: model.full_name().into(), |
| 525 | messages: vec![RequestMessage { | 555 | messages: vec![RequestMessage { |
| 526 | role: Role::User, | 556 | role: Role::User, |
| 527 | content: prompt, | 557 | content: prompt, |
| @@ -563,23 +593,92 @@ impl AssistantPanel { · 35 unmodified lines | |||
| 563 | indentation_text = ""; | 593 | indentation_text = ""; |
| 564 | }; | 594 | }; |
| 565 | 595 | ||
| 566 | let mut new_text = indentation_text | 596 | let mut inside_first_line = true; |
| 567 | .repeat(indentation_len.saturating_sub(selection_start.column) as usize); | 597 | let mut starts_with_fenced_code_block = None; |
| 598 | let mut has_pending_newline = false; | ||
| 599 | let mut new_text = String::new(); | ||
| 600 | |||
| 568 | while let Some(message) = messages.next().await { | 601 | while let Some(message) = messages.next().await { |
| 569 | let mut message = message?; | 602 | let mut message = message?; |
| 570 | if let Some(choice) = message.choices.pop() { | 603 | if let Some(mut choice) = message.choices.pop() { |
| 604 | if has_pending_newline { | ||
| 605 | has_pending_newline = false; | ||
| 606 | choice | ||
| 607 | .delta | ||
| 608 | .content | ||
| 609 | .get_or_insert(String::new()) | ||
| 610 | .insert(0, '\n'); | ||
| 611 | } | ||
| 612 | |||
| 613 | // Buffer a trailing codeblock fence. Note that we don't stop | ||
| 614 | // right away because this may be an inner fence that we need | ||
| 615 | // to insert into the editor. | ||
| 616 | if starts_with_fenced_code_block.is_some() | ||
| 617 | && choice.delta.content.as_deref() == Some("\n```") | ||
| 618 | { | ||
| 619 | new_text.push_str("\n```"); | ||
| 620 | continue; | ||
| 621 | } | ||
| 622 | |||
| 623 | // If this was the last completion and we started with a codeblock | ||
| 624 | // fence and we ended with another codeblock fence, then we can | ||
| 625 | // stop right away. Otherwise, whatever text we buffered will be | ||
| 626 | // processed normally. | ||
| 627 | if choice.finish_reason.is_some() | ||
| 628 | && starts_with_fenced_code_block.unwrap_or(false) | ||
| 629 | && new_text == "\n```" | ||
| 630 | { | ||
| 631 | break; | ||
| 632 | } | ||
| 633 | |||
| 571 | if let Some(text) = choice.delta.content { | 634 | if let Some(text) = choice.delta.content { |
| 635 | // Never push a newline if there's nothing after it. This is | ||
| 636 | // useful to detect if the newline was pushed because of a | ||
| 637 | // trailing codeblock fence. | ||
| 638 | let text = if let Some(prefix) = text.strip_suffix('\n') { | ||
| 639 | has_pending_newline = true; | ||
| 640 | prefix | ||
| 641 | } else { | ||
| 642 | text.as_str() | ||
| 643 | }; | ||
| 644 | |||
| 645 | if text.is_empty() { | ||
| 646 | continue; | ||
| 647 | } | ||
| 648 | |||
| 572 | let mut lines = text.split('\n'); | 649 | let mut lines = text.split('\n'); |
| 573 | if let Some(first_line) = lines.next() { | 650 | if let Some(line) = lines.next() { |
| 574 | new_text.push_str(&first_line); | 651 | if starts_with_fenced_code_block.is_none() { |
| 652 | starts_with_fenced_code_block = | ||
| 653 | Some(line.starts_with("```")); | ||
| 654 | } | ||
| 655 | |||
| 656 | // Avoid pushing the first line if it's the start of a fenced code block. | ||
| 657 | if !inside_first_line || !starts_with_fenced_code_block.unwrap() | ||
| 658 | { | ||
| 659 | new_text.push_str(&line); | ||
| 660 | } | ||
| 575 | } | 661 | } |
| 576 | 662 | ||
| 577 | for line in lines { | 663 | for line in lines { |
| 578 | new_text.push('\n'); | 664 | if inside_first_line && starts_with_fenced_code_block.unwrap() { |
| 579 | new_text.push_str( | 665 | // If we were inside the first line and that line was the |
| 580 | &indentation_text.repeat(indentation_len as usize), | 666 | // start of a fenced code block, we just need to push the |
| 581 | ); | 667 | // leading indentation of the original selection. |
| 668 | new_text.push_str(&indentation_text.repeat( | ||
| 669 | indentation_len.saturating_sub(selection_start.column) | ||
| 670 | as usize, | ||
| 671 | )); | ||
| 672 | } else { | ||
| 673 | // Otherwise, we need to push a newline and the base indentation. | ||
| 674 | new_text.push('\n'); | ||
| 675 | new_text.push_str( | ||
| 676 | &indentation_text.repeat(indentation_len as usize), | ||
| 677 | ); | ||
| 678 | } | ||
| 679 | |||
| 582 | new_text.push_str(line); | 680 | new_text.push_str(line); |
| 681 | inside_first_line = false; | ||
| 583 | } | 682 | } |
| 584 | } | 683 | } |
| 585 | } | 684 | } |