@amaster.ai/workflow-client 1.0.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Amaster Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @amaster.ai/workflow-client
2
+
3
+ Workflow execution client for running automated workflows.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @amaster.ai/workflow-client @amaster.ai/http-client axios
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { createWorkflowClient } from "@amaster.ai/workflow-client";
15
+
16
+ const client = createWorkflowClient();
17
+
18
+ const result = await client.run("data-processing", {
19
+ input_file: "data.csv",
20
+ output_format: "json",
21
+ });
22
+
23
+ if (result.data) {
24
+ console.log("Workflow completed:", result.data.outputs);
25
+ console.log("Elapsed time:", result.data.elapsed_time, "seconds");
26
+ }
27
+ ```
28
+
29
+ ## API Reference
30
+
31
+ ### `run(workflowName, inputs?)`
32
+
33
+ Execute a workflow by name.
34
+
35
+ ```typescript
36
+ // Simple inputs
37
+ await client.run("my-workflow", {
38
+ field1: "value1",
39
+ field2: 123,
40
+ });
41
+
42
+ // Full request with options
43
+ await client.run("my-workflow", {
44
+ inputs: { field1: "value1" },
45
+ response_mode: "blocking",
46
+ user: "john@example.com",
47
+ files: [{ url: "https://...", name: "doc.pdf" }],
48
+ });
49
+ ```
50
+
51
+ ## License
52
+
53
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ var httpClient = require('@amaster.ai/http-client');
4
+
5
+ // src/workflow-client.ts
6
+ function extractAppIdFromUrl() {
7
+ if (typeof window === "undefined") {
8
+ return null;
9
+ }
10
+ try {
11
+ const url = window.location.href;
12
+ const pathMatch = /\/app\/([\da-f-]+)(?:\/|$)/.exec(url);
13
+ if (pathMatch && pathMatch[1]) {
14
+ return pathMatch[1];
15
+ }
16
+ const hostname = window.location.hostname;
17
+ const domainMatch = /^([\da-f-]+)(?:-[^.]+)?\.amaster\.(?:local|ai)$/.exec(hostname);
18
+ if (domainMatch && domainMatch[1]) {
19
+ return domainMatch[1];
20
+ }
21
+ return null;
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+ function normalizeRunRequest(inputs) {
27
+ if (!inputs) {
28
+ return { inputs: {}, response_mode: "blocking", user: "anonymous" };
29
+ }
30
+ if (typeof inputs === "object" && ("inputs" in inputs || "response_mode" in inputs || "user" in inputs || "files" in inputs || "trace_id" in inputs)) {
31
+ const req = inputs;
32
+ return {
33
+ inputs: req.inputs || {},
34
+ response_mode: req.response_mode || "blocking",
35
+ user: req.user || "anonymous",
36
+ files: req.files,
37
+ trace_id: req.trace_id
38
+ };
39
+ }
40
+ return {
41
+ inputs,
42
+ response_mode: "blocking",
43
+ user: "anonymous"
44
+ };
45
+ }
46
+ function createWorkflowClient(http = httpClient.createHttpClient()) {
47
+ return {
48
+ async run(workflowName, inputs) {
49
+ if (!workflowName || workflowName.includes("/")) {
50
+ return {
51
+ data: null,
52
+ error: {
53
+ status: 400,
54
+ message: "Invalid workflowName: use workflow YAML filename without extension (no subpaths)"
55
+ },
56
+ status: 400
57
+ };
58
+ }
59
+ const normalizedRequest = normalizeRunRequest(inputs);
60
+ const appId = extractAppIdFromUrl();
61
+ if (appId && normalizedRequest.inputs) {
62
+ normalizedRequest.inputs.app_id = appId;
63
+ }
64
+ const response = await http.request({
65
+ url: `/api/runworkflow/${workflowName}`,
66
+ method: "post",
67
+ headers: { "Content-Type": "application/json" },
68
+ data: normalizedRequest
69
+ });
70
+ if (response.data?.data) {
71
+ const innerData = response.data.data;
72
+ return {
73
+ ...response,
74
+ data: {
75
+ task_id: response.data.task_id || "",
76
+ workflow_run_id: response.data.workflow_run_id || "",
77
+ status: innerData.status || "unknown",
78
+ outputs: innerData.outputs || {},
79
+ error: innerData.error || null,
80
+ elapsed_time: innerData.elapsed_time || 0,
81
+ total_tokens: innerData.total_tokens || 0,
82
+ total_steps: innerData.total_steps || 0,
83
+ created_at: innerData.created_at || 0,
84
+ finished_at: innerData.finished_at || 0
85
+ }
86
+ };
87
+ }
88
+ return {
89
+ data: null,
90
+ error: response.error || {
91
+ status: response.status,
92
+ message: "Invalid workflow response structure"
93
+ },
94
+ status: response.status
95
+ };
96
+ }
97
+ };
98
+ }
99
+
100
+ exports.createWorkflowClient = createWorkflowClient;
101
+ //# sourceMappingURL=index.cjs.map
102
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/workflow-client.ts"],"names":["createHttpClient"],"mappings":";;;;;AAOA,SAAS,mBAAA,GAAqC;AAC5C,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,IAAI;AAEF,I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U,SAAA;AAAA,YAC5B,OAAA,EAAS,SAAA,CAAU,OAAA,IAAY,EAAC;AAAA,YAChC,KAAA,EAAO,UAAU,KAAA,IAAS,IAAA;AAAA,YAC1B,YAAA,EAAc,UAAU,YAAA,IAAgB,CAAA;AAAA,YACxC,YAAA,EAAc,UAAU,YAAA,IAAgB,CAAA;AAAA,YACxC,WAAA,EAAa,UAAU,WAAA,IAAe,CAAA;AAAA,YACtC,UAAA,EAAY,UAAU,UAAA,IAAc,CAAA;AAAA,YACpC,WAAA,EAAa,UAAU,WAAA,IAAe;AAAA;AACxC,SACF;AAAA,MACF;AAEA,MAAA,OAAO;AAAA,QACL,IAAA,EAAM,IAAA;AAAA,QACN,KAAA,EAAO,SAAS,KAAA,IAAS;AAAA,UACvB,QAAQ,QAAA,CAAS,MAAA;AAAA,UACjB,OAAA,EAAS;AAAA,SACX;AAAA,QACA,QAAQ,QAAA,CAAS;AAAA,OACnB;AAAA,IACF;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import { type ClientResult, createHttpClient, type HttpClient } from \"@amaster.ai/http-client\";\n\n/**\n * Extract app_id from current page URL or domain\n * Method 1: From URL path - /app/{app_id}/...\n * Method 2: From domain (subdomain) - {app_id}-{env}.amaster.local\n */\nfunction extractAppIdFromUrl(): string | null {\n if (typeof window === \"undefined\") {\n return null;\n }\n\n try {\n // Method 1: Try to extract from URL path first\n const url = window.location.href;\n const pathMatch = /\\/app\\/([\\da-f-]+)(?:\\/|$)/.exec(url);\n if (pathMatch && pathMatch[1]) {\n return pathMatch[1];\n }\n\n // Method 2: Try to extract from domain (subdomain)\n const hostname = window.location.hostname;\n const domainMatch = /^([\\da-f-]+)(?:-[^.]+)?\\.amaster\\.(?:local|ai)$/.exec(hostname);\n if (domainMatch && domainMatch[1]) {\n return domainMatch[1];\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\nexport type WorkflowResponseMode = \"blocking\" | \"streaming\";\n\nexport type WorkflowInputValue =\n | string\n | number\n | boolean\n | null\n | undefined\n | Record<string, unknown>\n | unknown[];\n\nexport type WorkflowFile = {\n name?: string;\n type?: string;\n url?: string;\n [key: string]: unknown;\n};\n\nexport type WorkflowRunRequest = {\n inputs?: Record<string, WorkflowInputValue>;\n response_mode?: WorkflowResponseMode;\n user?: string;\n files?: WorkflowFile[];\n trace_id?: string;\n};\n\nexport type WorkflowRunResponse<TOutput = Record<string, unknown>> = {\n task_id: string;\n workflow_run_id: string;\n status: string;\n outputs: TOutput;\n error: string | null;\n elapsed_time: number;\n total_tokens: number;\n total_steps: number;\n created_at: number;\n finished_at: number;\n};\n\nexport type WorkflowClient = {\n run<TOutput = Record<string, unknown>>(\n workflowName: string,\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n ): Promise<ClientResult<WorkflowRunResponse<TOutput>>>;\n};\n\nfunction normalizeRunRequest(\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n): WorkflowRunRequest {\n if (!inputs) {\n return { inputs: {}, response_mode: \"blocking\", user: \"anonymous\" };\n }\n\n if (\n typeof inputs === \"object\" &&\n (\"inputs\" in inputs ||\n \"response_mode\" in inputs ||\n \"user\" in inputs ||\n \"files\" in inputs ||\n \"trace_id\" in inputs)\n ) {\n const req = inputs as WorkflowRunRequest;\n return {\n inputs: req.inputs || {},\n response_mode: req.response_mode || \"blocking\",\n user: req.user || \"anonymous\",\n files: req.files,\n trace_id: req.trace_id,\n };\n }\n\n return {\n inputs: inputs as Record<string, WorkflowInputValue>,\n response_mode: \"blocking\",\n user: \"anonymous\",\n };\n}\n\nexport function createWorkflowClient(http: HttpClient = createHttpClient()): WorkflowClient {\n return {\n async run<TOutput = Record<string, unknown>>(\n workflowName: string,\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n ): Promise<ClientResult<WorkflowRunResponse<TOutput>>> {\n if (!workflowName || workflowName.includes(\"/\")) {\n return {\n data: null,\n error: {\n status: 400,\n message:\n \"Invalid workflowName: use workflow YAML filename without extension (no subpaths)\",\n },\n status: 400,\n };\n }\n\n const normalizedRequest = normalizeRunRequest(inputs);\n\n // Auto-inject app_id from URL if not already provided\n const appId = extractAppIdFromUrl();\n if (appId && normalizedRequest.inputs) {\n normalizedRequest.inputs.app_id = appId;\n }\n\n const response = await http.request<{\n data: {\n status: string;\n outputs: TOutput;\n error: string | null;\n elapsed_time: number;\n total_tokens: number;\n total_steps: number;\n created_at: number;\n finished_at: number;\n };\n task_id: string;\n workflow_run_id: string;\n }>({\n url: `/api/runworkflow/${workflowName}`,\n method: \"post\",\n headers: { \"Content-Type\": \"application/json\" },\n data: normalizedRequest,\n });\n\n if (response.data?.data) {\n const innerData = response.data.data;\n return {\n ...response,\n data: {\n task_id: response.data.task_id || \"\",\n workflow_run_id: response.data.workflow_run_id || \"\",\n status: innerData.status || \"unknown\",\n outputs: innerData.outputs || ({} as TOutput),\n error: innerData.error || null,\n elapsed_time: innerData.elapsed_time || 0,\n total_tokens: innerData.total_tokens || 0,\n total_steps: innerData.total_steps || 0,\n created_at: innerData.created_at || 0,\n finished_at: innerData.finished_at || 0,\n },\n };\n }\n\n return {\n data: null,\n error: response.error || {\n status: response.status,\n message: \"Invalid workflow response structure\",\n },\n status: response.status,\n };\n },\n };\n}\n"]}
@@ -0,0 +1,35 @@
1
+ import { ClientResult, HttpClient } from '@amaster.ai/http-client';
2
+
3
+ type WorkflowResponseMode = "blocking" | "streaming";
4
+ type WorkflowInputValue = string | number | boolean | null | undefined | Record<string, unknown> | unknown[];
5
+ type WorkflowFile = {
6
+ name?: string;
7
+ type?: string;
8
+ url?: string;
9
+ [key: string]: unknown;
10
+ };
11
+ type WorkflowRunRequest = {
12
+ inputs?: Record<string, WorkflowInputValue>;
13
+ response_mode?: WorkflowResponseMode;
14
+ user?: string;
15
+ files?: WorkflowFile[];
16
+ trace_id?: string;
17
+ };
18
+ type WorkflowRunResponse<TOutput = Record<string, unknown>> = {
19
+ task_id: string;
20
+ workflow_run_id: string;
21
+ status: string;
22
+ outputs: TOutput;
23
+ error: string | null;
24
+ elapsed_time: number;
25
+ total_tokens: number;
26
+ total_steps: number;
27
+ created_at: number;
28
+ finished_at: number;
29
+ };
30
+ type WorkflowClient = {
31
+ run<TOutput = Record<string, unknown>>(workflowName: string, inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest): Promise<ClientResult<WorkflowRunResponse<TOutput>>>;
32
+ };
33
+ declare function createWorkflowClient(http?: HttpClient): WorkflowClient;
34
+
35
+ export { type WorkflowClient, type WorkflowFile, type WorkflowInputValue, type WorkflowResponseMode, type WorkflowRunRequest, type WorkflowRunResponse, createWorkflowClient };
@@ -0,0 +1,35 @@
1
+ import { ClientResult, HttpClient } from '@amaster.ai/http-client';
2
+
3
+ type WorkflowResponseMode = "blocking" | "streaming";
4
+ type WorkflowInputValue = string | number | boolean | null | undefined | Record<string, unknown> | unknown[];
5
+ type WorkflowFile = {
6
+ name?: string;
7
+ type?: string;
8
+ url?: string;
9
+ [key: string]: unknown;
10
+ };
11
+ type WorkflowRunRequest = {
12
+ inputs?: Record<string, WorkflowInputValue>;
13
+ response_mode?: WorkflowResponseMode;
14
+ user?: string;
15
+ files?: WorkflowFile[];
16
+ trace_id?: string;
17
+ };
18
+ type WorkflowRunResponse<TOutput = Record<string, unknown>> = {
19
+ task_id: string;
20
+ workflow_run_id: string;
21
+ status: string;
22
+ outputs: TOutput;
23
+ error: string | null;
24
+ elapsed_time: number;
25
+ total_tokens: number;
26
+ total_steps: number;
27
+ created_at: number;
28
+ finished_at: number;
29
+ };
30
+ type WorkflowClient = {
31
+ run<TOutput = Record<string, unknown>>(workflowName: string, inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest): Promise<ClientResult<WorkflowRunResponse<TOutput>>>;
32
+ };
33
+ declare function createWorkflowClient(http?: HttpClient): WorkflowClient;
34
+
35
+ export { type WorkflowClient, type WorkflowFile, type WorkflowInputValue, type WorkflowResponseMode, type WorkflowRunRequest, type WorkflowRunResponse, createWorkflowClient };
package/dist/index.js ADDED
@@ -0,0 +1,100 @@
1
+ import { createHttpClient } from '@amaster.ai/http-client';
2
+
3
+ // src/workflow-client.ts
4
+ function extractAppIdFromUrl() {
5
+ if (typeof window === "undefined") {
6
+ return null;
7
+ }
8
+ try {
9
+ const url = window.location.href;
10
+ const pathMatch = /\/app\/([\da-f-]+)(?:\/|$)/.exec(url);
11
+ if (pathMatch && pathMatch[1]) {
12
+ return pathMatch[1];
13
+ }
14
+ const hostname = window.location.hostname;
15
+ const domainMatch = /^([\da-f-]+)(?:-[^.]+)?\.amaster\.(?:local|ai)$/.exec(hostname);
16
+ if (domainMatch && domainMatch[1]) {
17
+ return domainMatch[1];
18
+ }
19
+ return null;
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+ function normalizeRunRequest(inputs) {
25
+ if (!inputs) {
26
+ return { inputs: {}, response_mode: "blocking", user: "anonymous" };
27
+ }
28
+ if (typeof inputs === "object" && ("inputs" in inputs || "response_mode" in inputs || "user" in inputs || "files" in inputs || "trace_id" in inputs)) {
29
+ const req = inputs;
30
+ return {
31
+ inputs: req.inputs || {},
32
+ response_mode: req.response_mode || "blocking",
33
+ user: req.user || "anonymous",
34
+ files: req.files,
35
+ trace_id: req.trace_id
36
+ };
37
+ }
38
+ return {
39
+ inputs,
40
+ response_mode: "blocking",
41
+ user: "anonymous"
42
+ };
43
+ }
44
+ function createWorkflowClient(http = createHttpClient()) {
45
+ return {
46
+ async run(workflowName, inputs) {
47
+ if (!workflowName || workflowName.includes("/")) {
48
+ return {
49
+ data: null,
50
+ error: {
51
+ status: 400,
52
+ message: "Invalid workflowName: use workflow YAML filename without extension (no subpaths)"
53
+ },
54
+ status: 400
55
+ };
56
+ }
57
+ const normalizedRequest = normalizeRunRequest(inputs);
58
+ const appId = extractAppIdFromUrl();
59
+ if (appId && normalizedRequest.inputs) {
60
+ normalizedRequest.inputs.app_id = appId;
61
+ }
62
+ const response = await http.request({
63
+ url: `/api/runworkflow/${workflowName}`,
64
+ method: "post",
65
+ headers: { "Content-Type": "application/json" },
66
+ data: normalizedRequest
67
+ });
68
+ if (response.data?.data) {
69
+ const innerData = response.data.data;
70
+ return {
71
+ ...response,
72
+ data: {
73
+ task_id: response.data.task_id || "",
74
+ workflow_run_id: response.data.workflow_run_id || "",
75
+ status: innerData.status || "unknown",
76
+ outputs: innerData.outputs || {},
77
+ error: innerData.error || null,
78
+ elapsed_time: innerData.elapsed_time || 0,
79
+ total_tokens: innerData.total_tokens || 0,
80
+ total_steps: innerData.total_steps || 0,
81
+ created_at: innerData.created_at || 0,
82
+ finished_at: innerData.finished_at || 0
83
+ }
84
+ };
85
+ }
86
+ return {
87
+ data: null,
88
+ error: response.error || {
89
+ status: response.status,
90
+ message: "Invalid workflow response structure"
91
+ },
92
+ status: response.status
93
+ };
94
+ }
95
+ };
96
+ }
97
+
98
+ export { createWorkflowClient };
99
+ //# sourceMappingURL=index.js.map
100
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/workflow-client.ts"],"names":[],"mappings":";;;AAOA,SAAS,mBAAA,GAAqC;AAC5C,EAAA,IAAI,OAAO,WAAW,WAAA,EAAa;AACjC,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,IAAI;AAEF,IAAA,MAAM,GAAA,GAAM,OAAO,QAAA,CAAS,IAAA;AAC5B,IAAA,MAAM,SAAA,GAAY,4BAAA,CAA6B,IAAA,CAAK,GAAG,CAAA;AACvD,IAAA,IAAI,SAAA,IAAa,SAAA,CAAU,CAAC,CAAA,EAAG;AAC7B,MAAA,OAAO,UAAU,CAAC,CAAA;AAAA,IACpB;AAGA,IAAA,MAAM,QAAA,GAAW,OAAO,QAAA,CAAS,QAAA;AACjC,IAAA,MAAM,WAAA,GAAc,iDAAA,CAAkD,IAAA,CAAK,QAAQ,CAAA;AACnF,IAAA,IAAI,WAAA,IAAe,WAAA,CAAY,CAAC,CAAA,EAAG;AACjC,MAAA,OAAO,YAAY,CAAC,CAAA;AAAA,IACtB;AAEA,IAAA,OA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vB,QAAQ,QAAA,CAAS,MAAA;AAAA,UACjB,OAAA,EAAS;AAAA,SACX;AAAA,QACA,QAAQ,QAAA,CAAS;AAAA,OACnB;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import { type ClientResult, createHttpClient, type HttpClient } from \"@amaster.ai/http-client\";\n\n/**\n * Extract app_id from current page URL or domain\n * Method 1: From URL path - /app/{app_id}/...\n * Method 2: From domain (subdomain) - {app_id}-{env}.amaster.local\n */\nfunction extractAppIdFromUrl(): string | null {\n if (typeof window === \"undefined\") {\n return null;\n }\n\n try {\n // Method 1: Try to extract from URL path first\n const url = window.location.href;\n const pathMatch = /\\/app\\/([\\da-f-]+)(?:\\/|$)/.exec(url);\n if (pathMatch && pathMatch[1]) {\n return pathMatch[1];\n }\n\n // Method 2: Try to extract from domain (subdomain)\n const hostname = window.location.hostname;\n const domainMatch = /^([\\da-f-]+)(?:-[^.]+)?\\.amaster\\.(?:local|ai)$/.exec(hostname);\n if (domainMatch && domainMatch[1]) {\n return domainMatch[1];\n }\n\n return null;\n } catch {\n return null;\n }\n}\n\nexport type WorkflowResponseMode = \"blocking\" | \"streaming\";\n\nexport type WorkflowInputValue =\n | string\n | number\n | boolean\n | null\n | undefined\n | Record<string, unknown>\n | unknown[];\n\nexport type WorkflowFile = {\n name?: string;\n type?: string;\n url?: string;\n [key: string]: unknown;\n};\n\nexport type WorkflowRunRequest = {\n inputs?: Record<string, WorkflowInputValue>;\n response_mode?: WorkflowResponseMode;\n user?: string;\n files?: WorkflowFile[];\n trace_id?: string;\n};\n\nexport type WorkflowRunResponse<TOutput = Record<string, unknown>> = {\n task_id: string;\n workflow_run_id: string;\n status: string;\n outputs: TOutput;\n error: string | null;\n elapsed_time: number;\n total_tokens: number;\n total_steps: number;\n created_at: number;\n finished_at: number;\n};\n\nexport type WorkflowClient = {\n run<TOutput = Record<string, unknown>>(\n workflowName: string,\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n ): Promise<ClientResult<WorkflowRunResponse<TOutput>>>;\n};\n\nfunction normalizeRunRequest(\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n): WorkflowRunRequest {\n if (!inputs) {\n return { inputs: {}, response_mode: \"blocking\", user: \"anonymous\" };\n }\n\n if (\n typeof inputs === \"object\" &&\n (\"inputs\" in inputs ||\n \"response_mode\" in inputs ||\n \"user\" in inputs ||\n \"files\" in inputs ||\n \"trace_id\" in inputs)\n ) {\n const req = inputs as WorkflowRunRequest;\n return {\n inputs: req.inputs || {},\n response_mode: req.response_mode || \"blocking\",\n user: req.user || \"anonymous\",\n files: req.files,\n trace_id: req.trace_id,\n };\n }\n\n return {\n inputs: inputs as Record<string, WorkflowInputValue>,\n response_mode: \"blocking\",\n user: \"anonymous\",\n };\n}\n\nexport function createWorkflowClient(http: HttpClient = createHttpClient()): WorkflowClient {\n return {\n async run<TOutput = Record<string, unknown>>(\n workflowName: string,\n inputs?: Record<string, WorkflowInputValue> | WorkflowRunRequest\n ): Promise<ClientResult<WorkflowRunResponse<TOutput>>> {\n if (!workflowName || workflowName.includes(\"/\")) {\n return {\n data: null,\n error: {\n status: 400,\n message:\n \"Invalid workflowName: use workflow YAML filename without extension (no subpaths)\",\n },\n status: 400,\n };\n }\n\n const normalizedRequest = normalizeRunRequest(inputs);\n\n // Auto-inject app_id from URL if not already provided\n const appId = extractAppIdFromUrl();\n if (appId && normalizedRequest.inputs) {\n normalizedRequest.inputs.app_id = appId;\n }\n\n const response = await http.request<{\n data: {\n status: string;\n outputs: TOutput;\n error: string | null;\n elapsed_time: number;\n total_tokens: number;\n total_steps: number;\n created_at: number;\n finished_at: number;\n };\n task_id: string;\n workflow_run_id: string;\n }>({\n url: `/api/runworkflow/${workflowName}`,\n method: \"post\",\n headers: { \"Content-Type\": \"application/json\" },\n data: normalizedRequest,\n });\n\n if (response.data?.data) {\n const innerData = response.data.data;\n return {\n ...response,\n data: {\n task_id: response.data.task_id || \"\",\n workflow_run_id: response.data.workflow_run_id || \"\",\n status: innerData.status || \"unknown\",\n outputs: innerData.outputs || ({} as TOutput),\n error: innerData.error || null,\n elapsed_time: innerData.elapsed_time || 0,\n total_tokens: innerData.total_tokens || 0,\n total_steps: innerData.total_steps || 0,\n created_at: innerData.created_at || 0,\n finished_at: innerData.finished_at || 0,\n },\n };\n }\n\n return {\n data: null,\n error: response.error || {\n status: response.status,\n message: \"Invalid workflow response structure\",\n },\n status: response.status,\n };\n },\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@amaster.ai/workflow-client",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "Workflow execution client",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
20
+ "keywords": [
21
+ "workflow",
22
+ "automation",
23
+ "api",
24
+ "typescript"
25
+ ],
26
+ "author": "Amaster Team",
27
+ "license": "MIT",
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "registry": "https://registry.npmjs.org/"
31
+ },
32
+ "dependencies": {
33
+ "@amaster.ai/http-client": "1.0.0-beta.0"
34
+ },
35
+ "peerDependencies": {
36
+ "axios": "^1.11.0"
37
+ },
38
+ "devDependencies": {
39
+ "axios": "^1.11.0",
40
+ "tsup": "^8.3.5",
41
+ "typescript": "~5.7.2"
42
+ },
43
+ "scripts": {
44
+ "build": "tsup",
45
+ "dev": "tsup --watch",
46
+ "clean": "rm -rf dist *.tsbuildinfo",
47
+ "type-check": "tsc --noEmit"
48
+ }
49
+ }