react-native icon indicating copy to clipboard operation
react-native copied to clipboard

HeadlessJS crashes on apps targeting newer Android SDKs

Open valeri-terziyski opened this issue 2 years ago • 11 comments

Description

The current HeadlessJS recomendation is to call startService on the passed context as described here

var service = Intent(applicationContext, MyTaskService::class)
applicationContext.startService(service)

This is problematic since Android 8 (SDK 26), there are limitations placed on the background services. So up until Android 12 (SDK 31) this could have been fixed with something like

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    applicationContext.startForegroundService(service)
} else {
    applicationContext.startService(service)
}

But this will cause issues on apps targeting higher SDKs, since there are additional resrtictions there.

So the latest recommended approach for starting a background service is by using either WorkManager or Coroutines - https://developer.android.com/guide/background#recommended-approaches

React Native Version

0.71.6

Output of npx react-native info

System: OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish) CPU: (8) x64 AMD Ryzen 7 4700U with Radeon Graphics Memory: 21.18 GB / 38.41 GB Shell: 5.1.16 - /bin/bash Binaries: Node: 16.14.2 - /usr/local/bin/node Yarn: 1.22.17 - /usr/local/bin/yarn npm: 8.5.0 - /usr/local/bin/npm Watchman: 2022.08.15.00 - /home/linuxbrew/.linuxbrew/bin/watchman SDKs: Android SDK: Not Found IDEs: Android Studio: Not Found Languages: Java: 11.0.18 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.2.0 => 18.2.0 react-native: 0.71.6 => 0.71.6 npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. Checkout the example app
  2. Build it with yarn start and yarn android
  3. Launch it
  4. The app crashes

Snack, code example, screenshot, or link to a repository

https://github.com/valeri-terziyski/react-native-headless-js-example

valeri-terziyski avatar Apr 05 '23 14:04 valeri-terziyski

Hi @vary! I'm here to help you with bugs, questions, and becoming a contributor. Let me know how I can assist you while you wait for a human maintainer.

To resolve the 400 error indicating "invalid request: cannot specify both message and tool_results in multistep mode" when using the "command-r-plus" model in an agent to invoke a tool, ensure that you are not specifying both message and tool_results in your request. The request should include either message or tool_results, but not both simultaneously.

Here is an example of how you might structure your request correctly:

model: command-r-plus
parameters:
  temperature: 0.7
  p: 0.75
  k: 50
  presence_penalty: 0.5
  frequency_penalty: 0.5
  max_tokens: 1024
# Ensure you only include either message or tool_results, not both
request:
  message: "Your message here"
  # tool_results: "Your tool results here"  # Comment this out or remove it

Make sure to adjust the parameters and request content according to your specific use case [1].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Jun 20 '24 10:06 dosubot[bot]

I encountered the same issue. Currently, I can only avoid it by setting force_single_step=True.

ice91 avatar Jul 03 '24 07:07 ice91

Encountered the same issue. Only thing I do differently than in documentation is that I stream the request. Cohere multistep tool docs show the following example

message = "Who is the mayor of the capital of Ontario?"
model = "command-r-plus"

# STEP 2: Check what tools the model wants to use and how

res = co.chat(model=model, message=message, force_single_step=False, tools=[web_search_tool])

# as long as the model sends back tool_calls,
# keep invoking tools and sending the results back to the model
while res.tool_calls:
  print(res.text) # This will be an observation and a plan with next steps
  tool_results = []
  for call in res.tool_calls:
    # use the `web_search` tool with the search query the model sent back
    web_search_results = {"call": call, "outputs": web_search(call.parameters["query"])}
    tool_results.append(web_search_results)

  res = co.chat(
    model="command-r-plus",
    chat_history=res.chat_history,
    message="",
    force_single_step=False,
    tools=[web_search_tool],
    tool_results=tool_results
  )

Are you invoking co.chat() or co.chat_stream(); I get this error when streaming the request. I believe multi-step streaming is not fully functional yet.

Omitting message or giving it an empty string with chat_stream will yield a (should have at least 1 token) error. Making it impossible without changing source-code.

surferfelix avatar Jul 10 '24 10:07 surferfelix