yukari-dsh-plugin 0.0.0-stage → 0.7.2

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.
Files changed (105) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -2
  3. package/client.js +8340 -0
  4. package/cordis.patch.yml +95 -0
  5. package/host/browser/index.js +397 -0
  6. package/host/browser/lib/browser.js +885 -0
  7. package/host/browser/lib/cdp.js +302 -0
  8. package/host/browser/lib/libraries.js +309 -0
  9. package/host/browser/lib/page-scripts.js +243 -0
  10. package/host/browser/lib/runtime.js +351 -0
  11. package/host/browser/lib/url-guard.js +122 -0
  12. package/host/browser/locale/en.json +6 -0
  13. package/host/browser/locale/zh.json +6 -0
  14. package/host/chats/index.js +254 -0
  15. package/host/chats/lib/admin.js +893 -0
  16. package/host/chats/lib/cleanup.js +272 -0
  17. package/host/chats/lib/paths.js +108 -0
  18. package/host/chats/lib/preview.js +74 -0
  19. package/host/chats/locale/en.json +6 -0
  20. package/host/chats/locale/zh.json +6 -0
  21. package/host/files/index.js +403 -0
  22. package/host/files/locale/en.json +6 -0
  23. package/host/files/locale/zh.json +6 -0
  24. package/host/github/index.js +121 -0
  25. package/host/github/lib/api.js +310 -0
  26. package/host/github/lib/atomic.js +42 -0
  27. package/host/github/lib/controller.js +450 -0
  28. package/host/github/lib/git.js +389 -0
  29. package/host/github/lib/redact.js +22 -0
  30. package/host/github/lib/signing.js +124 -0
  31. package/host/github/lib/store.js +181 -0
  32. package/host/github/locale/en.json +6 -0
  33. package/host/github/locale/zh.json +6 -0
  34. package/host/presets/index.js +450 -0
  35. package/host/presets/lib/definition.js +267 -0
  36. package/host/presets/lib/sources.js +278 -0
  37. package/host/presets/lib/store.js +136 -0
  38. package/host/presets/locale/en.json +6 -0
  39. package/host/presets/locale/zh.json +6 -0
  40. package/host/proxy/index.js +236 -0
  41. package/host/proxy/lib/controller.js +1151 -0
  42. package/host/proxy/lib/egress.js +120 -0
  43. package/host/proxy/lib/local-proxy.js +422 -0
  44. package/host/proxy/lib/shadowsocks.js +335 -0
  45. package/host/proxy/lib/ss-uri.js +501 -0
  46. package/host/proxy/lib/subscriptions.js +140 -0
  47. package/host/proxy/locale/en.json +6 -0
  48. package/host/proxy/locale/zh.json +6 -0
  49. package/host/purge/index.js +538 -0
  50. package/host/purge/lib/preset.js +140 -0
  51. package/host/purge/lib/prompts.js +95 -0
  52. package/host/purge/lib/retry-gate.js +97 -0
  53. package/host/purge/lib/rewriter.js +322 -0
  54. package/host/purge/lib/settings.js +54 -0
  55. package/host/purge/lib/stream-retry.js +255 -0
  56. package/host/purge/locale/en.json +6 -0
  57. package/host/purge/locale/zh.json +6 -0
  58. package/host/purge/presets/unrestricted/agent.cordis.yml +273 -0
  59. package/host/purge/presets/unrestricted/preset.json +75 -0
  60. package/host/purge/presets/unrestricted/preset.yml +3 -0
  61. package/host/rewind/index.js +127 -0
  62. package/host/rewind/lib/controller.js +449 -0
  63. package/host/rewind/lib/marker.js +53 -0
  64. package/host/rewind/lib/safe-path.js +47 -0
  65. package/host/rewind/lib/store.js +585 -0
  66. package/host/rewind/lib/surface.js +108 -0
  67. package/host/rewind/locale/en.json +6 -0
  68. package/host/rewind/locale/zh.json +6 -0
  69. package/host/rules/index.js +624 -0
  70. package/host/rules/lib/paths.js +87 -0
  71. package/host/rules/lib/render.js +92 -0
  72. package/host/rules/lib/scope.js +146 -0
  73. package/host/rules/lib/settings.js +55 -0
  74. package/host/rules/lib/store.js +222 -0
  75. package/host/rules/locale/en.json +6 -0
  76. package/host/rules/locale/zh.json +6 -0
  77. package/host/runtime/index.js +577 -0
  78. package/host/runtime/lib/npm.js +293 -0
  79. package/host/runtime/lib/restart.js +105 -0
  80. package/host/runtime/lib/update-guard.js +70 -0
  81. package/host/runtime/lib/versions.js +188 -0
  82. package/host/runtime/locale/en.json +6 -0
  83. package/host/runtime/locale/zh.json +6 -0
  84. package/host/shared/busy.js +40 -0
  85. package/host/shared/home.js +27 -0
  86. package/host/shared/http.js +159 -0
  87. package/host/shared/migrate.js +60 -0
  88. package/host/shared/mounted.js +54 -0
  89. package/host/shared/zip-writer.js +319 -0
  90. package/host/shared/zip.js +180 -0
  91. package/host/sites/index.js +258 -0
  92. package/host/sites/lib/sites.js +630 -0
  93. package/host/sites/locale/en.json +6 -0
  94. package/host/sites/locale/zh.json +6 -0
  95. package/host/skills/index.js +176 -0
  96. package/host/skills/lib/frontmatter.js +149 -0
  97. package/host/skills/lib/library.js +408 -0
  98. package/host/skills/lib/upload.js +115 -0
  99. package/host/skills/locale/en.json +6 -0
  100. package/host/skills/locale/zh.json +6 -0
  101. package/icon.png +0 -0
  102. package/index.js +702 -0
  103. package/locale/en.json +5 -0
  104. package/locale/zh.json +5 -0
  105. package/package.json +113 -4
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Navigation address policy for the controlled browser.
3
+ *
4
+ * A tool argument, a page link, or a redirect can point Chromium at loopback
5
+ * services, private networks, and cloud metadata endpoints. The host has no
6
+ * network boundary of its own, so the browser refuses those addresses before
7
+ * navigation and checks the landing address again after redirects.
8
+ * @module @local/browser-control/url-guard
9
+ */
10
+
11
+ const IPV4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/u
12
+
13
+ /**
14
+ * Why a literal IPv4 address is not publicly routable.
15
+ * @param address - dotted quad.
16
+ * @returns a Chinese reason, or null for a public address.
17
+ */
18
+ export function privateIpv4Reason(address) {
19
+ const match = IPV4.exec(address)
20
+ if (!match) return null
21
+ const octets = match.slice(1).map(Number)
22
+ if (octets.some((octet) => octet > 255)) return null
23
+ const [first, second] = octets
24
+ if (first === 0) return '未指定地址'
25
+ if (first === 127) return '本机回环地址'
26
+ if (first === 10) return '私有网段地址'
27
+ if (first === 172 && second >= 16 && second <= 31) return '私有网段地址'
28
+ if (first === 192 && second === 168) return '私有网段地址'
29
+ if (first === 169 && second === 254) return '链路本地地址'
30
+ return null
31
+ }
32
+
33
+ /** Expand an IPv6 literal into eight 16-bit groups, or null. */
34
+ function expandIpv6(host) {
35
+ if (!host.includes(':') || host.split('::').length > 2) return null
36
+ const [left, right] = host.split('::')
37
+ const expand = (part) => {
38
+ if (part === '') return []
39
+ const groups = []
40
+ for (const piece of part.split(':')) {
41
+ if (piece.includes('.')) {
42
+ const match = IPV4.exec(piece)
43
+ if (!match) return null
44
+ const octets = match.slice(1).map(Number)
45
+ if (octets.some((octet) => octet > 255)) return null
46
+ groups.push((octets[0] << 8) | octets[1], (octets[2] << 8) | octets[3])
47
+ } else if (/^[0-9a-f]{1,4}$/iu.test(piece)) {
48
+ groups.push(Number.parseInt(piece, 16))
49
+ } else {
50
+ return null
51
+ }
52
+ }
53
+ return groups
54
+ }
55
+ const head = expand(left)
56
+ const tail = expand(right ?? '')
57
+ if (head === null || tail === null) return null
58
+ if (right === undefined) return head.length === 8 ? head : null
59
+ const missing = 8 - head.length - tail.length
60
+ if (missing < 0) return null
61
+ return [...head, ...Array(missing).fill(0), ...tail]
62
+ }
63
+
64
+ /**
65
+ * Why a literal IPv6 address is not publicly routable.
66
+ * @param address - IPv6 literal without brackets.
67
+ * @returns a Chinese reason, or null for a public address.
68
+ */
69
+ export function privateIpv6Reason(address) {
70
+ const groups = expandIpv6(address)
71
+ if (groups === null) return null
72
+ if (groups.every((group) => group === 0)) return '未指定地址'
73
+ if (groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1) return '本机回环地址'
74
+ // IPv4-mapped addresses are the usual way a private IPv4 slips past a
75
+ // family check, so unwrap them and apply the IPv4 ranges.
76
+ if (groups.slice(0, 5).every((group) => group === 0) && groups[5] === 0xffff) {
77
+ const mapped = `${groups[6] >> 8}.${groups[6] & 0xff}.${groups[7] >> 8}.${groups[7] & 0xff}`
78
+ return privateIpv4Reason(mapped)
79
+ }
80
+ if ((groups[0] & 0xfe00) === 0xfc00) return '私有网段地址'
81
+ if ((groups[0] & 0xffc0) === 0xfe80) return '链路本地地址'
82
+ return null
83
+ }
84
+
85
+ /**
86
+ * Why a host name points inside the machine or the local network.
87
+ * @param hostname - WHATWG URL hostname, brackets included for IPv6.
88
+ * @returns a Chinese reason, or null when the host is public.
89
+ */
90
+ export function privateHostReason(hostname) {
91
+ const host = String(hostname ?? '').replace(/^\[|\]$/gu, '').replace(/\.$/u, '').toLowerCase()
92
+ if (host === '') return null
93
+ if (host === 'localhost' || host.endsWith('.localhost')) return '本机回环地址'
94
+ if (host.includes(':')) return privateIpv6Reason(host)
95
+ return privateIpv4Reason(host)
96
+ }
97
+
98
+ /**
99
+ * Decide whether a navigation may proceed.
100
+ * @param url - absolute URL, string or URL.
101
+ * @param options - `allowLocalUrls` opts into local content and private hosts.
102
+ * @returns a Chinese refusal reason, or null when the URL is allowed.
103
+ */
104
+ export function blockedNavigationReason(url, options = {}) {
105
+ let parsed
106
+ try {
107
+ parsed = url instanceof URL ? url : new URL(String(url))
108
+ } catch {
109
+ return `无法解析导航地址 ${String(url)}`
110
+ }
111
+ if (!/^(https?|file|data|about):$/u.test(parsed.protocol)) {
112
+ return `不支持的 URL 协议 ${parsed.protocol}`
113
+ }
114
+ if (parsed.protocol === 'about:') return null
115
+ if (options.allowLocalUrls === true) return null
116
+ if (parsed.protocol === 'file:' || parsed.protocol === 'data:') {
117
+ return `${parsed.protocol} URL 默认被禁用;确需本地内容时将 browser allowLocalUrls 设为 true`
118
+ }
119
+ const reason = privateHostReason(parsed.hostname)
120
+ if (reason === null) return null
121
+ return `默认禁止访问${reason}(${parsed.host});确需访问时将 browser allowLocalUrls 设为 true`
122
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "Browser",
4
+ "description": "Chromium browser tools for the agent"
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "meta": {
3
+ "title": "无头浏览器",
4
+ "description": "给智能体用的 Chromium 浏览器工具"
5
+ }
6
+ }
@@ -0,0 +1,254 @@
1
+ /**
2
+ * Chat manager bundle: one Host half that inventories stored chats and can
3
+ * delete them, plus one Client half that renders it as a sidebar panel.
4
+ *
5
+ * The Host exposes the same three operations twice — as a small JSON API the
6
+ * panel calls, and as agent tools with matching meaning — so the panel and the
7
+ * model never disagree about what a chat list or a delete is.
8
+ *
9
+ * @module @local/chat-manager
10
+ */
11
+ import { markMounted } from '../shared/mounted.js'
12
+ import { API_PATH, createAdmin, messageOf } from './lib/admin.js'
13
+ import { guardRoute, readJsonBody, sendJson } from '../shared/http.js'
14
+
15
+ export { API_PATH }
16
+
17
+ /** Rows one inventory tool answer carries. */
18
+ const TOOL_ROW_LIMIT = 200
19
+
20
+ /** Services this bundle needs. */
21
+ export const inject = ['sessionPersistence', 'sessionQuery', 'sessions', 'webServer', 'connection', 'tools']
22
+
23
+ /** Trim one chat row to the shape an agent tool answer needs. */
24
+ function toolRow(row) {
25
+ return {
26
+ id: row.id,
27
+ title: row.title ?? '(untitled)',
28
+ archived: row.archived === true,
29
+ live: row.live === true,
30
+ running: row.running === true,
31
+ updatedAt: new Date(row.updatedAt).toISOString(),
32
+ bytes: typeof row.bytes === 'number' ? row.bytes : 0,
33
+ ...(row.cwd === null ? {} : { cwd: row.cwd }),
34
+ }
35
+ }
36
+
37
+ /** Render one inventory answer as a compact table. */
38
+ function renderInventory(_args, value) {
39
+ const lines = [
40
+ `${value.total} chats · ${value.archived} archived · ${value.live} open · ${value.bytes} bytes stored`,
41
+ ]
42
+ for (const row of value.sessions) {
43
+ const flags = [row.archived ? 'archived' : 'active', row.live ? 'open' : 'closed']
44
+ lines.push(`- ${row.title} [${flags.join(', ')}] ${row.id} · ${row.bytes}B`)
45
+ }
46
+ return [{ type: 'text', text: lines.join('\n') }]
47
+ }
48
+
49
+ /**
50
+ * Register the Host half.
51
+ * @param ctx - plugin context.
52
+ * @param config - sessionsRoot and titleBudget.
53
+ */
54
+ export function apply(ctx, config = {}) {
55
+ markMounted(ctx, 'chats')
56
+ const admin = createAdmin({ ctx, config })
57
+
58
+ ctx.effect(
59
+ () =>
60
+ ctx.webServer.register({
61
+ kind: 'prefix',
62
+ path: API_PATH,
63
+ handler: guardRoute(ctx, async (req, res) => {
64
+ const url = new URL(String(req.url), 'http://localhost')
65
+ const action = url.pathname.slice(API_PATH.length).replace(/^\/+|\/+$/g, '')
66
+
67
+ if (action === '' || action === 'state') {
68
+ if (req.method !== 'GET') {
69
+ sendJson(res, 405, { ok: false, error: 'use GET' })
70
+ return
71
+ }
72
+ sendJson(res, 200, { ok: true, state: await admin.snapshot() })
73
+ return
74
+ }
75
+ if (action === 'preview') {
76
+ if (req.method !== 'GET') {
77
+ sendJson(res, 405, { ok: false, error: 'use GET' })
78
+ return
79
+ }
80
+ const limit = Number(url.searchParams.get('limit'))
81
+ sendJson(res, 200, {
82
+ ok: true,
83
+ preview: await admin.preview({ sessionId: url.searchParams.get('sessionId') ?? '', limit }),
84
+ })
85
+ return
86
+ }
87
+ if (req.method !== 'POST') {
88
+ sendJson(res, 405, { ok: false, error: 'use POST' })
89
+ return
90
+ }
91
+ const body = await readJsonBody(req)
92
+ if (action === 'delete') {
93
+ const result = await admin.remove({
94
+ ids: body.ids,
95
+ deleteFiles: body.deleteFiles === true,
96
+ deleteWorkspaceDir: body.deleteWorkspaceDir === true,
97
+ deleteWorkspace: body.deleteWorkspace === true,
98
+ workspaceId: body.workspaceId,
99
+ workspacePath: body.workspacePath,
100
+ })
101
+ sendJson(res, 200, { ok: true, ...result })
102
+ return
103
+ }
104
+ if (action === 'archive') {
105
+ const result = await admin.setArchived({
106
+ id: body.id,
107
+ archived: body.archived === true,
108
+ stopActivity: body.stopActivity === true,
109
+ })
110
+ sendJson(res, 200, { ok: true, ...result })
111
+ return
112
+ }
113
+ sendJson(res, 404, { ok: false, error: `unknown action "${action}"` })
114
+
115
+ }),
116
+ }),
117
+ `chat-manager: ${API_PATH}/*`,
118
+ )
119
+
120
+ ctx.effect(
121
+ () =>
122
+ ctx.tools.register({
123
+ name: 'chat_inventory',
124
+ description:
125
+ 'List the chats stored on this Host: id, title, whether each is archived, still open in this process, and how much it occupies on disk. Use it before deleting chats, and to answer "what chats do I have".',
126
+ parameters: {
127
+ type: 'object',
128
+ additionalProperties: false,
129
+ properties: {
130
+ filter: {
131
+ type: 'string',
132
+ enum: ['all', 'active', 'archived'],
133
+ description: 'Which chats to return; defaults to all.',
134
+ },
135
+ query: { type: 'string', description: 'Case-insensitive match on title, id, working directory, or workspace.' },
136
+ limit: { type: 'integer', description: `Maximum rows to return; defaults to 50, capped at ${TOOL_ROW_LIMIT}.` },
137
+ },
138
+ },
139
+ output: {
140
+ schema: {
141
+ type: 'object',
142
+ additionalProperties: false,
143
+ required: ['total', 'archived', 'live', 'bytes', 'sessions'],
144
+ properties: {
145
+ total: { type: 'integer', description: 'Chats matching the filter.' },
146
+ archived: { type: 'integer', description: 'Chats currently in the archive set.' },
147
+ live: { type: 'integer', description: 'Chats loaded in this Host process.' },
148
+ bytes: { type: 'integer', description: 'Bytes those chats occupy on disk.' },
149
+ sessions: {
150
+ type: 'array',
151
+ description: 'Matching chats, newest activity first.',
152
+ items: {
153
+ type: 'object',
154
+ additionalProperties: false,
155
+ required: ['id', 'title', 'archived', 'live', 'running', 'updatedAt', 'bytes'],
156
+ properties: {
157
+ id: { type: 'string', description: 'Session id; the key chat_delete takes.' },
158
+ title: { type: 'string', description: 'Latest logged title, or "(untitled)".' },
159
+ archived: { type: 'boolean', description: 'Whether the chat is archived.' },
160
+ live: { type: 'boolean', description: 'Whether the chat is loaded in this process.' },
161
+ running: { type: 'boolean', description: 'Whether the chat currently has a running turn.' },
162
+ updatedAt: { type: 'string', description: 'ISO instant of the last artifact change.' },
163
+ bytes: { type: 'integer', description: 'Artifact size in bytes.' },
164
+ cwd: { type: 'string', description: 'Working directory the chat was created in, when known.' },
165
+ },
166
+ },
167
+ },
168
+ },
169
+ },
170
+ render: renderInventory,
171
+ },
172
+ async execute(args = {}) {
173
+ const state = await admin.snapshot()
174
+ const filter = args.filter === 'active' || args.filter === 'archived' ? args.filter : 'all'
175
+ const needle = typeof args.query === 'string' ? args.query.trim().toLowerCase() : ''
176
+ const requested = Number.isFinite(Number(args.limit)) ? Math.floor(Number(args.limit)) : 50
177
+ const limit = Math.min(Math.max(requested, 1), TOOL_ROW_LIMIT)
178
+ const rows = state.sessions.filter((row) => {
179
+ if (filter === 'active' && row.archived) return false
180
+ if (filter === 'archived' && !row.archived) return false
181
+ if (needle === '') return true
182
+ const haystack = [row.title ?? '', row.id, row.cwd ?? '', row.workspaceTitle ?? ''].join('\n').toLowerCase()
183
+ return haystack.includes(needle)
184
+ })
185
+ const visible = rows.slice(0, limit).map(toolRow)
186
+ return {
187
+ total: rows.length,
188
+ archived: rows.filter((row) => row.archived).length,
189
+ live: rows.filter((row) => row.live).length,
190
+ bytes: rows.reduce((sum, row) => sum + (typeof row.bytes === 'number' ? row.bytes : 0), 0),
191
+ sessions: visible,
192
+ }
193
+ },
194
+ }),
195
+ 'chat-manager: chat_inventory tool',
196
+ )
197
+
198
+ ctx.effect(
199
+ () =>
200
+ ctx.tools.register({
201
+ name: 'chat_delete',
202
+ description:
203
+ 'Permanently delete stored chats by Session id: removes the chat artifact from the Host, then drops the id from workspace accounting, the archive set, and the pin set. Archived chats are deleted the same way. A chat that is merely loaded is deleted too; only a chat the Host still reports as working is refused, so stop it first.',
204
+ parameters: {
205
+ type: 'object',
206
+ additionalProperties: false,
207
+ required: ['ids'],
208
+ properties: {
209
+ ids: {
210
+ type: 'array',
211
+ items: { type: 'string' },
212
+ description: 'Session ids to delete, as returned by chat_inventory.',
213
+ },
214
+ },
215
+ },
216
+ output: {
217
+ schema: {
218
+ type: 'object',
219
+ additionalProperties: false,
220
+ required: ['deleted', 'ids', 'failed'],
221
+ properties: {
222
+ deleted: { type: 'integer', description: 'How many chats were removed.' },
223
+ ids: { type: 'array', items: { type: 'string' }, description: 'Ids that were removed.' },
224
+ failed: {
225
+ type: 'array',
226
+ description: 'Ids left in place, each with its reason.',
227
+ items: {
228
+ type: 'object',
229
+ additionalProperties: false,
230
+ required: ['id', 'reason'],
231
+ properties: {
232
+ id: { type: 'string', description: 'Session id that stayed.' },
233
+ reason: { type: 'string', description: 'running, unknown, no-artifact, unsafe-path, detach-failed, activity-error, io-error, or an errno message.' },
234
+ },
235
+ },
236
+ },
237
+ },
238
+ },
239
+ render: (_args, value) => {
240
+ const head = `deleted ${value.deleted} chat${value.deleted === 1 ? '' : 's'}`
241
+ const tail = value.failed.length === 0
242
+ ? ''
243
+ : `\nkept ${value.failed.length}: ${value.failed.map((item) => `${item.id} (${item.reason})`).join(', ')}`
244
+ return [{ type: 'text', text: head + tail }]
245
+ },
246
+ },
247
+ async execute(args = {}) {
248
+ const result = await admin.remove({ ids: args.ids })
249
+ return { deleted: result.deleted, ids: result.ids, failed: result.failed }
250
+ },
251
+ }),
252
+ 'chat-manager: chat_delete tool',
253
+ )
254
+ }