@@ -35,6 +35,25 @@ export default defineDkgPlugin((ctx, mcp, api) => {
3535 }
3636 }
3737
38+ async function resolveJsonLdInput ( jsonld : string ) : Promise < string > {
39+ if ( ! jsonld ) {
40+ console . error ( "No JSON-LD content provided after file read." ) ;
41+ throw new Error ( "No JSON-LD content provided." ) ;
42+ }
43+
44+ if ( jsonld . startsWith ( "{" ) || jsonld . startsWith ( "[" ) ) {
45+ return jsonld ;
46+ }
47+
48+ const blob = await ctx . blob . get ( jsonld ) ;
49+ if ( ! blob ) {
50+ console . error ( `File with id "${ jsonld } " not found` ) ;
51+ throw new Error ( `File with id "${ jsonld } " not found` ) ;
52+ }
53+
54+ return consumers . text ( blob . data ) ;
55+ }
56+
3857 function validateSparqlInput ( query : string ) : SparqlValidationResult {
3958 const validation = validateSparqlQuery ( query ) ;
4059 if ( ! validation . valid ) {
@@ -191,21 +210,8 @@ export default defineDkgPlugin((ctx, mcp, api) => {
191210 } ,
192211 } ,
193212 async ( input ) => {
194- if ( ! input . jsonld ) {
195- console . error ( "No JSON-LD content provided after file read." ) ;
196- throw new Error ( "No JSON-LD content provided." ) ;
197- }
198213 const privacy = input . privacy || "private" ;
199- const content =
200- input . jsonld . startsWith ( "{" ) || input . jsonld . startsWith ( "[" )
201- ? input . jsonld
202- : await ctx . blob . get ( input . jsonld ) . then ( ( r ) => {
203- if ( ! r ) {
204- console . error ( `File with id "${ input . jsonld } " not found` ) ;
205- throw new Error ( `File with id "${ input . jsonld } " not found` ) ;
206- }
207- return consumers . text ( r . data ) ;
208- } ) ;
214+ const content = await resolveJsonLdInput ( input . jsonld ) ;
209215
210216 const { ual, error } = await publishJsonLdAsset ( content , privacy ) ;
211217 if ( error ) {
@@ -306,6 +312,82 @@ export default defineDkgPlugin((ctx, mcp, api) => {
306312 } ,
307313 ) ;
308314
315+ api . post (
316+ "/api/dkg/create" ,
317+ openAPIRoute (
318+ {
319+ tag : "DKG Publishing" ,
320+ summary : "Create and Publish DKG Asset" ,
321+ description :
322+ "Synchronously create and publish a Knowledge Asset on DKG from JSON-LD content or uploaded blob id." ,
323+ body : z . object ( {
324+ jsonld : z
325+ . string ( )
326+ . describe ( "JSON-LD content or ID of an uploaded file" ) ,
327+ privacy : z . enum ( [ "private" , "public" ] ) . optional ( ) . default ( "private" ) ,
328+ } ) ,
329+ response : {
330+ schema : z . object ( {
331+ success : z . boolean ( ) ,
332+ data : z
333+ . object ( {
334+ ual : z . string ( ) ,
335+ explorerLink : z . string ( ) ,
336+ message : z . string ( ) ,
337+ } )
338+ . optional ( ) ,
339+ error : z . string ( ) . optional ( ) ,
340+ } ) ,
341+ } ,
342+ finalizeRouteConfig : ( config ) => ( {
343+ ...config ,
344+ security : [ ] ,
345+ } ) ,
346+ } ,
347+ async ( req , res ) => {
348+ try {
349+ const privacy = req . body . privacy || "private" ;
350+ const content = await resolveJsonLdInput ( req . body . jsonld ) ;
351+
352+ const { ual, error } = await publishJsonLdAsset ( content , privacy ) ;
353+ if ( error ) {
354+ console . error ( "Error creating asset:" , error ) ;
355+ return res . status ( 500 ) . json ( {
356+ success : false ,
357+ error : "Failed to create asset: " + error ,
358+ } ) ;
359+ }
360+
361+ if ( ! ual ) {
362+ return res . status ( 500 ) . json ( {
363+ success : false ,
364+ error : "Failed to create asset: missing UAL in response" ,
365+ } ) ;
366+ }
367+
368+ const explorerLink = getExplorerUrl ( ual ) ;
369+ const message = `Knowledge Asset collection successfully created.\n\nUAL: ${ ual } \nDKG Explorer link: ${ explorerLink } ` ;
370+
371+ return res . json ( {
372+ success : true ,
373+ data : {
374+ ual,
375+ explorerLink,
376+ message,
377+ } ,
378+ } ) ;
379+ } catch ( error ) {
380+ const errorMessage =
381+ error instanceof Error ? error . message : String ( error ) ;
382+ return res . status ( 500 ) . json ( {
383+ success : false ,
384+ error : errorMessage ,
385+ } ) ;
386+ }
387+ } ,
388+ ) ,
389+ ) ;
390+
309391 api . post (
310392 "/api/dkg/query" ,
311393 openAPIRoute (
0 commit comments