peachsim 0.1.2406 → 0.1.2409
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/cli/agent-note.ts +231 -0
- package/cli/cloud-client.ts +8 -3
- package/cli/cloud-dispatch.ts +1 -1
- package/cli/command-agent.ts +122 -0
- package/cli/commands/agent.ts +29 -26
- package/cli/commands/stream.ts +11 -7
- package/cli/main.ts +16 -2
- package/cli/outbound-endpoints.ts +15 -0
- package/cli/ws-bridge.ts +4 -1
- package/dist-lib/agent-daemon-client.cjs +23 -3
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +1 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +1 -1
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +1 -1
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +1 -1
- package/dist-lib/bundle-channel.cjs +205 -62
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +1 -1
- package/dist-lib/cloud-contract.cjs +20 -1
- package/dist-lib/cloud-contract.mjs +18 -1
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +1 -1
- package/dist-lib/cloud.cjs +1 -1
- package/dist-lib/cloud.mjs +1 -1
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +1 -1
- package/dist-lib/detox/index.cjs +1630 -1513
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/expo-swift-bundle.cjs +203 -60
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +1 -1
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +394 -102
- package/dist-lib/host/fetch-proxy-handler.cjs +5 -5
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +392 -272
- package/dist-lib/jump-to-source-babel.cjs +1 -1
- package/dist-lib/jump-to-source-native.cjs +1 -1
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +1 -1
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +1 -1
- package/dist-lib/metro-production-bundle.cjs +1 -1
- package/dist-lib/metro-production-bundle.mjs +1 -1
- package/dist-lib/metro.cjs +5 -5
- package/dist-lib/native-app-identity-project.cjs +3 -3
- package/dist-lib/native-app-identity-project.mjs +3 -3
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +1 -1
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +1 -1
- package/dist-lib/pdfium-raster.cjs +1 -1
- package/dist-lib/pdfium-raster.mjs +1 -1
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +1 -1
- package/dist-lib/profiles.cjs +1 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +24 -24
- package/dist-lib/public-origins-lib.json +70 -60
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +1 -1
- package/dist-lib/reap-browser.cjs +1 -1
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +1 -1
- package/dist-lib/sdk.mjs +1 -1
- package/dist-lib/skills.cjs +1199 -821
- package/dist-lib/swift-compile.cjs +197 -55
- package/dist-lib/swift-compile.mjs +197 -55
- package/dist-lib/swift-package.cjs +1 -1
- package/dist-lib/swift-package.mjs +1 -1
- package/dist-lib/swift-submit.cjs +1 -1
- package/dist-lib/swift-submit.mjs +1 -1
- package/dist-lib/swift-toolchain.cjs +1 -1
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vite-swift.cjs +213 -58
- package/dist-lib/vite.cjs +5 -5
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +1 -1
- package/dist-types/packages/peach-engine/src/remote-stream/cloud-display.d.ts +2 -2
- package/dist-types/packages/peach-globals/src/index.d.ts +1 -1
- package/dist-types/packages/peach-globals/src/replayBridgeContract.d.ts +7 -0
- package/kotlin/sdk.tar.gz +0 -0
- package/package.json +1 -1
- package/runtime-companion.json +1 -1
- package/skills/peach-setup/SKILL.md +8 -3
- package/src/agent-daemon-client.ts +35 -2
- package/src/agent-prompt.ts +68 -0
- package/src/bridge-contract.ts +4 -0
- package/src/canvas-agent-note-delivery.ts +83 -0
- package/src/cloud-contract.ts +30 -0
- package/src/host/agent-host.ts +128 -18
- package/src/swift-compile.ts +124 -66
- package/src/swift-host.ts +2 -1
- package/src/swift-objc-import.ts +99 -0
- package/src/swift-packages.ts +1 -0
- package/src/swift-shared-sdk.ts +2 -1
- package/src/xcode-header-search.ts +33 -0
|
@@ -21,12 +21,15 @@ import type {
|
|
|
21
21
|
AgentInspectListenerStatus,
|
|
22
22
|
AgentInspectMessage,
|
|
23
23
|
AgentPromptEnvelope,
|
|
24
|
+
CanvasAgentNote,
|
|
25
|
+
CanvasAgentNoteStatus,
|
|
24
26
|
} from './agent-prompt.ts'
|
|
25
27
|
import type { AgentSession, AttachedProject } from './attached-projects.ts'
|
|
26
28
|
|
|
27
29
|
export interface AgentDaemonClientOptions {
|
|
28
30
|
port?: number
|
|
29
31
|
commandTimeoutMs?: number
|
|
32
|
+
connectTimeoutMs?: number
|
|
30
33
|
/** label forwarded in logs; helps identify the daemon caller. */
|
|
31
34
|
clientLabel?: string
|
|
32
35
|
}
|
|
@@ -101,11 +104,16 @@ export class AgentDaemonClient {
|
|
|
101
104
|
this.commandTimeoutMs = opts.commandTimeoutMs ?? 15_000
|
|
102
105
|
this.ws = new WebSocket(`ws://127.0.0.1:${this.port}`)
|
|
103
106
|
this.ready = new Promise<void>((resolve, reject) => {
|
|
107
|
+
const timeout = opts.connectTimeoutMs
|
|
108
|
+
? setTimeout(() => this.ws.terminate(), opts.connectTimeoutMs)
|
|
109
|
+
: undefined
|
|
104
110
|
const onOpen = () => {
|
|
111
|
+
clearTimeout(timeout)
|
|
105
112
|
this.ws.off('error', onError)
|
|
106
113
|
resolve()
|
|
107
114
|
}
|
|
108
115
|
const onError = (err: Error) => {
|
|
116
|
+
clearTimeout(timeout)
|
|
109
117
|
this.ws.off('open', onOpen)
|
|
110
118
|
reject(
|
|
111
119
|
new AgentDaemonError(
|
|
@@ -169,8 +177,33 @@ export class AgentDaemonClient {
|
|
|
169
177
|
})
|
|
170
178
|
}
|
|
171
179
|
|
|
172
|
-
async listenForInspect(
|
|
173
|
-
|
|
180
|
+
async listenForInspect(
|
|
181
|
+
simId: string,
|
|
182
|
+
recipientId?: string,
|
|
183
|
+
): Promise<AgentInspectMessage> {
|
|
184
|
+
return this.send<AgentInspectMessage>('agent:listen-inspect', { simId, recipientId })
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async cancelInspectListener(simId: string): Promise<boolean> {
|
|
188
|
+
return this.send('agent:cancel-listen-inspect', { simId })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async takeInspectMessage(
|
|
192
|
+
simId: string,
|
|
193
|
+
recipientId: string,
|
|
194
|
+
): Promise<AgentInspectMessage | null> {
|
|
195
|
+
return this.send('agent:take-inspect-message', { simId, recipientId })
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async sendCanvasNote(simId: string, note: CanvasAgentNote): Promise<{ id: string }> {
|
|
199
|
+
return this.send('agent:send-canvas-note', { simId, note })
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async canvasNoteStatus(
|
|
203
|
+
simId: string,
|
|
204
|
+
messageId: string,
|
|
205
|
+
): Promise<{ status: CanvasAgentNoteStatus }> {
|
|
206
|
+
return this.send('agent:canvas-note-status', { simId, messageId })
|
|
174
207
|
}
|
|
175
208
|
|
|
176
209
|
async getInspectListenerStatus(simId: string): Promise<AgentInspectListenerStatus> {
|
package/src/agent-prompt.ts
CHANGED
|
@@ -10,6 +10,74 @@ export interface AgentInspectMessage {
|
|
|
10
10
|
simId: string
|
|
11
11
|
createdAt: string
|
|
12
12
|
prompt: AgentPromptEnvelope
|
|
13
|
+
recipientId?: string
|
|
14
|
+
crop?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface CanvasAgentNote {
|
|
18
|
+
agentId: string
|
|
19
|
+
text: string
|
|
20
|
+
inspectSummary: string
|
|
21
|
+
inspectTrace?: string
|
|
22
|
+
crop: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface CanvasAgentNoteDelivery {
|
|
26
|
+
send(note: CanvasAgentNote): Promise<{ id: string }>
|
|
27
|
+
waitUntilTaken(id: string, signal: AbortSignal): Promise<void>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type CanvasAgentNoteStatus = 'pending' | 'taken' | 'missing'
|
|
31
|
+
|
|
32
|
+
const MAX_NOTE_CROP_LENGTH = 2 * 1024 * 1024
|
|
33
|
+
export const CANVAS_AGENT_ID_MAX_CHARS = 200
|
|
34
|
+
export const MAX_CANVAS_AGENT_NOTE_BODY_BYTES = MAX_NOTE_CROP_LENGTH + 256 * 1024
|
|
35
|
+
export const AGENT_NOTE_TTL_MS = 24 * 60 * 60 * 1000
|
|
36
|
+
export const AGENT_NOTE_QUEUE_LIMIT = 16
|
|
37
|
+
|
|
38
|
+
export function readCanvasAgentId(value: unknown): string | undefined {
|
|
39
|
+
return typeof value === 'string' && value.length <= CANVAS_AGENT_ID_MAX_CHARS
|
|
40
|
+
? value.trim() || undefined
|
|
41
|
+
: undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readCanvasAgentNote(value: unknown): CanvasAgentNote | null {
|
|
45
|
+
if (!value || typeof value !== 'object') return null
|
|
46
|
+
const agentId = readCanvasAgentId(Reflect.get(value, 'agentId'))
|
|
47
|
+
const text: unknown = Reflect.get(value, 'text')
|
|
48
|
+
const inspectSummary: unknown = Reflect.get(value, 'inspectSummary')
|
|
49
|
+
const inspectTrace: unknown = Reflect.get(value, 'inspectTrace')
|
|
50
|
+
const crop: unknown = Reflect.get(value, 'crop')
|
|
51
|
+
if (
|
|
52
|
+
!agentId ||
|
|
53
|
+
typeof text !== 'string' ||
|
|
54
|
+
!text.trim() ||
|
|
55
|
+
text.length > 8192 ||
|
|
56
|
+
typeof inspectSummary !== 'string' ||
|
|
57
|
+
!inspectSummary.trim() ||
|
|
58
|
+
inspectSummary.length > 8192 ||
|
|
59
|
+
(inspectTrace !== undefined &&
|
|
60
|
+
(typeof inspectTrace !== 'string' || inspectTrace.length > 32_768)) ||
|
|
61
|
+
typeof crop !== 'string' ||
|
|
62
|
+
crop.length > MAX_NOTE_CROP_LENGTH ||
|
|
63
|
+
!/^data:image\/(?:webp|png);base64,[A-Za-z0-9+/]+={0,2}$/.test(crop)
|
|
64
|
+
)
|
|
65
|
+
return null
|
|
66
|
+
return { agentId, text: text.trim(), inspectSummary, inspectTrace, crop }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createCanvasAgentInspectMessage(
|
|
70
|
+
simId: string,
|
|
71
|
+
{ agentId, text, inspectSummary, inspectTrace, crop }: CanvasAgentNote,
|
|
72
|
+
): AgentInspectMessage {
|
|
73
|
+
return {
|
|
74
|
+
id: `inspect-${globalThis.crypto.randomUUID()}`,
|
|
75
|
+
simId,
|
|
76
|
+
createdAt: new Date().toISOString(),
|
|
77
|
+
recipientId: agentId,
|
|
78
|
+
prompt: { text, inspectSummary, inspectTrace },
|
|
79
|
+
crop,
|
|
80
|
+
}
|
|
13
81
|
}
|
|
14
82
|
|
|
15
83
|
export interface AgentInspectListenerStatus {
|
package/src/bridge-contract.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import type {
|
|
12
12
|
PeachAccessibilityNode,
|
|
13
13
|
SootSimAgentActionDetail,
|
|
14
|
+
SootSimAgentIdentity,
|
|
14
15
|
SootSimCaptureFilter,
|
|
15
16
|
SootSimExternalAppLifecycleOptions,
|
|
16
17
|
SootSimRecorderBridge,
|
|
@@ -100,6 +101,9 @@ export interface WsCommand {
|
|
|
100
101
|
acquireLock?: boolean
|
|
101
102
|
// bridge:claim and close — take over a cli lease another cli holds.
|
|
102
103
|
force?: boolean
|
|
104
|
+
// the identified cli driving this command; every agent action it causes
|
|
105
|
+
// carries it to the people watching the sim.
|
|
106
|
+
agent?: SootSimAgentIdentity
|
|
103
107
|
// bridge:list-sims — also report the retained disconnected sims (readyState
|
|
104
108
|
// closed with disconnectCause) after the live ones. read in bridge-host.
|
|
105
109
|
includeRetained?: boolean
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CanvasAgentNote,
|
|
3
|
+
CanvasAgentNoteDelivery,
|
|
4
|
+
CanvasAgentNoteStatus,
|
|
5
|
+
} from './agent-prompt'
|
|
6
|
+
|
|
7
|
+
export function createCanvasAgentNoteDelivery(
|
|
8
|
+
send: CanvasAgentNoteDelivery['send'],
|
|
9
|
+
readStatus: (id: string, signal: AbortSignal) => Promise<CanvasAgentNoteStatus>,
|
|
10
|
+
): CanvasAgentNoteDelivery {
|
|
11
|
+
return {
|
|
12
|
+
send,
|
|
13
|
+
async waitUntilTaken(id, signal) {
|
|
14
|
+
while (true) {
|
|
15
|
+
signal.throwIfAborted()
|
|
16
|
+
const status = await readStatus(id, signal)
|
|
17
|
+
if (status === 'taken') return
|
|
18
|
+
if (status === 'missing') throw new Error('This note is no longer available.')
|
|
19
|
+
await new Promise<void>((resolve, reject) => {
|
|
20
|
+
const abort = () => {
|
|
21
|
+
clearTimeout(timer)
|
|
22
|
+
reject(signal.reason)
|
|
23
|
+
}
|
|
24
|
+
const timer = setTimeout(() => {
|
|
25
|
+
signal.removeEventListener('abort', abort)
|
|
26
|
+
resolve()
|
|
27
|
+
}, 2_000)
|
|
28
|
+
signal.addEventListener('abort', abort, { once: true })
|
|
29
|
+
if (signal.aborted) abort()
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function cloudCanvasAgentNoteUrl(
|
|
37
|
+
origin: string,
|
|
38
|
+
target: { kind: 'sim' | 'box'; externalId: string },
|
|
39
|
+
): URL {
|
|
40
|
+
const url = new URL(
|
|
41
|
+
`/api/account/rnx-cloud/agent-notes/${encodeURIComponent(target.externalId)}`,
|
|
42
|
+
origin,
|
|
43
|
+
)
|
|
44
|
+
url.searchParams.set('kind', target.kind)
|
|
45
|
+
return url
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function cloudCanvasAgentNoteDelivery(
|
|
49
|
+
origin: string,
|
|
50
|
+
target: { kind: 'sim' | 'box'; externalId: string },
|
|
51
|
+
fetchNote: (input: URL, init: RequestInit) => Promise<Response> = fetch,
|
|
52
|
+
): CanvasAgentNoteDelivery {
|
|
53
|
+
const endpoint = cloudCanvasAgentNoteUrl(origin, target)
|
|
54
|
+
const post = async (body: object, signal?: AbortSignal): Promise<unknown> => {
|
|
55
|
+
const response = await fetchNote(endpoint, {
|
|
56
|
+
method: 'POST',
|
|
57
|
+
credentials: 'include',
|
|
58
|
+
headers: { 'content-type': 'application/json' },
|
|
59
|
+
body: JSON.stringify(body),
|
|
60
|
+
signal,
|
|
61
|
+
})
|
|
62
|
+
if (!response.ok) throw new Error('Could not reach the simulator’s note inbox.')
|
|
63
|
+
return response.json()
|
|
64
|
+
}
|
|
65
|
+
return createCanvasAgentNoteDelivery(
|
|
66
|
+
async (note: CanvasAgentNote) => {
|
|
67
|
+
const result = await post({ action: 'send', note })
|
|
68
|
+
const id: unknown =
|
|
69
|
+
result && typeof result === 'object' && Reflect.get(result, 'id')
|
|
70
|
+
if (typeof id !== 'string')
|
|
71
|
+
throw new Error('The simulator did not accept the note.')
|
|
72
|
+
return { id }
|
|
73
|
+
},
|
|
74
|
+
async (id, signal) => {
|
|
75
|
+
const result = await post({ action: 'status', id }, signal)
|
|
76
|
+
const status: unknown =
|
|
77
|
+
result && typeof result === 'object' && Reflect.get(result, 'status')
|
|
78
|
+
if (status === 'pending' || status === 'taken' || status === 'missing')
|
|
79
|
+
return status
|
|
80
|
+
throw new Error('The simulator did not report the note’s status.')
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
}
|
package/src/cloud-contract.ts
CHANGED
|
@@ -1,4 +1,34 @@
|
|
|
1
1
|
import type { BridgeCommandType } from './bridge-contract.ts'
|
|
2
|
+
import type { SootSimAgentIdentity } from '@peach/globals'
|
|
3
|
+
|
|
4
|
+
// each field of a command's driving agent, in characters.
|
|
5
|
+
export const AGENT_IDENTITY_MAX_CHARS = 200
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* the driving agent a command or an agent action carries, read from wire data.
|
|
9
|
+
* undefined is a command nobody identified; anything else that is not a whole
|
|
10
|
+
* identity is null, so every host refuses it the same way.
|
|
11
|
+
*/
|
|
12
|
+
export function readAgentIdentity(
|
|
13
|
+
value: unknown,
|
|
14
|
+
): SootSimAgentIdentity | null | undefined {
|
|
15
|
+
if (value === undefined) return undefined
|
|
16
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return null
|
|
17
|
+
const text = (key: string): string | null => {
|
|
18
|
+
const field: unknown = Reflect.get(value, key)
|
|
19
|
+
return typeof field === 'string' &&
|
|
20
|
+
field.trim() !== '' &&
|
|
21
|
+
field.length <= AGENT_IDENTITY_MAX_CHARS
|
|
22
|
+
? field
|
|
23
|
+
: null
|
|
24
|
+
}
|
|
25
|
+
const id = text('id')
|
|
26
|
+
const name = text('name')
|
|
27
|
+
const machine = text('machine')
|
|
28
|
+
const step = text('step')
|
|
29
|
+
if (id === null || name === null || machine === null || step === null) return null
|
|
30
|
+
return { id, name, machine, step }
|
|
31
|
+
}
|
|
2
32
|
|
|
3
33
|
// largest guest artifact a nano isolate measurably boots, settles, screenshots,
|
|
4
34
|
// and survives repeated invocations within Cloudflare Dynamic Worker platform
|
package/src/host/agent-host.ts
CHANGED
|
@@ -17,6 +17,15 @@ import { randomUUID } from 'node:crypto'
|
|
|
17
17
|
import fs from 'node:fs'
|
|
18
18
|
import path from 'node:path'
|
|
19
19
|
import { scanDevServers } from '../../scripts/dev-server-scanner.ts'
|
|
20
|
+
import {
|
|
21
|
+
AGENT_NOTE_QUEUE_LIMIT,
|
|
22
|
+
AGENT_NOTE_TTL_MS,
|
|
23
|
+
createCanvasAgentInspectMessage,
|
|
24
|
+
readCanvasAgentId,
|
|
25
|
+
readCanvasAgentNote,
|
|
26
|
+
type AgentInspectMessage,
|
|
27
|
+
type AgentPromptEnvelope,
|
|
28
|
+
} from '../agent-prompt.ts'
|
|
20
29
|
import {
|
|
21
30
|
AgentSessionError,
|
|
22
31
|
endSession as endAgentSession,
|
|
@@ -42,7 +51,6 @@ import {
|
|
|
42
51
|
type AttachedProject,
|
|
43
52
|
} from '../attached-projects.ts'
|
|
44
53
|
import type { AgentEvent } from '../agent-events.ts'
|
|
45
|
-
import type { AgentInspectMessage, AgentPromptEnvelope } from '../agent-prompt.ts'
|
|
46
54
|
import type { DiscoveredServer } from '../serverDiscoveryContract'
|
|
47
55
|
import type { WebSocket } from 'ws'
|
|
48
56
|
|
|
@@ -68,6 +76,7 @@ export interface AgentHostOptions {
|
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
interface InspectListener {
|
|
79
|
+
recipientId?: string
|
|
71
80
|
socket: WebSocket
|
|
72
81
|
resolve: (message: AgentInspectMessage) => void
|
|
73
82
|
reject: (error: Error) => void
|
|
@@ -99,6 +108,7 @@ export class AgentHost {
|
|
|
99
108
|
private allSockets = new Set<WebSocket>()
|
|
100
109
|
private inspectQueues = new Map<string, AgentInspectMessage[]>()
|
|
101
110
|
private inspectListeners = new Map<string, InspectListener[]>()
|
|
111
|
+
private takenNotes = new Map<string, { simId: string; expiresAt: number }>()
|
|
102
112
|
// agent wrappers also emit prompt-received from the FIFO reader, but the
|
|
103
113
|
// shell/daemon already knows the user-facing display text when send-prompt
|
|
104
114
|
// is accepted. emit the friendly prompt immediately and suppress the raw
|
|
@@ -201,6 +211,7 @@ export class AgentHost {
|
|
|
201
211
|
}
|
|
202
212
|
this.inspectListeners.clear()
|
|
203
213
|
this.inspectQueues.clear()
|
|
214
|
+
this.takenNotes.clear()
|
|
204
215
|
this.allSockets.clear()
|
|
205
216
|
}
|
|
206
217
|
|
|
@@ -222,7 +233,58 @@ export class AgentHost {
|
|
|
222
233
|
case 'agent:start-session':
|
|
223
234
|
return this.doStartSession(msg.input ?? {})
|
|
224
235
|
case 'agent:listen-inspect':
|
|
225
|
-
return this.listenForInspect(
|
|
236
|
+
return this.listenForInspect(
|
|
237
|
+
ws,
|
|
238
|
+
this.requireSimId(msg, type),
|
|
239
|
+
readCanvasAgentId(msg.recipientId),
|
|
240
|
+
)
|
|
241
|
+
case 'agent:cancel-listen-inspect': {
|
|
242
|
+
const simId = this.requireSimId(msg, type)
|
|
243
|
+
const listeners = this.inspectListeners.get(simId) ?? []
|
|
244
|
+
const index = listeners.findIndex((listener) => listener.socket === ws)
|
|
245
|
+
if (index < 0) return false
|
|
246
|
+
const [listener] = listeners.splice(index, 1)
|
|
247
|
+
if (!listeners.length) this.inspectListeners.delete(simId)
|
|
248
|
+
listener?.reject(new AgentSessionError('CANCELED', 'inspect listener canceled'))
|
|
249
|
+
return true
|
|
250
|
+
}
|
|
251
|
+
case 'agent:take-inspect-message': {
|
|
252
|
+
const recipientId = readCanvasAgentId(msg.recipientId)
|
|
253
|
+
return recipientId
|
|
254
|
+
? this.takeInspectMessage(this.requireSimId(msg, type), recipientId, true)
|
|
255
|
+
: null
|
|
256
|
+
}
|
|
257
|
+
case 'agent:send-canvas-note': {
|
|
258
|
+
const simId = this.requireSimId(msg, type)
|
|
259
|
+
const note = readCanvasAgentNote(msg.note)
|
|
260
|
+
if (!note) throw new AgentSessionError('INVALID_NOTE', 'invalid canvas note')
|
|
261
|
+
if (
|
|
262
|
+
this.inspectQueue(simId).filter((message) => message.recipientId).length >=
|
|
263
|
+
AGENT_NOTE_QUEUE_LIMIT
|
|
264
|
+
) {
|
|
265
|
+
throw new AgentSessionError(
|
|
266
|
+
'INBOX_FULL',
|
|
267
|
+
'the simulator has too many pending notes',
|
|
268
|
+
)
|
|
269
|
+
}
|
|
270
|
+
const message = createCanvasAgentInspectMessage(simId, note)
|
|
271
|
+
this.enqueueInspectMessage(message)
|
|
272
|
+
return { id: message.id }
|
|
273
|
+
}
|
|
274
|
+
case 'agent:canvas-note-status': {
|
|
275
|
+
const simId = this.requireSimId(msg, type)
|
|
276
|
+
this.pruneTakenNotes()
|
|
277
|
+
const pending = this.inspectQueue(simId).some(
|
|
278
|
+
(message) => message.id === msg.messageId,
|
|
279
|
+
)
|
|
280
|
+
return {
|
|
281
|
+
status: pending
|
|
282
|
+
? 'pending'
|
|
283
|
+
: this.takenNotes.get(msg.messageId)?.simId === simId
|
|
284
|
+
? 'taken'
|
|
285
|
+
: 'missing',
|
|
286
|
+
}
|
|
287
|
+
}
|
|
226
288
|
case 'agent:get-inspect-listener-status': {
|
|
227
289
|
const simId = this.requireSimId(msg, type)
|
|
228
290
|
return { simId, listening: (this.inspectListeners.get(simId)?.length ?? 0) > 0 }
|
|
@@ -274,20 +336,58 @@ export class AgentHost {
|
|
|
274
336
|
private listenForInspect(
|
|
275
337
|
socket: WebSocket,
|
|
276
338
|
simId: string,
|
|
339
|
+
recipientId?: string,
|
|
277
340
|
): Promise<AgentInspectMessage> {
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
if (queued && queue) {
|
|
281
|
-
if (queue.length === 0) this.inspectQueues.delete(simId)
|
|
282
|
-
return Promise.resolve(queued)
|
|
283
|
-
}
|
|
341
|
+
const queued = this.takeInspectMessage(simId, recipientId, false)
|
|
342
|
+
if (queued) return Promise.resolve(queued)
|
|
284
343
|
return new Promise((resolve, reject) => {
|
|
285
344
|
const listeners = this.inspectListeners.get(simId) ?? []
|
|
286
|
-
listeners.push({ socket, resolve, reject })
|
|
345
|
+
listeners.push({ socket, recipientId, resolve, reject })
|
|
287
346
|
this.inspectListeners.set(simId, listeners)
|
|
288
347
|
})
|
|
289
348
|
}
|
|
290
349
|
|
|
350
|
+
private pruneTakenNotes() {
|
|
351
|
+
const now = Date.now()
|
|
352
|
+
for (const [id, receipt] of this.takenNotes) {
|
|
353
|
+
if (receipt.expiresAt <= now) this.takenNotes.delete(id)
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private markInspectMessageTaken(message: AgentInspectMessage) {
|
|
358
|
+
if (!message.recipientId) return
|
|
359
|
+
this.pruneTakenNotes()
|
|
360
|
+
this.takenNotes.set(message.id, {
|
|
361
|
+
simId: message.simId,
|
|
362
|
+
expiresAt: Date.now() + AGENT_NOTE_TTL_MS,
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private inspectQueue(simId: string): AgentInspectMessage[] {
|
|
367
|
+
const queue = this.inspectQueues.get(simId)
|
|
368
|
+
if (!queue) return []
|
|
369
|
+
const cutoff = Date.now() - AGENT_NOTE_TTL_MS
|
|
370
|
+
const pending = queue.filter(
|
|
371
|
+
(message) => !message.recipientId || Date.parse(message.createdAt) > cutoff,
|
|
372
|
+
)
|
|
373
|
+
if (pending.length) this.inspectQueues.set(simId, pending)
|
|
374
|
+
else this.inspectQueues.delete(simId)
|
|
375
|
+
return pending
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
private takeInspectMessage(simId: string, recipientId?: string, notesOnly = false) {
|
|
379
|
+
const queue = this.inspectQueue(simId)
|
|
380
|
+
const index = queue.findIndex((message) =>
|
|
381
|
+
message.recipientId ? message.recipientId === recipientId : !notesOnly,
|
|
382
|
+
)
|
|
383
|
+
if (index < 0) return null
|
|
384
|
+
const [message] = queue.splice(index, 1)
|
|
385
|
+
if (!queue.length) this.inspectQueues.delete(simId)
|
|
386
|
+
if (!message) return null
|
|
387
|
+
this.markInspectMessageTaken(message)
|
|
388
|
+
return message
|
|
389
|
+
}
|
|
390
|
+
|
|
291
391
|
private sendInspectMessage(
|
|
292
392
|
simId: string,
|
|
293
393
|
prompt: AgentPromptEnvelope,
|
|
@@ -301,17 +401,27 @@ export class AgentHost {
|
|
|
301
401
|
createdAt: new Date().toISOString(),
|
|
302
402
|
prompt,
|
|
303
403
|
}
|
|
304
|
-
const
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
const
|
|
404
|
+
const queued = this.enqueueInspectMessage(message)
|
|
405
|
+
return { ok: true, queued, messageId: message.id }
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private enqueueInspectMessage(message: AgentInspectMessage): boolean {
|
|
409
|
+
const { simId, recipientId } = message
|
|
410
|
+
const listeners = this.inspectListeners.get(simId) ?? []
|
|
411
|
+
const index = listeners.findIndex(
|
|
412
|
+
(listener) => !recipientId || listener.recipientId === recipientId,
|
|
413
|
+
)
|
|
414
|
+
if (index >= 0) {
|
|
415
|
+
const [listener] = listeners.splice(index, 1)
|
|
416
|
+
if (!listeners.length) this.inspectListeners.delete(simId)
|
|
417
|
+
this.markInspectMessageTaken(message)
|
|
418
|
+
listener?.resolve(message)
|
|
419
|
+
return false
|
|
420
|
+
}
|
|
421
|
+
const queue = this.inspectQueue(simId)
|
|
312
422
|
queue.push(message)
|
|
313
423
|
this.inspectQueues.set(simId, queue)
|
|
314
|
-
return
|
|
424
|
+
return true
|
|
315
425
|
}
|
|
316
426
|
|
|
317
427
|
private async doStartSession(input: {
|