typeclaw 0.48.0 → 0.48.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/package.json +1 -1
- package/src/agent/plugin-tools.ts +68 -9
- package/src/agent-messenger/config-dir.ts +131 -0
- package/src/agent-messenger/host-config-dir.ts +121 -0
- package/src/cli/channel.ts +15 -16
- package/src/cli/hostd.ts +8 -13
- package/src/cli/init.ts +21 -7
- package/src/cli/model.ts +6 -3
- package/src/cli/restart.ts +13 -15
- package/src/compose/restart.ts +14 -6
- package/src/config/providers.ts +14 -125
- package/src/container/agent-operation-lock.ts +85 -0
- package/src/container/controller.ts +6 -0
- package/src/container/index.ts +8 -0
- package/src/container/restart.ts +55 -0
- package/src/container/shared.ts +5 -0
- package/src/container/start.ts +70 -4
- package/src/container/stop.ts +17 -1
- package/src/hostd/daemon.ts +1 -1
- package/src/hostd/paths.ts +17 -0
- package/src/hostd/supervisor.ts +27 -10
- package/src/init/dockerfile.ts +6 -5
- package/src/init/env-file.ts +42 -18
- package/src/init/instagram-auth.ts +55 -31
- package/src/init/kakaotalk-auth.ts +1 -5
- package/src/init/line-auth.ts +45 -29
- package/src/init/models-dev.ts +114 -7
- package/src/init/opengateway-catalog.ts +120 -0
- package/src/sandbox/canonical-secrets.ts +10 -1
- package/src/sandbox/errors.ts +0 -32
- package/src/sandbox/privileged-runtime.ts +8 -94
- package/src/test-helpers/fake-agent-operation-lock.ts +66 -0
- package/typeclaw.schema.json +5 -30
package/src/hostd/supervisor.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { CurrentHostDaemon } from '@/container'
|
|
2
|
+
import {
|
|
3
|
+
type AgentOperationLease,
|
|
4
|
+
reserveAgentOperationLock,
|
|
5
|
+
type ReserveAgentOperationLock,
|
|
6
|
+
} from '@/container/agent-operation-lock'
|
|
2
7
|
|
|
3
8
|
import type { Response } from './protocol'
|
|
4
9
|
|
|
@@ -13,6 +18,7 @@ export type SupervisorRestart = (input: {
|
|
|
13
18
|
// Injected by the daemon so the restart registers the container in-process
|
|
14
19
|
// instead of over the socket — see CurrentHostDaemon docs in src/container.
|
|
15
20
|
currentHostDaemon?: CurrentHostDaemon
|
|
21
|
+
operationLease?: AgentOperationLease
|
|
16
22
|
}) => Promise<{ ok: true } | { ok: false; reason: string }>
|
|
17
23
|
|
|
18
24
|
export type SupervisorOptions = {
|
|
@@ -30,34 +36,43 @@ export type Supervisor = {
|
|
|
30
36
|
cwd: string
|
|
31
37
|
build?: boolean
|
|
32
38
|
currentHostDaemon?: CurrentHostDaemon
|
|
33
|
-
}) => Response
|
|
39
|
+
}) => Promise<Response>
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
export type SupervisorBuildOptions = {
|
|
37
43
|
restart: SupervisorRestart
|
|
38
44
|
onLog: (event: SupervisorLogEvent) => void
|
|
39
45
|
isStopped: () => boolean
|
|
46
|
+
reserveLock?: ReserveAgentOperationLock
|
|
40
47
|
}
|
|
41
48
|
|
|
42
|
-
// The daemon
|
|
43
|
-
//
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
export function buildSupervisor({
|
|
49
|
+
// The daemon reserves the lifecycle lock before ACKing, then runs stop+start in
|
|
50
|
+
// the background so the agent's RPC connection can close before `docker stop`.
|
|
51
|
+
// The requester exits about 500 ms after the ACK, so a lock failure discovered
|
|
52
|
+
// later would be invisible; rejecting the ACK lets the agent surface and retry
|
|
53
|
+
// the error. Failures after scheduling are still surfaced through the log channel.
|
|
54
|
+
export function buildSupervisor({
|
|
55
|
+
restart,
|
|
56
|
+
onLog,
|
|
57
|
+
isStopped,
|
|
58
|
+
reserveLock = reserveAgentOperationLock,
|
|
59
|
+
}: SupervisorBuildOptions): Supervisor {
|
|
48
60
|
return {
|
|
49
|
-
scheduleRestart: ({ containerName, cwd, build = false, currentHostDaemon }): Response => {
|
|
61
|
+
scheduleRestart: async ({ containerName, cwd, build = false, currentHostDaemon }): Promise<Response> => {
|
|
50
62
|
if (isStopped()) return { ok: false, reason: 'daemon stopping' }
|
|
63
|
+
const reservation = await reserveLock({ agentDir: cwd, operation: 'restart' })
|
|
64
|
+
if (!reservation.ok) return { ok: false, reason: reservation.reason }
|
|
51
65
|
onLog({ kind: 'restart-scheduled', containerName, build })
|
|
52
|
-
void runRestart()
|
|
66
|
+
void runRestart(reservation.lease, reservation.release)
|
|
53
67
|
return { ok: true }
|
|
54
68
|
|
|
55
|
-
async function runRestart(): Promise<void> {
|
|
69
|
+
async function runRestart(lease: AgentOperationLease, release: () => Promise<void>): Promise<void> {
|
|
56
70
|
try {
|
|
57
71
|
const result = await restart({
|
|
58
72
|
containerName,
|
|
59
73
|
cwd,
|
|
60
74
|
build,
|
|
75
|
+
operationLease: lease,
|
|
61
76
|
...(currentHostDaemon ? { currentHostDaemon } : {}),
|
|
62
77
|
})
|
|
63
78
|
if (result.ok) onLog({ kind: 'restart-completed', containerName })
|
|
@@ -68,6 +83,8 @@ export function buildSupervisor({ restart, onLog, isStopped }: SupervisorBuildOp
|
|
|
68
83
|
containerName,
|
|
69
84
|
reason: error instanceof Error ? error.message : String(error),
|
|
70
85
|
})
|
|
86
|
+
} finally {
|
|
87
|
+
await release().catch(() => {})
|
|
71
88
|
}
|
|
72
89
|
}
|
|
73
90
|
},
|
package/src/init/dockerfile.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CONTAINER_AGENT_MESSENGER_CONFIG_DIR } from '@/agent-messenger/config-dir'
|
|
1
2
|
import { ASKPASS_SCRIPT, TYPECLAW_GIT_ASKPASS_PATH } from '@/bundled-plugins/github-cli-auth/git-askpass'
|
|
2
3
|
import { validateDockerfileAppendLine } from '@/config/config'
|
|
3
4
|
import type { DockerfileConfig, DockerfileFeatureToggle } from '@/config/config'
|
|
@@ -1380,11 +1381,11 @@ ENV NODE_ENV=production
|
|
|
1380
1381
|
# Persist first-party GWS config without changing global XDG/git config lookup.
|
|
1381
1382
|
ENV GWS_CONFIG_HOME=/agent/workspace/.config/gws
|
|
1382
1383
|
|
|
1383
|
-
# Keep agent-messenger
|
|
1384
|
-
#
|
|
1385
|
-
# credentials live in secrets.json#channels.kakaotalk and
|
|
1386
|
-
# through hostd's secrets-patch RPC.
|
|
1387
|
-
ENV AGENT_MESSENGER_CONFIG_DIR
|
|
1384
|
+
# Keep agent-messenger runtime storage inside workspace/. LINE writes E2EE key
|
|
1385
|
+
# material under line-storage/ here; Instagram stores its live session here.
|
|
1386
|
+
# KakaoTalk credentials instead live in secrets.json#channels.kakaotalk and
|
|
1387
|
+
# container writes go through hostd's secrets-patch RPC.
|
|
1388
|
+
ENV AGENT_MESSENGER_CONFIG_DIR=${CONTAINER_AGENT_MESSENGER_CONFIG_DIR}
|
|
1388
1389
|
|
|
1389
1390
|
${customLines}ENTRYPOINT ["${TYPECLAW_ENTRYPOINT_PATH}"]
|
|
1390
1391
|
CMD ["run"]
|
package/src/init/env-file.ts
CHANGED
|
@@ -3,10 +3,16 @@ import { join } from 'node:path'
|
|
|
3
3
|
|
|
4
4
|
const ENV_FILE = '.env'
|
|
5
5
|
|
|
6
|
-
// Parse the agent's `.env` into a key-value map,
|
|
7
|
-
// `--env-file` parser
|
|
8
|
-
//
|
|
9
|
-
//
|
|
6
|
+
// Parse the agent's `.env` into a key-value map, mirroring Docker's
|
|
7
|
+
// `--env-file` parser (docker/cli `pkg/kvfile.parseKeyValueFile`): a UTF-8 BOM
|
|
8
|
+
// is stripped from the FIRST line only, every line is left-trimmed of Unicode
|
|
9
|
+
// whitespace BEFORE the blank/`#` check, and nothing is trimmed around or after
|
|
10
|
+
// `=` (no quote stripping, no shell expansion). Last value wins on duplicates.
|
|
11
|
+
//
|
|
12
|
+
// The normalization is load-bearing, not cosmetic: Docker accepts a BOM-prefixed
|
|
13
|
+
// or indented `KEY=value` and passes it to the container, so a parser that missed
|
|
14
|
+
// those would report an operator-declared variable as absent — which callers read
|
|
15
|
+
// as "TypeClaw may manage this", e.g. migrating a deliberately pinned directory.
|
|
10
16
|
export function readEnvFile(cwd: string): Map<string, string> {
|
|
11
17
|
const out = new Map<string, string>()
|
|
12
18
|
let raw: string
|
|
@@ -16,18 +22,35 @@ export function readEnvFile(cwd: string): Map<string, string> {
|
|
|
16
22
|
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') return out
|
|
17
23
|
throw err
|
|
18
24
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const key = line.slice(0, eq)
|
|
25
|
-
const value = line.slice(eq + 1)
|
|
26
|
-
out.set(key, value)
|
|
25
|
+
const lines = raw.split(/\r?\n/)
|
|
26
|
+
for (const [index, rawLine] of lines.entries()) {
|
|
27
|
+
const parsed = parseEnvLine(rawLine, index)
|
|
28
|
+
if (parsed === null) continue
|
|
29
|
+
out.set(parsed.key, parsed.value)
|
|
27
30
|
}
|
|
28
31
|
return out
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
// Shared by the reader and the writer so a declaration one of them recognizes
|
|
35
|
+
// can never be invisible to the other — an indented key that parsed as declared
|
|
36
|
+
// but did not match on write would append a duplicate instead of replacing.
|
|
37
|
+
function parseEnvLine(rawLine: string, index: number): { key: string; value: string } | null {
|
|
38
|
+
const withoutBom = index === 0 ? rawLine.replace(/^\uFEFF/, '') : rawLine
|
|
39
|
+
// \p{White_Space}, not \s: JS \s also matches U+FEFF (and misses U+0085),
|
|
40
|
+
// so \s would strip a BOM on EVERY line while Go's unicode.IsSpace — which
|
|
41
|
+
// Docker uses — strips it on line 1 only.
|
|
42
|
+
const line = withoutBom.replace(/^\p{White_Space}+/u, '')
|
|
43
|
+
if (line.length === 0) return null
|
|
44
|
+
if (line.startsWith('#')) return null
|
|
45
|
+
const eq = line.indexOf('=')
|
|
46
|
+
if (eq <= 0) return null
|
|
47
|
+
const key = line.slice(0, eq)
|
|
48
|
+
// Docker rejects the whole file when a key contains a space or tab, so such
|
|
49
|
+
// a line can never reach a container; skip it rather than inventing a key.
|
|
50
|
+
if (/[ \t]/.test(key)) return null
|
|
51
|
+
return { key, value: line.slice(eq + 1) }
|
|
52
|
+
}
|
|
53
|
+
|
|
31
54
|
export function hasEnvKey(cwd: string, key: string): boolean {
|
|
32
55
|
const value = readEnvFile(cwd).get(key)
|
|
33
56
|
return value !== undefined && value.length > 0
|
|
@@ -52,13 +75,14 @@ export function appendOrReplaceEnvKey(cwd: string, key: string, value: string):
|
|
|
52
75
|
// regardless of replace-vs-append path.
|
|
53
76
|
if (lines.length > 0 && lines[lines.length - 1] === '') lines.pop()
|
|
54
77
|
let replaced = false
|
|
55
|
-
const next = lines.map((line) => {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (eq <= 0) return line
|
|
59
|
-
if (line.slice(0, eq) !== key) return line
|
|
78
|
+
const next = lines.map((line, index) => {
|
|
79
|
+
const parsed = parseEnvLine(line, index)
|
|
80
|
+
if (parsed === null || parsed.key !== key) return line
|
|
60
81
|
replaced = true
|
|
61
|
-
|
|
82
|
+
// Re-emit the BOM so replacing the first line does not silently strip the
|
|
83
|
+
// file's encoding marker.
|
|
84
|
+
const bom = index === 0 && line.startsWith('\uFEFF') ? '\uFEFF' : ''
|
|
85
|
+
return `${bom}${key}=${value}`
|
|
62
86
|
})
|
|
63
87
|
if (!replaced) next.push(`${key}=${value}`)
|
|
64
88
|
const out = `${next.join('\n')}\n`
|
|
@@ -8,10 +8,20 @@ import {
|
|
|
8
8
|
type InstagramAccount,
|
|
9
9
|
} from 'agent-messenger/instagram'
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
prepareAgentMessengerHostConfigDir,
|
|
13
|
+
type HostAgentMessengerConfigResult,
|
|
14
|
+
} from '@/agent-messenger/host-config-dir'
|
|
15
|
+
import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
|
|
11
16
|
import { SecretsInstagramCredentialStore } from '@/secrets/instagram-store'
|
|
12
17
|
|
|
13
18
|
export type InstagramBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
14
19
|
|
|
20
|
+
export type AgentMessengerHostConfigDeps = {
|
|
21
|
+
prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
|
|
22
|
+
operationLock?: WithAgentOperationLock
|
|
23
|
+
}
|
|
24
|
+
|
|
15
25
|
// Instagram gates a fresh login behind one of two interactive second factors:
|
|
16
26
|
// a 2FA code (authenticator app / SMS the account already has enabled) or a
|
|
17
27
|
// checkpoint challenge (a suspicious-login verification code sent to email or
|
|
@@ -78,44 +88,58 @@ export function instagramSecretsPath(agentDir: string): string {
|
|
|
78
88
|
return join(agentDir, 'secrets.json')
|
|
79
89
|
}
|
|
80
90
|
|
|
81
|
-
export function
|
|
82
|
-
|
|
91
|
+
export async function runInstagramBootstrap(
|
|
92
|
+
input: InstagramLoginInput,
|
|
93
|
+
deps: AgentMessengerHostConfigDeps = {},
|
|
94
|
+
): Promise<InstagramBootstrapStatus> {
|
|
95
|
+
const operationLock = deps.operationLock ?? withAgentOperationLock
|
|
96
|
+
try {
|
|
97
|
+
const locked = await operationLock({ agentDir: input.agentDir, operation: 'instagram-auth' }, async () => {
|
|
98
|
+
return await runInstagramBootstrapLocked(input, deps)
|
|
99
|
+
})
|
|
100
|
+
return locked.ok ? locked.value : { ok: false, reason: locked.reason }
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
103
|
+
}
|
|
83
104
|
}
|
|
84
105
|
|
|
85
|
-
|
|
106
|
+
async function runInstagramBootstrapLocked(
|
|
107
|
+
input: InstagramLoginInput,
|
|
108
|
+
deps: AgentMessengerHostConfigDeps,
|
|
109
|
+
): Promise<InstagramBootstrapStatus> {
|
|
86
110
|
try {
|
|
111
|
+
const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
|
|
112
|
+
if (!prepared.ok) return prepared
|
|
113
|
+
|
|
87
114
|
const store = new SecretsInstagramCredentialStore({
|
|
88
115
|
mode: 'host',
|
|
89
116
|
secretsPath: instagramSecretsPath(input.agentDir),
|
|
90
117
|
})
|
|
91
|
-
const result = await withInstagramConfigDir(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
},
|
|
118
|
-
)
|
|
118
|
+
const result = await withInstagramConfigDir(prepared.hostDir, async (): Promise<InstagramBootstrapStatus> => {
|
|
119
|
+
const sdkManager = new InstagramCredentialManager()
|
|
120
|
+
const manager = input.credentialManager ?? sdkManager
|
|
121
|
+
const accountId = createAccountId(input.username)
|
|
122
|
+
const paths = await manager.ensureAccountPaths(accountId)
|
|
123
|
+
const client = input.client ?? buildInstagramClient(sdkManager)
|
|
124
|
+
client.setSessionPath(paths.session_path)
|
|
125
|
+
|
|
126
|
+
// A checkpoint login persists a partial SDK session (challenge_path +
|
|
127
|
+
// cookies) inside authenticate(), before we know whether the operator
|
|
128
|
+
// can finish it. Snapshot the file up front and restore it on any
|
|
129
|
+
// failure — whether completeLogin returns a non-ok status (fail-closed
|
|
130
|
+
// / cancelled) or throws (e.g. challengeSubmitCode rejecting a wrong
|
|
131
|
+
// code) — so a half-written session is never left behind. Rethrow so
|
|
132
|
+
// the outer catch reports the error.
|
|
133
|
+
const sessionSnapshot = await snapshotSessionFile(paths.session_path)
|
|
134
|
+
try {
|
|
135
|
+
const result = await completeLogin({ store, manager, client, accountId, input })
|
|
136
|
+
if (!result.ok) await restoreSessionFile(paths.session_path, sessionSnapshot)
|
|
137
|
+
return result
|
|
138
|
+
} catch (err) {
|
|
139
|
+
await restoreSessionFile(paths.session_path, sessionSnapshot)
|
|
140
|
+
throw err
|
|
141
|
+
}
|
|
142
|
+
})
|
|
119
143
|
return result
|
|
120
144
|
} catch (err) {
|
|
121
145
|
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { join,
|
|
1
|
+
import { join, resolve } from 'node:path'
|
|
2
2
|
|
|
3
3
|
import { loginFlow as upstreamLoginFlow } from 'agent-messenger/kakaotalk'
|
|
4
4
|
|
|
@@ -33,10 +33,6 @@ export type LoginFlowFn = typeof upstreamLoginFlow
|
|
|
33
33
|
export type LoginFlowOptions = Parameters<LoginFlowFn>[0]
|
|
34
34
|
export type LoginFlowResult = Awaited<ReturnType<LoginFlowFn>>
|
|
35
35
|
|
|
36
|
-
export function kakaotalkConfigDir(agentDir: string): string {
|
|
37
|
-
return posix.join(agentDir, 'workspace', '.agent-messenger')
|
|
38
|
-
}
|
|
39
|
-
|
|
40
36
|
export function kakaotalkSecretsPath(agentDir: string): string {
|
|
41
37
|
return join(agentDir, 'secrets.json')
|
|
42
38
|
}
|
package/src/init/line-auth.ts
CHANGED
|
@@ -2,10 +2,20 @@ import { join } from 'node:path'
|
|
|
2
2
|
|
|
3
3
|
import { LineClient as RealLineClient, LineCredentialManager, type LineLoginResult } from 'agent-messenger/line'
|
|
4
4
|
|
|
5
|
+
import {
|
|
6
|
+
prepareAgentMessengerHostConfigDir,
|
|
7
|
+
type HostAgentMessengerConfigResult,
|
|
8
|
+
} from '@/agent-messenger/host-config-dir'
|
|
9
|
+
import { type WithAgentOperationLock, withAgentOperationLock } from '@/container/agent-operation-lock'
|
|
5
10
|
import { SecretsLineCredentialStore } from '@/secrets/line-store'
|
|
6
11
|
|
|
7
12
|
export type LineBootstrapStatus = { ok: true } | { ok: false; reason: string }
|
|
8
13
|
|
|
14
|
+
export type AgentMessengerHostConfigDeps = {
|
|
15
|
+
prepareConfigDir?: (agentDir: string) => Promise<HostAgentMessengerConfigResult>
|
|
16
|
+
operationLock?: WithAgentOperationLock
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
export type LineLoginCallbacks = {
|
|
10
20
|
onQRUrl?: (url: string) => void | Promise<void>
|
|
11
21
|
onPincode: (pin: string) => void
|
|
@@ -50,36 +60,41 @@ export function lineSecretsPath(agentDir: string): string {
|
|
|
50
60
|
return join(agentDir, 'secrets.json')
|
|
51
61
|
}
|
|
52
62
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
export async function runLineBootstrap(
|
|
64
|
+
input: LineLoginInput,
|
|
65
|
+
deps: AgentMessengerHostConfigDeps = {},
|
|
66
|
+
): Promise<LineBootstrapStatus> {
|
|
67
|
+
const operationLock = deps.operationLock ?? withAgentOperationLock
|
|
68
|
+
try {
|
|
69
|
+
const locked = await operationLock({ agentDir: input.agentDir, operation: 'line-auth' }, async () => {
|
|
70
|
+
return await runLineBootstrapLocked(input, deps)
|
|
71
|
+
})
|
|
72
|
+
return locked.ok ? locked.value : { ok: false, reason: locked.reason }
|
|
73
|
+
} catch (err) {
|
|
74
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) }
|
|
75
|
+
}
|
|
62
76
|
}
|
|
63
77
|
|
|
64
|
-
|
|
78
|
+
async function runLineBootstrapLocked(
|
|
79
|
+
input: LineLoginInput,
|
|
80
|
+
deps: AgentMessengerHostConfigDeps,
|
|
81
|
+
): Promise<LineBootstrapStatus> {
|
|
65
82
|
try {
|
|
83
|
+
const prepared = await (deps.prepareConfigDir ?? prepareAgentMessengerHostConfigDir)(input.agentDir)
|
|
84
|
+
if (!prepared.ok) return prepared
|
|
85
|
+
|
|
66
86
|
const store = new SecretsLineCredentialStore({ mode: 'host', secretsPath: lineSecretsPath(input.agentDir) })
|
|
67
87
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// ~/.config/agent-messenger to keep in sync.
|
|
79
|
-
const client = input.client ?? buildLineClient(store)
|
|
80
|
-
|
|
81
|
-
return suppressLineTokenInfoDump(() =>
|
|
82
|
-
input.method === 'qr'
|
|
88
|
+
const result = await suppressLineTokenInfoDump(() =>
|
|
89
|
+
withLineConfigDir(prepared.hostDir, () => {
|
|
90
|
+
// The SDK writes Letter-Sealing keys under AGENT_MESSENGER_CONFIG_DIR.
|
|
91
|
+
// The prepared host path follows the live container when one is running,
|
|
92
|
+
// so reauth never strands E2EE material in a directory that runtime cannot read.
|
|
93
|
+
const client = input.client ?? buildLineClient(store)
|
|
94
|
+
|
|
95
|
+
// The LINE SDK persists the minted auth_token + certificate by calling
|
|
96
|
+
// setAccount() on the credential manager wired into the client.
|
|
97
|
+
return input.method === 'qr'
|
|
83
98
|
? client.loginWithQR({
|
|
84
99
|
onQRUrl: async (url) => {
|
|
85
100
|
await input.callbacks.onQRUrl?.(url)
|
|
@@ -90,9 +105,9 @@ export async function runLineBootstrap(input: LineLoginInput): Promise<LineBoots
|
|
|
90
105
|
email: input.email,
|
|
91
106
|
password: input.password,
|
|
92
107
|
onPincode: input.callbacks.onPincode,
|
|
93
|
-
})
|
|
94
|
-
)
|
|
95
|
-
|
|
108
|
+
})
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
96
111
|
|
|
97
112
|
if (!result.authenticated || result.account_id === undefined) {
|
|
98
113
|
const reason = result.message ?? result.error ?? 'LINE login did not authenticate'
|
|
@@ -126,11 +141,12 @@ function buildLineClient(store: SecretsLineCredentialStore): LineLoginClient {
|
|
|
126
141
|
|
|
127
142
|
async function withLineConfigDir<T>(dir: string, fn: () => Promise<T>): Promise<T> {
|
|
128
143
|
const previous = process.env.AGENT_MESSENGER_CONFIG_DIR
|
|
129
|
-
|
|
144
|
+
process.env.AGENT_MESSENGER_CONFIG_DIR = dir
|
|
130
145
|
try {
|
|
131
146
|
return await fn()
|
|
132
147
|
} finally {
|
|
133
148
|
if (previous === undefined) delete process.env.AGENT_MESSENGER_CONFIG_DIR
|
|
149
|
+
else process.env.AGENT_MESSENGER_CONFIG_DIR = previous
|
|
134
150
|
}
|
|
135
151
|
}
|
|
136
152
|
|
package/src/init/models-dev.ts
CHANGED
|
@@ -9,6 +9,8 @@ import {
|
|
|
9
9
|
type ModelRef,
|
|
10
10
|
} from '@/config/providers'
|
|
11
11
|
|
|
12
|
+
import { fetchOpenGatewayCatalog, type OpenGatewayModel } from './opengateway-catalog'
|
|
13
|
+
|
|
12
14
|
const MODELS_DEV_URL = 'https://models.dev/api.json'
|
|
13
15
|
const REQUEST_TIMEOUT_MS = 10_000
|
|
14
16
|
|
|
@@ -113,12 +115,17 @@ type ModelsDevProvider = {
|
|
|
113
115
|
export type FetchModelsResult = {
|
|
114
116
|
options: ModelOption[]
|
|
115
117
|
source: 'models.dev' | 'curated'
|
|
118
|
+
sources: {
|
|
119
|
+
modelsDev: 'live' | 'unavailable'
|
|
120
|
+
openGatewayCatalog: 'live' | 'unavailable'
|
|
121
|
+
openGatewayPrices: 'live' | 'unavailable'
|
|
122
|
+
}
|
|
123
|
+
warnings: string[]
|
|
116
124
|
warning?: string
|
|
117
125
|
}
|
|
118
126
|
|
|
119
|
-
// Pulls
|
|
120
|
-
// curated
|
|
121
|
-
// (provider, model) pair the user is allowed to pick at init time.
|
|
127
|
+
// Pulls live catalogs from models.dev and OpenGateway, keeps every curated
|
|
128
|
+
// entry, and appends non-curated models whose provider refs TypeClaw can route.
|
|
122
129
|
//
|
|
123
130
|
// Falls back to the curated list alone if the network is unreachable, the
|
|
124
131
|
// response is malformed, or any unexpected error fires — the wizard MUST
|
|
@@ -129,16 +136,58 @@ export async function fetchModelOptions(
|
|
|
129
136
|
): Promise<FetchModelsResult> {
|
|
130
137
|
const fetchImpl = options.fetchImpl ?? fetch
|
|
131
138
|
const timeoutMs = options.timeoutMs ?? REQUEST_TIMEOUT_MS
|
|
139
|
+
const [modelsDev, openGateway] = await Promise.all([
|
|
140
|
+
fetchModelsDev(fetchImpl, timeoutMs),
|
|
141
|
+
fetchOpenGatewayCatalog({ fetchImpl, timeoutMs }),
|
|
142
|
+
])
|
|
143
|
+
// Composition is inside the guard because only the models.dev ROOT is shape-
|
|
144
|
+
// checked on fetch: a well-formed envelope can still carry a malformed nested
|
|
145
|
+
// model, and this function's contract is that `typeclaw init` keeps working
|
|
146
|
+
// on a fresh machine no matter what the network returns.
|
|
147
|
+
let baseOptions: ModelOption[]
|
|
148
|
+
let compositionWarning: string | undefined
|
|
149
|
+
try {
|
|
150
|
+
baseOptions = modelsDev.status === 'live' ? mergeWithCurated(modelsDev.data) : curatedOptions()
|
|
151
|
+
} catch (error) {
|
|
152
|
+
baseOptions = curatedOptions()
|
|
153
|
+
compositionWarning = `models.dev returned unusable data (${error instanceof Error ? error.message : String(error)})`
|
|
154
|
+
}
|
|
155
|
+
const mergedOptions = mergeOpenGateway(baseOptions, openGateway.models)
|
|
156
|
+
const warnings = [
|
|
157
|
+
...(modelsDev.status === 'unavailable' ? [modelsDev.warning] : []),
|
|
158
|
+
...(compositionWarning !== undefined ? [compositionWarning] : []),
|
|
159
|
+
...openGateway.warnings,
|
|
160
|
+
]
|
|
161
|
+
const warning = warnings.length > 0 ? warnings.join('; ') : undefined
|
|
162
|
+
return {
|
|
163
|
+
options: mergedOptions,
|
|
164
|
+
source: modelsDev.status === 'live' ? 'models.dev' : 'curated',
|
|
165
|
+
sources: {
|
|
166
|
+
modelsDev: modelsDev.status,
|
|
167
|
+
openGatewayCatalog: openGateway.catalog,
|
|
168
|
+
openGatewayPrices: openGateway.prices,
|
|
169
|
+
},
|
|
170
|
+
warnings,
|
|
171
|
+
...(warning !== undefined ? { warning } : {}),
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
type ModelsDevResult =
|
|
176
|
+
| { status: 'live'; data: Record<string, ModelsDevProvider> }
|
|
177
|
+
| { status: 'unavailable'; warning: string }
|
|
178
|
+
|
|
179
|
+
async function fetchModelsDev(fetchImpl: typeof fetch, timeoutMs: number): Promise<ModelsDevResult> {
|
|
132
180
|
try {
|
|
133
181
|
const res = await fetchImpl(MODELS_DEV_URL, { signal: AbortSignal.timeout(timeoutMs) })
|
|
134
182
|
if (!res.ok) {
|
|
135
|
-
return {
|
|
183
|
+
return { status: 'unavailable', warning: `models.dev returned ${res.status}` }
|
|
136
184
|
}
|
|
137
|
-
const data =
|
|
138
|
-
return {
|
|
185
|
+
const data: unknown = await res.json()
|
|
186
|
+
if (!isRecord(data)) return { status: 'unavailable', warning: 'models.dev returned malformed JSON' }
|
|
187
|
+
return { status: 'live', data: data as Record<string, ModelsDevProvider> }
|
|
139
188
|
} catch (error) {
|
|
140
189
|
const reason = error instanceof Error ? error.message : String(error)
|
|
141
|
-
return {
|
|
190
|
+
return { status: 'unavailable', warning: reason }
|
|
142
191
|
}
|
|
143
192
|
}
|
|
144
193
|
|
|
@@ -184,6 +233,7 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
184
233
|
const upstream = upstreamProviderFor(data, providerId)
|
|
185
234
|
const upstreamModels = upstream?.models ?? {}
|
|
186
235
|
for (const [fallbackModelId, upstreamModel] of Object.entries(upstreamModels)) {
|
|
236
|
+
if (!isRecord(upstreamModel)) continue
|
|
187
237
|
const modelId = upstreamModel.id ?? fallbackModelId
|
|
188
238
|
if (modelId.trim().length === 0) continue
|
|
189
239
|
const ref = `${providerId}/${modelId}`
|
|
@@ -195,6 +245,59 @@ function mergeWithCurated(data: Record<string, ModelsDevProvider>): ModelOption[
|
|
|
195
245
|
return out
|
|
196
246
|
}
|
|
197
247
|
|
|
248
|
+
function mergeOpenGateway(options: ModelOption[], models: OpenGatewayModel[]): ModelOption[] {
|
|
249
|
+
const out = [...options]
|
|
250
|
+
const seen = new Set(options.map((option) => option.ref))
|
|
251
|
+
for (const model of models) {
|
|
252
|
+
const ref = `opengateway/${model.id}`
|
|
253
|
+
if (seen.has(ref) || !isModelRef(ref)) continue
|
|
254
|
+
const sibling = nativeSiblingFor(model)
|
|
255
|
+
out.push({
|
|
256
|
+
ref,
|
|
257
|
+
providerId: 'opengateway',
|
|
258
|
+
providerName: KNOWN_PROVIDERS.opengateway.name,
|
|
259
|
+
modelId: model.id,
|
|
260
|
+
modelName: sibling?.name ?? model.id,
|
|
261
|
+
reasoning: sibling?.reasoning ?? false,
|
|
262
|
+
contextWindow: sibling?.contextWindow ?? null,
|
|
263
|
+
maxTokens: sibling?.maxTokens ?? null,
|
|
264
|
+
cost: model.cost,
|
|
265
|
+
curated: false,
|
|
266
|
+
supportsVision: model.input.includes('image'),
|
|
267
|
+
})
|
|
268
|
+
seen.add(ref)
|
|
269
|
+
}
|
|
270
|
+
return out
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
type NativeSibling = {
|
|
274
|
+
name: string
|
|
275
|
+
reasoning?: boolean
|
|
276
|
+
contextWindow?: number
|
|
277
|
+
maxTokens?: number
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function nativeSiblingFor(model: OpenGatewayModel): NativeSibling | undefined {
|
|
281
|
+
const slash = model.id.indexOf('/')
|
|
282
|
+
if (slash === -1) return undefined
|
|
283
|
+
const creator = model.id.slice(0, slash)
|
|
284
|
+
const modelId = model.id.slice(slash + 1)
|
|
285
|
+
const providerId = nativeProviderId(model.ownedBy) ?? nativeProviderId(creator)
|
|
286
|
+
if (providerId === undefined) return undefined
|
|
287
|
+
return (KNOWN_PROVIDERS[providerId].models as Record<string, NativeSibling>)[modelId]
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// The gateway namespaces some creators differently from this registry's
|
|
291
|
+
// provider ids (`moonshotai` vs `moonshot`, `x-ai` vs `xai`, `z-ai` vs `zai`).
|
|
292
|
+
// Without the alias the sibling lookup misses and the model loses its
|
|
293
|
+
// contextWindow/maxTokens, which neither OpenGateway endpoint publishes.
|
|
294
|
+
function nativeProviderId(value: string): KnownProviderId | undefined {
|
|
295
|
+
const aliases: Record<string, KnownProviderId> = { moonshotai: 'moonshot', 'x-ai': 'xai', 'z-ai': 'zai' }
|
|
296
|
+
const candidate = aliases[value] ?? value
|
|
297
|
+
if (!(candidate in KNOWN_PROVIDERS) || candidate === 'opengateway') return undefined
|
|
298
|
+
return candidate as KnownProviderId
|
|
299
|
+
}
|
|
300
|
+
|
|
198
301
|
type BuildOptionOpts = {
|
|
199
302
|
curated: boolean
|
|
200
303
|
upstream?: ModelsDevModel
|
|
@@ -250,3 +353,7 @@ function resolveCost(cost: ModelsDevModel['cost']): ModelOptionCost | null {
|
|
|
250
353
|
cacheWrite: cost.cacheWrite ?? cost.cache_write ?? 0,
|
|
251
354
|
}
|
|
252
355
|
}
|
|
356
|
+
|
|
357
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
358
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
359
|
+
}
|