@perstack/runtime 0.0.10 → 0.0.12

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.12",
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,
@@ -1496,6 +1577,7 @@ async function generatingToolCallLogic({
1496
1577
  tools: await getToolSet(skillManagers),
1497
1578
  toolChoice: "required"
1498
1579
  });
1580
+ console.error(result);
1499
1581
  const usage = usageFromGenerateTextResult(result);
1500
1582
  const { text, toolCalls, finishReason } = result;
1501
1583
  const toolCall = toolCalls[0];
@@ -1576,7 +1658,11 @@ async function generatingToolCallLogic({
1576
1658
  return callDelegate(setting, checkpoint, eventPayload);
1577
1659
  }
1578
1660
  }
1579
- var metaInstruction = dedent`
1661
+
1662
+ // src/messages/instruction-message.ts
1663
+ import { createId as createId6 } from "@paralleldrive/cuid2";
1664
+ import { dedent as dedent2 } from "ts-dedent";
1665
+ var metaInstruction = dedent2`
1580
1666
  IMPORTANT:
1581
1667
  You are NOT an "interactive" AI agent.
1582
1668
  From the start of the agent loop until the completion of the task,
@@ -1620,7 +1706,7 @@ var metaInstruction = dedent`
1620
1706
  - Current working directory is ${process.cwd()}
1621
1707
  `;
1622
1708
  function createInstructionMessage(expert, experts) {
1623
- const instruction = dedent`
1709
+ const instruction = dedent2`
1624
1710
  You are Perstack, an AI expert that tackles tasks requested by users by utilizing all available tools.
1625
1711
 
1626
1712
  (The following information describes your nature and role as an AI, the mechanisms of the AI system, and other meta-cognitive aspects.)
@@ -1648,12 +1734,12 @@ function createInstructionMessage(expert, experts) {
1648
1734
  type: "instructionMessage",
1649
1735
  contents: [
1650
1736
  {
1651
- id: createId(),
1737
+ id: createId6(),
1652
1738
  type: "textPart",
1653
1739
  text: instruction
1654
1740
  }
1655
1741
  ],
1656
- id: createId(),
1742
+ id: createId6(),
1657
1743
  cache: true
1658
1744
  };
1659
1745
  }
@@ -1662,7 +1748,7 @@ function getSkillRules(expert) {
1662
1748
  if (!skill.rule) {
1663
1749
  return acc;
1664
1750
  }
1665
- return dedent`
1751
+ return dedent2`
1666
1752
  ${acc}
1667
1753
 
1668
1754
  "${skill.name}" skill rules:
@@ -1676,7 +1762,7 @@ function getDelegateRules(expert, experts) {
1676
1762
  if (!delegate) {
1677
1763
  return acc;
1678
1764
  }
1679
- return dedent`
1765
+ return dedent2`
1680
1766
  ${acc}
1681
1767
 
1682
1768
  About "${delegate.name}":
@@ -1731,6 +1817,9 @@ async function preparingForStepLogic({
1731
1817
  messages: checkpoint.messages
1732
1818
  });
1733
1819
  }
1820
+
1821
+ // src/states/resolving-image-file.ts
1822
+ import { readFile as readFile2 } from "fs/promises";
1734
1823
  async function resolvingImageFileLogic({
1735
1824
  setting,
1736
1825
  checkpoint,
@@ -1755,7 +1844,7 @@ async function resolvingImageFileLogic({
1755
1844
  continue;
1756
1845
  }
1757
1846
  const { path: path2, mimeType, size } = imageInfo;
1758
- const file = await readFile(path2).then((buffer) => ({
1847
+ const file = await readFile2(path2).then((buffer) => ({
1759
1848
  encodedData: buffer.toString("base64"),
1760
1849
  mimeType,
1761
1850
  size
@@ -1779,6 +1868,9 @@ async function resolvingImageFileLogic({
1779
1868
  ]
1780
1869
  });
1781
1870
  }
1871
+
1872
+ // src/states/resolving-pdf-file.ts
1873
+ import { readFile as readFile3 } from "fs/promises";
1782
1874
  async function resolvingPdfFileLogic({
1783
1875
  setting,
1784
1876
  checkpoint,
@@ -1803,7 +1895,7 @@ async function resolvingPdfFileLogic({
1803
1895
  continue;
1804
1896
  }
1805
1897
  const { path: path2, mimeType, size } = pdfInfo;
1806
- const file = await readFile(path2).then((buffer) => ({
1898
+ const file = await readFile3(path2).then((buffer) => ({
1807
1899
  encodedData: buffer.toString("base64"),
1808
1900
  mimeType,
1809
1901
  size
@@ -2168,55 +2260,58 @@ var StateMachineLogics = {
2168
2260
  CallingDelegate: callingDelegateLogic,
2169
2261
  FinishingStep: finishingStepLogic
2170
2262
  };
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()
2263
+
2264
+ // src/schemas/perstack-toml.ts
2265
+ import { z as z4 } from "zod";
2266
+ var PerstackConfigSchema = z4.object({
2267
+ model: z4.string().optional(),
2268
+ temperature: z4.number().optional(),
2269
+ maxSteps: z4.number().optional(),
2270
+ maxRetries: z4.number().optional(),
2271
+ experts: z4.record(
2272
+ z4.string(),
2273
+ z4.object({
2274
+ version: z4.string().optional(),
2275
+ minRuntimeVersion: z4.string().optional(),
2276
+ description: z4.string().optional(),
2277
+ instruction: z4.string(),
2278
+ skills: z4.record(
2279
+ z4.string(),
2280
+ z4.discriminatedUnion("type", [
2281
+ z4.object({
2282
+ type: z4.literal("mcpStdioSkill"),
2283
+ description: z4.string().optional(),
2284
+ rule: z4.string().optional(),
2285
+ pick: z4.array(z4.string()).optional(),
2286
+ omit: z4.array(z4.string()).optional(),
2287
+ command: z4.string(),
2288
+ packageName: z4.string().optional(),
2289
+ args: z4.array(z4.string()).optional(),
2290
+ requiredEnv: z4.array(z4.string()).optional()
2196
2291
  }),
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()
2292
+ z4.object({
2293
+ type: z4.literal("mcpSseSkill"),
2294
+ description: z4.string().optional(),
2295
+ rule: z4.string().optional(),
2296
+ pick: z4.array(z4.string()).optional(),
2297
+ omit: z4.array(z4.string()).optional(),
2298
+ endpoint: z4.string()
2204
2299
  }),
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()
2300
+ z4.object({
2301
+ type: z4.literal("interactiveSkill"),
2302
+ description: z4.string().optional(),
2303
+ rule: z4.string().optional(),
2304
+ tools: z4.record(
2305
+ z4.string(),
2306
+ z4.object({
2307
+ description: z4.string().optional(),
2308
+ inputJsonSchema: z4.string()
2214
2309
  })
2215
2310
  )
2216
2311
  })
2217
2312
  ])
2218
2313
  ).optional(),
2219
- delegates: z.array(z.string()).optional()
2314
+ delegates: z4.array(z4.string()).optional()
2220
2315
  })
2221
2316
  ).optional()
2222
2317
  });
@@ -2248,7 +2343,7 @@ async function run(runInput, options) {
2248
2343
  experts
2249
2344
  },
2250
2345
  initialCheckpoint: checkpoint ?? {
2251
- id: createId(),
2346
+ id: createId7(),
2252
2347
  runId: setting.runId,
2253
2348
  expert: {
2254
2349
  key: setting.expertKey,
@@ -2316,7 +2411,7 @@ async function run(runInput, options) {
2316
2411
  };
2317
2412
  checkpoint = {
2318
2413
  ...await retrieveCheckpoint(setting.runId, checkpointId),
2319
- id: createId(),
2414
+ id: createId7(),
2320
2415
  stepNumber: runResultCheckpoint.stepNumber + 1,
2321
2416
  usage: runResultCheckpoint.usage
2322
2417
  };
@@ -2340,7 +2435,7 @@ async function run(runInput, options) {
2340
2435
  }
2341
2436
  };
2342
2437
  checkpoint = {
2343
- id: createId(),
2438
+ id: createId7(),
2344
2439
  runId: setting.runId,
2345
2440
  status: "init",
2346
2441
  stepNumber: runResultCheckpoint.stepNumber + 1,
@@ -2408,12 +2503,12 @@ async function storeRunSetting(setting) {
2408
2503
  const runDir = getRunDir(setting.runId);
2409
2504
  if (existsSync(runDir)) {
2410
2505
  const runSettingPath = path.resolve(runDir, "run-setting.json");
2411
- const runSetting = JSON.parse(await readFile(runSettingPath, "utf-8"));
2506
+ const runSetting = JSON.parse(await readFile4(runSettingPath, "utf-8"));
2412
2507
  runSetting.updatedAt = Date.now();
2413
- await writeFile(runSettingPath, JSON.stringify(runSetting, null, 2), "utf-8");
2508
+ await writeFile2(runSettingPath, JSON.stringify(runSetting, null, 2), "utf-8");
2414
2509
  } else {
2415
- await mkdir(runDir, { recursive: true });
2416
- await writeFile(
2510
+ await mkdir2(runDir, { recursive: true });
2511
+ await writeFile2(
2417
2512
  path.resolve(runDir, "run-setting.json"),
2418
2513
  JSON.stringify(setting, null, 2),
2419
2514
  "utf-8"
@@ -2427,13 +2522,16 @@ async function parseConfig(config) {
2427
2522
  const toml = TOML.parse(config ?? "");
2428
2523
  return PerstackConfigSchema.parse(toml);
2429
2524
  }
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) => {
2525
+
2526
+ // src/schemas/command-run.ts
2527
+ import { z as z5 } from "zod";
2528
+ var RunInputSchema = z5.object({
2529
+ expertKey: z5.string(),
2530
+ query: z5.string(),
2531
+ options: z5.object({
2532
+ config: z5.string().optional(),
2533
+ model: z5.string().optional(),
2534
+ temperature: z5.string().optional().transform((value) => {
2437
2535
  if (value === void 0) {
2438
2536
  return void 0;
2439
2537
  }
@@ -2443,7 +2541,7 @@ var RunInputSchema = z.object({
2443
2541
  }
2444
2542
  return parsedValue;
2445
2543
  }),
2446
- maxSteps: z.string().optional().transform((value) => {
2544
+ maxSteps: z5.string().optional().transform((value) => {
2447
2545
  if (value === void 0) {
2448
2546
  return void 0;
2449
2547
  }
@@ -2453,7 +2551,7 @@ var RunInputSchema = z.object({
2453
2551
  }
2454
2552
  return parsedValue;
2455
2553
  }),
2456
- maxRetries: z.string().optional().transform((value) => {
2554
+ maxRetries: z5.string().optional().transform((value) => {
2457
2555
  if (value === void 0) {
2458
2556
  return void 0;
2459
2557
  }
@@ -2463,10 +2561,66 @@ var RunInputSchema = z.object({
2463
2561
  }
2464
2562
  return parsedValue;
2465
2563
  }),
2466
- runId: z.string().optional()
2564
+ runId: z5.string().optional()
2467
2565
  })
2468
2566
  });
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
2567
+ export {
2568
+ CheckpointSchema,
2569
+ CheckpointStatusSchema,
2570
+ ExpertMessageSchema,
2571
+ ExpertSchema,
2572
+ FileBinaryPartSchema,
2573
+ FileInlinePartSchema,
2574
+ FileUrlPartSchema,
2575
+ ImageBinaryPartSchema,
2576
+ ImageInlinePartSchema,
2577
+ ImageUrlPartSchema,
2578
+ InstructionMessageSchema,
2579
+ InteractiveSkillSchema,
2580
+ InteractiveToolSchema,
2581
+ McpSseSkillSchema,
2582
+ McpStdioSkillSchema,
2583
+ MessageSchema,
2584
+ PerstackConfigSchema,
2585
+ RegistryV1ExpertsGetResponseSchema,
2586
+ RunInputSchema,
2587
+ RunParamsSchema,
2588
+ StateMachineLogics,
2589
+ TextPartSchema,
2590
+ ToolCallPartSchema,
2591
+ ToolMessageSchema,
2592
+ ToolResultPartSchema,
2593
+ UsageSchema,
2594
+ UserMessageSchema,
2595
+ attemptCompletion,
2596
+ callDelegate,
2597
+ callInteractiveTool,
2598
+ callTool,
2599
+ completeRun,
2600
+ continueToNextStep,
2601
+ createEvent,
2602
+ defaultEventListener,
2603
+ expertKeyRegex,
2604
+ expertNameRegex,
2605
+ finishToolCall,
2606
+ getRunDir,
2607
+ maxNameLength,
2608
+ parseConfig,
2609
+ parseExpertKey,
2610
+ resolveImageFile,
2611
+ resolvePdfFile,
2612
+ resolveThought,
2613
+ resolveToolResult,
2614
+ retry,
2615
+ run,
2616
+ runtimeStateMachine,
2617
+ skillNameRegex,
2618
+ startGeneration,
2619
+ startRun,
2620
+ stopRunByDelegate,
2621
+ stopRunByExceededMaxSteps,
2622
+ stopRunByInteractiveTool,
2623
+ tagNameRegex,
2624
+ versionRegex
2625
+ };
2472
2626
  //# sourceMappingURL=index.js.map