تخطَّ إلى المحتوى

Flutter

التثبيت

flutter pub add heysada_rafiq

شرح الاستخدام أدناه بالإنجليزية، كما يظهر في سجل الحزمة، والشيفرة نفسها في كل لغة.

The assistant’s chat for Flutter apps, laid out as the ChatGPT and Claude apps are: a greeting with suggestions, the person’s messages in bubbles and the answers as full-width text, the run’s steps in a small box and a shimmering line while it works, a rounded composer with attach and Stop, and the person’s chats with rename and delete. Every part of a message is drawn from its view with the app’s published design (ADR 031), and the frame from the design’s regions (ADR 035). Ships as products.rafiq.sdk.flutter in config/brands.json.

final client = RafiqClient(
  baseUrl: 'https://assistant.example.com', // the panel's Install screen fills these in
  appKey: 'pk_…',
  userToken: () => myApi.rafiqToken(), // optional: the signed-in person, null while signed out
);
final chat = RafiqChatController(client: client, language: 'ar')..start();

// The whole screen: the chat, the person's chats, a new chat.
RafiqChatScreen(
  controller: chat,
  language: 'ar',
  suggestions: const ['Where is my order?', 'Return an item'],
  attachments: pickPicture, // optional: your own picker (image_picker, file_selector)
);

// Or one chat inside your own screen, and the chats as a screen of their own:
RafiqChatView(controller: chat, language: 'ar', suggestions: const ['Where is my order?']);
RafiqChatList(controller: RafiqChatListController(client: client)..start(), onOpen: chat.open);
  • With appKey alone the client starts its own session (POST /v1/sessions/sdk): a guest Rafiq names, kept on the device (store, shared preferences by default), or the signed-in person when userToken gives your server’s token (HS256, signed with the app’s identity secret). It sends the app’s own id (X-App-Id, the package or bundle id), which a live app’s key is held to. session (advanced) gives a JWT your backend mints with the app’s secret instead.
  • Each answer has a thumbs up and down (RafiqChatController.rate); a thumbs down asks, optionally, what went wrong.

The screen

  • A new chat opens on the assistant’s logo and name, a greeting, and suggestions: as cards a tap sends; they step aside once the person writes. The chat is made on the server only when the first message (or the first picture) goes.
  • Messages: the person’s words are a soft bubble at the end side (the message_user region’s look: background, colour, radius, padding, max width), right in English and left in Arabic; the assistant’s answer is full-width text, its run’s work in a small box above it.
  • The run line shimmers with what the run is doing (run.step) while it works, still when the device asks for less motion; a failed run says why.
  • An answer’s actions: the newest answer has copy and ask again under it; any other message opens its menu on a long press.
  • The composer is a rounded box (the composer region) with the words on top and its tools under them: attach at the start, then the mic when it is empty, Stop while the person’s run works and nothing is written, or send. The design’s attach and voice switches turn those off.
  • The header names the assistant with its logo. In RafiqChatScreen it opens the person’s chats and starts a new one; onClose: adds a close button.
  • The chats (RafiqChatList): the person’s chats, the latest first, kept live by the list’s stream; a row’s menu (its ⋯, or a long press) renames or deletes it at once and puts it back, with the server’s reason, if the server refuses (a chat can’t be deleted while the AI works in it). Pull down to read it again.

Light or dark follows your app’s theme (not the phone’s setting); mode: fixes one. Every button has a label for screen readers and a 48 dp target.

Attach

The SDK has no picker of its own, so it adds no plugin to your app: pass attachments: a function that returns a RafiqAttachment (bytes, name, content type), or null when nothing was picked. A PNG, JPEG or WebP shows in the chat as a picture; anything else as a file. Without attachments: there is no attach button.

Future<RafiqAttachment?> pickPicture() async {
  final picked = await ImagePicker().pickImage(source: ImageSource.gallery);
  if (picked == null) return null;
  return RafiqAttachment(
      bytes: await picked.readAsBytes(), name: picked.name, contentType: picked.mimeType ?? 'image/jpeg');
}

The controller

RafiqChatController holds one chat at a time: open(id) switches to another (open(null): a new one) and keeps the design; send(text, attachments:) answers whether it went; attach(file) uploads; stop() stops the person’s run (canStop); regenerate(answer) asks again (canRegenerate); wordsOf(answer) is an answer’s text as written.

  • session (instead of appKey) gives the session JWT your backend mints (POST /v1/sessions with the app’s secret); it is asked again, once, when the server refuses it.
  • The chat’s read, its events and a sent message ask for part.v2. DesignKeeper holds the app’s bundle (GET /v1/design/bundle with its ETag: on start, every minute, and when a part names a newer design version; a 503 keeps what is held, else the renderer’s seed).
  • The assistant’s name and logo: the client’s own choice of name, else the bundle’s brand.assistant_name / brand.name / brand.logo when set, else the app’s white label, else the app’s name (https:// logos only). It follows a new bundle as it arrives; header: false leaves it out for a host with its own bar (controller.assistantName, .logo).
  • A press answers as the part’s own answer (a preview’s Edit as {action: "edit"}, a choice’s value, a form’s values); an answer that didn’t go lets its card answer again.
  • Client tools: tools: {'locate': RafiqClientTool(description: …, parameters: {…}, run: (input) => …)} are declared with every message to the AI and run on the device when the run calls one; hostCaller: httpHostCaller('https://api.example.com', headers: () => {...}) makes a client connection’s call (a change the person confirmed first) with your own session, and refuses any address outside that base. Only the person who started a run makes its calls, once a call.
  • example/ is a host app with the whole screen, suggestions and a picture picker: flutter run --dart-define=API_URL=… --dart-define=SESSION=….

The renderer (../ui) is vendored in by ../build.py vendor; the chat needs nothing of the core yet (task 460 moves it on).

Voice notes

The composer has a mic: RafiqChatView records, sends the note, and the server writes its words down in the language they were said. The host declares the permission, RECORD_AUDIO on Android and NSMicrophoneUsageDescription on iOS. voiceNotes: false takes the mic away.