handleChatProcessing handles the main chat processing logic
(currentFlags *Flags, registry *core.PluginRegistry, messageTools string)
| 19 | |
| 20 | // handleChatProcessing handles the main chat processing logic |
| 21 | func handleChatProcessing(currentFlags *Flags, registry *core.PluginRegistry, messageTools string) (err error) { |
| 22 | if messageTools != "" { |
| 23 | currentFlags.AppendMessage(messageTools) |
| 24 | } |
| 25 | // Check for pattern-specific model via environment variable |
| 26 | if currentFlags.Pattern != "" && currentFlags.Model == "" { |
| 27 | envVar := "FABRIC_MODEL_" + strings.ToUpper(strings.ReplaceAll(currentFlags.Pattern, "-", "_")) |
| 28 | if modelSpec := os.Getenv(envVar); modelSpec != "" { |
| 29 | parts := strings.SplitN(modelSpec, "|", 2) |
| 30 | if len(parts) == 2 { |
| 31 | currentFlags.Vendor = parts[0] |
| 32 | currentFlags.Model = parts[1] |
| 33 | } else { |
| 34 | currentFlags.Model = modelSpec |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | var chatter *core.Chatter |
| 40 | if chatter, err = registry.GetChatter(currentFlags.Model, currentFlags.ModelContextLength, |
| 41 | currentFlags.Vendor, currentFlags.Stream, currentFlags.DryRun); err != nil { |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | var session *fsdb.Session |
| 46 | var chatReq *domain.ChatRequest |
| 47 | if chatReq, err = currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " ")); err != nil { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | if chatReq.Language == "" { |
| 52 | chatReq.Language = registry.Language.DefaultLanguage.Value |
| 53 | } |
| 54 | var chatOptions *domain.ChatOptions |
| 55 | if chatOptions, err = currentFlags.BuildChatOptions(); err != nil { |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // Check if user is requesting audio output or using a TTS model |
| 60 | isAudioOutput := currentFlags.Output != "" && IsAudioFormat(currentFlags.Output) |
| 61 | isTTSModel := isTTSModel(currentFlags.Model) |
| 62 | |
| 63 | if isTTSModel && !isAudioOutput { |
| 64 | err = fmt.Errorf("%s", fmt.Sprintf(i18n.T("tts_model_requires_audio_output"), currentFlags.Model)) |
| 65 | return |
| 66 | } |
| 67 | |
| 68 | if isAudioOutput && !isTTSModel { |
| 69 | err = fmt.Errorf("%s", fmt.Sprintf(i18n.T("audio_output_file_specified_but_not_tts_model"), currentFlags.Output, currentFlags.Model)) |
| 70 | return |
| 71 | } |
| 72 | |
| 73 | // For TTS models, check if output file already exists BEFORE processing |
| 74 | if isTTSModel && isAudioOutput { |
| 75 | outputFile := currentFlags.Output |
| 76 | // Add .wav extension if not provided |
| 77 | if filepath.Ext(outputFile) == "" { |
| 78 | outputFile += ".wav" |
no test coverage detected