@lokvis/schema 0.1.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.
@@ -0,0 +1,212 @@
1
+ /**
2
+ * Lokvis Workflow Schema
3
+ *
4
+ * 第一年只支持 Linear Workflow(线性工作流)。
5
+ * 不支持:Branch / Loop / Condition / Parallel。
6
+ */
7
+
8
+ import type { AssetType } from './asset.js';
9
+
10
+ /** Workflow 节点类型 */
11
+ export type NodeType = 'load' | 'transform' | 'export';
12
+
13
+ /** Workflow 节点定义 */
14
+ export interface WorkflowNode {
15
+ /** 节点唯一 ID */
16
+ id: string;
17
+ /** 节点类型 */
18
+ type: NodeType;
19
+ /**
20
+ * 引用的能力名,如 `image.resize`。
21
+ * 仅 `type === 'transform'` 时必填;load/export 节点可不填。
22
+ */
23
+ capability?: string;
24
+ /** 能力参数 */
25
+ params?: Record<string, unknown>;
26
+ /** 节点标签(UI 显示用) */
27
+ label?: string;
28
+ }
29
+
30
+ /** Workflow 边定义(第一年只支持线性链) */
31
+ export interface WorkflowEdge {
32
+ from: string;
33
+ to: string;
34
+ }
35
+
36
+ /** Workflow 输入定义 */
37
+ export interface WorkflowInput {
38
+ type: AssetType;
39
+ /** 是否允许多个输入 */
40
+ multiple: boolean;
41
+ /** 最大数量(multiple=true 时生效) */
42
+ maxCount?: number;
43
+ /** 文件类型过滤 */
44
+ accept?: string[];
45
+ }
46
+
47
+ /** Workflow 输出定义 */
48
+ export interface WorkflowOutput {
49
+ type: AssetType | 'archive';
50
+ format?: string;
51
+ }
52
+
53
+ /** Workflow 作者信息 */
54
+ export interface WorkflowAuthor {
55
+ id: string;
56
+ name: string;
57
+ }
58
+
59
+ /** Workflow 语义版本 */
60
+ export interface WorkflowVersion {
61
+ /** semver 版本号 */
62
+ version: string;
63
+ /** Schema 版本 */
64
+ schemaVersion: string;
65
+ /** 兼容的最低 Runtime 版本 */
66
+ runtimeVersion: string;
67
+ changelog: string;
68
+ createdAt: number;
69
+ deprecated?: boolean;
70
+ }
71
+
72
+ /** Workflow 分类 */
73
+ export type WorkflowCategory =
74
+ | 'image'
75
+ | 'video'
76
+ | 'audio'
77
+ | 'pdf'
78
+ | 'ai'
79
+ | 'data'
80
+ | 'developer'
81
+ | 'ecommerce'
82
+ | 'content-creation'
83
+ | 'other';
84
+
85
+ /** 完整 Workflow 定义 */
86
+ export interface Workflow {
87
+ /** Schema URL */
88
+ $schema?: string;
89
+ /** Workflow 唯一 ID */
90
+ id: string;
91
+ /** 版本号 */
92
+ version: string;
93
+ /** 工作流名称 */
94
+ name: string;
95
+ /** 描述 */
96
+ description: string;
97
+ /** 作者 */
98
+ author: WorkflowAuthor;
99
+ /** 分类 */
100
+ category: WorkflowCategory;
101
+ /** 标签 */
102
+ tags: string[];
103
+ /** 节点列表 */
104
+ nodes: WorkflowNode[];
105
+ /** 边列表(线性链) */
106
+ edges: WorkflowEdge[];
107
+ /** 输入定义 */
108
+ inputs: WorkflowInput;
109
+ /** 输出定义 */
110
+ outputs: WorkflowOutput;
111
+ /** 是否为官方工作流 */
112
+ official?: boolean;
113
+ /** 创建时间 */
114
+ createdAt?: number;
115
+ /** 更新时间 */
116
+ updatedAt?: number;
117
+ }
118
+
119
+ /** Workflow 执行结果 */
120
+ export interface WorkflowResult {
121
+ workflowId: string;
122
+ /** 输出 Asset ID 列表 */
123
+ outputs: import('./asset.js').AssetId[];
124
+ /** 执行耗时(毫秒) */
125
+ duration: number;
126
+ /** 执行状态 */
127
+ status: 'completed' | 'cancelled' | 'failed';
128
+ /** 错误信息(status=failed 时) */
129
+ error?: string;
130
+ }
131
+
132
+ /**
133
+ * Workflow 的 AI 指令描述(见 docs/AI生态冲击调整方案.md §7.2)。
134
+ * 把 Workflow JSON 转换为 AI Agent 可理解的指令格式,
135
+ * 供 MCP server 在 prompt 模板 / resource 中暴露给 AI 客户端。
136
+ */
137
+ export interface WorkflowAiInstruction {
138
+ /** 人类可读的指令描述,如 "Execute 2-step workflow: image.resize with {...} → image.compress with {...}" */
139
+ instruction: string;
140
+ /** 涉及的 Lokvis capability 名(去重) */
141
+ capabilities: string[];
142
+ /** 输入参数 JSON Schema(描述 workflow 需要的输入) */
143
+ inputSchema: object;
144
+ /** 示例调用(供 AI 学习调用方式) */
145
+ example: {
146
+ input: Record<string, unknown>;
147
+ expectedOutput: string;
148
+ };
149
+ }
150
+
151
+ /**
152
+ * 把 Workflow 转换为 AI 可理解的指令描述。
153
+ *
154
+ * 注意:此函数仅做结构转换,不执行 workflow。
155
+ * AI 生成的 workflow 仍由确定性 Runtime 执行(见方案 §5.3 设计原则)。
156
+ *
157
+ * @param workflow 已定义的 Workflow
158
+ * @returns AI 指令描述,含人类可读指令、依赖能力、输入 schema、示例
159
+ */
160
+ export function workflowToAiInstruction(workflow: Workflow): WorkflowAiInstruction {
161
+ const steps = workflow.nodes
162
+ .filter((n) => n.capability)
163
+ .map((n) => `${n.capability} with params ${JSON.stringify(n.params ?? {})}`);
164
+
165
+ // 从节点中收集依赖的 capability 名(去重,保留顺序)
166
+ const seen = new Set<string>();
167
+ const capabilities: string[] = [];
168
+ for (const node of workflow.nodes) {
169
+ if (node.capability && !seen.has(node.capability)) {
170
+ seen.add(node.capability);
171
+ capabilities.push(node.capability);
172
+ }
173
+ }
174
+
175
+ // 期望输出反映 workflow 实际输出类型/格式(而非硬编码占位),
176
+ // 供 AI 理解调用后的产物。注意:此处仅描述,实际执行由确定性 Runtime 完成。
177
+ const outputFormat = workflow.outputs.format ?? workflow.outputs.type;
178
+
179
+ return {
180
+ instruction:
181
+ `Execute ${workflow.nodes.length}-step workflow "${workflow.name}": ${steps.join(' → ')}`,
182
+ capabilities,
183
+ inputSchema: workflowInputsToJsonSchema(workflow),
184
+ example: {
185
+ input: { input_path: `/path/to/input.${workflow.inputs.type}` },
186
+ expectedOutput: `Processed ${workflow.inputs.type} saved as ${outputFormat}`,
187
+ },
188
+ };
189
+ }
190
+
191
+ /** 把 Workflow 的输入定义转为 JSON Schema(供 AI 理解输入约束) */
192
+ function workflowInputsToJsonSchema(workflow: Workflow): object {
193
+ const inputDef = workflow.inputs;
194
+ const schema: Record<string, unknown> = {
195
+ type: 'object',
196
+ properties: {
197
+ input_path: {
198
+ type: 'string',
199
+ description: `Path or asset ID of the input ${inputDef.type} file${inputDef.multiple ? '(s)' : ''}`,
200
+ },
201
+ },
202
+ required: ['input_path'],
203
+ };
204
+ if (inputDef.multiple) {
205
+ (schema.properties as Record<string, unknown>).input_path = {
206
+ type: 'array',
207
+ items: { type: 'string' },
208
+ description: `List of input ${inputDef.type} file paths${inputDef.maxCount ? ` (max ${inputDef.maxCount})` : ''}`,
209
+ };
210
+ }
211
+ return schema;
212
+ }