@tyvm/knowhow 0.0.145 → 0.0.147
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/autodoc/cli-reference.md +36 -7
- package/bin/knowhow.js +8 -6
- package/package.json +1 -1
- package/src/agents/base/base.ts +24 -0
- package/src/agents/historyRollback.ts +36 -0
- package/src/agents/tools/startAgentTask.ts +41 -9
- package/src/auth/browserLogin.ts +9 -3
- package/src/auth/keyAuth.ts +115 -0
- package/src/auth/keyManager.ts +166 -0
- package/src/chat/CliChatService.ts +16 -0
- package/src/chat/modules/AgentModule.ts +144 -56
- package/src/chat/modules/AskModule.ts +2 -0
- package/src/chat/modules/BaseChatModule.ts +0 -1
- package/src/chat/modules/CustomCommandsModule.ts +2 -0
- package/src/chat/modules/InternalChatModule.ts +2 -0
- package/src/chat/modules/RemoteSyncModule.ts +4 -0
- package/src/chat/modules/RendererModule.ts +2 -0
- package/src/chat/modules/SearchModule.ts +2 -0
- package/src/chat/modules/SessionsModule.ts +110 -39
- package/src/chat/modules/SetupModule.ts +2 -0
- package/src/chat/modules/ShellCommandModule.ts +2 -0
- package/src/chat/modules/SystemModule.ts +2 -0
- package/src/chat/types.ts +5 -2
- package/src/cli.ts +1 -1
- package/src/clients/anthropic.ts +24 -0
- package/src/clients/fireworks.ts +3 -0
- package/src/clients/gemini.ts +8 -0
- package/src/clients/index.ts +2 -0
- package/src/clients/openai.ts +7 -4
- package/src/clients/types.ts +30 -7
- package/src/clients/xai.ts +23 -4
- package/src/commands/agent.ts +11 -39
- package/src/commands/agents.ts +403 -53
- package/src/commands/misc.ts +3 -2
- package/src/commands/services.ts +49 -3
- package/src/login.ts +89 -28
- package/src/plugins/plugins.ts +16 -0
- package/src/processors/TokenCompressor.ts +40 -8
- package/src/processors/ToolResponseCache.ts +17 -8
- package/src/processors/tools/boundedToolResponse.ts +49 -0
- package/src/processors/tools/grepToolResponse.ts +164 -38
- package/src/processors/tools/index.ts +2 -0
- package/src/processors/tools/jqToolResponse.ts +40 -10
- package/src/processors/tools/listStoredToolResponses.ts +51 -10
- package/src/processors/tools/tailToolResponse.ts +85 -17
- package/src/services/AgentService.ts +2 -0
- package/src/services/AgentSyncFs.ts +65 -0
- package/src/services/AgentSyncKnowhowWeb.ts +22 -4
- package/src/services/BehaviorsService.ts +2 -0
- package/src/services/DockerService.ts +2 -0
- package/src/services/EmbeddingService.ts +2 -0
- package/src/services/EmbeddingsService.ts +3 -0
- package/src/services/KnowhowClient.ts +68 -7
- package/src/services/LazyToolsService.ts +16 -0
- package/src/services/Mcp.ts +11 -2
- package/src/services/McpServer.ts +2 -0
- package/src/services/MessageProcessor.ts +2 -0
- package/src/services/MinimalToolsService.ts +2 -0
- package/src/services/RuntimeReloadService.ts +31 -0
- package/src/services/SessionManager.ts +5 -0
- package/src/services/SkillsService.ts +2 -0
- package/src/services/SyncedAgentWatcher.ts +2 -0
- package/src/services/SyncerService.ts +23 -8
- package/src/services/TaskRegistry.ts +2 -0
- package/src/services/Tools.ts +13 -0
- package/src/services/TracingService.ts +116 -0
- package/src/services/index.ts +10 -0
- package/src/services/modules/index.ts +49 -3
- package/src/services/modules/types.ts +8 -0
- package/src/types.ts +2 -0
- package/src/util/Trace.ts +236 -0
- package/src/utils/http.ts +18 -2
- package/tests/clients/fireworks.test.ts +65 -0
- package/tests/clients/reasoningEffort.test.ts +79 -0
- package/tests/compressor/toolResponseCache.test.ts +2 -1
- package/tests/manual/clients/completions.test.ts +27 -10
- package/tests/processors/TokenCompressor.test.ts +17 -3
- package/tests/processors/tools/boundedRetrievalTools.test.ts +68 -0
- package/tests/processors/tools/grepToolResponse.test.ts +88 -1
- package/tests/unit/agents/historyRollback.test.ts +53 -0
- package/tests/unit/auth/keyAuth.test.ts +102 -0
- package/tests/unit/auth/keyManager.test.ts +67 -0
- package/tests/unit/chat/historyCommandArgs.test.ts +37 -0
- package/tests/unit/commands/agents.test.ts +68 -0
- package/tests/unit/login.test.ts +119 -0
- package/tests/unit/plugins/pluginLoading.test.ts +31 -0
- package/tests/unit/services/AgentSyncKnowhowWeb.test.ts +38 -0
- package/tests/unit/services/LazyToolsService.test.ts +29 -0
- package/tests/unit/services/RuntimeReloadService.test.ts +29 -0
- package/tests/unit/services/SessionManager.test.ts +43 -0
- package/tests/unit/services/SyncerService.test.ts +45 -0
- package/tests/unit/util/Trace.test.ts +119 -0
- package/tests/unit/utils/http.test.ts +43 -0
- package/ts_build/package.json +1 -1
- package/ts_build/src/agents/base/base.d.ts +1 -0
- package/ts_build/src/agents/base/base.js +31 -2
- package/ts_build/src/agents/base/base.js.map +1 -1
- package/ts_build/src/agents/historyRollback.d.ts +2 -0
- package/ts_build/src/agents/historyRollback.js +22 -0
- package/ts_build/src/agents/historyRollback.js.map +1 -0
- package/ts_build/src/agents/tools/startAgentTask.d.ts +2 -0
- package/ts_build/src/agents/tools/startAgentTask.js +36 -9
- package/ts_build/src/agents/tools/startAgentTask.js.map +1 -1
- package/ts_build/src/auth/browserLogin.d.ts +1 -0
- package/ts_build/src/auth/browserLogin.js +5 -2
- package/ts_build/src/auth/browserLogin.js.map +1 -1
- package/ts_build/src/auth/keyAuth.d.ts +5 -0
- package/ts_build/src/auth/keyAuth.js +79 -0
- package/ts_build/src/auth/keyAuth.js.map +1 -0
- package/ts_build/src/auth/keyManager.d.ts +14 -0
- package/ts_build/src/auth/keyManager.js +145 -0
- package/ts_build/src/auth/keyManager.js.map +1 -0
- package/ts_build/src/chat/CliChatService.js +15 -0
- package/ts_build/src/chat/CliChatService.js.map +1 -1
- package/ts_build/src/chat/modules/AgentModule.d.ts +13 -1
- package/ts_build/src/chat/modules/AgentModule.js +96 -43
- package/ts_build/src/chat/modules/AgentModule.js.map +1 -1
- package/ts_build/src/chat/modules/AskModule.js +12 -2
- package/ts_build/src/chat/modules/AskModule.js.map +1 -1
- package/ts_build/src/chat/modules/BaseChatModule.js.map +1 -1
- package/ts_build/src/chat/modules/CustomCommandsModule.js +12 -2
- package/ts_build/src/chat/modules/CustomCommandsModule.js.map +1 -1
- package/ts_build/src/chat/modules/InternalChatModule.js +16 -2
- package/ts_build/src/chat/modules/InternalChatModule.js.map +1 -1
- package/ts_build/src/chat/modules/RemoteSyncModule.js +19 -2
- package/ts_build/src/chat/modules/RemoteSyncModule.js.map +1 -1
- package/ts_build/src/chat/modules/RendererModule.js +17 -2
- package/ts_build/src/chat/modules/RendererModule.js.map +1 -1
- package/ts_build/src/chat/modules/SearchModule.js +12 -2
- package/ts_build/src/chat/modules/SearchModule.js.map +1 -1
- package/ts_build/src/chat/modules/SessionsModule.d.ts +6 -0
- package/ts_build/src/chat/modules/SessionsModule.js +114 -24
- package/ts_build/src/chat/modules/SessionsModule.js.map +1 -1
- package/ts_build/src/chat/modules/SetupModule.js +17 -2
- package/ts_build/src/chat/modules/SetupModule.js.map +1 -1
- package/ts_build/src/chat/modules/ShellCommandModule.js +12 -2
- package/ts_build/src/chat/modules/ShellCommandModule.js.map +1 -1
- package/ts_build/src/chat/modules/SystemModule.js +12 -2
- package/ts_build/src/chat/modules/SystemModule.js.map +1 -1
- package/ts_build/src/chat/types.d.ts +5 -2
- package/ts_build/src/cli.js +1 -1
- package/ts_build/src/cli.js.map +1 -1
- package/ts_build/src/clients/anthropic.d.ts +1 -0
- package/ts_build/src/clients/anthropic.js +12 -0
- package/ts_build/src/clients/anthropic.js.map +1 -1
- package/ts_build/src/clients/fireworks.js +1 -0
- package/ts_build/src/clients/fireworks.js.map +1 -1
- package/ts_build/src/clients/gemini.js +8 -0
- package/ts_build/src/clients/gemini.js.map +1 -1
- package/ts_build/src/clients/index.js +19 -4
- package/ts_build/src/clients/index.js.map +1 -1
- package/ts_build/src/clients/openai.js +4 -2
- package/ts_build/src/clients/openai.js.map +1 -1
- package/ts_build/src/clients/types.d.ts +4 -1
- package/ts_build/src/clients/types.js +14 -0
- package/ts_build/src/clients/types.js.map +1 -1
- package/ts_build/src/clients/xai.d.ts +1 -0
- package/ts_build/src/clients/xai.js +23 -4
- package/ts_build/src/clients/xai.js.map +1 -1
- package/ts_build/src/commands/agent.js +7 -22
- package/ts_build/src/commands/agent.js.map +1 -1
- package/ts_build/src/commands/agents.d.ts +1 -1
- package/ts_build/src/commands/agents.js +174 -16
- package/ts_build/src/commands/agents.js.map +1 -1
- package/ts_build/src/commands/misc.js +3 -2
- package/ts_build/src/commands/misc.js.map +1 -1
- package/ts_build/src/commands/services.d.ts +2 -0
- package/ts_build/src/commands/services.js +45 -3
- package/ts_build/src/commands/services.js.map +1 -1
- package/ts_build/src/login.d.ts +1 -1
- package/ts_build/src/login.js +53 -15
- package/ts_build/src/login.js.map +1 -1
- package/ts_build/src/plugins/plugins.d.ts +1 -0
- package/ts_build/src/plugins/plugins.js +14 -0
- package/ts_build/src/plugins/plugins.js.map +1 -1
- package/ts_build/src/processors/TokenCompressor.js +15 -6
- package/ts_build/src/processors/TokenCompressor.js.map +1 -1
- package/ts_build/src/processors/ToolResponseCache.d.ts +3 -3
- package/ts_build/src/processors/ToolResponseCache.js +8 -8
- package/ts_build/src/processors/ToolResponseCache.js.map +1 -1
- package/ts_build/src/processors/tools/boundedToolResponse.d.ts +7 -0
- package/ts_build/src/processors/tools/boundedToolResponse.js +27 -0
- package/ts_build/src/processors/tools/boundedToolResponse.js.map +1 -0
- package/ts_build/src/processors/tools/grepToolResponse.d.ts +4 -0
- package/ts_build/src/processors/tools/grepToolResponse.js +101 -25
- package/ts_build/src/processors/tools/grepToolResponse.js.map +1 -1
- package/ts_build/src/processors/tools/index.d.ts +2 -2
- package/ts_build/src/processors/tools/index.js.map +1 -1
- package/ts_build/src/processors/tools/jqToolResponse.d.ts +5 -1
- package/ts_build/src/processors/tools/jqToolResponse.js +26 -9
- package/ts_build/src/processors/tools/jqToolResponse.js.map +1 -1
- package/ts_build/src/processors/tools/listStoredToolResponses.d.ts +7 -1
- package/ts_build/src/processors/tools/listStoredToolResponses.js +39 -9
- package/ts_build/src/processors/tools/listStoredToolResponses.js.map +1 -1
- package/ts_build/src/processors/tools/tailToolResponse.d.ts +5 -0
- package/ts_build/src/processors/tools/tailToolResponse.js +56 -13
- package/ts_build/src/processors/tools/tailToolResponse.js.map +1 -1
- package/ts_build/src/services/AgentService.js +18 -2
- package/ts_build/src/services/AgentService.js.map +1 -1
- package/ts_build/src/services/AgentSyncFs.d.ts +3 -0
- package/ts_build/src/services/AgentSyncFs.js +62 -15
- package/ts_build/src/services/AgentSyncFs.js.map +1 -1
- package/ts_build/src/services/AgentSyncKnowhowWeb.d.ts +2 -0
- package/ts_build/src/services/AgentSyncKnowhowWeb.js +28 -6
- package/ts_build/src/services/AgentSyncKnowhowWeb.js.map +1 -1
- package/ts_build/src/services/BehaviorsService.js +12 -2
- package/ts_build/src/services/BehaviorsService.js.map +1 -1
- package/ts_build/src/services/DockerService.js +20 -8
- package/ts_build/src/services/DockerService.js.map +1 -1
- package/ts_build/src/services/EmbeddingService.js +16 -2
- package/ts_build/src/services/EmbeddingService.js.map +1 -1
- package/ts_build/src/services/EmbeddingsService.js +12 -2
- package/ts_build/src/services/EmbeddingsService.js.map +1 -1
- package/ts_build/src/services/KnowhowClient.d.ts +7 -2
- package/ts_build/src/services/KnowhowClient.js +108 -10
- package/ts_build/src/services/KnowhowClient.js.map +1 -1
- package/ts_build/src/services/LazyToolsService.d.ts +2 -0
- package/ts_build/src/services/LazyToolsService.js +26 -2
- package/ts_build/src/services/LazyToolsService.js.map +1 -1
- package/ts_build/src/services/Mcp.js +20 -4
- package/ts_build/src/services/Mcp.js.map +1 -1
- package/ts_build/src/services/McpServer.js +17 -2
- package/ts_build/src/services/McpServer.js.map +1 -1
- package/ts_build/src/services/MessageProcessor.js +16 -2
- package/ts_build/src/services/MessageProcessor.js.map +1 -1
- package/ts_build/src/services/MinimalToolsService.js +16 -2
- package/ts_build/src/services/MinimalToolsService.js.map +1 -1
- package/ts_build/src/services/RuntimeReloadService.d.ts +12 -0
- package/ts_build/src/services/RuntimeReloadService.js +26 -0
- package/ts_build/src/services/RuntimeReloadService.js.map +1 -0
- package/ts_build/src/services/SessionManager.js +19 -2
- package/ts_build/src/services/SessionManager.js.map +1 -1
- package/ts_build/src/services/SkillsService.js +16 -2
- package/ts_build/src/services/SkillsService.js.map +1 -1
- package/ts_build/src/services/SyncedAgentWatcher.js +16 -2
- package/ts_build/src/services/SyncedAgentWatcher.js.map +1 -1
- package/ts_build/src/services/SyncerService.d.ts +1 -0
- package/ts_build/src/services/SyncerService.js +27 -5
- package/ts_build/src/services/SyncerService.js.map +1 -1
- package/ts_build/src/services/TaskRegistry.js +12 -2
- package/ts_build/src/services/TaskRegistry.js.map +1 -1
- package/ts_build/src/services/Tools.d.ts +1 -0
- package/ts_build/src/services/Tools.js +21 -2
- package/ts_build/src/services/Tools.js.map +1 -1
- package/ts_build/src/services/TracingService.d.ts +16 -0
- package/ts_build/src/services/TracingService.js +35 -0
- package/ts_build/src/services/TracingService.js.map +1 -0
- package/ts_build/src/services/index.d.ts +8 -0
- package/ts_build/src/services/index.js +9 -1
- package/ts_build/src/services/index.js.map +1 -1
- package/ts_build/src/services/modules/index.d.ts +6 -0
- package/ts_build/src/services/modules/index.js +29 -3
- package/ts_build/src/services/modules/index.js.map +1 -1
- package/ts_build/src/services/modules/types.d.ts +3 -0
- package/ts_build/src/types.d.ts +1 -0
- package/ts_build/src/types.js.map +1 -1
- package/ts_build/src/util/Trace.d.ts +11 -0
- package/ts_build/src/util/Trace.js +107 -0
- package/ts_build/src/util/Trace.js.map +1 -0
- package/ts_build/src/utils/http.d.ts +6 -2
- package/ts_build/src/utils/http.js +8 -2
- package/ts_build/src/utils/http.js.map +1 -1
- package/ts_build/tests/clients/fireworks.test.d.ts +1 -0
- package/ts_build/tests/clients/fireworks.test.js +65 -0
- package/ts_build/tests/clients/fireworks.test.js.map +1 -0
- package/ts_build/tests/clients/reasoningEffort.test.d.ts +1 -0
- package/ts_build/tests/clients/reasoningEffort.test.js +68 -0
- package/ts_build/tests/clients/reasoningEffort.test.js.map +1 -0
- package/ts_build/tests/compressor/toolResponseCache.test.js +1 -1
- package/ts_build/tests/compressor/toolResponseCache.test.js.map +1 -1
- package/ts_build/tests/manual/clients/completions.test.js +19 -6
- package/ts_build/tests/manual/clients/completions.test.js.map +1 -1
- package/ts_build/tests/processors/TokenCompressor.test.js +11 -2
- package/ts_build/tests/processors/TokenCompressor.test.js.map +1 -1
- package/ts_build/tests/processors/tools/boundedRetrievalTools.test.d.ts +1 -0
- package/ts_build/tests/processors/tools/boundedRetrievalTools.test.js +57 -0
- package/ts_build/tests/processors/tools/boundedRetrievalTools.test.js.map +1 -0
- package/ts_build/tests/processors/tools/grepToolResponse.test.js +37 -0
- package/ts_build/tests/processors/tools/grepToolResponse.test.js.map +1 -1
- package/ts_build/tests/unit/agents/historyRollback.test.d.ts +1 -0
- package/ts_build/tests/unit/agents/historyRollback.test.js +42 -0
- package/ts_build/tests/unit/agents/historyRollback.test.js.map +1 -0
- package/ts_build/tests/unit/auth/keyAuth.test.d.ts +1 -0
- package/ts_build/tests/unit/auth/keyAuth.test.js +77 -0
- package/ts_build/tests/unit/auth/keyAuth.test.js.map +1 -0
- package/ts_build/tests/unit/auth/keyManager.test.d.ts +1 -0
- package/ts_build/tests/unit/auth/keyManager.test.js +52 -0
- package/ts_build/tests/unit/auth/keyManager.test.js.map +1 -0
- package/ts_build/tests/unit/chat/historyCommandArgs.test.d.ts +1 -0
- package/ts_build/tests/unit/chat/historyCommandArgs.test.js +29 -0
- package/ts_build/tests/unit/chat/historyCommandArgs.test.js.map +1 -0
- package/ts_build/tests/unit/commands/agents.test.d.ts +1 -0
- package/ts_build/tests/unit/commands/agents.test.js +91 -0
- package/ts_build/tests/unit/commands/agents.test.js.map +1 -0
- package/ts_build/tests/unit/login.test.d.ts +1 -0
- package/ts_build/tests/unit/login.test.js +97 -0
- package/ts_build/tests/unit/login.test.js.map +1 -0
- package/ts_build/tests/unit/plugins/pluginLoading.test.js +59 -0
- package/ts_build/tests/unit/plugins/pluginLoading.test.js.map +1 -1
- package/ts_build/tests/unit/services/AgentSyncKnowhowWeb.test.d.ts +1 -0
- package/ts_build/tests/unit/services/AgentSyncKnowhowWeb.test.js +32 -0
- package/ts_build/tests/unit/services/AgentSyncKnowhowWeb.test.js.map +1 -0
- package/ts_build/tests/unit/services/LazyToolsService.test.d.ts +1 -0
- package/ts_build/tests/unit/services/LazyToolsService.test.js +27 -0
- package/ts_build/tests/unit/services/LazyToolsService.test.js.map +1 -0
- package/ts_build/tests/unit/services/RuntimeReloadService.test.d.ts +1 -0
- package/ts_build/tests/unit/services/RuntimeReloadService.test.js +26 -0
- package/ts_build/tests/unit/services/RuntimeReloadService.test.js.map +1 -0
- package/ts_build/tests/unit/services/SessionManager.test.d.ts +1 -0
- package/ts_build/tests/unit/services/SessionManager.test.js +72 -0
- package/ts_build/tests/unit/services/SessionManager.test.js.map +1 -0
- package/ts_build/tests/unit/services/SyncerService.test.d.ts +1 -0
- package/ts_build/tests/unit/services/SyncerService.test.js +40 -0
- package/ts_build/tests/unit/services/SyncerService.test.js.map +1 -0
- package/ts_build/tests/unit/util/Trace.test.d.ts +1 -0
- package/ts_build/tests/unit/util/Trace.test.js +107 -0
- package/ts_build/tests/unit/util/Trace.test.js.map +1 -0
- package/ts_build/tests/unit/utils/http.test.d.ts +1 -0
- package/ts_build/tests/unit/utils/http.test.js +69 -0
- package/ts_build/tests/unit/utils/http.test.js.map +1 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
|
|
6
|
+
const startChat = jest.fn().mockResolvedValue(undefined);
|
|
7
|
+
const setupServices = jest.fn().mockResolvedValue(undefined);
|
|
8
|
+
|
|
9
|
+
jest.mock("../../../src/chat", () => ({ startChat }));
|
|
10
|
+
jest.mock("../../../src/commands/services", () => ({ setupServices }));
|
|
11
|
+
|
|
12
|
+
import { addAgentsCommand } from "../../../src/commands/agents";
|
|
13
|
+
|
|
14
|
+
describe("agents resume terminal UX", () => {
|
|
15
|
+
let previousCwd: string;
|
|
16
|
+
let tempDir: string;
|
|
17
|
+
let isTTYDescriptor: PropertyDescriptor | undefined;
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
jest.clearAllMocks();
|
|
21
|
+
previousCwd = process.cwd();
|
|
22
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "knowhow-agents-command-"));
|
|
23
|
+
process.chdir(tempDir);
|
|
24
|
+
const taskId = "1700000000-resumable-task";
|
|
25
|
+
fs.mkdirSync(path.join(".knowhow", "processes", "agents", taskId), {
|
|
26
|
+
recursive: true,
|
|
27
|
+
});
|
|
28
|
+
fs.writeFileSync(
|
|
29
|
+
path.join(".knowhow", "processes", "agents", taskId, "metadata.json"),
|
|
30
|
+
JSON.stringify({ taskId, status: "completed", threads: [[]] })
|
|
31
|
+
);
|
|
32
|
+
isTTYDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
|
|
33
|
+
Object.defineProperty(process.stdin, "isTTY", {
|
|
34
|
+
configurable: true,
|
|
35
|
+
value: true,
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
process.chdir(previousCwd);
|
|
41
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
42
|
+
if (isTTYDescriptor) {
|
|
43
|
+
Object.defineProperty(process.stdin, "isTTY", isTTYDescriptor);
|
|
44
|
+
} else {
|
|
45
|
+
delete (process.stdin as NodeJS.ReadStream & { isTTY?: boolean }).isTTY;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("opens chat with the interactive resume command by default", async () => {
|
|
50
|
+
const program = new Command();
|
|
51
|
+
addAgentsCommand(program, jest.fn());
|
|
52
|
+
|
|
53
|
+
await program.parseAsync([
|
|
54
|
+
"node",
|
|
55
|
+
"knowhow",
|
|
56
|
+
"agents",
|
|
57
|
+
"resume",
|
|
58
|
+
"1700000000-resumable-task",
|
|
59
|
+
"--rollback",
|
|
60
|
+
"2",
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
expect(setupServices).toHaveBeenCalledTimes(1);
|
|
64
|
+
expect(startChat).toHaveBeenCalledWith(
|
|
65
|
+
"/resume 1700000000-resumable-task --rollback 2"
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const httpGet = jest.fn();
|
|
2
|
+
const browserLogin = jest.fn();
|
|
3
|
+
const hasKeyPair = jest.fn();
|
|
4
|
+
const authenticateWithKey = jest.fn();
|
|
5
|
+
const registerPublicKey = jest.fn();
|
|
6
|
+
const getOrCreatePublicKey = jest.fn();
|
|
7
|
+
const getConfig = jest.fn();
|
|
8
|
+
const updateConfig = jest.fn();
|
|
9
|
+
|
|
10
|
+
jest.mock("../../src/utils/http", () => ({
|
|
11
|
+
__esModule: true,
|
|
12
|
+
default: { get: httpGet, isHttpError: () => false },
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
jest.mock("../../src/services/KnowhowClient", () => ({
|
|
16
|
+
KNOWHOW_API_URL: "https://api.example",
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
jest.mock("../../src/auth/browserLogin", () => ({
|
|
20
|
+
BrowserLoginService: jest.fn().mockImplementation(() => ({ login: browserLogin })),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
jest.mock("../../src/auth/keyAuth", () => ({
|
|
24
|
+
hasKeyPair,
|
|
25
|
+
authenticateWithKey,
|
|
26
|
+
registerPublicKey,
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
jest.mock("../../src/auth/keyManager", () => ({ getOrCreatePublicKey }));
|
|
30
|
+
jest.mock("../../src/config", () => ({ getConfig, updateConfig }));
|
|
31
|
+
jest.mock("../../src/utils", () => ({ ask: jest.fn() }));
|
|
32
|
+
|
|
33
|
+
import fs from "fs";
|
|
34
|
+
import os from "os";
|
|
35
|
+
import path from "path";
|
|
36
|
+
import { login } from "../../src/login";
|
|
37
|
+
|
|
38
|
+
describe("login public-key bootstrap", () => {
|
|
39
|
+
const originalCwd = process.cwd();
|
|
40
|
+
let workingDirectory: string;
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
jest.clearAllMocks();
|
|
44
|
+
jest.spyOn(console, "log").mockImplementation(() => undefined);
|
|
45
|
+
jest.spyOn(console, "warn").mockImplementation(() => undefined);
|
|
46
|
+
workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "knowhow-login-"));
|
|
47
|
+
fs.mkdirSync(path.join(workingDirectory, ".knowhow"));
|
|
48
|
+
fs.writeFileSync(path.join(workingDirectory, ".knowhow", ".jwt"), "raw-jwt");
|
|
49
|
+
process.chdir(workingDirectory);
|
|
50
|
+
hasKeyPair.mockReturnValue(false);
|
|
51
|
+
browserLogin.mockResolvedValue(undefined);
|
|
52
|
+
getConfig.mockResolvedValue({ modelProviders: [] });
|
|
53
|
+
getOrCreatePublicKey.mockReturnValue({
|
|
54
|
+
publicKeyBase64: "raw-key",
|
|
55
|
+
fingerprint: "SHA256:key",
|
|
56
|
+
});
|
|
57
|
+
httpGet.mockResolvedValue({
|
|
58
|
+
data: {
|
|
59
|
+
orgId: "org-1",
|
|
60
|
+
user: {
|
|
61
|
+
email: "user@example.com",
|
|
62
|
+
orgs: [{ organizationId: "org-1", organization: { id: "org-1", name: "Org" } }],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
process.chdir(originalCwd);
|
|
70
|
+
fs.rmSync(workingDirectory, { recursive: true, force: true });
|
|
71
|
+
jest.restoreAllMocks();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("bootstraps the selected identity after PKCE without invalidating browser login on registration failure", async () => {
|
|
75
|
+
registerPublicKey.mockRejectedValueOnce(new Error("endpoint unavailable"));
|
|
76
|
+
|
|
77
|
+
await expect(login(false, "/keys/custom")).resolves.toBeUndefined();
|
|
78
|
+
|
|
79
|
+
expect(browserLogin).toHaveBeenCalledTimes(1);
|
|
80
|
+
expect(getOrCreatePublicKey).toHaveBeenCalledWith("/keys/custom");
|
|
81
|
+
expect(registerPublicKey).toHaveBeenCalledWith(
|
|
82
|
+
"raw-jwt",
|
|
83
|
+
"raw-key",
|
|
84
|
+
"https://api.example"
|
|
85
|
+
);
|
|
86
|
+
expect(updateConfig).toHaveBeenCalledWith(
|
|
87
|
+
expect.objectContaining({ orgId: "org-1", modelProviders: [{ provider: "knowhow" }] })
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("safely falls back to browser PKCE when the selected key cannot authenticate", async () => {
|
|
92
|
+
hasKeyPair.mockReturnValue(true);
|
|
93
|
+
getConfig.mockResolvedValue({ orgId: "org-1", modelProviders: [] });
|
|
94
|
+
authenticateWithKey.mockRejectedValueOnce(new Error("old backend"));
|
|
95
|
+
registerPublicKey.mockResolvedValueOnce(undefined);
|
|
96
|
+
|
|
97
|
+
await expect(login(false, "/keys/dev")).resolves.toBeUndefined();
|
|
98
|
+
|
|
99
|
+
expect(authenticateWithKey).toHaveBeenCalledWith(
|
|
100
|
+
"org-1",
|
|
101
|
+
"https://api.example",
|
|
102
|
+
"/keys/dev"
|
|
103
|
+
);
|
|
104
|
+
expect(browserLogin).toHaveBeenCalledTimes(1);
|
|
105
|
+
expect(getOrCreatePublicKey).toHaveBeenCalledWith("/keys/dev");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("does not attempt key authentication without a configured organization", async () => {
|
|
109
|
+
hasKeyPair.mockReturnValue(true);
|
|
110
|
+
getConfig.mockResolvedValue({ modelProviders: [] });
|
|
111
|
+
registerPublicKey.mockResolvedValueOnce(undefined);
|
|
112
|
+
|
|
113
|
+
await expect(login(false, "/keys/dev")).resolves.toBeUndefined();
|
|
114
|
+
|
|
115
|
+
expect(hasKeyPair).not.toHaveBeenCalled();
|
|
116
|
+
expect(authenticateWithKey).not.toHaveBeenCalled();
|
|
117
|
+
expect(browserLogin).toHaveBeenCalledTimes(1);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PluginService } from "../../../src/plugins/plugins";
|
|
2
2
|
import { PluginContext } from "../../../src/plugins/types";
|
|
3
|
+
import * as configModule from "../../../src/config";
|
|
3
4
|
|
|
4
5
|
// Minimal mock context for PluginService construction
|
|
5
6
|
// EmbeddingPlugin calls context.Events.on() in its constructor, so we must mock it.
|
|
@@ -63,4 +64,34 @@ describe("PluginService.loadPlugin", () => {
|
|
|
63
64
|
expect(plugin).toBeDefined();
|
|
64
65
|
expect(plugin!.meta.key).toBe(mockKey);
|
|
65
66
|
});
|
|
67
|
+
|
|
68
|
+
it("applies disabled plugins from config and re-enables removed entries", async () => {
|
|
69
|
+
const service = new PluginService(makeContext());
|
|
70
|
+
const plugin = {
|
|
71
|
+
meta: { key: "git", name: "Git Plugin" },
|
|
72
|
+
isEnabled: jest.fn(() => true),
|
|
73
|
+
enable: jest.fn(),
|
|
74
|
+
disable: jest.fn(),
|
|
75
|
+
call: jest.fn(async () => ""),
|
|
76
|
+
callMany: jest.fn(async () => ""),
|
|
77
|
+
};
|
|
78
|
+
service.registerPlugin("git", plugin);
|
|
79
|
+
|
|
80
|
+
const configSpy = jest.spyOn(configModule, "getConfig");
|
|
81
|
+
configSpy.mockResolvedValueOnce({
|
|
82
|
+
plugins: { enabled: [], disabled: ["git"] },
|
|
83
|
+
} as any);
|
|
84
|
+
await service.refreshConfiguredState();
|
|
85
|
+
|
|
86
|
+
expect(plugin.disable).toHaveBeenCalledTimes(1);
|
|
87
|
+
expect(plugin.enable).not.toHaveBeenCalled();
|
|
88
|
+
|
|
89
|
+
configSpy.mockResolvedValueOnce({
|
|
90
|
+
plugins: { enabled: ["git"], disabled: [] },
|
|
91
|
+
} as any);
|
|
92
|
+
await service.refreshConfiguredState();
|
|
93
|
+
|
|
94
|
+
expect(plugin.enable).toHaveBeenCalledTimes(1);
|
|
95
|
+
configSpy.mockRestore();
|
|
96
|
+
});
|
|
66
97
|
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AgentSyncKnowhowWeb } from "../../../src/services/AgentSyncKnowhowWeb";
|
|
2
|
+
|
|
3
|
+
describe("AgentSyncKnowhowWeb sync error reporting", () => {
|
|
4
|
+
let errorSpy: jest.SpyInstance;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
errorSpy = jest.spyOn(console, "error").mockImplementation(() => undefined);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
errorSpy.mockRestore();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("reports only the first remote failure for a task", async () => {
|
|
15
|
+
const sync = new AgentSyncKnowhowWeb();
|
|
16
|
+
(sync as any).client = {
|
|
17
|
+
createChatTask: jest.fn().mockRejectedValue(new Error("offline")),
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
await sync.createChatTask({ messageId: "message-1", prompt: "test" });
|
|
21
|
+
await sync.createChatTask({ messageId: "message-1", prompt: "test" });
|
|
22
|
+
|
|
23
|
+
expect(errorSpy).toHaveBeenCalledTimes(1);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("allows the next task to report its first failure after reset", async () => {
|
|
27
|
+
const sync = new AgentSyncKnowhowWeb();
|
|
28
|
+
(sync as any).client = {
|
|
29
|
+
createChatTask: jest.fn().mockRejectedValue(new Error("offline")),
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
await sync.createChatTask({ messageId: "message-1", prompt: "test" });
|
|
33
|
+
sync.reset();
|
|
34
|
+
await sync.createChatTask({ messageId: "message-2", prompt: "test" });
|
|
35
|
+
|
|
36
|
+
expect(errorSpy).toHaveBeenCalledTimes(2);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { LazyToolsService } from "../../../src/services/LazyToolsService";
|
|
2
|
+
import { Tool } from "../../../src/clients/types";
|
|
3
|
+
|
|
4
|
+
function tool(name: string): Tool {
|
|
5
|
+
return {
|
|
6
|
+
type: "function",
|
|
7
|
+
function: {
|
|
8
|
+
name,
|
|
9
|
+
description: name,
|
|
10
|
+
parameters: { type: "object", properties: {}, required: [] },
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe("LazyToolsService persisted tool restoration", () => {
|
|
16
|
+
test("replaces pattern state with the exact saved tool list", () => {
|
|
17
|
+
const tools = new LazyToolsService();
|
|
18
|
+
tools.addTools([tool("alpha"), tool("beta"), tool("finalAnswer")]);
|
|
19
|
+
tools.enableTools(["*"]);
|
|
20
|
+
tools.disableTools(["alpha"]);
|
|
21
|
+
|
|
22
|
+
tools.restoreEnabledTools(["alpha", "finalAnswer"]);
|
|
23
|
+
|
|
24
|
+
expect(tools.getTools().map((entry) => entry.function.name).sort()).toEqual([
|
|
25
|
+
"alpha",
|
|
26
|
+
"finalAnswer",
|
|
27
|
+
]);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RuntimeReloadService } from "../../../src/services/RuntimeReloadService";
|
|
2
|
+
|
|
3
|
+
describe("RuntimeReloadService", () => {
|
|
4
|
+
it("reports when reload has not been configured", async () => {
|
|
5
|
+
await expect(new RuntimeReloadService().reload()).rejects.toThrow(
|
|
6
|
+
"Runtime reload is unavailable"
|
|
7
|
+
);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("coalesces concurrent reload requests", async () => {
|
|
11
|
+
const service = new RuntimeReloadService();
|
|
12
|
+
let resolve!: (value: { tools: number; mcps: number; modules: number }) => void;
|
|
13
|
+
const result = { tools: 2, mcps: 1, modules: 3 };
|
|
14
|
+
const handler = jest.fn()
|
|
15
|
+
.mockImplementationOnce(() => new Promise<typeof result>((done) => (resolve = done)))
|
|
16
|
+
.mockResolvedValue(result);
|
|
17
|
+
service.configure(handler);
|
|
18
|
+
|
|
19
|
+
const first = service.reload();
|
|
20
|
+
const second = service.reload();
|
|
21
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
22
|
+
expect(second).toBe(first);
|
|
23
|
+
|
|
24
|
+
resolve(result);
|
|
25
|
+
await expect(first).resolves.toEqual(result);
|
|
26
|
+
await service.reload();
|
|
27
|
+
expect(handler).toHaveBeenCalledTimes(2);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
import { SessionManager } from "../../../src/services/SessionManager";
|
|
5
|
+
import { TaskInfo } from "../../../src/chat/types";
|
|
6
|
+
|
|
7
|
+
describe("SessionManager persisted run settings", () => {
|
|
8
|
+
let sessionsDir: string;
|
|
9
|
+
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
sessionsDir = fs.mkdtempSync(path.join(os.tmpdir(), "knowhow-sessions-"));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
fs.rmSync(sessionsDir, { recursive: true, force: true });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("saves and updates the exact enabled tool list", () => {
|
|
19
|
+
const manager = new SessionManager(sessionsDir);
|
|
20
|
+
const taskInfo = {
|
|
21
|
+
taskId: "local-task",
|
|
22
|
+
agentName: "Patcher",
|
|
23
|
+
initialInput: "continue",
|
|
24
|
+
status: "running",
|
|
25
|
+
startTime: Date.now(),
|
|
26
|
+
totalCost: 0,
|
|
27
|
+
enabledTools: ["alpha", "finalAnswer"],
|
|
28
|
+
} as TaskInfo;
|
|
29
|
+
|
|
30
|
+
manager.saveSession(taskInfo.taskId, taskInfo, [[]]);
|
|
31
|
+
expect(manager.loadSession(taskInfo.taskId)?.enabledTools).toEqual([
|
|
32
|
+
"alpha",
|
|
33
|
+
"finalAnswer",
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
taskInfo.enabledTools = ["beta", "finalAnswer"];
|
|
37
|
+
manager.updateSession(taskInfo.taskId, taskInfo, [[]]);
|
|
38
|
+
expect(manager.loadSession(taskInfo.taskId)?.enabledTools).toEqual([
|
|
39
|
+
"beta",
|
|
40
|
+
"finalAnswer",
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AgentSyncFs } from "../../../src/services/AgentSyncFs";
|
|
2
|
+
import { SyncerService } from "../../../src/services/SyncerService";
|
|
3
|
+
|
|
4
|
+
describe("SyncerService remote identity", () => {
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
jest
|
|
7
|
+
.spyOn(AgentSyncFs.prototype, "createTask")
|
|
8
|
+
.mockImplementation(async (options) => options.taskId);
|
|
9
|
+
jest
|
|
10
|
+
.spyOn(AgentSyncFs.prototype, "setRemoteIdentity")
|
|
11
|
+
.mockResolvedValue(undefined);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterEach(() => {
|
|
15
|
+
jest.restoreAllMocks();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("never attaches a local filesystem slug as a remote task", async () => {
|
|
19
|
+
const syncer = new SyncerService();
|
|
20
|
+
await syncer.createTask({
|
|
21
|
+
taskId: "1785905990-local-task-slug",
|
|
22
|
+
prompt: "resume",
|
|
23
|
+
syncRemote: true,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
expect(syncer.getCreatedWebTaskId()).toBeUndefined();
|
|
27
|
+
expect(AgentSyncFs.prototype.setRemoteIdentity).not.toHaveBeenCalled();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("attaches and persists an explicit remote task UUID", async () => {
|
|
31
|
+
const remoteTaskId = "123e4567-e89b-42d3-a456-426614174000";
|
|
32
|
+
const syncer = new SyncerService();
|
|
33
|
+
await syncer.createTask({
|
|
34
|
+
taskId: "1785905990-local-task-slug",
|
|
35
|
+
remoteTaskId,
|
|
36
|
+
prompt: "resume",
|
|
37
|
+
syncRemote: true,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(syncer.getCreatedWebTaskId()).toBe(remoteTaskId);
|
|
41
|
+
expect(AgentSyncFs.prototype.setRemoteIdentity).toHaveBeenCalledWith(
|
|
42
|
+
remoteTaskId
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Trace, TraceAll } from "../../../src/util/Trace";
|
|
2
|
+
import {
|
|
3
|
+
SpanHandle,
|
|
4
|
+
TracerImpl,
|
|
5
|
+
TracingService,
|
|
6
|
+
} from "../../../src/services/TracingService";
|
|
7
|
+
import { ToolsService } from "../../../src/services/Tools";
|
|
8
|
+
|
|
9
|
+
class TestSpan implements SpanHandle {
|
|
10
|
+
ended = false;
|
|
11
|
+
error?: unknown;
|
|
12
|
+
|
|
13
|
+
setAttribute() {}
|
|
14
|
+
|
|
15
|
+
recordError(error: unknown) {
|
|
16
|
+
this.error = error;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
end() {
|
|
20
|
+
this.ended = true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class TestTracer implements TracerImpl {
|
|
25
|
+
spans: Array<{
|
|
26
|
+
name: string;
|
|
27
|
+
attributes?: Record<string, string | number | boolean>;
|
|
28
|
+
span: TestSpan;
|
|
29
|
+
}> = [];
|
|
30
|
+
|
|
31
|
+
startSpan(
|
|
32
|
+
name: string,
|
|
33
|
+
attributes?: Record<string, string | number | boolean>
|
|
34
|
+
): SpanHandle {
|
|
35
|
+
const span = new TestSpan();
|
|
36
|
+
this.spans.push({ name, attributes, span });
|
|
37
|
+
return span;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
withSpan<T>(_span: SpanHandle, fn: () => T): T {
|
|
41
|
+
return fn();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("tracing decorators", () => {
|
|
46
|
+
const tracer = new TestTracer();
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
tracer.spans = [];
|
|
50
|
+
TracingService.register(tracer);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
afterAll(() => TracingService.reset());
|
|
54
|
+
|
|
55
|
+
it("traces and closes synchronous and asynchronous methods", async () => {
|
|
56
|
+
@TraceAll()
|
|
57
|
+
class Subject {
|
|
58
|
+
sync() {
|
|
59
|
+
return "sync";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async asyncMethod() {
|
|
63
|
+
return "async";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const subject = new Subject();
|
|
68
|
+
expect(subject.sync()).toBe("sync");
|
|
69
|
+
expect(await subject.asyncMethod()).toBe("async");
|
|
70
|
+
|
|
71
|
+
expect(tracer.spans.map(({ name }) => name)).toEqual([
|
|
72
|
+
"Subject.sync",
|
|
73
|
+
"Subject.asyncMethod",
|
|
74
|
+
]);
|
|
75
|
+
expect(tracer.spans.every(({ span }) => span.ended)).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("records rejected method errors before rethrowing them", async () => {
|
|
79
|
+
const failure = new Error("failed");
|
|
80
|
+
|
|
81
|
+
class Subject {
|
|
82
|
+
@Trace()
|
|
83
|
+
async fail() {
|
|
84
|
+
throw failure;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await expect(new Subject().fail()).rejects.toBe(failure);
|
|
89
|
+
expect(tracer.spans[0].span.error).toBe(failure);
|
|
90
|
+
expect(tracer.spans[0].span.ended).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("traces the concrete ToolsService.callTool path", async () => {
|
|
94
|
+
const tools = new ToolsService();
|
|
95
|
+
tools.addTool({
|
|
96
|
+
type: "function",
|
|
97
|
+
function: {
|
|
98
|
+
name: "echo",
|
|
99
|
+
description: "Echo a value",
|
|
100
|
+
parameters: {
|
|
101
|
+
type: "object",
|
|
102
|
+
properties: { value: { type: "string" } },
|
|
103
|
+
required: ["value"],
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
tools.setFunction("echo", ({ value }: { value: string }) => value);
|
|
108
|
+
|
|
109
|
+
const result = await tools.callTool({
|
|
110
|
+
id: "trace-test",
|
|
111
|
+
type: "function",
|
|
112
|
+
function: { name: "echo", arguments: JSON.stringify({ value: "ok" }) },
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(result.functionResp).toBe("ok");
|
|
116
|
+
expect(tracer.spans.some(({ name }) => name === "ToolsService.callTool"))
|
|
117
|
+
.toBe(true);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import http, { HTTP_UNAUTHORIZED_HANDLER, RefreshableHeaders } from "../../../src/utils/http";
|
|
2
|
+
|
|
3
|
+
describe("HTTP unauthorized renewal", () => {
|
|
4
|
+
const originalFetch = global.fetch;
|
|
5
|
+
|
|
6
|
+
afterEach(() => {
|
|
7
|
+
global.fetch = originalFetch;
|
|
8
|
+
jest.restoreAllMocks();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it("renews and retries a 401 once with the updated headers", async () => {
|
|
12
|
+
const headers: RefreshableHeaders = { Authorization: "Bearer old" };
|
|
13
|
+
const renew = jest.fn(async () => {
|
|
14
|
+
headers.Authorization = "Bearer new";
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(headers, HTTP_UNAUTHORIZED_HANDLER, { value: renew });
|
|
17
|
+
global.fetch = jest
|
|
18
|
+
.fn()
|
|
19
|
+
.mockResolvedValueOnce(new Response("unauthorized", { status: 401 }))
|
|
20
|
+
.mockResolvedValueOnce(new Response(JSON.stringify({ ok: true }), { status: 200 }));
|
|
21
|
+
|
|
22
|
+
await expect(http.get("https://example.test", { headers })).resolves.toMatchObject({
|
|
23
|
+
data: { ok: true },
|
|
24
|
+
});
|
|
25
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
26
|
+
expect(global.fetch).toHaveBeenNthCalledWith(
|
|
27
|
+
2,
|
|
28
|
+
"https://example.test",
|
|
29
|
+
expect.objectContaining({ headers: { Authorization: "Bearer new" } })
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("does not renew or retry a second 401", async () => {
|
|
34
|
+
const headers: RefreshableHeaders = { Authorization: "Bearer old" };
|
|
35
|
+
const renew = jest.fn(async () => undefined);
|
|
36
|
+
Object.defineProperty(headers, HTTP_UNAUTHORIZED_HANDLER, { value: renew });
|
|
37
|
+
global.fetch = jest.fn().mockResolvedValue(new Response("unauthorized", { status: 401 }));
|
|
38
|
+
|
|
39
|
+
await expect(http.get("https://example.test", { headers })).rejects.toMatchObject({ status: 401 });
|
|
40
|
+
expect(renew).toHaveBeenCalledTimes(1);
|
|
41
|
+
expect(global.fetch).toHaveBeenCalledTimes(2);
|
|
42
|
+
});
|
|
43
|
+
});
|
package/ts_build/package.json
CHANGED
|
@@ -137,6 +137,7 @@ export declare abstract class BaseAgent implements IAgent {
|
|
|
137
137
|
protected isTerminationResponse(content: string): boolean;
|
|
138
138
|
getEnabledTools(): Tool[];
|
|
139
139
|
getEnabledToolNames(): string[];
|
|
140
|
+
restoreEnabledTools(names: string[]): void;
|
|
140
141
|
disableTool(toolName: string): void;
|
|
141
142
|
isToolEnabled(toolName: string): boolean;
|
|
142
143
|
enableTool(toolName: string): void;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.BaseAgent = void 0;
|
|
4
13
|
const utils_1 = require("../../utils");
|
|
@@ -7,7 +16,8 @@ const clients_1 = require("../../clients");
|
|
|
7
16
|
const ai_1 = require("../../ai");
|
|
8
17
|
const MessageProcessor_1 = require("../../services/MessageProcessor");
|
|
9
18
|
const contextLimits_1 = require("../../clients/contextLimits");
|
|
10
|
-
|
|
19
|
+
const Trace_1 = require("../../util/Trace");
|
|
20
|
+
let BaseAgent = class BaseAgent {
|
|
11
21
|
status = "not_started";
|
|
12
22
|
lastHealthCheckTime = 0;
|
|
13
23
|
provider = "openai";
|
|
@@ -273,6 +283,21 @@ class BaseAgent {
|
|
|
273
283
|
getEnabledToolNames() {
|
|
274
284
|
return this.getEnabledTools().map((t) => t.function.name);
|
|
275
285
|
}
|
|
286
|
+
restoreEnabledTools(names) {
|
|
287
|
+
const uniqueNames = [...new Set(names)];
|
|
288
|
+
const lazyTools = this.tools;
|
|
289
|
+
if (lazyTools.restoreEnabledTools) {
|
|
290
|
+
lazyTools.restoreEnabledTools(uniqueNames);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
lazyTools.enableTools?.(uniqueNames);
|
|
294
|
+
}
|
|
295
|
+
const savedNames = new Set(uniqueNames);
|
|
296
|
+
this.disabledTools = this.tools
|
|
297
|
+
.getTools()
|
|
298
|
+
.map((tool) => tool.function.name)
|
|
299
|
+
.filter((name) => !savedNames.has(name));
|
|
300
|
+
}
|
|
276
301
|
disableTool(toolName) {
|
|
277
302
|
this.disabledTools.push(toolName);
|
|
278
303
|
}
|
|
@@ -1088,6 +1113,10 @@ class BaseAgent {
|
|
|
1088
1113
|
this.log(`Compressed messages from ${oldLength} to ${newLength}, ${compressionRatio}% reduction in size`);
|
|
1089
1114
|
return newMessages;
|
|
1090
1115
|
}
|
|
1091
|
-
}
|
|
1116
|
+
};
|
|
1092
1117
|
exports.BaseAgent = BaseAgent;
|
|
1118
|
+
exports.BaseAgent = BaseAgent = __decorate([
|
|
1119
|
+
(0, Trace_1.TraceAll)(),
|
|
1120
|
+
__metadata("design:paramtypes", [Object])
|
|
1121
|
+
], BaseAgent);
|
|
1093
1122
|
//# sourceMappingURL=base.js.map
|