rird 1.0.200
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 +18 -0
- package/README.md +15 -0
- package/bin/opencode +336 -0
- package/bin/pty-wrapper.js +285 -0
- package/bunfig.toml +4 -0
- package/facebook_ads_library.png +0 -0
- package/nul`nif +0 -0
- package/package.json +111 -0
- package/parsers-config.ts +239 -0
- package/rird-1.0.199.tgz +0 -0
- package/script/build-windows.ts +54 -0
- package/script/build.ts +167 -0
- package/script/postinstall.mjs +544 -0
- package/script/publish-registries.ts +187 -0
- package/script/publish.ts +72 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +1063 -0
- package/src/acp/session.ts +101 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +367 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +12 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +10 -0
- package/src/agent/prompt/title.txt +36 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +114 -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 +88 -0
- package/src/cli/cmd/agent.ts +256 -0
- package/src/cli/cmd/auth.ts +391 -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 +43 -0
- package/src/cli/cmd/debug/lsp.ts +48 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.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 +1400 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +654 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +368 -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 +696 -0
- package/src/cli/cmd/tui/attach.ts +30 -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-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +245 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +224 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +162 -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/did-you-know.tsx +85 -0
- package/src/cli/cmd/tui/component/logo.tsx +35 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +1090 -0
- package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
- package/src/cli/cmd/tui/component/tips.ts +27 -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 +49 -0
- package/src/cli/cmd/tui/context/local.tsx +354 -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 +74 -0
- package/src/cli/cmd/tui/context/sync.tsx +372 -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-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 +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -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/rird.json +245 -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 +1109 -0
- package/src/cli/cmd/tui/event.ts +40 -0
- package/src/cli/cmd/tui/routes/home.tsx +138 -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 +88 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1864 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +318 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +142 -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-help.tsx +38 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +332 -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 +100 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +56 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +80 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +97 -0
- package/src/config/config.ts +995 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +328 -0
- package/src/file/ripgrep.ts +393 -0
- package/src/file/time.ts +64 -0
- package/src/file/watcher.ts +103 -0
- package/src/flag/flag.ts +46 -0
- package/src/format/formatter.ts +315 -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 +76 -0
- package/src/index.ts +240 -0
- package/src/installation/index.ts +239 -0
- package/src/lsp/client.ts +229 -0
- package/src/lsp/index.ts +485 -0
- package/src/lsp/language.ts +116 -0
- package/src/lsp/server.ts +1895 -0
- package/src/mcp/auth.ts +135 -0
- package/src/mcp/index.ts +690 -0
- package/src/mcp/oauth-callback.ts +200 -0
- package/src/mcp/oauth-provider.ts +154 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +199 -0
- package/src/plugin/index.ts +91 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +78 -0
- package/src/project/project.ts +221 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +76 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +106 -0
- package/src/provider/provider.ts +1071 -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 +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 +455 -0
- package/src/pty/index.ts +231 -0
- package/src/security/guardrails.test.ts +341 -0
- package/src/security/guardrails.ts +558 -0
- package/src/security/index.ts +19 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +79 -0
- package/src/server/server.ts +2642 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +223 -0
- package/src/session/index.ts +461 -0
- package/src/session/llm.ts +201 -0
- package/src/session/message-v2.ts +690 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +409 -0
- package/src/session/prompt/act-switch.txt +5 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +85 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +103 -0
- package/src/session/prompt/codex.txt +304 -0
- package/src/session/prompt/copilot-gpt-5.txt +138 -0
- package/src/session/prompt/gemini.txt +85 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
- package/src/session/prompt/plan.txt +24 -0
- package/src/session/prompt/polaris.txt +84 -0
- package/src/session/prompt/qwen.txt +106 -0
- package/src/session/prompt.ts +1509 -0
- package/src/session/retry.ts +86 -0
- package/src/session/revert.ts +108 -0
- package/src/session/sensitive-filter.test.ts +327 -0
- package/src/session/sensitive-filter.ts +466 -0
- package/src/session/status.ts +76 -0
- package/src/session/summary.ts +194 -0
- package/src/session/system.ts +120 -0
- package/src/session/todo.ts +37 -0
- package/src/share/share-next.ts +194 -0
- package/src/share/share.ts +87 -0
- package/src/shell/shell.ts +67 -0
- package/src/skill/index.ts +1 -0
- package/src/skill/skill.ts +83 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/tests/agent.test.ts +308 -0
- package/src/tests/build-guards.test.ts +267 -0
- package/src/tests/config.test.ts +664 -0
- package/src/tests/tool-registry.test.ts +589 -0
- package/src/tool/bash.ts +317 -0
- package/src/tool/bash.txt +158 -0
- package/src/tool/batch.ts +175 -0
- package/src/tool/batch.txt +24 -0
- package/src/tool/codesearch.ts +168 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +675 -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 +121 -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/lsp.ts +87 -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/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +219 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +162 -0
- package/src/tool/skill.ts +100 -0
- package/src/tool/task.ts +136 -0
- package/src/tool/task.txt +51 -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 +71 -0
- package/src/tool/webfetch.ts +198 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +180 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +110 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +16 -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 +83 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +102 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/license.ts +325 -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 +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/agent/agent.test.ts +146 -0
- package/test/bun.test.ts +53 -0
- package/test/cli/github-remote.test.ts +80 -0
- package/test/config/agent-color.test.ts +66 -0
- package/test/config/config.test.ts +535 -0
- package/test/config/markdown.test.ts +89 -0
- package/test/file/ignore.test.ts +10 -0
- package/test/fixture/fixture.ts +36 -0
- package/test/fixture/lsp/fake-lsp-server.js +77 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/keybind.test.ts +421 -0
- package/test/lsp/client.test.ts +95 -0
- package/test/mcp/headers.test.ts +153 -0
- package/test/patch/patch.test.ts +348 -0
- package/test/preload.ts +57 -0
- package/test/project/project.test.ts +72 -0
- package/test/provider/provider.test.ts +1809 -0
- package/test/provider/transform.test.ts +411 -0
- package/test/session/retry.test.ts +111 -0
- package/test/session/session.test.ts +71 -0
- package/test/skill/skill.test.ts +131 -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 +434 -0
- package/test/tool/grep.test.ts +108 -0
- package/test/tool/patch.test.ts +259 -0
- package/test/tool/read.test.ts +42 -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 +16 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { BusEvent } from "@/bus/bus-event"
|
|
2
|
+
import { Bus } from "@/bus"
|
|
3
|
+
import { Decimal } from "decimal.js"
|
|
4
|
+
import z from "zod"
|
|
5
|
+
import { type LanguageModelUsage, type ProviderMetadata } from "ai"
|
|
6
|
+
import { Config } from "../config/config"
|
|
7
|
+
import { Flag } from "../flag/flag"
|
|
8
|
+
import { Identifier } from "../id/id"
|
|
9
|
+
import { Installation } from "../installation"
|
|
10
|
+
|
|
11
|
+
import { Storage } from "../storage/storage"
|
|
12
|
+
import { Log } from "../util/log"
|
|
13
|
+
import { MessageV2 } from "./message-v2"
|
|
14
|
+
import { Instance } from "../project/instance"
|
|
15
|
+
import { SessionPrompt } from "./prompt"
|
|
16
|
+
import { fn } from "@/util/fn"
|
|
17
|
+
import { Command } from "../command"
|
|
18
|
+
import { Snapshot } from "@/snapshot"
|
|
19
|
+
import { SensitiveFilter } from "./sensitive-filter"
|
|
20
|
+
|
|
21
|
+
import type { Provider } from "@/provider/provider"
|
|
22
|
+
|
|
23
|
+
export namespace Session {
|
|
24
|
+
const log = Log.create({ service: "session" })
|
|
25
|
+
|
|
26
|
+
const parentTitlePrefix = "New session - "
|
|
27
|
+
const childTitlePrefix = "Child session - "
|
|
28
|
+
|
|
29
|
+
function createDefaultTitle(isChild = false) {
|
|
30
|
+
return (isChild ? childTitlePrefix : parentTitlePrefix) + new Date().toISOString()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isDefaultTitle(title: string) {
|
|
34
|
+
return new RegExp(
|
|
35
|
+
`^(${parentTitlePrefix}|${childTitlePrefix})\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$`,
|
|
36
|
+
).test(title)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const Info = z
|
|
40
|
+
.object({
|
|
41
|
+
id: Identifier.schema("session"),
|
|
42
|
+
projectID: z.string(),
|
|
43
|
+
directory: z.string(),
|
|
44
|
+
parentID: Identifier.schema("session").optional(),
|
|
45
|
+
summary: z
|
|
46
|
+
.object({
|
|
47
|
+
additions: z.number(),
|
|
48
|
+
deletions: z.number(),
|
|
49
|
+
files: z.number(),
|
|
50
|
+
diffs: Snapshot.FileDiff.array().optional(),
|
|
51
|
+
})
|
|
52
|
+
.optional(),
|
|
53
|
+
share: z
|
|
54
|
+
.object({
|
|
55
|
+
url: z.string(),
|
|
56
|
+
})
|
|
57
|
+
.optional(),
|
|
58
|
+
title: z.string(),
|
|
59
|
+
version: z.string(),
|
|
60
|
+
time: z.object({
|
|
61
|
+
created: z.number(),
|
|
62
|
+
updated: z.number(),
|
|
63
|
+
compacting: z.number().optional(),
|
|
64
|
+
archived: z.number().optional(),
|
|
65
|
+
}),
|
|
66
|
+
revert: z
|
|
67
|
+
.object({
|
|
68
|
+
messageID: z.string(),
|
|
69
|
+
partID: z.string().optional(),
|
|
70
|
+
snapshot: z.string().optional(),
|
|
71
|
+
diff: z.string().optional(),
|
|
72
|
+
})
|
|
73
|
+
.optional(),
|
|
74
|
+
})
|
|
75
|
+
.meta({
|
|
76
|
+
ref: "Session",
|
|
77
|
+
})
|
|
78
|
+
export type Info = z.output<typeof Info>
|
|
79
|
+
|
|
80
|
+
export const ShareInfo = z
|
|
81
|
+
.object({
|
|
82
|
+
secret: z.string(),
|
|
83
|
+
url: z.string(),
|
|
84
|
+
})
|
|
85
|
+
.meta({
|
|
86
|
+
ref: "SessionShare",
|
|
87
|
+
})
|
|
88
|
+
export type ShareInfo = z.output<typeof ShareInfo>
|
|
89
|
+
|
|
90
|
+
export const Event = {
|
|
91
|
+
Created: BusEvent.define(
|
|
92
|
+
"session.created",
|
|
93
|
+
z.object({
|
|
94
|
+
info: Info,
|
|
95
|
+
}),
|
|
96
|
+
),
|
|
97
|
+
Updated: BusEvent.define(
|
|
98
|
+
"session.updated",
|
|
99
|
+
z.object({
|
|
100
|
+
info: Info,
|
|
101
|
+
}),
|
|
102
|
+
),
|
|
103
|
+
Deleted: BusEvent.define(
|
|
104
|
+
"session.deleted",
|
|
105
|
+
z.object({
|
|
106
|
+
info: Info,
|
|
107
|
+
}),
|
|
108
|
+
),
|
|
109
|
+
Diff: BusEvent.define(
|
|
110
|
+
"session.diff",
|
|
111
|
+
z.object({
|
|
112
|
+
sessionID: z.string(),
|
|
113
|
+
diff: Snapshot.FileDiff.array(),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
Error: BusEvent.define(
|
|
117
|
+
"session.error",
|
|
118
|
+
z.object({
|
|
119
|
+
sessionID: z.string().optional(),
|
|
120
|
+
error: MessageV2.Assistant.shape.error,
|
|
121
|
+
}),
|
|
122
|
+
),
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const create = fn(
|
|
126
|
+
z
|
|
127
|
+
.object({
|
|
128
|
+
parentID: Identifier.schema("session").optional(),
|
|
129
|
+
title: z.string().optional(),
|
|
130
|
+
})
|
|
131
|
+
.optional(),
|
|
132
|
+
async (input) => {
|
|
133
|
+
return createNext({
|
|
134
|
+
parentID: input?.parentID,
|
|
135
|
+
directory: Instance.directory,
|
|
136
|
+
title: input?.title,
|
|
137
|
+
})
|
|
138
|
+
},
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
export const fork = fn(
|
|
142
|
+
z.object({
|
|
143
|
+
sessionID: Identifier.schema("session"),
|
|
144
|
+
messageID: Identifier.schema("message").optional(),
|
|
145
|
+
}),
|
|
146
|
+
async (input) => {
|
|
147
|
+
const session = await createNext({
|
|
148
|
+
directory: Instance.directory,
|
|
149
|
+
})
|
|
150
|
+
const msgs = await messages({ sessionID: input.sessionID })
|
|
151
|
+
for (const msg of msgs) {
|
|
152
|
+
if (input.messageID && msg.info.id >= input.messageID) break
|
|
153
|
+
const cloned = await updateMessage({
|
|
154
|
+
...msg.info,
|
|
155
|
+
sessionID: session.id,
|
|
156
|
+
id: Identifier.ascending("message"),
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
for (const part of msg.parts) {
|
|
160
|
+
await updatePart({
|
|
161
|
+
...part,
|
|
162
|
+
id: Identifier.ascending("part"),
|
|
163
|
+
messageID: cloned.id,
|
|
164
|
+
sessionID: session.id,
|
|
165
|
+
})
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return session
|
|
169
|
+
},
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
export const touch = fn(Identifier.schema("session"), async (sessionID) => {
|
|
173
|
+
await update(sessionID, (draft) => {
|
|
174
|
+
draft.time.updated = Date.now()
|
|
175
|
+
})
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
export async function createNext(input: { id?: string; title?: string; parentID?: string; directory: string }) {
|
|
179
|
+
const result: Info = {
|
|
180
|
+
id: Identifier.descending("session", input.id),
|
|
181
|
+
version: Installation.VERSION,
|
|
182
|
+
projectID: Instance.project.id,
|
|
183
|
+
directory: input.directory,
|
|
184
|
+
parentID: input.parentID,
|
|
185
|
+
title: input.title ?? createDefaultTitle(!!input.parentID),
|
|
186
|
+
time: {
|
|
187
|
+
created: Date.now(),
|
|
188
|
+
updated: Date.now(),
|
|
189
|
+
},
|
|
190
|
+
}
|
|
191
|
+
log.info("created", result)
|
|
192
|
+
await Storage.write(["session", Instance.project.id, result.id], result)
|
|
193
|
+
Bus.publish(Event.Created, {
|
|
194
|
+
info: result,
|
|
195
|
+
})
|
|
196
|
+
const cfg = await Config.get()
|
|
197
|
+
if (!result.parentID && (Flag.RIRD_AUTO_SHARE || cfg.share === "auto"))
|
|
198
|
+
share(result.id)
|
|
199
|
+
.then((share) => {
|
|
200
|
+
update(result.id, (draft) => {
|
|
201
|
+
draft.share = share
|
|
202
|
+
})
|
|
203
|
+
})
|
|
204
|
+
.catch(() => {
|
|
205
|
+
// Silently ignore sharing errors during session creation
|
|
206
|
+
})
|
|
207
|
+
Bus.publish(Event.Updated, {
|
|
208
|
+
info: result,
|
|
209
|
+
})
|
|
210
|
+
return result
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export const get = fn(Identifier.schema("session"), async (id) => {
|
|
214
|
+
const read = await Storage.read<Info>(["session", Instance.project.id, id])
|
|
215
|
+
return read as Info
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
export const getShare = fn(Identifier.schema("session"), async (id) => {
|
|
219
|
+
return Storage.read<ShareInfo>(["share", id])
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
export const share = fn(Identifier.schema("session"), async (id) => {
|
|
223
|
+
const cfg = await Config.get()
|
|
224
|
+
if (cfg.share === "disabled") {
|
|
225
|
+
throw new Error("Sharing is disabled in configuration")
|
|
226
|
+
}
|
|
227
|
+
const { ShareNext } = await import("@/share/share-next")
|
|
228
|
+
const share = await ShareNext.create(id)
|
|
229
|
+
await update(id, (draft) => {
|
|
230
|
+
draft.share = {
|
|
231
|
+
url: share.url,
|
|
232
|
+
}
|
|
233
|
+
})
|
|
234
|
+
return share
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
export const unshare = fn(Identifier.schema("session"), async (id) => {
|
|
238
|
+
// Use ShareNext to remove the share (same as share function uses ShareNext to create)
|
|
239
|
+
const { ShareNext } = await import("@/share/share-next")
|
|
240
|
+
await ShareNext.remove(id)
|
|
241
|
+
await update(id, (draft) => {
|
|
242
|
+
draft.share = undefined
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
export async function update(id: string, editor: (session: Info) => void) {
|
|
247
|
+
const project = Instance.project
|
|
248
|
+
const result = await Storage.update<Info>(["session", project.id, id], (draft) => {
|
|
249
|
+
editor(draft)
|
|
250
|
+
draft.time.updated = Date.now()
|
|
251
|
+
})
|
|
252
|
+
Bus.publish(Event.Updated, {
|
|
253
|
+
info: result,
|
|
254
|
+
})
|
|
255
|
+
return result
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export const diff = fn(Identifier.schema("session"), async (sessionID) => {
|
|
259
|
+
const diffs = await Storage.read<Snapshot.FileDiff[]>(["session_diff", sessionID])
|
|
260
|
+
return diffs ?? []
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
export const messages = fn(
|
|
264
|
+
z.object({
|
|
265
|
+
sessionID: Identifier.schema("session"),
|
|
266
|
+
limit: z.number().optional(),
|
|
267
|
+
}),
|
|
268
|
+
async (input) => {
|
|
269
|
+
const result = [] as MessageV2.WithParts[]
|
|
270
|
+
for await (const msg of MessageV2.stream(input.sessionID)) {
|
|
271
|
+
if (input.limit && result.length >= input.limit) break
|
|
272
|
+
result.push(msg)
|
|
273
|
+
}
|
|
274
|
+
result.reverse()
|
|
275
|
+
return result
|
|
276
|
+
},
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
export async function* list() {
|
|
280
|
+
const project = Instance.project
|
|
281
|
+
for (const item of await Storage.list(["session", project.id])) {
|
|
282
|
+
yield Storage.read<Info>(item)
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export const children = fn(Identifier.schema("session"), async (parentID) => {
|
|
287
|
+
const project = Instance.project
|
|
288
|
+
const result = [] as Session.Info[]
|
|
289
|
+
for (const item of await Storage.list(["session", project.id])) {
|
|
290
|
+
const session = await Storage.read<Info>(item)
|
|
291
|
+
if (session.parentID !== parentID) continue
|
|
292
|
+
result.push(session)
|
|
293
|
+
}
|
|
294
|
+
return result
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
export const remove = fn(Identifier.schema("session"), async (sessionID) => {
|
|
298
|
+
const project = Instance.project
|
|
299
|
+
try {
|
|
300
|
+
const session = await get(sessionID)
|
|
301
|
+
for (const child of await children(sessionID)) {
|
|
302
|
+
await remove(child.id)
|
|
303
|
+
}
|
|
304
|
+
await unshare(sessionID).catch(() => {})
|
|
305
|
+
for (const msg of await Storage.list(["message", sessionID])) {
|
|
306
|
+
for (const part of await Storage.list(["part", msg.at(-1)!])) {
|
|
307
|
+
await Storage.remove(part)
|
|
308
|
+
}
|
|
309
|
+
await Storage.remove(msg)
|
|
310
|
+
}
|
|
311
|
+
await Storage.remove(["session", project.id, sessionID])
|
|
312
|
+
// Clean up sensitive data mappings for this session
|
|
313
|
+
SensitiveFilter.clearSession(sessionID)
|
|
314
|
+
Bus.publish(Event.Deleted, {
|
|
315
|
+
info: session,
|
|
316
|
+
})
|
|
317
|
+
} catch (e) {
|
|
318
|
+
log.error(e)
|
|
319
|
+
}
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
export const updateMessage = fn(MessageV2.Info, async (msg) => {
|
|
323
|
+
await Storage.write(["message", msg.sessionID, msg.id], msg)
|
|
324
|
+
Bus.publish(MessageV2.Event.Updated, {
|
|
325
|
+
info: msg,
|
|
326
|
+
})
|
|
327
|
+
return msg
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
export const removeMessage = fn(
|
|
331
|
+
z.object({
|
|
332
|
+
sessionID: Identifier.schema("session"),
|
|
333
|
+
messageID: Identifier.schema("message"),
|
|
334
|
+
}),
|
|
335
|
+
async (input) => {
|
|
336
|
+
await Storage.remove(["message", input.sessionID, input.messageID])
|
|
337
|
+
Bus.publish(MessageV2.Event.Removed, {
|
|
338
|
+
sessionID: input.sessionID,
|
|
339
|
+
messageID: input.messageID,
|
|
340
|
+
})
|
|
341
|
+
return input.messageID
|
|
342
|
+
},
|
|
343
|
+
)
|
|
344
|
+
|
|
345
|
+
export const removePart = fn(
|
|
346
|
+
z.object({
|
|
347
|
+
sessionID: Identifier.schema("session"),
|
|
348
|
+
messageID: Identifier.schema("message"),
|
|
349
|
+
partID: Identifier.schema("part"),
|
|
350
|
+
}),
|
|
351
|
+
async (input) => {
|
|
352
|
+
await Storage.remove(["part", input.messageID, input.partID])
|
|
353
|
+
Bus.publish(MessageV2.Event.PartRemoved, {
|
|
354
|
+
sessionID: input.sessionID,
|
|
355
|
+
messageID: input.messageID,
|
|
356
|
+
partID: input.partID,
|
|
357
|
+
})
|
|
358
|
+
return input.partID
|
|
359
|
+
},
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
const UpdatePartInput = z.union([
|
|
363
|
+
MessageV2.Part,
|
|
364
|
+
z.object({
|
|
365
|
+
part: MessageV2.TextPart,
|
|
366
|
+
delta: z.string(),
|
|
367
|
+
}),
|
|
368
|
+
z.object({
|
|
369
|
+
part: MessageV2.ReasoningPart,
|
|
370
|
+
delta: z.string(),
|
|
371
|
+
}),
|
|
372
|
+
])
|
|
373
|
+
|
|
374
|
+
export const updatePart = fn(UpdatePartInput, async (input) => {
|
|
375
|
+
const part = "delta" in input ? input.part : input
|
|
376
|
+
const delta = "delta" in input ? input.delta : undefined
|
|
377
|
+
await Storage.write(["part", part.messageID, part.id], part)
|
|
378
|
+
Bus.publish(MessageV2.Event.PartUpdated, {
|
|
379
|
+
part,
|
|
380
|
+
delta,
|
|
381
|
+
})
|
|
382
|
+
return part
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
export const getUsage = fn(
|
|
386
|
+
z.object({
|
|
387
|
+
model: z.custom<Provider.Model>(),
|
|
388
|
+
usage: z.custom<LanguageModelUsage>(),
|
|
389
|
+
metadata: z.custom<ProviderMetadata>().optional(),
|
|
390
|
+
}),
|
|
391
|
+
(input) => {
|
|
392
|
+
const cachedInputTokens = input.usage.cachedInputTokens ?? 0
|
|
393
|
+
const excludesCachedTokens = !!(input.metadata?.["anthropic"] || input.metadata?.["bedrock"])
|
|
394
|
+
const adjustedInputTokens = excludesCachedTokens
|
|
395
|
+
? (input.usage.inputTokens ?? 0)
|
|
396
|
+
: (input.usage.inputTokens ?? 0) - cachedInputTokens
|
|
397
|
+
const safe = (value: number) => {
|
|
398
|
+
if (!Number.isFinite(value)) return 0
|
|
399
|
+
return value
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const tokens = {
|
|
403
|
+
input: safe(adjustedInputTokens),
|
|
404
|
+
output: safe(input.usage.outputTokens ?? 0),
|
|
405
|
+
reasoning: safe(input.usage?.reasoningTokens ?? 0),
|
|
406
|
+
cache: {
|
|
407
|
+
write: safe(
|
|
408
|
+
(input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
|
|
409
|
+
// @ts-expect-error
|
|
410
|
+
input.metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
|
|
411
|
+
0) as number,
|
|
412
|
+
),
|
|
413
|
+
read: safe(cachedInputTokens),
|
|
414
|
+
},
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const costInfo =
|
|
418
|
+
input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
|
|
419
|
+
? input.model.cost.experimentalOver200K
|
|
420
|
+
: input.model.cost
|
|
421
|
+
return {
|
|
422
|
+
cost: safe(
|
|
423
|
+
new Decimal(0)
|
|
424
|
+
.add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
|
|
425
|
+
.add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
|
|
426
|
+
.add(new Decimal(tokens.cache.read).mul(costInfo?.cache?.read ?? 0).div(1_000_000))
|
|
427
|
+
.add(new Decimal(tokens.cache.write).mul(costInfo?.cache?.write ?? 0).div(1_000_000))
|
|
428
|
+
// TODO: update models.dev to have better pricing model, for now:
|
|
429
|
+
// charge reasoning tokens at the same rate as output tokens
|
|
430
|
+
.add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
|
|
431
|
+
.toNumber(),
|
|
432
|
+
),
|
|
433
|
+
tokens,
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
)
|
|
437
|
+
|
|
438
|
+
export class BusyError extends Error {
|
|
439
|
+
constructor(public readonly sessionID: string) {
|
|
440
|
+
super(`Session ${sessionID} is busy`)
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export const initialize = fn(
|
|
445
|
+
z.object({
|
|
446
|
+
sessionID: Identifier.schema("session"),
|
|
447
|
+
modelID: z.string(),
|
|
448
|
+
providerID: z.string(),
|
|
449
|
+
messageID: Identifier.schema("message"),
|
|
450
|
+
}),
|
|
451
|
+
async (input) => {
|
|
452
|
+
await SessionPrompt.command({
|
|
453
|
+
sessionID: input.sessionID,
|
|
454
|
+
messageID: input.messageID,
|
|
455
|
+
model: input.providerID + "/" + input.modelID,
|
|
456
|
+
command: Command.Default.INIT,
|
|
457
|
+
arguments: "",
|
|
458
|
+
})
|
|
459
|
+
},
|
|
460
|
+
)
|
|
461
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { Provider } from "@/provider/provider"
|
|
2
|
+
import { Log } from "@/util/log"
|
|
3
|
+
import { streamText, wrapLanguageModel, type ModelMessage, type StreamTextResult, type Tool, type ToolSet } from "ai"
|
|
4
|
+
import { clone, mergeDeep, pipe } from "remeda"
|
|
5
|
+
import { ProviderTransform } from "@/provider/transform"
|
|
6
|
+
import { Config } from "@/config/config"
|
|
7
|
+
import { Instance } from "@/project/instance"
|
|
8
|
+
import type { Agent } from "@/agent/agent"
|
|
9
|
+
import type { MessageV2 } from "./message-v2"
|
|
10
|
+
import { Plugin } from "@/plugin"
|
|
11
|
+
import { SystemPrompt } from "./system"
|
|
12
|
+
import { ToolRegistry } from "@/tool/registry"
|
|
13
|
+
import { Flag } from "@/flag/flag"
|
|
14
|
+
|
|
15
|
+
export namespace LLM {
|
|
16
|
+
const log = Log.create({ service: "llm" })
|
|
17
|
+
|
|
18
|
+
export const OUTPUT_TOKEN_MAX = Flag.RIRD_EXPERIMENTAL_OUTPUT_TOKEN_MAX || 32_000
|
|
19
|
+
|
|
20
|
+
export type StreamInput = {
|
|
21
|
+
user: MessageV2.User
|
|
22
|
+
sessionID: string
|
|
23
|
+
model: Provider.Model
|
|
24
|
+
agent: Agent.Info
|
|
25
|
+
system: string[]
|
|
26
|
+
abort: AbortSignal
|
|
27
|
+
messages: ModelMessage[]
|
|
28
|
+
small?: boolean
|
|
29
|
+
tools: Record<string, Tool>
|
|
30
|
+
retries?: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type StreamOutput = StreamTextResult<ToolSet, unknown>
|
|
34
|
+
|
|
35
|
+
export async function stream(input: StreamInput) {
|
|
36
|
+
const l = log
|
|
37
|
+
.clone()
|
|
38
|
+
.tag("providerID", input.model.providerID)
|
|
39
|
+
.tag("modelID", input.model.id)
|
|
40
|
+
.tag("sessionID", input.sessionID)
|
|
41
|
+
.tag("small", (input.small ?? false).toString())
|
|
42
|
+
.tag("agent", input.agent.name)
|
|
43
|
+
l.info("stream", {
|
|
44
|
+
modelID: input.model.id,
|
|
45
|
+
providerID: input.model.providerID,
|
|
46
|
+
})
|
|
47
|
+
const [language, cfg] = await Promise.all([Provider.getLanguage(input.model), Config.get()])
|
|
48
|
+
|
|
49
|
+
const system = SystemPrompt.header(input.model.providerID)
|
|
50
|
+
system.push(
|
|
51
|
+
[
|
|
52
|
+
// use agent prompt otherwise provider prompt
|
|
53
|
+
...(input.agent.prompt ? [input.agent.prompt] : SystemPrompt.provider(input.model)),
|
|
54
|
+
// any custom prompt passed into this call
|
|
55
|
+
...input.system,
|
|
56
|
+
// any custom prompt from last user message
|
|
57
|
+
...(input.user.system ? [input.user.system] : []),
|
|
58
|
+
]
|
|
59
|
+
.filter((x) => x)
|
|
60
|
+
.join("\n"),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
const header = system[0]
|
|
64
|
+
const original = clone(system)
|
|
65
|
+
await Plugin.trigger("experimental.chat.system.transform", {}, { system })
|
|
66
|
+
if (system.length === 0) {
|
|
67
|
+
system.push(...original)
|
|
68
|
+
}
|
|
69
|
+
// rejoin to maintain 2-part structure for caching if header unchanged
|
|
70
|
+
if (system.length > 2 && system[0] === header) {
|
|
71
|
+
const rest = system.slice(1)
|
|
72
|
+
system.length = 0
|
|
73
|
+
system.push(header, rest.join("\n"))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const provider = await Provider.getProvider(input.model.providerID)
|
|
77
|
+
|
|
78
|
+
const params = await Plugin.trigger(
|
|
79
|
+
"chat.params",
|
|
80
|
+
{
|
|
81
|
+
sessionID: input.sessionID,
|
|
82
|
+
agent: input.agent,
|
|
83
|
+
model: input.model,
|
|
84
|
+
provider: Provider.getProvider(input.model.providerID),
|
|
85
|
+
message: input.user,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
temperature: input.model.capabilities.temperature
|
|
89
|
+
? (input.agent.temperature ?? ProviderTransform.temperature(input.model))
|
|
90
|
+
: undefined,
|
|
91
|
+
topP: input.agent.topP ?? ProviderTransform.topP(input.model),
|
|
92
|
+
topK: ProviderTransform.topK(input.model),
|
|
93
|
+
options: pipe(
|
|
94
|
+
{},
|
|
95
|
+
mergeDeep(ProviderTransform.options(input.model, input.sessionID, provider.options)),
|
|
96
|
+
input.small ? mergeDeep(ProviderTransform.smallOptions(input.model)) : mergeDeep({}),
|
|
97
|
+
mergeDeep(input.model.options),
|
|
98
|
+
mergeDeep(input.agent.options),
|
|
99
|
+
),
|
|
100
|
+
},
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
l.info("params", {
|
|
104
|
+
params,
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const maxOutputTokens = ProviderTransform.maxOutputTokens(
|
|
108
|
+
input.model.api.npm,
|
|
109
|
+
params.options,
|
|
110
|
+
input.model.limit.output,
|
|
111
|
+
OUTPUT_TOKEN_MAX,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const tools = await resolveTools(input)
|
|
115
|
+
|
|
116
|
+
return streamText({
|
|
117
|
+
onError(error) {
|
|
118
|
+
l.error("stream error", {
|
|
119
|
+
error,
|
|
120
|
+
})
|
|
121
|
+
},
|
|
122
|
+
async experimental_repairToolCall(failed) {
|
|
123
|
+
const lower = failed.toolCall.toolName.toLowerCase()
|
|
124
|
+
if (lower !== failed.toolCall.toolName && tools[lower]) {
|
|
125
|
+
l.info("repairing tool call", {
|
|
126
|
+
tool: failed.toolCall.toolName,
|
|
127
|
+
repaired: lower,
|
|
128
|
+
})
|
|
129
|
+
return {
|
|
130
|
+
...failed.toolCall,
|
|
131
|
+
toolName: lower,
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
...failed.toolCall,
|
|
136
|
+
input: JSON.stringify({
|
|
137
|
+
tool: failed.toolCall.toolName,
|
|
138
|
+
error: failed.error.message,
|
|
139
|
+
}),
|
|
140
|
+
toolName: "invalid",
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
temperature: params.temperature,
|
|
144
|
+
topP: params.topP,
|
|
145
|
+
topK: params.topK,
|
|
146
|
+
providerOptions: ProviderTransform.providerOptions(input.model, params.options),
|
|
147
|
+
activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
|
|
148
|
+
tools,
|
|
149
|
+
maxOutputTokens,
|
|
150
|
+
abortSignal: input.abort,
|
|
151
|
+
headers: {
|
|
152
|
+
...(input.model.providerID.startsWith("rird")
|
|
153
|
+
? {
|
|
154
|
+
"x-rird-project": Instance.project.id,
|
|
155
|
+
"x-rird-session": input.sessionID,
|
|
156
|
+
"x-rird-request": input.user.id,
|
|
157
|
+
"x-rird-client": Flag.RIRD_CLIENT,
|
|
158
|
+
}
|
|
159
|
+
: undefined),
|
|
160
|
+
...input.model.headers,
|
|
161
|
+
},
|
|
162
|
+
maxRetries: input.retries ?? 0,
|
|
163
|
+
messages: [
|
|
164
|
+
...system.map(
|
|
165
|
+
(x): ModelMessage => ({
|
|
166
|
+
role: "system",
|
|
167
|
+
content: x,
|
|
168
|
+
}),
|
|
169
|
+
),
|
|
170
|
+
...input.messages,
|
|
171
|
+
],
|
|
172
|
+
model: wrapLanguageModel({
|
|
173
|
+
model: language,
|
|
174
|
+
middleware: [
|
|
175
|
+
{
|
|
176
|
+
async transformParams(args) {
|
|
177
|
+
if (args.type === "stream") {
|
|
178
|
+
// @ts-expect-error
|
|
179
|
+
args.params.prompt = ProviderTransform.message(args.params.prompt, input.model)
|
|
180
|
+
}
|
|
181
|
+
return args.params
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
}),
|
|
186
|
+
experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function resolveTools(input: Pick<StreamInput, "tools" | "agent" | "user">) {
|
|
191
|
+
const enabled = pipe(
|
|
192
|
+
input.agent.tools,
|
|
193
|
+
mergeDeep(await ToolRegistry.enabled(input.agent)),
|
|
194
|
+
mergeDeep(input.user.tools ?? {}),
|
|
195
|
+
)
|
|
196
|
+
for (const [key, value] of Object.entries(enabled)) {
|
|
197
|
+
if (value === false) delete input.tools[key]
|
|
198
|
+
}
|
|
199
|
+
return input.tools
|
|
200
|
+
}
|
|
201
|
+
}
|