@yaklabs-ai/plugin-sdk 0.13.4 → 0.13.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/kay-agent.js CHANGED
@@ -1,3 +1,197 @@
1
+ // packages/herd-uri/dist/claims.js
2
+ var SCHEME_RE = /^([a-z][a-z0-9+.-]*):\/\//i;
3
+ var CAPTURE_RE = /^[A-Za-z_][A-Za-z0-9_]*$/;
4
+ var REST_CAPTURE_RE = /^:([A-Za-z_][A-Za-z0-9_]*)\*$/;
5
+ var CONSTRAINED_CAPTURE_RE = /^([A-Za-z_][A-Za-z0-9_]*)(?:\((.*)\))?$/;
6
+ var MAX_CAPTURE_CONSTRAINT_LENGTH = 64;
7
+ function compileUriClaim(claim) {
8
+ if (claim.kind === "scheme") {
9
+ const scheme = normalizeProtocol(claim.scheme);
10
+ return {
11
+ claim,
12
+ match(input) {
13
+ const actual = SCHEME_RE.exec(input.trim())?.[1]?.toLowerCase();
14
+ if (actual !== scheme)
15
+ return void 0;
16
+ return { claimKind: claim.kind };
17
+ }
18
+ };
19
+ }
20
+ const pattern = compileRoutePattern(claim.pattern);
21
+ return {
22
+ claim,
23
+ match(input) {
24
+ const parsed = parsePatternInput(input);
25
+ if (parsed === void 0)
26
+ return void 0;
27
+ const captures = matchRoute(pattern, parsed);
28
+ if (captures === void 0)
29
+ return void 0;
30
+ return {
31
+ claimKind: claim.kind,
32
+ ...Object.keys(captures).length > 0 ? { captures } : {},
33
+ ...claim.normalizeTo !== void 0 ? {
34
+ normalizedUri: withInputSearch(claim, pattern, parsed, interpolateNormalizeTo(claim.normalizeTo, captures, pattern.restCaptures ?? []))
35
+ } : {}
36
+ };
37
+ }
38
+ };
39
+ }
40
+ function normalizeProtocol(value) {
41
+ return value.replace(/:$/, "").toLowerCase();
42
+ }
43
+ function parsePatternInput(input) {
44
+ try {
45
+ const url = new URL(input.trim());
46
+ return {
47
+ protocol: normalizeProtocol(url.protocol),
48
+ hostname: url.hostname.toLowerCase(),
49
+ pathname: url.pathname,
50
+ search: url.search.startsWith("?") ? url.search.slice(1) : url.search
51
+ };
52
+ } catch {
53
+ return void 0;
54
+ }
55
+ }
56
+ function compileRoutePattern(pattern) {
57
+ const pathname = pattern.pathname !== void 0 ? compileParts(pattern.pathname, "/", { allowRest: true }) : void 0;
58
+ return {
59
+ ...pattern.protocol !== void 0 ? { protocol: normalizeProtocol(pattern.protocol) } : {},
60
+ ...pattern.hostname !== void 0 ? { hostname: compileParts(pattern.hostname.toLowerCase(), ".") } : {},
61
+ ...pathname !== void 0 ? { pathname } : {},
62
+ ...pathname !== void 0 ? {
63
+ restCaptures: pathname.filter((part) => part.rest === true).map((part) => part.capture)
64
+ } : {},
65
+ ...pattern.search !== void 0 ? { search: compileParts(pattern.search.replace(/^\?/, ""), "&") } : {}
66
+ };
67
+ }
68
+ function compileParts(value, separator, options = {}) {
69
+ if (value === "")
70
+ return [];
71
+ const parts = value.split(separator);
72
+ return parts.map((part, index) => {
73
+ const restMatch = REST_CAPTURE_RE.exec(part);
74
+ if (restMatch !== null) {
75
+ if (options.allowRest !== true || index !== parts.length - 1) {
76
+ throw new Error("URI claim rest captures must be final pathname segments.");
77
+ }
78
+ return { capture: restMatch[1], rest: true };
79
+ }
80
+ if (part.startsWith(":")) {
81
+ const rawCapture = part.slice(1);
82
+ const match = CONSTRAINED_CAPTURE_RE.exec(rawCapture);
83
+ if (match === null) {
84
+ throw new Error(`Invalid URI claim capture ':${rawCapture}'.`);
85
+ }
86
+ const capture = match[1];
87
+ if (!CAPTURE_RE.test(capture)) {
88
+ throw new Error(`Invalid URI claim capture ':${rawCapture}'.`);
89
+ }
90
+ const constraint = match[2];
91
+ if (constraint !== void 0)
92
+ assertSupportedConstraint(constraint);
93
+ return {
94
+ capture,
95
+ ...constraint !== void 0 ? { constraint: new RegExp(`^(?:${constraint})$`) } : {}
96
+ };
97
+ }
98
+ return { literal: part };
99
+ });
100
+ }
101
+ function assertSupportedConstraint(constraint) {
102
+ if (constraint.length > MAX_CAPTURE_CONSTRAINT_LENGTH || /[()|]/.test(constraint)) {
103
+ throw new Error("URI claim capture constraints must be short segment regexes without grouping or alternation.");
104
+ }
105
+ }
106
+ function matchRoute(pattern, input) {
107
+ if (pattern.protocol !== void 0 && pattern.protocol !== input.protocol) {
108
+ return void 0;
109
+ }
110
+ const captures = {};
111
+ if (pattern.hostname !== void 0 && !matchParts(pattern.hostname, input.hostname, ".", captures)) {
112
+ return void 0;
113
+ }
114
+ if (pattern.pathname !== void 0 && !matchParts(pattern.pathname, input.pathname, "/", captures)) {
115
+ return void 0;
116
+ }
117
+ if (pattern.search !== void 0 && !matchParts(pattern.search, input.search, "&", captures)) {
118
+ return void 0;
119
+ }
120
+ return captures;
121
+ }
122
+ function matchParts(pattern, value, separator, captures) {
123
+ const parts = value.split(separator);
124
+ let partIndex = 0;
125
+ for (let i = 0; i < pattern.length; i += 1) {
126
+ const expected = pattern[i];
127
+ if (expected.rest === true) {
128
+ const remaining = parts.slice(partIndex);
129
+ if (remaining.every((part) => part === ""))
130
+ return false;
131
+ captures[expected.capture] = remaining.join(separator);
132
+ return true;
133
+ }
134
+ if (partIndex >= parts.length)
135
+ return false;
136
+ const actual = parts[partIndex];
137
+ if (expected.capture !== void 0) {
138
+ const decoded = safeDecode(actual);
139
+ if (expected.constraint !== void 0 && !expected.constraint.test(decoded)) {
140
+ return false;
141
+ }
142
+ captures[expected.capture] = decoded;
143
+ partIndex += 1;
144
+ continue;
145
+ }
146
+ if (expected.literal !== actual)
147
+ return false;
148
+ partIndex += 1;
149
+ }
150
+ return partIndex === parts.length;
151
+ }
152
+ function safeDecode(value) {
153
+ try {
154
+ return decodeURIComponent(value);
155
+ } catch {
156
+ return value;
157
+ }
158
+ }
159
+ function withInputSearch(claim, pattern, input, normalized) {
160
+ if (claim.kind !== "uriPattern" || claim.normalizeTo === void 0 || pattern.search !== void 0 || input.search.length === 0 || claim.normalizeTo.includes("?") || templateProtocol(claim.normalizeTo) !== input.protocol) {
161
+ return normalized;
162
+ }
163
+ return `${normalized}?${input.search}`;
164
+ }
165
+ function templateProtocol(template) {
166
+ return SCHEME_RE.exec(template)?.[1]?.toLowerCase();
167
+ }
168
+ function interpolateNormalizeTo(template, captures, restCaptures) {
169
+ return template.replace(/:([A-Za-z_][A-Za-z0-9_]*)/g, (full, name, offset) => {
170
+ const value = captures[name];
171
+ if (value === void 0)
172
+ return full;
173
+ return encodeCaptureValue({
174
+ template,
175
+ offset,
176
+ value,
177
+ rest: restCaptures.includes(name)
178
+ });
179
+ });
180
+ }
181
+ function encodeCaptureValue({ template, offset, value, rest }) {
182
+ const normalized = isAuthorityCapture(template, offset) ? value.toLowerCase() : value;
183
+ if (!rest)
184
+ return encodeURIComponent(normalized);
185
+ return normalized.split("/").join("/");
186
+ }
187
+ function isAuthorityCapture(template, offset) {
188
+ const prefix = template.slice(0, offset);
189
+ const schemeIndex = prefix.lastIndexOf("://");
190
+ if (schemeIndex === -1)
191
+ return false;
192
+ return !/[/?#]/.test(prefix.slice(schemeIndex + 3));
193
+ }
194
+
1
195
  // packages/herd-uri/dist/errors.js
2
196
  var UriError = class extends Error {
3
197
  };
@@ -32,6 +226,8 @@ var UriProviderError = class extends UriError {
32
226
  retryable;
33
227
  /** Provider-directed delay before retrying, in milliseconds. */
34
228
  retryAfterMs;
229
+ /** See {@link UriProviderErrorOptions.effect}. */
230
+ effect;
35
231
  constructor(options) {
36
232
  super(options.message);
37
233
  this.name = "UriProviderError";
@@ -50,6 +246,8 @@ var UriProviderError = class extends UriError {
50
246
  }
51
247
  this.retryAfterMs = options.retryAfterMs;
52
248
  }
249
+ if (options.effect !== void 0)
250
+ this.effect = options.effect;
53
251
  }
54
252
  };
55
253
  var UriNotFoundError = class extends UriError {
@@ -138,10 +336,10 @@ function isProducedResourceDeclaration(value) {
138
336
  // packages/herd-uri/dist/template-route.js
139
337
  var PLACEHOLDER_RE = /<([^<>]+)>/g;
140
338
  var REST_SUFFIX = "...";
141
- var SCHEME_RE = /^([a-zA-Z][a-zA-Z0-9+.-]*):(?:\/\/)?(.*)$/s;
339
+ var SCHEME_RE2 = /^([a-zA-Z][a-zA-Z0-9+.-]*):(?:\/\/)?(.*)$/s;
142
340
  function parseRoute(address) {
143
341
  const trimmed = address.trim();
144
- const match = SCHEME_RE.exec(trimmed);
342
+ const match = SCHEME_RE2.exec(trimmed);
145
343
  const scheme = match?.[1]?.toLowerCase() ?? "";
146
344
  const rest = match?.[2] ?? trimmed;
147
345
  const [rawPath = "", rawQuery] = splitQuery(rest);
@@ -784,7 +982,79 @@ function observationFromResource(resource) {
784
982
  return value;
785
983
  }
786
984
 
787
- // packages/herd-agent/dist/core/extensions/auth/scheme.js
985
+ // packages/plugin-primitives/dist/results.js
986
+ function textResult(text, details) {
987
+ const content = [{ type: "text", text }];
988
+ return { content, details };
989
+ }
990
+ function contentResult(content, details) {
991
+ return { content: [...content], details };
992
+ }
993
+
994
+ // packages/plugin-primitives/dist/transfer.js
995
+ var MAX_BYTES2 = 50 * 1024 * 1024;
996
+ async function readBodyWithByteCap(response, maxBytes) {
997
+ const reader = response.body?.getReader();
998
+ if (!reader)
999
+ return void 0;
1000
+ const chunks = [];
1001
+ let totalSize = 0;
1002
+ let capped = false;
1003
+ while (true) {
1004
+ const { done, value } = await reader.read();
1005
+ if (done)
1006
+ break;
1007
+ if (value) {
1008
+ chunks.push(value);
1009
+ totalSize += value.length;
1010
+ if (totalSize > maxBytes) {
1011
+ capped = true;
1012
+ await reader.cancel();
1013
+ break;
1014
+ }
1015
+ }
1016
+ }
1017
+ const bodyBuffer = Buffer.concat(chunks);
1018
+ const bytes = new Uint8Array(bodyBuffer.buffer, bodyBuffer.byteOffset, bodyBuffer.byteLength);
1019
+ return { bytes, capped };
1020
+ }
1021
+
1022
+ // packages/plugin-primitives/dist/tool-parameters.js
1023
+ import { z as z5 } from "zod";
1024
+ var URI_PAYLOAD_MARKER = "x-kay-uri-payload";
1025
+ var URI_PAYLOAD_WIRE_TYPES = [
1026
+ "string",
1027
+ "object",
1028
+ "array",
1029
+ "number",
1030
+ "boolean",
1031
+ "null"
1032
+ ];
1033
+ var uriPayloadSchema = z5.union([
1034
+ z5.string(),
1035
+ z5.record(z5.string(), z5.unknown()),
1036
+ z5.array(z5.unknown()),
1037
+ z5.number(),
1038
+ z5.boolean(),
1039
+ z5.null()
1040
+ ]).meta({ [URI_PAYLOAD_MARKER]: true });
1041
+ function toToolParameters(schema) {
1042
+ const jsonSchema = z5.toJSONSchema(schema, {
1043
+ io: "input",
1044
+ override(ctx) {
1045
+ const node = ctx.jsonSchema;
1046
+ if (node[URI_PAYLOAD_MARKER] === true) {
1047
+ delete node[URI_PAYLOAD_MARKER];
1048
+ delete node["anyOf"];
1049
+ node["type"] = [...URI_PAYLOAD_WIRE_TYPES];
1050
+ }
1051
+ }
1052
+ });
1053
+ const { $schema: _schema, $id: _id, ...rest } = jsonSchema;
1054
+ return rest;
1055
+ }
1056
+
1057
+ // packages/plugin-primitives/dist/auth-scheme.js
788
1058
  var ACCOUNT_ID_PARAM = "accountId";
789
1059
  function authContextOf(ctx) {
790
1060
  return ctx.auth;
@@ -845,7 +1115,7 @@ function withResolvedAuth(definition, binding, body) {
845
1115
  };
846
1116
  }
847
1117
 
848
- // packages/herd-agent/dist/core/web/search/types.js
1118
+ // packages/plugin-primitives/dist/search-provider.js
849
1119
  var SearchProviderError = class extends Error {
850
1120
  provider;
851
1121
  status;
@@ -856,8 +1126,6 @@ var SearchProviderError = class extends Error {
856
1126
  this.name = "SearchProviderError";
857
1127
  }
858
1128
  };
859
-
860
- // packages/herd-agent/dist/core/web/search/with-resolved-search-provider.js
861
1129
  function withResolvedSearchProvider(definition, binding, body) {
862
1130
  return {
863
1131
  id: definition.id,
@@ -885,47 +1153,12 @@ function withResolvedSearchProvider(definition, binding, body) {
885
1153
  };
886
1154
  }
887
1155
 
888
- // packages/herd-agent/dist/core/tools/tool-parameters.js
889
- import { z as z5 } from "zod";
890
- var URI_PAYLOAD_MARKER = "x-kay-uri-payload";
891
- var URI_PAYLOAD_WIRE_TYPES = [
892
- "string",
893
- "object",
894
- "array",
895
- "number",
896
- "boolean",
897
- "null"
898
- ];
899
- var uriPayloadSchema = z5.union([
900
- z5.string(),
901
- z5.record(z5.string(), z5.unknown()),
902
- z5.array(z5.unknown()),
903
- z5.number(),
904
- z5.boolean(),
905
- z5.null()
906
- ]).meta({ [URI_PAYLOAD_MARKER]: true });
907
- function toToolParameters(schema) {
908
- const jsonSchema = z5.toJSONSchema(schema, {
909
- io: "input",
910
- override(ctx) {
911
- const node = ctx.jsonSchema;
912
- if (node[URI_PAYLOAD_MARKER] === true) {
913
- delete node[URI_PAYLOAD_MARKER];
914
- delete node["anyOf"];
915
- node["type"] = [...URI_PAYLOAD_WIRE_TYPES];
916
- }
917
- }
918
- });
919
- const { $schema: _schema, $id: _id, ...rest } = jsonSchema;
920
- return rest;
921
- }
922
-
923
- // packages/herd-agent/dist/core/is-object.js
1156
+ // packages/plugin-primitives/dist/is-object.js
924
1157
  function isObject(value) {
925
1158
  return typeof value === "object" && value !== null && !Array.isArray(value);
926
1159
  }
927
1160
 
928
- // packages/herd-agent/dist/core/tools/produced-resources.js
1161
+ // packages/plugin-primitives/dist/produced-resources.js
929
1162
  var DECLARATIONS = Symbol.for("herd.producedResources.declarations");
930
1163
  function declareProducedResources(result, declarations) {
931
1164
  const details = declarableDetails(result.details);
@@ -973,43 +1206,6 @@ function jsonValue(value) {
973
1206
  return `<${Array.isArray(value) ? "array" : typeof value}>`;
974
1207
  }
975
1208
 
976
- // packages/plugin-primitives/dist/results.js
977
- function textResult(text, details) {
978
- const content = [{ type: "text", text }];
979
- return { content, details };
980
- }
981
- function contentResult(content, details) {
982
- return { content: [...content], details };
983
- }
984
-
985
- // packages/plugin-primitives/dist/transfer.js
986
- var MAX_BYTES2 = 50 * 1024 * 1024;
987
- async function readBodyWithByteCap(response, maxBytes) {
988
- const reader = response.body?.getReader();
989
- if (!reader)
990
- return void 0;
991
- const chunks = [];
992
- let totalSize = 0;
993
- let capped = false;
994
- while (true) {
995
- const { done, value } = await reader.read();
996
- if (done)
997
- break;
998
- if (value) {
999
- chunks.push(value);
1000
- totalSize += value.length;
1001
- if (totalSize > maxBytes) {
1002
- capped = true;
1003
- await reader.cancel();
1004
- break;
1005
- }
1006
- }
1007
- }
1008
- const bodyBuffer = Buffer.concat(chunks);
1009
- const bytes = new Uint8Array(bodyBuffer.buffer, bodyBuffer.byteOffset, bodyBuffer.byteLength);
1010
- return { bytes, capped };
1011
- }
1012
-
1013
1209
  // packages/plugin-sdk/dist/kay-agent/index.js
1014
1210
  import { z as z7 } from "zod";
1015
1211
 
@@ -1575,6 +1771,7 @@ export {
1575
1771
  bytesToBase64,
1576
1772
  capability,
1577
1773
  collectKayAgentAdvertisement,
1774
+ compileUriClaim,
1578
1775
  contentDigest,
1579
1776
  contentResult,
1580
1777
  createFallbackOnlyWithJev,