buildInitialPrompt method

String buildInitialPrompt(
  1. String userPrompt,
  2. String toolsJson, {
  3. String? optionsJson,
})

Build initial prompt with tools and user query using C++ implementation.

userPrompt The user's question/request toolsJson JSON array of tool definitions optionsJson Options as JSON (can be empty) Returns complete formatted prompt

Implementation

String buildInitialPrompt(
  String userPrompt,
  String toolsJson, {
  String? optionsJson,
}) {
  try {
    final buildFn = lib.lookupFunction<
        Int32 Function(
          Pointer<Utf8>,
          Pointer<Utf8>,
          Pointer<RacToolCallingOptionsStruct>,
          Pointer<Pointer<Utf8>>,
        ),
        int Function(
          Pointer<Utf8>,
          Pointer<Utf8>,
          Pointer<RacToolCallingOptionsStruct>,
          Pointer<Pointer<Utf8>>,
        )>('rac_tool_call_build_initial_prompt');

    final racFreeFn = lib.lookupFunction<Void Function(Pointer<Void>),
        void Function(Pointer<Void>)>('rac_free');

    final userPtr = userPrompt.toNativeUtf8();
    final toolsPtr = toolsJson.toNativeUtf8();
    final optionsPtr = calloc<RacToolCallingOptionsStruct>();
    final promptPtrPtr = calloc<Pointer<Utf8>>();

    // Set default options
    optionsPtr.ref.maxToolCalls = 5;
    optionsPtr.ref.autoExecute = RAC_TRUE;
    optionsPtr.ref.temperature = 0.7;
    optionsPtr.ref.maxTokens = 1024;
    optionsPtr.ref.systemPrompt = nullptr;
    optionsPtr.ref.replaceSystemPrompt = RAC_FALSE;
    optionsPtr.ref.keepToolsAvailable = RAC_FALSE;

    try {
      final rc = buildFn(userPtr, toolsPtr, optionsPtr, promptPtrPtr);

      if (rc != RAC_SUCCESS || promptPtrPtr.value == nullptr) {
        return userPrompt;
      }

      final result = promptPtrPtr.value.toDartString();
      racFreeFn(promptPtrPtr.value.cast());
      return result;
    } finally {
      calloc.free(userPtr);
      calloc.free(toolsPtr);
      calloc.free(optionsPtr);
      calloc.free(promptPtrPtr);
    }
  } catch (e) {
    _logger.error('buildInitialPrompt failed: $e');
    return userPrompt;
  }
}