@jangts/agent-core 0.2.0 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +55 -0
  3. package/NOTICE +6 -0
  4. package/README.md +8 -0
  5. package/development/docs/README.en.md +70 -0
  6. package/development/docs/README.md +1 -1
  7. package/development/docs/acceptance.en.md +34 -0
  8. package/development/docs/agent-development.en.md +42 -0
  9. package/development/docs/api/README.en.md +62 -0
  10. package/development/docs/api/advanced-agents.en.md +135 -0
  11. package/development/docs/api/context-processing.en.md +110 -0
  12. package/development/docs/api/core.en.md +274 -0
  13. package/development/docs/api/examples.en.md +118 -0
  14. package/development/docs/api/kits.en.md +183 -0
  15. package/development/docs/api/knowledge-answering.en.md +166 -0
  16. package/development/docs/api/mock.en.md +186 -0
  17. package/development/docs/api/observability.en.md +166 -0
  18. package/development/docs/api/reference.en.md +2235 -0
  19. package/development/docs/api/reference.md +290 -257
  20. package/development/docs/api/support.en.md +252 -0
  21. package/development/docs/api/types.en.md +181 -0
  22. package/development/docs/api/workflow.en.md +198 -0
  23. package/development/docs/api/workflow.md +22 -0
  24. package/development/docs/basic-usage.en.md +78 -0
  25. package/development/docs/create-app.en.md +76 -0
  26. package/development/docs/examples/offline-agent.en.md +273 -0
  27. package/development/docs/examples.en.md +36 -0
  28. package/development/docs/flows/knowledge-maintenance.en.md +38 -0
  29. package/development/docs/getting-started.en.md +71 -0
  30. package/development/docs/knowledge.en.md +89 -0
  31. package/development/docs/maintenance.en.md +62 -0
  32. package/development/docs/materials.en.md +38 -0
  33. package/development/docs/mechanisms.en.md +227 -0
  34. package/development/docs/memory-and-learning.en.md +42 -0
  35. package/development/docs/operations.en.md +51 -0
  36. package/development/docs/records.en.md +63 -0
  37. package/development/docs/registration.en.md +63 -0
  38. package/development/skills/agent-core-app/SKILL.md +4 -0
  39. package/development/skills/agent-core-app/scripts/create-app.mjs +19 -0
  40. package/development/skills/agent-core-app/scripts/lib/knowledge.mjs +37 -1
  41. package/development/skills/agent-core-app/scripts/plan.mjs +9 -0
  42. package/development/skills/agent-core-app/scripts/resources.mjs +17 -0
  43. package/dist/cjs/diagnostics/catalog.js +6 -0
  44. package/dist/cjs/execution/cache.js +162 -0
  45. package/dist/cjs/execution/engine.js +10 -0
  46. package/dist/cjs/kits/vector-search.js +4 -7
  47. package/dist/cjs/structs/execution/cache.js +6 -0
  48. package/dist/cjs/support/model/chat-completions.js +1 -1
  49. package/dist/cjs/support/model/completion/default.js +1 -0
  50. package/dist/cjs/support/model/completion/session.js +5 -0
  51. package/dist/esm/diagnostics/catalog.d.ts +6 -0
  52. package/dist/esm/diagnostics/catalog.js +6 -0
  53. package/dist/esm/execution/cache.d.ts +20 -0
  54. package/dist/esm/execution/cache.js +156 -0
  55. package/dist/esm/execution/engine.d.ts +5 -0
  56. package/dist/esm/execution/engine.js +10 -0
  57. package/dist/esm/kits/vector-search.js +4 -7
  58. package/dist/esm/structs/execution/cache.d.ts +54 -0
  59. package/dist/esm/structs/execution/cache.js +5 -0
  60. package/dist/esm/structs/execution/engine.d.ts +6 -1
  61. package/dist/esm/structs/execution/workflow.d.ts +2 -1
  62. package/dist/esm/structs/index.d.ts +1 -0
  63. package/dist/esm/structs/kits/retrieval.d.ts +1 -0
  64. package/dist/esm/structs/kits/search.d.ts +2 -0
  65. package/dist/esm/structs/model/messages.d.ts +4 -0
  66. package/dist/esm/structs/support/model/messages.d.ts +2 -0
  67. package/dist/esm/support/context/nodes.d.ts +16 -0
  68. package/dist/esm/support/context/processing.d.ts +7 -0
  69. package/dist/esm/support/model/chat-completions.js +1 -1
  70. package/dist/esm/support/model/children.d.ts +2 -0
  71. package/dist/esm/support/model/completion/default.d.ts +1 -0
  72. package/dist/esm/support/model/completion/default.js +1 -0
  73. package/dist/esm/support/model/completion/session.d.ts +5 -0
  74. package/dist/esm/support/model/completion/session.js +5 -0
  75. package/dist/esm/support/model/preview-rules.d.ts +6 -0
  76. package/dist/esm/support/model/reply-preview.d.ts +7 -0
  77. package/dist/esm/support/model/strategies.d.ts +4 -0
  78. package/package.json +7 -4
@@ -8,6 +8,7 @@
8
8
  * Exports: `LoopEvent`, `StepOutcome`, `CoreStep`, `AgentCoreEngineOptions`.
9
9
  */
10
10
  import type { ExecutionContext, ExecutionOptions, MaybePromise } from '../common.js';
11
+ import type { CacheContext, EngineCacheOptions, AgentCachePort } from './cache.js';
11
12
  /** Progress fact for one loop round, correlated with the enclosing diagnostics span. */
12
13
  export interface LoopEvent {
13
14
  /** Unique id of this fact. */
@@ -31,9 +32,13 @@ export type StepOutcome<Value> =
31
32
  readonly value: Value;
32
33
  };
33
34
  /** One loop iteration. Must be re-entrant: the same step object runs every round. */
34
- export type CoreStep<Result> = (roundIndex: number, context: ExecutionContext & import('../debug.js').NodeReportingContext) => MaybePromise<StepOutcome<Result>>;
35
+ export type CoreStep<Result> = (roundIndex: number, context: ExecutionContext & import('../debug.js').NodeReportingContext & CacheContext) => MaybePromise<StepOutcome<Result>>;
35
36
  /** Engine construction options. */
36
37
  export interface AgentCoreEngineOptions {
38
+ /** Engine-local LRU bounds; not copied as cache data when forking. */
39
+ cache?: EngineCacheOptions;
40
+ /** Borrowed App service cache; forks must explicitly provide their own port. */
41
+ agentCache?: AgentCachePort;
37
42
  /** Round budget; must be a positive safe integer. Defaults to 8 rounds. */
38
43
  maxRounds?: number;
39
44
  /** Cancellation and diagnostics shared by every round. */
@@ -10,6 +10,7 @@
10
10
  * `AgentWorkflowDefinition`, `WorkflowEvent`, `WorkflowFailure`, `WorkflowExtension`,
11
11
  * `WorkflowModel`.
12
12
  */
13
+ import type { CacheContext } from './cache.js';
13
14
  import type { OperationContext } from '../common.js';
14
15
  import type { DiagnosticContext } from '../diagnostics.js';
15
16
  import type { ModelTransportDefinition } from '../model/runtime.js';
@@ -20,7 +21,7 @@ type WorkflowModelRequest<Transport> = import('../model/runtime.js').ModelCallRe
20
21
  readonly modelId?: string;
21
22
  };
22
23
  /** Execution facts available to every node of a running workflow. */
23
- export interface WorkflowContext<Runtime = undefined> extends OperationContext<Runtime>, NodeReportingContext {
24
+ export interface WorkflowContext<Runtime = undefined> extends OperationContext<Runtime>, NodeReportingContext, CacheContext {
24
25
  /** Zero-based bounded round; phases within a round do not consume another round. */
25
26
  readonly roundIndex: number;
26
27
  /** Report progress while this node is active; Core supplies its execution identity. */
@@ -54,3 +54,4 @@ export * from './common.js';
54
54
  export type * from './invocation.js';
55
55
  export type * from './debug.js';
56
56
  export type * from './execution/events.js';
57
+ export type * from './execution/cache.js';
@@ -13,6 +13,7 @@
13
13
  export interface RetrievalSource {
14
14
  sourceId: string;
15
15
  }
16
+ /** Adapter output for one accepted query; the Kit renames each source before returning it. */
16
17
  export interface RetrievalResult<Source extends RetrievalSource> {
17
18
  sources: Source[];
18
19
  }
@@ -25,12 +25,14 @@ export interface TextSearchOptions<Document extends {
25
25
  /** How multiple query terms combine; defaults to the engine default (OR). */
26
26
  readonly combineWith?: 'AND' | 'OR';
27
27
  }
28
+ /** One ranked match; `terms` are post-tokenization and only stored fields are returned. */
28
29
  export interface TextSearchHit<Document extends {
29
30
  id: string;
30
31
  }> extends SearchHit<Partial<Document>> {
31
32
  /** Matched terms, as reported by the engine after tokenization. */
32
33
  readonly terms: readonly string[];
33
34
  }
35
+ /** Search input: a hard result limit and an App-owned filter, both required on every query. */
34
36
  export interface TextSearchQuery<Document extends {
35
37
  id: string;
36
38
  }> {
@@ -13,6 +13,7 @@ export interface ModelImage {
13
13
  /** Provider resolution hint; `original` asks for no downscaling where supported. */
14
14
  detail?: 'auto' | 'low' | 'high' | 'original';
15
15
  }
16
+ /** One provider-neutral turn; `images` are user visual input and are not part of the text budget. */
16
17
  export interface ModelMessage {
17
18
  role: 'system' | 'user' | 'assistant';
18
19
  /** Rendered text content sent to the model. */
@@ -20,6 +21,7 @@ export interface ModelMessage {
20
21
  /** User-supplied visual input. Binary payload is separate from the text character budget. */
21
22
  images?: readonly ModelImage[];
22
23
  }
24
+ /** Provider-reported token counts for one call; used for accounting, never for admission. */
23
25
  export interface ModelUsage {
24
26
  /** Input tokens billed for the call. */
25
27
  promptTokens: number;
@@ -39,6 +41,7 @@ export interface ModelFunction {
39
41
  /** Request strict argument validation from providers that support it. */
40
42
  strict?: boolean;
41
43
  }
44
+ /** One streamed fragment of a function call; fragments with the same `index` must be joined. */
42
45
  export interface FunctionCallDelta {
43
46
  type: 'function-call-delta';
44
47
  /** Index of the call within the response; deltas with the same index belong together. */
@@ -52,6 +55,7 @@ export interface FunctionCallDelta {
52
55
  }
53
56
  /** One streamed response part: assistant text or a function-call fragment. */
54
57
  export type ModelPart = string | FunctionCallDelta;
58
+ /** Per-call provider options; Core passes them through and does not normalize provider ranges. */
55
59
  export interface ModelOptions {
56
60
  /** Request a JSON-object response from providers that support it. */
57
61
  json?: boolean;
@@ -18,6 +18,7 @@ export interface ProviderToolCall {
18
18
  /** Raw JSON arguments exactly as the provider produced them. */
19
19
  readonly arguments: string;
20
20
  }
21
+ /** System instruction turn; text only, so the union rejects images and tool fields here. */
21
22
  export interface ProviderSystemMessage {
22
23
  readonly role: 'system';
23
24
  readonly content: string;
@@ -25,6 +26,7 @@ export interface ProviderSystemMessage {
25
26
  readonly toolCallId?: never;
26
27
  readonly toolCalls?: never;
27
28
  }
29
+ /** User turn; the only message type that may carry images, and never tool calls. */
28
30
  export interface ProviderUserMessage {
29
31
  readonly role: 'user';
30
32
  readonly content: string;
@@ -3,6 +3,7 @@ import type { ContextPlan } from '../../structs/context/processing.js';
3
3
  import type { BuiltContext } from '../../structs/context/fragments.js';
4
4
  import type { ModelPrepareRequest, PreparedModelCall, ModelTransportDefinition } from '../../structs/model/runtime.js';
5
5
  import type { ContextCompressor, ContextSelector, ContextPolicy, ContextProcessingReport } from '../../structs/support/context-processing.js';
6
+ /** Outcome of one context node: the built input, its prepared model call and the change report. */
6
7
  export interface ContextNodeResult {
7
8
  /** Context built from the processed fragments, sized against the full input limit. */
8
9
  readonly input: BuiltContext;
@@ -11,6 +12,13 @@ export interface ContextNodeResult {
11
12
  /** What processing changed: characters, token usage, selection decisions and reductions. */
12
13
  readonly report: ContextProcessingReport;
13
14
  }
15
+ /**
16
+ * Definition accepted by `createContextProcessingNode`.
17
+ *
18
+ * It is a normal Core `WorkflowNode` except that the node body is declared here: `plan` builds the
19
+ * context, `update` decides the committed state, and policy/handlers/timeout are validated when the
20
+ * node is created.
21
+ */
14
22
  export type ContextProcessingNodeDefinition<State, Result, App> = Omit<WorkflowNode<State, Result, App>, 'run' | 'report'> & {
15
23
  /** Builds the context plan; must be synchronous so budget failures surface before any I/O. */
16
24
  readonly plan: (state: Readonly<State>, context: WorkflowContext<App>) => ContextPlan;
@@ -33,6 +41,7 @@ export type ContextProcessingNodeDefinition<State, Result, App> = Omit<WorkflowN
33
41
  /** Per-node timeout in milliseconds; defaults to the Core call timeout. */
34
42
  readonly timeoutMs?: number;
35
43
  };
44
+ /** Result of a selection or reduction stage, before the node's `update` commits chosen fields. */
36
45
  export interface ContextStageResult {
37
46
  /** Plan describing current fragments at the full (unreduced) input limit. */
38
47
  readonly plan: ContextPlan;
@@ -41,14 +50,21 @@ export interface ContextStageResult {
41
50
  /** Processing report accumulated by this and preceding steps. */
42
51
  readonly report: ContextProcessingReport;
43
52
  }
53
+ /** Selection stage result: the chosen step IDs plus the unchanged stage snapshot. */
44
54
  export interface ContextSelectionResult extends ContextStageResult {
45
55
  /** Registered reduction step IDs chosen for execution; selection itself executes nothing. */
46
56
  readonly stepIds: readonly string[];
47
57
  }
58
+ /** Definition of a selection node: like a processing node, but `update` receives chosen step IDs. */
48
59
  export type ContextSelectionNodeDefinition<State, Result, App> = Omit<ContextProcessingNodeDefinition<State, Result, App>, 'update'> & {
49
60
  /** Receives the chosen step IDs so the graph can route to reduction nodes. */
50
61
  readonly update: (result: ContextSelectionResult, state: Readonly<State>) => Partial<State>;
51
62
  };
63
+ /**
64
+ * Definition of a reduction node: one registered step is applied per node run.
65
+ *
66
+ * `stepId` may be fixed or derived from state, which lets a graph route over several steps.
67
+ */
52
68
  export type ContextReductionNodeDefinition<State, Result, App> = Omit<ContextProcessingNodeDefinition<State, Result, App>, 'update'> & {
53
69
  /** ID of the reduction step to run, fixed or derived from state. */
54
70
  readonly stepId: string | ((state: Readonly<State>) => string);
@@ -49,6 +49,13 @@ export declare const contextReductionCatalog: {
49
49
  export declare function validateStructure(fragments: readonly ContextFragment[]): void;
50
50
  /** Execution ports accepted by the processing helpers; alias of the Core options contract. */
51
51
  export type ProcessingPorts = import('../../structs/support/context-processing.js').ContextProcessingOptions;
52
+ /**
53
+ * Working state for one context-processing stage.
54
+ *
55
+ * `fragments` is mutated in place by reductions while `before` keeps the original snapshot for the
56
+ * audit report; `ports` carries the signal, diagnostics and call budget already validated by the
57
+ * caller.
58
+ */
52
59
  export interface ProcessingState {
53
60
  /** Token count before processing; present only when a token measurement port was supplied. */
54
61
  beforeTokens?: number;
@@ -87,7 +87,7 @@ export function chatCompletionParts(delta) {
87
87
  */
88
88
  export function chatCompletionMessages(messages) {
89
89
  return messages.map((message) => {
90
- const { role, content, images, toolCalls, toolCallId } = message;
90
+ const { role, content, images, toolCalls, toolCallId, } = message;
91
91
  if (!['system', 'user', 'assistant', 'tool'].includes(role) || typeof content !== 'string')
92
92
  throw new TypeError('Invalid provider message role or content');
93
93
  if (toolCallId !== undefined && role !== 'tool')
@@ -11,6 +11,7 @@
11
11
  import type { ModelRunner } from '../../model/runner.js';
12
12
  import type { ModelChildOptions } from '../../structs/model/runtime.js';
13
13
  import type { MaybePromise } from '../../structs/common.js';
14
+ /** One child call in a group: its own limits plus the work to run with the child runner. */
14
15
  export interface ModelChildTask<Value> extends ModelChildOptions {
15
16
  /**
16
17
  * Work executed with the child runner bound to this task's limits.
@@ -19,6 +20,7 @@ export interface ModelChildTask<Value> extends ModelChildOptions {
19
20
  */
20
21
  run(model: ModelRunner, signal: AbortSignal): MaybePromise<Value>;
21
22
  }
23
+ /** Group options: worker count, extra cancellation and accept/commit hooks. */
22
24
  export interface ModelChildrenOptions<Value> {
23
25
  /** Parallel workers; defaults to, and may not exceed, the parent's concurrency limit. */
24
26
  readonly concurrency?: number;
@@ -7,4 +7,5 @@
7
7
  * Exports: `defaultCompletionPolicy`.
8
8
  */
9
9
  import type { CompletionPolicy } from '../../../structs/support/model/completion.js';
10
+ /** Frozen default policy: strictness depends only on whether the call declared functions. */
10
11
  export declare const defaultCompletionPolicy: CompletionPolicy;
@@ -26,4 +26,5 @@ const policy = {
26
26
  };
27
27
  },
28
28
  };
29
+ /** Frozen default policy: strictness depends only on whether the call declared functions. */
29
30
  export const defaultCompletionPolicy = Object.freeze(policy);
@@ -8,4 +8,9 @@
8
8
  * Exports: `sessionCompletionPolicy`.
9
9
  */
10
10
  import type { CompletionPolicy } from '../../../structs/support/model/completion.js';
11
+ /**
12
+ * Frozen opt-in policy that judges rules by the request's effective `tool_choice`.
13
+ *
14
+ * Consumers that do not select it keep `defaultCompletionPolicy`.
15
+ */
11
16
  export declare const sessionCompletionPolicy: CompletionPolicy;
@@ -96,4 +96,9 @@ const policy = {
96
96
  };
97
97
  },
98
98
  };
99
+ /**
100
+ * Frozen opt-in policy that judges rules by the request's effective `tool_choice`.
101
+ *
102
+ * Consumers that do not select it keep `defaultCompletionPolicy`.
103
+ */
99
104
  export const sessionCompletionPolicy = Object.freeze(policy);
@@ -1,5 +1,10 @@
1
1
  import type { JsonSchema } from '../../structs/common.js';
2
2
  import type { ReplyPreviewRule } from './reply-preview.js';
3
+ /**
4
+ * Capability declaration used to derive preview rules.
5
+ *
6
+ * Omitted `preview` means the capability never previews, so previewing is an explicit opt-in.
7
+ */
3
8
  export interface ReplyPreviewCapability {
4
9
  /** Capability ID, which must equal the native function wire name used for transport. */
5
10
  readonly id: string;
@@ -11,6 +16,7 @@ export interface ReplyPreviewCapability {
11
16
  */
12
17
  readonly preview?: true | Omit<ReplyPreviewRule, 'stageId' | 'capabilityId'>;
13
18
  }
19
+ /** Stage declaration: which capabilities it exposes and whether it previews text or arguments. */
14
20
  export interface ReplyPreviewStage {
15
21
  /** Stage ID used by the workflow and transport projection. */
16
22
  readonly id: string;
@@ -1,4 +1,9 @@
1
1
  import type { ModelOutputEvent } from '../../structs/model/runtime.js';
2
+ /**
3
+ * One projection rule: which stage/capability it matches and which prose fields may be shown.
4
+ *
5
+ * A rule without `capabilityId` may only match a registered plain-text stage.
6
+ */
2
7
  export interface ReplyPreviewRule {
3
8
  /** Native function wire name. Omit only for a registered plain-text stage. */
4
9
  capabilityId?: string;
@@ -12,6 +17,7 @@ export interface ReplyPreviewRule {
12
17
  values: readonly string[];
13
18
  };
14
19
  }
20
+ /** Rules plus optional renderer and character budget; all values are snapshotted at creation. */
15
21
  export interface ReplyPreviewSettings {
16
22
  rules: readonly ReplyPreviewRule[];
17
23
  /** Presentation only; must not authorize, persist or execute a proposal. */
@@ -19,6 +25,7 @@ export interface ReplyPreviewSettings {
19
25
  /** Streamed characters tolerated before preview is disabled; default 16000. */
20
26
  maxChars?: number;
21
27
  }
28
+ /** Settings for `createReplyPreview`; `sink` receives every bubble list and is required. */
22
29
  export interface ReplyPreviewOptions extends ReplyPreviewSettings {
23
30
  /** Receives the current bubble list; rejection propagates to the event caller. */
24
31
  readonly sink: (bubbles: string[]) => void | Promise<void>;
@@ -36,6 +36,10 @@ export type ModelBudgetStrategy = keyof typeof modelBudgetStrategies | Partial<M
36
36
  type StrategyModel<Transports extends readonly ModelTransportDefinition[], Execution> = Omit<ModelRunnerOptions<Transports>, 'execution'> & {
37
37
  execution?: Execution;
38
38
  };
39
+ /**
40
+ * Input of `createModelStrategy`: model selection, optional reasoning and budget plus explicit
41
+ * Core overrides. Explicit Core fields win over every generated value.
42
+ */
39
43
  export type ModelStrategyOptions<Transports extends readonly ModelTransportDefinition[] = readonly ModelTransportDefinition[], Execution = ExecutionOptions> = Partial<StrategyModel<Transports, Execution>> & {
40
44
  /** An existing definition, or an ID in the explicitly supplied model catalog. */
41
45
  readonly model: StrategyModel<Transports, Execution> | string;
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@jangts/agent-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.5",
4
4
  "type": "commonjs",
5
+ "license": "Apache-2.0",
5
6
  "description": "Platform-independent agent runtime and configurable modules for evidence, delivery, policies and interactions",
6
7
  "homepage": "https://agent-core-docs.pages.jangts.cafe/",
7
8
  "engines": {
@@ -15,7 +16,9 @@
15
16
  "dist",
16
17
  "development/docs",
17
18
  "development/skills",
18
- "CHANGELOG.md"
19
+ "CHANGELOG.md",
20
+ "LICENSE",
21
+ "NOTICE"
19
22
  ],
20
23
  "sideEffects": false,
21
24
  "exports": {
@@ -246,8 +249,8 @@
246
249
  "docs:api": "node scripts/api-reference.mjs"
247
250
  },
248
251
  "dependencies": {
249
- "@jangts/streaming": "0.1.0",
250
- "@jangts/mock-gpt": "0.2.0",
252
+ "@jangts/streaming": "^0.1.2",
253
+ "@jangts/mock-gpt": "0.2.2",
251
254
  "minisearch": "^7.2.0"
252
255
  },
253
256
  "devDependencies": {