@leuduan/lagent 0.1.2 → 44.0.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/README.md +28 -15
- package/dist/chunks/{UC3FVVJ2.js → 2GCIWSOA.js} +2 -2
- package/dist/chunks/2P47AHNK.js +7 -0
- package/dist/chunks/3UMZMPWW.js +1 -0
- package/dist/chunks/5IRF47YB.js +1 -0
- package/dist/chunks/6DRCKZ2C.js +1 -0
- package/dist/chunks/6FRNHMCV.js +1 -0
- package/dist/chunks/7LI64H5X.js +17 -0
- package/dist/chunks/{SL2YLB2F.js → 7VAPNE3L.js} +1 -1
- package/dist/chunks/{BM3S37EZ.js → ASSQZH3F.js} +1 -1
- package/dist/chunks/B4FXK5ER.js +77 -0
- package/dist/chunks/{37YOGDGY.js → BBCW2FZK.js} +1 -1
- package/dist/chunks/BPRCULZH.js +1 -0
- package/dist/chunks/CANPZYTH.js +24 -0
- package/dist/chunks/CQEVREBT.js +1 -0
- package/dist/chunks/CUK7CGC7.js +1 -0
- package/dist/chunks/D4PQP5MA.js +3 -0
- package/dist/chunks/{CDQG7RKA.js → DDZ7YF26.js} +1 -1
- package/dist/chunks/EMRB4KCN.js +6 -0
- package/dist/chunks/EPYQ5M3B.js +3 -0
- package/dist/chunks/G44LG6VL.js +1 -0
- package/dist/chunks/GGSVIIWJ.js +2 -0
- package/dist/chunks/IAAEP6VL.js +30 -0
- package/dist/chunks/{AP6Q4EPT.js → JCTBX33H.js} +1 -1
- package/dist/chunks/JFX2MXBA.js +4 -0
- package/dist/chunks/JFZZXVWD.js +15 -0
- package/dist/chunks/KUZCL2SH.js +1 -0
- package/dist/chunks/MHC6VPB4.js +7 -0
- package/dist/chunks/MMNNZCBO.js +5 -0
- package/dist/chunks/{C5LYCXJG.js → MNJ4SRF7.js} +1 -1
- package/dist/chunks/N6IXG2VL.js +1 -0
- package/dist/chunks/OCBPT7KE.js +1 -0
- package/dist/chunks/OL5A6ITQ.js +1 -0
- package/dist/chunks/Q75FUN7D.js +2 -0
- package/dist/chunks/Q7S5LFGN.js +1 -0
- package/dist/chunks/RCIHV3KG.js +1 -0
- package/dist/chunks/S34ZA2R6.js +1 -0
- package/dist/chunks/SS3B5W34.js +12 -0
- package/dist/chunks/VYMT4TXD.js +1 -0
- package/dist/chunks/W57RW55W.js +1 -0
- package/dist/compositions/base.d.ts +11 -0
- package/dist/{presets → compositions}/codex.d.ts +12 -6
- package/dist/{presets/prompts.d.ts → compositions/codexPrompts.d.ts} +1 -1
- package/dist/context.d.ts +18 -10
- package/dist/index.d.ts +31 -23
- package/dist/index.js +1 -77
- package/dist/journal/catalog.d.ts +1 -1
- package/dist/journal/journal.d.ts +3 -3
- package/dist/journal/projection.d.ts +1 -1
- package/dist/journal/store.d.ts +3 -3
- package/dist/llm/index.d.ts +19 -13
- package/dist/llm/index.js +1 -0
- package/dist/llm/provider.d.ts +11 -0
- package/dist/loop/agent.d.ts +16 -132
- package/dist/loop/authorization.d.ts +41 -0
- package/dist/loop/auxiliary.d.ts +7 -3
- package/dist/loop/chunks.d.ts +3 -3
- package/dist/loop/completion.d.ts +17 -0
- package/dist/loop/errors.d.ts +6 -29
- package/dist/loop/hooks.d.ts +39 -6
- package/dist/loop/index.d.ts +7 -28
- package/dist/loop/runner.d.ts +134 -0
- package/dist/loop/runtime.d.ts +7 -16
- package/dist/loop/strategy.d.ts +12 -3
- package/dist/loop/{prepare.d.ts → surfacePrepare.d.ts} +2 -2
- package/dist/loop/tool.d.ts +11 -6
- package/dist/loop/toolCalls.d.ts +3 -10
- package/dist/plugin.d.ts +31 -34
- package/dist/plugin.js +1 -1
- package/dist/plugins/agentsMd.d.ts +24 -20
- package/dist/plugins/agentsMd.js +1 -1
- package/dist/plugins/ask/index.d.ts +2 -2
- package/dist/plugins/ask/index.js +1 -1
- package/dist/plugins/ask/service.d.ts +2 -2
- package/dist/plugins/commands/index.d.ts +5 -2
- package/dist/plugins/commands/index.js +1 -1
- package/dist/plugins/commands/output.d.ts +1 -1
- package/dist/plugins/commands/record.d.ts +31 -22
- package/dist/plugins/commands/service.d.ts +18 -11
- package/dist/plugins/commands/{background.d.ts → tools.d.ts} +2 -0
- package/dist/plugins/compaction/checkpoint.d.ts +4 -1
- package/dist/plugins/compaction/config.d.ts +3 -3
- package/dist/plugins/compaction/estimate.d.ts +9 -1
- package/dist/plugins/compaction/index.d.ts +87 -2
- package/dist/plugins/compaction/index.js +5 -5
- package/dist/plugins/compaction/{records.d.ts → record.d.ts} +36 -36
- package/dist/plugins/compaction/{log.d.ts → state.d.ts} +8 -1
- package/dist/plugins/dateAndBranch.js +1 -1
- package/dist/plugins/delegations/answers.d.ts +1 -1
- package/dist/plugins/delegations/assignmentKey.d.ts +1 -1
- package/dist/plugins/delegations/cleanup.d.ts +1 -1
- package/dist/plugins/delegations/index.js +1 -1
- package/dist/plugins/delegations/notices.d.ts +8 -8
- package/dist/plugins/delegations/obligations.d.ts +13 -13
- package/dist/plugins/delegations/record.d.ts +11 -10
- package/dist/plugins/delegations/request.d.ts +8 -5
- package/dist/plugins/delegations/role.d.ts +1 -1
- package/dist/plugins/delegations/service.d.ts +9 -8
- package/dist/plugins/delegations/{waitBook.d.ts → waitResolver.d.ts} +9 -9
- package/dist/plugins/diagnostics/digest.d.ts +1 -1
- package/dist/plugins/diagnostics/index.d.ts +1 -1
- package/dist/plugins/diagnostics/index.js +1 -1
- package/dist/plugins/fileTools/config.d.ts +10 -0
- package/dist/plugins/{tools → fileTools}/editFiles.d.ts +16 -27
- package/dist/plugins/fileTools/gitDiscovery.d.ts +9 -0
- package/dist/plugins/fileTools/index.d.ts +18 -0
- package/dist/plugins/fileTools/index.js +1 -0
- package/dist/plugins/fileTools/tools.d.ts +6 -0
- package/dist/plugins/historyRecall/history.d.ts +1 -1
- package/dist/plugins/historyRecall/index.js +1 -1
- package/dist/plugins/historyRecall/render.d.ts +1 -1
- package/dist/plugins/mcp/catalog.d.ts +1 -1
- package/dist/plugins/mcp/index.d.ts +2 -2
- package/dist/plugins/mcp/index.js +1 -1
- package/dist/plugins/mcp/mount.d.ts +2 -2
- package/dist/plugins/mcp/render.d.ts +1 -1
- package/dist/plugins/mcp/{tool.d.ts → tools.d.ts} +1 -1
- package/dist/plugins/{routeContext.d.ts → routeNotice.d.ts} +2 -2
- package/dist/plugins/routeNotice.js +1 -0
- package/dist/{sandbox → plugins/sandbox}/absent.d.ts +11 -10
- package/dist/{sandbox → plugins/sandbox}/index.d.ts +5 -2
- package/dist/plugins/sandbox/index.js +4 -0
- package/dist/{sandbox → plugins/sandbox}/mode.d.ts +5 -5
- package/dist/plugins/sandbox/policy.d.ts +163 -0
- package/dist/{sandbox → plugins/sandbox}/service.d.ts +23 -13
- package/dist/plugins/schedules/index.js +3 -3
- package/dist/plugins/schedules/record.d.ts +18 -18
- package/dist/plugins/schedules/service.d.ts +2 -2
- package/dist/plugins/skills/discovery.d.ts +38 -9
- package/dist/plugins/skills/index.d.ts +1 -1
- package/dist/plugins/skills/index.js +1 -1
- package/dist/plugins/subAgents/index.js +1 -1
- package/dist/plugins/subAgents/{fork.d.ts → tools.d.ts} +36 -1
- package/dist/plugins/threadDirs.d.ts +36 -0
- package/dist/plugins/threadDirs.js +1 -0
- package/dist/plugins/toolPolicy/index.d.ts +2 -2
- package/dist/plugins/toolPolicy/index.js +1 -1
- package/dist/plugins/truncateToolOutput.d.ts +14 -10
- package/dist/plugins/truncateToolOutput.js +6 -1
- package/dist/plugins/usage/index.d.ts +4 -2
- package/dist/plugins/usage/index.js +1 -1
- package/dist/plugins/usage/tracker.d.ts +1 -1
- package/dist/plugins/verify/index.d.ts +7 -55
- package/dist/plugins/verify/index.js +6 -1
- package/dist/plugins/verify/judgement.d.ts +16 -0
- package/dist/plugins/verify/record.d.ts +382 -0
- package/dist/plugins/verify/service.d.ts +7 -4
- package/dist/plugins/verify/state.d.ts +58 -0
- package/dist/plugins/webTools/index.d.ts +34 -0
- package/dist/plugins/webTools/index.js +1 -0
- package/dist/plugins/workflows/assignment.d.ts +6 -2
- package/dist/plugins/workflows/cleanup.d.ts +1 -1
- package/dist/plugins/workflows/definition.d.ts +2 -2
- package/dist/plugins/workflows/{bindings.d.ts → entryPlan.d.ts} +2 -2
- package/dist/plugins/workflows/{providers.d.ts → hostBindings.d.ts} +2 -2
- package/dist/plugins/workflows/index.d.ts +4 -4
- package/dist/plugins/workflows/index.js +10 -5
- package/dist/plugins/workflows/observations.d.ts +3 -3
- package/dist/plugins/workflows/participantModels.d.ts +28 -0
- package/dist/plugins/workflows/participants.d.ts +2 -2
- package/dist/plugins/workflows/policy.d.ts +2 -2
- package/dist/plugins/workflows/record.d.ts +51 -65
- package/dist/plugins/workflows/routing.d.ts +2 -2
- package/dist/plugins/workflows/service.d.ts +24 -24
- package/dist/plugins/workflows/{fold.d.ts → state.d.ts} +2 -2
- package/dist/plugins/workflows/stopCheck.d.ts +13 -0
- package/dist/plugins/workflows/tools.d.ts +13 -0
- package/dist/plugins/workflows/view.d.ts +2 -2
- package/dist/plugins/{workspaceContext.d.ts → workspaceNotice.d.ts} +5 -7
- package/dist/plugins/workspaceNotice.js +1 -0
- package/dist/{plugins/providers → providers}/anthropic.d.ts +6 -3
- package/dist/{plugins/providers → providers}/anthropicMessages.d.ts +2 -1
- package/dist/{plugins/providers → providers}/codex.d.ts +14 -3
- package/dist/{plugins/providers → providers}/deepseek.d.ts +6 -2
- package/dist/{plugins/providers → providers}/failure.d.ts +1 -1
- package/dist/{plugins/providers → providers}/gateway.d.ts +2 -1
- package/dist/providers/index.d.ts +18 -0
- package/dist/providers/index.js +6 -0
- package/dist/{plugins/providers → providers}/openaiChat.d.ts +13 -2
- package/dist/{plugins/providers → providers}/openaiResponses.d.ts +12 -3
- package/dist/{plugins/providers → providers}/opencodeCopilot.d.ts +2 -2
- package/dist/{plugins/providers → providers}/opencodeGo.d.ts +18 -7
- package/dist/{plugins/providers → providers}/shared.d.ts +9 -1
- package/dist/{plugins/providers → providers}/stream.d.ts +4 -2
- package/dist/{models/door.d.ts → records/index.d.ts} +8 -7
- package/dist/records/index.js +1 -0
- package/dist/{models/wire → records}/journal.d.ts +210 -14
- package/dist/{models/wire/primitives.d.ts → records/toolResult.d.ts} +1 -1
- package/dist/services/agents.d.ts +26 -1
- package/dist/services/confinement/files.d.ts +118 -0
- package/dist/services/confinement/service.d.ts +219 -0
- package/dist/services/core.d.ts +2 -8
- package/dist/{kit/extension.d.ts → services/extensionEvents.d.ts} +5 -4
- package/dist/services/journal.d.ts +1 -1
- package/dist/services/llm.d.ts +4 -2
- package/dist/{kit → services}/modes.d.ts +1 -1
- package/dist/services/plugins.d.ts +34 -0
- package/dist/services/{preset.d.ts → presets/definition.d.ts} +4 -4
- package/dist/services/{presets.d.ts → presets/installations.d.ts} +3 -3
- package/dist/services/prompts.d.ts +23 -5
- package/dist/services/{threadActivation.d.ts → threads/activation.d.ts} +3 -3
- package/dist/services/{admission.d.ts → threads/admission.d.ts} +3 -3
- package/dist/services/{birth.d.ts → threads/birth.d.ts} +9 -10
- package/dist/services/threads/index.d.ts +14 -0
- package/dist/services/{runEvents.d.ts → threads/runEvents.d.ts} +2 -2
- package/dist/services/{owner.d.ts → threads/runOwner.d.ts} +5 -4
- package/dist/services/{threads.d.ts → threads/service.d.ts} +9 -8
- package/dist/services/{thread.d.ts → threads/thread.d.ts} +8 -7
- package/dist/services/{threadView.d.ts → threads/view.d.ts} +7 -6
- package/dist/services/tools.d.ts +79 -2
- package/dist/{loop/opening.d.ts → state/birth.d.ts} +22 -2
- package/dist/{loop → state}/chain.d.ts +3 -3
- package/dist/state/confirmations.d.ts +64 -0
- package/dist/{loop → state}/continuation.d.ts +3 -2
- package/dist/{loop → state}/decide.d.ts +10 -2
- package/dist/state/dispatches.d.ts +54 -0
- package/dist/state/errors.d.ts +26 -0
- package/dist/{loop → state}/forks.d.ts +1 -1
- package/dist/{loop → state}/holds.d.ts +1 -1
- package/dist/{loop → state}/inbox.d.ts +2 -2
- package/dist/state/index.d.ts +31 -0
- package/dist/state/inputs.d.ts +58 -0
- package/dist/state/installations.d.ts +28 -0
- package/dist/state/prompt.d.ts +13 -0
- package/dist/state/recovery.d.ts +7 -0
- package/dist/{loop → state}/replace.d.ts +2 -2
- package/dist/{loop → state}/requests.d.ts +3 -3
- package/dist/{loop → state}/route.d.ts +10 -2
- package/dist/state/summary.d.ts +16 -0
- package/dist/{loop → state}/surface.d.ts +3 -3
- package/dist/state/surfaceRead.d.ts +20 -0
- package/dist/{loop/threadState.d.ts → state/thread.d.ts} +12 -11
- package/dist/state/turns.d.ts +87 -0
- package/dist/state/waits.d.ts +38 -0
- package/dist/{loop → state}/wake.d.ts +1 -1
- package/dist/stores/durableObject.js +1 -0
- package/dist/{plugins/store → stores}/memory.d.ts +2 -2
- package/dist/{plugins/store → stores}/sql.d.ts +2 -2
- package/dist/stores/sqlite.js +1 -0
- package/dist/sys/environment.d.ts +0 -5
- package/dist/sys/git.d.ts +0 -13
- package/dist/sys/paths.d.ts +0 -47
- package/dist/sys/spawn.d.ts +16 -6
- package/dist/testing/clock.d.ts +1 -1
- package/dist/testing/fakeLlm.d.ts +4 -2
- package/dist/testing/fedLlm.d.ts +3 -1
- package/dist/testing/helpers.d.ts +31 -13
- package/dist/testing/index.js +1 -1
- package/dist/testing/settlement.d.ts +1 -1
- package/package.json +36 -28
- package/dist/chunks/2O7ZGEIT.js +0 -1
- package/dist/chunks/2OSESBIL.js +0 -1
- package/dist/chunks/3LQHQRIU.js +0 -6
- package/dist/chunks/3QCW4WDJ.js +0 -1
- package/dist/chunks/4XVRUSCQ.js +0 -1
- package/dist/chunks/6ECI2GGV.js +0 -1
- package/dist/chunks/74HKUGRR.js +0 -1
- package/dist/chunks/ADHERZLW.js +0 -2
- package/dist/chunks/D7E6EWIE.js +0 -3
- package/dist/chunks/DRYAN665.js +0 -1
- package/dist/chunks/DXFXAYDN.js +0 -17
- package/dist/chunks/EGZI4XQX.js +0 -24
- package/dist/chunks/ENWFSU27.js +0 -41
- package/dist/chunks/G6RXC3AL.js +0 -1
- package/dist/chunks/G74AGRAJ.js +0 -15
- package/dist/chunks/GQGNVSKB.js +0 -1
- package/dist/chunks/IJ2WNMD5.js +0 -7
- package/dist/chunks/JPZFKSWT.js +0 -1
- package/dist/chunks/KESCTXGO.js +0 -5
- package/dist/chunks/L3N4FIEN.js +0 -1
- package/dist/chunks/NAOUTU7A.js +0 -4
- package/dist/chunks/NDIM7MEW.js +0 -1
- package/dist/chunks/O4PPOM5Z.js +0 -2
- package/dist/chunks/OVZFGILJ.js +0 -1
- package/dist/chunks/P5ZHHOOM.js +0 -6
- package/dist/chunks/QMMLQJBS.js +0 -1
- package/dist/chunks/RK3VP2Q4.js +0 -3
- package/dist/chunks/SGGMBSBA.js +0 -1
- package/dist/chunks/TOOIB6J2.js +0 -1
- package/dist/chunks/TYTQRLTX.js +0 -1
- package/dist/chunks/UCTEU62F.js +0 -1
- package/dist/chunks/UD3VJER6.js +0 -1
- package/dist/chunks/WUUKTSRT.js +0 -7
- package/dist/chunks/X65X4KP5.js +0 -7
- package/dist/chunks/YIR5MYGU.js +0 -1
- package/dist/chunks/YLNJXQ3Z.js +0 -1
- package/dist/kit/claim.d.ts +0 -13
- package/dist/kit/drain.d.ts +0 -10
- package/dist/kit/grants.d.ts +0 -9
- package/dist/kit/scope.d.ts +0 -32
- package/dist/kit/service.d.ts +0 -7
- package/dist/kit/strategy.d.ts +0 -10
- package/dist/kit/tool.d.ts +0 -72
- package/dist/llm/door.d.ts +0 -21
- package/dist/llm/door.js +0 -1
- package/dist/loop/projections.d.ts +0 -307
- package/dist/loop/state.d.ts +0 -70
- package/dist/loop/summary.d.ts +0 -3
- package/dist/models/config/agent.d.ts +0 -18
- package/dist/models/config/prompt.d.ts +0 -18
- package/dist/models/door.js +0 -1
- package/dist/models/index.d.ts +0 -7
- package/dist/plugins/commands/foreground.d.ts +0 -4
- package/dist/plugins/compaction/preset.d.ts +0 -86
- package/dist/plugins/compaction/shared.d.ts +0 -14
- package/dist/plugins/delegations/testing.d.ts +0 -16
- package/dist/plugins/providers/door.d.ts +0 -61
- package/dist/plugins/providers/door.js +0 -1
- package/dist/plugins/providers/index.d.ts +0 -6
- package/dist/plugins/routeContext.js +0 -1
- package/dist/plugins/store/durableObject.js +0 -1
- package/dist/plugins/store/sqlite.js +0 -1
- package/dist/plugins/subAgents/tool.d.ts +0 -38
- package/dist/plugins/tools/index.d.ts +0 -59
- package/dist/plugins/tools/index.js +0 -1
- package/dist/plugins/verify/facts.d.ts +0 -356
- package/dist/plugins/workflows/submit.d.ts +0 -8
- package/dist/plugins/workspaceContext.js +0 -1
- package/dist/presets/agents.d.ts +0 -7
- package/dist/presets/base.d.ts +0 -10
- package/dist/presets/index.d.ts +0 -4
- package/dist/sandbox/index.js +0 -4
- package/dist/sandbox/policy.d.ts +0 -86
- package/dist/services/launches.d.ts +0 -104
- package/dist/services/threadDirs.d.ts +0 -38
- package/dist/testing/sandbox.d.ts +0 -71
- /package/dist/chunks/{UKXMCHHT.js → FJLU4UH3.js} +0 -0
- /package/dist/chunks/{J5S4URAW.js → HK6ZSONF.js} +0 -0
- /package/dist/plugins/ask/{askQuestion.d.ts → tools.d.ts} +0 -0
- /package/dist/plugins/delegations/{waits.d.ts → waitContracts.d.ts} +0 -0
- /package/dist/plugins/{tools → fileTools}/fsRead.d.ts +0 -0
- /package/dist/plugins/{tools → fileTools}/ripgrep.d.ts +0 -0
- /package/dist/plugins/{tools → fileTools}/textEdit.d.ts +0 -0
- /package/dist/{util → plugins/mcp}/version.d.ts +0 -0
- /package/dist/{sandbox → plugins/sandbox}/client.d.ts +0 -0
- /package/dist/{sandbox → plugins/sandbox}/files.d.ts +0 -0
- /package/dist/{sandbox → plugins/sandbox}/protocol.d.ts +0 -0
- /package/dist/{sandbox → plugins/sandbox}/worker.d.ts +0 -0
- /package/dist/{sandbox → plugins/sandbox}/worker.js +0 -0
- /package/dist/plugins/skills/{tool.d.ts → tools.d.ts} +0 -0
- /package/dist/plugins/{tools → webTools}/html.d.ts +0 -0
- /package/dist/plugins/{tools → webTools}/network.d.ts +0 -0
- /package/dist/plugins/{tools → webTools}/web.d.ts +0 -0
- /package/dist/plugins/workflows/{work.d.ts → workStatus.d.ts} +0 -0
- /package/dist/{plugins/providers → providers}/codexAuth.d.ts +0 -0
- /package/dist/services/{contributions.d.ts → catalogContributions.d.ts} +0 -0
- /package/dist/{loop → state}/limits.d.ts +0 -0
- /package/dist/{plugins/store → stores}/durableObject.d.ts +0 -0
- /package/dist/{plugins/store → stores}/durableObjectConnection.d.ts +0 -0
- /package/dist/{plugins/store → stores}/sqlite.d.ts +0 -0
- /package/dist/{models → util}/jsonSchema.d.ts +0 -0
- /package/dist/{kit → util}/retries.d.ts +0 -0
- /package/dist/{kit → util}/timers.d.ts +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leuduan/lagent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "44.0.0",
|
|
4
4
|
"description": "Plugin-based agent execution engine built on @leuduan/cordis",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Duan Leu",
|
|
@@ -15,29 +15,25 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
-
"./
|
|
19
|
-
"types": "./dist/
|
|
20
|
-
"import": "./dist/
|
|
18
|
+
"./records": {
|
|
19
|
+
"types": "./dist/records/index.d.ts",
|
|
20
|
+
"import": "./dist/records/index.js"
|
|
21
21
|
},
|
|
22
22
|
"./llm": {
|
|
23
|
-
"types": "./dist/llm/
|
|
24
|
-
"import": "./dist/llm/
|
|
23
|
+
"types": "./dist/llm/index.d.ts",
|
|
24
|
+
"import": "./dist/llm/index.js"
|
|
25
25
|
},
|
|
26
26
|
"./providers": {
|
|
27
|
-
"types": "./dist/
|
|
28
|
-
"import": "./dist/
|
|
27
|
+
"types": "./dist/providers/index.d.ts",
|
|
28
|
+
"import": "./dist/providers/index.js"
|
|
29
29
|
},
|
|
30
|
-
"./sqlite": {
|
|
31
|
-
"types": "./dist/
|
|
32
|
-
"import": "./dist/
|
|
30
|
+
"./stores/sqlite": {
|
|
31
|
+
"types": "./dist/stores/sqlite.d.ts",
|
|
32
|
+
"import": "./dist/stores/sqlite.js"
|
|
33
33
|
},
|
|
34
|
-
"./
|
|
35
|
-
"types": "./dist/
|
|
36
|
-
"import": "./dist/
|
|
37
|
-
},
|
|
38
|
-
"./sandbox": {
|
|
39
|
-
"types": "./dist/sandbox/index.d.ts",
|
|
40
|
-
"import": "./dist/sandbox/index.js"
|
|
34
|
+
"./stores/durableObject": {
|
|
35
|
+
"types": "./dist/stores/durableObject.d.ts",
|
|
36
|
+
"import": "./dist/stores/durableObject.js"
|
|
41
37
|
},
|
|
42
38
|
"./plugin": {
|
|
43
39
|
"types": "./dist/plugin.d.ts",
|
|
@@ -75,6 +71,10 @@
|
|
|
75
71
|
"types": "./dist/plugins/diagnostics/index.d.ts",
|
|
76
72
|
"import": "./dist/plugins/diagnostics/index.js"
|
|
77
73
|
},
|
|
74
|
+
"./plugins/fileTools": {
|
|
75
|
+
"types": "./dist/plugins/fileTools/index.d.ts",
|
|
76
|
+
"import": "./dist/plugins/fileTools/index.js"
|
|
77
|
+
},
|
|
78
78
|
"./plugins/historyRecall": {
|
|
79
79
|
"types": "./dist/plugins/historyRecall/index.d.ts",
|
|
80
80
|
"import": "./dist/plugins/historyRecall/index.js"
|
|
@@ -83,9 +83,13 @@
|
|
|
83
83
|
"types": "./dist/plugins/mcp/index.d.ts",
|
|
84
84
|
"import": "./dist/plugins/mcp/index.js"
|
|
85
85
|
},
|
|
86
|
-
"./plugins/
|
|
87
|
-
"types": "./dist/plugins/
|
|
88
|
-
"import": "./dist/plugins/
|
|
86
|
+
"./plugins/routeNotice": {
|
|
87
|
+
"types": "./dist/plugins/routeNotice.d.ts",
|
|
88
|
+
"import": "./dist/plugins/routeNotice.js"
|
|
89
|
+
},
|
|
90
|
+
"./plugins/sandbox": {
|
|
91
|
+
"types": "./dist/plugins/sandbox/index.d.ts",
|
|
92
|
+
"import": "./dist/plugins/sandbox/index.js"
|
|
89
93
|
},
|
|
90
94
|
"./plugins/schedules": {
|
|
91
95
|
"types": "./dist/plugins/schedules/index.d.ts",
|
|
@@ -99,9 +103,9 @@
|
|
|
99
103
|
"types": "./dist/plugins/subAgents/index.d.ts",
|
|
100
104
|
"import": "./dist/plugins/subAgents/index.js"
|
|
101
105
|
},
|
|
102
|
-
"./plugins/
|
|
103
|
-
"types": "./dist/plugins/
|
|
104
|
-
"import": "./dist/plugins/
|
|
106
|
+
"./plugins/threadDirs": {
|
|
107
|
+
"types": "./dist/plugins/threadDirs.d.ts",
|
|
108
|
+
"import": "./dist/plugins/threadDirs.js"
|
|
105
109
|
},
|
|
106
110
|
"./plugins/toolPolicy": {
|
|
107
111
|
"types": "./dist/plugins/toolPolicy/index.d.ts",
|
|
@@ -119,13 +123,17 @@
|
|
|
119
123
|
"types": "./dist/plugins/verify/index.d.ts",
|
|
120
124
|
"import": "./dist/plugins/verify/index.js"
|
|
121
125
|
},
|
|
126
|
+
"./plugins/webTools": {
|
|
127
|
+
"types": "./dist/plugins/webTools/index.d.ts",
|
|
128
|
+
"import": "./dist/plugins/webTools/index.js"
|
|
129
|
+
},
|
|
122
130
|
"./plugins/workflows": {
|
|
123
131
|
"types": "./dist/plugins/workflows/index.d.ts",
|
|
124
132
|
"import": "./dist/plugins/workflows/index.js"
|
|
125
133
|
},
|
|
126
|
-
"./plugins/
|
|
127
|
-
"types": "./dist/plugins/
|
|
128
|
-
"import": "./dist/plugins/
|
|
134
|
+
"./plugins/workspaceNotice": {
|
|
135
|
+
"types": "./dist/plugins/workspaceNotice.d.ts",
|
|
136
|
+
"import": "./dist/plugins/workspaceNotice.js"
|
|
129
137
|
},
|
|
130
138
|
"./package.json": "./package.json"
|
|
131
139
|
},
|
|
@@ -155,7 +163,7 @@
|
|
|
155
163
|
"esbuild": "0.28.1",
|
|
156
164
|
"typescript": "^6.0.3",
|
|
157
165
|
"vitest": "4.1.5",
|
|
158
|
-
"@
|
|
166
|
+
"@leuduan/tsconfig": "1.0.0"
|
|
159
167
|
},
|
|
160
168
|
"repository": {
|
|
161
169
|
"type": "git",
|
package/dist/chunks/2O7ZGEIT.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{b as wt}from"./4XVRUSCQ.js";import{a as mt}from"./UD3VJER6.js";import{a as lt}from"./SGGMBSBA.js";import{a as ct}from"./3QCW4WDJ.js";import{a as ft}from"./RK3VP2Q4.js";import{a as dt,b as ut}from"./74HKUGRR.js";import{a as pt}from"./YIR5MYGU.js";import{a as be}from"./NDIM7MEW.js";import{a as j,b as le}from"./34KW6VNQ.js";import{e as g}from"./G6RXC3AL.js";import{d as ht}from"./GQGNVSKB.js";import{b as ot,c as at}from"./2OSESBIL.js";import{$ as Ze,A as Ne,B as We,D as re,F as Ge,G as k,I as x,J as b,K as Je,L as P,M as Ve,N as Ke,O as $,P as F,Q as ie,R as Qe,T as Xe,V as ne,W as M,X as L,Y as O,Z as Ye,_ as se,aa as Ie,b as _e,ba as et,ca as B,d as ke,da as tt,e as qe,ea as rt,f as T,fa as it,g as xe,ga as nt,k as Re,l as $e,m as Fe,ma as st,n as Me,o as h,p as q,q as Le,s as v,t as y,u as Oe,v as Be,x as He,y as ze,z as Ue}from"./YLNJXQ3Z.js";import{a as w,f as oe,h as ae}from"./DDANMY45.js";import{a as m,c as Te,d as _,g as A,k as Ce,l as Ae,n as De,o as D,p as Pe,r as ee,s as te}from"./37YOGDGY.js";import{d as C,j as je,l as Se,n as Ee}from"./KESCTXGO.js";import{b as we,c as ge,e as I,g as ye}from"./TYTQRLTX.js";import{k as ve}from"./D7E6EWIE.js";import{Service as Ct}from"@leuduan/cordis";import{z as H}from"zod";var At=H.strictObject({maxAttempts:H.number().int().min(1).max(100),baseDelayMs:H.number().int().min(0).max(le),maxDelayMs:H.number().int().min(0).max(le)}).refine(a=>a.baseDelayMs<=a.maxDelayMs,{message:"baseDelayMs must not exceed maxDelayMs"}),Dt=new Set(["rate_limit","overloaded","transport","timeout","empty"]),gt=Symbol("register"),Zt=(a,e,t)=>{let r=At.parse(t.retry);return a.effect(()=>a.llm[gt](e,r))},z=class extends Ct{providers=new Map;policies=new Map;clients=new Map;constructor(e){super(e,"llm"),e.effect(()=>()=>this.disposeClients(this.detachClients(()=>!0)))}get providerNames(){return C(this.providers.keys())}[gt](e,t){if(this.providers.has(e.name))throw new Error(`LLM provider already registered for name: ${e.name}`);return this.providers.set(e.name,e),this.policies.set(e.name,t),async()=>{this.providers.get(e.name)===e&&(this.providers.delete(e.name),this.policies.delete(e.name),await this.disposeClients(this.detachClients(r=>r===e.name)))}}provider(e){let t=this.providers.get(e);if(t===void 0)throw new x({provider:e});return t}unavailable(e){return this.providers.has(e.provider)?null:new x(e).message}resolveRoute(e,t){let r=e.thinkingEffort,i=r==null?r:ve.parse(r);if(e.provider===void 0||e.provider===t.provider)return this.provider(t.provider),{provider:t.provider,model:e.model??t.model,thinking_effort:i===void 0?t.thinking_effort:i};let n=ye(this.provider(e.provider),e.model,i??void 0);return i===null?{...n,thinking_effort:null}:n}capacity(e){return this.provider(e.provider).capacity(e.model)}clientFor(e){let t=this.provider(e.provider),r=this.clients.get(t.name);return r===void 0&&(r=new de(t,t.createClient(),this.policies.get(t.name)),this.clients.set(t.name,r)),r}detachClients(e){let t=[];for(let[r,i]of this.clients)e(r)&&(this.clients.delete(r),t.push(i));return t}async disposeClients(e){let r=(await Promise.allSettled(e.map(async({inner:i})=>i.dispose?.()))).flatMap(i=>i.status==="rejected"?[i.reason]:[]);if(r.length)throw new AggregateError(r,"LLM client cleanup failed")}},de=class{providerName;provider;inner;policy;constructor(e,t,r){this.provider=e,this.inner=t,this.policy=r,this.providerName=t.providerName}call(e){return new ue(this,e)}failure(e,t){let{kind:r,retryAfterMs:i}=ge(this.provider,e);return new we(this.provider.name,r,t,i??null,{cause:e})}},ue=class{response;calls=0;current;yielded=!1;claimed=!1;entered=!1;ledger=new be;outcome;client;request;constructor(e,t){this.client=e,this.request=t,this.response=this.attempt().then(r=>(this.current=r,this))}get attempts(){return this.calls}[Symbol.asyncIterator](){this.claim();let e=this.read(!0),t,r=()=>this.entered?Promise.resolve():(t??=(async()=>{try{await this.current?.[Symbol.asyncIterator]().return?.()}catch(i){let n=this.final(i);throw this.outcome={error:n},n}finally{this.ledger.finish()}})(),t);return{next:()=>this.ledger.operation(()=>e.next()),return:i=>this.ledger.operation(async()=>{let n=e.return(i);return await r(),n}),throw:i=>this.ledger.operation(async()=>{let n=e.throw(i);return n.catch(()=>{}),await r(),n})}}claim(){if(this.claimed)throw new Error("A model call is read once: it has been claimed already");this.claimed=!0}async*read(e){this.entered=!0;try{for(;;)try{for await(let t of this.current)e&&(this.yielded=!0,yield t);this.outcome={response:await this.current.completed()};return}catch(t){try{await this.recover(t),this.current=await this.attempt()}catch(r){throw this.outcome={error:r},r}}}finally{this.ledger.finish()}}async completed(){if(this.claimed){if(!this.ledger.ended)throw new Error("A model call is settled before its read or after it, not while the read is under way")}else{this.claim();for await(let e of this.read(!1));}if(this.outcome!==void 0){if("error"in this.outcome)throw this.outcome.error;return this.outcome.response}try{let e=await this.current.completed();return this.outcome={response:e},e}catch(e){let t=this.final(e);throw this.outcome={error:t},t}}final(e){return ae(this.request.signal,e)?e:this.client.failure(e,this.calls)}async attempt(){for(;;){this.request.signal.throwIfAborted(),this.calls+=1;try{return await this.client.inner.chat(this.request)}catch(e){await this.recover(e)}}}async recover(e){let{signal:t}=this.request;if(ae(t,e))throw e;let r=this.calls,i=this.client.failure(e,r),{maxAttempts:n,baseDelayMs:s,maxDelayMs:o}=this.client.policy;if(!(!t.aborted&&!this.yielded&&this.request.providerTools.length===0&&Dt.has(i.kind)&&r<n&&(i.retryAfterMs===null||i.retryAfterMs<=o)))throw i;let l=Math.min(o,s*2**(r-1));await Pt(i.retryAfterMs??Math.floor(Math.random()*(l+1)),t)}};function Pt(a,e){return new Promise((t,r)=>{if(e.aborted){r(e.reason);return}let i=()=>{clearTimeout(n),r(e.reason)},n=setTimeout(()=>{e.removeEventListener("abort",i),t()},a);e.addEventListener("abort",i,{once:!0})})}var $t={releases:!1,chain:"person"},U=class{inbox;service;admission;id;journal;constructor(e,t,r,i){this.service=e,this.admission=t,this.id=r,this.journal=i,this.inbox=new P(i,r)}async read(e){let t=await this.service.retain(()=>[this.id],{lastRelease:"keep"});try{let r=e(this.service.view(this.id));if(typeof r?.then=="function")throw new TypeError("thread.read takes a synchronous reader; to read across awaits, hold the thread with ctx.threads.retain");return r}finally{t.release()}}get ctx(){return this.service.contextOf(this.id)}get signal(){return this.service.signalOf(this.id)}whenReady(){return this.service.whenReady(this.id)}configure(e){return this.service.configure(this.id,e)}close(){return this.service.close(this.id)}get summary(){let e=this.journal.summary(this.id);if(e===void 0)throw new ReferenceError(`Unknown thread: ${this.id}`);return e}get agentName(){return this.summary.agent_name}get metadata(){return this.summary.metadata}get origin(){return this.summary.origin}get depth(){return this.journal.origins.callers(this.id).threads.filter(e=>e.origin!==null).length}get originRoot(){let{threads:e,chain:t}=this.journal.origins.callers(this.id);return t==="complete"&&e.length>0?e[e.length-1].thread_id:null}get environment(){return this.summary.environment}get forkedFrom(){let{fork:e}=this.summary;return e===null?null:{threadId:e.thread_id,seq:e.seq}}get lineageRoot(){return Te(this.journal.summaryMap(),this.id).at(-1)?.thread_id??this.id}lastOutcome(){return this.summary.last_reason}fork(e){return this.service.fork(this.id,e)}rebuildPrompt(){return this.service.rebuildPrompt(this.id)}on(e){return this.journal.subscribe(t=>t.thread_id===this.id?e(t):void 0)}onChunk(e){return this.service.chunks.subscribe(t=>{t.thread_id===this.id&&e(t)})}send(e,t={}){return this.inbox.send(e,()=>this.admission.inputTarget(this.id),t,this.service.routes)}withdraw(e,t={}){return this.inbox.withdraw(e,t)}async answer(e,t={}){await this.journal.transact(this.id,()=>{let{confirmations:r}=this.journal.state(h,this.id),i=new Set(T(r,this.id).map(o=>o.request_id)),n=new Set;for(let{request_id:o}of e){if(n.has(o))throw new Error(`A batch of answers names request_id=${o} twice on thread ${this.id}`);if(n.add(o),i.has(o))continue;let d=r.get(o)?.resolution;throw d!=null?new Ne(this.id,o,d):new Error(`Unknown pending confirmation request_id=${o} on thread ${this.id}`)}let s=t.wake===!0?b(this.id,this.pendingRun(),$t,null).rows:[];return{rows:[...vt(this.id,e),...s],value:void 0}})}async resume(e={}){let t=e.route===void 0?{}:{route:e.route},{value:r}=await this.journal.transact(this.id,()=>{let i=b(this.id,this.pendingRun(),t,null,this.service.routes);return{rows:i.rows,value:i.runId}});return r}closeWaitRows(e){return ke(this.journal.state(h,this.id).waits).map(({declaration:t})=>{let r=this.journal.eventAt(t.call_seq);if(r?.type!=="tool/call")throw new Error(`The wait at ${t.seq} on thread ${this.id} cites no call at ${t.call_seq}`);return{type:"tool/result",data:{turn:t.turn,step:t.step,result:{call_id:t.call_id,name:r.data.name,output:`The call was cancelled while it waited: ${e}`,is_error:!0,notes:null,metadata:{wait_cancelled:!0}}},options:{threadId:this.id,sourceSeqs:[t.call_seq,t.seq]}}})}pendingRun(){return this.journal.state(h,this.id).requests.pending}async revokeApprovals(e,t=null){await this.journal.transact(this.id,()=>{if(this.admission.isRunning(this.id))throw new y(this.id);let r=new Map(F(this.journal,this.id).map(n=>[n.request_id,n])),i=[...new Set(e)];for(let n of i)if(r.get(n)?.resolution?.approved!==!0)throw new Error(`No approved, unfinished confirmation request_id=${n} on thread ${this.id}`);return{rows:vt(this.id,i.map(n=>({request_id:n,approved:!1,rejection_message:t}))),value:void 0}})}compact(e={}){return this.service.compact(this.id,e)}async cancel(e,t){let{intent:r}=t,i=this.admission.cancel(this.id,e,r),n=this.journal.transact(this.id,()=>{let s=Ie(this.pendingRun(),i.capturedSettles()?i.capturedRunId:null);return{rows:[{type:"thread/cancel",data:{cancel_id:k("cancel"),intent:r,run_id:i.capturedRunId,reason:e},options:{threadId:this.id}},...this.inbox.clearRows(),...this.closeWaitRows(e),...t.rows?.(this.id)??[],...s===null?[]:[{type:"run/settled",data:{run_id:s,outcome:O(r),reason:"cancel",error_text:null,through:this.journal.head,residual:[]},options:{threadId:this.id}}]],value:void 0}});if(i.wrote(n),t.until==="recorded"){await n;return}try{await n}finally{await i.settled}}},vt=(a,e)=>e.map(t=>({type:"confirm/resolved",data:{request_id:t.request_id,approved:t.approved,rejection_message:t.rejection_message??null},options:{threadId:a}}));import{mkdirSync as Ft}from"node:fs";import{join as yt}from"node:path";import{Service as Mt}from"@leuduan/cordis";var N=class extends Mt{dataDir;root;constructor(e,t){super(e,"threadDirs"),this.dataDir=t.dataDir===null?null:g(t.dataDir,"The data directory"),this.root=this.dataDir===null?null:yt(this.dataDir,"threads"),this.root!==null&&e.on("lagent/thread/activate",r=>{this.ensure(r.id)})}require(e){if(this.dataDir===null)throw new Error(`${e} needs thread directories; core was mounted with \`dataDir: null\``);return this.dataDir}dirFor(e){if(this.root===null)throw new Error("Thread directories are off; core was mounted with `dataDir: null`");let t=encodeURIComponent(e);if(t===""||t==="."||t==="..")throw new Error(`Invalid thread id: ${e}`);return yt(this.root,t)}ensure(e){let t=this.dirFor(e);return Ft(t,{recursive:!0,mode:448}),t}};import{Service as Ht}from"@leuduan/cordis";var W=class{admitted=new Map;cancels=new Map;listeners=new Set;closing;report;constructor(e,t){this.closing=e,this.report=t}requireFree(e){this.closing.throwIfAborted();let t=this.admitted.get(e);if(t!==void 0)throw new y(e,t.kind)}admit(e,t,r={}){this.closing.throwIfAborted(),r.signal?.throwIfAborted(),this.requireFree(e);let i=Promise.withResolvers(),n=this.operation(e,t,i,r);return this.admitted.set(e,n),this.publish({thread_id:e,kind:t,phase:"admitted"}),n}operation(e,t,r,i){let n=new AbortController,s=this.admitted,o={threadId:e,kind:t,runId:i.runId??null,controller:n,signal:AbortSignal.any([this.closing,n.signal,...i.signal?[i.signal]:[]]),settled:r.promise,settle:r.resolve,releasing:t==="configure",releasingStarted:Promise.withResolvers(),cancelIntent:null,settlementBuilt:!1,lease:{get held(){return s.get(e)===o}}};return o}async hold(e,t,r={}){let i=this.holding(e),n=!1;try{return await t()}catch(s){throw n=!0,s}finally{try{let l=r.cleanup?.();l!==void 0&&await l}catch(l){this.report(l)}if(r.finalize!==void 0)for(this.setReleasing(i,!0);;){let l=i.cancelIntent;if(await Promise.resolve().then(r.finalize).catch(this.report),i.cancelIntent===l)break}let{threadId:s}=i;this.admitted.get(s)===i&&this.admitted.delete(s);let o=this.cancels.get(s);o?.captured===i&&(o.captured=null,o.writes>0&&this.holdForCancel(s,o)),i.settle();let d=i.kind!=="release"&&i.signal.aborted?"cancelled":n?"failed":"completed";this.publish({thread_id:s,kind:i.kind,phase:"settled",ended:d}),o?.operation!=null&&this.publish({thread_id:s,kind:"cancel",phase:"admitted"}),o!==void 0&&this.finishCancel(s,o)}}setReleasing(e,t){let r=this.holding(e);r.releasing!==t&&(r.releasing=t,t?r.releasingStarted.resolve():r.releasingStarted=Promise.withResolvers())}cancel(e,t,r){let i=this.cancels.get(e);i===void 0&&(i={captured:null,writes:0,operation:null,failed:!1,done:Promise.withResolvers()},this.cancels.set(e,i));let n=this.admitted.get(e),s=n===void 0;s?this.holdForCancel(e,i):n!==i.operation&&(i.captured=n,n.cancelIntent??=r),i.writes+=1;let o=i,d=o.captured;return{capturedRunId:d?.runId??null,settled:o.done.promise,capturedSettles:()=>d!==null&&!d.settlementBuilt,wrote:l=>{l.then(()=>this.wroteCancel(e,o,!1),()=>this.wroteCancel(e,o,!0)),s&&this.publish({thread_id:e,kind:"cancel",phase:"admitted"}),d!==null&&d.kind!=="release"&&d.controller.abort(new Error(t))}}}cancelIntent(e){return e.cancelIntent}settlementBuilt(e){e.settlementBuilt=!0}holdForCancel(e,t){let r=this.operation(e,"cancel",t.done,{});t.operation=r,this.admitted.set(e,r)}wroteCancel(e,t,r){t.writes-=1,r&&(t.failed=!0),this.finishCancel(e,t)}finishCancel(e,t){if(t.writes>0||t.captured!==null)return;this.cancels.get(e)===t&&this.cancels.delete(e);let{operation:r}=t;if(r!==null&&this.admitted.get(e)===r&&this.admitted.delete(e),t.done.resolve(),r===null)return;let i=t.failed?"failed":"completed";this.publish({thread_id:e,kind:"cancel",phase:"settled",ended:i})}holder(e){let t=this.admitted.get(e);return t===void 0?null:Object.freeze({kind:t.kind,runId:t.runId})}isAdmitted(e){return this.admitted.has(e)}isRunning(e){let t=this.admitted.get(e)?.kind;return t==="run"||t==="compact"}isReleasing(e){let t=this.admitted.get(e);return t?.kind==="release"||t?.releasing===!0}inputTarget(e){let t=this.admitted.get(e);return t?.kind==="run"&&!t.signal.aborted&&!t.releasing?"next-step":"next-turn"}settlement(e){return this.admitted.get(e)?.settled??Promise.resolve()}async whenSettled(e){for(;;){let t=this.admitted.get(e);if(t===void 0||this.isReleasing(e))return;await Promise.race([t.settled,t.releasingStarted.promise])}}async drain(){for(;;){let e=[...[...this.admitted.values()].map(t=>t.settled),...[...this.cancels.values()].map(t=>t.done.promise)];if(e.length===0)return;await Promise.all(e)}}onOperation(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}holding(e){let t=this.admitted.get(e.threadId);if(t!==e)throw new Error(`The operation does not hold thread ${e.threadId}`);return t}publish(e){this.listeners.size!==0&&A(this.listeners,m(structuredClone(e)),this.report)}};var G=class{started=!1;starting;limit=Number.POSITIVE_INFINITY;counted=0;startupDeclined=new Set;owners=new Map;nextSerial=0;dirty=new Set;scheduled=!1;admitting=new Set;preparations=new Set;executing=new Map;waiting=[];holds=new Map;listeners=new Set;preparation=new Map;host;limits;constructor(e,t){this.host=e,this.limits=t}async start(e){if(this.starting!==void 0)throw new Error("ctx.threads.start was already called");let{resume:t,startLimit:r}=e;if(!Number.isSafeInteger(r)||r<1)throw new Error("startLimit must be a positive integer");let i=Promise.withResolvers();this.starting=i.promise,this.limit=r,await Promise.all([...this.owners.values()].map(s=>this.repair(s))),await this.host.journal.flush();let n=Date.now();for(let s of this.host.journal.summaries()){let o=s.pending_run;o!==null&&Lt(t,o.newest_cause_at,n)&&this.startupDeclined.add(s.thread_id)}this.started=!0,i.resolve();for(let s of this.host.journal.summaries())s.pending_run!==null&&this.schedule(s.thread_id)}registerOwner(e){if(this.owners.has(e.owner))throw new Error(`An owner is already registered for ${e.owner}`);let t={registration:e,serial:this.nextSerial+=1,state:e.repair===void 0?"ready":"repairing"};this.owners.set(e.owner,t),this.scheduleRequiring(e.owner),this.starting!==void 0&&this.starting.then(()=>this.repair(t));let r=()=>this.owners.get(e.owner)===t;return{changed:i=>{r()&&(this.holds.delete(i),this.forgetPreparation(i),this.schedule(i))},resolveWait:(i,n)=>r()?this.resolveWait(e.owner,r,i,n):Promise.reject(new Error(`The registration for ${e.owner} was disposed`)),dispose:()=>{if(r()){this.owners.delete(e.owner),this.scheduleRequiring(e.owner);for(let[i,n]of this.holds)n.hold.reason==="constraint"&&n.hold.owner===e.owner&&(this.holds.delete(i),this.schedule(i))}}}}async repair(e){let{registration:t}=e;if(t.repair===void 0)e.state="ready";else{e.state="repairing";try{await t.repair(),e.state="ready"}catch(r){this.host.report(r),e.state={failed:w(r)}}}this.scheduleRequiring(t.owner)}scheduleRequiring(e){let{journal:t}=this.host,r=new Set;for(let i of t.summaries()){if(!r.has(i.thread_id)&&i.requires.includes(e))for(let s of t.origins.subtree(i.thread_id,{self:!0}))r.add(s.thread_id);!i.waits.some(s=>s.owner===e)&&!r.has(i.thread_id)||(this.forgetDecided(i.thread_id,"missing"),this.forgetDecided(i.thread_id,"constraint"),this.forgetDecided(i.thread_id,"wait"),this.forgetPreparation(i.thread_id),this.schedule(i.thread_id))}}isRegistered(e){return this.owners.has(e)}async resolveWait(e,t,{threadId:r,callSeq:i},n){let{journal:s}=this.host;if(s.summary(r)===void 0)return{status:"unknown"};let{value:o}=await s.transact(r,()=>{if(!t())throw new Error(`The registration for ${e} was disposed`);let d=s.state(h,r),l=d.waits.get(i);if(l===void 0||l.declaration.owner!==e)return{rows:[],value:{status:"unknown"}};if(l.value!==null)return{rows:[],value:{status:"resolved-before",value:l.value}};if(l.answered)return{rows:[],value:{status:"closed"}};let u=n(l.declaration);if(u===null)return{rows:[],value:{status:"declined"}};let c={releases:!1,chain:u.chain??"reactive"};return{rows:[{type:"wait/resolved",data:{call_seq:i,value:u.value},options:{threadId:r,sourceSeqs:[l.declaration.seq,...u.evidence??[]]}},...b(r,d.requests.pending,c,null).rows],value:{status:"resolved"}}});return o}onRow(e,t){if(t.type==="run/cause"&&t.data.releases){this.forgetPreparation(e);for(let r of this.host.journal.origins.subtree(e,{self:!0}))this.startupDeclined.delete(r.thread_id)&&(this.forgetDecided(r.thread_id,"startup"),this.forgetPreparation(r.thread_id),this.schedule(r.thread_id))}if(t.type==="run/requested"&&this.publish({threadId:e,runId:t.data.run_id,state:"requested"}),t.type==="run/settled"){let r=t.data.run_id;this.executing.get(e)?.runId!==r&&this.publishState(e,r)}for(let r of this.host.journal.origins.callers(e).threads)this.holds.delete(r.thread_id),this.schedule(r.thread_id)}onSettled(e,t){let r=this.executing.get(e);t==="run"&&r!==void 0&&(this.executing.delete(e),this.afterExecution(e,r)),this.schedule(e)}async whenIdle(){await Promise.all([...this.preparations])}close(){for(let e of[...this.preparation.keys()])this.forgetPreparation(e);for(let e of this.host.journal.summaries()){let t=e.pending_run;t!==null&&this.executing.get(e.thread_id)?.runId!==t.run_id&&this.publish({threadId:e.thread_id,runId:t.run_id,state:"held",hold:{reason:"shutdown"},basis:"catalog"})}}onRunEvent(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}runState(e,t){if(this.executing.get(e)?.runId===t)return{state:"running"};let r=this.durable(e,t);if(r.state==="pending"&&this.host.signal.aborted)return{state:"held",hold:{reason:"shutdown"},basis:"catalog"};let i=r.state==="pending"?this.host.journal.summary(e):void 0,n=i===void 0?null:this.idleHold(i,t,this.approvalBlocked(i));return mt({durable:r,executing:!1,held:n})}inspect(e,t){let r=this.host.journal.summary(e);if(r===void 0)return null;t==="loaded"&&this.requireLoaded(e);let i=this.host.admission.holder(e),n=i===null||i.kind==="run"?null:i.kind,s=this.approvalBlocked(r),o=(u,c=null)=>({state:u,operation:n,hold:c,missing:c?.reason==="missing"?this.missingDetail(e):null,approvalBlocked:s,basis:t});if(this.executing.has(e))return o("running");let d=r.pending_run;if(d===null)return o("idle");let l=t==="loaded"?this.loadedHold(r,d.run_id):this.idleHold(r,d.run_id,s)?.hold??null;return l===null?o("requested"):o("held",l)}requireLoaded(e){let{journal:t}=this.host;for(let{thread_id:r}of t.origins.callers(e).threads)if(!t.isLoaded(r))throw new Error(`Thread ${e} cannot be inspected on the loaded basis: thread ${r} is not loaded`)}loadedHold(e,t){if(e.pending_run?.run_id!==t||this.admitting.has(e.thread_id))return null;let r=this.entryDecision(e.thread_id);return r.kind==="hold"?r.hold:null}approvalBlocked(e){return e.last_reason?.kind!=="blocked"?!1:this.host.journal.origins.subtree(e.thread_id,{self:!0}).some(t=>t.unanswered_confirmations.length>0)}idleHold(e,t,r){let i=e.thread_id,n=this.holds.get(i);if(n?.runId===t)return{hold:n.hold,basis:n.basis};if(e.pending_run?.run_id!==t||this.admitting.has(i))return null;let s=this.verdict(e);if(s.kind==="obsolete"||e.pending_run.cancelled)return null;let o=this.catalogHold(e,r);return o!==null?{hold:o,basis:"catalog"}:s.kind==="hold"?{hold:se(s),basis:"catalog"}:null}declined(e){return this.startupDeclined.has(e)}missing(e){let{owners:t,chain:r}=this.missingDetail(e);return[...r==="complete"?[]:[`the callers above ${r.at??e} cannot be read`],...t.map(({owner:n,state:s})=>s==="absent"?`${n} is not registered`:s==="repairing"?`${n} is repairing`:`${n} failed to repair: ${s.failed}`)]}missingDetail(e){let{owners:t,chain:r}=this.host.journal.origins.requiredOwners(e);return{chain:r,owners:t.flatMap(({owner:i,introducedBy:n})=>{let s=this.owners.get(i)?.state;return s==="ready"?[]:[{owner:i,state:s??"absent",introducedBy:n}]})}}requiredRegistrations(e){if(this.missing(e).length>0)return null;let{owners:t}=this.host.journal.origins.requiredOwners(e);return JSON.stringify(t.map(({owner:r})=>r).sort().map(r=>[r,this.owners.get(r).serial]))}verdict(e){let t=null;for(let{registration:r,state:i}of this.owners.values()){if(i!=="ready")continue;let n;try{let{constraint:s}=r;if(s?.applies(e)!==!0)continue;n=s.evaluate(e.thread_id)}catch(s){n={kind:"hold",reason:`${r.owner} could not decide: ${w(s)}`}}if(n.kind==="obsolete")return n;n.kind==="hold"&&(t??={...n,owner:r.owner})}return t??{kind:"allow"}}decide(e,{cancel:t,ending:r,start:i}){let{journal:n}=this.host,s=n.summary(e);if(s===void 0)throw new v(e);let o=n.state(h,e),d=o.requests.pending;if(d===null)throw new Error(`Thread ${e} has no pending run to decide for`);let l=n.origins.subtree(e,{self:!0}),u=o.requests.pendingCancel,c=o.inbox;return Ze({pending:d,ending:r,repairsLeft:Ye(o.requests),work:{hasWork:Ve(n,e,this.limits),nextStepQueued:c["next-step"].length>0,anyQueued:c["next-step"].length+c["next-turn"].length>0,unanswered:qe(o.surface.entries.map(f=>f.message)).length>0},answeredUnder:i!==null&&l.some(f=>f.answered_at!==null&&f.answered_at>i),holds:this.derivedHolds(e,o,l),constraint:this.verdict(s),cancel:t??(u?.runId===d.runId?u.intent:null)})}entryDecision(e){return this.decide(e,{cancel:null,...B(this.host.journal.state(h,e))})}derivedHolds(e,t,r){let i=[...t.waits.values()].find(_e)?.declaration;return ne({approval:t.turnBoundary.lastReason?.kind==="blocked"&&r.some(n=>n.unanswered_confirmations.length>0),failed:M(t.requests),cancelled:L(t.requests),startupDeclined:this.declined(e),missing:this.missing(e),unresolvedWait:i===void 0?null:{callSeq:i.call_seq,reason:i.reason,owner:i.owner,deadlineAt:i.deadline_at,registered:this.isRegistered(i.owner)}})}schedule(e){this.dirty.add(e),!this.scheduled&&(this.scheduled=!0,queueMicrotask(()=>{this.scheduled=!1;let t=[...this.dirty];this.dirty.clear();for(let r of t)this.consider(r)}))}consider(e){if(!this.started||this.host.signal.aborted||this.admitting.has(e)||this.executing.has(e))return;let t=this.host.journal.summary(e),r=t?.pending_run??null;if(t===void 0||r===null){this.holds.delete(e),this.forgetPreparation(e);return}if(this.preparation.get(e)?.runId!==r.run_id&&this.forgetPreparation(e),this.host.admission.isAdmitted(e))return;let i=this.verdict(t);if(!(i.kind==="obsolete"||r.cancelled)){let o=this.catalogHold(t,this.approvalBlocked(t));if(o!==null&&o.reason!=="approval")return this.hold(e,r.run_id,o,{decided:!1});if(o===null&&i.kind==="hold")return this.hold(e,r.run_id,se(i),{decided:!1});let d=this.holds.get(e);if(d?.runId===r.run_id&&d.decided)return}if(this.holds.delete(e),this.preparation.get(e)?.retryAt!=null)return;if(this.counted>=this.limit){this.waiting.includes(e)||this.waiting.push(e);return}if(this.host.signal.aborted)return;let s=this.admit(e).catch(o=>this.host.report(o));this.preparations.add(s),s.then(()=>this.preparations.delete(s))}catalogHold(e,t){let r=e.pending_run;if(r===null)return null;let i=e.waits.find(s=>!s.resolved),[n]=ne({approval:t,failed:e.latest_settlement?.outcome==="failed"&&!r.releasing,cancelled:e.latest_user_cancel!==null&&!e.latest_user_cancel.released,startupDeclined:this.startupDeclined.has(e.thread_id),missing:this.missing(e.thread_id),unresolvedWait:i===void 0?null:{callSeq:i.call_seq,reason:i.reason,owner:i.owner,deadlineAt:i.deadline_at,registered:this.isRegistered(i.owner)}});return n??null}async admit(e){let{admission:t,journal:r}=this.host;this.admitting.add(e),this.counted+=1;let i,n;try{if(i=await this.host.retain(e),this.host.signal.aborted||t.isAdmitted(e))return;let s=r.state(h,e).requests.pending;if(s===null){this.forgetPreparation(e);return}let o=this.entryDecision(e);if(this.forgetPreparation(e),o.kind==="hold"){o.hold.reason!=="shutdown"&&this.hold(e,s.runId,o.hold,{decided:!0});return}this.holds.delete(e),this.admitting.delete(e);let d;n={runId:s.runId,attemptCutoff:r.head,final:null,errorText:null,decide:f=>this.decide(e,{cancel:d===void 0?null:t.cancelIntent(d),...f})},this.executing.set(e,n),d=t.admit(e,"run",{runId:s.runId});let l=d,u=n,c=t.hold(l,()=>this.host.execute(u,l),{finalize:()=>this.finalize(l,u),cleanup:()=>this.host.cleanup(l)});this.publish({threadId:e,runId:s.runId,state:"running"}),await c.catch(f=>this.host.report(f))}catch(s){this.host.report(s),n===void 0&&this.retryLater(e)}finally{this.admitting.delete(e),n!==void 0&&this.executing.get(e)===n&&(this.executing.delete(e),this.afterExecution(e,n)),this.counted-=1,i?.release(),n!==void 0&&this.schedule(e),this.drain()}}async finalize(e,t){let{admission:r,journal:i}=this.host,{threadId:n}=e;if(!i.isLoaded(n)){r.settlementBuilt(e);return}await i.transact(n,()=>{r.settlementBuilt(e);let s=i.state(h,n),o=s.requests.pending;if(o?.runId!==t.runId)return{rows:[],value:void 0};let d=s.requests.pendingCancel?.runId===o.runId?s.requests.pendingCancel:null,l=r.cancelIntent(e)??d?.intent??null,u=l===null?t.final:{kind:"settle",outcome:O(l),reason:"cancel"};if(u?.kind!=="settle")return{rows:[],value:void 0};let c=Me(o,{outcome:u.outcome,reason:u.reason,attemptCutoff:t.attemptCutoff,cancelSeq:d?.seq??null},f=>$(s,f).state);return{rows:[{type:"run/settled",data:{run_id:o.runId,outcome:u.outcome,reason:u.reason,error_text:u.reason==="setup"?t.errorText??"the run failed before a turn":null,through:i.head,residual:c},options:{threadId:n}},...c.length===0?[]:[{type:"run/requested",data:{run_id:k("run")},options:{threadId:n}}]],value:void 0}})}retryLater(e){let t=this.host.journal.summary(e)?.pending_run?.run_id;if(this.host.signal.aborted||t===void 0)return;let r=this.preparation.get(e);r?.timer!=null&&clearTimeout(r.timer);let i=r?.runId===t?r.attempts+1:1,n=Math.min(6e4,500*2**(i-1)),s={runId:t,attempts:i,retryAt:Date.now()+n,timer:null};s.timer=setTimeout(()=>{this.preparation.get(e)===s&&(s.retryAt=null,s.timer=null,this.schedule(e))},n),s.timer.unref?.(),this.preparation.set(e,s)}forgetPreparation(e){let t=this.preparation.get(e);t!==void 0&&(t.timer!==null&&clearTimeout(t.timer),this.preparation.delete(e))}afterExecution(e,{runId:t,final:r}){let i=this.durable(e,t);if(i.state==="settled")return this.publishState(e,t);if(i.state!=="pending")return;if(this.host.signal.aborted)return this.publish({threadId:e,runId:t,state:"held",hold:{reason:"shutdown"},basis:"catalog"});if(r?.kind==="hold"&&r.hold.reason!=="shutdown")return this.hold(e,t,r.hold,{decided:!1,always:!0,basis:"loaded"});let n;try{n=this.entryDecision(e)}catch(s){this.host.report(s);return}n.kind==="hold"&&n.hold.reason!=="shutdown"&&this.hold(e,t,n.hold,{decided:!0,always:!0})}hold(e,t,r,i){let n=this.holds.get(e),s=i.basis??(i.decided?"loaded":"catalog");this.holds.set(e,{runId:t,hold:r,decided:i.decided,basis:s}),!(i.always!==!0&&n?.runId===t&&Xe(n.hold,r))&&this.publish({threadId:e,runId:t,state:"held",hold:r,basis:s})}forgetDecided(e,t){let r=this.holds.get(e);r?.decided===!0&&r.hold.reason===t&&this.holds.delete(e)}drain(){for(;this.counted<this.limit&&this.waiting.length>0;){let e=this.waiting.shift();this.consider(e)}}durable(e,t){let{journal:r}=this.host;if(r.isLoaded(e))return Fe(r.state(h,e).requests,t);let i=r.summary(e);if(i?.pending_run?.run_id===t)return{state:"pending"};let n=i?.latest_settlement;return n?.run_id!==t||n===void 0||n===null?{state:"unknown"}:{state:"settled",settlement:{runId:t,seq:0,outcome:n.outcome,reason:n.reason,errorText:n.error_text,through:0,residual:[]}}}publishState(e,t){let r=this.runState(e,t);r!==null&&this.publish({threadId:e,runId:t,...r})}publish(e){A([...this.listeners],Object.freeze(e),t=>this.host.report(t))}};function Lt(a,e,t){return a==="all"?!1:a==="none"||e===null?!0:t-Date.parse(e)>a.newerThanMs}var S=(a,e)=>_(a)===_(e);function he(a){return[...new Set(a.map(e=>g(e,"A read root")))]}var E=a=>[...new Set(a??[])].sort();function J(a,e){let{agent:t,origin:r,birth:i,reuse:n}=e;if(i.from==="exact"){let{spec:l}=i;return{agent:t,origin:r,spec:{route:I.parse(l.route),environment:{cwd:g(l.environment.cwd,"A thread cwd"),read_roots:he(l.environment.read_roots)},installations:a.presets.initialInstallations(l.installations.map(({installation_id:u,preset_id:c,config:f,lifetime:p})=>({presetId:c,config:f,installationId:u,lifetime:p})),t),requires:E(l.requires)}}}let s=i.installations??[];if(n==="configuration"){let l=s.find(u=>u.installationId===void 0);if(l!==void 0)throw new Error(`A creation reused by configuration must name its installations: ${l.presetId} has no id`)}let o=i.from==="caller"&&r!==null?a.journal.summary(r.caller_thread_id):void 0,d=a.presets.initialInstallations(s,t);return{agent:t,origin:r,spec:{route:I.parse(i.route),environment:{cwd:g(i.environment?.cwd??o?.environment.cwd??a.defaultCwd,"A thread cwd"),read_roots:he(i.environment?.read_roots??o?.environment.read_roots??[])},installations:i.from==="caller"&&r!==null?[...d,...a.handedDown(t,d,r.caller_thread_id)]:d,requires:E(i.requires)}}}function bt(a,e){return{environment:{cwd:g(e.environment?.cwd??a.environment.cwd,"A thread cwd"),read_roots:he(e.environment?.read_roots??a.environment.read_roots)},requires:E(e.requires)}}function _t(a,e){let t=a.stateAt(q("threadRoute"),e.threadId,e.seq);if(t===null)throw new Error(`Thread ${e.threadId} has no route at row ${e.seq}`);let{current:r}=a.stateAt(q("installations"),e.threadId,e.seq);return{route:t,installations:[...r.values()].map(({installationId:i,presetId:n,config:s,lifetime:o})=>({installation_id:i,preset_id:n,config:s,lifetime:o}))}}function kt(a,e){if(a.kind==="fresh"){let{agent:o,origin:d,spec:l}=a.opening;return{agent_name:o,metadata:e,origin:d,environment:{cwd:l.environment.cwd,read_roots:[...l.environment.read_roots]},installations:l.installations.map(u=>({...u})),requires:[...l.requires],route:{...l.route}}}let{agent:t,origin:r,cut:i,environment:n,requires:s}=a.opening;return{agent_name:t,metadata:e,fork:{thread_id:i.threadId,seq:i.seq},origin:r,environment:n,requires:s,route:null}}function V(a,e){let t=[],r=a.fork===null?"fresh":"fork";if(r!==e.kind&&t.push({field:"kind",recorded:r,requested:e.kind}),a.agent_name!==e.agent&&t.push({field:"agent",recorded:a.agent_name,requested:e.agent}),S(a.origin,e.origin)||t.push({field:"origin",recorded:a.origin,requested:e.origin}),a.fork!==null&&e.cut!==void 0){let i={threadId:a.fork.thread_id,seq:a.fork.seq};S(i,e.cut)||t.push({field:"cut",recorded:i,requested:e.cut})}return t}function qt(a,e){let t=[],r=(i,n,s)=>{S(n,s)||t.push({field:i,recorded:n,requested:s})};return r("route",a.route,e.route),r("environment",a.environment,e.environment),r("installations",a.installations??[],e.installations),r("requires",E(a.requires),e.requires),t}function xt(a,e){let t=[];return S(a.environment,e.environment)||t.push({field:"environment",recorded:a.environment,requested:e.environment}),S(E(a.requires),e.requires)||t.push({field:"requires",recorded:[...a.requires],requested:e.requires}),t}function R(a,e){if(e.length>0)throw new He(a,e)}import{createScope as Ot,FiberState as ce,scopeTarget as Bt}from"@leuduan/cordis";var K=class{scope;target;runtime;loop;readiness;controller=new AbortController;failed=!1;prepared=!1;readyTree;staleBecause;draining=!1;closingDone;service;admission;thread;released;constructor(e,t,r,i,n,s){this.service=e,this.admission=t,this.thread=i,this.released=s,this.scope=Ot(r),this.target=Bt(this.scope.ctx),this.runtime={...n(this.scope.key),ctx:this.scope.ctx,target:this.target,signal:this.controller.signal,stale:()=>this.isStale(),strategy:()=>wt(this.scope.ctx,i.id)},this.loop=new st({runtime:this.runtime,threadId:i.id,agentName:i.agentName});let o=this.scope.ctx.fiber;o.beforeUnload(()=>(this.controller.abort(new Error(`Thread ${i.id} context is closing`)),this.admission.whenSettled(i.id))),this.readiness=this.observe(Promise.resolve().then(async()=>{await o.await(),this.controller.signal.throwIfAborted(),await this.service.load(i.id),this.controller.signal.throwIfAborted();let d=r.parallel(this.target,"lagent/thread/activate",i,this.controller.signal);await Promise.resolve();let u=(await Promise.allSettled([d,r.presets.restore(i)])).flatMap(f=>f.status==="fulfilled"?[]:f.reason instanceof AggregateError?f.reason.errors:[f.reason]);if(u.length===1)throw u[0];if(u.length>1)throw new AggregateError(u,`Thread ${i.id} could not be activated`);let c=await this.settle();this.prepared=!0,this.watchTree(c)}))}watchTree(e){this.readyTree=e;for(let t of e.slice(1))t.beforeUnload(()=>this.markStale(`plugin ${t.name} was unloaded`))}markStale(e){this.staleBecause??=e}isStale(){if(this.staleBecause!==void 0)return!0;if(this.readyTree===void 0||this.closingDone!==void 0)return!1;let t=this.scope.ctx.fiber.tree().filter(i=>!this.readyTree.includes(i));if(t.length===0)return!1;let r=t.map(i=>i.name).join(", ");return this.markStale(`${r} mounted after it was ready`),this.scope.ctx.reportError(new Error(`Thread ${this.thread.id}: ${r} mounted through thread.ctx after the thread was ready; a thread's plugins come from its activate listeners and installations, so its next operation activates it afresh without them`)),!0}get activating(){return this.failed?!1:this.prepared?this.scope.ctx.fiber.tree().some(e=>e.transition!==void 0):!0}observe(e){return e.catch(t=>{if(this.failed=!0,!this.admission.isAdmitted(this.thread.id)&&!this.draining){let r=this.scope.ctx.fiber.tree().some(i=>i.state===ce.FAILED&&i.error===t);!this.controller.signal.aborted&&!r&&this.scope.ctx.reportError(t),this.service.releaseFailed(this.thread.id).catch(i=>this.scope.ctx.reportError(i))}}),e}get signal(){return this.controller.signal}get hasFailed(){return this.failed}get closed(){return this.closingDone}async settle(){let e=await this.scope.ctx.fiber.awaitTree();this.controller.signal.throwIfAborted();for(let t of e)if(t.state!==ce.ACTIVE)throw new Error(`Thread ${this.thread.id}: required plugin ${t.name} is ${ce[t.state]}`);return e}async run(e){let t=AbortSignal.any([this.controller.signal,e.signal]);t.throwIfAborted(),await this.ready(t),await this.loop.run({...e,signal:t})}async compact(e){let{lease:t}=e,r=AbortSignal.any([this.controller.signal,e.signal]);try{await this.ready(r)}catch(i){if(!(r.aborted&&Object.is(i,r.reason)))throw i;return{committed:[],ended:"cancelled",reason:w(r.reason)}}return this.loop.compact({signal:r,lease:t,options:e.options??null})}async ready(e){await j(this.readiness,e),e.throwIfAborted(),this.runtime.agent(this.thread.agentName),await this.service.load(this.thread.id),await this.service.recordPromptOnce(this.scope.key)}async whenReady(){await this.readiness,await this.service.load(this.thread.id)}stop(e){this.draining=!0,this.controller.abort(e)}async whenIdle(){await this.readiness.catch(()=>{}),await this.scope.ctx.fiber.awaitTree().catch(()=>{})}release(){if(this.closingDone!==void 0)return this.closingDone;let e=Promise.withResolvers();return this.closingDone=e.promise,this.controller.abort(new Error(`Thread ${this.thread.id} context released`)),(async()=>{try{await this.whenIdle(),await this.scope.dispose()}finally{this.released()}})().then(e.resolve,e.reject),this.closingDone}};var Q=class{inbox;journal;threadId;targetOf;constructor(e,t,r){this.journal=e,this.threadId=t,this.targetOf=r,this.inbox=new P(e,t)}target(){return m(structuredClone(this.targetOf(this.threadId)))}transcript(){return this.journal.state(h,this.threadId).surface.entries.map(e=>e.message)}messageSeqs(){return this.journal.state(h,this.threadId).surface.entries.map(e=>e.row)}requestBehind(e){return nt(this.journal,e)}originalsBehind(e){return Re(this.journal,e)}forkPoints(){return it(this.journal,this.threadId)}finalMessage(){return Qe(this.journal,this.threadId)}outcomeSince(e){return xe(this.journal,this.threadId,e)}prompt(){return this.journal.state(h,this.threadId).threadPrompt??[]}requestTools(e){if(!this.journal.inLineage(this.threadId,e))throw new Error(`Row ${e.seq} is not part of thread ${this.threadId}'s lineage`);return this.journal.stateAt(q("threadTools"),e.thread_id,e.seq)??[]}pendingConfirmations(){return m(structuredClone(T(this.journal.state(h,this.threadId).confirmations,this.threadId)))}waits(e={}){let{owner:t}=e,r=[...this.journal.state(h,this.threadId).waits.values()];return m(structuredClone(t===void 0?r:r.filter(i=>i.declaration.owner===t)))}replay(){return this.journal.rows(this.threadId)}replayLineage(){return this.journal.lineage(this.threadId)}queued(){return this.inbox.pending}input(e){return $(this.journal.state(h,this.threadId),e)}installations(){let{current:e}=this.journal.state(h,this.threadId).installations;return m([...e.values()].map(t=>structuredClone(t)))}};var fe=(a,e)=>a.signal.aborted&&Object.is(e,a.signal.reason),X=class extends Ht{static inject=["journal","agents","tools","llm","prompts","threadDirs","presets"];chunks;limits;defaultCwd;live=new Map;retentions=new Set;retiring=new Map;admission;owner;retention;chainLimit;handles=new Map;reportedCorrupt=new Set;controller=new AbortController;closing;constructor(e,t){super(e,"threads");let r=i=>e.reportError(i);if(this.chunks=new Le(r),this.admission=new W(this.controller.signal,r),this.retention=t.retention,!Number.isInteger(t.chainLimit)||t.chainLimit<1)throw new Error(`chainLimit must be a positive integer, not ${t.chainLimit}`);this.chainLimit=t.chainLimit,this.owner=new G({journal:this.journal,admission:this.admission,signal:this.controller.signal,execute:(i,n)=>this.execute(i,n),cleanup:i=>this.cleanup(i),retain:i=>this.retainExecutionReads(i,{retiring:!0}),report:r},t.limits),e.effect(()=>e.journal.subscribe(i=>this.owner.onRow(i.thread_id,i))),e.effect(()=>this.admission.onOperation(i=>{i.phase==="settled"&&this.owner.onSettled(i.thread_id,i.kind)})),this.limits=t.limits,this.defaultCwd=g(t.cwd,"The core cwd"),pt(e,()=>this.shutdown()),e.effect(()=>this.journal.onRevoked(()=>{this.shutdown().catch(i=>e.reportError(i))}))}get signal(){return this.controller.signal}shutdown(){if(this.closing!==void 0)return this.closing;let e=Promise.withResolvers();this.closing=e.promise;let t=new Error("lagent is closing");this.controller.abort(t),this.owner.close();let r=[...this.live.values()];for(let i of r)i.stop(t);return(async()=>{await this.owner.whenIdle(),await this.admission.drain(),await Promise.all(r.map(i=>i.whenIdle())),await Promise.all(r.map(i=>i.release()))})().then(e.resolve,e.reject),this.closing}list(e={}){let t=e.under===void 0?void 0:this.subtreeIds(e.under),r=e.origin===void 0?void 0:_(e.origin);return this.journal.summaries().filter(i=>!(e.agentName!==void 0&&i.agent_name!==e.agentName||e.callerThreadId!==void 0&&i.origin?.caller_thread_id!==e.callerThreadId||r!==void 0&&_(i.origin)!==r||t!==void 0&&!t.has(i.thread_id))).map(i=>this.handle(i.thread_id))}get(e){return this.journal.summary(e)!==void 0?this.handle(e):void 0}liveThreads(){return[...this.live.keys()].map(e=>this.handle(e))}start(e){return this.owner.start(e)}inspect(e,{basis:t}){return this.owner.inspect(e,t)}onRunEvent(e){return this.owner.onRunEvent(e)}runState(e,t){return this.owner.runState(e,t)}chain(e){return $e(this.journal.state(h,e).chain,this.chainLimit)}registerOwner(e){return this.owner.registerOwner(e)}async releaseUnder(e,t){let r=this.admission.admit(e,"release");await this.under(r,async()=>{await this.settledLive(e),await this.live.get(e)?.release(),await t?.()})}async retain(e,t){let r=[...e()];for(let u of r)this.requireThread(u);let i={members:e};this.retentions.add(i);let n=this.journal.hold(r),s=!1,o=()=>{if(!s&&(s=!0,this.retentions.delete(i),n(),!(t.lastRelease!=="retire"||this.closing!==void 0)))try{for(let u of new Set([...r,...e()]))this.retire(u)}catch(u){this.ctx.reportError(u)}};await Promise.all((t.waitOn??r).map(u=>this.retiring.get(u)));let l=(await Promise.allSettled(r.map(u=>this.load(u)))).find(u=>u.status==="rejected");if(l!==void 0)throw o(),l.reason;return{release:o}}view(e){return new Q(this.journal,e,t=>this.target(t))}covered(){let e=new Set;for(let t of this.retentions)for(let r of t.members())e.add(r);return e}retire(e){if(this.retiring.has(e)||this.closing!==void 0||!this.journal.isLoaded(e)||this.covered().has(e))return;let t=(async()=>{for(;this.admission.isAdmitted(e);)await this.admission.settlement(e);this.covered().has(e)||await this.close(e)})().catch(r=>{r instanceof y||this.ctx.reportError(r)}).finally(()=>{this.retiring.get(e)===t&&this.retiring.delete(e)});this.retiring.set(e,t)}async close(e){if(this.requireThread(e),this.closing!==void 0){await this.closing,this.journal.unload(e);return}await this.releaseUnder(e,()=>{let t=this.journal.hold([...this.covered()]);try{this.journal.unload(e)}finally{t()}})}async fork(e,t){jt(t.id);let r=this.journal.hold([e]);try{return await this.forkHeld(e,t)}finally{r()}}async forkHeld(e,t){this.controller.signal.throwIfAborted();let{journal:r}=this,i=r.summaryMap();if(!i.has(e))throw new v(e);if(t.before===!0&&t.input===void 0)throw new Error("A fork before a turn needs an input");await r.load(e);let n=t.atTurn===void 0?t.at:rt(r,e,t.atTurn,t.before??!1),s=tt(r,e,n),o=t.id,d=i.get(s.threadId),l=i.get(e),u=t.origin??null,c={kind:"fork",agent:d.agent_name,origin:u,cut:s},f=()=>bt(l,t),p=i.get(o);if(p!==void 0)return this.reuseFork(p,c,f,t.reuse);let Z=St(o,t),Rt=await this.originFor(o,t.origin),Tt={kind:"fork",opening:{agent:d.agent_name,origin:Rt,cut:s,...f(),inherits:_t(r,s)}},{value:pe}=await r.transact(o,()=>{let me=r.summary(o);return me!==void 0?{rows:[],value:me}:{rows:this.openingGroup(o,Tt,Z,t),value:void 0}});return pe===void 0?this.handle(o):this.reuseFork(pe,c,f,t.reuse)}reuseFork(e,t,r,i){return R(e.thread_id,V(e,t)),i==="configuration"&&R(e.thread_id,xt(e,r())),this.handle(e.thread_id)}openingGroup(e,t,r,i){let{wake:n}=i,s=n===void 0?null:b(e,null,n,r?.id??null),o=this.journal.head+1,d="next-turn",l=kt(t,i.metadata??{}),u={threadId:e,target:d,runId:s?.runId??null,inputId:r?.id??null},c=i.rows?.(t.kind==="fresh"?{kind:"create",...u,opening:m(structuredClone(t.opening))}:{kind:"fork",...u,opening:m(structuredClone(t.opening))})??[];if(Je(c))throw new ze(c.refuse,t.kind==="fresh"?"create":"fork");return[{type:"thread/spawned",data:l,options:{threadId:e}},...r===null?[]:[{type:"inbox/spliced",data:{target:d,start:0,inserted:[r]},options:{threadId:e,sourceSeqs:[o]}}],...s?.rows??[],...c]}async create(e,t){let r=jt(t.id);this.controller.signal.throwIfAborted();let{journal:i}=this,n=i.summary(r);if(n!==void 0)return this.reuseFresh(n,e,t);this.requireGlobalAgent(e);let s=await this.originFor(r,t.origin),o=J(this.birthContext(r),{agent:e,origin:s,birth:t.birth,reuse:t.reuse}),d=St(r,t),{value:l}=await i.transact(r,()=>{let u=i.summary(r);return u!==void 0?{rows:[],value:u}:{rows:this.openingGroup(r,{kind:"fresh",opening:o},d,t),value:void 0}});return l===void 0?this.handle(r):this.reuseFresh(l,e,t)}birthContext(e){return{journal:this.journal,presets:this.ctx.presets,defaultCwd:this.defaultCwd,handedDown:(t,r,i)=>this.handedDown(e,t,r,i)}}async reuseFresh(e,t,r){let i=r.origin??null;if(R(e.thread_id,V(e,{kind:"fresh",agent:t,origin:i})),r.reuse==="configuration"){let n=J(this.birthContext(e.thread_id),{agent:t,origin:i,birth:r.birth,reuse:"configuration"});R(e.thread_id,await this.specConflictsOf(e,n.spec))}return this.handle(e.thread_id)}async specConflictsOf(e,t){let r=await this.journal.readRow(e.spawn_seq);if(r?.type!=="thread/spawned")throw new Error(`Row ${e.spawn_seq} is not thread ${e.thread_id}'s spawn`);return qt(r.data,t)}async birthConflicts(e,t){let r=this.requireThread(e),i=J(this.birthContext(e),{agent:t.agent,origin:r.origin,birth:{from:"exact",spec:t.spec},reuse:"configuration"}),n=V(r,{kind:"fresh",agent:t.agent,origin:r.origin});return n.length>0?n:this.specConflictsOf(r,i.spec)}async pendingConfirmations(e={}){await this.journal.flush();let t=e.under===void 0?void 0:this.subtreeIds(e.under),r=this.journal.summaries().filter(n=>n.unanswered_confirmations.length>0&&(t===void 0||t.has(n.thread_id))).map(n=>n.thread_id),i=await this.retain(()=>r,{lastRelease:"keep"});try{return Object.freeze(r.flatMap(n=>this.view(n).pendingConfirmations()))}finally{i.release()}}async load(e){let t=this.readSet(e),i=(await Promise.allSettled(t.map(n=>this.journal.load(n)))).find(n=>n.status==="rejected");if(i!==void 0)throw i.reason;this.reportCorrupt(t)}reportCorrupt(e){for(let t of e){if(this.reportedCorrupt.has(t)||!this.journal.isLoaded(t))continue;let r=ie(this.journal,t);r!==null&&(this.reportedCorrupt.add(t),this.ctx.reportError(new re(t,r.row,r.refusal.message)))}}requireExecutable(e){let t=ie(this.journal,e);if(t!==null)throw new re(e,t.row,t.refusal.message)}readSet(e){return this.journal.origins.callers(e).threads.map(t=>t.thread_id)}async retainExecutionReads(e,{retiring:t}){this.requireThread(e);let r=this.readSet(e),i=()=>{if(!(!t||this.retention!=="retire")){if(this.admission.isAdmitted(e)){this.admission.settlement(e).then(i);return}try{this.retire(e)}catch(o){this.ctx.reportError(o)}}},n;try{n=await this.retain(()=>r,{lastRelease:"keep",waitOn:[e]})}catch(o){throw i(),o}let s=!1;return{release:()=>{s||(s=!0,n.release(),i())}}}onChunk(e){return this.chunks.subscribe(e)}subtreeIds(e){return new Set(this.journal.origins.subtree(e,{self:!0}).map(t=>t.thread_id))}handedDown(e,t,r,i){let n=this.live.get(i);if(n===void 0||n.closed!==void 0||this.admission.isReleasing(i))throw new Error(`Thread ${e} cannot be created with an origin on thread ${i}, which has no live activation: a child is created by a call running in its caller`);return this.ctx.presets.handedDown(n.scope.key,e,t,r)}async configure(e,t){this.requireThread(e);let r=this.ctx.presets.installationsFor(t.apply??[],"change"),i=[...t.remove??[]],n=t.rebuildPrompt===!0;if(this.admission.requireFree(e),this.live.get(e)?.activating===!0)throw new Ue(e);let s=this.admission.admit(e,"configure");return this.under(s,async()=>{let o=this.journal.hold([e]);try{await this.journal.load(e),s.signal.throwIfAborted();let d=this.ctx.presets.plan(e,r,i),l=()=>({applied:d.applied,removed:d.removed});if(d.rows.length===0)return l();this.refuseRecording(e);let u=this.live.get(e),c=u!==void 0&&u.closed===void 0&&!u.hasFailed||n&&this.journal.state(h,e).threadPrompt!==null;await this.settledLive(e),await this.live.get(e)?.release();let{value:f}=await this.journal.transact(e,()=>{s.signal.throwIfAborted(),this.refuseRecording(e);let p=this.ctx.presets.plan(e,r,i);return{rows:[...p.rows],value:p}});if(d=f,this.admission.setReleasing(s,!1),!c)return l();try{let p=this.activate(e);if(await j(p.whenReady(),s.signal),n&&this.journal.state(h,e).threadPrompt!==null){s.signal.throwIfAborted();let Z=this.renderPrompt(p.scope.key,e);await this.journal.append("thread/prompt",{blocks:Z},{threadId:e})}}catch(p){if(fe(s,p))return l();throw new Oe(e,p)}return l()}finally{o()}})}refuseRecording(e){this.controller.signal.throwIfAborted();let t=F(this.journal,e);if(t.length>0){let r=t.map(i=>i.request_id).join(", ");throw new We(e,`it has unfinished confirmations (${r}); answer them, let their calls run, or revoke their approvals first`)}}requireGlobalAgent(e){at(e)||this.ctx.agents.require(void 0,e)}async originFor(e,t){if(t===void 0)return null;let r=Ee.parse(t);if(this.journal.summary(r.caller_thread_id)===void 0)throw new v(r.caller_thread_id);if(this.handle(r.caller_thread_id).depth+1>this.limits.maxThreadDepth)throw new Be(e,this.limits.maxThreadDepth);return await this.journal.origins.validate(r),r}renderPrompt(e,t){let r=this.journal.summary(t);if(r===void 0)throw new v(t);return this.ctx.prompts.render(e,r.agent_name,{environment:r.environment})}async recordPromptOnce(e){let t=[...this.live.values()].find(i=>i.scope.key===e);if(t===void 0)return;let r=t.thread.id;await this.journal.transact(r,()=>({rows:this.journal.state(h,r).threadPrompt!==null?[]:[{type:"thread/prompt",data:{blocks:this.renderPrompt(e,r)},options:{threadId:r}}],value:void 0}))}async rebuildPrompt(e){this.requireThread(e);let t=this.admission.admit(e,"rebuild");return this.under(t,async()=>{let r=await this.activationFor(e,t);await j(r.whenReady(),t.signal),t.signal.throwIfAborted();let i=this.renderPrompt(r.scope.key,e);return await this.journal.append("thread/prompt",{blocks:i},{threadId:e}),i})}async whenReady(e){for(this.requireThread(e);;){await this.settledLive(e);let r=this.admission.holder(e);if(r?.kind==="release"||r?.kind==="configure"){await this.admission.settlement(e);continue}let i=this.live.get(e);if(i?.closed===void 0){if(i?.isStale()===!0){r===null?await this.releaseUnder(e):await this.admission.settlement(e);continue}break}}let t=this.activate(e);try{await t.whenReady()}catch(r){throw await this.releaseFailed(e),r}}contextOf(e){return this.activate(e).scope.ctx}signalOf(e){return this.activate(e).signal}activate(e){let t=this.live.get(e);if(t?.closed!==void 0)throw new y(e,"release");if(t===void 0){this.controller.signal.throwIfAborted();let r=this.admission.holder(e);if(r!==null&&this.admission.isReleasing(e))throw new y(e,r.kind);if(this.journal.summary(e)===void 0)throw new v(e);let i=new K(this,this.admission,this.ctx,this.handle(e),n=>this.loopServices(n),()=>{this.live.get(e)===i&&this.live.delete(e)});this.live.set(e,i),t=i}return t}async execute(e,t){let{threadId:r}=t,{decide:i}=e,n;try{if(this.requireExecutable(r),this.requireGlobalAgent(this.requireThread(r).agent_name),n=this.journal.state(h,r).turnBoundary.lastTurn,await this.settledLive(r),this.live.has(r)?await this.journal.flush():await Ke(this.journal,r,this.limits),t.signal.throwIfAborted(),this.journal.state(h,r).requests.pending?.runId!==e.runId)return;let s=B(this.journal.state(h,r)),o=i(s);if(e.final=o,o.kind!=="advance"&&o.kind!=="repair"){s.resumed!==null&&(e.attemptCutoff=s.resumed.attemptCutoff);return}let d={id:e.runId,attemptCutoff:e.attemptCutoff,continueAfter:(l,u)=>i({ending:l,start:u}).kind==="advance"};for(;o.kind==="advance"||o.kind==="repair";){await(await this.activationFor(r,t)).run({signal:t.signal,lease:t.lease,served:d}),t.signal.throwIfAborted();let u=et(this.journal.state(h,r),e.runId);o=i({ending:u?.ending??null,start:u?.start??null}),e.final=o}}catch(s){if(fe(t,s)){this.journal.isLoaded(r)&&(e.final=i({ending:{kind:"aborted"},start:null}));return}if(s instanceof x){e.final={kind:"settle",outcome:"failed",reason:"setup"},e.errorText=oe(s);return}let o=this.journal.isLoaded(r)?this.journal.state(h,r).turnBoundary:null;if(n===void 0||o?.lastTurn===n){e.final={kind:"settle",outcome:"failed",reason:"setup"},e.errorText=oe(s);return}throw e.final={kind:"settle",outcome:"failed",reason:"turn"},s}}target(e){let{threadRoute:t,requests:r}=this.journal.state(h,e);if(t===null)throw new Error(`Thread ${e} has no route`);let i=this.ctx.llm.unavailable(t),{pending:n}=r,s=this.admission.holder(e);return s?.kind==="run"&&n!==null&&s.runId===n.runId?{route:t,blocked:i,selectable:!1}:n===null||i!==null?{route:t,blocked:i,selectable:!0}:{route:t,blocked:i,selectable:M(r)||L(r)}}routes={target:e=>this.target(e),resolve:(e,t)=>this.ctx.llm.resolveRoute(e,t)};async compact(e,t){let r=this.requireThread(e);this.requireGlobalAgent(r.agent_name);let i=t;if(t.signal?.aborted===!0)return this.admission.requireFree(e),{committed:[],ended:"cancelled",reason:w(t.signal.reason)};let n=this.admission.admit(e,"compact",{signal:t.signal}),s,o;try{o=await this.under(n,async()=>{try{return s=await this.retainExecutionReads(e,{retiring:!1}),n.signal.throwIfAborted(),this.requireExecutable(e),await(await this.activationFor(e,n)).compact({...i,signal:n.signal,lease:n.lease})}catch(d){if(!fe(n,d))throw d;return{committed:[],ended:"cancelled",reason:w(n.signal.reason)}}})}finally{s?.release()}return!n.signal.aborted||o.ended==="cancelled"||o.ended==="failed"?o:{committed:o.committed,ended:"cancelled",reason:w(n.signal.reason)}}async releaseFailed(e){if(this.closing!==void 0)return;let t=this.admission.holder(e);if(t!==null){t.kind==="release"&&await this.admission.settlement(e);return}let r=this.live.get(e);r===void 0||!r.hasFailed||await this.releaseUnder(e)}async activationFor(e,t){await this.settledLive(e);let r=this.live.get(e);if(r!==void 0&&r.isStale()){this.admission.setReleasing(t,!0);try{await r.release()}finally{this.admission.setReleasing(t,!1)}}return this.activate(e)}async settledLive(e){let t=this.live.get(e)?.closed;t!==void 0&&await t.catch(()=>{})}loopServices(e){let{ctx:t}=this;return{journal:this.journal,chunks:this.chunks,limits:this.limits,unavailable:r=>t.llm.unavailable(r),resolveRoute:(r,i)=>t.llm.resolveRoute(r,i),client:r=>t.llm.clientFor(r),agent:r=>t.agents.require(e,r),toolsFor:r=>t.tools.toolsFor(e,r),tool:(r,i)=>t.tools.toolFor(e,r,i),waitOwner:r=>this.owner.isRegistered(r),requiredRegistrations:r=>this.owner.requiredRegistrations(r)}}get journal(){return ut(this.ctx)}under(e,t){return this.admission.hold(e,t,{cleanup:()=>this.cleanup(e)})}cleanup(e){let t=this.live.get(e.threadId);if(!(!(t!==void 0&&(t.hasFailed||t.isStale()))||e.kind==="release"||this.closing!==void 0))return this.admission.setReleasing(e,!0),t.release()}requireThread(e){let t=this.journal.summary(e);if(t===void 0)throw new v(e);return t}handle(e){let t=this.handles.get(e);return t===void 0&&(t=new U(this,this.admission,e,this.journal),this.handles.set(e,t)),t}};function jt(a){if(typeof a!="string"||a.length===0)throw new TypeError("A non-empty thread id is required");return a}function St(a,e){if(e.input!==void 0)return Se(e.input,e.inputId??k("in"));if(e.inputId!==void 0)throw new Error(`Thread ${a} was given an input id but no input`);return null}import{ScopeLayers as zt,Service as Ut}from"@leuduan/cordis";var Y=class extends Ut{static inject=["agents"];layers=new zt(()=>({entries:new Map,grants:[]}));constructor(e){super(e,"tools")}names(e){let t=new Set;for(let r of this.layers.closestFirst(e))for(let i of r.entries.keys())t.add(i);return C(t)}has(e,t){return this.get(e,t)!==void 0}get(e,t){for(let r of this.layers.closestFirst(e)){let i=r.entries.get(t);if(i!==void 0)return i}}register(e,t,r){let i=this.layers.layer(e).entries;if(i.has(t))throw new Error(`Tool already registered for name: ${t}`);if(!je.safeParse(r.onInterrupted).success)throw new Error(`Tool ${t} must declare onInterrupted as 'rerun' or 'report'`);if(r.executionMode!=="parallel"&&r.executionMode!=="exclusive")throw new Error(`Tool ${t} must declare executionMode as 'parallel' or 'exclusive'`);let n={name:t,tool:r,spec:m(structuredClone({...r.buildSpec(),name:t}))};return i.set(t,n),()=>{i.get(t)===n&&i.delete(t)}}grant(e,t,r){let i=this.layers.layer(e).grants,n={agents:Array.isArray(t)?[...t]:t,tool:r};return i.push(n),()=>{let s=i.indexOf(n);s>=0&&i.splice(s,1)}}toolsFor(e,t){this.ctx.agents.require(e,t);let r=new Set,i=[],n=s=>{if(r.has(s))return;r.add(s);let o=this.get(e,s);o!==void 0&&i.push(o)};for(let s of this.layers.globalFirst(e))for(let o of s.grants)ot(o.agents,t)&&n(o.tool);return i}toolFor(e,t,r){return this.toolsFor(e,t).find(i=>i.name===r)}mayCall(e,t,r){return this.toolFor(e,t,r)!==void 0}};var wi={name:"lagent:core",async apply(a,e){let t=Ge(e.limits);await a.plugin(dt,{store:e.store,limits:t}),await a.plugin(z),await a.plugin(lt),await a.plugin(Y),await a.plugin(ft),await a.plugin(N,{dataDir:e.dataDir}),await a.plugin(ht),await a.plugin(ct),await a.plugin(X,{cwd:e.cwd,limits:t,retention:e.retention,chainLimit:e.chainLimit})}};import{randomUUID as Nt}from"node:crypto";var Et=class extends Ce{rows=[];index=[];threadIndex=new Map;catalog=new Map;attempts=new Map;writer;get size(){return this.rows.length}async acquireWriter(){if(this.writer!==void 0)throw new Ae(this.constructor.name);let e=Nt();return this.writer=e,{identity:this.constructor.name,acquisitionId:e,release:async()=>{this.writer===e&&(this.writer=void 0)}}}async doOpen(){}async doClose(){}async doHead(){return this.headSeq()}async doAppend(e,t){let r=this.attempts.get(e.id);if(r!==void 0){if(r===t)return"duplicate";throw new Pe(e.id)}if(this.writer!==e.writer)throw new D(e.writer);let i=this.headSeq();if(i!==e.baseHead)throw new De(e.baseHead,i);let n=e.rows[0],s=this.threadIndex.get(n.thread_id);if(s===void 0&&n.type!=="thread/spawned")throw new Error(`Thread ${n.thread_id} is not in the store; its first row must be its spawn row`);if(e.rows.some(l=>l.type==="thread/spawned"&&(s!==void 0||l!==n)))throw new Error(`Thread ${n.thread_id} already has a spawn row`);let o=e.rows.map(l=>this.serialize(l));for(let[l,u]of e.rows.entries())this.rows.push(o[l]),this.index.push({seq:u.seq,threadId:u.thread_id});let d=e.rows.at(-1).seq;return this.threadIndex.set(n.thread_id,{spawnSeq:s?.spawnSeq??n.seq,lastSeq:d}),this.attempts.set(e.id,t),"appended"}async doRead(e){let t=[];for(let[r,i]of this.index.entries())i.threadId===e&&t.push(ee(this.rows[r]));return t}async doRowAt(e){let t=this.index.findIndex(r=>r.seq===e);return t<0?void 0:ee(this.rows[t])}async doThreads(){return[...this.threadIndex].map(([e,t])=>({threadId:e,...t})).sort((e,t)=>e.spawnSeq-t.spawnSeq)}async doSummaries(){return[...this.catalog.values()].map(e=>te(e)).sort((e,t)=>e.spawn_seq-t.spawn_seq)}async doPutSummaries(e,t){if(this.writer!==t)throw new D(t);let r=e.map(i=>[i,this.serializeSummary(i)]);for(let[i,n]of r){let s=this.catalog.get(i.thread_id);s!==void 0&&te(s).through_seq>i.through_seq||this.catalog.set(i.thread_id,n)}}async doReplaceCatalog(e,t){if(this.writer!==t)throw new D(t);let r=e.map(i=>[i.thread_id,this.serializeSummary(i)]);this.catalog.clear();for(let[i,n]of r)this.catalog.set(i,n)}headSeq(){return this.index.at(-1)?.seq??0}};export{Zt as a,U as b,wi as c,Et as d};
|
package/dist/chunks/2OSESBIL.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{scopeOf as t}from"@leuduan/cordis";import{z as n}from"zod";var a=n.union([n.array(n.string()),n.literal("global"),n.literal("all")]),c=(e,r)=>e==="all"?!0:e==="global"?!o(r):e.includes(r),o=e=>e.includes(".");function p(e,r){if(t(e)!==void 0)throw new Error(`Mount ${r} on an untagged application context, not in a thread's scope`)}export{a,c as b,o as c,p as d};
|
package/dist/chunks/3LQHQRIU.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{ja as g}from"./YLNJXQ3Z.js";import{c}from"./KESCTXGO.js";import{randomBytes as _}from"node:crypto";import{mkdir as T,writeFile as O}from"node:fs/promises";import{join as f}from"node:path";import{z as d}from"zod";var $=/[^A-Za-z0-9_.-]+/g,w="tool-outputs",x=String(Number.MAX_SAFE_INTEGER).length,s=t=>`
|
|
2
|
-
[\u2026 ${t} characters omitted \u2026]
|
|
3
|
-
`,m=s("9".repeat(x)).length+2,A=d.object({maxToolOutputLength:d.number().int().min(m,{message:`maxToolOutputLength must hold the gap marker and some head and tail: at least ${m}`}).default(1e4)}),P={name:"lagent:truncate-tool-output",inject:["threadDirs"],apply(t,n){t.threadDirs.require("truncateToolOutput");let{maxToolOutputLength:o}=A.parse(n??{});t.on("lagent/tool/execute",async(e,a)=>{let r=await a();if(g(r))return r;let u=f(t.threadDirs.dirFor(e.threadId),w);return await E(r,o,u)})}};async function E(t,n,o){let e=await M(t,n,o),a=[...t.notes??[]];return e.note!==null&&a.push(e.note),{...t,output:e.output,notes:a.length>0?a:null,metadata:{...t.metadata,truncation:e.metadata}}}async function M(t,n,o){let e=t.output;if(t.metadata.no_truncation===!0)return{output:e,note:null,metadata:{was_truncated:!1,limit:n,original_length:e.length,returned_length:e.length,no_truncation:!0}};if(e.length<=n)return{output:e,note:null,metadata:{was_truncated:!1,limit:n,original_length:e.length,returned_length:e.length}};let a=await I(e,t,o),r=e.split(`
|
|
4
|
-
`).length,{head:u,tail:l}=S(e,n),i=e.length-u.length-l.length,h=`${u}${s(String(i))}${l}`;return{output:h,note:`Output truncated to its first ${u.length} and last ${l.length} of ${e.length} characters (${r} lines). The full output is written to ${a}.`,metadata:{was_truncated:!0,limit:n,original_length:e.length,returned_length:h.length,omitted_length:i,full_output_path:a}}}function S(t,n){let o=n-s(String(t.length)).length,e=Math.ceil(o/2),a=o-e,r=e,u=t.lastIndexOf(`
|
|
5
|
-
`,r-1);u>=Math.floor(e/2)?r=u+1:r=c(t,r,"end");let l=t.length-a,i=t.indexOf(`
|
|
6
|
-
`,l);return i!==-1&&i+1<=l+Math.floor(a/2)?l=i+1:l=c(t,l,"start"),{head:t.slice(0,r),tail:t.slice(l)}}async function I(t,n,o){let e=`${p(n.name||"tool")}-${p(n.call_id||"call")}-`,a=f(o,`${e}${_(6).toString("hex")}.txt`);return await T(o,{recursive:!0,mode:448}),await O(a,t,{encoding:"utf8",flag:"wx",mode:384}),a}var p=t=>t.replace($,"-").slice(0,F).replace(/^[.-]+|[.-]+$/g,"")||"value",F=40;export{w as a,m as b,A as c,P as d,E as e};
|
package/dist/chunks/3QCW4WDJ.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{b as h}from"./74HKUGRR.js";import{c as I,d as $,e as y,f as p}from"./L3N4FIEN.js";import{G as m,o as f}from"./YLNJXQ3Z.js";import{a as g}from"./DDANMY45.js";import{a as w,d as c}from"./37YOGDGY.js";import{h as u}from"./KESCTXGO.js";import{ScopeLayers as E,Service as _,scopeOf as v}from"@leuduan/cordis";var b=class extends _{static inject=["journal"];registry=new Map;handDowns=new E(()=>[]);constructor(e){super(e,"presets")}register(e){if(this.registry.has(e.id))throw new Error(`Preset already registered: ${e.id}`);return this.registry.set(e.id,e),()=>{this.registry.get(e.id)===e&&this.registry.delete(e.id)}}has(e){return this.registry.has(e)}installationsFor(e,t,i){let r=new Set;return e.map(o=>{let s=this.requireRegistered(o.presetId);if(s.placement==="creation"&&t==="change")throw new Error(`Preset ${s.id} is recorded only when a thread is created; it cannot be applied later`);let n=y(s,o.config);p(s,n,`Preset ${s.id}: its schema must parse its own output back unchanged`);let a=o.installationId??m("preset");if(a==="")throw new Error("An installation id must be non-empty");if(r.has(a))throw new Error(`Installation id ${a} is given twice`);r.add(a);let l=i===void 0?void 0:s.definesAgent?.(n);if(l!==void 0&&l!==i)throw new Error(`Installation ${a} defines agent ${l}, but the thread runs on ${i}`);return{installation_id:a,preset_id:s.id,config:n}})}initialInstallations(e,t){let i=e.map(r=>u.parse(r.lifetime));return this.installationsFor(e,"creation",t).map((r,o)=>{let s=i[o];if(s!=="defining"&&this.requireRegistered(r.preset_id).placement==="creation")throw new Error(`Preset ${r.preset_id} is recorded only when a thread is created; it must be defining`);return{...r,lifetime:s}})}plan(e,t,i){let r=h(this.ctx).state(f,e).installations,o=[],s=[];for(let n of new Set(i)){let a=r.current.get(n);if(a?.lifetime==="defining")throw new Error(`Installation ${n} on thread ${e} defines the thread and cannot be removed`);a!==void 0&&(o.push({type:"installation/removed",data:{installation_id:n},options:{threadId:e}}),s.push(n))}for(let n of t){if(s.includes(n.installation_id))throw new Error(`Installation id ${n.installation_id} is removed by this change and cannot be reused`);this.alreadyApplied(r,e,n)||o.push({type:"installation/applied",data:n,options:{threadId:e}})}return{rows:o,applied:t.map(n=>n.installation_id),removed:s}}handDown(e,t){if(e===void 0)throw new Error(`Preset ${t.presetId} cannot be handed down from the global layer: only a thread's own scope hands down`);let[i]=this.installationsFor([t],"creation"),r=this.handDowns.layer(e),o=r.find(n=>n.installationId===t.installationId);if(o!==void 0)throw new Error(`Installation id ${t.installationId} is already handed down from this scope, by preset ${o.presetId}`);let s={presetId:t.presetId,config:i.config,installationId:t.installationId};return r.push(s),()=>{let n=r.indexOf(s);n>=0&&r.splice(n,1)}}handedDown(e,t,i,r){let o=new Map;for(let n of this.handDowns.closestFirst(e))for(let a of n)o.has(a.installationId)||o.set(a.installationId,a);if(o.size===0)return[];let s=new Set(r.map(n=>n.installation_id));for(let n of o.keys())if(s.has(n))throw new Error(`Thread ${t} is created with installation ${n}, which its caller's scope also hands down`);return this.initialInstallations([...o.values()].map(n=>({...n,lifetime:"defining"})),i)}async restore(e){let{signal:t}=e;for(let i of[...h(this.ctx).state(f,e.id).installations.current.values()]){t.throwIfAborted();let r=this.requireRegistered(i.presetId);p(r,i.config,`Installation ${i.installationId} on thread ${e.id} no longer validates`);let o=e.ctx.plugin($(r),I(w(structuredClone(i.config)),{thread:e,installationId:i.installationId,lifetime:i.lifetime,signal:t}));await x(o,i.installationId,e.id)}t.throwIfAborted()}requireRegistered(e){let t=this.registry.get(e);if(t===void 0)throw new Error(`Unknown preset: ${e}`);return t}alreadyApplied(e,t,i){let{installation_id:r}=i,o=e.current.get(r);if(o===void 0){if(e.used.has(r))throw new Error(`Installation id ${r} was removed from thread ${t} and cannot be reused`);return!1}if(o.presetId!==i.preset_id)throw new Error(`Installation ${r} on thread ${t} is preset ${o.presetId}, not ${i.preset_id}`);if(c(o.config)!==c(i.config))throw new Error(`Installation ${r} on thread ${t} was applied with a different configuration`);return!0}};async function x(d,e,t){try{await d.awaitTree()}catch(i){throw new Error(`Installation ${e} on thread ${t} could not be mounted: ${g(i)}`,{cause:i})}}var j=(d,e)=>d.effect(()=>d.presets.register(e));function z(d,e){let t=d.get("presets");return d.effect(()=>t.handDown(v(d),e))}export{b as a,j as b,z as c};
|
package/dist/chunks/4XVRUSCQ.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var o=new WeakMap;function i(t,e,s){let r=o.get(t.root);r===void 0&&(r=new Map,o.set(t.root,r));let a=r.get(e);if(a!==void 0)throw new Error(`Thread ${e} already runs the ${a.name} strategy; a thread runs one strategy. Mount strategies on the root with disjoint agent selectors, or install one per thread, never two.`);let n=r;t.effect(()=>(n.set(e,s),()=>{n.get(e)===s&&n.delete(e)}))}function g(t,e){return o.get(t.root)?.get(e)}export{i as a,g as b};
|
package/dist/chunks/6ECI2GGV.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var s=new WeakMap;function c(o,d,n,a,l){let e=s.get(o.root);e===void 0&&(e=new Map,s.set(o.root,e));let t=e.get(d);t===void 0&&(t=new Map,e.set(d,t));let f=t.get(n);if(f!==void 0)throw new Error(l(f.holder));let i={holder:a},r=t;o.effect(()=>(r.set(n,i),()=>{r.get(n)===i&&r.delete(n)}))}export{c as a};
|
package/dist/chunks/74HKUGRR.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{S as t,o as l}from"./YLNJXQ3Z.js";import{w as a}from"./37YOGDGY.js";var d={name:"lagent:journal",async apply(r,i){let{store:s,limits:p}=i,e=new a(s,{summarize:(o,n)=>t(o,n,p),onListenerError:o=>r.reportError(o),coreFolds:[l]});e.onRevoked(o=>{r.parallel("lagent/journal/revoked",o).catch(n=>r.reportError(n))}),await e.open(),r.provide("journal",e),r.effect(()=>()=>e.close())}},c=r=>r.journal;export{d as a,c as b};
|
package/dist/chunks/ADHERZLW.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{b as a}from"./RK3VP2Q4.js";import{b as d,e as p}from"./DRYAN665.js";import{a as i}from"./KESCTXGO.js";import{existsSync as m}from"node:fs";import{resolve as u}from"node:path";import{z as n}from"zod";var l=n.strictObject({cwd:n.string(),read_roots:n.array(n.string())}),f=d({plugin:"lagent:workspace-context",kind:"workspace-notice",version:3,schema:l}),$={name:"lagent:workspace-context",inject:["prompts","journal"],apply(e){a(e,{tag:"workspace",slot:"context",agents:"global",render:()=>i("environment_info",w())}),p(e,{event:f,current(r){let o=e.journal.summary(r.threadId);if(o===void 0)return;let{cwd:t,read_roots:s}=o.environment;return{cwd:t,read_roots:s}},render:g})}};function g(e,r){let o=u(e.cwd),t=m(o)?"":" (does not exist)",s=r===null||r.cwd===e.cwd?"":` (previously ${r.cwd})`,c=[`Working directory: ${o}${t}${s}`];return e.read_roots.length>0&&c.push(`Read roots: ${e.read_roots.join(", ")}. File tools read under these too, but change files only under the working directory and the application data directory.`),i("workspace",c.join(`
|
|
2
|
-
`))}function w(){return`Operating system: ${process.platform}`}export{$ as a,g as b,w as c};
|
package/dist/chunks/D7E6EWIE.js
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
var u=class extends Error{provider;feature;constructor(r,n){super(`Provider '${r}' cannot do ${n}`),this.provider=r,this.feature=n,this.name="ProviderUnsupportedError"}},d=class extends Error{provider;constructor(r,n){super(`Provider '${r}' refused the request: it exceeds the model's context window`,n),this.provider=r,this.name="ContextOverflowError"}},c=class extends Error{provider;kind;partial;providerMetadata;constructor(r,n,s,a,S){super(`${n}: ${S}`),this.provider=r,this.kind=n,this.partial=s,this.providerMetadata=a,this.name="ResponseSettlementError"}},p=class extends Error{provider;idleMs;constructor(r,n){super(`'${r}' sent nothing for ${n} ms`),this.provider=r,this.idleMs=n,this.name="StreamIdleError"}},m=class extends Error{provider;partial;providerMetadata;constructor(r,n,s,a){super(`the response of '${r}' failed after it had begun`,a),this.provider=r,this.partial=n,this.providerMetadata=s,this.name="ResponseInterruptedError"}};import{z as o}from"zod";var f=(...t)=>t.some(r=>r!==null&&r!==""),y=o.discriminatedUnion("type",[o.object({type:o.literal("text"),text:o.string()}),o.object({type:o.literal("image"),url:o.string().nullable().default(null),data:o.string().nullable().default(null),media_type:o.string().nullable().default(null),detail:o.enum(["auto","low","high"]).default("auto")}).refine(t=>f(t.url,t.data),{message:"An image part needs a url or base64 data"}),o.object({type:o.literal("file"),file_id:o.string().nullable().default(null),url:o.string().nullable().default(null),data:o.string().nullable().default(null),filename:o.string().nullable().default(null),media_type:o.string().nullable().default(null)}).refine(t=>f(t.file_id,t.url,t.data),{message:"A file part needs a file_id, a url or base64 data"})]),g=o.preprocess(t=>t==null?[]:typeof t=="string"?[{type:"text",text:t}]:t,o.array(y).default([])),R=t=>({type:"text",text:t}),h=t=>t.filter(r=>r.type==="text").map(r=>r.text).join("");import{z as e}from"zod";var j=["none","low","medium","high","max"],L=e.enum(j),l=e.record(e.string(),e.unknown()),x=()=>({type:"object",properties:{},additionalProperties:!0}),q=e.object({name:e.string(),description:e.string().default(""),input_schema:l.default(x)}).transform((t,r)=>{let n={...t.input_schema};if(Object.keys(n).length===0)return{...t,input_schema:x()};if(n.type!=="object")return r.addIssue({code:"custom",message:"ToolSpec.input_schema must describe an object"}),e.NEVER;let s=n.properties;if(s==null)n.properties={};else if(typeof s!="object"||Array.isArray(s))return r.addIssue({code:"custom",message:"ToolSpec.input_schema.properties must be a mapping"}),e.NEVER;let a=n.required;return a!=null&&!Array.isArray(a)?(r.addIssue({code:"custom",message:"ToolSpec.input_schema.required must be a list when present"}),e.NEVER):{...t,input_schema:n}}),E=e.looseObject({call_id:e.string(),name:e.string(),arguments:l.default({})}),v=e.looseObject({call_id:e.string(),name:e.string(),arguments:e.null(),malformed:e.strictObject({raw:e.string(),error:e.string()})}),T=e.union([E,v]),F=t=>t.arguments!==null,V=t=>t.arguments===null?t.malformed.raw:JSON.stringify(t.arguments),k=e.object({input_tokens:e.number().int().default(0),output_tokens:e.number().int().default(0),cache_read_input_tokens:e.number().int().default(0),cache_write_input_tokens:e.number().int().default(0),reasoning_output_tokens:e.number().int().default(0)});var b=e.looseObject({id:e.string(),type:e.string(),name:e.string().nullable().default(null),status:e.enum(["in_progress","completed","failed"]).default("completed"),summary:e.string().nullable().default(null)}),z=e.object({text_delta:e.string().default(""),reasoning_delta:e.string().default(""),provider_call:b.nullable().default(null)}),M=e.object({provider:e.string(),data:l.default({})}),P=["system","developer","user","assistant","tool"],_=e.object({role:e.enum(P),content:g,reasoning_content:e.string().nullable().default(null),tool_calls:e.array(T).default([]),provider_calls:e.array(b).default([]),tool_call_id:e.string().nullable().default(null),usage:k.nullable().default(null),provider_state:M.nullable().default(null)}),U=t=>_.parse(t),B=t=>h(t.content),C=["completed","length","other"],O=e.enum(C),G=e.object({message:_,finish:O,provider_metadata:l.default({})}).refine(t=>t.message.role==="assistant",{message:"ModelResponse.message must have role assistant"});import{z as i}from"zod";var K=i.object({kind:i.enum(["base","instruction","context"]),tag:i.string().nullable(),text:i.string()}),D=t=>t.map(r=>r.text).join(`
|
|
2
|
-
|
|
3
|
-
`);export{u as a,d as b,c,p as d,m as e,y as f,g,R as h,h as i,j,L as k,l,q as m,E as n,T as o,F as p,V as q,k as r,b as s,z as t,_ as u,U as v,B as w,O as x,G as y,K as z,D as A};
|
package/dist/chunks/DRYAN665.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{b as x}from"./2OSESBIL.js";import{la as b}from"./YLNJXQ3Z.js";import{h}from"./37YOGDGY.js";import{g as y,m as S}from"./KESCTXGO.js";import{isDeepStrictEqual as _}from"node:util";var g=class extends Error{record;supportedVersion;constructor(n,i){super(`Unsupported ${n.plugin}/${n.kind} payload version ${n.version}; expected ${i}`),this.record=n,this.supportedVersion=i,this.name="ExtensionVersionError"}};function $(r){let n=y.omit({data:!0}).parse({plugin:r.plugin,instance_id:r.instanceId,kind:r.kind,version:r.version});r.policyState===!0&&b(n);let i=e=>e.plugin===n.plugin&&e.instance_id===n.instance_id&&e.kind===n.kind,d=e=>e.type==="ext"&&i(e.data),p=e=>{if(e.version!==n.version)throw new g(e,n.version);return r.schema.parse(structuredClone(e.data))},o=e=>y.parse({...n,data:r.schema.parse(e)}),a=e=>e!==null&&i(e)?p(e):void 0,l=e=>e.type==="input/message"?a(e.data.source):void 0,f=h({key:`told:${JSON.stringify([n.plugin,n.instance_id,n.kind])}`,init:()=>({notices:[]}),apply(e,t){let s=e.notices;if(t.type==="surface/replaced"){let k=null;for(let m of s)m.visible&&(k??=new Set(t.source_seqs),k.has(m.row.seq)&&(m.visible=!1));return e}let c=l(t);return c===void 0||s.push({row:{threadId:t.thread_id,seq:t.seq},value:c,visible:!0}),e}}),u=(e,t)=>({type:"ext",data:o(e),options:t});return{record:o,parse:e=>r.schema.parse(e),entry:u,append(e,t,s){let c=u(t,s);return e.append(c.type,c.data,c.options)},read(e){return d(e)?p(e.data):void 0},contribution(e,t,s={}){return S({...e,source:o(t),source_seqs:[...s.sourceSeqs??[]]})},matchesSource(e){return i(e)},rejection(e,t){return{decision:"reject",reason:e,source:o(t)}},readContribution:l,readSource:a,told:f,projection(e){return h({key:JSON.stringify([n.plugin,n.instance_id,n.kind,e.key]),init:e.init,onFork:e.onFork,apply:(t,s)=>d(s)?e.apply(t,p(s.data),s):t})}}}function C(r){return r.notices.at(-1)??null}function q(r){for(let n=r.notices.length-1;n>=0;n-=1){let i=r.notices[n];if(i.visible)return i.value}return null}function N(r,n){let{event:i,agents:d,rank:p}=n;r.on("lagent/llm/step",o=>{if(d!==void 0){let t=r.journal.summary(o.threadId);if(t===void 0||!x(d,t.agent_name))return}let a=n.current(o);if(a===void 0)return;let l=i.parse(a),f=r.journal.state(i.told,o.threadId),u=q(f);if(u!==null&&_(u,l))return;let e=n.render(l,u,{step:o,notices:f});e!==null&&o.contribute(i.contribution({content:e},a),p)})}export{g as a,$ as b,C as c,q as d,N as e};
|
package/dist/chunks/DXFXAYDN.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import{d as v}from"./DDANMY45.js";import{c as k}from"./KESCTXGO.js";import{spawn as D}from"node:child_process";import{StringDecoder as E}from"node:string_decoder";var M=3e3,T="IFS= read -r go <&3 || exit 125; exec 3<&-; ",_="exec 2>&1; ";async function A(t){if(t.signal.throwIfAborted(),t.env===void 0||t.env===null)throw new TypeError("spawnCommand needs an env");let e=t.stdio==="pipes",s=e?T:T+_,n=typeof t.command=="string"?["-c",`${s}exec /bin/sh -c "$1"`,"sh",t.command]:["-c",`${s}exec "$@"`,"sh",...t.command],r=D("/bin/sh",n,{cwd:t.cwd,detached:!0,env:t.env,stdio:e?["pipe","pipe","pipe","pipe"]:["ignore","pipe","ignore","pipe"]}),a=r.stdio[3];a.on("error",()=>{});let o=r.pid;if(o===void 0)throw await new Promise(c=>r.once("error",c));r.on("error",()=>{});let u=new Promise(c=>r.once("exit",(w,m)=>c({code:w,signal:m}))),l=e?[r.stdout,r.stderr]:[r.stdout];for(let c of[...l,...e?[r.stdin]:[]])c.on("error",()=>{});let h=Promise.all(l.map(c=>new Promise(w=>c.once("close",w)))).then(()=>{}),p=Date.now();try{await $.register(o),t.signal.throwIfAborted(),await t.beforeRelease?.(o),t.signal.throwIfAborted()}catch(c){throw a.destroy(),L(o,"SIGKILL"),$.unregister(o),c}a.end(`go
|
|
2
|
-
`);let f={readables:l,stdin:e?r.stdin:null},x=new I({pid:o,streams:f,exited:u,outputClosed:h,startedAt:p},t);return e?Object.assign(x,{stdin:r.stdin,stdout:r.stdout,stderr:r.stderr}):Object.assign(x,{output:r.stdout})}async function z(t){let e=await A(t),s=new E("utf8"),n="";return e.output.on("data",a=>{n+=s.write(a)}),{...await e.result,output:n+s.end()}}var C=4096;async function H(t){let{onLine:e,maxLineChars:s,delimiter:n,...r}=t,a=await A({...r,stdio:"pipes"});a.stdin.end();let o="",u=new E("utf8");a.stderr.on("data",i=>{o=(o+u.write(i)).slice(-C)});let l=new E("utf8"),h="",p=!1,f=!1,x=i=>{if(p)return;let d=s-h.length;if(i.length<=d){h+=i;return}h+=i.slice(0,k(i,d,"end")),p=!0},c=()=>{let i=h,d=p;h="",p=!1,e(i,d)===!1&&(f=!0,a.stop())},w=i=>{let d=0;for(;!f;){let g=i.indexOf(n,d);if(g===-1){x(i.slice(d));return}x(i.slice(d,g)),c(),d=g+1}};a.stdout.on("data",i=>{f||w(l.write(i))});let m=await a.result;return f||(w(l.end()),!f&&(h.length>0||p)&&c()),{...m,stderr:o+u.end()}}var I=class{pid;result;requestStop;constructor({pid:e,streams:s,exited:n,outputClosed:r,startedAt:a},o){this.pid=e;let u=o.killGraceMs??M,l=Promise.withResolvers();this.requestStop=l.resolve;let h=()=>l.resolve("abort");o.signal.addEventListener("abort",h,{once:!0});let p=o.timeoutMs===void 0?void 0:setTimeout(()=>l.resolve("timeout"),o.timeoutMs),f=async()=>{let x=await Promise.race([n.then(g=>({kind:"exit",exit:g})),l.promise.then(g=>({kind:"stop",cause:g}))]);clearTimeout(p),o.signal.removeEventListener("abort",h);let c=null,w=!1,m=!0,i;x.kind==="stop"?(c=x.cause,i=Date.now()+2*u,m=await P(e,u)):S(e)?(w=!0,i=Date.now()+2*u,m=await P(e,u)):i=Date.now()+u;let d=await b(n,i-Date.now());m&&await b(r,Math.min(u,i-Date.now()));for(let g of s.readables)g.destroy();return s.stdin?.destroy(),m?$.unregister(e):$.watchUntilEmpty(e),{exitCode:d?.code??null,exitSignal:d?.signal??null,stoppedBy:c,leftoversStopped:w,stopFailed:!m,durationMs:Date.now()-a}};this.result=f()}stop(){return this.requestStop("stop"),this.result}};function S(t){try{return process.kill(-t,0),!0}catch(e){return v(e,"EPERM")}}function L(t,e){try{process.kill(-t,e)}catch{}}var G=50;async function P(t,e){let s=Date.now(),n=!1;for(L(t,"SIGTERM");S(t);){let r=Date.now()-s;if(!n&&r>=e)L(t,"SIGKILL"),n=!0;else if(n&&r>=2*e)return!1;await K(G)}return!0}var K=t=>new Promise(e=>setTimeout(e,t));async function b(t,e){let s,n=new Promise(r=>s=setTimeout(()=>r(void 0),Math.max(0,e)));try{return await Promise.race([t,n])}finally{clearTimeout(s)}}var O=`trap '' INT TERM HUP
|
|
3
|
-
groups=' '
|
|
4
|
-
while IFS= read -r line; do
|
|
5
|
-
g=\${line#?}
|
|
6
|
-
case $line in
|
|
7
|
-
+*) case $groups in *" $g "*) ;; *) groups="$groups$g " ;; esac ;;
|
|
8
|
-
-*) case $groups in *" $g "*) groups="\${groups%% $g *} \${groups#* $g }" ;; esac ;;
|
|
9
|
-
esac
|
|
10
|
-
done
|
|
11
|
-
[ "$groups" = ' ' ] && exit 0
|
|
12
|
-
for g in $groups; do kill -TERM -$g 2>/dev/null; done
|
|
13
|
-
sleep 1
|
|
14
|
-
for g in $groups; do kill -KILL -$g 2>/dev/null; done
|
|
15
|
-
`,y=class{child;stdin;exited=!1;constructor(e,s){this.child=D("/bin/sh",["-c",O],{detached:!0,stdio:["pipe","ignore","ignore"]}),this.stdin=this.child.stdin,this.stdin.on("error",()=>{});let n=()=>{this.exited||(this.exited=!0,s(this))};this.child.once("exit",n),this.child.once("error",n),this.child.unref(),this.stdin.unref?.();for(let r of e)this.stdin.write(`+${r}
|
|
16
|
-
`)}get pid(){return this.child.pid}write(e){return this.exited?Promise.reject(new Error("The process reaper has exited")):new Promise((s,n)=>this.stdin.write(`${e}
|
|
17
|
-
`,r=>r?n(r):s()))}kill(){this.child.kill("SIGKILL")}},R=class{groups=new Set;stuck=new Set;reaper;exitListener=!1;watcher;async register(e){this.installExitListener(),this.groups.add(e);let s;for(let n=0;n<2;n++){let r=this.ensureReaper();try{await r.write(`+${e}`);return}catch(a){s=a,this.reaper===r&&(this.reaper=void 0)}}throw this.groups.delete(e),new Error("Could not register the command with the process reaper",{cause:s})}unregister(e){this.stuck.delete(e),this.groups.delete(e)&&this.reaper?.write(`-${e}`).catch(()=>{})}watchUntilEmpty(e){this.stuck.add(e),this.watcher===void 0&&(this.watcher=setInterval(()=>{for(let s of this.stuck)S(s)||this.unregister(s);this.stuck.size===0&&(clearInterval(this.watcher),this.watcher=void 0)},1e3),this.watcher.unref())}tracked(){return[...this.groups]}reaperPid(){return this.reaper?.pid}killReaper(){this.reaper?.kill()}ensureReaper(){return this.reaper??=new y(this.groups,e=>this.onReaperExit(e)),this.reaper}onReaperExit(e){this.reaper===e&&(this.reaper=void 0,this.groups.size>0&&e.pid!==void 0&&this.ensureReaper())}installExitListener(){this.exitListener||(this.exitListener=!0,process.on("exit",()=>{for(let e of this.groups)L(e,"SIGKILL")}))}},$=new R;export{M as a,A as b,z as c,H as d};
|