@rei-standard/amsg-shared 0.4.0-next.3 → 0.4.0-next.6
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 +35 -2
- package/dist/index.cjs +314 -7
- package/dist/index.d.cts +93 -8
- package/dist/index.d.ts +93 -8
- package/dist/index.mjs +314 -7
- package/dist/llm-call.d.cts +24 -23
- package/dist/llm-call.d.ts +24 -23
- package/dist/multipart.d.cts +26 -0
- package/dist/multipart.d.ts +26 -0
- package/dist/protocol.d.cts +36 -0
- package/dist/protocol.d.ts +36 -0
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -154,6 +154,8 @@ export function buildToolRequestPush(args: {
|
|
|
154
154
|
* @param {string} [args.messageSubtype]
|
|
155
155
|
* @param {Object} [args.metadata]
|
|
156
156
|
* @param {NotificationDirective} [args.notification]
|
|
157
|
+
* @param {number} [args.llmStatus] - 上游 LLM 回的 HTTP 状态码(见 ErrorPush)
|
|
158
|
+
* @param {string} [args.providerCode] - provider 自己的错误码(见 ErrorPush)
|
|
157
159
|
* @returns {ErrorPush}
|
|
158
160
|
*/
|
|
159
161
|
export function buildErrorPush(args: {
|
|
@@ -168,7 +170,42 @@ export function buildErrorPush(args: {
|
|
|
168
170
|
messageSubtype?: string;
|
|
169
171
|
metadata?: any;
|
|
170
172
|
notification?: NotificationDirective;
|
|
173
|
+
llmStatus?: number;
|
|
174
|
+
providerCode?: string;
|
|
171
175
|
}): ErrorPush;
|
|
176
|
+
/**
|
|
177
|
+
* Build a {@link ResultPush} —— 宿主自定义的一条结果。
|
|
178
|
+
*
|
|
179
|
+
* 与另外四个 builder 有一处不同:**认识以外的字段原样保留**。结果的形状本来
|
|
180
|
+
* 就由宿主定,白名单式的复制等于把内容删掉一半。库只保证 `messageKind` 与
|
|
181
|
+
* `timestamp` 到位,其余交给宿主。
|
|
182
|
+
*
|
|
183
|
+
* `resultKind` 必填:宿主给这类结果起的名字(`'fire-pack'`、`'ledger-entry'`
|
|
184
|
+
* ……)。客户端有多种结果时按它分流,不用靠「有没有某个字段」去猜是哪一种。
|
|
185
|
+
*
|
|
186
|
+
* @param {Object} args - 整条 payload;下列几个字段有约束,其余原样带过去。
|
|
187
|
+
* @param {MessageType} args.messageType
|
|
188
|
+
* @param {PushSource} args.source
|
|
189
|
+
* @param {string} args.messageId
|
|
190
|
+
* @param {string} args.sessionId
|
|
191
|
+
* @param {string} args.resultKind - 这类结果的名字(宿主自定义)
|
|
192
|
+
* @param {string} [args.timestamp]
|
|
193
|
+
* @param {string} [args.title] - 通知标题的兜底(`notification.title` 优先)
|
|
194
|
+
* @param {string} [args.body] - 通知正文的兜底(`notification.body` 优先)
|
|
195
|
+
* @param {NotificationDirective} [args.notification]
|
|
196
|
+
* @returns {ResultPush}
|
|
197
|
+
*/
|
|
198
|
+
export function buildResultPush(args: {
|
|
199
|
+
messageType: MessageType;
|
|
200
|
+
source: PushSource;
|
|
201
|
+
messageId: string;
|
|
202
|
+
sessionId: string;
|
|
203
|
+
resultKind: string;
|
|
204
|
+
timestamp?: string;
|
|
205
|
+
title?: string;
|
|
206
|
+
body?: string;
|
|
207
|
+
notification?: NotificationDirective;
|
|
208
|
+
}): ResultPush;
|
|
172
209
|
/**
|
|
173
210
|
* Type guard: returns true if the argument is a {@link ContentPush}.
|
|
174
211
|
*
|
|
@@ -197,6 +234,13 @@ export function isToolRequestPush(value: unknown): value is ToolRequestPush;
|
|
|
197
234
|
* @returns {value is ErrorPush}
|
|
198
235
|
*/
|
|
199
236
|
export function isErrorPush(value: unknown): value is ErrorPush;
|
|
237
|
+
/**
|
|
238
|
+
* Type guard: returns true if the argument is a {@link ResultPush}.
|
|
239
|
+
*
|
|
240
|
+
* @param {unknown} value
|
|
241
|
+
* @returns {value is ResultPush}
|
|
242
|
+
*/
|
|
243
|
+
export function isResultPush(value: unknown): value is ResultPush;
|
|
200
244
|
/**
|
|
201
245
|
* Slice a string into UTF-8 byte chunks no larger than `maxBytes`,
|
|
202
246
|
* always cutting at codepoint boundaries (never inside a multi-byte
|
|
@@ -296,6 +340,7 @@ export function stripReasoningTags(content: string): string;
|
|
|
296
340
|
* @property {ChatMessage[]} messages - Including the just-appended assistant turn.
|
|
297
341
|
* @property {unknown} llmResponse - Full LLM response (choices, usage, …).
|
|
298
342
|
* @property {string} llmOutputText - May be '' for pure tool-call responses.
|
|
343
|
+
* @property {Record<string, unknown>|null} usage - `llmResponse.usage` 的直接引用(prompt/completion/total tokens)。响应没带 usage → null。llmResponse 里本来就有,单独提出来是让「记本轮用量」不必依赖响应对象的具体形状。
|
|
299
344
|
* @property {number} iteration - 0-indexed: the round that just finished.
|
|
300
345
|
* @property {Record<string, unknown>} metadata
|
|
301
346
|
* @property {string} contactName
|
|
@@ -391,7 +436,8 @@ export function extractToolCallsFromDecision(decision: unknown): Array<Record<st
|
|
|
391
436
|
* Three orthogonal axes:
|
|
392
437
|
* 1. messageType — how the push was produced (instant / fixed / prompted / auto)
|
|
393
438
|
* 2. messageSubtype — caller's business classification (free-form string)
|
|
394
|
-
* 3. messageKind — what the push carries (content / reasoning / tool_request /
|
|
439
|
+
* 3. messageKind — what the push carries (content / reasoning / tool_request /
|
|
440
|
+
* error / result)
|
|
395
441
|
*
|
|
396
442
|
* Zero runtime dependencies. The package is ESM/CJS dual-published and
|
|
397
443
|
* intentionally has no `dependencies:` entry — every other amsg sub-
|
|
@@ -407,7 +453,7 @@ export function extractToolCallsFromDecision(decision: unknown): Array<Record<st
|
|
|
407
453
|
/**
|
|
408
454
|
* What the push carries. Fixed enum — packages must not add values.
|
|
409
455
|
*
|
|
410
|
-
* @typedef {'content' | 'reasoning' | 'tool_request' | 'error'} MessageKind
|
|
456
|
+
* @typedef {'content' | 'reasoning' | 'tool_request' | 'error' | 'result'} MessageKind
|
|
411
457
|
*/
|
|
412
458
|
/**
|
|
413
459
|
* How the push was produced. Fixed enum — packages must not add values.
|
|
@@ -433,6 +479,7 @@ export const MESSAGE_KIND: Readonly<{
|
|
|
433
479
|
REASONING: "reasoning";
|
|
434
480
|
TOOL_REQUEST: "tool_request";
|
|
435
481
|
ERROR: "error";
|
|
482
|
+
RESULT: "result";
|
|
436
483
|
}>;
|
|
437
484
|
/**
|
|
438
485
|
* Runtime constant mirroring the {@link MessageType} type.
|
|
@@ -525,8 +572,9 @@ export type AmsgPushCommon = {
|
|
|
525
572
|
* so producers get builder validation for the fields the SW actually reads.
|
|
526
573
|
*
|
|
527
574
|
* Routing in SW:
|
|
528
|
-
* - By default (`show: "auto"` or omitted), `messageKind: 'content'` (and legacy
|
|
529
|
-
* will display a system notification. `reasoning` / `tool_request` /
|
|
575
|
+
* - By default (`show: "auto"` or omitted), `messageKind: 'content'` / `'result'` (and legacy
|
|
576
|
+
* un-kinded payloads) will display a system notification. `reasoning` / `tool_request` /
|
|
577
|
+
* `error` will dispatch silently.
|
|
530
578
|
* - `show: "always"`, `"when-hidden"`, or `false` overrides this default.
|
|
531
579
|
* - When rendering, `notification.*` is consulted first, with per-field
|
|
532
580
|
* fallback to the matching top-level payload fields (`title`,
|
|
@@ -649,18 +697,50 @@ export type ToolRequestPush = AmsgPushCommon & {
|
|
|
649
697
|
* `{ type: 'error', code: '...' }` envelope. `code` is a stable
|
|
650
698
|
* string; `iteration` is the agentic-loop iteration number when
|
|
651
699
|
* relevant (0 / absent otherwise).
|
|
700
|
+
*
|
|
701
|
+
* `llmStatus` / `providerCode` 是失败来自 LLM 调用时的机读标注:上游回的 HTTP
|
|
702
|
+
* 状态码,以及 provider 自己的错误码(`invalid_api_key` /
|
|
703
|
+
* `context_length_exceeded` 之类)。客户端靠它们分流「Key 失效要去改配置」和
|
|
704
|
+
* 「上游一时抽风等会儿再说」,不用回去正则匹配 `message` 那句人话——那句是给
|
|
705
|
+
* 用户看的,措辞随时会变。失败跟 LLM 无关时两个都不出现。
|
|
652
706
|
*/
|
|
653
707
|
export type ErrorPush = AmsgPushCommon & {
|
|
654
708
|
messageKind: "error";
|
|
655
709
|
code: string;
|
|
656
710
|
message: string;
|
|
657
711
|
iteration?: number;
|
|
712
|
+
llmStatus?: number;
|
|
713
|
+
providerCode?: string;
|
|
714
|
+
};
|
|
715
|
+
/**
|
|
716
|
+
* 宿主自定义的一条结果,不是聊天内容。
|
|
717
|
+
*
|
|
718
|
+
* 用在「这次跑完产出了点什么,客户端拿去自己消化」的场合:整理好的一份数据、
|
|
719
|
+
* 一条账目、一次后台生成的产物。形状由宿主定,库只固定三件事——`messageKind`
|
|
720
|
+
* 是 `'result'`(客户端一眼分得出这不是聊天),`resultKind` 是宿主给这类结果
|
|
721
|
+
* 起的名字(有多种结果时按它分流,不用去猜字段),其余字段随便加。
|
|
722
|
+
*
|
|
723
|
+
* 与另外四种 kind 的区别在投递路径:这一条同时落进服务端收件箱
|
|
724
|
+
* (`message_outbox`),客户端下次 `GET /outbox?since=` 一定拿得到——推送没送
|
|
725
|
+
* 到、内容超过一条推送的 4KB 上限,都不会让它丢。产出方见
|
|
726
|
+
* `@rei-standard/amsg-server` 的 `ctx.emitResult()`。
|
|
727
|
+
*
|
|
728
|
+
* SW 侧默认弹通知(与 `content` 同待遇,其余三种是静默送给页面):结果往往正
|
|
729
|
+
* 是「跑完了,回来看看」那句话。不想弹就带
|
|
730
|
+
* `notification: { show: false }`,标题正文也在 `notification` 里自定义。
|
|
731
|
+
*/
|
|
732
|
+
export type ResultPush = AmsgPushCommon & {
|
|
733
|
+
messageKind: "result";
|
|
734
|
+
resultKind: string;
|
|
735
|
+
title?: string;
|
|
736
|
+
body?: string;
|
|
737
|
+
data?: any;
|
|
658
738
|
};
|
|
659
739
|
/**
|
|
660
740
|
* Discriminated union of all pushes the SW can receive. TS consumers
|
|
661
741
|
* `switch` on `messageKind` and the compiler narrows automatically.
|
|
662
742
|
*/
|
|
663
|
-
export type AmsgPush = ContentPush | ReasoningPush | ToolRequestPush | ErrorPush;
|
|
743
|
+
export type AmsgPush = ContentPush | ReasoningPush | ToolRequestPush | ErrorPush | ResultPush;
|
|
664
744
|
export type ChatMessage = {
|
|
665
745
|
role: "system" | "user" | "assistant" | "tool";
|
|
666
746
|
content?: string | unknown[] | null;
|
|
@@ -690,6 +770,10 @@ export type SessionContext = {
|
|
|
690
770
|
* - May be '' for pure tool-call responses.
|
|
691
771
|
*/
|
|
692
772
|
llmOutputText: string;
|
|
773
|
+
/**
|
|
774
|
+
* - `llmResponse.usage` 的直接引用(prompt/completion/total tokens)。响应没带 usage → null。llmResponse 里本来就有,单独提出来是让「记本轮用量」不必依赖响应对象的具体形状。
|
|
775
|
+
*/
|
|
776
|
+
usage: Record<string, unknown> | null;
|
|
693
777
|
/**
|
|
694
778
|
* - 0-indexed: the round that just finished.
|
|
695
779
|
*/
|
|
@@ -705,7 +789,7 @@ export type SessionContext = {
|
|
|
705
789
|
/**
|
|
706
790
|
* What the push carries. Fixed enum — packages must not add values.
|
|
707
791
|
*/
|
|
708
|
-
export type MessageKind = "content" | "reasoning" | "tool_request" | "error";
|
|
792
|
+
export type MessageKind = "content" | "reasoning" | "tool_request" | "error" | "result";
|
|
709
793
|
/**
|
|
710
794
|
* How the push was produced. Fixed enum — packages must not add values.
|
|
711
795
|
*/
|
|
@@ -719,6 +803,7 @@ export type MessageType = "instant" | "fixed" | "prompted" | "auto";
|
|
|
719
803
|
export type PushSource = "instant" | "scheduled";
|
|
720
804
|
export { toUint8, concatBytes, utf8, utf8Decode, bytesToBase64, bytesToBase64Url, base64UrlToBytes, jsonToBase64Url, bytesToHex, hexToBytes, hmacSha256, timingSafeEqualBytes, randomBytes, randomUUID } from "./webcrypto-utils.js";
|
|
721
805
|
export { LLM_MESSAGES_ERROR, validateLlmMessagesShape } from "./llm-messages.js";
|
|
722
|
-
export { callLlm, buildLlmRequestBody, normalizeAiApiUrl } from "./llm-call.js";
|
|
806
|
+
export { callLlm, buildLlmRequestBody, normalizeAiApiUrl, redactCredentials } from "./llm-call.js";
|
|
723
807
|
export { sendWebPush, buildVapidJwt, verifyVapidJwt } from "./webpush.js";
|
|
724
|
-
export { MULTIPART_MESSAGE_KIND, MULTIPART_ENCODING, MULTIPART_VERSION, DEFAULT_MULTIPART_TTL_MS, DEFAULT_MULTIPART_MAX_CHUNKS, DEFAULT_MULTIPART_MAX_TOTAL_BYTES, REI_AMSG_POSTMESSAGE_TYPE, REI_SW_EVENT, REI_SW_MESSAGE_TYPE, REI_AMSG_DELIVER_MESSAGE_TYPE } from "./protocol.js";
|
|
808
|
+
export { MULTIPART_MESSAGE_KIND, MULTIPART_ENCODING, MULTIPART_VERSION, DEFAULT_MULTIPART_TTL_MS, DEFAULT_MULTIPART_MAX_CHUNKS, DEFAULT_MULTIPART_MAX_TOTAL_BYTES, MULTIPART_FAILURE_REASON, REI_AMSG_POSTMESSAGE_TYPE, REI_SW_EVENT, REI_SW_MESSAGE_TYPE, REI_AMSG_DELIVER_MESSAGE_TYPE } from "./protocol.js";
|
|
809
|
+
export { buildMultipartPushPayloads, DEFAULT_MULTIPART_CHUNK_BYTES } from "./multipart.js";
|
package/dist/index.d.ts
CHANGED
|
@@ -154,6 +154,8 @@ export function buildToolRequestPush(args: {
|
|
|
154
154
|
* @param {string} [args.messageSubtype]
|
|
155
155
|
* @param {Object} [args.metadata]
|
|
156
156
|
* @param {NotificationDirective} [args.notification]
|
|
157
|
+
* @param {number} [args.llmStatus] - 上游 LLM 回的 HTTP 状态码(见 ErrorPush)
|
|
158
|
+
* @param {string} [args.providerCode] - provider 自己的错误码(见 ErrorPush)
|
|
157
159
|
* @returns {ErrorPush}
|
|
158
160
|
*/
|
|
159
161
|
export function buildErrorPush(args: {
|
|
@@ -168,7 +170,42 @@ export function buildErrorPush(args: {
|
|
|
168
170
|
messageSubtype?: string;
|
|
169
171
|
metadata?: any;
|
|
170
172
|
notification?: NotificationDirective;
|
|
173
|
+
llmStatus?: number;
|
|
174
|
+
providerCode?: string;
|
|
171
175
|
}): ErrorPush;
|
|
176
|
+
/**
|
|
177
|
+
* Build a {@link ResultPush} —— 宿主自定义的一条结果。
|
|
178
|
+
*
|
|
179
|
+
* 与另外四个 builder 有一处不同:**认识以外的字段原样保留**。结果的形状本来
|
|
180
|
+
* 就由宿主定,白名单式的复制等于把内容删掉一半。库只保证 `messageKind` 与
|
|
181
|
+
* `timestamp` 到位,其余交给宿主。
|
|
182
|
+
*
|
|
183
|
+
* `resultKind` 必填:宿主给这类结果起的名字(`'fire-pack'`、`'ledger-entry'`
|
|
184
|
+
* ……)。客户端有多种结果时按它分流,不用靠「有没有某个字段」去猜是哪一种。
|
|
185
|
+
*
|
|
186
|
+
* @param {Object} args - 整条 payload;下列几个字段有约束,其余原样带过去。
|
|
187
|
+
* @param {MessageType} args.messageType
|
|
188
|
+
* @param {PushSource} args.source
|
|
189
|
+
* @param {string} args.messageId
|
|
190
|
+
* @param {string} args.sessionId
|
|
191
|
+
* @param {string} args.resultKind - 这类结果的名字(宿主自定义)
|
|
192
|
+
* @param {string} [args.timestamp]
|
|
193
|
+
* @param {string} [args.title] - 通知标题的兜底(`notification.title` 优先)
|
|
194
|
+
* @param {string} [args.body] - 通知正文的兜底(`notification.body` 优先)
|
|
195
|
+
* @param {NotificationDirective} [args.notification]
|
|
196
|
+
* @returns {ResultPush}
|
|
197
|
+
*/
|
|
198
|
+
export function buildResultPush(args: {
|
|
199
|
+
messageType: MessageType;
|
|
200
|
+
source: PushSource;
|
|
201
|
+
messageId: string;
|
|
202
|
+
sessionId: string;
|
|
203
|
+
resultKind: string;
|
|
204
|
+
timestamp?: string;
|
|
205
|
+
title?: string;
|
|
206
|
+
body?: string;
|
|
207
|
+
notification?: NotificationDirective;
|
|
208
|
+
}): ResultPush;
|
|
172
209
|
/**
|
|
173
210
|
* Type guard: returns true if the argument is a {@link ContentPush}.
|
|
174
211
|
*
|
|
@@ -197,6 +234,13 @@ export function isToolRequestPush(value: unknown): value is ToolRequestPush;
|
|
|
197
234
|
* @returns {value is ErrorPush}
|
|
198
235
|
*/
|
|
199
236
|
export function isErrorPush(value: unknown): value is ErrorPush;
|
|
237
|
+
/**
|
|
238
|
+
* Type guard: returns true if the argument is a {@link ResultPush}.
|
|
239
|
+
*
|
|
240
|
+
* @param {unknown} value
|
|
241
|
+
* @returns {value is ResultPush}
|
|
242
|
+
*/
|
|
243
|
+
export function isResultPush(value: unknown): value is ResultPush;
|
|
200
244
|
/**
|
|
201
245
|
* Slice a string into UTF-8 byte chunks no larger than `maxBytes`,
|
|
202
246
|
* always cutting at codepoint boundaries (never inside a multi-byte
|
|
@@ -296,6 +340,7 @@ export function stripReasoningTags(content: string): string;
|
|
|
296
340
|
* @property {ChatMessage[]} messages - Including the just-appended assistant turn.
|
|
297
341
|
* @property {unknown} llmResponse - Full LLM response (choices, usage, …).
|
|
298
342
|
* @property {string} llmOutputText - May be '' for pure tool-call responses.
|
|
343
|
+
* @property {Record<string, unknown>|null} usage - `llmResponse.usage` 的直接引用(prompt/completion/total tokens)。响应没带 usage → null。llmResponse 里本来就有,单独提出来是让「记本轮用量」不必依赖响应对象的具体形状。
|
|
299
344
|
* @property {number} iteration - 0-indexed: the round that just finished.
|
|
300
345
|
* @property {Record<string, unknown>} metadata
|
|
301
346
|
* @property {string} contactName
|
|
@@ -391,7 +436,8 @@ export function extractToolCallsFromDecision(decision: unknown): Array<Record<st
|
|
|
391
436
|
* Three orthogonal axes:
|
|
392
437
|
* 1. messageType — how the push was produced (instant / fixed / prompted / auto)
|
|
393
438
|
* 2. messageSubtype — caller's business classification (free-form string)
|
|
394
|
-
* 3. messageKind — what the push carries (content / reasoning / tool_request /
|
|
439
|
+
* 3. messageKind — what the push carries (content / reasoning / tool_request /
|
|
440
|
+
* error / result)
|
|
395
441
|
*
|
|
396
442
|
* Zero runtime dependencies. The package is ESM/CJS dual-published and
|
|
397
443
|
* intentionally has no `dependencies:` entry — every other amsg sub-
|
|
@@ -407,7 +453,7 @@ export function extractToolCallsFromDecision(decision: unknown): Array<Record<st
|
|
|
407
453
|
/**
|
|
408
454
|
* What the push carries. Fixed enum — packages must not add values.
|
|
409
455
|
*
|
|
410
|
-
* @typedef {'content' | 'reasoning' | 'tool_request' | 'error'} MessageKind
|
|
456
|
+
* @typedef {'content' | 'reasoning' | 'tool_request' | 'error' | 'result'} MessageKind
|
|
411
457
|
*/
|
|
412
458
|
/**
|
|
413
459
|
* How the push was produced. Fixed enum — packages must not add values.
|
|
@@ -433,6 +479,7 @@ export const MESSAGE_KIND: Readonly<{
|
|
|
433
479
|
REASONING: "reasoning";
|
|
434
480
|
TOOL_REQUEST: "tool_request";
|
|
435
481
|
ERROR: "error";
|
|
482
|
+
RESULT: "result";
|
|
436
483
|
}>;
|
|
437
484
|
/**
|
|
438
485
|
* Runtime constant mirroring the {@link MessageType} type.
|
|
@@ -525,8 +572,9 @@ export type AmsgPushCommon = {
|
|
|
525
572
|
* so producers get builder validation for the fields the SW actually reads.
|
|
526
573
|
*
|
|
527
574
|
* Routing in SW:
|
|
528
|
-
* - By default (`show: "auto"` or omitted), `messageKind: 'content'` (and legacy
|
|
529
|
-
* will display a system notification. `reasoning` / `tool_request` /
|
|
575
|
+
* - By default (`show: "auto"` or omitted), `messageKind: 'content'` / `'result'` (and legacy
|
|
576
|
+
* un-kinded payloads) will display a system notification. `reasoning` / `tool_request` /
|
|
577
|
+
* `error` will dispatch silently.
|
|
530
578
|
* - `show: "always"`, `"when-hidden"`, or `false` overrides this default.
|
|
531
579
|
* - When rendering, `notification.*` is consulted first, with per-field
|
|
532
580
|
* fallback to the matching top-level payload fields (`title`,
|
|
@@ -649,18 +697,50 @@ export type ToolRequestPush = AmsgPushCommon & {
|
|
|
649
697
|
* `{ type: 'error', code: '...' }` envelope. `code` is a stable
|
|
650
698
|
* string; `iteration` is the agentic-loop iteration number when
|
|
651
699
|
* relevant (0 / absent otherwise).
|
|
700
|
+
*
|
|
701
|
+
* `llmStatus` / `providerCode` 是失败来自 LLM 调用时的机读标注:上游回的 HTTP
|
|
702
|
+
* 状态码,以及 provider 自己的错误码(`invalid_api_key` /
|
|
703
|
+
* `context_length_exceeded` 之类)。客户端靠它们分流「Key 失效要去改配置」和
|
|
704
|
+
* 「上游一时抽风等会儿再说」,不用回去正则匹配 `message` 那句人话——那句是给
|
|
705
|
+
* 用户看的,措辞随时会变。失败跟 LLM 无关时两个都不出现。
|
|
652
706
|
*/
|
|
653
707
|
export type ErrorPush = AmsgPushCommon & {
|
|
654
708
|
messageKind: "error";
|
|
655
709
|
code: string;
|
|
656
710
|
message: string;
|
|
657
711
|
iteration?: number;
|
|
712
|
+
llmStatus?: number;
|
|
713
|
+
providerCode?: string;
|
|
714
|
+
};
|
|
715
|
+
/**
|
|
716
|
+
* 宿主自定义的一条结果,不是聊天内容。
|
|
717
|
+
*
|
|
718
|
+
* 用在「这次跑完产出了点什么,客户端拿去自己消化」的场合:整理好的一份数据、
|
|
719
|
+
* 一条账目、一次后台生成的产物。形状由宿主定,库只固定三件事——`messageKind`
|
|
720
|
+
* 是 `'result'`(客户端一眼分得出这不是聊天),`resultKind` 是宿主给这类结果
|
|
721
|
+
* 起的名字(有多种结果时按它分流,不用去猜字段),其余字段随便加。
|
|
722
|
+
*
|
|
723
|
+
* 与另外四种 kind 的区别在投递路径:这一条同时落进服务端收件箱
|
|
724
|
+
* (`message_outbox`),客户端下次 `GET /outbox?since=` 一定拿得到——推送没送
|
|
725
|
+
* 到、内容超过一条推送的 4KB 上限,都不会让它丢。产出方见
|
|
726
|
+
* `@rei-standard/amsg-server` 的 `ctx.emitResult()`。
|
|
727
|
+
*
|
|
728
|
+
* SW 侧默认弹通知(与 `content` 同待遇,其余三种是静默送给页面):结果往往正
|
|
729
|
+
* 是「跑完了,回来看看」那句话。不想弹就带
|
|
730
|
+
* `notification: { show: false }`,标题正文也在 `notification` 里自定义。
|
|
731
|
+
*/
|
|
732
|
+
export type ResultPush = AmsgPushCommon & {
|
|
733
|
+
messageKind: "result";
|
|
734
|
+
resultKind: string;
|
|
735
|
+
title?: string;
|
|
736
|
+
body?: string;
|
|
737
|
+
data?: any;
|
|
658
738
|
};
|
|
659
739
|
/**
|
|
660
740
|
* Discriminated union of all pushes the SW can receive. TS consumers
|
|
661
741
|
* `switch` on `messageKind` and the compiler narrows automatically.
|
|
662
742
|
*/
|
|
663
|
-
export type AmsgPush = ContentPush | ReasoningPush | ToolRequestPush | ErrorPush;
|
|
743
|
+
export type AmsgPush = ContentPush | ReasoningPush | ToolRequestPush | ErrorPush | ResultPush;
|
|
664
744
|
export type ChatMessage = {
|
|
665
745
|
role: "system" | "user" | "assistant" | "tool";
|
|
666
746
|
content?: string | unknown[] | null;
|
|
@@ -690,6 +770,10 @@ export type SessionContext = {
|
|
|
690
770
|
* - May be '' for pure tool-call responses.
|
|
691
771
|
*/
|
|
692
772
|
llmOutputText: string;
|
|
773
|
+
/**
|
|
774
|
+
* - `llmResponse.usage` 的直接引用(prompt/completion/total tokens)。响应没带 usage → null。llmResponse 里本来就有,单独提出来是让「记本轮用量」不必依赖响应对象的具体形状。
|
|
775
|
+
*/
|
|
776
|
+
usage: Record<string, unknown> | null;
|
|
693
777
|
/**
|
|
694
778
|
* - 0-indexed: the round that just finished.
|
|
695
779
|
*/
|
|
@@ -705,7 +789,7 @@ export type SessionContext = {
|
|
|
705
789
|
/**
|
|
706
790
|
* What the push carries. Fixed enum — packages must not add values.
|
|
707
791
|
*/
|
|
708
|
-
export type MessageKind = "content" | "reasoning" | "tool_request" | "error";
|
|
792
|
+
export type MessageKind = "content" | "reasoning" | "tool_request" | "error" | "result";
|
|
709
793
|
/**
|
|
710
794
|
* How the push was produced. Fixed enum — packages must not add values.
|
|
711
795
|
*/
|
|
@@ -719,6 +803,7 @@ export type MessageType = "instant" | "fixed" | "prompted" | "auto";
|
|
|
719
803
|
export type PushSource = "instant" | "scheduled";
|
|
720
804
|
export { toUint8, concatBytes, utf8, utf8Decode, bytesToBase64, bytesToBase64Url, base64UrlToBytes, jsonToBase64Url, bytesToHex, hexToBytes, hmacSha256, timingSafeEqualBytes, randomBytes, randomUUID } from "./webcrypto-utils.js";
|
|
721
805
|
export { LLM_MESSAGES_ERROR, validateLlmMessagesShape } from "./llm-messages.js";
|
|
722
|
-
export { callLlm, buildLlmRequestBody, normalizeAiApiUrl } from "./llm-call.js";
|
|
806
|
+
export { callLlm, buildLlmRequestBody, normalizeAiApiUrl, redactCredentials } from "./llm-call.js";
|
|
723
807
|
export { sendWebPush, buildVapidJwt, verifyVapidJwt } from "./webpush.js";
|
|
724
|
-
export { MULTIPART_MESSAGE_KIND, MULTIPART_ENCODING, MULTIPART_VERSION, DEFAULT_MULTIPART_TTL_MS, DEFAULT_MULTIPART_MAX_CHUNKS, DEFAULT_MULTIPART_MAX_TOTAL_BYTES, REI_AMSG_POSTMESSAGE_TYPE, REI_SW_EVENT, REI_SW_MESSAGE_TYPE, REI_AMSG_DELIVER_MESSAGE_TYPE } from "./protocol.js";
|
|
808
|
+
export { MULTIPART_MESSAGE_KIND, MULTIPART_ENCODING, MULTIPART_VERSION, DEFAULT_MULTIPART_TTL_MS, DEFAULT_MULTIPART_MAX_CHUNKS, DEFAULT_MULTIPART_MAX_TOTAL_BYTES, MULTIPART_FAILURE_REASON, REI_AMSG_POSTMESSAGE_TYPE, REI_SW_EVENT, REI_SW_MESSAGE_TYPE, REI_AMSG_DELIVER_MESSAGE_TYPE } from "./protocol.js";
|
|
809
|
+
export { buildMultipartPushPayloads, DEFAULT_MULTIPART_CHUNK_BYTES } from "./multipart.js";
|