@sixb/cli 0.1.25 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/commands/agent.d.ts +16 -0
- package/dist/commands/agent.d.ts.map +1 -0
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/check.d.ts.map +1 -1
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/dev-runner.d.ts +0 -1
- package/dist/commands/dev-runner.d.ts.map +1 -1
- package/dist/commands/dev.d.ts.map +1 -1
- package/dist/commands/env.d.ts +31 -0
- package/dist/commands/env.d.ts.map +1 -0
- 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 +41 -6
- package/dist/lib/command-line.d.ts.map +1 -1
- package/dist/lib/deploy-release.d.ts +2 -0
- package/dist/lib/deploy-release.d.ts.map +1 -1
- package/dist/lib/deploy-source.d.ts +2 -0
- package/dist/lib/deploy-source.d.ts.map +1 -1
- package/dist/lib/env-config.d.ts +17 -0
- package/dist/lib/env-config.d.ts.map +1 -0
- package/dist/lib/env-file.d.ts +16 -0
- package/dist/lib/env-file.d.ts.map +1 -0
- package/dist/lib/github-ci.d.ts.map +1 -1
- package/dist/lib/hidden-prompt.d.ts +19 -0
- package/dist/lib/hidden-prompt.d.ts.map +1 -0
- 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 +27 -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 +8 -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/context.d.ts +13 -0
- package/dist/tui/context.d.ts.map +1 -0
- package/dist/tui/format.d.ts +118 -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/markdown.d.ts +18 -0
- package/dist/tui/markdown.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 +74 -0
- package/dist/tui/parts.d.ts.map +1 -0
- package/dist/tui/runner.d.ts +75 -0
- package/dist/tui/runner.d.ts.map +1 -0
- package/dist/tui/settings.d.ts +56 -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 +42 -0
- package/dist/tui/ui.d.ts.map +1 -0
- package/dist/ui/deploy-screens.d.ts +3 -1
- package/dist/ui/deploy-screens.d.ts.map +1 -1
- package/dist/ui/env-screens.d.ts +37 -0
- package/dist/ui/env-screens.d.ts.map +1 -0
- package/package.json +19 -17
- package/src/commands/agent.ts +344 -0
- package/src/commands/api.ts +2 -1
- package/src/commands/check.ts +5 -1
- package/src/commands/deploy.ts +29 -3
- package/src/commands/dev-runner.ts +3 -5
- package/src/commands/dev.ts +3 -0
- package/src/commands/env.ts +422 -0
- package/src/commands/worker-group.ts +0 -4
- package/src/commands/worker.ts +0 -4
- package/src/index.tsx +95 -5
- package/src/lib/command-line.ts +111 -16
- package/src/lib/deploy-release.ts +3 -11
- package/src/lib/deploy-source.ts +10 -0
- package/src/lib/env-config.ts +96 -0
- package/src/lib/env-file.ts +104 -0
- package/src/lib/github-ci.ts +3 -1
- package/src/lib/hidden-prompt.ts +97 -0
- package/src/lib/runtime.ts +0 -2
- package/src/lib/worker-registry.ts +0 -2
- package/src/tui/app.tsx +1334 -0
- package/src/tui/brand.tsx +51 -0
- package/src/tui/computer.ts +91 -0
- package/src/tui/connection.ts +149 -0
- package/src/tui/context.tsx +149 -0
- package/src/tui/format.ts +477 -0
- package/src/tui/index.tsx +364 -0
- package/src/tui/markdown.ts +91 -0
- package/src/tui/palette.tsx +183 -0
- package/src/tui/parts.ts +274 -0
- package/src/tui/runner.ts +256 -0
- package/src/tui/settings.ts +176 -0
- package/src/tui/slash.ts +32 -0
- package/src/tui/theme.ts +111 -0
- package/src/tui/transcript.tsx +637 -0
- package/src/tui/ui.tsx +177 -0
- package/src/ui/deploy-screens.ts +12 -1
- package/src/ui/env-screens.ts +81 -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
package/src/tui/parts.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentRunStreamEvent,
|
|
3
|
+
ListAgentThreadMessagesResponses,
|
|
4
|
+
ModelUiChunk,
|
|
5
|
+
} from "@sixb/client"
|
|
6
|
+
|
|
7
|
+
// What a message shows, whether it was stored or is still streaming: the stored transcript and the
|
|
8
|
+
// live run both reduce to these parts, so one view renders either.
|
|
9
|
+
|
|
10
|
+
export type StoredMessage = ListAgentThreadMessagesResponses[200]["messages"][number]
|
|
11
|
+
|
|
12
|
+
export type ToolState = "running" | "done" | "error"
|
|
13
|
+
|
|
14
|
+
export type Part =
|
|
15
|
+
| { readonly kind: "text"; readonly text: string }
|
|
16
|
+
| { readonly kind: "reasoning"; readonly text: string; readonly done: boolean }
|
|
17
|
+
| {
|
|
18
|
+
readonly kind: "tool"
|
|
19
|
+
readonly id: string
|
|
20
|
+
readonly name: string
|
|
21
|
+
readonly state: ToolState
|
|
22
|
+
readonly input?: unknown
|
|
23
|
+
/** The input while the model still writes it, as JSON text. */
|
|
24
|
+
readonly inputText?: string
|
|
25
|
+
readonly output?: unknown
|
|
26
|
+
readonly error?: string
|
|
27
|
+
}
|
|
28
|
+
| { readonly kind: "file"; readonly name: string; readonly mediaType?: string }
|
|
29
|
+
|
|
30
|
+
type ApprovalOutcomePart = Extract<StoredMessage["parts"][number], { type: "approval-outcome" }>
|
|
31
|
+
|
|
32
|
+
/** How a held call ended, as the message that resumed or followed its turn told the agent. */
|
|
33
|
+
export interface ApprovalOutcome {
|
|
34
|
+
readonly approvalId: string
|
|
35
|
+
readonly status: ApprovalOutcomePart["status"]
|
|
36
|
+
readonly outcome?: ApprovalOutcomePart["outcome"]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ShownMessage {
|
|
40
|
+
readonly id: string
|
|
41
|
+
readonly role: "user" | "assistant"
|
|
42
|
+
readonly runId: string | null
|
|
43
|
+
readonly parts: readonly Part[]
|
|
44
|
+
/** How the held calls before it ended: not shown, but the calls' own lines say it. */
|
|
45
|
+
readonly outcomes: readonly ApprovalOutcome[]
|
|
46
|
+
/** Its run condensed the conversation first; `contextFull` when the provider refused it whole. */
|
|
47
|
+
readonly compaction?: { readonly contextFull: boolean }
|
|
48
|
+
readonly createdAt: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A stored message as shown: what is context for the agent alone stays hidden. */
|
|
52
|
+
export function shownMessage(message: StoredMessage): ShownMessage | null {
|
|
53
|
+
if (message.role === "system") return null
|
|
54
|
+
|
|
55
|
+
const outcomes = message.parts.flatMap((part): ApprovalOutcome[] =>
|
|
56
|
+
part.type === "approval-outcome"
|
|
57
|
+
? [
|
|
58
|
+
{
|
|
59
|
+
approvalId: part.approvalId,
|
|
60
|
+
status: part.status,
|
|
61
|
+
...(part.outcome === undefined ? {} : { outcome: part.outcome }),
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
: []
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const parts = message.parts.flatMap((part): Part[] => {
|
|
68
|
+
switch (part.type) {
|
|
69
|
+
case "text":
|
|
70
|
+
return part.text.trim() ? [{ kind: "text", text: part.text }] : []
|
|
71
|
+
case "reasoning":
|
|
72
|
+
return part.text.trim() ? [{ kind: "reasoning", text: part.text, done: true }] : []
|
|
73
|
+
case "tool-call":
|
|
74
|
+
return [
|
|
75
|
+
part.state === "output-available"
|
|
76
|
+
? {
|
|
77
|
+
kind: "tool",
|
|
78
|
+
id: part.toolCallId,
|
|
79
|
+
name: part.toolName,
|
|
80
|
+
state: "done",
|
|
81
|
+
input: part.input,
|
|
82
|
+
output: part.output,
|
|
83
|
+
}
|
|
84
|
+
: {
|
|
85
|
+
kind: "tool",
|
|
86
|
+
id: part.toolCallId,
|
|
87
|
+
name: part.toolName,
|
|
88
|
+
state: "error",
|
|
89
|
+
input: part.input,
|
|
90
|
+
error: part.errorText,
|
|
91
|
+
},
|
|
92
|
+
]
|
|
93
|
+
case "file":
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
kind: "file",
|
|
97
|
+
name: part.fileRef.fileName ?? "file",
|
|
98
|
+
...(part.fileRef.mediaType === undefined ? {} : { mediaType: part.fileRef.mediaType }),
|
|
99
|
+
},
|
|
100
|
+
]
|
|
101
|
+
default:
|
|
102
|
+
return []
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
// A turn resumed after approvals starts from a message the person never wrote: it shows
|
|
106
|
+
// nothing, and is kept for how its held calls ended.
|
|
107
|
+
if (message.role === "user" && parts.length === 0 && outcomes.length === 0) return null
|
|
108
|
+
return {
|
|
109
|
+
id: message.id,
|
|
110
|
+
role: message.role,
|
|
111
|
+
runId: message.runId,
|
|
112
|
+
parts,
|
|
113
|
+
outcomes,
|
|
114
|
+
...(message.compaction
|
|
115
|
+
? { compaction: { contextFull: message.compaction.reason === "overflow" } }
|
|
116
|
+
: {}),
|
|
117
|
+
createdAt: message.createdAt,
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** How each held call of a transcript ended, by its approval's id. */
|
|
122
|
+
export function approvalOutcomes(
|
|
123
|
+
messages: readonly ShownMessage[]
|
|
124
|
+
): ReadonlyMap<string, ApprovalOutcome> {
|
|
125
|
+
return new Map(
|
|
126
|
+
messages.flatMap((message) => message.outcomes.map((outcome) => [outcome.approvalId, outcome]))
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A run as it streams: its parts so far, and whether it still works. */
|
|
131
|
+
export interface LiveRun {
|
|
132
|
+
readonly runId: string
|
|
133
|
+
readonly parts: readonly Part[]
|
|
134
|
+
readonly keys: readonly string[]
|
|
135
|
+
readonly step: number
|
|
136
|
+
/** How many parts came before the current step: a step asked again drops the ones after. */
|
|
137
|
+
readonly stepStart: number
|
|
138
|
+
readonly working: boolean
|
|
139
|
+
readonly compacting: boolean
|
|
140
|
+
/** The condensing follows a request the provider refused as too long: the turn goes on after. */
|
|
141
|
+
readonly contextFull: boolean
|
|
142
|
+
/** Set once the run's answer is stored: the transcript then shows it. */
|
|
143
|
+
readonly finalized: boolean
|
|
144
|
+
readonly finished?: { readonly status: string; readonly error?: string }
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function liveRun(runId: string): LiveRun {
|
|
148
|
+
return {
|
|
149
|
+
runId,
|
|
150
|
+
parts: [],
|
|
151
|
+
keys: [],
|
|
152
|
+
step: 0,
|
|
153
|
+
stepStart: 0,
|
|
154
|
+
working: true,
|
|
155
|
+
compacting: false,
|
|
156
|
+
contextFull: false,
|
|
157
|
+
finalized: false,
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function reduceLive(state: LiveRun, event: AgentRunStreamEvent): LiveRun {
|
|
162
|
+
switch (event.type) {
|
|
163
|
+
case "agent.run.started":
|
|
164
|
+
return { ...state, working: true }
|
|
165
|
+
case "agent.compaction.started":
|
|
166
|
+
return { ...state, compacting: true, contextFull: event.reason === "overflow" }
|
|
167
|
+
case "agent.compaction.completed":
|
|
168
|
+
case "agent.compaction.failed":
|
|
169
|
+
return { ...state, compacting: false, contextFull: false }
|
|
170
|
+
case "agent.ui.chunk":
|
|
171
|
+
return reduceChunk(state, event.chunk)
|
|
172
|
+
case "agent.message.finalized":
|
|
173
|
+
return { ...state, finalized: true }
|
|
174
|
+
case "agent.run.finished":
|
|
175
|
+
return {
|
|
176
|
+
...state,
|
|
177
|
+
working: false,
|
|
178
|
+
compacting: false,
|
|
179
|
+
contextFull: false,
|
|
180
|
+
finished: {
|
|
181
|
+
status: event.status,
|
|
182
|
+
...(event.error ? { error: event.error.message } : {}),
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
default:
|
|
186
|
+
return state
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function reduceChunk(state: LiveRun, chunk: ModelUiChunk): LiveRun {
|
|
191
|
+
switch (chunk.type) {
|
|
192
|
+
case "start-step":
|
|
193
|
+
// A step asked again drops what its first call streamed.
|
|
194
|
+
if (chunk.retry) {
|
|
195
|
+
return {
|
|
196
|
+
...state,
|
|
197
|
+
parts: state.parts.slice(0, state.stepStart),
|
|
198
|
+
keys: state.keys.slice(0, state.stepStart),
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { ...state, step: state.step + 1, stepStart: state.parts.length }
|
|
202
|
+
case "text-delta": {
|
|
203
|
+
if (!chunk.delta) return state
|
|
204
|
+
const key = `text#${state.step}#${chunk.id}`
|
|
205
|
+
if (!state.keys.includes(key) && !chunk.delta.trim()) return state
|
|
206
|
+
return upsert(state, key, { kind: "text", text: chunk.delta }, (part) =>
|
|
207
|
+
part.kind === "text" ? { ...part, text: part.text + chunk.delta } : part
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
case "reasoning-delta":
|
|
211
|
+
return upsert(
|
|
212
|
+
state,
|
|
213
|
+
`reasoning#${state.step}#${chunk.id}`,
|
|
214
|
+
{ kind: "reasoning", text: chunk.delta, done: false },
|
|
215
|
+
(part) => (part.kind === "reasoning" ? { ...part, text: part.text + chunk.delta } : part)
|
|
216
|
+
)
|
|
217
|
+
case "reasoning-end":
|
|
218
|
+
return upsert(
|
|
219
|
+
state,
|
|
220
|
+
`reasoning#${state.step}#${chunk.id}`,
|
|
221
|
+
{ kind: "reasoning", text: "", done: true },
|
|
222
|
+
(part) => (part.kind === "reasoning" ? { ...part, done: true } : part)
|
|
223
|
+
)
|
|
224
|
+
case "tool-input-start":
|
|
225
|
+
case "tool-input-delta":
|
|
226
|
+
case "tool-input-available":
|
|
227
|
+
case "tool-input-error":
|
|
228
|
+
case "tool-output-available":
|
|
229
|
+
case "tool-output-error": {
|
|
230
|
+
const created: Part = {
|
|
231
|
+
kind: "tool",
|
|
232
|
+
id: chunk.toolCallId,
|
|
233
|
+
name: "toolName" in chunk && chunk.toolName ? chunk.toolName : "tool",
|
|
234
|
+
state: "running",
|
|
235
|
+
}
|
|
236
|
+
return upsert(state, `tool#${chunk.toolCallId}`, created, (part) =>
|
|
237
|
+
part.kind === "tool" ? applyToolChunk(part, chunk) : part
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
default:
|
|
241
|
+
return state
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
type ToolPart = Extract<Part, { readonly kind: "tool" }>
|
|
246
|
+
|
|
247
|
+
function applyToolChunk(part: ToolPart, chunk: ModelUiChunk): ToolPart {
|
|
248
|
+
const named = "toolName" in chunk && chunk.toolName ? { ...part, name: chunk.toolName } : part
|
|
249
|
+
switch (chunk.type) {
|
|
250
|
+
case "tool-input-delta":
|
|
251
|
+
return { ...named, inputText: (named.inputText ?? "") + chunk.inputTextDelta }
|
|
252
|
+
case "tool-input-available":
|
|
253
|
+
return { ...named, input: chunk.input }
|
|
254
|
+
case "tool-input-error":
|
|
255
|
+
return { ...named, state: "error", input: chunk.input, error: chunk.errorText }
|
|
256
|
+
case "tool-output-available":
|
|
257
|
+
return { ...named, state: "done", output: chunk.output }
|
|
258
|
+
case "tool-output-error":
|
|
259
|
+
return { ...named, state: "error", error: chunk.errorText }
|
|
260
|
+
default:
|
|
261
|
+
return named
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Append a part under a new key, or change the one already there, keeping their order. */
|
|
266
|
+
function upsert(state: LiveRun, key: string, created: Part, update: (part: Part) => Part): LiveRun {
|
|
267
|
+
const index = state.keys.indexOf(key)
|
|
268
|
+
if (index === -1) {
|
|
269
|
+
return { ...state, parts: [...state.parts, created], keys: [...state.keys, key] }
|
|
270
|
+
}
|
|
271
|
+
const parts = state.parts.slice()
|
|
272
|
+
parts[index] = update(parts[index] as Part)
|
|
273
|
+
return { ...state, parts }
|
|
274
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import { homedir, tmpdir } from "node:os"
|
|
3
|
+
import { dirname, join } from "node:path"
|
|
4
|
+
import { ComputerRunner } from "@sixb/agent"
|
|
5
|
+
import {
|
|
6
|
+
type GetAgentThreadResponses,
|
|
7
|
+
getAgentRun,
|
|
8
|
+
getAgentThread,
|
|
9
|
+
holdAgentComputerSession,
|
|
10
|
+
releaseAgentComputerSession,
|
|
11
|
+
} from "@sixb/client"
|
|
12
|
+
import type { ConversationAgentRunRecord } from "@sixb/core/storage"
|
|
13
|
+
import { LocalSandboxFactory } from "@sixb/sandboxes-local"
|
|
14
|
+
import { resolveConfigPath } from "../lib/profiles"
|
|
15
|
+
import { thisComputer, thisComputerId } from "./computer"
|
|
16
|
+
import type { Connection } from "./connection"
|
|
17
|
+
|
|
18
|
+
/** How long a closed terminal waits for a turn running here to record its end. */
|
|
19
|
+
const HANG_UP_WAIT_MS = 10_000
|
|
20
|
+
/** How often an open `sixb` renews its session, which lapses a minute after its last renewal. */
|
|
21
|
+
const SESSION_RENEW_MS = 20_000
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* What the agent's commands never read on this computer, whatever the folder: the CLI's own
|
|
25
|
+
* sign-in, and the credentials SSH and AWS keep.
|
|
26
|
+
*/
|
|
27
|
+
export function deniedPaths(): readonly string[] {
|
|
28
|
+
return [dirname(resolveConfigPath()), join(homedir(), ".ssh"), join(homedir(), ".aws")]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* This computer as a runner: it claims its owner's turns from the project and runs them in the
|
|
33
|
+
* folder `sixb` was opened in, one at a time per turn, each until it ends or is stopped. While its
|
|
34
|
+
* session is open, the project knows `sixb` is open here, and the turns sent from elsewhere to the
|
|
35
|
+
* conversations of this folder run here too.
|
|
36
|
+
*/
|
|
37
|
+
export class LocalRunner {
|
|
38
|
+
private readonly runner: ComputerRunner
|
|
39
|
+
private readonly running = new Map<
|
|
40
|
+
string,
|
|
41
|
+
{ readonly stop: AbortController; readonly done: Promise<boolean> }
|
|
42
|
+
>()
|
|
43
|
+
private readonly elsewhereListeners = new Set<(run: ConversationAgentRunRecord) => void>()
|
|
44
|
+
private session: { readonly id: string; readonly timer: ReturnType<typeof setInterval> } | null =
|
|
45
|
+
null
|
|
46
|
+
/** The session's last call: its release follows a renewal, which would open it again. */
|
|
47
|
+
private lastCall: Promise<void> = Promise.resolve()
|
|
48
|
+
|
|
49
|
+
constructor(
|
|
50
|
+
private readonly connection: Connection,
|
|
51
|
+
/** The folder `sixb` was opened in: its real path, as the project keeps it. */
|
|
52
|
+
readonly folder: string,
|
|
53
|
+
private readonly onFailure: (error: unknown) => void
|
|
54
|
+
) {
|
|
55
|
+
this.runner = new ComputerRunner({
|
|
56
|
+
apiUrl: connection.apiUrl,
|
|
57
|
+
authorization: () => connection.authorization(),
|
|
58
|
+
computer: thisComputer,
|
|
59
|
+
folder,
|
|
60
|
+
runFilesRoot: join(tmpdir(), "sixb-agent"),
|
|
61
|
+
onFailure,
|
|
62
|
+
openSandbox: ({ workingDirectory, runFiles, credential, signal }) =>
|
|
63
|
+
new LocalSandboxFactory({
|
|
64
|
+
readWritePaths: [runFiles],
|
|
65
|
+
denyReadPaths: deniedPaths(),
|
|
66
|
+
network: { mode: "all" },
|
|
67
|
+
}).create({
|
|
68
|
+
...(workingDirectory === undefined ? {} : { workingDirectory }),
|
|
69
|
+
network: { mode: "all" },
|
|
70
|
+
requestCredentials: [credential],
|
|
71
|
+
signal,
|
|
72
|
+
}),
|
|
73
|
+
})
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Hear each turn sent from elsewhere as it starts to run here. Returns the unsubscribe. */
|
|
77
|
+
onElsewhere(listener: (run: ConversationAgentRunRecord) => void): () => void {
|
|
78
|
+
this.elsewhereListeners.add(listener)
|
|
79
|
+
return () => this.elsewhereListeners.delete(listener)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether this computer runs `runId` now. */
|
|
83
|
+
runs(runId: string): boolean {
|
|
84
|
+
return this.running.has(runId)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Claim a queued turn and run it, unless this computer runs it already. Resolves once the turn
|
|
89
|
+
* ended here: false when it was not this computer's to run, or could not be claimed.
|
|
90
|
+
*/
|
|
91
|
+
claim(runId: string, onClaimed?: (run: ConversationAgentRunRecord) => void): Promise<boolean> {
|
|
92
|
+
const running = this.running.get(runId)
|
|
93
|
+
if (running) return running.done
|
|
94
|
+
const stop = new AbortController()
|
|
95
|
+
const done = this.runner
|
|
96
|
+
.runTurn(runId, stop.signal, onClaimed)
|
|
97
|
+
.catch((error: unknown) => {
|
|
98
|
+
this.onFailure(error)
|
|
99
|
+
return false
|
|
100
|
+
})
|
|
101
|
+
.finally(() => this.running.delete(runId))
|
|
102
|
+
this.running.set(runId, { stop, done })
|
|
103
|
+
return done
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Hold this `sixb`'s session while it is open: the conversations of this folder read as online,
|
|
108
|
+
* and the turns waiting for them are claimed. Idempotent.
|
|
109
|
+
*/
|
|
110
|
+
openSession(): void {
|
|
111
|
+
if (this.session) return
|
|
112
|
+
const id = `cs_${randomUUID()}`
|
|
113
|
+
const hold = async () => {
|
|
114
|
+
const { data, error } = await holdAgentComputerSession({
|
|
115
|
+
client: this.connection.client,
|
|
116
|
+
path: { sessionId: id },
|
|
117
|
+
body: { computer: { ...(await thisComputer()), folder: this.folder } },
|
|
118
|
+
})
|
|
119
|
+
if (error) throw error
|
|
120
|
+
// Closed meanwhile, with the setting or by quitting: what waits keeps waiting.
|
|
121
|
+
if (this.session?.id !== id) return
|
|
122
|
+
for (const runId of data?.waiting ?? []) void this.claimElsewhere(runId)
|
|
123
|
+
}
|
|
124
|
+
// A missed renewal is retried at the next; the session lasts three of them.
|
|
125
|
+
const renew = () => {
|
|
126
|
+
this.lastCall = hold().catch((error: unknown) =>
|
|
127
|
+
console.error("[SixbCLI] Session renewal failed.", error)
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
this.session = { id, timer: setInterval(renew, SESSION_RENEW_MS) }
|
|
131
|
+
renew()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Close the session: the conversations of this folder read as offline at once. */
|
|
135
|
+
async closeSession(): Promise<void> {
|
|
136
|
+
const session = this.session
|
|
137
|
+
if (session) {
|
|
138
|
+
this.session = null
|
|
139
|
+
clearInterval(session.timer)
|
|
140
|
+
// Whoever closes it next waits for this one, as quitting does after the screen let go.
|
|
141
|
+
this.lastCall = this.lastCall
|
|
142
|
+
.then(() =>
|
|
143
|
+
releaseAgentComputerSession({
|
|
144
|
+
client: this.connection.client,
|
|
145
|
+
path: { sessionId: session.id },
|
|
146
|
+
})
|
|
147
|
+
)
|
|
148
|
+
.then(
|
|
149
|
+
() => {},
|
|
150
|
+
() => {}
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
await this.lastCall
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Claim a turn sent from elsewhere when this computer runs its conversation: its owner's, on this
|
|
158
|
+
* computer and in this folder, and still queued, while the session is open. A running turn is
|
|
159
|
+
* another sixb's, or one whose sixb closed, which the API ends interrupted: claiming it again
|
|
160
|
+
* would run its commands twice.
|
|
161
|
+
*/
|
|
162
|
+
async claimIfHere(threadId: string, runId?: string): Promise<void> {
|
|
163
|
+
if (!this.session || (runId && this.running.has(runId))) return
|
|
164
|
+
const { client } = this.connection
|
|
165
|
+
const { data: thread } = await getAgentThread({ client, path: { threadId } })
|
|
166
|
+
if (!thread || !this.runsHere(thread, await thisComputerId())) return
|
|
167
|
+
|
|
168
|
+
const turn = runId ?? thread.activeRunId
|
|
169
|
+
if (!turn) return
|
|
170
|
+
const { data: run } = await getAgentRun({ client, path: { runId: turn } })
|
|
171
|
+
if (run?.status === "queued" && this.session) void this.claimElsewhere(turn)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Whether this `sixb` runs `thread`'s turns: its owner's, on this computer, in this folder. */
|
|
175
|
+
runsHere(thread: GetAgentThreadResponses[200], computerId: string): boolean {
|
|
176
|
+
return (
|
|
177
|
+
thread.runsOn === "computer" &&
|
|
178
|
+
owns(this.connection, thread) &&
|
|
179
|
+
thread.computer?.id === computerId &&
|
|
180
|
+
thread.computer.folder === this.folder
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** After a stop was asked for: the turn hears it now, not at its next lease renewal. */
|
|
185
|
+
hearStop(runId: string): void {
|
|
186
|
+
this.runner.renewNow(runId)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Whether a turn runs here now. */
|
|
190
|
+
get busy(): boolean {
|
|
191
|
+
return this.running.size > 0
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Stop every turn this computer runs, each ending interrupted with its end recorded, and close
|
|
196
|
+
* the session.
|
|
197
|
+
*/
|
|
198
|
+
async stop(): Promise<void> {
|
|
199
|
+
const running = [...this.running.values()]
|
|
200
|
+
for (const { stop } of running) stop.abort()
|
|
201
|
+
await Promise.allSettled([...running.map(({ done }) => done), this.closeSession()])
|
|
202
|
+
await this.runner.drain()
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** A turn this computer already runs joins that run, and is no news. */
|
|
206
|
+
private claimElsewhere(runId: string): Promise<boolean> {
|
|
207
|
+
return this.claim(runId, (run) => {
|
|
208
|
+
for (const listener of this.elsewhereListeners) listener(run)
|
|
209
|
+
})
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Whether the person signed in owns `thread`: with sign-in off, the system owns what it starts. */
|
|
214
|
+
export function owns(
|
|
215
|
+
connection: Pick<Connection, "user">,
|
|
216
|
+
thread: Pick<GetAgentThreadResponses[200], "ownerPrincipal">
|
|
217
|
+
): boolean {
|
|
218
|
+
const owner = thread.ownerPrincipal
|
|
219
|
+
return connection.user
|
|
220
|
+
? owner.type === "user" && owner.id === connection.user.id
|
|
221
|
+
: owner.type === "system"
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A closed terminal, or a kill, ends sixb as quitting does: the turns running here stop, each
|
|
226
|
+
* ending interrupted with its end recorded, instead of renewing their lease from a process nobody
|
|
227
|
+
* sees, and the session closes. Then `exit`, after a bounded wait. Returns the removal of the
|
|
228
|
+
* handlers.
|
|
229
|
+
*/
|
|
230
|
+
export function stopOnHangUp(
|
|
231
|
+
runner: () => Pick<LocalRunner, "stop"> | null,
|
|
232
|
+
exit: (signal: "SIGHUP" | "SIGTERM") => void
|
|
233
|
+
): () => void {
|
|
234
|
+
const hangUp = (signal: "SIGHUP" | "SIGTERM") => {
|
|
235
|
+
const running = runner()
|
|
236
|
+
if (!running) return exit(signal)
|
|
237
|
+
|
|
238
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
239
|
+
const waited = new Promise<void>((resolve) => {
|
|
240
|
+
timer = setTimeout(resolve, HANG_UP_WAIT_MS)
|
|
241
|
+
})
|
|
242
|
+
void Promise.race([running.stop(), waited]).finally(() => {
|
|
243
|
+
clearTimeout(timer)
|
|
244
|
+
exit(signal)
|
|
245
|
+
})
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const onHangUp = () => hangUp("SIGHUP")
|
|
249
|
+
const onTerminate = () => hangUp("SIGTERM")
|
|
250
|
+
process.on("SIGHUP", onHangUp)
|
|
251
|
+
process.on("SIGTERM", onTerminate)
|
|
252
|
+
return () => {
|
|
253
|
+
process.off("SIGHUP", onHangUp)
|
|
254
|
+
process.off("SIGTERM", onTerminate)
|
|
255
|
+
}
|
|
256
|
+
}
|