@sns-parse/core 0.4.0-alpha.9 → 0.6.0-alpha.1

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.
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.createDefaultPipeline = createDefaultPipeline;
37
+ const hooks_1 = require("./hooks");
38
+ const fetcher = __importStar(require("../engine/fetcher"));
39
+ const compose = __importStar(require("../engine/compose"));
40
+ const forward = __importStar(require("../engine/forward"));
41
+ async function sendDispatch(rt, input) {
42
+ const { items, session, sender, quoteId } = input;
43
+ const config = rt.config || {};
44
+ const forwardAllowed = config.enableForward && (session.platform === 'onebot' || session.platform === 'satori');
45
+ if (forwardAllowed) {
46
+ await forward.sendForward(rt, sender, session, items);
47
+ return;
48
+ }
49
+ if (config.sendStrategy === 'split') {
50
+ for (const item of items)
51
+ await compose.sendSplit(rt, sender, session, item, { quoteId });
52
+ return;
53
+ }
54
+ for (const item of items) {
55
+ await compose.sendSingle(rt, sender, session, item, { quoteId });
56
+ }
57
+ }
58
+ /** 构建带全部基线实现的管道;随后调用各扩展 setup(hooks) 注入/覆盖 */
59
+ function createDefaultPipeline(rt) {
60
+ const p = new hooks_1.Pipeline(rt);
61
+ p.replace('parse', (input, r) => fetcher.fetchApi(r, input.url, input.type, input.fieldMapping, input.platformConf));
62
+ p.replace('translate', async () => null);
63
+ p.replace('media.image', async (input) => ({ kind: 'raw', url: input.url }));
64
+ p.replace('media.video', async (input) => ({ kind: 'raw', url: input.videoUrl }));
65
+ p.replace('media.merged', async (input) => ({ kind: 'raw', buffer: input.buffer, url: input.refUrl }));
66
+ p.replace('merge', async () => null);
67
+ p.replace('transcode', async () => null);
68
+ p.replace('compose', (input, r) => compose.buildUnits(r, input.item));
69
+ p.replace('send', (input, r) => sendDispatch(r, input));
70
+ return p;
71
+ }
@@ -0,0 +1,125 @@
1
+ /**
2
+ * 工作流阶段钩子管道(core 内建完整工作流,扩展经钩子注入/修改)。
3
+ *
4
+ * 每阶段结构:{ before[], impl, after[] }
5
+ * - before:前置,可改输入(返回新输入;返回 undefined 表示不变)
6
+ * - after:后置,可改输出(返回新输出;返回 undefined 表示不变)
7
+ * - replace:整体替换该阶段实现(后注册者胜)
8
+ *
9
+ * 基线实现见 workflow/default.ts(无扩展时全链路可跑:
10
+ * 不合并逐张发 / GIF 退化发视频 / 不翻译 / 媒体直通)。
11
+ */
12
+ import type { ParsedData } from '../types';
13
+ import type { ImageOutcome, VideoOutcome, TranslateResult, GifOptions, MediaKind, VideoMeta } from '../extensions';
14
+ import type { ProcessedItem, MessageUnit } from '../engine/compose';
15
+ import type { OutboundSender, SessionLike } from '../sender';
16
+ export type StageName = 'parse' | 'translate' | 'media.image' | 'media.video' | 'media.merged' | 'merge' | 'transcode' | 'compose' | 'send';
17
+ export interface ParseInput {
18
+ url: string;
19
+ type: string;
20
+ fieldMapping?: Record<string, string>;
21
+ platformConf?: any;
22
+ }
23
+ export interface TranslateInput {
24
+ text: string;
25
+ target: string;
26
+ sourceLang?: string;
27
+ }
28
+ export interface MediaImageInput {
29
+ platform: string;
30
+ url: string;
31
+ kind: MediaKind;
32
+ }
33
+ export interface MediaVideoInput {
34
+ platform: string;
35
+ videoUrl: string;
36
+ coverUrl: string;
37
+ meta: VideoMeta;
38
+ }
39
+ export interface MediaMergedInput {
40
+ platform: string;
41
+ buffer: Buffer;
42
+ refUrl: string;
43
+ }
44
+ export interface MergeInput {
45
+ urls: string[];
46
+ }
47
+ export type MergeResult = {
48
+ buffer: Buffer;
49
+ layout?: any;
50
+ } | null;
51
+ export interface TranscodeInput {
52
+ url: string;
53
+ durationSec: number;
54
+ opts: GifOptions;
55
+ }
56
+ export interface ComposeInput {
57
+ item: ProcessedItem;
58
+ }
59
+ export interface SendInput {
60
+ items: ProcessedItem[];
61
+ session: SessionLike;
62
+ sender: OutboundSender;
63
+ quoteId?: string;
64
+ }
65
+ export interface StageIO {
66
+ parse: {
67
+ input: ParseInput;
68
+ output: ParsedData;
69
+ };
70
+ translate: {
71
+ input: TranslateInput;
72
+ output: TranslateResult | null;
73
+ };
74
+ 'media.image': {
75
+ input: MediaImageInput;
76
+ output: ImageOutcome;
77
+ };
78
+ 'media.video': {
79
+ input: MediaVideoInput;
80
+ output: VideoOutcome;
81
+ };
82
+ 'media.merged': {
83
+ input: MediaMergedInput;
84
+ output: ImageOutcome | null;
85
+ };
86
+ merge: {
87
+ input: MergeInput;
88
+ output: MergeResult;
89
+ };
90
+ transcode: {
91
+ input: TranscodeInput;
92
+ output: Buffer | null;
93
+ };
94
+ compose: {
95
+ input: ComposeInput;
96
+ output: MessageUnit[];
97
+ };
98
+ send: {
99
+ input: SendInput;
100
+ output: void;
101
+ };
102
+ }
103
+ export type StageImpl<S extends StageName = StageName> = (input: StageIO[S]['input'], rt: any) => StageIO[S]['output'] | Promise<StageIO[S]['output']>;
104
+ export type BeforeHandler<S extends StageName = StageName> = (input: StageIO[S]['input'], rt: any) => StageIO[S]['input'] | void | Promise<StageIO[S]['input'] | void>;
105
+ export type AfterHandler<S extends StageName = StageName> = (output: StageIO[S]['output'], input: StageIO[S]['input'], rt: any) => StageIO[S]['output'] | void | Promise<StageIO[S]['output'] | void>;
106
+ /** 扩展注入面:before/after 可插入,replace 可整体替换(后注册者胜) */
107
+ export interface WorkflowHooks {
108
+ before<S extends StageName>(stage: S, h: BeforeHandler<S>): void;
109
+ after<S extends StageName>(stage: S, h: AfterHandler<S>): void;
110
+ replace<S extends StageName>(stage: S, h: StageImpl<S>): void;
111
+ }
112
+ export declare class Pipeline implements WorkflowHooks {
113
+ rt: any;
114
+ private impls;
115
+ private befores;
116
+ private afters;
117
+ constructor(rt: any);
118
+ before<S extends StageName>(stage: S, h: BeforeHandler<S>): void;
119
+ after<S extends StageName>(stage: S, h: AfterHandler<S>): void;
120
+ replace<S extends StageName>(stage: S, h: StageImpl<S>): void;
121
+ run<S extends StageName>(stage: S, input: StageIO[S]['input']): Promise<StageIO[S]['output']>;
122
+ }
123
+ /** 取(或懒挂)runtime 的管道;裸 rt 对象也能工作(自动挂默认管道)。 */
124
+ export declare function ensurePipeline(rt: any): Pipeline;
125
+ export declare function runStage<S extends StageName>(rt: any, stage: S, input: StageIO[S]['input']): Promise<StageIO[S]['output']>;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pipeline = void 0;
4
+ exports.ensurePipeline = ensurePipeline;
5
+ exports.runStage = runStage;
6
+ class Pipeline {
7
+ constructor(rt) {
8
+ this.rt = rt;
9
+ this.impls = new Map();
10
+ this.befores = new Map();
11
+ this.afters = new Map();
12
+ }
13
+ before(stage, h) {
14
+ const list = this.befores.get(stage) || [];
15
+ list.push(h);
16
+ this.befores.set(stage, list);
17
+ }
18
+ after(stage, h) {
19
+ const list = this.afters.get(stage) || [];
20
+ list.push(h);
21
+ this.afters.set(stage, list);
22
+ }
23
+ replace(stage, h) {
24
+ this.impls.set(stage, h);
25
+ }
26
+ async run(stage, input) {
27
+ let cur = input;
28
+ for (const h of this.befores.get(stage) || []) {
29
+ const r = await h(cur, this.rt);
30
+ if (r !== undefined)
31
+ cur = r;
32
+ }
33
+ const impl = this.impls.get(stage);
34
+ if (!impl)
35
+ throw new Error(`workflow: 阶段缺少实现: ${stage}`);
36
+ let out = await impl(cur, this.rt);
37
+ for (const h of this.afters.get(stage) || []) {
38
+ const r = await h(out, cur, this.rt);
39
+ if (r !== undefined)
40
+ out = r;
41
+ }
42
+ return out;
43
+ }
44
+ }
45
+ exports.Pipeline = Pipeline;
46
+ /** 取(或懒挂)runtime 的管道;裸 rt 对象也能工作(自动挂默认管道)。 */
47
+ function ensurePipeline(rt) {
48
+ if (rt && rt.pipeline instanceof Pipeline)
49
+ return rt.pipeline;
50
+ const { createDefaultPipeline } = require('./default');
51
+ const p = createDefaultPipeline(rt);
52
+ if (rt)
53
+ rt.pipeline = p;
54
+ return p;
55
+ }
56
+ function runStage(rt, stage, input) {
57
+ return ensurePipeline(rt).run(stage, input);
58
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sns-parse/core",
3
- "version": "0.4.0-alpha.9+upstream.1.6.7",
3
+ "version": "0.6.0-alpha.1+upstream.1.6.7",
4
4
  "description": "sns-parse 核心:契约、宿主抽象、配置 DSL 与解析引擎(fetcher/parser/翻译/X 原生解析/合并/GIF/TLS 指纹客户端)",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",