@jangts/agent-core 0.2.5 → 0.3.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.
@@ -0,0 +1,83 @@
1
+ /**
2
+ * One process-realm definition registry per kind. The shared symbol makes ESM/CJS copies
3
+ * consume the same stores; consumers receive frozen read-only views, never pool factories.
4
+ */
5
+ import { ProtocolRegistry } from '../../utils/protocol-registry.js';
6
+ /** Enumerable categories drive allocation and lookup; no additional runtime dispatch layer. */
7
+ export const definitionKinds = ['node', 'tool', 'fragment', 'workflow', 'agent', 'model'];
8
+ const sharedKey = Symbol.for('@jangts/agent-core/decorator-roots/v2');
9
+ const realm = globalThis;
10
+ /** Allocate once per realm, including when another module format initialized the roots first. */
11
+ function sharedPools() {
12
+ const existing = realm[sharedKey];
13
+ if (existing) {
14
+ if (existing.version !== 2 || definitionKinds.some((kind) => !existing.stores[kind]))
15
+ throw new TypeError('Incompatible decorator root registry');
16
+ return existing;
17
+ }
18
+ const stores = Object.fromEntries(definitionKinds.map((kind) => [kind, new Map()]));
19
+ const pools = Object.freeze({ version: 2, stores: Object.freeze(stores) });
20
+ Object.defineProperty(globalThis, sharedKey, { value: pools });
21
+ return pools;
22
+ }
23
+ /** Namespace is an exact, nonblank key; no trimming, fallback or hierarchy is applied. */
24
+ function namespace(value) {
25
+ if (typeof value !== 'string' || !value.trim())
26
+ throw new TypeError('Decorator namespace must be a nonempty string');
27
+ return value;
28
+ }
29
+ /** Preflight every destination so a conflict never leaves partial multi-namespace registration. */
30
+ export function registerDefinition(kind, definition, ns = 'default') {
31
+ if (typeof definition.id !== 'string' || !definition.id.trim())
32
+ throw new TypeError('Invalid decorator definition id');
33
+ const requested = typeof ns === 'string' ? [ns] : ns;
34
+ if (!Array.isArray(requested) || requested.length === 0)
35
+ throw new TypeError('Decorator ns must be a namespace or a nonempty namespace array');
36
+ const names = [...new Set(requested.map(namespace))];
37
+ const store = sharedPools().stores[kind];
38
+ for (const name of names) {
39
+ if (store.get(name)?.has(definition.id))
40
+ throw new Error(`Duplicate ${kind} definition: ${definition.id} (ns: ${name})`);
41
+ }
42
+ for (const name of names) {
43
+ let entries = store.get(name);
44
+ if (!entries)
45
+ store.set(name, (entries = new ProtocolRegistry()));
46
+ entries.set(definition.id, definition);
47
+ }
48
+ }
49
+ /** Root reads never instantiate or execute definitions; array results are immutable snapshots. */
50
+ function reader(kind) {
51
+ const store = sharedPools().stores[kind];
52
+ return Object.freeze({
53
+ get(id, ns = 'default') {
54
+ return store.get(namespace(ns))?.get(id);
55
+ },
56
+ getAll(ns = 'default') {
57
+ return Object.freeze([...(store.get(namespace(ns))?.values() ?? [])]);
58
+ },
59
+ pick(ids, ns = 'default') {
60
+ const entries = store.get(namespace(ns));
61
+ const selected = new ProtocolRegistry();
62
+ for (const id of ids) {
63
+ const definition = entries?.get(id);
64
+ if (!definition)
65
+ throw new TypeError(`Unknown ${kind} definition: ${id} (ns: ${ns})`);
66
+ selected.set(id, definition);
67
+ }
68
+ return Object.freeze([...selected.values()]);
69
+ },
70
+ });
71
+ }
72
+ /** Decorated nodes; ids are unique within each namespace. */
73
+ export const nodePool = reader('node');
74
+ /** Decorated tools; namespaces isolate otherwise identical ids. */
75
+ export const toolPool = reader('tool');
76
+ /** Decorated fragment configurations; use the original builder to assemble them. */
77
+ export const fragmentPool = reader('fragment');
78
+ /** Decorated workflow definitions; execution still belongs to the original Engine. */
79
+ export const workflowPool = reader('workflow');
80
+ /** Decorated Engine options; request-specific App contracts are supplied on typed reads. */
81
+ export const agentPool = reader('agent');
82
+ /** Decorated Runner options; reading does not create a Runner or spend model calls. */
83
+ export const modelPool = reader('model');
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Class implementation contracts for definition decorators. The decorator supplies id;
3
+ * classes implement these named contracts instead of repeating Omit at every use site.
4
+ */
5
+ import type { WorkflowNode, AgentWorkflowDefinition } from '../../structs/execution/workflow.js';
6
+ import type { ToolDefinition } from '../../structs/capabilities/tools.js';
7
+ import type { ContextFragmentConfig } from '../../structs/support/context.js';
8
+ import type { AgentCoreEngineSetup } from '../../execution/engine.js';
9
+ import type { ModelRunnerOptions } from '../../structs/model/runtime.js';
10
+ import type { DefinitionBody } from './definition.js';
11
+ /** A node implementation; its public id is supplied by @node. */
12
+ export type Node<State, Result, App = undefined> = DefinitionBody<WorkflowNode<State, Result, App>>;
13
+ /** A tool implementation with the original schema, authorization and execution contracts. */
14
+ export type Tool<Input = unknown, Output = unknown, App = unknown> = DefinitionBody<ToolDefinition<Input, Output, App>>;
15
+ /** A formatter and its original provenance, trust and budget configuration. */
16
+ export type Fragment<Input = unknown> = DefinitionBody<ContextFragmentConfig<Input>>;
17
+ /** A complete graph definition whose workflow id is supplied by @workflow. */
18
+ export type Workflow<State, Result, App = undefined> = DefinitionBody<AgentWorkflowDefinition<State, Result, App>>;
19
+ /** Original Engine configuration; union branches retain their required App/model relationships. */
20
+ export type Agent<State = unknown, Result = unknown, App = undefined> = AgentCoreEngineSetup<State, Result, App>;
21
+ /** Original Runner configuration; the decorator's pool id is not a model identity override. */
22
+ export type Model = ModelRunnerOptions;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jangts/agent-core",
3
- "version": "0.2.5",
3
+ "version": "0.3.5",
4
4
  "type": "commonjs",
5
5
  "license": "Apache-2.0",
6
6
  "description": "Platform-independent agent runtime and configurable modules for evidence, delivery, policies and interactions",
@@ -241,6 +241,11 @@
241
241
  "types": "./dist/esm/mock/web.d.ts",
242
242
  "import": "./dist/esm/mock/web.js",
243
243
  "require": "./dist/cjs/mock/web.js"
244
+ },
245
+ "./support/decorators": {
246
+ "types": "./dist/esm/support/decorators/index.d.ts",
247
+ "import": "./dist/esm/support/decorators/index.js",
248
+ "require": "./dist/cjs/support/decorators/index.js"
244
249
  }
245
250
  },
246
251
  "scripts": {
@@ -254,7 +259,8 @@
254
259
  "minisearch": "^7.2.0"
255
260
  },
256
261
  "devDependencies": {
257
- "typescript": "~6.0.2"
262
+ "typescript": "~6.0.2",
263
+ "esbuild": "0.28.2"
258
264
  },
259
265
  "keywords": [
260
266
  "ai",
@@ -271,6 +277,8 @@
271
277
  "openai-compatible",
272
278
  "context-management",
273
279
  "streaming",
280
+ "decorators",
281
+ "typescript-decorators",
274
282
  "typescript",
275
283
  "nodejs",
276
284
  "browser"