1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
const std = @import("std");
const conversation = @import("conversation.zig");
pub const ContentBlockType = enum {
Text,
Thinking,
ToolUse,
ToolResult,
};
pub const BlockMeta = struct {
/// Only populated for ToolUse blocks. Null for Text/Thinking.
tool_id: ?[]const u8 = null,
tool_name: ?[]const u8 = null,
};
pub const ReceiverVTable = struct {
onMessageStart: *const fn (*anyopaque, conversation.MessageRole) void,
onBlockStart: *const fn (*anyopaque, ContentBlockType, usize, ?BlockMeta) void,
onContentDelta: *const fn (*anyopaque, usize, []const u8) void,
onBlockComplete: *const fn (*anyopaque, usize, conversation.ContentBlock) void,
onMessageComplete: *const fn (*anyopaque, conversation.Message) void,
};
pub const Receiver = struct {
ptr: *anyopaque,
vtable: *const ReceiverVTable,
pub fn onMessageStart(self: Receiver, role: conversation.MessageRole) void {
self.vtable.onMessageStart(self.ptr, role);
}
pub fn onBlockStart(self: Receiver, block_type: ContentBlockType, index: usize, meta: ?BlockMeta) void {
self.vtable.onBlockStart(self.ptr, block_type, index, meta);
}
pub fn onContentDelta(self: Receiver, block_index: usize, delta: []const u8) void {
self.vtable.onContentDelta(self.ptr, block_index, delta);
}
pub fn onBlockComplete(self: Receiver, block_index: usize, block: conversation.ContentBlock) void {
self.vtable.onBlockComplete(self.ptr, block_index, block);
}
pub fn onMessageComplete(self: Receiver, message: conversation.Message) void {
self.vtable.onMessageComplete(self.ptr, message);
}
};
pub const ProviderVTable = struct {
streamStep: *const fn (*anyopaque, *conversation.Conversation, *Receiver) anyerror!void,
deinit: *const fn (*anyopaque) void,
};
pub const Provider = struct {
ptr: *anyopaque,
vtable: *const ProviderVTable,
pub fn streamStep(self: Provider, conv: *conversation.Conversation, receiver: *Receiver) anyerror!void {
return self.vtable.streamStep(self.ptr, conv, receiver);
}
pub fn deinit(self: Provider) void {
self.vtable.deinit(self.ptr);
}
};
|