CompatibleModelProvider
SUPPORT · class · Runtime value
HTTP/SSE lifecycle shared by hosts. Apps supply config and user-visible error mapping only.
A static configuration is validated and frozen in the constructor. A configuration function is called once per request instead, so credentials and models may rotate between calls.
Import
import { CompatibleModelProvider } from '@jangts/agent-core/support';import { CompatibleModelProvider } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/support, @jangts/agent-core/support/model.
Definition
class CompatibleModelProviderExtends or implements: ChatProvider.
Constructor
new CompatibleModelProvider(options: CompatibleModelProviderOptions): CompatibleModelProviderCreate a provider.
| Parameter | Type | Passing | Description |
|---|---|---|---|
options | CompatibleModelProviderOptions | Required | — |
Fields of options
| Field | Type | Required | Description |
|---|---|---|---|
config | CompatibleModelConfig | (() => CompatibleModelConfig) | Required | Read-only. Static configuration, or a factory evaluated at each request boundary. |
ports | Omit<CompatibleModelPorts, "error" | "observer"> | undefined | Optional | Read-only. Port overrides other than observation and error mapping, which have their own keys. |
observe | { readonly provider?: ProviderObserver; } | undefined | Optional | Read-only. Provider observation sink. |
errors | { readonly failure?: (failure: ModelProviderFailure) => Error; } | undefined | Optional | Read-only. Error factory for classified failures. |
Returns: CompatibleModelProvider.
Member index
| Member | Purpose |
|---|---|
stream | Stream one chat-completions response as Core model parts. The method validates the effective request and yields parts as they arrive. Whether the finished stream is acceptable is decided by the configured completion policy, which defaults to the legacy rules. A stream that ends without the end-of-stream marker is incomplete, not a partial success. |
Member details
stream
Instance method.
Stream one chat-completions response as Core model parts.
The method validates the effective request and yields parts as they arrive. Whether the finished stream is acceptable is decided by the configured completion policy, which defaults to the legacy rules. A stream that ends without the end-of-stream marker is incomplete, not a partial success.
stream(messages: readonly (ModelMessage | ProviderMessage)[], context: ModelProviderContext): AsyncIterable<ModelPart>Stream one chat-completions response as Core model parts.
The method validates the effective request and yields parts as they arrive. Whether the finished stream is acceptable is decided by the configured completion policy, which defaults to the legacy rules. A stream that ends without the end-of-stream marker is incomplete, not a partial success.
| Parameter | Type | Passing | Description |
|---|---|---|---|
messages | readonly (ModelMessage | ProviderMessage)[] | Required | — |
context | ModelProviderContext | Required | — |
Fields of context
| Field | Type | Required | Description |
|---|---|---|---|
options | Readonly<ModelOptions> | undefined | Optional | Read-only. Options prepared for this call, including function schemas and JSON mode. |
logger | SubFlowLog | undefined | Optional | Read-only. Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | Required | Read-only. Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | Optional | Read-only. Diagnostic scope of the enclosing call, when recording is enabled. |
Returns: AsyncIterable<ModelPart>.
Examples
- Compatible Provider and custom Providers: Support owns the common Chat Completions HTTP/SSE lifecycle; an App can substitute its own ChatProvider.
Connect a common protocol with real server-side configuration
Beginner · Node.js
Running this example requires connection configuration for an available model service.
import { ModelRunner, Support } from '@jangts/agent-core';
const provider = new Support.CompatibleModelProvider({ config: {
baseUrl: process.env.MODEL_BASE_URL!, apiKey: process.env.MODEL_API_KEY!, model: process.env.MODEL_NAME!,
} });
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);import { ModelRunner, Support } from '@jangts/agent-core';
const provider = new Support.CompatibleModelProvider({ config: {
baseUrl: process.env.MODEL_BASE_URL, apiKey: process.env.MODEL_API_KEY, model: process.env.MODEL_NAME,
} });
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);Declared fields and wire mapping for a direct tool thread
Configuration · Node.js
This only demonstrates message conversion, with no network request or tool execution. Raw tool_calls, tool_call_id, and host-attached data are not forwarded.
import { Support } from '@jangts/agent-core';
const messages: Support.ProviderMessage[] = [
{ role: 'user', content: '查询资料' },
{ role: 'assistant', content: '', toolCalls: [
{ id: 'call-1', name: 'lookup', arguments: '{"query":"资料"}' },
] },
{ role: 'tool', content: '{"found":true}', toolCallId: 'call-1' },
];
console.log(Support.chatCompletionMessages(messages));import { Support } from '@jangts/agent-core';
const messages = [
{ role: 'user', content: '查询资料' },
{ role: 'assistant', content: '', toolCalls: [
{ id: 'call-1', name: 'lookup', arguments: '{"query":"资料"}' },
] },
{ role: 'tool', content: '{"found":true}', toolCallId: 'call-1' },
];
console.log(Support.chatCompletionMessages(messages));Connect an offline object implementation to the same contract
Configuration · Node.js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = { async *stream(messages: readonly { content: string }[]) {
yield '离线回答:' + messages.at(-1)!.content;
} };
console.log((await new ModelRunner({ provider }).invoke('你好')).value);import { ModelRunner } from '@jangts/agent-core/model';
const provider = { async *stream(messages) {
yield '离线回答:' + messages.at(-1).content;
} };
console.log((await new ModelRunner({ provider }).invoke('你好')).value);