@polka-codes/core 0.7.4 → 0.7.6

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.
@@ -8,10 +8,7 @@ declare abstract class AgentBase {
8
8
  protected readonly handlers: Record<string, FullToolInfo>;
9
9
  protected readonly messages: MessageParam[];
10
10
  constructor(name: string, ai: AiServiceBase, config: AgentBaseConfig);
11
- startTask({ task, context }: {
12
- task: string;
13
- context?: string;
14
- }): Promise<ExitReason>;
11
+ start(prompt: string): Promise<ExitReason>;
15
12
  continueTask(userMessage: string): Promise<ExitReason>;
16
13
  protected abstract onBeforeInvokeTool(name: string, args: Record<string, string>): Promise<ToolResponse | undefined>;
17
14
  get model(): {
@@ -82,6 +79,7 @@ declare interface AiServiceOptions {
82
79
  baseUrl?: string;
83
80
  usageMeter: UsageMeter;
84
81
  enableCache?: boolean;
82
+ parameters: Record<string, any>;
85
83
  }
86
84
  export { AiServiceOptions }
87
85
  export { AiServiceOptions as AiServiceOptions_alias_1 }
@@ -182,6 +180,7 @@ declare const anthropicModels: {
182
180
  readonly outputPrice: 15;
183
181
  readonly cacheWritesPrice: 3.75;
184
182
  readonly cacheReadsPrice: 0.3;
183
+ readonly reasoning: true;
185
184
  };
186
185
  readonly 'claude-3-5-sonnet-20241022': {
187
186
  readonly maxTokens: 8192;
@@ -239,9 +238,15 @@ export declare class AnthropicService extends AiServiceBase {
239
238
  sendImpl(systemPrompt: string, messages: MessageParam[]): ApiStream;
240
239
  }
241
240
 
242
- export declare type ApiStream = AsyncGenerator<ApiStreamChunk>;
241
+ declare type ApiStream = AsyncGenerator<ApiStreamChunk>;
242
+ export { ApiStream }
243
+ export { ApiStream as ApiStream_alias_1 }
244
+ export { ApiStream as ApiStream_alias_2 }
243
245
 
244
- export declare type ApiStreamChunk = ApiStreamTextChunk | ApiStreamUsageChunk | ApiStreamReasoningTextChunk;
246
+ declare type ApiStreamChunk = ApiStreamTextChunk | ApiStreamUsageChunk | ApiStreamReasoningTextChunk;
247
+ export { ApiStreamChunk }
248
+ export { ApiStreamChunk as ApiStreamChunk_alias_1 }
249
+ export { ApiStreamChunk as ApiStreamChunk_alias_2 }
245
250
 
246
251
  export declare interface ApiStreamReasoningTextChunk {
247
252
  type: 'reasoning';
@@ -1541,6 +1546,7 @@ declare interface ModelInfo {
1541
1546
  outputPrice?: number;
1542
1547
  cacheWritesPrice?: number;
1543
1548
  cacheReadsPrice?: number;
1549
+ reasoning?: boolean;
1544
1550
  }
1545
1551
  export { ModelInfo }
1546
1552
  export { ModelInfo as ModelInfo_alias_1 }
@@ -1558,6 +1564,7 @@ declare const modelInfos: {
1558
1564
  readonly outputPrice: 15;
1559
1565
  readonly cacheWritesPrice: 3.75;
1560
1566
  readonly cacheReadsPrice: 0.3;
1567
+ readonly reasoning: true;
1561
1568
  };
1562
1569
  readonly 'claude-3-5-sonnet-20241022': {
1563
1570
  readonly maxTokens: 8192;
@@ -1633,7 +1640,6 @@ declare class MultiAgent {
1633
1640
  startTask(options: {
1634
1641
  agentName: string;
1635
1642
  task: string;
1636
- context?: string;
1637
1643
  }): Promise<ExitReason>;
1638
1644
  continueTask(userMessage: string): Promise<ExitReason>;
1639
1645
  get hasActiveAgent(): boolean;
@@ -1644,7 +1650,7 @@ export { MultiAgent as MultiAgent_alias_2 }
1644
1650
 
1645
1651
  declare type MultiAgentConfig = {
1646
1652
  createAgent: (name: string) => Promise<AgentBase>;
1647
- getContext?: (name: string, context?: string, files?: string[]) => Promise<string>;
1653
+ getPrompt?: (name: string, task: string, context?: string, files?: string[]) => Promise<string>;
1648
1654
  };
1649
1655
  export { MultiAgentConfig }
1650
1656
  export { MultiAgentConfig as MultiAgentConfig_alias_1 }
package/dist/index.d.ts CHANGED
@@ -7,6 +7,8 @@ export { AiServiceOptions } from './_tsup-dts-rollup.js';
7
7
  export { AiServiceBase } from './_tsup-dts-rollup.js';
8
8
  export { ModelInfo } from './_tsup-dts-rollup.js';
9
9
  export { ApiUsage } from './_tsup-dts-rollup.js';
10
+ export { ApiStream } from './_tsup-dts-rollup.js';
11
+ export { ApiStreamChunk } from './_tsup-dts-rollup.js';
10
12
  export { allAgents } from './_tsup-dts-rollup.js';
11
13
  export { AgentNameType } from './_tsup-dts-rollup.js';
12
14
  export { TaskEventKind } from './_tsup-dts-rollup.js';
package/dist/index.js CHANGED
@@ -74,7 +74,8 @@ var anthropicModels = {
74
74
  inputPrice: 3,
75
75
  outputPrice: 15,
76
76
  cacheWritesPrice: 3.75,
77
- cacheReadsPrice: 0.3
77
+ cacheReadsPrice: 0.3,
78
+ reasoning: true
78
79
  },
79
80
  "claude-3-5-sonnet-20241022": {
80
81
  maxTokens: 8192,
@@ -183,6 +184,14 @@ var AnthropicService = class extends AiServiceBase {
183
184
  let stream;
184
185
  const modelId = this.model.id;
185
186
  const cacheControl = this.#options.enableCache ? { type: "ephemeral" } : void 0;
187
+ let temperature = 0;
188
+ let thinkingBudgetTokens = 0;
189
+ if (this.model.info.reasoning) {
190
+ thinkingBudgetTokens = this.#options.parameters.thinkingBudgetTokens ?? 0;
191
+ }
192
+ if (thinkingBudgetTokens > 0) {
193
+ temperature = void 0;
194
+ }
186
195
  switch (modelId) {
187
196
  // 'latest' alias does not support cache_control
188
197
  case "claude-3-7-sonnet-20250219":
@@ -201,7 +210,8 @@ var AnthropicService = class extends AiServiceBase {
201
210
  stream = await this.#client.messages.create({
202
211
  model: modelId,
203
212
  max_tokens: this.model.info.maxTokens || 8192,
204
- temperature: 0,
213
+ thinking: thinkingBudgetTokens ? { type: "enabled", budget_tokens: thinkingBudgetTokens } : void 0,
214
+ temperature,
205
215
  system: [
206
216
  {
207
217
  text: systemPrompt,
@@ -241,8 +251,6 @@ var AnthropicService = class extends AiServiceBase {
241
251
  temperature: 0,
242
252
  system: [{ text: systemPrompt, type: "text" }],
243
253
  messages,
244
- // tools,
245
- // tool_choice: { type: "auto" },
246
254
  stream: true
247
255
  });
248
256
  break;
@@ -285,6 +293,18 @@ var AnthropicService = class extends AiServiceBase {
285
293
  text: chunk.content_block.text
286
294
  };
287
295
  break;
296
+ case "thinking":
297
+ yield {
298
+ type: "reasoning",
299
+ text: chunk.content_block.thinking
300
+ };
301
+ break;
302
+ case "redacted_thinking":
303
+ yield {
304
+ type: "reasoning",
305
+ text: "[Redacted by providered]"
306
+ };
307
+ break;
288
308
  }
289
309
  break;
290
310
  case "content_block_delta":
@@ -295,6 +315,12 @@ var AnthropicService = class extends AiServiceBase {
295
315
  text: chunk.delta.text
296
316
  };
297
317
  break;
318
+ case "thinking_delta":
319
+ yield {
320
+ type: "reasoning",
321
+ text: chunk.delta.thinking
322
+ };
323
+ break;
298
324
  }
299
325
  break;
300
326
  case "content_block_stop":
@@ -354,10 +380,18 @@ function convertToOpenAiMessages(anthropicMessages) {
354
380
  role: "user",
355
381
  content: nonToolMessages.map((part) => {
356
382
  if (part.type === "image") {
383
+ if (part.source.type === "base64") {
384
+ return {
385
+ type: "image_url",
386
+ image_url: {
387
+ url: `data:${part.source.media_type};base64,${part.source.data}`
388
+ }
389
+ };
390
+ }
357
391
  return {
358
392
  type: "image_url",
359
393
  image_url: {
360
- url: `data:${part.source.media_type};base64,${part.source.data}`
394
+ url: part.source.url
361
395
  }
362
396
  };
363
397
  }
@@ -618,6 +652,20 @@ var OpenRouterService = class extends AiServiceBase {
618
652
  maxTokens = 8192;
619
653
  break;
620
654
  }
655
+ let reasoning = {};
656
+ switch (this.model.id) {
657
+ case "anthropic/claude-3.7-sonnet":
658
+ case "anthropic/claude-3.7-sonnet:beta":
659
+ case "anthropic/claude-3.7-sonnet:thinking":
660
+ case "anthropic/claude-3-7-sonnet":
661
+ case "anthropic/claude-3-7-sonnet:beta": {
662
+ const budget_tokens = this.#options.parameters.thinkingBudgetTokens || 0;
663
+ if (budget_tokens > 0) {
664
+ reasoning = { max_tokens: budget_tokens };
665
+ }
666
+ break;
667
+ }
668
+ }
621
669
  let shouldApplyMiddleOutTransform = !this.model.info.supportsPromptCache;
622
670
  if (this.model.id === "deepseek/deepseek-chat") {
623
671
  shouldApplyMiddleOutTransform = true;
@@ -629,7 +677,8 @@ var OpenRouterService = class extends AiServiceBase {
629
677
  temperature: 0,
630
678
  stream: true,
631
679
  transforms: shouldApplyMiddleOutTransform ? ["middle-out"] : void 0,
632
- include_reasoning: true
680
+ include_reasoning: true,
681
+ ...reasoning
633
682
  });
634
683
  let genId;
635
684
  for await (const chunk of stream) {
@@ -2160,14 +2209,9 @@ ${agents}`;
2160
2209
  async #callback(event) {
2161
2210
  await this.config.callback?.(event);
2162
2211
  }
2163
- async startTask({ task, context }) {
2164
- let text = `<task>${task}</task>`;
2165
- if (context) {
2166
- text += `
2167
- <context>${context}</context>`;
2168
- }
2212
+ async start(prompt5) {
2169
2213
  this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
2170
- return await this.#processLoop(text);
2214
+ return await this.#processLoop(prompt5);
2171
2215
  }
2172
2216
  async #processLoop(userMessage) {
2173
2217
  let nextRequest = userMessage;
@@ -2202,19 +2246,23 @@ ${agents}`;
2202
2246
  for (let i = 0; i < retryCount; i++) {
2203
2247
  currentAssistantMessage = "";
2204
2248
  const stream = this.ai.send(this.config.systemPrompt, this.messages);
2205
- for await (const chunk of stream) {
2206
- switch (chunk.type) {
2207
- case "usage":
2208
- await this.#callback({ kind: "Usage" /* Usage */, agent: this });
2209
- break;
2210
- case "text":
2211
- currentAssistantMessage += chunk.text;
2212
- await this.#callback({ kind: "Text" /* Text */, agent: this, newText: chunk.text });
2213
- break;
2214
- case "reasoning":
2215
- await this.#callback({ kind: "Reasoning" /* Reasoning */, agent: this, newText: chunk.text });
2216
- break;
2249
+ try {
2250
+ for await (const chunk of stream) {
2251
+ switch (chunk.type) {
2252
+ case "usage":
2253
+ await this.#callback({ kind: "Usage" /* Usage */, agent: this });
2254
+ break;
2255
+ case "text":
2256
+ currentAssistantMessage += chunk.text;
2257
+ await this.#callback({ kind: "Text" /* Text */, agent: this, newText: chunk.text });
2258
+ break;
2259
+ case "reasoning":
2260
+ await this.#callback({ kind: "Reasoning" /* Reasoning */, agent: this, newText: chunk.text });
2261
+ break;
2262
+ }
2217
2263
  }
2264
+ } catch (error) {
2265
+ console.error("Error in stream:", error);
2218
2266
  }
2219
2267
  if (currentAssistantMessage) {
2220
2268
  break;
@@ -2891,12 +2939,12 @@ var MultiAgent = class {
2891
2939
  switch (exitReason.type) {
2892
2940
  case "HandOver" /* HandOver */: {
2893
2941
  this.#agents.pop();
2894
- const newContext = await this.#config.getContext?.(exitReason.agentName, exitReason.context, exitReason.files);
2895
- return await this.#startTask(exitReason.agentName, exitReason.task, newContext);
2942
+ const prompt5 = await this.#config.getPrompt?.(exitReason.agentName, exitReason.task, exitReason.context, exitReason.files) ?? exitReason.task;
2943
+ return await this.#startTask(exitReason.agentName, prompt5);
2896
2944
  }
2897
2945
  case "Delegate" /* Delegate */: {
2898
- const newContext = await this.#config.getContext?.(exitReason.agentName, exitReason.context, exitReason.files);
2899
- const delegateResult = await this.#startTask(exitReason.agentName, exitReason.task, newContext);
2946
+ const prompt5 = await this.#config.getPrompt?.(exitReason.agentName, exitReason.task, exitReason.context, exitReason.files) ?? exitReason.task;
2947
+ const delegateResult = await this.#startTask(exitReason.agentName, prompt5);
2900
2948
  switch (delegateResult.type) {
2901
2949
  case "HandOver" /* HandOver */:
2902
2950
  case "Delegate" /* Delegate */:
@@ -2917,20 +2965,17 @@ var MultiAgent = class {
2917
2965
  return exitReason;
2918
2966
  }
2919
2967
  }
2920
- async #startTask(agentName, task, context) {
2968
+ async #startTask(agentName, task) {
2921
2969
  const newAgent = await this.#config.createAgent(agentName);
2922
2970
  this.#agents.push(newAgent);
2923
- const exitReason = await newAgent.startTask({
2924
- task,
2925
- context
2926
- });
2971
+ const exitReason = await newAgent.start(task);
2927
2972
  return await this.#handleTaskResult(exitReason);
2928
2973
  }
2929
2974
  async startTask(options) {
2930
2975
  if (this.#agents.length > 0) {
2931
2976
  throw new Error("An active agent already exists");
2932
2977
  }
2933
- return this.#startTask(options.agentName, options.task, options.context);
2978
+ return this.#startTask(options.agentName, options.task);
2934
2979
  }
2935
2980
  async continueTask(userMessage) {
2936
2981
  if (!this.#agents.length) {
@@ -3305,8 +3350,8 @@ var executeAgentTool = async (definition, agent, params) => {
3305
3350
  }
3306
3351
  const exitReason = await agent.startTask({
3307
3352
  agentName: definition.agent,
3308
- task: definition.prompt,
3309
- context: definition.formatInput(params)
3353
+ task: `<task>${definition.prompt}</task>
3354
+ <context>${definition.formatInput(params)}</context>`
3310
3355
  });
3311
3356
  if (exitReason.type === "Exit" /* Exit */) {
3312
3357
  return definition.parseOutput(exitReason.message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polka-codes/core",
3
- "version": "0.7.4",
3
+ "version": "0.7.6",
4
4
  "license": "AGPL-3.0",
5
5
  "author": "github@polka.codes",
6
6
  "type": "module",
@@ -15,7 +15,7 @@
15
15
  "build": "tsup src/index.ts --experimental-dts --format esm --clean"
16
16
  },
17
17
  "dependencies": {
18
- "@anthropic-ai/sdk": "^0.36.2",
18
+ "@anthropic-ai/sdk": "0.39.0",
19
19
  "lodash": "^4.17.21",
20
20
  "openai": "^4.80.0"
21
21
  }