@hanzo/dev 4.0.1 → 4.2.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/LICENSE +21 -0
- package/dist/{BinaryViewer-qn8xfPZp.js → BinaryViewer-CmFUK4zZ.js} +2 -2
- package/dist/{ChatMarkdown-Cyujn3JJ.js → ChatMarkdown-nvEI7iCv.js} +4 -4
- package/dist/{DiffCommentAnnotation-BZu1TP6E.js → DiffCommentAnnotation-BPTN_2a6.js} +1 -1
- package/dist/{DiffFileTree-CcJ91zOM.js → DiffFileTree-B_br90vY.js} +2 -2
- package/dist/{DiffPanel-BXC88NMs.js → DiffPanel-V5R5TPuK.js} +5 -5
- package/dist/{ExpandedImageDialog-BZnGRBJR.js → ExpandedImageDialog-CAorLacL.js} +1 -1
- package/dist/{FileExplorerPane-vBJS6V75.js → FileExplorerPane-CeHulcPz.js} +1 -1
- package/dist/{FilePreviewPanel-jJHWsMzm.js → FilePreviewPanel-BVNLvWOc.js} +8 -8
- package/dist/{MermaidDiagram-paNwQY33.js → MermaidDiagram-CTolIzQD.js} +1 -1
- package/dist/{MermaidFilePreview-B9KxfnIN.js → MermaidFilePreview-CP2zP4FU.js} +1 -1
- package/dist/{MermaidViewer-CAEGpLyD.js → MermaidViewer-BDfYkS9E.js} +2 -2
- package/dist/{NotebookPreview-D01mMCpR.js → NotebookPreview-Cybs5uM0.js} +1 -1
- package/dist/{OfficeFilePreview-DWaRgfQU.js → OfficeFilePreview-7JsmSoKU.js} +1 -1
- package/dist/{OfficeViewer-BudrZbBA.js → OfficeViewer-CJoojlJK.js} +1 -1
- package/dist/{OpenInPicker-Dd3ifHa1.js → OpenInPicker-BIMWUxBP.js} +7 -7
- package/dist/{OpenXmlOfficeViewer-CXcKdq7k.js → OpenXmlOfficeViewer-C24Kj8DM.js} +1 -1
- package/dist/{PreviewImage-C4Ku4z7v.js → PreviewImage-oY5YnuTr.js} +1 -1
- package/dist/{PreviewImageContent-BwOB9Lyd.js → PreviewImageContent-n6eLzY99.js} +1 -1
- package/dist/{PullRequestCodeTab-BcwyMk7d.js → PullRequestCodeTab-Dv_0kVad.js} +7 -7
- package/dist/{PullRequestReviewAnnotation-BU5t_1fC.js → PullRequestReviewAnnotation-LeuwO-iH.js} +4 -4
- package/dist/builder/activity.d.ts +16 -0
- package/dist/builder/activity.js +51 -0
- package/dist/builder/address.d.ts +11 -0
- package/dist/builder/address.js +50 -0
- package/dist/builder/ago.d.ts +2 -0
- package/dist/builder/ago.js +18 -0
- package/dist/builder/api/agents.d.ts +139 -0
- package/dist/builder/api/agents.js +253 -0
- package/dist/builder/api/auto.d.ts +166 -0
- package/dist/builder/api/auto.js +300 -0
- package/dist/builder/api/billing.d.ts +263 -0
- package/dist/builder/api/billing.js +401 -0
- package/dist/builder/api/call.d.ts +53 -0
- package/dist/builder/api/call.js +123 -0
- package/dist/builder/api/capabilities.d.ts +14 -0
- package/dist/builder/api/capabilities.js +41 -0
- package/dist/builder/api/changes.d.ts +74 -0
- package/dist/builder/api/changes.js +75 -0
- package/dist/builder/api/chat.d.ts +12 -0
- package/dist/builder/api/chat.js +28 -0
- package/dist/builder/api/cli.d.ts +59 -0
- package/dist/builder/api/cli.js +133 -0
- package/dist/builder/api/codebases.d.ts +46 -0
- package/dist/builder/api/codebases.js +100 -0
- package/dist/builder/api/coding.d.ts +117 -0
- package/dist/builder/api/coding.js +222 -0
- package/dist/builder/api/connectors.d.ts +76 -0
- package/dist/builder/api/connectors.js +111 -0
- package/dist/builder/api/consent.d.ts +22 -0
- package/dist/builder/api/consent.js +28 -0
- package/dist/builder/api/conversations.d.ts +20 -0
- package/dist/builder/api/conversations.js +23 -0
- package/dist/builder/api/draft.d.ts +38 -0
- package/dist/builder/api/draft.js +87 -0
- package/dist/builder/api/environment.d.ts +55 -0
- package/dist/builder/api/environment.js +126 -0
- package/dist/builder/api/gallery.d.ts +32 -0
- package/dist/builder/api/gallery.js +85 -0
- package/dist/builder/api/git.d.ts +59 -0
- package/dist/builder/api/git.js +117 -0
- package/dist/builder/api/github.d.ts +230 -0
- package/dist/builder/api/github.js +373 -0
- package/dist/builder/api/keys.d.ts +35 -0
- package/dist/builder/api/keys.js +72 -0
- package/dist/builder/api/library.d.ts +116 -0
- package/dist/builder/api/library.js +214 -0
- package/dist/builder/api/limits.d.ts +69 -0
- package/dist/builder/api/limits.js +86 -0
- package/dist/builder/api/links.d.ts +51 -0
- package/dist/builder/api/links.js +75 -0
- package/dist/builder/api/machines.d.ts +122 -0
- package/dist/builder/api/machines.js +238 -0
- package/dist/builder/api/mcp.d.ts +10 -0
- package/dist/builder/api/mcp.js +47 -0
- package/dist/builder/api/members.d.ts +35 -0
- package/dist/builder/api/members.js +95 -0
- package/dist/builder/api/memory.d.ts +14 -0
- package/dist/builder/api/memory.js +35 -0
- package/dist/builder/api/models.d.ts +17 -0
- package/dist/builder/api/models.js +20 -0
- package/dist/builder/api/places.d.ts +32 -0
- package/dist/builder/api/places.js +29 -0
- package/dist/builder/api/platform.d.ts +68 -0
- package/dist/builder/api/platform.js +87 -0
- package/dist/builder/api/plugins.d.ts +42 -0
- package/dist/builder/api/plugins.js +175 -0
- package/dist/builder/api/pref.d.ts +77 -0
- package/dist/builder/api/pref.js +93 -0
- package/dist/builder/api/profile.d.ts +11 -0
- package/dist/builder/api/profile.js +41 -0
- package/dist/builder/api/projects.d.ts +99 -0
- package/dist/builder/api/projects.js +166 -0
- package/dist/builder/api/provider.d.ts +49 -0
- package/dist/builder/api/provider.js +96 -0
- package/dist/builder/api/sandbox.d.ts +90 -0
- package/dist/builder/api/sandbox.js +223 -0
- package/dist/builder/api/sessions.d.ts +123 -0
- package/dist/builder/api/sessions.js +255 -0
- package/dist/builder/api/skills.d.ts +82 -0
- package/dist/builder/api/skills.js +117 -0
- package/dist/builder/api/sse.d.ts +24 -0
- package/dist/builder/api/sse.js +68 -0
- package/dist/builder/api/studio.d.ts +59 -0
- package/dist/builder/api/studio.js +91 -0
- package/dist/builder/api/talk.d.ts +48 -0
- package/dist/builder/api/talk.js +81 -0
- package/dist/builder/api/tools.d.ts +34 -0
- package/dist/builder/api/tools.js +37 -0
- package/dist/builder/api/turn.d.ts +202 -0
- package/dist/builder/api/turn.js +772 -0
- package/dist/builder/api/verdict.d.ts +3 -0
- package/dist/builder/api/verdict.js +21 -0
- package/dist/builder/api/webhooks.d.ts +47 -0
- package/dist/builder/api/webhooks.js +82 -0
- package/dist/builder/api/work.d.ts +110 -0
- package/dist/builder/api/work.js +218 -0
- package/dist/builder/ask.d.ts +51 -0
- package/dist/builder/ask.js +147 -0
- package/dist/builder/automations.d.ts +33 -0
- package/dist/builder/automations.js +453 -0
- package/dist/builder/awake.d.ts +24 -0
- package/dist/builder/awake.js +68 -0
- package/dist/builder/back.d.ts +30 -0
- package/dist/builder/back.js +105 -0
- package/dist/builder/builder.d.ts +12 -0
- package/dist/builder/builder.js +162 -0
- package/dist/builder/carry.d.ts +48 -0
- package/dist/builder/carry.js +278 -0
- package/dist/builder/choice.d.ts +134 -0
- package/dist/builder/choice.js +152 -0
- package/dist/builder/claims.d.ts +13 -0
- package/dist/builder/claims.js +35 -0
- package/dist/builder/clipboard.d.ts +2 -0
- package/dist/builder/clipboard.js +10 -0
- package/dist/builder/customize/agents.d.ts +11 -0
- package/dist/builder/customize/agents.js +513 -0
- package/dist/builder/customize/connectors.d.ts +2 -0
- package/dist/builder/customize/connectors.js +319 -0
- package/dist/builder/customize/drafted.d.ts +4 -0
- package/dist/builder/customize/drafted.js +92 -0
- package/dist/builder/customize/index.d.ts +8 -0
- package/dist/builder/customize/index.js +127 -0
- package/dist/builder/customize/inventory.d.ts +14 -0
- package/dist/builder/customize/inventory.js +127 -0
- package/dist/builder/customize/load.d.ts +9 -0
- package/dist/builder/customize/load.js +22 -0
- package/dist/builder/customize/ocean.d.ts +41 -0
- package/dist/builder/customize/ocean.js +332 -0
- package/dist/builder/customize/plugins.d.ts +2 -0
- package/dist/builder/customize/plugins.js +136 -0
- package/dist/builder/customize/roles.d.ts +4 -0
- package/dist/builder/customize/roles.js +105 -0
- package/dist/builder/customize/say.d.ts +13 -0
- package/dist/builder/customize/say.js +32 -0
- package/dist/builder/customize/skills.d.ts +2 -0
- package/dist/builder/customize/skills.js +208 -0
- package/dist/builder/customize/ui.d.ts +152 -0
- package/dist/builder/customize/ui.js +208 -0
- package/dist/builder/data.d.ts +57 -0
- package/dist/builder/data.js +322 -0
- package/dist/builder/design.d.ts +140 -0
- package/dist/builder/design.js +175 -0
- package/dist/builder/desk.d.ts +48 -0
- package/dist/builder/desk.js +449 -0
- package/dist/builder/dev-mode.d.ts +43 -0
- package/dist/builder/dev-mode.js +120 -0
- package/dist/builder/door.d.ts +11 -0
- package/dist/builder/door.js +126 -0
- package/dist/builder/environment.d.ts +23 -0
- package/dist/builder/environment.js +193 -0
- package/dist/builder/files.d.ts +23 -0
- package/dist/builder/files.js +175 -0
- package/dist/builder/find.d.ts +7 -0
- package/dist/builder/find.js +80 -0
- package/dist/builder/foot.d.ts +30 -0
- package/dist/builder/foot.js +79 -0
- package/dist/builder/forge.d.ts +4 -0
- package/dist/builder/forge.js +639 -0
- package/dist/builder/gear.d.ts +60 -0
- package/dist/builder/gear.js +180 -0
- package/dist/builder/git.d.ts +21 -0
- package/dist/builder/git.js +118 -0
- package/dist/builder/github-sync.d.ts +70 -0
- package/dist/builder/github-sync.js +435 -0
- package/dist/builder/gutter.d.ts +17 -0
- package/dist/builder/gutter.js +25 -0
- package/dist/builder/handoff.d.ts +8 -0
- package/dist/builder/handoff.js +22 -0
- package/dist/builder/host.d.ts +189 -0
- package/dist/builder/host.js +46 -0
- package/dist/builder/index.d.ts +108 -0
- package/dist/builder/index.js +99 -0
- package/dist/builder/landing.d.ts +34 -0
- package/dist/builder/landing.js +725 -0
- package/dist/builder/library.d.ts +21 -0
- package/dist/builder/library.js +526 -0
- package/dist/builder/local.d.ts +80 -0
- package/dist/builder/local.js +124 -0
- package/dist/builder/make.d.ts +10 -0
- package/dist/builder/make.js +120 -0
- package/dist/builder/markdown.d.ts +49 -0
- package/dist/builder/markdown.js +125 -0
- package/dist/builder/marks.d.ts +15 -0
- package/dist/builder/marks.js +18 -0
- package/dist/builder/merge.d.ts +223 -0
- package/dist/builder/merge.js +380 -0
- package/dist/builder/meter.d.ts +31 -0
- package/dist/builder/meter.js +54 -0
- package/dist/builder/mind.d.ts +107 -0
- package/dist/builder/mind.js +335 -0
- package/dist/builder/names.d.ts +40 -0
- package/dist/builder/names.js +189 -0
- package/dist/builder/nav.d.ts +28 -0
- package/dist/builder/nav.js +218 -0
- package/dist/builder/out.d.ts +16 -0
- package/dist/builder/out.js +24 -0
- package/dist/builder/page.d.ts +55 -0
- package/dist/builder/page.js +217 -0
- package/dist/builder/pane.d.ts +30 -0
- package/dist/builder/pane.js +21 -0
- package/dist/builder/panel.d.ts +101 -0
- package/dist/builder/panel.js +408 -0
- package/dist/builder/pdf.d.ts +139 -0
- package/dist/builder/pdf.js +847 -0
- package/dist/builder/pick.d.ts +10 -0
- package/dist/builder/pick.js +38 -0
- package/dist/builder/plain.d.ts +59 -0
- package/dist/builder/plain.js +209 -0
- package/dist/builder/plan.d.ts +63 -0
- package/dist/builder/plan.js +155 -0
- package/dist/builder/plans.d.ts +1 -0
- package/dist/builder/plans.js +157 -0
- package/dist/builder/plugin.d.ts +60 -0
- package/dist/builder/plugin.js +1 -0
- package/dist/builder/plugins/design.d.ts +8 -0
- package/dist/builder/plugins/design.js +71 -0
- package/dist/builder/plugins/index.d.ts +46 -0
- package/dist/builder/plugins/index.js +117 -0
- package/dist/builder/plugins/pdf.d.ts +18 -0
- package/dist/builder/plugins/pdf.js +83 -0
- package/dist/builder/plugins/project.d.ts +5 -0
- package/dist/builder/plugins/project.js +68 -0
- package/dist/builder/plugins/research.d.ts +24 -0
- package/dist/builder/plugins/research.js +147 -0
- package/dist/builder/plugins/sketch.d.ts +9 -0
- package/dist/builder/plugins/sketch.js +101 -0
- package/dist/builder/plugins/slides.d.ts +14 -0
- package/dist/builder/plugins/slides.js +55 -0
- package/dist/builder/plugins/template.d.ts +13 -0
- package/dist/builder/plugins/template.js +95 -0
- package/dist/builder/prefs.d.ts +29 -0
- package/dist/builder/prefs.js +91 -0
- package/dist/builder/preview-url.d.ts +47 -0
- package/dist/builder/preview-url.js +107 -0
- package/dist/builder/project.d.ts +18 -0
- package/dist/builder/project.js +604 -0
- package/dist/builder/prose.d.ts +4 -0
- package/dist/builder/prose.js +57 -0
- package/dist/builder/publish.d.ts +18 -0
- package/dist/builder/publish.js +141 -0
- package/dist/builder/question.d.ts +33 -0
- package/dist/builder/question.js +64 -0
- package/dist/builder/redesign.d.ts +32 -0
- package/dist/builder/redesign.js +117 -0
- package/dist/builder/removal.d.ts +71 -0
- package/dist/builder/removal.js +73 -0
- package/dist/builder/reply.d.ts +50 -0
- package/dist/builder/reply.js +196 -0
- package/dist/builder/roles.d.ts +10 -0
- package/dist/builder/roles.js +28 -0
- package/dist/builder/route.d.ts +85 -0
- package/dist/builder/route.js +126 -0
- package/dist/builder/run.d.ts +4 -0
- package/dist/builder/run.js +804 -0
- package/dist/builder/runmeta.d.ts +16 -0
- package/dist/builder/runmeta.js +69 -0
- package/dist/builder/say.d.ts +2 -0
- package/dist/builder/say.js +7 -0
- package/dist/builder/scheduled.d.ts +3 -0
- package/dist/builder/scheduled.js +328 -0
- package/dist/builder/section.d.ts +27 -0
- package/dist/builder/section.js +60 -0
- package/dist/builder/settings/account.d.ts +1 -0
- package/dist/builder/settings/account.js +111 -0
- package/dist/builder/settings/billing.d.ts +1 -0
- package/dist/builder/settings/billing.js +210 -0
- package/dist/builder/settings/capabilities.d.ts +1 -0
- package/dist/builder/settings/capabilities.js +66 -0
- package/dist/builder/settings/card.d.ts +31 -0
- package/dist/builder/settings/card.js +140 -0
- package/dist/builder/settings/code.d.ts +1 -0
- package/dist/builder/settings/code.js +70 -0
- package/dist/builder/settings/credits.d.ts +10 -0
- package/dist/builder/settings/credits.js +123 -0
- package/dist/builder/settings/environments.d.ts +1 -0
- package/dist/builder/settings/environments.js +60 -0
- package/dist/builder/settings/general.d.ts +1 -0
- package/dist/builder/settings/general.js +58 -0
- package/dist/builder/settings/index.d.ts +4 -0
- package/dist/builder/settings/index.js +25 -0
- package/dist/builder/settings/integrations.d.ts +8 -0
- package/dist/builder/settings/integrations.js +138 -0
- package/dist/builder/settings/keys.d.ts +1 -0
- package/dist/builder/settings/keys.js +114 -0
- package/dist/builder/settings/list.d.ts +10 -0
- package/dist/builder/settings/list.js +28 -0
- package/dist/builder/settings/machines.d.ts +4 -0
- package/dist/builder/settings/machines.js +243 -0
- package/dist/builder/settings/members.d.ts +1 -0
- package/dist/builder/settings/members.js +77 -0
- package/dist/builder/settings/memory.d.ts +1 -0
- package/dist/builder/settings/memory.js +62 -0
- package/dist/builder/settings/notifications.d.ts +1 -0
- package/dist/builder/settings/notifications.js +111 -0
- package/dist/builder/settings/plan.d.ts +17 -0
- package/dist/builder/settings/plan.js +131 -0
- package/dist/builder/settings/privacy.d.ts +1 -0
- package/dist/builder/settings/privacy.js +76 -0
- package/dist/builder/settings/sections.d.ts +16 -0
- package/dist/builder/settings/sections.js +34 -0
- package/dist/builder/settings/shared.d.ts +6 -0
- package/dist/builder/settings/shared.js +58 -0
- package/dist/builder/settings/ui.d.ts +50 -0
- package/dist/builder/settings/ui.js +53 -0
- package/dist/builder/settings/usage.d.ts +1 -0
- package/dist/builder/settings/usage.js +24 -0
- package/dist/builder/shared.d.ts +26 -0
- package/dist/builder/shared.js +15 -0
- package/dist/builder/shelf.d.ts +16 -0
- package/dist/builder/shelf.js +219 -0
- package/dist/builder/side.d.ts +40 -0
- package/dist/builder/side.js +208 -0
- package/dist/builder/site.d.ts +75 -0
- package/dist/builder/site.js +138 -0
- package/dist/builder/spend.d.ts +21 -0
- package/dist/builder/spend.js +80 -0
- package/dist/builder/standing.d.ts +36 -0
- package/dist/builder/standing.js +66 -0
- package/dist/builder/suggest.d.ts +238 -0
- package/dist/builder/suggest.js +463 -0
- package/dist/builder/suggestions.d.ts +49 -0
- package/dist/builder/suggestions.js +260 -0
- package/dist/builder/summary.d.ts +60 -0
- package/dist/builder/summary.js +81 -0
- package/dist/builder/sync.d.ts +1 -0
- package/dist/builder/sync.js +187 -0
- package/dist/builder/tabs.d.ts +85 -0
- package/dist/builder/tabs.js +288 -0
- package/dist/builder/talk.d.ts +5 -0
- package/dist/builder/talk.js +108 -0
- package/dist/builder/thread.d.ts +60 -0
- package/dist/builder/thread.js +302 -0
- package/dist/builder/tools.d.ts +22 -0
- package/dist/builder/tools.js +52 -0
- package/dist/builder/transcript.d.ts +47 -0
- package/dist/builder/transcript.js +108 -0
- package/dist/builder/voice.d.ts +8 -0
- package/dist/builder/voice.js +24 -0
- package/dist/{changedFileKind-DqgCCDUs.js → changedFileKind-D6QYvIez.js} +1 -1
- package/dist/core.js +1 -0
- package/dist/{decodeImagePreview-CesVZYP4.js → decodeImagePreview-DnfFEjhw.js} +1 -1
- package/dist/{embed-DYP-OUlb.js → embed-c030vUqM.js} +11 -11
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -1
- package/dist/{micromark-util-decode-string-NQjVkmZH.js → micromark-util-decode-string-C-5AXkOV.js} +5 -3
- package/dist/{readBinaryPreview-D2X4Uahu.js → readBinaryPreview-BAQ4fFjm.js} +1 -1
- package/dist/{useTextFieldSpeech-CyOZZPrV.js → useTextFieldSpeech-COa9XcRx.js} +1 -1
- package/package.json +101 -11
- /package/{index.d.ts → dist/core.d.ts} +0 -0
- /package/{keymap.d.ts → dist/keymap.d.ts} +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A run's sandbox. It outlives the run: kept after the run ends, parked (its pod
|
|
3
|
+
* stopped, its disk and working tree kept) while nobody watches, and retired a
|
|
4
|
+
* day after it parks; a free plan's sandbox is not parked, and ends within the
|
|
5
|
+
* hour. Resume gives a parked one a pod again under the same id and tree.
|
|
6
|
+
*
|
|
7
|
+
* GET /v1/sandbox/{id} its status and when its lease ends
|
|
8
|
+
* POST /v1/sandbox/{id}/pause park it: stop the pod, keep the disk
|
|
9
|
+
* POST /v1/sandbox/{id}/resume a pod again, same id and /work
|
|
10
|
+
* POST /v1/sandbox/{id}/{screen|terminal}/ticket one ticket, spent when the page's socket opens (30 s)
|
|
11
|
+
* GET /v1/sandbox/{id}/ports what listens in it, each with its preview host
|
|
12
|
+
* POST /v1/sandbox/{id}/preview {port} an address that opens that port in a browser
|
|
13
|
+
* POST /v1/sandbox/{id}/exec {argv, timeoutSec} one command in /work: {exitCode, stdout, stderr}
|
|
14
|
+
* POST /v1/sandbox/read {id, path} a directory's names, or a file's bytes
|
|
15
|
+
*
|
|
16
|
+
* A door or a preview asked of a sandbox that stopped as it was asked is 409: it
|
|
17
|
+
* is resumed, and asked again once.
|
|
18
|
+
*/
|
|
19
|
+
import { phrase } from '@hanzo/i18n';
|
|
20
|
+
import { call, Refusal, seg } from './call.js';
|
|
21
|
+
const STATES = ['pending', 'running', 'parked', 'error'];
|
|
22
|
+
function box(raw, id) {
|
|
23
|
+
const o = raw && typeof raw === 'object' ? raw : {};
|
|
24
|
+
// A parked sandbox is also said to be paused; a status this does not know is not one to open.
|
|
25
|
+
const state = STATES.find((s) => s === (o.status === 'paused' ? 'parked' : o.status)) ?? 'error';
|
|
26
|
+
const ends = typeof o.expiresAt === 'number' && o.expiresAt > 0 ? o.expiresAt * 1000 : 0;
|
|
27
|
+
return { id, state, ends, error: typeof o.error === 'string' ? o.error : '' };
|
|
28
|
+
}
|
|
29
|
+
/** The sandbox as it stands. One the caller no longer holds is gone. */
|
|
30
|
+
export async function sandbox(t, id) {
|
|
31
|
+
try {
|
|
32
|
+
return box(await call(t, 'GET', `/v1/sandbox/${seg(id)}`), id);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
if (e instanceof Refusal && e.status === 404)
|
|
36
|
+
return { id, state: 'gone', ends: 0, error: '' };
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Park it: its pod stops, its disk and working tree stay. */
|
|
41
|
+
export async function park(t, id) {
|
|
42
|
+
return box(await call(t, 'POST', `/v1/sandbox/${seg(id)}/pause`), id);
|
|
43
|
+
}
|
|
44
|
+
/** Give it a pod again, under the same id and tree. */
|
|
45
|
+
export async function wake(t, id) {
|
|
46
|
+
return box(await call(t, 'POST', `/v1/sandbox/${seg(id)}/resume`), id);
|
|
47
|
+
}
|
|
48
|
+
/** How long a resumed sandbox is waited on to run. */
|
|
49
|
+
const UP = 90_000;
|
|
50
|
+
/** Resume it and wait until it runs. */
|
|
51
|
+
async function up(t, id) {
|
|
52
|
+
let b = await wake(t, id);
|
|
53
|
+
const until = Date.now() + UP;
|
|
54
|
+
while (b.state === 'pending' && Date.now() < until) {
|
|
55
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
56
|
+
b = await sandbox(t, id);
|
|
57
|
+
}
|
|
58
|
+
if (b.state !== 'running')
|
|
59
|
+
throw new Refusal(503, b.error || phrase('The sandbox did not start again. Try again.'));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* What the page passes where nobody pressed anything (a door or a preview opening as the page loads, or reopening
|
|
63
|
+
* after the sandbox stopped): it never wakes one, so a remembered yes cannot wake a workspace by a page being open.
|
|
64
|
+
* The suspended state says so, beside its Wake.
|
|
65
|
+
*/
|
|
66
|
+
export const stay = async () => false;
|
|
67
|
+
/** Asks what needs a running sandbox; a parked one is resumed first, only on the person's yes. */
|
|
68
|
+
async function running(t, id, ask, agree) {
|
|
69
|
+
try {
|
|
70
|
+
return await ask();
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
if (!(e instanceof Refusal) || e.status !== 409)
|
|
74
|
+
throw e;
|
|
75
|
+
if (!(await agree()))
|
|
76
|
+
throw new Refusal(409, phrase('The cloud workspace is suspended. Wake it to go on.'));
|
|
77
|
+
await up(t, id);
|
|
78
|
+
return ask();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** A shell's tmux session name: the sandbox reattaches to it when the door opens again. */
|
|
82
|
+
export const NAME = /^[A-Za-z0-9_][A-Za-z0-9_-]{0,63}$/;
|
|
83
|
+
/** Where to frame one door, with a fresh ticket in its address. */
|
|
84
|
+
export async function door(t, id, which, agree, session = '') {
|
|
85
|
+
const r = await running(t, id, () => call(t, 'POST', `/v1/sandbox/${seg(id)}/${which}/ticket`), agree);
|
|
86
|
+
const path = typeof r?.url === 'string' ? r.url : '';
|
|
87
|
+
if (!path.startsWith('/v1/'))
|
|
88
|
+
throw new Refusal(502, phrase('The sandbox answered with no address to open'));
|
|
89
|
+
const arg = which === 'terminal' && NAME.test(session) ? `&arg=${session}` : '';
|
|
90
|
+
return `${t.api}${path}${arg}`;
|
|
91
|
+
}
|
|
92
|
+
/** What listens in the sandbox, ascending. */
|
|
93
|
+
export async function ports(t, id) {
|
|
94
|
+
const r = await call(t, 'GET', `/v1/sandbox/${seg(id)}/ports`);
|
|
95
|
+
const list = Array.isArray(r?.ports) ? r.ports : [];
|
|
96
|
+
return list
|
|
97
|
+
.map((p) => (p && typeof p === 'object' ? p : {}))
|
|
98
|
+
.filter((p) => typeof p.port === 'number' && p.port > 0 && p.port < 65536)
|
|
99
|
+
.map((p) => ({
|
|
100
|
+
port: p.port,
|
|
101
|
+
host: typeof p.host === 'string' ? p.host : '',
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* An address that opens one port in a browser: its own origin, with a single-use
|
|
106
|
+
* ticket that sets the preview's cookie and lands on its root.
|
|
107
|
+
*/
|
|
108
|
+
export async function preview(t, id, port, agree) {
|
|
109
|
+
const r = await running(t, id, () => call(t, 'POST', `/v1/sandbox/${seg(id)}/preview`, {
|
|
110
|
+
port,
|
|
111
|
+
}), agree);
|
|
112
|
+
let url;
|
|
113
|
+
try {
|
|
114
|
+
url = new URL(typeof r?.url === 'string' ? r.url : '');
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
throw new Refusal(502, phrase('The sandbox answered with no address to open'));
|
|
118
|
+
}
|
|
119
|
+
if (url.protocol !== 'https:')
|
|
120
|
+
throw new Refusal(502, phrase('The sandbox answered with no address to open'));
|
|
121
|
+
return url.href;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Where a server started from the Preview keeps its process group and its output:
|
|
125
|
+
* under /tmp, as a run keeps its own files out of the checkout it commits.
|
|
126
|
+
*/
|
|
127
|
+
export const SERVE_DIR = '/tmp/hanzo-preview';
|
|
128
|
+
/**
|
|
129
|
+
* The shell that starts the app's server for the Preview, detached in a session of
|
|
130
|
+
* its own so it outlives the one command that started it. A server a run starts
|
|
131
|
+
* ends with the run (the agent's harness takes it down), so the Preview starts one
|
|
132
|
+
* again. It runs the codebase's start command when its environment names one ($1),
|
|
133
|
+
* else the package's `dev` script, or `start`, by the package manager its lockfile
|
|
134
|
+
* names, with the environment's secrets sourced as a run sources them (cloud
|
|
135
|
+
* apps/coding envPath). A server this started before is stopped first, its whole
|
|
136
|
+
* group. It prints what it started, or exits 3 saying why nothing could be.
|
|
137
|
+
*/
|
|
138
|
+
export const SERVE = [
|
|
139
|
+
`d=${SERVE_DIR}`,
|
|
140
|
+
'mkdir -p "$d"',
|
|
141
|
+
'if [ -f /tmp/hanzo-run/env ]; then . /tmp/hanzo-run/env; fi',
|
|
142
|
+
'cmd=$1',
|
|
143
|
+
'if [ -z "$cmd" ]; then',
|
|
144
|
+
' [ -f package.json ] || { echo no-package; exit 3; }',
|
|
145
|
+
' if [ -f pnpm-lock.yaml ]; then pm=pnpm; elif [ -f yarn.lock ]; then pm=yarn; elif [ -f bun.lock ] || [ -f bun.lockb ]; then pm=bun; else pm=npm; fi',
|
|
146
|
+
` run=$(node -e 'const s=require("./package.json").scripts||{};process.stdout.write(s.dev?"dev":s.start?"start":"")' 2>/dev/null)`,
|
|
147
|
+
' [ -n "$run" ] || { echo no-script; exit 3; }',
|
|
148
|
+
' cmd="$pm run $run"',
|
|
149
|
+
'fi',
|
|
150
|
+
'old=$(cat "$d/pid" 2>/dev/null)',
|
|
151
|
+
'if [ -n "$old" ] && kill -0 "$old" 2>/dev/null; then kill -TERM "-$old" 2>/dev/null || kill -TERM "$old" 2>/dev/null; sleep 1; fi',
|
|
152
|
+
'if command -v setsid >/dev/null 2>&1; then setsid nohup sh -c "$cmd" </dev/null >"$d/log" 2>&1 & else nohup sh -c "$cmd" </dev/null >"$d/log" 2>&1 & fi',
|
|
153
|
+
'echo $! >"$d/pid"',
|
|
154
|
+
'printf %s "$cmd"',
|
|
155
|
+
].join('\n');
|
|
156
|
+
/** Whether the server the Preview started still runs (`up` or `down`), then its output's last lines. */
|
|
157
|
+
export const SERVED = [
|
|
158
|
+
`d=${SERVE_DIR}`,
|
|
159
|
+
'p=$(cat "$d/pid" 2>/dev/null)',
|
|
160
|
+
'if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then echo up; else echo down; fi',
|
|
161
|
+
'tail -n 40 "$d/log" 2>/dev/null',
|
|
162
|
+
].join('\n');
|
|
163
|
+
/** Why there was nothing to start, as SERVE says it. */
|
|
164
|
+
const UNSTARTED = {
|
|
165
|
+
'no-package': phrase('This checkout has no package.json to start. Set a start command in its Environment.'),
|
|
166
|
+
'no-script': phrase('Its package.json has no dev or start script. Set a start command in its Environment.'),
|
|
167
|
+
};
|
|
168
|
+
/** One command in the sandbox, as an argument vector: its exit code and output. A failed command is an answer, not a refusal. */
|
|
169
|
+
async function exec(t, id, argv, timeoutSec) {
|
|
170
|
+
const r = (await call(t, 'POST', `/v1/sandbox/${seg(id)}/exec`, { argv, timeoutSec })) ?? {};
|
|
171
|
+
return {
|
|
172
|
+
code: typeof r.exitCode === 'number' ? r.exitCode : -1,
|
|
173
|
+
out: typeof r.stdout === 'string' ? r.stdout : '',
|
|
174
|
+
err: typeof r.stderr === 'string' ? r.stderr : '',
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Start the app's server in the sandbox, detached, and answer the command it runs:
|
|
179
|
+
* `start` when the codebase's environment names one, else its package's dev script.
|
|
180
|
+
* A parked sandbox is resumed first, on the person's yes.
|
|
181
|
+
*/
|
|
182
|
+
export async function serve(t, id, agree, start = '') {
|
|
183
|
+
const r = await running(t, id, () => exec(t, id, ['sh', '-c', SERVE, 'sh', start.trim()], 30), agree);
|
|
184
|
+
const said = r.out.trim();
|
|
185
|
+
if (r.code === 0 && said)
|
|
186
|
+
return said;
|
|
187
|
+
const why = UNSTARTED[said];
|
|
188
|
+
if (r.code === 3 && why)
|
|
189
|
+
throw new Refusal(422, why);
|
|
190
|
+
throw new Refusal(502, r.err.trim().split('\n').pop() || phrase('The dev server could not be started.'));
|
|
191
|
+
}
|
|
192
|
+
/** Whether the server the Preview started still runs, and what it last wrote. */
|
|
193
|
+
export async function served(t, id) {
|
|
194
|
+
const r = await exec(t, id, ['sh', '-c', SERVED], 10);
|
|
195
|
+
const [first = '', ...rest] = r.out.split('\n');
|
|
196
|
+
return { up: first.trim() === 'up', log: rest.join('\n').trimEnd() };
|
|
197
|
+
}
|
|
198
|
+
/** What a file shows at most. */
|
|
199
|
+
const CAP = 1 << 20;
|
|
200
|
+
/** One path in the sandbox, relative to its working directory. */
|
|
201
|
+
export async function read(t, sandbox, path) {
|
|
202
|
+
const o = (await call(t, 'POST', '/v1/sandbox/read', { id: sandbox, path })) ?? {};
|
|
203
|
+
const dir = o.dir === true;
|
|
204
|
+
const names = Array.isArray(o.entries) ? o.entries.filter((n) => typeof n === 'string').sort() : [];
|
|
205
|
+
const bytes = !dir && typeof o.data === 'string' ? decode(o.data) : new Uint8Array();
|
|
206
|
+
const binary = bytes.includes(0);
|
|
207
|
+
const truncated = bytes.length > CAP;
|
|
208
|
+
return {
|
|
209
|
+
path: typeof o.path === 'string' ? o.path : path,
|
|
210
|
+
dir,
|
|
211
|
+
names,
|
|
212
|
+
text: binary || truncated ? '' : new TextDecoder().decode(bytes),
|
|
213
|
+
binary,
|
|
214
|
+
truncated,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function decode(b64) {
|
|
218
|
+
const bin = atob(b64);
|
|
219
|
+
const out = new Uint8Array(bin.length);
|
|
220
|
+
for (let i = 0; i < bin.length; i++)
|
|
221
|
+
out[i] = bin.charCodeAt(i);
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { type Target } from './call.ts';
|
|
2
|
+
export type Status = 'running' | 'paused' | 'done' | 'error' | 'stopped' | string;
|
|
3
|
+
export interface Session {
|
|
4
|
+
id: string;
|
|
5
|
+
title: string;
|
|
6
|
+
status: Status;
|
|
7
|
+
agent: string;
|
|
8
|
+
actor: string;
|
|
9
|
+
repo: string;
|
|
10
|
+
project: string;
|
|
11
|
+
target: string;
|
|
12
|
+
/** The hostname of the machine it runs on, as that machine reported it, or ''. */
|
|
13
|
+
host: string;
|
|
14
|
+
/** The folder it is working in now, as its machine reports it (a linked shell moves), or ''. */
|
|
15
|
+
cwd: string;
|
|
16
|
+
/** What sort of run: 'coding' for a coding run. */
|
|
17
|
+
kind: string;
|
|
18
|
+
/** The branch the run started from, or ''. */
|
|
19
|
+
base: string;
|
|
20
|
+
/** The branch the run pushes to — the only one it may write. */
|
|
21
|
+
branch: string;
|
|
22
|
+
/** 'sandbox', or the id of the org's machine it runs on. */
|
|
23
|
+
environment: string;
|
|
24
|
+
/** 'build' or 'plan', as the coding service recorded it; '' for other runs. */
|
|
25
|
+
mode: string;
|
|
26
|
+
/** The pull request the run proposed, or ''. */
|
|
27
|
+
pr: string;
|
|
28
|
+
/** The sandbox the run leased, once it has one. It is gone when the run ends. */
|
|
29
|
+
sandbox: string;
|
|
30
|
+
/** The org the run is in, which the public build route is addressed by. */
|
|
31
|
+
org: string;
|
|
32
|
+
/** Whether its story is open to the public build route. Only a run that names a project can be. */
|
|
33
|
+
published: boolean;
|
|
34
|
+
/** Where it was started: 'app' for a conversation in Dev (talk.ts), '' for a run on its own. */
|
|
35
|
+
channel: string;
|
|
36
|
+
/** The session it belongs to — a conversation, for the coding runs it started — or ''. */
|
|
37
|
+
parent: string;
|
|
38
|
+
/** The top of its tree: its own id for a session of its own. A stream is followed by it. */
|
|
39
|
+
root: string;
|
|
40
|
+
events: number;
|
|
41
|
+
createdAt: string;
|
|
42
|
+
updatedAt: string;
|
|
43
|
+
endedAt: string;
|
|
44
|
+
}
|
|
45
|
+
export interface Event {
|
|
46
|
+
id: string;
|
|
47
|
+
sessionId: string;
|
|
48
|
+
seq: number;
|
|
49
|
+
kind: string;
|
|
50
|
+
actor: string;
|
|
51
|
+
payload: unknown;
|
|
52
|
+
createdAt: string;
|
|
53
|
+
}
|
|
54
|
+
export interface Detail extends Session {
|
|
55
|
+
recent: Event[];
|
|
56
|
+
}
|
|
57
|
+
export declare function session(raw: unknown): Session;
|
|
58
|
+
export declare function event(raw: unknown): Event;
|
|
59
|
+
/**
|
|
60
|
+
* How long runs in one mode took, in whole minutes: the median and the ninetieth
|
|
61
|
+
* percentile of the finished ones. Null under three, which is too few to say.
|
|
62
|
+
*/
|
|
63
|
+
export declare function took(runs: Session[], mode: string): [number, number] | null;
|
|
64
|
+
export interface ListQuery {
|
|
65
|
+
kind?: string;
|
|
66
|
+
/** The sessions started on one channel: 'app' for Dev's conversations. */
|
|
67
|
+
channel?: string;
|
|
68
|
+
project?: string;
|
|
69
|
+
/** running, paused, done or error: the four the platform filters on. */
|
|
70
|
+
status?: string;
|
|
71
|
+
limit?: number;
|
|
72
|
+
/** The `next` of the page before. */
|
|
73
|
+
after?: string;
|
|
74
|
+
}
|
|
75
|
+
export interface Page {
|
|
76
|
+
sessions: Session[];
|
|
77
|
+
/** The cursor for the page after this one, or '' on the last. */
|
|
78
|
+
next: string;
|
|
79
|
+
}
|
|
80
|
+
export declare function page(t: Target, q?: ListQuery): Promise<Page>;
|
|
81
|
+
export declare function list(t: Target, q?: ListQuery): Promise<Session[]>;
|
|
82
|
+
export interface GetOptions {
|
|
83
|
+
limit?: number;
|
|
84
|
+
after?: number;
|
|
85
|
+
}
|
|
86
|
+
export declare function get(t: Target, id: string, opts?: GetOptions): Promise<Detail>;
|
|
87
|
+
/**
|
|
88
|
+
* A session's whole log, oldest first, read a page at a time — what a conversation
|
|
89
|
+
* draws from its first word, where the detail carries only the latest fifty.
|
|
90
|
+
*/
|
|
91
|
+
export declare function log(t: Target, id: string): Promise<Event[]>;
|
|
92
|
+
export declare function message(t: Target, id: string, text: string): Promise<void>;
|
|
93
|
+
export declare function stop(t: Target, id: string, why?: string): Promise<void>;
|
|
94
|
+
/** A run, deleted for good: its record and every link it was shared by. */
|
|
95
|
+
export declare function remove(t: Target, id: string): Promise<void>;
|
|
96
|
+
export declare function pause(t: Target, id: string): Promise<void>;
|
|
97
|
+
export declare function resume(t: Target, id: string): Promise<void>;
|
|
98
|
+
/** A new title, up to the platform's 512 characters. */
|
|
99
|
+
export declare function rename(t: Target, id: string, title: string): Promise<Session>;
|
|
100
|
+
/**
|
|
101
|
+
* Open the run's story to the public build route, or close it. The platform
|
|
102
|
+
* refuses to open one that names no project, because that route is keyed on
|
|
103
|
+
* the org and the project.
|
|
104
|
+
*/
|
|
105
|
+
export declare function publish(t: Target, id: string, on: boolean): Promise<Session>;
|
|
106
|
+
/** Where anyone can read a published run's story: the public build route, by org and project. '' without both. */
|
|
107
|
+
export declare const story: (t: Target, org: string | null, project: string) => string;
|
|
108
|
+
export interface Watch {
|
|
109
|
+
session?: (s: Session) => void;
|
|
110
|
+
event?: (e: Event) => void;
|
|
111
|
+
/** Each (re)connect. The first is 0; a later one means frames may have been missed. */
|
|
112
|
+
open?: (attempt: number) => void;
|
|
113
|
+
}
|
|
114
|
+
export interface WatchOptions {
|
|
115
|
+
/** Resume replaying missed events strictly after this sequence number. */
|
|
116
|
+
after?: number;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Follow one run's tree until `signal` aborts. Reconnects with a capped backoff
|
|
120
|
+
* and replays missed frames via `?after=<lastSeq>` + `Last-Event-ID`; a refusal
|
|
121
|
+
* (401/403) ends it, because retrying a no is not recovery.
|
|
122
|
+
*/
|
|
123
|
+
export declare function watch(t: Target, root: string, on: Watch, signal: AbortSignal, opts?: WatchOptions): Promise<void>;
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runs, as the platform records them: one session per run — and Dev's
|
|
3
|
+
* conversations (talk.ts), one session for every turn.
|
|
4
|
+
*
|
|
5
|
+
* GET /v1/agent/sessions?kind=&channel=&project=&status=&limit=&after= newest first, {sessions, next}
|
|
6
|
+
* GET /v1/agent/sessions/{id} + the 50 most recent events
|
|
7
|
+
* GET /v1/agent/sessions/{id}/events?after=&limit= its whole log, a page at a time, {events, next}
|
|
8
|
+
* PATCH /v1/agent/sessions/{id} {title}|{published} rename it; open its story to the public build route
|
|
9
|
+
* POST /v1/agent/sessions/{id}/message {message} steer a running run
|
|
10
|
+
* POST /v1/agent/sessions/{id}/pause pause it; a sandbox run keeps its work on its branch
|
|
11
|
+
* POST /v1/agent/sessions/{id}/resume ask a paused run to go on
|
|
12
|
+
* POST /v1/agent/sessions/{id}/stop {message} end it, work kept
|
|
13
|
+
* GET /v1/agent/sessions/stream?root={id} SSE: `session` and `event` frames,
|
|
14
|
+
* each wrapped: {"session":{…}}, {"event":{…}}
|
|
15
|
+
*
|
|
16
|
+
* The stream is best-effort by the platform's own statement: a subscriber that
|
|
17
|
+
* falls 256 frames behind is dropped. So `watch` reconnects, and every
|
|
18
|
+
* reconnect is reported, which is the caller's cue to re-read the detail.
|
|
19
|
+
*/
|
|
20
|
+
import { phrase } from '@hanzo/i18n';
|
|
21
|
+
import { call, headers, query, Refusal, seg } from './call.js';
|
|
22
|
+
import { read } from './sse.js';
|
|
23
|
+
const str = (v) => (typeof v === 'string' ? v : '');
|
|
24
|
+
const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
|
|
25
|
+
const obj = (v) => v && typeof v === 'object' ? v : {};
|
|
26
|
+
export function session(raw) {
|
|
27
|
+
const s = obj(raw);
|
|
28
|
+
return {
|
|
29
|
+
id: str(s.id),
|
|
30
|
+
title: str(s.title),
|
|
31
|
+
status: str(s.status),
|
|
32
|
+
agent: str(s.agent),
|
|
33
|
+
actor: str(s.actor),
|
|
34
|
+
repo: str(s.repo),
|
|
35
|
+
project: str(s.project),
|
|
36
|
+
target: str(s.target),
|
|
37
|
+
host: str(s.host),
|
|
38
|
+
cwd: str(s.cwd),
|
|
39
|
+
kind: str(s.kind),
|
|
40
|
+
base: str(s.base),
|
|
41
|
+
branch: str(s.branch),
|
|
42
|
+
environment: str(s.environment),
|
|
43
|
+
mode: str(s.mode),
|
|
44
|
+
pr: str(s.pr),
|
|
45
|
+
sandbox: str(s.sandbox),
|
|
46
|
+
org: str(s.org),
|
|
47
|
+
published: s.published === true,
|
|
48
|
+
channel: str(s.channel),
|
|
49
|
+
parent: str(s.parentSessionId),
|
|
50
|
+
root: str(s.rootSessionId),
|
|
51
|
+
events: num(s.events),
|
|
52
|
+
createdAt: str(s.createdAt),
|
|
53
|
+
updatedAt: str(s.updatedAt),
|
|
54
|
+
endedAt: str(s.endedAt),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function event(raw) {
|
|
58
|
+
const e = obj(raw);
|
|
59
|
+
return {
|
|
60
|
+
id: str(e.id),
|
|
61
|
+
sessionId: str(e.sessionId),
|
|
62
|
+
seq: num(e.seq),
|
|
63
|
+
kind: str(e.kind),
|
|
64
|
+
actor: str(e.actor),
|
|
65
|
+
payload: e.payload,
|
|
66
|
+
createdAt: str(e.createdAt),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* How long runs in one mode took, in whole minutes: the median and the ninetieth
|
|
71
|
+
* percentile of the finished ones. Null under three, which is too few to say.
|
|
72
|
+
*/
|
|
73
|
+
export function took(runs, mode) {
|
|
74
|
+
const spans = runs
|
|
75
|
+
.filter((r) => r.mode === mode && r.status === 'done' && r.endedAt)
|
|
76
|
+
.map((r) => Date.parse(r.endedAt) - Date.parse(r.createdAt))
|
|
77
|
+
.filter((ms) => Number.isFinite(ms) && ms > 0)
|
|
78
|
+
.sort((a, b) => a - b);
|
|
79
|
+
if (spans.length < 3)
|
|
80
|
+
return null;
|
|
81
|
+
const at = (q) => Math.max(1, Math.round(spans[Math.min(spans.length - 1, Math.floor(q * spans.length))] / 60_000));
|
|
82
|
+
return [at(0.5), at(0.9)];
|
|
83
|
+
}
|
|
84
|
+
export async function page(t, q = {}) {
|
|
85
|
+
const raw = obj(await call(t, 'GET', `/v1/agent/sessions${query({ ...q })}`));
|
|
86
|
+
return {
|
|
87
|
+
sessions: (Array.isArray(raw.sessions) ? raw.sessions : []).map(session).filter((s) => s.id),
|
|
88
|
+
next: str(raw.next),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export async function list(t, q = {}) {
|
|
92
|
+
return (await page(t, q)).sessions;
|
|
93
|
+
}
|
|
94
|
+
export async function get(t, id, opts) {
|
|
95
|
+
const raw = obj(await call(t, 'GET', `/v1/agent/sessions/${seg(id)}${query({ limit: opts?.limit, after: opts?.after })}`));
|
|
96
|
+
let recent = (Array.isArray(raw.recentEvents) ? raw.recentEvents : []).map(event);
|
|
97
|
+
// When `limit` is explicitly requested (e.g. Run's initial 500-event load) and a
|
|
98
|
+
// long coding run still exceeds one page (`recent[0].seq > 1`), page forward from
|
|
99
|
+
// seq 0 so middle history is never dropped. Without `opts`, keep the lightweight
|
|
100
|
+
// 2-event head fetch so short summary callers stay single-hop.
|
|
101
|
+
if (recent.length > 0 && recent[0].seq > 1 && opts?.after === undefined) {
|
|
102
|
+
const tailFloor = recent[0].seq;
|
|
103
|
+
if (opts?.limit && opts.limit > 50) {
|
|
104
|
+
try {
|
|
105
|
+
const earlier = [];
|
|
106
|
+
let cursor = 0;
|
|
107
|
+
for (let page = 0; page < 6 && cursor < tailFloor - 1; page++) {
|
|
108
|
+
const chunk = obj(await call(t, 'GET', `/v1/agent/sessions/${seg(id)}/events${query({ after: cursor || undefined, limit: Math.min(1000, opts.limit) })}`));
|
|
109
|
+
const rows = (Array.isArray(chunk.events) ? chunk.events : []).map(event);
|
|
110
|
+
earlier.push(...rows.filter((e) => e.seq > 0 && e.seq < tailFloor));
|
|
111
|
+
const next = num(chunk.next);
|
|
112
|
+
if (next <= cursor || next >= tailFloor - 1)
|
|
113
|
+
break;
|
|
114
|
+
cursor = next;
|
|
115
|
+
}
|
|
116
|
+
const have = new Set(recent.map((e) => e.seq));
|
|
117
|
+
recent = [...earlier.filter((e) => !have.has(e.seq)), ...recent].sort((a, b) => a.seq - b.seq);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
/* keep the tail */
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
try {
|
|
125
|
+
const head = obj(await call(t, 'GET', `/v1/agent/sessions/${seg(id)}/events?limit=2`));
|
|
126
|
+
const first = (Array.isArray(head.events) ? head.events : []).map(event);
|
|
127
|
+
const have = new Set(recent.map((e) => e.seq));
|
|
128
|
+
recent = [...first.filter((e) => e.seq > 0 && !have.has(e.seq)), ...recent].sort((a, b) => a.seq - b.seq);
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
/* keep the tail */
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
...session(raw),
|
|
137
|
+
recent,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A session's whole log, oldest first, read a page at a time — what a conversation
|
|
142
|
+
* draws from its first word, where the detail carries only the latest fifty.
|
|
143
|
+
*/
|
|
144
|
+
export async function log(t, id) {
|
|
145
|
+
const out = [];
|
|
146
|
+
let after = 0;
|
|
147
|
+
for (;;) {
|
|
148
|
+
const raw = obj(await call(t, 'GET', `/v1/agent/sessions/${seg(id)}/events${query({ after: after || undefined, limit: 1000 })}`));
|
|
149
|
+
out.push(...(Array.isArray(raw.events) ? raw.events : []).map(event));
|
|
150
|
+
const next = num(raw.next);
|
|
151
|
+
if (next <= after)
|
|
152
|
+
return out;
|
|
153
|
+
after = next;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export async function message(t, id, text) {
|
|
157
|
+
const m = text.trim();
|
|
158
|
+
if (!m)
|
|
159
|
+
throw new Refusal(400, phrase('Say something to the run'));
|
|
160
|
+
await call(t, 'POST', `/v1/agent/sessions/${seg(id)}/message`, { message: m });
|
|
161
|
+
}
|
|
162
|
+
export async function stop(t, id, why = 'Stopped from the builder') {
|
|
163
|
+
await call(t, 'POST', `/v1/agent/sessions/${seg(id)}/stop`, { message: why });
|
|
164
|
+
}
|
|
165
|
+
/** A run, deleted for good: its record and every link it was shared by. */
|
|
166
|
+
export async function remove(t, id) {
|
|
167
|
+
await call(t, 'DELETE', `/v1/agent/sessions/${seg(id)}`);
|
|
168
|
+
}
|
|
169
|
+
export async function pause(t, id) {
|
|
170
|
+
await call(t, 'POST', `/v1/agent/sessions/${seg(id)}/pause`, {});
|
|
171
|
+
}
|
|
172
|
+
export async function resume(t, id) {
|
|
173
|
+
await call(t, 'POST', `/v1/agent/sessions/${seg(id)}/resume`, {});
|
|
174
|
+
}
|
|
175
|
+
/** A new title, up to the platform's 512 characters. */
|
|
176
|
+
export async function rename(t, id, title) {
|
|
177
|
+
const name = title.trim();
|
|
178
|
+
if (!name)
|
|
179
|
+
throw new Refusal(400, phrase('Give the run a name'));
|
|
180
|
+
if (name.length > 512)
|
|
181
|
+
throw new Refusal(400, phrase('A run’s name is at most 512 characters'));
|
|
182
|
+
return session(await call(t, 'PATCH', `/v1/agent/sessions/${seg(id)}`, { title: name }));
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Open the run's story to the public build route, or close it. The platform
|
|
186
|
+
* refuses to open one that names no project, because that route is keyed on
|
|
187
|
+
* the org and the project.
|
|
188
|
+
*/
|
|
189
|
+
export async function publish(t, id, on) {
|
|
190
|
+
return session(await call(t, 'PATCH', `/v1/agent/sessions/${seg(id)}`, { published: on }));
|
|
191
|
+
}
|
|
192
|
+
/** Where anyone can read a published run's story: the public build route, by org and project. '' without both. */
|
|
193
|
+
export const story = (t, org, project) => org && project ? `${t.api}/v1/agent/builds/${seg(org)}/${seg(project)}` : '';
|
|
194
|
+
/**
|
|
195
|
+
* Follow one run's tree until `signal` aborts. Reconnects with a capped backoff
|
|
196
|
+
* and replays missed frames via `?after=<lastSeq>` + `Last-Event-ID`; a refusal
|
|
197
|
+
* (401/403) ends it, because retrying a no is not recovery.
|
|
198
|
+
*/
|
|
199
|
+
export async function watch(t, root, on, signal, opts) {
|
|
200
|
+
let opened = 0;
|
|
201
|
+
let fails = 0;
|
|
202
|
+
let lastSeq = opts?.after && Number.isFinite(opts.after) && opts.after > 0 ? Math.floor(opts.after) : 0;
|
|
203
|
+
while (!signal.aborted) {
|
|
204
|
+
try {
|
|
205
|
+
const extra = { Accept: 'text/event-stream' };
|
|
206
|
+
if (lastSeq > 0)
|
|
207
|
+
extra['Last-Event-ID'] = String(lastSeq);
|
|
208
|
+
const res = await fetch(`${t.api}/v1/agent/sessions/stream${query({ root, after: lastSeq > 0 ? lastSeq : undefined })}`, {
|
|
209
|
+
headers: headers(t, extra),
|
|
210
|
+
cache: 'no-store',
|
|
211
|
+
signal,
|
|
212
|
+
});
|
|
213
|
+
if (res.status === 401 || res.status === 403)
|
|
214
|
+
throw new Refusal(res.status, phrase('This account cannot follow this run'));
|
|
215
|
+
if (!res.ok || !res.body)
|
|
216
|
+
throw new Error(`stream answered ${res.status}`);
|
|
217
|
+
on.open?.(opened++);
|
|
218
|
+
fails = 0;
|
|
219
|
+
await read(res.body, (f) => {
|
|
220
|
+
let data;
|
|
221
|
+
try {
|
|
222
|
+
data = JSON.parse(f.data);
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (f.event === 'session')
|
|
228
|
+
on.session?.(session(obj(data).session));
|
|
229
|
+
else if (f.event === 'event') {
|
|
230
|
+
const ev = event(obj(data).event);
|
|
231
|
+
if (!root || !ev.sessionId || ev.sessionId === root) {
|
|
232
|
+
const fid = Number(f.id);
|
|
233
|
+
if (Number.isInteger(fid) && fid > lastSeq)
|
|
234
|
+
lastSeq = fid;
|
|
235
|
+
if (ev.seq > lastSeq)
|
|
236
|
+
lastSeq = ev.seq;
|
|
237
|
+
}
|
|
238
|
+
on.event?.(ev);
|
|
239
|
+
}
|
|
240
|
+
}, signal);
|
|
241
|
+
}
|
|
242
|
+
catch (e) {
|
|
243
|
+
if (e instanceof Refusal)
|
|
244
|
+
throw e;
|
|
245
|
+
if (signal.aborted)
|
|
246
|
+
return;
|
|
247
|
+
fails += 1;
|
|
248
|
+
}
|
|
249
|
+
const wait = Math.min(15000, 1000 * 2 ** Math.min(fails, 4));
|
|
250
|
+
await new Promise((done) => {
|
|
251
|
+
const t = setTimeout(done, wait);
|
|
252
|
+
signal.addEventListener('abort', () => (clearTimeout(t), done()), { once: true });
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|