minicode-ai 0.11.0 → 0.13.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/CHANGELOG.md +98 -0
- package/LICENSE +21 -21
- package/README.md +4 -4
- package/cli/args.ts +46 -0
- package/cli/commands/acp.ts +163 -10
- package/cli/commands/config.ts +33 -18
- package/cli/commands/daemon.ts +515 -0
- package/cli/commands/exec.ts +145 -53
- package/cli/commands/openwork.ts +986 -0
- package/cli/commands/sessions.ts +4 -1
- package/cli/commands/stats.ts +34 -4
- package/cli/commands.ts +217 -2
- package/cli/index.ts +146 -14
- package/cli/recovery-warning.ts +115 -0
- package/cli/router.ts +32 -1
- package/cli/setup.ts +2332 -171
- package/cli/tui.ts +113 -7
- package/docs/OUTPUT_ARCHITECTURE_AUDIT.md +489 -0
- package/docs/OUTPUT_EVENT_MODEL.md +289 -0
- package/docs/OUTPUT_IMPLEMENTATION_PLAN.md +391 -0
- package/docs/OUTPUT_PROTOCOL_SPEC.md +285 -0
- package/docs/OUTPUT_RENDERING_SPEC.md +248 -0
- package/docs/OUTPUT_UX_RULES.md +449 -0
- package/docs/PLAN_UIUX_V6.md +2 -2
- package/docs/README.md +4 -3
- package/docs/TERMINAL_CONTRACT.md +278 -66
- package/docs/architecture.md +89 -7
- package/docs/changelog.md +1 -1
- package/docs/choosing-mode.md +2 -2
- package/docs/cli.md +9 -5
- package/docs/concepts.md +1 -1
- package/docs/contributing.md +9 -2
- package/docs/environment.md +6 -2
- package/docs/exec.md +46 -5
- package/docs/memory-sessions.md +2 -0
- package/docs/policy-sandbox.md +1 -1
- package/docs/pricing-budget.md +1 -1
- package/docs/quickstart.md +2 -2
- package/docs/repl.md +6 -2
- package/docs/security-model.md +1 -1
- package/docs/security.md +1 -1
- package/docs/skills.md +2 -2
- package/docs/terminal.md +9 -5
- package/docs/tools.md +1 -1
- package/package.json +35 -5
- package/src/app/session.ts +261 -15
- package/src/app/tool-layer.ts +94 -7
- package/src/daemon/approvals.ts +361 -0
- package/src/daemon/capability.ts +145 -0
- package/src/daemon/children.ts +142 -0
- package/src/daemon/client.ts +267 -0
- package/src/daemon/control.ts +350 -0
- package/src/daemon/discovery.ts +234 -0
- package/src/daemon/feed.ts +155 -0
- package/src/daemon/framing.ts +94 -0
- package/src/daemon/host.ts +316 -0
- package/src/daemon/protocol.ts +217 -0
- package/src/daemon/resource.ts +170 -0
- package/src/daemon/sanitize.ts +57 -0
- package/src/daemon/server.ts +627 -0
- package/src/daemon/store.ts +317 -0
- package/src/daemon/subscription.ts +270 -0
- package/src/daemon/telemetry.ts +81 -0
- package/src/hooks/run.ts +12 -1
- package/src/lib/atomic-write.ts +29 -3
- package/src/lib/db-path.ts +26 -0
- package/src/lib/keystore.ts +544 -42
- package/src/lib/net.ts +43 -1
- package/src/lib/safe-open.ts +116 -1
- package/src/lib/session-id.ts +15 -0
- package/src/lsp/client.ts +123 -5
- package/src/mcp/client.ts +139 -17
- package/src/mcp/server.ts +177 -50
- package/src/mcp/transport.ts +16 -19
- package/src/memory/vector.ts +770 -55
- package/src/policy/allowlist.ts +14 -2
- package/src/policy/bash-guard.ts +105 -10
- package/src/policy/compaction.ts +82 -5
- package/src/policy/consequence-engine.ts +220 -0
- package/src/policy/context.ts +132 -0
- package/src/policy/executor.ts +187 -121
- package/src/policy/no-progress.ts +559 -0
- package/src/policy/permission.ts +4 -8
- package/src/policy/pricing.ts +17 -3
- package/src/policy/scrub.ts +69 -2
- package/src/policy/tool-contract.ts +515 -0
- package/src/policy/usage.ts +310 -24
- package/src/policy/verifier.ts +6 -3
- package/src/policy/work-mode.ts +182 -0
- package/src/presentation/adapter.ts +533 -26
- package/src/presentation/events.ts +151 -0
- package/src/presentation/model.ts +180 -27
- package/src/presentation/projection.ts +858 -0
- package/src/presentation/reducer.ts +645 -310
- package/src/presentation/store.ts +12 -7
- package/src/providers/anthropic.ts +18 -10
- package/src/providers/responses.ts +17 -7
- package/src/runtime/capability.ts +409 -0
- package/src/runtime/child-execution.ts +274 -0
- package/src/runtime/composition.ts +958 -0
- package/src/runtime/dispatch.ts +591 -0
- package/src/runtime/execution-backend.ts +632 -0
- package/src/runtime/execution-events.ts +272 -0
- package/src/runtime/execution-id.ts +230 -0
- package/src/runtime/execution-journal.ts +926 -0
- package/src/runtime/execution-kernel.ts +456 -0
- package/src/runtime/execution-registry.ts +379 -0
- package/src/runtime/process-supervision.ts +276 -0
- package/src/runtime/production-execution.ts +551 -0
- package/src/runtime/production-runtime.ts +217 -0
- package/src/runtime/recovery-safety.ts +70 -0
- package/src/runtime/recovery.ts +477 -0
- package/src/runtime/runtime-capabilities.ts +179 -0
- package/src/runtime/runtime-diagnostics.ts +141 -0
- package/src/runtime/runtime-host.ts +365 -0
- package/src/runtime/runtime-status-facts.ts +222 -0
- package/src/runtime/runtime-status-provider.ts +74 -0
- package/src/runtime/runtime-status.ts +459 -0
- package/src/runtime/runtime-task-admission.ts +101 -0
- package/src/runtime/supervisor.ts +444 -0
- package/src/session/authority.ts +168 -0
- package/src/session/checkpoint.ts +88 -1
- package/src/session/context-adapter.ts +251 -0
- package/src/session/context-assembly.ts +242 -0
- package/src/session/context-identity.ts +389 -0
- package/src/session/context-projection.ts +248 -0
- package/src/session/context-selector.ts +500 -0
- package/src/session/cross-session-adoption.ts +249 -0
- package/src/session/full-history-fold.ts +171 -0
- package/src/session/identity.ts +192 -0
- package/src/session/journal.ts +805 -102
- package/src/session/open-work-intent.ts +732 -0
- package/src/session/open-work.ts +767 -0
- package/src/session/pending-tools.ts +94 -0
- package/src/session/persistence.ts +3569 -127
- package/src/session/runtime-header.ts +208 -0
- package/src/session/shadow-git.ts +13 -5
- package/src/session/verification.ts +1623 -0
- package/src/task/assignment.ts +136 -0
- package/src/task/autonomous-adapter.ts +168 -0
- package/src/task/autonomous-context.ts +630 -0
- package/src/task/autonomous-policy.ts +490 -0
- package/src/task/continuity.ts +179 -0
- package/src/task/execution-cancellation.ts +165 -0
- package/src/task/graph-validate.ts +379 -0
- package/src/task/graph.ts +305 -0
- package/src/task/identity.ts +230 -0
- package/src/task/model.ts +417 -0
- package/src/task/production-scheduler.ts +293 -0
- package/src/task/readiness.ts +229 -0
- package/src/task/scheduler-observability.ts +387 -0
- package/src/task/scheduler.ts +1197 -0
- package/src/task/scheduling-policy.ts +165 -0
- package/src/task/session-authority.ts +148 -0
- package/src/task/session-ownership.ts +211 -0
- package/src/task/store.ts +2887 -0
- package/src/task/sync.ts +343 -0
- package/src/task/trigger.ts +260 -0
- package/src/tools/bash.ts +377 -211
- package/src/tools/code_run.ts +1 -1
- package/src/tools/delete_file.ts +1 -1
- package/src/tools/edit.ts +33 -6
- package/src/tools/evidence.ts +30 -0
- package/src/tools/git.ts +4 -4
- package/src/tools/glob.ts +12 -3
- package/src/tools/grep.ts +1 -1
- package/src/tools/lsp.ts +4 -4
- package/src/tools/mcp_call.ts +11 -4
- package/src/tools/memory.ts +153 -18
- package/src/tools/move_file.ts +1 -1
- package/src/tools/patch.ts +1 -1
- package/src/tools/read_file.ts +1 -1
- package/src/tools/read_image.ts +1 -1
- package/src/tools/submit_result.ts +1 -1
- package/src/tools/task.ts +437 -41
- package/src/tools/todo.ts +662 -35
- package/src/tools/web_fetch.ts +4 -2
- package/src/tools/web_search.ts +9 -3
- package/src/tools/write_file.ts +1 -1
- package/src/ui/assistant/simple.ts +123 -50
- package/src/ui/assistant/turn-status.ts +17 -3
- package/src/ui/contract.ts +184 -0
- package/src/ui/footer.ts +54 -18
- package/src/ui/i18n/en.ts +62 -6
- package/src/ui/i18n/id.ts +62 -6
- package/src/ui/input/prompt-engine.ts +120 -25
- package/src/ui/render/errors.ts +39 -1
- package/src/ui/render/markdown-table.ts +455 -0
- package/src/ui/render/markdown.ts +22 -1
- package/src/ui/render/table-grid.ts +184 -0
- package/src/ui/render/table.ts +15 -76
- package/src/ui/runtime/screen.ts +7 -2
- package/src/ui/runtime/statusline.ts +25 -6
- package/src/ui/tui/app.ts +598 -133
- package/src/ui/tui/transcript.ts +531 -89
- package/vendor/minicore/VENDOR.md +1 -1
- package/vendor/minicore/src/core/budget.ts +25 -25
- package/vendor/minicore/src/core/compact.ts +75 -75
- package/vendor/minicore/src/core/errors.ts +59 -59
- package/vendor/minicore/src/core/events.ts +73 -73
- package/vendor/minicore/src/core/executor.ts +139 -139
- package/vendor/minicore/src/core/history.ts +31 -31
- package/vendor/minicore/src/core/index.ts +41 -41
- package/vendor/minicore/src/core/loop.ts +355 -355
- package/vendor/minicore/src/core/permission.ts +22 -22
- package/vendor/minicore/src/core/provider.ts +39 -39
- package/vendor/minicore/src/core/recovery.ts +35 -35
- package/vendor/minicore/src/core/session.ts +327 -327
- package/vendor/minicore/src/core/snapshot.ts +146 -146
- package/vendor/minicore/src/core/tokens.ts +107 -107
- package/vendor/minicore/src/core/tool.ts +115 -115
- package/vendor/minicore/src/core/types.ts +50 -50
- package/vendor/minicore/src/providers/openai-compat.ts +417 -417
- package/AGENTS.md +0 -45
- package/PLAN.md +0 -491
- package/docs/ARCHITECTURE.html +0 -1248
- package/docs/CONTROL-PLANE-MAP.md +0 -139
- package/docs/HARNESS.md +0 -150
- package/docs/SUMMARY.md +0 -52
- package/docs/UI_RENDER_PIPELINE.md +0 -165
- package/docs/USAGE.md +0 -11
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
// Session: configuration binding + lifecycle records. The loop lives in
|
|
2
|
-
// loop.ts; this module only composes the parts and exposes a minimal API.
|
|
3
|
-
|
|
4
|
-
import type { BudgetPolicy } from "./budget.ts";
|
|
5
|
-
import { defaultBudgetPolicy } from "./budget.ts";
|
|
6
|
-
import type { CompactionStrategy } from "./compact.ts";
|
|
7
|
-
import { mechanicalCompaction } from "./compact.ts";
|
|
8
|
-
import { AgentError, abortError } from "./errors.ts";
|
|
9
|
-
import type { AgentEvent, EventBus } from "./events.ts";
|
|
10
|
-
import { createEventBus } from "./events.ts";
|
|
11
|
-
import type { ToolExecutor } from "./executor.ts";
|
|
12
|
-
import { sequentialExecutor } from "./executor.ts";
|
|
13
|
-
import { ContextStore } from "./history.ts";
|
|
14
|
-
import type { PermissionHandler } from "./permission.ts";
|
|
15
|
-
import type { ModelProvider } from "./provider.ts";
|
|
16
|
-
import type { RecoveryPolicy } from "./recovery.ts";
|
|
17
|
-
import { defaultRecoveryPolicy } from "./recovery.ts";
|
|
18
|
-
import { executeTurn } from "./loop.ts";
|
|
19
|
-
import { snapshotState, snapshotTurnResult } from "./snapshot.ts";
|
|
20
|
-
import type { Tool, ToolRegistry } from "./tool.ts";
|
|
21
|
-
import { createToolRegistry } from "./tool.ts";
|
|
22
|
-
import type { TokenEstimator } from "./tokens.ts";
|
|
23
|
-
import { defaultTokenEstimator, estimateSessionContext } from "./tokens.ts";
|
|
24
|
-
import type { Message, ToolCall, ToolResult } from "./types.ts";
|
|
25
|
-
|
|
26
|
-
export const DEFAULT_MAX_STEPS = 50;
|
|
27
|
-
export const DEFAULT_CONTEXT_WINDOW_TOKENS = 128_000;
|
|
28
|
-
export const DEFAULT_TOOL_RESULT_MAX_TOKENS = 4_096;
|
|
29
|
-
export const DEFAULT_KEEP_RECENT_TURNS = 3;
|
|
30
|
-
|
|
31
|
-
export const DEFAULT_TIMEOUT_MS = 600_000;
|
|
32
|
-
|
|
33
|
-
export interface SessionConfig {
|
|
34
|
-
provider: ModelProvider;
|
|
35
|
-
permissions: PermissionHandler;
|
|
36
|
-
tools?: readonly Tool[];
|
|
37
|
-
system?: string;
|
|
38
|
-
model?: string;
|
|
39
|
-
cwd?: string;
|
|
40
|
-
/**
|
|
41
|
-
* Session permission mode, forwarded to each turn's ToolContext so tools
|
|
42
|
-
* can adapt their behavior. Plain string or live getter — prefer the
|
|
43
|
-
* getter when the mode can change at runtime. Optional; absent preserves
|
|
44
|
-
* the legacy behavior (ctx.permissionMode undefined).
|
|
45
|
-
*/
|
|
46
|
-
permissionMode?: string | (() => string);
|
|
47
|
-
/**
|
|
48
|
-
* Seed history untuk sesi baru (mis. resume dari storage). Messages akan
|
|
49
|
-
* di-append ke ContextStore saat createSession, jadi `session.state.history`
|
|
50
|
-
* langsung memuatnya dan turn berikutnya meneruskan konteks penuh.
|
|
51
|
-
*/
|
|
52
|
-
initialMessages?: readonly Message[];
|
|
53
|
-
turnCount?: number;
|
|
54
|
-
stepCount?: number;
|
|
55
|
-
executor?: ToolExecutor;
|
|
56
|
-
budget?: BudgetPolicy;
|
|
57
|
-
estimator?: TokenEstimator;
|
|
58
|
-
compaction?: CompactionStrategy;
|
|
59
|
-
recovery?: RecoveryPolicy;
|
|
60
|
-
maxSteps?: number;
|
|
61
|
-
contextWindowTokens?: number;
|
|
62
|
-
toolResultMaxTokens?: number;
|
|
63
|
-
keepRecentTurns?: number;
|
|
64
|
-
/**
|
|
65
|
-
* Hard deadline for a single run() call, in milliseconds. When exceeded, the
|
|
66
|
-
* turn is aborted and run() rejects with AgentError("timeout") — even if the
|
|
67
|
-
* in-flight provider/tool ignores the abort signal. Defaults to
|
|
68
|
-
* DEFAULT_TIMEOUT_MS (10 minutes); set to Infinity to disable.
|
|
69
|
-
*/
|
|
70
|
-
timeoutMs?: number;
|
|
71
|
-
/**
|
|
72
|
-
* Reports errors thrown by event listeners. Observer code must never be able
|
|
73
|
-
* to break a turn (lifecycle/executor events) nor trigger a spurious retry
|
|
74
|
-
* (provider streaming events): emit catches every handler error, reports it
|
|
75
|
-
* here, and continues. Defaults to console.error.
|
|
76
|
-
*/
|
|
77
|
-
onHandlerError?: (error: unknown, event: AgentEvent) => void;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface SessionState {
|
|
81
|
-
readonly history: readonly Message[];
|
|
82
|
-
turnCount: number;
|
|
83
|
-
stepCount: number;
|
|
84
|
-
readonly model?: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface Execution {
|
|
88
|
-
readonly call: ToolCall;
|
|
89
|
-
readonly result: ToolResult;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export interface Step {
|
|
93
|
-
readonly index: number;
|
|
94
|
-
readonly toolCalls: readonly ToolCall[];
|
|
95
|
-
readonly results: readonly ToolResult[];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export interface TurnResult {
|
|
99
|
-
readonly steps: readonly Step[];
|
|
100
|
-
readonly finalText?: string;
|
|
101
|
-
readonly usage: { readonly turns: number; readonly steps: number };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface Session {
|
|
105
|
-
/**
|
|
106
|
-
* Point-in-time defensive snapshot of the committed session state.
|
|
107
|
-
*
|
|
108
|
-
* Each access deep-copies the committed history and counters, so a
|
|
109
|
-
* same-process caller that casts away the read-only types and mutates the
|
|
110
|
-
* result can never corrupt the kernel's state (the committed history that
|
|
111
|
-
* feeds the next turn) — the mutation touches only the caller's copy.
|
|
112
|
-
*
|
|
113
|
-
* Semantics: the returned value is a snapshot, not a live view. A reference
|
|
114
|
-
* held across a run stays frozen at the moment it was read; re-access
|
|
115
|
-
* `session.state` for fresh data. Committed state changes only when a turn
|
|
116
|
-
* completes successfully, and for change notification prefer the event bus
|
|
117
|
-
* (`turn:completed`, `step:completed`). Cost is O(history) per access, so
|
|
118
|
-
* read once per turn rather than polling.
|
|
119
|
-
*/
|
|
120
|
-
readonly state: Readonly<SessionState>;
|
|
121
|
-
readonly events: EventBus;
|
|
122
|
-
/**
|
|
123
|
-
* Current context size from the same source of truth as the loop's
|
|
124
|
-
* pressure evaluation (messages + system + tools under the session
|
|
125
|
-
* estimator). One number for drivers and UI — no duplicate estimator.
|
|
126
|
-
* O(history) per access (snapshot pattern); recomputed over the
|
|
127
|
-
* committed store on every access.
|
|
128
|
-
*/
|
|
129
|
-
readonly contextTokens: number;
|
|
130
|
-
run(input: string, opts?: { signal?: AbortSignal; model?: string }): Promise<TurnResult>;
|
|
131
|
-
abort(): void;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Internal handle consumed by loop.ts. Not part of the public surface. */
|
|
135
|
-
export interface SessionInternal {
|
|
136
|
-
readonly provider: ModelProvider;
|
|
137
|
-
readonly permissions: PermissionHandler;
|
|
138
|
-
readonly registry: ToolRegistry;
|
|
139
|
-
readonly executor: ToolExecutor;
|
|
140
|
-
readonly system?: string;
|
|
141
|
-
readonly model?: string;
|
|
142
|
-
readonly cwd?: string;
|
|
143
|
-
readonly permissionMode?: string | (() => string);
|
|
144
|
-
readonly budget: BudgetPolicy;
|
|
145
|
-
readonly estimator: TokenEstimator;
|
|
146
|
-
readonly compaction: CompactionStrategy;
|
|
147
|
-
readonly recovery: RecoveryPolicy;
|
|
148
|
-
readonly maxSteps: number;
|
|
149
|
-
readonly contextWindowTokens: number;
|
|
150
|
-
readonly toolResultMaxTokens: number;
|
|
151
|
-
readonly keepRecentTurns: number;
|
|
152
|
-
readonly timeoutMs: number;
|
|
153
|
-
readonly store: ContextStore;
|
|
154
|
-
readonly events: EventBus;
|
|
155
|
-
readonly state: SessionState;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
interface SessionImpl extends SessionInternal {
|
|
159
|
-
sessionAbort: AbortController;
|
|
160
|
-
running: boolean;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function createSession(config: SessionConfig): Session {
|
|
164
|
-
const store = new ContextStore();
|
|
165
|
-
if (config.initialMessages?.length) store.appendAll(config.initialMessages);
|
|
166
|
-
const events = createEventBus({ onHandlerError: config.onHandlerError });
|
|
167
|
-
const sessionAbort = new AbortController();
|
|
168
|
-
const state: SessionState = {
|
|
169
|
-
history: store.messages,
|
|
170
|
-
turnCount: config.turnCount ?? 0,
|
|
171
|
-
stepCount: config.stepCount ?? 0,
|
|
172
|
-
model: config.model,
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const impl: SessionImpl = {
|
|
176
|
-
provider: config.provider,
|
|
177
|
-
permissions: config.permissions,
|
|
178
|
-
registry: createToolRegistry(config.tools ?? []),
|
|
179
|
-
executor: config.executor ?? sequentialExecutor(),
|
|
180
|
-
system: config.system,
|
|
181
|
-
model: config.model,
|
|
182
|
-
cwd: config.cwd,
|
|
183
|
-
permissionMode: config.permissionMode,
|
|
184
|
-
budget: config.budget ?? defaultBudgetPolicy,
|
|
185
|
-
estimator: config.estimator ?? defaultTokenEstimator,
|
|
186
|
-
compaction: config.compaction ?? mechanicalCompaction,
|
|
187
|
-
recovery: config.recovery ?? defaultRecoveryPolicy,
|
|
188
|
-
maxSteps: config.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
189
|
-
contextWindowTokens: config.contextWindowTokens ?? DEFAULT_CONTEXT_WINDOW_TOKENS,
|
|
190
|
-
toolResultMaxTokens: config.toolResultMaxTokens ?? DEFAULT_TOOL_RESULT_MAX_TOKENS,
|
|
191
|
-
keepRecentTurns: config.keepRecentTurns ?? DEFAULT_KEEP_RECENT_TURNS,
|
|
192
|
-
timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
193
|
-
store,
|
|
194
|
-
events,
|
|
195
|
-
state,
|
|
196
|
-
sessionAbort,
|
|
197
|
-
running: false,
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
return {
|
|
201
|
-
// Public state crosses a host boundary, so it is a defensive snapshot per
|
|
202
|
-
// access (same policy as provider requests, tool ctx.state, and event
|
|
203
|
-
// payloads). The kernel keeps the raw object internally via `impl.state`.
|
|
204
|
-
get state() {
|
|
205
|
-
return snapshotState(state);
|
|
206
|
-
},
|
|
207
|
-
get events() {
|
|
208
|
-
return events;
|
|
209
|
-
},
|
|
210
|
-
// Same source of truth as the loop — estimateSessionContext in
|
|
211
|
-
// tokens.ts — never a duplicate estimator.
|
|
212
|
-
get contextTokens() {
|
|
213
|
-
return estimateSessionContext(store, config.system, impl.registry.list(), impl.estimator);
|
|
214
|
-
},
|
|
215
|
-
async run(input, opts) {
|
|
216
|
-
if (impl.running) throw new AgentError("busy", "session is already running");
|
|
217
|
-
impl.running = true;
|
|
218
|
-
const timeout = createTimeout(impl.timeoutMs);
|
|
219
|
-
const joined = joinSignals(impl.sessionAbort.signal, opts?.signal, timeout.signal);
|
|
220
|
-
const turnStore = new ContextStore();
|
|
221
|
-
turnStore.appendAll(store.messages);
|
|
222
|
-
const turnState: SessionState = {
|
|
223
|
-
history: turnStore.messages,
|
|
224
|
-
turnCount: state.turnCount,
|
|
225
|
-
stepCount: state.stepCount,
|
|
226
|
-
model: opts?.model ?? config.model,
|
|
227
|
-
};
|
|
228
|
-
const turnImpl: SessionInternal = {
|
|
229
|
-
...impl,
|
|
230
|
-
store: turnStore,
|
|
231
|
-
state: turnState,
|
|
232
|
-
};
|
|
233
|
-
try {
|
|
234
|
-
const turn = executeTurn(turnImpl, input, joined.signal, opts?.model);
|
|
235
|
-
// The race guarantees run() rejects as soon as the joined signal
|
|
236
|
-
// aborts (user abort, session.abort, or timeout), even if the
|
|
237
|
-
// in-flight provider/tool ignores the signal. Any late work mutates
|
|
238
|
-
// only turnStore and is discarded.
|
|
239
|
-
const result = await withAbort(turn, joined.signal);
|
|
240
|
-
// A run is transactional with respect to model context: failed,
|
|
241
|
-
// aborted, or timed-out turns are discarded instead of leaving orphaned
|
|
242
|
-
// user messages or assistant tool_calls without matching tool results.
|
|
243
|
-
// Non-cooperative providers/tools may still settle after withTimeout
|
|
244
|
-
// rejects, but they mutate only this private turnStore.
|
|
245
|
-
store.replace(0, store.messages.length, turnStore.messages);
|
|
246
|
-
state.turnCount = turnState.turnCount;
|
|
247
|
-
state.stepCount = turnState.stepCount;
|
|
248
|
-
events.emit({ type: "turn:completed", result: snapshotTurnResult(result) });
|
|
249
|
-
return result;
|
|
250
|
-
} finally {
|
|
251
|
-
timeout.cancel();
|
|
252
|
-
joined.cleanup();
|
|
253
|
-
impl.running = false;
|
|
254
|
-
if (impl.sessionAbort.signal.aborted) impl.sessionAbort = new AbortController();
|
|
255
|
-
}
|
|
256
|
-
},
|
|
257
|
-
abort() {
|
|
258
|
-
if (impl.running) impl.sessionAbort.abort(new AgentError("aborted", "turn aborted"));
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function joinSignals(...signals: Array<AbortSignal | undefined>): { signal: AbortSignal; cleanup(): void } {
|
|
264
|
-
const active = signals.filter((s): s is AbortSignal => s !== undefined);
|
|
265
|
-
if (active.length <= 1) {
|
|
266
|
-
return active[0] ? { signal: active[0], cleanup: noop } : { signal: new AbortController().signal, cleanup: noop };
|
|
267
|
-
}
|
|
268
|
-
const alreadyAborted = active.find((s) => s.aborted);
|
|
269
|
-
if (alreadyAborted) return { signal: alreadyAborted, cleanup: noop };
|
|
270
|
-
const controller = new AbortController();
|
|
271
|
-
const handlers: Array<[AbortSignal, () => void]> = active.map((s) => {
|
|
272
|
-
const onAbort = () => controller.abort(s.reason);
|
|
273
|
-
s.addEventListener("abort", onAbort, { once: true });
|
|
274
|
-
return [s, onAbort];
|
|
275
|
-
});
|
|
276
|
-
return {
|
|
277
|
-
signal: controller.signal,
|
|
278
|
-
cleanup() {
|
|
279
|
-
for (const [signal, onAbort] of handlers) signal.removeEventListener("abort", onAbort);
|
|
280
|
-
},
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
function noop(): void {}
|
|
285
|
-
|
|
286
|
-
interface Timeout {
|
|
287
|
-
readonly signal: AbortSignal;
|
|
288
|
-
cancel(): void;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
function createTimeout(ms: number): Timeout {
|
|
292
|
-
const controller = new AbortController();
|
|
293
|
-
const error = new AgentError("timeout", `turn exceeded ${ms}ms`);
|
|
294
|
-
// setTimeout clamps a non-finite delay to 1ms in Node/Bun, so Infinity would
|
|
295
|
-
// abort the turn almost immediately instead of disabling the deadline.
|
|
296
|
-
const timer = Number.isFinite(ms) ? setTimeout(() => controller.abort(error), ms) : undefined;
|
|
297
|
-
return {
|
|
298
|
-
signal: controller.signal,
|
|
299
|
-
cancel: () => {
|
|
300
|
-
if (timer !== undefined) clearTimeout(timer);
|
|
301
|
-
},
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
function withAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
306
|
-
if (signal.aborted) return Promise.reject(abortError(signal));
|
|
307
|
-
return new Promise((resolve, reject) => {
|
|
308
|
-
const onAbort = () => {
|
|
309
|
-
cleanup();
|
|
310
|
-
reject(abortError(signal));
|
|
311
|
-
};
|
|
312
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
313
|
-
promise.then(
|
|
314
|
-
(value) => {
|
|
315
|
-
cleanup();
|
|
316
|
-
resolve(value);
|
|
317
|
-
},
|
|
318
|
-
(cause) => {
|
|
319
|
-
cleanup();
|
|
320
|
-
reject(cause);
|
|
321
|
-
},
|
|
322
|
-
);
|
|
323
|
-
function cleanup() {
|
|
324
|
-
signal.removeEventListener("abort", onAbort);
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
}
|
|
1
|
+
// Session: configuration binding + lifecycle records. The loop lives in
|
|
2
|
+
// loop.ts; this module only composes the parts and exposes a minimal API.
|
|
3
|
+
|
|
4
|
+
import type { BudgetPolicy } from "./budget.ts";
|
|
5
|
+
import { defaultBudgetPolicy } from "./budget.ts";
|
|
6
|
+
import type { CompactionStrategy } from "./compact.ts";
|
|
7
|
+
import { mechanicalCompaction } from "./compact.ts";
|
|
8
|
+
import { AgentError, abortError } from "./errors.ts";
|
|
9
|
+
import type { AgentEvent, EventBus } from "./events.ts";
|
|
10
|
+
import { createEventBus } from "./events.ts";
|
|
11
|
+
import type { ToolExecutor } from "./executor.ts";
|
|
12
|
+
import { sequentialExecutor } from "./executor.ts";
|
|
13
|
+
import { ContextStore } from "./history.ts";
|
|
14
|
+
import type { PermissionHandler } from "./permission.ts";
|
|
15
|
+
import type { ModelProvider } from "./provider.ts";
|
|
16
|
+
import type { RecoveryPolicy } from "./recovery.ts";
|
|
17
|
+
import { defaultRecoveryPolicy } from "./recovery.ts";
|
|
18
|
+
import { executeTurn } from "./loop.ts";
|
|
19
|
+
import { snapshotState, snapshotTurnResult } from "./snapshot.ts";
|
|
20
|
+
import type { Tool, ToolRegistry } from "./tool.ts";
|
|
21
|
+
import { createToolRegistry } from "./tool.ts";
|
|
22
|
+
import type { TokenEstimator } from "./tokens.ts";
|
|
23
|
+
import { defaultTokenEstimator, estimateSessionContext } from "./tokens.ts";
|
|
24
|
+
import type { Message, ToolCall, ToolResult } from "./types.ts";
|
|
25
|
+
|
|
26
|
+
export const DEFAULT_MAX_STEPS = 50;
|
|
27
|
+
export const DEFAULT_CONTEXT_WINDOW_TOKENS = 128_000;
|
|
28
|
+
export const DEFAULT_TOOL_RESULT_MAX_TOKENS = 4_096;
|
|
29
|
+
export const DEFAULT_KEEP_RECENT_TURNS = 3;
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_TIMEOUT_MS = 600_000;
|
|
32
|
+
|
|
33
|
+
export interface SessionConfig {
|
|
34
|
+
provider: ModelProvider;
|
|
35
|
+
permissions: PermissionHandler;
|
|
36
|
+
tools?: readonly Tool[];
|
|
37
|
+
system?: string;
|
|
38
|
+
model?: string;
|
|
39
|
+
cwd?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Session permission mode, forwarded to each turn's ToolContext so tools
|
|
42
|
+
* can adapt their behavior. Plain string or live getter — prefer the
|
|
43
|
+
* getter when the mode can change at runtime. Optional; absent preserves
|
|
44
|
+
* the legacy behavior (ctx.permissionMode undefined).
|
|
45
|
+
*/
|
|
46
|
+
permissionMode?: string | (() => string);
|
|
47
|
+
/**
|
|
48
|
+
* Seed history untuk sesi baru (mis. resume dari storage). Messages akan
|
|
49
|
+
* di-append ke ContextStore saat createSession, jadi `session.state.history`
|
|
50
|
+
* langsung memuatnya dan turn berikutnya meneruskan konteks penuh.
|
|
51
|
+
*/
|
|
52
|
+
initialMessages?: readonly Message[];
|
|
53
|
+
turnCount?: number;
|
|
54
|
+
stepCount?: number;
|
|
55
|
+
executor?: ToolExecutor;
|
|
56
|
+
budget?: BudgetPolicy;
|
|
57
|
+
estimator?: TokenEstimator;
|
|
58
|
+
compaction?: CompactionStrategy;
|
|
59
|
+
recovery?: RecoveryPolicy;
|
|
60
|
+
maxSteps?: number;
|
|
61
|
+
contextWindowTokens?: number;
|
|
62
|
+
toolResultMaxTokens?: number;
|
|
63
|
+
keepRecentTurns?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Hard deadline for a single run() call, in milliseconds. When exceeded, the
|
|
66
|
+
* turn is aborted and run() rejects with AgentError("timeout") — even if the
|
|
67
|
+
* in-flight provider/tool ignores the abort signal. Defaults to
|
|
68
|
+
* DEFAULT_TIMEOUT_MS (10 minutes); set to Infinity to disable.
|
|
69
|
+
*/
|
|
70
|
+
timeoutMs?: number;
|
|
71
|
+
/**
|
|
72
|
+
* Reports errors thrown by event listeners. Observer code must never be able
|
|
73
|
+
* to break a turn (lifecycle/executor events) nor trigger a spurious retry
|
|
74
|
+
* (provider streaming events): emit catches every handler error, reports it
|
|
75
|
+
* here, and continues. Defaults to console.error.
|
|
76
|
+
*/
|
|
77
|
+
onHandlerError?: (error: unknown, event: AgentEvent) => void;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface SessionState {
|
|
81
|
+
readonly history: readonly Message[];
|
|
82
|
+
turnCount: number;
|
|
83
|
+
stepCount: number;
|
|
84
|
+
readonly model?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface Execution {
|
|
88
|
+
readonly call: ToolCall;
|
|
89
|
+
readonly result: ToolResult;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface Step {
|
|
93
|
+
readonly index: number;
|
|
94
|
+
readonly toolCalls: readonly ToolCall[];
|
|
95
|
+
readonly results: readonly ToolResult[];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface TurnResult {
|
|
99
|
+
readonly steps: readonly Step[];
|
|
100
|
+
readonly finalText?: string;
|
|
101
|
+
readonly usage: { readonly turns: number; readonly steps: number };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface Session {
|
|
105
|
+
/**
|
|
106
|
+
* Point-in-time defensive snapshot of the committed session state.
|
|
107
|
+
*
|
|
108
|
+
* Each access deep-copies the committed history and counters, so a
|
|
109
|
+
* same-process caller that casts away the read-only types and mutates the
|
|
110
|
+
* result can never corrupt the kernel's state (the committed history that
|
|
111
|
+
* feeds the next turn) — the mutation touches only the caller's copy.
|
|
112
|
+
*
|
|
113
|
+
* Semantics: the returned value is a snapshot, not a live view. A reference
|
|
114
|
+
* held across a run stays frozen at the moment it was read; re-access
|
|
115
|
+
* `session.state` for fresh data. Committed state changes only when a turn
|
|
116
|
+
* completes successfully, and for change notification prefer the event bus
|
|
117
|
+
* (`turn:completed`, `step:completed`). Cost is O(history) per access, so
|
|
118
|
+
* read once per turn rather than polling.
|
|
119
|
+
*/
|
|
120
|
+
readonly state: Readonly<SessionState>;
|
|
121
|
+
readonly events: EventBus;
|
|
122
|
+
/**
|
|
123
|
+
* Current context size from the same source of truth as the loop's
|
|
124
|
+
* pressure evaluation (messages + system + tools under the session
|
|
125
|
+
* estimator). One number for drivers and UI — no duplicate estimator.
|
|
126
|
+
* O(history) per access (snapshot pattern); recomputed over the
|
|
127
|
+
* committed store on every access.
|
|
128
|
+
*/
|
|
129
|
+
readonly contextTokens: number;
|
|
130
|
+
run(input: string, opts?: { signal?: AbortSignal; model?: string }): Promise<TurnResult>;
|
|
131
|
+
abort(): void;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Internal handle consumed by loop.ts. Not part of the public surface. */
|
|
135
|
+
export interface SessionInternal {
|
|
136
|
+
readonly provider: ModelProvider;
|
|
137
|
+
readonly permissions: PermissionHandler;
|
|
138
|
+
readonly registry: ToolRegistry;
|
|
139
|
+
readonly executor: ToolExecutor;
|
|
140
|
+
readonly system?: string;
|
|
141
|
+
readonly model?: string;
|
|
142
|
+
readonly cwd?: string;
|
|
143
|
+
readonly permissionMode?: string | (() => string);
|
|
144
|
+
readonly budget: BudgetPolicy;
|
|
145
|
+
readonly estimator: TokenEstimator;
|
|
146
|
+
readonly compaction: CompactionStrategy;
|
|
147
|
+
readonly recovery: RecoveryPolicy;
|
|
148
|
+
readonly maxSteps: number;
|
|
149
|
+
readonly contextWindowTokens: number;
|
|
150
|
+
readonly toolResultMaxTokens: number;
|
|
151
|
+
readonly keepRecentTurns: number;
|
|
152
|
+
readonly timeoutMs: number;
|
|
153
|
+
readonly store: ContextStore;
|
|
154
|
+
readonly events: EventBus;
|
|
155
|
+
readonly state: SessionState;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface SessionImpl extends SessionInternal {
|
|
159
|
+
sessionAbort: AbortController;
|
|
160
|
+
running: boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function createSession(config: SessionConfig): Session {
|
|
164
|
+
const store = new ContextStore();
|
|
165
|
+
if (config.initialMessages?.length) store.appendAll(config.initialMessages);
|
|
166
|
+
const events = createEventBus({ onHandlerError: config.onHandlerError });
|
|
167
|
+
const sessionAbort = new AbortController();
|
|
168
|
+
const state: SessionState = {
|
|
169
|
+
history: store.messages,
|
|
170
|
+
turnCount: config.turnCount ?? 0,
|
|
171
|
+
stepCount: config.stepCount ?? 0,
|
|
172
|
+
model: config.model,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const impl: SessionImpl = {
|
|
176
|
+
provider: config.provider,
|
|
177
|
+
permissions: config.permissions,
|
|
178
|
+
registry: createToolRegistry(config.tools ?? []),
|
|
179
|
+
executor: config.executor ?? sequentialExecutor(),
|
|
180
|
+
system: config.system,
|
|
181
|
+
model: config.model,
|
|
182
|
+
cwd: config.cwd,
|
|
183
|
+
permissionMode: config.permissionMode,
|
|
184
|
+
budget: config.budget ?? defaultBudgetPolicy,
|
|
185
|
+
estimator: config.estimator ?? defaultTokenEstimator,
|
|
186
|
+
compaction: config.compaction ?? mechanicalCompaction,
|
|
187
|
+
recovery: config.recovery ?? defaultRecoveryPolicy,
|
|
188
|
+
maxSteps: config.maxSteps ?? DEFAULT_MAX_STEPS,
|
|
189
|
+
contextWindowTokens: config.contextWindowTokens ?? DEFAULT_CONTEXT_WINDOW_TOKENS,
|
|
190
|
+
toolResultMaxTokens: config.toolResultMaxTokens ?? DEFAULT_TOOL_RESULT_MAX_TOKENS,
|
|
191
|
+
keepRecentTurns: config.keepRecentTurns ?? DEFAULT_KEEP_RECENT_TURNS,
|
|
192
|
+
timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
193
|
+
store,
|
|
194
|
+
events,
|
|
195
|
+
state,
|
|
196
|
+
sessionAbort,
|
|
197
|
+
running: false,
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
// Public state crosses a host boundary, so it is a defensive snapshot per
|
|
202
|
+
// access (same policy as provider requests, tool ctx.state, and event
|
|
203
|
+
// payloads). The kernel keeps the raw object internally via `impl.state`.
|
|
204
|
+
get state() {
|
|
205
|
+
return snapshotState(state);
|
|
206
|
+
},
|
|
207
|
+
get events() {
|
|
208
|
+
return events;
|
|
209
|
+
},
|
|
210
|
+
// Same source of truth as the loop — estimateSessionContext in
|
|
211
|
+
// tokens.ts — never a duplicate estimator.
|
|
212
|
+
get contextTokens() {
|
|
213
|
+
return estimateSessionContext(store, config.system, impl.registry.list(), impl.estimator);
|
|
214
|
+
},
|
|
215
|
+
async run(input, opts) {
|
|
216
|
+
if (impl.running) throw new AgentError("busy", "session is already running");
|
|
217
|
+
impl.running = true;
|
|
218
|
+
const timeout = createTimeout(impl.timeoutMs);
|
|
219
|
+
const joined = joinSignals(impl.sessionAbort.signal, opts?.signal, timeout.signal);
|
|
220
|
+
const turnStore = new ContextStore();
|
|
221
|
+
turnStore.appendAll(store.messages);
|
|
222
|
+
const turnState: SessionState = {
|
|
223
|
+
history: turnStore.messages,
|
|
224
|
+
turnCount: state.turnCount,
|
|
225
|
+
stepCount: state.stepCount,
|
|
226
|
+
model: opts?.model ?? config.model,
|
|
227
|
+
};
|
|
228
|
+
const turnImpl: SessionInternal = {
|
|
229
|
+
...impl,
|
|
230
|
+
store: turnStore,
|
|
231
|
+
state: turnState,
|
|
232
|
+
};
|
|
233
|
+
try {
|
|
234
|
+
const turn = executeTurn(turnImpl, input, joined.signal, opts?.model);
|
|
235
|
+
// The race guarantees run() rejects as soon as the joined signal
|
|
236
|
+
// aborts (user abort, session.abort, or timeout), even if the
|
|
237
|
+
// in-flight provider/tool ignores the signal. Any late work mutates
|
|
238
|
+
// only turnStore and is discarded.
|
|
239
|
+
const result = await withAbort(turn, joined.signal);
|
|
240
|
+
// A run is transactional with respect to model context: failed,
|
|
241
|
+
// aborted, or timed-out turns are discarded instead of leaving orphaned
|
|
242
|
+
// user messages or assistant tool_calls without matching tool results.
|
|
243
|
+
// Non-cooperative providers/tools may still settle after withTimeout
|
|
244
|
+
// rejects, but they mutate only this private turnStore.
|
|
245
|
+
store.replace(0, store.messages.length, turnStore.messages);
|
|
246
|
+
state.turnCount = turnState.turnCount;
|
|
247
|
+
state.stepCount = turnState.stepCount;
|
|
248
|
+
events.emit({ type: "turn:completed", result: snapshotTurnResult(result) });
|
|
249
|
+
return result;
|
|
250
|
+
} finally {
|
|
251
|
+
timeout.cancel();
|
|
252
|
+
joined.cleanup();
|
|
253
|
+
impl.running = false;
|
|
254
|
+
if (impl.sessionAbort.signal.aborted) impl.sessionAbort = new AbortController();
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
abort() {
|
|
258
|
+
if (impl.running) impl.sessionAbort.abort(new AgentError("aborted", "turn aborted"));
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function joinSignals(...signals: Array<AbortSignal | undefined>): { signal: AbortSignal; cleanup(): void } {
|
|
264
|
+
const active = signals.filter((s): s is AbortSignal => s !== undefined);
|
|
265
|
+
if (active.length <= 1) {
|
|
266
|
+
return active[0] ? { signal: active[0], cleanup: noop } : { signal: new AbortController().signal, cleanup: noop };
|
|
267
|
+
}
|
|
268
|
+
const alreadyAborted = active.find((s) => s.aborted);
|
|
269
|
+
if (alreadyAborted) return { signal: alreadyAborted, cleanup: noop };
|
|
270
|
+
const controller = new AbortController();
|
|
271
|
+
const handlers: Array<[AbortSignal, () => void]> = active.map((s) => {
|
|
272
|
+
const onAbort = () => controller.abort(s.reason);
|
|
273
|
+
s.addEventListener("abort", onAbort, { once: true });
|
|
274
|
+
return [s, onAbort];
|
|
275
|
+
});
|
|
276
|
+
return {
|
|
277
|
+
signal: controller.signal,
|
|
278
|
+
cleanup() {
|
|
279
|
+
for (const [signal, onAbort] of handlers) signal.removeEventListener("abort", onAbort);
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function noop(): void {}
|
|
285
|
+
|
|
286
|
+
interface Timeout {
|
|
287
|
+
readonly signal: AbortSignal;
|
|
288
|
+
cancel(): void;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function createTimeout(ms: number): Timeout {
|
|
292
|
+
const controller = new AbortController();
|
|
293
|
+
const error = new AgentError("timeout", `turn exceeded ${ms}ms`);
|
|
294
|
+
// setTimeout clamps a non-finite delay to 1ms in Node/Bun, so Infinity would
|
|
295
|
+
// abort the turn almost immediately instead of disabling the deadline.
|
|
296
|
+
const timer = Number.isFinite(ms) ? setTimeout(() => controller.abort(error), ms) : undefined;
|
|
297
|
+
return {
|
|
298
|
+
signal: controller.signal,
|
|
299
|
+
cancel: () => {
|
|
300
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function withAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
306
|
+
if (signal.aborted) return Promise.reject(abortError(signal));
|
|
307
|
+
return new Promise((resolve, reject) => {
|
|
308
|
+
const onAbort = () => {
|
|
309
|
+
cleanup();
|
|
310
|
+
reject(abortError(signal));
|
|
311
|
+
};
|
|
312
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
313
|
+
promise.then(
|
|
314
|
+
(value) => {
|
|
315
|
+
cleanup();
|
|
316
|
+
resolve(value);
|
|
317
|
+
},
|
|
318
|
+
(cause) => {
|
|
319
|
+
cleanup();
|
|
320
|
+
reject(cause);
|
|
321
|
+
},
|
|
322
|
+
);
|
|
323
|
+
function cleanup() {
|
|
324
|
+
signal.removeEventListener("abort", onAbort);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|