@mseep/claudian 2.0.25
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/.env.local.example +2 -0
- package/.github/workflows/ci.yml +59 -0
- package/.github/workflows/claude-code-review.yml +57 -0
- package/.github/workflows/claude.yml +50 -0
- package/.github/workflows/duplicate-issues.yml +22 -0
- package/.github/workflows/release.yml +73 -0
- package/.github/workflows/stale.yml +21 -0
- package/.node-version +1 -0
- package/AGENTS.md +3 -0
- package/CLAUDE.md +80 -0
- package/LICENSE +21 -0
- package/README.md +190 -0
- package/assets/Preview.png +0 -0
- package/assets/sponsors/MOMA.png +0 -0
- package/bun.lock +1618 -0
- package/esbuild.config.mjs +195 -0
- package/eslint.config.mjs +143 -0
- package/jest.config.js +41 -0
- package/main.js +104915 -0
- package/manifest.json +10 -0
- package/package.json +65 -0
- package/scripts/build-css.mjs +119 -0
- package/scripts/build.mjs +19 -0
- package/scripts/postinstall.mjs +25 -0
- package/scripts/rendererSafeUnref.js +205 -0
- package/scripts/run-jest.js +19 -0
- package/scripts/sync-version.js +16 -0
- package/src/app/settings/ClaudianSettingsStorage.ts +435 -0
- package/src/app/settings/defaultSettings.ts +54 -0
- package/src/app/storage/SharedStorageService.ts +106 -0
- package/src/core/CLAUDE.md +84 -0
- package/src/core/auxiliary/AuxQueryRunner.ts +11 -0
- package/src/core/auxiliary/QueryBackedInlineEditService.ts +73 -0
- package/src/core/auxiliary/QueryBackedInstructionRefineService.ts +78 -0
- package/src/core/auxiliary/QueryBackedTitleGenerationService.ts +90 -0
- package/src/core/bootstrap/SessionStorage.ts +179 -0
- package/src/core/bootstrap/StoragePaths.ts +7 -0
- package/src/core/bootstrap/storage.ts +20 -0
- package/src/core/commands/builtInCommands.ts +141 -0
- package/src/core/mcp/McpConfigParser.ts +102 -0
- package/src/core/mcp/McpServerManager.ts +119 -0
- package/src/core/mcp/McpTester.ts +310 -0
- package/src/core/prompt/inlineEdit.ts +252 -0
- package/src/core/prompt/instructionRefine.ts +72 -0
- package/src/core/prompt/mainAgent.ts +213 -0
- package/src/core/prompt/titleGeneration.ts +44 -0
- package/src/core/providers/ProviderRegistry.ts +253 -0
- package/src/core/providers/ProviderSettingsCoordinator.ts +434 -0
- package/src/core/providers/ProviderWorkspaceRegistry.ts +119 -0
- package/src/core/providers/commands/ProviderCommandCatalog.ts +21 -0
- package/src/core/providers/commands/ProviderCommandEntry.ts +33 -0
- package/src/core/providers/commands/hiddenCommands.ts +74 -0
- package/src/core/providers/modelRouting.ts +17 -0
- package/src/core/providers/modelSelection.ts +72 -0
- package/src/core/providers/providerConfig.ts +34 -0
- package/src/core/providers/providerEnvironment.ts +364 -0
- package/src/core/providers/types.ts +544 -0
- package/src/core/runtime/ChatRuntime.ts +66 -0
- package/src/core/runtime/QueuedTurn.ts +97 -0
- package/src/core/runtime/types.ts +118 -0
- package/src/core/security/ApprovalManager.ts +142 -0
- package/src/core/storage/HomeFileAdapter.ts +75 -0
- package/src/core/storage/VaultFileAdapter.ts +132 -0
- package/src/core/tools/todo.ts +65 -0
- package/src/core/tools/toolIcons.ts +80 -0
- package/src/core/tools/toolInput.ts +119 -0
- package/src/core/tools/toolNames.ts +149 -0
- package/src/core/tools/toolResultContent.ts +26 -0
- package/src/core/types/agent.ts +28 -0
- package/src/core/types/chat.ts +177 -0
- package/src/core/types/diff.ts +31 -0
- package/src/core/types/index.ts +78 -0
- package/src/core/types/mcp.ts +97 -0
- package/src/core/types/plugins.ts +9 -0
- package/src/core/types/provider.ts +1 -0
- package/src/core/types/settings.ts +152 -0
- package/src/core/types/tools.ts +80 -0
- package/src/features/chat/CLAUDE.md +136 -0
- package/src/features/chat/ClaudianView.ts +762 -0
- package/src/features/chat/constants.ts +114 -0
- package/src/features/chat/controllers/BrowserSelectionController.ts +295 -0
- package/src/features/chat/controllers/CanvasSelectionController.ts +142 -0
- package/src/features/chat/controllers/ConversationController.ts +1103 -0
- package/src/features/chat/controllers/InputController.ts +1707 -0
- package/src/features/chat/controllers/NavigationController.ts +209 -0
- package/src/features/chat/controllers/SelectionController.ts +430 -0
- package/src/features/chat/controllers/StreamController.ts +1560 -0
- package/src/features/chat/controllers/contextRowVisibility.ts +18 -0
- package/src/features/chat/rendering/DiffRenderer.ts +134 -0
- package/src/features/chat/rendering/InlineAskUserQuestion.ts +702 -0
- package/src/features/chat/rendering/InlineExitPlanMode.ts +263 -0
- package/src/features/chat/rendering/InlinePlanApproval.ts +183 -0
- package/src/features/chat/rendering/MessageRenderer.ts +907 -0
- package/src/features/chat/rendering/SubagentRenderer.ts +678 -0
- package/src/features/chat/rendering/ThinkingBlockRenderer.ts +126 -0
- package/src/features/chat/rendering/TodoListRenderer.ts +5 -0
- package/src/features/chat/rendering/ToolCallRenderer.ts +1161 -0
- package/src/features/chat/rendering/WriteEditRenderer.ts +232 -0
- package/src/features/chat/rendering/collapsible.ts +101 -0
- package/src/features/chat/rendering/subagentLifecycleResolution.ts +25 -0
- package/src/features/chat/rendering/todoUtils.ts +29 -0
- package/src/features/chat/rewind.ts +31 -0
- package/src/features/chat/services/BangBashService.ts +56 -0
- package/src/features/chat/services/SubagentManager.ts +1107 -0
- package/src/features/chat/state/ChatState.ts +436 -0
- package/src/features/chat/state/types.ts +138 -0
- package/src/features/chat/tabs/Tab.ts +1886 -0
- package/src/features/chat/tabs/TabBar.ts +179 -0
- package/src/features/chat/tabs/TabManager.ts +1021 -0
- package/src/features/chat/tabs/providerResolution.ts +34 -0
- package/src/features/chat/tabs/types.ts +287 -0
- package/src/features/chat/ui/BangBashModeManager.ts +121 -0
- package/src/features/chat/ui/FileContext.ts +385 -0
- package/src/features/chat/ui/ImageContext.ts +366 -0
- package/src/features/chat/ui/InputToolbar.ts +1244 -0
- package/src/features/chat/ui/InstructionModeManager.ts +158 -0
- package/src/features/chat/ui/NavigationSidebar.ts +126 -0
- package/src/features/chat/ui/StatusPanel.ts +589 -0
- package/src/features/chat/ui/file-context/state/FileContextState.ts +83 -0
- package/src/features/chat/ui/file-context/view/FileChipsView.ts +70 -0
- package/src/features/chat/ui/textareaResize.ts +47 -0
- package/src/features/chat/utils/usageInfo.ts +26 -0
- package/src/features/inline-edit/ui/InlineEditModal.ts +895 -0
- package/src/features/inline-edit/ui/inlineEditMarkdownPreview.ts +55 -0
- package/src/features/settings/ClaudianSettings.ts +672 -0
- package/src/features/settings/keyboardNavigation.ts +60 -0
- package/src/features/settings/ui/EnvSnippetManager.ts +430 -0
- package/src/features/settings/ui/EnvironmentSettingsSection.ts +85 -0
- package/src/features/settings/ui/McpServerModal.ts +335 -0
- package/src/features/settings/ui/McpSettingsManager.ts +400 -0
- package/src/features/settings/ui/McpTestModal.ts +346 -0
- package/src/i18n/constants.ts +58 -0
- package/src/i18n/i18n.ts +140 -0
- package/src/i18n/locales/de.json +322 -0
- package/src/i18n/locales/en.json +322 -0
- package/src/i18n/locales/es.json +322 -0
- package/src/i18n/locales/fr.json +322 -0
- package/src/i18n/locales/ja.json +322 -0
- package/src/i18n/locales/ko.json +322 -0
- package/src/i18n/locales/pt.json +322 -0
- package/src/i18n/locales/ru.json +322 -0
- package/src/i18n/locales/zh-CN.json +322 -0
- package/src/i18n/locales/zh-TW.json +322 -0
- package/src/i18n/types.ts +248 -0
- package/src/main.ts +772 -0
- package/src/providers/acp/AcpClientConnection.ts +361 -0
- package/src/providers/acp/AcpJsonRpcTransport.ts +427 -0
- package/src/providers/acp/AcpSessionConfig.ts +139 -0
- package/src/providers/acp/AcpSessionUpdateNormalizer.ts +371 -0
- package/src/providers/acp/AcpSubprocess.ts +155 -0
- package/src/providers/acp/AcpToolStreamAdapter.ts +132 -0
- package/src/providers/acp/buildAcpUsageInfo.ts +41 -0
- package/src/providers/acp/index.ts +9 -0
- package/src/providers/acp/methodNames.ts +50 -0
- package/src/providers/acp/types.ts +566 -0
- package/src/providers/claude/CLAUDE.md +78 -0
- package/src/providers/claude/agents/AgentManager.ts +225 -0
- package/src/providers/claude/agents/AgentStorage.ts +101 -0
- package/src/providers/claude/app/ClaudeWorkspaceServices.ts +90 -0
- package/src/providers/claude/auxiliary/ClaudeInlineEditService.ts +122 -0
- package/src/providers/claude/auxiliary/ClaudeInstructionRefineService.ts +90 -0
- package/src/providers/claude/auxiliary/ClaudeTitleGenerationService.ts +129 -0
- package/src/providers/claude/auxiliary/extractAssistantText.ts +28 -0
- package/src/providers/claude/capabilities.ts +17 -0
- package/src/providers/claude/cli/findClaudeCLIPath.ts +261 -0
- package/src/providers/claude/commands/ClaudeCommandCatalog.ts +149 -0
- package/src/providers/claude/commands/probeRuntimeCommands.ts +86 -0
- package/src/providers/claude/env/ClaudeSettingsReconciler.ts +90 -0
- package/src/providers/claude/env/claudeModelEnv.ts +86 -0
- package/src/providers/claude/history/ClaudeConversationHistoryService.ts +446 -0
- package/src/providers/claude/history/ClaudeHistoryStore.ts +170 -0
- package/src/providers/claude/history/sdkAsyncSubagent.ts +92 -0
- package/src/providers/claude/history/sdkBranchFilter.ts +271 -0
- package/src/providers/claude/history/sdkHistoryTypes.ts +61 -0
- package/src/providers/claude/history/sdkMessageParsing.ts +413 -0
- package/src/providers/claude/history/sdkSessionPaths.ts +98 -0
- package/src/providers/claude/history/sdkSubagentSidecar.ts +261 -0
- package/src/providers/claude/hooks/SubagentHooks.ts +31 -0
- package/src/providers/claude/modelLabels.ts +77 -0
- package/src/providers/claude/modelOptions.ts +113 -0
- package/src/providers/claude/modelSelection.ts +17 -0
- package/src/providers/claude/plugins/PluginManager.ts +194 -0
- package/src/providers/claude/prompt/ClaudeTurnEncoder.ts +46 -0
- package/src/providers/claude/registration.ts +39 -0
- package/src/providers/claude/runtime/ClaudeApprovalHandler.ts +153 -0
- package/src/providers/claude/runtime/ClaudeChatRuntime.ts +1812 -0
- package/src/providers/claude/runtime/ClaudeCliResolver.ts +94 -0
- package/src/providers/claude/runtime/ClaudeDynamicUpdates.ts +164 -0
- package/src/providers/claude/runtime/ClaudeMessageChannel.ts +209 -0
- package/src/providers/claude/runtime/ClaudeQueryOptionsBuilder.ts +315 -0
- package/src/providers/claude/runtime/ClaudeRewindService.ts +220 -0
- package/src/providers/claude/runtime/ClaudeSessionManager.ts +92 -0
- package/src/providers/claude/runtime/ClaudeTaskResultInterpreter.ts +172 -0
- package/src/providers/claude/runtime/ClaudeUserMessageFactory.ts +83 -0
- package/src/providers/claude/runtime/claudeColdStartQuery.ts +152 -0
- package/src/providers/claude/runtime/customSpawn.ts +87 -0
- package/src/providers/claude/runtime/types.ts +134 -0
- package/src/providers/claude/sdk/messages.ts +17 -0
- package/src/providers/claude/sdk/toolResultContent.ts +4 -0
- package/src/providers/claude/sdk/typeGuards.ts +14 -0
- package/src/providers/claude/sdk/types.ts +15 -0
- package/src/providers/claude/security/ClaudePermissionUpdates.ts +44 -0
- package/src/providers/claude/settings.ts +138 -0
- package/src/providers/claude/storage/AgentVaultStorage.ts +101 -0
- package/src/providers/claude/storage/CCSettingsStorage.ts +153 -0
- package/src/providers/claude/storage/ClaudianSettingsStorage.ts +6 -0
- package/src/providers/claude/storage/McpStorage.ts +139 -0
- package/src/providers/claude/storage/SessionStorage.ts +5 -0
- package/src/providers/claude/storage/SkillStorage.ts +61 -0
- package/src/providers/claude/storage/SlashCommandStorage.ts +96 -0
- package/src/providers/claude/storage/StorageService.ts +185 -0
- package/src/providers/claude/stream/toolInputStreamState.ts +318 -0
- package/src/providers/claude/stream/transformClaudeMessage.ts +586 -0
- package/src/providers/claude/types/agent.ts +2 -0
- package/src/providers/claude/types/models.ts +168 -0
- package/src/providers/claude/types/plugins.ts +14 -0
- package/src/providers/claude/types/providerState.ts +16 -0
- package/src/providers/claude/types/settings.ts +98 -0
- package/src/providers/claude/ui/AgentSettings.ts +389 -0
- package/src/providers/claude/ui/ClaudeChatUIConfig.ts +113 -0
- package/src/providers/claude/ui/ClaudeSettingsTab.ts +407 -0
- package/src/providers/claude/ui/PluginSettingsManager.ts +149 -0
- package/src/providers/claude/ui/SlashCommandSettings.ts +527 -0
- package/src/providers/codex/CLAUDE.md +64 -0
- package/src/providers/codex/agents/CodexAgentMentionProvider.ts +33 -0
- package/src/providers/codex/app/CodexWorkspaceServices.ts +76 -0
- package/src/providers/codex/auxiliary/CodexInlineEditService.ts +9 -0
- package/src/providers/codex/auxiliary/CodexInstructionRefineService.ts +9 -0
- package/src/providers/codex/auxiliary/CodexTaskResultInterpreter.ts +29 -0
- package/src/providers/codex/auxiliary/CodexTitleGenerationService.ts +22 -0
- package/src/providers/codex/capabilities.ts +16 -0
- package/src/providers/codex/commands/CodexSkillCatalog.ts +180 -0
- package/src/providers/codex/env/CodexSettingsReconciler.ts +68 -0
- package/src/providers/codex/history/CodexConversationHistoryService.ts +212 -0
- package/src/providers/codex/history/CodexHistoryStore.ts +1672 -0
- package/src/providers/codex/modelOptions.ts +99 -0
- package/src/providers/codex/modelSelection.ts +17 -0
- package/src/providers/codex/normalization/codexSubagentNormalization.ts +227 -0
- package/src/providers/codex/normalization/codexToolNormalization.ts +390 -0
- package/src/providers/codex/prompt/encodeCodexTurn.ts +57 -0
- package/src/providers/codex/registration.ts +29 -0
- package/src/providers/codex/runtime/CodexAppServerProcess.ts +105 -0
- package/src/providers/codex/runtime/CodexAuxQueryRunner.ts +180 -0
- package/src/providers/codex/runtime/CodexBinaryLocator.ts +49 -0
- package/src/providers/codex/runtime/CodexChatRuntime.ts +1296 -0
- package/src/providers/codex/runtime/CodexCliResolver.ts +65 -0
- package/src/providers/codex/runtime/CodexExecutionTargetResolver.ts +104 -0
- package/src/providers/codex/runtime/CodexLaunchSpecBuilder.ts +85 -0
- package/src/providers/codex/runtime/CodexNotificationRouter.ts +1033 -0
- package/src/providers/codex/runtime/CodexPathMapper.ts +155 -0
- package/src/providers/codex/runtime/CodexRpcTransport.ts +171 -0
- package/src/providers/codex/runtime/CodexRuntimeContext.ts +109 -0
- package/src/providers/codex/runtime/CodexServerRequestRouter.ts +331 -0
- package/src/providers/codex/runtime/CodexSessionFileTail.ts +792 -0
- package/src/providers/codex/runtime/CodexSessionManager.ts +39 -0
- package/src/providers/codex/runtime/codexAppServerSupport.ts +58 -0
- package/src/providers/codex/runtime/codexAppServerTypes.ts +705 -0
- package/src/providers/codex/runtime/codexLaunchTypes.ts +30 -0
- package/src/providers/codex/settings.ts +236 -0
- package/src/providers/codex/skills/CodexSkillListingService.ts +173 -0
- package/src/providers/codex/storage/CodexSkillStorage.ts +250 -0
- package/src/providers/codex/storage/CodexSubagentStorage.ts +212 -0
- package/src/providers/codex/types/index.ts +16 -0
- package/src/providers/codex/types/models.ts +46 -0
- package/src/providers/codex/types/subagent.ts +23 -0
- package/src/providers/codex/ui/CodexChatUIConfig.ts +128 -0
- package/src/providers/codex/ui/CodexSettingsTab.ts +432 -0
- package/src/providers/codex/ui/CodexSkillSettings.ts +275 -0
- package/src/providers/codex/ui/CodexSubagentSettings.ts +400 -0
- package/src/providers/defaultProviderConfigs.ts +14 -0
- package/src/providers/index.ts +30 -0
- package/src/providers/opencode/agents/OpencodeAgentMentionProvider.ts +42 -0
- package/src/providers/opencode/app/OpencodeRuntimeCommandLoader.ts +67 -0
- package/src/providers/opencode/app/OpencodeWorkspaceServices.ts +55 -0
- package/src/providers/opencode/auxiliary/OpencodeInlineEditService.ts +13 -0
- package/src/providers/opencode/auxiliary/OpencodeInstructionRefineService.ts +12 -0
- package/src/providers/opencode/auxiliary/OpencodeTaskResultInterpreter.ts +29 -0
- package/src/providers/opencode/auxiliary/OpencodeTitleGenerationService.ts +27 -0
- package/src/providers/opencode/capabilities.ts +16 -0
- package/src/providers/opencode/commands/OpencodeCommandCatalog.ts +92 -0
- package/src/providers/opencode/discoveryState.ts +135 -0
- package/src/providers/opencode/env/OpencodeSettingsReconciler.ts +178 -0
- package/src/providers/opencode/history/OpencodeConversationHistoryService.ts +84 -0
- package/src/providers/opencode/history/OpencodeHistoryStore.ts +472 -0
- package/src/providers/opencode/history/OpencodeSqliteReader.ts +284 -0
- package/src/providers/opencode/internal/compareCollections.ts +72 -0
- package/src/providers/opencode/internal/providerProjection.ts +15 -0
- package/src/providers/opencode/models.ts +378 -0
- package/src/providers/opencode/modes.ts +150 -0
- package/src/providers/opencode/normalization/opencodeToolNormalization.ts +406 -0
- package/src/providers/opencode/registration.ts +27 -0
- package/src/providers/opencode/runtime/OpencodeAuxQueryRunner.ts +436 -0
- package/src/providers/opencode/runtime/OpencodeChatRuntime.ts +1603 -0
- package/src/providers/opencode/runtime/OpencodeCliResolver.ts +57 -0
- package/src/providers/opencode/runtime/OpencodeLaunchArtifacts.ts +231 -0
- package/src/providers/opencode/runtime/OpencodePaths.ts +113 -0
- package/src/providers/opencode/runtime/OpencodeRuntimeEnvironment.ts +18 -0
- package/src/providers/opencode/runtime/buildOpencodePrompt.ts +66 -0
- package/src/providers/opencode/settings.ts +427 -0
- package/src/providers/opencode/storage/OpencodeAgentStorage.ts +346 -0
- package/src/providers/opencode/types/agent.ts +37 -0
- package/src/providers/opencode/types/index.ts +9 -0
- package/src/providers/opencode/ui/OpencodeAgentSettings.ts +579 -0
- package/src/providers/opencode/ui/OpencodeChatUIConfig.ts +316 -0
- package/src/providers/opencode/ui/OpencodeSettingsTab.ts +674 -0
- package/src/providers/pi/app/PiRuntimeCommandLoader.ts +57 -0
- package/src/providers/pi/app/PiWorkspaceServices.ts +39 -0
- package/src/providers/pi/auxiliary/PiInlineEditService.ts +9 -0
- package/src/providers/pi/auxiliary/PiInstructionRefineService.ts +9 -0
- package/src/providers/pi/auxiliary/PiTaskResultInterpreter.ts +29 -0
- package/src/providers/pi/auxiliary/PiTitleGenerationService.ts +19 -0
- package/src/providers/pi/capabilities.ts +16 -0
- package/src/providers/pi/commands/PiCommandCatalog.ts +92 -0
- package/src/providers/pi/env/PiSettingsReconciler.ts +180 -0
- package/src/providers/pi/history/PiConversationHistoryService.ts +123 -0
- package/src/providers/pi/history/PiHistoryStore.ts +664 -0
- package/src/providers/pi/internal/compareCollections.ts +4 -0
- package/src/providers/pi/internal/providerProjection.ts +18 -0
- package/src/providers/pi/models.ts +302 -0
- package/src/providers/pi/normalizations/piEventNormalization.ts +211 -0
- package/src/providers/pi/normalizations/piToolNormalization.ts +97 -0
- package/src/providers/pi/registration.ts +30 -0
- package/src/providers/pi/runtime/PiAuxQueryRunner.ts +216 -0
- package/src/providers/pi/runtime/PiChatRuntime.ts +1064 -0
- package/src/providers/pi/runtime/PiCliResolver.ts +53 -0
- package/src/providers/pi/runtime/PiExtensionUiBridge.ts +161 -0
- package/src/providers/pi/runtime/PiJsonl.ts +71 -0
- package/src/providers/pi/runtime/PiLaunchSpec.ts +70 -0
- package/src/providers/pi/runtime/PiModelDiscoveryService.ts +92 -0
- package/src/providers/pi/runtime/PiRpcPayloads.ts +18 -0
- package/src/providers/pi/runtime/PiRpcTransport.ts +243 -0
- package/src/providers/pi/runtime/PiSubprocess.ts +159 -0
- package/src/providers/pi/runtime/buildPiPrompt.ts +62 -0
- package/src/providers/pi/runtime/buildPiUsageInfo.ts +69 -0
- package/src/providers/pi/settings.ts +468 -0
- package/src/providers/pi/types.ts +64 -0
- package/src/providers/pi/ui/ObsidianPiExtensionUiRenderer.ts +251 -0
- package/src/providers/pi/ui/PiChatUIConfig.ts +265 -0
- package/src/providers/pi/ui/PiExtensionUiRenderer.ts +12 -0
- package/src/providers/pi/ui/PiSettingsTab.ts +642 -0
- package/src/shared/components/ResumeSessionDropdown.ts +185 -0
- package/src/shared/components/SelectableDropdown.ts +140 -0
- package/src/shared/components/SelectionHighlight.ts +77 -0
- package/src/shared/components/SlashCommandDropdown.ts +421 -0
- package/src/shared/icons.ts +180 -0
- package/src/shared/mention/MentionDropdownController.ts +627 -0
- package/src/shared/mention/VaultMentionCache.ts +106 -0
- package/src/shared/mention/VaultMentionDataProvider.ts +51 -0
- package/src/shared/mention/types.ts +67 -0
- package/src/shared/modals/ConfirmModal.ts +60 -0
- package/src/shared/modals/ForkTargetModal.ts +47 -0
- package/src/shared/modals/InstructionConfirmModal.ts +281 -0
- package/src/style/CLAUDE.md +49 -0
- package/src/style/accessibility.css +40 -0
- package/src/style/base/animations.css +44 -0
- package/src/style/base/container.css +20 -0
- package/src/style/base/variables.css +46 -0
- package/src/style/base/visibility.css +15 -0
- package/src/style/components/code.css +97 -0
- package/src/style/components/context-footer.css +76 -0
- package/src/style/components/header.css +27 -0
- package/src/style/components/history.css +221 -0
- package/src/style/components/input.css +312 -0
- package/src/style/components/messages.css +262 -0
- package/src/style/components/nav-sidebar.css +58 -0
- package/src/style/components/status-panel.css +202 -0
- package/src/style/components/subagent.css +248 -0
- package/src/style/components/tabs.css +112 -0
- package/src/style/components/thinking.css +88 -0
- package/src/style/components/toolcalls.css +278 -0
- package/src/style/features/ask-user-question.css +315 -0
- package/src/style/features/diff.css +197 -0
- package/src/style/features/file-context.css +188 -0
- package/src/style/features/file-link.css +22 -0
- package/src/style/features/image-context.css +179 -0
- package/src/style/features/image-embed.css +40 -0
- package/src/style/features/image-modal.css +52 -0
- package/src/style/features/inline-edit.css +278 -0
- package/src/style/features/plan-mode.css +103 -0
- package/src/style/features/resume-session.css +119 -0
- package/src/style/features/slash-commands.css +91 -0
- package/src/style/index.css +63 -0
- package/src/style/modals/fork-target.css +21 -0
- package/src/style/modals/instruction.css +161 -0
- package/src/style/modals/mcp-modal.css +241 -0
- package/src/style/settings/agent-settings.css +2 -0
- package/src/style/settings/base.css +300 -0
- package/src/style/settings/env-snippets.css +366 -0
- package/src/style/settings/mcp-settings.css +211 -0
- package/src/style/settings/plugin-settings.css +164 -0
- package/src/style/settings/provider-model-picker.css +367 -0
- package/src/style/settings/slash-settings.css +16 -0
- package/src/style/toolbar/external-context.css +177 -0
- package/src/style/toolbar/mcp-selector.css +176 -0
- package/src/style/toolbar/mode-selector.css +19 -0
- package/src/style/toolbar/model-selector.css +99 -0
- package/src/style/toolbar/permission-toggle.css +56 -0
- package/src/style/toolbar/service-tier-toggle.css +39 -0
- package/src/style/toolbar/thinking-selector.css +83 -0
- package/src/types/smol-toml.d.ts +4 -0
- package/src/utils/agent.ts +50 -0
- package/src/utils/animationFrame.ts +46 -0
- package/src/utils/browser.ts +46 -0
- package/src/utils/canvas.ts +14 -0
- package/src/utils/cliBinaryLocator.ts +97 -0
- package/src/utils/context.ts +117 -0
- package/src/utils/contextMentionResolver.ts +154 -0
- package/src/utils/date.ts +31 -0
- package/src/utils/diff.ts +384 -0
- package/src/utils/editor.ts +104 -0
- package/src/utils/electronCompat.ts +53 -0
- package/src/utils/env.ts +465 -0
- package/src/utils/externalContext.ts +143 -0
- package/src/utils/externalContextScanner.ts +135 -0
- package/src/utils/fileLink.ts +263 -0
- package/src/utils/frontmatter.ts +194 -0
- package/src/utils/imageEmbed.ts +139 -0
- package/src/utils/inlineEdit.ts +22 -0
- package/src/utils/interrupt.ts +23 -0
- package/src/utils/markdown.ts +25 -0
- package/src/utils/markdownMath.ts +130 -0
- package/src/utils/mcp.ts +96 -0
- package/src/utils/obsidianCompat.ts +23 -0
- package/src/utils/path.ts +342 -0
- package/src/utils/session.ts +240 -0
- package/src/utils/slashCommand.ts +152 -0
- package/src/utils/subagentJsonl.ts +52 -0
- package/src/utils/windowsCmdShim.ts +98 -0
- package/tests/__mocks__/claude-agent-sdk.ts +317 -0
- package/tests/__mocks__/codex-sdk.ts +88 -0
- package/tests/__mocks__/obsidian.ts +434 -0
- package/tests/helpers/mockElement.ts +403 -0
- package/tests/helpers/sdkMessages.ts +291 -0
- package/tests/integration/core/agent/ClaudianService.test.ts +1845 -0
- package/tests/integration/core/mcp/mcp.test.ts +905 -0
- package/tests/integration/features/chat/imagePersistence.test.ts +38 -0
- package/tests/integration/main.test.ts +1701 -0
- package/tests/setupWindow.ts +26 -0
- package/tests/tsconfig.json +7 -0
- package/tests/unit/core/commands/builtInCommands.test.ts +239 -0
- package/tests/unit/core/mcp/McpServerManager.test.ts +405 -0
- package/tests/unit/core/mcp/McpTester.test.ts +282 -0
- package/tests/unit/core/mcp/createNodeFetch.test.ts +188 -0
- package/tests/unit/core/providers/ProviderRegistry.test.ts +275 -0
- package/tests/unit/core/providers/ProviderSettingsCoordinator.test.ts +490 -0
- package/tests/unit/core/providers/ProviderWorkspaceRegistry.test.ts +84 -0
- package/tests/unit/core/providers/modelRouting.test.ts +91 -0
- package/tests/unit/core/providers/modelSelection.test.ts +155 -0
- package/tests/unit/core/providers/providerEnvironment.test.ts +162 -0
- package/tests/unit/core/providers/tabLifecycle.test.ts +217 -0
- package/tests/unit/core/security/ApprovalManager.test.ts +152 -0
- package/tests/unit/core/storage/VaultFileAdapter.test.ts +535 -0
- package/tests/unit/core/tools/todo.test.ts +227 -0
- package/tests/unit/core/tools/toolIcons.test.ts +75 -0
- package/tests/unit/core/tools/toolInput.test.ts +350 -0
- package/tests/unit/core/tools/toolNames.test.ts +464 -0
- package/tests/unit/core/types/mcp.test.ts +115 -0
- package/tests/unit/features/chat/ClaudianView.test.ts +404 -0
- package/tests/unit/features/chat/controllers/BrowserSelectionController.test.ts +179 -0
- package/tests/unit/features/chat/controllers/CanvasSelectionController.test.ts +216 -0
- package/tests/unit/features/chat/controllers/ConversationController.test.ts +2764 -0
- package/tests/unit/features/chat/controllers/InputController.test.ts +3188 -0
- package/tests/unit/features/chat/controllers/NavigationController.test.ts +640 -0
- package/tests/unit/features/chat/controllers/SelectionController.test.ts +695 -0
- package/tests/unit/features/chat/controllers/StreamController.test.ts +2534 -0
- package/tests/unit/features/chat/controllers/contextRowVisibility.test.ts +46 -0
- package/tests/unit/features/chat/controllers/index.test.ts +16 -0
- package/tests/unit/features/chat/rendering/DiffRenderer.test.ts +355 -0
- package/tests/unit/features/chat/rendering/InlineAskUserQuestion.test.ts +1035 -0
- package/tests/unit/features/chat/rendering/InlineExitPlanMode.test.ts +191 -0
- package/tests/unit/features/chat/rendering/InlinePlanApproval.test.ts +126 -0
- package/tests/unit/features/chat/rendering/MessageRenderer.test.ts +2004 -0
- package/tests/unit/features/chat/rendering/SubagentRenderer.test.ts +917 -0
- package/tests/unit/features/chat/rendering/ThinkingBlockRenderer.test.ts +124 -0
- package/tests/unit/features/chat/rendering/TodoListRenderer.test.ts +173 -0
- package/tests/unit/features/chat/rendering/ToolCallRenderer.test.ts +909 -0
- package/tests/unit/features/chat/rendering/WriteEditRenderer.test.ts +474 -0
- package/tests/unit/features/chat/rendering/collapsible.test.ts +158 -0
- package/tests/unit/features/chat/rendering/todoUtils.test.ts +105 -0
- package/tests/unit/features/chat/rewind.test.ts +56 -0
- package/tests/unit/features/chat/services/BangBashService.test.ts +142 -0
- package/tests/unit/features/chat/services/InstructionRefineService.test.ts +371 -0
- package/tests/unit/features/chat/services/SubagentManager.test.ts +1759 -0
- package/tests/unit/features/chat/services/TitleGenerationService.test.ts +480 -0
- package/tests/unit/features/chat/state/ChatState.test.ts +581 -0
- package/tests/unit/features/chat/tabs/Tab.test.ts +4287 -0
- package/tests/unit/features/chat/tabs/TabBar.test.ts +357 -0
- package/tests/unit/features/chat/tabs/TabManager.test.ts +2962 -0
- package/tests/unit/features/chat/tabs/index.test.ts +11 -0
- package/tests/unit/features/chat/ui/BangBashModeManager.test.ts +321 -0
- package/tests/unit/features/chat/ui/ExternalContextSelector.test.ts +555 -0
- package/tests/unit/features/chat/ui/FileContextManager.test.ts +876 -0
- package/tests/unit/features/chat/ui/ImageContext.test.ts +777 -0
- package/tests/unit/features/chat/ui/InputToolbar.test.ts +1139 -0
- package/tests/unit/features/chat/ui/InstructionModeManager.test.ts +243 -0
- package/tests/unit/features/chat/ui/NavigationSidebar.test.ts +570 -0
- package/tests/unit/features/chat/ui/StatusPanel.test.ts +953 -0
- package/tests/unit/features/chat/ui/file-context/state/FileContextState.test.ts +155 -0
- package/tests/unit/features/chat/ui/textareaResize.test.ts +102 -0
- package/tests/unit/features/chat/utils/usageInfo.test.ts +56 -0
- package/tests/unit/features/inline-edit/InlineEditService.test.ts +1199 -0
- package/tests/unit/features/inline-edit/ui/InlineEditModal.openAndWait.test.ts +1482 -0
- package/tests/unit/features/inline-edit/ui/InlineEditModal.test.ts +495 -0
- package/tests/unit/features/inline-edit/ui/inlineEditMarkdownPreview.test.ts +92 -0
- package/tests/unit/features/settings/AgentSettings.test.ts +82 -0
- package/tests/unit/features/settings/keyboardNavigation.test.ts +73 -0
- package/tests/unit/features/settings/ui/CodexSkillSettings.test.ts +294 -0
- package/tests/unit/features/settings/ui/CodexSubagentSettings.test.ts +207 -0
- package/tests/unit/i18n/constants.test.ts +43 -0
- package/tests/unit/i18n/i18n.test.ts +244 -0
- package/tests/unit/i18n/locales.test.ts +134 -0
- package/tests/unit/providers/acp/AcpClientConnection.test.ts +248 -0
- package/tests/unit/providers/acp/AcpJsonRpcTransport.test.ts +186 -0
- package/tests/unit/providers/acp/AcpSessionConfig.test.ts +247 -0
- package/tests/unit/providers/acp/AcpSessionUpdateNormalizer.test.ts +145 -0
- package/tests/unit/providers/acp/AcpSubprocess.test.ts +105 -0
- package/tests/unit/providers/acp/buildAcpUsageInfo.test.ts +51 -0
- package/tests/unit/providers/claude/agents/AgentManager.test.ts +590 -0
- package/tests/unit/providers/claude/agents/AgentStorage.test.ts +434 -0
- package/tests/unit/providers/claude/agents/index.test.ts +10 -0
- package/tests/unit/providers/claude/commands/ClaudeCommandCatalog.test.ts +396 -0
- package/tests/unit/providers/claude/commands/probeRuntimeCommands.test.ts +92 -0
- package/tests/unit/providers/claude/env/ClaudeSettingsReconciler.test.ts +57 -0
- package/tests/unit/providers/claude/env/claudeModelEnv.test.ts +228 -0
- package/tests/unit/providers/claude/hooks/SubagentHooks.test.ts +83 -0
- package/tests/unit/providers/claude/plugins/PluginManager.test.ts +832 -0
- package/tests/unit/providers/claude/plugins/index.test.ts +7 -0
- package/tests/unit/providers/claude/prompt/ClaudeTurnEncoder.test.ts +145 -0
- package/tests/unit/providers/claude/prompt/instructionRefine.test.ts +185 -0
- package/tests/unit/providers/claude/prompt/systemPrompt.test.ts +163 -0
- package/tests/unit/providers/claude/prompt/titleGeneration.test.ts +20 -0
- package/tests/unit/providers/claude/runtime/ClaudeTaskResultInterpreter.test.ts +28 -0
- package/tests/unit/providers/claude/runtime/ClaudianService.test.ts +3796 -0
- package/tests/unit/providers/claude/runtime/MessageChannel.test.ts +421 -0
- package/tests/unit/providers/claude/runtime/QueryOptionsBuilder.test.ts +775 -0
- package/tests/unit/providers/claude/runtime/SessionManager.test.ts +182 -0
- package/tests/unit/providers/claude/runtime/claudeColdStartQuery.test.ts +331 -0
- package/tests/unit/providers/claude/runtime/customSpawn.test.ts +374 -0
- package/tests/unit/providers/claude/runtime/index.test.ts +13 -0
- package/tests/unit/providers/claude/runtime/types.test.ts +190 -0
- package/tests/unit/providers/claude/sdk/typeGuards.test.ts +50 -0
- package/tests/unit/providers/claude/security/ClaudePermissionUpdates.test.ts +198 -0
- package/tests/unit/providers/claude/storage/AgentVaultStorage.test.ts +413 -0
- package/tests/unit/providers/claude/storage/CCSettingsStorage.test.ts +408 -0
- package/tests/unit/providers/claude/storage/ClaudianSettingsStorage.test.ts +653 -0
- package/tests/unit/providers/claude/storage/McpStorage.test.ts +619 -0
- package/tests/unit/providers/claude/storage/SessionStorage.test.ts +680 -0
- package/tests/unit/providers/claude/storage/SkillStorage.test.ts +275 -0
- package/tests/unit/providers/claude/storage/SlashCommandStorage.test.ts +612 -0
- package/tests/unit/providers/claude/storage/storage.test.ts +360 -0
- package/tests/unit/providers/claude/storage/storageService.convenience.test.ts +447 -0
- package/tests/unit/providers/claude/stream/transformSDKMessage.test.ts +1729 -0
- package/tests/unit/providers/claude/types/types.test.ts +726 -0
- package/tests/unit/providers/claude/ui/ClaudeChatUIConfig.test.ts +173 -0
- package/tests/unit/providers/claude/ui/ClaudeSettingsTab.test.ts +466 -0
- package/tests/unit/providers/codex/agents/CodexAgentMentionProvider.test.ts +89 -0
- package/tests/unit/providers/codex/auxiliary/CodexInstructionRefineService.test.ts +81 -0
- package/tests/unit/providers/codex/capabilities.test.ts +39 -0
- package/tests/unit/providers/codex/commands/CodexSkillCatalog.test.ts +413 -0
- package/tests/unit/providers/codex/env/CodexSettingsReconciler.test.ts +106 -0
- package/tests/unit/providers/codex/fixtures/codex-session-abort.jsonl +9 -0
- package/tests/unit/providers/codex/fixtures/codex-session-agent-lifecycle.jsonl +12 -0
- package/tests/unit/providers/codex/fixtures/codex-session-persisted-tools.jsonl +15 -0
- package/tests/unit/providers/codex/fixtures/codex-session-simple.jsonl +10 -0
- package/tests/unit/providers/codex/fixtures/codex-session-tools.jsonl +12 -0
- package/tests/unit/providers/codex/fixtures/codex-session-websearch-persisted.jsonl +3 -0
- package/tests/unit/providers/codex/fixtures/codex-session-websearch.jsonl +7 -0
- package/tests/unit/providers/codex/history/CodexConversationHistoryService.test.ts +690 -0
- package/tests/unit/providers/codex/history/CodexHistoryStore.test.ts +2202 -0
- package/tests/unit/providers/codex/normalization/codexSubagentNormalization.test.ts +81 -0
- package/tests/unit/providers/codex/normalization/codexToolNormalization.test.ts +322 -0
- package/tests/unit/providers/codex/prompt/encodeCodexTurn.test.ts +168 -0
- package/tests/unit/providers/codex/runtime/CodexAppServerProcess.test.ts +255 -0
- package/tests/unit/providers/codex/runtime/CodexAuxQueryRunner.test.ts +130 -0
- package/tests/unit/providers/codex/runtime/CodexBinaryLocator.test.ts +90 -0
- package/tests/unit/providers/codex/runtime/CodexChatRuntime.test.ts +2445 -0
- package/tests/unit/providers/codex/runtime/CodexCliResolver.test.ts +103 -0
- package/tests/unit/providers/codex/runtime/CodexExecutionTargetResolver.test.ts +105 -0
- package/tests/unit/providers/codex/runtime/CodexLaunchSpecBuilder.test.ts +150 -0
- package/tests/unit/providers/codex/runtime/CodexNotificationRouter.test.ts +1248 -0
- package/tests/unit/providers/codex/runtime/CodexPathMapper.test.ts +51 -0
- package/tests/unit/providers/codex/runtime/CodexRpcTransport.test.ts +220 -0
- package/tests/unit/providers/codex/runtime/CodexRuntimeContext.test.ts +107 -0
- package/tests/unit/providers/codex/runtime/CodexServerRequestRouter.test.ts +537 -0
- package/tests/unit/providers/codex/runtime/CodexSessionFileTail.test.ts +1305 -0
- package/tests/unit/providers/codex/runtime/CodexSessionManager.test.ts +82 -0
- package/tests/unit/providers/codex/runtime/codexAppServerTypes.test.ts +336 -0
- package/tests/unit/providers/codex/settings.test.ts +189 -0
- package/tests/unit/providers/codex/skills/CodexSkillListingService.test.ts +178 -0
- package/tests/unit/providers/codex/storage/CodexSkillStorage.test.ts +342 -0
- package/tests/unit/providers/codex/storage/CodexSubagentStorage.test.ts +376 -0
- package/tests/unit/providers/codex/ui/CodexChatUIConfig.test.ts +211 -0
- package/tests/unit/providers/codex/ui/CodexSettingsTab.test.ts +578 -0
- package/tests/unit/providers/defaultProviderConfigs.test.ts +18 -0
- package/tests/unit/providers/opencode/OpencodeAuxQueryRunner.test.ts +355 -0
- package/tests/unit/providers/opencode/OpencodeChatRuntime.test.ts +808 -0
- package/tests/unit/providers/opencode/OpencodeCliResolver.test.ts +93 -0
- package/tests/unit/providers/opencode/OpencodeCommandCatalog.test.ts +75 -0
- package/tests/unit/providers/opencode/OpencodeConversationHistoryService.test.ts +103 -0
- package/tests/unit/providers/opencode/OpencodeHistoryStore.test.ts +418 -0
- package/tests/unit/providers/opencode/OpencodeLaunchArtifacts.test.ts +268 -0
- package/tests/unit/providers/opencode/OpencodePaths.test.ts +35 -0
- package/tests/unit/providers/opencode/OpencodeRuntimeCommandLoader.test.ts +129 -0
- package/tests/unit/providers/opencode/OpencodeSettingsReconciler.test.ts +96 -0
- package/tests/unit/providers/opencode/OpencodeSettingsTab.test.ts +649 -0
- package/tests/unit/providers/opencode/OpencodeSqliteReader.test.ts +185 -0
- package/tests/unit/providers/opencode/agents/OpencodeAgentMentionProvider.test.ts +56 -0
- package/tests/unit/providers/opencode/buildOpencodePrompt.test.ts +87 -0
- package/tests/unit/providers/opencode/capabilities.test.ts +39 -0
- package/tests/unit/providers/opencode/models.test.ts +273 -0
- package/tests/unit/providers/opencode/modes.test.ts +151 -0
- package/tests/unit/providers/opencode/opencodeToolNormalization.test.ts +197 -0
- package/tests/unit/providers/opencode/settings.test.ts +469 -0
- package/tests/unit/providers/opencode/storage/OpencodeAgentStorage.test.ts +377 -0
- package/tests/unit/providers/opencode/ui/OpencodeAgentSettings.test.ts +91 -0
- package/tests/unit/providers/pi/PiRuntimeCommandLoader.test.ts +149 -0
- package/tests/unit/providers/pi/capabilities.test.ts +20 -0
- package/tests/unit/providers/pi/commands/PiCommandCatalog.test.ts +69 -0
- package/tests/unit/providers/pi/env/PiSettingsReconciler.test.ts +61 -0
- package/tests/unit/providers/pi/history/PiConversationHistoryService.test.ts +147 -0
- package/tests/unit/providers/pi/history/PiHistoryStore.test.ts +523 -0
- package/tests/unit/providers/pi/models.test.ts +96 -0
- package/tests/unit/providers/pi/registration.test.ts +54 -0
- package/tests/unit/providers/pi/runtime/PiAuxQueryRunner.test.ts +172 -0
- package/tests/unit/providers/pi/runtime/PiChatRuntime.test.ts +830 -0
- package/tests/unit/providers/pi/runtime/PiCliResolver.test.ts +105 -0
- package/tests/unit/providers/pi/runtime/PiEventNormalization.test.ts +147 -0
- package/tests/unit/providers/pi/runtime/PiExtensionUiBridge.test.ts +98 -0
- package/tests/unit/providers/pi/runtime/PiJsonl.test.ts +42 -0
- package/tests/unit/providers/pi/runtime/PiLaunchSpec.test.ts +92 -0
- package/tests/unit/providers/pi/runtime/PiModelDiscoveryService.test.ts +135 -0
- package/tests/unit/providers/pi/runtime/PiRpcPayloads.test.ts +15 -0
- package/tests/unit/providers/pi/runtime/PiRpcTransport.test.ts +116 -0
- package/tests/unit/providers/pi/runtime/PiSubprocess.test.ts +151 -0
- package/tests/unit/providers/pi/runtime/buildPiPrompt.test.ts +84 -0
- package/tests/unit/providers/pi/runtime/buildPiUsageInfo.test.ts +69 -0
- package/tests/unit/providers/pi/settings.test.ts +253 -0
- package/tests/unit/providers/pi/ui/PiChatUIConfig.test.ts +161 -0
- package/tests/unit/providers/pi/ui/PiSettingsTab.test.ts +492 -0
- package/tests/unit/scripts/rendererSafeUnref.test.ts +101 -0
- package/tests/unit/shared/components/ResumeSessionDropdown.test.ts +356 -0
- package/tests/unit/shared/components/SelectableDropdown.test.ts +406 -0
- package/tests/unit/shared/components/SlashCommandDropdown.provider.test.ts +354 -0
- package/tests/unit/shared/components/SlashCommandDropdown.test.ts +508 -0
- package/tests/unit/shared/icons.test.ts +56 -0
- package/tests/unit/shared/index.test.ts +46 -0
- package/tests/unit/shared/mention/MentionDropdownController.test.ts +823 -0
- package/tests/unit/shared/mention/VaultFileCache.test.ts +195 -0
- package/tests/unit/shared/mention/VaultFolderCache.test.ts +143 -0
- package/tests/unit/shared/mention/VaultMentionDataProvider.test.ts +87 -0
- package/tests/unit/shared/modals/ConfirmModal.test.ts +111 -0
- package/tests/unit/shared/modals/ForkTargetModal.test.ts +101 -0
- package/tests/unit/shared/modals/InstructionConfirmModal.test.ts +305 -0
- package/tests/unit/utils/agent.test.ts +395 -0
- package/tests/unit/utils/animationFrame.test.ts +59 -0
- package/tests/unit/utils/browser.test.ts +73 -0
- package/tests/unit/utils/canvas.test.ts +54 -0
- package/tests/unit/utils/claudeCli.test.ts +294 -0
- package/tests/unit/utils/cliBinaryLocator.test.ts +33 -0
- package/tests/unit/utils/context.test.ts +288 -0
- package/tests/unit/utils/contextMentionResolver.test.ts +270 -0
- package/tests/unit/utils/date.test.ts +80 -0
- package/tests/unit/utils/diff.test.ts +291 -0
- package/tests/unit/utils/editor.test.ts +249 -0
- package/tests/unit/utils/electronCompat.test.ts +87 -0
- package/tests/unit/utils/env.test.ts +1240 -0
- package/tests/unit/utils/externalContext.test.ts +336 -0
- package/tests/unit/utils/externalContextScanner.test.ts +186 -0
- package/tests/unit/utils/fileLink.dom.test.ts +273 -0
- package/tests/unit/utils/fileLink.handler.test.ts +64 -0
- package/tests/unit/utils/fileLink.test.ts +233 -0
- package/tests/unit/utils/frontmatter.test.ts +434 -0
- package/tests/unit/utils/imageEmbed.test.ts +407 -0
- package/tests/unit/utils/inlineEdit.test.ts +63 -0
- package/tests/unit/utils/interrupt.test.ts +73 -0
- package/tests/unit/utils/markdown.test.ts +35 -0
- package/tests/unit/utils/markdownMath.test.ts +54 -0
- package/tests/unit/utils/mcp.test.ts +256 -0
- package/tests/unit/utils/obsidianCompat.test.ts +18 -0
- package/tests/unit/utils/path.test.ts +677 -0
- package/tests/unit/utils/sdkSession.test.ts +2359 -0
- package/tests/unit/utils/session.test.ts +971 -0
- package/tests/unit/utils/slashCommand.test.ts +778 -0
- package/tests/unit/utils/utils.test.ts +809 -0
- package/tsconfig.jest.json +8 -0
- package/tsconfig.json +26 -0
- package/versions.json +4 -0
|
@@ -0,0 +1,1729 @@
|
|
|
1
|
+
import { buildSDKMessage } from '@test/helpers/sdkMessages';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createTransformStreamState,
|
|
5
|
+
createTransformUsageState,
|
|
6
|
+
transformSDKMessage,
|
|
7
|
+
} from '@/providers/claude/stream/transformClaudeMessage';
|
|
8
|
+
|
|
9
|
+
const msg = buildSDKMessage;
|
|
10
|
+
|
|
11
|
+
describe('transformSDKMessage', () => {
|
|
12
|
+
describe('system messages', () => {
|
|
13
|
+
it('yields session_init event for init subtype with session_id', () => {
|
|
14
|
+
const message = msg({
|
|
15
|
+
type: 'system',
|
|
16
|
+
subtype: 'init',
|
|
17
|
+
session_id: 'test-session-123',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const results = [...transformSDKMessage(message)];
|
|
21
|
+
|
|
22
|
+
expect(results).toEqual([
|
|
23
|
+
{
|
|
24
|
+
type: 'session_init',
|
|
25
|
+
sessionId: 'test-session-123',
|
|
26
|
+
agents: undefined,
|
|
27
|
+
permissionMode: 'default',
|
|
28
|
+
},
|
|
29
|
+
]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('yields nothing for system messages without init subtype', () => {
|
|
33
|
+
const message = msg({
|
|
34
|
+
type: 'system',
|
|
35
|
+
subtype: 'status',
|
|
36
|
+
session_id: 'test-session',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const results = [...transformSDKMessage(message)];
|
|
40
|
+
|
|
41
|
+
expect(results).toEqual([]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('yields context_compacted event for compact_boundary subtype', () => {
|
|
45
|
+
const message = msg({
|
|
46
|
+
type: 'system',
|
|
47
|
+
subtype: 'compact_boundary',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const results = [...transformSDKMessage(message)];
|
|
51
|
+
|
|
52
|
+
expect(results).toEqual([
|
|
53
|
+
{ type: 'context_compacted' },
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('captures agents from init message', () => {
|
|
58
|
+
const message = msg({
|
|
59
|
+
type: 'system',
|
|
60
|
+
subtype: 'init',
|
|
61
|
+
session_id: 'test-session-456',
|
|
62
|
+
agents: ['Explore', 'Plan', 'custom-agent'],
|
|
63
|
+
skills: ['commit', 'review-pr'],
|
|
64
|
+
slash_commands: ['clear', 'add-dir'],
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const results = [...transformSDKMessage(message)];
|
|
68
|
+
|
|
69
|
+
expect(results).toHaveLength(1);
|
|
70
|
+
expect(results[0]).toEqual({
|
|
71
|
+
type: 'session_init',
|
|
72
|
+
sessionId: 'test-session-456',
|
|
73
|
+
agents: ['Explore', 'Plan', 'custom-agent'],
|
|
74
|
+
permissionMode: 'default',
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('captures permissionMode from init message', () => {
|
|
79
|
+
const message = msg({
|
|
80
|
+
type: 'system',
|
|
81
|
+
subtype: 'init',
|
|
82
|
+
session_id: 'test-session-789',
|
|
83
|
+
permissionMode: 'plan',
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const results = [...transformSDKMessage(message)];
|
|
87
|
+
|
|
88
|
+
expect(results).toHaveLength(1);
|
|
89
|
+
expect(results[0]).toEqual({
|
|
90
|
+
type: 'session_init',
|
|
91
|
+
sessionId: 'test-session-789',
|
|
92
|
+
permissionMode: 'plan',
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('normalizes task_notification completion into async subagent result', () => {
|
|
97
|
+
const message = msg({
|
|
98
|
+
type: 'system',
|
|
99
|
+
subtype: 'task_notification',
|
|
100
|
+
task_id: 'agent-123',
|
|
101
|
+
status: 'completed',
|
|
102
|
+
output_file: '/tmp/agent-123.output',
|
|
103
|
+
summary: 'Agent completed successfully.',
|
|
104
|
+
} as any);
|
|
105
|
+
|
|
106
|
+
const results = [...transformSDKMessage(message)];
|
|
107
|
+
|
|
108
|
+
expect(results).toEqual([
|
|
109
|
+
{
|
|
110
|
+
type: 'async_subagent_result',
|
|
111
|
+
agentId: 'agent-123',
|
|
112
|
+
status: 'completed',
|
|
113
|
+
result: 'Agent completed successfully.',
|
|
114
|
+
},
|
|
115
|
+
]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('maps non-completed task_notification statuses to async subagent errors', () => {
|
|
119
|
+
const message = msg({
|
|
120
|
+
type: 'system',
|
|
121
|
+
subtype: 'task_notification',
|
|
122
|
+
task_id: 'agent-failed',
|
|
123
|
+
status: 'failed',
|
|
124
|
+
output_file: '/tmp/agent-failed.output',
|
|
125
|
+
summary: 'Agent failed.',
|
|
126
|
+
} as any);
|
|
127
|
+
|
|
128
|
+
const results = [...transformSDKMessage(message)];
|
|
129
|
+
|
|
130
|
+
expect(results).toEqual([
|
|
131
|
+
{
|
|
132
|
+
type: 'async_subagent_result',
|
|
133
|
+
agentId: 'agent-failed',
|
|
134
|
+
status: 'error',
|
|
135
|
+
result: 'Agent failed.',
|
|
136
|
+
},
|
|
137
|
+
]);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('assistant messages', () => {
|
|
142
|
+
it('yields text content block', () => {
|
|
143
|
+
const message = msg({
|
|
144
|
+
type: 'assistant',
|
|
145
|
+
message: {
|
|
146
|
+
content: [
|
|
147
|
+
{ type: 'text', text: 'Hello, world!' },
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
const results = [...transformSDKMessage(message)];
|
|
153
|
+
|
|
154
|
+
expect(results).toEqual([
|
|
155
|
+
{ type: 'text', content: 'Hello, world!' },
|
|
156
|
+
]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('yields thinking content block', () => {
|
|
160
|
+
const message = msg({
|
|
161
|
+
type: 'assistant',
|
|
162
|
+
message: {
|
|
163
|
+
content: [
|
|
164
|
+
{ type: 'thinking', thinking: 'Let me think about this...' },
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const results = [...transformSDKMessage(message)];
|
|
170
|
+
|
|
171
|
+
expect(results).toEqual([
|
|
172
|
+
{ type: 'thinking', content: 'Let me think about this...' },
|
|
173
|
+
]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('yields tool_use content block with all fields', () => {
|
|
177
|
+
const message = msg({
|
|
178
|
+
type: 'assistant',
|
|
179
|
+
message: {
|
|
180
|
+
content: [
|
|
181
|
+
{
|
|
182
|
+
type: 'tool_use',
|
|
183
|
+
id: 'tool-123',
|
|
184
|
+
name: 'Read',
|
|
185
|
+
input: { file_path: '/test/file.ts' },
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const results = [...transformSDKMessage(message)];
|
|
192
|
+
|
|
193
|
+
expect(results).toEqual([
|
|
194
|
+
{
|
|
195
|
+
type: 'tool_use',
|
|
196
|
+
id: 'tool-123',
|
|
197
|
+
name: 'Read',
|
|
198
|
+
input: { file_path: '/test/file.ts' },
|
|
199
|
+
},
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('generates fallback id for tool_use without id', () => {
|
|
204
|
+
const message = msg({
|
|
205
|
+
type: 'assistant',
|
|
206
|
+
message: {
|
|
207
|
+
content: [
|
|
208
|
+
{ type: 'tool_use', name: 'Bash' },
|
|
209
|
+
],
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const results = [...transformSDKMessage(message)];
|
|
214
|
+
|
|
215
|
+
expect(results.length).toBe(1);
|
|
216
|
+
expect(results[0].type).toBe('tool_use');
|
|
217
|
+
expect((results[0] as any).id).toMatch(/^tool-\d+-\w+$/);
|
|
218
|
+
expect((results[0] as any).name).toBe('Bash');
|
|
219
|
+
expect((results[0] as any).input).toEqual({});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('handles multiple content blocks', () => {
|
|
223
|
+
const message = msg({
|
|
224
|
+
type: 'assistant',
|
|
225
|
+
message: {
|
|
226
|
+
content: [
|
|
227
|
+
{ type: 'thinking', thinking: 'Thinking...' },
|
|
228
|
+
{ type: 'text', text: 'Here is my response' },
|
|
229
|
+
{ type: 'tool_use', id: 'tool-1', name: 'Read', input: {} },
|
|
230
|
+
],
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const results = [...transformSDKMessage(message)];
|
|
235
|
+
|
|
236
|
+
expect(results).toHaveLength(3);
|
|
237
|
+
expect(results[0]).toEqual({ type: 'thinking', content: 'Thinking...' });
|
|
238
|
+
expect(results[1]).toEqual({ type: 'text', content: 'Here is my response' });
|
|
239
|
+
expect(results[2]).toMatchObject({ type: 'tool_use', id: 'tool-1', name: 'Read' });
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('yields subagent_tool_use for assistant tool_use in subagent context', () => {
|
|
243
|
+
const message = msg({
|
|
244
|
+
type: 'assistant',
|
|
245
|
+
parent_tool_use_id: 'parent-tool-abc',
|
|
246
|
+
message: {
|
|
247
|
+
content: [
|
|
248
|
+
{ type: 'tool_use', id: 'child-tool-1', name: 'Read', input: { file_path: 'subagent.md' } },
|
|
249
|
+
],
|
|
250
|
+
},
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const results = [...transformSDKMessage(message)];
|
|
254
|
+
|
|
255
|
+
expect(results).toEqual([
|
|
256
|
+
{
|
|
257
|
+
type: 'subagent_tool_use',
|
|
258
|
+
subagentId: 'parent-tool-abc',
|
|
259
|
+
id: 'child-tool-1',
|
|
260
|
+
name: 'Read',
|
|
261
|
+
input: { file_path: 'subagent.md' },
|
|
262
|
+
},
|
|
263
|
+
]);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('handles empty content array', () => {
|
|
267
|
+
const message = msg({
|
|
268
|
+
type: 'assistant',
|
|
269
|
+
message: { content: [] },
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const results = [...transformSDKMessage(message)];
|
|
273
|
+
|
|
274
|
+
expect(results).toEqual([]);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('handles missing message.content', () => {
|
|
278
|
+
const message = msg({
|
|
279
|
+
type: 'assistant',
|
|
280
|
+
message: {},
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const results = [...transformSDKMessage(message)];
|
|
284
|
+
|
|
285
|
+
expect(results).toEqual([]);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('skips empty text blocks', () => {
|
|
289
|
+
const message = msg({
|
|
290
|
+
type: 'assistant',
|
|
291
|
+
message: {
|
|
292
|
+
content: [
|
|
293
|
+
{ type: 'text', text: '' },
|
|
294
|
+
{ type: 'text', text: 'Valid text' },
|
|
295
|
+
],
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
const results = [...transformSDKMessage(message)];
|
|
300
|
+
|
|
301
|
+
expect(results).toEqual([
|
|
302
|
+
{ type: 'text', content: 'Valid text' },
|
|
303
|
+
]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('skips "(no content)" placeholder text blocks', () => {
|
|
307
|
+
const message = msg({
|
|
308
|
+
type: 'assistant',
|
|
309
|
+
message: {
|
|
310
|
+
content: [
|
|
311
|
+
{ type: 'text', text: '(no content)' },
|
|
312
|
+
{ type: 'tool_use', id: 'tool-1', name: 'Skill', input: { skill: 'md2docx' } },
|
|
313
|
+
],
|
|
314
|
+
},
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const results = [...transformSDKMessage(message)];
|
|
318
|
+
|
|
319
|
+
expect(results).toEqual([
|
|
320
|
+
{ type: 'tool_use', id: 'tool-1', name: 'Skill', input: { skill: 'md2docx' } },
|
|
321
|
+
]);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('skips empty thinking blocks', () => {
|
|
325
|
+
const message = msg({
|
|
326
|
+
type: 'assistant',
|
|
327
|
+
message: {
|
|
328
|
+
content: [
|
|
329
|
+
{ type: 'thinking', thinking: '' },
|
|
330
|
+
{ type: 'thinking', thinking: 'Valid thinking' },
|
|
331
|
+
],
|
|
332
|
+
},
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
const results = [...transformSDKMessage(message)];
|
|
336
|
+
|
|
337
|
+
expect(results).toEqual([
|
|
338
|
+
{ type: 'thinking', content: 'Valid thinking' },
|
|
339
|
+
]);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('yields error event for assistant message with error field', () => {
|
|
343
|
+
const message = msg({
|
|
344
|
+
type: 'assistant',
|
|
345
|
+
error: 'rate_limit',
|
|
346
|
+
message: {
|
|
347
|
+
content: [
|
|
348
|
+
{ type: 'text', text: 'Partial response' },
|
|
349
|
+
],
|
|
350
|
+
},
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
const results = [...transformSDKMessage(message)];
|
|
354
|
+
|
|
355
|
+
expect(results).toEqual([
|
|
356
|
+
{ type: 'error', content: 'rate_limit' },
|
|
357
|
+
{ type: 'text', content: 'Partial response' },
|
|
358
|
+
]);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
describe('user messages', () => {
|
|
363
|
+
it('yields warning notice for blocked tool calls', () => {
|
|
364
|
+
const message = msg({
|
|
365
|
+
type: 'user',
|
|
366
|
+
_blocked: true,
|
|
367
|
+
_blockReason: 'Command blocked: rm -rf /',
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const results = [...transformSDKMessage(message)];
|
|
371
|
+
|
|
372
|
+
expect(results).toEqual([
|
|
373
|
+
{ type: 'notice', content: 'Command blocked: rm -rf /', level: 'warning' },
|
|
374
|
+
]);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it('yields tool_result for tool_use_result with parent_tool_use_id', () => {
|
|
378
|
+
const message = msg({
|
|
379
|
+
type: 'user',
|
|
380
|
+
parent_tool_use_id: 'tool-123',
|
|
381
|
+
tool_use_result: 'File contents here',
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const results = [...transformSDKMessage(message)];
|
|
385
|
+
|
|
386
|
+
expect(results).toEqual([
|
|
387
|
+
{
|
|
388
|
+
type: 'subagent_tool_result',
|
|
389
|
+
subagentId: 'tool-123',
|
|
390
|
+
id: 'tool-123',
|
|
391
|
+
content: 'File contents here',
|
|
392
|
+
isError: false,
|
|
393
|
+
toolUseResult: 'File contents here',
|
|
394
|
+
},
|
|
395
|
+
]);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('stringifies non-string tool_use_result', () => {
|
|
399
|
+
const message = msg({
|
|
400
|
+
type: 'user',
|
|
401
|
+
parent_tool_use_id: 'tool-123',
|
|
402
|
+
tool_use_result: { status: 'success', data: [1, 2, 3] },
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const results = [...transformSDKMessage(message)];
|
|
406
|
+
|
|
407
|
+
expect(results.length).toBe(1);
|
|
408
|
+
expect(results[0].type).toBe('subagent_tool_result');
|
|
409
|
+
expect((results[0] as any).content).toContain('"status": "success"');
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
it('extracts text from array-based tool_use_result content', () => {
|
|
413
|
+
const toolUseResult = [
|
|
414
|
+
{ type: 'text', text: 'Agent completed successfully.' },
|
|
415
|
+
{ type: 'text', text: 'Saved summary to notes.md' },
|
|
416
|
+
];
|
|
417
|
+
const message = msg({
|
|
418
|
+
type: 'user',
|
|
419
|
+
parent_tool_use_id: 'tool-123',
|
|
420
|
+
tool_use_result: toolUseResult,
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const results = [...transformSDKMessage(message)];
|
|
424
|
+
|
|
425
|
+
expect(results).toEqual([
|
|
426
|
+
{
|
|
427
|
+
type: 'subagent_tool_result',
|
|
428
|
+
subagentId: 'tool-123',
|
|
429
|
+
id: 'tool-123',
|
|
430
|
+
content: 'Agent completed successfully.\nSaved summary to notes.md',
|
|
431
|
+
isError: false,
|
|
432
|
+
toolUseResult,
|
|
433
|
+
},
|
|
434
|
+
]);
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('yields tool_result from message.content blocks', () => {
|
|
438
|
+
const message = msg({
|
|
439
|
+
type: 'user',
|
|
440
|
+
message: {
|
|
441
|
+
content: [
|
|
442
|
+
{
|
|
443
|
+
type: 'tool_result',
|
|
444
|
+
tool_use_id: 'tool-456',
|
|
445
|
+
content: 'Result content',
|
|
446
|
+
is_error: false,
|
|
447
|
+
},
|
|
448
|
+
],
|
|
449
|
+
},
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
const results = [...transformSDKMessage(message)];
|
|
453
|
+
|
|
454
|
+
expect(results).toEqual([
|
|
455
|
+
{
|
|
456
|
+
type: 'tool_result',
|
|
457
|
+
id: 'tool-456',
|
|
458
|
+
content: 'Result content',
|
|
459
|
+
isError: false,
|
|
460
|
+
},
|
|
461
|
+
]);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it('handles tool_result with is_error flag', () => {
|
|
465
|
+
const message = msg({
|
|
466
|
+
type: 'user',
|
|
467
|
+
message: {
|
|
468
|
+
content: [
|
|
469
|
+
{
|
|
470
|
+
type: 'tool_result',
|
|
471
|
+
tool_use_id: 'tool-error',
|
|
472
|
+
content: 'Error: File not found',
|
|
473
|
+
is_error: true,
|
|
474
|
+
},
|
|
475
|
+
],
|
|
476
|
+
},
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
const results = [...transformSDKMessage(message)];
|
|
480
|
+
|
|
481
|
+
expect(results).toEqual([
|
|
482
|
+
{
|
|
483
|
+
type: 'tool_result',
|
|
484
|
+
id: 'tool-error',
|
|
485
|
+
content: 'Error: File not found',
|
|
486
|
+
isError: true,
|
|
487
|
+
},
|
|
488
|
+
]);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it('extracts text from array content in tool_result blocks', () => {
|
|
492
|
+
const message = msg({
|
|
493
|
+
type: 'user',
|
|
494
|
+
message: {
|
|
495
|
+
content: [
|
|
496
|
+
{
|
|
497
|
+
type: 'tool_result',
|
|
498
|
+
tool_use_id: 'tool-agent',
|
|
499
|
+
content: [
|
|
500
|
+
{ type: 'text', text: 'Agent completed successfully.' },
|
|
501
|
+
{ type: 'text', text: 'Next step queued.' },
|
|
502
|
+
],
|
|
503
|
+
},
|
|
504
|
+
],
|
|
505
|
+
},
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
const results = [...transformSDKMessage(message)];
|
|
509
|
+
|
|
510
|
+
expect(results).toEqual([
|
|
511
|
+
{
|
|
512
|
+
type: 'tool_result',
|
|
513
|
+
id: 'tool-agent',
|
|
514
|
+
content: 'Agent completed successfully.\nNext step queued.',
|
|
515
|
+
isError: false,
|
|
516
|
+
},
|
|
517
|
+
]);
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
it('stringifies non-string object content in tool_result blocks', () => {
|
|
521
|
+
const message = msg({
|
|
522
|
+
type: 'user',
|
|
523
|
+
message: {
|
|
524
|
+
content: [
|
|
525
|
+
{
|
|
526
|
+
type: 'tool_result',
|
|
527
|
+
tool_use_id: 'tool-obj',
|
|
528
|
+
content: { key: 'value' },
|
|
529
|
+
},
|
|
530
|
+
],
|
|
531
|
+
},
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
const results = [...transformSDKMessage(message)];
|
|
535
|
+
|
|
536
|
+
expect(results.length).toBe(1);
|
|
537
|
+
expect((results[0] as any).content).toContain('"key": "value"');
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it('preserves tool_reference array content in tool_result blocks', () => {
|
|
541
|
+
const toolRefs = [
|
|
542
|
+
{ type: 'tool_reference', tool_name: 'WebSearch' },
|
|
543
|
+
{ type: 'tool_reference', tool_name: 'Grep' },
|
|
544
|
+
];
|
|
545
|
+
const message = msg({
|
|
546
|
+
type: 'user',
|
|
547
|
+
message: {
|
|
548
|
+
content: [
|
|
549
|
+
{
|
|
550
|
+
type: 'tool_result',
|
|
551
|
+
tool_use_id: 'tool-search-1',
|
|
552
|
+
content: toolRefs,
|
|
553
|
+
},
|
|
554
|
+
],
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
const results = [...transformSDKMessage(message)];
|
|
559
|
+
|
|
560
|
+
expect(results.length).toBe(1);
|
|
561
|
+
expect((results[0] as any).content).toBe(JSON.stringify(toolRefs, null, 2));
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it('uses parent_tool_use_id as fallback for tool_result id', () => {
|
|
565
|
+
const message = msg({
|
|
566
|
+
type: 'user',
|
|
567
|
+
parent_tool_use_id: 'fallback-id',
|
|
568
|
+
message: {
|
|
569
|
+
content: [
|
|
570
|
+
{ type: 'tool_result', content: 'Some result' },
|
|
571
|
+
],
|
|
572
|
+
},
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
const results = [...transformSDKMessage(message)];
|
|
576
|
+
|
|
577
|
+
expect(results.length).toBe(1);
|
|
578
|
+
expect((results[0] as any).id).toBe('fallback-id');
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
it('yields nothing for user messages without tool results', () => {
|
|
582
|
+
const message = msg({
|
|
583
|
+
type: 'user',
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
const results = [...transformSDKMessage(message)];
|
|
587
|
+
|
|
588
|
+
expect(results).toEqual([]);
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
describe('stream_event messages', () => {
|
|
593
|
+
it('yields tool_use for content_block_start with tool_use', () => {
|
|
594
|
+
const message = msg({
|
|
595
|
+
type: 'stream_event',
|
|
596
|
+
event: {
|
|
597
|
+
type: 'content_block_start',
|
|
598
|
+
content_block: {
|
|
599
|
+
type: 'tool_use',
|
|
600
|
+
id: 'stream-tool-1',
|
|
601
|
+
name: 'Write',
|
|
602
|
+
input: { file_path: '/test.ts' },
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
const results = [...transformSDKMessage(message)];
|
|
608
|
+
|
|
609
|
+
expect(results).toEqual([
|
|
610
|
+
{
|
|
611
|
+
type: 'tool_use',
|
|
612
|
+
id: 'stream-tool-1',
|
|
613
|
+
name: 'Write',
|
|
614
|
+
input: { file_path: '/test.ts' },
|
|
615
|
+
},
|
|
616
|
+
]);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
it('generates fallback id for content_block_start without id', () => {
|
|
620
|
+
const message = msg({
|
|
621
|
+
type: 'stream_event',
|
|
622
|
+
event: {
|
|
623
|
+
type: 'content_block_start',
|
|
624
|
+
content_block: {
|
|
625
|
+
type: 'tool_use',
|
|
626
|
+
name: 'Glob',
|
|
627
|
+
},
|
|
628
|
+
},
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
const results = [...transformSDKMessage(message)];
|
|
632
|
+
|
|
633
|
+
expect(results.length).toBe(1);
|
|
634
|
+
expect((results[0] as any).id).toMatch(/^tool-\d+$/);
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it('yields cumulative tool_use updates for input_json_delta', () => {
|
|
638
|
+
const streamState = createTransformStreamState();
|
|
639
|
+
const startMessage = msg({
|
|
640
|
+
type: 'stream_event',
|
|
641
|
+
event: {
|
|
642
|
+
type: 'content_block_start',
|
|
643
|
+
index: 0,
|
|
644
|
+
content_block: {
|
|
645
|
+
type: 'tool_use',
|
|
646
|
+
id: 'stream-tool-1',
|
|
647
|
+
name: 'Write',
|
|
648
|
+
input: {},
|
|
649
|
+
},
|
|
650
|
+
},
|
|
651
|
+
});
|
|
652
|
+
const firstDeltaMessage = msg({
|
|
653
|
+
type: 'stream_event',
|
|
654
|
+
event: {
|
|
655
|
+
type: 'content_block_delta',
|
|
656
|
+
index: 0,
|
|
657
|
+
delta: {
|
|
658
|
+
type: 'input_json_delta',
|
|
659
|
+
partial_json: '{"file_path":"notes.md"',
|
|
660
|
+
},
|
|
661
|
+
},
|
|
662
|
+
});
|
|
663
|
+
const secondDeltaMessage = msg({
|
|
664
|
+
type: 'stream_event',
|
|
665
|
+
event: {
|
|
666
|
+
type: 'content_block_delta',
|
|
667
|
+
index: 0,
|
|
668
|
+
delta: {
|
|
669
|
+
type: 'input_json_delta',
|
|
670
|
+
partial_json: ',"content":"Hello"',
|
|
671
|
+
},
|
|
672
|
+
},
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
expect([...transformSDKMessage(startMessage, { streamState })]).toEqual([
|
|
676
|
+
{
|
|
677
|
+
type: 'tool_use',
|
|
678
|
+
id: 'stream-tool-1',
|
|
679
|
+
name: 'Write',
|
|
680
|
+
input: {},
|
|
681
|
+
},
|
|
682
|
+
]);
|
|
683
|
+
expect([...transformSDKMessage(firstDeltaMessage, { streamState })]).toEqual([
|
|
684
|
+
{
|
|
685
|
+
type: 'tool_use',
|
|
686
|
+
id: 'stream-tool-1',
|
|
687
|
+
name: 'Write',
|
|
688
|
+
input: { file_path: 'notes.md' },
|
|
689
|
+
},
|
|
690
|
+
]);
|
|
691
|
+
expect([...transformSDKMessage(secondDeltaMessage, { streamState })]).toEqual([
|
|
692
|
+
{
|
|
693
|
+
type: 'tool_use',
|
|
694
|
+
id: 'stream-tool-1',
|
|
695
|
+
name: 'Write',
|
|
696
|
+
input: { file_path: 'notes.md', content: 'Hello' },
|
|
697
|
+
},
|
|
698
|
+
]);
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
it('yields thinking for content_block_start with thinking', () => {
|
|
702
|
+
const message = msg({
|
|
703
|
+
type: 'stream_event',
|
|
704
|
+
event: {
|
|
705
|
+
type: 'content_block_start',
|
|
706
|
+
content_block: {
|
|
707
|
+
type: 'thinking',
|
|
708
|
+
thinking: 'Initial thinking...',
|
|
709
|
+
},
|
|
710
|
+
},
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
const results = [...transformSDKMessage(message)];
|
|
714
|
+
|
|
715
|
+
expect(results).toEqual([
|
|
716
|
+
{ type: 'thinking', content: 'Initial thinking...' },
|
|
717
|
+
]);
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
it('yields text for content_block_start with text', () => {
|
|
721
|
+
const message = msg({
|
|
722
|
+
type: 'stream_event',
|
|
723
|
+
event: {
|
|
724
|
+
type: 'content_block_start',
|
|
725
|
+
content_block: {
|
|
726
|
+
type: 'text',
|
|
727
|
+
text: 'Starting response...',
|
|
728
|
+
},
|
|
729
|
+
},
|
|
730
|
+
});
|
|
731
|
+
|
|
732
|
+
const results = [...transformSDKMessage(message)];
|
|
733
|
+
|
|
734
|
+
expect(results).toEqual([
|
|
735
|
+
{ type: 'text', content: 'Starting response...' },
|
|
736
|
+
]);
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it('yields thinking for thinking_delta', () => {
|
|
740
|
+
const message = msg({
|
|
741
|
+
type: 'stream_event',
|
|
742
|
+
event: {
|
|
743
|
+
type: 'content_block_delta',
|
|
744
|
+
delta: {
|
|
745
|
+
type: 'thinking_delta',
|
|
746
|
+
thinking: 'More thinking...',
|
|
747
|
+
},
|
|
748
|
+
},
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
const results = [...transformSDKMessage(message)];
|
|
752
|
+
|
|
753
|
+
expect(results).toEqual([
|
|
754
|
+
{ type: 'thinking', content: 'More thinking...' },
|
|
755
|
+
]);
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
it('yields text for text_delta', () => {
|
|
759
|
+
const message = msg({
|
|
760
|
+
type: 'stream_event',
|
|
761
|
+
event: {
|
|
762
|
+
type: 'content_block_delta',
|
|
763
|
+
delta: {
|
|
764
|
+
type: 'text_delta',
|
|
765
|
+
text: ' additional text',
|
|
766
|
+
},
|
|
767
|
+
},
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
const results = [...transformSDKMessage(message)];
|
|
771
|
+
|
|
772
|
+
expect(results).toEqual([
|
|
773
|
+
{ type: 'text', content: ' additional text' },
|
|
774
|
+
]);
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
it('yields nothing for empty thinking in content_block_start', () => {
|
|
778
|
+
const message = msg({
|
|
779
|
+
type: 'stream_event',
|
|
780
|
+
event: {
|
|
781
|
+
type: 'content_block_start',
|
|
782
|
+
content_block: {
|
|
783
|
+
type: 'thinking',
|
|
784
|
+
thinking: '',
|
|
785
|
+
},
|
|
786
|
+
},
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
const results = [...transformSDKMessage(message)];
|
|
790
|
+
|
|
791
|
+
expect(results).toEqual([]);
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it('yields nothing for empty text in content_block_start', () => {
|
|
795
|
+
const message = msg({
|
|
796
|
+
type: 'stream_event',
|
|
797
|
+
event: {
|
|
798
|
+
type: 'content_block_start',
|
|
799
|
+
content_block: {
|
|
800
|
+
type: 'text',
|
|
801
|
+
text: '',
|
|
802
|
+
},
|
|
803
|
+
},
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
const results = [...transformSDKMessage(message)];
|
|
807
|
+
|
|
808
|
+
expect(results).toEqual([]);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
it('yields nothing for empty thinking_delta', () => {
|
|
812
|
+
const message = msg({
|
|
813
|
+
type: 'stream_event',
|
|
814
|
+
event: {
|
|
815
|
+
type: 'content_block_delta',
|
|
816
|
+
delta: {
|
|
817
|
+
type: 'thinking_delta',
|
|
818
|
+
thinking: '',
|
|
819
|
+
},
|
|
820
|
+
},
|
|
821
|
+
});
|
|
822
|
+
|
|
823
|
+
const results = [...transformSDKMessage(message)];
|
|
824
|
+
|
|
825
|
+
expect(results).toEqual([]);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it('yields nothing for empty text_delta', () => {
|
|
829
|
+
const message = msg({
|
|
830
|
+
type: 'stream_event',
|
|
831
|
+
event: {
|
|
832
|
+
type: 'content_block_delta',
|
|
833
|
+
delta: {
|
|
834
|
+
type: 'text_delta',
|
|
835
|
+
text: '',
|
|
836
|
+
},
|
|
837
|
+
},
|
|
838
|
+
});
|
|
839
|
+
|
|
840
|
+
const results = [...transformSDKMessage(message)];
|
|
841
|
+
|
|
842
|
+
expect(results).toEqual([]);
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
it('suppresses subagent text deltas in stream events', () => {
|
|
846
|
+
const message = msg({
|
|
847
|
+
type: 'stream_event',
|
|
848
|
+
parent_tool_use_id: 'subagent-parent',
|
|
849
|
+
event: {
|
|
850
|
+
type: 'content_block_delta',
|
|
851
|
+
delta: {
|
|
852
|
+
type: 'text_delta',
|
|
853
|
+
text: 'Subagent stream text',
|
|
854
|
+
},
|
|
855
|
+
},
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
const results = [...transformSDKMessage(message)];
|
|
859
|
+
|
|
860
|
+
expect(results).toEqual([]);
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
it('handles missing event property', () => {
|
|
864
|
+
const message = msg({
|
|
865
|
+
type: 'stream_event',
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
const results = [...transformSDKMessage(message)];
|
|
869
|
+
|
|
870
|
+
expect(results).toEqual([]);
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
it('yields usage when Anthropic-compatible message_delta carries prompt tokens', () => {
|
|
874
|
+
const usageState = createTransformUsageState();
|
|
875
|
+
const startMessage = msg({
|
|
876
|
+
type: 'stream_event',
|
|
877
|
+
event: {
|
|
878
|
+
type: 'message_start',
|
|
879
|
+
message: {
|
|
880
|
+
usage: {
|
|
881
|
+
input_tokens: 0,
|
|
882
|
+
output_tokens: 0,
|
|
883
|
+
},
|
|
884
|
+
},
|
|
885
|
+
},
|
|
886
|
+
});
|
|
887
|
+
const deltaMessage = msg({
|
|
888
|
+
type: 'stream_event',
|
|
889
|
+
event: {
|
|
890
|
+
type: 'message_delta',
|
|
891
|
+
delta: { stop_reason: 'end_turn' },
|
|
892
|
+
usage: {
|
|
893
|
+
input_tokens: 16,
|
|
894
|
+
output_tokens: 6,
|
|
895
|
+
cache_read_input_tokens: 0,
|
|
896
|
+
},
|
|
897
|
+
},
|
|
898
|
+
});
|
|
899
|
+
|
|
900
|
+
expect([...transformSDKMessage(startMessage, {
|
|
901
|
+
intendedModel: 'glm-5.1',
|
|
902
|
+
usageState,
|
|
903
|
+
})]).toEqual([]);
|
|
904
|
+
|
|
905
|
+
const results = [...transformSDKMessage(deltaMessage, {
|
|
906
|
+
intendedModel: 'glm-5.1',
|
|
907
|
+
usageState,
|
|
908
|
+
})];
|
|
909
|
+
|
|
910
|
+
expect(results).toEqual([
|
|
911
|
+
{
|
|
912
|
+
type: 'usage',
|
|
913
|
+
usage: {
|
|
914
|
+
model: 'glm-5.1',
|
|
915
|
+
inputTokens: 16,
|
|
916
|
+
cacheCreationInputTokens: 0,
|
|
917
|
+
cacheReadInputTokens: 0,
|
|
918
|
+
contextWindow: 200000,
|
|
919
|
+
contextTokens: 16,
|
|
920
|
+
percentage: 0,
|
|
921
|
+
},
|
|
922
|
+
},
|
|
923
|
+
]);
|
|
924
|
+
});
|
|
925
|
+
|
|
926
|
+
it('keeps standard message_start prompt usage on the final assistant usage path', () => {
|
|
927
|
+
const usageState = createTransformUsageState();
|
|
928
|
+
const startMessage = msg({
|
|
929
|
+
type: 'stream_event',
|
|
930
|
+
event: {
|
|
931
|
+
type: 'message_start',
|
|
932
|
+
message: {
|
|
933
|
+
usage: {
|
|
934
|
+
input_tokens: 10,
|
|
935
|
+
output_tokens: 0,
|
|
936
|
+
cache_creation_input_tokens: 0,
|
|
937
|
+
cache_read_input_tokens: 0,
|
|
938
|
+
},
|
|
939
|
+
},
|
|
940
|
+
},
|
|
941
|
+
});
|
|
942
|
+
const deltaMessage = msg({
|
|
943
|
+
type: 'stream_event',
|
|
944
|
+
event: {
|
|
945
|
+
type: 'message_delta',
|
|
946
|
+
delta: { stop_reason: 'end_turn' },
|
|
947
|
+
usage: {
|
|
948
|
+
input_tokens: 10,
|
|
949
|
+
output_tokens: 4,
|
|
950
|
+
},
|
|
951
|
+
},
|
|
952
|
+
});
|
|
953
|
+
const assistantMessage = msg({
|
|
954
|
+
type: 'assistant',
|
|
955
|
+
parent_tool_use_id: null,
|
|
956
|
+
message: {
|
|
957
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
958
|
+
usage: {
|
|
959
|
+
input_tokens: 10,
|
|
960
|
+
output_tokens: 4,
|
|
961
|
+
cache_creation_input_tokens: 0,
|
|
962
|
+
cache_read_input_tokens: 0,
|
|
963
|
+
},
|
|
964
|
+
},
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
const startResults = [...transformSDKMessage(startMessage, {
|
|
968
|
+
intendedModel: 'sonnet',
|
|
969
|
+
usageState,
|
|
970
|
+
})];
|
|
971
|
+
const deltaResults = [...transformSDKMessage(deltaMessage, {
|
|
972
|
+
intendedModel: 'sonnet',
|
|
973
|
+
usageState,
|
|
974
|
+
})];
|
|
975
|
+
const assistantResults = [...transformSDKMessage(assistantMessage, {
|
|
976
|
+
intendedModel: 'sonnet',
|
|
977
|
+
usageState,
|
|
978
|
+
})];
|
|
979
|
+
|
|
980
|
+
expect(startResults).toEqual([]);
|
|
981
|
+
expect(deltaResults).toEqual([]);
|
|
982
|
+
expect(assistantResults).toEqual([
|
|
983
|
+
{ type: 'text', content: 'Hello' },
|
|
984
|
+
{
|
|
985
|
+
type: 'usage',
|
|
986
|
+
usage: {
|
|
987
|
+
model: 'sonnet',
|
|
988
|
+
inputTokens: 10,
|
|
989
|
+
cacheCreationInputTokens: 0,
|
|
990
|
+
cacheReadInputTokens: 0,
|
|
991
|
+
contextWindow: 200000,
|
|
992
|
+
contextTokens: 10,
|
|
993
|
+
percentage: 0,
|
|
994
|
+
},
|
|
995
|
+
},
|
|
996
|
+
]);
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
it('emits message_start prompt usage at result when no assistant usage arrives', () => {
|
|
1000
|
+
const usageState = createTransformUsageState();
|
|
1001
|
+
const startMessage = msg({
|
|
1002
|
+
type: 'stream_event',
|
|
1003
|
+
event: {
|
|
1004
|
+
type: 'message_start',
|
|
1005
|
+
message: {
|
|
1006
|
+
usage: {
|
|
1007
|
+
input_tokens: 10,
|
|
1008
|
+
output_tokens: 0,
|
|
1009
|
+
cache_creation_input_tokens: 0,
|
|
1010
|
+
cache_read_input_tokens: 0,
|
|
1011
|
+
},
|
|
1012
|
+
},
|
|
1013
|
+
},
|
|
1014
|
+
});
|
|
1015
|
+
const deltaMessage = msg({
|
|
1016
|
+
type: 'stream_event',
|
|
1017
|
+
event: {
|
|
1018
|
+
type: 'message_delta',
|
|
1019
|
+
delta: { stop_reason: 'end_turn' },
|
|
1020
|
+
usage: {
|
|
1021
|
+
output_tokens: 4,
|
|
1022
|
+
},
|
|
1023
|
+
},
|
|
1024
|
+
});
|
|
1025
|
+
const resultMessage = msg({
|
|
1026
|
+
type: 'result',
|
|
1027
|
+
subtype: 'success',
|
|
1028
|
+
modelUsage: undefined,
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
expect([...transformSDKMessage(startMessage, {
|
|
1032
|
+
intendedModel: 'sonnet',
|
|
1033
|
+
usageState,
|
|
1034
|
+
})]).toEqual([]);
|
|
1035
|
+
expect([...transformSDKMessage(deltaMessage, {
|
|
1036
|
+
intendedModel: 'sonnet',
|
|
1037
|
+
usageState,
|
|
1038
|
+
})]).toEqual([]);
|
|
1039
|
+
|
|
1040
|
+
expect([...transformSDKMessage(resultMessage, {
|
|
1041
|
+
intendedModel: 'sonnet',
|
|
1042
|
+
usageState,
|
|
1043
|
+
})]).toEqual([
|
|
1044
|
+
{
|
|
1045
|
+
type: 'usage',
|
|
1046
|
+
usage: {
|
|
1047
|
+
model: 'sonnet',
|
|
1048
|
+
inputTokens: 10,
|
|
1049
|
+
cacheCreationInputTokens: 0,
|
|
1050
|
+
cacheReadInputTokens: 0,
|
|
1051
|
+
contextWindow: 200000,
|
|
1052
|
+
contextTokens: 10,
|
|
1053
|
+
percentage: 0,
|
|
1054
|
+
},
|
|
1055
|
+
},
|
|
1056
|
+
]);
|
|
1057
|
+
});
|
|
1058
|
+
|
|
1059
|
+
it('ignores standard message_delta usage that only contains output tokens', () => {
|
|
1060
|
+
const usageState = createTransformUsageState();
|
|
1061
|
+
const message = msg({
|
|
1062
|
+
type: 'stream_event',
|
|
1063
|
+
event: {
|
|
1064
|
+
type: 'message_delta',
|
|
1065
|
+
delta: { stop_reason: 'end_turn' },
|
|
1066
|
+
usage: {
|
|
1067
|
+
output_tokens: 6,
|
|
1068
|
+
},
|
|
1069
|
+
},
|
|
1070
|
+
});
|
|
1071
|
+
|
|
1072
|
+
const results = [...transformSDKMessage(message, { usageState })];
|
|
1073
|
+
|
|
1074
|
+
expect(results).toEqual([]);
|
|
1075
|
+
});
|
|
1076
|
+
|
|
1077
|
+
it('ignores subagent stream usage deltas', () => {
|
|
1078
|
+
const usageState = createTransformUsageState();
|
|
1079
|
+
const message = msg({
|
|
1080
|
+
type: 'stream_event',
|
|
1081
|
+
parent_tool_use_id: 'subagent-parent',
|
|
1082
|
+
event: {
|
|
1083
|
+
type: 'message_delta',
|
|
1084
|
+
usage: {
|
|
1085
|
+
input_tokens: 16,
|
|
1086
|
+
output_tokens: 6,
|
|
1087
|
+
},
|
|
1088
|
+
},
|
|
1089
|
+
});
|
|
1090
|
+
|
|
1091
|
+
const results = [...transformSDKMessage(message, { usageState })];
|
|
1092
|
+
|
|
1093
|
+
expect(results).toEqual([]);
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
it('ignores subagent message_start usage', () => {
|
|
1097
|
+
const usageState = createTransformUsageState();
|
|
1098
|
+
const message = msg({
|
|
1099
|
+
type: 'stream_event',
|
|
1100
|
+
parent_tool_use_id: 'subagent-parent',
|
|
1101
|
+
event: {
|
|
1102
|
+
type: 'message_start',
|
|
1103
|
+
message: {
|
|
1104
|
+
usage: {
|
|
1105
|
+
input_tokens: 16,
|
|
1106
|
+
output_tokens: 0,
|
|
1107
|
+
},
|
|
1108
|
+
},
|
|
1109
|
+
},
|
|
1110
|
+
});
|
|
1111
|
+
|
|
1112
|
+
const results = [...transformSDKMessage(message, { usageState })];
|
|
1113
|
+
|
|
1114
|
+
expect(results).toEqual([]);
|
|
1115
|
+
expect([...transformSDKMessage(msg({
|
|
1116
|
+
type: 'result',
|
|
1117
|
+
subtype: 'success',
|
|
1118
|
+
modelUsage: undefined,
|
|
1119
|
+
}), { usageState })]).toEqual([]);
|
|
1120
|
+
});
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
describe('result messages', () => {
|
|
1124
|
+
it('yields context_window for successful result messages with modelUsage', () => {
|
|
1125
|
+
const message = msg({
|
|
1126
|
+
type: 'result',
|
|
1127
|
+
modelUsage: {
|
|
1128
|
+
'claude-sonnet-4-5-20250514': {
|
|
1129
|
+
inputTokens: 1000,
|
|
1130
|
+
cacheCreationInputTokens: 500,
|
|
1131
|
+
cacheReadInputTokens: 200,
|
|
1132
|
+
outputTokens: 300,
|
|
1133
|
+
webSearchRequests: 0,
|
|
1134
|
+
costUSD: 0.01,
|
|
1135
|
+
contextWindow: 200000,
|
|
1136
|
+
maxOutputTokens: 8192,
|
|
1137
|
+
},
|
|
1138
|
+
},
|
|
1139
|
+
});
|
|
1140
|
+
|
|
1141
|
+
const results = [...transformSDKMessage(message)];
|
|
1142
|
+
|
|
1143
|
+
expect(results).toEqual([
|
|
1144
|
+
{ type: 'context_window', contextWindow: 200000 },
|
|
1145
|
+
]);
|
|
1146
|
+
});
|
|
1147
|
+
|
|
1148
|
+
it('yields error and context_window for failed result messages', () => {
|
|
1149
|
+
const message = msg({
|
|
1150
|
+
type: 'result',
|
|
1151
|
+
subtype: 'error_max_turns',
|
|
1152
|
+
errors: ['Hit maximum turn limit'],
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1155
|
+
const results = [...transformSDKMessage(message)];
|
|
1156
|
+
|
|
1157
|
+
expect(results).toEqual([
|
|
1158
|
+
{ type: 'error', content: 'Hit maximum turn limit' },
|
|
1159
|
+
{ type: 'context_window', contextWindow: 200000 },
|
|
1160
|
+
]);
|
|
1161
|
+
});
|
|
1162
|
+
|
|
1163
|
+
it('yields context_window with 1M for [1m] models', () => {
|
|
1164
|
+
const message = msg({
|
|
1165
|
+
type: 'result',
|
|
1166
|
+
modelUsage: {
|
|
1167
|
+
'claude-opus-4-6[1m]': {
|
|
1168
|
+
inputTokens: 1000,
|
|
1169
|
+
outputTokens: 300,
|
|
1170
|
+
cacheReadInputTokens: 0,
|
|
1171
|
+
cacheCreationInputTokens: 0,
|
|
1172
|
+
webSearchRequests: 0,
|
|
1173
|
+
costUSD: 0.01,
|
|
1174
|
+
contextWindow: 1000000,
|
|
1175
|
+
maxOutputTokens: 32000,
|
|
1176
|
+
},
|
|
1177
|
+
},
|
|
1178
|
+
});
|
|
1179
|
+
|
|
1180
|
+
const results = [...transformSDKMessage(message)];
|
|
1181
|
+
|
|
1182
|
+
expect(results).toEqual([
|
|
1183
|
+
{ type: 'context_window', contextWindow: 1000000 },
|
|
1184
|
+
]);
|
|
1185
|
+
});
|
|
1186
|
+
|
|
1187
|
+
it('prefers the exact intended model when modelUsage includes multiple entries', () => {
|
|
1188
|
+
const message = msg({
|
|
1189
|
+
type: 'result',
|
|
1190
|
+
modelUsage: {
|
|
1191
|
+
'custom-subagent-model': {
|
|
1192
|
+
inputTokens: 1000,
|
|
1193
|
+
outputTokens: 300,
|
|
1194
|
+
cacheReadInputTokens: 0,
|
|
1195
|
+
cacheCreationInputTokens: 0,
|
|
1196
|
+
webSearchRequests: 0,
|
|
1197
|
+
costUSD: 0.01,
|
|
1198
|
+
contextWindow: 1000000,
|
|
1199
|
+
maxOutputTokens: 32000,
|
|
1200
|
+
},
|
|
1201
|
+
'custom-main-model': {
|
|
1202
|
+
inputTokens: 1000,
|
|
1203
|
+
outputTokens: 300,
|
|
1204
|
+
cacheReadInputTokens: 0,
|
|
1205
|
+
cacheCreationInputTokens: 0,
|
|
1206
|
+
webSearchRequests: 0,
|
|
1207
|
+
costUSD: 0.01,
|
|
1208
|
+
contextWindow: 200000,
|
|
1209
|
+
maxOutputTokens: 32000,
|
|
1210
|
+
},
|
|
1211
|
+
},
|
|
1212
|
+
});
|
|
1213
|
+
|
|
1214
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'custom-main-model' })];
|
|
1215
|
+
|
|
1216
|
+
expect(results).toEqual([
|
|
1217
|
+
{ type: 'context_window', contextWindow: 200000 },
|
|
1218
|
+
]);
|
|
1219
|
+
});
|
|
1220
|
+
|
|
1221
|
+
it('matches built-in aliases against SDK modelUsage keys when unambiguous', () => {
|
|
1222
|
+
const message = msg({
|
|
1223
|
+
type: 'result',
|
|
1224
|
+
modelUsage: {
|
|
1225
|
+
'claude-sonnet-4-5-20250514': {
|
|
1226
|
+
inputTokens: 1000,
|
|
1227
|
+
outputTokens: 300,
|
|
1228
|
+
cacheReadInputTokens: 0,
|
|
1229
|
+
cacheCreationInputTokens: 0,
|
|
1230
|
+
webSearchRequests: 0,
|
|
1231
|
+
costUSD: 0.01,
|
|
1232
|
+
contextWindow: 200000,
|
|
1233
|
+
maxOutputTokens: 32000,
|
|
1234
|
+
},
|
|
1235
|
+
'claude-opus-4-6[1m]': {
|
|
1236
|
+
inputTokens: 1000,
|
|
1237
|
+
outputTokens: 300,
|
|
1238
|
+
cacheReadInputTokens: 0,
|
|
1239
|
+
cacheCreationInputTokens: 0,
|
|
1240
|
+
webSearchRequests: 0,
|
|
1241
|
+
costUSD: 0.01,
|
|
1242
|
+
contextWindow: 1000000,
|
|
1243
|
+
maxOutputTokens: 32000,
|
|
1244
|
+
},
|
|
1245
|
+
},
|
|
1246
|
+
});
|
|
1247
|
+
|
|
1248
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'opus[1m]' })];
|
|
1249
|
+
|
|
1250
|
+
expect(results).toEqual([
|
|
1251
|
+
{ type: 'context_window', contextWindow: 1000000 },
|
|
1252
|
+
]);
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
it('matches provider-qualified custom model ids against SDK modelUsage keys', () => {
|
|
1256
|
+
const message = msg({
|
|
1257
|
+
type: 'result',
|
|
1258
|
+
modelUsage: {
|
|
1259
|
+
'claude-haiku-4-5-20251001': {
|
|
1260
|
+
inputTokens: 1000,
|
|
1261
|
+
outputTokens: 300,
|
|
1262
|
+
cacheReadInputTokens: 0,
|
|
1263
|
+
cacheCreationInputTokens: 0,
|
|
1264
|
+
webSearchRequests: 0,
|
|
1265
|
+
costUSD: 0.01,
|
|
1266
|
+
contextWindow: 200000,
|
|
1267
|
+
maxOutputTokens: 32000,
|
|
1268
|
+
},
|
|
1269
|
+
'claude-opus-4-6[1m]': {
|
|
1270
|
+
inputTokens: 1000,
|
|
1271
|
+
outputTokens: 300,
|
|
1272
|
+
cacheReadInputTokens: 0,
|
|
1273
|
+
cacheCreationInputTokens: 0,
|
|
1274
|
+
webSearchRequests: 0,
|
|
1275
|
+
costUSD: 0.01,
|
|
1276
|
+
contextWindow: 1000000,
|
|
1277
|
+
maxOutputTokens: 32000,
|
|
1278
|
+
},
|
|
1279
|
+
},
|
|
1280
|
+
});
|
|
1281
|
+
|
|
1282
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'anthropic/claude-opus-4-6[1m]' })];
|
|
1283
|
+
|
|
1284
|
+
expect(results).toEqual([
|
|
1285
|
+
{ type: 'context_window', contextWindow: 1000000 },
|
|
1286
|
+
]);
|
|
1287
|
+
});
|
|
1288
|
+
|
|
1289
|
+
it('preserves literal exact matches when provider-qualified entries normalize to the same Claude id', () => {
|
|
1290
|
+
const message = msg({
|
|
1291
|
+
type: 'result',
|
|
1292
|
+
modelUsage: {
|
|
1293
|
+
'eu.anthropic.claude-opus-4-6[1m]': {
|
|
1294
|
+
inputTokens: 1000,
|
|
1295
|
+
outputTokens: 300,
|
|
1296
|
+
cacheReadInputTokens: 0,
|
|
1297
|
+
cacheCreationInputTokens: 0,
|
|
1298
|
+
webSearchRequests: 0,
|
|
1299
|
+
costUSD: 0.01,
|
|
1300
|
+
contextWindow: 1000000,
|
|
1301
|
+
maxOutputTokens: 32000,
|
|
1302
|
+
},
|
|
1303
|
+
'us.anthropic.claude-opus-4-6[1m]': {
|
|
1304
|
+
inputTokens: 1000,
|
|
1305
|
+
outputTokens: 300,
|
|
1306
|
+
cacheReadInputTokens: 0,
|
|
1307
|
+
cacheCreationInputTokens: 0,
|
|
1308
|
+
webSearchRequests: 0,
|
|
1309
|
+
costUSD: 0.01,
|
|
1310
|
+
contextWindow: 500000,
|
|
1311
|
+
maxOutputTokens: 32000,
|
|
1312
|
+
},
|
|
1313
|
+
},
|
|
1314
|
+
});
|
|
1315
|
+
|
|
1316
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'eu.anthropic.claude-opus-4-6[1m]' })];
|
|
1317
|
+
|
|
1318
|
+
expect(results).toEqual([
|
|
1319
|
+
{ type: 'context_window', contextWindow: 1000000 },
|
|
1320
|
+
]);
|
|
1321
|
+
});
|
|
1322
|
+
|
|
1323
|
+
it('matches provider-qualified custom model ids with uppercase 1M suffixes', () => {
|
|
1324
|
+
const message = msg({
|
|
1325
|
+
type: 'result',
|
|
1326
|
+
modelUsage: {
|
|
1327
|
+
'claude-haiku-4-5-20251001': {
|
|
1328
|
+
inputTokens: 1000,
|
|
1329
|
+
outputTokens: 300,
|
|
1330
|
+
cacheReadInputTokens: 0,
|
|
1331
|
+
cacheCreationInputTokens: 0,
|
|
1332
|
+
webSearchRequests: 0,
|
|
1333
|
+
costUSD: 0.01,
|
|
1334
|
+
contextWindow: 200000,
|
|
1335
|
+
maxOutputTokens: 32000,
|
|
1336
|
+
},
|
|
1337
|
+
'claude-opus-4-6[1m]': {
|
|
1338
|
+
inputTokens: 1000,
|
|
1339
|
+
outputTokens: 300,
|
|
1340
|
+
cacheReadInputTokens: 0,
|
|
1341
|
+
cacheCreationInputTokens: 0,
|
|
1342
|
+
webSearchRequests: 0,
|
|
1343
|
+
costUSD: 0.01,
|
|
1344
|
+
contextWindow: 1000000,
|
|
1345
|
+
maxOutputTokens: 32000,
|
|
1346
|
+
},
|
|
1347
|
+
},
|
|
1348
|
+
});
|
|
1349
|
+
|
|
1350
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'anthropic/claude-opus-4-6[1M]' })];
|
|
1351
|
+
|
|
1352
|
+
expect(results).toEqual([
|
|
1353
|
+
{ type: 'context_window', contextWindow: 1000000 },
|
|
1354
|
+
]);
|
|
1355
|
+
});
|
|
1356
|
+
|
|
1357
|
+
it('does not heuristically match different custom model ids', () => {
|
|
1358
|
+
const message = msg({
|
|
1359
|
+
type: 'result',
|
|
1360
|
+
modelUsage: {
|
|
1361
|
+
'claude-haiku-4-5-20251001': {
|
|
1362
|
+
inputTokens: 1000,
|
|
1363
|
+
outputTokens: 300,
|
|
1364
|
+
cacheReadInputTokens: 0,
|
|
1365
|
+
cacheCreationInputTokens: 0,
|
|
1366
|
+
webSearchRequests: 0,
|
|
1367
|
+
costUSD: 0.01,
|
|
1368
|
+
contextWindow: 200000,
|
|
1369
|
+
maxOutputTokens: 32000,
|
|
1370
|
+
},
|
|
1371
|
+
'claude-opus-4-6[1m]': {
|
|
1372
|
+
inputTokens: 1000,
|
|
1373
|
+
outputTokens: 300,
|
|
1374
|
+
cacheReadInputTokens: 0,
|
|
1375
|
+
cacheCreationInputTokens: 0,
|
|
1376
|
+
webSearchRequests: 0,
|
|
1377
|
+
costUSD: 0.01,
|
|
1378
|
+
contextWindow: 1000000,
|
|
1379
|
+
maxOutputTokens: 32000,
|
|
1380
|
+
},
|
|
1381
|
+
},
|
|
1382
|
+
});
|
|
1383
|
+
|
|
1384
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'anthropic/claude-opus-4-6' })];
|
|
1385
|
+
|
|
1386
|
+
expect(results).toEqual([]);
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
it('does not override the heuristic when multi-model result usage is ambiguous', () => {
|
|
1390
|
+
const message = msg({
|
|
1391
|
+
type: 'result',
|
|
1392
|
+
modelUsage: {
|
|
1393
|
+
'claude-sonnet-4-5-20250514': {
|
|
1394
|
+
inputTokens: 1000,
|
|
1395
|
+
outputTokens: 300,
|
|
1396
|
+
cacheReadInputTokens: 0,
|
|
1397
|
+
cacheCreationInputTokens: 0,
|
|
1398
|
+
webSearchRequests: 0,
|
|
1399
|
+
costUSD: 0.01,
|
|
1400
|
+
contextWindow: 200000,
|
|
1401
|
+
maxOutputTokens: 32000,
|
|
1402
|
+
},
|
|
1403
|
+
'claude-sonnet-4-6-20260101': {
|
|
1404
|
+
inputTokens: 1000,
|
|
1405
|
+
outputTokens: 300,
|
|
1406
|
+
cacheReadInputTokens: 0,
|
|
1407
|
+
cacheCreationInputTokens: 0,
|
|
1408
|
+
webSearchRequests: 0,
|
|
1409
|
+
costUSD: 0.01,
|
|
1410
|
+
contextWindow: 500000,
|
|
1411
|
+
maxOutputTokens: 32000,
|
|
1412
|
+
},
|
|
1413
|
+
},
|
|
1414
|
+
});
|
|
1415
|
+
|
|
1416
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'sonnet' })];
|
|
1417
|
+
|
|
1418
|
+
expect(results).toEqual([]);
|
|
1419
|
+
});
|
|
1420
|
+
});
|
|
1421
|
+
|
|
1422
|
+
describe('assistant message usage extraction', () => {
|
|
1423
|
+
it('yields usage info from main agent assistant message', () => {
|
|
1424
|
+
const message = msg({
|
|
1425
|
+
type: 'assistant',
|
|
1426
|
+
parent_tool_use_id: null, // Main agent
|
|
1427
|
+
message: {
|
|
1428
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1429
|
+
usage: {
|
|
1430
|
+
input_tokens: 1000,
|
|
1431
|
+
output_tokens: 500,
|
|
1432
|
+
cache_creation_input_tokens: 300,
|
|
1433
|
+
cache_read_input_tokens: 200,
|
|
1434
|
+
},
|
|
1435
|
+
},
|
|
1436
|
+
});
|
|
1437
|
+
|
|
1438
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'sonnet' })];
|
|
1439
|
+
|
|
1440
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1441
|
+
expect(usageResults).toHaveLength(1);
|
|
1442
|
+
|
|
1443
|
+
const usage = (usageResults[0] as any).usage;
|
|
1444
|
+
expect(usage.inputTokens).toBe(1000);
|
|
1445
|
+
expect(usage.cacheCreationInputTokens).toBe(300);
|
|
1446
|
+
expect(usage.cacheReadInputTokens).toBe(200);
|
|
1447
|
+
expect(usage.contextTokens).toBe(1500); // 1000 + 300 + 200
|
|
1448
|
+
expect(usage.contextWindow).toBe(200000); // Standard context window
|
|
1449
|
+
expect(usage.percentage).toBe(1); // 1500 / 200000 * 100 rounded
|
|
1450
|
+
});
|
|
1451
|
+
|
|
1452
|
+
it('yields usage from assistant message when usage state has no stream prompt usage', () => {
|
|
1453
|
+
const usageState = createTransformUsageState();
|
|
1454
|
+
const message = msg({
|
|
1455
|
+
type: 'assistant',
|
|
1456
|
+
parent_tool_use_id: null,
|
|
1457
|
+
message: {
|
|
1458
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1459
|
+
usage: {
|
|
1460
|
+
input_tokens: 1000,
|
|
1461
|
+
output_tokens: 500,
|
|
1462
|
+
cache_creation_input_tokens: 300,
|
|
1463
|
+
cache_read_input_tokens: 200,
|
|
1464
|
+
},
|
|
1465
|
+
},
|
|
1466
|
+
});
|
|
1467
|
+
|
|
1468
|
+
const results = [...transformSDKMessage(message, {
|
|
1469
|
+
intendedModel: 'sonnet',
|
|
1470
|
+
usageState,
|
|
1471
|
+
})];
|
|
1472
|
+
|
|
1473
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1474
|
+
expect(usageResults).toHaveLength(1);
|
|
1475
|
+
expect((usageResults[0] as any).usage).toEqual({
|
|
1476
|
+
model: 'sonnet',
|
|
1477
|
+
inputTokens: 1000,
|
|
1478
|
+
cacheCreationInputTokens: 300,
|
|
1479
|
+
cacheReadInputTokens: 200,
|
|
1480
|
+
contextWindow: 200000,
|
|
1481
|
+
contextTokens: 1500,
|
|
1482
|
+
percentage: 1,
|
|
1483
|
+
});
|
|
1484
|
+
});
|
|
1485
|
+
|
|
1486
|
+
it('skips usage extraction for subagent messages', () => {
|
|
1487
|
+
const message = msg({
|
|
1488
|
+
type: 'assistant',
|
|
1489
|
+
parent_tool_use_id: 'subagent-task-123', // Subagent
|
|
1490
|
+
message: {
|
|
1491
|
+
content: [{ type: 'text', text: 'Subagent response' }],
|
|
1492
|
+
usage: {
|
|
1493
|
+
input_tokens: 5000,
|
|
1494
|
+
output_tokens: 1000,
|
|
1495
|
+
cache_creation_input_tokens: 500,
|
|
1496
|
+
cache_read_input_tokens: 100,
|
|
1497
|
+
},
|
|
1498
|
+
},
|
|
1499
|
+
});
|
|
1500
|
+
|
|
1501
|
+
const results = [...transformSDKMessage(message)];
|
|
1502
|
+
|
|
1503
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1504
|
+
expect(usageResults).toHaveLength(0);
|
|
1505
|
+
});
|
|
1506
|
+
|
|
1507
|
+
it('uses custom context limits when provided', () => {
|
|
1508
|
+
const message = msg({
|
|
1509
|
+
type: 'assistant',
|
|
1510
|
+
parent_tool_use_id: null,
|
|
1511
|
+
message: {
|
|
1512
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1513
|
+
usage: {
|
|
1514
|
+
input_tokens: 50000,
|
|
1515
|
+
output_tokens: 10000,
|
|
1516
|
+
cache_creation_input_tokens: 0,
|
|
1517
|
+
cache_read_input_tokens: 0,
|
|
1518
|
+
},
|
|
1519
|
+
},
|
|
1520
|
+
});
|
|
1521
|
+
|
|
1522
|
+
const results = [...transformSDKMessage(message, {
|
|
1523
|
+
intendedModel: 'custom-model',
|
|
1524
|
+
customContextLimits: { 'custom-model': 500000 },
|
|
1525
|
+
})];
|
|
1526
|
+
|
|
1527
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1528
|
+
expect(usageResults).toHaveLength(1);
|
|
1529
|
+
|
|
1530
|
+
const usage = (usageResults[0] as any).usage;
|
|
1531
|
+
expect(usage.contextWindow).toBe(500000); // Custom context limit
|
|
1532
|
+
expect(usage.percentage).toBe(10); // 50000 / 500000 * 100 = 10%
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
it('uses custom context limits over standard window', () => {
|
|
1536
|
+
const message = msg({
|
|
1537
|
+
type: 'assistant',
|
|
1538
|
+
parent_tool_use_id: null,
|
|
1539
|
+
message: {
|
|
1540
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1541
|
+
usage: {
|
|
1542
|
+
input_tokens: 100000,
|
|
1543
|
+
output_tokens: 10000,
|
|
1544
|
+
cache_creation_input_tokens: 0,
|
|
1545
|
+
cache_read_input_tokens: 0,
|
|
1546
|
+
},
|
|
1547
|
+
},
|
|
1548
|
+
});
|
|
1549
|
+
|
|
1550
|
+
const results = [...transformSDKMessage(message, {
|
|
1551
|
+
intendedModel: 'sonnet',
|
|
1552
|
+
customContextLimits: { 'sonnet': 256000 },
|
|
1553
|
+
})];
|
|
1554
|
+
|
|
1555
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1556
|
+
expect(usageResults).toHaveLength(1);
|
|
1557
|
+
|
|
1558
|
+
const usage = (usageResults[0] as any).usage;
|
|
1559
|
+
expect(usage.contextWindow).toBe(256000); // Custom limit takes precedence
|
|
1560
|
+
expect(usage.percentage).toBe(39); // 100000 / 256000 * 100 ≈ 39%
|
|
1561
|
+
});
|
|
1562
|
+
|
|
1563
|
+
it('handles missing usage field gracefully', () => {
|
|
1564
|
+
const message = msg({
|
|
1565
|
+
type: 'assistant',
|
|
1566
|
+
parent_tool_use_id: null,
|
|
1567
|
+
message: {
|
|
1568
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1569
|
+
},
|
|
1570
|
+
});
|
|
1571
|
+
|
|
1572
|
+
const results = [...transformSDKMessage(message)];
|
|
1573
|
+
|
|
1574
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1575
|
+
expect(usageResults).toHaveLength(0);
|
|
1576
|
+
});
|
|
1577
|
+
|
|
1578
|
+
it('handles missing token fields with defaults', () => {
|
|
1579
|
+
const message = msg({
|
|
1580
|
+
type: 'assistant',
|
|
1581
|
+
parent_tool_use_id: null,
|
|
1582
|
+
message: {
|
|
1583
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1584
|
+
usage: {}, // Empty usage object
|
|
1585
|
+
},
|
|
1586
|
+
});
|
|
1587
|
+
|
|
1588
|
+
const results = [...transformSDKMessage(message, { intendedModel: 'sonnet' })];
|
|
1589
|
+
|
|
1590
|
+
const usageResults = results.filter(r => r.type === 'usage');
|
|
1591
|
+
expect(usageResults).toHaveLength(1);
|
|
1592
|
+
|
|
1593
|
+
const usage = (usageResults[0] as any).usage;
|
|
1594
|
+
expect(usage.inputTokens).toBe(0);
|
|
1595
|
+
expect(usage.cacheCreationInputTokens).toBe(0);
|
|
1596
|
+
expect(usage.cacheReadInputTokens).toBe(0);
|
|
1597
|
+
expect(usage.contextTokens).toBe(0);
|
|
1598
|
+
});
|
|
1599
|
+
|
|
1600
|
+
it('emits final zero usage with usage state when no stream prompt usage exists', () => {
|
|
1601
|
+
const usageState = createTransformUsageState();
|
|
1602
|
+
const message = msg({
|
|
1603
|
+
type: 'assistant',
|
|
1604
|
+
parent_tool_use_id: null,
|
|
1605
|
+
message: {
|
|
1606
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1607
|
+
usage: {
|
|
1608
|
+
input_tokens: 0,
|
|
1609
|
+
output_tokens: 6,
|
|
1610
|
+
},
|
|
1611
|
+
},
|
|
1612
|
+
});
|
|
1613
|
+
|
|
1614
|
+
const results = [...transformSDKMessage(message, {
|
|
1615
|
+
intendedModel: 'sonnet',
|
|
1616
|
+
usageState,
|
|
1617
|
+
})];
|
|
1618
|
+
|
|
1619
|
+
expect(results).toEqual([
|
|
1620
|
+
{ type: 'text', content: 'Hello' },
|
|
1621
|
+
{
|
|
1622
|
+
type: 'usage',
|
|
1623
|
+
usage: {
|
|
1624
|
+
model: 'sonnet',
|
|
1625
|
+
inputTokens: 0,
|
|
1626
|
+
cacheCreationInputTokens: 0,
|
|
1627
|
+
cacheReadInputTokens: 0,
|
|
1628
|
+
contextWindow: 200000,
|
|
1629
|
+
contextTokens: 0,
|
|
1630
|
+
percentage: 0,
|
|
1631
|
+
},
|
|
1632
|
+
},
|
|
1633
|
+
]);
|
|
1634
|
+
});
|
|
1635
|
+
|
|
1636
|
+
it('does not let final zero assistant usage overwrite positive stream usage', () => {
|
|
1637
|
+
const usageState = createTransformUsageState();
|
|
1638
|
+
const deltaMessage = msg({
|
|
1639
|
+
type: 'stream_event',
|
|
1640
|
+
event: {
|
|
1641
|
+
type: 'message_delta',
|
|
1642
|
+
usage: {
|
|
1643
|
+
input_tokens: 16,
|
|
1644
|
+
output_tokens: 6,
|
|
1645
|
+
},
|
|
1646
|
+
},
|
|
1647
|
+
});
|
|
1648
|
+
const assistantMessage = msg({
|
|
1649
|
+
type: 'assistant',
|
|
1650
|
+
parent_tool_use_id: null,
|
|
1651
|
+
message: {
|
|
1652
|
+
content: [{ type: 'text', text: 'Hello' }],
|
|
1653
|
+
usage: {
|
|
1654
|
+
input_tokens: 0,
|
|
1655
|
+
output_tokens: 6,
|
|
1656
|
+
},
|
|
1657
|
+
},
|
|
1658
|
+
});
|
|
1659
|
+
|
|
1660
|
+
const streamResults = [...transformSDKMessage(deltaMessage, {
|
|
1661
|
+
intendedModel: 'glm-5.1',
|
|
1662
|
+
usageState,
|
|
1663
|
+
})];
|
|
1664
|
+
const assistantResults = [...transformSDKMessage(assistantMessage, {
|
|
1665
|
+
intendedModel: 'glm-5.1',
|
|
1666
|
+
usageState,
|
|
1667
|
+
})];
|
|
1668
|
+
|
|
1669
|
+
expect(streamResults.filter(r => r.type === 'usage')).toHaveLength(1);
|
|
1670
|
+
expect(assistantResults).toEqual([
|
|
1671
|
+
{ type: 'text', content: 'Hello' },
|
|
1672
|
+
]);
|
|
1673
|
+
});
|
|
1674
|
+
});
|
|
1675
|
+
|
|
1676
|
+
describe('error messages', () => {
|
|
1677
|
+
it('yields error event from assistant message with error field', () => {
|
|
1678
|
+
const message = msg({
|
|
1679
|
+
type: 'assistant',
|
|
1680
|
+
error: 'unknown',
|
|
1681
|
+
message: { content: [] },
|
|
1682
|
+
});
|
|
1683
|
+
|
|
1684
|
+
const results = [...transformSDKMessage(message)];
|
|
1685
|
+
|
|
1686
|
+
expect(results).toEqual([
|
|
1687
|
+
{ type: 'error', content: 'unknown' },
|
|
1688
|
+
]);
|
|
1689
|
+
});
|
|
1690
|
+
|
|
1691
|
+
it('yields nothing for assistant message without error field', () => {
|
|
1692
|
+
const message = msg({
|
|
1693
|
+
type: 'assistant',
|
|
1694
|
+
message: { content: [] },
|
|
1695
|
+
});
|
|
1696
|
+
|
|
1697
|
+
const results = [...transformSDKMessage(message)];
|
|
1698
|
+
|
|
1699
|
+
expect(results).toEqual([]);
|
|
1700
|
+
});
|
|
1701
|
+
});
|
|
1702
|
+
|
|
1703
|
+
describe('unhandled message types', () => {
|
|
1704
|
+
it('yields nothing for tool_progress messages', () => {
|
|
1705
|
+
const message = msg({
|
|
1706
|
+
type: 'tool_progress',
|
|
1707
|
+
tool_use_id: 'tool-1',
|
|
1708
|
+
tool_name: 'Bash',
|
|
1709
|
+
elapsed_time_seconds: 5,
|
|
1710
|
+
});
|
|
1711
|
+
|
|
1712
|
+
const results = [...transformSDKMessage(message)];
|
|
1713
|
+
|
|
1714
|
+
expect(results).toEqual([]);
|
|
1715
|
+
});
|
|
1716
|
+
|
|
1717
|
+
it('yields nothing for auth_status messages', () => {
|
|
1718
|
+
const message = msg({
|
|
1719
|
+
type: 'auth_status',
|
|
1720
|
+
isAuthenticating: true,
|
|
1721
|
+
output: [],
|
|
1722
|
+
});
|
|
1723
|
+
|
|
1724
|
+
const results = [...transformSDKMessage(message)];
|
|
1725
|
+
|
|
1726
|
+
expect(results).toEqual([]);
|
|
1727
|
+
});
|
|
1728
|
+
});
|
|
1729
|
+
});
|