返回 DeepSeek-Reasonix
sdk_session_call.go
根目录 / internal / plugin / sdk_session_call.go
1 package plugin
2
3 import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 )
9
10 func (t *sdkSessionTransport) call(ctx context.Context, method string, params any) (json.RawMessage, error) {
11 managed, err := t.acquire(ctx)
12 if err != nil {
13 return nil, t.sanitizeError(err, nil)
14 }
15 result, err := t.invokeManaged(ctx, managed, method, params)
16 if err == nil {
17 t.clearRuntimeError(managed)
18 return result, nil
19 }
20
21 if isExplicitMCPSessionMissing(err) || managed.session.ID() == "" && t.isStreamableHTTPNotFound(err) {
22 if managed.session.ID() == "" {
23 endpointErr := fmt.Errorf("MCP endpoint returned HTTP 404 without an established session: %w", err)
24 t.noteRuntimeError(managed, SessionErrorProtocol, endpointErr)
25 return nil, t.sanitizeError(endpointErr, managed)
26 }
27 t.noteRuntimeError(managed, SessionErrorSessionMissing, err)
28 t.invalidate(managed)
29 replacement, rebuildErr := t.acquire(ctx)
30 if rebuildErr != nil {
31 return nil, t.sanitizeError(fmt.Errorf("MCP session expired; rebuild failed: %w", rebuildErr), managed)
32 }
33 result, err = t.invokeManaged(ctx, replacement, method, params)
34 if err == nil {
35 t.clearRuntimeError(replacement)
36 return result, nil
37 }
38 return nil, t.sanitizeError(err, replacement)
39 }
40
41 if isTerminalSDKError(err) || isAmbiguousTransportError(err) || errors.Is(err, context.DeadlineExceeded) {
42 kind := SessionErrorStreamClosed
43 if errors.Is(err, context.DeadlineExceeded) {
44 kind = SessionErrorTimeout
45 } else if !isTerminalSDKError(err) {
46 kind = SessionErrorTransport
47 }
48 t.noteRuntimeError(managed, kind, err)
49 t.invalidate(managed)
50 if safeToReplayMCPMethod(method) {
51 replacement, rebuildErr := t.acquire(ctx)
52 if rebuildErr != nil {
53 return nil, t.sanitizeError(fmt.Errorf("MCP connection closed; rebuild failed: %w", rebuildErr), managed)
54 }
55 result, err = t.invokeManaged(ctx, replacement, method, params)
56 if err == nil {
57 t.clearRuntimeError(replacement)
58 return result, nil
59 }
60 return nil, t.sanitizeError(err, replacement)
61 }
62 t.startAutoReconnect()
63 return nil, t.sanitizeError(fmt.Errorf("MCP tool connection closed after dispatch; execution result is unknown and the call was not retried: %w", err), managed)
64 }
65
66 kind := classifySessionError(err)
67 t.noteRuntimeError(managed, kind, err)
68 return nil, t.sanitizeError(err, managed)
69 }
70
70 lines GO