@rivus/gateway 0.21.2 → 0.22.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.
Files changed (31) hide show
  1. package/README.md +4 -1
  2. package/assets/audio-transcriber/fetch-model.mjs +63 -70
  3. package/assets/audio-transcriber/transcribe.mjs +38 -47
  4. package/assets/installation-policy.json +4 -3
  5. package/dist/bootstrap/feishu.d.ts +7 -2
  6. package/dist/bootstrap/feishu.js +19195 -17153
  7. package/dist/bootstrap/herdr-feishu.d.ts +1 -7
  8. package/dist/bootstrap/herdr-feishu.js +23 -12
  9. package/dist/chunks/automation-presentation.js +58 -60
  10. package/dist/chunks/feishu-receive-message.js +113 -0
  11. package/dist/chunks/herdr-feishu-runtime.js +1027 -642
  12. package/dist/chunks/public.d.ts +278 -0
  13. package/dist/chunks/public2.d.ts +10 -0
  14. package/dist/chunks/rivus-deployment-cli-process.d.ts +127 -455
  15. package/dist/chunks/rivus-home-deployment-env.js +1342 -1781
  16. package/dist/chunks/rivus-node-entrypoint.js +4349 -4063
  17. package/dist/index.d.ts +23 -11
  18. package/dist/mcp.d.ts +10 -9
  19. package/dist/mcp.js +43 -43
  20. package/package.json +15 -11
  21. package/templates/a-share-briefing-analysis.mjs +32 -34
  22. package/templates/a-share-briefing-renderer.mjs +175 -186
  23. package/templates/a-share-index-evidence.mjs +50 -54
  24. package/templates/a-share-market-briefing.mjs +37 -39
  25. package/templates/a-share-overseas-evidence.mjs +35 -39
  26. package/templates/a-share-policy-evidence.mjs +90 -96
  27. package/templates/a-share-sector-evidence.mjs +32 -35
  28. package/templates/acp-stdio-proxy.mjs +21 -22
  29. package/templates/current-weather.mjs +56 -58
  30. package/templates/html-drive-tools.mjs +130 -141
  31. package/templates/rivus-agents.plugin.mjs +55 -57
@@ -7,84 +7,6 @@ import { promisify } from "node:util";
7
7
 
8
8
  const execFileAsync = promisify(execFile);
9
9
  const DEFAULT_ARTIFACT_ROOT = ".rivus/deployment/html-artifacts";
10
-
11
- export function createHtmlArtifactWriter(options = {}) {
12
- const cwd = options.cwd ?? process.cwd();
13
- const artifactRoot = resolveArtifactRoot(cwd, options.artifactRoot);
14
- return {
15
- async execute(input, context) {
16
- const html = readCompleteHtml(input);
17
- const operationId = readOperationId(context);
18
- const agentDirectory = resolveAgentDirectory(artifactRoot, context?.agentId);
19
- const artifactId = createHash("sha256")
20
- .update(JSON.stringify({ agentId: context.agentId, operationId }))
21
- .digest("hex")
22
- .slice(0, 32);
23
- const trustedDirectory = await createTrustedDirectory(cwd, agentDirectory);
24
- const filePath = resolve(trustedDirectory, `${artifactId}.html`);
25
- try {
26
- await writeFile(filePath, html, { encoding: "utf8", flag: "wx" });
27
- } catch (error) {
28
- if (!isFileAlreadyPresent(error) || (await readFile(filePath, "utf8")) !== html) {
29
- throw error;
30
- }
31
- }
32
- return Object.freeze({
33
- artifactId,
34
- path: relative(cwd, resolve(agentDirectory, `${artifactId}.html`)),
35
- sha256: createHash("sha256").update(html).digest("hex"),
36
- size: Buffer.byteLength(html)
37
- });
38
- }
39
- };
40
- }
41
-
42
- export function createLarkDriveHtmlUploader(options = {}) {
43
- const cwd = options.cwd ?? process.cwd();
44
- const artifactRoot = resolveArtifactRoot(cwd, options.artifactRoot);
45
- const onCleanupError = options.onCleanupError ?? reportCleanupError;
46
- const removeDirectory = options.removeDirectory ?? rm;
47
- const runCommand = options.runCommand ?? runLarkCli;
48
- return {
49
- async execute(input, context) {
50
- readOperationId(context);
51
- const { artifactId, name } = readUploadInput(input);
52
- const agentDirectory = resolveAgentDirectory(artifactRoot, context?.agentId);
53
- const trustedDirectory = await resolveExistingTrustedDirectory(cwd, agentDirectory);
54
- const filePath = resolve(trustedDirectory, `${artifactId}.html`);
55
- const bytes = await readRegularFileNoFollow(filePath, artifactId);
56
- const stagingRoot = resolve(cwd, ".rivus/deployment/upload-staging");
57
- const trustedStagingRoot = await createTrustedDirectory(cwd, stagingRoot);
58
- const trustedWorkspace = await realpath(cwd);
59
- const stagingDirectory = await mkdtemp(join(trustedStagingRoot, "upload-"));
60
- const stagedPath = resolve(stagingDirectory, name);
61
- try {
62
- await writeFile(stagedPath, bytes, { flag: "wx", mode: 0o600 });
63
- const command = process.env.RIVUS_LARK_CLI_PATH?.trim() || "lark-cli";
64
- const { stdout } = await runCommand(
65
- command,
66
- [
67
- "drive",
68
- "+upload",
69
- "--as",
70
- "user",
71
- "--file",
72
- relative(trustedWorkspace, stagedPath),
73
- "--name",
74
- name,
75
- "--format",
76
- "json"
77
- ],
78
- { cwd, timeout: 120_000 }
79
- );
80
- return readUploadResult(stdout, name);
81
- } finally {
82
- await cleanupStaging(removeDirectory, onCleanupError, stagingDirectory);
83
- }
84
- }
85
- };
86
- }
87
-
88
10
  async function readRegularFileNoFollow(filePath, artifactId) {
89
11
  const linkMetadata = await lstat(filePath);
90
12
  if (linkMetadata.isSymbolicLink()) {
@@ -137,7 +59,41 @@ async function runLarkCli(command, args, options) {
137
59
  maxBuffer: 1024 * 1024
138
60
  });
139
61
  }
62
+ function readFirstString(value, keys) {
63
+ for (const key of keys) {
64
+ const candidate = value[key];
65
+ if (typeof candidate === "string" && candidate.trim()) {
66
+ return candidate.trim();
67
+ }
68
+ }
69
+ return undefined;
70
+ }
71
+
72
+ function resolveArtifactRoot(cwd, artifactRoot = DEFAULT_ARTIFACT_ROOT) {
73
+ const root = resolve(cwd, artifactRoot);
74
+ const relativeRoot = relative(cwd, root);
75
+ if (relativeRoot === ".." || relativeRoot.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)) {
76
+ throw new Error("HTML artifact root must stay inside the workspace");
77
+ }
78
+ return root;
79
+ }
80
+
81
+ function resolveAgentDirectory(artifactRoot, agentId) {
82
+ if (typeof agentId !== "string" || !agentId.trim()) {
83
+ throw new Error("HTML artifact tool requires an agent id");
84
+ }
85
+ return resolve(artifactRoot, createHash("sha256").update(agentId).digest("hex").slice(0, 16));
86
+ }
87
+ function readOperationId(context) {
88
+ if (typeof context?.operationId !== "string" || !context.operationId.trim()) {
89
+ throw new Error("HTML artifact tool requires a stable operation id");
90
+ }
91
+ return context.operationId;
92
+ }
140
93
 
94
+ function isRecord(value) {
95
+ return value !== null && typeof value === "object" && !Array.isArray(value);
96
+ }
141
97
  function readCompleteHtml(input) {
142
98
  if (!isRecord(input) || typeof input.html !== "string") {
143
99
  throw new Error("write-html-artifact requires an html string");
@@ -186,41 +142,30 @@ function readUploadResult(stdout, requestedName) {
186
142
  }
187
143
  return Object.freeze({ fileToken, name, url });
188
144
  }
189
-
190
- function readFirstString(value, keys) {
191
- for (const key of keys) {
192
- const candidate = value[key];
193
- if (typeof candidate === "string" && candidate.trim()) {
194
- return candidate.trim();
195
- }
196
- }
197
- return undefined;
145
+ function isFileAlreadyPresent(error) {
146
+ return error instanceof Error && "code" in error && error.code === "EEXIST";
198
147
  }
199
148
 
200
- function resolveArtifactRoot(cwd, artifactRoot = DEFAULT_ARTIFACT_ROOT) {
201
- const root = resolve(cwd, artifactRoot);
202
- const relativeRoot = relative(cwd, root);
203
- if (relativeRoot === ".." || relativeRoot.startsWith(`..${process.platform === "win32" ? "\\" : "/"}`)) {
204
- throw new Error("HTML artifact root must stay inside the workspace");
205
- }
206
- return root;
149
+ function isMissingFile(error) {
150
+ return error instanceof Error && "code" in error && error.code === "ENOENT";
207
151
  }
208
-
209
- function resolveAgentDirectory(artifactRoot, agentId) {
210
- if (typeof agentId !== "string" || !agentId.trim()) {
211
- throw new Error("HTML artifact tool requires an agent id");
152
+ async function readOrCreateDirectory(directory, create) {
153
+ try {
154
+ return await lstat(directory);
155
+ } catch (error) {
156
+ if (!isMissingFile(error) || !create) {
157
+ throw error;
158
+ }
212
159
  }
213
- return resolve(artifactRoot, createHash("sha256").update(agentId).digest("hex").slice(0, 16));
214
- }
215
-
216
- async function createTrustedDirectory(cwd, directory) {
217
- return resolveTrustedDirectory(cwd, directory, true);
218
- }
219
-
220
- async function resolveExistingTrustedDirectory(cwd, directory) {
221
- return resolveTrustedDirectory(cwd, directory, false);
160
+ try {
161
+ await mkdir(directory);
162
+ } catch (error) {
163
+ if (!isFileAlreadyPresent(error)) {
164
+ throw error;
165
+ }
166
+ }
167
+ return lstat(directory);
222
168
  }
223
-
224
169
  async function resolveTrustedDirectory(cwd, directory, create) {
225
170
  const lexicalWorkspace = resolve(cwd);
226
171
  const lexicalDirectory = resolve(directory);
@@ -247,40 +192,84 @@ async function resolveTrustedDirectory(cwd, directory, create) {
247
192
  }
248
193
  return directoryPath;
249
194
  }
250
-
251
- async function readOrCreateDirectory(directory, create) {
252
- try {
253
- return await lstat(directory);
254
- } catch (error) {
255
- if (!isMissingFile(error) || !create) {
256
- throw error;
257
- }
258
- }
259
- try {
260
- await mkdir(directory);
261
- } catch (error) {
262
- if (!isFileAlreadyPresent(error)) {
263
- throw error;
264
- }
265
- }
266
- return lstat(directory);
267
- }
268
-
269
- function readOperationId(context) {
270
- if (typeof context?.operationId !== "string" || !context.operationId.trim()) {
271
- throw new Error("HTML artifact tool requires a stable operation id");
272
- }
273
- return context.operationId;
195
+ async function createTrustedDirectory(cwd, directory) {
196
+ return resolveTrustedDirectory(cwd, directory, true);
274
197
  }
275
-
276
- function isRecord(value) {
277
- return value !== null && typeof value === "object" && !Array.isArray(value);
198
+ export function createHtmlArtifactWriter(options = {}) {
199
+ const cwd = options.cwd ?? process.cwd();
200
+ const artifactRoot = resolveArtifactRoot(cwd, options.artifactRoot);
201
+ return {
202
+ async execute(input, context) {
203
+ const html = readCompleteHtml(input);
204
+ const operationId = readOperationId(context);
205
+ const agentDirectory = resolveAgentDirectory(artifactRoot, context?.agentId);
206
+ const artifactId = createHash("sha256")
207
+ .update(JSON.stringify({ agentId: context.agentId, operationId }))
208
+ .digest("hex")
209
+ .slice(0, 32);
210
+ const trustedDirectory = await createTrustedDirectory(cwd, agentDirectory);
211
+ const filePath = resolve(trustedDirectory, `${artifactId}.html`);
212
+ try {
213
+ await writeFile(filePath, html, { encoding: "utf8", flag: "wx" });
214
+ } catch (error) {
215
+ if (!isFileAlreadyPresent(error) || (await readFile(filePath, "utf8")) !== html) {
216
+ throw error;
217
+ }
218
+ }
219
+ return Object.freeze({
220
+ artifactId,
221
+ path: relative(cwd, resolve(agentDirectory, `${artifactId}.html`)),
222
+ sha256: createHash("sha256").update(html).digest("hex"),
223
+ size: Buffer.byteLength(html)
224
+ });
225
+ }
226
+ };
278
227
  }
279
-
280
- function isFileAlreadyPresent(error) {
281
- return error instanceof Error && "code" in error && error.code === "EEXIST";
228
+ async function resolveExistingTrustedDirectory(cwd, directory) {
229
+ return resolveTrustedDirectory(cwd, directory, false);
282
230
  }
283
-
284
- function isMissingFile(error) {
285
- return error instanceof Error && "code" in error && error.code === "ENOENT";
231
+ export function createLarkDriveHtmlUploader(options = {}) {
232
+ const cwd = options.cwd ?? process.cwd();
233
+ const artifactRoot = resolveArtifactRoot(cwd, options.artifactRoot);
234
+ const onCleanupError = options.onCleanupError ?? reportCleanupError;
235
+ const removeDirectory = options.removeDirectory ?? rm;
236
+ const runCommand = options.runCommand ?? runLarkCli;
237
+ return {
238
+ async execute(input, context) {
239
+ readOperationId(context);
240
+ const { artifactId, name } = readUploadInput(input);
241
+ const agentDirectory = resolveAgentDirectory(artifactRoot, context?.agentId);
242
+ const trustedDirectory = await resolveExistingTrustedDirectory(cwd, agentDirectory);
243
+ const filePath = resolve(trustedDirectory, `${artifactId}.html`);
244
+ const bytes = await readRegularFileNoFollow(filePath, artifactId);
245
+ const stagingRoot = resolve(cwd, ".rivus/deployment/upload-staging");
246
+ const trustedStagingRoot = await createTrustedDirectory(cwd, stagingRoot);
247
+ const trustedWorkspace = await realpath(cwd);
248
+ const stagingDirectory = await mkdtemp(join(trustedStagingRoot, "upload-"));
249
+ const stagedPath = resolve(stagingDirectory, name);
250
+ try {
251
+ await writeFile(stagedPath, bytes, { flag: "wx", mode: 0o600 });
252
+ const command = process.env.RIVUS_LARK_CLI_PATH?.trim() || "lark-cli";
253
+ const { stdout } = await runCommand(
254
+ command,
255
+ [
256
+ "drive",
257
+ "+upload",
258
+ "--as",
259
+ "user",
260
+ "--file",
261
+ relative(trustedWorkspace, stagedPath),
262
+ "--name",
263
+ name,
264
+ "--format",
265
+ "json"
266
+ ],
267
+ { cwd, timeout: 120_000 }
268
+ );
269
+ return readUploadResult(stdout, name);
270
+ } finally {
271
+ await cleanupStaging(removeDirectory, onCleanupError, stagingDirectory);
272
+ }
273
+ }
274
+ };
286
275
  }
@@ -46,7 +46,62 @@ const LANGFUSE_PUBLISHER_SKILL = Object.freeze({
46
46
  title: "Langfuse HTML publisher",
47
47
  version: "5.0.0"
48
48
  });
49
+ function createAShareAutomation(id, session) {
50
+ const label = session === "pre-market" ? "盘前" : "盘后";
51
+ return {
52
+ createInput: ({ occurrence }) => {
53
+ const parameters = JSON.stringify({ session, occurrence });
54
+ return {
55
+ text: [
56
+ `计划发生时间:${occurrence}。生成 A 股${label}简报。`,
57
+ `必须且只调用一次 ${TOOL_IDS.aShareMarketBriefing},参数严格使用:${parameters}。`,
58
+ "工具返回的 markdown 字段已区分市场事实和分析推断并列出来源,就是最终交付正文;必须原样输出,不增加开场、解释、预测、荐股、因果归因或任何工具结果之外的数字。",
59
+ "如果工具失败,不得估算或补全行情,让本次 Automation 明确失败以便按同一 Tick 重试。"
60
+ ].join("\n")
61
+ };
62
+ },
63
+ id,
64
+ profileId: "agent-a",
65
+ requestedSkillIds: [],
66
+ requestedToolIds: [TOOL_IDS.aShareMarketBriefing]
67
+ };
68
+ }
49
69
 
70
+ async function saveNote(input, context) {
71
+ if (
72
+ !input ||
73
+ typeof input !== "object" ||
74
+ typeof input.title !== "string" ||
75
+ !input.title.trim() ||
76
+ typeof input.text !== "string" ||
77
+ !input.text.trim() ||
78
+ !context.operationId
79
+ ) {
80
+ throw new Error("save-note requires title, text, and a trusted operation id");
81
+ }
82
+ const directory = join(
83
+ process.cwd(),
84
+ ".rivus",
85
+ "deployment",
86
+ "tool-output",
87
+ createHash("sha256").update(context.agentId).digest("hex").slice(0, 16)
88
+ );
89
+ const fileName = `${context.operationId.replace(/[^a-zA-Z0-9_-]/g, "_")}.md`;
90
+ const filePath = join(directory, fileName);
91
+ const content = `# ${input.title.trim()}\n\n${input.text.trim()}\n`;
92
+ await mkdir(directory, { recursive: true });
93
+ try {
94
+ await writeFile(filePath, content, { encoding: "utf8", flag: "wx" });
95
+ } catch (error) {
96
+ if (!(error instanceof Error) || !("code" in error) || error.code !== "EEXIST") {
97
+ throw error;
98
+ }
99
+ if ((await readFile(filePath, "utf8")) !== content) {
100
+ throw new Error("operation id is already bound to different note content");
101
+ }
102
+ }
103
+ return { path: `.rivus/deployment/tool-output/${fileName}`, saved: true };
104
+ }
50
105
  export default {
51
106
  manifest: {
52
107
  apiVersion: "1",
@@ -233,60 +288,3 @@ export default {
233
288
  registry.registerAutomation(createAShareAutomation(A_SHARE_POST_MARKET_AUTOMATION_ID, "post-market"));
234
289
  }
235
290
  };
236
-
237
- function createAShareAutomation(id, session) {
238
- const label = session === "pre-market" ? "盘前" : "盘后";
239
- return {
240
- createInput: ({ occurrence }) => {
241
- const parameters = JSON.stringify({ session, occurrence });
242
- return {
243
- text: [
244
- `计划发生时间:${occurrence}。生成 A 股${label}简报。`,
245
- `必须且只调用一次 ${TOOL_IDS.aShareMarketBriefing},参数严格使用:${parameters}。`,
246
- "工具返回的 markdown 字段已区分市场事实和分析推断并列出来源,就是最终交付正文;必须原样输出,不增加开场、解释、预测、荐股、因果归因或任何工具结果之外的数字。",
247
- "如果工具失败,不得估算或补全行情,让本次 Automation 明确失败以便按同一 Tick 重试。"
248
- ].join("\n")
249
- };
250
- },
251
- id,
252
- profileId: "agent-a",
253
- requestedSkillIds: [],
254
- requestedToolIds: [TOOL_IDS.aShareMarketBriefing]
255
- };
256
- }
257
-
258
- async function saveNote(input, context) {
259
- if (
260
- !input ||
261
- typeof input !== "object" ||
262
- typeof input.title !== "string" ||
263
- !input.title.trim() ||
264
- typeof input.text !== "string" ||
265
- !input.text.trim() ||
266
- !context.operationId
267
- ) {
268
- throw new Error("save-note requires title, text, and a trusted operation id");
269
- }
270
- const directory = join(
271
- process.cwd(),
272
- ".rivus",
273
- "deployment",
274
- "tool-output",
275
- createHash("sha256").update(context.agentId).digest("hex").slice(0, 16)
276
- );
277
- const fileName = `${context.operationId.replace(/[^a-zA-Z0-9_-]/g, "_")}.md`;
278
- const filePath = join(directory, fileName);
279
- const content = `# ${input.title.trim()}\n\n${input.text.trim()}\n`;
280
- await mkdir(directory, { recursive: true });
281
- try {
282
- await writeFile(filePath, content, { encoding: "utf8", flag: "wx" });
283
- } catch (error) {
284
- if (!(error instanceof Error) || !("code" in error) || error.code !== "EEXIST") {
285
- throw error;
286
- }
287
- if ((await readFile(filePath, "utf8")) !== content) {
288
- throw new Error("operation id is already bound to different note content");
289
- }
290
- }
291
- return { path: `.rivus/deployment/tool-output/${fileName}`, saved: true };
292
- }