@ryuhq/sdk 0.0.5

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.
Files changed (61) hide show
  1. package/LICENSE +179 -0
  2. package/README.md +31 -0
  3. package/dist/agent.cjs +761 -0
  4. package/dist/agent.d.cts +3 -0
  5. package/dist/agent.d.ts +3 -0
  6. package/dist/agent.js +23 -0
  7. package/dist/chunk-GXHL5CO7.js +353 -0
  8. package/dist/chunk-KPKMMGVC.js +671 -0
  9. package/dist/chunk-ODFEUVPW.js +100 -0
  10. package/dist/cli.cjs +858 -0
  11. package/dist/cli.d.cts +1 -0
  12. package/dist/cli.d.ts +1 -0
  13. package/dist/cli.js +454 -0
  14. package/dist/index-CEbS1SlS.d.cts +988 -0
  15. package/dist/index-DAxq7Y0R.d.ts +988 -0
  16. package/dist/index.cjs +1900 -0
  17. package/dist/index.d.cts +759 -0
  18. package/dist/index.d.ts +759 -0
  19. package/dist/index.js +771 -0
  20. package/dist/manifest.cjs +399 -0
  21. package/dist/manifest.d.cts +355 -0
  22. package/dist/manifest.d.ts +355 -0
  23. package/dist/manifest.js +38 -0
  24. package/package.json +56 -0
  25. package/src/agent/agent.ts +208 -0
  26. package/src/agent/index.ts +51 -0
  27. package/src/agent/loop.test.ts +261 -0
  28. package/src/agent/loop.ts +259 -0
  29. package/src/agent/model-call.ts +190 -0
  30. package/src/agent/query.ts +40 -0
  31. package/src/agent/tools.ts +295 -0
  32. package/src/builder.ts +473 -0
  33. package/src/cli/dev.test.ts +178 -0
  34. package/src/cli/dev.ts +425 -0
  35. package/src/cli.ts +390 -0
  36. package/src/contracts-lockstep.test.ts +77 -0
  37. package/src/generated/plugin-manifest.ts +1121 -0
  38. package/src/index.ts +141 -0
  39. package/src/manifest.test.ts +610 -0
  40. package/src/manifest.ts +589 -0
  41. package/src/mcp/bridge.test.ts +196 -0
  42. package/src/mcp/client.ts +253 -0
  43. package/src/mcp/fixture-server.ts +23 -0
  44. package/src/mcp/server.ts +351 -0
  45. package/src/model/client.test.ts +107 -0
  46. package/src/model/client.ts +179 -0
  47. package/src/model/gateway.ts +41 -0
  48. package/src/plugin/ryu-plugin.ts +191 -0
  49. package/src/runnable/agent.ts +338 -0
  50. package/src/runnable/app.ts +233 -0
  51. package/src/runnable/index.ts +61 -0
  52. package/src/runnable/primitives-hostapi.test.ts +73 -0
  53. package/src/runnable/primitives.test.ts +286 -0
  54. package/src/runnable/primitives.ts +610 -0
  55. package/src/runnable/runnable-types.ts +113 -0
  56. package/src/runnable/runnable.test.ts +397 -0
  57. package/src/runnable/skill.ts +60 -0
  58. package/src/runnable/tool.ts +260 -0
  59. package/src/runnable/turn-hook.test.ts +81 -0
  60. package/src/runnable/turn-hook.ts +191 -0
  61. package/src/runnable/workflow.ts +76 -0
package/dist/index.js ADDED
@@ -0,0 +1,771 @@
1
+ import {
2
+ AppDependencySchema,
3
+ CapabilityReqSchema,
4
+ CompanionSurfaceSchema,
5
+ PluginManifestSchema,
6
+ RequiresSchema,
7
+ RunnableKindSchema,
8
+ RunnableMetaSchema,
9
+ SurfaceSchema,
10
+ ToolAppConfigSchema,
11
+ WidgetContributionSchema,
12
+ coreManifestJsonSchema,
13
+ validateManifestStrict,
14
+ validatePluginId
15
+ } from "./chunk-GXHL5CO7.js";
16
+ import {
17
+ Agent,
18
+ PRIMITIVE_BINDINGS,
19
+ createAgent,
20
+ createPrimitives,
21
+ httpPrimitiveTransport,
22
+ query,
23
+ ryuTool
24
+ } from "./chunk-KPKMMGVC.js";
25
+ import {
26
+ DEFAULT_GATEWAY_URL,
27
+ ModelClient,
28
+ assertAllowedEgressUrl,
29
+ defineModel,
30
+ resolveGatewayToken,
31
+ resolveGatewayUrl
32
+ } from "./chunk-ODFEUVPW.js";
33
+
34
+ // src/runnable/app.ts
35
+ var DEFAULT_APP_WIDGET_MIME = "text/html+skybridge";
36
+ var DEFAULT_APP_DISPLAY_MODE = "inline";
37
+ function appToolId(server, name) {
38
+ return `${server}__${name}`;
39
+ }
40
+ function defineApp(options) {
41
+ const server = options.server ?? options.slug;
42
+ const uri = `ui://widget/${options.slug}.html`;
43
+ const mime = options.mime ?? DEFAULT_APP_WIDGET_MIME;
44
+ const displayMode = options.displayMode ?? DEFAULT_APP_DISPLAY_MODE;
45
+ const hasCompanions = options.tools.some((t) => t.accessible === true);
46
+ const runnables = [];
47
+ const widgets = [];
48
+ for (const spec of options.tools) {
49
+ const isRender = spec.accessible !== true;
50
+ const id = appToolId(server, spec.name);
51
+ const config = {
52
+ slug: id,
53
+ description: spec.description,
54
+ widget: isRender,
55
+ widget_accessible: isRender ? hasCompanions : true,
56
+ ...spec.inputSchema ? { input_schema: spec.inputSchema } : {},
57
+ ...spec.invoking ? { invoking: spec.invoking } : {},
58
+ ...spec.invoked ? { invoked: spec.invoked } : {}
59
+ };
60
+ runnables.push({
61
+ id,
62
+ name: spec.name,
63
+ kind: "tool",
64
+ config
65
+ });
66
+ if (isRender) {
67
+ widgets.push({
68
+ tool_id: id,
69
+ uri,
70
+ ui_entry: options.uiEntry,
71
+ mime,
72
+ default_display_mode: displayMode
73
+ });
74
+ }
75
+ }
76
+ const contributes = {
77
+ turn_hooks: [],
78
+ composer_controls: [],
79
+ settings_tabs: [],
80
+ slash_commands: [],
81
+ widgets
82
+ };
83
+ const raw = {
84
+ id: options.id,
85
+ name: options.title,
86
+ version: options.version,
87
+ runnables,
88
+ permission_grants: options.grants ?? [],
89
+ activation_events: options.activationEvents ?? ["*"],
90
+ contributes,
91
+ // `targets: []` means EVERY surface, so an app that declares none is
92
+ // unrestricted — the backward-compatible default.
93
+ targets: options.targets ?? [],
94
+ // `requires` stays ABSENT (not `{apps:[],grants:[]}`) when undeclared, so the
95
+ // emitted manifest carries no key at all — matching Core's
96
+ // `Option<Requires>` + `skip_serializing_if = "Option::is_none"`.
97
+ ...options.requires ? {
98
+ requires: {
99
+ apps: options.requires.apps ?? [],
100
+ capabilities: options.requires.capabilities ?? [],
101
+ grants: options.requires.grants ?? []
102
+ }
103
+ } : {}
104
+ };
105
+ const result = PluginManifestSchema.safeParse(raw);
106
+ if (!result.success) {
107
+ const first = result.error.issues[0];
108
+ const field = first?.path.join(".") ?? "unknown";
109
+ const message = first?.message ?? "validation failed";
110
+ throw new Error(`plugin.json validation failed at '${field}': ${message}`);
111
+ }
112
+ return result.data;
113
+ }
114
+
115
+ // src/builder.ts
116
+ var RunnableBuilder = class {
117
+ _id = "";
118
+ _name = "";
119
+ id(value) {
120
+ this._id = value;
121
+ return this;
122
+ }
123
+ name(value) {
124
+ this._name = value;
125
+ return this;
126
+ }
127
+ };
128
+ var AgentBuilder = class extends RunnableBuilder {
129
+ build() {
130
+ const result = RunnableMetaSchema.safeParse({
131
+ id: this._id,
132
+ name: this._name,
133
+ kind: "agent"
134
+ });
135
+ if (!result.success) {
136
+ throw new Error(
137
+ `Invalid agent runnable: ${result.error.issues.map((i) => i.message).join("; ")}`
138
+ );
139
+ }
140
+ return result.data;
141
+ }
142
+ };
143
+ var WorkflowBuilder = class extends RunnableBuilder {
144
+ build() {
145
+ const result = RunnableMetaSchema.safeParse({
146
+ id: this._id,
147
+ name: this._name,
148
+ kind: "workflow"
149
+ });
150
+ if (!result.success) {
151
+ throw new Error(
152
+ `Invalid workflow runnable: ${result.error.issues.map((i) => i.message).join("; ")}`
153
+ );
154
+ }
155
+ return result.data;
156
+ }
157
+ };
158
+ var ToolBuilder = class extends RunnableBuilder {
159
+ build() {
160
+ const result = RunnableMetaSchema.safeParse({
161
+ id: this._id,
162
+ name: this._name,
163
+ kind: "tool"
164
+ });
165
+ if (!result.success) {
166
+ throw new Error(
167
+ `Invalid tool runnable: ${result.error.issues.map((i) => i.message).join("; ")}`
168
+ );
169
+ }
170
+ return result.data;
171
+ }
172
+ };
173
+ var SkillBuilder = class extends RunnableBuilder {
174
+ build() {
175
+ const result = RunnableMetaSchema.safeParse({
176
+ id: this._id,
177
+ name: this._name,
178
+ kind: "skill"
179
+ });
180
+ if (!result.success) {
181
+ throw new Error(
182
+ `Invalid skill runnable: ${result.error.issues.map((i) => i.message).join("; ")}`
183
+ );
184
+ }
185
+ return result.data;
186
+ }
187
+ };
188
+ var agent = () => new AgentBuilder();
189
+ var workflow = () => new WorkflowBuilder();
190
+ var tool = () => new ToolBuilder();
191
+ var skill = () => new SkillBuilder();
192
+ var PluginBuilder = class {
193
+ _id = "";
194
+ _name = "";
195
+ _version = "";
196
+ _runnables = [];
197
+ _grants = [];
198
+ _companion = void 0;
199
+ _dependencies = [];
200
+ _requiredCapabilities = [];
201
+ _requiredGrants = [];
202
+ _targets = [];
203
+ /** Set the reverse-domain app id (e.g. `"com.example.my-app"`). */
204
+ id(value) {
205
+ this._id = value;
206
+ return this;
207
+ }
208
+ /** Set the human-readable display name. */
209
+ name(value) {
210
+ this._name = value;
211
+ return this;
212
+ }
213
+ /** Set the semver version string (e.g. `"1.0.0"`). */
214
+ version(value) {
215
+ this._version = value;
216
+ return this;
217
+ }
218
+ /** Append a pre-built `RunnableMeta` (from any per-kind builder). */
219
+ runnable(meta) {
220
+ this._runnables.push(meta);
221
+ return this;
222
+ }
223
+ /** Declare a permission grant (e.g. `"mcp:web_search"`). */
224
+ grant(permission) {
225
+ this._grants.push(permission);
226
+ return this;
227
+ }
228
+ /** Set an optional Companion surface descriptor. */
229
+ companion(surface) {
230
+ this._companion = surface;
231
+ return this;
232
+ }
233
+ /**
234
+ * Declare a **plugin-to-plugin dependency**: `id` must be installed and is
235
+ * auto-enabled (in dependency order) before this plugin enables.
236
+ *
237
+ * `minVersion` is a MINIMUM — a bare `"1.2.0"` means `">=1.2.0"`, so an
238
+ * installed `2.0.0` satisfies it (comparator syntax like `">=1.2, <2"` is
239
+ * honoured verbatim).
240
+ */
241
+ dependsOn(id, minVersion) {
242
+ this._dependencies.push(
243
+ minVersion ? { id, min_version: minVersion } : { id }
244
+ );
245
+ return this;
246
+ }
247
+ /**
248
+ * Declare a permission grant implied by this plugin's dependencies
249
+ * (`requires.grants`). Declaration only — the Gateway remains the sole
250
+ * authority on what a grant allows. Use {@link PluginBuilder.grant} for the
251
+ * grants this plugin needs in its own right.
252
+ */
253
+ requiredGrant(permission) {
254
+ this._requiredGrants.push(permission);
255
+ return this;
256
+ }
257
+ /**
258
+ * Declare an abstract **capability** edge (`requires.capabilities`) the broker
259
+ * resolves to a bound provider at enable time — e.g. `requiresCapability("rag")`.
260
+ * Distinct from a specific-plugin dependency: a capability edge lets the
261
+ * binding registry choose the provider. `minVersion` is a MINIMUM (`"1.2.0"`
262
+ * = `">=1.2.0"`).
263
+ */
264
+ requiresCapability(capability, minVersion) {
265
+ this._requiredCapabilities.push(
266
+ minVersion ? { capability, min_version: minVersion } : { capability }
267
+ );
268
+ return this;
269
+ }
270
+ /**
271
+ * Restrict this plugin to a host surface (`"desktop"`, `"island"`, …).
272
+ * Declaring NO target is the default and means **every** surface.
273
+ */
274
+ target(surface) {
275
+ this._targets.push(surface);
276
+ return this;
277
+ }
278
+ /**
279
+ * Validate and return the assembled `PluginManifest`. Throws an `Error` with
280
+ * the failing field name and message when validation fails.
281
+ */
282
+ build() {
283
+ const hasRequires = this._dependencies.length > 0 || this._requiredCapabilities.length > 0 || this._requiredGrants.length > 0;
284
+ const raw = {
285
+ id: this._id,
286
+ name: this._name,
287
+ version: this._version,
288
+ runnables: this._runnables,
289
+ permission_grants: this._grants,
290
+ companion: this._companion,
291
+ targets: this._targets,
292
+ ...hasRequires ? {
293
+ requires: {
294
+ apps: this._dependencies,
295
+ capabilities: this._requiredCapabilities,
296
+ grants: this._requiredGrants
297
+ }
298
+ } : {}
299
+ };
300
+ const result = PluginManifestSchema.safeParse(raw);
301
+ if (!result.success) {
302
+ const first = result.error.issues[0];
303
+ const field = first?.path.join(".") ?? "unknown";
304
+ const message = first?.message ?? "validation failed";
305
+ throw new Error(
306
+ `plugin.json validation failed at '${field}': ${message}`
307
+ );
308
+ }
309
+ return result.data;
310
+ }
311
+ };
312
+ var AppBuilder = class {
313
+ _id = "";
314
+ _title = "";
315
+ _version = "";
316
+ _slug = "";
317
+ _server = void 0;
318
+ _displayMode = void 0;
319
+ _mime = void 0;
320
+ _uiEntry = "";
321
+ _grants = [];
322
+ _activationEvents = [];
323
+ _tools = [];
324
+ _dependencies = [];
325
+ _requiredCapabilities = [];
326
+ _requiredGrants = [];
327
+ _targets = [];
328
+ /** Set the reverse-domain app id (e.g. `"com.example.checklist"`). */
329
+ id(value) {
330
+ this._id = value;
331
+ return this;
332
+ }
333
+ /** Set the human-readable display name. */
334
+ title(value) {
335
+ this._title = value;
336
+ return this;
337
+ }
338
+ /** Set the semver version string (e.g. `"1.0.0"`). */
339
+ version(value) {
340
+ this._version = value;
341
+ return this;
342
+ }
343
+ /** Set the app slug (drives `ui://widget/<slug>.html` and the server default). */
344
+ slug(value) {
345
+ this._slug = value;
346
+ return this;
347
+ }
348
+ /** Override the MCP server namespace for tool ids (defaults to the slug). */
349
+ server(value) {
350
+ this._server = value;
351
+ return this;
352
+ }
353
+ /** Set the default widget display mode (`inline` | `fullscreen` | `pip`). */
354
+ displayMode(value) {
355
+ this._displayMode = value;
356
+ return this;
357
+ }
358
+ /** Override the widget MIME dialect (defaults to `text/html+skybridge`). */
359
+ mime(value) {
360
+ this._mime = value;
361
+ return this;
362
+ }
363
+ /** Set the widget UI source entry `ryu pack` bundles into `ui_code`. */
364
+ uiEntry(value) {
365
+ this._uiEntry = value;
366
+ return this;
367
+ }
368
+ /** Declare a permission grant (e.g. `"mcp:web_search"`). */
369
+ grant(permission) {
370
+ this._grants.push(permission);
371
+ return this;
372
+ }
373
+ /** Add a VS-Code-style activation event (empty = eager `["*"]`). */
374
+ activationEvent(event) {
375
+ this._activationEvents.push(event);
376
+ return this;
377
+ }
378
+ /** Append a tool spec (render tool unless `accessible:true`). */
379
+ tool(spec) {
380
+ this._tools.push(spec);
381
+ return this;
382
+ }
383
+ /**
384
+ * Declare a **plugin-to-plugin dependency** (auto-enabled, in dependency order,
385
+ * before this app). `minVersion` is a MINIMUM (`"1.2.0"` = `">=1.2.0"`).
386
+ */
387
+ dependsOn(id, minVersion) {
388
+ this._dependencies.push(
389
+ minVersion ? { id, min_version: minVersion } : { id }
390
+ );
391
+ return this;
392
+ }
393
+ /** Declare a grant implied by this app's dependencies (`requires.grants`). */
394
+ requiredGrant(permission) {
395
+ this._requiredGrants.push(permission);
396
+ return this;
397
+ }
398
+ /**
399
+ * Declare an abstract **capability** edge (`requires.capabilities`) the broker
400
+ * resolves to a bound provider at enable time — e.g. `requiresCapability("rag")`.
401
+ * Distinct from a specific-plugin dependency: a capability edge lets the
402
+ * binding registry choose the provider. `minVersion` is a MINIMUM (`"1.2.0"`
403
+ * = `">=1.2.0"`).
404
+ */
405
+ requiresCapability(capability, minVersion) {
406
+ this._requiredCapabilities.push(
407
+ minVersion ? { capability, min_version: minVersion } : { capability }
408
+ );
409
+ return this;
410
+ }
411
+ /** Restrict this app to a host surface. No target = every surface. */
412
+ target(surface) {
413
+ this._targets.push(surface);
414
+ return this;
415
+ }
416
+ /**
417
+ * Validate and return the assembled `PluginManifest`. Throws an `Error` naming
418
+ * the failing field when validation fails.
419
+ */
420
+ build() {
421
+ const hasRequires = this._dependencies.length > 0 || this._requiredCapabilities.length > 0 || this._requiredGrants.length > 0;
422
+ const options = {
423
+ id: this._id,
424
+ title: this._title,
425
+ version: this._version,
426
+ slug: this._slug,
427
+ uiEntry: this._uiEntry,
428
+ tools: this._tools,
429
+ grants: this._grants,
430
+ ...this._server ? { server: this._server } : {},
431
+ ...this._displayMode ? { displayMode: this._displayMode } : {},
432
+ ...this._mime ? { mime: this._mime } : {},
433
+ ...this._activationEvents.length > 0 ? { activationEvents: this._activationEvents } : {},
434
+ ...hasRequires ? {
435
+ requires: {
436
+ apps: this._dependencies,
437
+ capabilities: this._requiredCapabilities,
438
+ grants: this._requiredGrants
439
+ }
440
+ } : {},
441
+ ...this._targets.length > 0 ? { targets: this._targets } : {}
442
+ };
443
+ return defineApp(options);
444
+ }
445
+ };
446
+ var app = () => new AppBuilder();
447
+
448
+ // src/runnable/agent.ts
449
+ var CAPABILITY_SLOTS = ["rag", "memory", "tts", "stt"];
450
+ function lowerChatSlot(chat) {
451
+ if (chat === void 0) {
452
+ return {};
453
+ }
454
+ if (typeof chat === "string") {
455
+ return { model: chat };
456
+ }
457
+ return {
458
+ model: chat.model,
459
+ engine: chat.engine,
460
+ persona: chat.persona,
461
+ modelPrefKey: chat.modelPrefKey
462
+ };
463
+ }
464
+ function lowerCapabilitySlot(capability, slot, card) {
465
+ if (slot === void 0 || slot === false) {
466
+ return;
467
+ }
468
+ if (slot === true) {
469
+ card.capabilities.push({ capability });
470
+ return;
471
+ }
472
+ if (typeof slot === "string") {
473
+ card.capabilities.push({ capability });
474
+ card.providers[capability] = slot;
475
+ return;
476
+ }
477
+ card.capabilities.push(
478
+ slot.minVersion ? { capability, min_version: slot.minVersion } : { capability }
479
+ );
480
+ if (slot.provider) {
481
+ card.providers[capability] = slot.provider;
482
+ }
483
+ }
484
+ function lowerSlots(options) {
485
+ const card = {
486
+ ...lowerChatSlot(options.chat),
487
+ capabilities: [],
488
+ providers: {},
489
+ tools: []
490
+ };
491
+ for (const capability of CAPABILITY_SLOTS) {
492
+ lowerCapabilitySlot(capability, options[capability], card);
493
+ }
494
+ for (const t of options.tools ?? []) {
495
+ card.tools.push(typeof t === "string" ? t : t.id);
496
+ }
497
+ return card;
498
+ }
499
+ function defaultRun(card) {
500
+ return async (input, ctx) => {
501
+ const content = typeof input === "string" ? input : JSON.stringify(input ?? "");
502
+ const messages = card.persona ? [
503
+ { role: "system", content: card.persona },
504
+ { role: "user", content }
505
+ ] : [{ role: "user", content }];
506
+ const result = await ctx.gateway.chat(messages);
507
+ return result.content;
508
+ };
509
+ }
510
+ function cardConfig(card) {
511
+ const config = {};
512
+ if (card.model) {
513
+ config.model = card.model;
514
+ }
515
+ if (card.engine) {
516
+ config.engine = card.engine;
517
+ }
518
+ if (card.persona) {
519
+ config.persona = card.persona;
520
+ }
521
+ if (card.modelPrefKey) {
522
+ config.model_pref_key = card.modelPrefKey;
523
+ }
524
+ if (Object.keys(card.providers).length > 0) {
525
+ config.capability_providers = card.providers;
526
+ }
527
+ if (card.tools.length > 0) {
528
+ config.tools = card.tools;
529
+ }
530
+ return config;
531
+ }
532
+ function agentToManifest(agent2, options) {
533
+ const config = cardConfig(agent2.card);
534
+ const meta = {
535
+ id: agent2.id,
536
+ name: agent2.name,
537
+ kind: "agent",
538
+ ...Object.keys(config).length > 0 ? { config } : {}
539
+ };
540
+ const hasRequires = agent2.card.capabilities.length > 0 || (options.grants?.length ?? 0) > 0;
541
+ const raw = {
542
+ id: options.id,
543
+ name: options.name ?? agent2.name,
544
+ version: options.version,
545
+ runnables: [meta],
546
+ ...hasRequires ? {
547
+ requires: {
548
+ apps: [],
549
+ capabilities: agent2.card.capabilities,
550
+ grants: options.grants ?? []
551
+ }
552
+ } : {}
553
+ };
554
+ const result = PluginManifestSchema.safeParse(raw);
555
+ if (!result.success) {
556
+ const first = result.error.issues[0];
557
+ const field = first?.path.join(".") ?? "unknown";
558
+ const message = first?.message ?? "validation failed";
559
+ throw new Error(
560
+ `agent manifest validation failed at '${field}': ${message}`
561
+ );
562
+ }
563
+ return result.data;
564
+ }
565
+ function defineAgent(options) {
566
+ const { id, name } = options;
567
+ const card = lowerSlots(options);
568
+ const run = options.run ?? defaultRun(card);
569
+ return {
570
+ id,
571
+ name,
572
+ kind: "agent",
573
+ run,
574
+ card,
575
+ toManifest(manifestOptions) {
576
+ return agentToManifest(this, manifestOptions);
577
+ }
578
+ };
579
+ }
580
+
581
+ // src/runnable/skill.ts
582
+ function defineSkill(options) {
583
+ const { id, name, run } = options;
584
+ return {
585
+ id,
586
+ name,
587
+ kind: "skill",
588
+ run
589
+ };
590
+ }
591
+
592
+ // src/runnable/tool.ts
593
+ function validateInput(input, schema, toolId) {
594
+ if (typeof input !== "object" || input === null || Array.isArray(input)) {
595
+ throw new Error(
596
+ `[ryu-sdk] Tool "${toolId}" input must be an object, got ${JSON.stringify(input)}`
597
+ );
598
+ }
599
+ const record = input;
600
+ for (const requiredKey of schema.required ?? []) {
601
+ if (!(requiredKey in record)) {
602
+ throw new Error(
603
+ `[ryu-sdk] Tool "${toolId}" input missing required field "${requiredKey}"`
604
+ );
605
+ }
606
+ }
607
+ for (const [key, prop] of Object.entries(schema.properties)) {
608
+ if (!(key in record)) {
609
+ continue;
610
+ }
611
+ const value = record[key];
612
+ if (!checkType(value, prop.type)) {
613
+ throw new Error(
614
+ `[ryu-sdk] Tool "${toolId}" input field "${key}" expected type "${prop.type}", got ${JSON.stringify(value)}`
615
+ );
616
+ }
617
+ }
618
+ }
619
+ function checkType(value, type) {
620
+ switch (type) {
621
+ case "string":
622
+ return typeof value === "string";
623
+ case "number":
624
+ case "integer":
625
+ return typeof value === "number";
626
+ case "boolean":
627
+ return typeof value === "boolean";
628
+ case "array":
629
+ return Array.isArray(value);
630
+ case "object":
631
+ return typeof value === "object" && value !== null && !Array.isArray(value);
632
+ default:
633
+ return false;
634
+ }
635
+ }
636
+ function defineTool(options) {
637
+ const { id, name, schema, run } = options;
638
+ const code = `return await (${run.toString()})(input, host);`;
639
+ return {
640
+ id,
641
+ name,
642
+ kind: "tool",
643
+ schema,
644
+ code,
645
+ run(input, ctx) {
646
+ validateInput(input, schema, id);
647
+ return run(input, ctx);
648
+ }
649
+ };
650
+ }
651
+ function inlineToolRunnable(tool2, options) {
652
+ return {
653
+ id: tool2.id,
654
+ name: tool2.name,
655
+ kind: "tool",
656
+ config: {
657
+ slug: tool2.id,
658
+ backend: "inline_deno",
659
+ code: tool2.code,
660
+ input_schema: tool2.schema,
661
+ ...options?.description ? { description: options.description } : {}
662
+ }
663
+ };
664
+ }
665
+
666
+ // src/runnable/turn-hook.ts
667
+ function defineTurnHook(options) {
668
+ const source = options.run.toString();
669
+ const code = `return await (${source})(ctx, host);`;
670
+ return {
671
+ id: options.id,
672
+ on: options.on ?? "post_assistant_turn",
673
+ code
674
+ };
675
+ }
676
+ function definePlugin(options) {
677
+ const contributes = {
678
+ turn_hooks: options.turnHooks ?? [],
679
+ composer_controls: options.composerControls ?? [],
680
+ settings_tabs: options.settingsTabs ?? [],
681
+ slash_commands: options.slashCommands ?? [],
682
+ // A turn-hook plugin contributes no app widgets; the field is required on the
683
+ // resolved `Contributes` type (zod default applied), so set it explicitly.
684
+ widgets: []
685
+ };
686
+ const tools = options.tools ?? [];
687
+ const runnables = tools.map((t) => inlineToolRunnable(t));
688
+ const grants = new Set(options.grants ?? []);
689
+ if (tools.length > 0) {
690
+ grants.add("tool:execute");
691
+ }
692
+ return {
693
+ id: options.id,
694
+ name: options.name,
695
+ version: options.version,
696
+ runnables,
697
+ permission_grants: [...grants],
698
+ activation_events: options.activationEvents ?? ["*"],
699
+ contributes,
700
+ // Empty = EVERY surface (Core's backward-compatible default), never "hidden".
701
+ targets: options.targets ?? [],
702
+ // Absent (not `{apps:[],grants:[]}`) when undeclared, matching Core's
703
+ // `Option<Requires>` + `skip_serializing_if = "Option::is_none"`.
704
+ ...options.requires ? {
705
+ requires: {
706
+ apps: options.requires.apps ?? [],
707
+ capabilities: options.requires.capabilities ?? [],
708
+ grants: options.requires.grants ?? []
709
+ }
710
+ } : {}
711
+ };
712
+ }
713
+
714
+ // src/runnable/workflow.ts
715
+ function defineWorkflow(options) {
716
+ const { id, name, run } = options;
717
+ return {
718
+ id,
719
+ name,
720
+ kind: "workflow",
721
+ run
722
+ };
723
+ }
724
+ export {
725
+ Agent,
726
+ AgentBuilder,
727
+ AppBuilder,
728
+ AppDependencySchema,
729
+ CapabilityReqSchema,
730
+ CompanionSurfaceSchema,
731
+ DEFAULT_GATEWAY_URL,
732
+ ModelClient,
733
+ PRIMITIVE_BINDINGS,
734
+ PluginBuilder,
735
+ PluginManifestSchema,
736
+ RequiresSchema,
737
+ RunnableKindSchema,
738
+ RunnableMetaSchema,
739
+ SkillBuilder,
740
+ SurfaceSchema,
741
+ ToolAppConfigSchema,
742
+ ToolBuilder,
743
+ WidgetContributionSchema,
744
+ WorkflowBuilder,
745
+ agent,
746
+ app,
747
+ appToolId,
748
+ assertAllowedEgressUrl,
749
+ coreManifestJsonSchema,
750
+ createAgent,
751
+ createPrimitives,
752
+ defineAgent,
753
+ defineApp,
754
+ defineModel,
755
+ definePlugin,
756
+ defineSkill,
757
+ defineTool,
758
+ defineTurnHook,
759
+ defineWorkflow,
760
+ httpPrimitiveTransport,
761
+ inlineToolRunnable,
762
+ query,
763
+ resolveGatewayToken,
764
+ resolveGatewayUrl,
765
+ ryuTool,
766
+ skill,
767
+ tool,
768
+ validateManifestStrict,
769
+ validatePluginId,
770
+ workflow
771
+ };