typeclaw 0.39.2 → 0.39.4
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/auth.schema.json +110 -0
- package/package.json +2 -2
- package/secrets.schema.json +110 -0
- package/src/agent/index.ts +39 -18
- package/src/agent/model-fallback.ts +13 -4
- package/src/agent/plugin-tools.ts +12 -12
- package/src/agent/provider-error.ts +36 -1
- package/src/agent/session-origin.ts +2 -0
- package/src/agent/subagents.ts +29 -2
- package/src/agent/throttle-circuit.ts +50 -0
- package/src/agent/todo/store.ts +21 -1
- package/src/agent/tools/channel-reply.ts +18 -5
- package/src/agent/tools/channel-send.ts +13 -2
- package/src/agent/turn-runner.ts +162 -0
- package/src/bundled-plugins/doc-render/render.ts +2 -2
- package/src/bundled-plugins/github-cli-auth/approve-idempotency.ts +40 -18
- package/src/bundled-plugins/memory/README.md +12 -12
- package/src/bundled-plugins/memory/append-tool.ts +58 -6
- package/src/bundled-plugins/memory/dreaming.ts +1 -1
- package/src/bundled-plugins/memory/fragment-parser.ts +10 -3
- package/src/bundled-plugins/memory/load-memory.ts +28 -2
- package/src/bundled-plugins/memory/memory-logger.ts +14 -2
- package/src/bundled-plugins/memory/search-tool.ts +50 -7
- package/src/bundled-plugins/memory/stream-events.ts +23 -0
- package/src/bundled-plugins/memory/vector/hybrid.ts +9 -2
- package/src/bundled-plugins/security/index.ts +24 -1
- package/src/bundled-plugins/security/policies/cron-promotion.ts +94 -62
- package/src/bundled-plugins/security/policies/plugin-addition.ts +2 -4
- package/src/bundled-plugins/security/policies/role-promotion.ts +2 -4
- package/src/channels/adapters/discord-author-resolver.ts +52 -0
- package/src/channels/adapters/discord-bot.ts +1 -1
- package/src/channels/adapters/discord-channel-resolver.ts +44 -0
- package/src/channels/adapters/discord-classify.ts +121 -0
- package/src/channels/adapters/discord-reactions.ts +138 -0
- package/src/channels/adapters/discord.ts +416 -0
- package/src/channels/adapters/github/outbound.ts +23 -4
- package/src/channels/adapters/kakaotalk.ts +8 -1
- package/src/channels/adapters/line.ts +1 -1
- package/src/channels/adapters/slack-author-resolver.ts +54 -0
- package/src/channels/adapters/slack-bot.ts +3 -1
- package/src/channels/adapters/slack-channel-resolver.ts +49 -0
- package/src/channels/adapters/slack-classify.ts +83 -0
- package/src/channels/adapters/slack-reactions.ts +126 -0
- package/src/channels/adapters/slack.ts +416 -0
- package/src/channels/adapters/telegram-bot.ts +2 -1
- package/src/channels/adapters/webex-bot.ts +31 -27
- package/src/channels/adapters/webex-prefetch-limiter.ts +120 -0
- package/src/channels/adapters/webex.ts +81 -31
- package/src/channels/github-review-turn-ledger.ts +32 -0
- package/src/channels/github-verdict-activity.ts +60 -0
- package/src/channels/index.ts +6 -0
- package/src/channels/manager.ts +168 -37
- package/src/channels/pr-verdict-activity-bridge.ts +43 -0
- package/src/channels/router.ts +295 -13
- package/src/channels/schema.ts +4 -0
- package/src/channels/types.ts +25 -2
- package/src/cli/builtins.ts +6 -31
- package/src/cli/channel.ts +170 -18
- package/src/cli/command-meta.ts +93 -0
- package/src/cli/help.ts +24 -0
- package/src/cli/index.ts +45 -10
- package/src/cli/init.ts +2 -0
- package/src/cli/update-check.ts +14 -0
- package/src/cli/update-notify.ts +91 -0
- package/src/cli/update-suppression.ts +19 -0
- package/src/config/channels-mutation.ts +3 -1
- package/src/config/config.ts +78 -1
- package/src/config/index.ts +1 -0
- package/src/container/start.ts +52 -8
- package/src/doctor/channel-checks.ts +57 -3
- package/src/hostd/daemon.ts +77 -31
- package/src/hostd/index.ts +11 -1
- package/src/hostd/paths.ts +4 -0
- package/src/hostd/protocol.ts +15 -2
- package/src/hostd/spawn.ts +53 -19
- package/src/init/cli-version.ts +1 -1
- package/src/init/discord-auth.ts +50 -0
- package/src/init/index.ts +94 -4
- package/src/init/reconcile-plugin-deps.ts +30 -5
- package/src/init/run-owner-claim.ts +2 -0
- package/src/init/slack-auth.ts +51 -0
- package/src/mcp/manager.ts +94 -7
- package/src/mcp/tools.ts +7 -4
- package/src/permissions/permissions.ts +12 -0
- package/src/permissions/resolve.ts +2 -0
- package/src/role-claim/match-rule.ts +2 -0
- package/src/run/codex-fetch-observer.ts +11 -6
- package/src/run/index.ts +74 -29
- package/src/secrets/discord-store.ts +96 -0
- package/src/secrets/oauth-xai.ts +2 -2
- package/src/secrets/schema.ts +34 -0
- package/src/secrets/slack-store.ts +96 -0
- package/src/server/command-runner.ts +21 -0
- package/src/server/index.ts +27 -2
- package/src/skills/typeclaw-channels/SKILL.md +1 -1
- package/src/skills/typeclaw-config/SKILL.md +1 -0
- package/src/skills/typeclaw-cron/SKILL.md +20 -4
- package/src/skills/typeclaw-permissions/SKILL.md +18 -6
- package/src/tui/format.ts +0 -19
- package/src/tui/index.ts +2 -8
- package/src/update/check.ts +192 -0
- package/typeclaw.schema.json +332 -0
package/src/hostd/spawn.ts
CHANGED
|
@@ -111,12 +111,48 @@ async function ensureDaemonWithRetry(opts: EnsureDaemonOptions, retriesLeft: num
|
|
|
111
111
|
if (httpPort === null) return { ok: false, reason: 'daemon did not report an HTTP control port' }
|
|
112
112
|
return { ok: true, pid: await readPidQuiet(), spawned: false, httpPort }
|
|
113
113
|
}
|
|
114
|
+
// A prior spawn attempt can leave a live-but-not-yet-listening child (its
|
|
115
|
+
// readiness poll timed out without killing it — see spawnDaemonDetached).
|
|
116
|
+
// Spawning a second daemon would race two processes for the same socket, so
|
|
117
|
+
// adopt the existing child and poll IT for readiness instead of re-spawning.
|
|
118
|
+
const existingPid = await livePidfileChild()
|
|
119
|
+
if (existingPid !== null) {
|
|
120
|
+
const ready = await pollForReadiness(opts.spawnTimeoutMs ?? DEFAULT_SPAWN_TIMEOUT_MS)
|
|
121
|
+
if (ready === null) return { ok: false, reason: 'daemon spawned but did not become reachable yet' }
|
|
122
|
+
return { ok: true, pid: existingPid, spawned: false, httpPort: ready }
|
|
123
|
+
}
|
|
114
124
|
return await spawnDaemonDetached(opts)
|
|
115
125
|
} finally {
|
|
116
126
|
await releaseLock(lock.token)
|
|
117
127
|
}
|
|
118
128
|
}
|
|
119
129
|
|
|
130
|
+
// Reads the pidfile and returns its pid only if that process is still alive
|
|
131
|
+
// (signal 0 = existence check, no signal delivered). Returns null when the
|
|
132
|
+
// pidfile is absent/garbage or the process has exited — i.e. nothing to adopt.
|
|
133
|
+
async function livePidfileChild(): Promise<number | null> {
|
|
134
|
+
const pid = await readPidQuiet()
|
|
135
|
+
if (pid <= 0) return null
|
|
136
|
+
try {
|
|
137
|
+
process.kill(pid, 0)
|
|
138
|
+
return pid
|
|
139
|
+
} catch {
|
|
140
|
+
return null
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function pollForReadiness(timeoutMs: number): Promise<number | null> {
|
|
145
|
+
const deadline = Date.now() + timeoutMs
|
|
146
|
+
while (Date.now() < deadline) {
|
|
147
|
+
if (await isDaemonReachable()) {
|
|
148
|
+
const httpPort = await readHttpPort()
|
|
149
|
+
if (httpPort !== null) return httpPort
|
|
150
|
+
}
|
|
151
|
+
await sleep(POLL_INTERVAL_MS)
|
|
152
|
+
}
|
|
153
|
+
return null
|
|
154
|
+
}
|
|
155
|
+
|
|
120
156
|
async function spawnDaemonDetached(opts: EnsureDaemonOptions): Promise<SpawnAttemptResult> {
|
|
121
157
|
// Bun.spawn() with `stdout: <number>` consumes the file descriptor by
|
|
122
158
|
// dup()-ing it into the child; the parent's handle remains valid until we
|
|
@@ -154,26 +190,24 @@ async function spawnDaemonDetached(opts: EnsureDaemonOptions): Promise<SpawnAtte
|
|
|
154
190
|
return { ok: false, reason: `failed to write daemon pidfile: ${stringify(error)}` }
|
|
155
191
|
}
|
|
156
192
|
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
if (await isDaemonReachable()) {
|
|
160
|
-
const httpPort = await readHttpPort()
|
|
161
|
-
if (httpPort !== null) return { ok: true, pid: proc.pid, spawned: true, httpPort }
|
|
162
|
-
}
|
|
163
|
-
await sleep(POLL_INTERVAL_MS)
|
|
164
|
-
}
|
|
193
|
+
const httpPort = await pollForReadiness(opts.spawnTimeoutMs ?? DEFAULT_SPAWN_TIMEOUT_MS)
|
|
194
|
+
if (httpPort !== null) return { ok: true, pid: proc.pid, spawned: true, httpPort }
|
|
165
195
|
|
|
166
|
-
//
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
196
|
+
// Timed out waiting for readiness. A still-running child is "slow-booting",
|
|
197
|
+
// not "wedged" — killing it would throw away a daemon that's about to come up
|
|
198
|
+
// and force every caller into a respawn loop. Only reap a child that already
|
|
199
|
+
// EXITED (proc.exitCode !== null): that's a genuine failure with a dangling
|
|
200
|
+
// pidfile to clean. A live-but-not-ready child is left running so the caller
|
|
201
|
+
// can re-probe it (see registerWithDaemon's retry) — the next ensureDaemon()
|
|
202
|
+
// fast-paths through isDaemonReachable() once its socket binds.
|
|
203
|
+
if (proc.exitCode !== null) {
|
|
204
|
+
try {
|
|
205
|
+
const raw = await readFile(pidfilePath(), 'utf8').catch(() => '')
|
|
206
|
+
if (raw.trim() === String(proc.pid)) await unlink(pidfilePath())
|
|
207
|
+
} catch {}
|
|
208
|
+
return { ok: false, reason: 'daemon exited before becoming reachable' }
|
|
209
|
+
}
|
|
210
|
+
return { ok: false, reason: 'daemon spawned but did not become reachable yet' }
|
|
177
211
|
}
|
|
178
212
|
|
|
179
213
|
type LockToken = { path: string }
|
package/src/init/cli-version.ts
CHANGED
|
@@ -23,7 +23,7 @@ export const CLI_VERSION = cliPkg.version
|
|
|
23
23
|
|
|
24
24
|
const NODE_MODULES_SEGMENT = `${join('/', 'node_modules', '/')}`
|
|
25
25
|
|
|
26
|
-
function isInstalledCli(): boolean {
|
|
26
|
+
export function isInstalledCli(): boolean {
|
|
27
27
|
return CLI_PACKAGE_JSON_PATH.includes(NODE_MODULES_SEGMENT)
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import { loginWithRemoteAuth as upstreamLoginWithRemoteAuth, type RemoteAuthSession } from 'agent-messenger/discord'
|
|
5
|
+
|
|
6
|
+
import { SecretsDiscordCredentialStore } from '@/secrets/discord-store'
|
|
7
|
+
import type { DiscordAccountRecord } from '@/secrets/schema'
|
|
8
|
+
|
|
9
|
+
export type DiscordBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
10
|
+
|
|
11
|
+
export type LoginWithRemoteAuthFn = typeof upstreamLoginWithRemoteAuth
|
|
12
|
+
|
|
13
|
+
export type DiscordLoginInput = {
|
|
14
|
+
agentDir: string
|
|
15
|
+
onQrUrl?: (url: string) => void | Promise<void>
|
|
16
|
+
loginWithRemoteAuth?: LoginWithRemoteAuthFn
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function discordSecretsPath(agentDir: string): string {
|
|
20
|
+
return join(agentDir, 'secrets.json')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function runDiscordBootstrap(input: DiscordLoginInput): Promise<DiscordBootstrapStatus> {
|
|
24
|
+
try {
|
|
25
|
+
const loginWithRemoteAuth = input.loginWithRemoteAuth ?? upstreamLoginWithRemoteAuth
|
|
26
|
+
const store = new SecretsDiscordCredentialStore({ mode: 'host', secretsPath: discordSecretsPath(input.agentDir) })
|
|
27
|
+
const result = await loginWithRemoteAuth({ onQrUrl: input.onQrUrl })
|
|
28
|
+
const now = new Date().toISOString()
|
|
29
|
+
const accountId = discordAccountId(result)
|
|
30
|
+
const account: DiscordAccountRecord = {
|
|
31
|
+
account_id: accountId,
|
|
32
|
+
token: result.token,
|
|
33
|
+
...(result.user?.username !== undefined ? { username: result.user.username } : {}),
|
|
34
|
+
created_at: now,
|
|
35
|
+
updated_at: now,
|
|
36
|
+
}
|
|
37
|
+
await store.setAccount(account)
|
|
38
|
+
await store.setCurrentAccount(accountId)
|
|
39
|
+
return { ok: true }
|
|
40
|
+
} catch (err) {
|
|
41
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function discordAccountId(result: RemoteAuthSession): string {
|
|
46
|
+
if (result.user?.id !== undefined && result.user.id !== '') return result.user.id
|
|
47
|
+
return createHash('sha256').update(result.token).digest('hex').slice(0, 16)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type { RemoteAuthSession }
|
package/src/init/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { existsSync, readdirSync } from 'node:fs'
|
|
2
2
|
import { mkdir, readFile, writeFile } from 'node:fs/promises'
|
|
3
|
-
import { basename, join
|
|
3
|
+
import { basename, join } from 'node:path'
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
config,
|
|
@@ -84,6 +84,8 @@ export type InitStep =
|
|
|
84
84
|
| 'preflight'
|
|
85
85
|
| 'oauth-login'
|
|
86
86
|
| 'scaffold'
|
|
87
|
+
| 'slack-auth'
|
|
88
|
+
| 'discord-auth'
|
|
87
89
|
| 'kakaotalk-auth'
|
|
88
90
|
| 'webex-auth'
|
|
89
91
|
| 'github-webhooks'
|
|
@@ -94,6 +96,8 @@ export type InitStep =
|
|
|
94
96
|
|
|
95
97
|
export type KakaotalkAuthResult = { ok: true } | { ok: false; reason: string }
|
|
96
98
|
export type WebexAuthResult = { ok: true } | { ok: false; reason: string }
|
|
99
|
+
export type SlackAuthResult = { ok: true } | { ok: false; reason: string }
|
|
100
|
+
export type DiscordAuthResult = { ok: true } | { ok: false; reason: string }
|
|
97
101
|
|
|
98
102
|
// Structured credential block for the GitHub channel adapter. Mirrors the
|
|
99
103
|
// shape `runAddChannel({ channel: 'github', ... })` consumes so the wizard
|
|
@@ -131,6 +135,8 @@ export type InitStepEvent =
|
|
|
131
135
|
| { step: 'kakaotalk-auth'; phase: 'done'; result: KakaotalkAuthResult }
|
|
132
136
|
| { step: 'webex-auth'; phase: 'start' }
|
|
133
137
|
| { step: 'webex-auth'; phase: 'done'; result: WebexAuthResult }
|
|
138
|
+
| { step: 'discord-auth'; phase: 'start' }
|
|
139
|
+
| { step: 'discord-auth'; phase: 'done'; result: DiscordAuthResult }
|
|
134
140
|
| { step: 'github-webhooks'; phase: 'start' }
|
|
135
141
|
| { step: 'github-webhooks'; phase: 'done'; result: EagerGithubWebhookInstallResult }
|
|
136
142
|
| { step: 'install'; phase: 'start' }
|
|
@@ -212,6 +218,7 @@ export type InitOptions = {
|
|
|
212
218
|
// the existing slot in `secrets.json#channels` untouched. Defaults below
|
|
213
219
|
// mirror the legacy derivation (`<token> !== undefined && !== ''`).
|
|
214
220
|
withDiscord?: boolean
|
|
221
|
+
withDiscordUser?: boolean
|
|
215
222
|
withSlack?: boolean
|
|
216
223
|
withTelegram?: boolean
|
|
217
224
|
withWebex?: boolean
|
|
@@ -220,6 +227,7 @@ export type InitOptions = {
|
|
|
220
227
|
withGithub?: boolean
|
|
221
228
|
runKakaotalkAuth?: KakaotalkAuthRunner
|
|
222
229
|
runWebexAuth?: WebexAuthRunner
|
|
230
|
+
runDiscordAuth?: DiscordAuthRunner
|
|
223
231
|
// Structured GitHub credentials collected by the wizard. When omitted and
|
|
224
232
|
// `withGithub` is true, the existing secrets.json#channels.github block is
|
|
225
233
|
// reused as-is (the wizard's "reuse existing credentials" path).
|
|
@@ -260,6 +268,7 @@ export async function runInit({
|
|
|
260
268
|
telegramBotToken,
|
|
261
269
|
webexBotToken,
|
|
262
270
|
withDiscord,
|
|
271
|
+
withDiscordUser = false,
|
|
263
272
|
withSlack,
|
|
264
273
|
withTelegram,
|
|
265
274
|
withWebex,
|
|
@@ -268,6 +277,7 @@ export async function runInit({
|
|
|
268
277
|
withGithub = false,
|
|
269
278
|
runKakaotalkAuth,
|
|
270
279
|
runWebexAuth,
|
|
280
|
+
runDiscordAuth,
|
|
271
281
|
githubCredentials,
|
|
272
282
|
githubFetchImpl,
|
|
273
283
|
onProgress,
|
|
@@ -343,6 +353,7 @@ export async function runInit({
|
|
|
343
353
|
...(visionModel !== undefined ? { visionModel } : {}),
|
|
344
354
|
...(visionModelMeta !== undefined ? { visionModelMeta } : {}),
|
|
345
355
|
withDiscord: wantsDiscord,
|
|
356
|
+
withDiscordUser,
|
|
346
357
|
withSlack: wantsSlack,
|
|
347
358
|
withTelegram: wantsTelegram,
|
|
348
359
|
withWebex: wantsWebex,
|
|
@@ -391,6 +402,16 @@ export async function runInit({
|
|
|
391
402
|
}
|
|
392
403
|
}
|
|
393
404
|
|
|
405
|
+
if (withDiscordUser) {
|
|
406
|
+
emit({ step: 'discord-auth', phase: 'start' })
|
|
407
|
+
const runner = runDiscordAuth ?? defaultDiscordAuthRunner
|
|
408
|
+
const result = await runner({ cwd })
|
|
409
|
+
emit({ step: 'discord-auth', phase: 'done', result })
|
|
410
|
+
if (!result.ok) {
|
|
411
|
+
throw new Error(`Discord authentication failed: ${result.reason}`)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
394
415
|
// Write the structured github channel block alongside scaffold's bot-token
|
|
395
416
|
// blocks. We do NOT delegate to runAddChannel because that's the `channel
|
|
396
417
|
// add` semantics — strict no-overwrite, throws when secrets.json#channels
|
|
@@ -437,6 +458,7 @@ export async function runInit({
|
|
|
437
458
|
|
|
438
459
|
const configuredChannels: ChannelKind[] = []
|
|
439
460
|
if (wantsDiscord) configuredChannels.push('discord-bot')
|
|
461
|
+
if (withDiscordUser) configuredChannels.push('discord')
|
|
440
462
|
if (wantsSlack) configuredChannels.push('slack-bot')
|
|
441
463
|
if (wantsTelegram) configuredChannels.push('telegram-bot')
|
|
442
464
|
if (wantsWebex) configuredChannels.push('webex-bot')
|
|
@@ -606,6 +628,7 @@ export type ScaffoldOptions = {
|
|
|
606
628
|
visionModel?: ModelRef | string
|
|
607
629
|
visionModelMeta?: CustomModelMeta
|
|
608
630
|
withDiscord?: boolean
|
|
631
|
+
withDiscordUser?: boolean
|
|
609
632
|
withSlack?: boolean
|
|
610
633
|
withTelegram?: boolean
|
|
611
634
|
withWebex?: boolean
|
|
@@ -643,6 +666,7 @@ export async function scaffold(root: string, options: ScaffoldOptions = {}): Pro
|
|
|
643
666
|
if (Object.keys(customModels).length > 0) config.customModels = customModels
|
|
644
667
|
const channels: Record<string, Record<string, never>> = {}
|
|
645
668
|
if (options.withDiscord) channels['discord-bot'] = {}
|
|
669
|
+
if (options.withDiscordUser) channels.discord = {}
|
|
646
670
|
if (options.withSlack) channels['slack-bot'] = {}
|
|
647
671
|
if (options.withTelegram) channels['telegram-bot'] = {}
|
|
648
672
|
if (options.withWebex) channels['webex-bot'] = {}
|
|
@@ -728,13 +752,21 @@ export async function writeDockerAssets(root: string): Promise<DockerAssetsResul
|
|
|
728
752
|
try {
|
|
729
753
|
const pkg = await readPackageJson(root)
|
|
730
754
|
const typeclawSpec = pkg.dependencies?.typeclaw ?? ''
|
|
731
|
-
|
|
755
|
+
// Both `file:` (POSIX dev) and `link:` (native-Windows dev, see
|
|
756
|
+
// resolveTypeclawSpec) are locally-linked checkouts that must inline the
|
|
757
|
+
// heavy stack on the slim base, since the matching GHCR base tag for an
|
|
758
|
+
// unreleased dev build does not exist. Mirrors hasLocallyLinkedTypeclawDep.
|
|
759
|
+
const devMode = typeclawSpec.startsWith('file:') || typeclawSpec.startsWith('link:')
|
|
732
760
|
|
|
733
761
|
const typeclawConfig = await readTypeclawConfig(root)
|
|
734
762
|
await writeFile(
|
|
735
763
|
join(root, DOCKERFILE),
|
|
736
764
|
buildDockerfile(typeclawConfig.docker.file, {
|
|
737
|
-
|
|
765
|
+
// A local-spec dev install must inline the heavy stack: its unreleased
|
|
766
|
+
// version has no published `typeclaw-base` tag, so resolving a base
|
|
767
|
+
// image version would pin a nonexistent `FROM ghcr.io/...:<version>`.
|
|
768
|
+
// `null` selects the inline `oven/bun:1-slim` path in buildDockerfile.
|
|
769
|
+
baseImageVersion: devMode ? null : resolveBaseImageVersion(root),
|
|
738
770
|
cjkFontsAuto: hostLocaleIsCjk(),
|
|
739
771
|
}),
|
|
740
772
|
{ flag: 'wx' },
|
|
@@ -1076,7 +1108,9 @@ function ignoreExists(error: NodeJS.ErrnoException): void {
|
|
|
1076
1108
|
// behavior under a mode flag.
|
|
1077
1109
|
|
|
1078
1110
|
export type ChannelKind =
|
|
1111
|
+
| 'discord'
|
|
1079
1112
|
| 'discord-bot'
|
|
1113
|
+
| 'slack'
|
|
1080
1114
|
| 'slack-bot'
|
|
1081
1115
|
| 'telegram-bot'
|
|
1082
1116
|
| 'webex'
|
|
@@ -1091,6 +1125,8 @@ export type ChannelKind =
|
|
|
1091
1125
|
// when reading typeclaw.json.
|
|
1092
1126
|
export const CHANNEL_KINDS: ReadonlyArray<ChannelKind> = [
|
|
1093
1127
|
'slack-bot',
|
|
1128
|
+
'slack',
|
|
1129
|
+
'discord',
|
|
1094
1130
|
'discord-bot',
|
|
1095
1131
|
'telegram-bot',
|
|
1096
1132
|
'webex',
|
|
@@ -1100,7 +1136,15 @@ export const CHANNEL_KINDS: ReadonlyArray<ChannelKind> = [
|
|
|
1100
1136
|
'github',
|
|
1101
1137
|
]
|
|
1102
1138
|
|
|
1103
|
-
export type AddChannelStep =
|
|
1139
|
+
export type AddChannelStep =
|
|
1140
|
+
| 'line-auth'
|
|
1141
|
+
| 'kakaotalk-auth'
|
|
1142
|
+
| 'webex-auth'
|
|
1143
|
+
| 'discord-auth'
|
|
1144
|
+
| 'slack-auth'
|
|
1145
|
+
| 'config'
|
|
1146
|
+
| 'secrets'
|
|
1147
|
+
| 'github-webhooks'
|
|
1104
1148
|
|
|
1105
1149
|
export type AddChannelStepEvent =
|
|
1106
1150
|
| { step: 'config'; phase: 'start' }
|
|
@@ -1111,6 +1155,10 @@ export type AddChannelStepEvent =
|
|
|
1111
1155
|
| { step: 'kakaotalk-auth'; phase: 'done'; result: KakaotalkAuthResult }
|
|
1112
1156
|
| { step: 'webex-auth'; phase: 'start' }
|
|
1113
1157
|
| { step: 'webex-auth'; phase: 'done'; result: WebexAuthResult }
|
|
1158
|
+
| { step: 'discord-auth'; phase: 'start' }
|
|
1159
|
+
| { step: 'discord-auth'; phase: 'done'; result: DiscordAuthResult }
|
|
1160
|
+
| { step: 'slack-auth'; phase: 'start' }
|
|
1161
|
+
| { step: 'slack-auth'; phase: 'done'; result: SlackAuthResult }
|
|
1114
1162
|
| { step: 'secrets'; phase: 'start' }
|
|
1115
1163
|
| { step: 'secrets'; phase: 'done' }
|
|
1116
1164
|
| { step: 'github-webhooks'; phase: 'start' }
|
|
@@ -1124,6 +1172,8 @@ export type AddChannelOptions = {
|
|
|
1124
1172
|
onProgress?: (event: AddChannelStepEvent) => void
|
|
1125
1173
|
} & (
|
|
1126
1174
|
| { channel: 'discord-bot'; discordBotToken: string }
|
|
1175
|
+
| { channel: 'discord'; runDiscordAuth: DiscordAuthRunner }
|
|
1176
|
+
| { channel: 'slack'; slackQrDataUrl: string; runSlackAuth?: SlackAuthRunner }
|
|
1127
1177
|
| { channel: 'slack-bot'; slackBotToken: string; slackAppToken: string }
|
|
1128
1178
|
| { channel: 'telegram-bot'; telegramBotToken: string }
|
|
1129
1179
|
| { channel: 'webex-bot'; webexBotToken: string }
|
|
@@ -1144,6 +1194,9 @@ export type AddChannelOptions = {
|
|
|
1144
1194
|
}
|
|
1145
1195
|
)
|
|
1146
1196
|
|
|
1197
|
+
export type SlackAuthRunner = (options: { cwd: string; qrDataUrl: string }) => Promise<SlackAuthResult>
|
|
1198
|
+
export type DiscordAuthRunner = (options: { cwd: string }) => Promise<DiscordAuthResult>
|
|
1199
|
+
|
|
1147
1200
|
export async function runAddChannel(options: AddChannelOptions): Promise<void> {
|
|
1148
1201
|
const emit = options.onProgress ?? (() => {})
|
|
1149
1202
|
|
|
@@ -1176,6 +1229,21 @@ export async function runAddChannel(options: AddChannelOptions): Promise<void> {
|
|
|
1176
1229
|
if (!result.ok) throw new Error(`Webex authentication failed: ${result.reason}`)
|
|
1177
1230
|
}
|
|
1178
1231
|
|
|
1232
|
+
if (options.channel === 'discord') {
|
|
1233
|
+
emit({ step: 'discord-auth', phase: 'start' })
|
|
1234
|
+
const result = await options.runDiscordAuth({ cwd: options.cwd })
|
|
1235
|
+
emit({ step: 'discord-auth', phase: 'done', result })
|
|
1236
|
+
if (!result.ok) throw new Error(`Discord authentication failed: ${result.reason}`)
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
if (options.channel === 'slack') {
|
|
1240
|
+
emit({ step: 'slack-auth', phase: 'start' })
|
|
1241
|
+
const runner = options.runSlackAuth ?? defaultSlackAuthRunner
|
|
1242
|
+
const result = await runner({ cwd: options.cwd, qrDataUrl: options.slackQrDataUrl })
|
|
1243
|
+
emit({ step: 'slack-auth', phase: 'done', result })
|
|
1244
|
+
if (!result.ok) throw new Error(`Slack authentication failed: ${result.reason}`)
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1179
1247
|
emit({ step: 'config', phase: 'start' })
|
|
1180
1248
|
await mergeChannelIntoConfig(options.cwd, options)
|
|
1181
1249
|
emit({ step: 'config', phase: 'done' })
|
|
@@ -1244,8 +1312,12 @@ function channelSecretsFromOptions(options: AddChannelOptions): ChannelSecrets {
|
|
|
1244
1312
|
switch (options.channel) {
|
|
1245
1313
|
case 'discord-bot':
|
|
1246
1314
|
return { token: options.discordBotToken }
|
|
1315
|
+
case 'discord':
|
|
1316
|
+
return {}
|
|
1247
1317
|
case 'slack-bot':
|
|
1248
1318
|
return { botToken: options.slackBotToken, appToken: options.slackAppToken }
|
|
1319
|
+
case 'slack':
|
|
1320
|
+
return {}
|
|
1249
1321
|
case 'telegram-bot':
|
|
1250
1322
|
return { token: options.telegramBotToken }
|
|
1251
1323
|
case 'webex-bot':
|
|
@@ -1266,6 +1338,24 @@ function channelSecretsFromOptions(options: AddChannelOptions): ChannelSecrets {
|
|
|
1266
1338
|
}
|
|
1267
1339
|
}
|
|
1268
1340
|
|
|
1341
|
+
async function defaultSlackAuthRunner(options: { cwd: string; qrDataUrl: string }): Promise<SlackAuthResult> {
|
|
1342
|
+
const { runSlackBootstrap } = await import('./slack-auth')
|
|
1343
|
+
return await runSlackBootstrap({ agentDir: options.cwd, qrDataUrl: options.qrDataUrl })
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
async function defaultDiscordAuthRunner(options: { cwd: string }): Promise<DiscordAuthResult> {
|
|
1347
|
+
const [{ runDiscordBootstrap }, QRCode] = await Promise.all([import('./discord-auth'), import('qrcode')])
|
|
1348
|
+
return await runDiscordBootstrap({
|
|
1349
|
+
agentDir: options.cwd,
|
|
1350
|
+
onQrUrl: async (url) => {
|
|
1351
|
+
const qr = await QRCode.default.toString(url, { type: 'terminal', small: true })
|
|
1352
|
+
console.log(
|
|
1353
|
+
['Open Discord mobile app → Settings → scan QR.', '', qr, '', 'Approve the login on your phone.'].join('\n'),
|
|
1354
|
+
)
|
|
1355
|
+
},
|
|
1356
|
+
})
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1269
1359
|
type ChannelSecrets = Record<string, string>
|
|
1270
1360
|
|
|
1271
1361
|
// Returns the set of channel keys already present in typeclaw.json. Used by
|
|
@@ -104,34 +104,59 @@ type PackageJsonShape = {
|
|
|
104
104
|
// re-resolve `latest` on every start and silently rewrite package.json when the
|
|
105
105
|
// registry moves — bypassing the pluginAddition security gate, which only fires
|
|
106
106
|
// on a guarded config write.
|
|
107
|
+
//
|
|
108
|
+
// Registry resolution for genuinely-new bare names runs CONCURRENTLY: each
|
|
109
|
+
// `resolveLatest` spawns a `bun pm view` subprocess (a serial pass paid the
|
|
110
|
+
// per-call network round-trip once per new plugin on the cold-start critical
|
|
111
|
+
// path). A hard resolver failure (network/auth, NOT a 404) still rejects the
|
|
112
|
+
// whole pass via Promise.all, preserving the "network errors block start"
|
|
113
|
+
// contract. The `skipped` list is sorted so its order is independent of which
|
|
114
|
+
// concurrent probe settled first.
|
|
115
|
+
//
|
|
116
|
+
// Last-entry-wins is positional, matching the prior serial loop: for a name
|
|
117
|
+
// listed more than once, the LAST entry decides. An explicit/pinned version
|
|
118
|
+
// applied during the loop must therefore survive even if an EARLIER bare entry
|
|
119
|
+
// for the same name resolved from the registry afterward — so the post-await
|
|
120
|
+
// assignment is skipped for any name whose last occurrence was not the bare one.
|
|
107
121
|
async function resolveDesiredManaged(
|
|
108
122
|
plugins: readonly string[],
|
|
109
123
|
previousManaged: Record<string, string>,
|
|
110
124
|
resolveLatest: ResolveLatestVersion,
|
|
111
125
|
): Promise<{ desired: Record<string, string>; skipped: string[] }> {
|
|
112
126
|
const desired: Record<string, string> = {}
|
|
113
|
-
const
|
|
127
|
+
const bareIsLastFor = new Set<string>()
|
|
128
|
+
const toResolve: string[] = []
|
|
114
129
|
for (const entry of plugins) {
|
|
115
130
|
if (isLocalEntry(entry)) continue
|
|
116
131
|
const { name, versionSpec } = splitPluginEntrySpec(entry)
|
|
117
132
|
if (name.length === 0) continue
|
|
118
133
|
if (versionSpec !== undefined) {
|
|
119
134
|
desired[name] = versionSpec
|
|
135
|
+
bareIsLastFor.delete(name)
|
|
120
136
|
continue
|
|
121
137
|
}
|
|
122
138
|
const pinned = previousManaged[name]
|
|
123
139
|
if (pinned !== undefined) {
|
|
124
140
|
desired[name] = pinned
|
|
141
|
+
bareIsLastFor.delete(name)
|
|
125
142
|
continue
|
|
126
143
|
}
|
|
127
|
-
|
|
128
|
-
if (
|
|
144
|
+
bareIsLastFor.add(name)
|
|
145
|
+
if (!toResolve.includes(name)) toResolve.push(name)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const resolved = await Promise.all(toResolve.map(async (name) => ({ name, version: await resolveLatest(name) })))
|
|
149
|
+
const skipped: string[] = []
|
|
150
|
+
for (const { name, version } of resolved) {
|
|
151
|
+
if (!bareIsLastFor.has(name)) continue
|
|
152
|
+
if (version === null) {
|
|
129
153
|
skipped.push(name)
|
|
130
154
|
continue
|
|
131
155
|
}
|
|
132
|
-
desired[name] =
|
|
156
|
+
desired[name] = version
|
|
133
157
|
}
|
|
134
|
-
|
|
158
|
+
|
|
159
|
+
return { desired: sortKeys(desired), skipped: skipped.sort() }
|
|
135
160
|
}
|
|
136
161
|
|
|
137
162
|
function isLocalEntry(entry: string): boolean {
|
|
@@ -6,7 +6,9 @@ import { runClaimSession } from '@/role-claim'
|
|
|
6
6
|
import type { ChannelKind } from './index'
|
|
7
7
|
|
|
8
8
|
const CHANNEL_LABELS: Record<ChannelKind, string> = {
|
|
9
|
+
slack: 'Slack (User)',
|
|
9
10
|
'slack-bot': 'Slack',
|
|
11
|
+
discord: 'Discord (User)',
|
|
10
12
|
'discord-bot': 'Discord',
|
|
11
13
|
github: 'GitHub',
|
|
12
14
|
'telegram-bot': 'Telegram',
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { loginWithQr as upstreamLoginWithQr, type QrSession } from 'agent-messenger/slack'
|
|
4
|
+
|
|
5
|
+
import type { SlackAccountRecord } from '@/secrets/schema'
|
|
6
|
+
import { SecretsSlackCredentialStore } from '@/secrets/slack-store'
|
|
7
|
+
|
|
8
|
+
export type SlackBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
9
|
+
|
|
10
|
+
export type LoginWithQrFn = typeof upstreamLoginWithQr
|
|
11
|
+
|
|
12
|
+
export type SlackLoginInput = {
|
|
13
|
+
qrDataUrl: string
|
|
14
|
+
agentDir: string
|
|
15
|
+
loginWithQr?: LoginWithQrFn
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function slackSecretsPath(agentDir: string): string {
|
|
19
|
+
return join(agentDir, 'secrets.json')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function runSlackBootstrap(input: SlackLoginInput): Promise<SlackBootstrapStatus> {
|
|
23
|
+
try {
|
|
24
|
+
const loginWithQr = input.loginWithQr ?? upstreamLoginWithQr
|
|
25
|
+
const store = new SecretsSlackCredentialStore({ mode: 'host', secretsPath: slackSecretsPath(input.agentDir) })
|
|
26
|
+
const result = await loginWithQr(input.qrDataUrl)
|
|
27
|
+
const now = new Date().toISOString()
|
|
28
|
+
const accountId = workspaceId(result)
|
|
29
|
+
const account: SlackAccountRecord = {
|
|
30
|
+
account_id: accountId,
|
|
31
|
+
token: result.token,
|
|
32
|
+
cookie: result.cookie,
|
|
33
|
+
workspace_id: accountId,
|
|
34
|
+
workspace_name: result.workspace,
|
|
35
|
+
created_at: now,
|
|
36
|
+
updated_at: now,
|
|
37
|
+
}
|
|
38
|
+
await store.setAccount(account)
|
|
39
|
+
await store.setCurrentAccount(accountId)
|
|
40
|
+
return { ok: true }
|
|
41
|
+
} catch (err) {
|
|
42
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function workspaceId(result: QrSession): string {
|
|
47
|
+
const record = result as QrSession & { workspace_id?: unknown }
|
|
48
|
+
return typeof record.workspace_id === 'string' && record.workspace_id !== '' ? record.workspace_id : result.workspace
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type { QrSession }
|