@cqfe/sxin-chat-core 1.0.1 → 1.0.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/dist/{ChatCore-C-G9isDM.d.cts → ChatCore-cSM1HmXp.d.cts} +407 -337
- package/dist/{ChatCore-C-G9isDM.d.ts → ChatCore-cSM1HmXp.d.ts} +407 -337
- package/dist/chunk-Z65C2UII.mjs +2830 -0
- package/dist/index.cjs +1815 -1488
- package/dist/index.d.cts +606 -208
- package/dist/index.d.ts +606 -208
- package/dist/index.mjs +999 -143
- package/dist/vue/index.cjs +1429 -2177
- package/dist/vue/index.d.cts +2 -2
- package/dist/vue/index.d.ts +2 -2
- package/dist/vue/index.mjs +1 -1
- package/package.json +3 -3
- package/dist/chunk-K2ZOZPBB.mjs +0 -3391
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { A as AcquireResult,
|
|
1
|
+
import { B as Block, C as CitationItem, I as IKwargs, R as RunInfo, a as RunMessage, b as ReportBodyPayload, c as ReportOutlinePayload, d as BlockBuilderOptions, G as GroupMessageTool, M as MessageTokenUsage, O as OutlineSubChapter, e as OutlineChapter, T as ThreadView, f as Turn, g as ClarificationRecord, D as DecoratePatch, E as ExtractedPayload, h as ChatTransport, i as ThreadInitialValues, j as MessagesPage, k as ThreadTokenUsage, S as StreamSubmitInput, l as StreamSubmitOptions, H as HitlResponse, m as CreateStreamOptions } from './ChatCore-cSM1HmXp.cjs';
|
|
2
|
+
export { A as AcquireResult, n as AgentThreadState, o as AiTurn, p as ArtifactFile, q as Attachment, r as BlockBase, s as BlockSource, t as BlockStatus, u as BodySegment, v as ChatCore, w as ChatCoreOptions, x as ChatNotify, y as ChatStreamHandle, z as CustomBlock, F as FetchMessagesPageOptions, J as FormBlock, K as FormFieldItem, L as FormFieldType, N as GenerateBodyRequest, P as GenerateBodyResult, Q as HitlActionRequest, U as HitlDecision, V as HitlDecisionType, W as HitlInterruptPayload, X as HitlReviewConfig, Y as IAttachment, Z as IQuote, _ as InlineCitation, $ as InterruptReviewBlock, a0 as ItemSegment, a1 as ListBlock, a2 as ListItem, a3 as Message, a4 as MessageMetadata, a5 as MessageRole, a6 as MessageSource, a7 as MessageStatus, a8 as ReportGenerateBodyHumanKwargs, a9 as ReportSelectedArtifact, aa as ReportSelectedTemplate, ab as SearchThreadsOptions, ac as SendMessageOptions, ad as TextBlock, ae as ThreadDirectory, af as ThreadDirectoryOptions, ag as ThreadRuntime, ah as ThreadRuntimeOptions, ai as ThreadsPagination, aj as ToolRun, ak as TurnEffect, al as XmlAttrs, am as buildHitlDecisions, an as buildInterruptReviewBlock, ao as buildRejectDecisions, ap as buildRunDirectLatencyMs, aq as buildRunInfo, ar as createChatCore, as as createThreadRuntime, at as getAllowedDecisions, au as isSameArgs, av as normalizeForCompare, aw as parseHitlPayload, ax as pickRunMessageMetadata, ay as sliceBodySegments } from './ChatCore-cSM1HmXp.cjs';
|
|
3
3
|
import { Message, Interrupt } from '@langchain/langgraph-sdk';
|
|
4
|
-
import {
|
|
4
|
+
import { ShallowRef, ComputedRef } from 'vue';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 项目内消息视图类型(评审 C5):LangGraph Message union + 消息链路实际依赖的自由字段,
|
|
@@ -34,68 +34,48 @@ declare const DEFAULT_MAX_CONCURRENT_STREAMS = 3;
|
|
|
34
34
|
/** 默认 assistantId(web 为 import.meta.env.VITE_LANGGRAPH_ASSISTANT_ID 的兜底值) */
|
|
35
35
|
declare const DEFAULT_ASSISTANT_ID = "lead_agent";
|
|
36
36
|
|
|
37
|
-
/** SDK client.runs.stream yield 的单条 SSE 事件(解码后形态) */
|
|
38
|
-
interface StreamEvent {
|
|
39
|
-
event: string;
|
|
40
|
-
data: any;
|
|
41
|
-
}
|
|
42
|
-
/** 发起一次 run 流的上行 payload(web 端 = client.runs.stream 的第三参) */
|
|
43
|
-
interface RunStreamPayload {
|
|
44
|
-
input?: unknown;
|
|
45
|
-
command?: {
|
|
46
|
-
resume?: unknown;
|
|
47
|
-
};
|
|
48
|
-
config?: Record<string, unknown>;
|
|
49
|
-
onDisconnect?: 'continue' | 'cancel';
|
|
50
|
-
streamMode?: string[];
|
|
51
|
-
onRunCreated?: (params: {
|
|
52
|
-
run_id: string;
|
|
53
|
-
thread_id?: string;
|
|
54
|
-
}) => void;
|
|
55
|
-
[key: string]: unknown;
|
|
56
|
-
}
|
|
57
|
-
/** 平台注入的网络源:HTTP/SSE 收发由 adapter 实现,core 只消费解码后的事件流 */
|
|
58
|
-
interface StreamEventSource {
|
|
59
|
-
/** 发起一次 run 流(web 端 = client.runs.stream) */
|
|
60
|
-
openRunStream(payload: RunStreamPayload, signal: AbortSignal): AsyncIterable<StreamEvent>;
|
|
61
|
-
/** 加入既有 run 的流(web 端 = client.runs.joinStream) */
|
|
62
|
-
openJoinStream(runId: string, lastEventId: string, signal: AbortSignal): AsyncIterable<StreamEvent>;
|
|
63
|
-
}
|
|
64
|
-
declare function createStreamPump(source: StreamEventSource, options: CreateStreamOptions & {
|
|
65
|
-
assistantId: string;
|
|
66
|
-
threadId: string;
|
|
67
|
-
}): ChatStreamHandle;
|
|
68
|
-
|
|
69
37
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* 指令语义:
|
|
73
|
-
* - upsert / remove / clear / prepend:消息集合维护;定位一律按消息 id,禁止携带数组 index
|
|
74
|
-
* (翻页 prepend 使 index 全局失效)
|
|
75
|
-
* - finalize:流结束定稿
|
|
76
|
-
* - runId 已在指令产出前盖章进消息本体(信封盖章),消费端只读不再推导
|
|
38
|
+
* sxin AST 节点类型定义
|
|
77
39
|
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
40
|
+
* 独立成文件以打破 sxin-ast ↔ sxin-ast-collect 的模块环:
|
|
41
|
+
* tokenizer 与文本提取均只依赖本文件的类型,互不引用。
|
|
80
42
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
43
|
+
/** 文本节点:原文切片 */
|
|
44
|
+
interface TextNode {
|
|
45
|
+
type: 'text';
|
|
46
|
+
/** 原文内容 */
|
|
47
|
+
value: string;
|
|
48
|
+
/** 在原文中的起始偏移(含) */
|
|
49
|
+
start: number;
|
|
50
|
+
/** 在原文中的结束偏移(不含) */
|
|
51
|
+
end: number;
|
|
52
|
+
}
|
|
53
|
+
/** 注释节点:<!-- ... --> */
|
|
54
|
+
interface CommentNode {
|
|
55
|
+
type: 'comment';
|
|
56
|
+
/** 注释内部内容(不含 <!-- 与 -->) */
|
|
57
|
+
content: string;
|
|
58
|
+
start: number;
|
|
59
|
+
end: number;
|
|
60
|
+
}
|
|
61
|
+
/** 元素节点:<tag attrs>children</tag> 或 <tag attrs/> */
|
|
62
|
+
interface ElementNode {
|
|
63
|
+
type: 'element';
|
|
64
|
+
/** 标签名(统一小写,大小写不敏感) */
|
|
65
|
+
tagName: string;
|
|
66
|
+
/** 解析后的属性对象(key/value 均经 formatStr 格式化) */
|
|
67
|
+
attrs: Record<string, string>;
|
|
68
|
+
/** 子节点(已闭合或自闭合标签才有内容;未闭合标签延伸到 EOF) */
|
|
69
|
+
children: SxinNode[];
|
|
70
|
+
/** 是否自闭合(<tag/>) */
|
|
71
|
+
selfClosing: boolean;
|
|
72
|
+
/** 是否有配对闭合标签;false 表示流式截断或 LLM 少输出闭合标签 */
|
|
73
|
+
closed: boolean;
|
|
74
|
+
start: number;
|
|
75
|
+
/** closed=false 时等于原文长度 */
|
|
76
|
+
end: number;
|
|
77
|
+
}
|
|
78
|
+
type SxinNode = TextNode | CommentNode | ElementNode;
|
|
99
79
|
|
|
100
80
|
/**
|
|
101
81
|
* sxin-ast-attrs.ts — sxin 标签开口内的属性串解析
|
|
@@ -223,6 +203,9 @@ interface ThreadInfo {
|
|
|
223
203
|
/** 澄清/表单/list 的回答:request_id(toolCallId,已剥离 clarification: 前缀)→ 回答内容 */
|
|
224
204
|
clarificationAnswers: Map<string, string>;
|
|
225
205
|
}
|
|
206
|
+
/** 回答值 → 可读文本(展示侧只读文本值;结构化消费方在 rawMessages):
|
|
207
|
+
* 字符串原样;{values:{...}} 拼非空值为 '; ' 分隔;其他对象 JSON 兜底;其余 String()。 */
|
|
208
|
+
declare function formatAnswerText(value: unknown): string;
|
|
226
209
|
/** 全量遍历原始消息,单遍提取所有 thread 级信息 */
|
|
227
210
|
declare function extractThreadInfo(messages: Message[] | undefined | null): ThreadInfo;
|
|
228
211
|
/** human_input_response 负载字段(协议结构,写侧与读侧保持一致) */
|
|
@@ -250,6 +233,22 @@ declare function buildHumanInputResponse(requestId: string, options: HumanInputR
|
|
|
250
233
|
*/
|
|
251
234
|
/** 从 state payload 的 values 中提取 messages */
|
|
252
235
|
declare function extractStateMessages(state: Record<string, unknown> | null | undefined): Message[];
|
|
236
|
+
/**
|
|
237
|
+
* 合并 state 与 page 两源历史消息:以 state 为基准(语义字段全:artifact_meta /
|
|
238
|
+
* human_input_response / hide_from_ui 仅 state 侧存在),page(run-event 全量投影,
|
|
239
|
+
* seq 旧→新)补齐 state 被上下文压缩缺失的头部消息与 checkpoint 滞后的尾部消息。
|
|
240
|
+
*
|
|
241
|
+
* 对齐依据:human/ai 消息两源 id 一致(实测);page 侧普通 tool 消息无 id,按
|
|
242
|
+
* tool_call_id 与 ai 的 tool_calls 配对判定存在性。
|
|
243
|
+
*
|
|
244
|
+
* 返回:合并后的完整消息序列(旧→新)。page 为空返回 state;state 为空返回 page;
|
|
245
|
+
* 完全无 id 锚点(异常场景)降级返回 page(既有「page 权威」行为)。
|
|
246
|
+
*
|
|
247
|
+
* @deprecated 2026-11 语义层分离后 web 编排退役:消息层 page 权威直出,语义字段由
|
|
248
|
+
* SemanticOverlay 按 id 挂载(runtime/semantic-overlay.ts)。core/bridge/adapter.ts 与
|
|
249
|
+
* sdk-demo 迁移后删除本函数及 parser/thread.test.ts。
|
|
250
|
+
*/
|
|
251
|
+
declare function mergeStateWithPageMessages(stateMessages: Message[], pageMessages: Message[]): Message[];
|
|
253
252
|
/** 确保 runs map 中存在指定 runId 的 running 状态 RunInfo */
|
|
254
253
|
declare function ensureRunningRun(runMap: Map<string, RunInfo>, runId: string, threadId: string | null): RunInfo;
|
|
255
254
|
|
|
@@ -271,6 +270,17 @@ declare function extractRunEnrichment(messages: RunMessage[]): Map<string, {
|
|
|
271
270
|
feedback?: unknown;
|
|
272
271
|
latencyMs?: number;
|
|
273
272
|
}>;
|
|
273
|
+
/**
|
|
274
|
+
* 按消息 id 从原始 page 行回填行级 feedback 到组装后的行(伪 page 专用)。
|
|
275
|
+
* 背景:init 组装路径(fetchThreadInitialValues)以 state 为基准 + stateMessagesToRunMessages
|
|
276
|
+
* 伪 page,行级 feedback(page 行专属)在两次转换中丢失 → enrichFromPage 提取不到
|
|
277
|
+
* → runsMap.feedback 恒 undefined → 点赞/点踩历史不回显。与 mergeStateWithPageMessages
|
|
278
|
+
* 的 runId 盖章回填同一语义(page 是行级归属字段的权威源)。
|
|
279
|
+
* 纯函数:未命中/无 id 的行保持原引用;target 已有 feedback 不覆盖;入参不改。
|
|
280
|
+
*
|
|
281
|
+
* @deprecated 2026-11 语义层分离后 web 编排退役(page 直出行级字段天然在,无伪 page 装配)。
|
|
282
|
+
*/
|
|
283
|
+
declare function backfillRunMessageFeedback(source: RunMessage[], target: RunMessage[]): RunMessage[];
|
|
274
284
|
/** RunMessage[] → LangGraphMessage[](浅拷贝 content 并注入 runId 字段,供 initialValues 与 builder 消费) */
|
|
275
285
|
declare function runMessagesToMessages(messages: RunMessage[]): Message[];
|
|
276
286
|
/**
|
|
@@ -477,183 +487,571 @@ declare function isReportBodyHtml(content: string): boolean;
|
|
|
477
487
|
declare function extractReportTitleFromBody(content: string, fallback?: string): string;
|
|
478
488
|
|
|
479
489
|
/**
|
|
480
|
-
*
|
|
481
|
-
* runId 不在此推导——已在 protocol 信封盖章 / page 行自带,fold 只读消息本体;
|
|
482
|
-
* 组 status 也不在 fold 时定死:fold 只按消息序列产出组,status 由 viewmodel 依
|
|
483
|
-
* store.streamStatus 判定(见 group-materializer / viewmodel)。
|
|
490
|
+
* runtime/semantic-overlay.ts — 语义覆盖层(REST 数据层「两层权威」设计的语义层)。
|
|
484
491
|
*
|
|
485
|
-
*
|
|
492
|
+
* 职责(与消息层 page 权威互补,永不互相覆盖):
|
|
493
|
+
* - 权威源:/state(checkpoint),承载「仅 state 侧存在」的语义字段;
|
|
494
|
+
* - 提取:state.values.messages → 按消息 id 挂载的 SemanticPatch;
|
|
495
|
+
* - 应用:patch 回写消息副本(正文/keywords),渲染管线不感知数据源;
|
|
496
|
+
* - 失效:beginStream 时 invalidate——旧 checkpoint 的语义不适用于新一轮流;
|
|
497
|
+
* - 幂等:applyState 全量替换语义层;applySemanticPatches 多次应用零副作用。
|
|
498
|
+
*
|
|
499
|
+
* 背景:重构前消息基准在 state/page 间摇摆(init 用 state 基准、流后 page 权威
|
|
500
|
+
* 替换把语义抹掉、再靠拉 /state 救回),合并规则散落 4 处。citation 两次断点
|
|
501
|
+
*(兜底守卫误判 + finalize 索引丢失致回填 update 被静默丢弃)都源于此结构。
|
|
486
502
|
*/
|
|
487
503
|
|
|
504
|
+
/** 单条消息的语义补丁(全部可选;缺失字段不动本地消息) */
|
|
505
|
+
interface SemanticPatch {
|
|
506
|
+
/** 消息 id(对齐键) */
|
|
507
|
+
id: string;
|
|
508
|
+
/** citation 正文版本(state 侧 content,含 checkpoint 注入的 sxin-item 标签) */
|
|
509
|
+
content?: unknown;
|
|
510
|
+
/** additional_kwargs 补丁(hide_from_ui / human_input_response 等,合并缺失字段) */
|
|
511
|
+
additional_kwargs?: Record<string, unknown>;
|
|
512
|
+
}
|
|
513
|
+
interface SemanticOverlay {
|
|
514
|
+
/** 从完整 /state 响应提取语义层(全量替换,幂等) */
|
|
515
|
+
applyState(state: Record<string, unknown> | null | undefined): {
|
|
516
|
+
patched: number;
|
|
517
|
+
citedIds: string[];
|
|
518
|
+
};
|
|
519
|
+
/** 当前语义补丁(只读副本) */
|
|
520
|
+
getPatches(): Map<string, SemanticPatch>;
|
|
521
|
+
/** 新流开始:语义层失效(旧 checkpoint 语义不适用于本轮流) */
|
|
522
|
+
invalidate(): void;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* 从消息数组提取语义补丁(纯函数)。
|
|
526
|
+
* overlay.applyState 与流内 values 快照增量(finalizeAfterStream)共用同一提取规则。
|
|
527
|
+
*/
|
|
528
|
+
declare function extractSemanticPatchesFromMessages(messages: Message[]): Map<string, SemanticPatch>;
|
|
529
|
+
/**
|
|
530
|
+
* 创建单线程语义覆盖层(纯数据,零 Vue)。
|
|
531
|
+
* patches 按 id 挂载;applyState 全量替换语义(新 checkpoint 覆盖旧语义)。
|
|
532
|
+
*/
|
|
533
|
+
declare function createSemanticOverlay(): SemanticOverlay;
|
|
488
534
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
535
|
+
* 把语义补丁应用到消息数组(纯函数,新数组;入参不改)。
|
|
536
|
+
* 用途:init 种子——hydrate 前应用,materializer 首次构建时 hidden human 等
|
|
537
|
+
* 判定拿到的就是语义版消息。
|
|
538
|
+
* 规则:citation 正文仅当「本地不含且补丁含」时替换(幂等,普通对话零 delta);
|
|
539
|
+
* kwargs 只补本地缺失的键,不动已有值。
|
|
491
540
|
*/
|
|
492
|
-
declare function
|
|
541
|
+
declare function applySemanticPatches(messages: Message[], patches: ReadonlyMap<string, SemanticPatch>): Message[];
|
|
542
|
+
|
|
543
|
+
interface RuntimeMaterializerOptions {
|
|
544
|
+
/** 数字孪生附件识别(与旧 twin 配置同形) */
|
|
545
|
+
twin?: {
|
|
546
|
+
enabled?: boolean;
|
|
547
|
+
toolPrefix?: string;
|
|
548
|
+
};
|
|
549
|
+
/** 当前流 runId 获取(metadata 首帧权威值):段级 runId 兜底源,透传 materializer */
|
|
550
|
+
getStreamRunId?: () => string | null;
|
|
551
|
+
}
|
|
552
|
+
interface RuntimeMaterializer {
|
|
553
|
+
/** 物化器原始视图(shallowRef,发布走引用替换) */
|
|
554
|
+
readonly view: ShallowRef<ThreadView>;
|
|
555
|
+
/** turns 派生(UI v-for 直接消费) */
|
|
556
|
+
readonly turns: ComputedRef<Turn[]>;
|
|
557
|
+
/** R11 投影:尾 turn 尾 ai 段最后一个未答 ask_clarification(含乐观答案覆盖) */
|
|
558
|
+
readonly activeClarification: ComputedRef<ClarificationRecord | undefined>;
|
|
559
|
+
/** 澄清乐观答案覆盖层(callId → answer;C6 自持有,set 即触发投影重算) */
|
|
560
|
+
readonly answers: ComputedRef<ReadonlyMap<string, string>>;
|
|
561
|
+
setClarificationAnswer(toolCallId: string, answer: string): void;
|
|
562
|
+
clearClarificationAnswers(): void;
|
|
563
|
+
/** 当前消息基线(reconcile/乐观写入的合成原料) */
|
|
564
|
+
lastMessages(): readonly Message[];
|
|
565
|
+
hydrate(seed: Message[]): void;
|
|
566
|
+
applySnapshot(messages: Message[]): void;
|
|
567
|
+
finalize(): void;
|
|
568
|
+
/** C2 定点装饰(feedback 盖章/附件注入):透传 materializer.decorate */
|
|
569
|
+
decorate(messageId: string, patch: DecoratePatch): void;
|
|
570
|
+
/** R6:prepend 旧页全量重建(materializer seed);流中守卫命中返回 false */
|
|
571
|
+
prependHistory(older: Message[]): boolean;
|
|
572
|
+
/** 流后对账:page 按 id 替换引用 → update delta(lastMessages 顺序不变) */
|
|
573
|
+
reconcileWithPage(pageMessages: Message[]): void;
|
|
574
|
+
/** R5:API 路径乐观 turn(合成 human+ai 消息走 applySnapshot;rule 4 判据) */
|
|
575
|
+
addOptimisticTurn(body: string): void;
|
|
576
|
+
/** R5:更新指定 ai 段正文(合成同 id 新引用 → update delta) */
|
|
577
|
+
updateAiBody(aiId: string, body: string): void;
|
|
578
|
+
/**
|
|
579
|
+
* 语义补丁应用(语义层通道路径):SemanticOverlay 提取的 patch 按消息 id 对齐
|
|
580
|
+
* 回写本地消息副本(正文/keywords),page 对账不动语义层——见 semantic-overlay.ts。
|
|
581
|
+
* 幂等:本地已含 citation / 已有语义键 → 零 delta(同引用,跳过 feed)。
|
|
582
|
+
*/
|
|
583
|
+
applySemanticPatch(patches: ReadonlyMap<string, SemanticPatch>): void;
|
|
584
|
+
}
|
|
585
|
+
declare function createRuntimeMaterializer(options: RuntimeMaterializerOptions): RuntimeMaterializer;
|
|
493
586
|
|
|
494
587
|
/**
|
|
495
|
-
*
|
|
496
|
-
*
|
|
588
|
+
* 桥接协议的结构类型(core 包权威定义,自包含、零运行时依赖)。
|
|
589
|
+
*
|
|
590
|
+
* 与 @langchain/vue v2 流式控制器的 AgentServerAdapter 契约形状对齐:
|
|
591
|
+
* 控制器只按结构调用(send/openEventStream/getState/events),
|
|
592
|
+
* 只要形状一致即可通过 TS 检查并正常运行,无需依赖 @langchain/protocol 包。
|
|
497
593
|
*/
|
|
594
|
+
/** 服务端 → 客户端:命令成功应答(run.start 返回 run_id) */
|
|
595
|
+
interface CommandResponse {
|
|
596
|
+
type: 'success';
|
|
597
|
+
id: number;
|
|
598
|
+
result: Record<string, unknown>;
|
|
599
|
+
}
|
|
600
|
+
/** 服务端 → 客户端:命令失败应答 */
|
|
601
|
+
interface ErrorResponse {
|
|
602
|
+
type: 'error';
|
|
603
|
+
id: number | null;
|
|
604
|
+
error: string;
|
|
605
|
+
message: string;
|
|
606
|
+
}
|
|
607
|
+
/** 服务端 → 客户端:协议事件(values / messages / tools / lifecycle / input.requested 等) */
|
|
608
|
+
interface ProtocolEvent {
|
|
609
|
+
type: 'event';
|
|
610
|
+
/** 去重/重连用的事件 ID(由事件中枢统一分配) */
|
|
611
|
+
event_id?: string;
|
|
612
|
+
/** 单调递增序号(由事件中枢统一分配) */
|
|
613
|
+
seq?: number;
|
|
614
|
+
method: string;
|
|
615
|
+
params: Record<string, unknown>;
|
|
616
|
+
}
|
|
617
|
+
/** 协议线上消息 = 三种信封之一 */
|
|
618
|
+
type ProtocolMessage = CommandResponse | ErrorResponse | ProtocolEvent;
|
|
619
|
+
/** openEventStream 的过滤参数(桥接实现忽略过滤、全量投递,由客户端按 event_id 去重) */
|
|
620
|
+
interface SubscribeParams {
|
|
621
|
+
channels: string[];
|
|
622
|
+
namespaces?: string[][];
|
|
623
|
+
depth?: number;
|
|
624
|
+
}
|
|
625
|
+
/** openEventStream 返回的订阅句柄(结构与 SDK EventStreamHandle 一致) */
|
|
626
|
+
interface EventStreamHandle {
|
|
627
|
+
events: AsyncIterable<ProtocolMessage>;
|
|
628
|
+
/** 连接建立即视为就绪(桥接模式下事件来自内部缓冲,无需握手) */
|
|
629
|
+
ready: Promise<void>;
|
|
630
|
+
close(): void;
|
|
631
|
+
}
|
|
498
632
|
|
|
499
|
-
/**
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
633
|
+
/**
|
|
634
|
+
* SSE 帧解析器(core 包,Web API 注入版)。
|
|
635
|
+
*
|
|
636
|
+
* D8 剥离:core 不直接依赖 fetch/ReadableStream,字节流通过 ChunkReader 抽象注入,
|
|
637
|
+
* 默认的 fetch 实现(createFetchReaderFactory)由 web 宿主按需使用。
|
|
638
|
+
*
|
|
639
|
+
* deer-flow 的 /runs/stream 帧格式与 LangGraph Platform 对齐:
|
|
640
|
+
* event: values\n data: {...}\n\n
|
|
641
|
+
* 每帧包含 event(流模式名)与 data(JSON 字符串,可能多行)。
|
|
642
|
+
*/
|
|
643
|
+
/** 单个 SSE 帧 */
|
|
644
|
+
interface SseFrame {
|
|
645
|
+
/** event: 行的值(如 values / messages / metadata / custom / error / end) */
|
|
646
|
+
event: string;
|
|
647
|
+
/** id: 行的值(deer-flow 可能携带,仅透传用) */
|
|
505
648
|
id?: string;
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
/**
|
|
510
|
-
interface
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
ver: number;
|
|
519
|
-
}
|
|
520
|
-
/** 流状态(runtime 状态机驱动迁移;viewmodel 据此判定组 status) */
|
|
521
|
-
type StreamStatus = 'idle' | 'streaming' | 'settled';
|
|
522
|
-
interface ThreadStore {
|
|
523
|
-
/** 指令消费(protocol 的 chunk-buffer / runtime 的 pager 对账都汇到这里) */
|
|
524
|
-
applyInstructions(instructions: ThreadInstruction[]): void;
|
|
525
|
-
/** 新流开始(runtime 状态机迁移时调用):流状态 → streaming */
|
|
526
|
-
beginStream(): void;
|
|
527
|
-
/** 实体定位(O(1)) */
|
|
528
|
-
getEntity(id: string): MessageEntity | undefined;
|
|
529
|
-
/** 两段有序 id(page 段前缀 + 流式段后缀;只读引用,变更时整体替换) */
|
|
530
|
-
readonly order: readonly string[];
|
|
531
|
-
/** page 段长度(头部连续 page-source 前缀,= 流式段起始下标;派生值) */
|
|
532
|
-
readonly pageCount: number;
|
|
533
|
-
/** 有序消息数组(惰性构建,version 未变时返回缓存引用) */
|
|
534
|
-
getMessages(): SxinMessage[];
|
|
535
|
-
/** 流状态 */
|
|
536
|
-
readonly streamStatus: StreamStatus;
|
|
537
|
-
/** 流段代次(beginStream 自增):派生层判定「本轮在途消息」用,替代 runId/位置近似 */
|
|
538
|
-
readonly streamEpoch: number;
|
|
539
|
-
/** 变更版本号(消息集合或流状态变更时自增;computed 依赖此值触发重算) */
|
|
540
|
-
readonly version: Ref<number>;
|
|
541
|
-
/** 流状态响应式镜像(与 streamStatus 同步) */
|
|
542
|
-
readonly streamStatusRef: ShallowRef<StreamStatus>;
|
|
543
|
-
}
|
|
544
|
-
/** citation 补丁:viewmodel 构建 blocks 时注入到 AI 消息(content 回补 state 侧标签) */
|
|
545
|
-
interface CitationPatch {
|
|
546
|
-
content?: unknown;
|
|
649
|
+
/** data: 行原文(多行 data 以 \n 拼接,由调用方自行 JSON.parse) */
|
|
650
|
+
data: string;
|
|
651
|
+
}
|
|
652
|
+
/** 底层字节读取器抽象(宿主注入:fetch 流 / XHR / 原生实现) */
|
|
653
|
+
interface ChunkReader {
|
|
654
|
+
read(): Promise<IteratorResult<Uint8Array | string>>;
|
|
655
|
+
/** 提前断开(generator 被 return / 中断时由 parseSseFrames 调用) */
|
|
656
|
+
cancel(): Promise<void>;
|
|
657
|
+
}
|
|
658
|
+
/** fetch 系读取器额外携带响应头(SSE 的 run_id 从 Content-Location 提取) */
|
|
659
|
+
interface FetchChunkReader extends ChunkReader {
|
|
660
|
+
headers?: Headers;
|
|
547
661
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
662
|
+
/** SSE 流请求描述 */
|
|
663
|
+
interface StreamRequest {
|
|
664
|
+
url: string;
|
|
665
|
+
method: string;
|
|
666
|
+
headers: Record<string, string>;
|
|
667
|
+
body?: unknown;
|
|
668
|
+
signal?: AbortSignal;
|
|
669
|
+
}
|
|
670
|
+
/** 读取器工厂:把请求描述变成可逐块读取的字节流 */
|
|
671
|
+
type ChunkReaderFactory = (req: StreamRequest) => Promise<ChunkReader>;
|
|
672
|
+
/**
|
|
673
|
+
* 默认 fetch 读取器工厂(web 宿主使用)。
|
|
674
|
+
* 注入 fetchImpl 便于测试与特殊鉴权通道替换。
|
|
675
|
+
*/
|
|
676
|
+
declare function createFetchReaderFactory(fetchImpl?: typeof fetch): ChunkReaderFactory;
|
|
677
|
+
/**
|
|
678
|
+
* 逐帧解析 SSE。
|
|
679
|
+
* @param reader 字节流读取器(宿主注入)
|
|
680
|
+
*
|
|
681
|
+
* 容错语义:
|
|
682
|
+
* - AbortError(宿主主动中断)安静退出,不向上抛;
|
|
683
|
+
* - generator 被提前 return(for-await break)时 finally 里 cancel 读取器。
|
|
684
|
+
*/
|
|
685
|
+
declare function parseSseFrames(reader: ChunkReader): AsyncGenerator<SseFrame>;
|
|
686
|
+
|
|
687
|
+
/**
|
|
688
|
+
* 事件中枢:协议事件的统一编号、缓冲与订阅分发。
|
|
689
|
+
*
|
|
690
|
+
* 从 demo 版 DeerFlowV1Adapter 中拆出(单一职责:适配器管命令/HTTP,中枢管事件)。
|
|
691
|
+
* 满足 SDK 控制器的 replay contract:新订阅者先从缓冲全量回放(seq 升序),
|
|
692
|
+
* 再收实时推送。
|
|
693
|
+
*
|
|
694
|
+
* 关键回归场景(demo 实测踩过的 teardown 死锁):订阅者挂起在 next() 上时
|
|
695
|
+
* close() 必须唤醒它——否则控制器切换 thread 的 teardown 会停在 dispose 上,
|
|
696
|
+
* 阻断后续 hydrate。
|
|
697
|
+
*/
|
|
698
|
+
|
|
699
|
+
interface EventHub {
|
|
700
|
+
/** 分配 seq/event_id 并分发到缓冲与所有订阅者 */
|
|
701
|
+
publish(event: ProtocolEvent): void;
|
|
702
|
+
/** 打开订阅流:从重放缓冲全量回放后实时推送 */
|
|
703
|
+
open(): EventStreamHandle;
|
|
704
|
+
/** 关闭中枢:唤醒所有挂起的 next()(以 done 结束),此后 publish 静默丢弃 */
|
|
705
|
+
close(): void;
|
|
706
|
+
/** 当前缓冲长度(测试/观测用) */
|
|
707
|
+
readonly bufferedCount: number;
|
|
708
|
+
}
|
|
709
|
+
declare function createEventHub(): EventHub;
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* v1 SSE 帧 → v2 协议事件的翻译器(桥接的核心)。
|
|
713
|
+
*
|
|
714
|
+
* v1 帧(deer-flow /runs/stream):event ∈ {metadata, values, messages, updates,
|
|
715
|
+
* custom, error, end},data 为 JSON 字符串。
|
|
716
|
+
* v2 事件:{ method: 'values'|'messages'|'tools'|'lifecycle'|'input.requested'|'updates'|'custom', params }。
|
|
717
|
+
*
|
|
718
|
+
* 关键语义(对照 SDK dist/stream/projections/messages.js 与 MessageAssembler):
|
|
719
|
+
* - message-start 按消息 id 建条目并清空 blocks,同一 id 绝不能重复发,
|
|
720
|
+
* 增量 token 用 content-block-delta 追加;
|
|
721
|
+
* - 文本块固定占 index 0,tool_call_chunk 依次占用 index 1..N(v1 chunk 内 index 会重复);
|
|
722
|
+
* - messages 事件没有逐消息的 finish 信号,依赖 values 帧对账 + lifecycle 终态清理,
|
|
723
|
+
* 因此 end 帧必须翻译为 lifecycle 终态(completed / interrupted / failed);
|
|
724
|
+
* - v2 控制器只认 input.requested 事件提取 interrupt(不读 values.__interrupt__),
|
|
725
|
+
* 需从 values 帧里合成;
|
|
726
|
+
* - metadata 帧(G10 实测:首帧携带权威 run_id)v2 无对应通道,
|
|
727
|
+
* 经 onMetadata 回调透出给适配器/宿主。
|
|
728
|
+
*/
|
|
729
|
+
|
|
730
|
+
/** 翻译产物的出口:事件中枢的 publish(统一编号 + 分发 + 缓冲) */
|
|
731
|
+
type FramePusher = (event: ProtocolEvent) => void;
|
|
732
|
+
/** metadata 帧透出回调(run_id 权威来源,P0-0 G10 实测结论) */
|
|
733
|
+
type MetadataHandler = (meta: Record<string, unknown>) => void;
|
|
734
|
+
interface V1FrameTranslatorOptions {
|
|
735
|
+
/** 事件出口(必填) */
|
|
736
|
+
push: FramePusher;
|
|
737
|
+
/** metadata 帧回调(可选;缺省忽略该帧) */
|
|
738
|
+
onMetadata?: MetadataHandler;
|
|
739
|
+
}
|
|
740
|
+
declare class V1FrameTranslator {
|
|
741
|
+
/** 当前 run 是否处于 interrupt 待恢复态(end 帧据此选 terminal) */
|
|
742
|
+
interrupted: boolean;
|
|
743
|
+
private pendingInterrupts;
|
|
744
|
+
private lastError;
|
|
745
|
+
/** 已开始消息的组装状态,key = message id */
|
|
746
|
+
private started;
|
|
747
|
+
/** tools 通道防重:已合成 tool-started / tool-finished 的 callId(values 帧全量重放,须去重) */
|
|
748
|
+
private toolsStarted;
|
|
749
|
+
private toolsFinished;
|
|
750
|
+
private readonly push;
|
|
751
|
+
private readonly onMetadata?;
|
|
752
|
+
constructor(options: V1FrameTranslatorOptions);
|
|
753
|
+
/** 分发一个 v1 SSE 帧 */
|
|
754
|
+
handleFrame(event: string, dataRaw: string): void;
|
|
755
|
+
/** values 帧:剥离 __interrupt__ → values 事件 + 合成 input.requested + 合成 tools 通道事件 */
|
|
756
|
+
private handleValues;
|
|
553
757
|
/**
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
758
|
+
* 从 values 快照合成 v2 tools 通道事件(toolCalls 投影只认 tool-started/tool-finished):
|
|
759
|
+
* - AIMessage.tool_calls(args 已完整)→ tool-started
|
|
760
|
+
* - ToolMessage(工具结果)→ tool-finished
|
|
761
|
+
* values 每步全量重放,必须按 callId 防重;AI 消息在 tool 消息之前,顺序天然满足。
|
|
557
762
|
*/
|
|
558
|
-
|
|
559
|
-
/**
|
|
560
|
-
|
|
561
|
-
/**
|
|
562
|
-
|
|
563
|
-
/** 清空回答缓存(clearClarificationAnswers 通路;citation 补丁不受影响) */
|
|
564
|
-
clearAnswers(): void;
|
|
565
|
-
dispose(): void;
|
|
763
|
+
private synthesizeTools;
|
|
764
|
+
/** messages 帧:增量 chunk → message-start(一次) + content-block 事件序列 */
|
|
765
|
+
private handleMessages;
|
|
766
|
+
/** 产出一条未编号的协议事件(编号由事件中枢统一分配) */
|
|
767
|
+
private emit;
|
|
566
768
|
}
|
|
567
769
|
|
|
568
770
|
/**
|
|
569
|
-
*
|
|
570
|
-
*
|
|
571
|
-
*
|
|
572
|
-
*
|
|
573
|
-
*
|
|
771
|
+
* messages/page 全量拉取(deer-flow v1 端点):历史消息的权威数据源。
|
|
772
|
+
*
|
|
773
|
+
* 背景:/state 的 values.messages 是当前 checkpoint state,服务端上下文压缩后
|
|
774
|
+
* 早期消息会缺失;而 page 是 run-event store 投影,保留全量历史。
|
|
775
|
+
* 故 hydrate 时以 page 为准(page 为空回退 state.messages),
|
|
776
|
+
* 对齐业务侧 fetchThreadInitialValues 的「page 权威」语义。
|
|
777
|
+
*
|
|
778
|
+
* P0-0 实测补充(G6):响应消息键是 `data` 而非 `messages`,两者皆兼容。
|
|
779
|
+
*
|
|
780
|
+
* 分页协议:before_seq 值游标(上一页返回的 next_before_seq),缺省拉最新一页;
|
|
781
|
+
* 每页返回新→旧顺序,往前翻的页依次前置,最终组装为旧→新(与业务 pager 一致)。
|
|
574
782
|
*
|
|
575
|
-
*
|
|
576
|
-
* human → 视为已答(回答消息可能不在消息集合中,仅靠回答数据会永久误判待答)。
|
|
783
|
+
* D8 注入:fetchImpl 由宿主传入,core 不直接依赖全局 fetch。
|
|
577
784
|
*/
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* 拉取 thread 全量 page 消息(旧→新)。
|
|
788
|
+
* 任一一页非 2xx / 网络异常 → 返回已拉部分(可能为空,调用方走 state 兜底);
|
|
789
|
+
* 游标缺失但 has_more=true 时终止翻页(hasMore 语义)。
|
|
790
|
+
*/
|
|
791
|
+
declare function fetchAllPageMessages(apiUrl: string, threadId: string, buildHeaders: () => Promise<Record<string, string>>, fetchImpl: typeof fetch): Promise<Message[]>;
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* AgentServerAdapter 的本地结构类型(core 自包含声明,零包依赖)。
|
|
795
|
+
*
|
|
796
|
+
* 与 @langchain/langgraph-sdk/stream 导出的 AgentServerAdapter 形状对齐
|
|
797
|
+
* (v1.15.0,client/stream/transport.d.ts):web 侧把适配器实例传给
|
|
798
|
+
* useStream 时直接满足结构类型,无需 `as unknown as` 断言。
|
|
799
|
+
*
|
|
800
|
+
* 注意:replay contract 由 openEventStream 实现——新订阅必须从 seq=0
|
|
801
|
+
* 收全量(事件中枢缓冲保证),SDK 共享流轮换依赖该语义去重。
|
|
802
|
+
*/
|
|
803
|
+
|
|
804
|
+
/** 协议命令(控制器下发:run.start / input.respond / run.cancel …) */
|
|
805
|
+
interface ProtocolCommand {
|
|
806
|
+
id: number;
|
|
807
|
+
method: string;
|
|
808
|
+
params?: Record<string, any>;
|
|
809
|
+
}
|
|
810
|
+
/** TransportAdapter(AgentServerAdapter 的基座契约) */
|
|
811
|
+
interface TransportAdapter {
|
|
812
|
+
/** 当前绑定的 threadId */
|
|
813
|
+
readonly threadId: string;
|
|
814
|
+
/** 重绑定 thread(框架在懒创建 id 出现/切换 thread 时回调) */
|
|
815
|
+
setThreadId?(threadId: string): void;
|
|
816
|
+
/** 打开底层连接(HTTP/SSE 传输为 no-op) */
|
|
817
|
+
open(): Promise<void>;
|
|
818
|
+
/** 发送命令;可返回即时应答 */
|
|
819
|
+
send(command: ProtocolCommand): Promise<CommandResponse | ErrorResponse | void>;
|
|
820
|
+
/** 单连接共享事件流(WebSocket 形态;SSE 形态返回空流,走 openEventStream) */
|
|
821
|
+
events(): AsyncIterable<ProtocolMessage>;
|
|
822
|
+
/** 打开独立过滤事件流(SSE 形态的真实投递通道) */
|
|
823
|
+
openEventStream?(params: SubscribeParams): EventStreamHandle;
|
|
824
|
+
/** 关闭并释放资源 */
|
|
825
|
+
close(): Promise<void>;
|
|
826
|
+
}
|
|
827
|
+
/** AgentServerAdapter = TransportAdapter + 可选高层能力(state/history) */
|
|
828
|
+
interface AgentServerAdapter extends TransportAdapter {
|
|
829
|
+
/** hydrate 用:绑定的 thread 的最新 checkpoint state;缺省走 client.threads.getState */
|
|
830
|
+
getState?<StateType = unknown>(): Promise<{
|
|
831
|
+
values: StateType;
|
|
832
|
+
next?: unknown;
|
|
833
|
+
tasks?: unknown;
|
|
834
|
+
metadata?: unknown;
|
|
835
|
+
checkpoint?: {
|
|
836
|
+
checkpoint_id?: string;
|
|
837
|
+
} | null;
|
|
838
|
+
parent_checkpoint?: {
|
|
839
|
+
checkpoint_id?: string;
|
|
840
|
+
} | null;
|
|
841
|
+
} | null>;
|
|
842
|
+
/** checkpoint 历史切片(分支/时间旅行 UI 用;缺省则相关 UI no-op) */
|
|
843
|
+
getHistory?<StateType = unknown>(options?: {
|
|
844
|
+
limit?: number;
|
|
845
|
+
}): Promise<Array<{
|
|
846
|
+
values: StateType;
|
|
847
|
+
checkpoint?: {
|
|
848
|
+
checkpoint_id?: string;
|
|
849
|
+
} | null;
|
|
850
|
+
}>>;
|
|
583
851
|
}
|
|
852
|
+
|
|
853
|
+
interface DeerFlowBridgeAdapterOptions {
|
|
854
|
+
/** LangGraph 兼容 API 根地址(如 http://host:2026/api/langgraph) */
|
|
855
|
+
apiUrl: string;
|
|
856
|
+
/** 默认 assistant_id(控制器传 "_" 时兜底) */
|
|
857
|
+
assistantId: string;
|
|
858
|
+
/** 每个请求前构造鉴权头(SSO / CSRF / Bearer),与主应用同一套通道 */
|
|
859
|
+
buildHeaders: () => Promise<Record<string, string>>;
|
|
860
|
+
/** REST 请求实现注入(宿主 fetch;core 不依赖全局 fetch) */
|
|
861
|
+
fetchImpl: typeof fetch;
|
|
862
|
+
/**
|
|
863
|
+
* SSE 字节流工厂注入(缺省 createFetchReaderFactory(fetchImpl))。
|
|
864
|
+
* 返回的读取器可携带响应头(FetchChunkReader.headers),供 run_id 提取。
|
|
865
|
+
*/
|
|
866
|
+
readerFactory?: ChunkReaderFactory;
|
|
867
|
+
/**
|
|
868
|
+
* 原始 values 帧透出(翻译前,含 __interrupt__ 原样)。
|
|
869
|
+
* 供宿主 values-channel 精确喂入(调和 interrupt / title 提取)——
|
|
870
|
+
* 不用 watch 投影值,避免 hydrate 种子值误消费流事件的 firstEvent 语义。
|
|
871
|
+
*/
|
|
872
|
+
onValuesFrame?(data: Record<string, unknown>): void;
|
|
873
|
+
/**
|
|
874
|
+
* 本轮 run_id 透出(Content-Location 头 / metadata 首帧,G10 权威)。
|
|
875
|
+
* 宿主据此 markStreamRun(runs.currentRunId = 段级 runId 兜底源)——
|
|
876
|
+
* onCreated 依赖 Content-Location 响应头可读(CORS 未暴露时为空),
|
|
877
|
+
* metadata 帧是 SSE 首帧(先于所有消息帧),是 submit 主路径时序安全的兜底通道。
|
|
878
|
+
*/
|
|
879
|
+
onRunId?(runId: string): void;
|
|
880
|
+
/**
|
|
881
|
+
* 外部读取当前 threadId。关键:hydrate 发生在 transport 首次绑定
|
|
882
|
+
* (setThreadId,随首次 submit 的 threads.stream 工厂调用)之前,
|
|
883
|
+
* 适配器自身状态此刻还是空串,必须从 useStream 侧的 threadId ref 取。
|
|
884
|
+
*/
|
|
885
|
+
getThreadId: () => string;
|
|
886
|
+
}
|
|
887
|
+
declare class DeerFlowBridgeAdapter implements AgentServerAdapter {
|
|
888
|
+
#private;
|
|
889
|
+
/** 当前生效的 threadId:外部 getter 优先(hydrate 早于 transport 绑定),setThreadId 兜底 */
|
|
890
|
+
get threadId(): string;
|
|
891
|
+
/** metadata 帧记录的 run_id(首帧权威,G10;Content-Location 优先时两者一致) */
|
|
892
|
+
get lastRunId(): string;
|
|
893
|
+
/** 线程是否处于 interrupt 待恢复态(下一次 send 自动走 command.resume;观测/测试用) */
|
|
894
|
+
get isInterrupted(): boolean;
|
|
895
|
+
private readonly apiUrl;
|
|
896
|
+
private readonly assistantId;
|
|
897
|
+
private readonly buildHeaders;
|
|
898
|
+
private readonly fetchImpl;
|
|
899
|
+
private readonly onValuesFrame?;
|
|
900
|
+
private readonly onRunId?;
|
|
901
|
+
/** 事件中枢:统一编号 + 缓冲重放 + 订阅分发 */
|
|
902
|
+
private readonly hub;
|
|
903
|
+
/** 已确认存在的 thread(避免重复预创建) */
|
|
904
|
+
private knownThreads;
|
|
905
|
+
/** 线程处于 interrupt 待恢复态:下一个 run.start / input.respond 按 resume 翻译 */
|
|
906
|
+
private interrupted;
|
|
907
|
+
private activeAbort;
|
|
908
|
+
private disposed;
|
|
909
|
+
/** run-stream 编排的宿主回调(共享状态经此进出) */
|
|
910
|
+
private readonly streamHost;
|
|
911
|
+
constructor(options: DeerFlowBridgeAdapterOptions);
|
|
912
|
+
/** {@inheritDoc TransportAdapter.setThreadId} */
|
|
913
|
+
setThreadId(threadId: string): void;
|
|
914
|
+
/** HTTP/SSE 传输无握手 */
|
|
915
|
+
open(): Promise<void>;
|
|
916
|
+
/** 释放资源:断开进行中的 SSE,关闭事件中枢(唤醒挂起订阅) */
|
|
917
|
+
close(): Promise<void>;
|
|
918
|
+
/** SSE 模式下 events() 为空流,真实投递走 openEventStream */
|
|
919
|
+
events(): AsyncGenerator<ProtocolMessage>;
|
|
920
|
+
/** 发送协议命令:run.start / input.respond → v1 runs.stream,其余拒绝 */
|
|
921
|
+
send(command: ProtocolCommand): Promise<CommandResponse | ErrorResponse | void>;
|
|
922
|
+
/** 打开订阅流:事件中枢重放缓冲全量回放后实时推送 */
|
|
923
|
+
openEventStream(_params: SubscribeParams): EventStreamHandle;
|
|
924
|
+
/** hydrate 用:/state 与 messages/page 全量并发;messages 以 state 为基准(语义字段全),
|
|
925
|
+
* page(run-event 全量投影)补齐压缩缺失的头部/尾部——与 transport 的
|
|
926
|
+
* fetchThreadInitialValues 同一合并函数,保证 SDK getState 快照与物化器基线一致
|
|
927
|
+
* (不一致会让 watch(stream.messages) 的 diff 把快照差异误追加到对话尾部) */
|
|
928
|
+
getState<T = unknown>(): Promise<{
|
|
929
|
+
values: T;
|
|
930
|
+
tasks?: unknown;
|
|
931
|
+
next?: unknown;
|
|
932
|
+
checkpoint: null;
|
|
933
|
+
} | null>;
|
|
934
|
+
/** run.start / input.respond 落点:预创建 thread 后交 run-stream 打开 SSE 泵帧 */
|
|
935
|
+
private startRun;
|
|
936
|
+
/** 统一 REST 请求入口:注入鉴权头 + cookie */
|
|
937
|
+
private request;
|
|
938
|
+
/** 预创建 thread(deer-flow POST /threads 支持客户端指定 id 且幂等) */
|
|
939
|
+
private ensureThread;
|
|
940
|
+
/** 构造命令错误应答 */
|
|
941
|
+
private cmdError;
|
|
942
|
+
/** 推送 lifecycle 事件(running / failed) */
|
|
943
|
+
private lifecycle;
|
|
944
|
+
}
|
|
945
|
+
|
|
584
946
|
/**
|
|
585
|
-
*
|
|
586
|
-
*
|
|
587
|
-
*
|
|
947
|
+
* materializer/diff — §6.1 快照 diff(C6:protocol/snapshot-diff 内迁入本文件)。
|
|
948
|
+
* 三规则(G-ref 实测背书):同引用零 delta;引用变化 → upsert(id 在基线为 update,否则 append);
|
|
949
|
+
* 基线有而快照无的 id → tail-remove。基线由本模块持有(diff 状态是物化器内部状态)。
|
|
588
950
|
*/
|
|
589
|
-
declare function isClarificationAnswered(block: ClarificationBlockLike | undefined | null, answers: Map<string, string>): boolean;
|
|
590
|
-
/** 是否澄清类块(ask-clarification 专用块或 form(ask_clarification 来源)) */
|
|
591
|
-
declare function isClarificationBlock(block: {
|
|
592
|
-
type: string;
|
|
593
|
-
source?: string;
|
|
594
|
-
}): boolean;
|
|
595
951
|
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
952
|
+
/** §6.1 消息增量 */
|
|
953
|
+
type MessageDelta = {
|
|
954
|
+
type: 'append';
|
|
955
|
+
message: Message;
|
|
956
|
+
} | {
|
|
957
|
+
type: 'update';
|
|
958
|
+
id: string;
|
|
959
|
+
message: Message;
|
|
960
|
+
} | {
|
|
961
|
+
type: 'tail-remove';
|
|
962
|
+
id: string;
|
|
963
|
+
};
|
|
964
|
+
interface MessageDiff {
|
|
965
|
+
/** 喂入新快照,返回相对上次快照的类型化增量 */
|
|
966
|
+
apply(next: Message[]): MessageDelta[];
|
|
967
|
+
/** 重置基线(thread 切换 / hydrate 前) */
|
|
968
|
+
reset(): void;
|
|
969
|
+
/** 当前基线消息数(测试/观测用) */
|
|
970
|
+
readonly baselineSize: number;
|
|
606
971
|
}
|
|
607
|
-
declare function
|
|
972
|
+
declare function createMessageDiff(): MessageDiff;
|
|
608
973
|
|
|
609
974
|
/**
|
|
610
|
-
*
|
|
975
|
+
* materializer/turn-machine — §6.2 turn 归属状态机:状态壳 + 密封 + 增量分发。
|
|
976
|
+
*
|
|
977
|
+
* 规则(纯数据判据 f(消息自身字段),D5):
|
|
978
|
+
* 1. human + hide_from_ui → 不开 turn;提取 human_input_response,按 request_id(toolCallId)
|
|
979
|
+
* 定位 clarification 所在 turn 回填 answer(D6;跨轮 resume 场景定位前轮,密封态 COW)
|
|
980
|
+
* 2. human 可见 → 闭合所有开放段,开新 turn(id = human 消息 id)
|
|
981
|
+
* 3. ai/tool → 读 metadata.run_id:同开放段 runId 并入(delta/工具回填);
|
|
982
|
+
* 不同 → 闭合当前 ai 段(密封即切片),开新 ai 段挂当前 turn(resume 续写)
|
|
983
|
+
* 4. 乐观消息(流中新增 + 无 runId + kwargs 空,G-opt 实测判据)→ 开 optimistic 占位 turn;
|
|
984
|
+
* 吸收 = 同 id update 塌缩确认(placeholder-absorbed);回退 = tail-remove 命中占位
|
|
985
|
+
* (placeholder-dropped)。seed(hydrate)路径不判乐观(历史消息无占位)。
|
|
611
986
|
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
* 构建器显式持有并作为公开职责声明;唯一真状态(乐观 extras / 外部补丁,UI 指令写入)
|
|
615
|
-
* 在 ViewModelState。缓存有意为普通结构(Map/对象)——computed 内读写不触发响应式,
|
|
616
|
-
* 无循环风险(构建器生命周期 = derived-groups = runtime;线程切换新建 runtime 即新建缓存)。
|
|
987
|
+
* 规则的消息级实现(appendHuman/appendHiddenHuman/appendAi/applyToolMessage)拆在
|
|
988
|
+
* turn-machine-rules.ts(单文件 ≤300 行红线),经 TurnRulesCtx 注入本壳原语。
|
|
617
989
|
*
|
|
618
|
-
*
|
|
619
|
-
*
|
|
620
|
-
* blocks:流式组为空 + 预计算 streamingText(UI 直读);定稿组一次构建。
|
|
990
|
+
* 引用稳定性:已密封 turn 不被原地改写——密封后的修改走 COW(新 turn 对象替换数组元素);
|
|
991
|
+
* 变更路径上的 turn/ai 一律换新对象。机器是普通对象状态(非响应式),响应式发布由门面负责。
|
|
621
992
|
*/
|
|
622
993
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
/** 数字孪生 block 配置(缺省关闭) */
|
|
633
|
-
twin?: BlockBuilderLiteOptions['twin'];
|
|
634
|
-
}
|
|
635
|
-
/** 派生结果:装饰合并后的组列表 + store 源组(pruneExtras/patchGroup 消费) */
|
|
636
|
-
interface BuildGroupsResult {
|
|
637
|
-
/** 装饰合并后的完整组列表(UI 消费) */
|
|
638
|
-
groups: GroupMessage[];
|
|
639
|
-
/** store 源组(不含 extras 与补丁——二者在装饰链第 1 步才应用;供 pruneExtras 计数与 patchGroup 查找) */
|
|
640
|
-
sourceGroups: GroupMessage[];
|
|
994
|
+
/** 机器配置(C1:twin 附件识别) */
|
|
995
|
+
interface TurnMachineOptions {
|
|
996
|
+
twin?: {
|
|
997
|
+
enabled?: boolean;
|
|
998
|
+
toolPrefix?: string;
|
|
999
|
+
};
|
|
1000
|
+
/** 当前流 runId 获取(盖兜底源):流式 tuple 消息本体无 runId(权威值在 metadata 首帧),
|
|
1001
|
+
* 段级 runId 赋值/回填时兜底——对齐旧管线 chunk-buffer 的 stampRunId 信封盖章语义 */
|
|
1002
|
+
getStreamRunId?: () => string | null;
|
|
641
1003
|
}
|
|
1004
|
+
|
|
642
1005
|
/**
|
|
643
|
-
*
|
|
644
|
-
*
|
|
1006
|
+
* materializer/材料化门面 — §6.5 createMaterializer()。
|
|
1007
|
+
*
|
|
1008
|
+
* SDK 消息快照 → 响应式 ThreadView:
|
|
1009
|
+
* - hydrate(seed):全量种子(换线程/整体重置,O(n) 一次性);同时 priming diff 基线
|
|
1010
|
+
* - applySnapshot(messages):稳态增量(diff → 状态机 → 按需发布新 view)
|
|
1011
|
+
* - finalize():流结束密封开放段(P1 增补,§6.5 三方法之上——密封信号来自
|
|
1012
|
+
* 桥接同步器的 isLoading 下降沿;无此方法尾段永不密封、永不切片)
|
|
1013
|
+
* - reset():清场
|
|
1014
|
+
*
|
|
1015
|
+
* 发布语义:effects 非空才发新 view({ turns: [...state.turns] } 浅拷贝包新对象);
|
|
1016
|
+
* 已密封 turn 对象引用稳定(机器 COW 保证),零 delta 时 view 引用不变(消费方 computed 不重算)。
|
|
645
1017
|
*/
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
1018
|
+
|
|
1019
|
+
interface Materializer {
|
|
1020
|
+
/** 响应式视图(shallowRef:发布走引用替换,元素级引用由机器 COW 保持稳定) */
|
|
1021
|
+
readonly view: ShallowRef<ThreadView>;
|
|
1022
|
+
/** 全量种子(hydrate 后全部 turn 密封;diff 基线同步 priming) */
|
|
1023
|
+
hydrate(seed: Message[]): void;
|
|
1024
|
+
/** 稳态增量:快照 diff → 状态机 → 按需发布 */
|
|
1025
|
+
applySnapshot(messages: Message[]): void;
|
|
1026
|
+
/** 流结束:密封开放 ai 段(切片)与开放 turn;幂等 */
|
|
1027
|
+
finalize(): void;
|
|
1028
|
+
/** 定点装饰(R9,feedback 盖章):byMessageId 定位 ai 段 COW 更新;未知 id/human 静默 no-op */
|
|
1029
|
+
decorate(messageId: string, patch: DecoratePatch): void;
|
|
1030
|
+
reset(): void;
|
|
649
1031
|
}
|
|
1032
|
+
declare function createMaterializer(options?: TurnMachineOptions): Materializer;
|
|
1033
|
+
|
|
650
1034
|
/**
|
|
651
|
-
*
|
|
652
|
-
*
|
|
1035
|
+
* extractors — §6.4 tool result → ExtractedPayload 提取(纯函数)。
|
|
1036
|
+
*
|
|
1037
|
+
* 唯一调用点:tool 消息引用变化(diff update 命中 tool 消息)→ 回填 ToolRun.result
|
|
1038
|
+
* 时同步写 extracted。物化器不感知业务语义——澄清卡片/报告附件的数据源在此收敛。
|
|
1039
|
+
*
|
|
1040
|
+
* 提取保守原则:仅识别「结果本体即携带载荷」的形态(JSON 且字段齐全);
|
|
1041
|
+
* 报告产物例外(Bug 3):write_file result 可能是非 JSON 的 "OK" 等回执——此时
|
|
1042
|
+
* 回退从 tool call 本身(artifact_meta / path / description,复用 parser 层
|
|
1043
|
+
* resolveReportArtifactKind 判定)识别 report_outline / report_body,
|
|
1044
|
+
* 产出带 sourcePath 的载荷(内容经 artifact 端点按需拉取)。
|
|
1045
|
+
*/
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* 从 tool result 提取业务载荷;无识别形态时返回 undefined(常态,消费方判空)。
|
|
1049
|
+
*
|
|
1050
|
+
* @param toolCallLike tool call 本体(artifact_meta + args),供 write_file result
|
|
1051
|
+
* 非 JSON 时的报告产物回退判定(resolveReportArtifactKind 取 artifact_meta / args.path /
|
|
1052
|
+
* args.description 三个信号);缺省时回退判定无信号,返回 undefined。
|
|
653
1053
|
*/
|
|
654
|
-
declare function
|
|
655
|
-
/** 生成一条乐观 human GroupMessage(id 独立于服务端,回显后由 pruneExtras 收敛) */
|
|
656
|
-
declare function createOptimisticHuman(text: string, options?: SendMessageOptions): GroupMessage;
|
|
1054
|
+
declare function extractFromToolResult(name: string, result: string, toolCallLike?: unknown): ExtractedPayload | undefined;
|
|
657
1055
|
|
|
658
1056
|
/** __emit 可推进的快照字段(每次整体替换引用) */
|
|
659
1057
|
interface MockStreamSnapshot {
|
|
@@ -714,11 +1112,11 @@ interface MockTransport extends ChatTransport {
|
|
|
714
1112
|
declare function createMockTransport(): MockTransport;
|
|
715
1113
|
|
|
716
1114
|
/**
|
|
717
|
-
* 公开入口:类型层 / transport 契约 /
|
|
718
|
-
*
|
|
719
|
-
* 内部模块(store 装配、
|
|
1115
|
+
* 公开入口:类型层 / transport 契约 / parser 纯函数 /
|
|
1116
|
+
* runtime 运行时 / materializer / core 门面 / bridge / 测试工具。
|
|
1117
|
+
* 内部模块(store 装配、values 通道内部)不对外导出。
|
|
720
1118
|
*/
|
|
721
1119
|
/** 包版本常量,发布时由人工随 package.json 同步(changesets 只改 package.json,此处发版时核对) */
|
|
722
1120
|
declare const CORE_VERSION = "2.0.0";
|
|
723
1121
|
|
|
724
|
-
export {
|
|
1122
|
+
export { type AgentServerAdapter, Block, BlockBuilderOptions, CORE_VERSION, ChatTransport, type ChunkReader, type ChunkReaderFactory, CitationItem, ClarificationRecord, type CommandResponse, type CommentNode, CreateStreamOptions, DEFAULT_ASSISTANT_ID, DEFAULT_MAX_CACHED_SESSIONS, DEFAULT_MAX_CONCURRENT_STREAMS, DecoratePatch, DeerFlowBridgeAdapter, type DeerFlowBridgeAdapterOptions, type ElementNode, type ErrorResponse, type EventHub, type EventStreamHandle, ExtractedPayload, type FetchChunkReader, type FramePusher, GroupMessageTool, HitlResponse, type HumanInputResponseOptions, IKwargs, INJECTED_TAGS, type KnowledgeItem, MESSAGE_TYPE_NORMALIZE, type Materializer, type MessageDelta, type MessageDiff, MessageTokenUsage, MessagesPage, type MetadataHandler, type MockStreamSnapshot, type MockTransport, type MockTransportCalls, OUTLINE_MAX_LEVEL, OutlineChapter, OutlineSubChapter, type ProtocolCommand, type ProtocolEvent, type ProtocolMessage, ReportBodyPayload, ReportOutlinePayload, RunInfo, RunMessage, type RuntimeMaterializer, type RuntimeMaterializerOptions, type SemanticOverlay, type SemanticPatch, type SseFrame, type StreamRequest, StreamSubmitInput, StreamSubmitOptions, type SubscribeParams, type SxinMessage, type SxinNode, type TextNode, type ThreadInfo, ThreadInitialValues, ThreadTokenUsage, ThreadView, type TransportAdapter, Turn, V1FrameTranslator, type V1FrameTranslatorOptions, applySemanticPatches, backfillRunMessageFeedback, buildArtifactFiles, buildHumanInputResponse, buildReportBodyPayload, buildReportOutlinePayload, collectTextKeepSxin, collectVisibleText, createEmptySubChapter, createEventHub, createFetchReaderFactory, createMaterializer, createMessageDiff, createMockTransport, createRuntimeMaterializer, createSemanticOverlay, deriveToolsFromMessages, ensureOutlineContentThemes, ensureRunningRun, extractAiContentFromMessage, extractBlocksFromContent, extractBlocksFromNodes, extractCitationAnchors, extractCitationAnchorsFromSpec, extractFromToolResult, extractReportTitleFromBody, extractRunEnrichment, extractSemanticPatchesFromMessages, extractStateMessages, extractTextFromMessage, extractThreadInfo, extractTitleFromArtifactPath, extractUserMessageTexts, fetchAllPageMessages, formatAnswerText, formatStr, genBlockId, genMessageId, getHumanMessageBaseId, getToolCallArtifactMeta, isAiMessage, isHiddenUserMessage, isHumanMessage, isInjectedElement, isReportBodyHtml, isToolMessage, isVisibleUserMessage, isWriteFileToolName, mergeStateWithPageMessages, normalizeMessageType, normalizeOutline, normalizeUsage, parseHumanMessageId, parseKnowledgeItemsFromValue, parseMarkdownToOutline, parseSseFrames, readMessageContentText, resolveReportArtifactKind, runMessagesToMessages, simpleHash, stateMessagesToRunMessages, toRunMessage, tokenizeSxin, validateOutline };
|