@bloxystudios/bloxycode 1.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/LICENSE +21 -0
- package/README.md +256 -0
- package/bin/bloxycode +84 -0
- package/package.json +133 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1437 -0
- package/src/acp/session.ts +105 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +356 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/bloxy.txt +46 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +11 -0
- package/src/agent/prompt/title.txt +44 -0
- package/src/auth/index.ts +73 -0
- package/src/bloxy/event.ts +41 -0
- package/src/bloxy/index.ts +5 -0
- package/src/bloxy/parser.ts +263 -0
- package/src/bloxy/prompt.ts +121 -0
- package/src/bloxy/runner.ts +193 -0
- package/src/bloxy/state.ts +246 -0
- package/src/bun/index.ts +134 -0
- package/src/bus/bus-event.ts +43 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +105 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/acp.ts +69 -0
- package/src/cli/cmd/agent.ts +257 -0
- package/src/cli/cmd/auth.ts +400 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/agent.ts +167 -0
- package/src/cli/cmd/debug/config.ts +16 -0
- package/src/cli/cmd/debug/file.ts +97 -0
- package/src/cli/cmd/debug/index.ts +48 -0
- package/src/cli/cmd/debug/lsp.ts +52 -0
- package/src/cli/cmd/debug/ripgrep.ts +87 -0
- package/src/cli/cmd/debug/scrap.ts +16 -0
- package/src/cli/cmd/debug/skill.ts +16 -0
- package/src/cli/cmd/debug/snapshot.ts +52 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1548 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +755 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +395 -0
- package/src/cli/cmd/serve.ts +20 -0
- package/src/cli/cmd/session.ts +135 -0
- package/src/cli/cmd/stats.ts +402 -0
- package/src/cli/cmd/tui/app.tsx +771 -0
- package/src/cli/cmd/tui/attach.ts +39 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +148 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +234 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +256 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +114 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-stash.tsx +87 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +164 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/logo.tsx +102 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +653 -0
- package/src/cli/cmd/tui/component/prompt/frecency.tsx +89 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1138 -0
- package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
- package/src/cli/cmd/tui/component/textarea-keybindings.ts +73 -0
- package/src/cli/cmd/tui/component/tips.tsx +153 -0
- package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +13 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +101 -0
- package/src/cli/cmd/tui/context/kv.tsx +52 -0
- package/src/cli/cmd/tui/context/local.tsx +402 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +46 -0
- package/src/cli/cmd/tui/context/sdk.tsx +94 -0
- package/src/cli/cmd/tui/context/sync.tsx +470 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/bloxycode.json +245 -0
- package/src/cli/cmd/tui/context/theme/carbonfox.json +248 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +242 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +237 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +249 -0
- package/src/cli/cmd/tui/context/theme/osaka-jade.json +93 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1152 -0
- package/src/cli/cmd/tui/event.ts +48 -0
- package/src/cli/cmd/tui/routes/home.tsx +140 -0
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +91 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +142 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +2048 -0
- package/src/cli/cmd/tui/routes/session/permission.tsx +508 -0
- package/src/cli/cmd/tui/routes/session/question.tsx +453 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +313 -0
- package/src/cli/cmd/tui/thread.ts +165 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
- package/src/cli/cmd/tui/ui/dialog-export-options.tsx +204 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +385 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +167 -0
- package/src/cli/cmd/tui/ui/link.tsx +28 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +160 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/signal.ts +7 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/util/transcript.ts +98 -0
- package/src/cli/cmd/tui/worker.ts +152 -0
- package/src/cli/cmd/uninstall.ts +357 -0
- package/src/cli/cmd/upgrade.ts +73 -0
- package/src/cli/cmd/web.ts +81 -0
- package/src/cli/error.ts +57 -0
- package/src/cli/network.ts +53 -0
- package/src/cli/ui.ts +86 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +173 -0
- package/src/command/template/bloxy-resume.txt +15 -0
- package/src/command/template/bloxy-status.txt +25 -0
- package/src/command/template/bloxy-validate.txt +22 -0
- package/src/command/template/bloxy.txt +14 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +99 -0
- package/src/config/config.ts +1367 -0
- package/src/config/markdown.ts +93 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +415 -0
- package/src/file/ripgrep.ts +407 -0
- package/src/file/time.ts +69 -0
- package/src/file/watcher.ts +127 -0
- package/src/flag/flag.ts +79 -0
- package/src/format/formatter.ts +357 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +55 -0
- package/src/id/id.ts +83 -0
- package/src/ide/index.ts +76 -0
- package/src/index.ts +159 -0
- package/src/installation/index.ts +246 -0
- package/src/lsp/client.ts +252 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +119 -0
- package/src/lsp/server.ts +2046 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +934 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +680 -0
- package/src/permission/arity.ts +163 -0
- package/src/permission/index.ts +210 -0
- package/src/permission/next.ts +280 -0
- package/src/plugin/antigravity.ts +378 -0
- package/src/plugin/codex.ts +506 -0
- package/src/plugin/copilot.ts +298 -0
- package/src/plugin/index.ts +136 -0
- package/src/project/bootstrap.ts +35 -0
- package/src/project/instance.ts +91 -0
- package/src/project/project.ts +371 -0
- package/src/project/state.ts +66 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +147 -0
- package/src/provider/models-snapshot.ts +2 -0
- package/src/provider/models.ts +133 -0
- package/src/provider/provider.ts +1241 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1732 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +741 -0
- package/src/pty/index.ts +241 -0
- package/src/question/index.ts +171 -0
- package/src/scheduler/index.ts +61 -0
- package/src/server/error.ts +36 -0
- package/src/server/event.ts +7 -0
- package/src/server/mdns.ts +59 -0
- package/src/server/routes/config.ts +92 -0
- package/src/server/routes/experimental.ts +208 -0
- package/src/server/routes/file.ts +197 -0
- package/src/server/routes/global.ts +135 -0
- package/src/server/routes/mcp.ts +225 -0
- package/src/server/routes/permission.ts +68 -0
- package/src/server/routes/project.ts +82 -0
- package/src/server/routes/provider.ts +165 -0
- package/src/server/routes/pty.ts +169 -0
- package/src/server/routes/question.ts +98 -0
- package/src/server/routes/session.ts +939 -0
- package/src/server/routes/tui.ts +379 -0
- package/src/server/server.ts +604 -0
- package/src/session/compaction.ts +225 -0
- package/src/session/fallback.ts +246 -0
- package/src/session/index.ts +498 -0
- package/src/session/instruction.ts +164 -0
- package/src/session/llm.ts +298 -0
- package/src/session/message-v2.ts +747 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +450 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +105 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex_header.txt +79 -0
- package/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/src/session/prompt/gemini.txt +155 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/src/session/prompt/plan.txt +26 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt.ts +1822 -0
- package/src/session/retry.ts +99 -0
- package/src/session/revert.ts +121 -0
- package/src/session/status.ts +100 -0
- package/src/session/summary.ts +217 -0
- package/src/session/system.ts +52 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +200 -0
- package/src/share/share.ts +92 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +135 -0
- package/src/snapshot/index.ts +236 -0
- package/src/storage/storage.ts +227 -0
- package/src/tool/apply_patch.ts +281 -0
- package/src/tool/apply_patch.txt +33 -0
- package/src/tool/bash.ts +258 -0
- package/src/tool/bash.txt +115 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/bloxy-control.ts +123 -0
- package/src/tool/bloxy-control.txt +13 -0
- package/src/tool/codesearch.ts +132 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +655 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/external-directory.ts +32 -0
- package/src/tool/glob.ts +77 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +154 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +121 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp.ts +96 -0
- package/src/tool/lsp.txt +19 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/plan-enter.txt +14 -0
- package/src/tool/plan-exit.txt +13 -0
- package/src/tool/plan.ts +130 -0
- package/src/tool/question.ts +33 -0
- package/src/tool/question.txt +10 -0
- package/src/tool/read.ts +211 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +161 -0
- package/src/tool/skill.ts +82 -0
- package/src/tool/task.ts +191 -0
- package/src/tool/task.txt +60 -0
- package/src/tool/todo.ts +53 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +89 -0
- package/src/tool/truncation.ts +106 -0
- package/src/tool/webfetch.ts +188 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +150 -0
- package/src/tool/websearch.txt +14 -0
- package/src/tool/write.ts +85 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -0
- package/src/util/binary.ts +41 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/error.ts +54 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +93 -0
- package/src/util/fn.ts +11 -0
- package/src/util/format.ts +20 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +103 -0
- package/src/util/lazy.ts +23 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +180 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +66 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/slug.ts +74 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +56 -0
- package/src/worktree/index.ts +549 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { BusEvent } from "@/bus/bus-event"
|
|
2
|
+
import { Bus } from "@/bus"
|
|
3
|
+
import { Session } from "."
|
|
4
|
+
import { Identifier } from "../id/id"
|
|
5
|
+
import { Instance } from "../project/instance"
|
|
6
|
+
import { Provider } from "../provider/provider"
|
|
7
|
+
import { MessageV2 } from "./message-v2"
|
|
8
|
+
import z from "zod"
|
|
9
|
+
import { SessionPrompt } from "./prompt"
|
|
10
|
+
import { Token } from "../util/token"
|
|
11
|
+
import { Log } from "../util/log"
|
|
12
|
+
import { SessionProcessor } from "./processor"
|
|
13
|
+
import { fn } from "@/util/fn"
|
|
14
|
+
import { Agent } from "@/agent/agent"
|
|
15
|
+
import { Plugin } from "@/plugin"
|
|
16
|
+
import { Config } from "@/config/config"
|
|
17
|
+
|
|
18
|
+
export namespace SessionCompaction {
|
|
19
|
+
const log = Log.create({ service: "session.compaction" })
|
|
20
|
+
|
|
21
|
+
export const Event = {
|
|
22
|
+
Compacted: BusEvent.define(
|
|
23
|
+
"session.compacted",
|
|
24
|
+
z.object({
|
|
25
|
+
sessionID: z.string(),
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function isOverflow(input: { tokens: MessageV2.Assistant["tokens"]; model: Provider.Model }) {
|
|
31
|
+
const config = await Config.get()
|
|
32
|
+
if (config.compaction?.auto === false) return false
|
|
33
|
+
const context = input.model.limit.context
|
|
34
|
+
if (context === 0) return false
|
|
35
|
+
const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
|
|
36
|
+
const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
|
|
37
|
+
const usable = input.model.limit.input || context - output
|
|
38
|
+
return count > usable
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const PRUNE_MINIMUM = 20_000
|
|
42
|
+
export const PRUNE_PROTECT = 40_000
|
|
43
|
+
|
|
44
|
+
const PRUNE_PROTECTED_TOOLS = ["skill"]
|
|
45
|
+
|
|
46
|
+
// goes backwards through parts until there are 40_000 tokens worth of tool
|
|
47
|
+
// calls. then erases output of previous tool calls. idea is to throw away old
|
|
48
|
+
// tool calls that are no longer relevant.
|
|
49
|
+
export async function prune(input: { sessionID: string }) {
|
|
50
|
+
const config = await Config.get()
|
|
51
|
+
if (config.compaction?.prune === false) return
|
|
52
|
+
log.info("pruning")
|
|
53
|
+
const msgs = await Session.messages({ sessionID: input.sessionID })
|
|
54
|
+
let total = 0
|
|
55
|
+
let pruned = 0
|
|
56
|
+
const toPrune = []
|
|
57
|
+
let turns = 0
|
|
58
|
+
|
|
59
|
+
loop: for (let msgIndex = msgs.length - 1; msgIndex >= 0; msgIndex--) {
|
|
60
|
+
const msg = msgs[msgIndex]
|
|
61
|
+
if (msg.info.role === "user") turns++
|
|
62
|
+
if (turns < 2) continue
|
|
63
|
+
if (msg.info.role === "assistant" && msg.info.summary) break loop
|
|
64
|
+
for (let partIndex = msg.parts.length - 1; partIndex >= 0; partIndex--) {
|
|
65
|
+
const part = msg.parts[partIndex]
|
|
66
|
+
if (part.type === "tool")
|
|
67
|
+
if (part.state.status === "completed") {
|
|
68
|
+
if (PRUNE_PROTECTED_TOOLS.includes(part.tool)) continue
|
|
69
|
+
|
|
70
|
+
if (part.state.time.compacted) break loop
|
|
71
|
+
const estimate = Token.estimate(part.state.output)
|
|
72
|
+
total += estimate
|
|
73
|
+
if (total > PRUNE_PROTECT) {
|
|
74
|
+
pruned += estimate
|
|
75
|
+
toPrune.push(part)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
log.info("found", { pruned, total })
|
|
81
|
+
if (pruned > PRUNE_MINIMUM) {
|
|
82
|
+
for (const part of toPrune) {
|
|
83
|
+
if (part.state.status === "completed") {
|
|
84
|
+
part.state.time.compacted = Date.now()
|
|
85
|
+
await Session.updatePart(part)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
log.info("pruned", { count: toPrune.length })
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function process(input: {
|
|
93
|
+
parentID: string
|
|
94
|
+
messages: MessageV2.WithParts[]
|
|
95
|
+
sessionID: string
|
|
96
|
+
abort: AbortSignal
|
|
97
|
+
auto: boolean
|
|
98
|
+
}) {
|
|
99
|
+
const userMessage = input.messages.findLast((m) => m.info.id === input.parentID)!.info as MessageV2.User
|
|
100
|
+
const agent = await Agent.get("compaction")
|
|
101
|
+
const model = agent.model
|
|
102
|
+
? await Provider.getModel(agent.model.providerID, agent.model.modelID)
|
|
103
|
+
: await Provider.getModel(userMessage.model.providerID, userMessage.model.modelID)
|
|
104
|
+
const msg = (await Session.updateMessage({
|
|
105
|
+
id: Identifier.ascending("message"),
|
|
106
|
+
role: "assistant",
|
|
107
|
+
parentID: input.parentID,
|
|
108
|
+
sessionID: input.sessionID,
|
|
109
|
+
mode: "compaction",
|
|
110
|
+
agent: "compaction",
|
|
111
|
+
summary: true,
|
|
112
|
+
path: {
|
|
113
|
+
cwd: Instance.directory,
|
|
114
|
+
root: Instance.worktree,
|
|
115
|
+
},
|
|
116
|
+
cost: 0,
|
|
117
|
+
tokens: {
|
|
118
|
+
output: 0,
|
|
119
|
+
input: 0,
|
|
120
|
+
reasoning: 0,
|
|
121
|
+
cache: { read: 0, write: 0 },
|
|
122
|
+
},
|
|
123
|
+
modelID: model.id,
|
|
124
|
+
providerID: model.providerID,
|
|
125
|
+
time: {
|
|
126
|
+
created: Date.now(),
|
|
127
|
+
},
|
|
128
|
+
})) as MessageV2.Assistant
|
|
129
|
+
const processor = SessionProcessor.create({
|
|
130
|
+
assistantMessage: msg,
|
|
131
|
+
sessionID: input.sessionID,
|
|
132
|
+
model,
|
|
133
|
+
abort: input.abort,
|
|
134
|
+
})
|
|
135
|
+
// Allow plugins to inject context or replace compaction prompt
|
|
136
|
+
const compacting = await Plugin.trigger(
|
|
137
|
+
"experimental.session.compacting",
|
|
138
|
+
{ sessionID: input.sessionID },
|
|
139
|
+
{ context: [], prompt: undefined },
|
|
140
|
+
)
|
|
141
|
+
const defaultPrompt =
|
|
142
|
+
"Provide a detailed prompt for continuing our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next considering new session will not have access to our conversation."
|
|
143
|
+
const promptText = compacting.prompt ?? [defaultPrompt, ...compacting.context].join("\n\n")
|
|
144
|
+
const result = await processor.process({
|
|
145
|
+
user: userMessage,
|
|
146
|
+
agent,
|
|
147
|
+
abort: input.abort,
|
|
148
|
+
sessionID: input.sessionID,
|
|
149
|
+
tools: {},
|
|
150
|
+
system: [],
|
|
151
|
+
messages: [
|
|
152
|
+
...MessageV2.toModelMessages(input.messages, model),
|
|
153
|
+
{
|
|
154
|
+
role: "user",
|
|
155
|
+
content: [
|
|
156
|
+
{
|
|
157
|
+
type: "text",
|
|
158
|
+
text: promptText,
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
model,
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
if (result === "continue" && input.auto) {
|
|
167
|
+
const continueMsg = await Session.updateMessage({
|
|
168
|
+
id: Identifier.ascending("message"),
|
|
169
|
+
role: "user",
|
|
170
|
+
sessionID: input.sessionID,
|
|
171
|
+
time: {
|
|
172
|
+
created: Date.now(),
|
|
173
|
+
},
|
|
174
|
+
agent: userMessage.agent,
|
|
175
|
+
model: userMessage.model,
|
|
176
|
+
})
|
|
177
|
+
await Session.updatePart({
|
|
178
|
+
id: Identifier.ascending("part"),
|
|
179
|
+
messageID: continueMsg.id,
|
|
180
|
+
sessionID: input.sessionID,
|
|
181
|
+
type: "text",
|
|
182
|
+
synthetic: true,
|
|
183
|
+
text: "Continue if you have next steps",
|
|
184
|
+
time: {
|
|
185
|
+
start: Date.now(),
|
|
186
|
+
end: Date.now(),
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
if (processor.message.error) return "stop"
|
|
191
|
+
Bus.publish(Event.Compacted, { sessionID: input.sessionID })
|
|
192
|
+
return "continue"
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export const create = fn(
|
|
196
|
+
z.object({
|
|
197
|
+
sessionID: Identifier.schema("session"),
|
|
198
|
+
agent: z.string(),
|
|
199
|
+
model: z.object({
|
|
200
|
+
providerID: z.string(),
|
|
201
|
+
modelID: z.string(),
|
|
202
|
+
}),
|
|
203
|
+
auto: z.boolean(),
|
|
204
|
+
}),
|
|
205
|
+
async (input) => {
|
|
206
|
+
const msg = await Session.updateMessage({
|
|
207
|
+
id: Identifier.ascending("message"),
|
|
208
|
+
role: "user",
|
|
209
|
+
model: input.model,
|
|
210
|
+
sessionID: input.sessionID,
|
|
211
|
+
agent: input.agent,
|
|
212
|
+
time: {
|
|
213
|
+
created: Date.now(),
|
|
214
|
+
},
|
|
215
|
+
})
|
|
216
|
+
await Session.updatePart({
|
|
217
|
+
id: Identifier.ascending("part"),
|
|
218
|
+
messageID: msg.id,
|
|
219
|
+
sessionID: msg.sessionID,
|
|
220
|
+
type: "compaction",
|
|
221
|
+
auto: input.auto,
|
|
222
|
+
})
|
|
223
|
+
},
|
|
224
|
+
)
|
|
225
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Provider } from "../provider/provider"
|
|
2
|
+
import { Log } from "../util/log"
|
|
3
|
+
|
|
4
|
+
export namespace SessionFallback {
|
|
5
|
+
const log = Log.create({ service: "session.fallback" })
|
|
6
|
+
|
|
7
|
+
export interface RateLimitInfo {
|
|
8
|
+
providerID: string
|
|
9
|
+
resetAt: number // timestamp when limit resets
|
|
10
|
+
reason: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ProviderModel {
|
|
14
|
+
providerID: string
|
|
15
|
+
modelID: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ModelCapabilities {
|
|
19
|
+
toolcall?: boolean
|
|
20
|
+
attachment?: boolean
|
|
21
|
+
reasoning?: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// In-memory state tracking rate-limited providers
|
|
25
|
+
const rateLimited = new Map<string, RateLimitInfo>()
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Parse reset time from various header formats
|
|
29
|
+
* Returns timestamp in milliseconds when the rate limit resets
|
|
30
|
+
*/
|
|
31
|
+
export function parseResetTime(headers?: Record<string, string>): number | null {
|
|
32
|
+
if (!headers) return null
|
|
33
|
+
|
|
34
|
+
// Check x-ratelimit-reset (Unix timestamp in seconds)
|
|
35
|
+
const xRateLimitReset = headers["x-ratelimit-reset"]
|
|
36
|
+
if (xRateLimitReset) {
|
|
37
|
+
const timestamp = Number.parseInt(xRateLimitReset, 10)
|
|
38
|
+
if (!Number.isNaN(timestamp)) {
|
|
39
|
+
// If it's a reasonable Unix timestamp (after year 2020)
|
|
40
|
+
if (timestamp > 1577836800) {
|
|
41
|
+
return timestamp * 1000 // Convert seconds to ms
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Check x-ratelimit-reset-requests (seconds until reset)
|
|
47
|
+
const xRateLimitResetRequests = headers["x-ratelimit-reset-requests"]
|
|
48
|
+
if (xRateLimitResetRequests) {
|
|
49
|
+
// Parse formats like "1s", "2m30s", "1h"
|
|
50
|
+
const match = xRateLimitResetRequests.match(/(?:(\d+)h)?(?:(\d+)m)?(?:(\d+(?:\.\d+)?)s)?/)
|
|
51
|
+
if (match) {
|
|
52
|
+
const hours = Number.parseInt(match[1] || "0", 10)
|
|
53
|
+
const minutes = Number.parseInt(match[2] || "0", 10)
|
|
54
|
+
const seconds = Number.parseFloat(match[3] || "0")
|
|
55
|
+
const totalMs = (hours * 3600 + minutes * 60 + seconds) * 1000
|
|
56
|
+
if (totalMs > 0) {
|
|
57
|
+
return Date.now() + totalMs
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Check retry-after-ms (milliseconds)
|
|
63
|
+
const retryAfterMs = headers["retry-after-ms"]
|
|
64
|
+
if (retryAfterMs) {
|
|
65
|
+
const ms = Number.parseFloat(retryAfterMs)
|
|
66
|
+
if (!Number.isNaN(ms) && ms > 0) {
|
|
67
|
+
return Date.now() + ms
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Check retry-after (seconds or HTTP-date)
|
|
72
|
+
const retryAfter = headers["retry-after"]
|
|
73
|
+
if (retryAfter) {
|
|
74
|
+
// Try as seconds
|
|
75
|
+
const seconds = Number.parseFloat(retryAfter)
|
|
76
|
+
if (!Number.isNaN(seconds) && seconds > 0) {
|
|
77
|
+
return Date.now() + seconds * 1000
|
|
78
|
+
}
|
|
79
|
+
// Try as HTTP-date
|
|
80
|
+
const parsed = Date.parse(retryAfter)
|
|
81
|
+
if (!Number.isNaN(parsed) && parsed > Date.now()) {
|
|
82
|
+
return parsed
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Mark a provider as rate-limited
|
|
91
|
+
*/
|
|
92
|
+
export function markRateLimited(
|
|
93
|
+
providerID: string,
|
|
94
|
+
reason: string,
|
|
95
|
+
headers?: Record<string, string>,
|
|
96
|
+
): void {
|
|
97
|
+
const resetAt = parseResetTime(headers) ?? Date.now() + 60_000 // Default: 1 minute
|
|
98
|
+
|
|
99
|
+
rateLimited.set(providerID, {
|
|
100
|
+
providerID,
|
|
101
|
+
resetAt,
|
|
102
|
+
reason,
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
log.info("Provider marked as rate-limited", {
|
|
106
|
+
providerID,
|
|
107
|
+
reason,
|
|
108
|
+
resetAt: new Date(resetAt).toISOString(),
|
|
109
|
+
waitMs: resetAt - Date.now(),
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Check if a provider is currently rate-limited
|
|
115
|
+
*/
|
|
116
|
+
export function isRateLimited(providerID: string): boolean {
|
|
117
|
+
const info = rateLimited.get(providerID)
|
|
118
|
+
if (!info) return false
|
|
119
|
+
|
|
120
|
+
// Check if rate limit has expired
|
|
121
|
+
if (Date.now() >= info.resetAt) {
|
|
122
|
+
rateLimited.delete(providerID)
|
|
123
|
+
log.info("Rate limit expired", { providerID })
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return true
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Clear rate limit status for a provider
|
|
132
|
+
*/
|
|
133
|
+
export function clearRateLimit(providerID: string): void {
|
|
134
|
+
if (rateLimited.delete(providerID)) {
|
|
135
|
+
log.info("Cleared rate limit", { providerID })
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Clear all expired rate limits
|
|
141
|
+
*/
|
|
142
|
+
export function clearExpiredLimits(): void {
|
|
143
|
+
const now = Date.now()
|
|
144
|
+
for (const [providerID, info] of rateLimited) {
|
|
145
|
+
if (now >= info.resetAt) {
|
|
146
|
+
rateLimited.delete(providerID)
|
|
147
|
+
log.info("Cleared expired rate limit", { providerID })
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Get all currently rate-limited providers
|
|
154
|
+
*/
|
|
155
|
+
export function getRateLimitedProviders(): RateLimitInfo[] {
|
|
156
|
+
clearExpiredLimits()
|
|
157
|
+
return Array.from(rateLimited.values())
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Get earliest reset time across all rate-limited providers
|
|
162
|
+
* Returns null if no providers are rate-limited
|
|
163
|
+
*/
|
|
164
|
+
export function getEarliestResetTime(): number | null {
|
|
165
|
+
clearExpiredLimits()
|
|
166
|
+
|
|
167
|
+
if (rateLimited.size === 0) return null
|
|
168
|
+
|
|
169
|
+
let earliest = Infinity
|
|
170
|
+
for (const info of rateLimited.values()) {
|
|
171
|
+
if (info.resetAt < earliest) {
|
|
172
|
+
earliest = info.resetAt
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return earliest === Infinity ? null : earliest
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Find the next available provider/model that isn't rate-limited
|
|
181
|
+
* and has the required capabilities
|
|
182
|
+
*/
|
|
183
|
+
export async function getNextAvailable(
|
|
184
|
+
currentProviderID: string,
|
|
185
|
+
capabilities?: ModelCapabilities,
|
|
186
|
+
): Promise<ProviderModel | null> {
|
|
187
|
+
clearExpiredLimits()
|
|
188
|
+
|
|
189
|
+
const providers = await Provider.list()
|
|
190
|
+
|
|
191
|
+
for (const [providerID, provider] of Object.entries(providers)) {
|
|
192
|
+
// Skip current provider
|
|
193
|
+
if (providerID === currentProviderID) continue
|
|
194
|
+
|
|
195
|
+
// Skip rate-limited providers
|
|
196
|
+
if (isRateLimited(providerID)) continue
|
|
197
|
+
|
|
198
|
+
// Find a model with required capabilities
|
|
199
|
+
for (const [modelID, model] of Object.entries(provider.models)) {
|
|
200
|
+
// Check capabilities if specified
|
|
201
|
+
if (capabilities) {
|
|
202
|
+
if (capabilities.toolcall && !model.capabilities.toolcall) continue
|
|
203
|
+
if (capabilities.attachment && !model.capabilities.attachment) continue
|
|
204
|
+
if (capabilities.reasoning && !model.capabilities.reasoning) continue
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
log.info("Found alternative provider", {
|
|
208
|
+
from: currentProviderID,
|
|
209
|
+
to: providerID,
|
|
210
|
+
model: modelID,
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
return { providerID, modelID }
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
log.info("No alternative providers available", {
|
|
218
|
+
currentProviderID,
|
|
219
|
+
rateLimited: Array.from(rateLimited.keys()),
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
return null
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Check if all configured providers are rate-limited
|
|
227
|
+
*/
|
|
228
|
+
export async function allProvidersRateLimited(): Promise<boolean> {
|
|
229
|
+
clearExpiredLimits()
|
|
230
|
+
|
|
231
|
+
const providers = await Provider.list()
|
|
232
|
+
const providerIDs = Object.keys(providers)
|
|
233
|
+
|
|
234
|
+
if (providerIDs.length === 0) return false
|
|
235
|
+
|
|
236
|
+
return providerIDs.every((id) => isRateLimited(id))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Reset all rate limit tracking (useful for new sessions)
|
|
241
|
+
*/
|
|
242
|
+
export function reset(): void {
|
|
243
|
+
rateLimited.clear()
|
|
244
|
+
log.info("Reset all rate limit tracking")
|
|
245
|
+
}
|
|
246
|
+
}
|