@oyasmi/pipiclaw 0.9.6-beta.1 → 0.9.6-beta.3
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 +279 -215
- package/README.md +274 -213
- package/dist/agent/builtin-commands.d.ts +1 -1
- package/dist/agent/builtin-commands.js +1 -1
- package/dist/agent/channel-runner.d.ts +3 -2
- package/dist/agent/channel-runner.js +18 -27
- package/dist/agent/maintenance-context.js +22 -3
- 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 +1 -1
- 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/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 +4 -2
- package/dist/agent/session/session-host.js +17 -15
- package/dist/agent/status-render.js +3 -0
- 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 +14 -12
- 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/types.d.ts +20 -2
- 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/channel-context.d.ts +6 -0
- package/dist/channel/config.d.ts +3 -0
- package/dist/channel/config.js +11 -5
- package/dist/commands/catalog.js +22 -6
- package/dist/config/settings.d.ts +0 -2
- package/dist/config/settings.js +0 -2
- package/dist/config/tools.d.ts +8 -2
- package/dist/config/tools.js +35 -19
- package/dist/desk/commands.d.ts +31 -4
- package/dist/desk/commands.js +101 -15
- package/dist/desk/paths.d.ts +6 -0
- package/dist/desk/paths.js +21 -1
- package/dist/desk/registry.d.ts +11 -0
- package/dist/desk/registry.js +53 -9
- package/dist/desk/types.d.ts +66 -1
- package/dist/dingtalk/config.js +10 -10
- package/dist/dingtalk/delivery.js +2 -0
- package/dist/dingtalk/inbound.js +2 -3
- package/dist/events/watcher.d.ts +1 -1
- package/dist/events/watcher.js +2 -2
- package/dist/main.js +15 -3
- package/dist/memory/anchors.d.ts +5 -0
- package/dist/memory/anchors.js +49 -0
- package/dist/memory/boundary-work.d.ts +3 -1
- package/dist/memory/boundary-work.js +2 -1
- package/dist/memory/commands.js +23 -6
- package/dist/memory/index-budget.d.ts +16 -0
- package/dist/memory/index-budget.js +24 -2
- package/dist/memory/journal.d.ts +7 -12
- package/dist/memory/journal.js +9 -41
- package/dist/memory/lifecycle.d.ts +3 -1
- package/dist/memory/lifecycle.js +4 -2
- package/dist/memory/reflect-job.d.ts +3 -1
- package/dist/memory/reflect-job.js +2 -1
- package/dist/memory/reflect.d.ts +12 -1
- package/dist/memory/reflect.js +104 -20
- package/dist/memory/render.d.ts +4 -1
- package/dist/memory/render.js +6 -4
- package/dist/memory/review-log.d.ts +2 -2
- package/dist/memory/scheduler.d.ts +9 -1
- package/dist/memory/scheduler.js +71 -4
- package/dist/memory/search-index-db.d.ts +15 -0
- package/dist/memory/search-index-db.js +247 -0
- package/dist/memory/search-index-files.d.ts +11 -0
- package/dist/memory/search-index-files.js +26 -0
- package/dist/memory/search-index-notes.d.ts +11 -0
- package/dist/memory/search-index-notes.js +155 -0
- package/dist/memory/search-index-sync.d.ts +19 -0
- package/dist/memory/search-index-sync.js +267 -0
- package/dist/memory/search-terms.d.ts +12 -0
- package/dist/memory/search-terms.js +185 -0
- package/dist/memory/search.d.ts +8 -28
- package/dist/memory/search.js +18 -278
- package/dist/memory/session-corpus.d.ts +15 -26
- package/dist/memory/session-corpus.js +120 -321
- package/dist/memory/session-search.d.ts +7 -4
- package/dist/memory/session-search.js +120 -172
- package/dist/memory/sidecar-worker.d.ts +4 -1
- package/dist/memory/sidecar-worker.js +27 -5
- package/dist/memory/store.d.ts +4 -0
- package/dist/memory/store.js +22 -1
- package/dist/memory/task-archive-digest.d.ts +61 -0
- package/dist/memory/task-archive-digest.js +366 -0
- 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.d.ts +1 -1
- package/dist/models/provider-login.js +8 -7
- 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 +19 -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 +4 -0
- package/dist/runtime/bootstrap.js +82 -22
- package/dist/runtime/daemon-lock.js +3 -3
- package/dist/runtime/desk-runtime.d.ts +33 -6
- package/dist/runtime/desk-runtime.js +302 -47
- package/dist/runtime/ingress-commands.js +24 -4
- package/dist/runtime/shutdown.d.ts +3 -1
- package/dist/runtime/shutdown.js +1 -0
- package/dist/runtime/task-close.d.ts +1 -1
- package/dist/runtime/task-close.js +6 -3
- package/dist/runtime/task-control.d.ts +1 -3
- package/dist/runtime/task-control.js +15 -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 +1 -0
- package/dist/runtime/task-lane.js +17 -14
- package/dist/runtime/task-step.d.ts +3 -3
- package/dist/runtime/task-step.js +23 -14
- package/dist/runtime/transports.js +6 -5
- package/dist/security/command-guard.js +23 -1
- package/dist/shared/text-utils.d.ts +0 -1
- package/dist/shared/text-utils.js +0 -1
- package/dist/subagents/commands.js +14 -5
- package/dist/subagents/completion-wake.js +6 -0
- package/dist/subagents/dispatch.js +20 -18
- package/dist/subagents/external/dispatch.js +20 -14
- package/dist/subagents/external/run.js +10 -20
- package/dist/subagents/external/settlement.js +5 -1
- package/dist/subagents/internal/outcome.js +19 -12
- package/dist/subagents/internal/provider-retry.js +1 -3
- package/dist/subagents/internal/run.d.ts +1 -1
- package/dist/subagents/internal/run.js +27 -4
- package/dist/subagents/internal/state.d.ts +1 -3
- package/dist/subagents/internal/state.js +1 -2
- package/dist/subagents/internal/worker.js +17 -9
- package/dist/subagents/result.d.ts +5 -2
- package/dist/subagents/result.js +35 -6
- package/dist/subagents/run-record.d.ts +21 -0
- package/dist/subagents/run-record.js +5 -0
- package/dist/subagents/runs.d.ts +5 -8
- package/dist/subagents/runs.js +24 -35
- package/dist/subagents/task-envelope.js +11 -8
- package/dist/subagents/task-settlement.js +1 -0
- package/dist/subagents/types.d.ts +14 -4
- package/dist/subagents/workspace-lease.d.ts +37 -10
- package/dist/subagents/workspace-lease.js +121 -13
- package/dist/subagents/worktree.d.ts +47 -5
- package/dist/subagents/worktree.js +316 -30
- package/dist/tasks/brief.d.ts +5 -1
- package/dist/tasks/brief.js +19 -5
- package/dist/tasks/commands.d.ts +3 -1
- package/dist/tasks/commands.js +17 -6
- package/dist/tasks/control.d.ts +9 -0
- package/dist/tasks/loop-log.d.ts +10 -0
- package/dist/tasks/loop-log.js +1 -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 +27 -15
- package/dist/tools/contract/presentation.js +4 -1
- 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 +1 -2
- package/dist/tools/event-manage.js +27 -38
- 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 +4 -1
- package/dist/tools/index.js +7 -4
- package/dist/tools/job.js +12 -11
- package/dist/tools/memory-manage.d.ts +1 -0
- package/dist/tools/memory-manage.js +9 -23
- 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 +31 -11
- package/dist/tools/session-search.d.ts +3 -1
- package/dist/tools/session-search.js +24 -8
- package/dist/tools/skill.js +13 -12
- package/dist/tools/subagent-manage.js +10 -3
- package/dist/tools/subagent.d.ts +22 -2
- package/dist/tools/subagent.js +19 -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 +39 -3
- package/dist/tools/task-manage/lifecycle.d.ts +2 -2
- package/dist/tools/task-manage/lifecycle.js +7 -4
- package/dist/tools/task-manage/shared.d.ts +1 -1
- package/dist/tools/task-manage/types.d.ts +1 -0
- package/dist/tools/web-search.d.ts +13 -7
- package/dist/tools/web-search.js +62 -19
- 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 +5 -0
- package/dist/tui/cli.js +97 -31
- package/dist/tui/client.d.ts +5 -0
- package/dist/tui/client.js +63 -21
- 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/web/client.d.ts +3 -0
- package/dist/web/client.js +18 -3
- package/dist/web/config.d.ts +17 -4
- package/dist/web/config.js +42 -4
- package/dist/web/fetch.js +6 -3
- package/dist/web/format.d.ts +2 -1
- package/dist/web/format.js +7 -3
- package/dist/web/search-providers.d.ts +15 -8
- package/dist/web/search-providers.js +208 -139
- package/dist/web/search.d.ts +13 -3
- package/dist/web/search.js +86 -40
- 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 +9 -7
- 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 +8 -9
- package/dist/agent/session/request-tools.d.ts +0 -3
- package/dist/agent/session/request-tools.js +0 -20
- package/dist/memory/chinese-words.d.ts +0 -1
- package/dist/memory/chinese-words.js +0 -273
- 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/agy-main.md +0 -23
- 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
|
@@ -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,6 +15,7 @@ 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
21
|
import type { SubAgentRunManager } from "../../subagents/runs.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";
|
|
@@ -193,6 +194,7 @@ export class SessionHost {
|
|
|
193
194
|
modelsConfigPath: this.modelsConfigPath,
|
|
194
195
|
});
|
|
195
196
|
this.modelRegistry = wrapModelRegistry(this.modelRuntime);
|
|
197
|
+
this.modelRequests = createModelRequests(this.modelRuntime);
|
|
196
198
|
// Resolve model: prefer saved global default, fall back to first available model.
|
|
197
199
|
this.models.activeModel = resolveInitialModel(this.modelRegistry, this.settingsManager);
|
|
198
200
|
log.logInfo(`Using model: ${this.models.activeModel.provider}/${this.models.activeModel.id} (${this.models.activeModel.name})`);
|
|
@@ -259,7 +261,7 @@ export class SessionHost {
|
|
|
259
261
|
return;
|
|
260
262
|
this.rebuildSessionTools();
|
|
261
263
|
try {
|
|
262
|
-
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()));
|
|
263
265
|
}
|
|
264
266
|
catch (error) {
|
|
265
267
|
log.logWarn("agent.session.reload_unsettled", "Session reload did not settle", {
|
|
@@ -281,7 +283,7 @@ export class SessionHost {
|
|
|
281
283
|
return { cancelled: result.cancelled };
|
|
282
284
|
},
|
|
283
285
|
navigateTree: async (targetId, options) => {
|
|
284
|
-
const result = await this.session.navigateTree(targetId, options);
|
|
286
|
+
const result = await trackModelWork(this.modelRuntime, () => this.session.navigateTree(targetId, options));
|
|
285
287
|
this.syncMemoryBootstrapPendingFromSession();
|
|
286
288
|
return { cancelled: result.cancelled };
|
|
287
289
|
},
|
|
@@ -305,7 +307,7 @@ export class SessionHost {
|
|
|
305
307
|
// snapshot — stale availability is a far smaller problem than a reload
|
|
306
308
|
// (and, through it, a `/new` turn) that never returns.
|
|
307
309
|
try {
|
|
308
|
-
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));
|
|
309
311
|
}
|
|
310
312
|
catch (error) {
|
|
311
313
|
log.logWarn("agent.models.refresh_unsettled", "Model registry refresh did not settle; using the previous snapshot", { channelId: this.channelId, reason: errorMessage(error) });
|
|
@@ -331,7 +333,7 @@ export class SessionHost {
|
|
|
331
333
|
})),
|
|
332
334
|
soul: resources.soul,
|
|
333
335
|
agents: resources.agents,
|
|
334
|
-
playbooks:
|
|
336
|
+
playbooks: selectRuntimePlaybooks(loadRuntimePlaybookCatalog(TOOL_NAMES), toolNames),
|
|
335
337
|
subAgents: this.subAgentDiscovery.agents.map((agent) => ({
|
|
336
338
|
name: agent.name,
|
|
337
339
|
description: agent.description,
|
|
@@ -375,7 +377,7 @@ export class SessionHost {
|
|
|
375
377
|
createCommandExtension({
|
|
376
378
|
getCurrentModel: () => this.session.model ?? this.models.activeModel,
|
|
377
379
|
getAvailableModels: async () => {
|
|
378
|
-
await this.
|
|
380
|
+
await refreshModelRegistry(this.modelRuntime);
|
|
379
381
|
return this.modelRegistry.getAvailable();
|
|
380
382
|
},
|
|
381
383
|
getSessionStats: () => this.session.getSessionStats(),
|
|
@@ -463,6 +465,7 @@ export class SessionHost {
|
|
|
463
465
|
throw new Error("Session generation retired");
|
|
464
466
|
const tools = this.buildRuntimeTools();
|
|
465
467
|
const agent = new Agent({
|
|
468
|
+
sessionId: sessionManager.getSessionId(),
|
|
466
469
|
initialState: {
|
|
467
470
|
systemPrompt: "",
|
|
468
471
|
model: this.models.activeModel,
|
|
@@ -471,10 +474,9 @@ export class SessionHost {
|
|
|
471
474
|
messages: sessionManager.buildSessionContext().messages,
|
|
472
475
|
},
|
|
473
476
|
convertToLlm: (messages) => this.contextInput.convert(messages),
|
|
474
|
-
getApiKey: async () => getApiKeyForModel(this.modelRegistry, this.models.activeModel),
|
|
475
477
|
streamFn: (model, context, options) => {
|
|
476
478
|
this.trace.startRequest();
|
|
477
|
-
return
|
|
479
|
+
return this.modelRequests.stream(model, context, options);
|
|
478
480
|
},
|
|
479
481
|
});
|
|
480
482
|
this.initializeThinkingLevel(agent, this.models.activeModel, sessionManager);
|
|
@@ -520,14 +522,15 @@ export class SessionHost {
|
|
|
520
522
|
const toolsLoad = loadToolsConfigWithDiagnostics(this.appHomeDir);
|
|
521
523
|
reportConfigDiagnostics([...securityLoad.diagnostics, ...toolsLoad.diagnostics], this.channelId);
|
|
522
524
|
this.tasksEnabled = toolsLoad.config.tools.tasks.enabled;
|
|
523
|
-
const
|
|
525
|
+
const active = createPipiclawTools({
|
|
526
|
+
requestScoped: this.deps.profile.kind === "request",
|
|
524
527
|
jobManager: this.jobManager,
|
|
525
528
|
runManager: this.runManager,
|
|
526
529
|
executor: this.executor,
|
|
527
530
|
fileStore: this.fileStore,
|
|
528
531
|
getCurrentModel: () => this.models.activeModel,
|
|
529
532
|
getAvailableModels: () => this.modelRegistry.getAvailable(),
|
|
530
|
-
|
|
533
|
+
modelRequests: this.modelRequests,
|
|
531
534
|
workspaceDir: this.workspaceDir,
|
|
532
535
|
appHomeDir: this.appHomeDir,
|
|
533
536
|
cwd: this.cwd,
|
|
@@ -545,7 +548,6 @@ export class SessionHost {
|
|
|
545
548
|
...(this.deps.profile.kind === "task" ? { taskSession: this.deps.profile.session } : {}),
|
|
546
549
|
...(this.deps.profile.kind === "chat" ? { taskControl: this.deps.getTaskControl?.() } : {}),
|
|
547
550
|
});
|
|
548
|
-
const active = this.deps.profile.kind === "request" ? requestTools(tools) : tools;
|
|
549
551
|
this.currentTools = active;
|
|
550
552
|
// Tool schemas are billed with the system prompt and, unlike it, nothing trims them.
|
|
551
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");
|
|
@@ -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
|
}
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
import { readFile } from "node:fs/promises";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
import { TASK_DIGEST_SETTINGS } from "../../config/settings.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { buildJournalBlocksForBootstrap, buildMemoryIndexForBootstrap, clipWorkspaceMemoryForBootstrap, } from "../../memory/index-budget.js";
|
|
10
|
+
import { readRecentJournalForBootstrap } from "../../memory/journal.js";
|
|
11
11
|
import { renderMemoryBootstrap } from "../../memory/render.js";
|
|
12
12
|
import { listMemoryEntries } from "../../memory/store.js";
|
|
13
13
|
import { localDayKey } from "../../shared/local-time.js";
|
|
@@ -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
|
}
|
|
@@ -57,16 +55,20 @@ export async function buildFirstTurnMemoryBootstrap(deskDir, workspaceDir) {
|
|
|
57
55
|
}
|
|
58
56
|
};
|
|
59
57
|
const today = localDayKey();
|
|
60
|
-
const [entries, workspaceMemory,
|
|
58
|
+
const [entries, workspaceMemory, recentJournal] = await Promise.all([
|
|
61
59
|
listMemoryEntries(deskDir).catch(() => []),
|
|
62
60
|
readOptionalFile(join(workspaceDir, "MEMORY.md")),
|
|
63
|
-
|
|
61
|
+
readRecentJournalForBootstrap(deskDir, today).catch(() => ({ todayText: "" })),
|
|
64
62
|
]);
|
|
65
63
|
const index = buildMemoryIndexForBootstrap(entries);
|
|
66
|
-
const
|
|
64
|
+
const journal = buildJournalBlocksForBootstrap({
|
|
65
|
+
todayDate: today,
|
|
66
|
+
todayText: recentJournal.todayText,
|
|
67
|
+
previous: recentJournal.previous,
|
|
68
|
+
});
|
|
67
69
|
return renderMemoryBootstrap({
|
|
68
70
|
workspaceMemory: clipWorkspaceMemoryForBootstrap(workspaceMemory),
|
|
69
71
|
memoryIndex: entries.length > 0 ? index.text : "",
|
|
70
|
-
journal:
|
|
72
|
+
journal: journal.length > 0 ? journal : undefined,
|
|
71
73
|
});
|
|
72
74
|
}
|
|
@@ -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;
|
|
@@ -32,7 +32,8 @@ export function assembleTurnPrompt(parts) {
|
|
|
32
32
|
parts.durableMemoryBootstrap,
|
|
33
33
|
parts.taskDigest,
|
|
34
34
|
parts.channelCapsule,
|
|
35
|
+
parts.taskContract,
|
|
35
36
|
`<user_message>\n${parts.userMessage}\n</user_message>`,
|
|
36
37
|
];
|
|
37
|
-
return { text: blocks.filter(
|
|
38
|
+
return { text: blocks.filter(Boolean).join("\n\n"), stats };
|
|
38
39
|
}
|
|
@@ -16,7 +16,10 @@ const SYNTHETIC_TOOL_RESULT_HEADER = "Error: Pipiclaw restarted before a durable
|
|
|
16
16
|
"do not repeat the operation blindly.";
|
|
17
17
|
const SYNTHETIC_ABORTED_ASSISTANT_TEXT = "Error: Pipiclaw restarted before a response was generated. Re-send the request if it is still needed.";
|
|
18
18
|
function toolGuidance(toolName) {
|
|
19
|
-
if (toolName === "subagent" ||
|
|
19
|
+
if (toolName === "subagent" ||
|
|
20
|
+
toolName === "subagent_inline" ||
|
|
21
|
+
toolName === "subagent_list" ||
|
|
22
|
+
toolName === "subagent_run") {
|
|
20
23
|
return "先用 `subagent_list` / `subagent_run op=show` 查询这次委派是否已经作为一个 durable run 存在。";
|
|
21
24
|
}
|
|
22
25
|
if (toolName === "job" || toolName === "bash") {
|