@chendb888/dsh-project-steward 0.0.0-stage → 0.1.1

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/src/tools.js ADDED
@@ -0,0 +1,74 @@
1
+ // 项目管家 · M9 Agent Tools(06§2)。三面共用 schemas.inputs 校验。
2
+ // 后续任务(release/listing/channel/ops/market)向 buildTools 追加。
3
+ import { inputs } from './schemas.js'
4
+ import { buildPipelineTools } from './pipeline-tools.js'
5
+ import { buildListingTools } from './listing-tools.js'
6
+ import { buildOpsTools } from './ops-tools.js'
7
+ import { buildMarketTools } from './market/tools.js'
8
+ import { buildDocsTools } from './docs-tools.js'
9
+
10
+ const ok = data => ({ ok: true, data })
11
+ const fail = e => ({ ok: false, error: { code: e.code || 'ERROR', message: String(e.message || e) } })
12
+ const guard = (schema, args, fn) => {
13
+ const parsed = schema.safeParse(args || {})
14
+ if (!parsed.success) return fail({ code: 'INVALID_INPUT', message: parsed.error.issues.map(i => `${i.path.join('.')}: ${i.message}`).join('; ') })
15
+ return Promise.resolve(fn(parsed.data)).then(ok, fail)
16
+ }
17
+
18
+ export function buildTools(service) {
19
+ return [
20
+ {
21
+ name: 'steward_list',
22
+ description: '列出纳管的项目(可按 type/stage/tag 筛选)。只读。',
23
+ parameters: { type: 'object', properties: { type: { type: 'string' }, stage: { type: 'string' }, tag: { type: 'string' } }, additionalProperties: false },
24
+ execute: async args => ok(service.listProjects(args || {})),
25
+ },
26
+ {
27
+ name: 'steward_get',
28
+ description: '获取单个项目详情。只读。',
29
+ parameters: { type: 'object', properties: { projectId: { type: 'string' } }, required: ['projectId'], additionalProperties: false },
30
+ execute: async args => {
31
+ const p = service.getProject(args.projectId)
32
+ return p ? ok(p) : fail({ code: 'NOT_FOUND', message: `项目 ${args.projectId} 不存在` })
33
+ },
34
+ },
35
+ {
36
+ name: 'steward_register',
37
+ description: '登记一个本地项目进管家(扫描后由用户自选纳管)。',
38
+ parameters: { type: 'object', properties: { name: { type: 'string' }, rootPath: { type: 'string' }, type: { type: 'string' }, tags: { type: 'array', items: { type: 'string' } }, notes: { type: 'string' }, remote: { type: 'string' } }, required: ['name', 'rootPath', 'type'], additionalProperties: false },
39
+ execute: async args => guard(inputs.registerProject, args, d => service.registerProject(d, 'agent')),
40
+ },
41
+ {
42
+ name: 'steward_scan',
43
+ description: '扫描用户选定的目录,提出候选项目(不落盘,由用户勾选纳管)。只读。',
44
+ parameters: { type: 'object', properties: { roots: { type: 'array', items: { type: 'string' } }, exclude: { type: 'array', items: { type: 'string' } } }, required: ['roots'], additionalProperties: false },
45
+ execute: async args => ok(service.scanCandidates(args.roots, { exclude: args.exclude })),
46
+ },
47
+ {
48
+ name: 'steward_overview',
49
+ description: '全店概览:项目计数/阶段分布/异常聚合/最近活动。只读。',
50
+ parameters: { type: 'object', properties: {}, additionalProperties: false },
51
+ execute: async () => ok(service.overview()),
52
+ },
53
+ ...buildPipelineTools(service),
54
+ ...buildListingTools(service),
55
+ ...buildOpsTools(service),
56
+ ...buildMarketTools(service),
57
+ ...buildDocsTools(service),
58
+ ]
59
+ }
60
+
61
+ export function registerStewardTools(ctx, service) {
62
+ const disposers = []
63
+ const defaultOutput = {
64
+ schema: { type: 'object' },
65
+ render: (_args, value) => {
66
+ const v = value && value.ok === false ? `❌ ${value.error?.code || 'ERROR'}: ${value.error?.message || ''}` : JSON.stringify(value?.data ?? value, null, 2)
67
+ return [{ type: 'text', text: String(v).slice(0, 4000) }]
68
+ },
69
+ }
70
+ for (const tool of buildTools(service)) {
71
+ disposers.push(ctx.tools.register({ output: defaultOutput, ...tool }))
72
+ }
73
+ return () => { for (const d of disposers) { try { d() } catch {} } }
74
+ }