@geminixiang/mikan 1.5.1 → 1.6.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/dist/adapters/commands/login.js +1 -1
- package/dist/adapters/commands/login.js.map +1 -1
- package/dist/adapters/commands/manifest.js +1 -1
- package/dist/adapters/commands/manifest.js.map +1 -1
- package/dist/adapters/commands/model.d.ts.map +1 -1
- package/dist/adapters/commands/model.js.map +1 -1
- package/dist/adapters/commands/types.d.ts +3 -2
- package/dist/adapters/commands/types.d.ts.map +1 -1
- package/dist/adapters/commands/types.js.map +1 -1
- package/dist/adapters/discord/bot.d.ts +2 -3
- package/dist/adapters/discord/bot.d.ts.map +1 -1
- package/dist/adapters/discord/bot.js +8 -20
- package/dist/adapters/discord/bot.js.map +1 -1
- package/dist/adapters/discord/context.d.ts.map +1 -1
- package/dist/adapters/discord/context.js +7 -23
- package/dist/adapters/discord/context.js.map +1 -1
- package/dist/adapters/github/bot.d.ts +1 -2
- package/dist/adapters/github/bot.d.ts.map +1 -1
- package/dist/adapters/github/bot.js +5 -13
- package/dist/adapters/github/bot.js.map +1 -1
- package/dist/adapters/github/context.d.ts.map +1 -1
- package/dist/adapters/github/context.js +6 -22
- package/dist/adapters/github/context.js.map +1 -1
- package/dist/adapters/github/tools/checks.d.ts +2 -2
- package/dist/adapters/github/tools/checks.d.ts.map +1 -1
- package/dist/adapters/github/tools/checks.js +3 -3
- package/dist/adapters/github/tools/checks.js.map +1 -1
- package/dist/adapters/github/tools/issue.d.ts +2 -2
- package/dist/adapters/github/tools/issue.d.ts.map +1 -1
- package/dist/adapters/github/tools/issue.js +1 -1
- package/dist/adapters/github/tools/issue.js.map +1 -1
- package/dist/adapters/github/tools/pr.d.ts +2 -2
- package/dist/adapters/github/tools/pr.d.ts.map +1 -1
- package/dist/adapters/github/tools/pr.js +1 -1
- package/dist/adapters/github/tools/pr.js.map +1 -1
- package/dist/adapters/github/tools/read.d.ts +2 -2
- package/dist/adapters/github/tools/read.d.ts.map +1 -1
- package/dist/adapters/github/tools/read.js +1 -1
- package/dist/adapters/github/tools/read.js.map +1 -1
- package/dist/adapters/github/tools/review-reply.d.ts +2 -2
- package/dist/adapters/github/tools/review-reply.d.ts.map +1 -1
- package/dist/adapters/github/tools/review-reply.js +1 -1
- package/dist/adapters/github/tools/review-reply.js.map +1 -1
- package/dist/adapters/progressive-renderer.d.ts +1 -2
- package/dist/adapters/progressive-renderer.d.ts.map +1 -1
- package/dist/adapters/progressive-renderer.js +0 -24
- package/dist/adapters/progressive-renderer.js.map +1 -1
- package/dist/adapters/shared.d.ts +12 -2
- package/dist/adapters/shared.d.ts.map +1 -1
- package/dist/adapters/shared.js +48 -1
- package/dist/adapters/shared.js.map +1 -1
- package/dist/adapters/slack/bot.d.ts +4 -5
- package/dist/adapters/slack/bot.d.ts.map +1 -1
- package/dist/adapters/slack/bot.js +63 -87
- package/dist/adapters/slack/bot.js.map +1 -1
- package/dist/adapters/slack/response-lifecycle.d.ts.map +1 -1
- package/dist/adapters/slack/response-lifecycle.js +0 -28
- package/dist/adapters/slack/response-lifecycle.js.map +1 -1
- package/dist/adapters/slack/tools/blockkit.d.ts +2 -2
- package/dist/adapters/slack/tools/blockkit.d.ts.map +1 -1
- package/dist/adapters/slack/tools/blockkit.js +3 -4
- package/dist/adapters/slack/tools/blockkit.js.map +1 -1
- package/dist/adapters/slack/types.d.ts +10 -0
- package/dist/adapters/slack/types.d.ts.map +1 -1
- package/dist/adapters/slack/types.js.map +1 -1
- package/dist/adapters/slack/update-diagnostics.d.ts.map +1 -1
- package/dist/adapters/slack/update-diagnostics.js +8 -2
- package/dist/adapters/slack/update-diagnostics.js.map +1 -1
- package/dist/adapters/telegram/bot.d.ts +2 -3
- package/dist/adapters/telegram/bot.d.ts.map +1 -1
- package/dist/adapters/telegram/bot.js +8 -15
- package/dist/adapters/telegram/bot.js.map +1 -1
- package/dist/adapters/telegram/context.d.ts.map +1 -1
- package/dist/adapters/telegram/context.js +3 -19
- package/dist/adapters/telegram/context.js.map +1 -1
- package/dist/adapters/types.d.ts +8 -3
- package/dist/adapters/types.d.ts.map +1 -1
- package/dist/adapters/types.js.map +1 -1
- package/dist/adapters/web/admin/portal.d.ts +3 -8
- package/dist/adapters/web/admin/portal.d.ts.map +1 -1
- package/dist/adapters/web/admin/portal.js +35 -46
- package/dist/adapters/web/admin/portal.js.map +1 -1
- package/dist/adapters/web/desktop/index.d.ts +1 -1
- package/dist/adapters/web/desktop/index.d.ts.map +1 -1
- package/dist/adapters/web/desktop/index.js +0 -22
- package/dist/adapters/web/desktop/index.js.map +1 -1
- package/dist/adapters/web/desktop/page.d.ts.map +1 -1
- package/dist/adapters/web/desktop/page.js +0 -34
- package/dist/adapters/web/desktop/page.js.map +1 -1
- package/dist/adapters/web/login/portal.d.ts.map +1 -1
- package/dist/adapters/web/login/portal.js +54 -85
- package/dist/adapters/web/login/portal.js.map +1 -1
- package/dist/adapters/web/portal-shell.d.ts +3 -0
- package/dist/adapters/web/portal-shell.d.ts.map +1 -1
- package/dist/adapters/web/portal-shell.js +26 -0
- package/dist/adapters/web/portal-shell.js.map +1 -1
- package/dist/adapters/web/session-view/portal.d.ts.map +1 -1
- package/dist/adapters/web/session-view/portal.js +15 -23
- package/dist/adapters/web/session-view/portal.js.map +1 -1
- package/dist/adapters/web/types.d.ts +8 -0
- package/dist/adapters/web/types.d.ts.map +1 -1
- package/dist/adapters/web/types.js.map +1 -1
- package/dist/env-manifest.d.ts +1 -1
- package/dist/env-manifest.d.ts.map +1 -1
- package/dist/env-manifest.js +3 -6
- package/dist/env-manifest.js.map +1 -1
- package/dist/events/index.d.ts +3 -0
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +12 -0
- package/dist/events/index.js.map +1 -1
- package/dist/events/scheduler.d.ts +1 -1
- package/dist/events/scheduler.d.ts.map +1 -1
- package/dist/events/scheduler.js +14 -21
- package/dist/events/scheduler.js.map +1 -1
- package/dist/harness/execution-resolver.d.ts.map +1 -1
- package/dist/harness/execution-resolver.js +2 -8
- package/dist/harness/execution-resolver.js.map +1 -1
- package/dist/harness/jev.d.ts +2 -0
- package/dist/harness/jev.d.ts.map +1 -1
- package/dist/harness/jev.js +1 -0
- package/dist/harness/jev.js.map +1 -1
- package/dist/harness/mcp-result.d.ts +3 -3
- package/dist/harness/mcp-result.d.ts.map +1 -1
- package/dist/harness/mcp-result.js +13 -30
- package/dist/harness/mcp-result.js.map +1 -1
- package/dist/harness/mcp.d.ts.map +1 -1
- package/dist/harness/mcp.js +10 -23
- package/dist/harness/mcp.js.map +1 -1
- package/dist/harness/presenter.d.ts +0 -1
- package/dist/harness/presenter.d.ts.map +1 -1
- package/dist/harness/presenter.js +22 -151
- package/dist/harness/presenter.js.map +1 -1
- package/dist/harness/prompt.d.ts.map +1 -1
- package/dist/harness/prompt.js +9 -7
- package/dist/harness/prompt.js.map +1 -1
- package/dist/harness/run-events.d.ts +1 -0
- package/dist/harness/run-events.d.ts.map +1 -1
- package/dist/harness/run-events.js +8 -19
- package/dist/harness/run-events.js.map +1 -1
- package/dist/harness/runner.d.ts.map +1 -1
- package/dist/harness/runner.js +21 -131
- package/dist/harness/runner.js.map +1 -1
- package/dist/harness/session.d.ts +19 -22
- package/dist/harness/session.d.ts.map +1 -1
- package/dist/harness/session.js +285 -282
- package/dist/harness/session.js.map +1 -1
- package/dist/harness/skills.d.ts +0 -1
- package/dist/harness/skills.d.ts.map +1 -1
- package/dist/harness/skills.js +11 -16
- package/dist/harness/skills.js.map +1 -1
- package/dist/harness/tools/attach.d.ts +2 -2
- package/dist/harness/tools/attach.d.ts.map +1 -1
- package/dist/harness/tools/attach.js +2 -3
- package/dist/harness/tools/attach.js.map +1 -1
- package/dist/harness/tools/event.d.ts +2 -2
- package/dist/harness/tools/event.d.ts.map +1 -1
- package/dist/harness/tools/event.js +2 -3
- package/dist/harness/tools/event.js.map +1 -1
- package/dist/harness/tools/generate-image.d.ts +2 -2
- package/dist/harness/tools/generate-image.d.ts.map +1 -1
- package/dist/harness/tools/generate-image.js +2 -3
- package/dist/harness/tools/generate-image.js.map +1 -1
- package/dist/harness/tools/history.d.ts +2 -2
- package/dist/harness/tools/history.d.ts.map +1 -1
- package/dist/harness/tools/history.js +2 -3
- package/dist/harness/tools/history.js.map +1 -1
- package/dist/harness/tools/host-fn-tool.d.ts +8 -3
- package/dist/harness/tools/host-fn-tool.d.ts.map +1 -1
- package/dist/harness/tools/host-fn-tool.js +23 -15
- package/dist/harness/tools/host-fn-tool.js.map +1 -1
- package/dist/harness/tools/index.d.ts +4 -5
- package/dist/harness/tools/index.d.ts.map +1 -1
- package/dist/harness/tools/index.js +10 -21
- package/dist/harness/tools/index.js.map +1 -1
- package/dist/harness/tools/jev.d.ts +2 -2
- package/dist/harness/tools/jev.d.ts.map +1 -1
- package/dist/harness/tools/jev.js +3 -3
- package/dist/harness/tools/jev.js.map +1 -1
- package/dist/harness/tools/pi-tools.d.ts +3 -7
- package/dist/harness/tools/pi-tools.d.ts.map +1 -1
- package/dist/harness/tools/pi-tools.js +23 -60
- package/dist/harness/tools/pi-tools.js.map +1 -1
- package/dist/harness/tools/react.d.ts +2 -2
- package/dist/harness/tools/react.d.ts.map +1 -1
- package/dist/harness/tools/react.js +1 -1
- package/dist/harness/tools/react.js.map +1 -1
- package/dist/harness/tools/sandbox.d.ts +2 -2
- package/dist/harness/tools/sandbox.d.ts.map +1 -1
- package/dist/harness/tools/sandbox.js +2 -3
- package/dist/harness/tools/sandbox.js.map +1 -1
- package/dist/harness/tools/secret-redaction.d.ts +2 -2
- package/dist/harness/tools/secret-redaction.d.ts.map +1 -1
- package/dist/harness/tools/secret-redaction.js +24 -9
- package/dist/harness/tools/secret-redaction.js.map +1 -1
- package/dist/harness/tools/task.d.ts +6 -6
- package/dist/harness/tools/task.d.ts.map +1 -1
- package/dist/harness/tools/task.js +7 -8
- package/dist/harness/tools/task.js.map +1 -1
- package/dist/harness/tools/types.d.ts +2 -6
- package/dist/harness/tools/types.d.ts.map +1 -1
- package/dist/harness/tools/types.js.map +1 -1
- package/dist/harness/tools/web-fetch.d.ts +2 -2
- package/dist/harness/tools/web-fetch.d.ts.map +1 -1
- package/dist/harness/tools/web-fetch.js +2 -2
- package/dist/harness/tools/web-fetch.js.map +1 -1
- package/dist/harness/types.d.ts +13 -146
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/harness/types.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/main.js +3 -9
- package/dist/main.js.map +1 -1
- package/dist/observability/index.d.ts +2 -4
- package/dist/observability/index.d.ts.map +1 -1
- package/dist/observability/index.js +3 -80
- package/dist/observability/index.js.map +1 -1
- package/dist/observability/types.d.ts +8 -16
- package/dist/observability/types.d.ts.map +1 -1
- package/dist/observability/types.js.map +1 -1
- package/dist/office/index.d.ts.map +1 -1
- package/dist/office/index.js +0 -1
- package/dist/office/index.js.map +1 -1
- package/dist/office/types.d.ts +0 -1
- package/dist/office/types.d.ts.map +1 -1
- package/dist/office/types.js.map +1 -1
- package/dist/runtime/conversation-runtime.d.ts.map +1 -1
- package/dist/runtime/conversation-runtime.js +7 -29
- package/dist/runtime/conversation-runtime.js.map +1 -1
- package/dist/runtime/session-lifecycle.d.ts +1 -3
- package/dist/runtime/session-lifecycle.d.ts.map +1 -1
- package/dist/runtime/session-lifecycle.js +5 -7
- package/dist/runtime/session-lifecycle.js.map +1 -1
- package/dist/runtime/types.d.ts +0 -2
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js.map +1 -1
- package/dist/sandbox/desktop.d.ts +1 -11
- package/dist/sandbox/desktop.d.ts.map +1 -1
- package/dist/sandbox/desktop.js +1 -47
- package/dist/sandbox/desktop.js.map +1 -1
- package/dist/sandbox/provisioner.d.ts +1 -6
- package/dist/sandbox/provisioner.d.ts.map +1 -1
- package/dist/sandbox/provisioner.js +64 -72
- package/dist/sandbox/provisioner.js.map +1 -1
- package/dist/sandbox/types.d.ts +0 -5
- package/dist/sandbox/types.d.ts.map +1 -1
- package/dist/sandbox/types.js.map +1 -1
- package/dist/sandbox/utils.d.ts +0 -1
- package/dist/sandbox/utils.d.ts.map +1 -1
- package/dist/sandbox/utils.js +0 -10
- package/dist/sandbox/utils.js.map +1 -1
- package/dist/sessions/chat-history-sync.d.ts +1 -2
- package/dist/sessions/chat-history-sync.d.ts.map +1 -1
- package/dist/sessions/chat-history-sync.js +1 -14
- package/dist/sessions/chat-history-sync.js.map +1 -1
- package/dist/sessions/compaction-summary.d.ts +2 -2
- package/dist/sessions/compaction-summary.d.ts.map +1 -1
- package/dist/sessions/compaction-summary.js.map +1 -1
- package/dist/sessions/session-key.d.ts +6 -0
- package/dist/sessions/session-key.d.ts.map +1 -1
- package/dist/sessions/session-key.js +7 -0
- package/dist/sessions/session-key.js.map +1 -1
- package/dist/sessions/session-store.d.ts +5 -9
- package/dist/sessions/session-store.d.ts.map +1 -1
- package/dist/sessions/session-store.js +45 -86
- package/dist/sessions/session-store.js.map +1 -1
- package/dist/sessions/types.d.ts +4 -8
- package/dist/sessions/types.d.ts.map +1 -1
- package/dist/sessions/types.js.map +1 -1
- package/dist/settings/index.d.ts +4 -4
- package/dist/settings/index.d.ts.map +1 -1
- package/dist/settings/index.js +13 -14
- package/dist/settings/index.js.map +1 -1
- package/dist/types.d.ts +3 -40
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/unknown-values.d.ts +2 -0
- package/dist/unknown-values.d.ts.map +1 -1
- package/dist/unknown-values.js +6 -0
- package/dist/unknown-values.js.map +1 -1
- package/dist/vault/index.d.ts +0 -1
- package/dist/vault/index.d.ts.map +1 -1
- package/dist/vault/index.js +0 -15
- package/dist/vault/index.js.map +1 -1
- package/dist/vault/types.d.ts +0 -1
- package/dist/vault/types.d.ts.map +1 -1
- package/dist/vault/types.js.map +1 -1
- package/package.json +7 -8
- package/dist/adapters/slack/persona.d.ts +0 -6
- package/dist/adapters/slack/persona.d.ts.map +0 -1
- package/dist/adapters/slack/persona.js +0 -13
- package/dist/adapters/slack/persona.js.map +0 -1
- package/dist/harness/loop-guard.d.ts +0 -10
- package/dist/harness/loop-guard.d.ts.map +0 -1
- package/dist/harness/loop-guard.js +0 -111
- package/dist/harness/loop-guard.js.map +0 -1
- package/dist/harness/subagent-profiles.d.ts +0 -3
- package/dist/harness/subagent-profiles.d.ts.map +0 -1
- package/dist/harness/subagent-profiles.js +0 -260
- package/dist/harness/subagent-profiles.js.map +0 -1
- package/dist/harness/subagent.d.ts +0 -39
- package/dist/harness/subagent.d.ts.map +0 -1
- package/dist/harness/subagent.js +0 -613
- package/dist/harness/subagent.js.map +0 -1
- package/dist/harness/tools/codemode.d.ts +0 -5
- package/dist/harness/tools/codemode.d.ts.map +0 -1
- package/dist/harness/tools/codemode.js +0 -253
- package/dist/harness/tools/codemode.js.map +0 -1
- package/dist/harness/tools/jev-browser.d.ts +0 -53
- package/dist/harness/tools/jev-browser.d.ts.map +0 -1
- package/dist/harness/tools/jev-browser.js +0 -730
- package/dist/harness/tools/jev-browser.js.map +0 -1
- package/dist/harness/tools/subagent.d.ts +0 -51
- package/dist/harness/tools/subagent.d.ts.map +0 -1
- package/dist/harness/tools/subagent.js +0 -581
- package/dist/harness/tools/subagent.js.map +0 -1
- package/dist/harness/tools/tool-search.d.ts +0 -6
- package/dist/harness/tools/tool-search.d.ts.map +0 -1
- package/dist/harness/tools/tool-search.js +0 -146
- package/dist/harness/tools/tool-search.js.map +0 -1
- package/dist/memory-capture/index.d.ts +0 -15
- package/dist/memory-capture/index.d.ts.map +0 -1
- package/dist/memory-capture/index.js +0 -198
- package/dist/memory-capture/index.js.map +0 -1
- package/dist/memory-capture/types.d.ts +0 -27
- package/dist/memory-capture/types.d.ts.map +0 -1
- package/dist/memory-capture/types.js +0 -2
- package/dist/memory-capture/types.js.map +0 -1
package/dist/harness/session.js
CHANGED
|
@@ -1,24 +1,45 @@
|
|
|
1
1
|
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
2
2
|
import { contentText, createAssistantMessageEventStream } from "@earendil-works/pi-ai";
|
|
3
3
|
import { estimateMessageTokens } from "@earendil-works/pi-ai/utils/estimate";
|
|
4
|
-
import {
|
|
4
|
+
import { DEFAULT_COMPACTION_POLICY, CompactionTask, GenerationTask, LiveDoc, ToolTask, defineExtension, hook, section, wrapTool, } from "@earendil-works/pi-durable";
|
|
5
5
|
import * as log from "../log.js";
|
|
6
|
-
import {
|
|
7
|
-
import { adaptAgentTool, isHarnessTool } from "./tools/pi-tools.js";
|
|
6
|
+
import { withRequiredLabel } from "./tools/pi-tools.js";
|
|
8
7
|
import { START_TASK_TOOL, TASK_STATUS_TOOL } from "./tools/task.js";
|
|
9
|
-
import { createCodemodeTool, isCodemodeCallable, withScriptCallNote } from "./tools/codemode.js";
|
|
10
8
|
import { renderMcpServersSection } from "./mcp.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
9
|
+
import { CodemodeSandbox } from "@earendil-works/pi-codemode";
|
|
10
|
+
import { createCodemodeExtension } from "@earendil-works/pi-durable/codemode";
|
|
11
|
+
import { createToolSearchExtension } from "@earendil-works/pi-durable/tool-search";
|
|
13
12
|
import { errorMessage } from "../unknown-values.js";
|
|
14
13
|
const context = BACKGROUND_CONTEXT;
|
|
15
14
|
const COMPACTION_REQUEST = Symbol("compaction-request");
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const CODEMODE_SANDBOX = new CodemodeSandbox({ memoryLimitBytes: 64 * 1024 * 1024 });
|
|
16
|
+
const TOOL_SEARCH_EXTENSION = createToolSearchExtension();
|
|
17
|
+
const ENTRY_PAGE_SIZE = 200;
|
|
18
|
+
function followFrom(view) {
|
|
19
|
+
const calls = new Map();
|
|
20
|
+
for (const entry of view.entries) {
|
|
21
|
+
const message = entry.model?.[0];
|
|
22
|
+
if (message?.role !== "assistant")
|
|
23
|
+
continue;
|
|
24
|
+
for (const part of message.content) {
|
|
25
|
+
if (part.type === "toolCall")
|
|
26
|
+
calls.set(part.id, { name: part.name, args: part.arguments });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
lastEntryId: view.entries.at(-1)?.id ?? 0,
|
|
31
|
+
streaming: false,
|
|
32
|
+
calls,
|
|
33
|
+
startedTools: new Set(),
|
|
34
|
+
endedTools: new Set(),
|
|
35
|
+
toolDetails: new Map(),
|
|
36
|
+
retryAt: undefined,
|
|
37
|
+
compactions: new Map(),
|
|
38
|
+
usage: view.docs["pi.usage"],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function liveState(view) {
|
|
42
|
+
return (view.docs["pi.live"] ?? {});
|
|
22
43
|
}
|
|
23
44
|
export class MikanAgentSession {
|
|
24
45
|
constructor(options) {
|
|
@@ -30,13 +51,8 @@ export class MikanAgentSession {
|
|
|
30
51
|
this.retryAttempt = 0;
|
|
31
52
|
this.runMessages = [];
|
|
32
53
|
this.streamingContent = [];
|
|
33
|
-
this.
|
|
34
|
-
this.loopGuard = new ToolLoopGuard();
|
|
35
|
-
this.loopNotices = new Map();
|
|
36
|
-
this.runStarted = false;
|
|
37
|
-
this.runEndSeen = false;
|
|
54
|
+
this.viewQueue = Promise.resolve();
|
|
38
55
|
this.observedUsage = createEmptyUsage();
|
|
39
|
-
this.runEnded = deferred();
|
|
40
56
|
this.tally = {
|
|
41
57
|
usage: createEmptyUsage(),
|
|
42
58
|
llmCalls: 0,
|
|
@@ -46,7 +62,7 @@ export class MikanAgentSession {
|
|
|
46
62
|
};
|
|
47
63
|
this.sessionStore = options.sessionStore;
|
|
48
64
|
this.model = options.model;
|
|
49
|
-
this.systemPrompt = options.systemPrompt;
|
|
65
|
+
this.systemPrompt = options.systemPrompt ?? "";
|
|
50
66
|
this.settings = resolveHarnessSettings(options.settings);
|
|
51
67
|
}
|
|
52
68
|
get lastRunMessages() {
|
|
@@ -73,17 +89,6 @@ export class MikanAgentSession {
|
|
|
73
89
|
status: this.lastSessionRunStatus,
|
|
74
90
|
};
|
|
75
91
|
}
|
|
76
|
-
captureExternalUsageSink() {
|
|
77
|
-
const tally = this.tally;
|
|
78
|
-
return async (usage) => {
|
|
79
|
-
addUsage(tally.usage, usage);
|
|
80
|
-
if (this.tally !== tally || !this.runActive || this.budgetExceededReason)
|
|
81
|
-
return;
|
|
82
|
-
const reason = this.resourceOverBudgetReason();
|
|
83
|
-
if (reason)
|
|
84
|
-
await this.exceedBudget(reason);
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
92
|
subscribe(listener) {
|
|
88
93
|
this.listeners.add(listener);
|
|
89
94
|
return () => this.listeners.delete(listener);
|
|
@@ -129,13 +134,10 @@ export class MikanAgentSession {
|
|
|
129
134
|
this.cancellationError = undefined;
|
|
130
135
|
this.retryAttempt = 0;
|
|
131
136
|
this.runMessages = [];
|
|
132
|
-
this.
|
|
133
|
-
this.
|
|
134
|
-
this.runEnded = deferred();
|
|
137
|
+
this.followed = undefined;
|
|
138
|
+
this.viewQueue = Promise.resolve();
|
|
135
139
|
this.activeRequest = undefined;
|
|
136
140
|
this.requestRunId = undefined;
|
|
137
|
-
this.loopGuard = new ToolLoopGuard();
|
|
138
|
-
this.loopNotices.clear();
|
|
139
141
|
this.runBudget = { ...this.settings.budget, ...options?.budget };
|
|
140
142
|
this.lastSessionRunStatus = undefined;
|
|
141
143
|
this.tally = {
|
|
@@ -146,7 +148,7 @@ export class MikanAgentSession {
|
|
|
146
148
|
startedAt: Date.now(),
|
|
147
149
|
};
|
|
148
150
|
let runFailure;
|
|
149
|
-
let
|
|
151
|
+
let watch;
|
|
150
152
|
let status = "completed";
|
|
151
153
|
try {
|
|
152
154
|
if (!(await this.checkCallBudget()))
|
|
@@ -160,15 +162,20 @@ export class MikanAgentSession {
|
|
|
160
162
|
const attached = await this.attach();
|
|
161
163
|
if (this.runAborted)
|
|
162
164
|
return;
|
|
163
|
-
const tools =
|
|
165
|
+
const tools = (options?.tools ?? this.options.tools).filter((tool) => (tool.name !== START_TASK_TOOL || options?.allowTaskHandoff === true) &&
|
|
164
166
|
(tool.name !== TASK_STATUS_TOOL || options?.allowTaskStatus === true));
|
|
165
167
|
await this.installRunTools(attached, tools);
|
|
166
168
|
if (!(await this.checkCallBudget()))
|
|
167
169
|
return;
|
|
168
170
|
await this.sessionStore.recordRun({ startedAt: Date.now() });
|
|
169
|
-
|
|
170
|
-
this.
|
|
171
|
-
|
|
171
|
+
watch = await attached.conversation.watch(context);
|
|
172
|
+
this.followed = followFrom(watch.value);
|
|
173
|
+
this.observedUsage = sumUsageState(usageState(watch.value));
|
|
174
|
+
await this.followView(watch.value);
|
|
175
|
+
watch.start(async (call) => {
|
|
176
|
+
if (call.kind === "value")
|
|
177
|
+
await this.followView(call.value);
|
|
178
|
+
});
|
|
172
179
|
status = options?.continueInterrupted
|
|
173
180
|
? await this.continueRun()
|
|
174
181
|
: await this.drive(attached, text, options?.images);
|
|
@@ -181,13 +188,13 @@ export class MikanAgentSession {
|
|
|
181
188
|
finally {
|
|
182
189
|
if (status !== "failed" && this.runAborted)
|
|
183
190
|
status = "aborted";
|
|
184
|
-
await this.cleanupRun(runFailure,
|
|
191
|
+
await this.cleanupRun(runFailure, watch, status);
|
|
185
192
|
}
|
|
186
193
|
}
|
|
187
194
|
async continueRun() {
|
|
188
|
-
|
|
189
|
-
if (this.
|
|
190
|
-
await this.
|
|
195
|
+
const continued = await this.sessionStore.continueInterruptedRun();
|
|
196
|
+
if (continued && this.attached)
|
|
197
|
+
await this.followRunToEnd(this.attached.conversation);
|
|
191
198
|
return this.runAborted ? "aborted" : "completed";
|
|
192
199
|
}
|
|
193
200
|
async drive({ harness, conversation }, text, images) {
|
|
@@ -202,8 +209,7 @@ export class MikanAgentSession {
|
|
|
202
209
|
if (this.runAborted)
|
|
203
210
|
this.requestCancellation();
|
|
204
211
|
const settled = await submission.wait(context);
|
|
205
|
-
|
|
206
|
-
await this.runEnded.promise;
|
|
212
|
+
await this.followRunToEnd(conversation);
|
|
207
213
|
if (settled.status === "done")
|
|
208
214
|
return "completed";
|
|
209
215
|
if (this.runAborted || settled.reason === "aborted")
|
|
@@ -212,14 +218,14 @@ export class MikanAgentSession {
|
|
|
212
218
|
return "failed";
|
|
213
219
|
throw new Error(`Pi run failed: ${settled.reason}`);
|
|
214
220
|
}
|
|
215
|
-
async cleanupRun(runFailure,
|
|
221
|
+
async cleanupRun(runFailure, watch, status) {
|
|
216
222
|
clearTimeout(this.deadlineTimer);
|
|
217
223
|
this.deadlineTimer = undefined;
|
|
218
224
|
let cleanupFailure;
|
|
219
225
|
try {
|
|
220
226
|
await this.cancellation;
|
|
221
227
|
await this.deadlineNotification;
|
|
222
|
-
await
|
|
228
|
+
await watch?.stop();
|
|
223
229
|
if (this.attached) {
|
|
224
230
|
await this.sessionStore.recordRun({ endedAt: Date.now(), status });
|
|
225
231
|
}
|
|
@@ -230,8 +236,6 @@ export class MikanAgentSession {
|
|
|
230
236
|
cleanupFailure = { error };
|
|
231
237
|
}
|
|
232
238
|
finally {
|
|
233
|
-
this.toolArgs.clear();
|
|
234
|
-
this.loopNotices.clear();
|
|
235
239
|
this.cancellation = undefined;
|
|
236
240
|
this.deadlineNotification = undefined;
|
|
237
241
|
this.tally.endedAt = Date.now();
|
|
@@ -306,6 +310,9 @@ export class MikanAgentSession {
|
|
|
306
310
|
get: (target, property) => {
|
|
307
311
|
if (property === "streamSimple") {
|
|
308
312
|
return (...[model, request, options]) => {
|
|
313
|
+
if (options?.sessionId !== this.attached?.providerSessionId) {
|
|
314
|
+
return target.streamSimple(model, request, options);
|
|
315
|
+
}
|
|
309
316
|
const over = this.runActive ? this.callOverBudgetReason() : undefined;
|
|
310
317
|
if (over || this.runAborted)
|
|
311
318
|
return abortedStream(model, over ?? "Operation aborted");
|
|
@@ -319,73 +326,62 @@ export class MikanAgentSession {
|
|
|
319
326
|
},
|
|
320
327
|
});
|
|
321
328
|
}
|
|
322
|
-
|
|
323
|
-
return tools.map((tool) => (isHarnessTool(tool) ? tool : adaptAgentTool(tool)));
|
|
324
|
-
}
|
|
325
|
-
async installRunTools(attached, selected) {
|
|
326
|
-
const tools = [...selected];
|
|
327
|
-
const deferredTools = tools.filter((tool) => tool.exposure === "deferred");
|
|
329
|
+
async installRunTools(attached, registered) {
|
|
328
330
|
const servers = this.sessionStore.mcpServers();
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
load: async (names) => {
|
|
344
|
-
for (const name of names)
|
|
345
|
-
if (authorized.has(name))
|
|
346
|
-
loaded.add(name);
|
|
347
|
-
},
|
|
348
|
-
})));
|
|
331
|
+
const { conversation, registry, extensionName } = attached;
|
|
332
|
+
const own = (id) => id === conversation.id;
|
|
333
|
+
const added = [];
|
|
334
|
+
if (registered.length > 0) {
|
|
335
|
+
added.push(createCodemodeExtension({
|
|
336
|
+
sandbox: CODEMODE_SANDBOX,
|
|
337
|
+
namespaces: servers.map((server) => ({
|
|
338
|
+
name: `mcp__${server.name}`,
|
|
339
|
+
description: server.description,
|
|
340
|
+
instructions: server.instructions,
|
|
341
|
+
prefix: `mcp__${server.name}__`,
|
|
342
|
+
})),
|
|
343
|
+
models: false,
|
|
344
|
+
}));
|
|
349
345
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
346
|
+
if (registered.some((tool) => tool.deferred === true))
|
|
347
|
+
added.push(TOOL_SEARCH_EXTENSION);
|
|
348
|
+
const addedTools = added.flatMap((extension) => extension.tools ?? []);
|
|
349
|
+
for (const extension of added)
|
|
350
|
+
registry.install(extension);
|
|
354
351
|
registry.install(defineExtension({
|
|
355
352
|
name: extensionName,
|
|
356
353
|
tools: registered,
|
|
357
354
|
sections: [
|
|
358
|
-
section("mikan", () => this.systemPrompt
|
|
359
|
-
|
|
360
|
-
],
|
|
361
|
-
hooks: [
|
|
362
|
-
hook(GenerationTask, {
|
|
363
|
-
beforeRequest: (_request, api) => this.beforeRequest(String(api.taskId)),
|
|
364
|
-
}),
|
|
365
|
-
hook(CompactionTask, {
|
|
366
|
-
beforeCompact: (_compaction, api) => this.beforeCompact(String(api.taskId)),
|
|
367
|
-
}),
|
|
368
|
-
hook(ToolTask, {
|
|
369
|
-
beforeTool: async (call, api) => this.beforeTool(call, api),
|
|
370
|
-
afterTool: (call, result) => {
|
|
371
|
-
const notice = this.takeLoopNotice(call.id);
|
|
372
|
-
return notice === undefined
|
|
373
|
-
? undefined
|
|
374
|
-
: { ...result, output: withNotice(result.output, notice) };
|
|
375
|
-
},
|
|
355
|
+
section("mikan", (input) => (own(input.conversationId) ? this.systemPrompt : undefined), {
|
|
356
|
+
tag: false,
|
|
376
357
|
}),
|
|
358
|
+
section("mcp_servers", (input) => own(input.conversationId) ? renderMcpServersSection(servers) || undefined : undefined),
|
|
377
359
|
],
|
|
360
|
+
hooks: this.extensionHooks(own),
|
|
361
|
+
wraps: addedTools.map((tool) => wrapTool(tool, withRequiredLabel)),
|
|
378
362
|
}));
|
|
379
363
|
await conversation.configure({
|
|
380
364
|
model: { provider: this.model.provider, modelId: this.model.id },
|
|
381
365
|
thinkingLevel: this.options.thinkingLevel,
|
|
382
|
-
extensions: [registry.snapshot().extension(extensionName)],
|
|
383
|
-
tools: registered
|
|
384
|
-
const tool = tools[index];
|
|
385
|
-
return tool.exposure === undefined || loaded.has(tool.name);
|
|
386
|
-
}),
|
|
366
|
+
extensions: [registry.snapshot().extension(extensionName), ...added],
|
|
367
|
+
tools: [...registered, ...addedTools],
|
|
387
368
|
}, context);
|
|
388
369
|
}
|
|
370
|
+
extensionHooks(own) {
|
|
371
|
+
return [
|
|
372
|
+
hook(GenerationTask, {
|
|
373
|
+
beforeRequest: async (_request, api) => own(api.conversationId) ? this.beforeRequest(String(api.taskId)) : undefined,
|
|
374
|
+
}),
|
|
375
|
+
hook(CompactionTask, {
|
|
376
|
+
beforeCompact: (_compaction, api) => own(api.conversationId)
|
|
377
|
+
? this.beforeCompact(String(api.taskId))
|
|
378
|
+
: { decline: true },
|
|
379
|
+
}),
|
|
380
|
+
hook(ToolTask, {
|
|
381
|
+
beforeTool: async (_call, api) => own(api.conversationId) ? this.beforeTool(api) : undefined,
|
|
382
|
+
}),
|
|
383
|
+
];
|
|
384
|
+
}
|
|
389
385
|
beforeCompact(taskId) {
|
|
390
386
|
if (!this.runActive)
|
|
391
387
|
return undefined;
|
|
@@ -402,60 +398,12 @@ export class MikanAgentSession {
|
|
|
402
398
|
this.requestRunId = runId;
|
|
403
399
|
return undefined;
|
|
404
400
|
}
|
|
405
|
-
async beforeTool(
|
|
401
|
+
async beforeTool(api) {
|
|
406
402
|
const round = (await api.snapshot(LiveDoc, api.conversationId, context))?.tools ?? [];
|
|
407
403
|
if (round.length > 1 && round.some((slot) => slot.name === START_TASK_TOOL)) {
|
|
408
404
|
return { block: `Call ${START_TASK_TOOL} alone, without other tools in the same batch.` };
|
|
409
405
|
}
|
|
410
|
-
return
|
|
411
|
-
}
|
|
412
|
-
takeLoopNotice(callId) {
|
|
413
|
-
const notice = this.loopNotices.get(callId);
|
|
414
|
-
this.loopNotices.delete(callId);
|
|
415
|
-
return notice;
|
|
416
|
-
}
|
|
417
|
-
async executeNestedTool(tool, call) {
|
|
418
|
-
const [args, api, toolContext] = call;
|
|
419
|
-
const id = api.callId;
|
|
420
|
-
if (this.runAborted || toolContext.abortSignal?.aborted)
|
|
421
|
-
throw new Error("Operation aborted");
|
|
422
|
-
await this.emitToolStart(id, tool.name, args);
|
|
423
|
-
let result;
|
|
424
|
-
try {
|
|
425
|
-
const verdict = await this.checkToolLoop(id, tool.name, args);
|
|
426
|
-
if (verdict?.block)
|
|
427
|
-
throw new Error(verdict.block);
|
|
428
|
-
toolContext.abortSignal?.throwIfAborted();
|
|
429
|
-
result = await tool.execute(args, api, toolContext);
|
|
430
|
-
const notice = this.takeLoopNotice(id);
|
|
431
|
-
if (notice !== undefined)
|
|
432
|
-
result = { ...result, content: withNotice(result.content, notice) };
|
|
433
|
-
}
|
|
434
|
-
catch (error) {
|
|
435
|
-
result = { content: [{ type: "text", text: errorMessage(error) }], isError: true };
|
|
436
|
-
}
|
|
437
|
-
finally {
|
|
438
|
-
this.loopNotices.delete(id);
|
|
439
|
-
}
|
|
440
|
-
await this.emitToolEnd(id, tool.name, { content: result.content ?? [], details: result.details }, result.isError === true);
|
|
441
|
-
return result;
|
|
442
|
-
}
|
|
443
|
-
async checkToolLoop(toolCallId, toolName, args) {
|
|
444
|
-
const verdict = this.loopGuard.observe(toolName, args);
|
|
445
|
-
switch (verdict.kind) {
|
|
446
|
-
case "allow":
|
|
447
|
-
return undefined;
|
|
448
|
-
case "notice":
|
|
449
|
-
this.loopNotices.set(toolCallId, verdict.text);
|
|
450
|
-
return undefined;
|
|
451
|
-
case "block":
|
|
452
|
-
return { block: verdict.reason };
|
|
453
|
-
case "stop":
|
|
454
|
-
await this.exceedBudget(verdict.reason);
|
|
455
|
-
return { block: verdict.reason };
|
|
456
|
-
default:
|
|
457
|
-
return verdict;
|
|
458
|
-
}
|
|
406
|
+
return undefined;
|
|
459
407
|
}
|
|
460
408
|
async emit(event) {
|
|
461
409
|
for (const listener of this.listeners) {
|
|
@@ -467,100 +415,200 @@ export class MikanAgentSession {
|
|
|
467
415
|
}
|
|
468
416
|
}
|
|
469
417
|
}
|
|
470
|
-
|
|
471
|
-
|
|
418
|
+
followView(view) {
|
|
419
|
+
this.viewQueue = this.viewQueue.then(async () => {
|
|
472
420
|
try {
|
|
473
|
-
await this.
|
|
421
|
+
await this.applyView(view);
|
|
474
422
|
}
|
|
475
423
|
catch (error) {
|
|
476
|
-
log.logWarning("
|
|
424
|
+
log.logWarning("Conversation view handling failed", errorMessage(error));
|
|
477
425
|
}
|
|
426
|
+
});
|
|
427
|
+
return this.viewQueue;
|
|
428
|
+
}
|
|
429
|
+
async followRunToEnd(conversation) {
|
|
430
|
+
for (;;) {
|
|
431
|
+
const watch = await conversation.watch(context);
|
|
432
|
+
const view = watch.value;
|
|
433
|
+
await watch.stop();
|
|
434
|
+
await this.followView(view);
|
|
435
|
+
if (liveState(view).run === undefined)
|
|
436
|
+
break;
|
|
437
|
+
await conversation.waitForIdle(context);
|
|
478
438
|
}
|
|
479
|
-
|
|
480
|
-
this.runEnded.resolve();
|
|
439
|
+
await this.endRetry(this.runAborted ? "Retry cancelled" : undefined);
|
|
481
440
|
}
|
|
482
|
-
async
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
441
|
+
async applyView(view) {
|
|
442
|
+
const followed = this.followed;
|
|
443
|
+
if (!followed)
|
|
444
|
+
return;
|
|
445
|
+
for (const entry of await this.entriesSince(followed, view)) {
|
|
446
|
+
followed.lastEntryId = entry.id;
|
|
447
|
+
await this.applyEntry(followed, entry);
|
|
448
|
+
}
|
|
449
|
+
const live = liveState(view);
|
|
450
|
+
await this.applyGeneration(followed, live);
|
|
451
|
+
await this.applyTools(followed, live);
|
|
452
|
+
await this.applyCompactions(followed, live);
|
|
453
|
+
const usage = view.docs["pi.usage"];
|
|
454
|
+
if (usage !== undefined && usage !== followed.usage) {
|
|
455
|
+
followed.usage = usage;
|
|
456
|
+
await this.recordUsage(usageState(view));
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async entriesSince(followed, view) {
|
|
460
|
+
if (view.before === undefined || view.before <= followed.lastEntryId) {
|
|
461
|
+
return view.entries.filter((entry) => entry.id > followed.lastEntryId);
|
|
462
|
+
}
|
|
463
|
+
const conversation = this.attached?.conversation;
|
|
464
|
+
if (!conversation)
|
|
465
|
+
return view.entries;
|
|
466
|
+
const missed = [];
|
|
467
|
+
let page = await conversation.entries({ minEntryId: (followed.lastEntryId + 1), order: "ascending" }, ENTRY_PAGE_SIZE, undefined, context);
|
|
468
|
+
missed.push(...page.items);
|
|
469
|
+
while (page.next) {
|
|
470
|
+
page = await conversation.entries({}, ENTRY_PAGE_SIZE, page.next, context);
|
|
471
|
+
missed.push(...page.items);
|
|
472
|
+
}
|
|
473
|
+
return missed;
|
|
474
|
+
}
|
|
475
|
+
async applyEntry(followed, entry) {
|
|
476
|
+
const message = entry.model?.[0];
|
|
477
|
+
if (!message || message.role === "system")
|
|
478
|
+
return;
|
|
479
|
+
if (message.role === "toolResult") {
|
|
480
|
+
await this.startTool(followed, message.toolCallId, message.toolName);
|
|
481
|
+
await this.endTool(followed, message.toolCallId, message.toolName, {
|
|
482
|
+
content: message.content,
|
|
483
|
+
details: message.details,
|
|
484
|
+
isError: message.isError,
|
|
485
|
+
durationMs: message.durationMs,
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
if (message.role !== "assistant" || !followed.streaming) {
|
|
489
|
+
this.streamingContent = [];
|
|
490
|
+
await this.emit({ type: "message_start", message });
|
|
491
|
+
}
|
|
492
|
+
if (message.role === "assistant") {
|
|
493
|
+
followed.streaming = false;
|
|
494
|
+
for (const part of message.content) {
|
|
495
|
+
if (part.type === "toolCall")
|
|
496
|
+
followed.calls.set(part.id, { name: part.name, args: part.arguments });
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
await this.endMessage(message);
|
|
500
|
+
}
|
|
501
|
+
async applyGeneration(followed, live) {
|
|
502
|
+
const message = live.generation?.message;
|
|
503
|
+
if (message) {
|
|
504
|
+
if (!followed.streaming) {
|
|
505
|
+
followed.streaming = true;
|
|
496
506
|
this.streamingContent = [];
|
|
497
|
-
await this.emit({ type: "message_start", message
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
507
|
+
await this.emit({ type: "message_start", message });
|
|
508
|
+
}
|
|
509
|
+
await this.streamContent(message.content);
|
|
510
|
+
}
|
|
511
|
+
const retry = live.generation?.retry;
|
|
512
|
+
if (retry && retry.at !== followed.retryAt) {
|
|
513
|
+
followed.retryAt = retry.at;
|
|
514
|
+
this.retryAttempt = live.generation?.attempt ?? 1;
|
|
515
|
+
await this.emit({
|
|
516
|
+
type: "auto_retry_start",
|
|
517
|
+
attempt: this.retryAttempt,
|
|
518
|
+
maxAttempts: this.settings.retry.maxRetries,
|
|
519
|
+
delayMs: Math.max(0, retry.at - Date.now()),
|
|
520
|
+
errorMessage: retry.error,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
if (!retry && followed.retryAt !== undefined) {
|
|
524
|
+
followed.retryAt = undefined;
|
|
525
|
+
const attempt = this.retryAttempt;
|
|
526
|
+
this.retryAttempt = 0;
|
|
527
|
+
await this.emit({
|
|
528
|
+
type: "auto_retry_end",
|
|
529
|
+
attempt,
|
|
530
|
+
success: !this.runAborted,
|
|
531
|
+
finalError: this.runAborted ? "Retry cancelled" : undefined,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
async applyTools(followed, live) {
|
|
536
|
+
for (const slot of [...(live.tools ?? []), ...(live.nestedTools ?? [])]) {
|
|
537
|
+
if (slot.status === "pending")
|
|
538
|
+
continue;
|
|
539
|
+
const nested = "arguments" in slot ? slot : undefined;
|
|
540
|
+
if (nested)
|
|
541
|
+
followed.calls.set(slot.callId, { name: slot.name, args: nested.arguments });
|
|
542
|
+
await this.startTool(followed, slot.callId, slot.name);
|
|
543
|
+
if (slot.details !== undefined && followed.toolDetails.get(slot.callId) !== slot.details) {
|
|
544
|
+
followed.toolDetails.set(slot.callId, slot.details);
|
|
510
545
|
await this.emit({
|
|
511
546
|
type: "tool_execution_update",
|
|
512
|
-
toolCallId:
|
|
513
|
-
toolName:
|
|
514
|
-
details:
|
|
547
|
+
toolCallId: slot.callId,
|
|
548
|
+
toolName: slot.name,
|
|
549
|
+
details: slot.details,
|
|
515
550
|
});
|
|
516
|
-
return;
|
|
517
|
-
case "tool_execution_end": {
|
|
518
|
-
const message = event.entry?.model?.[0];
|
|
519
|
-
const toolResult = message?.role === "toolResult" ? message : undefined;
|
|
520
|
-
await this.emitToolEnd(event.toolCallId, event.toolName, { content: toolResult?.content ?? [], details: toolResult?.details }, toolResult?.isError ?? true);
|
|
521
|
-
return;
|
|
522
551
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
});
|
|
532
|
-
return;
|
|
533
|
-
case "auto_retry_end":
|
|
534
|
-
this.retryAttempt = 0;
|
|
535
|
-
await this.emit({
|
|
536
|
-
type: "auto_retry_end",
|
|
537
|
-
attempt: event.attempt,
|
|
538
|
-
success: !this.runAborted,
|
|
539
|
-
finalError: this.runAborted ? "Retry cancelled" : undefined,
|
|
552
|
+
if (slot.status !== "done")
|
|
553
|
+
continue;
|
|
554
|
+
if (nested) {
|
|
555
|
+
await this.endTool(followed, slot.callId, slot.name, {
|
|
556
|
+
content: [],
|
|
557
|
+
details: slot.details,
|
|
558
|
+
isError: nested.summary?.isError ?? true,
|
|
559
|
+
durationMs: nested.summary?.durationMs,
|
|
540
560
|
});
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
await this.
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
const outcome = await this.compactionOutcome(event.taskId);
|
|
549
|
-
await this.emit({
|
|
550
|
-
type: "compaction_end",
|
|
551
|
-
reason: event.reason,
|
|
552
|
-
aborted: outcome?.status === "aborted",
|
|
553
|
-
errorMessage: outcome?.status === "failed" ? outcome.error.message : undefined,
|
|
561
|
+
}
|
|
562
|
+
else if (!("entry" in slot)) {
|
|
563
|
+
await this.endTool(followed, slot.callId, slot.name, {
|
|
564
|
+
content: [],
|
|
565
|
+
details: slot.details,
|
|
566
|
+
isError: true,
|
|
567
|
+
durationMs: undefined,
|
|
554
568
|
});
|
|
555
|
-
return;
|
|
556
569
|
}
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
async startTool(followed, callId, name) {
|
|
573
|
+
if (followed.startedTools.has(callId))
|
|
574
|
+
return;
|
|
575
|
+
followed.startedTools.add(callId);
|
|
576
|
+
await this.emitToolStart(callId, name, followed.calls.get(callId)?.args ?? {});
|
|
577
|
+
}
|
|
578
|
+
async endTool(followed, callId, name, result) {
|
|
579
|
+
if (followed.endedTools.has(callId))
|
|
580
|
+
return;
|
|
581
|
+
followed.endedTools.add(callId);
|
|
582
|
+
await this.emit({
|
|
583
|
+
type: "tool_execution_end",
|
|
584
|
+
toolCallId: callId,
|
|
585
|
+
toolName: name,
|
|
586
|
+
result: { content: result.content, details: result.details },
|
|
587
|
+
isError: result.isError,
|
|
588
|
+
durationMs: result.durationMs,
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
async applyCompactions(followed, live) {
|
|
592
|
+
const running = new Map((live.compactions ?? []).map((status) => [status.taskId, status]));
|
|
593
|
+
for (const [taskId, status] of running) {
|
|
594
|
+
if (followed.compactions.has(taskId))
|
|
595
|
+
continue;
|
|
596
|
+
followed.compactions.set(taskId, status);
|
|
597
|
+
await this.emit({ type: "compaction_start", reason: status.trigger });
|
|
598
|
+
}
|
|
599
|
+
for (const [taskId, status] of followed.compactions) {
|
|
600
|
+
if (running.has(taskId))
|
|
601
|
+
continue;
|
|
602
|
+
followed.compactions.delete(taskId);
|
|
603
|
+
if (this.activeRequest?.token === COMPACTION_REQUEST)
|
|
604
|
+
this.activeRequest = undefined;
|
|
605
|
+
const outcome = await this.compactionOutcome(status.taskId);
|
|
606
|
+
await this.emit({
|
|
607
|
+
type: "compaction_end",
|
|
608
|
+
reason: status.trigger,
|
|
609
|
+
aborted: outcome?.status === "aborted",
|
|
610
|
+
errorMessage: outcome?.status === "failed" ? outcome.error.message : undefined,
|
|
611
|
+
});
|
|
564
612
|
}
|
|
565
613
|
}
|
|
566
614
|
async streamContent(content) {
|
|
@@ -609,15 +657,10 @@ export class MikanAgentSession {
|
|
|
609
657
|
await this.exceedBudget(reason);
|
|
610
658
|
}
|
|
611
659
|
async emitToolStart(toolCallId, toolName, args) {
|
|
612
|
-
this.toolArgs.set(toolCallId, args);
|
|
613
660
|
this.tally.toolCalls += 1;
|
|
614
661
|
this.tally.toolCallCounts[toolName] = (this.tally.toolCallCounts[toolName] ?? 0) + 1;
|
|
615
662
|
await this.emit({ type: "tool_execution_start", toolCallId, toolName, args });
|
|
616
663
|
}
|
|
617
|
-
async emitToolEnd(toolCallId, toolName, result, isError) {
|
|
618
|
-
this.toolArgs.delete(toolCallId);
|
|
619
|
-
await this.emit({ type: "tool_execution_end", toolCallId, toolName, result, isError });
|
|
620
|
-
}
|
|
621
664
|
async exceedBudget(reason) {
|
|
622
665
|
if (this.budgetExceededReason)
|
|
623
666
|
return;
|
|
@@ -743,6 +786,9 @@ export function addUsage(total, usage) {
|
|
|
743
786
|
total.reasoning = (total.reasoning ?? 0) + usage.reasoning;
|
|
744
787
|
}
|
|
745
788
|
}
|
|
789
|
+
function usageState(view) {
|
|
790
|
+
return (view.docs["pi.usage"] ?? { models: {}, tools: {} });
|
|
791
|
+
}
|
|
746
792
|
export function sumUsageState(state) {
|
|
747
793
|
const total = createEmptyUsage();
|
|
748
794
|
for (const usage of [...Object.values(state.models), ...Object.values(state.tools)]) {
|
|
@@ -767,50 +813,7 @@ function subtractUsage(total, base) {
|
|
|
767
813
|
reasoning: total.reasoning === undefined ? undefined : total.reasoning - (base.reasoning ?? 0),
|
|
768
814
|
};
|
|
769
815
|
}
|
|
770
|
-
|
|
816
|
+
function copyUsage(usage) {
|
|
771
817
|
return { ...usage, cost: { ...usage.cost } };
|
|
772
818
|
}
|
|
773
|
-
function applyMessageChanges(content, changes) {
|
|
774
|
-
const next = [...content];
|
|
775
|
-
for (const change of changes) {
|
|
776
|
-
switch (change.type) {
|
|
777
|
-
case "message":
|
|
778
|
-
next.splice(0, next.length, ...change.message.content);
|
|
779
|
-
break;
|
|
780
|
-
case "text_start":
|
|
781
|
-
case "thinking_start":
|
|
782
|
-
case "toolcall_start":
|
|
783
|
-
case "block":
|
|
784
|
-
next[change.contentIndex] = change.block;
|
|
785
|
-
break;
|
|
786
|
-
case "text_delta": {
|
|
787
|
-
const block = next[change.contentIndex];
|
|
788
|
-
next[change.contentIndex] =
|
|
789
|
-
block?.type === "text"
|
|
790
|
-
? { ...block, text: `${block.text}${change.delta}` }
|
|
791
|
-
: { type: "text", text: change.delta };
|
|
792
|
-
break;
|
|
793
|
-
}
|
|
794
|
-
case "thinking_delta":
|
|
795
|
-
case "toolcall_delta":
|
|
796
|
-
break;
|
|
797
|
-
default:
|
|
798
|
-
change;
|
|
799
|
-
}
|
|
800
|
-
}
|
|
801
|
-
return next;
|
|
802
|
-
}
|
|
803
|
-
function withNotice(content, notice) {
|
|
804
|
-
return [...(content ?? []), { type: "text", text: notice }];
|
|
805
|
-
}
|
|
806
|
-
function toPiTool(tool) {
|
|
807
|
-
const { outputSchema: _outputSchema, execute, ...registration } = tool;
|
|
808
|
-
return {
|
|
809
|
-
...registration,
|
|
810
|
-
execute: async (args, api, toolContext) => {
|
|
811
|
-
const { content, structuredContent: _structured, ...result } = await execute(args, api, toolContext);
|
|
812
|
-
return { ...result, output: content };
|
|
813
|
-
},
|
|
814
|
-
};
|
|
815
|
-
}
|
|
816
819
|
//# sourceMappingURL=session.js.map
|