Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fcitx5-engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ add_library(config_loader_obj OBJECT src/config_loader.cpp)
target_include_directories(config_loader_obj PUBLIC src)
set_target_properties(config_loader_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)

# ollama_request — pure request-body builder, consumed by the addon and tests
add_library(ollama_request_obj OBJECT src/ollama_request.cpp)
target_include_directories(ollama_request_obj PUBLIC src)
set_target_properties(ollama_request_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)

add_library(xtype-fcitx5 SHARED
src/xtype.cpp
src/inference_client.cpp
Expand All @@ -53,6 +58,7 @@ target_link_libraries(xtype-fcitx5 PRIVATE
style_profile_obj
prompt_builder_obj
config_loader_obj
ollama_request_obj
Fcitx5::Core
CURL::libcurl
)
Expand Down
79 changes: 79 additions & 0 deletions fcitx5-engine/src/caret_context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

// Pure helpers for the text a suggestion continues. Header-only and
// Fcitx5-free so tests can use them directly.

#include <optional>
#include <string>

namespace caret_context {

inline bool isContinuation(char c) { // UTF-8 continuation byte (10xxxxxx)
return (static_cast<unsigned char>(c) & 0xC0) == 0x80;
}

// The app's own text before the caret, from Fcitx5 surrounding text: `text`
// with the caret `cursorChars` code points in. nullopt when it can't be
// trusted — the caret is past the end, or the text before it doesn't end with
// `lastTyped` (the app hasn't reported the keystroke we just saw yet).
inline std::optional<std::string> beforeCaret(const std::string &text,
unsigned cursorChars,
const std::string &lastTyped) {
size_t bytes = 0;
for (unsigned n = 0; n < cursorChars; ++n) {
if (bytes >= text.size()) return std::nullopt;
++bytes;
while (bytes < text.size() && isContinuation(text[bytes])) ++bytes;
}
std::string before = text.substr(0, bytes);
if (before.size() < lastTyped.size() ||
before.compare(before.size() - lastTyped.size(), lastTyped.size(), lastTyped) != 0)
return std::nullopt;
return before;
}

// The last code point of s, or "" if s is empty.
inline std::string lastChar(const std::string &s) {
if (s.empty()) return {};
size_t start = s.size() - 1;
while (start > 0 && isContinuation(s[start])) --start;
return s.substr(start);
}

inline bool isBlank(char c) { return c == ' ' || c == '\t'; }

// The prompt to send for `context`: without trailing spaces/tabs. Tokenizers
// attach a word's leading space to the word, so a prompt ending on a bare
// space pushes the model toward digits and word fragments ("ritten", "0800").
// The model then emits " word"; trimLeadingBlanksAfter() drops that space
// against the real context.
inline std::string promptFor(const std::string &context) {
size_t end = context.size();
while (end > 0 && isBlank(context[end - 1])) --end;
return context.substr(0, end);
}

// The model's continuation from its raw streamed output. Some chat templates
// (e.g. Qwen3 in Ollama) return the assistant prefill in front of the
// continuation; that echo is cut off. While the stream is still inside the
// echo, nothing is shown yet.
inline std::string withoutPromptEcho(const std::string &raw, const std::string &prompt) {
if (prompt.empty()) return raw;
if (raw.size() >= prompt.size())
return raw.compare(0, prompt.size(), prompt) == 0 ? raw.substr(prompt.size()) : raw;
return prompt.compare(0, raw.size(), raw) == 0 ? std::string{} : raw;
}

// A suggestion's leading spaces/tabs when the context already ends in
// whitespace — accepting them would type a double space. Mid-word and
// after-word contexts keep the model's spacing as is.
inline void trimLeadingBlanksAfter(std::string &suggestion, const std::string &context) {
if (context.empty()) return;
const char last = context.back();
if (!isBlank(last) && last != '\n') return;
size_t n = 0;
while (n < suggestion.size() && isBlank(suggestion[n])) ++n;
suggestion.erase(0, n);
}

} // namespace caret_context
71 changes: 3 additions & 68 deletions fcitx5-engine/src/inference_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <curl/curl.h>
#include <algorithm>
#include <charconv>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <string_view>
#include <system_error>

#include "ollama_request.h"

static FILE *ic_logfile() {
static FILE *f = []() -> FILE* {
const char* home = std::getenv("HOME");
Expand All @@ -35,22 +36,6 @@ static constexpr char kBaseSystemPrompt[] =

// ── JSON helpers ──────────────────────────────────────────────────────────────

static std::string json_escape(std::string_view s) {
std::string out;
out.reserve(s.size());
for (char c : s) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default: out += c;
}
}
return out;
}

// Extract a JSON string value for `key` from a flat JSON object.
// Returns empty string if not found.
static std::string json_str(std::string_view json, std::string_view key) {
Expand Down Expand Up @@ -81,56 +66,6 @@ static std::string json_str(std::string_view json, std::string_view key) {
return result;
}

// ── Payload builder ───────────────────────────────────────────────────────────

static std::string build_payload(const InferenceConfig& cfg,
const std::string& context,
const std::string& system_prompt) {
std::string stop_arr = "[";
for (size_t i = 0; i < cfg.stop_tokens.size(); ++i) {
if (i) stop_arr += ',';
stop_arr += '"';
stop_arr += json_escape(cfg.stop_tokens[i]);
stop_arr += '"';
}
stop_arr += ']';

// std::to_chars is locale-independent — snprintf respects LC_NUMERIC and would
// emit "0,3" instead of "0.3" under European locales, breaking Ollama's JSON parser.
char temp_buf[32] = {}, top_p_buf[32] = {};
{
auto r = std::to_chars(temp_buf, temp_buf + sizeof(temp_buf) - 1,
static_cast<double>(cfg.temperature),
std::chars_format::general, 6);
*r.ptr = '\0';
}
{
auto r = std::to_chars(top_p_buf, top_p_buf + sizeof(top_p_buf) - 1,
static_cast<double>(cfg.top_p),
std::chars_format::general, 6);
*r.ptr = '\0';
}

// Assistant-prefill: context is placed in an incomplete assistant turn.
// The model continues its own text, bypassing the chat-response pattern that
// fires when conversational text appears in the user role.
std::string payload = std::string(R"({"model":")") + json_escape(cfg.model)
+ R"(","messages":[)"
+ R"({"role":"system","content":")" + json_escape(system_prompt) + R"("},)"
+ R"({"role":"assistant","content":")" + json_escape(context) + R"("})"
+ R"(],"stream":true,"options":{"num_predict":)"
+ std::to_string(cfg.num_predict)
+ R"(,"temperature":)" + temp_buf
+ R"(,"top_p":)" + top_p_buf
+ R"(,"stop":)" + stop_arr;

if (cfg.threads.has_value())
payload += R"(,"num_thread":)" + std::to_string(*cfg.threads);

payload += "}}";
return payload;
}

// ── CURL write callbacks ──────────────────────────────────────────────────────

struct WriteState {
Expand Down Expand Up @@ -289,7 +224,7 @@ void InferenceClient::execute(Req& req) {
prompt_snapshot = _system_prompt; // frozen for this request
}

std::string payload = build_payload(req.cfg, req.context, prompt_snapshot);
std::string payload = buildChatPayload(req.cfg, req.context, prompt_snapshot);
std::string url = req.cfg.ollama_host + "/api/chat";

WriteState ws(req.gen, _gen, req.on_token);
Expand Down
67 changes: 67 additions & 0 deletions fcitx5-engine/src/ollama_request.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "ollama_request.h"

#include <charconv>
#include <string_view>

namespace {

std::string json_escape(std::string_view s) {
std::string out;
out.reserve(s.size());
for (char c : s) {
switch (c) {
case '"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default: out += c;
}
}
return out;
}

// std::to_chars is locale-independent — snprintf respects LC_NUMERIC and would
// emit "0,3" instead of "0.3" under European locales, breaking Ollama's JSON parser.
std::string number(float v) {
char buf[32] = {};
auto r = std::to_chars(buf, buf + sizeof(buf) - 1, static_cast<double>(v),
std::chars_format::general, 6);
*r.ptr = '\0';
return buf;
}

std::string options(const InferenceConfig& cfg) {
std::string stop_arr = "[";
for (size_t i = 0; i < cfg.stop_tokens.size(); ++i) {
if (i) stop_arr += ',';
stop_arr += '"';
stop_arr += json_escape(cfg.stop_tokens[i]);
stop_arr += '"';
}
stop_arr += ']';

std::string out = R"("options":{"num_predict":)" + std::to_string(cfg.num_predict)
+ R"(,"temperature":)" + number(cfg.temperature)
+ R"(,"top_p":)" + number(cfg.top_p)
+ R"(,"stop":)" + stop_arr;
if (cfg.threads.has_value())
out += R"(,"num_thread":)" + std::to_string(*cfg.threads);
out += '}';
return out;
}

} // namespace

std::string buildChatPayload(const InferenceConfig& cfg,
const std::string& context,
const std::string& systemPrompt) {
// Assistant-prefill: context is placed in an incomplete assistant turn.
// The model continues its own text, bypassing the chat-response pattern that
// fires when conversational text appears in the user role.
return std::string(R"({"model":")") + json_escape(cfg.model)
+ R"(","messages":[)"
+ R"({"role":"system","content":")" + json_escape(systemPrompt) + R"("},)"
+ R"({"role":"assistant","content":")" + json_escape(context) + R"("})"
+ R"(],"stream":true,)" + options(cfg) + "}";
}
14 changes: 14 additions & 0 deletions fcitx5-engine/src/ollama_request.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

// Pure builder for the Ollama request body behind one suggestion: POST
// /api/chat with the system prompt and the typed text as an unfinished
// assistant turn (assistant-prefill), streamed back as
// {"message":{"content":"…"}} lines.

#include <string>

#include "config.h"

std::string buildChatPayload(const InferenceConfig& cfg,
const std::string& context,
const std::string& systemPrompt);
49 changes: 38 additions & 11 deletions fcitx5-engine/src/xtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include <fcitx/inputcontext.h>
#include <fcitx/inputpanel.h>
#include <fcitx/instance.h>
#include <fcitx/surroundingtext.h>
#include <fcitx/text.h>

#include "caret_context.h"
#include "config_loader.h"
#include "path_utils.h"
#include "prompt_builder.h"
Expand Down Expand Up @@ -375,7 +377,25 @@ void XTypeEngine::requestInference(
fcitx::TrackableObjectReference<fcitx::InputContext> icRef,
fcitx::InputContext *icPtr)
{
if (!icRef.isValid()) return;

// Prefer the app's own text before the caret: unlike the keystroke buffer
// it includes text that was there before focus, pastes, and caret moves.
// Fall back to the buffer when the app doesn't report surrounding text or
// its copy doesn't have the last keystroke yet.
auto ctx = _ctx.contextText();
const char *source = "typed";
if (icPtr->capabilityFlags().test(fcitx::CapabilityFlag::SurroundingText) &&
icPtr->surroundingText().isValid()) {
const auto &st = icPtr->surroundingText();
if (auto before = caret_context::beforeCaret(st.text(), st.cursor(),
caret_context::lastChar(ctx))) {
ctx = std::move(*before);
source = "surrounding";
} else {
source = "typed (surrounding stale)";
}
}
if (static_cast<int>(ctx.size()) < _cfg.inference.min_context_chars)
return;
if (static_cast<int>(ctx.size()) > _cfg.inference.context_window)
Expand All @@ -386,42 +406,48 @@ void XTypeEngine::requestInference(
++_metrics.suggestions_generated;
const uint64_t myGen = _gen;
const auto startUs = fcitx::now(CLOCK_MONOTONIC);
const std::string sentCtx = ctx; // the text being continued; used to tidy the output
std::string prompt = caret_context::promptFor(ctx);

InferenceConfig reqCfg = _cfg.inference;
if (auto* ov = currentAppOverride(); ov && ov->num_predict.has_value())
reqCfg.num_predict = *ov->num_predict;

dbg("requestInference ctx='%.40s...'", ctx.c_str());
dbg("requestInference src=%s len=%zu ctx='%.40s...'", source, ctx.size(), ctx.c_str());
// Raw model output so far; only touched on the main thread.
auto raw = std::make_shared<std::string>();
_inference.request(
std::move(ctx),
prompt,
std::move(reqCfg),
[this, myGen, icRef, icPtr](std::string token) {
[this, myGen, icRef, icPtr, sentCtx, prompt, raw](std::string token) {
dbg("token received: '%s' icValid=%d", token.c_str(), (int)icRef.isValid());
if (!icRef.isValid()) { dbg("on_token: icRef invalid — dropping"); return; }
_instance->eventDispatcher().scheduleWithContext(
icRef,
[this, myGen, tok = std::move(token), icPtr]() {
[this, myGen, tok = std::move(token), icPtr, sentCtx, prompt, raw]() {
if (myGen != _gen) return;
std::string current =
_ctx.hasSuggestion() ? *_ctx.suggestion() : "";
_ctx.setSuggestion(current + tok);
dbg("preedit updated: '%s'", _ctx.suggestion()->c_str());
*raw += tok;
std::string s = caret_context::withoutPromptEcho(*raw, prompt);
caret_context::trimLeadingBlanksAfter(s, sentCtx);
_ctx.setSuggestion(std::move(s));
if (_ctx.hasSuggestion())
dbg("preedit updated: '%s'", _ctx.suggestion()->c_str());
updatePreedit(icPtr);
});
},
[this, myGen, icRef, icPtr, startUs]() {
[this, myGen, icRef, icPtr, startUs, sentCtx]() {
dbg("inference done, myGen=%llu curGen=%llu", (unsigned long long)myGen, (unsigned long long)_gen);
_instance->eventDispatcher().scheduleWithContext(
icRef,
[this, myGen, icPtr, startUs]() {
[this, myGen, icPtr, startUs, sentCtx]() {
if (myGen != _gen) return;
if (_ctx.hasSuggestion()) {
std::string s = *_ctx.suggestion();
// Strip trailing whitespace.
while (!s.empty() &&
std::isspace(static_cast<unsigned char>(s.back())))
s.pop_back();
const std::string ctxText = _ctx.contextText();
const std::string &ctxText = sentCtx;
// Strip tail-of-context echo: model repeats recently typed text.
constexpr size_t kMaxCheck = 80;
size_t check = std::min({s.size(), ctxText.size(), kMaxCheck});
Expand All @@ -445,6 +471,7 @@ void XTypeEngine::requestInference(
if (lower(s.substr(0, kHeadCheck)) == lower(ctxText.substr(0, kHeadCheck)))
s.clear();
}
caret_context::trimLeadingBlanksAfter(s, ctxText);
_ctx.setSuggestion(std::move(s));
}
updatePreedit(icPtr);
Expand Down
Loading