@stacknet/stacks 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.
Files changed (71) hide show
  1. package/README.md +103 -0
  2. package/dist/billing-BaJlf_S8.d.cts +1154 -0
  3. package/dist/billing-BaJlf_S8.d.ts +1154 -0
  4. package/dist/clients/index.cjs +4 -0
  5. package/dist/clients/index.d.cts +752 -0
  6. package/dist/clients/index.d.ts +752 -0
  7. package/dist/clients/index.js +4 -0
  8. package/dist/index-DVzKiF_0.d.cts +198 -0
  9. package/dist/index-DVzKiF_0.d.ts +198 -0
  10. package/dist/index.cjs +17 -0
  11. package/dist/index.d.cts +309 -0
  12. package/dist/index.d.ts +309 -0
  13. package/dist/index.js +17 -0
  14. package/dist/proxy/index.cjs +2 -0
  15. package/dist/proxy/index.d.cts +1 -0
  16. package/dist/proxy/index.d.ts +1 -0
  17. package/dist/proxy/index.js +2 -0
  18. package/dist/streaming/index.cjs +13 -0
  19. package/dist/streaming/index.d.cts +145 -0
  20. package/dist/streaming/index.d.ts +145 -0
  21. package/dist/streaming/index.js +13 -0
  22. package/dist/types/index.cjs +1 -0
  23. package/dist/types/index.d.cts +335 -0
  24. package/dist/types/index.d.ts +335 -0
  25. package/dist/types/index.js +1 -0
  26. package/package.json +75 -0
  27. package/src/clients/agents.ts +233 -0
  28. package/src/clients/billing.ts +157 -0
  29. package/src/clients/coder.ts +655 -0
  30. package/src/clients/files.ts +86 -0
  31. package/src/clients/index.ts +93 -0
  32. package/src/clients/magma.ts +299 -0
  33. package/src/clients/mcp.ts +208 -0
  34. package/src/clients/network.ts +118 -0
  35. package/src/clients/points.ts +403 -0
  36. package/src/clients/skills.ts +236 -0
  37. package/src/clients/social.ts +286 -0
  38. package/src/clients/stack-management.ts +279 -0
  39. package/src/clients/task-network.ts +303 -0
  40. package/src/clients/user.ts +84 -0
  41. package/src/clients/widgets.ts +171 -0
  42. package/src/index.ts +387 -0
  43. package/src/managers/index.ts +10 -0
  44. package/src/managers/task-manager.ts +310 -0
  45. package/src/proxy/forwarder.ts +146 -0
  46. package/src/proxy/index.ts +32 -0
  47. package/src/proxy/route-handlers.ts +950 -0
  48. package/src/streaming/component-stream.ts +319 -0
  49. package/src/streaming/index.ts +21 -0
  50. package/src/streaming/sse.ts +241 -0
  51. package/src/types/agent.ts +106 -0
  52. package/src/types/billing.ts +87 -0
  53. package/src/types/chat.ts +58 -0
  54. package/src/types/coder.ts +345 -0
  55. package/src/types/credential.ts +111 -0
  56. package/src/types/file.ts +15 -0
  57. package/src/types/imagination.ts +50 -0
  58. package/src/types/index.ts +20 -0
  59. package/src/types/mcp.ts +35 -0
  60. package/src/types/network.ts +97 -0
  61. package/src/types/points.ts +250 -0
  62. package/src/types/skill.ts +107 -0
  63. package/src/types/social.ts +109 -0
  64. package/src/types/stack.ts +269 -0
  65. package/src/types/task.ts +41 -0
  66. package/src/types/user.ts +29 -0
  67. package/src/types/widget.ts +57 -0
  68. package/src/utils/constants.ts +26 -0
  69. package/src/utils/errors.ts +169 -0
  70. package/src/utils/helpers.ts +85 -0
  71. package/src/utils/index.ts +7 -0
@@ -0,0 +1,319 @@
1
+ /**
2
+ * Component Streaming - Artifact streaming adapter
3
+ *
4
+ * Converts tool call responses to artifact data stream format
5
+ */
6
+
7
+ export interface DataStreamWriter {
8
+ write(data: { type: string; data: unknown; transient?: boolean }): void;
9
+ }
10
+
11
+ export interface ArtifactToolCall {
12
+ id: string;
13
+ type: 'function';
14
+ function: {
15
+ name: string;
16
+ arguments: string;
17
+ };
18
+ }
19
+
20
+ export interface ArtifactToolResult {
21
+ type: string;
22
+ id?: string;
23
+ title?: string;
24
+ kind?: string;
25
+ content?: string;
26
+ message?: string;
27
+ [key: string]: unknown;
28
+ }
29
+
30
+ /**
31
+ * Artifact streaming adapter for AI SDK integration
32
+ */
33
+ export class ComponentStreamAdapter {
34
+ constructor(private dataStream: DataStreamWriter) {}
35
+
36
+ /**
37
+ * Process tool calls and convert to artifact stream events
38
+ */
39
+ processToolCalls(toolCalls: ArtifactToolCall[]): void {
40
+ if (!toolCalls || toolCalls.length === 0) return;
41
+
42
+ for (const toolCall of toolCalls) {
43
+ try {
44
+ const args = JSON.parse(toolCall.function.arguments);
45
+ const toolName = toolCall.function.name;
46
+
47
+ switch (toolName) {
48
+ case 'create_document':
49
+ this.handleCreateDocument(args, toolCall.id);
50
+ break;
51
+ case 'update_document':
52
+ this.handleUpdateDocument(args, toolCall.id);
53
+ break;
54
+ default:
55
+ console.log(`[ComponentStream] Unknown tool: ${toolName}`);
56
+ }
57
+ } catch (error) {
58
+ console.error('[ComponentStream] Error processing tool call:', error);
59
+ }
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Process tool results
65
+ */
66
+ processToolResult(result: ArtifactToolResult): void {
67
+ try {
68
+ if (result.type === 'artifact') {
69
+ this.handleArtifactResult(result);
70
+ } else if (result.type === 'artifact_update') {
71
+ this.handleArtifactUpdate(result);
72
+ }
73
+ } catch (error) {
74
+ console.error('[ComponentStream] Error processing tool result:', error);
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Stream an artifact with metadata
80
+ */
81
+ streamArtifact(artifact: {
82
+ id: string;
83
+ kind: string;
84
+ title: string;
85
+ content: string;
86
+ }): void {
87
+ this.dataStream.write({
88
+ type: 'data-kind',
89
+ data: artifact.kind,
90
+ transient: true,
91
+ });
92
+
93
+ this.dataStream.write({
94
+ type: 'data-id',
95
+ data: artifact.id,
96
+ transient: true,
97
+ });
98
+
99
+ this.dataStream.write({
100
+ type: 'data-title',
101
+ data: artifact.title,
102
+ transient: true,
103
+ });
104
+
105
+ this.dataStream.write({
106
+ type: 'data-clear',
107
+ data: null,
108
+ transient: true,
109
+ });
110
+
111
+ this.streamContent(artifact.content);
112
+
113
+ this.dataStream.write({
114
+ type: 'data-finish',
115
+ data: null,
116
+ transient: true,
117
+ });
118
+ }
119
+
120
+ /**
121
+ * Stream a text delta
122
+ */
123
+ streamTextDelta(delta: string): void {
124
+ this.dataStream.write({
125
+ type: 'data-textDelta',
126
+ data: delta,
127
+ transient: true,
128
+ });
129
+ }
130
+
131
+ /**
132
+ * Stream content in chunks
133
+ */
134
+ streamContent(content: string, chunkSize: number = 50): void {
135
+ const chunks = this.splitIntoChunks(content, chunkSize);
136
+
137
+ for (const chunk of chunks) {
138
+ this.dataStream.write({
139
+ type: 'data-textDelta',
140
+ data: chunk,
141
+ transient: true,
142
+ });
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Signal finish
148
+ */
149
+ finish(): void {
150
+ this.dataStream.write({
151
+ type: 'data-finish',
152
+ data: null,
153
+ transient: true,
154
+ });
155
+ }
156
+
157
+ private handleCreateDocument(
158
+ args: { title: string; kind: string; content: string },
159
+ toolCallId: string
160
+ ): void {
161
+ const { title, kind, content } = args;
162
+ const documentId = `doc_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
163
+
164
+ console.log(`[ComponentStream] Creating document: ${title} (${kind})`);
165
+
166
+ this.streamArtifact({
167
+ id: documentId,
168
+ kind,
169
+ title,
170
+ content,
171
+ });
172
+
173
+ this.dataStream.write({
174
+ type: 'tool-result',
175
+ data: {
176
+ toolCallId,
177
+ toolName: 'create_document',
178
+ result: {
179
+ id: documentId,
180
+ title,
181
+ kind,
182
+ message: 'Document created successfully',
183
+ },
184
+ },
185
+ });
186
+ }
187
+
188
+ private handleUpdateDocument(
189
+ args: { id: string; description: string; content: string },
190
+ toolCallId: string
191
+ ): void {
192
+ const { id, description, content } = args;
193
+
194
+ console.log(`[ComponentStream] Updating document: ${id}`);
195
+
196
+ this.dataStream.write({
197
+ type: 'data-clear',
198
+ data: null,
199
+ transient: true,
200
+ });
201
+
202
+ this.streamContent(content);
203
+
204
+ this.dataStream.write({
205
+ type: 'data-finish',
206
+ data: null,
207
+ transient: true,
208
+ });
209
+
210
+ this.dataStream.write({
211
+ type: 'tool-result',
212
+ data: {
213
+ toolCallId,
214
+ toolName: 'update_document',
215
+ result: {
216
+ id,
217
+ message: `Document updated: ${description}`,
218
+ },
219
+ },
220
+ });
221
+ }
222
+
223
+ private handleArtifactResult(result: ArtifactToolResult): void {
224
+ const { id, title, kind, content } = result;
225
+
226
+ if (!id || !title || !kind || !content) {
227
+ console.error('[ComponentStream] Invalid artifact result:', result);
228
+ return;
229
+ }
230
+
231
+ this.streamArtifact({ id, kind, title, content });
232
+ }
233
+
234
+ private handleArtifactUpdate(result: ArtifactToolResult): void {
235
+ const { content } = result;
236
+
237
+ if (!content) {
238
+ console.error('[ComponentStream] Invalid artifact update:', result);
239
+ return;
240
+ }
241
+
242
+ this.dataStream.write({
243
+ type: 'data-clear',
244
+ data: null,
245
+ transient: true,
246
+ });
247
+
248
+ this.streamContent(content);
249
+
250
+ this.dataStream.write({
251
+ type: 'data-finish',
252
+ data: null,
253
+ transient: true,
254
+ });
255
+ }
256
+
257
+ private splitIntoChunks(content: string, chunkSize: number): string[] {
258
+ const chunks: string[] = [];
259
+ const words = content.split(' ');
260
+ let currentChunk = '';
261
+
262
+ for (let i = 0; i < words.length; i++) {
263
+ const word = i === 0 ? words[i] : ' ' + words[i];
264
+
265
+ if (currentChunk.length + word.length > chunkSize && currentChunk.length > 0) {
266
+ chunks.push(currentChunk);
267
+ currentChunk = word.trim();
268
+ } else {
269
+ currentChunk += word;
270
+ }
271
+ }
272
+
273
+ if (currentChunk.length > 0) {
274
+ chunks.push(currentChunk);
275
+ }
276
+
277
+ return chunks;
278
+ }
279
+ }
280
+
281
+ /**
282
+ * Helper to detect tool calls in a message
283
+ */
284
+ export function hasToolCalls(message: {
285
+ tool_calls?: unknown[];
286
+ }): boolean {
287
+ return !!(
288
+ message?.tool_calls &&
289
+ Array.isArray(message.tool_calls) &&
290
+ message.tool_calls.length > 0
291
+ );
292
+ }
293
+
294
+ /**
295
+ * Helper to extract tool results from content
296
+ */
297
+ export function extractToolResults(content: string): ArtifactToolResult[] {
298
+ try {
299
+ const parsed = JSON.parse(content);
300
+ if (
301
+ parsed.type &&
302
+ (parsed.type === 'artifact' || parsed.type === 'artifact_update')
303
+ ) {
304
+ return [parsed];
305
+ }
306
+ } catch {
307
+ // Not JSON
308
+ }
309
+ return [];
310
+ }
311
+
312
+ /**
313
+ * Factory function
314
+ */
315
+ export function createComponentStreamAdapter(
316
+ dataStream: DataStreamWriter
317
+ ): ComponentStreamAdapter {
318
+ return new ComponentStreamAdapter(dataStream);
319
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @stacknet/stacks - Streaming utilities
3
+ */
4
+
5
+ export {
6
+ parseSSEStream,
7
+ createSSEResponse,
8
+ SSEWriter,
9
+ createSSEWriter,
10
+ type SSEEvent,
11
+ } from './sse';
12
+
13
+ export {
14
+ ComponentStreamAdapter,
15
+ createComponentStreamAdapter,
16
+ hasToolCalls,
17
+ extractToolResults,
18
+ type DataStreamWriter,
19
+ type ArtifactToolCall,
20
+ type ArtifactToolResult,
21
+ } from './component-stream';
@@ -0,0 +1,241 @@
1
+ /**
2
+ * SSE (Server-Sent Events) streaming utilities
3
+ */
4
+
5
+ import { SSE_HEADERS } from '../utils/constants';
6
+
7
+ export interface SSEEvent {
8
+ event?: string;
9
+ data: unknown;
10
+ id?: string;
11
+ retry?: number;
12
+ }
13
+
14
+ /**
15
+ * Parse SSE stream from a Response
16
+ */
17
+ export async function* parseSSEStream(
18
+ response: Response
19
+ ): AsyncIterable<SSEEvent> {
20
+ if (!response.body) {
21
+ throw new Error('Response body is null');
22
+ }
23
+
24
+ const reader = response.body.getReader();
25
+ const decoder = new TextDecoder();
26
+ let buffer = '';
27
+ let currentEvent: Partial<SSEEvent> = {};
28
+
29
+ try {
30
+ while (true) {
31
+ const { done, value } = await reader.read();
32
+ if (done) break;
33
+
34
+ buffer += decoder.decode(value, { stream: true });
35
+ const lines = buffer.split('\n');
36
+ buffer = lines.pop() || '';
37
+
38
+ for (const line of lines) {
39
+ if (line === '') {
40
+ // Empty line means end of event
41
+ if (currentEvent.data !== undefined) {
42
+ yield currentEvent as SSEEvent;
43
+ }
44
+ currentEvent = {};
45
+ continue;
46
+ }
47
+
48
+ if (line.startsWith(':')) {
49
+ // Comment, skip
50
+ continue;
51
+ }
52
+
53
+ const colonIndex = line.indexOf(':');
54
+ if (colonIndex === -1) continue;
55
+
56
+ const field = line.slice(0, colonIndex);
57
+ const value = line.slice(colonIndex + 1).trimStart();
58
+
59
+ switch (field) {
60
+ case 'event':
61
+ currentEvent.event = value;
62
+ break;
63
+ case 'data':
64
+ try {
65
+ currentEvent.data = JSON.parse(value);
66
+ } catch {
67
+ currentEvent.data = value;
68
+ }
69
+ break;
70
+ case 'id':
71
+ currentEvent.id = value;
72
+ break;
73
+ case 'retry':
74
+ currentEvent.retry = parseInt(value, 10);
75
+ break;
76
+ }
77
+ }
78
+ }
79
+
80
+ // Emit any remaining event
81
+ if (currentEvent.data !== undefined) {
82
+ yield currentEvent as SSEEvent;
83
+ }
84
+ } finally {
85
+ reader.releaseLock();
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Create an SSE Response from an async iterable
91
+ */
92
+ export function createSSEResponse(
93
+ stream: AsyncIterable<SSEEvent>,
94
+ headers?: Record<string, string>
95
+ ): Response {
96
+ const encoder = new TextEncoder();
97
+
98
+ const readableStream = new ReadableStream({
99
+ async start(controller) {
100
+ try {
101
+ for await (const event of stream) {
102
+ let message = '';
103
+
104
+ if (event.event) {
105
+ message += `event: ${event.event}\n`;
106
+ }
107
+
108
+ if (event.id) {
109
+ message += `id: ${event.id}\n`;
110
+ }
111
+
112
+ if (event.retry) {
113
+ message += `retry: ${event.retry}\n`;
114
+ }
115
+
116
+ const data =
117
+ typeof event.data === 'string'
118
+ ? event.data
119
+ : JSON.stringify(event.data);
120
+
121
+ message += `data: ${data}\n\n`;
122
+
123
+ controller.enqueue(encoder.encode(message));
124
+ }
125
+ } catch (error) {
126
+ console.error('SSE stream error:', error);
127
+ } finally {
128
+ controller.close();
129
+ }
130
+ },
131
+ });
132
+
133
+ return new Response(readableStream, {
134
+ headers: {
135
+ ...SSE_HEADERS,
136
+ ...headers,
137
+ },
138
+ });
139
+ }
140
+
141
+ /**
142
+ * Create an SSE writer for streaming events
143
+ */
144
+ export class SSEWriter {
145
+ private encoder = new TextEncoder();
146
+ private controller: ReadableStreamDefaultController<Uint8Array> | null = null;
147
+ private stream: ReadableStream<Uint8Array>;
148
+
149
+ constructor() {
150
+ this.stream = new ReadableStream({
151
+ start: (controller) => {
152
+ this.controller = controller;
153
+ },
154
+ });
155
+ }
156
+
157
+ /**
158
+ * Get the underlying ReadableStream
159
+ */
160
+ getStream(): ReadableStream<Uint8Array> {
161
+ return this.stream;
162
+ }
163
+
164
+ /**
165
+ * Get a Response object for this stream
166
+ */
167
+ getResponse(headers?: Record<string, string>): Response {
168
+ return new Response(this.stream, {
169
+ headers: {
170
+ ...SSE_HEADERS,
171
+ ...headers,
172
+ },
173
+ });
174
+ }
175
+
176
+ /**
177
+ * Write an SSE event
178
+ */
179
+ write(event: SSEEvent): void {
180
+ if (!this.controller) return;
181
+
182
+ let message = '';
183
+
184
+ if (event.event) {
185
+ message += `event: ${event.event}\n`;
186
+ }
187
+
188
+ if (event.id) {
189
+ message += `id: ${event.id}\n`;
190
+ }
191
+
192
+ const data =
193
+ typeof event.data === 'string' ? event.data : JSON.stringify(event.data);
194
+
195
+ message += `data: ${data}\n\n`;
196
+
197
+ this.controller.enqueue(this.encoder.encode(message));
198
+ }
199
+
200
+ /**
201
+ * Write a data-only event
202
+ */
203
+ writeData(data: unknown): void {
204
+ this.write({ data });
205
+ }
206
+
207
+ /**
208
+ * Write a comment (for keep-alive)
209
+ */
210
+ writeComment(comment: string): void {
211
+ if (!this.controller) return;
212
+ this.controller.enqueue(this.encoder.encode(`: ${comment}\n\n`));
213
+ }
214
+
215
+ /**
216
+ * Close the stream
217
+ */
218
+ close(): void {
219
+ if (this.controller) {
220
+ this.controller.close();
221
+ this.controller = null;
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Signal an error
227
+ */
228
+ error(err: Error): void {
229
+ if (this.controller) {
230
+ this.controller.error(err);
231
+ this.controller = null;
232
+ }
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Create an SSE writer
238
+ */
239
+ export function createSSEWriter(): SSEWriter {
240
+ return new SSEWriter();
241
+ }
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Agent type definitions
3
+ */
4
+
5
+ export interface Agent {
6
+ id: string;
7
+ name: string;
8
+ description?: string;
9
+ system_prompt?: string;
10
+ model?: string;
11
+ tools?: string[];
12
+ triggers?: AgentTrigger[];
13
+ workflow?: AgentWorkflow;
14
+ visibility?: 'public' | 'private' | 'template';
15
+ creator_mid?: string;
16
+ created_at?: string;
17
+ updated_at?: string;
18
+ enabled?: boolean;
19
+ }
20
+
21
+ export interface AgentTrigger {
22
+ type: 'webhook' | 'cron' | 'event';
23
+ config: Record<string, unknown>;
24
+ }
25
+
26
+ export interface AgentWorkflow {
27
+ nodes: AgentWorkflowNode[];
28
+ edges: AgentWorkflowEdge[];
29
+ }
30
+
31
+ export interface AgentWorkflowNode {
32
+ id: string;
33
+ type: string;
34
+ position: { x: number; y: number };
35
+ data: Record<string, unknown>;
36
+ }
37
+
38
+ export interface AgentWorkflowEdge {
39
+ id: string;
40
+ source: string;
41
+ target: string;
42
+ sourceHandle?: string;
43
+ targetHandle?: string;
44
+ }
45
+
46
+ export interface AgentExecuteRequest {
47
+ input?: string;
48
+ context?: Record<string, unknown>;
49
+ stream?: boolean;
50
+ }
51
+
52
+ export interface AgentExecuteResponse {
53
+ id: string;
54
+ output: string;
55
+ status: 'success' | 'error';
56
+ metadata?: Record<string, unknown>;
57
+ }
58
+
59
+ export interface AgentsListResponse {
60
+ agents: Agent[];
61
+ }
62
+
63
+ export interface AgentResponse {
64
+ agent?: Agent;
65
+ id?: string;
66
+ name?: string;
67
+ error?: string;
68
+ }
69
+
70
+ export interface AgentCreateInput {
71
+ name: string;
72
+ description?: string;
73
+ system_prompt?: string;
74
+ model?: string;
75
+ tools?: string[];
76
+ triggers?: AgentTrigger[];
77
+ workflow?: AgentWorkflow;
78
+ visibility?: 'public' | 'private' | 'template';
79
+ created_by?: string;
80
+ }
81
+
82
+ export interface AgentUpdateInput {
83
+ name?: string;
84
+ description?: string;
85
+ system_prompt?: string;
86
+ model?: string;
87
+ tools?: string[];
88
+ triggers?: AgentTrigger[];
89
+ workflow?: AgentWorkflow;
90
+ visibility?: 'public' | 'private' | 'template';
91
+ enabled?: boolean;
92
+ }
93
+
94
+ export interface AgentFromPromptInput {
95
+ prompt: string;
96
+ created_by?: string;
97
+ }
98
+
99
+ export interface AgentsClientConfig {
100
+ baseUrl?: string;
101
+ /**
102
+ * Use the coprocessor API (/cpx/agent/agents) instead of legacy (/agents).
103
+ * Defaults to true. Set to false for backwards compatibility.
104
+ */
105
+ useCpxApi?: boolean;
106
+ }