AICompletion
AICompletion is an observable state object for one prompt and one generated response. It is useful for rewrite tools, title generators, summary panels, and other non-chat UI.
| Public state or config | Meaning |
|---|---|
input / prompt | Editable text sent to the model |
output / completion | Generated text returned by the model |
isLoading | True while generation is active |
error | Last generation error |
Constructor options | Private configuration used by send() |
State Model
@State private var completion = AICompletion(
model: "openai/gpt-4o-mini",
options: GenerationOptions(
system: "Write concise UI copy.",
temperature: 0.4
)
)prompt and completion are aliases for input and output.
Generate
completion.input = "Write a tooltip for archiving a project."
completion.send()send() clears the previous output, starts a task, and writes the final response into output.
View Example
struct RewriteView: View {
@State private var completion = AICompletion(model: "openai/gpt-4o-mini")
var body: some View {
VStack(alignment: .leading) {
TextEditor(text: $completion.prompt)
.frame(minHeight: 120)
HStack {
Button("Generate") {
completion.send()
}
.disabled(completion.isLoading || completion.prompt.isEmpty)
Button("Reset") {
completion.reset()
}
}
if completion.isLoading {
ProgressView()
}
Text(completion.completion)
}
}
}Cancellation
completion.stop()stop() cancels the active task and resets isLoading. reset() also clears input, output, and error.