@ynhcj/xiaoyi-channel 0.0.217-beta → 0.0.218-next
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/index.d.ts +3 -3
- package/dist/index.js +133 -3
- package/dist/src/bot.js +111 -30
- package/dist/src/channel.js +2 -0
- package/dist/src/compaction-provider.d.ts +50 -0
- package/dist/src/compaction-provider.js +202 -0
- package/dist/src/cron-query-handler.d.ts +20 -0
- package/dist/src/cron-query-handler.js +481 -91
- package/dist/src/cron-recovery.d.ts +25 -0
- package/dist/src/cron-recovery.js +587 -0
- package/dist/src/cspl/call_api.d.ts +7 -1
- package/dist/src/cspl/call_api.js +51 -6
- package/dist/src/cspl/sentinel_hook.js +3 -3
- package/dist/src/cspl/skill_scope_hook.d.ts +17 -0
- package/dist/src/cspl/skill_scope_hook.js +125 -0
- package/dist/src/cspl/upload_file.js +5 -2
- package/dist/src/cspl/utils.d.ts +16 -6
- package/dist/src/cspl/utils.js +113 -7
- package/dist/src/file-download.d.ts +1 -0
- package/dist/src/file-download.js +1 -0
- package/dist/src/formatter.d.ts +36 -0
- package/dist/src/formatter.js +74 -0
- package/dist/src/log-reporter/index.js +7 -3
- package/dist/src/monitor.js +49 -8
- package/dist/src/outbound.d.ts +7 -0
- package/dist/src/outbound.js +62 -6
- package/dist/src/provider.js +90 -2
- package/dist/src/reply-dispatcher.d.ts +2 -0
- package/dist/src/reply-dispatcher.js +127 -48
- package/dist/src/skill-retriever/hooks.d.ts +17 -0
- package/dist/src/skill-retriever/hooks.js +123 -3
- package/dist/src/skill-retriever/tool-search.js +13 -11
- package/dist/src/steer-queue.d.ts +1 -0
- package/dist/src/steer-queue.js +9 -0
- package/dist/src/subagent-wait-state.d.ts +7 -0
- package/dist/src/subagent-wait-state.js +14 -0
- package/dist/src/task-manager.d.ts +9 -3
- package/dist/src/task-manager.js +48 -16
- package/dist/src/tools/call-phone-tool.d.ts +0 -1
- package/dist/src/tools/call-phone-tool.js +5 -8
- package/dist/src/tools/hmos-cli.d.ts +1 -1
- package/dist/src/tools/hmos-cli.js +4 -4
- package/dist/src/tools/send-email-tool.js +1 -0
- package/dist/src/tools/xiaoyi-append-reference.d.ts +47 -0
- package/dist/src/tools/xiaoyi-append-reference.js +107 -0
- package/dist/src/tools/xiaoyi-gui-tool.js +10 -1
- package/dist/src/websocket.js +10 -2
- package/package.json +3 -2
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CompactionProvider for xiaoyi-channel.
|
|
3
|
+
*
|
|
4
|
+
* During compaction, the safeguard's built-in summarization path
|
|
5
|
+
* (summarizeInStages -> generateSummary -> completeSimple) bypasses the
|
|
6
|
+
* plugin's wrapStreamFn, so x-hag-trace-id is never injected. Registering
|
|
7
|
+
* a CompactionProvider intercepts the safeguard before it reaches the LLM
|
|
8
|
+
* path and makes the summarization API call ourselves with proper headers.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from "crypto";
|
|
11
|
+
import { logger } from "./utils/logger.js";
|
|
12
|
+
// ── Header constants (mirrors provider.ts) ────────────────────────
|
|
13
|
+
const HEADER_TRACE_ID = "x-hag-trace-id";
|
|
14
|
+
const HEADER_SESSION_ID = "x-session-id";
|
|
15
|
+
const HEADER_INTERACTION_ID = "x-interaction-id";
|
|
16
|
+
// ── Summarization prompt ──────────────────────────────────────────
|
|
17
|
+
const SUMMARIZATION_SYSTEM_PROMPT = "You are a structured conversation summarization assistant. " +
|
|
18
|
+
"Produce a concise summary that preserves key facts, decisions, " +
|
|
19
|
+
"in-progress tasks, tool results, and the latest user intent.";
|
|
20
|
+
let storedConfig = null;
|
|
21
|
+
let storedSessionSnapshot = null;
|
|
22
|
+
/** Store config captured from prepareExtraParams so summarize() can read it. */
|
|
23
|
+
export function setCompactionConfig(config) {
|
|
24
|
+
storedConfig = config;
|
|
25
|
+
if (config) {
|
|
26
|
+
logger.log(`[compaction-provider] config stored uid=${config.uid.slice(0, 8)}... ` +
|
|
27
|
+
`baseUrl=${config.baseUrl} model=${config.modelName}`);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
logger.warn("[compaction-provider] config cleared (uid or baseUrl missing)");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Store a snapshot of the resolved session headers captured from the
|
|
35
|
+
* most recent wrapStreamFn call. This lets the CompactionProvider reuse
|
|
36
|
+
* the A2A traceId/sessionId during compaction summarization.
|
|
37
|
+
*/
|
|
38
|
+
export function setCompactionSessionSnapshot(snapshot) {
|
|
39
|
+
storedSessionSnapshot = snapshot;
|
|
40
|
+
}
|
|
41
|
+
// ── Helpers ───────────────────────────────────────────────────────
|
|
42
|
+
function encodeUid(uid) {
|
|
43
|
+
return createHash("sha256").update(uid).digest("hex").slice(0, 32);
|
|
44
|
+
}
|
|
45
|
+
function extractMessageText(content) {
|
|
46
|
+
if (typeof content === "string")
|
|
47
|
+
return content;
|
|
48
|
+
if (Array.isArray(content)) {
|
|
49
|
+
return content
|
|
50
|
+
.map((block) => {
|
|
51
|
+
if (block && typeof block === "object" && block.type === "text") {
|
|
52
|
+
return String(block.text ?? "");
|
|
53
|
+
}
|
|
54
|
+
return "";
|
|
55
|
+
})
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.join("\n");
|
|
58
|
+
}
|
|
59
|
+
return "";
|
|
60
|
+
}
|
|
61
|
+
function formatMessagesForSummarization(messages) {
|
|
62
|
+
const lines = [];
|
|
63
|
+
for (const msg of messages) {
|
|
64
|
+
if (!msg || typeof msg !== "object")
|
|
65
|
+
continue;
|
|
66
|
+
const m = msg;
|
|
67
|
+
if (m.role === "custom")
|
|
68
|
+
continue; // skip internal runtime messages
|
|
69
|
+
const role = String(m.role ?? "unknown");
|
|
70
|
+
const text = extractMessageText(m.content).trim();
|
|
71
|
+
if (!text)
|
|
72
|
+
continue;
|
|
73
|
+
let label;
|
|
74
|
+
if (role === "assistant") {
|
|
75
|
+
label = "Assistant";
|
|
76
|
+
}
|
|
77
|
+
else if (role === "user") {
|
|
78
|
+
label = "User";
|
|
79
|
+
}
|
|
80
|
+
else if (role === "toolResult") {
|
|
81
|
+
const toolName = typeof m.toolName === "string" && m.toolName ? m.toolName : "tool";
|
|
82
|
+
label = `Tool result (${toolName})`;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
label = role;
|
|
86
|
+
}
|
|
87
|
+
lines.push(`[${label}]: ${text}`);
|
|
88
|
+
}
|
|
89
|
+
return lines.join("\n\n");
|
|
90
|
+
}
|
|
91
|
+
// ── CompactionProvider ────────────────────────────────────────────
|
|
92
|
+
export const xiaoyiCompactionProvider = {
|
|
93
|
+
id: "xiaoyiprovider",
|
|
94
|
+
label: "Xiaoyi Compaction Provider",
|
|
95
|
+
async summarize(params) {
|
|
96
|
+
const cfg = storedConfig;
|
|
97
|
+
if (!cfg) {
|
|
98
|
+
// No stored config: let the safeguard fall through to the LLM path.
|
|
99
|
+
// Throwing here is intentional — tryProviderSummarize catches and
|
|
100
|
+
// falls through.
|
|
101
|
+
throw new Error("Compaction config not available (uid or baseUrl missing)");
|
|
102
|
+
}
|
|
103
|
+
// Use the A2A session snapshot when available (captured from the last
|
|
104
|
+
// normal wrapStreamFn call before compaction was triggered). Otherwise
|
|
105
|
+
// fall back to the uid_timestamp pattern.
|
|
106
|
+
const session = storedSessionSnapshot;
|
|
107
|
+
const traceId = session?.traceId ?? `${encodeUid(cfg.uid)}_${Date.now()}`;
|
|
108
|
+
const sessionId = session?.sessionId ?? traceId;
|
|
109
|
+
const interactionId = session?.interactionId ?? traceId;
|
|
110
|
+
logger.log(`[compaction-provider] summarize traceId=${traceId} source=${session ? "a2a-snapshot" : "uid-fallback"} msgCount=${params.messages.length}`);
|
|
111
|
+
// ── Build prompt ──────────────────────────────────────────────
|
|
112
|
+
const conversationText = formatMessagesForSummarization(params.messages);
|
|
113
|
+
let promptText = `<conversation>\n${conversationText}\n</conversation>`;
|
|
114
|
+
if (params.previousSummary) {
|
|
115
|
+
promptText += `\n\n<previous-summary>\n${params.previousSummary}\n</previous-summary>`;
|
|
116
|
+
}
|
|
117
|
+
let instructions = "Provide a concise but comprehensive summary of the conversation above. " +
|
|
118
|
+
"Include: key decisions, in-progress tasks, tool results, file operations, " +
|
|
119
|
+
"and the latest user request. Preserve exact file paths, URLs, identifiers, " +
|
|
120
|
+
"and error messages.";
|
|
121
|
+
if (params.customInstructions) {
|
|
122
|
+
instructions += `\n\nAdditional focus: ${params.customInstructions}`;
|
|
123
|
+
}
|
|
124
|
+
promptText += `\n\n${instructions}`;
|
|
125
|
+
// ── Build request ─────────────────────────────────────────────
|
|
126
|
+
// Reconstruct the exact same headers that the normal request uses.
|
|
127
|
+
// streamSimple adds model.headers to the HTTP request directly, so
|
|
128
|
+
// we capture them separately from options.headers.
|
|
129
|
+
const reqHdrs = session?.requestHeaders;
|
|
130
|
+
const modelHdrs = session?.modelHeaders;
|
|
131
|
+
const reqKeys = reqHdrs ? Object.keys(reqHdrs) : [];
|
|
132
|
+
const modelKeys = modelHdrs ? Object.keys(modelHdrs) : [];
|
|
133
|
+
logger.log(`[compaction-provider] snapshot auth: hasApiKey=${!!session?.apiKey} ` +
|
|
134
|
+
`requestHeaders=[${reqKeys.join(",")}] modelHeaders=[${modelKeys.join(",")}]`);
|
|
135
|
+
const headers = {
|
|
136
|
+
"Content-Type": "application/json",
|
|
137
|
+
...modelHdrs,
|
|
138
|
+
...reqHdrs,
|
|
139
|
+
...(session?.apiKey ? { Authorization: `Bearer ${session.apiKey}` } : {}),
|
|
140
|
+
[HEADER_TRACE_ID]: traceId,
|
|
141
|
+
[HEADER_SESSION_ID]: sessionId,
|
|
142
|
+
[HEADER_INTERACTION_ID]: interactionId,
|
|
143
|
+
};
|
|
144
|
+
if (session?.deviceType) {
|
|
145
|
+
headers["x-device-type"] = session.deviceType;
|
|
146
|
+
}
|
|
147
|
+
if (session?.appVer) {
|
|
148
|
+
headers["x-app-ver"] = session.appVer;
|
|
149
|
+
}
|
|
150
|
+
if (session?.sdkApiVersion) {
|
|
151
|
+
headers["x-sdk-api-version"] = session.sdkApiVersion;
|
|
152
|
+
}
|
|
153
|
+
const body = {
|
|
154
|
+
model: cfg.modelName,
|
|
155
|
+
messages: [
|
|
156
|
+
{ role: "system", content: SUMMARIZATION_SYSTEM_PROMPT },
|
|
157
|
+
{ role: "user", content: promptText },
|
|
158
|
+
],
|
|
159
|
+
max_tokens: 4096,
|
|
160
|
+
temperature: 0.3,
|
|
161
|
+
};
|
|
162
|
+
const url = `${cfg.baseUrl.replace(/\/+$/, "")}/chat/completions`;
|
|
163
|
+
// Log headers for diagnostics (redact sensitive values)
|
|
164
|
+
const safeHeaders = {};
|
|
165
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
166
|
+
if (key.toLowerCase().includes("auth") || key.toLowerCase().includes("api-key")) {
|
|
167
|
+
safeHeaders[key] = value ? `${value.slice(0, 4)}***` : "(empty)";
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
safeHeaders[key] = value;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
logger.log(`[compaction-provider] POST ${url} headers=${JSON.stringify(safeHeaders)}`);
|
|
174
|
+
// ── Call ──────────────────────────────────────────────────────
|
|
175
|
+
let response;
|
|
176
|
+
try {
|
|
177
|
+
response = await fetch(url, {
|
|
178
|
+
method: "POST",
|
|
179
|
+
headers,
|
|
180
|
+
body: JSON.stringify(body),
|
|
181
|
+
signal: params.signal,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
logger.error(`[compaction-provider] fetch failed: ${String(err)}`);
|
|
186
|
+
throw err;
|
|
187
|
+
}
|
|
188
|
+
if (!response.ok) {
|
|
189
|
+
const errorText = await response.text().catch(() => "");
|
|
190
|
+
logger.error(`[compaction-provider] API error status=${response.status} body=${errorText.slice(0, 500)}`);
|
|
191
|
+
throw new Error(`Compaction API returned ${response.status}`);
|
|
192
|
+
}
|
|
193
|
+
const data = (await response.json());
|
|
194
|
+
const summary = data.choices?.[0]?.message?.content;
|
|
195
|
+
if (!summary?.trim()) {
|
|
196
|
+
logger.warn("[compaction-provider] empty summary returned");
|
|
197
|
+
throw new Error("Empty summary from compaction API");
|
|
198
|
+
}
|
|
199
|
+
logger.log(`[compaction-provider] done summaryLen=${summary.length}`);
|
|
200
|
+
return summary;
|
|
201
|
+
},
|
|
202
|
+
};
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
export interface CronRunLogEntry {
|
|
2
|
+
jobId: string;
|
|
3
|
+
ts: number;
|
|
4
|
+
runId?: string;
|
|
5
|
+
status?: string;
|
|
6
|
+
error?: string;
|
|
7
|
+
summary?: string;
|
|
8
|
+
diagnosticsSummary?: string;
|
|
9
|
+
deliveryStatus?: string;
|
|
10
|
+
deliveryError?: string;
|
|
11
|
+
delivered?: boolean;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
sessionKey?: string;
|
|
14
|
+
runAtMs?: number;
|
|
15
|
+
durationMs?: number;
|
|
16
|
+
nextRunAtMs?: number;
|
|
17
|
+
model?: string;
|
|
18
|
+
provider?: string;
|
|
19
|
+
totalTokens?: number;
|
|
20
|
+
}
|
|
1
21
|
/**
|
|
2
22
|
* Handle a cron-query-event.
|
|
3
23
|
*
|