Skip to content

Commit d8cada4

Browse files
nenad1002Your Name
andauthored
Add C++ SDK Support (#544)
- pipeline support missing, will come in future - ARM support will come soon --------- Co-authored-by: Your Name <you@example.com>
1 parent fd0d53e commit d8cada4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5640
-0
lines changed

sdk/cpp/.clang-format

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: Microsoft
4+
5+
# Match the existing project style
6+
Standard: c++17
7+
ColumnLimit: 120
8+
9+
# Indentation
10+
IndentWidth: 4
11+
TabWidth: 4
12+
UseTab: Never
13+
AccessModifierOffset: -4
14+
IndentCaseLabels: false
15+
NamespaceIndentation: All
16+
17+
# Braces
18+
BreakBeforeBraces: Custom
19+
BraceWrapping:
20+
AfterCaseLabel: false
21+
AfterClass: false
22+
AfterControlStatement: Never
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
26+
AfterStruct: false
27+
BeforeCatch: true
28+
BeforeElse: true
29+
IndentBraces: false
30+
31+
# Alignment
32+
AlignAfterOpenBracket: Align
33+
AlignOperands: Align
34+
AlignTrailingComments: true
35+
36+
# Includes
37+
SortIncludes: false
38+
IncludeBlocks: Preserve
39+
40+
# Misc
41+
AllowShortFunctionsOnASingleLine: Inline
42+
AllowShortIfStatementsOnASingleLine: Never
43+
AllowShortLoopsOnASingleLine: false
44+
AllowShortBlocksOnASingleLine: Empty
45+
PointerAlignment: Left
46+
SpaceAfterCStyleCast: false
47+
SpaceBeforeParens: ControlStatements

sdk/cpp/CMakeLists.txt

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
# VS hot reload policy (safe-guarded)
4+
if (POLICY CMP0141)
5+
cmake_policy(SET CMP0141 NEW)
6+
if (MSVC)
7+
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT
8+
"$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>")
9+
endif()
10+
endif()
11+
12+
project(CppSdk LANGUAGES CXX)
13+
14+
# -----------------------------
15+
# Windows-only + compiler guard
16+
# -----------------------------
17+
if (NOT WIN32)
18+
message(FATAL_ERROR "CppSdk is Windows-only for now (uses Win32/WIL headers).")
19+
endif()
20+
21+
# Accept MSVC OR clang-cl (Clang in MSVC compatibility mode).
22+
# VS CMake Open-Folder often uses clang-cl by default.
23+
if (NOT (MSVC OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")))
24+
message(STATUS "CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID}")
25+
message(STATUS "CMAKE_CXX_COMPILER = ${CMAKE_CXX_COMPILER}")
26+
message(STATUS "CMAKE_CXX_SIMULATE_ID = ${CMAKE_CXX_SIMULATE_ID}")
27+
message(FATAL_ERROR "Need MSVC or clang-cl (MSVC-compatible toolchain).")
28+
endif()
29+
30+
set(CMAKE_CXX_STANDARD 17)
31+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
32+
set(CMAKE_CXX_EXTENSIONS OFF)
33+
34+
# Optional: target Windows 10+ APIs (adjust if you need older)
35+
add_compile_definitions(_WIN32_WINNT=0x0A00 WINVER=0x0A00)
36+
37+
# -----------------------------
38+
# Dependencies (installed via vcpkg)
39+
# -----------------------------
40+
find_package(nlohmann_json CONFIG REQUIRED)
41+
find_package(wil CONFIG REQUIRED)
42+
find_package(Microsoft.GSL CONFIG REQUIRED)
43+
option(BUILD_TESTING "Build unit and end-to-end tests" ON)
44+
if (BUILD_TESTING)
45+
find_package(GTest CONFIG REQUIRED)
46+
endif()
47+
48+
# -----------------------------
49+
# SDK library (STATIC)
50+
# List ONLY .cpp files here.
51+
# -----------------------------
52+
add_library(CppSdk STATIC
53+
src/model.cpp
54+
src/catalog.cpp
55+
src/openai_chat_client.cpp
56+
src/openai_audio_client.cpp
57+
src/foundry_local_manager.cpp
58+
)
59+
60+
target_include_directories(CppSdk
61+
PUBLIC
62+
${CMAKE_CURRENT_SOURCE_DIR}/include
63+
PRIVATE
64+
${CMAKE_CURRENT_SOURCE_DIR}/src
65+
)
66+
67+
target_link_libraries(CppSdk
68+
PUBLIC
69+
nlohmann_json::nlohmann_json
70+
Microsoft.GSL::GSL
71+
WIL::WIL
72+
)
73+
74+
# -----------------------------
75+
# Sample executable
76+
# -----------------------------
77+
add_executable(CppSdkSample
78+
sample/main.cpp
79+
)
80+
81+
target_link_libraries(CppSdkSample PRIVATE CppSdk)
82+
83+
# -----------------------------
84+
# Unit tests
85+
# -----------------------------
86+
if (BUILD_TESTING)
87+
enable_testing()
88+
89+
add_executable(CppSdkTests
90+
test/parser_and_types_test.cpp
91+
test/model_variant_test.cpp
92+
test/catalog_test.cpp
93+
test/client_test.cpp
94+
)
95+
96+
target_include_directories(CppSdkTests
97+
PRIVATE
98+
${CMAKE_CURRENT_SOURCE_DIR}/test
99+
${CMAKE_CURRENT_SOURCE_DIR}/src
100+
)
101+
102+
target_compile_definitions(CppSdkTests PRIVATE FL_TESTS)
103+
104+
target_link_libraries(CppSdkTests
105+
PRIVATE
106+
CppSdk
107+
GTest::gtest_main
108+
)
109+
110+
# Copy testdata files next to the test executable so file-based tests can find them.
111+
add_custom_command(TARGET CppSdkTests POST_BUILD
112+
COMMAND ${CMAKE_COMMAND} -E copy_directory
113+
${CMAKE_CURRENT_SOURCE_DIR}/test/testdata
114+
$<TARGET_FILE_DIR:CppSdkTests>/testdata
115+
)
116+
117+
include(GoogleTest)
118+
gtest_discover_tests(CppSdkTests
119+
WORKING_DIRECTORY $<TARGET_FILE_DIR:CppSdkTests>
120+
)
121+
122+
# -----------------------------
123+
# End-to-end tests (separate executable, requires Core DLL)
124+
# Exercises the full public API against the real catalog.
125+
# Tests that need model download are DISABLED by default;
126+
# run with --gtest_also_run_disabled_tests locally.
127+
# -----------------------------
128+
add_executable(CppSdkE2ETests
129+
test/e2e_test.cpp
130+
)
131+
132+
target_include_directories(CppSdkE2ETests
133+
PRIVATE
134+
${CMAKE_CURRENT_SOURCE_DIR}/test
135+
${CMAKE_CURRENT_SOURCE_DIR}/src
136+
)
137+
138+
target_link_libraries(CppSdkE2ETests
139+
PRIVATE
140+
CppSdk
141+
GTest::gtest_main
142+
)
143+
144+
gtest_discover_tests(CppSdkE2ETests
145+
WORKING_DIRECTORY $<TARGET_FILE_DIR:CppSdkE2ETests>
146+
)
147+
endif()
148+
149+
# Make Visual Studio start/debug this target by default
150+
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
151+
PROPERTY VS_STARTUP_PROJECT CppSdkSample)

sdk/cpp/CMakePresets.json

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"version": 6,
3+
"configurePresets": [
4+
{
5+
"name": "windows-base",
6+
"hidden": true,
7+
"generator": "Ninja",
8+
"binaryDir": "${sourceDir}/out/build/${presetName}",
9+
"installDir": "${sourceDir}/out/install/${presetName}",
10+
"toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
11+
"cacheVariables": {
12+
"CMAKE_C_COMPILER": "cl.exe",
13+
"CMAKE_CXX_COMPILER": "cl.exe",
14+
"VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/triplets"
15+
},
16+
"condition": {
17+
"type": "equals",
18+
"lhs": "${hostSystemName}",
19+
"rhs": "Windows"
20+
}
21+
},
22+
{
23+
"name": "x64-debug",
24+
"displayName": "MSVC x64 Debug",
25+
"inherits": "windows-base",
26+
"architecture": {
27+
"value": "x64",
28+
"strategy": "external"
29+
},
30+
"cacheVariables": {
31+
"CMAKE_BUILD_TYPE": "Debug",
32+
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md"
33+
}
34+
},
35+
{
36+
"name": "x64-release",
37+
"displayName": "MSVC x64 Release",
38+
"inherits": "windows-base",
39+
"architecture": {
40+
"value": "x64",
41+
"strategy": "external"
42+
},
43+
"cacheVariables": {
44+
"CMAKE_BUILD_TYPE": "Release",
45+
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md"
46+
}
47+
},
48+
{
49+
"name": "linux-debug",
50+
"displayName": "Linux Debug",
51+
"generator": "Ninja",
52+
"binaryDir": "${sourceDir}/out/build/${presetName}",
53+
"installDir": "${sourceDir}/out/install/${presetName}",
54+
"cacheVariables": {
55+
"CMAKE_BUILD_TYPE": "Debug"
56+
},
57+
"condition": {
58+
"type": "equals",
59+
"lhs": "${hostSystemName}",
60+
"rhs": "Linux"
61+
}
62+
},
63+
{
64+
"name": "macos-debug",
65+
"displayName": "macOS Debug",
66+
"generator": "Ninja",
67+
"binaryDir": "${sourceDir}/out/build/${presetName}",
68+
"installDir": "${sourceDir}/out/install/${presetName}",
69+
"cacheVariables": {
70+
"CMAKE_BUILD_TYPE": "Debug"
71+
},
72+
"condition": {
73+
"type": "equals",
74+
"lhs": "${hostSystemName}",
75+
"rhs": "Darwin"
76+
}
77+
}
78+
],
79+
"buildPresets": [
80+
{
81+
"name": "x64-debug",
82+
"configurePreset": "x64-debug",
83+
"displayName": "MSVC x64 Debug Build"
84+
},
85+
{
86+
"name": "x64-release",
87+
"configurePreset": "x64-release",
88+
"displayName": "MSVC x64 Release Build"
89+
}
90+
],
91+
"testPresets": [
92+
{
93+
"name": "x64-debug",
94+
"configurePreset": "x64-debug",
95+
"displayName": "MSVC x64 Debug Tests",
96+
"output": {
97+
"outputOnFailure": true
98+
}
99+
},
100+
{
101+
"name": "x64-release",
102+
"configurePreset": "x64-release",
103+
"displayName": "MSVC x64 Release Tests",
104+
"output": {
105+
"outputOnFailure": true
106+
}
107+
}
108+
]
109+
}

sdk/cpp/include/catalog.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
#include <unordered_map>
9+
#include <chrono>
10+
#include <memory>
11+
#include <mutex>
12+
13+
#include <gsl/pointers>
14+
#include <gsl/span>
15+
16+
#include "model.h"
17+
18+
namespace foundry_local::Internal {
19+
struct IFoundryLocalCore;
20+
}
21+
22+
namespace foundry_local {
23+
24+
class Catalog final {
25+
public:
26+
Catalog(const Catalog&) = delete;
27+
Catalog& operator=(const Catalog&) = delete;
28+
Catalog(Catalog&&) = delete;
29+
Catalog& operator=(Catalog&&) = delete;
30+
31+
explicit Catalog(gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> injected,
32+
gsl::not_null<ILogger*> logger);
33+
34+
static std::unique_ptr<Catalog> Create(gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core,
35+
gsl::not_null<ILogger*> logger) {
36+
return std::make_unique<Catalog>(core, logger);
37+
}
38+
39+
const std::string& GetName() const { return name_; }
40+
std::vector<IModel*> ListModels() const;
41+
std::vector<IModel*> GetLoadedModels() const;
42+
std::vector<IModel*> GetCachedModels() const;
43+
44+
IModel* GetModel(std::string_view modelId) const;
45+
IModel* GetModelVariant(std::string_view modelVariantId) const;
46+
IModel& GetLatestVersion(const IModel& modelOrModelVariant) const;
47+
48+
private:
49+
struct CatalogState {
50+
std::unordered_map<std::string, Model> byAlias;
51+
std::unordered_map<std::string, ModelVariant*> modelIdToModelVariant;
52+
std::chrono::steady_clock::time_point lastFetch{};
53+
};
54+
55+
void UpdateModels() const;
56+
std::shared_ptr<const CatalogState> GetState() const;
57+
58+
mutable std::mutex mutex_;
59+
mutable std::shared_ptr<const CatalogState> state_;
60+
61+
gsl::not_null<foundry_local::Internal::IFoundryLocalCore*> core_;
62+
std::string name_;
63+
gsl::not_null<ILogger*> logger_;
64+
};
65+
66+
} // namespace foundry_local

0 commit comments

Comments
 (0)