@cmodjs/core 8.10.3 → 8.11.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/docs/commands.md +1 -1
- package/docs/session.md +5 -3
- package/mod.d.ts +11 -1
- package/mod.d.ts.map +1 -1
- package/package.json +1 -1
- package/records.d.ts +1 -1
- package/records.js +1 -1
- package/runtime/conversations.d.ts.map +1 -1
- package/runtime/conversations.js +92 -75
- package/runtime/interfaces.d.ts +9 -3
- package/runtime/interfaces.d.ts.map +1 -1
- package/runtime/interfaces.js +102 -31
- package/runtime/sessions.js +1 -1
- package/session/conversation.d.ts +1 -0
- package/session/conversation.d.ts.map +1 -1
- package/session/session.d.ts +5 -0
- package/session/session.d.ts.map +1 -1
- package/src/mod.ts +3 -3
- package/src/records.ts +1 -1
- package/src/runtime/conversations.ts +87 -74
- package/src/runtime/interfaces.ts +101 -30
- package/src/runtime/sessions.ts +1 -1
- package/src/session/conversation.ts +1 -0
- package/src/session/session.ts +2 -1
- package/src/testing/fake-serve.ts +2 -0
- package/src/utils/text.ts +12 -0
- package/testing/fake-serve.d.ts.map +1 -1
- package/testing/fake-serve.js +2 -0
- package/utils/text.d.ts +2 -0
- package/utils/text.d.ts.map +1 -1
- package/utils/text.js +9 -0
package/src/mod.ts
CHANGED
|
@@ -119,8 +119,8 @@ declare module 'claude-code' {
|
|
|
119
119
|
interface CmodInterfaces {
|
|
120
120
|
'cmod/Sessions': {
|
|
121
121
|
methods: {
|
|
122
|
-
sessions(scope?: { project?: string; everyProject?: boolean; running?: boolean; since?: string }): { id: string; title: string; color?: string; project: string; cwd: string; model: string; startedAt: string; lastAt: string; forkedFrom?: string; isRunning: boolean; agentType?: string; transcriptPath?: string; visitedAt?: string; device?: { id: string; name: string }; status?: 'busy' | 'shell' | 'idle' | 'waiting'; waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }; contextPercent?: number; lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string } }[]
|
|
123
|
-
session(session: string): { id: string; title: string; color?: string; project: string; cwd: string; model: string; startedAt: string; lastAt: string; forkedFrom?: string; isRunning: boolean; agentType?: string; transcriptPath?: string; visitedAt?: string; device?: { id: string; name: string }; status?: 'busy' | 'shell' | 'idle' | 'waiting'; waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }; contextPercent?: number; lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string } }
|
|
122
|
+
sessions(scope?: { project?: string; everyProject?: boolean; running?: boolean; since?: string }): { id: string; title: string; color?: string; project: string; cwd: string; model: string; startedAt: string; lastAt: string; forkedFrom?: string; isRunning: boolean; agentType?: string; transcriptPath?: string; visitedAt?: string; device?: { id: string; name: string }; status?: 'busy' | 'shell' | 'idle' | 'waiting'; waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }; contextPercent?: number; lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string; reply?: string }; openCall?: { tool: string; summary: string } }[]
|
|
123
|
+
session(session: string): { id: string; title: string; color?: string; project: string; cwd: string; model: string; startedAt: string; lastAt: string; forkedFrom?: string; isRunning: boolean; agentType?: string; transcriptPath?: string; visitedAt?: string; device?: { id: string; name: string }; status?: 'busy' | 'shell' | 'idle' | 'waiting'; waitingFor?: { dialog: string; tool?: string; input?: Record<string, unknown>; question?: string }; contextPercent?: number; lastTurn?: { reason: 'answered' | 'stopped' | 'failed'; at: string; failure?: string; reply?: string }; openCall?: { tool: string; summary: string } }
|
|
124
124
|
search(text: string, options?: { project?: string; everyProject?: boolean; running?: boolean; since?: string; limit?: number; before?: { session: string; id: string }; record?: { session?: string; task?: string; turn?: string; context?: number; isAgent?: true } }): unknown[]
|
|
125
125
|
read(session: string, part: 'messages' | 'turns' | 'contexts' | 'tasks' | 'goal' | 'usage' | 'state' | 'mail', options?: { project?: string; task?: string; turn?: string; context?: number; after?: string; last?: number; all?: true }): unknown
|
|
126
126
|
send(session: string, message: { type: 'prompt' | 'note'; text: string; as?: 'person'; from?: { session: string; title: string }; replyTo?: number }, context?: { project?: string; caller?: string; task?: string }): { id?: number; state: 'queued' | 'delivered' }
|
|
@@ -134,7 +134,7 @@ declare module 'claude-code' {
|
|
|
134
134
|
visit(session: string, at?: string): void
|
|
135
135
|
delete(session: string, input?: null, context?: { project?: string; caller?: string }): void
|
|
136
136
|
}
|
|
137
|
-
events: { Change: { readonly session: string; readonly part: 'visit' | 'running' | 'tasks' | 'goal' | 'status' | 'deleted' | 'messages' | 'mail' } }
|
|
137
|
+
events: { Change: { readonly session: string; readonly part: 'visit' | 'running' | 'tasks' | 'goal' | 'status' | 'title' | 'deleted' | 'messages' | 'mail' } }
|
|
138
138
|
}
|
|
139
139
|
'cmod/Store': {
|
|
140
140
|
methods: { changed(part: 'plugins' | 'widgets' | 'buildFailures' | 'preferences' | 'links'): void }
|
package/src/records.ts
CHANGED
|
@@ -511,7 +511,7 @@ function readHotkeys(hotkeys: unknown): Steps {
|
|
|
511
511
|
return { hotkeys: checkHotkeys('package.json "cmod"', hotkeys as Record<string, HotkeyInit>) }
|
|
512
512
|
}
|
|
513
513
|
|
|
514
|
-
export const coreVersion = '8.
|
|
514
|
+
export const coreVersion = '8.11.0'
|
|
515
515
|
|
|
516
516
|
export const oldestCmodVersion = '8.0.0'
|
|
517
517
|
|
|
@@ -11,12 +11,12 @@ import { TaskEnd, TaskStart } from '../session/goal/task.js'
|
|
|
11
11
|
import { receivedMatcher } from '../utils/matcher.js'
|
|
12
12
|
import { resolve } from '../vendor.js'
|
|
13
13
|
import { ifMissing } from '../utils/paths.js'
|
|
14
|
-
import { messageOf } from '../utils/text.js'
|
|
14
|
+
import { callSummary, firstLine, messageOf, questionOf } from '../utils/text.js'
|
|
15
15
|
import { beforeAnswerKeys, foldBefore, installBefore, relayOf, type Decide, type Place } from './before.js'
|
|
16
16
|
import type { Claude } from './claude.js'
|
|
17
17
|
import { cmodPluginName, isNotInstalled } from './dependencies.js'
|
|
18
18
|
import { MissingMachineId } from './device.js'
|
|
19
|
-
import { interfaceListeners, interfaceMethods, providerActs, providerPeer, serveProviders, sessionActs, sessionsInterface, uiInterface, type HeldStatus, type Implementation, type Provided, type KeptListen, type ProvidedHeard, type ProviderIdentity, type SessionsChange, type HeldSession, storeInterface, methodName, listeningOf, type StoreChange, type StorePart } from './interfaces.js'
|
|
19
|
+
import { findSession, interfaceListeners, interfaceMethods, providerActs, providerPeer, serveProviders, sessionActs, sessionsInterface, uiInterface, type HeldStatus, type Implementation, type Provided, type KeptListen, type ProvidedHeard, type ProviderIdentity, type SessionsChange, type HeldSession, storeInterface, methodName, listeningOf, type StoreChange, type StorePart } from './interfaces.js'
|
|
20
20
|
import { runCmod } from './programs.js'
|
|
21
21
|
import { addressCall, connectRpc, isConnectionGone, RpcError, rpcCodes, type RpcClient } from './rpc.js'
|
|
22
22
|
import type { RoutedHook, Router } from './router.js'
|
|
@@ -75,7 +75,7 @@ const noneKept: Kept = { subscribed: {}, watched: {}, provided: [] }
|
|
|
75
75
|
|
|
76
76
|
type Registered = { remove(): void }
|
|
77
77
|
|
|
78
|
-
type OtherProvider = { readonly identity: ProviderIdentity; readonly peer: Pick<RpcClient, 'request'
|
|
78
|
+
type OtherProvider = { readonly identity: ProviderIdentity; readonly peer: Pick<RpcClient, 'request'> }
|
|
79
79
|
|
|
80
80
|
export function serveConversations({
|
|
81
81
|
claude,
|
|
@@ -125,16 +125,19 @@ export function serveConversations({
|
|
|
125
125
|
let contextPercent: number | undefined
|
|
126
126
|
let endedTurn: HeldStatus['lastTurn']
|
|
127
127
|
let failedWith: string | undefined
|
|
128
|
+
let repliedWith: string | undefined
|
|
128
129
|
const openCalls = new Map<string, ToolCallArgs>()
|
|
129
130
|
const tellStatus = () => {
|
|
130
131
|
if (session === '') return
|
|
131
|
-
|
|
132
|
+
const open = [...openCalls.values()].at(-1)
|
|
133
|
+
const openCall = open === undefined ? undefined : { tool: open.name, summary: callSummary(open.input) }
|
|
134
|
+
local.client.notify(sessionActs.status, { session, status: waitingFor !== undefined ? 'waiting' : isWorking ? 'busy' : 'idle', ...(waitingFor === undefined ? {} : { waitingFor }), ...(contextPercent === undefined ? {} : { contextPercent }), ...(endedTurn === undefined ? {} : { lastTurn: endedTurn }), ...(openCall === undefined ? {} : { openCall }) } satisfies HeldStatus & { readonly session: string })
|
|
132
135
|
}
|
|
133
136
|
const waitFor = (callId: string) => {
|
|
134
137
|
const call = openCalls.get(callId)
|
|
135
138
|
if (call === undefined) return
|
|
136
|
-
const question = call.name === questionTool ? (call.input
|
|
137
|
-
waitingFor =
|
|
139
|
+
const question = call.name === questionTool ? questionOf(call.input) : undefined
|
|
140
|
+
waitingFor = question !== undefined ? { dialog: permissionPrompt, question } : { dialog: permissionPrompt, tool: call.name, input: call.input }
|
|
138
141
|
waitingOn = callId
|
|
139
142
|
tellStatus()
|
|
140
143
|
}
|
|
@@ -143,6 +146,7 @@ export function serveConversations({
|
|
|
143
146
|
if (isStarted) {
|
|
144
147
|
endedTurn = undefined
|
|
145
148
|
failedWith = undefined
|
|
149
|
+
repliedWith = undefined
|
|
146
150
|
}
|
|
147
151
|
waitingFor = undefined
|
|
148
152
|
waitingOn = undefined
|
|
@@ -160,7 +164,7 @@ export function serveConversations({
|
|
|
160
164
|
if (e.agentId !== undefined) return completed
|
|
161
165
|
const reason = e.reason === 'answer' ? 'answered' : e.reason === 'aborted' ? 'stopped' : 'failed'
|
|
162
166
|
const failure = e.reason === 'refusal' ? (e.refusal.explanation ?? e.refusal.category ?? 'Claude refused to answer.') : failedWith
|
|
163
|
-
endedTurn = { reason, at: new Date().toISOString(), ...(reason === 'failed' && failure !== undefined ? { failure } : {}) }
|
|
167
|
+
endedTurn = { reason, at: new Date().toISOString(), ...(reason === 'failed' && failure !== undefined ? { failure } : {}), ...(reason === 'answered' && repliedWith !== undefined ? { reply: repliedWith } : {}) }
|
|
164
168
|
turnMoved(false)
|
|
165
169
|
return completed
|
|
166
170
|
})
|
|
@@ -168,6 +172,7 @@ export function serveConversations({
|
|
|
168
172
|
if (e.agentId !== undefined) return next(e)
|
|
169
173
|
openCalls.set(e.tool_use_id, { name: e.tool, input: toolInputOf(e) })
|
|
170
174
|
if (e.tool === questionTool) waitFor(e.tool_use_id)
|
|
175
|
+
else tellStatus()
|
|
171
176
|
try {
|
|
172
177
|
return await next(e)
|
|
173
178
|
} finally {
|
|
@@ -175,8 +180,8 @@ export function serveConversations({
|
|
|
175
180
|
if (waitingOn === e.tool_use_id) {
|
|
176
181
|
waitingFor = undefined
|
|
177
182
|
waitingOn = undefined
|
|
178
|
-
tellStatus()
|
|
179
183
|
}
|
|
184
|
+
tellStatus()
|
|
180
185
|
}
|
|
181
186
|
})
|
|
182
187
|
router.add('classic.Notification', async (e, next) => {
|
|
@@ -216,7 +221,10 @@ export function serveConversations({
|
|
|
216
221
|
router.add('classic.SubagentStop', async (e, next) => endTurn({ reason: 'answer', ...(e.last_assistant_message === undefined ? {} : { answer: e.last_assistant_message }) }, e.agent_id)(await next(e), next.signal.aborted))
|
|
217
222
|
router.add('classic.Stop', async (e, next) => {
|
|
218
223
|
const below = await next(e)
|
|
219
|
-
if (below.block === undefined && !next.signal.aborted)
|
|
224
|
+
if (below.block === undefined && !next.signal.aborted) {
|
|
225
|
+
repliedWith = firstLine(e.last_assistant_message ?? '') || undefined
|
|
226
|
+
void answerTask().catch((error: unknown) => report('task', 'Could not record that this session answered its task', error))
|
|
227
|
+
}
|
|
220
228
|
return below
|
|
221
229
|
})
|
|
222
230
|
|
|
@@ -283,7 +291,7 @@ export function serveConversations({
|
|
|
283
291
|
await served.replay()
|
|
284
292
|
await held.replay(client)
|
|
285
293
|
storeChanged(undefined)
|
|
286
|
-
await reconcileTasks()
|
|
294
|
+
await reconcileTasks({ atConnect: true })
|
|
287
295
|
},
|
|
288
296
|
onError: (error) => {
|
|
289
297
|
if (!isUnreached) claude.ui.warn('serve:connect', `cmod: this session waits for cmod serve: ${messageOf(error)}`)
|
|
@@ -315,47 +323,41 @@ export function serveConversations({
|
|
|
315
323
|
changed: () => void saveListeners().catch((error: unknown) => report('listeners', 'Could not keep the provider listeners of this session', error)),
|
|
316
324
|
})
|
|
317
325
|
|
|
318
|
-
const
|
|
319
|
-
const keyOf = ({ mod, program, device }: ProviderIdentity) => `${device?.id ?? ''}\n${mod}\n${program ?? ''}`
|
|
320
|
-
const otherProviders = async () => {
|
|
321
|
-
const found = await local.client.request<ProviderIdentity[]>(providerActs.providers, { interface: sessionsInterface })
|
|
322
|
-
const keys = new Set(found.map(keyOf))
|
|
323
|
-
for (const [key, other] of others) {
|
|
324
|
-
if (keys.has(key)) continue
|
|
325
|
-
others.delete(key)
|
|
326
|
-
other.change.remove()
|
|
327
|
-
}
|
|
328
|
-
for (const identity of found) if (!others.has(keyOf(identity))) others.set(keyOf(identity), followProvider(identity))
|
|
329
|
-
return [...others.values()]
|
|
330
|
-
}
|
|
326
|
+
const otherProviders = async (): Promise<OtherProvider[]> => (await local.client.request<ProviderIdentity[]>(providerActs.providers, { interface: sessionsInterface })).map((identity) => ({ identity, peer: providerPeer(local.client, identity) }))
|
|
331
327
|
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
})
|
|
337
|
-
|
|
328
|
+
const taskWatches = new Map<string, { readonly runner: OtherProvider; readonly watch: Registered }>()
|
|
329
|
+
const watchTask = (task: string, runner: OtherProvider) => {
|
|
330
|
+
if (taskWatches.has(task)) return
|
|
331
|
+
const listening = { ...listeningOf(sessionsInterface, 'on', 'Change', [task], { part: 'running' }), provider: runner.identity }
|
|
332
|
+
taskWatches.set(task, { runner, watch: held.listen(local.client, listening, () => followTask(runner, task)) })
|
|
333
|
+
void followTask(runner, task)
|
|
334
|
+
}
|
|
335
|
+
const unwatchTask = (task: string) => {
|
|
336
|
+
taskWatches.get(task)?.watch.remove()
|
|
337
|
+
taskWatches.delete(task)
|
|
338
338
|
}
|
|
339
339
|
|
|
340
340
|
const providers = async () => [local.client, ...(await otherProviders()).map((other) => other.peer)]
|
|
341
341
|
|
|
342
|
-
const isNotHeld = (error: unknown) => error instanceof RpcError && error.code === rpcCodes.notHeld
|
|
342
|
+
const isNotHeld = (error: unknown): error is RpcError => error instanceof RpcError && error.code === rpcCodes.notHeld
|
|
343
|
+
|
|
344
|
+
const sessionsAsk = (client: Pick<RpcClient, 'request'>) => (method: string, args: readonly unknown[]) => client.request(`${sessionsInterface}.${method}`, args)
|
|
343
345
|
|
|
344
|
-
const firstHolding = async <Value>(ask: (
|
|
345
|
-
let last
|
|
346
|
-
for (const
|
|
346
|
+
const firstHolding = async <Item, Value>(items: readonly Item[], ask: (item: Item) => Promise<Value>): Promise<Value> => {
|
|
347
|
+
let last = new RpcError(rpcCodes.notHeld, 'No sessions provider answers.')
|
|
348
|
+
for (const item of items) {
|
|
347
349
|
try {
|
|
348
|
-
return await ask(
|
|
350
|
+
return await ask(item)
|
|
349
351
|
} catch (error) {
|
|
350
352
|
if (!isNotHeld(error)) throw error
|
|
351
353
|
last = error
|
|
352
354
|
}
|
|
353
355
|
}
|
|
354
|
-
throw last
|
|
356
|
+
throw last
|
|
355
357
|
}
|
|
356
358
|
|
|
357
|
-
const read = <Value>(id: string, part: string, options: Request = {}): Promise<Value> =>
|
|
358
|
-
firstHolding((client) => client.request<Value>(`${sessionsInterface}.read`, [id, part, { project: project(), ...options }])).catch((error: unknown) => {
|
|
359
|
+
const read = async <Value>(id: string, part: string, options: Request = {}): Promise<Value> =>
|
|
360
|
+
firstHolding(await providers(), (client) => client.request<Value>(`${sessionsInterface}.read`, [id, part, { project: project(), ...options }])).catch((error: unknown) => {
|
|
359
361
|
throw isNotHeld(error) ? new Error(`no session ${id} on any of your devices.`) : error
|
|
360
362
|
})
|
|
361
363
|
|
|
@@ -365,7 +367,7 @@ export function serveConversations({
|
|
|
365
367
|
return lists.flat().filter((each) => !seen.has(each.id) && Boolean(seen.add(each.id)))
|
|
366
368
|
}
|
|
367
369
|
|
|
368
|
-
const actOn = (target: string, action: Act, input: unknown, context: Request) => firstHolding((client) => client.request(`${sessionsInterface}.${action}`, [target, input, { project: project(), ...context }]))
|
|
370
|
+
const actOn = async (target: string, action: Act, input: unknown, context: Request) => firstHolding(await providers(), (client) => client.request(`${sessionsInterface}.${action}`, [target, input, { project: project(), ...context }]))
|
|
369
371
|
|
|
370
372
|
let color: SessionColor = { session: '' }
|
|
371
373
|
let onColor: (told: SessionColor) => void = () => undefined
|
|
@@ -400,6 +402,7 @@ export function serveConversations({
|
|
|
400
402
|
const dispatch = (changes: readonly SessionChange[]) => {
|
|
401
403
|
if (session !== '' && changes.some((change) => change.type === TaskStart || change.type === TaskEnd)) local.client.notify(sessionActs.changed, { session, part: 'tasks' })
|
|
402
404
|
for (const change of changes) {
|
|
405
|
+
if (change.type === TaskEnd) unwatchTask(change.task.id)
|
|
403
406
|
if ((change.type === TurnStart || change.type === TurnEnd) && change.turn.prompt.task === undefined) lastTurn = change.turn
|
|
404
407
|
if (change.type === Message && !liveIds.has(change.message.id)) {
|
|
405
408
|
live.push(change.message)
|
|
@@ -553,7 +556,7 @@ export function serveConversations({
|
|
|
553
556
|
|
|
554
557
|
const listenToServe = () => {
|
|
555
558
|
serveChanges?.remove()
|
|
556
|
-
serveChanges = held.listen(local.client,
|
|
559
|
+
serveChanges = held.listen(local.client, listeningOf(sessionsInterface, 'on', 'Change', [session], { part: ['tasks', 'goal'] }), (change: SessionsChange) => {
|
|
557
560
|
if (change.part === 'tasks') void reconcileTasks()
|
|
558
561
|
if (change.part === 'goal') void rereadGoal()
|
|
559
562
|
})
|
|
@@ -563,6 +566,7 @@ export function serveConversations({
|
|
|
563
566
|
if (id === session) return
|
|
564
567
|
const previous = session
|
|
565
568
|
session = id
|
|
569
|
+
for (const task of taskWatches.keys()) unwatchTask(task)
|
|
566
570
|
live = []
|
|
567
571
|
liveIds.clear()
|
|
568
572
|
waitingRows.clear()
|
|
@@ -595,40 +599,53 @@ export function serveConversations({
|
|
|
595
599
|
)
|
|
596
600
|
listening = Promise.all([restored, holding]).then(() => undefined)
|
|
597
601
|
listenToServe()
|
|
598
|
-
void starting.then(reconcileTasks)
|
|
602
|
+
void starting.then(() => reconcileTasks())
|
|
599
603
|
if (heard.size === 0 && watched.size === 0 && served.listened().length === 0) return
|
|
600
604
|
void Promise.all([previous === '' ? undefined : onListenerFiles(() => claude.fs.write(`${listeners}/${previous}.json`, JSON.stringify(noneKept))), saveListeners()]).catch((error: unknown) => report('follow', 'Kept telling the mods about the session from before', error))
|
|
601
605
|
}
|
|
602
606
|
|
|
603
|
-
const
|
|
607
|
+
const openTasks = () => liveTasks().filter((task) => task.isBackground === true && task.closedAt === undefined)
|
|
608
|
+
const openTask = (id: string) => openTasks().find((task) => task.id === id)
|
|
609
|
+
|
|
610
|
+
const reconcileTasks = async ({ atConnect = false } = {}) => {
|
|
604
611
|
if (session === '') return
|
|
605
612
|
await starting
|
|
606
|
-
|
|
607
|
-
if (open.length === 0) return
|
|
613
|
+
if (openTasks().length === 0) return
|
|
608
614
|
const saved = await read<TaskData[]>(session, 'tasks').catch((error: unknown) => (report('tasks', 'Could not read whether its background tasks ended', error), []))
|
|
609
|
-
for (const task of saved)
|
|
610
|
-
|
|
611
|
-
|
|
615
|
+
for (const task of saved) if (task.closedAt !== undefined && openTask(task.id) !== undefined) dispatch(conversation.endTask(task, task.closedAt))
|
|
616
|
+
const open = openTasks().filter((task) => atConnect || !taskWatches.has(task.id))
|
|
617
|
+
const others = open.every((task) => taskWatches.has(task.id)) ? [] : await otherProviders()
|
|
618
|
+
for (const task of open) {
|
|
619
|
+
const watched = taskWatches.get(task.id)
|
|
620
|
+
if (watched !== undefined) await followTask(watched.runner, task.id)
|
|
621
|
+
else await findTask(task.id, others).catch((error: unknown) => report('tasks', 'Could not read whether its tasks on another device ended', error))
|
|
612
622
|
}
|
|
613
|
-
for (const { peer } of await otherProviders()) await closeEndedOn(peer, open.map((task) => task.id)).catch((error: unknown) => report('tasks', 'Could not read whether its tasks on another device ended', error))
|
|
614
623
|
}
|
|
615
624
|
|
|
616
|
-
const
|
|
617
|
-
await
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
return
|
|
622
|
-
})
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
625
|
+
const findTask = async (id: string, others: readonly OtherProvider[]) => {
|
|
626
|
+
if ((await findSession(sessionsAsk(local.client), id)) !== undefined) return
|
|
627
|
+
for (const runner of others) {
|
|
628
|
+
const found = await findSession(sessionsAsk(runner.peer), id).catch((error: unknown) => {
|
|
629
|
+
report('tasks', `Could not ask ${runner.identity.mod} whether it runs task ${id}`, error)
|
|
630
|
+
return undefined
|
|
631
|
+
})
|
|
632
|
+
if (found !== undefined) return watchTask(id, runner)
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const followTask = async (runner: OtherProvider, id: string) => {
|
|
637
|
+
try {
|
|
638
|
+
await starting
|
|
639
|
+
if (openTask(id) === undefined) return
|
|
640
|
+
const found = await findSession(sessionsAsk(runner.peer), id)
|
|
641
|
+
if (found === undefined) return unwatchTask(id)
|
|
642
|
+
const task = openTask(id)
|
|
643
|
+
if (task === undefined || found.isRunning) return
|
|
629
644
|
const closedAt = new Date().toISOString()
|
|
630
645
|
await claude.fs.write(`${backgroundTasks}/${session}/${task.id}.json`, JSON.stringify({ ...task, closedAt } satisfies TaskData))
|
|
631
646
|
dispatch(conversation.endTask(task, closedAt))
|
|
647
|
+
} catch (error) {
|
|
648
|
+
report('tasks', `Could not read whether a task ${runner.identity.mod} runs ended`, error)
|
|
632
649
|
}
|
|
633
650
|
}
|
|
634
651
|
|
|
@@ -743,7 +760,7 @@ export function serveConversations({
|
|
|
743
760
|
|
|
744
761
|
const exitNow = async () => {
|
|
745
762
|
endsAfterTurn = false
|
|
746
|
-
const { isStopped } =
|
|
763
|
+
const { isStopped } = await local.client.request<{ readonly isStopped: boolean }>(sessionActs.stop, {})
|
|
747
764
|
if (!isStopped) await claude.command.run({ command: 'exit' })
|
|
748
765
|
}
|
|
749
766
|
const endSoon = () => claude.clock.after(0, () => void exitNow().catch((error: unknown) => report('end', `Could not end session ${session}`, error)))
|
|
@@ -825,12 +842,14 @@ export function serveConversations({
|
|
|
825
842
|
const elsewhere: TaskStartArgs = { ...task, ...owner, title: answer?.title ?? task.title, prompt: answer?.prompt ?? task.prompt }
|
|
826
843
|
const others = await otherProviders()
|
|
827
844
|
const onDevice = others.filter(({ identity }) => identity.device !== undefined && (identity.device.name === task.device || identity.device.id === task.device))
|
|
828
|
-
const
|
|
829
|
-
|
|
845
|
+
const candidates = onDevice.length > 0 ? onDevice : others
|
|
846
|
+
const { opened, runner } = await firstHolding(candidates, async (runner) => ({ opened: await runner.peer.request<TaskData>(`${sessionsInterface}.startTask`, [session, elsewhere, { project: project() }]), runner })).catch((error: unknown) => {
|
|
847
|
+
throw isNotHeld(error) ? new Error(`no mod starts sessions on ${task.device}, so the task did not start. Leave device out to start it on ${here.name}.`) : error
|
|
830
848
|
})
|
|
831
849
|
const started: TaskData = { ...asked, id: opened.id, title: elsewhere.title }
|
|
832
850
|
await claude.fs.write(`${backgroundTasks}/${session}/${started.id}.json`, JSON.stringify(started))
|
|
833
851
|
dispatch(conversation.startTask(started))
|
|
852
|
+
watchTask(started.id, runner)
|
|
834
853
|
return started
|
|
835
854
|
}
|
|
836
855
|
}
|
|
@@ -910,17 +929,11 @@ export function serveConversations({
|
|
|
910
929
|
const fields = (request: Request, keys: readonly string[]) => Object.fromEntries(keys.flatMap((key) => (request[key] === undefined ? [] : [[key, request[key]]])))
|
|
911
930
|
|
|
912
931
|
const sessionNamed = async (id: string): Promise<SessionData> => {
|
|
913
|
-
const
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
return listed
|
|
919
|
-
})
|
|
920
|
-
const found = await firstHolding(lookedUp).catch((error: unknown) => {
|
|
921
|
-
throw isNotHeld(error) ? new Error(`no session ${id} on any of your devices.`) : error
|
|
922
|
-
})
|
|
923
|
-
return found.id === session ? { ...found, isRunning: true } : found
|
|
932
|
+
for (const client of await providers()) {
|
|
933
|
+
const found = await findSession(sessionsAsk(client), id)
|
|
934
|
+
if (found !== undefined) return found.id === session ? { ...found, isRunning: true } : found
|
|
935
|
+
}
|
|
936
|
+
throw new Error(`no session ${id} on any of your devices.`)
|
|
924
937
|
}
|
|
925
938
|
|
|
926
939
|
const methods: Readonly<Record<string, (request: Request, caller: string | undefined) => Promise<unknown>>> = {
|