Skip to content
English

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.

导入 ​

ts
import { CompatibleModelProvider } from '@jangts/agent-core/support';
js
import { CompatibleModelProvider } from '@jangts/agent-core/support';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/support、@jangts/agent-core/support/model。

定义 ​

ts
class CompatibleModelProvider

继承或实现: ChatProvider。

构造函数 ​

ts
new CompatibleModelProvider(options: CompatibleModelProviderOptions): CompatibleModelProvider

Create a provider.

参数类型传递说明
optionsCompatibleModelProviderOptions必需—
options 的字段
字段类型必需说明
configCompatibleModelConfig | (() => CompatibleModelConfig)必需只读。Static configuration, or a factory evaluated at each request boundary.
portsOmit<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。

成员目录 ​

成员用途
streamStream 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.

ts
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.

参数类型传递说明
messagesreadonly (ModelMessage | ProviderMessage)[]必需—
contextModelProviderContext必需—
context 的字段
字段类型必需说明
optionsReadonly<ModelOptions> | undefined可省略只读。Options prepared for this call, including function schemas and JSON mode.
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.

返回: AsyncIterable<ModelPart>。

使用示例 ​

真实服务端配置接入常见协议 ​

入门 · Node.js

运行此例需要提供可用模型服务的连接配置。

ts
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);
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);

直连工具线程的声明字段与 wire 映射 ​

配置 · Node.js

只演示消息转换,无网络请求或工具执行。原始 tool_calls、tool_call_id 和宿主附加数据不透传。

ts
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));
js
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

ts
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);
js
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);

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。