@luketandjung/ariadne 0.1.0
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/index.cjs +3672 -0
- package/dist/index.d.ts +8504 -0
- package/dist/index.js +3638 -0
- package/package.json +35 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3672 @@
|
|
|
1
|
+
var import_node_module = require("node:module");
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
9
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
10
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
11
|
+
for (let key of __getOwnPropNames(mod))
|
|
12
|
+
if (!__hasOwnProp.call(to, key))
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: () => mod[key],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
20
|
+
var __toCommonJS = (from) => {
|
|
21
|
+
var entry = __moduleCache.get(from), desc;
|
|
22
|
+
if (entry)
|
|
23
|
+
return entry;
|
|
24
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
26
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
27
|
+
get: () => from[key],
|
|
28
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
29
|
+
}));
|
|
30
|
+
__moduleCache.set(from, entry);
|
|
31
|
+
return entry;
|
|
32
|
+
};
|
|
33
|
+
var __export = (target, all) => {
|
|
34
|
+
for (var name in all)
|
|
35
|
+
__defProp(target, name, {
|
|
36
|
+
get: all[name],
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
set: (newValue) => all[name] = () => newValue
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// packages/ariadne/src/index.ts
|
|
44
|
+
var exports_src = {};
|
|
45
|
+
__export(exports_src, {
|
|
46
|
+
Toolkit: () => exports_Toolkit,
|
|
47
|
+
Tool: () => exports_Tool,
|
|
48
|
+
Tokenizer: () => exports_Tokenizer,
|
|
49
|
+
Telemetry: () => exports_Telemetry,
|
|
50
|
+
Response: () => exports_Response,
|
|
51
|
+
Prompt: () => exports_Prompt,
|
|
52
|
+
Model: () => exports_Model,
|
|
53
|
+
McpServer: () => exports_McpServer,
|
|
54
|
+
McpSchema: () => exports_McpSchema,
|
|
55
|
+
McpRegistry: () => exports_McpRegistry,
|
|
56
|
+
LanguageModel: () => exports_LanguageModel,
|
|
57
|
+
IdGenerator: () => exports_IdGenerator,
|
|
58
|
+
EmbeddingModel: () => exports_EmbeddingModel,
|
|
59
|
+
Chat: () => exports_Chat,
|
|
60
|
+
AiError: () => exports_AiError,
|
|
61
|
+
AgentRunner: () => exports_AgentRunner
|
|
62
|
+
});
|
|
63
|
+
module.exports = __toCommonJS(exports_src);
|
|
64
|
+
|
|
65
|
+
// packages/ariadne/src/AiError.ts
|
|
66
|
+
var exports_AiError = {};
|
|
67
|
+
__export(exports_AiError, {
|
|
68
|
+
isAiError: () => isAiError,
|
|
69
|
+
UnknownError: () => UnknownError,
|
|
70
|
+
TypeId: () => TypeId,
|
|
71
|
+
MalformedOutput: () => MalformedOutput,
|
|
72
|
+
MalformedInput: () => MalformedInput,
|
|
73
|
+
HttpResponseError: () => HttpResponseError,
|
|
74
|
+
HttpResponseDetails: () => HttpResponseDetails,
|
|
75
|
+
HttpRequestError: () => HttpRequestError,
|
|
76
|
+
HttpRequestDetails: () => HttpRequestDetails,
|
|
77
|
+
AiError: () => AiError
|
|
78
|
+
});
|
|
79
|
+
var Effect = __toESM(require("effect/Effect"));
|
|
80
|
+
var Inspectable = __toESM(require("effect/Inspectable"));
|
|
81
|
+
var Predicate = __toESM(require("effect/Predicate"));
|
|
82
|
+
var Schema = __toESM(require("effect/Schema"));
|
|
83
|
+
var TypeId = "~@effect/ai/AiError";
|
|
84
|
+
var isAiError = (u) => Predicate.hasProperty(u, TypeId);
|
|
85
|
+
var HttpRequestDetails = Schema.Struct({
|
|
86
|
+
method: Schema.Literal("GET", "POST", "PATCH", "PUT", "DELETE", "HEAD", "OPTIONS"),
|
|
87
|
+
url: Schema.String,
|
|
88
|
+
urlParams: Schema.Array(Schema.Tuple(Schema.String, Schema.String)),
|
|
89
|
+
hash: Schema.Option(Schema.String),
|
|
90
|
+
headers: Schema.Record({ key: Schema.String, value: Schema.String })
|
|
91
|
+
}).annotations({ identifier: "HttpRequestDetails" });
|
|
92
|
+
|
|
93
|
+
class HttpRequestError extends Schema.TaggedError("@effect/ai/AiError/HttpRequestError")("HttpRequestError", {
|
|
94
|
+
module: Schema.String,
|
|
95
|
+
method: Schema.String,
|
|
96
|
+
reason: Schema.Literal("Transport", "Encode", "InvalidUrl"),
|
|
97
|
+
request: HttpRequestDetails,
|
|
98
|
+
description: Schema.optional(Schema.String),
|
|
99
|
+
cause: Schema.optional(Schema.Defect)
|
|
100
|
+
}) {
|
|
101
|
+
[TypeId] = TypeId;
|
|
102
|
+
static fromRequestError({
|
|
103
|
+
error,
|
|
104
|
+
...params
|
|
105
|
+
}) {
|
|
106
|
+
return new HttpRequestError({
|
|
107
|
+
...params,
|
|
108
|
+
cause: error,
|
|
109
|
+
description: error.description,
|
|
110
|
+
reason: error.reason,
|
|
111
|
+
request: {
|
|
112
|
+
hash: error.request.hash,
|
|
113
|
+
headers: Inspectable.redact(error.request.headers),
|
|
114
|
+
method: error.request.method,
|
|
115
|
+
url: error.request.url,
|
|
116
|
+
urlParams: error.request.urlParams
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
get message() {
|
|
121
|
+
const methodAndUrl = `${this.request.method} ${this.request.url}`;
|
|
122
|
+
let baseMessage = this.description ? `${this.reason}: ${this.description}` : `${this.reason}: An HTTP request error occurred.`;
|
|
123
|
+
baseMessage += ` (${methodAndUrl})`;
|
|
124
|
+
let suggestion = "";
|
|
125
|
+
switch (this.reason) {
|
|
126
|
+
case "Encode": {
|
|
127
|
+
suggestion += "Check that the request body data is properly formatted and matches the expected content type.";
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case "InvalidUrl": {
|
|
131
|
+
suggestion += "Verify that the URL format is correct and that all required parameters have been provided.";
|
|
132
|
+
suggestion += " Check for any special characters that may need encoding.";
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
case "Transport": {
|
|
136
|
+
suggestion += "Check your network connection and verify that the requested URL is accessible.";
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
baseMessage += `
|
|
141
|
+
|
|
142
|
+
Suggestion: ${suggestion}`;
|
|
143
|
+
return baseMessage;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
var HttpResponseDetails = Schema.Struct({
|
|
147
|
+
status: Schema.Number,
|
|
148
|
+
headers: Schema.Record({ key: Schema.String, value: Schema.String })
|
|
149
|
+
}).annotations({ identifier: "HttpResponseDetails" });
|
|
150
|
+
|
|
151
|
+
class HttpResponseError extends Schema.TaggedError("@effect/ai/AiError/HttpResponseError")("HttpResponseError", {
|
|
152
|
+
module: Schema.String,
|
|
153
|
+
method: Schema.String,
|
|
154
|
+
request: HttpRequestDetails,
|
|
155
|
+
response: HttpResponseDetails,
|
|
156
|
+
body: Schema.optional(Schema.String),
|
|
157
|
+
reason: Schema.Literal("StatusCode", "Decode", "EmptyBody"),
|
|
158
|
+
description: Schema.optional(Schema.String)
|
|
159
|
+
}) {
|
|
160
|
+
[TypeId] = TypeId;
|
|
161
|
+
static fromResponseError({
|
|
162
|
+
error,
|
|
163
|
+
...params
|
|
164
|
+
}) {
|
|
165
|
+
let body = Effect.void;
|
|
166
|
+
const contentType = error.response.headers["content-type"] ?? "";
|
|
167
|
+
if (contentType.includes("application/json")) {
|
|
168
|
+
body = error.response.json;
|
|
169
|
+
} else if (contentType.includes("text/") || contentType.includes("urlencoded")) {
|
|
170
|
+
body = error.response.text;
|
|
171
|
+
}
|
|
172
|
+
return Effect.flatMap(Effect.merge(body), (body2) => new HttpResponseError({
|
|
173
|
+
...params,
|
|
174
|
+
description: error.description,
|
|
175
|
+
reason: error.reason,
|
|
176
|
+
request: {
|
|
177
|
+
hash: error.request.hash,
|
|
178
|
+
headers: Inspectable.redact(error.request.headers),
|
|
179
|
+
method: error.request.method,
|
|
180
|
+
url: error.request.url,
|
|
181
|
+
urlParams: error.request.urlParams
|
|
182
|
+
},
|
|
183
|
+
response: {
|
|
184
|
+
headers: Inspectable.redact(error.response.headers),
|
|
185
|
+
status: error.response.status
|
|
186
|
+
},
|
|
187
|
+
body: Inspectable.format(body2)
|
|
188
|
+
}));
|
|
189
|
+
}
|
|
190
|
+
get message() {
|
|
191
|
+
const methodUrlStatus = `${this.response.status} ${this.request.method} ${this.request.url}`;
|
|
192
|
+
let baseMessage = this.description ? `${this.reason}: ${this.description}` : `${this.reason}: An HTTP response error occurred.`;
|
|
193
|
+
baseMessage += ` (${methodUrlStatus})`;
|
|
194
|
+
let suggestion = "";
|
|
195
|
+
switch (this.reason) {
|
|
196
|
+
case "Decode": {
|
|
197
|
+
suggestion += "The response format does not match what is expected. " + "Verify API version compatibility, check response content-type, " + "and/or examine if the endpoint schema has changed.";
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case "EmptyBody": {
|
|
201
|
+
suggestion += "The response body was empty. This may indicate a server " + "issue, API version mismatch, or the endpoint may have changed its response format.";
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "StatusCode": {
|
|
205
|
+
suggestion += getStatusCodeSuggestion(this.response.status);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
baseMessage += `
|
|
210
|
+
|
|
211
|
+
${suggestion}`;
|
|
212
|
+
if (Predicate.isNotUndefined(this.body)) {
|
|
213
|
+
baseMessage += `
|
|
214
|
+
|
|
215
|
+
Response Body: ${this.body}`;
|
|
216
|
+
}
|
|
217
|
+
return baseMessage;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
class MalformedInput extends Schema.TaggedError("@effect/ai/AiError/MalformedInput")("MalformedInput", {
|
|
222
|
+
module: Schema.String,
|
|
223
|
+
method: Schema.String,
|
|
224
|
+
description: Schema.optional(Schema.String),
|
|
225
|
+
cause: Schema.optional(Schema.Defect)
|
|
226
|
+
}) {
|
|
227
|
+
[TypeId] = TypeId;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
class MalformedOutput extends Schema.TaggedError("@effect/ai/AiError/MalformedOutput")("MalformedOutput", {
|
|
231
|
+
module: Schema.String,
|
|
232
|
+
method: Schema.String,
|
|
233
|
+
description: Schema.optional(Schema.String),
|
|
234
|
+
cause: Schema.optional(Schema.Defect)
|
|
235
|
+
}) {
|
|
236
|
+
[TypeId] = TypeId;
|
|
237
|
+
static fromParseError({
|
|
238
|
+
error,
|
|
239
|
+
...params
|
|
240
|
+
}) {
|
|
241
|
+
return new MalformedOutput({
|
|
242
|
+
...params,
|
|
243
|
+
cause: error
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
class UnknownError extends Schema.TaggedError("@effect/ai/UnknownError")("UnknownError", {
|
|
249
|
+
module: Schema.String,
|
|
250
|
+
method: Schema.String,
|
|
251
|
+
description: Schema.optional(Schema.String),
|
|
252
|
+
cause: Schema.optional(Schema.Defect)
|
|
253
|
+
}) {
|
|
254
|
+
[TypeId] = TypeId;
|
|
255
|
+
get message() {
|
|
256
|
+
const moduleMethod = `${this.module}.${this.method}`;
|
|
257
|
+
return Predicate.isUndefined(this.description) ? `${moduleMethod}: An error occurred` : `${moduleMethod}: ${this.description}`;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
var AiError = Schema.Union(HttpRequestError, HttpResponseError, MalformedInput, MalformedOutput, UnknownError);
|
|
261
|
+
var getStatusCodeSuggestion = (statusCode) => {
|
|
262
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
263
|
+
switch (statusCode) {
|
|
264
|
+
case 400:
|
|
265
|
+
return "Bad Request - Check request parameters, headers, and body format against API documentation.";
|
|
266
|
+
case 401:
|
|
267
|
+
return "Unauthorized - Verify API key, authentication credentials, or token expiration.";
|
|
268
|
+
case 403:
|
|
269
|
+
return "Forbidden - Check API permissions, usage limits, or resource access rights.";
|
|
270
|
+
case 404:
|
|
271
|
+
return "Not Found - Verify the endpoint URL, API version, and resource identifiers.";
|
|
272
|
+
case 408:
|
|
273
|
+
return "Request Timeout - Consider increasing timeout duration or implementing retry logic.";
|
|
274
|
+
case 422:
|
|
275
|
+
return "Unprocessable Entity - Check request data validation, required fields, and data formats.";
|
|
276
|
+
case 429:
|
|
277
|
+
return "Rate Limited - Implement exponential backoff or reduce request frequency.";
|
|
278
|
+
default:
|
|
279
|
+
return "Client error - Review request format, parameters, and API documentation.";
|
|
280
|
+
}
|
|
281
|
+
} else if (statusCode >= 500) {
|
|
282
|
+
return "Server error - This is likely temporary. Implement retry logic with exponential backoff.";
|
|
283
|
+
} else {
|
|
284
|
+
return "Check API documentation for this status code.";
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
// packages/ariadne/src/AgentRunner.ts
|
|
288
|
+
var exports_AgentRunner = {};
|
|
289
|
+
__export(exports_AgentRunner, {
|
|
290
|
+
defaultConfig: () => defaultConfig,
|
|
291
|
+
ReAct: () => ReAct,
|
|
292
|
+
Config: () => Config
|
|
293
|
+
});
|
|
294
|
+
var Context6 = __toESM(require("effect/Context"));
|
|
295
|
+
var Effect6 = __toESM(require("effect/Effect"));
|
|
296
|
+
var Layer3 = __toESM(require("effect/Layer"));
|
|
297
|
+
var Ref = __toESM(require("effect/Ref"));
|
|
298
|
+
var Stream2 = __toESM(require("effect/Stream"));
|
|
299
|
+
|
|
300
|
+
// packages/ariadne/src/LanguageModel.ts
|
|
301
|
+
var exports_LanguageModel = {};
|
|
302
|
+
__export(exports_LanguageModel, {
|
|
303
|
+
streamText: () => streamText,
|
|
304
|
+
streamObject: () => streamObject,
|
|
305
|
+
make: () => make6,
|
|
306
|
+
generateText: () => generateText,
|
|
307
|
+
generateObject: () => generateObject,
|
|
308
|
+
LanguageModel: () => LanguageModel,
|
|
309
|
+
GenerateTextResponse: () => GenerateTextResponse,
|
|
310
|
+
GenerateObjectResponse: () => GenerateObjectResponse
|
|
311
|
+
});
|
|
312
|
+
var Chunk = __toESM(require("effect/Chunk"));
|
|
313
|
+
var Context5 = __toESM(require("effect/Context"));
|
|
314
|
+
var Effect5 = __toESM(require("effect/Effect"));
|
|
315
|
+
var Mailbox = __toESM(require("effect/Mailbox"));
|
|
316
|
+
var Option3 = __toESM(require("effect/Option"));
|
|
317
|
+
var ParseResult4 = __toESM(require("effect/ParseResult"));
|
|
318
|
+
var Predicate8 = __toESM(require("effect/Predicate"));
|
|
319
|
+
var Schema6 = __toESM(require("effect/Schema"));
|
|
320
|
+
var Stream = __toESM(require("effect/Stream"));
|
|
321
|
+
|
|
322
|
+
// packages/ariadne/src/IdGenerator.ts
|
|
323
|
+
var exports_IdGenerator = {};
|
|
324
|
+
__export(exports_IdGenerator, {
|
|
325
|
+
make: () => make,
|
|
326
|
+
layer: () => layer,
|
|
327
|
+
defaultIdGenerator: () => defaultIdGenerator,
|
|
328
|
+
IdGenerator: () => IdGenerator
|
|
329
|
+
});
|
|
330
|
+
var Cause = __toESM(require("effect/Cause"));
|
|
331
|
+
var Context = __toESM(require("effect/Context"));
|
|
332
|
+
var Effect2 = __toESM(require("effect/Effect"));
|
|
333
|
+
var Layer = __toESM(require("effect/Layer"));
|
|
334
|
+
var Predicate2 = __toESM(require("effect/Predicate"));
|
|
335
|
+
var Random = __toESM(require("effect/Random"));
|
|
336
|
+
|
|
337
|
+
class IdGenerator extends Context.Tag("@effect/ai/IdGenerator")() {
|
|
338
|
+
}
|
|
339
|
+
var DEFAULT_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
340
|
+
var DEFAULT_SEPARATOR = "_";
|
|
341
|
+
var DEFAULT_SIZE = 16;
|
|
342
|
+
var makeGenerator = ({
|
|
343
|
+
alphabet = DEFAULT_ALPHABET,
|
|
344
|
+
prefix,
|
|
345
|
+
separator = DEFAULT_SEPARATOR,
|
|
346
|
+
size = DEFAULT_SIZE
|
|
347
|
+
}) => {
|
|
348
|
+
const alphabetLength = alphabet.length;
|
|
349
|
+
return Effect2.fnUntraced(function* () {
|
|
350
|
+
const chars = new Array(size);
|
|
351
|
+
for (let i = 0;i < size; i++) {
|
|
352
|
+
chars[i] = alphabet[(yield* Random.next) * alphabetLength | 0];
|
|
353
|
+
}
|
|
354
|
+
const identifier = chars.join("");
|
|
355
|
+
if (Predicate2.isUndefined(prefix)) {
|
|
356
|
+
return identifier;
|
|
357
|
+
}
|
|
358
|
+
return `${prefix}${separator}${identifier}`;
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
var defaultIdGenerator = {
|
|
362
|
+
generateId: makeGenerator({ prefix: "id" })
|
|
363
|
+
};
|
|
364
|
+
var make = Effect2.fnUntraced(function* ({
|
|
365
|
+
alphabet = DEFAULT_ALPHABET,
|
|
366
|
+
prefix,
|
|
367
|
+
separator = DEFAULT_SEPARATOR,
|
|
368
|
+
size = DEFAULT_SIZE
|
|
369
|
+
}) {
|
|
370
|
+
if (alphabet.includes(separator)) {
|
|
371
|
+
const message = `The separator "${separator}" must not be part of the alphabet "${alphabet}".`;
|
|
372
|
+
return yield* new Cause.IllegalArgumentException(message);
|
|
373
|
+
}
|
|
374
|
+
const generateId = makeGenerator({ alphabet, prefix, separator, size });
|
|
375
|
+
return {
|
|
376
|
+
generateId
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
var layer = (options) => Layer.effect(IdGenerator, make(options));
|
|
380
|
+
|
|
381
|
+
// packages/ariadne/src/Prompt.ts
|
|
382
|
+
var exports_Prompt = {};
|
|
383
|
+
__export(exports_Prompt, {
|
|
384
|
+
userMessage: () => userMessage,
|
|
385
|
+
toolResultPart: () => toolResultPart,
|
|
386
|
+
toolMessage: () => toolMessage,
|
|
387
|
+
toolCallPart: () => toolCallPart,
|
|
388
|
+
textPart: () => textPart,
|
|
389
|
+
systemMessage: () => systemMessage,
|
|
390
|
+
setSystem: () => setSystem,
|
|
391
|
+
reasoningPart: () => reasoningPart,
|
|
392
|
+
prependSystem: () => prependSystem,
|
|
393
|
+
merge: () => merge2,
|
|
394
|
+
makePart: () => makePart,
|
|
395
|
+
makeMessage: () => makeMessage,
|
|
396
|
+
make: () => make2,
|
|
397
|
+
isPrompt: () => isPrompt,
|
|
398
|
+
isPart: () => isPart,
|
|
399
|
+
isMessage: () => isMessage,
|
|
400
|
+
fromResponseParts: () => fromResponseParts,
|
|
401
|
+
fromMessages: () => fromMessages,
|
|
402
|
+
filePart: () => filePart,
|
|
403
|
+
empty: () => empty,
|
|
404
|
+
assistantMessage: () => assistantMessage,
|
|
405
|
+
appendSystem: () => appendSystem,
|
|
406
|
+
UserMessage: () => UserMessage,
|
|
407
|
+
TypeId: () => TypeId2,
|
|
408
|
+
ToolResultPart: () => ToolResultPart,
|
|
409
|
+
ToolMessage: () => ToolMessage,
|
|
410
|
+
ToolCallPart: () => ToolCallPart,
|
|
411
|
+
TextPart: () => TextPart,
|
|
412
|
+
SystemMessage: () => SystemMessage,
|
|
413
|
+
ReasoningPart: () => ReasoningPart,
|
|
414
|
+
ProviderOptions: () => ProviderOptions,
|
|
415
|
+
PromptFromSelf: () => PromptFromSelf,
|
|
416
|
+
Prompt: () => Prompt,
|
|
417
|
+
PartTypeId: () => PartTypeId,
|
|
418
|
+
MessageTypeId: () => MessageTypeId,
|
|
419
|
+
MessageContentFromString: () => MessageContentFromString,
|
|
420
|
+
Message: () => Message,
|
|
421
|
+
FromJson: () => FromJson,
|
|
422
|
+
FilePart: () => FilePart,
|
|
423
|
+
AssistantMessage: () => AssistantMessage
|
|
424
|
+
});
|
|
425
|
+
var Arbitrary = __toESM(require("effect/Arbitrary"));
|
|
426
|
+
var Arr = __toESM(require("effect/Array"));
|
|
427
|
+
var import_Function = require("effect/Function");
|
|
428
|
+
var ParseResult = __toESM(require("effect/ParseResult"));
|
|
429
|
+
var import_Pipeable = require("effect/Pipeable");
|
|
430
|
+
var Predicate3 = __toESM(require("effect/Predicate"));
|
|
431
|
+
var Schema2 = __toESM(require("effect/Schema"));
|
|
432
|
+
var constEmptyObject = () => ({});
|
|
433
|
+
var ProviderOptions = Schema2.Record({
|
|
434
|
+
key: Schema2.String,
|
|
435
|
+
value: Schema2.UndefinedOr(Schema2.Record({
|
|
436
|
+
key: Schema2.String,
|
|
437
|
+
value: Schema2.Unknown
|
|
438
|
+
}))
|
|
439
|
+
});
|
|
440
|
+
var PartTypeId = "~effect/ai/Prompt/Part";
|
|
441
|
+
var isPart = (u) => Predicate3.hasProperty(u, PartTypeId);
|
|
442
|
+
var makePart = (type, params) => ({
|
|
443
|
+
...params,
|
|
444
|
+
[PartTypeId]: PartTypeId,
|
|
445
|
+
type,
|
|
446
|
+
options: params.options ?? {}
|
|
447
|
+
});
|
|
448
|
+
var TextPart = Schema2.Struct({
|
|
449
|
+
type: Schema2.Literal("text"),
|
|
450
|
+
text: Schema2.String,
|
|
451
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
452
|
+
default: constEmptyObject
|
|
453
|
+
})
|
|
454
|
+
}).pipe(Schema2.attachPropertySignature(PartTypeId, PartTypeId), Schema2.annotations({ identifier: "TextPart" }));
|
|
455
|
+
var textPart = (params) => makePart("text", params);
|
|
456
|
+
var ReasoningPart = Schema2.Struct({
|
|
457
|
+
type: Schema2.Literal("reasoning"),
|
|
458
|
+
text: Schema2.String,
|
|
459
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
460
|
+
default: constEmptyObject
|
|
461
|
+
})
|
|
462
|
+
}).pipe(Schema2.attachPropertySignature(PartTypeId, PartTypeId), Schema2.annotations({ identifier: "ReasoningPart" }));
|
|
463
|
+
var reasoningPart = (params) => makePart("reasoning", params);
|
|
464
|
+
var FilePart = Schema2.Struct({
|
|
465
|
+
type: Schema2.Literal("file"),
|
|
466
|
+
mediaType: Schema2.String,
|
|
467
|
+
fileName: Schema2.optional(Schema2.String),
|
|
468
|
+
data: Schema2.Union(Schema2.String, Schema2.Uint8ArrayFromSelf, Schema2.URLFromSelf),
|
|
469
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
470
|
+
default: constEmptyObject
|
|
471
|
+
})
|
|
472
|
+
}).pipe(Schema2.attachPropertySignature(PartTypeId, PartTypeId), Schema2.annotations({ identifier: "FilePart" }));
|
|
473
|
+
var filePart = (params) => makePart("file", params);
|
|
474
|
+
var ToolCallPart = Schema2.Struct({
|
|
475
|
+
type: Schema2.Literal("tool-call"),
|
|
476
|
+
id: Schema2.String,
|
|
477
|
+
name: Schema2.String,
|
|
478
|
+
params: Schema2.Unknown,
|
|
479
|
+
providerExecuted: Schema2.optionalWith(Schema2.Boolean, {
|
|
480
|
+
default: import_Function.constFalse
|
|
481
|
+
}),
|
|
482
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
483
|
+
default: constEmptyObject
|
|
484
|
+
})
|
|
485
|
+
}).pipe(Schema2.attachPropertySignature(PartTypeId, PartTypeId), Schema2.annotations({ identifier: "ToolCallPart" }));
|
|
486
|
+
var toolCallPart = (params) => makePart("tool-call", params);
|
|
487
|
+
var ToolResultPart = Schema2.Struct({
|
|
488
|
+
type: Schema2.Literal("tool-result"),
|
|
489
|
+
id: Schema2.String,
|
|
490
|
+
name: Schema2.String,
|
|
491
|
+
isFailure: Schema2.Boolean,
|
|
492
|
+
result: Schema2.Unknown,
|
|
493
|
+
providerExecuted: Schema2.Boolean,
|
|
494
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
495
|
+
default: constEmptyObject
|
|
496
|
+
})
|
|
497
|
+
}).pipe(Schema2.attachPropertySignature(PartTypeId, PartTypeId), Schema2.annotations({ identifier: "ToolResultPart" }));
|
|
498
|
+
var toolResultPart = (params) => makePart("tool-result", params);
|
|
499
|
+
var MessageTypeId = "~effect/ai/Prompt/Message";
|
|
500
|
+
var isMessage = (u) => Predicate3.hasProperty(u, MessageTypeId);
|
|
501
|
+
var makeMessage = (role, params) => ({
|
|
502
|
+
...params,
|
|
503
|
+
[MessageTypeId]: MessageTypeId,
|
|
504
|
+
role,
|
|
505
|
+
options: params.options ?? {}
|
|
506
|
+
});
|
|
507
|
+
var MessageContentFromString = Schema2.transform(Schema2.String, Schema2.NonEmptyArray(Schema2.typeSchema(TextPart)), {
|
|
508
|
+
strict: true,
|
|
509
|
+
decode: (text) => Arr.of(makePart("text", { text })),
|
|
510
|
+
encode: (content) => content[0].text
|
|
511
|
+
});
|
|
512
|
+
var SystemMessage = Schema2.Struct({
|
|
513
|
+
role: Schema2.Literal("system"),
|
|
514
|
+
content: Schema2.String,
|
|
515
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
516
|
+
default: constEmptyObject
|
|
517
|
+
})
|
|
518
|
+
}).pipe(Schema2.attachPropertySignature(MessageTypeId, MessageTypeId), Schema2.annotations({ identifier: "SystemMessage" }));
|
|
519
|
+
var systemMessage = (params) => makeMessage("system", params);
|
|
520
|
+
var UserMessage = Schema2.Struct({
|
|
521
|
+
role: Schema2.Literal("user"),
|
|
522
|
+
content: Schema2.Union(MessageContentFromString, Schema2.Array(Schema2.Union(TextPart, FilePart))),
|
|
523
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
524
|
+
default: constEmptyObject
|
|
525
|
+
})
|
|
526
|
+
}).pipe(Schema2.attachPropertySignature(MessageTypeId, MessageTypeId), Schema2.annotations({ identifier: "UserMessage" }));
|
|
527
|
+
var userMessage = (params) => makeMessage("user", params);
|
|
528
|
+
var AssistantMessage = Schema2.Struct({
|
|
529
|
+
role: Schema2.Literal("assistant"),
|
|
530
|
+
content: Schema2.Union(MessageContentFromString, Schema2.Array(Schema2.Union(TextPart, FilePart, ReasoningPart, ToolCallPart, ToolResultPart))),
|
|
531
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
532
|
+
default: constEmptyObject
|
|
533
|
+
})
|
|
534
|
+
}).pipe(Schema2.attachPropertySignature(MessageTypeId, MessageTypeId), Schema2.annotations({ identifier: "AssistantMessage" }));
|
|
535
|
+
var assistantMessage = (params) => makeMessage("assistant", params);
|
|
536
|
+
var ToolMessage = Schema2.Struct({
|
|
537
|
+
role: Schema2.Literal("tool"),
|
|
538
|
+
content: Schema2.Array(ToolResultPart),
|
|
539
|
+
options: Schema2.optionalWith(ProviderOptions, {
|
|
540
|
+
default: constEmptyObject
|
|
541
|
+
})
|
|
542
|
+
}).pipe(Schema2.attachPropertySignature(MessageTypeId, MessageTypeId), Schema2.annotations({ identifier: "ToolMessage" }));
|
|
543
|
+
var toolMessage = (params) => makeMessage("tool", params);
|
|
544
|
+
var Message = Schema2.Union(SystemMessage, UserMessage, AssistantMessage, ToolMessage);
|
|
545
|
+
var TypeId2 = "~@effect/ai/Prompt";
|
|
546
|
+
var isPrompt = (u) => Predicate3.hasProperty(u, TypeId2);
|
|
547
|
+
|
|
548
|
+
class PromptFromSelf extends Schema2.declare((u) => isPrompt(u), {
|
|
549
|
+
typeConstructor: { _tag: "effect/ai/Prompt" },
|
|
550
|
+
identifier: "PromptFromSelf",
|
|
551
|
+
description: "a Prompt instance",
|
|
552
|
+
arbitrary: () => (fc) => fc.array(Arbitrary.makeLazy(Message)(fc)).map(makePrompt)
|
|
553
|
+
}) {
|
|
554
|
+
}
|
|
555
|
+
var Prompt = Schema2.transformOrFail(Schema2.Struct({ content: Schema2.Array(Schema2.encodedSchema(Message)) }), PromptFromSelf, {
|
|
556
|
+
strict: true,
|
|
557
|
+
decode: (i, _, ast) => decodePrompt(i, ast),
|
|
558
|
+
encode: (a, _, ast) => encodePrompt(a, ast)
|
|
559
|
+
}).annotations({ identifier: "Prompt" });
|
|
560
|
+
var decodeMessages = ParseResult.decodeEither(Schema2.Array(Message));
|
|
561
|
+
var encodeMessages = ParseResult.encodeEither(Schema2.Array(Message));
|
|
562
|
+
var decodePrompt = (input, ast) => ParseResult.mapBoth(decodeMessages(input.content), {
|
|
563
|
+
onFailure: () => new ParseResult.Type(ast, input, `Unable to decode ${JSON.stringify(input)} into a Prompt`),
|
|
564
|
+
onSuccess: makePrompt
|
|
565
|
+
});
|
|
566
|
+
var encodePrompt = (input, ast) => ParseResult.mapBoth(encodeMessages(input.content), {
|
|
567
|
+
onFailure: () => new ParseResult.Type(ast, input, `Failed to encode Prompt`),
|
|
568
|
+
onSuccess: (messages) => ({ content: messages })
|
|
569
|
+
});
|
|
570
|
+
var FromJson = Schema2.parseJson(Prompt);
|
|
571
|
+
var Proto = {
|
|
572
|
+
[TypeId2]: TypeId2,
|
|
573
|
+
pipe() {
|
|
574
|
+
return import_Pipeable.pipeArguments(this, arguments);
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
var makePrompt = (content) => Object.assign(Object.create(Proto), {
|
|
578
|
+
content
|
|
579
|
+
});
|
|
580
|
+
var decodeMessagesSync = Schema2.decodeSync(Schema2.Array(Message));
|
|
581
|
+
var empty = makePrompt([]);
|
|
582
|
+
var make2 = (input) => {
|
|
583
|
+
if (Predicate3.isString(input)) {
|
|
584
|
+
const part = makePart("text", { text: input });
|
|
585
|
+
const message = makeMessage("user", { content: [part] });
|
|
586
|
+
return makePrompt([message]);
|
|
587
|
+
}
|
|
588
|
+
if (Predicate3.isIterable(input)) {
|
|
589
|
+
return makePrompt(decodeMessagesSync(Arr.fromIterable(input), {
|
|
590
|
+
errors: "all"
|
|
591
|
+
}));
|
|
592
|
+
}
|
|
593
|
+
return input;
|
|
594
|
+
};
|
|
595
|
+
var fromMessages = (messages) => makePrompt(messages);
|
|
596
|
+
var fromResponseParts = (parts) => {
|
|
597
|
+
if (parts.length === 0) {
|
|
598
|
+
return empty;
|
|
599
|
+
}
|
|
600
|
+
const assistantParts = [];
|
|
601
|
+
const toolParts = [];
|
|
602
|
+
const activeTextDeltas = new Map;
|
|
603
|
+
const activeReasoningDeltas = new Map;
|
|
604
|
+
for (const part of parts) {
|
|
605
|
+
switch (part.type) {
|
|
606
|
+
case "text": {
|
|
607
|
+
assistantParts.push(makePart("text", { text: part.text }));
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
case "text-start": {
|
|
611
|
+
activeTextDeltas.set(part.id, { text: "" });
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
case "text-delta": {
|
|
615
|
+
if (activeTextDeltas.has(part.id)) {
|
|
616
|
+
activeTextDeltas.get(part.id).text += part.delta;
|
|
617
|
+
}
|
|
618
|
+
break;
|
|
619
|
+
}
|
|
620
|
+
case "text-end": {
|
|
621
|
+
if (activeTextDeltas.has(part.id)) {
|
|
622
|
+
assistantParts.push(makePart("text", activeTextDeltas.get(part.id)));
|
|
623
|
+
}
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
case "reasoning": {
|
|
627
|
+
assistantParts.push(makePart("reasoning", { text: part.text }));
|
|
628
|
+
break;
|
|
629
|
+
}
|
|
630
|
+
case "reasoning-start": {
|
|
631
|
+
activeReasoningDeltas.set(part.id, { text: "" });
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
case "reasoning-delta": {
|
|
635
|
+
if (activeReasoningDeltas.has(part.id)) {
|
|
636
|
+
activeReasoningDeltas.get(part.id).text += part.delta;
|
|
637
|
+
}
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
case "reasoning-end": {
|
|
641
|
+
if (activeReasoningDeltas.has(part.id)) {
|
|
642
|
+
assistantParts.push(makePart("reasoning", activeReasoningDeltas.get(part.id)));
|
|
643
|
+
}
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
case "tool-call": {
|
|
647
|
+
assistantParts.push(makePart("tool-call", {
|
|
648
|
+
id: part.id,
|
|
649
|
+
name: part.providerName ?? part.name,
|
|
650
|
+
params: part.params,
|
|
651
|
+
providerExecuted: part.providerExecuted ?? false
|
|
652
|
+
}));
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
case "tool-result": {
|
|
656
|
+
const toolPart = makePart("tool-result", {
|
|
657
|
+
id: part.id,
|
|
658
|
+
name: part.providerName ?? part.name,
|
|
659
|
+
isFailure: part.isFailure,
|
|
660
|
+
result: part.encodedResult,
|
|
661
|
+
providerExecuted: part.providerExecuted ?? false
|
|
662
|
+
});
|
|
663
|
+
if (part.providerExecuted) {
|
|
664
|
+
assistantParts.push(toolPart);
|
|
665
|
+
} else {
|
|
666
|
+
toolParts.push(toolPart);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
if (assistantParts.length === 0 && toolParts.length === 0) {
|
|
672
|
+
return empty;
|
|
673
|
+
}
|
|
674
|
+
const messages = [];
|
|
675
|
+
if (assistantParts.length > 0) {
|
|
676
|
+
messages.push(makeMessage("assistant", { content: assistantParts }));
|
|
677
|
+
}
|
|
678
|
+
if (toolParts.length > 0) {
|
|
679
|
+
messages.push(makeMessage("tool", { content: toolParts }));
|
|
680
|
+
}
|
|
681
|
+
return makePrompt(messages);
|
|
682
|
+
};
|
|
683
|
+
var merge2 = import_Function.dual(2, (self, input) => {
|
|
684
|
+
const other = make2(input);
|
|
685
|
+
if (self.content.length === 0) {
|
|
686
|
+
return other;
|
|
687
|
+
}
|
|
688
|
+
if (other.content.length === 0) {
|
|
689
|
+
return self;
|
|
690
|
+
}
|
|
691
|
+
return fromMessages([...self.content, ...other.content]);
|
|
692
|
+
});
|
|
693
|
+
var setSystem = import_Function.dual(2, (self, content) => {
|
|
694
|
+
const messages = [makeMessage("system", { content })];
|
|
695
|
+
for (const message of self.content) {
|
|
696
|
+
if (message.role !== "system") {
|
|
697
|
+
messages.push(message);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return makePrompt(messages);
|
|
701
|
+
});
|
|
702
|
+
var prependSystem = import_Function.dual(2, (self, content) => {
|
|
703
|
+
let system = undefined;
|
|
704
|
+
for (const message of self.content) {
|
|
705
|
+
if (message.role === "system") {
|
|
706
|
+
system = makeMessage("system", {
|
|
707
|
+
content: content + message.content
|
|
708
|
+
});
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
if (Predicate3.isUndefined(system)) {
|
|
713
|
+
system = makeMessage("system", { content });
|
|
714
|
+
}
|
|
715
|
+
return makePrompt([system, ...self.content]);
|
|
716
|
+
});
|
|
717
|
+
var appendSystem = import_Function.dual(2, (self, content) => {
|
|
718
|
+
let system = undefined;
|
|
719
|
+
for (const message of self.content) {
|
|
720
|
+
if (message.role === "system") {
|
|
721
|
+
system = makeMessage("system", {
|
|
722
|
+
content: message.content + content
|
|
723
|
+
});
|
|
724
|
+
break;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
if (Predicate3.isUndefined(system)) {
|
|
728
|
+
system = makeMessage("system", { content });
|
|
729
|
+
}
|
|
730
|
+
return makePrompt([system, ...self.content]);
|
|
731
|
+
});
|
|
732
|
+
|
|
733
|
+
// packages/ariadne/src/Response.ts
|
|
734
|
+
var exports_Response = {};
|
|
735
|
+
__export(exports_Response, {
|
|
736
|
+
urlSourcePart: () => urlSourcePart,
|
|
737
|
+
toolResultPart: () => toolResultPart2,
|
|
738
|
+
toolParamsStartPart: () => toolParamsStartPart,
|
|
739
|
+
toolParamsEndPart: () => toolParamsEndPart,
|
|
740
|
+
toolParamsDeltaPart: () => toolParamsDeltaPart,
|
|
741
|
+
toolCallPart: () => toolCallPart2,
|
|
742
|
+
textStartPart: () => textStartPart,
|
|
743
|
+
textPart: () => textPart2,
|
|
744
|
+
textEndPart: () => textEndPart,
|
|
745
|
+
textDeltaPart: () => textDeltaPart,
|
|
746
|
+
responseMetadataPart: () => responseMetadataPart,
|
|
747
|
+
reasoningStartPart: () => reasoningStartPart,
|
|
748
|
+
reasoningPart: () => reasoningPart2,
|
|
749
|
+
reasoningEndPart: () => reasoningEndPart,
|
|
750
|
+
reasoningDeltaPart: () => reasoningDeltaPart,
|
|
751
|
+
objectDonePart: () => objectDonePart,
|
|
752
|
+
objectDeltaPart: () => objectDeltaPart,
|
|
753
|
+
makePart: () => makePart2,
|
|
754
|
+
isPart: () => isPart2,
|
|
755
|
+
finishPart: () => finishPart,
|
|
756
|
+
filePart: () => filePart2,
|
|
757
|
+
errorPart: () => errorPart,
|
|
758
|
+
documentSourcePart: () => documentSourcePart,
|
|
759
|
+
Usage: () => Usage,
|
|
760
|
+
UrlSourcePart: () => UrlSourcePart,
|
|
761
|
+
ToolResultPart: () => ToolResultPart2,
|
|
762
|
+
ToolParamsStartPart: () => ToolParamsStartPart,
|
|
763
|
+
ToolParamsEndPart: () => ToolParamsEndPart,
|
|
764
|
+
ToolParamsDeltaPart: () => ToolParamsDeltaPart,
|
|
765
|
+
ToolCallPart: () => ToolCallPart2,
|
|
766
|
+
TextStartPart: () => TextStartPart,
|
|
767
|
+
TextPart: () => TextPart2,
|
|
768
|
+
TextEndPart: () => TextEndPart,
|
|
769
|
+
TextDeltaPart: () => TextDeltaPart,
|
|
770
|
+
StreamPart: () => StreamPart,
|
|
771
|
+
ResponseMetadataPart: () => ResponseMetadataPart,
|
|
772
|
+
ReasoningStartPart: () => ReasoningStartPart,
|
|
773
|
+
ReasoningPart: () => ReasoningPart2,
|
|
774
|
+
ReasoningEndPart: () => ReasoningEndPart,
|
|
775
|
+
ReasoningDeltaPart: () => ReasoningDeltaPart,
|
|
776
|
+
ProviderMetadata: () => ProviderMetadata,
|
|
777
|
+
PartTypeId: () => PartTypeId2,
|
|
778
|
+
Part: () => Part,
|
|
779
|
+
ObjectDonePart: () => ObjectDonePart,
|
|
780
|
+
ObjectDeltaPart: () => ObjectDeltaPart,
|
|
781
|
+
FinishReason: () => FinishReason,
|
|
782
|
+
FinishPart: () => FinishPart,
|
|
783
|
+
FilePart: () => FilePart2,
|
|
784
|
+
ErrorPart: () => ErrorPart,
|
|
785
|
+
DocumentSourcePart: () => DocumentSourcePart,
|
|
786
|
+
AllParts: () => AllParts
|
|
787
|
+
});
|
|
788
|
+
var Effect3 = __toESM(require("effect/Effect"));
|
|
789
|
+
var import_Function2 = require("effect/Function");
|
|
790
|
+
var ParseResult2 = __toESM(require("effect/ParseResult"));
|
|
791
|
+
var Predicate4 = __toESM(require("effect/Predicate"));
|
|
792
|
+
var Schema3 = __toESM(require("effect/Schema"));
|
|
793
|
+
var constEmptyObject2 = () => ({});
|
|
794
|
+
var PartTypeId2 = "~effect/ai/Content/Part";
|
|
795
|
+
var isPart2 = (u) => Predicate4.hasProperty(u, PartTypeId2);
|
|
796
|
+
var AllParts = (toolkit) => {
|
|
797
|
+
const toolCalls = [];
|
|
798
|
+
const toolCallResults = [];
|
|
799
|
+
for (const tool of Object.values(toolkit.tools)) {
|
|
800
|
+
toolCalls.push(ToolCallPart2(tool.name, tool.parametersSchema));
|
|
801
|
+
toolCallResults.push(ToolResultPart2(tool.name, tool.successSchema, tool.failureSchema));
|
|
802
|
+
}
|
|
803
|
+
return Schema3.Union(TextPart2, TextStartPart, TextDeltaPart, TextEndPart, ReasoningPart2, ReasoningStartPart, ReasoningDeltaPart, ReasoningEndPart, ToolParamsStartPart, ToolParamsDeltaPart, ToolParamsEndPart, FilePart2, DocumentSourcePart, UrlSourcePart, ResponseMetadataPart, FinishPart, ErrorPart, ...toolCalls, ...toolCallResults);
|
|
804
|
+
};
|
|
805
|
+
var Part = (toolkit) => {
|
|
806
|
+
const toolCalls = [];
|
|
807
|
+
const toolCallResults = [];
|
|
808
|
+
for (const tool of Object.values(toolkit.tools)) {
|
|
809
|
+
toolCalls.push(ToolCallPart2(tool.name, tool.parametersSchema));
|
|
810
|
+
toolCallResults.push(ToolResultPart2(tool.name, tool.successSchema, tool.failureSchema));
|
|
811
|
+
}
|
|
812
|
+
return Schema3.Union(TextPart2, ReasoningPart2, FilePart2, DocumentSourcePart, UrlSourcePart, ResponseMetadataPart, FinishPart, ...toolCalls, ...toolCallResults);
|
|
813
|
+
};
|
|
814
|
+
var StreamPart = (toolkit) => {
|
|
815
|
+
const toolCalls = [];
|
|
816
|
+
const toolCallResults = [];
|
|
817
|
+
for (const tool of Object.values(toolkit.tools)) {
|
|
818
|
+
toolCalls.push(ToolCallPart2(tool.name, tool.parametersSchema));
|
|
819
|
+
toolCallResults.push(ToolResultPart2(tool.name, tool.successSchema, tool.failureSchema));
|
|
820
|
+
}
|
|
821
|
+
return Schema3.Union(TextStartPart, TextDeltaPart, TextEndPart, ReasoningStartPart, ReasoningDeltaPart, ReasoningEndPart, ToolParamsStartPart, ToolParamsDeltaPart, ToolParamsEndPart, FilePart2, DocumentSourcePart, UrlSourcePart, ResponseMetadataPart, FinishPart, ErrorPart, ...toolCalls, ...toolCallResults);
|
|
822
|
+
};
|
|
823
|
+
var ProviderMetadata = Schema3.Record({
|
|
824
|
+
key: Schema3.String,
|
|
825
|
+
value: Schema3.UndefinedOr(Schema3.Record({
|
|
826
|
+
key: Schema3.String,
|
|
827
|
+
value: Schema3.Unknown
|
|
828
|
+
}))
|
|
829
|
+
});
|
|
830
|
+
var makePart2 = (type, params) => ({
|
|
831
|
+
...params,
|
|
832
|
+
[PartTypeId2]: PartTypeId2,
|
|
833
|
+
type,
|
|
834
|
+
metadata: params.metadata ?? {}
|
|
835
|
+
});
|
|
836
|
+
var TextPart2 = Schema3.Struct({
|
|
837
|
+
type: Schema3.Literal("text"),
|
|
838
|
+
text: Schema3.String,
|
|
839
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
840
|
+
default: constEmptyObject2
|
|
841
|
+
})
|
|
842
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "TextPart" }));
|
|
843
|
+
var textPart2 = (params) => makePart2("text", params);
|
|
844
|
+
var TextStartPart = Schema3.Struct({
|
|
845
|
+
type: Schema3.Literal("text-start"),
|
|
846
|
+
id: Schema3.String,
|
|
847
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
848
|
+
default: constEmptyObject2
|
|
849
|
+
})
|
|
850
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "TextStartPart" }));
|
|
851
|
+
var textStartPart = (params) => makePart2("text-start", params);
|
|
852
|
+
var TextDeltaPart = Schema3.Struct({
|
|
853
|
+
type: Schema3.Literal("text-delta"),
|
|
854
|
+
id: Schema3.String,
|
|
855
|
+
delta: Schema3.String,
|
|
856
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
857
|
+
default: constEmptyObject2
|
|
858
|
+
})
|
|
859
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "TextDeltaPart" }));
|
|
860
|
+
var textDeltaPart = (params) => makePart2("text-delta", params);
|
|
861
|
+
var TextEndPart = Schema3.Struct({
|
|
862
|
+
type: Schema3.Literal("text-end"),
|
|
863
|
+
id: Schema3.String,
|
|
864
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
865
|
+
default: constEmptyObject2
|
|
866
|
+
})
|
|
867
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "TextEndPart" }));
|
|
868
|
+
var textEndPart = (params) => makePart2("text-end", params);
|
|
869
|
+
var ReasoningPart2 = Schema3.Struct({
|
|
870
|
+
type: Schema3.Literal("reasoning"),
|
|
871
|
+
text: Schema3.String,
|
|
872
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
873
|
+
default: constEmptyObject2
|
|
874
|
+
})
|
|
875
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ReasoningPart" }));
|
|
876
|
+
var reasoningPart2 = (params) => makePart2("reasoning", params);
|
|
877
|
+
var ReasoningStartPart = Schema3.Struct({
|
|
878
|
+
type: Schema3.Literal("reasoning-start"),
|
|
879
|
+
id: Schema3.String,
|
|
880
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
881
|
+
default: constEmptyObject2
|
|
882
|
+
})
|
|
883
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ReasoningStartPart" }));
|
|
884
|
+
var reasoningStartPart = (params) => makePart2("reasoning-start", params);
|
|
885
|
+
var ReasoningDeltaPart = Schema3.Struct({
|
|
886
|
+
type: Schema3.Literal("reasoning-delta"),
|
|
887
|
+
id: Schema3.String,
|
|
888
|
+
delta: Schema3.String,
|
|
889
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
890
|
+
default: constEmptyObject2
|
|
891
|
+
})
|
|
892
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ReasoningDeltaPart" }));
|
|
893
|
+
var reasoningDeltaPart = (params) => makePart2("reasoning-delta", params);
|
|
894
|
+
var ReasoningEndPart = Schema3.Struct({
|
|
895
|
+
type: Schema3.Literal("reasoning-end"),
|
|
896
|
+
id: Schema3.String,
|
|
897
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
898
|
+
default: constEmptyObject2
|
|
899
|
+
})
|
|
900
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ReasoningEndPart" }));
|
|
901
|
+
var reasoningEndPart = (params) => makePart2("reasoning-end", params);
|
|
902
|
+
var ToolParamsStartPart = Schema3.Struct({
|
|
903
|
+
type: Schema3.Literal("tool-params-start"),
|
|
904
|
+
id: Schema3.String,
|
|
905
|
+
name: Schema3.String,
|
|
906
|
+
providerName: Schema3.optional(Schema3.String),
|
|
907
|
+
providerExecuted: Schema3.optionalWith(Schema3.Boolean, {
|
|
908
|
+
default: import_Function2.constFalse
|
|
909
|
+
}),
|
|
910
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
911
|
+
default: constEmptyObject2
|
|
912
|
+
})
|
|
913
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ToolParamsStartPart" }));
|
|
914
|
+
var toolParamsStartPart = (params) => makePart2("tool-params-start", params);
|
|
915
|
+
var ToolParamsDeltaPart = Schema3.Struct({
|
|
916
|
+
type: Schema3.Literal("tool-params-delta"),
|
|
917
|
+
id: Schema3.String,
|
|
918
|
+
delta: Schema3.String,
|
|
919
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
920
|
+
default: constEmptyObject2
|
|
921
|
+
})
|
|
922
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ToolParamsDeltaPart" }));
|
|
923
|
+
var toolParamsDeltaPart = (params) => makePart2("tool-params-delta", params);
|
|
924
|
+
var ToolParamsEndPart = Schema3.Struct({
|
|
925
|
+
type: Schema3.Literal("tool-params-end"),
|
|
926
|
+
id: Schema3.String,
|
|
927
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
928
|
+
default: constEmptyObject2
|
|
929
|
+
})
|
|
930
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ToolParamsEndPart" }));
|
|
931
|
+
var toolParamsEndPart = (params) => makePart2("tool-params-end", params);
|
|
932
|
+
var ToolCallPart2 = (name, params) => Schema3.Struct({
|
|
933
|
+
type: Schema3.Literal("tool-call"),
|
|
934
|
+
id: Schema3.String,
|
|
935
|
+
name: Schema3.Literal(name),
|
|
936
|
+
params,
|
|
937
|
+
providerName: Schema3.optional(Schema3.String),
|
|
938
|
+
providerExecuted: Schema3.optionalWith(Schema3.Boolean, {
|
|
939
|
+
default: import_Function2.constFalse
|
|
940
|
+
}),
|
|
941
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
942
|
+
default: constEmptyObject2
|
|
943
|
+
})
|
|
944
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ToolCallPart" }));
|
|
945
|
+
var toolCallPart2 = (params) => makePart2("tool-call", params);
|
|
946
|
+
var ToolResultPart2 = (name, success, failure) => {
|
|
947
|
+
const Base = Schema3.Struct({
|
|
948
|
+
id: Schema3.String,
|
|
949
|
+
type: Schema3.Literal("tool-result"),
|
|
950
|
+
providerName: Schema3.optional(Schema3.String),
|
|
951
|
+
isFailure: Schema3.Boolean
|
|
952
|
+
});
|
|
953
|
+
const ResultSchema = Schema3.Union(success, failure);
|
|
954
|
+
const Encoded = Schema3.Struct({
|
|
955
|
+
...Base.fields,
|
|
956
|
+
name: Schema3.String,
|
|
957
|
+
result: Schema3.encodedSchema(ResultSchema),
|
|
958
|
+
providerExecuted: Schema3.optional(Schema3.Boolean),
|
|
959
|
+
metadata: Schema3.optional(ProviderMetadata)
|
|
960
|
+
});
|
|
961
|
+
const Decoded = Schema3.Struct({
|
|
962
|
+
...Base.fields,
|
|
963
|
+
[PartTypeId2]: Schema3.Literal(PartTypeId2),
|
|
964
|
+
name: Schema3.Literal(name),
|
|
965
|
+
result: Schema3.typeSchema(ResultSchema),
|
|
966
|
+
encodedResult: Schema3.encodedSchema(ResultSchema),
|
|
967
|
+
providerExecuted: Schema3.Boolean,
|
|
968
|
+
metadata: ProviderMetadata
|
|
969
|
+
});
|
|
970
|
+
const decodeResult = ParseResult2.decode(ResultSchema);
|
|
971
|
+
const encodeResult = ParseResult2.encode(ResultSchema);
|
|
972
|
+
return Schema3.transformOrFail(Encoded, Decoded, {
|
|
973
|
+
strict: true,
|
|
974
|
+
decode: Effect3.fnUntraced(function* (encoded) {
|
|
975
|
+
const decoded = yield* decodeResult(encoded.result);
|
|
976
|
+
const providerExecuted = encoded.providerExecuted ?? false;
|
|
977
|
+
return {
|
|
978
|
+
...encoded,
|
|
979
|
+
[PartTypeId2]: PartTypeId2,
|
|
980
|
+
name: encoded.name,
|
|
981
|
+
result: decoded,
|
|
982
|
+
encodedResult: encoded.result,
|
|
983
|
+
metadata: encoded.metadata ?? {},
|
|
984
|
+
providerExecuted
|
|
985
|
+
};
|
|
986
|
+
}),
|
|
987
|
+
encode: Effect3.fnUntraced(function* (decoded) {
|
|
988
|
+
const encoded = yield* encodeResult(decoded.result);
|
|
989
|
+
return {
|
|
990
|
+
...decoded,
|
|
991
|
+
result: encoded,
|
|
992
|
+
...decoded.metadata ?? {},
|
|
993
|
+
...decoded.providerName ? { providerName: decoded.providerName } : {},
|
|
994
|
+
...decoded.providerExecuted ? { providerExecuted: true } : {}
|
|
995
|
+
};
|
|
996
|
+
})
|
|
997
|
+
}).annotations({ identifier: `ToolResultPart(${name})` });
|
|
998
|
+
};
|
|
999
|
+
var toolResultPart2 = (params) => makePart2("tool-result", params);
|
|
1000
|
+
var FilePart2 = Schema3.Struct({
|
|
1001
|
+
type: Schema3.Literal("file"),
|
|
1002
|
+
mediaType: Schema3.String,
|
|
1003
|
+
data: Schema3.Uint8ArrayFromBase64,
|
|
1004
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1005
|
+
default: constEmptyObject2
|
|
1006
|
+
})
|
|
1007
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "FilePart" }));
|
|
1008
|
+
var filePart2 = (params) => makePart2("file", params);
|
|
1009
|
+
var DocumentSourcePart = Schema3.Struct({
|
|
1010
|
+
type: Schema3.Literal("source"),
|
|
1011
|
+
sourceType: Schema3.Literal("document"),
|
|
1012
|
+
id: Schema3.String,
|
|
1013
|
+
mediaType: Schema3.String,
|
|
1014
|
+
title: Schema3.String,
|
|
1015
|
+
fileName: Schema3.optional(Schema3.String),
|
|
1016
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1017
|
+
default: constEmptyObject2
|
|
1018
|
+
})
|
|
1019
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "DocumentSourcePart" }));
|
|
1020
|
+
var documentSourcePart = (params) => makePart2("source", { ...params, sourceType: "document" });
|
|
1021
|
+
var UrlSourcePart = Schema3.Struct({
|
|
1022
|
+
type: Schema3.Literal("source"),
|
|
1023
|
+
sourceType: Schema3.Literal("url"),
|
|
1024
|
+
id: Schema3.String,
|
|
1025
|
+
url: Schema3.URL,
|
|
1026
|
+
title: Schema3.String,
|
|
1027
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1028
|
+
default: constEmptyObject2
|
|
1029
|
+
})
|
|
1030
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "UrlSourcePart" }));
|
|
1031
|
+
var urlSourcePart = (params) => makePart2("source", { ...params, sourceType: "url" });
|
|
1032
|
+
var ResponseMetadataPart = Schema3.Struct({
|
|
1033
|
+
type: Schema3.Literal("response-metadata"),
|
|
1034
|
+
id: Schema3.optionalWith(Schema3.String, { as: "Option" }),
|
|
1035
|
+
modelId: Schema3.optionalWith(Schema3.String, { as: "Option" }),
|
|
1036
|
+
timestamp: Schema3.optionalWith(Schema3.DateTimeUtc, { as: "Option" }),
|
|
1037
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1038
|
+
default: constEmptyObject2
|
|
1039
|
+
})
|
|
1040
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ResponseMetadataPart" }));
|
|
1041
|
+
var responseMetadataPart = (params) => makePart2("response-metadata", params);
|
|
1042
|
+
var FinishReason = Schema3.Literal("stop", "length", "content-filter", "tool-calls", "error", "pause", "other", "unknown");
|
|
1043
|
+
|
|
1044
|
+
class Usage extends Schema3.Class("@effect/ai/AiResponse/Usage")({
|
|
1045
|
+
inputTokens: Schema3.UndefinedOr(Schema3.Number),
|
|
1046
|
+
outputTokens: Schema3.UndefinedOr(Schema3.Number),
|
|
1047
|
+
totalTokens: Schema3.UndefinedOr(Schema3.Number),
|
|
1048
|
+
reasoningTokens: Schema3.optional(Schema3.Number),
|
|
1049
|
+
cachedInputTokens: Schema3.optional(Schema3.Number)
|
|
1050
|
+
}) {
|
|
1051
|
+
}
|
|
1052
|
+
var FinishPart = Schema3.Struct({
|
|
1053
|
+
type: Schema3.Literal("finish"),
|
|
1054
|
+
reason: FinishReason,
|
|
1055
|
+
usage: Usage,
|
|
1056
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1057
|
+
default: constEmptyObject2
|
|
1058
|
+
})
|
|
1059
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "FinishPart" }));
|
|
1060
|
+
var finishPart = (params) => makePart2("finish", params);
|
|
1061
|
+
var ErrorPart = Schema3.Struct({
|
|
1062
|
+
type: Schema3.Literal("error"),
|
|
1063
|
+
error: Schema3.Unknown,
|
|
1064
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1065
|
+
default: constEmptyObject2
|
|
1066
|
+
})
|
|
1067
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ErrorPart" }));
|
|
1068
|
+
var errorPart = (params) => makePart2("error", params);
|
|
1069
|
+
var ObjectDeltaPart = Schema3.Struct({
|
|
1070
|
+
type: Schema3.Literal("object-delta"),
|
|
1071
|
+
id: Schema3.String,
|
|
1072
|
+
delta: Schema3.String,
|
|
1073
|
+
accumulated: Schema3.String,
|
|
1074
|
+
partial: Schema3.Unknown,
|
|
1075
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1076
|
+
default: constEmptyObject2
|
|
1077
|
+
})
|
|
1078
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ObjectDeltaPart" }));
|
|
1079
|
+
var objectDeltaPart = (params) => makePart2("object-delta", params);
|
|
1080
|
+
var ObjectDonePart = Schema3.Struct({
|
|
1081
|
+
type: Schema3.Literal("object-done"),
|
|
1082
|
+
id: Schema3.String,
|
|
1083
|
+
value: Schema3.Unknown,
|
|
1084
|
+
raw: Schema3.String,
|
|
1085
|
+
metadata: Schema3.optionalWith(ProviderMetadata, {
|
|
1086
|
+
default: constEmptyObject2
|
|
1087
|
+
})
|
|
1088
|
+
}).pipe(Schema3.attachPropertySignature(PartTypeId2, PartTypeId2), Schema3.annotations({ identifier: "ObjectDonePart" }));
|
|
1089
|
+
var objectDonePart = (params) => makePart2("object-done", params);
|
|
1090
|
+
|
|
1091
|
+
// packages/ariadne/src/Telemetry.ts
|
|
1092
|
+
var exports_Telemetry = {};
|
|
1093
|
+
__export(exports_Telemetry, {
|
|
1094
|
+
addSpanAttributes: () => addSpanAttributes,
|
|
1095
|
+
addGenAIAnnotations: () => addGenAIAnnotations,
|
|
1096
|
+
CurrentSpanTransformer: () => CurrentSpanTransformer
|
|
1097
|
+
});
|
|
1098
|
+
var Context2 = __toESM(require("effect/Context"));
|
|
1099
|
+
var import_Function3 = require("effect/Function");
|
|
1100
|
+
var Predicate5 = __toESM(require("effect/Predicate"));
|
|
1101
|
+
var String5 = __toESM(require("effect/String"));
|
|
1102
|
+
var addSpanAttributes = (keyPrefix, transformKey) => (span, attributes) => {
|
|
1103
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
1104
|
+
if (Predicate5.isNotNullable(value)) {
|
|
1105
|
+
span.attribute(`${keyPrefix}.${transformKey(key)}`, value);
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
};
|
|
1109
|
+
var addSpanBaseAttributes = addSpanAttributes("gen_ai", String5.camelToSnake);
|
|
1110
|
+
var addSpanOperationAttributes = addSpanAttributes("gen_ai.operation", String5.camelToSnake);
|
|
1111
|
+
var addSpanRequestAttributes = addSpanAttributes("gen_ai.request", String5.camelToSnake);
|
|
1112
|
+
var addSpanResponseAttributes = addSpanAttributes("gen_ai.response", String5.camelToSnake);
|
|
1113
|
+
var addSpanTokenAttributes = addSpanAttributes("gen_ai.token", String5.camelToSnake);
|
|
1114
|
+
var addSpanUsageAttributes = addSpanAttributes("gen_ai.usage", String5.camelToSnake);
|
|
1115
|
+
var addGenAIAnnotations = import_Function3.dual(2, (span, options) => {
|
|
1116
|
+
addSpanBaseAttributes(span, { system: options.system });
|
|
1117
|
+
if (Predicate5.isNotNullable(options.operation))
|
|
1118
|
+
addSpanOperationAttributes(span, options.operation);
|
|
1119
|
+
if (Predicate5.isNotNullable(options.request))
|
|
1120
|
+
addSpanRequestAttributes(span, options.request);
|
|
1121
|
+
if (Predicate5.isNotNullable(options.response))
|
|
1122
|
+
addSpanResponseAttributes(span, options.response);
|
|
1123
|
+
if (Predicate5.isNotNullable(options.token))
|
|
1124
|
+
addSpanTokenAttributes(span, options.token);
|
|
1125
|
+
if (Predicate5.isNotNullable(options.usage))
|
|
1126
|
+
addSpanUsageAttributes(span, options.usage);
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
class CurrentSpanTransformer extends Context2.Tag("@effect/ai/Telemetry/CurrentSpanTransformer")() {
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
// packages/ariadne/src/Toolkit.ts
|
|
1133
|
+
var exports_Toolkit = {};
|
|
1134
|
+
__export(exports_Toolkit, {
|
|
1135
|
+
merge: () => merge5,
|
|
1136
|
+
make: () => make4,
|
|
1137
|
+
empty: () => empty3,
|
|
1138
|
+
TypeId: () => TypeId4
|
|
1139
|
+
});
|
|
1140
|
+
var Context4 = __toESM(require("effect/Context"));
|
|
1141
|
+
var Effect4 = __toESM(require("effect/Effect"));
|
|
1142
|
+
var import_Effectable = require("effect/Effectable");
|
|
1143
|
+
var import_Function5 = require("effect/Function");
|
|
1144
|
+
var import_Inspectable = require("effect/Inspectable");
|
|
1145
|
+
var Layer2 = __toESM(require("effect/Layer"));
|
|
1146
|
+
var ParseResult3 = __toESM(require("effect/ParseResult"));
|
|
1147
|
+
var import_Pipeable3 = require("effect/Pipeable");
|
|
1148
|
+
var Predicate7 = __toESM(require("effect/Predicate"));
|
|
1149
|
+
var Schema5 = __toESM(require("effect/Schema"));
|
|
1150
|
+
|
|
1151
|
+
// packages/ariadne/src/Tool.ts
|
|
1152
|
+
var exports_Tool = {};
|
|
1153
|
+
__export(exports_Tool, {
|
|
1154
|
+
unsafeSecureJsonParse: () => unsafeSecureJsonParse,
|
|
1155
|
+
providerDefined: () => providerDefined,
|
|
1156
|
+
make: () => make3,
|
|
1157
|
+
isUserDefined: () => isUserDefined,
|
|
1158
|
+
isProviderDefined: () => isProviderDefined,
|
|
1159
|
+
getJsonSchemaFromSchemaAst: () => getJsonSchemaFromSchemaAst,
|
|
1160
|
+
getJsonSchema: () => getJsonSchema,
|
|
1161
|
+
getDescriptionFromSchemaAst: () => getDescriptionFromSchemaAst,
|
|
1162
|
+
getDescription: () => getDescription,
|
|
1163
|
+
fromTaggedRequest: () => fromTaggedRequest,
|
|
1164
|
+
TypeId: () => TypeId3,
|
|
1165
|
+
Title: () => Title,
|
|
1166
|
+
Readonly: () => Readonly,
|
|
1167
|
+
ProviderDefinedTypeId: () => ProviderDefinedTypeId,
|
|
1168
|
+
OpenWorld: () => OpenWorld,
|
|
1169
|
+
Idempotent: () => Idempotent,
|
|
1170
|
+
Destructive: () => Destructive
|
|
1171
|
+
});
|
|
1172
|
+
var Context3 = __toESM(require("effect/Context"));
|
|
1173
|
+
var import_Function4 = require("effect/Function");
|
|
1174
|
+
var JsonSchema = __toESM(require("effect/JSONSchema"));
|
|
1175
|
+
var Option2 = __toESM(require("effect/Option"));
|
|
1176
|
+
var import_Pipeable2 = require("effect/Pipeable");
|
|
1177
|
+
var Predicate6 = __toESM(require("effect/Predicate"));
|
|
1178
|
+
var Schema4 = __toESM(require("effect/Schema"));
|
|
1179
|
+
var AST = __toESM(require("effect/SchemaAST"));
|
|
1180
|
+
var TypeId3 = "~@effect/ai/Tool";
|
|
1181
|
+
var ProviderDefinedTypeId = "~@effect/ai/Tool/ProviderDefined";
|
|
1182
|
+
var isUserDefined = (u) => Predicate6.hasProperty(u, TypeId3) && !isProviderDefined(u);
|
|
1183
|
+
var isProviderDefined = (u) => Predicate6.hasProperty(u, ProviderDefinedTypeId);
|
|
1184
|
+
var Proto2 = {
|
|
1185
|
+
[TypeId3]: { _Requirements: import_Function4.identity },
|
|
1186
|
+
pipe() {
|
|
1187
|
+
return import_Pipeable2.pipeArguments(this, arguments);
|
|
1188
|
+
},
|
|
1189
|
+
addDependency() {
|
|
1190
|
+
return userDefinedProto({ ...this });
|
|
1191
|
+
},
|
|
1192
|
+
setParameters(parametersSchema) {
|
|
1193
|
+
return userDefinedProto({
|
|
1194
|
+
...this,
|
|
1195
|
+
parametersSchema: Schema4.isSchema(parametersSchema) ? parametersSchema : Schema4.Struct(parametersSchema)
|
|
1196
|
+
});
|
|
1197
|
+
},
|
|
1198
|
+
setSuccess(successSchema) {
|
|
1199
|
+
return userDefinedProto({
|
|
1200
|
+
...this,
|
|
1201
|
+
successSchema
|
|
1202
|
+
});
|
|
1203
|
+
},
|
|
1204
|
+
setFailure(failureSchema) {
|
|
1205
|
+
return userDefinedProto({
|
|
1206
|
+
...this,
|
|
1207
|
+
failureSchema
|
|
1208
|
+
});
|
|
1209
|
+
},
|
|
1210
|
+
annotate(tag, value) {
|
|
1211
|
+
return userDefinedProto({
|
|
1212
|
+
...this,
|
|
1213
|
+
annotations: Context3.add(this.annotations, tag, value)
|
|
1214
|
+
});
|
|
1215
|
+
},
|
|
1216
|
+
annotateContext(context) {
|
|
1217
|
+
return userDefinedProto({
|
|
1218
|
+
...this,
|
|
1219
|
+
annotations: Context3.merge(this.annotations, context)
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
};
|
|
1223
|
+
var ProviderDefinedProto = {
|
|
1224
|
+
...Proto2,
|
|
1225
|
+
[ProviderDefinedTypeId]: ProviderDefinedTypeId
|
|
1226
|
+
};
|
|
1227
|
+
var userDefinedProto = (options) => {
|
|
1228
|
+
const self = Object.assign(Object.create(Proto2), options);
|
|
1229
|
+
self.id = `@effect/ai/Tool/${options.name}`;
|
|
1230
|
+
return self;
|
|
1231
|
+
};
|
|
1232
|
+
var providerDefinedProto = (options) => Object.assign(Object.create(ProviderDefinedProto), options);
|
|
1233
|
+
var constEmptyStruct = Schema4.Struct({});
|
|
1234
|
+
var make3 = (name, options) => {
|
|
1235
|
+
const successSchema = options?.success ?? Schema4.Void;
|
|
1236
|
+
const failureSchema = options?.failure ?? Schema4.Never;
|
|
1237
|
+
return userDefinedProto({
|
|
1238
|
+
name,
|
|
1239
|
+
description: options?.description,
|
|
1240
|
+
parametersSchema: options?.parameters ? Schema4.Struct(options?.parameters) : constEmptyStruct,
|
|
1241
|
+
successSchema,
|
|
1242
|
+
failureSchema,
|
|
1243
|
+
failureMode: options?.failureMode ?? "error",
|
|
1244
|
+
annotations: Context3.empty()
|
|
1245
|
+
});
|
|
1246
|
+
};
|
|
1247
|
+
var providerDefined = (options) => (args) => {
|
|
1248
|
+
const failureMode = "failureMode" in args ? args.failureMode : undefined;
|
|
1249
|
+
const successSchema = options?.success ?? Schema4.Void;
|
|
1250
|
+
const failureSchema = options?.failure ?? Schema4.Never;
|
|
1251
|
+
return providerDefinedProto({
|
|
1252
|
+
id: options.id,
|
|
1253
|
+
name: options.toolkitName,
|
|
1254
|
+
providerName: options.providerName,
|
|
1255
|
+
args,
|
|
1256
|
+
argsSchema: Schema4.Struct(options.args),
|
|
1257
|
+
requiresHandler: options.requiresHandler ?? false,
|
|
1258
|
+
parametersSchema: options?.parameters ? Schema4.Struct(options?.parameters) : constEmptyStruct,
|
|
1259
|
+
successSchema,
|
|
1260
|
+
failureSchema,
|
|
1261
|
+
failureMode: failureMode ?? "error"
|
|
1262
|
+
});
|
|
1263
|
+
};
|
|
1264
|
+
var fromTaggedRequest = (schema) => userDefinedProto({
|
|
1265
|
+
name: schema._tag,
|
|
1266
|
+
description: Option2.getOrUndefined(AST.getDescriptionAnnotation(schema.ast.to)),
|
|
1267
|
+
parametersSchema: schema,
|
|
1268
|
+
successSchema: schema.success,
|
|
1269
|
+
failureSchema: schema.failure,
|
|
1270
|
+
failureMode: "error",
|
|
1271
|
+
annotations: Context3.empty()
|
|
1272
|
+
});
|
|
1273
|
+
var getDescription = (tool) => {
|
|
1274
|
+
if (Predicate6.isNotUndefined(tool.description)) {
|
|
1275
|
+
return tool.description;
|
|
1276
|
+
}
|
|
1277
|
+
return getDescriptionFromSchemaAst(tool.parametersSchema.ast);
|
|
1278
|
+
};
|
|
1279
|
+
var getDescriptionFromSchemaAst = (ast) => {
|
|
1280
|
+
const annotations3 = ast._tag === "Transformation" ? {
|
|
1281
|
+
...ast.to.annotations,
|
|
1282
|
+
...ast.annotations
|
|
1283
|
+
} : ast.annotations;
|
|
1284
|
+
return AST.DescriptionAnnotationId in annotations3 ? annotations3[AST.DescriptionAnnotationId] : undefined;
|
|
1285
|
+
};
|
|
1286
|
+
var getJsonSchema = (tool) => getJsonSchemaFromSchemaAst(tool.parametersSchema.ast);
|
|
1287
|
+
var getJsonSchemaFromSchemaAst = (ast) => {
|
|
1288
|
+
const props = AST.getPropertySignatures(ast);
|
|
1289
|
+
if (props.length === 0) {
|
|
1290
|
+
return {
|
|
1291
|
+
type: "object",
|
|
1292
|
+
properties: {},
|
|
1293
|
+
required: [],
|
|
1294
|
+
additionalProperties: false
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
const $defs = {};
|
|
1298
|
+
const schema = JsonSchema.fromAST(ast, {
|
|
1299
|
+
definitions: $defs,
|
|
1300
|
+
topLevelReferenceStrategy: "skip"
|
|
1301
|
+
});
|
|
1302
|
+
if (Object.keys($defs).length === 0)
|
|
1303
|
+
return schema;
|
|
1304
|
+
schema.$defs = $defs;
|
|
1305
|
+
return schema;
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
class Title extends Context3.Tag("@effect/ai/Tool/Title")() {
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
class Readonly extends Context3.Reference()("@effect/ai/Tool/Readonly", {
|
|
1312
|
+
defaultValue: import_Function4.constFalse
|
|
1313
|
+
}) {
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
class Destructive extends Context3.Reference()("@effect/ai/Tool/Destructive", {
|
|
1317
|
+
defaultValue: import_Function4.constTrue
|
|
1318
|
+
}) {
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
class Idempotent extends Context3.Reference()("@effect/ai/Tool/Idempotent", {
|
|
1322
|
+
defaultValue: import_Function4.constFalse
|
|
1323
|
+
}) {
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
class OpenWorld extends Context3.Reference()("@effect/ai/Tool/OpenWorld", {
|
|
1327
|
+
defaultValue: import_Function4.constTrue
|
|
1328
|
+
}) {
|
|
1329
|
+
}
|
|
1330
|
+
var suspectProtoRx = /"__proto__"\s*:/;
|
|
1331
|
+
var suspectConstructorRx = /"constructor"\s*:/;
|
|
1332
|
+
function _parse(text) {
|
|
1333
|
+
const obj = JSON.parse(text);
|
|
1334
|
+
if (obj === null || typeof obj !== "object") {
|
|
1335
|
+
return obj;
|
|
1336
|
+
}
|
|
1337
|
+
if (suspectProtoRx.test(text) === false && suspectConstructorRx.test(text) === false) {
|
|
1338
|
+
return obj;
|
|
1339
|
+
}
|
|
1340
|
+
return filter(obj);
|
|
1341
|
+
}
|
|
1342
|
+
function filter(obj) {
|
|
1343
|
+
let next2 = [obj];
|
|
1344
|
+
while (next2.length) {
|
|
1345
|
+
const nodes = next2;
|
|
1346
|
+
next2 = [];
|
|
1347
|
+
for (const node of nodes) {
|
|
1348
|
+
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
1349
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
1350
|
+
}
|
|
1351
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
1352
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
1353
|
+
}
|
|
1354
|
+
for (const key in node) {
|
|
1355
|
+
const value = node[key];
|
|
1356
|
+
if (value && typeof value === "object") {
|
|
1357
|
+
next2.push(value);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return obj;
|
|
1363
|
+
}
|
|
1364
|
+
var unsafeSecureJsonParse = (text) => {
|
|
1365
|
+
const { stackTraceLimit } = Error;
|
|
1366
|
+
Error.stackTraceLimit = 0;
|
|
1367
|
+
try {
|
|
1368
|
+
return _parse(text);
|
|
1369
|
+
} finally {
|
|
1370
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
1371
|
+
}
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
// packages/ariadne/src/Toolkit.ts
|
|
1375
|
+
var TypeId4 = "~@effect/ai/Toolkit";
|
|
1376
|
+
var Proto3 = {
|
|
1377
|
+
...import_Effectable.CommitPrototype,
|
|
1378
|
+
...import_Inspectable.BaseProto,
|
|
1379
|
+
of: import_Function5.identity,
|
|
1380
|
+
toContext(build) {
|
|
1381
|
+
return Effect4.gen(this, function* () {
|
|
1382
|
+
const context2 = yield* Effect4.context();
|
|
1383
|
+
const handlers = Effect4.isEffect(build) ? yield* build : build;
|
|
1384
|
+
const contextMap = new Map;
|
|
1385
|
+
for (const [name, handler] of Object.entries(handlers)) {
|
|
1386
|
+
const tool = this.tools[name];
|
|
1387
|
+
contextMap.set(tool.id, { handler, context: context2 });
|
|
1388
|
+
}
|
|
1389
|
+
return Context4.unsafeMake(contextMap);
|
|
1390
|
+
});
|
|
1391
|
+
},
|
|
1392
|
+
toLayer(build) {
|
|
1393
|
+
return Layer2.scopedContext(this.toContext(build));
|
|
1394
|
+
},
|
|
1395
|
+
commit() {
|
|
1396
|
+
return Effect4.gen(this, function* () {
|
|
1397
|
+
const tools = this.tools;
|
|
1398
|
+
const context2 = yield* Effect4.context();
|
|
1399
|
+
const schemasCache = new WeakMap;
|
|
1400
|
+
const getSchemas = (tool) => {
|
|
1401
|
+
let schemas = schemasCache.get(tool);
|
|
1402
|
+
if (Predicate7.isUndefined(schemas)) {
|
|
1403
|
+
const handler = context2.unsafeMap.get(tool.id);
|
|
1404
|
+
const decodeParameters = Schema5.decodeUnknown(tool.parametersSchema);
|
|
1405
|
+
const resultSchema = Schema5.Union(tool.successSchema, tool.failureSchema);
|
|
1406
|
+
const validateResult = Schema5.validate(resultSchema);
|
|
1407
|
+
const encodeResult = Schema5.encodeUnknown(resultSchema);
|
|
1408
|
+
schemas = {
|
|
1409
|
+
context: handler.context,
|
|
1410
|
+
handler: handler.handler,
|
|
1411
|
+
decodeParameters,
|
|
1412
|
+
validateResult,
|
|
1413
|
+
encodeResult
|
|
1414
|
+
};
|
|
1415
|
+
schemasCache.set(tool, schemas);
|
|
1416
|
+
}
|
|
1417
|
+
return schemas;
|
|
1418
|
+
};
|
|
1419
|
+
const handle = Effect4.fn("Toolkit.handle", {
|
|
1420
|
+
captureStackTrace: false
|
|
1421
|
+
})(function* (name, params) {
|
|
1422
|
+
yield* Effect4.annotateCurrentSpan({
|
|
1423
|
+
tool: name,
|
|
1424
|
+
parameters: params
|
|
1425
|
+
});
|
|
1426
|
+
const tool = tools[name];
|
|
1427
|
+
if (Predicate7.isUndefined(tool)) {
|
|
1428
|
+
const toolNames = Object.keys(tools).join(",");
|
|
1429
|
+
return yield* new MalformedOutput({
|
|
1430
|
+
module: "Toolkit",
|
|
1431
|
+
method: `${name}.handle`,
|
|
1432
|
+
description: `Failed to find tool with name '${name}' in toolkit - available tools: ${toolNames}`
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
const schemas = getSchemas(tool);
|
|
1436
|
+
const decodedParams = yield* Effect4.mapError(schemas.decodeParameters(params), (cause) => new MalformedOutput({
|
|
1437
|
+
module: "Toolkit",
|
|
1438
|
+
method: `${name}.handle`,
|
|
1439
|
+
description: `Failed to decode tool call parameters for tool '${name}' from:
|
|
1440
|
+
'${JSON.stringify(params, undefined, 2)}'`,
|
|
1441
|
+
cause
|
|
1442
|
+
}));
|
|
1443
|
+
const { isFailure, result } = yield* schemas.handler(decodedParams).pipe(Effect4.map((result2) => ({ result: result2, isFailure: false })), Effect4.catchAll((error) => tool.failureMode === "error" ? Effect4.fail(error) : Effect4.succeed({
|
|
1444
|
+
result: error,
|
|
1445
|
+
isFailure: true
|
|
1446
|
+
})), Effect4.tap(({ result: result2 }) => schemas.validateResult(result2)), Effect4.mapInputContext((input) => Context4.merge(schemas.context, input)), Effect4.mapError((cause) => ParseResult3.isParseError(cause) ? new MalformedInput({
|
|
1447
|
+
module: "Toolkit",
|
|
1448
|
+
method: `${name}.handle`,
|
|
1449
|
+
description: `Failed to validate tool call result for tool '${name}'`,
|
|
1450
|
+
cause
|
|
1451
|
+
}) : cause));
|
|
1452
|
+
const encodedResult = yield* Effect4.mapError(schemas.encodeResult(result), (cause) => new MalformedInput({
|
|
1453
|
+
module: "Toolkit",
|
|
1454
|
+
method: `${name}.handle`,
|
|
1455
|
+
description: `Failed to encode tool call result for tool '${name}'`,
|
|
1456
|
+
cause
|
|
1457
|
+
}));
|
|
1458
|
+
return {
|
|
1459
|
+
isFailure,
|
|
1460
|
+
result,
|
|
1461
|
+
encodedResult
|
|
1462
|
+
};
|
|
1463
|
+
});
|
|
1464
|
+
return {
|
|
1465
|
+
tools,
|
|
1466
|
+
handle
|
|
1467
|
+
};
|
|
1468
|
+
});
|
|
1469
|
+
},
|
|
1470
|
+
toJSON() {
|
|
1471
|
+
return {
|
|
1472
|
+
_id: "@effect/ai/Toolkit",
|
|
1473
|
+
tools: Array.from(Object.values(this.tools)).map((tool) => tool.name)
|
|
1474
|
+
};
|
|
1475
|
+
},
|
|
1476
|
+
pipe() {
|
|
1477
|
+
return import_Pipeable3.pipeArguments(this, arguments);
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
var makeProto = (tools) => Object.assign(() => {}, Proto3, { tools });
|
|
1481
|
+
var resolveInput = (...tools) => {
|
|
1482
|
+
const output = {};
|
|
1483
|
+
for (const tool of tools) {
|
|
1484
|
+
const value = Schema5.isSchema(tool) ? fromTaggedRequest(tool) : tool;
|
|
1485
|
+
output[tool.name] = value;
|
|
1486
|
+
}
|
|
1487
|
+
return output;
|
|
1488
|
+
};
|
|
1489
|
+
var empty3 = makeProto({});
|
|
1490
|
+
var make4 = (...tools) => makeProto(resolveInput(...tools));
|
|
1491
|
+
var merge5 = (...toolkits) => {
|
|
1492
|
+
const tools = {};
|
|
1493
|
+
for (const toolkit of toolkits) {
|
|
1494
|
+
for (const [name, tool] of Object.entries(toolkit.tools)) {
|
|
1495
|
+
tools[name] = tool;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
return makeProto(tools);
|
|
1499
|
+
};
|
|
1500
|
+
|
|
1501
|
+
// packages/ariadne/src/LanguageModel.ts
|
|
1502
|
+
class LanguageModel extends Context5.Tag("@effect/ai/LanguageModel")() {
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
class GenerateTextResponse {
|
|
1506
|
+
content;
|
|
1507
|
+
constructor(content) {
|
|
1508
|
+
this.content = content;
|
|
1509
|
+
}
|
|
1510
|
+
get text() {
|
|
1511
|
+
const text = [];
|
|
1512
|
+
for (const part of this.content) {
|
|
1513
|
+
if (part.type === "text") {
|
|
1514
|
+
text.push(part.text);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
return text.join("");
|
|
1518
|
+
}
|
|
1519
|
+
get reasoning() {
|
|
1520
|
+
return this.content.filter((part) => part.type === "reasoning");
|
|
1521
|
+
}
|
|
1522
|
+
get reasoningText() {
|
|
1523
|
+
const text = [];
|
|
1524
|
+
for (const part of this.content) {
|
|
1525
|
+
if (part.type === "reasoning") {
|
|
1526
|
+
text.push(part.text);
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
return text.length === 0 ? undefined : text.join("");
|
|
1530
|
+
}
|
|
1531
|
+
get toolCalls() {
|
|
1532
|
+
return this.content.filter((part) => part.type === "tool-call");
|
|
1533
|
+
}
|
|
1534
|
+
get toolResults() {
|
|
1535
|
+
return this.content.filter((part) => part.type === "tool-result");
|
|
1536
|
+
}
|
|
1537
|
+
get finishReason() {
|
|
1538
|
+
const finishPart2 = this.content.find((part) => part.type === "finish");
|
|
1539
|
+
return Predicate8.isUndefined(finishPart2) ? "unknown" : finishPart2.reason;
|
|
1540
|
+
}
|
|
1541
|
+
get usage() {
|
|
1542
|
+
const finishPart2 = this.content.find((part) => part.type === "finish");
|
|
1543
|
+
if (Predicate8.isUndefined(finishPart2)) {
|
|
1544
|
+
return new Usage({
|
|
1545
|
+
inputTokens: undefined,
|
|
1546
|
+
outputTokens: undefined,
|
|
1547
|
+
totalTokens: undefined,
|
|
1548
|
+
reasoningTokens: undefined,
|
|
1549
|
+
cachedInputTokens: undefined
|
|
1550
|
+
});
|
|
1551
|
+
}
|
|
1552
|
+
return finishPart2.usage;
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
class GenerateObjectResponse extends GenerateTextResponse {
|
|
1557
|
+
value;
|
|
1558
|
+
constructor(value, content) {
|
|
1559
|
+
super(content);
|
|
1560
|
+
this.value = value;
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
var make6 = Effect5.fnUntraced(function* (params) {
|
|
1564
|
+
const parentSpanTransformer = yield* Effect5.serviceOption(CurrentSpanTransformer);
|
|
1565
|
+
const getSpanTransformer = Effect5.serviceOption(CurrentSpanTransformer).pipe(Effect5.map(Option3.orElse(() => parentSpanTransformer)));
|
|
1566
|
+
const idGenerator = yield* Effect5.serviceOption(IdGenerator).pipe(Effect5.map(Option3.getOrElse(() => defaultIdGenerator)));
|
|
1567
|
+
const generateText = (options) => Effect5.useSpan("LanguageModel.generateText", {
|
|
1568
|
+
captureStackTrace: false,
|
|
1569
|
+
attributes: {
|
|
1570
|
+
concurrency: options.concurrency,
|
|
1571
|
+
toolChoice: options.toolChoice
|
|
1572
|
+
}
|
|
1573
|
+
}, Effect5.fnUntraced(function* (span) {
|
|
1574
|
+
const spanTransformer = yield* getSpanTransformer;
|
|
1575
|
+
const providerOptions = {
|
|
1576
|
+
prompt: make2(options.prompt),
|
|
1577
|
+
tools: [],
|
|
1578
|
+
toolChoice: "none",
|
|
1579
|
+
responseFormat: { type: "text" },
|
|
1580
|
+
mcpServers: options.mcpServers ?? [],
|
|
1581
|
+
span
|
|
1582
|
+
};
|
|
1583
|
+
const content = yield* generateContent(options, providerOptions);
|
|
1584
|
+
applySpanTransformer(spanTransformer, content, providerOptions);
|
|
1585
|
+
return new GenerateTextResponse(content);
|
|
1586
|
+
}, Effect5.catchTag("ParseError", (error) => MalformedOutput.fromParseError({
|
|
1587
|
+
module: "LanguageModel",
|
|
1588
|
+
method: "generateText",
|
|
1589
|
+
error
|
|
1590
|
+
})), (effect2, span) => Effect5.withParentSpan(effect2, span), Effect5.provideService(IdGenerator, idGenerator)));
|
|
1591
|
+
const generateObject = (options) => {
|
|
1592
|
+
const schema = options.schema;
|
|
1593
|
+
const objectName = getObjectName(options.objectName, schema);
|
|
1594
|
+
return Effect5.useSpan("LanguageModel.generateObject", {
|
|
1595
|
+
captureStackTrace: false,
|
|
1596
|
+
attributes: {
|
|
1597
|
+
objectName,
|
|
1598
|
+
concurrency: options.concurrency,
|
|
1599
|
+
toolChoice: options.toolChoice
|
|
1600
|
+
}
|
|
1601
|
+
}, Effect5.fnUntraced(function* (span) {
|
|
1602
|
+
const spanTransformer = yield* getSpanTransformer;
|
|
1603
|
+
const providerOptions = {
|
|
1604
|
+
prompt: make2(options.prompt),
|
|
1605
|
+
tools: [],
|
|
1606
|
+
toolChoice: "none",
|
|
1607
|
+
responseFormat: {
|
|
1608
|
+
type: "json",
|
|
1609
|
+
objectName,
|
|
1610
|
+
schema
|
|
1611
|
+
},
|
|
1612
|
+
mcpServers: options.mcpServers ?? [],
|
|
1613
|
+
span
|
|
1614
|
+
};
|
|
1615
|
+
const content = yield* generateContent(options, providerOptions);
|
|
1616
|
+
applySpanTransformer(spanTransformer, content, providerOptions);
|
|
1617
|
+
const finishPart2 = content.find((part) => part.type === "finish");
|
|
1618
|
+
const finishReason = finishPart2?.reason ?? "unknown";
|
|
1619
|
+
if (finishReason === "tool-calls") {
|
|
1620
|
+
return new GenerateObjectResponse(undefined, content);
|
|
1621
|
+
}
|
|
1622
|
+
const value = yield* resolveStructuredOutput(content, schema);
|
|
1623
|
+
return new GenerateObjectResponse(value, content);
|
|
1624
|
+
}, Effect5.catchTag("ParseError", (error) => MalformedOutput.fromParseError({
|
|
1625
|
+
module: "LanguageModel",
|
|
1626
|
+
method: "generateText",
|
|
1627
|
+
error
|
|
1628
|
+
})), (effect2, span) => Effect5.withParentSpan(effect2, span), Effect5.provideService(IdGenerator, idGenerator)));
|
|
1629
|
+
};
|
|
1630
|
+
const streamText = Effect5.fnUntraced(function* (options) {
|
|
1631
|
+
const span = yield* Effect5.makeSpanScoped("LanguageModel.streamText", {
|
|
1632
|
+
captureStackTrace: false,
|
|
1633
|
+
attributes: {
|
|
1634
|
+
concurrency: options.concurrency,
|
|
1635
|
+
toolChoice: options.toolChoice
|
|
1636
|
+
}
|
|
1637
|
+
});
|
|
1638
|
+
const providerOptions = {
|
|
1639
|
+
prompt: make2(options.prompt),
|
|
1640
|
+
tools: [],
|
|
1641
|
+
toolChoice: "none",
|
|
1642
|
+
responseFormat: { type: "text" },
|
|
1643
|
+
mcpServers: options.mcpServers ?? [],
|
|
1644
|
+
span
|
|
1645
|
+
};
|
|
1646
|
+
const stream = yield* streamContent(options, providerOptions);
|
|
1647
|
+
const spanTransformer = yield* getSpanTransformer;
|
|
1648
|
+
if (Option3.isNone(spanTransformer)) {
|
|
1649
|
+
return stream;
|
|
1650
|
+
}
|
|
1651
|
+
let content = [];
|
|
1652
|
+
return stream.pipe(Stream.mapChunks((chunk) => {
|
|
1653
|
+
content = [...content, ...chunk];
|
|
1654
|
+
return chunk;
|
|
1655
|
+
}), Stream.ensuring(Effect5.sync(() => {
|
|
1656
|
+
spanTransformer.value({
|
|
1657
|
+
...providerOptions,
|
|
1658
|
+
response: content
|
|
1659
|
+
});
|
|
1660
|
+
})));
|
|
1661
|
+
}, Stream.unwrapScoped, Stream.mapError((error) => ParseResult4.isParseError(error) ? MalformedOutput.fromParseError({
|
|
1662
|
+
module: "LanguageModel",
|
|
1663
|
+
method: "streamText",
|
|
1664
|
+
error
|
|
1665
|
+
}) : error), Stream.provideService(IdGenerator, idGenerator));
|
|
1666
|
+
const generateContent = Effect5.fnUntraced(function* (options, providerOptions) {
|
|
1667
|
+
const toolChoice = options.toolChoice ?? "auto";
|
|
1668
|
+
if (Predicate8.isUndefined(options.toolkit)) {
|
|
1669
|
+
const ResponseSchema2 = Schema6.mutable(Schema6.Array(Part(empty3)));
|
|
1670
|
+
const rawContent2 = yield* params.generateText(providerOptions);
|
|
1671
|
+
const content2 = yield* Schema6.decodeUnknown(ResponseSchema2)(rawContent2);
|
|
1672
|
+
return content2;
|
|
1673
|
+
}
|
|
1674
|
+
const toolkit = yield* resolveToolkit(options.toolkit);
|
|
1675
|
+
if (Object.values(toolkit.tools).length === 0) {
|
|
1676
|
+
const ResponseSchema2 = Schema6.mutable(Schema6.Array(Part(empty3)));
|
|
1677
|
+
const rawContent2 = yield* params.generateText(providerOptions);
|
|
1678
|
+
const content2 = yield* Schema6.decodeUnknown(ResponseSchema2)(rawContent2);
|
|
1679
|
+
return content2;
|
|
1680
|
+
}
|
|
1681
|
+
const tools = typeof toolChoice === "object" && "oneOf" in toolChoice ? Object.values(toolkit.tools).filter((tool) => toolChoice.oneOf.includes(tool.name)) : Object.values(toolkit.tools);
|
|
1682
|
+
providerOptions.tools = tools;
|
|
1683
|
+
providerOptions.toolChoice = toolChoice;
|
|
1684
|
+
const ResponseSchema = Schema6.mutable(Schema6.Array(Part(toolkit)));
|
|
1685
|
+
if (options.disableToolCallResolution === true) {
|
|
1686
|
+
const rawContent2 = yield* params.generateText(providerOptions);
|
|
1687
|
+
const content2 = yield* Schema6.decodeUnknown(ResponseSchema)(rawContent2);
|
|
1688
|
+
return content2;
|
|
1689
|
+
}
|
|
1690
|
+
const rawContent = yield* params.generateText(providerOptions);
|
|
1691
|
+
const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency);
|
|
1692
|
+
const content = yield* Schema6.decodeUnknown(ResponseSchema)(rawContent);
|
|
1693
|
+
return [...content, ...toolResults];
|
|
1694
|
+
});
|
|
1695
|
+
const streamContent = Effect5.fnUntraced(function* (options, providerOptions) {
|
|
1696
|
+
const toolChoice = options.toolChoice ?? "auto";
|
|
1697
|
+
if (Predicate8.isUndefined(options.toolkit)) {
|
|
1698
|
+
const schema = Schema6.ChunkFromSelf(StreamPart(empty3));
|
|
1699
|
+
const decode4 = Schema6.decode(schema);
|
|
1700
|
+
return params.streamText(providerOptions).pipe(Stream.mapChunksEffect(decode4));
|
|
1701
|
+
}
|
|
1702
|
+
const toolkit = Effect5.isEffect(options.toolkit) ? yield* options.toolkit : options.toolkit;
|
|
1703
|
+
if (Object.values(toolkit.tools).length === 0) {
|
|
1704
|
+
const schema = Schema6.ChunkFromSelf(StreamPart(empty3));
|
|
1705
|
+
const decode4 = Schema6.decode(schema);
|
|
1706
|
+
return params.streamText(providerOptions).pipe(Stream.mapChunksEffect(decode4));
|
|
1707
|
+
}
|
|
1708
|
+
const tools = typeof toolChoice === "object" && "oneOf" in toolChoice ? Object.values(toolkit.tools).filter((tool) => toolChoice.oneOf.includes(tool.name)) : Object.values(toolkit.tools);
|
|
1709
|
+
providerOptions.tools = tools;
|
|
1710
|
+
providerOptions.toolChoice = toolChoice;
|
|
1711
|
+
if (options.disableToolCallResolution === true) {
|
|
1712
|
+
const schema = Schema6.ChunkFromSelf(StreamPart(toolkit));
|
|
1713
|
+
const decode4 = Schema6.decode(schema);
|
|
1714
|
+
return params.streamText(providerOptions).pipe(Stream.mapChunksEffect(decode4));
|
|
1715
|
+
}
|
|
1716
|
+
const mailbox = yield* Mailbox.make();
|
|
1717
|
+
const ResponseSchema = Schema6.Array(StreamPart(toolkit));
|
|
1718
|
+
const decode3 = Schema6.decode(ResponseSchema);
|
|
1719
|
+
yield* params.streamText(providerOptions).pipe(Stream.runForEachChunk(Effect5.fnUntraced(function* (chunk) {
|
|
1720
|
+
const rawContent = Chunk.toArray(chunk);
|
|
1721
|
+
const content = yield* decode3(rawContent);
|
|
1722
|
+
yield* mailbox.offerAll(content);
|
|
1723
|
+
const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency);
|
|
1724
|
+
yield* mailbox.offerAll(toolResults);
|
|
1725
|
+
})), Mailbox.into(mailbox), Effect5.forkScoped);
|
|
1726
|
+
return Mailbox.toStream(mailbox);
|
|
1727
|
+
});
|
|
1728
|
+
const streamObject = (options) => {
|
|
1729
|
+
const schema = options.schema;
|
|
1730
|
+
const objectName = getObjectName(options.objectName, schema);
|
|
1731
|
+
const tryParsePartial = (json) => {
|
|
1732
|
+
try {
|
|
1733
|
+
return JSON.parse(json);
|
|
1734
|
+
} catch {
|
|
1735
|
+
try {
|
|
1736
|
+
const trimmed = json.trim();
|
|
1737
|
+
let closed = trimmed;
|
|
1738
|
+
const openBraces = (trimmed.match(/\{/g) || []).length - (trimmed.match(/\}/g) || []).length;
|
|
1739
|
+
const openBrackets = (trimmed.match(/\[/g) || []).length - (trimmed.match(/\]/g) || []).length;
|
|
1740
|
+
for (let i = 0;i < openBrackets; i++)
|
|
1741
|
+
closed += "]";
|
|
1742
|
+
for (let i = 0;i < openBraces; i++)
|
|
1743
|
+
closed += "}";
|
|
1744
|
+
return JSON.parse(closed);
|
|
1745
|
+
} catch {
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
};
|
|
1750
|
+
return Effect5.fnUntraced(function* () {
|
|
1751
|
+
const span = yield* Effect5.makeSpanScoped("LanguageModel.streamObject", {
|
|
1752
|
+
captureStackTrace: false,
|
|
1753
|
+
attributes: { objectName }
|
|
1754
|
+
});
|
|
1755
|
+
const providerOptions = {
|
|
1756
|
+
prompt: make2(options.prompt),
|
|
1757
|
+
tools: [],
|
|
1758
|
+
toolChoice: "none",
|
|
1759
|
+
responseFormat: { type: "json", objectName, schema },
|
|
1760
|
+
mcpServers: options.mcpServers ?? [],
|
|
1761
|
+
span
|
|
1762
|
+
};
|
|
1763
|
+
const decodeSchema = Schema6.ChunkFromSelf(StreamPart(empty3));
|
|
1764
|
+
const decode3 = Schema6.decode(decodeSchema);
|
|
1765
|
+
const decodedStream = params.streamText(providerOptions).pipe(Stream.mapChunksEffect(decode3));
|
|
1766
|
+
const objectId = yield* idGenerator.generateId();
|
|
1767
|
+
let accumulated = "";
|
|
1768
|
+
return decodedStream.pipe(Stream.mapEffect(Effect5.fnUntraced(function* (part) {
|
|
1769
|
+
if (part.type === "text-delta") {
|
|
1770
|
+
accumulated += part.delta;
|
|
1771
|
+
const partial = tryParsePartial(accumulated);
|
|
1772
|
+
return objectDeltaPart({
|
|
1773
|
+
id: objectId,
|
|
1774
|
+
delta: part.delta,
|
|
1775
|
+
accumulated,
|
|
1776
|
+
partial
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
if (part.type === "finish") {
|
|
1780
|
+
const decodeJson = Schema6.decode(Schema6.parseJson(schema));
|
|
1781
|
+
const value = yield* Effect5.mapError(decodeJson(accumulated), (cause) => new MalformedOutput({
|
|
1782
|
+
module: "LanguageModel",
|
|
1783
|
+
method: "streamObject",
|
|
1784
|
+
description: "Generated object failed to conform to provided schema",
|
|
1785
|
+
cause
|
|
1786
|
+
}));
|
|
1787
|
+
return objectDonePart({
|
|
1788
|
+
id: objectId,
|
|
1789
|
+
value,
|
|
1790
|
+
raw: accumulated
|
|
1791
|
+
});
|
|
1792
|
+
}
|
|
1793
|
+
if (part.type === "response-metadata") {
|
|
1794
|
+
return responseMetadataPart({
|
|
1795
|
+
id: part.id,
|
|
1796
|
+
modelId: part.modelId,
|
|
1797
|
+
timestamp: part.timestamp
|
|
1798
|
+
});
|
|
1799
|
+
}
|
|
1800
|
+
if (part.type === "error") {
|
|
1801
|
+
return errorPart({
|
|
1802
|
+
error: part.error
|
|
1803
|
+
});
|
|
1804
|
+
}
|
|
1805
|
+
return;
|
|
1806
|
+
})), Stream.filter((part) => part !== undefined));
|
|
1807
|
+
}, Stream.unwrapScoped, Stream.mapError((error) => ParseResult4.isParseError(error) ? MalformedOutput.fromParseError({
|
|
1808
|
+
module: "LanguageModel",
|
|
1809
|
+
method: "streamObject",
|
|
1810
|
+
error
|
|
1811
|
+
}) : error), Stream.provideService(IdGenerator, idGenerator))();
|
|
1812
|
+
};
|
|
1813
|
+
return {
|
|
1814
|
+
generateText,
|
|
1815
|
+
generateObject,
|
|
1816
|
+
streamText,
|
|
1817
|
+
streamObject
|
|
1818
|
+
};
|
|
1819
|
+
});
|
|
1820
|
+
var generateText = Effect5.serviceFunctionEffect(LanguageModel, (model) => model.generateText);
|
|
1821
|
+
var generateObject = Effect5.serviceFunctionEffect(LanguageModel, (model) => model.generateObject);
|
|
1822
|
+
var streamText = (options) => Stream.unwrap(LanguageModel.pipe(Effect5.map((model) => model.streamText(options))));
|
|
1823
|
+
var streamObject = (options) => Stream.unwrap(LanguageModel.pipe(Effect5.map((model) => model.streamObject(options))));
|
|
1824
|
+
var resolveToolCalls = (content, toolkit, concurrency) => {
|
|
1825
|
+
const toolNames = [];
|
|
1826
|
+
const toolCalls = [];
|
|
1827
|
+
for (const part of content) {
|
|
1828
|
+
if (part.type === "tool-call") {
|
|
1829
|
+
toolNames.push(part.name);
|
|
1830
|
+
if (part.providerExecuted === true) {
|
|
1831
|
+
continue;
|
|
1832
|
+
}
|
|
1833
|
+
toolCalls.push(part);
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
return Effect5.forEach(toolCalls, (toolCall) => {
|
|
1837
|
+
return toolkit.handle(toolCall.name, toolCall.params).pipe(Effect5.map(({ encodedResult, isFailure, result }) => makePart2("tool-result", {
|
|
1838
|
+
id: toolCall.id,
|
|
1839
|
+
name: toolCall.name,
|
|
1840
|
+
result,
|
|
1841
|
+
encodedResult,
|
|
1842
|
+
isFailure,
|
|
1843
|
+
providerExecuted: false,
|
|
1844
|
+
...toolCall.providerName !== undefined ? { providerName: toolCall.providerName } : {}
|
|
1845
|
+
})));
|
|
1846
|
+
}, { concurrency });
|
|
1847
|
+
};
|
|
1848
|
+
var resolveToolkit = (toolkit) => Effect5.isEffect(toolkit) ? toolkit : Effect5.succeed(toolkit);
|
|
1849
|
+
var getObjectName = (objectName, schema) => Predicate8.isNotUndefined(objectName) ? objectName : ("_tag" in schema) ? schema._tag : ("identifier" in schema) ? schema.identifier : "generateObject";
|
|
1850
|
+
var resolveStructuredOutput = Effect5.fnUntraced(function* (response, ResultSchema) {
|
|
1851
|
+
const text = [];
|
|
1852
|
+
for (const part of response) {
|
|
1853
|
+
if (part.type === "text") {
|
|
1854
|
+
text.push(part.text);
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
if (text.length === 0) {
|
|
1858
|
+
return yield* new MalformedOutput({
|
|
1859
|
+
module: "LanguageModel",
|
|
1860
|
+
method: "generateObject",
|
|
1861
|
+
description: "No object was generated by the large language model"
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
const decode3 = Schema6.decode(Schema6.parseJson(ResultSchema));
|
|
1865
|
+
return yield* Effect5.mapError(decode3(text.join("")), (cause) => new MalformedOutput({
|
|
1866
|
+
module: "LanguageModel",
|
|
1867
|
+
method: "generateObject",
|
|
1868
|
+
description: "Generated object failed to conform to provided schema",
|
|
1869
|
+
cause
|
|
1870
|
+
}));
|
|
1871
|
+
});
|
|
1872
|
+
var applySpanTransformer = (transformer, response, options) => {
|
|
1873
|
+
if (Option3.isSome(transformer)) {
|
|
1874
|
+
transformer.value({ ...options, response });
|
|
1875
|
+
}
|
|
1876
|
+
};
|
|
1877
|
+
|
|
1878
|
+
// packages/ariadne/src/AgentRunner.ts
|
|
1879
|
+
class Config extends Context6.Tag("ariadne/AgentRunner/Config")() {
|
|
1880
|
+
static getOrUndefined = Effect6.map(Effect6.context(), (context3) => context3.unsafeMap.get(Config.key));
|
|
1881
|
+
}
|
|
1882
|
+
var defaultConfig = Layer3.succeed(Config, {
|
|
1883
|
+
maxTurns: 10
|
|
1884
|
+
});
|
|
1885
|
+
var ReAct = Layer3.effect(LanguageModel, Effect6.gen(function* () {
|
|
1886
|
+
const underlying = yield* LanguageModel;
|
|
1887
|
+
const config = yield* Config.getOrUndefined;
|
|
1888
|
+
const maxTurns = config?.maxTurns ?? 10;
|
|
1889
|
+
const shouldTerminate = (finishReason) => finishReason === "stop" || finishReason === "length" || finishReason === "content-filter" || finishReason === "error" || finishReason === "unknown";
|
|
1890
|
+
const excludeFinishParts = (content) => content.filter((p) => p.type !== "finish");
|
|
1891
|
+
const generateText2 = (options) => Effect6.gen(function* () {
|
|
1892
|
+
const history = yield* Ref.make(make2(options.prompt));
|
|
1893
|
+
let turns = 0;
|
|
1894
|
+
let allContent = [];
|
|
1895
|
+
while (turns < maxTurns) {
|
|
1896
|
+
turns++;
|
|
1897
|
+
const currentPrompt = yield* Ref.get(history);
|
|
1898
|
+
const response = yield* underlying.generateText({
|
|
1899
|
+
...options,
|
|
1900
|
+
prompt: currentPrompt
|
|
1901
|
+
});
|
|
1902
|
+
if (shouldTerminate(response.finishReason)) {
|
|
1903
|
+
allContent = [...allContent, ...response.content];
|
|
1904
|
+
return new GenerateTextResponse(allContent);
|
|
1905
|
+
}
|
|
1906
|
+
allContent = [
|
|
1907
|
+
...allContent,
|
|
1908
|
+
...excludeFinishParts(response.content)
|
|
1909
|
+
];
|
|
1910
|
+
const newPrompt = merge2(currentPrompt, fromResponseParts(response.content));
|
|
1911
|
+
yield* Ref.set(history, newPrompt);
|
|
1912
|
+
}
|
|
1913
|
+
return new GenerateTextResponse(allContent);
|
|
1914
|
+
});
|
|
1915
|
+
const generateObject2 = (options) => Effect6.gen(function* () {
|
|
1916
|
+
const history = yield* Ref.make(make2(options.prompt));
|
|
1917
|
+
let turns = 0;
|
|
1918
|
+
let allContent = [];
|
|
1919
|
+
let finalValue;
|
|
1920
|
+
while (turns < maxTurns) {
|
|
1921
|
+
turns++;
|
|
1922
|
+
const currentPrompt = yield* Ref.get(history);
|
|
1923
|
+
const response = yield* underlying.generateObject({
|
|
1924
|
+
...options,
|
|
1925
|
+
prompt: currentPrompt
|
|
1926
|
+
});
|
|
1927
|
+
finalValue = response.value;
|
|
1928
|
+
if (shouldTerminate(response.finishReason)) {
|
|
1929
|
+
allContent = [...allContent, ...response.content];
|
|
1930
|
+
return new GenerateObjectResponse(finalValue, allContent);
|
|
1931
|
+
}
|
|
1932
|
+
allContent = [
|
|
1933
|
+
...allContent,
|
|
1934
|
+
...excludeFinishParts(response.content)
|
|
1935
|
+
];
|
|
1936
|
+
const newPrompt = merge2(currentPrompt, fromResponseParts(response.content));
|
|
1937
|
+
yield* Ref.set(history, newPrompt);
|
|
1938
|
+
}
|
|
1939
|
+
return new GenerateObjectResponse(finalValue, allContent);
|
|
1940
|
+
});
|
|
1941
|
+
const streamText2 = (options) => {
|
|
1942
|
+
const history = Ref.unsafeMake(make2(options.prompt));
|
|
1943
|
+
let turns = 0;
|
|
1944
|
+
const runTurn = () => Stream2.unwrap(Effect6.gen(function* () {
|
|
1945
|
+
if (turns >= maxTurns) {
|
|
1946
|
+
return Stream2.empty;
|
|
1947
|
+
}
|
|
1948
|
+
turns++;
|
|
1949
|
+
const currentPrompt = yield* Ref.get(history);
|
|
1950
|
+
let accumulatedParts = [];
|
|
1951
|
+
let finishReason = "unknown";
|
|
1952
|
+
const innerStream = underlying.streamText({
|
|
1953
|
+
...options,
|
|
1954
|
+
prompt: currentPrompt
|
|
1955
|
+
}).pipe(Stream2.tap((part) => Effect6.sync(() => {
|
|
1956
|
+
accumulatedParts.push(part);
|
|
1957
|
+
if (part.type === "finish") {
|
|
1958
|
+
finishReason = part.reason;
|
|
1959
|
+
}
|
|
1960
|
+
})));
|
|
1961
|
+
const continueOrEnd = Effect6.gen(function* () {
|
|
1962
|
+
if (!shouldTerminate(finishReason)) {
|
|
1963
|
+
const currentPrompt2 = yield* Ref.get(history);
|
|
1964
|
+
const newPrompt = merge2(currentPrompt2, fromResponseParts(accumulatedParts));
|
|
1965
|
+
yield* Ref.set(history, newPrompt);
|
|
1966
|
+
return runTurn();
|
|
1967
|
+
}
|
|
1968
|
+
return Stream2.empty;
|
|
1969
|
+
});
|
|
1970
|
+
return Stream2.concat(innerStream, Stream2.unwrap(continueOrEnd));
|
|
1971
|
+
}));
|
|
1972
|
+
return runTurn();
|
|
1973
|
+
};
|
|
1974
|
+
const streamObject2 = (options) => underlying.streamObject(options);
|
|
1975
|
+
return {
|
|
1976
|
+
generateText: generateText2,
|
|
1977
|
+
generateObject: generateObject2,
|
|
1978
|
+
streamText: streamText2,
|
|
1979
|
+
streamObject: streamObject2
|
|
1980
|
+
};
|
|
1981
|
+
}));
|
|
1982
|
+
// packages/ariadne/src/Chat.ts
|
|
1983
|
+
var exports_Chat = {};
|
|
1984
|
+
__export(exports_Chat, {
|
|
1985
|
+
makePersisted: () => makePersisted,
|
|
1986
|
+
layerPersisted: () => layerPersisted,
|
|
1987
|
+
fromPrompt: () => fromPrompt,
|
|
1988
|
+
fromJson: () => fromJson,
|
|
1989
|
+
fromExport: () => fromExport,
|
|
1990
|
+
empty: () => empty6,
|
|
1991
|
+
Persistence: () => Persistence,
|
|
1992
|
+
ChatNotFoundError: () => ChatNotFoundError,
|
|
1993
|
+
Chat: () => Chat
|
|
1994
|
+
});
|
|
1995
|
+
var import_Persistence = require("@effect/experimental/Persistence");
|
|
1996
|
+
var Channel = __toESM(require("effect/Channel"));
|
|
1997
|
+
var Chunk2 = __toESM(require("effect/Chunk"));
|
|
1998
|
+
var Context7 = __toESM(require("effect/Context"));
|
|
1999
|
+
var Duration = __toESM(require("effect/Duration"));
|
|
2000
|
+
var Effect7 = __toESM(require("effect/Effect"));
|
|
2001
|
+
var Layer4 = __toESM(require("effect/Layer"));
|
|
2002
|
+
var Option4 = __toESM(require("effect/Option"));
|
|
2003
|
+
var Predicate9 = __toESM(require("effect/Predicate"));
|
|
2004
|
+
var Ref2 = __toESM(require("effect/Ref"));
|
|
2005
|
+
var Schema7 = __toESM(require("effect/Schema"));
|
|
2006
|
+
var Stream3 = __toESM(require("effect/Stream"));
|
|
2007
|
+
class Chat extends Context7.Tag("@effect/ai/Chat")() {
|
|
2008
|
+
}
|
|
2009
|
+
var empty6 = Effect7.gen(function* () {
|
|
2010
|
+
const history = yield* Ref2.make(empty);
|
|
2011
|
+
const context4 = yield* Effect7.context();
|
|
2012
|
+
const semaphore = yield* Effect7.makeSemaphore(1);
|
|
2013
|
+
const provideContext = (effect3) => Effect7.mapInputContext(effect3, (input) => Context7.merge(context4, input));
|
|
2014
|
+
const provideContextStream = (stream) => Stream3.mapInputContext(stream, (input) => Context7.merge(context4, input));
|
|
2015
|
+
const encodeHistory = Schema7.encode(Prompt);
|
|
2016
|
+
const encodeHistoryJson = Schema7.encode(FromJson);
|
|
2017
|
+
return Chat.of({
|
|
2018
|
+
history,
|
|
2019
|
+
export: Ref2.get(history).pipe(Effect7.flatMap(encodeHistory), Effect7.catchTag("ParseError", (error) => MalformedOutput.fromParseError({
|
|
2020
|
+
module: "Chat",
|
|
2021
|
+
method: "exportJson",
|
|
2022
|
+
description: "Failed to encode chat history",
|
|
2023
|
+
error
|
|
2024
|
+
})), Effect7.withSpan("Chat.export")),
|
|
2025
|
+
exportJson: Ref2.get(history).pipe(Effect7.flatMap(encodeHistoryJson), Effect7.catchTag("ParseError", (error) => MalformedOutput.fromParseError({
|
|
2026
|
+
module: "Chat",
|
|
2027
|
+
method: "exportJson",
|
|
2028
|
+
description: "Failed to encode chat history into JSON",
|
|
2029
|
+
error
|
|
2030
|
+
})), Effect7.withSpan("Chat.exportJson")),
|
|
2031
|
+
generateText: Effect7.fnUntraced(function* (options) {
|
|
2032
|
+
const newPrompt = make2(options.prompt);
|
|
2033
|
+
const oldPrompt = yield* Ref2.get(history);
|
|
2034
|
+
const prompt = merge2(oldPrompt, newPrompt);
|
|
2035
|
+
const response = yield* generateText({
|
|
2036
|
+
...options,
|
|
2037
|
+
prompt
|
|
2038
|
+
});
|
|
2039
|
+
const newHistory = merge2(prompt, fromResponseParts(response.content));
|
|
2040
|
+
yield* Ref2.set(history, newHistory);
|
|
2041
|
+
return response;
|
|
2042
|
+
}, provideContext, semaphore.withPermits(1), Effect7.withSpan("Chat.generateText", { captureStackTrace: false })),
|
|
2043
|
+
streamText: Effect7.fnUntraced(function* (options) {
|
|
2044
|
+
let parts = Chunk2.empty();
|
|
2045
|
+
return Stream3.fromChannel(Channel.acquireUseRelease(semaphore.take(1).pipe(Effect7.zipRight(Ref2.get(history)), Effect7.map((history2) => merge2(history2, make2(options.prompt)))), (prompt) => streamText({ ...options, prompt }).pipe(Stream3.mapChunks((chunk) => {
|
|
2046
|
+
parts = Chunk2.appendAll(parts, chunk);
|
|
2047
|
+
return chunk;
|
|
2048
|
+
}), Stream3.toChannel), (prompt) => Effect7.zipRight(Ref2.set(history, merge2(prompt, fromResponseParts(Array.from(parts)))), semaphore.release(1)))).pipe(provideContextStream, Stream3.withSpan("Chat.streamText", {
|
|
2049
|
+
captureStackTrace: false
|
|
2050
|
+
}));
|
|
2051
|
+
}, Stream3.unwrap),
|
|
2052
|
+
generateObject: Effect7.fnUntraced(function* (options) {
|
|
2053
|
+
const newPrompt = make2(options.prompt);
|
|
2054
|
+
const oldPrompt = yield* Ref2.get(history);
|
|
2055
|
+
const prompt = merge2(oldPrompt, newPrompt);
|
|
2056
|
+
const response = yield* generateObject({
|
|
2057
|
+
...options,
|
|
2058
|
+
prompt
|
|
2059
|
+
});
|
|
2060
|
+
const newHistory = merge2(prompt, fromResponseParts(response.content));
|
|
2061
|
+
yield* Ref2.set(history, newHistory);
|
|
2062
|
+
return response;
|
|
2063
|
+
}, provideContext, semaphore.withPermits(1), (effect3, options) => Effect7.withSpan(effect3, "Chat.generateObject", {
|
|
2064
|
+
attributes: {
|
|
2065
|
+
objectName: "objectName" in options ? options.objectName : ("_tag" in options.schema) ? options.schema._tag : options.schema.identifier ?? "generateObject"
|
|
2066
|
+
},
|
|
2067
|
+
captureStackTrace: false
|
|
2068
|
+
}))
|
|
2069
|
+
});
|
|
2070
|
+
});
|
|
2071
|
+
var fromPrompt = Effect7.fnUntraced(function* (prompt) {
|
|
2072
|
+
const chat = yield* empty6;
|
|
2073
|
+
yield* Ref2.set(chat.history, make2(prompt));
|
|
2074
|
+
return chat;
|
|
2075
|
+
});
|
|
2076
|
+
var decodeUnknown4 = Schema7.decodeUnknown(Prompt);
|
|
2077
|
+
var fromExport = (data) => Effect7.flatMap(decodeUnknown4(data), fromPrompt);
|
|
2078
|
+
var decodeHistoryJson = Schema7.decodeUnknown(FromJson);
|
|
2079
|
+
var fromJson = (data) => Effect7.flatMap(decodeHistoryJson(data), fromPrompt);
|
|
2080
|
+
|
|
2081
|
+
class ChatNotFoundError extends Schema7.TaggedError("@effect/ai/Chat/ChatNotFoundError")("ChatNotFoundError", { chatId: Schema7.String }) {
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
class Persistence extends Context7.Tag("@effect/ai/Chat/Persisted")() {
|
|
2085
|
+
}
|
|
2086
|
+
var makePersisted = Effect7.fnUntraced(function* (options) {
|
|
2087
|
+
const persistence = yield* import_Persistence.BackingPersistence;
|
|
2088
|
+
const store = yield* persistence.make(options.storeId);
|
|
2089
|
+
const toPersisted = Effect7.fnUntraced(function* (chatId, chat, ttl) {
|
|
2090
|
+
const idGenerator = yield* Effect7.serviceOption(IdGenerator).pipe(Effect7.map(Option4.getOrElse(() => defaultIdGenerator)));
|
|
2091
|
+
const saveChat = Effect7.fnUntraced(function* (prevHistory) {
|
|
2092
|
+
const history = yield* Ref2.get(chat.history);
|
|
2093
|
+
const lastMessage = prevHistory.content[prevHistory.content.length - 1];
|
|
2094
|
+
let messageId = undefined;
|
|
2095
|
+
if (Predicate9.isNotUndefined(lastMessage) && lastMessage.role === "assistant") {
|
|
2096
|
+
messageId = lastMessage.options[Persistence.key]?.messageId;
|
|
2097
|
+
}
|
|
2098
|
+
if (Predicate9.isUndefined(messageId)) {
|
|
2099
|
+
messageId = yield* idGenerator.generateId();
|
|
2100
|
+
}
|
|
2101
|
+
for (let i = prevHistory.content.length;i < history.content.length; i++) {
|
|
2102
|
+
const message = history.content[i];
|
|
2103
|
+
message.options[Persistence.key] = { messageId };
|
|
2104
|
+
}
|
|
2105
|
+
yield* Ref2.set(chat.history, history);
|
|
2106
|
+
const json = yield* chat.exportJson;
|
|
2107
|
+
yield* store.set(chatId, json, ttl);
|
|
2108
|
+
}, Effect7.catchTag("PersistenceError", (error) => {
|
|
2109
|
+
if (error.reason === "ParseError")
|
|
2110
|
+
return Effect7.die(error);
|
|
2111
|
+
return Effect7.fail(error);
|
|
2112
|
+
}));
|
|
2113
|
+
const persisted = {
|
|
2114
|
+
...chat,
|
|
2115
|
+
id: chatId,
|
|
2116
|
+
save: Effect7.flatMap(Ref2.get(chat.history), saveChat),
|
|
2117
|
+
generateText: Effect7.fnUntraced(function* (options2) {
|
|
2118
|
+
const history = yield* Ref2.get(chat.history);
|
|
2119
|
+
return yield* chat.generateText(options2).pipe(Effect7.ensuring(Effect7.orDie(saveChat(history))));
|
|
2120
|
+
}),
|
|
2121
|
+
generateObject: Effect7.fnUntraced(function* (options2) {
|
|
2122
|
+
const history = yield* Ref2.get(chat.history);
|
|
2123
|
+
return yield* chat.generateObject(options2).pipe(Effect7.ensuring(Effect7.orDie(saveChat(history))));
|
|
2124
|
+
}),
|
|
2125
|
+
streamText: Effect7.fnUntraced(function* (options2) {
|
|
2126
|
+
const history = yield* Ref2.get(chat.history);
|
|
2127
|
+
const stream = chat.streamText(options2).pipe(Stream3.ensuring(Effect7.orDie(saveChat(history))));
|
|
2128
|
+
return stream;
|
|
2129
|
+
}, Stream3.unwrap)
|
|
2130
|
+
};
|
|
2131
|
+
return persisted;
|
|
2132
|
+
});
|
|
2133
|
+
const createChat = Effect7.fnUntraced(function* (chatId, ttl) {
|
|
2134
|
+
const chat = yield* empty6;
|
|
2135
|
+
const history = yield* chat.exportJson;
|
|
2136
|
+
yield* store.set(chatId, history, ttl);
|
|
2137
|
+
return yield* toPersisted(chatId, chat, ttl);
|
|
2138
|
+
}, Effect7.catchTag("PersistenceError", (error) => {
|
|
2139
|
+
if (error.reason === "ParseError")
|
|
2140
|
+
return Effect7.die(error);
|
|
2141
|
+
return Effect7.fail(error);
|
|
2142
|
+
}));
|
|
2143
|
+
const getChat = Effect7.fnUntraced(function* (chatId, ttl) {
|
|
2144
|
+
const chat = yield* empty6;
|
|
2145
|
+
yield* store.get(chatId).pipe(Effect7.flatMap(Effect7.transposeMapOption(decodeHistoryJson)), Effect7.flatten, Effect7.catchTag("NoSuchElementException", () => new ChatNotFoundError({ chatId })), Effect7.flatMap((history) => Ref2.set(chat.history, history)));
|
|
2146
|
+
return yield* toPersisted(chatId, chat, ttl);
|
|
2147
|
+
}, Effect7.catchTags({
|
|
2148
|
+
ParseError: (error) => Effect7.die(error),
|
|
2149
|
+
PersistenceError: (error) => {
|
|
2150
|
+
if (error.reason === "ParseError")
|
|
2151
|
+
return Effect7.die(error);
|
|
2152
|
+
return Effect7.fail(error);
|
|
2153
|
+
}
|
|
2154
|
+
}));
|
|
2155
|
+
const makeTTL = (timeToLive) => Option4.fromNullable(timeToLive).pipe(Option4.map(Duration.decode));
|
|
2156
|
+
const get3 = Effect7.fn("PersistedChat.get")(function* (chatId, options2) {
|
|
2157
|
+
const ttl = makeTTL(options2?.timeToLive);
|
|
2158
|
+
return yield* getChat(chatId, ttl);
|
|
2159
|
+
});
|
|
2160
|
+
const getOrCreate = Effect7.fn("PersistedChat.getOrCreate")(function* (chatId, options2) {
|
|
2161
|
+
const ttl = makeTTL(options2?.timeToLive);
|
|
2162
|
+
return yield* getChat(chatId, ttl).pipe(Effect7.catchTag("ChatNotFoundError", () => createChat(chatId, ttl)));
|
|
2163
|
+
});
|
|
2164
|
+
return Persistence.of({
|
|
2165
|
+
get: get3,
|
|
2166
|
+
getOrCreate
|
|
2167
|
+
});
|
|
2168
|
+
});
|
|
2169
|
+
var layerPersisted = (options) => Layer4.scoped(Persistence, makePersisted(options));
|
|
2170
|
+
// packages/ariadne/src/EmbeddingModel.ts
|
|
2171
|
+
var exports_EmbeddingModel = {};
|
|
2172
|
+
__export(exports_EmbeddingModel, {
|
|
2173
|
+
makeDataLoader: () => makeDataLoader,
|
|
2174
|
+
make: () => make9,
|
|
2175
|
+
EmbeddingModel: () => EmbeddingModel
|
|
2176
|
+
});
|
|
2177
|
+
var import_RequestResolver = require("@effect/experimental/RequestResolver");
|
|
2178
|
+
var Context8 = __toESM(require("effect/Context"));
|
|
2179
|
+
var Effect8 = __toESM(require("effect/Effect"));
|
|
2180
|
+
var import_Function6 = require("effect/Function");
|
|
2181
|
+
var Option5 = __toESM(require("effect/Option"));
|
|
2182
|
+
var Request = __toESM(require("effect/Request"));
|
|
2183
|
+
var RequestResolver = __toESM(require("effect/RequestResolver"));
|
|
2184
|
+
var Schema8 = __toESM(require("effect/Schema"));
|
|
2185
|
+
class EmbeddingModel extends Context8.Tag("@effect/ai/EmbeddingModel")() {
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
class EmbeddingRequest extends Schema8.TaggedRequest("@effect/ai/EmbeddingModel/Request")("EmbeddingRequest", {
|
|
2189
|
+
failure: AiError,
|
|
2190
|
+
success: Schema8.mutable(Schema8.Array(Schema8.Number)),
|
|
2191
|
+
payload: { input: Schema8.String }
|
|
2192
|
+
}) {
|
|
2193
|
+
}
|
|
2194
|
+
var makeBatchedResolver = (embedMany) => RequestResolver.makeBatched((requests) => embedMany(requests.map((request2) => request2.input)).pipe(Effect8.flatMap(Effect8.forEach(({ embeddings, index }) => Request.succeed(requests[index], embeddings), { discard: true })), Effect8.catchAll((error) => Effect8.forEach(requests, (request2) => Request.fail(request2, error), { discard: true }))));
|
|
2195
|
+
var make9 = (options) => Effect8.gen(function* () {
|
|
2196
|
+
const cache = yield* Option5.fromNullable(options.cache).pipe(Effect8.flatMap((config) => Request.makeCache(config)), Effect8.optionFromOptional);
|
|
2197
|
+
const resolver = makeBatchedResolver(options.embedMany).pipe(options.maxBatchSize ? RequestResolver.batchN(options.maxBatchSize) : import_Function6.identity);
|
|
2198
|
+
const embed = (input) => {
|
|
2199
|
+
const request2 = Effect8.request(new EmbeddingRequest({ input }), resolver);
|
|
2200
|
+
return Option5.match(cache, {
|
|
2201
|
+
onNone: () => request2,
|
|
2202
|
+
onSome: (cache2) => request2.pipe(Effect8.withRequestCaching(true), Effect8.withRequestCache(cache2))
|
|
2203
|
+
});
|
|
2204
|
+
};
|
|
2205
|
+
const embedMany = (inputs, options2) => Effect8.forEach(inputs, embed, {
|
|
2206
|
+
batching: true,
|
|
2207
|
+
concurrency: options2?.concurrency
|
|
2208
|
+
});
|
|
2209
|
+
return EmbeddingModel.of({
|
|
2210
|
+
embed: (input) => embed(input).pipe(Effect8.withSpan("EmbeddingModel.embed", {
|
|
2211
|
+
captureStackTrace: false
|
|
2212
|
+
})),
|
|
2213
|
+
embedMany: (inputs) => embedMany(inputs).pipe(Effect8.withSpan("EmbeddingModel.embedMany", {
|
|
2214
|
+
captureStackTrace: false
|
|
2215
|
+
}))
|
|
2216
|
+
});
|
|
2217
|
+
});
|
|
2218
|
+
var makeDataLoader = (options) => Effect8.gen(function* () {
|
|
2219
|
+
const resolver = makeBatchedResolver(options.embedMany);
|
|
2220
|
+
const resolverDelayed = yield* import_RequestResolver.dataLoader(resolver, {
|
|
2221
|
+
window: options.window,
|
|
2222
|
+
maxBatchSize: options.maxBatchSize
|
|
2223
|
+
});
|
|
2224
|
+
function embed(input) {
|
|
2225
|
+
return Effect8.request(new EmbeddingRequest({ input }), resolverDelayed).pipe(Effect8.withSpan("EmbeddingModel.embed", {
|
|
2226
|
+
captureStackTrace: false
|
|
2227
|
+
}));
|
|
2228
|
+
}
|
|
2229
|
+
function embedMany(inputs, options2) {
|
|
2230
|
+
return Effect8.forEach(inputs, embed, {
|
|
2231
|
+
batching: true,
|
|
2232
|
+
concurrency: options2?.concurrency
|
|
2233
|
+
}).pipe(Effect8.withSpan("EmbeddingModel.embedMany", {
|
|
2234
|
+
captureStackTrace: false
|
|
2235
|
+
}));
|
|
2236
|
+
}
|
|
2237
|
+
return EmbeddingModel.of({
|
|
2238
|
+
embed,
|
|
2239
|
+
embedMany
|
|
2240
|
+
});
|
|
2241
|
+
});
|
|
2242
|
+
// packages/ariadne/src/McpRegistry.ts
|
|
2243
|
+
var exports_McpRegistry = {};
|
|
2244
|
+
__export(exports_McpRegistry, {
|
|
2245
|
+
url: () => url,
|
|
2246
|
+
toApiFormat: () => toApiFormat,
|
|
2247
|
+
marketplace: () => marketplace,
|
|
2248
|
+
make: () => make10,
|
|
2249
|
+
isUrl: () => isUrl,
|
|
2250
|
+
isMarketplace: () => isMarketplace
|
|
2251
|
+
});
|
|
2252
|
+
var url = (url2) => ({ _tag: "url", url: url2 });
|
|
2253
|
+
var marketplace = (id) => ({
|
|
2254
|
+
_tag: "marketplace",
|
|
2255
|
+
id
|
|
2256
|
+
});
|
|
2257
|
+
var make10 = (servers) => Array.from(servers);
|
|
2258
|
+
var toApiFormat = (servers) => servers.map((server) => server._tag === "url" ? server.url : server.id);
|
|
2259
|
+
var isUrl = (server) => server._tag === "url";
|
|
2260
|
+
var isMarketplace = (server) => server._tag === "marketplace";
|
|
2261
|
+
// packages/ariadne/src/McpSchema.ts
|
|
2262
|
+
var exports_McpSchema = {};
|
|
2263
|
+
__export(exports_McpSchema, {
|
|
2264
|
+
param: () => param,
|
|
2265
|
+
Unsubscribe: () => Unsubscribe,
|
|
2266
|
+
ToolListChangedNotification: () => ToolListChangedNotification,
|
|
2267
|
+
ToolAnnotations: () => ToolAnnotations,
|
|
2268
|
+
Tool: () => Tool,
|
|
2269
|
+
TextResourceContents: () => TextResourceContents,
|
|
2270
|
+
TextContent: () => TextContent,
|
|
2271
|
+
Subscribe: () => Subscribe,
|
|
2272
|
+
SetLevel: () => SetLevel,
|
|
2273
|
+
ServerRequestRpcs: () => ServerRequestRpcs,
|
|
2274
|
+
ServerNotificationRpcs: () => ServerNotificationRpcs,
|
|
2275
|
+
ServerCapabilities: () => ServerCapabilities,
|
|
2276
|
+
SamplingMessage: () => SamplingMessage,
|
|
2277
|
+
RootsListChangedNotification: () => RootsListChangedNotification,
|
|
2278
|
+
Root: () => Root,
|
|
2279
|
+
Role: () => Role,
|
|
2280
|
+
ResultMeta: () => ResultMeta,
|
|
2281
|
+
ResourceUpdatedNotification: () => ResourceUpdatedNotification,
|
|
2282
|
+
ResourceTemplate: () => ResourceTemplate,
|
|
2283
|
+
ResourceReference: () => ResourceReference,
|
|
2284
|
+
ResourceListChangedNotification: () => ResourceListChangedNotification,
|
|
2285
|
+
ResourceLink: () => ResourceLink,
|
|
2286
|
+
ResourceContents: () => ResourceContents,
|
|
2287
|
+
Resource: () => Resource,
|
|
2288
|
+
RequestMeta: () => RequestMeta,
|
|
2289
|
+
RequestId: () => RequestId,
|
|
2290
|
+
ReadResourceResult: () => ReadResourceResult,
|
|
2291
|
+
ReadResource: () => ReadResource,
|
|
2292
|
+
PromptReference: () => PromptReference,
|
|
2293
|
+
PromptMessage: () => PromptMessage,
|
|
2294
|
+
PromptListChangedNotification: () => PromptListChangedNotification,
|
|
2295
|
+
PromptArgument: () => PromptArgument,
|
|
2296
|
+
Prompt: () => Prompt2,
|
|
2297
|
+
ProgressToken: () => ProgressToken,
|
|
2298
|
+
ProgressNotification: () => ProgressNotification,
|
|
2299
|
+
Ping: () => Ping,
|
|
2300
|
+
ParseError: () => ParseError,
|
|
2301
|
+
ParamAnnotation: () => ParamAnnotation,
|
|
2302
|
+
PaginatedResultMeta: () => PaginatedResultMeta,
|
|
2303
|
+
PaginatedRequestMeta: () => PaginatedRequestMeta,
|
|
2304
|
+
PARSE_ERROR_CODE: () => PARSE_ERROR_CODE,
|
|
2305
|
+
NotificationMeta: () => NotificationMeta,
|
|
2306
|
+
ModelPreferences: () => ModelPreferences,
|
|
2307
|
+
ModelHint: () => ModelHint,
|
|
2308
|
+
MethodNotFound: () => MethodNotFound,
|
|
2309
|
+
McpServerClientMiddleware: () => McpServerClientMiddleware,
|
|
2310
|
+
McpServerClient: () => McpServerClient,
|
|
2311
|
+
McpError: () => McpError,
|
|
2312
|
+
METHOD_NOT_FOUND_ERROR_CODE: () => METHOD_NOT_FOUND_ERROR_CODE,
|
|
2313
|
+
LoggingMessageNotification: () => LoggingMessageNotification,
|
|
2314
|
+
LoggingLevel: () => LoggingLevel,
|
|
2315
|
+
ListToolsResult: () => ListToolsResult,
|
|
2316
|
+
ListTools: () => ListTools,
|
|
2317
|
+
ListRootsResult: () => ListRootsResult,
|
|
2318
|
+
ListRoots: () => ListRoots,
|
|
2319
|
+
ListResourcesResult: () => ListResourcesResult,
|
|
2320
|
+
ListResources: () => ListResources,
|
|
2321
|
+
ListResourceTemplatesResult: () => ListResourceTemplatesResult,
|
|
2322
|
+
ListResourceTemplates: () => ListResourceTemplates,
|
|
2323
|
+
ListPromptsResult: () => ListPromptsResult,
|
|
2324
|
+
ListPrompts: () => ListPrompts,
|
|
2325
|
+
InvalidRequest: () => InvalidRequest,
|
|
2326
|
+
InvalidParams: () => InvalidParams,
|
|
2327
|
+
InternalError: () => InternalError,
|
|
2328
|
+
InitializedNotification: () => InitializedNotification,
|
|
2329
|
+
InitializeResult: () => InitializeResult,
|
|
2330
|
+
Initialize: () => Initialize,
|
|
2331
|
+
Implementation: () => Implementation,
|
|
2332
|
+
ImageContent: () => ImageContent,
|
|
2333
|
+
INVALID_REQUEST_ERROR_CODE: () => INVALID_REQUEST_ERROR_CODE,
|
|
2334
|
+
INVALID_PARAMS_ERROR_CODE: () => INVALID_PARAMS_ERROR_CODE,
|
|
2335
|
+
INTERNAL_ERROR_CODE: () => INTERNAL_ERROR_CODE,
|
|
2336
|
+
GetPromptResult: () => GetPromptResult,
|
|
2337
|
+
GetPrompt: () => GetPrompt,
|
|
2338
|
+
EmbeddedResource: () => EmbeddedResource,
|
|
2339
|
+
ElicitationDeclined: () => ElicitationDeclined,
|
|
2340
|
+
ElicitResult: () => ElicitResult,
|
|
2341
|
+
ElicitDeclineResult: () => ElicitDeclineResult,
|
|
2342
|
+
ElicitAcceptResult: () => ElicitAcceptResult,
|
|
2343
|
+
Elicit: () => Elicit,
|
|
2344
|
+
Cursor: () => Cursor,
|
|
2345
|
+
CreateMessageResult: () => CreateMessageResult,
|
|
2346
|
+
CreateMessage: () => CreateMessage,
|
|
2347
|
+
ContentBlock: () => ContentBlock,
|
|
2348
|
+
CompleteResult: () => CompleteResult,
|
|
2349
|
+
Complete: () => Complete,
|
|
2350
|
+
ClientRpcs: () => ClientRpcs,
|
|
2351
|
+
ClientRequestRpcs: () => ClientRequestRpcs,
|
|
2352
|
+
ClientNotificationRpcs: () => ClientNotificationRpcs,
|
|
2353
|
+
ClientCapabilities: () => ClientCapabilities,
|
|
2354
|
+
CancelledNotification: () => CancelledNotification,
|
|
2355
|
+
CallToolResult: () => CallToolResult,
|
|
2356
|
+
CallTool: () => CallTool,
|
|
2357
|
+
BlobResourceContents: () => BlobResourceContents,
|
|
2358
|
+
AudioContent: () => AudioContent,
|
|
2359
|
+
Annotations: () => Annotations
|
|
2360
|
+
});
|
|
2361
|
+
var Rpc = __toESM(require("@effect/rpc/Rpc"));
|
|
2362
|
+
var RpcGroup = __toESM(require("@effect/rpc/RpcGroup"));
|
|
2363
|
+
var RpcMiddleware = __toESM(require("@effect/rpc/RpcMiddleware"));
|
|
2364
|
+
var Context9 = __toESM(require("effect/Context"));
|
|
2365
|
+
var Schema9 = __toESM(require("effect/Schema"));
|
|
2366
|
+
var RequestId = Schema9.Union(Schema9.String, Schema9.Number);
|
|
2367
|
+
var ProgressToken = Schema9.Union(Schema9.String, Schema9.Number);
|
|
2368
|
+
|
|
2369
|
+
class RequestMeta extends Schema9.Struct({
|
|
2370
|
+
_meta: Schema9.optional(Schema9.Struct({
|
|
2371
|
+
progressToken: Schema9.optional(ProgressToken)
|
|
2372
|
+
}))
|
|
2373
|
+
}) {
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
class ResultMeta extends Schema9.Struct({
|
|
2377
|
+
_meta: Schema9.optional(Schema9.Record({ key: Schema9.String, value: Schema9.Unknown }))
|
|
2378
|
+
}) {
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
class NotificationMeta extends Schema9.Struct({
|
|
2382
|
+
_meta: Schema9.optional(Schema9.Record({ key: Schema9.String, value: Schema9.Unknown }))
|
|
2383
|
+
}) {
|
|
2384
|
+
}
|
|
2385
|
+
var Cursor = Schema9.String;
|
|
2386
|
+
|
|
2387
|
+
class PaginatedRequestMeta extends Schema9.Struct({
|
|
2388
|
+
...RequestMeta.fields,
|
|
2389
|
+
cursor: Schema9.optional(Cursor)
|
|
2390
|
+
}) {
|
|
2391
|
+
}
|
|
2392
|
+
|
|
2393
|
+
class PaginatedResultMeta extends Schema9.Struct({
|
|
2394
|
+
...ResultMeta.fields,
|
|
2395
|
+
nextCursor: Schema9.optional(Cursor)
|
|
2396
|
+
}) {
|
|
2397
|
+
}
|
|
2398
|
+
var Role = Schema9.Literal("user", "assistant");
|
|
2399
|
+
|
|
2400
|
+
class Annotations extends Schema9.Struct({
|
|
2401
|
+
audience: Schema9.optional(Schema9.Array(Role)),
|
|
2402
|
+
priority: Schema9.optional(Schema9.Number.pipe(Schema9.between(0, 1)))
|
|
2403
|
+
}) {
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
class Implementation extends Schema9.Struct({
|
|
2407
|
+
name: Schema9.String,
|
|
2408
|
+
title: Schema9.optional(Schema9.String),
|
|
2409
|
+
version: Schema9.String
|
|
2410
|
+
}) {
|
|
2411
|
+
}
|
|
2412
|
+
|
|
2413
|
+
class ClientCapabilities extends Schema9.Class("@effect/ai/McpSchema/ClientCapabilities")({
|
|
2414
|
+
experimental: Schema9.optional(Schema9.Record({
|
|
2415
|
+
key: Schema9.String,
|
|
2416
|
+
value: Schema9.Struct({})
|
|
2417
|
+
})),
|
|
2418
|
+
roots: Schema9.optional(Schema9.Struct({
|
|
2419
|
+
listChanged: Schema9.optional(Schema9.Boolean)
|
|
2420
|
+
})),
|
|
2421
|
+
sampling: Schema9.optional(Schema9.Struct({})),
|
|
2422
|
+
elicitation: Schema9.optional(Schema9.Struct({}))
|
|
2423
|
+
}) {
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
class ServerCapabilities extends Schema9.Struct({
|
|
2427
|
+
experimental: Schema9.optional(Schema9.Record({
|
|
2428
|
+
key: Schema9.String,
|
|
2429
|
+
value: Schema9.Struct({})
|
|
2430
|
+
})),
|
|
2431
|
+
logging: Schema9.optional(Schema9.Struct({})),
|
|
2432
|
+
completions: Schema9.optional(Schema9.Struct({})),
|
|
2433
|
+
prompts: Schema9.optional(Schema9.Struct({
|
|
2434
|
+
listChanged: Schema9.optional(Schema9.Boolean)
|
|
2435
|
+
})),
|
|
2436
|
+
resources: Schema9.optional(Schema9.Struct({
|
|
2437
|
+
subscribe: Schema9.optional(Schema9.Boolean),
|
|
2438
|
+
listChanged: Schema9.optional(Schema9.Boolean)
|
|
2439
|
+
})),
|
|
2440
|
+
tools: Schema9.optional(Schema9.Struct({
|
|
2441
|
+
listChanged: Schema9.optional(Schema9.Boolean)
|
|
2442
|
+
}))
|
|
2443
|
+
}) {
|
|
2444
|
+
}
|
|
2445
|
+
|
|
2446
|
+
class McpError extends Schema9.Class("@effect/ai/McpSchema/McpError")({
|
|
2447
|
+
code: Schema9.Number,
|
|
2448
|
+
message: Schema9.String,
|
|
2449
|
+
data: Schema9.optional(Schema9.Unknown)
|
|
2450
|
+
}) {
|
|
2451
|
+
}
|
|
2452
|
+
var INVALID_REQUEST_ERROR_CODE = -32600;
|
|
2453
|
+
var METHOD_NOT_FOUND_ERROR_CODE = -32601;
|
|
2454
|
+
var INVALID_PARAMS_ERROR_CODE = -32602;
|
|
2455
|
+
var INTERNAL_ERROR_CODE = -32603;
|
|
2456
|
+
var PARSE_ERROR_CODE = -32700;
|
|
2457
|
+
|
|
2458
|
+
class ParseError extends Schema9.TaggedError()("ParseError", {
|
|
2459
|
+
...McpError.fields,
|
|
2460
|
+
code: Schema9.tag(PARSE_ERROR_CODE)
|
|
2461
|
+
}) {
|
|
2462
|
+
}
|
|
2463
|
+
|
|
2464
|
+
class InvalidRequest extends Schema9.TaggedError()("InvalidRequest", {
|
|
2465
|
+
...McpError.fields,
|
|
2466
|
+
code: Schema9.tag(INVALID_REQUEST_ERROR_CODE)
|
|
2467
|
+
}) {
|
|
2468
|
+
}
|
|
2469
|
+
|
|
2470
|
+
class MethodNotFound extends Schema9.TaggedError()("MethodNotFound", {
|
|
2471
|
+
...McpError.fields,
|
|
2472
|
+
code: Schema9.tag(METHOD_NOT_FOUND_ERROR_CODE)
|
|
2473
|
+
}) {
|
|
2474
|
+
}
|
|
2475
|
+
|
|
2476
|
+
class InvalidParams extends Schema9.TaggedError()("InvalidParams", {
|
|
2477
|
+
...McpError.fields,
|
|
2478
|
+
code: Schema9.tag(INVALID_PARAMS_ERROR_CODE)
|
|
2479
|
+
}) {
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
class InternalError extends Schema9.TaggedError()("InternalError", {
|
|
2483
|
+
...McpError.fields,
|
|
2484
|
+
code: Schema9.tag(INTERNAL_ERROR_CODE)
|
|
2485
|
+
}) {
|
|
2486
|
+
static notImplemented = new InternalError({
|
|
2487
|
+
message: "Not implemented"
|
|
2488
|
+
});
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
class Ping extends Rpc.make("ping", {
|
|
2492
|
+
success: Schema9.Struct({}),
|
|
2493
|
+
error: McpError,
|
|
2494
|
+
payload: Schema9.UndefinedOr(RequestMeta)
|
|
2495
|
+
}) {
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
class InitializeResult extends Schema9.Class("@effect/ai/McpSchema/InitializeResult")({
|
|
2499
|
+
...ResultMeta.fields,
|
|
2500
|
+
protocolVersion: Schema9.String,
|
|
2501
|
+
capabilities: ServerCapabilities,
|
|
2502
|
+
serverInfo: Implementation,
|
|
2503
|
+
instructions: Schema9.optional(Schema9.String)
|
|
2504
|
+
}) {
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
class Initialize extends Rpc.make("initialize", {
|
|
2508
|
+
success: InitializeResult,
|
|
2509
|
+
error: McpError,
|
|
2510
|
+
payload: {
|
|
2511
|
+
...RequestMeta.fields,
|
|
2512
|
+
protocolVersion: Schema9.String,
|
|
2513
|
+
capabilities: ClientCapabilities,
|
|
2514
|
+
clientInfo: Implementation
|
|
2515
|
+
}
|
|
2516
|
+
}) {
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
class InitializedNotification extends Rpc.make("notifications/initialized", {
|
|
2520
|
+
payload: Schema9.UndefinedOr(NotificationMeta)
|
|
2521
|
+
}) {
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
class CancelledNotification extends Rpc.make("notifications/cancelled", {
|
|
2525
|
+
payload: {
|
|
2526
|
+
...NotificationMeta.fields,
|
|
2527
|
+
requestId: RequestId,
|
|
2528
|
+
reason: Schema9.optional(Schema9.String)
|
|
2529
|
+
}
|
|
2530
|
+
}) {
|
|
2531
|
+
}
|
|
2532
|
+
|
|
2533
|
+
class ProgressNotification extends Rpc.make("notifications/progress", {
|
|
2534
|
+
payload: {
|
|
2535
|
+
...NotificationMeta.fields,
|
|
2536
|
+
progressToken: ProgressToken,
|
|
2537
|
+
progress: Schema9.optional(Schema9.Number),
|
|
2538
|
+
total: Schema9.optional(Schema9.Number),
|
|
2539
|
+
message: Schema9.optional(Schema9.String)
|
|
2540
|
+
}
|
|
2541
|
+
}) {
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
class Resource extends Schema9.Class("@effect/ai/McpSchema/Resource")({
|
|
2545
|
+
uri: Schema9.String,
|
|
2546
|
+
name: Schema9.String,
|
|
2547
|
+
title: Schema9.optional(Schema9.String),
|
|
2548
|
+
description: Schema9.optional(Schema9.String),
|
|
2549
|
+
mimeType: Schema9.optional(Schema9.String),
|
|
2550
|
+
annotations: Schema9.optional(Annotations),
|
|
2551
|
+
size: Schema9.optional(Schema9.Number)
|
|
2552
|
+
}) {
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
class ResourceTemplate extends Schema9.Class("@effect/ai/McpSchema/ResourceTemplate")({
|
|
2556
|
+
uriTemplate: Schema9.String,
|
|
2557
|
+
name: Schema9.String,
|
|
2558
|
+
title: Schema9.optional(Schema9.String),
|
|
2559
|
+
description: Schema9.optional(Schema9.String),
|
|
2560
|
+
mimeType: Schema9.optional(Schema9.String),
|
|
2561
|
+
annotations: Schema9.optional(Annotations)
|
|
2562
|
+
}) {
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
class ResourceContents extends Schema9.Struct({
|
|
2566
|
+
uri: Schema9.String,
|
|
2567
|
+
mimeType: Schema9.optional(Schema9.String)
|
|
2568
|
+
}) {
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
class TextResourceContents extends Schema9.Struct({
|
|
2572
|
+
...ResourceContents.fields,
|
|
2573
|
+
text: Schema9.String
|
|
2574
|
+
}) {
|
|
2575
|
+
}
|
|
2576
|
+
|
|
2577
|
+
class BlobResourceContents extends Schema9.Struct({
|
|
2578
|
+
...ResourceContents.fields,
|
|
2579
|
+
blob: Schema9.Uint8ArrayFromBase64
|
|
2580
|
+
}) {
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
class ListResourcesResult extends Schema9.Class("@effect/ai/McpSchema/ListResourcesResult")({
|
|
2584
|
+
...PaginatedResultMeta.fields,
|
|
2585
|
+
resources: Schema9.Array(Resource)
|
|
2586
|
+
}) {
|
|
2587
|
+
}
|
|
2588
|
+
|
|
2589
|
+
class ListResources extends Rpc.make("resources/list", {
|
|
2590
|
+
success: ListResourcesResult,
|
|
2591
|
+
error: McpError,
|
|
2592
|
+
payload: Schema9.UndefinedOr(PaginatedRequestMeta)
|
|
2593
|
+
}) {
|
|
2594
|
+
}
|
|
2595
|
+
|
|
2596
|
+
class ListResourceTemplatesResult extends Schema9.Class("@effect/ai/McpSchema/ListResourceTemplatesResult")({
|
|
2597
|
+
...PaginatedResultMeta.fields,
|
|
2598
|
+
resourceTemplates: Schema9.Array(ResourceTemplate)
|
|
2599
|
+
}) {
|
|
2600
|
+
}
|
|
2601
|
+
|
|
2602
|
+
class ListResourceTemplates extends Rpc.make("resources/templates/list", {
|
|
2603
|
+
success: ListResourceTemplatesResult,
|
|
2604
|
+
error: McpError,
|
|
2605
|
+
payload: Schema9.UndefinedOr(PaginatedRequestMeta)
|
|
2606
|
+
}) {
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
class ReadResourceResult extends Schema9.Struct({
|
|
2610
|
+
...ResultMeta.fields,
|
|
2611
|
+
contents: Schema9.Array(Schema9.Union(TextResourceContents, BlobResourceContents))
|
|
2612
|
+
}) {
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
class ReadResource extends Rpc.make("resources/read", {
|
|
2616
|
+
success: ReadResourceResult,
|
|
2617
|
+
error: McpError,
|
|
2618
|
+
payload: {
|
|
2619
|
+
...RequestMeta.fields,
|
|
2620
|
+
uri: Schema9.String
|
|
2621
|
+
}
|
|
2622
|
+
}) {
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
class ResourceListChangedNotification extends Rpc.make("notifications/resources/list_changed", {
|
|
2626
|
+
payload: Schema9.UndefinedOr(NotificationMeta)
|
|
2627
|
+
}) {
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
class Subscribe extends Rpc.make("resources/subscribe", {
|
|
2631
|
+
error: McpError,
|
|
2632
|
+
payload: {
|
|
2633
|
+
...RequestMeta.fields,
|
|
2634
|
+
uri: Schema9.String
|
|
2635
|
+
}
|
|
2636
|
+
}) {
|
|
2637
|
+
}
|
|
2638
|
+
|
|
2639
|
+
class Unsubscribe extends Rpc.make("resources/unsubscribe", {
|
|
2640
|
+
error: McpError,
|
|
2641
|
+
payload: {
|
|
2642
|
+
...RequestMeta.fields,
|
|
2643
|
+
uri: Schema9.String
|
|
2644
|
+
}
|
|
2645
|
+
}) {
|
|
2646
|
+
}
|
|
2647
|
+
|
|
2648
|
+
class ResourceUpdatedNotification extends Rpc.make("notifications/resources/updated", {
|
|
2649
|
+
payload: {
|
|
2650
|
+
...NotificationMeta.fields,
|
|
2651
|
+
uri: Schema9.String
|
|
2652
|
+
}
|
|
2653
|
+
}) {
|
|
2654
|
+
}
|
|
2655
|
+
|
|
2656
|
+
class PromptArgument extends Schema9.Struct({
|
|
2657
|
+
name: Schema9.String,
|
|
2658
|
+
title: Schema9.optional(Schema9.String),
|
|
2659
|
+
description: Schema9.optional(Schema9.String),
|
|
2660
|
+
required: Schema9.optional(Schema9.Boolean)
|
|
2661
|
+
}) {
|
|
2662
|
+
}
|
|
2663
|
+
|
|
2664
|
+
class Prompt2 extends Schema9.Class("@effect/ai/McpSchema/Prompt")({
|
|
2665
|
+
name: Schema9.String,
|
|
2666
|
+
title: Schema9.optional(Schema9.String),
|
|
2667
|
+
description: Schema9.optional(Schema9.String),
|
|
2668
|
+
arguments: Schema9.optional(Schema9.Array(PromptArgument))
|
|
2669
|
+
}) {
|
|
2670
|
+
}
|
|
2671
|
+
|
|
2672
|
+
class TextContent extends Schema9.Struct({
|
|
2673
|
+
type: Schema9.tag("text"),
|
|
2674
|
+
text: Schema9.String,
|
|
2675
|
+
annotations: Schema9.optional(Annotations)
|
|
2676
|
+
}) {
|
|
2677
|
+
}
|
|
2678
|
+
|
|
2679
|
+
class ImageContent extends Schema9.Struct({
|
|
2680
|
+
type: Schema9.tag("image"),
|
|
2681
|
+
data: Schema9.Uint8ArrayFromBase64,
|
|
2682
|
+
mimeType: Schema9.String,
|
|
2683
|
+
annotations: Schema9.optional(Annotations)
|
|
2684
|
+
}) {
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
class AudioContent extends Schema9.Struct({
|
|
2688
|
+
type: Schema9.tag("audio"),
|
|
2689
|
+
data: Schema9.Uint8ArrayFromBase64,
|
|
2690
|
+
mimeType: Schema9.String,
|
|
2691
|
+
annotations: Schema9.optional(Annotations)
|
|
2692
|
+
}) {
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
class EmbeddedResource extends Schema9.Struct({
|
|
2696
|
+
type: Schema9.tag("resource"),
|
|
2697
|
+
resource: Schema9.Union(TextResourceContents, BlobResourceContents),
|
|
2698
|
+
annotations: Schema9.optional(Annotations)
|
|
2699
|
+
}) {
|
|
2700
|
+
}
|
|
2701
|
+
|
|
2702
|
+
class ResourceLink extends Schema9.Struct({
|
|
2703
|
+
...Resource.fields,
|
|
2704
|
+
type: Schema9.tag("resource_link")
|
|
2705
|
+
}) {
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
class ContentBlock extends Schema9.Union(TextContent, ImageContent, AudioContent, EmbeddedResource, ResourceLink) {
|
|
2709
|
+
}
|
|
2710
|
+
|
|
2711
|
+
class PromptMessage extends Schema9.Struct({
|
|
2712
|
+
role: Role,
|
|
2713
|
+
content: ContentBlock
|
|
2714
|
+
}) {
|
|
2715
|
+
}
|
|
2716
|
+
|
|
2717
|
+
class ListPromptsResult extends Schema9.Class("@effect/ai/McpSchema/ListPromptsResult")({
|
|
2718
|
+
...PaginatedResultMeta.fields,
|
|
2719
|
+
prompts: Schema9.Array(Prompt2)
|
|
2720
|
+
}) {
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
class ListPrompts extends Rpc.make("prompts/list", {
|
|
2724
|
+
success: ListPromptsResult,
|
|
2725
|
+
error: McpError,
|
|
2726
|
+
payload: Schema9.UndefinedOr(PaginatedRequestMeta)
|
|
2727
|
+
}) {
|
|
2728
|
+
}
|
|
2729
|
+
|
|
2730
|
+
class GetPromptResult extends Schema9.Class("@effect/ai/McpSchema/GetPromptResult")({
|
|
2731
|
+
...ResultMeta.fields,
|
|
2732
|
+
messages: Schema9.Array(PromptMessage),
|
|
2733
|
+
description: Schema9.optional(Schema9.String)
|
|
2734
|
+
}) {
|
|
2735
|
+
}
|
|
2736
|
+
|
|
2737
|
+
class GetPrompt extends Rpc.make("prompts/get", {
|
|
2738
|
+
success: GetPromptResult,
|
|
2739
|
+
error: McpError,
|
|
2740
|
+
payload: {
|
|
2741
|
+
...RequestMeta.fields,
|
|
2742
|
+
name: Schema9.String,
|
|
2743
|
+
title: Schema9.optional(Schema9.String),
|
|
2744
|
+
arguments: Schema9.optional(Schema9.Record({
|
|
2745
|
+
key: Schema9.String,
|
|
2746
|
+
value: Schema9.String
|
|
2747
|
+
}))
|
|
2748
|
+
}
|
|
2749
|
+
}) {
|
|
2750
|
+
}
|
|
2751
|
+
|
|
2752
|
+
class PromptListChangedNotification extends Rpc.make("notifications/prompts/list_changed", {
|
|
2753
|
+
payload: Schema9.UndefinedOr(NotificationMeta)
|
|
2754
|
+
}) {
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
class ToolAnnotations extends Schema9.Class("@effect/ai/McpSchema/ToolAnnotations")({
|
|
2758
|
+
title: Schema9.optional(Schema9.String),
|
|
2759
|
+
readOnlyHint: Schema9.optionalWith(Schema9.Boolean, { default: () => false }),
|
|
2760
|
+
destructiveHint: Schema9.optionalWith(Schema9.Boolean, {
|
|
2761
|
+
default: () => true
|
|
2762
|
+
}),
|
|
2763
|
+
idempotentHint: Schema9.optionalWith(Schema9.Boolean, {
|
|
2764
|
+
default: () => false
|
|
2765
|
+
}),
|
|
2766
|
+
openWorldHint: Schema9.optionalWith(Schema9.Boolean, { default: () => true })
|
|
2767
|
+
}) {
|
|
2768
|
+
}
|
|
2769
|
+
|
|
2770
|
+
class Tool extends Schema9.Class("@effect/ai/McpSchema/Tool")({
|
|
2771
|
+
name: Schema9.String,
|
|
2772
|
+
title: Schema9.optional(Schema9.String),
|
|
2773
|
+
description: Schema9.optional(Schema9.String),
|
|
2774
|
+
inputSchema: Schema9.Unknown,
|
|
2775
|
+
annotations: Schema9.optional(ToolAnnotations)
|
|
2776
|
+
}) {
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
class ListToolsResult extends Schema9.Class("@effect/ai/McpSchema/ListToolsResult")({
|
|
2780
|
+
...PaginatedResultMeta.fields,
|
|
2781
|
+
tools: Schema9.Array(Tool)
|
|
2782
|
+
}) {
|
|
2783
|
+
}
|
|
2784
|
+
|
|
2785
|
+
class ListTools extends Rpc.make("tools/list", {
|
|
2786
|
+
success: ListToolsResult,
|
|
2787
|
+
error: McpError,
|
|
2788
|
+
payload: Schema9.UndefinedOr(PaginatedRequestMeta)
|
|
2789
|
+
}) {
|
|
2790
|
+
}
|
|
2791
|
+
|
|
2792
|
+
class CallToolResult extends Schema9.Class("@effect/ai/McpSchema/CallToolResult")({
|
|
2793
|
+
...ResultMeta.fields,
|
|
2794
|
+
content: Schema9.Array(ContentBlock),
|
|
2795
|
+
structuredContent: Schema9.optional(Schema9.Unknown),
|
|
2796
|
+
isError: Schema9.optional(Schema9.Boolean)
|
|
2797
|
+
}) {
|
|
2798
|
+
}
|
|
2799
|
+
|
|
2800
|
+
class CallTool extends Rpc.make("tools/call", {
|
|
2801
|
+
success: CallToolResult,
|
|
2802
|
+
error: McpError,
|
|
2803
|
+
payload: {
|
|
2804
|
+
...RequestMeta.fields,
|
|
2805
|
+
name: Schema9.String,
|
|
2806
|
+
arguments: Schema9.Record({
|
|
2807
|
+
key: Schema9.String,
|
|
2808
|
+
value: Schema9.Unknown
|
|
2809
|
+
})
|
|
2810
|
+
}
|
|
2811
|
+
}) {
|
|
2812
|
+
}
|
|
2813
|
+
|
|
2814
|
+
class ToolListChangedNotification extends Rpc.make("notifications/tools/list_changed", {
|
|
2815
|
+
payload: Schema9.UndefinedOr(NotificationMeta)
|
|
2816
|
+
}) {
|
|
2817
|
+
}
|
|
2818
|
+
var LoggingLevel = Schema9.Literal("debug", "info", "notice", "warning", "error", "critical", "alert", "emergency");
|
|
2819
|
+
|
|
2820
|
+
class SetLevel extends Rpc.make("logging/setLevel", {
|
|
2821
|
+
payload: {
|
|
2822
|
+
...RequestMeta.fields,
|
|
2823
|
+
level: LoggingLevel
|
|
2824
|
+
},
|
|
2825
|
+
error: McpError
|
|
2826
|
+
}) {
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
class LoggingMessageNotification extends Rpc.make("notifications/message", {
|
|
2830
|
+
payload: Schema9.Struct({
|
|
2831
|
+
...NotificationMeta.fields,
|
|
2832
|
+
level: LoggingLevel,
|
|
2833
|
+
logger: Schema9.optional(Schema9.String),
|
|
2834
|
+
data: Schema9.Unknown
|
|
2835
|
+
})
|
|
2836
|
+
}) {
|
|
2837
|
+
}
|
|
2838
|
+
|
|
2839
|
+
class SamplingMessage extends Schema9.Struct({
|
|
2840
|
+
role: Role,
|
|
2841
|
+
content: Schema9.Union(TextContent, ImageContent, AudioContent)
|
|
2842
|
+
}) {
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
class ModelHint extends Schema9.Struct({
|
|
2846
|
+
name: Schema9.optional(Schema9.String)
|
|
2847
|
+
}) {
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
class ModelPreferences extends Schema9.Class("@effect/ai/McpSchema/ModelPreferences")({
|
|
2851
|
+
hints: Schema9.optional(Schema9.Array(ModelHint)),
|
|
2852
|
+
costPriority: Schema9.optional(Schema9.Number.pipe(Schema9.between(0, 1))),
|
|
2853
|
+
speedPriority: Schema9.optional(Schema9.Number.pipe(Schema9.between(0, 1))),
|
|
2854
|
+
intelligencePriority: Schema9.optional(Schema9.Number.pipe(Schema9.between(0, 1)))
|
|
2855
|
+
}) {
|
|
2856
|
+
}
|
|
2857
|
+
|
|
2858
|
+
class CreateMessageResult extends Schema9.Class("@effect/ai/McpSchema/CreateMessageResult")({
|
|
2859
|
+
model: Schema9.String,
|
|
2860
|
+
stopReason: Schema9.optional(Schema9.String)
|
|
2861
|
+
}) {
|
|
2862
|
+
}
|
|
2863
|
+
|
|
2864
|
+
class CreateMessage extends Rpc.make("sampling/createMessage", {
|
|
2865
|
+
success: CreateMessageResult,
|
|
2866
|
+
error: McpError,
|
|
2867
|
+
payload: {
|
|
2868
|
+
messages: Schema9.Array(SamplingMessage),
|
|
2869
|
+
modelPreferences: Schema9.optional(ModelPreferences),
|
|
2870
|
+
systemPrompt: Schema9.optional(Schema9.String),
|
|
2871
|
+
includeContext: Schema9.optional(Schema9.Literal("none", "thisServer", "allServers")),
|
|
2872
|
+
temperature: Schema9.optional(Schema9.Number),
|
|
2873
|
+
maxTokens: Schema9.Number,
|
|
2874
|
+
stopSequences: Schema9.optional(Schema9.Array(Schema9.String)),
|
|
2875
|
+
metadata: Schema9.Unknown
|
|
2876
|
+
}
|
|
2877
|
+
}) {
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2880
|
+
class ResourceReference extends Schema9.Struct({
|
|
2881
|
+
type: Schema9.tag("ref/resource"),
|
|
2882
|
+
uri: Schema9.String
|
|
2883
|
+
}) {
|
|
2884
|
+
}
|
|
2885
|
+
|
|
2886
|
+
class PromptReference extends Schema9.Struct({
|
|
2887
|
+
type: Schema9.tag("ref/prompt"),
|
|
2888
|
+
name: Schema9.String,
|
|
2889
|
+
title: Schema9.optional(Schema9.String)
|
|
2890
|
+
}) {
|
|
2891
|
+
}
|
|
2892
|
+
|
|
2893
|
+
class CompleteResult extends Schema9.Class("@effect/ai/McpSchema/CompleteResult")({
|
|
2894
|
+
completion: Schema9.Struct({
|
|
2895
|
+
values: Schema9.Array(Schema9.String),
|
|
2896
|
+
total: Schema9.optional(Schema9.Number),
|
|
2897
|
+
hasMore: Schema9.optional(Schema9.Boolean)
|
|
2898
|
+
})
|
|
2899
|
+
}) {
|
|
2900
|
+
static empty = CompleteResult.make({
|
|
2901
|
+
completion: {
|
|
2902
|
+
values: [],
|
|
2903
|
+
total: 0,
|
|
2904
|
+
hasMore: false
|
|
2905
|
+
}
|
|
2906
|
+
});
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
class Complete extends Rpc.make("completion/complete", {
|
|
2910
|
+
success: CompleteResult,
|
|
2911
|
+
error: McpError,
|
|
2912
|
+
payload: Schema9.Struct({
|
|
2913
|
+
ref: Schema9.Union(PromptReference, ResourceReference),
|
|
2914
|
+
argument: Schema9.Struct({
|
|
2915
|
+
name: Schema9.String,
|
|
2916
|
+
value: Schema9.String
|
|
2917
|
+
}),
|
|
2918
|
+
context: Schema9.optionalWith(Schema9.Struct({
|
|
2919
|
+
arguments: Schema9.optionalWith(Schema9.Record({
|
|
2920
|
+
key: Schema9.String,
|
|
2921
|
+
value: Schema9.String
|
|
2922
|
+
}), { default: () => ({}) })
|
|
2923
|
+
}), { default: () => ({ arguments: {} }) })
|
|
2924
|
+
})
|
|
2925
|
+
}) {
|
|
2926
|
+
}
|
|
2927
|
+
|
|
2928
|
+
class Root extends Schema9.Class("@effect/ai/McpSchema/Root")({
|
|
2929
|
+
uri: Schema9.String,
|
|
2930
|
+
name: Schema9.optional(Schema9.String)
|
|
2931
|
+
}) {
|
|
2932
|
+
}
|
|
2933
|
+
|
|
2934
|
+
class ListRootsResult extends Schema9.Class("@effect/ai/McpSchema/ListRootsResult")({
|
|
2935
|
+
roots: Schema9.Array(Root)
|
|
2936
|
+
}) {
|
|
2937
|
+
}
|
|
2938
|
+
|
|
2939
|
+
class ListRoots extends Rpc.make("roots/list", {
|
|
2940
|
+
success: ListRootsResult,
|
|
2941
|
+
error: McpError,
|
|
2942
|
+
payload: Schema9.UndefinedOr(RequestMeta)
|
|
2943
|
+
}) {
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
class RootsListChangedNotification extends Rpc.make("notifications/roots/list_changed", {
|
|
2947
|
+
payload: Schema9.UndefinedOr(NotificationMeta)
|
|
2948
|
+
}) {
|
|
2949
|
+
}
|
|
2950
|
+
|
|
2951
|
+
class ElicitAcceptResult extends Schema9.Class("@effect/ai/McpSchema/ElicitAcceptResult")({
|
|
2952
|
+
...ResultMeta.fields,
|
|
2953
|
+
action: Schema9.Literal("accept"),
|
|
2954
|
+
content: Schema9.Unknown
|
|
2955
|
+
}) {
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
class ElicitDeclineResult extends Schema9.Class("@effect/ai/McpSchema/ElicitDeclineResult")({
|
|
2959
|
+
...ResultMeta.fields,
|
|
2960
|
+
action: Schema9.Literal("cancel", "decline")
|
|
2961
|
+
}) {
|
|
2962
|
+
}
|
|
2963
|
+
var ElicitResult = Schema9.Union(ElicitAcceptResult, ElicitDeclineResult);
|
|
2964
|
+
|
|
2965
|
+
class Elicit extends Rpc.make("elicitation/create", {
|
|
2966
|
+
success: ElicitResult,
|
|
2967
|
+
error: McpError,
|
|
2968
|
+
payload: {
|
|
2969
|
+
message: Schema9.String,
|
|
2970
|
+
requestedSchema: Schema9.Unknown
|
|
2971
|
+
}
|
|
2972
|
+
}) {
|
|
2973
|
+
}
|
|
2974
|
+
|
|
2975
|
+
class ElicitationDeclined extends Schema9.TaggedError("@effect/ai/McpSchema/ElicitationDeclined")("ElicitationDeclined", {
|
|
2976
|
+
request: Elicit.payloadSchema,
|
|
2977
|
+
cause: Schema9.optional(Schema9.Defect)
|
|
2978
|
+
}) {
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
class McpServerClient extends Context9.Tag("@effect/ai/McpSchema/McpServerClient")() {
|
|
2982
|
+
}
|
|
2983
|
+
|
|
2984
|
+
class McpServerClientMiddleware extends RpcMiddleware.Tag()("@effect/ai/McpSchema/McpServerClientMiddleware", {
|
|
2985
|
+
provides: McpServerClient
|
|
2986
|
+
}) {
|
|
2987
|
+
}
|
|
2988
|
+
|
|
2989
|
+
class ClientRequestRpcs extends RpcGroup.make(Ping, Initialize, Complete, SetLevel, GetPrompt, ListPrompts, ListResources, ListResourceTemplates, ReadResource, Subscribe, Unsubscribe, CallTool, ListTools).middleware(McpServerClientMiddleware) {
|
|
2990
|
+
}
|
|
2991
|
+
|
|
2992
|
+
class ClientNotificationRpcs extends RpcGroup.make(CancelledNotification, ProgressNotification, InitializedNotification, RootsListChangedNotification) {
|
|
2993
|
+
}
|
|
2994
|
+
|
|
2995
|
+
class ClientRpcs extends ClientRequestRpcs.merge(ClientNotificationRpcs) {
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
class ServerRequestRpcs extends RpcGroup.make(Ping, CreateMessage, ListRoots, Elicit) {
|
|
2999
|
+
}
|
|
3000
|
+
|
|
3001
|
+
class ServerNotificationRpcs extends RpcGroup.make(CancelledNotification, ProgressNotification, LoggingMessageNotification, ResourceUpdatedNotification, ResourceListChangedNotification, ToolListChangedNotification, PromptListChangedNotification) {
|
|
3002
|
+
}
|
|
3003
|
+
var ParamAnnotation = Symbol.for("@effect/ai/McpSchema/ParamNameId");
|
|
3004
|
+
var param = (id, schema) => schema.annotations({
|
|
3005
|
+
[ParamAnnotation]: id
|
|
3006
|
+
});
|
|
3007
|
+
// packages/ariadne/src/McpServer.ts
|
|
3008
|
+
var exports_McpServer = {};
|
|
3009
|
+
__export(exports_McpServer, {
|
|
3010
|
+
toolkit: () => toolkit,
|
|
3011
|
+
run: () => run,
|
|
3012
|
+
resource: () => resource,
|
|
3013
|
+
registerToolkit: () => registerToolkit,
|
|
3014
|
+
registerResource: () => registerResource,
|
|
3015
|
+
registerPrompt: () => registerPrompt,
|
|
3016
|
+
prompt: () => prompt,
|
|
3017
|
+
layerStdio: () => layerStdio,
|
|
3018
|
+
layerHttpRouter: () => layerHttpRouter,
|
|
3019
|
+
layerHttp: () => layerHttp,
|
|
3020
|
+
layer: () => layer2,
|
|
3021
|
+
elicit: () => elicit,
|
|
3022
|
+
McpServer: () => McpServer
|
|
3023
|
+
});
|
|
3024
|
+
var Headers = __toESM(require("@effect/platform/Headers"));
|
|
3025
|
+
var RpcClient = __toESM(require("@effect/rpc/RpcClient"));
|
|
3026
|
+
var RpcSerialization = __toESM(require("@effect/rpc/RpcSerialization"));
|
|
3027
|
+
var RpcServer = __toESM(require("@effect/rpc/RpcServer"));
|
|
3028
|
+
var Arr2 = __toESM(require("effect/Array"));
|
|
3029
|
+
var Cause2 = __toESM(require("effect/Cause"));
|
|
3030
|
+
var Context10 = __toESM(require("effect/Context"));
|
|
3031
|
+
var Effect9 = __toESM(require("effect/Effect"));
|
|
3032
|
+
var Exit = __toESM(require("effect/Exit"));
|
|
3033
|
+
var JsonSchema2 = __toESM(require("effect/JSONSchema"));
|
|
3034
|
+
var Layer5 = __toESM(require("effect/Layer"));
|
|
3035
|
+
var Logger = __toESM(require("effect/Logger"));
|
|
3036
|
+
var Mailbox2 = __toESM(require("effect/Mailbox"));
|
|
3037
|
+
var Option6 = __toESM(require("effect/Option"));
|
|
3038
|
+
var RcMap = __toESM(require("effect/RcMap"));
|
|
3039
|
+
var Schema10 = __toESM(require("effect/Schema"));
|
|
3040
|
+
var AST2 = __toESM(require("effect/SchemaAST"));
|
|
3041
|
+
var FindMyWay = __toESM(require("find-my-way-ts"));
|
|
3042
|
+
class McpServer extends Context10.Tag("@effect/ai/McpServer")() {
|
|
3043
|
+
static make = Effect9.gen(function* () {
|
|
3044
|
+
const matcher = makeUriMatcher();
|
|
3045
|
+
const tools = Arr2.empty();
|
|
3046
|
+
const toolMap = new Map;
|
|
3047
|
+
const resources = [];
|
|
3048
|
+
const resourceTemplates = [];
|
|
3049
|
+
const prompts = [];
|
|
3050
|
+
const promptMap = new Map;
|
|
3051
|
+
const completionsMap = new Map;
|
|
3052
|
+
const notificationsMailbox = yield* Mailbox2.make();
|
|
3053
|
+
const listChangedHandles = new Map;
|
|
3054
|
+
const notifications = yield* RpcClient.makeNoSerialization(ServerNotificationRpcs, {
|
|
3055
|
+
spanPrefix: "McpServer/Notifications",
|
|
3056
|
+
onFromClient(options) {
|
|
3057
|
+
const message = options.message;
|
|
3058
|
+
if (message._tag !== "Request") {
|
|
3059
|
+
return Effect9.void;
|
|
3060
|
+
}
|
|
3061
|
+
if (message.tag.includes("list_changed")) {
|
|
3062
|
+
if (!listChangedHandles.has(message.tag)) {
|
|
3063
|
+
listChangedHandles.set(message.tag, setTimeout(() => {
|
|
3064
|
+
notificationsMailbox.unsafeOffer(message);
|
|
3065
|
+
listChangedHandles.delete(message.tag);
|
|
3066
|
+
}, 0));
|
|
3067
|
+
}
|
|
3068
|
+
} else {
|
|
3069
|
+
notificationsMailbox.unsafeOffer(message);
|
|
3070
|
+
}
|
|
3071
|
+
return notifications.write({
|
|
3072
|
+
clientId: 0,
|
|
3073
|
+
requestId: message.id,
|
|
3074
|
+
_tag: "Exit",
|
|
3075
|
+
exit: Exit.void
|
|
3076
|
+
});
|
|
3077
|
+
}
|
|
3078
|
+
});
|
|
3079
|
+
return McpServer.of({
|
|
3080
|
+
notifications: notifications.client,
|
|
3081
|
+
notificationsMailbox,
|
|
3082
|
+
initializedClients: new Set,
|
|
3083
|
+
get tools() {
|
|
3084
|
+
return tools;
|
|
3085
|
+
},
|
|
3086
|
+
addTool: (options) => Effect9.suspend(() => {
|
|
3087
|
+
tools.push(options.tool);
|
|
3088
|
+
toolMap.set(options.tool.name, options.handle);
|
|
3089
|
+
return notifications.client["notifications/tools/list_changed"]({});
|
|
3090
|
+
}),
|
|
3091
|
+
callTool: (request2) => Effect9.suspend(() => {
|
|
3092
|
+
const handle = toolMap.get(request2.name);
|
|
3093
|
+
if (!handle) {
|
|
3094
|
+
return Effect9.fail(new InvalidParams({
|
|
3095
|
+
message: `Tool '${request2.name}' not found`
|
|
3096
|
+
}));
|
|
3097
|
+
}
|
|
3098
|
+
return handle(request2.arguments);
|
|
3099
|
+
}),
|
|
3100
|
+
get resources() {
|
|
3101
|
+
return resources;
|
|
3102
|
+
},
|
|
3103
|
+
get resourceTemplates() {
|
|
3104
|
+
return resourceTemplates;
|
|
3105
|
+
},
|
|
3106
|
+
addResource: (resource, effect3) => Effect9.suspend(() => {
|
|
3107
|
+
resources.push(resource);
|
|
3108
|
+
matcher.add(resource.uri, { _tag: "Resource", effect: effect3 });
|
|
3109
|
+
return notifications.client["notifications/resources/list_changed"]({});
|
|
3110
|
+
}),
|
|
3111
|
+
addResourceTemplate: ({
|
|
3112
|
+
completions,
|
|
3113
|
+
handle,
|
|
3114
|
+
routerPath,
|
|
3115
|
+
template
|
|
3116
|
+
}) => Effect9.suspend(() => {
|
|
3117
|
+
resourceTemplates.push(template);
|
|
3118
|
+
matcher.add(routerPath, {
|
|
3119
|
+
_tag: "ResourceTemplate",
|
|
3120
|
+
handle
|
|
3121
|
+
});
|
|
3122
|
+
for (const [param2, handle2] of Object.entries(completions)) {
|
|
3123
|
+
completionsMap.set(`ref/resource/${template.uriTemplate}/${param2}`, handle2);
|
|
3124
|
+
}
|
|
3125
|
+
return notifications.client["notifications/resources/list_changed"]({});
|
|
3126
|
+
}),
|
|
3127
|
+
findResource: (uri) => Effect9.suspend(() => {
|
|
3128
|
+
const match3 = matcher.find(uri);
|
|
3129
|
+
if (!match3) {
|
|
3130
|
+
return Effect9.succeed({ contents: [] });
|
|
3131
|
+
} else if (match3.handler._tag === "Resource") {
|
|
3132
|
+
return match3.handler.effect;
|
|
3133
|
+
}
|
|
3134
|
+
const params = [];
|
|
3135
|
+
for (const key of Object.keys(match3.params)) {
|
|
3136
|
+
params[Number(key)] = match3.params[key];
|
|
3137
|
+
}
|
|
3138
|
+
return match3.handler.handle(uri, params);
|
|
3139
|
+
}),
|
|
3140
|
+
get prompts() {
|
|
3141
|
+
return prompts;
|
|
3142
|
+
},
|
|
3143
|
+
addPrompt: (options) => Effect9.suspend(() => {
|
|
3144
|
+
prompts.push(options.prompt);
|
|
3145
|
+
promptMap.set(options.prompt.name, options.handle);
|
|
3146
|
+
for (const [param2, handle] of Object.entries(options.completions)) {
|
|
3147
|
+
completionsMap.set(`ref/prompt/${options.prompt.name}/${param2}`, handle);
|
|
3148
|
+
}
|
|
3149
|
+
return notifications.client["notifications/prompts/list_changed"]({});
|
|
3150
|
+
}),
|
|
3151
|
+
getPromptResult: Effect9.fnUntraced(function* ({
|
|
3152
|
+
arguments: params,
|
|
3153
|
+
name
|
|
3154
|
+
}) {
|
|
3155
|
+
const handler = promptMap.get(name);
|
|
3156
|
+
if (!handler) {
|
|
3157
|
+
return yield* new InvalidParams({
|
|
3158
|
+
message: `Prompt '${name}' not found`
|
|
3159
|
+
});
|
|
3160
|
+
}
|
|
3161
|
+
return yield* handler(params ?? {});
|
|
3162
|
+
}),
|
|
3163
|
+
completion: Effect9.fnUntraced(function* (complete) {
|
|
3164
|
+
const ref = complete.ref;
|
|
3165
|
+
const key = ref.type === "ref/resource" ? `ref/resource/${ref.uri}/${complete.argument.name}` : `ref/prompt/${ref.name}/${complete.argument.name}`;
|
|
3166
|
+
const handler = completionsMap.get(key);
|
|
3167
|
+
return handler ? yield* handler(complete.argument.value) : CompleteResult.empty;
|
|
3168
|
+
})
|
|
3169
|
+
});
|
|
3170
|
+
});
|
|
3171
|
+
static layer = Layer5.scoped(McpServer, McpServer.make);
|
|
3172
|
+
}
|
|
3173
|
+
var LATEST_PROTOCOL_VERSION = "2025-06-18";
|
|
3174
|
+
var SUPPORTED_PROTOCOL_VERSIONS = [
|
|
3175
|
+
LATEST_PROTOCOL_VERSION,
|
|
3176
|
+
"2025-03-26",
|
|
3177
|
+
"2024-11-05",
|
|
3178
|
+
"2024-10-07"
|
|
3179
|
+
];
|
|
3180
|
+
var run = Effect9.fnUntraced(function* (options) {
|
|
3181
|
+
const protocol = yield* RpcServer.Protocol;
|
|
3182
|
+
const handlers = yield* Layer5.build(layerHandlers(options));
|
|
3183
|
+
const server = yield* McpServer;
|
|
3184
|
+
const clients = yield* RcMap.make({
|
|
3185
|
+
lookup: Effect9.fnUntraced(function* (clientId) {
|
|
3186
|
+
let write;
|
|
3187
|
+
const client = yield* RpcClient.make(ServerRequestRpcs, {
|
|
3188
|
+
spanPrefix: "McpServer/Client"
|
|
3189
|
+
}).pipe(Effect9.provideServiceEffect(RpcClient.Protocol, RpcClient.Protocol.make(Effect9.fnUntraced(function* (writeResponse) {
|
|
3190
|
+
write = writeResponse;
|
|
3191
|
+
return {
|
|
3192
|
+
send(request2, _transferables) {
|
|
3193
|
+
return protocol.send(clientId, {
|
|
3194
|
+
...request2,
|
|
3195
|
+
headers: undefined,
|
|
3196
|
+
traceId: undefined,
|
|
3197
|
+
spanId: undefined,
|
|
3198
|
+
sampled: undefined
|
|
3199
|
+
});
|
|
3200
|
+
},
|
|
3201
|
+
supportsAck: true,
|
|
3202
|
+
supportsTransferables: false
|
|
3203
|
+
};
|
|
3204
|
+
}))));
|
|
3205
|
+
return { client, write };
|
|
3206
|
+
}),
|
|
3207
|
+
idleTimeToLive: 1e4
|
|
3208
|
+
});
|
|
3209
|
+
const clientMiddleware = McpServerClientMiddleware.of(({ clientId }) => Effect9.sync(() => McpServerClient.of({
|
|
3210
|
+
clientId,
|
|
3211
|
+
getClient: RcMap.get(clients, clientId).pipe(Effect9.map(({ client }) => client))
|
|
3212
|
+
})));
|
|
3213
|
+
const patchedProtocol = RpcServer.Protocol.of({
|
|
3214
|
+
...protocol,
|
|
3215
|
+
run: (f) => protocol.run((clientId, request_) => {
|
|
3216
|
+
const request2 = request_;
|
|
3217
|
+
switch (request2._tag) {
|
|
3218
|
+
case "Request": {
|
|
3219
|
+
if (ClientNotificationRpcs.requests.has(request2.tag)) {
|
|
3220
|
+
if (request2.tag === "notifications/cancelled") {
|
|
3221
|
+
return f(clientId, {
|
|
3222
|
+
_tag: "Interrupt",
|
|
3223
|
+
requestId: String(request2.payload.requestId)
|
|
3224
|
+
});
|
|
3225
|
+
}
|
|
3226
|
+
const handler = handlers.unsafeMap.get(request2.tag);
|
|
3227
|
+
return handler ? handler.handler(request2.payload, {
|
|
3228
|
+
clientId,
|
|
3229
|
+
headers: Headers.fromInput(request2.headers)
|
|
3230
|
+
}) : Effect9.void;
|
|
3231
|
+
}
|
|
3232
|
+
return f(clientId, request2);
|
|
3233
|
+
}
|
|
3234
|
+
case "Ping":
|
|
3235
|
+
case "Ack":
|
|
3236
|
+
case "Interrupt":
|
|
3237
|
+
case "Eof":
|
|
3238
|
+
return f(clientId, request2);
|
|
3239
|
+
case "Pong":
|
|
3240
|
+
case "Exit":
|
|
3241
|
+
case "Chunk":
|
|
3242
|
+
case "ClientProtocolError":
|
|
3243
|
+
case "Defect":
|
|
3244
|
+
return RcMap.get(clients, clientId).pipe(Effect9.flatMap(({ write }) => write(request2)), Effect9.scoped);
|
|
3245
|
+
}
|
|
3246
|
+
})
|
|
3247
|
+
});
|
|
3248
|
+
const encodeNotification = Schema10.encode(Schema10.Union(...Array.from(ServerNotificationRpcs.requests.values(), (rpc) => rpc.payloadSchema)));
|
|
3249
|
+
yield* server.notificationsMailbox.take.pipe(Effect9.flatMap(Effect9.fnUntraced(function* (request2) {
|
|
3250
|
+
const encoded = yield* encodeNotification(request2.payload);
|
|
3251
|
+
const message = {
|
|
3252
|
+
_tag: "Request",
|
|
3253
|
+
tag: request2.tag,
|
|
3254
|
+
payload: encoded
|
|
3255
|
+
};
|
|
3256
|
+
const clientIds = yield* patchedProtocol.clientIds;
|
|
3257
|
+
for (const clientId of server.initializedClients) {
|
|
3258
|
+
if (!clientIds.has(clientId)) {
|
|
3259
|
+
server.initializedClients.delete(clientId);
|
|
3260
|
+
continue;
|
|
3261
|
+
}
|
|
3262
|
+
yield* patchedProtocol.send(clientId, message);
|
|
3263
|
+
}
|
|
3264
|
+
})), Effect9.catchAllCause(() => Effect9.void), Effect9.forever, Effect9.forkScoped);
|
|
3265
|
+
return yield* RpcServer.make(ClientRpcs, {
|
|
3266
|
+
spanPrefix: "McpServer",
|
|
3267
|
+
disableFatalDefects: true
|
|
3268
|
+
}).pipe(Effect9.provideService(RpcServer.Protocol, patchedProtocol), Effect9.provideService(McpServerClientMiddleware, clientMiddleware), Effect9.provide(handlers));
|
|
3269
|
+
}, Effect9.scoped);
|
|
3270
|
+
var layer2 = (options) => Layer5.scopedDiscard(Effect9.forkScoped(run(options))).pipe(Layer5.provideMerge(McpServer.layer));
|
|
3271
|
+
var layerStdio = (options) => layer2(options).pipe(Layer5.provide(RpcServer.layerProtocolStdio({
|
|
3272
|
+
stdin: options.stdin,
|
|
3273
|
+
stdout: options.stdout
|
|
3274
|
+
})), Layer5.provide(RpcSerialization.layerNdJsonRpc()), Layer5.provideMerge(Logger.remove(Logger.defaultLogger)), Layer5.provideMerge(Logger.remove(Logger.prettyLoggerDefault)));
|
|
3275
|
+
var layerHttp = (options) => layer2(options).pipe(Layer5.provide(RpcServer.layerProtocolHttp(options)), Layer5.provide(RpcSerialization.layerJsonRpc()));
|
|
3276
|
+
var layerHttpRouter = (options) => layer2(options).pipe(Layer5.provide(RpcServer.layerProtocolHttpRouter(options)), Layer5.provide(RpcSerialization.layerJsonRpc()));
|
|
3277
|
+
var registerToolkit = Effect9.fnUntraced(function* (toolkit) {
|
|
3278
|
+
const registry = yield* McpServer;
|
|
3279
|
+
const built = yield* toolkit;
|
|
3280
|
+
const context5 = yield* Effect9.context();
|
|
3281
|
+
for (const tool of Object.values(built.tools)) {
|
|
3282
|
+
const mcpTool = new Tool({
|
|
3283
|
+
name: tool.name,
|
|
3284
|
+
description: tool.description,
|
|
3285
|
+
inputSchema: makeJsonSchema(tool.parametersSchema.ast),
|
|
3286
|
+
annotations: new ToolAnnotations({
|
|
3287
|
+
...Context10.getOption(tool.annotations, Title).pipe(Option6.map((title) => ({ title })), Option6.getOrUndefined),
|
|
3288
|
+
readOnlyHint: Context10.get(tool.annotations, Readonly),
|
|
3289
|
+
destructiveHint: Context10.get(tool.annotations, Destructive),
|
|
3290
|
+
idempotentHint: Context10.get(tool.annotations, Idempotent),
|
|
3291
|
+
openWorldHint: Context10.get(tool.annotations, OpenWorld)
|
|
3292
|
+
})
|
|
3293
|
+
});
|
|
3294
|
+
yield* registry.addTool({
|
|
3295
|
+
tool: mcpTool,
|
|
3296
|
+
handle(payload) {
|
|
3297
|
+
return built.handle(tool.name, payload).pipe(Effect9.provide(context5), Effect9.match({
|
|
3298
|
+
onFailure: (error) => new CallToolResult({
|
|
3299
|
+
isError: true,
|
|
3300
|
+
structuredContent: typeof error === "object" ? error : undefined,
|
|
3301
|
+
content: [
|
|
3302
|
+
{
|
|
3303
|
+
type: "text",
|
|
3304
|
+
text: JSON.stringify(error)
|
|
3305
|
+
}
|
|
3306
|
+
]
|
|
3307
|
+
}),
|
|
3308
|
+
onSuccess: (result) => new CallToolResult({
|
|
3309
|
+
isError: false,
|
|
3310
|
+
structuredContent: typeof result.encodedResult === "object" ? result.encodedResult : undefined,
|
|
3311
|
+
content: [
|
|
3312
|
+
{
|
|
3313
|
+
type: "text",
|
|
3314
|
+
text: JSON.stringify(result.encodedResult)
|
|
3315
|
+
}
|
|
3316
|
+
]
|
|
3317
|
+
})
|
|
3318
|
+
}));
|
|
3319
|
+
}
|
|
3320
|
+
});
|
|
3321
|
+
}
|
|
3322
|
+
});
|
|
3323
|
+
var toolkit = (toolkit2) => Layer5.effectDiscard(registerToolkit(toolkit2)).pipe(Layer5.provide(McpServer.layer));
|
|
3324
|
+
var registerResource = function() {
|
|
3325
|
+
if (arguments.length === 1) {
|
|
3326
|
+
const options = arguments[0];
|
|
3327
|
+
return Effect9.gen(function* () {
|
|
3328
|
+
const context5 = yield* Effect9.context();
|
|
3329
|
+
const registry = yield* McpServer;
|
|
3330
|
+
yield* registry.addResource(new Resource({
|
|
3331
|
+
...options,
|
|
3332
|
+
annotations: options
|
|
3333
|
+
}), options.content.pipe(Effect9.provide(context5), Effect9.map((content) => resolveResourceContent(options.uri, content)), Effect9.catchAllCause((cause) => {
|
|
3334
|
+
const prettyError = Cause2.prettyErrors(cause)[0];
|
|
3335
|
+
return new InternalError({
|
|
3336
|
+
message: prettyError.message
|
|
3337
|
+
});
|
|
3338
|
+
})));
|
|
3339
|
+
});
|
|
3340
|
+
}
|
|
3341
|
+
const { params, routerPath, schema, uriPath } = compileUriTemplate(...arguments);
|
|
3342
|
+
return Effect9.fnUntraced(function* (options) {
|
|
3343
|
+
const context5 = yield* Effect9.context();
|
|
3344
|
+
const registry = yield* McpServer;
|
|
3345
|
+
const decode4 = Schema10.decodeUnknown(schema);
|
|
3346
|
+
const template = new ResourceTemplate({
|
|
3347
|
+
...options,
|
|
3348
|
+
uriTemplate: uriPath,
|
|
3349
|
+
annotations: options
|
|
3350
|
+
});
|
|
3351
|
+
const completions = {};
|
|
3352
|
+
for (const [param2, handle] of Object.entries(options.completion ?? {})) {
|
|
3353
|
+
const encodeArray = Schema10.encodeUnknown(Schema10.Array(params[param2]));
|
|
3354
|
+
const handler = (input) => handle(input).pipe(Effect9.flatMap(encodeArray), Effect9.map((values) => new CompleteResult({
|
|
3355
|
+
completion: {
|
|
3356
|
+
values,
|
|
3357
|
+
total: values.length,
|
|
3358
|
+
hasMore: false
|
|
3359
|
+
}
|
|
3360
|
+
})), Effect9.catchAllCause((cause) => {
|
|
3361
|
+
const prettyError = Cause2.prettyErrors(cause)[0];
|
|
3362
|
+
return new InternalError({
|
|
3363
|
+
message: prettyError.message
|
|
3364
|
+
});
|
|
3365
|
+
}), Effect9.provide(context5));
|
|
3366
|
+
completions[param2] = handler;
|
|
3367
|
+
}
|
|
3368
|
+
yield* registry.addResourceTemplate({
|
|
3369
|
+
template,
|
|
3370
|
+
routerPath,
|
|
3371
|
+
completions,
|
|
3372
|
+
handle: (uri, params2) => decode4(params2).pipe(Effect9.mapError((error) => new InvalidParams({ message: error.message })), Effect9.flatMap((params3) => options.content(uri, ...params3).pipe(Effect9.map((content) => resolveResourceContent(uri, content)), Effect9.catchAllCause((cause) => {
|
|
3373
|
+
const prettyError = Cause2.prettyErrors(cause)[0];
|
|
3374
|
+
return new InternalError({
|
|
3375
|
+
message: prettyError.message
|
|
3376
|
+
});
|
|
3377
|
+
}))), Effect9.provide(context5))
|
|
3378
|
+
});
|
|
3379
|
+
});
|
|
3380
|
+
};
|
|
3381
|
+
var resource = function() {
|
|
3382
|
+
if (arguments.length === 1) {
|
|
3383
|
+
return Layer5.effectDiscard(registerResource(arguments[0])).pipe(Layer5.provide(McpServer.layer));
|
|
3384
|
+
}
|
|
3385
|
+
const register = registerResource(...arguments);
|
|
3386
|
+
return (options) => Layer5.effectDiscard(register(options)).pipe(Layer5.provide(McpServer.layer));
|
|
3387
|
+
};
|
|
3388
|
+
var registerPrompt = (options) => {
|
|
3389
|
+
const args = Arr2.empty();
|
|
3390
|
+
const props = {};
|
|
3391
|
+
const propSignatures = options.parameters ? AST2.getPropertySignatures(options.parameters.ast) : [];
|
|
3392
|
+
for (const prop of propSignatures) {
|
|
3393
|
+
args.push({
|
|
3394
|
+
name: prop.name,
|
|
3395
|
+
description: Option6.getOrUndefined(AST2.getDescriptionAnnotation(prop)),
|
|
3396
|
+
required: !prop.isOptional
|
|
3397
|
+
});
|
|
3398
|
+
props[prop.name] = Schema10.make(prop.type);
|
|
3399
|
+
}
|
|
3400
|
+
const prompt = new Prompt2({
|
|
3401
|
+
name: options.name,
|
|
3402
|
+
description: options.description,
|
|
3403
|
+
arguments: args
|
|
3404
|
+
});
|
|
3405
|
+
const decode4 = options.parameters ? Schema10.decodeUnknown(options.parameters) : () => Effect9.succeed({});
|
|
3406
|
+
const completion = options.completion ?? {};
|
|
3407
|
+
return Effect9.gen(function* () {
|
|
3408
|
+
const registry = yield* McpServer;
|
|
3409
|
+
const context5 = yield* Effect9.context();
|
|
3410
|
+
const completions = {};
|
|
3411
|
+
for (const [param2, handle] of Object.entries(completion)) {
|
|
3412
|
+
const encodeArray = Schema10.encodeUnknown(Schema10.Array(props[param2]));
|
|
3413
|
+
const handler = (input) => handle(input).pipe(Effect9.flatMap(encodeArray), Effect9.map((values) => ({
|
|
3414
|
+
completion: {
|
|
3415
|
+
values,
|
|
3416
|
+
total: values.length,
|
|
3417
|
+
hasMore: false
|
|
3418
|
+
}
|
|
3419
|
+
})), Effect9.catchAllCause((cause) => {
|
|
3420
|
+
const prettyError = Cause2.prettyErrors(cause)[0];
|
|
3421
|
+
return new InternalError({
|
|
3422
|
+
message: prettyError.message
|
|
3423
|
+
});
|
|
3424
|
+
}), Effect9.provide(context5));
|
|
3425
|
+
completions[param2] = handler;
|
|
3426
|
+
}
|
|
3427
|
+
yield* registry.addPrompt({
|
|
3428
|
+
prompt,
|
|
3429
|
+
completions,
|
|
3430
|
+
handle: (params) => decode4(params).pipe(Effect9.mapError((error) => new InvalidParams({ message: error.message })), Effect9.flatMap((params2) => options.content(params2)), Effect9.map((messages) => {
|
|
3431
|
+
messages = typeof messages === "string" ? [
|
|
3432
|
+
{
|
|
3433
|
+
role: "user",
|
|
3434
|
+
content: TextContent.make({
|
|
3435
|
+
text: messages
|
|
3436
|
+
})
|
|
3437
|
+
}
|
|
3438
|
+
] : messages;
|
|
3439
|
+
return new GetPromptResult({
|
|
3440
|
+
messages,
|
|
3441
|
+
description: prompt.description
|
|
3442
|
+
});
|
|
3443
|
+
}), Effect9.catchAllCause((cause) => {
|
|
3444
|
+
const prettyError = Cause2.prettyErrors(cause)[0];
|
|
3445
|
+
return new InternalError({
|
|
3446
|
+
message: prettyError.message
|
|
3447
|
+
});
|
|
3448
|
+
}), Effect9.provide(context5))
|
|
3449
|
+
});
|
|
3450
|
+
});
|
|
3451
|
+
};
|
|
3452
|
+
var prompt = (options) => Layer5.effectDiscard(registerPrompt(options)).pipe(Layer5.provide(McpServer.layer));
|
|
3453
|
+
var elicit = Effect9.fnUntraced(function* (options) {
|
|
3454
|
+
const { getClient } = yield* McpServerClient;
|
|
3455
|
+
const client = yield* getClient;
|
|
3456
|
+
const request2 = Elicit.payloadSchema.make({
|
|
3457
|
+
message: options.message,
|
|
3458
|
+
requestedSchema: makeJsonSchema(options.schema.ast)
|
|
3459
|
+
});
|
|
3460
|
+
const res = yield* client["elicitation/create"](request2).pipe(Effect9.catchAllCause((cause) => Effect9.fail(new ElicitationDeclined({
|
|
3461
|
+
cause: Cause2.squash(cause),
|
|
3462
|
+
request: request2
|
|
3463
|
+
}))));
|
|
3464
|
+
switch (res.action) {
|
|
3465
|
+
case "accept":
|
|
3466
|
+
return yield* Effect9.orDie(Schema10.decodeUnknown(options.schema)(res.content));
|
|
3467
|
+
case "cancel":
|
|
3468
|
+
return yield* Effect9.interrupt;
|
|
3469
|
+
case "decline":
|
|
3470
|
+
return yield* Effect9.fail(new ElicitationDeclined({ request: request2 }));
|
|
3471
|
+
}
|
|
3472
|
+
}, Effect9.scoped);
|
|
3473
|
+
var makeUriMatcher = () => {
|
|
3474
|
+
const router = FindMyWay.make({
|
|
3475
|
+
ignoreTrailingSlash: true,
|
|
3476
|
+
ignoreDuplicateSlashes: true,
|
|
3477
|
+
caseSensitive: true
|
|
3478
|
+
});
|
|
3479
|
+
const add2 = (uri, value) => {
|
|
3480
|
+
router.on("GET", uri, value);
|
|
3481
|
+
};
|
|
3482
|
+
const find = (uri) => router.find("GET", uri);
|
|
3483
|
+
return { add: add2, find };
|
|
3484
|
+
};
|
|
3485
|
+
var compileUriTemplate = (segments, ...schemas) => {
|
|
3486
|
+
let routerPath = segments[0].replace(":", "::");
|
|
3487
|
+
let uriPath = segments[0];
|
|
3488
|
+
const params = {};
|
|
3489
|
+
let pathSchema = Schema10.Tuple();
|
|
3490
|
+
if (schemas.length > 0) {
|
|
3491
|
+
const arr = [];
|
|
3492
|
+
for (let i = 0;i < schemas.length; i++) {
|
|
3493
|
+
const schema = schemas[i];
|
|
3494
|
+
const segment = segments[i + 1];
|
|
3495
|
+
const key = String(i);
|
|
3496
|
+
arr.push(schema);
|
|
3497
|
+
routerPath += `:${key}${segment.replace(":", "::")}`;
|
|
3498
|
+
const paramName = AST2.getAnnotation(ParamAnnotation)(schema.ast).pipe(Option6.getOrElse(() => `param${key}`));
|
|
3499
|
+
params[paramName] = schema;
|
|
3500
|
+
uriPath += `{${paramName}}${segment}`;
|
|
3501
|
+
}
|
|
3502
|
+
pathSchema = Schema10.Tuple(...arr);
|
|
3503
|
+
}
|
|
3504
|
+
return {
|
|
3505
|
+
routerPath,
|
|
3506
|
+
uriPath,
|
|
3507
|
+
schema: pathSchema,
|
|
3508
|
+
params
|
|
3509
|
+
};
|
|
3510
|
+
};
|
|
3511
|
+
var layerHandlers = (serverInfo) => ClientRpcs.toLayer(Effect9.gen(function* () {
|
|
3512
|
+
const server = yield* McpServer;
|
|
3513
|
+
return {
|
|
3514
|
+
ping: () => Effect9.succeed({}),
|
|
3515
|
+
initialize(params, { clientId }) {
|
|
3516
|
+
const requestedVersion = params.protocolVersion;
|
|
3517
|
+
const capabilities = {
|
|
3518
|
+
completions: {}
|
|
3519
|
+
};
|
|
3520
|
+
if (server.tools.length > 0) {
|
|
3521
|
+
capabilities.tools = { listChanged: true };
|
|
3522
|
+
}
|
|
3523
|
+
if (server.resources.length > 0 || server.resourceTemplates.length > 0) {
|
|
3524
|
+
capabilities.resources = {
|
|
3525
|
+
listChanged: true,
|
|
3526
|
+
subscribe: false
|
|
3527
|
+
};
|
|
3528
|
+
}
|
|
3529
|
+
if (server.prompts.length > 0) {
|
|
3530
|
+
capabilities.prompts = { listChanged: true };
|
|
3531
|
+
}
|
|
3532
|
+
server.initializedClients.add(clientId);
|
|
3533
|
+
return Effect9.succeed({
|
|
3534
|
+
capabilities,
|
|
3535
|
+
serverInfo,
|
|
3536
|
+
protocolVersion: SUPPORTED_PROTOCOL_VERSIONS.includes(requestedVersion) ? requestedVersion : LATEST_PROTOCOL_VERSION
|
|
3537
|
+
});
|
|
3538
|
+
},
|
|
3539
|
+
"completion/complete": server.completion,
|
|
3540
|
+
"logging/setLevel": () => InternalError.notImplemented,
|
|
3541
|
+
"prompts/get": server.getPromptResult,
|
|
3542
|
+
"prompts/list": () => Effect9.sync(() => new ListPromptsResult({ prompts: server.prompts })),
|
|
3543
|
+
"resources/list": () => Effect9.sync(() => new ListResourcesResult({
|
|
3544
|
+
resources: server.resources
|
|
3545
|
+
})),
|
|
3546
|
+
"resources/read": ({ uri }) => server.findResource(uri),
|
|
3547
|
+
"resources/subscribe": () => InternalError.notImplemented,
|
|
3548
|
+
"resources/unsubscribe": () => InternalError.notImplemented,
|
|
3549
|
+
"resources/templates/list": () => Effect9.sync(() => new ListResourceTemplatesResult({
|
|
3550
|
+
resourceTemplates: server.resourceTemplates
|
|
3551
|
+
})),
|
|
3552
|
+
"tools/call": server.callTool,
|
|
3553
|
+
"tools/list": () => Effect9.sync(() => new ListToolsResult({ tools: server.tools })),
|
|
3554
|
+
"notifications/cancelled": (_) => Effect9.void,
|
|
3555
|
+
"notifications/initialized": (_) => Effect9.void,
|
|
3556
|
+
"notifications/progress": (_) => Effect9.void,
|
|
3557
|
+
"notifications/roots/list_changed": (_) => Effect9.void
|
|
3558
|
+
};
|
|
3559
|
+
}));
|
|
3560
|
+
var makeJsonSchema = (ast) => {
|
|
3561
|
+
const props = AST2.getPropertySignatures(ast);
|
|
3562
|
+
if (props.length === 0) {
|
|
3563
|
+
return {
|
|
3564
|
+
type: "object",
|
|
3565
|
+
properties: {},
|
|
3566
|
+
required: [],
|
|
3567
|
+
additionalProperties: false
|
|
3568
|
+
};
|
|
3569
|
+
}
|
|
3570
|
+
const $defs = {};
|
|
3571
|
+
const schema = JsonSchema2.fromAST(ast, {
|
|
3572
|
+
definitions: $defs,
|
|
3573
|
+
topLevelReferenceStrategy: "skip"
|
|
3574
|
+
});
|
|
3575
|
+
if (Object.keys($defs).length === 0)
|
|
3576
|
+
return schema;
|
|
3577
|
+
schema.$defs = $defs;
|
|
3578
|
+
return schema;
|
|
3579
|
+
};
|
|
3580
|
+
var resolveResourceContent = (uri, content) => {
|
|
3581
|
+
if (typeof content === "string") {
|
|
3582
|
+
return {
|
|
3583
|
+
contents: [
|
|
3584
|
+
{
|
|
3585
|
+
uri,
|
|
3586
|
+
text: content
|
|
3587
|
+
}
|
|
3588
|
+
]
|
|
3589
|
+
};
|
|
3590
|
+
} else if (content instanceof Uint8Array) {
|
|
3591
|
+
return {
|
|
3592
|
+
contents: [
|
|
3593
|
+
{
|
|
3594
|
+
uri,
|
|
3595
|
+
blob: content
|
|
3596
|
+
}
|
|
3597
|
+
]
|
|
3598
|
+
};
|
|
3599
|
+
}
|
|
3600
|
+
return content;
|
|
3601
|
+
};
|
|
3602
|
+
// packages/ariadne/src/Model.ts
|
|
3603
|
+
var exports_Model = {};
|
|
3604
|
+
__export(exports_Model, {
|
|
3605
|
+
make: () => make19,
|
|
3606
|
+
TypeId: () => TypeId5,
|
|
3607
|
+
ProviderName: () => ProviderName
|
|
3608
|
+
});
|
|
3609
|
+
var Context11 = __toESM(require("effect/Context"));
|
|
3610
|
+
var Effect10 = __toESM(require("effect/Effect"));
|
|
3611
|
+
var import_Effectable2 = require("effect/Effectable");
|
|
3612
|
+
var import_Function7 = require("effect/Function");
|
|
3613
|
+
var Layer6 = __toESM(require("effect/Layer"));
|
|
3614
|
+
var TypeId5 = "~@effect/ai/Model";
|
|
3615
|
+
|
|
3616
|
+
class ProviderName extends Context11.Tag("@effect/ai/Model/ProviderName")() {
|
|
3617
|
+
}
|
|
3618
|
+
var ModelProto = {
|
|
3619
|
+
...import_Effectable2.CommitPrototype,
|
|
3620
|
+
[TypeId5]: TypeId5,
|
|
3621
|
+
[Layer6.LayerTypeId]: {
|
|
3622
|
+
_ROut: import_Function7.identity,
|
|
3623
|
+
_E: import_Function7.identity,
|
|
3624
|
+
_RIn: import_Function7.identity
|
|
3625
|
+
},
|
|
3626
|
+
commit() {
|
|
3627
|
+
return Effect10.contextWith((context5) => {
|
|
3628
|
+
return Layer6.provide(this, Layer6.succeedContext(context5));
|
|
3629
|
+
});
|
|
3630
|
+
}
|
|
3631
|
+
};
|
|
3632
|
+
var make19 = (provider, layer3) => Object.assign(Object.create(ModelProto), { provider }, Layer6.merge(Layer6.succeed(ProviderName, provider), layer3));
|
|
3633
|
+
// packages/ariadne/src/Tokenizer.ts
|
|
3634
|
+
var exports_Tokenizer = {};
|
|
3635
|
+
__export(exports_Tokenizer, {
|
|
3636
|
+
make: () => make20,
|
|
3637
|
+
Tokenizer: () => Tokenizer
|
|
3638
|
+
});
|
|
3639
|
+
var Context12 = __toESM(require("effect/Context"));
|
|
3640
|
+
var Effect11 = __toESM(require("effect/Effect"));
|
|
3641
|
+
var Predicate10 = __toESM(require("effect/Predicate"));
|
|
3642
|
+
class Tokenizer extends Context12.Tag("@effect/ai/Tokenizer")() {
|
|
3643
|
+
}
|
|
3644
|
+
var make20 = (options) => Tokenizer.of({
|
|
3645
|
+
tokenize(input) {
|
|
3646
|
+
return options.tokenize(make2(input));
|
|
3647
|
+
},
|
|
3648
|
+
truncate(input, tokens) {
|
|
3649
|
+
return truncate(make2(input), options.tokenize, tokens);
|
|
3650
|
+
}
|
|
3651
|
+
});
|
|
3652
|
+
var truncate = (self, tokenize, maxTokens) => Effect11.suspend(() => {
|
|
3653
|
+
let count = 0;
|
|
3654
|
+
let inputMessages = self.content;
|
|
3655
|
+
let outputMessages = [];
|
|
3656
|
+
const loop = Effect11.suspend(() => {
|
|
3657
|
+
const message = inputMessages[inputMessages.length - 1];
|
|
3658
|
+
if (Predicate10.isUndefined(message)) {
|
|
3659
|
+
return Effect11.succeed(fromMessages(outputMessages));
|
|
3660
|
+
}
|
|
3661
|
+
inputMessages = inputMessages.slice(0, inputMessages.length - 1);
|
|
3662
|
+
return Effect11.flatMap(tokenize(fromMessages([message])), (tokens) => {
|
|
3663
|
+
count += tokens.length;
|
|
3664
|
+
if (count > maxTokens) {
|
|
3665
|
+
return Effect11.succeed(fromMessages(outputMessages));
|
|
3666
|
+
}
|
|
3667
|
+
outputMessages = [message, ...outputMessages];
|
|
3668
|
+
return loop;
|
|
3669
|
+
});
|
|
3670
|
+
});
|
|
3671
|
+
return loop;
|
|
3672
|
+
});
|