<commit_before>
'use strict'

############################################################################################################
PATH                      = require 'path'
# njs_fs                    = require 'fs'
#...........................................................................................................
CND                       = require 'cnd'
rpr                       = CND.rpr
badge                     = 'mkts/tex-writer-call'
log                       = CND.get_logger 'plain',     badge
info                      = CND.get_logger 'info',      badge
whisper                   = CND.get_logger 'whisper',   badge
alert                     = CND.get_logger 'alert',     badge
debug                     = CND.get_logger 'debug',     badge
warn                      = CND.get_logger 'warn',      badge
help                      = CND.get_logger 'help',      badge
urge                      = CND.get_logger 'urge',      badge
echo                      = CND.echo.bind CND
#...........................................................................................................
PIPEDREAMS                = require '../../../pipedreams'
PS                        = require 'pipestreams'
{ $, $async, }            = PS
#...........................................................................................................
MD_READER                 = require './md-reader'
hide                      = MD_READER.hide.bind        MD_READER
copy                      = MD_READER.copy.bind        MD_READER
stamp                     = MD_READER.stamp.bind       MD_READER
select                    = MD_READER.select.bind      MD_READER
is_hidden                 = MD_READER.is_hidden.bind   MD_READER
is_stamped                = MD_READER.is_stamped.bind  MD_READER

#-----------------------------------------------------------------------------------------------------------
@_resolve_arguments = ( S, event ) =>
  reference_path                      = S.layout_info[ 'source-home' ]
  [ type, name, parameters, meta, ]   = event
  [ module_path, method_path, P..., ] = parameters
  locator                             = PATH.join reference_path, module_path
  [ crumbs..., method_name, ]         = method_path.split '.'
  #.....................................................................................................
  try
    module  = require locator
    module  = module[ crumb ] for crumb in crumbs
  catch error
    alert '98987', "when trying to resolve #{crumbs.join '.'}"
    alert '98987', "starting with module #{rpr locator}"
    alert '98987', "an error occurred at path component #{rpr crumb}"
    throw error
  #.....................................................................................................
  return {
    type,
    name,
    parameters,
    meta,
    module_path,
    method_path,
    P,
    method_name,
    locator,
    module,
    crumbs, }

#-----------------------------------------------------------------------------------------------------------
@$call_await = ( S ) =>
  ### TAINT implicitly assumes return value will be lines of text ###
  ### parses MKTS commands of the form `<<!call_await module_path, method_name, parameters... >>` ###
  # self = @
  return PIPEDREAMS.$async ( event, send, end ) =>
    #.......................................................................................................
    if event? and select event, '!', 'call_await'
      { module, method_name, P, meta, locator, crumbs, }  = @_resolve_arguments S, event
      ctx                                                 = { S, event, }
      #.....................................................................................................
      try
        lines   = await module[ method_name ] ctx, P...
      #.....................................................................................................
      catch error
        alert '98987', "when trying to call method #{rpr method_name}"
        alert '98987', "from module #{rpr locator}##{crumbs.join '.'}"
        alert '98987', "with arguments #{rpr P}"
        alert '98987', "an error occurred:"
        alert '98987', error.message
        throw error if throw_all_errors
        send [ '.', 'warning', error.message, ( copy meta ), ]
        return send.done()
      #.....................................................................................................
      for line in lines
        if CND.isa_text line
          line = line + '\n' unless line.endsWith '\n'
          send [ '.', 'text', line, ( copy meta ), ]
        else
          send line
      send.done()
    #.......................................................................................................
    else
      send event
      send.done()
    #.......................................................................................................
    end() if end?
    return null

#-----------------------------------------------------------------------------------------------------------
@$call_stream = ( S ) =>
  ### TAINT implicitly assumes return value will be lines of text ###
  # self = @
  return PIPEDREAMS.$async ( event, send, end ) =>
    #.......................................................................................................
    if event? and select event, '!', 'call_stream'
      { module, method_name, P, meta, locator, crumbs, }  = @_resolve_arguments S, event
      ctx                                                 = { S, event, }
      #.....................................................................................................
      try<commit_msg>fix error handling<commit_after>
'use strict'

############################################################################################################
PATH                      = require 'path'
# njs_fs                    = require 'fs'
#...........................................................................................................
CND                       = require 'cnd'
rpr                       = CND.rpr
badge                     = 'mkts/tex-writer-call'
log                       = CND.get_logger 'plain',     badge
info                      = CND.get_logger 'info',      badge
whisper                   = CND.get_logger 'whisper',   badge
alert                     = CND.get_logger 'alert',     badge
debug                     = CND.get_logger 'debug',     badge
warn                      = CND.get_logger 'warn',      badge
help                      = CND.get_logger 'help',      badge
urge                      = CND.get_logger 'urge',      badge
echo                      = CND.echo.bind CND
#...........................................................................................................
PIPEDREAMS                = require '../../../pipedreams'
PS                        = require 'pipestreams'
{ $, $async, }            = PS
#...........................................................................................................
MD_READER                 = require './md-reader'
hide                      = MD_READER.hide.bind        MD_READER
copy                      = MD_READER.copy.bind        MD_READER
stamp                     = MD_READER.stamp.bind       MD_READER
select                    = MD_READER.select.bind      MD_READER
is_hidden                 = MD_READER.is_hidden.bind   MD_READER
is_stamped                = MD_READER.is_stamped.bind  MD_READER

#-----------------------------------------------------------------------------------------------------------
@_resolve_arguments = ( S, event ) =>
  reference_path                      = S.layout_info[ 'source-home' ]
  [ type, name, parameters, meta, ]   = event
  [ module_path, method_path, P..., ] = parameters
  locator                             = PATH.join reference_path, module_path
  [ crumbs..., method_name, ]         = method_path.split '.'
  #.....................................................................................................
  try
    module  = require locator
    module  = module[ crumb ] for crumb in crumbs
  catch error
    alert '98987', "when trying to resolve #{crumbs.join '.'}"
    alert '98987', "starting with module #{rpr locator}"
    alert '98987', "an error occurred at path component #{rpr crumb}"
    throw error
  #.....................................................................................................
  return {
    type,
    name,
    parameters,
    meta,
    module_path,
    method_path,
    P,
    method_name,
    locator,
    module,
    crumbs, }

#-----------------------------------------------------------------------------------------------------------
@$call_await = ( S ) =>
  ### TAINT implicitly assumes return value will be lines of text ###
  ### parses MKTS commands of the form `<<!call_await module_path, method_name, parameters... >>` ###
  # self = @
  return PIPEDREAMS.$async ( event, send, end ) =>
    #.......................................................................................................
    if event? and select event, '!', 'call_await'
      { module, method_name, P, meta, locator, crumbs, }  = @_resolve_arguments S, event
      ctx                                                 = { S, event, }
      #.....................................................................................................
      try
        lines   = await module[ method_name ] ctx, P...
      #.....................................................................................................
      catch error
        alert '98987', "when trying to call method #{rpr method_name}"
        alert '98987', "from module #{rpr locator}##{crumbs.join '.'}"
        alert '98987', "with arguments #{rpr P}"
        alert '98987', "an error occurred:"
        alert '98987', error.message
        throw error
      #.....................................................................................................
      for line in lines
        if CND.isa_text line
          line = line + '\n' unless line.endsWith '\n'
          send [ '.', 'text', line, ( copy meta ), ]
        else
          send line
      send.done()
    #.......................................................................................................
    else
      send event
      send.done()
    #.......................................................................................................
    end() if end?
    return null

#-----------------------------------------------------------------------------------------------------------
@$call_stream = ( S ) =>
  ### TAINT implicitly assumes return value will be lines of text ###
  # self = @
  return PIPEDREAMS.$async ( event, send, end ) =>
    #.......................................................................................................
    if event? and select event, '!', 'call_stream'
      { module, method_name, P, meta, locator, crumbs, }  = @_resolve_arguments S, event
      ctx                                                 = { S, event, }
      #.....................................................................................................
      try
{'fraction_non_alphanumeric': 0.3902504233134302, 'fraction_numerical': 0.007129489350325283, 'mean_word_length': 0.7561815336463223, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7190780', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2293, 'n_words': 624}