@xingular/harness 0.0.0-stage → 0.1.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/README.md +269 -2
- package/dist/cli.js +11046 -0
- package/dist/terminal-pi.js +326 -0
- package/package.json +41 -4
- package/src/accounts/accounts.test.ts +223 -0
- package/src/accounts/accounts.ts +522 -0
- package/src/accounts/env.ts +204 -0
- package/src/accounts/secrets.ts +50 -0
- package/src/accounts/store.ts +338 -0
- package/src/capabilities/api.ts +75 -0
- package/src/capabilities/runSnapshot.ts +59 -0
- package/src/capabilities/types.ts +34 -0
- package/src/cli.ts +110 -0
- package/src/core/bus.ts +34 -0
- package/src/core/changes.ts +33 -0
- package/src/core/env.ts +27 -0
- package/src/core/errors.ts +32 -0
- package/src/core/frontmatter.ts +47 -0
- package/src/core/fs.test.ts +38 -0
- package/src/core/fs.ts +92 -0
- package/src/core/index.ts +193 -0
- package/src/core/path.ts +45 -0
- package/src/core/projects.ts +90 -0
- package/src/core/text.ts +39 -0
- package/src/harness.ts +289 -0
- package/src/index.ts +69 -0
- package/src/mcp/connections.ts +441 -0
- package/src/mcp/manager.ts +130 -0
- package/src/mcp/oauth.ts +195 -0
- package/src/mcp/service.test.ts +376 -0
- package/src/mcp/service.ts +567 -0
- package/src/mcp/types.ts +105 -0
- package/src/memory/api.test.ts +82 -0
- package/src/memory/api.ts +287 -0
- package/src/memory/blocks.test.ts +78 -0
- package/src/memory/blocks.ts +406 -0
- package/src/memory/caller.ts +57 -0
- package/src/memory/dispatch.ts +59 -0
- package/src/memory/facts.test.ts +41 -0
- package/src/memory/facts.ts +248 -0
- package/src/memory/graph.ts +103 -0
- package/src/memory/health.test.ts +130 -0
- package/src/memory/health.ts +74 -0
- package/src/memory/maintainer.ts +272 -0
- package/src/memory/maintenance.test.ts +176 -0
- package/src/memory/maintenance.ts +330 -0
- package/src/memory/migrate.ts +79 -0
- package/src/memory/operations.ts +138 -0
- package/src/memory/proposals.test.ts +168 -0
- package/src/memory/proposals.ts +576 -0
- package/src/memory/recall.test.ts +104 -0
- package/src/memory/recall.ts +271 -0
- package/src/memory/references.ts +77 -0
- package/src/memory/runContext.ts +93 -0
- package/src/memory/secrets.ts +6 -0
- package/src/memory/store.test.ts +66 -0
- package/src/memory/store.ts +385 -0
- package/src/memory/wiki.test.ts +47 -0
- package/src/memory/wiki.ts +422 -0
- package/src/memory/wikiIndex.test.ts +88 -0
- package/src/memory/wikiIndex.ts +116 -0
- package/src/plugins/api.ts +181 -0
- package/src/plugins/attachments.test.ts +84 -0
- package/src/plugins/attachments.ts +128 -0
- package/src/plugins/builtin.ts +82 -0
- package/src/plugins/catalogue.test.ts +254 -0
- package/src/plugins/catalogue.ts +250 -0
- package/src/plugins/cli.ts +184 -0
- package/src/plugins/discover.ts +151 -0
- package/src/plugins/firstParty.test.ts +143 -0
- package/src/plugins/firstParty.ts +129 -0
- package/src/plugins/hooks.test.ts +250 -0
- package/src/plugins/hooks.ts +239 -0
- package/src/plugins/identity.ts +37 -0
- package/src/plugins/install.test.ts +107 -0
- package/src/plugins/install.ts +106 -0
- package/src/plugins/inventory.ts +123 -0
- package/src/plugins/manifest.ts +44 -0
- package/src/plugins/plugins.test.ts +205 -0
- package/src/plugins/plugins.ts +184 -0
- package/src/plugins/proposal.ts +121 -0
- package/src/plugins/runtime.test.ts +40 -0
- package/src/plugins/runtime.ts +82 -0
- package/src/plugins/trust.ts +142 -0
- package/src/plugins/types.ts +130 -0
- package/src/prompt/compile.test.ts +261 -0
- package/src/prompt/compile.ts +732 -0
- package/src/server/methods.ts +308 -0
- package/src/server/remote.ts +77 -0
- package/src/server/rpc.ts +116 -0
- package/src/server/serve.ts +28 -0
- package/src/server/server.test.ts +174 -0
- package/src/sessions/approvals.ts +118 -0
- package/src/sessions/contracts.ts +299 -0
- package/src/sessions/conversation.test.ts +150 -0
- package/src/sessions/extensionSession.test.ts +74 -0
- package/src/sessions/extensionUi.test.ts +65 -0
- package/src/sessions/extensionUi.ts +102 -0
- package/src/sessions/history.ts +51 -0
- package/src/sessions/lineageJoin.ts +75 -0
- package/src/sessions/policy.ts +32 -0
- package/src/sessions/pricing.ts +60 -0
- package/src/sessions/reasoningSummaries.ts +59 -0
- package/src/sessions/registry.ts +294 -0
- package/src/sessions/runIndex.ts +99 -0
- package/src/sessions/runStore.test.ts +152 -0
- package/src/sessions/runStore.ts +332 -0
- package/src/sessions/runner.ts +556 -0
- package/src/sessions/runtimeResources.test.ts +61 -0
- package/src/sessions/sessions.test.ts +434 -0
- package/src/sessions/sessions.ts +295 -0
- package/src/skills/registry.test.ts +104 -0
- package/src/skills/registry.ts +394 -0
- package/src/terminal/cli.ts +149 -0
- package/src/terminal/client.ts +74 -0
- package/src/terminal/controller.ts +227 -0
- package/src/terminal/piExtension.ts +208 -0
- package/src/terminal/pluginControl.test.ts +146 -0
- package/src/terminal/pluginMenu.ts +112 -0
- package/src/terminal/socket.ts +182 -0
- package/src/terminal/standalone.test.ts +115 -0
- package/src/terminal/standalone.ts +126 -0
- package/src/terminal/terminal.test.ts +302 -0
- package/src/terminal/terminals.ts +429 -0
- package/src/tools/askUser.ts +45 -0
- package/src/tools/definitions.ts +187 -0
- package/src/tools/memoryTools.ts +22 -0
- package/src/tools/pluginPropose.ts +41 -0
- package/src/tools/types.ts +36 -0
- package/src/web/safeRequest.ts +171 -0
- package/src/web/text.ts +53 -0
- package/src/web/tools.ts +134 -0
- package/src/web/web.test.ts +141 -0
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
// Canonical skills, prompts, and rules under `<data root>/registry/`. Each item is a Markdown
|
|
2
|
+
// file (a skill may also be a folder with `SKILL.md`) with optional `name`/`description`
|
|
3
|
+
// frontmatter. These files are the single source of truth. Items are enabled globally and per
|
|
4
|
+
// project; enabled items are loaded automatically, and any item can be retrieved on demand.
|
|
5
|
+
//
|
|
6
|
+
// Built-in items ship with the harness or the application that embeds it, in folders laid out
|
|
7
|
+
// like `registry/`. They are read-only and on everywhere until a person switches one off; an
|
|
8
|
+
// item of the same id in the data root replaces the built-in one.
|
|
9
|
+
import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { basename, dirname, join } from 'node:path'
|
|
11
|
+
|
|
12
|
+
import { invalid } from '../core/errors'
|
|
13
|
+
import * as frontmatter from '../core/frontmatter'
|
|
14
|
+
import { atomicWrite, now, readJsonOr, safeRelative } from '../core/fs'
|
|
15
|
+
import { findProject } from '../core/projects'
|
|
16
|
+
import { compareText, lines, takeChars } from '../core/text'
|
|
17
|
+
import * as store from '../memory/store'
|
|
18
|
+
import * as attachments from '../plugins/attachments'
|
|
19
|
+
|
|
20
|
+
export const KINDS = ['skills', 'prompts', 'rules'] as const
|
|
21
|
+
export type RegistryKind = (typeof KINDS)[number]
|
|
22
|
+
|
|
23
|
+
export interface RegistryItem {
|
|
24
|
+
/** `<kind>/<name>`, stable across edits. */
|
|
25
|
+
id: string
|
|
26
|
+
kind: string
|
|
27
|
+
name: string
|
|
28
|
+
description: string
|
|
29
|
+
path: string
|
|
30
|
+
/** Set on an item that ships with the harness or the application, not on a person's own. */
|
|
31
|
+
builtin?: true
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SkillBundleInput {
|
|
35
|
+
name: string
|
|
36
|
+
files: { path: string; content: string; encoding: 'utf8' | 'base64' }[]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const MAX_SKILL_BUNDLES = 20
|
|
40
|
+
const MAX_SKILL_FILES = 100
|
|
41
|
+
const MAX_SKILL_IMPORT_BYTES = 10 * 1024 * 1024
|
|
42
|
+
|
|
43
|
+
let builtinDirs: string[] = []
|
|
44
|
+
|
|
45
|
+
/** The folders built-in items are read from, each laid out like `registry/`. Later folders win. */
|
|
46
|
+
export function configureBuiltins(directories: readonly string[]): void {
|
|
47
|
+
builtinDirs = [...directories]
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function itemFrom(kind: string, stem: string, file: string): RegistryItem | null {
|
|
51
|
+
let text: string
|
|
52
|
+
try {
|
|
53
|
+
text = readFileSync(file, 'utf8')
|
|
54
|
+
} catch {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
const parsed = frontmatter.parse(text)
|
|
58
|
+
const name = parsed?.front.name
|
|
59
|
+
const description = parsed?.front.description
|
|
60
|
+
// A frontmatter block whose name or description is not text is treated as no frontmatter.
|
|
61
|
+
const usable = parsed !== null && (name === undefined || name === null || typeof name === 'string') && (description === undefined || description === null || typeof description === 'string')
|
|
62
|
+
const body = usable ? parsed.body : text
|
|
63
|
+
return {
|
|
64
|
+
id: `${kind}/${stem}`,
|
|
65
|
+
kind,
|
|
66
|
+
name: usable && typeof name === 'string' ? name : stem.replace(/[-_]/g, ' '),
|
|
67
|
+
description: usable && typeof description === 'string' ? description : takeChars(lines(body).find((line) => line.trim() && !line.startsWith('#')) ?? '', 160),
|
|
68
|
+
path: file
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const stemOf = (name: string): string => (name.lastIndexOf('.') > 0 ? name.slice(0, name.lastIndexOf('.')) : name)
|
|
73
|
+
|
|
74
|
+
/** The name and description of a skill or prompt file anywhere on disk, read the way a registry item is. */
|
|
75
|
+
export function describeFile(file: string): { name: string; description: string } | null {
|
|
76
|
+
const stem = basename(file) === 'SKILL.md' ? basename(dirname(file)) : stemOf(basename(file))
|
|
77
|
+
const item = itemFrom('', stem, file)
|
|
78
|
+
return item && { name: item.name, description: item.description }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function itemsIn(directory: string): RegistryItem[] {
|
|
82
|
+
const items: RegistryItem[] = []
|
|
83
|
+
for (const kind of KINDS) {
|
|
84
|
+
let names: string[]
|
|
85
|
+
try {
|
|
86
|
+
names = readdirSync(join(directory, kind))
|
|
87
|
+
} catch {
|
|
88
|
+
continue
|
|
89
|
+
}
|
|
90
|
+
for (const name of names) {
|
|
91
|
+
const path = join(directory, kind, name)
|
|
92
|
+
const stem = stemOf(name)
|
|
93
|
+
if (stem.startsWith('.')) continue
|
|
94
|
+
const file = store.isDir(path) ? join(path, 'SKILL.md') : name.endsWith('.md') ? path : null
|
|
95
|
+
const item = file && itemFrom(kind, stem, file)
|
|
96
|
+
if (item) items.push(item)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return items
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function list(root: string): RegistryItem[] {
|
|
103
|
+
const items = new Map<string, RegistryItem>()
|
|
104
|
+
for (const directory of builtinDirs) for (const item of itemsIn(directory)) items.set(item.id, { ...item, builtin: true })
|
|
105
|
+
for (const item of itemsIn(join(root, 'registry'))) items.set(item.id, item)
|
|
106
|
+
return [...items.values()].sort((left, right) => compareText(left.id, right.id))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function enablementPath(root: string): string {
|
|
110
|
+
return join(root, 'config/registry-enabled.json')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Key for items enabled everywhere. Older data roots used `personal`, which is read the same way. */
|
|
114
|
+
export const GLOBAL_KEY = 'global'
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The registry's earlier enablement file as it is on disk: ids keyed by project id, or `global`
|
|
118
|
+
* for everyone. The choices now live with every other plugin's (`plugins/attachments.ts`), which
|
|
119
|
+
* reads this file until the first change is saved.
|
|
120
|
+
*/
|
|
121
|
+
export function enablement(root: string): Record<string, unknown> {
|
|
122
|
+
const map = readJsonOr<unknown>(enablementPath(root), {})
|
|
123
|
+
return map && typeof map === 'object' && !Array.isArray(map) ? (map as Record<string, unknown>) : {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function enablementIds(map: Record<string, unknown>, key: string): string[] {
|
|
127
|
+
const ids = map[key]
|
|
128
|
+
return Array.isArray(ids) ? ids.filter((id): id is string => typeof id === 'string') : []
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Built-in items a person switched off. Everything else built in is on everywhere. */
|
|
132
|
+
export const BUILTIN_OFF_KEY = 'builtinOff'
|
|
133
|
+
|
|
134
|
+
/** Whether an id names a registry item, as `<kind>/<name>`. */
|
|
135
|
+
export const isItemId = (id: string): boolean => KINDS.some((kind) => id.startsWith(`${kind}/`))
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Items active for a caller. A built-in is on until it is switched off; a person's own is off
|
|
139
|
+
* until it is switched on, everywhere, for the caller's project, or for the calling agent.
|
|
140
|
+
*/
|
|
141
|
+
export function enabledFor(root: string, projectId: string | null, agentId: string | null = null): RegistryItem[] {
|
|
142
|
+
const choices = attachments.load(root)
|
|
143
|
+
return list(root).filter((item) => attachments.attached(choices, [item.id], { agentId, projectId }, !!item.builtin))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** An item's Markdown body without frontmatter. */
|
|
147
|
+
export function itemBody(item: RegistryItem): string {
|
|
148
|
+
try {
|
|
149
|
+
const text = readFileSync(item.path, 'utf8')
|
|
150
|
+
return frontmatter.split(text)?.body ?? text
|
|
151
|
+
} catch {
|
|
152
|
+
return ''
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface RegistryResources {
|
|
157
|
+
ids: string[]
|
|
158
|
+
skillPaths: string[]
|
|
159
|
+
promptPaths: string[]
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** What a Pi session needs to load enabled items natively. Rule text reaches the model through the compiled prompt. */
|
|
163
|
+
export function resources(root: string, projectId: string | null, agentId: string | null = null): RegistryResources {
|
|
164
|
+
const items = enabledFor(root, projectId, agentId)
|
|
165
|
+
return {
|
|
166
|
+
ids: items.map((item) => item.id),
|
|
167
|
+
skillPaths: items.filter((item) => item.kind === 'skills').map((item) => item.path),
|
|
168
|
+
promptPaths: items.filter((item) => item.kind === 'prompts').map((item) => item.path)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Prefix of the entries an earlier version of the harness linked into a Pi agent folder's `skills/`.
|
|
173
|
+
const LINKED_SKILL_PREFIX = 'agentx-'
|
|
174
|
+
|
|
175
|
+
function isLinkedSkill(path: string): boolean {
|
|
176
|
+
let meta
|
|
177
|
+
try {
|
|
178
|
+
meta = lstatSync(path)
|
|
179
|
+
} catch {
|
|
180
|
+
return false
|
|
181
|
+
}
|
|
182
|
+
if (meta.isSymbolicLink()) return true
|
|
183
|
+
if (!meta.isDirectory()) return false
|
|
184
|
+
// A single-file skill was linked as `agentx-<name>/SKILL.md` inside a folder the harness made.
|
|
185
|
+
try {
|
|
186
|
+
const entries = readdirSync(path)
|
|
187
|
+
return entries.length === 1 && entries[0] === 'SKILL.md' && lstatSync(join(path, 'SKILL.md')).isSymbolicLink()
|
|
188
|
+
} catch {
|
|
189
|
+
return false
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Removes the `agentx-<name>` links an earlier version kept in a terminal session's `skills/`
|
|
195
|
+
* folder, leaving the user's own skills alone. Enabled skills now reach a session as paths.
|
|
196
|
+
*/
|
|
197
|
+
export function removeLinkedSkills(skillsDir: string): void {
|
|
198
|
+
let names: string[]
|
|
199
|
+
try {
|
|
200
|
+
names = readdirSync(skillsDir)
|
|
201
|
+
} catch {
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
for (const name of names) {
|
|
205
|
+
const path = join(skillsDir, name)
|
|
206
|
+
if (!name.startsWith(LINKED_SKILL_PREFIX) || !isLinkedSkill(path)) continue
|
|
207
|
+
if (lstatSync(path).isSymbolicLink()) unlinkSync(path)
|
|
208
|
+
else rmSync(path, { recursive: true, force: true })
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface RegistryState {
|
|
213
|
+
items: RegistryItem[]
|
|
214
|
+
enabled: Record<string, unknown>
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Enablement as a person sees it, in the earlier file's shape: ids keyed by project id, `global`
|
|
219
|
+
* for everyone (built-ins that are on count as enabled everywhere), and the built-ins switched off.
|
|
220
|
+
*/
|
|
221
|
+
function shown(items: readonly RegistryItem[], choices: attachments.Attachments): Record<string, unknown> {
|
|
222
|
+
const builtin = items.filter((item) => item.builtin).map((item) => item.id)
|
|
223
|
+
const off = choices.global.off.filter((id) => builtin.includes(id))
|
|
224
|
+
const global = choices.global.on.filter((id) => isItemId(id) && !builtin.includes(id))
|
|
225
|
+
const map: Record<string, unknown> = { [GLOBAL_KEY]: [...global, ...builtin.filter((id) => !off.includes(id))] }
|
|
226
|
+
for (const [projectId, entries] of Object.entries(choices.projects)) map[projectId] = entries.on.filter(isItemId)
|
|
227
|
+
if (off.length) map[BUILTIN_OFF_KEY] = off
|
|
228
|
+
return map
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function state(root: string): RegistryState {
|
|
232
|
+
const items = list(root)
|
|
233
|
+
return { items, enabled: shown(items, attachments.load(root)) }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Sets enabled items for a project, or globally when `projectId` is `null`. */
|
|
237
|
+
export function setEnabled(root: string, projectId: string | null, ids: string[]): RegistryState {
|
|
238
|
+
if (projectId) findProject(root, projectId)
|
|
239
|
+
const items = list(root)
|
|
240
|
+
const unknown = ids.find((id) => !items.some((item) => item.id === id))
|
|
241
|
+
if (unknown !== undefined) throw invalid(`Unknown registry item ${unknown}.`)
|
|
242
|
+
const builtin = items.filter((item) => item.builtin).map((item) => item.id)
|
|
243
|
+
const choices = attachments.load(root)
|
|
244
|
+
const entries = projectId ? (choices.projects[projectId] ??= { on: [], off: [] }) : choices.global
|
|
245
|
+
// Only the registry's own ids are replaced; other plugins' choices in the same scope stay.
|
|
246
|
+
entries.on = [...entries.on.filter((id) => !isItemId(id)), ...ids.filter((id) => !builtin.includes(id))]
|
|
247
|
+
entries.off = entries.off.filter((id) => !isItemId(id) || (!ids.includes(id) && !(!projectId && builtin.includes(id))))
|
|
248
|
+
// A built-in left out of the global list was switched off.
|
|
249
|
+
if (!projectId) entries.off.push(...builtin.filter((id) => !ids.includes(id)))
|
|
250
|
+
attachments.save(root, choices)
|
|
251
|
+
return { items, enabled: shown(items, choices) }
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function kindDir(root: string, kind: string): string {
|
|
255
|
+
if (!(KINDS as readonly string[]).includes(kind)) throw invalid('Unknown registry kind.')
|
|
256
|
+
return join(root, 'registry', kind)
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Creates a starter item from a name: a Markdown file, or a skill folder with `SKILL.md`. */
|
|
260
|
+
export function create(root: string, kind: string, rawName: string): RegistryState {
|
|
261
|
+
const directory = kindDir(root, kind)
|
|
262
|
+
const name = rawName.trim()
|
|
263
|
+
const slug = Array.from(name.toLowerCase(), (character) => (/^[a-z0-9]$/.test(character) ? character : '-')).join('').split('-').filter(Boolean).join('-')
|
|
264
|
+
if (!slug || slug.length > 64) throw invalid('Use a short name with letters or numbers.')
|
|
265
|
+
const description = 'Describe when to use this.'
|
|
266
|
+
const target = kind === 'skills' ? join(directory, slug) : join(directory, `${slug}.md`)
|
|
267
|
+
let exists = true
|
|
268
|
+
try {
|
|
269
|
+
lstatSync(target)
|
|
270
|
+
} catch {
|
|
271
|
+
exists = false
|
|
272
|
+
}
|
|
273
|
+
if (exists) throw invalid('An item with that name already exists.')
|
|
274
|
+
if (kind === 'skills') atomicWrite(join(target, 'SKILL.md'), `---\nname: ${slug}\ndescription: ${description}\n---\n\n# ${name}\n\nSteps the agent should follow.\n`)
|
|
275
|
+
else atomicWrite(target, `---\nname: ${name}\ndescription: ${description}\n---\n\n`)
|
|
276
|
+
return state(root)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Imports complete user-owned skill folders without replacing existing or built-in items. */
|
|
280
|
+
export function importSkills(root: string, bundles: SkillBundleInput[]): RegistryState {
|
|
281
|
+
if (!Array.isArray(bundles) || bundles.length === 0 || bundles.length > MAX_SKILL_BUNDLES) {
|
|
282
|
+
throw invalid(`Import between 1 and ${MAX_SKILL_BUNDLES} skills at a time.`)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const directory = kindDir(root, 'skills')
|
|
286
|
+
mkdirSync(directory, { recursive: true })
|
|
287
|
+
const existing = new Set(list(root).map((item) => item.id))
|
|
288
|
+
const seen = new Set<string>()
|
|
289
|
+
let totalBytes = 0
|
|
290
|
+
const prepared = bundles.map((bundle) => {
|
|
291
|
+
if (!bundle || typeof bundle.name !== 'string' || !Array.isArray(bundle.files) || bundle.files.length === 0 || bundle.files.length > MAX_SKILL_FILES) {
|
|
292
|
+
throw invalid('Each skill needs a name and a bounded list of files.')
|
|
293
|
+
}
|
|
294
|
+
const name = bundle.name.trim()
|
|
295
|
+
if (name.length > 64) throw invalid('Use a skill name with at most 64 letters or numbers.')
|
|
296
|
+
const slug = Array.from(name.toLowerCase(), (character) => (/^[a-z0-9]$/.test(character) ? character : '-')).join('').split('-').filter(Boolean).join('-')
|
|
297
|
+
if (!slug || slug.length > 64) throw invalid('Use a skill name with at most 64 letters or numbers.')
|
|
298
|
+
const id = `skills/${slug}`
|
|
299
|
+
if (existing.has(id) || seen.has(id)) throw invalid(`The skill “${name}” already exists.`)
|
|
300
|
+
try {
|
|
301
|
+
lstatSync(join(directory, slug))
|
|
302
|
+
throw invalid(`The skill “${name}” already exists.`)
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
305
|
+
}
|
|
306
|
+
seen.add(id)
|
|
307
|
+
|
|
308
|
+
const files = new Map<string, Uint8Array>()
|
|
309
|
+
for (const file of bundle.files) {
|
|
310
|
+
if (!file || typeof file.path !== 'string' || file.path.length > 240 || typeof file.content !== 'string' || !['utf8', 'base64'].includes(file.encoding)) {
|
|
311
|
+
throw invalid(`The skill “${name}” contains an invalid file.`)
|
|
312
|
+
}
|
|
313
|
+
let relative: string
|
|
314
|
+
try {
|
|
315
|
+
relative = safeRelative(file.path)
|
|
316
|
+
} catch {
|
|
317
|
+
throw invalid(`The skill “${name}” contains a path outside its folder.`)
|
|
318
|
+
}
|
|
319
|
+
if (relative.includes('\0') || relative.split('/').some((part) => part.startsWith('.') || part.includes(':'))) {
|
|
320
|
+
throw invalid(`The skill “${name}” contains an unsupported file path.`)
|
|
321
|
+
}
|
|
322
|
+
const portablePath = relative.normalize('NFC').toLowerCase()
|
|
323
|
+
if ([...files.keys()].some((path) => path.normalize('NFC').toLowerCase() === portablePath)) throw invalid(`The skill “${name}” contains a duplicate file path.`)
|
|
324
|
+
let byteLength: number
|
|
325
|
+
if (file.encoding === 'base64') {
|
|
326
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(file.content)) {
|
|
327
|
+
throw invalid(`The file “${relative}” is not valid base64.`)
|
|
328
|
+
}
|
|
329
|
+
if (file.content.length > Math.ceil(MAX_SKILL_IMPORT_BYTES / 3) * 4) throw invalid('The skill import is larger than 10 MiB.')
|
|
330
|
+
byteLength = Math.floor(file.content.length * 3 / 4) - (file.content.endsWith('==') ? 2 : file.content.endsWith('=') ? 1 : 0)
|
|
331
|
+
} else {
|
|
332
|
+
byteLength = Buffer.byteLength(file.content, 'utf8')
|
|
333
|
+
}
|
|
334
|
+
totalBytes += byteLength
|
|
335
|
+
if (totalBytes > MAX_SKILL_IMPORT_BYTES) throw invalid('The skill import is larger than 10 MiB.')
|
|
336
|
+
const bytes = file.encoding === 'base64' ? Buffer.from(file.content, 'base64') : Buffer.from(file.content, 'utf8')
|
|
337
|
+
if (file.encoding === 'base64' && bytes.toString('base64') !== file.content) throw invalid(`The file “${relative}” is not valid base64.`)
|
|
338
|
+
files.set(relative, bytes)
|
|
339
|
+
}
|
|
340
|
+
if (!files.has('SKILL.md')) throw invalid(`The skill “${name}” must include SKILL.md at its root.`)
|
|
341
|
+
return { name, slug, files }
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
const staging = mkdtempSync(join(directory, '.agent-template-import-'))
|
|
345
|
+
const installed: string[] = []
|
|
346
|
+
try {
|
|
347
|
+
for (const bundle of prepared) {
|
|
348
|
+
const target = join(staging, bundle.slug)
|
|
349
|
+
for (const [relative, bytes] of bundle.files) {
|
|
350
|
+
const path = join(target, ...relative.split('/'))
|
|
351
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
352
|
+
writeFileSync(path, bytes, { flag: 'wx' })
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
for (const bundle of prepared) {
|
|
356
|
+
const target = join(directory, bundle.slug)
|
|
357
|
+
renameSync(join(staging, bundle.slug), target)
|
|
358
|
+
installed.push(target)
|
|
359
|
+
}
|
|
360
|
+
} catch (error) {
|
|
361
|
+
for (const target of installed) rmSync(target, { recursive: true, force: true })
|
|
362
|
+
throw error
|
|
363
|
+
} finally {
|
|
364
|
+
rmSync(staging, { recursive: true, force: true })
|
|
365
|
+
}
|
|
366
|
+
return state(root)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Writes a skill, prompt, or rule (an accepted proposal), keeping an existing item's location.
|
|
371
|
+
* The previous content and the new content are recorded as Wiki revisions of the item.
|
|
372
|
+
*/
|
|
373
|
+
export function writeItem(root: string, kind: string, name: string, description: string, body: string, reason: string): string {
|
|
374
|
+
const plural = kind === 'skill' ? 'skills' : kind === 'prompt' ? 'prompts' : kind === 'rule' ? 'rules' : null
|
|
375
|
+
if (!plural) throw invalid('Only skills, prompts, and rules can be written.')
|
|
376
|
+
const stem = store.slug(name)
|
|
377
|
+
const id = `${plural}/${stem}`
|
|
378
|
+
const existing = list(root).find((item) => item.id === id)
|
|
379
|
+
// A built-in is never edited in place: the accepted text becomes the person's own item.
|
|
380
|
+
const path = existing && !existing.builtin ? existing.path : plural === 'skills' ? join(kindDir(root, plural), stem, 'SKILL.md') : join(kindDir(root, plural), `${stem}.md`)
|
|
381
|
+
const text = frontmatter.serialize({ name: plural === 'skills' ? stem : name.trim(), ...(description.trim() ? { description: description.trim() } : {}) }, body)
|
|
382
|
+
const wiki = store.wikiDir(root)
|
|
383
|
+
if (existing) {
|
|
384
|
+
try {
|
|
385
|
+
store.snapshot(wiki, id, readFileSync(existing.path, 'utf8'), 'user', 'Before accepted proposal')
|
|
386
|
+
} catch {
|
|
387
|
+
// Nothing readable to keep.
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
atomicWrite(path, text)
|
|
391
|
+
store.snapshot(wiki, id, text, 'user', reason)
|
|
392
|
+
store.log(wiki, `- ${now()} · user · ${existing ? 'updated' : 'created'} \`${id}\` · ${reason}`)
|
|
393
|
+
return id
|
|
394
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// `agentx-wiki` and `agentx-mcp`: the harness's memory, Wiki, and connected apps as commands,
|
|
2
|
+
// for a `pi` terminal session's shell. Each command maps onto one call to the session socket.
|
|
3
|
+
import type { WikiCall } from '../tools/definitions'
|
|
4
|
+
import { connectionFromEnv, socketCall } from './client'
|
|
5
|
+
|
|
6
|
+
export const WIKI_COMMAND = 'agentx-wiki'
|
|
7
|
+
export const APPS_COMMAND = 'agentx-mcp'
|
|
8
|
+
|
|
9
|
+
export const USAGE = `Usage: ${WIKI_COMMAND} <command> [arguments]
|
|
10
|
+
|
|
11
|
+
entrypoint Your context pack: memory blocks, rules, skills, project map
|
|
12
|
+
search <query> [--kind K] [--namespace NS]
|
|
13
|
+
Search every namespace (K: page, fact, skill, rule, prompt)
|
|
14
|
+
read <id or path> Read one page, fact, skill, rule, or prompt
|
|
15
|
+
list [namespace] [--kind K] List namespaces and pages, or all entries of a kind
|
|
16
|
+
skill [name] Load a skill, or list skills
|
|
17
|
+
facts [query] [--namespace NS] Recall facts (also: memory)
|
|
18
|
+
history <query> [--scope all] [--since YYYY-MM-DD]
|
|
19
|
+
Search earlier conversations and runs (also: recall)
|
|
20
|
+
history-read <run id> Read one earlier run (also: recall-read)
|
|
21
|
+
block append <label> <text> Add to a memory block (user, project, or a shared block)
|
|
22
|
+
block replace <label> --old <text> --new <text>
|
|
23
|
+
Replace one exact passage in a memory block
|
|
24
|
+
block rethink <label> <text> Rewrite a whole memory block
|
|
25
|
+
propose <json | -> Record knowledge, e.g. {"kind":"fact","title":"…","body":"…",
|
|
26
|
+
"type":"preference","sourceRefs":[{"kind":"run","ref":"current"}]}
|
|
27
|
+
`
|
|
28
|
+
|
|
29
|
+
export const APPS_USAGE = `Usage: ${APPS_COMMAND} <command>
|
|
30
|
+
|
|
31
|
+
list The app and connected-app tools this session may use
|
|
32
|
+
call <tool-name> '<JSON arguments>'
|
|
33
|
+
Use one of them
|
|
34
|
+
`
|
|
35
|
+
|
|
36
|
+
export type CliCommand = { kind: 'call'; call: WikiCall; params: Record<string, unknown> } | { kind: 'help' } | { kind: 'stdin-propose' }
|
|
37
|
+
|
|
38
|
+
function options(args: string[]): { positional: string[]; flags: Record<string, string> } {
|
|
39
|
+
const positional: string[] = []
|
|
40
|
+
const flags: Record<string, string> = {}
|
|
41
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
42
|
+
const arg = args[index]
|
|
43
|
+
if (arg.startsWith('--') && index + 1 < args.length) {
|
|
44
|
+
flags[arg.slice(2)] = args[index + 1]
|
|
45
|
+
index += 1
|
|
46
|
+
} else {
|
|
47
|
+
positional.push(arg)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { positional, flags }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function parseCli(argv: string[]): CliCommand {
|
|
54
|
+
const [command, ...rest] = argv
|
|
55
|
+
const { positional, flags } = options(rest)
|
|
56
|
+
const joined = positional.join(' ').trim()
|
|
57
|
+
switch (command) {
|
|
58
|
+
case 'entrypoint':
|
|
59
|
+
return { kind: 'call', call: 'wiki.entrypoint', params: {} }
|
|
60
|
+
case 'search':
|
|
61
|
+
if (!joined) return { kind: 'help' }
|
|
62
|
+
return { kind: 'call', call: 'wiki.search', params: { query: joined, ...(flags.kind ? { kinds: [flags.kind] } : {}), ...(flags.namespace ? { namespace: flags.namespace } : {}) } }
|
|
63
|
+
case 'read':
|
|
64
|
+
if (!joined) return { kind: 'help' }
|
|
65
|
+
return { kind: 'call', call: 'wiki.read', params: { id: joined } }
|
|
66
|
+
case 'list':
|
|
67
|
+
return { kind: 'call', call: 'wiki.list', params: { ...(joined ? { namespace: joined } : {}), ...(flags.kind ? { kind: flags.kind } : {}) } }
|
|
68
|
+
case 'skill':
|
|
69
|
+
return { kind: 'call', call: 'wiki.skill', params: joined ? { name: joined } : {} }
|
|
70
|
+
case 'facts':
|
|
71
|
+
case 'memory':
|
|
72
|
+
return { kind: 'call', call: 'wiki.memory', params: { ...(joined ? { query: joined } : {}), ...(flags.namespace ? { namespace: flags.namespace } : {}) } }
|
|
73
|
+
case 'history':
|
|
74
|
+
case 'recall':
|
|
75
|
+
if (!joined) return { kind: 'help' }
|
|
76
|
+
return { kind: 'call', call: 'recall.search', params: { query: joined, ...(flags.scope ? { scope: flags.scope } : {}), ...(flags.since ? { since: flags.since } : {}) } }
|
|
77
|
+
case 'history-read':
|
|
78
|
+
case 'recall-read':
|
|
79
|
+
if (!joined) return { kind: 'help' }
|
|
80
|
+
return { kind: 'call', call: 'recall.read', params: { run: joined } }
|
|
81
|
+
case 'block': {
|
|
82
|
+
const [action, label, ...words] = positional
|
|
83
|
+
const body = words.join(' ').trim()
|
|
84
|
+
if (action === 'append' && label && body) return { kind: 'call', call: 'memory.append', params: { block: label, text: body } }
|
|
85
|
+
if (action === 'rethink' && label && body) return { kind: 'call', call: 'memory.rethink', params: { block: label, body } }
|
|
86
|
+
if (action === 'replace' && label && flags.old !== undefined && flags.new !== undefined) return { kind: 'call', call: 'memory.replace', params: { block: label, old: flags.old, new: flags.new } }
|
|
87
|
+
return { kind: 'help' }
|
|
88
|
+
}
|
|
89
|
+
case 'propose': {
|
|
90
|
+
if (joined === '-') return { kind: 'stdin-propose' }
|
|
91
|
+
try {
|
|
92
|
+
const params = JSON.parse(joined) as unknown
|
|
93
|
+
if (params && typeof params === 'object' && !Array.isArray(params)) return { kind: 'call', call: 'wiki.propose', params: params as Record<string, unknown> }
|
|
94
|
+
} catch {
|
|
95
|
+
// Falls through to help.
|
|
96
|
+
}
|
|
97
|
+
return { kind: 'help' }
|
|
98
|
+
}
|
|
99
|
+
default:
|
|
100
|
+
return { kind: 'help' }
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function readStdin(): Promise<string> {
|
|
105
|
+
let text = ''
|
|
106
|
+
for await (const chunk of process.stdin) text += chunk.toString()
|
|
107
|
+
return text
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error))
|
|
111
|
+
|
|
112
|
+
/** Runs `agentx-wiki <argv>`; returns the exit code. */
|
|
113
|
+
export async function runWikiCli(argv: string[]): Promise<number> {
|
|
114
|
+
const command = parseCli(argv)
|
|
115
|
+
if (command.kind === 'help') {
|
|
116
|
+
process.stdout.write(USAGE)
|
|
117
|
+
return argv.length ? 2 : 0
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const connection = connectionFromEnv()
|
|
121
|
+
const result =
|
|
122
|
+
command.kind === 'stdin-propose'
|
|
123
|
+
? await socketCall<string>(connection, 'wiki.propose', JSON.parse(await readStdin()) as Record<string, unknown>)
|
|
124
|
+
: await socketCall<string>(connection, command.call, command.params)
|
|
125
|
+
process.stdout.write(`${typeof result === 'string' ? result : JSON.stringify(result)}\n`)
|
|
126
|
+
return 0
|
|
127
|
+
} catch (error) {
|
|
128
|
+
process.stderr.write(`${errorText(error)}\n`)
|
|
129
|
+
return 1
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Runs `agentx-mcp <argv>`; returns the exit code. */
|
|
134
|
+
export async function runAppsCli(argv: string[]): Promise<number> {
|
|
135
|
+
const [command, name, args] = argv
|
|
136
|
+
if (command !== 'list' && !(command === 'call' && name)) {
|
|
137
|
+
process.stdout.write(APPS_USAGE)
|
|
138
|
+
return argv.length ? 2 : 0
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const connection = connectionFromEnv()
|
|
142
|
+
const result = command === 'list' ? await socketCall(connection, 'mcp.list', {}, 90_000) : await socketCall(connection, 'mcp.call', { name, args: JSON.parse(args ?? '{}') as unknown }, 1_230_000)
|
|
143
|
+
process.stdout.write(`${JSON.stringify(result)}\n`)
|
|
144
|
+
return 0
|
|
145
|
+
} catch (error) {
|
|
146
|
+
process.stderr.write(`${errorText(error)}\n`)
|
|
147
|
+
return 1
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// How a process outside the harness (the `pi` command's extension, the `agentx-wiki` and
|
|
2
|
+
// `agentx-mcp` commands) reaches it: one newline-delimited JSON request and reply per
|
|
3
|
+
// connection to the session socket, authorized by the token the harness put in its environment.
|
|
4
|
+
import { createConnection } from 'node:net'
|
|
5
|
+
|
|
6
|
+
export const SOCKET_ENV = 'XINGULAR_HARNESS_SOCKET'
|
|
7
|
+
export const TOKEN_ENV = 'XINGULAR_HARNESS_TOKEN'
|
|
8
|
+
|
|
9
|
+
export class SocketError extends Error {
|
|
10
|
+
constructor(
|
|
11
|
+
readonly code: string,
|
|
12
|
+
message: string
|
|
13
|
+
) {
|
|
14
|
+
super(message)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SocketConnection {
|
|
19
|
+
socket: string
|
|
20
|
+
token: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function connectionFromEnv(env: NodeJS.ProcessEnv = process.env): SocketConnection {
|
|
24
|
+
const socket = env[SOCKET_ENV]
|
|
25
|
+
const token = env[TOKEN_ENV]
|
|
26
|
+
if (!socket || !token) throw new SocketError('not_configured', `This command only works inside a terminal session the harness started (${SOCKET_ENV} and ${TOKEN_ENV} are not set).`)
|
|
27
|
+
return { socket, token }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function request<T>(connection: SocketConnection, body: Record<string, unknown>, what: string, timeoutMs: number): Promise<T> {
|
|
31
|
+
return new Promise<T>((resolve, reject) => {
|
|
32
|
+
const stream = createConnection(connection.socket)
|
|
33
|
+
let buffer = ''
|
|
34
|
+
let settled = false
|
|
35
|
+
const finish = (action: () => void) => {
|
|
36
|
+
if (settled) return
|
|
37
|
+
settled = true
|
|
38
|
+
clearTimeout(timer)
|
|
39
|
+
stream.end()
|
|
40
|
+
action()
|
|
41
|
+
}
|
|
42
|
+
const timer = setTimeout(() => {
|
|
43
|
+
stream.destroy()
|
|
44
|
+
finish(() => reject(new SocketError('timeout', `The harness did not answer ${what}.`)))
|
|
45
|
+
}, timeoutMs)
|
|
46
|
+
stream.on('connect', () => stream.write(`${JSON.stringify({ token: connection.token, ...body })}\n`))
|
|
47
|
+
stream.on('data', (chunk) => {
|
|
48
|
+
buffer += chunk.toString('utf8')
|
|
49
|
+
const newline = buffer.indexOf('\n')
|
|
50
|
+
if (newline < 0) return
|
|
51
|
+
try {
|
|
52
|
+
const reply = JSON.parse(buffer.slice(0, newline)) as { result?: T; error?: { code?: string; message?: string } | string }
|
|
53
|
+
if (reply.error) {
|
|
54
|
+
const error = typeof reply.error === 'string' ? { message: reply.error } : reply.error
|
|
55
|
+
finish(() => reject(new SocketError(error.code ?? 'error', error.message ?? `${what} failed.`)))
|
|
56
|
+
} else finish(() => resolve(reply.result as T))
|
|
57
|
+
} catch {
|
|
58
|
+
finish(() => reject(new SocketError('invalid_reply', 'The harness sent an unreadable reply.')))
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
stream.on('error', (error) => finish(() => reject(new SocketError('unavailable', `The harness is not reachable: ${error.message}.`))))
|
|
62
|
+
stream.on('close', () => finish(() => reject(new SocketError('unavailable', 'The harness closed the connection.'))))
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** A memory, Wiki, recall, or connected-app call made on behalf of the session that owns the token. */
|
|
67
|
+
export function socketCall<T = unknown>(connection: SocketConnection, call: string, params: Record<string, unknown> = {}, timeoutMs = 120_000, control?: string): Promise<T> {
|
|
68
|
+
return request<T>(connection, { call, params, ...(control ? { control } : {}) }, call, timeoutMs)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Telemetry and decisions from the native `pi` process. Approvals may wait on a person, so the default wait is long. */
|
|
72
|
+
export function terminalCall<T = Record<string, unknown>>(event: string, data: Record<string, unknown> = {}, timeoutMs = 600_000): Promise<T> {
|
|
73
|
+
return request<T>(connectionFromEnv(), { event, data }, event, timeoutMs)
|
|
74
|
+
}
|