Define prompt execution contract
This commit is contained in:
80
internal/promptexec/copy.go
Normal file
80
internal/promptexec/copy.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package promptexec
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func copyPreparation(value Preparation) Preparation {
|
||||
value.InputHashes = copyStringMap(value.InputHashes)
|
||||
return value
|
||||
}
|
||||
|
||||
func copyPreparationDebug(value *PreparationDebug) *PreparationDebug {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := *value
|
||||
copy.RenderedMessages = append([]RenderedMessage(nil), value.RenderedMessages...)
|
||||
copy.StructuredSchema = append([]byte(nil), value.StructuredSchema...)
|
||||
copy.ParametersJSON = append([]byte(nil), value.ParametersJSON...)
|
||||
return ©
|
||||
}
|
||||
|
||||
func copyExecution(value Execution) Execution {
|
||||
value.InputHashes = copyStringMap(value.InputHashes)
|
||||
value.Validation.Diagnostics = boundDiagnostics(value.Validation.Diagnostics)
|
||||
value.RawOutput = append([]byte(nil), value.RawOutput...)
|
||||
value.Debug = copyExecutionDebug(value.Debug)
|
||||
return value
|
||||
}
|
||||
|
||||
func copyExecutionDebug(value *ExecutionDebug) *ExecutionDebug {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := *value
|
||||
copy.RawOutput = append([]byte(nil), value.RawOutput...)
|
||||
copy.ValidationDiagnostics = boundDiagnostics(value.ValidationDiagnostics)
|
||||
return ©
|
||||
}
|
||||
|
||||
func copyStringMap(value map[string]string) map[string]string {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
copy := make(map[string]string, len(value))
|
||||
for key, item := range value {
|
||||
copy[key] = item
|
||||
}
|
||||
return copy
|
||||
}
|
||||
|
||||
func boundDiagnostics(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
if len(values) > maxValidationDiagnostics {
|
||||
values = values[:maxValidationDiagnostics]
|
||||
}
|
||||
bounded := make([]string, len(values))
|
||||
for index, value := range values {
|
||||
bounded[index] = boundText(value, maxDiagnosticBytes)
|
||||
}
|
||||
return bounded
|
||||
}
|
||||
|
||||
func boundText(value string, limit int) string {
|
||||
if limit <= 0 || value == "" {
|
||||
return ""
|
||||
}
|
||||
value = strings.ToValidUTF8(value, "<22>")
|
||||
if len(value) <= limit {
|
||||
return value
|
||||
}
|
||||
value = value[:limit]
|
||||
for len(value) > 0 && !utf8.ValidString(value) {
|
||||
value = value[:len(value)-1]
|
||||
}
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user