@perstack/runtime 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,21 +1,22 @@
1
- import { existsSync } from 'fs';
2
- import { readFile, writeFile, mkdir, readdir } from 'fs/promises';
3
- import path from 'path';
4
- import { createId } from '@paralleldrive/cuid2';
5
- import TOML from 'smol-toml';
6
- import { setup, assign, createActor } from 'xstate';
7
- import { z, ZodError } from 'zod';
8
- import { anthropic } from '@ai-sdk/anthropic';
9
- import { google } from '@ai-sdk/google';
10
- import { openai } from '@ai-sdk/openai';
11
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
12
- import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
13
- import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
14
- import { McpError } from '@modelcontextprotocol/sdk/types.js';
15
- import { generateText, jsonSchema } from 'ai';
16
- import { dedent } from 'ts-dedent';
17
-
18
1
  // src/runtime.ts
2
+ import { existsSync } from "fs";
3
+ import { mkdir as mkdir2, readFile as readFile4, writeFile as writeFile2 } from "fs/promises";
4
+ import path from "path";
5
+ import { createId as createId7 } from "@paralleldrive/cuid2";
6
+ import TOML from "smol-toml";
7
+ import { createActor } from "xstate";
8
+
9
+ // src/default-store.ts
10
+ import { mkdir, readFile, readdir, writeFile } from "fs/promises";
11
+
12
+ // src/schemas/runtime.ts
13
+ import { createId } from "@paralleldrive/cuid2";
14
+ import { z as z2 } from "zod";
15
+
16
+ // src/model.ts
17
+ import { anthropic } from "@ai-sdk/anthropic";
18
+ import { google } from "@ai-sdk/google";
19
+ import { openai } from "@ai-sdk/openai";
19
20
  var DefaultModel = "claude-4-sonnet-20250514";
20
21
  var AnthropicModels = [
21
22
  "claude-4-opus-20250514",
@@ -129,6 +130,9 @@ function getModel(modelId) {
129
130
  }
130
131
  throw new Error(`Unsupported model: ${unwrappedModelId}`);
131
132
  }
133
+
134
+ // src/schemas/messages.ts
135
+ import { z } from "zod";
132
136
  var BasePartSchema = z.object({
133
137
  id: z.string()
134
138
  });
@@ -239,38 +243,38 @@ function parseExpertKey(expertKey) {
239
243
  tag
240
244
  };
241
245
  }
242
- var McpStdioSkillSchema = z.object({
243
- type: z.literal("mcpStdioSkill"),
244
- name: z.string(),
245
- description: z.string().optional(),
246
- rule: z.string().optional(),
247
- pick: z.array(z.string()).optional().default([]),
248
- omit: z.array(z.string()).optional().default([]),
249
- command: z.string(),
250
- packageName: z.string().optional(),
251
- args: z.array(z.string()).optional().default([]),
252
- requiredEnv: z.array(z.string()).optional().default([])
246
+ var McpStdioSkillSchema = z2.object({
247
+ type: z2.literal("mcpStdioSkill"),
248
+ name: z2.string(),
249
+ description: z2.string().optional(),
250
+ rule: z2.string().optional(),
251
+ pick: z2.array(z2.string()).optional().default([]),
252
+ omit: z2.array(z2.string()).optional().default([]),
253
+ command: z2.string(),
254
+ packageName: z2.string().optional(),
255
+ args: z2.array(z2.string()).optional().default([]),
256
+ requiredEnv: z2.array(z2.string()).optional().default([])
253
257
  });
254
- var McpSseSkillSchema = z.object({
255
- type: z.literal("mcpSseSkill"),
256
- name: z.string(),
257
- description: z.string().optional(),
258
- rule: z.string().optional(),
259
- pick: z.array(z.string()).optional().default([]),
260
- omit: z.array(z.string()).optional().default([]),
261
- endpoint: z.string()
258
+ var McpSseSkillSchema = z2.object({
259
+ type: z2.literal("mcpSseSkill"),
260
+ name: z2.string(),
261
+ description: z2.string().optional(),
262
+ rule: z2.string().optional(),
263
+ pick: z2.array(z2.string()).optional().default([]),
264
+ omit: z2.array(z2.string()).optional().default([]),
265
+ endpoint: z2.string()
262
266
  });
263
- var InteractiveToolSchema = z.object({
264
- name: z.string(),
265
- description: z.string().optional(),
266
- inputJsonSchema: z.string()
267
+ var InteractiveToolSchema = z2.object({
268
+ name: z2.string(),
269
+ description: z2.string().optional(),
270
+ inputJsonSchema: z2.string()
267
271
  });
268
- var InteractiveSkillSchema = z.object({
269
- type: z.literal("interactiveSkill"),
270
- name: z.string(),
271
- description: z.string().optional(),
272
- rule: z.string().optional(),
273
- tools: z.record(z.string(), InteractiveToolSchema.omit({ name: true })).transform((tools) => {
272
+ var InteractiveSkillSchema = z2.object({
273
+ type: z2.literal("interactiveSkill"),
274
+ name: z2.string(),
275
+ description: z2.string().optional(),
276
+ rule: z2.string().optional(),
277
+ tools: z2.record(z2.string(), InteractiveToolSchema.omit({ name: true })).transform((tools) => {
274
278
  return Object.fromEntries(
275
279
  Object.entries(tools).map(([key, toolWithoutName]) => [
276
280
  key,
@@ -279,15 +283,15 @@ var InteractiveSkillSchema = z.object({
279
283
  );
280
284
  })
281
285
  });
282
- var ExpertSchema = z.object({
283
- key: z.string().regex(expertKeyRegex).min(1),
284
- name: z.string().regex(expertNameRegex).min(1).max(maxNameLength),
285
- version: z.string().regex(versionRegex),
286
- description: z.string().min(1).max(1024 * 2).optional(),
287
- instruction: z.string().min(1).max(1024 * 20),
288
- skills: z.record(
289
- z.string(),
290
- z.discriminatedUnion("type", [
286
+ var ExpertSchema = z2.object({
287
+ key: z2.string().regex(expertKeyRegex).min(1),
288
+ name: z2.string().regex(expertNameRegex).min(1).max(maxNameLength),
289
+ version: z2.string().regex(versionRegex),
290
+ description: z2.string().min(1).max(1024 * 2).optional(),
291
+ instruction: z2.string().min(1).max(1024 * 20),
292
+ skills: z2.record(
293
+ z2.string(),
294
+ z2.discriminatedUnion("type", [
291
295
  McpStdioSkillSchema.omit({ name: true }),
292
296
  McpSseSkillSchema.omit({ name: true }),
293
297
  InteractiveSkillSchema.omit({ name: true })
@@ -303,7 +307,7 @@ var ExpertSchema = z.object({
303
307
  return Object.fromEntries(
304
308
  Object.entries(skills).map(([key, skillWithoutName]) => [
305
309
  key,
306
- z.discriminatedUnion("type", [
310
+ z2.discriminatedUnion("type", [
307
311
  McpStdioSkillSchema,
308
312
  McpSseSkillSchema,
309
313
  InteractiveSkillSchema
@@ -311,17 +315,17 @@ var ExpertSchema = z.object({
311
315
  ])
312
316
  );
313
317
  }),
314
- delegates: z.array(z.string().regex(expertKeyRegex).min(1)).optional().default([]),
315
- tags: z.array(z.string().regex(tagNameRegex).min(1)).optional().default([])
318
+ delegates: z2.array(z2.string().regex(expertKeyRegex).min(1)).optional().default([]),
319
+ tags: z2.array(z2.string().regex(tagNameRegex).min(1)).optional().default([])
316
320
  });
317
- var UsageSchema = z.object({
318
- promptTokens: z.number(),
319
- completionTokens: z.number(),
320
- totalTokens: z.number(),
321
- cacheCreationInputTokens: z.number(),
322
- cacheReadInputTokens: z.number()
321
+ var UsageSchema = z2.object({
322
+ promptTokens: z2.number(),
323
+ completionTokens: z2.number(),
324
+ totalTokens: z2.number(),
325
+ cacheCreationInputTokens: z2.number(),
326
+ cacheReadInputTokens: z2.number()
323
327
  });
324
- var CheckpointStatusSchema = z.enum([
328
+ var CheckpointStatusSchema = z2.enum([
325
329
  "init",
326
330
  "proceeding",
327
331
  "completed",
@@ -330,52 +334,52 @@ var CheckpointStatusSchema = z.enum([
330
334
  "stoppedByExceededMaxSteps",
331
335
  "stoppedByError"
332
336
  ]);
333
- var CheckpointSchema = z.object({
334
- id: z.string(),
335
- runId: z.string(),
337
+ var CheckpointSchema = z2.object({
338
+ id: z2.string(),
339
+ runId: z2.string(),
336
340
  status: CheckpointStatusSchema,
337
- stepNumber: z.number(),
338
- messages: z.array(MessageSchema),
339
- expert: z.object({
340
- key: z.string(),
341
- name: z.string(),
342
- version: z.string()
341
+ stepNumber: z2.number(),
342
+ messages: z2.array(MessageSchema),
343
+ expert: z2.object({
344
+ key: z2.string(),
345
+ name: z2.string(),
346
+ version: z2.string()
343
347
  }),
344
- delegateTo: z.object({
345
- expert: z.object({
346
- key: z.string(),
347
- name: z.string(),
348
- version: z.string()
348
+ delegateTo: z2.object({
349
+ expert: z2.object({
350
+ key: z2.string(),
351
+ name: z2.string(),
352
+ version: z2.string()
349
353
  }),
350
- toolCallId: z.string(),
351
- toolName: z.string(),
352
- query: z.string()
354
+ toolCallId: z2.string(),
355
+ toolName: z2.string(),
356
+ query: z2.string()
353
357
  }).optional(),
354
- delegatedBy: z.object({
355
- expert: z.object({
356
- key: z.string(),
357
- name: z.string(),
358
- version: z.string()
358
+ delegatedBy: z2.object({
359
+ expert: z2.object({
360
+ key: z2.string(),
361
+ name: z2.string(),
362
+ version: z2.string()
359
363
  }),
360
- toolCallId: z.string(),
361
- toolName: z.string(),
362
- checkpointId: z.string()
364
+ toolCallId: z2.string(),
365
+ toolName: z2.string(),
366
+ checkpointId: z2.string()
363
367
  }).optional(),
364
368
  usage: UsageSchema
365
369
  });
366
- var RunParamsSchema = z.object({
367
- setting: z.object({
368
- runId: z.string().optional().transform((value) => value ?? createId()),
369
- expertKey: z.string().regex(expertKeyRegex).min(1),
370
- input: z.object({
371
- text: z.string().optional(),
372
- interactiveToolCallResult: z.object({
373
- toolCallId: z.string(),
374
- toolName: z.string(),
375
- text: z.string()
370
+ var RunParamsSchema = z2.object({
371
+ setting: z2.object({
372
+ runId: z2.string().optional().transform((value) => value ?? createId()),
373
+ expertKey: z2.string().regex(expertKeyRegex).min(1),
374
+ input: z2.object({
375
+ text: z2.string().optional(),
376
+ interactiveToolCallResult: z2.object({
377
+ toolCallId: z2.string(),
378
+ toolName: z2.string(),
379
+ text: z2.string()
376
380
  }).optional()
377
381
  }),
378
- experts: z.record(z.string().regex(expertKeyRegex).min(1), ExpertSchema.omit({ key: true })).optional().default({}).transform(
382
+ experts: z2.record(z2.string().regex(expertKeyRegex).min(1), ExpertSchema.omit({ key: true })).optional().default({}).transform(
379
383
  (experts) => Object.fromEntries(
380
384
  Object.entries(experts).map(([key, expertWithoutKey]) => [
381
385
  key,
@@ -386,14 +390,14 @@ var RunParamsSchema = z.object({
386
390
  ])
387
391
  )
388
392
  ),
389
- model: z.string().optional().default(getDefaultModelName()),
390
- temperature: z.number().min(0).max(1).optional().default(0.3),
391
- maxSteps: z.number().min(1).optional(),
392
- maxRetries: z.number().min(0).optional().default(10),
393
+ model: z2.string().optional().default(getDefaultModelName()),
394
+ temperature: z2.number().min(0).max(1).optional().default(0.3),
395
+ maxSteps: z2.number().min(1).optional(),
396
+ maxRetries: z2.number().min(0).optional().default(10),
393
397
  /** Optional workspace directory path. If provided, sets WORKSPACE_DIR environment variable for skill execution. */
394
- workspace: z.string().optional(),
395
- startedAt: z.number().optional().default(Date.now()),
396
- updatedAt: z.number().optional().default(Date.now())
398
+ workspace: z2.string().optional(),
399
+ startedAt: z2.number().optional().default(Date.now()),
400
+ updatedAt: z2.number().optional().default(Date.now())
397
401
  }),
398
402
  checkpoint: CheckpointSchema.optional()
399
403
  });
@@ -459,7 +463,53 @@ async function defaultStoreEvent(event) {
459
463
 
460
464
  // package.json
461
465
  var package_default = {
462
- version: "0.0.10"};
466
+ name: "@perstack/runtime",
467
+ version: "0.0.11",
468
+ description: "PerStack Runtime",
469
+ author: "Wintermute Technologies, Inc.",
470
+ license: "Apache-2.0",
471
+ type: "module",
472
+ exports: {
473
+ ".": "./src/index.ts"
474
+ },
475
+ publishConfig: {
476
+ exports: {
477
+ ".": "./dist/index.js"
478
+ },
479
+ types: "./dist/index.d.ts"
480
+ },
481
+ files: [
482
+ "dist"
483
+ ],
484
+ scripts: {
485
+ clean: "rm -rf dist",
486
+ dev: "tsup --watch --config ../../tsup.config.ts",
487
+ build: "npm run clean && tsup --config ../../tsup.config.ts",
488
+ prepublishOnly: "npm run clean && npm run build"
489
+ },
490
+ dependencies: {
491
+ "@ai-sdk/anthropic": "^1.2.12",
492
+ "@ai-sdk/google": "^1.2.19",
493
+ "@ai-sdk/openai": "^1.3.22",
494
+ "@modelcontextprotocol/sdk": "^1.12.3",
495
+ "@paralleldrive/cuid2": "^2.2.2",
496
+ ai: "^4.3.16",
497
+ "smol-toml": "^1.3.4",
498
+ "ts-dedent": "^2.2.0",
499
+ xstate: "^5.19.4",
500
+ zod: "^3.25.67"
501
+ },
502
+ devDependencies: {
503
+ "@tsconfig/node22": "^22.0.2",
504
+ "@types/node": "^24.0.3",
505
+ tsup: "^8.5.0",
506
+ typescript: "^5.8.3",
507
+ vitest: "^3.2.3"
508
+ },
509
+ engines: {
510
+ node: ">=22.0.0"
511
+ }
512
+ };
463
513
 
464
514
  // src/events/default-event-listener.ts
465
515
  var log = console.info;
@@ -661,6 +711,9 @@ function logUsage(e) {
661
711
  ].join(", ");
662
712
  log(`${header(e)} \u{1F4CA} Total usage: ${usageByRun}`);
663
713
  }
714
+
715
+ // src/events/event-emitter.ts
716
+ import { createId as createId2 } from "@paralleldrive/cuid2";
664
717
  var RunEventEmitter = class {
665
718
  listeners = [];
666
719
  subscribe(listener) {
@@ -670,35 +723,38 @@ var RunEventEmitter = class {
670
723
  for (const listener of this.listeners) {
671
724
  await listener({
672
725
  ...event,
673
- id: createId(),
726
+ id: createId2(),
674
727
  timestamp: Date.now()
675
728
  });
676
729
  }
677
730
  }
678
731
  };
679
- var RegistryV1ExpertSchema = z.object({
680
- name: z.string(),
681
- version: z.string(),
682
- minRuntimeVersion: z.string(),
683
- description: z.string(),
684
- instruction: z.string(),
685
- skills: z.record(
686
- z.string(),
687
- z.discriminatedUnion("type", [
732
+
733
+ // src/schemas/api-registry.ts
734
+ import { z as z3 } from "zod";
735
+ var RegistryV1ExpertSchema = z3.object({
736
+ name: z3.string(),
737
+ version: z3.string(),
738
+ minRuntimeVersion: z3.string(),
739
+ description: z3.string(),
740
+ instruction: z3.string(),
741
+ skills: z3.record(
742
+ z3.string(),
743
+ z3.discriminatedUnion("type", [
688
744
  McpStdioSkillSchema.omit({ name: true }),
689
745
  McpSseSkillSchema.omit({ name: true }),
690
746
  InteractiveSkillSchema.omit({ name: true })
691
747
  ])
692
748
  ),
693
- delegates: z.array(z.string()),
694
- tags: z.array(z.string()),
695
- status: z.string(),
696
- owner: z.object({ name: z.string() }),
697
- createdAt: z.string().datetime(),
698
- updatedAt: z.string().datetime()
749
+ delegates: z3.array(z3.string()),
750
+ tags: z3.array(z3.string()),
751
+ status: z3.string(),
752
+ owner: z3.object({ name: z3.string() }),
753
+ createdAt: z3.string().datetime(),
754
+ updatedAt: z3.string().datetime()
699
755
  });
700
- var RegistryV1ExpertsGetResponseSchema = z.object({
701
- data: z.object({
756
+ var RegistryV1ExpertsGetResponseSchema = z3.object({
757
+ data: z3.object({
702
758
  expert: RegistryV1ExpertSchema
703
759
  })
704
760
  });
@@ -753,6 +809,18 @@ async function resolveExpertToRun(expertKey, experts) {
753
809
  experts[expertKey] = expert;
754
810
  return expert;
755
811
  }
812
+
813
+ // src/runtime-state-machine.ts
814
+ import { assign, setup } from "xstate";
815
+
816
+ // src/skill-manager.ts
817
+ import { Client as McpClient } from "@modelcontextprotocol/sdk/client/index.js";
818
+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
819
+ import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
820
+ import { McpError } from "@modelcontextprotocol/sdk/types.js";
821
+ import { createId as createId3 } from "@paralleldrive/cuid2";
822
+ import { jsonSchema } from "ai";
823
+ import { ZodError } from "zod";
756
824
  var SkillManager = class {
757
825
  _toolDefinitions = [];
758
826
  _initialized = false;
@@ -798,7 +866,7 @@ var SkillManager = class {
798
866
  }
799
867
  }
800
868
  async _initMcpSkill(params) {
801
- this._mcpClient = new Client({
869
+ this._mcpClient = new McpClient({
802
870
  name: `${params.skill.name}-mcp-client`,
803
871
  version: "1.0.0"
804
872
  });
@@ -926,12 +994,12 @@ var SkillManager = class {
926
994
  {
927
995
  type: "textPart",
928
996
  text: `Error calling tool ${toolName}: ${error.message}`,
929
- id: createId()
997
+ id: createId3()
930
998
  }
931
999
  ];
932
1000
  }
933
1001
  if (error instanceof ZodError) {
934
- return [{ type: "textPart", text: `Invalid tool call arguments: ${error}`, id: createId() }];
1002
+ return [{ type: "textPart", text: `Invalid tool call arguments: ${error}`, id: createId3() }];
935
1003
  }
936
1004
  throw error;
937
1005
  }
@@ -941,7 +1009,7 @@ var SkillManager = class {
941
1009
  {
942
1010
  type: "textPart",
943
1011
  text: `Tool ${toolName} returned nothing with arguments: ${JSON.stringify(input)}`,
944
- id: createId()
1012
+ id: createId3()
945
1013
  }
946
1014
  ];
947
1015
  }
@@ -951,9 +1019,9 @@ var SkillManager = class {
951
1019
  switch (part.type) {
952
1020
  case "text":
953
1021
  if (!part.text || part.text === "") {
954
- return { type: "textPart", text: "Error: No content", id: createId() };
1022
+ return { type: "textPart", text: "Error: No content", id: createId3() };
955
1023
  }
956
- return { type: "textPart", text: part.text, id: createId() };
1024
+ return { type: "textPart", text: part.text, id: createId3() };
957
1025
  case "image":
958
1026
  if (!part.data || !part.mimeType) {
959
1027
  throw new Error("Image part must have both data and mimeType");
@@ -962,7 +1030,7 @@ var SkillManager = class {
962
1030
  type: "imageInlinePart",
963
1031
  encodedData: part.data,
964
1032
  mimeType: part.mimeType,
965
- id: createId()
1033
+ id: createId3()
966
1034
  };
967
1035
  case "resource":
968
1036
  if (!part.resource) {
@@ -976,14 +1044,14 @@ var SkillManager = class {
976
1044
  throw new Error(`Resource ${JSON.stringify(resource)} has no mimeType`);
977
1045
  }
978
1046
  if (resource.text && typeof resource.text === "string") {
979
- return { type: "textPart", text: resource.text, id: createId() };
1047
+ return { type: "textPart", text: resource.text, id: createId3() };
980
1048
  }
981
1049
  if (resource.blob && typeof resource.blob === "string") {
982
1050
  return {
983
1051
  type: "fileInlinePart",
984
1052
  encodedData: resource.blob,
985
1053
  mimeType: resource.mimeType,
986
- id: createId()
1054
+ id: createId3()
987
1055
  };
988
1056
  }
989
1057
  throw new Error(`Unsupported resource type: ${JSON.stringify(resource)}`);
@@ -1167,6 +1235,9 @@ async function callingToolLogic({
1167
1235
  }
1168
1236
  return resolveToolResult(setting, checkpoint, { toolResult });
1169
1237
  }
1238
+
1239
+ // src/states/finishing-step.ts
1240
+ import { createId as createId4 } from "@paralleldrive/cuid2";
1170
1241
  async function finishingStepLogic({
1171
1242
  setting,
1172
1243
  checkpoint,
@@ -1194,19 +1265,26 @@ async function finishingStepLogic({
1194
1265
  },
1195
1266
  nextCheckpoint: {
1196
1267
  ...checkpoint,
1197
- id: createId(),
1268
+ id: createId4(),
1198
1269
  stepNumber: checkpoint.stepNumber + 1
1199
1270
  }
1200
1271
  });
1201
1272
  }
1273
+
1274
+ // src/states/generating-run-result.ts
1275
+ import { generateText } from "ai";
1276
+
1277
+ // src/messages/message.ts
1278
+ import { createId as createId5 } from "@paralleldrive/cuid2";
1279
+ import { dedent } from "ts-dedent";
1202
1280
  function createUserMessage(contents) {
1203
1281
  return {
1204
1282
  type: "userMessage",
1205
1283
  contents: contents.map((part) => ({
1206
1284
  ...part,
1207
- id: createId()
1285
+ id: createId5()
1208
1286
  })),
1209
- id: createId()
1287
+ id: createId5()
1210
1288
  };
1211
1289
  }
1212
1290
  function createExpertMessage(contents) {
@@ -1214,9 +1292,9 @@ function createExpertMessage(contents) {
1214
1292
  type: "expertMessage",
1215
1293
  contents: contents.map((part) => ({
1216
1294
  ...part,
1217
- id: createId()
1295
+ id: createId5()
1218
1296
  })),
1219
- id: createId()
1297
+ id: createId5()
1220
1298
  };
1221
1299
  }
1222
1300
  function createToolMessage(contents) {
@@ -1226,11 +1304,11 @@ function createToolMessage(contents) {
1226
1304
  ...part,
1227
1305
  contents: part.contents.map((part2) => ({
1228
1306
  ...part2,
1229
- id: createId()
1307
+ id: createId5()
1230
1308
  })),
1231
- id: createId()
1309
+ id: createId5()
1232
1310
  })),
1233
- id: createId()
1311
+ id: createId5()
1234
1312
  };
1235
1313
  }
1236
1314
  function messageToCoreMessage(message) {
@@ -1481,6 +1559,9 @@ async function generatingRunResultLogic({
1481
1559
  usage
1482
1560
  });
1483
1561
  }
1562
+
1563
+ // src/states/generating-tool-call.ts
1564
+ import { generateText as generateText2 } from "ai";
1484
1565
  async function generatingToolCallLogic({
1485
1566
  setting,
1486
1567
  checkpoint,
@@ -1488,7 +1569,7 @@ async function generatingToolCallLogic({
1488
1569
  }) {
1489
1570
  const { messages } = checkpoint;
1490
1571
  const model = getModel(setting.model);
1491
- const result = await generateText({
1572
+ const result = await generateText2({
1492
1573
  model,
1493
1574
  messages: messages.map(messageToCoreMessage),
1494
1575
  temperature: setting.temperature,
@@ -1576,7 +1657,11 @@ async function generatingToolCallLogic({
1576
1657
  return callDelegate(setting, checkpoint, eventPayload);
1577
1658
  }
1578
1659
  }
1579
- var metaInstruction = dedent`
1660
+
1661
+ // src/messages/instruction-message.ts
1662
+ import { createId as createId6 } from "@paralleldrive/cuid2";
1663
+ import { dedent as dedent2 } from "ts-dedent";
1664
+ var metaInstruction = dedent2`
1580
1665
  IMPORTANT:
1581
1666
  You are NOT an "interactive" AI agent.
1582
1667
  From the start of the agent loop until the completion of the task,
@@ -1620,7 +1705,7 @@ var metaInstruction = dedent`
1620
1705
  - Current working directory is ${process.cwd()}
1621
1706
  `;
1622
1707
  function createInstructionMessage(expert, experts) {
1623
- const instruction = dedent`
1708
+ const instruction = dedent2`
1624
1709
  You are Perstack, an AI expert that tackles tasks requested by users by utilizing all available tools.
1625
1710
 
1626
1711
  (The following information describes your nature and role as an AI, the mechanisms of the AI system, and other meta-cognitive aspects.)
@@ -1648,12 +1733,12 @@ function createInstructionMessage(expert, experts) {
1648
1733
  type: "instructionMessage",
1649
1734
  contents: [
1650
1735
  {
1651
- id: createId(),
1736
+ id: createId6(),
1652
1737
  type: "textPart",
1653
1738
  text: instruction
1654
1739
  }
1655
1740
  ],
1656
- id: createId(),
1741
+ id: createId6(),
1657
1742
  cache: true
1658
1743
  };
1659
1744
  }
@@ -1662,7 +1747,7 @@ function getSkillRules(expert) {
1662
1747
  if (!skill.rule) {
1663
1748
  return acc;
1664
1749
  }
1665
- return dedent`
1750
+ return dedent2`
1666
1751
  ${acc}
1667
1752
 
1668
1753
  "${skill.name}" skill rules:
@@ -1676,7 +1761,7 @@ function getDelegateRules(expert, experts) {
1676
1761
  if (!delegate) {
1677
1762
  return acc;
1678
1763
  }
1679
- return dedent`
1764
+ return dedent2`
1680
1765
  ${acc}
1681
1766
 
1682
1767
  About "${delegate.name}":
@@ -1731,6 +1816,9 @@ async function preparingForStepLogic({
1731
1816
  messages: checkpoint.messages
1732
1817
  });
1733
1818
  }
1819
+
1820
+ // src/states/resolving-image-file.ts
1821
+ import { readFile as readFile2 } from "fs/promises";
1734
1822
  async function resolvingImageFileLogic({
1735
1823
  setting,
1736
1824
  checkpoint,
@@ -1755,7 +1843,7 @@ async function resolvingImageFileLogic({
1755
1843
  continue;
1756
1844
  }
1757
1845
  const { path: path2, mimeType, size } = imageInfo;
1758
- const file = await readFile(path2).then((buffer) => ({
1846
+ const file = await readFile2(path2).then((buffer) => ({
1759
1847
  encodedData: buffer.toString("base64"),
1760
1848
  mimeType,
1761
1849
  size
@@ -1779,6 +1867,9 @@ async function resolvingImageFileLogic({
1779
1867
  ]
1780
1868
  });
1781
1869
  }
1870
+
1871
+ // src/states/resolving-pdf-file.ts
1872
+ import { readFile as readFile3 } from "fs/promises";
1782
1873
  async function resolvingPdfFileLogic({
1783
1874
  setting,
1784
1875
  checkpoint,
@@ -1803,7 +1894,7 @@ async function resolvingPdfFileLogic({
1803
1894
  continue;
1804
1895
  }
1805
1896
  const { path: path2, mimeType, size } = pdfInfo;
1806
- const file = await readFile(path2).then((buffer) => ({
1897
+ const file = await readFile3(path2).then((buffer) => ({
1807
1898
  encodedData: buffer.toString("base64"),
1808
1899
  mimeType,
1809
1900
  size
@@ -2168,55 +2259,58 @@ var StateMachineLogics = {
2168
2259
  CallingDelegate: callingDelegateLogic,
2169
2260
  FinishingStep: finishingStepLogic
2170
2261
  };
2171
- var PerstackConfigSchema = z.object({
2172
- model: z.string().optional(),
2173
- temperature: z.number().optional(),
2174
- maxSteps: z.number().optional(),
2175
- maxRetries: z.number().optional(),
2176
- experts: z.record(
2177
- z.string(),
2178
- z.object({
2179
- version: z.string().optional(),
2180
- minRuntimeVersion: z.string().optional(),
2181
- description: z.string().optional(),
2182
- instruction: z.string(),
2183
- skills: z.record(
2184
- z.string(),
2185
- z.discriminatedUnion("type", [
2186
- z.object({
2187
- type: z.literal("mcpStdioSkill"),
2188
- description: z.string().optional(),
2189
- rule: z.string().optional(),
2190
- pick: z.array(z.string()).optional(),
2191
- omit: z.array(z.string()).optional(),
2192
- command: z.string(),
2193
- packageName: z.string().optional(),
2194
- args: z.array(z.string()).optional(),
2195
- requiredEnv: z.array(z.string()).optional()
2262
+
2263
+ // src/schemas/perstack-toml.ts
2264
+ import { z as z4 } from "zod";
2265
+ var PerstackConfigSchema = z4.object({
2266
+ model: z4.string().optional(),
2267
+ temperature: z4.number().optional(),
2268
+ maxSteps: z4.number().optional(),
2269
+ maxRetries: z4.number().optional(),
2270
+ experts: z4.record(
2271
+ z4.string(),
2272
+ z4.object({
2273
+ version: z4.string().optional(),
2274
+ minRuntimeVersion: z4.string().optional(),
2275
+ description: z4.string().optional(),
2276
+ instruction: z4.string(),
2277
+ skills: z4.record(
2278
+ z4.string(),
2279
+ z4.discriminatedUnion("type", [
2280
+ z4.object({
2281
+ type: z4.literal("mcpStdioSkill"),
2282
+ description: z4.string().optional(),
2283
+ rule: z4.string().optional(),
2284
+ pick: z4.array(z4.string()).optional(),
2285
+ omit: z4.array(z4.string()).optional(),
2286
+ command: z4.string(),
2287
+ packageName: z4.string().optional(),
2288
+ args: z4.array(z4.string()).optional(),
2289
+ requiredEnv: z4.array(z4.string()).optional()
2196
2290
  }),
2197
- z.object({
2198
- type: z.literal("mcpSseSkill"),
2199
- description: z.string().optional(),
2200
- rule: z.string().optional(),
2201
- pick: z.array(z.string()).optional(),
2202
- omit: z.array(z.string()).optional(),
2203
- endpoint: z.string()
2291
+ z4.object({
2292
+ type: z4.literal("mcpSseSkill"),
2293
+ description: z4.string().optional(),
2294
+ rule: z4.string().optional(),
2295
+ pick: z4.array(z4.string()).optional(),
2296
+ omit: z4.array(z4.string()).optional(),
2297
+ endpoint: z4.string()
2204
2298
  }),
2205
- z.object({
2206
- type: z.literal("interactiveSkill"),
2207
- description: z.string().optional(),
2208
- rule: z.string().optional(),
2209
- tools: z.record(
2210
- z.string(),
2211
- z.object({
2212
- description: z.string().optional(),
2213
- inputJsonSchema: z.string()
2299
+ z4.object({
2300
+ type: z4.literal("interactiveSkill"),
2301
+ description: z4.string().optional(),
2302
+ rule: z4.string().optional(),
2303
+ tools: z4.record(
2304
+ z4.string(),
2305
+ z4.object({
2306
+ description: z4.string().optional(),
2307
+ inputJsonSchema: z4.string()
2214
2308
  })
2215
2309
  )
2216
2310
  })
2217
2311
  ])
2218
2312
  ).optional(),
2219
- delegates: z.array(z.string()).optional()
2313
+ delegates: z4.array(z4.string()).optional()
2220
2314
  })
2221
2315
  ).optional()
2222
2316
  });
@@ -2248,7 +2342,7 @@ async function run(runInput, options) {
2248
2342
  experts
2249
2343
  },
2250
2344
  initialCheckpoint: checkpoint ?? {
2251
- id: createId(),
2345
+ id: createId7(),
2252
2346
  runId: setting.runId,
2253
2347
  expert: {
2254
2348
  key: setting.expertKey,
@@ -2316,7 +2410,7 @@ async function run(runInput, options) {
2316
2410
  };
2317
2411
  checkpoint = {
2318
2412
  ...await retrieveCheckpoint(setting.runId, checkpointId),
2319
- id: createId(),
2413
+ id: createId7(),
2320
2414
  stepNumber: runResultCheckpoint.stepNumber + 1,
2321
2415
  usage: runResultCheckpoint.usage
2322
2416
  };
@@ -2340,7 +2434,7 @@ async function run(runInput, options) {
2340
2434
  }
2341
2435
  };
2342
2436
  checkpoint = {
2343
- id: createId(),
2437
+ id: createId7(),
2344
2438
  runId: setting.runId,
2345
2439
  status: "init",
2346
2440
  stepNumber: runResultCheckpoint.stepNumber + 1,
@@ -2408,12 +2502,12 @@ async function storeRunSetting(setting) {
2408
2502
  const runDir = getRunDir(setting.runId);
2409
2503
  if (existsSync(runDir)) {
2410
2504
  const runSettingPath = path.resolve(runDir, "run-setting.json");
2411
- const runSetting = JSON.parse(await readFile(runSettingPath, "utf-8"));
2505
+ const runSetting = JSON.parse(await readFile4(runSettingPath, "utf-8"));
2412
2506
  runSetting.updatedAt = Date.now();
2413
- await writeFile(runSettingPath, JSON.stringify(runSetting, null, 2), "utf-8");
2507
+ await writeFile2(runSettingPath, JSON.stringify(runSetting, null, 2), "utf-8");
2414
2508
  } else {
2415
- await mkdir(runDir, { recursive: true });
2416
- await writeFile(
2509
+ await mkdir2(runDir, { recursive: true });
2510
+ await writeFile2(
2417
2511
  path.resolve(runDir, "run-setting.json"),
2418
2512
  JSON.stringify(setting, null, 2),
2419
2513
  "utf-8"
@@ -2427,13 +2521,16 @@ async function parseConfig(config) {
2427
2521
  const toml = TOML.parse(config ?? "");
2428
2522
  return PerstackConfigSchema.parse(toml);
2429
2523
  }
2430
- var RunInputSchema = z.object({
2431
- expertKey: z.string(),
2432
- query: z.string(),
2433
- options: z.object({
2434
- config: z.string().optional(),
2435
- model: z.string().optional(),
2436
- temperature: z.string().optional().transform((value) => {
2524
+
2525
+ // src/schemas/command-run.ts
2526
+ import { z as z5 } from "zod";
2527
+ var RunInputSchema = z5.object({
2528
+ expertKey: z5.string(),
2529
+ query: z5.string(),
2530
+ options: z5.object({
2531
+ config: z5.string().optional(),
2532
+ model: z5.string().optional(),
2533
+ temperature: z5.string().optional().transform((value) => {
2437
2534
  if (value === void 0) {
2438
2535
  return void 0;
2439
2536
  }
@@ -2443,7 +2540,7 @@ var RunInputSchema = z.object({
2443
2540
  }
2444
2541
  return parsedValue;
2445
2542
  }),
2446
- maxSteps: z.string().optional().transform((value) => {
2543
+ maxSteps: z5.string().optional().transform((value) => {
2447
2544
  if (value === void 0) {
2448
2545
  return void 0;
2449
2546
  }
@@ -2453,7 +2550,7 @@ var RunInputSchema = z.object({
2453
2550
  }
2454
2551
  return parsedValue;
2455
2552
  }),
2456
- maxRetries: z.string().optional().transform((value) => {
2553
+ maxRetries: z5.string().optional().transform((value) => {
2457
2554
  if (value === void 0) {
2458
2555
  return void 0;
2459
2556
  }
@@ -2463,10 +2560,66 @@ var RunInputSchema = z.object({
2463
2560
  }
2464
2561
  return parsedValue;
2465
2562
  }),
2466
- runId: z.string().optional()
2563
+ runId: z5.string().optional()
2467
2564
  })
2468
2565
  });
2469
-
2470
- export { CheckpointSchema, CheckpointStatusSchema, ExpertMessageSchema, ExpertSchema, FileBinaryPartSchema, FileInlinePartSchema, FileUrlPartSchema, ImageBinaryPartSchema, ImageInlinePartSchema, ImageUrlPartSchema, InstructionMessageSchema, InteractiveSkillSchema, InteractiveToolSchema, McpSseSkillSchema, McpStdioSkillSchema, MessageSchema, PerstackConfigSchema, RegistryV1ExpertsGetResponseSchema, RunInputSchema, RunParamsSchema, StateMachineLogics, TextPartSchema, ToolCallPartSchema, ToolMessageSchema, ToolResultPartSchema, UsageSchema, UserMessageSchema, attemptCompletion, callDelegate, callInteractiveTool, callTool, completeRun, continueToNextStep, createEvent, defaultEventListener, expertKeyRegex, expertNameRegex, finishToolCall, getRunDir, maxNameLength, parseConfig, parseExpertKey, resolveImageFile, resolvePdfFile, resolveThought, resolveToolResult, retry, run, runtimeStateMachine, skillNameRegex, startGeneration, startRun, stopRunByDelegate, stopRunByExceededMaxSteps, stopRunByInteractiveTool, tagNameRegex, versionRegex };
2471
- //# sourceMappingURL=index.js.map
2566
+ export {
2567
+ CheckpointSchema,
2568
+ CheckpointStatusSchema,
2569
+ ExpertMessageSchema,
2570
+ ExpertSchema,
2571
+ FileBinaryPartSchema,
2572
+ FileInlinePartSchema,
2573
+ FileUrlPartSchema,
2574
+ ImageBinaryPartSchema,
2575
+ ImageInlinePartSchema,
2576
+ ImageUrlPartSchema,
2577
+ InstructionMessageSchema,
2578
+ InteractiveSkillSchema,
2579
+ InteractiveToolSchema,
2580
+ McpSseSkillSchema,
2581
+ McpStdioSkillSchema,
2582
+ MessageSchema,
2583
+ PerstackConfigSchema,
2584
+ RegistryV1ExpertsGetResponseSchema,
2585
+ RunInputSchema,
2586
+ RunParamsSchema,
2587
+ StateMachineLogics,
2588
+ TextPartSchema,
2589
+ ToolCallPartSchema,
2590
+ ToolMessageSchema,
2591
+ ToolResultPartSchema,
2592
+ UsageSchema,
2593
+ UserMessageSchema,
2594
+ attemptCompletion,
2595
+ callDelegate,
2596
+ callInteractiveTool,
2597
+ callTool,
2598
+ completeRun,
2599
+ continueToNextStep,
2600
+ createEvent,
2601
+ defaultEventListener,
2602
+ expertKeyRegex,
2603
+ expertNameRegex,
2604
+ finishToolCall,
2605
+ getRunDir,
2606
+ maxNameLength,
2607
+ parseConfig,
2608
+ parseExpertKey,
2609
+ resolveImageFile,
2610
+ resolvePdfFile,
2611
+ resolveThought,
2612
+ resolveToolResult,
2613
+ retry,
2614
+ run,
2615
+ runtimeStateMachine,
2616
+ skillNameRegex,
2617
+ startGeneration,
2618
+ startRun,
2619
+ stopRunByDelegate,
2620
+ stopRunByExceededMaxSteps,
2621
+ stopRunByInteractiveTool,
2622
+ tagNameRegex,
2623
+ versionRegex
2624
+ };
2472
2625
  //# sourceMappingURL=index.js.map