CompatibleModelProvider
SUPPORT · class · 运行时值
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 { CompatibleModelProvider } from '@jangts/agent-core/support';import { CompatibleModelProvider } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
class CompatibleModelProvider继承或实现: ChatProvider。
构造函数
new CompatibleModelProvider(options: CompatibleModelProviderOptions): CompatibleModelProviderCreate a provider.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | CompatibleModelProviderOptions | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
config | CompatibleModelConfig | (() => CompatibleModelConfig) | 必需 | 只读。Static configuration, or a factory evaluated at each request boundary. |
ports | Omit<CompatibleModelPorts, "error" | "observer"> | undefined | 可省略 | 只读。Port overrides other than observation and error mapping, which have their own keys. |
observe | { readonly provider?: ProviderObserver; } | undefined | 可省略 | 只读。Provider observation sink. |
errors | { readonly failure?: (failure: ModelProviderFailure) => Error; } | undefined | 可省略 | 只读。Error factory for classified failures. |
返回: CompatibleModelProvider。
成员目录
| 成员 | 用途 |
|---|---|
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. |
成员详情
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.
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
messages | readonly (ModelMessage | ProviderMessage)[] | 必需 | — |
context | ModelProviderContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
options | Readonly<ModelOptions> | undefined | 可省略 | 只读。Options prepared for this call, including function schemas and JSON mode. |
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
返回: AsyncIterable<ModelPart>。
使用示例
- 兼容 Provider 与自定义 Provider:Support 负责常见 Chat Completions HTTP/SSE 生命周期;App 可替换为自己的 ChatProvider。
真实服务端配置接入常见协议
入门 · Node.js
运行此例需要提供可用模型服务的连接配置。
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);直连工具线程的声明字段与 wire 映射
配置 · Node.js
只演示消息转换,无网络请求或工具执行。原始 tool_calls、tool_call_id 和宿主附加数据不透传。
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));同一合同接入离线对象实现
配置 · 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);