@synergenius/flow-weaver 0.22.9 → 0.22.10

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,122 @@
1
+ export class PlatformClient {
2
+ baseUrl;
3
+ token;
4
+ constructor(creds) {
5
+ this.baseUrl = creds.platformUrl.replace(/\/+$/, '');
6
+ this.token = creds.token;
7
+ }
8
+ async fetch(path, opts = {}) {
9
+ const isApiKey = this.token.startsWith('fw_');
10
+ const headers = {
11
+ 'Content-Type': 'application/json',
12
+ ...(isApiKey
13
+ ? { 'X-API-Key': this.token }
14
+ : { Authorization: `Bearer ${this.token}` }),
15
+ ...(opts.headers ?? {}),
16
+ };
17
+ return fetch(`${this.baseUrl}${path}`, { ...opts, headers });
18
+ }
19
+ // Auth
20
+ async getUser() {
21
+ const resp = await this.fetch('/auth/me');
22
+ if (!resp.ok)
23
+ throw new Error(`Auth failed: ${resp.status}`);
24
+ const data = await resp.json();
25
+ return data.user;
26
+ }
27
+ // Workflows
28
+ async pushWorkflow(name, source) {
29
+ // Try update first, then create
30
+ const slug = name.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-');
31
+ let resp = await this.fetch(`/workflows/${slug}`, {
32
+ method: 'PUT',
33
+ body: JSON.stringify({ source, name }),
34
+ });
35
+ if (resp.status === 404) {
36
+ resp = await this.fetch('/workflows', {
37
+ method: 'POST',
38
+ body: JSON.stringify({ source, name }),
39
+ });
40
+ }
41
+ if (!resp.ok) {
42
+ const err = await resp.json().catch(() => ({ error: resp.statusText }));
43
+ throw new Error(err.error ?? `Push failed: ${resp.status}`);
44
+ }
45
+ const data = await resp.json();
46
+ return data.workflow;
47
+ }
48
+ async deploy(slug) {
49
+ const resp = await this.fetch(`/workflows/${slug}/deploy`, { method: 'POST' });
50
+ if (!resp.ok) {
51
+ const err = await resp.json().catch(() => ({ error: resp.statusText }));
52
+ throw new Error(err.error ?? `Deploy failed: ${resp.status}`);
53
+ }
54
+ const data = await resp.json();
55
+ return data.deployment;
56
+ }
57
+ async undeploy(slug) {
58
+ const resp = await this.fetch(`/deployments/${slug}`, { method: 'DELETE' });
59
+ if (!resp.ok && resp.status !== 404)
60
+ throw new Error(`Undeploy failed: ${resp.status}`);
61
+ }
62
+ async listDeployments() {
63
+ const resp = await this.fetch('/deployments');
64
+ if (!resp.ok)
65
+ throw new Error(`List failed: ${resp.status}`);
66
+ const data = await resp.json();
67
+ return data.deployments;
68
+ }
69
+ // Usage
70
+ async getUsage() {
71
+ const resp = await this.fetch('/monitoring/usage');
72
+ if (!resp.ok)
73
+ return { executions: 0, aiCalls: 0, plan: 'unknown' };
74
+ return await resp.json();
75
+ }
76
+ // AI Chat streaming
77
+ async *streamChat(message, conversationId) {
78
+ const resp = await this.fetch('/ai-chat/stream', {
79
+ method: 'POST',
80
+ body: JSON.stringify({ message, conversationId }),
81
+ });
82
+ if (!resp.ok) {
83
+ const err = await resp.text();
84
+ throw new Error(`AI chat failed: ${resp.status} ${err.slice(0, 200)}`);
85
+ }
86
+ if (!resp.body)
87
+ return;
88
+ const reader = resp.body.getReader();
89
+ const decoder = new TextDecoder();
90
+ let buffer = '';
91
+ while (true) {
92
+ const { done, value } = await reader.read();
93
+ if (done)
94
+ break;
95
+ buffer += decoder.decode(value, { stream: true });
96
+ const lines = buffer.split('\n');
97
+ buffer = lines.pop() || '';
98
+ for (const line of lines) {
99
+ if (!line.startsWith('data: '))
100
+ continue;
101
+ try {
102
+ yield JSON.parse(line.slice(6));
103
+ }
104
+ catch { /* skip non-JSON */ }
105
+ }
106
+ }
107
+ }
108
+ // Validate connection
109
+ async validate() {
110
+ try {
111
+ const resp = await this.fetch('/ready');
112
+ return resp.ok;
113
+ }
114
+ catch {
115
+ return false;
116
+ }
117
+ }
118
+ }
119
+ export function createPlatformClient(creds) {
120
+ return new PlatformClient(creds);
121
+ }
122
+ //# sourceMappingURL=platform-client.js.map