agency-lang 0.7.3 → 0.7.4

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.
@@ -0,0 +1,2975 @@
1
+ import { print, printJSON, input, sleep, read, write, writeBinary, readBinary, range, callback, map, filter, exclude, find, findIndex, reduce, flatMap, every, some, count, sortBy, unique, groupBy } from "agency-lang/stdlib/index.js";
2
+ import { fetchJSON } from "agency-lang/stdlib/http.js";
3
+ import { env } from "agency-lang/stdlib/system.js";
4
+ import { mapValues, values } from "agency-lang/stdlib/object.js";
5
+ import { fileURLToPath } from "url";
6
+ import __process from "process";
7
+ import { readFileSync } from "fs";
8
+ import { z } from "agency-lang/zod";
9
+ import { nanoid } from "agency-lang";
10
+ import path from "path";
11
+ import {
12
+ RuntimeContext,
13
+ Runner,
14
+ setupFunction,
15
+ callHook,
16
+ checkpoint as __checkpoint_impl,
17
+ getCheckpoint as __getCheckpoint_impl,
18
+ restore as __restore_impl,
19
+ _run as __runtime_run_impl,
20
+ interrupt,
21
+ isInterrupt,
22
+ hasInterrupts,
23
+ isDebugger,
24
+ isRejected,
25
+ isApproved,
26
+ interruptWithHandlers,
27
+ respondToInterrupts as _respondToInterrupts,
28
+ rewindFrom as _rewindFrom,
29
+ runExportedFunction as _runExportedFunction,
30
+ RestoreSignal,
31
+ AgencyAbort,
32
+ deepFreeze as __deepFreeze,
33
+ __UNINIT_STATIC,
34
+ __readStatic,
35
+ __registerStaticInit,
36
+ __registerGlobalsInit,
37
+ success,
38
+ failure,
39
+ isSuccess,
40
+ isFailure,
41
+ __eq,
42
+ AgencyFunction as __AgencyFunction,
43
+ UNSET as __UNSET,
44
+ __call,
45
+ __stateStack,
46
+ __globals,
47
+ getRuntimeContext,
48
+ agencyStore,
49
+ functionRefReviver as __functionRefReviver,
50
+ DeterministicClient as __DeterministicClient,
51
+ installFetchMock as __installFetchMock,
52
+ createLogger as __createLogger
53
+ } from "agency-lang/runtime";
54
+ const __filename = fileURLToPath(import.meta.url);
55
+ const __dirname = path.dirname(__filename);
56
+ const __cwd = __process.cwd();
57
+ const getDirname = () => __dirname;
58
+ const __globalCtx = new RuntimeContext({
59
+ statelogConfig: {
60
+ host: "https://statelog.adit.io",
61
+ apiKey: __process.env["STATELOG_API_KEY"] || "",
62
+ projectId: "agency-lang",
63
+ debugMode: false,
64
+ observability: true,
65
+ logFile: "log.jsonl"
66
+ },
67
+ smoltalkDefaults: {
68
+ apiKey: {
69
+ openAi: __process.env["OPENAI_API_KEY"] || "",
70
+ google: __process.env["GEMINI_API_KEY"] || "",
71
+ anthropic: __process.env["ANTHROPIC_API_KEY"] || "",
72
+ openRouter: __process.env["OPENROUTER_API_KEY"] || "",
73
+ deepInfra: __process.env["DEEPINFRA_API_KEY"] || "",
74
+ liteLlm: __process.env["LITELLM_API_KEY"] || "",
75
+ openAiCompat: __process.env["OPENAI_COMPAT_API_KEY"] || ""
76
+ },
77
+ baseUrl: {
78
+ liteLlm: __process.env["LITELLM_BASE_URL"] || "",
79
+ openAiCompat: __process.env["OPENAI_COMPAT_BASE_URL"] || ""
80
+ },
81
+ model: "gpt-4o-mini",
82
+ logLevel: "warn",
83
+ statelog: {
84
+ host: "https://statelog.adit.io",
85
+ projectId: "smoltalk",
86
+ apiKey: __process.env["STATELOG_SMOLTALK_API_KEY"] || "",
87
+ traceId: nanoid()
88
+ }
89
+ },
90
+ dirname: __dirname,
91
+ logLevel: "info",
92
+ traceConfig: {
93
+ program: "stdlib/data/wikidata.agency"
94
+ }
95
+ });
96
+ const graph = __globalCtx.graph;
97
+ function approve(value) {
98
+ return { type: "approve", value };
99
+ }
100
+ function reject(value) {
101
+ return { type: "reject", value };
102
+ }
103
+ function propagate() {
104
+ return { type: "propagate" };
105
+ }
106
+ const respondToInterrupts = (interrupts, responses, opts) => _respondToInterrupts({ ctx: __globalCtx, interrupts, responses, overrides: opts?.overrides, metadata: opts?.metadata, registerTopLevelCallbacks: __registerTopLevelCallbacks, moduleDir: __dirname });
107
+ const rewindFrom = (checkpoint2, overrides, opts) => _rewindFrom({ ctx: __globalCtx, checkpoint: checkpoint2, overrides, metadata: opts?.metadata, registerTopLevelCallbacks: __registerTopLevelCallbacks, moduleDir: __dirname });
108
+ const __invokeFunction = (fn, namedArgs) => _runExportedFunction({ ctx: __globalCtx, fn, namedArgs, initializeGlobals: __initializeGlobals, registerTopLevelCallbacks: __registerTopLevelCallbacks, moduleDir: __dirname });
109
+ const __setDebugger = (dbg) => {
110
+ __globalCtx.debuggerState = dbg;
111
+ };
112
+ const __setTraceFile = (filePath) => {
113
+ __globalCtx.traceConfig.traceFile = filePath;
114
+ };
115
+ const __setLLMClient = (client) => {
116
+ __globalCtx.setLLMClient(client);
117
+ };
118
+ const __getCheckpoints = () => __globalCtx.checkpoints;
119
+ if (__process.env.AGENCY_LLM_MOCKS) {
120
+ __globalCtx.setLLMClient(
121
+ new __DeterministicClient(JSON.parse(__process.env.AGENCY_LLM_MOCKS))
122
+ );
123
+ }
124
+ if (__process.env.AGENCY_FETCH_MOCKS_FILE) {
125
+ __installFetchMock(JSON.parse(readFileSync(__process.env.AGENCY_FETCH_MOCKS_FILE, "utf-8")));
126
+ }
127
+ const __toolRegistry = __functionRefReviver.registry ??= {};
128
+ function __registerTool(value, _aliasName) {
129
+ if (__AgencyFunction.isAgencyFunction(value)) {
130
+ __toolRegistry[`${value.module}:${value.name}`] = value;
131
+ }
132
+ }
133
+ const checkpoint = __AgencyFunction.create({ name: "checkpoint", module: "__runtime", fn: __checkpoint_impl, params: [], toolDefinition: null }, __toolRegistry);
134
+ const getCheckpoint = __AgencyFunction.create({ name: "getCheckpoint", module: "__runtime", fn: __getCheckpoint_impl, params: [{ name: "checkpointId", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry);
135
+ const restore = __AgencyFunction.create({ name: "restore", module: "__runtime", fn: __restore_impl, params: [{ name: "checkpointIdOrCheckpoint", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "options", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry);
136
+ const _run = __AgencyFunction.create({ name: "_run", module: "__runtime", fn: __runtime_run_impl, params: [{ name: "compiled", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "node", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "args", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "wallClock", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "memory", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "ipcPayload", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "stdout", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "configOverrides", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "cwd", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "maxDepth", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry);
137
+ function setLLMClient(client) {
138
+ __globalCtx.setLLMClient(client);
139
+ }
140
+ function registerTools(tools) {
141
+ for (const tool of tools) {
142
+ if (__AgencyFunction.isAgencyFunction(tool)) {
143
+ __toolRegistry[`${tool.module}:${tool.name}`] = tool;
144
+ }
145
+ }
146
+ }
147
+ __registerTool(print);
148
+ __registerTool(printJSON);
149
+ __registerTool(input);
150
+ __registerTool(sleep);
151
+ __registerTool(read);
152
+ __registerTool(write);
153
+ __registerTool(writeBinary);
154
+ __registerTool(readBinary);
155
+ __registerTool(range);
156
+ __registerTool(callback);
157
+ __registerTool(map);
158
+ __registerTool(filter);
159
+ __registerTool(exclude);
160
+ __registerTool(find);
161
+ __registerTool(findIndex);
162
+ __registerTool(reduce);
163
+ __registerTool(flatMap);
164
+ __registerTool(every);
165
+ __registerTool(some);
166
+ __registerTool(count);
167
+ __registerTool(sortBy);
168
+ __registerTool(unique);
169
+ __registerTool(groupBy);
170
+ __registerTool(fetchJSON);
171
+ __registerTool(env);
172
+ __registerTool(mapValues);
173
+ __registerTool(values);
174
+ let __staticInitPromise = null;
175
+ let WD_BASE = __UNINIT_STATIC;
176
+ let WD_DOMAINS = __UNINIT_STATIC;
177
+ let WDQS_BASE = __UNINIT_STATIC;
178
+ let WDQS_DOMAINS = __UNINIT_STATIC;
179
+ async function __initializeStatic(__ctx) {
180
+ if (__staticInitPromise) {
181
+ return __staticInitPromise;
182
+ }
183
+ __staticInitPromise = (async () => {
184
+ WD_BASE = __deepFreeze(`https://www.wikidata.org`);
185
+ WD_DOMAINS = __deepFreeze([`www.wikidata.org`]);
186
+ WDQS_BASE = __deepFreeze(`https://query.wikidata.org`);
187
+ WDQS_DOMAINS = __deepFreeze([`query.wikidata.org`]);
188
+ })();
189
+ return __staticInitPromise;
190
+ }
191
+ function __getStaticVars() {
192
+ return {
193
+ WD_BASE,
194
+ WD_DOMAINS,
195
+ WDQS_BASE,
196
+ WDQS_DOMAINS
197
+ };
198
+ }
199
+ __globalCtx.getStaticVars = __getStaticVars;
200
+ __registerStaticInit("stdlib/data/wikidata.agency", __initializeStatic);
201
+ async function __initializeGlobals(__ctx) {
202
+ if (__ctx.globals.isInitialized("stdlib/data/wikidata.agency")) {
203
+ return;
204
+ }
205
+ __ctx.globals.markInitialized("stdlib/data/wikidata.agency");
206
+ await __initializeStatic(__ctx);
207
+ await __ctx.writeStaticStateToTrace(__globalCtx.getStaticVars());
208
+ __ctx.globals.set("stdlib/data/wikidata.agency", "wikidataUserAgent", await __call(env, {
209
+ type: "positional",
210
+ args: [`WIKIDATA_USER_AGENT`]
211
+ }) ?? `agency-lang (https://agency-lang.com)`);
212
+ __ctx.globals.set("stdlib/data/wikidata.agency", "WD_HEADERS", {
213
+ "User-Agent": __globals().get("stdlib/data/wikidata.agency", "wikidataUserAgent")
214
+ });
215
+ }
216
+ __registerGlobalsInit("stdlib/data/wikidata.agency", __initializeGlobals);
217
+ async function __registerTopLevelCallbacks(__ctx) {
218
+ __ctx.topLevelCallbacks = [];
219
+ }
220
+ __functionRefReviver.registry = __toolRegistry;
221
+ const Entity = z.object({ "id": z.string(), "label": z.string(), "description": z.string(), "url": z.string() });
222
+ const EntityDetail = z.object({ "id": z.string(), "label": z.string(), "description": z.string(), "aliases": z.array(z.string()), "claims": z.record(z.string(), z.array(z.string())) });
223
+ async function __buildSearchPath_impl(name, limit) {
224
+ const __setupData = setupFunction();
225
+ const __stack = __setupData.stack;
226
+ const __step = __setupData.step;
227
+ const __self = __setupData.self;
228
+ const __ctx = getRuntimeContext().ctx;
229
+ let __forked;
230
+ let __functionCompleted = false;
231
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
232
+ await __initializeGlobals(__ctx);
233
+ }
234
+ let __funcStartTime = performance.now();
235
+ __stack.args["name"] = name;
236
+ __stack.args["limit"] = limit;
237
+ __self.__retryable = __self.__retryable ?? true;
238
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "buildSearchPath", threads: __setupData.threads });
239
+ let __resultCheckpointId = -1;
240
+ if (__ctx._pendingArgOverrides) {
241
+ const __overrides = __ctx._pendingArgOverrides;
242
+ __ctx._pendingArgOverrides = void 0;
243
+ if ("name" in __overrides) {
244
+ name = __overrides["name"];
245
+ __stack.args["name"] = name;
246
+ }
247
+ if ("limit" in __overrides) {
248
+ limit = __overrides["limit"];
249
+ __stack.args["limit"] = limit;
250
+ }
251
+ }
252
+ try {
253
+ await agencyStore.run({
254
+ ...getRuntimeContext(),
255
+ ctx: __ctx,
256
+ stack: __setupData.stateStack,
257
+ threads: __setupData.threads
258
+ }, async () => {
259
+ await runner.hook(0, async () => {
260
+ await callHook({
261
+ name: "onFunctionStart",
262
+ data: {
263
+ functionName: "buildSearchPath",
264
+ args: {
265
+ name,
266
+ limit
267
+ },
268
+ moduleId: "stdlib/data/wikidata.agency"
269
+ }
270
+ });
271
+ });
272
+ await runner.step(1, async (runner2) => {
273
+ __stack.locals.q = await __call(encodeURIComponent, {
274
+ type: "positional",
275
+ args: [__stack.args.name]
276
+ });
277
+ if (hasInterrupts(__stack.locals.q)) {
278
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
279
+ runner2.halt(__stack.locals.q);
280
+ return;
281
+ }
282
+ });
283
+ await runner.step(2, async (runner2) => {
284
+ __functionCompleted = true;
285
+ runner2.halt(`/w/api.php?action=wbsearchentities&search=${__stack.locals.q}&language=en&format=json&limit=${__stack.args.limit}`);
286
+ return;
287
+ });
288
+ });
289
+ if (runner.halted) {
290
+ if (isFailure(runner.haltResult)) {
291
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
292
+ }
293
+ return runner.haltResult;
294
+ }
295
+ } catch (__error) {
296
+ if (__error instanceof RestoreSignal) {
297
+ throw __error;
298
+ }
299
+ if (__error instanceof AgencyAbort) {
300
+ throw __error;
301
+ }
302
+ {
303
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
304
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
305
+ const __log = __createLogger(__ctx.logLevel);
306
+ __log.error("Function buildSearchPath threw an exception (converted to Failure): " + __errMsg);
307
+ if (__errStack) __log.error(__errStack);
308
+ __ctx.statelogClient?.error?.({
309
+ errorType: "runtimeError",
310
+ message: __errMsg,
311
+ functionName: "buildSearchPath",
312
+ retryable: __self.__retryable
313
+ });
314
+ }
315
+ return failure(
316
+ __error instanceof Error ? __error.message : String(__error),
317
+ {
318
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
319
+ retryable: __self.__retryable,
320
+ functionName: "buildSearchPath",
321
+ args: __stack.args
322
+ }
323
+ );
324
+ } finally {
325
+ __stateStack()?.pop();
326
+ if (__functionCompleted) {
327
+ await callHook({
328
+ name: "onFunctionEnd",
329
+ data: {
330
+ functionName: "buildSearchPath",
331
+ timeTaken: performance.now() - __funcStartTime
332
+ }
333
+ });
334
+ }
335
+ }
336
+ }
337
+ const buildSearchPath = __AgencyFunction.create({
338
+ name: "buildSearchPath",
339
+ module: "stdlib/data/wikidata.agency",
340
+ fn: __buildSearchPath_impl,
341
+ params: [{
342
+ name: "name",
343
+ hasDefault: false,
344
+ defaultValue: void 0,
345
+ variadic: false,
346
+ isFunctionTyped: false
347
+ }, {
348
+ name: "limit",
349
+ hasDefault: false,
350
+ defaultValue: void 0,
351
+ variadic: false,
352
+ isFunctionTyped: false
353
+ }],
354
+ toolDefinition: {
355
+ name: "buildSearchPath",
356
+ description: "No description provided.",
357
+ schema: z.object({ "name": z.string(), "limit": z.number() })
358
+ },
359
+ safe: true,
360
+ exported: false
361
+ }, __toolRegistry);
362
+ async function __buildEntityPath_impl(qid) {
363
+ const __setupData = setupFunction();
364
+ const __stack = __setupData.stack;
365
+ const __step = __setupData.step;
366
+ const __self = __setupData.self;
367
+ const __ctx = getRuntimeContext().ctx;
368
+ let __forked;
369
+ let __functionCompleted = false;
370
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
371
+ await __initializeGlobals(__ctx);
372
+ }
373
+ let __funcStartTime = performance.now();
374
+ __stack.args["qid"] = qid;
375
+ __self.__retryable = __self.__retryable ?? true;
376
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "buildEntityPath", threads: __setupData.threads });
377
+ let __resultCheckpointId = -1;
378
+ if (__ctx._pendingArgOverrides) {
379
+ const __overrides = __ctx._pendingArgOverrides;
380
+ __ctx._pendingArgOverrides = void 0;
381
+ if ("qid" in __overrides) {
382
+ qid = __overrides["qid"];
383
+ __stack.args["qid"] = qid;
384
+ }
385
+ }
386
+ try {
387
+ await agencyStore.run({
388
+ ...getRuntimeContext(),
389
+ ctx: __ctx,
390
+ stack: __setupData.stateStack,
391
+ threads: __setupData.threads
392
+ }, async () => {
393
+ await runner.hook(0, async () => {
394
+ await callHook({
395
+ name: "onFunctionStart",
396
+ data: {
397
+ functionName: "buildEntityPath",
398
+ args: {
399
+ qid
400
+ },
401
+ moduleId: "stdlib/data/wikidata.agency"
402
+ }
403
+ });
404
+ });
405
+ await runner.step(1, async (runner2) => {
406
+ __functionCompleted = true;
407
+ runner2.halt(`/wiki/Special:EntityData/${await __call(encodeURIComponent, {
408
+ type: "positional",
409
+ args: [__stack.args.qid]
410
+ })}.json`);
411
+ return;
412
+ });
413
+ });
414
+ if (runner.halted) {
415
+ if (isFailure(runner.haltResult)) {
416
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
417
+ }
418
+ return runner.haltResult;
419
+ }
420
+ } catch (__error) {
421
+ if (__error instanceof RestoreSignal) {
422
+ throw __error;
423
+ }
424
+ if (__error instanceof AgencyAbort) {
425
+ throw __error;
426
+ }
427
+ {
428
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
429
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
430
+ const __log = __createLogger(__ctx.logLevel);
431
+ __log.error("Function buildEntityPath threw an exception (converted to Failure): " + __errMsg);
432
+ if (__errStack) __log.error(__errStack);
433
+ __ctx.statelogClient?.error?.({
434
+ errorType: "runtimeError",
435
+ message: __errMsg,
436
+ functionName: "buildEntityPath",
437
+ retryable: __self.__retryable
438
+ });
439
+ }
440
+ return failure(
441
+ __error instanceof Error ? __error.message : String(__error),
442
+ {
443
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
444
+ retryable: __self.__retryable,
445
+ functionName: "buildEntityPath",
446
+ args: __stack.args
447
+ }
448
+ );
449
+ } finally {
450
+ __stateStack()?.pop();
451
+ if (__functionCompleted) {
452
+ await callHook({
453
+ name: "onFunctionEnd",
454
+ data: {
455
+ functionName: "buildEntityPath",
456
+ timeTaken: performance.now() - __funcStartTime
457
+ }
458
+ });
459
+ }
460
+ }
461
+ }
462
+ const buildEntityPath = __AgencyFunction.create({
463
+ name: "buildEntityPath",
464
+ module: "stdlib/data/wikidata.agency",
465
+ fn: __buildEntityPath_impl,
466
+ params: [{
467
+ name: "qid",
468
+ hasDefault: false,
469
+ defaultValue: void 0,
470
+ variadic: false,
471
+ isFunctionTyped: false
472
+ }],
473
+ toolDefinition: {
474
+ name: "buildEntityPath",
475
+ description: "No description provided.",
476
+ schema: z.object({ "qid": z.string() })
477
+ },
478
+ safe: true,
479
+ exported: false
480
+ }, __toolRegistry);
481
+ async function __buildQueryPath_impl(sparql) {
482
+ const __setupData = setupFunction();
483
+ const __stack = __setupData.stack;
484
+ const __step = __setupData.step;
485
+ const __self = __setupData.self;
486
+ const __ctx = getRuntimeContext().ctx;
487
+ let __forked;
488
+ let __functionCompleted = false;
489
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
490
+ await __initializeGlobals(__ctx);
491
+ }
492
+ let __funcStartTime = performance.now();
493
+ __stack.args["sparql"] = sparql;
494
+ __self.__retryable = __self.__retryable ?? true;
495
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "buildQueryPath", threads: __setupData.threads });
496
+ let __resultCheckpointId = -1;
497
+ if (__ctx._pendingArgOverrides) {
498
+ const __overrides = __ctx._pendingArgOverrides;
499
+ __ctx._pendingArgOverrides = void 0;
500
+ if ("sparql" in __overrides) {
501
+ sparql = __overrides["sparql"];
502
+ __stack.args["sparql"] = sparql;
503
+ }
504
+ }
505
+ try {
506
+ await agencyStore.run({
507
+ ...getRuntimeContext(),
508
+ ctx: __ctx,
509
+ stack: __setupData.stateStack,
510
+ threads: __setupData.threads
511
+ }, async () => {
512
+ await runner.hook(0, async () => {
513
+ await callHook({
514
+ name: "onFunctionStart",
515
+ data: {
516
+ functionName: "buildQueryPath",
517
+ args: {
518
+ sparql
519
+ },
520
+ moduleId: "stdlib/data/wikidata.agency"
521
+ }
522
+ });
523
+ });
524
+ await runner.step(1, async (runner2) => {
525
+ __stack.locals.q = await __call(encodeURIComponent, {
526
+ type: "positional",
527
+ args: [__stack.args.sparql]
528
+ });
529
+ if (hasInterrupts(__stack.locals.q)) {
530
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
531
+ runner2.halt(__stack.locals.q);
532
+ return;
533
+ }
534
+ });
535
+ await runner.step(2, async (runner2) => {
536
+ __functionCompleted = true;
537
+ runner2.halt(`/sparql?query=${__stack.locals.q}&format=json`);
538
+ return;
539
+ });
540
+ });
541
+ if (runner.halted) {
542
+ if (isFailure(runner.haltResult)) {
543
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
544
+ }
545
+ return runner.haltResult;
546
+ }
547
+ } catch (__error) {
548
+ if (__error instanceof RestoreSignal) {
549
+ throw __error;
550
+ }
551
+ if (__error instanceof AgencyAbort) {
552
+ throw __error;
553
+ }
554
+ {
555
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
556
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
557
+ const __log = __createLogger(__ctx.logLevel);
558
+ __log.error("Function buildQueryPath threw an exception (converted to Failure): " + __errMsg);
559
+ if (__errStack) __log.error(__errStack);
560
+ __ctx.statelogClient?.error?.({
561
+ errorType: "runtimeError",
562
+ message: __errMsg,
563
+ functionName: "buildQueryPath",
564
+ retryable: __self.__retryable
565
+ });
566
+ }
567
+ return failure(
568
+ __error instanceof Error ? __error.message : String(__error),
569
+ {
570
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
571
+ retryable: __self.__retryable,
572
+ functionName: "buildQueryPath",
573
+ args: __stack.args
574
+ }
575
+ );
576
+ } finally {
577
+ __stateStack()?.pop();
578
+ if (__functionCompleted) {
579
+ await callHook({
580
+ name: "onFunctionEnd",
581
+ data: {
582
+ functionName: "buildQueryPath",
583
+ timeTaken: performance.now() - __funcStartTime
584
+ }
585
+ });
586
+ }
587
+ }
588
+ }
589
+ const buildQueryPath = __AgencyFunction.create({
590
+ name: "buildQueryPath",
591
+ module: "stdlib/data/wikidata.agency",
592
+ fn: __buildQueryPath_impl,
593
+ params: [{
594
+ name: "sparql",
595
+ hasDefault: false,
596
+ defaultValue: void 0,
597
+ variadic: false,
598
+ isFunctionTyped: false
599
+ }],
600
+ toolDefinition: {
601
+ name: "buildQueryPath",
602
+ description: "No description provided.",
603
+ schema: z.object({ "sparql": z.string() })
604
+ },
605
+ safe: true,
606
+ exported: false
607
+ }, __toolRegistry);
608
+ async function __parseSearch_impl(raw) {
609
+ const __setupData = setupFunction();
610
+ const __stack = __setupData.stack;
611
+ const __step = __setupData.step;
612
+ const __self = __setupData.self;
613
+ const __ctx = getRuntimeContext().ctx;
614
+ let __forked;
615
+ let __functionCompleted = false;
616
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
617
+ await __initializeGlobals(__ctx);
618
+ }
619
+ let __funcStartTime = performance.now();
620
+ __stack.args["raw"] = raw;
621
+ __self.__retryable = __self.__retryable ?? true;
622
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseSearch", threads: __setupData.threads });
623
+ let __resultCheckpointId = -1;
624
+ if (__ctx._pendingArgOverrides) {
625
+ const __overrides = __ctx._pendingArgOverrides;
626
+ __ctx._pendingArgOverrides = void 0;
627
+ if ("raw" in __overrides) {
628
+ raw = __overrides["raw"];
629
+ __stack.args["raw"] = raw;
630
+ }
631
+ }
632
+ try {
633
+ await agencyStore.run({
634
+ ...getRuntimeContext(),
635
+ ctx: __ctx,
636
+ stack: __setupData.stateStack,
637
+ threads: __setupData.threads
638
+ }, async () => {
639
+ await runner.hook(0, async () => {
640
+ await callHook({
641
+ name: "onFunctionStart",
642
+ data: {
643
+ functionName: "parseSearch",
644
+ args: {
645
+ raw
646
+ },
647
+ moduleId: "stdlib/data/wikidata.agency"
648
+ }
649
+ });
650
+ });
651
+ await runner.step(1, async (runner2) => {
652
+ __stack.locals.r = __stack.args.raw ?? {};
653
+ });
654
+ await runner.step(2, async (runner2) => {
655
+ __stack.locals.results = __stack.locals.r.search ?? [];
656
+ });
657
+ await runner.step(3, async (runner2) => {
658
+ __functionCompleted = true;
659
+ runner2.halt(await __call(map, {
660
+ type: "named",
661
+ positionalArgs: [__stack.locals.results],
662
+ namedArgs: {},
663
+ blockArg: __AgencyFunction.create({ name: "__block_0", module: "stdlib/data/wikidata.agency", fn: async (s) => {
664
+ const __bsetup = setupFunction();
665
+ const __bstack = __bsetup.stack;
666
+ const __self2 = __bstack.locals;
667
+ const __bframe___block_0 = __bstack;
668
+ __bstack.args["s"] = s;
669
+ const runner3 = new Runner(__ctx, __bstack, { state: __bstack, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_0" });
670
+ try {
671
+ await runner3.step(0, async (runner4) => {
672
+ __bstack.locals.e = __bstack.args.s ?? {};
673
+ });
674
+ await runner3.step(1, async (runner4) => {
675
+ runner4.halt({
676
+ "id": __bstack.locals.e.id ?? ``,
677
+ "label": __bstack.locals.e.label ?? ``,
678
+ "description": __bstack.locals.e.description ?? ``,
679
+ "url": __bstack.locals.e.concepturi ?? ``
680
+ });
681
+ return;
682
+ });
683
+ return runner3.halted ? runner3.haltResult : void 0;
684
+ } finally {
685
+ __bsetup.stateStack.pop();
686
+ }
687
+ }, params: [{ name: "s", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
688
+ }));
689
+ return;
690
+ });
691
+ });
692
+ if (runner.halted) {
693
+ if (isFailure(runner.haltResult)) {
694
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
695
+ }
696
+ return runner.haltResult;
697
+ }
698
+ } catch (__error) {
699
+ if (__error instanceof RestoreSignal) {
700
+ throw __error;
701
+ }
702
+ if (__error instanceof AgencyAbort) {
703
+ throw __error;
704
+ }
705
+ {
706
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
707
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
708
+ const __log = __createLogger(__ctx.logLevel);
709
+ __log.error("Function parseSearch threw an exception (converted to Failure): " + __errMsg);
710
+ if (__errStack) __log.error(__errStack);
711
+ __ctx.statelogClient?.error?.({
712
+ errorType: "runtimeError",
713
+ message: __errMsg,
714
+ functionName: "parseSearch",
715
+ retryable: __self.__retryable
716
+ });
717
+ }
718
+ return failure(
719
+ __error instanceof Error ? __error.message : String(__error),
720
+ {
721
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
722
+ retryable: __self.__retryable,
723
+ functionName: "parseSearch",
724
+ args: __stack.args
725
+ }
726
+ );
727
+ } finally {
728
+ __stateStack()?.pop();
729
+ if (__functionCompleted) {
730
+ await callHook({
731
+ name: "onFunctionEnd",
732
+ data: {
733
+ functionName: "parseSearch",
734
+ timeTaken: performance.now() - __funcStartTime
735
+ }
736
+ });
737
+ }
738
+ }
739
+ }
740
+ const parseSearch = __AgencyFunction.create({
741
+ name: "parseSearch",
742
+ module: "stdlib/data/wikidata.agency",
743
+ fn: __parseSearch_impl,
744
+ params: [{
745
+ name: "raw",
746
+ hasDefault: false,
747
+ defaultValue: void 0,
748
+ variadic: false,
749
+ isFunctionTyped: false
750
+ }],
751
+ toolDefinition: {
752
+ name: "parseSearch",
753
+ description: "No description provided.",
754
+ schema: z.object({ "raw": z.any() })
755
+ },
756
+ safe: true,
757
+ exported: false
758
+ }, __toolRegistry);
759
+ async function __parseClaimValue_impl(mainsnak) {
760
+ const __setupData = setupFunction();
761
+ const __stack = __setupData.stack;
762
+ const __step = __setupData.step;
763
+ const __self = __setupData.self;
764
+ const __ctx = getRuntimeContext().ctx;
765
+ let __forked;
766
+ let __functionCompleted = false;
767
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
768
+ await __initializeGlobals(__ctx);
769
+ }
770
+ let __funcStartTime = performance.now();
771
+ __stack.args["mainsnak"] = mainsnak;
772
+ __self.__retryable = __self.__retryable ?? true;
773
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseClaimValue", threads: __setupData.threads });
774
+ let __resultCheckpointId = -1;
775
+ if (__ctx._pendingArgOverrides) {
776
+ const __overrides = __ctx._pendingArgOverrides;
777
+ __ctx._pendingArgOverrides = void 0;
778
+ if ("mainsnak" in __overrides) {
779
+ mainsnak = __overrides["mainsnak"];
780
+ __stack.args["mainsnak"] = mainsnak;
781
+ }
782
+ }
783
+ try {
784
+ await agencyStore.run({
785
+ ...getRuntimeContext(),
786
+ ctx: __ctx,
787
+ stack: __setupData.stateStack,
788
+ threads: __setupData.threads
789
+ }, async () => {
790
+ await runner.hook(0, async () => {
791
+ await callHook({
792
+ name: "onFunctionStart",
793
+ data: {
794
+ functionName: "parseClaimValue",
795
+ args: {
796
+ mainsnak
797
+ },
798
+ moduleId: "stdlib/data/wikidata.agency"
799
+ }
800
+ });
801
+ });
802
+ await runner.step(1, async (runner2) => {
803
+ __stack.locals.snak = __stack.args.mainsnak ?? {};
804
+ });
805
+ await runner.ifElse(2, [
806
+ {
807
+ condition: async () => !__eq(__stack.locals.snak.snaktype ?? ``, `value`),
808
+ body: async (runner2) => {
809
+ await runner2.step(0, async (runner3) => {
810
+ __functionCompleted = true;
811
+ runner3.halt(``);
812
+ return;
813
+ });
814
+ }
815
+ }
816
+ ]);
817
+ await runner.step(3, async (runner2) => {
818
+ __stack.locals.dv = __stack.locals.snak.datavalue ?? {};
819
+ });
820
+ await runner.step(4, async (runner2) => {
821
+ __stack.locals.t = __stack.locals.dv.type ?? ``;
822
+ });
823
+ await runner.step(5, async (runner2) => {
824
+ __stack.locals.v = __stack.locals.dv.value ?? null;
825
+ });
826
+ await runner.ifElse(6, [
827
+ {
828
+ condition: async () => __eq(__stack.locals.t, `wikibase-entityid`),
829
+ body: async (runner2) => {
830
+ await runner2.step(0, async (runner3) => {
831
+ __functionCompleted = true;
832
+ runner3.halt(__stack.locals.v.id ?? ``);
833
+ return;
834
+ });
835
+ }
836
+ }
837
+ ]);
838
+ await runner.ifElse(7, [
839
+ {
840
+ condition: async () => __eq(__stack.locals.t, `string`),
841
+ body: async (runner2) => {
842
+ await runner2.step(0, async (runner3) => {
843
+ __functionCompleted = true;
844
+ runner3.halt(__stack.locals.v ?? ``);
845
+ return;
846
+ });
847
+ }
848
+ }
849
+ ]);
850
+ await runner.ifElse(8, [
851
+ {
852
+ condition: async () => __eq(__stack.locals.t, `monolingualtext`),
853
+ body: async (runner2) => {
854
+ await runner2.step(0, async (runner3) => {
855
+ __functionCompleted = true;
856
+ runner3.halt(__stack.locals.v.text ?? ``);
857
+ return;
858
+ });
859
+ }
860
+ }
861
+ ]);
862
+ await runner.ifElse(9, [
863
+ {
864
+ condition: async () => __eq(__stack.locals.t, `time`),
865
+ body: async (runner2) => {
866
+ await runner2.step(0, async (runner3) => {
867
+ __functionCompleted = true;
868
+ runner3.halt(__stack.locals.v.time ?? ``);
869
+ return;
870
+ });
871
+ }
872
+ }
873
+ ]);
874
+ await runner.ifElse(10, [
875
+ {
876
+ condition: async () => __eq(__stack.locals.t, `quantity`),
877
+ body: async (runner2) => {
878
+ await runner2.step(0, async (runner3) => {
879
+ __functionCompleted = true;
880
+ runner3.halt(__stack.locals.v.amount ?? ``);
881
+ return;
882
+ });
883
+ }
884
+ }
885
+ ]);
886
+ await runner.ifElse(11, [
887
+ {
888
+ condition: async () => __eq(__stack.locals.t, `globecoordinate`),
889
+ body: async (runner2) => {
890
+ await runner2.step(0, async (runner3) => {
891
+ __functionCompleted = true;
892
+ runner3.halt(`${__stack.locals.v.latitude ?? 0},${__stack.locals.v.longitude ?? 0}`);
893
+ return;
894
+ });
895
+ }
896
+ }
897
+ ]);
898
+ await runner.step(12, async (runner2) => {
899
+ __functionCompleted = true;
900
+ runner2.halt(``);
901
+ return;
902
+ });
903
+ });
904
+ if (runner.halted) {
905
+ if (isFailure(runner.haltResult)) {
906
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
907
+ }
908
+ return runner.haltResult;
909
+ }
910
+ } catch (__error) {
911
+ if (__error instanceof RestoreSignal) {
912
+ throw __error;
913
+ }
914
+ if (__error instanceof AgencyAbort) {
915
+ throw __error;
916
+ }
917
+ {
918
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
919
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
920
+ const __log = __createLogger(__ctx.logLevel);
921
+ __log.error("Function parseClaimValue threw an exception (converted to Failure): " + __errMsg);
922
+ if (__errStack) __log.error(__errStack);
923
+ __ctx.statelogClient?.error?.({
924
+ errorType: "runtimeError",
925
+ message: __errMsg,
926
+ functionName: "parseClaimValue",
927
+ retryable: __self.__retryable
928
+ });
929
+ }
930
+ return failure(
931
+ __error instanceof Error ? __error.message : String(__error),
932
+ {
933
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
934
+ retryable: __self.__retryable,
935
+ functionName: "parseClaimValue",
936
+ args: __stack.args
937
+ }
938
+ );
939
+ } finally {
940
+ __stateStack()?.pop();
941
+ if (__functionCompleted) {
942
+ await callHook({
943
+ name: "onFunctionEnd",
944
+ data: {
945
+ functionName: "parseClaimValue",
946
+ timeTaken: performance.now() - __funcStartTime
947
+ }
948
+ });
949
+ }
950
+ }
951
+ }
952
+ const parseClaimValue = __AgencyFunction.create({
953
+ name: "parseClaimValue",
954
+ module: "stdlib/data/wikidata.agency",
955
+ fn: __parseClaimValue_impl,
956
+ params: [{
957
+ name: "mainsnak",
958
+ hasDefault: false,
959
+ defaultValue: void 0,
960
+ variadic: false,
961
+ isFunctionTyped: false
962
+ }],
963
+ toolDefinition: {
964
+ name: "parseClaimValue",
965
+ description: "No description provided.",
966
+ schema: z.object({ "mainsnak": z.any() })
967
+ },
968
+ safe: true,
969
+ exported: false
970
+ }, __toolRegistry);
971
+ async function __parseClaimList_impl(statements) {
972
+ const __setupData = setupFunction();
973
+ const __stack = __setupData.stack;
974
+ const __step = __setupData.step;
975
+ const __self = __setupData.self;
976
+ const __ctx = getRuntimeContext().ctx;
977
+ let __forked;
978
+ let __functionCompleted = false;
979
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
980
+ await __initializeGlobals(__ctx);
981
+ }
982
+ let __funcStartTime = performance.now();
983
+ __stack.args["statements"] = statements;
984
+ __self.__retryable = __self.__retryable ?? true;
985
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseClaimList", threads: __setupData.threads });
986
+ let __resultCheckpointId = -1;
987
+ if (__ctx._pendingArgOverrides) {
988
+ const __overrides = __ctx._pendingArgOverrides;
989
+ __ctx._pendingArgOverrides = void 0;
990
+ if ("statements" in __overrides) {
991
+ statements = __overrides["statements"];
992
+ __stack.args["statements"] = statements;
993
+ }
994
+ }
995
+ try {
996
+ await agencyStore.run({
997
+ ...getRuntimeContext(),
998
+ ctx: __ctx,
999
+ stack: __setupData.stateStack,
1000
+ threads: __setupData.threads
1001
+ }, async () => {
1002
+ await runner.hook(0, async () => {
1003
+ await callHook({
1004
+ name: "onFunctionStart",
1005
+ data: {
1006
+ functionName: "parseClaimList",
1007
+ args: {
1008
+ statements
1009
+ },
1010
+ moduleId: "stdlib/data/wikidata.agency"
1011
+ }
1012
+ });
1013
+ });
1014
+ await runner.step(1, async (runner2) => {
1015
+ __stack.locals.arr = __stack.args.statements ?? [];
1016
+ });
1017
+ await runner.step(2, async (runner2) => {
1018
+ __functionCompleted = true;
1019
+ runner2.halt(await __call(map, {
1020
+ type: "named",
1021
+ positionalArgs: [__stack.locals.arr],
1022
+ namedArgs: {},
1023
+ blockArg: __AgencyFunction.create({ name: "__block_1", module: "stdlib/data/wikidata.agency", fn: async (s) => {
1024
+ const __bsetup = setupFunction();
1025
+ const __bstack = __bsetup.stack;
1026
+ const __self2 = __bstack.locals;
1027
+ const __bframe___block_1 = __bstack;
1028
+ __bstack.args["s"] = s;
1029
+ const runner3 = new Runner(__ctx, __bstack, { state: __bstack, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_1" });
1030
+ try {
1031
+ await runner3.step(0, async (runner4) => {
1032
+ __bstack.locals.st = __bstack.args.s ?? {};
1033
+ });
1034
+ await runner3.step(1, async (runner4) => {
1035
+ runner4.halt(await __call(parseClaimValue, {
1036
+ type: "positional",
1037
+ args: [__bstack.locals.st.mainsnak ?? {}]
1038
+ }));
1039
+ return;
1040
+ });
1041
+ return runner3.halted ? runner3.haltResult : void 0;
1042
+ } finally {
1043
+ __bsetup.stateStack.pop();
1044
+ }
1045
+ }, params: [{ name: "s", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
1046
+ }));
1047
+ return;
1048
+ });
1049
+ });
1050
+ if (runner.halted) {
1051
+ if (isFailure(runner.haltResult)) {
1052
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1053
+ }
1054
+ return runner.haltResult;
1055
+ }
1056
+ } catch (__error) {
1057
+ if (__error instanceof RestoreSignal) {
1058
+ throw __error;
1059
+ }
1060
+ if (__error instanceof AgencyAbort) {
1061
+ throw __error;
1062
+ }
1063
+ {
1064
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1065
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1066
+ const __log = __createLogger(__ctx.logLevel);
1067
+ __log.error("Function parseClaimList threw an exception (converted to Failure): " + __errMsg);
1068
+ if (__errStack) __log.error(__errStack);
1069
+ __ctx.statelogClient?.error?.({
1070
+ errorType: "runtimeError",
1071
+ message: __errMsg,
1072
+ functionName: "parseClaimList",
1073
+ retryable: __self.__retryable
1074
+ });
1075
+ }
1076
+ return failure(
1077
+ __error instanceof Error ? __error.message : String(__error),
1078
+ {
1079
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1080
+ retryable: __self.__retryable,
1081
+ functionName: "parseClaimList",
1082
+ args: __stack.args
1083
+ }
1084
+ );
1085
+ } finally {
1086
+ __stateStack()?.pop();
1087
+ if (__functionCompleted) {
1088
+ await callHook({
1089
+ name: "onFunctionEnd",
1090
+ data: {
1091
+ functionName: "parseClaimList",
1092
+ timeTaken: performance.now() - __funcStartTime
1093
+ }
1094
+ });
1095
+ }
1096
+ }
1097
+ }
1098
+ const parseClaimList = __AgencyFunction.create({
1099
+ name: "parseClaimList",
1100
+ module: "stdlib/data/wikidata.agency",
1101
+ fn: __parseClaimList_impl,
1102
+ params: [{
1103
+ name: "statements",
1104
+ hasDefault: false,
1105
+ defaultValue: void 0,
1106
+ variadic: false,
1107
+ isFunctionTyped: false
1108
+ }],
1109
+ toolDefinition: {
1110
+ name: "parseClaimList",
1111
+ description: "No description provided.",
1112
+ schema: z.object({ "statements": z.any() })
1113
+ },
1114
+ safe: true,
1115
+ exported: false
1116
+ }, __toolRegistry);
1117
+ async function __parseClaims_impl(claims) {
1118
+ const __setupData = setupFunction();
1119
+ const __stack = __setupData.stack;
1120
+ const __step = __setupData.step;
1121
+ const __self = __setupData.self;
1122
+ const __ctx = getRuntimeContext().ctx;
1123
+ let __forked;
1124
+ let __functionCompleted = false;
1125
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1126
+ await __initializeGlobals(__ctx);
1127
+ }
1128
+ let __funcStartTime = performance.now();
1129
+ __stack.args["claims"] = claims;
1130
+ __self.__retryable = __self.__retryable ?? true;
1131
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseClaims", threads: __setupData.threads });
1132
+ let __resultCheckpointId = -1;
1133
+ if (__ctx._pendingArgOverrides) {
1134
+ const __overrides = __ctx._pendingArgOverrides;
1135
+ __ctx._pendingArgOverrides = void 0;
1136
+ if ("claims" in __overrides) {
1137
+ claims = __overrides["claims"];
1138
+ __stack.args["claims"] = claims;
1139
+ }
1140
+ }
1141
+ try {
1142
+ await agencyStore.run({
1143
+ ...getRuntimeContext(),
1144
+ ctx: __ctx,
1145
+ stack: __setupData.stateStack,
1146
+ threads: __setupData.threads
1147
+ }, async () => {
1148
+ await runner.hook(0, async () => {
1149
+ await callHook({
1150
+ name: "onFunctionStart",
1151
+ data: {
1152
+ functionName: "parseClaims",
1153
+ args: {
1154
+ claims
1155
+ },
1156
+ moduleId: "stdlib/data/wikidata.agency"
1157
+ }
1158
+ });
1159
+ });
1160
+ await runner.step(1, async (runner2) => {
1161
+ __stack.locals.c = __stack.args.claims ?? {};
1162
+ });
1163
+ await runner.step(2, async (runner2) => {
1164
+ __functionCompleted = true;
1165
+ runner2.halt(await __call(mapValues, {
1166
+ type: "named",
1167
+ positionalArgs: [__stack.locals.c],
1168
+ namedArgs: {},
1169
+ blockArg: __AgencyFunction.create({ name: "__block_2", module: "stdlib/data/wikidata.agency", fn: async (statements, pid) => {
1170
+ const __bsetup = setupFunction();
1171
+ const __bstack = __bsetup.stack;
1172
+ const __self2 = __bstack.locals;
1173
+ const __bframe___block_2 = __bstack;
1174
+ __bstack.args["statements"] = statements;
1175
+ __bstack.args["pid"] = pid;
1176
+ const runner3 = new Runner(__ctx, __bstack, { state: __bstack, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_2" });
1177
+ try {
1178
+ await runner3.step(0, async (runner4) => {
1179
+ runner4.halt(await __call(parseClaimList, {
1180
+ type: "positional",
1181
+ args: [__bstack.args.statements]
1182
+ }));
1183
+ return;
1184
+ });
1185
+ return runner3.halted ? runner3.haltResult : void 0;
1186
+ } finally {
1187
+ __bsetup.stateStack.pop();
1188
+ }
1189
+ }, params: [{ name: "statements", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "pid", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
1190
+ }));
1191
+ return;
1192
+ });
1193
+ });
1194
+ if (runner.halted) {
1195
+ if (isFailure(runner.haltResult)) {
1196
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1197
+ }
1198
+ return runner.haltResult;
1199
+ }
1200
+ } catch (__error) {
1201
+ if (__error instanceof RestoreSignal) {
1202
+ throw __error;
1203
+ }
1204
+ if (__error instanceof AgencyAbort) {
1205
+ throw __error;
1206
+ }
1207
+ {
1208
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1209
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1210
+ const __log = __createLogger(__ctx.logLevel);
1211
+ __log.error("Function parseClaims threw an exception (converted to Failure): " + __errMsg);
1212
+ if (__errStack) __log.error(__errStack);
1213
+ __ctx.statelogClient?.error?.({
1214
+ errorType: "runtimeError",
1215
+ message: __errMsg,
1216
+ functionName: "parseClaims",
1217
+ retryable: __self.__retryable
1218
+ });
1219
+ }
1220
+ return failure(
1221
+ __error instanceof Error ? __error.message : String(__error),
1222
+ {
1223
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1224
+ retryable: __self.__retryable,
1225
+ functionName: "parseClaims",
1226
+ args: __stack.args
1227
+ }
1228
+ );
1229
+ } finally {
1230
+ __stateStack()?.pop();
1231
+ if (__functionCompleted) {
1232
+ await callHook({
1233
+ name: "onFunctionEnd",
1234
+ data: {
1235
+ functionName: "parseClaims",
1236
+ timeTaken: performance.now() - __funcStartTime
1237
+ }
1238
+ });
1239
+ }
1240
+ }
1241
+ }
1242
+ const parseClaims = __AgencyFunction.create({
1243
+ name: "parseClaims",
1244
+ module: "stdlib/data/wikidata.agency",
1245
+ fn: __parseClaims_impl,
1246
+ params: [{
1247
+ name: "claims",
1248
+ hasDefault: false,
1249
+ defaultValue: void 0,
1250
+ variadic: false,
1251
+ isFunctionTyped: false
1252
+ }],
1253
+ toolDefinition: {
1254
+ name: "parseClaims",
1255
+ description: "No description provided.",
1256
+ schema: z.object({ "claims": z.any() })
1257
+ },
1258
+ safe: true,
1259
+ exported: false
1260
+ }, __toolRegistry);
1261
+ async function __parseEntity_impl(raw, qid) {
1262
+ const __setupData = setupFunction();
1263
+ const __stack = __setupData.stack;
1264
+ const __step = __setupData.step;
1265
+ const __self = __setupData.self;
1266
+ const __ctx = getRuntimeContext().ctx;
1267
+ let __forked;
1268
+ let __functionCompleted = false;
1269
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1270
+ await __initializeGlobals(__ctx);
1271
+ }
1272
+ let __funcStartTime = performance.now();
1273
+ __stack.args["raw"] = raw;
1274
+ __stack.args["qid"] = qid;
1275
+ __self.__retryable = __self.__retryable ?? true;
1276
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseEntity", threads: __setupData.threads });
1277
+ let __resultCheckpointId = -1;
1278
+ if (__ctx._pendingArgOverrides) {
1279
+ const __overrides = __ctx._pendingArgOverrides;
1280
+ __ctx._pendingArgOverrides = void 0;
1281
+ if ("raw" in __overrides) {
1282
+ raw = __overrides["raw"];
1283
+ __stack.args["raw"] = raw;
1284
+ }
1285
+ if ("qid" in __overrides) {
1286
+ qid = __overrides["qid"];
1287
+ __stack.args["qid"] = qid;
1288
+ }
1289
+ }
1290
+ try {
1291
+ await agencyStore.run({
1292
+ ...getRuntimeContext(),
1293
+ ctx: __ctx,
1294
+ stack: __setupData.stateStack,
1295
+ threads: __setupData.threads
1296
+ }, async () => {
1297
+ await runner.hook(0, async () => {
1298
+ await callHook({
1299
+ name: "onFunctionStart",
1300
+ data: {
1301
+ functionName: "parseEntity",
1302
+ args: {
1303
+ raw,
1304
+ qid
1305
+ },
1306
+ moduleId: "stdlib/data/wikidata.agency"
1307
+ }
1308
+ });
1309
+ });
1310
+ await runner.step(1, async (runner2) => {
1311
+ __stack.locals.r = __stack.args.raw ?? {};
1312
+ });
1313
+ await runner.step(2, async (runner2) => {
1314
+ __stack.locals.ents = await __call(values, {
1315
+ type: "positional",
1316
+ args: [__stack.locals.r.entities ?? {}]
1317
+ });
1318
+ if (hasInterrupts(__stack.locals.ents)) {
1319
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
1320
+ runner2.halt(__stack.locals.ents);
1321
+ return;
1322
+ }
1323
+ });
1324
+ await runner.step(3, async (runner2) => {
1325
+ __stack.locals.ent = __stack.locals.ents[0] ?? {};
1326
+ });
1327
+ await runner.step(4, async (runner2) => {
1328
+ __stack.locals.labels = __stack.locals.ent.labels ?? {};
1329
+ });
1330
+ await runner.step(5, async (runner2) => {
1331
+ __stack.locals.descriptions = __stack.locals.ent.descriptions ?? {};
1332
+ });
1333
+ await runner.step(6, async (runner2) => {
1334
+ __stack.locals.aliasesObj = __stack.locals.ent.aliases ?? {};
1335
+ });
1336
+ await runner.step(7, async (runner2) => {
1337
+ __stack.locals.enLabel = __stack.locals.labels.en ?? {};
1338
+ });
1339
+ await runner.step(8, async (runner2) => {
1340
+ __stack.locals.enDesc = __stack.locals.descriptions.en ?? {};
1341
+ });
1342
+ await runner.step(9, async (runner2) => {
1343
+ __stack.locals.enAliases = __stack.locals.aliasesObj.en ?? [];
1344
+ });
1345
+ await runner.step(10, async (runner2) => {
1346
+ __functionCompleted = true;
1347
+ runner2.halt({
1348
+ "id": __stack.locals.ent.id ?? __stack.args.qid,
1349
+ "label": __stack.locals.enLabel.value ?? ``,
1350
+ "description": __stack.locals.enDesc.value ?? ``,
1351
+ "aliases": await __call(map, {
1352
+ type: "named",
1353
+ positionalArgs: [__stack.locals.enAliases],
1354
+ namedArgs: {},
1355
+ blockArg: __AgencyFunction.create({ name: "__block_3", module: "stdlib/data/wikidata.agency", fn: async (a) => {
1356
+ const __bsetup = setupFunction();
1357
+ const __bstack = __bsetup.stack;
1358
+ const __self2 = __bstack.locals;
1359
+ const __bframe___block_3 = __bstack;
1360
+ __bstack.args["a"] = a;
1361
+ const runner3 = new Runner(__ctx, __bstack, { state: __bstack, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_3" });
1362
+ try {
1363
+ await runner3.step(0, async (runner4) => {
1364
+ __bstack.locals.al = __bstack.args.a ?? {};
1365
+ });
1366
+ await runner3.step(1, async (runner4) => {
1367
+ runner4.halt(__bstack.locals.al.value ?? ``);
1368
+ return;
1369
+ });
1370
+ return runner3.halted ? runner3.haltResult : void 0;
1371
+ } finally {
1372
+ __bsetup.stateStack.pop();
1373
+ }
1374
+ }, params: [{ name: "a", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
1375
+ }),
1376
+ "claims": await __call(parseClaims, {
1377
+ type: "positional",
1378
+ args: [__stack.locals.ent.claims ?? {}]
1379
+ })
1380
+ });
1381
+ return;
1382
+ });
1383
+ });
1384
+ if (runner.halted) {
1385
+ if (isFailure(runner.haltResult)) {
1386
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1387
+ }
1388
+ return runner.haltResult;
1389
+ }
1390
+ } catch (__error) {
1391
+ if (__error instanceof RestoreSignal) {
1392
+ throw __error;
1393
+ }
1394
+ if (__error instanceof AgencyAbort) {
1395
+ throw __error;
1396
+ }
1397
+ {
1398
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1399
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1400
+ const __log = __createLogger(__ctx.logLevel);
1401
+ __log.error("Function parseEntity threw an exception (converted to Failure): " + __errMsg);
1402
+ if (__errStack) __log.error(__errStack);
1403
+ __ctx.statelogClient?.error?.({
1404
+ errorType: "runtimeError",
1405
+ message: __errMsg,
1406
+ functionName: "parseEntity",
1407
+ retryable: __self.__retryable
1408
+ });
1409
+ }
1410
+ return failure(
1411
+ __error instanceof Error ? __error.message : String(__error),
1412
+ {
1413
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1414
+ retryable: __self.__retryable,
1415
+ functionName: "parseEntity",
1416
+ args: __stack.args
1417
+ }
1418
+ );
1419
+ } finally {
1420
+ __stateStack()?.pop();
1421
+ if (__functionCompleted) {
1422
+ await callHook({
1423
+ name: "onFunctionEnd",
1424
+ data: {
1425
+ functionName: "parseEntity",
1426
+ timeTaken: performance.now() - __funcStartTime
1427
+ }
1428
+ });
1429
+ }
1430
+ }
1431
+ }
1432
+ const parseEntity = __AgencyFunction.create({
1433
+ name: "parseEntity",
1434
+ module: "stdlib/data/wikidata.agency",
1435
+ fn: __parseEntity_impl,
1436
+ params: [{
1437
+ name: "raw",
1438
+ hasDefault: false,
1439
+ defaultValue: void 0,
1440
+ variadic: false,
1441
+ isFunctionTyped: false
1442
+ }, {
1443
+ name: "qid",
1444
+ hasDefault: false,
1445
+ defaultValue: void 0,
1446
+ variadic: false,
1447
+ isFunctionTyped: false
1448
+ }],
1449
+ toolDefinition: {
1450
+ name: "parseEntity",
1451
+ description: "No description provided.",
1452
+ schema: z.object({ "raw": z.any(), "qid": z.string() })
1453
+ },
1454
+ safe: true,
1455
+ exported: false
1456
+ }, __toolRegistry);
1457
+ async function __parseBindings_impl(raw) {
1458
+ const __setupData = setupFunction();
1459
+ const __stack = __setupData.stack;
1460
+ const __step = __setupData.step;
1461
+ const __self = __setupData.self;
1462
+ const __ctx = getRuntimeContext().ctx;
1463
+ let __forked;
1464
+ let __functionCompleted = false;
1465
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1466
+ await __initializeGlobals(__ctx);
1467
+ }
1468
+ let __funcStartTime = performance.now();
1469
+ __stack.args["raw"] = raw;
1470
+ __self.__retryable = __self.__retryable ?? true;
1471
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "parseBindings", threads: __setupData.threads });
1472
+ let __resultCheckpointId = -1;
1473
+ if (__ctx._pendingArgOverrides) {
1474
+ const __overrides = __ctx._pendingArgOverrides;
1475
+ __ctx._pendingArgOverrides = void 0;
1476
+ if ("raw" in __overrides) {
1477
+ raw = __overrides["raw"];
1478
+ __stack.args["raw"] = raw;
1479
+ }
1480
+ }
1481
+ try {
1482
+ await agencyStore.run({
1483
+ ...getRuntimeContext(),
1484
+ ctx: __ctx,
1485
+ stack: __setupData.stateStack,
1486
+ threads: __setupData.threads
1487
+ }, async () => {
1488
+ await runner.hook(0, async () => {
1489
+ await callHook({
1490
+ name: "onFunctionStart",
1491
+ data: {
1492
+ functionName: "parseBindings",
1493
+ args: {
1494
+ raw
1495
+ },
1496
+ moduleId: "stdlib/data/wikidata.agency"
1497
+ }
1498
+ });
1499
+ });
1500
+ await runner.step(1, async (runner2) => {
1501
+ __stack.locals.r = __stack.args.raw ?? {};
1502
+ });
1503
+ await runner.step(2, async (runner2) => {
1504
+ __stack.locals.res = __stack.locals.r.results ?? {};
1505
+ });
1506
+ await runner.step(3, async (runner2) => {
1507
+ __stack.locals.bindings = __stack.locals.res.bindings ?? [];
1508
+ });
1509
+ await runner.step(4, async (runner2) => {
1510
+ __functionCompleted = true;
1511
+ runner2.halt(await __call(map, {
1512
+ type: "named",
1513
+ positionalArgs: [__stack.locals.bindings],
1514
+ namedArgs: {},
1515
+ blockArg: __AgencyFunction.create({ name: "__block_4", module: "stdlib/data/wikidata.agency", fn: async (b) => {
1516
+ const __bsetup = setupFunction();
1517
+ const __bstack = __bsetup.stack;
1518
+ const __self2 = __bstack.locals;
1519
+ const __bframe___block_4 = __bstack;
1520
+ __bstack.args["b"] = b;
1521
+ const runner3 = new Runner(__ctx, __bstack, { state: __bstack, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_4" });
1522
+ try {
1523
+ await runner3.step(0, async (runner4) => {
1524
+ __bstack.locals.binding = __bstack.args.b ?? {};
1525
+ });
1526
+ await runner3.step(1, async (runner4) => {
1527
+ runner4.halt(await __call(mapValues, {
1528
+ type: "named",
1529
+ positionalArgs: [__bstack.locals.binding],
1530
+ namedArgs: {},
1531
+ blockArg: __AgencyFunction.create({ name: "__block_5", module: "stdlib/data/wikidata.agency", fn: async (cell, varName) => {
1532
+ const __bsetup2 = setupFunction();
1533
+ const __bstack2 = __bsetup2.stack;
1534
+ const __self3 = __bstack2.locals;
1535
+ const __bframe___block_5 = __bstack2;
1536
+ __bstack2.args["cell"] = cell;
1537
+ __bstack2.args["varName"] = varName;
1538
+ const runner5 = new Runner(__ctx, __bstack2, { state: __bstack2, moduleId: "stdlib/data/wikidata.agency", scopeName: "__block_5" });
1539
+ try {
1540
+ await runner5.step(0, async (runner6) => {
1541
+ __bstack2.locals.c = __bstack2.args.cell ?? {};
1542
+ });
1543
+ await runner5.step(1, async (runner6) => {
1544
+ runner6.halt(__bstack2.locals.c.value ?? ``);
1545
+ return;
1546
+ });
1547
+ return runner5.halted ? runner5.haltResult : void 0;
1548
+ } finally {
1549
+ __bsetup2.stateStack.pop();
1550
+ }
1551
+ }, params: [{ name: "cell", hasDefault: false, defaultValue: void 0, variadic: false }, { name: "varName", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
1552
+ }));
1553
+ return;
1554
+ });
1555
+ return runner3.halted ? runner3.haltResult : void 0;
1556
+ } finally {
1557
+ __bsetup.stateStack.pop();
1558
+ }
1559
+ }, params: [{ name: "b", hasDefault: false, defaultValue: void 0, variadic: false }], toolDefinition: null }, __toolRegistry)
1560
+ }));
1561
+ return;
1562
+ });
1563
+ });
1564
+ if (runner.halted) {
1565
+ if (isFailure(runner.haltResult)) {
1566
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1567
+ }
1568
+ return runner.haltResult;
1569
+ }
1570
+ } catch (__error) {
1571
+ if (__error instanceof RestoreSignal) {
1572
+ throw __error;
1573
+ }
1574
+ if (__error instanceof AgencyAbort) {
1575
+ throw __error;
1576
+ }
1577
+ {
1578
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1579
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1580
+ const __log = __createLogger(__ctx.logLevel);
1581
+ __log.error("Function parseBindings threw an exception (converted to Failure): " + __errMsg);
1582
+ if (__errStack) __log.error(__errStack);
1583
+ __ctx.statelogClient?.error?.({
1584
+ errorType: "runtimeError",
1585
+ message: __errMsg,
1586
+ functionName: "parseBindings",
1587
+ retryable: __self.__retryable
1588
+ });
1589
+ }
1590
+ return failure(
1591
+ __error instanceof Error ? __error.message : String(__error),
1592
+ {
1593
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1594
+ retryable: __self.__retryable,
1595
+ functionName: "parseBindings",
1596
+ args: __stack.args
1597
+ }
1598
+ );
1599
+ } finally {
1600
+ __stateStack()?.pop();
1601
+ if (__functionCompleted) {
1602
+ await callHook({
1603
+ name: "onFunctionEnd",
1604
+ data: {
1605
+ functionName: "parseBindings",
1606
+ timeTaken: performance.now() - __funcStartTime
1607
+ }
1608
+ });
1609
+ }
1610
+ }
1611
+ }
1612
+ const parseBindings = __AgencyFunction.create({
1613
+ name: "parseBindings",
1614
+ module: "stdlib/data/wikidata.agency",
1615
+ fn: __parseBindings_impl,
1616
+ params: [{
1617
+ name: "raw",
1618
+ hasDefault: false,
1619
+ defaultValue: void 0,
1620
+ variadic: false,
1621
+ isFunctionTyped: false
1622
+ }],
1623
+ toolDefinition: {
1624
+ name: "parseBindings",
1625
+ description: "No description provided.",
1626
+ schema: z.object({ "raw": z.any() })
1627
+ },
1628
+ safe: true,
1629
+ exported: false
1630
+ }, __toolRegistry);
1631
+ async function __wikidataError_impl(err) {
1632
+ const __setupData = setupFunction();
1633
+ const __stack = __setupData.stack;
1634
+ const __step = __setupData.step;
1635
+ const __self = __setupData.self;
1636
+ const __ctx = getRuntimeContext().ctx;
1637
+ let __forked;
1638
+ let __functionCompleted = false;
1639
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1640
+ await __initializeGlobals(__ctx);
1641
+ }
1642
+ let __funcStartTime = performance.now();
1643
+ __stack.args["err"] = err;
1644
+ __self.__retryable = __self.__retryable ?? true;
1645
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataError", threads: __setupData.threads });
1646
+ let __resultCheckpointId = -1;
1647
+ if (__ctx._pendingArgOverrides) {
1648
+ const __overrides = __ctx._pendingArgOverrides;
1649
+ __ctx._pendingArgOverrides = void 0;
1650
+ if ("err" in __overrides) {
1651
+ err = __overrides["err"];
1652
+ __stack.args["err"] = err;
1653
+ }
1654
+ }
1655
+ try {
1656
+ await agencyStore.run({
1657
+ ...getRuntimeContext(),
1658
+ ctx: __ctx,
1659
+ stack: __setupData.stateStack,
1660
+ threads: __setupData.threads
1661
+ }, async () => {
1662
+ await runner.hook(0, async () => {
1663
+ await callHook({
1664
+ name: "onFunctionStart",
1665
+ data: {
1666
+ functionName: "wikidataError",
1667
+ args: {
1668
+ err
1669
+ },
1670
+ moduleId: "stdlib/data/wikidata.agency"
1671
+ }
1672
+ });
1673
+ });
1674
+ await runner.step(1, async (runner2) => {
1675
+ __functionCompleted = true;
1676
+ runner2.halt(`Wikidata request failed (the API is rate-limited and expects a descriptive User-Agent): ${__stack.args.err.message ?? __stack.args.err}`);
1677
+ return;
1678
+ });
1679
+ });
1680
+ if (runner.halted) {
1681
+ if (isFailure(runner.haltResult)) {
1682
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1683
+ }
1684
+ return runner.haltResult;
1685
+ }
1686
+ } catch (__error) {
1687
+ if (__error instanceof RestoreSignal) {
1688
+ throw __error;
1689
+ }
1690
+ if (__error instanceof AgencyAbort) {
1691
+ throw __error;
1692
+ }
1693
+ {
1694
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1695
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1696
+ const __log = __createLogger(__ctx.logLevel);
1697
+ __log.error("Function wikidataError threw an exception (converted to Failure): " + __errMsg);
1698
+ if (__errStack) __log.error(__errStack);
1699
+ __ctx.statelogClient?.error?.({
1700
+ errorType: "runtimeError",
1701
+ message: __errMsg,
1702
+ functionName: "wikidataError",
1703
+ retryable: __self.__retryable
1704
+ });
1705
+ }
1706
+ return failure(
1707
+ __error instanceof Error ? __error.message : String(__error),
1708
+ {
1709
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1710
+ retryable: __self.__retryable,
1711
+ functionName: "wikidataError",
1712
+ args: __stack.args
1713
+ }
1714
+ );
1715
+ } finally {
1716
+ __stateStack()?.pop();
1717
+ if (__functionCompleted) {
1718
+ await callHook({
1719
+ name: "onFunctionEnd",
1720
+ data: {
1721
+ functionName: "wikidataError",
1722
+ timeTaken: performance.now() - __funcStartTime
1723
+ }
1724
+ });
1725
+ }
1726
+ }
1727
+ }
1728
+ const wikidataError = __AgencyFunction.create({
1729
+ name: "wikidataError",
1730
+ module: "stdlib/data/wikidata.agency",
1731
+ fn: __wikidataError_impl,
1732
+ params: [{
1733
+ name: "err",
1734
+ hasDefault: false,
1735
+ defaultValue: void 0,
1736
+ variadic: false,
1737
+ isFunctionTyped: false
1738
+ }],
1739
+ toolDefinition: {
1740
+ name: "wikidataError",
1741
+ description: "No description provided.",
1742
+ schema: z.object({ "err": z.any() })
1743
+ },
1744
+ safe: true,
1745
+ exported: false
1746
+ }, __toolRegistry);
1747
+ async function __searchFinalize_impl(fetchResult) {
1748
+ const __setupData = setupFunction();
1749
+ const __stack = __setupData.stack;
1750
+ const __step = __setupData.step;
1751
+ const __self = __setupData.self;
1752
+ const __ctx = getRuntimeContext().ctx;
1753
+ let __forked;
1754
+ let __functionCompleted = false;
1755
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1756
+ await __initializeGlobals(__ctx);
1757
+ }
1758
+ let __funcStartTime = performance.now();
1759
+ __stack.args["fetchResult"] = fetchResult;
1760
+ __self.__retryable = __self.__retryable ?? true;
1761
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "searchFinalize", threads: __setupData.threads });
1762
+ let __resultCheckpointId = -1;
1763
+ if (__ctx._pendingArgOverrides) {
1764
+ const __overrides = __ctx._pendingArgOverrides;
1765
+ __ctx._pendingArgOverrides = void 0;
1766
+ if ("fetchResult" in __overrides) {
1767
+ fetchResult = __overrides["fetchResult"];
1768
+ __stack.args["fetchResult"] = fetchResult;
1769
+ }
1770
+ }
1771
+ try {
1772
+ await agencyStore.run({
1773
+ ...getRuntimeContext(),
1774
+ ctx: __ctx,
1775
+ stack: __setupData.stateStack,
1776
+ threads: __setupData.threads
1777
+ }, async () => {
1778
+ await runner.hook(0, async () => {
1779
+ await callHook({
1780
+ name: "onFunctionStart",
1781
+ data: {
1782
+ functionName: "searchFinalize",
1783
+ args: {
1784
+ fetchResult
1785
+ },
1786
+ moduleId: "stdlib/data/wikidata.agency"
1787
+ }
1788
+ });
1789
+ });
1790
+ await runner.step(1, async (runner2) => {
1791
+ __stack.locals.__scrutinee_4 = __stack.args.fetchResult;
1792
+ });
1793
+ await runner.ifElse(2, [
1794
+ {
1795
+ condition: async () => await isSuccess(__stack.locals.__scrutinee_4),
1796
+ body: async (runner2) => {
1797
+ await runner2.step(0, async (runner3) => {
1798
+ __stack.locals.body = __stack.locals.__scrutinee_4.value;
1799
+ });
1800
+ await runner2.step(1, async (runner3) => {
1801
+ __stack.locals.__armval_2 = await success(await __call(parseSearch, {
1802
+ type: "positional",
1803
+ args: [__stack.locals.body]
1804
+ }));
1805
+ if (hasInterrupts(__stack.locals.__armval_2)) {
1806
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
1807
+ runner3.halt(__stack.locals.__armval_2);
1808
+ return;
1809
+ }
1810
+ });
1811
+ await runner2.step(2, async (runner3) => {
1812
+ runner3.exitMatch(1, __stack.locals.__armval_2);
1813
+ return;
1814
+ });
1815
+ }
1816
+ },
1817
+ {
1818
+ condition: async () => await isFailure(__stack.locals.__scrutinee_4),
1819
+ body: async (runner2) => {
1820
+ await runner2.step(3, async (runner3) => {
1821
+ __stack.locals.err = __stack.locals.__scrutinee_4.error;
1822
+ });
1823
+ await runner2.step(4, async (runner3) => {
1824
+ __stack.locals.__armval_3 = failure(await __call(wikidataError, {
1825
+ type: "positional",
1826
+ args: [__stack.locals.err]
1827
+ }), { checkpoint: getRuntimeContext().ctx.getResultCheckpoint(), functionName: "searchFinalize", args: __stack.args });
1828
+ if (hasInterrupts(__stack.locals.__armval_3)) {
1829
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
1830
+ runner3.halt(__stack.locals.__armval_3);
1831
+ return;
1832
+ }
1833
+ });
1834
+ await runner2.step(5, async (runner3) => {
1835
+ runner3.exitMatch(1, __stack.locals.__armval_3);
1836
+ return;
1837
+ });
1838
+ }
1839
+ }
1840
+ ], void 0, { matchId: 1 });
1841
+ await runner.step(3, async (runner2) => {
1842
+ __functionCompleted = true;
1843
+ runner2.halt(__stack.locals.__matchval_1);
1844
+ return;
1845
+ });
1846
+ });
1847
+ if (runner.halted) {
1848
+ if (isFailure(runner.haltResult)) {
1849
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
1850
+ }
1851
+ return runner.haltResult;
1852
+ }
1853
+ } catch (__error) {
1854
+ if (__error instanceof RestoreSignal) {
1855
+ throw __error;
1856
+ }
1857
+ if (__error instanceof AgencyAbort) {
1858
+ throw __error;
1859
+ }
1860
+ {
1861
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
1862
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
1863
+ const __log = __createLogger(__ctx.logLevel);
1864
+ __log.error("Function searchFinalize threw an exception (converted to Failure): " + __errMsg);
1865
+ if (__errStack) __log.error(__errStack);
1866
+ __ctx.statelogClient?.error?.({
1867
+ errorType: "runtimeError",
1868
+ message: __errMsg,
1869
+ functionName: "searchFinalize",
1870
+ retryable: __self.__retryable
1871
+ });
1872
+ }
1873
+ return failure(
1874
+ __error instanceof Error ? __error.message : String(__error),
1875
+ {
1876
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
1877
+ retryable: __self.__retryable,
1878
+ functionName: "searchFinalize",
1879
+ args: __stack.args
1880
+ }
1881
+ );
1882
+ } finally {
1883
+ __stateStack()?.pop();
1884
+ if (__functionCompleted) {
1885
+ await callHook({
1886
+ name: "onFunctionEnd",
1887
+ data: {
1888
+ functionName: "searchFinalize",
1889
+ timeTaken: performance.now() - __funcStartTime
1890
+ }
1891
+ });
1892
+ }
1893
+ }
1894
+ }
1895
+ const searchFinalize = __AgencyFunction.create({
1896
+ name: "searchFinalize",
1897
+ module: "stdlib/data/wikidata.agency",
1898
+ fn: __searchFinalize_impl,
1899
+ params: [{
1900
+ name: "fetchResult",
1901
+ hasDefault: false,
1902
+ defaultValue: void 0,
1903
+ variadic: false,
1904
+ isFunctionTyped: false
1905
+ }],
1906
+ toolDefinition: {
1907
+ name: "searchFinalize",
1908
+ description: "No description provided.",
1909
+ schema: z.object({ "fetchResult": z.any() })
1910
+ },
1911
+ safe: true,
1912
+ exported: false
1913
+ }, __toolRegistry);
1914
+ async function __entityFinalize_impl(fetchResult, qid) {
1915
+ const __setupData = setupFunction();
1916
+ const __stack = __setupData.stack;
1917
+ const __step = __setupData.step;
1918
+ const __self = __setupData.self;
1919
+ const __ctx = getRuntimeContext().ctx;
1920
+ let __forked;
1921
+ let __functionCompleted = false;
1922
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
1923
+ await __initializeGlobals(__ctx);
1924
+ }
1925
+ let __funcStartTime = performance.now();
1926
+ __stack.args["fetchResult"] = fetchResult;
1927
+ __stack.args["qid"] = qid;
1928
+ __self.__retryable = __self.__retryable ?? true;
1929
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "entityFinalize", threads: __setupData.threads });
1930
+ let __resultCheckpointId = -1;
1931
+ if (__ctx._pendingArgOverrides) {
1932
+ const __overrides = __ctx._pendingArgOverrides;
1933
+ __ctx._pendingArgOverrides = void 0;
1934
+ if ("fetchResult" in __overrides) {
1935
+ fetchResult = __overrides["fetchResult"];
1936
+ __stack.args["fetchResult"] = fetchResult;
1937
+ }
1938
+ if ("qid" in __overrides) {
1939
+ qid = __overrides["qid"];
1940
+ __stack.args["qid"] = qid;
1941
+ }
1942
+ }
1943
+ try {
1944
+ await agencyStore.run({
1945
+ ...getRuntimeContext(),
1946
+ ctx: __ctx,
1947
+ stack: __setupData.stateStack,
1948
+ threads: __setupData.threads
1949
+ }, async () => {
1950
+ await runner.hook(0, async () => {
1951
+ await callHook({
1952
+ name: "onFunctionStart",
1953
+ data: {
1954
+ functionName: "entityFinalize",
1955
+ args: {
1956
+ fetchResult,
1957
+ qid
1958
+ },
1959
+ moduleId: "stdlib/data/wikidata.agency"
1960
+ }
1961
+ });
1962
+ });
1963
+ await runner.step(1, async (runner2) => {
1964
+ __stack.locals.__scrutinee_7 = __stack.args.fetchResult;
1965
+ });
1966
+ await runner.ifElse(2, [
1967
+ {
1968
+ condition: async () => await isSuccess(__stack.locals.__scrutinee_7),
1969
+ body: async (runner2) => {
1970
+ await runner2.step(0, async (runner3) => {
1971
+ __stack.locals.body = __stack.locals.__scrutinee_7.value;
1972
+ });
1973
+ await runner2.step(1, async (runner3) => {
1974
+ __stack.locals.b = __stack.locals.body ?? {};
1975
+ });
1976
+ await runner2.step(2, async (runner3) => {
1977
+ __stack.locals.ents = await __call(values, {
1978
+ type: "positional",
1979
+ args: [__stack.locals.b.entities ?? {}]
1980
+ });
1981
+ if (hasInterrupts(__stack.locals.ents)) {
1982
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
1983
+ runner3.halt(__stack.locals.ents);
1984
+ return;
1985
+ }
1986
+ });
1987
+ await runner2.ifElse(3, [
1988
+ {
1989
+ condition: async () => __eq(__stack.locals.ents.length, 0),
1990
+ body: async (runner3) => {
1991
+ await runner3.step(0, async (runner4) => {
1992
+ runner4.exitMatch(5, failure(`Wikidata returned no entity for ${__stack.args.qid}`, { checkpoint: getRuntimeContext().ctx.getResultCheckpoint(), functionName: "entityFinalize", args: __stack.args }));
1993
+ return;
1994
+ });
1995
+ }
1996
+ }
1997
+ ]);
1998
+ await runner2.step(4, async (runner3) => {
1999
+ runner3.exitMatch(5, await success(await __call(parseEntity, {
2000
+ type: "positional",
2001
+ args: [__stack.locals.b, __stack.args.qid]
2002
+ })));
2003
+ return;
2004
+ });
2005
+ }
2006
+ },
2007
+ {
2008
+ condition: async () => await isFailure(__stack.locals.__scrutinee_7),
2009
+ body: async (runner2) => {
2010
+ await runner2.step(5, async (runner3) => {
2011
+ __stack.locals.err = __stack.locals.__scrutinee_7.error;
2012
+ });
2013
+ await runner2.step(6, async (runner3) => {
2014
+ __stack.locals.__armval_6 = failure(await __call(wikidataError, {
2015
+ type: "positional",
2016
+ args: [__stack.locals.err]
2017
+ }), { checkpoint: getRuntimeContext().ctx.getResultCheckpoint(), functionName: "entityFinalize", args: __stack.args });
2018
+ if (hasInterrupts(__stack.locals.__armval_6)) {
2019
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2020
+ runner3.halt(__stack.locals.__armval_6);
2021
+ return;
2022
+ }
2023
+ });
2024
+ await runner2.step(7, async (runner3) => {
2025
+ runner3.exitMatch(5, __stack.locals.__armval_6);
2026
+ return;
2027
+ });
2028
+ }
2029
+ }
2030
+ ], void 0, { matchId: 5 });
2031
+ await runner.step(3, async (runner2) => {
2032
+ __functionCompleted = true;
2033
+ runner2.halt(__stack.locals.__matchval_5);
2034
+ return;
2035
+ });
2036
+ });
2037
+ if (runner.halted) {
2038
+ if (isFailure(runner.haltResult)) {
2039
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2040
+ }
2041
+ return runner.haltResult;
2042
+ }
2043
+ } catch (__error) {
2044
+ if (__error instanceof RestoreSignal) {
2045
+ throw __error;
2046
+ }
2047
+ if (__error instanceof AgencyAbort) {
2048
+ throw __error;
2049
+ }
2050
+ {
2051
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2052
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2053
+ const __log = __createLogger(__ctx.logLevel);
2054
+ __log.error("Function entityFinalize threw an exception (converted to Failure): " + __errMsg);
2055
+ if (__errStack) __log.error(__errStack);
2056
+ __ctx.statelogClient?.error?.({
2057
+ errorType: "runtimeError",
2058
+ message: __errMsg,
2059
+ functionName: "entityFinalize",
2060
+ retryable: __self.__retryable
2061
+ });
2062
+ }
2063
+ return failure(
2064
+ __error instanceof Error ? __error.message : String(__error),
2065
+ {
2066
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2067
+ retryable: __self.__retryable,
2068
+ functionName: "entityFinalize",
2069
+ args: __stack.args
2070
+ }
2071
+ );
2072
+ } finally {
2073
+ __stateStack()?.pop();
2074
+ if (__functionCompleted) {
2075
+ await callHook({
2076
+ name: "onFunctionEnd",
2077
+ data: {
2078
+ functionName: "entityFinalize",
2079
+ timeTaken: performance.now() - __funcStartTime
2080
+ }
2081
+ });
2082
+ }
2083
+ }
2084
+ }
2085
+ const entityFinalize = __AgencyFunction.create({
2086
+ name: "entityFinalize",
2087
+ module: "stdlib/data/wikidata.agency",
2088
+ fn: __entityFinalize_impl,
2089
+ params: [{
2090
+ name: "fetchResult",
2091
+ hasDefault: false,
2092
+ defaultValue: void 0,
2093
+ variadic: false,
2094
+ isFunctionTyped: false
2095
+ }, {
2096
+ name: "qid",
2097
+ hasDefault: false,
2098
+ defaultValue: void 0,
2099
+ variadic: false,
2100
+ isFunctionTyped: false
2101
+ }],
2102
+ toolDefinition: {
2103
+ name: "entityFinalize",
2104
+ description: "No description provided.",
2105
+ schema: z.object({ "fetchResult": z.any(), "qid": z.string() })
2106
+ },
2107
+ safe: true,
2108
+ exported: false
2109
+ }, __toolRegistry);
2110
+ async function __queryFinalize_impl(fetchResult) {
2111
+ const __setupData = setupFunction();
2112
+ const __stack = __setupData.stack;
2113
+ const __step = __setupData.step;
2114
+ const __self = __setupData.self;
2115
+ const __ctx = getRuntimeContext().ctx;
2116
+ let __forked;
2117
+ let __functionCompleted = false;
2118
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
2119
+ await __initializeGlobals(__ctx);
2120
+ }
2121
+ let __funcStartTime = performance.now();
2122
+ __stack.args["fetchResult"] = fetchResult;
2123
+ __self.__retryable = __self.__retryable ?? true;
2124
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "queryFinalize", threads: __setupData.threads });
2125
+ let __resultCheckpointId = -1;
2126
+ if (__ctx._pendingArgOverrides) {
2127
+ const __overrides = __ctx._pendingArgOverrides;
2128
+ __ctx._pendingArgOverrides = void 0;
2129
+ if ("fetchResult" in __overrides) {
2130
+ fetchResult = __overrides["fetchResult"];
2131
+ __stack.args["fetchResult"] = fetchResult;
2132
+ }
2133
+ }
2134
+ try {
2135
+ await agencyStore.run({
2136
+ ...getRuntimeContext(),
2137
+ ctx: __ctx,
2138
+ stack: __setupData.stateStack,
2139
+ threads: __setupData.threads
2140
+ }, async () => {
2141
+ await runner.hook(0, async () => {
2142
+ await callHook({
2143
+ name: "onFunctionStart",
2144
+ data: {
2145
+ functionName: "queryFinalize",
2146
+ args: {
2147
+ fetchResult
2148
+ },
2149
+ moduleId: "stdlib/data/wikidata.agency"
2150
+ }
2151
+ });
2152
+ });
2153
+ await runner.step(1, async (runner2) => {
2154
+ __stack.locals.__scrutinee_11 = __stack.args.fetchResult;
2155
+ });
2156
+ await runner.ifElse(2, [
2157
+ {
2158
+ condition: async () => await isSuccess(__stack.locals.__scrutinee_11),
2159
+ body: async (runner2) => {
2160
+ await runner2.step(0, async (runner3) => {
2161
+ __stack.locals.body = __stack.locals.__scrutinee_11.value;
2162
+ });
2163
+ await runner2.step(1, async (runner3) => {
2164
+ __stack.locals.__armval_9 = await success(await __call(parseBindings, {
2165
+ type: "positional",
2166
+ args: [__stack.locals.body]
2167
+ }));
2168
+ if (hasInterrupts(__stack.locals.__armval_9)) {
2169
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2170
+ runner3.halt(__stack.locals.__armval_9);
2171
+ return;
2172
+ }
2173
+ });
2174
+ await runner2.step(2, async (runner3) => {
2175
+ runner3.exitMatch(8, __stack.locals.__armval_9);
2176
+ return;
2177
+ });
2178
+ }
2179
+ },
2180
+ {
2181
+ condition: async () => await isFailure(__stack.locals.__scrutinee_11),
2182
+ body: async (runner2) => {
2183
+ await runner2.step(3, async (runner3) => {
2184
+ __stack.locals.err = __stack.locals.__scrutinee_11.error;
2185
+ });
2186
+ await runner2.step(4, async (runner3) => {
2187
+ __stack.locals.__armval_10 = failure(await __call(wikidataError, {
2188
+ type: "positional",
2189
+ args: [__stack.locals.err]
2190
+ }), { checkpoint: getRuntimeContext().ctx.getResultCheckpoint(), functionName: "queryFinalize", args: __stack.args });
2191
+ if (hasInterrupts(__stack.locals.__armval_10)) {
2192
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2193
+ runner3.halt(__stack.locals.__armval_10);
2194
+ return;
2195
+ }
2196
+ });
2197
+ await runner2.step(5, async (runner3) => {
2198
+ runner3.exitMatch(8, __stack.locals.__armval_10);
2199
+ return;
2200
+ });
2201
+ }
2202
+ }
2203
+ ], void 0, { matchId: 8 });
2204
+ await runner.step(3, async (runner2) => {
2205
+ __functionCompleted = true;
2206
+ runner2.halt(__stack.locals.__matchval_8);
2207
+ return;
2208
+ });
2209
+ });
2210
+ if (runner.halted) {
2211
+ if (isFailure(runner.haltResult)) {
2212
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2213
+ }
2214
+ return runner.haltResult;
2215
+ }
2216
+ } catch (__error) {
2217
+ if (__error instanceof RestoreSignal) {
2218
+ throw __error;
2219
+ }
2220
+ if (__error instanceof AgencyAbort) {
2221
+ throw __error;
2222
+ }
2223
+ {
2224
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2225
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2226
+ const __log = __createLogger(__ctx.logLevel);
2227
+ __log.error("Function queryFinalize threw an exception (converted to Failure): " + __errMsg);
2228
+ if (__errStack) __log.error(__errStack);
2229
+ __ctx.statelogClient?.error?.({
2230
+ errorType: "runtimeError",
2231
+ message: __errMsg,
2232
+ functionName: "queryFinalize",
2233
+ retryable: __self.__retryable
2234
+ });
2235
+ }
2236
+ return failure(
2237
+ __error instanceof Error ? __error.message : String(__error),
2238
+ {
2239
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2240
+ retryable: __self.__retryable,
2241
+ functionName: "queryFinalize",
2242
+ args: __stack.args
2243
+ }
2244
+ );
2245
+ } finally {
2246
+ __stateStack()?.pop();
2247
+ if (__functionCompleted) {
2248
+ await callHook({
2249
+ name: "onFunctionEnd",
2250
+ data: {
2251
+ functionName: "queryFinalize",
2252
+ timeTaken: performance.now() - __funcStartTime
2253
+ }
2254
+ });
2255
+ }
2256
+ }
2257
+ }
2258
+ const queryFinalize = __AgencyFunction.create({
2259
+ name: "queryFinalize",
2260
+ module: "stdlib/data/wikidata.agency",
2261
+ fn: __queryFinalize_impl,
2262
+ params: [{
2263
+ name: "fetchResult",
2264
+ hasDefault: false,
2265
+ defaultValue: void 0,
2266
+ variadic: false,
2267
+ isFunctionTyped: false
2268
+ }],
2269
+ toolDefinition: {
2270
+ name: "queryFinalize",
2271
+ description: "No description provided.",
2272
+ schema: z.object({ "fetchResult": z.any() })
2273
+ },
2274
+ safe: true,
2275
+ exported: false
2276
+ }, __toolRegistry);
2277
+ async function __wikidataFetch_impl(base, domains, path2) {
2278
+ const __setupData = setupFunction();
2279
+ const __stack = __setupData.stack;
2280
+ const __step = __setupData.step;
2281
+ const __self = __setupData.self;
2282
+ const __ctx = getRuntimeContext().ctx;
2283
+ let __forked;
2284
+ let __functionCompleted = false;
2285
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
2286
+ await __initializeGlobals(__ctx);
2287
+ }
2288
+ let __funcStartTime = performance.now();
2289
+ __stack.args["base"] = base;
2290
+ __stack.args["domains"] = domains;
2291
+ __stack.args["path"] = path2;
2292
+ __self.__retryable = __self.__retryable ?? true;
2293
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataFetch", threads: __setupData.threads });
2294
+ let __resultCheckpointId = -1;
2295
+ if (__ctx._pendingArgOverrides) {
2296
+ const __overrides = __ctx._pendingArgOverrides;
2297
+ __ctx._pendingArgOverrides = void 0;
2298
+ if ("base" in __overrides) {
2299
+ base = __overrides["base"];
2300
+ __stack.args["base"] = base;
2301
+ }
2302
+ if ("domains" in __overrides) {
2303
+ domains = __overrides["domains"];
2304
+ __stack.args["domains"] = domains;
2305
+ }
2306
+ if ("path" in __overrides) {
2307
+ path2 = __overrides["path"];
2308
+ __stack.args["path"] = path2;
2309
+ }
2310
+ }
2311
+ try {
2312
+ await agencyStore.run({
2313
+ ...getRuntimeContext(),
2314
+ ctx: __ctx,
2315
+ stack: __setupData.stateStack,
2316
+ threads: __setupData.threads
2317
+ }, async () => {
2318
+ await runner.hook(0, async () => {
2319
+ await callHook({
2320
+ name: "onFunctionStart",
2321
+ data: {
2322
+ functionName: "wikidataFetch",
2323
+ args: {
2324
+ base,
2325
+ domains,
2326
+ path: path2
2327
+ },
2328
+ moduleId: "stdlib/data/wikidata.agency"
2329
+ }
2330
+ });
2331
+ });
2332
+ await runner.handle(1, async (data) => {
2333
+ if (__eq(data.effect, `std::http::fetchJSON`)) {
2334
+ return await approve();
2335
+ }
2336
+ }, async (runner2) => {
2337
+ await runner2.step(0, async (runner3) => {
2338
+ __functionCompleted = true;
2339
+ runner3.halt(await __call(fetchJSON, {
2340
+ type: "named",
2341
+ positionalArgs: [],
2342
+ namedArgs: {
2343
+ baseUrl: __stack.args.base,
2344
+ path: __stack.args.path,
2345
+ headers: __globals().get("stdlib/data/wikidata.agency", "WD_HEADERS"),
2346
+ allowedDomains: __stack.args.domains
2347
+ }
2348
+ }));
2349
+ return;
2350
+ });
2351
+ });
2352
+ });
2353
+ if (runner.halted) {
2354
+ if (isFailure(runner.haltResult)) {
2355
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2356
+ }
2357
+ return runner.haltResult;
2358
+ }
2359
+ } catch (__error) {
2360
+ if (__error instanceof RestoreSignal) {
2361
+ throw __error;
2362
+ }
2363
+ if (__error instanceof AgencyAbort) {
2364
+ throw __error;
2365
+ }
2366
+ {
2367
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2368
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2369
+ const __log = __createLogger(__ctx.logLevel);
2370
+ __log.error("Function wikidataFetch threw an exception (converted to Failure): " + __errMsg);
2371
+ if (__errStack) __log.error(__errStack);
2372
+ __ctx.statelogClient?.error?.({
2373
+ errorType: "runtimeError",
2374
+ message: __errMsg,
2375
+ functionName: "wikidataFetch",
2376
+ retryable: __self.__retryable
2377
+ });
2378
+ }
2379
+ return failure(
2380
+ __error instanceof Error ? __error.message : String(__error),
2381
+ {
2382
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2383
+ retryable: __self.__retryable,
2384
+ functionName: "wikidataFetch",
2385
+ args: __stack.args
2386
+ }
2387
+ );
2388
+ } finally {
2389
+ __stateStack()?.pop();
2390
+ if (__functionCompleted) {
2391
+ await callHook({
2392
+ name: "onFunctionEnd",
2393
+ data: {
2394
+ functionName: "wikidataFetch",
2395
+ timeTaken: performance.now() - __funcStartTime
2396
+ }
2397
+ });
2398
+ }
2399
+ }
2400
+ }
2401
+ const wikidataFetch = __AgencyFunction.create({
2402
+ name: "wikidataFetch",
2403
+ module: "stdlib/data/wikidata.agency",
2404
+ fn: __wikidataFetch_impl,
2405
+ params: [{
2406
+ name: "base",
2407
+ hasDefault: false,
2408
+ defaultValue: void 0,
2409
+ variadic: false,
2410
+ isFunctionTyped: false
2411
+ }, {
2412
+ name: "domains",
2413
+ hasDefault: false,
2414
+ defaultValue: void 0,
2415
+ variadic: false,
2416
+ isFunctionTyped: false
2417
+ }, {
2418
+ name: "path",
2419
+ hasDefault: false,
2420
+ defaultValue: void 0,
2421
+ variadic: false,
2422
+ isFunctionTyped: false
2423
+ }],
2424
+ toolDefinition: {
2425
+ name: "wikidataFetch",
2426
+ description: "No description provided.",
2427
+ schema: z.object({ "base": z.string(), "domains": z.array(z.string()), "path": z.string() })
2428
+ },
2429
+ safe: true,
2430
+ exported: false
2431
+ }, __toolRegistry);
2432
+ async function __wikidataSearch_impl(name, limit = __UNSET) {
2433
+ const __setupData = setupFunction();
2434
+ const __stack = __setupData.stack;
2435
+ const __step = __setupData.step;
2436
+ const __self = __setupData.self;
2437
+ const __ctx = getRuntimeContext().ctx;
2438
+ let __forked;
2439
+ let __functionCompleted = false;
2440
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
2441
+ await __initializeGlobals(__ctx);
2442
+ }
2443
+ let __funcStartTime = performance.now();
2444
+ __stack.args["name"] = name;
2445
+ __stack.args["limit"] = limit === __UNSET ? 5 : limit;
2446
+ __self.__retryable = __self.__retryable ?? true;
2447
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataSearch", threads: __setupData.threads });
2448
+ let __resultCheckpointId = -1;
2449
+ if (__ctx._pendingArgOverrides) {
2450
+ const __overrides = __ctx._pendingArgOverrides;
2451
+ __ctx._pendingArgOverrides = void 0;
2452
+ if ("name" in __overrides) {
2453
+ name = __overrides["name"];
2454
+ __stack.args["name"] = name;
2455
+ }
2456
+ if ("limit" in __overrides) {
2457
+ limit = __overrides["limit"];
2458
+ __stack.args["limit"] = limit;
2459
+ }
2460
+ }
2461
+ try {
2462
+ await agencyStore.run({
2463
+ ...getRuntimeContext(),
2464
+ ctx: __ctx,
2465
+ stack: __setupData.stateStack,
2466
+ threads: __setupData.threads
2467
+ }, async () => {
2468
+ await runner.hook(0, async () => {
2469
+ await callHook({
2470
+ name: "onFunctionStart",
2471
+ data: {
2472
+ functionName: "wikidataSearch",
2473
+ args: {
2474
+ name,
2475
+ limit
2476
+ },
2477
+ moduleId: "stdlib/data/wikidata.agency"
2478
+ }
2479
+ });
2480
+ });
2481
+ await runner.step(1, async (runner2) => {
2482
+ const __response = getRuntimeContext().ctx.getInterruptResponse(__self.__interruptId_1);
2483
+ if (__response) {
2484
+ if (__response.type === "approve") {
2485
+ } else if (__response.type === "reject") {
2486
+ runner2.halt(failure("interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.getResultCheckpoint() }));
2487
+ return;
2488
+ }
2489
+ } else {
2490
+ const __handlerResult = await interruptWithHandlers("std::wikidata", `Search Wikidata for this name?`, {
2491
+ "op": `search`,
2492
+ "query": __stack.args.name
2493
+ }, "std::data/wikidata", __ctx, __stateStack());
2494
+ if (isRejected(__handlerResult)) {
2495
+ runner2.halt(failure(__handlerResult.value ?? "interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.checkpoints.get(__resultCheckpointId) }));
2496
+ return;
2497
+ }
2498
+ if (!isApproved(__handlerResult)) {
2499
+ __self.__interruptId_1 = __handlerResult[0].interruptId;
2500
+ const __checkpointId = getRuntimeContext().ctx.checkpoints.create(__stateStack(), __ctx, { moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataSearch", stepPath: "1" });
2501
+ __handlerResult[0].checkpointId = __checkpointId;
2502
+ __handlerResult[0].checkpoint = getRuntimeContext().ctx.checkpoints.get(__checkpointId);
2503
+ runner2.halt(__handlerResult);
2504
+ return;
2505
+ }
2506
+ }
2507
+ });
2508
+ await runner.step(2, async (runner2) => {
2509
+ __stack.locals.result = await __call(wikidataFetch, {
2510
+ type: "positional",
2511
+ args: [__readStatic(WD_BASE, "WD_BASE", "stdlib/data/wikidata.agency"), __readStatic(WD_DOMAINS, "WD_DOMAINS", "stdlib/data/wikidata.agency"), await __call(buildSearchPath, {
2512
+ type: "positional",
2513
+ args: [__stack.args.name, __stack.args.limit]
2514
+ })]
2515
+ });
2516
+ if (hasInterrupts(__stack.locals.result)) {
2517
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2518
+ runner2.halt(__stack.locals.result);
2519
+ return;
2520
+ }
2521
+ });
2522
+ await runner.step(3, async (runner2) => {
2523
+ __functionCompleted = true;
2524
+ runner2.halt(await __call(searchFinalize, {
2525
+ type: "positional",
2526
+ args: [__stack.locals.result]
2527
+ }));
2528
+ return;
2529
+ });
2530
+ });
2531
+ if (runner.halted) {
2532
+ if (isFailure(runner.haltResult)) {
2533
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2534
+ }
2535
+ return runner.haltResult;
2536
+ }
2537
+ } catch (__error) {
2538
+ if (__error instanceof RestoreSignal) {
2539
+ throw __error;
2540
+ }
2541
+ if (__error instanceof AgencyAbort) {
2542
+ throw __error;
2543
+ }
2544
+ {
2545
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2546
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2547
+ const __log = __createLogger(__ctx.logLevel);
2548
+ __log.error("Function wikidataSearch threw an exception (converted to Failure): " + __errMsg);
2549
+ if (__errStack) __log.error(__errStack);
2550
+ __ctx.statelogClient?.error?.({
2551
+ errorType: "runtimeError",
2552
+ message: __errMsg,
2553
+ functionName: "wikidataSearch",
2554
+ retryable: __self.__retryable
2555
+ });
2556
+ }
2557
+ return failure(
2558
+ __error instanceof Error ? __error.message : String(__error),
2559
+ {
2560
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2561
+ retryable: __self.__retryable,
2562
+ functionName: "wikidataSearch",
2563
+ args: __stack.args
2564
+ }
2565
+ );
2566
+ } finally {
2567
+ __stateStack()?.pop();
2568
+ if (__functionCompleted) {
2569
+ await callHook({
2570
+ name: "onFunctionEnd",
2571
+ data: {
2572
+ functionName: "wikidataSearch",
2573
+ timeTaken: performance.now() - __funcStartTime
2574
+ }
2575
+ });
2576
+ }
2577
+ }
2578
+ }
2579
+ const wikidataSearch = __AgencyFunction.create({
2580
+ name: "wikidataSearch",
2581
+ module: "stdlib/data/wikidata.agency",
2582
+ fn: __wikidataSearch_impl,
2583
+ params: [{
2584
+ name: "name",
2585
+ hasDefault: false,
2586
+ defaultValue: void 0,
2587
+ variadic: false,
2588
+ isFunctionTyped: false
2589
+ }, {
2590
+ name: "limit",
2591
+ hasDefault: true,
2592
+ defaultValue: void 0,
2593
+ variadic: false,
2594
+ isFunctionTyped: false
2595
+ }],
2596
+ toolDefinition: {
2597
+ name: "wikidataSearch",
2598
+ description: `Search Wikidata for entities by name. Returns matches with QID, label, description, and URL. The
2599
+ QID identifies the entity for a follow-up entity fetch or SPARQL query. Empty result set returns an
2600
+ empty list.
2601
+
2602
+ @param name - The person, organization, place, or thing to search for
2603
+ @param limit - Maximum number of matches to return (default 5)`,
2604
+ schema: z.object({ "name": z.string(), "limit": z.number().nullable().describe("Default: 5") })
2605
+ },
2606
+ safe: true,
2607
+ exported: true
2608
+ }, __toolRegistry);
2609
+ async function __wikidataEntity_impl(qid) {
2610
+ const __setupData = setupFunction();
2611
+ const __stack = __setupData.stack;
2612
+ const __step = __setupData.step;
2613
+ const __self = __setupData.self;
2614
+ const __ctx = getRuntimeContext().ctx;
2615
+ let __forked;
2616
+ let __functionCompleted = false;
2617
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
2618
+ await __initializeGlobals(__ctx);
2619
+ }
2620
+ let __funcStartTime = performance.now();
2621
+ __stack.args["qid"] = qid;
2622
+ __self.__retryable = __self.__retryable ?? true;
2623
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataEntity", threads: __setupData.threads });
2624
+ let __resultCheckpointId = -1;
2625
+ if (__ctx._pendingArgOverrides) {
2626
+ const __overrides = __ctx._pendingArgOverrides;
2627
+ __ctx._pendingArgOverrides = void 0;
2628
+ if ("qid" in __overrides) {
2629
+ qid = __overrides["qid"];
2630
+ __stack.args["qid"] = qid;
2631
+ }
2632
+ }
2633
+ try {
2634
+ await agencyStore.run({
2635
+ ...getRuntimeContext(),
2636
+ ctx: __ctx,
2637
+ stack: __setupData.stateStack,
2638
+ threads: __setupData.threads
2639
+ }, async () => {
2640
+ await runner.hook(0, async () => {
2641
+ await callHook({
2642
+ name: "onFunctionStart",
2643
+ data: {
2644
+ functionName: "wikidataEntity",
2645
+ args: {
2646
+ qid
2647
+ },
2648
+ moduleId: "stdlib/data/wikidata.agency"
2649
+ }
2650
+ });
2651
+ });
2652
+ await runner.step(1, async (runner2) => {
2653
+ const __response = getRuntimeContext().ctx.getInterruptResponse(__self.__interruptId_1);
2654
+ if (__response) {
2655
+ if (__response.type === "approve") {
2656
+ } else if (__response.type === "reject") {
2657
+ runner2.halt(failure("interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.getResultCheckpoint() }));
2658
+ return;
2659
+ }
2660
+ } else {
2661
+ const __handlerResult = await interruptWithHandlers("std::wikidata", `Fetch this Wikidata entity?`, {
2662
+ "op": `entity`,
2663
+ "query": __stack.args.qid
2664
+ }, "std::data/wikidata", __ctx, __stateStack());
2665
+ if (isRejected(__handlerResult)) {
2666
+ runner2.halt(failure(__handlerResult.value ?? "interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.checkpoints.get(__resultCheckpointId) }));
2667
+ return;
2668
+ }
2669
+ if (!isApproved(__handlerResult)) {
2670
+ __self.__interruptId_1 = __handlerResult[0].interruptId;
2671
+ const __checkpointId = getRuntimeContext().ctx.checkpoints.create(__stateStack(), __ctx, { moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataEntity", stepPath: "1" });
2672
+ __handlerResult[0].checkpointId = __checkpointId;
2673
+ __handlerResult[0].checkpoint = getRuntimeContext().ctx.checkpoints.get(__checkpointId);
2674
+ runner2.halt(__handlerResult);
2675
+ return;
2676
+ }
2677
+ }
2678
+ });
2679
+ await runner.step(2, async (runner2) => {
2680
+ __stack.locals.result = await __call(wikidataFetch, {
2681
+ type: "positional",
2682
+ args: [__readStatic(WD_BASE, "WD_BASE", "stdlib/data/wikidata.agency"), __readStatic(WD_DOMAINS, "WD_DOMAINS", "stdlib/data/wikidata.agency"), await __call(buildEntityPath, {
2683
+ type: "positional",
2684
+ args: [__stack.args.qid]
2685
+ })]
2686
+ });
2687
+ if (hasInterrupts(__stack.locals.result)) {
2688
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2689
+ runner2.halt(__stack.locals.result);
2690
+ return;
2691
+ }
2692
+ });
2693
+ await runner.step(3, async (runner2) => {
2694
+ __functionCompleted = true;
2695
+ runner2.halt(await __call(entityFinalize, {
2696
+ type: "positional",
2697
+ args: [__stack.locals.result, __stack.args.qid]
2698
+ }));
2699
+ return;
2700
+ });
2701
+ });
2702
+ if (runner.halted) {
2703
+ if (isFailure(runner.haltResult)) {
2704
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2705
+ }
2706
+ return runner.haltResult;
2707
+ }
2708
+ } catch (__error) {
2709
+ if (__error instanceof RestoreSignal) {
2710
+ throw __error;
2711
+ }
2712
+ if (__error instanceof AgencyAbort) {
2713
+ throw __error;
2714
+ }
2715
+ {
2716
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2717
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2718
+ const __log = __createLogger(__ctx.logLevel);
2719
+ __log.error("Function wikidataEntity threw an exception (converted to Failure): " + __errMsg);
2720
+ if (__errStack) __log.error(__errStack);
2721
+ __ctx.statelogClient?.error?.({
2722
+ errorType: "runtimeError",
2723
+ message: __errMsg,
2724
+ functionName: "wikidataEntity",
2725
+ retryable: __self.__retryable
2726
+ });
2727
+ }
2728
+ return failure(
2729
+ __error instanceof Error ? __error.message : String(__error),
2730
+ {
2731
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2732
+ retryable: __self.__retryable,
2733
+ functionName: "wikidataEntity",
2734
+ args: __stack.args
2735
+ }
2736
+ );
2737
+ } finally {
2738
+ __stateStack()?.pop();
2739
+ if (__functionCompleted) {
2740
+ await callHook({
2741
+ name: "onFunctionEnd",
2742
+ data: {
2743
+ functionName: "wikidataEntity",
2744
+ timeTaken: performance.now() - __funcStartTime
2745
+ }
2746
+ });
2747
+ }
2748
+ }
2749
+ }
2750
+ const wikidataEntity = __AgencyFunction.create({
2751
+ name: "wikidataEntity",
2752
+ module: "stdlib/data/wikidata.agency",
2753
+ fn: __wikidataEntity_impl,
2754
+ params: [{
2755
+ name: "qid",
2756
+ hasDefault: false,
2757
+ defaultValue: void 0,
2758
+ variadic: false,
2759
+ isFunctionTyped: false
2760
+ }],
2761
+ toolDefinition: {
2762
+ name: "wikidataEntity",
2763
+ description: `Fetch one Wikidata entity by its QID. Returns the English label, description, aliases, and claims
2764
+ (property id to values; entity-valued claims are QIDs to resolve separately). Unknown QID returns
2765
+ a failure.
2766
+
2767
+ @param qid - The Wikidata entity id, e.g. "Q42"`,
2768
+ schema: z.object({ "qid": z.string() })
2769
+ },
2770
+ safe: true,
2771
+ exported: true
2772
+ }, __toolRegistry);
2773
+ async function __wikidataQuery_impl(sparql) {
2774
+ const __setupData = setupFunction();
2775
+ const __stack = __setupData.stack;
2776
+ const __step = __setupData.step;
2777
+ const __self = __setupData.self;
2778
+ const __ctx = getRuntimeContext().ctx;
2779
+ let __forked;
2780
+ let __functionCompleted = false;
2781
+ if (!__globals().isInitialized("stdlib/data/wikidata.agency")) {
2782
+ await __initializeGlobals(__ctx);
2783
+ }
2784
+ let __funcStartTime = performance.now();
2785
+ __stack.args["sparql"] = sparql;
2786
+ __self.__retryable = __self.__retryable ?? true;
2787
+ const runner = new Runner(__ctx, __stack, { state: __stack, moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataQuery", threads: __setupData.threads });
2788
+ let __resultCheckpointId = -1;
2789
+ if (__ctx._pendingArgOverrides) {
2790
+ const __overrides = __ctx._pendingArgOverrides;
2791
+ __ctx._pendingArgOverrides = void 0;
2792
+ if ("sparql" in __overrides) {
2793
+ sparql = __overrides["sparql"];
2794
+ __stack.args["sparql"] = sparql;
2795
+ }
2796
+ }
2797
+ try {
2798
+ await agencyStore.run({
2799
+ ...getRuntimeContext(),
2800
+ ctx: __ctx,
2801
+ stack: __setupData.stateStack,
2802
+ threads: __setupData.threads
2803
+ }, async () => {
2804
+ await runner.hook(0, async () => {
2805
+ await callHook({
2806
+ name: "onFunctionStart",
2807
+ data: {
2808
+ functionName: "wikidataQuery",
2809
+ args: {
2810
+ sparql
2811
+ },
2812
+ moduleId: "stdlib/data/wikidata.agency"
2813
+ }
2814
+ });
2815
+ });
2816
+ await runner.step(1, async (runner2) => {
2817
+ const __response = getRuntimeContext().ctx.getInterruptResponse(__self.__interruptId_1);
2818
+ if (__response) {
2819
+ if (__response.type === "approve") {
2820
+ } else if (__response.type === "reject") {
2821
+ runner2.halt(failure("interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.getResultCheckpoint() }));
2822
+ return;
2823
+ }
2824
+ } else {
2825
+ const __handlerResult = await interruptWithHandlers("std::wikidata", `Run this SPARQL query against Wikidata?`, {
2826
+ "op": `query`,
2827
+ "query": __stack.args.sparql
2828
+ }, "std::data/wikidata", __ctx, __stateStack());
2829
+ if (isRejected(__handlerResult)) {
2830
+ runner2.halt(failure(__handlerResult.value ?? "interrupt rejected", { retryable: false, checkpoint: getRuntimeContext().ctx.checkpoints.get(__resultCheckpointId) }));
2831
+ return;
2832
+ }
2833
+ if (!isApproved(__handlerResult)) {
2834
+ __self.__interruptId_1 = __handlerResult[0].interruptId;
2835
+ const __checkpointId = getRuntimeContext().ctx.checkpoints.create(__stateStack(), __ctx, { moduleId: "stdlib/data/wikidata.agency", scopeName: "wikidataQuery", stepPath: "1" });
2836
+ __handlerResult[0].checkpointId = __checkpointId;
2837
+ __handlerResult[0].checkpoint = getRuntimeContext().ctx.checkpoints.get(__checkpointId);
2838
+ runner2.halt(__handlerResult);
2839
+ return;
2840
+ }
2841
+ }
2842
+ });
2843
+ await runner.step(2, async (runner2) => {
2844
+ __stack.locals.result = await __call(wikidataFetch, {
2845
+ type: "positional",
2846
+ args: [__readStatic(WDQS_BASE, "WDQS_BASE", "stdlib/data/wikidata.agency"), __readStatic(WDQS_DOMAINS, "WDQS_DOMAINS", "stdlib/data/wikidata.agency"), await __call(buildQueryPath, {
2847
+ type: "positional",
2848
+ args: [__stack.args.sparql]
2849
+ })]
2850
+ });
2851
+ if (hasInterrupts(__stack.locals.result)) {
2852
+ await getRuntimeContext().ctx.pendingPromises.awaitAll();
2853
+ runner2.halt(__stack.locals.result);
2854
+ return;
2855
+ }
2856
+ });
2857
+ await runner.step(3, async (runner2) => {
2858
+ __functionCompleted = true;
2859
+ runner2.halt(await __call(queryFinalize, {
2860
+ type: "positional",
2861
+ args: [__stack.locals.result]
2862
+ }));
2863
+ return;
2864
+ });
2865
+ });
2866
+ if (runner.halted) {
2867
+ if (isFailure(runner.haltResult)) {
2868
+ runner.haltResult.retryable = runner.haltResult.retryable && __self.__retryable;
2869
+ }
2870
+ return runner.haltResult;
2871
+ }
2872
+ } catch (__error) {
2873
+ if (__error instanceof RestoreSignal) {
2874
+ throw __error;
2875
+ }
2876
+ if (__error instanceof AgencyAbort) {
2877
+ throw __error;
2878
+ }
2879
+ {
2880
+ const __errMsg = __error instanceof Error ? __error.message : String(__error);
2881
+ const __errStack = __error instanceof Error && __error.stack ? __error.stack : "";
2882
+ const __log = __createLogger(__ctx.logLevel);
2883
+ __log.error("Function wikidataQuery threw an exception (converted to Failure): " + __errMsg);
2884
+ if (__errStack) __log.error(__errStack);
2885
+ __ctx.statelogClient?.error?.({
2886
+ errorType: "runtimeError",
2887
+ message: __errMsg,
2888
+ functionName: "wikidataQuery",
2889
+ retryable: __self.__retryable
2890
+ });
2891
+ }
2892
+ return failure(
2893
+ __error instanceof Error ? __error.message : String(__error),
2894
+ {
2895
+ checkpoint: getRuntimeContext().ctx.getResultCheckpoint(),
2896
+ retryable: __self.__retryable,
2897
+ functionName: "wikidataQuery",
2898
+ args: __stack.args
2899
+ }
2900
+ );
2901
+ } finally {
2902
+ __stateStack()?.pop();
2903
+ if (__functionCompleted) {
2904
+ await callHook({
2905
+ name: "onFunctionEnd",
2906
+ data: {
2907
+ functionName: "wikidataQuery",
2908
+ timeTaken: performance.now() - __funcStartTime
2909
+ }
2910
+ });
2911
+ }
2912
+ }
2913
+ }
2914
+ const wikidataQuery = __AgencyFunction.create({
2915
+ name: "wikidataQuery",
2916
+ module: "stdlib/data/wikidata.agency",
2917
+ fn: __wikidataQuery_impl,
2918
+ params: [{
2919
+ name: "sparql",
2920
+ hasDefault: false,
2921
+ defaultValue: void 0,
2922
+ variadic: false,
2923
+ isFunctionTyped: false
2924
+ }],
2925
+ toolDefinition: {
2926
+ name: "wikidataQuery",
2927
+ description: `Run a SPARQL query against the Wikidata Query Service and return rows (each a map of the SELECTed
2928
+ variable names to string values). Prefixes wd:/wdt:/rdfs: and SERVICE wikibase:label are available.
2929
+ Example: SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q5 . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } LIMIT 10
2930
+
2931
+ @param sparql - The SPARQL query text`,
2932
+ schema: z.object({ "sparql": z.string() })
2933
+ },
2934
+ safe: true,
2935
+ exported: true
2936
+ }, __toolRegistry);
2937
+ var stdin_default = graph;
2938
+ const __sourceMap = { "stdlib/data/wikidata.agency:buildSearchPath": { "1": { "line": 65, "col": 2 }, "2": { "line": 66, "col": 2 } }, "stdlib/data/wikidata.agency:buildEntityPath": { "1": { "line": 71, "col": 2 } }, "stdlib/data/wikidata.agency:buildQueryPath": { "1": { "line": 76, "col": 2 }, "2": { "line": 77, "col": 2 } }, "stdlib/data/wikidata.agency:parseSearch": { "1": { "line": 82, "col": 2 }, "2": { "line": 83, "col": 2 }, "3": { "line": 84, "col": 2 } }, "stdlib/data/wikidata.agency:__block_0": { "3.0": { "line": 85, "col": 4 }, "3.1": { "line": 86, "col": 4 } }, "stdlib/data/wikidata.agency:parseClaimValue": { "1": { "line": 98, "col": 2 }, "2": { "line": 99, "col": 2 }, "3": { "line": 102, "col": 2 }, "4": { "line": 103, "col": 2 }, "5": { "line": 104, "col": 2 }, "6": { "line": 105, "col": 2 }, "7": { "line": 108, "col": 2 }, "8": { "line": 111, "col": 2 }, "9": { "line": 114, "col": 2 }, "10": { "line": 117, "col": 2 }, "11": { "line": 120, "col": 2 }, "12": { "line": 123, "col": 2 }, "2.0": { "line": 100, "col": 4 }, "6.0": { "line": 106, "col": 4 }, "7.0": { "line": 109, "col": 4 }, "8.0": { "line": 112, "col": 4 }, "9.0": { "line": 115, "col": 4 }, "10.0": { "line": 118, "col": 4 }, "11.0": { "line": 121, "col": 4 } }, "stdlib/data/wikidata.agency:parseClaimList": { "1": { "line": 128, "col": 2 }, "2": { "line": 129, "col": 2 } }, "stdlib/data/wikidata.agency:__block_1": { "2.0": { "line": 130, "col": 4 }, "2.1": { "line": 131, "col": 4 } }, "stdlib/data/wikidata.agency:parseClaims": { "1": { "line": 137, "col": 2 }, "2": { "line": 138, "col": 2 } }, "stdlib/data/wikidata.agency:__block_2": { "2.0": { "line": 139, "col": 4 } }, "stdlib/data/wikidata.agency:parseEntity": { "1": { "line": 145, "col": 2 }, "2": { "line": 146, "col": 2 }, "3": { "line": 147, "col": 2 }, "4": { "line": 148, "col": 2 }, "5": { "line": 149, "col": 2 }, "6": { "line": 150, "col": 2 }, "7": { "line": 151, "col": 2 }, "8": { "line": 152, "col": 2 }, "9": { "line": 153, "col": 2 }, "10": { "line": 154, "col": 2 } }, "stdlib/data/wikidata.agency:__block_3": { "10.0": { "line": 159, "col": 6 }, "10.1": { "line": 160, "col": 6 } }, "stdlib/data/wikidata.agency:parseBindings": { "1": { "line": 168, "col": 2 }, "2": { "line": 169, "col": 2 }, "3": { "line": 170, "col": 2 }, "4": { "line": 171, "col": 2 } }, "stdlib/data/wikidata.agency:__block_4": { "4.0": { "line": 172, "col": 4 }, "4.1": { "line": 173, "col": 4 } }, "stdlib/data/wikidata.agency:__block_5": { "4.1.0": { "line": 174, "col": 6 }, "4.1.1": { "line": 175, "col": 6 } }, "stdlib/data/wikidata.agency:wikidataError": { "1": { "line": 182, "col": 2 } }, "stdlib/data/wikidata.agency:searchFinalize": { "1": { "line": 187, "col": 9 }, "2": { "line": 187, "col": 9 }, "3": { "line": 187, "col": 2 }, "2.0": { "line": 187, "col": 9 }, "2.1": { "line": 188, "col": 21 }, "2.2": { "line": 188, "col": 21 }, "2.3": { "line": 187, "col": 9 }, "2.4": { "line": 189, "col": 20 }, "2.5": { "line": 189, "col": 20 } }, "stdlib/data/wikidata.agency:entityFinalize": { "1": { "line": 195, "col": 9 }, "2": { "line": 195, "col": 9 }, "3": { "line": 195, "col": 2 }, "2.0": { "line": 195, "col": 9 }, "2.1": { "line": 197, "col": 6 }, "2.2": { "line": 198, "col": 6 }, "2.3.0": { "line": 200, "col": 8 }, "2.3": { "line": 199, "col": 6 }, "2.4": { "line": 202, "col": 6 }, "2.5": { "line": 195, "col": 9 }, "2.6": { "line": 204, "col": 20 }, "2.7": { "line": 204, "col": 20 } }, "stdlib/data/wikidata.agency:queryFinalize": { "1": { "line": 210, "col": 9 }, "2": { "line": 210, "col": 9 }, "3": { "line": 210, "col": 2 }, "2.0": { "line": 210, "col": 9 }, "2.1": { "line": 211, "col": 21 }, "2.2": { "line": 211, "col": 21 }, "2.3": { "line": 210, "col": 9 }, "2.4": { "line": 212, "col": 20 }, "2.5": { "line": 212, "col": 20 } }, "stdlib/data/wikidata.agency:wikidataFetch": { "1": { "line": 221, "col": 2 }, "1.0": { "line": 222, "col": 4 } }, "stdlib/data/wikidata.agency:wikidataSearch": { "1": { "line": 239, "col": 2 }, "2": { "line": 240, "col": 2 }, "3": { "line": 241, "col": 2 } }, "stdlib/data/wikidata.agency:wikidataEntity": { "1": { "line": 252, "col": 2 }, "2": { "line": 253, "col": 2 }, "3": { "line": 254, "col": 2 } }, "stdlib/data/wikidata.agency:wikidataQuery": { "1": { "line": 265, "col": 2 }, "2": { "line": 266, "col": 2 }, "3": { "line": 267, "col": 2 } } };
2939
+ export {
2940
+ Entity,
2941
+ EntityDetail,
2942
+ __getCheckpoints,
2943
+ __invokeFunction,
2944
+ __setDebugger,
2945
+ __setLLMClient,
2946
+ __setTraceFile,
2947
+ __sourceMap,
2948
+ __toolRegistry,
2949
+ approve,
2950
+ buildEntityPath,
2951
+ buildQueryPath,
2952
+ buildSearchPath,
2953
+ stdin_default as default,
2954
+ entityFinalize,
2955
+ hasInterrupts,
2956
+ interrupt,
2957
+ isDebugger,
2958
+ isInterrupt,
2959
+ parseBindings,
2960
+ parseClaimList,
2961
+ parseClaimValue,
2962
+ parseClaims,
2963
+ parseEntity,
2964
+ parseSearch,
2965
+ queryFinalize,
2966
+ reject,
2967
+ respondToInterrupts,
2968
+ rewindFrom,
2969
+ searchFinalize,
2970
+ wikidataEntity,
2971
+ wikidataError,
2972
+ wikidataFetch,
2973
+ wikidataQuery,
2974
+ wikidataSearch
2975
+ };