-
-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathPromptToCodeCustomization.swift
More file actions
136 lines (121 loc) · 4.04 KB
/
PromptToCodeCustomization.swift
File metadata and controls
136 lines (121 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import ComposableArchitecture
import Dependencies
import Foundation
import ModificationBasic
import SuggestionBasic
import SwiftUI
public enum PromptToCodeCustomization {
public static var CustomizedUI: any PromptToCodeCustomizedUI = NoPromptToCodeCustomizedUI()
}
public struct PromptToCodeCustomizationContextWrapper<Content: View>: View {
@State var context: AnyObject
let content: (AnyObject) -> Content
init<O: AnyObject>(context: O, @ViewBuilder content: @escaping (O) -> Content) {
self.context = context
self.content = { context in
content(context as! O)
}
}
public var body: some View {
content(context)
}
}
public protocol PromptToCodeCustomizedUI {
typealias PromptToCodeCustomizedViews = (
extraMenuItems: AnyView?,
extraButtons: AnyView?,
extraAcceptButtonVariants: AnyView?,
inputField: AnyView?
)
func callAsFunction<V: View>(
state: Shared<ModificationState>,
isInputFieldFocused: Binding<Bool>,
@ViewBuilder view: @escaping (PromptToCodeCustomizedViews) -> V
) -> PromptToCodeCustomizationContextWrapper<V>
}
struct NoPromptToCodeCustomizedUI: PromptToCodeCustomizedUI {
private class Context {}
func callAsFunction<V: View>(
state: Shared<ModificationState>,
isInputFieldFocused: Binding<Bool>,
@ViewBuilder view: @escaping (PromptToCodeCustomizedViews) -> V
) -> PromptToCodeCustomizationContextWrapper<V> {
PromptToCodeCustomizationContextWrapper(context: Context()) { _ in
view((
extraMenuItems: nil,
extraButtons: nil,
extraAcceptButtonVariants: nil,
inputField: nil
))
}
}
}
public struct ModificationState: Equatable {
public typealias Source = ModificationAgentRequest.ModificationSource
public var source: Source
public var history: [ModificationHistoryNode] = []
public var snippets: IdentifiedArrayOf<ModificationSnippet> = []
public var isGenerating: Bool = false
public var instruction: String
public var extraSystemPrompt: String
public var isAttachedToTarget: Bool = true
public init(
source: Source,
history: [ModificationHistoryNode] = [],
snippets: IdentifiedArrayOf<ModificationSnippet>,
instruction: String,
extraSystemPrompt: String,
isAttachedToTarget: Bool
) {
self.history = history
self.snippets = snippets
isGenerating = false
self.instruction = instruction
self.isAttachedToTarget = isAttachedToTarget
self.extraSystemPrompt = extraSystemPrompt
self.source = source
}
public init(
source: Source,
originalCode: String,
attachedRange: CursorRange,
instruction: String,
extraSystemPrompt: String
) {
self.init(
source: source,
snippets: [
.init(
startLineIndex: 0,
originalCode: originalCode,
modifiedCode: originalCode,
description: "",
error: nil,
attachedRange: attachedRange
),
],
instruction: instruction,
extraSystemPrompt: extraSystemPrompt,
isAttachedToTarget: !attachedRange.isEmpty
)
}
public mutating func popHistory() {
if !history.isEmpty {
let last = history.removeLast()
snippets = last.snippets
instruction = last.instruction
}
}
public mutating func pushHistory() {
history.append(.init(snippets: snippets, instruction: instruction))
let oldSnippets = snippets
snippets = IdentifiedArrayOf()
for var snippet in oldSnippets {
snippet.originalCode = snippet.modifiedCode
snippet.modifiedCode = ""
snippet.description = ""
snippet.error = nil
snippets.append(snippet)
}
}
}