Repository navigation
Expand file tree
/
Copy pathsession.ts
More file actions
245 lines (222 loc) · 8.36 KB
/
Copy pathsession.ts
File metadata and controls
245 lines (222 loc) · 8.36 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import type { ExperimentalSessionApi, SessionApi } from "@opencode/client/effect/api"
import type { GenerationOptionsFields, Message, SystemPart } from "@opencode/ai/effect"
import type { Agent } from "@opencode/schema/agent"
import type { Model } from "@opencode/schema/model"
import type { PromptInput } from "@opencode/schema/prompt-input"
import type { Session } from "@opencode/schema/session"
import type { SessionInbox } from "@opencode/schema/session-inbox"
import type { SessionError } from "@opencode/schema/session-error"
import type { SessionMessage } from "@opencode/schema/session-message"
import type { TokenUsage } from "@opencode/schema/token-usage"
import type { JsonSchema, Types } from "effect"
import type { ModelHooks } from "./registration.js"
export interface SessionPrompt {
readonly sessionID: Session.ID
readonly messageID: SessionMessage.ID
prompt: Types.DeepMutable<PromptInput.Prompt>
metadata?: Record<string, unknown>
delivery: SessionInbox.Delivery
}
/** Request overrides. Typed keys are generation settings; any other key is a provider option. */
export type SessionRequestOptions = Types.DeepMutable<GenerationOptionsFields> & Record<string, unknown>
/** A request message. `metadata` is the stored Session message's metadata, for reading only; it is not sent to the model. */
export type SessionRequestMessage = Message & { readonly metadata?: Record<string, unknown> }
export interface SessionRequest {
readonly sessionID: Session.ID
readonly model: Model.Ref
/**
* The instructions part keeps each source's text in `metadata.experimental.instructions` as `{ key, text }`. It is
* not updated when a hook edits `text`. Experimental.
*/
system: Array<SystemPart>
messages: Array<SessionRequestMessage>
options: SessionRequestOptions
}
export interface SessionContext extends SessionRequest {
readonly agent: Agent.ID
tools: Record<string, { description: string; input: JsonSchema.JsonSchema }>
}
export interface SessionCompactionResult {
summary: string
/** `native.messages`, when set, must be a provider replacement window; it replays instead of `summary`. */
native?: SessionMessage.Native
metadata?: Record<string, unknown>
tokens?: TokenUsage.Info
}
export interface SessionCompaction extends SessionContext {
/** Set to use this compaction and skip the model request. */
result?: SessionCompactionResult
}
export interface SessionGenerate extends SessionContext {}
export interface SessionTitle extends SessionRequest {
/** Set to use this title and skip the model request. */
result?: string
}
/**
* Why a Session request is being made. Auxiliary requests share the Session's
* hook identity but need to be told apart from the agent loop.
*/
export type SessionRequestKind = "primary" | "compaction" | "title" | "generate"
/**
* Runs whenever a Session resolves the model for a request, before anything is built for it. `model` starts as the
* Session's or agent's selection; change it to serve this request with another model from the same provider, including
* models hidden from the picker. The Session keeps its selection, and the request and its records use the new model.
* Router models such as GitHub Copilot's Auto use it. Experimental.
*/
export interface SessionModelResolve {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly kind: SessionRequestKind
model: Model.Ref
}
export interface SessionModelRequest {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
baseURL?: string
headers: Record<string, string>
}
export interface SessionHttpRequest {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
request: Request
}
export interface SessionHttpResponse {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
readonly request: Request
response: Response
}
/**
* Connection a WebSocket-backed request opens or reuses. Runs once per model call before the
* Session's socket is selected; changing `url` or `headers` reopens the socket. Experimental.
*/
export interface SessionWebSocketHandshake {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
url: string
headers: Record<string, string>
}
/**
* Outbound frame about to be written to the Session's socket, after the provider driver has built
* it. Replacing `frame` sends the replacement verbatim; the driver still tracks state from the
* provider's replies, so a rewrite that changes protocol meaning is on the plugin. Experimental.
*/
export interface SessionWebSocketSend {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
frame: string
}
/**
* Inbound frame read from the Session's socket, before the provider driver observes it. Replacing
* `frame` hands the replacement to the driver verbatim. Experimental.
*/
export interface SessionWebSocketReceive {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly kind: SessionRequestKind
frame: string
}
export type SessionRetryDecision = { retry: false } | { retry: true; delay: number }
export interface SessionRetry {
readonly sessionID: Session.ID
readonly agent: Agent.ID
readonly model: Model.Ref
readonly error: SessionError.Error
readonly attempt: number
decision: SessionRetryDecision
}
/**
* Background work still running for the idle Session or one of its descendants. `sessionID` is
* the Session that owns the job: the idle Session itself, or a descendant such as a subagent child.
*/
export type SessionIdleBackground =
| { readonly kind: "codemode"; readonly sessionID: Session.ID; readonly jobID: string }
| {
readonly kind: "shell"
readonly sessionID: Session.ID
readonly shellID: string
readonly command: string
}
| {
readonly kind: "subagent"
readonly sessionID: Session.ID
readonly childSessionID: Session.ID
readonly agent: string
readonly description: string
}
| {
readonly kind: "image"
readonly sessionID: Session.ID
readonly path: string
readonly model: string
}
| {
readonly kind: "video"
readonly sessionID: Session.ID
readonly path: string
}
/**
* Runs when a Session finishes model work and has no pending input, just before it goes idle.
* It does not run after interruption, failure, or control-only work such as a manual compaction.
* Input admitted by the hook, such as a synthetic message, runs in the same execution instead
* of the Session going idle.
*
* `background` lists work that is still running. When the Session's own shell or subagent finishes,
* its notice wakes the Session and the hook runs again, so a plugin that waits for that work can
* simply return. Work a descendant owns, such as a shell a subagent left running, wakes that
* descendant instead, so it is listed for information only. Notices admitted with `resume: false`,
* such as those recovered after a restart, wait for the next wake. Experimental.
*/
export interface SessionIdle {
readonly sessionID: Session.ID
readonly background: ReadonlyArray<SessionIdleBackground>
}
export interface SessionHooks {
readonly fork: { readonly sessionID: Session.ID }
readonly prompt: SessionPrompt
readonly context: SessionContext
readonly compaction: SessionCompaction
readonly generate: SessionGenerate
readonly title: SessionTitle
readonly "experimental.model.resolve": SessionModelResolve
readonly "model.request": SessionModelRequest
readonly "http.request": SessionHttpRequest
readonly "http.response": SessionHttpResponse
readonly "experimental.ws.handshake": SessionWebSocketHandshake
readonly "experimental.ws.send": SessionWebSocketSend
readonly "experimental.ws.receive": SessionWebSocketReceive
readonly retry: SessionRetry
readonly "experimental.idle": SessionIdle
}
export type SessionDomain = Pick<
SessionApi<unknown>,
| "list"
| "create"
| "get"
| "remove"
| "switchAgent"
| "switchModel"
| "prompt"
| "generate"
| "command"
| "compact"
| "synthetic"
| "interrupt"
| "update"
| "move"
| "context"
> &
Pick<ExperimentalSessionApi<unknown>, "wait" | "instructions"> & {
readonly hook: ModelHooks<SessionHooks>
}