Class NaruModelProtocolBase
- All Implemented Interfaces:
NaruModelProtocol
- Direct Known Subclasses:
NaruModelProtocolAnthropicCompat,NaruModelProtocolGeminiNative,NaruModelProtocolOllamaNative,NaruModelProtocolOllamaOpenApiCompat,NaruModelProtocolOpenAICompat
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final NaruModelCapabilitiesprotected final Stringprotected final Stringprotected final NaruModelConfigprotected final net.thevpc.nuts.elem.NElementReaderprotected final NaruModelProviderprotected final NaruModelRequestSerializer -
Constructor Summary
ConstructorsConstructorDescriptionNaruModelProtocolBase(NaruModelProvider provider, NaruModelConfig model, String configPrefix, String chatPath, NaruModelCapabilities capabilities, NaruModelRequestSerializer serializer, net.thevpc.nuts.elem.NElementDeserializer<NaruResponse> responseParser) -
Method Summary
Modifier and TypeMethodDescriptionprotected Stringprotected Stringchat(NaruModelRequest mrequest, NaruTask task) protected StringThe endpoint to POST to, relative to the base url.protected StringclassifyStatus(net.thevpc.nuts.net.NHttpResponse response, AtomicReference<net.thevpc.nuts.time.NDuration> dynamicRetryAfter) Classifies a response status into "proceed" or "this attempt failed".protected voidcommitCacheState(NaruModelRequest request, NaruCachePlanView plan, NaruCachingMode mode, NaruTask task) Remember the prefix this turn actually sent.protected net.thevpc.nuts.time.NDurationconnectTimeout(NaruTask task, Map<String, net.thevpc.nuts.elem.NElement> env) protected intmaxRetries(NaruTask task, Map<String, net.thevpc.nuts.elem.NElement> env) protected voidonResponseReceived(net.thevpc.nuts.net.NHttpResponse response, NaruTask task) protected NaruResponseparseResponse(String json) static NaruToolCallparseXmlLikeToolCall(String input) protected voidprepareRequest(net.thevpc.nuts.net.NHttpRequest request, net.thevpc.nuts.elem.NElement body, NaruTask task) protected NaruModelRequestpreprocessRequest(NaruModelRequest mrequest, NaruTask task) provider()protected net.thevpc.nuts.time.NDurationreadTimeout(NaruTask task, Map<String, net.thevpc.nuts.elem.NElement> env) protected net.thevpc.nuts.time.NDurationretryPeriod(NaruTask task, Map<String, net.thevpc.nuts.elem.NElement> env) protected net.thevpc.nuts.elem.NElementserialize(NaruModelRequest request, NaruCachePlanView plan, NaruTask task) Serialise with the cache plan when the provider's serializer understands one, and otherwise fall back to the plain three-argument call.protected net.thevpc.nuts.elem.NElementserialize(NaruModelRequest request, NaruCachePlanView plan, NaruTask task, boolean stream) Serialise, adding the streaming flag when the body is going to be read incrementally.protected NaruResponsestreamChat(NaruModelRequest mrequest, NaruTask task, NaruStreamResponseParser parser) Runs a streamed call, retrying only while nothing has been delivered.protected StringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.thevpc.naru.api.model.NaruModelProtocol
chatStream
-
Field Details
-
nElementReader
protected final net.thevpc.nuts.elem.NElementReader nElementReader -
model
-
capabilities
-
configPrefix
-
chatPath
-
serializer
-
provider
-
-
Constructor Details
-
NaruModelProtocolBase
public NaruModelProtocolBase(NaruModelProvider provider, NaruModelConfig model, String configPrefix, String chatPath, NaruModelCapabilities capabilities, NaruModelRequestSerializer serializer, net.thevpc.nuts.elem.NElementDeserializer<NaruResponse> responseParser)
-
-
Method Details
-
provider
-
apiKey
-
apiKeyConfigKey
-
parseXmlLikeToolCall
-
url
-
chatPath
The endpoint to POST to, relative to the base url.Overridable because some wire formats put the model in the path (
models/gemini-2.5-pro:generateContent) rather than in the body, which a fixed path cannot express. -
connectTimeout
-
readTimeout
-
preprocessRequest
-
classifyStatus
protected String classifyStatus(net.thevpc.nuts.net.NHttpResponse response, AtomicReference<net.thevpc.nuts.time.NDuration> dynamicRetryAfter) Classifies a response status into "proceed" or "this attempt failed".Shared by the batched and the streamed path so that a 401, a 429 and a retryable 503 mean the same thing either way. Duplicating this is how a streaming client ends up retrying a 400 forever, or ignoring a 429 that the batched path honours.
On a 5xx
failFastis called so a retry does not have to drain an error page before reusing the connection.- Parameters:
dynamicRetryAfter- collects a server-sentRetry-Afterwhen one is present, so the retry policy can honour it- Returns:
nullwhen the response should be read normally, otherwise the error body for the caller to log and report
-
prepareRequest
protected void prepareRequest(net.thevpc.nuts.net.NHttpRequest request, net.thevpc.nuts.elem.NElement body, NaruTask task) -
serialize
protected net.thevpc.nuts.elem.NElement serialize(NaruModelRequest request, NaruCachePlanView plan, NaruTask task) Serialise with the cache plan when the provider's serializer understands one, and otherwise fall back to the plain three-argument call.Degrading silently rather than throwing is deliberate: a provider can declare caching support while reusing a shared serializer that predates it, and a user should get uncached-but-working behaviour instead of an error.
NaruCachePlanView.none()keeps the emitted body identical to what a non-segmented request always produced. -
serialize
protected net.thevpc.nuts.elem.NElement serialize(NaruModelRequest request, NaruCachePlanView plan, NaruTask task, boolean stream) Serialise, adding the streaming flag when the body is going to be read incrementally. -
onResponseReceived
-
maxRetries
-
retryPeriod
-
chat
- Specified by:
chatin interfaceNaruModelProtocol
-
providerName
- Specified by:
providerNamein interfaceNaruModelProtocol
-
streamChat
protected NaruResponse streamChat(NaruModelRequest mrequest, NaruTask task, NaruStreamResponseParser parser) Runs a streamed call, retrying only while nothing has been delivered.Retrying a stream that has already emitted tokens would deliver them twice: the renderer has already drawn them and the transcript has already recorded them, and there is no way to un-send bytes to a third party. So a retry is allowed only while the attempt produced no chunk at all, which is exactly the case a retry fixes -- a connection refused, a 503 before the first token. Once anything has been delivered, the failure is reported.
- Parameters:
parser- consumes events in order and assembles the response
-
commitCacheState
protected void commitCacheState(NaruModelRequest request, NaruCachePlanView plan, NaruCachingMode mode, NaruTask task) Remember the prefix this turn actually sent.Overridable so a provider with a stateful cache can record the server's resource id instead. Any failure here is swallowed: losing a cache hint costs tokens, and must never cost the response the user is waiting for.
-
parseResponse
-
getCapabilities
- Specified by:
getCapabilitiesin interfaceNaruModelProtocol
-