cerebras-cli 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/AGENTS.md +27 -0
- package/Dockerfile +10 -0
- package/README.md +15 -0
- package/bin/opencode +84 -0
- package/bunfig.toml +4 -0
- package/package.json +128 -0
- package/parsers-config.ts +239 -0
- package/script/build.ts +151 -0
- package/script/postinstall.mjs +122 -0
- package/script/publish.ts +256 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +812 -0
- package/src/acp/session.ts +70 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +310 -0
- package/src/agent/generate.txt +75 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +152 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +142 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +165 -0
- package/src/cli/cmd/auth.ts +369 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +41 -0
- package/src/cli/cmd/debug/lsp.ts +47 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1200 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +400 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +342 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +732 -0
- package/src/cli/cmd/tui/attach.ts +25 -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 +124 -0
- package/src/cli/cmd/tui/component/dialog-feedback.tsx +160 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +223 -0
- package/src/cli/cmd/tui/component/dialog-notification.tsx +78 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +222 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +97 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +114 -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 +37 -0
- package/src/cli/cmd/tui/component/notification-banner.tsx +58 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +530 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +107 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +931 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +12 -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 +111 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +339 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +45 -0
- package/src/cli/cmd/tui/context/sdk.tsx +75 -0
- package/src/cli/cmd/tui/context/sync.tsx +374 -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/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/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 +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -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 +245 -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 +1077 -0
- package/src/cli/cmd/tui/event.ts +39 -0
- package/src/cli/cmd/tui/routes/home.tsx +104 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +93 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +37 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +76 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +183 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1703 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +586 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +120 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +55 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +81 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +36 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +75 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +317 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -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 +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +67 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +55 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +79 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +73 -0
- package/src/config/config.ts +886 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/fzf.ts +124 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +326 -0
- package/src/file/ripgrep.ts +391 -0
- package/src/file/time.ts +38 -0
- package/src/file/watcher.ts +89 -0
- package/src/flag/flag.ts +28 -0
- package/src/format/formatter.ts +277 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +75 -0
- package/src/index.ts +158 -0
- package/src/installation/index.ts +194 -0
- package/src/lsp/client.ts +215 -0
- package/src/lsp/index.ts +370 -0
- package/src/lsp/language.ts +111 -0
- package/src/lsp/server.ts +1327 -0
- package/src/mcp/auth.ts +82 -0
- package/src/mcp/index.ts +576 -0
- package/src/mcp/oauth-callback.ts +203 -0
- package/src/mcp/oauth-provider.ts +132 -0
- package/src/notification/index.ts +101 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +198 -0
- package/src/plugin/index.ts +95 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +68 -0
- package/src/project/project.ts +133 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +77 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +93 -0
- package/src/provider/provider.ts +996 -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 +27 -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 +1713 -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 +406 -0
- package/src/pty/index.ts +226 -0
- package/src/ratelimit/index.ts +185 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +50 -0
- package/src/server/server.ts +2463 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +257 -0
- package/src/session/index.ts +470 -0
- package/src/session/message-v2.ts +641 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +443 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +105 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex.txt +318 -0
- package/src/session/prompt/compaction.txt +12 -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/polaris.txt +107 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt/summarize.txt +4 -0
- package/src/session/prompt/title.txt +36 -0
- package/src/session/prompt.ts +1541 -0
- package/src/session/retry.ts +82 -0
- package/src/session/revert.ts +108 -0
- package/src/session/status.ts +75 -0
- package/src/session/summary.ts +203 -0
- package/src/session/system.ts +148 -0
- package/src/session/todo.ts +36 -0
- package/src/share/share-next.ts +195 -0
- package/src/share/share.ts +87 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/telemetry/index.ts +232 -0
- package/src/tool/bash.ts +365 -0
- package/src/tool/bash.txt +128 -0
- package/src/tool/batch.ts +173 -0
- package/src/tool/batch.txt +28 -0
- package/src/tool/codesearch.ts +138 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +674 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +120 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +217 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +148 -0
- package/src/tool/task.ts +135 -0
- package/src/tool/task.txt +60 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +66 -0
- package/src/tool/webfetch.ts +187 -0
- package/src/tool/webfetch.txt +14 -0
- package/src/tool/websearch.ts +150 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +99 -0
- package/src/tool/write.txt +8 -0
- package/src/types/shims.d.ts +3 -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/eventloop.ts +20 -0
- package/src/util/filesystem.ts +69 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +79 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +177 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/sst-env.d.ts +9 -0
- package/test/bun.test.ts +53 -0
- package/test/config/agent-color.test.ts +66 -0
- package/test/config/config.test.ts +503 -0
- package/test/config/markdown.test.ts +89 -0
- package/test/file/ignore.test.ts +10 -0
- package/test/fixture/fixture.ts +28 -0
- package/test/fixture/lsp/fake-lsp-server.js +77 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/keybind.test.ts +317 -0
- package/test/lsp/client.test.ts +95 -0
- package/test/patch/patch.test.ts +348 -0
- package/test/preload.ts +38 -0
- package/test/project/project.test.ts +42 -0
- package/test/provider/provider.test.ts +1809 -0
- package/test/provider/transform.test.ts +305 -0
- package/test/session/retry.test.ts +61 -0
- package/test/session/session.test.ts +71 -0
- package/test/snapshot/snapshot.test.ts +939 -0
- package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
- package/test/tool/bash.test.ts +55 -0
- package/test/tool/patch.test.ts +259 -0
- package/test/util/iife.test.ts +36 -0
- package/test/util/lazy.test.ts +50 -0
- package/test/util/timeout.test.ts +21 -0
- package/test/util/wildcard.test.ts +55 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { NamedError } from "@opencode-ai/util/error"
|
|
2
|
+
import { MessageV2 } from "./message-v2"
|
|
3
|
+
|
|
4
|
+
export namespace SessionRetry {
|
|
5
|
+
// Hand-tuned backoff schedule (ms): 10s, 10s, 10s, 15s, 15s; after that, stop retrying
|
|
6
|
+
const BACKOFF_SCHEDULE = [10_000, 10_000, 10_000, 15_000, 15_000]
|
|
7
|
+
export const RETRY_MAX_DELAY = 60_000 // absolute cap
|
|
8
|
+
|
|
9
|
+
export async function sleep(ms: number, signal: AbortSignal): Promise<void> {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
const timeout = setTimeout(resolve, ms)
|
|
12
|
+
signal.addEventListener(
|
|
13
|
+
"abort",
|
|
14
|
+
() => {
|
|
15
|
+
clearTimeout(timeout)
|
|
16
|
+
reject(new DOMException("Aborted", "AbortError"))
|
|
17
|
+
},
|
|
18
|
+
{ once: true },
|
|
19
|
+
)
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function delay(attempt: number, error?: MessageV2.APIError) {
|
|
24
|
+
if (attempt > BACKOFF_SCHEDULE.length) {
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
const idx = Math.min(attempt - 1, BACKOFF_SCHEDULE.length - 1)
|
|
28
|
+
const baseDelay = BACKOFF_SCHEDULE[idx]
|
|
29
|
+
|
|
30
|
+
if (error) {
|
|
31
|
+
const headers = error.data.responseHeaders
|
|
32
|
+
if (headers) {
|
|
33
|
+
const retryAfterMs = headers["retry-after-ms"]
|
|
34
|
+
if (retryAfterMs) {
|
|
35
|
+
const parsedMs = Number.parseFloat(retryAfterMs)
|
|
36
|
+
if (!Number.isNaN(parsedMs)) {
|
|
37
|
+
return Math.min(parsedMs, baseDelay, RETRY_MAX_DELAY)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const retryAfter = headers["retry-after"]
|
|
42
|
+
if (retryAfter) {
|
|
43
|
+
const parsedSeconds = Number.parseFloat(retryAfter)
|
|
44
|
+
if (!Number.isNaN(parsedSeconds)) {
|
|
45
|
+
// convert seconds to milliseconds
|
|
46
|
+
return Math.min(Math.ceil(parsedSeconds * 1000), baseDelay, RETRY_MAX_DELAY)
|
|
47
|
+
}
|
|
48
|
+
// Try parsing as HTTP date format
|
|
49
|
+
const parsed = Date.parse(retryAfter) - Date.now()
|
|
50
|
+
if (!Number.isNaN(parsed) && parsed > 0) {
|
|
51
|
+
return Math.min(Math.ceil(parsed), baseDelay, RETRY_MAX_DELAY)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return Math.min(baseDelay, RETRY_MAX_DELAY)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return baseDelay
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function retryable(error: ReturnType<NamedError["toObject"]>) {
|
|
63
|
+
if (MessageV2.APIError.isInstance(error)) {
|
|
64
|
+
if (!error.data.isRetryable) return undefined
|
|
65
|
+
return error.data.message.includes("Overloaded") ? "Provider is overloaded" : error.data.message
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeof error.data?.message === "string") {
|
|
69
|
+
try {
|
|
70
|
+
const json = JSON.parse(error.data.message)
|
|
71
|
+
if (json.type === "error" && json.error?.type === "too_many_requests") {
|
|
72
|
+
return "Too Many Requests"
|
|
73
|
+
}
|
|
74
|
+
if (json.code === "Some resource has been exhausted") {
|
|
75
|
+
return "Provider is overloaded"
|
|
76
|
+
}
|
|
77
|
+
} catch {}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Identifier } from "../id/id"
|
|
3
|
+
import { Snapshot } from "../snapshot"
|
|
4
|
+
import { MessageV2 } from "./message-v2"
|
|
5
|
+
import { Session } from "."
|
|
6
|
+
import { Log } from "../util/log"
|
|
7
|
+
import { splitWhen } from "remeda"
|
|
8
|
+
import { Storage } from "../storage/storage"
|
|
9
|
+
import { Bus } from "../bus"
|
|
10
|
+
import { SessionPrompt } from "./prompt"
|
|
11
|
+
|
|
12
|
+
export namespace SessionRevert {
|
|
13
|
+
const log = Log.create({ service: "session.revert" })
|
|
14
|
+
|
|
15
|
+
export const RevertInput = z.object({
|
|
16
|
+
sessionID: Identifier.schema("session"),
|
|
17
|
+
messageID: Identifier.schema("message"),
|
|
18
|
+
partID: Identifier.schema("part").optional(),
|
|
19
|
+
})
|
|
20
|
+
export type RevertInput = z.infer<typeof RevertInput>
|
|
21
|
+
|
|
22
|
+
export async function revert(input: RevertInput) {
|
|
23
|
+
SessionPrompt.assertNotBusy(input.sessionID)
|
|
24
|
+
const all = await Session.messages({ sessionID: input.sessionID })
|
|
25
|
+
let lastUser: MessageV2.User | undefined
|
|
26
|
+
const session = await Session.get(input.sessionID)
|
|
27
|
+
|
|
28
|
+
let revert: Session.Info["revert"]
|
|
29
|
+
const patches: Snapshot.Patch[] = []
|
|
30
|
+
for (const msg of all) {
|
|
31
|
+
if (msg.info.role === "user") lastUser = msg.info
|
|
32
|
+
const remaining = []
|
|
33
|
+
for (const part of msg.parts) {
|
|
34
|
+
if (revert) {
|
|
35
|
+
if (part.type === "patch") {
|
|
36
|
+
patches.push(part)
|
|
37
|
+
}
|
|
38
|
+
continue
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (!revert) {
|
|
42
|
+
if ((msg.info.id === input.messageID && !input.partID) || part.id === input.partID) {
|
|
43
|
+
// if no useful parts left in message, same as reverting whole message
|
|
44
|
+
const partID = remaining.some((item) => ["text", "tool"].includes(item.type)) ? input.partID : undefined
|
|
45
|
+
revert = {
|
|
46
|
+
messageID: !partID && lastUser ? lastUser.id : msg.info.id,
|
|
47
|
+
partID,
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
remaining.push(part)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (revert) {
|
|
56
|
+
const session = await Session.get(input.sessionID)
|
|
57
|
+
revert.snapshot = session.revert?.snapshot ?? (await Snapshot.track())
|
|
58
|
+
await Snapshot.revert(patches)
|
|
59
|
+
if (revert.snapshot) revert.diff = await Snapshot.diff(revert.snapshot)
|
|
60
|
+
return Session.update(input.sessionID, (draft) => {
|
|
61
|
+
draft.revert = revert
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
return session
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function unrevert(input: { sessionID: string }) {
|
|
68
|
+
log.info("unreverting", input)
|
|
69
|
+
SessionPrompt.assertNotBusy(input.sessionID)
|
|
70
|
+
const session = await Session.get(input.sessionID)
|
|
71
|
+
if (!session.revert) return session
|
|
72
|
+
if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot)
|
|
73
|
+
const next = await Session.update(input.sessionID, (draft) => {
|
|
74
|
+
draft.revert = undefined
|
|
75
|
+
})
|
|
76
|
+
return next
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function cleanup(session: Session.Info) {
|
|
80
|
+
if (!session.revert) return
|
|
81
|
+
const sessionID = session.id
|
|
82
|
+
let msgs = await Session.messages({ sessionID })
|
|
83
|
+
const messageID = session.revert.messageID
|
|
84
|
+
const [preserve, remove] = splitWhen(msgs, (x) => x.info.id === messageID)
|
|
85
|
+
msgs = preserve
|
|
86
|
+
for (const msg of remove) {
|
|
87
|
+
await Storage.remove(["message", sessionID, msg.info.id])
|
|
88
|
+
await Bus.publish(MessageV2.Event.Removed, { sessionID: sessionID, messageID: msg.info.id })
|
|
89
|
+
}
|
|
90
|
+
const last = preserve.at(-1)
|
|
91
|
+
if (session.revert.partID && last) {
|
|
92
|
+
const partID = session.revert.partID
|
|
93
|
+
const [preserveParts, removeParts] = splitWhen(last.parts, (x) => x.id === partID)
|
|
94
|
+
last.parts = preserveParts
|
|
95
|
+
for (const part of removeParts) {
|
|
96
|
+
await Storage.remove(["part", last.info.id, part.id])
|
|
97
|
+
await Bus.publish(MessageV2.Event.PartRemoved, {
|
|
98
|
+
sessionID: sessionID,
|
|
99
|
+
messageID: last.info.id,
|
|
100
|
+
partID: part.id,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
await Session.update(sessionID, (draft) => {
|
|
105
|
+
draft.revert = undefined
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Bus } from "@/bus"
|
|
2
|
+
import { Instance } from "@/project/instance"
|
|
3
|
+
import z from "zod"
|
|
4
|
+
|
|
5
|
+
export namespace SessionStatus {
|
|
6
|
+
export const Info = z
|
|
7
|
+
.union([
|
|
8
|
+
z.object({
|
|
9
|
+
type: z.literal("idle"),
|
|
10
|
+
}),
|
|
11
|
+
z.object({
|
|
12
|
+
type: z.literal("retry"),
|
|
13
|
+
attempt: z.number(),
|
|
14
|
+
message: z.string(),
|
|
15
|
+
next: z.number(),
|
|
16
|
+
}),
|
|
17
|
+
z.object({
|
|
18
|
+
type: z.literal("busy"),
|
|
19
|
+
}),
|
|
20
|
+
])
|
|
21
|
+
.meta({
|
|
22
|
+
ref: "SessionStatus",
|
|
23
|
+
})
|
|
24
|
+
export type Info = z.infer<typeof Info>
|
|
25
|
+
|
|
26
|
+
export const Event = {
|
|
27
|
+
Status: Bus.event(
|
|
28
|
+
"session.status",
|
|
29
|
+
z.object({
|
|
30
|
+
sessionID: z.string(),
|
|
31
|
+
status: Info,
|
|
32
|
+
}),
|
|
33
|
+
),
|
|
34
|
+
// deprecated
|
|
35
|
+
Idle: Bus.event(
|
|
36
|
+
"session.idle",
|
|
37
|
+
z.object({
|
|
38
|
+
sessionID: z.string(),
|
|
39
|
+
}),
|
|
40
|
+
),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const state = Instance.state(() => {
|
|
44
|
+
const data: Record<string, Info> = {}
|
|
45
|
+
return data
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
export function get(sessionID: string) {
|
|
49
|
+
return (
|
|
50
|
+
state()[sessionID] ?? {
|
|
51
|
+
type: "idle",
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function list() {
|
|
57
|
+
return Object.values(state())
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function set(sessionID: string, status: Info) {
|
|
61
|
+
Bus.publish(Event.Status, {
|
|
62
|
+
sessionID,
|
|
63
|
+
status,
|
|
64
|
+
})
|
|
65
|
+
if (status.type === "idle") {
|
|
66
|
+
// deprecated
|
|
67
|
+
Bus.publish(Event.Idle, {
|
|
68
|
+
sessionID,
|
|
69
|
+
})
|
|
70
|
+
delete state()[sessionID]
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
state()[sessionID] = status
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { Provider } from "@/provider/provider"
|
|
2
|
+
import { Config } from "@/config/config"
|
|
3
|
+
import { fn } from "@/util/fn"
|
|
4
|
+
import z from "zod"
|
|
5
|
+
import { Session } from "."
|
|
6
|
+
import { generateText, type ModelMessage } from "ai"
|
|
7
|
+
import { MessageV2 } from "./message-v2"
|
|
8
|
+
import { Identifier } from "@/id/id"
|
|
9
|
+
import { Snapshot } from "@/snapshot"
|
|
10
|
+
import { ProviderTransform } from "@/provider/transform"
|
|
11
|
+
import { SystemPrompt } from "./system"
|
|
12
|
+
import { Log } from "@/util/log"
|
|
13
|
+
import path from "path"
|
|
14
|
+
import { Instance } from "@/project/instance"
|
|
15
|
+
import { Storage } from "@/storage/storage"
|
|
16
|
+
import { Bus } from "@/bus"
|
|
17
|
+
import { mergeDeep, pipe } from "remeda"
|
|
18
|
+
|
|
19
|
+
export namespace SessionSummary {
|
|
20
|
+
const log = Log.create({ service: "session.summary" })
|
|
21
|
+
|
|
22
|
+
export const summarize = fn(
|
|
23
|
+
z.object({
|
|
24
|
+
sessionID: z.string(),
|
|
25
|
+
messageID: z.string(),
|
|
26
|
+
}),
|
|
27
|
+
async (input) => {
|
|
28
|
+
const all = await Session.messages({ sessionID: input.sessionID })
|
|
29
|
+
await Promise.all([
|
|
30
|
+
summarizeSession({ sessionID: input.sessionID, messages: all }),
|
|
31
|
+
summarizeMessage({ messageID: input.messageID, messages: all }),
|
|
32
|
+
])
|
|
33
|
+
},
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
async function summarizeSession(input: { sessionID: string; messages: MessageV2.WithParts[] }) {
|
|
37
|
+
const files = new Set(
|
|
38
|
+
input.messages
|
|
39
|
+
.flatMap((x) => x.parts)
|
|
40
|
+
.filter((x) => x.type === "patch")
|
|
41
|
+
.flatMap((x) => x.files)
|
|
42
|
+
.map((x) => path.relative(Instance.worktree, x)),
|
|
43
|
+
)
|
|
44
|
+
const diffs = await computeDiff({ messages: input.messages }).then((x) =>
|
|
45
|
+
x.filter((x) => {
|
|
46
|
+
return files.has(x.file)
|
|
47
|
+
}),
|
|
48
|
+
)
|
|
49
|
+
await Session.update(input.sessionID, (draft) => {
|
|
50
|
+
draft.summary = {
|
|
51
|
+
additions: diffs.reduce((sum, x) => sum + x.additions, 0),
|
|
52
|
+
deletions: diffs.reduce((sum, x) => sum + x.deletions, 0),
|
|
53
|
+
files: diffs.length,
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
await Storage.write(["session_diff", input.sessionID], diffs)
|
|
57
|
+
Bus.publish(Session.Event.Diff, {
|
|
58
|
+
sessionID: input.sessionID,
|
|
59
|
+
diff: diffs,
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function summarizeMessage(input: { messageID: string; messages: MessageV2.WithParts[] }) {
|
|
64
|
+
const cfg = await Config.get()
|
|
65
|
+
const messages = input.messages.filter(
|
|
66
|
+
(m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
|
|
67
|
+
)
|
|
68
|
+
const msgWithParts = messages.find((m) => m.info.id === input.messageID)!
|
|
69
|
+
const userMsg = msgWithParts.info as MessageV2.User
|
|
70
|
+
const diffs = await computeDiff({ messages })
|
|
71
|
+
userMsg.summary = {
|
|
72
|
+
...userMsg.summary,
|
|
73
|
+
diffs,
|
|
74
|
+
}
|
|
75
|
+
await Session.updateMessage(userMsg)
|
|
76
|
+
|
|
77
|
+
const assistantMsg = messages.find((m) => m.info.role === "assistant")!.info as MessageV2.Assistant
|
|
78
|
+
const small =
|
|
79
|
+
(await Provider.getSmallModel(assistantMsg.providerID)) ??
|
|
80
|
+
(await Provider.getModel(assistantMsg.providerID, assistantMsg.modelID))
|
|
81
|
+
const language = await Provider.getLanguage(small)
|
|
82
|
+
|
|
83
|
+
const options = pipe(
|
|
84
|
+
{},
|
|
85
|
+
mergeDeep(ProviderTransform.options(small, assistantMsg.sessionID)),
|
|
86
|
+
mergeDeep(ProviderTransform.smallOptions(small)),
|
|
87
|
+
mergeDeep(small.options),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
const textPart = msgWithParts.parts.find((p) => p.type === "text" && !p.synthetic) as MessageV2.TextPart
|
|
91
|
+
if (textPart && !userMsg.summary?.title) {
|
|
92
|
+
const result = await generateText({
|
|
93
|
+
maxOutputTokens: small.capabilities.reasoning ? 1500 : 20,
|
|
94
|
+
providerOptions: ProviderTransform.providerOptions(small.api.npm, small.providerID, options),
|
|
95
|
+
messages: [
|
|
96
|
+
...SystemPrompt.title(small.providerID).map(
|
|
97
|
+
(x): ModelMessage => ({
|
|
98
|
+
role: "system",
|
|
99
|
+
content: x,
|
|
100
|
+
}),
|
|
101
|
+
),
|
|
102
|
+
{
|
|
103
|
+
role: "user" as const,
|
|
104
|
+
content: `
|
|
105
|
+
The following is the text to summarize:
|
|
106
|
+
<text>
|
|
107
|
+
${textPart?.text ?? ""}
|
|
108
|
+
</text>
|
|
109
|
+
`,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
headers: small.headers,
|
|
113
|
+
model: language,
|
|
114
|
+
experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
|
|
115
|
+
})
|
|
116
|
+
log.info("title", { title: result.text })
|
|
117
|
+
userMsg.summary.title = result.text
|
|
118
|
+
await Session.updateMessage(userMsg)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (
|
|
122
|
+
messages.some(
|
|
123
|
+
(m) =>
|
|
124
|
+
m.info.role === "assistant" && m.parts.some((p) => p.type === "step-finish" && p.reason !== "tool-calls"),
|
|
125
|
+
)
|
|
126
|
+
) {
|
|
127
|
+
let summary = messages
|
|
128
|
+
.findLast((m) => m.info.role === "assistant")
|
|
129
|
+
?.parts.findLast((p) => p.type === "text")?.text
|
|
130
|
+
if (!summary || diffs.length > 0) {
|
|
131
|
+
for (const msg of messages) {
|
|
132
|
+
for (const part of msg.parts) {
|
|
133
|
+
if (part.type === "tool" && part.state.status === "completed") {
|
|
134
|
+
part.state.output = "[TOOL OUTPUT PRUNED]"
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const result = await generateText({
|
|
139
|
+
model: language,
|
|
140
|
+
maxOutputTokens: 100,
|
|
141
|
+
providerOptions: ProviderTransform.providerOptions(small.api.npm, small.providerID, options),
|
|
142
|
+
messages: [
|
|
143
|
+
...SystemPrompt.summarize(small.providerID).map(
|
|
144
|
+
(x): ModelMessage => ({
|
|
145
|
+
role: "system",
|
|
146
|
+
content: x,
|
|
147
|
+
}),
|
|
148
|
+
),
|
|
149
|
+
...MessageV2.toModelMessage(messages),
|
|
150
|
+
{
|
|
151
|
+
role: "user",
|
|
152
|
+
content: `Summarize the above conversation according to your system prompts.`,
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
headers: small.headers,
|
|
156
|
+
experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
|
|
157
|
+
}).catch(() => {})
|
|
158
|
+
if (result) summary = result.text
|
|
159
|
+
}
|
|
160
|
+
userMsg.summary.body = summary
|
|
161
|
+
log.info("body", { body: summary })
|
|
162
|
+
await Session.updateMessage(userMsg)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const diff = fn(
|
|
167
|
+
z.object({
|
|
168
|
+
sessionID: Identifier.schema("session"),
|
|
169
|
+
messageID: Identifier.schema("message").optional(),
|
|
170
|
+
}),
|
|
171
|
+
async (input) => {
|
|
172
|
+
return Storage.read<Snapshot.FileDiff[]>(["session_diff", input.sessionID]).catch(() => [])
|
|
173
|
+
},
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
async function computeDiff(input: { messages: MessageV2.WithParts[] }) {
|
|
177
|
+
let from: string | undefined
|
|
178
|
+
let to: string | undefined
|
|
179
|
+
|
|
180
|
+
// scan assistant messages to find earliest from and latest to
|
|
181
|
+
// snapshot
|
|
182
|
+
for (const item of input.messages) {
|
|
183
|
+
if (!from) {
|
|
184
|
+
for (const part of item.parts) {
|
|
185
|
+
if (part.type === "step-start" && part.snapshot) {
|
|
186
|
+
from = part.snapshot
|
|
187
|
+
break
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const part of item.parts) {
|
|
193
|
+
if (part.type === "step-finish" && part.snapshot) {
|
|
194
|
+
to = part.snapshot
|
|
195
|
+
break
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (from && to) return Snapshot.diffFull(from, to)
|
|
201
|
+
return []
|
|
202
|
+
}
|
|
203
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Ripgrep } from "../file/ripgrep"
|
|
2
|
+
import { Global } from "../global"
|
|
3
|
+
import { Filesystem } from "../util/filesystem"
|
|
4
|
+
import { Config } from "../config/config"
|
|
5
|
+
|
|
6
|
+
import { Instance } from "../project/instance"
|
|
7
|
+
import path from "path"
|
|
8
|
+
import os from "os"
|
|
9
|
+
|
|
10
|
+
import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"
|
|
11
|
+
import PROMPT_ANTHROPIC_WITHOUT_TODO from "./prompt/qwen.txt"
|
|
12
|
+
import PROMPT_POLARIS from "./prompt/polaris.txt"
|
|
13
|
+
import PROMPT_BEAST from "./prompt/beast.txt"
|
|
14
|
+
import PROMPT_GEMINI from "./prompt/gemini.txt"
|
|
15
|
+
import PROMPT_ANTHROPIC_SPOOF from "./prompt/anthropic_spoof.txt"
|
|
16
|
+
import PROMPT_COMPACTION from "./prompt/compaction.txt"
|
|
17
|
+
import PROMPT_SUMMARIZE from "./prompt/summarize.txt"
|
|
18
|
+
import PROMPT_TITLE from "./prompt/title.txt"
|
|
19
|
+
import PROMPT_CODEX from "./prompt/codex.txt"
|
|
20
|
+
import type { Provider } from "@/provider/provider"
|
|
21
|
+
|
|
22
|
+
export namespace SystemPrompt {
|
|
23
|
+
export function header(providerID: string) {
|
|
24
|
+
if (providerID.includes("anthropic")) return [PROMPT_ANTHROPIC_SPOOF.trim()]
|
|
25
|
+
return []
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function provider(model: Provider.Model) {
|
|
29
|
+
if (model.api.id.includes("gpt-5")) return [PROMPT_CODEX]
|
|
30
|
+
if (model.api.id.includes("gpt-") || model.api.id.includes("o1") || model.api.id.includes("o3"))
|
|
31
|
+
return [PROMPT_BEAST]
|
|
32
|
+
if (model.api.id.includes("gemini-")) return [PROMPT_GEMINI]
|
|
33
|
+
if (model.api.id.includes("claude")) return [PROMPT_ANTHROPIC]
|
|
34
|
+
if (model.api.id.includes("polaris-alpha")) return [PROMPT_POLARIS]
|
|
35
|
+
return [PROMPT_ANTHROPIC_WITHOUT_TODO]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function environment() {
|
|
39
|
+
const project = Instance.project
|
|
40
|
+
return [
|
|
41
|
+
[
|
|
42
|
+
`Here is some useful information about the environment you are running in:`,
|
|
43
|
+
`<env>`,
|
|
44
|
+
` Working directory: ${Instance.directory}`,
|
|
45
|
+
` Is directory a git repo: ${project.vcs === "git" ? "yes" : "no"}`,
|
|
46
|
+
` Platform: ${process.platform}`,
|
|
47
|
+
` Today's date: ${new Date().toDateString()}`,
|
|
48
|
+
`</env>`,
|
|
49
|
+
`<files>`,
|
|
50
|
+
` ${
|
|
51
|
+
project.vcs === "git"
|
|
52
|
+
? await Ripgrep.tree({
|
|
53
|
+
cwd: Instance.directory,
|
|
54
|
+
limit: 200,
|
|
55
|
+
})
|
|
56
|
+
: ""
|
|
57
|
+
}`,
|
|
58
|
+
`</files>`,
|
|
59
|
+
].join("\n"),
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const LOCAL_RULE_FILES = [
|
|
64
|
+
"AGENTS.md",
|
|
65
|
+
"CLAUDE.md",
|
|
66
|
+
"CONTEXT.md", // deprecated
|
|
67
|
+
]
|
|
68
|
+
const GLOBAL_RULE_FILES = [
|
|
69
|
+
path.join(Global.Path.config, "AGENTS.md"),
|
|
70
|
+
path.join(os.homedir(), ".claude", "CLAUDE.md"),
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
export async function custom() {
|
|
74
|
+
const config = await Config.get()
|
|
75
|
+
const paths = new Set<string>()
|
|
76
|
+
|
|
77
|
+
for (const localRuleFile of LOCAL_RULE_FILES) {
|
|
78
|
+
const matches = await Filesystem.findUp(localRuleFile, Instance.directory, Instance.worktree)
|
|
79
|
+
if (matches.length > 0) {
|
|
80
|
+
matches.forEach((path) => paths.add(path))
|
|
81
|
+
break
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (const globalRuleFile of GLOBAL_RULE_FILES) {
|
|
86
|
+
if (await Bun.file(globalRuleFile).exists()) {
|
|
87
|
+
paths.add(globalRuleFile)
|
|
88
|
+
break
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (config.instructions) {
|
|
93
|
+
for (let instruction of config.instructions) {
|
|
94
|
+
if (instruction.startsWith("~/")) {
|
|
95
|
+
instruction = path.join(os.homedir(), instruction.slice(2))
|
|
96
|
+
}
|
|
97
|
+
let matches: string[] = []
|
|
98
|
+
if (path.isAbsolute(instruction)) {
|
|
99
|
+
matches = await Array.fromAsync(
|
|
100
|
+
new Bun.Glob(path.basename(instruction)).scan({
|
|
101
|
+
cwd: path.dirname(instruction),
|
|
102
|
+
absolute: true,
|
|
103
|
+
onlyFiles: true,
|
|
104
|
+
}),
|
|
105
|
+
).catch(() => [])
|
|
106
|
+
} else {
|
|
107
|
+
matches = await Filesystem.globUp(instruction, Instance.directory, Instance.worktree).catch(() => [])
|
|
108
|
+
}
|
|
109
|
+
matches.forEach((path) => paths.add(path))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const found = Array.from(paths).map((p) =>
|
|
114
|
+
Bun.file(p)
|
|
115
|
+
.text()
|
|
116
|
+
.catch(() => "")
|
|
117
|
+
.then((x) => "Instructions from: " + p + "\n" + x),
|
|
118
|
+
)
|
|
119
|
+
return Promise.all(found).then((result) => result.filter(Boolean))
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function compaction(providerID: string) {
|
|
123
|
+
switch (providerID) {
|
|
124
|
+
case "anthropic":
|
|
125
|
+
return [PROMPT_ANTHROPIC_SPOOF.trim(), PROMPT_COMPACTION]
|
|
126
|
+
default:
|
|
127
|
+
return [PROMPT_COMPACTION]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function summarize(providerID: string) {
|
|
132
|
+
switch (providerID) {
|
|
133
|
+
case "anthropic":
|
|
134
|
+
return [PROMPT_ANTHROPIC_SPOOF.trim(), PROMPT_SUMMARIZE]
|
|
135
|
+
default:
|
|
136
|
+
return [PROMPT_SUMMARIZE]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function title(providerID: string) {
|
|
141
|
+
switch (providerID) {
|
|
142
|
+
case "anthropic":
|
|
143
|
+
return [PROMPT_ANTHROPIC_SPOOF.trim(), PROMPT_TITLE]
|
|
144
|
+
default:
|
|
145
|
+
return [PROMPT_TITLE]
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Bus } from "../bus"
|
|
3
|
+
import { Storage } from "../storage/storage"
|
|
4
|
+
|
|
5
|
+
export namespace Todo {
|
|
6
|
+
export const Info = z
|
|
7
|
+
.object({
|
|
8
|
+
content: z.string().describe("Brief description of the task"),
|
|
9
|
+
status: z.string().describe("Current status of the task: pending, in_progress, completed, cancelled"),
|
|
10
|
+
priority: z.string().describe("Priority level of the task: high, medium, low"),
|
|
11
|
+
id: z.string().describe("Unique identifier for the todo item"),
|
|
12
|
+
})
|
|
13
|
+
.meta({ ref: "Todo" })
|
|
14
|
+
export type Info = z.infer<typeof Info>
|
|
15
|
+
|
|
16
|
+
export const Event = {
|
|
17
|
+
Updated: Bus.event(
|
|
18
|
+
"todo.updated",
|
|
19
|
+
z.object({
|
|
20
|
+
sessionID: z.string(),
|
|
21
|
+
todos: z.array(Info),
|
|
22
|
+
}),
|
|
23
|
+
),
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function update(input: { sessionID: string; todos: Info[] }) {
|
|
27
|
+
await Storage.write(["todo", input.sessionID], input.todos)
|
|
28
|
+
Bus.publish(Event.Updated, input)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function get(sessionID: string) {
|
|
32
|
+
return Storage.read<Info[]>(["todo", sessionID])
|
|
33
|
+
.then((x) => x || [])
|
|
34
|
+
.catch(() => [])
|
|
35
|
+
}
|
|
36
|
+
}
|