|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { createHostContext } from 'devframe/node'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { |
| 5 | + AGENT_ENTRIES, |
| 6 | + ACTION_TOOLS, |
| 7 | + ACTION_TOOL_FREE_MODES, |
| 8 | + AGENT_INSPECTOR, |
| 9 | + AGENT_SAFETY, |
| 10 | + PAGE_AGENT_ENTRIES, |
| 11 | + PANGULAR_ACTIONS, |
| 12 | + READ_ONLY_VARIANTS, |
| 13 | + RPC_INSPECTOR, |
| 14 | + agentInspector, |
| 15 | + agentTags, |
| 16 | + type AgentEntryKind, |
| 17 | + type PangularConfig, |
| 18 | +} from '../config.ts'; |
| 19 | +import { createPangular, type PangularHostOptions } from '../devframe.ts'; |
| 20 | +import { travelServerPlugin } from '../../../../src/app/devtools/travel-plugin.server.ts'; |
| 21 | + |
| 22 | +/** |
| 23 | + * The lists `config.ts` kept by hand before `AGENT_ENTRIES`, sorted. The |
| 24 | + * derived lists must stay the same until a tool is added, changed or removed. |
| 25 | + */ |
| 26 | +const FIXTURE = JSON.parse( |
| 27 | + readFileSync(new URL('./fixtures/agent-metadata-derived.json', import.meta.url), 'utf8'), |
| 28 | +) as { |
| 29 | + AGENT_INSPECTOR: Record<string, string>; |
| 30 | + ACTION_TOOLS: Record<string, string[]>; |
| 31 | + ACTION_TOOL_FREE_MODES: Record<string, string[]>; |
| 32 | + AGENT_SAFETY: Record<string, string>; |
| 33 | + READ_ONLY_VARIANTS: string[]; |
| 34 | + PAGE_AGENT_ENTRIES: string[]; |
| 35 | + agentTags: Record<string, string[] | null>; |
| 36 | +}; |
| 37 | + |
| 38 | +const sorted = (list: readonly string[]) => [...list].sort(); |
| 39 | +const sortedValues = (record: Record<string, readonly string[]>) => |
| 40 | + Object.fromEntries(Object.entries(record).map(([key, list]) => [key, sorted(list)])); |
| 41 | + |
| 42 | +/** What the devframe setup registers for agents, by `AGENT_ENTRIES` kind, without the prefix. */ |
| 43 | +async function registered(config: PangularConfig = {}, host: PangularHostOptions = {}) { |
| 44 | + const ctx = await createHostContext({ |
| 45 | + cwd: process.cwd(), |
| 46 | + mode: 'dev', |
| 47 | + host: { |
| 48 | + mountStatic: () => {}, |
| 49 | + resolveOrigin: () => 'http://localhost', |
| 50 | + getStorageDir: () => '', |
| 51 | + } as never, |
| 52 | + }); |
| 53 | + await createPangular(config, host).setup(ctx as never); |
| 54 | + const definitions = (ctx.rpc as unknown as { definitions: Map<string, { agent?: unknown }> }) |
| 55 | + .definitions; |
| 56 | + const rpc = new Set( |
| 57 | + [...definitions].filter(([, definition]) => definition.agent).map(([name]) => name), |
| 58 | + ); |
| 59 | + const strip = (id: string) => id.replace(/^pangular:/, ''); |
| 60 | + const manifest = ctx.agent.list(); |
| 61 | + const kinds = new Map<string, AgentEntryKind>(); |
| 62 | + const tools = manifest.tools.filter((tool) => tool.id.startsWith('pangular:')); |
| 63 | + for (const tool of tools) kinds.set(strip(tool.id), rpc.has(tool.id) ? 'rpc' : 'tool'); |
| 64 | + for (const resource of manifest.resources.filter((r) => r.id.startsWith('pangular:'))) { |
| 65 | + kinds.set(strip(resource.id), 'resource'); |
| 66 | + } |
| 67 | + return { kinds, tools }; |
| 68 | +} |
| 69 | + |
| 70 | +describe('agent metadata', () => { |
| 71 | + it('derives the same lists config.ts kept by hand', () => { |
| 72 | + expect(AGENT_INSPECTOR).toEqual(FIXTURE.AGENT_INSPECTOR); |
| 73 | + expect(sortedValues(ACTION_TOOLS)).toEqual(FIXTURE.ACTION_TOOLS); |
| 74 | + expect(Object.keys(ACTION_TOOLS)).toEqual([...PANGULAR_ACTIONS]); |
| 75 | + expect(sortedValues(ACTION_TOOL_FREE_MODES)).toEqual(FIXTURE.ACTION_TOOL_FREE_MODES); |
| 76 | + expect(AGENT_SAFETY).toEqual(FIXTURE.AGENT_SAFETY); |
| 77 | + expect(sorted(READ_ONLY_VARIANTS)).toEqual(FIXTURE.READ_ONLY_VARIANTS); |
| 78 | + expect(sorted(PAGE_AGENT_ENTRIES)).toEqual(FIXTURE.PAGE_AGENT_ENTRIES); |
| 79 | + for (const [name, tags] of Object.entries(FIXTURE.agentTags)) { |
| 80 | + expect(agentTags(`pangular:${name}`) ?? null, name).toEqual(tags); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it('has an entry for every registered tool and resource, and nothing else', async () => { |
| 85 | + // Every built-in entry registers under the default config. Plugin tools |
| 86 | + // come from `serverPlugins` and are left out of the table on purpose. |
| 87 | + const { kinds } = await registered({}, { serverPlugins: [travelServerPlugin] }); |
| 88 | + const plugin = [...kinds.keys()].filter((name) => name.startsWith('plugin:')); |
| 89 | + expect(plugin.length).toBeGreaterThan(0); |
| 90 | + for (const name of plugin) kinds.delete(name); |
| 91 | + |
| 92 | + const missing = [...kinds.keys()].filter((name) => !Object.hasOwn(AGENT_ENTRIES, name)); |
| 93 | + expect(missing, 'registered without an entry in AGENT_ENTRIES (config.ts)').toEqual([]); |
| 94 | + const unregistered = Object.keys(AGENT_ENTRIES).filter((name) => !kinds.has(name)); |
| 95 | + expect(unregistered, 'in AGENT_ENTRIES but never registered').toEqual([]); |
| 96 | + for (const [name, kind] of kinds) expect(AGENT_ENTRIES[name]?.kind, name).toBe(kind); |
| 97 | + }); |
| 98 | + |
| 99 | + it('registers each tool with the tier and tags of its entry', async () => { |
| 100 | + const { tools } = await registered(); |
| 101 | + for (const tool of tools) { |
| 102 | + const name = tool.id.replace(/^pangular:/, ''); |
| 103 | + expect(tool.safety, name).toBe(AGENT_ENTRIES[name]?.safety ?? 'read'); |
| 104 | + expect(tool.tags, name).toEqual(AGENT_ENTRIES[name]?.inspectors); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + it('leaves the page entries out of the stdio MCP server', async () => { |
| 109 | + const { kinds } = await registered({}, { pageTools: false }); |
| 110 | + const page = Object.keys(AGENT_ENTRIES).filter((name) => AGENT_ENTRIES[name]?.page); |
| 111 | + expect(page.filter((name) => kinds.has(name))).toEqual([]); |
| 112 | + expect(Object.keys(AGENT_ENTRIES).filter((name) => !kinds.has(name))).toEqual(page); |
| 113 | + }); |
| 114 | + |
| 115 | + it('keeps each entry consistent', () => { |
| 116 | + for (const [name, entry] of Object.entries(AGENT_ENTRIES)) { |
| 117 | + expect(name, name).not.toMatch(/^(pangular|plugin):/); |
| 118 | + if (entry.kind === 'rpc' && entry.inspectors) { |
| 119 | + expect(entry.inspectors, name).toEqual([RPC_INSPECTOR[name]]); |
| 120 | + } |
| 121 | + // A tool that writes for an action has a tier and an inspector to turn it off with. |
| 122 | + if (entry.actions || entry.readOnlyVariant) expect(entry.safety, name).toBeDefined(); |
| 123 | + if (entry.actions) expect(entry.inspectors, name).toBeDefined(); |
| 124 | + if (entry.freeModes) expect(entry.actions, name).toBeDefined(); |
| 125 | + expect(agentInspector(`pangular:${name}`), name).toBe(entry.inspectors?.[0]); |
| 126 | + } |
| 127 | + }); |
| 128 | +}); |
0 commit comments