forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernelArtifacts.ts
More file actions
175 lines (160 loc) · 6.13 KB
/
Copy pathkernelArtifacts.ts
File metadata and controls
175 lines (160 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// KernelArtifacts — Windows port of the macOS agent runtime's kernel-artifacts.ts
// (desktop/macos/agent/src/runtime/kernel-artifacts.ts).
//
// Artifact + run inspection surface: getRun, inspectArtifacts, persistArtifact,
// artifact lifecycle transitions, the artifact-delivery outbox, and the
// adapter-capacity predicates the callers use before dispatching work.
import { KernelRuns } from './kernelRuns'
import { boundedLimit, desktopArtifactDeliveryFromRow, placeholders } from './kernelSupport'
import type { AgentArtifact, DesktopArtifactDelivery, NewDesktopArtifactDelivery } from './types'
import type {
GetRunInput,
InspectArtifactsInput,
KernelRunDetails,
PersistArtifactInput,
UpdateArtifactLifecycleInput,
UpdateArtifactLifecycleResult
} from './kernelTypes'
export class KernelArtifacts extends KernelRuns {
queueArtifactDelivery(input: NewDesktopArtifactDelivery): DesktopArtifactDelivery {
if (input.deliveryId) {
const existing = this.store.getOptionalRow(
'SELECT * FROM desktop_artifact_deliveries WHERE delivery_id = ? AND owner_id = ?',
[input.deliveryId, input.ownerId]
)
if (existing) return desktopArtifactDeliveryFromRow(existing)
}
return this.store.insertDesktopArtifactDelivery(input)
}
listArtifactDeliveries(input: {
ownerId: string
targetRef?: string
statuses?: DesktopArtifactDelivery['deliveryStatus'][]
limit?: number
}): DesktopArtifactDelivery[] {
const where = ['owner_id = ?']
const values: unknown[] = [input.ownerId]
if (input.targetRef) {
where.push('target_ref = ?')
values.push(input.targetRef)
}
if (input.statuses?.length) {
where.push(`delivery_status IN (${placeholders(input.statuses.length)})`)
values.push(...input.statuses)
}
values.push(boundedLimit(input.limit, 100, 500))
return this.store
.allRows(
`SELECT * FROM desktop_artifact_deliveries
WHERE ${where.join(' AND ')}
ORDER BY created_at_ms ASC LIMIT ?`,
values
)
.map(desktopArtifactDeliveryFromRow)
}
updateArtifactDelivery(
deliveryId: string,
input: { ownerId: string } & Partial<
Pick<
DesktopArtifactDelivery,
'deliveryStatus' | 'attemptCount' | 'receiptJson' | 'errorJson' | 'deliveredAtMs'
>
>
): DesktopArtifactDelivery {
return this.store.updateDesktopArtifactDelivery(deliveryId, input)
}
getRun(input: GetRunInput): KernelRunDetails {
const run = this.readRun(input.runId)
const session = this.readSession(run.sessionId)
if (input.ownerId) {
this.assertSessionOwner(session, input.ownerId)
}
return {
session,
run,
attempts: this.readAttemptsForRun(run.runId),
adapterBindings: this.readBindingsForSession(session.sessionId),
artifacts: this.readArtifacts({ runId: run.runId, limit: 100 }),
events: input.includeEvents
? this.readEventsForRun(run.runId, boundedLimit(input.eventLimit, 100, 500))
: [],
parentDelegations: this.readParentDelegationsForRun(run.runId),
childDelegations: this.readChildDelegationsForRun(run.runId)
}
}
inspectArtifacts(input: InspectArtifactsInput): AgentArtifact[] {
if (!input.artifactId && !input.sessionId && !input.runId && !input.attemptId) {
throw new Error('Inspecting artifacts requires artifactId, sessionId, runId, or attemptId')
}
if (input.ownerId) {
this.assertArtifactSelectorOwner(input, input.ownerId)
}
return this.readArtifacts(input)
}
updateArtifactLifecycle(input: UpdateArtifactLifecycleInput): UpdateArtifactLifecycleResult {
return this.withTransaction(() => {
const artifact = this.readArtifact(input.artifactId)
this.assertArtifactScope(artifact, input)
if (input.ownerId) {
this.assertSessionOwner(this.readSession(artifact.sessionId), input.ownerId)
}
if (artifact.lifecycleState === input.state) {
return { artifact, changed: false, event: null }
}
const now = Date.now()
this.store.execute(
'UPDATE artifacts SET lifecycle_state = ?, lifecycle_updated_at_ms = ? WHERE artifact_id = ?',
[input.state, now, artifact.artifactId]
)
const updatedArtifact = this.readArtifact(artifact.artifactId)
const event = this.appendEvent({
sessionId: updatedArtifact.sessionId,
runId: updatedArtifact.runId,
attemptId: updatedArtifact.attemptId,
type: 'artifact.lifecycle_updated',
payload: {
artifactId: updatedArtifact.artifactId,
previousState: artifact.lifecycleState,
state: updatedArtifact.lifecycleState,
reason: input.reason ?? null,
metadata: input.metadata ?? {},
lifecycleUpdatedAtMs: now
}
})
return { artifact: updatedArtifact, changed: true, event }
})
}
persistArtifact(input: PersistArtifactInput): AgentArtifact {
return this.withTransaction(() => this.persistArtifactInTransaction(input))
}
hasActiveExecutionForAdapter(adapterId: string): boolean {
for (const active of this.activeExecutions.values()) {
if (active.adapter.adapterId === adapterId) return true
}
return false
}
hasActiveExecutionForSessionAdapter(sessionId: string, adapterId: string): boolean {
for (const active of this.activeExecutions.values()) {
if (active.sessionId === sessionId && active.adapter.adapterId === adapterId) return true
}
return false
}
hasExecutionCapacityForAdapter(adapterId: string): boolean {
if (!this.registry.has(adapterId)) return false
let activeCount = 0
for (const active of this.activeExecutions.values()) {
if (active.adapter.adapterId === adapterId) activeCount += 1
}
return activeCount < this.registry.capacity(adapterId)
}
isAdapterRegistered(adapterId: string): boolean {
return this.registry.has(adapterId)
}
defaultAdapterIdForSession(sessionId: string): string {
return this.readSession(sessionId).defaultAdapterId
}
defaultAdapterIdForRun(runId: string): string {
const run = this.readRun(runId)
return this.readSession(run.sessionId).defaultAdapterId
}
}