|
| 1 | +const schema = @import("schema.zig"); |
| 2 | +const extism = @import("extism-pdk"); |
| 3 | +const http = extism.http; |
| 4 | +const plugin = @import("pdk.zig")._plugin; |
| 5 | +const std = @import("std"); |
| 6 | +const json = std.json; |
| 7 | +const eql = std.mem.eql; |
| 8 | +const allocPrint = std.fmt.allocPrint; |
| 9 | +const allocator = std.heap.wasm_allocator; |
| 10 | + |
| 11 | +/// Called when the tool is invoked. |
| 12 | +/// If you support multiple tools, you must switch on the input.params.name to detect which tool is being called. |
| 13 | +/// It takes CallToolRequest as input (The incoming tool request from the LLM) |
| 14 | +/// And returns CallToolResult (The servlet's response to the given tool call) |
| 15 | +pub fn call(input: schema.CallToolRequest) !schema.CallToolResult { |
| 16 | + const name = input.params.name; |
| 17 | + if (eql(u8, name, "photos")) { |
| 18 | + return getPhotos(input.params.arguments); |
| 19 | + } else if (eql(u8, name, "photos_id")) { |
| 20 | + return getPhotosId(input.params.arguments); |
| 21 | + } else if (eql(u8, name, "search_photos")) { |
| 22 | + return searchPhotos(input.params.arguments); |
| 23 | + } |
| 24 | + return error.PluginFunctionNotImplemented; |
| 25 | +} |
| 26 | + |
| 27 | +fn getPhotos(args: ?json.ArrayHashMap(std.json.Value)) !schema.CallToolResult { |
| 28 | + const apiKey = try plugin.getConfig("API_KEY") orelse return error.MissingConfig; |
| 29 | + const page = args.?.map.get("page") orelse json.Value{ .integer = 1 }; |
| 30 | + const per_page = args.?.map.get("per_page") orelse json.Value{ .integer = 10 }; |
| 31 | + const url = try allocPrint( |
| 32 | + allocator, |
| 33 | + "https://api.unsplash.com/photos?page={d}&per_page={d}", |
| 34 | + .{ page.integer, per_page.integer }, |
| 35 | + ); |
| 36 | + var req = http.HttpRequest.init("GET", url); |
| 37 | + defer req.deinit(allocator); |
| 38 | + try req.setHeader( |
| 39 | + allocator, |
| 40 | + "Authorization", |
| 41 | + try allocPrint(allocator, "Client-ID {s}", .{apiKey}), |
| 42 | + ); |
| 43 | + const resp = try plugin.request(req, null); |
| 44 | + const body = try resp.body(allocator); |
| 45 | + if (resp.status != 200) { |
| 46 | + return callToolError(try allocPrint( |
| 47 | + allocator, |
| 48 | + "Error {d}: {s}", |
| 49 | + .{ resp.status, body }, |
| 50 | + )); |
| 51 | + } |
| 52 | + return schema.CallToolResult{ |
| 53 | + .content = try allocator.dupe(schema.Content, &.{.{ |
| 54 | + .type = schema.ContentType.text, |
| 55 | + .text = body, |
| 56 | + }}), |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +fn getPhotosId(arguments: ?json.ArrayHashMap(std.json.Value)) !schema.CallToolResult { |
| 61 | + const apiKey = try plugin.getConfig("API_KEY") orelse return error.MissingConfig; |
| 62 | + const args = arguments orelse return callToolError("missing arguments"); |
| 63 | + const id = args.map.get("id") orelse return error.MissingArgument; |
| 64 | + const url = try allocPrint( |
| 65 | + allocator, |
| 66 | + "https://api.unsplash.com/photos/{s}", |
| 67 | + .{id.string}, |
| 68 | + ); |
| 69 | + var req = http.HttpRequest.init("GET", url); |
| 70 | + defer req.deinit(allocator); |
| 71 | + try req.setHeader( |
| 72 | + allocator, |
| 73 | + "Authorization", |
| 74 | + try allocPrint(allocator, "Client-ID {s}", .{apiKey}), |
| 75 | + ); |
| 76 | + const resp = try plugin.request(req, null); |
| 77 | + const body = try resp.body(allocator); |
| 78 | + if (resp.status != 200) { |
| 79 | + return callToolError(try allocPrint( |
| 80 | + allocator, |
| 81 | + "Error {d}: {s}", |
| 82 | + .{ resp.status, body }, |
| 83 | + )); |
| 84 | + } |
| 85 | + return schema.CallToolResult{ |
| 86 | + .content = try allocator.dupe(schema.Content, &.{.{ |
| 87 | + .type = schema.ContentType.text, |
| 88 | + .text = body, |
| 89 | + }}), |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +// query Search terms. |
| 94 | +// page Page number to retrieve. (Optional; default: 1) |
| 95 | +// per_page Number of items per page. (Optional; default: 10) |
| 96 | +// order_by How to sort the photos. (Optional; default: relevant). Valid values are latest and relevant. |
| 97 | +// collections Collection ID(‘s) to narrow search. Optional. If multiple, comma-separated. |
| 98 | +// content_filter Limit results by content safety. (Optional; default: low). Valid values are low and high. |
| 99 | +// color Filter results by color. Optional. Valid values are: black_and_white, black, white, yellow, orange, red, purple, magenta, green, teal, and blue. |
| 100 | +// orientation Filter by photo orientation. Optional. (Valid values: landscape, portrait, squarish) |
| 101 | + |
| 102 | +fn searchPhotos(arguments: ?json.ArrayHashMap(std.json.Value)) !schema.CallToolResult { |
| 103 | + const apiKey = try plugin.getConfig("API_KEY") orelse return error.MissingConfig; |
| 104 | + const args = arguments orelse return callToolError("missing arguments"); |
| 105 | + const query = args.map.get("query") orelse return callToolError("missing query"); |
| 106 | + const page = args.map.get("page") orelse json.Value{ .integer = 1 }; |
| 107 | + const per_page = args.map.get("per_page") orelse json.Value{ .integer = 10 }; |
| 108 | + const order_by = args.map.get("order_by") orelse json.Value{ .string = "relevant" }; |
| 109 | + const content_filter = args.map.get("content_filter") orelse json.Value{ .string = "low" }; |
| 110 | + var color: []u8 = ""; |
| 111 | + if (args.map.get("color") != null) { |
| 112 | + const c = args.map.get("color").?.string; |
| 113 | + color = try allocPrint(allocator, "&color={s}", .{c}); |
| 114 | + } |
| 115 | + var orientation: []u8 = ""; |
| 116 | + if (args.map.get("orientation") != null) { |
| 117 | + const c = args.map.get("orientation").?.string; |
| 118 | + orientation = try allocPrint(allocator, "&orientation={s}", .{c}); |
| 119 | + } |
| 120 | + const url = try allocPrint( |
| 121 | + allocator, |
| 122 | + "https://api.unsplash.com/search/photos?page={d}&per_page={d}&order_by={s}&content_filter={s}{s}{s}&query={s}", |
| 123 | + .{ page.integer, per_page.integer, order_by.string, content_filter.string, color, orientation, query.string }, |
| 124 | + ); |
| 125 | + var req = http.HttpRequest.init("GET", url); |
| 126 | + defer req.deinit(allocator); |
| 127 | + try req.setHeader( |
| 128 | + allocator, |
| 129 | + "Authorization", |
| 130 | + try allocPrint(allocator, "Client-ID {s}", .{apiKey}), |
| 131 | + ); |
| 132 | + const resp = try plugin.request(req, null); |
| 133 | + const body = try resp.body(allocator); |
| 134 | + if (resp.status != 200) { |
| 135 | + return callToolError(try allocPrint( |
| 136 | + allocator, |
| 137 | + "Error {d}: {s}", |
| 138 | + .{ resp.status, body }, |
| 139 | + )); |
| 140 | + } |
| 141 | + return schema.CallToolResult{ |
| 142 | + .content = try allocator.dupe(schema.Content, &.{.{ |
| 143 | + .type = schema.ContentType.text, |
| 144 | + .text = body, |
| 145 | + }}), |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | +fn callToolError(msg: []const u8) !schema.CallToolResult { |
| 150 | + return schema.CallToolResult{ |
| 151 | + .isError = true, |
| 152 | + .content = try allocator.dupe(schema.Content, &.{.{ |
| 153 | + .type = schema.ContentType.text, |
| 154 | + .text = msg, |
| 155 | + }}), |
| 156 | + }; |
| 157 | +} |
| 158 | + |
| 159 | +pub fn describe() !schema.ListToolsResult { |
| 160 | + const tools = @embedFile("tools.json"); |
| 161 | + const r = try json.parseFromSlice(schema.ListToolsResult, allocator, tools, .{}); |
| 162 | + return r.value; |
| 163 | +} |
0 commit comments