@zhin.js/agent-feature 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 凉菜
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @zhin.js/agent-feature
2
+
3
+ 下一代 Markdown Agent Feature。它从 `agents/<name>.agent.md` 构造 immutable Agent definition,不读取旧分形 `agent.ts`、`instructions.md` 或模块级 SubAgent registry。
4
+
5
+ ## 目录约定
6
+
7
+ ```text
8
+ agents/
9
+ ├── planner.agent.md
10
+ └── reviewer.agent.md
11
+ ```
12
+
13
+ Agent 目录只允许一级、精确 `.agent.md` 后缀。`planner.agent.md` 的 local name 是 `planner`;child Plugin 的 qualified name 自动包含 Plugin instance path。
14
+
15
+ ## Markdown 契约
16
+
17
+ ```markdown
18
+ # Planner
19
+
20
+ Break the request into verifiable steps. Prefer reversible actions.
21
+ ```
22
+
23
+ 完整 Markdown 是 `instructions` SSOT。首个 heading 作为 description;没有 heading 时使用文件 identity。Feature 不绑定模型、system prompt 模板、tool policy 或 session store,这些属于 orchestrator adapter。
24
+
25
+ ## Projection
26
+
27
+ `AgentIndex.list()` 返回全树 qualified descriptors;`visible(owner)` 和 `get(owner, name)` 使用 nearest-owner inheritance。不同 Plugin owner 的同名 Agent 不通过扫描顺序覆盖。
28
+
29
+ Agent definition 是纯数据,没有 runtime disposer。单文件 HMR 只替换 Agent Slot,旧 turn lease 继续使用旧 instructions。
30
+
31
+ ## 依赖
32
+
33
+ 只依赖 Next Kernel 与 Feature Kit,不引入 AI SDK、模型 provider、Markdown/YAML parser 或数据库。
34
+
35
+ ## 验证
36
+
37
+ ```bash
38
+ pnpm --filter @zhin.js/agent-feature test
39
+ pnpm --filter @zhin.js/agent-feature build
40
+ ```
@@ -0,0 +1,14 @@
1
+ import type { CapabilitySlot, PluginId, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
2
+ import type { AgentDefinition } from './definition.js';
3
+ export interface AgentDescriptor extends AgentDefinition {
4
+ readonly owner: PluginId;
5
+ readonly qualifiedName: string;
6
+ readonly source: string;
7
+ }
8
+ export declare class AgentIndex {
9
+ #private;
10
+ constructor(slots: readonly Readonly<CapabilitySlot<AgentDefinition>>[], snapshot: RuntimeSnapshot);
11
+ list(): readonly AgentDescriptor[];
12
+ visible(requester: PluginId): readonly AgentDescriptor[];
13
+ get(requester: PluginId, name: string): AgentDescriptor | undefined;
14
+ }
@@ -0,0 +1,25 @@
1
+ import { OwnerCapabilityIndex, } from '@zhin.js/feature-kit';
2
+ export class AgentIndex {
3
+ #index;
4
+ constructor(slots, snapshot) {
5
+ this.#index = new OwnerCapabilityIndex(slots, snapshot);
6
+ }
7
+ list() {
8
+ return this.#index.entries().map(toDescriptor);
9
+ }
10
+ visible(requester) {
11
+ return this.#index.visible(requester).map(toDescriptor);
12
+ }
13
+ get(requester, name) {
14
+ const entry = this.#index.resolve(requester, name);
15
+ return entry ? toDescriptor(entry) : undefined;
16
+ }
17
+ }
18
+ function toDescriptor(entry) {
19
+ return Object.freeze({
20
+ ...entry.slot.definition,
21
+ owner: entry.owner,
22
+ qualifiedName: entry.qualifiedName,
23
+ source: entry.source,
24
+ });
25
+ }
@@ -0,0 +1,10 @@
1
+ import type { ValidationContext } from '@zhin.js/feature-kit';
2
+ declare const agentBrand: "zhin.agent/1";
3
+ export interface AgentDefinition {
4
+ readonly $feature: typeof agentBrand;
5
+ readonly name: string;
6
+ readonly description: string;
7
+ readonly instructions: string;
8
+ }
9
+ export declare function parseAgentMarkdown(value: unknown, context: ValidationContext): AgentDefinition;
10
+ export {};
@@ -0,0 +1,18 @@
1
+ const agentBrand = 'zhin.agent/1';
2
+ export function parseAgentMarkdown(value, context) {
3
+ if (typeof value !== 'string' || !value.trim()) {
4
+ throw new TypeError(`Agent ${context.source} must contain Markdown instructions`);
5
+ }
6
+ return Object.freeze({
7
+ $feature: agentBrand,
8
+ name: context.localName,
9
+ description: markdownSummary(value, context.localName),
10
+ instructions: value,
11
+ });
12
+ }
13
+ function markdownSummary(markdown, fallback) {
14
+ const line = markdown.split(/\r?\n/u)
15
+ .map((value) => value.trim())
16
+ .find((value) => /^#+\s+\S/u.test(value));
17
+ return line?.replace(/^#+\s*/u, '') ?? fallback;
18
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './agent-index.js';
2
+ export * from './definition.js';
3
+ export * from './provider.js';
4
+ export { default } from './provider.js';
package/lib/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export * from './agent-index.js';
2
+ export * from './definition.js';
3
+ export * from './provider.js';
4
+ export { default } from './provider.js';
@@ -0,0 +1,5 @@
1
+ import { AgentIndex } from './agent-index.js';
2
+ export declare const agentFeatureId: import("@zhin.js/plugin-runtime").FeatureId;
3
+ declare const agentFeature: Readonly<import("@zhin.js/feature-kit").FeatureProvider<import("./definition.js").AgentDefinition, AgentIndex>>;
4
+ export { agentFeature };
5
+ export default agentFeature;
@@ -0,0 +1,44 @@
1
+ import { join } from 'node:path';
2
+ import { featureId } from '@zhin.js/plugin-runtime';
3
+ import { defineFeatureProvider, } from '@zhin.js/feature-kit';
4
+ import { AgentIndex } from './agent-index.js';
5
+ import { parseAgentMarkdown } from './definition.js';
6
+ export const agentFeatureId = featureId('zhin.agent');
7
+ const agentFiles = {
8
+ id: 'agent-markdown',
9
+ async *discover(context) {
10
+ const directory = join(context.packageRoot, 'agents');
11
+ const entries = [...await context.host.list(directory)]
12
+ .sort((left, right) => left.name.localeCompare(right.name));
13
+ for (const entry of entries) {
14
+ if (entry.kind !== 'file' || !isAgentFile(entry.name))
15
+ continue;
16
+ yield {
17
+ localName: entry.name.slice(0, -'.agent.md'.length),
18
+ source: join(directory, entry.name),
19
+ target: 'server',
20
+ };
21
+ }
22
+ },
23
+ load(source, context) {
24
+ return context.host.readText(source.source);
25
+ },
26
+ };
27
+ const agentFeature = defineFeatureProvider({
28
+ protocol: 1,
29
+ id: agentFeatureId,
30
+ authoring: {
31
+ conventions: [agentFiles],
32
+ validate: parseAgentMarkdown,
33
+ },
34
+ runtime: {
35
+ project(slots, context) {
36
+ return { value: new AgentIndex(slots, context.snapshot) };
37
+ },
38
+ },
39
+ });
40
+ function isAgentFile(value) {
41
+ return /^[a-z0-9][a-z0-9-]*\.agent\.md$/u.test(value);
42
+ }
43
+ export { agentFeature };
44
+ export default agentFeature;
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@zhin.js/agent-feature",
3
+ "version": "1.0.0",
4
+ "description": "Convention-based Markdown Agent Feature for Zhin Plugin Runtime",
5
+ "type": "module",
6
+ "main": "./lib/index.js",
7
+ "types": "./lib/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/index.d.ts",
11
+ "development": "./src/index.ts",
12
+ "import": "./lib/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "lib",
17
+ "src"
18
+ ],
19
+ "dependencies": {
20
+ "@zhin.js/feature-kit": "1.0.0",
21
+ "@zhin.js/plugin-runtime": "1.0.0"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^26.1.0",
25
+ "typescript": "^6.0.3"
26
+ },
27
+ "zhin": {
28
+ "protocol": 1,
29
+ "type": "feature",
30
+ "entry": "./lib/provider.js",
31
+ "engine": "^1.0.0",
32
+ "featureApi": "1.0.0"
33
+ },
34
+ "engines": {
35
+ "node": "^20.19.0 || >=22.12.0"
36
+ },
37
+ "license": "MIT",
38
+ "private": false,
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/zhinjs/zhin.git",
42
+ "directory": "packages/im/agent-feature"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public",
46
+ "registry": "https://registry.npmjs.org"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc",
50
+ "clean": "rimraf lib",
51
+ "test": "vitest run --root ../../.. packages/im/agent-feature/tests"
52
+ }
53
+ }
@@ -0,0 +1,45 @@
1
+ import type { CapabilitySlot, PluginId, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
2
+ import {
3
+ OwnerCapabilityIndex,
4
+ type OwnerCapabilityEntry,
5
+ } from '@zhin.js/feature-kit';
6
+ import type { AgentDefinition } from './definition.js';
7
+
8
+ export interface AgentDescriptor extends AgentDefinition {
9
+ readonly owner: PluginId;
10
+ readonly qualifiedName: string;
11
+ readonly source: string;
12
+ }
13
+
14
+ export class AgentIndex {
15
+ readonly #index: OwnerCapabilityIndex<AgentDefinition>;
16
+
17
+ constructor(
18
+ slots: readonly Readonly<CapabilitySlot<AgentDefinition>>[],
19
+ snapshot: RuntimeSnapshot,
20
+ ) {
21
+ this.#index = new OwnerCapabilityIndex(slots, snapshot);
22
+ }
23
+
24
+ list(): readonly AgentDescriptor[] {
25
+ return this.#index.entries().map(toDescriptor);
26
+ }
27
+
28
+ visible(requester: PluginId): readonly AgentDescriptor[] {
29
+ return this.#index.visible(requester).map(toDescriptor);
30
+ }
31
+
32
+ get(requester: PluginId, name: string): AgentDescriptor | undefined {
33
+ const entry = this.#index.resolve(requester, name);
34
+ return entry ? toDescriptor(entry) : undefined;
35
+ }
36
+ }
37
+
38
+ function toDescriptor(entry: OwnerCapabilityEntry<AgentDefinition>): AgentDescriptor {
39
+ return Object.freeze({
40
+ ...entry.slot.definition,
41
+ owner: entry.owner,
42
+ qualifiedName: entry.qualifiedName,
43
+ source: entry.source,
44
+ });
45
+ }
@@ -0,0 +1,29 @@
1
+ import type { ValidationContext } from '@zhin.js/feature-kit';
2
+
3
+ const agentBrand = 'zhin.agent/1' as const;
4
+
5
+ export interface AgentDefinition {
6
+ readonly $feature: typeof agentBrand;
7
+ readonly name: string;
8
+ readonly description: string;
9
+ readonly instructions: string;
10
+ }
11
+
12
+ export function parseAgentMarkdown(value: unknown, context: ValidationContext): AgentDefinition {
13
+ if (typeof value !== 'string' || !value.trim()) {
14
+ throw new TypeError(`Agent ${context.source} must contain Markdown instructions`);
15
+ }
16
+ return Object.freeze({
17
+ $feature: agentBrand,
18
+ name: context.localName,
19
+ description: markdownSummary(value, context.localName),
20
+ instructions: value,
21
+ });
22
+ }
23
+
24
+ function markdownSummary(markdown: string, fallback: string): string {
25
+ const line = markdown.split(/\r?\n/u)
26
+ .map((value) => value.trim())
27
+ .find((value) => /^#+\s+\S/u.test(value));
28
+ return line?.replace(/^#+\s*/u, '') ?? fallback;
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './agent-index.js';
2
+ export * from './definition.js';
3
+ export * from './provider.js';
4
+ export { default } from './provider.js';
@@ -0,0 +1,51 @@
1
+ import { join } from 'node:path';
2
+ import { featureId } from '@zhin.js/plugin-runtime';
3
+ import {
4
+ defineFeatureProvider,
5
+ type SourceConvention,
6
+ } from '@zhin.js/feature-kit';
7
+ import { AgentIndex } from './agent-index.js';
8
+ import { parseAgentMarkdown } from './definition.js';
9
+
10
+ export const agentFeatureId = featureId('zhin.agent');
11
+
12
+ const agentFiles: SourceConvention = {
13
+ id: 'agent-markdown',
14
+ async *discover(context) {
15
+ const directory = join(context.packageRoot, 'agents');
16
+ const entries = [...await context.host.list(directory)]
17
+ .sort((left, right) => left.name.localeCompare(right.name));
18
+ for (const entry of entries) {
19
+ if (entry.kind !== 'file' || !isAgentFile(entry.name)) continue;
20
+ yield {
21
+ localName: entry.name.slice(0, -'.agent.md'.length),
22
+ source: join(directory, entry.name),
23
+ target: 'server',
24
+ };
25
+ }
26
+ },
27
+ load(source, context) {
28
+ return context.host.readText(source.source);
29
+ },
30
+ };
31
+
32
+ const agentFeature = defineFeatureProvider({
33
+ protocol: 1,
34
+ id: agentFeatureId,
35
+ authoring: {
36
+ conventions: [agentFiles],
37
+ validate: parseAgentMarkdown,
38
+ },
39
+ runtime: {
40
+ project(slots, context) {
41
+ return { value: new AgentIndex(slots, context.snapshot) };
42
+ },
43
+ },
44
+ });
45
+
46
+ function isAgentFile(value: string): boolean {
47
+ return /^[a-z0-9][a-z0-9-]*\.agent\.md$/u.test(value);
48
+ }
49
+
50
+ export { agentFeature };
51
+ export default agentFeature;