Make applyChatTemplate a buffer-based C ABI for objcxx interop#7
Merged
Conversation
…nterop
The previous applyChatTemplate took a std::vector<ChatMessage> (a struct with
std::string members) and returned std::string. That compiles under the Cxx
interop mode this package's test target uses, but does NOT bridge into the
Cotabby app target, which uses objcxx interop — a returned std::string has no
usable Swift String initializer there, so the app could never call it.
Replace it with a detokenize-style C ABI that crosses the boundary cleanly:
int applyChatTemplate(const char* system_text, const char* user_text,
bool add_assistant, char* buffer, int buffer_size)
system + user are passed directly (autocomplete is always exactly those two
turns), matching how the app already calls tokenize/detokenize with const char*
and a caller buffer. Return contract: >0 bytes written, 0 = no model / no
template / render failure (caller falls back to raw), <0 = -(required size) so
the caller can resize and retry. Drops the ChatMessage struct entirely.
Tests updated to the new signature; swift test green (15 tests, 0 failures,
3 model-dependent skipped).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
applyChatTemplateshipped in #6 took astd::vector<ChatMessage>(a struct withstd::stringmembers) and returnedstd::string. That compiles under this package's test target (Cxx interop) but does not bridge into the Cotabby app target, which uses objcxx interop — a returnedstd::stringhas no usable SwiftStringinitializer there, so the app literally cannot call it.This replaces it with a
detokenize-style C ABI that crosses the boundary cleanly:system+userare passed directly (autocomplete is always exactly those two turns), matching how the app already callstokenize/detokenizewithconst char*+ a caller buffer. TheChatMessagestruct is removed.Return contract:
> 0— bytes written (<= buffer_size), the formatted prompt.0— no model, no chat template, or render failure → caller falls back to the raw prompt path.< 0—-(required size); buffer too small, caller resizes and retries.Validation
swift build— clean.swift test— Executed 15 tests, 0 failures (3 model-dependent tests skipped withoutCOTABBY_TEST_MODEL_PATH). The no-model guard test and the end-to-end chat-template assertion were updated to the new signature.Risk / rollout notes
applyChatTemplate/ChatMessagehad no working consumer (the app couldn't call them), so nothing is actually broken.hasChatTemplateandtokenizeWithOptionsare unchanged.