@shareai-lab/kode-sdk 1.0.0-beta.0 → 1.0.0-beta.2
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/core/agent.js +59 -2
- package/dist/core/events.js +7 -1
- package/package.json +5 -1
package/dist/core/agent.js
CHANGED
|
@@ -458,18 +458,75 @@ class Agent {
|
|
|
458
458
|
this.events.emitEvent({ type: 'state', state: 'BUSY' });
|
|
459
459
|
try {
|
|
460
460
|
await this.hooks.runPreModel(this.messages);
|
|
461
|
-
|
|
461
|
+
// Stream response
|
|
462
|
+
const contentBlocks = [];
|
|
463
|
+
let usage;
|
|
464
|
+
const stream = this.provider.stream(this.messages, {
|
|
462
465
|
tools: this.getToolSchemas(),
|
|
463
466
|
system: this.system,
|
|
464
467
|
maxTokens: this.maxTokens,
|
|
465
468
|
temperature: this.temperature,
|
|
466
469
|
});
|
|
470
|
+
let currentTextIndex = -1;
|
|
471
|
+
let currentToolIndex = -1;
|
|
472
|
+
for await (const chunk of stream) {
|
|
473
|
+
if (chunk.type === 'content_block_start') {
|
|
474
|
+
if (chunk.content_block?.type === 'text') {
|
|
475
|
+
currentTextIndex = chunk.index;
|
|
476
|
+
contentBlocks[currentTextIndex] = { type: 'text', text: '' };
|
|
477
|
+
}
|
|
478
|
+
else if (chunk.content_block?.type === 'tool_use') {
|
|
479
|
+
currentToolIndex = chunk.index;
|
|
480
|
+
contentBlocks[currentToolIndex] = {
|
|
481
|
+
type: 'tool_use',
|
|
482
|
+
id: chunk.content_block.id,
|
|
483
|
+
name: chunk.content_block.name,
|
|
484
|
+
input: {},
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
else if (chunk.type === 'content_block_delta') {
|
|
489
|
+
if (chunk.delta?.type === 'text_delta') {
|
|
490
|
+
const text = chunk.delta.text || '';
|
|
491
|
+
if (currentTextIndex >= 0) {
|
|
492
|
+
contentBlocks[currentTextIndex].text += text;
|
|
493
|
+
// Emit text_chunk event
|
|
494
|
+
this.events.emitEvent({ type: 'text_chunk', delta: text });
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
else if (chunk.delta?.type === 'input_json_delta') {
|
|
498
|
+
const json = chunk.delta.partial_json || '';
|
|
499
|
+
if (currentToolIndex >= 0) {
|
|
500
|
+
const block = contentBlocks[currentToolIndex];
|
|
501
|
+
const currentJson = JSON.stringify(block.input || {});
|
|
502
|
+
const prefix = currentJson === '{}' ? '' : currentJson.slice(0, -1);
|
|
503
|
+
try {
|
|
504
|
+
block.input = JSON.parse(prefix + json);
|
|
505
|
+
}
|
|
506
|
+
catch {
|
|
507
|
+
// Partial JSON, will complete in next chunk
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else if (chunk.type === 'message_delta' && chunk.usage) {
|
|
513
|
+
usage = {
|
|
514
|
+
input_tokens: usage?.input_tokens || 0,
|
|
515
|
+
output_tokens: (usage?.output_tokens || 0) + chunk.usage.output_tokens
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
const response = {
|
|
520
|
+
role: 'assistant',
|
|
521
|
+
content: contentBlocks,
|
|
522
|
+
usage,
|
|
523
|
+
};
|
|
467
524
|
await this.hooks.runPostModel(response);
|
|
468
525
|
this.messages.push({
|
|
469
526
|
role: 'assistant',
|
|
470
527
|
content: response.content,
|
|
471
528
|
});
|
|
472
|
-
// Emit text events
|
|
529
|
+
// Emit complete text events
|
|
473
530
|
const textBlocks = response.content.filter((c) => c.type === 'text');
|
|
474
531
|
for (const block of textBlocks) {
|
|
475
532
|
this.events.emitEvent({ type: 'text', text: block.text });
|
package/dist/core/events.js
CHANGED
|
@@ -55,16 +55,22 @@ class EventBus extends events_1.EventEmitter {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
|
+
const self = this;
|
|
58
59
|
return {
|
|
59
60
|
[Symbol.asyncIterator]: () => ({
|
|
60
61
|
next: async () => {
|
|
61
62
|
const event = await subscriber.next();
|
|
62
63
|
if (!event) {
|
|
63
|
-
|
|
64
|
+
self.subscribers.delete(subscriber);
|
|
64
65
|
return { done: true, value: undefined };
|
|
65
66
|
}
|
|
66
67
|
return { done: false, value: event };
|
|
67
68
|
},
|
|
69
|
+
return: async () => {
|
|
70
|
+
subscriber.close();
|
|
71
|
+
self.subscribers.delete(subscriber);
|
|
72
|
+
return { done: true, value: undefined };
|
|
73
|
+
},
|
|
68
74
|
}),
|
|
69
75
|
};
|
|
70
76
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shareai-lab/kode-sdk",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Event-driven Agent Model Client SDK for building long-running, collaborative AI agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"author": "",
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@shareai-lab/kode-sdk": "^1.0.0-beta.1",
|
|
35
36
|
"@types/node": "^20.0.0",
|
|
36
37
|
"ts-node": "^10.9.0",
|
|
37
38
|
"typescript": "^5.3.0"
|
|
@@ -39,6 +40,9 @@
|
|
|
39
40
|
"engines": {
|
|
40
41
|
"node": ">=18.0.0"
|
|
41
42
|
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
42
46
|
"files": [
|
|
43
47
|
"dist",
|
|
44
48
|
"README.md",
|