| 1345 | }) |
| 1346 | export type ShellInput = z.infer<typeof ShellInput> |
| 1347 | export async function shell(input: ShellInput) { |
| 1348 | const abort = start(input.sessionID) |
| 1349 | if (!abort) { |
| 1350 | throw new Session.BusyError(input.sessionID) |
| 1351 | } |
| 1352 | using _ = defer(() => cancel(input.sessionID)) |
| 1353 | |
| 1354 | const session = await Session.get(input.sessionID) |
| 1355 | if (session.revert) { |
| 1356 | SessionRevert.cleanup(session) |
| 1357 | } |
| 1358 | const agent = await Agent.get(input.agent) |
| 1359 | const model = input.model ?? agent.model ?? (await lastModel(input.sessionID)) |
| 1360 | const userMsg: MessageV2.User = { |
| 1361 | id: Identifier.ascending("message"), |
| 1362 | sessionID: input.sessionID, |
| 1363 | time: { |
| 1364 | created: Date.now(), |
| 1365 | }, |
| 1366 | role: "user", |
| 1367 | agent: input.agent, |
| 1368 | model: { |
| 1369 | providerID: model.providerID, |
| 1370 | modelID: model.modelID, |
| 1371 | }, |
| 1372 | } |
| 1373 | await Session.updateMessage(userMsg) |
| 1374 | const userPart: MessageV2.Part = { |
| 1375 | type: "text", |
| 1376 | id: Identifier.ascending("part"), |
| 1377 | messageID: userMsg.id, |
| 1378 | sessionID: input.sessionID, |
| 1379 | text: "The following tool was executed by the user", |
| 1380 | synthetic: true, |
| 1381 | } |
| 1382 | await Session.updatePart(userPart) |
| 1383 | |
| 1384 | const msg: MessageV2.Assistant = { |
| 1385 | id: Identifier.ascending("message"), |
| 1386 | sessionID: input.sessionID, |
| 1387 | parentID: userMsg.id, |
| 1388 | mode: input.agent, |
| 1389 | cost: 0, |
| 1390 | path: { |
| 1391 | cwd: Instance.directory, |
| 1392 | root: Instance.worktree, |
| 1393 | }, |
| 1394 | time: { |
| 1395 | created: Date.now(), |
| 1396 | }, |
| 1397 | role: "assistant", |
| 1398 | tokens: { |
| 1399 | input: 0, |
| 1400 | output: 0, |
| 1401 | reasoning: 0, |
| 1402 | cache: { read: 0, write: 0 }, |
| 1403 | }, |
| 1404 | modelID: model.modelID, |