CompletionSession
SUPPORT · interface · Type contract
Per-request completion state observed by one policy.
Import
import type { CompletionSession } from '@jangts/agent-core/support';/** @typedef {import('@jangts/agent-core/support').CompletionSession} CompletionSession */Other public entry points:@jangts/agent-core/support, @jangts/agent-core/support/model.
Definition
interface CompletionSessionField structure
| Field | Type | Required | Description |
|---|---|---|---|
part | (part: ModelPart) => void | Required | Observe one part immediately before the provider yields it. |
finish | (reason: string) => void | Required | Observe the stream's single finish_reason. |
end | () => void | Required | Complete the response when the end-of-stream marker arrives. |
Member index
| Member | Purpose |
|---|---|
part | Observe one part immediately before the provider yields it. |
finish | Observe the stream's single finish_reason. |
end | Complete the response when the end-of-stream marker arrives. |
Member details
part
Observe one part immediately before the provider yields it.
CompletionSession.part(part: ModelPart): voidObserve one part immediately before the provider yields it.
| Parameter | Type | Passing | Description |
|---|---|---|---|
part | ModelPart | Required | — |
Optional shapes of Parameter part: choose one shape; required fields for each branch are listed separately.
Branch 1
string
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
type | "function-call-delta" | Required | See the type definition and usage context. |
index | number | Required | Index of the call within the response; deltas with the same index belong together. |
id | string | undefined | Optional | Provider call id, usually delivered once with the first delta. |
name | string | undefined | Optional | Function name, usually delivered once with the first delta. |
arguments | string | undefined | Optional | Raw JSON argument fragment; concatenating all fragments yields the full arguments. |
Returns: void.
finish
Observe the stream's single finish_reason.
CompletionSession.finish(reason: string): voidObserve the stream's single finish_reason.
| Parameter | Type | Passing | Description |
|---|---|---|---|
reason | string | Required | — |
Returns: void.
end
Complete the response when the end-of-stream marker arrives.
CompletionSession.end(): voidComplete the response when the end-of-stream marker arrives.
No call parameters.
Returns: void.
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));