Skip to content
English

CompletionPolicy ​

SUPPORT · interface · 类型合同

Decision whether a finished stream satisfies the request.

导入 ​

ts
import type { CompletionPolicy } from '@jangts/agent-core/support';
js
/** @typedef {import('@jangts/agent-core/support').CompletionPolicy} CompletionPolicy */

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

定义 ​

ts
interface CompletionPolicy

字段结构 ​

字段类型必需说明
idstring必需只读。Registered identifier; the catalog key must match it.
begin(facts: CompletionRequestFacts, fail: CompletionFailure) => CompletionSession必需Start one request. Throwing here rejects the request before it is sent, which is how a policy refuses a request shape it cannot judge after the fact.

成员目录 ​

成员用途
beginStart one request. Throwing here rejects the request before it is sent, which is how a policy refuses a request shape it cannot judge after the fact.

成员详情 ​

begin ​

Start one request.

Throwing here rejects the request before it is sent, which is how a policy refuses a request shape it cannot judge after the fact.

ts
CompletionPolicy.begin(facts: CompletionRequestFacts, fail: CompletionFailure): CompletionSession

Start one request.

Throwing here rejects the request before it is sent, which is how a policy refuses a request shape it cannot judge after the fact.

参数类型传递说明
factsCompletionRequestFacts必需—
failCompletionFailure必需—
facts 的字段
字段类型必需说明
functionsboolean必需只读。Whether the call declared native functions through Core options.
toolChoiceunknown可省略只读。Effective tool_choice of the request body; an absent field reads as undefined.
allowEmptyTextboolean必需只读。Whether an empty response is allowed because JSON mode permits it.

返回: CompletionSession。

返回值结构
字段类型必需说明
part(part: ModelPart) => void必需Observe one part immediately before the provider yields it.
finish(reason: string) => void必需Observe the stream's single finish_reason.
end() => void必需Complete the response when the end-of-stream marker arrives.

使用示例 ​

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

入门 · 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));

相关策略源码 ​

相关定义 ​

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