MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / wrapStreamResult

Method wrapStreamResult

sdk/cliproxy/auth/conductor.go:1741–1819  ·  view source on GitHub ↗
(ctx context.Context, auth *Auth, provider, resultModel string, headers http.Header, buffered []cliproxyexecutor.StreamChunk, remaining <-chan cliproxyexecutor.StreamChunk, aliasResult OAuthModelAliasResult)

Source from the content-addressed store, hash-verified

1739}
1740
1741func (m *Manager) wrapStreamResult(ctx context.Context, auth *Auth, provider, resultModel string, headers http.Header, buffered []cliproxyexecutor.StreamChunk, remaining <-chan cliproxyexecutor.StreamChunk, aliasResult OAuthModelAliasResult) *cliproxyexecutor.StreamResult {
1742 out := make(chan cliproxyexecutor.StreamChunk)
1743 go func() {
1744 defer close(out)
1745 var failed bool
1746 forward := true
1747 var rewriter *StreamRewriter
1748 if aliasResult.ForceMapping && strings.TrimSpace(aliasResult.OriginalAlias) != "" {
1749 rewriter = NewStreamRewriter(StreamRewriteOptions{RewriteModel: aliasResult.OriginalAlias})
1750 }
1751 emit := func(chunk cliproxyexecutor.StreamChunk) bool {
1752 if chunk.Err != nil && !failed {
1753 failed = true
1754 rerr := &Error{Message: chunk.Err.Error()}
1755 if se, ok := errors.AsType[cliproxyexecutor.StatusError](chunk.Err); ok && se != nil {
1756 rerr.HTTPStatus = se.StatusCode()
1757 }
1758 m.MarkResult(ctx, Result{AuthID: auth.ID, Provider: provider, Model: resultModel, Success: false, Error: rerr})
1759 }
1760 if !forward {
1761 return false
1762 }
1763 if chunk.Err != nil {
1764 if ctx == nil {
1765 out <- chunk
1766 return true
1767 }
1768 select {
1769 case <-ctx.Done():
1770 forward = false
1771 return false
1772 case out <- chunk:
1773 return true
1774 }
1775 }
1776 if len(chunk.Payload) == 0 {
1777 return true
1778 }
1779 payload := rewriteForceMappedStreamChunk(rewriter, chunk.Payload)
1780 if len(payload) == 0 {
1781 return true
1782 }
1783 chunk.Payload = payload
1784 if ctx == nil {
1785 out <- chunk
1786 return true
1787 }
1788 select {
1789 case <-ctx.Done():
1790 forward = false
1791 return false
1792 case out <- chunk:
1793 return true
1794 }
1795 }
1796 for _, chunk := range buffered {
1797 if ok := emit(chunk); !ok {
1798 discardStreamChunks(remaining)

Callers 1

Calls 7

MarkResultMethod · 0.95
NewStreamRewriterFunction · 0.85
discardStreamChunksFunction · 0.85
StatusCodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected