@sixb/cli 0.1.25 → 0.1.26
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 +5 -5
- package/dist/commands/agent.d.ts +16 -0
- package/dist/commands/agent.d.ts.map +1 -0
- package/dist/commands/dev-runner.d.ts +0 -1
- package/dist/commands/dev-runner.d.ts.map +1 -1
- package/dist/commands/worker-group.d.ts +0 -1
- package/dist/commands/worker-group.d.ts.map +1 -1
- package/dist/commands/worker.d.ts +0 -1
- package/dist/commands/worker.d.ts.map +1 -1
- package/dist/lib/command-line.d.ts +16 -6
- package/dist/lib/command-line.d.ts.map +1 -1
- package/dist/lib/deploy-release.d.ts.map +1 -1
- package/dist/lib/github-ci.d.ts.map +1 -1
- package/dist/lib/runtime.d.ts +0 -1
- package/dist/lib/runtime.d.ts.map +1 -1
- package/dist/lib/worker-registry.d.ts +0 -1
- package/dist/lib/worker-registry.d.ts.map +1 -1
- package/dist/tui/app.d.ts +19 -0
- package/dist/tui/app.d.ts.map +1 -0
- package/dist/tui/brand.d.ts +7 -0
- package/dist/tui/brand.d.ts.map +1 -0
- package/dist/tui/computer.d.ts +3 -0
- package/dist/tui/computer.d.ts.map +1 -0
- package/dist/tui/connection.d.ts +45 -0
- package/dist/tui/connection.d.ts.map +1 -0
- package/dist/tui/folders.d.ts +7 -0
- package/dist/tui/folders.d.ts.map +1 -0
- package/dist/tui/format.d.ts +33 -0
- package/dist/tui/format.d.ts.map +1 -0
- package/dist/tui/index.d.ts +12 -0
- package/dist/tui/index.d.ts.map +1 -0
- package/dist/tui/palette.d.ts +25 -0
- package/dist/tui/palette.d.ts.map +1 -0
- package/dist/tui/parts.d.ts +66 -0
- package/dist/tui/parts.d.ts.map +1 -0
- package/dist/tui/runner.d.ts +52 -0
- package/dist/tui/runner.d.ts.map +1 -0
- package/dist/tui/settings.d.ts +44 -0
- package/dist/tui/settings.d.ts.map +1 -0
- package/dist/tui/slash.d.ts +14 -0
- package/dist/tui/slash.d.ts.map +1 -0
- package/dist/tui/theme.d.ts +39 -0
- package/dist/tui/theme.d.ts.map +1 -0
- package/dist/tui/transcript.d.ts +52 -0
- package/dist/tui/transcript.d.ts.map +1 -0
- package/dist/tui/ui.d.ts +35 -0
- package/dist/tui/ui.d.ts.map +1 -0
- package/package.json +17 -15
- package/src/commands/agent.ts +337 -0
- package/src/commands/dev-runner.ts +0 -4
- package/src/commands/worker-group.ts +0 -4
- package/src/commands/worker.ts +0 -4
- package/src/index.tsx +57 -5
- package/src/lib/command-line.ts +52 -15
- package/src/lib/deploy-release.ts +0 -11
- package/src/lib/github-ci.ts +3 -1
- package/src/lib/runtime.ts +0 -2
- package/src/lib/worker-registry.ts +0 -2
- package/src/tui/app.tsx +1198 -0
- package/src/tui/brand.tsx +51 -0
- package/src/tui/computer.ts +54 -0
- package/src/tui/connection.ts +149 -0
- package/src/tui/folders.ts +41 -0
- package/src/tui/format.ts +260 -0
- package/src/tui/index.tsx +353 -0
- package/src/tui/palette.tsx +183 -0
- package/src/tui/parts.ts +254 -0
- package/src/tui/runner.ts +173 -0
- package/src/tui/settings.ts +127 -0
- package/src/tui/slash.ts +32 -0
- package/src/tui/theme.ts +111 -0
- package/src/tui/transcript.tsx +555 -0
- package/src/tui/ui.tsx +148 -0
- package/dist/lib/agent-turn-timeout.d.ts +0 -8
- package/dist/lib/agent-turn-timeout.d.ts.map +0 -1
- package/src/lib/agent-turn-timeout.ts +0 -46
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/react */
|
|
2
|
+
import type { TuiColors } from "./theme"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sixb's name, set in half blocks at one weight: every stroke a single pixel, so stems and bars
|
|
6
|
+
* match and the letters read as type, with the corners cut round and the `x`'s diagonals in pairs
|
|
7
|
+
* of pixels, so they read as lines. Eight pixels tall, the `i`'s dot
|
|
8
|
+
* and the `b`'s ascender above an x-height of five.
|
|
9
|
+
*/
|
|
10
|
+
const WORDMARK = [
|
|
11
|
+
" ▄ █ ",
|
|
12
|
+
" ▄▄▄▄▄▄ ▄ ▄▄ ▄▄ █▄▄▄▄ ",
|
|
13
|
+
"▀▄▄▄▄▄ █ ▀█▄█▀ █ ▀▄",
|
|
14
|
+
"▄▄▄▄▄▄▀ █ ▄█▀ ▀█▄ █▄▄▄▄▀ ",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
const WIDTH = Math.max(...WORDMARK.map((row) => row.length))
|
|
18
|
+
|
|
19
|
+
/** The wordmark in Sixb's blues, palest to deepest across the name, as the website's hero fills. */
|
|
20
|
+
export function Logo({ colors }: { readonly colors: TuiColors }) {
|
|
21
|
+
const blues = colors.wordmark
|
|
22
|
+
const tint = (column: number) =>
|
|
23
|
+
blues[Math.min(blues.length - 1, Math.floor((column / WIDTH) * blues.length))] ?? colors.blue
|
|
24
|
+
// One text, not a row each: a column of rows drew one row past its own box.
|
|
25
|
+
return (
|
|
26
|
+
<text wrapMode="none" flexShrink={0}>
|
|
27
|
+
{WORDMARK.flatMap((row, index) => [
|
|
28
|
+
...runs(row).map((run) => (
|
|
29
|
+
<span key={`${index}-${run.start}`} fg={tint(run.start)}>
|
|
30
|
+
{run.text}
|
|
31
|
+
</span>
|
|
32
|
+
)),
|
|
33
|
+
index < WORDMARK.length - 1 ? <span key={`${index}-break`}>{"\n"}</span> : null,
|
|
34
|
+
])}
|
|
35
|
+
</text>
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A row cut where its color steps: one span per stretch of a single blue. */
|
|
40
|
+
function runs(row: string): { readonly start: number; readonly text: string }[] {
|
|
41
|
+
const steps = 4
|
|
42
|
+
const out: { start: number; text: string }[] = []
|
|
43
|
+
for (let start = 0; start < row.length; ) {
|
|
44
|
+
const step = Math.floor((start / WIDTH) * steps)
|
|
45
|
+
let end = start
|
|
46
|
+
while (end < row.length && Math.floor((end / WIDTH) * steps) === step) end += 1
|
|
47
|
+
out.push({ start, text: row.slice(start, end) })
|
|
48
|
+
start = end
|
|
49
|
+
}
|
|
50
|
+
return out
|
|
51
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises"
|
|
3
|
+
import { dirname, join } from "node:path"
|
|
4
|
+
import { resolveConfigPath } from "../lib/profiles"
|
|
5
|
+
|
|
6
|
+
// This computer's id, which every claim of a turn names: a conversation that runs on its owner's
|
|
7
|
+
// computer runs on one of them, and only that one claims its turns. Kept beside the CLI's sign-in,
|
|
8
|
+
// which the agent's commands never read. Not a secret: it tells your own computers apart.
|
|
9
|
+
|
|
10
|
+
const COMPUTER_FILE = () => join(dirname(resolveConfigPath()), "computer.json")
|
|
11
|
+
|
|
12
|
+
const named = new Map<string, Promise<string>>()
|
|
13
|
+
|
|
14
|
+
/** This computer's id: named once, the first time `sixb` runs here, and kept. */
|
|
15
|
+
export function thisComputerId(): Promise<string> {
|
|
16
|
+
const path = COMPUTER_FILE()
|
|
17
|
+
let id = named.get(path)
|
|
18
|
+
if (!id) {
|
|
19
|
+
id = readOrName(path)
|
|
20
|
+
named.set(path, id)
|
|
21
|
+
// A failure is not kept: the next call tries again.
|
|
22
|
+
id.catch(() => named.delete(path))
|
|
23
|
+
}
|
|
24
|
+
return id
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function readOrName(path: string): Promise<string> {
|
|
28
|
+
const stored = await read(path)
|
|
29
|
+
if (stored) return stored
|
|
30
|
+
|
|
31
|
+
const id = `cmp_${randomUUID()}`
|
|
32
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 })
|
|
33
|
+
try {
|
|
34
|
+
await writeFile(path, `${JSON.stringify({ id }, null, 2)}\n`, { mode: 0o600, flag: "wx" })
|
|
35
|
+
return id
|
|
36
|
+
} catch (error) {
|
|
37
|
+
// Another sixb named this computer first: its name is the one.
|
|
38
|
+
const raced = await read(path)
|
|
39
|
+
if (raced) return raced
|
|
40
|
+
throw new Error(
|
|
41
|
+
`[SixbCLI] Could not keep this computer's id in ${path}: remove the file, and sixb names the computer again.`,
|
|
42
|
+
{ cause: error }
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function read(path: string): Promise<string | undefined> {
|
|
48
|
+
try {
|
|
49
|
+
const stored = JSON.parse(await readFile(path, "utf8")) as { readonly id?: unknown }
|
|
50
|
+
return typeof stored.id === "string" && stored.id.trim() ? stored.id : undefined
|
|
51
|
+
} catch {
|
|
52
|
+
return undefined
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { hostname } from "node:os"
|
|
2
|
+
import {
|
|
3
|
+
createSixbClient,
|
|
4
|
+
getAuthSession,
|
|
5
|
+
getProjectInfo,
|
|
6
|
+
getSixbSessionAccessToken,
|
|
7
|
+
type SixbClient,
|
|
8
|
+
type SixbSessionTokens,
|
|
9
|
+
startSixbDeviceLogin,
|
|
10
|
+
} from "@sixb/client"
|
|
11
|
+
import { normalizeApiUrl } from "../lib/api-client"
|
|
12
|
+
import {
|
|
13
|
+
profileSessionStore,
|
|
14
|
+
type ResolvedProfile,
|
|
15
|
+
resolveProfile,
|
|
16
|
+
updateConfig,
|
|
17
|
+
} from "../lib/profiles"
|
|
18
|
+
|
|
19
|
+
/** A project the app talks to, as the person signed in to it. */
|
|
20
|
+
export interface Connection {
|
|
21
|
+
readonly apiUrl: string
|
|
22
|
+
readonly client: SixbClient
|
|
23
|
+
readonly project: { readonly id: string; readonly timeZone: string; readonly locale: string }
|
|
24
|
+
/** Who is signed in; absent where the project runs without sign-in. */
|
|
25
|
+
readonly user?: { readonly id: string; readonly email: string; readonly name: string }
|
|
26
|
+
/** The person's own credential, as an `Authorization` header, read fresh for each use. */
|
|
27
|
+
authorization(): Promise<string>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Why the app cannot connect yet, and where it would. */
|
|
31
|
+
export interface SignedOut {
|
|
32
|
+
readonly apiUrl: string
|
|
33
|
+
readonly profile?: string
|
|
34
|
+
readonly reason: "signed-out" | "session-ended" | "unreachable"
|
|
35
|
+
readonly message?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Connect as the CLI's current profile, as every `sixb` command does. */
|
|
39
|
+
export async function connect(input: {
|
|
40
|
+
readonly apiUrl?: string
|
|
41
|
+
readonly profile?: string
|
|
42
|
+
readonly token?: string
|
|
43
|
+
}): Promise<Connection | SignedOut> {
|
|
44
|
+
let resolved: ResolvedProfile
|
|
45
|
+
try {
|
|
46
|
+
resolved = await resolveProfile(input)
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return { apiUrl: input.apiUrl ?? "", reason: "session-ended", message: errorText(error) }
|
|
49
|
+
}
|
|
50
|
+
const profile = resolved.profile
|
|
51
|
+
const viaSession = resolved.tokenSource === "session" && profile !== undefined
|
|
52
|
+
const client = createSixbClient({
|
|
53
|
+
baseUrl: resolved.apiUrl,
|
|
54
|
+
auth: viaSession
|
|
55
|
+
? { kind: "session", store: profileSessionStore(profile) }
|
|
56
|
+
: resolved.token
|
|
57
|
+
? { kind: "bearer", token: resolved.token }
|
|
58
|
+
: { kind: "none" },
|
|
59
|
+
})
|
|
60
|
+
const authorization = viaSession
|
|
61
|
+
? async () => {
|
|
62
|
+
const token = await getSixbSessionAccessToken({
|
|
63
|
+
baseUrl: resolved.apiUrl,
|
|
64
|
+
store: profileSessionStore(profile),
|
|
65
|
+
})
|
|
66
|
+
return token ? `Bearer ${token}` : ""
|
|
67
|
+
}
|
|
68
|
+
: async () => (resolved.token ? `Bearer ${resolved.token}` : "")
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
const [project, session] = await Promise.all([
|
|
72
|
+
getProjectInfo({ client, throwOnError: true }),
|
|
73
|
+
getAuthSession({ client, throwOnError: true }),
|
|
74
|
+
])
|
|
75
|
+
const signedIn = session.data.authenticated ? session.data.user : undefined
|
|
76
|
+
if (!session.data.authenticated && session.data.authEnabled) {
|
|
77
|
+
return {
|
|
78
|
+
apiUrl: resolved.apiUrl,
|
|
79
|
+
...(profile === undefined ? {} : { profile }),
|
|
80
|
+
reason: "signed-out",
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
apiUrl: resolved.apiUrl,
|
|
85
|
+
client,
|
|
86
|
+
project: project.data,
|
|
87
|
+
...(signedIn
|
|
88
|
+
? {
|
|
89
|
+
user: {
|
|
90
|
+
id: signedIn.id,
|
|
91
|
+
email: signedIn.email,
|
|
92
|
+
name: signedIn.displayName ?? signedIn.email,
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
: {}),
|
|
96
|
+
authorization,
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
const status = (error as { status?: unknown }).status
|
|
100
|
+
return {
|
|
101
|
+
apiUrl: resolved.apiUrl,
|
|
102
|
+
...(profile === undefined ? {} : { profile }),
|
|
103
|
+
reason: status === 401 || status === 403 ? "signed-out" : "unreachable",
|
|
104
|
+
message: errorText(error),
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A sign-in in progress: show the code, open the page, then wait for the person to approve it. */
|
|
110
|
+
export interface PendingSignIn {
|
|
111
|
+
readonly apiUrl: string
|
|
112
|
+
readonly userCode: string
|
|
113
|
+
readonly url: string
|
|
114
|
+
complete(signal: AbortSignal): Promise<void>
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Sign this computer in to `apiUrl` with the device flow, as `sixb login` does, and keep the
|
|
119
|
+
* session on a profile named after the project.
|
|
120
|
+
*/
|
|
121
|
+
export async function startSignIn(apiUrl: string): Promise<PendingSignIn> {
|
|
122
|
+
const baseUrl = normalizeApiUrl(apiUrl)
|
|
123
|
+
const login = await startSixbDeviceLogin({ baseUrl, clientName: `sixb on ${hostname()}` })
|
|
124
|
+
return {
|
|
125
|
+
apiUrl: baseUrl,
|
|
126
|
+
userCode: login.userCode,
|
|
127
|
+
url: login.verificationUriComplete,
|
|
128
|
+
async complete(signal) {
|
|
129
|
+
const session: SixbSessionTokens = await login.complete({ signal })
|
|
130
|
+
const client = createSixbClient({
|
|
131
|
+
baseUrl,
|
|
132
|
+
auth: { kind: "bearer", token: session.accessToken },
|
|
133
|
+
})
|
|
134
|
+
const { data: project } = await getProjectInfo({ client, throwOnError: true })
|
|
135
|
+
await updateConfig((config) => ({
|
|
136
|
+
version: 1,
|
|
137
|
+
currentProfile: project.id,
|
|
138
|
+
profiles: {
|
|
139
|
+
...config.profiles,
|
|
140
|
+
[project.id]: { apiUrl: baseUrl, projectId: project.id, session },
|
|
141
|
+
},
|
|
142
|
+
}))
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function errorText(error: unknown): string {
|
|
148
|
+
return error instanceof Error ? error.message : String(error)
|
|
149
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises"
|
|
2
|
+
import { dirname, join } from "node:path"
|
|
3
|
+
import { resolveConfigPath } from "../lib/profiles"
|
|
4
|
+
|
|
5
|
+
// Which folder each conversation that runs on this computer works in. A conversation's turns run
|
|
6
|
+
// only there: opened from another folder, it says where it lives instead of working on the wrong
|
|
7
|
+
// files. Kept on this computer, beside the CLI's profiles.
|
|
8
|
+
|
|
9
|
+
const FOLDERS_FILE = () => join(dirname(resolveConfigPath()), "conversations.json")
|
|
10
|
+
|
|
11
|
+
type Folders = Record<string, string>
|
|
12
|
+
|
|
13
|
+
async function load(): Promise<Folders> {
|
|
14
|
+
try {
|
|
15
|
+
const stored = JSON.parse(await readFile(FOLDERS_FILE(), "utf8")) as unknown
|
|
16
|
+
return stored && typeof stored === "object" && !Array.isArray(stored) ? (stored as Folders) : {}
|
|
17
|
+
} catch {
|
|
18
|
+
return {}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The folder a conversation works in on this computer; undefined when it started elsewhere. */
|
|
23
|
+
export async function folderOf(threadId: string): Promise<string | undefined> {
|
|
24
|
+
return (await load())[threadId]
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Keep `folder` as the one a conversation works in on this computer. */
|
|
28
|
+
export async function rememberFolder(threadId: string, folder: string): Promise<void> {
|
|
29
|
+
const folders = await load()
|
|
30
|
+
folders[threadId] = folder
|
|
31
|
+
await writeFile(FOLDERS_FILE(), `${JSON.stringify(folders, null, 2)}\n`, { mode: 0o600 }).catch(
|
|
32
|
+
() => {}
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The conversations that work in `folder` on this computer. */
|
|
37
|
+
export async function conversationsIn(folder: string): Promise<readonly string[]> {
|
|
38
|
+
return Object.entries(await load()).flatMap(([threadId, path]) =>
|
|
39
|
+
path === folder ? [threadId] : []
|
|
40
|
+
)
|
|
41
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import type { ApprovalOutcome, Part } from "./parts"
|
|
2
|
+
|
|
3
|
+
type ToolPart = Extract<Part, { readonly kind: "tool" }>
|
|
4
|
+
|
|
5
|
+
/** A tool call as one line: what it did, then what it acted on. */
|
|
6
|
+
export interface ToolLine {
|
|
7
|
+
readonly verb: string
|
|
8
|
+
readonly detail: string
|
|
9
|
+
/** A few lines of what came back, when they say something the line does not. */
|
|
10
|
+
readonly preview?: readonly string[]
|
|
11
|
+
/** A short outcome beside the line: `exit 1`, `3 lines`. */
|
|
12
|
+
readonly note?: string
|
|
13
|
+
readonly failed?: boolean
|
|
14
|
+
/** A call the API held for the person's approval: neither done nor failed. */
|
|
15
|
+
readonly held?: boolean
|
|
16
|
+
/** A shell command, shown whole as the call's panel. */
|
|
17
|
+
readonly command?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const PREVIEW_LINES = 4
|
|
21
|
+
|
|
22
|
+
const NO_OUTCOMES: ReadonlyMap<string, ApprovalOutcome> = new Map()
|
|
23
|
+
|
|
24
|
+
/** `outcomes` says how the held calls ended, by approval id: a decided call is no longer held. */
|
|
25
|
+
export function toolLine(
|
|
26
|
+
part: ToolPart,
|
|
27
|
+
outcomes: ReadonlyMap<string, ApprovalOutcome> = NO_OUTCOMES
|
|
28
|
+
): ToolLine {
|
|
29
|
+
const input = record(part.input) ?? partialInput(part.inputText)
|
|
30
|
+
const output = record(part.output)
|
|
31
|
+
const failed = part.state === "error"
|
|
32
|
+
const error = failed ? { note: firstLine(part.error ?? "failed"), failed } : {}
|
|
33
|
+
|
|
34
|
+
switch (part.name) {
|
|
35
|
+
case "bash": {
|
|
36
|
+
const command = text(input?.command)
|
|
37
|
+
const exitCode = typeof output?.exitCode === "number" ? output.exitCode : undefined
|
|
38
|
+
const stdout = text(output?.stdout)
|
|
39
|
+
const stderr = text(output?.stderr)
|
|
40
|
+
const shown = exitCode !== undefined && exitCode !== 0 && stderr ? stderr : stdout || stderr
|
|
41
|
+
const heldIds = heldApprovalIds(stdout)
|
|
42
|
+
const decided = heldIds.flatMap((id) => outcomes.get(id) ?? [])
|
|
43
|
+
const held = heldIds.length > decided.length
|
|
44
|
+
return {
|
|
45
|
+
verb: "bash",
|
|
46
|
+
command: command.trim(),
|
|
47
|
+
detail: firstLine(command) + (command.includes("\n") ? " …" : ""),
|
|
48
|
+
...(shown && heldIds.length === 0 ? { preview: lines(shown) } : {}),
|
|
49
|
+
...(held
|
|
50
|
+
? { note: "held for your approval", held }
|
|
51
|
+
: heldIds.length > 0
|
|
52
|
+
? decidedLine(decided)
|
|
53
|
+
: exitCode !== undefined && exitCode !== 0
|
|
54
|
+
? { note: `exit ${exitCode}`, failed: true }
|
|
55
|
+
: {}),
|
|
56
|
+
...error,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
case "read": {
|
|
60
|
+
const lineCount = typeof output?.endLine === "number" && typeof output.startLine === "number"
|
|
61
|
+
return {
|
|
62
|
+
verb: "read",
|
|
63
|
+
detail: text(input?.path),
|
|
64
|
+
...(lineCount
|
|
65
|
+
? { note: `lines ${output?.startLine}–${output?.endLine}${output?.truncated ? "+" : ""}` }
|
|
66
|
+
: {}),
|
|
67
|
+
...error,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
case "view_file":
|
|
71
|
+
return { verb: "view", detail: text(input?.path), ...error }
|
|
72
|
+
case "publish_artifact":
|
|
73
|
+
case "publish_artifact_version":
|
|
74
|
+
return {
|
|
75
|
+
verb: "publish",
|
|
76
|
+
detail: text(input?.title) || text(input?.path) || text(input?.artifactId),
|
|
77
|
+
...(output?.artifactId ? { note: `artifact:${text(output.artifactId)}` } : {}),
|
|
78
|
+
...error,
|
|
79
|
+
}
|
|
80
|
+
case "spawn_agent":
|
|
81
|
+
return {
|
|
82
|
+
verb: "agent",
|
|
83
|
+
detail: [text(input?.key), firstLine(text(input?.task))].filter(Boolean).join(" "),
|
|
84
|
+
...error,
|
|
85
|
+
}
|
|
86
|
+
case "wait_agent": {
|
|
87
|
+
const runIds = Array.isArray(input?.runIds) ? input.runIds.length : 0
|
|
88
|
+
return {
|
|
89
|
+
verb: "wait",
|
|
90
|
+
detail: runIds === 1 ? "for its agent" : `for ${runIds || "its"} agents`,
|
|
91
|
+
...error,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
case "message_agent":
|
|
95
|
+
return {
|
|
96
|
+
verb: "message",
|
|
97
|
+
detail: [text(input?.key), firstLine(text(input?.text))].filter(Boolean).join(" "),
|
|
98
|
+
...error,
|
|
99
|
+
}
|
|
100
|
+
case "ask_parent":
|
|
101
|
+
return {
|
|
102
|
+
verb: "ask",
|
|
103
|
+
detail: firstLine(text(input?.question) || text(input?.text)),
|
|
104
|
+
...error,
|
|
105
|
+
}
|
|
106
|
+
default:
|
|
107
|
+
return {
|
|
108
|
+
verb: part.name,
|
|
109
|
+
detail: compact(part.input ?? part.inputText),
|
|
110
|
+
...(part.state === "done" && part.output !== undefined
|
|
111
|
+
? { preview: lines(compact(part.output)) }
|
|
112
|
+
: {}),
|
|
113
|
+
...error,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The approvals a `bash` output says it waits for. `sixb` prints the API's answer to a call it held
|
|
120
|
+
* as one JSON line naming the approval, but the output is the model's to shape: as in the web
|
|
121
|
+
* chat, only a line that names a record counts, wherever it sits.
|
|
122
|
+
*/
|
|
123
|
+
function heldApprovalIds(stdout: string): string[] {
|
|
124
|
+
return stdout.split("\n").flatMap((line) => {
|
|
125
|
+
const trimmed = line.trim()
|
|
126
|
+
if (!trimmed.startsWith("{")) return []
|
|
127
|
+
|
|
128
|
+
const parsed = record(parseJson(trimmed))
|
|
129
|
+
if (parsed?.status !== "awaiting_approval") return []
|
|
130
|
+
|
|
131
|
+
const approval = record(parsed.approval)
|
|
132
|
+
return typeof approval?.id === "string" && approval.id ? [approval.id] : []
|
|
133
|
+
})
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** How decided calls ended, as the web chat's approval card names it. */
|
|
137
|
+
function decidedLine(outcomes: readonly ApprovalOutcome[]): Pick<ToolLine, "note" | "failed"> {
|
|
138
|
+
const ended = outcomes.map(outcomeNote)
|
|
139
|
+
const failed = ended.some((entry) => entry.failed)
|
|
140
|
+
return {
|
|
141
|
+
note: [...new Set(ended.map((entry) => entry.note))].join(", "),
|
|
142
|
+
...(failed ? { failed } : {}),
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function outcomeNote(outcome: ApprovalOutcome): { readonly note: string; readonly failed?: true } {
|
|
147
|
+
switch (outcome.status) {
|
|
148
|
+
case "approved": {
|
|
149
|
+
const ran = outcome.outcome
|
|
150
|
+
if (ran?.kind === "error" && ran.error.code === "agent.approval_authority_lost") {
|
|
151
|
+
return { note: "approved, but it didn't run", failed: true }
|
|
152
|
+
}
|
|
153
|
+
if (ran?.kind === "error" || (ran?.kind === "action-run" && ran.status === "failed")) {
|
|
154
|
+
return { note: "approved, but it failed", failed: true }
|
|
155
|
+
}
|
|
156
|
+
return { note: "approved" }
|
|
157
|
+
}
|
|
158
|
+
case "denied":
|
|
159
|
+
return { note: "denied" }
|
|
160
|
+
case "superseded":
|
|
161
|
+
return { note: "set aside" }
|
|
162
|
+
case "cancelled":
|
|
163
|
+
return { note: "stopped" }
|
|
164
|
+
case "expired":
|
|
165
|
+
return { note: "expired" }
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function parseJson(value: string): unknown {
|
|
170
|
+
try {
|
|
171
|
+
return JSON.parse(value)
|
|
172
|
+
} catch {
|
|
173
|
+
return undefined
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function record(value: unknown): Record<string, unknown> | undefined {
|
|
178
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
179
|
+
? (value as Record<string, unknown>)
|
|
180
|
+
: undefined
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The input so far, while the model still writes it, read where it already parses. */
|
|
184
|
+
function partialInput(inputText: string | undefined): Record<string, unknown> | undefined {
|
|
185
|
+
if (!inputText) return undefined
|
|
186
|
+
try {
|
|
187
|
+
return record(JSON.parse(inputText))
|
|
188
|
+
} catch {
|
|
189
|
+
const command = /"(?:command|path|task|key)"\s*:\s*"((?:[^"\\]|\\.)*)/.exec(inputText)
|
|
190
|
+
return command ? { command: command[1], path: command[1], task: command[1] } : undefined
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function text(value: unknown): string {
|
|
195
|
+
return typeof value === "string"
|
|
196
|
+
? value
|
|
197
|
+
: value === undefined || value === null
|
|
198
|
+
? ""
|
|
199
|
+
: String(value)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function firstLine(value: string): string {
|
|
203
|
+
return value.trim().split("\n")[0]?.trim() ?? ""
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function lines(value: string): string[] {
|
|
207
|
+
const all = value.replace(/\s+$/, "").split("\n")
|
|
208
|
+
return all.length > PREVIEW_LINES
|
|
209
|
+
? [...all.slice(0, PREVIEW_LINES), `… ${plural(all.length - PREVIEW_LINES, "more line")}`]
|
|
210
|
+
: all
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** A value on one line, for a tool with no line of its own. */
|
|
214
|
+
function compact(value: unknown): string {
|
|
215
|
+
if (value === undefined) return ""
|
|
216
|
+
if (typeof value === "string") return firstLine(value)
|
|
217
|
+
const json = JSON.stringify(value)
|
|
218
|
+
return json && json.length > 160 ? `${json.slice(0, 157)}…` : (json ?? "")
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** `1m 04s`, `12.4s`, `820ms`. */
|
|
222
|
+
export function duration(ms: number): string {
|
|
223
|
+
if (ms < 1_000) return `${Math.round(ms)}ms`
|
|
224
|
+
if (ms < 59_950) return `${(ms / 1_000).toFixed(1)}s`
|
|
225
|
+
const total = Math.round(ms / 1_000)
|
|
226
|
+
return `${Math.floor(total / 60)}m ${String(total % 60).padStart(2, "0")}s`
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** A path with the home directory as `~`, and its middle elided past `max` characters. */
|
|
230
|
+
export function shortPath(path: string, home: string, max = 48): string {
|
|
231
|
+
const homed = path.startsWith(home) ? `~${path.slice(home.length)}` : path
|
|
232
|
+
if (homed.length <= max) return homed
|
|
233
|
+
const parts = homed.split("/")
|
|
234
|
+
const last = parts.at(-1) ?? ""
|
|
235
|
+
const head = parts.slice(0, 2).join("/")
|
|
236
|
+
const elided = `${head}/…/${last}`
|
|
237
|
+
if (elided.length <= max) return elided
|
|
238
|
+
return last.length + 2 <= max ? `…/${last}` : ""
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** `1 more line`, `3 more lines`. */
|
|
242
|
+
export function plural(count: number, noun: string): string {
|
|
243
|
+
return `${count} ${noun}${count === 1 ? "" : "s"}`
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* A path as the person reads it: relative to the folder the agent works in, and the run's own
|
|
248
|
+
* files, kept outside it, as `run/…`.
|
|
249
|
+
*/
|
|
250
|
+
export function friendlyPath(text: string, folder?: string): string {
|
|
251
|
+
let shown = text.replace(
|
|
252
|
+
/\/(?:private\/)?(?:var|tmp)\/[^\s"']*?\/sixb-agent\/[^/\s"']+\//g,
|
|
253
|
+
"run/"
|
|
254
|
+
)
|
|
255
|
+
if (folder) {
|
|
256
|
+
const prefixes = [folder, folder.replace(/^\/private/, "")].map((prefix) => `${prefix}/`)
|
|
257
|
+
for (const prefix of prefixes) shown = shown.split(prefix).join("")
|
|
258
|
+
}
|
|
259
|
+
return shown
|
|
260
|
+
}
|