diff options
Diffstat (limited to 'libpanto/src/provider.zig')
| -rw-r--r-- | libpanto/src/provider.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libpanto/src/provider.zig b/libpanto/src/provider.zig index 16c3dd6..6d97bb7 100644 --- a/libpanto/src/provider.zig +++ b/libpanto/src/provider.zig @@ -1,5 +1,6 @@ const std = @import("std"); const conversation = @import("conversation.zig"); +const config_mod = @import("config.zig"); pub const ContentBlockType = enum { Text, @@ -73,6 +74,35 @@ pub const Provider = struct { ptr: *anyopaque, vtable: *const ProviderVTable, + /// Construct the concrete provider implementation that matches `cfg`'s + /// API style, heap-allocate it, and return a `Provider` interface bound + /// to it. `deinit` tears down the impl and frees the allocation. + /// + /// The concrete provider types (`OpenAIChatProvider`, etc.) are + /// implementation details; callers interact only with this interface. + pub fn init( + allocator: std.mem.Allocator, + io: std.Io, + cfg: config_mod.Config, + ) !Provider { + // Imported lazily to break the circular module graph: + // provider.zig <- provider_openai_chat.zig <- provider.zig. + const provider_openai_chat = @import("provider_openai_chat.zig"); + const provider_anthropic_messages = @import("provider_anthropic_messages.zig"); + switch (cfg) { + .openai_chat => |c| { + const impl = try allocator.create(provider_openai_chat.OpenAIChatProvider); + impl.* = provider_openai_chat.OpenAIChatProvider.init(allocator, io, c); + return impl.provider(); + }, + .anthropic_messages => |c| { + const impl = try allocator.create(provider_anthropic_messages.AnthropicMessagesProvider); + impl.* = provider_anthropic_messages.AnthropicMessagesProvider.init(allocator, io, c); + return impl.provider(); + }, + } + } + pub fn streamStep(self: Provider, conv: *conversation.Conversation, receiver: *Receiver) anyerror!void { return self.vtable.streamStep(self.ptr, conv, receiver); } |
