@usejunior/email-core 0.1.5 → 0.1.6
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/dist/actions/compose-helpers.d.ts +54 -0
- package/dist/actions/compose-helpers.d.ts.map +1 -0
- package/dist/actions/compose-helpers.js +118 -0
- package/dist/actions/compose-helpers.js.map +1 -0
- package/dist/actions/draft.d.ts +12 -0
- package/dist/actions/draft.d.ts.map +1 -1
- package/dist/actions/draft.js +95 -134
- package/dist/actions/draft.js.map +1 -1
- package/dist/actions/reply.d.ts +6 -0
- package/dist/actions/reply.d.ts.map +1 -1
- package/dist/actions/reply.js +42 -71
- package/dist/actions/reply.js.map +1 -1
- package/dist/actions/send.d.ts +6 -0
- package/dist/actions/send.d.ts.map +1 -1
- package/dist/actions/send.js +51 -101
- package/dist/actions/send.js.map +1 -1
- package/dist/content/body-renderer.d.ts +41 -0
- package/dist/content/body-renderer.d.ts.map +1 -0
- package/dist/content/body-renderer.js +41 -0
- package/dist/content/body-renderer.js.map +1 -0
- package/dist/content/frontmatter.d.ts +3 -0
- package/dist/content/frontmatter.d.ts.map +1 -1
- package/dist/content/frontmatter.js +12 -3
- package/dist/content/frontmatter.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/security/watched-allowlist.d.ts +25 -0
- package/dist/security/watched-allowlist.d.ts.map +1 -0
- package/dist/security/watched-allowlist.js +114 -0
- package/dist/security/watched-allowlist.js.map +1 -0
- package/dist/testing/mock-provider.d.ts.map +1 -1
- package/dist/testing/mock-provider.js +21 -3
- package/dist/testing/mock-provider.js.map +1 -1
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -1
package/dist/actions/reply.js
CHANGED
|
@@ -2,13 +2,19 @@
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { checkSendAllowlist } from '../security/send-allowlist.js';
|
|
4
4
|
import { isPlausibleMessageId } from '../security/reply-validation.js';
|
|
5
|
-
import {
|
|
5
|
+
import { withRetry } from '../providers/provider.js';
|
|
6
|
+
import { renderEmailBody } from '../content/body-renderer.js';
|
|
7
|
+
import { checkMailboxRequired, checkRateLimit, handleProviderError, } from './compose-helpers.js';
|
|
6
8
|
const ReplyToEmailInput = z.object({
|
|
7
9
|
message_id: z.string(),
|
|
8
10
|
body: z.string(),
|
|
9
11
|
mailbox: z.string().optional(),
|
|
10
12
|
cc: z.array(z.string()).optional(),
|
|
11
13
|
draft: z.boolean().optional(),
|
|
14
|
+
format: z.enum(['markdown', 'html', 'text']).optional()
|
|
15
|
+
.describe("Body format. 'markdown' (default) renders via GFM with line-break preservation; 'html' is passthrough; 'text' sends as plain text."),
|
|
16
|
+
force_black: z.boolean().optional()
|
|
17
|
+
.describe('Wrap rendered HTML in a force-black div so Outlook dark mode does not hide the text. Default true.'),
|
|
12
18
|
});
|
|
13
19
|
const ReplyToEmailOutput = z.object({
|
|
14
20
|
success: z.boolean(),
|
|
@@ -22,21 +28,15 @@ const ReplyToEmailOutput = z.object({
|
|
|
22
28
|
});
|
|
23
29
|
export const replyToEmailAction = {
|
|
24
30
|
name: 'reply_to_email',
|
|
25
|
-
description: 'Reply to an email within an existing thread.
|
|
31
|
+
description: 'Reply to an email within an existing thread. Send path gated by send allowlist; draft path bypasses.',
|
|
26
32
|
input: ReplyToEmailInput,
|
|
27
33
|
output: ReplyToEmailOutput,
|
|
28
34
|
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
29
35
|
run: async (ctx, input) => {
|
|
30
36
|
// Check mailbox requirement for multi-mailbox
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
error: {
|
|
35
|
-
code: 'MAILBOX_REQUIRED',
|
|
36
|
-
message: 'mailbox parameter required when multiple mailboxes are configured',
|
|
37
|
-
recoverable: false,
|
|
38
|
-
},
|
|
39
|
-
};
|
|
37
|
+
const mailboxError = checkMailboxRequired(input.mailbox, ctx.allMailboxes);
|
|
38
|
+
if (mailboxError) {
|
|
39
|
+
return { success: false, error: mailboxError };
|
|
40
40
|
}
|
|
41
41
|
// Validate message ID plausibility
|
|
42
42
|
if (!isPlausibleMessageId(input.message_id)) {
|
|
@@ -49,24 +49,11 @@ export const replyToEmailAction = {
|
|
|
49
49
|
},
|
|
50
50
|
};
|
|
51
51
|
}
|
|
52
|
-
//
|
|
53
|
-
const
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (allowlistError) {
|
|
58
|
-
return {
|
|
59
|
-
success: false,
|
|
60
|
-
error: {
|
|
61
|
-
code: 'ALLOWLIST_BLOCKED',
|
|
62
|
-
message: allowlistError.includes('not configured')
|
|
63
|
-
? allowlistError
|
|
64
|
-
: `Recipient not in send allowlist`,
|
|
65
|
-
recoverable: false,
|
|
66
|
-
},
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
// Draft branch — create reply draft instead of sending
|
|
52
|
+
// Render body: markdown → HTML by default
|
|
53
|
+
const rendered = renderEmailBody(input.body, { format: input.format, forceBlack: input.force_black });
|
|
54
|
+
const bodyPlain = rendered.body;
|
|
55
|
+
const bodyHtml = rendered.bodyHtml;
|
|
56
|
+
// Draft branch — create reply draft, bypass allowlist
|
|
70
57
|
if (input.draft) {
|
|
71
58
|
if (!ctx.provider.createReplyDraft) {
|
|
72
59
|
return {
|
|
@@ -79,8 +66,9 @@ export const replyToEmailAction = {
|
|
|
79
66
|
};
|
|
80
67
|
}
|
|
81
68
|
try {
|
|
82
|
-
const draftResult = await ctx.provider.createReplyDraft(input.message_id,
|
|
69
|
+
const draftResult = await ctx.provider.createReplyDraft(input.message_id, bodyPlain, {
|
|
83
70
|
cc: input.cc?.map(email => ({ email })),
|
|
71
|
+
bodyHtml,
|
|
84
72
|
});
|
|
85
73
|
return {
|
|
86
74
|
success: draftResult.success,
|
|
@@ -93,39 +81,35 @@ export const replyToEmailAction = {
|
|
|
93
81
|
};
|
|
94
82
|
}
|
|
95
83
|
catch (err) {
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
success: false,
|
|
99
|
-
error: { code: err.code, message: err.message, recoverable: err.recoverable },
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
success: false,
|
|
104
|
-
error: {
|
|
105
|
-
code: 'DRAFT_FAILED',
|
|
106
|
-
message: err instanceof Error ? err.message : String(err),
|
|
107
|
-
recoverable: false,
|
|
108
|
-
},
|
|
109
|
-
};
|
|
84
|
+
return handleProviderError(err, 'DRAFT_FAILED');
|
|
110
85
|
}
|
|
111
86
|
}
|
|
87
|
+
// Send path — get the original message to check the recipient against allowlist
|
|
88
|
+
const originalMessage = await ctx.provider.getMessage(input.message_id);
|
|
89
|
+
const replyRecipient = originalMessage.from.email;
|
|
90
|
+
// Check send allowlist — reply recipients must also be allowed
|
|
91
|
+
const allowlistError = checkSendAllowlist([replyRecipient], ctx.sendAllowlist);
|
|
92
|
+
if (allowlistError) {
|
|
93
|
+
return {
|
|
94
|
+
success: false,
|
|
95
|
+
error: {
|
|
96
|
+
code: 'ALLOWLIST_BLOCKED',
|
|
97
|
+
message: allowlistError.includes('not configured')
|
|
98
|
+
? allowlistError
|
|
99
|
+
: `Recipient not in send allowlist`,
|
|
100
|
+
recoverable: false,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
112
104
|
// Check rate limit
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
success: false,
|
|
118
|
-
error: {
|
|
119
|
-
code: 'RATE_LIMITED',
|
|
120
|
-
message: `Send rate limit exceeded. Retry after ${rateCheck.retryAfter}s`,
|
|
121
|
-
recoverable: true,
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
}
|
|
105
|
+
const rateLimitError = checkRateLimit(ctx.rateLimiter, 'reply_to_email');
|
|
106
|
+
if (rateLimitError) {
|
|
107
|
+
return rateLimitError;
|
|
125
108
|
}
|
|
126
109
|
try {
|
|
127
|
-
const result = await withRetry(() => ctx.provider.replyToMessage(input.message_id,
|
|
110
|
+
const result = await withRetry(() => ctx.provider.replyToMessage(input.message_id, bodyPlain, {
|
|
128
111
|
cc: input.cc?.map(email => ({ email })),
|
|
112
|
+
bodyHtml,
|
|
129
113
|
}), { maxRetries: 3, baseDelay: 1000 });
|
|
130
114
|
if (ctx.rateLimiter) {
|
|
131
115
|
ctx.rateLimiter.recordUsage('reply_to_email');
|
|
@@ -141,20 +125,7 @@ export const replyToEmailAction = {
|
|
|
141
125
|
};
|
|
142
126
|
}
|
|
143
127
|
catch (err) {
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
success: false,
|
|
147
|
-
error: { code: err.code, message: err.message, recoverable: err.recoverable },
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
success: false,
|
|
152
|
-
error: {
|
|
153
|
-
code: 'REPLY_FAILED',
|
|
154
|
-
message: err instanceof Error ? err.message : String(err),
|
|
155
|
-
recoverable: false,
|
|
156
|
-
},
|
|
157
|
-
};
|
|
128
|
+
return handleProviderError(err, 'REPLY_FAILED');
|
|
158
129
|
}
|
|
159
130
|
},
|
|
160
131
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reply.js","sourceRoot":"","sources":["../../src/actions/reply.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"reply.js","sourceRoot":"","sources":["../../src/actions/reply.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MA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,EAAE,cAAc,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF,CAAC"}
|
package/dist/actions/send.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ declare const SendEmailInput: z.ZodObject<{
|
|
|
8
8
|
body_file: z.ZodOptional<z.ZodString>;
|
|
9
9
|
mailbox: z.ZodOptional<z.ZodString>;
|
|
10
10
|
draft: z.ZodOptional<z.ZodBoolean>;
|
|
11
|
+
format: z.ZodOptional<z.ZodEnum<{
|
|
12
|
+
text: "text";
|
|
13
|
+
html: "html";
|
|
14
|
+
markdown: "markdown";
|
|
15
|
+
}>>;
|
|
16
|
+
force_black: z.ZodOptional<z.ZodBoolean>;
|
|
11
17
|
}, z.core.$strip>;
|
|
12
18
|
declare const SendEmailOutput: z.ZodObject<{
|
|
13
19
|
success: z.ZodBoolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/actions/send.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../../src/actions/send.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAcjD,QAAA,MAAM,cAAc;;;;;;;;;;;;;;iBAYlB,CAAC;AAEH,QAAA,MAAM,eAAe;;;;;;;;;iBASnB,CAAC;AAEH,eAAO,MAAM,eAAe,EAAE,WAAW,CACvC,CAAC,CAAC,KAAK,CAAC,OAAO,cAAc,CAAC,EAC9B,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAuHhC,CAAC"}
|
package/dist/actions/send.js
CHANGED
|
@@ -2,8 +2,10 @@
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { checkSendAllowlist } from '../security/send-allowlist.js';
|
|
4
4
|
import { checkReplyThreading } from '../security/reply-validation.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
5
|
+
import { withRetry } from '../providers/provider.js';
|
|
6
|
+
import { truncateBody, BODY_SIZE_LIMIT } from '../content/body-loader.js';
|
|
7
|
+
import { renderEmailBody } from '../content/body-renderer.js';
|
|
8
|
+
import { checkMailboxRequired, resolveComposeFields, validateRequiredFields, checkRateLimit, handleProviderError, } from './compose-helpers.js';
|
|
7
9
|
const SendEmailInput = z.object({
|
|
8
10
|
to: z.string().or(z.array(z.string())).optional(),
|
|
9
11
|
cc: z.array(z.string()).optional(),
|
|
@@ -12,6 +14,10 @@ const SendEmailInput = z.object({
|
|
|
12
14
|
body_file: z.string().optional(),
|
|
13
15
|
mailbox: z.string().optional(),
|
|
14
16
|
draft: z.boolean().optional(),
|
|
17
|
+
format: z.enum(['markdown', 'html', 'text']).optional()
|
|
18
|
+
.describe("Body format. 'markdown' (default) renders via GFM with line-break preservation; 'html' is passthrough; 'text' sends as plain text."),
|
|
19
|
+
force_black: z.boolean().optional()
|
|
20
|
+
.describe('Wrap rendered HTML in a force-black div so Outlook dark mode does not hide the text. Default true. Ignored when format is "text".'),
|
|
15
21
|
});
|
|
16
22
|
const SendEmailOutput = z.object({
|
|
17
23
|
success: z.boolean(),
|
|
@@ -25,110 +31,57 @@ const SendEmailOutput = z.object({
|
|
|
25
31
|
});
|
|
26
32
|
export const sendEmailAction = {
|
|
27
33
|
name: 'send_email',
|
|
28
|
-
description: 'Compose and send a new email. Gated by send allowlist.',
|
|
34
|
+
description: 'Compose and send a new email. Gated by send allowlist. Draft mode bypasses allowlist.',
|
|
29
35
|
input: SendEmailInput,
|
|
30
36
|
output: SendEmailOutput,
|
|
31
37
|
annotations: { readOnlyHint: false, destructiveHint: false },
|
|
32
38
|
run: async (ctx, input) => {
|
|
33
39
|
// Check mailbox requirement for multi-mailbox
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
error: {
|
|
38
|
-
code: 'MAILBOX_REQUIRED',
|
|
39
|
-
message: 'mailbox parameter required when multiple mailboxes are configured',
|
|
40
|
-
recoverable: false,
|
|
41
|
-
},
|
|
42
|
-
};
|
|
40
|
+
const mailboxError = checkMailboxRequired(input.mailbox, ctx.allMailboxes);
|
|
41
|
+
if (mailboxError) {
|
|
42
|
+
return { success: false, error: mailboxError };
|
|
43
43
|
}
|
|
44
44
|
// Resolve body content and frontmatter
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
let subject = input.subject;
|
|
49
|
-
let draft = input.draft;
|
|
50
|
-
if (input.body_file) {
|
|
51
|
-
const bodyResult = await resolveBodyFile(input.body_file, ctx.safeDir);
|
|
52
|
-
if (bodyResult.error) {
|
|
53
|
-
return { success: false, error: bodyResult.error };
|
|
54
|
-
}
|
|
55
|
-
body = bodyResult.content;
|
|
56
|
-
// Frontmatter is authoritative — overrides action params for fields it declares
|
|
57
|
-
if (bodyResult.frontmatter) {
|
|
58
|
-
const fm = bodyResult.frontmatter;
|
|
59
|
-
if (fm.to !== undefined)
|
|
60
|
-
to = fm.to;
|
|
61
|
-
if (fm.cc !== undefined) {
|
|
62
|
-
cc = Array.isArray(fm.cc) ? fm.cc : [fm.cc];
|
|
63
|
-
}
|
|
64
|
-
if (fm.subject !== undefined)
|
|
65
|
-
subject = fm.subject;
|
|
66
|
-
if (fm.draft !== undefined)
|
|
67
|
-
draft = fm.draft;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
else if (input.body) {
|
|
71
|
-
body = input.body;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
return {
|
|
75
|
-
success: false,
|
|
76
|
-
error: { code: 'MISSING_BODY', message: 'Either body or body_file is required', recoverable: false },
|
|
77
|
-
};
|
|
45
|
+
const fields = await resolveComposeFields(input, ctx.safeDir);
|
|
46
|
+
if (fields.error) {
|
|
47
|
+
return { success: false, error: fields.error };
|
|
78
48
|
}
|
|
49
|
+
const { to, cc, subject, draft, format, forceBlack } = fields;
|
|
50
|
+
let { body } = fields;
|
|
79
51
|
// Validate required fields after merge
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
error: { code: 'MISSING_FIELD', message: 'to is required — provide it as a parameter or in body_file frontmatter', recoverable: false },
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
if (!subject) {
|
|
87
|
-
return {
|
|
88
|
-
success: false,
|
|
89
|
-
error: { code: 'MISSING_FIELD', message: 'subject is required — provide it as a parameter or in body_file frontmatter', recoverable: false },
|
|
90
|
-
};
|
|
52
|
+
const requiredError = validateRequiredFields(to, subject);
|
|
53
|
+
if (requiredError) {
|
|
54
|
+
return { success: false, error: requiredError };
|
|
91
55
|
}
|
|
92
56
|
// Resolve recipients
|
|
93
57
|
const recipients = Array.isArray(to) ? to : [to];
|
|
94
|
-
// Check send allowlist
|
|
95
|
-
const allowlistError = checkSendAllowlist(recipients, ctx.sendAllowlist);
|
|
96
|
-
if (allowlistError) {
|
|
97
|
-
return {
|
|
98
|
-
success: false,
|
|
99
|
-
error: { code: 'ALLOWLIST_BLOCKED', message: allowlistError, recoverable: false },
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
58
|
// Re: threading guardrail
|
|
103
59
|
const threadingError = checkReplyThreading(subject);
|
|
104
60
|
if (threadingError) {
|
|
105
61
|
return { success: false, error: threadingError };
|
|
106
62
|
}
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
recoverable: true,
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
}
|
|
63
|
+
// Render body for transport: markdown → HTML by default.
|
|
64
|
+
// Provider branches on bodyHtml to pick HTML vs Text content-type; the
|
|
65
|
+
// raw source stays in `body` as a plain-text fallback.
|
|
66
|
+
const rendered = renderEmailBody(body, { format, forceBlack });
|
|
67
|
+
let outBody = rendered.body;
|
|
68
|
+
let outBodyHtml = rendered.bodyHtml;
|
|
69
|
+
// Graceful truncation — each field independently capped at size limit.
|
|
70
|
+
if (Buffer.byteLength(outBody, 'utf-8') > BODY_SIZE_LIMIT) {
|
|
71
|
+
outBody = truncateBody(outBody);
|
|
120
72
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
body = truncateBody(body);
|
|
73
|
+
if (outBodyHtml !== undefined && Buffer.byteLength(outBodyHtml, 'utf-8') > BODY_SIZE_LIMIT) {
|
|
74
|
+
outBodyHtml = truncateBody(outBodyHtml);
|
|
124
75
|
}
|
|
125
|
-
|
|
76
|
+
body = outBody;
|
|
77
|
+
// Draft workflow — skip allowlist check and rate limit
|
|
126
78
|
if (draft) {
|
|
127
79
|
const draftResult = await ctx.provider.createDraft({
|
|
128
80
|
to: recipients.map(email => ({ email })),
|
|
129
81
|
cc: cc?.map(email => ({ email })),
|
|
130
|
-
subject,
|
|
82
|
+
subject: subject,
|
|
131
83
|
body,
|
|
84
|
+
bodyHtml: outBodyHtml,
|
|
132
85
|
});
|
|
133
86
|
return {
|
|
134
87
|
success: draftResult.success,
|
|
@@ -140,13 +93,27 @@ export const sendEmailAction = {
|
|
|
140
93
|
} : undefined,
|
|
141
94
|
};
|
|
142
95
|
}
|
|
96
|
+
// Send path — check allowlist
|
|
97
|
+
const allowlistError = checkSendAllowlist(recipients, ctx.sendAllowlist);
|
|
98
|
+
if (allowlistError) {
|
|
99
|
+
return {
|
|
100
|
+
success: false,
|
|
101
|
+
error: { code: 'ALLOWLIST_BLOCKED', message: allowlistError, recoverable: false },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
// Check rate limit
|
|
105
|
+
const rateLimitError = checkRateLimit(ctx.rateLimiter, 'send_email');
|
|
106
|
+
if (rateLimitError) {
|
|
107
|
+
return rateLimitError;
|
|
108
|
+
}
|
|
143
109
|
// Send with retry on transient errors
|
|
144
110
|
try {
|
|
145
111
|
const result = await withRetry(() => ctx.provider.sendMessage({
|
|
146
112
|
to: recipients.map(email => ({ email })),
|
|
147
113
|
cc: cc?.map(email => ({ email })),
|
|
148
|
-
subject,
|
|
114
|
+
subject: subject,
|
|
149
115
|
body,
|
|
116
|
+
bodyHtml: outBodyHtml,
|
|
150
117
|
}), { maxRetries: 3, baseDelay: 1000 });
|
|
151
118
|
if (ctx.rateLimiter) {
|
|
152
119
|
ctx.rateLimiter.recordUsage('send_email');
|
|
@@ -162,24 +129,7 @@ export const sendEmailAction = {
|
|
|
162
129
|
};
|
|
163
130
|
}
|
|
164
131
|
catch (err) {
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
success: false,
|
|
168
|
-
error: {
|
|
169
|
-
code: err.code,
|
|
170
|
-
message: err.message,
|
|
171
|
-
recoverable: err.recoverable,
|
|
172
|
-
},
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
return {
|
|
176
|
-
success: false,
|
|
177
|
-
error: {
|
|
178
|
-
code: 'SEND_FAILED',
|
|
179
|
-
message: err instanceof Error ? err.message : String(err),
|
|
180
|
-
recoverable: false,
|
|
181
|
-
},
|
|
182
|
-
};
|
|
132
|
+
return handleProviderError(err, 'SEND_FAILED');
|
|
183
133
|
}
|
|
184
134
|
},
|
|
185
135
|
};
|
package/dist/actions/send.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../src/actions/send.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACtE,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../src/actions/send.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,OAAO,EAAE,CAAC,EAAE,MAAM,K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AM,SAAS,CAC5B,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC;gBAC7B,EAAE,EAAE,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBACxC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBACjC,OAAO,EAAE,OAAQ;gBACjB,IAAI;gBACJ,QAAQ,EAAE,WAAW;aACtB,CAAC,EACF,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CACnC,CAAC;YAEF,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC;gBACpB,GAAG,CAAC,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC;YAC5C,CAAC;YAED,OAAO;gBACL,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;oBACpB,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI;oBACvB,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO;oBAC7B,WAAW,EAAE,MAAM,CAAC,KAAK,CAAC,WAAW;iBACtC,CAAC,CAAC,CAAC,SAAS;aACd,CAAC;QACJ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,mBAAmB,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;CACF,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type BodyFormat = 'markdown' | 'html' | 'text';
|
|
2
|
+
export declare const BODY_FORMATS: readonly BodyFormat[];
|
|
3
|
+
export interface RenderOptions {
|
|
4
|
+
/** Default 'markdown'. */
|
|
5
|
+
format?: BodyFormat;
|
|
6
|
+
/**
|
|
7
|
+
* Wrap rendered HTML in `<div style="color:#000000">` so Outlook dark mode
|
|
8
|
+
* doesn't invert body text to unreadable white-on-white. Default true.
|
|
9
|
+
* Ignored when format is 'text'.
|
|
10
|
+
*/
|
|
11
|
+
forceBlack?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface RenderedBody {
|
|
14
|
+
/**
|
|
15
|
+
* Plain-text / source content. Always populated — holds the raw input as
|
|
16
|
+
* a plain-text fallback for clients that can't render HTML.
|
|
17
|
+
*/
|
|
18
|
+
body: string;
|
|
19
|
+
/**
|
|
20
|
+
* Rendered HTML. Populated for `markdown` and `html` formats; undefined
|
|
21
|
+
* for `text`. Providers that see `bodyHtml` send with HTML content-type;
|
|
22
|
+
* otherwise they fall back to `body` as plain text.
|
|
23
|
+
*/
|
|
24
|
+
bodyHtml?: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Render an email body for transport.
|
|
28
|
+
*
|
|
29
|
+
* - `markdown` (default): renders via `marked` with `breaks: true` (single `\n`
|
|
30
|
+
* becomes `<br>`) and `gfm: true` (tables, strikethrough, fenced code).
|
|
31
|
+
* Raw HTML inside markdown is preserved, so callers that already hand us
|
|
32
|
+
* HTML don't regress. The raw markdown is kept in `body` as a plain-text
|
|
33
|
+
* fallback.
|
|
34
|
+
* - `html`: passthrough; caller already rendered.
|
|
35
|
+
* - `text`: no rendering; `bodyHtml` is undefined so providers send as plain.
|
|
36
|
+
*
|
|
37
|
+
* When HTML is produced, it is wrapped in a force-black div by default so
|
|
38
|
+
* Outlook's dark mode doesn't make the text invisible.
|
|
39
|
+
*/
|
|
40
|
+
export declare function renderEmailBody(raw: string, opts?: RenderOptions): RenderedBody;
|
|
41
|
+
//# sourceMappingURL=body-renderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-renderer.d.ts","sourceRoot":"","sources":["../../src/content/body-renderer.ts"],"names":[],"mappings":"AASA,MAAM,MAAM,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAC;AAEtD,eAAO,MAAM,YAAY,EAAE,SAAS,UAAU,EAAiC,CAAC;AAEhF,MAAM,WAAW,aAAa;IAC5B,0BAA0B;IAC1B,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC3B;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,aAAkB,GAAG,YAAY,CAoBnF"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Markdown → HTML rendering for outgoing email bodies.
|
|
2
|
+
// Centralizes the "agents write markdown, providers ship HTML" contract so
|
|
3
|
+
// actions don't have to think about format and providers stay dumb.
|
|
4
|
+
//
|
|
5
|
+
// Ref: `~/Projects/foam-notes/scripts/save_draft_to_outlook.py` uses the same
|
|
6
|
+
// approach (python-markdown with `nl2br`+`extra`, force-black wrapper).
|
|
7
|
+
import { marked } from 'marked';
|
|
8
|
+
export const BODY_FORMATS = ['markdown', 'html', 'text'];
|
|
9
|
+
/**
|
|
10
|
+
* Render an email body for transport.
|
|
11
|
+
*
|
|
12
|
+
* - `markdown` (default): renders via `marked` with `breaks: true` (single `\n`
|
|
13
|
+
* becomes `<br>`) and `gfm: true` (tables, strikethrough, fenced code).
|
|
14
|
+
* Raw HTML inside markdown is preserved, so callers that already hand us
|
|
15
|
+
* HTML don't regress. The raw markdown is kept in `body` as a plain-text
|
|
16
|
+
* fallback.
|
|
17
|
+
* - `html`: passthrough; caller already rendered.
|
|
18
|
+
* - `text`: no rendering; `bodyHtml` is undefined so providers send as plain.
|
|
19
|
+
*
|
|
20
|
+
* When HTML is produced, it is wrapped in a force-black div by default so
|
|
21
|
+
* Outlook's dark mode doesn't make the text invisible.
|
|
22
|
+
*/
|
|
23
|
+
export function renderEmailBody(raw, opts = {}) {
|
|
24
|
+
const format = opts.format ?? 'markdown';
|
|
25
|
+
if (format === 'text') {
|
|
26
|
+
return { body: raw };
|
|
27
|
+
}
|
|
28
|
+
let html;
|
|
29
|
+
if (format === 'html') {
|
|
30
|
+
html = raw;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// marked.parse is sync when async:false is passed; cast for TS.
|
|
34
|
+
html = marked.parse(raw, { breaks: true, gfm: true, async: false });
|
|
35
|
+
}
|
|
36
|
+
if (opts.forceBlack !== false) {
|
|
37
|
+
html = `<div style="color: #000000;">${html}</div>`;
|
|
38
|
+
}
|
|
39
|
+
return { body: raw, bodyHtml: html };
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=body-renderer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"body-renderer.js","sourceRoot":"","sources":["../../src/content/body-renderer.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,2EAA2E;AAC3E,oEAAoE;AACpE,EAAE;AACF,8EAA8E;AAC9E,wEAAwE;AAExE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,MAAM,CAAC,MAAM,YAAY,GAA0B,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AA2BhF;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,eAAe,CAAC,GAAW,EAAE,OAAsB,EAAE;IACnE,MAAM,MAAM,GAAe,IAAI,CAAC,MAAM,IAAI,UAAU,CAAC;IAErD,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;QACtB,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;IACvB,CAAC;IAED,IAAI,IAAY,CAAC;IACjB,IAAI,MAAM,KAAK,MAAM,EAAE,CAAC;QACtB,IAAI,GAAG,GAAG,CAAC;IACb,CAAC;SAAM,CAAC;QACN,gEAAgE;QAChE,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAW,CAAC;IAChF,CAAC;IAED,IAAI,IAAI,CAAC,UAAU,KAAK,KAAK,EAAE,CAAC;QAC9B,IAAI,GAAG,gCAAgC,IAAI,QAAQ,CAAC;IACtD,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import type { BodyFormat } from './body-renderer.js';
|
|
1
2
|
export interface FrontmatterFields {
|
|
2
3
|
to?: string | string[];
|
|
3
4
|
cc?: string | string[];
|
|
4
5
|
subject?: string;
|
|
5
6
|
reply_to?: string;
|
|
6
7
|
draft?: boolean;
|
|
8
|
+
format?: BodyFormat;
|
|
9
|
+
force_black?: boolean;
|
|
7
10
|
}
|
|
8
11
|
export declare function parseFrontmatter(content: string): {
|
|
9
12
|
frontmatter?: FrontmatterFields;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../../src/content/frontmatter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"frontmatter.d.ts","sourceRoot":"","sources":["../../src/content/frontmatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAGrD,MAAM,WAAW,iBAAiB;IAChC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACvB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAID,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,GACd;IAAE,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CA6DnD"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const KNOWN_KEYS = new Set(['to', 'cc', 'subject', 'reply_to', 'draft']);
|
|
1
|
+
import { BODY_FORMATS } from './body-renderer.js';
|
|
2
|
+
const KNOWN_KEYS = new Set(['to', 'cc', 'subject', 'reply_to', 'draft', 'format', 'force_black']);
|
|
4
3
|
export function parseFrontmatter(content) {
|
|
5
4
|
// Normalize CRLF to LF
|
|
6
5
|
const normalized = content.replace(/\r\n/g, '\n');
|
|
@@ -47,6 +46,16 @@ export function parseFrontmatter(content) {
|
|
|
47
46
|
else if (key === 'reply_to') {
|
|
48
47
|
fields.reply_to = value;
|
|
49
48
|
}
|
|
49
|
+
else if (key === 'format') {
|
|
50
|
+
const lowered = value.toLowerCase();
|
|
51
|
+
if (BODY_FORMATS.includes(lowered)) {
|
|
52
|
+
fields.format = lowered;
|
|
53
|
+
}
|
|
54
|
+
// unknown values silently ignored — action layer falls back to default
|
|
55
|
+
}
|
|
56
|
+
else if (key === 'force_black') {
|
|
57
|
+
fields.force_black = value.toLowerCase() === 'true';
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
60
|
const hasSomeField = Object.keys(fields).length > 0;
|
|
52
61
|
return { frontmatter: hasSomeField ? fields : undefined, body };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frontmatter.js","sourceRoot":"","sources":["../../src/content/frontmatter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"frontmatter.js","sourceRoot":"","sources":["../../src/content/frontmatter.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAYlD,MAAM,UAAU,GAAG,IAAI,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