@agentuity/runtime 0.0.104 → 0.0.106

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 (125) hide show
  1. package/README.md +61 -35
  2. package/dist/_context.d.ts +2 -1
  3. package/dist/_context.d.ts.map +1 -1
  4. package/dist/_context.js +1 -0
  5. package/dist/_context.js.map +1 -1
  6. package/dist/_metadata.d.ts +4 -0
  7. package/dist/_metadata.d.ts.map +1 -1
  8. package/dist/_metadata.js +28 -1
  9. package/dist/_metadata.js.map +1 -1
  10. package/dist/_server.d.ts +1 -1
  11. package/dist/_server.d.ts.map +1 -1
  12. package/dist/_server.js +4 -1
  13. package/dist/_server.js.map +1 -1
  14. package/dist/_services.d.ts +2 -1
  15. package/dist/_services.d.ts.map +1 -1
  16. package/dist/_services.js +11 -2
  17. package/dist/_services.js.map +1 -1
  18. package/dist/_standalone.d.ts +2 -1
  19. package/dist/_standalone.d.ts.map +1 -1
  20. package/dist/_standalone.js +1 -0
  21. package/dist/_standalone.js.map +1 -1
  22. package/dist/_tokens.d.ts +9 -1
  23. package/dist/_tokens.d.ts.map +1 -1
  24. package/dist/_tokens.js +5 -8
  25. package/dist/_tokens.js.map +1 -1
  26. package/dist/agent.d.ts +26 -2
  27. package/dist/agent.d.ts.map +1 -1
  28. package/dist/agent.js +10 -0
  29. package/dist/agent.js.map +1 -1
  30. package/dist/app.d.ts +7 -3
  31. package/dist/app.d.ts.map +1 -1
  32. package/dist/app.js.map +1 -1
  33. package/dist/bun-s3-patch.d.ts +26 -0
  34. package/dist/bun-s3-patch.d.ts.map +1 -0
  35. package/dist/bun-s3-patch.js +65 -0
  36. package/dist/bun-s3-patch.js.map +1 -0
  37. package/dist/handlers/cron.d.ts +47 -0
  38. package/dist/handlers/cron.d.ts.map +1 -0
  39. package/dist/handlers/cron.js +49 -0
  40. package/dist/handlers/cron.js.map +1 -0
  41. package/dist/handlers/index.d.ts +5 -0
  42. package/dist/handlers/index.d.ts.map +1 -0
  43. package/dist/handlers/index.js +5 -0
  44. package/dist/handlers/index.js.map +1 -0
  45. package/dist/handlers/sse.d.ts +74 -0
  46. package/dist/handlers/sse.d.ts.map +1 -0
  47. package/dist/handlers/sse.js +70 -0
  48. package/dist/handlers/sse.js.map +1 -0
  49. package/dist/handlers/stream.d.ts +52 -0
  50. package/dist/handlers/stream.d.ts.map +1 -0
  51. package/dist/handlers/stream.js +75 -0
  52. package/dist/handlers/stream.js.map +1 -0
  53. package/dist/handlers/websocket.d.ts +49 -0
  54. package/dist/handlers/websocket.d.ts.map +1 -0
  55. package/dist/handlers/websocket.js +130 -0
  56. package/dist/handlers/websocket.js.map +1 -0
  57. package/dist/index.d.ts +2 -1
  58. package/dist/index.d.ts.map +1 -1
  59. package/dist/index.js +4 -2
  60. package/dist/index.js.map +1 -1
  61. package/dist/middleware.d.ts +1 -1
  62. package/dist/middleware.d.ts.map +1 -1
  63. package/dist/middleware.js +8 -6
  64. package/dist/middleware.js.map +1 -1
  65. package/dist/otel/logger.d.ts +1 -4
  66. package/dist/otel/logger.d.ts.map +1 -1
  67. package/dist/otel/logger.js +11 -2
  68. package/dist/otel/logger.js.map +1 -1
  69. package/dist/router.d.ts +46 -236
  70. package/dist/router.d.ts.map +1 -1
  71. package/dist/router.js +82 -349
  72. package/dist/router.js.map +1 -1
  73. package/dist/services/sandbox/http.d.ts +13 -0
  74. package/dist/services/sandbox/http.d.ts.map +1 -0
  75. package/dist/services/sandbox/http.js +130 -0
  76. package/dist/services/sandbox/http.js.map +1 -0
  77. package/dist/services/sandbox/index.d.ts +2 -0
  78. package/dist/services/sandbox/index.d.ts.map +1 -0
  79. package/dist/services/sandbox/index.js +2 -0
  80. package/dist/services/sandbox/index.js.map +1 -0
  81. package/dist/services/session/http.d.ts +12 -1
  82. package/dist/services/session/http.d.ts.map +1 -1
  83. package/dist/services/session/http.js +31 -1
  84. package/dist/services/session/http.js.map +1 -1
  85. package/dist/services/thread/local.d.ts.map +1 -1
  86. package/dist/services/thread/local.js +7 -6
  87. package/dist/services/thread/local.js.map +1 -1
  88. package/dist/session.d.ts +35 -8
  89. package/dist/session.d.ts.map +1 -1
  90. package/dist/session.js +25 -24
  91. package/dist/session.js.map +1 -1
  92. package/dist/workbench.d.ts +6 -2
  93. package/dist/workbench.d.ts.map +1 -1
  94. package/dist/workbench.js +40 -34
  95. package/dist/workbench.js.map +1 -1
  96. package/package.json +5 -7
  97. package/src/_context.ts +2 -0
  98. package/src/_metadata.ts +37 -1
  99. package/src/_server.ts +4 -1
  100. package/src/_services.ts +12 -2
  101. package/src/_standalone.ts +7 -1
  102. package/src/_tokens.ts +5 -9
  103. package/src/agent.ts +40 -1
  104. package/src/app.ts +9 -2
  105. package/src/bun-s3-patch.ts +91 -0
  106. package/src/handlers/cron.ts +70 -0
  107. package/src/handlers/index.ts +4 -0
  108. package/src/handlers/sse.ts +118 -0
  109. package/src/handlers/stream.ts +86 -0
  110. package/src/handlers/websocket.ts +153 -0
  111. package/src/index.ts +18 -3
  112. package/src/middleware.ts +8 -10
  113. package/src/otel/logger.ts +13 -2
  114. package/src/router.ts +110 -597
  115. package/src/services/sandbox/http.ts +215 -0
  116. package/src/services/sandbox/index.ts +1 -0
  117. package/src/services/session/http.ts +39 -1
  118. package/src/services/thread/local.ts +8 -5
  119. package/src/session.ts +58 -32
  120. package/src/workbench.ts +41 -39
  121. package/dist/io/email.d.ts +0 -77
  122. package/dist/io/email.d.ts.map +0 -1
  123. package/dist/io/email.js +0 -162
  124. package/dist/io/email.js.map +0 -1
  125. package/src/io/email.ts +0 -191
package/dist/io/email.js DELETED
@@ -1,162 +0,0 @@
1
- import { StructuredError } from '@agentuity/core';
2
- import { simpleParser } from 'mailparser';
3
- /**
4
- * A class representing an email with common information for processing.
5
- *
6
- * This class wraps the parsed email message and provides convenient accessor methods
7
- * for common email properties like subject, sender, recipient, body content, etc.
8
- */
9
- export class Email {
10
- _message;
11
- constructor(data) {
12
- this._message = data;
13
- }
14
- toString() {
15
- return `[Email id=${this.messageId()},from=${this.fromEmail()},subject=${this.subject()}]`;
16
- }
17
- /**
18
- * The date of the email.
19
- */
20
- date() {
21
- return this._message.date ?? null;
22
- }
23
- /**
24
- * The message ID of the email.
25
- */
26
- messageId() {
27
- return this._message.messageId ?? null;
28
- }
29
- /**
30
- * The headers of the email.
31
- */
32
- headers() {
33
- return this._message.headers;
34
- }
35
- /**
36
- * The email address of the recipient or null if there is no recipient.
37
- *
38
- * If the email has multiple recipients, the email addresses are comma separated.
39
- */
40
- to() {
41
- if (!this._message.to) {
42
- return null;
43
- }
44
- if (Array.isArray(this._message.to)) {
45
- return this._message.to
46
- .map((addr) => (addr.text ?? '').trim())
47
- .filter((text) => text.length > 0)
48
- .join(', ');
49
- }
50
- if (typeof this._message.to === 'object' && 'text' in this._message.to) {
51
- return this._message.to.text ? this._message.to.text.trim() : null;
52
- }
53
- return null;
54
- }
55
- /**
56
- * The email address of the sender or null if there is no sender.
57
- */
58
- fromEmail() {
59
- return this._message.from?.value[0]?.address ?? null;
60
- }
61
- /**
62
- * The name of the sender or null if there is no name.
63
- */
64
- fromName() {
65
- return this._message.from?.value[0]?.name ?? null;
66
- }
67
- /**
68
- * The email address of the first recipient or null if there is no recipient.
69
- */
70
- toEmail() {
71
- if (!this._message.to) {
72
- return null;
73
- }
74
- if (Array.isArray(this._message.to)) {
75
- return this._message.to[0]?.value[0]?.address ?? null;
76
- }
77
- if (typeof this._message.to === 'object' && 'value' in this._message.to) {
78
- return this._message.to.value[0]?.address ?? null;
79
- }
80
- return null;
81
- }
82
- /**
83
- * The name of the first recipient or null if there is no name.
84
- */
85
- toName() {
86
- if (!this._message.to) {
87
- return null;
88
- }
89
- if (Array.isArray(this._message.to)) {
90
- return this._message.to[0]?.value[0]?.name ?? null;
91
- }
92
- if (typeof this._message.to === 'object' && 'value' in this._message.to) {
93
- return this._message.to.value[0]?.name ?? null;
94
- }
95
- return null;
96
- }
97
- /**
98
- * The subject of the email or null if there is no subject.
99
- */
100
- subject() {
101
- return this._message.subject ?? null;
102
- }
103
- /**
104
- * The plain text body of the email or null if there is no plain text body.
105
- */
106
- text() {
107
- return this._message.text ?? null;
108
- }
109
- /**
110
- * The HTML body of the email or null if there is no HTML body.
111
- */
112
- html() {
113
- return this._message.html ? this._message.html : null;
114
- }
115
- /**
116
- * The attachments of the email or an empty array if there are no attachments.
117
- *
118
- * Note: Attachment handling is minimal in this implementation.
119
- * For full attachment support with SSRF protection, see the sdk-js implementation.
120
- */
121
- attachments() {
122
- if (!this._message.attachments || this._message.attachments.length === 0) {
123
- return [];
124
- }
125
- return this._message.attachments.map((att) => ({
126
- filename: att.filename ?? 'unknown',
127
- contentType: att.contentType ?? 'application/octet-stream',
128
- }));
129
- }
130
- }
131
- const InvalidEmailEmptyError = StructuredError('InvalidEmailEmptyError', 'Failed to parse email: empty buffer');
132
- const InvalidEmailHeadersError = StructuredError('InvalidEmailHeadersError', 'Failed to parse email: missing headers');
133
- const InvalidEmailParseError = StructuredError('InvalidEmailParseError', 'Failed to parse email: body was invalid')();
134
- /**
135
- * Parse an email from a buffer and return an Email object.
136
- *
137
- * @param data - The raw RFC822 email message as a Buffer
138
- * @returns A promise that resolves to an Email object
139
- * @throws Error if the email cannot be parsed or if the input is not a valid RFC822 message
140
- */
141
- export async function parseEmail(data) {
142
- if (data.length === 0) {
143
- throw new InvalidEmailEmptyError();
144
- }
145
- const first16KB = data.slice(0, 16384).toString('utf-8', 0, Math.min(data.length, 16384));
146
- const hasHeaders = /(^|\r?\n)[!-9;-~]+:\s/.test(first16KB);
147
- if (!hasHeaders) {
148
- throw new InvalidEmailHeadersError();
149
- }
150
- try {
151
- const message = await simpleParser(data);
152
- return new Email(message);
153
- }
154
- catch (error) {
155
- throw new InvalidEmailParseError({
156
- bufferSize: data.length,
157
- preview: data.subarray(0, 50).toString('hex'),
158
- cause: error,
159
- });
160
- }
161
- }
162
- //# sourceMappingURL=email.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"email.js","sourceRoot":"","s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src/io/email.ts DELETED
@@ -1,191 +0,0 @@
1
- import { StructuredError } from '@agentuity/core';
2
- import { type ParsedMail, type Headers, simpleParser, type AddressObject } from 'mailparser';
3
-
4
- /**
5
- * A class representing an email with common information for processing.
6
- *
7
- * This class wraps the parsed email message and provides convenient accessor methods
8
- * for common email properties like subject, sender, recipient, body content, etc.
9
- */
10
- export class Email {
11
- private readonly _message: ParsedMail;
12
-
13
- constructor(data: ParsedMail) {
14
- this._message = data;
15
- }
16
-
17
- toString(): string {
18
- return `[Email id=${this.messageId()},from=${this.fromEmail()},subject=${this.subject()}]`;
19
- }
20
-
21
- /**
22
- * The date of the email.
23
- */
24
- date(): Date | null {
25
- return this._message.date ?? null;
26
- }
27
-
28
- /**
29
- * The message ID of the email.
30
- */
31
- messageId(): string | null {
32
- return this._message.messageId ?? null;
33
- }
34
-
35
- /**
36
- * The headers of the email.
37
- */
38
- headers(): Headers {
39
- return this._message.headers;
40
- }
41
-
42
- /**
43
- * The email address of the recipient or null if there is no recipient.
44
- *
45
- * If the email has multiple recipients, the email addresses are comma separated.
46
- */
47
- to(): string | null {
48
- if (!this._message.to) {
49
- return null;
50
- }
51
- if (Array.isArray(this._message.to)) {
52
- return this._message.to
53
- .map((addr: AddressObject) => (addr.text ?? '').trim())
54
- .filter((text: string) => text.length > 0)
55
- .join(', ');
56
- }
57
- if (typeof this._message.to === 'object' && 'text' in this._message.to) {
58
- return this._message.to.text ? this._message.to.text.trim() : null;
59
- }
60
- return null;
61
- }
62
-
63
- /**
64
- * The email address of the sender or null if there is no sender.
65
- */
66
- fromEmail(): string | null {
67
- return this._message.from?.value[0]?.address ?? null;
68
- }
69
-
70
- /**
71
- * The name of the sender or null if there is no name.
72
- */
73
- fromName(): string | null {
74
- return this._message.from?.value[0]?.name ?? null;
75
- }
76
-
77
- /**
78
- * The email address of the first recipient or null if there is no recipient.
79
- */
80
- toEmail(): string | null {
81
- if (!this._message.to) {
82
- return null;
83
- }
84
- if (Array.isArray(this._message.to)) {
85
- return this._message.to[0]?.value[0]?.address ?? null;
86
- }
87
- if (typeof this._message.to === 'object' && 'value' in this._message.to) {
88
- return this._message.to.value[0]?.address ?? null;
89
- }
90
- return null;
91
- }
92
-
93
- /**
94
- * The name of the first recipient or null if there is no name.
95
- */
96
- toName(): string | null {
97
- if (!this._message.to) {
98
- return null;
99
- }
100
- if (Array.isArray(this._message.to)) {
101
- return this._message.to[0]?.value[0]?.name ?? null;
102
- }
103
- if (typeof this._message.to === 'object' && 'value' in this._message.to) {
104
- return this._message.to.value[0]?.name ?? null;
105
- }
106
- return null;
107
- }
108
-
109
- /**
110
- * The subject of the email or null if there is no subject.
111
- */
112
- subject(): string | null {
113
- return this._message.subject ?? null;
114
- }
115
-
116
- /**
117
- * The plain text body of the email or null if there is no plain text body.
118
- */
119
- text(): string | null {
120
- return this._message.text ?? null;
121
- }
122
-
123
- /**
124
- * The HTML body of the email or null if there is no HTML body.
125
- */
126
- html(): string | null {
127
- return this._message.html ? this._message.html : null;
128
- }
129
-
130
- /**
131
- * The attachments of the email or an empty array if there are no attachments.
132
- *
133
- * Note: Attachment handling is minimal in this implementation.
134
- * For full attachment support with SSRF protection, see the sdk-js implementation.
135
- */
136
- attachments(): Array<{ filename: string; contentType: string }> {
137
- if (!this._message.attachments || this._message.attachments.length === 0) {
138
- return [];
139
- }
140
- return this._message.attachments.map((att: { filename?: string; contentType?: string }) => ({
141
- filename: att.filename ?? 'unknown',
142
- contentType: att.contentType ?? 'application/octet-stream',
143
- }));
144
- }
145
- }
146
-
147
- const InvalidEmailEmptyError = StructuredError(
148
- 'InvalidEmailEmptyError',
149
- 'Failed to parse email: empty buffer'
150
- );
151
-
152
- const InvalidEmailHeadersError = StructuredError(
153
- 'InvalidEmailHeadersError',
154
- 'Failed to parse email: missing headers'
155
- );
156
-
157
- const InvalidEmailParseError = StructuredError(
158
- 'InvalidEmailParseError',
159
- 'Failed to parse email: body was invalid'
160
- )<{ bufferSize: number; preview?: string }>();
161
-
162
- /**
163
- * Parse an email from a buffer and return an Email object.
164
- *
165
- * @param data - The raw RFC822 email message as a Buffer
166
- * @returns A promise that resolves to an Email object
167
- * @throws Error if the email cannot be parsed or if the input is not a valid RFC822 message
168
- */
169
- export async function parseEmail(data: Buffer): Promise<Email> {
170
- if (data.length === 0) {
171
- throw new InvalidEmailEmptyError();
172
- }
173
-
174
- const first16KB = data.slice(0, 16384).toString('utf-8', 0, Math.min(data.length, 16384));
175
- const hasHeaders = /(^|\r?\n)[!-9;-~]+:\s/.test(first16KB);
176
-
177
- if (!hasHeaders) {
178
- throw new InvalidEmailHeadersError();
179
- }
180
-
181
- try {
182
- const message = await simpleParser(data);
183
- return new Email(message);
184
- } catch (error) {
185
- throw new InvalidEmailParseError({
186
- bufferSize: data.length,
187
- preview: data.subarray(0, 50).toString('hex'),
188
- cause: error,
189
- });
190
- }
191
- }