@polka-codes/core 0.7.15 → 0.7.17

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.
@@ -1,5 +1,6 @@
1
1
  import type { Anthropic } from '@anthropic-ai/sdk';
2
2
  import type OpenAI from 'openai';
3
+ import { z } from 'zod';
3
4
 
4
5
  declare abstract class AgentBase {
5
6
  #private;
@@ -11,9 +12,11 @@ declare abstract class AgentBase {
11
12
  start(prompt: string): Promise<ExitReason>;
12
13
  step(promp: string, messages?: MessageParam[]): Promise<AssistantMessageContent[]>;
13
14
  handleStepResponse(response: AssistantMessageContent[], messages?: MessageParam[]): Promise<{
14
- replay: string;
15
+ type: "reply";
16
+ message: string;
15
17
  } | {
16
- exit: ExitReason;
18
+ type: "exit";
19
+ reason: ExitReason;
17
20
  }>;
18
21
  continueTask(userMessage: string): Promise<ExitReason>;
19
22
  protected abstract onBeforeInvokeTool(name: string, args: Record<string, string>): Promise<ToolResponse | undefined>;
@@ -390,6 +393,293 @@ export { CommandProvider }
390
393
  export { CommandProvider as CommandProvider_alias_1 }
391
394
  export { CommandProvider as CommandProvider_alias_2 }
392
395
 
396
+ declare type Config = z.infer<typeof configSchema>;
397
+ export { Config }
398
+ export { Config as Config_alias_1 }
399
+
400
+ declare const configSchema: z.ZodObject<{
401
+ agent: z.ZodOptional<z.ZodString>;
402
+ prices: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodObject<{
403
+ inputPrice: z.ZodOptional<z.ZodNumber>;
404
+ outputPrice: z.ZodOptional<z.ZodNumber>;
405
+ cacheWritesPrice: z.ZodOptional<z.ZodNumber>;
406
+ cacheReadsPrice: z.ZodOptional<z.ZodNumber>;
407
+ }, "strip", z.ZodTypeAny, {
408
+ inputPrice?: number | undefined;
409
+ outputPrice?: number | undefined;
410
+ cacheWritesPrice?: number | undefined;
411
+ cacheReadsPrice?: number | undefined;
412
+ }, {
413
+ inputPrice?: number | undefined;
414
+ outputPrice?: number | undefined;
415
+ cacheWritesPrice?: number | undefined;
416
+ cacheReadsPrice?: number | undefined;
417
+ }>>>>;
418
+ providers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
419
+ apiKey: z.ZodOptional<z.ZodString>;
420
+ defaultModel: z.ZodOptional<z.ZodString>;
421
+ defaultParameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
422
+ }, "strip", z.ZodTypeAny, {
423
+ apiKey?: string | undefined;
424
+ defaultModel?: string | undefined;
425
+ defaultParameters?: Record<string, any> | undefined;
426
+ }, {
427
+ apiKey?: string | undefined;
428
+ defaultModel?: string | undefined;
429
+ defaultParameters?: Record<string, any> | undefined;
430
+ }>>>;
431
+ defaultProvider: z.ZodOptional<z.ZodString>;
432
+ defaultModel: z.ZodOptional<z.ZodString>;
433
+ defaultParameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
434
+ maxMessageCount: z.ZodOptional<z.ZodNumber>;
435
+ budget: z.ZodOptional<z.ZodNumber>;
436
+ scripts: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodObject<{
437
+ command: z.ZodString;
438
+ description: z.ZodString;
439
+ }, "strip", z.ZodTypeAny, {
440
+ command: string;
441
+ description: string;
442
+ }, {
443
+ command: string;
444
+ description: string;
445
+ }>]>>>;
446
+ agents: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<z.objectUtil.extendShape<{
447
+ provider: z.ZodOptional<z.ZodString>;
448
+ model: z.ZodOptional<z.ZodString>;
449
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
450
+ }, {
451
+ initialContext: z.ZodOptional<z.ZodObject<{
452
+ maxFileCount: z.ZodOptional<z.ZodNumber>;
453
+ excludes: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
454
+ }, "strip", z.ZodTypeAny, {
455
+ maxFileCount?: number | undefined;
456
+ excludes?: string[] | undefined;
457
+ }, {
458
+ maxFileCount?: number | undefined;
459
+ excludes?: string[] | undefined;
460
+ }>>;
461
+ }>, "strip", z.ZodTypeAny, {
462
+ provider?: string | undefined;
463
+ model?: string | undefined;
464
+ parameters?: Record<string, any> | undefined;
465
+ initialContext?: {
466
+ maxFileCount?: number | undefined;
467
+ excludes?: string[] | undefined;
468
+ } | undefined;
469
+ }, {
470
+ provider?: string | undefined;
471
+ model?: string | undefined;
472
+ parameters?: Record<string, any> | undefined;
473
+ initialContext?: {
474
+ maxFileCount?: number | undefined;
475
+ excludes?: string[] | undefined;
476
+ } | undefined;
477
+ }>>>;
478
+ commands: z.ZodOptional<z.ZodObject<{
479
+ default: z.ZodOptional<z.ZodObject<{
480
+ provider: z.ZodOptional<z.ZodString>;
481
+ model: z.ZodOptional<z.ZodString>;
482
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
483
+ }, "strip", z.ZodTypeAny, {
484
+ provider?: string | undefined;
485
+ model?: string | undefined;
486
+ parameters?: Record<string, any> | undefined;
487
+ }, {
488
+ provider?: string | undefined;
489
+ model?: string | undefined;
490
+ parameters?: Record<string, any> | undefined;
491
+ }>>;
492
+ }, "strip", z.ZodObject<{
493
+ provider: z.ZodOptional<z.ZodString>;
494
+ model: z.ZodOptional<z.ZodString>;
495
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
496
+ }, "strip", z.ZodTypeAny, {
497
+ provider?: string | undefined;
498
+ model?: string | undefined;
499
+ parameters?: Record<string, any> | undefined;
500
+ }, {
501
+ provider?: string | undefined;
502
+ model?: string | undefined;
503
+ parameters?: Record<string, any> | undefined;
504
+ }>, z.objectOutputType<{
505
+ default: z.ZodOptional<z.ZodObject<{
506
+ provider: z.ZodOptional<z.ZodString>;
507
+ model: z.ZodOptional<z.ZodString>;
508
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
509
+ }, "strip", z.ZodTypeAny, {
510
+ provider?: string | undefined;
511
+ model?: string | undefined;
512
+ parameters?: Record<string, any> | undefined;
513
+ }, {
514
+ provider?: string | undefined;
515
+ model?: string | undefined;
516
+ parameters?: Record<string, any> | undefined;
517
+ }>>;
518
+ }, z.ZodObject<{
519
+ provider: z.ZodOptional<z.ZodString>;
520
+ model: z.ZodOptional<z.ZodString>;
521
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
522
+ }, "strip", z.ZodTypeAny, {
523
+ provider?: string | undefined;
524
+ model?: string | undefined;
525
+ parameters?: Record<string, any> | undefined;
526
+ }, {
527
+ provider?: string | undefined;
528
+ model?: string | undefined;
529
+ parameters?: Record<string, any> | undefined;
530
+ }>, "strip">, z.objectInputType<{
531
+ default: z.ZodOptional<z.ZodObject<{
532
+ provider: z.ZodOptional<z.ZodString>;
533
+ model: z.ZodOptional<z.ZodString>;
534
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
535
+ }, "strip", z.ZodTypeAny, {
536
+ provider?: string | undefined;
537
+ model?: string | undefined;
538
+ parameters?: Record<string, any> | undefined;
539
+ }, {
540
+ provider?: string | undefined;
541
+ model?: string | undefined;
542
+ parameters?: Record<string, any> | undefined;
543
+ }>>;
544
+ }, z.ZodObject<{
545
+ provider: z.ZodOptional<z.ZodString>;
546
+ model: z.ZodOptional<z.ZodString>;
547
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
548
+ }, "strip", z.ZodTypeAny, {
549
+ provider?: string | undefined;
550
+ model?: string | undefined;
551
+ parameters?: Record<string, any> | undefined;
552
+ }, {
553
+ provider?: string | undefined;
554
+ model?: string | undefined;
555
+ parameters?: Record<string, any> | undefined;
556
+ }>, "strip">>>;
557
+ rules: z.ZodOptional<z.ZodUnion<[z.ZodOptional<z.ZodArray<z.ZodString, "many">>, z.ZodString]>>;
558
+ excludeFiles: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
559
+ }, "strict", z.ZodTypeAny, {
560
+ agent?: string | undefined;
561
+ prices?: Record<string, Record<string, {
562
+ inputPrice?: number | undefined;
563
+ outputPrice?: number | undefined;
564
+ cacheWritesPrice?: number | undefined;
565
+ cacheReadsPrice?: number | undefined;
566
+ }>> | undefined;
567
+ defaultModel?: string | undefined;
568
+ defaultParameters?: Record<string, any> | undefined;
569
+ providers?: Record<string, {
570
+ apiKey?: string | undefined;
571
+ defaultModel?: string | undefined;
572
+ defaultParameters?: Record<string, any> | undefined;
573
+ }> | undefined;
574
+ defaultProvider?: string | undefined;
575
+ maxMessageCount?: number | undefined;
576
+ budget?: number | undefined;
577
+ scripts?: Record<string, string | {
578
+ command: string;
579
+ description: string;
580
+ }> | undefined;
581
+ agents?: Record<string, {
582
+ provider?: string | undefined;
583
+ model?: string | undefined;
584
+ parameters?: Record<string, any> | undefined;
585
+ initialContext?: {
586
+ maxFileCount?: number | undefined;
587
+ excludes?: string[] | undefined;
588
+ } | undefined;
589
+ }> | undefined;
590
+ commands?: z.objectOutputType<{
591
+ default: z.ZodOptional<z.ZodObject<{
592
+ provider: z.ZodOptional<z.ZodString>;
593
+ model: z.ZodOptional<z.ZodString>;
594
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
595
+ }, "strip", z.ZodTypeAny, {
596
+ provider?: string | undefined;
597
+ model?: string | undefined;
598
+ parameters?: Record<string, any> | undefined;
599
+ }, {
600
+ provider?: string | undefined;
601
+ model?: string | undefined;
602
+ parameters?: Record<string, any> | undefined;
603
+ }>>;
604
+ }, z.ZodObject<{
605
+ provider: z.ZodOptional<z.ZodString>;
606
+ model: z.ZodOptional<z.ZodString>;
607
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
608
+ }, "strip", z.ZodTypeAny, {
609
+ provider?: string | undefined;
610
+ model?: string | undefined;
611
+ parameters?: Record<string, any> | undefined;
612
+ }, {
613
+ provider?: string | undefined;
614
+ model?: string | undefined;
615
+ parameters?: Record<string, any> | undefined;
616
+ }>, "strip"> | undefined;
617
+ rules?: string | string[] | undefined;
618
+ excludeFiles?: string[] | undefined;
619
+ }, {
620
+ agent?: string | undefined;
621
+ prices?: Record<string, Record<string, {
622
+ inputPrice?: number | undefined;
623
+ outputPrice?: number | undefined;
624
+ cacheWritesPrice?: number | undefined;
625
+ cacheReadsPrice?: number | undefined;
626
+ }>> | undefined;
627
+ defaultModel?: string | undefined;
628
+ defaultParameters?: Record<string, any> | undefined;
629
+ providers?: Record<string, {
630
+ apiKey?: string | undefined;
631
+ defaultModel?: string | undefined;
632
+ defaultParameters?: Record<string, any> | undefined;
633
+ }> | undefined;
634
+ defaultProvider?: string | undefined;
635
+ maxMessageCount?: number | undefined;
636
+ budget?: number | undefined;
637
+ scripts?: Record<string, string | {
638
+ command: string;
639
+ description: string;
640
+ }> | undefined;
641
+ agents?: Record<string, {
642
+ provider?: string | undefined;
643
+ model?: string | undefined;
644
+ parameters?: Record<string, any> | undefined;
645
+ initialContext?: {
646
+ maxFileCount?: number | undefined;
647
+ excludes?: string[] | undefined;
648
+ } | undefined;
649
+ }> | undefined;
650
+ commands?: z.objectInputType<{
651
+ default: z.ZodOptional<z.ZodObject<{
652
+ provider: z.ZodOptional<z.ZodString>;
653
+ model: z.ZodOptional<z.ZodString>;
654
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
655
+ }, "strip", z.ZodTypeAny, {
656
+ provider?: string | undefined;
657
+ model?: string | undefined;
658
+ parameters?: Record<string, any> | undefined;
659
+ }, {
660
+ provider?: string | undefined;
661
+ model?: string | undefined;
662
+ parameters?: Record<string, any> | undefined;
663
+ }>>;
664
+ }, z.ZodObject<{
665
+ provider: z.ZodOptional<z.ZodString>;
666
+ model: z.ZodOptional<z.ZodString>;
667
+ parameters: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
668
+ }, "strip", z.ZodTypeAny, {
669
+ provider?: string | undefined;
670
+ model?: string | undefined;
671
+ parameters?: Record<string, any> | undefined;
672
+ }, {
673
+ provider?: string | undefined;
674
+ model?: string | undefined;
675
+ parameters?: Record<string, any> | undefined;
676
+ }>, "strip"> | undefined;
677
+ rules?: string | string[] | undefined;
678
+ excludeFiles?: string[] | undefined;
679
+ }>;
680
+ export { configSchema }
681
+ export { configSchema as configSchema_alias_1 }
682
+
393
683
  export declare function convertToAnthropicMessage(completion: OpenAI.Chat.Completions.ChatCompletion): Anthropic.Messages.Message;
394
684
 
395
685
  export declare function convertToOpenAiMessages(anthropicMessages: Anthropic.Messages.MessageParam[]): OpenAI.Chat.ChatCompletionMessageParam[];
package/dist/index.d.ts CHANGED
@@ -109,6 +109,8 @@ export { ToolResponseDelegate } from './_tsup-dts-rollup.js';
109
109
  export { ToolResponse } from './_tsup-dts-rollup.js';
110
110
  export { ToolHandler } from './_tsup-dts-rollup.js';
111
111
  export { getAvailableTools_alias_1 as getAvailableTools } from './_tsup-dts-rollup.js';
112
+ export { configSchema_alias_1 as configSchema } from './_tsup-dts-rollup.js';
113
+ export { Config_alias_1 as Config } from './_tsup-dts-rollup.js';
112
114
  export { getString } from './_tsup-dts-rollup.js';
113
115
  export { getStringArray } from './_tsup-dts-rollup.js';
114
116
  export { getBoolean } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -2257,11 +2257,11 @@ ${agents}`;
2257
2257
  }
2258
2258
  const response = await this.#request(nextRequest);
2259
2259
  const resp = await this.#handleResponse(response);
2260
- if ("exit" in resp) {
2261
- this.#callback({ kind: "EndTask" /* EndTask */, agent: this, exitReason: resp.exit });
2262
- return resp.exit;
2260
+ if (resp.type === "exit") {
2261
+ this.#callback({ kind: "EndTask" /* EndTask */, agent: this, exitReason: resp.reason });
2262
+ return resp.reason;
2263
2263
  }
2264
- nextRequest = resp.replay;
2264
+ nextRequest = resp.message;
2265
2265
  }
2266
2266
  }
2267
2267
  async continueTask(userMessage) {
@@ -2333,7 +2333,7 @@ ${agents}`;
2333
2333
  if (toolReponses.length > 0) {
2334
2334
  break outer;
2335
2335
  }
2336
- return { exit: toolResp };
2336
+ return { type: "exit", reason: toolResp };
2337
2337
  case "Invalid" /* Invalid */:
2338
2338
  await this.#callback({ kind: "ToolInvalid" /* ToolInvalid */, agent: this, tool: content.name });
2339
2339
  toolReponses.push({ tool: content.name, response: toolResp.message });
@@ -2344,7 +2344,7 @@ ${agents}`;
2344
2344
  break outer;
2345
2345
  case "Interrupted" /* Interrupted */:
2346
2346
  await this.#callback({ kind: "ToolInterrupted" /* ToolInterrupted */, agent: this, tool: content.name });
2347
- return { exit: toolResp };
2347
+ return { type: "exit", reason: toolResp };
2348
2348
  case "HandOver" /* HandOver */:
2349
2349
  if (toolReponses.length > 0) {
2350
2350
  break outer;
@@ -2358,7 +2358,7 @@ ${agents}`;
2358
2358
  context: toolResp.context,
2359
2359
  files: toolResp.files
2360
2360
  });
2361
- return { exit: toolResp };
2361
+ return { type: "exit", reason: toolResp };
2362
2362
  case "Delegate" /* Delegate */:
2363
2363
  if (toolReponses.length > 0) {
2364
2364
  continue;
@@ -2372,17 +2372,17 @@ ${agents}`;
2372
2372
  context: toolResp.context,
2373
2373
  files: toolResp.files
2374
2374
  });
2375
- return { exit: toolResp };
2375
+ return { type: "exit", reason: toolResp };
2376
2376
  }
2377
2377
  break;
2378
2378
  }
2379
2379
  }
2380
2380
  }
2381
2381
  if (toolReponses.length === 0) {
2382
- return { replay: responsePrompts.requireUseTool };
2382
+ return { type: "reply", message: responsePrompts.requireUseTool };
2383
2383
  }
2384
2384
  const finalResp = toolReponses.map(({ tool, response: response2 }) => responsePrompts.toolResults(tool, response2)).join("\n\n");
2385
- return { replay: finalResp };
2385
+ return { type: "reply", message: finalResp };
2386
2386
  }
2387
2387
  async #invokeTool(name, args) {
2388
2388
  try {
@@ -3407,6 +3407,65 @@ var generateGitCommitMessage = makeTool(generateGitCommitMessage_default);
3407
3407
  var generateGithubPullRequestDetails = makeTool(generateGithubPullRequestDetails_default);
3408
3408
  var generateProjectConfig = makeAgentTool(generateProjectConfig_default);
3409
3409
  var createNewProject = makeAgentTool(createNewProject_default);
3410
+
3411
+ // src/config.ts
3412
+ import { z } from "zod";
3413
+ var providerModelSchema = z.object({
3414
+ provider: z.string().optional(),
3415
+ model: z.string().optional(),
3416
+ parameters: z.record(z.string(), z.any()).optional()
3417
+ });
3418
+ var agentSchema = providerModelSchema.extend({
3419
+ initialContext: z.object({
3420
+ maxFileCount: z.number().int().positive().optional(),
3421
+ excludes: z.array(z.string()).optional()
3422
+ }).optional()
3423
+ });
3424
+ var configSchema = z.object({
3425
+ agent: z.string().optional(),
3426
+ prices: z.record(
3427
+ z.string(),
3428
+ // provider
3429
+ z.record(
3430
+ z.string(),
3431
+ // model
3432
+ z.object({
3433
+ inputPrice: z.number().optional(),
3434
+ outputPrice: z.number().optional(),
3435
+ cacheWritesPrice: z.number().optional(),
3436
+ cacheReadsPrice: z.number().optional()
3437
+ })
3438
+ )
3439
+ ).optional(),
3440
+ providers: z.record(
3441
+ z.string(),
3442
+ z.object({
3443
+ apiKey: z.string().optional(),
3444
+ defaultModel: z.string().optional(),
3445
+ defaultParameters: z.record(z.string(), z.any()).optional()
3446
+ })
3447
+ ).optional(),
3448
+ defaultProvider: z.string().optional(),
3449
+ defaultModel: z.string().optional(),
3450
+ defaultParameters: z.record(z.string(), z.any()).optional(),
3451
+ maxMessageCount: z.number().int().positive().optional(),
3452
+ budget: z.number().positive().optional(),
3453
+ scripts: z.record(
3454
+ z.string(),
3455
+ z.string().or(
3456
+ z.object({
3457
+ command: z.string(),
3458
+ description: z.string()
3459
+ })
3460
+ )
3461
+ ).optional(),
3462
+ agents: z.record(z.string(), agentSchema).optional(),
3463
+ commands: z.object({
3464
+ default: providerModelSchema.optional()
3465
+ }).catchall(providerModelSchema).optional(),
3466
+ rules: z.array(z.string()).optional().or(z.string()).optional(),
3467
+ excludeFiles: z.array(z.string()).optional()
3468
+ }).strict();
3410
3469
  export {
3411
3470
  AgentBase,
3412
3471
  AiServiceBase,
@@ -3433,6 +3492,7 @@ export {
3433
3492
  capabilities,
3434
3493
  codeFixerAgentInfo,
3435
3494
  coderAgentInfo,
3495
+ configSchema,
3436
3496
  createNewProject,
3437
3497
  createService,
3438
3498
  customInstructions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.7.15",
3
+ "version": "0.7.17",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",
@@ -17,6 +17,7 @@
17
17
  "dependencies": {
18
18
  "@anthropic-ai/sdk": "0.39.0",
19
19
  "lodash": "^4.17.21",
20
- "openai": "^4.80.0"
20
+ "openai": "^4.80.0",
21
+ "zod": "^3.24.2"
21
22
  }
22
23
  }