@oyasmi/pipiclaw 0.9.5 → 0.9.6-beta.2
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/README.en.md +300 -142
- package/README.md +295 -140
- package/dist/agent/builtin-commands.d.ts +1 -1
- package/dist/agent/builtin-commands.js +1 -1
- package/dist/agent/channel-runner.d.ts +4 -3
- package/dist/agent/channel-runner.js +17 -28
- package/dist/agent/maintenance-context.js +2 -2
- package/dist/agent/prompt/builder.d.ts +1 -1
- package/dist/agent/prompt/builder.js +1 -1
- package/dist/agent/prompt/extension.d.ts +1 -1
- package/dist/agent/prompt/extension.js +1 -1
- package/dist/agent/prompt/manifest.d.ts +6 -1
- package/dist/agent/prompt/manifest.js +12 -5
- package/dist/agent/prompt/resources.d.ts +0 -3
- package/dist/agent/prompt/resources.js +0 -6
- package/dist/agent/prompt/sections.d.ts +2 -2
- package/dist/agent/prompt/sections.js +36 -26
- package/dist/agent/prompt/types.d.ts +3 -2
- package/dist/agent/runner-factory.js +1 -1
- package/dist/agent/session/command-extension.js +3 -1
- package/dist/agent/session/compaction-summary.js +3 -6
- package/dist/agent/session/compaction.d.ts +2 -0
- package/dist/agent/session/compaction.js +22 -14
- package/dist/agent/session/event-guards.d.ts +2 -1
- package/dist/agent/session/event-guards.js +6 -0
- package/dist/agent/session/model-selection.d.ts +2 -1
- package/dist/agent/session/model-selection.js +5 -5
- package/dist/agent/session/session-events.js +31 -4
- package/dist/agent/session/session-host.d.ts +5 -3
- package/dist/agent/session/session-host.js +20 -16
- package/dist/agent/status-render.js +3 -0
- package/dist/agent/stop-watchdog.js +5 -1
- package/dist/agent/turn/task-digest.d.ts +2 -5
- package/dist/agent/turn/task-digest.js +25 -45
- package/dist/agent/turn/turn-context.js +4 -6
- package/dist/agent/turn/turn-error.d.ts +1 -1
- package/dist/agent/turn/turn-error.js +9 -0
- package/dist/agent/turn/turn-pipeline.d.ts +10 -5
- package/dist/agent/turn/turn-pipeline.js +36 -7
- package/dist/agent/turn/turn-prompt.d.ts +2 -0
- package/dist/agent/turn/turn-prompt.js +2 -1
- package/dist/agent/turn/turn-recovery.js +4 -1
- package/dist/agent/turn/turn-state.d.ts +2 -0
- package/dist/agent/turn/turn-state.js +2 -1
- package/dist/agent/types.d.ts +23 -3
- package/dist/agent/types.js +2 -0
- package/dist/app/auth-command.d.ts +25 -0
- package/dist/app/auth-command.js +200 -67
- package/dist/app/home.d.ts +7 -8
- package/dist/app/home.js +143 -56
- package/dist/channel/attachments.d.ts +4 -0
- package/dist/channel/attachments.js +4 -0
- package/dist/channel/channel-context.d.ts +6 -0
- package/dist/channel/config.d.ts +3 -0
- package/dist/channel/config.js +11 -5
- package/dist/channel/store.d.ts +1 -1
- package/dist/commands/catalog.js +42 -10
- package/dist/commands/router.js +5 -1
- package/dist/desk/commands.d.ts +44 -5
- package/dist/desk/commands.js +162 -28
- package/dist/desk/paths.d.ts +6 -0
- package/dist/desk/paths.js +21 -1
- package/dist/desk/registry.d.ts +17 -2
- package/dist/desk/registry.js +122 -12
- package/dist/desk/types.d.ts +83 -1
- package/dist/dingtalk/bot.d.ts +1 -1
- package/dist/dingtalk/bot.js +4 -3
- package/dist/dingtalk/config.js +10 -10
- package/dist/dingtalk/delivery.js +15 -8
- package/dist/dingtalk/inbound-media.d.ts +36 -14
- package/dist/dingtalk/inbound-media.js +71 -12
- package/dist/dingtalk/inbound.d.ts +9 -2
- package/dist/dingtalk/inbound.js +17 -2
- package/dist/dingtalk/media.d.ts +4 -10
- package/dist/dingtalk/media.js +58 -32
- package/dist/events/commands.d.ts +2 -0
- package/dist/events/commands.js +13 -8
- package/dist/events/history.d.ts +9 -0
- package/dist/events/history.js +66 -0
- package/dist/events/parse.js +7 -3
- package/dist/events/types.d.ts +3 -0
- package/dist/events/watcher.d.ts +4 -8
- package/dist/events/watcher.js +53 -58
- package/dist/jobs/kill.d.ts +23 -0
- package/dist/jobs/kill.js +65 -0
- package/dist/jobs/manager.d.ts +19 -18
- package/dist/jobs/manager.js +36 -103
- package/dist/main.js +15 -3
- package/dist/memory/boundary-work.d.ts +2 -1
- package/dist/memory/boundary-work.js +1 -1
- package/dist/memory/lifecycle.d.ts +2 -1
- package/dist/memory/lifecycle.js +2 -2
- package/dist/memory/reflect-job.d.ts +2 -1
- package/dist/memory/reflect-job.js +1 -1
- package/dist/memory/reflect.d.ts +2 -1
- package/dist/memory/reflect.js +25 -4
- package/dist/memory/scheduler.d.ts +2 -1
- package/dist/memory/scheduler.js +1 -1
- package/dist/memory/session-corpus.d.ts +1 -1
- package/dist/memory/session-corpus.js +76 -1
- package/dist/memory/session-search.d.ts +2 -1
- package/dist/memory/session-search.js +14 -12
- package/dist/memory/sidecar-worker.d.ts +2 -1
- package/dist/memory/sidecar-worker.js +2 -4
- package/dist/memory/store.d.ts +2 -0
- package/dist/memory/store.js +19 -9
- package/dist/models/credential-work.d.ts +7 -0
- package/dist/models/credential-work.js +36 -0
- package/dist/models/login-ui.js +4 -4
- package/dist/models/provider-login.js +7 -6
- package/dist/models/requests.d.ts +8 -0
- package/dist/models/requests.js +69 -0
- package/dist/models/runtime.d.ts +3 -1
- package/dist/models/runtime.js +9 -2
- package/dist/playbooks/agent-delegation.md +20 -45
- package/dist/playbooks/background-jobs.md +1 -1
- package/dist/playbooks/catalog.js +1 -1
- package/dist/playbooks/delegation-inline.md +25 -0
- package/dist/playbooks/delegation-recovery.md +28 -0
- package/dist/playbooks/event-scheduling.md +18 -37
- package/dist/playbooks/event-sensors.md +26 -0
- package/dist/playbooks/event-task-template.md +22 -0
- package/dist/playbooks/learning-and-skills.md +26 -0
- package/dist/playbooks/memory-operations.md +36 -0
- package/dist/playbooks/outbound-media.md +2 -2
- package/dist/playbooks/runtime-files.md +29 -0
- package/dist/playbooks/runtime-operations.md +25 -0
- package/dist/playbooks/runtime-orientation.md +18 -53
- package/dist/playbooks/task-lead.md +22 -68
- package/dist/playbooks/task-manage.md +38 -0
- package/dist/playbooks/task-recovery.md +24 -0
- package/dist/playbooks/task-verify.md +22 -0
- package/dist/runtime/bootstrap.d.ts +10 -0
- package/dist/runtime/bootstrap.js +93 -29
- package/dist/runtime/daemon-lock.js +3 -3
- package/dist/runtime/desk-runtime.d.ts +40 -7
- package/dist/runtime/desk-runtime.js +330 -44
- package/dist/runtime/durable-dispatch.d.ts +14 -2
- package/dist/runtime/durable-dispatch.js +207 -65
- package/dist/runtime/ingress-busy.js +17 -8
- package/dist/runtime/ingress-commands.js +24 -4
- package/dist/runtime/ingress-deps.d.ts +4 -1
- package/dist/runtime/ingress.js +6 -1
- package/dist/runtime/request-execution.js +7 -6
- package/dist/runtime/shutdown.d.ts +4 -1
- package/dist/runtime/shutdown.js +88 -35
- package/dist/runtime/task-close.d.ts +1 -1
- package/dist/runtime/task-close.js +6 -3
- package/dist/runtime/task-control.d.ts +2 -3
- package/dist/runtime/task-control.js +38 -11
- package/dist/runtime/task-input.js +1 -1
- package/dist/runtime/task-lane-types.d.ts +12 -2
- package/dist/runtime/task-lane-types.js +9 -0
- package/dist/runtime/task-lane.d.ts +2 -0
- package/dist/runtime/task-lane.js +38 -20
- package/dist/runtime/task-step.d.ts +3 -3
- package/dist/runtime/task-step.js +34 -16
- package/dist/runtime/transports.js +6 -5
- package/dist/runtime/turn-run.js +103 -66
- package/dist/security/command-guard.d.ts +1 -1
- package/dist/security/command-guard.js +259 -314
- package/dist/security/path-guard.js +48 -7
- package/dist/security/shell-parse.d.ts +10 -0
- package/dist/security/shell-parse.js +224 -0
- package/dist/security/types.d.ts +2 -0
- package/dist/subagents/commands.d.ts +1 -1
- package/dist/subagents/commands.js +14 -5
- package/dist/subagents/completion-wake.js +6 -0
- package/dist/subagents/detached-discovery.d.ts +1 -1
- package/dist/subagents/discovery.d.ts +18 -199
- package/dist/subagents/discovery.js +35 -289
- package/dist/subagents/dispatch.d.ts +1 -2
- package/dist/subagents/dispatch.js +50 -29
- package/dist/subagents/external/antigravity-cli.d.ts +2 -0
- package/dist/subagents/external/antigravity-cli.js +517 -0
- package/dist/subagents/external/claude-code.js +8 -17
- package/dist/subagents/external/codex-cli.js +8 -18
- package/dist/subagents/external/cursor-cli.js +34 -31
- package/dist/subagents/external/dispatch.d.ts +21 -7
- package/dist/subagents/external/dispatch.js +146 -12
- package/dist/subagents/external/exec.js +3 -0
- package/dist/subagents/external/harness.d.ts +57 -33
- package/dist/subagents/external/harness.js +69 -40
- package/dist/subagents/external/reconciliation.js +1 -0
- package/dist/subagents/external/registry.js +4 -1
- package/dist/subagents/external/role-warnings.d.ts +4 -13
- package/dist/subagents/external/role-warnings.js +4 -28
- package/dist/subagents/external/run.d.ts +1 -2
- package/dist/subagents/external/run.js +16 -26
- package/dist/subagents/external/settlement.d.ts +2 -0
- package/dist/subagents/external/settlement.js +13 -3
- package/dist/subagents/format.d.ts +1 -1
- package/dist/subagents/format.js +1 -1
- package/dist/subagents/internal/outcome.d.ts +1 -2
- package/dist/subagents/internal/outcome.js +19 -12
- package/dist/subagents/internal/provider-retry.d.ts +1 -2
- package/dist/subagents/internal/provider-retry.js +2 -4
- package/dist/subagents/internal/run.d.ts +2 -3
- package/dist/subagents/internal/run.js +27 -4
- package/dist/subagents/internal/state.d.ts +2 -5
- package/dist/subagents/internal/state.js +1 -2
- package/dist/subagents/internal/worker.d.ts +1 -2
- package/dist/subagents/internal/worker.js +20 -10
- package/dist/subagents/resolve.d.ts +56 -0
- package/dist/subagents/resolve.js +202 -0
- package/dist/subagents/result.d.ts +6 -4
- package/dist/subagents/result.js +39 -6
- package/dist/subagents/run-record.d.ts +54 -2
- package/dist/subagents/run-record.js +7 -0
- package/dist/subagents/runs.d.ts +6 -31
- package/dist/subagents/runs.js +26 -31
- package/dist/subagents/task-envelope.d.ts +1 -2
- package/dist/subagents/task-envelope.js +13 -4
- package/dist/subagents/task-settlement.js +1 -0
- package/dist/subagents/types.d.ts +105 -6
- package/dist/subagents/types.js +16 -1
- package/dist/subagents/workspace-lease.d.ts +41 -24
- package/dist/subagents/workspace-lease.js +159 -24
- package/dist/subagents/worktree.d.ts +52 -0
- package/dist/subagents/worktree.js +363 -0
- package/dist/tasks/archive.js +4 -0
- package/dist/tasks/brief.d.ts +5 -1
- package/dist/tasks/brief.js +19 -5
- package/dist/tasks/commands.d.ts +6 -1
- package/dist/tasks/commands.js +32 -7
- package/dist/tasks/control.d.ts +11 -0
- package/dist/tasks/delivery.d.ts +2 -0
- package/dist/tasks/delivery.js +141 -21
- package/dist/tasks/loop-log.d.ts +12 -0
- package/dist/tasks/loop-log.js +10 -1
- package/dist/tasks/receipts.d.ts +7 -3
- package/dist/tasks/receipts.js +34 -5
- package/dist/tools/bash.d.ts +3 -0
- package/dist/tools/bash.js +29 -15
- package/dist/tools/contract/presentation.js +7 -5
- package/dist/tools/edit-diff.d.ts +11 -0
- package/dist/tools/edit-diff.js +148 -0
- package/dist/tools/edit-match.d.ts +18 -0
- package/dist/tools/edit-match.js +242 -0
- package/dist/tools/edit.d.ts +10 -2
- package/dist/tools/edit.js +91 -361
- package/dist/tools/event-manage.d.ts +2 -1
- package/dist/tools/event-manage.js +46 -39
- package/dist/tools/file-observations.d.ts +9 -0
- package/dist/tools/file-observations.js +50 -0
- package/dist/tools/glob-match.d.ts +3 -1
- package/dist/tools/glob-match.js +40 -17
- package/dist/tools/glob.d.ts +1 -0
- package/dist/tools/glob.js +39 -19
- package/dist/tools/grep-command.d.ts +13 -0
- package/dist/tools/grep-command.js +77 -0
- package/dist/tools/grep.js +96 -201
- package/dist/tools/index.d.ts +6 -2
- package/dist/tools/index.js +9 -4
- package/dist/tools/job.js +12 -11
- package/dist/tools/read.d.ts +2 -0
- package/dist/tools/read.js +10 -2
- package/dist/tools/registry.d.ts +16 -11
- package/dist/tools/registry.js +32 -12
- package/dist/tools/send-media.js +9 -5
- package/dist/tools/session-search.d.ts +2 -1
- package/dist/tools/session-search.js +15 -5
- package/dist/tools/skill.js +13 -12
- package/dist/tools/subagent-manage.d.ts +1 -1
- package/dist/tools/subagent-manage.js +25 -152
- package/dist/tools/subagent.d.ts +24 -2
- package/dist/tools/subagent.js +24 -18
- package/dist/tools/task-manage/control-tools.js +1 -1
- package/dist/tools/task-manage/factory.d.ts +2 -2
- package/dist/tools/task-manage/factory.js +40 -4
- package/dist/tools/task-manage/lifecycle.d.ts +3 -3
- package/dist/tools/task-manage/lifecycle.js +56 -13
- package/dist/tools/task-manage/schema.d.ts +4 -1
- package/dist/tools/task-manage/schema.js +4 -1
- package/dist/tools/task-manage/shared.d.ts +1 -1
- package/dist/tools/task-manage/types.d.ts +5 -1
- package/dist/tools/write-content.d.ts +7 -1
- package/dist/tools/write-content.js +18 -6
- package/dist/tools/write.d.ts +3 -0
- package/dist/tools/write.js +6 -2
- package/dist/tui/cli.d.ts +6 -6
- package/dist/tui/cli.js +120 -28
- package/dist/tui/client.d.ts +7 -0
- package/dist/tui/client.js +67 -16
- package/dist/tui/frontend.d.ts +2 -0
- package/dist/tui/pitui-frontend.d.ts +4 -0
- package/dist/tui/pitui-frontend.js +27 -0
- package/dist/tui/render.d.ts +1 -0
- package/dist/tui/render.js +5 -0
- package/dist/tui/session.js +6 -0
- package/dist/usage/ledger.d.ts +1 -1
- package/dist/usage/render.js +1 -1
- package/dist/web/client.d.ts +1 -0
- package/dist/web/client.js +11 -0
- package/dist/web/fetch.js +6 -3
- package/dist/webapi/auth.d.ts +0 -3
- package/dist/webapi/auth.js +3 -7
- package/dist/webapi/config.js +17 -14
- package/dist/webapi/key-command.js +41 -41
- package/dist/webapi/response-state.d.ts +7 -1
- package/dist/webapi/response-state.js +166 -3
- package/dist/webapi/routes/websocket.js +24 -1
- package/dist/webapi/server.d.ts +3 -1
- package/dist/webapi/server.js +32 -1
- package/dist/webapi/service.js +8 -0
- package/dist/webapi/transport.d.ts +2 -0
- package/dist/webapi/transport.js +11 -1
- package/examples/sub-agents/README.md +43 -75
- package/examples/sub-agents/agy-main.md +21 -0
- package/examples/sub-agents/claude-high.md +21 -0
- package/examples/sub-agents/claude-main.md +21 -0
- package/examples/sub-agents/codex-flash.md +21 -0
- package/examples/sub-agents/codex-high.md +21 -0
- package/examples/sub-agents/codex-main.md +21 -0
- package/examples/sub-agents/codex-review.md +20 -0
- package/examples/sub-agents/cursor-high.md +20 -0
- package/examples/sub-agents/cursor-main.md +20 -0
- package/examples/sub-agents/glm-flash.md +21 -0
- package/examples/sub-agents/glm-high.md +21 -0
- package/examples/sub-agents/pi-scout.md +20 -0
- package/package.json +11 -12
- package/dist/agent/session/request-tools.d.ts +0 -3
- package/dist/agent/session/request-tools.js +0 -20
- package/dist/models/api-keys.d.ts +0 -11
- package/dist/models/api-keys.js +0 -22
- package/dist/playbooks/memory-and-learning.md +0 -55
- package/examples/sub-agents/agents/claude-high.md +0 -23
- package/examples/sub-agents/agents/claude-main.md +0 -23
- package/examples/sub-agents/agents/codex-flash.md +0 -23
- package/examples/sub-agents/agents/codex-high.md +0 -23
- package/examples/sub-agents/agents/codex-main.md +0 -23
- package/examples/sub-agents/agents/codex-review.md +0 -23
- package/examples/sub-agents/agents/cursor-high.md +0 -22
- package/examples/sub-agents/agents/cursor-main.md +0 -22
- package/examples/sub-agents/agents/glm-flash.md +0 -23
- package/examples/sub-agents/agents/glm-high.md +0 -23
- package/examples/sub-agents/agents/scout.md +0 -30
- package/examples/sub-agents/roles/builder-hard.md +0 -34
- package/examples/sub-agents/roles/builder.md +0 -34
- package/examples/sub-agents/roles/explorer.md +0 -39
- package/examples/sub-agents/roles/git-committer.md +0 -43
- package/examples/sub-agents/roles/planner.md +0 -33
- package/examples/sub-agents/roles/reviewer.md +0 -33
- package/examples/sub-agents/roles/verifier.md +0 -35
- package/examples/sub-agents/roles/worker.md +0 -34
|
@@ -66,6 +66,12 @@ export function isToolExecutionEndEvent(value) {
|
|
|
66
66
|
export function isMessageStartEvent(value) {
|
|
67
67
|
return hasEventType(value, "message_start") && "message" in value;
|
|
68
68
|
}
|
|
69
|
+
export function isMessageUpdateEvent(value) {
|
|
70
|
+
return (hasEventType(value, "message_update") &&
|
|
71
|
+
"message" in value &&
|
|
72
|
+
isRecord(value.assistantMessageEvent) &&
|
|
73
|
+
typeof value.assistantMessageEvent.type === "string");
|
|
74
|
+
}
|
|
69
75
|
export function isMessageEndEvent(value) {
|
|
70
76
|
return hasEventType(value, "message_end") && "message" in value;
|
|
71
77
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
8
8
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
9
|
-
import type { AgentSession, ModelRegistry, SettingsManager as SDKSettingsManager } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import type { AgentSession, ModelRegistry, ModelRuntime, SettingsManager as SDKSettingsManager } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import type { PipiclawSettingsManager } from "../../config/settings.js";
|
|
11
11
|
export interface ModelSelectionDeps {
|
|
12
12
|
channelId: string;
|
|
@@ -14,6 +14,7 @@ export interface ModelSelectionDeps {
|
|
|
14
14
|
getSession: () => AgentSession;
|
|
15
15
|
getSessionSettingsManager: () => SDKSettingsManager;
|
|
16
16
|
getModelRegistry: () => ModelRegistry;
|
|
17
|
+
getModelRuntime: () => ModelRuntime;
|
|
17
18
|
}
|
|
18
19
|
export declare class ModelSelection {
|
|
19
20
|
private deps;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import * as log from "../../log.js";
|
|
2
|
-
import { getApiKeyForModel } from "../../models/api-keys.js";
|
|
3
2
|
import { PRIMARY_COOLDOWN_MS, shouldRestorePrimary } from "../../models/fallback.js";
|
|
4
3
|
import { findExactModelReferenceMatch, formatModelReference } from "../../models/reference.js";
|
|
5
|
-
import {
|
|
4
|
+
import { createModelRequests } from "../../models/requests.js";
|
|
5
|
+
import { defaultModel, refreshModelRegistry } from "../../models/runtime.js";
|
|
6
6
|
import { errorMessage } from "../../shared/text-utils.js";
|
|
7
7
|
import { cycleThinkingLevelWithConditionalPersist, setModelWithThinkingPreservation, setThinkingLevelWithConditionalPersist, } from "./session-adapter.js";
|
|
8
8
|
export class ModelSelection {
|
|
@@ -74,7 +74,7 @@ export class ModelSelection {
|
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
76
|
const modelRegistry = this.deps.getModelRegistry();
|
|
77
|
-
await
|
|
77
|
+
await refreshModelRegistry(this.deps.getModelRuntime());
|
|
78
78
|
const available = modelRegistry.getAvailable();
|
|
79
79
|
const { match, ambiguous } = findExactModelReferenceMatch(reference, available);
|
|
80
80
|
if (!match) {
|
|
@@ -85,12 +85,12 @@ export class ModelSelection {
|
|
|
85
85
|
return null;
|
|
86
86
|
}
|
|
87
87
|
try {
|
|
88
|
-
await
|
|
88
|
+
await createModelRequests(this.deps.getModelRuntime()).ensureAuth(match);
|
|
89
89
|
}
|
|
90
90
|
catch {
|
|
91
91
|
log.logWarn("agent.model.fallback_unresolved", "Fallback model skipped", {
|
|
92
92
|
channelId: this.deps.channelId,
|
|
93
|
-
fields: { reference, problem: "
|
|
93
|
+
fields: { reference, problem: "authentication unavailable" },
|
|
94
94
|
});
|
|
95
95
|
return null;
|
|
96
96
|
}
|
|
@@ -13,7 +13,10 @@ import { describeToolCall } from "../../tools/contract/presentation.js";
|
|
|
13
13
|
import { addUsage } from "../../usage/totals.js";
|
|
14
14
|
import { extractToolResultText } from "../turn/progress-formatter.js";
|
|
15
15
|
import { renderCompactionFailure } from "../turn/turn-error.js";
|
|
16
|
-
import { extractCustomCommandResultText, isAssistantEventMessage, isAutoCompactionEndEvent, isAutoCompactionStartEvent, isAutoRetryStartEvent, isMessageEndEvent, isMessageStartEvent, isSubAgentToolDetails, isTextPart, isThinkingPart, isToolExecutionEndEvent, isToolExecutionStartEvent, isToolExecutionUpdateEvent, isTurnEndEvent, isUserEventMessage, } from "./event-guards.js";
|
|
16
|
+
import { extractCustomCommandResultText, isAssistantEventMessage, isAutoCompactionEndEvent, isAutoCompactionStartEvent, isAutoRetryStartEvent, isMessageEndEvent, isMessageStartEvent, isMessageUpdateEvent, isSubAgentToolDetails, isTextPart, isThinkingPart, isToolExecutionEndEvent, isToolExecutionStartEvent, isToolExecutionUpdateEvent, isTurnEndEvent, isUserEventMessage, } from "./event-guards.js";
|
|
17
|
+
function isDelegationToolName(name) {
|
|
18
|
+
return name === "subagent" || name === "subagent_inline";
|
|
19
|
+
}
|
|
17
20
|
/**
|
|
18
21
|
* A `write`/`edit` result whose resolved target landed inside either skill tree. The
|
|
19
22
|
* `skill` tool is read-only (spec 045); authoring a workspace skill goes through the generic file
|
|
@@ -70,7 +73,7 @@ export async function handleSessionEvent(event, context) {
|
|
|
70
73
|
return;
|
|
71
74
|
}
|
|
72
75
|
if (isToolExecutionUpdateEvent(event)) {
|
|
73
|
-
if (event.toolName
|
|
76
|
+
if (!isDelegationToolName(event.toolName)) {
|
|
74
77
|
return;
|
|
75
78
|
}
|
|
76
79
|
const partialText = truncate(extractToolResultText(event.partialResult), 200);
|
|
@@ -85,7 +88,7 @@ export async function handleSessionEvent(event, context) {
|
|
|
85
88
|
const pending = pendingTools.get(event.toolCallId);
|
|
86
89
|
pendingTools.delete(event.toolCallId);
|
|
87
90
|
const durationMs = pending ? Date.now() - pending.startTime : 0;
|
|
88
|
-
const subAgentDetails = event.toolName
|
|
91
|
+
const subAgentDetails = isDelegationToolName(event.toolName) &&
|
|
89
92
|
isRecord(event.result) &&
|
|
90
93
|
"details" in event.result &&
|
|
91
94
|
isSubAgentToolDetails(event.result.details)
|
|
@@ -151,7 +154,7 @@ export async function handleSessionEvent(event, context) {
|
|
|
151
154
|
ctx.emit({
|
|
152
155
|
type: "tool.end",
|
|
153
156
|
callId: event.toolCallId,
|
|
154
|
-
ok: !
|
|
157
|
+
ok: !treatAsError && !rejected,
|
|
155
158
|
output: truncate(resultStr, TOOL_EVENT_OUTPUT_CHARS),
|
|
156
159
|
...(failure ? { failure } : {}),
|
|
157
160
|
});
|
|
@@ -159,6 +162,8 @@ export async function handleSessionEvent(event, context) {
|
|
|
159
162
|
}
|
|
160
163
|
if (isMessageStartEvent(event)) {
|
|
161
164
|
if (isAssistantEventMessage(event.message)) {
|
|
165
|
+
runState.streamingTextBuffer = "";
|
|
166
|
+
runState.streamingTextFlushed = false;
|
|
162
167
|
log.logResponseStart(logCtx);
|
|
163
168
|
}
|
|
164
169
|
else if (isUserEventMessage(event.message)) {
|
|
@@ -170,6 +175,28 @@ export async function handleSessionEvent(event, context) {
|
|
|
170
175
|
}
|
|
171
176
|
return;
|
|
172
177
|
}
|
|
178
|
+
if (isMessageUpdateEvent(event)) {
|
|
179
|
+
const sub = event.assistantMessageEvent;
|
|
180
|
+
if (sub.type === "thinking_delta" && typeof sub.delta === "string" && sub.delta.length > 0) {
|
|
181
|
+
ctx.emit({ type: "reasoning.delta", delta: sub.delta });
|
|
182
|
+
}
|
|
183
|
+
else if (sub.type === "text_delta" && typeof sub.delta === "string" && sub.delta.length > 0) {
|
|
184
|
+
if (runState.streamingTextFlushed) {
|
|
185
|
+
ctx.emit({ type: "text.delta", delta: sub.delta });
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
runState.streamingTextBuffer += sub.delta;
|
|
189
|
+
const trimmed = runState.streamingTextBuffer.trimStart();
|
|
190
|
+
if (trimmed.length > 0 && !"[SILENT]".startsWith(trimmed) && !trimmed.startsWith("[SILENT]")) {
|
|
191
|
+
runState.streamingTextFlushed = true;
|
|
192
|
+
const flushed = runState.streamingTextBuffer;
|
|
193
|
+
runState.streamingTextBuffer = "";
|
|
194
|
+
ctx.emit({ type: "text.delta", delta: flushed });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
173
200
|
if (isMessageEndEvent(event)) {
|
|
174
201
|
const commandResultText = extractCustomCommandResultText(event.message);
|
|
175
202
|
if (commandResultText) {
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* turns; `ChannelRunner` drives turns through it.
|
|
7
7
|
*/
|
|
8
8
|
import { Agent, type AgentTool } from "@earendil-works/pi-agent-core";
|
|
9
|
-
import { AgentSession, type ModelRegistry, type SettingsManager as SDKSettingsManager, type SessionManager } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { AgentSession, type ModelRegistry, type ModelRuntime, type SettingsManager as SDKSettingsManager, type SessionManager } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import type { SendMedia } from "../../channel/transport.js";
|
|
11
11
|
import type { TurnContext } from "../../channel/turn-context.js";
|
|
12
12
|
import type { PipiclawSettingsManager } from "../../config/settings.js";
|
|
@@ -15,10 +15,11 @@ import type { Executor } from "../../executor.js";
|
|
|
15
15
|
import type { FileStore } from "../../file-store.js";
|
|
16
16
|
import type { DeskJobManager } from "../../jobs/manager.js";
|
|
17
17
|
import type { MemoryLifecycle } from "../../memory/lifecycle.js";
|
|
18
|
+
import { type ModelRequests } from "../../models/requests.js";
|
|
18
19
|
import type { LoadedSecurityConfig } from "../../security/config.js";
|
|
19
20
|
import type { PipiclawSkillsResult } from "../../skills/types.js";
|
|
20
|
-
import { type SubAgentDiscoveryResult } from "../../subagents/discovery.js";
|
|
21
21
|
import type { SubAgentRunManager } from "../../subagents/runs.js";
|
|
22
|
+
import type { SubAgentDiscoveryResult } from "../../subagents/types.js";
|
|
22
23
|
import type { TaskControl } from "../../tasks/control.js";
|
|
23
24
|
import type { UsageLedger } from "../../usage/ledger.js";
|
|
24
25
|
import { type WorkspacePromptResources } from "../prompt/resources.js";
|
|
@@ -88,8 +89,9 @@ export declare class SessionHost {
|
|
|
88
89
|
session: AgentSession;
|
|
89
90
|
/** Settings manager owned by the currently bound AgentSession. */
|
|
90
91
|
sessionSettingsManager: SDKSettingsManager;
|
|
91
|
-
|
|
92
|
+
modelRuntime: ModelRuntime;
|
|
92
93
|
modelRegistry: ModelRegistry;
|
|
94
|
+
modelRequests: ModelRequests;
|
|
93
95
|
/** Mutual exclusion between prompt submission and session-resource reloads. */
|
|
94
96
|
private readonly sessionResourceGate;
|
|
95
97
|
sessionReady: Promise<void>;
|
|
@@ -6,16 +6,17 @@
|
|
|
6
6
|
* turns; `ChannelRunner` drives turns through it.
|
|
7
7
|
*/
|
|
8
8
|
import { Agent } from "@earendil-works/pi-agent-core";
|
|
9
|
-
import { streamSimple } from "@earendil-works/pi-ai/compat";
|
|
10
9
|
import { AgentSession, AgentSessionRuntime, DefaultResourceLoader, } from "@earendil-works/pi-coding-agent";
|
|
11
10
|
import { commitActiveSessionRef } from "../../channel/active-session-store.js";
|
|
12
11
|
import { loadToolsConfigWithDiagnostics } from "../../config/tools.js";
|
|
13
12
|
import * as log from "../../log.js";
|
|
14
13
|
import { handleMemoryCommand } from "../../memory/commands.js";
|
|
15
14
|
import { hasMemoryBootstrapBlock } from "../../memory/render.js";
|
|
16
|
-
import {
|
|
15
|
+
import { trackModelWork } from "../../models/credential-work.js";
|
|
17
16
|
import { findExactModelReferenceMatch, hasKnownModelPricing } from "../../models/reference.js";
|
|
18
|
-
import {
|
|
17
|
+
import { createModelRequests } from "../../models/requests.js";
|
|
18
|
+
import { createModelRuntime, refreshModelRegistry, resolveInitialModel, wrapModelRegistry, } from "../../models/runtime.js";
|
|
19
|
+
import { loadRuntimePlaybookCatalog, selectRuntimePlaybooks } from "../../playbooks/catalog.js";
|
|
19
20
|
import { errorMessage } from "../../shared/text-utils.js";
|
|
20
21
|
import { isRecord } from "../../shared/type-guards.js";
|
|
21
22
|
import { withTimeout } from "../../shared/with-timeout.js";
|
|
@@ -24,16 +25,16 @@ import { getSkillRoots } from "../../skills/roots.js";
|
|
|
24
25
|
import { discoverSubAgents } from "../../subagents/discovery.js";
|
|
25
26
|
import { withExecutionTrace } from "../../tools/contract/execution-trace.js";
|
|
26
27
|
import { createPipiclawTools } from "../../tools/index.js";
|
|
28
|
+
import { TOOL_NAMES } from "../../tools/registry.js";
|
|
27
29
|
import { buildPipiclawSystemPrompt } from "../prompt/builder.js";
|
|
28
30
|
import { createPromptBoundaryExtension } from "../prompt/extension.js";
|
|
29
31
|
import { measureToolSchemas, TOOL_SCHEMA_TARGET_UNITS, toolSchemaBudgetWarning } from "../prompt/manifest.js";
|
|
30
|
-
import {
|
|
32
|
+
import { loadWorkspacePromptResources } from "../prompt/resources.js";
|
|
31
33
|
import { createCommandExtension } from "./command-extension.js";
|
|
32
34
|
import { SessionCompaction } from "./compaction.js";
|
|
33
35
|
import { RequestContextInput } from "./context-input.js";
|
|
34
36
|
import { getLastAssistantUsage, isAutoCompactionEndEvent } from "./event-guards.js";
|
|
35
37
|
import { SessionExecutionTrace } from "./execution-trace.js";
|
|
36
|
-
import { requestTools } from "./request-tools.js";
|
|
37
38
|
import { asSdkSettingsManager, DEFAULT_MAIN_THINKING_LEVEL, initializeThinkingLevelCompat, setSessionBaseToolsOverride, } from "./session-adapter.js";
|
|
38
39
|
import { handleSessionEvent } from "./session-events.js";
|
|
39
40
|
import { SessionResourceGate } from "./session-resource-gate.js";
|
|
@@ -104,6 +105,7 @@ export class SessionHost {
|
|
|
104
105
|
ledger: this.ledger,
|
|
105
106
|
getRunState: () => this.runState,
|
|
106
107
|
isEnabled: () => this.settingsManager.getCompactionEnabled(),
|
|
108
|
+
isTaskSession: deps.profile?.kind === "task",
|
|
107
109
|
onPersistenceFault: (session, error) => this.markContextFault(session, error, "background-compaction"),
|
|
108
110
|
onAdopt: () => {
|
|
109
111
|
this.firstTurnMemoryBootstrapPending = true;
|
|
@@ -192,6 +194,7 @@ export class SessionHost {
|
|
|
192
194
|
modelsConfigPath: this.modelsConfigPath,
|
|
193
195
|
});
|
|
194
196
|
this.modelRegistry = wrapModelRegistry(this.modelRuntime);
|
|
197
|
+
this.modelRequests = createModelRequests(this.modelRuntime);
|
|
195
198
|
// Resolve model: prefer saved global default, fall back to first available model.
|
|
196
199
|
this.models.activeModel = resolveInitialModel(this.modelRegistry, this.settingsManager);
|
|
197
200
|
log.logInfo(`Using model: ${this.models.activeModel.provider}/${this.models.activeModel.id} (${this.models.activeModel.name})`);
|
|
@@ -251,14 +254,14 @@ export class SessionHost {
|
|
|
251
254
|
this.currentSkills = loadPipiclawSkills({
|
|
252
255
|
workspaceDir: this.workspaceDir,
|
|
253
256
|
securityConfig: this.loadSecurityConfig().config,
|
|
254
|
-
securityContext: { agentWorkspaceDir: this.workspaceDir, cwd: this.cwd },
|
|
257
|
+
securityContext: { agentWorkspaceDir: this.workspaceDir, cwd: this.cwd, appHomeDir: this.appHomeDir },
|
|
255
258
|
});
|
|
256
259
|
this.subAgentDiscovery = await this.refreshSubAgentDiscovery();
|
|
257
260
|
if (this.retired)
|
|
258
261
|
return;
|
|
259
262
|
this.rebuildSessionTools();
|
|
260
263
|
try {
|
|
261
|
-
await withTimeout(`[${this.channelId}] session reload`, SESSION_RELOAD_TIMEOUT_MS, () => this.session.reload());
|
|
264
|
+
await withTimeout(`[${this.channelId}] session reload`, SESSION_RELOAD_TIMEOUT_MS, () => trackModelWork(this.modelRuntime, () => this.session.reload()));
|
|
262
265
|
}
|
|
263
266
|
catch (error) {
|
|
264
267
|
log.logWarn("agent.session.reload_unsettled", "Session reload did not settle", {
|
|
@@ -280,7 +283,7 @@ export class SessionHost {
|
|
|
280
283
|
return { cancelled: result.cancelled };
|
|
281
284
|
},
|
|
282
285
|
navigateTree: async (targetId, options) => {
|
|
283
|
-
const result = await this.session.navigateTree(targetId, options);
|
|
286
|
+
const result = await trackModelWork(this.modelRuntime, () => this.session.navigateTree(targetId, options));
|
|
284
287
|
this.syncMemoryBootstrapPendingFromSession();
|
|
285
288
|
return { cancelled: result.cancelled };
|
|
286
289
|
},
|
|
@@ -304,7 +307,7 @@ export class SessionHost {
|
|
|
304
307
|
// snapshot — stale availability is a far smaller problem than a reload
|
|
305
308
|
// (and, through it, a `/new` turn) that never returns.
|
|
306
309
|
try {
|
|
307
|
-
await withTimeout(`[${this.channelId}] model registry refresh`, MODEL_REGISTRY_REFRESH_TIMEOUT_MS, () => this.
|
|
310
|
+
await withTimeout(`[${this.channelId}] model registry refresh`, MODEL_REGISTRY_REFRESH_TIMEOUT_MS, () => refreshModelRegistry(this.modelRuntime));
|
|
308
311
|
}
|
|
309
312
|
catch (error) {
|
|
310
313
|
log.logWarn("agent.models.refresh_unsettled", "Model registry refresh did not settle; using the previous snapshot", { channelId: this.channelId, reason: errorMessage(error) });
|
|
@@ -330,7 +333,7 @@ export class SessionHost {
|
|
|
330
333
|
})),
|
|
331
334
|
soul: resources.soul,
|
|
332
335
|
agents: resources.agents,
|
|
333
|
-
playbooks:
|
|
336
|
+
playbooks: selectRuntimePlaybooks(loadRuntimePlaybookCatalog(TOOL_NAMES), toolNames),
|
|
334
337
|
subAgents: this.subAgentDiscovery.agents.map((agent) => ({
|
|
335
338
|
name: agent.name,
|
|
336
339
|
description: agent.description,
|
|
@@ -374,7 +377,7 @@ export class SessionHost {
|
|
|
374
377
|
createCommandExtension({
|
|
375
378
|
getCurrentModel: () => this.session.model ?? this.models.activeModel,
|
|
376
379
|
getAvailableModels: async () => {
|
|
377
|
-
await this.
|
|
380
|
+
await refreshModelRegistry(this.modelRuntime);
|
|
378
381
|
return this.modelRegistry.getAvailable();
|
|
379
382
|
},
|
|
380
383
|
getSessionStats: () => this.session.getSessionStats(),
|
|
@@ -462,6 +465,7 @@ export class SessionHost {
|
|
|
462
465
|
throw new Error("Session generation retired");
|
|
463
466
|
const tools = this.buildRuntimeTools();
|
|
464
467
|
const agent = new Agent({
|
|
468
|
+
sessionId: sessionManager.getSessionId(),
|
|
465
469
|
initialState: {
|
|
466
470
|
systemPrompt: "",
|
|
467
471
|
model: this.models.activeModel,
|
|
@@ -470,10 +474,9 @@ export class SessionHost {
|
|
|
470
474
|
messages: sessionManager.buildSessionContext().messages,
|
|
471
475
|
},
|
|
472
476
|
convertToLlm: (messages) => this.contextInput.convert(messages),
|
|
473
|
-
getApiKey: async () => getApiKeyForModel(this.modelRegistry, this.models.activeModel),
|
|
474
477
|
streamFn: (model, context, options) => {
|
|
475
478
|
this.trace.startRequest();
|
|
476
|
-
return
|
|
479
|
+
return this.modelRequests.stream(model, context, options);
|
|
477
480
|
},
|
|
478
481
|
});
|
|
479
482
|
this.initializeThinkingLevel(agent, this.models.activeModel, sessionManager);
|
|
@@ -519,15 +522,17 @@ export class SessionHost {
|
|
|
519
522
|
const toolsLoad = loadToolsConfigWithDiagnostics(this.appHomeDir);
|
|
520
523
|
reportConfigDiagnostics([...securityLoad.diagnostics, ...toolsLoad.diagnostics], this.channelId);
|
|
521
524
|
this.tasksEnabled = toolsLoad.config.tools.tasks.enabled;
|
|
522
|
-
const
|
|
525
|
+
const active = createPipiclawTools({
|
|
526
|
+
requestScoped: this.deps.profile.kind === "request",
|
|
523
527
|
jobManager: this.jobManager,
|
|
524
528
|
runManager: this.runManager,
|
|
525
529
|
executor: this.executor,
|
|
526
530
|
fileStore: this.fileStore,
|
|
527
531
|
getCurrentModel: () => this.models.activeModel,
|
|
528
532
|
getAvailableModels: () => this.modelRegistry.getAvailable(),
|
|
529
|
-
|
|
533
|
+
modelRequests: this.modelRequests,
|
|
530
534
|
workspaceDir: this.workspaceDir,
|
|
535
|
+
appHomeDir: this.appHomeDir,
|
|
531
536
|
cwd: this.cwd,
|
|
532
537
|
desk: this.conversation.desk,
|
|
533
538
|
deskDir: this.deskDir,
|
|
@@ -543,7 +548,6 @@ export class SessionHost {
|
|
|
543
548
|
...(this.deps.profile.kind === "task" ? { taskSession: this.deps.profile.session } : {}),
|
|
544
549
|
...(this.deps.profile.kind === "chat" ? { taskControl: this.deps.getTaskControl?.() } : {}),
|
|
545
550
|
});
|
|
546
|
-
const active = this.deps.profile.kind === "request" ? requestTools(tools) : tools;
|
|
547
551
|
this.currentTools = active;
|
|
548
552
|
// Tool schemas are billed with the system prompt and, unlike it, nothing trims them.
|
|
549
553
|
// Warn where the prompt's own budget diagnostics are reported, once per tool rebuild.
|
|
@@ -31,6 +31,9 @@ export function renderStatus(options) {
|
|
|
31
31
|
else if (runner && snapshot) {
|
|
32
32
|
try {
|
|
33
33
|
let modelLine = `- 模型:\`${snapshot.model}\`(思考档位 \`${snapshot.thinkingLevel}\`)`;
|
|
34
|
+
if (snapshot.modelConfigured === false) {
|
|
35
|
+
modelLine += " · ⚠️ 未检测到凭据(在终端执行 `pipiclaw auth login` 或配置 `models.json`)";
|
|
36
|
+
}
|
|
34
37
|
if (snapshot.fallback) {
|
|
35
38
|
const until = new Date(snapshot.fallback.cooldownUntilMs);
|
|
36
39
|
const hh = String(until.getHours()).padStart(2, "0");
|
|
@@ -19,7 +19,11 @@ export async function forceEndStuckTurnAfterStop(input) {
|
|
|
19
19
|
const graceMs = input.graceMs ?? STOP_FORCE_END_GRACE_MS;
|
|
20
20
|
const pollMs = input.pollMs ?? STOP_FORCE_END_POLL_MS;
|
|
21
21
|
const deadline = Date.now() + graceMs;
|
|
22
|
-
const
|
|
22
|
+
const generation = input.runner.getTurnStatus().generation;
|
|
23
|
+
const stopped = () => input.runner.isBusy() &&
|
|
24
|
+
input.runner.getTurnStatus().generation === generation &&
|
|
25
|
+
input.runner.getTurnStatus().stopRequested &&
|
|
26
|
+
(input.isCurrent?.() ?? true);
|
|
23
27
|
while (stopped() && Date.now() < deadline) {
|
|
24
28
|
await sleepUnref(Math.min(pollMs, Math.max(1, deadline - Date.now())));
|
|
25
29
|
}
|
|
@@ -3,11 +3,8 @@ export declare const TASK_AGENDA_MAX_UNITS = 600;
|
|
|
3
3
|
/**
|
|
4
4
|
* Builds the `<task_agenda>` block injected into each main-agent turn (spec 020 §2).
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* frontmatters, and the in-flight agenda is universally relevant to an agent that drives
|
|
9
|
-
* work across turns. The block is bounded (maxTasks / maxChars) and framed as background
|
|
10
|
-
* reference, not instruction, so it can never turn an unrelated user turn into task work.
|
|
6
|
+
* The agenda lets chat match a reply to a pending question and identify other work by id.
|
|
7
|
+
* Task controls provide the authoritative state and complete progress.
|
|
11
8
|
*/
|
|
12
9
|
export interface TaskDigestOptions {
|
|
13
10
|
/** The desk directory; tasks live in `<deskDir>/tasks/`. */
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { join } from "node:path";
|
|
2
2
|
import { countPromptUnits } from "../../shared/prompt-units.js";
|
|
3
3
|
import { truncate } from "../../shared/text-utils.js";
|
|
4
|
-
import { taskCostSummary } from "../../tasks/budget.js";
|
|
5
4
|
import { readActiveTasks } from "../../tasks/contract.js";
|
|
6
5
|
/** Automatic-context share for the in-flight task agenda (spec 026 §5.3). */
|
|
7
6
|
export const TASK_AGENDA_MAX_UNITS = 600;
|
|
@@ -24,26 +23,18 @@ function describeState(fields) {
|
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
}
|
|
26
|
+
function deliveryRank(entry, channelId) {
|
|
27
|
+
if (channelId && entry.fields.channel !== channelId)
|
|
28
|
+
return 2;
|
|
29
|
+
return entry.fields.state === "waiting" && entry.fields.waiting?.kind === "user" ? 0 : 1;
|
|
30
|
+
}
|
|
27
31
|
function renderLine(entry, channelId) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (channelId && entry.fields.channel !== channelId) {
|
|
33
|
-
parts.push(entry.fields.channel ? `reports to ${entry.fields.channel}` : "no delivery channel (suspended)");
|
|
34
|
-
}
|
|
35
|
-
// The agenda shows Work Items progress and the current item, not the full list — the complete
|
|
36
|
-
// section is read from the task file by the loop itself, staying well inside the 600-unit
|
|
37
|
-
// budget this block competes for (spec 025/026).
|
|
38
|
-
if (entry.items) {
|
|
39
|
-
parts.push(`items ${entry.items.done}/${entry.items.total} · @${entry.items.current?.id ?? "-"}`);
|
|
32
|
+
if (!entry.readable)
|
|
33
|
+
return `- ${entry.id} · unreadable frontmatter`;
|
|
34
|
+
if (deliveryRank(entry, channelId) === 2) {
|
|
35
|
+
return `- ${entry.id} · ${entry.fields.channel ? `reports to ${entry.fields.channel}` : "delivery suspended"}`;
|
|
40
36
|
}
|
|
41
|
-
|
|
42
|
-
if (usage) {
|
|
43
|
-
const cost = usage.usd > 0 || usage.unknownCostCount ? ` · ${taskCostSummary(usage)}` : "";
|
|
44
|
-
parts.push(`${usage.steps} 步${cost}`);
|
|
45
|
-
}
|
|
46
|
-
return `- ${parts.join(" · ")}`;
|
|
37
|
+
return `- ${entry.id} — ${truncate(entry.title, 80)} · ${describeState(entry.fields)}`;
|
|
47
38
|
}
|
|
48
39
|
/**
|
|
49
40
|
* Render the in-flight task agenda, or `""` when there are no live tasks to show.
|
|
@@ -57,37 +48,26 @@ export async function buildTaskDigest(options) {
|
|
|
57
48
|
const agenda = all.filter((entry) => !entry.fields.outcome);
|
|
58
49
|
if (agenda.length === 0)
|
|
59
50
|
return "";
|
|
60
|
-
|
|
61
|
-
const
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
"The task lane advances the work: do not take it over or advance it because chat is idle.",
|
|
68
|
-
"Full detail lives in the matching tasks/<id>.md file.",
|
|
69
|
-
"",
|
|
70
|
-
];
|
|
71
|
-
const lines = shown.map((entry) => renderLine(entry, options.channelId));
|
|
72
|
-
if (omitted > 0)
|
|
73
|
-
lines.push(`- (+${omitted} more)`);
|
|
74
|
-
const footer = ["</task_agenda>"];
|
|
51
|
+
agenda.sort((a, b) => deliveryRank(a, options.channelId) - deliveryRank(b, options.channelId));
|
|
52
|
+
const header = "<task_agenda>\nBackground reference. Use task_list for current state. Relay matching answers via task_message when available. The task lane advances the work.";
|
|
53
|
+
const render = (kept) => {
|
|
54
|
+
const omitted = agenda.length - kept.length;
|
|
55
|
+
const tail = omitted > 0 ? [`- (+${omitted} more)`] : [];
|
|
56
|
+
return [header, ...kept, ...tail, "</task_agenda>"].join("\n");
|
|
57
|
+
};
|
|
75
58
|
const maxUnits = options.maxUnits ?? Number.POSITIVE_INFINITY;
|
|
76
59
|
const fits = (text) => text.length <= options.maxChars && countPromptUnits(text) <= maxUnits;
|
|
77
|
-
let rendered = [...header, ...lines, ...footer].join("\n");
|
|
78
|
-
if (fits(rendered))
|
|
79
|
-
return rendered;
|
|
80
|
-
// Over one of the budgets: drop whole lines from the end until it fits, keeping
|
|
81
|
-
// actionable-first order. Chars and units are checked together (spec 026 §10.7).
|
|
82
60
|
const kept = [];
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
61
|
+
let rendered = render(kept);
|
|
62
|
+
if (!fits(rendered))
|
|
63
|
+
return "";
|
|
64
|
+
for (const entry of agenda.slice(0, options.maxTasks)) {
|
|
65
|
+
const line = renderLine(entry, options.channelId);
|
|
66
|
+
const candidate = render([...kept, line]);
|
|
67
|
+
if (!fits(candidate))
|
|
86
68
|
break;
|
|
87
69
|
kept.push(line);
|
|
70
|
+
rendered = candidate;
|
|
88
71
|
}
|
|
89
|
-
const droppedCount = agenda.length - kept.length;
|
|
90
|
-
const tail = droppedCount > 0 ? [`- (+${droppedCount} more)`] : [];
|
|
91
|
-
rendered = [...header, ...kept, ...tail, ...footer].join("\n");
|
|
92
72
|
return rendered;
|
|
93
73
|
}
|
|
@@ -23,12 +23,10 @@ export function renderDeskTurnContext(conversation,
|
|
|
23
23
|
channelLabel, workspaceDir) {
|
|
24
24
|
return [
|
|
25
25
|
"<runtime_turn_context>",
|
|
26
|
-
`Workspace: ${workspaceDir}
|
|
27
|
-
conversation.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
`This conversation: ${channelLabel ? `${channelLabel} ` : ""}(channel ${conversation.channelId}).`,
|
|
31
|
-
"The desk's `MEMORY.md` index, `memory/`, `journal/`, `tasks/` and `events/` live in the desk directory. Prefer the context supplied with this turn and the desk-bound tools; read those files directly only when you need detail they did not provide.",
|
|
26
|
+
`Workspace: ${workspaceDir}`,
|
|
27
|
+
`Desk: ${conversation.desk} (${conversation.deskDir})`,
|
|
28
|
+
`Cwd: ${conversation.cwd}`,
|
|
29
|
+
`Channel: ${conversation.channelId}${channelLabel ? ` (${channelLabel})` : ""}`,
|
|
32
30
|
"</runtime_turn_context>",
|
|
33
31
|
].join("\n");
|
|
34
32
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Only stable features are matched — HTTP status codes and the phrases providers have used for
|
|
10
10
|
* years. Anything unrecognised is `other`, which still carries a next step.
|
|
11
11
|
*/
|
|
12
|
-
export type TurnErrorKind = "auth" | "rate_limit" | "context_overflow" | "network" | "other";
|
|
12
|
+
export type TurnErrorKind = "missing_credentials" | "auth" | "rate_limit" | "context_overflow" | "network" | "other";
|
|
13
13
|
export declare function classifyTurnError(message: string): TurnErrorKind;
|
|
14
14
|
/** What the user is told for each class: a reason (absent for `other`) and what to do next. */
|
|
15
15
|
export declare function describeTurnError(kind: TurnErrorKind): {
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
const PATTERNS = [
|
|
2
|
+
[
|
|
3
|
+
"missing_credentials",
|
|
4
|
+
/\bno api[ _-]?key found\b|missing[ _-]?api[ _-]?key|no credentials found for provider|provider is not configured:|尚未配置.*模型凭据/i,
|
|
5
|
+
],
|
|
2
6
|
[
|
|
3
7
|
"auth",
|
|
4
8
|
/\b40[13]\b|unauthori[sz]ed|forbidden|invalid[ _-]?(?:x-)?api[ _-]?key|incorrect api key|authentication[ _-]?(?:error|failed)|(?:token|credential)s? (?:has |have )?expired/i,
|
|
@@ -26,6 +30,11 @@ export function classifyTurnError(message) {
|
|
|
26
30
|
/** What the user is told for each class: a reason (absent for `other`) and what to do next. */
|
|
27
31
|
export function describeTurnError(kind) {
|
|
28
32
|
switch (kind) {
|
|
33
|
+
case "missing_credentials":
|
|
34
|
+
return {
|
|
35
|
+
reason: "尚未配置可用的模型凭据。",
|
|
36
|
+
nextStep: "请在终端执行 `pipiclaw auth login` 登录,或设置供应商环境变量(如 `ANTHROPIC_API_KEY`),或在 `models.json` 中配置模型网关后发送 `/reload`。",
|
|
37
|
+
};
|
|
29
38
|
case "auth":
|
|
30
39
|
return {
|
|
31
40
|
reason: "模型凭据无效或已过期。",
|
|
@@ -2,6 +2,7 @@ import type { InboundImage } from "../../channel/channel-event.js";
|
|
|
2
2
|
import type { TurnInfo } from "../../channel/transport.js";
|
|
3
3
|
import type { DeskConversation } from "../../desk/types.js";
|
|
4
4
|
import type { FallbackRunDeps } from "../../models/fallback.js";
|
|
5
|
+
import type { TaskBrief } from "../../tasks/brief.js";
|
|
5
6
|
import type { ModelSelection } from "../session/model-selection.js";
|
|
6
7
|
import type { SessionHost } from "../session/session-host.js";
|
|
7
8
|
import { type RunnerProfile, type RunState, type TaskSteerScope } from "../types.js";
|
|
@@ -19,6 +20,11 @@ export interface TurnProgress {
|
|
|
19
20
|
bootstrapPrepared: boolean;
|
|
20
21
|
images: PreparedInboundImages;
|
|
21
22
|
}
|
|
23
|
+
interface PreparedTurnInput {
|
|
24
|
+
clippedInput: string;
|
|
25
|
+
userMessage: string;
|
|
26
|
+
preserveRawInput: boolean;
|
|
27
|
+
}
|
|
22
28
|
export declare function createTurnProgress(): TurnProgress;
|
|
23
29
|
export interface TurnPipelineDeps {
|
|
24
30
|
profile: RunnerProfile;
|
|
@@ -46,16 +52,14 @@ export declare class TurnPipeline {
|
|
|
46
52
|
private get runState();
|
|
47
53
|
private get session();
|
|
48
54
|
/** Oversized input is spilled to disk and clipped; the user is told. Slash commands pass through raw. */
|
|
49
|
-
prepareUserInput(turn: TurnInfo, ctx: TurnOutput): Promise<
|
|
50
|
-
clippedInput: string;
|
|
51
|
-
userMessage: string;
|
|
52
|
-
preserveRawInput: boolean;
|
|
53
|
-
}>;
|
|
55
|
+
prepareUserInput(turn: TurnInfo, ctx: TurnOutput): Promise<PreparedTurnInput>;
|
|
54
56
|
/**
|
|
55
57
|
* The turn-dynamic context: the channel capsule (returned) plus, recorded on `turn`, the task
|
|
56
58
|
* digest and the first-turn memory bootstrap. A raw-passthrough input (a slash command) gets none.
|
|
57
59
|
*/
|
|
58
60
|
gatherTurnContext(preserveRawInput: boolean, turn: TurnProgress): Promise<string>;
|
|
61
|
+
private prepareMemoryBootstrap;
|
|
62
|
+
preparePrompt(input: PreparedTurnInput, turn: TurnProgress, channelCapsule: string, taskContract?: TaskBrief["contract"]): Promise<import("../prompt/manifest.js").PromptTurnContextStats>;
|
|
59
63
|
/** Inbound images for the current model, with a notice to the user for each one that was left out. */
|
|
60
64
|
prepareTurnImages(turn: TurnInfo, ctx: TurnOutput): Promise<PreparedInboundImages>;
|
|
61
65
|
/** The seams `runPromptWithFallback` drives: submit the prompt, observe/reset the error, switch model. */
|
|
@@ -71,3 +75,4 @@ export declare class TurnPipeline {
|
|
|
71
75
|
private acceptsScope;
|
|
72
76
|
maybeRunPreventiveCompactionForIncomingText(incomingText: string, imageCount?: number): Promise<void>;
|
|
73
77
|
}
|
|
78
|
+
export {};
|
|
@@ -8,6 +8,7 @@ import { clipUserInput } from "./progress-formatter.js";
|
|
|
8
8
|
import { buildFirstTurnMemoryBootstrap, buildTaskDigestForTurn, renderDeskTurnContext } from "./turn-context.js";
|
|
9
9
|
import { renderFallbackSwitch } from "./turn-error.js";
|
|
10
10
|
import { formatUserMessage, persistOversizedInput, prepareInboundImages, shouldPreserveRawInput, } from "./turn-input.js";
|
|
11
|
+
import { assembleTurnPrompt } from "./turn-prompt.js";
|
|
11
12
|
export function createTurnProgress() {
|
|
12
13
|
return {
|
|
13
14
|
promptSubmitted: false,
|
|
@@ -78,13 +79,7 @@ export class TurnPipeline {
|
|
|
78
79
|
const taskDigestPromise = (this.host.tasksEnabled && this.deps.profile.kind !== "task"
|
|
79
80
|
? buildTaskDigestForTurn(this.deskDir, this.channelId)
|
|
80
81
|
: Promise.resolve("")).then((text) => ({ text }), (error) => ({ error }));
|
|
81
|
-
|
|
82
|
-
// (and the first turn after compaction — see `subscribeToSessionEvents`), never per
|
|
83
|
-
// turn. Mid-session, the model uses `memory_search` to look things up.
|
|
84
|
-
if (this.host.firstTurnMemoryBootstrapPending) {
|
|
85
|
-
turn.durableMemoryBootstrapText = await buildFirstTurnMemoryBootstrap(this.deskDir, this.workspaceDir);
|
|
86
|
-
turn.bootstrapPrepared = true;
|
|
87
|
-
}
|
|
82
|
+
await this.prepareMemoryBootstrap(turn);
|
|
88
83
|
const taskDigestResult = await taskDigestPromise;
|
|
89
84
|
if (taskDigestResult.error !== undefined) {
|
|
90
85
|
throw taskDigestResult.error;
|
|
@@ -92,6 +87,40 @@ export class TurnPipeline {
|
|
|
92
87
|
turn.taskDigestText = taskDigestResult.text ?? "";
|
|
93
88
|
return channelCapsuleText;
|
|
94
89
|
}
|
|
90
|
+
async prepareMemoryBootstrap(turn) {
|
|
91
|
+
if (turn.bootstrapPrepared || !this.host.firstTurnMemoryBootstrapPending)
|
|
92
|
+
return false;
|
|
93
|
+
turn.durableMemoryBootstrapText = await buildFirstTurnMemoryBootstrap(this.deskDir, this.workspaceDir);
|
|
94
|
+
turn.bootstrapPrepared = true;
|
|
95
|
+
return Boolean(turn.durableMemoryBootstrapText);
|
|
96
|
+
}
|
|
97
|
+
async preparePrompt(input, turn, channelCapsule, taskContract) {
|
|
98
|
+
let restoredContract;
|
|
99
|
+
// Check the final projection after compaction. Rebudget when we attach missing context;
|
|
100
|
+
// each source can be added at most once. Keep the task inputs and their receipts intact.
|
|
101
|
+
while (true) {
|
|
102
|
+
const assembled = assembleTurnPrompt({
|
|
103
|
+
...input,
|
|
104
|
+
channelCapsule,
|
|
105
|
+
durableMemoryBootstrap: turn.durableMemoryBootstrapText,
|
|
106
|
+
taskDigest: turn.taskDigestText,
|
|
107
|
+
taskContract: restoredContract,
|
|
108
|
+
});
|
|
109
|
+
turn.promptText = assembled.text;
|
|
110
|
+
await this.maybeRunPreventiveCompactionForIncomingText(turn.promptText, turn.images.images.length);
|
|
111
|
+
if (input.preserveRawInput)
|
|
112
|
+
return assembled.stats;
|
|
113
|
+
const addedMemory = await this.prepareMemoryBootstrap(turn);
|
|
114
|
+
if (!restoredContract &&
|
|
115
|
+
taskContract &&
|
|
116
|
+
!this.host.contextIncludes((text) => text.includes(taskContract.tag))) {
|
|
117
|
+
restoredContract = clipUserInput(taskContract.text, MAX_USER_MESSAGE_CHARS, taskContract.path);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (!addedMemory)
|
|
121
|
+
return assembled.stats;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
95
124
|
/** Inbound images for the current model, with a notice to the user for each one that was left out. */
|
|
96
125
|
async prepareTurnImages(turn, ctx) {
|
|
97
126
|
const prepared = await prepareInboundImages(turn.images, this.session.model ?? this.models.activeModel);
|
|
@@ -14,6 +14,8 @@ export interface TurnPromptParts {
|
|
|
14
14
|
/** `<memory_bootstrap>`: workspace memory + desk memory index + journal, first turn only (spec 050, D1). */
|
|
15
15
|
durableMemoryBootstrap: string;
|
|
16
16
|
taskDigest: string;
|
|
17
|
+
/** Restored contract for a delta brief when the canonical projection dropped its original. */
|
|
18
|
+
taskContract?: string;
|
|
17
19
|
}
|
|
18
20
|
export interface AssembledTurnPrompt {
|
|
19
21
|
text: string;
|