@sanity/agent-directives 0.0.14 → 0.0.16
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/_chunks-dts/index.d.ts +43 -1
- package/dist/_chunks-dts/index.d.ts.map +1 -1
- package/dist/_chunks-es/formatters.js +16 -2
- package/dist/_chunks-es/formatters.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -1
- package/dist/lib/formatters.d.ts +2 -2
- package/dist/lib/formatters.js +2 -1
- package/dist/lib/react.d.ts +1 -1
- package/dist/lib/react.d.ts.map +1 -1
- package/dist/lib/react.js +21 -0
- package/dist/lib/react.js.map +1 -1
- package/dist/lib/server.d.ts +1 -1
- package/package.json +3 -2
|
@@ -53,6 +53,25 @@ declare const ChangesDirectiveSchema: z.ZodObject<{
|
|
|
53
53
|
createdCount: number;
|
|
54
54
|
updatedCount: number;
|
|
55
55
|
}>;
|
|
56
|
+
declare const ConfirmDirectiveSchema: z.ZodObject<{
|
|
57
|
+
status: z.ZodEnum<["pending", "approved", "cancelled"]>;
|
|
58
|
+
toolCallId: z.ZodString;
|
|
59
|
+
runId: z.ZodString;
|
|
60
|
+
toolName: z.ZodString;
|
|
61
|
+
data: z.ZodString;
|
|
62
|
+
}, "strip", z.ZodTypeAny, {
|
|
63
|
+
status: "pending" | "approved" | "cancelled";
|
|
64
|
+
toolCallId: string;
|
|
65
|
+
runId: string;
|
|
66
|
+
toolName: string;
|
|
67
|
+
data: string;
|
|
68
|
+
}, {
|
|
69
|
+
status: "pending" | "approved" | "cancelled";
|
|
70
|
+
toolCallId: string;
|
|
71
|
+
runId: string;
|
|
72
|
+
toolName: string;
|
|
73
|
+
data: string;
|
|
74
|
+
}>;
|
|
56
75
|
declare const DirectiveSchemas: {
|
|
57
76
|
readonly document: z.ZodObject<{
|
|
58
77
|
id: z.ZodString;
|
|
@@ -108,6 +127,25 @@ declare const DirectiveSchemas: {
|
|
|
108
127
|
}, {
|
|
109
128
|
source: string;
|
|
110
129
|
}>;
|
|
130
|
+
readonly confirm: z.ZodObject<{
|
|
131
|
+
status: z.ZodEnum<["pending", "approved", "cancelled"]>;
|
|
132
|
+
toolCallId: z.ZodString;
|
|
133
|
+
runId: z.ZodString;
|
|
134
|
+
toolName: z.ZodString;
|
|
135
|
+
data: z.ZodString;
|
|
136
|
+
}, "strip", z.ZodTypeAny, {
|
|
137
|
+
status: "pending" | "approved" | "cancelled";
|
|
138
|
+
toolCallId: string;
|
|
139
|
+
runId: string;
|
|
140
|
+
toolName: string;
|
|
141
|
+
data: string;
|
|
142
|
+
}, {
|
|
143
|
+
status: "pending" | "approved" | "cancelled";
|
|
144
|
+
toolCallId: string;
|
|
145
|
+
runId: string;
|
|
146
|
+
toolName: string;
|
|
147
|
+
data: string;
|
|
148
|
+
}>;
|
|
111
149
|
};
|
|
112
150
|
declare const DIRECTIVE_NAMES: {
|
|
113
151
|
readonly document: "document";
|
|
@@ -116,6 +154,7 @@ declare const DIRECTIVE_NAMES: {
|
|
|
116
154
|
readonly set: "set";
|
|
117
155
|
readonly changes: "changes";
|
|
118
156
|
readonly requestProjectAccess: "requestProjectAccess";
|
|
157
|
+
readonly confirm: "confirm";
|
|
119
158
|
};
|
|
120
159
|
type DirectiveName = (typeof DIRECTIVE_NAMES)[keyof typeof DIRECTIVE_NAMES];
|
|
121
160
|
type DocumentDirectiveProps = z.infer<typeof DocumentDirectiveSchema>;
|
|
@@ -124,6 +163,7 @@ type ResourceDirectiveProps = z.infer<typeof ResourceDirectiveSchema>;
|
|
|
124
163
|
type SetDirectiveProps = z.infer<typeof SetDirectiveSchema>;
|
|
125
164
|
type ChangesDirectiveProps = z.infer<typeof ChangesDirectiveSchema>;
|
|
126
165
|
type RequestProjectAccessDirectiveProps = z.infer<typeof RequestProjectAccessDirectiveSchema>;
|
|
166
|
+
type ConfirmDirectiveProps = z.infer<typeof ConfirmDirectiveSchema>;
|
|
127
167
|
interface DirectivePropsMap {
|
|
128
168
|
document: DocumentDirectiveProps;
|
|
129
169
|
release: ReleaseDirectiveProps;
|
|
@@ -131,6 +171,7 @@ interface DirectivePropsMap {
|
|
|
131
171
|
set: SetDirectiveProps;
|
|
132
172
|
changes: ChangesDirectiveProps;
|
|
133
173
|
requestProjectAccess: RequestProjectAccessDirectiveProps;
|
|
174
|
+
confirm: ConfirmDirectiveProps;
|
|
134
175
|
}
|
|
135
176
|
interface DefineDirectiveOptions {
|
|
136
177
|
/**
|
|
@@ -148,5 +189,6 @@ declare function createResourceDirective(props: ResourceDirectiveProps): string;
|
|
|
148
189
|
declare function createSetDirective(props: SetDirectiveProps): string;
|
|
149
190
|
declare function createChangesDirective(props: ChangesDirectiveProps): string;
|
|
150
191
|
declare function createRequestProjectAccessDirective(props: RequestProjectAccessDirectiveProps): string;
|
|
151
|
-
|
|
192
|
+
declare function createConfirmDirective(props: ConfirmDirectiveProps, label?: string): string;
|
|
193
|
+
export { createRequestProjectAccessDirective as C, formatDirective as E, createReleaseDirective as S, createSetDirective as T, SetDirectiveProps as _, DIRECTIVE_NAMES as a, createConfirmDirective as b, DirectivePropsMap as c, DocumentDirectiveSchema as d, ReleaseDirectiveProps as f, ResourceDirectiveSchema as g, ResourceDirectiveProps as h, ConfirmDirectiveSchema as i, DirectiveSchemas as l, RequestProjectAccessDirectiveProps as m, ChangesDirectiveSchema as n, DefineDirectiveOptions as o, ReleaseDirectiveSchema as p, ConfirmDirectiveProps as r, DirectiveName as s, ChangesDirectiveProps as t, DocumentDirectiveProps as u, SetDirectiveSchema as v, createResourceDirective as w, createDocumentDirective as x, createChangesDirective as y };
|
|
152
194
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/_internal/index.ts"],"mappings":";cAKa,uBAAA,EAAuB,CAAA,CAAA,SAAA;;;;;;;;;;;;;cAMvB,sBAAA,EAAsB,CAAA,CAAA,SAAA;;;;;;;;;;cAKtB,uBAAA,EA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BAAA;EACV,OAAA,EAAS,qBAAA;EACT,QAAA,EAAU,sBAAA;EACV,GAAA,EAAK,iBAAA;EACL,OAAA,EAAS,qBAAA;EACT,oBAAA,EAAsB,kCAAA;EACtB,OAAA,EAAS,qBAAA;AAAA;AAAA,UAGM,sBAAA;;;;;EAKf,eAAA;;EAEA,mBAAA;AAAA;AAAA,iBA8Bc,eAAA,CACd,IAAA,EAAM,aAAA,EACN,KAAA,EAAO,MAAA,mBACP,KAAA;AAAA,iBASc,uBAAA,CAAwB,KAAA,EAAO,sBAAA,EAAwB,KAAA;AAAA,iBAIvD,sBAAA,CAAuB,KAAA,EAAO,qBAAA,EAAuB,KAAA;AAAA,iBAIrD,uBAAA,CAAwB,KAAA,EAAO,sBAAA;AAAA,iBAI/B,kBAAA,CAAmB,KAAA,EAAO,iBAAA;AAAA,iBAI1B,sBAAA,CAAuB,KAAA,EAAO,qBAAA;AAAA,iBAI9B,mCAAA,CACd,KAAA,EAAO,kCAAA;AAAA,iBAKO,sBAAA,CAAuB,KAAA,EAAO,qBAAA,EAAuB,KAAA"}
|
|
@@ -15,20 +15,29 @@ const DocumentDirectiveSchema = z.object({
|
|
|
15
15
|
}), ChangesDirectiveSchema = z.object({
|
|
16
16
|
createdCount: z.coerce.number().min(0),
|
|
17
17
|
updatedCount: z.coerce.number().min(0)
|
|
18
|
+
}), ConfirmDirectiveSchema = z.object({
|
|
19
|
+
status: z.enum(["pending", "approved", "cancelled"]),
|
|
20
|
+
toolCallId: z.string(),
|
|
21
|
+
runId: z.string(),
|
|
22
|
+
toolName: z.string(),
|
|
23
|
+
data: z.string()
|
|
24
|
+
// base64-encoded JSON, typed per toolName
|
|
18
25
|
}), DirectiveSchemas = {
|
|
19
26
|
document: DocumentDirectiveSchema,
|
|
20
27
|
release: ReleaseDirectiveSchema,
|
|
21
28
|
resource: ResourceDirectiveSchema,
|
|
22
29
|
set: SetDirectiveSchema,
|
|
23
30
|
changes: ChangesDirectiveSchema,
|
|
24
|
-
requestProjectAccess: RequestProjectAccessDirectiveSchema
|
|
31
|
+
requestProjectAccess: RequestProjectAccessDirectiveSchema,
|
|
32
|
+
confirm: ConfirmDirectiveSchema
|
|
25
33
|
}, DIRECTIVE_NAMES = {
|
|
26
34
|
document: "document",
|
|
27
35
|
release: "release",
|
|
28
36
|
resource: "resource",
|
|
29
37
|
set: "set",
|
|
30
38
|
changes: "changes",
|
|
31
|
-
requestProjectAccess: "requestProjectAccess"
|
|
39
|
+
requestProjectAccess: "requestProjectAccess",
|
|
40
|
+
confirm: "confirm"
|
|
32
41
|
}, DIRECTIVE_TYPES = ["textDirective", "leafDirective", "containerDirective"], LABEL = "(?:[^\\[\\]]|\\[[^\\[\\]]*\\])*", BLOCK_DIRECTIVE = new RegExp(`::([a-zA-Z]\\w*)(?:\\[(${LABEL})\\])?\\{([^}]*)\\}`), INLINE_DIRECTIVE = new RegExp(`:([a-zA-Z]\\w*)\\[(${LABEL})\\]\\{([^}]*)\\}`);
|
|
33
42
|
function parseDirectiveAttributes(attrString) {
|
|
34
43
|
const attrs = {};
|
|
@@ -61,9 +70,13 @@ function createChangesDirective(props) {
|
|
|
61
70
|
function createRequestProjectAccessDirective(props) {
|
|
62
71
|
return formatDirective(DIRECTIVE_NAMES.requestProjectAccess, props);
|
|
63
72
|
}
|
|
73
|
+
function createConfirmDirective(props, label) {
|
|
74
|
+
return formatDirective(DIRECTIVE_NAMES.confirm, props, label);
|
|
75
|
+
}
|
|
64
76
|
export {
|
|
65
77
|
BLOCK_DIRECTIVE,
|
|
66
78
|
ChangesDirectiveSchema,
|
|
79
|
+
ConfirmDirectiveSchema,
|
|
67
80
|
DIRECTIVE_NAMES,
|
|
68
81
|
DIRECTIVE_TYPES,
|
|
69
82
|
DirectiveSchemas,
|
|
@@ -73,6 +86,7 @@ export {
|
|
|
73
86
|
ResourceDirectiveSchema,
|
|
74
87
|
SetDirectiveSchema,
|
|
75
88
|
createChangesDirective,
|
|
89
|
+
createConfirmDirective,
|
|
76
90
|
createDocumentDirective,
|
|
77
91
|
createReleaseDirective,
|
|
78
92
|
createRequestProjectAccessDirective,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatters.js","sources":["../../src/_internal/index.ts"],"sourcesContent":["// This internal module contains all shared code that both the main index\n// and sub-exports can import without causing self-referencing issues with pkg-utils\n\nimport { z } from 'zod'\n\nexport const DocumentDirectiveSchema = z.object({\n id: z.string(),\n type: z.string().optional(),\n source: z.string().optional(),\n})\n\nexport const ReleaseDirectiveSchema = z.object({\n id: z.string(),\n source: z.string().optional(),\n})\n\nexport const ResourceDirectiveSchema = z.object({\n source: z.string(),\n})\n\nexport const RequestProjectAccessDirectiveSchema = z.object({\n source: z.string(),\n})\n\nexport const SetDirectiveSchema = z.object({\n id: z.string(),\n})\n\nexport const ChangesDirectiveSchema = z.object({\n createdCount: z.coerce.number().min(0),\n updatedCount: z.coerce.number().min(0),\n})\n\nexport const DirectiveSchemas = {\n document: DocumentDirectiveSchema,\n release: ReleaseDirectiveSchema,\n resource: ResourceDirectiveSchema,\n set: SetDirectiveSchema,\n changes: ChangesDirectiveSchema,\n requestProjectAccess: RequestProjectAccessDirectiveSchema,\n} as const\n\nexport const DIRECTIVE_NAMES = {\n document: 'document',\n release: 'release',\n resource: 'resource',\n set: 'set',\n changes: 'changes',\n requestProjectAccess: 'requestProjectAccess',\n} as const\n\nexport const DIRECTIVE_TYPES = ['textDirective', 'leafDirective', 'containerDirective'] as const\n\nexport type DirectiveType = (typeof DIRECTIVE_TYPES)[number]\n\nexport type DirectiveName = (typeof DIRECTIVE_NAMES)[keyof typeof DIRECTIVE_NAMES]\n\nexport type DocumentDirectiveProps = z.infer<typeof DocumentDirectiveSchema>\nexport type ReleaseDirectiveProps = z.infer<typeof ReleaseDirectiveSchema>\nexport type ResourceDirectiveProps = z.infer<typeof ResourceDirectiveSchema>\nexport type SetDirectiveProps = z.infer<typeof SetDirectiveSchema>\nexport type ChangesDirectiveProps = z.infer<typeof ChangesDirectiveSchema>\nexport type RequestProjectAccessDirectiveProps = z.infer<typeof RequestProjectAccessDirectiveSchema>\n\nexport interface DirectivePropsMap {\n document: DocumentDirectiveProps\n release: ReleaseDirectiveProps\n resource: ResourceDirectiveProps\n set: SetDirectiveProps\n changes: ChangesDirectiveProps\n requestProjectAccess: RequestProjectAccessDirectiveProps\n}\n\nexport interface DefineDirectiveOptions {\n /**\n * Whether this directive requires context/application to render.\n * If true (default), the directive won't render without context.\n */\n requiresContext?: boolean\n /** @deprecated Use `requiresContext` instead */\n requiresApplication?: boolean\n}\n\n// Label content pattern: matches non-bracket chars or single-level nested [...]\n// e.g. \"Guide [E-commerce edition]\" matches, but won't span across \"] other text [\"\nconst LABEL = '(?:[^\\\\[\\\\]]|\\\\[[^\\\\[\\\\]]*\\\\])*'\n\n// Matches block directives: ::name{attrs} or ::name[label]{attrs}\n// Captures: (1) name, (2) label or undefined, (3) attribute string\nexport const BLOCK_DIRECTIVE = new RegExp(`::([a-zA-Z]\\\\w*)(?:\\\\[(${LABEL})\\\\])?\\\\{([^}]*)\\\\}`)\n\n// Matches inline directives: :name[label]{attrs}\n// Captures: (1) name, (2) label, (3) attribute string\nexport const INLINE_DIRECTIVE = new RegExp(`:([a-zA-Z]\\\\w*)\\\\[(${LABEL})\\\\]\\\\{([^}]*)\\\\}`)\n\nexport function parseDirectiveAttributes(attrString: string): Record<string, string> {\n const attrs: Record<string, string> = {}\n for (const match of attrString.matchAll(/(\\w+)=\"([^\"]*)\"/g)) {\n attrs[match[1]] = match[2]\n }\n return attrs\n}\n\nfunction buildAttributes(params: Record<string, unknown>): string {\n return Object.entries(params)\n .filter(([, value]) => value !== undefined && value !== null)\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')\n}\n\nexport function formatDirective(\n name: DirectiveName,\n props: Record<string, unknown>,\n label?: string,\n): string {\n const attrs = buildAttributes(props)\n if (label) {\n return attrs ? `:${name}[${label}]{${attrs}}` : `:${name}[${label}]`\n }\n return attrs ? `::${name}{${attrs}}` : `::${name}`\n}\n\nexport function createDocumentDirective(props: DocumentDirectiveProps, label?: string): string {\n return formatDirective(DIRECTIVE_NAMES.document, props, label)\n}\n\nexport function createReleaseDirective(props: ReleaseDirectiveProps, label?: string): string {\n return formatDirective(DIRECTIVE_NAMES.release, props, label)\n}\n\nexport function createResourceDirective(props: ResourceDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.resource, props)\n}\n\nexport function createSetDirective(props: SetDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.set, props)\n}\n\nexport function createChangesDirective(props: ChangesDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.changes, props)\n}\n\nexport function createRequestProjectAccessDirective(\n props: RequestProjectAccessDirectiveProps,\n): string {\n return formatDirective(DIRECTIVE_NAMES.requestProjectAccess, props)\n}\n"],"names":[],"mappings":";AAKO,MAAM,0BAA0B,EAAE,OAAO;AAAA,EAC9C,IAAI,EAAE,OAAA;AAAA,EACN,MAAM,EAAE,OAAA,EAAS,SAAA;AAAA,EACjB,QAAQ,EAAE,OAAA,EAAS,SAAA;AACrB,CAAC,GAEY,yBAAyB,EAAE,OAAO;AAAA,EAC7C,IAAI,EAAE,OAAA;AAAA,EACN,QAAQ,EAAE,OAAA,EAAS,SAAA;AACrB,CAAC,GAEY,0BAA0B,EAAE,OAAO;AAAA,EAC9C,QAAQ,EAAE,OAAA;AACZ,CAAC,GAEY,sCAAsC,EAAE,OAAO;AAAA,EAC1D,QAAQ,EAAE,OAAA;AACZ,CAAC,GAEY,qBAAqB,EAAE,OAAO;AAAA,EACzC,IAAI,EAAE,OAAA;AACR,CAAC,GAEY,yBAAyB,EAAE,OAAO;AAAA,EAC7C,cAAc,EAAE,OAAO,OAAA,EAAS,IAAI,CAAC;AAAA,EACrC,cAAc,EAAE,OAAO,OAAA,EAAS,IAAI,CAAC;AACvC,CAAC,GAEY,mBAAmB;AAAA,EAC9B,UAAU;AAAA,EACV,SAAS;AAAA,EACT,UAAU;AAAA,EACV,KAAK;AAAA,EACL,SAAS;AAAA,EACT,sBAAsB;
|
|
1
|
+
{"version":3,"file":"formatters.js","sources":["../../src/_internal/index.ts"],"sourcesContent":["// This internal module contains all shared code that both the main index\n// and sub-exports can import without causing self-referencing issues with pkg-utils\n\nimport { z } from 'zod'\n\nexport const DocumentDirectiveSchema = z.object({\n id: z.string(),\n type: z.string().optional(),\n source: z.string().optional(),\n})\n\nexport const ReleaseDirectiveSchema = z.object({\n id: z.string(),\n source: z.string().optional(),\n})\n\nexport const ResourceDirectiveSchema = z.object({\n source: z.string(),\n})\n\nexport const RequestProjectAccessDirectiveSchema = z.object({\n source: z.string(),\n})\n\nexport const SetDirectiveSchema = z.object({\n id: z.string(),\n})\n\nexport const ChangesDirectiveSchema = z.object({\n createdCount: z.coerce.number().min(0),\n updatedCount: z.coerce.number().min(0),\n})\n\nexport const ConfirmDirectiveSchema = z.object({\n status: z.enum(['pending', 'approved', 'cancelled']),\n toolCallId: z.string(),\n runId: z.string(),\n toolName: z.string(),\n data: z.string(), // base64-encoded JSON, typed per toolName\n})\n\nexport const DirectiveSchemas = {\n document: DocumentDirectiveSchema,\n release: ReleaseDirectiveSchema,\n resource: ResourceDirectiveSchema,\n set: SetDirectiveSchema,\n changes: ChangesDirectiveSchema,\n requestProjectAccess: RequestProjectAccessDirectiveSchema,\n confirm: ConfirmDirectiveSchema,\n} as const\n\nexport const DIRECTIVE_NAMES = {\n document: 'document',\n release: 'release',\n resource: 'resource',\n set: 'set',\n changes: 'changes',\n requestProjectAccess: 'requestProjectAccess',\n confirm: 'confirm',\n} as const\n\nexport const DIRECTIVE_TYPES = ['textDirective', 'leafDirective', 'containerDirective'] as const\n\nexport type DirectiveType = (typeof DIRECTIVE_TYPES)[number]\n\nexport type DirectiveName = (typeof DIRECTIVE_NAMES)[keyof typeof DIRECTIVE_NAMES]\n\nexport type DocumentDirectiveProps = z.infer<typeof DocumentDirectiveSchema>\nexport type ReleaseDirectiveProps = z.infer<typeof ReleaseDirectiveSchema>\nexport type ResourceDirectiveProps = z.infer<typeof ResourceDirectiveSchema>\nexport type SetDirectiveProps = z.infer<typeof SetDirectiveSchema>\nexport type ChangesDirectiveProps = z.infer<typeof ChangesDirectiveSchema>\nexport type RequestProjectAccessDirectiveProps = z.infer<typeof RequestProjectAccessDirectiveSchema>\nexport type ConfirmDirectiveProps = z.infer<typeof ConfirmDirectiveSchema>\n\nexport interface DirectivePropsMap {\n document: DocumentDirectiveProps\n release: ReleaseDirectiveProps\n resource: ResourceDirectiveProps\n set: SetDirectiveProps\n changes: ChangesDirectiveProps\n requestProjectAccess: RequestProjectAccessDirectiveProps\n confirm: ConfirmDirectiveProps\n}\n\nexport interface DefineDirectiveOptions {\n /**\n * Whether this directive requires context/application to render.\n * If true (default), the directive won't render without context.\n */\n requiresContext?: boolean\n /** @deprecated Use `requiresContext` instead */\n requiresApplication?: boolean\n}\n\n// Label content pattern: matches non-bracket chars or single-level nested [...]\n// e.g. \"Guide [E-commerce edition]\" matches, but won't span across \"] other text [\"\nconst LABEL = '(?:[^\\\\[\\\\]]|\\\\[[^\\\\[\\\\]]*\\\\])*'\n\n// Matches block directives: ::name{attrs} or ::name[label]{attrs}\n// Captures: (1) name, (2) label or undefined, (3) attribute string\nexport const BLOCK_DIRECTIVE = new RegExp(`::([a-zA-Z]\\\\w*)(?:\\\\[(${LABEL})\\\\])?\\\\{([^}]*)\\\\}`)\n\n// Matches inline directives: :name[label]{attrs}\n// Captures: (1) name, (2) label, (3) attribute string\nexport const INLINE_DIRECTIVE = new RegExp(`:([a-zA-Z]\\\\w*)\\\\[(${LABEL})\\\\]\\\\{([^}]*)\\\\}`)\n\nexport function parseDirectiveAttributes(attrString: string): Record<string, string> {\n const attrs: Record<string, string> = {}\n for (const match of attrString.matchAll(/(\\w+)=\"([^\"]*)\"/g)) {\n attrs[match[1]] = match[2]\n }\n return attrs\n}\n\nfunction buildAttributes(params: Record<string, unknown>): string {\n return Object.entries(params)\n .filter(([, value]) => value !== undefined && value !== null)\n .map(([key, value]) => `${key}=\"${value}\"`)\n .join(' ')\n}\n\nexport function formatDirective(\n name: DirectiveName,\n props: Record<string, unknown>,\n label?: string,\n): string {\n const attrs = buildAttributes(props)\n if (label) {\n return attrs ? `:${name}[${label}]{${attrs}}` : `:${name}[${label}]`\n }\n return attrs ? `::${name}{${attrs}}` : `::${name}`\n}\n\nexport function createDocumentDirective(props: DocumentDirectiveProps, label?: string): string {\n return formatDirective(DIRECTIVE_NAMES.document, props, label)\n}\n\nexport function createReleaseDirective(props: ReleaseDirectiveProps, label?: string): string {\n return formatDirective(DIRECTIVE_NAMES.release, props, label)\n}\n\nexport function createResourceDirective(props: ResourceDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.resource, props)\n}\n\nexport function createSetDirective(props: SetDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.set, props)\n}\n\nexport function createChangesDirective(props: ChangesDirectiveProps): string {\n return formatDirective(DIRECTIVE_NAMES.changes, props)\n}\n\nexport function createRequestProjectAccessDirective(\n props: RequestProjectAccessDirectiveProps,\n): string {\n return formatDirective(DIRECTIVE_NAMES.requestProjectAccess, props)\n}\n\nexport function createConfirmDirective(props: ConfirmDirectiveProps, label?: string): string {\n return formatDirective(DIRECTIVE_NAMES.confirm, props, label)\n}\n"],"names":[],"mappings":";AAKO,MAAM,0BAA0B,EAAE,OAAO;AAAA,EAC9C,IAAI,EAAE,OAAA;AAAA,EACN,MAAM,EAAE,OAAA,EAAS,SAAA;AAAA,EACjB,QAAQ,EAAE,OAAA,EAAS,SAAA;AACrB,CAAC,GAEY,yBAAyB,EAAE,OAAO;AAAA,EAC7C,IAAI,EAAE,OAAA;AAAA,EACN,QAAQ,EAAE,OAAA,EAAS,SAAA;AACrB,CAAC,GAEY,0BAA0B,EAAE,OAAO;AAAA,EAC9C,QAAQ,EAAE,OAAA;AACZ,CAAC,GAEY,sCAAsC,EAAE,OAAO;AAAA,EAC1D,QAAQ,EAAE,OAAA;AACZ,CAAC,GAEY,qBAAqB,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G;AACb;AAEO,SAAS,gBACd,MACA,OACA,OACQ;AACR,QAAM,QAAQ,gBAAgB,KAAK;AACnC,SAAI,QACK,QAAQ,IAAI,IAAI,IAAI,KAAK,KAAK,KAAK,MAAM,IAAI,IAAI,IAAI,KAAK,MAE5D,QAAQ,KAAK,IAAI,IAAI,KAAK,MAAM,KAAK,IAAI;AAClD;AAEO,SAAS,wBAAwB,OAA+B,OAAwB;AAC7F,SAAO,gBAAgB,gBAAgB,UAAU,OAAO,KAAK;AAC/D;AAEO,SAAS,uBAAuB,OAA8B,OAAwB;AAC3F,SAAO,gBAAgB,gBAAgB,SAAS,OAAO,KAAK;AAC9D;AAEO,SAAS,wBAAwB,OAAuC;AAC7E,SAAO,gBAAgB,gBAAgB,UAAU,KAAK;AACxD;AAEO,SAAS,mBAAmB,OAAkC;AACnE,SAAO,gBAAgB,gBAAgB,KAAK,KAAK;AACnD;AAEO,SAAS,uBAAuB,OAAsC;AAC3E,SAAO,gBAAgB,gBAAgB,SAAS,KAAK;AACvD;AAEO,SAAS,oCACd,OACQ;AACR,SAAO,gBAAgB,gBAAgB,sBAAsB,KAAK;AACpE;AAEO,SAAS,uBAAuB,OAA8B,OAAwB;AAC3F,SAAO,gBAAgB,gBAAgB,SAAS,OAAO,KAAK;AAC9D;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { E as formatDirective, S as createReleaseDirective, T as createSetDirective, _ as SetDirectiveProps, a as DIRECTIVE_NAMES, b as createConfirmDirective, c as DirectivePropsMap, d as DocumentDirectiveSchema, f as ReleaseDirectiveProps, g as ResourceDirectiveSchema, h as ResourceDirectiveProps, i as ConfirmDirectiveSchema, l as DirectiveSchemas, n as ChangesDirectiveSchema, o as DefineDirectiveOptions, p as ReleaseDirectiveSchema, r as ConfirmDirectiveProps, s as DirectiveName, t as ChangesDirectiveProps, u as DocumentDirectiveProps, v as SetDirectiveSchema, w as createResourceDirective, x as createDocumentDirective, y as createChangesDirective } from "./_chunks-dts/index.js";
|
|
2
2
|
/**
|
|
3
3
|
* Scans `text` for block directives (`::name{attrs}`) of the given type,
|
|
4
4
|
* validates each match through its Zod schema, and returns the parsed props.
|
|
5
5
|
*/
|
|
6
6
|
declare function findDirectives<N extends keyof DirectivePropsMap>(text: string, name: N): DirectivePropsMap[N][];
|
|
7
|
-
export { type ChangesDirectiveProps, ChangesDirectiveSchema, DIRECTIVE_NAMES, type DefineDirectiveOptions, type DirectiveName, type DirectivePropsMap, DirectiveSchemas, type DocumentDirectiveProps, DocumentDirectiveSchema, type ReleaseDirectiveProps, ReleaseDirectiveSchema, type ResourceDirectiveProps, ResourceDirectiveSchema, type SetDirectiveProps, SetDirectiveSchema, createChangesDirective, createDocumentDirective, createReleaseDirective, createResourceDirective, createSetDirective, findDirectives, formatDirective };
|
|
7
|
+
export { type ChangesDirectiveProps, ChangesDirectiveSchema, type ConfirmDirectiveProps, ConfirmDirectiveSchema, DIRECTIVE_NAMES, type DefineDirectiveOptions, type DirectiveName, type DirectivePropsMap, DirectiveSchemas, type DocumentDirectiveProps, DocumentDirectiveSchema, type ReleaseDirectiveProps, ReleaseDirectiveSchema, type ResourceDirectiveProps, ResourceDirectiveSchema, type SetDirectiveProps, SetDirectiveSchema, createChangesDirective, createConfirmDirective, createDocumentDirective, createReleaseDirective, createResourceDirective, createSetDirective, findDirectives, formatDirective };
|
|
8
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseDirectiveAttributes, DirectiveSchemas, BLOCK_DIRECTIVE } from "./_chunks-es/formatters.js";
|
|
2
|
-
import { ChangesDirectiveSchema, DIRECTIVE_NAMES, DocumentDirectiveSchema, ReleaseDirectiveSchema, ResourceDirectiveSchema, SetDirectiveSchema, createChangesDirective, createDocumentDirective, createReleaseDirective, createResourceDirective, createSetDirective, formatDirective } from "./_chunks-es/formatters.js";
|
|
2
|
+
import { ChangesDirectiveSchema, ConfirmDirectiveSchema, DIRECTIVE_NAMES, DocumentDirectiveSchema, ReleaseDirectiveSchema, ResourceDirectiveSchema, SetDirectiveSchema, createChangesDirective, createConfirmDirective, createDocumentDirective, createReleaseDirective, createResourceDirective, createSetDirective, formatDirective } from "./_chunks-es/formatters.js";
|
|
3
3
|
function findDirectives(text, name) {
|
|
4
4
|
const regex = new RegExp(BLOCK_DIRECTIVE.source, "g"), schema = DirectiveSchemas[name], results = [];
|
|
5
5
|
for (const match of text.matchAll(regex)) {
|
|
@@ -11,6 +11,7 @@ function findDirectives(text, name) {
|
|
|
11
11
|
}
|
|
12
12
|
export {
|
|
13
13
|
ChangesDirectiveSchema,
|
|
14
|
+
ConfirmDirectiveSchema,
|
|
14
15
|
DIRECTIVE_NAMES,
|
|
15
16
|
DirectiveSchemas,
|
|
16
17
|
DocumentDirectiveSchema,
|
|
@@ -18,6 +19,7 @@ export {
|
|
|
18
19
|
ResourceDirectiveSchema,
|
|
19
20
|
SetDirectiveSchema,
|
|
20
21
|
createChangesDirective,
|
|
22
|
+
createConfirmDirective,
|
|
21
23
|
createDocumentDirective,
|
|
22
24
|
createReleaseDirective,
|
|
23
25
|
createResourceDirective,
|
package/dist/lib/formatters.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { C as formatDirective, S as createSetDirective, _ as
|
|
2
|
-
export { type ChangesDirectiveProps, DIRECTIVE_NAMES, type DirectiveName, type DocumentDirectiveProps, type ReleaseDirectiveProps, type RequestProjectAccessDirectiveProps, type ResourceDirectiveProps, type SetDirectiveProps, createChangesDirective, createDocumentDirective, createReleaseDirective, createRequestProjectAccessDirective, createResourceDirective, createSetDirective, formatDirective };
|
|
1
|
+
import { C as createRequestProjectAccessDirective, E as formatDirective, S as createReleaseDirective, T as createSetDirective, _ as SetDirectiveProps, a as DIRECTIVE_NAMES, b as createConfirmDirective, f as ReleaseDirectiveProps, h as ResourceDirectiveProps, m as RequestProjectAccessDirectiveProps, r as ConfirmDirectiveProps, s as DirectiveName, t as ChangesDirectiveProps, u as DocumentDirectiveProps, w as createResourceDirective, x as createDocumentDirective, y as createChangesDirective } from "../_chunks-dts/index.js";
|
|
2
|
+
export { type ChangesDirectiveProps, type ConfirmDirectiveProps, DIRECTIVE_NAMES, type DirectiveName, type DocumentDirectiveProps, type ReleaseDirectiveProps, type RequestProjectAccessDirectiveProps, type ResourceDirectiveProps, type SetDirectiveProps, createChangesDirective, createConfirmDirective, createDocumentDirective, createReleaseDirective, createRequestProjectAccessDirective, createResourceDirective, createSetDirective, formatDirective };
|
package/dist/lib/formatters.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { DIRECTIVE_NAMES, createChangesDirective, createDocumentDirective, createReleaseDirective, createRequestProjectAccessDirective, createResourceDirective, createSetDirective, formatDirective } from "../_chunks-es/formatters.js";
|
|
1
|
+
import { DIRECTIVE_NAMES, createChangesDirective, createConfirmDirective, createDocumentDirective, createReleaseDirective, createRequestProjectAccessDirective, createResourceDirective, createSetDirective, formatDirective } from "../_chunks-es/formatters.js";
|
|
2
2
|
export {
|
|
3
3
|
DIRECTIVE_NAMES,
|
|
4
4
|
createChangesDirective,
|
|
5
|
+
createConfirmDirective,
|
|
5
6
|
createDocumentDirective,
|
|
6
7
|
createReleaseDirective,
|
|
7
8
|
createRequestProjectAccessDirective,
|
package/dist/lib/react.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { c as DirectivePropsMap, o as DefineDirectiveOptions, s as DirectiveName } from "../_chunks-dts/index.js";
|
|
2
2
|
import { ComponentType, ReactNode } from "react";
|
|
3
3
|
import { Processor } from "unified";
|
|
4
4
|
import { Node } from "unist";
|
package/dist/lib/react.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.d.ts","names":[],"sources":["../../src/lib/react.tsx"],"mappings":";;;;KAuBK,cAAA,WAAyB,aAAA,IAAiB,iBAAA,C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,uBAAA,CAAwB,KAAA;AAAA"}
|
package/dist/lib/react.js
CHANGED
|
@@ -42,6 +42,27 @@ function remarkDirectivesTransform() {
|
|
|
42
42
|
children: label ? [{ type: "text", name: "", value: label }] : []
|
|
43
43
|
}, parentNode = parent;
|
|
44
44
|
parentNode?.children && typeof index == "number" && (parentNode.children[index] = directiveNode);
|
|
45
|
+
}), visit(tree, "tableCell", (node) => {
|
|
46
|
+
const cellNode = node;
|
|
47
|
+
if (cellNode.children)
|
|
48
|
+
for (let i = 0; i < cellNode.children.length; i++) {
|
|
49
|
+
const child = cellNode.children[i];
|
|
50
|
+
if (typeof child.value != "string") continue;
|
|
51
|
+
const match = child.value.match(BLOCK_DIRECTIVE);
|
|
52
|
+
if (!match) continue;
|
|
53
|
+
const [, name, label, attrsString] = match;
|
|
54
|
+
KNOWN_DIRECTIVE_NAMES.has(name) && (cellNode.children[i] = {
|
|
55
|
+
type: "textDirective",
|
|
56
|
+
name,
|
|
57
|
+
attributes: parseDirectiveAttributes(attrsString),
|
|
58
|
+
children: label ? [{ type: "text", name: "", value: label }] : []
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}), visit(tree, "tableCell", (node) => {
|
|
62
|
+
const cellNode = node;
|
|
63
|
+
if (cellNode.children)
|
|
64
|
+
for (const child of cellNode.children)
|
|
65
|
+
child.type === "leafDirective" && (child.type = "textDirective");
|
|
45
66
|
}), visit(
|
|
46
67
|
tree,
|
|
47
68
|
"list",
|
package/dist/lib/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sources":["../../src/lib/react.tsx"],"sourcesContent":["import { pascalCase } from 'es-toolkit'\nimport { directiveFromMarkdown, directiveToMarkdown } from 'mdast-util-directive'\nimport { directive } from 'micromark-extension-directive'\nimport type { ComponentType, ReactNode } from 'react'\nimport type { Processor } from 'unified'\nimport type { Node } from 'unist'\nimport { visit } from 'unist-util-visit'\nimport {\n BLOCK_DIRECTIVE,\n type DefineDirectiveOptions,\n DIRECTIVE_NAMES,\n DIRECTIVE_TYPES,\n type DirectiveName,\n type DirectivePropsMap,\n DirectiveSchemas,\n type DirectiveType,\n parseDirectiveAttributes,\n} from '../_internal'\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T]\n\nexport interface NodeProps {\n node?: { properties?: Record<string, unknown> }\n}\n\nexport type DirectiveRenderProps<\n TName extends DirectiveName,\n TApplication = undefined,\n TRequiresApplication extends boolean = true,\n> = DirectiveProps<TName> & {\n isInline: boolean\n} & (TRequiresApplication extends true\n ? TApplication extends undefined\n ? {}\n : { application: TApplication }\n : {})\n\nexport type { DefineDirectiveOptions }\n\nexport type DirectiveComponentProps<TName extends DirectiveName> = DirectiveProps<TName> & {\n source?: string\n} & NodeProps\n\n// ============================================================================\n// Remark Plugin Types\n// ============================================================================\n\ninterface DirectiveNode extends Node {\n type: 'textDirective' | 'leafDirective' | 'containerDirective'\n name: string\n attributes?: Record<string, string>\n children?: DirectiveNode[]\n value?: string\n data?: {\n hName?: string\n hProperties?: Record<string, unknown>\n }\n}\n\nconst isDirectiveNode = (node: Node): node is DirectiveNode =>\n 'name' in node &&\n typeof (node as DirectiveNode).name === 'string' &&\n /^[a-zA-Z-]+$/.test((node as DirectiveNode).name) &&\n DIRECTIVE_TYPES.includes(node.type as DirectiveType)\n\nconst isListItemWithOnlyDirective = (node: Node): boolean => {\n if (node.type !== 'listItem') return false\n const children = (node as DirectiveNode).children || []\n return children.length === 1 && children[0].type === 'leafDirective'\n}\n\nconst KNOWN_DIRECTIVE_NAMES: Set<string> = new Set(Object.values(DIRECTIVE_NAMES))\n\nfunction getTextContent(node: DirectiveNode): string {\n if (typeof node.value === 'string') return node.value\n if (!node.children) return ''\n return node.children.map((child) => getTextContent(child)).join('')\n}\n\n// ============================================================================\n// Remark Plugin\n// ============================================================================\n\n/**\n * Remark plugin that transforms directive syntax into React components.\n *\n * - Validates directive names (letters and hyphens only)\n * - Rescues unparsed block directives from paragraph text (lists, trailing text, streaming junk)\n * - Extracts directives from list items\n * - Groups consecutive leaf directives into DirectivesStack wrappers\n * - Filters incomplete streaming directives\n * - Converts directive names to PascalCase component names\n */\nfunction remarkDirectivesTransform() {\n return (tree: Node) => {\n // Remove invalid directive nodes (e.g., \":02\" in \"14:02\")\n visit(\n tree,\n DIRECTIVE_TYPES,\n (node: Node, index: number | undefined, parent: Node | undefined) => {\n if (!isDirectiveNode(node)) {\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n const nodeName = 'name' in node ? String((node as DirectiveNode).name) : ''\n parentNode.children[index] = {\n type: 'text' as DirectiveType,\n name: '',\n value: `:${nodeName}`,\n } as DirectiveNode\n }\n }\n },\n )\n\n // Rescue unparsed block directives from paragraph text.\n // When the LLM outputs directives inside lists, with trailing text, or when\n // the streaming preprocessor appends junk characters (e.g. closing a `_`),\n // micromark cannot parse them as leaf directives because they require their\n // own line with no surrounding content. This step finds block directive\n // patterns in paragraph text and promotes them to proper directive nodes,\n // discarding any surrounding text on the same line.\n visit(tree, 'paragraph', (node: Node, index: number | undefined, parent: Node | undefined) => {\n const paragraphNode = node as DirectiveNode\n const text = getTextContent(paragraphNode)\n const match = text.match(BLOCK_DIRECTIVE)\n if (!match) return\n\n const [, name, label, attrsString] = match\n if (!KNOWN_DIRECTIVE_NAMES.has(name)) return\n\n const directiveNode: DirectiveNode = {\n type: 'leafDirective',\n name,\n attributes: parseDirectiveAttributes(attrsString),\n children: label\n ? [{ type: 'text' as DirectiveType, name: '', value: label } as DirectiveNode]\n : [],\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children[index] = directiveNode\n }\n })\n\n // Extract directives from list items\n visit(\n tree,\n 'list',\n (node: Node, index: number | undefined, parent: Node | undefined): number | undefined => {\n const listNode = node as DirectiveNode\n if (!listNode.children) return undefined\n\n const transformed: DirectiveNode[] = []\n let hasTransformed = false\n\n for (const child of listNode.children) {\n if (isListItemWithOnlyDirective(child) && child.children?.[0]) {\n transformed.push(child.children[0])\n hasTransformed = true\n } else {\n transformed.push(child)\n }\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (hasTransformed && transformed.every((n) => isDirectiveNode(n))) {\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children.splice(index, 1, ...transformed)\n return index + transformed.length\n }\n }\n return undefined\n },\n )\n\n // Group consecutive leaf directives into DirectivesStack\n visit(tree, (node: Node) => {\n const dirNode = node as DirectiveNode\n if (!dirNode.children?.length || dirNode.data?.hName === 'DirectivesStack') return\n\n const newChildren: DirectiveNode[] = []\n let group: DirectiveNode[] = []\n\n for (const child of dirNode.children) {\n if (isDirectiveNode(child) && child.type === 'leafDirective') {\n group.push(child)\n } else {\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n group = []\n }\n newChildren.push(child)\n }\n }\n\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n }\n\n dirNode.children = newChildren\n })\n\n // Filter incomplete streaming directives\n visit(tree, 'text', (node: Node) => {\n const textNode = node as DirectiveNode\n if (typeof textNode.value === 'string') {\n if (textNode.value.match(/::?\\w+[{[]/) && !textNode.value.match(/::?\\w+[{[].+[}\\]]/)) {\n textNode.value = ''\n }\n }\n })\n\n // Transform directives to React components\n visit(tree, DIRECTIVE_TYPES, (node: Node) => {\n if (!isDirectiveNode(node)) return\n\n node.data = node.data || {}\n node.data.hName = pascalCase(node.name)\n node.data.hProperties = {\n ...node.attributes,\n isInline: node.type === 'textDirective',\n }\n })\n }\n}\n\ninterface RemarkData {\n micromarkExtensions?: unknown[]\n fromMarkdownExtensions?: unknown[]\n toMarkdownExtensions?: unknown[]\n}\n\n/**\n * Remark plugin for agent directive support.\n * Parses and transforms directive syntax into React components.\n *\n * @example\n * ```tsx\n * import { remarkAgentDirectives } from '@sanity/agent-directives/react'\n *\n * <ReactMarkdown remarkPlugins={[remarkAgentDirectives]}>\n * {content}\n * </ReactMarkdown>\n * ```\n */\nexport function remarkAgentDirectives(this: Processor) {\n const data = this.data() as RemarkData\n\n if (!data.micromarkExtensions) data.micromarkExtensions = []\n if (!data.fromMarkdownExtensions) data.fromMarkdownExtensions = []\n if (!data.toMarkdownExtensions) data.toMarkdownExtensions = []\n\n data.micromarkExtensions.push(directive())\n data.fromMarkdownExtensions.push(directiveFromMarkdown())\n data.toMarkdownExtensions.push(directiveToMarkdown())\n\n return remarkDirectivesTransform()\n}\n\n// ============================================================================\n// Directive Kit\n// ============================================================================\n\nexport interface DirectiveKitOptions<TApplication> {\n useApplication?: (source: string | undefined) => TApplication | undefined\n renderApplicationUnavailable?: ComponentType<{ name: DirectiveName; source?: string }>\n}\n\n/**\n * Creates a directive kit with application injection.\n *\n * @example\n * ```tsx\n * // kit.ts\n * export const { defineDirective } = createDirectiveKit<Application>({\n * useApplication: (source) => useThreadApplication(source),\n * })\n *\n * // Document.tsx\n * export const DocumentDirective = defineDirective('document', ({ id, application }) => {\n * return <DocumentCard id={id} app={application} />\n * })\n * ```\n */\nexport function createDirectiveKit<TApplication = undefined>(\n options: DirectiveKitOptions<TApplication> = {},\n) {\n const { useApplication, renderApplicationUnavailable: Unavailable } = options\n\n function defineDirective<\n TName extends DirectiveName,\n TRequiresApplication extends boolean = true,\n >(\n name: TName,\n render: (props: DirectiveRenderProps<TName, TApplication, TRequiresApplication>) => ReactNode,\n directiveOptions: DefineDirectiveOptions = {},\n ): ComponentType<DirectiveComponentProps<TName>> {\n const requiresContext =\n directiveOptions.requiresContext ?? directiveOptions.requiresApplication ?? true\n\n function Directive(props: DirectiveComponentProps<TName>): ReactNode {\n const { node, source, ...rest } = props\n\n const application = useApplication?.(source)\n if (requiresContext && useApplication && !application) {\n return Unavailable ? <Unavailable name={name} source={source} /> : null\n }\n\n const merged = {\n ...(node?.properties ?? {}),\n ...rest,\n ...(source !== undefined && { source }),\n }\n\n const schema = DirectiveSchemas[name]\n const result = schema.safeParse(merged)\n if (!result.success) {\n console.error(`Directive \"${name}\" validation error:`, result.error.format())\n return null\n }\n\n const renderProps = {\n ...(result.data as DirectiveProps<TName>),\n ...(requiresContext && useApplication ? { application } : {}),\n isInline: merged.isInline === true,\n } as DirectiveRenderProps<TName, TApplication, TRequiresApplication>\n\n return render(renderProps)\n }\n\n Directive.displayName = `Directive(${name})`\n return Directive\n }\n\n return { defineDirective }\n}\n"],"names":[],"mappings":";;;;;;AA+DA,MAAM,kBAAkB,CAAC,SACvB,UAAU,QACV,OAAQ,KAAuB,QAAS,YACxC,eAAe,KAAM,KAAuB,IAAI,KAChD,gBAAgB,SAAS,KAAK,IAAqB,GAE/C,8BAA8B,CAAC,SAAwB;AAC3D,MAAI,KAAK,SAAS,WAAY,QAAO;AACrC,QAAM,WAAY,KAAuB,YAAY,CAAA;AACrD,SAAO,SAAS,WAAW,KAAK,SAAS,CAAC,EAAE,SAAS;AACvD,GAEM,wBAAqC,IAAI,IAAI,OAAO,OAAO,eAAe,CAAC;AAEjF,SAAS,eAAe,MAA6B;AACnD,SAAI,OAAO,KAAK,SAAU,WAAiB,KAAK,QAC3C,KAAK,WACH,KAAK,SAAS,IAAI,CAAC,UAAU,eAAe,KAAK,CAAC,EAAE,KAAK,EAAE,IADvC;AAE7B;AAgBA,SAAS,4BAA4B;AACnC,SAAO,CAAC,SAAe;AAErB;AAAA,MACE;AAAA,MACA;AAAA,MACA,CAAC,MAAY,OAA2B,WAA6B;AACnE,YAAI,CAAC,gBAAgB,IAAI,GAAG;AAC1B,gBAAM,aAAa;AACnB,cAAI,YAAY,YAAY,OAAO,SAAU,UAAU;AACrD,kBAAM,WAAW,UAAU,OAAO,OAAQ,KAAuB,IAAI,IAAI;AACzE,uBAAW,SAAS,KAAK,IAAI;AAAA,cAC3B,MAAM;AAAA,cACN,MAAM;AAAA,cACN,OAAO,IAAI,QAAQ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AA2BO,SAAS,mBACd,UAA6C,IAC7C;AACA,QAAM,EAAE,gBAAgB,8BAA8B,YAAA,IAAgB;AAEtE,WAAS,gBAIP,MACA,QACA,mBAA2C,CAAA,GACI;AAC/C,UAAM,kBACJ,iBAAiB,mBAAmB,iBAAiB,uBAAuB;AAE9E,aAAS,UAAU,OAAkD;AACnE,YAAM,EAAE,MAAM,QAAQ,GAAG,SAAS,OAE5B,cAAc,iBAAiB,MAAM;AAC3C,UAAI,mBAAmB,kBAAkB,CAAC;AACxC,eAAO,cAAc,oBAAC,aAAA,EAAY,MAAY,QAAgB,IAAK;AAGrE,YAAM,SAAS;AAAA,QACb,GAAI,MAAM,cAAc,CAAA;AAAA,QACxB,GAAG;AAAA,QACH,GAAI,WAAW,UAAa,EAAE,OAAA;AAAA,MAAO,GAIjC,SADS,iBAAiB,IAAI,EACd,UAAU,MAAM;AACtC,UAAI,CAAC,OAAO;AACV,eAAA,QAAQ,MAAM,cAAc,IAAI,uBAAuB,OAAO,MAAM,OAAA,CAAQ,GACrE;AAGT,YAAM,cAAc;AAAA,QAClB,GAAI,OAAO;AAAA,QACX,GAAI,mBAAmB,iBAAiB,EAAE,YAAA,IAAgB,CAAA;AAAA,QAC1D,UAAU,OAAO,aAAa;AAAA,MAAA;AAGhC,aAAO,OAAO,WAAW;AAAA,IAC3B;AAEA,WAAA,UAAU,cAAc,aAAa,IAAI,KAClC;AAAA,EACT;AAEA,SAAO,EAAE,gBAAA;AACX;"}
|
|
1
|
+
{"version":3,"file":"react.js","sources":["../../src/lib/react.tsx"],"sourcesContent":["import { pascalCase } from 'es-toolkit'\nimport { directiveFromMarkdown, directiveToMarkdown } from 'mdast-util-directive'\nimport { directive } from 'micromark-extension-directive'\nimport type { ComponentType, ReactNode } from 'react'\nimport type { Processor } from 'unified'\nimport type { Node } from 'unist'\nimport { visit } from 'unist-util-visit'\nimport {\n BLOCK_DIRECTIVE,\n type DefineDirectiveOptions,\n DIRECTIVE_NAMES,\n DIRECTIVE_TYPES,\n type DirectiveName,\n type DirectivePropsMap,\n DirectiveSchemas,\n type DirectiveType,\n parseDirectiveAttributes,\n} from '../_internal'\n\n// ============================================================================\n// Types\n// ============================================================================\n\ntype DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T]\n\nexport interface NodeProps {\n node?: { properties?: Record<string, unknown> }\n}\n\nexport type DirectiveRenderProps<\n TName extends DirectiveName,\n TApplication = undefined,\n TRequiresApplication extends boolean = true,\n> = DirectiveProps<TName> & {\n isInline: boolean\n} & (TRequiresApplication extends true\n ? TApplication extends undefined\n ? {}\n : { application: TApplication }\n : {})\n\nexport type { DefineDirectiveOptions }\n\nexport type DirectiveComponentProps<TName extends DirectiveName> = DirectiveProps<TName> & {\n source?: string\n} & NodeProps\n\n// ============================================================================\n// Remark Plugin Types\n// ============================================================================\n\ninterface DirectiveNode extends Node {\n type: 'textDirective' | 'leafDirective' | 'containerDirective'\n name: string\n attributes?: Record<string, string>\n children?: DirectiveNode[]\n value?: string\n data?: {\n hName?: string\n hProperties?: Record<string, unknown>\n }\n}\n\nconst isDirectiveNode = (node: Node): node is DirectiveNode =>\n 'name' in node &&\n typeof (node as DirectiveNode).name === 'string' &&\n /^[a-zA-Z-]+$/.test((node as DirectiveNode).name) &&\n DIRECTIVE_TYPES.includes(node.type as DirectiveType)\n\nconst isListItemWithOnlyDirective = (node: Node): boolean => {\n if (node.type !== 'listItem') return false\n const children = (node as DirectiveNode).children || []\n return children.length === 1 && children[0].type === 'leafDirective'\n}\n\nconst KNOWN_DIRECTIVE_NAMES: Set<string> = new Set(Object.values(DIRECTIVE_NAMES))\n\nfunction getTextContent(node: DirectiveNode): string {\n if (typeof node.value === 'string') return node.value\n if (!node.children) return ''\n return node.children.map((child) => getTextContent(child)).join('')\n}\n\n// ============================================================================\n// Remark Plugin\n// ============================================================================\n\n/**\n * Remark plugin that transforms directive syntax into React components.\n *\n * - Validates directive names (letters and hyphens only)\n * - Rescues unparsed block directives from paragraph text (lists, trailing text, streaming junk)\n * - Extracts directives from list items\n * - Groups consecutive leaf directives into DirectivesStack wrappers\n * - Filters incomplete streaming directives\n * - Converts directive names to PascalCase component names\n */\nfunction remarkDirectivesTransform() {\n return (tree: Node) => {\n // Remove invalid directive nodes (e.g., \":02\" in \"14:02\")\n visit(\n tree,\n DIRECTIVE_TYPES,\n (node: Node, index: number | undefined, parent: Node | undefined) => {\n if (!isDirectiveNode(node)) {\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n const nodeName = 'name' in node ? String((node as DirectiveNode).name) : ''\n parentNode.children[index] = {\n type: 'text' as DirectiveType,\n name: '',\n value: `:${nodeName}`,\n } as DirectiveNode\n }\n }\n },\n )\n\n // Rescue unparsed block directives from paragraph text.\n // When the LLM outputs directives inside lists, with trailing text, or when\n // the streaming preprocessor appends junk characters (e.g. closing a `_`),\n // micromark cannot parse them as leaf directives because they require their\n // own line with no surrounding content. This step finds block directive\n // patterns in paragraph text and promotes them to proper directive nodes,\n // discarding any surrounding text on the same line.\n visit(tree, 'paragraph', (node: Node, index: number | undefined, parent: Node | undefined) => {\n const paragraphNode = node as DirectiveNode\n const text = getTextContent(paragraphNode)\n const match = text.match(BLOCK_DIRECTIVE)\n if (!match) return\n\n const [, name, label, attrsString] = match\n if (!KNOWN_DIRECTIVE_NAMES.has(name)) return\n\n const directiveNode: DirectiveNode = {\n type: 'leafDirective',\n name,\n attributes: parseDirectiveAttributes(attrsString),\n children: label\n ? [{ type: 'text' as DirectiveType, name: '', value: label } as DirectiveNode]\n : [],\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children[index] = directiveNode\n }\n })\n\n // Rescue unparsed block directives from table cells.\n // Leaf directives (::name{attrs}) are registered as flow content in micromark,\n // but GFM table cells only contain text (phrasing) content. This means ::\n // directives render as plain text inside tables. We rescue them here and\n // convert to textDirective (inline) so they render with isInline=true.\n visit(tree, 'tableCell', (node: Node) => {\n const cellNode = node as DirectiveNode\n if (!cellNode.children) return\n\n for (let i = 0; i < cellNode.children.length; i++) {\n const child = cellNode.children[i]\n if (typeof child.value !== 'string') continue\n\n const match = child.value.match(BLOCK_DIRECTIVE)\n if (!match) continue\n\n const [, name, label, attrsString] = match\n if (!KNOWN_DIRECTIVE_NAMES.has(name)) continue\n\n cellNode.children[i] = {\n type: 'textDirective',\n name,\n attributes: parseDirectiveAttributes(attrsString),\n children: label\n ? [{ type: 'text' as DirectiveType, name: '', value: label } as DirectiveNode]\n : [],\n }\n }\n })\n\n // Downgrade any leafDirective inside a table cell to textDirective (inline).\n // This handles cases where a directive was already parsed but as the wrong\n // type for inline context (e.g. from a prior rescue pass or future parser changes).\n visit(tree, 'tableCell', (node: Node) => {\n const cellNode = node as DirectiveNode\n if (!cellNode.children) return\n\n for (const child of cellNode.children) {\n if (child.type === 'leafDirective') {\n child.type = 'textDirective'\n }\n }\n })\n\n // Extract directives from list items\n visit(\n tree,\n 'list',\n (node: Node, index: number | undefined, parent: Node | undefined): number | undefined => {\n const listNode = node as DirectiveNode\n if (!listNode.children) return undefined\n\n const transformed: DirectiveNode[] = []\n let hasTransformed = false\n\n for (const child of listNode.children) {\n if (isListItemWithOnlyDirective(child) && child.children?.[0]) {\n transformed.push(child.children[0])\n hasTransformed = true\n } else {\n transformed.push(child)\n }\n }\n\n const parentNode = parent as DirectiveNode | undefined\n if (hasTransformed && transformed.every((n) => isDirectiveNode(n))) {\n if (parentNode?.children && typeof index === 'number') {\n parentNode.children.splice(index, 1, ...transformed)\n return index + transformed.length\n }\n }\n return undefined\n },\n )\n\n // Group consecutive leaf directives into DirectivesStack\n visit(tree, (node: Node) => {\n const dirNode = node as DirectiveNode\n if (!dirNode.children?.length || dirNode.data?.hName === 'DirectivesStack') return\n\n const newChildren: DirectiveNode[] = []\n let group: DirectiveNode[] = []\n\n for (const child of dirNode.children) {\n if (isDirectiveNode(child) && child.type === 'leafDirective') {\n group.push(child)\n } else {\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n group = []\n }\n newChildren.push(child)\n }\n }\n\n if (group.length > 0) {\n newChildren.push({\n type: 'leafDirective',\n name: 'DirectivesStack',\n data: { hName: 'DirectivesStack' },\n children: group,\n })\n }\n\n dirNode.children = newChildren\n })\n\n // Filter incomplete streaming directives\n visit(tree, 'text', (node: Node) => {\n const textNode = node as DirectiveNode\n if (typeof textNode.value === 'string') {\n if (textNode.value.match(/::?\\w+[{[]/) && !textNode.value.match(/::?\\w+[{[].+[}\\]]/)) {\n textNode.value = ''\n }\n }\n })\n\n // Transform directives to React components\n visit(tree, DIRECTIVE_TYPES, (node: Node) => {\n if (!isDirectiveNode(node)) return\n\n node.data = node.data || {}\n node.data.hName = pascalCase(node.name)\n node.data.hProperties = {\n ...node.attributes,\n isInline: node.type === 'textDirective',\n }\n })\n }\n}\n\ninterface RemarkData {\n micromarkExtensions?: unknown[]\n fromMarkdownExtensions?: unknown[]\n toMarkdownExtensions?: unknown[]\n}\n\n/**\n * Remark plugin for agent directive support.\n * Parses and transforms directive syntax into React components.\n *\n * @example\n * ```tsx\n * import { remarkAgentDirectives } from '@sanity/agent-directives/react'\n *\n * <ReactMarkdown remarkPlugins={[remarkAgentDirectives]}>\n * {content}\n * </ReactMarkdown>\n * ```\n */\nexport function remarkAgentDirectives(this: Processor) {\n const data = this.data() as RemarkData\n\n if (!data.micromarkExtensions) data.micromarkExtensions = []\n if (!data.fromMarkdownExtensions) data.fromMarkdownExtensions = []\n if (!data.toMarkdownExtensions) data.toMarkdownExtensions = []\n\n data.micromarkExtensions.push(directive())\n data.fromMarkdownExtensions.push(directiveFromMarkdown())\n data.toMarkdownExtensions.push(directiveToMarkdown())\n\n return remarkDirectivesTransform()\n}\n\n// ============================================================================\n// Directive Kit\n// ============================================================================\n\nexport interface DirectiveKitOptions<TApplication> {\n useApplication?: (source: string | undefined) => TApplication | undefined\n renderApplicationUnavailable?: ComponentType<{ name: DirectiveName; source?: string }>\n}\n\n/**\n * Creates a directive kit with application injection.\n *\n * @example\n * ```tsx\n * // kit.ts\n * export const { defineDirective } = createDirectiveKit<Application>({\n * useApplication: (source) => useThreadApplication(source),\n * })\n *\n * // Document.tsx\n * export const DocumentDirective = defineDirective('document', ({ id, application }) => {\n * return <DocumentCard id={id} app={application} />\n * })\n * ```\n */\nexport function createDirectiveKit<TApplication = undefined>(\n options: DirectiveKitOptions<TApplication> = {},\n) {\n const { useApplication, renderApplicationUnavailable: Unavailable } = options\n\n function defineDirective<\n TName extends DirectiveName,\n TRequiresApplication extends boolean = true,\n >(\n name: TName,\n render: (props: DirectiveRenderProps<TName, TApplication, TRequiresApplication>) => ReactNode,\n directiveOptions: DefineDirectiveOptions = {},\n ): ComponentType<DirectiveComponentProps<TName>> {\n const requiresContext =\n directiveOptions.requiresContext ?? directiveOptions.requiresApplication ?? true\n\n function Directive(props: DirectiveComponentProps<TName>): ReactNode {\n const { node, source, ...rest } = props\n\n const application = useApplication?.(source)\n if (requiresContext && useApplication && !application) {\n return Unavailable ? <Unavailable name={name} source={source} /> : null\n }\n\n const merged = {\n ...(node?.properties ?? {}),\n ...rest,\n ...(source !== undefined && { source }),\n }\n\n const schema = DirectiveSchemas[name]\n const result = schema.safeParse(merged)\n if (!result.success) {\n console.error(`Directive \"${name}\" validation error:`, result.error.format())\n return null\n }\n\n const renderProps = {\n ...(result.data as DirectiveProps<TName>),\n ...(requiresContext && useApplication ? { application } : {}),\n isInline: merged.isInline === true,\n } as DirectiveRenderProps<TName, TApplication, TRequiresApplication>\n\n return render(renderProps)\n }\n\n Directive.displayName = `Directive(${name})`\n return Directive\n }\n\n return { defineDirective }\n}\n"],"names":[],"mappings":";;;;;;AA+DA,MAAM,kBAAkB,CAAC,SACvB,UAAU,QACV,OAAQ,KAAuB,QAAS,YACxC,eAAe,KAAM,KAAuB,IAAI,KAChD,gBAAgB,SAAS,KAAK,IAAqB,GAE/C,8BAA8B,CAAC,SAAwB;AAC3D,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iveName } from "../_chunks-dts/index.js";
|
|
2
2
|
import { Root } from "mdast";
|
|
3
3
|
type DirectiveProps<T extends DirectiveName> = DirectivePropsMap[T];
|
|
4
4
|
interface DirectiveNode {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/agent-directives",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "Shared directive system for Sanity Agent",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"es-toolkit": "^1.
|
|
40
|
+
"es-toolkit": "^1.44.0",
|
|
41
41
|
"mdast-util-directive": "^3.0.0",
|
|
42
42
|
"micromark-extension-directive": "^3.0.0",
|
|
43
43
|
"remark-directive": "^3.0.0",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"react": "^19.0.0",
|
|
59
59
|
"react-dom": "^19.0.0",
|
|
60
60
|
"react-markdown": "^10.0.0",
|
|
61
|
+
"remark-gfm": "^4.0.1",
|
|
61
62
|
"typescript": "^5.9.3",
|
|
62
63
|
"vitest": "^4.0.18",
|
|
63
64
|
"@repo/package.config": "0.0.0",
|