@polka-codes/core 0.7.12 → 0.7.14

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.
@@ -6,9 +6,15 @@ declare abstract class AgentBase {
6
6
  protected readonly ai: AiServiceBase;
7
7
  protected readonly config: Readonly<AgentBaseConfig>;
8
8
  protected readonly handlers: Record<string, FullToolInfo>;
9
- protected readonly messages: MessageParam[];
10
- constructor(name: string, ai: AiServiceBase, config: AgentBaseConfig);
9
+ constructor(name: string, ai: AiServiceBase, config: AgentBaseConfig, messages?: MessageParam[]);
10
+ get messages(): Readonly<MessageParam[]>;
11
11
  start(prompt: string): Promise<ExitReason>;
12
+ step(promp: string, messages?: MessageParam[]): Promise<AssistantMessageContent[]>;
13
+ handleStepResponse(response: AssistantMessageContent[], messages?: MessageParam[]): Promise<{
14
+ replay: string;
15
+ } | {
16
+ exit: ExitReason;
17
+ }>;
12
18
  continueTask(userMessage: string): Promise<ExitReason>;
13
19
  protected abstract onBeforeInvokeTool(name: string, args: Record<string, string>): Promise<ToolResponse | undefined>;
14
20
  get model(): {
@@ -297,7 +303,10 @@ export { ArchitectAgentOptions }
297
303
  export { ArchitectAgentOptions as ArchitectAgentOptions_alias_1 }
298
304
  export { ArchitectAgentOptions as ArchitectAgentOptions_alias_2 }
299
305
 
300
- export declare type AssistantMessageContent = TextContent | ToolUse;
306
+ declare type AssistantMessageContent = TextContent | ToolUse;
307
+ export { AssistantMessageContent }
308
+ export { AssistantMessageContent as AssistantMessageContent_alias_1 }
309
+ export { AssistantMessageContent as AssistantMessageContent_alias_2 }
301
310
 
302
311
  export declare const basePrompt = "You are a highly skilled software engineer specializing in debugging and fixing code issues. You have extensive experience with:\n- Type systems and type checking\n- Test frameworks and debugging test failures\n- Code quality tools and best practices\n- Systematic debugging approaches";
303
312
 
@@ -1745,7 +1754,10 @@ declare type Output = {
1745
1754
  * // {type: "text", content: "Here are the results"}
1746
1755
  * // ]
1747
1756
  */
1748
- export declare function parseAssistantMessage(assistantMessage: string, tools: ToolInfo[], toolNamePrefix: string): AssistantMessageContent[];
1757
+ declare function parseAssistantMessage(assistantMessage: string, tools: ToolInfo[], toolNamePrefix: string): AssistantMessageContent[];
1758
+ export { parseAssistantMessage }
1759
+ export { parseAssistantMessage as parseAssistantMessage_alias_1 }
1760
+ export { parseAssistantMessage as parseAssistantMessage_alias_2 }
1749
1761
 
1750
1762
  declare enum PermissionLevel {
1751
1763
  None = 0,
@@ -1948,10 +1960,13 @@ export { TaskEventUsageExceeded }
1948
1960
  export { TaskEventUsageExceeded as TaskEventUsageExceeded_alias_1 }
1949
1961
  export { TaskEventUsageExceeded as TaskEventUsageExceeded_alias_2 }
1950
1962
 
1951
- export declare interface TextContent {
1963
+ declare interface TextContent {
1952
1964
  type: 'text';
1953
1965
  content: string;
1954
1966
  }
1967
+ export { TextContent }
1968
+ export { TextContent as TextContent_alias_1 }
1969
+ export { TextContent as TextContent_alias_2 }
1955
1970
 
1956
1971
  declare type ToolExample = {
1957
1972
  description: string;
@@ -2392,6 +2407,7 @@ export { ToolResponseError as ToolResponseError_alias_1 }
2392
2407
  declare type ToolResponseExit = {
2393
2408
  type: ToolResponseType.Exit;
2394
2409
  message: string;
2410
+ object?: any;
2395
2411
  };
2396
2412
  export { ToolResponseExit }
2397
2413
  export { ToolResponseExit as ToolResponseExit_alias_1 }
@@ -2439,11 +2455,14 @@ declare enum ToolResponseType {
2439
2455
  export { ToolResponseType }
2440
2456
  export { ToolResponseType as ToolResponseType_alias_1 }
2441
2457
 
2442
- export declare interface ToolUse {
2458
+ declare interface ToolUse {
2443
2459
  type: 'tool_use';
2444
2460
  name: string;
2445
2461
  params: Record<string, string>;
2446
2462
  }
2463
+ export { ToolUse }
2464
+ export { ToolUse as ToolUse_alias_1 }
2465
+ export { ToolUse as ToolUse_alias_2 }
2447
2466
 
2448
2467
  declare const toolUsePrompt: (tools: ToolInfo[], toolNamePrefix: string) => string;
2449
2468
  export { toolUsePrompt }
@@ -2469,9 +2488,12 @@ declare class UsageMeter {
2469
2488
  incrementMessageCount(count?: number): void;
2470
2489
  isLimitExceeded(): {
2471
2490
  messageCount: boolean;
2491
+ maxMessageCount: number;
2472
2492
  cost: boolean;
2493
+ maxCost: number;
2473
2494
  result: boolean;
2474
2495
  };
2496
+ checkLimit(): void;
2475
2497
  /**
2476
2498
  * Get current usage totals
2477
2499
  */
package/dist/index.d.ts CHANGED
@@ -51,6 +51,10 @@ export { systemInformation } from './_tsup-dts-rollup.js';
51
51
  export { customInstructions } from './_tsup-dts-rollup.js';
52
52
  export { customScripts } from './_tsup-dts-rollup.js';
53
53
  export { responsePrompts } from './_tsup-dts-rollup.js';
54
+ export { parseAssistantMessage } from './_tsup-dts-rollup.js';
55
+ export { TextContent } from './_tsup-dts-rollup.js';
56
+ export { ToolUse } from './_tsup-dts-rollup.js';
57
+ export { AssistantMessageContent } from './_tsup-dts-rollup.js';
54
58
  export { allTools } from './_tsup-dts-rollup.js';
55
59
  export { FilesystemProvider } from './_tsup-dts-rollup.js';
56
60
  export { CommandProvider } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ var AiServiceBase = class {
11
11
  this.usageMeter = usageMeter;
12
12
  }
13
13
  async *send(systemPrompt, messages) {
14
+ this.usageMeter.checkLimit();
14
15
  this.usageMeter.incrementMessageCount();
15
16
  const stream = this.sendImpl(systemPrompt, messages);
16
17
  for await (const chunk of stream) {
@@ -23,6 +24,7 @@ var AiServiceBase = class {
23
24
  }
24
25
  }
25
26
  async request(systemPrompt, messages) {
27
+ this.usageMeter.checkLimit();
26
28
  this.usageMeter.incrementMessageCount();
27
29
  const stream = this.sendImpl(systemPrompt, messages);
28
30
  const usage = {
@@ -783,10 +785,20 @@ var UsageMeter = class {
783
785
  const cost = this.#usage.totalCost >= this.maxCost;
784
786
  return {
785
787
  messageCount,
788
+ maxMessageCount: this.maxMessageCount,
786
789
  cost,
790
+ maxCost: this.maxCost,
787
791
  result: messageCount || cost
788
792
  };
789
793
  }
794
+ checkLimit() {
795
+ const result = this.isLimitExceeded();
796
+ if (result.result) {
797
+ throw new Error(
798
+ `Usage limit exceeded. Message count: ${result.messageCount}/${result.maxMessageCount}, cost: ${result.cost}/${result.maxCost}`
799
+ );
800
+ }
801
+ }
790
802
  /**
791
803
  * Get current usage totals
792
804
  */
@@ -2185,9 +2197,10 @@ var AgentBase = class {
2185
2197
  ai;
2186
2198
  config;
2187
2199
  handlers;
2188
- messages = [];
2189
- constructor(name, ai, config) {
2200
+ #messages;
2201
+ constructor(name, ai, config, messages = []) {
2190
2202
  this.ai = ai;
2203
+ this.#messages = messages;
2191
2204
  if (config.agents && config.agents.length > 0) {
2192
2205
  const agents = agentsPrompt(config.agents, name);
2193
2206
  config.systemPrompt += `
@@ -2200,6 +2213,9 @@ ${agents}`;
2200
2213
  }
2201
2214
  this.handlers = handlers;
2202
2215
  }
2216
+ get messages() {
2217
+ return this.#messages;
2218
+ }
2203
2219
  async #callback(event) {
2204
2220
  await this.config.callback?.(event);
2205
2221
  }
@@ -2207,6 +2223,21 @@ ${agents}`;
2207
2223
  this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
2208
2224
  return await this.#processLoop(prompt5);
2209
2225
  }
2226
+ async step(promp, messages) {
2227
+ if (messages) {
2228
+ this.#messages = messages;
2229
+ }
2230
+ if (this.#messages.length === 0) {
2231
+ this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
2232
+ }
2233
+ return await this.#request(promp);
2234
+ }
2235
+ async handleStepResponse(response, messages) {
2236
+ if (messages) {
2237
+ this.#messages = messages;
2238
+ }
2239
+ return this.#handleResponse(response);
2240
+ }
2210
2241
  async #processLoop(userMessage) {
2211
2242
  let nextRequest = userMessage;
2212
2243
  while (true) {
@@ -2231,7 +2262,7 @@ ${agents}`;
2231
2262
  throw new Error("userMessage is missing");
2232
2263
  }
2233
2264
  await this.#callback({ kind: "StartRequest" /* StartRequest */, agent: this, userMessage });
2234
- this.messages.push({
2265
+ this.#messages.push({
2235
2266
  role: "user",
2236
2267
  content: userMessage
2237
2268
  });
@@ -2239,7 +2270,7 @@ ${agents}`;
2239
2270
  const retryCount = 5;
2240
2271
  for (let i = 0; i < retryCount; i++) {
2241
2272
  currentAssistantMessage = "";
2242
- const stream = this.ai.send(this.config.systemPrompt, this.messages);
2273
+ const stream = this.ai.send(this.config.systemPrompt, this.#messages);
2243
2274
  try {
2244
2275
  for await (const chunk of stream) {
2245
2276
  switch (chunk.type) {
@@ -2266,7 +2297,7 @@ ${agents}`;
2266
2297
  if (!currentAssistantMessage) {
2267
2298
  throw new Error("No assistant message received");
2268
2299
  }
2269
- this.messages.push({
2300
+ this.#messages.push({
2270
2301
  role: "assistant",
2271
2302
  content: currentAssistantMessage
2272
2303
  });
@@ -3418,6 +3449,7 @@ export {
3418
3449
  makeTool,
3419
3450
  modelInfos,
3420
3451
  openAiModelInfoSaneDefaults,
3452
+ parseAssistantMessage,
3421
3453
  readFile_default as readFile,
3422
3454
  removeFile_default as removeFile,
3423
3455
  renameFile_default as renameFile,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.7.12",
3
+ "version": "0.7.14",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",