mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-13 11:40:53 +08:00
Comments, doc comments, UI labels, log and error messages, LLM prompt strings, markdown docs, test data and assertions. Identifiers, enum keys, routes, placeholders and code structure are unchanged. Notes: - docs/漏洞流量证据.md renamed to docs/vulnerability-traffic-evidence.md - tests that exercised CJK specific handling (GBK zip names, Chinese full text search) now use ASCII equivalents; go build passes and the test suite matches the pre translation baseline (remaining failures need a PostgreSQL DSN and fail identically on the original) - frontend mention syntax now uses English kinds (finding, asset, ...) matching the backend, chat mentions tests pass
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package sidequestion
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/Autumn-27/norma/llm"
|
|
)
|
|
|
|
// SideQuestionService has no harness, tool executor, transcript writer or model
|
|
// failover chain. Answer is one completion; Respond adds bounded preparation
|
|
// and at most one context-overflow recovery around that completion.
|
|
type SideQuestionService struct{ Provider llm.Provider }
|
|
|
|
type Answer struct {
|
|
Text string
|
|
Usage llm.Usage
|
|
ToolUse bool
|
|
}
|
|
|
|
func (s SideQuestionService) Answer(ctx context.Context, req llm.CompletionRequest, streaming bool, update func(Answer)) (out Answer, err error) {
|
|
if streaming {
|
|
complete := false
|
|
for ev, streamErr := range s.Provider.Stream(ctx, req) {
|
|
if streamErr != nil {
|
|
err = streamErr
|
|
break
|
|
}
|
|
switch ev.Type {
|
|
case llm.SETextDelta:
|
|
out.Text += ev.Text
|
|
case llm.SEToolUseStart:
|
|
out.ToolUse = true
|
|
case llm.SEMessageStart, llm.SEMessageDelta:
|
|
out.Usage.Add(ev.Usage)
|
|
case llm.SEMessageStop:
|
|
complete = true
|
|
}
|
|
if update != nil {
|
|
update(out)
|
|
}
|
|
if ctx.Err() != nil {
|
|
err = ctx.Err()
|
|
break
|
|
}
|
|
}
|
|
if err == nil && !complete {
|
|
err = errors.New("model response was interrupted, please ask again")
|
|
}
|
|
} else {
|
|
var msg llm.Message
|
|
msg, _, out.Usage, err = s.Provider.Complete(ctx, req)
|
|
out.Text, out.ToolUse = msg.Text(), len(msg.ToolUses()) > 0
|
|
}
|
|
if ctx.Err() != nil {
|
|
err = ctx.Err()
|
|
}
|
|
if err == nil && strings.TrimSpace(out.Text) == "" {
|
|
if out.ToolUse {
|
|
out.Text = "Side questions cannot execute tool operations; please send operation requests in the main session."
|
|
} else {
|
|
err = errors.New("model did not return an answer")
|
|
}
|
|
}
|
|
return out, err
|
|
}
|