status.go 834 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package llm
  2. import (
  3. "bytes"
  4. "os"
  5. )
  6. // StatusWriter is a writer that captures error messages from the llama runner process
  7. type StatusWriter struct {
  8. LastErrMsg string
  9. out *os.File
  10. }
  11. func NewStatusWriter(out *os.File) *StatusWriter {
  12. return &StatusWriter{
  13. out: out,
  14. }
  15. }
  16. // TODO - regex matching to detect errors like
  17. // libcublasLt.so.11: cannot open shared object file: No such file or directory
  18. var errorPrefixes = []string{
  19. "error:",
  20. "CUDA error",
  21. "cudaMalloc failed",
  22. "\"ERR\"",
  23. "error loading model",
  24. }
  25. func (w *StatusWriter) Write(b []byte) (int, error) {
  26. var errMsg string
  27. for _, prefix := range errorPrefixes {
  28. if _, after, ok := bytes.Cut(b, []byte(prefix)); ok {
  29. errMsg = prefix + string(bytes.TrimSpace(after))
  30. }
  31. }
  32. if errMsg != "" {
  33. w.LastErrMsg = errMsg
  34. }
  35. return w.out.Write(b)
  36. }