@harrylabsj/kiwi 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/contracts/kiwi-contracts.lock.json +1 -1
- package/contracts/manifest.json +2 -2
- package/contracts/runtime-binding/0.1.2/document.schema.json +3 -1
- package/dist/a2a/node.d.ts +2 -0
- package/dist/a2a/node.js +92 -13
- package/dist/a2a/node.js.map +1 -1
- package/dist/a2a/server/merchant-handler.js +31 -17
- package/dist/a2a/server/merchant-handler.js.map +1 -1
- package/dist/a2a/server/pipeline.js +1 -0
- package/dist/a2a/server/pipeline.js.map +1 -1
- package/dist/a2a/server/types.d.ts +1 -0
- package/dist/agent/buyer/buyer-tools.d.ts +2 -2
- package/dist/agent/buyer/buyer-tools.js.map +1 -1
- package/dist/agent/chat-tools.d.ts +2 -11
- package/dist/agent/chat-tools.js.map +1 -1
- package/dist/agent/fake-chat-model.d.ts +3 -3
- package/dist/agent/fake-chat-model.js +3 -5
- package/dist/agent/fake-chat-model.js.map +1 -1
- package/dist/agent/kernel-builder.js +4 -4
- package/dist/agent/kernel-builder.js.map +1 -1
- package/dist/agent/kernel.d.ts +15 -5
- package/dist/agent/kernel.js +143 -67
- package/dist/agent/kernel.js.map +1 -1
- package/dist/agent/kiwi-tool.d.ts +33 -0
- package/dist/agent/kiwi-tool.js +17 -0
- package/dist/agent/kiwi-tool.js.map +1 -0
- package/dist/agent/merchant/merchant-tools.d.ts +2 -17
- package/dist/agent/merchant/merchant-tools.js.map +1 -1
- package/dist/agent/negotiation-chat.d.ts +3 -2
- package/dist/agent/negotiation-chat.js.map +1 -1
- package/dist/agent/session.d.ts +15 -3
- package/dist/agent/session.js +114 -122
- package/dist/agent/session.js.map +1 -1
- package/dist/agent/skills/tools.d.ts +2 -2
- package/dist/agent/skills/tools.js.map +1 -1
- package/dist/cli.d.ts +5 -1
- package/dist/cli.js +239 -32
- package/dist/cli.js.map +1 -1
- package/dist/cloud/binding/enrollment-challenge.d.ts +37 -0
- package/dist/cloud/binding/enrollment-challenge.js +153 -0
- package/dist/cloud/binding/enrollment-challenge.js.map +1 -0
- package/dist/cloud/binding/enrollment-heartbeat.d.ts +26 -0
- package/dist/cloud/binding/enrollment-heartbeat.js +146 -0
- package/dist/cloud/binding/enrollment-heartbeat.js.map +1 -0
- package/dist/cloud/binding/enrollment-key-recovery.d.ts +27 -0
- package/dist/cloud/binding/enrollment-key-recovery.js +127 -0
- package/dist/cloud/binding/enrollment-key-recovery.js.map +1 -0
- package/dist/cloud/binding/runtime-challenge.d.ts +1 -1
- package/dist/cloud/binding/runtime-challenge.js +8 -1
- package/dist/cloud/binding/runtime-challenge.js.map +1 -1
- package/dist/cloud/bootstrap.js +241 -17
- package/dist/cloud/bootstrap.js.map +1 -1
- package/dist/cloud/catalog-client.d.ts +305 -0
- package/dist/cloud/catalog-client.js +962 -0
- package/dist/cloud/catalog-client.js.map +1 -0
- package/dist/cloud/config.d.ts +12 -0
- package/dist/cloud/config.js +45 -0
- package/dist/cloud/config.js.map +1 -1
- package/dist/cloud/http-router.d.ts +2 -0
- package/dist/cloud/http-router.js +8 -0
- package/dist/cloud/http-router.js.map +1 -1
- package/dist/cloud/listing-publisher.d.ts +45 -0
- package/dist/cloud/listing-publisher.js +244 -0
- package/dist/cloud/listing-publisher.js.map +1 -0
- package/dist/cloud/merchant-connect.d.ts +47 -0
- package/dist/cloud/merchant-connect.js +331 -0
- package/dist/cloud/merchant-connect.js.map +1 -0
- package/dist/cloud/merchant-identity.d.ts +14 -0
- package/dist/cloud/merchant-identity.js +76 -0
- package/dist/cloud/merchant-identity.js.map +1 -0
- package/dist/cloud/onboarding/catalog-pipeline.d.ts +135 -0
- package/dist/cloud/onboarding/catalog-pipeline.js +454 -0
- package/dist/cloud/onboarding/catalog-pipeline.js.map +1 -0
- package/dist/cloud/onboarding/steps.js +2 -2
- package/dist/cloud/onboarding/steps.js.map +1 -1
- package/dist/cloud/product-import-tabular.d.ts +100 -0
- package/dist/cloud/product-import-tabular.js +1073 -0
- package/dist/cloud/product-import-tabular.js.map +1 -0
- package/dist/cloud/product-source.d.ts +8 -1
- package/dist/cloud/product-source.js +63 -13
- package/dist/cloud/product-source.js.map +1 -1
- package/dist/config/profile.d.ts +4 -0
- package/dist/config/profile.js +14 -0
- package/dist/config/profile.js.map +1 -1
- package/dist/discovery/catalog-source/cloud-card.d.ts +1 -1
- package/dist/discovery/catalog-source/cloud-card.js +7 -1
- package/dist/discovery/catalog-source/cloud-card.js.map +1 -1
- package/dist/http/merchant-management/api.d.ts +17 -0
- package/dist/http/merchant-management/api.js +244 -7
- package/dist/http/merchant-management/api.js.map +1 -1
- package/dist/http/merchant-management/page.js +313 -18
- package/dist/http/merchant-management/page.js.map +1 -1
- package/dist/mcp/merchant-runtime-assembly.js +4 -1
- package/dist/mcp/merchant-runtime-assembly.js.map +1 -1
- package/dist/mcp/merchant-server.d.ts +2 -0
- package/dist/mcp/merchant-server.js +15 -0
- package/dist/mcp/merchant-server.js.map +1 -1
- package/dist/merchant/negotiation-observer.d.ts +92 -0
- package/dist/merchant/negotiation-observer.js +278 -0
- package/dist/merchant/negotiation-observer.js.map +1 -0
- package/dist/merchant/operations-report.d.ts +85 -0
- package/dist/merchant/operations-report.js +227 -0
- package/dist/merchant/operations-report.js.map +1 -0
- package/dist/merchant/stats-store.d.ts +5 -0
- package/dist/merchant/stats-store.js +18 -0
- package/dist/merchant/stats-store.js.map +1 -1
- package/dist/merchant-gateway/catalog-publications.d.ts +28 -0
- package/dist/merchant-gateway/catalog-publications.js +69 -0
- package/dist/merchant-gateway/catalog-publications.js.map +1 -1
- package/dist/merchant-gateway/catalog-tools.js +20 -0
- package/dist/merchant-gateway/catalog-tools.js.map +1 -1
- package/dist/negotiation/lease/store.js +21 -2
- package/dist/negotiation/lease/store.js.map +1 -1
- package/dist/product-cli.d.ts +2 -2
- package/dist/product-cli.js +24 -9
- package/dist/product-cli.js.map +1 -1
- package/dist/product-init.d.ts +4 -1
- package/dist/product-init.js +39 -11
- package/dist/product-init.js.map +1 -1
- package/dist/product-publish.d.ts +8 -2
- package/dist/product-publish.js +222 -142
- package/dist/product-publish.js.map +1 -1
- package/dist/runtime/fake-model.js +9 -3
- package/dist/runtime/fake-model.js.map +1 -1
- package/package.json +7 -4
package/dist/agent/kernel.d.ts
CHANGED
|
@@ -26,8 +26,9 @@
|
|
|
26
26
|
* - expose memory write tools to the model (governance lives in
|
|
27
27
|
* MemoryStore) and deterministic slash commands to the operator.
|
|
28
28
|
*/
|
|
29
|
-
import {
|
|
30
|
-
import
|
|
29
|
+
import type { ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
30
|
+
import { ModelRuntime } from "@earendil-works/pi-coding-agent";
|
|
31
|
+
import type { Api, Model, Provider } from "@earendil-works/pi-ai";
|
|
31
32
|
import type { AgentProfile } from "../config/profile.js";
|
|
32
33
|
import type { CommerceClient } from "../commerce/types.js";
|
|
33
34
|
import { type AgentPaths } from "./agent-db.js";
|
|
@@ -56,7 +57,17 @@ export interface AgentKernelOptions {
|
|
|
56
57
|
profile: AgentProfile;
|
|
57
58
|
/** Injectable paths (tests); defaults to .kiwi/agents/<agent_id>. */
|
|
58
59
|
paths?: AgentPaths;
|
|
59
|
-
|
|
60
|
+
/**
|
|
61
|
+
* pi-ai model runtime (0.87): the sole model/auth surface handed to
|
|
62
|
+
* createAgentSession. When omitted, a runtime is built and the providers
|
|
63
|
+
* from `models` are registered into it (test/fake path).
|
|
64
|
+
*/
|
|
65
|
+
modelRuntime?: ModelRuntime;
|
|
66
|
+
/**
|
|
67
|
+
* Extra providers to register into the model runtime (legacy `Models`
|
|
68
|
+
* collections from tests; each provider is registered natively).
|
|
69
|
+
*/
|
|
70
|
+
providers?: Provider[];
|
|
60
71
|
model: Model<Api>;
|
|
61
72
|
thinkingLevel?: ThinkingLevel;
|
|
62
73
|
vault?: PrivateVault;
|
|
@@ -109,7 +120,6 @@ export declare class AgentKernel {
|
|
|
109
120
|
private readonly db;
|
|
110
121
|
private readonly store;
|
|
111
122
|
private readonly session;
|
|
112
|
-
private readonly harness;
|
|
113
123
|
private briefing;
|
|
114
124
|
/** 统一时钟(构造注入或墙钟;与 open() 的 clock 闭包同源语义)。 */
|
|
115
125
|
private readonly clock;
|
|
@@ -130,7 +140,7 @@ export declare class AgentKernel {
|
|
|
130
140
|
/** Optional host event projection; it never owns business state. */
|
|
131
141
|
private readonly emitEvent?;
|
|
132
142
|
/** Removes the optional AgentHarness stream bridge during kernel shutdown. */
|
|
133
|
-
private readonly
|
|
143
|
+
private readonly unsubscribeSessionEvents?;
|
|
134
144
|
/** Shared mutable mode ref (tools read it via a getter before the kernel exists). */
|
|
135
145
|
private readonly modeRef;
|
|
136
146
|
/** Live execution hooks for pending candidates (v0.3.0-C /approve). */
|
package/dist/agent/kernel.js
CHANGED
|
@@ -13,20 +13,7 @@
|
|
|
13
13
|
* See the License for the specific language governing permissions and
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
* AgentKernel (design §4.1): lifecycle and concurrency owner of one Kiwi
|
|
18
|
-
* agent instance.
|
|
19
|
-
*
|
|
20
|
-
* v0.3.0-A responsibilities:
|
|
21
|
-
* - open/recover the persistent main conversation (Pi AgentHarness Session);
|
|
22
|
-
* - serialize every user message, slash command and (later) event through
|
|
23
|
-
* one queue — only the kernel submits state changes;
|
|
24
|
-
* - retrieve relevant memories per turn (logged, redaction-leveled) and
|
|
25
|
-
* inject them into the system prompt as an untamperable briefing;
|
|
26
|
-
* - expose memory write tools to the model (governance lives in
|
|
27
|
-
* MemoryStore) and deterministic slash commands to the operator.
|
|
28
|
-
*/
|
|
29
|
-
import { AgentHarness } from "@earendil-works/pi-agent-core";
|
|
16
|
+
import { createAgentSession, createExtensionRuntime, ModelRuntime, SettingsManager, } from "@earendil-works/pi-coding-agent";
|
|
30
17
|
import { existsSync, readFileSync, rmSync } from "node:fs";
|
|
31
18
|
import path from "node:path";
|
|
32
19
|
import { fileURLToPath } from "node:url";
|
|
@@ -54,7 +41,7 @@ import { DeterministicNegotiationRunner } from "../operator/runner.js";
|
|
|
54
41
|
import { MemoryStore } from "./memory/store.js";
|
|
55
42
|
import { MemoryError } from "./memory/types.js";
|
|
56
43
|
import { PrivateVault } from "./memory/vault.js";
|
|
57
|
-
import {
|
|
44
|
+
import { AgentSessionError, openMainSessionManager, validateSessionLog, } from "./session.js";
|
|
58
45
|
import { registerCatalogAgent } from "../discovery/catalog-source/register.js";
|
|
59
46
|
import { KiwiCatalogSource } from "../discovery/catalog-source/kiwi-source.js";
|
|
60
47
|
import { baseSystemPrompt, combineDynamicBriefing, renderMemoryBriefing } from "./system-prompt.js";
|
|
@@ -99,13 +86,6 @@ async function rememberNegotiation(store, input) {
|
|
|
99
86
|
});
|
|
100
87
|
return outcome.kind === "conflict" ? outcome.existing.memory_id : outcome.memory.memory_id;
|
|
101
88
|
}
|
|
102
|
-
function assistantText(message) {
|
|
103
|
-
return message.content
|
|
104
|
-
.filter((b) => b.type === "text")
|
|
105
|
-
.map((b) => b.text)
|
|
106
|
-
.join("\n")
|
|
107
|
-
.trim();
|
|
108
|
-
}
|
|
109
89
|
const PRIVATE_TOOL_RESULT_NAMES = new Set(["view_private_thresholds"]);
|
|
110
90
|
export function toolResultSummary(toolName, result) {
|
|
111
91
|
if (PRIVATE_TOOL_RESULT_NAMES.has(toolName))
|
|
@@ -126,9 +106,9 @@ export function toolResultSummary(toolName, result) {
|
|
|
126
106
|
}
|
|
127
107
|
return "工具调用已完成。";
|
|
128
108
|
}
|
|
129
|
-
/** Bridge
|
|
130
|
-
function
|
|
131
|
-
return
|
|
109
|
+
/** Bridge AgentSession events into the optional host protocol. */
|
|
110
|
+
function attachSessionHostEvents(session, emitEvent) {
|
|
111
|
+
return session.subscribe(async (event) => {
|
|
132
112
|
if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") {
|
|
133
113
|
if (event.assistantMessageEvent.delta !== "") {
|
|
134
114
|
await emitEvent("text_delta", { text: event.assistantMessageEvent.delta });
|
|
@@ -216,6 +196,34 @@ const COMMANDS_HELP = `/memory [preferences|private] 查看记忆概览 / 学
|
|
|
216
196
|
把本 merchant 注册进 agent catalog(buyer 发现用)
|
|
217
197
|
/help 本帮助
|
|
218
198
|
/quit 退出`;
|
|
199
|
+
/**
|
|
200
|
+
* Build a ModelRuntime for callers that only supply legacy provider objects
|
|
201
|
+
* (tests / fake chat models): providers are registered natively so their
|
|
202
|
+
* streamFn and static model lists stay authoritative.
|
|
203
|
+
*/
|
|
204
|
+
async function buildModelRuntime(options) {
|
|
205
|
+
const runtime = await ModelRuntime.create({ refreshOnCreate: false });
|
|
206
|
+
for (const provider of options.providers ?? []) {
|
|
207
|
+
runtime.registerNativeProvider(provider);
|
|
208
|
+
}
|
|
209
|
+
return runtime;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* §18.1 对位:0.87 的 AgentSession 没有 harness 时代的 per-session
|
|
213
|
+
* streamOptions 注入口,profile 的 prompt_cache_retention 只能在
|
|
214
|
+
* ModelRuntime 边界补齐——包装 streamSimple,为未显式指定 cacheRetention
|
|
215
|
+
* 的请求补上 profile 值(compaction 等的显式取值不受影响)。
|
|
216
|
+
*/
|
|
217
|
+
function withPromptCacheRetention(runtime, retention) {
|
|
218
|
+
if (retention === undefined)
|
|
219
|
+
return runtime;
|
|
220
|
+
const original = runtime.streamSimple.bind(runtime);
|
|
221
|
+
runtime.streamSimple = (model, context, options) => original(model, context, {
|
|
222
|
+
...options,
|
|
223
|
+
cacheRetention: options?.cacheRetention ?? retention,
|
|
224
|
+
});
|
|
225
|
+
return runtime;
|
|
226
|
+
}
|
|
219
227
|
/** 读会话 JSONL 中最后一条模型记录(model_change 或 assistant 消息的 provider/model)。 */
|
|
220
228
|
function sessionLastModel(file) {
|
|
221
229
|
let last;
|
|
@@ -259,7 +267,6 @@ export class AgentKernel {
|
|
|
259
267
|
db;
|
|
260
268
|
store;
|
|
261
269
|
session;
|
|
262
|
-
harness;
|
|
263
270
|
briefing;
|
|
264
271
|
/** 统一时钟(构造注入或墙钟;与 open() 的 clock 闭包同源语义)。 */
|
|
265
272
|
clock;
|
|
@@ -280,7 +287,7 @@ export class AgentKernel {
|
|
|
280
287
|
/** Optional host event projection; it never owns business state. */
|
|
281
288
|
emitEvent;
|
|
282
289
|
/** Removes the optional AgentHarness stream bridge during kernel shutdown. */
|
|
283
|
-
|
|
290
|
+
unsubscribeSessionEvents;
|
|
284
291
|
/** Shared mutable mode ref (tools read it via a getter before the kernel exists). */
|
|
285
292
|
modeRef = { value: DEFAULT_AGENT_MODE };
|
|
286
293
|
/** Live execution hooks for pending candidates (v0.3.0-C /approve). */
|
|
@@ -318,7 +325,6 @@ export class AgentKernel {
|
|
|
318
325
|
this.store = options.store;
|
|
319
326
|
this.principal = options.principal;
|
|
320
327
|
this.session = options.session;
|
|
321
|
-
this.harness = options.harness;
|
|
322
328
|
if (options.taskStore !== undefined)
|
|
323
329
|
this.taskStore = options.taskStore;
|
|
324
330
|
if (options.scheduler !== undefined)
|
|
@@ -338,7 +344,7 @@ export class AgentKernel {
|
|
|
338
344
|
if (options.merchantIntelligence !== undefined)
|
|
339
345
|
this.merchantIntelligence = options.merchantIntelligence;
|
|
340
346
|
this.emitEvent = options.emitEvent;
|
|
341
|
-
this.
|
|
347
|
+
this.unsubscribeSessionEvents = options.unsubscribeSessionEvents;
|
|
342
348
|
if (options.modeRef !== undefined)
|
|
343
349
|
this.modeRef = options.modeRef;
|
|
344
350
|
this.pendingHooks = options.pendingHooks ?? new Map();
|
|
@@ -349,6 +355,13 @@ export class AgentKernel {
|
|
|
349
355
|
// 会话来自不同模型(如 fake→deepseek)时重置:旧模型的消息会让新模型首轮
|
|
350
356
|
// 产生空响应(模型没有返回任何文本)。模型变更 = 新的对话历史。
|
|
351
357
|
if (options.model !== undefined && existsSync(paths.mainSession)) {
|
|
358
|
+
// 先验证完整日志,避免损坏行被 sessionLastModel 跳过后直接删除旧会话。
|
|
359
|
+
try {
|
|
360
|
+
validateSessionLog(paths.mainSession);
|
|
361
|
+
}
|
|
362
|
+
catch (e) {
|
|
363
|
+
throw new AgentSessionError(`cannot open main session ${paths.mainSession}: ${e instanceof Error ? e.message : String(e)} (failing closed)`);
|
|
364
|
+
}
|
|
352
365
|
const sessionModel = sessionLastModel(paths.mainSession);
|
|
353
366
|
if (sessionModel !== undefined &&
|
|
354
367
|
(sessionModel.provider !== options.model.provider ||
|
|
@@ -365,9 +378,9 @@ export class AgentKernel {
|
|
|
365
378
|
role: options.profile.role,
|
|
366
379
|
});
|
|
367
380
|
store.bindPrincipal(principal.principal_id);
|
|
368
|
-
let
|
|
381
|
+
let mainSessionManager;
|
|
369
382
|
try {
|
|
370
|
-
|
|
383
|
+
mainSessionManager = openMainSessionManager(paths, paths.dir, options.eventSessionId ?? MAIN_SESSION_ID);
|
|
371
384
|
}
|
|
372
385
|
catch (err) {
|
|
373
386
|
db.close(); // never leak the SQLite handle on a fail-closed open
|
|
@@ -539,47 +552,68 @@ export class AgentKernel {
|
|
|
539
552
|
}
|
|
540
553
|
const base = baseSystemPrompt(options.profile, principal, merchantSkillRegistry.names.length > 0 ? merchantSkillRegistry : undefined);
|
|
541
554
|
let briefing;
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
555
|
+
// §18.1 对位:0.87 的请求超时走 settings(providerRetry.timeoutMs /
|
|
556
|
+
// httpIdleTimeoutMs),kernel 侧另有 Promise.race 兜底。inMemory 设置避免
|
|
557
|
+
// 读写用户 ~/.pi/agent 的全局配置。
|
|
558
|
+
const settingsManager = SettingsManager.inMemory({
|
|
559
|
+
retry: {
|
|
560
|
+
provider: {
|
|
561
|
+
timeoutMs: options.profile.runtime.turn_timeout_seconds * 1000,
|
|
562
|
+
},
|
|
563
|
+
},
|
|
564
|
+
httpIdleTimeoutMs: options.profile.runtime.turn_timeout_seconds * 1000,
|
|
565
|
+
});
|
|
566
|
+
const modelRuntime = withPromptCacheRetention(options.modelRuntime ?? (await buildModelRuntime(options)), options.profile.merchant_experience?.prompt_cache_retention);
|
|
567
|
+
const kiwiTools = [
|
|
568
|
+
...buildMemoryTools(store, { turnId: () => turnId.current }),
|
|
569
|
+
...buyerTools,
|
|
570
|
+
...merchantTools,
|
|
571
|
+
...merchantSkillTools,
|
|
572
|
+
];
|
|
573
|
+
const customTools = kiwiTools.map((tool) => ({
|
|
574
|
+
name: tool.name,
|
|
575
|
+
label: tool.label,
|
|
576
|
+
description: tool.description,
|
|
577
|
+
parameters: tool.parameters,
|
|
578
|
+
// 0.87 harness declares six execute parameters (toolContext/invocation/
|
|
579
|
+
// context trailing); every kiwi tool implements only (id, params), so
|
|
580
|
+
// the adapter forwards exactly those two.
|
|
581
|
+
execute: async (toolCallId, params) => tool.execute(toolCallId, params),
|
|
582
|
+
}));
|
|
583
|
+
const { session: agentSession } = await createAgentSession({
|
|
584
|
+
cwd: paths.dir,
|
|
545
585
|
model: options.model,
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
586
|
+
modelRuntime: modelRuntime,
|
|
587
|
+
settingsManager,
|
|
588
|
+
sessionManager: mainSessionManager.manager,
|
|
589
|
+
customTools,
|
|
590
|
+
noTools: "builtin",
|
|
591
|
+
resourceLoader: {
|
|
592
|
+
getExtensions: () => ({ extensions: [], errors: [], runtime: createExtensionRuntime() }),
|
|
593
|
+
getSkills: () => ({ skills: [], diagnostics: [] }),
|
|
594
|
+
getPrompts: () => ({ prompts: [], diagnostics: [] }),
|
|
595
|
+
getThemes: () => ({ themes: [], diagnostics: [] }),
|
|
596
|
+
getAgentsFiles: () => ({ agentsFiles: [] }),
|
|
597
|
+
// 每次 rebuild 都会读取:briefing setter 改字段后由下一 turn 生效。
|
|
598
|
+
getSystemPrompt: () => (briefing === undefined ? base : `${base}\n\n${briefing}`),
|
|
599
|
+
getSystemPromptSource: () => undefined,
|
|
600
|
+
getAppendSystemPrompt: () => [],
|
|
601
|
+
getAppendSystemPromptSources: () => [],
|
|
602
|
+
extendResources: () => { },
|
|
603
|
+
reload: async () => { },
|
|
563
604
|
},
|
|
564
605
|
...(options.thinkingLevel !== undefined ? { thinkingLevel: options.thinkingLevel } : {}),
|
|
565
606
|
});
|
|
566
|
-
|
|
567
|
-
// 记录恢复历史模型——同一 agent_id 换过模型(如 fake→deepseek)时,旧会话
|
|
568
|
-
// 会把 fake 模型恢复回来覆盖新传入的 real 模型。显式 setModel 覆盖之。
|
|
569
|
-
if (options.model !== undefined) {
|
|
570
|
-
await harness.setModel(options.model);
|
|
571
|
-
}
|
|
572
|
-
const unsubscribeHarnessEvents = emitEvent === undefined
|
|
607
|
+
const unsubscribeSessionEvents = emitEvent === undefined
|
|
573
608
|
? undefined
|
|
574
|
-
:
|
|
609
|
+
: attachSessionHostEvents(agentSession, emitEvent);
|
|
575
610
|
const kernel = new AgentKernel({
|
|
576
611
|
profile: options.profile,
|
|
577
612
|
paths,
|
|
578
613
|
db,
|
|
579
614
|
store,
|
|
580
615
|
principal,
|
|
581
|
-
session,
|
|
582
|
-
harness,
|
|
616
|
+
session: agentSession,
|
|
583
617
|
approvals,
|
|
584
618
|
...(handoffRuntime !== undefined ? { handoffRuntime } : {}),
|
|
585
619
|
modeRef,
|
|
@@ -594,7 +628,7 @@ export class AgentKernel {
|
|
|
594
628
|
...(options.broker !== undefined ? { broker: options.broker } : {}),
|
|
595
629
|
...(merchantIntelligence !== undefined ? { merchantIntelligence } : {}),
|
|
596
630
|
emitEvent,
|
|
597
|
-
...(
|
|
631
|
+
...(unsubscribeSessionEvents !== undefined ? { unsubscribeSessionEvents } : {}),
|
|
598
632
|
});
|
|
599
633
|
kernel.briefingSetter = (value) => {
|
|
600
634
|
briefing = value;
|
|
@@ -1071,11 +1105,14 @@ export class AgentKernel {
|
|
|
1071
1105
|
* (消除"两个脑")。
|
|
1072
1106
|
*/
|
|
1073
1107
|
async injectContext(text) {
|
|
1074
|
-
|
|
1108
|
+
// 0.87:走 sessionManager 纯入列(不触发模型),下轮 prompt 时进入
|
|
1109
|
+
// buildSessionProjection。appendMessage 已被消毒包装拦截 thinking。
|
|
1110
|
+
this.session.sessionManager.appendMessage({
|
|
1075
1111
|
role: "user",
|
|
1076
1112
|
content: `[系统记录] ${text}`,
|
|
1077
1113
|
timestamp: Date.now(),
|
|
1078
1114
|
});
|
|
1115
|
+
this.session.refreshContext();
|
|
1079
1116
|
}
|
|
1080
1117
|
async runMerchantGroundingRead(read) {
|
|
1081
1118
|
await this.emitEvent?.("grounding_started", {
|
|
@@ -1170,8 +1207,47 @@ export class AgentKernel {
|
|
|
1170
1207
|
// dedup into a single evidence piece (§9.3).
|
|
1171
1208
|
this.turnId.current = `${MAIN_SESSION_ID}:${++this.turnSeq}`;
|
|
1172
1209
|
try {
|
|
1173
|
-
|
|
1174
|
-
|
|
1210
|
+
// 0.87:session.prompt 返回 void;assistant 文本从事件流收集。
|
|
1211
|
+
let reply = "";
|
|
1212
|
+
const collect = (event) => {
|
|
1213
|
+
if (event.type === "message_end" &&
|
|
1214
|
+
event.message?.role === "assistant" &&
|
|
1215
|
+
Array.isArray(event.message.content)) {
|
|
1216
|
+
const text = event.message.content
|
|
1217
|
+
.filter((b) => b.type === "text")
|
|
1218
|
+
.map((b) => b.text ?? "")
|
|
1219
|
+
.join("");
|
|
1220
|
+
if (text !== "")
|
|
1221
|
+
reply = text;
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
const unsubscribe = this.session.subscribe(collect);
|
|
1225
|
+
const timeoutMs = this.profile.runtime.turn_timeout_seconds * 1000;
|
|
1226
|
+
let timer;
|
|
1227
|
+
const timeout = new Promise((resolve) => {
|
|
1228
|
+
timer = setTimeout(() => resolve("timeout"), timeoutMs);
|
|
1229
|
+
});
|
|
1230
|
+
try {
|
|
1231
|
+
const outcome = await Promise.race([
|
|
1232
|
+
this.session.prompt(text).then(() => "done"),
|
|
1233
|
+
timeout,
|
|
1234
|
+
]);
|
|
1235
|
+
if (outcome === "timeout") {
|
|
1236
|
+
await this.session.abort();
|
|
1237
|
+
await this.session.waitForIdle();
|
|
1238
|
+
reply = "";
|
|
1239
|
+
await this.emitEvent?.("error", {
|
|
1240
|
+
code: "model_turn_timeout",
|
|
1241
|
+
message: `model turn exceeded ${this.profile.runtime.turn_timeout_seconds}s`,
|
|
1242
|
+
retryable: true,
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
finally {
|
|
1247
|
+
if (timer !== undefined)
|
|
1248
|
+
clearTimeout(timer);
|
|
1249
|
+
unsubscribe();
|
|
1250
|
+
}
|
|
1175
1251
|
// §18.1: a model failure (throw OR empty response) must leave the
|
|
1176
1252
|
// conversation and TUI intact.
|
|
1177
1253
|
if (reply === "") {
|
|
@@ -1528,12 +1604,12 @@ export class AgentKernel {
|
|
|
1528
1604
|
// 链上错误已各自 catch(this.chain 赋值时 catch),此处仅确保等待完成。
|
|
1529
1605
|
}
|
|
1530
1606
|
try {
|
|
1531
|
-
await this.
|
|
1607
|
+
await this.session.waitForIdle();
|
|
1532
1608
|
}
|
|
1533
1609
|
catch {
|
|
1534
1610
|
// best-effort flush; closing the store is what must not be skipped
|
|
1535
1611
|
}
|
|
1536
|
-
this.
|
|
1612
|
+
this.unsubscribeSessionEvents?.();
|
|
1537
1613
|
this.db.close();
|
|
1538
1614
|
}
|
|
1539
1615
|
}
|