MaybePromise
CORE · type · Type contract
A value that may already be resolved or still pending.
Import
ts
import type { MaybePromise } from '@jangts/agent-core';js
/** @typedef {import('@jangts/agent-core').MaybePromise} MaybePromise */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
ts
type MaybePromise<Value> = Value | Promise<Value>;Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Value | — | — |
Union branch structure
Examples
- Agent: run first, then bind tools and dependencies step by step: The Engine executes a bounded loop; after a Workflow is bound, each run only receives the data that changed in state.
- Real tool invocation and in-request runtime: Core registers and executes real tools and returns receipts carrying an invocation; business authorization is owned by the implementation registered by the App.
- Tool best practice: one definition generates the model tool and execution runtime: Register Schema, authorization, execution, and output validation once, and bind the App per request instead of maintaining two tool lists.
- Child task composition: sequential execution and controlled parallelism: Reuse createChild and the shared budget, check every result, and commit in declaration order.
- Decorators: complete handlers and existing host methods: Classes construct definitions automatically; methods bind the real host. Outputs still use original define, constructor and bind contracts.
- Log reflection: authorization metadata and evidence reports: The App supplies authorized evidence sources; the reflection task produces a sourced report through ordinary model and tool contracts.
A single ordinary step is enough to run
Beginner · Node.js
ts
import { AgentCoreEngine } from '@jangts/agent-core/engine';
const agent = new AgentCoreEngine<string>();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);js
import { AgentCoreEngine } from '@jangts/agent-core/engine';
const agent = new AgentCoreEngine();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);Add an explicit Workflow and still call run
Configuration · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({
id: 'greeting',
nodes: [{
id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
run: state => ({ answer: '你好,' + state.name }),
routes: [route.deliver('answer')],
}],
});
const agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
id: 'greeting',
nodes: [{
id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
run: state => ({ answer: '你好,' + state.name }),
routes: [route.deliver('answer')],
}],
});
const agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));Related policy source
- Execution implementation of the bounded loop, commit, and cancellation
- Check order for tool input, execution, output, and late results
- Authorization and model tool projection come from the same definition
- Child acceptance, cancellation, and ordered commit
- Reflection evidence references and report checks