new features

This commit is contained in:
Mark Gregory
2026-11-18 08:48:24 +00:00
parent 8467977403
commit 17bcf178a9
11 changed files with 367 additions and 43 deletions
+15 -3
View File
@@ -58,6 +58,11 @@ struct Message: Identifiable, Equatable, Sendable {
var webSources: [WebReplySource] = []
var documentNames: [String] = []
var documentCitations: [DocumentCitation] = []
var imagePNG: Data? = nil
var imageDataURL: String? {
imagePNG.map { "data:image/png;base64," + $0.base64EncodedString() }
}
}
private extension Message {
@@ -77,6 +82,7 @@ struct DocumentCitation: Identifiable, Codable, Equatable, Sendable {
let excerpt: String
let documentID: UUID?
let chunkID: Int64?
var requiresFullText: Bool? = nil
var id: Int { number }
var reference: String { "[D\(number)] \(title)\n\(excerpt)" }
@@ -327,8 +333,9 @@ enum PromptBudget {
}
var history = messages.dropFirst(system.count).dropLast()
let systemCost = system.reduce(0) { $0 + estimate($1) }
while !latest.documentCitations.isEmpty && systemCost + estimate(latest) > capacity {
latest.documentCitations.removeLast()
while systemCost + estimate(latest) > capacity,
let index = latest.documentCitations.lastIndex(where: { $0.requiresFullText != true }) {
latest.documentCitations.remove(at: index)
}
var cost = systemCost + estimate(latest) + history.reduce(0) { $0 + estimate($1) }
while !history.isEmpty && (cost > capacity || history.first?.role != .user) {
@@ -338,6 +345,9 @@ enum PromptBudget {
}
}
guard cost <= capacity else {
if latest.documentCitations.contains(where: { $0.requiresFullText == true }) {
throw StudioError("The full attachment text and latest input exceed the prompt budget. Increase the context budget or choose a model with a larger context window.")
}
throw StudioError("The system instructions and latest input exceed the prompt budget.")
}
return Array(system) + history + [latest]
@@ -378,7 +388,9 @@ struct StudioPromptRequest {
let systemEnd = messages.prefix(while: { $0.role == .system }).count
messages.insert(contentsOf: rendered.dropLast(), at: systemEnd)
}
if !options.useDocuments { messages[messages.count - 1].documentCitations = [] }
if !options.useDocuments {
messages[messages.count - 1].documentCitations.removeAll { $0.requiresFullText != true }
}
}
messages = try PromptBudget.fit(messages, limit: limit, reserve: reserve)
let citations = messages.last?.documentCitations ?? []